cls_matchall.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/cls_matchll.c Match-all classifier
  4. *
  5. * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/percpu.h>
  11. #include <net/sch_generic.h>
  12. #include <net/pkt_cls.h>
  13. #include <net/tc_wrapper.h>
  14. struct cls_mall_head {
  15. struct tcf_exts exts;
  16. struct tcf_result res;
  17. u32 handle;
  18. u32 flags;
  19. unsigned int in_hw_count;
  20. struct tc_matchall_pcnt __percpu *pf;
  21. struct rcu_work rwork;
  22. bool deleting;
  23. };
  24. TC_INDIRECT_SCOPE int mall_classify(struct sk_buff *skb,
  25. const struct tcf_proto *tp,
  26. struct tcf_result *res)
  27. {
  28. struct cls_mall_head *head = rcu_dereference_bh(tp->root);
  29. if (unlikely(!head))
  30. return -1;
  31. if (tc_skip_sw(head->flags))
  32. return -1;
  33. *res = head->res;
  34. __this_cpu_inc(head->pf->rhit);
  35. return tcf_exts_exec(skb, &head->exts, res);
  36. }
  37. static int mall_init(struct tcf_proto *tp)
  38. {
  39. return 0;
  40. }
  41. static void __mall_destroy(struct cls_mall_head *head)
  42. {
  43. tcf_exts_destroy(&head->exts);
  44. tcf_exts_put_net(&head->exts);
  45. free_percpu(head->pf);
  46. kfree(head);
  47. }
  48. static void mall_destroy_work(struct work_struct *work)
  49. {
  50. struct cls_mall_head *head = container_of(to_rcu_work(work),
  51. struct cls_mall_head,
  52. rwork);
  53. rtnl_lock();
  54. __mall_destroy(head);
  55. rtnl_unlock();
  56. }
  57. static void mall_destroy_hw_filter(struct tcf_proto *tp,
  58. struct cls_mall_head *head,
  59. unsigned long cookie,
  60. struct netlink_ext_ack *extack)
  61. {
  62. struct tc_cls_matchall_offload cls_mall = {};
  63. struct tcf_block *block = tp->chain->block;
  64. tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
  65. cls_mall.command = TC_CLSMATCHALL_DESTROY;
  66. cls_mall.cookie = cookie;
  67. tc_setup_cb_destroy(block, tp, TC_SETUP_CLSMATCHALL, &cls_mall, false,
  68. &head->flags, &head->in_hw_count, true);
  69. }
  70. static int mall_replace_hw_filter(struct tcf_proto *tp,
  71. struct cls_mall_head *head,
  72. unsigned long cookie,
  73. struct netlink_ext_ack *extack)
  74. {
  75. struct tc_cls_matchall_offload cls_mall = {};
  76. struct tcf_block *block = tp->chain->block;
  77. bool skip_sw = tc_skip_sw(head->flags);
  78. int err;
  79. cls_mall.rule = flow_rule_alloc(tcf_exts_num_actions(&head->exts));
  80. if (!cls_mall.rule)
  81. return -ENOMEM;
  82. tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
  83. cls_mall.command = TC_CLSMATCHALL_REPLACE;
  84. cls_mall.cookie = cookie;
  85. err = tc_setup_offload_action(&cls_mall.rule->action, &head->exts,
  86. cls_mall.common.extack);
  87. if (err) {
  88. kfree(cls_mall.rule);
  89. mall_destroy_hw_filter(tp, head, cookie, NULL);
  90. return skip_sw ? err : 0;
  91. }
  92. err = tc_setup_cb_add(block, tp, TC_SETUP_CLSMATCHALL, &cls_mall,
  93. skip_sw, &head->flags, &head->in_hw_count, true);
  94. tc_cleanup_offload_action(&cls_mall.rule->action);
  95. kfree(cls_mall.rule);
  96. if (err) {
  97. mall_destroy_hw_filter(tp, head, cookie, NULL);
  98. return err;
  99. }
  100. if (skip_sw && !(head->flags & TCA_CLS_FLAGS_IN_HW))
  101. return -EINVAL;
  102. return 0;
  103. }
  104. static void mall_destroy(struct tcf_proto *tp, bool rtnl_held,
  105. struct netlink_ext_ack *extack)
  106. {
  107. struct cls_mall_head *head = rtnl_dereference(tp->root);
  108. if (!head)
  109. return;
  110. tcf_unbind_filter(tp, &head->res);
  111. if (!tc_skip_hw(head->flags))
  112. mall_destroy_hw_filter(tp, head, (unsigned long) head, extack);
  113. if (tcf_exts_get_net(&head->exts))
  114. tcf_queue_work(&head->rwork, mall_destroy_work);
  115. else
  116. __mall_destroy(head);
  117. }
  118. static void *mall_get(struct tcf_proto *tp, u32 handle)
  119. {
  120. struct cls_mall_head *head = rtnl_dereference(tp->root);
  121. if (head && head->handle == handle)
  122. return head;
  123. return NULL;
  124. }
  125. static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
  126. [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
  127. [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
  128. [TCA_MATCHALL_FLAGS] = { .type = NLA_U32 },
  129. };
  130. static int mall_change(struct net *net, struct sk_buff *in_skb,
  131. struct tcf_proto *tp, unsigned long base,
  132. u32 handle, struct nlattr **tca,
  133. void **arg, u32 flags,
  134. struct netlink_ext_ack *extack)
  135. {
  136. struct cls_mall_head *head = rtnl_dereference(tp->root);
  137. struct nlattr *tb[TCA_MATCHALL_MAX + 1];
  138. bool bound_to_filter = false;
  139. struct cls_mall_head *new;
  140. u32 userflags = 0;
  141. int err;
  142. if (!tca[TCA_OPTIONS])
  143. return -EINVAL;
  144. if (head)
  145. return -EEXIST;
  146. err = nla_parse_nested_deprecated(tb, TCA_MATCHALL_MAX,
  147. tca[TCA_OPTIONS], mall_policy, NULL);
  148. if (err < 0)
  149. return err;
  150. if (tb[TCA_MATCHALL_FLAGS]) {
  151. userflags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
  152. if (!tc_flags_valid(userflags))
  153. return -EINVAL;
  154. }
  155. new = kzalloc(sizeof(*new), GFP_KERNEL);
  156. if (!new)
  157. return -ENOBUFS;
  158. err = tcf_exts_init(&new->exts, net, TCA_MATCHALL_ACT, 0);
  159. if (err)
  160. goto err_exts_init;
  161. if (!handle)
  162. handle = 1;
  163. new->handle = handle;
  164. new->flags = userflags;
  165. new->pf = alloc_percpu(struct tc_matchall_pcnt);
  166. if (!new->pf) {
  167. err = -ENOMEM;
  168. goto err_alloc_percpu;
  169. }
  170. err = tcf_exts_validate_ex(net, tp, tb, tca[TCA_RATE],
  171. &new->exts, flags, new->flags, extack);
  172. if (err < 0)
  173. goto err_set_parms;
  174. if (tb[TCA_MATCHALL_CLASSID]) {
  175. new->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
  176. tcf_bind_filter(tp, &new->res, base);
  177. bound_to_filter = true;
  178. }
  179. if (!tc_skip_hw(new->flags)) {
  180. err = mall_replace_hw_filter(tp, new, (unsigned long)new,
  181. extack);
  182. if (err)
  183. goto err_replace_hw_filter;
  184. }
  185. if (!tc_in_hw(new->flags))
  186. new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
  187. tcf_proto_update_usesw(tp, new->flags);
  188. *arg = head;
  189. rcu_assign_pointer(tp->root, new);
  190. return 0;
  191. err_replace_hw_filter:
  192. if (bound_to_filter)
  193. tcf_unbind_filter(tp, &new->res);
  194. err_set_parms:
  195. free_percpu(new->pf);
  196. err_alloc_percpu:
  197. tcf_exts_destroy(&new->exts);
  198. err_exts_init:
  199. kfree(new);
  200. return err;
  201. }
  202. static int mall_delete(struct tcf_proto *tp, void *arg, bool *last,
  203. bool rtnl_held, struct netlink_ext_ack *extack)
  204. {
  205. struct cls_mall_head *head = rtnl_dereference(tp->root);
  206. head->deleting = true;
  207. *last = true;
  208. return 0;
  209. }
  210. static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg,
  211. bool rtnl_held)
  212. {
  213. struct cls_mall_head *head = rtnl_dereference(tp->root);
  214. if (arg->count < arg->skip)
  215. goto skip;
  216. if (!head || head->deleting)
  217. return;
  218. if (arg->fn(tp, head, arg) < 0)
  219. arg->stop = 1;
  220. skip:
  221. arg->count++;
  222. }
  223. static int mall_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
  224. void *cb_priv, struct netlink_ext_ack *extack)
  225. {
  226. struct cls_mall_head *head = rtnl_dereference(tp->root);
  227. struct tc_cls_matchall_offload cls_mall = {};
  228. struct tcf_block *block = tp->chain->block;
  229. int err;
  230. if (tc_skip_hw(head->flags))
  231. return 0;
  232. cls_mall.rule = flow_rule_alloc(tcf_exts_num_actions(&head->exts));
  233. if (!cls_mall.rule)
  234. return -ENOMEM;
  235. tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
  236. cls_mall.command = add ?
  237. TC_CLSMATCHALL_REPLACE : TC_CLSMATCHALL_DESTROY;
  238. cls_mall.cookie = (unsigned long)head;
  239. err = tc_setup_offload_action(&cls_mall.rule->action, &head->exts,
  240. cls_mall.common.extack);
  241. if (err) {
  242. kfree(cls_mall.rule);
  243. return add && tc_skip_sw(head->flags) ? err : 0;
  244. }
  245. err = tc_setup_cb_reoffload(block, tp, add, cb, TC_SETUP_CLSMATCHALL,
  246. &cls_mall, cb_priv, &head->flags,
  247. &head->in_hw_count);
  248. tc_cleanup_offload_action(&cls_mall.rule->action);
  249. kfree(cls_mall.rule);
  250. return err;
  251. }
  252. static void mall_stats_hw_filter(struct tcf_proto *tp,
  253. struct cls_mall_head *head,
  254. unsigned long cookie)
  255. {
  256. struct tc_cls_matchall_offload cls_mall = {};
  257. struct tcf_block *block = tp->chain->block;
  258. tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, NULL);
  259. cls_mall.command = TC_CLSMATCHALL_STATS;
  260. cls_mall.cookie = cookie;
  261. tc_setup_cb_call(block, TC_SETUP_CLSMATCHALL, &cls_mall, false, true);
  262. tcf_exts_hw_stats_update(&head->exts, &cls_mall.stats, cls_mall.use_act_stats);
  263. }
  264. static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh,
  265. struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
  266. {
  267. struct tc_matchall_pcnt gpf = {};
  268. struct cls_mall_head *head = fh;
  269. struct nlattr *nest;
  270. int cpu;
  271. if (!head)
  272. return skb->len;
  273. if (!tc_skip_hw(head->flags))
  274. mall_stats_hw_filter(tp, head, (unsigned long)head);
  275. t->tcm_handle = head->handle;
  276. nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
  277. if (!nest)
  278. goto nla_put_failure;
  279. if (head->res.classid &&
  280. nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
  281. goto nla_put_failure;
  282. if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags))
  283. goto nla_put_failure;
  284. for_each_possible_cpu(cpu) {
  285. struct tc_matchall_pcnt *pf = per_cpu_ptr(head->pf, cpu);
  286. gpf.rhit += pf->rhit;
  287. }
  288. if (nla_put_64bit(skb, TCA_MATCHALL_PCNT,
  289. sizeof(struct tc_matchall_pcnt),
  290. &gpf, TCA_MATCHALL_PAD))
  291. goto nla_put_failure;
  292. if (tcf_exts_dump(skb, &head->exts))
  293. goto nla_put_failure;
  294. nla_nest_end(skb, nest);
  295. if (tcf_exts_dump_stats(skb, &head->exts) < 0)
  296. goto nla_put_failure;
  297. return skb->len;
  298. nla_put_failure:
  299. nla_nest_cancel(skb, nest);
  300. return -1;
  301. }
  302. static void mall_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
  303. unsigned long base)
  304. {
  305. struct cls_mall_head *head = fh;
  306. tc_cls_bind_class(classid, cl, q, &head->res, base);
  307. }
  308. static struct tcf_proto_ops cls_mall_ops __read_mostly = {
  309. .kind = "matchall",
  310. .classify = mall_classify,
  311. .init = mall_init,
  312. .destroy = mall_destroy,
  313. .get = mall_get,
  314. .change = mall_change,
  315. .delete = mall_delete,
  316. .walk = mall_walk,
  317. .reoffload = mall_reoffload,
  318. .dump = mall_dump,
  319. .bind_class = mall_bind_class,
  320. .owner = THIS_MODULE,
  321. };
  322. MODULE_ALIAS_NET_CLS("matchall");
  323. static int __init cls_mall_init(void)
  324. {
  325. return register_tcf_proto_ops(&cls_mall_ops);
  326. }
  327. static void __exit cls_mall_exit(void)
  328. {
  329. unregister_tcf_proto_ops(&cls_mall_ops);
  330. }
  331. module_init(cls_mall_init);
  332. module_exit(cls_mall_exit);
  333. MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
  334. MODULE_DESCRIPTION("Match-all classifier");
  335. MODULE_LICENSE("GPL v2");