cls_cgroup.c 4.6 KB

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