VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / net / sched / cls_u32.c
1 /*
2  * net/sched/cls_u32.c  Ugly (or Universal) 32bit key Packet Classifier.
3  *
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.
8  *
9  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *
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.
14  *
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.
19  *
20  *      It seems that it represents the best middle point between
21  *      speed and manageability both by human and by machine.
22  *
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.
26  *
27  *      JHS: We should remove the CONFIG_NET_CLS_IND from here
28  *      eventually when the meta match extension is made available
29  *
30  */
31
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>
41 #include <linux/mm.h>
42 #include <linux/socket.h>
43 #include <linux/sockios.h>
44 #include <linux/in.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>
53 #include <net/ip.h>
54 #include <net/route.h>
55 #include <linux/skbuff.h>
56 #include <net/sock.h>
57 #include <net/pkt_sched.h>
58
59
60 struct tc_u_knode
61 {
62         struct tc_u_knode       *next;
63         u32                     handle;
64         struct tc_u_hnode       *ht_up;
65 #ifdef CONFIG_NET_CLS_ACT
66         struct tc_action        *action;
67 #else
68 #ifdef CONFIG_NET_CLS_POLICE
69         struct tcf_police       *police;
70 #endif
71 #endif
72 #ifdef CONFIG_NET_CLS_IND
73         char                     indev[IFNAMSIZ];
74 #endif
75         u8                      fshift;
76         struct tcf_result       res;
77         struct tc_u_hnode       *ht_down;
78 #ifdef CONFIG_CLS_U32_PERF
79         struct tc_u32_pcnt      *pf;
80 #endif
81         struct tc_u32_sel       sel;
82 };
83
84 struct tc_u_hnode
85 {
86         struct tc_u_hnode       *next;
87         u32                     handle;
88         struct tc_u_common      *tp_c;
89         int                     refcnt;
90         unsigned                divisor;
91         u32                     hgenerator;
92         struct tc_u_knode       *ht[1];
93 };
94
95 struct tc_u_common
96 {
97         struct tc_u_common      *next;
98         struct tc_u_hnode       *hlist;
99         struct Qdisc            *q;
100         int                     refcnt;
101         u32                     hgenerator;
102 };
103
104 static struct tc_u_common *u32_list;
105
106 static __inline__ unsigned u32_hash_fold(u32 key, struct tc_u32_sel *sel, u8 fshift)
107 {
108         unsigned h = (key & sel->hmask)>>fshift;
109
110         return h;
111 }
112
113 static int u32_classify(struct sk_buff *skb, struct tcf_proto *tp, struct tcf_result *res)
114 {
115         struct {
116                 struct tc_u_knode *knode;
117                 u8                *ptr;
118         } stack[TC_U32_MAXDEPTH];
119
120         struct tc_u_hnode *ht = (struct tc_u_hnode*)tp->root;
121         u8 *ptr = skb->nh.raw;
122         struct tc_u_knode *n;
123         int sdepth = 0;
124         int off2 = 0;
125         int sel = 0;
126 #ifdef CONFIG_CLS_U32_PERF
127         int j;
128 #endif
129         int i;
130
131 next_ht:
132         n = ht->ht[sel];
133
134 next_knode:
135         if (n) {
136                 struct tc_u32_key *key = n->sel.keys;
137
138 #ifdef CONFIG_CLS_U32_PERF
139                 n->pf->rcnt +=1;
140                 j = 0;
141 #endif
142                 for (i = n->sel.nkeys; i>0; i--, key++) {
143
144                         if ((*(u32*)(ptr+key->off+(off2&key->offmask))^key->val)&key->mask) {
145                                 n = n->next;
146                                 goto next_knode;
147                         }
148 #ifdef CONFIG_CLS_U32_PERF
149                         n->pf->kcnts[j] +=1;
150                         j++;
151 #endif
152                 }
153                 if (n->ht_down == NULL) {
154 check_terminal:
155                         if (n->sel.flags&TC_U32_TERMINAL) {
156
157                                 *res = n->res;
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) {
163                                                 n = n->next;
164                                                 goto next_knode;
165                                         } else {
166                                                 if (0 != strcmp(n->indev, skb->input_dev->name)) {
167                                                         n = n->next;
168                                                         goto next_knode;
169                                                 }
170                                         }
171                                 }
172 #endif
173 #ifdef CONFIG_CLS_U32_PERF
174                                 n->pf->rhit +=1;
175 #endif
176 #ifdef CONFIG_NET_CLS_ACT
177                                 if (n->action) {
178                                         int pol_res = tcf_action_exec(skb, n->action);
179                                         if (skb->tc_classid > 0) {
180                                                 res->classid = skb->tc_classid;
181                                                 skb->tc_classid = 0;
182                                         }
183
184                                         if (pol_res >= 0)
185                                                 return pol_res;
186                                 } else
187 #else
188 #ifdef CONFIG_NET_CLS_POLICE
189                                 if (n->police) {
190                                         int pol_res = tcf_police(skb, n->police);
191                                         if (pol_res >= 0)
192                                                 return pol_res;
193                                 } else
194 #endif
195 #endif
196                                         return 0;
197                         }
198                         n = n->next;
199                         goto next_knode;
200                 }
201
202                 /* PUSH */
203                 if (sdepth >= TC_U32_MAXDEPTH)
204                         goto deadloop;
205                 stack[sdepth].knode = n;
206                 stack[sdepth].ptr = ptr;
207                 sdepth++;
208
209                 ht = n->ht_down;
210                 sel = 0;
211                 if (ht->divisor)
212                         sel = ht->divisor&u32_hash_fold(*(u32*)(ptr+n->sel.hoff), &n->sel,n->fshift);
213
214                 if (!(n->sel.flags&(TC_U32_VAROFFSET|TC_U32_OFFSET|TC_U32_EAT)))
215                         goto next_ht;
216
217                 if (n->sel.flags&(TC_U32_OFFSET|TC_U32_VAROFFSET)) {
218                         off2 = n->sel.off + 3;
219                         if (n->sel.flags&TC_U32_VAROFFSET)
220                                 off2 += ntohs(n->sel.offmask & *(u16*)(ptr+n->sel.offoff)) >>n->sel.offshift;
221                         off2 &= ~3;
222                 }
223                 if (n->sel.flags&TC_U32_EAT) {
224                         ptr += off2;
225                         off2 = 0;
226                 }
227
228                 if (ptr < skb->tail)
229                         goto next_ht;
230         }
231
232         /* POP */
233         if (sdepth--) {
234                 n = stack[sdepth].knode;
235                 ht = n->ht_up;
236                 ptr = stack[sdepth].ptr;
237                 goto check_terminal;
238         }
239         return -1;
240
241 deadloop:
242         if (net_ratelimit())
243                 printk("cls_u32: dead loop\n");
244         return -1;
245 }
246
247 static __inline__ struct tc_u_hnode *
248 u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
249 {
250         struct tc_u_hnode *ht;
251
252         for (ht = tp_c->hlist; ht; ht = ht->next)
253                 if (ht->handle == handle)
254                         break;
255
256         return ht;
257 }
258
259 static __inline__ struct tc_u_knode *
260 u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
261 {
262         unsigned sel;
263         struct tc_u_knode *n = NULL;
264
265         sel = TC_U32_HASH(handle);
266         if (sel > ht->divisor)
267                 goto out;
268
269         for (n = ht->ht[sel]; n; n = n->next)
270                 if (n->handle == handle)
271                         break;
272 out:
273         return n;
274 }
275
276
277 static unsigned long u32_get(struct tcf_proto *tp, u32 handle)
278 {
279         struct tc_u_hnode *ht;
280         struct tc_u_common *tp_c = tp->data;
281
282         if (TC_U32_HTID(handle) == TC_U32_ROOT)
283                 ht = tp->root;
284         else
285                 ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
286
287         if (!ht)
288                 return 0;
289
290         if (TC_U32_KEY(handle) == 0)
291                 return (unsigned long)ht;
292
293         return (unsigned long)u32_lookup_key(ht, handle);
294 }
295
296 static void u32_put(struct tcf_proto *tp, unsigned long f)
297 {
298 }
299
300 static u32 gen_new_htid(struct tc_u_common *tp_c)
301 {
302         int i = 0x800;
303
304         do {
305                 if (++tp_c->hgenerator == 0x7FF)
306                         tp_c->hgenerator = 1;
307         } while (--i>0 && u32_lookup_ht(tp_c, (tp_c->hgenerator|0x800)<<20));
308
309         return i > 0 ? (tp_c->hgenerator|0x800)<<20 : 0;
310 }
311
312 static int u32_init(struct tcf_proto *tp)
313 {
314         struct tc_u_hnode *root_ht;
315         struct tc_u_common *tp_c;
316
317         for (tp_c = u32_list; tp_c; tp_c = tp_c->next)
318                 if (tp_c->q == tp->q)
319                         break;
320
321         root_ht = kmalloc(sizeof(*root_ht), GFP_KERNEL);
322         if (root_ht == NULL)
323                 return -ENOBUFS;
324
325         memset(root_ht, 0, sizeof(*root_ht));
326         root_ht->divisor = 0;
327         root_ht->refcnt++;
328         root_ht->handle = tp_c ? gen_new_htid(tp_c) : 0x80000000;
329
330         if (tp_c == NULL) {
331                 tp_c = kmalloc(sizeof(*tp_c), GFP_KERNEL);
332                 if (tp_c == NULL) {
333                         kfree(root_ht);
334                         return -ENOBUFS;
335                 }
336                 memset(tp_c, 0, sizeof(*tp_c));
337                 tp_c->q = tp->q;
338                 tp_c->next = u32_list;
339                 u32_list = tp_c;
340         }
341
342         tp_c->refcnt++;
343         root_ht->next = tp_c->hlist;
344         tp_c->hlist = root_ht;
345         root_ht->tp_c = tp_c;
346
347         tp->root = root_ht;
348         tp->data = tp_c;
349         return 0;
350 }
351
352 static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n)
353 {
354         unsigned long cl;
355
356         if ((cl = __cls_set_class(&n->res.class, 0)) != 0)
357                 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
358 #ifdef CONFIG_NET_CLS_ACT
359         if (n->action) {
360                 tcf_action_destroy(n->action, TCA_ACT_UNBIND);
361         }
362 #else
363 #ifdef CONFIG_NET_CLS_POLICE
364         tcf_police_release(n->police, TCA_ACT_UNBIND);
365 #endif
366 #endif
367         if (n->ht_down)
368                 n->ht_down->refcnt--;
369 #ifdef CONFIG_CLS_U32_PERF
370         if (n && (NULL != n->pf))
371                 kfree(n->pf);
372 #endif
373         kfree(n);
374         return 0;
375 }
376
377 static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode* key)
378 {
379         struct tc_u_knode **kp;
380         struct tc_u_hnode *ht = key->ht_up;
381
382         if (ht) {
383                 for (kp = &ht->ht[TC_U32_HASH(key->handle)]; *kp; kp = &(*kp)->next) {
384                         if (*kp == key) {
385                                 tcf_tree_lock(tp);
386                                 *kp = key->next;
387                                 tcf_tree_unlock(tp);
388
389                                 u32_destroy_key(tp, key);
390                                 return 0;
391                         }
392                 }
393         }
394         BUG_TRAP(0);
395         return 0;
396 }
397
398 static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
399 {
400         struct tc_u_knode *n;
401         unsigned h;
402
403         for (h=0; h<=ht->divisor; h++) {
404                 while ((n = ht->ht[h]) != NULL) {
405                         ht->ht[h] = n->next;
406
407                         u32_destroy_key(tp, n);
408                 }
409         }
410 }
411
412 static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
413 {
414         struct tc_u_common *tp_c = tp->data;
415         struct tc_u_hnode **hn;
416
417         BUG_TRAP(!ht->refcnt);
418
419         u32_clear_hnode(tp, ht);
420
421         for (hn = &tp_c->hlist; *hn; hn = &(*hn)->next) {
422                 if (*hn == ht) {
423                         *hn = ht->next;
424                         kfree(ht);
425                         return 0;
426                 }
427         }
428
429         BUG_TRAP(0);
430         return -ENOENT;
431 }
432
433 static void u32_destroy(struct tcf_proto *tp)
434 {
435         struct tc_u_common *tp_c = tp->data;
436         struct tc_u_hnode *root_ht = xchg(&tp->root, NULL);
437
438         BUG_TRAP(root_ht != NULL);
439
440         if (root_ht && --root_ht->refcnt == 0)
441                 u32_destroy_hnode(tp, root_ht);
442
443         if (--tp_c->refcnt == 0) {
444                 struct tc_u_hnode *ht;
445                 struct tc_u_common **tp_cp;
446
447                 for (tp_cp = &u32_list; *tp_cp; tp_cp = &(*tp_cp)->next) {
448                         if (*tp_cp == tp_c) {
449                                 *tp_cp = tp_c->next;
450                                 break;
451                         }
452                 }
453
454                 for (ht=tp_c->hlist; ht; ht = ht->next)
455                         u32_clear_hnode(tp, ht);
456
457                 while ((ht = tp_c->hlist) != NULL) {
458                         tp_c->hlist = ht->next;
459
460                         BUG_TRAP(ht->refcnt == 0);
461
462                         kfree(ht);
463                 };
464
465                 kfree(tp_c);
466         }
467
468         tp->data = NULL;
469 }
470
471 static int u32_delete(struct tcf_proto *tp, unsigned long arg)
472 {
473         struct tc_u_hnode *ht = (struct tc_u_hnode*)arg;
474
475         if (ht == NULL)
476                 return 0;
477
478         if (TC_U32_KEY(ht->handle))
479                 return u32_delete_key(tp, (struct tc_u_knode*)ht);
480
481         if (tp->root == ht)
482                 return -EINVAL;
483
484         if (--ht->refcnt == 0)
485                 u32_destroy_hnode(tp, ht);
486
487         return 0;
488 }
489
490 static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
491 {
492         struct tc_u_knode *n;
493         unsigned i = 0x7FF;
494
495         for (n=ht->ht[TC_U32_HASH(handle)]; n; n = n->next)
496                 if (i < TC_U32_NODE(n->handle))
497                         i = TC_U32_NODE(n->handle);
498         i++;
499
500         return handle|(i>0xFFF ? 0xFFF : i);
501 }
502
503 static int u32_set_parms(struct Qdisc *q, unsigned long base,
504                          struct tc_u_hnode *ht,
505                          struct tc_u_knode *n, struct rtattr **tb,
506                          struct rtattr *est)
507 {
508 #ifdef CONFIG_NET_CLS_ACT
509         struct tc_action *act = NULL;
510         int ret;
511 #endif
512         if (tb[TCA_U32_LINK-1]) {
513                 u32 handle = *(u32*)RTA_DATA(tb[TCA_U32_LINK-1]);
514                 struct tc_u_hnode *ht_down = NULL;
515
516                 if (TC_U32_KEY(handle))
517                         return -EINVAL;
518
519                 if (handle) {
520                         ht_down = u32_lookup_ht(ht->tp_c, handle);
521
522                         if (ht_down == NULL)
523                                 return -EINVAL;
524                         ht_down->refcnt++;
525                 }
526
527                 sch_tree_lock(q);
528                 ht_down = xchg(&n->ht_down, ht_down);
529                 sch_tree_unlock(q);
530
531                 if (ht_down)
532                         ht_down->refcnt--;
533         }
534         if (tb[TCA_U32_CLASSID-1]) {
535                 unsigned long cl;
536
537                 n->res.classid = *(u32*)RTA_DATA(tb[TCA_U32_CLASSID-1]);
538                 sch_tree_lock(q);
539                 cl = __cls_set_class(&n->res.class, q->ops->cl_ops->bind_tcf(q, base, n->res.classid));
540                 sch_tree_unlock(q);
541                 if (cl)
542                         q->ops->cl_ops->unbind_tcf(q, cl);
543         }
544 #ifdef CONFIG_NET_CLS_ACT
545         /*backward compatibility */
546         if (tb[TCA_U32_POLICE-1])
547         {
548                 act = kmalloc(sizeof(*act),GFP_KERNEL);
549                 if (NULL == act)
550                         return -ENOMEM;
551
552                 memset(act,0,sizeof(*act));
553                 ret = tcf_action_init_1(tb[TCA_U32_POLICE-1], est,act,"police", TCA_ACT_NOREPLACE, TCA_ACT_BIND);
554                 if (0 > ret){
555                         tcf_action_destroy(act, TCA_ACT_UNBIND);
556                         return ret;
557                 }
558                 act->type = TCA_OLD_COMPAT;
559
560                 sch_tree_lock(q);
561                 act = xchg(&n->action, act);
562                 sch_tree_unlock(q);
563
564                 tcf_action_destroy(act, TCA_ACT_UNBIND);
565
566         }
567
568         if(tb[TCA_U32_ACT-1]) {
569                 act = kmalloc(sizeof(*act),GFP_KERNEL);
570                 if (NULL == act)
571                         return -ENOMEM;
572                 memset(act,0,sizeof(*act));
573                 ret = tcf_action_init(tb[TCA_U32_ACT-1], est,act,NULL,TCA_ACT_NOREPLACE, TCA_ACT_BIND);
574                 if (0 > ret) {
575                         tcf_action_destroy(act, TCA_ACT_UNBIND);
576                         return ret;
577                 }
578
579                 sch_tree_lock(q);
580                 act = xchg(&n->action, act);
581                 sch_tree_unlock(q);
582
583                 tcf_action_destroy(act, TCA_ACT_UNBIND);
584         }
585
586
587 #else
588 #ifdef CONFIG_NET_CLS_POLICE
589         if (tb[TCA_U32_POLICE-1]) {
590                 struct tcf_police *police = tcf_police_locate(tb[TCA_U32_POLICE-1], est);
591                 sch_tree_lock(q);
592                 police = xchg(&n->police, police);
593                 sch_tree_unlock(q);
594                 tcf_police_release(police, TCA_ACT_UNBIND);
595         }
596 #endif
597 #endif
598 #ifdef CONFIG_NET_CLS_IND
599         n->indev[0] = 0;
600         if(tb[TCA_U32_INDEV-1]) {
601                 struct rtattr *input_dev = tb[TCA_U32_INDEV-1];
602                 if (RTA_PAYLOAD(input_dev) >= IFNAMSIZ) {
603                         printk("cls_u32: bad indev name %s\n",(char*)RTA_DATA(input_dev));
604                         /* should we clear state first? */
605                         return  -EINVAL;
606                 }
607                 sprintf(n->indev, "%s", (char*)RTA_DATA(input_dev));
608                 printk("got IND %s\n",n->indev);
609         }
610 #endif
611
612         return 0;
613 }
614
615 static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
616                       struct rtattr **tca,
617                       unsigned long *arg)
618 {
619         struct tc_u_common *tp_c = tp->data;
620         struct tc_u_hnode *ht;
621         struct tc_u_knode *n;
622         struct tc_u32_sel *s;
623         struct rtattr *opt = tca[TCA_OPTIONS-1];
624         struct rtattr *tb[TCA_U32_MAX];
625         u32 htid;
626         int err;
627
628         if (opt == NULL)
629                 return handle ? -EINVAL : 0;
630
631         if (rtattr_parse(tb, TCA_U32_MAX, RTA_DATA(opt), RTA_PAYLOAD(opt)) < 0)
632                 return -EINVAL;
633
634         if ((n = (struct tc_u_knode*)*arg) != NULL) {
635                 if (TC_U32_KEY(n->handle) == 0)
636                         return -EINVAL;
637
638                 return u32_set_parms(tp->q, base, n->ht_up, n, tb, tca[TCA_RATE-1]);
639         }
640
641         if (tb[TCA_U32_DIVISOR-1]) {
642                 unsigned divisor = *(unsigned*)RTA_DATA(tb[TCA_U32_DIVISOR-1]);
643
644                 if (--divisor > 0x100)
645                         return -EINVAL;
646                 if (TC_U32_KEY(handle))
647                         return -EINVAL;
648                 if (handle == 0) {
649                         handle = gen_new_htid(tp->data);
650                         if (handle == 0)
651                                 return -ENOMEM;
652                 }
653                 ht = kmalloc(sizeof(*ht) + divisor*sizeof(void*), GFP_KERNEL);
654                 if (ht == NULL)
655                         return -ENOBUFS;
656                 memset(ht, 0, sizeof(*ht) + divisor*sizeof(void*));
657                 ht->tp_c = tp_c;
658                 ht->refcnt = 0;
659                 ht->divisor = divisor;
660                 ht->handle = handle;
661                 ht->next = tp_c->hlist;
662                 tp_c->hlist = ht;
663                 *arg = (unsigned long)ht;
664                 return 0;
665         }
666
667         if (tb[TCA_U32_HASH-1]) {
668                 htid = *(unsigned*)RTA_DATA(tb[TCA_U32_HASH-1]);
669                 if (TC_U32_HTID(htid) == TC_U32_ROOT) {
670                         ht = tp->root;
671                         htid = ht->handle;
672                 } else {
673                         ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
674                         if (ht == NULL)
675                                 return -EINVAL;
676                 }
677         } else {
678                 ht = tp->root;
679                 htid = ht->handle;
680         }
681
682         if (ht->divisor < TC_U32_HASH(htid))
683                 return -EINVAL;
684
685         if (handle) {
686                 if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
687                         return -EINVAL;
688                 handle = htid | TC_U32_NODE(handle);
689         } else
690                 handle = gen_new_kid(ht, htid);
691
692         if (tb[TCA_U32_SEL-1] == 0 ||
693             RTA_PAYLOAD(tb[TCA_U32_SEL-1]) < sizeof(struct tc_u32_sel))
694                 return -EINVAL;
695
696         s = RTA_DATA(tb[TCA_U32_SEL-1]);
697
698         n = kmalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
699         if (n == NULL)
700                 return -ENOBUFS;
701
702         memset(n, 0, sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key));
703 #ifdef CONFIG_CLS_U32_PERF
704         n->pf = kmalloc(sizeof(struct tc_u32_pcnt) + s->nkeys*sizeof(__u64), GFP_KERNEL);
705         if (n->pf == NULL) {
706                 kfree(n);
707                 return -ENOBUFS;
708         }
709         memset(n->pf, 0, sizeof(struct tc_u32_pcnt) + s->nkeys*sizeof(__u64));
710 #endif
711
712         memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
713         n->ht_up = ht;
714         n->handle = handle;
715 {
716         u8 i = 0;
717         u32 mask = s->hmask;
718         if (mask) {
719                 while (!(mask & 1)) {
720                         i++;
721                         mask>>=1;
722                 }
723         }
724         n->fshift = i;
725 }
726         err = u32_set_parms(tp->q, base, ht, n, tb, tca[TCA_RATE-1]);
727         if (err == 0) {
728                 struct tc_u_knode **ins;
729                 for (ins = &ht->ht[TC_U32_HASH(handle)]; *ins; ins = &(*ins)->next)
730                         if (TC_U32_NODE(handle) < TC_U32_NODE((*ins)->handle))
731                                 break;
732
733                 n->next = *ins;
734                 wmb();
735                 *ins = n;
736
737                 *arg = (unsigned long)n;
738                 return 0;
739         }
740 #ifdef CONFIG_CLS_U32_PERF
741         if (n && (NULL != n->pf))
742                 kfree(n->pf);
743 #endif
744         kfree(n);
745         return err;
746 }
747
748 static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
749 {
750         struct tc_u_common *tp_c = tp->data;
751         struct tc_u_hnode *ht;
752         struct tc_u_knode *n;
753         unsigned h;
754
755         if (arg->stop)
756                 return;
757
758         for (ht = tp_c->hlist; ht; ht = ht->next) {
759                 if (arg->count >= arg->skip) {
760                         if (arg->fn(tp, (unsigned long)ht, arg) < 0) {
761                                 arg->stop = 1;
762                                 return;
763                         }
764                 }
765                 arg->count++;
766                 for (h = 0; h <= ht->divisor; h++) {
767                         for (n = ht->ht[h]; n; n = n->next) {
768                                 if (arg->count < arg->skip) {
769                                         arg->count++;
770                                         continue;
771                                 }
772                                 if (arg->fn(tp, (unsigned long)n, arg) < 0) {
773                                         arg->stop = 1;
774                                         return;
775                                 }
776                                 arg->count++;
777                         }
778                 }
779         }
780 }
781
782 static int u32_dump(struct tcf_proto *tp, unsigned long fh,
783                      struct sk_buff *skb, struct tcmsg *t)
784 {
785         struct tc_u_knode *n = (struct tc_u_knode*)fh;
786         unsigned char    *b = skb->tail;
787         struct rtattr *rta;
788
789         if (n == NULL)
790                 return skb->len;
791
792         t->tcm_handle = n->handle;
793
794         rta = (struct rtattr*)b;
795         RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
796
797         if (TC_U32_KEY(n->handle) == 0) {
798                 struct tc_u_hnode *ht = (struct tc_u_hnode*)fh;
799                 u32 divisor = ht->divisor+1;
800                 RTA_PUT(skb, TCA_U32_DIVISOR, 4, &divisor);
801         } else {
802                 RTA_PUT(skb, TCA_U32_SEL,
803                         sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
804                         &n->sel);
805                 if (n->ht_up) {
806                         u32 htid = n->handle & 0xFFFFF000;
807                         RTA_PUT(skb, TCA_U32_HASH, 4, &htid);
808                 }
809                 if (n->res.classid)
810                         RTA_PUT(skb, TCA_U32_CLASSID, 4, &n->res.classid);
811                 if (n->ht_down)
812                         RTA_PUT(skb, TCA_U32_LINK, 4, &n->ht_down->handle);
813 #ifdef CONFIG_NET_CLS_ACT
814                 /* again for backward compatible mode - we want
815                 *  to work with both old and new modes of entering
816                 *  tc data even if iproute2  was newer - jhs 
817                 */
818                 if (n->action) {
819                         struct rtattr * p_rta = (struct rtattr*)skb->tail;
820
821                         if (n->action->type != TCA_OLD_COMPAT) {
822                                 RTA_PUT(skb, TCA_U32_ACT, 0, NULL);
823                                 if (tcf_action_dump(skb,n->action, 0, 0) < 0) {
824                                         goto rtattr_failure;
825                                 }
826                         } else {
827                                 RTA_PUT(skb, TCA_U32_POLICE, 0, NULL);
828                                 if (tcf_action_dump_old(skb,n->action,0,0) < 0) {
829                                         goto rtattr_failure;
830                                 }
831                         }
832
833                         p_rta->rta_len = skb->tail - (u8*)p_rta;
834                 }
835
836 #else
837 #ifdef CONFIG_NET_CLS_POLICE
838                 if (n->police) {
839                         struct rtattr * p_rta = (struct rtattr*)skb->tail;
840                         RTA_PUT(skb, TCA_U32_POLICE, 0, NULL);
841          
842                         if (tcf_police_dump(skb, n->police) < 0)
843                                 goto rtattr_failure;
844
845                         p_rta->rta_len = skb->tail - (u8*)p_rta;
846
847                 }
848 #endif
849 #endif
850
851 #ifdef CONFIG_NET_CLS_IND
852                 if(strlen(n->indev)) {
853                         struct rtattr * p_rta = (struct rtattr*)skb->tail;
854                         RTA_PUT(skb, TCA_U32_INDEV, IFNAMSIZ, n->indev);
855                         p_rta->rta_len = skb->tail - (u8*)p_rta;
856                 }
857 #endif
858 #ifdef CONFIG_CLS_U32_PERF
859                 RTA_PUT(skb, TCA_U32_PCNT, 
860                 sizeof(struct tc_u32_pcnt) + n->sel.nkeys*sizeof(__u64),
861                         n->pf);
862 #endif
863         }
864
865         rta->rta_len = skb->tail - b;
866 #ifdef CONFIG_NET_CLS_ACT
867         if (TC_U32_KEY(n->handle) != 0) {
868                 if (TC_U32_KEY(n->handle) && n->action && n->action->type == TCA_OLD_COMPAT) {
869                         if (tcf_action_copy_stats(skb,n->action))
870                                 goto rtattr_failure;
871                 }
872         }
873 #else
874 #ifdef CONFIG_NET_CLS_POLICE
875         if (TC_U32_KEY(n->handle) && n->police) {
876                 if (qdisc_copy_stats(skb, &n->police->stats,
877                                      n->police->stats_lock))
878                         goto rtattr_failure;
879         }
880 #endif
881 #endif
882         return skb->len;
883
884 rtattr_failure:
885         skb_trim(skb, b - skb->data);
886         return -1;
887 }
888
889 static struct tcf_proto_ops cls_u32_ops = {
890         .next           =       NULL,
891         .kind           =       "u32",
892         .classify       =       u32_classify,
893         .init           =       u32_init,
894         .destroy        =       u32_destroy,
895         .get            =       u32_get,
896         .put            =       u32_put,
897         .change         =       u32_change,
898         .delete         =       u32_delete,
899         .walk           =       u32_walk,
900         .dump           =       u32_dump,
901         .owner          =       THIS_MODULE,
902 };
903
904 static int __init init_u32(void)
905 {
906         printk("u32 classifier\n");
907 #ifdef CONFIG_CLS_U32_PERF
908         printk("    Perfomance counters on\n");
909 #endif
910 #ifdef CONFIG_NET_CLS_POLICE
911         printk("    OLD policer on \n");
912 #endif
913 #ifdef CONFIG_NET_CLS_IND
914         printk("    input device check on \n");
915 #endif
916 #ifdef CONFIG_NET_CLS_ACT
917         printk("    Actions configured \n");
918 #endif
919         return register_tcf_proto_ops(&cls_u32_ops);
920 }
921
922 static void __exit exit_u32(void) 
923 {
924         unregister_tcf_proto_ops(&cls_u32_ops);
925 }
926
927 module_init(init_u32)
928 module_exit(exit_u32)
929 MODULE_LICENSE("GPL");