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;
74 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;
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;
262 for (tp_c = u32_list; tp_c; tp_c = tp_c->next)
263 if (tp_c->q == tp->q)
266 root_ht = kmalloc(sizeof(*root_ht), GFP_KERNEL);
267 if (root_ht == NULL) {
271 memset(root_ht, 0, sizeof(*root_ht));
272 root_ht->divisor = 0;
274 root_ht->handle = tp_c ? gen_new_htid(tp_c) : 0x80000000;
275 root_ht->prio = tp->prio;
278 tp_c = kmalloc(sizeof(*tp_c), GFP_KERNEL);
284 memset(tp_c, 0, sizeof(*tp_c));
286 tp_c->next = u32_list;
291 root_ht->next = tp_c->hlist;
292 tp_c->hlist = root_ht;
293 root_ht->tp_c = tp_c;
300 static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n)
304 if ((cl = __cls_set_class(&n->res.class, 0)) != 0)
305 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
306 #ifdef CONFIG_NET_CLS_POLICE
307 tcf_police_release(n->police);
310 n->ht_down->refcnt--;
315 static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode* key)
317 struct tc_u_knode **kp;
318 struct tc_u_hnode *ht = key->ht_up;
321 for (kp = &ht->ht[TC_U32_HASH(key->handle)]; *kp; kp = &(*kp)->next) {
327 u32_destroy_key(tp, key);
336 static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
338 struct tc_u_knode *n;
341 for (h=0; h<=ht->divisor; h++) {
342 while ((n = ht->ht[h]) != NULL) {
345 u32_destroy_key(tp, n);
350 static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
352 struct tc_u_common *tp_c = tp->data;
353 struct tc_u_hnode **hn;
355 BUG_TRAP(!ht->refcnt);
357 u32_clear_hnode(tp, ht);
359 for (hn = &tp_c->hlist; *hn; hn = &(*hn)->next) {
371 static void u32_destroy(struct tcf_proto *tp)
373 struct tc_u_common *tp_c = tp->data;
374 struct tc_u_hnode *root_ht = xchg(&tp->root, NULL);
376 BUG_TRAP(root_ht != NULL);
378 if (root_ht && --root_ht->refcnt == 0)
379 u32_destroy_hnode(tp, root_ht);
381 if (--tp_c->refcnt == 0) {
382 struct tc_u_hnode *ht;
383 struct tc_u_common **tp_cp;
385 for (tp_cp = &u32_list; *tp_cp; tp_cp = &(*tp_cp)->next) {
386 if (*tp_cp == tp_c) {
392 for (ht=tp_c->hlist; ht; ht = ht->next)
393 u32_clear_hnode(tp, ht);
395 while ((ht = tp_c->hlist) != NULL) {
396 tp_c->hlist = ht->next;
398 BUG_TRAP(ht->refcnt == 0);
410 static int u32_delete(struct tcf_proto *tp, unsigned long arg)
412 struct tc_u_hnode *ht = (struct tc_u_hnode*)arg;
417 if (TC_U32_KEY(ht->handle))
418 return u32_delete_key(tp, (struct tc_u_knode*)ht);
423 if (--ht->refcnt == 0)
424 u32_destroy_hnode(tp, ht);
429 static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
431 struct tc_u_knode *n;
434 for (n=ht->ht[TC_U32_HASH(handle)]; n; n = n->next)
435 if (i < TC_U32_NODE(n->handle))
436 i = TC_U32_NODE(n->handle);
439 return handle|(i>0xFFF ? 0xFFF : i);
442 static int u32_set_parms(struct Qdisc *q, unsigned long base,
443 struct tc_u_hnode *ht,
444 struct tc_u_knode *n, struct rtattr **tb,
447 if (tb[TCA_U32_LINK-1]) {
448 u32 handle = *(u32*)RTA_DATA(tb[TCA_U32_LINK-1]);
449 struct tc_u_hnode *ht_down = NULL;
451 if (TC_U32_KEY(handle))
455 ht_down = u32_lookup_ht(ht->tp_c, handle);
463 ht_down = xchg(&n->ht_down, ht_down);
469 if (tb[TCA_U32_CLASSID-1]) {
472 n->res.classid = *(u32*)RTA_DATA(tb[TCA_U32_CLASSID-1]);
474 cl = __cls_set_class(&n->res.class, q->ops->cl_ops->bind_tcf(q, base, n->res.classid));
477 q->ops->cl_ops->unbind_tcf(q, cl);
479 #ifdef CONFIG_NET_CLS_POLICE
480 if (tb[TCA_U32_POLICE-1]) {
481 struct tcf_police *police = tcf_police_locate(tb[TCA_U32_POLICE-1], est);
484 police = xchg(&n->police, police);
487 tcf_police_release(police);
493 static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
497 struct tc_u_common *tp_c = tp->data;
498 struct tc_u_hnode *ht;
499 struct tc_u_knode *n;
500 struct tc_u32_sel *s;
501 struct rtattr *opt = tca[TCA_OPTIONS-1];
502 struct rtattr *tb[TCA_U32_MAX];
507 return handle ? -EINVAL : 0;
509 if (rtattr_parse(tb, TCA_U32_MAX, RTA_DATA(opt), RTA_PAYLOAD(opt)) < 0)
512 if ((n = (struct tc_u_knode*)*arg) != NULL) {
513 if (TC_U32_KEY(n->handle) == 0)
516 return u32_set_parms(tp->q, base, n->ht_up, n, tb, tca[TCA_RATE-1]);
519 if (tb[TCA_U32_DIVISOR-1]) {
520 unsigned divisor = *(unsigned*)RTA_DATA(tb[TCA_U32_DIVISOR-1]);
522 if (--divisor > 0x100)
524 if (TC_U32_KEY(handle))
527 handle = gen_new_htid(tp->data);
531 ht = kmalloc(sizeof(*ht) + divisor*sizeof(void*), GFP_KERNEL);
534 memset(ht, 0, sizeof(*ht) + divisor*sizeof(void*));
537 ht->divisor = divisor;
540 ht->next = tp_c->hlist;
542 *arg = (unsigned long)ht;
546 if (tb[TCA_U32_HASH-1]) {
547 htid = *(unsigned*)RTA_DATA(tb[TCA_U32_HASH-1]);
548 if (TC_U32_HTID(htid) == TC_U32_ROOT) {
552 ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
561 if (ht->divisor < TC_U32_HASH(htid))
565 if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
567 handle = htid | TC_U32_NODE(handle);
569 handle = gen_new_kid(ht, htid);
571 if (tb[TCA_U32_SEL-1] == 0 ||
572 RTA_PAYLOAD(tb[TCA_U32_SEL-1]) < sizeof(struct tc_u32_sel))
575 s = RTA_DATA(tb[TCA_U32_SEL-1]);
576 n = kmalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
579 memset(n, 0, sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key));
580 memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
583 err = u32_set_parms(tp->q, base, ht, n, tb, tca[TCA_RATE-1]);
585 struct tc_u_knode **ins;
586 for (ins = &ht->ht[TC_U32_HASH(handle)]; *ins; ins = &(*ins)->next)
587 if (TC_U32_NODE(handle) < TC_U32_NODE((*ins)->handle))
594 *arg = (unsigned long)n;
601 static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
603 struct tc_u_common *tp_c = tp->data;
604 struct tc_u_hnode *ht;
605 struct tc_u_knode *n;
611 for (ht = tp_c->hlist; ht; ht = ht->next) {
612 if (ht->prio != tp->prio)
614 if (arg->count >= arg->skip) {
615 if (arg->fn(tp, (unsigned long)ht, arg) < 0) {
621 for (h = 0; h <= ht->divisor; h++) {
622 for (n = ht->ht[h]; n; n = n->next) {
623 if (arg->count < arg->skip) {
627 if (arg->fn(tp, (unsigned long)n, arg) < 0) {
637 static int u32_dump(struct tcf_proto *tp, unsigned long fh,
638 struct sk_buff *skb, struct tcmsg *t)
640 struct tc_u_knode *n = (struct tc_u_knode*)fh;
641 unsigned char *b = skb->tail;
647 t->tcm_handle = n->handle;
649 rta = (struct rtattr*)b;
650 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
652 if (TC_U32_KEY(n->handle) == 0) {
653 struct tc_u_hnode *ht = (struct tc_u_hnode*)fh;
654 u32 divisor = ht->divisor+1;
655 RTA_PUT(skb, TCA_U32_DIVISOR, 4, &divisor);
657 RTA_PUT(skb, TCA_U32_SEL,
658 sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
661 u32 htid = n->handle & 0xFFFFF000;
662 RTA_PUT(skb, TCA_U32_HASH, 4, &htid);
665 RTA_PUT(skb, TCA_U32_CLASSID, 4, &n->res.classid);
667 RTA_PUT(skb, TCA_U32_LINK, 4, &n->ht_down->handle);
668 #ifdef CONFIG_NET_CLS_POLICE
670 struct rtattr * p_rta = (struct rtattr*)skb->tail;
672 RTA_PUT(skb, TCA_U32_POLICE, 0, NULL);
674 if (tcf_police_dump(skb, n->police) < 0)
677 p_rta->rta_len = skb->tail - (u8*)p_rta;
682 rta->rta_len = skb->tail - b;
683 #ifdef CONFIG_NET_CLS_POLICE
684 if (TC_U32_KEY(n->handle) && n->police) {
685 if (qdisc_copy_stats(skb, &n->police->stats))
692 skb_trim(skb, b - skb->data);
696 struct tcf_proto_ops cls_u32_ops = {
712 int init_module(void)
714 return register_tcf_proto_ops(&cls_u32_ops);
717 void cleanup_module(void)
719 unregister_tcf_proto_ops(&cls_u32_ops);
722 MODULE_LICENSE("GPL");