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/config.h>
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/kernel.h>
40 #include <linux/sched.h>
41 #include <linux/string.h>
43 #include <linux/socket.h>
44 #include <linux/sockios.h>
46 #include <linux/errno.h>
47 #include <linux/interrupt.h>
48 #include <linux/if_ether.h>
49 #include <linux/inet.h>
50 #include <linux/netdevice.h>
51 #include <linux/etherdevice.h>
52 #include <linux/notifier.h>
53 #include <linux/rtnetlink.h>
55 #include <net/route.h>
56 #include <linux/skbuff.h>
58 #include <net/act_api.h>
59 #include <net/pkt_cls.h>
63 struct tc_u_knode
*next
;
65 struct tc_u_hnode
*ht_up
;
67 #ifdef CONFIG_NET_CLS_IND
71 struct tcf_result res
;
72 struct tc_u_hnode
*ht_down
;
73 #ifdef CONFIG_CLS_U32_PERF
74 struct tc_u32_pcnt
*pf
;
76 #ifdef CONFIG_CLS_U32_MARK
77 struct tc_u32_mark mark
;
79 struct tc_u32_sel sel
;
84 struct tc_u_hnode
*next
;
87 struct tc_u_common
*tp_c
;
90 struct tc_u_knode
*ht
[1];
95 struct tc_u_common
*next
;
96 struct tc_u_hnode
*hlist
;
102 static struct tcf_ext_map u32_ext_map
= {
103 .action
= TCA_U32_ACT
,
104 .police
= TCA_U32_POLICE
107 static struct tc_u_common
*u32_list
;
109 static __inline__
unsigned u32_hash_fold(u32 key
, struct tc_u32_sel
*sel
, u8 fshift
)
111 unsigned h
= (key
& sel
->hmask
)>>fshift
;
116 static int u32_classify(struct sk_buff
*skb
, struct tcf_proto
*tp
, struct tcf_result
*res
)
119 struct tc_u_knode
*knode
;
121 } stack
[TC_U32_MAXDEPTH
];
123 struct tc_u_hnode
*ht
= (struct tc_u_hnode
*)tp
->root
;
124 u8
*ptr
= skb
->nh
.raw
;
125 struct tc_u_knode
*n
;
129 #ifdef CONFIG_CLS_U32_PERF
139 struct tc_u32_key
*key
= n
->sel
.keys
;
141 #ifdef CONFIG_CLS_U32_PERF
146 #ifdef CONFIG_CLS_U32_MARK
147 if ((skb
->nfmark
& n
->mark
.mask
) != n
->mark
.val
) {
155 for (i
= n
->sel
.nkeys
; i
>0; i
--, key
++) {
157 if ((*(u32
*)(ptr
+key
->off
+(off2
&key
->offmask
))^key
->val
)&key
->mask
) {
161 #ifdef CONFIG_CLS_U32_PERF
166 if (n
->ht_down
== NULL
) {
168 if (n
->sel
.flags
&TC_U32_TERMINAL
) {
171 #ifdef CONFIG_NET_CLS_IND
172 if (!tcf_match_indev(skb
, n
->indev
)) {
177 #ifdef CONFIG_CLS_U32_PERF
180 r
= tcf_exts_exec(skb
, &n
->exts
, res
);
193 if (sdepth
>= TC_U32_MAXDEPTH
)
195 stack
[sdepth
].knode
= n
;
196 stack
[sdepth
].ptr
= ptr
;
202 sel
= ht
->divisor
&u32_hash_fold(*(u32
*)(ptr
+n
->sel
.hoff
), &n
->sel
,n
->fshift
);
204 if (!(n
->sel
.flags
&(TC_U32_VAROFFSET
|TC_U32_OFFSET
|TC_U32_EAT
)))
207 if (n
->sel
.flags
&(TC_U32_OFFSET
|TC_U32_VAROFFSET
)) {
208 off2
= n
->sel
.off
+ 3;
209 if (n
->sel
.flags
&TC_U32_VAROFFSET
)
210 off2
+= ntohs(n
->sel
.offmask
& *(u16
*)(ptr
+n
->sel
.offoff
)) >>n
->sel
.offshift
;
213 if (n
->sel
.flags
&TC_U32_EAT
) {
224 n
= stack
[sdepth
].knode
;
226 ptr
= stack
[sdepth
].ptr
;
233 printk("cls_u32: dead loop\n");
237 static __inline__
struct tc_u_hnode
*
238 u32_lookup_ht(struct tc_u_common
*tp_c
, u32 handle
)
240 struct tc_u_hnode
*ht
;
242 for (ht
= tp_c
->hlist
; ht
; ht
= ht
->next
)
243 if (ht
->handle
== handle
)
249 static __inline__
struct tc_u_knode
*
250 u32_lookup_key(struct tc_u_hnode
*ht
, u32 handle
)
253 struct tc_u_knode
*n
= NULL
;
255 sel
= TC_U32_HASH(handle
);
256 if (sel
> ht
->divisor
)
259 for (n
= ht
->ht
[sel
]; n
; n
= n
->next
)
260 if (n
->handle
== handle
)
267 static unsigned long u32_get(struct tcf_proto
*tp
, u32 handle
)
269 struct tc_u_hnode
*ht
;
270 struct tc_u_common
*tp_c
= tp
->data
;
272 if (TC_U32_HTID(handle
) == TC_U32_ROOT
)
275 ht
= u32_lookup_ht(tp_c
, TC_U32_HTID(handle
));
280 if (TC_U32_KEY(handle
) == 0)
281 return (unsigned long)ht
;
283 return (unsigned long)u32_lookup_key(ht
, handle
);
286 static void u32_put(struct tcf_proto
*tp
, unsigned long f
)
290 static u32
gen_new_htid(struct tc_u_common
*tp_c
)
295 if (++tp_c
->hgenerator
== 0x7FF)
296 tp_c
->hgenerator
= 1;
297 } while (--i
>0 && u32_lookup_ht(tp_c
, (tp_c
->hgenerator
|0x800)<<20));
299 return i
> 0 ? (tp_c
->hgenerator
|0x800)<<20 : 0;
302 static int u32_init(struct tcf_proto
*tp
)
304 struct tc_u_hnode
*root_ht
;
305 struct tc_u_common
*tp_c
;
307 for (tp_c
= u32_list
; tp_c
; tp_c
= tp_c
->next
)
308 if (tp_c
->q
== tp
->q
)
311 root_ht
= kmalloc(sizeof(*root_ht
), GFP_KERNEL
);
315 memset(root_ht
, 0, sizeof(*root_ht
));
316 root_ht
->divisor
= 0;
318 root_ht
->handle
= tp_c
? gen_new_htid(tp_c
) : 0x80000000;
319 root_ht
->prio
= tp
->prio
;
322 tp_c
= kmalloc(sizeof(*tp_c
), GFP_KERNEL
);
327 memset(tp_c
, 0, sizeof(*tp_c
));
329 tp_c
->next
= u32_list
;
334 root_ht
->next
= tp_c
->hlist
;
335 tp_c
->hlist
= root_ht
;
336 root_ht
->tp_c
= tp_c
;
343 static int u32_destroy_key(struct tcf_proto
*tp
, struct tc_u_knode
*n
)
345 tcf_unbind_filter(tp
, &n
->res
);
346 tcf_exts_destroy(tp
, &n
->exts
);
348 n
->ht_down
->refcnt
--;
349 #ifdef CONFIG_CLS_U32_PERF
357 static int u32_delete_key(struct tcf_proto
*tp
, struct tc_u_knode
* key
)
359 struct tc_u_knode
**kp
;
360 struct tc_u_hnode
*ht
= key
->ht_up
;
363 for (kp
= &ht
->ht
[TC_U32_HASH(key
->handle
)]; *kp
; kp
= &(*kp
)->next
) {
369 u32_destroy_key(tp
, key
);
378 static void u32_clear_hnode(struct tcf_proto
*tp
, struct tc_u_hnode
*ht
)
380 struct tc_u_knode
*n
;
383 for (h
=0; h
<=ht
->divisor
; h
++) {
384 while ((n
= ht
->ht
[h
]) != NULL
) {
387 u32_destroy_key(tp
, n
);
392 static int u32_destroy_hnode(struct tcf_proto
*tp
, struct tc_u_hnode
*ht
)
394 struct tc_u_common
*tp_c
= tp
->data
;
395 struct tc_u_hnode
**hn
;
397 BUG_TRAP(!ht
->refcnt
);
399 u32_clear_hnode(tp
, ht
);
401 for (hn
= &tp_c
->hlist
; *hn
; hn
= &(*hn
)->next
) {
413 static void u32_destroy(struct tcf_proto
*tp
)
415 struct tc_u_common
*tp_c
= tp
->data
;
416 struct tc_u_hnode
*root_ht
= xchg(&tp
->root
, NULL
);
418 BUG_TRAP(root_ht
!= NULL
);
420 if (root_ht
&& --root_ht
->refcnt
== 0)
421 u32_destroy_hnode(tp
, root_ht
);
423 if (--tp_c
->refcnt
== 0) {
424 struct tc_u_hnode
*ht
;
425 struct tc_u_common
**tp_cp
;
427 for (tp_cp
= &u32_list
; *tp_cp
; tp_cp
= &(*tp_cp
)->next
) {
428 if (*tp_cp
== tp_c
) {
434 for (ht
=tp_c
->hlist
; ht
; ht
= ht
->next
)
435 u32_clear_hnode(tp
, ht
);
437 while ((ht
= tp_c
->hlist
) != NULL
) {
438 tp_c
->hlist
= ht
->next
;
440 BUG_TRAP(ht
->refcnt
== 0);
451 static int u32_delete(struct tcf_proto
*tp
, unsigned long arg
)
453 struct tc_u_hnode
*ht
= (struct tc_u_hnode
*)arg
;
458 if (TC_U32_KEY(ht
->handle
))
459 return u32_delete_key(tp
, (struct tc_u_knode
*)ht
);
464 if (--ht
->refcnt
== 0)
465 u32_destroy_hnode(tp
, ht
);
470 static u32
gen_new_kid(struct tc_u_hnode
*ht
, u32 handle
)
472 struct tc_u_knode
*n
;
475 for (n
=ht
->ht
[TC_U32_HASH(handle
)]; n
; n
= n
->next
)
476 if (i
< TC_U32_NODE(n
->handle
))
477 i
= TC_U32_NODE(n
->handle
);
480 return handle
|(i
>0xFFF ? 0xFFF : i
);
483 static int u32_set_parms(struct tcf_proto
*tp
, unsigned long base
,
484 struct tc_u_hnode
*ht
,
485 struct tc_u_knode
*n
, struct rtattr
**tb
,
491 err
= tcf_exts_validate(tp
, tb
, est
, &e
, &u32_ext_map
);
496 if (tb
[TCA_U32_LINK
-1]) {
497 u32 handle
= *(u32
*)RTA_DATA(tb
[TCA_U32_LINK
-1]);
498 struct tc_u_hnode
*ht_down
= NULL
;
500 if (TC_U32_KEY(handle
))
504 ht_down
= u32_lookup_ht(ht
->tp_c
, handle
);
512 ht_down
= xchg(&n
->ht_down
, ht_down
);
518 if (tb
[TCA_U32_CLASSID
-1]) {
519 n
->res
.classid
= *(u32
*)RTA_DATA(tb
[TCA_U32_CLASSID
-1]);
520 tcf_bind_filter(tp
, &n
->res
, base
);
523 #ifdef CONFIG_NET_CLS_IND
524 if (tb
[TCA_U32_INDEV
-1]) {
525 int err
= tcf_change_indev(tp
, n
->indev
, tb
[TCA_U32_INDEV
-1]);
530 tcf_exts_change(tp
, &n
->exts
, &e
);
534 tcf_exts_destroy(tp
, &e
);
538 static int u32_change(struct tcf_proto
*tp
, unsigned long base
, u32 handle
,
542 struct tc_u_common
*tp_c
= tp
->data
;
543 struct tc_u_hnode
*ht
;
544 struct tc_u_knode
*n
;
545 struct tc_u32_sel
*s
;
546 struct rtattr
*opt
= tca
[TCA_OPTIONS
-1];
547 struct rtattr
*tb
[TCA_U32_MAX
];
552 return handle
? -EINVAL
: 0;
554 if (rtattr_parse_nested(tb
, TCA_U32_MAX
, opt
) < 0)
557 if ((n
= (struct tc_u_knode
*)*arg
) != NULL
) {
558 if (TC_U32_KEY(n
->handle
) == 0)
561 return u32_set_parms(tp
, base
, n
->ht_up
, n
, tb
, tca
[TCA_RATE
-1]);
564 if (tb
[TCA_U32_DIVISOR
-1]) {
565 unsigned divisor
= *(unsigned*)RTA_DATA(tb
[TCA_U32_DIVISOR
-1]);
567 if (--divisor
> 0x100)
569 if (TC_U32_KEY(handle
))
572 handle
= gen_new_htid(tp
->data
);
576 ht
= kmalloc(sizeof(*ht
) + divisor
*sizeof(void*), GFP_KERNEL
);
579 memset(ht
, 0, sizeof(*ht
) + divisor
*sizeof(void*));
582 ht
->divisor
= divisor
;
585 ht
->next
= tp_c
->hlist
;
587 *arg
= (unsigned long)ht
;
591 if (tb
[TCA_U32_HASH
-1]) {
592 htid
= *(unsigned*)RTA_DATA(tb
[TCA_U32_HASH
-1]);
593 if (TC_U32_HTID(htid
) == TC_U32_ROOT
) {
597 ht
= u32_lookup_ht(tp
->data
, TC_U32_HTID(htid
));
606 if (ht
->divisor
< TC_U32_HASH(htid
))
610 if (TC_U32_HTID(handle
) && TC_U32_HTID(handle
^htid
))
612 handle
= htid
| TC_U32_NODE(handle
);
614 handle
= gen_new_kid(ht
, htid
);
616 if (tb
[TCA_U32_SEL
-1] == 0 ||
617 RTA_PAYLOAD(tb
[TCA_U32_SEL
-1]) < sizeof(struct tc_u32_sel
))
620 s
= RTA_DATA(tb
[TCA_U32_SEL
-1]);
622 n
= kmalloc(sizeof(*n
) + s
->nkeys
*sizeof(struct tc_u32_key
), GFP_KERNEL
);
626 memset(n
, 0, sizeof(*n
) + s
->nkeys
*sizeof(struct tc_u32_key
));
627 #ifdef CONFIG_CLS_U32_PERF
628 n
->pf
= kmalloc(sizeof(struct tc_u32_pcnt
) + s
->nkeys
*sizeof(u64
), GFP_KERNEL
);
633 memset(n
->pf
, 0, sizeof(struct tc_u32_pcnt
) + s
->nkeys
*sizeof(u64
));
636 memcpy(&n
->sel
, s
, sizeof(*s
) + s
->nkeys
*sizeof(struct tc_u32_key
));
643 while (!(mask
& 1)) {
651 #ifdef CONFIG_CLS_U32_MARK
652 if (tb
[TCA_U32_MARK
-1]) {
653 struct tc_u32_mark
*mark
;
655 if (RTA_PAYLOAD(tb
[TCA_U32_MARK
-1]) < sizeof(struct tc_u32_mark
)) {
656 #ifdef CONFIG_CLS_U32_PERF
662 mark
= RTA_DATA(tb
[TCA_U32_MARK
-1]);
663 memcpy(&n
->mark
, mark
, sizeof(struct tc_u32_mark
));
668 err
= u32_set_parms(tp
, base
, ht
, n
, tb
, tca
[TCA_RATE
-1]);
670 struct tc_u_knode
**ins
;
671 for (ins
= &ht
->ht
[TC_U32_HASH(handle
)]; *ins
; ins
= &(*ins
)->next
)
672 if (TC_U32_NODE(handle
) < TC_U32_NODE((*ins
)->handle
))
679 *arg
= (unsigned long)n
;
682 #ifdef CONFIG_CLS_U32_PERF
690 static void u32_walk(struct tcf_proto
*tp
, struct tcf_walker
*arg
)
692 struct tc_u_common
*tp_c
= tp
->data
;
693 struct tc_u_hnode
*ht
;
694 struct tc_u_knode
*n
;
700 for (ht
= tp_c
->hlist
; ht
; ht
= ht
->next
) {
701 if (ht
->prio
!= tp
->prio
)
703 if (arg
->count
>= arg
->skip
) {
704 if (arg
->fn(tp
, (unsigned long)ht
, arg
) < 0) {
710 for (h
= 0; h
<= ht
->divisor
; h
++) {
711 for (n
= ht
->ht
[h
]; n
; n
= n
->next
) {
712 if (arg
->count
< arg
->skip
) {
716 if (arg
->fn(tp
, (unsigned long)n
, arg
) < 0) {
726 static int u32_dump(struct tcf_proto
*tp
, unsigned long fh
,
727 struct sk_buff
*skb
, struct tcmsg
*t
)
729 struct tc_u_knode
*n
= (struct tc_u_knode
*)fh
;
730 unsigned char *b
= skb
->tail
;
736 t
->tcm_handle
= n
->handle
;
738 rta
= (struct rtattr
*)b
;
739 RTA_PUT(skb
, TCA_OPTIONS
, 0, NULL
);
741 if (TC_U32_KEY(n
->handle
) == 0) {
742 struct tc_u_hnode
*ht
= (struct tc_u_hnode
*)fh
;
743 u32 divisor
= ht
->divisor
+1;
744 RTA_PUT(skb
, TCA_U32_DIVISOR
, 4, &divisor
);
746 RTA_PUT(skb
, TCA_U32_SEL
,
747 sizeof(n
->sel
) + n
->sel
.nkeys
*sizeof(struct tc_u32_key
),
750 u32 htid
= n
->handle
& 0xFFFFF000;
751 RTA_PUT(skb
, TCA_U32_HASH
, 4, &htid
);
754 RTA_PUT(skb
, TCA_U32_CLASSID
, 4, &n
->res
.classid
);
756 RTA_PUT(skb
, TCA_U32_LINK
, 4, &n
->ht_down
->handle
);
758 #ifdef CONFIG_CLS_U32_MARK
759 if (n
->mark
.val
|| n
->mark
.mask
)
760 RTA_PUT(skb
, TCA_U32_MARK
, sizeof(n
->mark
), &n
->mark
);
763 if (tcf_exts_dump(skb
, &n
->exts
, &u32_ext_map
) < 0)
766 #ifdef CONFIG_NET_CLS_IND
768 RTA_PUT(skb
, TCA_U32_INDEV
, IFNAMSIZ
, n
->indev
);
770 #ifdef CONFIG_CLS_U32_PERF
771 RTA_PUT(skb
, TCA_U32_PCNT
,
772 sizeof(struct tc_u32_pcnt
) + n
->sel
.nkeys
*sizeof(u64
),
777 rta
->rta_len
= skb
->tail
- b
;
778 if (TC_U32_KEY(n
->handle
))
779 if (tcf_exts_dump_stats(skb
, &n
->exts
, &u32_ext_map
) < 0)
784 skb_trim(skb
, b
- skb
->data
);
788 static struct tcf_proto_ops cls_u32_ops
= {
791 .classify
= u32_classify
,
793 .destroy
= u32_destroy
,
796 .change
= u32_change
,
797 .delete = u32_delete
,
800 .owner
= THIS_MODULE
,
803 static int __init
init_u32(void)
805 printk("u32 classifier\n");
806 #ifdef CONFIG_CLS_U32_PERF
807 printk(" Perfomance counters on\n");
809 #ifdef CONFIG_NET_CLS_POLICE
810 printk(" OLD policer on \n");
812 #ifdef CONFIG_NET_CLS_IND
813 printk(" input device check on \n");
815 #ifdef CONFIG_NET_CLS_ACT
816 printk(" Actions configured \n");
818 return register_tcf_proto_ops(&cls_u32_ops
);
821 static void __exit
exit_u32(void)
823 unregister_tcf_proto_ops(&cls_u32_ops
);
826 module_init(init_u32
)
827 module_exit(exit_u32
)
828 MODULE_LICENSE("GPL");