cls_matchall.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * net/sched/cls_matchll.c Match-all classifier
  3. *
  4. * Copyright (c) 2016 Jiri Pirko <[email protected]>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <net/sch_generic.h>
  15. #include <net/pkt_cls.h>
  16. struct cls_mall_head {
  17. struct tcf_exts exts;
  18. struct tcf_result res;
  19. u32 handle;
  20. u32 flags;
  21. struct rcu_head rcu;
  22. };
  23. static int mall_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  24. struct tcf_result *res)
  25. {
  26. struct cls_mall_head *head = rcu_dereference_bh(tp->root);
  27. if (tc_skip_sw(head->flags))
  28. return -1;
  29. *res = head->res;
  30. return tcf_exts_exec(skb, &head->exts, res);
  31. }
  32. static int mall_init(struct tcf_proto *tp)
  33. {
  34. return 0;
  35. }
  36. static void mall_destroy_rcu(struct rcu_head *rcu)
  37. {
  38. struct cls_mall_head *head = container_of(rcu, struct cls_mall_head,
  39. rcu);
  40. tcf_exts_destroy(&head->exts);
  41. kfree(head);
  42. }
  43. static int mall_replace_hw_filter(struct tcf_proto *tp,
  44. struct cls_mall_head *head,
  45. unsigned long cookie)
  46. {
  47. struct net_device *dev = tp->q->dev_queue->dev;
  48. struct tc_to_netdev offload;
  49. struct tc_cls_matchall_offload mall_offload = {0};
  50. offload.type = TC_SETUP_MATCHALL;
  51. offload.cls_mall = &mall_offload;
  52. offload.cls_mall->command = TC_CLSMATCHALL_REPLACE;
  53. offload.cls_mall->exts = &head->exts;
  54. offload.cls_mall->cookie = cookie;
  55. return dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol,
  56. &offload);
  57. }
  58. static void mall_destroy_hw_filter(struct tcf_proto *tp,
  59. struct cls_mall_head *head,
  60. unsigned long cookie)
  61. {
  62. struct net_device *dev = tp->q->dev_queue->dev;
  63. struct tc_to_netdev offload;
  64. struct tc_cls_matchall_offload mall_offload = {0};
  65. offload.type = TC_SETUP_MATCHALL;
  66. offload.cls_mall = &mall_offload;
  67. offload.cls_mall->command = TC_CLSMATCHALL_DESTROY;
  68. offload.cls_mall->exts = NULL;
  69. offload.cls_mall->cookie = cookie;
  70. dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol,
  71. &offload);
  72. }
  73. static bool mall_destroy(struct tcf_proto *tp, bool force)
  74. {
  75. struct cls_mall_head *head = rtnl_dereference(tp->root);
  76. struct net_device *dev = tp->q->dev_queue->dev;
  77. if (!head)
  78. return true;
  79. tcf_unbind_filter(tp, &head->res);
  80. if (tc_should_offload(dev, tp, head->flags))
  81. mall_destroy_hw_filter(tp, head, (unsigned long) head);
  82. call_rcu(&head->rcu, mall_destroy_rcu);
  83. return true;
  84. }
  85. static unsigned long mall_get(struct tcf_proto *tp, u32 handle)
  86. {
  87. return 0UL;
  88. }
  89. static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
  90. [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
  91. [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
  92. };
  93. static int mall_set_parms(struct net *net, struct tcf_proto *tp,
  94. struct cls_mall_head *head,
  95. unsigned long base, struct nlattr **tb,
  96. struct nlattr *est, bool ovr)
  97. {
  98. struct tcf_exts e;
  99. int err;
  100. tcf_exts_init(&e, TCA_MATCHALL_ACT, 0);
  101. err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
  102. if (err < 0)
  103. return err;
  104. if (tb[TCA_MATCHALL_CLASSID]) {
  105. head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
  106. tcf_bind_filter(tp, &head->res, base);
  107. }
  108. tcf_exts_change(tp, &head->exts, &e);
  109. return 0;
  110. }
  111. static int mall_change(struct net *net, struct sk_buff *in_skb,
  112. struct tcf_proto *tp, unsigned long base,
  113. u32 handle, struct nlattr **tca,
  114. unsigned long *arg, bool ovr)
  115. {
  116. struct cls_mall_head *head = rtnl_dereference(tp->root);
  117. struct net_device *dev = tp->q->dev_queue->dev;
  118. struct nlattr *tb[TCA_MATCHALL_MAX + 1];
  119. struct cls_mall_head *new;
  120. u32 flags = 0;
  121. int err;
  122. if (!tca[TCA_OPTIONS])
  123. return -EINVAL;
  124. if (head)
  125. return -EEXIST;
  126. err = nla_parse_nested(tb, TCA_MATCHALL_MAX,
  127. tca[TCA_OPTIONS], mall_policy);
  128. if (err < 0)
  129. return err;
  130. if (tb[TCA_MATCHALL_FLAGS]) {
  131. flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
  132. if (!tc_flags_valid(flags))
  133. return -EINVAL;
  134. }
  135. new = kzalloc(sizeof(*new), GFP_KERNEL);
  136. if (!new)
  137. return -ENOBUFS;
  138. tcf_exts_init(&new->exts, TCA_MATCHALL_ACT, 0);
  139. if (!handle)
  140. handle = 1;
  141. new->handle = handle;
  142. new->flags = flags;
  143. err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr);
  144. if (err)
  145. goto errout;
  146. if (tc_should_offload(dev, tp, flags)) {
  147. err = mall_replace_hw_filter(tp, new, (unsigned long) new);
  148. if (err) {
  149. if (tc_skip_sw(flags))
  150. goto errout;
  151. else
  152. err = 0;
  153. }
  154. }
  155. *arg = (unsigned long) head;
  156. rcu_assign_pointer(tp->root, new);
  157. if (head)
  158. call_rcu(&head->rcu, mall_destroy_rcu);
  159. return 0;
  160. errout:
  161. kfree(new);
  162. return err;
  163. }
  164. static int mall_delete(struct tcf_proto *tp, unsigned long arg)
  165. {
  166. return -EOPNOTSUPP;
  167. }
  168. static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  169. {
  170. struct cls_mall_head *head = rtnl_dereference(tp->root);
  171. if (arg->count < arg->skip)
  172. goto skip;
  173. if (arg->fn(tp, (unsigned long) head, arg) < 0)
  174. arg->stop = 1;
  175. skip:
  176. arg->count++;
  177. }
  178. static int mall_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  179. struct sk_buff *skb, struct tcmsg *t)
  180. {
  181. struct cls_mall_head *head = (struct cls_mall_head *) fh;
  182. struct nlattr *nest;
  183. if (!head)
  184. return skb->len;
  185. t->tcm_handle = head->handle;
  186. nest = nla_nest_start(skb, TCA_OPTIONS);
  187. if (!nest)
  188. goto nla_put_failure;
  189. if (head->res.classid &&
  190. nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
  191. goto nla_put_failure;
  192. if (tcf_exts_dump(skb, &head->exts))
  193. goto nla_put_failure;
  194. nla_nest_end(skb, nest);
  195. if (tcf_exts_dump_stats(skb, &head->exts) < 0)
  196. goto nla_put_failure;
  197. return skb->len;
  198. nla_put_failure:
  199. nla_nest_cancel(skb, nest);
  200. return -1;
  201. }
  202. static struct tcf_proto_ops cls_mall_ops __read_mostly = {
  203. .kind = "matchall",
  204. .classify = mall_classify,
  205. .init = mall_init,
  206. .destroy = mall_destroy,
  207. .get = mall_get,
  208. .change = mall_change,
  209. .delete = mall_delete,
  210. .walk = mall_walk,
  211. .dump = mall_dump,
  212. .owner = THIS_MODULE,
  213. };
  214. static int __init cls_mall_init(void)
  215. {
  216. return register_tcf_proto_ops(&cls_mall_ops);
  217. }
  218. static void __exit cls_mall_exit(void)
  219. {
  220. unregister_tcf_proto_ops(&cls_mall_ops);
  221. }
  222. module_init(cls_mall_init);
  223. module_exit(cls_mall_exit);
  224. MODULE_AUTHOR("Jiri Pirko <[email protected]>");
  225. MODULE_DESCRIPTION("Match-all classifier");
  226. MODULE_LICENSE("GPL v2");