cls_cgroup.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. };
  24. static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  25. struct tcf_result *res)
  26. {
  27. struct cls_cgroup_head *head = tp->root;
  28. u32 classid;
  29. rcu_read_lock();
  30. classid = task_cls_state(current)->classid;
  31. rcu_read_unlock();
  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 int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
  71. struct tcf_proto *tp, unsigned long base,
  72. u32 handle, struct nlattr **tca,
  73. unsigned long *arg, bool ovr)
  74. {
  75. struct nlattr *tb[TCA_CGROUP_MAX + 1];
  76. struct cls_cgroup_head *head = tp->root;
  77. struct tcf_ematch_tree t;
  78. struct tcf_exts e;
  79. int err;
  80. if (!tca[TCA_OPTIONS])
  81. return -EINVAL;
  82. if (head == NULL) {
  83. if (!handle)
  84. return -EINVAL;
  85. head = kzalloc(sizeof(*head), GFP_KERNEL);
  86. if (head == NULL)
  87. return -ENOBUFS;
  88. tcf_exts_init(&head->exts, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
  89. head->handle = handle;
  90. tcf_tree_lock(tp);
  91. tp->root = head;
  92. tcf_tree_unlock(tp);
  93. }
  94. if (handle != head->handle)
  95. return -ENOENT;
  96. err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS],
  97. cgroup_policy);
  98. if (err < 0)
  99. return err;
  100. tcf_exts_init(&e, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
  101. err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &e, ovr);
  102. if (err < 0)
  103. return err;
  104. err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t);
  105. if (err < 0)
  106. return err;
  107. tcf_exts_change(tp, &head->exts, &e);
  108. tcf_em_tree_change(tp, &head->ematches, &t);
  109. return 0;
  110. }
  111. static void cls_cgroup_destroy(struct tcf_proto *tp)
  112. {
  113. struct cls_cgroup_head *head = tp->root;
  114. if (head) {
  115. tcf_exts_destroy(tp, &head->exts);
  116. tcf_em_tree_destroy(tp, &head->ematches);
  117. kfree(head);
  118. }
  119. }
  120. static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
  121. {
  122. return -EOPNOTSUPP;
  123. }
  124. static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  125. {
  126. struct cls_cgroup_head *head = tp->root;
  127. if (arg->count < arg->skip)
  128. goto skip;
  129. if (arg->fn(tp, (unsigned long) head, arg) < 0) {
  130. arg->stop = 1;
  131. return;
  132. }
  133. skip:
  134. arg->count++;
  135. }
  136. static int cls_cgroup_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  137. struct sk_buff *skb, struct tcmsg *t)
  138. {
  139. struct cls_cgroup_head *head = tp->root;
  140. unsigned char *b = skb_tail_pointer(skb);
  141. struct nlattr *nest;
  142. t->tcm_handle = head->handle;
  143. nest = nla_nest_start(skb, TCA_OPTIONS);
  144. if (nest == NULL)
  145. goto nla_put_failure;
  146. if (tcf_exts_dump(skb, &head->exts) < 0 ||
  147. tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
  148. goto nla_put_failure;
  149. nla_nest_end(skb, nest);
  150. if (tcf_exts_dump_stats(skb, &head->exts) < 0)
  151. goto nla_put_failure;
  152. return skb->len;
  153. nla_put_failure:
  154. nlmsg_trim(skb, b);
  155. return -1;
  156. }
  157. static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
  158. .kind = "cgroup",
  159. .init = cls_cgroup_init,
  160. .change = cls_cgroup_change,
  161. .classify = cls_cgroup_classify,
  162. .destroy = cls_cgroup_destroy,
  163. .get = cls_cgroup_get,
  164. .put = cls_cgroup_put,
  165. .delete = cls_cgroup_delete,
  166. .walk = cls_cgroup_walk,
  167. .dump = cls_cgroup_dump,
  168. .owner = THIS_MODULE,
  169. };
  170. static int __init init_cgroup_cls(void)
  171. {
  172. return register_tcf_proto_ops(&cls_cgroup_ops);
  173. }
  174. static void __exit exit_cgroup_cls(void)
  175. {
  176. unregister_tcf_proto_ops(&cls_cgroup_ops);
  177. }
  178. module_init(init_cgroup_cls);
  179. module_exit(exit_cgroup_cls);
  180. MODULE_LICENSE("GPL");