2 * net/sched/cls_u32.c Ugly (or Universal) 32bit key Packet Classifier.
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
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.
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.
20 * It seems that it represents the best middle point between
21 * speed and manageability both by human and by machine.
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.
27 * JHS: We should remove the CONFIG_NET_CLS_IND from here
28 * eventually when the meta match extension is made available
30 * nfmark match added by Catalin(ux aka Dino) BOIE <catab at umbrella.ro>
33 #include <linux/module.h>
34 #include <linux/slab.h>
35 #include <linux/types.h>
36 #include <linux/kernel.h>
37 #include <linux/string.h>
38 #include <linux/errno.h>
39 #include <linux/percpu.h>
40 #include <linux/rtnetlink.h>
41 #include <linux/skbuff.h>
42 #include <linux/bitmap.h>
43 #include <net/netlink.h>
44 #include <net/act_api.h>
45 #include <net/pkt_cls.h>
46 #include <linux/netdevice.h>
49 struct tc_u_knode __rcu *next;
51 struct tc_u_hnode __rcu *ht_up;
53 #ifdef CONFIG_NET_CLS_IND
57 struct tcf_result res;
58 struct tc_u_hnode __rcu *ht_down;
59 #ifdef CONFIG_CLS_U32_PERF
60 struct tc_u32_pcnt __percpu *pf;
63 #ifdef CONFIG_CLS_U32_MARK
66 u32 __percpu *pcpu_success;
70 /* The 'sel' field MUST be the last field in structure to allow for
71 * tc_u32_keys allocated at end of structure.
73 struct tc_u32_sel sel;
77 struct tc_u_hnode __rcu *next;
80 struct tc_u_common *tp_c;
84 /* The 'ht' field MUST be the last field in structure to allow for
85 * more entries allocated at end of structure.
87 struct tc_u_knode __rcu *ht[1];
91 struct tc_u_hnode __rcu *hlist;
98 static inline unsigned int u32_hash_fold(__be32 key,
99 const struct tc_u32_sel *sel,
102 unsigned int h = ntohl(key & sel->hmask) >> fshift;
107 static int u32_classify(struct sk_buff *skb, const struct tcf_proto *tp, struct tcf_result *res)
110 struct tc_u_knode *knode;
112 } stack[TC_U32_MAXDEPTH];
114 struct tc_u_hnode *ht = rcu_dereference_bh(tp->root);
115 unsigned int off = skb_network_offset(skb);
116 struct tc_u_knode *n;
120 #ifdef CONFIG_CLS_U32_PERF
126 n = rcu_dereference_bh(ht->ht[sel]);
130 struct tc_u32_key *key = n->sel.keys;
132 #ifdef CONFIG_CLS_U32_PERF
133 __this_cpu_inc(n->pf->rcnt);
137 #ifdef CONFIG_CLS_U32_MARK
138 if ((skb->mark & n->mask) != n->val) {
139 n = rcu_dereference_bh(n->next);
142 __this_cpu_inc(*n->pcpu_success);
146 for (i = n->sel.nkeys; i > 0; i--, key++) {
147 int toff = off + key->off + (off2 & key->offmask);
150 if (skb_headroom(skb) + toff > INT_MAX)
153 data = skb_header_pointer(skb, toff, 4, &hdata);
156 if ((*data ^ key->val) & key->mask) {
157 n = rcu_dereference_bh(n->next);
160 #ifdef CONFIG_CLS_U32_PERF
161 __this_cpu_inc(n->pf->kcnts[j]);
166 ht = rcu_dereference_bh(n->ht_down);
169 if (n->sel.flags & TC_U32_TERMINAL) {
172 #ifdef CONFIG_NET_CLS_IND
173 if (!tcf_match_indev(skb, n->ifindex)) {
174 n = rcu_dereference_bh(n->next);
178 #ifdef CONFIG_CLS_U32_PERF
179 __this_cpu_inc(n->pf->rhit);
181 r = tcf_exts_exec(skb, &n->exts, res);
183 n = rcu_dereference_bh(n->next);
189 n = rcu_dereference_bh(n->next);
194 if (sdepth >= TC_U32_MAXDEPTH)
196 stack[sdepth].knode = n;
197 stack[sdepth].off = off;
200 ht = rcu_dereference_bh(n->ht_down);
205 data = skb_header_pointer(skb, off + n->sel.hoff, 4,
209 sel = ht->divisor & u32_hash_fold(*data, &n->sel,
212 if (!(n->sel.flags & (TC_U32_VAROFFSET | TC_U32_OFFSET | TC_U32_EAT)))
215 if (n->sel.flags & (TC_U32_OFFSET | TC_U32_VAROFFSET)) {
216 off2 = n->sel.off + 3;
217 if (n->sel.flags & TC_U32_VAROFFSET) {
220 data = skb_header_pointer(skb,
225 off2 += ntohs(n->sel.offmask & *data) >>
230 if (n->sel.flags & TC_U32_EAT) {
241 n = stack[sdepth].knode;
242 ht = rcu_dereference_bh(n->ht_up);
243 off = stack[sdepth].off;
250 net_warn_ratelimited("cls_u32: dead loop\n");
254 static struct tc_u_hnode *
255 u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
257 struct tc_u_hnode *ht;
259 for (ht = rtnl_dereference(tp_c->hlist);
261 ht = rtnl_dereference(ht->next))
262 if (ht->handle == handle)
268 static struct tc_u_knode *
269 u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
272 struct tc_u_knode *n = NULL;
274 sel = TC_U32_HASH(handle);
275 if (sel > ht->divisor)
278 for (n = rtnl_dereference(ht->ht[sel]);
280 n = rtnl_dereference(n->next))
281 if (n->handle == handle)
288 static unsigned long u32_get(struct tcf_proto *tp, u32 handle)
290 struct tc_u_hnode *ht;
291 struct tc_u_common *tp_c = tp->data;
293 if (TC_U32_HTID(handle) == TC_U32_ROOT)
294 ht = rtnl_dereference(tp->root);
296 ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
301 if (TC_U32_KEY(handle) == 0)
302 return (unsigned long)ht;
304 return (unsigned long)u32_lookup_key(ht, handle);
307 static u32 gen_new_htid(struct tc_u_common *tp_c)
311 /* hgenerator only used inside rtnl lock it is safe to increment
312 * without read _copy_ update semantics
315 if (++tp_c->hgenerator == 0x7FF)
316 tp_c->hgenerator = 1;
317 } while (--i > 0 && u32_lookup_ht(tp_c, (tp_c->hgenerator|0x800)<<20));
319 return i > 0 ? (tp_c->hgenerator|0x800)<<20 : 0;
322 static int u32_init(struct tcf_proto *tp)
324 struct tc_u_hnode *root_ht;
325 struct tc_u_common *tp_c;
327 tp_c = tp->q->u32_node;
329 root_ht = kzalloc(sizeof(*root_ht), GFP_KERNEL);
333 root_ht->divisor = 0;
335 root_ht->handle = tp_c ? gen_new_htid(tp_c) : 0x80000000;
336 root_ht->prio = tp->prio;
339 tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL);
345 tp->q->u32_node = tp_c;
349 RCU_INIT_POINTER(root_ht->next, tp_c->hlist);
350 rcu_assign_pointer(tp_c->hlist, root_ht);
351 root_ht->tp_c = tp_c;
353 rcu_assign_pointer(tp->root, root_ht);
358 static int u32_destroy_key(struct tcf_proto *tp,
359 struct tc_u_knode *n,
362 tcf_exts_destroy(&n->exts);
364 n->ht_down->refcnt--;
365 #ifdef CONFIG_CLS_U32_PERF
369 #ifdef CONFIG_CLS_U32_MARK
371 free_percpu(n->pcpu_success);
377 /* u32_delete_key_rcu should be called when free'ing a copied
378 * version of a tc_u_knode obtained from u32_init_knode(). When
379 * copies are obtained from u32_init_knode() the statistics are
380 * shared between the old and new copies to allow readers to
381 * continue to update the statistics during the copy. To support
382 * this the u32_delete_key_rcu variant does not free the percpu
385 static void u32_delete_key_rcu(struct rcu_head *rcu)
387 struct tc_u_knode *key = container_of(rcu, struct tc_u_knode, rcu);
389 u32_destroy_key(key->tp, key, false);
392 /* u32_delete_key_freepf_rcu is the rcu callback variant
393 * that free's the entire structure including the statistics
394 * percpu variables. Only use this if the key is not a copy
395 * returned by u32_init_knode(). See u32_delete_key_rcu()
396 * for the variant that should be used with keys return from
399 static void u32_delete_key_freepf_rcu(struct rcu_head *rcu)
401 struct tc_u_knode *key = container_of(rcu, struct tc_u_knode, rcu);
403 u32_destroy_key(key->tp, key, true);
406 static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key)
408 struct tc_u_knode __rcu **kp;
409 struct tc_u_knode *pkp;
410 struct tc_u_hnode *ht = rtnl_dereference(key->ht_up);
413 kp = &ht->ht[TC_U32_HASH(key->handle)];
414 for (pkp = rtnl_dereference(*kp); pkp;
415 kp = &pkp->next, pkp = rtnl_dereference(*kp)) {
417 RCU_INIT_POINTER(*kp, key->next);
419 tcf_unbind_filter(tp, &key->res);
420 call_rcu(&key->rcu, u32_delete_key_freepf_rcu);
429 static void u32_remove_hw_knode(struct tcf_proto *tp, u32 handle)
431 struct net_device *dev = tp->q->dev_queue->dev;
432 struct tc_cls_u32_offload u32_offload = {0};
433 struct tc_to_netdev offload;
435 offload.type = TC_SETUP_CLSU32;
436 offload.cls_u32 = &u32_offload;
438 if (tc_should_offload(dev, 0)) {
439 offload.cls_u32->command = TC_CLSU32_DELETE_KNODE;
440 offload.cls_u32->knode.handle = handle;
441 dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle,
442 tp->protocol, &offload);
446 static void u32_replace_hw_hnode(struct tcf_proto *tp,
447 struct tc_u_hnode *h,
450 struct net_device *dev = tp->q->dev_queue->dev;
451 struct tc_cls_u32_offload u32_offload = {0};
452 struct tc_to_netdev offload;
454 offload.type = TC_SETUP_CLSU32;
455 offload.cls_u32 = &u32_offload;
457 if (tc_should_offload(dev, flags)) {
458 offload.cls_u32->command = TC_CLSU32_NEW_HNODE;
459 offload.cls_u32->hnode.divisor = h->divisor;
460 offload.cls_u32->hnode.handle = h->handle;
461 offload.cls_u32->hnode.prio = h->prio;
463 dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle,
464 tp->protocol, &offload);
468 static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h)
470 struct net_device *dev = tp->q->dev_queue->dev;
471 struct tc_cls_u32_offload u32_offload = {0};
472 struct tc_to_netdev offload;
474 offload.type = TC_SETUP_CLSU32;
475 offload.cls_u32 = &u32_offload;
477 if (tc_should_offload(dev, 0)) {
478 offload.cls_u32->command = TC_CLSU32_DELETE_HNODE;
479 offload.cls_u32->hnode.divisor = h->divisor;
480 offload.cls_u32->hnode.handle = h->handle;
481 offload.cls_u32->hnode.prio = h->prio;
483 dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle,
484 tp->protocol, &offload);
488 static void u32_replace_hw_knode(struct tcf_proto *tp,
489 struct tc_u_knode *n,
492 struct net_device *dev = tp->q->dev_queue->dev;
493 struct tc_cls_u32_offload u32_offload = {0};
494 struct tc_to_netdev offload;
496 offload.type = TC_SETUP_CLSU32;
497 offload.cls_u32 = &u32_offload;
499 if (tc_should_offload(dev, flags)) {
500 offload.cls_u32->command = TC_CLSU32_REPLACE_KNODE;
501 offload.cls_u32->knode.handle = n->handle;
502 offload.cls_u32->knode.fshift = n->fshift;
503 #ifdef CONFIG_CLS_U32_MARK
504 offload.cls_u32->knode.val = n->val;
505 offload.cls_u32->knode.mask = n->mask;
507 offload.cls_u32->knode.val = 0;
508 offload.cls_u32->knode.mask = 0;
510 offload.cls_u32->knode.sel = &n->sel;
511 offload.cls_u32->knode.exts = &n->exts;
513 offload.cls_u32->knode.link_handle = n->ht_down->handle;
515 dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle,
516 tp->protocol, &offload);
520 static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
522 struct tc_u_knode *n;
525 for (h = 0; h <= ht->divisor; h++) {
526 while ((n = rtnl_dereference(ht->ht[h])) != NULL) {
527 RCU_INIT_POINTER(ht->ht[h],
528 rtnl_dereference(n->next));
529 tcf_unbind_filter(tp, &n->res);
530 u32_remove_hw_knode(tp, n->handle);
531 call_rcu(&n->rcu, u32_delete_key_freepf_rcu);
536 static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
538 struct tc_u_common *tp_c = tp->data;
539 struct tc_u_hnode __rcu **hn;
540 struct tc_u_hnode *phn;
544 u32_clear_hnode(tp, ht);
547 for (phn = rtnl_dereference(*hn);
549 hn = &phn->next, phn = rtnl_dereference(*hn)) {
551 u32_clear_hw_hnode(tp, ht);
552 RCU_INIT_POINTER(*hn, ht->next);
561 static bool ht_empty(struct tc_u_hnode *ht)
565 for (h = 0; h <= ht->divisor; h++)
566 if (rcu_access_pointer(ht->ht[h]))
572 static bool u32_destroy(struct tcf_proto *tp, bool force)
574 struct tc_u_common *tp_c = tp->data;
575 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
577 WARN_ON(root_ht == NULL);
581 if (root_ht->refcnt > 1)
583 if (root_ht->refcnt == 1) {
584 if (!ht_empty(root_ht))
589 if (tp_c->refcnt > 1)
592 if (tp_c->refcnt == 1) {
593 struct tc_u_hnode *ht;
595 for (ht = rtnl_dereference(tp_c->hlist);
597 ht = rtnl_dereference(ht->next))
603 if (root_ht && --root_ht->refcnt == 0)
604 u32_destroy_hnode(tp, root_ht);
606 if (--tp_c->refcnt == 0) {
607 struct tc_u_hnode *ht;
609 tp->q->u32_node = NULL;
611 for (ht = rtnl_dereference(tp_c->hlist);
613 ht = rtnl_dereference(ht->next)) {
615 u32_clear_hnode(tp, ht);
618 while ((ht = rtnl_dereference(tp_c->hlist)) != NULL) {
619 RCU_INIT_POINTER(tp_c->hlist, ht->next);
630 static int u32_delete(struct tcf_proto *tp, unsigned long arg)
632 struct tc_u_hnode *ht = (struct tc_u_hnode *)arg;
633 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
638 if (TC_U32_KEY(ht->handle)) {
639 u32_remove_hw_knode(tp, ht->handle);
640 return u32_delete_key(tp, (struct tc_u_knode *)ht);
646 if (ht->refcnt == 1) {
648 u32_destroy_hnode(tp, ht);
656 #define NR_U32_NODE (1<<12)
657 static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
659 struct tc_u_knode *n;
661 unsigned long *bitmap = kzalloc(BITS_TO_LONGS(NR_U32_NODE) * sizeof(unsigned long),
664 return handle | 0xFFF;
666 for (n = rtnl_dereference(ht->ht[TC_U32_HASH(handle)]);
668 n = rtnl_dereference(n->next))
669 set_bit(TC_U32_NODE(n->handle), bitmap);
671 i = find_next_zero_bit(bitmap, NR_U32_NODE, 0x800);
672 if (i >= NR_U32_NODE)
673 i = find_next_zero_bit(bitmap, NR_U32_NODE, 1);
676 return handle | (i >= NR_U32_NODE ? 0xFFF : i);
679 static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
680 [TCA_U32_CLASSID] = { .type = NLA_U32 },
681 [TCA_U32_HASH] = { .type = NLA_U32 },
682 [TCA_U32_LINK] = { .type = NLA_U32 },
683 [TCA_U32_DIVISOR] = { .type = NLA_U32 },
684 [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) },
685 [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
686 [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) },
687 [TCA_U32_FLAGS] = { .type = NLA_U32 },
690 static int u32_set_parms(struct net *net, struct tcf_proto *tp,
691 unsigned long base, struct tc_u_hnode *ht,
692 struct tc_u_knode *n, struct nlattr **tb,
693 struct nlattr *est, bool ovr)
698 tcf_exts_init(&e, TCA_U32_ACT, TCA_U32_POLICE);
699 err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
704 if (tb[TCA_U32_LINK]) {
705 u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
706 struct tc_u_hnode *ht_down = NULL, *ht_old;
708 if (TC_U32_KEY(handle))
712 ht_down = u32_lookup_ht(ht->tp_c, handle);
719 ht_old = rtnl_dereference(n->ht_down);
720 rcu_assign_pointer(n->ht_down, ht_down);
725 if (tb[TCA_U32_CLASSID]) {
726 n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
727 tcf_bind_filter(tp, &n->res, base);
730 #ifdef CONFIG_NET_CLS_IND
731 if (tb[TCA_U32_INDEV]) {
733 ret = tcf_change_indev(net, tb[TCA_U32_INDEV]);
739 tcf_exts_change(tp, &n->exts, &e);
743 tcf_exts_destroy(&e);
747 static void u32_replace_knode(struct tcf_proto *tp,
748 struct tc_u_common *tp_c,
749 struct tc_u_knode *n)
751 struct tc_u_knode __rcu **ins;
752 struct tc_u_knode *pins;
753 struct tc_u_hnode *ht;
755 if (TC_U32_HTID(n->handle) == TC_U32_ROOT)
756 ht = rtnl_dereference(tp->root);
758 ht = u32_lookup_ht(tp_c, TC_U32_HTID(n->handle));
760 ins = &ht->ht[TC_U32_HASH(n->handle)];
762 /* The node must always exist for it to be replaced if this is not the
763 * case then something went very wrong elsewhere.
765 for (pins = rtnl_dereference(*ins); ;
766 ins = &pins->next, pins = rtnl_dereference(*ins))
767 if (pins->handle == n->handle)
770 RCU_INIT_POINTER(n->next, pins->next);
771 rcu_assign_pointer(*ins, n);
774 static struct tc_u_knode *u32_init_knode(struct tcf_proto *tp,
775 struct tc_u_knode *n)
777 struct tc_u_knode *new;
778 struct tc_u32_sel *s = &n->sel;
780 new = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key),
786 RCU_INIT_POINTER(new->next, n->next);
787 new->handle = n->handle;
788 RCU_INIT_POINTER(new->ht_up, n->ht_up);
790 #ifdef CONFIG_NET_CLS_IND
791 new->ifindex = n->ifindex;
793 new->fshift = n->fshift;
795 new->flags = n->flags;
796 RCU_INIT_POINTER(new->ht_down, n->ht_down);
798 /* bump reference count as long as we hold pointer to structure */
800 new->ht_down->refcnt++;
802 #ifdef CONFIG_CLS_U32_PERF
803 /* Statistics may be incremented by readers during update
804 * so we must keep them in tact. When the node is later destroyed
805 * a special destroy call must be made to not free the pf memory.
810 #ifdef CONFIG_CLS_U32_MARK
813 /* Similarly success statistics must be moved as pointers */
814 new->pcpu_success = n->pcpu_success;
817 memcpy(&new->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
819 tcf_exts_init(&new->exts, TCA_U32_ACT, TCA_U32_POLICE);
824 static int u32_change(struct net *net, struct sk_buff *in_skb,
825 struct tcf_proto *tp, unsigned long base, u32 handle,
827 unsigned long *arg, bool ovr)
829 struct tc_u_common *tp_c = tp->data;
830 struct tc_u_hnode *ht;
831 struct tc_u_knode *n;
832 struct tc_u32_sel *s;
833 struct nlattr *opt = tca[TCA_OPTIONS];
834 struct nlattr *tb[TCA_U32_MAX + 1];
837 #ifdef CONFIG_CLS_U32_PERF
842 return handle ? -EINVAL : 0;
844 err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy);
848 if (tb[TCA_U32_FLAGS])
849 flags = nla_get_u32(tb[TCA_U32_FLAGS]);
851 n = (struct tc_u_knode *)*arg;
853 struct tc_u_knode *new;
855 if (TC_U32_KEY(n->handle) == 0)
858 if (n->flags != flags)
861 new = u32_init_knode(tp, n);
865 err = u32_set_parms(net, tp, base,
866 rtnl_dereference(n->ht_up), new, tb,
870 u32_destroy_key(tp, new, false);
874 u32_replace_knode(tp, tp_c, new);
875 tcf_unbind_filter(tp, &n->res);
876 call_rcu(&n->rcu, u32_delete_key_rcu);
877 u32_replace_hw_knode(tp, new, flags);
881 if (tb[TCA_U32_DIVISOR]) {
882 unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
884 if (--divisor > 0x100)
886 if (TC_U32_KEY(handle))
889 handle = gen_new_htid(tp->data);
893 ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL);
898 ht->divisor = divisor;
901 RCU_INIT_POINTER(ht->next, tp_c->hlist);
902 rcu_assign_pointer(tp_c->hlist, ht);
903 *arg = (unsigned long)ht;
905 u32_replace_hw_hnode(tp, ht, flags);
909 if (tb[TCA_U32_HASH]) {
910 htid = nla_get_u32(tb[TCA_U32_HASH]);
911 if (TC_U32_HTID(htid) == TC_U32_ROOT) {
912 ht = rtnl_dereference(tp->root);
915 ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
920 ht = rtnl_dereference(tp->root);
924 if (ht->divisor < TC_U32_HASH(htid))
928 if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
930 handle = htid | TC_U32_NODE(handle);
932 handle = gen_new_kid(ht, htid);
934 if (tb[TCA_U32_SEL] == NULL)
937 s = nla_data(tb[TCA_U32_SEL]);
939 n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
943 #ifdef CONFIG_CLS_U32_PERF
944 size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64);
945 n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt));
952 memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
953 RCU_INIT_POINTER(n->ht_up, ht);
955 n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
957 tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE);
960 #ifdef CONFIG_CLS_U32_MARK
961 n->pcpu_success = alloc_percpu(u32);
962 if (!n->pcpu_success) {
967 if (tb[TCA_U32_MARK]) {
968 struct tc_u32_mark *mark;
970 mark = nla_data(tb[TCA_U32_MARK]);
972 n->mask = mark->mask;
976 err = u32_set_parms(net, tp, base, ht, n, tb, tca[TCA_RATE], ovr);
978 struct tc_u_knode __rcu **ins;
979 struct tc_u_knode *pins;
981 ins = &ht->ht[TC_U32_HASH(handle)];
982 for (pins = rtnl_dereference(*ins); pins;
983 ins = &pins->next, pins = rtnl_dereference(*ins))
984 if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle))
987 RCU_INIT_POINTER(n->next, pins);
988 rcu_assign_pointer(*ins, n);
989 u32_replace_hw_knode(tp, n, flags);
990 *arg = (unsigned long)n;
994 #ifdef CONFIG_CLS_U32_MARK
995 free_percpu(n->pcpu_success);
999 #ifdef CONFIG_CLS_U32_PERF
1006 static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1008 struct tc_u_common *tp_c = tp->data;
1009 struct tc_u_hnode *ht;
1010 struct tc_u_knode *n;
1016 for (ht = rtnl_dereference(tp_c->hlist);
1018 ht = rtnl_dereference(ht->next)) {
1019 if (ht->prio != tp->prio)
1021 if (arg->count >= arg->skip) {
1022 if (arg->fn(tp, (unsigned long)ht, arg) < 0) {
1028 for (h = 0; h <= ht->divisor; h++) {
1029 for (n = rtnl_dereference(ht->ht[h]);
1031 n = rtnl_dereference(n->next)) {
1032 if (arg->count < arg->skip) {
1036 if (arg->fn(tp, (unsigned long)n, arg) < 0) {
1046 static int u32_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
1047 struct sk_buff *skb, struct tcmsg *t)
1049 struct tc_u_knode *n = (struct tc_u_knode *)fh;
1050 struct tc_u_hnode *ht_up, *ht_down;
1051 struct nlattr *nest;
1056 t->tcm_handle = n->handle;
1058 nest = nla_nest_start(skb, TCA_OPTIONS);
1060 goto nla_put_failure;
1062 if (TC_U32_KEY(n->handle) == 0) {
1063 struct tc_u_hnode *ht = (struct tc_u_hnode *)fh;
1064 u32 divisor = ht->divisor + 1;
1066 if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
1067 goto nla_put_failure;
1069 #ifdef CONFIG_CLS_U32_PERF
1070 struct tc_u32_pcnt *gpf;
1074 if (nla_put(skb, TCA_U32_SEL,
1075 sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
1077 goto nla_put_failure;
1079 ht_up = rtnl_dereference(n->ht_up);
1081 u32 htid = n->handle & 0xFFFFF000;
1082 if (nla_put_u32(skb, TCA_U32_HASH, htid))
1083 goto nla_put_failure;
1085 if (n->res.classid &&
1086 nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid))
1087 goto nla_put_failure;
1089 ht_down = rtnl_dereference(n->ht_down);
1091 nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))
1092 goto nla_put_failure;
1094 if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags))
1095 goto nla_put_failure;
1097 #ifdef CONFIG_CLS_U32_MARK
1098 if ((n->val || n->mask)) {
1099 struct tc_u32_mark mark = {.val = n->val,
1104 for_each_possible_cpu(cpum) {
1105 __u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum);
1107 mark.success += cnt;
1110 if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark))
1111 goto nla_put_failure;
1115 if (tcf_exts_dump(skb, &n->exts) < 0)
1116 goto nla_put_failure;
1118 #ifdef CONFIG_NET_CLS_IND
1120 struct net_device *dev;
1121 dev = __dev_get_by_index(net, n->ifindex);
1122 if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
1123 goto nla_put_failure;
1126 #ifdef CONFIG_CLS_U32_PERF
1127 gpf = kzalloc(sizeof(struct tc_u32_pcnt) +
1128 n->sel.nkeys * sizeof(u64),
1131 goto nla_put_failure;
1133 for_each_possible_cpu(cpu) {
1135 struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu);
1137 gpf->rcnt += pf->rcnt;
1138 gpf->rhit += pf->rhit;
1139 for (i = 0; i < n->sel.nkeys; i++)
1140 gpf->kcnts[i] += pf->kcnts[i];
1143 if (nla_put(skb, TCA_U32_PCNT,
1144 sizeof(struct tc_u32_pcnt) + n->sel.nkeys*sizeof(u64),
1147 goto nla_put_failure;
1153 nla_nest_end(skb, nest);
1155 if (TC_U32_KEY(n->handle))
1156 if (tcf_exts_dump_stats(skb, &n->exts) < 0)
1157 goto nla_put_failure;
1161 nla_nest_cancel(skb, nest);
1165 static struct tcf_proto_ops cls_u32_ops __read_mostly = {
1167 .classify = u32_classify,
1169 .destroy = u32_destroy,
1171 .change = u32_change,
1172 .delete = u32_delete,
1175 .owner = THIS_MODULE,
1178 static int __init init_u32(void)
1180 pr_info("u32 classifier\n");
1181 #ifdef CONFIG_CLS_U32_PERF
1182 pr_info(" Performance counters on\n");
1184 #ifdef CONFIG_NET_CLS_IND
1185 pr_info(" input device check on\n");
1187 #ifdef CONFIG_NET_CLS_ACT
1188 pr_info(" Actions configured\n");
1190 return register_tcf_proto_ops(&cls_u32_ops);
1193 static void __exit exit_u32(void)
1195 unregister_tcf_proto_ops(&cls_u32_ops);
1198 module_init(init_u32)
1199 module_exit(exit_u32)
1200 MODULE_LICENSE("GPL");