VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / net / sched / sch_htb.c
1 /* vim: ts=8 sw=8
2  * net/sched/sch_htb.c  Hierarchical token bucket, feed tree version
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:     Martin Devera, <devik@cdi.cz>
10  *
11  * Credits (in time order) for older HTB versions:
12  *              Stef Coene <stef.coene@docum.org>
13  *                      HTB support at LARTC mailing list
14  *              Ondrej Kraus, <krauso@barr.cz> 
15  *                      found missing INIT_QDISC(htb)
16  *              Vladimir Smelhaus, Aamer Akhter, Bert Hubert
17  *                      helped a lot to locate nasty class stall bug
18  *              Andi Kleen, Jamal Hadi, Bert Hubert
19  *                      code review and helpful comments on shaping
20  *              Tomasz Wrona, <tw@eter.tym.pl>
21  *                      created test case so that I was able to fix nasty bug
22  *              Wilfried Weissmann
23  *                      spotted bug in dequeue code and helped with fix
24  *              Jiri Fojtasek
25  *                      fixed requeue routine
26  *              and many others. thanks.
27  *
28  * $Id: sch_htb.c,v 1.25 2003/12/07 11:08:25 devik Exp devik $
29  */
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <asm/uaccess.h>
33 #include <asm/system.h>
34 #include <asm/bitops.h>
35 #include <linux/types.h>
36 #include <linux/kernel.h>
37 #include <linux/sched.h>
38 #include <linux/string.h>
39 #include <linux/mm.h>
40 #include <linux/socket.h>
41 #include <linux/sockios.h>
42 #include <linux/in.h>
43 #include <linux/errno.h>
44 #include <linux/interrupt.h>
45 #include <linux/if_ether.h>
46 #include <linux/inet.h>
47 #include <linux/netdevice.h>
48 #include <linux/etherdevice.h>
49 #include <linux/notifier.h>
50 #include <net/ip.h>
51 #include <net/route.h>
52 #include <linux/skbuff.h>
53 #include <linux/list.h>
54 #include <linux/compiler.h>
55 #include <net/sock.h>
56 #include <net/pkt_sched.h>
57 #include <linux/rbtree.h>
58
59 /* HTB algorithm.
60     Author: devik@cdi.cz
61     ========================================================================
62     HTB is like TBF with multiple classes. It is also similar to CBQ because
63     it allows to assign priority to each class in hierarchy. 
64     In fact it is another implementation of Floyd's formal sharing.
65
66     Levels:
67     Each class is assigned level. Leaf has ALWAYS level 0 and root 
68     classes have level TC_HTB_MAXDEPTH-1. Interior nodes has level
69     one less than their parent.
70 */
71
72 #define HTB_HSIZE 16    /* classid hash size */
73 #define HTB_EWMAC 2     /* rate average over HTB_EWMAC*HTB_HSIZE sec */
74 #define HTB_DEBUG 1     /* compile debugging support (activated by tc tool) */
75 #define HTB_RATECM 1    /* whether to use rate computer */
76 #define HTB_HYSTERESIS 1/* whether to use mode hysteresis for speedup */
77 #define HTB_QLOCK(S) spin_lock_bh(&(S)->dev->queue_lock)
78 #define HTB_QUNLOCK(S) spin_unlock_bh(&(S)->dev->queue_lock)
79 #define HTB_VER 0x30011 /* major must be matched with number suplied by TC as version */
80
81 #if HTB_VER >> 16 != TC_HTB_PROTOVER
82 #error "Mismatched sch_htb.c and pkt_sch.h"
83 #endif
84
85 /* debugging support; S is subsystem, these are defined:
86   0 - netlink messages
87   1 - enqueue
88   2 - drop & requeue
89   3 - dequeue main
90   4 - dequeue one prio DRR part
91   5 - dequeue class accounting
92   6 - class overlimit status computation
93   7 - hint tree
94   8 - event queue
95  10 - rate estimator
96  11 - classifier 
97  12 - fast dequeue cache
98
99  L is level; 0 = none, 1 = basic info, 2 = detailed, 3 = full
100  q->debug uint32 contains 16 2-bit fields one for subsystem starting
101  from LSB
102  */
103 #ifdef HTB_DEBUG
104 #define HTB_DBG_COND(S,L) (((q->debug>>(2*S))&3) >= L)
105 #define HTB_DBG(S,L,FMT,ARG...) if (HTB_DBG_COND(S,L)) \
106         printk(KERN_DEBUG FMT,##ARG)
107 #define HTB_CHCL(cl) BUG_TRAP((cl)->magic == HTB_CMAGIC)
108 #define HTB_PASSQ q,
109 #define HTB_ARGQ struct htb_sched *q,
110 #define static
111 #undef __inline__
112 #define __inline__
113 #undef inline
114 #define inline
115 #define HTB_CMAGIC 0xFEFAFEF1
116 #define htb_safe_rb_erase(N,R) do { BUG_TRAP((N)->rb_color != -1); \
117                 if ((N)->rb_color == -1) break; \
118                 rb_erase(N,R); \
119                 (N)->rb_color = -1; } while (0)
120 #else
121 #define HTB_DBG_COND(S,L) (0)
122 #define HTB_DBG(S,L,FMT,ARG...)
123 #define HTB_PASSQ
124 #define HTB_ARGQ
125 #define HTB_CHCL(cl)
126 #define htb_safe_rb_erase(N,R) rb_erase(N,R)
127 #endif
128
129
130 /* used internaly to keep status of single class */
131 enum htb_cmode {
132     HTB_CANT_SEND,              /* class can't send and can't borrow */
133     HTB_MAY_BORROW,             /* class can't send but may borrow */
134     HTB_CAN_SEND                /* class can send */
135 };
136
137 /* interior & leaf nodes; props specific to leaves are marked L: */
138 struct htb_class
139 {
140 #ifdef HTB_DEBUG
141         unsigned magic;
142 #endif
143     /* general class parameters */
144     u32 classid;
145     struct tc_stats     stats;  /* generic stats */
146     spinlock_t          *stats_lock;
147     struct tc_htb_xstats xstats;/* our special stats */
148     int refcnt;                 /* usage count of this class */
149
150 #ifdef HTB_RATECM
151     /* rate measurement counters */
152     unsigned long rate_bytes,sum_bytes;
153     unsigned long rate_packets,sum_packets;
154 #endif
155
156     /* topology */
157     int level;                  /* our level (see above) */
158     struct htb_class *parent;   /* parent class */
159     struct list_head hlist;     /* classid hash list item */
160     struct list_head sibling;   /* sibling list item */
161     struct list_head children;  /* children list */
162
163     union {
164             struct htb_class_leaf {
165                     struct Qdisc *q;
166                     int prio;
167                     int aprio;  
168                     int quantum;
169                     int deficit[TC_HTB_MAXDEPTH];
170                     struct list_head drop_list;
171             } leaf;
172             struct htb_class_inner {
173                     struct rb_root feed[TC_HTB_NUMPRIO]; /* feed trees */
174                     struct rb_node *ptr[TC_HTB_NUMPRIO]; /* current class ptr */
175             /* When class changes from state 1->2 and disconnects from 
176                parent's feed then we lost ptr value and start from the
177               first child again. Here we store classid of the
178               last valid ptr (used when ptr is NULL). */
179               u32 last_ptr_id[TC_HTB_NUMPRIO];
180             } inner;
181     } un;
182     struct rb_node node[TC_HTB_NUMPRIO]; /* node for self or feed tree */
183     struct rb_node pq_node;              /* node for event queue */
184     unsigned long pq_key;       /* the same type as jiffies global */
185     
186     int prio_activity;          /* for which prios are we active */
187     enum htb_cmode cmode;       /* current mode of the class */
188
189     /* class attached filters */
190     struct tcf_proto *filter_list;
191     int filter_cnt;
192
193     int warned;         /* only one warning about non work conserving .. */
194
195     /* token bucket parameters */
196     struct qdisc_rate_table *rate;      /* rate table of the class itself */
197     struct qdisc_rate_table *ceil;      /* ceiling rate (limits borrows too) */
198     long buffer,cbuffer;                /* token bucket depth/rate */
199     long mbuffer;                       /* max wait time */
200     long tokens,ctokens;                /* current number of tokens */
201     psched_time_t t_c;                  /* checkpoint time */
202 };
203
204 /* TODO: maybe compute rate when size is too large .. or drop ? */
205 static __inline__ long L2T(struct htb_class *cl,struct qdisc_rate_table *rate,
206         int size)
207
208     int slot = size >> rate->rate.cell_log;
209     if (slot > 255) {
210         cl->xstats.giants++;
211         slot = 255;
212     }
213     return rate->data[slot];
214 }
215
216 struct htb_sched
217 {
218     struct list_head root;                      /* root classes list */
219     struct list_head hash[HTB_HSIZE];           /* hashed by classid */
220     struct list_head drops[TC_HTB_NUMPRIO];     /* active leaves (for drops) */
221     
222     /* self list - roots of self generating tree */
223     struct rb_root row[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
224     int row_mask[TC_HTB_MAXDEPTH];
225     struct rb_node *ptr[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
226     u32 last_ptr_id[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
227
228     /* self wait list - roots of wait PQs per row */
229     struct rb_root wait_pq[TC_HTB_MAXDEPTH];
230
231     /* time of nearest event per level (row) */
232     unsigned long near_ev_cache[TC_HTB_MAXDEPTH];
233
234     /* cached value of jiffies in dequeue */
235     unsigned long jiffies;
236
237     /* whether we hit non-work conserving class during this dequeue; we use */
238     int nwc_hit;        /* this to disable mindelay complaint in dequeue */
239
240     int defcls;         /* class where unclassified flows go to */
241     u32 debug;          /* subsystem debug levels */
242
243     /* filters for qdisc itself */
244     struct tcf_proto *filter_list;
245     int filter_cnt;
246
247     int rate2quantum;           /* quant = rate / rate2quantum */
248     psched_time_t now;          /* cached dequeue time */
249     struct timer_list timer;    /* send delay timer */
250 #ifdef HTB_RATECM
251     struct timer_list rttim;    /* rate computer timer */
252     int recmp_bucket;           /* which hash bucket to recompute next */
253 #endif
254     
255     /* non shaped skbs; let them go directly thru */
256     struct sk_buff_head direct_queue;
257     int direct_qlen;  /* max qlen of above */
258
259     long direct_pkts;
260 };
261
262 /* compute hash of size HTB_HSIZE for given handle */
263 static __inline__ int htb_hash(u32 h) 
264 {
265 #if HTB_HSIZE != 16
266  #error "Declare new hash for your HTB_HSIZE"
267 #endif
268     h ^= h>>8;  /* stolen from cbq_hash */
269     h ^= h>>4;
270     return h & 0xf;
271 }
272
273 /* find class in global hash table using given handle */
274 static __inline__ struct htb_class *htb_find(u32 handle, struct Qdisc *sch)
275 {
276         struct htb_sched *q = qdisc_priv(sch);
277         struct list_head *p;
278         if (TC_H_MAJ(handle) != sch->handle) 
279                 return NULL;
280         
281         list_for_each (p,q->hash+htb_hash(handle)) {
282                 struct htb_class *cl = list_entry(p,struct htb_class,hlist);
283                 if (cl->classid == handle)
284                         return cl;
285         }
286         return NULL;
287 }
288
289 /**
290  * htb_classify - classify a packet into class
291  *
292  * It returns NULL if the packet should be dropped or -1 if the packet
293  * should be passed directly thru. In all other cases leaf class is returned.
294  * We allow direct class selection by classid in priority. The we examine
295  * filters in qdisc and in inner nodes (if higher filter points to the inner
296  * node). If we end up with classid MAJOR:0 we enqueue the skb into special
297  * internal fifo (direct). These packets then go directly thru. If we still 
298  * have no valid leaf we try to use MAJOR:default leaf. It still unsuccessfull
299  * then finish and return direct queue.
300  */
301 #define HTB_DIRECT (struct htb_class*)-1
302 static inline u32 htb_classid(struct htb_class *cl)
303 {
304         return (cl && cl != HTB_DIRECT) ? cl->classid : TC_H_UNSPEC;
305 }
306
307 static struct htb_class *htb_classify(struct sk_buff *skb, struct Qdisc *sch, int *qres)
308 {
309         struct htb_sched *q = qdisc_priv(sch);
310         struct htb_class *cl;
311         struct tcf_result res;
312         struct tcf_proto *tcf;
313         int result;
314
315         /* allow to select class by setting skb->priority to valid classid;
316            note that nfmark can be used too by attaching filter fw with no
317            rules in it */
318         if (skb->priority == sch->handle)
319                 return HTB_DIRECT;  /* X:0 (direct flow) selected */
320         if ((cl = htb_find(skb->priority,sch)) != NULL && cl->level == 0) 
321                 return cl;
322
323         tcf = q->filter_list;
324         while (tcf && (result = tc_classify(skb, tcf, &res)) >= 0) {
325 #ifdef CONFIG_NET_CLS_ACT
326                 int terminal = 0;
327                 switch (result) {
328                 case TC_ACT_SHOT: /* Stop and kfree */
329                         *qres = NET_XMIT_DROP;
330                         terminal = 1;
331                         break;
332                 case TC_ACT_QUEUED:
333                 case TC_ACT_STOLEN: 
334                         terminal = 1;
335                         break;
336                 case TC_ACT_RECLASSIFY:  /* Things look good */
337                 case TC_ACT_OK:
338                 case TC_ACT_UNSPEC:
339                 default:
340                 break;
341                 }
342
343                 if (terminal) {
344                         kfree_skb(skb);
345                         return NULL;
346                 }
347 #else
348 #ifdef CONFIG_NET_CLS_POLICE
349                 if (result == TC_POLICE_SHOT)
350                         return NULL;
351 #endif
352 #endif
353                 if ((cl = (void*)res.class) == NULL) {
354                         if (res.classid == sch->handle)
355                                 return HTB_DIRECT;  /* X:0 (direct flow) */
356                         if ((cl = htb_find(res.classid,sch)) == NULL)
357                                 break; /* filter selected invalid classid */
358                 }
359                 if (!cl->level)
360                         return cl; /* we hit leaf; return it */
361
362                 /* we have got inner class; apply inner filter chain */
363                 tcf = cl->filter_list;
364         }
365         /* classification failed; try to use default class */
366         cl = htb_find(TC_H_MAKE(TC_H_MAJ(sch->handle),q->defcls),sch);
367         if (!cl || cl->level)
368                 return HTB_DIRECT; /* bad default .. this is safe bet */
369         return cl;
370 }
371
372 #ifdef HTB_DEBUG
373 static void htb_next_rb_node(struct rb_node **n);
374 #define HTB_DUMTREE(root,memb) if(root) { \
375         struct rb_node *n = (root)->rb_node; \
376         while (n->rb_left) n = n->rb_left; \
377         while (n) { \
378                 struct htb_class *cl = rb_entry(n, struct htb_class, memb); \
379                 printk(" %x",cl->classid); htb_next_rb_node (&n); \
380         } }
381
382 static void htb_debug_dump (struct htb_sched *q)
383 {
384         int i,p;
385         printk(KERN_DEBUG "htb*g j=%lu lj=%lu\n",jiffies,q->jiffies);
386         /* rows */
387         for (i=TC_HTB_MAXDEPTH-1;i>=0;i--) {
388                 printk(KERN_DEBUG "htb*r%d m=%x",i,q->row_mask[i]);
389                 for (p=0;p<TC_HTB_NUMPRIO;p++) {
390                         if (!q->row[i][p].rb_node) continue;
391                         printk(" p%d:",p);
392                         HTB_DUMTREE(q->row[i]+p,node[p]);
393                 }
394                 printk("\n");
395         }
396         /* classes */
397         for (i = 0; i < HTB_HSIZE; i++) {
398                 struct list_head *l;
399                 list_for_each (l,q->hash+i) {
400                         struct htb_class *cl = list_entry(l,struct htb_class,hlist);
401                         long diff = PSCHED_TDIFF_SAFE(q->now, cl->t_c, (u32)cl->mbuffer);
402                         printk(KERN_DEBUG "htb*c%x m=%d t=%ld c=%ld pq=%lu df=%ld ql=%d "
403                                         "pa=%x f:",
404                                 cl->classid,cl->cmode,cl->tokens,cl->ctokens,
405                                 cl->pq_node.rb_color==-1?0:cl->pq_key,diff,
406                                 cl->level?0:cl->un.leaf.q->q.qlen,cl->prio_activity);
407                         if (cl->level)
408                         for (p=0;p<TC_HTB_NUMPRIO;p++) {
409                                 if (!cl->un.inner.feed[p].rb_node) continue;
410                                 printk(" p%d a=%x:",p,cl->un.inner.ptr[p]?rb_entry(cl->un.inner.ptr[p], struct htb_class,node[p])->classid:0);
411                                 HTB_DUMTREE(cl->un.inner.feed+p,node[p]);
412                         }
413                         printk("\n");
414                 }
415         }
416 }
417 #endif
418 /**
419  * htb_add_to_id_tree - adds class to the round robin list
420  *
421  * Routine adds class to the list (actually tree) sorted by classid.
422  * Make sure that class is not already on such list for given prio.
423  */
424 static void htb_add_to_id_tree (HTB_ARGQ struct rb_root *root,
425                 struct htb_class *cl,int prio)
426 {
427         struct rb_node **p = &root->rb_node, *parent = NULL;
428         HTB_DBG(7,3,"htb_add_id_tree cl=%X prio=%d\n",cl->classid,prio);
429 #ifdef HTB_DEBUG
430         if (cl->node[prio].rb_color != -1) { BUG_TRAP(0); return; }
431         HTB_CHCL(cl);
432         if (*p) {
433                 struct htb_class *x = rb_entry(*p,struct htb_class,node[prio]);
434                 HTB_CHCL(x);
435         }
436 #endif
437         while (*p) {
438                 struct htb_class *c; parent = *p;
439                 c = rb_entry(parent, struct htb_class, node[prio]);
440                 HTB_CHCL(c);
441                 if (cl->classid > c->classid)
442                         p = &parent->rb_right;
443                 else 
444                         p = &parent->rb_left;
445         }
446         rb_link_node(&cl->node[prio], parent, p);
447         rb_insert_color(&cl->node[prio], root);
448 }
449
450 /**
451  * htb_add_to_wait_tree - adds class to the event queue with delay
452  *
453  * The class is added to priority event queue to indicate that class will
454  * change its mode in cl->pq_key microseconds. Make sure that class is not
455  * already in the queue.
456  */
457 static void htb_add_to_wait_tree (struct htb_sched *q,
458                 struct htb_class *cl,long delay,int debug_hint)
459 {
460         struct rb_node **p = &q->wait_pq[cl->level].rb_node, *parent = NULL;
461         HTB_DBG(7,3,"htb_add_wt cl=%X key=%lu\n",cl->classid,cl->pq_key);
462 #ifdef HTB_DEBUG
463         if (cl->pq_node.rb_color != -1) { BUG_TRAP(0); return; }
464         HTB_CHCL(cl);
465         if ((delay <= 0 || delay > cl->mbuffer) && net_ratelimit())
466                 printk(KERN_ERR "HTB: suspicious delay in wait_tree d=%ld cl=%X h=%d\n",delay,cl->classid,debug_hint);
467 #endif
468         cl->pq_key = q->jiffies + PSCHED_US2JIFFIE(delay);
469         if (cl->pq_key == q->jiffies)
470                 cl->pq_key++;
471
472         /* update the nearest event cache */
473         if (time_after(q->near_ev_cache[cl->level], cl->pq_key))
474                 q->near_ev_cache[cl->level] = cl->pq_key;
475         
476         while (*p) {
477                 struct htb_class *c; parent = *p;
478                 c = rb_entry(parent, struct htb_class, pq_node);
479                 if (time_after_eq(cl->pq_key, c->pq_key))
480                         p = &parent->rb_right;
481                 else 
482                         p = &parent->rb_left;
483         }
484         rb_link_node(&cl->pq_node, parent, p);
485         rb_insert_color(&cl->pq_node, &q->wait_pq[cl->level]);
486 }
487
488 /**
489  * htb_next_rb_node - finds next node in binary tree
490  *
491  * When we are past last key we return NULL.
492  * Average complexity is 2 steps per call.
493  */
494 static void htb_next_rb_node(struct rb_node **n)
495 {
496         *n = rb_next(*n);
497 }
498
499 /**
500  * htb_add_class_to_row - add class to its row
501  *
502  * The class is added to row at priorities marked in mask.
503  * It does nothing if mask == 0.
504  */
505 static inline void htb_add_class_to_row(struct htb_sched *q, 
506                 struct htb_class *cl,int mask)
507 {
508         HTB_DBG(7,2,"htb_addrow cl=%X mask=%X rmask=%X\n",
509                         cl->classid,mask,q->row_mask[cl->level]);
510         HTB_CHCL(cl);
511         q->row_mask[cl->level] |= mask;
512         while (mask) {
513                 int prio = ffz(~mask);
514                 mask &= ~(1 << prio);
515                 htb_add_to_id_tree(HTB_PASSQ q->row[cl->level]+prio,cl,prio);
516         }
517 }
518
519 /**
520  * htb_remove_class_from_row - removes class from its row
521  *
522  * The class is removed from row at priorities marked in mask.
523  * It does nothing if mask == 0.
524  */
525 static __inline__ void htb_remove_class_from_row(struct htb_sched *q,
526                 struct htb_class *cl,int mask)
527 {
528         int m = 0;
529         HTB_CHCL(cl);
530         while (mask) {
531                 int prio = ffz(~mask);
532                 mask &= ~(1 << prio);
533                 if (q->ptr[cl->level][prio] == cl->node+prio)
534                         htb_next_rb_node(q->ptr[cl->level]+prio);
535                 htb_safe_rb_erase(cl->node + prio,q->row[cl->level]+prio);
536                 if (!q->row[cl->level][prio].rb_node) 
537                         m |= 1 << prio;
538         }
539         HTB_DBG(7,2,"htb_delrow cl=%X mask=%X rmask=%X maskdel=%X\n",
540                         cl->classid,mask,q->row_mask[cl->level],m);
541         q->row_mask[cl->level] &= ~m;
542 }
543
544 /**
545  * htb_activate_prios - creates active classe's feed chain
546  *
547  * The class is connected to ancestors and/or appropriate rows
548  * for priorities it is participating on. cl->cmode must be new 
549  * (activated) mode. It does nothing if cl->prio_activity == 0.
550  */
551 static void htb_activate_prios(struct htb_sched *q,struct htb_class *cl)
552 {
553         struct htb_class *p = cl->parent;
554         long m,mask = cl->prio_activity;
555         HTB_DBG(7,2,"htb_act_prios cl=%X mask=%lX cmode=%d\n",cl->classid,mask,cl->cmode);
556         HTB_CHCL(cl);
557
558         while (cl->cmode == HTB_MAY_BORROW && p && mask) {
559                 HTB_CHCL(p);
560                 m = mask; while (m) {
561                         int prio = ffz(~m);
562                         m &= ~(1 << prio);
563                         
564                         if (p->un.inner.feed[prio].rb_node)
565                                 /* parent already has its feed in use so that
566                                    reset bit in mask as parent is already ok */
567                                 mask &= ~(1 << prio);
568                         
569                         htb_add_to_id_tree(HTB_PASSQ p->un.inner.feed+prio,cl,prio);
570                 }
571                 HTB_DBG(7,3,"htb_act_pr_aft p=%X pact=%X mask=%lX pmode=%d\n",
572                                 p->classid,p->prio_activity,mask,p->cmode);
573                 p->prio_activity |= mask;
574                 cl = p; p = cl->parent;
575                 HTB_CHCL(cl);
576         }
577         if (cl->cmode == HTB_CAN_SEND && mask)
578                 htb_add_class_to_row(q,cl,mask);
579 }
580
581 /**
582  * htb_deactivate_prios - remove class from feed chain
583  *
584  * cl->cmode must represent old mode (before deactivation). It does 
585  * nothing if cl->prio_activity == 0. Class is removed from all feed
586  * chains and rows.
587  */
588 static void htb_deactivate_prios(struct htb_sched *q, struct htb_class *cl)
589 {
590         struct htb_class *p = cl->parent;
591         long m,mask = cl->prio_activity;
592         HTB_DBG(7,2,"htb_deact_prios cl=%X mask=%lX cmode=%d\n",cl->classid,mask,cl->cmode);
593         HTB_CHCL(cl);
594
595         while (cl->cmode == HTB_MAY_BORROW && p && mask) {
596                 m = mask; mask = 0; 
597                 while (m) {
598                         int prio = ffz(~m);
599                         m &= ~(1 << prio);
600                         
601                         if (p->un.inner.ptr[prio] == cl->node+prio) {
602                                 /* we are removing child which is pointed to from
603                                    parent feed - forget the pointer but remember
604                                    classid */
605                                 p->un.inner.last_ptr_id[prio] = cl->classid;
606                                 p->un.inner.ptr[prio] = NULL;
607                         }
608                         
609                         htb_safe_rb_erase(cl->node + prio,p->un.inner.feed + prio);
610                         
611                         if (!p->un.inner.feed[prio].rb_node) 
612                                 mask |= 1 << prio;
613                 }
614                 HTB_DBG(7,3,"htb_deact_pr_aft p=%X pact=%X mask=%lX pmode=%d\n",
615                                 p->classid,p->prio_activity,mask,p->cmode);
616                 p->prio_activity &= ~mask;
617                 cl = p; p = cl->parent;
618                 HTB_CHCL(cl);
619         }
620         if (cl->cmode == HTB_CAN_SEND && mask) 
621                 htb_remove_class_from_row(q,cl,mask);
622 }
623
624 /**
625  * htb_class_mode - computes and returns current class mode
626  *
627  * It computes cl's mode at time cl->t_c+diff and returns it. If mode
628  * is not HTB_CAN_SEND then cl->pq_key is updated to time difference
629  * from now to time when cl will change its state. 
630  * Also it is worth to note that class mode doesn't change simply
631  * at cl->{c,}tokens == 0 but there can rather be hysteresis of 
632  * 0 .. -cl->{c,}buffer range. It is meant to limit number of
633  * mode transitions per time unit. The speed gain is about 1/6.
634  */
635 static __inline__ enum htb_cmode 
636 htb_class_mode(struct htb_class *cl,long *diff)
637 {
638     long toks;
639
640     if ((toks = (cl->ctokens + *diff)) < (
641 #if HTB_HYSTERESIS
642             cl->cmode != HTB_CANT_SEND ? -cl->cbuffer :
643 #endif
644             0)) {
645             *diff = -toks;
646             return HTB_CANT_SEND;
647     }
648     if ((toks = (cl->tokens + *diff)) >= (
649 #if HTB_HYSTERESIS
650             cl->cmode == HTB_CAN_SEND ? -cl->buffer :
651 #endif
652             0))
653             return HTB_CAN_SEND;
654
655     *diff = -toks;
656     return HTB_MAY_BORROW;
657 }
658
659 /**
660  * htb_change_class_mode - changes classe's mode
661  *
662  * This should be the only way how to change classe's mode under normal
663  * cirsumstances. Routine will update feed lists linkage, change mode
664  * and add class to the wait event queue if appropriate. New mode should
665  * be different from old one and cl->pq_key has to be valid if changing
666  * to mode other than HTB_CAN_SEND (see htb_add_to_wait_tree).
667  */
668 static void 
669 htb_change_class_mode(struct htb_sched *q, struct htb_class *cl, long *diff)
670
671         enum htb_cmode new_mode = htb_class_mode(cl,diff);
672         
673         HTB_CHCL(cl);
674         HTB_DBG(7,1,"htb_chging_clmode %d->%d cl=%X\n",cl->cmode,new_mode,cl->classid);
675
676         if (new_mode == cl->cmode)
677                 return; 
678         
679         if (cl->prio_activity) { /* not necessary: speed optimization */
680                 if (cl->cmode != HTB_CANT_SEND) 
681                         htb_deactivate_prios(q,cl);
682                 cl->cmode = new_mode;
683                 if (new_mode != HTB_CANT_SEND) 
684                         htb_activate_prios(q,cl);
685         } else 
686                 cl->cmode = new_mode;
687 }
688
689 /**
690  * htb_activate - inserts leaf cl into appropriate active feeds 
691  *
692  * Routine learns (new) priority of leaf and activates feed chain
693  * for the prio. It can be called on already active leaf safely.
694  * It also adds leaf into droplist.
695  */
696 static __inline__ void htb_activate(struct htb_sched *q,struct htb_class *cl)
697 {
698         BUG_TRAP(!cl->level && cl->un.leaf.q && cl->un.leaf.q->q.qlen);
699         HTB_CHCL(cl);
700         if (!cl->prio_activity) {
701                 cl->prio_activity = 1 << (cl->un.leaf.aprio = cl->un.leaf.prio);
702                 htb_activate_prios(q,cl);
703                 list_add_tail(&cl->un.leaf.drop_list,q->drops+cl->un.leaf.aprio);
704         }
705 }
706
707 /**
708  * htb_deactivate - remove leaf cl from active feeds 
709  *
710  * Make sure that leaf is active. In the other words it can't be called
711  * with non-active leaf. It also removes class from the drop list.
712  */
713 static __inline__ void 
714 htb_deactivate(struct htb_sched *q,struct htb_class *cl)
715 {
716         BUG_TRAP(cl->prio_activity);
717         HTB_CHCL(cl);
718         htb_deactivate_prios(q,cl);
719         cl->prio_activity = 0;
720         list_del_init(&cl->un.leaf.drop_list);
721 }
722
723 static int htb_enqueue(struct sk_buff *skb, struct Qdisc *sch)
724 {
725     int ret = NET_XMIT_SUCCESS;
726     struct htb_sched *q = qdisc_priv(sch);
727     struct htb_class *cl = htb_classify(skb,sch,&ret);
728
729
730 #ifdef CONFIG_NET_CLS_ACT
731     if (cl == HTB_DIRECT ) {
732         if (q->direct_queue.qlen < q->direct_qlen ) {
733             __skb_queue_tail(&q->direct_queue, skb);
734             q->direct_pkts++;
735         }
736     } else if (!cl) {
737             if (NET_XMIT_DROP == ret) {
738                     sch->stats.drops++;
739             }
740             return ret;
741     }
742 #else
743     if (cl == HTB_DIRECT || !cl) {
744         /* enqueue to helper queue */
745         if (q->direct_queue.qlen < q->direct_qlen && cl) {
746             __skb_queue_tail(&q->direct_queue, skb);
747             q->direct_pkts++;
748         } else {
749             kfree_skb (skb);
750             sch->stats.drops++;
751             return NET_XMIT_DROP;
752         }
753     }
754 #endif
755     else if (cl->un.leaf.q->enqueue(skb, cl->un.leaf.q) != NET_XMIT_SUCCESS) {
756         sch->stats.drops++;
757         cl->stats.drops++;
758         return NET_XMIT_DROP;
759     } else {
760         cl->stats.packets++; cl->stats.bytes += skb->len;
761         htb_activate (q,cl);
762     }
763
764     sch->q.qlen++;
765     sch->stats.packets++; sch->stats.bytes += skb->len;
766     HTB_DBG(1,1,"htb_enq_ok cl=%X skb=%p\n",(cl && cl != HTB_DIRECT)?cl->classid:0,skb);
767     return NET_XMIT_SUCCESS;
768 }
769
770 /* TODO: requeuing packet charges it to policers again !! */
771 static int htb_requeue(struct sk_buff *skb, struct Qdisc *sch)
772 {
773     struct htb_sched *q = qdisc_priv(sch);
774     int ret =  NET_XMIT_SUCCESS;
775     struct htb_class *cl = htb_classify(skb,sch, &ret);
776     struct sk_buff *tskb;
777
778     if (cl == HTB_DIRECT || !cl) {
779         /* enqueue to helper queue */
780         if (q->direct_queue.qlen < q->direct_qlen && cl) {
781             __skb_queue_head(&q->direct_queue, skb);
782         } else {
783             __skb_queue_head(&q->direct_queue, skb);
784             tskb = __skb_dequeue_tail(&q->direct_queue);
785             kfree_skb (tskb);
786             sch->stats.drops++;
787             return NET_XMIT_CN; 
788         }
789     } else if (cl->un.leaf.q->ops->requeue(skb, cl->un.leaf.q) != NET_XMIT_SUCCESS) {
790         sch->stats.drops++;
791         cl->stats.drops++;
792         return NET_XMIT_DROP;
793     } else 
794             htb_activate (q,cl);
795
796     sch->q.qlen++;
797     HTB_DBG(1,1,"htb_req_ok cl=%X skb=%p\n",(cl && cl != HTB_DIRECT)?cl->classid:0,skb);
798     return NET_XMIT_SUCCESS;
799 }
800
801 static void htb_timer(unsigned long arg)
802 {
803     struct Qdisc *sch = (struct Qdisc*)arg;
804     sch->flags &= ~TCQ_F_THROTTLED;
805     wmb();
806     netif_schedule(sch->dev);
807 }
808
809 #ifdef HTB_RATECM
810 #define RT_GEN(D,R) R+=D-(R/HTB_EWMAC);D=0
811 static void htb_rate_timer(unsigned long arg)
812 {
813         struct Qdisc *sch = (struct Qdisc*)arg;
814         struct htb_sched *q = qdisc_priv(sch);
815         struct list_head *p;
816
817         /* lock queue so that we can muck with it */
818         HTB_QLOCK(sch);
819         HTB_DBG(10,1,"htb_rttmr j=%ld\n",jiffies);
820
821         q->rttim.expires = jiffies + HZ;
822         add_timer(&q->rttim);
823
824         /* scan and recompute one bucket at time */
825         if (++q->recmp_bucket >= HTB_HSIZE) 
826                 q->recmp_bucket = 0;
827         list_for_each (p,q->hash+q->recmp_bucket) {
828                 struct htb_class *cl = list_entry(p,struct htb_class,hlist);
829                 HTB_DBG(10,2,"htb_rttmr_cl cl=%X sbyte=%lu spkt=%lu\n",
830                                 cl->classid,cl->sum_bytes,cl->sum_packets);
831                 RT_GEN (cl->sum_bytes,cl->rate_bytes);
832                 RT_GEN (cl->sum_packets,cl->rate_packets);
833         }
834         HTB_QUNLOCK(sch);
835 }
836 #endif
837
838 /**
839  * htb_charge_class - charges amount "bytes" to leaf and ancestors
840  *
841  * Routine assumes that packet "bytes" long was dequeued from leaf cl
842  * borrowing from "level". It accounts bytes to ceil leaky bucket for
843  * leaf and all ancestors and to rate bucket for ancestors at levels
844  * "level" and higher. It also handles possible change of mode resulting
845  * from the update. Note that mode can also increase here (MAY_BORROW to
846  * CAN_SEND) because we can use more precise clock that event queue here.
847  * In such case we remove class from event queue first.
848  */
849 static void htb_charge_class(struct htb_sched *q,struct htb_class *cl,
850                 int level,int bytes)
851 {       
852         long toks,diff;
853         enum htb_cmode old_mode;
854         HTB_DBG(5,1,"htb_chrg_cl cl=%X lev=%d len=%d\n",cl->classid,level,bytes);
855
856 #define HTB_ACCNT(T,B,R) toks = diff + cl->T; \
857         if (toks > cl->B) toks = cl->B; \
858         toks -= L2T(cl, cl->R, bytes); \
859         if (toks <= -cl->mbuffer) toks = 1-cl->mbuffer; \
860         cl->T = toks
861
862         while (cl) {
863                 HTB_CHCL(cl);
864                 diff = PSCHED_TDIFF_SAFE(q->now, cl->t_c, (u32)cl->mbuffer);
865 #ifdef HTB_DEBUG
866                 if (diff > cl->mbuffer || diff < 0 || PSCHED_TLESS(q->now, cl->t_c)) {
867                         if (net_ratelimit())
868                                 printk(KERN_ERR "HTB: bad diff in charge, cl=%X diff=%lX now=%Lu then=%Lu j=%lu\n",
869                                        cl->classid, diff,
870 #ifdef CONFIG_NET_SCH_CLK_GETTIMEOFDAY
871                                        q->now.tv_sec * 1000000ULL + q->now.tv_usec,
872                                        cl->t_c.tv_sec * 1000000ULL + cl->t_c.tv_usec,
873 #else
874                                        (unsigned long long) q->now,
875                                        (unsigned long long) cl->t_c,
876 #endif
877                                        q->jiffies);
878                         diff = 1000;
879                 }
880 #endif
881                 if (cl->level >= level) {
882                         if (cl->level == level) cl->xstats.lends++;
883                         HTB_ACCNT (tokens,buffer,rate);
884                 } else {
885                         cl->xstats.borrows++;
886                         cl->tokens += diff; /* we moved t_c; update tokens */
887                 }
888                 HTB_ACCNT (ctokens,cbuffer,ceil);
889                 cl->t_c = q->now;
890                 HTB_DBG(5,2,"htb_chrg_clp cl=%X diff=%ld tok=%ld ctok=%ld\n",cl->classid,diff,cl->tokens,cl->ctokens);
891
892                 old_mode = cl->cmode; diff = 0;
893                 htb_change_class_mode(q,cl,&diff);
894                 if (old_mode != cl->cmode) {
895                         if (old_mode != HTB_CAN_SEND)
896                                 htb_safe_rb_erase(&cl->pq_node,q->wait_pq+cl->level);
897                         if (cl->cmode != HTB_CAN_SEND)
898                                 htb_add_to_wait_tree (q,cl,diff,1);
899                 }
900                 
901 #ifdef HTB_RATECM
902                 /* update rate counters */
903                 cl->sum_bytes += bytes; cl->sum_packets++;
904 #endif
905
906                 /* update byte stats except for leaves which are already updated */
907                 if (cl->level) {
908                         cl->stats.bytes += bytes;
909                         cl->stats.packets++;
910                 }
911                 cl = cl->parent;
912         }
913 }
914
915 /**
916  * htb_do_events - make mode changes to classes at the level
917  *
918  * Scans event queue for pending events and applies them. Returns jiffies to
919  * next pending event (0 for no event in pq).
920  * Note: Aplied are events whose have cl->pq_key <= jiffies.
921  */
922 static long htb_do_events(struct htb_sched *q,int level)
923 {
924         int i;
925         HTB_DBG(8,1,"htb_do_events l=%d root=%p rmask=%X\n",
926                         level,q->wait_pq[level].rb_node,q->row_mask[level]);
927         for (i = 0; i < 500; i++) {
928                 struct htb_class *cl;
929                 long diff;
930                 struct rb_node *p = q->wait_pq[level].rb_node;
931                 if (!p) return 0;
932                 while (p->rb_left) p = p->rb_left;
933
934                 cl = rb_entry(p, struct htb_class, pq_node);
935                 if (time_after(cl->pq_key, q->jiffies)) {
936                         HTB_DBG(8,3,"htb_do_ev_ret delay=%ld\n",cl->pq_key - q->jiffies);
937                         return cl->pq_key - q->jiffies;
938                 }
939                 htb_safe_rb_erase(p,q->wait_pq+level);
940                 diff = PSCHED_TDIFF_SAFE(q->now, cl->t_c, (u32)cl->mbuffer);
941 #ifdef HTB_DEBUG
942                 if (diff > cl->mbuffer || diff < 0 || PSCHED_TLESS(q->now, cl->t_c)) {
943                         if (net_ratelimit())
944                                 printk(KERN_ERR "HTB: bad diff in events, cl=%X diff=%lX now=%Lu then=%Lu j=%lu\n",
945                                        cl->classid, diff,
946 #ifdef CONFIG_NET_SCH_CLK_GETTIMEOFDAY
947                                        q->now.tv_sec * 1000000ULL + q->now.tv_usec,
948                                        cl->t_c.tv_sec * 1000000ULL + cl->t_c.tv_usec,
949 #else
950                                        (unsigned long long) q->now,
951                                        (unsigned long long) cl->t_c,
952 #endif
953                                        q->jiffies);
954                         diff = 1000;
955                 }
956 #endif
957                 htb_change_class_mode(q,cl,&diff);
958                 if (cl->cmode != HTB_CAN_SEND)
959                         htb_add_to_wait_tree (q,cl,diff,2);
960         }
961         if (net_ratelimit())
962                 printk(KERN_WARNING "htb: too many events !\n");
963         return HZ/10;
964 }
965
966 /* Returns class->node+prio from id-tree where classe's id is >= id. NULL
967    is no such one exists. */
968 static struct rb_node *
969 htb_id_find_next_upper(int prio,struct rb_node *n,u32 id)
970 {
971         struct rb_node *r = NULL;
972         while (n) {
973                 struct htb_class *cl = rb_entry(n,struct htb_class,node[prio]);
974                 if (id == cl->classid) return n;
975                 
976                 if (id > cl->classid) {
977                         n = n->rb_right;
978                 } else {
979                         r = n;
980                         n = n->rb_left;
981                 }
982         }
983         return r;
984 }
985
986 /**
987  * htb_lookup_leaf - returns next leaf class in DRR order
988  *
989  * Find leaf where current feed pointers points to.
990  */
991 static struct htb_class *
992 htb_lookup_leaf(HTB_ARGQ struct rb_root *tree,int prio,struct rb_node **pptr,u32 *pid)
993 {
994         int i;
995         struct {
996                 struct rb_node *root;
997                 struct rb_node **pptr;
998                 u32 *pid;
999         } stk[TC_HTB_MAXDEPTH],*sp = stk;
1000         
1001         BUG_TRAP(tree->rb_node);
1002         sp->root = tree->rb_node;
1003         sp->pptr = pptr;
1004         sp->pid = pid;
1005
1006         for (i = 0; i < 65535; i++) {
1007                 HTB_DBG(4,2,"htb_lleaf ptr=%p pid=%X\n",*sp->pptr,*sp->pid);
1008                 
1009                 if (!*sp->pptr && *sp->pid) { 
1010                         /* ptr was invalidated but id is valid - try to recover 
1011                            the original or next ptr */
1012                         *sp->pptr = htb_id_find_next_upper(prio,sp->root,*sp->pid);
1013                 }
1014                 *sp->pid = 0; /* ptr is valid now so that remove this hint as it
1015                                  can become out of date quickly */
1016                 if (!*sp->pptr) { /* we are at right end; rewind & go up */
1017                         *sp->pptr = sp->root;
1018                         while ((*sp->pptr)->rb_left) 
1019                                 *sp->pptr = (*sp->pptr)->rb_left;
1020                         if (sp > stk) {
1021                                 sp--;
1022                                 BUG_TRAP(*sp->pptr); if(!*sp->pptr) return NULL;
1023                                 htb_next_rb_node (sp->pptr);
1024                         }
1025                 } else {
1026                         struct htb_class *cl;
1027                         cl = rb_entry(*sp->pptr,struct htb_class,node[prio]);
1028                         HTB_CHCL(cl);
1029                         if (!cl->level) 
1030                                 return cl;
1031                         (++sp)->root = cl->un.inner.feed[prio].rb_node;
1032                         sp->pptr = cl->un.inner.ptr+prio;
1033                         sp->pid = cl->un.inner.last_ptr_id+prio;
1034                 }
1035         }
1036         BUG_TRAP(0);
1037         return NULL;
1038 }
1039
1040 /* dequeues packet at given priority and level; call only if
1041    you are sure that there is active class at prio/level */
1042 static struct sk_buff *
1043 htb_dequeue_tree(struct htb_sched *q,int prio,int level)
1044 {
1045         struct sk_buff *skb = NULL;
1046         struct htb_class *cl,*start;
1047         /* look initial class up in the row */
1048         start = cl = htb_lookup_leaf (HTB_PASSQ q->row[level]+prio,prio,
1049                         q->ptr[level]+prio,q->last_ptr_id[level]+prio);
1050         
1051         do {
1052 next:
1053                 BUG_TRAP(cl); 
1054                 if (!cl) return NULL;
1055                 HTB_DBG(4,1,"htb_deq_tr prio=%d lev=%d cl=%X defic=%d\n",
1056                                 prio,level,cl->classid,cl->un.leaf.deficit[level]);
1057
1058                 /* class can be empty - it is unlikely but can be true if leaf
1059                    qdisc drops packets in enqueue routine or if someone used
1060                    graft operation on the leaf since last dequeue; 
1061                    simply deactivate and skip such class */
1062                 if (unlikely(cl->un.leaf.q->q.qlen == 0)) {
1063                         struct htb_class *next;
1064                         htb_deactivate(q,cl);
1065
1066                         /* row/level might become empty */
1067                         if ((q->row_mask[level] & (1 << prio)) == 0)
1068                                 return NULL; 
1069                         
1070                         next = htb_lookup_leaf (HTB_PASSQ q->row[level]+prio,
1071                                         prio,q->ptr[level]+prio,q->last_ptr_id[level]+prio);
1072
1073                         if (cl == start) /* fix start if we just deleted it */
1074                                 start = next;
1075                         cl = next;
1076                         goto next;
1077                 }
1078         
1079                 if (likely((skb = cl->un.leaf.q->dequeue(cl->un.leaf.q)) != NULL)) 
1080                         break;
1081                 if (!cl->warned) {
1082                         printk(KERN_WARNING "htb: class %X isn't work conserving ?!\n",cl->classid);
1083                         cl->warned = 1;
1084                 }
1085                 q->nwc_hit++;
1086                 htb_next_rb_node((level?cl->parent->un.inner.ptr:q->ptr[0])+prio);
1087                 cl = htb_lookup_leaf (HTB_PASSQ q->row[level]+prio,prio,q->ptr[level]+prio,
1088                                 q->last_ptr_id[level]+prio);
1089
1090         } while (cl != start);
1091
1092         if (likely(skb != NULL)) {
1093                 if ((cl->un.leaf.deficit[level] -= skb->len) < 0) {
1094                         HTB_DBG(4,2,"htb_next_cl oldptr=%p quant_add=%d\n",
1095                                 level?cl->parent->un.inner.ptr[prio]:q->ptr[0][prio],cl->un.leaf.quantum);
1096                         cl->un.leaf.deficit[level] += cl->un.leaf.quantum;
1097                         htb_next_rb_node((level?cl->parent->un.inner.ptr:q->ptr[0])+prio);
1098                 }
1099                 /* this used to be after charge_class but this constelation
1100                    gives us slightly better performance */
1101                 if (!cl->un.leaf.q->q.qlen)
1102                         htb_deactivate (q,cl);
1103                 htb_charge_class (q,cl,level,skb->len);
1104         }
1105         return skb;
1106 }
1107
1108 static void htb_delay_by(struct Qdisc *sch,long delay)
1109 {
1110         struct htb_sched *q = qdisc_priv(sch);
1111         if (delay <= 0) delay = 1;
1112         if (unlikely(delay > 5*HZ)) {
1113                 if (net_ratelimit())
1114                         printk(KERN_INFO "HTB delay %ld > 5sec\n", delay);
1115                 delay = 5*HZ;
1116         }
1117         /* why don't use jiffies here ? because expires can be in past */
1118         mod_timer(&q->timer, q->jiffies + delay);
1119         sch->flags |= TCQ_F_THROTTLED;
1120         sch->stats.overlimits++;
1121         HTB_DBG(3,1,"htb_deq t_delay=%ld\n",delay);
1122 }
1123
1124 static struct sk_buff *htb_dequeue(struct Qdisc *sch)
1125 {
1126         struct sk_buff *skb = NULL;
1127         struct htb_sched *q = qdisc_priv(sch);
1128         int level;
1129         long min_delay;
1130 #ifdef HTB_DEBUG
1131         int evs_used = 0;
1132 #endif
1133
1134         q->jiffies = jiffies;
1135         HTB_DBG(3,1,"htb_deq dircnt=%d qlen=%d\n",skb_queue_len(&q->direct_queue),
1136                         sch->q.qlen);
1137
1138         /* try to dequeue direct packets as high prio (!) to minimize cpu work */
1139         if ((skb = __skb_dequeue(&q->direct_queue)) != NULL) {
1140                 sch->flags &= ~TCQ_F_THROTTLED;
1141                 sch->q.qlen--;
1142                 return skb;
1143         }
1144
1145         if (!sch->q.qlen) goto fin;
1146         PSCHED_GET_TIME(q->now);
1147
1148         min_delay = LONG_MAX;
1149         q->nwc_hit = 0;
1150         for (level = 0; level < TC_HTB_MAXDEPTH; level++) {
1151                 /* common case optimization - skip event handler quickly */
1152                 int m;
1153                 long delay;
1154                 if (time_after_eq(q->jiffies, q->near_ev_cache[level])) {
1155                         delay = htb_do_events(q,level);
1156                         q->near_ev_cache[level] = q->jiffies + (delay ? delay : HZ);
1157 #ifdef HTB_DEBUG
1158                         evs_used++;
1159 #endif
1160                 } else
1161                         delay = q->near_ev_cache[level] - q->jiffies;   
1162                 
1163                 if (delay && min_delay > delay) 
1164                         min_delay = delay;
1165                 m = ~q->row_mask[level];
1166                 while (m != (int)(-1)) {
1167                         int prio = ffz (m);
1168                         m |= 1 << prio;
1169                         skb = htb_dequeue_tree(q,prio,level);
1170                         if (likely(skb != NULL)) {
1171                                 sch->q.qlen--;
1172                                 sch->flags &= ~TCQ_F_THROTTLED;
1173                                 goto fin;
1174                         }
1175                 }
1176         }
1177 #ifdef HTB_DEBUG
1178         if (!q->nwc_hit && min_delay >= 10*HZ && net_ratelimit()) {
1179                 if (min_delay == LONG_MAX) {
1180                         printk(KERN_ERR "HTB: dequeue bug (%d,%lu,%lu), report it please !\n",
1181                                         evs_used,q->jiffies,jiffies);
1182                         htb_debug_dump(q);
1183                 } else 
1184                         printk(KERN_WARNING "HTB: mindelay=%ld, some class has "
1185                                         "too small rate\n",min_delay);
1186         }
1187 #endif
1188         htb_delay_by (sch,min_delay > 5*HZ ? 5*HZ : min_delay);
1189 fin:
1190         HTB_DBG(3,1,"htb_deq_end %s j=%lu skb=%p\n",sch->dev->name,q->jiffies,skb);
1191         return skb;
1192 }
1193
1194 /* try to drop from each class (by prio) until one succeed */
1195 static unsigned int htb_drop(struct Qdisc* sch)
1196 {
1197         struct htb_sched *q = qdisc_priv(sch);
1198         int prio;
1199
1200         for (prio = TC_HTB_NUMPRIO - 1; prio >= 0; prio--) {
1201                 struct list_head *p;
1202                 list_for_each (p,q->drops+prio) {
1203                         struct htb_class *cl = list_entry(p, struct htb_class,
1204                                                           un.leaf.drop_list);
1205                         unsigned int len;
1206                         if (cl->un.leaf.q->ops->drop && 
1207                                 (len = cl->un.leaf.q->ops->drop(cl->un.leaf.q))) {
1208                                 sch->q.qlen--;
1209                                 if (!cl->un.leaf.q->q.qlen)
1210                                         htb_deactivate (q,cl);
1211                                 return len;
1212                         }
1213                 }
1214         }
1215         return 0;
1216 }
1217
1218 /* reset all classes */
1219 /* always caled under BH & queue lock */
1220 static void htb_reset(struct Qdisc* sch)
1221 {
1222         struct htb_sched *q = qdisc_priv(sch);
1223         int i;
1224         HTB_DBG(0,1,"htb_reset sch=%p, handle=%X\n",sch,sch->handle);
1225
1226         for (i = 0; i < HTB_HSIZE; i++) {
1227                 struct list_head *p;
1228                 list_for_each (p,q->hash+i) {
1229                         struct htb_class *cl = list_entry(p,struct htb_class,hlist);
1230                         if (cl->level)
1231                                 memset(&cl->un.inner,0,sizeof(cl->un.inner));
1232                         else {
1233                                 if (cl->un.leaf.q) 
1234                                         qdisc_reset(cl->un.leaf.q);
1235                                 INIT_LIST_HEAD(&cl->un.leaf.drop_list);
1236                         }
1237                         cl->prio_activity = 0;
1238                         cl->cmode = HTB_CAN_SEND;
1239 #ifdef HTB_DEBUG
1240                         cl->pq_node.rb_color = -1;
1241                         memset(cl->node,255,sizeof(cl->node));
1242 #endif
1243
1244                 }
1245         }
1246         sch->flags &= ~TCQ_F_THROTTLED;
1247         del_timer(&q->timer);
1248         __skb_queue_purge(&q->direct_queue);
1249         sch->q.qlen = 0;
1250         memset(q->row,0,sizeof(q->row));
1251         memset(q->row_mask,0,sizeof(q->row_mask));
1252         memset(q->wait_pq,0,sizeof(q->wait_pq));
1253         memset(q->ptr,0,sizeof(q->ptr));
1254         for (i = 0; i < TC_HTB_NUMPRIO; i++)
1255                 INIT_LIST_HEAD(q->drops+i);
1256 }
1257
1258 static int htb_init(struct Qdisc *sch, struct rtattr *opt)
1259 {
1260         struct htb_sched *q = qdisc_priv(sch);
1261         struct rtattr *tb[TCA_HTB_INIT];
1262         struct tc_htb_glob *gopt;
1263         int i;
1264 #ifdef HTB_DEBUG
1265         printk(KERN_INFO "HTB init, kernel part version %d.%d\n",
1266                           HTB_VER >> 16,HTB_VER & 0xffff);
1267 #endif
1268         if (!opt || rtattr_parse(tb, TCA_HTB_INIT, RTA_DATA(opt), RTA_PAYLOAD(opt)) ||
1269                         tb[TCA_HTB_INIT-1] == NULL ||
1270                         RTA_PAYLOAD(tb[TCA_HTB_INIT-1]) < sizeof(*gopt)) {
1271                 printk(KERN_ERR "HTB: hey probably you have bad tc tool ?\n");
1272                 return -EINVAL;
1273         }
1274         gopt = RTA_DATA(tb[TCA_HTB_INIT-1]);
1275         if (gopt->version != HTB_VER >> 16) {
1276                 printk(KERN_ERR "HTB: need tc/htb version %d (minor is %d), you have %d\n",
1277                                 HTB_VER >> 16,HTB_VER & 0xffff,gopt->version);
1278                 return -EINVAL;
1279         }
1280         memset(q,0,sizeof(*q));
1281         q->debug = gopt->debug;
1282         HTB_DBG(0,1,"htb_init sch=%p handle=%X r2q=%d\n",sch,sch->handle,gopt->rate2quantum);
1283
1284         INIT_LIST_HEAD(&q->root);
1285         for (i = 0; i < HTB_HSIZE; i++)
1286                 INIT_LIST_HEAD(q->hash+i);
1287         for (i = 0; i < TC_HTB_NUMPRIO; i++)
1288                 INIT_LIST_HEAD(q->drops+i);
1289
1290         init_timer(&q->timer);
1291         skb_queue_head_init(&q->direct_queue);
1292
1293         q->direct_qlen = sch->dev->tx_queue_len;
1294         if (q->direct_qlen < 2) /* some devices have zero tx_queue_len */
1295                 q->direct_qlen = 2;
1296         q->timer.function = htb_timer;
1297         q->timer.data = (unsigned long)sch;
1298
1299 #ifdef HTB_RATECM
1300         init_timer(&q->rttim);
1301         q->rttim.function = htb_rate_timer;
1302         q->rttim.data = (unsigned long)sch;
1303         q->rttim.expires = jiffies + HZ;
1304         add_timer(&q->rttim);
1305 #endif
1306         if ((q->rate2quantum = gopt->rate2quantum) < 1)
1307                 q->rate2quantum = 1;
1308         q->defcls = gopt->defcls;
1309
1310         return 0;
1311 }
1312
1313 static int htb_dump(struct Qdisc *sch, struct sk_buff *skb)
1314 {
1315         struct htb_sched *q = qdisc_priv(sch);
1316         unsigned char    *b = skb->tail;
1317         struct rtattr *rta;
1318         struct tc_htb_glob gopt;
1319         HTB_DBG(0,1,"htb_dump sch=%p, handle=%X\n",sch,sch->handle);
1320         /* stats */
1321         HTB_QLOCK(sch);
1322         gopt.direct_pkts = q->direct_pkts;
1323
1324 #ifdef HTB_DEBUG
1325         if (HTB_DBG_COND(0,2))
1326                 htb_debug_dump(q);
1327 #endif
1328         gopt.version = HTB_VER;
1329         gopt.rate2quantum = q->rate2quantum;
1330         gopt.defcls = q->defcls;
1331         gopt.debug = q->debug;
1332         rta = (struct rtattr*)b;
1333         RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
1334         RTA_PUT(skb, TCA_HTB_INIT, sizeof(gopt), &gopt);
1335         rta->rta_len = skb->tail - b;
1336         sch->stats.qlen = sch->q.qlen;
1337         RTA_PUT(skb, TCA_STATS, sizeof(sch->stats), &sch->stats);
1338         HTB_QUNLOCK(sch);
1339         return skb->len;
1340 rtattr_failure:
1341         HTB_QUNLOCK(sch);
1342         skb_trim(skb, skb->tail - skb->data);
1343         return -1;
1344 }
1345
1346 static int htb_dump_class(struct Qdisc *sch, unsigned long arg,
1347         struct sk_buff *skb, struct tcmsg *tcm)
1348 {
1349 #ifdef HTB_DEBUG
1350         struct htb_sched *q = qdisc_priv(sch);
1351 #endif
1352         struct htb_class *cl = (struct htb_class*)arg;
1353         unsigned char    *b = skb->tail;
1354         struct rtattr *rta;
1355         struct tc_htb_opt opt;
1356
1357         HTB_DBG(0,1,"htb_dump_class handle=%X clid=%X\n",sch->handle,cl->classid);
1358
1359         HTB_QLOCK(sch);
1360         tcm->tcm_parent = cl->parent ? cl->parent->classid : TC_H_ROOT;
1361         tcm->tcm_handle = cl->classid;
1362         if (!cl->level && cl->un.leaf.q) {
1363                 tcm->tcm_info = cl->un.leaf.q->handle;
1364                 cl->stats.qlen = cl->un.leaf.q->q.qlen;
1365         }
1366
1367         rta = (struct rtattr*)b;
1368         RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
1369
1370         memset (&opt,0,sizeof(opt));
1371
1372         opt.rate = cl->rate->rate; opt.buffer = cl->buffer;
1373         opt.ceil = cl->ceil->rate; opt.cbuffer = cl->cbuffer;
1374         opt.quantum = cl->un.leaf.quantum; opt.prio = cl->un.leaf.prio;
1375         opt.level = cl->level; 
1376         RTA_PUT(skb, TCA_HTB_PARMS, sizeof(opt), &opt);
1377         rta->rta_len = skb->tail - b;
1378
1379 #ifdef HTB_RATECM
1380         cl->stats.bps = cl->rate_bytes/(HTB_EWMAC*HTB_HSIZE);
1381         cl->stats.pps = cl->rate_packets/(HTB_EWMAC*HTB_HSIZE);
1382 #endif
1383
1384         cl->xstats.tokens = cl->tokens;
1385         cl->xstats.ctokens = cl->ctokens;
1386         RTA_PUT(skb, TCA_STATS, sizeof(cl->stats), &cl->stats);
1387         RTA_PUT(skb, TCA_XSTATS, sizeof(cl->xstats), &cl->xstats);
1388         HTB_QUNLOCK(sch);
1389         return skb->len;
1390 rtattr_failure:
1391         HTB_QUNLOCK(sch);
1392         skb_trim(skb, b - skb->data);
1393         return -1;
1394 }
1395
1396 static int htb_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
1397         struct Qdisc **old)
1398 {
1399         struct htb_class *cl = (struct htb_class*)arg;
1400
1401         if (cl && !cl->level) {
1402                 if (new == NULL && (new = qdisc_create_dflt(sch->dev, 
1403                                         &pfifo_qdisc_ops)) == NULL)
1404                                         return -ENOBUFS;
1405                 sch_tree_lock(sch);
1406                 if ((*old = xchg(&cl->un.leaf.q, new)) != NULL) {
1407                         if (cl->prio_activity)
1408                                 htb_deactivate (qdisc_priv(sch),cl);
1409
1410                         /* TODO: is it correct ? Why CBQ doesn't do it ? */
1411                         sch->q.qlen -= (*old)->q.qlen;  
1412                         qdisc_reset(*old);
1413                 }
1414                 sch_tree_unlock(sch);
1415                 return 0;
1416         }
1417         return -ENOENT;
1418 }
1419
1420 static struct Qdisc * htb_leaf(struct Qdisc *sch, unsigned long arg)
1421 {
1422         struct htb_class *cl = (struct htb_class*)arg;
1423         return (cl && !cl->level) ? cl->un.leaf.q : NULL;
1424 }
1425
1426 static unsigned long htb_get(struct Qdisc *sch, u32 classid)
1427 {
1428 #ifdef HTB_DEBUG
1429         struct htb_sched *q = qdisc_priv(sch);
1430 #endif
1431         struct htb_class *cl = htb_find(classid,sch);
1432         HTB_DBG(0,1,"htb_get clid=%X q=%p cl=%p ref=%d\n",classid,q,cl,cl?cl->refcnt:0);
1433         if (cl) 
1434                 cl->refcnt++;
1435         return (unsigned long)cl;
1436 }
1437
1438 static void htb_destroy_filters(struct tcf_proto **fl)
1439 {
1440         struct tcf_proto *tp;
1441
1442         while ((tp = *fl) != NULL) {
1443                 *fl = tp->next;
1444                 tcf_destroy(tp);
1445         }
1446 }
1447
1448 static void htb_destroy_class(struct Qdisc* sch,struct htb_class *cl)
1449 {
1450         struct htb_sched *q = qdisc_priv(sch);
1451         HTB_DBG(0,1,"htb_destrycls clid=%X ref=%d\n", cl?cl->classid:0,cl?cl->refcnt:0);
1452         if (!cl->level) {
1453                 BUG_TRAP(cl->un.leaf.q);
1454                 sch->q.qlen -= cl->un.leaf.q->q.qlen;
1455                 qdisc_destroy(cl->un.leaf.q);
1456         }
1457         qdisc_put_rtab(cl->rate);
1458         qdisc_put_rtab(cl->ceil);
1459         
1460 #ifdef CONFIG_NET_ESTIMATOR
1461         qdisc_kill_estimator(&cl->stats);
1462 #endif
1463         htb_destroy_filters (&cl->filter_list);
1464         
1465         while (!list_empty(&cl->children)) 
1466                 htb_destroy_class (sch,list_entry(cl->children.next,
1467                                         struct htb_class,sibling));
1468
1469         /* note: this delete may happen twice (see htb_delete) */
1470         list_del(&cl->hlist);
1471         list_del(&cl->sibling);
1472         
1473         if (cl->prio_activity)
1474                 htb_deactivate (q,cl);
1475         
1476         if (cl->cmode != HTB_CAN_SEND)
1477                 htb_safe_rb_erase(&cl->pq_node,q->wait_pq+cl->level);
1478         
1479         kfree(cl);
1480 }
1481
1482 /* always caled under BH & queue lock */
1483 static void htb_destroy(struct Qdisc* sch)
1484 {
1485         struct htb_sched *q = qdisc_priv(sch);
1486         HTB_DBG(0,1,"htb_destroy q=%p\n",q);
1487
1488         del_timer_sync (&q->timer);
1489 #ifdef HTB_RATECM
1490         del_timer_sync (&q->rttim);
1491 #endif
1492         /* This line used to be after htb_destroy_class call below
1493            and surprisingly it worked in 2.4. But it must precede it 
1494            because filter need its target class alive to be able to call
1495            unbind_filter on it (without Oops). */
1496         htb_destroy_filters(&q->filter_list);
1497         
1498         while (!list_empty(&q->root)) 
1499                 htb_destroy_class (sch,list_entry(q->root.next,
1500                                         struct htb_class,sibling));
1501
1502         __skb_queue_purge(&q->direct_queue);
1503 }
1504
1505 static int htb_delete(struct Qdisc *sch, unsigned long arg)
1506 {
1507         struct htb_sched *q = qdisc_priv(sch);
1508         struct htb_class *cl = (struct htb_class*)arg;
1509         HTB_DBG(0,1,"htb_delete q=%p cl=%X ref=%d\n",q,cl?cl->classid:0,cl?cl->refcnt:0);
1510
1511         // TODO: why don't allow to delete subtree ? references ? does
1512         // tc subsys quarantee us that in htb_destroy it holds no class
1513         // refs so that we can remove children safely there ?
1514         if (!list_empty(&cl->children) || cl->filter_cnt)
1515                 return -EBUSY;
1516         
1517         sch_tree_lock(sch);
1518         
1519         /* delete from hash and active; remainder in destroy_class */
1520         list_del_init(&cl->hlist);
1521         if (cl->prio_activity)
1522                 htb_deactivate (q,cl);
1523
1524         if (--cl->refcnt == 0)
1525                 htb_destroy_class(sch,cl);
1526
1527         sch_tree_unlock(sch);
1528         return 0;
1529 }
1530
1531 static void htb_put(struct Qdisc *sch, unsigned long arg)
1532 {
1533 #ifdef HTB_DEBUG
1534         struct htb_sched *q = qdisc_priv(sch);
1535 #endif
1536         struct htb_class *cl = (struct htb_class*)arg;
1537         HTB_DBG(0,1,"htb_put q=%p cl=%X ref=%d\n",q,cl?cl->classid:0,cl?cl->refcnt:0);
1538
1539         if (--cl->refcnt == 0)
1540                 htb_destroy_class(sch,cl);
1541 }
1542
1543 static int htb_change_class(struct Qdisc *sch, u32 classid, 
1544                 u32 parentid, struct rtattr **tca, unsigned long *arg)
1545 {
1546         int err = -EINVAL;
1547         struct htb_sched *q = qdisc_priv(sch);
1548         struct htb_class *cl = (struct htb_class*)*arg,*parent;
1549         struct rtattr *opt = tca[TCA_OPTIONS-1];
1550         struct qdisc_rate_table *rtab = NULL, *ctab = NULL;
1551         struct rtattr *tb[TCA_HTB_RTAB];
1552         struct tc_htb_opt *hopt;
1553
1554         /* extract all subattrs from opt attr */
1555         if (!opt || rtattr_parse(tb, TCA_HTB_RTAB, RTA_DATA(opt), RTA_PAYLOAD(opt)) ||
1556                         tb[TCA_HTB_PARMS-1] == NULL ||
1557                         RTA_PAYLOAD(tb[TCA_HTB_PARMS-1]) < sizeof(*hopt))
1558                 goto failure;
1559         
1560         parent = parentid == TC_H_ROOT ? NULL : htb_find (parentid,sch);
1561
1562         hopt = RTA_DATA(tb[TCA_HTB_PARMS-1]);
1563         HTB_DBG(0,1,"htb_chg cl=%p(%X), clid=%X, parid=%X, opt/prio=%d, rate=%u, buff=%d, quant=%d\n", cl,cl?cl->classid:0,classid,parentid,(int)hopt->prio,hopt->rate.rate,hopt->buffer,hopt->quantum);
1564         rtab = qdisc_get_rtab(&hopt->rate, tb[TCA_HTB_RTAB-1]);
1565         ctab = qdisc_get_rtab(&hopt->ceil, tb[TCA_HTB_CTAB-1]);
1566         if (!rtab || !ctab) goto failure;
1567
1568         if (!cl) { /* new class */
1569                 struct Qdisc *new_q;
1570                 /* check for valid classid */
1571                 if (!classid || TC_H_MAJ(classid^sch->handle) || htb_find(classid,sch))
1572                         goto failure;
1573
1574                 /* check maximal depth */
1575                 if (parent && parent->parent && parent->parent->level < 2) {
1576                         printk(KERN_ERR "htb: tree is too deep\n");
1577                         goto failure;
1578                 }
1579                 err = -ENOBUFS;
1580                 if ((cl = kmalloc(sizeof(*cl), GFP_KERNEL)) == NULL)
1581                         goto failure;
1582                 
1583                 memset(cl, 0, sizeof(*cl));
1584                 cl->refcnt = 1;
1585                 INIT_LIST_HEAD(&cl->sibling);
1586                 INIT_LIST_HEAD(&cl->hlist);
1587                 INIT_LIST_HEAD(&cl->children);
1588                 INIT_LIST_HEAD(&cl->un.leaf.drop_list);
1589 #ifdef HTB_DEBUG
1590                 cl->magic = HTB_CMAGIC;
1591 #endif
1592
1593                 /* create leaf qdisc early because it uses kmalloc(GFP_KERNEL)
1594                    so that can't be used inside of sch_tree_lock
1595                    -- thanks to Karlis Peisenieks */
1596                 new_q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
1597                 sch_tree_lock(sch);
1598                 if (parent && !parent->level) {
1599                         /* turn parent into inner node */
1600                         sch->q.qlen -= parent->un.leaf.q->q.qlen;
1601                         qdisc_destroy (parent->un.leaf.q);
1602                         if (parent->prio_activity) 
1603                                 htb_deactivate (q,parent);
1604
1605                         /* remove from evt list because of level change */
1606                         if (parent->cmode != HTB_CAN_SEND) {
1607                                 htb_safe_rb_erase(&parent->pq_node,q->wait_pq /*+0*/);
1608                                 parent->cmode = HTB_CAN_SEND;
1609                         }
1610                         parent->level = (parent->parent ? parent->parent->level
1611                                         : TC_HTB_MAXDEPTH) - 1;
1612                         memset (&parent->un.inner,0,sizeof(parent->un.inner));
1613                 }
1614                 /* leaf (we) needs elementary qdisc */
1615                 cl->un.leaf.q = new_q ? new_q : &noop_qdisc;
1616
1617                 cl->classid = classid; cl->parent = parent;
1618
1619                 /* set class to be in HTB_CAN_SEND state */
1620                 cl->tokens = hopt->buffer;
1621                 cl->ctokens = hopt->cbuffer;
1622                 cl->mbuffer = 60000000; /* 1min */
1623                 PSCHED_GET_TIME(cl->t_c);
1624                 cl->cmode = HTB_CAN_SEND;
1625
1626                 /* attach to the hash list and parent's family */
1627                 list_add_tail(&cl->hlist, q->hash+htb_hash(classid));
1628                 list_add_tail(&cl->sibling, parent ? &parent->children : &q->root);
1629 #ifdef HTB_DEBUG
1630                 { 
1631                         int i;
1632                         for (i = 0; i < TC_HTB_NUMPRIO; i++) cl->node[i].rb_color = -1;
1633                         cl->pq_node.rb_color = -1;
1634                 }
1635 #endif
1636         } else sch_tree_lock(sch);
1637
1638         /* it used to be a nasty bug here, we have to check that node
1639            is really leaf before changing cl->un.leaf ! */
1640         if (!cl->level) {
1641                 cl->un.leaf.quantum = rtab->rate.rate / q->rate2quantum;
1642                 if (!hopt->quantum && cl->un.leaf.quantum < 1000) {
1643                         printk(KERN_WARNING "HTB: quantum of class %X is small. Consider r2q change.\n", cl->classid);
1644                         cl->un.leaf.quantum = 1000;
1645                 }
1646                 if (!hopt->quantum && cl->un.leaf.quantum > 200000) {
1647                         printk(KERN_WARNING "HTB: quantum of class %X is big. Consider r2q change.\n", cl->classid);
1648                         cl->un.leaf.quantum = 200000;
1649                 }
1650                 if (hopt->quantum)
1651                         cl->un.leaf.quantum = hopt->quantum;
1652                 if ((cl->un.leaf.prio = hopt->prio) >= TC_HTB_NUMPRIO)
1653                         cl->un.leaf.prio = TC_HTB_NUMPRIO - 1;
1654         }
1655
1656         cl->buffer = hopt->buffer;
1657         cl->cbuffer = hopt->cbuffer;
1658         if (cl->rate) qdisc_put_rtab(cl->rate); cl->rate = rtab;
1659         if (cl->ceil) qdisc_put_rtab(cl->ceil); cl->ceil = ctab;
1660         sch_tree_unlock(sch);
1661
1662         *arg = (unsigned long)cl;
1663         return 0;
1664
1665 failure:
1666         if (rtab) qdisc_put_rtab(rtab);
1667         if (ctab) qdisc_put_rtab(ctab);
1668         return err;
1669 }
1670
1671 static struct tcf_proto **htb_find_tcf(struct Qdisc *sch, unsigned long arg)
1672 {
1673         struct htb_sched *q = qdisc_priv(sch);
1674         struct htb_class *cl = (struct htb_class *)arg;
1675         struct tcf_proto **fl = cl ? &cl->filter_list : &q->filter_list;
1676         HTB_DBG(0,2,"htb_tcf q=%p clid=%X fref=%d fl=%p\n",q,cl?cl->classid:0,cl?cl->filter_cnt:q->filter_cnt,*fl);
1677         return fl;
1678 }
1679
1680 static unsigned long htb_bind_filter(struct Qdisc *sch, unsigned long parent,
1681         u32 classid)
1682 {
1683         struct htb_sched *q = qdisc_priv(sch);
1684         struct htb_class *cl = htb_find (classid,sch);
1685         HTB_DBG(0,2,"htb_bind q=%p clid=%X cl=%p fref=%d\n",q,classid,cl,cl?cl->filter_cnt:q->filter_cnt);
1686         /*if (cl && !cl->level) return 0;
1687           The line above used to be there to prevent attaching filters to 
1688           leaves. But at least tc_index filter uses this just to get class 
1689           for other reasons so that we have to allow for it.
1690           ----
1691           19.6.2002 As Werner explained it is ok - bind filter is just
1692           another way to "lock" the class - unlike "get" this lock can
1693           be broken by class during destroy IIUC.
1694          */
1695         if (cl) 
1696                 cl->filter_cnt++; 
1697         else 
1698                 q->filter_cnt++;
1699         return (unsigned long)cl;
1700 }
1701
1702 static void htb_unbind_filter(struct Qdisc *sch, unsigned long arg)
1703 {
1704         struct htb_sched *q = qdisc_priv(sch);
1705         struct htb_class *cl = (struct htb_class *)arg;
1706         HTB_DBG(0,2,"htb_unbind q=%p cl=%p fref=%d\n",q,cl,cl?cl->filter_cnt:q->filter_cnt);
1707         if (cl) 
1708                 cl->filter_cnt--; 
1709         else 
1710                 q->filter_cnt--;
1711 }
1712
1713 static void htb_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1714 {
1715         struct htb_sched *q = qdisc_priv(sch);
1716         int i;
1717
1718         if (arg->stop)
1719                 return;
1720
1721         for (i = 0; i < HTB_HSIZE; i++) {
1722                 struct list_head *p;
1723                 list_for_each (p,q->hash+i) {
1724                         struct htb_class *cl = list_entry(p,struct htb_class,hlist);
1725                         if (arg->count < arg->skip) {
1726                                 arg->count++;
1727                                 continue;
1728                         }
1729                         if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
1730                                 arg->stop = 1;
1731                                 return;
1732                         }
1733                         arg->count++;
1734                 }
1735         }
1736 }
1737
1738 static struct Qdisc_class_ops htb_class_ops = {
1739         .graft          =       htb_graft,
1740         .leaf           =       htb_leaf,
1741         .get            =       htb_get,
1742         .put            =       htb_put,
1743         .change         =       htb_change_class,
1744         .delete         =       htb_delete,
1745         .walk           =       htb_walk,
1746         .tcf_chain      =       htb_find_tcf,
1747         .bind_tcf       =       htb_bind_filter,
1748         .unbind_tcf     =       htb_unbind_filter,
1749         .dump           =       htb_dump_class,
1750 };
1751
1752 static struct Qdisc_ops htb_qdisc_ops = {
1753         .next           =       NULL,
1754         .cl_ops         =       &htb_class_ops,
1755         .id             =       "htb",
1756         .priv_size      =       sizeof(struct htb_sched),
1757         .enqueue        =       htb_enqueue,
1758         .dequeue        =       htb_dequeue,
1759         .requeue        =       htb_requeue,
1760         .drop           =       htb_drop,
1761         .init           =       htb_init,
1762         .reset          =       htb_reset,
1763         .destroy        =       htb_destroy,
1764         .change         =       NULL /* htb_change */,
1765         .dump           =       htb_dump,
1766         .owner          =       THIS_MODULE,
1767 };
1768
1769 static int __init htb_module_init(void)
1770 {
1771     return register_qdisc(&htb_qdisc_ops);
1772 }
1773 static void __exit htb_module_exit(void) 
1774 {
1775     unregister_qdisc(&htb_qdisc_ops);
1776 }
1777 module_init(htb_module_init)
1778 module_exit(htb_module_exit)
1779 MODULE_LICENSE("GPL");