cls_cgroup.c 7.3 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 *css_cls_state(struct cgroup_subsys_state *css)
  25. {
  26. return css ? container_of(css, struct cgroup_cls_state, css) : NULL;
  27. }
  28. static inline struct cgroup_cls_state *task_cls_state(struct task_struct *p)
  29. {
  30. return css_cls_state(task_css(p, net_cls_subsys_id));
  31. }
  32. static struct cgroup_subsys_state *
  33. cgrp_css_alloc(struct cgroup_subsys_state *parent_css)
  34. {
  35. struct cgroup_cls_state *cs;
  36. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  37. if (!cs)
  38. return ERR_PTR(-ENOMEM);
  39. return &cs->css;
  40. }
  41. static int cgrp_css_online(struct cgroup_subsys_state *css)
  42. {
  43. struct cgroup_cls_state *cs = css_cls_state(css);
  44. struct cgroup_cls_state *parent = css_cls_state(css_parent(css));
  45. if (parent)
  46. cs->classid = parent->classid;
  47. return 0;
  48. }
  49. static void cgrp_css_free(struct cgroup_subsys_state *css)
  50. {
  51. kfree(css_cls_state(css));
  52. }
  53. static int update_classid(const void *v, struct file *file, unsigned n)
  54. {
  55. int err;
  56. struct socket *sock = sock_from_file(file, &err);
  57. if (sock)
  58. sock->sk->sk_classid = (u32)(unsigned long)v;
  59. return 0;
  60. }
  61. static void cgrp_attach(struct cgroup_subsys_state *css,
  62. struct cgroup_taskset *tset)
  63. {
  64. struct task_struct *p;
  65. struct cgroup_cls_state *cs = css_cls_state(css);
  66. void *v = (void *)(unsigned long)cs->classid;
  67. cgroup_taskset_for_each(p, css, tset) {
  68. task_lock(p);
  69. iterate_fd(p->files, 0, update_classid, v);
  70. task_unlock(p);
  71. }
  72. }
  73. static u64 read_classid(struct cgroup_subsys_state *css, struct cftype *cft)
  74. {
  75. return css_cls_state(css)->classid;
  76. }
  77. static int write_classid(struct cgroup_subsys_state *css, struct cftype *cft,
  78. u64 value)
  79. {
  80. css_cls_state(css)->classid = (u32) value;
  81. return 0;
  82. }
  83. static struct cftype ss_files[] = {
  84. {
  85. .name = "classid",
  86. .read_u64 = read_classid,
  87. .write_u64 = write_classid,
  88. },
  89. { } /* terminate */
  90. };
  91. struct cgroup_subsys net_cls_subsys = {
  92. .name = "net_cls",
  93. .css_alloc = cgrp_css_alloc,
  94. .css_online = cgrp_css_online,
  95. .css_free = cgrp_css_free,
  96. .attach = cgrp_attach,
  97. .subsys_id = net_cls_subsys_id,
  98. .base_cftypes = ss_files,
  99. .module = THIS_MODULE,
  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 net *net, 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(net, tp, tb, tca[TCA_RATE], &e,
  186. &cgroup_ext_map);
  187. if (err < 0)
  188. return err;
  189. err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t);
  190. if (err < 0)
  191. return err;
  192. tcf_exts_change(tp, &head->exts, &e);
  193. tcf_em_tree_change(tp, &head->ematches, &t);
  194. return 0;
  195. }
  196. static void cls_cgroup_destroy(struct tcf_proto *tp)
  197. {
  198. struct cls_cgroup_head *head = tp->root;
  199. if (head) {
  200. tcf_exts_destroy(tp, &head->exts);
  201. tcf_em_tree_destroy(tp, &head->ematches);
  202. kfree(head);
  203. }
  204. }
  205. static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
  206. {
  207. return -EOPNOTSUPP;
  208. }
  209. static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  210. {
  211. struct cls_cgroup_head *head = tp->root;
  212. if (arg->count < arg->skip)
  213. goto skip;
  214. if (arg->fn(tp, (unsigned long) head, arg) < 0) {
  215. arg->stop = 1;
  216. return;
  217. }
  218. skip:
  219. arg->count++;
  220. }
  221. static int cls_cgroup_dump(struct tcf_proto *tp, unsigned long fh,
  222. struct sk_buff *skb, struct tcmsg *t)
  223. {
  224. struct cls_cgroup_head *head = tp->root;
  225. unsigned char *b = skb_tail_pointer(skb);
  226. struct nlattr *nest;
  227. t->tcm_handle = head->handle;
  228. nest = nla_nest_start(skb, TCA_OPTIONS);
  229. if (nest == NULL)
  230. goto nla_put_failure;
  231. if (tcf_exts_dump(skb, &head->exts, &cgroup_ext_map) < 0 ||
  232. tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
  233. goto nla_put_failure;
  234. nla_nest_end(skb, nest);
  235. if (tcf_exts_dump_stats(skb, &head->exts, &cgroup_ext_map) < 0)
  236. goto nla_put_failure;
  237. return skb->len;
  238. nla_put_failure:
  239. nlmsg_trim(skb, b);
  240. return -1;
  241. }
  242. static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
  243. .kind = "cgroup",
  244. .init = cls_cgroup_init,
  245. .change = cls_cgroup_change,
  246. .classify = cls_cgroup_classify,
  247. .destroy = cls_cgroup_destroy,
  248. .get = cls_cgroup_get,
  249. .put = cls_cgroup_put,
  250. .delete = cls_cgroup_delete,
  251. .walk = cls_cgroup_walk,
  252. .dump = cls_cgroup_dump,
  253. .owner = THIS_MODULE,
  254. };
  255. static int __init init_cgroup_cls(void)
  256. {
  257. int ret;
  258. ret = cgroup_load_subsys(&net_cls_subsys);
  259. if (ret)
  260. goto out;
  261. ret = register_tcf_proto_ops(&cls_cgroup_ops);
  262. if (ret)
  263. cgroup_unload_subsys(&net_cls_subsys);
  264. out:
  265. return ret;
  266. }
  267. static void __exit exit_cgroup_cls(void)
  268. {
  269. unregister_tcf_proto_ops(&cls_cgroup_ops);
  270. cgroup_unload_subsys(&net_cls_subsys);
  271. }
  272. module_init(init_cgroup_cls);
  273. module_exit(exit_cgroup_cls);
  274. MODULE_LICENSE("GPL");