1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/sched/cls_matchll.c Match-all classifier
5 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/percpu.h>
13 #include <net/sch_generic.h>
14 #include <net/pkt_cls.h>
15 #include <net/tc_wrapper.h>
17 struct cls_mall_head {
19 struct tcf_result res;
22 unsigned int in_hw_count;
23 struct tc_matchall_pcnt __percpu *pf;
24 struct rcu_work rwork;
28 TC_INDIRECT_SCOPE int mall_classify(struct sk_buff *skb,
29 const struct tcf_proto *tp,
30 struct tcf_result *res)
32 struct cls_mall_head *head = rcu_dereference_bh(tp->root);
37 if (tc_skip_sw(head->flags))
41 __this_cpu_inc(head->pf->rhit);
42 return tcf_exts_exec(skb, &head->exts, res);
45 static int mall_init(struct tcf_proto *tp)
50 static void __mall_destroy(struct cls_mall_head *head)
52 tcf_exts_destroy(&head->exts);
53 tcf_exts_put_net(&head->exts);
54 free_percpu(head->pf);
58 static void mall_destroy_work(struct work_struct *work)
60 struct cls_mall_head *head = container_of(to_rcu_work(work),
68 static void mall_destroy_hw_filter(struct tcf_proto *tp,
69 struct cls_mall_head *head,
71 struct netlink_ext_ack *extack)
73 struct tc_cls_matchall_offload cls_mall = {};
74 struct tcf_block *block = tp->chain->block;
76 tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
77 cls_mall.command = TC_CLSMATCHALL_DESTROY;
78 cls_mall.cookie = cookie;
80 tc_setup_cb_destroy(block, tp, TC_SETUP_CLSMATCHALL, &cls_mall, false,
81 &head->flags, &head->in_hw_count, true);
84 static int mall_replace_hw_filter(struct tcf_proto *tp,
85 struct cls_mall_head *head,
87 struct netlink_ext_ack *extack)
89 struct tc_cls_matchall_offload cls_mall = {};
90 struct tcf_block *block = tp->chain->block;
91 bool skip_sw = tc_skip_sw(head->flags);
94 cls_mall.rule = flow_rule_alloc(tcf_exts_num_actions(&head->exts));
98 tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
99 cls_mall.command = TC_CLSMATCHALL_REPLACE;
100 cls_mall.cookie = cookie;
102 err = tc_setup_offload_action(&cls_mall.rule->action, &head->exts,
103 cls_mall.common.extack);
105 kfree(cls_mall.rule);
106 mall_destroy_hw_filter(tp, head, cookie, NULL);
108 return skip_sw ? err : 0;
111 err = tc_setup_cb_add(block, tp, TC_SETUP_CLSMATCHALL, &cls_mall,
112 skip_sw, &head->flags, &head->in_hw_count, true);
113 tc_cleanup_offload_action(&cls_mall.rule->action);
114 kfree(cls_mall.rule);
117 mall_destroy_hw_filter(tp, head, cookie, NULL);
121 if (skip_sw && !(head->flags & TCA_CLS_FLAGS_IN_HW))
127 static void mall_destroy(struct tcf_proto *tp, bool rtnl_held,
128 struct netlink_ext_ack *extack)
130 struct cls_mall_head *head = rtnl_dereference(tp->root);
135 tcf_unbind_filter(tp, &head->res);
137 if (!tc_skip_hw(head->flags))
138 mall_destroy_hw_filter(tp, head, (unsigned long) head, extack);
140 if (tcf_exts_get_net(&head->exts))
141 tcf_queue_work(&head->rwork, mall_destroy_work);
143 __mall_destroy(head);
146 static void *mall_get(struct tcf_proto *tp, u32 handle)
148 struct cls_mall_head *head = rtnl_dereference(tp->root);
150 if (head && head->handle == handle)
156 static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
157 [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
158 [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
159 [TCA_MATCHALL_FLAGS] = { .type = NLA_U32 },
162 static int mall_set_parms(struct net *net, struct tcf_proto *tp,
163 struct cls_mall_head *head,
164 unsigned long base, struct nlattr **tb,
165 struct nlattr *est, u32 flags, u32 fl_flags,
166 struct netlink_ext_ack *extack)
170 err = tcf_exts_validate_ex(net, tp, tb, est, &head->exts, flags,
175 if (tb[TCA_MATCHALL_CLASSID]) {
176 head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
177 tcf_bind_filter(tp, &head->res, base);
182 static int mall_change(struct net *net, struct sk_buff *in_skb,
183 struct tcf_proto *tp, unsigned long base,
184 u32 handle, struct nlattr **tca,
185 void **arg, u32 flags,
186 struct netlink_ext_ack *extack)
188 struct cls_mall_head *head = rtnl_dereference(tp->root);
189 struct nlattr *tb[TCA_MATCHALL_MAX + 1];
190 struct cls_mall_head *new;
194 if (!tca[TCA_OPTIONS])
200 err = nla_parse_nested_deprecated(tb, TCA_MATCHALL_MAX,
201 tca[TCA_OPTIONS], mall_policy, NULL);
205 if (tb[TCA_MATCHALL_FLAGS]) {
206 userflags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
207 if (!tc_flags_valid(userflags))
211 new = kzalloc(sizeof(*new), GFP_KERNEL);
215 err = tcf_exts_init(&new->exts, net, TCA_MATCHALL_ACT, 0);
221 new->handle = handle;
222 new->flags = userflags;
223 new->pf = alloc_percpu(struct tc_matchall_pcnt);
226 goto err_alloc_percpu;
229 err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE],
230 flags, new->flags, extack);
234 if (!tc_skip_hw(new->flags)) {
235 err = mall_replace_hw_filter(tp, new, (unsigned long)new,
238 goto err_replace_hw_filter;
241 if (!tc_in_hw(new->flags))
242 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
245 rcu_assign_pointer(tp->root, new);
248 err_replace_hw_filter:
250 free_percpu(new->pf);
252 tcf_exts_destroy(&new->exts);
258 static int mall_delete(struct tcf_proto *tp, void *arg, bool *last,
259 bool rtnl_held, struct netlink_ext_ack *extack)
261 struct cls_mall_head *head = rtnl_dereference(tp->root);
263 head->deleting = true;
268 static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg,
271 struct cls_mall_head *head = rtnl_dereference(tp->root);
273 if (arg->count < arg->skip)
276 if (!head || head->deleting)
278 if (arg->fn(tp, head, arg) < 0)
284 static int mall_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
285 void *cb_priv, struct netlink_ext_ack *extack)
287 struct cls_mall_head *head = rtnl_dereference(tp->root);
288 struct tc_cls_matchall_offload cls_mall = {};
289 struct tcf_block *block = tp->chain->block;
292 if (tc_skip_hw(head->flags))
295 cls_mall.rule = flow_rule_alloc(tcf_exts_num_actions(&head->exts));
299 tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
300 cls_mall.command = add ?
301 TC_CLSMATCHALL_REPLACE : TC_CLSMATCHALL_DESTROY;
302 cls_mall.cookie = (unsigned long)head;
304 err = tc_setup_offload_action(&cls_mall.rule->action, &head->exts,
305 cls_mall.common.extack);
307 kfree(cls_mall.rule);
309 return add && tc_skip_sw(head->flags) ? err : 0;
312 err = tc_setup_cb_reoffload(block, tp, add, cb, TC_SETUP_CLSMATCHALL,
313 &cls_mall, cb_priv, &head->flags,
315 tc_cleanup_offload_action(&cls_mall.rule->action);
316 kfree(cls_mall.rule);
321 static void mall_stats_hw_filter(struct tcf_proto *tp,
322 struct cls_mall_head *head,
323 unsigned long cookie)
325 struct tc_cls_matchall_offload cls_mall = {};
326 struct tcf_block *block = tp->chain->block;
328 tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, NULL);
329 cls_mall.command = TC_CLSMATCHALL_STATS;
330 cls_mall.cookie = cookie;
332 tc_setup_cb_call(block, TC_SETUP_CLSMATCHALL, &cls_mall, false, true);
334 tcf_exts_hw_stats_update(&head->exts, &cls_mall.stats, cls_mall.use_act_stats);
337 static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh,
338 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
340 struct tc_matchall_pcnt gpf = {};
341 struct cls_mall_head *head = fh;
348 if (!tc_skip_hw(head->flags))
349 mall_stats_hw_filter(tp, head, (unsigned long)head);
351 t->tcm_handle = head->handle;
353 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
355 goto nla_put_failure;
357 if (head->res.classid &&
358 nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
359 goto nla_put_failure;
361 if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags))
362 goto nla_put_failure;
364 for_each_possible_cpu(cpu) {
365 struct tc_matchall_pcnt *pf = per_cpu_ptr(head->pf, cpu);
367 gpf.rhit += pf->rhit;
370 if (nla_put_64bit(skb, TCA_MATCHALL_PCNT,
371 sizeof(struct tc_matchall_pcnt),
372 &gpf, TCA_MATCHALL_PAD))
373 goto nla_put_failure;
375 if (tcf_exts_dump(skb, &head->exts))
376 goto nla_put_failure;
378 nla_nest_end(skb, nest);
380 if (tcf_exts_dump_stats(skb, &head->exts) < 0)
381 goto nla_put_failure;
386 nla_nest_cancel(skb, nest);
390 static void mall_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
393 struct cls_mall_head *head = fh;
395 tc_cls_bind_class(classid, cl, q, &head->res, base);
398 static struct tcf_proto_ops cls_mall_ops __read_mostly = {
400 .classify = mall_classify,
402 .destroy = mall_destroy,
404 .change = mall_change,
405 .delete = mall_delete,
407 .reoffload = mall_reoffload,
409 .bind_class = mall_bind_class,
410 .owner = THIS_MODULE,
413 static int __init cls_mall_init(void)
415 return register_tcf_proto_ops(&cls_mall_ops);
418 static void __exit cls_mall_exit(void)
420 unregister_tcf_proto_ops(&cls_mall_ops);
423 module_init(cls_mall_init);
424 module_exit(cls_mall_exit);
426 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
427 MODULE_DESCRIPTION("Match-all classifier");
428 MODULE_LICENSE("GPL v2");