cls_cgroup.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 = 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, NULL);
  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 void cls_cgroup_destroy(struct tcf_proto *tp)
  113. {
  114. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  115. /* Head can still be NULL due to cls_cgroup_init(). */
  116. if (head)
  117. call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
  118. }
  119. static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg, bool *last)
  120. {
  121. return -EOPNOTSUPP;
  122. }
  123. static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  124. {
  125. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  126. if (arg->count < arg->skip)
  127. goto skip;
  128. if (arg->fn(tp, (unsigned long) head, arg) < 0) {
  129. arg->stop = 1;
  130. return;
  131. }
  132. skip:
  133. arg->count++;
  134. }
  135. static int cls_cgroup_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  136. struct sk_buff *skb, struct tcmsg *t)
  137. {
  138. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  139. struct nlattr *nest;
  140. t->tcm_handle = head->handle;
  141. nest = nla_nest_start(skb, TCA_OPTIONS);
  142. if (nest == NULL)
  143. goto nla_put_failure;
  144. if (tcf_exts_dump(skb, &head->exts) < 0 ||
  145. tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
  146. goto nla_put_failure;
  147. nla_nest_end(skb, nest);
  148. if (tcf_exts_dump_stats(skb, &head->exts) < 0)
  149. goto nla_put_failure;
  150. return skb->len;
  151. nla_put_failure:
  152. nla_nest_cancel(skb, nest);
  153. return -1;
  154. }
  155. static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
  156. .kind = "cgroup",
  157. .init = cls_cgroup_init,
  158. .change = cls_cgroup_change,
  159. .classify = cls_cgroup_classify,
  160. .destroy = cls_cgroup_destroy,
  161. .get = cls_cgroup_get,
  162. .delete = cls_cgroup_delete,
  163. .walk = cls_cgroup_walk,
  164. .dump = cls_cgroup_dump,
  165. .owner = THIS_MODULE,
  166. };
  167. static int __init init_cgroup_cls(void)
  168. {
  169. return register_tcf_proto_ops(&cls_cgroup_ops);
  170. }
  171. static void __exit exit_cgroup_cls(void)
  172. {
  173. unregister_tcf_proto_ops(&cls_cgroup_ops);
  174. }
  175. module_init(init_cgroup_cls);
  176. module_exit(exit_cgroup_cls);
  177. MODULE_LICENSE("GPL");