2 * net/sched/cls_matchll.c Match-all classifier
4 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
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.
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/percpu.h>
17 #include <net/sch_generic.h>
18 #include <net/pkt_cls.h>
20 struct cls_mall_head {
22 struct tcf_result res;
25 unsigned int in_hw_count;
26 struct tc_matchall_pcnt __percpu *pf;
27 struct rcu_work rwork;
30 static int mall_classify(struct sk_buff *skb, const struct tcf_proto *tp,
31 struct tcf_result *res)
33 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_call(block, TC_SETUP_CLSMATCHALL, &cls_mall, false);
79 tcf_block_offload_dec(block, &head->flags);
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 tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
93 cls_mall.command = TC_CLSMATCHALL_REPLACE;
94 cls_mall.exts = &head->exts;
95 cls_mall.cookie = cookie;
97 err = tc_setup_cb_call(block, TC_SETUP_CLSMATCHALL, &cls_mall, skip_sw);
99 mall_destroy_hw_filter(tp, head, cookie, NULL);
101 } else if (err > 0) {
102 head->in_hw_count = err;
103 tcf_block_offload_inc(block, &head->flags);
106 if (skip_sw && !(head->flags & TCA_CLS_FLAGS_IN_HW))
112 static void mall_destroy(struct tcf_proto *tp, bool rtnl_held,
113 struct netlink_ext_ack *extack)
115 struct cls_mall_head *head = rtnl_dereference(tp->root);
120 tcf_unbind_filter(tp, &head->res);
122 if (!tc_skip_hw(head->flags))
123 mall_destroy_hw_filter(tp, head, (unsigned long) head, extack);
125 if (tcf_exts_get_net(&head->exts))
126 tcf_queue_work(&head->rwork, mall_destroy_work);
128 __mall_destroy(head);
131 static void *mall_get(struct tcf_proto *tp, u32 handle)
133 struct cls_mall_head *head = rtnl_dereference(tp->root);
135 if (head && head->handle == handle)
141 static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
142 [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
143 [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
146 static int mall_set_parms(struct net *net, struct tcf_proto *tp,
147 struct cls_mall_head *head,
148 unsigned long base, struct nlattr **tb,
149 struct nlattr *est, bool ovr,
150 struct netlink_ext_ack *extack)
154 err = tcf_exts_validate(net, tp, tb, est, &head->exts, ovr, true,
159 if (tb[TCA_MATCHALL_CLASSID]) {
160 head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
161 tcf_bind_filter(tp, &head->res, base);
166 static int mall_change(struct net *net, struct sk_buff *in_skb,
167 struct tcf_proto *tp, unsigned long base,
168 u32 handle, struct nlattr **tca,
169 void **arg, bool ovr, bool rtnl_held,
170 struct netlink_ext_ack *extack)
172 struct cls_mall_head *head = rtnl_dereference(tp->root);
173 struct nlattr *tb[TCA_MATCHALL_MAX + 1];
174 struct cls_mall_head *new;
178 if (!tca[TCA_OPTIONS])
184 err = nla_parse_nested(tb, TCA_MATCHALL_MAX, tca[TCA_OPTIONS],
189 if (tb[TCA_MATCHALL_FLAGS]) {
190 flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
191 if (!tc_flags_valid(flags))
195 new = kzalloc(sizeof(*new), GFP_KERNEL);
199 err = tcf_exts_init(&new->exts, net, TCA_MATCHALL_ACT, 0);
205 new->handle = handle;
207 new->pf = alloc_percpu(struct tc_matchall_pcnt);
210 goto err_alloc_percpu;
213 err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr,
218 if (!tc_skip_hw(new->flags)) {
219 err = mall_replace_hw_filter(tp, new, (unsigned long)new,
222 goto err_replace_hw_filter;
225 if (!tc_in_hw(new->flags))
226 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
229 rcu_assign_pointer(tp->root, new);
232 err_replace_hw_filter:
234 free_percpu(new->pf);
236 tcf_exts_destroy(&new->exts);
242 static int mall_delete(struct tcf_proto *tp, void *arg, bool *last,
243 bool rtnl_held, struct netlink_ext_ack *extack)
248 static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg,
251 struct cls_mall_head *head = rtnl_dereference(tp->root);
253 if (arg->count < arg->skip)
258 if (arg->fn(tp, head, arg) < 0)
264 static int mall_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
265 void *cb_priv, struct netlink_ext_ack *extack)
267 struct cls_mall_head *head = rtnl_dereference(tp->root);
268 struct tc_cls_matchall_offload cls_mall = {};
269 struct tcf_block *block = tp->chain->block;
272 if (tc_skip_hw(head->flags))
275 tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
276 cls_mall.command = add ?
277 TC_CLSMATCHALL_REPLACE : TC_CLSMATCHALL_DESTROY;
278 cls_mall.exts = &head->exts;
279 cls_mall.cookie = (unsigned long)head;
281 err = cb(TC_SETUP_CLSMATCHALL, &cls_mall, cb_priv);
283 if (add && tc_skip_sw(head->flags))
288 tc_cls_offload_cnt_update(block, &head->in_hw_count, &head->flags, add);
293 static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh,
294 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
296 struct tc_matchall_pcnt gpf = {};
297 struct cls_mall_head *head = fh;
304 t->tcm_handle = head->handle;
306 nest = nla_nest_start(skb, TCA_OPTIONS);
308 goto nla_put_failure;
310 if (head->res.classid &&
311 nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
312 goto nla_put_failure;
314 if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags))
315 goto nla_put_failure;
317 for_each_possible_cpu(cpu) {
318 struct tc_matchall_pcnt *pf = per_cpu_ptr(head->pf, cpu);
320 gpf.rhit += pf->rhit;
323 if (nla_put_64bit(skb, TCA_MATCHALL_PCNT,
324 sizeof(struct tc_matchall_pcnt),
325 &gpf, TCA_MATCHALL_PAD))
326 goto nla_put_failure;
328 if (tcf_exts_dump(skb, &head->exts))
329 goto nla_put_failure;
331 nla_nest_end(skb, nest);
333 if (tcf_exts_dump_stats(skb, &head->exts) < 0)
334 goto nla_put_failure;
339 nla_nest_cancel(skb, nest);
343 static void mall_bind_class(void *fh, u32 classid, unsigned long cl)
345 struct cls_mall_head *head = fh;
347 if (head && head->res.classid == classid)
348 head->res.class = cl;
351 static struct tcf_proto_ops cls_mall_ops __read_mostly = {
353 .classify = mall_classify,
355 .destroy = mall_destroy,
357 .change = mall_change,
358 .delete = mall_delete,
360 .reoffload = mall_reoffload,
362 .bind_class = mall_bind_class,
363 .owner = THIS_MODULE,
366 static int __init cls_mall_init(void)
368 return register_tcf_proto_ops(&cls_mall_ops);
371 static void __exit cls_mall_exit(void)
373 unregister_tcf_proto_ops(&cls_mall_ops);
376 module_init(cls_mall_init);
377 module_exit(cls_mall_exit);
379 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
380 MODULE_DESCRIPTION("Match-all classifier");
381 MODULE_LICENSE("GPL v2");