cls_matchall.c 6.4 KB

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