blob: 570ef3b0c09b7abe49da4787b668397d4413f0e8 [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>
Jiri Pirkocf1facd2017-02-09 14:38:56 +010043#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/* HTB algorithm.
46 Author: devik@cdi.cz
47 ========================================================================
48 HTB is like TBF with multiple classes. It is also similar to CBQ because
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090049 it allows to assign priority to each class in hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 In fact it is another implementation of Floyd's formal sharing.
51
52 Levels:
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090053 Each class is assigned level. Leaf has ALWAYS level 0 and root
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 classes have level TC_HTB_MAXDEPTH-1. Interior nodes has level
55 one less than their parent.
56*/
57
Jesper Dangaard Brouer47083fc2008-06-16 16:39:32 -070058static int htb_hysteresis __read_mostly = 0; /* whether to use mode hysteresis for speedup */
Stephen Hemminger87990462006-08-10 23:35:16 -070059#define HTB_VER 0x30011 /* major must be matched with number suplied by TC as version */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#if HTB_VER >> 16 != TC_HTB_PROTOVER
62#error "Mismatched sch_htb.c and pkt_sch.h"
63#endif
64
Jesper Dangaard Brouer47083fc2008-06-16 16:39:32 -070065/* Module parameter and sysfs export */
66module_param (htb_hysteresis, int, 0640);
67MODULE_PARM_DESC(htb_hysteresis, "Hysteresis mode, less CPU load, less accurate");
68
Eric Dumazet64153ce2013-06-06 14:53:16 -070069static int htb_rate_est = 0; /* htb classes have a default rate estimator */
70module_param(htb_rate_est, int, 0640);
71MODULE_PARM_DESC(htb_rate_est, "setup a default rate estimator (4sec 16sec) for htb classes");
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* used internaly to keep status of single class */
74enum htb_cmode {
Stephen Hemminger87990462006-08-10 23:35:16 -070075 HTB_CANT_SEND, /* class can't send and can't borrow */
76 HTB_MAY_BORROW, /* class can't send but may borrow */
77 HTB_CAN_SEND /* class can send */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
Eric Dumazetc9364632013-06-15 03:30:10 -070080struct htb_prio {
81 union {
82 struct rb_root row;
83 struct rb_root feed;
84 };
85 struct rb_node *ptr;
86 /* When class changes from state 1->2 and disconnects from
87 * parent's feed then we lost ptr value and start from the
88 * first child again. Here we store classid of the
89 * last valid ptr (used when ptr is NULL).
90 */
91 u32 last_ptr_id;
92};
93
Eric Dumazetca4ec902013-06-13 07:58:30 -070094/* interior & leaf nodes; props specific to leaves are marked L:
95 * To reduce false sharing, place mostly read fields at beginning,
96 * and mostly written ones at the end.
97 */
Stephen Hemminger87990462006-08-10 23:35:16 -070098struct htb_class {
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -070099 struct Qdisc_class_common common;
Eric Dumazetca4ec902013-06-13 07:58:30 -0700100 struct psched_ratecfg rate;
101 struct psched_ratecfg ceil;
102 s64 buffer, cbuffer;/* token bucket depth/rate */
103 s64 mbuffer; /* max wait time */
stephen hemmingercbd37552013-08-01 22:32:07 -0700104 u32 prio; /* these two are used only by leaves... */
Eric Dumazetca4ec902013-06-13 07:58:30 -0700105 int quantum; /* but stored for parent-to-leaf return */
106
John Fastabend25d8c0d2014-09-12 20:05:27 -0700107 struct tcf_proto __rcu *filter_list; /* class attached filters */
Eric Dumazetca4ec902013-06-13 07:58:30 -0700108 int filter_cnt;
109 int refcnt; /* usage count of this class */
110
111 int level; /* our level (see above) */
112 unsigned int children;
113 struct htb_class *parent; /* parent class */
114
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800115 struct net_rate_estimator __rcu *rate_est;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Eric Dumazetca4ec902013-06-13 07:58:30 -0700117 /*
118 * Written often fields
119 */
120 struct gnet_stats_basic_packed bstats;
Eric Dumazetca4ec902013-06-13 07:58:30 -0700121 struct tc_htb_xstats xstats; /* our special stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Eric Dumazetca4ec902013-06-13 07:58:30 -0700123 /* token bucket parameters */
124 s64 tokens, ctokens;/* current number of tokens */
125 s64 t_c; /* checkpoint time */
Jarek Poplawskic19f7a32008-12-03 21:09:45 -0800126
Stephen Hemminger87990462006-08-10 23:35:16 -0700127 union {
128 struct htb_class_leaf {
Stephen Hemminger87990462006-08-10 23:35:16 -0700129 struct list_head drop_list;
Eric Dumazetc9364632013-06-15 03:30:10 -0700130 int deficit[TC_HTB_MAXDEPTH];
131 struct Qdisc *q;
Stephen Hemminger87990462006-08-10 23:35:16 -0700132 } leaf;
133 struct htb_class_inner {
Eric Dumazetc9364632013-06-15 03:30:10 -0700134 struct htb_prio clprio[TC_HTB_NUMPRIO];
Stephen Hemminger87990462006-08-10 23:35:16 -0700135 } inner;
136 } un;
Eric Dumazetca4ec902013-06-13 07:58:30 -0700137 s64 pq_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Eric Dumazetca4ec902013-06-13 07:58:30 -0700139 int prio_activity; /* for which prios are we active */
140 enum htb_cmode cmode; /* current mode of the class */
141 struct rb_node pq_node; /* node for event queue */
142 struct rb_node node[TC_HTB_NUMPRIO]; /* node for self or feed tree */
Eric Dumazet338ed9b2016-06-21 23:16:51 -0700143
144 unsigned int drops ____cacheline_aligned_in_smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
Eric Dumazetc9364632013-06-15 03:30:10 -0700147struct htb_level {
148 struct rb_root wait_pq;
149 struct htb_prio hprio[TC_HTB_NUMPRIO];
150};
151
Stephen Hemminger87990462006-08-10 23:35:16 -0700152struct htb_sched {
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700153 struct Qdisc_class_hash clhash;
Eric Dumazetc9364632013-06-15 03:30:10 -0700154 int defcls; /* class where unclassified flows go to */
155 int rate2quantum; /* quant = rate / rate2quantum */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Stephen Hemminger87990462006-08-10 23:35:16 -0700157 /* filters for qdisc itself */
John Fastabend25d8c0d2014-09-12 20:05:27 -0700158 struct tcf_proto __rcu *filter_list;
Jarek Poplawskie82181d2009-02-01 01:13:05 -0800159
160#define HTB_WARN_TOOMANYEVENTS 0x1
Eric Dumazetc9364632013-06-15 03:30:10 -0700161 unsigned int warned; /* only one warning */
162 int direct_qlen;
163 struct work_struct work;
164
165 /* non shaped skbs; let them go directly thru */
Florian Westphal48da34b2016-09-18 00:57:34 +0200166 struct qdisc_skb_head direct_queue;
Eric Dumazetc9364632013-06-15 03:30:10 -0700167 long direct_pkts;
168
169 struct qdisc_watchdog watchdog;
170
171 s64 now; /* cached dequeue time */
172 struct list_head drops[TC_HTB_NUMPRIO];/* active leaves (for drops) */
173
174 /* time of nearest event per level (row) */
175 s64 near_ev_cache[TC_HTB_MAXDEPTH];
176
177 int row_mask[TC_HTB_MAXDEPTH];
178
179 struct htb_level hlevel[TC_HTB_MAXDEPTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180};
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/* find class in global hash table using given handle */
Stephen Hemminger87990462006-08-10 23:35:16 -0700183static inline struct htb_class *htb_find(u32 handle, struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 struct htb_sched *q = qdisc_priv(sch);
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700186 struct Qdisc_class_common *clc;
Stephen Hemminger0cef2962006-08-10 23:35:38 -0700187
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700188 clc = qdisc_class_find(&q->clhash, handle);
189 if (clc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return NULL;
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700191 return container_of(clc, struct htb_class, common);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/**
195 * htb_classify - classify a packet into class
196 *
197 * It returns NULL if the packet should be dropped or -1 if the packet
198 * should be passed directly thru. In all other cases leaf class is returned.
199 * We allow direct class selection by classid in priority. The we examine
200 * filters in qdisc and in inner nodes (if higher filter points to the inner
201 * node). If we end up with classid MAJOR:0 we enqueue the skb into special
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900202 * internal fifo (direct). These packets then go directly thru. If we still
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300203 * have no valid leaf we try to use MAJOR:default leaf. It still unsuccessful
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * then finish and return direct queue.
205 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000206#define HTB_DIRECT ((struct htb_class *)-1L)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Stephen Hemminger87990462006-08-10 23:35:16 -0700208static struct htb_class *htb_classify(struct sk_buff *skb, struct Qdisc *sch,
209 int *qerr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 struct htb_sched *q = qdisc_priv(sch);
212 struct htb_class *cl;
213 struct tcf_result res;
214 struct tcf_proto *tcf;
215 int result;
216
217 /* allow to select class by setting skb->priority to valid classid;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000218 * note that nfmark can be used too by attaching filter fw with no
219 * rules in it
220 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (skb->priority == sch->handle)
Stephen Hemminger87990462006-08-10 23:35:16 -0700222 return HTB_DIRECT; /* X:0 (direct flow) selected */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000223 cl = htb_find(skb->priority, sch);
Harry Mason29824312014-01-17 13:22:32 +0000224 if (cl) {
225 if (cl->level == 0)
226 return cl;
227 /* Start with inner filter chain if a non-leaf class is selected */
John Fastabend25d8c0d2014-09-12 20:05:27 -0700228 tcf = rcu_dereference_bh(cl->filter_list);
Harry Mason29824312014-01-17 13:22:32 +0000229 } else {
John Fastabend25d8c0d2014-09-12 20:05:27 -0700230 tcf = rcu_dereference_bh(q->filter_list);
Harry Mason29824312014-01-17 13:22:32 +0000231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Jarek Poplawskic27f3392008-08-04 22:39:11 -0700233 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
Daniel Borkmann3b3ae882015-08-26 23:00:06 +0200234 while (tcf && (result = tc_classify(skb, tcf, &res, false)) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#ifdef CONFIG_NET_CLS_ACT
236 switch (result) {
237 case TC_ACT_QUEUED:
Stephen Hemminger87990462006-08-10 23:35:16 -0700238 case TC_ACT_STOLEN:
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700239 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 case TC_ACT_SHOT:
241 return NULL;
242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#endif
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000244 cl = (void *)res.class;
245 if (!cl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (res.classid == sch->handle)
Stephen Hemminger87990462006-08-10 23:35:16 -0700247 return HTB_DIRECT; /* X:0 (direct flow) */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000248 cl = htb_find(res.classid, sch);
249 if (!cl)
Stephen Hemminger87990462006-08-10 23:35:16 -0700250 break; /* filter selected invalid classid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 if (!cl->level)
Stephen Hemminger87990462006-08-10 23:35:16 -0700253 return cl; /* we hit leaf; return it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 /* we have got inner class; apply inner filter chain */
John Fastabend25d8c0d2014-09-12 20:05:27 -0700256 tcf = rcu_dereference_bh(cl->filter_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258 /* classification failed; try to use default class */
Stephen Hemminger87990462006-08-10 23:35:16 -0700259 cl = htb_find(TC_H_MAKE(TC_H_MAJ(sch->handle), q->defcls), sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (!cl || cl->level)
Stephen Hemminger87990462006-08-10 23:35:16 -0700261 return HTB_DIRECT; /* bad default .. this is safe bet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return cl;
263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/**
266 * htb_add_to_id_tree - adds class to the round robin list
267 *
268 * Routine adds class to the list (actually tree) sorted by classid.
269 * Make sure that class is not already on such list for given prio.
270 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700271static void htb_add_to_id_tree(struct rb_root *root,
272 struct htb_class *cl, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct rb_node **p = &root->rb_node, *parent = NULL;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 while (*p) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700277 struct htb_class *c;
278 parent = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 c = rb_entry(parent, struct htb_class, node[prio]);
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700280
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700281 if (cl->common.classid > c->common.classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 p = &parent->rb_right;
Stephen Hemminger87990462006-08-10 23:35:16 -0700283 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 p = &parent->rb_left;
285 }
286 rb_link_node(&cl->node[prio], parent, p);
287 rb_insert_color(&cl->node[prio], root);
288}
289
290/**
291 * htb_add_to_wait_tree - adds class to the event queue with delay
292 *
293 * The class is added to priority event queue to indicate that class will
294 * change its mode in cl->pq_key microseconds. Make sure that class is not
295 * already in the queue.
296 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700297static void htb_add_to_wait_tree(struct htb_sched *q,
Vimalkumar56b765b2012-10-31 06:04:11 +0000298 struct htb_class *cl, s64 delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Eric Dumazetc9364632013-06-15 03:30:10 -0700300 struct rb_node **p = &q->hlevel[cl->level].wait_pq.rb_node, *parent = NULL;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700301
Patrick McHardyfb983d42007-03-16 01:22:39 -0700302 cl->pq_key = q->now + delay;
303 if (cl->pq_key == q->now)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 cl->pq_key++;
305
306 /* update the nearest event cache */
Patrick McHardyfb983d42007-03-16 01:22:39 -0700307 if (q->near_ev_cache[cl->level] > cl->pq_key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 q->near_ev_cache[cl->level] = cl->pq_key;
Stephen Hemminger87990462006-08-10 23:35:16 -0700309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 while (*p) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700311 struct htb_class *c;
312 parent = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 c = rb_entry(parent, struct htb_class, pq_node);
Patrick McHardyfb983d42007-03-16 01:22:39 -0700314 if (cl->pq_key >= c->pq_key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 p = &parent->rb_right;
Stephen Hemminger87990462006-08-10 23:35:16 -0700316 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 p = &parent->rb_left;
318 }
319 rb_link_node(&cl->pq_node, parent, p);
Eric Dumazetc9364632013-06-15 03:30:10 -0700320 rb_insert_color(&cl->pq_node, &q->hlevel[cl->level].wait_pq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323/**
324 * htb_next_rb_node - finds next node in binary tree
325 *
326 * When we are past last key we return NULL.
327 * Average complexity is 2 steps per call.
328 */
Stephen Hemminger3696f622006-08-10 23:36:01 -0700329static inline void htb_next_rb_node(struct rb_node **n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 *n = rb_next(*n);
332}
333
334/**
335 * htb_add_class_to_row - add class to its row
336 *
337 * The class is added to row at priorities marked in mask.
338 * It does nothing if mask == 0.
339 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700340static inline void htb_add_class_to_row(struct htb_sched *q,
341 struct htb_class *cl, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 q->row_mask[cl->level] |= mask;
344 while (mask) {
345 int prio = ffz(~mask);
346 mask &= ~(1 << prio);
Eric Dumazetc9364632013-06-15 03:30:10 -0700347 htb_add_to_id_tree(&q->hlevel[cl->level].hprio[prio].row, cl, prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349}
350
Stephen Hemminger3696f622006-08-10 23:36:01 -0700351/* If this triggers, it is a bug in this code, but it need not be fatal */
352static void htb_safe_rb_erase(struct rb_node *rb, struct rb_root *root)
353{
Ismail Donmez81771b32006-10-03 13:49:10 -0700354 if (RB_EMPTY_NODE(rb)) {
Stephen Hemminger3696f622006-08-10 23:36:01 -0700355 WARN_ON(1);
356 } else {
357 rb_erase(rb, root);
358 RB_CLEAR_NODE(rb);
359 }
360}
361
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363/**
364 * htb_remove_class_from_row - removes class from its row
365 *
366 * The class is removed from row at priorities marked in mask.
367 * It does nothing if mask == 0.
368 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700369static inline void htb_remove_class_from_row(struct htb_sched *q,
370 struct htb_class *cl, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 int m = 0;
Eric Dumazetc9364632013-06-15 03:30:10 -0700373 struct htb_level *hlevel = &q->hlevel[cl->level];
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 while (mask) {
376 int prio = ffz(~mask);
Eric Dumazetc9364632013-06-15 03:30:10 -0700377 struct htb_prio *hprio = &hlevel->hprio[prio];
Stephen Hemminger3696f622006-08-10 23:36:01 -0700378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 mask &= ~(1 << prio);
Eric Dumazetc9364632013-06-15 03:30:10 -0700380 if (hprio->ptr == cl->node + prio)
381 htb_next_rb_node(&hprio->ptr);
Stephen Hemminger3696f622006-08-10 23:36:01 -0700382
Eric Dumazetc9364632013-06-15 03:30:10 -0700383 htb_safe_rb_erase(cl->node + prio, &hprio->row);
384 if (!hprio->row.rb_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 m |= 1 << prio;
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 q->row_mask[cl->level] &= ~m;
388}
389
390/**
391 * htb_activate_prios - creates active classe's feed chain
392 *
393 * The class is connected to ancestors and/or appropriate rows
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900394 * for priorities it is participating on. cl->cmode must be new
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 * (activated) mode. It does nothing if cl->prio_activity == 0.
396 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700397static void htb_activate_prios(struct htb_sched *q, struct htb_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 struct htb_class *p = cl->parent;
Stephen Hemminger87990462006-08-10 23:35:16 -0700400 long m, mask = cl->prio_activity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 while (cl->cmode == HTB_MAY_BORROW && p && mask) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700403 m = mask;
404 while (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 int prio = ffz(~m);
406 m &= ~(1 << prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700407
Eric Dumazetc9364632013-06-15 03:30:10 -0700408 if (p->un.inner.clprio[prio].feed.rb_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /* parent already has its feed in use so that
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000410 * reset bit in mask as parent is already ok
411 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 mask &= ~(1 << prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700413
Eric Dumazetc9364632013-06-15 03:30:10 -0700414 htb_add_to_id_tree(&p->un.inner.clprio[prio].feed, cl, prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 p->prio_activity |= mask;
Stephen Hemminger87990462006-08-10 23:35:16 -0700417 cl = p;
418 p = cl->parent;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421 if (cl->cmode == HTB_CAN_SEND && mask)
Stephen Hemminger87990462006-08-10 23:35:16 -0700422 htb_add_class_to_row(q, cl, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425/**
426 * htb_deactivate_prios - remove class from feed chain
427 *
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900428 * cl->cmode must represent old mode (before deactivation). It does
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * nothing if cl->prio_activity == 0. Class is removed from all feed
430 * chains and rows.
431 */
432static void htb_deactivate_prios(struct htb_sched *q, struct htb_class *cl)
433{
434 struct htb_class *p = cl->parent;
Stephen Hemminger87990462006-08-10 23:35:16 -0700435 long m, mask = cl->prio_activity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 while (cl->cmode == HTB_MAY_BORROW && p && mask) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700438 m = mask;
439 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 while (m) {
441 int prio = ffz(~m);
442 m &= ~(1 << prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700443
Eric Dumazetc9364632013-06-15 03:30:10 -0700444 if (p->un.inner.clprio[prio].ptr == cl->node + prio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /* we are removing child which is pointed to from
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000446 * parent feed - forget the pointer but remember
447 * classid
448 */
Eric Dumazetc9364632013-06-15 03:30:10 -0700449 p->un.inner.clprio[prio].last_ptr_id = cl->common.classid;
450 p->un.inner.clprio[prio].ptr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
Stephen Hemminger87990462006-08-10 23:35:16 -0700452
Eric Dumazetc9364632013-06-15 03:30:10 -0700453 htb_safe_rb_erase(cl->node + prio,
454 &p->un.inner.clprio[prio].feed);
Stephen Hemminger87990462006-08-10 23:35:16 -0700455
Eric Dumazetc9364632013-06-15 03:30:10 -0700456 if (!p->un.inner.clprio[prio].feed.rb_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 mask |= 1 << prio;
458 }
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 p->prio_activity &= ~mask;
Stephen Hemminger87990462006-08-10 23:35:16 -0700461 cl = p;
462 p = cl->parent;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
Stephen Hemminger87990462006-08-10 23:35:16 -0700465 if (cl->cmode == HTB_CAN_SEND && mask)
466 htb_remove_class_from_row(q, cl, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Vimalkumar56b765b2012-10-31 06:04:11 +0000469static inline s64 htb_lowater(const struct htb_class *cl)
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700470{
Jesper Dangaard Brouer47083fc2008-06-16 16:39:32 -0700471 if (htb_hysteresis)
472 return cl->cmode != HTB_CANT_SEND ? -cl->cbuffer : 0;
473 else
474 return 0;
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700475}
Vimalkumar56b765b2012-10-31 06:04:11 +0000476static inline s64 htb_hiwater(const struct htb_class *cl)
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700477{
Jesper Dangaard Brouer47083fc2008-06-16 16:39:32 -0700478 if (htb_hysteresis)
479 return cl->cmode == HTB_CAN_SEND ? -cl->buffer : 0;
480 else
481 return 0;
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700482}
Jesper Dangaard Brouer47083fc2008-06-16 16:39:32 -0700483
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485/**
486 * htb_class_mode - computes and returns current class mode
487 *
488 * It computes cl's mode at time cl->t_c+diff and returns it. If mode
489 * is not HTB_CAN_SEND then cl->pq_key is updated to time difference
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900490 * from now to time when cl will change its state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 * Also it is worth to note that class mode doesn't change simply
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900492 * at cl->{c,}tokens == 0 but there can rather be hysteresis of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * 0 .. -cl->{c,}buffer range. It is meant to limit number of
494 * mode transitions per time unit. The speed gain is about 1/6.
495 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700496static inline enum htb_cmode
Vimalkumar56b765b2012-10-31 06:04:11 +0000497htb_class_mode(struct htb_class *cl, s64 *diff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Vimalkumar56b765b2012-10-31 06:04:11 +0000499 s64 toks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Stephen Hemminger87990462006-08-10 23:35:16 -0700501 if ((toks = (cl->ctokens + *diff)) < htb_lowater(cl)) {
502 *diff = -toks;
503 return HTB_CANT_SEND;
504 }
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700505
Stephen Hemminger87990462006-08-10 23:35:16 -0700506 if ((toks = (cl->tokens + *diff)) >= htb_hiwater(cl))
507 return HTB_CAN_SEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Stephen Hemminger87990462006-08-10 23:35:16 -0700509 *diff = -toks;
510 return HTB_MAY_BORROW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513/**
514 * htb_change_class_mode - changes classe's mode
515 *
516 * This should be the only way how to change classe's mode under normal
517 * cirsumstances. Routine will update feed lists linkage, change mode
518 * and add class to the wait event queue if appropriate. New mode should
519 * be different from old one and cl->pq_key has to be valid if changing
520 * to mode other than HTB_CAN_SEND (see htb_add_to_wait_tree).
521 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700522static void
Vimalkumar56b765b2012-10-31 06:04:11 +0000523htb_change_class_mode(struct htb_sched *q, struct htb_class *cl, s64 *diff)
Stephen Hemminger87990462006-08-10 23:35:16 -0700524{
525 enum htb_cmode new_mode = htb_class_mode(cl, diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 if (new_mode == cl->cmode)
Stephen Hemminger87990462006-08-10 23:35:16 -0700528 return;
529
530 if (cl->prio_activity) { /* not necessary: speed optimization */
531 if (cl->cmode != HTB_CANT_SEND)
532 htb_deactivate_prios(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 cl->cmode = new_mode;
Stephen Hemminger87990462006-08-10 23:35:16 -0700534 if (new_mode != HTB_CANT_SEND)
535 htb_activate_prios(q, cl);
536 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 cl->cmode = new_mode;
538}
539
540/**
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900541 * htb_activate - inserts leaf cl into appropriate active feeds
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 *
543 * Routine learns (new) priority of leaf and activates feed chain
544 * for the prio. It can be called on already active leaf safely.
545 * It also adds leaf into droplist.
546 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700547static inline void htb_activate(struct htb_sched *q, struct htb_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700549 WARN_ON(cl->level || !cl->un.leaf.q || !cl->un.leaf.q->q.qlen);
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (!cl->prio_activity) {
Jarek Poplawskic19f7a32008-12-03 21:09:45 -0800552 cl->prio_activity = 1 << cl->prio;
Stephen Hemminger87990462006-08-10 23:35:16 -0700553 htb_activate_prios(q, cl);
554 list_add_tail(&cl->un.leaf.drop_list,
Jarek Poplawskic19f7a32008-12-03 21:09:45 -0800555 q->drops + cl->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557}
558
559/**
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900560 * htb_deactivate - remove leaf cl from active feeds
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 *
562 * Make sure that leaf is active. In the other words it can't be called
563 * with non-active leaf. It also removes class from the drop list.
564 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700565static inline void htb_deactivate(struct htb_sched *q, struct htb_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700567 WARN_ON(!cl->prio_activity);
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700568
Stephen Hemminger87990462006-08-10 23:35:16 -0700569 htb_deactivate_prios(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 cl->prio_activity = 0;
571 list_del_init(&cl->un.leaf.drop_list);
572}
573
Florian Westphal48da34b2016-09-18 00:57:34 +0200574static void htb_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
575 struct qdisc_skb_head *qh)
576{
577 struct sk_buff *last = qh->tail;
578
579 if (last) {
580 skb->next = NULL;
581 last->next = skb;
582 qh->tail = skb;
583 } else {
584 qh->tail = skb;
585 qh->head = skb;
586 }
587 qh->qlen++;
588}
589
Eric Dumazet520ac302016-06-21 23:16:49 -0700590static int htb_enqueue(struct sk_buff *skb, struct Qdisc *sch,
591 struct sk_buff **to_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Jarek Poplawskif30ab412008-11-13 22:56:30 -0800593 int uninitialized_var(ret);
Stephen Hemminger87990462006-08-10 23:35:16 -0700594 struct htb_sched *q = qdisc_priv(sch);
595 struct htb_class *cl = htb_classify(skb, sch, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Stephen Hemminger87990462006-08-10 23:35:16 -0700597 if (cl == HTB_DIRECT) {
598 /* enqueue to helper queue */
599 if (q->direct_queue.qlen < q->direct_qlen) {
Florian Westphal48da34b2016-09-18 00:57:34 +0200600 htb_enqueue_tail(skb, sch, &q->direct_queue);
Stephen Hemminger87990462006-08-10 23:35:16 -0700601 q->direct_pkts++;
602 } else {
Eric Dumazet520ac302016-06-21 23:16:49 -0700603 return qdisc_drop(skb, sch, to_free);
Stephen Hemminger87990462006-08-10 23:35:16 -0700604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605#ifdef CONFIG_NET_CLS_ACT
Stephen Hemminger87990462006-08-10 23:35:16 -0700606 } else if (!cl) {
Jarek Poplawskic27f3392008-08-04 22:39:11 -0700607 if (ret & __NET_XMIT_BYPASS)
John Fastabend25331d62014-09-28 11:53:29 -0700608 qdisc_qstats_drop(sch);
Eric Dumazet520ac302016-06-21 23:16:49 -0700609 __qdisc_drop(skb, to_free);
Stephen Hemminger87990462006-08-10 23:35:16 -0700610 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611#endif
Eric Dumazet520ac302016-06-21 23:16:49 -0700612 } else if ((ret = qdisc_enqueue(skb, cl->un.leaf.q,
613 to_free)) != NET_XMIT_SUCCESS) {
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700614 if (net_xmit_drop_count(ret)) {
John Fastabend25331d62014-09-28 11:53:29 -0700615 qdisc_qstats_drop(sch);
Eric Dumazet338ed9b2016-06-21 23:16:51 -0700616 cl->drops++;
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700617 }
David S. Miller69747652008-08-17 23:55:36 -0700618 return ret;
Stephen Hemminger87990462006-08-10 23:35:16 -0700619 } else {
Stephen Hemminger87990462006-08-10 23:35:16 -0700620 htb_activate(q, cl);
621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
WANG Cong431e3a82016-02-25 14:55:02 -0800623 qdisc_qstats_backlog_inc(sch, skb);
Stephen Hemminger87990462006-08-10 23:35:16 -0700624 sch->q.qlen++;
Stephen Hemminger87990462006-08-10 23:35:16 -0700625 return NET_XMIT_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Vimalkumar56b765b2012-10-31 06:04:11 +0000628static inline void htb_accnt_tokens(struct htb_class *cl, int bytes, s64 diff)
Jarek Poplawski59e42202008-12-03 21:17:27 -0800629{
Vimalkumar56b765b2012-10-31 06:04:11 +0000630 s64 toks = diff + cl->tokens;
Jarek Poplawski59e42202008-12-03 21:17:27 -0800631
632 if (toks > cl->buffer)
633 toks = cl->buffer;
Jiri Pirko292f1c72013-02-12 00:12:03 +0000634 toks -= (s64) psched_l2t_ns(&cl->rate, bytes);
Jarek Poplawski59e42202008-12-03 21:17:27 -0800635 if (toks <= -cl->mbuffer)
636 toks = 1 - cl->mbuffer;
637
638 cl->tokens = toks;
639}
640
Vimalkumar56b765b2012-10-31 06:04:11 +0000641static inline void htb_accnt_ctokens(struct htb_class *cl, int bytes, s64 diff)
Jarek Poplawski59e42202008-12-03 21:17:27 -0800642{
Vimalkumar56b765b2012-10-31 06:04:11 +0000643 s64 toks = diff + cl->ctokens;
Jarek Poplawski59e42202008-12-03 21:17:27 -0800644
645 if (toks > cl->cbuffer)
646 toks = cl->cbuffer;
Jiri Pirko292f1c72013-02-12 00:12:03 +0000647 toks -= (s64) psched_l2t_ns(&cl->ceil, bytes);
Jarek Poplawski59e42202008-12-03 21:17:27 -0800648 if (toks <= -cl->mbuffer)
649 toks = 1 - cl->mbuffer;
650
651 cl->ctokens = toks;
652}
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654/**
655 * htb_charge_class - charges amount "bytes" to leaf and ancestors
656 *
657 * Routine assumes that packet "bytes" long was dequeued from leaf cl
658 * borrowing from "level". It accounts bytes to ceil leaky bucket for
659 * leaf and all ancestors and to rate bucket for ancestors at levels
660 * "level" and higher. It also handles possible change of mode resulting
661 * from the update. Note that mode can also increase here (MAY_BORROW to
662 * CAN_SEND) because we can use more precise clock that event queue here.
663 * In such case we remove class from event queue first.
664 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700665static void htb_charge_class(struct htb_sched *q, struct htb_class *cl,
Ranjit Manomohanc9726d62007-07-10 22:43:16 -0700666 int level, struct sk_buff *skb)
Stephen Hemminger87990462006-08-10 23:35:16 -0700667{
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700668 int bytes = qdisc_pkt_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 enum htb_cmode old_mode;
Vimalkumar56b765b2012-10-31 06:04:11 +0000670 s64 diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 while (cl) {
Vimalkumar56b765b2012-10-31 06:04:11 +0000673 diff = min_t(s64, q->now - cl->t_c, cl->mbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (cl->level >= level) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700675 if (cl->level == level)
676 cl->xstats.lends++;
Jarek Poplawski59e42202008-12-03 21:17:27 -0800677 htb_accnt_tokens(cl, bytes, diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 } else {
679 cl->xstats.borrows++;
Stephen Hemminger87990462006-08-10 23:35:16 -0700680 cl->tokens += diff; /* we moved t_c; update tokens */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
Jarek Poplawski59e42202008-12-03 21:17:27 -0800682 htb_accnt_ctokens(cl, bytes, diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 cl->t_c = q->now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Stephen Hemminger87990462006-08-10 23:35:16 -0700685 old_mode = cl->cmode;
686 diff = 0;
687 htb_change_class_mode(q, cl, &diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (old_mode != cl->cmode) {
689 if (old_mode != HTB_CAN_SEND)
Eric Dumazetc9364632013-06-15 03:30:10 -0700690 htb_safe_rb_erase(&cl->pq_node, &q->hlevel[cl->level].wait_pq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (cl->cmode != HTB_CAN_SEND)
Stephen Hemminger87990462006-08-10 23:35:16 -0700692 htb_add_to_wait_tree(q, cl, diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Eric Dumazetbfe0d022011-01-09 08:30:54 +0000695 /* update basic stats except for leaves which are already updated */
696 if (cl->level)
697 bstats_update(&cl->bstats, skb);
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 cl = cl->parent;
700 }
701}
702
703/**
704 * htb_do_events - make mode changes to classes at the level
705 *
Patrick McHardyfb983d42007-03-16 01:22:39 -0700706 * Scans event queue for pending events and applies them. Returns time of
Jarek Poplawski12247362009-02-01 01:13:22 -0800707 * next pending event (0 for no event in pq, q->now for too many events).
Patrick McHardyfb983d42007-03-16 01:22:39 -0700708 * Note: Applied are events whose have cl->pq_key <= q->now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 */
Eric Dumazetc9364632013-06-15 03:30:10 -0700710static s64 htb_do_events(struct htb_sched *q, const int level,
Eric Dumazet5343a7f2013-06-04 07:11:48 +0000711 unsigned long start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Martin Devera8f3ea332008-03-23 22:00:38 -0700713 /* don't run for longer than 2 jiffies; 2 is used instead of
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000714 * 1 to simplify things when jiffy is going to be incremented
715 * too soon
716 */
Jarek Poplawskia73be042009-01-12 21:54:40 -0800717 unsigned long stop_at = start + 2;
Eric Dumazetc9364632013-06-15 03:30:10 -0700718 struct rb_root *wait_pq = &q->hlevel[level].wait_pq;
719
Martin Devera8f3ea332008-03-23 22:00:38 -0700720 while (time_before(jiffies, stop_at)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 struct htb_class *cl;
Vimalkumar56b765b2012-10-31 06:04:11 +0000722 s64 diff;
Eric Dumazetc9364632013-06-15 03:30:10 -0700723 struct rb_node *p = rb_first(wait_pq);
Akinbou Mita30bdbe32006-10-12 01:52:05 -0700724
Stephen Hemminger87990462006-08-10 23:35:16 -0700725 if (!p)
726 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 cl = rb_entry(p, struct htb_class, pq_node);
Patrick McHardyfb983d42007-03-16 01:22:39 -0700729 if (cl->pq_key > q->now)
730 return cl->pq_key;
731
Eric Dumazetc9364632013-06-15 03:30:10 -0700732 htb_safe_rb_erase(p, wait_pq);
Vimalkumar56b765b2012-10-31 06:04:11 +0000733 diff = min_t(s64, q->now - cl->t_c, cl->mbuffer);
Stephen Hemminger87990462006-08-10 23:35:16 -0700734 htb_change_class_mode(q, cl, &diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (cl->cmode != HTB_CAN_SEND)
Stephen Hemminger87990462006-08-10 23:35:16 -0700736 htb_add_to_wait_tree(q, cl, diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
Jarek Poplawski12247362009-02-01 01:13:22 -0800738
739 /* too much load - let's continue after a break for scheduling */
Jarek Poplawskie82181d2009-02-01 01:13:05 -0800740 if (!(q->warned & HTB_WARN_TOOMANYEVENTS)) {
Yang Yingliangc17988a2013-12-23 17:38:58 +0800741 pr_warn("htb: too many events!\n");
Jarek Poplawskie82181d2009-02-01 01:13:05 -0800742 q->warned |= HTB_WARN_TOOMANYEVENTS;
743 }
Jarek Poplawski12247362009-02-01 01:13:22 -0800744
745 return q->now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
748/* Returns class->node+prio from id-tree where classe's id is >= id. NULL
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000749 * is no such one exists.
750 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700751static struct rb_node *htb_id_find_next_upper(int prio, struct rb_node *n,
752 u32 id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 struct rb_node *r = NULL;
755 while (n) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700756 struct htb_class *cl =
757 rb_entry(n, struct htb_class, node[prio]);
Stephen Hemminger87990462006-08-10 23:35:16 -0700758
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700759 if (id > cl->common.classid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 n = n->rb_right;
Jarek Poplawski1b5c0072008-12-09 22:34:40 -0800761 } else if (id < cl->common.classid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 r = n;
763 n = n->rb_left;
Jarek Poplawski1b5c0072008-12-09 22:34:40 -0800764 } else {
765 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767 }
768 return r;
769}
770
771/**
772 * htb_lookup_leaf - returns next leaf class in DRR order
773 *
774 * Find leaf where current feed pointers points to.
775 */
Eric Dumazetc9364632013-06-15 03:30:10 -0700776static struct htb_class *htb_lookup_leaf(struct htb_prio *hprio, const int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 int i;
779 struct {
780 struct rb_node *root;
781 struct rb_node **pptr;
782 u32 *pid;
Stephen Hemminger87990462006-08-10 23:35:16 -0700783 } stk[TC_HTB_MAXDEPTH], *sp = stk;
784
Eric Dumazetc9364632013-06-15 03:30:10 -0700785 BUG_ON(!hprio->row.rb_node);
786 sp->root = hprio->row.rb_node;
787 sp->pptr = &hprio->ptr;
788 sp->pid = &hprio->last_ptr_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 for (i = 0; i < 65535; i++) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700791 if (!*sp->pptr && *sp->pid) {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900792 /* ptr was invalidated but id is valid - try to recover
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000793 * the original or next ptr
794 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700795 *sp->pptr =
796 htb_id_find_next_upper(prio, sp->root, *sp->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
Stephen Hemminger87990462006-08-10 23:35:16 -0700798 *sp->pid = 0; /* ptr is valid now so that remove this hint as it
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000799 * can become out of date quickly
800 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700801 if (!*sp->pptr) { /* we are at right end; rewind & go up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 *sp->pptr = sp->root;
Stephen Hemminger87990462006-08-10 23:35:16 -0700803 while ((*sp->pptr)->rb_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 *sp->pptr = (*sp->pptr)->rb_left;
805 if (sp > stk) {
806 sp--;
Jarek Poplawski512bb432008-12-09 22:35:02 -0800807 if (!*sp->pptr) {
808 WARN_ON(1);
Stephen Hemminger87990462006-08-10 23:35:16 -0700809 return NULL;
Jarek Poplawski512bb432008-12-09 22:35:02 -0800810 }
Stephen Hemminger87990462006-08-10 23:35:16 -0700811 htb_next_rb_node(sp->pptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813 } else {
814 struct htb_class *cl;
Eric Dumazetc9364632013-06-15 03:30:10 -0700815 struct htb_prio *clp;
816
Stephen Hemminger87990462006-08-10 23:35:16 -0700817 cl = rb_entry(*sp->pptr, struct htb_class, node[prio]);
818 if (!cl->level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return cl;
Eric Dumazetc9364632013-06-15 03:30:10 -0700820 clp = &cl->un.inner.clprio[prio];
821 (++sp)->root = clp->feed.rb_node;
822 sp->pptr = &clp->ptr;
823 sp->pid = &clp->last_ptr_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700826 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return NULL;
828}
829
830/* dequeues packet at given priority and level; call only if
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000831 * you are sure that there is active class at prio/level
832 */
Eric Dumazetc9364632013-06-15 03:30:10 -0700833static struct sk_buff *htb_dequeue_tree(struct htb_sched *q, const int prio,
834 const int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
836 struct sk_buff *skb = NULL;
Stephen Hemminger87990462006-08-10 23:35:16 -0700837 struct htb_class *cl, *start;
Eric Dumazetc9364632013-06-15 03:30:10 -0700838 struct htb_level *hlevel = &q->hlevel[level];
839 struct htb_prio *hprio = &hlevel->hprio[prio];
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /* look initial class up in the row */
Eric Dumazetc9364632013-06-15 03:30:10 -0700842 start = cl = htb_lookup_leaf(hprio, prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 do {
845next:
Jarek Poplawski512bb432008-12-09 22:35:02 -0800846 if (unlikely(!cl))
Stephen Hemminger87990462006-08-10 23:35:16 -0700847 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /* class can be empty - it is unlikely but can be true if leaf
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000850 * qdisc drops packets in enqueue routine or if someone used
851 * graft operation on the leaf since last dequeue;
852 * simply deactivate and skip such class
853 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (unlikely(cl->un.leaf.q->q.qlen == 0)) {
855 struct htb_class *next;
Stephen Hemminger87990462006-08-10 23:35:16 -0700856 htb_deactivate(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 /* row/level might become empty */
859 if ((q->row_mask[level] & (1 << prio)) == 0)
Stephen Hemminger87990462006-08-10 23:35:16 -0700860 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Eric Dumazetc9364632013-06-15 03:30:10 -0700862 next = htb_lookup_leaf(hprio, prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700863
864 if (cl == start) /* fix start if we just deleted it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 start = next;
866 cl = next;
867 goto next;
868 }
Stephen Hemminger87990462006-08-10 23:35:16 -0700869
870 skb = cl->un.leaf.q->dequeue(cl->un.leaf.q);
871 if (likely(skb != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 break;
Jarek Poplawski633fe662008-12-03 21:09:10 -0800873
Jarek Poplawskib00355d2009-02-01 01:12:42 -0800874 qdisc_warn_nonwc("htb", cl->un.leaf.q);
Eric Dumazetc9364632013-06-15 03:30:10 -0700875 htb_next_rb_node(level ? &cl->parent->un.inner.clprio[prio].ptr:
876 &q->hlevel[0].hprio[prio].ptr);
877 cl = htb_lookup_leaf(hprio, prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 } while (cl != start);
880
881 if (likely(skb != NULL)) {
Eric Dumazet196d97f2012-11-05 16:40:49 +0000882 bstats_update(&cl->bstats, skb);
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700883 cl->un.leaf.deficit[level] -= qdisc_pkt_len(skb);
884 if (cl->un.leaf.deficit[level] < 0) {
Jarek Poplawskic19f7a32008-12-03 21:09:45 -0800885 cl->un.leaf.deficit[level] += cl->quantum;
Eric Dumazetc9364632013-06-15 03:30:10 -0700886 htb_next_rb_node(level ? &cl->parent->un.inner.clprio[prio].ptr :
887 &q->hlevel[0].hprio[prio].ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889 /* this used to be after charge_class but this constelation
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000890 * gives us slightly better performance
891 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (!cl->un.leaf.q->q.qlen)
Stephen Hemminger87990462006-08-10 23:35:16 -0700893 htb_deactivate(q, cl);
Ranjit Manomohanc9726d62007-07-10 22:43:16 -0700894 htb_charge_class(q, cl, level, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896 return skb;
897}
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899static struct sk_buff *htb_dequeue(struct Qdisc *sch)
900{
Eric Dumazet9190b3b2011-01-20 23:31:33 -0800901 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 struct htb_sched *q = qdisc_priv(sch);
903 int level;
Eric Dumazet5343a7f2013-06-04 07:11:48 +0000904 s64 next_event;
Jarek Poplawskia73be042009-01-12 21:54:40 -0800905 unsigned long start_at;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 /* try to dequeue direct packets as high prio (!) to minimize cpu work */
Florian Westphal48da34b2016-09-18 00:57:34 +0200908 skb = __qdisc_dequeue_head(&q->direct_queue);
Stephen Hemminger87990462006-08-10 23:35:16 -0700909 if (skb != NULL) {
Eric Dumazet9190b3b2011-01-20 23:31:33 -0800910ok:
911 qdisc_bstats_update(sch, skb);
WANG Cong431e3a82016-02-25 14:55:02 -0800912 qdisc_qstats_backlog_dec(sch, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 sch->q.qlen--;
914 return skb;
915 }
916
Stephen Hemminger87990462006-08-10 23:35:16 -0700917 if (!sch->q.qlen)
918 goto fin;
Eric Dumazetd2de8752014-08-22 18:32:09 -0700919 q->now = ktime_get_ns();
Jarek Poplawskia73be042009-01-12 21:54:40 -0800920 start_at = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Stefan Haskod2fe85d2012-12-21 15:04:59 +0000922 next_event = q->now + 5LLU * NSEC_PER_SEC;
Jarek Poplawski633fe662008-12-03 21:09:10 -0800923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 for (level = 0; level < TC_HTB_MAXDEPTH; level++) {
925 /* common case optimization - skip event handler quickly */
926 int m;
Eric Dumazetc9364632013-06-15 03:30:10 -0700927 s64 event = q->near_ev_cache[level];
Stephen Hemminger87990462006-08-10 23:35:16 -0700928
Eric Dumazetc9364632013-06-15 03:30:10 -0700929 if (q->now >= event) {
Jarek Poplawskia73be042009-01-12 21:54:40 -0800930 event = htb_do_events(q, level, start_at);
Patrick McHardy2e4b3b02007-05-23 23:39:54 -0700931 if (!event)
Vimalkumar56b765b2012-10-31 06:04:11 +0000932 event = q->now + NSEC_PER_SEC;
Patrick McHardy2e4b3b02007-05-23 23:39:54 -0700933 q->near_ev_cache[level] = event;
Eric Dumazetc9364632013-06-15 03:30:10 -0700934 }
Patrick McHardyfb983d42007-03-16 01:22:39 -0700935
Jarek Poplawskic0851342009-01-12 21:54:16 -0800936 if (next_event > event)
Patrick McHardyfb983d42007-03-16 01:22:39 -0700937 next_event = event;
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 m = ~q->row_mask[level];
940 while (m != (int)(-1)) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700941 int prio = ffz(m);
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 m |= 1 << prio;
Stephen Hemminger87990462006-08-10 23:35:16 -0700944 skb = htb_dequeue_tree(q, prio, level);
Eric Dumazet9190b3b2011-01-20 23:31:33 -0800945 if (likely(skb != NULL))
946 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948 }
John Fastabend25331d62014-09-28 11:53:29 -0700949 qdisc_qstats_overlimit(sch);
Eric Dumazeta9efad82016-05-23 14:24:56 -0700950 if (likely(next_event > q->now))
Eric Dumazet45f50be2016-06-10 16:41:39 -0700951 qdisc_watchdog_schedule_ns(&q->watchdog, next_event);
Eric Dumazeta9efad82016-05-23 14:24:56 -0700952 else
Jarek Poplawski12247362009-02-01 01:13:22 -0800953 schedule_work(&q->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954fin:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return skb;
956}
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958/* reset all classes */
959/* always caled under BH & queue lock */
Stephen Hemminger87990462006-08-10 23:35:16 -0700960static void htb_reset(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962 struct htb_sched *q = qdisc_priv(sch);
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700963 struct htb_class *cl;
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700964 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700966 for (i = 0; i < q->clhash.hashsize; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800967 hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (cl->level)
Stephen Hemminger87990462006-08-10 23:35:16 -0700969 memset(&cl->un.inner, 0, sizeof(cl->un.inner));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 else {
Stephen Hemminger87990462006-08-10 23:35:16 -0700971 if (cl->un.leaf.q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 qdisc_reset(cl->un.leaf.q);
973 INIT_LIST_HEAD(&cl->un.leaf.drop_list);
974 }
975 cl->prio_activity = 0;
976 cl->cmode = HTB_CAN_SEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
978 }
Patrick McHardyfb983d42007-03-16 01:22:39 -0700979 qdisc_watchdog_cancel(&q->watchdog);
Eric Dumazeta5a9f532016-06-13 20:21:56 -0700980 __qdisc_reset_queue(&q->direct_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 sch->q.qlen = 0;
WANG Cong431e3a82016-02-25 14:55:02 -0800982 sch->qstats.backlog = 0;
Eric Dumazetc9364632013-06-15 03:30:10 -0700983 memset(q->hlevel, 0, sizeof(q->hlevel));
Stephen Hemminger87990462006-08-10 23:35:16 -0700984 memset(q->row_mask, 0, sizeof(q->row_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 for (i = 0; i < TC_HTB_NUMPRIO; i++)
Stephen Hemminger87990462006-08-10 23:35:16 -0700986 INIT_LIST_HEAD(q->drops + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
Patrick McHardy27a34212008-01-23 20:35:39 -0800989static const struct nla_policy htb_policy[TCA_HTB_MAX + 1] = {
990 [TCA_HTB_PARMS] = { .len = sizeof(struct tc_htb_opt) },
991 [TCA_HTB_INIT] = { .len = sizeof(struct tc_htb_glob) },
992 [TCA_HTB_CTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
993 [TCA_HTB_RTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
Eric Dumazet6906f4e2013-03-06 06:49:21 +0000994 [TCA_HTB_DIRECT_QLEN] = { .type = NLA_U32 },
Eric Dumazetdf62cdf2013-09-19 09:10:20 -0700995 [TCA_HTB_RATE64] = { .type = NLA_U64 },
996 [TCA_HTB_CEIL64] = { .type = NLA_U64 },
Patrick McHardy27a34212008-01-23 20:35:39 -0800997};
998
Jarek Poplawski12247362009-02-01 01:13:22 -0800999static void htb_work_func(struct work_struct *work)
1000{
1001 struct htb_sched *q = container_of(work, struct htb_sched, work);
1002 struct Qdisc *sch = q->watchdog.qdisc;
1003
Florian Westphal0ee13622016-06-14 06:16:27 +02001004 rcu_read_lock();
Jarek Poplawski12247362009-02-01 01:13:22 -08001005 __netif_schedule(qdisc_root(sch));
Florian Westphal0ee13622016-06-14 06:16:27 +02001006 rcu_read_unlock();
Jarek Poplawski12247362009-02-01 01:13:22 -08001007}
1008
Patrick McHardy1e904742008-01-22 22:11:17 -08001009static int htb_init(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 struct htb_sched *q = qdisc_priv(sch);
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001012 struct nlattr *tb[TCA_HTB_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 struct tc_htb_glob *gopt;
Patrick McHardycee63722008-01-23 20:33:32 -08001014 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 int i;
Patrick McHardycee63722008-01-23 20:33:32 -08001016
1017 if (!opt)
1018 return -EINVAL;
1019
Johannes Bergfceb6432017-04-12 14:34:07 +02001020 err = nla_parse_nested(tb, TCA_HTB_MAX, opt, htb_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -08001021 if (err < 0)
1022 return err;
1023
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001024 if (!tb[TCA_HTB_INIT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return -EINVAL;
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001026
Patrick McHardy1e904742008-01-22 22:11:17 -08001027 gopt = nla_data(tb[TCA_HTB_INIT]);
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001028 if (gopt->version != HTB_VER >> 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001031 err = qdisc_class_hash_init(&q->clhash);
1032 if (err < 0)
1033 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 for (i = 0; i < TC_HTB_NUMPRIO; i++)
Stephen Hemminger87990462006-08-10 23:35:16 -07001035 INIT_LIST_HEAD(q->drops + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Patrick McHardyfb983d42007-03-16 01:22:39 -07001037 qdisc_watchdog_init(&q->watchdog, sch);
Jarek Poplawski12247362009-02-01 01:13:22 -08001038 INIT_WORK(&q->work, htb_work_func);
Florian Westphal48da34b2016-09-18 00:57:34 +02001039 qdisc_skb_head_init(&q->direct_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001041 if (tb[TCA_HTB_DIRECT_QLEN])
1042 q->direct_qlen = nla_get_u32(tb[TCA_HTB_DIRECT_QLEN]);
Phil Sutter348e3432015-08-18 10:30:49 +02001043 else
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001044 q->direct_qlen = qdisc_dev(sch)->tx_queue_len;
Phil Sutter348e3432015-08-18 10:30:49 +02001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if ((q->rate2quantum = gopt->rate2quantum) < 1)
1047 q->rate2quantum = 1;
1048 q->defcls = gopt->defcls;
1049
1050 return 0;
1051}
1052
1053static int htb_dump(struct Qdisc *sch, struct sk_buff *skb)
1054{
1055 struct htb_sched *q = qdisc_priv(sch);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001056 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 struct tc_htb_glob gopt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Eric Dumazet6f542ef2014-03-05 10:14:34 -08001059 /* Its safe to not acquire qdisc lock. As we hold RTNL,
1060 * no change can happen on the qdisc parameters.
1061 */
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001062
1063 gopt.direct_pkts = q->direct_pkts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 gopt.version = HTB_VER;
1065 gopt.rate2quantum = q->rate2quantum;
1066 gopt.defcls = q->defcls;
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001067 gopt.debug = 0;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001068
1069 nest = nla_nest_start(skb, TCA_OPTIONS);
1070 if (nest == NULL)
1071 goto nla_put_failure;
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001072 if (nla_put(skb, TCA_HTB_INIT, sizeof(gopt), &gopt) ||
1073 nla_put_u32(skb, TCA_HTB_DIRECT_QLEN, q->direct_qlen))
David S. Miller1b34ec42012-03-29 05:11:39 -04001074 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001075
Eric Dumazet6f542ef2014-03-05 10:14:34 -08001076 return nla_nest_end(skb, nest);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001077
Patrick McHardy1e904742008-01-22 22:11:17 -08001078nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001079 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return -1;
1081}
1082
1083static int htb_dump_class(struct Qdisc *sch, unsigned long arg,
Stephen Hemminger87990462006-08-10 23:35:16 -07001084 struct sk_buff *skb, struct tcmsg *tcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Stephen Hemminger87990462006-08-10 23:35:16 -07001086 struct htb_class *cl = (struct htb_class *)arg;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001087 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 struct tc_htb_opt opt;
1089
Eric Dumazet6f542ef2014-03-05 10:14:34 -08001090 /* Its safe to not acquire qdisc lock. As we hold RTNL,
1091 * no change can happen on the class parameters.
1092 */
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001093 tcm->tcm_parent = cl->parent ? cl->parent->common.classid : TC_H_ROOT;
1094 tcm->tcm_handle = cl->common.classid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (!cl->level && cl->un.leaf.q)
1096 tcm->tcm_info = cl->un.leaf.q->handle;
1097
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001098 nest = nla_nest_start(skb, TCA_OPTIONS);
1099 if (nest == NULL)
1100 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Stephen Hemminger87990462006-08-10 23:35:16 -07001102 memset(&opt, 0, sizeof(opt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Eric Dumazet01cb71d2013-06-02 13:55:05 +00001104 psched_ratecfg_getrate(&opt.rate, &cl->rate);
Jiri Pirko9c10f412013-02-12 00:12:00 +00001105 opt.buffer = PSCHED_NS2TICKS(cl->buffer);
Eric Dumazet01cb71d2013-06-02 13:55:05 +00001106 psched_ratecfg_getrate(&opt.ceil, &cl->ceil);
Jiri Pirko9c10f412013-02-12 00:12:00 +00001107 opt.cbuffer = PSCHED_NS2TICKS(cl->cbuffer);
Jarek Poplawskic19f7a32008-12-03 21:09:45 -08001108 opt.quantum = cl->quantum;
1109 opt.prio = cl->prio;
Stephen Hemminger87990462006-08-10 23:35:16 -07001110 opt.level = cl->level;
David S. Miller1b34ec42012-03-29 05:11:39 -04001111 if (nla_put(skb, TCA_HTB_PARMS, sizeof(opt), &opt))
1112 goto nla_put_failure;
Eric Dumazetdf62cdf2013-09-19 09:10:20 -07001113 if ((cl->rate.rate_bytes_ps >= (1ULL << 32)) &&
Nicolas Dichtel2a51c1e2016-04-25 10:25:15 +02001114 nla_put_u64_64bit(skb, TCA_HTB_RATE64, cl->rate.rate_bytes_ps,
1115 TCA_HTB_PAD))
Eric Dumazetdf62cdf2013-09-19 09:10:20 -07001116 goto nla_put_failure;
1117 if ((cl->ceil.rate_bytes_ps >= (1ULL << 32)) &&
Nicolas Dichtel2a51c1e2016-04-25 10:25:15 +02001118 nla_put_u64_64bit(skb, TCA_HTB_CEIL64, cl->ceil.rate_bytes_ps,
1119 TCA_HTB_PAD))
Eric Dumazetdf62cdf2013-09-19 09:10:20 -07001120 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001121
Eric Dumazet6f542ef2014-03-05 10:14:34 -08001122 return nla_nest_end(skb, nest);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001123
Patrick McHardy1e904742008-01-22 22:11:17 -08001124nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001125 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return -1;
1127}
1128
1129static int
Stephen Hemminger87990462006-08-10 23:35:16 -07001130htb_dump_class_stats(struct Qdisc *sch, unsigned long arg, struct gnet_dump *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Stephen Hemminger87990462006-08-10 23:35:16 -07001132 struct htb_class *cl = (struct htb_class *)arg;
Eric Dumazet338ed9b2016-06-21 23:16:51 -07001133 struct gnet_stats_queue qs = {
1134 .drops = cl->drops,
1135 };
John Fastabend64015852014-09-28 11:53:57 -07001136 __u32 qlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Eric Dumazet338ed9b2016-06-21 23:16:51 -07001138 if (!cl->level && cl->un.leaf.q) {
John Fastabend64015852014-09-28 11:53:57 -07001139 qlen = cl->un.leaf.q->q.qlen;
Eric Dumazet338ed9b2016-06-21 23:16:51 -07001140 qs.backlog = cl->un.leaf.q->qstats.backlog;
1141 }
Konstantin Khlebnikov0564bf02016-07-16 17:08:56 +03001142 cl->xstats.tokens = clamp_t(s64, PSCHED_NS2TICKS(cl->tokens),
1143 INT_MIN, INT_MAX);
1144 cl->xstats.ctokens = clamp_t(s64, PSCHED_NS2TICKS(cl->ctokens),
1145 INT_MIN, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Eric Dumazetedb09eb2016-06-06 09:37:16 -07001147 if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
1148 d, NULL, &cl->bstats) < 0 ||
Eric Dumazet1c0d32f2016-12-04 09:48:16 -08001149 gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
Eric Dumazet338ed9b2016-06-21 23:16:51 -07001150 gnet_stats_copy_queue(d, NULL, &qs, qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return -1;
1152
1153 return gnet_stats_copy_app(d, &cl->xstats, sizeof(cl->xstats));
1154}
1155
1156static int htb_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
Stephen Hemminger87990462006-08-10 23:35:16 -07001157 struct Qdisc **old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Stephen Hemminger87990462006-08-10 23:35:16 -07001159 struct htb_class *cl = (struct htb_class *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Patrick McHardy5b9a9cc2009-09-04 06:41:17 +00001161 if (cl->level)
1162 return -EINVAL;
1163 if (new == NULL &&
Changli Gao3511c912010-10-16 13:04:08 +00001164 (new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
Patrick McHardy5b9a9cc2009-09-04 06:41:17 +00001165 cl->common.classid)) == NULL)
1166 return -ENOBUFS;
1167
WANG Cong86a79962016-02-25 14:55:00 -08001168 *old = qdisc_replace(sch, new, &cl->un.leaf.q);
Patrick McHardy5b9a9cc2009-09-04 06:41:17 +00001169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
Stephen Hemminger87990462006-08-10 23:35:16 -07001172static struct Qdisc *htb_leaf(struct Qdisc *sch, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Stephen Hemminger87990462006-08-10 23:35:16 -07001174 struct htb_class *cl = (struct htb_class *)arg;
Patrick McHardy5b9a9cc2009-09-04 06:41:17 +00001175 return !cl->level ? cl->un.leaf.q : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
Patrick McHardy256d61b2006-11-29 17:37:05 -08001178static void htb_qlen_notify(struct Qdisc *sch, unsigned long arg)
1179{
1180 struct htb_class *cl = (struct htb_class *)arg;
1181
1182 if (cl->un.leaf.q->q.qlen == 0)
1183 htb_deactivate(qdisc_priv(sch), cl);
1184}
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186static unsigned long htb_get(struct Qdisc *sch, u32 classid)
1187{
Stephen Hemminger87990462006-08-10 23:35:16 -07001188 struct htb_class *cl = htb_find(classid, sch);
1189 if (cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 cl->refcnt++;
1191 return (unsigned long)cl;
1192}
1193
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001194static inline int htb_parent_last_child(struct htb_class *cl)
1195{
1196 if (!cl->parent)
1197 /* the root class */
1198 return 0;
Patrick McHardy42077592008-07-05 23:22:53 -07001199 if (cl->parent->children > 1)
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001200 /* not the last child */
1201 return 0;
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001202 return 1;
1203}
1204
Jarek Poplawski3ba08b02008-05-03 20:46:29 -07001205static void htb_parent_to_leaf(struct htb_sched *q, struct htb_class *cl,
1206 struct Qdisc *new_q)
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001207{
1208 struct htb_class *parent = cl->parent;
1209
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001210 WARN_ON(cl->level || !cl->un.leaf.q || cl->prio_activity);
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001211
Jarek Poplawski3ba08b02008-05-03 20:46:29 -07001212 if (parent->cmode != HTB_CAN_SEND)
Eric Dumazetc9364632013-06-15 03:30:10 -07001213 htb_safe_rb_erase(&parent->pq_node,
1214 &q->hlevel[parent->level].wait_pq);
Jarek Poplawski3ba08b02008-05-03 20:46:29 -07001215
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001216 parent->level = 0;
1217 memset(&parent->un.inner, 0, sizeof(parent->un.inner));
1218 INIT_LIST_HEAD(&parent->un.leaf.drop_list);
1219 parent->un.leaf.q = new_q ? new_q : &noop_qdisc;
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001220 parent->tokens = parent->buffer;
1221 parent->ctokens = parent->cbuffer;
Eric Dumazetd2de8752014-08-22 18:32:09 -07001222 parent->t_c = ktime_get_ns();
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001223 parent->cmode = HTB_CAN_SEND;
1224}
1225
Stephen Hemminger87990462006-08-10 23:35:16 -07001226static void htb_destroy_class(struct Qdisc *sch, struct htb_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (!cl->level) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001229 WARN_ON(!cl->un.leaf.q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 qdisc_destroy(cl->un.leaf.q);
1231 }
Eric Dumazet1c0d32f2016-12-04 09:48:16 -08001232 gen_kill_estimator(&cl->rate_est);
Patrick McHardyff31ab52008-07-01 19:52:38 -07001233 tcf_destroy_chain(&cl->filter_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 kfree(cl);
1235}
1236
Stephen Hemminger87990462006-08-10 23:35:16 -07001237static void htb_destroy(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
1239 struct htb_sched *q = qdisc_priv(sch);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001240 struct hlist_node *next;
Patrick McHardyfbd8f132008-07-05 23:22:19 -07001241 struct htb_class *cl;
1242 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Jarek Poplawski12247362009-02-01 01:13:22 -08001244 cancel_work_sync(&q->work);
Patrick McHardyfb983d42007-03-16 01:22:39 -07001245 qdisc_watchdog_cancel(&q->watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 /* This line used to be after htb_destroy_class call below
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001247 * and surprisingly it worked in 2.4. But it must precede it
1248 * because filter need its target class alive to be able to call
1249 * unbind_filter on it (without Oops).
1250 */
Patrick McHardyff31ab52008-07-01 19:52:38 -07001251 tcf_destroy_chain(&q->filter_list);
Stephen Hemminger87990462006-08-10 23:35:16 -07001252
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001253 for (i = 0; i < q->clhash.hashsize; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001254 hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode)
Patrick McHardyfbd8f132008-07-05 23:22:19 -07001255 tcf_destroy_chain(&cl->filter_list);
1256 }
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001257 for (i = 0; i < q->clhash.hashsize; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001258 hlist_for_each_entry_safe(cl, next, &q->clhash.hash[i],
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001259 common.hnode)
Patrick McHardyfbd8f132008-07-05 23:22:19 -07001260 htb_destroy_class(sch, cl);
1261 }
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001262 qdisc_class_hash_destroy(&q->clhash);
Eric Dumazeta5a9f532016-06-13 20:21:56 -07001263 __qdisc_reset_queue(&q->direct_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
1266static int htb_delete(struct Qdisc *sch, unsigned long arg)
1267{
1268 struct htb_sched *q = qdisc_priv(sch);
Stephen Hemminger87990462006-08-10 23:35:16 -07001269 struct htb_class *cl = (struct htb_class *)arg;
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001270 struct Qdisc *new_q = NULL;
1271 int last_child = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Yang Yinglianga071d272013-12-23 17:38:59 +08001273 /* TODO: why don't allow to delete subtree ? references ? does
1274 * tc subsys guarantee us that in htb_destroy it holds no class
1275 * refs so that we can remove children safely there ?
1276 */
Patrick McHardy42077592008-07-05 23:22:53 -07001277 if (cl->children || cl->filter_cnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return -EBUSY;
Stephen Hemminger87990462006-08-10 23:35:16 -07001279
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001280 if (!cl->level && htb_parent_last_child(cl)) {
Changli Gao3511c912010-10-16 13:04:08 +00001281 new_q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
David S. Millerbb949fb2008-07-08 16:55:56 -07001282 cl->parent->common.classid);
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001283 last_child = 1;
1284 }
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 sch_tree_lock(sch);
Stephen Hemminger87990462006-08-10 23:35:16 -07001287
Patrick McHardy814a175e2006-11-29 17:34:50 -08001288 if (!cl->level) {
WANG Cong2ccccf52016-02-25 14:55:01 -08001289 unsigned int qlen = cl->un.leaf.q->q.qlen;
1290 unsigned int backlog = cl->un.leaf.q->qstats.backlog;
1291
Patrick McHardy814a175e2006-11-29 17:34:50 -08001292 qdisc_reset(cl->un.leaf.q);
WANG Cong2ccccf52016-02-25 14:55:01 -08001293 qdisc_tree_reduce_backlog(cl->un.leaf.q, qlen, backlog);
Patrick McHardy814a175e2006-11-29 17:34:50 -08001294 }
1295
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001296 /* delete from hash and active; remainder in destroy_class */
1297 qdisc_class_hash_remove(&q->clhash, &cl->common);
Jarek Poplawski26b284d2008-08-13 15:16:43 -07001298 if (cl->parent)
1299 cl->parent->children--;
Patrick McHardyc38c83c2007-03-27 14:04:24 -07001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (cl->prio_activity)
Stephen Hemminger87990462006-08-10 23:35:16 -07001302 htb_deactivate(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Patrick McHardyfbd8f132008-07-05 23:22:19 -07001304 if (cl->cmode != HTB_CAN_SEND)
Eric Dumazetc9364632013-06-15 03:30:10 -07001305 htb_safe_rb_erase(&cl->pq_node,
1306 &q->hlevel[cl->level].wait_pq);
Patrick McHardyfbd8f132008-07-05 23:22:19 -07001307
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001308 if (last_child)
Jarek Poplawski3ba08b02008-05-03 20:46:29 -07001309 htb_parent_to_leaf(q, cl, new_q);
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001310
Jarek Poplawski7cd0a632009-03-15 20:00:19 -07001311 BUG_ON(--cl->refcnt == 0);
1312 /*
1313 * This shouldn't happen: we "hold" one cops->get() when called
1314 * from tc_ctl_tclass; the destroy method is done from cops->put().
1315 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 sch_tree_unlock(sch);
1318 return 0;
1319}
1320
1321static void htb_put(struct Qdisc *sch, unsigned long arg)
1322{
Stephen Hemminger87990462006-08-10 23:35:16 -07001323 struct htb_class *cl = (struct htb_class *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 if (--cl->refcnt == 0)
Stephen Hemminger87990462006-08-10 23:35:16 -07001326 htb_destroy_class(sch, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Stephen Hemminger87990462006-08-10 23:35:16 -07001329static int htb_change_class(struct Qdisc *sch, u32 classid,
Patrick McHardy1e904742008-01-22 22:11:17 -08001330 u32 parentid, struct nlattr **tca,
Stephen Hemminger87990462006-08-10 23:35:16 -07001331 unsigned long *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
1333 int err = -EINVAL;
1334 struct htb_sched *q = qdisc_priv(sch);
Stephen Hemminger87990462006-08-10 23:35:16 -07001335 struct htb_class *cl = (struct htb_class *)*arg, *parent;
Patrick McHardy1e904742008-01-22 22:11:17 -08001336 struct nlattr *opt = tca[TCA_OPTIONS];
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001337 struct nlattr *tb[TCA_HTB_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 struct tc_htb_opt *hopt;
Eric Dumazetdf62cdf2013-09-19 09:10:20 -07001339 u64 rate64, ceil64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 /* extract all subattrs from opt attr */
Patrick McHardycee63722008-01-23 20:33:32 -08001342 if (!opt)
1343 goto failure;
1344
Johannes Bergfceb6432017-04-12 14:34:07 +02001345 err = nla_parse_nested(tb, TCA_HTB_MAX, opt, htb_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -08001346 if (err < 0)
1347 goto failure;
1348
1349 err = -EINVAL;
Patrick McHardy27a34212008-01-23 20:35:39 -08001350 if (tb[TCA_HTB_PARMS] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Stephen Hemminger87990462006-08-10 23:35:16 -07001353 parent = parentid == TC_H_ROOT ? NULL : htb_find(parentid, sch);
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001354
Patrick McHardy1e904742008-01-22 22:11:17 -08001355 hopt = nla_data(tb[TCA_HTB_PARMS]);
Eric Dumazet196d97f2012-11-05 16:40:49 +00001356 if (!hopt->rate.rate || !hopt->ceil.rate)
Stephen Hemminger87990462006-08-10 23:35:16 -07001357 goto failure;
1358
Jesper Dangaard Brouer8a8e3d82013-08-14 23:47:11 +02001359 /* Keeping backward compatible with rate_table based iproute2 tc */
Yang Yingliang6b1dd852013-12-11 15:48:37 +08001360 if (hopt->rate.linklayer == TC_LINKLAYER_UNAWARE)
1361 qdisc_put_rtab(qdisc_get_rtab(&hopt->rate, tb[TCA_HTB_RTAB]));
1362
1363 if (hopt->ceil.linklayer == TC_LINKLAYER_UNAWARE)
1364 qdisc_put_rtab(qdisc_get_rtab(&hopt->ceil, tb[TCA_HTB_CTAB]));
Jesper Dangaard Brouer8a8e3d82013-08-14 23:47:11 +02001365
Stephen Hemminger87990462006-08-10 23:35:16 -07001366 if (!cl) { /* new class */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 struct Qdisc *new_q;
Stephen Hemminger3696f622006-08-10 23:36:01 -07001368 int prio;
Patrick McHardyee39e102007-07-02 22:48:13 -07001369 struct {
Patrick McHardy1e904742008-01-22 22:11:17 -08001370 struct nlattr nla;
Patrick McHardyee39e102007-07-02 22:48:13 -07001371 struct gnet_estimator opt;
1372 } est = {
Patrick McHardy1e904742008-01-22 22:11:17 -08001373 .nla = {
1374 .nla_len = nla_attr_size(sizeof(est.opt)),
1375 .nla_type = TCA_RATE,
Patrick McHardyee39e102007-07-02 22:48:13 -07001376 },
1377 .opt = {
1378 /* 4s interval, 16s averaging constant */
1379 .interval = 2,
1380 .ewma_log = 2,
1381 },
1382 };
Stephen Hemminger3696f622006-08-10 23:36:01 -07001383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 /* check for valid classid */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001385 if (!classid || TC_H_MAJ(classid ^ sch->handle) ||
1386 htb_find(classid, sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 goto failure;
1388
1389 /* check maximal depth */
1390 if (parent && parent->parent && parent->parent->level < 2) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001391 pr_err("htb: tree is too deep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 goto failure;
1393 }
1394 err = -ENOBUFS;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001395 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
1396 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 goto failure;
Stephen Hemminger87990462006-08-10 23:35:16 -07001398
Eric Dumazet64153ce2013-06-06 14:53:16 -07001399 if (htb_rate_est || tca[TCA_RATE]) {
John Fastabend22e0f8b2014-09-28 11:52:56 -07001400 err = gen_new_estimator(&cl->bstats, NULL,
1401 &cl->rate_est,
Eric Dumazetedb09eb2016-06-06 09:37:16 -07001402 NULL,
1403 qdisc_root_sleeping_running(sch),
Eric Dumazet64153ce2013-06-06 14:53:16 -07001404 tca[TCA_RATE] ? : &est.nla);
1405 if (err) {
1406 kfree(cl);
1407 goto failure;
1408 }
Stephen Hemminger71bcb092008-11-25 21:13:31 -08001409 }
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 cl->refcnt = 1;
Patrick McHardy42077592008-07-05 23:22:53 -07001412 cl->children = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 INIT_LIST_HEAD(&cl->un.leaf.drop_list);
Stephen Hemminger3696f622006-08-10 23:36:01 -07001414 RB_CLEAR_NODE(&cl->pq_node);
1415
1416 for (prio = 0; prio < TC_HTB_NUMPRIO; prio++)
1417 RB_CLEAR_NODE(&cl->node[prio]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 /* create leaf qdisc early because it uses kmalloc(GFP_KERNEL)
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001420 * so that can't be used inside of sch_tree_lock
1421 * -- thanks to Karlis Peisenieks
1422 */
Changli Gao3511c912010-10-16 13:04:08 +00001423 new_q = qdisc_create_dflt(sch->dev_queue,
David S. Millerbb949fb2008-07-08 16:55:56 -07001424 &pfifo_qdisc_ops, classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 sch_tree_lock(sch);
1426 if (parent && !parent->level) {
Patrick McHardy256d61b2006-11-29 17:37:05 -08001427 unsigned int qlen = parent->un.leaf.q->q.qlen;
WANG Cong2ccccf52016-02-25 14:55:01 -08001428 unsigned int backlog = parent->un.leaf.q->qstats.backlog;
Patrick McHardy256d61b2006-11-29 17:37:05 -08001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 /* turn parent into inner node */
Patrick McHardy256d61b2006-11-29 17:37:05 -08001431 qdisc_reset(parent->un.leaf.q);
WANG Cong2ccccf52016-02-25 14:55:01 -08001432 qdisc_tree_reduce_backlog(parent->un.leaf.q, qlen, backlog);
Stephen Hemminger87990462006-08-10 23:35:16 -07001433 qdisc_destroy(parent->un.leaf.q);
1434 if (parent->prio_activity)
1435 htb_deactivate(q, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 /* remove from evt list because of level change */
1438 if (parent->cmode != HTB_CAN_SEND) {
Eric Dumazetc9364632013-06-15 03:30:10 -07001439 htb_safe_rb_erase(&parent->pq_node, &q->hlevel[0].wait_pq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 parent->cmode = HTB_CAN_SEND;
1441 }
1442 parent->level = (parent->parent ? parent->parent->level
Stephen Hemminger87990462006-08-10 23:35:16 -07001443 : TC_HTB_MAXDEPTH) - 1;
1444 memset(&parent->un.inner, 0, sizeof(parent->un.inner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
1446 /* leaf (we) needs elementary qdisc */
1447 cl->un.leaf.q = new_q ? new_q : &noop_qdisc;
1448
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001449 cl->common.classid = classid;
Stephen Hemminger87990462006-08-10 23:35:16 -07001450 cl->parent = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 /* set class to be in HTB_CAN_SEND state */
Jiri Pirkob9a7afd2013-02-12 00:12:02 +00001453 cl->tokens = PSCHED_TICKS2NS(hopt->buffer);
1454 cl->ctokens = PSCHED_TICKS2NS(hopt->cbuffer);
Eric Dumazet5343a7f2013-06-04 07:11:48 +00001455 cl->mbuffer = 60ULL * NSEC_PER_SEC; /* 1min */
Eric Dumazetd2de8752014-08-22 18:32:09 -07001456 cl->t_c = ktime_get_ns();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 cl->cmode = HTB_CAN_SEND;
1458
1459 /* attach to the hash list and parent's family */
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001460 qdisc_class_hash_insert(&q->clhash, &cl->common);
Patrick McHardy42077592008-07-05 23:22:53 -07001461 if (parent)
1462 parent->children++;
Jiri Kosina49b49972017-03-08 16:03:32 +01001463 if (cl->un.leaf.q != &noop_qdisc)
1464 qdisc_hash_add(cl->un.leaf.q, true);
Patrick McHardyee39e102007-07-02 22:48:13 -07001465 } else {
Stephen Hemminger71bcb092008-11-25 21:13:31 -08001466 if (tca[TCA_RATE]) {
John Fastabend22e0f8b2014-09-28 11:52:56 -07001467 err = gen_replace_estimator(&cl->bstats, NULL,
1468 &cl->rate_est,
Eric Dumazetedb09eb2016-06-06 09:37:16 -07001469 NULL,
1470 qdisc_root_sleeping_running(sch),
Stephen Hemminger71bcb092008-11-25 21:13:31 -08001471 tca[TCA_RATE]);
1472 if (err)
1473 return err;
1474 }
Stephen Hemminger87990462006-08-10 23:35:16 -07001475 sch_tree_lock(sch);
Patrick McHardyee39e102007-07-02 22:48:13 -07001476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Yang Yingliang1598f7c2013-12-10 14:59:28 +08001478 rate64 = tb[TCA_HTB_RATE64] ? nla_get_u64(tb[TCA_HTB_RATE64]) : 0;
1479
1480 ceil64 = tb[TCA_HTB_CEIL64] ? nla_get_u64(tb[TCA_HTB_CEIL64]) : 0;
1481
1482 psched_ratecfg_precompute(&cl->rate, &hopt->rate, rate64);
1483 psched_ratecfg_precompute(&cl->ceil, &hopt->ceil, ceil64);
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 /* it used to be a nasty bug here, we have to check that node
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001486 * is really leaf before changing cl->un.leaf !
1487 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (!cl->level) {
Yang Yingliang1598f7c2013-12-10 14:59:28 +08001489 u64 quantum = cl->rate.rate_bytes_ps;
1490
1491 do_div(quantum, q->rate2quantum);
1492 cl->quantum = min_t(u64, quantum, INT_MAX);
1493
Jarek Poplawskic19f7a32008-12-03 21:09:45 -08001494 if (!hopt->quantum && cl->quantum < 1000) {
Yang Yingliangc17988a2013-12-23 17:38:58 +08001495 pr_warn("HTB: quantum of class %X is small. Consider r2q change.\n",
1496 cl->common.classid);
Jarek Poplawskic19f7a32008-12-03 21:09:45 -08001497 cl->quantum = 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
Jarek Poplawskic19f7a32008-12-03 21:09:45 -08001499 if (!hopt->quantum && cl->quantum > 200000) {
Yang Yingliangc17988a2013-12-23 17:38:58 +08001500 pr_warn("HTB: quantum of class %X is big. Consider r2q change.\n",
1501 cl->common.classid);
Jarek Poplawskic19f7a32008-12-03 21:09:45 -08001502 cl->quantum = 200000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
1504 if (hopt->quantum)
Jarek Poplawskic19f7a32008-12-03 21:09:45 -08001505 cl->quantum = hopt->quantum;
1506 if ((cl->prio = hopt->prio) >= TC_HTB_NUMPRIO)
1507 cl->prio = TC_HTB_NUMPRIO - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
1509
Jiri Pirko324f5aa2013-02-12 00:11:59 +00001510 cl->buffer = PSCHED_TICKS2NS(hopt->buffer);
Vimalkumarf3ad8572013-09-10 17:36:37 -07001511 cl->cbuffer = PSCHED_TICKS2NS(hopt->cbuffer);
Vimalkumar56b765b2012-10-31 06:04:11 +00001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 sch_tree_unlock(sch);
1514
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001515 qdisc_class_hash_grow(sch, &q->clhash);
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 *arg = (unsigned long)cl;
1518 return 0;
1519
1520failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return err;
1522}
1523
John Fastabend25d8c0d2014-09-12 20:05:27 -07001524static struct tcf_proto __rcu **htb_find_tcf(struct Qdisc *sch,
1525 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
1527 struct htb_sched *q = qdisc_priv(sch);
1528 struct htb_class *cl = (struct htb_class *)arg;
John Fastabend25d8c0d2014-09-12 20:05:27 -07001529 struct tcf_proto __rcu **fl = cl ? &cl->filter_list : &q->filter_list;
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return fl;
1532}
1533
1534static unsigned long htb_bind_filter(struct Qdisc *sch, unsigned long parent,
Stephen Hemminger87990462006-08-10 23:35:16 -07001535 u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Stephen Hemminger87990462006-08-10 23:35:16 -07001537 struct htb_class *cl = htb_find(classid, sch);
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 /*if (cl && !cl->level) return 0;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001540 * The line above used to be there to prevent attaching filters to
1541 * leaves. But at least tc_index filter uses this just to get class
1542 * for other reasons so that we have to allow for it.
1543 * ----
1544 * 19.6.2002 As Werner explained it is ok - bind filter is just
1545 * another way to "lock" the class - unlike "get" this lock can
1546 * be broken by class during destroy IIUC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 */
Stephen Hemminger87990462006-08-10 23:35:16 -07001548 if (cl)
1549 cl->filter_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return (unsigned long)cl;
1551}
1552
1553static void htb_unbind_filter(struct Qdisc *sch, unsigned long arg)
1554{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 struct htb_class *cl = (struct htb_class *)arg;
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001556
Stephen Hemminger87990462006-08-10 23:35:16 -07001557 if (cl)
1558 cl->filter_cnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
1561static void htb_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1562{
1563 struct htb_sched *q = qdisc_priv(sch);
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001564 struct htb_class *cl;
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001565 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 if (arg->stop)
1568 return;
1569
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001570 for (i = 0; i < q->clhash.hashsize; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001571 hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (arg->count < arg->skip) {
1573 arg->count++;
1574 continue;
1575 }
1576 if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
1577 arg->stop = 1;
1578 return;
1579 }
1580 arg->count++;
1581 }
1582 }
1583}
1584
Eric Dumazet20fea082007-11-14 01:44:41 -08001585static const struct Qdisc_class_ops htb_class_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 .graft = htb_graft,
1587 .leaf = htb_leaf,
Patrick McHardy256d61b2006-11-29 17:37:05 -08001588 .qlen_notify = htb_qlen_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 .get = htb_get,
1590 .put = htb_put,
1591 .change = htb_change_class,
1592 .delete = htb_delete,
1593 .walk = htb_walk,
1594 .tcf_chain = htb_find_tcf,
1595 .bind_tcf = htb_bind_filter,
1596 .unbind_tcf = htb_unbind_filter,
1597 .dump = htb_dump_class,
1598 .dump_stats = htb_dump_class_stats,
1599};
1600
Eric Dumazet20fea082007-11-14 01:44:41 -08001601static struct Qdisc_ops htb_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 .cl_ops = &htb_class_ops,
1603 .id = "htb",
1604 .priv_size = sizeof(struct htb_sched),
1605 .enqueue = htb_enqueue,
1606 .dequeue = htb_dequeue,
Jarek Poplawski77be1552008-10-31 00:47:01 -07001607 .peek = qdisc_peek_dequeued,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 .init = htb_init,
1609 .reset = htb_reset,
1610 .destroy = htb_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 .dump = htb_dump,
1612 .owner = THIS_MODULE,
1613};
1614
1615static int __init htb_module_init(void)
1616{
Stephen Hemminger87990462006-08-10 23:35:16 -07001617 return register_qdisc(&htb_qdisc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
Stephen Hemminger87990462006-08-10 23:35:16 -07001619static void __exit htb_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Stephen Hemminger87990462006-08-10 23:35:16 -07001621 unregister_qdisc(&htb_qdisc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622}
Stephen Hemminger87990462006-08-10 23:35:16 -07001623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624module_init(htb_module_init)
1625module_exit(htb_module_exit)
1626MODULE_LICENSE("GPL");