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 <asm/uaccess.h>
34 #include <asm/system.h>
35 #include <linux/bitops.h>
36 #include <linux/module.h>
37 #include <linux/types.h>
38 #include <linux/kernel.h>
39 #include <linux/string.h>
41 #include <linux/socket.h>
42 #include <linux/sockios.h>
44 #include <linux/errno.h>
45 #include <linux/interrupt.h>
46 #include <linux/if_ether.h>
47 #include <linux/inet.h>
48 #include <linux/netdevice.h>
49 #include <linux/etherdevice.h>
50 #include <linux/notifier.h>
51 #include <linux/rtnetlink.h>
53 #include <net/netlink.h>
54 #include <net/route.h>
55 #include <linux/skbuff.h>
57 #include <net/act_api.h>
58 #include <net/pkt_cls.h>
62 struct tc_u_knode
*next
;
64 struct tc_u_hnode
*ht_up
;
66 #ifdef CONFIG_NET_CLS_IND
70 struct tcf_result res
;
71 struct tc_u_hnode
*ht_down
;
72 #ifdef CONFIG_CLS_U32_PERF
73 struct tc_u32_pcnt
*pf
;
75 #ifdef CONFIG_CLS_U32_MARK
76 struct tc_u32_mark mark
;
78 struct tc_u32_sel sel
;
83 struct tc_u_hnode
*next
;
86 struct tc_u_common
*tp_c
;
89 struct tc_u_knode
*ht
[1];
94 struct tc_u_common
*next
;
95 struct tc_u_hnode
*hlist
;
101 static struct tcf_ext_map u32_ext_map
= {
102 .action
= TCA_U32_ACT
,
103 .police
= TCA_U32_POLICE
106 static struct tc_u_common
*u32_list
;
108 static __inline__
unsigned u32_hash_fold(u32 key
, struct tc_u32_sel
*sel
, u8 fshift
)
110 unsigned h
= (key
& sel
->hmask
)>>fshift
;
115 static int u32_classify(struct sk_buff
*skb
, struct tcf_proto
*tp
, struct tcf_result
*res
)
118 struct tc_u_knode
*knode
;
120 } stack
[TC_U32_MAXDEPTH
];
122 struct tc_u_hnode
*ht
= (struct tc_u_hnode
*)tp
->root
;
123 u8
*ptr
= skb_network_header(skb
);
124 struct tc_u_knode
*n
;
128 #ifdef CONFIG_CLS_U32_PERF
138 struct tc_u32_key
*key
= n
->sel
.keys
;
140 #ifdef CONFIG_CLS_U32_PERF
145 #ifdef CONFIG_CLS_U32_MARK
146 if ((skb
->mark
& n
->mark
.mask
) != n
->mark
.val
) {
154 for (i
= n
->sel
.nkeys
; i
>0; i
--, key
++) {
156 if ((*(u32
*)(ptr
+key
->off
+(off2
&key
->offmask
))^key
->val
)&key
->mask
) {
160 #ifdef CONFIG_CLS_U32_PERF
165 if (n
->ht_down
== NULL
) {
167 if (n
->sel
.flags
&TC_U32_TERMINAL
) {
170 #ifdef CONFIG_NET_CLS_IND
171 if (!tcf_match_indev(skb
, n
->indev
)) {
176 #ifdef CONFIG_CLS_U32_PERF
179 r
= tcf_exts_exec(skb
, &n
->exts
, res
);
192 if (sdepth
>= TC_U32_MAXDEPTH
)
194 stack
[sdepth
].knode
= n
;
195 stack
[sdepth
].ptr
= ptr
;
201 sel
= ht
->divisor
&u32_hash_fold(*(u32
*)(ptr
+n
->sel
.hoff
), &n
->sel
,n
->fshift
);
203 if (!(n
->sel
.flags
&(TC_U32_VAROFFSET
|TC_U32_OFFSET
|TC_U32_EAT
)))
206 if (n
->sel
.flags
&(TC_U32_OFFSET
|TC_U32_VAROFFSET
)) {
207 off2
= n
->sel
.off
+ 3;
208 if (n
->sel
.flags
&TC_U32_VAROFFSET
)
209 off2
+= ntohs(n
->sel
.offmask
& *(u16
*)(ptr
+n
->sel
.offoff
)) >>n
->sel
.offshift
;
212 if (n
->sel
.flags
&TC_U32_EAT
) {
217 if (ptr
< skb_tail_pointer(skb
))
223 n
= stack
[sdepth
].knode
;
225 ptr
= stack
[sdepth
].ptr
;
232 printk("cls_u32: dead loop\n");
236 static __inline__
struct tc_u_hnode
*
237 u32_lookup_ht(struct tc_u_common
*tp_c
, u32 handle
)
239 struct tc_u_hnode
*ht
;
241 for (ht
= tp_c
->hlist
; ht
; ht
= ht
->next
)
242 if (ht
->handle
== handle
)
248 static __inline__
struct tc_u_knode
*
249 u32_lookup_key(struct tc_u_hnode
*ht
, u32 handle
)
252 struct tc_u_knode
*n
= NULL
;
254 sel
= TC_U32_HASH(handle
);
255 if (sel
> ht
->divisor
)
258 for (n
= ht
->ht
[sel
]; n
; n
= n
->next
)
259 if (n
->handle
== handle
)
266 static unsigned long u32_get(struct tcf_proto
*tp
, u32 handle
)
268 struct tc_u_hnode
*ht
;
269 struct tc_u_common
*tp_c
= tp
->data
;
271 if (TC_U32_HTID(handle
) == TC_U32_ROOT
)
274 ht
= u32_lookup_ht(tp_c
, TC_U32_HTID(handle
));
279 if (TC_U32_KEY(handle
) == 0)
280 return (unsigned long)ht
;
282 return (unsigned long)u32_lookup_key(ht
, handle
);
285 static void u32_put(struct tcf_proto
*tp
, unsigned long f
)
289 static u32
gen_new_htid(struct tc_u_common
*tp_c
)
294 if (++tp_c
->hgenerator
== 0x7FF)
295 tp_c
->hgenerator
= 1;
296 } while (--i
>0 && u32_lookup_ht(tp_c
, (tp_c
->hgenerator
|0x800)<<20));
298 return i
> 0 ? (tp_c
->hgenerator
|0x800)<<20 : 0;
301 static int u32_init(struct tcf_proto
*tp
)
303 struct tc_u_hnode
*root_ht
;
304 struct tc_u_common
*tp_c
;
306 for (tp_c
= u32_list
; tp_c
; tp_c
= tp_c
->next
)
307 if (tp_c
->q
== tp
->q
)
310 root_ht
= kzalloc(sizeof(*root_ht
), GFP_KERNEL
);
314 root_ht
->divisor
= 0;
316 root_ht
->handle
= tp_c
? gen_new_htid(tp_c
) : 0x80000000;
317 root_ht
->prio
= tp
->prio
;
320 tp_c
= kzalloc(sizeof(*tp_c
), GFP_KERNEL
);
326 tp_c
->next
= u32_list
;
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
;
393 BUG_TRAP(!ht
->refcnt
);
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
= xchg(&tp
->root
, NULL
);
414 BUG_TRAP(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
;
421 struct tc_u_common
**tp_cp
;
423 for (tp_cp
= &u32_list
; *tp_cp
; tp_cp
= &(*tp_cp
)->next
) {
424 if (*tp_cp
== tp_c
) {
430 for (ht
=tp_c
->hlist
; ht
; ht
= ht
->next
)
431 u32_clear_hnode(tp
, ht
);
433 while ((ht
= tp_c
->hlist
) != NULL
) {
434 tp_c
->hlist
= ht
->next
;
436 BUG_TRAP(ht
->refcnt
== 0);
447 static int u32_delete(struct tcf_proto
*tp
, unsigned long arg
)
449 struct tc_u_hnode
*ht
= (struct tc_u_hnode
*)arg
;
454 if (TC_U32_KEY(ht
->handle
))
455 return u32_delete_key(tp
, (struct tc_u_knode
*)ht
);
460 if (--ht
->refcnt
== 0)
461 u32_destroy_hnode(tp
, ht
);
466 static u32
gen_new_kid(struct tc_u_hnode
*ht
, u32 handle
)
468 struct tc_u_knode
*n
;
471 for (n
=ht
->ht
[TC_U32_HASH(handle
)]; n
; n
= n
->next
)
472 if (i
< TC_U32_NODE(n
->handle
))
473 i
= TC_U32_NODE(n
->handle
);
476 return handle
|(i
>0xFFF ? 0xFFF : i
);
479 static int u32_set_parms(struct tcf_proto
*tp
, unsigned long base
,
480 struct tc_u_hnode
*ht
,
481 struct tc_u_knode
*n
, struct rtattr
**tb
,
487 err
= tcf_exts_validate(tp
, tb
, est
, &e
, &u32_ext_map
);
492 if (tb
[TCA_U32_LINK
-1]) {
493 u32 handle
= *(u32
*)RTA_DATA(tb
[TCA_U32_LINK
-1]);
494 struct tc_u_hnode
*ht_down
= NULL
;
496 if (TC_U32_KEY(handle
))
500 ht_down
= u32_lookup_ht(ht
->tp_c
, handle
);
508 ht_down
= xchg(&n
->ht_down
, ht_down
);
514 if (tb
[TCA_U32_CLASSID
-1]) {
515 n
->res
.classid
= *(u32
*)RTA_DATA(tb
[TCA_U32_CLASSID
-1]);
516 tcf_bind_filter(tp
, &n
->res
, base
);
519 #ifdef CONFIG_NET_CLS_IND
520 if (tb
[TCA_U32_INDEV
-1]) {
521 int err
= tcf_change_indev(tp
, n
->indev
, tb
[TCA_U32_INDEV
-1]);
526 tcf_exts_change(tp
, &n
->exts
, &e
);
530 tcf_exts_destroy(tp
, &e
);
534 static int u32_change(struct tcf_proto
*tp
, unsigned long base
, u32 handle
,
538 struct tc_u_common
*tp_c
= tp
->data
;
539 struct tc_u_hnode
*ht
;
540 struct tc_u_knode
*n
;
541 struct tc_u32_sel
*s
;
542 struct rtattr
*opt
= tca
[TCA_OPTIONS
-1];
543 struct rtattr
*tb
[TCA_U32_MAX
];
548 return handle
? -EINVAL
: 0;
550 if (rtattr_parse_nested(tb
, TCA_U32_MAX
, opt
) < 0)
553 if ((n
= (struct tc_u_knode
*)*arg
) != NULL
) {
554 if (TC_U32_KEY(n
->handle
) == 0)
557 return u32_set_parms(tp
, base
, n
->ht_up
, n
, tb
, tca
[TCA_RATE
-1]);
560 if (tb
[TCA_U32_DIVISOR
-1]) {
561 unsigned divisor
= *(unsigned*)RTA_DATA(tb
[TCA_U32_DIVISOR
-1]);
563 if (--divisor
> 0x100)
565 if (TC_U32_KEY(handle
))
568 handle
= gen_new_htid(tp
->data
);
572 ht
= kzalloc(sizeof(*ht
) + divisor
*sizeof(void*), GFP_KERNEL
);
577 ht
->divisor
= divisor
;
580 ht
->next
= tp_c
->hlist
;
582 *arg
= (unsigned long)ht
;
586 if (tb
[TCA_U32_HASH
-1]) {
587 htid
= *(unsigned*)RTA_DATA(tb
[TCA_U32_HASH
-1]);
588 if (TC_U32_HTID(htid
) == TC_U32_ROOT
) {
592 ht
= u32_lookup_ht(tp
->data
, TC_U32_HTID(htid
));
601 if (ht
->divisor
< TC_U32_HASH(htid
))
605 if (TC_U32_HTID(handle
) && TC_U32_HTID(handle
^htid
))
607 handle
= htid
| TC_U32_NODE(handle
);
609 handle
= gen_new_kid(ht
, htid
);
611 if (tb
[TCA_U32_SEL
-1] == 0 ||
612 RTA_PAYLOAD(tb
[TCA_U32_SEL
-1]) < sizeof(struct tc_u32_sel
))
615 s
= RTA_DATA(tb
[TCA_U32_SEL
-1]);
617 n
= kzalloc(sizeof(*n
) + s
->nkeys
*sizeof(struct tc_u32_key
), GFP_KERNEL
);
621 #ifdef CONFIG_CLS_U32_PERF
622 n
->pf
= kzalloc(sizeof(struct tc_u32_pcnt
) + s
->nkeys
*sizeof(u64
), GFP_KERNEL
);
629 memcpy(&n
->sel
, s
, sizeof(*s
) + s
->nkeys
*sizeof(struct tc_u32_key
));
636 while (!(mask
& 1)) {
644 #ifdef CONFIG_CLS_U32_MARK
645 if (tb
[TCA_U32_MARK
-1]) {
646 struct tc_u32_mark
*mark
;
648 if (RTA_PAYLOAD(tb
[TCA_U32_MARK
-1]) < sizeof(struct tc_u32_mark
)) {
649 #ifdef CONFIG_CLS_U32_PERF
655 mark
= RTA_DATA(tb
[TCA_U32_MARK
-1]);
656 memcpy(&n
->mark
, mark
, sizeof(struct tc_u32_mark
));
661 err
= u32_set_parms(tp
, base
, ht
, n
, tb
, tca
[TCA_RATE
-1]);
663 struct tc_u_knode
**ins
;
664 for (ins
= &ht
->ht
[TC_U32_HASH(handle
)]; *ins
; ins
= &(*ins
)->next
)
665 if (TC_U32_NODE(handle
) < TC_U32_NODE((*ins
)->handle
))
672 *arg
= (unsigned long)n
;
675 #ifdef CONFIG_CLS_U32_PERF
682 static void u32_walk(struct tcf_proto
*tp
, struct tcf_walker
*arg
)
684 struct tc_u_common
*tp_c
= tp
->data
;
685 struct tc_u_hnode
*ht
;
686 struct tc_u_knode
*n
;
692 for (ht
= tp_c
->hlist
; ht
; ht
= ht
->next
) {
693 if (ht
->prio
!= tp
->prio
)
695 if (arg
->count
>= arg
->skip
) {
696 if (arg
->fn(tp
, (unsigned long)ht
, arg
) < 0) {
702 for (h
= 0; h
<= ht
->divisor
; h
++) {
703 for (n
= ht
->ht
[h
]; n
; n
= n
->next
) {
704 if (arg
->count
< arg
->skip
) {
708 if (arg
->fn(tp
, (unsigned long)n
, arg
) < 0) {
718 static int u32_dump(struct tcf_proto
*tp
, unsigned long fh
,
719 struct sk_buff
*skb
, struct tcmsg
*t
)
721 struct tc_u_knode
*n
= (struct tc_u_knode
*)fh
;
722 unsigned char *b
= skb_tail_pointer(skb
);
728 t
->tcm_handle
= n
->handle
;
730 rta
= (struct rtattr
*)b
;
731 RTA_PUT(skb
, TCA_OPTIONS
, 0, NULL
);
733 if (TC_U32_KEY(n
->handle
) == 0) {
734 struct tc_u_hnode
*ht
= (struct tc_u_hnode
*)fh
;
735 u32 divisor
= ht
->divisor
+1;
736 RTA_PUT(skb
, TCA_U32_DIVISOR
, 4, &divisor
);
738 RTA_PUT(skb
, TCA_U32_SEL
,
739 sizeof(n
->sel
) + n
->sel
.nkeys
*sizeof(struct tc_u32_key
),
742 u32 htid
= n
->handle
& 0xFFFFF000;
743 RTA_PUT(skb
, TCA_U32_HASH
, 4, &htid
);
746 RTA_PUT(skb
, TCA_U32_CLASSID
, 4, &n
->res
.classid
);
748 RTA_PUT(skb
, TCA_U32_LINK
, 4, &n
->ht_down
->handle
);
750 #ifdef CONFIG_CLS_U32_MARK
751 if (n
->mark
.val
|| n
->mark
.mask
)
752 RTA_PUT(skb
, TCA_U32_MARK
, sizeof(n
->mark
), &n
->mark
);
755 if (tcf_exts_dump(skb
, &n
->exts
, &u32_ext_map
) < 0)
758 #ifdef CONFIG_NET_CLS_IND
760 RTA_PUT(skb
, TCA_U32_INDEV
, IFNAMSIZ
, n
->indev
);
762 #ifdef CONFIG_CLS_U32_PERF
763 RTA_PUT(skb
, TCA_U32_PCNT
,
764 sizeof(struct tc_u32_pcnt
) + n
->sel
.nkeys
*sizeof(u64
),
769 rta
->rta_len
= skb_tail_pointer(skb
) - b
;
770 if (TC_U32_KEY(n
->handle
))
771 if (tcf_exts_dump_stats(skb
, &n
->exts
, &u32_ext_map
) < 0)
780 static struct tcf_proto_ops cls_u32_ops
= {
783 .classify
= u32_classify
,
785 .destroy
= u32_destroy
,
788 .change
= u32_change
,
789 .delete = u32_delete
,
792 .owner
= THIS_MODULE
,
795 static int __init
init_u32(void)
797 printk("u32 classifier\n");
798 #ifdef CONFIG_CLS_U32_PERF
799 printk(" Performance counters on\n");
801 #ifdef CONFIG_NET_CLS_POLICE
802 printk(" OLD policer on \n");
804 #ifdef CONFIG_NET_CLS_IND
805 printk(" input device check on \n");
807 #ifdef CONFIG_NET_CLS_ACT
808 printk(" Actions configured \n");
810 return register_tcf_proto_ops(&cls_u32_ops
);
813 static void __exit
exit_u32(void)
815 unregister_tcf_proto_ops(&cls_u32_ops
);
818 module_init(init_u32
)
819 module_exit(exit_u32
)
820 MODULE_LICENSE("GPL");