blob: 7954e73d118ac6857a33ab1a49da4160acdae3f4 [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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
Jesper Dangaard Brouer47083fc2008-06-16 16:39:32 -070029#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/types.h>
31#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/skbuff.h>
35#include <linux/list.h>
36#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/rbtree.h>
Jarek Poplawski12247362009-02-01 01:13:22 -080038#include <linux/workqueue.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Patrick McHardy0ba48052007-07-02 22:49:07 -070040#include <net/netlink.h>
Jiri Pirko292f1c72013-02-12 00:12:03 +000041#include <net/sch_generic.h>
Patrick McHardy0ba48052007-07-02 22:49:07 -070042#include <net/pkt_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/* HTB algorithm.
45 Author: devik@cdi.cz
46 ========================================================================
47 HTB is like TBF with multiple classes. It is also similar to CBQ because
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090048 it allows to assign priority to each class in hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 In fact it is another implementation of Floyd's formal sharing.
50
51 Levels:
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090052 Each class is assigned level. Leaf has ALWAYS level 0 and root
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 classes have level TC_HTB_MAXDEPTH-1. Interior nodes has level
54 one less than their parent.
55*/
56
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
Eric Dumazet64153ce2013-06-06 14:53:16 -070068static int htb_rate_est = 0; /* htb classes have a default rate estimator */
69module_param(htb_rate_est, int, 0640);
70MODULE_PARM_DESC(htb_rate_est, "setup a default rate estimator (4sec 16sec) for htb classes");
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* used internaly to keep status of single class */
73enum htb_cmode {
Stephen Hemminger87990462006-08-10 23:35:16 -070074 HTB_CANT_SEND, /* class can't send and can't borrow */
75 HTB_MAY_BORROW, /* class can't send but may borrow */
76 HTB_CAN_SEND /* class can send */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
Eric Dumazetca4ec902013-06-13 07:58:30 -070079/* interior & leaf nodes; props specific to leaves are marked L:
80 * To reduce false sharing, place mostly read fields at beginning,
81 * and mostly written ones at the end.
82 */
Stephen Hemminger87990462006-08-10 23:35:16 -070083struct htb_class {
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -070084 struct Qdisc_class_common common;
Eric Dumazetca4ec902013-06-13 07:58:30 -070085 struct psched_ratecfg rate;
86 struct psched_ratecfg ceil;
87 s64 buffer, cbuffer;/* token bucket depth/rate */
88 s64 mbuffer; /* max wait time */
89 int prio; /* these two are used only by leaves... */
90 int quantum; /* but stored for parent-to-leaf return */
91
92 struct tcf_proto *filter_list; /* class attached filters */
93 int filter_cnt;
94 int refcnt; /* usage count of this class */
95
96 int level; /* our level (see above) */
97 unsigned int children;
98 struct htb_class *parent; /* parent class */
99
Eric Dumazet45203a32013-06-06 08:43:22 -0700100 struct gnet_stats_rate_est64 rate_est;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Eric Dumazetca4ec902013-06-13 07:58:30 -0700102 /*
103 * Written often fields
104 */
105 struct gnet_stats_basic_packed bstats;
106 struct gnet_stats_queue qstats;
107 struct tc_htb_xstats xstats; /* our special stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Eric Dumazetca4ec902013-06-13 07:58:30 -0700109 /* token bucket parameters */
110 s64 tokens, ctokens;/* current number of tokens */
111 s64 t_c; /* checkpoint time */
Jarek Poplawskic19f7a32008-12-03 21:09:45 -0800112
Stephen Hemminger87990462006-08-10 23:35:16 -0700113 union {
114 struct htb_class_leaf {
115 struct Qdisc *q;
Stephen Hemminger87990462006-08-10 23:35:16 -0700116 int deficit[TC_HTB_MAXDEPTH];
117 struct list_head drop_list;
118 } leaf;
119 struct htb_class_inner {
120 struct rb_root feed[TC_HTB_NUMPRIO]; /* feed trees */
121 struct rb_node *ptr[TC_HTB_NUMPRIO]; /* current class ptr */
122 /* When class changes from state 1->2 and disconnects from
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000123 * parent's feed then we lost ptr value and start from the
124 * first child again. Here we store classid of the
125 * last valid ptr (used when ptr is NULL).
126 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700127 u32 last_ptr_id[TC_HTB_NUMPRIO];
128 } inner;
129 } un;
Eric Dumazetca4ec902013-06-13 07:58:30 -0700130 s64 pq_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Eric Dumazetca4ec902013-06-13 07:58:30 -0700132 int prio_activity; /* for which prios are we active */
133 enum htb_cmode cmode; /* current mode of the class */
134 struct rb_node pq_node; /* node for event queue */
135 struct rb_node node[TC_HTB_NUMPRIO]; /* node for self or feed tree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
Stephen Hemminger87990462006-08-10 23:35:16 -0700138struct htb_sched {
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700139 struct Qdisc_class_hash clhash;
Stephen Hemminger0cef2962006-08-10 23:35:38 -0700140 struct list_head drops[TC_HTB_NUMPRIO];/* active leaves (for drops) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Stephen Hemminger87990462006-08-10 23:35:16 -0700142 /* self list - roots of self generating tree */
143 struct rb_root row[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
144 int row_mask[TC_HTB_MAXDEPTH];
145 struct rb_node *ptr[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
146 u32 last_ptr_id[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Stephen Hemminger87990462006-08-10 23:35:16 -0700148 /* self wait list - roots of wait PQs per row */
149 struct rb_root wait_pq[TC_HTB_MAXDEPTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Stephen Hemminger87990462006-08-10 23:35:16 -0700151 /* time of nearest event per level (row) */
Eric Dumazet5343a7f2013-06-04 07:11:48 +0000152 s64 near_ev_cache[TC_HTB_MAXDEPTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Stephen Hemminger87990462006-08-10 23:35:16 -0700154 int defcls; /* class where unclassified flows go to */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Stephen Hemminger87990462006-08-10 23:35:16 -0700156 /* filters for qdisc itself */
157 struct tcf_proto *filter_list;
Stephen Hemminger87990462006-08-10 23:35:16 -0700158
Eric Dumazet5343a7f2013-06-04 07:11:48 +0000159 int rate2quantum; /* quant = rate / rate2quantum */
160 s64 now; /* cached dequeue time */
Patrick McHardyfb983d42007-03-16 01:22:39 -0700161 struct qdisc_watchdog watchdog;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Stephen Hemminger87990462006-08-10 23:35:16 -0700163 /* non shaped skbs; let them go directly thru */
164 struct sk_buff_head direct_queue;
165 int direct_qlen; /* max qlen of above */
166
167 long direct_pkts;
Jarek Poplawskie82181d2009-02-01 01:13:05 -0800168
169#define HTB_WARN_TOOMANYEVENTS 0x1
170 unsigned int warned; /* only one warning */
Jarek Poplawski12247362009-02-01 01:13:22 -0800171 struct work_struct work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172};
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/* find class in global hash table using given handle */
Stephen Hemminger87990462006-08-10 23:35:16 -0700175static inline struct htb_class *htb_find(u32 handle, struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct htb_sched *q = qdisc_priv(sch);
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700178 struct Qdisc_class_common *clc;
Stephen Hemminger0cef2962006-08-10 23:35:38 -0700179
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700180 clc = qdisc_class_find(&q->clhash, handle);
181 if (clc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return NULL;
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700183 return container_of(clc, struct htb_class, common);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186/**
187 * htb_classify - classify a packet into class
188 *
189 * It returns NULL if the packet should be dropped or -1 if the packet
190 * should be passed directly thru. In all other cases leaf class is returned.
191 * We allow direct class selection by classid in priority. The we examine
192 * filters in qdisc and in inner nodes (if higher filter points to the inner
193 * node). If we end up with classid MAJOR:0 we enqueue the skb into special
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900194 * internal fifo (direct). These packets then go directly thru. If we still
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300195 * have no valid leaf we try to use MAJOR:default leaf. It still unsuccessful
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * then finish and return direct queue.
197 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000198#define HTB_DIRECT ((struct htb_class *)-1L)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Stephen Hemminger87990462006-08-10 23:35:16 -0700200static struct htb_class *htb_classify(struct sk_buff *skb, struct Qdisc *sch,
201 int *qerr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 struct htb_sched *q = qdisc_priv(sch);
204 struct htb_class *cl;
205 struct tcf_result res;
206 struct tcf_proto *tcf;
207 int result;
208
209 /* allow to select class by setting skb->priority to valid classid;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000210 * note that nfmark can be used too by attaching filter fw with no
211 * rules in it
212 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (skb->priority == sch->handle)
Stephen Hemminger87990462006-08-10 23:35:16 -0700214 return HTB_DIRECT; /* X:0 (direct flow) selected */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000215 cl = htb_find(skb->priority, sch);
216 if (cl && cl->level == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return cl;
218
Jarek Poplawskic27f3392008-08-04 22:39:11 -0700219 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 tcf = q->filter_list;
221 while (tcf && (result = tc_classify(skb, tcf, &res)) >= 0) {
222#ifdef CONFIG_NET_CLS_ACT
223 switch (result) {
224 case TC_ACT_QUEUED:
Stephen Hemminger87990462006-08-10 23:35:16 -0700225 case TC_ACT_STOLEN:
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700226 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 case TC_ACT_SHOT:
228 return NULL;
229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230#endif
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000231 cl = (void *)res.class;
232 if (!cl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (res.classid == sch->handle)
Stephen Hemminger87990462006-08-10 23:35:16 -0700234 return HTB_DIRECT; /* X:0 (direct flow) */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000235 cl = htb_find(res.classid, sch);
236 if (!cl)
Stephen Hemminger87990462006-08-10 23:35:16 -0700237 break; /* filter selected invalid classid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239 if (!cl->level)
Stephen Hemminger87990462006-08-10 23:35:16 -0700240 return cl; /* we hit leaf; return it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /* we have got inner class; apply inner filter chain */
243 tcf = cl->filter_list;
244 }
245 /* classification failed; try to use default class */
Stephen Hemminger87990462006-08-10 23:35:16 -0700246 cl = htb_find(TC_H_MAKE(TC_H_MAJ(sch->handle), q->defcls), sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (!cl || cl->level)
Stephen Hemminger87990462006-08-10 23:35:16 -0700248 return HTB_DIRECT; /* bad default .. this is safe bet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return cl;
250}
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252/**
253 * htb_add_to_id_tree - adds class to the round robin list
254 *
255 * Routine adds class to the list (actually tree) sorted by classid.
256 * Make sure that class is not already on such list for given prio.
257 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700258static void htb_add_to_id_tree(struct rb_root *root,
259 struct htb_class *cl, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct rb_node **p = &root->rb_node, *parent = NULL;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 while (*p) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700264 struct htb_class *c;
265 parent = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 c = rb_entry(parent, struct htb_class, node[prio]);
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700267
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700268 if (cl->common.classid > c->common.classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 p = &parent->rb_right;
Stephen Hemminger87990462006-08-10 23:35:16 -0700270 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 p = &parent->rb_left;
272 }
273 rb_link_node(&cl->node[prio], parent, p);
274 rb_insert_color(&cl->node[prio], root);
275}
276
277/**
278 * htb_add_to_wait_tree - adds class to the event queue with delay
279 *
280 * The class is added to priority event queue to indicate that class will
281 * change its mode in cl->pq_key microseconds. Make sure that class is not
282 * already in the queue.
283 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700284static void htb_add_to_wait_tree(struct htb_sched *q,
Vimalkumar56b765b2012-10-31 06:04:11 +0000285 struct htb_class *cl, s64 delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct rb_node **p = &q->wait_pq[cl->level].rb_node, *parent = NULL;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700288
Patrick McHardyfb983d42007-03-16 01:22:39 -0700289 cl->pq_key = q->now + delay;
290 if (cl->pq_key == q->now)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 cl->pq_key++;
292
293 /* update the nearest event cache */
Patrick McHardyfb983d42007-03-16 01:22:39 -0700294 if (q->near_ev_cache[cl->level] > cl->pq_key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 q->near_ev_cache[cl->level] = cl->pq_key;
Stephen Hemminger87990462006-08-10 23:35:16 -0700296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 while (*p) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700298 struct htb_class *c;
299 parent = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 c = rb_entry(parent, struct htb_class, pq_node);
Patrick McHardyfb983d42007-03-16 01:22:39 -0700301 if (cl->pq_key >= c->pq_key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 p = &parent->rb_right;
Stephen Hemminger87990462006-08-10 23:35:16 -0700303 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 p = &parent->rb_left;
305 }
306 rb_link_node(&cl->pq_node, parent, p);
307 rb_insert_color(&cl->pq_node, &q->wait_pq[cl->level]);
308}
309
310/**
311 * htb_next_rb_node - finds next node in binary tree
312 *
313 * When we are past last key we return NULL.
314 * Average complexity is 2 steps per call.
315 */
Stephen Hemminger3696f622006-08-10 23:36:01 -0700316static inline void htb_next_rb_node(struct rb_node **n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 *n = rb_next(*n);
319}
320
321/**
322 * htb_add_class_to_row - add class to its row
323 *
324 * The class is added to row at priorities marked in mask.
325 * It does nothing if mask == 0.
326 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700327static inline void htb_add_class_to_row(struct htb_sched *q,
328 struct htb_class *cl, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 q->row_mask[cl->level] |= mask;
331 while (mask) {
332 int prio = ffz(~mask);
333 mask &= ~(1 << prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700334 htb_add_to_id_tree(q->row[cl->level] + prio, cl, prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336}
337
Stephen Hemminger3696f622006-08-10 23:36:01 -0700338/* If this triggers, it is a bug in this code, but it need not be fatal */
339static void htb_safe_rb_erase(struct rb_node *rb, struct rb_root *root)
340{
Ismail Donmez81771b32006-10-03 13:49:10 -0700341 if (RB_EMPTY_NODE(rb)) {
Stephen Hemminger3696f622006-08-10 23:36:01 -0700342 WARN_ON(1);
343 } else {
344 rb_erase(rb, root);
345 RB_CLEAR_NODE(rb);
346 }
347}
348
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350/**
351 * htb_remove_class_from_row - removes class from its row
352 *
353 * The class is removed from row at priorities marked in mask.
354 * It does nothing if mask == 0.
355 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700356static inline void htb_remove_class_from_row(struct htb_sched *q,
357 struct htb_class *cl, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 int m = 0;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 while (mask) {
362 int prio = ffz(~mask);
Stephen Hemminger3696f622006-08-10 23:36:01 -0700363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 mask &= ~(1 << prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700365 if (q->ptr[cl->level][prio] == cl->node + prio)
366 htb_next_rb_node(q->ptr[cl->level] + prio);
Stephen Hemminger3696f622006-08-10 23:36:01 -0700367
368 htb_safe_rb_erase(cl->node + prio, q->row[cl->level] + prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700369 if (!q->row[cl->level][prio].rb_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 m |= 1 << prio;
371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 q->row_mask[cl->level] &= ~m;
373}
374
375/**
376 * htb_activate_prios - creates active classe's feed chain
377 *
378 * The class is connected to ancestors and/or appropriate rows
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900379 * for priorities it is participating on. cl->cmode must be new
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 * (activated) mode. It does nothing if cl->prio_activity == 0.
381 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700382static void htb_activate_prios(struct htb_sched *q, struct htb_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 struct htb_class *p = cl->parent;
Stephen Hemminger87990462006-08-10 23:35:16 -0700385 long m, mask = cl->prio_activity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 while (cl->cmode == HTB_MAY_BORROW && p && mask) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700388 m = mask;
389 while (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 int prio = ffz(~m);
391 m &= ~(1 << prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (p->un.inner.feed[prio].rb_node)
394 /* parent already has its feed in use so that
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000395 * reset bit in mask as parent is already ok
396 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 mask &= ~(1 << prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700398
399 htb_add_to_id_tree(p->un.inner.feed + prio, cl, prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 p->prio_activity |= mask;
Stephen Hemminger87990462006-08-10 23:35:16 -0700402 cl = p;
403 p = cl->parent;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406 if (cl->cmode == HTB_CAN_SEND && mask)
Stephen Hemminger87990462006-08-10 23:35:16 -0700407 htb_add_class_to_row(q, cl, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
410/**
411 * htb_deactivate_prios - remove class from feed chain
412 *
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900413 * cl->cmode must represent old mode (before deactivation). It does
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 * nothing if cl->prio_activity == 0. Class is removed from all feed
415 * chains and rows.
416 */
417static void htb_deactivate_prios(struct htb_sched *q, struct htb_class *cl)
418{
419 struct htb_class *p = cl->parent;
Stephen Hemminger87990462006-08-10 23:35:16 -0700420 long m, mask = cl->prio_activity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 while (cl->cmode == HTB_MAY_BORROW && p && mask) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700423 m = mask;
424 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 while (m) {
426 int prio = ffz(~m);
427 m &= ~(1 << prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700428
429 if (p->un.inner.ptr[prio] == cl->node + prio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /* we are removing child which is pointed to from
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000431 * parent feed - forget the pointer but remember
432 * classid
433 */
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700434 p->un.inner.last_ptr_id[prio] = cl->common.classid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 p->un.inner.ptr[prio] = NULL;
436 }
Stephen Hemminger87990462006-08-10 23:35:16 -0700437
Stephen Hemminger3696f622006-08-10 23:36:01 -0700438 htb_safe_rb_erase(cl->node + prio, p->un.inner.feed + prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700439
440 if (!p->un.inner.feed[prio].rb_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 mask |= 1 << prio;
442 }
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 p->prio_activity &= ~mask;
Stephen Hemminger87990462006-08-10 23:35:16 -0700445 cl = p;
446 p = cl->parent;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
Stephen Hemminger87990462006-08-10 23:35:16 -0700449 if (cl->cmode == HTB_CAN_SEND && mask)
450 htb_remove_class_from_row(q, cl, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Vimalkumar56b765b2012-10-31 06:04:11 +0000453static inline s64 htb_lowater(const struct htb_class *cl)
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700454{
Jesper Dangaard Brouer47083fc2008-06-16 16:39:32 -0700455 if (htb_hysteresis)
456 return cl->cmode != HTB_CANT_SEND ? -cl->cbuffer : 0;
457 else
458 return 0;
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700459}
Vimalkumar56b765b2012-10-31 06:04:11 +0000460static inline s64 htb_hiwater(const struct htb_class *cl)
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700461{
Jesper Dangaard Brouer47083fc2008-06-16 16:39:32 -0700462 if (htb_hysteresis)
463 return cl->cmode == HTB_CAN_SEND ? -cl->buffer : 0;
464 else
465 return 0;
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700466}
Jesper Dangaard Brouer47083fc2008-06-16 16:39:32 -0700467
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469/**
470 * htb_class_mode - computes and returns current class mode
471 *
472 * It computes cl's mode at time cl->t_c+diff and returns it. If mode
473 * is not HTB_CAN_SEND then cl->pq_key is updated to time difference
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900474 * from now to time when cl will change its state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 * Also it is worth to note that class mode doesn't change simply
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900476 * at cl->{c,}tokens == 0 but there can rather be hysteresis of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 * 0 .. -cl->{c,}buffer range. It is meant to limit number of
478 * mode transitions per time unit. The speed gain is about 1/6.
479 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700480static inline enum htb_cmode
Vimalkumar56b765b2012-10-31 06:04:11 +0000481htb_class_mode(struct htb_class *cl, s64 *diff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Vimalkumar56b765b2012-10-31 06:04:11 +0000483 s64 toks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Stephen Hemminger87990462006-08-10 23:35:16 -0700485 if ((toks = (cl->ctokens + *diff)) < htb_lowater(cl)) {
486 *diff = -toks;
487 return HTB_CANT_SEND;
488 }
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700489
Stephen Hemminger87990462006-08-10 23:35:16 -0700490 if ((toks = (cl->tokens + *diff)) >= htb_hiwater(cl))
491 return HTB_CAN_SEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Stephen Hemminger87990462006-08-10 23:35:16 -0700493 *diff = -toks;
494 return HTB_MAY_BORROW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497/**
498 * htb_change_class_mode - changes classe's mode
499 *
500 * This should be the only way how to change classe's mode under normal
501 * cirsumstances. Routine will update feed lists linkage, change mode
502 * and add class to the wait event queue if appropriate. New mode should
503 * be different from old one and cl->pq_key has to be valid if changing
504 * to mode other than HTB_CAN_SEND (see htb_add_to_wait_tree).
505 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700506static void
Vimalkumar56b765b2012-10-31 06:04:11 +0000507htb_change_class_mode(struct htb_sched *q, struct htb_class *cl, s64 *diff)
Stephen Hemminger87990462006-08-10 23:35:16 -0700508{
509 enum htb_cmode new_mode = htb_class_mode(cl, diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 if (new_mode == cl->cmode)
Stephen Hemminger87990462006-08-10 23:35:16 -0700512 return;
513
514 if (cl->prio_activity) { /* not necessary: speed optimization */
515 if (cl->cmode != HTB_CANT_SEND)
516 htb_deactivate_prios(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 cl->cmode = new_mode;
Stephen Hemminger87990462006-08-10 23:35:16 -0700518 if (new_mode != HTB_CANT_SEND)
519 htb_activate_prios(q, cl);
520 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 cl->cmode = new_mode;
522}
523
524/**
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900525 * htb_activate - inserts leaf cl into appropriate active feeds
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 *
527 * Routine learns (new) priority of leaf and activates feed chain
528 * for the prio. It can be called on already active leaf safely.
529 * It also adds leaf into droplist.
530 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700531static inline void htb_activate(struct htb_sched *q, struct htb_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700533 WARN_ON(cl->level || !cl->un.leaf.q || !cl->un.leaf.q->q.qlen);
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (!cl->prio_activity) {
Jarek Poplawskic19f7a32008-12-03 21:09:45 -0800536 cl->prio_activity = 1 << cl->prio;
Stephen Hemminger87990462006-08-10 23:35:16 -0700537 htb_activate_prios(q, cl);
538 list_add_tail(&cl->un.leaf.drop_list,
Jarek Poplawskic19f7a32008-12-03 21:09:45 -0800539 q->drops + cl->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541}
542
543/**
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900544 * htb_deactivate - remove leaf cl from active feeds
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 *
546 * Make sure that leaf is active. In the other words it can't be called
547 * with non-active leaf. It also removes class from the drop list.
548 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700549static inline void htb_deactivate(struct htb_sched *q, struct htb_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700551 WARN_ON(!cl->prio_activity);
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700552
Stephen Hemminger87990462006-08-10 23:35:16 -0700553 htb_deactivate_prios(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 cl->prio_activity = 0;
555 list_del_init(&cl->un.leaf.drop_list);
556}
557
558static int htb_enqueue(struct sk_buff *skb, struct Qdisc *sch)
559{
Jarek Poplawskif30ab412008-11-13 22:56:30 -0800560 int uninitialized_var(ret);
Stephen Hemminger87990462006-08-10 23:35:16 -0700561 struct htb_sched *q = qdisc_priv(sch);
562 struct htb_class *cl = htb_classify(skb, sch, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Stephen Hemminger87990462006-08-10 23:35:16 -0700564 if (cl == HTB_DIRECT) {
565 /* enqueue to helper queue */
566 if (q->direct_queue.qlen < q->direct_qlen) {
567 __skb_queue_tail(&q->direct_queue, skb);
568 q->direct_pkts++;
569 } else {
Eric Dumazet17045752012-05-04 04:37:21 +0000570 return qdisc_drop(skb, sch);
Stephen Hemminger87990462006-08-10 23:35:16 -0700571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572#ifdef CONFIG_NET_CLS_ACT
Stephen Hemminger87990462006-08-10 23:35:16 -0700573 } else if (!cl) {
Jarek Poplawskic27f3392008-08-04 22:39:11 -0700574 if (ret & __NET_XMIT_BYPASS)
Stephen Hemminger87990462006-08-10 23:35:16 -0700575 sch->qstats.drops++;
576 kfree_skb(skb);
577 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578#endif
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700579 } else if ((ret = qdisc_enqueue(skb, cl->un.leaf.q)) != NET_XMIT_SUCCESS) {
580 if (net_xmit_drop_count(ret)) {
581 sch->qstats.drops++;
582 cl->qstats.drops++;
583 }
David S. Miller69747652008-08-17 23:55:36 -0700584 return ret;
Stephen Hemminger87990462006-08-10 23:35:16 -0700585 } else {
Stephen Hemminger87990462006-08-10 23:35:16 -0700586 htb_activate(q, cl);
587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Stephen Hemminger87990462006-08-10 23:35:16 -0700589 sch->q.qlen++;
Stephen Hemminger87990462006-08-10 23:35:16 -0700590 return NET_XMIT_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Vimalkumar56b765b2012-10-31 06:04:11 +0000593static inline void htb_accnt_tokens(struct htb_class *cl, int bytes, s64 diff)
Jarek Poplawski59e42202008-12-03 21:17:27 -0800594{
Vimalkumar56b765b2012-10-31 06:04:11 +0000595 s64 toks = diff + cl->tokens;
Jarek Poplawski59e42202008-12-03 21:17:27 -0800596
597 if (toks > cl->buffer)
598 toks = cl->buffer;
Jiri Pirko292f1c72013-02-12 00:12:03 +0000599 toks -= (s64) psched_l2t_ns(&cl->rate, bytes);
Jarek Poplawski59e42202008-12-03 21:17:27 -0800600 if (toks <= -cl->mbuffer)
601 toks = 1 - cl->mbuffer;
602
603 cl->tokens = toks;
604}
605
Vimalkumar56b765b2012-10-31 06:04:11 +0000606static inline void htb_accnt_ctokens(struct htb_class *cl, int bytes, s64 diff)
Jarek Poplawski59e42202008-12-03 21:17:27 -0800607{
Vimalkumar56b765b2012-10-31 06:04:11 +0000608 s64 toks = diff + cl->ctokens;
Jarek Poplawski59e42202008-12-03 21:17:27 -0800609
610 if (toks > cl->cbuffer)
611 toks = cl->cbuffer;
Jiri Pirko292f1c72013-02-12 00:12:03 +0000612 toks -= (s64) psched_l2t_ns(&cl->ceil, bytes);
Jarek Poplawski59e42202008-12-03 21:17:27 -0800613 if (toks <= -cl->mbuffer)
614 toks = 1 - cl->mbuffer;
615
616 cl->ctokens = toks;
617}
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/**
620 * htb_charge_class - charges amount "bytes" to leaf and ancestors
621 *
622 * Routine assumes that packet "bytes" long was dequeued from leaf cl
623 * borrowing from "level". It accounts bytes to ceil leaky bucket for
624 * leaf and all ancestors and to rate bucket for ancestors at levels
625 * "level" and higher. It also handles possible change of mode resulting
626 * from the update. Note that mode can also increase here (MAY_BORROW to
627 * CAN_SEND) because we can use more precise clock that event queue here.
628 * In such case we remove class from event queue first.
629 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700630static void htb_charge_class(struct htb_sched *q, struct htb_class *cl,
Ranjit Manomohanc9726d62007-07-10 22:43:16 -0700631 int level, struct sk_buff *skb)
Stephen Hemminger87990462006-08-10 23:35:16 -0700632{
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700633 int bytes = qdisc_pkt_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 enum htb_cmode old_mode;
Vimalkumar56b765b2012-10-31 06:04:11 +0000635 s64 diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 while (cl) {
Vimalkumar56b765b2012-10-31 06:04:11 +0000638 diff = min_t(s64, q->now - cl->t_c, cl->mbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (cl->level >= level) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700640 if (cl->level == level)
641 cl->xstats.lends++;
Jarek Poplawski59e42202008-12-03 21:17:27 -0800642 htb_accnt_tokens(cl, bytes, diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 } else {
644 cl->xstats.borrows++;
Stephen Hemminger87990462006-08-10 23:35:16 -0700645 cl->tokens += diff; /* we moved t_c; update tokens */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
Jarek Poplawski59e42202008-12-03 21:17:27 -0800647 htb_accnt_ctokens(cl, bytes, diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 cl->t_c = q->now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Stephen Hemminger87990462006-08-10 23:35:16 -0700650 old_mode = cl->cmode;
651 diff = 0;
652 htb_change_class_mode(q, cl, &diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (old_mode != cl->cmode) {
654 if (old_mode != HTB_CAN_SEND)
Stephen Hemminger3696f622006-08-10 23:36:01 -0700655 htb_safe_rb_erase(&cl->pq_node, q->wait_pq + cl->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (cl->cmode != HTB_CAN_SEND)
Stephen Hemminger87990462006-08-10 23:35:16 -0700657 htb_add_to_wait_tree(q, cl, diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Eric Dumazetbfe0d022011-01-09 08:30:54 +0000660 /* update basic stats except for leaves which are already updated */
661 if (cl->level)
662 bstats_update(&cl->bstats, skb);
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 cl = cl->parent;
665 }
666}
667
668/**
669 * htb_do_events - make mode changes to classes at the level
670 *
Patrick McHardyfb983d42007-03-16 01:22:39 -0700671 * Scans event queue for pending events and applies them. Returns time of
Jarek Poplawski12247362009-02-01 01:13:22 -0800672 * next pending event (0 for no event in pq, q->now for too many events).
Patrick McHardyfb983d42007-03-16 01:22:39 -0700673 * Note: Applied are events whose have cl->pq_key <= q->now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 */
Eric Dumazet5343a7f2013-06-04 07:11:48 +0000675static s64 htb_do_events(struct htb_sched *q, int level,
676 unsigned long start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Martin Devera8f3ea332008-03-23 22:00:38 -0700678 /* don't run for longer than 2 jiffies; 2 is used instead of
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000679 * 1 to simplify things when jiffy is going to be incremented
680 * too soon
681 */
Jarek Poplawskia73be042009-01-12 21:54:40 -0800682 unsigned long stop_at = start + 2;
Martin Devera8f3ea332008-03-23 22:00:38 -0700683 while (time_before(jiffies, stop_at)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 struct htb_class *cl;
Vimalkumar56b765b2012-10-31 06:04:11 +0000685 s64 diff;
Akinbou Mita30bdbe32006-10-12 01:52:05 -0700686 struct rb_node *p = rb_first(&q->wait_pq[level]);
687
Stephen Hemminger87990462006-08-10 23:35:16 -0700688 if (!p)
689 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 cl = rb_entry(p, struct htb_class, pq_node);
Patrick McHardyfb983d42007-03-16 01:22:39 -0700692 if (cl->pq_key > q->now)
693 return cl->pq_key;
694
Stephen Hemminger3696f622006-08-10 23:36:01 -0700695 htb_safe_rb_erase(p, q->wait_pq + level);
Vimalkumar56b765b2012-10-31 06:04:11 +0000696 diff = min_t(s64, q->now - cl->t_c, cl->mbuffer);
Stephen Hemminger87990462006-08-10 23:35:16 -0700697 htb_change_class_mode(q, cl, &diff);
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 }
Jarek Poplawski12247362009-02-01 01:13:22 -0800701
702 /* too much load - let's continue after a break for scheduling */
Jarek Poplawskie82181d2009-02-01 01:13:05 -0800703 if (!(q->warned & HTB_WARN_TOOMANYEVENTS)) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000704 pr_warning("htb: too many events!\n");
Jarek Poplawskie82181d2009-02-01 01:13:05 -0800705 q->warned |= HTB_WARN_TOOMANYEVENTS;
706 }
Jarek Poplawski12247362009-02-01 01:13:22 -0800707
708 return q->now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
711/* Returns class->node+prio from id-tree where classe's id is >= id. NULL
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000712 * is no such one exists.
713 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700714static struct rb_node *htb_id_find_next_upper(int prio, struct rb_node *n,
715 u32 id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 struct rb_node *r = NULL;
718 while (n) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700719 struct htb_class *cl =
720 rb_entry(n, struct htb_class, node[prio]);
Stephen Hemminger87990462006-08-10 23:35:16 -0700721
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700722 if (id > cl->common.classid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 n = n->rb_right;
Jarek Poplawski1b5c0072008-12-09 22:34:40 -0800724 } else if (id < cl->common.classid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 r = n;
726 n = n->rb_left;
Jarek Poplawski1b5c0072008-12-09 22:34:40 -0800727 } else {
728 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730 }
731 return r;
732}
733
734/**
735 * htb_lookup_leaf - returns next leaf class in DRR order
736 *
737 * Find leaf where current feed pointers points to.
738 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700739static struct htb_class *htb_lookup_leaf(struct rb_root *tree, int prio,
740 struct rb_node **pptr, u32 * pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 int i;
743 struct {
744 struct rb_node *root;
745 struct rb_node **pptr;
746 u32 *pid;
Stephen Hemminger87990462006-08-10 23:35:16 -0700747 } stk[TC_HTB_MAXDEPTH], *sp = stk;
748
Jarek Poplawski512bb432008-12-09 22:35:02 -0800749 BUG_ON(!tree->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 sp->root = tree->rb_node;
751 sp->pptr = pptr;
752 sp->pid = pid;
753
754 for (i = 0; i < 65535; i++) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700755 if (!*sp->pptr && *sp->pid) {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900756 /* ptr was invalidated but id is valid - try to recover
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000757 * the original or next ptr
758 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700759 *sp->pptr =
760 htb_id_find_next_upper(prio, sp->root, *sp->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
Stephen Hemminger87990462006-08-10 23:35:16 -0700762 *sp->pid = 0; /* ptr is valid now so that remove this hint as it
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000763 * can become out of date quickly
764 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700765 if (!*sp->pptr) { /* we are at right end; rewind & go up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 *sp->pptr = sp->root;
Stephen Hemminger87990462006-08-10 23:35:16 -0700767 while ((*sp->pptr)->rb_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 *sp->pptr = (*sp->pptr)->rb_left;
769 if (sp > stk) {
770 sp--;
Jarek Poplawski512bb432008-12-09 22:35:02 -0800771 if (!*sp->pptr) {
772 WARN_ON(1);
Stephen Hemminger87990462006-08-10 23:35:16 -0700773 return NULL;
Jarek Poplawski512bb432008-12-09 22:35:02 -0800774 }
Stephen Hemminger87990462006-08-10 23:35:16 -0700775 htb_next_rb_node(sp->pptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777 } else {
778 struct htb_class *cl;
Stephen Hemminger87990462006-08-10 23:35:16 -0700779 cl = rb_entry(*sp->pptr, struct htb_class, node[prio]);
780 if (!cl->level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return cl;
782 (++sp)->root = cl->un.inner.feed[prio].rb_node;
Stephen Hemminger87990462006-08-10 23:35:16 -0700783 sp->pptr = cl->un.inner.ptr + prio;
784 sp->pid = cl->un.inner.last_ptr_id + prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700787 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return NULL;
789}
790
791/* dequeues packet at given priority and level; call only if
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000792 * you are sure that there is active class at prio/level
793 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700794static struct sk_buff *htb_dequeue_tree(struct htb_sched *q, int prio,
795 int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 struct sk_buff *skb = NULL;
Stephen Hemminger87990462006-08-10 23:35:16 -0700798 struct htb_class *cl, *start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 /* look initial class up in the row */
Stephen Hemminger87990462006-08-10 23:35:16 -0700800 start = cl = htb_lookup_leaf(q->row[level] + prio, prio,
801 q->ptr[level] + prio,
802 q->last_ptr_id[level] + prio);
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 do {
805next:
Jarek Poplawski512bb432008-12-09 22:35:02 -0800806 if (unlikely(!cl))
Stephen Hemminger87990462006-08-10 23:35:16 -0700807 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 /* class can be empty - it is unlikely but can be true if leaf
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000810 * qdisc drops packets in enqueue routine or if someone used
811 * graft operation on the leaf since last dequeue;
812 * simply deactivate and skip such class
813 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (unlikely(cl->un.leaf.q->q.qlen == 0)) {
815 struct htb_class *next;
Stephen Hemminger87990462006-08-10 23:35:16 -0700816 htb_deactivate(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 /* row/level might become empty */
819 if ((q->row_mask[level] & (1 << prio)) == 0)
Stephen Hemminger87990462006-08-10 23:35:16 -0700820 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Stephen Hemminger87990462006-08-10 23:35:16 -0700822 next = htb_lookup_leaf(q->row[level] + prio,
823 prio, q->ptr[level] + prio,
824 q->last_ptr_id[level] + prio);
825
826 if (cl == start) /* fix start if we just deleted it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 start = next;
828 cl = next;
829 goto next;
830 }
Stephen Hemminger87990462006-08-10 23:35:16 -0700831
832 skb = cl->un.leaf.q->dequeue(cl->un.leaf.q);
833 if (likely(skb != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 break;
Jarek Poplawski633fe662008-12-03 21:09:10 -0800835
Jarek Poplawskib00355d2009-02-01 01:12:42 -0800836 qdisc_warn_nonwc("htb", cl->un.leaf.q);
Stephen Hemminger87990462006-08-10 23:35:16 -0700837 htb_next_rb_node((level ? cl->parent->un.inner.ptr : q->
838 ptr[0]) + prio);
839 cl = htb_lookup_leaf(q->row[level] + prio, prio,
840 q->ptr[level] + prio,
841 q->last_ptr_id[level] + prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 } while (cl != start);
844
845 if (likely(skb != NULL)) {
Eric Dumazet196d97f2012-11-05 16:40:49 +0000846 bstats_update(&cl->bstats, skb);
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700847 cl->un.leaf.deficit[level] -= qdisc_pkt_len(skb);
848 if (cl->un.leaf.deficit[level] < 0) {
Jarek Poplawskic19f7a32008-12-03 21:09:45 -0800849 cl->un.leaf.deficit[level] += cl->quantum;
Stephen Hemminger87990462006-08-10 23:35:16 -0700850 htb_next_rb_node((level ? cl->parent->un.inner.ptr : q->
851 ptr[0]) + prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853 /* this used to be after charge_class but this constelation
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000854 * gives us slightly better performance
855 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (!cl->un.leaf.q->q.qlen)
Stephen Hemminger87990462006-08-10 23:35:16 -0700857 htb_deactivate(q, cl);
Ranjit Manomohanc9726d62007-07-10 22:43:16 -0700858 htb_charge_class(q, cl, level, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860 return skb;
861}
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863static struct sk_buff *htb_dequeue(struct Qdisc *sch)
864{
Eric Dumazet9190b3b2011-01-20 23:31:33 -0800865 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 struct htb_sched *q = qdisc_priv(sch);
867 int level;
Eric Dumazet5343a7f2013-06-04 07:11:48 +0000868 s64 next_event;
Jarek Poplawskia73be042009-01-12 21:54:40 -0800869 unsigned long start_at;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 /* try to dequeue direct packets as high prio (!) to minimize cpu work */
Stephen Hemminger87990462006-08-10 23:35:16 -0700872 skb = __skb_dequeue(&q->direct_queue);
873 if (skb != NULL) {
Eric Dumazet9190b3b2011-01-20 23:31:33 -0800874ok:
875 qdisc_bstats_update(sch, skb);
Eric Dumazetfd245a42011-01-20 05:27:16 +0000876 qdisc_unthrottled(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 sch->q.qlen--;
878 return skb;
879 }
880
Stephen Hemminger87990462006-08-10 23:35:16 -0700881 if (!sch->q.qlen)
882 goto fin;
Vimalkumar56b765b2012-10-31 06:04:11 +0000883 q->now = ktime_to_ns(ktime_get());
Jarek Poplawskia73be042009-01-12 21:54:40 -0800884 start_at = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Stefan Haskod2fe85d2012-12-21 15:04:59 +0000886 next_event = q->now + 5LLU * NSEC_PER_SEC;
Jarek Poplawski633fe662008-12-03 21:09:10 -0800887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 for (level = 0; level < TC_HTB_MAXDEPTH; level++) {
889 /* common case optimization - skip event handler quickly */
890 int m;
Eric Dumazet5343a7f2013-06-04 07:11:48 +0000891 s64 event;
Stephen Hemminger87990462006-08-10 23:35:16 -0700892
Patrick McHardyfb983d42007-03-16 01:22:39 -0700893 if (q->now >= q->near_ev_cache[level]) {
Jarek Poplawskia73be042009-01-12 21:54:40 -0800894 event = htb_do_events(q, level, start_at);
Patrick McHardy2e4b3b02007-05-23 23:39:54 -0700895 if (!event)
Vimalkumar56b765b2012-10-31 06:04:11 +0000896 event = q->now + NSEC_PER_SEC;
Patrick McHardy2e4b3b02007-05-23 23:39:54 -0700897 q->near_ev_cache[level] = event;
Patrick McHardyfb983d42007-03-16 01:22:39 -0700898 } else
899 event = q->near_ev_cache[level];
900
Jarek Poplawskic0851342009-01-12 21:54:16 -0800901 if (next_event > event)
Patrick McHardyfb983d42007-03-16 01:22:39 -0700902 next_event = event;
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 m = ~q->row_mask[level];
905 while (m != (int)(-1)) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700906 int prio = ffz(m);
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 m |= 1 << prio;
Stephen Hemminger87990462006-08-10 23:35:16 -0700909 skb = htb_dequeue_tree(q, prio, level);
Eric Dumazet9190b3b2011-01-20 23:31:33 -0800910 if (likely(skb != NULL))
911 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
913 }
Patrick McHardyfb983d42007-03-16 01:22:39 -0700914 sch->qstats.overlimits++;
Vimalkumar56b765b2012-10-31 06:04:11 +0000915 if (likely(next_event > q->now)) {
916 if (!test_bit(__QDISC_STATE_DEACTIVATED,
917 &qdisc_root_sleeping(q->watchdog.qdisc)->state)) {
918 ktime_t time = ns_to_ktime(next_event);
919 qdisc_throttled(q->watchdog.qdisc);
920 hrtimer_start(&q->watchdog.timer, time,
921 HRTIMER_MODE_ABS);
922 }
923 } else {
Jarek Poplawski12247362009-02-01 01:13:22 -0800924 schedule_work(&q->work);
Vimalkumar56b765b2012-10-31 06:04:11 +0000925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926fin:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return skb;
928}
929
930/* try to drop from each class (by prio) until one succeed */
Stephen Hemminger87990462006-08-10 23:35:16 -0700931static unsigned int htb_drop(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 struct htb_sched *q = qdisc_priv(sch);
934 int prio;
935
936 for (prio = TC_HTB_NUMPRIO - 1; prio >= 0; prio--) {
937 struct list_head *p;
Stephen Hemminger87990462006-08-10 23:35:16 -0700938 list_for_each(p, q->drops + prio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 struct htb_class *cl = list_entry(p, struct htb_class,
940 un.leaf.drop_list);
941 unsigned int len;
Stephen Hemminger87990462006-08-10 23:35:16 -0700942 if (cl->un.leaf.q->ops->drop &&
943 (len = cl->un.leaf.q->ops->drop(cl->un.leaf.q))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 sch->q.qlen--;
945 if (!cl->un.leaf.q->q.qlen)
Stephen Hemminger87990462006-08-10 23:35:16 -0700946 htb_deactivate(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return len;
948 }
949 }
950 }
951 return 0;
952}
953
954/* reset all classes */
955/* always caled under BH & queue lock */
Stephen Hemminger87990462006-08-10 23:35:16 -0700956static void htb_reset(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 struct htb_sched *q = qdisc_priv(sch);
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700959 struct htb_class *cl;
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700960 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700962 for (i = 0; i < q->clhash.hashsize; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800963 hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (cl->level)
Stephen Hemminger87990462006-08-10 23:35:16 -0700965 memset(&cl->un.inner, 0, sizeof(cl->un.inner));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 else {
Stephen Hemminger87990462006-08-10 23:35:16 -0700967 if (cl->un.leaf.q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 qdisc_reset(cl->un.leaf.q);
969 INIT_LIST_HEAD(&cl->un.leaf.drop_list);
970 }
971 cl->prio_activity = 0;
972 cl->cmode = HTB_CAN_SEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 }
975 }
Patrick McHardyfb983d42007-03-16 01:22:39 -0700976 qdisc_watchdog_cancel(&q->watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 __skb_queue_purge(&q->direct_queue);
978 sch->q.qlen = 0;
Stephen Hemminger87990462006-08-10 23:35:16 -0700979 memset(q->row, 0, sizeof(q->row));
980 memset(q->row_mask, 0, sizeof(q->row_mask));
981 memset(q->wait_pq, 0, sizeof(q->wait_pq));
982 memset(q->ptr, 0, sizeof(q->ptr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 for (i = 0; i < TC_HTB_NUMPRIO; i++)
Stephen Hemminger87990462006-08-10 23:35:16 -0700984 INIT_LIST_HEAD(q->drops + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Patrick McHardy27a34212008-01-23 20:35:39 -0800987static const struct nla_policy htb_policy[TCA_HTB_MAX + 1] = {
988 [TCA_HTB_PARMS] = { .len = sizeof(struct tc_htb_opt) },
989 [TCA_HTB_INIT] = { .len = sizeof(struct tc_htb_glob) },
990 [TCA_HTB_CTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
991 [TCA_HTB_RTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
Eric Dumazet6906f4e2013-03-06 06:49:21 +0000992 [TCA_HTB_DIRECT_QLEN] = { .type = NLA_U32 },
Patrick McHardy27a34212008-01-23 20:35:39 -0800993};
994
Jarek Poplawski12247362009-02-01 01:13:22 -0800995static void htb_work_func(struct work_struct *work)
996{
997 struct htb_sched *q = container_of(work, struct htb_sched, work);
998 struct Qdisc *sch = q->watchdog.qdisc;
999
1000 __netif_schedule(qdisc_root(sch));
1001}
1002
Patrick McHardy1e904742008-01-22 22:11:17 -08001003static int htb_init(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005 struct htb_sched *q = qdisc_priv(sch);
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001006 struct nlattr *tb[TCA_HTB_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 struct tc_htb_glob *gopt;
Patrick McHardycee63722008-01-23 20:33:32 -08001008 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 int i;
Patrick McHardycee63722008-01-23 20:33:32 -08001010
1011 if (!opt)
1012 return -EINVAL;
1013
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001014 err = nla_parse_nested(tb, TCA_HTB_MAX, opt, htb_policy);
Patrick McHardycee63722008-01-23 20:33:32 -08001015 if (err < 0)
1016 return err;
1017
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001018 if (!tb[TCA_HTB_INIT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return -EINVAL;
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001020
Patrick McHardy1e904742008-01-22 22:11:17 -08001021 gopt = nla_data(tb[TCA_HTB_INIT]);
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001022 if (gopt->version != HTB_VER >> 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001025 err = qdisc_class_hash_init(&q->clhash);
1026 if (err < 0)
1027 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 for (i = 0; i < TC_HTB_NUMPRIO; i++)
Stephen Hemminger87990462006-08-10 23:35:16 -07001029 INIT_LIST_HEAD(q->drops + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Patrick McHardyfb983d42007-03-16 01:22:39 -07001031 qdisc_watchdog_init(&q->watchdog, sch);
Jarek Poplawski12247362009-02-01 01:13:22 -08001032 INIT_WORK(&q->work, htb_work_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 skb_queue_head_init(&q->direct_queue);
1034
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001035 if (tb[TCA_HTB_DIRECT_QLEN])
1036 q->direct_qlen = nla_get_u32(tb[TCA_HTB_DIRECT_QLEN]);
1037 else {
1038 q->direct_qlen = qdisc_dev(sch)->tx_queue_len;
1039 if (q->direct_qlen < 2) /* some devices have zero tx_queue_len */
1040 q->direct_qlen = 2;
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if ((q->rate2quantum = gopt->rate2quantum) < 1)
1043 q->rate2quantum = 1;
1044 q->defcls = gopt->defcls;
1045
1046 return 0;
1047}
1048
1049static int htb_dump(struct Qdisc *sch, struct sk_buff *skb)
1050{
Jarek Poplawski102396a2008-08-29 14:21:52 -07001051 spinlock_t *root_lock = qdisc_root_sleeping_lock(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 struct htb_sched *q = qdisc_priv(sch);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001053 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 struct tc_htb_glob gopt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
David S. Miller7698b4f2008-07-16 01:42:40 -07001056 spin_lock_bh(root_lock);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001057
1058 gopt.direct_pkts = q->direct_pkts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 gopt.version = HTB_VER;
1060 gopt.rate2quantum = q->rate2quantum;
1061 gopt.defcls = q->defcls;
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001062 gopt.debug = 0;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001063
1064 nest = nla_nest_start(skb, TCA_OPTIONS);
1065 if (nest == NULL)
1066 goto nla_put_failure;
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001067 if (nla_put(skb, TCA_HTB_INIT, sizeof(gopt), &gopt) ||
1068 nla_put_u32(skb, TCA_HTB_DIRECT_QLEN, q->direct_qlen))
David S. Miller1b34ec42012-03-29 05:11:39 -04001069 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001070 nla_nest_end(skb, nest);
1071
David S. Miller7698b4f2008-07-16 01:42:40 -07001072 spin_unlock_bh(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return skb->len;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001074
Patrick McHardy1e904742008-01-22 22:11:17 -08001075nla_put_failure:
David S. Miller7698b4f2008-07-16 01:42:40 -07001076 spin_unlock_bh(root_lock);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001077 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return -1;
1079}
1080
1081static int htb_dump_class(struct Qdisc *sch, unsigned long arg,
Stephen Hemminger87990462006-08-10 23:35:16 -07001082 struct sk_buff *skb, struct tcmsg *tcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Stephen Hemminger87990462006-08-10 23:35:16 -07001084 struct htb_class *cl = (struct htb_class *)arg;
Jarek Poplawski102396a2008-08-29 14:21:52 -07001085 spinlock_t *root_lock = qdisc_root_sleeping_lock(sch);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001086 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 struct tc_htb_opt opt;
1088
David S. Miller7698b4f2008-07-16 01:42:40 -07001089 spin_lock_bh(root_lock);
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001090 tcm->tcm_parent = cl->parent ? cl->parent->common.classid : TC_H_ROOT;
1091 tcm->tcm_handle = cl->common.classid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (!cl->level && cl->un.leaf.q)
1093 tcm->tcm_info = cl->un.leaf.q->handle;
1094
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001095 nest = nla_nest_start(skb, TCA_OPTIONS);
1096 if (nest == NULL)
1097 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Stephen Hemminger87990462006-08-10 23:35:16 -07001099 memset(&opt, 0, sizeof(opt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Eric Dumazet01cb71d2013-06-02 13:55:05 +00001101 psched_ratecfg_getrate(&opt.rate, &cl->rate);
Jiri Pirko9c10f412013-02-12 00:12:00 +00001102 opt.buffer = PSCHED_NS2TICKS(cl->buffer);
Eric Dumazet01cb71d2013-06-02 13:55:05 +00001103 psched_ratecfg_getrate(&opt.ceil, &cl->ceil);
Jiri Pirko9c10f412013-02-12 00:12:00 +00001104 opt.cbuffer = PSCHED_NS2TICKS(cl->cbuffer);
Jarek Poplawskic19f7a32008-12-03 21:09:45 -08001105 opt.quantum = cl->quantum;
1106 opt.prio = cl->prio;
Stephen Hemminger87990462006-08-10 23:35:16 -07001107 opt.level = cl->level;
David S. Miller1b34ec42012-03-29 05:11:39 -04001108 if (nla_put(skb, TCA_HTB_PARMS, sizeof(opt), &opt))
1109 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001110
1111 nla_nest_end(skb, nest);
David S. Miller7698b4f2008-07-16 01:42:40 -07001112 spin_unlock_bh(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return skb->len;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001114
Patrick McHardy1e904742008-01-22 22:11:17 -08001115nla_put_failure:
David S. Miller7698b4f2008-07-16 01:42:40 -07001116 spin_unlock_bh(root_lock);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001117 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return -1;
1119}
1120
1121static int
Stephen Hemminger87990462006-08-10 23:35:16 -07001122htb_dump_class_stats(struct Qdisc *sch, unsigned long arg, struct gnet_dump *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Stephen Hemminger87990462006-08-10 23:35:16 -07001124 struct htb_class *cl = (struct htb_class *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (!cl->level && cl->un.leaf.q)
1127 cl->qstats.qlen = cl->un.leaf.q->q.qlen;
Eric Dumazet5343a7f2013-06-04 07:11:48 +00001128 cl->xstats.tokens = PSCHED_NS2TICKS(cl->tokens);
1129 cl->xstats.ctokens = PSCHED_NS2TICKS(cl->ctokens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 if (gnet_stats_copy_basic(d, &cl->bstats) < 0 ||
Eric Dumazetd250a5f2009-10-02 10:32:18 +00001132 gnet_stats_copy_rate_est(d, NULL, &cl->rate_est) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 gnet_stats_copy_queue(d, &cl->qstats) < 0)
1134 return -1;
1135
1136 return gnet_stats_copy_app(d, &cl->xstats, sizeof(cl->xstats));
1137}
1138
1139static int htb_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
Stephen Hemminger87990462006-08-10 23:35:16 -07001140 struct Qdisc **old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Stephen Hemminger87990462006-08-10 23:35:16 -07001142 struct htb_class *cl = (struct htb_class *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Patrick McHardy5b9a9cc2009-09-04 06:41:17 +00001144 if (cl->level)
1145 return -EINVAL;
1146 if (new == NULL &&
Changli Gao3511c912010-10-16 13:04:08 +00001147 (new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
Patrick McHardy5b9a9cc2009-09-04 06:41:17 +00001148 cl->common.classid)) == NULL)
1149 return -ENOBUFS;
1150
1151 sch_tree_lock(sch);
1152 *old = cl->un.leaf.q;
1153 cl->un.leaf.q = new;
1154 if (*old != NULL) {
1155 qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
1156 qdisc_reset(*old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
Patrick McHardy5b9a9cc2009-09-04 06:41:17 +00001158 sch_tree_unlock(sch);
1159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
Stephen Hemminger87990462006-08-10 23:35:16 -07001162static struct Qdisc *htb_leaf(struct Qdisc *sch, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Stephen Hemminger87990462006-08-10 23:35:16 -07001164 struct htb_class *cl = (struct htb_class *)arg;
Patrick McHardy5b9a9cc2009-09-04 06:41:17 +00001165 return !cl->level ? cl->un.leaf.q : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
Patrick McHardy256d61b2006-11-29 17:37:05 -08001168static void htb_qlen_notify(struct Qdisc *sch, unsigned long arg)
1169{
1170 struct htb_class *cl = (struct htb_class *)arg;
1171
1172 if (cl->un.leaf.q->q.qlen == 0)
1173 htb_deactivate(qdisc_priv(sch), cl);
1174}
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176static unsigned long htb_get(struct Qdisc *sch, u32 classid)
1177{
Stephen Hemminger87990462006-08-10 23:35:16 -07001178 struct htb_class *cl = htb_find(classid, sch);
1179 if (cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 cl->refcnt++;
1181 return (unsigned long)cl;
1182}
1183
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001184static inline int htb_parent_last_child(struct htb_class *cl)
1185{
1186 if (!cl->parent)
1187 /* the root class */
1188 return 0;
Patrick McHardy42077592008-07-05 23:22:53 -07001189 if (cl->parent->children > 1)
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001190 /* not the last child */
1191 return 0;
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001192 return 1;
1193}
1194
Jarek Poplawski3ba08b02008-05-03 20:46:29 -07001195static void htb_parent_to_leaf(struct htb_sched *q, struct htb_class *cl,
1196 struct Qdisc *new_q)
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001197{
1198 struct htb_class *parent = cl->parent;
1199
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001200 WARN_ON(cl->level || !cl->un.leaf.q || cl->prio_activity);
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001201
Jarek Poplawski3ba08b02008-05-03 20:46:29 -07001202 if (parent->cmode != HTB_CAN_SEND)
1203 htb_safe_rb_erase(&parent->pq_node, q->wait_pq + parent->level);
1204
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001205 parent->level = 0;
1206 memset(&parent->un.inner, 0, sizeof(parent->un.inner));
1207 INIT_LIST_HEAD(&parent->un.leaf.drop_list);
1208 parent->un.leaf.q = new_q ? new_q : &noop_qdisc;
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001209 parent->tokens = parent->buffer;
1210 parent->ctokens = parent->cbuffer;
Eric Dumazet5343a7f2013-06-04 07:11:48 +00001211 parent->t_c = ktime_to_ns(ktime_get());
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001212 parent->cmode = HTB_CAN_SEND;
1213}
1214
Stephen Hemminger87990462006-08-10 23:35:16 -07001215static void htb_destroy_class(struct Qdisc *sch, struct htb_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (!cl->level) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001218 WARN_ON(!cl->un.leaf.q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 qdisc_destroy(cl->un.leaf.q);
1220 }
Patrick McHardyee39e102007-07-02 22:48:13 -07001221 gen_kill_estimator(&cl->bstats, &cl->rate_est);
Patrick McHardyff31ab52008-07-01 19:52:38 -07001222 tcf_destroy_chain(&cl->filter_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 kfree(cl);
1224}
1225
Stephen Hemminger87990462006-08-10 23:35:16 -07001226static void htb_destroy(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
1228 struct htb_sched *q = qdisc_priv(sch);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001229 struct hlist_node *next;
Patrick McHardyfbd8f132008-07-05 23:22:19 -07001230 struct htb_class *cl;
1231 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Jarek Poplawski12247362009-02-01 01:13:22 -08001233 cancel_work_sync(&q->work);
Patrick McHardyfb983d42007-03-16 01:22:39 -07001234 qdisc_watchdog_cancel(&q->watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 /* This line used to be after htb_destroy_class call below
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001236 * and surprisingly it worked in 2.4. But it must precede it
1237 * because filter need its target class alive to be able to call
1238 * unbind_filter on it (without Oops).
1239 */
Patrick McHardyff31ab52008-07-01 19:52:38 -07001240 tcf_destroy_chain(&q->filter_list);
Stephen Hemminger87990462006-08-10 23:35:16 -07001241
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001242 for (i = 0; i < q->clhash.hashsize; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001243 hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode)
Patrick McHardyfbd8f132008-07-05 23:22:19 -07001244 tcf_destroy_chain(&cl->filter_list);
1245 }
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001246 for (i = 0; i < q->clhash.hashsize; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001247 hlist_for_each_entry_safe(cl, next, &q->clhash.hash[i],
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001248 common.hnode)
Patrick McHardyfbd8f132008-07-05 23:22:19 -07001249 htb_destroy_class(sch, cl);
1250 }
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001251 qdisc_class_hash_destroy(&q->clhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 __skb_queue_purge(&q->direct_queue);
1253}
1254
1255static int htb_delete(struct Qdisc *sch, unsigned long arg)
1256{
1257 struct htb_sched *q = qdisc_priv(sch);
Stephen Hemminger87990462006-08-10 23:35:16 -07001258 struct htb_class *cl = (struct htb_class *)arg;
Patrick McHardy256d61b2006-11-29 17:37:05 -08001259 unsigned int qlen;
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001260 struct Qdisc *new_q = NULL;
1261 int last_child = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 // TODO: why don't allow to delete subtree ? references ? does
1264 // tc subsys quarantee us that in htb_destroy it holds no class
1265 // refs so that we can remove children safely there ?
Patrick McHardy42077592008-07-05 23:22:53 -07001266 if (cl->children || cl->filter_cnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return -EBUSY;
Stephen Hemminger87990462006-08-10 23:35:16 -07001268
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001269 if (!cl->level && htb_parent_last_child(cl)) {
Changli Gao3511c912010-10-16 13:04:08 +00001270 new_q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
David S. Millerbb949fb2008-07-08 16:55:56 -07001271 cl->parent->common.classid);
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001272 last_child = 1;
1273 }
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 sch_tree_lock(sch);
Stephen Hemminger87990462006-08-10 23:35:16 -07001276
Patrick McHardy814a175e2006-11-29 17:34:50 -08001277 if (!cl->level) {
Patrick McHardy256d61b2006-11-29 17:37:05 -08001278 qlen = cl->un.leaf.q->q.qlen;
Patrick McHardy814a175e2006-11-29 17:34:50 -08001279 qdisc_reset(cl->un.leaf.q);
Patrick McHardy256d61b2006-11-29 17:37:05 -08001280 qdisc_tree_decrease_qlen(cl->un.leaf.q, qlen);
Patrick McHardy814a175e2006-11-29 17:34:50 -08001281 }
1282
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001283 /* delete from hash and active; remainder in destroy_class */
1284 qdisc_class_hash_remove(&q->clhash, &cl->common);
Jarek Poplawski26b284d2008-08-13 15:16:43 -07001285 if (cl->parent)
1286 cl->parent->children--;
Patrick McHardyc38c83c2007-03-27 14:04:24 -07001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (cl->prio_activity)
Stephen Hemminger87990462006-08-10 23:35:16 -07001289 htb_deactivate(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Patrick McHardyfbd8f132008-07-05 23:22:19 -07001291 if (cl->cmode != HTB_CAN_SEND)
1292 htb_safe_rb_erase(&cl->pq_node, q->wait_pq + cl->level);
1293
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001294 if (last_child)
Jarek Poplawski3ba08b02008-05-03 20:46:29 -07001295 htb_parent_to_leaf(q, cl, new_q);
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001296
Jarek Poplawski7cd0a632009-03-15 20:00:19 -07001297 BUG_ON(--cl->refcnt == 0);
1298 /*
1299 * This shouldn't happen: we "hold" one cops->get() when called
1300 * from tc_ctl_tclass; the destroy method is done from cops->put().
1301 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 sch_tree_unlock(sch);
1304 return 0;
1305}
1306
1307static void htb_put(struct Qdisc *sch, unsigned long arg)
1308{
Stephen Hemminger87990462006-08-10 23:35:16 -07001309 struct htb_class *cl = (struct htb_class *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 if (--cl->refcnt == 0)
Stephen Hemminger87990462006-08-10 23:35:16 -07001312 htb_destroy_class(sch, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314
Stephen Hemminger87990462006-08-10 23:35:16 -07001315static int htb_change_class(struct Qdisc *sch, u32 classid,
Patrick McHardy1e904742008-01-22 22:11:17 -08001316 u32 parentid, struct nlattr **tca,
Stephen Hemminger87990462006-08-10 23:35:16 -07001317 unsigned long *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 int err = -EINVAL;
1320 struct htb_sched *q = qdisc_priv(sch);
Stephen Hemminger87990462006-08-10 23:35:16 -07001321 struct htb_class *cl = (struct htb_class *)*arg, *parent;
Patrick McHardy1e904742008-01-22 22:11:17 -08001322 struct nlattr *opt = tca[TCA_OPTIONS];
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001323 struct nlattr *tb[TCA_HTB_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 struct tc_htb_opt *hopt;
1325
1326 /* extract all subattrs from opt attr */
Patrick McHardycee63722008-01-23 20:33:32 -08001327 if (!opt)
1328 goto failure;
1329
Changli Gaoe18434c2010-09-30 06:17:44 +00001330 err = nla_parse_nested(tb, TCA_HTB_MAX, opt, htb_policy);
Patrick McHardycee63722008-01-23 20:33:32 -08001331 if (err < 0)
1332 goto failure;
1333
1334 err = -EINVAL;
Patrick McHardy27a34212008-01-23 20:35:39 -08001335 if (tb[TCA_HTB_PARMS] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Stephen Hemminger87990462006-08-10 23:35:16 -07001338 parent = parentid == TC_H_ROOT ? NULL : htb_find(parentid, sch);
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001339
Patrick McHardy1e904742008-01-22 22:11:17 -08001340 hopt = nla_data(tb[TCA_HTB_PARMS]);
Eric Dumazet196d97f2012-11-05 16:40:49 +00001341 if (!hopt->rate.rate || !hopt->ceil.rate)
Stephen Hemminger87990462006-08-10 23:35:16 -07001342 goto failure;
1343
1344 if (!cl) { /* new class */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 struct Qdisc *new_q;
Stephen Hemminger3696f622006-08-10 23:36:01 -07001346 int prio;
Patrick McHardyee39e102007-07-02 22:48:13 -07001347 struct {
Patrick McHardy1e904742008-01-22 22:11:17 -08001348 struct nlattr nla;
Patrick McHardyee39e102007-07-02 22:48:13 -07001349 struct gnet_estimator opt;
1350 } est = {
Patrick McHardy1e904742008-01-22 22:11:17 -08001351 .nla = {
1352 .nla_len = nla_attr_size(sizeof(est.opt)),
1353 .nla_type = TCA_RATE,
Patrick McHardyee39e102007-07-02 22:48:13 -07001354 },
1355 .opt = {
1356 /* 4s interval, 16s averaging constant */
1357 .interval = 2,
1358 .ewma_log = 2,
1359 },
1360 };
Stephen Hemminger3696f622006-08-10 23:36:01 -07001361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 /* check for valid classid */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001363 if (!classid || TC_H_MAJ(classid ^ sch->handle) ||
1364 htb_find(classid, sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 goto failure;
1366
1367 /* check maximal depth */
1368 if (parent && parent->parent && parent->parent->level < 2) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001369 pr_err("htb: tree is too deep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 goto failure;
1371 }
1372 err = -ENOBUFS;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001373 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
1374 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 goto failure;
Stephen Hemminger87990462006-08-10 23:35:16 -07001376
Eric Dumazet64153ce2013-06-06 14:53:16 -07001377 if (htb_rate_est || tca[TCA_RATE]) {
1378 err = gen_new_estimator(&cl->bstats, &cl->rate_est,
1379 qdisc_root_sleeping_lock(sch),
1380 tca[TCA_RATE] ? : &est.nla);
1381 if (err) {
1382 kfree(cl);
1383 goto failure;
1384 }
Stephen Hemminger71bcb092008-11-25 21:13:31 -08001385 }
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 cl->refcnt = 1;
Patrick McHardy42077592008-07-05 23:22:53 -07001388 cl->children = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 INIT_LIST_HEAD(&cl->un.leaf.drop_list);
Stephen Hemminger3696f622006-08-10 23:36:01 -07001390 RB_CLEAR_NODE(&cl->pq_node);
1391
1392 for (prio = 0; prio < TC_HTB_NUMPRIO; prio++)
1393 RB_CLEAR_NODE(&cl->node[prio]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 /* create leaf qdisc early because it uses kmalloc(GFP_KERNEL)
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001396 * so that can't be used inside of sch_tree_lock
1397 * -- thanks to Karlis Peisenieks
1398 */
Changli Gao3511c912010-10-16 13:04:08 +00001399 new_q = qdisc_create_dflt(sch->dev_queue,
David S. Millerbb949fb2008-07-08 16:55:56 -07001400 &pfifo_qdisc_ops, classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 sch_tree_lock(sch);
1402 if (parent && !parent->level) {
Patrick McHardy256d61b2006-11-29 17:37:05 -08001403 unsigned int qlen = parent->un.leaf.q->q.qlen;
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 /* turn parent into inner node */
Patrick McHardy256d61b2006-11-29 17:37:05 -08001406 qdisc_reset(parent->un.leaf.q);
1407 qdisc_tree_decrease_qlen(parent->un.leaf.q, qlen);
Stephen Hemminger87990462006-08-10 23:35:16 -07001408 qdisc_destroy(parent->un.leaf.q);
1409 if (parent->prio_activity)
1410 htb_deactivate(q, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 /* remove from evt list because of level change */
1413 if (parent->cmode != HTB_CAN_SEND) {
Stephen Hemminger3696f622006-08-10 23:36:01 -07001414 htb_safe_rb_erase(&parent->pq_node, q->wait_pq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 parent->cmode = HTB_CAN_SEND;
1416 }
1417 parent->level = (parent->parent ? parent->parent->level
Stephen Hemminger87990462006-08-10 23:35:16 -07001418 : TC_HTB_MAXDEPTH) - 1;
1419 memset(&parent->un.inner, 0, sizeof(parent->un.inner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421 /* leaf (we) needs elementary qdisc */
1422 cl->un.leaf.q = new_q ? new_q : &noop_qdisc;
1423
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001424 cl->common.classid = classid;
Stephen Hemminger87990462006-08-10 23:35:16 -07001425 cl->parent = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 /* set class to be in HTB_CAN_SEND state */
Jiri Pirkob9a7afd2013-02-12 00:12:02 +00001428 cl->tokens = PSCHED_TICKS2NS(hopt->buffer);
1429 cl->ctokens = PSCHED_TICKS2NS(hopt->cbuffer);
Eric Dumazet5343a7f2013-06-04 07:11:48 +00001430 cl->mbuffer = 60ULL * NSEC_PER_SEC; /* 1min */
1431 cl->t_c = ktime_to_ns(ktime_get());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 cl->cmode = HTB_CAN_SEND;
1433
1434 /* attach to the hash list and parent's family */
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001435 qdisc_class_hash_insert(&q->clhash, &cl->common);
Patrick McHardy42077592008-07-05 23:22:53 -07001436 if (parent)
1437 parent->children++;
Patrick McHardyee39e102007-07-02 22:48:13 -07001438 } else {
Stephen Hemminger71bcb092008-11-25 21:13:31 -08001439 if (tca[TCA_RATE]) {
1440 err = gen_replace_estimator(&cl->bstats, &cl->rate_est,
1441 qdisc_root_sleeping_lock(sch),
1442 tca[TCA_RATE]);
1443 if (err)
1444 return err;
1445 }
Stephen Hemminger87990462006-08-10 23:35:16 -07001446 sch_tree_lock(sch);
Patrick McHardyee39e102007-07-02 22:48:13 -07001447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 /* it used to be a nasty bug here, we have to check that node
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001450 * is really leaf before changing cl->un.leaf !
1451 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (!cl->level) {
Eric Dumazet196d97f2012-11-05 16:40:49 +00001453 cl->quantum = hopt->rate.rate / q->rate2quantum;
Jarek Poplawskic19f7a32008-12-03 21:09:45 -08001454 if (!hopt->quantum && cl->quantum < 1000) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001455 pr_warning(
Stephen Hemminger87990462006-08-10 23:35:16 -07001456 "HTB: quantum of class %X is small. Consider r2q change.\n",
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001457 cl->common.classid);
Jarek Poplawskic19f7a32008-12-03 21:09:45 -08001458 cl->quantum = 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
Jarek Poplawskic19f7a32008-12-03 21:09:45 -08001460 if (!hopt->quantum && cl->quantum > 200000) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001461 pr_warning(
Stephen Hemminger87990462006-08-10 23:35:16 -07001462 "HTB: quantum of class %X is big. Consider r2q change.\n",
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001463 cl->common.classid);
Jarek Poplawskic19f7a32008-12-03 21:09:45 -08001464 cl->quantum = 200000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
1466 if (hopt->quantum)
Jarek Poplawskic19f7a32008-12-03 21:09:45 -08001467 cl->quantum = hopt->quantum;
1468 if ((cl->prio = hopt->prio) >= TC_HTB_NUMPRIO)
1469 cl->prio = TC_HTB_NUMPRIO - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 }
1471
Eric Dumazet01cb71d2013-06-02 13:55:05 +00001472 psched_ratecfg_precompute(&cl->rate, &hopt->rate);
1473 psched_ratecfg_precompute(&cl->ceil, &hopt->ceil);
Vimalkumar56b765b2012-10-31 06:04:11 +00001474
Jiri Pirko324f5aa2013-02-12 00:11:59 +00001475 cl->buffer = PSCHED_TICKS2NS(hopt->buffer);
1476 cl->cbuffer = PSCHED_TICKS2NS(hopt->buffer);
Vimalkumar56b765b2012-10-31 06:04:11 +00001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 sch_tree_unlock(sch);
1479
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001480 qdisc_class_hash_grow(sch, &q->clhash);
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 *arg = (unsigned long)cl;
1483 return 0;
1484
1485failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return err;
1487}
1488
1489static struct tcf_proto **htb_find_tcf(struct Qdisc *sch, unsigned long arg)
1490{
1491 struct htb_sched *q = qdisc_priv(sch);
1492 struct htb_class *cl = (struct htb_class *)arg;
1493 struct tcf_proto **fl = cl ? &cl->filter_list : &q->filter_list;
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return fl;
1496}
1497
1498static unsigned long htb_bind_filter(struct Qdisc *sch, unsigned long parent,
Stephen Hemminger87990462006-08-10 23:35:16 -07001499 u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Stephen Hemminger87990462006-08-10 23:35:16 -07001501 struct htb_class *cl = htb_find(classid, sch);
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 /*if (cl && !cl->level) return 0;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001504 * The line above used to be there to prevent attaching filters to
1505 * leaves. But at least tc_index filter uses this just to get class
1506 * for other reasons so that we have to allow for it.
1507 * ----
1508 * 19.6.2002 As Werner explained it is ok - bind filter is just
1509 * another way to "lock" the class - unlike "get" this lock can
1510 * be broken by class during destroy IIUC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 */
Stephen Hemminger87990462006-08-10 23:35:16 -07001512 if (cl)
1513 cl->filter_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return (unsigned long)cl;
1515}
1516
1517static void htb_unbind_filter(struct Qdisc *sch, unsigned long arg)
1518{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 struct htb_class *cl = (struct htb_class *)arg;
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001520
Stephen Hemminger87990462006-08-10 23:35:16 -07001521 if (cl)
1522 cl->filter_cnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
1525static void htb_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1526{
1527 struct htb_sched *q = qdisc_priv(sch);
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001528 struct htb_class *cl;
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001529 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 if (arg->stop)
1532 return;
1533
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001534 for (i = 0; i < q->clhash.hashsize; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001535 hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (arg->count < arg->skip) {
1537 arg->count++;
1538 continue;
1539 }
1540 if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
1541 arg->stop = 1;
1542 return;
1543 }
1544 arg->count++;
1545 }
1546 }
1547}
1548
Eric Dumazet20fea082007-11-14 01:44:41 -08001549static const struct Qdisc_class_ops htb_class_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 .graft = htb_graft,
1551 .leaf = htb_leaf,
Patrick McHardy256d61b2006-11-29 17:37:05 -08001552 .qlen_notify = htb_qlen_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 .get = htb_get,
1554 .put = htb_put,
1555 .change = htb_change_class,
1556 .delete = htb_delete,
1557 .walk = htb_walk,
1558 .tcf_chain = htb_find_tcf,
1559 .bind_tcf = htb_bind_filter,
1560 .unbind_tcf = htb_unbind_filter,
1561 .dump = htb_dump_class,
1562 .dump_stats = htb_dump_class_stats,
1563};
1564
Eric Dumazet20fea082007-11-14 01:44:41 -08001565static struct Qdisc_ops htb_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 .cl_ops = &htb_class_ops,
1567 .id = "htb",
1568 .priv_size = sizeof(struct htb_sched),
1569 .enqueue = htb_enqueue,
1570 .dequeue = htb_dequeue,
Jarek Poplawski77be1552008-10-31 00:47:01 -07001571 .peek = qdisc_peek_dequeued,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 .drop = htb_drop,
1573 .init = htb_init,
1574 .reset = htb_reset,
1575 .destroy = htb_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 .dump = htb_dump,
1577 .owner = THIS_MODULE,
1578};
1579
1580static int __init htb_module_init(void)
1581{
Stephen Hemminger87990462006-08-10 23:35:16 -07001582 return register_qdisc(&htb_qdisc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
Stephen Hemminger87990462006-08-10 23:35:16 -07001584static void __exit htb_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Stephen Hemminger87990462006-08-10 23:35:16 -07001586 unregister_qdisc(&htb_qdisc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
Stephen Hemminger87990462006-08-10 23:35:16 -07001588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589module_init(htb_module_init)
1590module_exit(htb_module_exit)
1591MODULE_LICENSE("GPL");