cls_matchall.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. RCU_INIT_POINTER(tp->root, NULL);
  94. kfree_rcu(head, rcu);
  95. return true;
  96. }
  97. static unsigned long mall_get(struct tcf_proto *tp, u32 handle)
  98. {
  99. struct cls_mall_head *head = rtnl_dereference(tp->root);
  100. struct cls_mall_filter *f = head->filter;
  101. if (f && f->handle == handle)
  102. return (unsigned long) f;
  103. return 0;
  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_filter *f,
  111. unsigned long base, struct nlattr **tb,
  112. struct nlattr *est, bool ovr)
  113. {
  114. struct tcf_exts e;
  115. int err;
  116. tcf_exts_init(&e, TCA_MATCHALL_ACT, 0);
  117. err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
  118. if (err < 0)
  119. return err;
  120. if (tb[TCA_MATCHALL_CLASSID]) {
  121. f->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
  122. tcf_bind_filter(tp, &f->res, base);
  123. }
  124. tcf_exts_change(tp, &f->exts, &e);
  125. return 0;
  126. }
  127. static int mall_change(struct net *net, struct sk_buff *in_skb,
  128. struct tcf_proto *tp, unsigned long base,
  129. u32 handle, struct nlattr **tca,
  130. unsigned long *arg, bool ovr)
  131. {
  132. struct cls_mall_head *head = rtnl_dereference(tp->root);
  133. struct cls_mall_filter *fold = (struct cls_mall_filter *) *arg;
  134. struct net_device *dev = tp->q->dev_queue->dev;
  135. struct cls_mall_filter *f;
  136. struct nlattr *tb[TCA_MATCHALL_MAX + 1];
  137. u32 flags = 0;
  138. int err;
  139. if (!tca[TCA_OPTIONS])
  140. return -EINVAL;
  141. if (head->filter)
  142. return -EBUSY;
  143. if (fold)
  144. return -EINVAL;
  145. err = nla_parse_nested(tb, TCA_MATCHALL_MAX,
  146. tca[TCA_OPTIONS], mall_policy);
  147. if (err < 0)
  148. return err;
  149. if (tb[TCA_MATCHALL_FLAGS]) {
  150. flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
  151. if (!tc_flags_valid(flags))
  152. return -EINVAL;
  153. }
  154. f = kzalloc(sizeof(*f), GFP_KERNEL);
  155. if (!f)
  156. return -ENOBUFS;
  157. tcf_exts_init(&f->exts, TCA_MATCHALL_ACT, 0);
  158. if (!handle)
  159. handle = 1;
  160. f->handle = handle;
  161. f->flags = flags;
  162. err = mall_set_parms(net, tp, f, base, tb, tca[TCA_RATE], ovr);
  163. if (err)
  164. goto errout;
  165. if (tc_should_offload(dev, tp, flags)) {
  166. err = mall_replace_hw_filter(tp, f, (unsigned long) f);
  167. if (err) {
  168. if (tc_skip_sw(flags))
  169. goto errout;
  170. else
  171. err = 0;
  172. }
  173. }
  174. *arg = (unsigned long) f;
  175. rcu_assign_pointer(head->filter, f);
  176. return 0;
  177. errout:
  178. kfree(f);
  179. return err;
  180. }
  181. static int mall_delete(struct tcf_proto *tp, unsigned long arg)
  182. {
  183. struct cls_mall_head *head = rtnl_dereference(tp->root);
  184. struct cls_mall_filter *f = (struct cls_mall_filter *) arg;
  185. struct net_device *dev = tp->q->dev_queue->dev;
  186. if (tc_should_offload(dev, tp, f->flags))
  187. mall_destroy_hw_filter(tp, f, (unsigned long) f);
  188. RCU_INIT_POINTER(head->filter, NULL);
  189. tcf_unbind_filter(tp, &f->res);
  190. call_rcu(&f->rcu, mall_destroy_filter);
  191. return 0;
  192. }
  193. static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  194. {
  195. struct cls_mall_head *head = rtnl_dereference(tp->root);
  196. struct cls_mall_filter *f = head->filter;
  197. if (arg->count < arg->skip)
  198. goto skip;
  199. if (arg->fn(tp, (unsigned long) f, arg) < 0)
  200. arg->stop = 1;
  201. skip:
  202. arg->count++;
  203. }
  204. static int mall_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  205. struct sk_buff *skb, struct tcmsg *t)
  206. {
  207. struct cls_mall_filter *f = (struct cls_mall_filter *) fh;
  208. struct nlattr *nest;
  209. if (!f)
  210. return skb->len;
  211. t->tcm_handle = f->handle;
  212. nest = nla_nest_start(skb, TCA_OPTIONS);
  213. if (!nest)
  214. goto nla_put_failure;
  215. if (f->res.classid &&
  216. nla_put_u32(skb, TCA_MATCHALL_CLASSID, f->res.classid))
  217. goto nla_put_failure;
  218. if (tcf_exts_dump(skb, &f->exts))
  219. goto nla_put_failure;
  220. nla_nest_end(skb, nest);
  221. if (tcf_exts_dump_stats(skb, &f->exts) < 0)
  222. goto nla_put_failure;
  223. return skb->len;
  224. nla_put_failure:
  225. nla_nest_cancel(skb, nest);
  226. return -1;
  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. .owner = THIS_MODULE,
  239. };
  240. static int __init cls_mall_init(void)
  241. {
  242. return register_tcf_proto_ops(&cls_mall_ops);
  243. }
  244. static void __exit cls_mall_exit(void)
  245. {
  246. unregister_tcf_proto_ops(&cls_mall_ops);
  247. }
  248. module_init(cls_mall_init);
  249. module_exit(cls_mall_exit);
  250. MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
  251. MODULE_DESCRIPTION("Match-all classifier");
  252. MODULE_LICENSE("GPL v2");