cls_cgroup.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. union {
  25. struct work_struct work;
  26. struct rcu_head rcu;
  27. };
  28. };
  29. static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  30. struct tcf_result *res)
  31. {
  32. struct cls_cgroup_head *head = rcu_dereference_bh(tp->root);
  33. u32 classid = task_get_classid(skb);
  34. if (!classid)
  35. return -1;
  36. if (!tcf_em_tree_match(skb, &head->ematches, NULL))
  37. return -1;
  38. res->classid = classid;
  39. res->class = 0;
  40. return tcf_exts_exec(skb, &head->exts, res);
  41. }
  42. static void *cls_cgroup_get(struct tcf_proto *tp, u32 handle)
  43. {
  44. return NULL;
  45. }
  46. static int cls_cgroup_init(struct tcf_proto *tp)
  47. {
  48. return 0;
  49. }
  50. static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
  51. [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED },
  52. };
  53. static void __cls_cgroup_destroy(struct cls_cgroup_head *head)
  54. {
  55. tcf_exts_destroy(&head->exts);
  56. tcf_em_tree_destroy(&head->ematches);
  57. tcf_exts_put_net(&head->exts);
  58. kfree(head);
  59. }
  60. static void cls_cgroup_destroy_work(struct work_struct *work)
  61. {
  62. struct cls_cgroup_head *head = container_of(work,
  63. struct cls_cgroup_head,
  64. work);
  65. rtnl_lock();
  66. __cls_cgroup_destroy(head);
  67. rtnl_unlock();
  68. }
  69. static void cls_cgroup_destroy_rcu(struct rcu_head *root)
  70. {
  71. struct cls_cgroup_head *head = container_of(root,
  72. struct cls_cgroup_head,
  73. rcu);
  74. INIT_WORK(&head->work, cls_cgroup_destroy_work);
  75. tcf_queue_work(&head->work);
  76. }
  77. static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
  78. struct tcf_proto *tp, unsigned long base,
  79. u32 handle, struct nlattr **tca,
  80. void **arg, bool ovr)
  81. {
  82. struct nlattr *tb[TCA_CGROUP_MAX + 1];
  83. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  84. struct cls_cgroup_head *new;
  85. int err;
  86. if (!tca[TCA_OPTIONS])
  87. return -EINVAL;
  88. if (!head && !handle)
  89. return -EINVAL;
  90. if (head && handle != head->handle)
  91. return -ENOENT;
  92. new = kzalloc(sizeof(*head), GFP_KERNEL);
  93. if (!new)
  94. return -ENOBUFS;
  95. err = tcf_exts_init(&new->exts, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
  96. if (err < 0)
  97. goto errout;
  98. new->handle = handle;
  99. new->tp = tp;
  100. err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS],
  101. cgroup_policy, NULL);
  102. if (err < 0)
  103. goto errout;
  104. err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &new->exts, ovr);
  105. if (err < 0)
  106. goto errout;
  107. err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &new->ematches);
  108. if (err < 0)
  109. goto errout;
  110. rcu_assign_pointer(tp->root, new);
  111. if (head) {
  112. tcf_exts_get_net(&head->exts);
  113. call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
  114. }
  115. return 0;
  116. errout:
  117. tcf_exts_destroy(&new->exts);
  118. kfree(new);
  119. return err;
  120. }
  121. static void cls_cgroup_destroy(struct tcf_proto *tp)
  122. {
  123. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  124. /* Head can still be NULL due to cls_cgroup_init(). */
  125. if (head) {
  126. if (tcf_exts_get_net(&head->exts))
  127. call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
  128. else
  129. __cls_cgroup_destroy(head);
  130. }
  131. }
  132. static int cls_cgroup_delete(struct tcf_proto *tp, void *arg, bool *last)
  133. {
  134. return -EOPNOTSUPP;
  135. }
  136. static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  137. {
  138. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  139. if (arg->count < arg->skip)
  140. goto skip;
  141. if (arg->fn(tp, head, arg) < 0) {
  142. arg->stop = 1;
  143. return;
  144. }
  145. skip:
  146. arg->count++;
  147. }
  148. static int cls_cgroup_dump(struct net *net, struct tcf_proto *tp, void *fh,
  149. struct sk_buff *skb, struct tcmsg *t)
  150. {
  151. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  152. struct nlattr *nest;
  153. t->tcm_handle = head->handle;
  154. nest = nla_nest_start(skb, TCA_OPTIONS);
  155. if (nest == NULL)
  156. goto nla_put_failure;
  157. if (tcf_exts_dump(skb, &head->exts) < 0 ||
  158. tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
  159. goto nla_put_failure;
  160. nla_nest_end(skb, nest);
  161. if (tcf_exts_dump_stats(skb, &head->exts) < 0)
  162. goto nla_put_failure;
  163. return skb->len;
  164. nla_put_failure:
  165. nla_nest_cancel(skb, nest);
  166. return -1;
  167. }
  168. static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
  169. .kind = "cgroup",
  170. .init = cls_cgroup_init,
  171. .change = cls_cgroup_change,
  172. .classify = cls_cgroup_classify,
  173. .destroy = cls_cgroup_destroy,
  174. .get = cls_cgroup_get,
  175. .delete = cls_cgroup_delete,
  176. .walk = cls_cgroup_walk,
  177. .dump = cls_cgroup_dump,
  178. .owner = THIS_MODULE,
  179. };
  180. static int __init init_cgroup_cls(void)
  181. {
  182. return register_tcf_proto_ops(&cls_cgroup_ops);
  183. }
  184. static void __exit exit_cgroup_cls(void)
  185. {
  186. unregister_tcf_proto_ops(&cls_cgroup_ops);
  187. }
  188. module_init(init_cgroup_cls);
  189. module_exit(exit_cgroup_cls);
  190. MODULE_LICENSE("GPL");