cls_cgroup.c 7.3 KB

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