cls_matchall.c 6.9 KB

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