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