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