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/config.h>
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/kernel.h>
40 #include <linux/sched.h>
41 #include <linux/string.h>
43 #include <linux/socket.h>
44 #include <linux/sockios.h>
46 #include <linux/errno.h>
47 #include <linux/interrupt.h>
48 #include <linux/if_ether.h>
49 #include <linux/inet.h>
50 #include <linux/netdevice.h>
51 #include <linux/etherdevice.h>
52 #include <linux/notifier.h>
53 #include <linux/rtnetlink.h>
55 #include <net/route.h>
56 #include <linux/skbuff.h>
58 #include <net/act_api.h>
59 #include <net/pkt_cls.h>
71 struct tc_u_knode *next;
73 struct tc_u_hnode *ht_up;
75 #ifdef CONFIG_NET_CLS_IND
79 struct tcf_result res;
80 struct tc_u_hnode *ht_down;
81 #ifdef CONFIG_CLS_U32_PERF
82 struct tc_u32_pcnt *pf;
84 #ifdef CONFIG_CLS_U32_MARK
85 struct tc_u32_mark mark;
87 struct tc_u32_sel sel;
92 struct tc_u_hnode *next;
95 struct tc_u_common *tp_c;
98 struct tc_u_knode *ht[1];
103 struct tc_u_common *next;
104 struct tc_u_hnode *hlist;
110 static struct tcf_ext_map u32_ext_map = {
111 .action = TCA_U32_ACT,
112 .police = TCA_U32_POLICE
115 static struct tc_u_common *u32_list;
117 static __inline__ unsigned u32_hash_fold(u32 key, struct tc_u32_sel *sel, u8 fshift)
119 unsigned h = (key & sel->hmask)>>fshift;
124 static int u32_classify(struct sk_buff *skb, struct tcf_proto *tp, struct tcf_result *res)
127 struct tc_u_knode *knode;
129 } stack[TC_U32_MAXDEPTH];
131 struct tc_u_hnode *ht = (struct tc_u_hnode*)tp->root;
132 u8 *ptr = skb->nh.raw;
133 struct tc_u_knode *n;
137 #ifdef CONFIG_CLS_U32_PERF
147 struct tc_u32_key *key = n->sel.keys;
149 #ifdef CONFIG_CLS_U32_PERF
154 #ifdef CONFIG_CLS_U32_MARK
155 if ((skb->nfmark & n->mark.mask) != n->mark.val) {
163 for (i = n->sel.nkeys; i>0; i--, key++) {
165 if ((*(u32*)(ptr+key->off+(off2&key->offmask))^key->val)&key->mask) {
169 #ifdef CONFIG_CLS_U32_PERF
174 if (n->ht_down == NULL) {
176 if (n->sel.flags&TC_U32_TERMINAL) {
179 #ifdef CONFIG_NET_CLS_IND
180 if (!tcf_match_indev(skb, n->indev)) {
185 #ifdef CONFIG_CLS_U32_PERF
188 r = tcf_exts_exec(skb, &n->exts, res);
201 if (sdepth >= TC_U32_MAXDEPTH)
203 stack[sdepth].knode = n;
204 stack[sdepth].ptr = ptr;
210 sel = ht->divisor&u32_hash_fold(*(u32*)(ptr+n->sel.hoff), &n->sel,n->fshift);
212 if (!(n->sel.flags&(TC_U32_VAROFFSET|TC_U32_OFFSET|TC_U32_EAT)))
215 if (n->sel.flags&(TC_U32_OFFSET|TC_U32_VAROFFSET)) {
216 off2 = n->sel.off + 3;
217 if (n->sel.flags&TC_U32_VAROFFSET)
218 off2 += ntohs(n->sel.offmask & *(u16*)(ptr+n->sel.offoff)) >>n->sel.offshift;
221 if (n->sel.flags&TC_U32_EAT) {
232 n = stack[sdepth].knode;
234 ptr = stack[sdepth].ptr;
241 printk("cls_u32: dead loop\n");
245 static __inline__ struct tc_u_hnode *
246 u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
248 struct tc_u_hnode *ht;
250 for (ht = tp_c->hlist; ht; ht = ht->next)
251 if (ht->handle == handle)
257 static __inline__ struct tc_u_knode *
258 u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
261 struct tc_u_knode *n = NULL;
263 sel = TC_U32_HASH(handle);
264 if (sel > ht->divisor)
267 for (n = ht->ht[sel]; n; n = n->next)
268 if (n->handle == handle)
275 static unsigned long u32_get(struct tcf_proto *tp, u32 handle)
277 struct tc_u_hnode *ht;
278 struct tc_u_common *tp_c = tp->data;
280 if (TC_U32_HTID(handle) == TC_U32_ROOT)
283 ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
288 if (TC_U32_KEY(handle) == 0)
289 return (unsigned long)ht;
291 return (unsigned long)u32_lookup_key(ht, handle);
294 static void u32_put(struct tcf_proto *tp, unsigned long f)
298 static u32 gen_new_htid(struct tc_u_common *tp_c)
303 if (++tp_c->hgenerator == 0x7FF)
304 tp_c->hgenerator = 1;
305 } while (--i>0 && u32_lookup_ht(tp_c, (tp_c->hgenerator|0x800)<<20));
307 return i > 0 ? (tp_c->hgenerator|0x800)<<20 : 0;
310 static int u32_init(struct tcf_proto *tp)
312 struct tc_u_hnode *root_ht;
313 struct tc_u_common *tp_c;
315 for (tp_c = u32_list; tp_c; tp_c = tp_c->next)
316 if (tp_c->q == tp->q)
319 root_ht = kmalloc(sizeof(*root_ht), GFP_KERNEL);
323 memset(root_ht, 0, sizeof(*root_ht));
324 root_ht->divisor = 0;
326 root_ht->handle = tp_c ? gen_new_htid(tp_c) : 0x80000000;
327 root_ht->prio = tp->prio;
330 tp_c = kmalloc(sizeof(*tp_c), GFP_KERNEL);
335 memset(tp_c, 0, sizeof(*tp_c));
337 tp_c->next = u32_list;
342 root_ht->next = tp_c->hlist;
343 tp_c->hlist = root_ht;
344 root_ht->tp_c = tp_c;
351 static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n)
353 tcf_unbind_filter(tp, &n->res);
354 tcf_exts_destroy(tp, &n->exts);
356 n->ht_down->refcnt--;
357 #ifdef CONFIG_CLS_U32_PERF
358 if (n && (NULL != n->pf))
365 static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode* key)
367 struct tc_u_knode **kp;
368 struct tc_u_hnode *ht = key->ht_up;
371 for (kp = &ht->ht[TC_U32_HASH(key->handle)]; *kp; kp = &(*kp)->next) {
377 u32_destroy_key(tp, key);
386 static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
388 struct tc_u_knode *n;
391 for (h=0; h<=ht->divisor; h++) {
392 while ((n = ht->ht[h]) != NULL) {
395 u32_destroy_key(tp, n);
400 static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
402 struct tc_u_common *tp_c = tp->data;
403 struct tc_u_hnode **hn;
405 BUG_TRAP(!ht->refcnt);
407 u32_clear_hnode(tp, ht);
409 for (hn = &tp_c->hlist; *hn; hn = &(*hn)->next) {
421 static void u32_destroy(struct tcf_proto *tp)
423 struct tc_u_common *tp_c = tp->data;
424 struct tc_u_hnode *root_ht = xchg(&tp->root, NULL);
426 BUG_TRAP(root_ht != NULL);
428 if (root_ht && --root_ht->refcnt == 0)
429 u32_destroy_hnode(tp, root_ht);
431 if (--tp_c->refcnt == 0) {
432 struct tc_u_hnode *ht;
433 struct tc_u_common **tp_cp;
435 for (tp_cp = &u32_list; *tp_cp; tp_cp = &(*tp_cp)->next) {
436 if (*tp_cp == tp_c) {
442 for (ht=tp_c->hlist; ht; ht = ht->next)
443 u32_clear_hnode(tp, ht);
445 while ((ht = tp_c->hlist) != NULL) {
446 tp_c->hlist = ht->next;
448 BUG_TRAP(ht->refcnt == 0);
459 static int u32_delete(struct tcf_proto *tp, unsigned long arg)
461 struct tc_u_hnode *ht = (struct tc_u_hnode*)arg;
466 if (TC_U32_KEY(ht->handle))
467 return u32_delete_key(tp, (struct tc_u_knode*)ht);
472 if (--ht->refcnt == 0)
473 u32_destroy_hnode(tp, ht);
478 static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
480 struct tc_u_knode *n;
483 for (n=ht->ht[TC_U32_HASH(handle)]; n; n = n->next)
484 if (i < TC_U32_NODE(n->handle))
485 i = TC_U32_NODE(n->handle);
488 return handle|(i>0xFFF ? 0xFFF : i);
491 static int u32_set_parms(struct tcf_proto *tp, unsigned long base,
492 struct tc_u_hnode *ht,
493 struct tc_u_knode *n, struct rtattr **tb,
499 err = tcf_exts_validate(tp, tb, est, &e, &u32_ext_map);
504 if (tb[TCA_U32_LINK-1]) {
505 u32 handle = *(u32*)RTA_DATA(tb[TCA_U32_LINK-1]);
506 struct tc_u_hnode *ht_down = NULL;
508 if (TC_U32_KEY(handle))
512 ht_down = u32_lookup_ht(ht->tp_c, handle);
520 ht_down = xchg(&n->ht_down, ht_down);
526 if (tb[TCA_U32_CLASSID-1]) {
527 n->res.classid = *(u32*)RTA_DATA(tb[TCA_U32_CLASSID-1]);
528 tcf_bind_filter(tp, &n->res, base);
531 #ifdef CONFIG_NET_CLS_IND
532 if (tb[TCA_U32_INDEV-1]) {
533 int err = tcf_change_indev(tp, n->indev, tb[TCA_U32_INDEV-1]);
538 tcf_exts_change(tp, &n->exts, &e);
542 tcf_exts_destroy(tp, &e);
546 static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
550 struct tc_u_common *tp_c = tp->data;
551 struct tc_u_hnode *ht;
552 struct tc_u_knode *n;
553 struct tc_u32_sel *s;
554 struct rtattr *opt = tca[TCA_OPTIONS-1];
555 struct rtattr *tb[TCA_U32_MAX];
560 return handle ? -EINVAL : 0;
562 if (rtattr_parse_nested(tb, TCA_U32_MAX, opt) < 0)
565 if ((n = (struct tc_u_knode*)*arg) != NULL) {
566 if (TC_U32_KEY(n->handle) == 0)
569 return u32_set_parms(tp, base, n->ht_up, n, tb, tca[TCA_RATE-1]);
572 if (tb[TCA_U32_DIVISOR-1]) {
573 unsigned divisor = *(unsigned*)RTA_DATA(tb[TCA_U32_DIVISOR-1]);
575 if (--divisor > 0x100)
577 if (TC_U32_KEY(handle))
580 handle = gen_new_htid(tp->data);
584 ht = kmalloc(sizeof(*ht) + divisor*sizeof(void*), GFP_KERNEL);
587 memset(ht, 0, sizeof(*ht) + divisor*sizeof(void*));
590 ht->divisor = divisor;
593 ht->next = tp_c->hlist;
595 *arg = (unsigned long)ht;
599 if (tb[TCA_U32_HASH-1]) {
600 htid = *(unsigned*)RTA_DATA(tb[TCA_U32_HASH-1]);
601 if (TC_U32_HTID(htid) == TC_U32_ROOT) {
605 ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
614 if (ht->divisor < TC_U32_HASH(htid))
618 if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
620 handle = htid | TC_U32_NODE(handle);
622 handle = gen_new_kid(ht, htid);
624 if (tb[TCA_U32_SEL-1] == 0 ||
625 RTA_PAYLOAD(tb[TCA_U32_SEL-1]) < sizeof(struct tc_u32_sel))
628 s = RTA_DATA(tb[TCA_U32_SEL-1]);
630 n = kmalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
634 memset(n, 0, sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key));
635 #ifdef CONFIG_CLS_U32_PERF
636 n->pf = kmalloc(sizeof(struct tc_u32_pcnt) + s->nkeys*sizeof(u64), GFP_KERNEL);
641 memset(n->pf, 0, sizeof(struct tc_u32_pcnt) + s->nkeys*sizeof(u64));
644 memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
651 while (!(mask & 1)) {
659 #ifdef CONFIG_CLS_U32_MARK
660 if (tb[TCA_U32_MARK-1]) {
661 struct tc_u32_mark *mark;
663 if (RTA_PAYLOAD(tb[TCA_U32_MARK-1]) < sizeof(struct tc_u32_mark)) {
664 #ifdef CONFIG_CLS_U32_PERF
670 mark = RTA_DATA(tb[TCA_U32_MARK-1]);
671 memcpy(&n->mark, mark, sizeof(struct tc_u32_mark));
676 err = u32_set_parms(tp, base, ht, n, tb, tca[TCA_RATE-1]);
678 struct tc_u_knode **ins;
679 for (ins = &ht->ht[TC_U32_HASH(handle)]; *ins; ins = &(*ins)->next)
680 if (TC_U32_NODE(handle) < TC_U32_NODE((*ins)->handle))
687 *arg = (unsigned long)n;
690 #ifdef CONFIG_CLS_U32_PERF
691 if (n && (NULL != n->pf))
698 static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
700 struct tc_u_common *tp_c = tp->data;
701 struct tc_u_hnode *ht;
702 struct tc_u_knode *n;
708 for (ht = tp_c->hlist; ht; ht = ht->next) {
709 if (ht->prio != tp->prio)
711 if (arg->count >= arg->skip) {
712 if (arg->fn(tp, (unsigned long)ht, arg) < 0) {
718 for (h = 0; h <= ht->divisor; h++) {
719 for (n = ht->ht[h]; n; n = n->next) {
720 if (arg->count < arg->skip) {
724 if (arg->fn(tp, (unsigned long)n, arg) < 0) {
734 static int u32_dump(struct tcf_proto *tp, unsigned long fh,
735 struct sk_buff *skb, struct tcmsg *t)
737 struct tc_u_knode *n = (struct tc_u_knode*)fh;
738 unsigned char *b = skb->tail;
744 t->tcm_handle = n->handle;
746 rta = (struct rtattr*)b;
747 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
749 if (TC_U32_KEY(n->handle) == 0) {
750 struct tc_u_hnode *ht = (struct tc_u_hnode*)fh;
751 u32 divisor = ht->divisor+1;
752 RTA_PUT(skb, TCA_U32_DIVISOR, 4, &divisor);
754 RTA_PUT(skb, TCA_U32_SEL,
755 sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
758 u32 htid = n->handle & 0xFFFFF000;
759 RTA_PUT(skb, TCA_U32_HASH, 4, &htid);
762 RTA_PUT(skb, TCA_U32_CLASSID, 4, &n->res.classid);
764 RTA_PUT(skb, TCA_U32_LINK, 4, &n->ht_down->handle);
766 #ifdef CONFIG_CLS_U32_MARK
767 if (n->mark.val || n->mark.mask)
768 RTA_PUT(skb, TCA_U32_MARK, sizeof(n->mark), &n->mark);
771 if (tcf_exts_dump(skb, &n->exts, &u32_ext_map) < 0)
774 #ifdef CONFIG_NET_CLS_IND
776 RTA_PUT(skb, TCA_U32_INDEV, IFNAMSIZ, n->indev);
778 #ifdef CONFIG_CLS_U32_PERF
779 RTA_PUT(skb, TCA_U32_PCNT,
780 sizeof(struct tc_u32_pcnt) + n->sel.nkeys*sizeof(u64),
785 rta->rta_len = skb->tail - b;
786 if (TC_U32_KEY(n->handle))
787 if (tcf_exts_dump_stats(skb, &n->exts, &u32_ext_map) < 0)
792 skb_trim(skb, b - skb->data);
796 static struct tcf_proto_ops cls_u32_ops = {
799 .classify = u32_classify,
801 .destroy = u32_destroy,
804 .change = u32_change,
805 .delete = u32_delete,
808 .owner = THIS_MODULE,
811 static int __init init_u32(void)
813 printk("u32 classifier\n");
814 #ifdef CONFIG_CLS_U32_PERF
815 printk(" Perfomance counters on\n");
817 #ifdef CONFIG_NET_CLS_POLICE
818 printk(" OLD policer on \n");
820 #ifdef CONFIG_NET_CLS_IND
821 printk(" input device check on \n");
823 #ifdef CONFIG_NET_CLS_ACT
824 printk(" Actions configured \n");
826 return register_tcf_proto_ops(&cls_u32_ops);
829 static void __exit exit_u32(void)
831 unregister_tcf_proto_ops(&cls_u32_ops);
834 module_init(init_u32)
835 module_exit(exit_u32)
836 MODULE_LICENSE("GPL");