cls_cgroup.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 int cls_cgroup_init(struct tcf_proto *tp)
  61. {
  62. return 0;
  63. }
  64. static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
  65. [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED },
  66. };
  67. static void cls_cgroup_destroy_rcu(struct rcu_head *root)
  68. {
  69. struct cls_cgroup_head *head = container_of(root,
  70. struct cls_cgroup_head,
  71. rcu);
  72. tcf_exts_destroy(&head->exts);
  73. tcf_em_tree_destroy(&head->ematches);
  74. kfree(head);
  75. }
  76. static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
  77. struct tcf_proto *tp, unsigned long base,
  78. u32 handle, struct nlattr **tca,
  79. unsigned long *arg, bool ovr)
  80. {
  81. struct nlattr *tb[TCA_CGROUP_MAX + 1];
  82. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  83. struct cls_cgroup_head *new;
  84. struct tcf_ematch_tree t;
  85. struct tcf_exts e;
  86. int err;
  87. if (!tca[TCA_OPTIONS])
  88. return -EINVAL;
  89. if (!head && !handle)
  90. return -EINVAL;
  91. if (head && handle != head->handle)
  92. return -ENOENT;
  93. new = kzalloc(sizeof(*head), GFP_KERNEL);
  94. if (!new)
  95. return -ENOBUFS;
  96. tcf_exts_init(&new->exts, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
  97. new->handle = handle;
  98. new->tp = tp;
  99. err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS],
  100. cgroup_policy);
  101. if (err < 0)
  102. goto errout;
  103. tcf_exts_init(&e, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
  104. err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &e, ovr);
  105. if (err < 0)
  106. goto errout;
  107. err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t);
  108. if (err < 0) {
  109. tcf_exts_destroy(&e);
  110. goto errout;
  111. }
  112. tcf_exts_change(tp, &new->exts, &e);
  113. tcf_em_tree_change(tp, &new->ematches, &t);
  114. rcu_assign_pointer(tp->root, new);
  115. if (head)
  116. call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
  117. return 0;
  118. errout:
  119. kfree(new);
  120. return err;
  121. }
  122. static bool cls_cgroup_destroy(struct tcf_proto *tp, bool force)
  123. {
  124. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  125. if (!force)
  126. return false;
  127. if (head) {
  128. RCU_INIT_POINTER(tp->root, NULL);
  129. call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
  130. }
  131. return true;
  132. }
  133. static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
  134. {
  135. return -EOPNOTSUPP;
  136. }
  137. static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  138. {
  139. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  140. if (arg->count < arg->skip)
  141. goto skip;
  142. if (arg->fn(tp, (unsigned long) head, arg) < 0) {
  143. arg->stop = 1;
  144. return;
  145. }
  146. skip:
  147. arg->count++;
  148. }
  149. static int cls_cgroup_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  150. struct sk_buff *skb, struct tcmsg *t)
  151. {
  152. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  153. struct nlattr *nest;
  154. t->tcm_handle = head->handle;
  155. nest = nla_nest_start(skb, TCA_OPTIONS);
  156. if (nest == NULL)
  157. goto nla_put_failure;
  158. if (tcf_exts_dump(skb, &head->exts) < 0 ||
  159. tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
  160. goto nla_put_failure;
  161. nla_nest_end(skb, nest);
  162. if (tcf_exts_dump_stats(skb, &head->exts) < 0)
  163. goto nla_put_failure;
  164. return skb->len;
  165. nla_put_failure:
  166. nla_nest_cancel(skb, nest);
  167. return -1;
  168. }
  169. static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
  170. .kind = "cgroup",
  171. .init = cls_cgroup_init,
  172. .change = cls_cgroup_change,
  173. .classify = cls_cgroup_classify,
  174. .destroy = cls_cgroup_destroy,
  175. .get = cls_cgroup_get,
  176. .delete = cls_cgroup_delete,
  177. .walk = cls_cgroup_walk,
  178. .dump = cls_cgroup_dump,
  179. .owner = THIS_MODULE,
  180. };
  181. static int __init init_cgroup_cls(void)
  182. {
  183. return register_tcf_proto_ops(&cls_cgroup_ops);
  184. }
  185. static void __exit exit_cgroup_cls(void)
  186. {
  187. unregister_tcf_proto_ops(&cls_cgroup_ops);
  188. }
  189. module_init(init_cgroup_cls);
  190. module_exit(exit_cgroup_cls);
  191. MODULE_LICENSE("GPL");