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