blob: d8c1a6b0def1a8e1b91a5cf5f97f75a6ae7661cf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define HTB_RATECM 1 /* whether to use rate computer */
74#define HTB_HYSTERESIS 1/* whether to use mode hysteresis for speedup */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/* 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{
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 /* 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 */
Stephen Hemmingerb3a62512006-07-14 16:32:27 -0700148 psched_tdiff_t mbuffer; /* max wait time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
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
Jamal Hadi Salim29f1df62006-01-08 22:35:55 -0800271 *qerr = NET_XMIT_BYPASS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305/**
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 */
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700311static void htb_add_to_id_tree (struct rb_root *root,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 struct htb_class *cl,int prio)
313{
314 struct rb_node **p = &root->rb_node, *parent = NULL;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 while (*p) {
317 struct htb_class *c; parent = *p;
318 c = rb_entry(parent, struct htb_class, node[prio]);
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 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,
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700337 struct htb_class *cl,long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 struct rb_node **p = &q->wait_pq[cl->level].rb_node, *parent = NULL;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 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{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 q->row_mask[cl->level] |= mask;
382 while (mask) {
383 int prio = ffz(~mask);
384 mask &= ~(1 << prio);
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700385 htb_add_to_id_tree(q->row[cl->level]+prio,cl,prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
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;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 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);
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700405 rb_erase(cl->node + prio,q->row[cl->level]+prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (!q->row[cl->level][prio].rb_node)
407 m |= 1 << prio;
408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 while (cl->cmode == HTB_MAY_BORROW && p && mask) {
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 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
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700435 htb_add_to_id_tree(p->un.inner.feed+prio,cl,prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 p->prio_activity |= mask;
438 cl = p; p = cl->parent;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
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;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
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
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700472 rb_erase(cl->node + prio,p->un.inner.feed + prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 if (!p->un.inner.feed[prio].rb_node)
475 mask |= 1 << prio;
476 }
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 p->prio_activity &= ~mask;
479 cl = p; p = cl->parent;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482 if (cl->cmode == HTB_CAN_SEND && mask)
483 htb_remove_class_from_row(q,cl,mask);
484}
485
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700486#if HTB_HYSTERESIS
487static inline long htb_lowater(const struct htb_class *cl)
488{
489 return cl->cmode != HTB_CANT_SEND ? -cl->cbuffer : 0;
490}
491static inline long htb_hiwater(const struct htb_class *cl)
492{
493 return cl->cmode == HTB_CAN_SEND ? -cl->buffer : 0;
494}
495#else
496#define htb_lowater(cl) (0)
497#define htb_hiwater(cl) (0)
498#endif
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500/**
501 * htb_class_mode - computes and returns current class mode
502 *
503 * It computes cl's mode at time cl->t_c+diff and returns it. If mode
504 * is not HTB_CAN_SEND then cl->pq_key is updated to time difference
505 * from now to time when cl will change its state.
506 * Also it is worth to note that class mode doesn't change simply
507 * at cl->{c,}tokens == 0 but there can rather be hysteresis of
508 * 0 .. -cl->{c,}buffer range. It is meant to limit number of
509 * mode transitions per time unit. The speed gain is about 1/6.
510 */
511static __inline__ enum htb_cmode
512htb_class_mode(struct htb_class *cl,long *diff)
513{
514 long toks;
515
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700516 if ((toks = (cl->ctokens + *diff)) < htb_lowater(cl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 *diff = -toks;
518 return HTB_CANT_SEND;
519 }
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700520
521 if ((toks = (cl->tokens + *diff)) >= htb_hiwater(cl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return HTB_CAN_SEND;
523
524 *diff = -toks;
525 return HTB_MAY_BORROW;
526}
527
528/**
529 * htb_change_class_mode - changes classe's mode
530 *
531 * This should be the only way how to change classe's mode under normal
532 * cirsumstances. Routine will update feed lists linkage, change mode
533 * and add class to the wait event queue if appropriate. New mode should
534 * be different from old one and cl->pq_key has to be valid if changing
535 * to mode other than HTB_CAN_SEND (see htb_add_to_wait_tree).
536 */
537static void
538htb_change_class_mode(struct htb_sched *q, struct htb_class *cl, long *diff)
539{
540 enum htb_cmode new_mode = htb_class_mode(cl,diff);
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 if (new_mode == cl->cmode)
544 return;
545
546 if (cl->prio_activity) { /* not necessary: speed optimization */
547 if (cl->cmode != HTB_CANT_SEND)
548 htb_deactivate_prios(q,cl);
549 cl->cmode = new_mode;
550 if (new_mode != HTB_CANT_SEND)
551 htb_activate_prios(q,cl);
552 } else
553 cl->cmode = new_mode;
554}
555
556/**
557 * htb_activate - inserts leaf cl into appropriate active feeds
558 *
559 * Routine learns (new) priority of leaf and activates feed chain
560 * for the prio. It can be called on already active leaf safely.
561 * It also adds leaf into droplist.
562 */
563static __inline__ void htb_activate(struct htb_sched *q,struct htb_class *cl)
564{
565 BUG_TRAP(!cl->level && cl->un.leaf.q && cl->un.leaf.q->q.qlen);
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (!cl->prio_activity) {
568 cl->prio_activity = 1 << (cl->un.leaf.aprio = cl->un.leaf.prio);
569 htb_activate_prios(q,cl);
570 list_add_tail(&cl->un.leaf.drop_list,q->drops+cl->un.leaf.aprio);
571 }
572}
573
574/**
575 * htb_deactivate - remove leaf cl from active feeds
576 *
577 * Make sure that leaf is active. In the other words it can't be called
578 * with non-active leaf. It also removes class from the drop list.
579 */
580static __inline__ void
581htb_deactivate(struct htb_sched *q,struct htb_class *cl)
582{
583 BUG_TRAP(cl->prio_activity);
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 htb_deactivate_prios(q,cl);
586 cl->prio_activity = 0;
587 list_del_init(&cl->un.leaf.drop_list);
588}
589
590static int htb_enqueue(struct sk_buff *skb, struct Qdisc *sch)
591{
592 int ret;
593 struct htb_sched *q = qdisc_priv(sch);
594 struct htb_class *cl = htb_classify(skb,sch,&ret);
595
596 if (cl == HTB_DIRECT) {
597 /* enqueue to helper queue */
598 if (q->direct_queue.qlen < q->direct_qlen) {
599 __skb_queue_tail(&q->direct_queue, skb);
600 q->direct_pkts++;
Asim Shankar033d8992005-05-03 14:39:33 -0700601 } else {
602 kfree_skb(skb);
603 sch->qstats.drops++;
604 return NET_XMIT_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606#ifdef CONFIG_NET_CLS_ACT
607 } else if (!cl) {
Jamal Hadi Salim29f1df62006-01-08 22:35:55 -0800608 if (ret == NET_XMIT_BYPASS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 sch->qstats.drops++;
610 kfree_skb (skb);
611 return ret;
612#endif
613 } else if (cl->un.leaf.q->enqueue(skb, cl->un.leaf.q) != NET_XMIT_SUCCESS) {
614 sch->qstats.drops++;
615 cl->qstats.drops++;
616 return NET_XMIT_DROP;
617 } else {
618 cl->bstats.packets++; cl->bstats.bytes += skb->len;
619 htb_activate (q,cl);
620 }
621
622 sch->q.qlen++;
623 sch->bstats.packets++; sch->bstats.bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return NET_XMIT_SUCCESS;
625}
626
627/* TODO: requeuing packet charges it to policers again !! */
628static int htb_requeue(struct sk_buff *skb, struct Qdisc *sch)
629{
630 struct htb_sched *q = qdisc_priv(sch);
631 int ret = NET_XMIT_SUCCESS;
632 struct htb_class *cl = htb_classify(skb,sch, &ret);
633 struct sk_buff *tskb;
634
635 if (cl == HTB_DIRECT || !cl) {
636 /* enqueue to helper queue */
637 if (q->direct_queue.qlen < q->direct_qlen && cl) {
638 __skb_queue_head(&q->direct_queue, skb);
639 } else {
640 __skb_queue_head(&q->direct_queue, skb);
641 tskb = __skb_dequeue_tail(&q->direct_queue);
642 kfree_skb (tskb);
643 sch->qstats.drops++;
644 return NET_XMIT_CN;
645 }
646 } else if (cl->un.leaf.q->ops->requeue(skb, cl->un.leaf.q) != NET_XMIT_SUCCESS) {
647 sch->qstats.drops++;
648 cl->qstats.drops++;
649 return NET_XMIT_DROP;
650 } else
651 htb_activate (q,cl);
652
653 sch->q.qlen++;
654 sch->qstats.requeues++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return NET_XMIT_SUCCESS;
656}
657
658static void htb_timer(unsigned long arg)
659{
660 struct Qdisc *sch = (struct Qdisc*)arg;
661 sch->flags &= ~TCQ_F_THROTTLED;
662 wmb();
663 netif_schedule(sch->dev);
664}
665
666#ifdef HTB_RATECM
667#define RT_GEN(D,R) R+=D-(R/HTB_EWMAC);D=0
668static void htb_rate_timer(unsigned long arg)
669{
670 struct Qdisc *sch = (struct Qdisc*)arg;
671 struct htb_sched *q = qdisc_priv(sch);
672 struct list_head *p;
673
674 /* lock queue so that we can muck with it */
Stephen Hemminger9ac961e2006-08-10 23:33:16 -0700675 spin_lock_bh(&sch->dev->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 q->rttim.expires = jiffies + HZ;
678 add_timer(&q->rttim);
679
680 /* scan and recompute one bucket at time */
681 if (++q->recmp_bucket >= HTB_HSIZE)
682 q->recmp_bucket = 0;
683 list_for_each (p,q->hash+q->recmp_bucket) {
684 struct htb_class *cl = list_entry(p,struct htb_class,hlist);
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 RT_GEN (cl->sum_bytes,cl->rate_bytes);
687 RT_GEN (cl->sum_packets,cl->rate_packets);
688 }
Stephen Hemminger9ac961e2006-08-10 23:33:16 -0700689 spin_unlock_bh(&sch->dev->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691#endif
692
693/**
694 * htb_charge_class - charges amount "bytes" to leaf and ancestors
695 *
696 * Routine assumes that packet "bytes" long was dequeued from leaf cl
697 * borrowing from "level". It accounts bytes to ceil leaky bucket for
698 * leaf and all ancestors and to rate bucket for ancestors at levels
699 * "level" and higher. It also handles possible change of mode resulting
700 * from the update. Note that mode can also increase here (MAY_BORROW to
701 * CAN_SEND) because we can use more precise clock that event queue here.
702 * In such case we remove class from event queue first.
703 */
704static void htb_charge_class(struct htb_sched *q,struct htb_class *cl,
705 int level,int bytes)
706{
707 long toks,diff;
708 enum htb_cmode old_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710#define HTB_ACCNT(T,B,R) toks = diff + cl->T; \
711 if (toks > cl->B) toks = cl->B; \
712 toks -= L2T(cl, cl->R, bytes); \
713 if (toks <= -cl->mbuffer) toks = 1-cl->mbuffer; \
714 cl->T = toks
715
716 while (cl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 diff = PSCHED_TDIFF_SAFE(q->now, cl->t_c, (u32)cl->mbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (cl->level >= level) {
719 if (cl->level == level) cl->xstats.lends++;
720 HTB_ACCNT (tokens,buffer,rate);
721 } else {
722 cl->xstats.borrows++;
723 cl->tokens += diff; /* we moved t_c; update tokens */
724 }
725 HTB_ACCNT (ctokens,cbuffer,ceil);
726 cl->t_c = q->now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 old_mode = cl->cmode; diff = 0;
729 htb_change_class_mode(q,cl,&diff);
730 if (old_mode != cl->cmode) {
731 if (old_mode != HTB_CAN_SEND)
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700732 rb_erase(&cl->pq_node,q->wait_pq+cl->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (cl->cmode != HTB_CAN_SEND)
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700734 htb_add_to_wait_tree (q,cl,diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736
737#ifdef HTB_RATECM
738 /* update rate counters */
739 cl->sum_bytes += bytes; cl->sum_packets++;
740#endif
741
742 /* update byte stats except for leaves which are already updated */
743 if (cl->level) {
744 cl->bstats.bytes += bytes;
745 cl->bstats.packets++;
746 }
747 cl = cl->parent;
748 }
749}
750
751/**
752 * htb_do_events - make mode changes to classes at the level
753 *
754 * Scans event queue for pending events and applies them. Returns jiffies to
755 * next pending event (0 for no event in pq).
756 * Note: Aplied are events whose have cl->pq_key <= jiffies.
757 */
758static long htb_do_events(struct htb_sched *q,int level)
759{
760 int i;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 for (i = 0; i < 500; i++) {
763 struct htb_class *cl;
764 long diff;
765 struct rb_node *p = q->wait_pq[level].rb_node;
766 if (!p) return 0;
767 while (p->rb_left) p = p->rb_left;
768
769 cl = rb_entry(p, struct htb_class, pq_node);
770 if (time_after(cl->pq_key, q->jiffies)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return cl->pq_key - q->jiffies;
772 }
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700773 rb_erase(p,q->wait_pq+level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 diff = PSCHED_TDIFF_SAFE(q->now, cl->t_c, (u32)cl->mbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 htb_change_class_mode(q,cl,&diff);
776 if (cl->cmode != HTB_CAN_SEND)
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700777 htb_add_to_wait_tree (q,cl,diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779 if (net_ratelimit())
780 printk(KERN_WARNING "htb: too many events !\n");
781 return HZ/10;
782}
783
784/* Returns class->node+prio from id-tree where classe's id is >= id. NULL
785 is no such one exists. */
786static struct rb_node *
787htb_id_find_next_upper(int prio,struct rb_node *n,u32 id)
788{
789 struct rb_node *r = NULL;
790 while (n) {
791 struct htb_class *cl = rb_entry(n,struct htb_class,node[prio]);
792 if (id == cl->classid) return n;
793
794 if (id > cl->classid) {
795 n = n->rb_right;
796 } else {
797 r = n;
798 n = n->rb_left;
799 }
800 }
801 return r;
802}
803
804/**
805 * htb_lookup_leaf - returns next leaf class in DRR order
806 *
807 * Find leaf where current feed pointers points to.
808 */
809static struct htb_class *
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700810htb_lookup_leaf(struct rb_root *tree,int prio,struct rb_node **pptr,u32 *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 int i;
813 struct {
814 struct rb_node *root;
815 struct rb_node **pptr;
816 u32 *pid;
817 } stk[TC_HTB_MAXDEPTH],*sp = stk;
818
819 BUG_TRAP(tree->rb_node);
820 sp->root = tree->rb_node;
821 sp->pptr = pptr;
822 sp->pid = pid;
823
824 for (i = 0; i < 65535; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (!*sp->pptr && *sp->pid) {
826 /* ptr was invalidated but id is valid - try to recover
827 the original or next ptr */
828 *sp->pptr = htb_id_find_next_upper(prio,sp->root,*sp->pid);
829 }
830 *sp->pid = 0; /* ptr is valid now so that remove this hint as it
831 can become out of date quickly */
832 if (!*sp->pptr) { /* we are at right end; rewind & go up */
833 *sp->pptr = sp->root;
834 while ((*sp->pptr)->rb_left)
835 *sp->pptr = (*sp->pptr)->rb_left;
836 if (sp > stk) {
837 sp--;
838 BUG_TRAP(*sp->pptr); if(!*sp->pptr) return NULL;
839 htb_next_rb_node (sp->pptr);
840 }
841 } else {
842 struct htb_class *cl;
843 cl = rb_entry(*sp->pptr,struct htb_class,node[prio]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (!cl->level)
845 return cl;
846 (++sp)->root = cl->un.inner.feed[prio].rb_node;
847 sp->pptr = cl->un.inner.ptr+prio;
848 sp->pid = cl->un.inner.last_ptr_id+prio;
849 }
850 }
851 BUG_TRAP(0);
852 return NULL;
853}
854
855/* dequeues packet at given priority and level; call only if
856 you are sure that there is active class at prio/level */
857static struct sk_buff *
858htb_dequeue_tree(struct htb_sched *q,int prio,int level)
859{
860 struct sk_buff *skb = NULL;
861 struct htb_class *cl,*start;
862 /* look initial class up in the row */
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700863 start = cl = htb_lookup_leaf (q->row[level]+prio,prio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 q->ptr[level]+prio,q->last_ptr_id[level]+prio);
865
866 do {
867next:
868 BUG_TRAP(cl);
869 if (!cl) return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 /* class can be empty - it is unlikely but can be true if leaf
872 qdisc drops packets in enqueue routine or if someone used
873 graft operation on the leaf since last dequeue;
874 simply deactivate and skip such class */
875 if (unlikely(cl->un.leaf.q->q.qlen == 0)) {
876 struct htb_class *next;
877 htb_deactivate(q,cl);
878
879 /* row/level might become empty */
880 if ((q->row_mask[level] & (1 << prio)) == 0)
881 return NULL;
882
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700883 next = htb_lookup_leaf (q->row[level]+prio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 prio,q->ptr[level]+prio,q->last_ptr_id[level]+prio);
885
886 if (cl == start) /* fix start if we just deleted it */
887 start = next;
888 cl = next;
889 goto next;
890 }
891
892 if (likely((skb = cl->un.leaf.q->dequeue(cl->un.leaf.q)) != NULL))
893 break;
894 if (!cl->warned) {
895 printk(KERN_WARNING "htb: class %X isn't work conserving ?!\n",cl->classid);
896 cl->warned = 1;
897 }
898 q->nwc_hit++;
899 htb_next_rb_node((level?cl->parent->un.inner.ptr:q->ptr[0])+prio);
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700900 cl = htb_lookup_leaf (q->row[level]+prio,prio,q->ptr[level]+prio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 q->last_ptr_id[level]+prio);
902
903 } while (cl != start);
904
905 if (likely(skb != NULL)) {
906 if ((cl->un.leaf.deficit[level] -= skb->len) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 cl->un.leaf.deficit[level] += cl->un.leaf.quantum;
908 htb_next_rb_node((level?cl->parent->un.inner.ptr:q->ptr[0])+prio);
909 }
910 /* this used to be after charge_class but this constelation
911 gives us slightly better performance */
912 if (!cl->un.leaf.q->q.qlen)
913 htb_deactivate (q,cl);
914 htb_charge_class (q,cl,level,skb->len);
915 }
916 return skb;
917}
918
919static void htb_delay_by(struct Qdisc *sch,long delay)
920{
921 struct htb_sched *q = qdisc_priv(sch);
922 if (delay <= 0) delay = 1;
923 if (unlikely(delay > 5*HZ)) {
924 if (net_ratelimit())
925 printk(KERN_INFO "HTB delay %ld > 5sec\n", delay);
926 delay = 5*HZ;
927 }
928 /* why don't use jiffies here ? because expires can be in past */
929 mod_timer(&q->timer, q->jiffies + delay);
930 sch->flags |= TCQ_F_THROTTLED;
931 sch->qstats.overlimits++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
934static struct sk_buff *htb_dequeue(struct Qdisc *sch)
935{
936 struct sk_buff *skb = NULL;
937 struct htb_sched *q = qdisc_priv(sch);
938 int level;
939 long min_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 q->jiffies = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 /* try to dequeue direct packets as high prio (!) to minimize cpu work */
944 if ((skb = __skb_dequeue(&q->direct_queue)) != NULL) {
945 sch->flags &= ~TCQ_F_THROTTLED;
946 sch->q.qlen--;
947 return skb;
948 }
949
950 if (!sch->q.qlen) goto fin;
951 PSCHED_GET_TIME(q->now);
952
953 min_delay = LONG_MAX;
954 q->nwc_hit = 0;
955 for (level = 0; level < TC_HTB_MAXDEPTH; level++) {
956 /* common case optimization - skip event handler quickly */
957 int m;
958 long delay;
959 if (time_after_eq(q->jiffies, q->near_ev_cache[level])) {
960 delay = htb_do_events(q,level);
961 q->near_ev_cache[level] = q->jiffies + (delay ? delay : HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 } else
963 delay = q->near_ev_cache[level] - q->jiffies;
964
965 if (delay && min_delay > delay)
966 min_delay = delay;
967 m = ~q->row_mask[level];
968 while (m != (int)(-1)) {
969 int prio = ffz (m);
970 m |= 1 << prio;
971 skb = htb_dequeue_tree(q,prio,level);
972 if (likely(skb != NULL)) {
973 sch->q.qlen--;
974 sch->flags &= ~TCQ_F_THROTTLED;
975 goto fin;
976 }
977 }
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 htb_delay_by (sch,min_delay > 5*HZ ? 5*HZ : min_delay);
980fin:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return skb;
982}
983
984/* try to drop from each class (by prio) until one succeed */
985static unsigned int htb_drop(struct Qdisc* sch)
986{
987 struct htb_sched *q = qdisc_priv(sch);
988 int prio;
989
990 for (prio = TC_HTB_NUMPRIO - 1; prio >= 0; prio--) {
991 struct list_head *p;
992 list_for_each (p,q->drops+prio) {
993 struct htb_class *cl = list_entry(p, struct htb_class,
994 un.leaf.drop_list);
995 unsigned int len;
996 if (cl->un.leaf.q->ops->drop &&
997 (len = cl->un.leaf.q->ops->drop(cl->un.leaf.q))) {
998 sch->q.qlen--;
999 if (!cl->un.leaf.q->q.qlen)
1000 htb_deactivate (q,cl);
1001 return len;
1002 }
1003 }
1004 }
1005 return 0;
1006}
1007
1008/* reset all classes */
1009/* always caled under BH & queue lock */
1010static void htb_reset(struct Qdisc* sch)
1011{
1012 struct htb_sched *q = qdisc_priv(sch);
1013 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 for (i = 0; i < HTB_HSIZE; i++) {
1016 struct list_head *p;
1017 list_for_each (p,q->hash+i) {
1018 struct htb_class *cl = list_entry(p,struct htb_class,hlist);
1019 if (cl->level)
1020 memset(&cl->un.inner,0,sizeof(cl->un.inner));
1021 else {
1022 if (cl->un.leaf.q)
1023 qdisc_reset(cl->un.leaf.q);
1024 INIT_LIST_HEAD(&cl->un.leaf.drop_list);
1025 }
1026 cl->prio_activity = 0;
1027 cl->cmode = HTB_CAN_SEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 }
1030 }
1031 sch->flags &= ~TCQ_F_THROTTLED;
1032 del_timer(&q->timer);
1033 __skb_queue_purge(&q->direct_queue);
1034 sch->q.qlen = 0;
1035 memset(q->row,0,sizeof(q->row));
1036 memset(q->row_mask,0,sizeof(q->row_mask));
1037 memset(q->wait_pq,0,sizeof(q->wait_pq));
1038 memset(q->ptr,0,sizeof(q->ptr));
1039 for (i = 0; i < TC_HTB_NUMPRIO; i++)
1040 INIT_LIST_HEAD(q->drops+i);
1041}
1042
1043static int htb_init(struct Qdisc *sch, struct rtattr *opt)
1044{
1045 struct htb_sched *q = qdisc_priv(sch);
1046 struct rtattr *tb[TCA_HTB_INIT];
1047 struct tc_htb_glob *gopt;
1048 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (!opt || rtattr_parse_nested(tb, TCA_HTB_INIT, opt) ||
1050 tb[TCA_HTB_INIT-1] == NULL ||
1051 RTA_PAYLOAD(tb[TCA_HTB_INIT-1]) < sizeof(*gopt)) {
1052 printk(KERN_ERR "HTB: hey probably you have bad tc tool ?\n");
1053 return -EINVAL;
1054 }
1055 gopt = RTA_DATA(tb[TCA_HTB_INIT-1]);
1056 if (gopt->version != HTB_VER >> 16) {
1057 printk(KERN_ERR "HTB: need tc/htb version %d (minor is %d), you have %d\n",
1058 HTB_VER >> 16,HTB_VER & 0xffff,gopt->version);
1059 return -EINVAL;
1060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 INIT_LIST_HEAD(&q->root);
1063 for (i = 0; i < HTB_HSIZE; i++)
1064 INIT_LIST_HEAD(q->hash+i);
1065 for (i = 0; i < TC_HTB_NUMPRIO; i++)
1066 INIT_LIST_HEAD(q->drops+i);
1067
1068 init_timer(&q->timer);
1069 skb_queue_head_init(&q->direct_queue);
1070
1071 q->direct_qlen = sch->dev->tx_queue_len;
1072 if (q->direct_qlen < 2) /* some devices have zero tx_queue_len */
1073 q->direct_qlen = 2;
1074 q->timer.function = htb_timer;
1075 q->timer.data = (unsigned long)sch;
1076
1077#ifdef HTB_RATECM
1078 init_timer(&q->rttim);
1079 q->rttim.function = htb_rate_timer;
1080 q->rttim.data = (unsigned long)sch;
1081 q->rttim.expires = jiffies + HZ;
1082 add_timer(&q->rttim);
1083#endif
1084 if ((q->rate2quantum = gopt->rate2quantum) < 1)
1085 q->rate2quantum = 1;
1086 q->defcls = gopt->defcls;
1087
1088 return 0;
1089}
1090
1091static int htb_dump(struct Qdisc *sch, struct sk_buff *skb)
1092{
1093 struct htb_sched *q = qdisc_priv(sch);
1094 unsigned char *b = skb->tail;
1095 struct rtattr *rta;
1096 struct tc_htb_glob gopt;
Stephen Hemminger9ac961e2006-08-10 23:33:16 -07001097 spin_lock_bh(&sch->dev->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 gopt.direct_pkts = q->direct_pkts;
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 gopt.version = HTB_VER;
1101 gopt.rate2quantum = q->rate2quantum;
1102 gopt.defcls = q->defcls;
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001103 gopt.debug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 rta = (struct rtattr*)b;
1105 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
1106 RTA_PUT(skb, TCA_HTB_INIT, sizeof(gopt), &gopt);
1107 rta->rta_len = skb->tail - b;
Stephen Hemminger9ac961e2006-08-10 23:33:16 -07001108 spin_unlock_bh(&sch->dev->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return skb->len;
1110rtattr_failure:
Stephen Hemminger9ac961e2006-08-10 23:33:16 -07001111 spin_unlock_bh(&sch->dev->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 skb_trim(skb, skb->tail - skb->data);
1113 return -1;
1114}
1115
1116static int htb_dump_class(struct Qdisc *sch, unsigned long arg,
1117 struct sk_buff *skb, struct tcmsg *tcm)
1118{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 struct htb_class *cl = (struct htb_class*)arg;
1120 unsigned char *b = skb->tail;
1121 struct rtattr *rta;
1122 struct tc_htb_opt opt;
1123
Stephen Hemminger9ac961e2006-08-10 23:33:16 -07001124 spin_lock_bh(&sch->dev->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 tcm->tcm_parent = cl->parent ? cl->parent->classid : TC_H_ROOT;
1126 tcm->tcm_handle = cl->classid;
1127 if (!cl->level && cl->un.leaf.q)
1128 tcm->tcm_info = cl->un.leaf.q->handle;
1129
1130 rta = (struct rtattr*)b;
1131 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
1132
1133 memset (&opt,0,sizeof(opt));
1134
1135 opt.rate = cl->rate->rate; opt.buffer = cl->buffer;
1136 opt.ceil = cl->ceil->rate; opt.cbuffer = cl->cbuffer;
1137 opt.quantum = cl->un.leaf.quantum; opt.prio = cl->un.leaf.prio;
1138 opt.level = cl->level;
1139 RTA_PUT(skb, TCA_HTB_PARMS, sizeof(opt), &opt);
1140 rta->rta_len = skb->tail - b;
Stephen Hemminger9ac961e2006-08-10 23:33:16 -07001141 spin_unlock_bh(&sch->dev->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return skb->len;
1143rtattr_failure:
Stephen Hemminger9ac961e2006-08-10 23:33:16 -07001144 spin_unlock_bh(&sch->dev->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 skb_trim(skb, b - skb->data);
1146 return -1;
1147}
1148
1149static int
1150htb_dump_class_stats(struct Qdisc *sch, unsigned long arg,
1151 struct gnet_dump *d)
1152{
1153 struct htb_class *cl = (struct htb_class*)arg;
1154
1155#ifdef HTB_RATECM
1156 cl->rate_est.bps = cl->rate_bytes/(HTB_EWMAC*HTB_HSIZE);
1157 cl->rate_est.pps = cl->rate_packets/(HTB_EWMAC*HTB_HSIZE);
1158#endif
1159
1160 if (!cl->level && cl->un.leaf.q)
1161 cl->qstats.qlen = cl->un.leaf.q->q.qlen;
1162 cl->xstats.tokens = cl->tokens;
1163 cl->xstats.ctokens = cl->ctokens;
1164
1165 if (gnet_stats_copy_basic(d, &cl->bstats) < 0 ||
1166 gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
1167 gnet_stats_copy_queue(d, &cl->qstats) < 0)
1168 return -1;
1169
1170 return gnet_stats_copy_app(d, &cl->xstats, sizeof(cl->xstats));
1171}
1172
1173static int htb_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
1174 struct Qdisc **old)
1175{
1176 struct htb_class *cl = (struct htb_class*)arg;
1177
1178 if (cl && !cl->level) {
1179 if (new == NULL && (new = qdisc_create_dflt(sch->dev,
1180 &pfifo_qdisc_ops)) == NULL)
1181 return -ENOBUFS;
1182 sch_tree_lock(sch);
1183 if ((*old = xchg(&cl->un.leaf.q, new)) != NULL) {
1184 if (cl->prio_activity)
1185 htb_deactivate (qdisc_priv(sch),cl);
1186
1187 /* TODO: is it correct ? Why CBQ doesn't do it ? */
1188 sch->q.qlen -= (*old)->q.qlen;
1189 qdisc_reset(*old);
1190 }
1191 sch_tree_unlock(sch);
1192 return 0;
1193 }
1194 return -ENOENT;
1195}
1196
1197static struct Qdisc * htb_leaf(struct Qdisc *sch, unsigned long arg)
1198{
1199 struct htb_class *cl = (struct htb_class*)arg;
1200 return (cl && !cl->level) ? cl->un.leaf.q : NULL;
1201}
1202
1203static unsigned long htb_get(struct Qdisc *sch, u32 classid)
1204{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 struct htb_class *cl = htb_find(classid,sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (cl)
1207 cl->refcnt++;
1208 return (unsigned long)cl;
1209}
1210
1211static void htb_destroy_filters(struct tcf_proto **fl)
1212{
1213 struct tcf_proto *tp;
1214
1215 while ((tp = *fl) != NULL) {
1216 *fl = tp->next;
1217 tcf_destroy(tp);
1218 }
1219}
1220
1221static void htb_destroy_class(struct Qdisc* sch,struct htb_class *cl)
1222{
1223 struct htb_sched *q = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (!cl->level) {
1225 BUG_TRAP(cl->un.leaf.q);
1226 sch->q.qlen -= cl->un.leaf.q->q.qlen;
1227 qdisc_destroy(cl->un.leaf.q);
1228 }
1229 qdisc_put_rtab(cl->rate);
1230 qdisc_put_rtab(cl->ceil);
1231
1232 htb_destroy_filters (&cl->filter_list);
1233
1234 while (!list_empty(&cl->children))
1235 htb_destroy_class (sch,list_entry(cl->children.next,
1236 struct htb_class,sibling));
1237
1238 /* note: this delete may happen twice (see htb_delete) */
1239 list_del(&cl->hlist);
1240 list_del(&cl->sibling);
1241
1242 if (cl->prio_activity)
1243 htb_deactivate (q,cl);
1244
1245 if (cl->cmode != HTB_CAN_SEND)
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001246 rb_erase(&cl->pq_node,q->wait_pq+cl->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 kfree(cl);
1249}
1250
1251/* always caled under BH & queue lock */
1252static void htb_destroy(struct Qdisc* sch)
1253{
1254 struct htb_sched *q = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 del_timer_sync (&q->timer);
1257#ifdef HTB_RATECM
1258 del_timer_sync (&q->rttim);
1259#endif
1260 /* This line used to be after htb_destroy_class call below
1261 and surprisingly it worked in 2.4. But it must precede it
1262 because filter need its target class alive to be able to call
1263 unbind_filter on it (without Oops). */
1264 htb_destroy_filters(&q->filter_list);
1265
1266 while (!list_empty(&q->root))
1267 htb_destroy_class (sch,list_entry(q->root.next,
1268 struct htb_class,sibling));
1269
1270 __skb_queue_purge(&q->direct_queue);
1271}
1272
1273static int htb_delete(struct Qdisc *sch, unsigned long arg)
1274{
1275 struct htb_sched *q = qdisc_priv(sch);
1276 struct htb_class *cl = (struct htb_class*)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 // TODO: why don't allow to delete subtree ? references ? does
1279 // tc subsys quarantee us that in htb_destroy it holds no class
1280 // refs so that we can remove children safely there ?
1281 if (!list_empty(&cl->children) || cl->filter_cnt)
1282 return -EBUSY;
1283
1284 sch_tree_lock(sch);
1285
1286 /* delete from hash and active; remainder in destroy_class */
1287 list_del_init(&cl->hlist);
1288 if (cl->prio_activity)
1289 htb_deactivate (q,cl);
1290
1291 if (--cl->refcnt == 0)
1292 htb_destroy_class(sch,cl);
1293
1294 sch_tree_unlock(sch);
1295 return 0;
1296}
1297
1298static void htb_put(struct Qdisc *sch, unsigned long arg)
1299{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 struct htb_class *cl = (struct htb_class*)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 if (--cl->refcnt == 0)
1303 htb_destroy_class(sch,cl);
1304}
1305
1306static int htb_change_class(struct Qdisc *sch, u32 classid,
1307 u32 parentid, struct rtattr **tca, unsigned long *arg)
1308{
1309 int err = -EINVAL;
1310 struct htb_sched *q = qdisc_priv(sch);
1311 struct htb_class *cl = (struct htb_class*)*arg,*parent;
1312 struct rtattr *opt = tca[TCA_OPTIONS-1];
1313 struct qdisc_rate_table *rtab = NULL, *ctab = NULL;
1314 struct rtattr *tb[TCA_HTB_RTAB];
1315 struct tc_htb_opt *hopt;
1316
1317 /* extract all subattrs from opt attr */
1318 if (!opt || rtattr_parse_nested(tb, TCA_HTB_RTAB, opt) ||
1319 tb[TCA_HTB_PARMS-1] == NULL ||
1320 RTA_PAYLOAD(tb[TCA_HTB_PARMS-1]) < sizeof(*hopt))
1321 goto failure;
1322
1323 parent = parentid == TC_H_ROOT ? NULL : htb_find (parentid,sch);
1324
1325 hopt = RTA_DATA(tb[TCA_HTB_PARMS-1]);
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 rtab = qdisc_get_rtab(&hopt->rate, tb[TCA_HTB_RTAB-1]);
1328 ctab = qdisc_get_rtab(&hopt->ceil, tb[TCA_HTB_CTAB-1]);
1329 if (!rtab || !ctab) goto failure;
1330
1331 if (!cl) { /* new class */
1332 struct Qdisc *new_q;
1333 /* check for valid classid */
1334 if (!classid || TC_H_MAJ(classid^sch->handle) || htb_find(classid,sch))
1335 goto failure;
1336
1337 /* check maximal depth */
1338 if (parent && parent->parent && parent->parent->level < 2) {
1339 printk(KERN_ERR "htb: tree is too deep\n");
1340 goto failure;
1341 }
1342 err = -ENOBUFS;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001343 if ((cl = kzalloc(sizeof(*cl), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 goto failure;
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 cl->refcnt = 1;
1347 INIT_LIST_HEAD(&cl->sibling);
1348 INIT_LIST_HEAD(&cl->hlist);
1349 INIT_LIST_HEAD(&cl->children);
1350 INIT_LIST_HEAD(&cl->un.leaf.drop_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 /* create leaf qdisc early because it uses kmalloc(GFP_KERNEL)
1353 so that can't be used inside of sch_tree_lock
1354 -- thanks to Karlis Peisenieks */
1355 new_q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
1356 sch_tree_lock(sch);
1357 if (parent && !parent->level) {
1358 /* turn parent into inner node */
1359 sch->q.qlen -= parent->un.leaf.q->q.qlen;
1360 qdisc_destroy (parent->un.leaf.q);
1361 if (parent->prio_activity)
1362 htb_deactivate (q,parent);
1363
1364 /* remove from evt list because of level change */
1365 if (parent->cmode != HTB_CAN_SEND) {
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001366 rb_erase(&parent->pq_node,q->wait_pq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 parent->cmode = HTB_CAN_SEND;
1368 }
1369 parent->level = (parent->parent ? parent->parent->level
1370 : TC_HTB_MAXDEPTH) - 1;
1371 memset (&parent->un.inner,0,sizeof(parent->un.inner));
1372 }
1373 /* leaf (we) needs elementary qdisc */
1374 cl->un.leaf.q = new_q ? new_q : &noop_qdisc;
1375
1376 cl->classid = classid; cl->parent = parent;
1377
1378 /* set class to be in HTB_CAN_SEND state */
1379 cl->tokens = hopt->buffer;
1380 cl->ctokens = hopt->cbuffer;
Dave Jones2724a1a2006-07-15 03:41:12 -04001381 cl->mbuffer = PSCHED_JIFFIE2US(HZ*60); /* 1min */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 PSCHED_GET_TIME(cl->t_c);
1383 cl->cmode = HTB_CAN_SEND;
1384
1385 /* attach to the hash list and parent's family */
1386 list_add_tail(&cl->hlist, q->hash+htb_hash(classid));
1387 list_add_tail(&cl->sibling, parent ? &parent->children : &q->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 } else sch_tree_lock(sch);
1389
1390 /* it used to be a nasty bug here, we have to check that node
1391 is really leaf before changing cl->un.leaf ! */
1392 if (!cl->level) {
1393 cl->un.leaf.quantum = rtab->rate.rate / q->rate2quantum;
1394 if (!hopt->quantum && cl->un.leaf.quantum < 1000) {
1395 printk(KERN_WARNING "HTB: quantum of class %X is small. Consider r2q change.\n", cl->classid);
1396 cl->un.leaf.quantum = 1000;
1397 }
1398 if (!hopt->quantum && cl->un.leaf.quantum > 200000) {
1399 printk(KERN_WARNING "HTB: quantum of class %X is big. Consider r2q change.\n", cl->classid);
1400 cl->un.leaf.quantum = 200000;
1401 }
1402 if (hopt->quantum)
1403 cl->un.leaf.quantum = hopt->quantum;
1404 if ((cl->un.leaf.prio = hopt->prio) >= TC_HTB_NUMPRIO)
1405 cl->un.leaf.prio = TC_HTB_NUMPRIO - 1;
1406 }
1407
1408 cl->buffer = hopt->buffer;
1409 cl->cbuffer = hopt->cbuffer;
1410 if (cl->rate) qdisc_put_rtab(cl->rate); cl->rate = rtab;
1411 if (cl->ceil) qdisc_put_rtab(cl->ceil); cl->ceil = ctab;
1412 sch_tree_unlock(sch);
1413
1414 *arg = (unsigned long)cl;
1415 return 0;
1416
1417failure:
1418 if (rtab) qdisc_put_rtab(rtab);
1419 if (ctab) qdisc_put_rtab(ctab);
1420 return err;
1421}
1422
1423static struct tcf_proto **htb_find_tcf(struct Qdisc *sch, unsigned long arg)
1424{
1425 struct htb_sched *q = qdisc_priv(sch);
1426 struct htb_class *cl = (struct htb_class *)arg;
1427 struct tcf_proto **fl = cl ? &cl->filter_list : &q->filter_list;
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return fl;
1430}
1431
1432static unsigned long htb_bind_filter(struct Qdisc *sch, unsigned long parent,
1433 u32 classid)
1434{
1435 struct htb_sched *q = qdisc_priv(sch);
1436 struct htb_class *cl = htb_find (classid,sch);
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 /*if (cl && !cl->level) return 0;
1439 The line above used to be there to prevent attaching filters to
1440 leaves. But at least tc_index filter uses this just to get class
1441 for other reasons so that we have to allow for it.
1442 ----
1443 19.6.2002 As Werner explained it is ok - bind filter is just
1444 another way to "lock" the class - unlike "get" this lock can
1445 be broken by class during destroy IIUC.
1446 */
1447 if (cl)
1448 cl->filter_cnt++;
1449 else
1450 q->filter_cnt++;
1451 return (unsigned long)cl;
1452}
1453
1454static void htb_unbind_filter(struct Qdisc *sch, unsigned long arg)
1455{
1456 struct htb_sched *q = qdisc_priv(sch);
1457 struct htb_class *cl = (struct htb_class *)arg;
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (cl)
1460 cl->filter_cnt--;
1461 else
1462 q->filter_cnt--;
1463}
1464
1465static void htb_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1466{
1467 struct htb_sched *q = qdisc_priv(sch);
1468 int i;
1469
1470 if (arg->stop)
1471 return;
1472
1473 for (i = 0; i < HTB_HSIZE; i++) {
1474 struct list_head *p;
1475 list_for_each (p,q->hash+i) {
1476 struct htb_class *cl = list_entry(p,struct htb_class,hlist);
1477 if (arg->count < arg->skip) {
1478 arg->count++;
1479 continue;
1480 }
1481 if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
1482 arg->stop = 1;
1483 return;
1484 }
1485 arg->count++;
1486 }
1487 }
1488}
1489
1490static struct Qdisc_class_ops htb_class_ops = {
1491 .graft = htb_graft,
1492 .leaf = htb_leaf,
1493 .get = htb_get,
1494 .put = htb_put,
1495 .change = htb_change_class,
1496 .delete = htb_delete,
1497 .walk = htb_walk,
1498 .tcf_chain = htb_find_tcf,
1499 .bind_tcf = htb_bind_filter,
1500 .unbind_tcf = htb_unbind_filter,
1501 .dump = htb_dump_class,
1502 .dump_stats = htb_dump_class_stats,
1503};
1504
1505static struct Qdisc_ops htb_qdisc_ops = {
1506 .next = NULL,
1507 .cl_ops = &htb_class_ops,
1508 .id = "htb",
1509 .priv_size = sizeof(struct htb_sched),
1510 .enqueue = htb_enqueue,
1511 .dequeue = htb_dequeue,
1512 .requeue = htb_requeue,
1513 .drop = htb_drop,
1514 .init = htb_init,
1515 .reset = htb_reset,
1516 .destroy = htb_destroy,
1517 .change = NULL /* htb_change */,
1518 .dump = htb_dump,
1519 .owner = THIS_MODULE,
1520};
1521
1522static int __init htb_module_init(void)
1523{
1524 return register_qdisc(&htb_qdisc_ops);
1525}
1526static void __exit htb_module_exit(void)
1527{
1528 unregister_qdisc(&htb_qdisc_ops);
1529}
1530module_init(htb_module_init)
1531module_exit(htb_module_exit)
1532MODULE_LICENSE("GPL");