cls_cgroup.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 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(tp, tb, tca[TCA_RATE], &e, &cgroup_ext_map);
  184. if (err < 0)
  185. return err;
  186. err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t);
  187. if (err < 0)
  188. return err;
  189. tcf_exts_change(tp, &head->exts, &e);
  190. tcf_em_tree_change(tp, &head->ematches, &t);
  191. return 0;
  192. }
  193. static void cls_cgroup_destroy(struct tcf_proto *tp)
  194. {
  195. struct cls_cgroup_head *head = tp->root;
  196. if (head) {
  197. tcf_exts_destroy(tp, &head->exts);
  198. tcf_em_tree_destroy(tp, &head->ematches);
  199. kfree(head);
  200. }
  201. }
  202. static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
  203. {
  204. return -EOPNOTSUPP;
  205. }
  206. static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  207. {
  208. struct cls_cgroup_head *head = tp->root;
  209. if (arg->count < arg->skip)
  210. goto skip;
  211. if (arg->fn(tp, (unsigned long) head, arg) < 0) {
  212. arg->stop = 1;
  213. return;
  214. }
  215. skip:
  216. arg->count++;
  217. }
  218. static int cls_cgroup_dump(struct tcf_proto *tp, unsigned long fh,
  219. struct sk_buff *skb, struct tcmsg *t)
  220. {
  221. struct cls_cgroup_head *head = tp->root;
  222. unsigned char *b = skb_tail_pointer(skb);
  223. struct nlattr *nest;
  224. t->tcm_handle = head->handle;
  225. nest = nla_nest_start(skb, TCA_OPTIONS);
  226. if (nest == NULL)
  227. goto nla_put_failure;
  228. if (tcf_exts_dump(skb, &head->exts, &cgroup_ext_map) < 0 ||
  229. tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
  230. goto nla_put_failure;
  231. nla_nest_end(skb, nest);
  232. if (tcf_exts_dump_stats(skb, &head->exts, &cgroup_ext_map) < 0)
  233. goto nla_put_failure;
  234. return skb->len;
  235. nla_put_failure:
  236. nlmsg_trim(skb, b);
  237. return -1;
  238. }
  239. static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
  240. .kind = "cgroup",
  241. .init = cls_cgroup_init,
  242. .change = cls_cgroup_change,
  243. .classify = cls_cgroup_classify,
  244. .destroy = cls_cgroup_destroy,
  245. .get = cls_cgroup_get,
  246. .put = cls_cgroup_put,
  247. .delete = cls_cgroup_delete,
  248. .walk = cls_cgroup_walk,
  249. .dump = cls_cgroup_dump,
  250. .owner = THIS_MODULE,
  251. };
  252. static int __init init_cgroup_cls(void)
  253. {
  254. int ret;
  255. ret = cgroup_load_subsys(&net_cls_subsys);
  256. if (ret)
  257. goto out;
  258. ret = register_tcf_proto_ops(&cls_cgroup_ops);
  259. if (ret)
  260. cgroup_unload_subsys(&net_cls_subsys);
  261. out:
  262. return ret;
  263. }
  264. static void __exit exit_cgroup_cls(void)
  265. {
  266. unregister_tcf_proto_ops(&cls_cgroup_ops);
  267. cgroup_unload_subsys(&net_cls_subsys);
  268. }
  269. module_init(init_cgroup_cls);
  270. module_exit(exit_cgroup_cls);
  271. MODULE_LICENSE("GPL");