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
32 #include <asm/uaccess.h>
33 #include <asm/system.h>
34 #include <linux/bitops.h>
35 #include <linux/config.h>
36 #include <linux/module.h>
37 #include <linux/types.h>
38 #include <linux/kernel.h>
39 #include <linux/sched.h>
40 #include <linux/string.h>
42 #include <linux/socket.h>
43 #include <linux/sockios.h>
45 #include <linux/errno.h>
46 #include <linux/interrupt.h>
47 #include <linux/if_ether.h>
48 #include <linux/inet.h>
49 #include <linux/netdevice.h>
50 #include <linux/etherdevice.h>
51 #include <linux/notifier.h>
52 #include <linux/rtnetlink.h>
54 #include <net/route.h>
55 #include <linux/skbuff.h>
57 #include <net/act_api.h>
58 #include <net/pkt_cls.h>
63 struct tc_u_knode *next;
65 struct tc_u_hnode *ht_up;
66 #ifdef CONFIG_NET_CLS_ACT
67 struct tc_action *action;
69 #ifdef CONFIG_NET_CLS_POLICE
70 struct tcf_police *police;
73 #ifdef CONFIG_NET_CLS_IND
77 struct tcf_result res;
78 struct tc_u_hnode *ht_down;
79 #ifdef CONFIG_CLS_U32_PERF
80 struct tc_u32_pcnt *pf;
82 struct tc_u32_sel sel;
87 struct tc_u_hnode *next;
89 struct tc_u_common *tp_c;
92 struct tc_u_knode *ht[1];
97 struct tc_u_common *next;
98 struct tc_u_hnode *hlist;
104 static struct tc_u_common *u32_list;
106 static __inline__ unsigned u32_hash_fold(u32 key, struct tc_u32_sel *sel, u8 fshift)
108 unsigned h = (key & sel->hmask)>>fshift;
113 static int u32_classify(struct sk_buff *skb, struct tcf_proto *tp, struct tcf_result *res)
116 struct tc_u_knode *knode;
118 } stack[TC_U32_MAXDEPTH];
120 struct tc_u_hnode *ht = (struct tc_u_hnode*)tp->root;
121 u8 *ptr = skb->nh.raw;
122 struct tc_u_knode *n;
126 #ifdef CONFIG_CLS_U32_PERF
136 struct tc_u32_key *key = n->sel.keys;
138 #ifdef CONFIG_CLS_U32_PERF
142 for (i = n->sel.nkeys; i>0; i--, key++) {
144 if ((*(u32*)(ptr+key->off+(off2&key->offmask))^key->val)&key->mask) {
148 #ifdef CONFIG_CLS_U32_PERF
153 if (n->ht_down == NULL) {
155 if (n->sel.flags&TC_U32_TERMINAL) {
158 #ifdef CONFIG_NET_CLS_IND
159 if (!tcf_match_indev(skb, n->indev)) {
164 #ifdef CONFIG_CLS_U32_PERF
167 #ifdef CONFIG_NET_CLS_ACT
169 int pol_res = tcf_action_exec(skb, n->action, res);
174 #ifdef CONFIG_NET_CLS_POLICE
176 int pol_res = tcf_police(skb, n->police);
189 if (sdepth >= TC_U32_MAXDEPTH)
191 stack[sdepth].knode = n;
192 stack[sdepth].ptr = ptr;
198 sel = ht->divisor&u32_hash_fold(*(u32*)(ptr+n->sel.hoff), &n->sel,n->fshift);
200 if (!(n->sel.flags&(TC_U32_VAROFFSET|TC_U32_OFFSET|TC_U32_EAT)))
203 if (n->sel.flags&(TC_U32_OFFSET|TC_U32_VAROFFSET)) {
204 off2 = n->sel.off + 3;
205 if (n->sel.flags&TC_U32_VAROFFSET)
206 off2 += ntohs(n->sel.offmask & *(u16*)(ptr+n->sel.offoff)) >>n->sel.offshift;
209 if (n->sel.flags&TC_U32_EAT) {
220 n = stack[sdepth].knode;
222 ptr = stack[sdepth].ptr;
229 printk("cls_u32: dead loop\n");
233 static __inline__ struct tc_u_hnode *
234 u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
236 struct tc_u_hnode *ht;
238 for (ht = tp_c->hlist; ht; ht = ht->next)
239 if (ht->handle == handle)
245 static __inline__ struct tc_u_knode *
246 u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
249 struct tc_u_knode *n = NULL;
251 sel = TC_U32_HASH(handle);
252 if (sel > ht->divisor)
255 for (n = ht->ht[sel]; n; n = n->next)
256 if (n->handle == handle)
263 static unsigned long u32_get(struct tcf_proto *tp, u32 handle)
265 struct tc_u_hnode *ht;
266 struct tc_u_common *tp_c = tp->data;
268 if (TC_U32_HTID(handle) == TC_U32_ROOT)
271 ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
276 if (TC_U32_KEY(handle) == 0)
277 return (unsigned long)ht;
279 return (unsigned long)u32_lookup_key(ht, handle);
282 static void u32_put(struct tcf_proto *tp, unsigned long f)
286 static u32 gen_new_htid(struct tc_u_common *tp_c)
291 if (++tp_c->hgenerator == 0x7FF)
292 tp_c->hgenerator = 1;
293 } while (--i>0 && u32_lookup_ht(tp_c, (tp_c->hgenerator|0x800)<<20));
295 return i > 0 ? (tp_c->hgenerator|0x800)<<20 : 0;
298 static int u32_init(struct tcf_proto *tp)
300 struct tc_u_hnode *root_ht;
301 struct tc_u_common *tp_c;
303 for (tp_c = u32_list; tp_c; tp_c = tp_c->next)
304 if (tp_c->q == tp->q)
307 root_ht = kmalloc(sizeof(*root_ht), GFP_KERNEL);
311 memset(root_ht, 0, sizeof(*root_ht));
312 root_ht->divisor = 0;
314 root_ht->handle = tp_c ? gen_new_htid(tp_c) : 0x80000000;
317 tp_c = kmalloc(sizeof(*tp_c), GFP_KERNEL);
322 memset(tp_c, 0, sizeof(*tp_c));
324 tp_c->next = u32_list;
329 root_ht->next = tp_c->hlist;
330 tp_c->hlist = root_ht;
331 root_ht->tp_c = tp_c;
338 static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n)
340 tcf_unbind_filter(tp, &n->res);
341 #ifdef CONFIG_NET_CLS_ACT
343 tcf_action_destroy(n->action, TCA_ACT_UNBIND);
346 #ifdef CONFIG_NET_CLS_POLICE
347 tcf_police_release(n->police, TCA_ACT_UNBIND);
351 n->ht_down->refcnt--;
352 #ifdef CONFIG_CLS_U32_PERF
353 if (n && (NULL != n->pf))
360 static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode* key)
362 struct tc_u_knode **kp;
363 struct tc_u_hnode *ht = key->ht_up;
366 for (kp = &ht->ht[TC_U32_HASH(key->handle)]; *kp; kp = &(*kp)->next) {
372 u32_destroy_key(tp, key);
381 static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
383 struct tc_u_knode *n;
386 for (h=0; h<=ht->divisor; h++) {
387 while ((n = ht->ht[h]) != NULL) {
390 u32_destroy_key(tp, n);
395 static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
397 struct tc_u_common *tp_c = tp->data;
398 struct tc_u_hnode **hn;
400 BUG_TRAP(!ht->refcnt);
402 u32_clear_hnode(tp, ht);
404 for (hn = &tp_c->hlist; *hn; hn = &(*hn)->next) {
416 static void u32_destroy(struct tcf_proto *tp)
418 struct tc_u_common *tp_c = tp->data;
419 struct tc_u_hnode *root_ht = xchg(&tp->root, NULL);
421 BUG_TRAP(root_ht != NULL);
423 if (root_ht && --root_ht->refcnt == 0)
424 u32_destroy_hnode(tp, root_ht);
426 if (--tp_c->refcnt == 0) {
427 struct tc_u_hnode *ht;
428 struct tc_u_common **tp_cp;
430 for (tp_cp = &u32_list; *tp_cp; tp_cp = &(*tp_cp)->next) {
431 if (*tp_cp == tp_c) {
437 for (ht=tp_c->hlist; ht; ht = ht->next)
438 u32_clear_hnode(tp, ht);
440 while ((ht = tp_c->hlist) != NULL) {
441 tp_c->hlist = ht->next;
443 BUG_TRAP(ht->refcnt == 0);
454 static int u32_delete(struct tcf_proto *tp, unsigned long arg)
456 struct tc_u_hnode *ht = (struct tc_u_hnode*)arg;
461 if (TC_U32_KEY(ht->handle))
462 return u32_delete_key(tp, (struct tc_u_knode*)ht);
467 if (--ht->refcnt == 0)
468 u32_destroy_hnode(tp, ht);
473 static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
475 struct tc_u_knode *n;
478 for (n=ht->ht[TC_U32_HASH(handle)]; n; n = n->next)
479 if (i < TC_U32_NODE(n->handle))
480 i = TC_U32_NODE(n->handle);
483 return handle|(i>0xFFF ? 0xFFF : i);
486 static int u32_set_parms(struct tcf_proto *tp, unsigned long base,
487 struct tc_u_hnode *ht,
488 struct tc_u_knode *n, struct rtattr **tb,
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);
517 #ifdef CONFIG_NET_CLS_ACT
518 if (tb[TCA_U32_POLICE-1]) {
519 int err = tcf_change_act_police(tp, &n->action, tb[TCA_U32_POLICE-1], est);
524 if (tb[TCA_U32_ACT-1]) {
525 int err = tcf_change_act(tp, &n->action, tb[TCA_U32_ACT-1], est);
530 #ifdef CONFIG_NET_CLS_POLICE
531 if (tb[TCA_U32_POLICE-1]) {
532 int err = tcf_change_police(tp, &n->police, tb[TCA_U32_POLICE-1], est);
538 #ifdef CONFIG_NET_CLS_IND
539 if (tb[TCA_U32_INDEV-1]) {
540 int err = tcf_change_indev(tp, n->indev, tb[TCA_U32_INDEV-1]);
549 static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
553 struct tc_u_common *tp_c = tp->data;
554 struct tc_u_hnode *ht;
555 struct tc_u_knode *n;
556 struct tc_u32_sel *s;
557 struct rtattr *opt = tca[TCA_OPTIONS-1];
558 struct rtattr *tb[TCA_U32_MAX];
563 return handle ? -EINVAL : 0;
565 if (rtattr_parse(tb, TCA_U32_MAX, RTA_DATA(opt), RTA_PAYLOAD(opt)) < 0)
568 if ((n = (struct tc_u_knode*)*arg) != NULL) {
569 if (TC_U32_KEY(n->handle) == 0)
572 return u32_set_parms(tp, base, n->ht_up, n, tb, tca[TCA_RATE-1]);
575 if (tb[TCA_U32_DIVISOR-1]) {
576 unsigned divisor = *(unsigned*)RTA_DATA(tb[TCA_U32_DIVISOR-1]);
578 if (--divisor > 0x100)
580 if (TC_U32_KEY(handle))
583 handle = gen_new_htid(tp->data);
587 ht = kmalloc(sizeof(*ht) + divisor*sizeof(void*), GFP_KERNEL);
590 memset(ht, 0, sizeof(*ht) + divisor*sizeof(void*));
593 ht->divisor = divisor;
595 ht->next = tp_c->hlist;
597 *arg = (unsigned long)ht;
601 if (tb[TCA_U32_HASH-1]) {
602 htid = *(unsigned*)RTA_DATA(tb[TCA_U32_HASH-1]);
603 if (TC_U32_HTID(htid) == TC_U32_ROOT) {
607 ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
616 if (ht->divisor < TC_U32_HASH(htid))
620 if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
622 handle = htid | TC_U32_NODE(handle);
624 handle = gen_new_kid(ht, htid);
626 if (tb[TCA_U32_SEL-1] == 0 ||
627 RTA_PAYLOAD(tb[TCA_U32_SEL-1]) < sizeof(struct tc_u32_sel))
630 s = RTA_DATA(tb[TCA_U32_SEL-1]);
632 n = kmalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
636 memset(n, 0, sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key));
637 #ifdef CONFIG_CLS_U32_PERF
638 n->pf = kmalloc(sizeof(struct tc_u32_pcnt) + s->nkeys*sizeof(__u64), GFP_KERNEL);
643 memset(n->pf, 0, sizeof(struct tc_u32_pcnt) + s->nkeys*sizeof(__u64));
646 memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
653 while (!(mask & 1)) {
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
675 if (n && (NULL != n->pf))
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 (arg->count >= arg->skip) {
694 if (arg->fn(tp, (unsigned long)ht, arg) < 0) {
700 for (h = 0; h <= ht->divisor; h++) {
701 for (n = ht->ht[h]; n; n = n->next) {
702 if (arg->count < arg->skip) {
706 if (arg->fn(tp, (unsigned long)n, arg) < 0) {
716 static int u32_dump(struct tcf_proto *tp, unsigned long fh,
717 struct sk_buff *skb, struct tcmsg *t)
719 struct tc_u_knode *n = (struct tc_u_knode*)fh;
720 unsigned char *b = skb->tail;
726 t->tcm_handle = n->handle;
728 rta = (struct rtattr*)b;
729 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
731 if (TC_U32_KEY(n->handle) == 0) {
732 struct tc_u_hnode *ht = (struct tc_u_hnode*)fh;
733 u32 divisor = ht->divisor+1;
734 RTA_PUT(skb, TCA_U32_DIVISOR, 4, &divisor);
736 RTA_PUT(skb, TCA_U32_SEL,
737 sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
740 u32 htid = n->handle & 0xFFFFF000;
741 RTA_PUT(skb, TCA_U32_HASH, 4, &htid);
744 RTA_PUT(skb, TCA_U32_CLASSID, 4, &n->res.classid);
746 RTA_PUT(skb, TCA_U32_LINK, 4, &n->ht_down->handle);
747 #ifdef CONFIG_NET_CLS_ACT
748 if (tcf_dump_act(skb, n->action, TCA_U32_ACT, TCA_U32_POLICE) < 0)
751 #ifdef CONFIG_NET_CLS_POLICE
752 if (tcf_dump_police(skb, n->police, TCA_U32_POLICE) < 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 #ifdef CONFIG_NET_CLS_ACT
770 if (TC_U32_KEY(n->handle) != 0) {
771 if (TC_U32_KEY(n->handle) && n->action && n->action->type == TCA_OLD_COMPAT) {
772 if (tcf_action_copy_stats(skb,n->action))
777 #ifdef CONFIG_NET_CLS_POLICE
778 if (TC_U32_KEY(n->handle) && n->police)
779 if (tcf_police_dump_stats(skb, n->police) < 0)
786 skb_trim(skb, b - skb->data);
790 static struct tcf_proto_ops cls_u32_ops = {
793 .classify = u32_classify,
795 .destroy = u32_destroy,
798 .change = u32_change,
799 .delete = u32_delete,
802 .owner = THIS_MODULE,
805 static int __init init_u32(void)
807 printk("u32 classifier\n");
808 #ifdef CONFIG_CLS_U32_PERF
809 printk(" Perfomance counters on\n");
811 #ifdef CONFIG_NET_CLS_POLICE
812 printk(" OLD policer on \n");
814 #ifdef CONFIG_NET_CLS_IND
815 printk(" input device check on \n");
817 #ifdef CONFIG_NET_CLS_ACT
818 printk(" Actions configured \n");
820 return register_tcf_proto_ops(&cls_u32_ops);
823 static void __exit exit_u32(void)
825 unregister_tcf_proto_ops(&cls_u32_ops);
828 module_init(init_u32)
829 module_exit(exit_u32)
830 MODULE_LICENSE("GPL");