Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
bf3994d2 JP |
2 | /* |
3 | * net/sched/cls_matchll.c Match-all classifier | |
4 | * | |
5 | * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com> | |
bf3994d2 JP |
6 | */ |
7 | ||
8 | #include <linux/kernel.h> | |
9 | #include <linux/init.h> | |
10 | #include <linux/module.h> | |
f88c19aa | 11 | #include <linux/percpu.h> |
bf3994d2 JP |
12 | |
13 | #include <net/sch_generic.h> | |
14 | #include <net/pkt_cls.h> | |
15 | ||
fd62d9f5 | 16 | struct cls_mall_head { |
bf3994d2 JP |
17 | struct tcf_exts exts; |
18 | struct tcf_result res; | |
19 | u32 handle; | |
b87f7936 | 20 | u32 flags; |
0efd1b3a | 21 | unsigned int in_hw_count; |
f88c19aa | 22 | struct tc_matchall_pcnt __percpu *pf; |
aaa908ff | 23 | struct rcu_work rwork; |
f517f271 | 24 | bool deleting; |
bf3994d2 JP |
25 | }; |
26 | ||
27 | static int mall_classify(struct sk_buff *skb, const struct tcf_proto *tp, | |
28 | struct tcf_result *res) | |
29 | { | |
30 | struct cls_mall_head *head = rcu_dereference_bh(tp->root); | |
bf3994d2 | 31 | |
25426043 MC |
32 | if (unlikely(!head)) |
33 | return -1; | |
34 | ||
fd62d9f5 | 35 | if (tc_skip_sw(head->flags)) |
b87f7936 YG |
36 | return -1; |
37 | ||
3ff4cbec | 38 | *res = head->res; |
f88c19aa | 39 | __this_cpu_inc(head->pf->rhit); |
fd62d9f5 | 40 | return tcf_exts_exec(skb, &head->exts, res); |
bf3994d2 JP |
41 | } |
42 | ||
43 | static int mall_init(struct tcf_proto *tp) | |
44 | { | |
bf3994d2 JP |
45 | return 0; |
46 | } | |
47 | ||
57767e78 CW |
48 | static void __mall_destroy(struct cls_mall_head *head) |
49 | { | |
50 | tcf_exts_destroy(&head->exts); | |
51 | tcf_exts_put_net(&head->exts); | |
f88c19aa | 52 | free_percpu(head->pf); |
57767e78 CW |
53 | kfree(head); |
54 | } | |
55 | ||
df2735ee CW |
56 | static void mall_destroy_work(struct work_struct *work) |
57 | { | |
aaa908ff CW |
58 | struct cls_mall_head *head = container_of(to_rcu_work(work), |
59 | struct cls_mall_head, | |
60 | rwork); | |
df2735ee | 61 | rtnl_lock(); |
57767e78 | 62 | __mall_destroy(head); |
df2735ee CW |
63 | rtnl_unlock(); |
64 | } | |
65 | ||
2447a96f JP |
66 | static void mall_destroy_hw_filter(struct tcf_proto *tp, |
67 | struct cls_mall_head *head, | |
b505b29f JK |
68 | unsigned long cookie, |
69 | struct netlink_ext_ack *extack) | |
b87f7936 | 70 | { |
de4784ca | 71 | struct tc_cls_matchall_offload cls_mall = {}; |
2447a96f | 72 | struct tcf_block *block = tp->chain->block; |
b87f7936 | 73 | |
d6787147 | 74 | tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack); |
2447a96f | 75 | cls_mall.command = TC_CLSMATCHALL_DESTROY; |
de4784ca | 76 | cls_mall.cookie = cookie; |
b87f7936 | 77 | |
40119211 VB |
78 | tc_setup_cb_destroy(block, tp, TC_SETUP_CLSMATCHALL, &cls_mall, false, |
79 | &head->flags, &head->in_hw_count, true); | |
b87f7936 YG |
80 | } |
81 | ||
2447a96f JP |
82 | static int mall_replace_hw_filter(struct tcf_proto *tp, |
83 | struct cls_mall_head *head, | |
02798140 QM |
84 | unsigned long cookie, |
85 | struct netlink_ext_ack *extack) | |
b87f7936 | 86 | { |
de4784ca | 87 | struct tc_cls_matchall_offload cls_mall = {}; |
2447a96f JP |
88 | struct tcf_block *block = tp->chain->block; |
89 | bool skip_sw = tc_skip_sw(head->flags); | |
90 | int err; | |
b87f7936 | 91 | |
f00cbf19 PJV |
92 | cls_mall.rule = flow_rule_alloc(tcf_exts_num_actions(&head->exts)); |
93 | if (!cls_mall.rule) | |
94 | return -ENOMEM; | |
95 | ||
d6787147 | 96 | tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack); |
2447a96f | 97 | cls_mall.command = TC_CLSMATCHALL_REPLACE; |
de4784ca | 98 | cls_mall.cookie = cookie; |
b87f7936 | 99 | |
9838b20a | 100 | err = tc_setup_flow_action(&cls_mall.rule->action, &head->exts, true); |
f00cbf19 PJV |
101 | if (err) { |
102 | kfree(cls_mall.rule); | |
103 | mall_destroy_hw_filter(tp, head, cookie, NULL); | |
104 | if (skip_sw) | |
105 | NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action"); | |
106 | else | |
107 | err = 0; | |
108 | ||
109 | return err; | |
110 | } | |
111 | ||
40119211 VB |
112 | err = tc_setup_cb_add(block, tp, TC_SETUP_CLSMATCHALL, &cls_mall, |
113 | skip_sw, &head->flags, &head->in_hw_count, true); | |
f2b795ea | 114 | tc_cleanup_flow_action(&cls_mall.rule->action); |
f00cbf19 PJV |
115 | kfree(cls_mall.rule); |
116 | ||
40119211 | 117 | if (err) { |
b505b29f | 118 | mall_destroy_hw_filter(tp, head, cookie, NULL); |
2447a96f | 119 | return err; |
2447a96f JP |
120 | } |
121 | ||
122 | if (skip_sw && !(head->flags & TCA_CLS_FLAGS_IN_HW)) | |
123 | return -EINVAL; | |
124 | ||
125 | return 0; | |
b87f7936 YG |
126 | } |
127 | ||
12db03b6 VB |
128 | static void mall_destroy(struct tcf_proto *tp, bool rtnl_held, |
129 | struct netlink_ext_ack *extack) | |
bf3994d2 JP |
130 | { |
131 | struct cls_mall_head *head = rtnl_dereference(tp->root); | |
132 | ||
fd62d9f5 | 133 | if (!head) |
763dbf63 | 134 | return; |
bf3994d2 | 135 | |
a51c76b4 HL |
136 | tcf_unbind_filter(tp, &head->res); |
137 | ||
2447a96f | 138 | if (!tc_skip_hw(head->flags)) |
b505b29f | 139 | mall_destroy_hw_filter(tp, head, (unsigned long) head, extack); |
b87f7936 | 140 | |
57767e78 | 141 | if (tcf_exts_get_net(&head->exts)) |
aaa908ff | 142 | tcf_queue_work(&head->rwork, mall_destroy_work); |
57767e78 CW |
143 | else |
144 | __mall_destroy(head); | |
bf3994d2 JP |
145 | } |
146 | ||
8113c095 | 147 | static void *mall_get(struct tcf_proto *tp, u32 handle) |
bf3994d2 | 148 | { |
0db6f8be ND |
149 | struct cls_mall_head *head = rtnl_dereference(tp->root); |
150 | ||
151 | if (head && head->handle == handle) | |
152 | return head; | |
153 | ||
8113c095 | 154 | return NULL; |
bf3994d2 JP |
155 | } |
156 | ||
157 | static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = { | |
158 | [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC }, | |
159 | [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 }, | |
1afa3cc9 | 160 | [TCA_MATCHALL_FLAGS] = { .type = NLA_U32 }, |
bf3994d2 JP |
161 | }; |
162 | ||
163 | static int mall_set_parms(struct net *net, struct tcf_proto *tp, | |
fd62d9f5 | 164 | struct cls_mall_head *head, |
bf3994d2 | 165 | unsigned long base, struct nlattr **tb, |
50a56190 AA |
166 | struct nlattr *est, bool ovr, |
167 | struct netlink_ext_ack *extack) | |
bf3994d2 | 168 | { |
bf3994d2 JP |
169 | int err; |
170 | ||
ec6743a1 VB |
171 | err = tcf_exts_validate(net, tp, tb, est, &head->exts, ovr, true, |
172 | extack); | |
bf3994d2 | 173 | if (err < 0) |
a74cb369 | 174 | return err; |
bf3994d2 JP |
175 | |
176 | if (tb[TCA_MATCHALL_CLASSID]) { | |
fd62d9f5 YG |
177 | head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]); |
178 | tcf_bind_filter(tp, &head->res, base); | |
bf3994d2 | 179 | } |
bf3994d2 JP |
180 | return 0; |
181 | } | |
182 | ||
183 | static int mall_change(struct net *net, struct sk_buff *in_skb, | |
184 | struct tcf_proto *tp, unsigned long base, | |
185 | u32 handle, struct nlattr **tca, | |
12db03b6 VB |
186 | void **arg, bool ovr, bool rtnl_held, |
187 | struct netlink_ext_ack *extack) | |
bf3994d2 JP |
188 | { |
189 | struct cls_mall_head *head = rtnl_dereference(tp->root); | |
bf3994d2 | 190 | struct nlattr *tb[TCA_MATCHALL_MAX + 1]; |
fd62d9f5 | 191 | struct cls_mall_head *new; |
b87f7936 | 192 | u32 flags = 0; |
bf3994d2 JP |
193 | int err; |
194 | ||
195 | if (!tca[TCA_OPTIONS]) | |
196 | return -EINVAL; | |
197 | ||
fd62d9f5 YG |
198 | if (head) |
199 | return -EEXIST; | |
bf3994d2 | 200 | |
8cb08174 JB |
201 | err = nla_parse_nested_deprecated(tb, TCA_MATCHALL_MAX, |
202 | tca[TCA_OPTIONS], mall_policy, NULL); | |
bf3994d2 JP |
203 | if (err < 0) |
204 | return err; | |
205 | ||
b87f7936 YG |
206 | if (tb[TCA_MATCHALL_FLAGS]) { |
207 | flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]); | |
208 | if (!tc_flags_valid(flags)) | |
209 | return -EINVAL; | |
210 | } | |
211 | ||
fd62d9f5 YG |
212 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
213 | if (!new) | |
bf3994d2 JP |
214 | return -ENOBUFS; |
215 | ||
14215108 | 216 | err = tcf_exts_init(&new->exts, net, TCA_MATCHALL_ACT, 0); |
ec2507d2 YG |
217 | if (err) |
218 | goto err_exts_init; | |
bf3994d2 JP |
219 | |
220 | if (!handle) | |
221 | handle = 1; | |
fd62d9f5 YG |
222 | new->handle = handle; |
223 | new->flags = flags; | |
f88c19aa CW |
224 | new->pf = alloc_percpu(struct tc_matchall_pcnt); |
225 | if (!new->pf) { | |
226 | err = -ENOMEM; | |
227 | goto err_alloc_percpu; | |
228 | } | |
bf3994d2 | 229 | |
50a56190 AA |
230 | err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr, |
231 | extack); | |
bf3994d2 | 232 | if (err) |
ec2507d2 | 233 | goto err_set_parms; |
bf3994d2 | 234 | |
2447a96f | 235 | if (!tc_skip_hw(new->flags)) { |
02798140 QM |
236 | err = mall_replace_hw_filter(tp, new, (unsigned long)new, |
237 | extack); | |
2447a96f JP |
238 | if (err) |
239 | goto err_replace_hw_filter; | |
b87f7936 | 240 | } |
c7d2b2f5 OG |
241 | |
242 | if (!tc_in_hw(new->flags)) | |
243 | new->flags |= TCA_CLS_FLAGS_NOT_IN_HW; | |
b87f7936 | 244 | |
8113c095 | 245 | *arg = head; |
fd62d9f5 | 246 | rcu_assign_pointer(tp->root, new); |
bf3994d2 JP |
247 | return 0; |
248 | ||
ec2507d2 YG |
249 | err_replace_hw_filter: |
250 | err_set_parms: | |
f88c19aa CW |
251 | free_percpu(new->pf); |
252 | err_alloc_percpu: | |
e2160156 | 253 | tcf_exts_destroy(&new->exts); |
ec2507d2 | 254 | err_exts_init: |
fd62d9f5 | 255 | kfree(new); |
bf3994d2 JP |
256 | return err; |
257 | } | |
258 | ||
571acf21 | 259 | static int mall_delete(struct tcf_proto *tp, void *arg, bool *last, |
12db03b6 | 260 | bool rtnl_held, struct netlink_ext_ack *extack) |
bf3994d2 | 261 | { |
f517f271 JP |
262 | struct cls_mall_head *head = rtnl_dereference(tp->root); |
263 | ||
264 | head->deleting = true; | |
265 | *last = true; | |
266 | return 0; | |
bf3994d2 JP |
267 | } |
268 | ||
12db03b6 VB |
269 | static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg, |
270 | bool rtnl_held) | |
bf3994d2 JP |
271 | { |
272 | struct cls_mall_head *head = rtnl_dereference(tp->root); | |
bf3994d2 JP |
273 | |
274 | if (arg->count < arg->skip) | |
275 | goto skip; | |
d66022cd | 276 | |
f517f271 | 277 | if (!head || head->deleting) |
d66022cd | 278 | return; |
8113c095 | 279 | if (arg->fn(tp, head, arg) < 0) |
bf3994d2 JP |
280 | arg->stop = 1; |
281 | skip: | |
282 | arg->count++; | |
283 | } | |
284 | ||
a7323311 | 285 | static int mall_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb, |
0efd1b3a JH |
286 | void *cb_priv, struct netlink_ext_ack *extack) |
287 | { | |
288 | struct cls_mall_head *head = rtnl_dereference(tp->root); | |
289 | struct tc_cls_matchall_offload cls_mall = {}; | |
290 | struct tcf_block *block = tp->chain->block; | |
291 | int err; | |
292 | ||
293 | if (tc_skip_hw(head->flags)) | |
294 | return 0; | |
295 | ||
f00cbf19 PJV |
296 | cls_mall.rule = flow_rule_alloc(tcf_exts_num_actions(&head->exts)); |
297 | if (!cls_mall.rule) | |
298 | return -ENOMEM; | |
299 | ||
d6787147 | 300 | tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack); |
0efd1b3a JH |
301 | cls_mall.command = add ? |
302 | TC_CLSMATCHALL_REPLACE : TC_CLSMATCHALL_DESTROY; | |
0efd1b3a JH |
303 | cls_mall.cookie = (unsigned long)head; |
304 | ||
9838b20a | 305 | err = tc_setup_flow_action(&cls_mall.rule->action, &head->exts, true); |
f00cbf19 PJV |
306 | if (err) { |
307 | kfree(cls_mall.rule); | |
308 | if (add && tc_skip_sw(head->flags)) { | |
309 | NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action"); | |
310 | return err; | |
311 | } | |
5f058368 | 312 | return 0; |
f00cbf19 PJV |
313 | } |
314 | ||
40119211 VB |
315 | err = tc_setup_cb_reoffload(block, tp, add, cb, TC_SETUP_CLSMATCHALL, |
316 | &cls_mall, cb_priv, &head->flags, | |
317 | &head->in_hw_count); | |
f2b795ea | 318 | tc_cleanup_flow_action(&cls_mall.rule->action); |
f00cbf19 PJV |
319 | kfree(cls_mall.rule); |
320 | ||
40119211 VB |
321 | if (err) |
322 | return err; | |
0efd1b3a JH |
323 | |
324 | return 0; | |
325 | } | |
326 | ||
b7fe4ab8 PJV |
327 | static void mall_stats_hw_filter(struct tcf_proto *tp, |
328 | struct cls_mall_head *head, | |
329 | unsigned long cookie) | |
330 | { | |
331 | struct tc_cls_matchall_offload cls_mall = {}; | |
332 | struct tcf_block *block = tp->chain->block; | |
333 | ||
d6787147 | 334 | tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, NULL); |
b7fe4ab8 PJV |
335 | cls_mall.command = TC_CLSMATCHALL_STATS; |
336 | cls_mall.cookie = cookie; | |
337 | ||
40119211 | 338 | tc_setup_cb_call(block, TC_SETUP_CLSMATCHALL, &cls_mall, false, true); |
b7fe4ab8 PJV |
339 | |
340 | tcf_exts_stats_update(&head->exts, cls_mall.stats.bytes, | |
341 | cls_mall.stats.pkts, cls_mall.stats.lastused); | |
342 | } | |
343 | ||
8113c095 | 344 | static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh, |
12db03b6 | 345 | struct sk_buff *skb, struct tcmsg *t, bool rtnl_held) |
bf3994d2 | 346 | { |
f88c19aa | 347 | struct tc_matchall_pcnt gpf = {}; |
8113c095 | 348 | struct cls_mall_head *head = fh; |
bf3994d2 | 349 | struct nlattr *nest; |
f88c19aa | 350 | int cpu; |
bf3994d2 | 351 | |
fd62d9f5 | 352 | if (!head) |
bf3994d2 JP |
353 | return skb->len; |
354 | ||
b7fe4ab8 PJV |
355 | if (!tc_skip_hw(head->flags)) |
356 | mall_stats_hw_filter(tp, head, (unsigned long)head); | |
357 | ||
fd62d9f5 | 358 | t->tcm_handle = head->handle; |
bf3994d2 | 359 | |
ae0be8de | 360 | nest = nla_nest_start_noflag(skb, TCA_OPTIONS); |
bf3994d2 JP |
361 | if (!nest) |
362 | goto nla_put_failure; | |
363 | ||
fd62d9f5 YG |
364 | if (head->res.classid && |
365 | nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid)) | |
bf3994d2 JP |
366 | goto nla_put_failure; |
367 | ||
7a335ada OG |
368 | if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags)) |
369 | goto nla_put_failure; | |
370 | ||
f88c19aa CW |
371 | for_each_possible_cpu(cpu) { |
372 | struct tc_matchall_pcnt *pf = per_cpu_ptr(head->pf, cpu); | |
373 | ||
374 | gpf.rhit += pf->rhit; | |
375 | } | |
376 | ||
377 | if (nla_put_64bit(skb, TCA_MATCHALL_PCNT, | |
378 | sizeof(struct tc_matchall_pcnt), | |
379 | &gpf, TCA_MATCHALL_PAD)) | |
380 | goto nla_put_failure; | |
381 | ||
fd62d9f5 | 382 | if (tcf_exts_dump(skb, &head->exts)) |
bf3994d2 JP |
383 | goto nla_put_failure; |
384 | ||
385 | nla_nest_end(skb, nest); | |
386 | ||
fd62d9f5 | 387 | if (tcf_exts_dump_stats(skb, &head->exts) < 0) |
bf3994d2 JP |
388 | goto nla_put_failure; |
389 | ||
390 | return skb->len; | |
391 | ||
392 | nla_put_failure: | |
393 | nla_nest_cancel(skb, nest); | |
394 | return -1; | |
395 | } | |
396 | ||
2e24cd75 CW |
397 | static void mall_bind_class(void *fh, u32 classid, unsigned long cl, void *q, |
398 | unsigned long base) | |
07d79fc7 CW |
399 | { |
400 | struct cls_mall_head *head = fh; | |
401 | ||
2e24cd75 CW |
402 | if (head && head->res.classid == classid) { |
403 | if (cl) | |
404 | __tcf_bind_filter(q, &head->res, base); | |
405 | else | |
406 | __tcf_unbind_filter(q, &head->res); | |
407 | } | |
07d79fc7 CW |
408 | } |
409 | ||
bf3994d2 JP |
410 | static struct tcf_proto_ops cls_mall_ops __read_mostly = { |
411 | .kind = "matchall", | |
412 | .classify = mall_classify, | |
413 | .init = mall_init, | |
414 | .destroy = mall_destroy, | |
415 | .get = mall_get, | |
416 | .change = mall_change, | |
417 | .delete = mall_delete, | |
418 | .walk = mall_walk, | |
0efd1b3a | 419 | .reoffload = mall_reoffload, |
bf3994d2 | 420 | .dump = mall_dump, |
07d79fc7 | 421 | .bind_class = mall_bind_class, |
bf3994d2 JP |
422 | .owner = THIS_MODULE, |
423 | }; | |
424 | ||
425 | static int __init cls_mall_init(void) | |
426 | { | |
427 | return register_tcf_proto_ops(&cls_mall_ops); | |
428 | } | |
429 | ||
430 | static void __exit cls_mall_exit(void) | |
431 | { | |
432 | unregister_tcf_proto_ops(&cls_mall_ops); | |
433 | } | |
434 | ||
435 | module_init(cls_mall_init); | |
436 | module_exit(cls_mall_exit); | |
437 | ||
438 | MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>"); | |
439 | MODULE_DESCRIPTION("Match-all classifier"); | |
440 | MODULE_LICENSE("GPL v2"); |