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 | |
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 | |
b505b29f | 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 | |
aeb3fecd | 78 | tc_setup_cb_call(block, TC_SETUP_CLSMATCHALL, &cls_mall, false); |
caa72601 | 79 | tcf_block_offload_dec(block, &head->flags); |
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 | |
93da52b5 | 92 | tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack); |
2447a96f JP |
93 | cls_mall.command = TC_CLSMATCHALL_REPLACE; |
94 | cls_mall.exts = &head->exts; | |
de4784ca | 95 | cls_mall.cookie = cookie; |
b87f7936 | 96 | |
aeb3fecd | 97 | err = tc_setup_cb_call(block, TC_SETUP_CLSMATCHALL, &cls_mall, skip_sw); |
2447a96f | 98 | if (err < 0) { |
b505b29f | 99 | mall_destroy_hw_filter(tp, head, cookie, NULL); |
2447a96f JP |
100 | return err; |
101 | } else if (err > 0) { | |
0efd1b3a | 102 | head->in_hw_count = err; |
caa72601 | 103 | tcf_block_offload_inc(block, &head->flags); |
2447a96f JP |
104 | } |
105 | ||
106 | if (skip_sw && !(head->flags & TCA_CLS_FLAGS_IN_HW)) | |
107 | return -EINVAL; | |
108 | ||
109 | return 0; | |
b87f7936 YG |
110 | } |
111 | ||
12db03b6 VB |
112 | static void mall_destroy(struct tcf_proto *tp, bool rtnl_held, |
113 | struct netlink_ext_ack *extack) | |
bf3994d2 JP |
114 | { |
115 | struct cls_mall_head *head = rtnl_dereference(tp->root); | |
116 | ||
fd62d9f5 | 117 | if (!head) |
763dbf63 | 118 | return; |
bf3994d2 | 119 | |
a51c76b4 HL |
120 | tcf_unbind_filter(tp, &head->res); |
121 | ||
2447a96f | 122 | if (!tc_skip_hw(head->flags)) |
b505b29f | 123 | mall_destroy_hw_filter(tp, head, (unsigned long) head, extack); |
b87f7936 | 124 | |
57767e78 | 125 | if (tcf_exts_get_net(&head->exts)) |
aaa908ff | 126 | tcf_queue_work(&head->rwork, mall_destroy_work); |
57767e78 CW |
127 | else |
128 | __mall_destroy(head); | |
bf3994d2 JP |
129 | } |
130 | ||
8113c095 | 131 | static void *mall_get(struct tcf_proto *tp, u32 handle) |
bf3994d2 | 132 | { |
8113c095 | 133 | return NULL; |
bf3994d2 JP |
134 | } |
135 | ||
136 | static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = { | |
137 | [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC }, | |
138 | [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 }, | |
139 | }; | |
140 | ||
141 | static int mall_set_parms(struct net *net, struct tcf_proto *tp, | |
fd62d9f5 | 142 | struct cls_mall_head *head, |
bf3994d2 | 143 | unsigned long base, struct nlattr **tb, |
50a56190 AA |
144 | struct nlattr *est, bool ovr, |
145 | struct netlink_ext_ack *extack) | |
bf3994d2 | 146 | { |
bf3994d2 JP |
147 | int err; |
148 | ||
ec6743a1 VB |
149 | err = tcf_exts_validate(net, tp, tb, est, &head->exts, ovr, true, |
150 | extack); | |
bf3994d2 | 151 | if (err < 0) |
a74cb369 | 152 | return err; |
bf3994d2 JP |
153 | |
154 | if (tb[TCA_MATCHALL_CLASSID]) { | |
fd62d9f5 YG |
155 | head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]); |
156 | tcf_bind_filter(tp, &head->res, base); | |
bf3994d2 | 157 | } |
bf3994d2 JP |
158 | return 0; |
159 | } | |
160 | ||
161 | static int mall_change(struct net *net, struct sk_buff *in_skb, | |
162 | struct tcf_proto *tp, unsigned long base, | |
163 | u32 handle, struct nlattr **tca, | |
12db03b6 VB |
164 | void **arg, bool ovr, bool rtnl_held, |
165 | struct netlink_ext_ack *extack) | |
bf3994d2 JP |
166 | { |
167 | struct cls_mall_head *head = rtnl_dereference(tp->root); | |
bf3994d2 | 168 | struct nlattr *tb[TCA_MATCHALL_MAX + 1]; |
fd62d9f5 | 169 | struct cls_mall_head *new; |
b87f7936 | 170 | u32 flags = 0; |
bf3994d2 JP |
171 | int err; |
172 | ||
173 | if (!tca[TCA_OPTIONS]) | |
174 | return -EINVAL; | |
175 | ||
fd62d9f5 YG |
176 | if (head) |
177 | return -EEXIST; | |
bf3994d2 | 178 | |
fceb6435 JB |
179 | err = nla_parse_nested(tb, TCA_MATCHALL_MAX, tca[TCA_OPTIONS], |
180 | mall_policy, NULL); | |
bf3994d2 JP |
181 | if (err < 0) |
182 | return err; | |
183 | ||
b87f7936 YG |
184 | if (tb[TCA_MATCHALL_FLAGS]) { |
185 | flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]); | |
186 | if (!tc_flags_valid(flags)) | |
187 | return -EINVAL; | |
188 | } | |
189 | ||
fd62d9f5 YG |
190 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
191 | if (!new) | |
bf3994d2 JP |
192 | return -ENOBUFS; |
193 | ||
14215108 | 194 | err = tcf_exts_init(&new->exts, net, TCA_MATCHALL_ACT, 0); |
ec2507d2 YG |
195 | if (err) |
196 | goto err_exts_init; | |
bf3994d2 JP |
197 | |
198 | if (!handle) | |
199 | handle = 1; | |
fd62d9f5 YG |
200 | new->handle = handle; |
201 | new->flags = flags; | |
f88c19aa CW |
202 | new->pf = alloc_percpu(struct tc_matchall_pcnt); |
203 | if (!new->pf) { | |
204 | err = -ENOMEM; | |
205 | goto err_alloc_percpu; | |
206 | } | |
bf3994d2 | 207 | |
50a56190 AA |
208 | err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr, |
209 | extack); | |
bf3994d2 | 210 | if (err) |
ec2507d2 | 211 | goto err_set_parms; |
bf3994d2 | 212 | |
2447a96f | 213 | if (!tc_skip_hw(new->flags)) { |
02798140 QM |
214 | err = mall_replace_hw_filter(tp, new, (unsigned long)new, |
215 | extack); | |
2447a96f JP |
216 | if (err) |
217 | goto err_replace_hw_filter; | |
b87f7936 | 218 | } |
c7d2b2f5 OG |
219 | |
220 | if (!tc_in_hw(new->flags)) | |
221 | new->flags |= TCA_CLS_FLAGS_NOT_IN_HW; | |
b87f7936 | 222 | |
8113c095 | 223 | *arg = head; |
fd62d9f5 | 224 | rcu_assign_pointer(tp->root, new); |
bf3994d2 JP |
225 | return 0; |
226 | ||
ec2507d2 YG |
227 | err_replace_hw_filter: |
228 | err_set_parms: | |
f88c19aa CW |
229 | free_percpu(new->pf); |
230 | err_alloc_percpu: | |
e2160156 | 231 | tcf_exts_destroy(&new->exts); |
ec2507d2 | 232 | err_exts_init: |
fd62d9f5 | 233 | kfree(new); |
bf3994d2 JP |
234 | return err; |
235 | } | |
236 | ||
571acf21 | 237 | static int mall_delete(struct tcf_proto *tp, void *arg, bool *last, |
12db03b6 | 238 | bool rtnl_held, struct netlink_ext_ack *extack) |
bf3994d2 | 239 | { |
fd62d9f5 | 240 | return -EOPNOTSUPP; |
bf3994d2 JP |
241 | } |
242 | ||
12db03b6 VB |
243 | static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg, |
244 | bool rtnl_held) | |
bf3994d2 JP |
245 | { |
246 | struct cls_mall_head *head = rtnl_dereference(tp->root); | |
bf3994d2 JP |
247 | |
248 | if (arg->count < arg->skip) | |
249 | goto skip; | |
d66022cd VB |
250 | |
251 | if (!head) | |
252 | return; | |
8113c095 | 253 | if (arg->fn(tp, head, arg) < 0) |
bf3994d2 JP |
254 | arg->stop = 1; |
255 | skip: | |
256 | arg->count++; | |
257 | } | |
258 | ||
0efd1b3a JH |
259 | static int mall_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb, |
260 | void *cb_priv, struct netlink_ext_ack *extack) | |
261 | { | |
262 | struct cls_mall_head *head = rtnl_dereference(tp->root); | |
263 | struct tc_cls_matchall_offload cls_mall = {}; | |
264 | struct tcf_block *block = tp->chain->block; | |
265 | int err; | |
266 | ||
267 | if (tc_skip_hw(head->flags)) | |
268 | return 0; | |
269 | ||
270 | tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack); | |
271 | cls_mall.command = add ? | |
272 | TC_CLSMATCHALL_REPLACE : TC_CLSMATCHALL_DESTROY; | |
273 | cls_mall.exts = &head->exts; | |
274 | cls_mall.cookie = (unsigned long)head; | |
275 | ||
276 | err = cb(TC_SETUP_CLSMATCHALL, &cls_mall, cb_priv); | |
277 | if (err) { | |
278 | if (add && tc_skip_sw(head->flags)) | |
279 | return err; | |
280 | return 0; | |
281 | } | |
282 | ||
283 | tc_cls_offload_cnt_update(block, &head->in_hw_count, &head->flags, add); | |
284 | ||
285 | return 0; | |
286 | } | |
287 | ||
8113c095 | 288 | static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh, |
12db03b6 | 289 | struct sk_buff *skb, struct tcmsg *t, bool rtnl_held) |
bf3994d2 | 290 | { |
f88c19aa | 291 | struct tc_matchall_pcnt gpf = {}; |
8113c095 | 292 | struct cls_mall_head *head = fh; |
bf3994d2 | 293 | struct nlattr *nest; |
f88c19aa | 294 | int cpu; |
bf3994d2 | 295 | |
fd62d9f5 | 296 | if (!head) |
bf3994d2 JP |
297 | return skb->len; |
298 | ||
fd62d9f5 | 299 | t->tcm_handle = head->handle; |
bf3994d2 JP |
300 | |
301 | nest = nla_nest_start(skb, TCA_OPTIONS); | |
302 | if (!nest) | |
303 | goto nla_put_failure; | |
304 | ||
fd62d9f5 YG |
305 | if (head->res.classid && |
306 | nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid)) | |
bf3994d2 JP |
307 | goto nla_put_failure; |
308 | ||
7a335ada OG |
309 | if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags)) |
310 | goto nla_put_failure; | |
311 | ||
f88c19aa CW |
312 | for_each_possible_cpu(cpu) { |
313 | struct tc_matchall_pcnt *pf = per_cpu_ptr(head->pf, cpu); | |
314 | ||
315 | gpf.rhit += pf->rhit; | |
316 | } | |
317 | ||
318 | if (nla_put_64bit(skb, TCA_MATCHALL_PCNT, | |
319 | sizeof(struct tc_matchall_pcnt), | |
320 | &gpf, TCA_MATCHALL_PAD)) | |
321 | goto nla_put_failure; | |
322 | ||
fd62d9f5 | 323 | if (tcf_exts_dump(skb, &head->exts)) |
bf3994d2 JP |
324 | goto nla_put_failure; |
325 | ||
326 | nla_nest_end(skb, nest); | |
327 | ||
fd62d9f5 | 328 | if (tcf_exts_dump_stats(skb, &head->exts) < 0) |
bf3994d2 JP |
329 | goto nla_put_failure; |
330 | ||
331 | return skb->len; | |
332 | ||
333 | nla_put_failure: | |
334 | nla_nest_cancel(skb, nest); | |
335 | return -1; | |
336 | } | |
337 | ||
07d79fc7 CW |
338 | static void mall_bind_class(void *fh, u32 classid, unsigned long cl) |
339 | { | |
340 | struct cls_mall_head *head = fh; | |
341 | ||
342 | if (head && head->res.classid == classid) | |
343 | head->res.class = cl; | |
344 | } | |
345 | ||
bf3994d2 JP |
346 | static struct tcf_proto_ops cls_mall_ops __read_mostly = { |
347 | .kind = "matchall", | |
348 | .classify = mall_classify, | |
349 | .init = mall_init, | |
350 | .destroy = mall_destroy, | |
351 | .get = mall_get, | |
352 | .change = mall_change, | |
353 | .delete = mall_delete, | |
354 | .walk = mall_walk, | |
0efd1b3a | 355 | .reoffload = mall_reoffload, |
bf3994d2 | 356 | .dump = mall_dump, |
07d79fc7 | 357 | .bind_class = mall_bind_class, |
bf3994d2 JP |
358 | .owner = THIS_MODULE, |
359 | }; | |
360 | ||
361 | static int __init cls_mall_init(void) | |
362 | { | |
363 | return register_tcf_proto_ops(&cls_mall_ops); | |
364 | } | |
365 | ||
366 | static void __exit cls_mall_exit(void) | |
367 | { | |
368 | unregister_tcf_proto_ops(&cls_mall_ops); | |
369 | } | |
370 | ||
371 | module_init(cls_mall_init); | |
372 | module_exit(cls_mall_exit); | |
373 | ||
374 | MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>"); | |
375 | MODULE_DESCRIPTION("Match-all classifier"); | |
376 | MODULE_LICENSE("GPL v2"); |