cls_cgroup.c 7.2 KB

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