cls_matchall.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. return tcf_exts_exec(skb, &head->exts, res);
  30. }
  31. static int mall_init(struct tcf_proto *tp)
  32. {
  33. return 0;
  34. }
  35. static void mall_destroy_rcu(struct rcu_head *rcu)
  36. {
  37. struct cls_mall_head *head = container_of(rcu, struct cls_mall_head,
  38. rcu);
  39. tcf_exts_destroy(&head->exts);
  40. kfree(head);
  41. }
  42. static int mall_replace_hw_filter(struct tcf_proto *tp,
  43. struct cls_mall_head *head,
  44. unsigned long cookie)
  45. {
  46. struct net_device *dev = tp->q->dev_queue->dev;
  47. struct tc_to_netdev offload;
  48. struct tc_cls_matchall_offload mall_offload = {0};
  49. int err;
  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. err = dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle,
  56. tp->chain->index,
  57. tp->protocol, &offload);
  58. if (!err)
  59. head->flags |= TCA_CLS_FLAGS_IN_HW;
  60. return err;
  61. }
  62. static void mall_destroy_hw_filter(struct tcf_proto *tp,
  63. struct cls_mall_head *head,
  64. unsigned long cookie)
  65. {
  66. struct net_device *dev = tp->q->dev_queue->dev;
  67. struct tc_to_netdev offload;
  68. struct tc_cls_matchall_offload mall_offload = {0};
  69. offload.type = TC_SETUP_MATCHALL;
  70. offload.cls_mall = &mall_offload;
  71. offload.cls_mall->command = TC_CLSMATCHALL_DESTROY;
  72. offload.cls_mall->exts = NULL;
  73. offload.cls_mall->cookie = cookie;
  74. dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->chain->index,
  75. tp->protocol, &offload);
  76. }
  77. static void mall_destroy(struct tcf_proto *tp)
  78. {
  79. struct cls_mall_head *head = rtnl_dereference(tp->root);
  80. struct net_device *dev = tp->q->dev_queue->dev;
  81. if (!head)
  82. return;
  83. if (tc_should_offload(dev, tp, head->flags))
  84. mall_destroy_hw_filter(tp, head, (unsigned long) head);
  85. call_rcu(&head->rcu, mall_destroy_rcu);
  86. }
  87. static unsigned long mall_get(struct tcf_proto *tp, u32 handle)
  88. {
  89. return 0UL;
  90. }
  91. static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
  92. [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
  93. [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
  94. };
  95. static int mall_set_parms(struct net *net, struct tcf_proto *tp,
  96. struct cls_mall_head *head,
  97. unsigned long base, struct nlattr **tb,
  98. struct nlattr *est, bool ovr)
  99. {
  100. struct tcf_exts e;
  101. int err;
  102. err = tcf_exts_init(&e, TCA_MATCHALL_ACT, 0);
  103. if (err)
  104. return err;
  105. err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
  106. if (err < 0)
  107. goto errout;
  108. if (tb[TCA_MATCHALL_CLASSID]) {
  109. head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
  110. tcf_bind_filter(tp, &head->res, base);
  111. }
  112. tcf_exts_change(tp, &head->exts, &e);
  113. return 0;
  114. errout:
  115. tcf_exts_destroy(&e);
  116. return err;
  117. }
  118. static int mall_change(struct net *net, struct sk_buff *in_skb,
  119. struct tcf_proto *tp, unsigned long base,
  120. u32 handle, struct nlattr **tca,
  121. unsigned long *arg, bool ovr)
  122. {
  123. struct cls_mall_head *head = rtnl_dereference(tp->root);
  124. struct net_device *dev = tp->q->dev_queue->dev;
  125. struct nlattr *tb[TCA_MATCHALL_MAX + 1];
  126. struct cls_mall_head *new;
  127. u32 flags = 0;
  128. int err;
  129. if (!tca[TCA_OPTIONS])
  130. return -EINVAL;
  131. if (head)
  132. return -EEXIST;
  133. err = nla_parse_nested(tb, TCA_MATCHALL_MAX, tca[TCA_OPTIONS],
  134. mall_policy, NULL);
  135. if (err < 0)
  136. return err;
  137. if (tb[TCA_MATCHALL_FLAGS]) {
  138. flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
  139. if (!tc_flags_valid(flags))
  140. return -EINVAL;
  141. }
  142. new = kzalloc(sizeof(*new), GFP_KERNEL);
  143. if (!new)
  144. return -ENOBUFS;
  145. err = tcf_exts_init(&new->exts, TCA_MATCHALL_ACT, 0);
  146. if (err)
  147. goto err_exts_init;
  148. if (!handle)
  149. handle = 1;
  150. new->handle = handle;
  151. new->flags = flags;
  152. err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr);
  153. if (err)
  154. goto err_set_parms;
  155. if (tc_should_offload(dev, tp, flags)) {
  156. err = mall_replace_hw_filter(tp, new, (unsigned long) new);
  157. if (err) {
  158. if (tc_skip_sw(flags))
  159. goto err_replace_hw_filter;
  160. else
  161. err = 0;
  162. }
  163. }
  164. if (!tc_in_hw(new->flags))
  165. new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
  166. *arg = (unsigned long) head;
  167. rcu_assign_pointer(tp->root, new);
  168. return 0;
  169. err_replace_hw_filter:
  170. err_set_parms:
  171. tcf_exts_destroy(&new->exts);
  172. err_exts_init:
  173. kfree(new);
  174. return err;
  175. }
  176. static int mall_delete(struct tcf_proto *tp, unsigned long arg, bool *last)
  177. {
  178. return -EOPNOTSUPP;
  179. }
  180. static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  181. {
  182. struct cls_mall_head *head = rtnl_dereference(tp->root);
  183. if (arg->count < arg->skip)
  184. goto skip;
  185. if (arg->fn(tp, (unsigned long) head, arg) < 0)
  186. arg->stop = 1;
  187. skip:
  188. arg->count++;
  189. }
  190. static int mall_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  191. struct sk_buff *skb, struct tcmsg *t)
  192. {
  193. struct cls_mall_head *head = (struct cls_mall_head *) fh;
  194. struct nlattr *nest;
  195. if (!head)
  196. return skb->len;
  197. t->tcm_handle = head->handle;
  198. nest = nla_nest_start(skb, TCA_OPTIONS);
  199. if (!nest)
  200. goto nla_put_failure;
  201. if (head->res.classid &&
  202. nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
  203. goto nla_put_failure;
  204. if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags))
  205. goto nla_put_failure;
  206. if (tcf_exts_dump(skb, &head->exts))
  207. goto nla_put_failure;
  208. nla_nest_end(skb, nest);
  209. if (tcf_exts_dump_stats(skb, &head->exts) < 0)
  210. goto nla_put_failure;
  211. return skb->len;
  212. nla_put_failure:
  213. nla_nest_cancel(skb, nest);
  214. return -1;
  215. }
  216. static struct tcf_proto_ops cls_mall_ops __read_mostly = {
  217. .kind = "matchall",
  218. .classify = mall_classify,
  219. .init = mall_init,
  220. .destroy = mall_destroy,
  221. .get = mall_get,
  222. .change = mall_change,
  223. .delete = mall_delete,
  224. .walk = mall_walk,
  225. .dump = mall_dump,
  226. .owner = THIS_MODULE,
  227. };
  228. static int __init cls_mall_init(void)
  229. {
  230. return register_tcf_proto_ops(&cls_mall_ops);
  231. }
  232. static void __exit cls_mall_exit(void)
  233. {
  234. unregister_tcf_proto_ops(&cls_mall_ops);
  235. }
  236. module_init(cls_mall_init);
  237. module_exit(cls_mall_exit);
  238. MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
  239. MODULE_DESCRIPTION("Match-all classifier");
  240. MODULE_LICENSE("GPL v2");