cls_cgroup.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * net/sched/cls_cgroup.c Control Group Classifier
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Thomas Graf <tgraf@suug.ch>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/string.h>
  14. #include <linux/errno.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/cgroup.h>
  17. #include <net/rtnetlink.h>
  18. #include <net/pkt_cls.h>
  19. struct cgroup_cls_state
  20. {
  21. struct cgroup_subsys_state css;
  22. u32 classid;
  23. };
  24. static inline struct cgroup_cls_state *cgrp_cls_state(struct cgroup *cgrp)
  25. {
  26. return container_of(cgroup_subsys_state(cgrp, net_cls_subsys_id),
  27. struct cgroup_cls_state, css);
  28. }
  29. static inline struct cgroup_cls_state *task_cls_state(struct task_struct *p)
  30. {
  31. return container_of(task_subsys_state(p, net_cls_subsys_id),
  32. struct cgroup_cls_state, css);
  33. }
  34. static struct cgroup_subsys_state *cgrp_create(struct cgroup_subsys *ss,
  35. struct cgroup *cgrp)
  36. {
  37. struct cgroup_cls_state *cs;
  38. if (!(cs = kzalloc(sizeof(*cs), GFP_KERNEL)))
  39. return ERR_PTR(-ENOMEM);
  40. if (cgrp->parent)
  41. cs->classid = cgrp_cls_state(cgrp->parent)->classid;
  42. return &cs->css;
  43. }
  44. static void cgrp_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
  45. {
  46. kfree(cgrp_cls_state(cgrp));
  47. }
  48. static u64 read_classid(struct cgroup *cgrp, struct cftype *cft)
  49. {
  50. return cgrp_cls_state(cgrp)->classid;
  51. }
  52. static int write_classid(struct cgroup *cgrp, struct cftype *cft, u64 value)
  53. {
  54. if (!cgroup_lock_live_group(cgrp))
  55. return -ENODEV;
  56. cgrp_cls_state(cgrp)->classid = (u32) value;
  57. cgroup_unlock();
  58. return 0;
  59. }
  60. static struct cftype ss_files[] = {
  61. {
  62. .name = "classid",
  63. .read_u64 = read_classid,
  64. .write_u64 = write_classid,
  65. },
  66. };
  67. static int cgrp_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
  68. {
  69. return cgroup_add_files(cgrp, ss, ss_files, ARRAY_SIZE(ss_files));
  70. }
  71. struct cgroup_subsys net_cls_subsys = {
  72. .name = "net_cls",
  73. .create = cgrp_create,
  74. .destroy = cgrp_destroy,
  75. .populate = cgrp_populate,
  76. .subsys_id = net_cls_subsys_id,
  77. };
  78. struct cls_cgroup_head
  79. {
  80. u32 handle;
  81. struct tcf_exts exts;
  82. struct tcf_ematch_tree ematches;
  83. };
  84. static int cls_cgroup_classify(struct sk_buff *skb, struct tcf_proto *tp,
  85. struct tcf_result *res)
  86. {
  87. struct cls_cgroup_head *head = tp->root;
  88. u32 classid;
  89. /*
  90. * Due to the nature of the classifier it is required to ignore all
  91. * packets originating from softirq context as accessing `current'
  92. * would lead to false results.
  93. *
  94. * This test assumes that all callers of dev_queue_xmit() explicitely
  95. * disable bh. Knowing this, it is possible to detect softirq based
  96. * calls by looking at the number of nested bh disable calls because
  97. * softirqs always disables bh.
  98. */
  99. if (softirq_count() != SOFTIRQ_OFFSET)
  100. return -1;
  101. rcu_read_lock();
  102. classid = task_cls_state(current)->classid;
  103. rcu_read_unlock();
  104. if (!classid)
  105. return -1;
  106. if (!tcf_em_tree_match(skb, &head->ematches, NULL))
  107. return -1;
  108. res->classid = classid;
  109. res->class = 0;
  110. return tcf_exts_exec(skb, &head->exts, res);
  111. }
  112. static unsigned long cls_cgroup_get(struct tcf_proto *tp, u32 handle)
  113. {
  114. return 0UL;
  115. }
  116. static void cls_cgroup_put(struct tcf_proto *tp, unsigned long f)
  117. {
  118. }
  119. static int cls_cgroup_init(struct tcf_proto *tp)
  120. {
  121. return 0;
  122. }
  123. static const struct tcf_ext_map cgroup_ext_map = {
  124. .action = TCA_CGROUP_ACT,
  125. .police = TCA_CGROUP_POLICE,
  126. };
  127. static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
  128. [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED },
  129. };
  130. static int cls_cgroup_change(struct tcf_proto *tp, unsigned long base,
  131. u32 handle, struct nlattr **tca,
  132. unsigned long *arg)
  133. {
  134. struct nlattr *tb[TCA_CGROUP_MAX+1];
  135. struct cls_cgroup_head *head = tp->root;
  136. struct tcf_ematch_tree t;
  137. struct tcf_exts e;
  138. int err;
  139. if (!tca[TCA_OPTIONS])
  140. return -EINVAL;
  141. if (head == NULL) {
  142. if (!handle)
  143. return -EINVAL;
  144. head = kzalloc(sizeof(*head), GFP_KERNEL);
  145. if (head == NULL)
  146. return -ENOBUFS;
  147. head->handle = handle;
  148. tcf_tree_lock(tp);
  149. tp->root = head;
  150. tcf_tree_unlock(tp);
  151. }
  152. if (handle != head->handle)
  153. return -ENOENT;
  154. err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS],
  155. cgroup_policy);
  156. if (err < 0)
  157. return err;
  158. err = tcf_exts_validate(tp, tb, tca[TCA_RATE], &e, &cgroup_ext_map);
  159. if (err < 0)
  160. return err;
  161. err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t);
  162. if (err < 0)
  163. return err;
  164. tcf_exts_change(tp, &head->exts, &e);
  165. tcf_em_tree_change(tp, &head->ematches, &t);
  166. return 0;
  167. }
  168. static void cls_cgroup_destroy(struct tcf_proto *tp)
  169. {
  170. struct cls_cgroup_head *head = tp->root;
  171. if (head) {
  172. tcf_exts_destroy(tp, &head->exts);
  173. tcf_em_tree_destroy(tp, &head->ematches);
  174. kfree(head);
  175. }
  176. }
  177. static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
  178. {
  179. return -EOPNOTSUPP;
  180. }
  181. static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  182. {
  183. struct cls_cgroup_head *head = tp->root;
  184. if (arg->count < arg->skip)
  185. goto skip;
  186. if (arg->fn(tp, (unsigned long) head, arg) < 0) {
  187. arg->stop = 1;
  188. return;
  189. }
  190. skip:
  191. arg->count++;
  192. }
  193. static int cls_cgroup_dump(struct tcf_proto *tp, unsigned long fh,
  194. struct sk_buff *skb, struct tcmsg *t)
  195. {
  196. struct cls_cgroup_head *head = tp->root;
  197. unsigned char *b = skb_tail_pointer(skb);
  198. struct nlattr *nest;
  199. t->tcm_handle = head->handle;
  200. nest = nla_nest_start(skb, TCA_OPTIONS);
  201. if (nest == NULL)
  202. goto nla_put_failure;
  203. if (tcf_exts_dump(skb, &head->exts, &cgroup_ext_map) < 0 ||
  204. tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
  205. goto nla_put_failure;
  206. nla_nest_end(skb, nest);
  207. if (tcf_exts_dump_stats(skb, &head->exts, &cgroup_ext_map) < 0)
  208. goto nla_put_failure;
  209. return skb->len;
  210. nla_put_failure:
  211. nlmsg_trim(skb, b);
  212. return -1;
  213. }
  214. static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
  215. .kind = "cgroup",
  216. .init = cls_cgroup_init,
  217. .change = cls_cgroup_change,
  218. .classify = cls_cgroup_classify,
  219. .destroy = cls_cgroup_destroy,
  220. .get = cls_cgroup_get,
  221. .put = cls_cgroup_put,
  222. .delete = cls_cgroup_delete,
  223. .walk = cls_cgroup_walk,
  224. .dump = cls_cgroup_dump,
  225. .owner = THIS_MODULE,
  226. };
  227. static int __init init_cgroup_cls(void)
  228. {
  229. return register_tcf_proto_ops(&cls_cgroup_ops);
  230. }
  231. static void __exit exit_cgroup_cls(void)
  232. {
  233. unregister_tcf_proto_ops(&cls_cgroup_ops);
  234. }
  235. module_init(init_cgroup_cls);
  236. module_exit(exit_cgroup_cls);
  237. MODULE_LICENSE("GPL");