blob: 3fb58f428f724ca9a8fd8c6cd9a41450df567c4d [file] [log] [blame]
Stephen Hemminger87990462006-08-10 23:35:16 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090014 * Ondrej Kraus, <krauso@barr.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * 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>
Jesper Dangaard Brouer47083fc2008-06-16 16:39:32 -070031#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/types.h>
33#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/skbuff.h>
37#include <linux/list.h>
38#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/rbtree.h>
Patrick McHardy0ba48052007-07-02 22:49:07 -070040#include <net/netlink.h>
41#include <net/pkt_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/* HTB algorithm.
44 Author: devik@cdi.cz
45 ========================================================================
46 HTB is like TBF with multiple classes. It is also similar to CBQ because
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090047 it allows to assign priority to each class in hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 In fact it is another implementation of Floyd's formal sharing.
49
50 Levels:
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090051 Each class is assigned level. Leaf has ALWAYS level 0 and root
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 classes have level TC_HTB_MAXDEPTH-1. Interior nodes has level
53 one less than their parent.
54*/
55
Stephen Hemminger87990462006-08-10 23:35:16 -070056#define HTB_HSIZE 16 /* classid hash size */
Jesper Dangaard Brouer47083fc2008-06-16 16:39:32 -070057static int htb_hysteresis __read_mostly = 0; /* whether to use mode hysteresis for speedup */
Stephen Hemminger87990462006-08-10 23:35:16 -070058#define HTB_VER 0x30011 /* major must be matched with number suplied by TC as version */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#if HTB_VER >> 16 != TC_HTB_PROTOVER
61#error "Mismatched sch_htb.c and pkt_sch.h"
62#endif
63
Jesper Dangaard Brouer47083fc2008-06-16 16:39:32 -070064/* Module parameter and sysfs export */
65module_param (htb_hysteresis, int, 0640);
66MODULE_PARM_DESC(htb_hysteresis, "Hysteresis mode, less CPU load, less accurate");
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* used internaly to keep status of single class */
69enum htb_cmode {
Stephen Hemminger87990462006-08-10 23:35:16 -070070 HTB_CANT_SEND, /* class can't send and can't borrow */
71 HTB_MAY_BORROW, /* class can't send but may borrow */
72 HTB_CAN_SEND /* class can send */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
75/* interior & leaf nodes; props specific to leaves are marked L: */
Stephen Hemminger87990462006-08-10 23:35:16 -070076struct htb_class {
77 /* general class parameters */
78 u32 classid;
79 struct gnet_stats_basic bstats;
80 struct gnet_stats_queue qstats;
81 struct gnet_stats_rate_est rate_est;
82 struct tc_htb_xstats xstats; /* our special stats */
83 int refcnt; /* usage count of this class */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Stephen Hemminger87990462006-08-10 23:35:16 -070085 /* topology */
86 int level; /* our level (see above) */
87 struct htb_class *parent; /* parent class */
Stephen Hemminger0cef2962006-08-10 23:35:38 -070088 struct hlist_node hlist; /* classid hash list item */
Stephen Hemminger87990462006-08-10 23:35:16 -070089 struct list_head sibling; /* sibling list item */
90 struct list_head children; /* children list */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Stephen Hemminger87990462006-08-10 23:35:16 -070092 union {
93 struct htb_class_leaf {
94 struct Qdisc *q;
95 int prio;
96 int aprio;
97 int quantum;
98 int deficit[TC_HTB_MAXDEPTH];
99 struct list_head drop_list;
100 } leaf;
101 struct htb_class_inner {
102 struct rb_root feed[TC_HTB_NUMPRIO]; /* feed trees */
103 struct rb_node *ptr[TC_HTB_NUMPRIO]; /* current class ptr */
104 /* When class changes from state 1->2 and disconnects from
105 parent's feed then we lost ptr value and start from the
106 first child again. Here we store classid of the
107 last valid ptr (used when ptr is NULL). */
108 u32 last_ptr_id[TC_HTB_NUMPRIO];
109 } inner;
110 } un;
111 struct rb_node node[TC_HTB_NUMPRIO]; /* node for self or feed tree */
112 struct rb_node pq_node; /* node for event queue */
Patrick McHardyfb983d42007-03-16 01:22:39 -0700113 psched_time_t pq_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Stephen Hemminger87990462006-08-10 23:35:16 -0700115 int prio_activity; /* for which prios are we active */
116 enum htb_cmode cmode; /* current mode of the class */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Stephen Hemminger87990462006-08-10 23:35:16 -0700118 /* class attached filters */
119 struct tcf_proto *filter_list;
120 int filter_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Stephen Hemminger87990462006-08-10 23:35:16 -0700122 int warned; /* only one warning about non work conserving .. */
123
124 /* token bucket parameters */
125 struct qdisc_rate_table *rate; /* rate table of the class itself */
126 struct qdisc_rate_table *ceil; /* ceiling rate (limits borrows too) */
127 long buffer, cbuffer; /* token bucket depth/rate */
128 psched_tdiff_t mbuffer; /* max wait time */
129 long tokens, ctokens; /* current number of tokens */
130 psched_time_t t_c; /* checkpoint time */
Jarek Poplawski160d5e12006-12-08 00:26:56 -0800131
132 int prio; /* For parent to leaf return possible here */
133 int quantum; /* we do backup. Finally full replacement */
134 /* of un.leaf originals should be done. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
Stephen Hemminger87990462006-08-10 23:35:16 -0700137static inline long L2T(struct htb_class *cl, struct qdisc_rate_table *rate,
138 int size)
139{
Jesper Dangaard Brouere9bef552007-09-12 16:35:24 +0200140 long result = qdisc_l2t(rate, size);
141 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Stephen Hemminger87990462006-08-10 23:35:16 -0700144struct htb_sched {
145 struct list_head root; /* root classes list */
Stephen Hemminger0cef2962006-08-10 23:35:38 -0700146 struct hlist_head hash[HTB_HSIZE]; /* hashed by classid */
147 struct list_head drops[TC_HTB_NUMPRIO];/* active leaves (for drops) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Stephen Hemminger87990462006-08-10 23:35:16 -0700149 /* self list - roots of self generating tree */
150 struct rb_root row[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
151 int row_mask[TC_HTB_MAXDEPTH];
152 struct rb_node *ptr[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
153 u32 last_ptr_id[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Stephen Hemminger87990462006-08-10 23:35:16 -0700155 /* self wait list - roots of wait PQs per row */
156 struct rb_root wait_pq[TC_HTB_MAXDEPTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Stephen Hemminger87990462006-08-10 23:35:16 -0700158 /* time of nearest event per level (row) */
Patrick McHardyfb983d42007-03-16 01:22:39 -0700159 psched_time_t near_ev_cache[TC_HTB_MAXDEPTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Stephen Hemminger87990462006-08-10 23:35:16 -0700161 /* whether we hit non-work conserving class during this dequeue; we use */
162 int nwc_hit; /* this to disable mindelay complaint in dequeue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Stephen Hemminger87990462006-08-10 23:35:16 -0700164 int defcls; /* class where unclassified flows go to */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Stephen Hemminger87990462006-08-10 23:35:16 -0700166 /* filters for qdisc itself */
167 struct tcf_proto *filter_list;
168 int filter_cnt;
169
170 int rate2quantum; /* quant = rate / rate2quantum */
171 psched_time_t now; /* cached dequeue time */
Patrick McHardyfb983d42007-03-16 01:22:39 -0700172 struct qdisc_watchdog watchdog;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Stephen Hemminger87990462006-08-10 23:35:16 -0700174 /* non shaped skbs; let them go directly thru */
175 struct sk_buff_head direct_queue;
176 int direct_qlen; /* max qlen of above */
177
178 long direct_pkts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179};
180
181/* compute hash of size HTB_HSIZE for given handle */
Stephen Hemminger87990462006-08-10 23:35:16 -0700182static inline int htb_hash(u32 h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184#if HTB_HSIZE != 16
Stephen Hemminger87990462006-08-10 23:35:16 -0700185#error "Declare new hash for your HTB_HSIZE"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#endif
Stephen Hemminger87990462006-08-10 23:35:16 -0700187 h ^= h >> 8; /* stolen from cbq_hash */
188 h ^= h >> 4;
189 return h & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
192/* find class in global hash table using given handle */
Stephen Hemminger87990462006-08-10 23:35:16 -0700193static inline struct htb_class *htb_find(u32 handle, struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 struct htb_sched *q = qdisc_priv(sch);
Stephen Hemminger0cef2962006-08-10 23:35:38 -0700196 struct hlist_node *p;
197 struct htb_class *cl;
198
Stephen Hemminger87990462006-08-10 23:35:16 -0700199 if (TC_H_MAJ(handle) != sch->handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return NULL;
Stephen Hemminger87990462006-08-10 23:35:16 -0700201
Stephen Hemminger0cef2962006-08-10 23:35:38 -0700202 hlist_for_each_entry(cl, p, q->hash + htb_hash(handle), hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (cl->classid == handle)
204 return cl;
205 }
206 return NULL;
207}
208
209/**
210 * htb_classify - classify a packet into class
211 *
212 * It returns NULL if the packet should be dropped or -1 if the packet
213 * should be passed directly thru. In all other cases leaf class is returned.
214 * We allow direct class selection by classid in priority. The we examine
215 * filters in qdisc and in inner nodes (if higher filter points to the inner
216 * node). If we end up with classid MAJOR:0 we enqueue the skb into special
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900217 * internal fifo (direct). These packets then go directly thru. If we still
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * have no valid leaf we try to use MAJOR:default leaf. It still unsuccessfull
219 * then finish and return direct queue.
220 */
221#define HTB_DIRECT (struct htb_class*)-1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Stephen Hemminger87990462006-08-10 23:35:16 -0700223static struct htb_class *htb_classify(struct sk_buff *skb, struct Qdisc *sch,
224 int *qerr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 struct htb_sched *q = qdisc_priv(sch);
227 struct htb_class *cl;
228 struct tcf_result res;
229 struct tcf_proto *tcf;
230 int result;
231
232 /* allow to select class by setting skb->priority to valid classid;
233 note that nfmark can be used too by attaching filter fw with no
234 rules in it */
235 if (skb->priority == sch->handle)
Stephen Hemminger87990462006-08-10 23:35:16 -0700236 return HTB_DIRECT; /* X:0 (direct flow) selected */
237 if ((cl = htb_find(skb->priority, sch)) != NULL && cl->level == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return cl;
239
Jamal Hadi Salim29f1df62006-01-08 22:35:55 -0800240 *qerr = NET_XMIT_BYPASS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 tcf = q->filter_list;
242 while (tcf && (result = tc_classify(skb, tcf, &res)) >= 0) {
243#ifdef CONFIG_NET_CLS_ACT
244 switch (result) {
245 case TC_ACT_QUEUED:
Stephen Hemminger87990462006-08-10 23:35:16 -0700246 case TC_ACT_STOLEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 *qerr = NET_XMIT_SUCCESS;
248 case TC_ACT_SHOT:
249 return NULL;
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251#endif
Stephen Hemminger87990462006-08-10 23:35:16 -0700252 if ((cl = (void *)res.class) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (res.classid == sch->handle)
Stephen Hemminger87990462006-08-10 23:35:16 -0700254 return HTB_DIRECT; /* X:0 (direct flow) */
255 if ((cl = htb_find(res.classid, sch)) == NULL)
256 break; /* filter selected invalid classid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258 if (!cl->level)
Stephen Hemminger87990462006-08-10 23:35:16 -0700259 return cl; /* we hit leaf; return it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 /* we have got inner class; apply inner filter chain */
262 tcf = cl->filter_list;
263 }
264 /* classification failed; try to use default class */
Stephen Hemminger87990462006-08-10 23:35:16 -0700265 cl = htb_find(TC_H_MAKE(TC_H_MAJ(sch->handle), q->defcls), sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (!cl || cl->level)
Stephen Hemminger87990462006-08-10 23:35:16 -0700267 return HTB_DIRECT; /* bad default .. this is safe bet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return cl;
269}
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271/**
272 * htb_add_to_id_tree - adds class to the round robin list
273 *
274 * Routine adds class to the list (actually tree) sorted by classid.
275 * Make sure that class is not already on such list for given prio.
276 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700277static void htb_add_to_id_tree(struct rb_root *root,
278 struct htb_class *cl, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 struct rb_node **p = &root->rb_node, *parent = NULL;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 while (*p) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700283 struct htb_class *c;
284 parent = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 c = rb_entry(parent, struct htb_class, node[prio]);
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (cl->classid > c->classid)
288 p = &parent->rb_right;
Stephen Hemminger87990462006-08-10 23:35:16 -0700289 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 p = &parent->rb_left;
291 }
292 rb_link_node(&cl->node[prio], parent, p);
293 rb_insert_color(&cl->node[prio], root);
294}
295
296/**
297 * htb_add_to_wait_tree - adds class to the event queue with delay
298 *
299 * The class is added to priority event queue to indicate that class will
300 * change its mode in cl->pq_key microseconds. Make sure that class is not
301 * already in the queue.
302 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700303static void htb_add_to_wait_tree(struct htb_sched *q,
304 struct htb_class *cl, long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 struct rb_node **p = &q->wait_pq[cl->level].rb_node, *parent = NULL;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700307
Patrick McHardyfb983d42007-03-16 01:22:39 -0700308 cl->pq_key = q->now + delay;
309 if (cl->pq_key == q->now)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 cl->pq_key++;
311
312 /* update the nearest event cache */
Patrick McHardyfb983d42007-03-16 01:22:39 -0700313 if (q->near_ev_cache[cl->level] > cl->pq_key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 q->near_ev_cache[cl->level] = cl->pq_key;
Stephen Hemminger87990462006-08-10 23:35:16 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 while (*p) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700317 struct htb_class *c;
318 parent = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 c = rb_entry(parent, struct htb_class, pq_node);
Patrick McHardyfb983d42007-03-16 01:22:39 -0700320 if (cl->pq_key >= c->pq_key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 p = &parent->rb_right;
Stephen Hemminger87990462006-08-10 23:35:16 -0700322 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 p = &parent->rb_left;
324 }
325 rb_link_node(&cl->pq_node, parent, p);
326 rb_insert_color(&cl->pq_node, &q->wait_pq[cl->level]);
327}
328
329/**
330 * htb_next_rb_node - finds next node in binary tree
331 *
332 * When we are past last key we return NULL.
333 * Average complexity is 2 steps per call.
334 */
Stephen Hemminger3696f622006-08-10 23:36:01 -0700335static inline void htb_next_rb_node(struct rb_node **n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 *n = rb_next(*n);
338}
339
340/**
341 * htb_add_class_to_row - add class to its row
342 *
343 * The class is added to row at priorities marked in mask.
344 * It does nothing if mask == 0.
345 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700346static inline void htb_add_class_to_row(struct htb_sched *q,
347 struct htb_class *cl, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 q->row_mask[cl->level] |= mask;
350 while (mask) {
351 int prio = ffz(~mask);
352 mask &= ~(1 << prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700353 htb_add_to_id_tree(q->row[cl->level] + prio, cl, prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355}
356
Stephen Hemminger3696f622006-08-10 23:36:01 -0700357/* If this triggers, it is a bug in this code, but it need not be fatal */
358static void htb_safe_rb_erase(struct rb_node *rb, struct rb_root *root)
359{
Ismail Donmez81771b32006-10-03 13:49:10 -0700360 if (RB_EMPTY_NODE(rb)) {
Stephen Hemminger3696f622006-08-10 23:36:01 -0700361 WARN_ON(1);
362 } else {
363 rb_erase(rb, root);
364 RB_CLEAR_NODE(rb);
365 }
366}
367
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369/**
370 * htb_remove_class_from_row - removes class from its row
371 *
372 * The class is removed from row at priorities marked in mask.
373 * It does nothing if mask == 0.
374 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700375static inline void htb_remove_class_from_row(struct htb_sched *q,
376 struct htb_class *cl, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 int m = 0;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 while (mask) {
381 int prio = ffz(~mask);
Stephen Hemminger3696f622006-08-10 23:36:01 -0700382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 mask &= ~(1 << prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700384 if (q->ptr[cl->level][prio] == cl->node + prio)
385 htb_next_rb_node(q->ptr[cl->level] + prio);
Stephen Hemminger3696f622006-08-10 23:36:01 -0700386
387 htb_safe_rb_erase(cl->node + prio, q->row[cl->level] + prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700388 if (!q->row[cl->level][prio].rb_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 m |= 1 << prio;
390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 q->row_mask[cl->level] &= ~m;
392}
393
394/**
395 * htb_activate_prios - creates active classe's feed chain
396 *
397 * The class is connected to ancestors and/or appropriate rows
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900398 * for priorities it is participating on. cl->cmode must be new
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 * (activated) mode. It does nothing if cl->prio_activity == 0.
400 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700401static void htb_activate_prios(struct htb_sched *q, struct htb_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 struct htb_class *p = cl->parent;
Stephen Hemminger87990462006-08-10 23:35:16 -0700404 long m, mask = cl->prio_activity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 while (cl->cmode == HTB_MAY_BORROW && p && mask) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700407 m = mask;
408 while (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 int prio = ffz(~m);
410 m &= ~(1 << prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (p->un.inner.feed[prio].rb_node)
413 /* parent already has its feed in use so that
414 reset bit in mask as parent is already ok */
415 mask &= ~(1 << prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700416
417 htb_add_to_id_tree(p->un.inner.feed + prio, cl, prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 p->prio_activity |= mask;
Stephen Hemminger87990462006-08-10 23:35:16 -0700420 cl = p;
421 p = cl->parent;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424 if (cl->cmode == HTB_CAN_SEND && mask)
Stephen Hemminger87990462006-08-10 23:35:16 -0700425 htb_add_class_to_row(q, cl, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428/**
429 * htb_deactivate_prios - remove class from feed chain
430 *
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900431 * cl->cmode must represent old mode (before deactivation). It does
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 * nothing if cl->prio_activity == 0. Class is removed from all feed
433 * chains and rows.
434 */
435static void htb_deactivate_prios(struct htb_sched *q, struct htb_class *cl)
436{
437 struct htb_class *p = cl->parent;
Stephen Hemminger87990462006-08-10 23:35:16 -0700438 long m, mask = cl->prio_activity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 while (cl->cmode == HTB_MAY_BORROW && p && mask) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700441 m = mask;
442 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 while (m) {
444 int prio = ffz(~m);
445 m &= ~(1 << prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700446
447 if (p->un.inner.ptr[prio] == cl->node + prio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /* we are removing child which is pointed to from
449 parent feed - forget the pointer but remember
450 classid */
451 p->un.inner.last_ptr_id[prio] = cl->classid;
452 p->un.inner.ptr[prio] = NULL;
453 }
Stephen Hemminger87990462006-08-10 23:35:16 -0700454
Stephen Hemminger3696f622006-08-10 23:36:01 -0700455 htb_safe_rb_erase(cl->node + prio, p->un.inner.feed + prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700456
457 if (!p->un.inner.feed[prio].rb_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 mask |= 1 << prio;
459 }
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 p->prio_activity &= ~mask;
Stephen Hemminger87990462006-08-10 23:35:16 -0700462 cl = p;
463 p = cl->parent;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Stephen Hemminger87990462006-08-10 23:35:16 -0700466 if (cl->cmode == HTB_CAN_SEND && mask)
467 htb_remove_class_from_row(q, cl, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700470static inline long htb_lowater(const struct htb_class *cl)
471{
Jesper Dangaard Brouer47083fc2008-06-16 16:39:32 -0700472 if (htb_hysteresis)
473 return cl->cmode != HTB_CANT_SEND ? -cl->cbuffer : 0;
474 else
475 return 0;
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700476}
477static inline long htb_hiwater(const struct htb_class *cl)
478{
Jesper Dangaard Brouer47083fc2008-06-16 16:39:32 -0700479 if (htb_hysteresis)
480 return cl->cmode == HTB_CAN_SEND ? -cl->buffer : 0;
481 else
482 return 0;
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700483}
Jesper Dangaard Brouer47083fc2008-06-16 16:39:32 -0700484
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/**
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
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900491 * from now to time when cl will change its state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 * Also it is worth to note that class mode doesn't change simply
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900493 * at cl->{c,}tokens == 0 but there can rather be hysteresis of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * 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 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700497static inline enum htb_cmode
498htb_class_mode(struct htb_class *cl, long *diff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Stephen Hemminger87990462006-08-10 23:35:16 -0700500 long toks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Stephen Hemminger87990462006-08-10 23:35:16 -0700502 if ((toks = (cl->ctokens + *diff)) < htb_lowater(cl)) {
503 *diff = -toks;
504 return HTB_CANT_SEND;
505 }
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700506
Stephen Hemminger87990462006-08-10 23:35:16 -0700507 if ((toks = (cl->tokens + *diff)) >= htb_hiwater(cl))
508 return HTB_CAN_SEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Stephen Hemminger87990462006-08-10 23:35:16 -0700510 *diff = -toks;
511 return HTB_MAY_BORROW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
514/**
515 * htb_change_class_mode - changes classe's mode
516 *
517 * This should be the only way how to change classe's mode under normal
518 * cirsumstances. Routine will update feed lists linkage, change mode
519 * and add class to the wait event queue if appropriate. New mode should
520 * be different from old one and cl->pq_key has to be valid if changing
521 * to mode other than HTB_CAN_SEND (see htb_add_to_wait_tree).
522 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700523static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524htb_change_class_mode(struct htb_sched *q, struct htb_class *cl, long *diff)
Stephen Hemminger87990462006-08-10 23:35:16 -0700525{
526 enum htb_cmode new_mode = htb_class_mode(cl, diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 if (new_mode == cl->cmode)
Stephen Hemminger87990462006-08-10 23:35:16 -0700529 return;
530
531 if (cl->prio_activity) { /* not necessary: speed optimization */
532 if (cl->cmode != HTB_CANT_SEND)
533 htb_deactivate_prios(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 cl->cmode = new_mode;
Stephen Hemminger87990462006-08-10 23:35:16 -0700535 if (new_mode != HTB_CANT_SEND)
536 htb_activate_prios(q, cl);
537 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 cl->cmode = new_mode;
539}
540
541/**
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900542 * htb_activate - inserts leaf cl into appropriate active feeds
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 *
544 * Routine learns (new) priority of leaf and activates feed chain
545 * for the prio. It can be called on already active leaf safely.
546 * It also adds leaf into droplist.
547 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700548static inline void htb_activate(struct htb_sched *q, struct htb_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 BUG_TRAP(!cl->level && cl->un.leaf.q && cl->un.leaf.q->q.qlen);
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (!cl->prio_activity) {
553 cl->prio_activity = 1 << (cl->un.leaf.aprio = cl->un.leaf.prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700554 htb_activate_prios(q, cl);
555 list_add_tail(&cl->un.leaf.drop_list,
556 q->drops + cl->un.leaf.aprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558}
559
560/**
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900561 * htb_deactivate - remove leaf cl from active feeds
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 *
563 * Make sure that leaf is active. In the other words it can't be called
564 * with non-active leaf. It also removes class from the drop list.
565 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700566static inline void htb_deactivate(struct htb_sched *q, struct htb_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 BUG_TRAP(cl->prio_activity);
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700569
Stephen Hemminger87990462006-08-10 23:35:16 -0700570 htb_deactivate_prios(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 cl->prio_activity = 0;
572 list_del_init(&cl->un.leaf.drop_list);
573}
574
575static int htb_enqueue(struct sk_buff *skb, struct Qdisc *sch)
576{
Stephen Hemminger87990462006-08-10 23:35:16 -0700577 int ret;
578 struct htb_sched *q = qdisc_priv(sch);
579 struct htb_class *cl = htb_classify(skb, sch, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Stephen Hemminger87990462006-08-10 23:35:16 -0700581 if (cl == HTB_DIRECT) {
582 /* enqueue to helper queue */
583 if (q->direct_queue.qlen < q->direct_qlen) {
584 __skb_queue_tail(&q->direct_queue, skb);
585 q->direct_pkts++;
586 } else {
587 kfree_skb(skb);
588 sch->qstats.drops++;
589 return NET_XMIT_DROP;
590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591#ifdef CONFIG_NET_CLS_ACT
Stephen Hemminger87990462006-08-10 23:35:16 -0700592 } else if (!cl) {
593 if (ret == NET_XMIT_BYPASS)
594 sch->qstats.drops++;
595 kfree_skb(skb);
596 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597#endif
Stephen Hemminger87990462006-08-10 23:35:16 -0700598 } else if (cl->un.leaf.q->enqueue(skb, cl->un.leaf.q) !=
599 NET_XMIT_SUCCESS) {
600 sch->qstats.drops++;
601 cl->qstats.drops++;
602 return NET_XMIT_DROP;
603 } else {
Ranjit Manomohanc9726d62007-07-10 22:43:16 -0700604 cl->bstats.packets +=
605 skb_is_gso(skb)?skb_shinfo(skb)->gso_segs:1;
Stephen Hemminger87990462006-08-10 23:35:16 -0700606 cl->bstats.bytes += skb->len;
607 htb_activate(q, cl);
608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Stephen Hemminger87990462006-08-10 23:35:16 -0700610 sch->q.qlen++;
Ranjit Manomohanc9726d62007-07-10 22:43:16 -0700611 sch->bstats.packets += skb_is_gso(skb)?skb_shinfo(skb)->gso_segs:1;
Stephen Hemminger87990462006-08-10 23:35:16 -0700612 sch->bstats.bytes += skb->len;
613 return NET_XMIT_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616/* TODO: requeuing packet charges it to policers again !! */
617static int htb_requeue(struct sk_buff *skb, struct Qdisc *sch)
618{
Jarek Poplawski21347452008-02-09 23:44:00 -0800619 int ret;
Stephen Hemminger87990462006-08-10 23:35:16 -0700620 struct htb_sched *q = qdisc_priv(sch);
Stephen Hemminger87990462006-08-10 23:35:16 -0700621 struct htb_class *cl = htb_classify(skb, sch, &ret);
622 struct sk_buff *tskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Jarek Poplawski21347452008-02-09 23:44:00 -0800624 if (cl == HTB_DIRECT) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700625 /* enqueue to helper queue */
Jarek Poplawski21347452008-02-09 23:44:00 -0800626 if (q->direct_queue.qlen < q->direct_qlen) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700627 __skb_queue_head(&q->direct_queue, skb);
628 } else {
629 __skb_queue_head(&q->direct_queue, skb);
630 tskb = __skb_dequeue_tail(&q->direct_queue);
631 kfree_skb(tskb);
632 sch->qstats.drops++;
633 return NET_XMIT_CN;
634 }
Jarek Poplawski21347452008-02-09 23:44:00 -0800635#ifdef CONFIG_NET_CLS_ACT
636 } else if (!cl) {
637 if (ret == NET_XMIT_BYPASS)
638 sch->qstats.drops++;
639 kfree_skb(skb);
640 return ret;
641#endif
Stephen Hemminger87990462006-08-10 23:35:16 -0700642 } else if (cl->un.leaf.q->ops->requeue(skb, cl->un.leaf.q) !=
643 NET_XMIT_SUCCESS) {
644 sch->qstats.drops++;
645 cl->qstats.drops++;
646 return NET_XMIT_DROP;
647 } else
648 htb_activate(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Stephen Hemminger87990462006-08-10 23:35:16 -0700650 sch->q.qlen++;
651 sch->qstats.requeues++;
652 return NET_XMIT_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/**
656 * htb_charge_class - charges amount "bytes" to leaf and ancestors
657 *
658 * Routine assumes that packet "bytes" long was dequeued from leaf cl
659 * borrowing from "level". It accounts bytes to ceil leaky bucket for
660 * leaf and all ancestors and to rate bucket for ancestors at levels
661 * "level" and higher. It also handles possible change of mode resulting
662 * from the update. Note that mode can also increase here (MAY_BORROW to
663 * CAN_SEND) because we can use more precise clock that event queue here.
664 * In such case we remove class from event queue first.
665 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700666static void htb_charge_class(struct htb_sched *q, struct htb_class *cl,
Ranjit Manomohanc9726d62007-07-10 22:43:16 -0700667 int level, struct sk_buff *skb)
Stephen Hemminger87990462006-08-10 23:35:16 -0700668{
Ranjit Manomohanc9726d62007-07-10 22:43:16 -0700669 int bytes = skb->len;
Stephen Hemminger87990462006-08-10 23:35:16 -0700670 long toks, diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 enum htb_cmode old_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673#define HTB_ACCNT(T,B,R) toks = diff + cl->T; \
674 if (toks > cl->B) toks = cl->B; \
675 toks -= L2T(cl, cl->R, bytes); \
676 if (toks <= -cl->mbuffer) toks = 1-cl->mbuffer; \
677 cl->T = toks
678
679 while (cl) {
Patrick McHardy03cc45c2007-03-23 11:29:11 -0700680 diff = psched_tdiff_bounded(q->now, cl->t_c, cl->mbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (cl->level >= level) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700682 if (cl->level == level)
683 cl->xstats.lends++;
684 HTB_ACCNT(tokens, buffer, rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 } else {
686 cl->xstats.borrows++;
Stephen Hemminger87990462006-08-10 23:35:16 -0700687 cl->tokens += diff; /* we moved t_c; update tokens */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
Stephen Hemminger87990462006-08-10 23:35:16 -0700689 HTB_ACCNT(ctokens, cbuffer, ceil);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 cl->t_c = q->now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Stephen Hemminger87990462006-08-10 23:35:16 -0700692 old_mode = cl->cmode;
693 diff = 0;
694 htb_change_class_mode(q, cl, &diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (old_mode != cl->cmode) {
696 if (old_mode != HTB_CAN_SEND)
Stephen Hemminger3696f622006-08-10 23:36:01 -0700697 htb_safe_rb_erase(&cl->pq_node, q->wait_pq + cl->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (cl->cmode != HTB_CAN_SEND)
Stephen Hemminger87990462006-08-10 23:35:16 -0700699 htb_add_to_wait_tree(q, cl, diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 /* update byte stats except for leaves which are already updated */
703 if (cl->level) {
704 cl->bstats.bytes += bytes;
Ranjit Manomohanc9726d62007-07-10 22:43:16 -0700705 cl->bstats.packets += skb_is_gso(skb)?
706 skb_shinfo(skb)->gso_segs:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708 cl = cl->parent;
709 }
710}
711
712/**
713 * htb_do_events - make mode changes to classes at the level
714 *
Patrick McHardyfb983d42007-03-16 01:22:39 -0700715 * Scans event queue for pending events and applies them. Returns time of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 * next pending event (0 for no event in pq).
Patrick McHardyfb983d42007-03-16 01:22:39 -0700717 * Note: Applied are events whose have cl->pq_key <= q->now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 */
Patrick McHardyfb983d42007-03-16 01:22:39 -0700719static psched_time_t htb_do_events(struct htb_sched *q, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Martin Devera8f3ea332008-03-23 22:00:38 -0700721 /* don't run for longer than 2 jiffies; 2 is used instead of
722 1 to simplify things when jiffy is going to be incremented
723 too soon */
724 unsigned long stop_at = jiffies + 2;
725 while (time_before(jiffies, stop_at)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct htb_class *cl;
727 long diff;
Akinbou Mita30bdbe32006-10-12 01:52:05 -0700728 struct rb_node *p = rb_first(&q->wait_pq[level]);
729
Stephen Hemminger87990462006-08-10 23:35:16 -0700730 if (!p)
731 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 cl = rb_entry(p, struct htb_class, pq_node);
Patrick McHardyfb983d42007-03-16 01:22:39 -0700734 if (cl->pq_key > q->now)
735 return cl->pq_key;
736
Stephen Hemminger3696f622006-08-10 23:36:01 -0700737 htb_safe_rb_erase(p, q->wait_pq + level);
Patrick McHardy03cc45c2007-03-23 11:29:11 -0700738 diff = psched_tdiff_bounded(q->now, cl->t_c, cl->mbuffer);
Stephen Hemminger87990462006-08-10 23:35:16 -0700739 htb_change_class_mode(q, cl, &diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (cl->cmode != HTB_CAN_SEND)
Stephen Hemminger87990462006-08-10 23:35:16 -0700741 htb_add_to_wait_tree(q, cl, diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
Martin Devera8f3ea332008-03-23 22:00:38 -0700743 /* too much load - let's continue on next jiffie */
744 return q->now + PSCHED_TICKS_PER_SEC / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
747/* Returns class->node+prio from id-tree where classe's id is >= id. NULL
748 is no such one exists. */
Stephen Hemminger87990462006-08-10 23:35:16 -0700749static struct rb_node *htb_id_find_next_upper(int prio, struct rb_node *n,
750 u32 id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 struct rb_node *r = NULL;
753 while (n) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700754 struct htb_class *cl =
755 rb_entry(n, struct htb_class, node[prio]);
756 if (id == cl->classid)
757 return n;
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (id > cl->classid) {
760 n = n->rb_right;
761 } else {
762 r = n;
763 n = n->rb_left;
764 }
765 }
766 return r;
767}
768
769/**
770 * htb_lookup_leaf - returns next leaf class in DRR order
771 *
772 * Find leaf where current feed pointers points to.
773 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700774static struct htb_class *htb_lookup_leaf(struct rb_root *tree, int prio,
775 struct rb_node **pptr, u32 * pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 int i;
778 struct {
779 struct rb_node *root;
780 struct rb_node **pptr;
781 u32 *pid;
Stephen Hemminger87990462006-08-10 23:35:16 -0700782 } stk[TC_HTB_MAXDEPTH], *sp = stk;
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 BUG_TRAP(tree->rb_node);
785 sp->root = tree->rb_node;
786 sp->pptr = pptr;
787 sp->pid = pid;
788
789 for (i = 0; i < 65535; i++) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700790 if (!*sp->pptr && *sp->pid) {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900791 /* ptr was invalidated but id is valid - try to recover
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 the original or next ptr */
Stephen Hemminger87990462006-08-10 23:35:16 -0700793 *sp->pptr =
794 htb_id_find_next_upper(prio, sp->root, *sp->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
Stephen Hemminger87990462006-08-10 23:35:16 -0700796 *sp->pid = 0; /* ptr is valid now so that remove this hint as it
797 can become out of date quickly */
798 if (!*sp->pptr) { /* we are at right end; rewind & go up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 *sp->pptr = sp->root;
Stephen Hemminger87990462006-08-10 23:35:16 -0700800 while ((*sp->pptr)->rb_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 *sp->pptr = (*sp->pptr)->rb_left;
802 if (sp > stk) {
803 sp--;
Stephen Hemminger87990462006-08-10 23:35:16 -0700804 BUG_TRAP(*sp->pptr);
805 if (!*sp->pptr)
806 return NULL;
807 htb_next_rb_node(sp->pptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809 } else {
810 struct htb_class *cl;
Stephen Hemminger87990462006-08-10 23:35:16 -0700811 cl = rb_entry(*sp->pptr, struct htb_class, node[prio]);
812 if (!cl->level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return cl;
814 (++sp)->root = cl->un.inner.feed[prio].rb_node;
Stephen Hemminger87990462006-08-10 23:35:16 -0700815 sp->pptr = cl->un.inner.ptr + prio;
816 sp->pid = cl->un.inner.last_ptr_id + prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818 }
819 BUG_TRAP(0);
820 return NULL;
821}
822
823/* dequeues packet at given priority and level; call only if
824 you are sure that there is active class at prio/level */
Stephen Hemminger87990462006-08-10 23:35:16 -0700825static struct sk_buff *htb_dequeue_tree(struct htb_sched *q, int prio,
826 int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 struct sk_buff *skb = NULL;
Stephen Hemminger87990462006-08-10 23:35:16 -0700829 struct htb_class *cl, *start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /* look initial class up in the row */
Stephen Hemminger87990462006-08-10 23:35:16 -0700831 start = cl = htb_lookup_leaf(q->row[level] + prio, prio,
832 q->ptr[level] + prio,
833 q->last_ptr_id[level] + prio);
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 do {
836next:
Stephen Hemminger87990462006-08-10 23:35:16 -0700837 BUG_TRAP(cl);
838 if (!cl)
839 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /* class can be empty - it is unlikely but can be true if leaf
842 qdisc drops packets in enqueue routine or if someone used
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900843 graft operation on the leaf since last dequeue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 simply deactivate and skip such class */
845 if (unlikely(cl->un.leaf.q->q.qlen == 0)) {
846 struct htb_class *next;
Stephen Hemminger87990462006-08-10 23:35:16 -0700847 htb_deactivate(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /* row/level might become empty */
850 if ((q->row_mask[level] & (1 << prio)) == 0)
Stephen Hemminger87990462006-08-10 23:35:16 -0700851 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Stephen Hemminger87990462006-08-10 23:35:16 -0700853 next = htb_lookup_leaf(q->row[level] + prio,
854 prio, q->ptr[level] + prio,
855 q->last_ptr_id[level] + prio);
856
857 if (cl == start) /* fix start if we just deleted it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 start = next;
859 cl = next;
860 goto next;
861 }
Stephen Hemminger87990462006-08-10 23:35:16 -0700862
863 skb = cl->un.leaf.q->dequeue(cl->un.leaf.q);
864 if (likely(skb != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 break;
866 if (!cl->warned) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700867 printk(KERN_WARNING
868 "htb: class %X isn't work conserving ?!\n",
869 cl->classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 cl->warned = 1;
871 }
872 q->nwc_hit++;
Stephen Hemminger87990462006-08-10 23:35:16 -0700873 htb_next_rb_node((level ? cl->parent->un.inner.ptr : q->
874 ptr[0]) + prio);
875 cl = htb_lookup_leaf(q->row[level] + prio, prio,
876 q->ptr[level] + prio,
877 q->last_ptr_id[level] + prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 } while (cl != start);
880
881 if (likely(skb != NULL)) {
882 if ((cl->un.leaf.deficit[level] -= skb->len) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 cl->un.leaf.deficit[level] += cl->un.leaf.quantum;
Stephen Hemminger87990462006-08-10 23:35:16 -0700884 htb_next_rb_node((level ? cl->parent->un.inner.ptr : q->
885 ptr[0]) + prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887 /* this used to be after charge_class but this constelation
888 gives us slightly better performance */
889 if (!cl->un.leaf.q->q.qlen)
Stephen Hemminger87990462006-08-10 23:35:16 -0700890 htb_deactivate(q, cl);
Ranjit Manomohanc9726d62007-07-10 22:43:16 -0700891 htb_charge_class(q, cl, level, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893 return skb;
894}
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896static struct sk_buff *htb_dequeue(struct Qdisc *sch)
897{
898 struct sk_buff *skb = NULL;
899 struct htb_sched *q = qdisc_priv(sch);
900 int level;
Patrick McHardyfb983d42007-03-16 01:22:39 -0700901 psched_time_t next_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 /* try to dequeue direct packets as high prio (!) to minimize cpu work */
Stephen Hemminger87990462006-08-10 23:35:16 -0700904 skb = __skb_dequeue(&q->direct_queue);
905 if (skb != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 sch->flags &= ~TCQ_F_THROTTLED;
907 sch->q.qlen--;
908 return skb;
909 }
910
Stephen Hemminger87990462006-08-10 23:35:16 -0700911 if (!sch->q.qlen)
912 goto fin;
Patrick McHardy3bebcda2007-03-23 11:29:25 -0700913 q->now = psched_get_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Patrick McHardyfb983d42007-03-16 01:22:39 -0700915 next_event = q->now + 5 * PSCHED_TICKS_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 q->nwc_hit = 0;
917 for (level = 0; level < TC_HTB_MAXDEPTH; level++) {
918 /* common case optimization - skip event handler quickly */
919 int m;
Patrick McHardyfb983d42007-03-16 01:22:39 -0700920 psched_time_t event;
Stephen Hemminger87990462006-08-10 23:35:16 -0700921
Patrick McHardyfb983d42007-03-16 01:22:39 -0700922 if (q->now >= q->near_ev_cache[level]) {
923 event = htb_do_events(q, level);
Patrick McHardy2e4b3b02007-05-23 23:39:54 -0700924 if (!event)
925 event = q->now + PSCHED_TICKS_PER_SEC;
926 q->near_ev_cache[level] = event;
Patrick McHardyfb983d42007-03-16 01:22:39 -0700927 } else
928 event = q->near_ev_cache[level];
929
930 if (event && next_event > event)
931 next_event = event;
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 m = ~q->row_mask[level];
934 while (m != (int)(-1)) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700935 int prio = ffz(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 m |= 1 << prio;
Stephen Hemminger87990462006-08-10 23:35:16 -0700937 skb = htb_dequeue_tree(q, prio, level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (likely(skb != NULL)) {
939 sch->q.qlen--;
940 sch->flags &= ~TCQ_F_THROTTLED;
941 goto fin;
942 }
943 }
944 }
Patrick McHardyfb983d42007-03-16 01:22:39 -0700945 sch->qstats.overlimits++;
946 qdisc_watchdog_schedule(&q->watchdog, next_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947fin:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return skb;
949}
950
951/* try to drop from each class (by prio) until one succeed */
Stephen Hemminger87990462006-08-10 23:35:16 -0700952static unsigned int htb_drop(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 struct htb_sched *q = qdisc_priv(sch);
955 int prio;
956
957 for (prio = TC_HTB_NUMPRIO - 1; prio >= 0; prio--) {
958 struct list_head *p;
Stephen Hemminger87990462006-08-10 23:35:16 -0700959 list_for_each(p, q->drops + prio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 struct htb_class *cl = list_entry(p, struct htb_class,
961 un.leaf.drop_list);
962 unsigned int len;
Stephen Hemminger87990462006-08-10 23:35:16 -0700963 if (cl->un.leaf.q->ops->drop &&
964 (len = cl->un.leaf.q->ops->drop(cl->un.leaf.q))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 sch->q.qlen--;
966 if (!cl->un.leaf.q->q.qlen)
Stephen Hemminger87990462006-08-10 23:35:16 -0700967 htb_deactivate(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return len;
969 }
970 }
971 }
972 return 0;
973}
974
975/* reset all classes */
976/* always caled under BH & queue lock */
Stephen Hemminger87990462006-08-10 23:35:16 -0700977static void htb_reset(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 struct htb_sched *q = qdisc_priv(sch);
980 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 for (i = 0; i < HTB_HSIZE; i++) {
Stephen Hemminger0cef2962006-08-10 23:35:38 -0700983 struct hlist_node *p;
984 struct htb_class *cl;
985
986 hlist_for_each_entry(cl, p, q->hash + i, hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (cl->level)
Stephen Hemminger87990462006-08-10 23:35:16 -0700988 memset(&cl->un.inner, 0, sizeof(cl->un.inner));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 else {
Stephen Hemminger87990462006-08-10 23:35:16 -0700990 if (cl->un.leaf.q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 qdisc_reset(cl->un.leaf.q);
992 INIT_LIST_HEAD(&cl->un.leaf.drop_list);
993 }
994 cl->prio_activity = 0;
995 cl->cmode = HTB_CAN_SEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 }
998 }
Patrick McHardyfb983d42007-03-16 01:22:39 -0700999 qdisc_watchdog_cancel(&q->watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 __skb_queue_purge(&q->direct_queue);
1001 sch->q.qlen = 0;
Stephen Hemminger87990462006-08-10 23:35:16 -07001002 memset(q->row, 0, sizeof(q->row));
1003 memset(q->row_mask, 0, sizeof(q->row_mask));
1004 memset(q->wait_pq, 0, sizeof(q->wait_pq));
1005 memset(q->ptr, 0, sizeof(q->ptr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 for (i = 0; i < TC_HTB_NUMPRIO; i++)
Stephen Hemminger87990462006-08-10 23:35:16 -07001007 INIT_LIST_HEAD(q->drops + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Patrick McHardy27a34212008-01-23 20:35:39 -08001010static const struct nla_policy htb_policy[TCA_HTB_MAX + 1] = {
1011 [TCA_HTB_PARMS] = { .len = sizeof(struct tc_htb_opt) },
1012 [TCA_HTB_INIT] = { .len = sizeof(struct tc_htb_glob) },
1013 [TCA_HTB_CTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
1014 [TCA_HTB_RTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
1015};
1016
Patrick McHardy1e904742008-01-22 22:11:17 -08001017static int htb_init(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 struct htb_sched *q = qdisc_priv(sch);
Patrick McHardy1e904742008-01-22 22:11:17 -08001020 struct nlattr *tb[TCA_HTB_INIT + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 struct tc_htb_glob *gopt;
Patrick McHardycee63722008-01-23 20:33:32 -08001022 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 int i;
Patrick McHardycee63722008-01-23 20:33:32 -08001024
1025 if (!opt)
1026 return -EINVAL;
1027
Patrick McHardy27a34212008-01-23 20:35:39 -08001028 err = nla_parse_nested(tb, TCA_HTB_INIT, opt, htb_policy);
Patrick McHardycee63722008-01-23 20:33:32 -08001029 if (err < 0)
1030 return err;
1031
Patrick McHardy27a34212008-01-23 20:35:39 -08001032 if (tb[TCA_HTB_INIT] == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 printk(KERN_ERR "HTB: hey probably you have bad tc tool ?\n");
1034 return -EINVAL;
1035 }
Patrick McHardy1e904742008-01-22 22:11:17 -08001036 gopt = nla_data(tb[TCA_HTB_INIT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (gopt->version != HTB_VER >> 16) {
Stephen Hemminger87990462006-08-10 23:35:16 -07001038 printk(KERN_ERR
1039 "HTB: need tc/htb version %d (minor is %d), you have %d\n",
1040 HTB_VER >> 16, HTB_VER & 0xffff, gopt->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return -EINVAL;
1042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 INIT_LIST_HEAD(&q->root);
1045 for (i = 0; i < HTB_HSIZE; i++)
Stephen Hemminger0cef2962006-08-10 23:35:38 -07001046 INIT_HLIST_HEAD(q->hash + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 for (i = 0; i < TC_HTB_NUMPRIO; i++)
Stephen Hemminger87990462006-08-10 23:35:16 -07001048 INIT_LIST_HEAD(q->drops + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Patrick McHardyfb983d42007-03-16 01:22:39 -07001050 qdisc_watchdog_init(&q->watchdog, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 skb_queue_head_init(&q->direct_queue);
1052
1053 q->direct_qlen = sch->dev->tx_queue_len;
Stephen Hemminger87990462006-08-10 23:35:16 -07001054 if (q->direct_qlen < 2) /* some devices have zero tx_queue_len */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 q->direct_qlen = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if ((q->rate2quantum = gopt->rate2quantum) < 1)
1058 q->rate2quantum = 1;
1059 q->defcls = gopt->defcls;
1060
1061 return 0;
1062}
1063
1064static int htb_dump(struct Qdisc *sch, struct sk_buff *skb)
1065{
1066 struct htb_sched *q = qdisc_priv(sch);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001067 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 struct tc_htb_glob gopt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001070 spin_lock_bh(&sch->dev->queue_lock);
1071
1072 gopt.direct_pkts = q->direct_pkts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 gopt.version = HTB_VER;
1074 gopt.rate2quantum = q->rate2quantum;
1075 gopt.defcls = q->defcls;
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001076 gopt.debug = 0;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001077
1078 nest = nla_nest_start(skb, TCA_OPTIONS);
1079 if (nest == NULL)
1080 goto nla_put_failure;
Patrick McHardy1e904742008-01-22 22:11:17 -08001081 NLA_PUT(skb, TCA_HTB_INIT, sizeof(gopt), &gopt);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001082 nla_nest_end(skb, nest);
1083
Stephen Hemminger9ac961e2006-08-10 23:33:16 -07001084 spin_unlock_bh(&sch->dev->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return skb->len;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001086
Patrick McHardy1e904742008-01-22 22:11:17 -08001087nla_put_failure:
Stephen Hemminger9ac961e2006-08-10 23:33:16 -07001088 spin_unlock_bh(&sch->dev->queue_lock);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001089 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return -1;
1091}
1092
1093static int htb_dump_class(struct Qdisc *sch, unsigned long arg,
Stephen Hemminger87990462006-08-10 23:35:16 -07001094 struct sk_buff *skb, struct tcmsg *tcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Stephen Hemminger87990462006-08-10 23:35:16 -07001096 struct htb_class *cl = (struct htb_class *)arg;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001097 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 struct tc_htb_opt opt;
1099
Stephen Hemminger9ac961e2006-08-10 23:33:16 -07001100 spin_lock_bh(&sch->dev->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 tcm->tcm_parent = cl->parent ? cl->parent->classid : TC_H_ROOT;
1102 tcm->tcm_handle = cl->classid;
1103 if (!cl->level && cl->un.leaf.q)
1104 tcm->tcm_info = cl->un.leaf.q->handle;
1105
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001106 nest = nla_nest_start(skb, TCA_OPTIONS);
1107 if (nest == NULL)
1108 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Stephen Hemminger87990462006-08-10 23:35:16 -07001110 memset(&opt, 0, sizeof(opt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Stephen Hemminger87990462006-08-10 23:35:16 -07001112 opt.rate = cl->rate->rate;
1113 opt.buffer = cl->buffer;
1114 opt.ceil = cl->ceil->rate;
1115 opt.cbuffer = cl->cbuffer;
1116 opt.quantum = cl->un.leaf.quantum;
1117 opt.prio = cl->un.leaf.prio;
1118 opt.level = cl->level;
Patrick McHardy1e904742008-01-22 22:11:17 -08001119 NLA_PUT(skb, TCA_HTB_PARMS, sizeof(opt), &opt);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001120
1121 nla_nest_end(skb, nest);
Stephen Hemminger9ac961e2006-08-10 23:33:16 -07001122 spin_unlock_bh(&sch->dev->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return skb->len;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001124
Patrick McHardy1e904742008-01-22 22:11:17 -08001125nla_put_failure:
Stephen Hemminger9ac961e2006-08-10 23:33:16 -07001126 spin_unlock_bh(&sch->dev->queue_lock);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001127 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return -1;
1129}
1130
1131static int
Stephen Hemminger87990462006-08-10 23:35:16 -07001132htb_dump_class_stats(struct Qdisc *sch, unsigned long arg, struct gnet_dump *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Stephen Hemminger87990462006-08-10 23:35:16 -07001134 struct htb_class *cl = (struct htb_class *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (!cl->level && cl->un.leaf.q)
1137 cl->qstats.qlen = cl->un.leaf.q->q.qlen;
1138 cl->xstats.tokens = cl->tokens;
1139 cl->xstats.ctokens = cl->ctokens;
1140
1141 if (gnet_stats_copy_basic(d, &cl->bstats) < 0 ||
1142 gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
1143 gnet_stats_copy_queue(d, &cl->qstats) < 0)
1144 return -1;
1145
1146 return gnet_stats_copy_app(d, &cl->xstats, sizeof(cl->xstats));
1147}
1148
1149static int htb_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
Stephen Hemminger87990462006-08-10 23:35:16 -07001150 struct Qdisc **old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Stephen Hemminger87990462006-08-10 23:35:16 -07001152 struct htb_class *cl = (struct htb_class *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 if (cl && !cl->level) {
Patrick McHardy9f9afec2006-11-29 17:35:18 -08001155 if (new == NULL &&
1156 (new = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001157 cl->classid))
Stephen Hemminger87990462006-08-10 23:35:16 -07001158 == NULL)
1159 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 sch_tree_lock(sch);
1161 if ((*old = xchg(&cl->un.leaf.q, new)) != NULL) {
Patrick McHardy256d61b2006-11-29 17:37:05 -08001162 qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 qdisc_reset(*old);
1164 }
1165 sch_tree_unlock(sch);
1166 return 0;
1167 }
1168 return -ENOENT;
1169}
1170
Stephen Hemminger87990462006-08-10 23:35:16 -07001171static struct Qdisc *htb_leaf(struct Qdisc *sch, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Stephen Hemminger87990462006-08-10 23:35:16 -07001173 struct htb_class *cl = (struct htb_class *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return (cl && !cl->level) ? cl->un.leaf.q : NULL;
1175}
1176
Patrick McHardy256d61b2006-11-29 17:37:05 -08001177static void htb_qlen_notify(struct Qdisc *sch, unsigned long arg)
1178{
1179 struct htb_class *cl = (struct htb_class *)arg;
1180
1181 if (cl->un.leaf.q->q.qlen == 0)
1182 htb_deactivate(qdisc_priv(sch), cl);
1183}
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185static unsigned long htb_get(struct Qdisc *sch, u32 classid)
1186{
Stephen Hemminger87990462006-08-10 23:35:16 -07001187 struct htb_class *cl = htb_find(classid, sch);
1188 if (cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 cl->refcnt++;
1190 return (unsigned long)cl;
1191}
1192
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001193static inline int htb_parent_last_child(struct htb_class *cl)
1194{
1195 if (!cl->parent)
1196 /* the root class */
1197 return 0;
1198
1199 if (!(cl->parent->children.next == &cl->sibling &&
1200 cl->parent->children.prev == &cl->sibling))
1201 /* not the last child */
1202 return 0;
1203
1204 return 1;
1205}
1206
Jarek Poplawski3ba08b02008-05-03 20:46:29 -07001207static void htb_parent_to_leaf(struct htb_sched *q, struct htb_class *cl,
1208 struct Qdisc *new_q)
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001209{
1210 struct htb_class *parent = cl->parent;
1211
1212 BUG_TRAP(!cl->level && cl->un.leaf.q && !cl->prio_activity);
1213
Jarek Poplawski3ba08b02008-05-03 20:46:29 -07001214 if (parent->cmode != HTB_CAN_SEND)
1215 htb_safe_rb_erase(&parent->pq_node, q->wait_pq + parent->level);
1216
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001217 parent->level = 0;
1218 memset(&parent->un.inner, 0, sizeof(parent->un.inner));
1219 INIT_LIST_HEAD(&parent->un.leaf.drop_list);
1220 parent->un.leaf.q = new_q ? new_q : &noop_qdisc;
1221 parent->un.leaf.quantum = parent->quantum;
1222 parent->un.leaf.prio = parent->prio;
1223 parent->tokens = parent->buffer;
1224 parent->ctokens = parent->cbuffer;
Patrick McHardy3bebcda2007-03-23 11:29:25 -07001225 parent->t_c = psched_get_time();
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001226 parent->cmode = HTB_CAN_SEND;
1227}
1228
Stephen Hemminger87990462006-08-10 23:35:16 -07001229static void htb_destroy_class(struct Qdisc *sch, struct htb_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 struct htb_sched *q = qdisc_priv(sch);
Patrick McHardy814a175e2006-11-29 17:34:50 -08001232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (!cl->level) {
1234 BUG_TRAP(cl->un.leaf.q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 qdisc_destroy(cl->un.leaf.q);
1236 }
Patrick McHardyee39e102007-07-02 22:48:13 -07001237 gen_kill_estimator(&cl->bstats, &cl->rate_est);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 qdisc_put_rtab(cl->rate);
1239 qdisc_put_rtab(cl->ceil);
Stephen Hemminger87990462006-08-10 23:35:16 -07001240
Patrick McHardyff31ab52008-07-01 19:52:38 -07001241 tcf_destroy_chain(&cl->filter_list);
Stephen Hemminger87990462006-08-10 23:35:16 -07001242
1243 while (!list_empty(&cl->children))
1244 htb_destroy_class(sch, list_entry(cl->children.next,
1245 struct htb_class, sibling));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 /* note: this delete may happen twice (see htb_delete) */
Stephen Hemmingerda33e3e2006-11-07 14:54:46 -08001248 hlist_del_init(&cl->hlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 list_del(&cl->sibling);
Stephen Hemminger87990462006-08-10 23:35:16 -07001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (cl->prio_activity)
Stephen Hemminger87990462006-08-10 23:35:16 -07001252 htb_deactivate(q, cl);
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (cl->cmode != HTB_CAN_SEND)
Stephen Hemminger3696f622006-08-10 23:36:01 -07001255 htb_safe_rb_erase(&cl->pq_node, q->wait_pq + cl->level);
Stephen Hemminger87990462006-08-10 23:35:16 -07001256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 kfree(cl);
1258}
1259
1260/* always caled under BH & queue lock */
Stephen Hemminger87990462006-08-10 23:35:16 -07001261static void htb_destroy(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 struct htb_sched *q = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Patrick McHardyfb983d42007-03-16 01:22:39 -07001265 qdisc_watchdog_cancel(&q->watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /* This line used to be after htb_destroy_class call below
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001267 and surprisingly it worked in 2.4. But it must precede it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 because filter need its target class alive to be able to call
1269 unbind_filter on it (without Oops). */
Patrick McHardyff31ab52008-07-01 19:52:38 -07001270 tcf_destroy_chain(&q->filter_list);
Stephen Hemminger87990462006-08-10 23:35:16 -07001271
1272 while (!list_empty(&q->root))
1273 htb_destroy_class(sch, list_entry(q->root.next,
1274 struct htb_class, sibling));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 __skb_queue_purge(&q->direct_queue);
1277}
1278
1279static int htb_delete(struct Qdisc *sch, unsigned long arg)
1280{
1281 struct htb_sched *q = qdisc_priv(sch);
Stephen Hemminger87990462006-08-10 23:35:16 -07001282 struct htb_class *cl = (struct htb_class *)arg;
Patrick McHardy256d61b2006-11-29 17:37:05 -08001283 unsigned int qlen;
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001284 struct Qdisc *new_q = NULL;
1285 int last_child = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 // TODO: why don't allow to delete subtree ? references ? does
1288 // tc subsys quarantee us that in htb_destroy it holds no class
1289 // refs so that we can remove children safely there ?
1290 if (!list_empty(&cl->children) || cl->filter_cnt)
1291 return -EBUSY;
Stephen Hemminger87990462006-08-10 23:35:16 -07001292
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001293 if (!cl->level && htb_parent_last_child(cl)) {
1294 new_q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
1295 cl->parent->classid);
1296 last_child = 1;
1297 }
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 sch_tree_lock(sch);
Stephen Hemminger87990462006-08-10 23:35:16 -07001300
Patrick McHardy814a175e2006-11-29 17:34:50 -08001301 if (!cl->level) {
Patrick McHardy256d61b2006-11-29 17:37:05 -08001302 qlen = cl->un.leaf.q->q.qlen;
Patrick McHardy814a175e2006-11-29 17:34:50 -08001303 qdisc_reset(cl->un.leaf.q);
Patrick McHardy256d61b2006-11-29 17:37:05 -08001304 qdisc_tree_decrease_qlen(cl->un.leaf.q, qlen);
Patrick McHardy814a175e2006-11-29 17:34:50 -08001305 }
1306
Patrick McHardyc38c83c2007-03-27 14:04:24 -07001307 /* delete from hash and active; remainder in destroy_class */
1308 hlist_del_init(&cl->hlist);
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (cl->prio_activity)
Stephen Hemminger87990462006-08-10 23:35:16 -07001311 htb_deactivate(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001313 if (last_child)
Jarek Poplawski3ba08b02008-05-03 20:46:29 -07001314 htb_parent_to_leaf(q, cl, new_q);
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (--cl->refcnt == 0)
Stephen Hemminger87990462006-08-10 23:35:16 -07001317 htb_destroy_class(sch, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 sch_tree_unlock(sch);
1320 return 0;
1321}
1322
1323static void htb_put(struct Qdisc *sch, unsigned long arg)
1324{
Stephen Hemminger87990462006-08-10 23:35:16 -07001325 struct htb_class *cl = (struct htb_class *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 if (--cl->refcnt == 0)
Stephen Hemminger87990462006-08-10 23:35:16 -07001328 htb_destroy_class(sch, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
Stephen Hemminger87990462006-08-10 23:35:16 -07001331static int htb_change_class(struct Qdisc *sch, u32 classid,
Patrick McHardy1e904742008-01-22 22:11:17 -08001332 u32 parentid, struct nlattr **tca,
Stephen Hemminger87990462006-08-10 23:35:16 -07001333 unsigned long *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 int err = -EINVAL;
1336 struct htb_sched *q = qdisc_priv(sch);
Stephen Hemminger87990462006-08-10 23:35:16 -07001337 struct htb_class *cl = (struct htb_class *)*arg, *parent;
Patrick McHardy1e904742008-01-22 22:11:17 -08001338 struct nlattr *opt = tca[TCA_OPTIONS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 struct qdisc_rate_table *rtab = NULL, *ctab = NULL;
Patrick McHardy1e904742008-01-22 22:11:17 -08001340 struct nlattr *tb[TCA_HTB_RTAB + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 struct tc_htb_opt *hopt;
1342
1343 /* extract all subattrs from opt attr */
Patrick McHardycee63722008-01-23 20:33:32 -08001344 if (!opt)
1345 goto failure;
1346
Patrick McHardy27a34212008-01-23 20:35:39 -08001347 err = nla_parse_nested(tb, TCA_HTB_RTAB, opt, htb_policy);
Patrick McHardycee63722008-01-23 20:33:32 -08001348 if (err < 0)
1349 goto failure;
1350
1351 err = -EINVAL;
Patrick McHardy27a34212008-01-23 20:35:39 -08001352 if (tb[TCA_HTB_PARMS] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Stephen Hemminger87990462006-08-10 23:35:16 -07001355 parent = parentid == TC_H_ROOT ? NULL : htb_find(parentid, sch);
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001356
Patrick McHardy1e904742008-01-22 22:11:17 -08001357 hopt = nla_data(tb[TCA_HTB_PARMS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Patrick McHardy1e904742008-01-22 22:11:17 -08001359 rtab = qdisc_get_rtab(&hopt->rate, tb[TCA_HTB_RTAB]);
1360 ctab = qdisc_get_rtab(&hopt->ceil, tb[TCA_HTB_CTAB]);
Stephen Hemminger87990462006-08-10 23:35:16 -07001361 if (!rtab || !ctab)
1362 goto failure;
1363
1364 if (!cl) { /* new class */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 struct Qdisc *new_q;
Stephen Hemminger3696f622006-08-10 23:36:01 -07001366 int prio;
Patrick McHardyee39e102007-07-02 22:48:13 -07001367 struct {
Patrick McHardy1e904742008-01-22 22:11:17 -08001368 struct nlattr nla;
Patrick McHardyee39e102007-07-02 22:48:13 -07001369 struct gnet_estimator opt;
1370 } est = {
Patrick McHardy1e904742008-01-22 22:11:17 -08001371 .nla = {
1372 .nla_len = nla_attr_size(sizeof(est.opt)),
1373 .nla_type = TCA_RATE,
Patrick McHardyee39e102007-07-02 22:48:13 -07001374 },
1375 .opt = {
1376 /* 4s interval, 16s averaging constant */
1377 .interval = 2,
1378 .ewma_log = 2,
1379 },
1380 };
Stephen Hemminger3696f622006-08-10 23:36:01 -07001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 /* check for valid classid */
Stephen Hemminger87990462006-08-10 23:35:16 -07001383 if (!classid || TC_H_MAJ(classid ^ sch->handle)
1384 || htb_find(classid, sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 goto failure;
1386
1387 /* check maximal depth */
1388 if (parent && parent->parent && parent->parent->level < 2) {
1389 printk(KERN_ERR "htb: tree is too deep\n");
1390 goto failure;
1391 }
1392 err = -ENOBUFS;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001393 if ((cl = kzalloc(sizeof(*cl), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 goto failure;
Stephen Hemminger87990462006-08-10 23:35:16 -07001395
Patrick McHardyee39e102007-07-02 22:48:13 -07001396 gen_new_estimator(&cl->bstats, &cl->rate_est,
1397 &sch->dev->queue_lock,
Patrick McHardy1e904742008-01-22 22:11:17 -08001398 tca[TCA_RATE] ? : &est.nla);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 cl->refcnt = 1;
1400 INIT_LIST_HEAD(&cl->sibling);
Stephen Hemminger0cef2962006-08-10 23:35:38 -07001401 INIT_HLIST_NODE(&cl->hlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 INIT_LIST_HEAD(&cl->children);
1403 INIT_LIST_HEAD(&cl->un.leaf.drop_list);
Stephen Hemminger3696f622006-08-10 23:36:01 -07001404 RB_CLEAR_NODE(&cl->pq_node);
1405
1406 for (prio = 0; prio < TC_HTB_NUMPRIO; prio++)
1407 RB_CLEAR_NODE(&cl->node[prio]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 /* create leaf qdisc early because it uses kmalloc(GFP_KERNEL)
1410 so that can't be used inside of sch_tree_lock
1411 -- thanks to Karlis Peisenieks */
Patrick McHardy9f9afec2006-11-29 17:35:18 -08001412 new_q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops, classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 sch_tree_lock(sch);
1414 if (parent && !parent->level) {
Patrick McHardy256d61b2006-11-29 17:37:05 -08001415 unsigned int qlen = parent->un.leaf.q->q.qlen;
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 /* turn parent into inner node */
Patrick McHardy256d61b2006-11-29 17:37:05 -08001418 qdisc_reset(parent->un.leaf.q);
1419 qdisc_tree_decrease_qlen(parent->un.leaf.q, qlen);
Stephen Hemminger87990462006-08-10 23:35:16 -07001420 qdisc_destroy(parent->un.leaf.q);
1421 if (parent->prio_activity)
1422 htb_deactivate(q, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 /* remove from evt list because of level change */
1425 if (parent->cmode != HTB_CAN_SEND) {
Stephen Hemminger3696f622006-08-10 23:36:01 -07001426 htb_safe_rb_erase(&parent->pq_node, q->wait_pq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 parent->cmode = HTB_CAN_SEND;
1428 }
1429 parent->level = (parent->parent ? parent->parent->level
Stephen Hemminger87990462006-08-10 23:35:16 -07001430 : TC_HTB_MAXDEPTH) - 1;
1431 memset(&parent->un.inner, 0, sizeof(parent->un.inner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
1433 /* leaf (we) needs elementary qdisc */
1434 cl->un.leaf.q = new_q ? new_q : &noop_qdisc;
1435
Stephen Hemminger87990462006-08-10 23:35:16 -07001436 cl->classid = classid;
1437 cl->parent = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 /* set class to be in HTB_CAN_SEND state */
1440 cl->tokens = hopt->buffer;
1441 cl->ctokens = hopt->cbuffer;
Patrick McHardy00c04af2007-03-16 01:23:02 -07001442 cl->mbuffer = 60 * PSCHED_TICKS_PER_SEC; /* 1min */
Patrick McHardy3bebcda2007-03-23 11:29:25 -07001443 cl->t_c = psched_get_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 cl->cmode = HTB_CAN_SEND;
1445
1446 /* attach to the hash list and parent's family */
Stephen Hemminger0cef2962006-08-10 23:35:38 -07001447 hlist_add_head(&cl->hlist, q->hash + htb_hash(classid));
Stephen Hemminger87990462006-08-10 23:35:16 -07001448 list_add_tail(&cl->sibling,
1449 parent ? &parent->children : &q->root);
Patrick McHardyee39e102007-07-02 22:48:13 -07001450 } else {
Patrick McHardy1e904742008-01-22 22:11:17 -08001451 if (tca[TCA_RATE])
Patrick McHardyee39e102007-07-02 22:48:13 -07001452 gen_replace_estimator(&cl->bstats, &cl->rate_est,
1453 &sch->dev->queue_lock,
Patrick McHardy1e904742008-01-22 22:11:17 -08001454 tca[TCA_RATE]);
Stephen Hemminger87990462006-08-10 23:35:16 -07001455 sch_tree_lock(sch);
Patrick McHardyee39e102007-07-02 22:48:13 -07001456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 /* it used to be a nasty bug here, we have to check that node
Stephen Hemminger87990462006-08-10 23:35:16 -07001459 is really leaf before changing cl->un.leaf ! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (!cl->level) {
1461 cl->un.leaf.quantum = rtab->rate.rate / q->rate2quantum;
1462 if (!hopt->quantum && cl->un.leaf.quantum < 1000) {
Stephen Hemminger87990462006-08-10 23:35:16 -07001463 printk(KERN_WARNING
1464 "HTB: quantum of class %X is small. Consider r2q change.\n",
1465 cl->classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 cl->un.leaf.quantum = 1000;
1467 }
1468 if (!hopt->quantum && cl->un.leaf.quantum > 200000) {
Stephen Hemminger87990462006-08-10 23:35:16 -07001469 printk(KERN_WARNING
1470 "HTB: quantum of class %X is big. Consider r2q change.\n",
1471 cl->classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 cl->un.leaf.quantum = 200000;
1473 }
1474 if (hopt->quantum)
1475 cl->un.leaf.quantum = hopt->quantum;
1476 if ((cl->un.leaf.prio = hopt->prio) >= TC_HTB_NUMPRIO)
1477 cl->un.leaf.prio = TC_HTB_NUMPRIO - 1;
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001478
1479 /* backup for htb_parent_to_leaf */
1480 cl->quantum = cl->un.leaf.quantum;
1481 cl->prio = cl->un.leaf.prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
1483
1484 cl->buffer = hopt->buffer;
1485 cl->cbuffer = hopt->cbuffer;
Stephen Hemminger87990462006-08-10 23:35:16 -07001486 if (cl->rate)
1487 qdisc_put_rtab(cl->rate);
1488 cl->rate = rtab;
1489 if (cl->ceil)
1490 qdisc_put_rtab(cl->ceil);
1491 cl->ceil = ctab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 sch_tree_unlock(sch);
1493
1494 *arg = (unsigned long)cl;
1495 return 0;
1496
1497failure:
Stephen Hemminger87990462006-08-10 23:35:16 -07001498 if (rtab)
1499 qdisc_put_rtab(rtab);
1500 if (ctab)
1501 qdisc_put_rtab(ctab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return err;
1503}
1504
1505static struct tcf_proto **htb_find_tcf(struct Qdisc *sch, unsigned long arg)
1506{
1507 struct htb_sched *q = qdisc_priv(sch);
1508 struct htb_class *cl = (struct htb_class *)arg;
1509 struct tcf_proto **fl = cl ? &cl->filter_list : &q->filter_list;
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return fl;
1512}
1513
1514static unsigned long htb_bind_filter(struct Qdisc *sch, unsigned long parent,
Stephen Hemminger87990462006-08-10 23:35:16 -07001515 u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
1517 struct htb_sched *q = qdisc_priv(sch);
Stephen Hemminger87990462006-08-10 23:35:16 -07001518 struct htb_class *cl = htb_find(classid, sch);
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 /*if (cl && !cl->level) return 0;
Stephen Hemminger87990462006-08-10 23:35:16 -07001521 The line above used to be there to prevent attaching filters to
1522 leaves. But at least tc_index filter uses this just to get class
1523 for other reasons so that we have to allow for it.
1524 ----
1525 19.6.2002 As Werner explained it is ok - bind filter is just
1526 another way to "lock" the class - unlike "get" this lock can
1527 be broken by class during destroy IIUC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 */
Stephen Hemminger87990462006-08-10 23:35:16 -07001529 if (cl)
1530 cl->filter_cnt++;
1531 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 q->filter_cnt++;
1533 return (unsigned long)cl;
1534}
1535
1536static void htb_unbind_filter(struct Qdisc *sch, unsigned long arg)
1537{
1538 struct htb_sched *q = qdisc_priv(sch);
1539 struct htb_class *cl = (struct htb_class *)arg;
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001540
Stephen Hemminger87990462006-08-10 23:35:16 -07001541 if (cl)
1542 cl->filter_cnt--;
1543 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 q->filter_cnt--;
1545}
1546
1547static void htb_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1548{
1549 struct htb_sched *q = qdisc_priv(sch);
1550 int i;
1551
1552 if (arg->stop)
1553 return;
1554
1555 for (i = 0; i < HTB_HSIZE; i++) {
Stephen Hemminger0cef2962006-08-10 23:35:38 -07001556 struct hlist_node *p;
1557 struct htb_class *cl;
1558
1559 hlist_for_each_entry(cl, p, q->hash + i, hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (arg->count < arg->skip) {
1561 arg->count++;
1562 continue;
1563 }
1564 if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
1565 arg->stop = 1;
1566 return;
1567 }
1568 arg->count++;
1569 }
1570 }
1571}
1572
Eric Dumazet20fea082007-11-14 01:44:41 -08001573static const struct Qdisc_class_ops htb_class_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 .graft = htb_graft,
1575 .leaf = htb_leaf,
Patrick McHardy256d61b2006-11-29 17:37:05 -08001576 .qlen_notify = htb_qlen_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 .get = htb_get,
1578 .put = htb_put,
1579 .change = htb_change_class,
1580 .delete = htb_delete,
1581 .walk = htb_walk,
1582 .tcf_chain = htb_find_tcf,
1583 .bind_tcf = htb_bind_filter,
1584 .unbind_tcf = htb_unbind_filter,
1585 .dump = htb_dump_class,
1586 .dump_stats = htb_dump_class_stats,
1587};
1588
Eric Dumazet20fea082007-11-14 01:44:41 -08001589static struct Qdisc_ops htb_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 .next = NULL,
1591 .cl_ops = &htb_class_ops,
1592 .id = "htb",
1593 .priv_size = sizeof(struct htb_sched),
1594 .enqueue = htb_enqueue,
1595 .dequeue = htb_dequeue,
1596 .requeue = htb_requeue,
1597 .drop = htb_drop,
1598 .init = htb_init,
1599 .reset = htb_reset,
1600 .destroy = htb_destroy,
1601 .change = NULL /* htb_change */,
1602 .dump = htb_dump,
1603 .owner = THIS_MODULE,
1604};
1605
1606static int __init htb_module_init(void)
1607{
Stephen Hemminger87990462006-08-10 23:35:16 -07001608 return register_qdisc(&htb_qdisc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
Stephen Hemminger87990462006-08-10 23:35:16 -07001610static void __exit htb_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
Stephen Hemminger87990462006-08-10 23:35:16 -07001612 unregister_qdisc(&htb_qdisc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
Stephen Hemminger87990462006-08-10 23:35:16 -07001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615module_init(htb_module_init)
1616module_exit(htb_module_exit)
1617MODULE_LICENSE("GPL");