cls_matchall.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. tcf_unbind_filter(tp, &head->res);
  95. if (tc_should_offload(dev, head->flags))
  96. mall_destroy_hw_filter(tp, head, (unsigned long) head);
  97. if (tcf_exts_get_net(&head->exts))
  98. call_rcu(&head->rcu, mall_destroy_rcu);
  99. else
  100. __mall_destroy(head);
  101. }
  102. static void *mall_get(struct tcf_proto *tp, u32 handle)
  103. {
  104. struct cls_mall_head *head = rtnl_dereference(tp->root);
  105. if (head && head->handle == handle)
  106. return head;
  107. return NULL;
  108. }
  109. static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
  110. [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
  111. [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
  112. [TCA_MATCHALL_FLAGS] = { .type = NLA_U32 },
  113. };
  114. static int mall_set_parms(struct net *net, struct tcf_proto *tp,
  115. struct cls_mall_head *head,
  116. unsigned long base, struct nlattr **tb,
  117. struct nlattr *est, bool ovr)
  118. {
  119. int err;
  120. err = tcf_exts_validate(net, tp, tb, est, &head->exts, ovr);
  121. if (err < 0)
  122. return err;
  123. if (tb[TCA_MATCHALL_CLASSID]) {
  124. head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
  125. tcf_bind_filter(tp, &head->res, base);
  126. }
  127. return 0;
  128. }
  129. static int mall_change(struct net *net, struct sk_buff *in_skb,
  130. struct tcf_proto *tp, unsigned long base,
  131. u32 handle, struct nlattr **tca,
  132. void **arg, bool ovr)
  133. {
  134. struct cls_mall_head *head = rtnl_dereference(tp->root);
  135. struct net_device *dev = tp->q->dev_queue->dev;
  136. struct nlattr *tb[TCA_MATCHALL_MAX + 1];
  137. struct cls_mall_head *new;
  138. u32 flags = 0;
  139. int err;
  140. if (!tca[TCA_OPTIONS])
  141. return -EINVAL;
  142. if (head)
  143. return -EEXIST;
  144. err = nla_parse_nested(tb, TCA_MATCHALL_MAX, tca[TCA_OPTIONS],
  145. mall_policy, NULL);
  146. if (err < 0)
  147. return err;
  148. if (tb[TCA_MATCHALL_FLAGS]) {
  149. flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
  150. if (!tc_flags_valid(flags))
  151. return -EINVAL;
  152. }
  153. new = kzalloc(sizeof(*new), GFP_KERNEL);
  154. if (!new)
  155. return -ENOBUFS;
  156. err = tcf_exts_init(&new->exts, TCA_MATCHALL_ACT, 0);
  157. if (err)
  158. goto err_exts_init;
  159. if (!handle)
  160. handle = 1;
  161. new->handle = handle;
  162. new->flags = flags;
  163. err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr);
  164. if (err)
  165. goto err_set_parms;
  166. if (tc_should_offload(dev, flags)) {
  167. err = mall_replace_hw_filter(tp, new, (unsigned long) new);
  168. if (err) {
  169. if (tc_skip_sw(flags))
  170. goto err_replace_hw_filter;
  171. else
  172. err = 0;
  173. }
  174. }
  175. if (!tc_in_hw(new->flags))
  176. new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
  177. *arg = head;
  178. rcu_assign_pointer(tp->root, new);
  179. return 0;
  180. err_replace_hw_filter:
  181. err_set_parms:
  182. tcf_exts_destroy(&new->exts);
  183. err_exts_init:
  184. kfree(new);
  185. return err;
  186. }
  187. static int mall_delete(struct tcf_proto *tp, void *arg, bool *last)
  188. {
  189. return -EOPNOTSUPP;
  190. }
  191. static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  192. {
  193. struct cls_mall_head *head = rtnl_dereference(tp->root);
  194. if (arg->count < arg->skip)
  195. goto skip;
  196. if (arg->fn(tp, head, arg) < 0)
  197. arg->stop = 1;
  198. skip:
  199. arg->count++;
  200. }
  201. static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh,
  202. struct sk_buff *skb, struct tcmsg *t)
  203. {
  204. struct cls_mall_head *head = fh;
  205. struct nlattr *nest;
  206. if (!head)
  207. return skb->len;
  208. t->tcm_handle = head->handle;
  209. nest = nla_nest_start(skb, TCA_OPTIONS);
  210. if (!nest)
  211. goto nla_put_failure;
  212. if (head->res.classid &&
  213. nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
  214. goto nla_put_failure;
  215. if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags))
  216. goto nla_put_failure;
  217. if (tcf_exts_dump(skb, &head->exts))
  218. goto nla_put_failure;
  219. nla_nest_end(skb, nest);
  220. if (tcf_exts_dump_stats(skb, &head->exts) < 0)
  221. goto nla_put_failure;
  222. return skb->len;
  223. nla_put_failure:
  224. nla_nest_cancel(skb, nest);
  225. return -1;
  226. }
  227. static void mall_bind_class(void *fh, u32 classid, unsigned long cl)
  228. {
  229. struct cls_mall_head *head = fh;
  230. if (head && head->res.classid == classid)
  231. head->res.class = cl;
  232. }
  233. static struct tcf_proto_ops cls_mall_ops __read_mostly = {
  234. .kind = "matchall",
  235. .classify = mall_classify,
  236. .init = mall_init,
  237. .destroy = mall_destroy,
  238. .get = mall_get,
  239. .change = mall_change,
  240. .delete = mall_delete,
  241. .walk = mall_walk,
  242. .dump = mall_dump,
  243. .bind_class = mall_bind_class,
  244. .owner = THIS_MODULE,
  245. };
  246. static int __init cls_mall_init(void)
  247. {
  248. return register_tcf_proto_ops(&cls_mall_ops);
  249. }
  250. static void __exit cls_mall_exit(void)
  251. {
  252. unregister_tcf_proto_ops(&cls_mall_ops);
  253. }
  254. module_init(cls_mall_init);
  255. module_exit(cls_mall_exit);
  256. MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
  257. MODULE_DESCRIPTION("Match-all classifier");
  258. MODULE_LICENSE("GPL v2");