net/sched: add block pointer to tc_cls_common_offload structure
[linux-block.git] / net / sched / cls_u32.c
CommitLineData
1da177e4
LT
1/*
2 * net/sched/cls_u32.c Ugly (or Universal) 32bit key Packet Classifier.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 * The filters are packed to hash tables of key nodes
12 * with a set of 32bit key/mask pairs at every node.
13 * Nodes reference next level hash tables etc.
14 *
15 * This scheme is the best universal classifier I managed to
16 * invent; it is not super-fast, but it is not slow (provided you
17 * program it correctly), and general enough. And its relative
18 * speed grows as the number of rules becomes larger.
19 *
20 * It seems that it represents the best middle point between
21 * speed and manageability both by human and by machine.
22 *
23 * It is especially useful for link sharing combined with QoS;
24 * pure RSVP doesn't need such a general approach and can use
25 * much simpler (and faster) schemes, sort of cls_rsvp.c.
26 *
27 * JHS: We should remove the CONFIG_NET_CLS_IND from here
28 * eventually when the meta match extension is made available
29 *
30 * nfmark match added by Catalin(ux aka Dino) BOIE <catab at umbrella.ro>
31 */
32
1da177e4 33#include <linux/module.h>
5a0e3ad6 34#include <linux/slab.h>
1da177e4
LT
35#include <linux/types.h>
36#include <linux/kernel.h>
1da177e4 37#include <linux/string.h>
1da177e4 38#include <linux/errno.h>
1ce87720 39#include <linux/percpu.h>
1da177e4 40#include <linux/rtnetlink.h>
1da177e4 41#include <linux/skbuff.h>
7801db8a 42#include <linux/bitmap.h>
3cd904ec
WC
43#include <linux/netdevice.h>
44#include <linux/hash.h>
0ba48053 45#include <net/netlink.h>
1da177e4
LT
46#include <net/act_api.h>
47#include <net/pkt_cls.h>
e7614370 48#include <linux/idr.h>
1da177e4 49
cc7ec456 50struct tc_u_knode {
1ce87720 51 struct tc_u_knode __rcu *next;
1da177e4 52 u32 handle;
1ce87720 53 struct tc_u_hnode __rcu *ht_up;
1da177e4
LT
54 struct tcf_exts exts;
55#ifdef CONFIG_NET_CLS_IND
2519a602 56 int ifindex;
1da177e4
LT
57#endif
58 u8 fshift;
59 struct tcf_result res;
1ce87720 60 struct tc_u_hnode __rcu *ht_down;
1da177e4 61#ifdef CONFIG_CLS_U32_PERF
459d5f62 62 struct tc_u32_pcnt __percpu *pf;
1da177e4 63#endif
9e8ce79c 64 u32 flags;
530d9951 65 unsigned int in_hw_count;
1da177e4 66#ifdef CONFIG_CLS_U32_MARK
459d5f62
JF
67 u32 val;
68 u32 mask;
69 u32 __percpu *pcpu_success;
1da177e4 70#endif
aaa908ff 71 struct rcu_work rwork;
4e2840ee
JF
72 /* The 'sel' field MUST be the last field in structure to allow for
73 * tc_u32_keys allocated at end of structure.
74 */
75 struct tc_u32_sel sel;
1da177e4
LT
76};
77
cc7ec456 78struct tc_u_hnode {
1ce87720 79 struct tc_u_hnode __rcu *next;
1da177e4
LT
80 u32 handle;
81 u32 prio;
1da177e4 82 int refcnt;
cc7ec456 83 unsigned int divisor;
e7614370 84 struct idr handle_idr;
b44ef845 85 bool is_root;
1ce87720 86 struct rcu_head rcu;
f40fe58d 87 u32 flags;
5778d39d
WC
88 /* The 'ht' field MUST be the last field in structure to allow for
89 * more entries allocated at end of structure.
90 */
91 struct tc_u_knode __rcu *ht[1];
1da177e4
LT
92};
93
cc7ec456 94struct tc_u_common {
1ce87720 95 struct tc_u_hnode __rcu *hlist;
339c21d7 96 void *ptr;
1da177e4 97 int refcnt;
e7614370 98 struct idr handle_idr;
3cd904ec 99 struct hlist_node hnode;
b245d32c 100 long knodes;
1da177e4
LT
101};
102
cc7ec456
ED
103static inline unsigned int u32_hash_fold(__be32 key,
104 const struct tc_u32_sel *sel,
105 u8 fshift)
1da177e4 106{
cc7ec456 107 unsigned int h = ntohl(key & sel->hmask) >> fshift;
1da177e4
LT
108
109 return h;
110}
111
5a7a5555
JHS
112static int u32_classify(struct sk_buff *skb, const struct tcf_proto *tp,
113 struct tcf_result *res)
1da177e4
LT
114{
115 struct {
116 struct tc_u_knode *knode;
fbc2e7d9 117 unsigned int off;
1da177e4
LT
118 } stack[TC_U32_MAXDEPTH];
119
1ce87720 120 struct tc_u_hnode *ht = rcu_dereference_bh(tp->root);
fbc2e7d9 121 unsigned int off = skb_network_offset(skb);
1da177e4
LT
122 struct tc_u_knode *n;
123 int sdepth = 0;
124 int off2 = 0;
125 int sel = 0;
126#ifdef CONFIG_CLS_U32_PERF
127 int j;
128#endif
129 int i, r;
130
131next_ht:
1ce87720 132 n = rcu_dereference_bh(ht->ht[sel]);
1da177e4
LT
133
134next_knode:
135 if (n) {
136 struct tc_u32_key *key = n->sel.keys;
137
138#ifdef CONFIG_CLS_U32_PERF
459d5f62 139 __this_cpu_inc(n->pf->rcnt);
1da177e4
LT
140 j = 0;
141#endif
142
d34e3e18
SS
143 if (tc_skip_sw(n->flags)) {
144 n = rcu_dereference_bh(n->next);
145 goto next_knode;
146 }
147
1da177e4 148#ifdef CONFIG_CLS_U32_MARK
459d5f62 149 if ((skb->mark & n->mask) != n->val) {
1ce87720 150 n = rcu_dereference_bh(n->next);
1da177e4
LT
151 goto next_knode;
152 } else {
459d5f62 153 __this_cpu_inc(*n->pcpu_success);
1da177e4
LT
154 }
155#endif
156
cc7ec456 157 for (i = n->sel.nkeys; i > 0; i--, key++) {
66d50d25 158 int toff = off + key->off + (off2 & key->offmask);
86fce3ba 159 __be32 *data, hdata;
fbc2e7d9 160
4e18b3ed 161 if (skb_headroom(skb) + toff > INT_MAX)
66d50d25 162 goto out;
163
86fce3ba 164 data = skb_header_pointer(skb, toff, 4, &hdata);
fbc2e7d9
CG
165 if (!data)
166 goto out;
167 if ((*data ^ key->val) & key->mask) {
1ce87720 168 n = rcu_dereference_bh(n->next);
1da177e4
LT
169 goto next_knode;
170 }
171#ifdef CONFIG_CLS_U32_PERF
459d5f62 172 __this_cpu_inc(n->pf->kcnts[j]);
1da177e4
LT
173 j++;
174#endif
175 }
1ce87720
JF
176
177 ht = rcu_dereference_bh(n->ht_down);
178 if (!ht) {
1da177e4 179check_terminal:
cc7ec456 180 if (n->sel.flags & TC_U32_TERMINAL) {
1da177e4
LT
181
182 *res = n->res;
183#ifdef CONFIG_NET_CLS_IND
2519a602 184 if (!tcf_match_indev(skb, n->ifindex)) {
1ce87720 185 n = rcu_dereference_bh(n->next);
1da177e4
LT
186 goto next_knode;
187 }
188#endif
189#ifdef CONFIG_CLS_U32_PERF
459d5f62 190 __this_cpu_inc(n->pf->rhit);
1da177e4
LT
191#endif
192 r = tcf_exts_exec(skb, &n->exts, res);
193 if (r < 0) {
1ce87720 194 n = rcu_dereference_bh(n->next);
1da177e4
LT
195 goto next_knode;
196 }
197
198 return r;
199 }
1ce87720 200 n = rcu_dereference_bh(n->next);
1da177e4
LT
201 goto next_knode;
202 }
203
204 /* PUSH */
205 if (sdepth >= TC_U32_MAXDEPTH)
206 goto deadloop;
207 stack[sdepth].knode = n;
fbc2e7d9 208 stack[sdepth].off = off;
1da177e4
LT
209 sdepth++;
210
1ce87720 211 ht = rcu_dereference_bh(n->ht_down);
1da177e4 212 sel = 0;
fbc2e7d9 213 if (ht->divisor) {
86fce3ba 214 __be32 *data, hdata;
fbc2e7d9
CG
215
216 data = skb_header_pointer(skb, off + n->sel.hoff, 4,
86fce3ba 217 &hdata);
fbc2e7d9
CG
218 if (!data)
219 goto out;
220 sel = ht->divisor & u32_hash_fold(*data, &n->sel,
221 n->fshift);
222 }
cc7ec456 223 if (!(n->sel.flags & (TC_U32_VAROFFSET | TC_U32_OFFSET | TC_U32_EAT)))
1da177e4
LT
224 goto next_ht;
225
cc7ec456 226 if (n->sel.flags & (TC_U32_OFFSET | TC_U32_VAROFFSET)) {
1da177e4 227 off2 = n->sel.off + 3;
fbc2e7d9 228 if (n->sel.flags & TC_U32_VAROFFSET) {
86fce3ba 229 __be16 *data, hdata;
fbc2e7d9
CG
230
231 data = skb_header_pointer(skb,
232 off + n->sel.offoff,
86fce3ba 233 2, &hdata);
fbc2e7d9
CG
234 if (!data)
235 goto out;
236 off2 += ntohs(n->sel.offmask & *data) >>
237 n->sel.offshift;
238 }
1da177e4
LT
239 off2 &= ~3;
240 }
cc7ec456 241 if (n->sel.flags & TC_U32_EAT) {
fbc2e7d9 242 off += off2;
1da177e4
LT
243 off2 = 0;
244 }
245
fbc2e7d9 246 if (off < skb->len)
1da177e4
LT
247 goto next_ht;
248 }
249
250 /* POP */
251 if (sdepth--) {
252 n = stack[sdepth].knode;
1ce87720 253 ht = rcu_dereference_bh(n->ht_up);
fbc2e7d9 254 off = stack[sdepth].off;
1da177e4
LT
255 goto check_terminal;
256 }
fbc2e7d9 257out:
1da177e4
LT
258 return -1;
259
260deadloop:
e87cc472 261 net_warn_ratelimited("cls_u32: dead loop\n");
1da177e4
LT
262 return -1;
263}
264
5a7a5555 265static struct tc_u_hnode *u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
1da177e4
LT
266{
267 struct tc_u_hnode *ht;
268
1ce87720
JF
269 for (ht = rtnl_dereference(tp_c->hlist);
270 ht;
271 ht = rtnl_dereference(ht->next))
1da177e4
LT
272 if (ht->handle == handle)
273 break;
274
275 return ht;
276}
277
5a7a5555 278static struct tc_u_knode *u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
1da177e4 279{
cc7ec456 280 unsigned int sel;
1da177e4
LT
281 struct tc_u_knode *n = NULL;
282
283 sel = TC_U32_HASH(handle);
284 if (sel > ht->divisor)
285 goto out;
286
1ce87720
JF
287 for (n = rtnl_dereference(ht->ht[sel]);
288 n;
289 n = rtnl_dereference(n->next))
1da177e4
LT
290 if (n->handle == handle)
291 break;
292out:
293 return n;
294}
295
296
8113c095 297static void *u32_get(struct tcf_proto *tp, u32 handle)
1da177e4
LT
298{
299 struct tc_u_hnode *ht;
300 struct tc_u_common *tp_c = tp->data;
301
302 if (TC_U32_HTID(handle) == TC_U32_ROOT)
1ce87720 303 ht = rtnl_dereference(tp->root);
1da177e4
LT
304 else
305 ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
306
307 if (!ht)
8113c095 308 return NULL;
1da177e4
LT
309
310 if (TC_U32_KEY(handle) == 0)
8113c095 311 return ht;
1da177e4 312
8113c095 313 return u32_lookup_key(ht, handle);
1da177e4
LT
314}
315
ffdc2d9e 316/* Protected by rtnl lock */
e7614370 317static u32 gen_new_htid(struct tc_u_common *tp_c, struct tc_u_hnode *ptr)
1da177e4 318{
ffdc2d9e
MW
319 int id = idr_alloc_cyclic(&tp_c->handle_idr, ptr, 1, 0x7FF, GFP_KERNEL);
320 if (id < 0)
e7614370 321 return 0;
ffdc2d9e 322 return (id | 0x800U) << 20;
1da177e4
LT
323}
324
3cd904ec
WC
325static struct hlist_head *tc_u_common_hash;
326
327#define U32_HASH_SHIFT 10
328#define U32_HASH_SIZE (1 << U32_HASH_SHIFT)
329
339c21d7
JP
330static void *tc_u_common_ptr(const struct tcf_proto *tp)
331{
332 struct tcf_block *block = tp->chain->block;
333
334 /* The block sharing is currently supported only
335 * for classless qdiscs. In that case we use block
336 * for tc_u_common identification. In case the
337 * block is not shared, block->q is a valid pointer
338 * and we can use that. That works for classful qdiscs.
339 */
340 if (tcf_block_shared(block))
341 return block;
342 else
343 return block->q;
344}
345
4895c42f 346static struct hlist_head *tc_u_hash(void *key)
3cd904ec 347{
4895c42f 348 return tc_u_common_hash + hash_ptr(key, U32_HASH_SHIFT);
3cd904ec
WC
349}
350
4895c42f 351static struct tc_u_common *tc_u_common_find(void *key)
3cd904ec
WC
352{
353 struct tc_u_common *tc;
4895c42f
AV
354 hlist_for_each_entry(tc, tc_u_hash(key), hnode) {
355 if (tc->ptr == key)
3cd904ec
WC
356 return tc;
357 }
358 return NULL;
359}
360
1da177e4
LT
361static int u32_init(struct tcf_proto *tp)
362{
363 struct tc_u_hnode *root_ht;
4895c42f
AV
364 void *key = tc_u_common_ptr(tp);
365 struct tc_u_common *tp_c = tc_u_common_find(key);
1da177e4 366
0da974f4 367 root_ht = kzalloc(sizeof(*root_ht), GFP_KERNEL);
1da177e4
LT
368 if (root_ht == NULL)
369 return -ENOBUFS;
370
1da177e4 371 root_ht->refcnt++;
e7614370 372 root_ht->handle = tp_c ? gen_new_htid(tp_c, root_ht) : 0x80000000;
1da177e4 373 root_ht->prio = tp->prio;
b44ef845 374 root_ht->is_root = true;
e7614370 375 idr_init(&root_ht->handle_idr);
1da177e4
LT
376
377 if (tp_c == NULL) {
0da974f4 378 tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL);
1da177e4
LT
379 if (tp_c == NULL) {
380 kfree(root_ht);
381 return -ENOBUFS;
382 }
4895c42f 383 tp_c->ptr = key;
3cd904ec 384 INIT_HLIST_NODE(&tp_c->hnode);
e7614370 385 idr_init(&tp_c->handle_idr);
3cd904ec 386
4895c42f 387 hlist_add_head(&tp_c->hnode, tc_u_hash(key));
1da177e4
LT
388 }
389
390 tp_c->refcnt++;
1ce87720
JF
391 RCU_INIT_POINTER(root_ht->next, tp_c->hlist);
392 rcu_assign_pointer(tp_c->hlist, root_ht);
1da177e4 393
6d4c4077 394 root_ht->refcnt++;
1ce87720 395 rcu_assign_pointer(tp->root, root_ht);
1da177e4
LT
396 tp->data = tp_c;
397 return 0;
398}
399
dc07c573 400static int u32_destroy_key(struct tc_u_knode *n, bool free_pf)
1da177e4 401{
d7cdee5e
PA
402 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
403
18d0264f 404 tcf_exts_destroy(&n->exts);
35c55fc1 405 tcf_exts_put_net(&n->exts);
d7cdee5e
PA
406 if (ht && --ht->refcnt == 0)
407 kfree(ht);
1da177e4 408#ifdef CONFIG_CLS_U32_PERF
de5df632
JF
409 if (free_pf)
410 free_percpu(n->pf);
a1ddcfee
JF
411#endif
412#ifdef CONFIG_CLS_U32_MARK
de5df632
JF
413 if (free_pf)
414 free_percpu(n->pcpu_success);
1da177e4
LT
415#endif
416 kfree(n);
417 return 0;
418}
419
de5df632
JF
420/* u32_delete_key_rcu should be called when free'ing a copied
421 * version of a tc_u_knode obtained from u32_init_knode(). When
422 * copies are obtained from u32_init_knode() the statistics are
423 * shared between the old and new copies to allow readers to
424 * continue to update the statistics during the copy. To support
425 * this the u32_delete_key_rcu variant does not free the percpu
426 * statistics.
427 */
c0d378ef
CW
428static void u32_delete_key_work(struct work_struct *work)
429{
aaa908ff
CW
430 struct tc_u_knode *key = container_of(to_rcu_work(work),
431 struct tc_u_knode,
432 rwork);
c0d378ef 433 rtnl_lock();
dc07c573 434 u32_destroy_key(key, false);
c0d378ef
CW
435 rtnl_unlock();
436}
437
de5df632
JF
438/* u32_delete_key_freepf_rcu is the rcu callback variant
439 * that free's the entire structure including the statistics
440 * percpu variables. Only use this if the key is not a copy
441 * returned by u32_init_knode(). See u32_delete_key_rcu()
442 * for the variant that should be used with keys return from
443 * u32_init_knode()
444 */
c0d378ef
CW
445static void u32_delete_key_freepf_work(struct work_struct *work)
446{
aaa908ff
CW
447 struct tc_u_knode *key = container_of(to_rcu_work(work),
448 struct tc_u_knode,
449 rwork);
c0d378ef 450 rtnl_lock();
dc07c573 451 u32_destroy_key(key, true);
c0d378ef
CW
452 rtnl_unlock();
453}
454
82d567c2 455static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key)
1da177e4 456{
b245d32c 457 struct tc_u_common *tp_c = tp->data;
1ce87720
JF
458 struct tc_u_knode __rcu **kp;
459 struct tc_u_knode *pkp;
a96366bf 460 struct tc_u_hnode *ht = rtnl_dereference(key->ht_up);
1da177e4
LT
461
462 if (ht) {
1ce87720
JF
463 kp = &ht->ht[TC_U32_HASH(key->handle)];
464 for (pkp = rtnl_dereference(*kp); pkp;
465 kp = &pkp->next, pkp = rtnl_dereference(*kp)) {
466 if (pkp == key) {
467 RCU_INIT_POINTER(*kp, key->next);
b245d32c 468 tp_c->knodes--;
1da177e4 469
a0efb80c 470 tcf_unbind_filter(tp, &key->res);
f12c6432 471 idr_remove(&ht->handle_idr, key->handle);
35c55fc1 472 tcf_exts_get_net(&key->exts);
aaa908ff 473 tcf_queue_work(&key->rwork, u32_delete_key_freepf_work);
1da177e4
LT
474 return 0;
475 }
476 }
477 }
547b792c 478 WARN_ON(1);
1da177e4
LT
479 return 0;
480}
481
458e704d
JK
482static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
483 struct netlink_ext_ack *extack)
a1b7c5fd 484{
245dc512 485 struct tcf_block *block = tp->chain->block;
de4784ca 486 struct tc_cls_u32_offload cls_u32 = {};
a1b7c5fd 487
88c44a52
PJV
488 tc_cls_common_offload_init(&cls_u32.common, tp, h->flags, block,
489 extack);
77460411
JP
490 cls_u32.command = TC_CLSU32_DELETE_HNODE;
491 cls_u32.hnode.divisor = h->divisor;
492 cls_u32.hnode.handle = h->handle;
493 cls_u32.hnode.prio = h->prio;
de4784ca 494
aeb3fecd 495 tc_setup_cb_call(block, TC_SETUP_CLSU32, &cls_u32, false);
a1b7c5fd
JF
496}
497
5a7a5555 498static int u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
10a47e0f 499 u32 flags, struct netlink_ext_ack *extack)
a1b7c5fd 500{
245dc512 501 struct tcf_block *block = tp->chain->block;
de4784ca 502 struct tc_cls_u32_offload cls_u32 = {};
245dc512
JP
503 bool skip_sw = tc_skip_sw(flags);
504 bool offloaded = false;
d34e3e18 505 int err;
a1b7c5fd 506
88c44a52 507 tc_cls_common_offload_init(&cls_u32.common, tp, flags, block, extack);
de4784ca
JP
508 cls_u32.command = TC_CLSU32_NEW_HNODE;
509 cls_u32.hnode.divisor = h->divisor;
510 cls_u32.hnode.handle = h->handle;
511 cls_u32.hnode.prio = h->prio;
a1b7c5fd 512
aeb3fecd 513 err = tc_setup_cb_call(block, TC_SETUP_CLSU32, &cls_u32, skip_sw);
245dc512 514 if (err < 0) {
458e704d 515 u32_clear_hw_hnode(tp, h, NULL);
d47a0f38 516 return err;
245dc512
JP
517 } else if (err > 0) {
518 offloaded = true;
519 }
520
521 if (skip_sw && !offloaded)
522 return -EINVAL;
d34e3e18
SS
523
524 return 0;
a1b7c5fd
JF
525}
526
458e704d
JK
527static void u32_remove_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
528 struct netlink_ext_ack *extack)
a1b7c5fd 529{
245dc512 530 struct tcf_block *block = tp->chain->block;
de4784ca 531 struct tc_cls_u32_offload cls_u32 = {};
a1b7c5fd 532
88c44a52
PJV
533 tc_cls_common_offload_init(&cls_u32.common, tp, n->flags, block,
534 extack);
77460411 535 cls_u32.command = TC_CLSU32_DELETE_KNODE;
caa72601 536 cls_u32.knode.handle = n->handle;
a1b7c5fd 537
aeb3fecd 538 tc_setup_cb_call(block, TC_SETUP_CLSU32, &cls_u32, false);
caa72601 539 tcf_block_offload_dec(block, &n->flags);
a1b7c5fd
JF
540}
541
5a7a5555 542static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
10a47e0f 543 u32 flags, struct netlink_ext_ack *extack)
a1b7c5fd 544{
058a6c03 545 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
245dc512 546 struct tcf_block *block = tp->chain->block;
de4784ca 547 struct tc_cls_u32_offload cls_u32 = {};
245dc512 548 bool skip_sw = tc_skip_sw(flags);
d34e3e18 549 int err;
a1b7c5fd 550
88c44a52 551 tc_cls_common_offload_init(&cls_u32.common, tp, flags, block, extack);
de4784ca
JP
552 cls_u32.command = TC_CLSU32_REPLACE_KNODE;
553 cls_u32.knode.handle = n->handle;
554 cls_u32.knode.fshift = n->fshift;
a1b7c5fd 555#ifdef CONFIG_CLS_U32_MARK
de4784ca
JP
556 cls_u32.knode.val = n->val;
557 cls_u32.knode.mask = n->mask;
a1b7c5fd 558#else
de4784ca
JP
559 cls_u32.knode.val = 0;
560 cls_u32.knode.mask = 0;
a1b7c5fd 561#endif
de4784ca 562 cls_u32.knode.sel = &n->sel;
068ceb35 563 cls_u32.knode.res = &n->res;
de4784ca 564 cls_u32.knode.exts = &n->exts;
201c44bd 565 if (n->ht_down)
058a6c03 566 cls_u32.knode.link_handle = ht->handle;
201c44bd 567
aeb3fecd 568 err = tc_setup_cb_call(block, TC_SETUP_CLSU32, &cls_u32, skip_sw);
245dc512 569 if (err < 0) {
458e704d 570 u32_remove_hw_knode(tp, n, NULL);
201c44bd 571 return err;
245dc512 572 } else if (err > 0) {
530d9951 573 n->in_hw_count = err;
caa72601 574 tcf_block_offload_inc(block, &n->flags);
245dc512
JP
575 }
576
0f04d057 577 if (skip_sw && !(n->flags & TCA_CLS_FLAGS_IN_HW))
245dc512 578 return -EINVAL;
d34e3e18
SS
579
580 return 0;
a1b7c5fd
JF
581}
582
458e704d
JK
583static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
584 struct netlink_ext_ack *extack)
1da177e4 585{
b245d32c 586 struct tc_u_common *tp_c = tp->data;
1da177e4 587 struct tc_u_knode *n;
cc7ec456 588 unsigned int h;
1da177e4 589
cc7ec456 590 for (h = 0; h <= ht->divisor; h++) {
1ce87720
JF
591 while ((n = rtnl_dereference(ht->ht[h])) != NULL) {
592 RCU_INIT_POINTER(ht->ht[h],
593 rtnl_dereference(n->next));
b245d32c 594 tp_c->knodes--;
a0efb80c 595 tcf_unbind_filter(tp, &n->res);
458e704d 596 u32_remove_hw_knode(tp, n, extack);
9c160941 597 idr_remove(&ht->handle_idr, n->handle);
35c55fc1 598 if (tcf_exts_get_net(&n->exts))
aaa908ff 599 tcf_queue_work(&n->rwork, u32_delete_key_freepf_work);
35c55fc1 600 else
dc07c573 601 u32_destroy_key(n, true);
1da177e4
LT
602 }
603 }
604}
605
458e704d
JK
606static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
607 struct netlink_ext_ack *extack)
1da177e4
LT
608{
609 struct tc_u_common *tp_c = tp->data;
1ce87720
JF
610 struct tc_u_hnode __rcu **hn;
611 struct tc_u_hnode *phn;
1da177e4 612
6d4c4077 613 WARN_ON(--ht->refcnt);
1da177e4 614
458e704d 615 u32_clear_hnode(tp, ht, extack);
1da177e4 616
1ce87720
JF
617 hn = &tp_c->hlist;
618 for (phn = rtnl_dereference(*hn);
619 phn;
620 hn = &phn->next, phn = rtnl_dereference(*hn)) {
621 if (phn == ht) {
458e704d 622 u32_clear_hw_hnode(tp, ht, extack);
e7614370 623 idr_destroy(&ht->handle_idr);
9c160941 624 idr_remove(&tp_c->handle_idr, ht->handle);
1ce87720
JF
625 RCU_INIT_POINTER(*hn, ht->next);
626 kfree_rcu(ht, rcu);
1da177e4
LT
627 return 0;
628 }
629 }
630
1da177e4
LT
631 return -ENOENT;
632}
633
12db03b6
VB
634static void u32_destroy(struct tcf_proto *tp, bool rtnl_held,
635 struct netlink_ext_ack *extack)
1da177e4
LT
636{
637 struct tc_u_common *tp_c = tp->data;
1ce87720 638 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
1da177e4 639
547b792c 640 WARN_ON(root_ht == NULL);
1da177e4 641
6d4c4077 642 if (root_ht && --root_ht->refcnt == 1)
458e704d 643 u32_destroy_hnode(tp, root_ht, extack);
1da177e4
LT
644
645 if (--tp_c->refcnt == 0) {
646 struct tc_u_hnode *ht;
1da177e4 647
3cd904ec 648 hlist_del(&tp_c->hnode);
1da177e4 649
1ce87720 650 while ((ht = rtnl_dereference(tp_c->hlist)) != NULL) {
d7cdee5e 651 u32_clear_hnode(tp, ht, extack);
1ce87720 652 RCU_INIT_POINTER(tp_c->hlist, ht->next);
d7cdee5e
PA
653
654 /* u32_destroy_key() will later free ht for us, if it's
655 * still referenced by some knode
656 */
657 if (--ht->refcnt == 0)
658 kfree_rcu(ht, rcu);
3ff50b79 659 }
1da177e4 660
e7614370 661 idr_destroy(&tp_c->handle_idr);
1da177e4
LT
662 kfree(tp_c);
663 }
664
665 tp->data = NULL;
666}
667
571acf21 668static int u32_delete(struct tcf_proto *tp, void *arg, bool *last,
12db03b6 669 bool rtnl_held, struct netlink_ext_ack *extack)
1da177e4 670{
8113c095 671 struct tc_u_hnode *ht = arg;
763dbf63
WC
672 struct tc_u_common *tp_c = tp->data;
673 int ret = 0;
1da177e4 674
a1b7c5fd 675 if (TC_U32_KEY(ht->handle)) {
458e704d 676 u32_remove_hw_knode(tp, (struct tc_u_knode *)ht, extack);
763dbf63
WC
677 ret = u32_delete_key(tp, (struct tc_u_knode *)ht);
678 goto out;
a1b7c5fd 679 }
1da177e4 680
b44ef845 681 if (ht->is_root) {
4b981dbc 682 NL_SET_ERR_MSG_MOD(extack, "Not allowed to delete root node");
1da177e4 683 return -EINVAL;
4b981dbc 684 }
1da177e4 685
e56cfad1 686 if (ht->refcnt == 1) {
458e704d 687 u32_destroy_hnode(tp, ht, extack);
e56cfad1 688 } else {
4b981dbc 689 NL_SET_ERR_MSG_MOD(extack, "Can not delete in-use filter");
e56cfad1
JP
690 return -EBUSY;
691 }
1da177e4 692
763dbf63 693out:
a0305986 694 *last = tp_c->refcnt == 1 && tp_c->knodes == 0;
763dbf63 695 return ret;
1da177e4
LT
696}
697
e7614370 698static u32 gen_new_kid(struct tc_u_hnode *ht, u32 htid)
1da177e4 699{
f730cb93 700 u32 index = htid | 0x800;
e7614370 701 u32 max = htid | 0xFFF;
e7614370 702
f730cb93
MW
703 if (idr_alloc_u32(&ht->handle_idr, NULL, &index, max, GFP_KERNEL)) {
704 index = htid + 1;
705 if (idr_alloc_u32(&ht->handle_idr, NULL, &index, max,
706 GFP_KERNEL))
707 index = max;
e7614370 708 }
7801db8a 709
f730cb93 710 return index;
1da177e4
LT
711}
712
6fa8c014
PM
713static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
714 [TCA_U32_CLASSID] = { .type = NLA_U32 },
715 [TCA_U32_HASH] = { .type = NLA_U32 },
716 [TCA_U32_LINK] = { .type = NLA_U32 },
717 [TCA_U32_DIVISOR] = { .type = NLA_U32 },
718 [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) },
719 [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
720 [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) },
9e8ce79c 721 [TCA_U32_FLAGS] = { .type = NLA_U32 },
6fa8c014
PM
722};
723
c1b52739 724static int u32_set_parms(struct net *net, struct tcf_proto *tp,
8a8065f6 725 unsigned long base,
add93b61 726 struct tc_u_knode *n, struct nlattr **tb,
50a56190
AA
727 struct nlattr *est, bool ovr,
728 struct netlink_ext_ack *extack)
1da177e4 729{
b9a24bb7 730 int err;
1da177e4 731
ec6743a1 732 err = tcf_exts_validate(net, tp, tb, est, &n->exts, ovr, true, extack);
1da177e4
LT
733 if (err < 0)
734 return err;
735
add93b61 736 if (tb[TCA_U32_LINK]) {
1587bac4 737 u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
47a1a1d4 738 struct tc_u_hnode *ht_down = NULL, *ht_old;
1da177e4 739
4b981dbc
AA
740 if (TC_U32_KEY(handle)) {
741 NL_SET_ERR_MSG_MOD(extack, "u32 Link handle must be a hash table");
705c7091 742 return -EINVAL;
4b981dbc 743 }
1da177e4
LT
744
745 if (handle) {
8a8065f6 746 ht_down = u32_lookup_ht(tp->data, handle);
1da177e4 747
4b981dbc
AA
748 if (!ht_down) {
749 NL_SET_ERR_MSG_MOD(extack, "Link hash table not found");
705c7091 750 return -EINVAL;
4b981dbc 751 }
27594ec4
AV
752 if (ht_down->is_root) {
753 NL_SET_ERR_MSG_MOD(extack, "Not linking to root node");
754 return -EINVAL;
755 }
1da177e4
LT
756 ht_down->refcnt++;
757 }
758
1ce87720
JF
759 ht_old = rtnl_dereference(n->ht_down);
760 rcu_assign_pointer(n->ht_down, ht_down);
1da177e4 761
47a1a1d4
PM
762 if (ht_old)
763 ht_old->refcnt--;
1da177e4 764 }
add93b61 765 if (tb[TCA_U32_CLASSID]) {
1587bac4 766 n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
1da177e4
LT
767 tcf_bind_filter(tp, &n->res, base);
768 }
769
770#ifdef CONFIG_NET_CLS_IND
add93b61 771 if (tb[TCA_U32_INDEV]) {
2519a602 772 int ret;
1057c55f 773 ret = tcf_change_indev(net, tb[TCA_U32_INDEV], extack);
2519a602 774 if (ret < 0)
705c7091 775 return -EINVAL;
2519a602 776 n->ifindex = ret;
1da177e4
LT
777 }
778#endif
1da177e4 779 return 0;
1da177e4
LT
780}
781
5a7a5555 782static void u32_replace_knode(struct tcf_proto *tp, struct tc_u_common *tp_c,
de5df632
JF
783 struct tc_u_knode *n)
784{
785 struct tc_u_knode __rcu **ins;
786 struct tc_u_knode *pins;
787 struct tc_u_hnode *ht;
788
789 if (TC_U32_HTID(n->handle) == TC_U32_ROOT)
790 ht = rtnl_dereference(tp->root);
791 else
792 ht = u32_lookup_ht(tp_c, TC_U32_HTID(n->handle));
793
794 ins = &ht->ht[TC_U32_HASH(n->handle)];
795
796 /* The node must always exist for it to be replaced if this is not the
797 * case then something went very wrong elsewhere.
798 */
799 for (pins = rtnl_dereference(*ins); ;
800 ins = &pins->next, pins = rtnl_dereference(*ins))
801 if (pins->handle == n->handle)
802 break;
803
234a4624 804 idr_replace(&ht->handle_idr, n, n->handle);
de5df632
JF
805 RCU_INIT_POINTER(n->next, pins->next);
806 rcu_assign_pointer(*ins, n);
807}
808
14215108 809static struct tc_u_knode *u32_init_knode(struct net *net, struct tcf_proto *tp,
de5df632
JF
810 struct tc_u_knode *n)
811{
058a6c03 812 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
de5df632 813 struct tc_u32_sel *s = &n->sel;
058a6c03 814 struct tc_u_knode *new;
de5df632
JF
815
816 new = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key),
817 GFP_KERNEL);
818
819 if (!new)
820 return NULL;
821
822 RCU_INIT_POINTER(new->next, n->next);
823 new->handle = n->handle;
824 RCU_INIT_POINTER(new->ht_up, n->ht_up);
825
826#ifdef CONFIG_NET_CLS_IND
827 new->ifindex = n->ifindex;
828#endif
829 new->fshift = n->fshift;
830 new->res = n->res;
9e8ce79c 831 new->flags = n->flags;
058a6c03 832 RCU_INIT_POINTER(new->ht_down, ht);
de5df632
JF
833
834 /* bump reference count as long as we hold pointer to structure */
058a6c03
PA
835 if (ht)
836 ht->refcnt++;
de5df632
JF
837
838#ifdef CONFIG_CLS_U32_PERF
839 /* Statistics may be incremented by readers during update
840 * so we must keep them in tact. When the node is later destroyed
841 * a special destroy call must be made to not free the pf memory.
842 */
843 new->pf = n->pf;
844#endif
845
846#ifdef CONFIG_CLS_U32_MARK
847 new->val = n->val;
848 new->mask = n->mask;
849 /* Similarly success statistics must be moved as pointers */
850 new->pcpu_success = n->pcpu_success;
851#endif
e512fcf0 852 memcpy(&new->sel, s, struct_size(s, keys, s->nkeys));
de5df632 853
14215108 854 if (tcf_exts_init(&new->exts, net, TCA_U32_ACT, TCA_U32_POLICE)) {
b9a24bb7
WC
855 kfree(new);
856 return NULL;
857 }
de5df632
JF
858
859 return new;
860}
861
c1b52739 862static int u32_change(struct net *net, struct sk_buff *in_skb,
af4c6641 863 struct tcf_proto *tp, unsigned long base, u32 handle,
12db03b6 864 struct nlattr **tca, void **arg, bool ovr, bool rtnl_held,
7306db38 865 struct netlink_ext_ack *extack)
1da177e4
LT
866{
867 struct tc_u_common *tp_c = tp->data;
868 struct tc_u_hnode *ht;
869 struct tc_u_knode *n;
870 struct tc_u32_sel *s;
add93b61
PM
871 struct nlattr *opt = tca[TCA_OPTIONS];
872 struct nlattr *tb[TCA_U32_MAX + 1];
9e8ce79c 873 u32 htid, flags = 0;
98c8f125 874 size_t sel_size;
1da177e4 875 int err;
459d5f62
JF
876#ifdef CONFIG_CLS_U32_PERF
877 size_t size;
878#endif
1da177e4 879
4b981dbc
AA
880 if (!opt) {
881 if (handle) {
882 NL_SET_ERR_MSG_MOD(extack, "Filter handle requires options");
883 return -EINVAL;
884 } else {
885 return 0;
886 }
887 }
1da177e4 888
8cb08174
JB
889 err = nla_parse_nested_deprecated(tb, TCA_U32_MAX, opt, u32_policy,
890 extack);
cee63723
PM
891 if (err < 0)
892 return err;
1da177e4 893
d34e3e18 894 if (tb[TCA_U32_FLAGS]) {
9e8ce79c 895 flags = nla_get_u32(tb[TCA_U32_FLAGS]);
4b981dbc
AA
896 if (!tc_flags_valid(flags)) {
897 NL_SET_ERR_MSG_MOD(extack, "Invalid filter flags");
1a0f7d29 898 return -EINVAL;
4b981dbc 899 }
d34e3e18 900 }
9e8ce79c 901
8113c095 902 n = *arg;
cc7ec456 903 if (n) {
de5df632
JF
904 struct tc_u_knode *new;
905
4b981dbc
AA
906 if (TC_U32_KEY(n->handle) == 0) {
907 NL_SET_ERR_MSG_MOD(extack, "Key node id cannot be zero");
1da177e4 908 return -EINVAL;
4b981dbc 909 }
1da177e4 910
eb53f7af
IV
911 if ((n->flags ^ flags) &
912 ~(TCA_CLS_FLAGS_IN_HW | TCA_CLS_FLAGS_NOT_IN_HW)) {
4b981dbc 913 NL_SET_ERR_MSG_MOD(extack, "Key node flags do not match passed flags");
9e8ce79c 914 return -EINVAL;
4b981dbc 915 }
9e8ce79c 916
14215108 917 new = u32_init_knode(net, tp, n);
de5df632
JF
918 if (!new)
919 return -ENOMEM;
920
8a8065f6 921 err = u32_set_parms(net, tp, base, new, tb,
50a56190 922 tca[TCA_RATE], ovr, extack);
de5df632
JF
923
924 if (err) {
dc07c573 925 u32_destroy_key(new, false);
de5df632
JF
926 return err;
927 }
928
10a47e0f 929 err = u32_replace_hw_knode(tp, new, flags, extack);
d34e3e18 930 if (err) {
dc07c573 931 u32_destroy_key(new, false);
d34e3e18
SS
932 return err;
933 }
934
24d3dc6d
OG
935 if (!tc_in_hw(new->flags))
936 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
937
de5df632 938 u32_replace_knode(tp, tp_c, new);
a0efb80c 939 tcf_unbind_filter(tp, &n->res);
35c55fc1 940 tcf_exts_get_net(&n->exts);
aaa908ff 941 tcf_queue_work(&n->rwork, u32_delete_key_work);
de5df632 942 return 0;
1da177e4
LT
943 }
944
add93b61 945 if (tb[TCA_U32_DIVISOR]) {
cc7ec456 946 unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
1da177e4 947
2f0c982d
AV
948 if (!is_power_of_2(divisor)) {
949 NL_SET_ERR_MSG_MOD(extack, "Divisor is not a power of 2");
950 return -EINVAL;
951 }
952 if (divisor-- > 0x100) {
4b981dbc 953 NL_SET_ERR_MSG_MOD(extack, "Exceeded maximum 256 hash buckets");
1da177e4 954 return -EINVAL;
4b981dbc
AA
955 }
956 if (TC_U32_KEY(handle)) {
957 NL_SET_ERR_MSG_MOD(extack, "Divisor can only be used on a hash table");
1da177e4 958 return -EINVAL;
4b981dbc 959 }
cc7ec456 960 ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL);
1da177e4
LT
961 if (ht == NULL)
962 return -ENOBUFS;
e7614370
CW
963 if (handle == 0) {
964 handle = gen_new_htid(tp->data, ht);
965 if (handle == 0) {
966 kfree(ht);
967 return -ENOMEM;
968 }
969 } else {
f730cb93
MW
970 err = idr_alloc_u32(&tp_c->handle_idr, ht, &handle,
971 handle, GFP_KERNEL);
e7614370
CW
972 if (err) {
973 kfree(ht);
974 return err;
975 }
976 }
e56cfad1 977 ht->refcnt = 1;
1da177e4
LT
978 ht->divisor = divisor;
979 ht->handle = handle;
980 ht->prio = tp->prio;
e7614370 981 idr_init(&ht->handle_idr);
f40fe58d 982 ht->flags = flags;
6eef3801 983
10a47e0f 984 err = u32_replace_hw_hnode(tp, ht, flags, extack);
6eef3801 985 if (err) {
9c160941 986 idr_remove(&tp_c->handle_idr, handle);
6eef3801
JK
987 kfree(ht);
988 return err;
989 }
990
1ce87720
JF
991 RCU_INIT_POINTER(ht->next, tp_c->hlist);
992 rcu_assign_pointer(tp_c->hlist, ht);
8113c095 993 *arg = ht;
a1b7c5fd 994
1da177e4
LT
995 return 0;
996 }
997
add93b61 998 if (tb[TCA_U32_HASH]) {
1587bac4 999 htid = nla_get_u32(tb[TCA_U32_HASH]);
1da177e4 1000 if (TC_U32_HTID(htid) == TC_U32_ROOT) {
1ce87720 1001 ht = rtnl_dereference(tp->root);
1da177e4
LT
1002 htid = ht->handle;
1003 } else {
1004 ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
4b981dbc
AA
1005 if (!ht) {
1006 NL_SET_ERR_MSG_MOD(extack, "Specified hash table not found");
1da177e4 1007 return -EINVAL;
4b981dbc 1008 }
1da177e4
LT
1009 }
1010 } else {
1ce87720 1011 ht = rtnl_dereference(tp->root);
1da177e4
LT
1012 htid = ht->handle;
1013 }
1014
4b981dbc
AA
1015 if (ht->divisor < TC_U32_HASH(htid)) {
1016 NL_SET_ERR_MSG_MOD(extack, "Specified hash table buckets exceed configured value");
1da177e4 1017 return -EINVAL;
4b981dbc 1018 }
1da177e4
LT
1019
1020 if (handle) {
4b981dbc
AA
1021 if (TC_U32_HTID(handle) && TC_U32_HTID(handle ^ htid)) {
1022 NL_SET_ERR_MSG_MOD(extack, "Handle specified hash table address mismatch");
1da177e4 1023 return -EINVAL;
4b981dbc 1024 }
1da177e4 1025 handle = htid | TC_U32_NODE(handle);
f730cb93 1026 err = idr_alloc_u32(&ht->handle_idr, NULL, &handle, handle,
e7614370
CW
1027 GFP_KERNEL);
1028 if (err)
1029 return err;
1da177e4
LT
1030 } else
1031 handle = gen_new_kid(ht, htid);
1032
e7614370 1033 if (tb[TCA_U32_SEL] == NULL) {
4b981dbc 1034 NL_SET_ERR_MSG_MOD(extack, "Selector not specified");
e7614370
CW
1035 err = -EINVAL;
1036 goto erridr;
1037 }
1da177e4 1038
add93b61 1039 s = nla_data(tb[TCA_U32_SEL]);
98c8f125
KC
1040 sel_size = struct_size(s, keys, s->nkeys);
1041 if (nla_len(tb[TCA_U32_SEL]) < sel_size) {
1042 err = -EINVAL;
1043 goto erridr;
1044 }
1da177e4 1045
98c8f125 1046 n = kzalloc(offsetof(typeof(*n), sel) + sel_size, GFP_KERNEL);
e7614370
CW
1047 if (n == NULL) {
1048 err = -ENOBUFS;
1049 goto erridr;
1050 }
1da177e4 1051
1da177e4 1052#ifdef CONFIG_CLS_U32_PERF
459d5f62
JF
1053 size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64);
1054 n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt));
1055 if (!n->pf) {
e7614370
CW
1056 err = -ENOBUFS;
1057 goto errfree;
1da177e4 1058 }
1da177e4
LT
1059#endif
1060
98c8f125 1061 memcpy(&n->sel, s, sel_size);
a96366bf 1062 RCU_INIT_POINTER(n->ht_up, ht);
1da177e4 1063 n->handle = handle;
b2268016 1064 n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
9e8ce79c 1065 n->flags = flags;
1da177e4 1066
14215108 1067 err = tcf_exts_init(&n->exts, net, TCA_U32_ACT, TCA_U32_POLICE);
b9a24bb7
WC
1068 if (err < 0)
1069 goto errout;
1070
1da177e4 1071#ifdef CONFIG_CLS_U32_MARK
459d5f62 1072 n->pcpu_success = alloc_percpu(u32);
a1ddcfee
JF
1073 if (!n->pcpu_success) {
1074 err = -ENOMEM;
1075 goto errout;
1076 }
459d5f62 1077
add93b61 1078 if (tb[TCA_U32_MARK]) {
1da177e4
LT
1079 struct tc_u32_mark *mark;
1080
add93b61 1081 mark = nla_data(tb[TCA_U32_MARK]);
459d5f62
JF
1082 n->val = mark->val;
1083 n->mask = mark->mask;
1da177e4
LT
1084 }
1085#endif
1086
8a8065f6 1087 err = u32_set_parms(net, tp, base, n, tb, tca[TCA_RATE], ovr,
50a56190 1088 extack);
1da177e4 1089 if (err == 0) {
1ce87720
JF
1090 struct tc_u_knode __rcu **ins;
1091 struct tc_u_knode *pins;
1092
10a47e0f 1093 err = u32_replace_hw_knode(tp, n, flags, extack);
d34e3e18
SS
1094 if (err)
1095 goto errhw;
1096
24d3dc6d
OG
1097 if (!tc_in_hw(n->flags))
1098 n->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1099
1ce87720
JF
1100 ins = &ht->ht[TC_U32_HASH(handle)];
1101 for (pins = rtnl_dereference(*ins); pins;
1102 ins = &pins->next, pins = rtnl_dereference(*ins))
1103 if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle))
1da177e4
LT
1104 break;
1105
1ce87720
JF
1106 RCU_INIT_POINTER(n->next, pins);
1107 rcu_assign_pointer(*ins, n);
b245d32c 1108 tp_c->knodes++;
8113c095 1109 *arg = n;
1da177e4
LT
1110 return 0;
1111 }
a1ddcfee 1112
d34e3e18 1113errhw:
a1ddcfee
JF
1114#ifdef CONFIG_CLS_U32_MARK
1115 free_percpu(n->pcpu_success);
1116#endif
1117
b9a24bb7
WC
1118errout:
1119 tcf_exts_destroy(&n->exts);
1da177e4 1120#ifdef CONFIG_CLS_U32_PERF
e7614370 1121errfree:
1ce87720 1122 free_percpu(n->pf);
1da177e4
LT
1123#endif
1124 kfree(n);
e7614370 1125erridr:
9c160941 1126 idr_remove(&ht->handle_idr, handle);
1da177e4
LT
1127 return err;
1128}
1129
12db03b6
VB
1130static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg,
1131 bool rtnl_held)
1da177e4
LT
1132{
1133 struct tc_u_common *tp_c = tp->data;
1134 struct tc_u_hnode *ht;
1135 struct tc_u_knode *n;
cc7ec456 1136 unsigned int h;
1da177e4
LT
1137
1138 if (arg->stop)
1139 return;
1140
1ce87720
JF
1141 for (ht = rtnl_dereference(tp_c->hlist);
1142 ht;
1143 ht = rtnl_dereference(ht->next)) {
1da177e4
LT
1144 if (ht->prio != tp->prio)
1145 continue;
1146 if (arg->count >= arg->skip) {
8113c095 1147 if (arg->fn(tp, ht, arg) < 0) {
1da177e4
LT
1148 arg->stop = 1;
1149 return;
1150 }
1151 }
1152 arg->count++;
1153 for (h = 0; h <= ht->divisor; h++) {
1ce87720
JF
1154 for (n = rtnl_dereference(ht->ht[h]);
1155 n;
1156 n = rtnl_dereference(n->next)) {
1da177e4
LT
1157 if (arg->count < arg->skip) {
1158 arg->count++;
1159 continue;
1160 }
8113c095 1161 if (arg->fn(tp, n, arg) < 0) {
1da177e4
LT
1162 arg->stop = 1;
1163 return;
1164 }
1165 arg->count++;
1166 }
1167 }
1168 }
1169}
1170
530d9951
JH
1171static int u32_reoffload_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
1172 bool add, tc_setup_cb_t *cb, void *cb_priv,
1173 struct netlink_ext_ack *extack)
1174{
88c44a52 1175 struct tcf_block *block = tp->chain->block;
530d9951
JH
1176 struct tc_cls_u32_offload cls_u32 = {};
1177 int err;
1178
88c44a52
PJV
1179 tc_cls_common_offload_init(&cls_u32.common, tp, ht->flags, block,
1180 extack);
530d9951
JH
1181 cls_u32.command = add ? TC_CLSU32_NEW_HNODE : TC_CLSU32_DELETE_HNODE;
1182 cls_u32.hnode.divisor = ht->divisor;
1183 cls_u32.hnode.handle = ht->handle;
1184 cls_u32.hnode.prio = ht->prio;
1185
1186 err = cb(TC_SETUP_CLSU32, &cls_u32, cb_priv);
1187 if (err && add && tc_skip_sw(ht->flags))
1188 return err;
1189
1190 return 0;
1191}
1192
1193static int u32_reoffload_knode(struct tcf_proto *tp, struct tc_u_knode *n,
1194 bool add, tc_setup_cb_t *cb, void *cb_priv,
1195 struct netlink_ext_ack *extack)
1196{
1197 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
1198 struct tcf_block *block = tp->chain->block;
1199 struct tc_cls_u32_offload cls_u32 = {};
1200 int err;
1201
88c44a52
PJV
1202 tc_cls_common_offload_init(&cls_u32.common, tp, n->flags, block,
1203 extack);
530d9951
JH
1204 cls_u32.command = add ?
1205 TC_CLSU32_REPLACE_KNODE : TC_CLSU32_DELETE_KNODE;
1206 cls_u32.knode.handle = n->handle;
1207
1208 if (add) {
1209 cls_u32.knode.fshift = n->fshift;
1210#ifdef CONFIG_CLS_U32_MARK
1211 cls_u32.knode.val = n->val;
1212 cls_u32.knode.mask = n->mask;
1213#else
1214 cls_u32.knode.val = 0;
1215 cls_u32.knode.mask = 0;
1216#endif
1217 cls_u32.knode.sel = &n->sel;
068ceb35 1218 cls_u32.knode.res = &n->res;
530d9951
JH
1219 cls_u32.knode.exts = &n->exts;
1220 if (n->ht_down)
1221 cls_u32.knode.link_handle = ht->handle;
1222 }
1223
1224 err = cb(TC_SETUP_CLSU32, &cls_u32, cb_priv);
1225 if (err) {
1226 if (add && tc_skip_sw(n->flags))
1227 return err;
1228 return 0;
1229 }
1230
1231 tc_cls_offload_cnt_update(block, &n->in_hw_count, &n->flags, add);
1232
1233 return 0;
1234}
1235
1236static int u32_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
1237 void *cb_priv, struct netlink_ext_ack *extack)
1238{
1239 struct tc_u_common *tp_c = tp->data;
1240 struct tc_u_hnode *ht;
1241 struct tc_u_knode *n;
1242 unsigned int h;
1243 int err;
1244
1245 for (ht = rtnl_dereference(tp_c->hlist);
1246 ht;
1247 ht = rtnl_dereference(ht->next)) {
1248 if (ht->prio != tp->prio)
1249 continue;
1250
1251 /* When adding filters to a new dev, try to offload the
1252 * hashtable first. When removing, do the filters before the
1253 * hashtable.
1254 */
1255 if (add && !tc_skip_hw(ht->flags)) {
1256 err = u32_reoffload_hnode(tp, ht, add, cb, cb_priv,
1257 extack);
1258 if (err)
1259 return err;
1260 }
1261
1262 for (h = 0; h <= ht->divisor; h++) {
1263 for (n = rtnl_dereference(ht->ht[h]);
1264 n;
1265 n = rtnl_dereference(n->next)) {
1266 if (tc_skip_hw(n->flags))
1267 continue;
1268
1269 err = u32_reoffload_knode(tp, n, add, cb,
1270 cb_priv, extack);
1271 if (err)
1272 return err;
1273 }
1274 }
1275
1276 if (!add && !tc_skip_hw(ht->flags))
1277 u32_reoffload_hnode(tp, ht, add, cb, cb_priv, extack);
1278 }
1279
1280 return 0;
1281}
1282
07d79fc7
CW
1283static void u32_bind_class(void *fh, u32 classid, unsigned long cl)
1284{
1285 struct tc_u_knode *n = fh;
1286
1287 if (n && n->res.classid == classid)
1288 n->res.class = cl;
1289}
1290
8113c095 1291static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
12db03b6 1292 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
1da177e4 1293{
8113c095 1294 struct tc_u_knode *n = fh;
1ce87720 1295 struct tc_u_hnode *ht_up, *ht_down;
4b3550ef 1296 struct nlattr *nest;
1da177e4
LT
1297
1298 if (n == NULL)
1299 return skb->len;
1300
1301 t->tcm_handle = n->handle;
1302
ae0be8de 1303 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
4b3550ef
PM
1304 if (nest == NULL)
1305 goto nla_put_failure;
1da177e4
LT
1306
1307 if (TC_U32_KEY(n->handle) == 0) {
8113c095 1308 struct tc_u_hnode *ht = fh;
cc7ec456
ED
1309 u32 divisor = ht->divisor + 1;
1310
1b34ec43
DM
1311 if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
1312 goto nla_put_failure;
1da177e4 1313 } else {
459d5f62
JF
1314#ifdef CONFIG_CLS_U32_PERF
1315 struct tc_u32_pcnt *gpf;
459d5f62 1316 int cpu;
80aab73d 1317#endif
459d5f62 1318
1b34ec43
DM
1319 if (nla_put(skb, TCA_U32_SEL,
1320 sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
1321 &n->sel))
1322 goto nla_put_failure;
1ce87720
JF
1323
1324 ht_up = rtnl_dereference(n->ht_up);
1325 if (ht_up) {
1da177e4 1326 u32 htid = n->handle & 0xFFFFF000;
1b34ec43
DM
1327 if (nla_put_u32(skb, TCA_U32_HASH, htid))
1328 goto nla_put_failure;
1da177e4 1329 }
1b34ec43
DM
1330 if (n->res.classid &&
1331 nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid))
1332 goto nla_put_failure;
1ce87720
JF
1333
1334 ht_down = rtnl_dereference(n->ht_down);
1335 if (ht_down &&
1336 nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))
1b34ec43 1337 goto nla_put_failure;
1da177e4 1338
9e8ce79c
JF
1339 if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags))
1340 goto nla_put_failure;
1341
1da177e4 1342#ifdef CONFIG_CLS_U32_MARK
459d5f62
JF
1343 if ((n->val || n->mask)) {
1344 struct tc_u32_mark mark = {.val = n->val,
1345 .mask = n->mask,
1346 .success = 0};
80aab73d 1347 int cpum;
459d5f62 1348
80aab73d
JF
1349 for_each_possible_cpu(cpum) {
1350 __u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum);
459d5f62
JF
1351
1352 mark.success += cnt;
1353 }
1354
1355 if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark))
1356 goto nla_put_failure;
1357 }
1da177e4
LT
1358#endif
1359
5da57f42 1360 if (tcf_exts_dump(skb, &n->exts) < 0)
add93b61 1361 goto nla_put_failure;
1da177e4
LT
1362
1363#ifdef CONFIG_NET_CLS_IND
2519a602
WC
1364 if (n->ifindex) {
1365 struct net_device *dev;
1366 dev = __dev_get_by_index(net, n->ifindex);
1367 if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
1368 goto nla_put_failure;
1369 }
1da177e4
LT
1370#endif
1371#ifdef CONFIG_CLS_U32_PERF
459d5f62
JF
1372 gpf = kzalloc(sizeof(struct tc_u32_pcnt) +
1373 n->sel.nkeys * sizeof(u64),
1374 GFP_KERNEL);
1375 if (!gpf)
1376 goto nla_put_failure;
1377
1378 for_each_possible_cpu(cpu) {
1379 int i;
1380 struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu);
1381
1382 gpf->rcnt += pf->rcnt;
1383 gpf->rhit += pf->rhit;
1384 for (i = 0; i < n->sel.nkeys; i++)
1385 gpf->kcnts[i] += pf->kcnts[i];
1386 }
1387
9854518e
ND
1388 if (nla_put_64bit(skb, TCA_U32_PCNT,
1389 sizeof(struct tc_u32_pcnt) +
1390 n->sel.nkeys * sizeof(u64),
1391 gpf, TCA_U32_PAD)) {
459d5f62 1392 kfree(gpf);
1b34ec43 1393 goto nla_put_failure;
459d5f62
JF
1394 }
1395 kfree(gpf);
1da177e4
LT
1396#endif
1397 }
1398
4b3550ef
PM
1399 nla_nest_end(skb, nest);
1400
1da177e4 1401 if (TC_U32_KEY(n->handle))
5da57f42 1402 if (tcf_exts_dump_stats(skb, &n->exts) < 0)
add93b61 1403 goto nla_put_failure;
1da177e4
LT
1404 return skb->len;
1405
add93b61 1406nla_put_failure:
4b3550ef 1407 nla_nest_cancel(skb, nest);
1da177e4
LT
1408 return -1;
1409}
1410
2eb9d75c 1411static struct tcf_proto_ops cls_u32_ops __read_mostly = {
1da177e4
LT
1412 .kind = "u32",
1413 .classify = u32_classify,
1414 .init = u32_init,
1415 .destroy = u32_destroy,
1416 .get = u32_get,
1da177e4
LT
1417 .change = u32_change,
1418 .delete = u32_delete,
1419 .walk = u32_walk,
530d9951 1420 .reoffload = u32_reoffload,
1da177e4 1421 .dump = u32_dump,
07d79fc7 1422 .bind_class = u32_bind_class,
1da177e4
LT
1423 .owner = THIS_MODULE,
1424};
1425
1426static int __init init_u32(void)
1427{
3cd904ec
WC
1428 int i, ret;
1429
6ff9c364 1430 pr_info("u32 classifier\n");
1da177e4 1431#ifdef CONFIG_CLS_U32_PERF
6ff9c364 1432 pr_info(" Performance counters on\n");
1da177e4 1433#endif
1da177e4 1434#ifdef CONFIG_NET_CLS_IND
6ff9c364 1435 pr_info(" input device check on\n");
1da177e4
LT
1436#endif
1437#ifdef CONFIG_NET_CLS_ACT
6ff9c364 1438 pr_info(" Actions configured\n");
1da177e4 1439#endif
3cd904ec
WC
1440 tc_u_common_hash = kvmalloc_array(U32_HASH_SIZE,
1441 sizeof(struct hlist_head),
1442 GFP_KERNEL);
1443 if (!tc_u_common_hash)
1444 return -ENOMEM;
1445
1446 for (i = 0; i < U32_HASH_SIZE; i++)
1447 INIT_HLIST_HEAD(&tc_u_common_hash[i]);
1448
1449 ret = register_tcf_proto_ops(&cls_u32_ops);
1450 if (ret)
1451 kvfree(tc_u_common_hash);
1452 return ret;
1da177e4
LT
1453}
1454
10297b99 1455static void __exit exit_u32(void)
1da177e4
LT
1456{
1457 unregister_tcf_proto_ops(&cls_u32_ops);
3cd904ec 1458 kvfree(tc_u_common_hash);
1da177e4
LT
1459}
1460
1461module_init(init_u32)
1462module_exit(exit_u32)
1463MODULE_LICENSE("GPL");