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 | |
c2ccf84e IS |
100 | err = tc_setup_offload_action(&cls_mall.rule->action, &head->exts, |
101 | cls_mall.common.extack); | |
f00cbf19 PJV |
102 | if (err) { |
103 | kfree(cls_mall.rule); | |
104 | mall_destroy_hw_filter(tp, head, cookie, NULL); | |
f00cbf19 | 105 | |
4c096ea2 | 106 | return skip_sw ? err : 0; |
f00cbf19 PJV |
107 | } |
108 | ||
40119211 VB |
109 | err = tc_setup_cb_add(block, tp, TC_SETUP_CLSMATCHALL, &cls_mall, |
110 | skip_sw, &head->flags, &head->in_hw_count, true); | |
9c1c0e12 | 111 | tc_cleanup_offload_action(&cls_mall.rule->action); |
f00cbf19 PJV |
112 | kfree(cls_mall.rule); |
113 | ||
40119211 | 114 | if (err) { |
b505b29f | 115 | mall_destroy_hw_filter(tp, head, cookie, NULL); |
2447a96f | 116 | return err; |
2447a96f JP |
117 | } |
118 | ||
119 | if (skip_sw && !(head->flags & TCA_CLS_FLAGS_IN_HW)) | |
120 | return -EINVAL; | |
121 | ||
122 | return 0; | |
b87f7936 YG |
123 | } |
124 | ||
12db03b6 VB |
125 | static void mall_destroy(struct tcf_proto *tp, bool rtnl_held, |
126 | struct netlink_ext_ack *extack) | |
bf3994d2 JP |
127 | { |
128 | struct cls_mall_head *head = rtnl_dereference(tp->root); | |
129 | ||
fd62d9f5 | 130 | if (!head) |
763dbf63 | 131 | return; |
bf3994d2 | 132 | |
a51c76b4 HL |
133 | tcf_unbind_filter(tp, &head->res); |
134 | ||
2447a96f | 135 | if (!tc_skip_hw(head->flags)) |
b505b29f | 136 | mall_destroy_hw_filter(tp, head, (unsigned long) head, extack); |
b87f7936 | 137 | |
57767e78 | 138 | if (tcf_exts_get_net(&head->exts)) |
aaa908ff | 139 | tcf_queue_work(&head->rwork, mall_destroy_work); |
57767e78 CW |
140 | else |
141 | __mall_destroy(head); | |
bf3994d2 JP |
142 | } |
143 | ||
8113c095 | 144 | static void *mall_get(struct tcf_proto *tp, u32 handle) |
bf3994d2 | 145 | { |
0db6f8be ND |
146 | struct cls_mall_head *head = rtnl_dereference(tp->root); |
147 | ||
148 | if (head && head->handle == handle) | |
149 | return head; | |
150 | ||
8113c095 | 151 | return NULL; |
bf3994d2 JP |
152 | } |
153 | ||
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 }, | |
1afa3cc9 | 157 | [TCA_MATCHALL_FLAGS] = { .type = NLA_U32 }, |
bf3994d2 JP |
158 | }; |
159 | ||
160 | static int mall_set_parms(struct net *net, struct tcf_proto *tp, | |
fd62d9f5 | 161 | struct cls_mall_head *head, |
bf3994d2 | 162 | unsigned long base, struct nlattr **tb, |
c86e0209 | 163 | struct nlattr *est, u32 flags, u32 fl_flags, |
50a56190 | 164 | struct netlink_ext_ack *extack) |
bf3994d2 | 165 | { |
bf3994d2 JP |
166 | int err; |
167 | ||
c86e0209 BZ |
168 | err = tcf_exts_validate_ex(net, tp, tb, est, &head->exts, flags, |
169 | fl_flags, extack); | |
bf3994d2 | 170 | if (err < 0) |
a74cb369 | 171 | return err; |
bf3994d2 JP |
172 | |
173 | if (tb[TCA_MATCHALL_CLASSID]) { | |
fd62d9f5 YG |
174 | head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]); |
175 | tcf_bind_filter(tp, &head->res, base); | |
bf3994d2 | 176 | } |
bf3994d2 JP |
177 | return 0; |
178 | } | |
179 | ||
180 | static int mall_change(struct net *net, struct sk_buff *in_skb, | |
181 | struct tcf_proto *tp, unsigned long base, | |
182 | u32 handle, struct nlattr **tca, | |
695176bf | 183 | void **arg, u32 flags, |
12db03b6 | 184 | struct netlink_ext_ack *extack) |
bf3994d2 JP |
185 | { |
186 | struct cls_mall_head *head = rtnl_dereference(tp->root); | |
bf3994d2 | 187 | struct nlattr *tb[TCA_MATCHALL_MAX + 1]; |
fd62d9f5 | 188 | struct cls_mall_head *new; |
695176bf | 189 | u32 userflags = 0; |
bf3994d2 JP |
190 | int err; |
191 | ||
192 | if (!tca[TCA_OPTIONS]) | |
193 | return -EINVAL; | |
194 | ||
fd62d9f5 YG |
195 | if (head) |
196 | return -EEXIST; | |
bf3994d2 | 197 | |
8cb08174 JB |
198 | err = nla_parse_nested_deprecated(tb, TCA_MATCHALL_MAX, |
199 | tca[TCA_OPTIONS], mall_policy, NULL); | |
bf3994d2 JP |
200 | if (err < 0) |
201 | return err; | |
202 | ||
b87f7936 | 203 | if (tb[TCA_MATCHALL_FLAGS]) { |
695176bf CW |
204 | userflags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]); |
205 | if (!tc_flags_valid(userflags)) | |
b87f7936 YG |
206 | return -EINVAL; |
207 | } | |
208 | ||
fd62d9f5 YG |
209 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
210 | if (!new) | |
bf3994d2 JP |
211 | return -ENOBUFS; |
212 | ||
14215108 | 213 | err = tcf_exts_init(&new->exts, net, TCA_MATCHALL_ACT, 0); |
ec2507d2 YG |
214 | if (err) |
215 | goto err_exts_init; | |
bf3994d2 JP |
216 | |
217 | if (!handle) | |
218 | handle = 1; | |
fd62d9f5 | 219 | new->handle = handle; |
695176bf | 220 | new->flags = userflags; |
f88c19aa CW |
221 | new->pf = alloc_percpu(struct tc_matchall_pcnt); |
222 | if (!new->pf) { | |
223 | err = -ENOMEM; | |
224 | goto err_alloc_percpu; | |
225 | } | |
bf3994d2 | 226 | |
c86e0209 BZ |
227 | err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], |
228 | flags, new->flags, extack); | |
bf3994d2 | 229 | if (err) |
ec2507d2 | 230 | goto err_set_parms; |
bf3994d2 | 231 | |
2447a96f | 232 | if (!tc_skip_hw(new->flags)) { |
02798140 QM |
233 | err = mall_replace_hw_filter(tp, new, (unsigned long)new, |
234 | extack); | |
2447a96f JP |
235 | if (err) |
236 | goto err_replace_hw_filter; | |
b87f7936 | 237 | } |
c7d2b2f5 OG |
238 | |
239 | if (!tc_in_hw(new->flags)) | |
240 | new->flags |= TCA_CLS_FLAGS_NOT_IN_HW; | |
b87f7936 | 241 | |
8113c095 | 242 | *arg = head; |
fd62d9f5 | 243 | rcu_assign_pointer(tp->root, new); |
bf3994d2 JP |
244 | return 0; |
245 | ||
ec2507d2 YG |
246 | err_replace_hw_filter: |
247 | err_set_parms: | |
f88c19aa CW |
248 | free_percpu(new->pf); |
249 | err_alloc_percpu: | |
e2160156 | 250 | tcf_exts_destroy(&new->exts); |
ec2507d2 | 251 | err_exts_init: |
fd62d9f5 | 252 | kfree(new); |
bf3994d2 JP |
253 | return err; |
254 | } | |
255 | ||
571acf21 | 256 | static int mall_delete(struct tcf_proto *tp, void *arg, bool *last, |
12db03b6 | 257 | bool rtnl_held, struct netlink_ext_ack *extack) |
bf3994d2 | 258 | { |
f517f271 JP |
259 | struct cls_mall_head *head = rtnl_dereference(tp->root); |
260 | ||
261 | head->deleting = true; | |
262 | *last = true; | |
263 | return 0; | |
bf3994d2 JP |
264 | } |
265 | ||
12db03b6 VB |
266 | static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg, |
267 | bool rtnl_held) | |
bf3994d2 JP |
268 | { |
269 | struct cls_mall_head *head = rtnl_dereference(tp->root); | |
bf3994d2 JP |
270 | |
271 | if (arg->count < arg->skip) | |
272 | goto skip; | |
d66022cd | 273 | |
f517f271 | 274 | if (!head || head->deleting) |
d66022cd | 275 | return; |
8113c095 | 276 | if (arg->fn(tp, head, arg) < 0) |
bf3994d2 JP |
277 | arg->stop = 1; |
278 | skip: | |
279 | arg->count++; | |
280 | } | |
281 | ||
a7323311 | 282 | static int mall_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb, |
0efd1b3a JH |
283 | void *cb_priv, struct netlink_ext_ack *extack) |
284 | { | |
285 | struct cls_mall_head *head = rtnl_dereference(tp->root); | |
286 | struct tc_cls_matchall_offload cls_mall = {}; | |
287 | struct tcf_block *block = tp->chain->block; | |
288 | int err; | |
289 | ||
290 | if (tc_skip_hw(head->flags)) | |
291 | return 0; | |
292 | ||
f00cbf19 PJV |
293 | cls_mall.rule = flow_rule_alloc(tcf_exts_num_actions(&head->exts)); |
294 | if (!cls_mall.rule) | |
295 | return -ENOMEM; | |
296 | ||
d6787147 | 297 | tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack); |
0efd1b3a JH |
298 | cls_mall.command = add ? |
299 | TC_CLSMATCHALL_REPLACE : TC_CLSMATCHALL_DESTROY; | |
0efd1b3a JH |
300 | cls_mall.cookie = (unsigned long)head; |
301 | ||
c2ccf84e IS |
302 | err = tc_setup_offload_action(&cls_mall.rule->action, &head->exts, |
303 | cls_mall.common.extack); | |
f00cbf19 PJV |
304 | if (err) { |
305 | kfree(cls_mall.rule); | |
4c096ea2 IS |
306 | |
307 | return add && tc_skip_sw(head->flags) ? err : 0; | |
f00cbf19 PJV |
308 | } |
309 | ||
40119211 VB |
310 | err = tc_setup_cb_reoffload(block, tp, add, cb, TC_SETUP_CLSMATCHALL, |
311 | &cls_mall, cb_priv, &head->flags, | |
312 | &head->in_hw_count); | |
9c1c0e12 | 313 | tc_cleanup_offload_action(&cls_mall.rule->action); |
f00cbf19 PJV |
314 | kfree(cls_mall.rule); |
315 | ||
2801f30e | 316 | return err; |
0efd1b3a JH |
317 | } |
318 | ||
b7fe4ab8 PJV |
319 | static void mall_stats_hw_filter(struct tcf_proto *tp, |
320 | struct cls_mall_head *head, | |
321 | unsigned long cookie) | |
322 | { | |
323 | struct tc_cls_matchall_offload cls_mall = {}; | |
324 | struct tcf_block *block = tp->chain->block; | |
325 | ||
d6787147 | 326 | tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, NULL); |
b7fe4ab8 PJV |
327 | cls_mall.command = TC_CLSMATCHALL_STATS; |
328 | cls_mall.cookie = cookie; | |
329 | ||
40119211 | 330 | tc_setup_cb_call(block, TC_SETUP_CLSMATCHALL, &cls_mall, false, true); |
b7fe4ab8 | 331 | |
bcd64368 BZ |
332 | tcf_exts_hw_stats_update(&head->exts, cls_mall.stats.bytes, |
333 | cls_mall.stats.pkts, cls_mall.stats.drops, | |
334 | cls_mall.stats.lastused, | |
335 | cls_mall.stats.used_hw_stats, | |
336 | cls_mall.stats.used_hw_stats_valid); | |
b7fe4ab8 PJV |
337 | } |
338 | ||
8113c095 | 339 | static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh, |
12db03b6 | 340 | struct sk_buff *skb, struct tcmsg *t, bool rtnl_held) |
bf3994d2 | 341 | { |
f88c19aa | 342 | struct tc_matchall_pcnt gpf = {}; |
8113c095 | 343 | struct cls_mall_head *head = fh; |
bf3994d2 | 344 | struct nlattr *nest; |
f88c19aa | 345 | int cpu; |
bf3994d2 | 346 | |
fd62d9f5 | 347 | if (!head) |
bf3994d2 JP |
348 | return skb->len; |
349 | ||
b7fe4ab8 PJV |
350 | if (!tc_skip_hw(head->flags)) |
351 | mall_stats_hw_filter(tp, head, (unsigned long)head); | |
352 | ||
fd62d9f5 | 353 | t->tcm_handle = head->handle; |
bf3994d2 | 354 | |
ae0be8de | 355 | nest = nla_nest_start_noflag(skb, TCA_OPTIONS); |
bf3994d2 JP |
356 | if (!nest) |
357 | goto nla_put_failure; | |
358 | ||
fd62d9f5 YG |
359 | if (head->res.classid && |
360 | nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid)) | |
bf3994d2 JP |
361 | goto nla_put_failure; |
362 | ||
7a335ada OG |
363 | if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags)) |
364 | goto nla_put_failure; | |
365 | ||
f88c19aa CW |
366 | for_each_possible_cpu(cpu) { |
367 | struct tc_matchall_pcnt *pf = per_cpu_ptr(head->pf, cpu); | |
368 | ||
369 | gpf.rhit += pf->rhit; | |
370 | } | |
371 | ||
372 | if (nla_put_64bit(skb, TCA_MATCHALL_PCNT, | |
373 | sizeof(struct tc_matchall_pcnt), | |
374 | &gpf, TCA_MATCHALL_PAD)) | |
375 | goto nla_put_failure; | |
376 | ||
fd62d9f5 | 377 | if (tcf_exts_dump(skb, &head->exts)) |
bf3994d2 JP |
378 | goto nla_put_failure; |
379 | ||
380 | nla_nest_end(skb, nest); | |
381 | ||
fd62d9f5 | 382 | if (tcf_exts_dump_stats(skb, &head->exts) < 0) |
bf3994d2 JP |
383 | goto nla_put_failure; |
384 | ||
385 | return skb->len; | |
386 | ||
387 | nla_put_failure: | |
388 | nla_nest_cancel(skb, nest); | |
389 | return -1; | |
390 | } | |
391 | ||
2e24cd75 CW |
392 | static void mall_bind_class(void *fh, u32 classid, unsigned long cl, void *q, |
393 | unsigned long base) | |
07d79fc7 CW |
394 | { |
395 | struct cls_mall_head *head = fh; | |
396 | ||
2e24cd75 CW |
397 | if (head && head->res.classid == classid) { |
398 | if (cl) | |
399 | __tcf_bind_filter(q, &head->res, base); | |
400 | else | |
401 | __tcf_unbind_filter(q, &head->res); | |
402 | } | |
07d79fc7 CW |
403 | } |
404 | ||
bf3994d2 JP |
405 | static struct tcf_proto_ops cls_mall_ops __read_mostly = { |
406 | .kind = "matchall", | |
407 | .classify = mall_classify, | |
408 | .init = mall_init, | |
409 | .destroy = mall_destroy, | |
410 | .get = mall_get, | |
411 | .change = mall_change, | |
412 | .delete = mall_delete, | |
413 | .walk = mall_walk, | |
0efd1b3a | 414 | .reoffload = mall_reoffload, |
bf3994d2 | 415 | .dump = mall_dump, |
07d79fc7 | 416 | .bind_class = mall_bind_class, |
bf3994d2 JP |
417 | .owner = THIS_MODULE, |
418 | }; | |
419 | ||
420 | static int __init cls_mall_init(void) | |
421 | { | |
422 | return register_tcf_proto_ops(&cls_mall_ops); | |
423 | } | |
424 | ||
425 | static void __exit cls_mall_exit(void) | |
426 | { | |
427 | unregister_tcf_proto_ops(&cls_mall_ops); | |
428 | } | |
429 | ||
430 | module_init(cls_mall_init); | |
431 | module_exit(cls_mall_exit); | |
432 | ||
433 | MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>"); | |
434 | MODULE_DESCRIPTION("Match-all classifier"); | |
435 | MODULE_LICENSE("GPL v2"); |