cls_matchall.c 6.2 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. 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_cls_matchall_offload cls_mall = {};
  48. int err;
  49. tc_cls_common_offload_init(&cls_mall.common, tp);
  50. cls_mall.command = TC_CLSMATCHALL_REPLACE;
  51. cls_mall.exts = &head->exts;
  52. cls_mall.cookie = cookie;
  53. err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSMATCHALL,
  54. &cls_mall);
  55. if (!err)
  56. head->flags |= TCA_CLS_FLAGS_IN_HW;
  57. return err;
  58. }
  59. static void mall_destroy_hw_filter(struct tcf_proto *tp,
  60. struct cls_mall_head *head,
  61. unsigned long cookie)
  62. {
  63. struct net_device *dev = tp->q->dev_queue->dev;
  64. struct tc_cls_matchall_offload cls_mall = {};
  65. tc_cls_common_offload_init(&cls_mall.common, tp);
  66. cls_mall.command = TC_CLSMATCHALL_DESTROY;
  67. cls_mall.cookie = cookie;
  68. dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSMATCHALL, &cls_mall);
  69. }
  70. static void mall_destroy(struct tcf_proto *tp)
  71. {
  72. struct cls_mall_head *head = rtnl_dereference(tp->root);
  73. struct net_device *dev = tp->q->dev_queue->dev;
  74. if (!head)
  75. return;
  76. if (tc_should_offload(dev, tp, head->flags))
  77. mall_destroy_hw_filter(tp, head, (unsigned long) head);
  78. call_rcu(&head->rcu, mall_destroy_rcu);
  79. }
  80. static void *mall_get(struct tcf_proto *tp, u32 handle)
  81. {
  82. return NULL;
  83. }
  84. static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
  85. [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
  86. [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
  87. };
  88. static int mall_set_parms(struct net *net, struct tcf_proto *tp,
  89. struct cls_mall_head *head,
  90. unsigned long base, struct nlattr **tb,
  91. struct nlattr *est, bool ovr)
  92. {
  93. int err;
  94. err = tcf_exts_validate(net, tp, tb, est, &head->exts, ovr);
  95. if (err < 0)
  96. return err;
  97. if (tb[TCA_MATCHALL_CLASSID]) {
  98. head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
  99. tcf_bind_filter(tp, &head->res, base);
  100. }
  101. return 0;
  102. }
  103. static int mall_change(struct net *net, struct sk_buff *in_skb,
  104. struct tcf_proto *tp, unsigned long base,
  105. u32 handle, struct nlattr **tca,
  106. void **arg, bool ovr)
  107. {
  108. struct cls_mall_head *head = rtnl_dereference(tp->root);
  109. struct net_device *dev = tp->q->dev_queue->dev;
  110. struct nlattr *tb[TCA_MATCHALL_MAX + 1];
  111. struct cls_mall_head *new;
  112. u32 flags = 0;
  113. int err;
  114. if (!tca[TCA_OPTIONS])
  115. return -EINVAL;
  116. if (head)
  117. return -EEXIST;
  118. err = nla_parse_nested(tb, TCA_MATCHALL_MAX, tca[TCA_OPTIONS],
  119. mall_policy, NULL);
  120. if (err < 0)
  121. return err;
  122. if (tb[TCA_MATCHALL_FLAGS]) {
  123. flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
  124. if (!tc_flags_valid(flags))
  125. return -EINVAL;
  126. }
  127. new = kzalloc(sizeof(*new), GFP_KERNEL);
  128. if (!new)
  129. return -ENOBUFS;
  130. err = tcf_exts_init(&new->exts, TCA_MATCHALL_ACT, 0);
  131. if (err)
  132. goto err_exts_init;
  133. if (!handle)
  134. handle = 1;
  135. new->handle = handle;
  136. new->flags = flags;
  137. err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr);
  138. if (err)
  139. goto err_set_parms;
  140. if (tc_should_offload(dev, tp, flags)) {
  141. err = mall_replace_hw_filter(tp, new, (unsigned long) new);
  142. if (err) {
  143. if (tc_skip_sw(flags))
  144. goto err_replace_hw_filter;
  145. else
  146. err = 0;
  147. }
  148. }
  149. if (!tc_in_hw(new->flags))
  150. new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
  151. *arg = head;
  152. rcu_assign_pointer(tp->root, new);
  153. return 0;
  154. err_replace_hw_filter:
  155. err_set_parms:
  156. tcf_exts_destroy(&new->exts);
  157. err_exts_init:
  158. kfree(new);
  159. return err;
  160. }
  161. static int mall_delete(struct tcf_proto *tp, void *arg, bool *last)
  162. {
  163. return -EOPNOTSUPP;
  164. }
  165. static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  166. {
  167. struct cls_mall_head *head = rtnl_dereference(tp->root);
  168. if (arg->count < arg->skip)
  169. goto skip;
  170. if (arg->fn(tp, head, arg) < 0)
  171. arg->stop = 1;
  172. skip:
  173. arg->count++;
  174. }
  175. static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh,
  176. struct sk_buff *skb, struct tcmsg *t)
  177. {
  178. struct cls_mall_head *head = fh;
  179. struct nlattr *nest;
  180. if (!head)
  181. return skb->len;
  182. t->tcm_handle = head->handle;
  183. nest = nla_nest_start(skb, TCA_OPTIONS);
  184. if (!nest)
  185. goto nla_put_failure;
  186. if (head->res.classid &&
  187. nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
  188. goto nla_put_failure;
  189. if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags))
  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");