cls_cgroup.c 7.4 KB

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