cls_cgroup.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. tcf_exts_init(&new->exts, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
  80. new->handle = handle;
  81. new->tp = tp;
  82. err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS],
  83. cgroup_policy);
  84. if (err < 0)
  85. goto errout;
  86. tcf_exts_init(&e, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
  87. err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &e, ovr);
  88. if (err < 0)
  89. goto errout;
  90. err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &t);
  91. if (err < 0) {
  92. tcf_exts_destroy(&e);
  93. goto errout;
  94. }
  95. tcf_exts_change(tp, &new->exts, &e);
  96. tcf_em_tree_change(tp, &new->ematches, &t);
  97. rcu_assign_pointer(tp->root, new);
  98. if (head)
  99. call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
  100. return 0;
  101. errout:
  102. kfree(new);
  103. return err;
  104. }
  105. static bool cls_cgroup_destroy(struct tcf_proto *tp, bool force)
  106. {
  107. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  108. if (!force)
  109. return false;
  110. /* Head can still be NULL due to cls_cgroup_init(). */
  111. if (head)
  112. call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
  113. return true;
  114. }
  115. static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
  116. {
  117. return -EOPNOTSUPP;
  118. }
  119. static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  120. {
  121. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  122. if (arg->count < arg->skip)
  123. goto skip;
  124. if (arg->fn(tp, (unsigned long) head, arg) < 0) {
  125. arg->stop = 1;
  126. return;
  127. }
  128. skip:
  129. arg->count++;
  130. }
  131. static int cls_cgroup_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  132. struct sk_buff *skb, struct tcmsg *t)
  133. {
  134. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  135. struct nlattr *nest;
  136. t->tcm_handle = head->handle;
  137. nest = nla_nest_start(skb, TCA_OPTIONS);
  138. if (nest == NULL)
  139. goto nla_put_failure;
  140. if (tcf_exts_dump(skb, &head->exts) < 0 ||
  141. tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
  142. goto nla_put_failure;
  143. nla_nest_end(skb, nest);
  144. if (tcf_exts_dump_stats(skb, &head->exts) < 0)
  145. goto nla_put_failure;
  146. return skb->len;
  147. nla_put_failure:
  148. nla_nest_cancel(skb, nest);
  149. return -1;
  150. }
  151. static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
  152. .kind = "cgroup",
  153. .init = cls_cgroup_init,
  154. .change = cls_cgroup_change,
  155. .classify = cls_cgroup_classify,
  156. .destroy = cls_cgroup_destroy,
  157. .get = cls_cgroup_get,
  158. .delete = cls_cgroup_delete,
  159. .walk = cls_cgroup_walk,
  160. .dump = cls_cgroup_dump,
  161. .owner = THIS_MODULE,
  162. };
  163. static int __init init_cgroup_cls(void)
  164. {
  165. return register_tcf_proto_ops(&cls_cgroup_ops);
  166. }
  167. static void __exit exit_cgroup_cls(void)
  168. {
  169. unregister_tcf_proto_ops(&cls_cgroup_ops);
  170. }
  171. module_init(init_cgroup_cls);
  172. module_exit(exit_cgroup_cls);
  173. MODULE_LICENSE("GPL");