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>
16 struct cls_mall_head {
18 struct tcf_result res;
21 unsigned int in_hw_count;
22 struct tc_matchall_pcnt __percpu *pf;
23 struct rcu_work rwork;
27 static int mall_classify(struct sk_buff *skb, const struct tcf_proto *tp,
28 struct tcf_result *res)
30 struct cls_mall_head *head = rcu_dereference_bh(tp->root);
35 if (tc_skip_sw(head->flags))
39 __this_cpu_inc(head->pf->rhit);
40 return tcf_exts_exec(skb, &head->exts, res);
43 static int mall_init(struct tcf_proto *tp)
48 static void __mall_destroy(struct cls_mall_head *head)
50 tcf_exts_destroy(&head->exts);
51 tcf_exts_put_net(&head->exts);
52 free_percpu(head->pf);
56 static void mall_destroy_work(struct work_struct *work)
58 struct cls_mall_head *head = container_of(to_rcu_work(work),
66 static void mall_destroy_hw_filter(struct tcf_proto *tp,
67 struct cls_mall_head *head,
69 struct netlink_ext_ack *extack)
71 struct tc_cls_matchall_offload cls_mall = {};
72 struct tcf_block *block = tp->chain->block;
74 tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
75 cls_mall.command = TC_CLSMATCHALL_DESTROY;
76 cls_mall.cookie = cookie;
78 tc_setup_cb_destroy(block, tp, TC_SETUP_CLSMATCHALL, &cls_mall, false,
79 &head->flags, &head->in_hw_count, true);
82 static int mall_replace_hw_filter(struct tcf_proto *tp,
83 struct cls_mall_head *head,
85 struct netlink_ext_ack *extack)
87 struct tc_cls_matchall_offload cls_mall = {};
88 struct tcf_block *block = tp->chain->block;
89 bool skip_sw = tc_skip_sw(head->flags);
92 cls_mall.rule = flow_rule_alloc(tcf_exts_num_actions(&head->exts));
96 tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
97 cls_mall.command = TC_CLSMATCHALL_REPLACE;
98 cls_mall.cookie = cookie;
100 err = tc_setup_offload_action(&cls_mall.rule->action, &head->exts,
101 cls_mall.common.extack);
103 kfree(cls_mall.rule);
104 mall_destroy_hw_filter(tp, head, cookie, NULL);
106 return skip_sw ? err : 0;
109 err = tc_setup_cb_add(block, tp, TC_SETUP_CLSMATCHALL, &cls_mall,
110 skip_sw, &head->flags, &head->in_hw_count, true);
111 tc_cleanup_offload_action(&cls_mall.rule->action);
112 kfree(cls_mall.rule);
115 mall_destroy_hw_filter(tp, head, cookie, NULL);
119 if (skip_sw && !(head->flags & TCA_CLS_FLAGS_IN_HW))
125 static void mall_destroy(struct tcf_proto *tp, bool rtnl_held,
126 struct netlink_ext_ack *extack)
128 struct cls_mall_head *head = rtnl_dereference(tp->root);
133 tcf_unbind_filter(tp, &head->res);
135 if (!tc_skip_hw(head->flags))
136 mall_destroy_hw_filter(tp, head, (unsigned long) head, extack);
138 if (tcf_exts_get_net(&head->exts))
139 tcf_queue_work(&head->rwork, mall_destroy_work);
141 __mall_destroy(head);
144 static void *mall_get(struct tcf_proto *tp, u32 handle)
146 struct cls_mall_head *head = rtnl_dereference(tp->root);
148 if (head && head->handle == handle)
154 static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
155 [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
156 [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
157 [TCA_MATCHALL_FLAGS] = { .type = NLA_U32 },
160 static int mall_change(struct net *net, struct sk_buff *in_skb,
161 struct tcf_proto *tp, unsigned long base,
162 u32 handle, struct nlattr **tca,
163 void **arg, u32 flags,
164 struct netlink_ext_ack *extack)
166 struct cls_mall_head *head = rtnl_dereference(tp->root);
167 struct nlattr *tb[TCA_MATCHALL_MAX + 1];
168 bool bound_to_filter = false;
169 struct cls_mall_head *new;
173 if (!tca[TCA_OPTIONS])
179 err = nla_parse_nested_deprecated(tb, TCA_MATCHALL_MAX,
180 tca[TCA_OPTIONS], mall_policy, NULL);
184 if (tb[TCA_MATCHALL_FLAGS]) {
185 userflags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
186 if (!tc_flags_valid(userflags))
190 new = kzalloc(sizeof(*new), GFP_KERNEL);
194 err = tcf_exts_init(&new->exts, net, TCA_MATCHALL_ACT, 0);
200 new->handle = handle;
201 new->flags = userflags;
202 new->pf = alloc_percpu(struct tc_matchall_pcnt);
205 goto err_alloc_percpu;
208 err = tcf_exts_validate_ex(net, tp, tb, tca[TCA_RATE],
209 &new->exts, flags, new->flags, extack);
213 if (tb[TCA_MATCHALL_CLASSID]) {
214 new->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
215 tcf_bind_filter(tp, &new->res, base);
216 bound_to_filter = true;
219 if (!tc_skip_hw(new->flags)) {
220 err = mall_replace_hw_filter(tp, new, (unsigned long)new,
223 goto err_replace_hw_filter;
226 if (!tc_in_hw(new->flags))
227 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
230 rcu_assign_pointer(tp->root, new);
233 err_replace_hw_filter:
235 tcf_unbind_filter(tp, &new->res);
237 free_percpu(new->pf);
239 tcf_exts_destroy(&new->exts);
245 static int mall_delete(struct tcf_proto *tp, void *arg, bool *last,
246 bool rtnl_held, struct netlink_ext_ack *extack)
248 struct cls_mall_head *head = rtnl_dereference(tp->root);
250 head->deleting = true;
255 static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg,
258 struct cls_mall_head *head = rtnl_dereference(tp->root);
260 if (arg->count < arg->skip)
263 if (!head || head->deleting)
265 if (arg->fn(tp, head, arg) < 0)
271 static int mall_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
272 void *cb_priv, struct netlink_ext_ack *extack)
274 struct cls_mall_head *head = rtnl_dereference(tp->root);
275 struct tc_cls_matchall_offload cls_mall = {};
276 struct tcf_block *block = tp->chain->block;
279 if (tc_skip_hw(head->flags))
282 cls_mall.rule = flow_rule_alloc(tcf_exts_num_actions(&head->exts));
286 tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
287 cls_mall.command = add ?
288 TC_CLSMATCHALL_REPLACE : TC_CLSMATCHALL_DESTROY;
289 cls_mall.cookie = (unsigned long)head;
291 err = tc_setup_offload_action(&cls_mall.rule->action, &head->exts,
292 cls_mall.common.extack);
294 kfree(cls_mall.rule);
296 return add && tc_skip_sw(head->flags) ? err : 0;
299 err = tc_setup_cb_reoffload(block, tp, add, cb, TC_SETUP_CLSMATCHALL,
300 &cls_mall, cb_priv, &head->flags,
302 tc_cleanup_offload_action(&cls_mall.rule->action);
303 kfree(cls_mall.rule);
308 static void mall_stats_hw_filter(struct tcf_proto *tp,
309 struct cls_mall_head *head,
310 unsigned long cookie)
312 struct tc_cls_matchall_offload cls_mall = {};
313 struct tcf_block *block = tp->chain->block;
315 tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, NULL);
316 cls_mall.command = TC_CLSMATCHALL_STATS;
317 cls_mall.cookie = cookie;
319 tc_setup_cb_call(block, TC_SETUP_CLSMATCHALL, &cls_mall, false, true);
321 tcf_exts_hw_stats_update(&head->exts, cls_mall.stats.bytes,
322 cls_mall.stats.pkts, cls_mall.stats.drops,
323 cls_mall.stats.lastused,
324 cls_mall.stats.used_hw_stats,
325 cls_mall.stats.used_hw_stats_valid);
328 static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh,
329 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
331 struct tc_matchall_pcnt gpf = {};
332 struct cls_mall_head *head = fh;
339 if (!tc_skip_hw(head->flags))
340 mall_stats_hw_filter(tp, head, (unsigned long)head);
342 t->tcm_handle = head->handle;
344 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
346 goto nla_put_failure;
348 if (head->res.classid &&
349 nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
350 goto nla_put_failure;
352 if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags))
353 goto nla_put_failure;
355 for_each_possible_cpu(cpu) {
356 struct tc_matchall_pcnt *pf = per_cpu_ptr(head->pf, cpu);
358 gpf.rhit += pf->rhit;
361 if (nla_put_64bit(skb, TCA_MATCHALL_PCNT,
362 sizeof(struct tc_matchall_pcnt),
363 &gpf, TCA_MATCHALL_PAD))
364 goto nla_put_failure;
366 if (tcf_exts_dump(skb, &head->exts))
367 goto nla_put_failure;
369 nla_nest_end(skb, nest);
371 if (tcf_exts_dump_stats(skb, &head->exts) < 0)
372 goto nla_put_failure;
377 nla_nest_cancel(skb, nest);
381 static void mall_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
384 struct cls_mall_head *head = fh;
386 tc_cls_bind_class(classid, cl, q, &head->res, base);
389 static struct tcf_proto_ops cls_mall_ops __read_mostly = {
391 .classify = mall_classify,
393 .destroy = mall_destroy,
395 .change = mall_change,
396 .delete = mall_delete,
398 .reoffload = mall_reoffload,
400 .bind_class = mall_bind_class,
401 .owner = THIS_MODULE,
404 static int __init cls_mall_init(void)
406 return register_tcf_proto_ops(&cls_mall_ops);
409 static void __exit cls_mall_exit(void)
411 unregister_tcf_proto_ops(&cls_mall_ops);
414 module_init(cls_mall_init);
415 module_exit(cls_mall_exit);
417 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
418 MODULE_DESCRIPTION("Match-all classifier");
419 MODULE_LICENSE("GPL v2");