3 # Patch managed by http://www.mn-logistik.de/unsupported/pxa250/patcher
6 --- linux-2.4.20/net/sched/sch_htb.c~110-schhtb 2005-01-07 02:53:54.527710000 -0500
7 +++ linux-2.4.20/net/sched/sch_htb.c 2005-01-07 02:54:26.350872392 -0500
9 * Authors: Martin Devera, <devik@cdi.cz>
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
18 * code review and helpful comments on shaping
19 * Tomasz Wrona, <tw@eter.tym.pl>
20 * created test case so that I was able to fix nasty bug
21 + * Wilfried Weissmann
22 + * spotted bug in dequeue code and helped with fix
24 + * fixed requeue routine
25 * and many others. thanks.
27 - * $Id: sch_htb.c,v 1.14 2002/09/28 12:55:22 devik Exp devik $
28 + * $Id: sch_htb.c,v 1.25 2003/12/07 11:08:25 devik Exp devik $
30 #include <linux/config.h>
31 #include <linux/module.h>
33 #define HTB_HYSTERESIS 1/* whether to use mode hysteresis for speedup */
34 #define HTB_QLOCK(S) spin_lock_bh(&(S)->dev->queue_lock)
35 #define HTB_QUNLOCK(S) spin_unlock_bh(&(S)->dev->queue_lock)
36 -#define HTB_VER 0x30007 /* major must be matched with number suplied by TC as version */
37 +#define HTB_VER 0x30010 /* major must be matched with number suplied by TC as version */
39 #if HTB_VER >> 16 != TC_HTB_PROTOVER
40 #error "Mismatched sch_htb.c and pkt_sch.h"
43 -/* temporary debug defines to be removed after beta stage */
44 -#define DEVIK_MEND(N)
45 -#define DEVIK_MSTART(N)
47 /* debugging support; S is subsystem, these are defined:
54 -#define HTB_DBG(S,L,FMT,ARG...) if (((q->debug>>(2*S))&3) >= L) \
55 +#define HTB_DBG_COND(S,L) (((q->debug>>(2*S))&3) >= L)
56 +#define HTB_DBG(S,L,FMT,ARG...) if (HTB_DBG_COND(S,L)) \
57 printk(KERN_DEBUG FMT,##ARG)
58 #define HTB_CHCL(cl) BUG_TRAP((cl)->magic == HTB_CMAGIC)
60 #define HTB_ARGQ struct htb_sched *q,
66 #define HTB_CMAGIC 0xFEFAFEF1
67 #define htb_safe_rb_erase(N,R) do { BUG_TRAP((N)->rb_color != -1); \
70 (N)->rb_color = -1; } while (0)
72 +#define HTB_DBG_COND(S,L) (0)
73 #define HTB_DBG(S,L,FMT,ARG...)
77 /* time of nearest event per level (row) */
78 unsigned long near_ev_cache[TC_HTB_MAXDEPTH];
80 + /* cached value of jiffies in dequeue */
81 + unsigned long jiffies;
83 /* whether we hit non-work conserving class during this dequeue; we use */
84 int nwc_hit; /* this to disable mindelay complaint in dequeue */
88 if (skb->priority == sch->handle)
89 return HTB_DIRECT; /* X:0 (direct flow) selected */
90 - if ((cl = htb_find(skb->priority,sch)) != NULL)
91 + if ((cl = htb_find(skb->priority,sch)) != NULL && cl->level == 0)
96 static void htb_debug_dump (struct htb_sched *q)
99 - printk(KERN_DEBUG "htb*g j=%lu\n",jiffies);
100 + printk(KERN_DEBUG "htb*g j=%lu lj=%lu\n",jiffies,q->jiffies);
102 for (i=TC_HTB_MAXDEPTH-1;i>=0;i--) {
103 printk(KERN_DEBUG "htb*r%d m=%x",i,q->row_mask[i]);
104 @@ -421,26 +430,24 @@
105 if ((delay <= 0 || delay > cl->mbuffer) && net_ratelimit())
106 printk(KERN_ERR "HTB: suspicious delay in wait_tree d=%ld cl=%X h=%d\n",delay,cl->classid,debug_hint);
109 - cl->pq_key = jiffies + PSCHED_US2JIFFIE(delay);
110 - if (cl->pq_key == jiffies)
111 + cl->pq_key = q->jiffies + PSCHED_US2JIFFIE(delay);
112 + if (cl->pq_key == q->jiffies)
115 /* update the nearest event cache */
116 - if (q->near_ev_cache[cl->level] - cl->pq_key < 0x80000000)
117 + if (time_after(q->near_ev_cache[cl->level], cl->pq_key))
118 q->near_ev_cache[cl->level] = cl->pq_key;
121 struct htb_class *c; parent = *p;
122 c = rb_entry(parent, struct htb_class, pq_node);
123 - if (cl->pq_key - c->pq_key < 0x80000000)
124 + if (time_after_eq(cl->pq_key, c->pq_key))
125 p = &parent->rb_right;
127 p = &parent->rb_left;
129 rb_link_node(&cl->pq_node, parent, p);
130 rb_insert_color(&cl->pq_node, &q->wait_pq[cl->level]);
135 @@ -453,12 +460,14 @@
138 if ((*n)->rb_right) {
139 + /* child at right. use it or its leftmost ancestor */
141 while ((*n)->rb_left)
145 while ((p = (*n)->rb_parent) != NULL) {
146 + /* if we've arrived from left child then we have next node */
147 if (p->rb_left == *n) break;
153 if ((toks = (cl->ctokens + *diff)) < (
154 -#ifdef HTB_HYSTERESIS
156 cl->cmode != HTB_CANT_SEND ? -cl->cbuffer :
160 return HTB_CANT_SEND;
162 if ((toks = (cl->tokens + *diff)) >= (
163 -#ifdef HTB_HYSTERESIS
165 cl->cmode == HTB_CAN_SEND ? -cl->buffer :
169 struct htb_sched *q = (struct htb_sched *)sch->data;
170 struct htb_class *cl = htb_classify(skb,sch);
173 if (cl == HTB_DIRECT || !cl) {
174 /* enqueue to helper queue */
175 if (q->direct_queue.qlen < q->direct_qlen && cl) {
176 @@ -698,25 +706,20 @@
181 return NET_XMIT_DROP;
183 } else if (cl->un.leaf.q->enqueue(skb, cl->un.leaf.q) != NET_XMIT_SUCCESS) {
187 return NET_XMIT_DROP;
189 cl->stats.packets++; cl->stats.bytes += skb->len;
196 sch->stats.packets++; sch->stats.bytes += skb->len;
197 - HTB_DBG(1,1,"htb_enq_ok cl=%X skb=%p\n",cl?cl->classid:0,skb);
199 + HTB_DBG(1,1,"htb_enq_ok cl=%X skb=%p\n",(cl && cl != HTB_DIRECT)?cl->classid:0,skb);
200 return NET_XMIT_SUCCESS;
203 @@ -725,16 +728,18 @@
205 struct htb_sched *q = (struct htb_sched *)sch->data;
206 struct htb_class *cl = htb_classify(skb,sch);
207 + struct sk_buff *tskb;
209 if (cl == HTB_DIRECT || !cl) {
210 /* enqueue to helper queue */
211 if (q->direct_queue.qlen < q->direct_qlen && cl) {
212 - __skb_queue_tail(&q->direct_queue, skb);
214 + __skb_queue_head(&q->direct_queue, skb);
217 - sch->stats.drops++;
218 - return NET_XMIT_DROP;
219 + __skb_queue_head(&q->direct_queue, skb);
220 + tskb = __skb_dequeue_tail(&q->direct_queue);
222 + sch->stats.drops++;
223 + return NET_XMIT_CN;
225 } else if (cl->un.leaf.q->ops->requeue(skb, cl->un.leaf.q) != NET_XMIT_SUCCESS) {
231 - HTB_DBG(1,1,"htb_req_ok cl=%X skb=%p\n",cl?cl->classid:0,skb);
232 + HTB_DBG(1,1,"htb_req_ok cl=%X skb=%p\n",(cl && cl != HTB_DIRECT)?cl->classid:0,skb);
233 return NET_XMIT_SUCCESS;
238 (unsigned long long) q->now,
239 (unsigned long long) cl->t_c,
247 * Scans event queue for pending events and applies them. Returns jiffies to
248 * next pending event (0 for no event in pq).
249 + * Note: Aplied are events whose have cl->pq_key <= jiffies.
251 static long htb_do_events(struct htb_sched *q,int level)
254 while (p->rb_left) p = p->rb_left;
256 cl = rb_entry(p, struct htb_class, pq_node);
257 - if (cl->pq_key - (jiffies+1) < 0x80000000) {
258 - HTB_DBG(8,3,"htb_do_ev_ret delay=%ld\n",cl->pq_key - jiffies);
259 - return cl->pq_key - jiffies;
260 + if (time_after(cl->pq_key, q->jiffies)) {
261 + HTB_DBG(8,3,"htb_do_ev_ret delay=%ld\n",cl->pq_key - q->jiffies);
262 + return cl->pq_key - q->jiffies;
264 htb_safe_rb_erase(p,q->wait_pq+level);
265 diff = PSCHED_TDIFF_SAFE(q->now, cl->t_c, (u32)cl->mbuffer, 0);
268 (unsigned long long) q->now,
269 (unsigned long long) cl->t_c,
277 } stk[TC_HTB_MAXDEPTH],*sp = stk;
279 + BUG_TRAP(tree->rb_node);
280 sp->root = tree->rb_node;
283 @@ -949,16 +956,36 @@
284 htb_dequeue_tree(struct htb_sched *q,int prio,int level)
286 struct sk_buff *skb = NULL;
287 - //struct htb_sched *q = (struct htb_sched *)sch->data;
288 struct htb_class *cl,*start;
289 /* look initial class up in the row */
291 start = cl = htb_lookup_leaf (q->row[level]+prio,prio,q->ptr[level]+prio);
294 - BUG_TRAP(cl && cl->un.leaf.q->q.qlen); if (!cl) return NULL;
297 + if (!cl) return NULL;
298 HTB_DBG(4,1,"htb_deq_tr prio=%d lev=%d cl=%X defic=%d\n",
299 prio,level,cl->classid,cl->un.leaf.deficit[level]);
301 + /* class can be empty - it is unlikely but can be true if leaf
302 + qdisc drops packets in enqueue routine or if someone used
303 + graft operation on the leaf since last dequeue;
304 + simply deactivate and skip such class */
305 + if (unlikely(cl->un.leaf.q->q.qlen == 0)) {
306 + struct htb_class *next;
307 + htb_deactivate(q,cl);
309 + /* row/level might become empty */
310 + if ((q->row_mask[level] & (1 << prio)) == 0)
313 + next = htb_lookup_leaf (q->row[level]+prio,
314 + prio,q->ptr[level]+prio);
315 + if (cl == start) /* fix start if we just deleted it */
321 if (likely((skb = cl->un.leaf.q->dequeue(cl->un.leaf.q)) != NULL))
324 cl = htb_lookup_leaf (q->row[level]+prio,prio,q->ptr[level]+prio);
325 } while (cl != start);
329 if (likely(skb != NULL)) {
330 if ((cl->un.leaf.deficit[level] -= skb->len) < 0) {
331 HTB_DBG(4,2,"htb_next_cl oldptr=%p quant_add=%d\n",
332 @@ -984,11 +1009,8 @@
333 gives us slightly better performance */
334 if (!cl->un.leaf.q->q.qlen)
335 htb_deactivate (q,cl);
337 htb_charge_class (q,cl,level,skb->len);
344 @@ -1002,9 +1024,8 @@
345 printk(KERN_INFO "HTB delay %ld > 5sec\n", delay);
348 - del_timer(&q->timer);
349 - q->timer.expires = jiffies + delay;
350 - add_timer(&q->timer);
351 + /* why don't use jiffies here ? because expires can be in past */
352 + mod_timer(&q->timer, q->jiffies + delay);
353 sch->flags |= TCQ_F_THROTTLED;
354 sch->stats.overlimits++;
355 HTB_DBG(3,1,"htb_deq t_delay=%ld\n",delay);
356 @@ -1016,7 +1037,11 @@
357 struct htb_sched *q = (struct htb_sched *)sch->data;
364 + q->jiffies = jiffies;
365 HTB_DBG(3,1,"htb_deq dircnt=%d qlen=%d\n",skb_queue_len(&q->direct_queue),
368 @@ -1027,27 +1052,26 @@
373 if (!sch->q.qlen) goto fin;
374 PSCHED_GET_TIME(q->now);
377 + min_delay = LONG_MAX;
379 for (level = 0; level < TC_HTB_MAXDEPTH; level++) {
380 /* common case optimization - skip event handler quickly */
384 - if (jiffies - q->near_ev_cache[level] < 0x80000000 || 0) {
385 + if (time_after_eq(q->jiffies, q->near_ev_cache[level])) {
386 delay = htb_do_events(q,level);
387 - q->near_ev_cache[level] += delay ? delay : HZ;
388 + q->near_ev_cache[level] = q->jiffies + (delay ? delay : HZ);
393 - delay = q->near_ev_cache[level] - jiffies;
394 + delay = q->near_ev_cache[level] - q->jiffies;
396 if (delay && min_delay > delay)
400 m = ~q->row_mask[level];
401 while (m != (int)(-1)) {
403 @@ -1056,29 +1080,29 @@
404 if (likely(skb != NULL)) {
406 sch->flags &= ~TCQ_F_THROTTLED;
415 - if (!q->nwc_hit && min_delay >= 5*HZ && net_ratelimit()) {
416 - printk(KERN_ERR "HTB: mindelay=%ld, report it please !\n",min_delay);
418 + if (!q->nwc_hit && min_delay >= 10*HZ && net_ratelimit()) {
419 + if (min_delay == LONG_MAX) {
420 + printk(KERN_ERR "HTB: dequeue bug (%d,%lu,%lu), report it please !\n",
421 + evs_used,q->jiffies,jiffies);
424 + printk(KERN_WARNING "HTB: mindelay=%ld, some class has "
425 + "too small rate\n",min_delay);
428 - htb_delay_by (sch,min_delay);
430 + htb_delay_by (sch,min_delay > 5*HZ ? 5*HZ : min_delay);
432 - HTB_DBG(3,1,"htb_deq_end %s j=%lu skb=%p\n",sch->dev->name,jiffies,skb);
434 + HTB_DBG(3,1,"htb_deq_end %s j=%lu skb=%p\n",sch->dev->name,q->jiffies,skb);
438 /* try to drop from each class (by prio) until one succeed */
439 -static int htb_drop(struct Qdisc* sch)
440 +static unsigned int htb_drop(struct Qdisc* sch)
442 struct htb_sched *q = (struct htb_sched *)sch->data;
444 @@ -1086,14 +1110,15 @@
445 for (prio = TC_HTB_NUMPRIO - 1; prio >= 0; prio--) {
447 list_for_each (p,q->drops+prio) {
448 - struct htb_class *cl = list_entry(p,struct htb_class,
449 - un.leaf.drop_list);
450 + struct htb_class *cl = list_entry(p, struct htb_class,
451 + un.leaf.drop_list);
453 if (cl->un.leaf.q->ops->drop &&
454 - cl->un.leaf.q->ops->drop(cl->un.leaf.q)) {
455 + (len = cl->un.leaf.q->ops->drop(cl->un.leaf.q))) {
457 if (!cl->un.leaf.q->q.qlen)
458 htb_deactivate (q,cl);
464 @@ -1208,7 +1233,8 @@
465 gopt.direct_pkts = q->direct_pkts;
469 + if (HTB_DBG_COND(0,2))
472 gopt.version = HTB_VER;
473 gopt.rate2quantum = q->rate2quantum;
474 @@ -1289,6 +1315,9 @@
477 if ((*old = xchg(&cl->un.leaf.q, new)) != NULL) {
478 + if (cl->prio_activity)
479 + htb_deactivate ((struct htb_sched*)sch->data,cl);
481 /* TODO: is it correct ? Why CBQ doesn't do it ? */
482 sch->q.qlen -= (*old)->q.qlen;
484 @@ -1323,7 +1352,7 @@
486 while ((tp = *fl) != NULL) {
488 - tp->ops->destroy(tp);
493 @@ -1371,11 +1400,16 @@
495 del_timer_sync (&q->rttim);
497 + /* This line used to be after htb_destroy_class call below
498 + and surprisingly it worked in 2.4. But it must precede it
499 + because filter need its target class alive to be able to call
500 + unbind_filter on it (without Oops). */
501 + htb_destroy_filters(&q->filter_list);
503 while (!list_empty(&q->root))
504 htb_destroy_class (sch,list_entry(q->root.next,
505 struct htb_class,sibling));
507 - htb_destroy_filters(&q->filter_list);
508 __skb_queue_purge(&q->direct_queue);
511 @@ -1438,12 +1472,13 @@
512 parent = parentid == TC_H_ROOT ? NULL : htb_find (parentid,sch);
514 hopt = RTA_DATA(tb[TCA_HTB_PARMS-1]);
515 - HTB_DBG(0,1,"htb_chg cl=%p, clid=%X, opt/prio=%d, rate=%u, buff=%d, quant=%d\n", cl,cl?cl->classid:0,(int)hopt->prio,hopt->rate.rate,hopt->buffer,hopt->quantum);
516 + 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);
517 rtab = qdisc_get_rtab(&hopt->rate, tb[TCA_HTB_RTAB-1]);
518 ctab = qdisc_get_rtab(&hopt->ceil, tb[TCA_HTB_CTAB-1]);
519 if (!rtab || !ctab) goto failure;
521 if (!cl) { /* new class */
522 + struct Qdisc *new_q;
523 /* check for valid classid */
524 if (!classid || TC_H_MAJ(classid^sch->handle) || htb_find(classid,sch))
526 @@ -1467,6 +1502,10 @@
527 cl->magic = HTB_CMAGIC;
530 + /* create leaf qdisc early because it uses kmalloc(GFP_KERNEL)
531 + so that can't be used inside of sch_tree_lock
532 + -- thanks to Karlis Peisenieks */
533 + new_q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
535 if (parent && !parent->level) {
536 /* turn parent into inner node */
537 @@ -1485,8 +1524,7 @@
538 memset (&parent->un.inner,0,sizeof(parent->un.inner));
540 /* leaf (we) needs elementary qdisc */
541 - if (!(cl->un.leaf.q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops)))
542 - cl->un.leaf.q = &noop_qdisc;
543 + cl->un.leaf.q = new_q ? new_q : &noop_qdisc;
545 cl->classid = classid; cl->parent = parent;
547 @@ -1514,11 +1552,11 @@
549 cl->un.leaf.quantum = rtab->rate.rate / q->rate2quantum;
550 if (!hopt->quantum && cl->un.leaf.quantum < 1000) {
551 - printk(KERN_WARNING "HTB: quantum of class %X is small. Consider r2q change.", cl->classid);
552 + printk(KERN_WARNING "HTB: quantum of class %X is small. Consider r2q change.\n", cl->classid);
553 cl->un.leaf.quantum = 1000;
555 if (!hopt->quantum && cl->un.leaf.quantum > 200000) {
556 - printk(KERN_WARNING "HTB: quantum of class %X is big. Consider r2q change.", cl->classid);
557 + printk(KERN_WARNING "HTB: quantum of class %X is big. Consider r2q change.\n", cl->classid);
558 cl->un.leaf.quantum = 200000;
561 --- linux-2.4.20/include/net/pkt_cls.h~110-schhtb 2005-01-07 02:53:54.529709000 -0500
562 +++ linux-2.4.20/include/net/pkt_cls.h 2005-01-07 02:53:55.918498816 -0500
568 +static inline void tcf_destroy(struct tcf_proto *tp)
570 + tp->ops->destroy(tp);
574 extern int register_tcf_proto_ops(struct tcf_proto_ops *ops);
575 extern int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);