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/rtnetlink.h>
40 #include <linux/skbuff.h>
41 #include <linux/bitmap.h>
42 #include <net/netlink.h>
43 #include <net/act_api.h>
44 #include <net/pkt_cls.h>
47 struct tc_u_knode
*next
;
49 struct tc_u_hnode
*ht_up
;
51 #ifdef CONFIG_NET_CLS_IND
55 struct tcf_result res
;
56 struct tc_u_hnode
*ht_down
;
57 #ifdef CONFIG_CLS_U32_PERF
58 struct tc_u32_pcnt __percpu
*pf
;
60 #ifdef CONFIG_CLS_U32_MARK
63 u32 __percpu
*pcpu_success
;
65 struct tc_u32_sel sel
;
69 struct tc_u_hnode
*next
;
72 struct tc_u_common
*tp_c
;
75 struct tc_u_knode
*ht
[1];
79 struct tc_u_hnode
*hlist
;
85 static inline unsigned int u32_hash_fold(__be32 key
,
86 const struct tc_u32_sel
*sel
,
89 unsigned int h
= ntohl(key
& sel
->hmask
) >> fshift
;
94 static int u32_classify(struct sk_buff
*skb
, const struct tcf_proto
*tp
, struct tcf_result
*res
)
97 struct tc_u_knode
*knode
;
99 } stack
[TC_U32_MAXDEPTH
];
101 struct tc_u_hnode
*ht
= tp
->root
;
102 unsigned int off
= skb_network_offset(skb
);
103 struct tc_u_knode
*n
;
107 #ifdef CONFIG_CLS_U32_PERF
117 struct tc_u32_key
*key
= n
->sel
.keys
;
119 #ifdef CONFIG_CLS_U32_PERF
120 __this_cpu_inc(n
->pf
->rcnt
);
124 #ifdef CONFIG_CLS_U32_MARK
125 if ((skb
->mark
& n
->mask
) != n
->val
) {
129 __this_cpu_inc(*n
->pcpu_success
);
133 for (i
= n
->sel
.nkeys
; i
> 0; i
--, key
++) {
134 int toff
= off
+ key
->off
+ (off2
& key
->offmask
);
137 if (skb_headroom(skb
) + toff
> INT_MAX
)
140 data
= skb_header_pointer(skb
, toff
, 4, &hdata
);
143 if ((*data
^ key
->val
) & key
->mask
) {
147 #ifdef CONFIG_CLS_U32_PERF
148 __this_cpu_inc(n
->pf
->kcnts
[j
]);
152 if (n
->ht_down
== NULL
) {
154 if (n
->sel
.flags
& TC_U32_TERMINAL
) {
157 #ifdef CONFIG_NET_CLS_IND
158 if (!tcf_match_indev(skb
, n
->ifindex
)) {
163 #ifdef CONFIG_CLS_U32_PERF
164 __this_cpu_inc(n
->pf
->rhit
);
166 r
= tcf_exts_exec(skb
, &n
->exts
, res
);
179 if (sdepth
>= TC_U32_MAXDEPTH
)
181 stack
[sdepth
].knode
= n
;
182 stack
[sdepth
].off
= off
;
190 data
= skb_header_pointer(skb
, off
+ n
->sel
.hoff
, 4,
194 sel
= ht
->divisor
& u32_hash_fold(*data
, &n
->sel
,
197 if (!(n
->sel
.flags
& (TC_U32_VAROFFSET
| TC_U32_OFFSET
| TC_U32_EAT
)))
200 if (n
->sel
.flags
& (TC_U32_OFFSET
| TC_U32_VAROFFSET
)) {
201 off2
= n
->sel
.off
+ 3;
202 if (n
->sel
.flags
& TC_U32_VAROFFSET
) {
205 data
= skb_header_pointer(skb
,
210 off2
+= ntohs(n
->sel
.offmask
& *data
) >>
215 if (n
->sel
.flags
& TC_U32_EAT
) {
226 n
= stack
[sdepth
].knode
;
228 off
= stack
[sdepth
].off
;
235 net_warn_ratelimited("cls_u32: dead loop\n");
239 static struct tc_u_hnode
*
240 u32_lookup_ht(struct tc_u_common
*tp_c
, u32 handle
)
242 struct tc_u_hnode
*ht
;
244 for (ht
= tp_c
->hlist
; ht
; ht
= ht
->next
)
245 if (ht
->handle
== handle
)
251 static struct tc_u_knode
*
252 u32_lookup_key(struct tc_u_hnode
*ht
, u32 handle
)
255 struct tc_u_knode
*n
= NULL
;
257 sel
= TC_U32_HASH(handle
);
258 if (sel
> ht
->divisor
)
261 for (n
= ht
->ht
[sel
]; n
; n
= n
->next
)
262 if (n
->handle
== handle
)
269 static unsigned long u32_get(struct tcf_proto
*tp
, u32 handle
)
271 struct tc_u_hnode
*ht
;
272 struct tc_u_common
*tp_c
= tp
->data
;
274 if (TC_U32_HTID(handle
) == TC_U32_ROOT
)
277 ht
= u32_lookup_ht(tp_c
, TC_U32_HTID(handle
));
282 if (TC_U32_KEY(handle
) == 0)
283 return (unsigned long)ht
;
285 return (unsigned long)u32_lookup_key(ht
, handle
);
288 static void u32_put(struct tcf_proto
*tp
, unsigned long f
)
292 static u32
gen_new_htid(struct tc_u_common
*tp_c
)
297 if (++tp_c
->hgenerator
== 0x7FF)
298 tp_c
->hgenerator
= 1;
299 } while (--i
> 0 && u32_lookup_ht(tp_c
, (tp_c
->hgenerator
|0x800)<<20));
301 return i
> 0 ? (tp_c
->hgenerator
|0x800)<<20 : 0;
304 static int u32_init(struct tcf_proto
*tp
)
306 struct tc_u_hnode
*root_ht
;
307 struct tc_u_common
*tp_c
;
309 tp_c
= tp
->q
->u32_node
;
311 root_ht
= kzalloc(sizeof(*root_ht
), GFP_KERNEL
);
315 root_ht
->divisor
= 0;
317 root_ht
->handle
= tp_c
? gen_new_htid(tp_c
) : 0x80000000;
318 root_ht
->prio
= tp
->prio
;
321 tp_c
= kzalloc(sizeof(*tp_c
), GFP_KERNEL
);
327 tp
->q
->u32_node
= tp_c
;
331 root_ht
->next
= tp_c
->hlist
;
332 tp_c
->hlist
= root_ht
;
333 root_ht
->tp_c
= tp_c
;
340 static int u32_destroy_key(struct tcf_proto
*tp
, struct tc_u_knode
*n
)
342 tcf_unbind_filter(tp
, &n
->res
);
343 tcf_exts_destroy(tp
, &n
->exts
);
345 n
->ht_down
->refcnt
--;
346 #ifdef CONFIG_CLS_U32_PERF
353 static int u32_delete_key(struct tcf_proto
*tp
, struct tc_u_knode
*key
)
355 struct tc_u_knode
**kp
;
356 struct tc_u_hnode
*ht
= key
->ht_up
;
359 for (kp
= &ht
->ht
[TC_U32_HASH(key
->handle
)]; *kp
; kp
= &(*kp
)->next
) {
365 u32_destroy_key(tp
, key
);
374 static void u32_clear_hnode(struct tcf_proto
*tp
, struct tc_u_hnode
*ht
)
376 struct tc_u_knode
*n
;
379 for (h
= 0; h
<= ht
->divisor
; h
++) {
380 while ((n
= ht
->ht
[h
]) != NULL
) {
383 u32_destroy_key(tp
, n
);
388 static int u32_destroy_hnode(struct tcf_proto
*tp
, struct tc_u_hnode
*ht
)
390 struct tc_u_common
*tp_c
= tp
->data
;
391 struct tc_u_hnode
**hn
;
395 u32_clear_hnode(tp
, ht
);
397 for (hn
= &tp_c
->hlist
; *hn
; hn
= &(*hn
)->next
) {
409 static void u32_destroy(struct tcf_proto
*tp
)
411 struct tc_u_common
*tp_c
= tp
->data
;
412 struct tc_u_hnode
*root_ht
= tp
->root
;
414 WARN_ON(root_ht
== NULL
);
416 if (root_ht
&& --root_ht
->refcnt
== 0)
417 u32_destroy_hnode(tp
, root_ht
);
419 if (--tp_c
->refcnt
== 0) {
420 struct tc_u_hnode
*ht
;
422 tp
->q
->u32_node
= NULL
;
424 for (ht
= tp_c
->hlist
; ht
; ht
= ht
->next
) {
426 u32_clear_hnode(tp
, ht
);
429 while ((ht
= tp_c
->hlist
) != NULL
) {
430 tp_c
->hlist
= ht
->next
;
432 WARN_ON(ht
->refcnt
!= 0);
443 static int u32_delete(struct tcf_proto
*tp
, unsigned long arg
)
445 struct tc_u_hnode
*ht
= (struct tc_u_hnode
*)arg
;
450 if (TC_U32_KEY(ht
->handle
))
451 return u32_delete_key(tp
, (struct tc_u_knode
*)ht
);
456 if (ht
->refcnt
== 1) {
458 u32_destroy_hnode(tp
, ht
);
466 #define NR_U32_NODE (1<<12)
467 static u32
gen_new_kid(struct tc_u_hnode
*ht
, u32 handle
)
469 struct tc_u_knode
*n
;
471 unsigned long *bitmap
= kzalloc(BITS_TO_LONGS(NR_U32_NODE
) * sizeof(unsigned long),
474 return handle
| 0xFFF;
476 for (n
= ht
->ht
[TC_U32_HASH(handle
)]; n
; n
= n
->next
)
477 set_bit(TC_U32_NODE(n
->handle
), bitmap
);
479 i
= find_next_zero_bit(bitmap
, NR_U32_NODE
, 0x800);
480 if (i
>= NR_U32_NODE
)
481 i
= find_next_zero_bit(bitmap
, NR_U32_NODE
, 1);
484 return handle
| (i
>= NR_U32_NODE
? 0xFFF : i
);
487 static const struct nla_policy u32_policy
[TCA_U32_MAX
+ 1] = {
488 [TCA_U32_CLASSID
] = { .type
= NLA_U32
},
489 [TCA_U32_HASH
] = { .type
= NLA_U32
},
490 [TCA_U32_LINK
] = { .type
= NLA_U32
},
491 [TCA_U32_DIVISOR
] = { .type
= NLA_U32
},
492 [TCA_U32_SEL
] = { .len
= sizeof(struct tc_u32_sel
) },
493 [TCA_U32_INDEV
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
},
494 [TCA_U32_MARK
] = { .len
= sizeof(struct tc_u32_mark
) },
497 static int u32_set_parms(struct net
*net
, struct tcf_proto
*tp
,
498 unsigned long base
, struct tc_u_hnode
*ht
,
499 struct tc_u_knode
*n
, struct nlattr
**tb
,
500 struct nlattr
*est
, bool ovr
)
505 tcf_exts_init(&e
, TCA_U32_ACT
, TCA_U32_POLICE
);
506 err
= tcf_exts_validate(net
, tp
, tb
, est
, &e
, ovr
);
511 if (tb
[TCA_U32_LINK
]) {
512 u32 handle
= nla_get_u32(tb
[TCA_U32_LINK
]);
513 struct tc_u_hnode
*ht_down
= NULL
, *ht_old
;
515 if (TC_U32_KEY(handle
))
519 ht_down
= u32_lookup_ht(ht
->tp_c
, handle
);
528 n
->ht_down
= ht_down
;
534 if (tb
[TCA_U32_CLASSID
]) {
535 n
->res
.classid
= nla_get_u32(tb
[TCA_U32_CLASSID
]);
536 tcf_bind_filter(tp
, &n
->res
, base
);
539 #ifdef CONFIG_NET_CLS_IND
540 if (tb
[TCA_U32_INDEV
]) {
542 ret
= tcf_change_indev(net
, tb
[TCA_U32_INDEV
]);
548 tcf_exts_change(tp
, &n
->exts
, &e
);
552 tcf_exts_destroy(tp
, &e
);
556 static int u32_change(struct net
*net
, struct sk_buff
*in_skb
,
557 struct tcf_proto
*tp
, unsigned long base
, u32 handle
,
559 unsigned long *arg
, bool ovr
)
561 struct tc_u_common
*tp_c
= tp
->data
;
562 struct tc_u_hnode
*ht
;
563 struct tc_u_knode
*n
;
564 struct tc_u32_sel
*s
;
565 struct nlattr
*opt
= tca
[TCA_OPTIONS
];
566 struct nlattr
*tb
[TCA_U32_MAX
+ 1];
569 #ifdef CONFIG_CLS_U32_PERF
574 return handle
? -EINVAL
: 0;
576 err
= nla_parse_nested(tb
, TCA_U32_MAX
, opt
, u32_policy
);
580 n
= (struct tc_u_knode
*)*arg
;
582 if (TC_U32_KEY(n
->handle
) == 0)
585 return u32_set_parms(net
, tp
, base
, n
->ht_up
, n
, tb
,
589 if (tb
[TCA_U32_DIVISOR
]) {
590 unsigned int divisor
= nla_get_u32(tb
[TCA_U32_DIVISOR
]);
592 if (--divisor
> 0x100)
594 if (TC_U32_KEY(handle
))
597 handle
= gen_new_htid(tp
->data
);
601 ht
= kzalloc(sizeof(*ht
) + divisor
*sizeof(void *), GFP_KERNEL
);
606 ht
->divisor
= divisor
;
609 ht
->next
= tp_c
->hlist
;
611 *arg
= (unsigned long)ht
;
615 if (tb
[TCA_U32_HASH
]) {
616 htid
= nla_get_u32(tb
[TCA_U32_HASH
]);
617 if (TC_U32_HTID(htid
) == TC_U32_ROOT
) {
621 ht
= u32_lookup_ht(tp
->data
, TC_U32_HTID(htid
));
630 if (ht
->divisor
< TC_U32_HASH(htid
))
634 if (TC_U32_HTID(handle
) && TC_U32_HTID(handle
^htid
))
636 handle
= htid
| TC_U32_NODE(handle
);
638 handle
= gen_new_kid(ht
, htid
);
640 if (tb
[TCA_U32_SEL
] == NULL
)
643 s
= nla_data(tb
[TCA_U32_SEL
]);
645 n
= kzalloc(sizeof(*n
) + s
->nkeys
*sizeof(struct tc_u32_key
), GFP_KERNEL
);
649 #ifdef CONFIG_CLS_U32_PERF
650 size
= sizeof(struct tc_u32_pcnt
) + s
->nkeys
* sizeof(u64
);
651 n
->pf
= __alloc_percpu(size
, __alignof__(struct tc_u32_pcnt
));
658 memcpy(&n
->sel
, s
, sizeof(*s
) + s
->nkeys
*sizeof(struct tc_u32_key
));
661 n
->fshift
= s
->hmask
? ffs(ntohl(s
->hmask
)) - 1 : 0;
662 tcf_exts_init(&n
->exts
, TCA_U32_ACT
, TCA_U32_POLICE
);
664 #ifdef CONFIG_CLS_U32_MARK
665 n
->pcpu_success
= alloc_percpu(u32
);
667 if (tb
[TCA_U32_MARK
]) {
668 struct tc_u32_mark
*mark
;
670 mark
= nla_data(tb
[TCA_U32_MARK
]);
672 n
->mask
= mark
->mask
;
676 err
= u32_set_parms(net
, tp
, base
, ht
, n
, tb
, tca
[TCA_RATE
], ovr
);
678 struct tc_u_knode
**ins
;
679 for (ins
= &ht
->ht
[TC_U32_HASH(handle
)]; *ins
; ins
= &(*ins
)->next
)
680 if (TC_U32_NODE(handle
) < TC_U32_NODE((*ins
)->handle
))
688 *arg
= (unsigned long)n
;
691 #ifdef CONFIG_CLS_U32_PERF
698 static void u32_walk(struct tcf_proto
*tp
, struct tcf_walker
*arg
)
700 struct tc_u_common
*tp_c
= tp
->data
;
701 struct tc_u_hnode
*ht
;
702 struct tc_u_knode
*n
;
708 for (ht
= tp_c
->hlist
; ht
; ht
= ht
->next
) {
709 if (ht
->prio
!= tp
->prio
)
711 if (arg
->count
>= arg
->skip
) {
712 if (arg
->fn(tp
, (unsigned long)ht
, arg
) < 0) {
718 for (h
= 0; h
<= ht
->divisor
; h
++) {
719 for (n
= ht
->ht
[h
]; n
; n
= n
->next
) {
720 if (arg
->count
< arg
->skip
) {
724 if (arg
->fn(tp
, (unsigned long)n
, arg
) < 0) {
734 static int u32_dump(struct net
*net
, struct tcf_proto
*tp
, unsigned long fh
,
735 struct sk_buff
*skb
, struct tcmsg
*t
)
737 struct tc_u_knode
*n
= (struct tc_u_knode
*)fh
;
743 t
->tcm_handle
= n
->handle
;
745 nest
= nla_nest_start(skb
, TCA_OPTIONS
);
747 goto nla_put_failure
;
749 if (TC_U32_KEY(n
->handle
) == 0) {
750 struct tc_u_hnode
*ht
= (struct tc_u_hnode
*)fh
;
751 u32 divisor
= ht
->divisor
+ 1;
753 if (nla_put_u32(skb
, TCA_U32_DIVISOR
, divisor
))
754 goto nla_put_failure
;
756 #ifdef CONFIG_CLS_U32_PERF
757 struct tc_u32_pcnt
*gpf
;
761 if (nla_put(skb
, TCA_U32_SEL
,
762 sizeof(n
->sel
) + n
->sel
.nkeys
*sizeof(struct tc_u32_key
),
764 goto nla_put_failure
;
766 u32 htid
= n
->handle
& 0xFFFFF000;
767 if (nla_put_u32(skb
, TCA_U32_HASH
, htid
))
768 goto nla_put_failure
;
770 if (n
->res
.classid
&&
771 nla_put_u32(skb
, TCA_U32_CLASSID
, n
->res
.classid
))
772 goto nla_put_failure
;
774 nla_put_u32(skb
, TCA_U32_LINK
, n
->ht_down
->handle
))
775 goto nla_put_failure
;
777 #ifdef CONFIG_CLS_U32_MARK
778 if ((n
->val
|| n
->mask
)) {
779 struct tc_u32_mark mark
= {.val
= n
->val
,
783 for_each_possible_cpu(cpu
) {
784 __u32 cnt
= *per_cpu_ptr(n
->pcpu_success
, cpu
);
789 if (nla_put(skb
, TCA_U32_MARK
, sizeof(mark
), &mark
))
790 goto nla_put_failure
;
794 if (tcf_exts_dump(skb
, &n
->exts
) < 0)
795 goto nla_put_failure
;
797 #ifdef CONFIG_NET_CLS_IND
799 struct net_device
*dev
;
800 dev
= __dev_get_by_index(net
, n
->ifindex
);
801 if (dev
&& nla_put_string(skb
, TCA_U32_INDEV
, dev
->name
))
802 goto nla_put_failure
;
805 #ifdef CONFIG_CLS_U32_PERF
806 gpf
= kzalloc(sizeof(struct tc_u32_pcnt
) +
807 n
->sel
.nkeys
* sizeof(u64
),
810 goto nla_put_failure
;
812 for_each_possible_cpu(cpu
) {
814 struct tc_u32_pcnt
*pf
= per_cpu_ptr(n
->pf
, cpu
);
816 gpf
->rcnt
+= pf
->rcnt
;
817 gpf
->rhit
+= pf
->rhit
;
818 for (i
= 0; i
< n
->sel
.nkeys
; i
++)
819 gpf
->kcnts
[i
] += pf
->kcnts
[i
];
822 if (nla_put(skb
, TCA_U32_PCNT
,
823 sizeof(struct tc_u32_pcnt
) + n
->sel
.nkeys
*sizeof(u64
),
826 goto nla_put_failure
;
832 nla_nest_end(skb
, nest
);
834 if (TC_U32_KEY(n
->handle
))
835 if (tcf_exts_dump_stats(skb
, &n
->exts
) < 0)
836 goto nla_put_failure
;
840 nla_nest_cancel(skb
, nest
);
844 static struct tcf_proto_ops cls_u32_ops __read_mostly
= {
846 .classify
= u32_classify
,
848 .destroy
= u32_destroy
,
851 .change
= u32_change
,
852 .delete = u32_delete
,
855 .owner
= THIS_MODULE
,
858 static int __init
init_u32(void)
860 pr_info("u32 classifier\n");
861 #ifdef CONFIG_CLS_U32_PERF
862 pr_info(" Performance counters on\n");
864 #ifdef CONFIG_NET_CLS_IND
865 pr_info(" input device check on\n");
867 #ifdef CONFIG_NET_CLS_ACT
868 pr_info(" Actions configured\n");
870 return register_tcf_proto_ops(&cls_u32_ops
);
873 static void __exit
exit_u32(void)
875 unregister_tcf_proto_ops(&cls_u32_ops
);
878 module_init(init_u32
)
879 module_exit(exit_u32
)
880 MODULE_LICENSE("GPL");