Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6/libata-dev.git] / net / sched / sch_htb.c
blob2cef8f34b2cb4b4c75bc318421a59088930a80f0
1 /*
2 * net/sched/sch_htb.c Hierarchical token bucket, feed tree version
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: 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
17 * helped a lot to locate nasty class stall bug
18 * Andi Kleen, Jamal Hadi, Bert Hubert
19 * code review and helpful comments on shaping
20 * Tomasz Wrona, <tw@eter.tym.pl>
21 * created test case so that I was able to fix nasty bug
22 * Wilfried Weissmann
23 * spotted bug in dequeue code and helped with fix
24 * Jiri Fojtasek
25 * fixed requeue routine
26 * and many others. thanks.
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/types.h>
31 #include <linux/kernel.h>
32 #include <linux/string.h>
33 #include <linux/errno.h>
34 #include <linux/skbuff.h>
35 #include <linux/list.h>
36 #include <linux/compiler.h>
37 #include <linux/rbtree.h>
38 #include <net/netlink.h>
39 #include <net/pkt_sched.h>
41 /* HTB algorithm.
42 Author: devik@cdi.cz
43 ========================================================================
44 HTB is like TBF with multiple classes. It is also similar to CBQ because
45 it allows to assign priority to each class in hierarchy.
46 In fact it is another implementation of Floyd's formal sharing.
48 Levels:
49 Each class is assigned level. Leaf has ALWAYS level 0 and root
50 classes have level TC_HTB_MAXDEPTH-1. Interior nodes has level
51 one less than their parent.
54 #define HTB_HSIZE 16 /* classid hash size */
55 static int htb_hysteresis __read_mostly = 0; /* whether to use mode hysteresis for speedup */
56 #define HTB_VER 0x30011 /* major must be matched with number suplied by TC as version */
58 #if HTB_VER >> 16 != TC_HTB_PROTOVER
59 #error "Mismatched sch_htb.c and pkt_sch.h"
60 #endif
62 /* Module parameter and sysfs export */
63 module_param (htb_hysteresis, int, 0640);
64 MODULE_PARM_DESC(htb_hysteresis, "Hysteresis mode, less CPU load, less accurate");
66 /* used internaly to keep status of single class */
67 enum htb_cmode {
68 HTB_CANT_SEND, /* class can't send and can't borrow */
69 HTB_MAY_BORROW, /* class can't send but may borrow */
70 HTB_CAN_SEND /* class can send */
73 /* interior & leaf nodes; props specific to leaves are marked L: */
74 struct htb_class {
75 /* general class parameters */
76 u32 classid;
77 struct gnet_stats_basic bstats;
78 struct gnet_stats_queue qstats;
79 struct gnet_stats_rate_est rate_est;
80 struct tc_htb_xstats xstats; /* our special stats */
81 int refcnt; /* usage count of this class */
83 /* topology */
84 int level; /* our level (see above) */
85 struct htb_class *parent; /* parent class */
86 struct hlist_node hlist; /* classid hash list item */
87 struct list_head sibling; /* sibling list item */
88 struct list_head children; /* children list */
90 union {
91 struct htb_class_leaf {
92 struct Qdisc *q;
93 int prio;
94 int aprio;
95 int quantum;
96 int deficit[TC_HTB_MAXDEPTH];
97 struct list_head drop_list;
98 } leaf;
99 struct htb_class_inner {
100 struct rb_root feed[TC_HTB_NUMPRIO]; /* feed trees */
101 struct rb_node *ptr[TC_HTB_NUMPRIO]; /* current class ptr */
102 /* When class changes from state 1->2 and disconnects from
103 parent's feed then we lost ptr value and start from the
104 first child again. Here we store classid of the
105 last valid ptr (used when ptr is NULL). */
106 u32 last_ptr_id[TC_HTB_NUMPRIO];
107 } inner;
108 } un;
109 struct rb_node node[TC_HTB_NUMPRIO]; /* node for self or feed tree */
110 struct rb_node pq_node; /* node for event queue */
111 psched_time_t pq_key;
113 int prio_activity; /* for which prios are we active */
114 enum htb_cmode cmode; /* current mode of the class */
116 /* class attached filters */
117 struct tcf_proto *filter_list;
118 int filter_cnt;
120 int warned; /* only one warning about non work conserving .. */
122 /* token bucket parameters */
123 struct qdisc_rate_table *rate; /* rate table of the class itself */
124 struct qdisc_rate_table *ceil; /* ceiling rate (limits borrows too) */
125 long buffer, cbuffer; /* token bucket depth/rate */
126 psched_tdiff_t mbuffer; /* max wait time */
127 long tokens, ctokens; /* current number of tokens */
128 psched_time_t t_c; /* checkpoint time */
130 int prio; /* For parent to leaf return possible here */
131 int quantum; /* we do backup. Finally full replacement */
132 /* of un.leaf originals should be done. */
135 static inline long L2T(struct htb_class *cl, struct qdisc_rate_table *rate,
136 int size)
138 long result = qdisc_l2t(rate, size);
139 return result;
142 struct htb_sched {
143 struct list_head root; /* root classes list */
144 struct hlist_head hash[HTB_HSIZE]; /* hashed by classid */
145 struct list_head drops[TC_HTB_NUMPRIO];/* active leaves (for drops) */
147 /* self list - roots of self generating tree */
148 struct rb_root row[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
149 int row_mask[TC_HTB_MAXDEPTH];
150 struct rb_node *ptr[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
151 u32 last_ptr_id[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
153 /* self wait list - roots of wait PQs per row */
154 struct rb_root wait_pq[TC_HTB_MAXDEPTH];
156 /* time of nearest event per level (row) */
157 psched_time_t near_ev_cache[TC_HTB_MAXDEPTH];
159 /* whether we hit non-work conserving class during this dequeue; we use */
160 int nwc_hit; /* this to disable mindelay complaint in dequeue */
162 int defcls; /* class where unclassified flows go to */
164 /* filters for qdisc itself */
165 struct tcf_proto *filter_list;
166 int filter_cnt;
168 int rate2quantum; /* quant = rate / rate2quantum */
169 psched_time_t now; /* cached dequeue time */
170 struct qdisc_watchdog watchdog;
172 /* non shaped skbs; let them go directly thru */
173 struct sk_buff_head direct_queue;
174 int direct_qlen; /* max qlen of above */
176 long direct_pkts;
179 /* compute hash of size HTB_HSIZE for given handle */
180 static inline int htb_hash(u32 h)
182 #if HTB_HSIZE != 16
183 #error "Declare new hash for your HTB_HSIZE"
184 #endif
185 h ^= h >> 8; /* stolen from cbq_hash */
186 h ^= h >> 4;
187 return h & 0xf;
190 /* find class in global hash table using given handle */
191 static inline struct htb_class *htb_find(u32 handle, struct Qdisc *sch)
193 struct htb_sched *q = qdisc_priv(sch);
194 struct hlist_node *p;
195 struct htb_class *cl;
197 if (TC_H_MAJ(handle) != sch->handle)
198 return NULL;
200 hlist_for_each_entry(cl, p, q->hash + htb_hash(handle), hlist) {
201 if (cl->classid == handle)
202 return cl;
204 return NULL;
208 * htb_classify - classify a packet into class
210 * It returns NULL if the packet should be dropped or -1 if the packet
211 * should be passed directly thru. In all other cases leaf class is returned.
212 * We allow direct class selection by classid in priority. The we examine
213 * filters in qdisc and in inner nodes (if higher filter points to the inner
214 * node). If we end up with classid MAJOR:0 we enqueue the skb into special
215 * internal fifo (direct). These packets then go directly thru. If we still
216 * have no valid leaf we try to use MAJOR:default leaf. It still unsuccessfull
217 * then finish and return direct queue.
219 #define HTB_DIRECT (struct htb_class*)-1
221 static struct htb_class *htb_classify(struct sk_buff *skb, struct Qdisc *sch,
222 int *qerr)
224 struct htb_sched *q = qdisc_priv(sch);
225 struct htb_class *cl;
226 struct tcf_result res;
227 struct tcf_proto *tcf;
228 int result;
230 /* allow to select class by setting skb->priority to valid classid;
231 note that nfmark can be used too by attaching filter fw with no
232 rules in it */
233 if (skb->priority == sch->handle)
234 return HTB_DIRECT; /* X:0 (direct flow) selected */
235 if ((cl = htb_find(skb->priority, sch)) != NULL && cl->level == 0)
236 return cl;
238 *qerr = NET_XMIT_BYPASS;
239 tcf = q->filter_list;
240 while (tcf && (result = tc_classify(skb, tcf, &res)) >= 0) {
241 #ifdef CONFIG_NET_CLS_ACT
242 switch (result) {
243 case TC_ACT_QUEUED:
244 case TC_ACT_STOLEN:
245 *qerr = NET_XMIT_SUCCESS;
246 case TC_ACT_SHOT:
247 return NULL;
249 #endif
250 if ((cl = (void *)res.class) == NULL) {
251 if (res.classid == sch->handle)
252 return HTB_DIRECT; /* X:0 (direct flow) */
253 if ((cl = htb_find(res.classid, sch)) == NULL)
254 break; /* filter selected invalid classid */
256 if (!cl->level)
257 return cl; /* we hit leaf; return it */
259 /* we have got inner class; apply inner filter chain */
260 tcf = cl->filter_list;
262 /* classification failed; try to use default class */
263 cl = htb_find(TC_H_MAKE(TC_H_MAJ(sch->handle), q->defcls), sch);
264 if (!cl || cl->level)
265 return HTB_DIRECT; /* bad default .. this is safe bet */
266 return cl;
270 * htb_add_to_id_tree - adds class to the round robin list
272 * Routine adds class to the list (actually tree) sorted by classid.
273 * Make sure that class is not already on such list for given prio.
275 static void htb_add_to_id_tree(struct rb_root *root,
276 struct htb_class *cl, int prio)
278 struct rb_node **p = &root->rb_node, *parent = NULL;
280 while (*p) {
281 struct htb_class *c;
282 parent = *p;
283 c = rb_entry(parent, struct htb_class, node[prio]);
285 if (cl->classid > c->classid)
286 p = &parent->rb_right;
287 else
288 p = &parent->rb_left;
290 rb_link_node(&cl->node[prio], parent, p);
291 rb_insert_color(&cl->node[prio], root);
295 * htb_add_to_wait_tree - adds class to the event queue with delay
297 * The class is added to priority event queue to indicate that class will
298 * change its mode in cl->pq_key microseconds. Make sure that class is not
299 * already in the queue.
301 static void htb_add_to_wait_tree(struct htb_sched *q,
302 struct htb_class *cl, long delay)
304 struct rb_node **p = &q->wait_pq[cl->level].rb_node, *parent = NULL;
306 cl->pq_key = q->now + delay;
307 if (cl->pq_key == q->now)
308 cl->pq_key++;
310 /* update the nearest event cache */
311 if (q->near_ev_cache[cl->level] > cl->pq_key)
312 q->near_ev_cache[cl->level] = cl->pq_key;
314 while (*p) {
315 struct htb_class *c;
316 parent = *p;
317 c = rb_entry(parent, struct htb_class, pq_node);
318 if (cl->pq_key >= c->pq_key)
319 p = &parent->rb_right;
320 else
321 p = &parent->rb_left;
323 rb_link_node(&cl->pq_node, parent, p);
324 rb_insert_color(&cl->pq_node, &q->wait_pq[cl->level]);
328 * htb_next_rb_node - finds next node in binary tree
330 * When we are past last key we return NULL.
331 * Average complexity is 2 steps per call.
333 static inline void htb_next_rb_node(struct rb_node **n)
335 *n = rb_next(*n);
339 * htb_add_class_to_row - add class to its row
341 * The class is added to row at priorities marked in mask.
342 * It does nothing if mask == 0.
344 static inline void htb_add_class_to_row(struct htb_sched *q,
345 struct htb_class *cl, int mask)
347 q->row_mask[cl->level] |= mask;
348 while (mask) {
349 int prio = ffz(~mask);
350 mask &= ~(1 << prio);
351 htb_add_to_id_tree(q->row[cl->level] + prio, cl, prio);
355 /* If this triggers, it is a bug in this code, but it need not be fatal */
356 static void htb_safe_rb_erase(struct rb_node *rb, struct rb_root *root)
358 if (RB_EMPTY_NODE(rb)) {
359 WARN_ON(1);
360 } else {
361 rb_erase(rb, root);
362 RB_CLEAR_NODE(rb);
368 * htb_remove_class_from_row - removes class from its row
370 * The class is removed from row at priorities marked in mask.
371 * It does nothing if mask == 0.
373 static inline void htb_remove_class_from_row(struct htb_sched *q,
374 struct htb_class *cl, int mask)
376 int m = 0;
378 while (mask) {
379 int prio = ffz(~mask);
381 mask &= ~(1 << prio);
382 if (q->ptr[cl->level][prio] == cl->node + prio)
383 htb_next_rb_node(q->ptr[cl->level] + prio);
385 htb_safe_rb_erase(cl->node + prio, q->row[cl->level] + prio);
386 if (!q->row[cl->level][prio].rb_node)
387 m |= 1 << prio;
389 q->row_mask[cl->level] &= ~m;
393 * htb_activate_prios - creates active classe's feed chain
395 * The class is connected to ancestors and/or appropriate rows
396 * for priorities it is participating on. cl->cmode must be new
397 * (activated) mode. It does nothing if cl->prio_activity == 0.
399 static void htb_activate_prios(struct htb_sched *q, struct htb_class *cl)
401 struct htb_class *p = cl->parent;
402 long m, mask = cl->prio_activity;
404 while (cl->cmode == HTB_MAY_BORROW && p && mask) {
405 m = mask;
406 while (m) {
407 int prio = ffz(~m);
408 m &= ~(1 << prio);
410 if (p->un.inner.feed[prio].rb_node)
411 /* parent already has its feed in use so that
412 reset bit in mask as parent is already ok */
413 mask &= ~(1 << prio);
415 htb_add_to_id_tree(p->un.inner.feed + prio, cl, prio);
417 p->prio_activity |= mask;
418 cl = p;
419 p = cl->parent;
422 if (cl->cmode == HTB_CAN_SEND && mask)
423 htb_add_class_to_row(q, cl, mask);
427 * htb_deactivate_prios - remove class from feed chain
429 * cl->cmode must represent old mode (before deactivation). It does
430 * nothing if cl->prio_activity == 0. Class is removed from all feed
431 * chains and rows.
433 static void htb_deactivate_prios(struct htb_sched *q, struct htb_class *cl)
435 struct htb_class *p = cl->parent;
436 long m, mask = cl->prio_activity;
438 while (cl->cmode == HTB_MAY_BORROW && p && mask) {
439 m = mask;
440 mask = 0;
441 while (m) {
442 int prio = ffz(~m);
443 m &= ~(1 << prio);
445 if (p->un.inner.ptr[prio] == cl->node + prio) {
446 /* we are removing child which is pointed to from
447 parent feed - forget the pointer but remember
448 classid */
449 p->un.inner.last_ptr_id[prio] = cl->classid;
450 p->un.inner.ptr[prio] = NULL;
453 htb_safe_rb_erase(cl->node + prio, p->un.inner.feed + prio);
455 if (!p->un.inner.feed[prio].rb_node)
456 mask |= 1 << prio;
459 p->prio_activity &= ~mask;
460 cl = p;
461 p = cl->parent;
464 if (cl->cmode == HTB_CAN_SEND && mask)
465 htb_remove_class_from_row(q, cl, mask);
468 static inline long htb_lowater(const struct htb_class *cl)
470 if (htb_hysteresis)
471 return cl->cmode != HTB_CANT_SEND ? -cl->cbuffer : 0;
472 else
473 return 0;
475 static inline long htb_hiwater(const struct htb_class *cl)
477 if (htb_hysteresis)
478 return cl->cmode == HTB_CAN_SEND ? -cl->buffer : 0;
479 else
480 return 0;
485 * htb_class_mode - computes and returns current class mode
487 * It computes cl's mode at time cl->t_c+diff and returns it. If mode
488 * is not HTB_CAN_SEND then cl->pq_key is updated to time difference
489 * from now to time when cl will change its state.
490 * Also it is worth to note that class mode doesn't change simply
491 * at cl->{c,}tokens == 0 but there can rather be hysteresis of
492 * 0 .. -cl->{c,}buffer range. It is meant to limit number of
493 * mode transitions per time unit. The speed gain is about 1/6.
495 static inline enum htb_cmode
496 htb_class_mode(struct htb_class *cl, long *diff)
498 long toks;
500 if ((toks = (cl->ctokens + *diff)) < htb_lowater(cl)) {
501 *diff = -toks;
502 return HTB_CANT_SEND;
505 if ((toks = (cl->tokens + *diff)) >= htb_hiwater(cl))
506 return HTB_CAN_SEND;
508 *diff = -toks;
509 return HTB_MAY_BORROW;
513 * htb_change_class_mode - changes classe's mode
515 * This should be the only way how to change classe's mode under normal
516 * cirsumstances. Routine will update feed lists linkage, change mode
517 * and add class to the wait event queue if appropriate. New mode should
518 * be different from old one and cl->pq_key has to be valid if changing
519 * to mode other than HTB_CAN_SEND (see htb_add_to_wait_tree).
521 static void
522 htb_change_class_mode(struct htb_sched *q, struct htb_class *cl, long *diff)
524 enum htb_cmode new_mode = htb_class_mode(cl, diff);
526 if (new_mode == cl->cmode)
527 return;
529 if (cl->prio_activity) { /* not necessary: speed optimization */
530 if (cl->cmode != HTB_CANT_SEND)
531 htb_deactivate_prios(q, cl);
532 cl->cmode = new_mode;
533 if (new_mode != HTB_CANT_SEND)
534 htb_activate_prios(q, cl);
535 } else
536 cl->cmode = new_mode;
540 * htb_activate - inserts leaf cl into appropriate active feeds
542 * Routine learns (new) priority of leaf and activates feed chain
543 * for the prio. It can be called on already active leaf safely.
544 * It also adds leaf into droplist.
546 static inline void htb_activate(struct htb_sched *q, struct htb_class *cl)
548 BUG_TRAP(!cl->level && cl->un.leaf.q && cl->un.leaf.q->q.qlen);
550 if (!cl->prio_activity) {
551 cl->prio_activity = 1 << (cl->un.leaf.aprio = cl->un.leaf.prio);
552 htb_activate_prios(q, cl);
553 list_add_tail(&cl->un.leaf.drop_list,
554 q->drops + cl->un.leaf.aprio);
559 * htb_deactivate - remove leaf cl from active feeds
561 * Make sure that leaf is active. In the other words it can't be called
562 * with non-active leaf. It also removes class from the drop list.
564 static inline void htb_deactivate(struct htb_sched *q, struct htb_class *cl)
566 BUG_TRAP(cl->prio_activity);
568 htb_deactivate_prios(q, cl);
569 cl->prio_activity = 0;
570 list_del_init(&cl->un.leaf.drop_list);
573 static int htb_enqueue(struct sk_buff *skb, struct Qdisc *sch)
575 int ret;
576 struct htb_sched *q = qdisc_priv(sch);
577 struct htb_class *cl = htb_classify(skb, sch, &ret);
579 if (cl == HTB_DIRECT) {
580 /* enqueue to helper queue */
581 if (q->direct_queue.qlen < q->direct_qlen) {
582 __skb_queue_tail(&q->direct_queue, skb);
583 q->direct_pkts++;
584 } else {
585 kfree_skb(skb);
586 sch->qstats.drops++;
587 return NET_XMIT_DROP;
589 #ifdef CONFIG_NET_CLS_ACT
590 } else if (!cl) {
591 if (ret == NET_XMIT_BYPASS)
592 sch->qstats.drops++;
593 kfree_skb(skb);
594 return ret;
595 #endif
596 } else if (cl->un.leaf.q->enqueue(skb, cl->un.leaf.q) !=
597 NET_XMIT_SUCCESS) {
598 sch->qstats.drops++;
599 cl->qstats.drops++;
600 return NET_XMIT_DROP;
601 } else {
602 cl->bstats.packets +=
603 skb_is_gso(skb)?skb_shinfo(skb)->gso_segs:1;
604 cl->bstats.bytes += skb->len;
605 htb_activate(q, cl);
608 sch->q.qlen++;
609 sch->bstats.packets += skb_is_gso(skb)?skb_shinfo(skb)->gso_segs:1;
610 sch->bstats.bytes += skb->len;
611 return NET_XMIT_SUCCESS;
614 /* TODO: requeuing packet charges it to policers again !! */
615 static int htb_requeue(struct sk_buff *skb, struct Qdisc *sch)
617 int ret;
618 struct htb_sched *q = qdisc_priv(sch);
619 struct htb_class *cl = htb_classify(skb, sch, &ret);
620 struct sk_buff *tskb;
622 if (cl == HTB_DIRECT) {
623 /* enqueue to helper queue */
624 if (q->direct_queue.qlen < q->direct_qlen) {
625 __skb_queue_head(&q->direct_queue, skb);
626 } else {
627 __skb_queue_head(&q->direct_queue, skb);
628 tskb = __skb_dequeue_tail(&q->direct_queue);
629 kfree_skb(tskb);
630 sch->qstats.drops++;
631 return NET_XMIT_CN;
633 #ifdef CONFIG_NET_CLS_ACT
634 } else if (!cl) {
635 if (ret == NET_XMIT_BYPASS)
636 sch->qstats.drops++;
637 kfree_skb(skb);
638 return ret;
639 #endif
640 } else if (cl->un.leaf.q->ops->requeue(skb, cl->un.leaf.q) !=
641 NET_XMIT_SUCCESS) {
642 sch->qstats.drops++;
643 cl->qstats.drops++;
644 return NET_XMIT_DROP;
645 } else
646 htb_activate(q, cl);
648 sch->q.qlen++;
649 sch->qstats.requeues++;
650 return NET_XMIT_SUCCESS;
654 * htb_charge_class - charges amount "bytes" to leaf and ancestors
656 * Routine assumes that packet "bytes" long was dequeued from leaf cl
657 * borrowing from "level". It accounts bytes to ceil leaky bucket for
658 * leaf and all ancestors and to rate bucket for ancestors at levels
659 * "level" and higher. It also handles possible change of mode resulting
660 * from the update. Note that mode can also increase here (MAY_BORROW to
661 * CAN_SEND) because we can use more precise clock that event queue here.
662 * In such case we remove class from event queue first.
664 static void htb_charge_class(struct htb_sched *q, struct htb_class *cl,
665 int level, struct sk_buff *skb)
667 int bytes = skb->len;
668 long toks, diff;
669 enum htb_cmode old_mode;
671 #define HTB_ACCNT(T,B,R) toks = diff + cl->T; \
672 if (toks > cl->B) toks = cl->B; \
673 toks -= L2T(cl, cl->R, bytes); \
674 if (toks <= -cl->mbuffer) toks = 1-cl->mbuffer; \
675 cl->T = toks
677 while (cl) {
678 diff = psched_tdiff_bounded(q->now, cl->t_c, cl->mbuffer);
679 if (cl->level >= level) {
680 if (cl->level == level)
681 cl->xstats.lends++;
682 HTB_ACCNT(tokens, buffer, rate);
683 } else {
684 cl->xstats.borrows++;
685 cl->tokens += diff; /* we moved t_c; update tokens */
687 HTB_ACCNT(ctokens, cbuffer, ceil);
688 cl->t_c = q->now;
690 old_mode = cl->cmode;
691 diff = 0;
692 htb_change_class_mode(q, cl, &diff);
693 if (old_mode != cl->cmode) {
694 if (old_mode != HTB_CAN_SEND)
695 htb_safe_rb_erase(&cl->pq_node, q->wait_pq + cl->level);
696 if (cl->cmode != HTB_CAN_SEND)
697 htb_add_to_wait_tree(q, cl, diff);
700 /* update byte stats except for leaves which are already updated */
701 if (cl->level) {
702 cl->bstats.bytes += bytes;
703 cl->bstats.packets += skb_is_gso(skb)?
704 skb_shinfo(skb)->gso_segs:1;
706 cl = cl->parent;
711 * htb_do_events - make mode changes to classes at the level
713 * Scans event queue for pending events and applies them. Returns time of
714 * next pending event (0 for no event in pq).
715 * Note: Applied are events whose have cl->pq_key <= q->now.
717 static psched_time_t htb_do_events(struct htb_sched *q, int level)
719 /* don't run for longer than 2 jiffies; 2 is used instead of
720 1 to simplify things when jiffy is going to be incremented
721 too soon */
722 unsigned long stop_at = jiffies + 2;
723 while (time_before(jiffies, stop_at)) {
724 struct htb_class *cl;
725 long diff;
726 struct rb_node *p = rb_first(&q->wait_pq[level]);
728 if (!p)
729 return 0;
731 cl = rb_entry(p, struct htb_class, pq_node);
732 if (cl->pq_key > q->now)
733 return cl->pq_key;
735 htb_safe_rb_erase(p, q->wait_pq + level);
736 diff = psched_tdiff_bounded(q->now, cl->t_c, cl->mbuffer);
737 htb_change_class_mode(q, cl, &diff);
738 if (cl->cmode != HTB_CAN_SEND)
739 htb_add_to_wait_tree(q, cl, diff);
741 /* too much load - let's continue on next jiffie */
742 return q->now + PSCHED_TICKS_PER_SEC / HZ;
745 /* Returns class->node+prio from id-tree where classe's id is >= id. NULL
746 is no such one exists. */
747 static struct rb_node *htb_id_find_next_upper(int prio, struct rb_node *n,
748 u32 id)
750 struct rb_node *r = NULL;
751 while (n) {
752 struct htb_class *cl =
753 rb_entry(n, struct htb_class, node[prio]);
754 if (id == cl->classid)
755 return n;
757 if (id > cl->classid) {
758 n = n->rb_right;
759 } else {
760 r = n;
761 n = n->rb_left;
764 return r;
768 * htb_lookup_leaf - returns next leaf class in DRR order
770 * Find leaf where current feed pointers points to.
772 static struct htb_class *htb_lookup_leaf(struct rb_root *tree, int prio,
773 struct rb_node **pptr, u32 * pid)
775 int i;
776 struct {
777 struct rb_node *root;
778 struct rb_node **pptr;
779 u32 *pid;
780 } stk[TC_HTB_MAXDEPTH], *sp = stk;
782 BUG_TRAP(tree->rb_node);
783 sp->root = tree->rb_node;
784 sp->pptr = pptr;
785 sp->pid = pid;
787 for (i = 0; i < 65535; i++) {
788 if (!*sp->pptr && *sp->pid) {
789 /* ptr was invalidated but id is valid - try to recover
790 the original or next ptr */
791 *sp->pptr =
792 htb_id_find_next_upper(prio, sp->root, *sp->pid);
794 *sp->pid = 0; /* ptr is valid now so that remove this hint as it
795 can become out of date quickly */
796 if (!*sp->pptr) { /* we are at right end; rewind & go up */
797 *sp->pptr = sp->root;
798 while ((*sp->pptr)->rb_left)
799 *sp->pptr = (*sp->pptr)->rb_left;
800 if (sp > stk) {
801 sp--;
802 BUG_TRAP(*sp->pptr);
803 if (!*sp->pptr)
804 return NULL;
805 htb_next_rb_node(sp->pptr);
807 } else {
808 struct htb_class *cl;
809 cl = rb_entry(*sp->pptr, struct htb_class, node[prio]);
810 if (!cl->level)
811 return cl;
812 (++sp)->root = cl->un.inner.feed[prio].rb_node;
813 sp->pptr = cl->un.inner.ptr + prio;
814 sp->pid = cl->un.inner.last_ptr_id + prio;
817 BUG_TRAP(0);
818 return NULL;
821 /* dequeues packet at given priority and level; call only if
822 you are sure that there is active class at prio/level */
823 static struct sk_buff *htb_dequeue_tree(struct htb_sched *q, int prio,
824 int level)
826 struct sk_buff *skb = NULL;
827 struct htb_class *cl, *start;
828 /* look initial class up in the row */
829 start = cl = htb_lookup_leaf(q->row[level] + prio, prio,
830 q->ptr[level] + prio,
831 q->last_ptr_id[level] + prio);
833 do {
834 next:
835 BUG_TRAP(cl);
836 if (!cl)
837 return NULL;
839 /* class can be empty - it is unlikely but can be true if leaf
840 qdisc drops packets in enqueue routine or if someone used
841 graft operation on the leaf since last dequeue;
842 simply deactivate and skip such class */
843 if (unlikely(cl->un.leaf.q->q.qlen == 0)) {
844 struct htb_class *next;
845 htb_deactivate(q, cl);
847 /* row/level might become empty */
848 if ((q->row_mask[level] & (1 << prio)) == 0)
849 return NULL;
851 next = htb_lookup_leaf(q->row[level] + prio,
852 prio, q->ptr[level] + prio,
853 q->last_ptr_id[level] + prio);
855 if (cl == start) /* fix start if we just deleted it */
856 start = next;
857 cl = next;
858 goto next;
861 skb = cl->un.leaf.q->dequeue(cl->un.leaf.q);
862 if (likely(skb != NULL))
863 break;
864 if (!cl->warned) {
865 printk(KERN_WARNING
866 "htb: class %X isn't work conserving ?!\n",
867 cl->classid);
868 cl->warned = 1;
870 q->nwc_hit++;
871 htb_next_rb_node((level ? cl->parent->un.inner.ptr : q->
872 ptr[0]) + prio);
873 cl = htb_lookup_leaf(q->row[level] + prio, prio,
874 q->ptr[level] + prio,
875 q->last_ptr_id[level] + prio);
877 } while (cl != start);
879 if (likely(skb != NULL)) {
880 if ((cl->un.leaf.deficit[level] -= skb->len) < 0) {
881 cl->un.leaf.deficit[level] += cl->un.leaf.quantum;
882 htb_next_rb_node((level ? cl->parent->un.inner.ptr : q->
883 ptr[0]) + prio);
885 /* this used to be after charge_class but this constelation
886 gives us slightly better performance */
887 if (!cl->un.leaf.q->q.qlen)
888 htb_deactivate(q, cl);
889 htb_charge_class(q, cl, level, skb);
891 return skb;
894 static struct sk_buff *htb_dequeue(struct Qdisc *sch)
896 struct sk_buff *skb = NULL;
897 struct htb_sched *q = qdisc_priv(sch);
898 int level;
899 psched_time_t next_event;
901 /* try to dequeue direct packets as high prio (!) to minimize cpu work */
902 skb = __skb_dequeue(&q->direct_queue);
903 if (skb != NULL) {
904 sch->flags &= ~TCQ_F_THROTTLED;
905 sch->q.qlen--;
906 return skb;
909 if (!sch->q.qlen)
910 goto fin;
911 q->now = psched_get_time();
913 next_event = q->now + 5 * PSCHED_TICKS_PER_SEC;
914 q->nwc_hit = 0;
915 for (level = 0; level < TC_HTB_MAXDEPTH; level++) {
916 /* common case optimization - skip event handler quickly */
917 int m;
918 psched_time_t event;
920 if (q->now >= q->near_ev_cache[level]) {
921 event = htb_do_events(q, level);
922 if (!event)
923 event = q->now + PSCHED_TICKS_PER_SEC;
924 q->near_ev_cache[level] = event;
925 } else
926 event = q->near_ev_cache[level];
928 if (event && next_event > event)
929 next_event = event;
931 m = ~q->row_mask[level];
932 while (m != (int)(-1)) {
933 int prio = ffz(m);
934 m |= 1 << prio;
935 skb = htb_dequeue_tree(q, prio, level);
936 if (likely(skb != NULL)) {
937 sch->q.qlen--;
938 sch->flags &= ~TCQ_F_THROTTLED;
939 goto fin;
943 sch->qstats.overlimits++;
944 qdisc_watchdog_schedule(&q->watchdog, next_event);
945 fin:
946 return skb;
949 /* try to drop from each class (by prio) until one succeed */
950 static unsigned int htb_drop(struct Qdisc *sch)
952 struct htb_sched *q = qdisc_priv(sch);
953 int prio;
955 for (prio = TC_HTB_NUMPRIO - 1; prio >= 0; prio--) {
956 struct list_head *p;
957 list_for_each(p, q->drops + prio) {
958 struct htb_class *cl = list_entry(p, struct htb_class,
959 un.leaf.drop_list);
960 unsigned int len;
961 if (cl->un.leaf.q->ops->drop &&
962 (len = cl->un.leaf.q->ops->drop(cl->un.leaf.q))) {
963 sch->q.qlen--;
964 if (!cl->un.leaf.q->q.qlen)
965 htb_deactivate(q, cl);
966 return len;
970 return 0;
973 /* reset all classes */
974 /* always caled under BH & queue lock */
975 static void htb_reset(struct Qdisc *sch)
977 struct htb_sched *q = qdisc_priv(sch);
978 int i;
980 for (i = 0; i < HTB_HSIZE; i++) {
981 struct hlist_node *p;
982 struct htb_class *cl;
984 hlist_for_each_entry(cl, p, q->hash + i, hlist) {
985 if (cl->level)
986 memset(&cl->un.inner, 0, sizeof(cl->un.inner));
987 else {
988 if (cl->un.leaf.q)
989 qdisc_reset(cl->un.leaf.q);
990 INIT_LIST_HEAD(&cl->un.leaf.drop_list);
992 cl->prio_activity = 0;
993 cl->cmode = HTB_CAN_SEND;
997 qdisc_watchdog_cancel(&q->watchdog);
998 __skb_queue_purge(&q->direct_queue);
999 sch->q.qlen = 0;
1000 memset(q->row, 0, sizeof(q->row));
1001 memset(q->row_mask, 0, sizeof(q->row_mask));
1002 memset(q->wait_pq, 0, sizeof(q->wait_pq));
1003 memset(q->ptr, 0, sizeof(q->ptr));
1004 for (i = 0; i < TC_HTB_NUMPRIO; i++)
1005 INIT_LIST_HEAD(q->drops + i);
1008 static const struct nla_policy htb_policy[TCA_HTB_MAX + 1] = {
1009 [TCA_HTB_PARMS] = { .len = sizeof(struct tc_htb_opt) },
1010 [TCA_HTB_INIT] = { .len = sizeof(struct tc_htb_glob) },
1011 [TCA_HTB_CTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
1012 [TCA_HTB_RTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
1015 static int htb_init(struct Qdisc *sch, struct nlattr *opt)
1017 struct htb_sched *q = qdisc_priv(sch);
1018 struct nlattr *tb[TCA_HTB_INIT + 1];
1019 struct tc_htb_glob *gopt;
1020 int err;
1021 int i;
1023 if (!opt)
1024 return -EINVAL;
1026 err = nla_parse_nested(tb, TCA_HTB_INIT, opt, htb_policy);
1027 if (err < 0)
1028 return err;
1030 if (tb[TCA_HTB_INIT] == NULL) {
1031 printk(KERN_ERR "HTB: hey probably you have bad tc tool ?\n");
1032 return -EINVAL;
1034 gopt = nla_data(tb[TCA_HTB_INIT]);
1035 if (gopt->version != HTB_VER >> 16) {
1036 printk(KERN_ERR
1037 "HTB: need tc/htb version %d (minor is %d), you have %d\n",
1038 HTB_VER >> 16, HTB_VER & 0xffff, gopt->version);
1039 return -EINVAL;
1042 INIT_LIST_HEAD(&q->root);
1043 for (i = 0; i < HTB_HSIZE; i++)
1044 INIT_HLIST_HEAD(q->hash + i);
1045 for (i = 0; i < TC_HTB_NUMPRIO; i++)
1046 INIT_LIST_HEAD(q->drops + i);
1048 qdisc_watchdog_init(&q->watchdog, sch);
1049 skb_queue_head_init(&q->direct_queue);
1051 q->direct_qlen = sch->dev->tx_queue_len;
1052 if (q->direct_qlen < 2) /* some devices have zero tx_queue_len */
1053 q->direct_qlen = 2;
1055 if ((q->rate2quantum = gopt->rate2quantum) < 1)
1056 q->rate2quantum = 1;
1057 q->defcls = gopt->defcls;
1059 return 0;
1062 static int htb_dump(struct Qdisc *sch, struct sk_buff *skb)
1064 struct htb_sched *q = qdisc_priv(sch);
1065 struct nlattr *nest;
1066 struct tc_htb_glob gopt;
1068 spin_lock_bh(&sch->dev->queue_lock);
1070 gopt.direct_pkts = q->direct_pkts;
1071 gopt.version = HTB_VER;
1072 gopt.rate2quantum = q->rate2quantum;
1073 gopt.defcls = q->defcls;
1074 gopt.debug = 0;
1076 nest = nla_nest_start(skb, TCA_OPTIONS);
1077 if (nest == NULL)
1078 goto nla_put_failure;
1079 NLA_PUT(skb, TCA_HTB_INIT, sizeof(gopt), &gopt);
1080 nla_nest_end(skb, nest);
1082 spin_unlock_bh(&sch->dev->queue_lock);
1083 return skb->len;
1085 nla_put_failure:
1086 spin_unlock_bh(&sch->dev->queue_lock);
1087 nla_nest_cancel(skb, nest);
1088 return -1;
1091 static int htb_dump_class(struct Qdisc *sch, unsigned long arg,
1092 struct sk_buff *skb, struct tcmsg *tcm)
1094 struct htb_class *cl = (struct htb_class *)arg;
1095 struct nlattr *nest;
1096 struct tc_htb_opt opt;
1098 spin_lock_bh(&sch->dev->queue_lock);
1099 tcm->tcm_parent = cl->parent ? cl->parent->classid : TC_H_ROOT;
1100 tcm->tcm_handle = cl->classid;
1101 if (!cl->level && cl->un.leaf.q)
1102 tcm->tcm_info = cl->un.leaf.q->handle;
1104 nest = nla_nest_start(skb, TCA_OPTIONS);
1105 if (nest == NULL)
1106 goto nla_put_failure;
1108 memset(&opt, 0, sizeof(opt));
1110 opt.rate = cl->rate->rate;
1111 opt.buffer = cl->buffer;
1112 opt.ceil = cl->ceil->rate;
1113 opt.cbuffer = cl->cbuffer;
1114 opt.quantum = cl->un.leaf.quantum;
1115 opt.prio = cl->un.leaf.prio;
1116 opt.level = cl->level;
1117 NLA_PUT(skb, TCA_HTB_PARMS, sizeof(opt), &opt);
1119 nla_nest_end(skb, nest);
1120 spin_unlock_bh(&sch->dev->queue_lock);
1121 return skb->len;
1123 nla_put_failure:
1124 spin_unlock_bh(&sch->dev->queue_lock);
1125 nla_nest_cancel(skb, nest);
1126 return -1;
1129 static int
1130 htb_dump_class_stats(struct Qdisc *sch, unsigned long arg, struct gnet_dump *d)
1132 struct htb_class *cl = (struct htb_class *)arg;
1134 if (!cl->level && cl->un.leaf.q)
1135 cl->qstats.qlen = cl->un.leaf.q->q.qlen;
1136 cl->xstats.tokens = cl->tokens;
1137 cl->xstats.ctokens = cl->ctokens;
1139 if (gnet_stats_copy_basic(d, &cl->bstats) < 0 ||
1140 gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
1141 gnet_stats_copy_queue(d, &cl->qstats) < 0)
1142 return -1;
1144 return gnet_stats_copy_app(d, &cl->xstats, sizeof(cl->xstats));
1147 static int htb_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
1148 struct Qdisc **old)
1150 struct htb_class *cl = (struct htb_class *)arg;
1152 if (cl && !cl->level) {
1153 if (new == NULL &&
1154 (new = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
1155 cl->classid))
1156 == NULL)
1157 return -ENOBUFS;
1158 sch_tree_lock(sch);
1159 if ((*old = xchg(&cl->un.leaf.q, new)) != NULL) {
1160 qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
1161 qdisc_reset(*old);
1163 sch_tree_unlock(sch);
1164 return 0;
1166 return -ENOENT;
1169 static struct Qdisc *htb_leaf(struct Qdisc *sch, unsigned long arg)
1171 struct htb_class *cl = (struct htb_class *)arg;
1172 return (cl && !cl->level) ? cl->un.leaf.q : NULL;
1175 static void htb_qlen_notify(struct Qdisc *sch, unsigned long arg)
1177 struct htb_class *cl = (struct htb_class *)arg;
1179 if (cl->un.leaf.q->q.qlen == 0)
1180 htb_deactivate(qdisc_priv(sch), cl);
1183 static unsigned long htb_get(struct Qdisc *sch, u32 classid)
1185 struct htb_class *cl = htb_find(classid, sch);
1186 if (cl)
1187 cl->refcnt++;
1188 return (unsigned long)cl;
1191 static inline int htb_parent_last_child(struct htb_class *cl)
1193 if (!cl->parent)
1194 /* the root class */
1195 return 0;
1197 if (!(cl->parent->children.next == &cl->sibling &&
1198 cl->parent->children.prev == &cl->sibling))
1199 /* not the last child */
1200 return 0;
1202 return 1;
1205 static void htb_parent_to_leaf(struct htb_sched *q, struct htb_class *cl,
1206 struct Qdisc *new_q)
1208 struct htb_class *parent = cl->parent;
1210 BUG_TRAP(!cl->level && cl->un.leaf.q && !cl->prio_activity);
1212 if (parent->cmode != HTB_CAN_SEND)
1213 htb_safe_rb_erase(&parent->pq_node, q->wait_pq + parent->level);
1215 parent->level = 0;
1216 memset(&parent->un.inner, 0, sizeof(parent->un.inner));
1217 INIT_LIST_HEAD(&parent->un.leaf.drop_list);
1218 parent->un.leaf.q = new_q ? new_q : &noop_qdisc;
1219 parent->un.leaf.quantum = parent->quantum;
1220 parent->un.leaf.prio = parent->prio;
1221 parent->tokens = parent->buffer;
1222 parent->ctokens = parent->cbuffer;
1223 parent->t_c = psched_get_time();
1224 parent->cmode = HTB_CAN_SEND;
1227 static void htb_destroy_class(struct Qdisc *sch, struct htb_class *cl)
1229 struct htb_sched *q = qdisc_priv(sch);
1231 if (!cl->level) {
1232 BUG_TRAP(cl->un.leaf.q);
1233 qdisc_destroy(cl->un.leaf.q);
1235 gen_kill_estimator(&cl->bstats, &cl->rate_est);
1236 qdisc_put_rtab(cl->rate);
1237 qdisc_put_rtab(cl->ceil);
1239 tcf_destroy_chain(cl->filter_list);
1241 while (!list_empty(&cl->children))
1242 htb_destroy_class(sch, list_entry(cl->children.next,
1243 struct htb_class, sibling));
1245 /* note: this delete may happen twice (see htb_delete) */
1246 hlist_del_init(&cl->hlist);
1247 list_del(&cl->sibling);
1249 if (cl->prio_activity)
1250 htb_deactivate(q, cl);
1252 if (cl->cmode != HTB_CAN_SEND)
1253 htb_safe_rb_erase(&cl->pq_node, q->wait_pq + cl->level);
1255 kfree(cl);
1258 /* always caled under BH & queue lock */
1259 static void htb_destroy(struct Qdisc *sch)
1261 struct htb_sched *q = qdisc_priv(sch);
1263 qdisc_watchdog_cancel(&q->watchdog);
1264 /* This line used to be after htb_destroy_class call below
1265 and surprisingly it worked in 2.4. But it must precede it
1266 because filter need its target class alive to be able to call
1267 unbind_filter on it (without Oops). */
1268 tcf_destroy_chain(q->filter_list);
1270 while (!list_empty(&q->root))
1271 htb_destroy_class(sch, list_entry(q->root.next,
1272 struct htb_class, sibling));
1274 __skb_queue_purge(&q->direct_queue);
1277 static int htb_delete(struct Qdisc *sch, unsigned long arg)
1279 struct htb_sched *q = qdisc_priv(sch);
1280 struct htb_class *cl = (struct htb_class *)arg;
1281 unsigned int qlen;
1282 struct Qdisc *new_q = NULL;
1283 int last_child = 0;
1285 // TODO: why don't allow to delete subtree ? references ? does
1286 // tc subsys quarantee us that in htb_destroy it holds no class
1287 // refs so that we can remove children safely there ?
1288 if (!list_empty(&cl->children) || cl->filter_cnt)
1289 return -EBUSY;
1291 if (!cl->level && htb_parent_last_child(cl)) {
1292 new_q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
1293 cl->parent->classid);
1294 last_child = 1;
1297 sch_tree_lock(sch);
1299 if (!cl->level) {
1300 qlen = cl->un.leaf.q->q.qlen;
1301 qdisc_reset(cl->un.leaf.q);
1302 qdisc_tree_decrease_qlen(cl->un.leaf.q, qlen);
1305 /* delete from hash and active; remainder in destroy_class */
1306 hlist_del_init(&cl->hlist);
1308 if (cl->prio_activity)
1309 htb_deactivate(q, cl);
1311 if (last_child)
1312 htb_parent_to_leaf(q, cl, new_q);
1314 if (--cl->refcnt == 0)
1315 htb_destroy_class(sch, cl);
1317 sch_tree_unlock(sch);
1318 return 0;
1321 static void htb_put(struct Qdisc *sch, unsigned long arg)
1323 struct htb_class *cl = (struct htb_class *)arg;
1325 if (--cl->refcnt == 0)
1326 htb_destroy_class(sch, cl);
1329 static int htb_change_class(struct Qdisc *sch, u32 classid,
1330 u32 parentid, struct nlattr **tca,
1331 unsigned long *arg)
1333 int err = -EINVAL;
1334 struct htb_sched *q = qdisc_priv(sch);
1335 struct htb_class *cl = (struct htb_class *)*arg, *parent;
1336 struct nlattr *opt = tca[TCA_OPTIONS];
1337 struct qdisc_rate_table *rtab = NULL, *ctab = NULL;
1338 struct nlattr *tb[TCA_HTB_RTAB + 1];
1339 struct tc_htb_opt *hopt;
1341 /* extract all subattrs from opt attr */
1342 if (!opt)
1343 goto failure;
1345 err = nla_parse_nested(tb, TCA_HTB_RTAB, opt, htb_policy);
1346 if (err < 0)
1347 goto failure;
1349 err = -EINVAL;
1350 if (tb[TCA_HTB_PARMS] == NULL)
1351 goto failure;
1353 parent = parentid == TC_H_ROOT ? NULL : htb_find(parentid, sch);
1355 hopt = nla_data(tb[TCA_HTB_PARMS]);
1357 rtab = qdisc_get_rtab(&hopt->rate, tb[TCA_HTB_RTAB]);
1358 ctab = qdisc_get_rtab(&hopt->ceil, tb[TCA_HTB_CTAB]);
1359 if (!rtab || !ctab)
1360 goto failure;
1362 if (!cl) { /* new class */
1363 struct Qdisc *new_q;
1364 int prio;
1365 struct {
1366 struct nlattr nla;
1367 struct gnet_estimator opt;
1368 } est = {
1369 .nla = {
1370 .nla_len = nla_attr_size(sizeof(est.opt)),
1371 .nla_type = TCA_RATE,
1373 .opt = {
1374 /* 4s interval, 16s averaging constant */
1375 .interval = 2,
1376 .ewma_log = 2,
1380 /* check for valid classid */
1381 if (!classid || TC_H_MAJ(classid ^ sch->handle)
1382 || htb_find(classid, sch))
1383 goto failure;
1385 /* check maximal depth */
1386 if (parent && parent->parent && parent->parent->level < 2) {
1387 printk(KERN_ERR "htb: tree is too deep\n");
1388 goto failure;
1390 err = -ENOBUFS;
1391 if ((cl = kzalloc(sizeof(*cl), GFP_KERNEL)) == NULL)
1392 goto failure;
1394 gen_new_estimator(&cl->bstats, &cl->rate_est,
1395 &sch->dev->queue_lock,
1396 tca[TCA_RATE] ? : &est.nla);
1397 cl->refcnt = 1;
1398 INIT_LIST_HEAD(&cl->sibling);
1399 INIT_HLIST_NODE(&cl->hlist);
1400 INIT_LIST_HEAD(&cl->children);
1401 INIT_LIST_HEAD(&cl->un.leaf.drop_list);
1402 RB_CLEAR_NODE(&cl->pq_node);
1404 for (prio = 0; prio < TC_HTB_NUMPRIO; prio++)
1405 RB_CLEAR_NODE(&cl->node[prio]);
1407 /* create leaf qdisc early because it uses kmalloc(GFP_KERNEL)
1408 so that can't be used inside of sch_tree_lock
1409 -- thanks to Karlis Peisenieks */
1410 new_q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops, classid);
1411 sch_tree_lock(sch);
1412 if (parent && !parent->level) {
1413 unsigned int qlen = parent->un.leaf.q->q.qlen;
1415 /* turn parent into inner node */
1416 qdisc_reset(parent->un.leaf.q);
1417 qdisc_tree_decrease_qlen(parent->un.leaf.q, qlen);
1418 qdisc_destroy(parent->un.leaf.q);
1419 if (parent->prio_activity)
1420 htb_deactivate(q, parent);
1422 /* remove from evt list because of level change */
1423 if (parent->cmode != HTB_CAN_SEND) {
1424 htb_safe_rb_erase(&parent->pq_node, q->wait_pq);
1425 parent->cmode = HTB_CAN_SEND;
1427 parent->level = (parent->parent ? parent->parent->level
1428 : TC_HTB_MAXDEPTH) - 1;
1429 memset(&parent->un.inner, 0, sizeof(parent->un.inner));
1431 /* leaf (we) needs elementary qdisc */
1432 cl->un.leaf.q = new_q ? new_q : &noop_qdisc;
1434 cl->classid = classid;
1435 cl->parent = parent;
1437 /* set class to be in HTB_CAN_SEND state */
1438 cl->tokens = hopt->buffer;
1439 cl->ctokens = hopt->cbuffer;
1440 cl->mbuffer = 60 * PSCHED_TICKS_PER_SEC; /* 1min */
1441 cl->t_c = psched_get_time();
1442 cl->cmode = HTB_CAN_SEND;
1444 /* attach to the hash list and parent's family */
1445 hlist_add_head(&cl->hlist, q->hash + htb_hash(classid));
1446 list_add_tail(&cl->sibling,
1447 parent ? &parent->children : &q->root);
1448 } else {
1449 if (tca[TCA_RATE])
1450 gen_replace_estimator(&cl->bstats, &cl->rate_est,
1451 &sch->dev->queue_lock,
1452 tca[TCA_RATE]);
1453 sch_tree_lock(sch);
1456 /* it used to be a nasty bug here, we have to check that node
1457 is really leaf before changing cl->un.leaf ! */
1458 if (!cl->level) {
1459 cl->un.leaf.quantum = rtab->rate.rate / q->rate2quantum;
1460 if (!hopt->quantum && cl->un.leaf.quantum < 1000) {
1461 printk(KERN_WARNING
1462 "HTB: quantum of class %X is small. Consider r2q change.\n",
1463 cl->classid);
1464 cl->un.leaf.quantum = 1000;
1466 if (!hopt->quantum && cl->un.leaf.quantum > 200000) {
1467 printk(KERN_WARNING
1468 "HTB: quantum of class %X is big. Consider r2q change.\n",
1469 cl->classid);
1470 cl->un.leaf.quantum = 200000;
1472 if (hopt->quantum)
1473 cl->un.leaf.quantum = hopt->quantum;
1474 if ((cl->un.leaf.prio = hopt->prio) >= TC_HTB_NUMPRIO)
1475 cl->un.leaf.prio = TC_HTB_NUMPRIO - 1;
1477 /* backup for htb_parent_to_leaf */
1478 cl->quantum = cl->un.leaf.quantum;
1479 cl->prio = cl->un.leaf.prio;
1482 cl->buffer = hopt->buffer;
1483 cl->cbuffer = hopt->cbuffer;
1484 if (cl->rate)
1485 qdisc_put_rtab(cl->rate);
1486 cl->rate = rtab;
1487 if (cl->ceil)
1488 qdisc_put_rtab(cl->ceil);
1489 cl->ceil = ctab;
1490 sch_tree_unlock(sch);
1492 *arg = (unsigned long)cl;
1493 return 0;
1495 failure:
1496 if (rtab)
1497 qdisc_put_rtab(rtab);
1498 if (ctab)
1499 qdisc_put_rtab(ctab);
1500 return err;
1503 static struct tcf_proto **htb_find_tcf(struct Qdisc *sch, unsigned long arg)
1505 struct htb_sched *q = qdisc_priv(sch);
1506 struct htb_class *cl = (struct htb_class *)arg;
1507 struct tcf_proto **fl = cl ? &cl->filter_list : &q->filter_list;
1509 return fl;
1512 static unsigned long htb_bind_filter(struct Qdisc *sch, unsigned long parent,
1513 u32 classid)
1515 struct htb_sched *q = qdisc_priv(sch);
1516 struct htb_class *cl = htb_find(classid, sch);
1518 /*if (cl && !cl->level) return 0;
1519 The line above used to be there to prevent attaching filters to
1520 leaves. But at least tc_index filter uses this just to get class
1521 for other reasons so that we have to allow for it.
1522 ----
1523 19.6.2002 As Werner explained it is ok - bind filter is just
1524 another way to "lock" the class - unlike "get" this lock can
1525 be broken by class during destroy IIUC.
1527 if (cl)
1528 cl->filter_cnt++;
1529 else
1530 q->filter_cnt++;
1531 return (unsigned long)cl;
1534 static void htb_unbind_filter(struct Qdisc *sch, unsigned long arg)
1536 struct htb_sched *q = qdisc_priv(sch);
1537 struct htb_class *cl = (struct htb_class *)arg;
1539 if (cl)
1540 cl->filter_cnt--;
1541 else
1542 q->filter_cnt--;
1545 static void htb_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1547 struct htb_sched *q = qdisc_priv(sch);
1548 int i;
1550 if (arg->stop)
1551 return;
1553 for (i = 0; i < HTB_HSIZE; i++) {
1554 struct hlist_node *p;
1555 struct htb_class *cl;
1557 hlist_for_each_entry(cl, p, q->hash + i, hlist) {
1558 if (arg->count < arg->skip) {
1559 arg->count++;
1560 continue;
1562 if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
1563 arg->stop = 1;
1564 return;
1566 arg->count++;
1571 static const struct Qdisc_class_ops htb_class_ops = {
1572 .graft = htb_graft,
1573 .leaf = htb_leaf,
1574 .qlen_notify = htb_qlen_notify,
1575 .get = htb_get,
1576 .put = htb_put,
1577 .change = htb_change_class,
1578 .delete = htb_delete,
1579 .walk = htb_walk,
1580 .tcf_chain = htb_find_tcf,
1581 .bind_tcf = htb_bind_filter,
1582 .unbind_tcf = htb_unbind_filter,
1583 .dump = htb_dump_class,
1584 .dump_stats = htb_dump_class_stats,
1587 static struct Qdisc_ops htb_qdisc_ops __read_mostly = {
1588 .next = NULL,
1589 .cl_ops = &htb_class_ops,
1590 .id = "htb",
1591 .priv_size = sizeof(struct htb_sched),
1592 .enqueue = htb_enqueue,
1593 .dequeue = htb_dequeue,
1594 .requeue = htb_requeue,
1595 .drop = htb_drop,
1596 .init = htb_init,
1597 .reset = htb_reset,
1598 .destroy = htb_destroy,
1599 .change = NULL /* htb_change */,
1600 .dump = htb_dump,
1601 .owner = THIS_MODULE,
1604 static int __init htb_module_init(void)
1606 return register_qdisc(&htb_qdisc_ops);
1608 static void __exit htb_module_exit(void)
1610 unregister_qdisc(&htb_qdisc_ops);
1613 module_init(htb_module_init)
1614 module_exit(htb_module_exit)
1615 MODULE_LICENSE("GPL");