]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/sched/cls_u32.c
net_sched: switch to rcu_work
[mirror_ubuntu-bionic-kernel.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;
1da177e4 65#ifdef CONFIG_CLS_U32_MARK
459d5f62
JF
66 u32 val;
67 u32 mask;
68 u32 __percpu *pcpu_success;
1da177e4 69#endif
1ce87720 70 struct tcf_proto *tp;
7afe8e4f 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;
82 struct tc_u_common *tp_c;
83 int refcnt;
cc7ec456 84 unsigned int divisor;
e7614370 85 struct idr handle_idr;
1ce87720 86 struct rcu_head rcu;
5778d39d
WC
87 /* The 'ht' field MUST be the last field in structure to allow for
88 * more entries allocated at end of structure.
89 */
90 struct tc_u_knode __rcu *ht[1];
1da177e4
LT
91};
92
cc7ec456 93struct tc_u_common {
1ce87720 94 struct tc_u_hnode __rcu *hlist;
7fa9d974 95 struct tcf_block *block;
1da177e4 96 int refcnt;
e7614370 97 struct idr handle_idr;
3cd904ec 98 struct hlist_node hnode;
1ce87720 99 struct rcu_head rcu;
1da177e4
LT
100};
101
cc7ec456
ED
102static inline unsigned int u32_hash_fold(__be32 key,
103 const struct tc_u32_sel *sel,
104 u8 fshift)
1da177e4 105{
cc7ec456 106 unsigned int h = ntohl(key & sel->hmask) >> fshift;
1da177e4
LT
107
108 return h;
109}
110
5a7a5555
JHS
111static int u32_classify(struct sk_buff *skb, const struct tcf_proto *tp,
112 struct tcf_result *res)
1da177e4
LT
113{
114 struct {
115 struct tc_u_knode *knode;
fbc2e7d9 116 unsigned int off;
1da177e4
LT
117 } stack[TC_U32_MAXDEPTH];
118
1ce87720 119 struct tc_u_hnode *ht = rcu_dereference_bh(tp->root);
fbc2e7d9 120 unsigned int off = skb_network_offset(skb);
1da177e4
LT
121 struct tc_u_knode *n;
122 int sdepth = 0;
123 int off2 = 0;
124 int sel = 0;
125#ifdef CONFIG_CLS_U32_PERF
126 int j;
127#endif
128 int i, r;
129
130next_ht:
1ce87720 131 n = rcu_dereference_bh(ht->ht[sel]);
1da177e4
LT
132
133next_knode:
134 if (n) {
135 struct tc_u32_key *key = n->sel.keys;
136
137#ifdef CONFIG_CLS_U32_PERF
459d5f62 138 __this_cpu_inc(n->pf->rcnt);
1da177e4
LT
139 j = 0;
140#endif
141
d34e3e18
SS
142 if (tc_skip_sw(n->flags)) {
143 n = rcu_dereference_bh(n->next);
144 goto next_knode;
145 }
146
1da177e4 147#ifdef CONFIG_CLS_U32_MARK
459d5f62 148 if ((skb->mark & n->mask) != n->val) {
1ce87720 149 n = rcu_dereference_bh(n->next);
1da177e4
LT
150 goto next_knode;
151 } else {
459d5f62 152 __this_cpu_inc(*n->pcpu_success);
1da177e4
LT
153 }
154#endif
155
cc7ec456 156 for (i = n->sel.nkeys; i > 0; i--, key++) {
66d50d25 157 int toff = off + key->off + (off2 & key->offmask);
86fce3ba 158 __be32 *data, hdata;
fbc2e7d9 159
4e18b3ed 160 if (skb_headroom(skb) + toff > INT_MAX)
66d50d25 161 goto out;
162
86fce3ba 163 data = skb_header_pointer(skb, toff, 4, &hdata);
fbc2e7d9
CG
164 if (!data)
165 goto out;
166 if ((*data ^ key->val) & key->mask) {
1ce87720 167 n = rcu_dereference_bh(n->next);
1da177e4
LT
168 goto next_knode;
169 }
170#ifdef CONFIG_CLS_U32_PERF
459d5f62 171 __this_cpu_inc(n->pf->kcnts[j]);
1da177e4
LT
172 j++;
173#endif
174 }
1ce87720
JF
175
176 ht = rcu_dereference_bh(n->ht_down);
177 if (!ht) {
1da177e4 178check_terminal:
cc7ec456 179 if (n->sel.flags & TC_U32_TERMINAL) {
1da177e4
LT
180
181 *res = n->res;
182#ifdef CONFIG_NET_CLS_IND
2519a602 183 if (!tcf_match_indev(skb, n->ifindex)) {
1ce87720 184 n = rcu_dereference_bh(n->next);
1da177e4
LT
185 goto next_knode;
186 }
187#endif
188#ifdef CONFIG_CLS_U32_PERF
459d5f62 189 __this_cpu_inc(n->pf->rhit);
1da177e4
LT
190#endif
191 r = tcf_exts_exec(skb, &n->exts, res);
192 if (r < 0) {
1ce87720 193 n = rcu_dereference_bh(n->next);
1da177e4
LT
194 goto next_knode;
195 }
196
197 return r;
198 }
1ce87720 199 n = rcu_dereference_bh(n->next);
1da177e4
LT
200 goto next_knode;
201 }
202
203 /* PUSH */
204 if (sdepth >= TC_U32_MAXDEPTH)
205 goto deadloop;
206 stack[sdepth].knode = n;
fbc2e7d9 207 stack[sdepth].off = off;
1da177e4
LT
208 sdepth++;
209
1ce87720 210 ht = rcu_dereference_bh(n->ht_down);
1da177e4 211 sel = 0;
fbc2e7d9 212 if (ht->divisor) {
86fce3ba 213 __be32 *data, hdata;
fbc2e7d9
CG
214
215 data = skb_header_pointer(skb, off + n->sel.hoff, 4,
86fce3ba 216 &hdata);
fbc2e7d9
CG
217 if (!data)
218 goto out;
219 sel = ht->divisor & u32_hash_fold(*data, &n->sel,
220 n->fshift);
221 }
cc7ec456 222 if (!(n->sel.flags & (TC_U32_VAROFFSET | TC_U32_OFFSET | TC_U32_EAT)))
1da177e4
LT
223 goto next_ht;
224
cc7ec456 225 if (n->sel.flags & (TC_U32_OFFSET | TC_U32_VAROFFSET)) {
1da177e4 226 off2 = n->sel.off + 3;
fbc2e7d9 227 if (n->sel.flags & TC_U32_VAROFFSET) {
86fce3ba 228 __be16 *data, hdata;
fbc2e7d9
CG
229
230 data = skb_header_pointer(skb,
231 off + n->sel.offoff,
86fce3ba 232 2, &hdata);
fbc2e7d9
CG
233 if (!data)
234 goto out;
235 off2 += ntohs(n->sel.offmask & *data) >>
236 n->sel.offshift;
237 }
1da177e4
LT
238 off2 &= ~3;
239 }
cc7ec456 240 if (n->sel.flags & TC_U32_EAT) {
fbc2e7d9 241 off += off2;
1da177e4
LT
242 off2 = 0;
243 }
244
fbc2e7d9 245 if (off < skb->len)
1da177e4
LT
246 goto next_ht;
247 }
248
249 /* POP */
250 if (sdepth--) {
251 n = stack[sdepth].knode;
1ce87720 252 ht = rcu_dereference_bh(n->ht_up);
fbc2e7d9 253 off = stack[sdepth].off;
1da177e4
LT
254 goto check_terminal;
255 }
fbc2e7d9 256out:
1da177e4
LT
257 return -1;
258
259deadloop:
e87cc472 260 net_warn_ratelimited("cls_u32: dead loop\n");
1da177e4
LT
261 return -1;
262}
263
5a7a5555 264static struct tc_u_hnode *u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
1da177e4
LT
265{
266 struct tc_u_hnode *ht;
267
1ce87720
JF
268 for (ht = rtnl_dereference(tp_c->hlist);
269 ht;
270 ht = rtnl_dereference(ht->next))
1da177e4
LT
271 if (ht->handle == handle)
272 break;
273
274 return ht;
275}
276
5a7a5555 277static struct tc_u_knode *u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
1da177e4 278{
cc7ec456 279 unsigned int sel;
1da177e4
LT
280 struct tc_u_knode *n = NULL;
281
282 sel = TC_U32_HASH(handle);
283 if (sel > ht->divisor)
284 goto out;
285
1ce87720
JF
286 for (n = rtnl_dereference(ht->ht[sel]);
287 n;
288 n = rtnl_dereference(n->next))
1da177e4
LT
289 if (n->handle == handle)
290 break;
291out:
292 return n;
293}
294
295
8113c095 296static void *u32_get(struct tcf_proto *tp, u32 handle)
1da177e4
LT
297{
298 struct tc_u_hnode *ht;
299 struct tc_u_common *tp_c = tp->data;
300
301 if (TC_U32_HTID(handle) == TC_U32_ROOT)
1ce87720 302 ht = rtnl_dereference(tp->root);
1da177e4
LT
303 else
304 ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
305
306 if (!ht)
8113c095 307 return NULL;
1da177e4
LT
308
309 if (TC_U32_KEY(handle) == 0)
8113c095 310 return ht;
1da177e4 311
8113c095 312 return u32_lookup_key(ht, handle);
1da177e4
LT
313}
314
e7614370 315static u32 gen_new_htid(struct tc_u_common *tp_c, struct tc_u_hnode *ptr)
1da177e4 316{
e7614370
CW
317 unsigned long idr_index;
318 int err;
1da177e4 319
e7614370 320 /* This is only used inside rtnl lock it is safe to increment
1ce87720
JF
321 * without read _copy_ update semantics
322 */
e7614370
CW
323 err = idr_alloc_ext(&tp_c->handle_idr, ptr, &idr_index,
324 1, 0x7FF, GFP_KERNEL);
325 if (err)
326 return 0;
327 return (u32)(idr_index | 0x800) << 20;
1da177e4
LT
328}
329
3cd904ec
WC
330static struct hlist_head *tc_u_common_hash;
331
332#define U32_HASH_SHIFT 10
333#define U32_HASH_SIZE (1 << U32_HASH_SHIFT)
334
335static unsigned int tc_u_hash(const struct tcf_proto *tp)
336{
d18b4b35 337 return hash_ptr(tp->chain->block, U32_HASH_SHIFT);
3cd904ec
WC
338}
339
340static struct tc_u_common *tc_u_common_find(const struct tcf_proto *tp)
341{
342 struct tc_u_common *tc;
343 unsigned int h;
344
345 h = tc_u_hash(tp);
346 hlist_for_each_entry(tc, &tc_u_common_hash[h], hnode) {
7fa9d974 347 if (tc->block == tp->chain->block)
3cd904ec
WC
348 return tc;
349 }
350 return NULL;
351}
352
1da177e4
LT
353static int u32_init(struct tcf_proto *tp)
354{
355 struct tc_u_hnode *root_ht;
356 struct tc_u_common *tp_c;
3cd904ec 357 unsigned int h;
1da177e4 358
3cd904ec 359 tp_c = tc_u_common_find(tp);
1da177e4 360
0da974f4 361 root_ht = kzalloc(sizeof(*root_ht), GFP_KERNEL);
1da177e4
LT
362 if (root_ht == NULL)
363 return -ENOBUFS;
364
1da177e4 365 root_ht->refcnt++;
e7614370 366 root_ht->handle = tp_c ? gen_new_htid(tp_c, root_ht) : 0x80000000;
1da177e4 367 root_ht->prio = tp->prio;
e7614370 368 idr_init(&root_ht->handle_idr);
1da177e4
LT
369
370 if (tp_c == NULL) {
0da974f4 371 tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL);
1da177e4
LT
372 if (tp_c == NULL) {
373 kfree(root_ht);
374 return -ENOBUFS;
375 }
7fa9d974 376 tp_c->block = tp->chain->block;
3cd904ec 377 INIT_HLIST_NODE(&tp_c->hnode);
e7614370 378 idr_init(&tp_c->handle_idr);
3cd904ec
WC
379
380 h = tc_u_hash(tp);
381 hlist_add_head(&tp_c->hnode, &tc_u_common_hash[h]);
1da177e4
LT
382 }
383
384 tp_c->refcnt++;
1ce87720
JF
385 RCU_INIT_POINTER(root_ht->next, tp_c->hlist);
386 rcu_assign_pointer(tp_c->hlist, root_ht);
1da177e4
LT
387 root_ht->tp_c = tp_c;
388
1ce87720 389 rcu_assign_pointer(tp->root, root_ht);
1da177e4
LT
390 tp->data = tp_c;
391 return 0;
392}
393
5a7a5555 394static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n,
de5df632 395 bool free_pf)
1da177e4 396{
360a6fdd
PA
397 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
398
18d0264f 399 tcf_exts_destroy(&n->exts);
35c55fc1 400 tcf_exts_put_net(&n->exts);
360a6fdd
PA
401 if (ht && --ht->refcnt == 0)
402 kfree(ht);
1da177e4 403#ifdef CONFIG_CLS_U32_PERF
de5df632
JF
404 if (free_pf)
405 free_percpu(n->pf);
a1ddcfee
JF
406#endif
407#ifdef CONFIG_CLS_U32_MARK
de5df632
JF
408 if (free_pf)
409 free_percpu(n->pcpu_success);
1da177e4
LT
410#endif
411 kfree(n);
412 return 0;
413}
414
de5df632
JF
415/* u32_delete_key_rcu should be called when free'ing a copied
416 * version of a tc_u_knode obtained from u32_init_knode(). When
417 * copies are obtained from u32_init_knode() the statistics are
418 * shared between the old and new copies to allow readers to
419 * continue to update the statistics during the copy. To support
420 * this the u32_delete_key_rcu variant does not free the percpu
421 * statistics.
422 */
c0d378ef
CW
423static void u32_delete_key_work(struct work_struct *work)
424{
7afe8e4f
CW
425 struct tc_u_knode *key = container_of(to_rcu_work(work),
426 struct tc_u_knode,
427 rwork);
c0d378ef
CW
428 rtnl_lock();
429 u32_destroy_key(key->tp, key, false);
430 rtnl_unlock();
431}
432
de5df632
JF
433/* u32_delete_key_freepf_rcu is the rcu callback variant
434 * that free's the entire structure including the statistics
435 * percpu variables. Only use this if the key is not a copy
436 * returned by u32_init_knode(). See u32_delete_key_rcu()
437 * for the variant that should be used with keys return from
438 * u32_init_knode()
439 */
c0d378ef
CW
440static void u32_delete_key_freepf_work(struct work_struct *work)
441{
7afe8e4f
CW
442 struct tc_u_knode *key = container_of(to_rcu_work(work),
443 struct tc_u_knode,
444 rwork);
c0d378ef
CW
445 rtnl_lock();
446 u32_destroy_key(key->tp, key, true);
447 rtnl_unlock();
448}
449
82d567c2 450static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key)
1da177e4 451{
1ce87720
JF
452 struct tc_u_knode __rcu **kp;
453 struct tc_u_knode *pkp;
a96366bf 454 struct tc_u_hnode *ht = rtnl_dereference(key->ht_up);
1da177e4
LT
455
456 if (ht) {
1ce87720
JF
457 kp = &ht->ht[TC_U32_HASH(key->handle)];
458 for (pkp = rtnl_dereference(*kp); pkp;
459 kp = &pkp->next, pkp = rtnl_dereference(*kp)) {
460 if (pkp == key) {
461 RCU_INIT_POINTER(*kp, key->next);
1da177e4 462
a0efb80c 463 tcf_unbind_filter(tp, &key->res);
cf0a9345 464 idr_remove(&ht->handle_idr, key->handle);
35c55fc1 465 tcf_exts_get_net(&key->exts);
7afe8e4f 466 tcf_queue_work(&key->rwork, u32_delete_key_freepf_work);
1da177e4
LT
467 return 0;
468 }
469 }
470 }
547b792c 471 WARN_ON(1);
1da177e4
LT
472 return 0;
473}
474
77460411 475static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h)
a1b7c5fd 476{
245dc512 477 struct tcf_block *block = tp->chain->block;
de4784ca 478 struct tc_cls_u32_offload cls_u32 = {};
a1b7c5fd 479
de4784ca 480 tc_cls_common_offload_init(&cls_u32.common, tp);
77460411
JP
481 cls_u32.command = TC_CLSU32_DELETE_HNODE;
482 cls_u32.hnode.divisor = h->divisor;
483 cls_u32.hnode.handle = h->handle;
484 cls_u32.hnode.prio = h->prio;
de4784ca 485
245dc512 486 tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, false);
a1b7c5fd
JF
487}
488
5a7a5555
JHS
489static int u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
490 u32 flags)
a1b7c5fd 491{
245dc512 492 struct tcf_block *block = tp->chain->block;
de4784ca 493 struct tc_cls_u32_offload cls_u32 = {};
245dc512
JP
494 bool skip_sw = tc_skip_sw(flags);
495 bool offloaded = false;
d34e3e18 496 int err;
a1b7c5fd 497
de4784ca
JP
498 tc_cls_common_offload_init(&cls_u32.common, tp);
499 cls_u32.command = TC_CLSU32_NEW_HNODE;
500 cls_u32.hnode.divisor = h->divisor;
501 cls_u32.hnode.handle = h->handle;
502 cls_u32.hnode.prio = h->prio;
a1b7c5fd 503
245dc512
JP
504 err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, skip_sw);
505 if (err < 0) {
506 u32_clear_hw_hnode(tp, h);
d47a0f38 507 return err;
245dc512
JP
508 } else if (err > 0) {
509 offloaded = true;
510 }
511
512 if (skip_sw && !offloaded)
513 return -EINVAL;
d34e3e18
SS
514
515 return 0;
a1b7c5fd
JF
516}
517
77460411 518static void u32_remove_hw_knode(struct tcf_proto *tp, u32 handle)
a1b7c5fd 519{
245dc512 520 struct tcf_block *block = tp->chain->block;
de4784ca 521 struct tc_cls_u32_offload cls_u32 = {};
a1b7c5fd 522
de4784ca 523 tc_cls_common_offload_init(&cls_u32.common, tp);
77460411
JP
524 cls_u32.command = TC_CLSU32_DELETE_KNODE;
525 cls_u32.knode.handle = handle;
a1b7c5fd 526
245dc512 527 tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, false);
a1b7c5fd
JF
528}
529
5a7a5555
JHS
530static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
531 u32 flags)
a1b7c5fd 532{
b49f9c88 533 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
245dc512 534 struct tcf_block *block = tp->chain->block;
de4784ca 535 struct tc_cls_u32_offload cls_u32 = {};
245dc512 536 bool skip_sw = tc_skip_sw(flags);
d34e3e18 537 int err;
a1b7c5fd 538
de4784ca
JP
539 tc_cls_common_offload_init(&cls_u32.common, tp);
540 cls_u32.command = TC_CLSU32_REPLACE_KNODE;
541 cls_u32.knode.handle = n->handle;
542 cls_u32.knode.fshift = n->fshift;
a1b7c5fd 543#ifdef CONFIG_CLS_U32_MARK
de4784ca
JP
544 cls_u32.knode.val = n->val;
545 cls_u32.knode.mask = n->mask;
a1b7c5fd 546#else
de4784ca
JP
547 cls_u32.knode.val = 0;
548 cls_u32.knode.mask = 0;
a1b7c5fd 549#endif
de4784ca
JP
550 cls_u32.knode.sel = &n->sel;
551 cls_u32.knode.exts = &n->exts;
201c44bd 552 if (n->ht_down)
b49f9c88 553 cls_u32.knode.link_handle = ht->handle;
201c44bd 554
245dc512
JP
555 err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, skip_sw);
556 if (err < 0) {
557 u32_remove_hw_knode(tp, n->handle);
201c44bd 558 return err;
245dc512
JP
559 } else if (err > 0) {
560 n->flags |= TCA_CLS_FLAGS_IN_HW;
561 }
562
0f04d057 563 if (skip_sw && !(n->flags & TCA_CLS_FLAGS_IN_HW))
245dc512 564 return -EINVAL;
d34e3e18
SS
565
566 return 0;
a1b7c5fd
JF
567}
568
a0efb80c 569static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
1da177e4
LT
570{
571 struct tc_u_knode *n;
cc7ec456 572 unsigned int h;
1da177e4 573
cc7ec456 574 for (h = 0; h <= ht->divisor; h++) {
1ce87720
JF
575 while ((n = rtnl_dereference(ht->ht[h])) != NULL) {
576 RCU_INIT_POINTER(ht->ht[h],
577 rtnl_dereference(n->next));
a0efb80c 578 tcf_unbind_filter(tp, &n->res);
a1b7c5fd 579 u32_remove_hw_knode(tp, n->handle);
e7614370 580 idr_remove_ext(&ht->handle_idr, n->handle);
35c55fc1 581 if (tcf_exts_get_net(&n->exts))
7afe8e4f 582 tcf_queue_work(&n->rwork, u32_delete_key_freepf_work);
35c55fc1
CW
583 else
584 u32_destroy_key(n->tp, n, true);
1da177e4
LT
585 }
586 }
587}
588
589static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
590{
591 struct tc_u_common *tp_c = tp->data;
1ce87720
JF
592 struct tc_u_hnode __rcu **hn;
593 struct tc_u_hnode *phn;
1da177e4 594
547b792c 595 WARN_ON(ht->refcnt);
1da177e4 596
a0efb80c 597 u32_clear_hnode(tp, ht);
1da177e4 598
1ce87720
JF
599 hn = &tp_c->hlist;
600 for (phn = rtnl_dereference(*hn);
601 phn;
602 hn = &phn->next, phn = rtnl_dereference(*hn)) {
603 if (phn == ht) {
a1b7c5fd 604 u32_clear_hw_hnode(tp, ht);
e7614370
CW
605 idr_destroy(&ht->handle_idr);
606 idr_remove_ext(&tp_c->handle_idr, ht->handle);
1ce87720
JF
607 RCU_INIT_POINTER(*hn, ht->next);
608 kfree_rcu(ht, rcu);
1da177e4
LT
609 return 0;
610 }
611 }
612
1da177e4
LT
613 return -ENOENT;
614}
615
1e052be6
CW
616static bool ht_empty(struct tc_u_hnode *ht)
617{
618 unsigned int h;
619
620 for (h = 0; h <= ht->divisor; h++)
621 if (rcu_access_pointer(ht->ht[h]))
622 return false;
623
624 return true;
625}
626
763dbf63 627static void u32_destroy(struct tcf_proto *tp)
1da177e4
LT
628{
629 struct tc_u_common *tp_c = tp->data;
1ce87720 630 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
1da177e4 631
547b792c 632 WARN_ON(root_ht == NULL);
1da177e4
LT
633
634 if (root_ht && --root_ht->refcnt == 0)
635 u32_destroy_hnode(tp, root_ht);
636
637 if (--tp_c->refcnt == 0) {
638 struct tc_u_hnode *ht;
1da177e4 639
3cd904ec 640 hlist_del(&tp_c->hnode);
1da177e4 641
1ce87720 642 while ((ht = rtnl_dereference(tp_c->hlist)) != NULL) {
360a6fdd 643 u32_clear_hnode(tp, ht);
1ce87720 644 RCU_INIT_POINTER(tp_c->hlist, ht->next);
360a6fdd
PA
645
646 /* u32_destroy_key() will later free ht for us, if it's
647 * still referenced by some knode
648 */
649 if (--ht->refcnt == 0)
650 kfree_rcu(ht, rcu);
3ff50b79 651 }
1da177e4 652
e7614370 653 idr_destroy(&tp_c->handle_idr);
1da177e4
LT
654 kfree(tp_c);
655 }
656
657 tp->data = NULL;
658}
659
8113c095 660static int u32_delete(struct tcf_proto *tp, void *arg, bool *last)
1da177e4 661{
8113c095 662 struct tc_u_hnode *ht = arg;
1ce87720 663 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
763dbf63
WC
664 struct tc_u_common *tp_c = tp->data;
665 int ret = 0;
1da177e4
LT
666
667 if (ht == NULL)
763dbf63 668 goto out;
1da177e4 669
a1b7c5fd
JF
670 if (TC_U32_KEY(ht->handle)) {
671 u32_remove_hw_knode(tp, ht->handle);
763dbf63
WC
672 ret = u32_delete_key(tp, (struct tc_u_knode *)ht);
673 goto out;
a1b7c5fd 674 }
1da177e4 675
1ce87720 676 if (root_ht == ht)
1da177e4
LT
677 return -EINVAL;
678
e56cfad1
JP
679 if (ht->refcnt == 1) {
680 ht->refcnt--;
1da177e4 681 u32_destroy_hnode(tp, ht);
e56cfad1
JP
682 } else {
683 return -EBUSY;
684 }
1da177e4 685
763dbf63
WC
686out:
687 *last = true;
688 if (root_ht) {
689 if (root_ht->refcnt > 1) {
690 *last = false;
691 goto ret;
692 }
693 if (root_ht->refcnt == 1) {
694 if (!ht_empty(root_ht)) {
695 *last = false;
696 goto ret;
697 }
698 }
699 }
700
701 if (tp_c->refcnt > 1) {
702 *last = false;
703 goto ret;
704 }
705
706 if (tp_c->refcnt == 1) {
707 struct tc_u_hnode *ht;
708
709 for (ht = rtnl_dereference(tp_c->hlist);
710 ht;
711 ht = rtnl_dereference(ht->next))
712 if (!ht_empty(ht)) {
713 *last = false;
714 break;
715 }
716 }
717
718ret:
719 return ret;
1da177e4
LT
720}
721
e7614370 722static u32 gen_new_kid(struct tc_u_hnode *ht, u32 htid)
1da177e4 723{
e7614370
CW
724 unsigned long idr_index;
725 u32 start = htid | 0x800;
726 u32 max = htid | 0xFFF;
727 u32 min = htid;
728
729 if (idr_alloc_ext(&ht->handle_idr, NULL, &idr_index,
730 start, max + 1, GFP_KERNEL)) {
731 if (idr_alloc_ext(&ht->handle_idr, NULL, &idr_index,
732 min + 1, max + 1, GFP_KERNEL))
733 return max;
734 }
7801db8a 735
e7614370 736 return (u32)idr_index;
1da177e4
LT
737}
738
6fa8c014
PM
739static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
740 [TCA_U32_CLASSID] = { .type = NLA_U32 },
741 [TCA_U32_HASH] = { .type = NLA_U32 },
742 [TCA_U32_LINK] = { .type = NLA_U32 },
743 [TCA_U32_DIVISOR] = { .type = NLA_U32 },
744 [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) },
745 [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
746 [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) },
9e8ce79c 747 [TCA_U32_FLAGS] = { .type = NLA_U32 },
6fa8c014
PM
748};
749
c1b52739
BL
750static int u32_set_parms(struct net *net, struct tcf_proto *tp,
751 unsigned long base, struct tc_u_hnode *ht,
add93b61 752 struct tc_u_knode *n, struct nlattr **tb,
2f7ef2f8 753 struct nlattr *est, bool ovr)
1da177e4 754{
b9a24bb7 755 int err;
1da177e4 756
705c7091 757 err = tcf_exts_validate(net, tp, tb, est, &n->exts, ovr);
1da177e4
LT
758 if (err < 0)
759 return err;
760
add93b61 761 if (tb[TCA_U32_LINK]) {
1587bac4 762 u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
47a1a1d4 763 struct tc_u_hnode *ht_down = NULL, *ht_old;
1da177e4
LT
764
765 if (TC_U32_KEY(handle))
705c7091 766 return -EINVAL;
1da177e4
LT
767
768 if (handle) {
769 ht_down = u32_lookup_ht(ht->tp_c, handle);
770
771 if (ht_down == NULL)
705c7091 772 return -EINVAL;
1da177e4
LT
773 ht_down->refcnt++;
774 }
775
1ce87720
JF
776 ht_old = rtnl_dereference(n->ht_down);
777 rcu_assign_pointer(n->ht_down, ht_down);
1da177e4 778
47a1a1d4
PM
779 if (ht_old)
780 ht_old->refcnt--;
1da177e4 781 }
add93b61 782 if (tb[TCA_U32_CLASSID]) {
1587bac4 783 n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
1da177e4
LT
784 tcf_bind_filter(tp, &n->res, base);
785 }
786
787#ifdef CONFIG_NET_CLS_IND
add93b61 788 if (tb[TCA_U32_INDEV]) {
2519a602
WC
789 int ret;
790 ret = tcf_change_indev(net, tb[TCA_U32_INDEV]);
791 if (ret < 0)
705c7091 792 return -EINVAL;
2519a602 793 n->ifindex = ret;
1da177e4
LT
794 }
795#endif
1da177e4 796 return 0;
1da177e4
LT
797}
798
5a7a5555 799static void u32_replace_knode(struct tcf_proto *tp, struct tc_u_common *tp_c,
de5df632
JF
800 struct tc_u_knode *n)
801{
802 struct tc_u_knode __rcu **ins;
803 struct tc_u_knode *pins;
804 struct tc_u_hnode *ht;
805
806 if (TC_U32_HTID(n->handle) == TC_U32_ROOT)
807 ht = rtnl_dereference(tp->root);
808 else
809 ht = u32_lookup_ht(tp_c, TC_U32_HTID(n->handle));
810
811 ins = &ht->ht[TC_U32_HASH(n->handle)];
812
813 /* The node must always exist for it to be replaced if this is not the
814 * case then something went very wrong elsewhere.
815 */
816 for (pins = rtnl_dereference(*ins); ;
817 ins = &pins->next, pins = rtnl_dereference(*ins))
818 if (pins->handle == n->handle)
819 break;
820
e7614370 821 idr_replace_ext(&ht->handle_idr, n, n->handle);
de5df632
JF
822 RCU_INIT_POINTER(n->next, pins->next);
823 rcu_assign_pointer(*ins, n);
824}
825
826static struct tc_u_knode *u32_init_knode(struct tcf_proto *tp,
827 struct tc_u_knode *n)
828{
b49f9c88 829 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
de5df632 830 struct tc_u32_sel *s = &n->sel;
b49f9c88 831 struct tc_u_knode *new;
de5df632
JF
832
833 new = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key),
834 GFP_KERNEL);
835
836 if (!new)
837 return NULL;
838
839 RCU_INIT_POINTER(new->next, n->next);
840 new->handle = n->handle;
841 RCU_INIT_POINTER(new->ht_up, n->ht_up);
842
843#ifdef CONFIG_NET_CLS_IND
844 new->ifindex = n->ifindex;
845#endif
846 new->fshift = n->fshift;
847 new->res = n->res;
9e8ce79c 848 new->flags = n->flags;
b49f9c88 849 RCU_INIT_POINTER(new->ht_down, ht);
de5df632
JF
850
851 /* bump reference count as long as we hold pointer to structure */
b49f9c88
PA
852 if (ht)
853 ht->refcnt++;
de5df632
JF
854
855#ifdef CONFIG_CLS_U32_PERF
856 /* Statistics may be incremented by readers during update
857 * so we must keep them in tact. When the node is later destroyed
858 * a special destroy call must be made to not free the pf memory.
859 */
860 new->pf = n->pf;
861#endif
862
863#ifdef CONFIG_CLS_U32_MARK
864 new->val = n->val;
865 new->mask = n->mask;
866 /* Similarly success statistics must be moved as pointers */
867 new->pcpu_success = n->pcpu_success;
868#endif
869 new->tp = tp;
870 memcpy(&new->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
871
b9a24bb7
WC
872 if (tcf_exts_init(&new->exts, TCA_U32_ACT, TCA_U32_POLICE)) {
873 kfree(new);
874 return NULL;
875 }
de5df632
JF
876
877 return new;
878}
879
c1b52739 880static int u32_change(struct net *net, struct sk_buff *in_skb,
af4c6641 881 struct tcf_proto *tp, unsigned long base, u32 handle,
8113c095 882 struct nlattr **tca, void **arg, bool ovr)
1da177e4
LT
883{
884 struct tc_u_common *tp_c = tp->data;
885 struct tc_u_hnode *ht;
886 struct tc_u_knode *n;
887 struct tc_u32_sel *s;
add93b61
PM
888 struct nlattr *opt = tca[TCA_OPTIONS];
889 struct nlattr *tb[TCA_U32_MAX + 1];
9e8ce79c 890 u32 htid, flags = 0;
1da177e4 891 int err;
459d5f62
JF
892#ifdef CONFIG_CLS_U32_PERF
893 size_t size;
894#endif
1da177e4
LT
895
896 if (opt == NULL)
897 return handle ? -EINVAL : 0;
898
fceb6435 899 err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy, NULL);
cee63723
PM
900 if (err < 0)
901 return err;
1da177e4 902
d34e3e18 903 if (tb[TCA_U32_FLAGS]) {
9e8ce79c 904 flags = nla_get_u32(tb[TCA_U32_FLAGS]);
d34e3e18 905 if (!tc_flags_valid(flags))
1a0f7d29 906 return -EINVAL;
d34e3e18 907 }
9e8ce79c 908
8113c095 909 n = *arg;
cc7ec456 910 if (n) {
de5df632
JF
911 struct tc_u_knode *new;
912
1da177e4
LT
913 if (TC_U32_KEY(n->handle) == 0)
914 return -EINVAL;
915
2d965923
IV
916 if ((n->flags ^ flags) &
917 ~(TCA_CLS_FLAGS_IN_HW | TCA_CLS_FLAGS_NOT_IN_HW))
9e8ce79c
JF
918 return -EINVAL;
919
de5df632
JF
920 new = u32_init_knode(tp, n);
921 if (!new)
922 return -ENOMEM;
923
924 err = u32_set_parms(net, tp, base,
925 rtnl_dereference(n->ht_up), new, tb,
926 tca[TCA_RATE], ovr);
927
928 if (err) {
929 u32_destroy_key(tp, new, false);
930 return err;
931 }
932
d34e3e18
SS
933 err = u32_replace_hw_knode(tp, new, flags);
934 if (err) {
935 u32_destroy_key(tp, new, false);
936 return err;
937 }
938
24d3dc6d
OG
939 if (!tc_in_hw(new->flags))
940 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
941
de5df632 942 u32_replace_knode(tp, tp_c, new);
a0efb80c 943 tcf_unbind_filter(tp, &n->res);
35c55fc1 944 tcf_exts_get_net(&n->exts);
7afe8e4f 945 tcf_queue_work(&n->rwork, u32_delete_key_work);
de5df632 946 return 0;
1da177e4
LT
947 }
948
add93b61 949 if (tb[TCA_U32_DIVISOR]) {
cc7ec456 950 unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
1da177e4
LT
951
952 if (--divisor > 0x100)
953 return -EINVAL;
954 if (TC_U32_KEY(handle))
955 return -EINVAL;
cc7ec456 956 ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL);
1da177e4
LT
957 if (ht == NULL)
958 return -ENOBUFS;
e7614370
CW
959 if (handle == 0) {
960 handle = gen_new_htid(tp->data, ht);
961 if (handle == 0) {
962 kfree(ht);
963 return -ENOMEM;
964 }
965 } else {
966 err = idr_alloc_ext(&tp_c->handle_idr, ht, NULL,
967 handle, handle + 1, GFP_KERNEL);
968 if (err) {
969 kfree(ht);
970 return err;
971 }
972 }
1da177e4 973 ht->tp_c = tp_c;
e56cfad1 974 ht->refcnt = 1;
1da177e4
LT
975 ht->divisor = divisor;
976 ht->handle = handle;
977 ht->prio = tp->prio;
e7614370 978 idr_init(&ht->handle_idr);
6eef3801
JK
979
980 err = u32_replace_hw_hnode(tp, ht, flags);
981 if (err) {
e7614370 982 idr_remove_ext(&tp_c->handle_idr, handle);
6eef3801
JK
983 kfree(ht);
984 return err;
985 }
986
1ce87720
JF
987 RCU_INIT_POINTER(ht->next, tp_c->hlist);
988 rcu_assign_pointer(tp_c->hlist, ht);
8113c095 989 *arg = ht;
a1b7c5fd 990
1da177e4
LT
991 return 0;
992 }
993
add93b61 994 if (tb[TCA_U32_HASH]) {
1587bac4 995 htid = nla_get_u32(tb[TCA_U32_HASH]);
1da177e4 996 if (TC_U32_HTID(htid) == TC_U32_ROOT) {
1ce87720 997 ht = rtnl_dereference(tp->root);
1da177e4
LT
998 htid = ht->handle;
999 } else {
1000 ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
1001 if (ht == NULL)
1002 return -EINVAL;
1003 }
1004 } else {
1ce87720 1005 ht = rtnl_dereference(tp->root);
1da177e4
LT
1006 htid = ht->handle;
1007 }
1008
1009 if (ht->divisor < TC_U32_HASH(htid))
1010 return -EINVAL;
1011
1012 if (handle) {
1013 if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
1014 return -EINVAL;
1015 handle = htid | TC_U32_NODE(handle);
e7614370
CW
1016 err = idr_alloc_ext(&ht->handle_idr, NULL, NULL,
1017 handle, handle + 1,
1018 GFP_KERNEL);
1019 if (err)
1020 return err;
1da177e4
LT
1021 } else
1022 handle = gen_new_kid(ht, htid);
1023
e7614370
CW
1024 if (tb[TCA_U32_SEL] == NULL) {
1025 err = -EINVAL;
1026 goto erridr;
1027 }
1da177e4 1028
add93b61 1029 s = nla_data(tb[TCA_U32_SEL]);
1da177e4 1030
0da974f4 1031 n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
e7614370
CW
1032 if (n == NULL) {
1033 err = -ENOBUFS;
1034 goto erridr;
1035 }
1da177e4 1036
1da177e4 1037#ifdef CONFIG_CLS_U32_PERF
459d5f62
JF
1038 size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64);
1039 n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt));
1040 if (!n->pf) {
e7614370
CW
1041 err = -ENOBUFS;
1042 goto errfree;
1da177e4 1043 }
1da177e4
LT
1044#endif
1045
1046 memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
a96366bf 1047 RCU_INIT_POINTER(n->ht_up, ht);
1da177e4 1048 n->handle = handle;
b2268016 1049 n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
9e8ce79c 1050 n->flags = flags;
1ce87720 1051 n->tp = tp;
1da177e4 1052
b9a24bb7
WC
1053 err = tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE);
1054 if (err < 0)
1055 goto errout;
1056
1da177e4 1057#ifdef CONFIG_CLS_U32_MARK
459d5f62 1058 n->pcpu_success = alloc_percpu(u32);
a1ddcfee
JF
1059 if (!n->pcpu_success) {
1060 err = -ENOMEM;
1061 goto errout;
1062 }
459d5f62 1063
add93b61 1064 if (tb[TCA_U32_MARK]) {
1da177e4
LT
1065 struct tc_u32_mark *mark;
1066
add93b61 1067 mark = nla_data(tb[TCA_U32_MARK]);
459d5f62
JF
1068 n->val = mark->val;
1069 n->mask = mark->mask;
1da177e4
LT
1070 }
1071#endif
1072
2f7ef2f8 1073 err = u32_set_parms(net, tp, base, ht, n, tb, tca[TCA_RATE], ovr);
1da177e4 1074 if (err == 0) {
1ce87720
JF
1075 struct tc_u_knode __rcu **ins;
1076 struct tc_u_knode *pins;
1077
d34e3e18
SS
1078 err = u32_replace_hw_knode(tp, n, flags);
1079 if (err)
1080 goto errhw;
1081
24d3dc6d
OG
1082 if (!tc_in_hw(n->flags))
1083 n->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1084
1ce87720
JF
1085 ins = &ht->ht[TC_U32_HASH(handle)];
1086 for (pins = rtnl_dereference(*ins); pins;
1087 ins = &pins->next, pins = rtnl_dereference(*ins))
1088 if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle))
1da177e4
LT
1089 break;
1090
1ce87720
JF
1091 RCU_INIT_POINTER(n->next, pins);
1092 rcu_assign_pointer(*ins, n);
8113c095 1093 *arg = n;
1da177e4
LT
1094 return 0;
1095 }
a1ddcfee 1096
d34e3e18 1097errhw:
a1ddcfee
JF
1098#ifdef CONFIG_CLS_U32_MARK
1099 free_percpu(n->pcpu_success);
1100#endif
1101
b9a24bb7
WC
1102errout:
1103 tcf_exts_destroy(&n->exts);
1da177e4 1104#ifdef CONFIG_CLS_U32_PERF
e7614370 1105errfree:
1ce87720 1106 free_percpu(n->pf);
1da177e4
LT
1107#endif
1108 kfree(n);
e7614370
CW
1109erridr:
1110 idr_remove_ext(&ht->handle_idr, handle);
1da177e4
LT
1111 return err;
1112}
1113
1114static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1115{
1116 struct tc_u_common *tp_c = tp->data;
1117 struct tc_u_hnode *ht;
1118 struct tc_u_knode *n;
cc7ec456 1119 unsigned int h;
1da177e4
LT
1120
1121 if (arg->stop)
1122 return;
1123
1ce87720
JF
1124 for (ht = rtnl_dereference(tp_c->hlist);
1125 ht;
1126 ht = rtnl_dereference(ht->next)) {
1da177e4
LT
1127 if (ht->prio != tp->prio)
1128 continue;
1129 if (arg->count >= arg->skip) {
8113c095 1130 if (arg->fn(tp, ht, arg) < 0) {
1da177e4
LT
1131 arg->stop = 1;
1132 return;
1133 }
1134 }
1135 arg->count++;
1136 for (h = 0; h <= ht->divisor; h++) {
1ce87720
JF
1137 for (n = rtnl_dereference(ht->ht[h]);
1138 n;
1139 n = rtnl_dereference(n->next)) {
1da177e4
LT
1140 if (arg->count < arg->skip) {
1141 arg->count++;
1142 continue;
1143 }
8113c095 1144 if (arg->fn(tp, n, arg) < 0) {
1da177e4
LT
1145 arg->stop = 1;
1146 return;
1147 }
1148 arg->count++;
1149 }
1150 }
1151 }
1152}
1153
07d79fc7
CW
1154static void u32_bind_class(void *fh, u32 classid, unsigned long cl)
1155{
1156 struct tc_u_knode *n = fh;
1157
1158 if (n && n->res.classid == classid)
1159 n->res.class = cl;
1160}
1161
8113c095 1162static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
5a7a5555 1163 struct sk_buff *skb, struct tcmsg *t)
1da177e4 1164{
8113c095 1165 struct tc_u_knode *n = fh;
1ce87720 1166 struct tc_u_hnode *ht_up, *ht_down;
4b3550ef 1167 struct nlattr *nest;
1da177e4
LT
1168
1169 if (n == NULL)
1170 return skb->len;
1171
1172 t->tcm_handle = n->handle;
1173
4b3550ef
PM
1174 nest = nla_nest_start(skb, TCA_OPTIONS);
1175 if (nest == NULL)
1176 goto nla_put_failure;
1da177e4
LT
1177
1178 if (TC_U32_KEY(n->handle) == 0) {
8113c095 1179 struct tc_u_hnode *ht = fh;
cc7ec456
ED
1180 u32 divisor = ht->divisor + 1;
1181
1b34ec43
DM
1182 if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
1183 goto nla_put_failure;
1da177e4 1184 } else {
459d5f62
JF
1185#ifdef CONFIG_CLS_U32_PERF
1186 struct tc_u32_pcnt *gpf;
459d5f62 1187 int cpu;
80aab73d 1188#endif
459d5f62 1189
1b34ec43
DM
1190 if (nla_put(skb, TCA_U32_SEL,
1191 sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
1192 &n->sel))
1193 goto nla_put_failure;
1ce87720
JF
1194
1195 ht_up = rtnl_dereference(n->ht_up);
1196 if (ht_up) {
1da177e4 1197 u32 htid = n->handle & 0xFFFFF000;
1b34ec43
DM
1198 if (nla_put_u32(skb, TCA_U32_HASH, htid))
1199 goto nla_put_failure;
1da177e4 1200 }
1b34ec43
DM
1201 if (n->res.classid &&
1202 nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid))
1203 goto nla_put_failure;
1ce87720
JF
1204
1205 ht_down = rtnl_dereference(n->ht_down);
1206 if (ht_down &&
1207 nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))
1b34ec43 1208 goto nla_put_failure;
1da177e4 1209
9e8ce79c
JF
1210 if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags))
1211 goto nla_put_failure;
1212
1da177e4 1213#ifdef CONFIG_CLS_U32_MARK
459d5f62
JF
1214 if ((n->val || n->mask)) {
1215 struct tc_u32_mark mark = {.val = n->val,
1216 .mask = n->mask,
1217 .success = 0};
80aab73d 1218 int cpum;
459d5f62 1219
80aab73d
JF
1220 for_each_possible_cpu(cpum) {
1221 __u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum);
459d5f62
JF
1222
1223 mark.success += cnt;
1224 }
1225
1226 if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark))
1227 goto nla_put_failure;
1228 }
1da177e4
LT
1229#endif
1230
5da57f42 1231 if (tcf_exts_dump(skb, &n->exts) < 0)
add93b61 1232 goto nla_put_failure;
1da177e4
LT
1233
1234#ifdef CONFIG_NET_CLS_IND
2519a602
WC
1235 if (n->ifindex) {
1236 struct net_device *dev;
1237 dev = __dev_get_by_index(net, n->ifindex);
1238 if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
1239 goto nla_put_failure;
1240 }
1da177e4
LT
1241#endif
1242#ifdef CONFIG_CLS_U32_PERF
459d5f62
JF
1243 gpf = kzalloc(sizeof(struct tc_u32_pcnt) +
1244 n->sel.nkeys * sizeof(u64),
1245 GFP_KERNEL);
1246 if (!gpf)
1247 goto nla_put_failure;
1248
1249 for_each_possible_cpu(cpu) {
1250 int i;
1251 struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu);
1252
1253 gpf->rcnt += pf->rcnt;
1254 gpf->rhit += pf->rhit;
1255 for (i = 0; i < n->sel.nkeys; i++)
1256 gpf->kcnts[i] += pf->kcnts[i];
1257 }
1258
9854518e
ND
1259 if (nla_put_64bit(skb, TCA_U32_PCNT,
1260 sizeof(struct tc_u32_pcnt) +
1261 n->sel.nkeys * sizeof(u64),
1262 gpf, TCA_U32_PAD)) {
459d5f62 1263 kfree(gpf);
1b34ec43 1264 goto nla_put_failure;
459d5f62
JF
1265 }
1266 kfree(gpf);
1da177e4
LT
1267#endif
1268 }
1269
4b3550ef
PM
1270 nla_nest_end(skb, nest);
1271
1da177e4 1272 if (TC_U32_KEY(n->handle))
5da57f42 1273 if (tcf_exts_dump_stats(skb, &n->exts) < 0)
add93b61 1274 goto nla_put_failure;
1da177e4
LT
1275 return skb->len;
1276
add93b61 1277nla_put_failure:
4b3550ef 1278 nla_nest_cancel(skb, nest);
1da177e4
LT
1279 return -1;
1280}
1281
2eb9d75c 1282static struct tcf_proto_ops cls_u32_ops __read_mostly = {
1da177e4
LT
1283 .kind = "u32",
1284 .classify = u32_classify,
1285 .init = u32_init,
1286 .destroy = u32_destroy,
1287 .get = u32_get,
1da177e4
LT
1288 .change = u32_change,
1289 .delete = u32_delete,
1290 .walk = u32_walk,
1291 .dump = u32_dump,
07d79fc7 1292 .bind_class = u32_bind_class,
1da177e4
LT
1293 .owner = THIS_MODULE,
1294};
1295
1296static int __init init_u32(void)
1297{
3cd904ec
WC
1298 int i, ret;
1299
6ff9c364 1300 pr_info("u32 classifier\n");
1da177e4 1301#ifdef CONFIG_CLS_U32_PERF
6ff9c364 1302 pr_info(" Performance counters on\n");
1da177e4 1303#endif
1da177e4 1304#ifdef CONFIG_NET_CLS_IND
6ff9c364 1305 pr_info(" input device check on\n");
1da177e4
LT
1306#endif
1307#ifdef CONFIG_NET_CLS_ACT
6ff9c364 1308 pr_info(" Actions configured\n");
1da177e4 1309#endif
3cd904ec
WC
1310 tc_u_common_hash = kvmalloc_array(U32_HASH_SIZE,
1311 sizeof(struct hlist_head),
1312 GFP_KERNEL);
1313 if (!tc_u_common_hash)
1314 return -ENOMEM;
1315
1316 for (i = 0; i < U32_HASH_SIZE; i++)
1317 INIT_HLIST_HEAD(&tc_u_common_hash[i]);
1318
1319 ret = register_tcf_proto_ops(&cls_u32_ops);
1320 if (ret)
1321 kvfree(tc_u_common_hash);
1322 return ret;
1da177e4
LT
1323}
1324
10297b99 1325static void __exit exit_u32(void)
1da177e4
LT
1326{
1327 unregister_tcf_proto_ops(&cls_u32_ops);
3cd904ec 1328 kvfree(tc_u_common_hash);
1da177e4
LT
1329}
1330
1331module_init(init_u32)
1332module_exit(exit_u32)
1333MODULE_LICENSE("GPL");