cls_cgroup.c 7.2 KB

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