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 <asm/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/pkt_sched.h>
62 struct tc_u_knode *next;
64 struct tc_u_hnode *ht_up;
65 #ifdef CONFIG_NET_CLS_ACT
66 struct tc_action *action;
68 #ifdef CONFIG_NET_CLS_POLICE
69 struct tcf_police *police;
72 #ifdef CONFIG_NET_CLS_IND
76 struct tcf_result res;
77 struct tc_u_hnode *ht_down;
78 #ifdef CONFIG_CLS_U32_PERF
79 struct tc_u32_pcnt *pf;
81 struct tc_u32_sel sel;
86 struct tc_u_hnode *next;
88 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 /* yes, i know it sucks but the feature is
160 ** optional dammit! - JHS */
161 if (0 != n->indev[0]) {
162 if (NULL == skb->input_dev) {
166 if (0 != strcmp(n->indev, skb->input_dev->name)) {
173 #ifdef CONFIG_CLS_U32_PERF
176 #ifdef CONFIG_NET_CLS_ACT
178 int pol_res = tcf_action_exec(skb, n->action, res);
183 #ifdef CONFIG_NET_CLS_POLICE
185 int pol_res = tcf_police(skb, n->police);
198 if (sdepth >= TC_U32_MAXDEPTH)
200 stack[sdepth].knode = n;
201 stack[sdepth].ptr = ptr;
207 sel = ht->divisor&u32_hash_fold(*(u32*)(ptr+n->sel.hoff), &n->sel,n->fshift);
209 if (!(n->sel.flags&(TC_U32_VAROFFSET|TC_U32_OFFSET|TC_U32_EAT)))
212 if (n->sel.flags&(TC_U32_OFFSET|TC_U32_VAROFFSET)) {
213 off2 = n->sel.off + 3;
214 if (n->sel.flags&TC_U32_VAROFFSET)
215 off2 += ntohs(n->sel.offmask & *(u16*)(ptr+n->sel.offoff)) >>n->sel.offshift;
218 if (n->sel.flags&TC_U32_EAT) {
229 n = stack[sdepth].knode;
231 ptr = stack[sdepth].ptr;
238 printk("cls_u32: dead loop\n");
242 static __inline__ struct tc_u_hnode *
243 u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
245 struct tc_u_hnode *ht;
247 for (ht = tp_c->hlist; ht; ht = ht->next)
248 if (ht->handle == handle)
254 static __inline__ struct tc_u_knode *
255 u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
258 struct tc_u_knode *n = NULL;
260 sel = TC_U32_HASH(handle);
261 if (sel > ht->divisor)
264 for (n = ht->ht[sel]; n; n = n->next)
265 if (n->handle == handle)
272 static unsigned long u32_get(struct tcf_proto *tp, u32 handle)
274 struct tc_u_hnode *ht;
275 struct tc_u_common *tp_c = tp->data;
277 if (TC_U32_HTID(handle) == TC_U32_ROOT)
280 ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
285 if (TC_U32_KEY(handle) == 0)
286 return (unsigned long)ht;
288 return (unsigned long)u32_lookup_key(ht, handle);
291 static void u32_put(struct tcf_proto *tp, unsigned long f)
295 static u32 gen_new_htid(struct tc_u_common *tp_c)
300 if (++tp_c->hgenerator == 0x7FF)
301 tp_c->hgenerator = 1;
302 } while (--i>0 && u32_lookup_ht(tp_c, (tp_c->hgenerator|0x800)<<20));
304 return i > 0 ? (tp_c->hgenerator|0x800)<<20 : 0;
307 static int u32_init(struct tcf_proto *tp)
309 struct tc_u_hnode *root_ht;
310 struct tc_u_common *tp_c;
312 for (tp_c = u32_list; tp_c; tp_c = tp_c->next)
313 if (tp_c->q == tp->q)
316 root_ht = kmalloc(sizeof(*root_ht), GFP_KERNEL);
320 memset(root_ht, 0, sizeof(*root_ht));
321 root_ht->divisor = 0;
323 root_ht->handle = tp_c ? gen_new_htid(tp_c) : 0x80000000;
326 tp_c = kmalloc(sizeof(*tp_c), GFP_KERNEL);
331 memset(tp_c, 0, sizeof(*tp_c));
333 tp_c->next = u32_list;
338 root_ht->next = tp_c->hlist;
339 tp_c->hlist = root_ht;
340 root_ht->tp_c = tp_c;
347 static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n)
351 if ((cl = __cls_set_class(&n->res.class, 0)) != 0)
352 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
353 #ifdef CONFIG_NET_CLS_ACT
355 tcf_action_destroy(n->action, TCA_ACT_UNBIND);
358 #ifdef CONFIG_NET_CLS_POLICE
359 tcf_police_release(n->police, TCA_ACT_UNBIND);
363 n->ht_down->refcnt--;
364 #ifdef CONFIG_CLS_U32_PERF
365 if (n && (NULL != n->pf))
372 static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode* key)
374 struct tc_u_knode **kp;
375 struct tc_u_hnode *ht = key->ht_up;
378 for (kp = &ht->ht[TC_U32_HASH(key->handle)]; *kp; kp = &(*kp)->next) {
384 u32_destroy_key(tp, key);
393 static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
395 struct tc_u_knode *n;
398 for (h=0; h<=ht->divisor; h++) {
399 while ((n = ht->ht[h]) != NULL) {
402 u32_destroy_key(tp, n);
407 static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
409 struct tc_u_common *tp_c = tp->data;
410 struct tc_u_hnode **hn;
412 BUG_TRAP(!ht->refcnt);
414 u32_clear_hnode(tp, ht);
416 for (hn = &tp_c->hlist; *hn; hn = &(*hn)->next) {
428 static void u32_destroy(struct tcf_proto *tp)
430 struct tc_u_common *tp_c = tp->data;
431 struct tc_u_hnode *root_ht = xchg(&tp->root, NULL);
433 BUG_TRAP(root_ht != NULL);
435 if (root_ht && --root_ht->refcnt == 0)
436 u32_destroy_hnode(tp, root_ht);
438 if (--tp_c->refcnt == 0) {
439 struct tc_u_hnode *ht;
440 struct tc_u_common **tp_cp;
442 for (tp_cp = &u32_list; *tp_cp; tp_cp = &(*tp_cp)->next) {
443 if (*tp_cp == tp_c) {
449 for (ht=tp_c->hlist; ht; ht = ht->next)
450 u32_clear_hnode(tp, ht);
452 while ((ht = tp_c->hlist) != NULL) {
453 tp_c->hlist = ht->next;
455 BUG_TRAP(ht->refcnt == 0);
466 static int u32_delete(struct tcf_proto *tp, unsigned long arg)
468 struct tc_u_hnode *ht = (struct tc_u_hnode*)arg;
473 if (TC_U32_KEY(ht->handle))
474 return u32_delete_key(tp, (struct tc_u_knode*)ht);
479 if (--ht->refcnt == 0)
480 u32_destroy_hnode(tp, ht);
485 static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
487 struct tc_u_knode *n;
490 for (n=ht->ht[TC_U32_HASH(handle)]; n; n = n->next)
491 if (i < TC_U32_NODE(n->handle))
492 i = TC_U32_NODE(n->handle);
495 return handle|(i>0xFFF ? 0xFFF : i);
498 static int u32_set_parms(struct Qdisc *q, unsigned long base,
499 struct tc_u_hnode *ht,
500 struct tc_u_knode *n, struct rtattr **tb,
503 #ifdef CONFIG_NET_CLS_ACT
504 struct tc_action *act = NULL;
507 if (tb[TCA_U32_LINK-1]) {
508 u32 handle = *(u32*)RTA_DATA(tb[TCA_U32_LINK-1]);
509 struct tc_u_hnode *ht_down = NULL;
511 if (TC_U32_KEY(handle))
515 ht_down = u32_lookup_ht(ht->tp_c, handle);
523 ht_down = xchg(&n->ht_down, ht_down);
529 if (tb[TCA_U32_CLASSID-1]) {
532 n->res.classid = *(u32*)RTA_DATA(tb[TCA_U32_CLASSID-1]);
534 cl = __cls_set_class(&n->res.class, q->ops->cl_ops->bind_tcf(q, base, n->res.classid));
537 q->ops->cl_ops->unbind_tcf(q, cl);
539 #ifdef CONFIG_NET_CLS_ACT
540 /*backward compatibility */
541 if (tb[TCA_U32_POLICE-1])
543 act = kmalloc(sizeof(*act),GFP_KERNEL);
547 memset(act,0,sizeof(*act));
548 ret = tcf_action_init_1(tb[TCA_U32_POLICE-1], est,act,"police", TCA_ACT_NOREPLACE, TCA_ACT_BIND);
550 tcf_action_destroy(act, TCA_ACT_UNBIND);
553 act->type = TCA_OLD_COMPAT;
556 act = xchg(&n->action, act);
559 tcf_action_destroy(act, TCA_ACT_UNBIND);
563 if(tb[TCA_U32_ACT-1]) {
564 act = kmalloc(sizeof(*act),GFP_KERNEL);
567 memset(act,0,sizeof(*act));
568 ret = tcf_action_init(tb[TCA_U32_ACT-1], est,act,NULL,TCA_ACT_NOREPLACE, TCA_ACT_BIND);
570 tcf_action_destroy(act, TCA_ACT_UNBIND);
575 act = xchg(&n->action, act);
578 tcf_action_destroy(act, TCA_ACT_UNBIND);
583 #ifdef CONFIG_NET_CLS_POLICE
584 if (tb[TCA_U32_POLICE-1]) {
585 struct tcf_police *police = tcf_police_locate(tb[TCA_U32_POLICE-1], est);
587 police = xchg(&n->police, police);
589 tcf_police_release(police, TCA_ACT_UNBIND);
593 #ifdef CONFIG_NET_CLS_IND
595 if(tb[TCA_U32_INDEV-1]) {
596 struct rtattr *input_dev = tb[TCA_U32_INDEV-1];
597 if (RTA_PAYLOAD(input_dev) >= IFNAMSIZ) {
598 printk("cls_u32: bad indev name %s\n",(char*)RTA_DATA(input_dev));
599 /* should we clear state first? */
602 sprintf(n->indev, "%s", (char*)RTA_DATA(input_dev));
603 printk("got IND %s\n",n->indev);
610 static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
614 struct tc_u_common *tp_c = tp->data;
615 struct tc_u_hnode *ht;
616 struct tc_u_knode *n;
617 struct tc_u32_sel *s;
618 struct rtattr *opt = tca[TCA_OPTIONS-1];
619 struct rtattr *tb[TCA_U32_MAX];
624 return handle ? -EINVAL : 0;
626 if (rtattr_parse(tb, TCA_U32_MAX, RTA_DATA(opt), RTA_PAYLOAD(opt)) < 0)
629 if ((n = (struct tc_u_knode*)*arg) != NULL) {
630 if (TC_U32_KEY(n->handle) == 0)
633 return u32_set_parms(tp->q, base, n->ht_up, n, tb, tca[TCA_RATE-1]);
636 if (tb[TCA_U32_DIVISOR-1]) {
637 unsigned divisor = *(unsigned*)RTA_DATA(tb[TCA_U32_DIVISOR-1]);
639 if (--divisor > 0x100)
641 if (TC_U32_KEY(handle))
644 handle = gen_new_htid(tp->data);
648 ht = kmalloc(sizeof(*ht) + divisor*sizeof(void*), GFP_KERNEL);
651 memset(ht, 0, sizeof(*ht) + divisor*sizeof(void*));
654 ht->divisor = divisor;
656 ht->next = tp_c->hlist;
658 *arg = (unsigned long)ht;
662 if (tb[TCA_U32_HASH-1]) {
663 htid = *(unsigned*)RTA_DATA(tb[TCA_U32_HASH-1]);
664 if (TC_U32_HTID(htid) == TC_U32_ROOT) {
668 ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
677 if (ht->divisor < TC_U32_HASH(htid))
681 if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
683 handle = htid | TC_U32_NODE(handle);
685 handle = gen_new_kid(ht, htid);
687 if (tb[TCA_U32_SEL-1] == 0 ||
688 RTA_PAYLOAD(tb[TCA_U32_SEL-1]) < sizeof(struct tc_u32_sel))
691 s = RTA_DATA(tb[TCA_U32_SEL-1]);
693 n = kmalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
697 memset(n, 0, sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key));
698 #ifdef CONFIG_CLS_U32_PERF
699 n->pf = kmalloc(sizeof(struct tc_u32_pcnt) + s->nkeys*sizeof(__u64), GFP_KERNEL);
704 memset(n->pf, 0, sizeof(struct tc_u32_pcnt) + s->nkeys*sizeof(__u64));
707 memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
714 while (!(mask & 1)) {
721 err = u32_set_parms(tp->q, base, ht, n, tb, tca[TCA_RATE-1]);
723 struct tc_u_knode **ins;
724 for (ins = &ht->ht[TC_U32_HASH(handle)]; *ins; ins = &(*ins)->next)
725 if (TC_U32_NODE(handle) < TC_U32_NODE((*ins)->handle))
732 *arg = (unsigned long)n;
735 #ifdef CONFIG_CLS_U32_PERF
736 if (n && (NULL != n->pf))
743 static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
745 struct tc_u_common *tp_c = tp->data;
746 struct tc_u_hnode *ht;
747 struct tc_u_knode *n;
753 for (ht = tp_c->hlist; ht; ht = ht->next) {
754 if (arg->count >= arg->skip) {
755 if (arg->fn(tp, (unsigned long)ht, arg) < 0) {
761 for (h = 0; h <= ht->divisor; h++) {
762 for (n = ht->ht[h]; n; n = n->next) {
763 if (arg->count < arg->skip) {
767 if (arg->fn(tp, (unsigned long)n, arg) < 0) {
777 static int u32_dump(struct tcf_proto *tp, unsigned long fh,
778 struct sk_buff *skb, struct tcmsg *t)
780 struct tc_u_knode *n = (struct tc_u_knode*)fh;
781 unsigned char *b = skb->tail;
787 t->tcm_handle = n->handle;
789 rta = (struct rtattr*)b;
790 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
792 if (TC_U32_KEY(n->handle) == 0) {
793 struct tc_u_hnode *ht = (struct tc_u_hnode*)fh;
794 u32 divisor = ht->divisor+1;
795 RTA_PUT(skb, TCA_U32_DIVISOR, 4, &divisor);
797 RTA_PUT(skb, TCA_U32_SEL,
798 sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
801 u32 htid = n->handle & 0xFFFFF000;
802 RTA_PUT(skb, TCA_U32_HASH, 4, &htid);
805 RTA_PUT(skb, TCA_U32_CLASSID, 4, &n->res.classid);
807 RTA_PUT(skb, TCA_U32_LINK, 4, &n->ht_down->handle);
808 #ifdef CONFIG_NET_CLS_ACT
809 /* again for backward compatible mode - we want
810 * to work with both old and new modes of entering
811 * tc data even if iproute2 was newer - jhs
814 struct rtattr * p_rta = (struct rtattr*)skb->tail;
816 if (n->action->type != TCA_OLD_COMPAT) {
817 RTA_PUT(skb, TCA_U32_ACT, 0, NULL);
818 if (tcf_action_dump(skb,n->action, 0, 0) < 0) {
822 RTA_PUT(skb, TCA_U32_POLICE, 0, NULL);
823 if (tcf_action_dump_old(skb,n->action,0,0) < 0) {
828 p_rta->rta_len = skb->tail - (u8*)p_rta;
832 #ifdef CONFIG_NET_CLS_POLICE
834 struct rtattr * p_rta = (struct rtattr*)skb->tail;
835 RTA_PUT(skb, TCA_U32_POLICE, 0, NULL);
837 if (tcf_police_dump(skb, n->police) < 0)
840 p_rta->rta_len = skb->tail - (u8*)p_rta;
846 #ifdef CONFIG_NET_CLS_IND
847 if(strlen(n->indev)) {
848 struct rtattr * p_rta = (struct rtattr*)skb->tail;
849 RTA_PUT(skb, TCA_U32_INDEV, IFNAMSIZ, n->indev);
850 p_rta->rta_len = skb->tail - (u8*)p_rta;
853 #ifdef CONFIG_CLS_U32_PERF
854 RTA_PUT(skb, TCA_U32_PCNT,
855 sizeof(struct tc_u32_pcnt) + n->sel.nkeys*sizeof(__u64),
860 rta->rta_len = skb->tail - b;
861 #ifdef CONFIG_NET_CLS_ACT
862 if (TC_U32_KEY(n->handle) != 0) {
863 if (TC_U32_KEY(n->handle) && n->action && n->action->type == TCA_OLD_COMPAT) {
864 if (tcf_action_copy_stats(skb,n->action))
869 #ifdef CONFIG_NET_CLS_POLICE
870 if (TC_U32_KEY(n->handle) && n->police) {
871 if (qdisc_copy_stats(skb, &n->police->stats,
872 n->police->stats_lock))
880 skb_trim(skb, b - skb->data);
884 static struct tcf_proto_ops cls_u32_ops = {
887 .classify = u32_classify,
889 .destroy = u32_destroy,
892 .change = u32_change,
893 .delete = u32_delete,
896 .owner = THIS_MODULE,
899 static int __init init_u32(void)
901 printk("u32 classifier\n");
902 #ifdef CONFIG_CLS_U32_PERF
903 printk(" Perfomance counters on\n");
905 #ifdef CONFIG_NET_CLS_POLICE
906 printk(" OLD policer on \n");
908 #ifdef CONFIG_NET_CLS_IND
909 printk(" input device check on \n");
911 #ifdef CONFIG_NET_CLS_ACT
912 printk(" Actions configured \n");
914 return register_tcf_proto_ops(&cls_u32_ops);
917 static void __exit exit_u32(void)
919 unregister_tcf_proto_ops(&cls_u32_ops);
922 module_init(init_u32)
923 module_exit(exit_u32)
924 MODULE_LICENSE("GPL");