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