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