cls_matchall.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * net/sched/cls_matchll.c Match-all classifier
  3. *
  4. * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
  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. if (tc_should_offload(dev, tp, head->flags))
  80. mall_destroy_hw_filter(tp, head, (unsigned long) head);
  81. call_rcu(&head->rcu, mall_destroy_rcu);
  82. return true;
  83. }
  84. static unsigned long mall_get(struct tcf_proto *tp, u32 handle)
  85. {
  86. return 0UL;
  87. }
  88. static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
  89. [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
  90. [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
  91. };
  92. static int mall_set_parms(struct net *net, struct tcf_proto *tp,
  93. struct cls_mall_head *head,
  94. unsigned long base, struct nlattr **tb,
  95. struct nlattr *est, bool ovr)
  96. {
  97. struct tcf_exts e;
  98. int err;
  99. tcf_exts_init(&e, TCA_MATCHALL_ACT, 0);
  100. err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
  101. if (err < 0)
  102. return err;
  103. if (tb[TCA_MATCHALL_CLASSID]) {
  104. head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
  105. tcf_bind_filter(tp, &head->res, base);
  106. }
  107. tcf_exts_change(tp, &head->exts, &e);
  108. return 0;
  109. }
  110. static int mall_change(struct net *net, struct sk_buff *in_skb,
  111. struct tcf_proto *tp, unsigned long base,
  112. u32 handle, struct nlattr **tca,
  113. unsigned long *arg, bool ovr)
  114. {
  115. struct cls_mall_head *head = rtnl_dereference(tp->root);
  116. struct net_device *dev = tp->q->dev_queue->dev;
  117. struct nlattr *tb[TCA_MATCHALL_MAX + 1];
  118. struct cls_mall_head *new;
  119. u32 flags = 0;
  120. int err;
  121. if (!tca[TCA_OPTIONS])
  122. return -EINVAL;
  123. if (head)
  124. return -EEXIST;
  125. err = nla_parse_nested(tb, TCA_MATCHALL_MAX,
  126. tca[TCA_OPTIONS], mall_policy);
  127. if (err < 0)
  128. return err;
  129. if (tb[TCA_MATCHALL_FLAGS]) {
  130. flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
  131. if (!tc_flags_valid(flags))
  132. return -EINVAL;
  133. }
  134. new = kzalloc(sizeof(*new), GFP_KERNEL);
  135. if (!new)
  136. return -ENOBUFS;
  137. tcf_exts_init(&new->exts, TCA_MATCHALL_ACT, 0);
  138. if (!handle)
  139. handle = 1;
  140. new->handle = handle;
  141. new->flags = flags;
  142. err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr);
  143. if (err)
  144. goto errout;
  145. if (tc_should_offload(dev, tp, flags)) {
  146. err = mall_replace_hw_filter(tp, new, (unsigned long) new);
  147. if (err) {
  148. if (tc_skip_sw(flags))
  149. goto errout;
  150. else
  151. err = 0;
  152. }
  153. }
  154. *arg = (unsigned long) head;
  155. rcu_assign_pointer(tp->root, new);
  156. if (head)
  157. call_rcu(&head->rcu, mall_destroy_rcu);
  158. return 0;
  159. errout:
  160. kfree(new);
  161. return err;
  162. }
  163. static int mall_delete(struct tcf_proto *tp, unsigned long arg)
  164. {
  165. return -EOPNOTSUPP;
  166. }
  167. static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  168. {
  169. struct cls_mall_head *head = rtnl_dereference(tp->root);
  170. if (arg->count < arg->skip)
  171. goto skip;
  172. if (arg->fn(tp, (unsigned long) head, arg) < 0)
  173. arg->stop = 1;
  174. skip:
  175. arg->count++;
  176. }
  177. static int mall_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  178. struct sk_buff *skb, struct tcmsg *t)
  179. {
  180. struct cls_mall_head *head = (struct cls_mall_head *) fh;
  181. struct nlattr *nest;
  182. if (!head)
  183. return skb->len;
  184. t->tcm_handle = head->handle;
  185. nest = nla_nest_start(skb, TCA_OPTIONS);
  186. if (!nest)
  187. goto nla_put_failure;
  188. if (head->res.classid &&
  189. nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
  190. goto nla_put_failure;
  191. if (tcf_exts_dump(skb, &head->exts))
  192. goto nla_put_failure;
  193. nla_nest_end(skb, nest);
  194. if (tcf_exts_dump_stats(skb, &head->exts) < 0)
  195. goto nla_put_failure;
  196. return skb->len;
  197. nla_put_failure:
  198. nla_nest_cancel(skb, nest);
  199. return -1;
  200. }
  201. static struct tcf_proto_ops cls_mall_ops __read_mostly = {
  202. .kind = "matchall",
  203. .classify = mall_classify,
  204. .init = mall_init,
  205. .destroy = mall_destroy,
  206. .get = mall_get,
  207. .change = mall_change,
  208. .delete = mall_delete,
  209. .walk = mall_walk,
  210. .dump = mall_dump,
  211. .owner = THIS_MODULE,
  212. };
  213. static int __init cls_mall_init(void)
  214. {
  215. return register_tcf_proto_ops(&cls_mall_ops);
  216. }
  217. static void __exit cls_mall_exit(void)
  218. {
  219. unregister_tcf_proto_ops(&cls_mall_ops);
  220. }
  221. module_init(cls_mall_init);
  222. module_exit(cls_mall_exit);
  223. MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
  224. MODULE_DESCRIPTION("Match-all classifier");
  225. MODULE_LICENSE("GPL v2");