cls_cgroup.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/slab.h>
  13. #include <linux/types.h>
  14. #include <linux/string.h>
  15. #include <linux/errno.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/cgroup.h>
  18. #include <linux/rcupdate.h>
  19. #include <net/rtnetlink.h>
  20. #include <net/pkt_cls.h>
  21. #include <net/sock.h>
  22. #include <net/cls_cgroup.h>
  23. static inline struct cgroup_cls_state *cgrp_cls_state(struct cgroup *cgrp)
  24. {
  25. return container_of(cgroup_subsys_state(cgrp, net_cls_subsys_id),
  26. struct cgroup_cls_state, css);
  27. }
  28. static inline struct cgroup_cls_state *task_cls_state(struct task_struct *p)
  29. {
  30. return container_of(task_subsys_state(p, net_cls_subsys_id),
  31. struct cgroup_cls_state, css);
  32. }
  33. static struct cgroup_subsys_state *cgrp_create(struct cgroup *cgrp)
  34. {
  35. struct cgroup_cls_state *cs;
  36. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  37. if (!cs)
  38. return ERR_PTR(-ENOMEM);
  39. if (cgrp->parent)
  40. cs->classid = cgrp_cls_state(cgrp->parent)->classid;
  41. return &cs->css;
  42. }
  43. static void cgrp_destroy(struct cgroup *cgrp)
  44. {
  45. kfree(cgrp_cls_state(cgrp));
  46. }
  47. static u64 read_classid(struct cgroup *cgrp, struct cftype *cft)
  48. {
  49. return cgrp_cls_state(cgrp)->classid;
  50. }
  51. static int write_classid(struct cgroup *cgrp, struct cftype *cft, u64 value)
  52. {
  53. cgrp_cls_state(cgrp)->classid = (u32) value;
  54. return 0;
  55. }
  56. static struct cftype ss_files[] = {
  57. {
  58. .name = "classid",
  59. .read_u64 = read_classid,
  60. .write_u64 = write_classid,
  61. },
  62. { } /* terminate */
  63. };
  64. struct cgroup_subsys net_cls_subsys = {
  65. .name = "net_cls",
  66. .create = cgrp_create,
  67. .destroy = cgrp_destroy,
  68. .subsys_id = net_cls_subsys_id,
  69. .base_cftypes = ss_files,
  70. .module = THIS_MODULE,
  71. /*
  72. * While net_cls cgroup has the rudimentary hierarchy support of
  73. * inheriting the parent's classid on cgroup creation, it doesn't
  74. * properly propagates config changes in ancestors to their
  75. * descendents. A child should follow the parent's configuration
  76. * but be allowed to override it. Fix it and remove the following.
  77. */
  78. .broken_hierarchy = true,
  79. };
  80. struct cls_cgroup_head {
  81. u32 handle;
  82. struct tcf_exts exts;
  83. struct tcf_ematch_tree ematches;
  84. };
  85. static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  86. struct tcf_result *res)
  87. {
  88. struct cls_cgroup_head *head = tp->root;
  89. u32 classid;
  90. rcu_read_lock();
  91. classid = task_cls_state(current)->classid;
  92. rcu_read_unlock();
  93. /*
  94. * Due to the nature of the classifier it is required to ignore all
  95. * packets originating from softirq context as accessing `current'
  96. * would lead to false results.
  97. *
  98. * This test assumes that all callers of dev_queue_xmit() explicitely
  99. * disable bh. Knowing this, it is possible to detect softirq based
  100. * calls by looking at the number of nested bh disable calls because
  101. * softirqs always disables bh.
  102. */
  103. if (in_serving_softirq()) {
  104. /* If there is an sk_classid we'll use that. */
  105. if (!skb->sk)
  106. return -1;
  107. classid = skb->sk->sk_classid;
  108. }
  109. if (!classid)
  110. return -1;
  111. if (!tcf_em_tree_match(skb, &head->ematches, NULL))
  112. return -1;
  113. res->classid = classid;
  114. res->class = 0;
  115. return tcf_exts_exec(skb, &head->exts, res);
  116. }
  117. static unsigned long cls_cgroup_get(struct tcf_proto *tp, u32 handle)
  118. {
  119. return 0UL;
  120. }
  121. static void cls_cgroup_put(struct tcf_proto *tp, unsigned long f)
  122. {
  123. }
  124. static int cls_cgroup_init(struct tcf_proto *tp)
  125. {
  126. return 0;
  127. }
  128. static const struct tcf_ext_map cgroup_ext_map = {
  129. .action = TCA_CGROUP_ACT,
  130. .police = TCA_CGROUP_POLICE,
  131. };
  132. static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
  133. [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED },
  134. };
  135. static int cls_cgroup_change(struct sk_buff *in_skb,
  136. struct tcf_proto *tp, unsigned long base,
  137. u32 handle, struct nlattr **tca,
  138. unsigned long *arg)
  139. {
  140. struct nlattr *tb[TCA_CGROUP_MAX + 1];
  141. struct cls_cgroup_head *head = tp->root;
  142. struct tcf_ematch_tree t;
  143. struct tcf_exts e;
  144. int err;
  145. if (!tca[TCA_OPTIONS])
  146. return -EINVAL;
  147. if (head == NULL) {
  148. if (!handle)
  149. return -EINVAL;
  150. head = kzalloc(sizeof(*head), GFP_KERNEL);
  151. if (head == NULL)
  152. return -ENOBUFS;
  153. head->handle = handle;
  154. tcf_tree_lock(tp);
  155. tp->root = head;
  156. tcf_tree_unlock(tp);
  157. }
  158. if (handle != head->handle)
  159. return -ENOENT;
  160. err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS],
  161. cgroup_policy);
  162. if (err < 0)
  163. return err;
  164. err = tcf_exts_validate(tp, tb, tca[TCA_RATE], &e, &cgroup_ext_map);
  165. if (err < 0)
  166. return err;
  167. err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t);
  168. if (err < 0)
  169. return err;
  170. tcf_exts_change(tp, &head->exts, &e);
  171. tcf_em_tree_change(tp, &head->ematches, &t);
  172. return 0;
  173. }
  174. static void cls_cgroup_destroy(struct tcf_proto *tp)
  175. {
  176. struct cls_cgroup_head *head = tp->root;
  177. if (head) {
  178. tcf_exts_destroy(tp, &head->exts);
  179. tcf_em_tree_destroy(tp, &head->ematches);
  180. kfree(head);
  181. }
  182. }
  183. static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
  184. {
  185. return -EOPNOTSUPP;
  186. }
  187. static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  188. {
  189. struct cls_cgroup_head *head = tp->root;
  190. if (arg->count < arg->skip)
  191. goto skip;
  192. if (arg->fn(tp, (unsigned long) head, arg) < 0) {
  193. arg->stop = 1;
  194. return;
  195. }
  196. skip:
  197. arg->count++;
  198. }
  199. static int cls_cgroup_dump(struct tcf_proto *tp, unsigned long fh,
  200. struct sk_buff *skb, struct tcmsg *t)
  201. {
  202. struct cls_cgroup_head *head = tp->root;
  203. unsigned char *b = skb_tail_pointer(skb);
  204. struct nlattr *nest;
  205. t->tcm_handle = head->handle;
  206. nest = nla_nest_start(skb, TCA_OPTIONS);
  207. if (nest == NULL)
  208. goto nla_put_failure;
  209. if (tcf_exts_dump(skb, &head->exts, &cgroup_ext_map) < 0 ||
  210. tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
  211. goto nla_put_failure;
  212. nla_nest_end(skb, nest);
  213. if (tcf_exts_dump_stats(skb, &head->exts, &cgroup_ext_map) < 0)
  214. goto nla_put_failure;
  215. return skb->len;
  216. nla_put_failure:
  217. nlmsg_trim(skb, b);
  218. return -1;
  219. }
  220. static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
  221. .kind = "cgroup",
  222. .init = cls_cgroup_init,
  223. .change = cls_cgroup_change,
  224. .classify = cls_cgroup_classify,
  225. .destroy = cls_cgroup_destroy,
  226. .get = cls_cgroup_get,
  227. .put = cls_cgroup_put,
  228. .delete = cls_cgroup_delete,
  229. .walk = cls_cgroup_walk,
  230. .dump = cls_cgroup_dump,
  231. .owner = THIS_MODULE,
  232. };
  233. static int __init init_cgroup_cls(void)
  234. {
  235. int ret;
  236. ret = cgroup_load_subsys(&net_cls_subsys);
  237. if (ret)
  238. goto out;
  239. ret = register_tcf_proto_ops(&cls_cgroup_ops);
  240. if (ret)
  241. cgroup_unload_subsys(&net_cls_subsys);
  242. out:
  243. return ret;
  244. }
  245. static void __exit exit_cgroup_cls(void)
  246. {
  247. unregister_tcf_proto_ops(&cls_cgroup_ops);
  248. cgroup_unload_subsys(&net_cls_subsys);
  249. }
  250. module_init(init_cgroup_cls);
  251. module_exit(exit_cgroup_cls);
  252. MODULE_LICENSE("GPL");