cls_cgroup.c 7.4 KB

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