cls_matchall.c 7.2 KB

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