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;
120 struct tc_u32_key *key = n->sel.keys;
122 for (i = n->sel.nkeys; i>0; i--, key++) {
123 if ((*(u32*)(ptr+key->off+(off2&key->offmask))^key->val)&key->mask) {
128 if (n->ht_down == NULL) {
130 if (n->sel.flags&TC_U32_TERMINAL) {
132 #ifdef CONFIG_NET_CLS_POLICE
134 int pol_res = tcf_police(skb, n->police);
146 if (sdepth >= TC_U32_MAXDEPTH)
148 stack[sdepth].knode = n;
149 stack[sdepth].ptr = ptr;
155 sel = ht->divisor&u32_hash_fold(*(u32*)(ptr+n->sel.hoff), &n->sel);
157 if (!(n->sel.flags&(TC_U32_VAROFFSET|TC_U32_OFFSET|TC_U32_EAT)))
160 if (n->sel.flags&(TC_U32_OFFSET|TC_U32_VAROFFSET)) {
161 off2 = n->sel.off + 3;
162 if (n->sel.flags&TC_U32_VAROFFSET)
163 off2 += ntohs(n->sel.offmask & *(u16*)(ptr+n->sel.offoff)) >>n->sel.offshift;
166 if (n->sel.flags&TC_U32_EAT) {
177 n = stack[sdepth].knode;
179 ptr = stack[sdepth].ptr;
186 printk("cls_u32: dead loop\n");
190 static __inline__ struct tc_u_hnode *
191 u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
193 struct tc_u_hnode *ht;
195 for (ht = tp_c->hlist; ht; ht = ht->next)
196 if (ht->handle == handle)
202 static __inline__ struct tc_u_knode *
203 u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
206 struct tc_u_knode *n = NULL;
208 sel = TC_U32_HASH(handle);
209 if (sel > ht->divisor)
212 for (n = ht->ht[sel]; n; n = n->next)
213 if (n->handle == handle)
220 static unsigned long u32_get(struct tcf_proto *tp, u32 handle)
222 struct tc_u_hnode *ht;
223 struct tc_u_common *tp_c = tp->data;
225 if (TC_U32_HTID(handle) == TC_U32_ROOT)
228 ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
233 if (TC_U32_KEY(handle) == 0)
234 return (unsigned long)ht;
236 return (unsigned long)u32_lookup_key(ht, handle);
239 static void u32_put(struct tcf_proto *tp, unsigned long f)
243 static u32 gen_new_htid(struct tc_u_common *tp_c)
248 if (++tp_c->hgenerator == 0x7FF)
249 tp_c->hgenerator = 1;
250 } while (--i>0 && u32_lookup_ht(tp_c, (tp_c->hgenerator|0x800)<<20));
252 return i > 0 ? (tp_c->hgenerator|0x800)<<20 : 0;
255 static int u32_init(struct tcf_proto *tp)
257 struct tc_u_hnode *root_ht;
258 struct tc_u_common *tp_c;
260 for (tp_c = u32_list; tp_c; tp_c = tp_c->next)
261 if (tp_c->q == tp->q)
264 root_ht = kmalloc(sizeof(*root_ht), GFP_KERNEL);
268 memset(root_ht, 0, sizeof(*root_ht));
269 root_ht->divisor = 0;
271 root_ht->handle = tp_c ? gen_new_htid(tp_c) : 0x80000000;
274 tp_c = kmalloc(sizeof(*tp_c), GFP_KERNEL);
279 memset(tp_c, 0, sizeof(*tp_c));
281 tp_c->next = u32_list;
286 root_ht->next = tp_c->hlist;
287 tp_c->hlist = root_ht;
288 root_ht->tp_c = tp_c;
295 static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n)
299 if ((cl = __cls_set_class(&n->res.class, 0)) != 0)
300 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
301 #ifdef CONFIG_NET_CLS_POLICE
302 tcf_police_release(n->police);
305 n->ht_down->refcnt--;
310 static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode* key)
312 struct tc_u_knode **kp;
313 struct tc_u_hnode *ht = key->ht_up;
316 for (kp = &ht->ht[TC_U32_HASH(key->handle)]; *kp; kp = &(*kp)->next) {
322 u32_destroy_key(tp, key);
331 static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
333 struct tc_u_knode *n;
336 for (h=0; h<=ht->divisor; h++) {
337 while ((n = ht->ht[h]) != NULL) {
340 u32_destroy_key(tp, n);
345 static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
347 struct tc_u_common *tp_c = tp->data;
348 struct tc_u_hnode **hn;
350 BUG_TRAP(!ht->refcnt);
352 u32_clear_hnode(tp, ht);
354 for (hn = &tp_c->hlist; *hn; hn = &(*hn)->next) {
366 static void u32_destroy(struct tcf_proto *tp)
368 struct tc_u_common *tp_c = tp->data;
369 struct tc_u_hnode *root_ht = xchg(&tp->root, NULL);
371 BUG_TRAP(root_ht != NULL);
373 if (root_ht && --root_ht->refcnt == 0)
374 u32_destroy_hnode(tp, root_ht);
376 if (--tp_c->refcnt == 0) {
377 struct tc_u_hnode *ht;
378 struct tc_u_common **tp_cp;
380 for (tp_cp = &u32_list; *tp_cp; tp_cp = &(*tp_cp)->next) {
381 if (*tp_cp == tp_c) {
387 for (ht=tp_c->hlist; ht; ht = ht->next)
388 u32_clear_hnode(tp, ht);
390 while ((ht = tp_c->hlist) != NULL) {
391 tp_c->hlist = ht->next;
393 BUG_TRAP(ht->refcnt == 0);
404 static int u32_delete(struct tcf_proto *tp, unsigned long arg)
406 struct tc_u_hnode *ht = (struct tc_u_hnode*)arg;
411 if (TC_U32_KEY(ht->handle))
412 return u32_delete_key(tp, (struct tc_u_knode*)ht);
417 if (--ht->refcnt == 0)
418 u32_destroy_hnode(tp, ht);
423 static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
425 struct tc_u_knode *n;
428 for (n=ht->ht[TC_U32_HASH(handle)]; n; n = n->next)
429 if (i < TC_U32_NODE(n->handle))
430 i = TC_U32_NODE(n->handle);
433 return handle|(i>0xFFF ? 0xFFF : i);
436 static int u32_set_parms(struct Qdisc *q, unsigned long base,
437 struct tc_u_hnode *ht,
438 struct tc_u_knode *n, struct rtattr **tb,
441 if (tb[TCA_U32_LINK-1]) {
442 u32 handle = *(u32*)RTA_DATA(tb[TCA_U32_LINK-1]);
443 struct tc_u_hnode *ht_down = NULL;
445 if (TC_U32_KEY(handle))
449 ht_down = u32_lookup_ht(ht->tp_c, handle);
457 ht_down = xchg(&n->ht_down, ht_down);
463 if (tb[TCA_U32_CLASSID-1]) {
466 n->res.classid = *(u32*)RTA_DATA(tb[TCA_U32_CLASSID-1]);
468 cl = __cls_set_class(&n->res.class, q->ops->cl_ops->bind_tcf(q, base, n->res.classid));
471 q->ops->cl_ops->unbind_tcf(q, cl);
473 #ifdef CONFIG_NET_CLS_POLICE
474 if (tb[TCA_U32_POLICE-1]) {
475 struct tcf_police *police = tcf_police_locate(tb[TCA_U32_POLICE-1], est);
478 police = xchg(&n->police, police);
481 tcf_police_release(police);
487 static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
491 struct tc_u_common *tp_c = tp->data;
492 struct tc_u_hnode *ht;
493 struct tc_u_knode *n;
494 struct tc_u32_sel *s;
495 struct rtattr *opt = tca[TCA_OPTIONS-1];
496 struct rtattr *tb[TCA_U32_MAX];
501 return handle ? -EINVAL : 0;
503 if (rtattr_parse(tb, TCA_U32_MAX, RTA_DATA(opt), RTA_PAYLOAD(opt)) < 0)
506 if ((n = (struct tc_u_knode*)*arg) != NULL) {
507 if (TC_U32_KEY(n->handle) == 0)
510 return u32_set_parms(tp->q, base, n->ht_up, n, tb, tca[TCA_RATE-1]);
513 if (tb[TCA_U32_DIVISOR-1]) {
514 unsigned divisor = *(unsigned*)RTA_DATA(tb[TCA_U32_DIVISOR-1]);
516 if (--divisor > 0x100)
518 if (TC_U32_KEY(handle))
521 handle = gen_new_htid(tp->data);
525 ht = kmalloc(sizeof(*ht) + divisor*sizeof(void*), GFP_KERNEL);
528 memset(ht, 0, sizeof(*ht) + divisor*sizeof(void*));
531 ht->divisor = divisor;
533 ht->next = tp_c->hlist;
535 *arg = (unsigned long)ht;
539 if (tb[TCA_U32_HASH-1]) {
540 htid = *(unsigned*)RTA_DATA(tb[TCA_U32_HASH-1]);
541 if (TC_U32_HTID(htid) == TC_U32_ROOT) {
545 ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
554 if (ht->divisor < TC_U32_HASH(htid))
558 if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
560 handle = htid | TC_U32_NODE(handle);
562 handle = gen_new_kid(ht, htid);
564 if (tb[TCA_U32_SEL-1] == 0 ||
565 RTA_PAYLOAD(tb[TCA_U32_SEL-1]) < sizeof(struct tc_u32_sel))
568 s = RTA_DATA(tb[TCA_U32_SEL-1]);
569 n = kmalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
572 memset(n, 0, sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key));
573 memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
576 err = u32_set_parms(tp->q, base, ht, n, tb, tca[TCA_RATE-1]);
578 struct tc_u_knode **ins;
579 for (ins = &ht->ht[TC_U32_HASH(handle)]; *ins; ins = &(*ins)->next)
580 if (TC_U32_NODE(handle) < TC_U32_NODE((*ins)->handle))
587 *arg = (unsigned long)n;
594 static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
596 struct tc_u_common *tp_c = tp->data;
597 struct tc_u_hnode *ht;
598 struct tc_u_knode *n;
604 for (ht = tp_c->hlist; ht; ht = ht->next) {
605 if (arg->count >= arg->skip) {
606 if (arg->fn(tp, (unsigned long)ht, arg) < 0) {
612 for (h = 0; h <= ht->divisor; h++) {
613 for (n = ht->ht[h]; n; n = n->next) {
614 if (arg->count < arg->skip) {
618 if (arg->fn(tp, (unsigned long)n, arg) < 0) {
628 static int u32_dump(struct tcf_proto *tp, unsigned long fh,
629 struct sk_buff *skb, struct tcmsg *t)
631 struct tc_u_knode *n = (struct tc_u_knode*)fh;
632 unsigned char *b = skb->tail;
638 t->tcm_handle = n->handle;
640 rta = (struct rtattr*)b;
641 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
643 if (TC_U32_KEY(n->handle) == 0) {
644 struct tc_u_hnode *ht = (struct tc_u_hnode*)fh;
645 u32 divisor = ht->divisor+1;
646 RTA_PUT(skb, TCA_U32_DIVISOR, 4, &divisor);
648 RTA_PUT(skb, TCA_U32_SEL,
649 sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
652 u32 htid = n->handle & 0xFFFFF000;
653 RTA_PUT(skb, TCA_U32_HASH, 4, &htid);
656 RTA_PUT(skb, TCA_U32_CLASSID, 4, &n->res.classid);
658 RTA_PUT(skb, TCA_U32_LINK, 4, &n->ht_down->handle);
659 #ifdef CONFIG_NET_CLS_POLICE
661 struct rtattr * p_rta = (struct rtattr*)skb->tail;
663 RTA_PUT(skb, TCA_U32_POLICE, 0, NULL);
665 if (tcf_police_dump(skb, n->police) < 0)
668 p_rta->rta_len = skb->tail - (u8*)p_rta;
673 rta->rta_len = skb->tail - b;
674 #ifdef CONFIG_NET_CLS_POLICE
675 if (TC_U32_KEY(n->handle) && n->police) {
676 if (qdisc_copy_stats(skb, &n->police->stats))
683 skb_trim(skb, b - skb->data);
687 static struct tcf_proto_ops cls_u32_ops = {
690 .classify = u32_classify,
692 .destroy = u32_destroy,
695 .change = u32_change,
696 .delete = u32_delete,
699 .owner = THIS_MODULE,
702 static int __init init_u32(void)
704 return register_tcf_proto_ops(&cls_u32_ops);
707 static void __exit exit_u32(void)
709 unregister_tcf_proto_ops(&cls_u32_ops);
712 module_init(init_u32)
713 module_exit(exit_u32)
714 MODULE_LICENSE("GPL");