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/route.h>
54 #include <linux/skbuff.h>
56 #include <net/act_api.h>
57 #include <net/pkt_cls.h>
61 struct tc_u_knode
*next
;
63 struct tc_u_hnode
*ht_up
;
65 #ifdef CONFIG_NET_CLS_IND
69 struct tcf_result res
;
70 struct tc_u_hnode
*ht_down
;
71 #ifdef CONFIG_CLS_U32_PERF
72 struct tc_u32_pcnt
*pf
;
74 #ifdef CONFIG_CLS_U32_MARK
75 struct tc_u32_mark mark
;
77 struct tc_u32_sel sel
;
82 struct tc_u_hnode
*next
;
85 struct tc_u_common
*tp_c
;
88 struct tc_u_knode
*ht
[1];
93 struct tc_u_common
*next
;
94 struct tc_u_hnode
*hlist
;
100 static struct tcf_ext_map u32_ext_map
= {
101 .action
= TCA_U32_ACT
,
102 .police
= TCA_U32_POLICE
105 static struct tc_u_common
*u32_list
;
107 static __inline__
unsigned u32_hash_fold(u32 key
, struct tc_u32_sel
*sel
, u8 fshift
)
109 unsigned h
= (key
& sel
->hmask
)>>fshift
;
114 static int u32_classify(struct sk_buff
*skb
, struct tcf_proto
*tp
, struct tcf_result
*res
)
117 struct tc_u_knode
*knode
;
119 } stack
[TC_U32_MAXDEPTH
];
121 struct tc_u_hnode
*ht
= (struct tc_u_hnode
*)tp
->root
;
122 u8
*ptr
= skb
->nh
.raw
;
123 struct tc_u_knode
*n
;
127 #ifdef CONFIG_CLS_U32_PERF
137 struct tc_u32_key
*key
= n
->sel
.keys
;
139 #ifdef CONFIG_CLS_U32_PERF
144 #ifdef CONFIG_CLS_U32_MARK
145 if ((skb
->mark
& n
->mark
.mask
) != n
->mark
.val
) {
153 for (i
= n
->sel
.nkeys
; i
>0; i
--, key
++) {
155 if ((*(u32
*)(ptr
+key
->off
+(off2
&key
->offmask
))^key
->val
)&key
->mask
) {
159 #ifdef CONFIG_CLS_U32_PERF
164 if (n
->ht_down
== NULL
) {
166 if (n
->sel
.flags
&TC_U32_TERMINAL
) {
169 #ifdef CONFIG_NET_CLS_IND
170 if (!tcf_match_indev(skb
, n
->indev
)) {
175 #ifdef CONFIG_CLS_U32_PERF
178 r
= tcf_exts_exec(skb
, &n
->exts
, res
);
191 if (sdepth
>= TC_U32_MAXDEPTH
)
193 stack
[sdepth
].knode
= n
;
194 stack
[sdepth
].ptr
= ptr
;
200 sel
= ht
->divisor
&u32_hash_fold(*(u32
*)(ptr
+n
->sel
.hoff
), &n
->sel
,n
->fshift
);
202 if (!(n
->sel
.flags
&(TC_U32_VAROFFSET
|TC_U32_OFFSET
|TC_U32_EAT
)))
205 if (n
->sel
.flags
&(TC_U32_OFFSET
|TC_U32_VAROFFSET
)) {
206 off2
= n
->sel
.off
+ 3;
207 if (n
->sel
.flags
&TC_U32_VAROFFSET
)
208 off2
+= ntohs(n
->sel
.offmask
& *(u16
*)(ptr
+n
->sel
.offoff
)) >>n
->sel
.offshift
;
211 if (n
->sel
.flags
&TC_U32_EAT
) {
222 n
= stack
[sdepth
].knode
;
224 ptr
= stack
[sdepth
].ptr
;
231 printk("cls_u32: dead loop\n");
235 static __inline__
struct tc_u_hnode
*
236 u32_lookup_ht(struct tc_u_common
*tp_c
, u32 handle
)
238 struct tc_u_hnode
*ht
;
240 for (ht
= tp_c
->hlist
; ht
; ht
= ht
->next
)
241 if (ht
->handle
== handle
)
247 static __inline__
struct tc_u_knode
*
248 u32_lookup_key(struct tc_u_hnode
*ht
, u32 handle
)
251 struct tc_u_knode
*n
= NULL
;
253 sel
= TC_U32_HASH(handle
);
254 if (sel
> ht
->divisor
)
257 for (n
= ht
->ht
[sel
]; n
; n
= n
->next
)
258 if (n
->handle
== handle
)
265 static unsigned long u32_get(struct tcf_proto
*tp
, u32 handle
)
267 struct tc_u_hnode
*ht
;
268 struct tc_u_common
*tp_c
= tp
->data
;
270 if (TC_U32_HTID(handle
) == TC_U32_ROOT
)
273 ht
= u32_lookup_ht(tp_c
, TC_U32_HTID(handle
));
278 if (TC_U32_KEY(handle
) == 0)
279 return (unsigned long)ht
;
281 return (unsigned long)u32_lookup_key(ht
, handle
);
284 static void u32_put(struct tcf_proto
*tp
, unsigned long f
)
288 static u32
gen_new_htid(struct tc_u_common
*tp_c
)
293 if (++tp_c
->hgenerator
== 0x7FF)
294 tp_c
->hgenerator
= 1;
295 } while (--i
>0 && u32_lookup_ht(tp_c
, (tp_c
->hgenerator
|0x800)<<20));
297 return i
> 0 ? (tp_c
->hgenerator
|0x800)<<20 : 0;
300 static int u32_init(struct tcf_proto
*tp
)
302 struct tc_u_hnode
*root_ht
;
303 struct tc_u_common
*tp_c
;
305 for (tp_c
= u32_list
; tp_c
; tp_c
= tp_c
->next
)
306 if (tp_c
->q
== tp
->q
)
309 root_ht
= kzalloc(sizeof(*root_ht
), GFP_KERNEL
);
313 root_ht
->divisor
= 0;
315 root_ht
->handle
= tp_c
? gen_new_htid(tp_c
) : 0x80000000;
316 root_ht
->prio
= tp
->prio
;
319 tp_c
= kzalloc(sizeof(*tp_c
), GFP_KERNEL
);
325 tp_c
->next
= u32_list
;
330 root_ht
->next
= tp_c
->hlist
;
331 tp_c
->hlist
= root_ht
;
332 root_ht
->tp_c
= tp_c
;
339 static int u32_destroy_key(struct tcf_proto
*tp
, struct tc_u_knode
*n
)
341 tcf_unbind_filter(tp
, &n
->res
);
342 tcf_exts_destroy(tp
, &n
->exts
);
344 n
->ht_down
->refcnt
--;
345 #ifdef CONFIG_CLS_U32_PERF
352 static int u32_delete_key(struct tcf_proto
*tp
, struct tc_u_knode
* key
)
354 struct tc_u_knode
**kp
;
355 struct tc_u_hnode
*ht
= key
->ht_up
;
358 for (kp
= &ht
->ht
[TC_U32_HASH(key
->handle
)]; *kp
; kp
= &(*kp
)->next
) {
364 u32_destroy_key(tp
, key
);
373 static void u32_clear_hnode(struct tcf_proto
*tp
, struct tc_u_hnode
*ht
)
375 struct tc_u_knode
*n
;
378 for (h
=0; h
<=ht
->divisor
; h
++) {
379 while ((n
= ht
->ht
[h
]) != NULL
) {
382 u32_destroy_key(tp
, n
);
387 static int u32_destroy_hnode(struct tcf_proto
*tp
, struct tc_u_hnode
*ht
)
389 struct tc_u_common
*tp_c
= tp
->data
;
390 struct tc_u_hnode
**hn
;
392 BUG_TRAP(!ht
->refcnt
);
394 u32_clear_hnode(tp
, ht
);
396 for (hn
= &tp_c
->hlist
; *hn
; hn
= &(*hn
)->next
) {
408 static void u32_destroy(struct tcf_proto
*tp
)
410 struct tc_u_common
*tp_c
= tp
->data
;
411 struct tc_u_hnode
*root_ht
= xchg(&tp
->root
, NULL
);
413 BUG_TRAP(root_ht
!= NULL
);
415 if (root_ht
&& --root_ht
->refcnt
== 0)
416 u32_destroy_hnode(tp
, root_ht
);
418 if (--tp_c
->refcnt
== 0) {
419 struct tc_u_hnode
*ht
;
420 struct tc_u_common
**tp_cp
;
422 for (tp_cp
= &u32_list
; *tp_cp
; tp_cp
= &(*tp_cp
)->next
) {
423 if (*tp_cp
== tp_c
) {
429 for (ht
=tp_c
->hlist
; ht
; ht
= ht
->next
)
430 u32_clear_hnode(tp
, ht
);
432 while ((ht
= tp_c
->hlist
) != NULL
) {
433 tp_c
->hlist
= ht
->next
;
435 BUG_TRAP(ht
->refcnt
== 0);
446 static int u32_delete(struct tcf_proto
*tp
, unsigned long arg
)
448 struct tc_u_hnode
*ht
= (struct tc_u_hnode
*)arg
;
453 if (TC_U32_KEY(ht
->handle
))
454 return u32_delete_key(tp
, (struct tc_u_knode
*)ht
);
459 if (--ht
->refcnt
== 0)
460 u32_destroy_hnode(tp
, ht
);
465 static u32
gen_new_kid(struct tc_u_hnode
*ht
, u32 handle
)
467 struct tc_u_knode
*n
;
470 for (n
=ht
->ht
[TC_U32_HASH(handle
)]; n
; n
= n
->next
)
471 if (i
< TC_U32_NODE(n
->handle
))
472 i
= TC_U32_NODE(n
->handle
);
475 return handle
|(i
>0xFFF ? 0xFFF : i
);
478 static int u32_set_parms(struct tcf_proto
*tp
, unsigned long base
,
479 struct tc_u_hnode
*ht
,
480 struct tc_u_knode
*n
, struct rtattr
**tb
,
486 err
= tcf_exts_validate(tp
, tb
, est
, &e
, &u32_ext_map
);
491 if (tb
[TCA_U32_LINK
-1]) {
492 u32 handle
= *(u32
*)RTA_DATA(tb
[TCA_U32_LINK
-1]);
493 struct tc_u_hnode
*ht_down
= NULL
;
495 if (TC_U32_KEY(handle
))
499 ht_down
= u32_lookup_ht(ht
->tp_c
, handle
);
507 ht_down
= xchg(&n
->ht_down
, ht_down
);
513 if (tb
[TCA_U32_CLASSID
-1]) {
514 n
->res
.classid
= *(u32
*)RTA_DATA(tb
[TCA_U32_CLASSID
-1]);
515 tcf_bind_filter(tp
, &n
->res
, base
);
518 #ifdef CONFIG_NET_CLS_IND
519 if (tb
[TCA_U32_INDEV
-1]) {
520 int err
= tcf_change_indev(tp
, n
->indev
, tb
[TCA_U32_INDEV
-1]);
525 tcf_exts_change(tp
, &n
->exts
, &e
);
529 tcf_exts_destroy(tp
, &e
);
533 static int u32_change(struct tcf_proto
*tp
, unsigned long base
, u32 handle
,
537 struct tc_u_common
*tp_c
= tp
->data
;
538 struct tc_u_hnode
*ht
;
539 struct tc_u_knode
*n
;
540 struct tc_u32_sel
*s
;
541 struct rtattr
*opt
= tca
[TCA_OPTIONS
-1];
542 struct rtattr
*tb
[TCA_U32_MAX
];
547 return handle
? -EINVAL
: 0;
549 if (rtattr_parse_nested(tb
, TCA_U32_MAX
, opt
) < 0)
552 if ((n
= (struct tc_u_knode
*)*arg
) != NULL
) {
553 if (TC_U32_KEY(n
->handle
) == 0)
556 return u32_set_parms(tp
, base
, n
->ht_up
, n
, tb
, tca
[TCA_RATE
-1]);
559 if (tb
[TCA_U32_DIVISOR
-1]) {
560 unsigned divisor
= *(unsigned*)RTA_DATA(tb
[TCA_U32_DIVISOR
-1]);
562 if (--divisor
> 0x100)
564 if (TC_U32_KEY(handle
))
567 handle
= gen_new_htid(tp
->data
);
571 ht
= kzalloc(sizeof(*ht
) + divisor
*sizeof(void*), GFP_KERNEL
);
576 ht
->divisor
= divisor
;
579 ht
->next
= tp_c
->hlist
;
581 *arg
= (unsigned long)ht
;
585 if (tb
[TCA_U32_HASH
-1]) {
586 htid
= *(unsigned*)RTA_DATA(tb
[TCA_U32_HASH
-1]);
587 if (TC_U32_HTID(htid
) == TC_U32_ROOT
) {
591 ht
= u32_lookup_ht(tp
->data
, TC_U32_HTID(htid
));
600 if (ht
->divisor
< TC_U32_HASH(htid
))
604 if (TC_U32_HTID(handle
) && TC_U32_HTID(handle
^htid
))
606 handle
= htid
| TC_U32_NODE(handle
);
608 handle
= gen_new_kid(ht
, htid
);
610 if (tb
[TCA_U32_SEL
-1] == 0 ||
611 RTA_PAYLOAD(tb
[TCA_U32_SEL
-1]) < sizeof(struct tc_u32_sel
))
614 s
= RTA_DATA(tb
[TCA_U32_SEL
-1]);
616 n
= kzalloc(sizeof(*n
) + s
->nkeys
*sizeof(struct tc_u32_key
), GFP_KERNEL
);
620 #ifdef CONFIG_CLS_U32_PERF
621 n
->pf
= kzalloc(sizeof(struct tc_u32_pcnt
) + s
->nkeys
*sizeof(u64
), GFP_KERNEL
);
628 memcpy(&n
->sel
, s
, sizeof(*s
) + s
->nkeys
*sizeof(struct tc_u32_key
));
635 while (!(mask
& 1)) {
643 #ifdef CONFIG_CLS_U32_MARK
644 if (tb
[TCA_U32_MARK
-1]) {
645 struct tc_u32_mark
*mark
;
647 if (RTA_PAYLOAD(tb
[TCA_U32_MARK
-1]) < sizeof(struct tc_u32_mark
)) {
648 #ifdef CONFIG_CLS_U32_PERF
654 mark
= RTA_DATA(tb
[TCA_U32_MARK
-1]);
655 memcpy(&n
->mark
, mark
, sizeof(struct tc_u32_mark
));
660 err
= u32_set_parms(tp
, base
, ht
, n
, tb
, tca
[TCA_RATE
-1]);
662 struct tc_u_knode
**ins
;
663 for (ins
= &ht
->ht
[TC_U32_HASH(handle
)]; *ins
; ins
= &(*ins
)->next
)
664 if (TC_U32_NODE(handle
) < TC_U32_NODE((*ins
)->handle
))
671 *arg
= (unsigned long)n
;
674 #ifdef CONFIG_CLS_U32_PERF
681 static void u32_walk(struct tcf_proto
*tp
, struct tcf_walker
*arg
)
683 struct tc_u_common
*tp_c
= tp
->data
;
684 struct tc_u_hnode
*ht
;
685 struct tc_u_knode
*n
;
691 for (ht
= tp_c
->hlist
; ht
; ht
= ht
->next
) {
692 if (ht
->prio
!= tp
->prio
)
694 if (arg
->count
>= arg
->skip
) {
695 if (arg
->fn(tp
, (unsigned long)ht
, arg
) < 0) {
701 for (h
= 0; h
<= ht
->divisor
; h
++) {
702 for (n
= ht
->ht
[h
]; n
; n
= n
->next
) {
703 if (arg
->count
< arg
->skip
) {
707 if (arg
->fn(tp
, (unsigned long)n
, arg
) < 0) {
717 static int u32_dump(struct tcf_proto
*tp
, unsigned long fh
,
718 struct sk_buff
*skb
, struct tcmsg
*t
)
720 struct tc_u_knode
*n
= (struct tc_u_knode
*)fh
;
721 unsigned char *b
= skb
->tail
;
727 t
->tcm_handle
= n
->handle
;
729 rta
= (struct rtattr
*)b
;
730 RTA_PUT(skb
, TCA_OPTIONS
, 0, NULL
);
732 if (TC_U32_KEY(n
->handle
) == 0) {
733 struct tc_u_hnode
*ht
= (struct tc_u_hnode
*)fh
;
734 u32 divisor
= ht
->divisor
+1;
735 RTA_PUT(skb
, TCA_U32_DIVISOR
, 4, &divisor
);
737 RTA_PUT(skb
, TCA_U32_SEL
,
738 sizeof(n
->sel
) + n
->sel
.nkeys
*sizeof(struct tc_u32_key
),
741 u32 htid
= n
->handle
& 0xFFFFF000;
742 RTA_PUT(skb
, TCA_U32_HASH
, 4, &htid
);
745 RTA_PUT(skb
, TCA_U32_CLASSID
, 4, &n
->res
.classid
);
747 RTA_PUT(skb
, TCA_U32_LINK
, 4, &n
->ht_down
->handle
);
749 #ifdef CONFIG_CLS_U32_MARK
750 if (n
->mark
.val
|| n
->mark
.mask
)
751 RTA_PUT(skb
, TCA_U32_MARK
, sizeof(n
->mark
), &n
->mark
);
754 if (tcf_exts_dump(skb
, &n
->exts
, &u32_ext_map
) < 0)
757 #ifdef CONFIG_NET_CLS_IND
759 RTA_PUT(skb
, TCA_U32_INDEV
, IFNAMSIZ
, n
->indev
);
761 #ifdef CONFIG_CLS_U32_PERF
762 RTA_PUT(skb
, TCA_U32_PCNT
,
763 sizeof(struct tc_u32_pcnt
) + n
->sel
.nkeys
*sizeof(u64
),
768 rta
->rta_len
= skb
->tail
- b
;
769 if (TC_U32_KEY(n
->handle
))
770 if (tcf_exts_dump_stats(skb
, &n
->exts
, &u32_ext_map
) < 0)
775 skb_trim(skb
, b
- skb
->data
);
779 static struct tcf_proto_ops cls_u32_ops
= {
782 .classify
= u32_classify
,
784 .destroy
= u32_destroy
,
787 .change
= u32_change
,
788 .delete = u32_delete
,
791 .owner
= THIS_MODULE
,
794 static int __init
init_u32(void)
796 printk("u32 classifier\n");
797 #ifdef CONFIG_CLS_U32_PERF
798 printk(" Performance counters on\n");
800 #ifdef CONFIG_NET_CLS_POLICE
801 printk(" OLD policer on \n");
803 #ifdef CONFIG_NET_CLS_IND
804 printk(" input device check on \n");
806 #ifdef CONFIG_NET_CLS_ACT
807 printk(" Actions configured \n");
809 return register_tcf_proto_ops(&cls_u32_ops
);
812 static void __exit
exit_u32(void)
814 unregister_tcf_proto_ops(&cls_u32_ops
);
817 module_init(init_u32
)
818 module_exit(exit_u32
)
819 MODULE_LICENSE("GPL");