cls_matchall.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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_work rwork;
  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(struct cls_mall_head *head)
  37. {
  38. tcf_exts_destroy(&head->exts);
  39. tcf_exts_put_net(&head->exts);
  40. kfree(head);
  41. }
  42. static void mall_destroy_work(struct work_struct *work)
  43. {
  44. struct cls_mall_head *head = container_of(to_rcu_work(work),
  45. struct cls_mall_head,
  46. rwork);
  47. rtnl_lock();
  48. __mall_destroy(head);
  49. rtnl_unlock();
  50. }
  51. static void mall_destroy_hw_filter(struct tcf_proto *tp,
  52. struct cls_mall_head *head,
  53. unsigned long cookie,
  54. struct netlink_ext_ack *extack)
  55. {
  56. struct tc_cls_matchall_offload cls_mall = {};
  57. struct tcf_block *block = tp->chain->block;
  58. tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
  59. cls_mall.command = TC_CLSMATCHALL_DESTROY;
  60. cls_mall.cookie = cookie;
  61. tc_setup_cb_call(block, NULL, TC_SETUP_CLSMATCHALL, &cls_mall, false);
  62. tcf_block_offload_dec(block, &head->flags);
  63. }
  64. static int mall_replace_hw_filter(struct tcf_proto *tp,
  65. struct cls_mall_head *head,
  66. unsigned long cookie,
  67. struct netlink_ext_ack *extack)
  68. {
  69. struct tc_cls_matchall_offload cls_mall = {};
  70. struct tcf_block *block = tp->chain->block;
  71. bool skip_sw = tc_skip_sw(head->flags);
  72. int err;
  73. tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
  74. cls_mall.command = TC_CLSMATCHALL_REPLACE;
  75. cls_mall.exts = &head->exts;
  76. cls_mall.cookie = cookie;
  77. err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSMATCHALL,
  78. &cls_mall, skip_sw);
  79. if (err < 0) {
  80. mall_destroy_hw_filter(tp, head, cookie, NULL);
  81. return err;
  82. } else if (err > 0) {
  83. tcf_block_offload_inc(block, &head->flags);
  84. }
  85. if (skip_sw && !(head->flags & TCA_CLS_FLAGS_IN_HW))
  86. return -EINVAL;
  87. return 0;
  88. }
  89. static void mall_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
  90. {
  91. struct cls_mall_head *head = rtnl_dereference(tp->root);
  92. if (!head)
  93. return;
  94. if (!tc_skip_hw(head->flags))
  95. mall_destroy_hw_filter(tp, head, (unsigned long) head, extack);
  96. if (tcf_exts_get_net(&head->exts))
  97. tcf_queue_work(&head->rwork, mall_destroy_work);
  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. struct netlink_ext_ack *extack)
  114. {
  115. int err;
  116. err = tcf_exts_validate(net, tp, tb, est, &head->exts, ovr, extack);
  117. if (err < 0)
  118. return err;
  119. if (tb[TCA_MATCHALL_CLASSID]) {
  120. head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
  121. tcf_bind_filter(tp, &head->res, base);
  122. }
  123. return 0;
  124. }
  125. static int mall_change(struct net *net, struct sk_buff *in_skb,
  126. struct tcf_proto *tp, unsigned long base,
  127. u32 handle, struct nlattr **tca,
  128. void **arg, bool ovr, struct netlink_ext_ack *extack)
  129. {
  130. struct cls_mall_head *head = rtnl_dereference(tp->root);
  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. extack);
  160. if (err)
  161. goto err_set_parms;
  162. if (!tc_skip_hw(new->flags)) {
  163. err = mall_replace_hw_filter(tp, new, (unsigned long)new,
  164. extack);
  165. if (err)
  166. goto err_replace_hw_filter;
  167. }
  168. if (!tc_in_hw(new->flags))
  169. new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
  170. *arg = head;
  171. rcu_assign_pointer(tp->root, new);
  172. return 0;
  173. err_replace_hw_filter:
  174. err_set_parms:
  175. tcf_exts_destroy(&new->exts);
  176. err_exts_init:
  177. kfree(new);
  178. return err;
  179. }
  180. static int mall_delete(struct tcf_proto *tp, void *arg, bool *last,
  181. struct netlink_ext_ack *extack)
  182. {
  183. return -EOPNOTSUPP;
  184. }
  185. static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  186. {
  187. struct cls_mall_head *head = rtnl_dereference(tp->root);
  188. if (arg->count < arg->skip)
  189. goto skip;
  190. if (arg->fn(tp, head, arg) < 0)
  191. arg->stop = 1;
  192. skip:
  193. arg->count++;
  194. }
  195. static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh,
  196. struct sk_buff *skb, struct tcmsg *t)
  197. {
  198. struct cls_mall_head *head = fh;
  199. struct nlattr *nest;
  200. if (!head)
  201. return skb->len;
  202. t->tcm_handle = head->handle;
  203. nest = nla_nest_start(skb, TCA_OPTIONS);
  204. if (!nest)
  205. goto nla_put_failure;
  206. if (head->res.classid &&
  207. nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
  208. goto nla_put_failure;
  209. if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags))
  210. goto nla_put_failure;
  211. if (tcf_exts_dump(skb, &head->exts))
  212. goto nla_put_failure;
  213. nla_nest_end(skb, nest);
  214. if (tcf_exts_dump_stats(skb, &head->exts) < 0)
  215. goto nla_put_failure;
  216. return skb->len;
  217. nla_put_failure:
  218. nla_nest_cancel(skb, nest);
  219. return -1;
  220. }
  221. static void mall_bind_class(void *fh, u32 classid, unsigned long cl)
  222. {
  223. struct cls_mall_head *head = fh;
  224. if (head && head->res.classid == classid)
  225. head->res.class = cl;
  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. .bind_class = mall_bind_class,
  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");