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 <linux/netdevice.h>
44 #include <linux/hash.h>
45 #include <net/netlink.h>
46 #include <net/act_api.h>
47 #include <net/pkt_cls.h>
48 #include <linux/netdevice.h>
51 struct tc_u_knode __rcu *next;
53 struct tc_u_hnode __rcu *ht_up;
55 #ifdef CONFIG_NET_CLS_IND
59 struct tcf_result res;
60 struct tc_u_hnode __rcu *ht_down;
61 #ifdef CONFIG_CLS_U32_PERF
62 struct tc_u32_pcnt __percpu *pf;
65 #ifdef CONFIG_CLS_U32_MARK
68 u32 __percpu *pcpu_success;
72 /* The 'sel' field MUST be the last field in structure to allow for
73 * tc_u32_keys allocated at end of structure.
75 struct tc_u32_sel sel;
79 struct tc_u_hnode __rcu *next;
82 struct tc_u_common *tp_c;
86 /* The 'ht' field MUST be the last field in structure to allow for
87 * more entries allocated at end of structure.
89 struct tc_u_knode __rcu *ht[1];
93 struct tc_u_hnode __rcu *hlist;
97 struct hlist_node hnode;
101 static inline unsigned int u32_hash_fold(__be32 key,
102 const struct tc_u32_sel *sel,
105 unsigned int h = ntohl(key & sel->hmask) >> fshift;
110 static int u32_classify(struct sk_buff *skb, const struct tcf_proto *tp,
111 struct tcf_result *res)
114 struct tc_u_knode *knode;
116 } stack[TC_U32_MAXDEPTH];
118 struct tc_u_hnode *ht = rcu_dereference_bh(tp->root);
119 unsigned int off = skb_network_offset(skb);
120 struct tc_u_knode *n;
124 #ifdef CONFIG_CLS_U32_PERF
130 n = rcu_dereference_bh(ht->ht[sel]);
134 struct tc_u32_key *key = n->sel.keys;
136 #ifdef CONFIG_CLS_U32_PERF
137 __this_cpu_inc(n->pf->rcnt);
141 if (tc_skip_sw(n->flags)) {
142 n = rcu_dereference_bh(n->next);
146 #ifdef CONFIG_CLS_U32_MARK
147 if ((skb->mark & n->mask) != n->val) {
148 n = rcu_dereference_bh(n->next);
151 __this_cpu_inc(*n->pcpu_success);
155 for (i = n->sel.nkeys; i > 0; i--, key++) {
156 int toff = off + key->off + (off2 & key->offmask);
159 if (skb_headroom(skb) + toff > INT_MAX)
162 data = skb_header_pointer(skb, toff, 4, &hdata);
165 if ((*data ^ key->val) & key->mask) {
166 n = rcu_dereference_bh(n->next);
169 #ifdef CONFIG_CLS_U32_PERF
170 __this_cpu_inc(n->pf->kcnts[j]);
175 ht = rcu_dereference_bh(n->ht_down);
178 if (n->sel.flags & TC_U32_TERMINAL) {
181 #ifdef CONFIG_NET_CLS_IND
182 if (!tcf_match_indev(skb, n->ifindex)) {
183 n = rcu_dereference_bh(n->next);
187 #ifdef CONFIG_CLS_U32_PERF
188 __this_cpu_inc(n->pf->rhit);
190 r = tcf_exts_exec(skb, &n->exts, res);
192 n = rcu_dereference_bh(n->next);
198 n = rcu_dereference_bh(n->next);
203 if (sdepth >= TC_U32_MAXDEPTH)
205 stack[sdepth].knode = n;
206 stack[sdepth].off = off;
209 ht = rcu_dereference_bh(n->ht_down);
214 data = skb_header_pointer(skb, off + n->sel.hoff, 4,
218 sel = ht->divisor & u32_hash_fold(*data, &n->sel,
221 if (!(n->sel.flags & (TC_U32_VAROFFSET | TC_U32_OFFSET | TC_U32_EAT)))
224 if (n->sel.flags & (TC_U32_OFFSET | TC_U32_VAROFFSET)) {
225 off2 = n->sel.off + 3;
226 if (n->sel.flags & TC_U32_VAROFFSET) {
229 data = skb_header_pointer(skb,
234 off2 += ntohs(n->sel.offmask & *data) >>
239 if (n->sel.flags & TC_U32_EAT) {
250 n = stack[sdepth].knode;
251 ht = rcu_dereference_bh(n->ht_up);
252 off = stack[sdepth].off;
259 net_warn_ratelimited("cls_u32: dead loop\n");
263 static struct tc_u_hnode *u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
265 struct tc_u_hnode *ht;
267 for (ht = rtnl_dereference(tp_c->hlist);
269 ht = rtnl_dereference(ht->next))
270 if (ht->handle == handle)
276 static struct tc_u_knode *u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
279 struct tc_u_knode *n = NULL;
281 sel = TC_U32_HASH(handle);
282 if (sel > ht->divisor)
285 for (n = rtnl_dereference(ht->ht[sel]);
287 n = rtnl_dereference(n->next))
288 if (n->handle == handle)
295 static void *u32_get(struct tcf_proto *tp, u32 handle)
297 struct tc_u_hnode *ht;
298 struct tc_u_common *tp_c = tp->data;
300 if (TC_U32_HTID(handle) == TC_U32_ROOT)
301 ht = rtnl_dereference(tp->root);
303 ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
308 if (TC_U32_KEY(handle) == 0)
311 return u32_lookup_key(ht, handle);
314 static u32 gen_new_htid(struct tc_u_common *tp_c)
318 /* hgenerator only used inside rtnl lock it is safe to increment
319 * without read _copy_ update semantics
322 if (++tp_c->hgenerator == 0x7FF)
323 tp_c->hgenerator = 1;
324 } while (--i > 0 && u32_lookup_ht(tp_c, (tp_c->hgenerator|0x800)<<20));
326 return i > 0 ? (tp_c->hgenerator|0x800)<<20 : 0;
329 static struct hlist_head *tc_u_common_hash;
331 #define U32_HASH_SHIFT 10
332 #define U32_HASH_SIZE (1 << U32_HASH_SHIFT)
334 static unsigned int tc_u_hash(const struct tcf_proto *tp)
336 struct net_device *dev = tp->q->dev_queue->dev;
337 u32 qhandle = tp->q->handle;
338 int ifindex = dev->ifindex;
340 return hash_64((u64)ifindex << 32 | qhandle, U32_HASH_SHIFT);
343 static struct tc_u_common *tc_u_common_find(const struct tcf_proto *tp)
345 struct tc_u_common *tc;
349 hlist_for_each_entry(tc, &tc_u_common_hash[h], hnode) {
356 static int u32_init(struct tcf_proto *tp)
358 struct tc_u_hnode *root_ht;
359 struct tc_u_common *tp_c;
362 tp_c = tc_u_common_find(tp);
364 root_ht = kzalloc(sizeof(*root_ht), GFP_KERNEL);
369 root_ht->handle = tp_c ? gen_new_htid(tp_c) : 0x80000000;
370 root_ht->prio = tp->prio;
373 tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL);
379 INIT_HLIST_NODE(&tp_c->hnode);
382 hlist_add_head(&tp_c->hnode, &tc_u_common_hash[h]);
386 RCU_INIT_POINTER(root_ht->next, tp_c->hlist);
387 rcu_assign_pointer(tp_c->hlist, root_ht);
388 root_ht->tp_c = tp_c;
390 rcu_assign_pointer(tp->root, root_ht);
395 static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n,
398 tcf_exts_destroy(&n->exts);
400 n->ht_down->refcnt--;
401 #ifdef CONFIG_CLS_U32_PERF
405 #ifdef CONFIG_CLS_U32_MARK
407 free_percpu(n->pcpu_success);
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
421 static void u32_delete_key_rcu(struct rcu_head *rcu)
423 struct tc_u_knode *key = container_of(rcu, struct tc_u_knode, rcu);
425 u32_destroy_key(key->tp, key, false);
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
435 static void u32_delete_key_freepf_rcu(struct rcu_head *rcu)
437 struct tc_u_knode *key = container_of(rcu, struct tc_u_knode, rcu);
439 u32_destroy_key(key->tp, key, true);
442 static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key)
444 struct tc_u_knode __rcu **kp;
445 struct tc_u_knode *pkp;
446 struct tc_u_hnode *ht = rtnl_dereference(key->ht_up);
449 kp = &ht->ht[TC_U32_HASH(key->handle)];
450 for (pkp = rtnl_dereference(*kp); pkp;
451 kp = &pkp->next, pkp = rtnl_dereference(*kp)) {
453 RCU_INIT_POINTER(*kp, key->next);
455 tcf_unbind_filter(tp, &key->res);
456 call_rcu(&key->rcu, u32_delete_key_freepf_rcu);
465 static void u32_remove_hw_knode(struct tcf_proto *tp, u32 handle)
467 struct net_device *dev = tp->q->dev_queue->dev;
468 struct tc_cls_u32_offload cls_u32 = {};
470 if (!tc_should_offload(dev, 0))
473 tc_cls_common_offload_init(&cls_u32.common, tp);
474 cls_u32.command = TC_CLSU32_DELETE_KNODE;
475 cls_u32.knode.handle = handle;
477 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSU32, &cls_u32);
480 static int u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
483 struct net_device *dev = tp->q->dev_queue->dev;
484 struct tc_cls_u32_offload cls_u32 = {};
487 if (!tc_should_offload(dev, flags))
488 return tc_skip_sw(flags) ? -EINVAL : 0;
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;
496 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSU32, &cls_u32);
497 if (tc_skip_sw(flags))
503 static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h)
505 struct net_device *dev = tp->q->dev_queue->dev;
506 struct tc_cls_u32_offload cls_u32 = {};
508 if (!tc_should_offload(dev, 0))
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;
517 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSU32, &cls_u32);
520 static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
523 struct net_device *dev = tp->q->dev_queue->dev;
524 struct tc_cls_u32_offload cls_u32 = {};
527 if (!tc_should_offload(dev, flags))
528 return tc_skip_sw(flags) ? -EINVAL : 0;
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;
534 #ifdef CONFIG_CLS_U32_MARK
535 cls_u32.knode.val = n->val;
536 cls_u32.knode.mask = n->mask;
538 cls_u32.knode.val = 0;
539 cls_u32.knode.mask = 0;
541 cls_u32.knode.sel = &n->sel;
542 cls_u32.knode.exts = &n->exts;
544 cls_u32.knode.link_handle = n->ht_down->handle;
546 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSU32, &cls_u32);
549 n->flags |= TCA_CLS_FLAGS_IN_HW;
551 if (tc_skip_sw(flags))
557 static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
559 struct tc_u_knode *n;
562 for (h = 0; h <= ht->divisor; h++) {
563 while ((n = rtnl_dereference(ht->ht[h])) != NULL) {
564 RCU_INIT_POINTER(ht->ht[h],
565 rtnl_dereference(n->next));
566 tcf_unbind_filter(tp, &n->res);
567 u32_remove_hw_knode(tp, n->handle);
568 call_rcu(&n->rcu, u32_delete_key_freepf_rcu);
573 static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
575 struct tc_u_common *tp_c = tp->data;
576 struct tc_u_hnode __rcu **hn;
577 struct tc_u_hnode *phn;
581 u32_clear_hnode(tp, ht);
584 for (phn = rtnl_dereference(*hn);
586 hn = &phn->next, phn = rtnl_dereference(*hn)) {
588 u32_clear_hw_hnode(tp, ht);
589 RCU_INIT_POINTER(*hn, ht->next);
598 static bool ht_empty(struct tc_u_hnode *ht)
602 for (h = 0; h <= ht->divisor; h++)
603 if (rcu_access_pointer(ht->ht[h]))
609 static void u32_destroy(struct tcf_proto *tp)
611 struct tc_u_common *tp_c = tp->data;
612 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
614 WARN_ON(root_ht == NULL);
616 if (root_ht && --root_ht->refcnt == 0)
617 u32_destroy_hnode(tp, root_ht);
619 if (--tp_c->refcnt == 0) {
620 struct tc_u_hnode *ht;
622 hlist_del(&tp_c->hnode);
624 for (ht = rtnl_dereference(tp_c->hlist);
626 ht = rtnl_dereference(ht->next)) {
628 u32_clear_hnode(tp, ht);
631 while ((ht = rtnl_dereference(tp_c->hlist)) != NULL) {
632 RCU_INIT_POINTER(tp_c->hlist, ht->next);
642 static int u32_delete(struct tcf_proto *tp, void *arg, bool *last)
644 struct tc_u_hnode *ht = arg;
645 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
646 struct tc_u_common *tp_c = tp->data;
652 if (TC_U32_KEY(ht->handle)) {
653 u32_remove_hw_knode(tp, ht->handle);
654 ret = u32_delete_key(tp, (struct tc_u_knode *)ht);
661 if (ht->refcnt == 1) {
663 u32_destroy_hnode(tp, ht);
671 if (root_ht->refcnt > 1) {
675 if (root_ht->refcnt == 1) {
676 if (!ht_empty(root_ht)) {
683 if (tp_c->refcnt > 1) {
688 if (tp_c->refcnt == 1) {
689 struct tc_u_hnode *ht;
691 for (ht = rtnl_dereference(tp_c->hlist);
693 ht = rtnl_dereference(ht->next))
704 #define NR_U32_NODE (1<<12)
705 static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
707 struct tc_u_knode *n;
709 unsigned long *bitmap = kzalloc(BITS_TO_LONGS(NR_U32_NODE) * sizeof(unsigned long),
712 return handle | 0xFFF;
714 for (n = rtnl_dereference(ht->ht[TC_U32_HASH(handle)]);
716 n = rtnl_dereference(n->next))
717 set_bit(TC_U32_NODE(n->handle), bitmap);
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);
724 return handle | (i >= NR_U32_NODE ? 0xFFF : i);
727 static 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) },
735 [TCA_U32_FLAGS] = { .type = NLA_U32 },
738 static int u32_set_parms(struct net *net, struct tcf_proto *tp,
739 unsigned long base, struct tc_u_hnode *ht,
740 struct tc_u_knode *n, struct nlattr **tb,
741 struct nlattr *est, bool ovr)
745 err = tcf_exts_validate(net, tp, tb, est, &n->exts, ovr);
749 if (tb[TCA_U32_LINK]) {
750 u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
751 struct tc_u_hnode *ht_down = NULL, *ht_old;
753 if (TC_U32_KEY(handle))
757 ht_down = u32_lookup_ht(ht->tp_c, handle);
764 ht_old = rtnl_dereference(n->ht_down);
765 rcu_assign_pointer(n->ht_down, ht_down);
770 if (tb[TCA_U32_CLASSID]) {
771 n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
772 tcf_bind_filter(tp, &n->res, base);
775 #ifdef CONFIG_NET_CLS_IND
776 if (tb[TCA_U32_INDEV]) {
778 ret = tcf_change_indev(net, tb[TCA_U32_INDEV]);
787 static void u32_replace_knode(struct tcf_proto *tp, struct tc_u_common *tp_c,
788 struct tc_u_knode *n)
790 struct tc_u_knode __rcu **ins;
791 struct tc_u_knode *pins;
792 struct tc_u_hnode *ht;
794 if (TC_U32_HTID(n->handle) == TC_U32_ROOT)
795 ht = rtnl_dereference(tp->root);
797 ht = u32_lookup_ht(tp_c, TC_U32_HTID(n->handle));
799 ins = &ht->ht[TC_U32_HASH(n->handle)];
801 /* The node must always exist for it to be replaced if this is not the
802 * case then something went very wrong elsewhere.
804 for (pins = rtnl_dereference(*ins); ;
805 ins = &pins->next, pins = rtnl_dereference(*ins))
806 if (pins->handle == n->handle)
809 RCU_INIT_POINTER(n->next, pins->next);
810 rcu_assign_pointer(*ins, n);
813 static struct tc_u_knode *u32_init_knode(struct tcf_proto *tp,
814 struct tc_u_knode *n)
816 struct tc_u_knode *new;
817 struct tc_u32_sel *s = &n->sel;
819 new = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key),
825 RCU_INIT_POINTER(new->next, n->next);
826 new->handle = n->handle;
827 RCU_INIT_POINTER(new->ht_up, n->ht_up);
829 #ifdef CONFIG_NET_CLS_IND
830 new->ifindex = n->ifindex;
832 new->fshift = n->fshift;
834 new->flags = n->flags;
835 RCU_INIT_POINTER(new->ht_down, n->ht_down);
837 /* bump reference count as long as we hold pointer to structure */
839 new->ht_down->refcnt++;
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.
849 #ifdef CONFIG_CLS_U32_MARK
852 /* Similarly success statistics must be moved as pointers */
853 new->pcpu_success = n->pcpu_success;
856 memcpy(&new->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
858 if (tcf_exts_init(&new->exts, TCA_U32_ACT, TCA_U32_POLICE)) {
866 static int u32_change(struct net *net, struct sk_buff *in_skb,
867 struct tcf_proto *tp, unsigned long base, u32 handle,
868 struct nlattr **tca, void **arg, bool ovr)
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;
874 struct nlattr *opt = tca[TCA_OPTIONS];
875 struct nlattr *tb[TCA_U32_MAX + 1];
878 #ifdef CONFIG_CLS_U32_PERF
883 return handle ? -EINVAL : 0;
885 err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy, NULL);
889 if (tb[TCA_U32_FLAGS]) {
890 flags = nla_get_u32(tb[TCA_U32_FLAGS]);
891 if (!tc_flags_valid(flags))
897 struct tc_u_knode *new;
899 if (TC_U32_KEY(n->handle) == 0)
902 if (n->flags != flags)
905 new = u32_init_knode(tp, n);
909 err = u32_set_parms(net, tp, base,
910 rtnl_dereference(n->ht_up), new, tb,
914 u32_destroy_key(tp, new, false);
918 err = u32_replace_hw_knode(tp, new, flags);
920 u32_destroy_key(tp, new, false);
924 if (!tc_in_hw(new->flags))
925 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
927 u32_replace_knode(tp, tp_c, new);
928 tcf_unbind_filter(tp, &n->res);
929 call_rcu(&n->rcu, u32_delete_key_rcu);
933 if (tb[TCA_U32_DIVISOR]) {
934 unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
936 if (--divisor > 0x100)
938 if (TC_U32_KEY(handle))
941 handle = gen_new_htid(tp->data);
945 ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL);
950 ht->divisor = divisor;
954 err = u32_replace_hw_hnode(tp, ht, flags);
960 RCU_INIT_POINTER(ht->next, tp_c->hlist);
961 rcu_assign_pointer(tp_c->hlist, ht);
967 if (tb[TCA_U32_HASH]) {
968 htid = nla_get_u32(tb[TCA_U32_HASH]);
969 if (TC_U32_HTID(htid) == TC_U32_ROOT) {
970 ht = rtnl_dereference(tp->root);
973 ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
978 ht = rtnl_dereference(tp->root);
982 if (ht->divisor < TC_U32_HASH(htid))
986 if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
988 handle = htid | TC_U32_NODE(handle);
990 handle = gen_new_kid(ht, htid);
992 if (tb[TCA_U32_SEL] == NULL)
995 s = nla_data(tb[TCA_U32_SEL]);
997 n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
1001 #ifdef CONFIG_CLS_U32_PERF
1002 size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64);
1003 n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt));
1010 memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
1011 RCU_INIT_POINTER(n->ht_up, ht);
1013 n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
1017 err = tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE);
1021 #ifdef CONFIG_CLS_U32_MARK
1022 n->pcpu_success = alloc_percpu(u32);
1023 if (!n->pcpu_success) {
1028 if (tb[TCA_U32_MARK]) {
1029 struct tc_u32_mark *mark;
1031 mark = nla_data(tb[TCA_U32_MARK]);
1033 n->mask = mark->mask;
1037 err = u32_set_parms(net, tp, base, ht, n, tb, tca[TCA_RATE], ovr);
1039 struct tc_u_knode __rcu **ins;
1040 struct tc_u_knode *pins;
1042 err = u32_replace_hw_knode(tp, n, flags);
1046 if (!tc_in_hw(n->flags))
1047 n->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
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))
1055 RCU_INIT_POINTER(n->next, pins);
1056 rcu_assign_pointer(*ins, n);
1062 #ifdef CONFIG_CLS_U32_MARK
1063 free_percpu(n->pcpu_success);
1067 tcf_exts_destroy(&n->exts);
1068 #ifdef CONFIG_CLS_U32_PERF
1075 static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1077 struct tc_u_common *tp_c = tp->data;
1078 struct tc_u_hnode *ht;
1079 struct tc_u_knode *n;
1085 for (ht = rtnl_dereference(tp_c->hlist);
1087 ht = rtnl_dereference(ht->next)) {
1088 if (ht->prio != tp->prio)
1090 if (arg->count >= arg->skip) {
1091 if (arg->fn(tp, ht, arg) < 0) {
1097 for (h = 0; h <= ht->divisor; h++) {
1098 for (n = rtnl_dereference(ht->ht[h]);
1100 n = rtnl_dereference(n->next)) {
1101 if (arg->count < arg->skip) {
1105 if (arg->fn(tp, n, arg) < 0) {
1115 static void u32_bind_class(void *fh, u32 classid, unsigned long cl)
1117 struct tc_u_knode *n = fh;
1119 if (n && n->res.classid == classid)
1123 static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
1124 struct sk_buff *skb, struct tcmsg *t)
1126 struct tc_u_knode *n = fh;
1127 struct tc_u_hnode *ht_up, *ht_down;
1128 struct nlattr *nest;
1133 t->tcm_handle = n->handle;
1135 nest = nla_nest_start(skb, TCA_OPTIONS);
1137 goto nla_put_failure;
1139 if (TC_U32_KEY(n->handle) == 0) {
1140 struct tc_u_hnode *ht = fh;
1141 u32 divisor = ht->divisor + 1;
1143 if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
1144 goto nla_put_failure;
1146 #ifdef CONFIG_CLS_U32_PERF
1147 struct tc_u32_pcnt *gpf;
1151 if (nla_put(skb, TCA_U32_SEL,
1152 sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
1154 goto nla_put_failure;
1156 ht_up = rtnl_dereference(n->ht_up);
1158 u32 htid = n->handle & 0xFFFFF000;
1159 if (nla_put_u32(skb, TCA_U32_HASH, htid))
1160 goto nla_put_failure;
1162 if (n->res.classid &&
1163 nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid))
1164 goto nla_put_failure;
1166 ht_down = rtnl_dereference(n->ht_down);
1168 nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))
1169 goto nla_put_failure;
1171 if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags))
1172 goto nla_put_failure;
1174 #ifdef CONFIG_CLS_U32_MARK
1175 if ((n->val || n->mask)) {
1176 struct tc_u32_mark mark = {.val = n->val,
1181 for_each_possible_cpu(cpum) {
1182 __u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum);
1184 mark.success += cnt;
1187 if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark))
1188 goto nla_put_failure;
1192 if (tcf_exts_dump(skb, &n->exts) < 0)
1193 goto nla_put_failure;
1195 #ifdef CONFIG_NET_CLS_IND
1197 struct net_device *dev;
1198 dev = __dev_get_by_index(net, n->ifindex);
1199 if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
1200 goto nla_put_failure;
1203 #ifdef CONFIG_CLS_U32_PERF
1204 gpf = kzalloc(sizeof(struct tc_u32_pcnt) +
1205 n->sel.nkeys * sizeof(u64),
1208 goto nla_put_failure;
1210 for_each_possible_cpu(cpu) {
1212 struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu);
1214 gpf->rcnt += pf->rcnt;
1215 gpf->rhit += pf->rhit;
1216 for (i = 0; i < n->sel.nkeys; i++)
1217 gpf->kcnts[i] += pf->kcnts[i];
1220 if (nla_put_64bit(skb, TCA_U32_PCNT,
1221 sizeof(struct tc_u32_pcnt) +
1222 n->sel.nkeys * sizeof(u64),
1223 gpf, TCA_U32_PAD)) {
1225 goto nla_put_failure;
1231 nla_nest_end(skb, nest);
1233 if (TC_U32_KEY(n->handle))
1234 if (tcf_exts_dump_stats(skb, &n->exts) < 0)
1235 goto nla_put_failure;
1239 nla_nest_cancel(skb, nest);
1243 static struct tcf_proto_ops cls_u32_ops __read_mostly = {
1245 .classify = u32_classify,
1247 .destroy = u32_destroy,
1249 .change = u32_change,
1250 .delete = u32_delete,
1253 .bind_class = u32_bind_class,
1254 .owner = THIS_MODULE,
1257 static int __init init_u32(void)
1261 pr_info("u32 classifier\n");
1262 #ifdef CONFIG_CLS_U32_PERF
1263 pr_info(" Performance counters on\n");
1265 #ifdef CONFIG_NET_CLS_IND
1266 pr_info(" input device check on\n");
1268 #ifdef CONFIG_NET_CLS_ACT
1269 pr_info(" Actions configured\n");
1271 tc_u_common_hash = kvmalloc_array(U32_HASH_SIZE,
1272 sizeof(struct hlist_head),
1274 if (!tc_u_common_hash)
1277 for (i = 0; i < U32_HASH_SIZE; i++)
1278 INIT_HLIST_HEAD(&tc_u_common_hash[i]);
1280 ret = register_tcf_proto_ops(&cls_u32_ops);
1282 kvfree(tc_u_common_hash);
1286 static void __exit exit_u32(void)
1288 unregister_tcf_proto_ops(&cls_u32_ops);
1289 kvfree(tc_u_common_hash);
1292 module_init(init_u32)
1293 module_exit(exit_u32)
1294 MODULE_LICENSE("GPL");