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.
28 #include <asm/uaccess.h>
29 #include <asm/system.h>
30 #include <asm/bitops.h>
31 #include <linux/config.h>
32 #include <linux/module.h>
33 #include <linux/types.h>
34 #include <linux/kernel.h>
35 #include <linux/sched.h>
36 #include <linux/string.h>
38 #include <linux/socket.h>
39 #include <linux/sockios.h>
41 #include <linux/errno.h>
42 #include <linux/interrupt.h>
43 #include <linux/if_ether.h>
44 #include <linux/inet.h>
45 #include <linux/netdevice.h>
46 #include <linux/etherdevice.h>
47 #include <linux/notifier.h>
48 #include <linux/rtnetlink.h>
50 #include <net/route.h>
51 #include <linux/skbuff.h>
53 #include <net/pkt_sched.h>
58 struct tc_u_knode
*next
;
60 struct tc_u_hnode
*ht_up
;
61 #ifdef CONFIG_NET_CLS_POLICE
62 struct tcf_police
*police
;
64 struct tcf_result res
;
65 struct tc_u_hnode
*ht_down
;
66 struct tc_u32_sel sel
;
71 struct tc_u_hnode
*next
;
73 struct tc_u_common
*tp_c
;
77 struct tc_u_knode
*ht
[1];
82 struct tc_u_common
*next
;
83 struct tc_u_hnode
*hlist
;
89 static struct tc_u_common
*u32_list
;
91 static __inline__
unsigned u32_hash_fold(u32 key
, struct tc_u32_sel
*sel
)
93 unsigned h
= key
& sel
->hmask
;
100 static int u32_classify(struct sk_buff
*skb
, struct tcf_proto
*tp
, struct tcf_result
*res
)
103 struct tc_u_knode
*knode
;
105 } stack
[TC_U32_MAXDEPTH
];
107 struct tc_u_hnode
*ht
= (struct tc_u_hnode
*)tp
->root
;
108 u8
*ptr
= skb
->nh
.raw
;
109 struct tc_u_knode
*n
;
115 #if !defined(__i386__) && !defined(__mc68000__)
116 if ((unsigned long)ptr
& 3)
125 struct tc_u32_key
*key
= n
->sel
.keys
;
127 for (i
= n
->sel
.nkeys
; i
>0; i
--, key
++) {
128 if ((*(u32
*)(ptr
+key
->off
+(off2
&key
->offmask
))^key
->val
)&key
->mask
) {
133 if (n
->ht_down
== NULL
) {
135 if (n
->sel
.flags
&TC_U32_TERMINAL
) {
137 #ifdef CONFIG_NET_CLS_POLICE
139 int pol_res
= tcf_police(skb
, n
->police
);
151 if (sdepth
>= TC_U32_MAXDEPTH
)
153 stack
[sdepth
].knode
= n
;
154 stack
[sdepth
].ptr
= ptr
;
160 sel
= ht
->divisor
&u32_hash_fold(*(u32
*)(ptr
+n
->sel
.hoff
), &n
->sel
);
162 if (!(n
->sel
.flags
&(TC_U32_VAROFFSET
|TC_U32_OFFSET
|TC_U32_EAT
)))
165 if (n
->sel
.flags
&(TC_U32_EAT
|TC_U32_VAROFFSET
)) {
166 off2
= n
->sel
.off
+ 3;
167 if (n
->sel
.flags
&TC_U32_VAROFFSET
)
168 off2
+= ntohs(n
->sel
.offmask
& *(u16
*)(ptr
+n
->sel
.offoff
)) >>n
->sel
.offshift
;
171 if (n
->sel
.flags
&TC_U32_EAT
) {
182 n
= stack
[sdepth
].knode
;
184 ptr
= stack
[sdepth
].ptr
;
191 printk("cls_u32: dead loop\n");
195 static __inline__
struct tc_u_hnode
*
196 u32_lookup_ht(struct tc_u_common
*tp_c
, u32 handle
)
198 struct tc_u_hnode
*ht
;
200 for (ht
= tp_c
->hlist
; ht
; ht
= ht
->next
)
201 if (ht
->handle
== handle
)
207 static __inline__
struct tc_u_knode
*
208 u32_lookup_key(struct tc_u_hnode
*ht
, u32 handle
)
211 struct tc_u_knode
*n
;
213 sel
= TC_U32_HASH(handle
);
214 if (sel
> ht
->divisor
)
217 for (n
= ht
->ht
[sel
]; n
; n
= n
->next
)
218 if (n
->handle
== handle
)
225 static unsigned long u32_get(struct tcf_proto
*tp
, u32 handle
)
227 struct tc_u_hnode
*ht
;
228 struct tc_u_common
*tp_c
= tp
->data
;
230 if (TC_U32_HTID(handle
) == TC_U32_ROOT
)
233 ht
= u32_lookup_ht(tp_c
, TC_U32_HTID(handle
));
238 if (TC_U32_KEY(handle
) == 0)
239 return (unsigned long)ht
;
241 return (unsigned long)u32_lookup_key(ht
, handle
);
244 static void u32_put(struct tcf_proto
*tp
, unsigned long f
)
248 static u32
gen_new_htid(struct tc_u_common
*tp_c
)
253 if (++tp_c
->hgenerator
== 0x7FF)
254 tp_c
->hgenerator
= 1;
255 } while (i
>0 && u32_lookup_ht(tp_c
, (tp_c
->hgenerator
|0x800)<<20));
257 return i
> 0 ? (tp_c
->hgenerator
|0x800)<<20 : 0;
260 static int u32_init(struct tcf_proto
*tp
)
262 struct tc_u_hnode
*root_ht
;
263 struct tc_u_common
*tp_c
;
267 for (tp_c
= u32_list
; tp_c
; tp_c
= tp_c
->next
)
268 if (tp_c
->q
== tp
->q
)
271 root_ht
= kmalloc(sizeof(*root_ht
), GFP_KERNEL
);
272 if (root_ht
== NULL
) {
276 memset(root_ht
, 0, sizeof(*root_ht
));
277 root_ht
->divisor
= 0;
279 root_ht
->handle
= tp_c
? gen_new_htid(tp_c
) : 0x80000000;
282 tp_c
= kmalloc(sizeof(*tp_c
), GFP_KERNEL
);
288 memset(tp_c
, 0, sizeof(*tp_c
));
290 tp_c
->next
= u32_list
;
295 root_ht
->next
= tp_c
->hlist
;
296 tp_c
->hlist
= root_ht
;
297 root_ht
->tp_c
= tp_c
;
304 static int u32_destroy_key(struct tcf_proto
*tp
, struct tc_u_knode
*n
)
308 if ((cl
= __cls_set_class(&n
->res
.class, 0)) != 0)
309 tp
->q
->ops
->cl_ops
->unbind_tcf(tp
->q
, cl
);
310 #ifdef CONFIG_NET_CLS_POLICE
311 tcf_police_release(n
->police
);
314 n
->ht_down
->refcnt
--;
319 static int u32_delete_key(struct tcf_proto
*tp
, struct tc_u_knode
* key
)
321 struct tc_u_knode
**kp
;
322 struct tc_u_hnode
*ht
= key
->ht_up
;
325 for (kp
= &ht
->ht
[TC_U32_HASH(key
->handle
)]; *kp
; kp
= &(*kp
)->next
) {
331 u32_destroy_key(tp
, key
);
340 static void u32_clear_hnode(struct tcf_proto
*tp
, struct tc_u_hnode
*ht
)
342 struct tc_u_knode
*n
;
345 for (h
=0; h
<=ht
->divisor
; h
++) {
346 while ((n
= ht
->ht
[h
]) != NULL
) {
349 u32_destroy_key(tp
, n
);
354 static int u32_destroy_hnode(struct tcf_proto
*tp
, struct tc_u_hnode
*ht
)
356 struct tc_u_common
*tp_c
= tp
->data
;
357 struct tc_u_hnode
**hn
;
359 BUG_TRAP(!ht
->refcnt
);
361 u32_clear_hnode(tp
, ht
);
363 for (hn
= &tp_c
->hlist
; *hn
; hn
= &(*hn
)->next
) {
375 static void u32_destroy(struct tcf_proto
*tp
)
377 struct tc_u_common
*tp_c
= tp
->data
;
378 struct tc_u_hnode
*root_ht
= xchg(&tp
->root
, NULL
);
380 BUG_TRAP(root_ht
!= NULL
);
382 if (root_ht
&& --root_ht
->refcnt
== 0)
383 u32_destroy_hnode(tp
, root_ht
);
385 if (--tp_c
->refcnt
== 0) {
386 struct tc_u_hnode
*ht
;
387 struct tc_u_common
**tp_cp
;
389 for (tp_cp
= &u32_list
; *tp_cp
; tp_cp
= &(*tp_cp
)->next
) {
390 if (*tp_cp
== tp_c
) {
396 for (ht
=tp_c
->hlist
; ht
; ht
= ht
->next
)
397 u32_clear_hnode(tp
, ht
);
399 while ((ht
= tp_c
->hlist
) != NULL
) {
400 tp_c
->hlist
= ht
->next
;
402 BUG_TRAP(ht
->refcnt
== 0);
414 static int u32_delete(struct tcf_proto
*tp
, unsigned long arg
)
416 struct tc_u_hnode
*ht
= (struct tc_u_hnode
*)arg
;
421 if (TC_U32_KEY(ht
->handle
))
422 return u32_delete_key(tp
, (struct tc_u_knode
*)ht
);
427 if (--ht
->refcnt
== 0)
428 u32_destroy_hnode(tp
, ht
);
433 static u32
gen_new_kid(struct tc_u_hnode
*ht
, u32 handle
)
435 struct tc_u_knode
*n
;
438 for (n
=ht
->ht
[TC_U32_HASH(handle
)]; n
; n
= n
->next
)
439 if (i
< TC_U32_NODE(n
->handle
))
440 i
= TC_U32_NODE(n
->handle
);
443 return handle
|(i
>0xFFF ? 0xFFF : i
);
446 static int u32_set_parms(struct Qdisc
*q
, unsigned long base
,
447 struct tc_u_hnode
*ht
,
448 struct tc_u_knode
*n
, struct rtattr
**tb
,
451 if (tb
[TCA_U32_LINK
-1]) {
452 u32 handle
= *(u32
*)RTA_DATA(tb
[TCA_U32_LINK
-1]);
453 struct tc_u_hnode
*ht_down
= NULL
;
455 if (TC_U32_KEY(handle
))
459 ht_down
= u32_lookup_ht(ht
->tp_c
, handle
);
467 ht_down
= xchg(&n
->ht_down
, ht_down
);
473 if (tb
[TCA_U32_CLASSID
-1]) {
476 n
->res
.classid
= *(u32
*)RTA_DATA(tb
[TCA_U32_CLASSID
-1]);
478 cl
= __cls_set_class(&n
->res
.class, q
->ops
->cl_ops
->bind_tcf(q
, base
, n
->res
.classid
));
481 q
->ops
->cl_ops
->unbind_tcf(q
, cl
);
483 #ifdef CONFIG_NET_CLS_POLICE
484 if (tb
[TCA_U32_POLICE
-1]) {
485 struct tcf_police
*police
= tcf_police_locate(tb
[TCA_U32_POLICE
-1], est
);
488 police
= xchg(&n
->police
, police
);
491 tcf_police_release(police
);
497 static int u32_change(struct tcf_proto
*tp
, unsigned long base
, u32 handle
,
501 struct tc_u_common
*tp_c
= tp
->data
;
502 struct tc_u_hnode
*ht
;
503 struct tc_u_knode
*n
;
504 struct tc_u32_sel
*s
;
505 struct rtattr
*opt
= tca
[TCA_OPTIONS
-1];
506 struct rtattr
*tb
[TCA_U32_MAX
];
511 return handle
? -EINVAL
: 0;
513 if (rtattr_parse(tb
, TCA_U32_MAX
, RTA_DATA(opt
), RTA_PAYLOAD(opt
)) < 0)
516 if ((n
= (struct tc_u_knode
*)*arg
) != NULL
) {
517 if (TC_U32_KEY(n
->handle
) == 0)
520 return u32_set_parms(tp
->q
, base
, n
->ht_up
, n
, tb
, tca
[TCA_RATE
-1]);
523 if (tb
[TCA_U32_DIVISOR
-1]) {
524 unsigned divisor
= *(unsigned*)RTA_DATA(tb
[TCA_U32_DIVISOR
-1]);
526 if (--divisor
> 0x100)
528 if (TC_U32_KEY(handle
))
531 handle
= gen_new_htid(tp
->data
);
535 ht
= kmalloc(sizeof(*ht
) + divisor
*sizeof(void*), GFP_KERNEL
);
538 memset(ht
, 0, sizeof(*ht
) + divisor
*sizeof(void*));
541 ht
->divisor
= divisor
;
543 ht
->next
= tp_c
->hlist
;
545 *arg
= (unsigned long)ht
;
549 if (tb
[TCA_U32_HASH
-1]) {
550 htid
= *(unsigned*)RTA_DATA(tb
[TCA_U32_HASH
-1]);
551 if (TC_U32_HTID(htid
) == TC_U32_ROOT
) {
555 ht
= u32_lookup_ht(tp
->data
, TC_U32_HTID(htid
));
564 if (ht
->divisor
< TC_U32_HASH(htid
))
568 if (TC_U32_HTID(handle
) && TC_U32_HTID(handle
^htid
))
570 handle
= htid
| TC_U32_NODE(handle
);
572 handle
= gen_new_kid(ht
, htid
);
574 if (tb
[TCA_U32_SEL
-1] == 0 ||
575 RTA_PAYLOAD(tb
[TCA_U32_SEL
-1]) < sizeof(struct tc_u32_sel
))
578 s
= RTA_DATA(tb
[TCA_U32_SEL
-1]);
579 n
= kmalloc(sizeof(*n
) + s
->nkeys
*sizeof(struct tc_u32_key
), GFP_KERNEL
);
582 memset(n
, 0, sizeof(*n
) + s
->nkeys
*sizeof(struct tc_u32_key
));
583 memcpy(&n
->sel
, s
, sizeof(*s
) + s
->nkeys
*sizeof(struct tc_u32_key
));
586 err
= u32_set_parms(tp
->q
, base
, ht
, n
, tb
, tca
[TCA_RATE
-1]);
588 struct tc_u_knode
**ins
;
589 for (ins
= &ht
->ht
[TC_U32_HASH(handle
)]; *ins
; ins
= &(*ins
)->next
)
590 if (TC_U32_NODE(handle
) < TC_U32_NODE((*ins
)->handle
))
597 *arg
= (unsigned long)n
;
604 static void u32_walk(struct tcf_proto
*tp
, struct tcf_walker
*arg
)
606 struct tc_u_common
*tp_c
= tp
->data
;
607 struct tc_u_hnode
*ht
;
608 struct tc_u_knode
*n
;
614 for (ht
= tp_c
->hlist
; ht
; ht
= ht
->next
) {
615 if (arg
->count
>= arg
->skip
) {
616 if (arg
->fn(tp
, (unsigned long)ht
, arg
) < 0) {
622 for (h
= 0; h
<= ht
->divisor
; h
++) {
623 for (n
= ht
->ht
[h
]; n
; n
= n
->next
) {
624 if (arg
->count
< arg
->skip
) {
628 if (arg
->fn(tp
, (unsigned long)n
, arg
) < 0) {
638 #ifdef CONFIG_RTNETLINK
639 static int u32_dump(struct tcf_proto
*tp
, unsigned long fh
,
640 struct sk_buff
*skb
, struct tcmsg
*t
)
642 struct tc_u_knode
*n
= (struct tc_u_knode
*)fh
;
643 unsigned char *b
= skb
->tail
;
649 t
->tcm_handle
= n
->handle
;
651 rta
= (struct rtattr
*)b
;
652 RTA_PUT(skb
, TCA_OPTIONS
, 0, NULL
);
654 if (TC_U32_KEY(n
->handle
) == 0) {
655 struct tc_u_hnode
*ht
= (struct tc_u_hnode
*)fh
;
656 u32 divisor
= ht
->divisor
+1;
657 RTA_PUT(skb
, TCA_U32_DIVISOR
, 4, &divisor
);
659 RTA_PUT(skb
, TCA_U32_SEL
,
660 sizeof(n
->sel
) + n
->sel
.nkeys
*sizeof(struct tc_u32_key
),
663 u32 htid
= n
->handle
& 0xFFFFF000;
664 RTA_PUT(skb
, TCA_U32_HASH
, 4, &htid
);
667 RTA_PUT(skb
, TCA_U32_CLASSID
, 4, &n
->res
.classid
);
669 RTA_PUT(skb
, TCA_U32_LINK
, 4, &n
->ht_down
->handle
);
670 #ifdef CONFIG_NET_CLS_POLICE
672 struct rtattr
* p_rta
= (struct rtattr
*)skb
->tail
;
674 RTA_PUT(skb
, TCA_U32_POLICE
, 0, NULL
);
676 if (tcf_police_dump(skb
, n
->police
) < 0)
679 p_rta
->rta_len
= skb
->tail
- (u8
*)p_rta
;
684 rta
->rta_len
= skb
->tail
- b
;
685 #ifdef CONFIG_NET_CLS_POLICE
686 if (TC_U32_KEY(n
->handle
) && n
->police
) {
687 if (qdisc_copy_stats(skb
, &n
->police
->stats
))
694 skb_trim(skb
, b
- skb
->data
);
699 struct tcf_proto_ops cls_u32_ops
= {
711 #ifdef CONFIG_RTNETLINK
719 int init_module(void)
721 return register_tcf_proto_ops(&cls_u32_ops
);
724 void cleanup_module(void)
726 unregister_tcf_proto_ops(&cls_u32_ops
);