cls_cgroup.c 6.3 KB

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