cls_cgroup.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <[email protected]>
  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 = task_get_classid(skb);
  31. if (!classid)
  32. return -1;
  33. if (!tcf_em_tree_match(skb, &head->ematches, NULL))
  34. return -1;
  35. res->classid = classid;
  36. res->class = 0;
  37. return tcf_exts_exec(skb, &head->exts, res);
  38. }
  39. static unsigned long cls_cgroup_get(struct tcf_proto *tp, u32 handle)
  40. {
  41. return 0UL;
  42. }
  43. static int cls_cgroup_init(struct tcf_proto *tp)
  44. {
  45. return 0;
  46. }
  47. static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
  48. [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED },
  49. };
  50. static void cls_cgroup_destroy_rcu(struct rcu_head *root)
  51. {
  52. struct cls_cgroup_head *head = container_of(root,
  53. struct cls_cgroup_head,
  54. rcu);
  55. tcf_exts_destroy(&head->exts);
  56. tcf_em_tree_destroy(&head->ematches);
  57. kfree(head);
  58. }
  59. static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
  60. struct tcf_proto *tp, unsigned long base,
  61. u32 handle, struct nlattr **tca,
  62. unsigned long *arg, bool ovr)
  63. {
  64. struct nlattr *tb[TCA_CGROUP_MAX + 1];
  65. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  66. struct cls_cgroup_head *new;
  67. struct tcf_ematch_tree t;
  68. struct tcf_exts e;
  69. int err;
  70. if (!tca[TCA_OPTIONS])
  71. return -EINVAL;
  72. if (!head && !handle)
  73. return -EINVAL;
  74. if (head && handle != head->handle)
  75. return -ENOENT;
  76. new = kzalloc(sizeof(*head), GFP_KERNEL);
  77. if (!new)
  78. return -ENOBUFS;
  79. err = tcf_exts_init(&new->exts, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
  80. if (err < 0)
  81. goto errout;
  82. new->handle = handle;
  83. new->tp = tp;
  84. err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS],
  85. cgroup_policy);
  86. if (err < 0)
  87. goto errout;
  88. err = tcf_exts_init(&e, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
  89. if (err < 0)
  90. goto errout;
  91. err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &e, ovr);
  92. if (err < 0) {
  93. tcf_exts_destroy(&e);
  94. goto errout;
  95. }
  96. err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t);
  97. if (err < 0) {
  98. tcf_exts_destroy(&e);
  99. goto errout;
  100. }
  101. tcf_exts_change(tp, &new->exts, &e);
  102. tcf_em_tree_change(tp, &new->ematches, &t);
  103. rcu_assign_pointer(tp->root, new);
  104. if (head)
  105. call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
  106. return 0;
  107. errout:
  108. tcf_exts_destroy(&new->exts);
  109. kfree(new);
  110. return err;
  111. }
  112. static bool cls_cgroup_destroy(struct tcf_proto *tp, bool force)
  113. {
  114. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  115. if (!force)
  116. return false;
  117. /* Head can still be NULL due to cls_cgroup_init(). */
  118. if (head)
  119. call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
  120. return true;
  121. }
  122. static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
  123. {
  124. return -EOPNOTSUPP;
  125. }
  126. static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  127. {
  128. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  129. if (arg->count < arg->skip)
  130. goto skip;
  131. if (arg->fn(tp, (unsigned long) head, arg) < 0) {
  132. arg->stop = 1;
  133. return;
  134. }
  135. skip:
  136. arg->count++;
  137. }
  138. static int cls_cgroup_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  139. struct sk_buff *skb, struct tcmsg *t)
  140. {
  141. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  142. struct nlattr *nest;
  143. t->tcm_handle = head->handle;
  144. nest = nla_nest_start(skb, TCA_OPTIONS);
  145. if (nest == NULL)
  146. goto nla_put_failure;
  147. if (tcf_exts_dump(skb, &head->exts) < 0 ||
  148. tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
  149. goto nla_put_failure;
  150. nla_nest_end(skb, nest);
  151. if (tcf_exts_dump_stats(skb, &head->exts) < 0)
  152. goto nla_put_failure;
  153. return skb->len;
  154. nla_put_failure:
  155. nla_nest_cancel(skb, nest);
  156. return -1;
  157. }
  158. static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
  159. .kind = "cgroup",
  160. .init = cls_cgroup_init,
  161. .change = cls_cgroup_change,
  162. .classify = cls_cgroup_classify,
  163. .destroy = cls_cgroup_destroy,
  164. .get = cls_cgroup_get,
  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");