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