cls_cgroup.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/skbuff.h>
  14. #include <linux/rcupdate.h>
  15. #include <net/rtnetlink.h>
  16. #include <net/pkt_cls.h>
  17. #include <net/sock.h>
  18. #include <net/cls_cgroup.h>
  19. struct cls_cgroup_head {
  20. u32 handle;
  21. struct tcf_exts exts;
  22. struct tcf_ematch_tree ematches;
  23. struct tcf_proto *tp;
  24. struct rcu_head rcu;
  25. };
  26. static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  27. struct tcf_result *res)
  28. {
  29. struct cls_cgroup_head *head = rcu_dereference_bh(tp->root);
  30. u32 classid;
  31. classid = task_cls_state(current)->classid;
  32. /*
  33. * Due to the nature of the classifier it is required to ignore all
  34. * packets originating from softirq context as accessing `current'
  35. * would lead to false results.
  36. *
  37. * This test assumes that all callers of dev_queue_xmit() explicitely
  38. * disable bh. Knowing this, it is possible to detect softirq based
  39. * calls by looking at the number of nested bh disable calls because
  40. * softirqs always disables bh.
  41. */
  42. if (in_serving_softirq()) {
  43. /* If there is an sk_classid we'll use that. */
  44. if (!skb->sk)
  45. return -1;
  46. classid = skb->sk->sk_classid;
  47. }
  48. if (!classid)
  49. return -1;
  50. if (!tcf_em_tree_match(skb, &head->ematches, NULL))
  51. return -1;
  52. res->classid = classid;
  53. res->class = 0;
  54. return tcf_exts_exec(skb, &head->exts, res);
  55. }
  56. static unsigned long cls_cgroup_get(struct tcf_proto *tp, u32 handle)
  57. {
  58. return 0UL;
  59. }
  60. static void cls_cgroup_put(struct tcf_proto *tp, unsigned long f)
  61. {
  62. }
  63. static int cls_cgroup_init(struct tcf_proto *tp)
  64. {
  65. return 0;
  66. }
  67. static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
  68. [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED },
  69. };
  70. static void cls_cgroup_destroy_rcu(struct rcu_head *root)
  71. {
  72. struct cls_cgroup_head *head = container_of(root,
  73. struct cls_cgroup_head,
  74. rcu);
  75. tcf_exts_destroy(&head->exts);
  76. tcf_em_tree_destroy(&head->ematches);
  77. kfree(head);
  78. }
  79. static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
  80. struct tcf_proto *tp, unsigned long base,
  81. u32 handle, struct nlattr **tca,
  82. unsigned long *arg, bool ovr)
  83. {
  84. struct nlattr *tb[TCA_CGROUP_MAX + 1];
  85. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  86. struct cls_cgroup_head *new;
  87. struct tcf_ematch_tree t;
  88. struct tcf_exts e;
  89. int err;
  90. if (!tca[TCA_OPTIONS])
  91. return -EINVAL;
  92. if (!head && !handle)
  93. return -EINVAL;
  94. if (head && handle != head->handle)
  95. return -ENOENT;
  96. new = kzalloc(sizeof(*head), GFP_KERNEL);
  97. if (!new)
  98. return -ENOBUFS;
  99. tcf_exts_init(&new->exts, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
  100. if (head)
  101. new->handle = head->handle;
  102. else
  103. new->handle = handle;
  104. new->tp = tp;
  105. err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS],
  106. cgroup_policy);
  107. if (err < 0)
  108. goto errout;
  109. tcf_exts_init(&e, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
  110. err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &e, ovr);
  111. if (err < 0)
  112. goto errout;
  113. err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t);
  114. if (err < 0) {
  115. tcf_exts_destroy(&e);
  116. goto errout;
  117. }
  118. tcf_exts_change(tp, &new->exts, &e);
  119. tcf_em_tree_change(tp, &new->ematches, &t);
  120. rcu_assign_pointer(tp->root, new);
  121. if (head)
  122. call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
  123. return 0;
  124. errout:
  125. kfree(new);
  126. return err;
  127. }
  128. static void cls_cgroup_destroy(struct tcf_proto *tp)
  129. {
  130. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  131. if (head) {
  132. RCU_INIT_POINTER(tp->root, NULL);
  133. call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
  134. }
  135. }
  136. static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
  137. {
  138. return -EOPNOTSUPP;
  139. }
  140. static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  141. {
  142. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  143. if (arg->count < arg->skip)
  144. goto skip;
  145. if (arg->fn(tp, (unsigned long) head, arg) < 0) {
  146. arg->stop = 1;
  147. return;
  148. }
  149. skip:
  150. arg->count++;
  151. }
  152. static int cls_cgroup_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  153. struct sk_buff *skb, struct tcmsg *t)
  154. {
  155. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  156. unsigned char *b = skb_tail_pointer(skb);
  157. struct nlattr *nest;
  158. t->tcm_handle = head->handle;
  159. nest = nla_nest_start(skb, TCA_OPTIONS);
  160. if (nest == NULL)
  161. goto nla_put_failure;
  162. if (tcf_exts_dump(skb, &head->exts) < 0 ||
  163. tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
  164. goto nla_put_failure;
  165. nla_nest_end(skb, nest);
  166. if (tcf_exts_dump_stats(skb, &head->exts) < 0)
  167. goto nla_put_failure;
  168. return skb->len;
  169. nla_put_failure:
  170. nlmsg_trim(skb, b);
  171. return -1;
  172. }
  173. static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
  174. .kind = "cgroup",
  175. .init = cls_cgroup_init,
  176. .change = cls_cgroup_change,
  177. .classify = cls_cgroup_classify,
  178. .destroy = cls_cgroup_destroy,
  179. .get = cls_cgroup_get,
  180. .put = cls_cgroup_put,
  181. .delete = cls_cgroup_delete,
  182. .walk = cls_cgroup_walk,
  183. .dump = cls_cgroup_dump,
  184. .owner = THIS_MODULE,
  185. };
  186. static int __init init_cgroup_cls(void)
  187. {
  188. return register_tcf_proto_ops(&cls_cgroup_ops);
  189. }
  190. static void __exit exit_cgroup_cls(void)
  191. {
  192. unregister_tcf_proto_ops(&cls_cgroup_ops);
  193. }
  194. module_init(init_cgroup_cls);
  195. module_exit(exit_cgroup_cls);
  196. MODULE_LICENSE("GPL");