blob: 195bbca9eb0bf859073c574be694e911e07d8f99 [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 */
Jiri Pirko6529eab2017-05-17 11:07:55 +0200108 struct tcf_block *block;
Eric Dumazetca4ec902013-06-13 07:58:30 -0700109 int filter_cnt;
110 int refcnt; /* usage count of this class */
111
112 int level; /* our level (see above) */
113 unsigned int children;
114 struct htb_class *parent; /* parent class */
115
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800116 struct net_rate_estimator __rcu *rate_est;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Eric Dumazetca4ec902013-06-13 07:58:30 -0700118 /*
119 * Written often fields
120 */
121 struct gnet_stats_basic_packed bstats;
Eric Dumazetca4ec902013-06-13 07:58:30 -0700122 struct tc_htb_xstats xstats; /* our special stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Eric Dumazetca4ec902013-06-13 07:58:30 -0700124 /* token bucket parameters */
125 s64 tokens, ctokens;/* current number of tokens */
126 s64 t_c; /* checkpoint time */
Jarek Poplawskic19f7a32008-12-03 21:09:45 -0800127
Stephen Hemminger87990462006-08-10 23:35:16 -0700128 union {
129 struct htb_class_leaf {
Stephen Hemminger87990462006-08-10 23:35:16 -0700130 struct list_head drop_list;
Eric Dumazetc9364632013-06-15 03:30:10 -0700131 int deficit[TC_HTB_MAXDEPTH];
132 struct Qdisc *q;
Stephen Hemminger87990462006-08-10 23:35:16 -0700133 } leaf;
134 struct htb_class_inner {
Eric Dumazetc9364632013-06-15 03:30:10 -0700135 struct htb_prio clprio[TC_HTB_NUMPRIO];
Stephen Hemminger87990462006-08-10 23:35:16 -0700136 } inner;
137 } un;
Eric Dumazetca4ec902013-06-13 07:58:30 -0700138 s64 pq_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Eric Dumazetca4ec902013-06-13 07:58:30 -0700140 int prio_activity; /* for which prios are we active */
141 enum htb_cmode cmode; /* current mode of the class */
142 struct rb_node pq_node; /* node for event queue */
143 struct rb_node node[TC_HTB_NUMPRIO]; /* node for self or feed tree */
Eric Dumazet338ed9b2016-06-21 23:16:51 -0700144
145 unsigned int drops ____cacheline_aligned_in_smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
Eric Dumazetc9364632013-06-15 03:30:10 -0700148struct htb_level {
149 struct rb_root wait_pq;
150 struct htb_prio hprio[TC_HTB_NUMPRIO];
151};
152
Stephen Hemminger87990462006-08-10 23:35:16 -0700153struct htb_sched {
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700154 struct Qdisc_class_hash clhash;
Eric Dumazetc9364632013-06-15 03:30:10 -0700155 int defcls; /* class where unclassified flows go to */
156 int rate2quantum; /* quant = rate / rate2quantum */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Stephen Hemminger87990462006-08-10 23:35:16 -0700158 /* filters for qdisc itself */
John Fastabend25d8c0d2014-09-12 20:05:27 -0700159 struct tcf_proto __rcu *filter_list;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200160 struct tcf_block *block;
Jarek Poplawskie82181d2009-02-01 01:13:05 -0800161
162#define HTB_WARN_TOOMANYEVENTS 0x1
Eric Dumazetc9364632013-06-15 03:30:10 -0700163 unsigned int warned; /* only one warning */
164 int direct_qlen;
165 struct work_struct work;
166
167 /* non shaped skbs; let them go directly thru */
Florian Westphal48da34b2016-09-18 00:57:34 +0200168 struct qdisc_skb_head direct_queue;
Eric Dumazetc9364632013-06-15 03:30:10 -0700169 long direct_pkts;
170
171 struct qdisc_watchdog watchdog;
172
173 s64 now; /* cached dequeue time */
174 struct list_head drops[TC_HTB_NUMPRIO];/* active leaves (for drops) */
175
176 /* time of nearest event per level (row) */
177 s64 near_ev_cache[TC_HTB_MAXDEPTH];
178
179 int row_mask[TC_HTB_MAXDEPTH];
180
181 struct htb_level hlevel[TC_HTB_MAXDEPTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/* find class in global hash table using given handle */
Stephen Hemminger87990462006-08-10 23:35:16 -0700185static inline struct htb_class *htb_find(u32 handle, struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 struct htb_sched *q = qdisc_priv(sch);
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700188 struct Qdisc_class_common *clc;
Stephen Hemminger0cef2962006-08-10 23:35:38 -0700189
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700190 clc = qdisc_class_find(&q->clhash, handle);
191 if (clc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return NULL;
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700193 return container_of(clc, struct htb_class, common);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196/**
197 * htb_classify - classify a packet into class
198 *
199 * It returns NULL if the packet should be dropped or -1 if the packet
200 * should be passed directly thru. In all other cases leaf class is returned.
201 * We allow direct class selection by classid in priority. The we examine
202 * filters in qdisc and in inner nodes (if higher filter points to the inner
203 * node). If we end up with classid MAJOR:0 we enqueue the skb into special
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900204 * internal fifo (direct). These packets then go directly thru. If we still
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300205 * have no valid leaf we try to use MAJOR:default leaf. It still unsuccessful
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * then finish and return direct queue.
207 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000208#define HTB_DIRECT ((struct htb_class *)-1L)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Stephen Hemminger87990462006-08-10 23:35:16 -0700210static struct htb_class *htb_classify(struct sk_buff *skb, struct Qdisc *sch,
211 int *qerr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 struct htb_sched *q = qdisc_priv(sch);
214 struct htb_class *cl;
215 struct tcf_result res;
216 struct tcf_proto *tcf;
217 int result;
218
219 /* allow to select class by setting skb->priority to valid classid;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000220 * note that nfmark can be used too by attaching filter fw with no
221 * rules in it
222 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (skb->priority == sch->handle)
Stephen Hemminger87990462006-08-10 23:35:16 -0700224 return HTB_DIRECT; /* X:0 (direct flow) selected */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000225 cl = htb_find(skb->priority, sch);
Harry Mason29824312014-01-17 13:22:32 +0000226 if (cl) {
227 if (cl->level == 0)
228 return cl;
229 /* Start with inner filter chain if a non-leaf class is selected */
John Fastabend25d8c0d2014-09-12 20:05:27 -0700230 tcf = rcu_dereference_bh(cl->filter_list);
Harry Mason29824312014-01-17 13:22:32 +0000231 } else {
John Fastabend25d8c0d2014-09-12 20:05:27 -0700232 tcf = rcu_dereference_bh(q->filter_list);
Harry Mason29824312014-01-17 13:22:32 +0000233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Jarek Poplawskic27f3392008-08-04 22:39:11 -0700235 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
Jiri Pirko87d83092017-05-17 11:07:54 +0200236 while (tcf && (result = tcf_classify(skb, tcf, &res, false)) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237#ifdef CONFIG_NET_CLS_ACT
238 switch (result) {
239 case TC_ACT_QUEUED:
Stephen Hemminger87990462006-08-10 23:35:16 -0700240 case TC_ACT_STOLEN:
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700241 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 case TC_ACT_SHOT:
243 return NULL;
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245#endif
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000246 cl = (void *)res.class;
247 if (!cl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (res.classid == sch->handle)
Stephen Hemminger87990462006-08-10 23:35:16 -0700249 return HTB_DIRECT; /* X:0 (direct flow) */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000250 cl = htb_find(res.classid, sch);
251 if (!cl)
Stephen Hemminger87990462006-08-10 23:35:16 -0700252 break; /* filter selected invalid classid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254 if (!cl->level)
Stephen Hemminger87990462006-08-10 23:35:16 -0700255 return cl; /* we hit leaf; return it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 /* we have got inner class; apply inner filter chain */
John Fastabend25d8c0d2014-09-12 20:05:27 -0700258 tcf = rcu_dereference_bh(cl->filter_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260 /* classification failed; try to use default class */
Stephen Hemminger87990462006-08-10 23:35:16 -0700261 cl = htb_find(TC_H_MAKE(TC_H_MAJ(sch->handle), q->defcls), sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (!cl || cl->level)
Stephen Hemminger87990462006-08-10 23:35:16 -0700263 return HTB_DIRECT; /* bad default .. this is safe bet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return cl;
265}
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/**
268 * htb_add_to_id_tree - adds class to the round robin list
269 *
270 * Routine adds class to the list (actually tree) sorted by classid.
271 * Make sure that class is not already on such list for given prio.
272 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700273static void htb_add_to_id_tree(struct rb_root *root,
274 struct htb_class *cl, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 struct rb_node **p = &root->rb_node, *parent = NULL;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 while (*p) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700279 struct htb_class *c;
280 parent = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 c = rb_entry(parent, struct htb_class, node[prio]);
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700282
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700283 if (cl->common.classid > c->common.classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 p = &parent->rb_right;
Stephen Hemminger87990462006-08-10 23:35:16 -0700285 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 p = &parent->rb_left;
287 }
288 rb_link_node(&cl->node[prio], parent, p);
289 rb_insert_color(&cl->node[prio], root);
290}
291
292/**
293 * htb_add_to_wait_tree - adds class to the event queue with delay
294 *
295 * The class is added to priority event queue to indicate that class will
296 * change its mode in cl->pq_key microseconds. Make sure that class is not
297 * already in the queue.
298 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700299static void htb_add_to_wait_tree(struct htb_sched *q,
Vimalkumar56b765b2012-10-31 06:04:11 +0000300 struct htb_class *cl, s64 delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Eric Dumazetc9364632013-06-15 03:30:10 -0700302 struct rb_node **p = &q->hlevel[cl->level].wait_pq.rb_node, *parent = NULL;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700303
Patrick McHardyfb983d42007-03-16 01:22:39 -0700304 cl->pq_key = q->now + delay;
305 if (cl->pq_key == q->now)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 cl->pq_key++;
307
308 /* update the nearest event cache */
Patrick McHardyfb983d42007-03-16 01:22:39 -0700309 if (q->near_ev_cache[cl->level] > cl->pq_key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 q->near_ev_cache[cl->level] = cl->pq_key;
Stephen Hemminger87990462006-08-10 23:35:16 -0700311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 while (*p) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700313 struct htb_class *c;
314 parent = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 c = rb_entry(parent, struct htb_class, pq_node);
Patrick McHardyfb983d42007-03-16 01:22:39 -0700316 if (cl->pq_key >= c->pq_key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 p = &parent->rb_right;
Stephen Hemminger87990462006-08-10 23:35:16 -0700318 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 p = &parent->rb_left;
320 }
321 rb_link_node(&cl->pq_node, parent, p);
Eric Dumazetc9364632013-06-15 03:30:10 -0700322 rb_insert_color(&cl->pq_node, &q->hlevel[cl->level].wait_pq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325/**
326 * htb_next_rb_node - finds next node in binary tree
327 *
328 * When we are past last key we return NULL.
329 * Average complexity is 2 steps per call.
330 */
Stephen Hemminger3696f622006-08-10 23:36:01 -0700331static inline void htb_next_rb_node(struct rb_node **n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 *n = rb_next(*n);
334}
335
336/**
337 * htb_add_class_to_row - add class to its row
338 *
339 * The class is added to row at priorities marked in mask.
340 * It does nothing if mask == 0.
341 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700342static inline void htb_add_class_to_row(struct htb_sched *q,
343 struct htb_class *cl, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 q->row_mask[cl->level] |= mask;
346 while (mask) {
347 int prio = ffz(~mask);
348 mask &= ~(1 << prio);
Eric Dumazetc9364632013-06-15 03:30:10 -0700349 htb_add_to_id_tree(&q->hlevel[cl->level].hprio[prio].row, cl, prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351}
352
Stephen Hemminger3696f622006-08-10 23:36:01 -0700353/* If this triggers, it is a bug in this code, but it need not be fatal */
354static void htb_safe_rb_erase(struct rb_node *rb, struct rb_root *root)
355{
Ismail Donmez81771b32006-10-03 13:49:10 -0700356 if (RB_EMPTY_NODE(rb)) {
Stephen Hemminger3696f622006-08-10 23:36:01 -0700357 WARN_ON(1);
358 } else {
359 rb_erase(rb, root);
360 RB_CLEAR_NODE(rb);
361 }
362}
363
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/**
366 * htb_remove_class_from_row - removes class from its row
367 *
368 * The class is removed from row at priorities marked in mask.
369 * It does nothing if mask == 0.
370 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700371static inline void htb_remove_class_from_row(struct htb_sched *q,
372 struct htb_class *cl, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 int m = 0;
Eric Dumazetc9364632013-06-15 03:30:10 -0700375 struct htb_level *hlevel = &q->hlevel[cl->level];
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 while (mask) {
378 int prio = ffz(~mask);
Eric Dumazetc9364632013-06-15 03:30:10 -0700379 struct htb_prio *hprio = &hlevel->hprio[prio];
Stephen Hemminger3696f622006-08-10 23:36:01 -0700380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 mask &= ~(1 << prio);
Eric Dumazetc9364632013-06-15 03:30:10 -0700382 if (hprio->ptr == cl->node + prio)
383 htb_next_rb_node(&hprio->ptr);
Stephen Hemminger3696f622006-08-10 23:36:01 -0700384
Eric Dumazetc9364632013-06-15 03:30:10 -0700385 htb_safe_rb_erase(cl->node + prio, &hprio->row);
386 if (!hprio->row.rb_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 m |= 1 << prio;
388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 q->row_mask[cl->level] &= ~m;
390}
391
392/**
393 * htb_activate_prios - creates active classe's feed chain
394 *
395 * The class is connected to ancestors and/or appropriate rows
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900396 * for priorities it is participating on. cl->cmode must be new
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 * (activated) mode. It does nothing if cl->prio_activity == 0.
398 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700399static void htb_activate_prios(struct htb_sched *q, struct htb_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 struct htb_class *p = cl->parent;
Stephen Hemminger87990462006-08-10 23:35:16 -0700402 long m, mask = cl->prio_activity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 while (cl->cmode == HTB_MAY_BORROW && p && mask) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700405 m = mask;
406 while (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 int prio = ffz(~m);
408 m &= ~(1 << prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700409
Eric Dumazetc9364632013-06-15 03:30:10 -0700410 if (p->un.inner.clprio[prio].feed.rb_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* parent already has its feed in use so that
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000412 * reset bit in mask as parent is already ok
413 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 mask &= ~(1 << prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700415
Eric Dumazetc9364632013-06-15 03:30:10 -0700416 htb_add_to_id_tree(&p->un.inner.clprio[prio].feed, cl, prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 p->prio_activity |= mask;
Stephen Hemminger87990462006-08-10 23:35:16 -0700419 cl = p;
420 p = cl->parent;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423 if (cl->cmode == HTB_CAN_SEND && mask)
Stephen Hemminger87990462006-08-10 23:35:16 -0700424 htb_add_class_to_row(q, cl, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427/**
428 * htb_deactivate_prios - remove class from feed chain
429 *
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900430 * cl->cmode must represent old mode (before deactivation). It does
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * nothing if cl->prio_activity == 0. Class is removed from all feed
432 * chains and rows.
433 */
434static void htb_deactivate_prios(struct htb_sched *q, struct htb_class *cl)
435{
436 struct htb_class *p = cl->parent;
Stephen Hemminger87990462006-08-10 23:35:16 -0700437 long m, mask = cl->prio_activity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 while (cl->cmode == HTB_MAY_BORROW && p && mask) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700440 m = mask;
441 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 while (m) {
443 int prio = ffz(~m);
444 m &= ~(1 << prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700445
Eric Dumazetc9364632013-06-15 03:30:10 -0700446 if (p->un.inner.clprio[prio].ptr == cl->node + prio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /* we are removing child which is pointed to from
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000448 * parent feed - forget the pointer but remember
449 * classid
450 */
Eric Dumazetc9364632013-06-15 03:30:10 -0700451 p->un.inner.clprio[prio].last_ptr_id = cl->common.classid;
452 p->un.inner.clprio[prio].ptr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
Stephen Hemminger87990462006-08-10 23:35:16 -0700454
Eric Dumazetc9364632013-06-15 03:30:10 -0700455 htb_safe_rb_erase(cl->node + prio,
456 &p->un.inner.clprio[prio].feed);
Stephen Hemminger87990462006-08-10 23:35:16 -0700457
Eric Dumazetc9364632013-06-15 03:30:10 -0700458 if (!p->un.inner.clprio[prio].feed.rb_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 mask |= 1 << prio;
460 }
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 p->prio_activity &= ~mask;
Stephen Hemminger87990462006-08-10 23:35:16 -0700463 cl = p;
464 p = cl->parent;
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
Stephen Hemminger87990462006-08-10 23:35:16 -0700467 if (cl->cmode == HTB_CAN_SEND && mask)
468 htb_remove_class_from_row(q, cl, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Vimalkumar56b765b2012-10-31 06:04:11 +0000471static inline s64 htb_lowater(const struct htb_class *cl)
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700472{
Jesper Dangaard Brouer47083fc2008-06-16 16:39:32 -0700473 if (htb_hysteresis)
474 return cl->cmode != HTB_CANT_SEND ? -cl->cbuffer : 0;
475 else
476 return 0;
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700477}
Vimalkumar56b765b2012-10-31 06:04:11 +0000478static inline s64 htb_hiwater(const struct htb_class *cl)
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700479{
Jesper Dangaard Brouer47083fc2008-06-16 16:39:32 -0700480 if (htb_hysteresis)
481 return cl->cmode == HTB_CAN_SEND ? -cl->buffer : 0;
482 else
483 return 0;
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700484}
Jesper Dangaard Brouer47083fc2008-06-16 16:39:32 -0700485
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487/**
488 * htb_class_mode - computes and returns current class mode
489 *
490 * It computes cl's mode at time cl->t_c+diff and returns it. If mode
491 * is not HTB_CAN_SEND then cl->pq_key is updated to time difference
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900492 * from now to time when cl will change its state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * Also it is worth to note that class mode doesn't change simply
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900494 * at cl->{c,}tokens == 0 but there can rather be hysteresis of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * 0 .. -cl->{c,}buffer range. It is meant to limit number of
496 * mode transitions per time unit. The speed gain is about 1/6.
497 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700498static inline enum htb_cmode
Vimalkumar56b765b2012-10-31 06:04:11 +0000499htb_class_mode(struct htb_class *cl, s64 *diff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Vimalkumar56b765b2012-10-31 06:04:11 +0000501 s64 toks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Stephen Hemminger87990462006-08-10 23:35:16 -0700503 if ((toks = (cl->ctokens + *diff)) < htb_lowater(cl)) {
504 *diff = -toks;
505 return HTB_CANT_SEND;
506 }
Stephen Hemminger18a63e82006-08-10 23:34:02 -0700507
Stephen Hemminger87990462006-08-10 23:35:16 -0700508 if ((toks = (cl->tokens + *diff)) >= htb_hiwater(cl))
509 return HTB_CAN_SEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Stephen Hemminger87990462006-08-10 23:35:16 -0700511 *diff = -toks;
512 return HTB_MAY_BORROW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
515/**
516 * htb_change_class_mode - changes classe's mode
517 *
518 * This should be the only way how to change classe's mode under normal
519 * cirsumstances. Routine will update feed lists linkage, change mode
520 * and add class to the wait event queue if appropriate. New mode should
521 * be different from old one and cl->pq_key has to be valid if changing
522 * to mode other than HTB_CAN_SEND (see htb_add_to_wait_tree).
523 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700524static void
Vimalkumar56b765b2012-10-31 06:04:11 +0000525htb_change_class_mode(struct htb_sched *q, struct htb_class *cl, s64 *diff)
Stephen Hemminger87990462006-08-10 23:35:16 -0700526{
527 enum htb_cmode new_mode = htb_class_mode(cl, diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 if (new_mode == cl->cmode)
Stephen Hemminger87990462006-08-10 23:35:16 -0700530 return;
531
532 if (cl->prio_activity) { /* not necessary: speed optimization */
533 if (cl->cmode != HTB_CANT_SEND)
534 htb_deactivate_prios(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 cl->cmode = new_mode;
Stephen Hemminger87990462006-08-10 23:35:16 -0700536 if (new_mode != HTB_CANT_SEND)
537 htb_activate_prios(q, cl);
538 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 cl->cmode = new_mode;
540}
541
542/**
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900543 * htb_activate - inserts leaf cl into appropriate active feeds
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 *
545 * Routine learns (new) priority of leaf and activates feed chain
546 * for the prio. It can be called on already active leaf safely.
547 * It also adds leaf into droplist.
548 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700549static inline void htb_activate(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->level || !cl->un.leaf.q || !cl->un.leaf.q->q.qlen);
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (!cl->prio_activity) {
Jarek Poplawskic19f7a32008-12-03 21:09:45 -0800554 cl->prio_activity = 1 << cl->prio;
Stephen Hemminger87990462006-08-10 23:35:16 -0700555 htb_activate_prios(q, cl);
556 list_add_tail(&cl->un.leaf.drop_list,
Jarek Poplawskic19f7a32008-12-03 21:09:45 -0800557 q->drops + cl->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559}
560
561/**
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900562 * htb_deactivate - remove leaf cl from active feeds
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 *
564 * Make sure that leaf is active. In the other words it can't be called
565 * with non-active leaf. It also removes class from the drop list.
566 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700567static inline void htb_deactivate(struct htb_sched *q, struct htb_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700569 WARN_ON(!cl->prio_activity);
Stephen Hemminger3bf72952006-08-10 23:31:08 -0700570
Stephen Hemminger87990462006-08-10 23:35:16 -0700571 htb_deactivate_prios(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 cl->prio_activity = 0;
573 list_del_init(&cl->un.leaf.drop_list);
574}
575
Florian Westphal48da34b2016-09-18 00:57:34 +0200576static void htb_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
577 struct qdisc_skb_head *qh)
578{
579 struct sk_buff *last = qh->tail;
580
581 if (last) {
582 skb->next = NULL;
583 last->next = skb;
584 qh->tail = skb;
585 } else {
586 qh->tail = skb;
587 qh->head = skb;
588 }
589 qh->qlen++;
590}
591
Eric Dumazet520ac302016-06-21 23:16:49 -0700592static int htb_enqueue(struct sk_buff *skb, struct Qdisc *sch,
593 struct sk_buff **to_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Jarek Poplawskif30ab412008-11-13 22:56:30 -0800595 int uninitialized_var(ret);
Stephen Hemminger87990462006-08-10 23:35:16 -0700596 struct htb_sched *q = qdisc_priv(sch);
597 struct htb_class *cl = htb_classify(skb, sch, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Stephen Hemminger87990462006-08-10 23:35:16 -0700599 if (cl == HTB_DIRECT) {
600 /* enqueue to helper queue */
601 if (q->direct_queue.qlen < q->direct_qlen) {
Florian Westphal48da34b2016-09-18 00:57:34 +0200602 htb_enqueue_tail(skb, sch, &q->direct_queue);
Stephen Hemminger87990462006-08-10 23:35:16 -0700603 q->direct_pkts++;
604 } else {
Eric Dumazet520ac302016-06-21 23:16:49 -0700605 return qdisc_drop(skb, sch, to_free);
Stephen Hemminger87990462006-08-10 23:35:16 -0700606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607#ifdef CONFIG_NET_CLS_ACT
Stephen Hemminger87990462006-08-10 23:35:16 -0700608 } else if (!cl) {
Jarek Poplawskic27f3392008-08-04 22:39:11 -0700609 if (ret & __NET_XMIT_BYPASS)
John Fastabend25331d62014-09-28 11:53:29 -0700610 qdisc_qstats_drop(sch);
Eric Dumazet520ac302016-06-21 23:16:49 -0700611 __qdisc_drop(skb, to_free);
Stephen Hemminger87990462006-08-10 23:35:16 -0700612 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613#endif
Eric Dumazet520ac302016-06-21 23:16:49 -0700614 } else if ((ret = qdisc_enqueue(skb, cl->un.leaf.q,
615 to_free)) != NET_XMIT_SUCCESS) {
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700616 if (net_xmit_drop_count(ret)) {
John Fastabend25331d62014-09-28 11:53:29 -0700617 qdisc_qstats_drop(sch);
Eric Dumazet338ed9b2016-06-21 23:16:51 -0700618 cl->drops++;
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700619 }
David S. Miller69747652008-08-17 23:55:36 -0700620 return ret;
Stephen Hemminger87990462006-08-10 23:35:16 -0700621 } else {
Stephen Hemminger87990462006-08-10 23:35:16 -0700622 htb_activate(q, cl);
623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
WANG Cong431e3a82016-02-25 14:55:02 -0800625 qdisc_qstats_backlog_inc(sch, skb);
Stephen Hemminger87990462006-08-10 23:35:16 -0700626 sch->q.qlen++;
Stephen Hemminger87990462006-08-10 23:35:16 -0700627 return NET_XMIT_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Vimalkumar56b765b2012-10-31 06:04:11 +0000630static inline void htb_accnt_tokens(struct htb_class *cl, int bytes, s64 diff)
Jarek Poplawski59e42202008-12-03 21:17:27 -0800631{
Vimalkumar56b765b2012-10-31 06:04:11 +0000632 s64 toks = diff + cl->tokens;
Jarek Poplawski59e42202008-12-03 21:17:27 -0800633
634 if (toks > cl->buffer)
635 toks = cl->buffer;
Jiri Pirko292f1c72013-02-12 00:12:03 +0000636 toks -= (s64) psched_l2t_ns(&cl->rate, bytes);
Jarek Poplawski59e42202008-12-03 21:17:27 -0800637 if (toks <= -cl->mbuffer)
638 toks = 1 - cl->mbuffer;
639
640 cl->tokens = toks;
641}
642
Vimalkumar56b765b2012-10-31 06:04:11 +0000643static inline void htb_accnt_ctokens(struct htb_class *cl, int bytes, s64 diff)
Jarek Poplawski59e42202008-12-03 21:17:27 -0800644{
Vimalkumar56b765b2012-10-31 06:04:11 +0000645 s64 toks = diff + cl->ctokens;
Jarek Poplawski59e42202008-12-03 21:17:27 -0800646
647 if (toks > cl->cbuffer)
648 toks = cl->cbuffer;
Jiri Pirko292f1c72013-02-12 00:12:03 +0000649 toks -= (s64) psched_l2t_ns(&cl->ceil, bytes);
Jarek Poplawski59e42202008-12-03 21:17:27 -0800650 if (toks <= -cl->mbuffer)
651 toks = 1 - cl->mbuffer;
652
653 cl->ctokens = toks;
654}
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656/**
657 * htb_charge_class - charges amount "bytes" to leaf and ancestors
658 *
659 * Routine assumes that packet "bytes" long was dequeued from leaf cl
660 * borrowing from "level". It accounts bytes to ceil leaky bucket for
661 * leaf and all ancestors and to rate bucket for ancestors at levels
662 * "level" and higher. It also handles possible change of mode resulting
663 * from the update. Note that mode can also increase here (MAY_BORROW to
664 * CAN_SEND) because we can use more precise clock that event queue here.
665 * In such case we remove class from event queue first.
666 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700667static void htb_charge_class(struct htb_sched *q, struct htb_class *cl,
Ranjit Manomohanc9726d62007-07-10 22:43:16 -0700668 int level, struct sk_buff *skb)
Stephen Hemminger87990462006-08-10 23:35:16 -0700669{
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700670 int bytes = qdisc_pkt_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 enum htb_cmode old_mode;
Vimalkumar56b765b2012-10-31 06:04:11 +0000672 s64 diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 while (cl) {
Vimalkumar56b765b2012-10-31 06:04:11 +0000675 diff = min_t(s64, q->now - cl->t_c, cl->mbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (cl->level >= level) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700677 if (cl->level == level)
678 cl->xstats.lends++;
Jarek Poplawski59e42202008-12-03 21:17:27 -0800679 htb_accnt_tokens(cl, bytes, diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 } else {
681 cl->xstats.borrows++;
Stephen Hemminger87990462006-08-10 23:35:16 -0700682 cl->tokens += diff; /* we moved t_c; update tokens */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
Jarek Poplawski59e42202008-12-03 21:17:27 -0800684 htb_accnt_ctokens(cl, bytes, diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 cl->t_c = q->now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Stephen Hemminger87990462006-08-10 23:35:16 -0700687 old_mode = cl->cmode;
688 diff = 0;
689 htb_change_class_mode(q, cl, &diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (old_mode != cl->cmode) {
691 if (old_mode != HTB_CAN_SEND)
Eric Dumazetc9364632013-06-15 03:30:10 -0700692 htb_safe_rb_erase(&cl->pq_node, &q->hlevel[cl->level].wait_pq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (cl->cmode != HTB_CAN_SEND)
Stephen Hemminger87990462006-08-10 23:35:16 -0700694 htb_add_to_wait_tree(q, cl, diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Eric Dumazetbfe0d022011-01-09 08:30:54 +0000697 /* update basic stats except for leaves which are already updated */
698 if (cl->level)
699 bstats_update(&cl->bstats, skb);
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 cl = cl->parent;
702 }
703}
704
705/**
706 * htb_do_events - make mode changes to classes at the level
707 *
Patrick McHardyfb983d42007-03-16 01:22:39 -0700708 * Scans event queue for pending events and applies them. Returns time of
Jarek Poplawski12247362009-02-01 01:13:22 -0800709 * next pending event (0 for no event in pq, q->now for too many events).
Patrick McHardyfb983d42007-03-16 01:22:39 -0700710 * Note: Applied are events whose have cl->pq_key <= q->now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 */
Eric Dumazetc9364632013-06-15 03:30:10 -0700712static s64 htb_do_events(struct htb_sched *q, const int level,
Eric Dumazet5343a7f2013-06-04 07:11:48 +0000713 unsigned long start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Martin Devera8f3ea332008-03-23 22:00:38 -0700715 /* don't run for longer than 2 jiffies; 2 is used instead of
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000716 * 1 to simplify things when jiffy is going to be incremented
717 * too soon
718 */
Jarek Poplawskia73be042009-01-12 21:54:40 -0800719 unsigned long stop_at = start + 2;
Eric Dumazetc9364632013-06-15 03:30:10 -0700720 struct rb_root *wait_pq = &q->hlevel[level].wait_pq;
721
Martin Devera8f3ea332008-03-23 22:00:38 -0700722 while (time_before(jiffies, stop_at)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct htb_class *cl;
Vimalkumar56b765b2012-10-31 06:04:11 +0000724 s64 diff;
Eric Dumazetc9364632013-06-15 03:30:10 -0700725 struct rb_node *p = rb_first(wait_pq);
Akinbou Mita30bdbe32006-10-12 01:52:05 -0700726
Stephen Hemminger87990462006-08-10 23:35:16 -0700727 if (!p)
728 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 cl = rb_entry(p, struct htb_class, pq_node);
Patrick McHardyfb983d42007-03-16 01:22:39 -0700731 if (cl->pq_key > q->now)
732 return cl->pq_key;
733
Eric Dumazetc9364632013-06-15 03:30:10 -0700734 htb_safe_rb_erase(p, wait_pq);
Vimalkumar56b765b2012-10-31 06:04:11 +0000735 diff = min_t(s64, q->now - cl->t_c, cl->mbuffer);
Stephen Hemminger87990462006-08-10 23:35:16 -0700736 htb_change_class_mode(q, cl, &diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (cl->cmode != HTB_CAN_SEND)
Stephen Hemminger87990462006-08-10 23:35:16 -0700738 htb_add_to_wait_tree(q, cl, diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
Jarek Poplawski12247362009-02-01 01:13:22 -0800740
741 /* too much load - let's continue after a break for scheduling */
Jarek Poplawskie82181d2009-02-01 01:13:05 -0800742 if (!(q->warned & HTB_WARN_TOOMANYEVENTS)) {
Yang Yingliangc17988a2013-12-23 17:38:58 +0800743 pr_warn("htb: too many events!\n");
Jarek Poplawskie82181d2009-02-01 01:13:05 -0800744 q->warned |= HTB_WARN_TOOMANYEVENTS;
745 }
Jarek Poplawski12247362009-02-01 01:13:22 -0800746
747 return q->now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
750/* Returns class->node+prio from id-tree where classe's id is >= id. NULL
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000751 * is no such one exists.
752 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700753static struct rb_node *htb_id_find_next_upper(int prio, struct rb_node *n,
754 u32 id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 struct rb_node *r = NULL;
757 while (n) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700758 struct htb_class *cl =
759 rb_entry(n, struct htb_class, node[prio]);
Stephen Hemminger87990462006-08-10 23:35:16 -0700760
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700761 if (id > cl->common.classid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 n = n->rb_right;
Jarek Poplawski1b5c0072008-12-09 22:34:40 -0800763 } else if (id < cl->common.classid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 r = n;
765 n = n->rb_left;
Jarek Poplawski1b5c0072008-12-09 22:34:40 -0800766 } else {
767 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769 }
770 return r;
771}
772
773/**
774 * htb_lookup_leaf - returns next leaf class in DRR order
775 *
776 * Find leaf where current feed pointers points to.
777 */
Eric Dumazetc9364632013-06-15 03:30:10 -0700778static struct htb_class *htb_lookup_leaf(struct htb_prio *hprio, const int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 int i;
781 struct {
782 struct rb_node *root;
783 struct rb_node **pptr;
784 u32 *pid;
Stephen Hemminger87990462006-08-10 23:35:16 -0700785 } stk[TC_HTB_MAXDEPTH], *sp = stk;
786
Eric Dumazetc9364632013-06-15 03:30:10 -0700787 BUG_ON(!hprio->row.rb_node);
788 sp->root = hprio->row.rb_node;
789 sp->pptr = &hprio->ptr;
790 sp->pid = &hprio->last_ptr_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 for (i = 0; i < 65535; i++) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700793 if (!*sp->pptr && *sp->pid) {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900794 /* ptr was invalidated but id is valid - try to recover
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000795 * the original or next ptr
796 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700797 *sp->pptr =
798 htb_id_find_next_upper(prio, sp->root, *sp->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Stephen Hemminger87990462006-08-10 23:35:16 -0700800 *sp->pid = 0; /* ptr is valid now so that remove this hint as it
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000801 * can become out of date quickly
802 */
Stephen Hemminger87990462006-08-10 23:35:16 -0700803 if (!*sp->pptr) { /* we are at right end; rewind & go up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 *sp->pptr = sp->root;
Stephen Hemminger87990462006-08-10 23:35:16 -0700805 while ((*sp->pptr)->rb_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 *sp->pptr = (*sp->pptr)->rb_left;
807 if (sp > stk) {
808 sp--;
Jarek Poplawski512bb432008-12-09 22:35:02 -0800809 if (!*sp->pptr) {
810 WARN_ON(1);
Stephen Hemminger87990462006-08-10 23:35:16 -0700811 return NULL;
Jarek Poplawski512bb432008-12-09 22:35:02 -0800812 }
Stephen Hemminger87990462006-08-10 23:35:16 -0700813 htb_next_rb_node(sp->pptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815 } else {
816 struct htb_class *cl;
Eric Dumazetc9364632013-06-15 03:30:10 -0700817 struct htb_prio *clp;
818
Stephen Hemminger87990462006-08-10 23:35:16 -0700819 cl = rb_entry(*sp->pptr, struct htb_class, node[prio]);
820 if (!cl->level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return cl;
Eric Dumazetc9364632013-06-15 03:30:10 -0700822 clp = &cl->un.inner.clprio[prio];
823 (++sp)->root = clp->feed.rb_node;
824 sp->pptr = &clp->ptr;
825 sp->pid = &clp->last_ptr_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700828 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return NULL;
830}
831
832/* dequeues packet at given priority and level; call only if
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000833 * you are sure that there is active class at prio/level
834 */
Eric Dumazetc9364632013-06-15 03:30:10 -0700835static struct sk_buff *htb_dequeue_tree(struct htb_sched *q, const int prio,
836 const int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
838 struct sk_buff *skb = NULL;
Stephen Hemminger87990462006-08-10 23:35:16 -0700839 struct htb_class *cl, *start;
Eric Dumazetc9364632013-06-15 03:30:10 -0700840 struct htb_level *hlevel = &q->hlevel[level];
841 struct htb_prio *hprio = &hlevel->hprio[prio];
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 /* look initial class up in the row */
Eric Dumazetc9364632013-06-15 03:30:10 -0700844 start = cl = htb_lookup_leaf(hprio, prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 do {
847next:
Jarek Poplawski512bb432008-12-09 22:35:02 -0800848 if (unlikely(!cl))
Stephen Hemminger87990462006-08-10 23:35:16 -0700849 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 /* class can be empty - it is unlikely but can be true if leaf
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000852 * qdisc drops packets in enqueue routine or if someone used
853 * graft operation on the leaf since last dequeue;
854 * simply deactivate and skip such class
855 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (unlikely(cl->un.leaf.q->q.qlen == 0)) {
857 struct htb_class *next;
Stephen Hemminger87990462006-08-10 23:35:16 -0700858 htb_deactivate(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 /* row/level might become empty */
861 if ((q->row_mask[level] & (1 << prio)) == 0)
Stephen Hemminger87990462006-08-10 23:35:16 -0700862 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Eric Dumazetc9364632013-06-15 03:30:10 -0700864 next = htb_lookup_leaf(hprio, prio);
Stephen Hemminger87990462006-08-10 23:35:16 -0700865
866 if (cl == start) /* fix start if we just deleted it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 start = next;
868 cl = next;
869 goto next;
870 }
Stephen Hemminger87990462006-08-10 23:35:16 -0700871
872 skb = cl->un.leaf.q->dequeue(cl->un.leaf.q);
873 if (likely(skb != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 break;
Jarek Poplawski633fe662008-12-03 21:09:10 -0800875
Jarek Poplawskib00355d2009-02-01 01:12:42 -0800876 qdisc_warn_nonwc("htb", cl->un.leaf.q);
Eric Dumazetc9364632013-06-15 03:30:10 -0700877 htb_next_rb_node(level ? &cl->parent->un.inner.clprio[prio].ptr:
878 &q->hlevel[0].hprio[prio].ptr);
879 cl = htb_lookup_leaf(hprio, prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 } while (cl != start);
882
883 if (likely(skb != NULL)) {
Eric Dumazet196d97f2012-11-05 16:40:49 +0000884 bstats_update(&cl->bstats, skb);
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700885 cl->un.leaf.deficit[level] -= qdisc_pkt_len(skb);
886 if (cl->un.leaf.deficit[level] < 0) {
Jarek Poplawskic19f7a32008-12-03 21:09:45 -0800887 cl->un.leaf.deficit[level] += cl->quantum;
Eric Dumazetc9364632013-06-15 03:30:10 -0700888 htb_next_rb_node(level ? &cl->parent->un.inner.clprio[prio].ptr :
889 &q->hlevel[0].hprio[prio].ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891 /* this used to be after charge_class but this constelation
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000892 * gives us slightly better performance
893 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (!cl->un.leaf.q->q.qlen)
Stephen Hemminger87990462006-08-10 23:35:16 -0700895 htb_deactivate(q, cl);
Ranjit Manomohanc9726d62007-07-10 22:43:16 -0700896 htb_charge_class(q, cl, level, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898 return skb;
899}
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901static struct sk_buff *htb_dequeue(struct Qdisc *sch)
902{
Eric Dumazet9190b3b2011-01-20 23:31:33 -0800903 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 struct htb_sched *q = qdisc_priv(sch);
905 int level;
Eric Dumazet5343a7f2013-06-04 07:11:48 +0000906 s64 next_event;
Jarek Poplawskia73be042009-01-12 21:54:40 -0800907 unsigned long start_at;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 /* try to dequeue direct packets as high prio (!) to minimize cpu work */
Florian Westphal48da34b2016-09-18 00:57:34 +0200910 skb = __qdisc_dequeue_head(&q->direct_queue);
Stephen Hemminger87990462006-08-10 23:35:16 -0700911 if (skb != NULL) {
Eric Dumazet9190b3b2011-01-20 23:31:33 -0800912ok:
913 qdisc_bstats_update(sch, skb);
WANG Cong431e3a82016-02-25 14:55:02 -0800914 qdisc_qstats_backlog_dec(sch, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 sch->q.qlen--;
916 return skb;
917 }
918
Stephen Hemminger87990462006-08-10 23:35:16 -0700919 if (!sch->q.qlen)
920 goto fin;
Eric Dumazetd2de8752014-08-22 18:32:09 -0700921 q->now = ktime_get_ns();
Jarek Poplawskia73be042009-01-12 21:54:40 -0800922 start_at = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Stefan Haskod2fe85d2012-12-21 15:04:59 +0000924 next_event = q->now + 5LLU * NSEC_PER_SEC;
Jarek Poplawski633fe662008-12-03 21:09:10 -0800925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 for (level = 0; level < TC_HTB_MAXDEPTH; level++) {
927 /* common case optimization - skip event handler quickly */
928 int m;
Eric Dumazetc9364632013-06-15 03:30:10 -0700929 s64 event = q->near_ev_cache[level];
Stephen Hemminger87990462006-08-10 23:35:16 -0700930
Eric Dumazetc9364632013-06-15 03:30:10 -0700931 if (q->now >= event) {
Jarek Poplawskia73be042009-01-12 21:54:40 -0800932 event = htb_do_events(q, level, start_at);
Patrick McHardy2e4b3b02007-05-23 23:39:54 -0700933 if (!event)
Vimalkumar56b765b2012-10-31 06:04:11 +0000934 event = q->now + NSEC_PER_SEC;
Patrick McHardy2e4b3b02007-05-23 23:39:54 -0700935 q->near_ev_cache[level] = event;
Eric Dumazetc9364632013-06-15 03:30:10 -0700936 }
Patrick McHardyfb983d42007-03-16 01:22:39 -0700937
Jarek Poplawskic0851342009-01-12 21:54:16 -0800938 if (next_event > event)
Patrick McHardyfb983d42007-03-16 01:22:39 -0700939 next_event = event;
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 m = ~q->row_mask[level];
942 while (m != (int)(-1)) {
Stephen Hemminger87990462006-08-10 23:35:16 -0700943 int prio = ffz(m);
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 m |= 1 << prio;
Stephen Hemminger87990462006-08-10 23:35:16 -0700946 skb = htb_dequeue_tree(q, prio, level);
Eric Dumazet9190b3b2011-01-20 23:31:33 -0800947 if (likely(skb != NULL))
948 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950 }
John Fastabend25331d62014-09-28 11:53:29 -0700951 qdisc_qstats_overlimit(sch);
Eric Dumazeta9efad82016-05-23 14:24:56 -0700952 if (likely(next_event > q->now))
Eric Dumazet45f50be2016-06-10 16:41:39 -0700953 qdisc_watchdog_schedule_ns(&q->watchdog, next_event);
Eric Dumazeta9efad82016-05-23 14:24:56 -0700954 else
Jarek Poplawski12247362009-02-01 01:13:22 -0800955 schedule_work(&q->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956fin:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return skb;
958}
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960/* reset all classes */
961/* always caled under BH & queue lock */
Stephen Hemminger87990462006-08-10 23:35:16 -0700962static void htb_reset(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
964 struct htb_sched *q = qdisc_priv(sch);
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700965 struct htb_class *cl;
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700966 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -0700968 for (i = 0; i < q->clhash.hashsize; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800969 hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (cl->level)
Stephen Hemminger87990462006-08-10 23:35:16 -0700971 memset(&cl->un.inner, 0, sizeof(cl->un.inner));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 else {
Stephen Hemminger87990462006-08-10 23:35:16 -0700973 if (cl->un.leaf.q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 qdisc_reset(cl->un.leaf.q);
975 INIT_LIST_HEAD(&cl->un.leaf.drop_list);
976 }
977 cl->prio_activity = 0;
978 cl->cmode = HTB_CAN_SEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980 }
Patrick McHardyfb983d42007-03-16 01:22:39 -0700981 qdisc_watchdog_cancel(&q->watchdog);
Eric Dumazeta5a9f5342016-06-13 20:21:56 -0700982 __qdisc_reset_queue(&q->direct_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 sch->q.qlen = 0;
WANG Cong431e3a82016-02-25 14:55:02 -0800984 sch->qstats.backlog = 0;
Eric Dumazetc9364632013-06-15 03:30:10 -0700985 memset(q->hlevel, 0, sizeof(q->hlevel));
Stephen Hemminger87990462006-08-10 23:35:16 -0700986 memset(q->row_mask, 0, sizeof(q->row_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 for (i = 0; i < TC_HTB_NUMPRIO; i++)
Stephen Hemminger87990462006-08-10 23:35:16 -0700988 INIT_LIST_HEAD(q->drops + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
Patrick McHardy27a34212008-01-23 20:35:39 -0800991static const struct nla_policy htb_policy[TCA_HTB_MAX + 1] = {
992 [TCA_HTB_PARMS] = { .len = sizeof(struct tc_htb_opt) },
993 [TCA_HTB_INIT] = { .len = sizeof(struct tc_htb_glob) },
994 [TCA_HTB_CTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
995 [TCA_HTB_RTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
Eric Dumazet6906f4e2013-03-06 06:49:21 +0000996 [TCA_HTB_DIRECT_QLEN] = { .type = NLA_U32 },
Eric Dumazetdf62cdf2013-09-19 09:10:20 -0700997 [TCA_HTB_RATE64] = { .type = NLA_U64 },
998 [TCA_HTB_CEIL64] = { .type = NLA_U64 },
Patrick McHardy27a34212008-01-23 20:35:39 -0800999};
1000
Jarek Poplawski12247362009-02-01 01:13:22 -08001001static void htb_work_func(struct work_struct *work)
1002{
1003 struct htb_sched *q = container_of(work, struct htb_sched, work);
1004 struct Qdisc *sch = q->watchdog.qdisc;
1005
Florian Westphal0ee13622016-06-14 06:16:27 +02001006 rcu_read_lock();
Jarek Poplawski12247362009-02-01 01:13:22 -08001007 __netif_schedule(qdisc_root(sch));
Florian Westphal0ee13622016-06-14 06:16:27 +02001008 rcu_read_unlock();
Jarek Poplawski12247362009-02-01 01:13:22 -08001009}
1010
Patrick McHardy1e904742008-01-22 22:11:17 -08001011static int htb_init(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
1013 struct htb_sched *q = qdisc_priv(sch);
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001014 struct nlattr *tb[TCA_HTB_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 struct tc_htb_glob *gopt;
Patrick McHardycee63722008-01-23 20:33:32 -08001016 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 int i;
Patrick McHardycee63722008-01-23 20:33:32 -08001018
1019 if (!opt)
1020 return -EINVAL;
1021
Jiri Pirko6529eab2017-05-17 11:07:55 +02001022 err = tcf_block_get(&q->block, &q->filter_list);
1023 if (err)
1024 return err;
1025
Johannes Bergfceb6432017-04-12 14:34:07 +02001026 err = nla_parse_nested(tb, TCA_HTB_MAX, opt, htb_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -08001027 if (err < 0)
1028 return err;
1029
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001030 if (!tb[TCA_HTB_INIT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return -EINVAL;
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001032
Patrick McHardy1e904742008-01-22 22:11:17 -08001033 gopt = nla_data(tb[TCA_HTB_INIT]);
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001034 if (gopt->version != HTB_VER >> 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001037 err = qdisc_class_hash_init(&q->clhash);
1038 if (err < 0)
1039 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 for (i = 0; i < TC_HTB_NUMPRIO; i++)
Stephen Hemminger87990462006-08-10 23:35:16 -07001041 INIT_LIST_HEAD(q->drops + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Patrick McHardyfb983d42007-03-16 01:22:39 -07001043 qdisc_watchdog_init(&q->watchdog, sch);
Jarek Poplawski12247362009-02-01 01:13:22 -08001044 INIT_WORK(&q->work, htb_work_func);
Florian Westphal48da34b2016-09-18 00:57:34 +02001045 qdisc_skb_head_init(&q->direct_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001047 if (tb[TCA_HTB_DIRECT_QLEN])
1048 q->direct_qlen = nla_get_u32(tb[TCA_HTB_DIRECT_QLEN]);
Phil Sutter348e3432015-08-18 10:30:49 +02001049 else
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001050 q->direct_qlen = qdisc_dev(sch)->tx_queue_len;
Phil Sutter348e3432015-08-18 10:30:49 +02001051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if ((q->rate2quantum = gopt->rate2quantum) < 1)
1053 q->rate2quantum = 1;
1054 q->defcls = gopt->defcls;
1055
1056 return 0;
1057}
1058
1059static int htb_dump(struct Qdisc *sch, struct sk_buff *skb)
1060{
1061 struct htb_sched *q = qdisc_priv(sch);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001062 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 struct tc_htb_glob gopt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Eric Dumazet6f542ef2014-03-05 10:14:34 -08001065 /* Its safe to not acquire qdisc lock. As we hold RTNL,
1066 * no change can happen on the qdisc parameters.
1067 */
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001068
1069 gopt.direct_pkts = q->direct_pkts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 gopt.version = HTB_VER;
1071 gopt.rate2quantum = q->rate2quantum;
1072 gopt.defcls = q->defcls;
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001073 gopt.debug = 0;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001074
1075 nest = nla_nest_start(skb, TCA_OPTIONS);
1076 if (nest == NULL)
1077 goto nla_put_failure;
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001078 if (nla_put(skb, TCA_HTB_INIT, sizeof(gopt), &gopt) ||
1079 nla_put_u32(skb, TCA_HTB_DIRECT_QLEN, q->direct_qlen))
David S. Miller1b34ec42012-03-29 05:11:39 -04001080 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001081
Eric Dumazet6f542ef2014-03-05 10:14:34 -08001082 return nla_nest_end(skb, nest);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001083
Patrick McHardy1e904742008-01-22 22:11:17 -08001084nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001085 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return -1;
1087}
1088
1089static int htb_dump_class(struct Qdisc *sch, unsigned long arg,
Stephen Hemminger87990462006-08-10 23:35:16 -07001090 struct sk_buff *skb, struct tcmsg *tcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Stephen Hemminger87990462006-08-10 23:35:16 -07001092 struct htb_class *cl = (struct htb_class *)arg;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001093 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 struct tc_htb_opt opt;
1095
Eric Dumazet6f542ef2014-03-05 10:14:34 -08001096 /* Its safe to not acquire qdisc lock. As we hold RTNL,
1097 * no change can happen on the class parameters.
1098 */
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001099 tcm->tcm_parent = cl->parent ? cl->parent->common.classid : TC_H_ROOT;
1100 tcm->tcm_handle = cl->common.classid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (!cl->level && cl->un.leaf.q)
1102 tcm->tcm_info = cl->un.leaf.q->handle;
1103
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001104 nest = nla_nest_start(skb, TCA_OPTIONS);
1105 if (nest == NULL)
1106 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Stephen Hemminger87990462006-08-10 23:35:16 -07001108 memset(&opt, 0, sizeof(opt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Eric Dumazet01cb71d2013-06-02 13:55:05 +00001110 psched_ratecfg_getrate(&opt.rate, &cl->rate);
Jiri Pirko9c10f412013-02-12 00:12:00 +00001111 opt.buffer = PSCHED_NS2TICKS(cl->buffer);
Eric Dumazet01cb71d2013-06-02 13:55:05 +00001112 psched_ratecfg_getrate(&opt.ceil, &cl->ceil);
Jiri Pirko9c10f412013-02-12 00:12:00 +00001113 opt.cbuffer = PSCHED_NS2TICKS(cl->cbuffer);
Jarek Poplawskic19f7a32008-12-03 21:09:45 -08001114 opt.quantum = cl->quantum;
1115 opt.prio = cl->prio;
Stephen Hemminger87990462006-08-10 23:35:16 -07001116 opt.level = cl->level;
David S. Miller1b34ec42012-03-29 05:11:39 -04001117 if (nla_put(skb, TCA_HTB_PARMS, sizeof(opt), &opt))
1118 goto nla_put_failure;
Eric Dumazetdf62cdf2013-09-19 09:10:20 -07001119 if ((cl->rate.rate_bytes_ps >= (1ULL << 32)) &&
Nicolas Dichtel2a51c1e2016-04-25 10:25:15 +02001120 nla_put_u64_64bit(skb, TCA_HTB_RATE64, cl->rate.rate_bytes_ps,
1121 TCA_HTB_PAD))
Eric Dumazetdf62cdf2013-09-19 09:10:20 -07001122 goto nla_put_failure;
1123 if ((cl->ceil.rate_bytes_ps >= (1ULL << 32)) &&
Nicolas Dichtel2a51c1e2016-04-25 10:25:15 +02001124 nla_put_u64_64bit(skb, TCA_HTB_CEIL64, cl->ceil.rate_bytes_ps,
1125 TCA_HTB_PAD))
Eric Dumazetdf62cdf2013-09-19 09:10:20 -07001126 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001127
Eric Dumazet6f542ef2014-03-05 10:14:34 -08001128 return nla_nest_end(skb, nest);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001129
Patrick McHardy1e904742008-01-22 22:11:17 -08001130nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001131 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return -1;
1133}
1134
1135static int
Stephen Hemminger87990462006-08-10 23:35:16 -07001136htb_dump_class_stats(struct Qdisc *sch, unsigned long arg, struct gnet_dump *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Stephen Hemminger87990462006-08-10 23:35:16 -07001138 struct htb_class *cl = (struct htb_class *)arg;
Eric Dumazet338ed9b2016-06-21 23:16:51 -07001139 struct gnet_stats_queue qs = {
1140 .drops = cl->drops,
1141 };
John Fastabend64015852014-09-28 11:53:57 -07001142 __u32 qlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Eric Dumazet338ed9b2016-06-21 23:16:51 -07001144 if (!cl->level && cl->un.leaf.q) {
John Fastabend64015852014-09-28 11:53:57 -07001145 qlen = cl->un.leaf.q->q.qlen;
Eric Dumazet338ed9b2016-06-21 23:16:51 -07001146 qs.backlog = cl->un.leaf.q->qstats.backlog;
1147 }
Konstantin Khlebnikov0564bf02016-07-16 17:08:56 +03001148 cl->xstats.tokens = clamp_t(s64, PSCHED_NS2TICKS(cl->tokens),
1149 INT_MIN, INT_MAX);
1150 cl->xstats.ctokens = clamp_t(s64, PSCHED_NS2TICKS(cl->ctokens),
1151 INT_MIN, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Eric Dumazetedb09eb2016-06-06 09:37:16 -07001153 if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
1154 d, NULL, &cl->bstats) < 0 ||
Eric Dumazet1c0d32f2016-12-04 09:48:16 -08001155 gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
Eric Dumazet338ed9b2016-06-21 23:16:51 -07001156 gnet_stats_copy_queue(d, NULL, &qs, qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return -1;
1158
1159 return gnet_stats_copy_app(d, &cl->xstats, sizeof(cl->xstats));
1160}
1161
1162static int htb_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
Stephen Hemminger87990462006-08-10 23:35:16 -07001163 struct Qdisc **old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
Stephen Hemminger87990462006-08-10 23:35:16 -07001165 struct htb_class *cl = (struct htb_class *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Patrick McHardy5b9a9cc2009-09-04 06:41:17 +00001167 if (cl->level)
1168 return -EINVAL;
1169 if (new == NULL &&
Changli Gao3511c912010-10-16 13:04:08 +00001170 (new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
Patrick McHardy5b9a9cc2009-09-04 06:41:17 +00001171 cl->common.classid)) == NULL)
1172 return -ENOBUFS;
1173
WANG Cong86a79962016-02-25 14:55:00 -08001174 *old = qdisc_replace(sch, new, &cl->un.leaf.q);
Patrick McHardy5b9a9cc2009-09-04 06:41:17 +00001175 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
Stephen Hemminger87990462006-08-10 23:35:16 -07001178static struct Qdisc *htb_leaf(struct Qdisc *sch, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Stephen Hemminger87990462006-08-10 23:35:16 -07001180 struct htb_class *cl = (struct htb_class *)arg;
Patrick McHardy5b9a9cc2009-09-04 06:41:17 +00001181 return !cl->level ? cl->un.leaf.q : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
Patrick McHardy256d61b2006-11-29 17:37:05 -08001184static void htb_qlen_notify(struct Qdisc *sch, unsigned long arg)
1185{
1186 struct htb_class *cl = (struct htb_class *)arg;
1187
1188 if (cl->un.leaf.q->q.qlen == 0)
1189 htb_deactivate(qdisc_priv(sch), cl);
1190}
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192static unsigned long htb_get(struct Qdisc *sch, u32 classid)
1193{
Stephen Hemminger87990462006-08-10 23:35:16 -07001194 struct htb_class *cl = htb_find(classid, sch);
1195 if (cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 cl->refcnt++;
1197 return (unsigned long)cl;
1198}
1199
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001200static inline int htb_parent_last_child(struct htb_class *cl)
1201{
1202 if (!cl->parent)
1203 /* the root class */
1204 return 0;
Patrick McHardy42077592008-07-05 23:22:53 -07001205 if (cl->parent->children > 1)
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001206 /* not the last child */
1207 return 0;
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001208 return 1;
1209}
1210
Jarek Poplawski3ba08b02008-05-03 20:46:29 -07001211static void htb_parent_to_leaf(struct htb_sched *q, struct htb_class *cl,
1212 struct Qdisc *new_q)
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001213{
1214 struct htb_class *parent = cl->parent;
1215
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001216 WARN_ON(cl->level || !cl->un.leaf.q || cl->prio_activity);
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001217
Jarek Poplawski3ba08b02008-05-03 20:46:29 -07001218 if (parent->cmode != HTB_CAN_SEND)
Eric Dumazetc9364632013-06-15 03:30:10 -07001219 htb_safe_rb_erase(&parent->pq_node,
1220 &q->hlevel[parent->level].wait_pq);
Jarek Poplawski3ba08b02008-05-03 20:46:29 -07001221
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001222 parent->level = 0;
1223 memset(&parent->un.inner, 0, sizeof(parent->un.inner));
1224 INIT_LIST_HEAD(&parent->un.leaf.drop_list);
1225 parent->un.leaf.q = new_q ? new_q : &noop_qdisc;
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001226 parent->tokens = parent->buffer;
1227 parent->ctokens = parent->cbuffer;
Eric Dumazetd2de8752014-08-22 18:32:09 -07001228 parent->t_c = ktime_get_ns();
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001229 parent->cmode = HTB_CAN_SEND;
1230}
1231
Stephen Hemminger87990462006-08-10 23:35:16 -07001232static void htb_destroy_class(struct Qdisc *sch, struct htb_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (!cl->level) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001235 WARN_ON(!cl->un.leaf.q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 qdisc_destroy(cl->un.leaf.q);
1237 }
Eric Dumazet1c0d32f2016-12-04 09:48:16 -08001238 gen_kill_estimator(&cl->rate_est);
Jiri Pirko6529eab2017-05-17 11:07:55 +02001239 tcf_block_put(cl->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 kfree(cl);
1241}
1242
Stephen Hemminger87990462006-08-10 23:35:16 -07001243static void htb_destroy(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
1245 struct htb_sched *q = qdisc_priv(sch);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001246 struct hlist_node *next;
Patrick McHardyfbd8f132008-07-05 23:22:19 -07001247 struct htb_class *cl;
1248 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Jarek Poplawski12247362009-02-01 01:13:22 -08001250 cancel_work_sync(&q->work);
Patrick McHardyfb983d42007-03-16 01:22:39 -07001251 qdisc_watchdog_cancel(&q->watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 /* This line used to be after htb_destroy_class call below
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001253 * and surprisingly it worked in 2.4. But it must precede it
1254 * because filter need its target class alive to be able to call
1255 * unbind_filter on it (without Oops).
1256 */
Jiri Pirko6529eab2017-05-17 11:07:55 +02001257 tcf_block_put(q->block);
Stephen Hemminger87990462006-08-10 23:35:16 -07001258
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001259 for (i = 0; i < q->clhash.hashsize; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001260 hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode)
Jiri Pirko6529eab2017-05-17 11:07:55 +02001261 tcf_block_put(cl->block);
Patrick McHardyfbd8f132008-07-05 23:22:19 -07001262 }
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001263 for (i = 0; i < q->clhash.hashsize; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001264 hlist_for_each_entry_safe(cl, next, &q->clhash.hash[i],
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001265 common.hnode)
Patrick McHardyfbd8f132008-07-05 23:22:19 -07001266 htb_destroy_class(sch, cl);
1267 }
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001268 qdisc_class_hash_destroy(&q->clhash);
Eric Dumazeta5a9f5342016-06-13 20:21:56 -07001269 __qdisc_reset_queue(&q->direct_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272static int htb_delete(struct Qdisc *sch, unsigned long arg)
1273{
1274 struct htb_sched *q = qdisc_priv(sch);
Stephen Hemminger87990462006-08-10 23:35:16 -07001275 struct htb_class *cl = (struct htb_class *)arg;
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001276 struct Qdisc *new_q = NULL;
1277 int last_child = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Yang Yinglianga071d272013-12-23 17:38:59 +08001279 /* TODO: why don't allow to delete subtree ? references ? does
1280 * tc subsys guarantee us that in htb_destroy it holds no class
1281 * refs so that we can remove children safely there ?
1282 */
Patrick McHardy42077592008-07-05 23:22:53 -07001283 if (cl->children || cl->filter_cnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return -EBUSY;
Stephen Hemminger87990462006-08-10 23:35:16 -07001285
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001286 if (!cl->level && htb_parent_last_child(cl)) {
Changli Gao3511c912010-10-16 13:04:08 +00001287 new_q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
David S. Millerbb949fb2008-07-08 16:55:56 -07001288 cl->parent->common.classid);
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001289 last_child = 1;
1290 }
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 sch_tree_lock(sch);
Stephen Hemminger87990462006-08-10 23:35:16 -07001293
Patrick McHardy814a175e2006-11-29 17:34:50 -08001294 if (!cl->level) {
WANG Cong2ccccf52016-02-25 14:55:01 -08001295 unsigned int qlen = cl->un.leaf.q->q.qlen;
1296 unsigned int backlog = cl->un.leaf.q->qstats.backlog;
1297
Patrick McHardy814a175e2006-11-29 17:34:50 -08001298 qdisc_reset(cl->un.leaf.q);
WANG Cong2ccccf52016-02-25 14:55:01 -08001299 qdisc_tree_reduce_backlog(cl->un.leaf.q, qlen, backlog);
Patrick McHardy814a175e2006-11-29 17:34:50 -08001300 }
1301
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001302 /* delete from hash and active; remainder in destroy_class */
1303 qdisc_class_hash_remove(&q->clhash, &cl->common);
Jarek Poplawski26b284d2008-08-13 15:16:43 -07001304 if (cl->parent)
1305 cl->parent->children--;
Patrick McHardyc38c83c2007-03-27 14:04:24 -07001306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (cl->prio_activity)
Stephen Hemminger87990462006-08-10 23:35:16 -07001308 htb_deactivate(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Patrick McHardyfbd8f132008-07-05 23:22:19 -07001310 if (cl->cmode != HTB_CAN_SEND)
Eric Dumazetc9364632013-06-15 03:30:10 -07001311 htb_safe_rb_erase(&cl->pq_node,
1312 &q->hlevel[cl->level].wait_pq);
Patrick McHardyfbd8f132008-07-05 23:22:19 -07001313
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001314 if (last_child)
Jarek Poplawski3ba08b02008-05-03 20:46:29 -07001315 htb_parent_to_leaf(q, cl, new_q);
Jarek Poplawski160d5e12006-12-08 00:26:56 -08001316
Jarek Poplawski7cd0a632009-03-15 20:00:19 -07001317 BUG_ON(--cl->refcnt == 0);
1318 /*
1319 * This shouldn't happen: we "hold" one cops->get() when called
1320 * from tc_ctl_tclass; the destroy method is done from cops->put().
1321 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 sch_tree_unlock(sch);
1324 return 0;
1325}
1326
1327static void htb_put(struct Qdisc *sch, unsigned long arg)
1328{
Stephen Hemminger87990462006-08-10 23:35:16 -07001329 struct htb_class *cl = (struct htb_class *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 if (--cl->refcnt == 0)
Stephen Hemminger87990462006-08-10 23:35:16 -07001332 htb_destroy_class(sch, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
Stephen Hemminger87990462006-08-10 23:35:16 -07001335static int htb_change_class(struct Qdisc *sch, u32 classid,
Patrick McHardy1e904742008-01-22 22:11:17 -08001336 u32 parentid, struct nlattr **tca,
Stephen Hemminger87990462006-08-10 23:35:16 -07001337 unsigned long *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 int err = -EINVAL;
1340 struct htb_sched *q = qdisc_priv(sch);
Stephen Hemminger87990462006-08-10 23:35:16 -07001341 struct htb_class *cl = (struct htb_class *)*arg, *parent;
Patrick McHardy1e904742008-01-22 22:11:17 -08001342 struct nlattr *opt = tca[TCA_OPTIONS];
Eric Dumazet6906f4e2013-03-06 06:49:21 +00001343 struct nlattr *tb[TCA_HTB_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 struct tc_htb_opt *hopt;
Eric Dumazetdf62cdf2013-09-19 09:10:20 -07001345 u64 rate64, ceil64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 /* extract all subattrs from opt attr */
Patrick McHardycee63722008-01-23 20:33:32 -08001348 if (!opt)
1349 goto failure;
1350
Johannes Bergfceb6432017-04-12 14:34:07 +02001351 err = nla_parse_nested(tb, TCA_HTB_MAX, opt, htb_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -08001352 if (err < 0)
1353 goto failure;
1354
1355 err = -EINVAL;
Patrick McHardy27a34212008-01-23 20:35:39 -08001356 if (tb[TCA_HTB_PARMS] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Stephen Hemminger87990462006-08-10 23:35:16 -07001359 parent = parentid == TC_H_ROOT ? NULL : htb_find(parentid, sch);
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001360
Patrick McHardy1e904742008-01-22 22:11:17 -08001361 hopt = nla_data(tb[TCA_HTB_PARMS]);
Eric Dumazet196d97f2012-11-05 16:40:49 +00001362 if (!hopt->rate.rate || !hopt->ceil.rate)
Stephen Hemminger87990462006-08-10 23:35:16 -07001363 goto failure;
1364
Jesper Dangaard Brouer8a8e3d82013-08-14 23:47:11 +02001365 /* Keeping backward compatible with rate_table based iproute2 tc */
Yang Yingliang6b1dd852013-12-11 15:48:37 +08001366 if (hopt->rate.linklayer == TC_LINKLAYER_UNAWARE)
1367 qdisc_put_rtab(qdisc_get_rtab(&hopt->rate, tb[TCA_HTB_RTAB]));
1368
1369 if (hopt->ceil.linklayer == TC_LINKLAYER_UNAWARE)
1370 qdisc_put_rtab(qdisc_get_rtab(&hopt->ceil, tb[TCA_HTB_CTAB]));
Jesper Dangaard Brouer8a8e3d82013-08-14 23:47:11 +02001371
Stephen Hemminger87990462006-08-10 23:35:16 -07001372 if (!cl) { /* new class */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 struct Qdisc *new_q;
Stephen Hemminger3696f622006-08-10 23:36:01 -07001374 int prio;
Patrick McHardyee39e102007-07-02 22:48:13 -07001375 struct {
Patrick McHardy1e904742008-01-22 22:11:17 -08001376 struct nlattr nla;
Patrick McHardyee39e102007-07-02 22:48:13 -07001377 struct gnet_estimator opt;
1378 } est = {
Patrick McHardy1e904742008-01-22 22:11:17 -08001379 .nla = {
1380 .nla_len = nla_attr_size(sizeof(est.opt)),
1381 .nla_type = TCA_RATE,
Patrick McHardyee39e102007-07-02 22:48:13 -07001382 },
1383 .opt = {
1384 /* 4s interval, 16s averaging constant */
1385 .interval = 2,
1386 .ewma_log = 2,
1387 },
1388 };
Stephen Hemminger3696f622006-08-10 23:36:01 -07001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /* check for valid classid */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001391 if (!classid || TC_H_MAJ(classid ^ sch->handle) ||
1392 htb_find(classid, sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 goto failure;
1394
1395 /* check maximal depth */
1396 if (parent && parent->parent && parent->parent->level < 2) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001397 pr_err("htb: tree is too deep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 goto failure;
1399 }
1400 err = -ENOBUFS;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001401 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
1402 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 goto failure;
Stephen Hemminger87990462006-08-10 23:35:16 -07001404
Jiri Pirko6529eab2017-05-17 11:07:55 +02001405 err = tcf_block_get(&cl->block, &cl->filter_list);
1406 if (err) {
1407 kfree(cl);
1408 goto failure;
1409 }
Eric Dumazet64153ce2013-06-06 14:53:16 -07001410 if (htb_rate_est || tca[TCA_RATE]) {
John Fastabend22e0f8b2014-09-28 11:52:56 -07001411 err = gen_new_estimator(&cl->bstats, NULL,
1412 &cl->rate_est,
Eric Dumazetedb09eb2016-06-06 09:37:16 -07001413 NULL,
1414 qdisc_root_sleeping_running(sch),
Eric Dumazet64153ce2013-06-06 14:53:16 -07001415 tca[TCA_RATE] ? : &est.nla);
1416 if (err) {
Jiri Pirko6529eab2017-05-17 11:07:55 +02001417 tcf_block_put(cl->block);
Eric Dumazet64153ce2013-06-06 14:53:16 -07001418 kfree(cl);
1419 goto failure;
1420 }
Stephen Hemminger71bcb092008-11-25 21:13:31 -08001421 }
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 cl->refcnt = 1;
Patrick McHardy42077592008-07-05 23:22:53 -07001424 cl->children = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 INIT_LIST_HEAD(&cl->un.leaf.drop_list);
Stephen Hemminger3696f622006-08-10 23:36:01 -07001426 RB_CLEAR_NODE(&cl->pq_node);
1427
1428 for (prio = 0; prio < TC_HTB_NUMPRIO; prio++)
1429 RB_CLEAR_NODE(&cl->node[prio]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 /* create leaf qdisc early because it uses kmalloc(GFP_KERNEL)
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001432 * so that can't be used inside of sch_tree_lock
1433 * -- thanks to Karlis Peisenieks
1434 */
Changli Gao3511c912010-10-16 13:04:08 +00001435 new_q = qdisc_create_dflt(sch->dev_queue,
David S. Millerbb949fb2008-07-08 16:55:56 -07001436 &pfifo_qdisc_ops, classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 sch_tree_lock(sch);
1438 if (parent && !parent->level) {
Patrick McHardy256d61b2006-11-29 17:37:05 -08001439 unsigned int qlen = parent->un.leaf.q->q.qlen;
WANG Cong2ccccf52016-02-25 14:55:01 -08001440 unsigned int backlog = parent->un.leaf.q->qstats.backlog;
Patrick McHardy256d61b2006-11-29 17:37:05 -08001441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /* turn parent into inner node */
Patrick McHardy256d61b2006-11-29 17:37:05 -08001443 qdisc_reset(parent->un.leaf.q);
WANG Cong2ccccf52016-02-25 14:55:01 -08001444 qdisc_tree_reduce_backlog(parent->un.leaf.q, qlen, backlog);
Stephen Hemminger87990462006-08-10 23:35:16 -07001445 qdisc_destroy(parent->un.leaf.q);
1446 if (parent->prio_activity)
1447 htb_deactivate(q, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 /* remove from evt list because of level change */
1450 if (parent->cmode != HTB_CAN_SEND) {
Eric Dumazetc9364632013-06-15 03:30:10 -07001451 htb_safe_rb_erase(&parent->pq_node, &q->hlevel[0].wait_pq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 parent->cmode = HTB_CAN_SEND;
1453 }
1454 parent->level = (parent->parent ? parent->parent->level
Stephen Hemminger87990462006-08-10 23:35:16 -07001455 : TC_HTB_MAXDEPTH) - 1;
1456 memset(&parent->un.inner, 0, sizeof(parent->un.inner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
1458 /* leaf (we) needs elementary qdisc */
1459 cl->un.leaf.q = new_q ? new_q : &noop_qdisc;
1460
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001461 cl->common.classid = classid;
Stephen Hemminger87990462006-08-10 23:35:16 -07001462 cl->parent = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 /* set class to be in HTB_CAN_SEND state */
Jiri Pirkob9a7afd2013-02-12 00:12:02 +00001465 cl->tokens = PSCHED_TICKS2NS(hopt->buffer);
1466 cl->ctokens = PSCHED_TICKS2NS(hopt->cbuffer);
Eric Dumazet5343a7f2013-06-04 07:11:48 +00001467 cl->mbuffer = 60ULL * NSEC_PER_SEC; /* 1min */
Eric Dumazetd2de8752014-08-22 18:32:09 -07001468 cl->t_c = ktime_get_ns();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 cl->cmode = HTB_CAN_SEND;
1470
1471 /* attach to the hash list and parent's family */
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001472 qdisc_class_hash_insert(&q->clhash, &cl->common);
Patrick McHardy42077592008-07-05 23:22:53 -07001473 if (parent)
1474 parent->children++;
Jiri Kosina49b49972017-03-08 16:03:32 +01001475 if (cl->un.leaf.q != &noop_qdisc)
1476 qdisc_hash_add(cl->un.leaf.q, true);
Patrick McHardyee39e102007-07-02 22:48:13 -07001477 } else {
Stephen Hemminger71bcb092008-11-25 21:13:31 -08001478 if (tca[TCA_RATE]) {
John Fastabend22e0f8b2014-09-28 11:52:56 -07001479 err = gen_replace_estimator(&cl->bstats, NULL,
1480 &cl->rate_est,
Eric Dumazetedb09eb2016-06-06 09:37:16 -07001481 NULL,
1482 qdisc_root_sleeping_running(sch),
Stephen Hemminger71bcb092008-11-25 21:13:31 -08001483 tca[TCA_RATE]);
1484 if (err)
1485 return err;
1486 }
Stephen Hemminger87990462006-08-10 23:35:16 -07001487 sch_tree_lock(sch);
Patrick McHardyee39e102007-07-02 22:48:13 -07001488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Yang Yingliang1598f7c2013-12-10 14:59:28 +08001490 rate64 = tb[TCA_HTB_RATE64] ? nla_get_u64(tb[TCA_HTB_RATE64]) : 0;
1491
1492 ceil64 = tb[TCA_HTB_CEIL64] ? nla_get_u64(tb[TCA_HTB_CEIL64]) : 0;
1493
1494 psched_ratecfg_precompute(&cl->rate, &hopt->rate, rate64);
1495 psched_ratecfg_precompute(&cl->ceil, &hopt->ceil, ceil64);
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 /* it used to be a nasty bug here, we have to check that node
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001498 * is really leaf before changing cl->un.leaf !
1499 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (!cl->level) {
Yang Yingliang1598f7c2013-12-10 14:59:28 +08001501 u64 quantum = cl->rate.rate_bytes_ps;
1502
1503 do_div(quantum, q->rate2quantum);
1504 cl->quantum = min_t(u64, quantum, INT_MAX);
1505
Jarek Poplawskic19f7a32008-12-03 21:09:45 -08001506 if (!hopt->quantum && cl->quantum < 1000) {
Yang Yingliangc17988a2013-12-23 17:38:58 +08001507 pr_warn("HTB: quantum of class %X is small. Consider r2q change.\n",
1508 cl->common.classid);
Jarek Poplawskic19f7a32008-12-03 21:09:45 -08001509 cl->quantum = 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
Jarek Poplawskic19f7a32008-12-03 21:09:45 -08001511 if (!hopt->quantum && cl->quantum > 200000) {
Yang Yingliangc17988a2013-12-23 17:38:58 +08001512 pr_warn("HTB: quantum of class %X is big. Consider r2q change.\n",
1513 cl->common.classid);
Jarek Poplawskic19f7a32008-12-03 21:09:45 -08001514 cl->quantum = 200000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
1516 if (hopt->quantum)
Jarek Poplawskic19f7a32008-12-03 21:09:45 -08001517 cl->quantum = hopt->quantum;
1518 if ((cl->prio = hopt->prio) >= TC_HTB_NUMPRIO)
1519 cl->prio = TC_HTB_NUMPRIO - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
1521
Jiri Pirko324f5aa2013-02-12 00:11:59 +00001522 cl->buffer = PSCHED_TICKS2NS(hopt->buffer);
Vimalkumarf3ad8572013-09-10 17:36:37 -07001523 cl->cbuffer = PSCHED_TICKS2NS(hopt->cbuffer);
Vimalkumar56b765b2012-10-31 06:04:11 +00001524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 sch_tree_unlock(sch);
1526
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001527 qdisc_class_hash_grow(sch, &q->clhash);
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 *arg = (unsigned long)cl;
1530 return 0;
1531
1532failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return err;
1534}
1535
Jiri Pirko6529eab2017-05-17 11:07:55 +02001536static struct tcf_block *htb_tcf_block(struct Qdisc *sch, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
1538 struct htb_sched *q = qdisc_priv(sch);
1539 struct htb_class *cl = (struct htb_class *)arg;
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001540
Jiri Pirko6529eab2017-05-17 11:07:55 +02001541 return cl ? cl->block : q->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
1544static unsigned long htb_bind_filter(struct Qdisc *sch, unsigned long parent,
Stephen Hemminger87990462006-08-10 23:35:16 -07001545 u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Stephen Hemminger87990462006-08-10 23:35:16 -07001547 struct htb_class *cl = htb_find(classid, sch);
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 /*if (cl && !cl->level) return 0;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001550 * The line above used to be there to prevent attaching filters to
1551 * leaves. But at least tc_index filter uses this just to get class
1552 * for other reasons so that we have to allow for it.
1553 * ----
1554 * 19.6.2002 As Werner explained it is ok - bind filter is just
1555 * another way to "lock" the class - unlike "get" this lock can
1556 * be broken by class during destroy IIUC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 */
Stephen Hemminger87990462006-08-10 23:35:16 -07001558 if (cl)
1559 cl->filter_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return (unsigned long)cl;
1561}
1562
1563static void htb_unbind_filter(struct Qdisc *sch, unsigned long arg)
1564{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 struct htb_class *cl = (struct htb_class *)arg;
Stephen Hemminger3bf72952006-08-10 23:31:08 -07001566
Stephen Hemminger87990462006-08-10 23:35:16 -07001567 if (cl)
1568 cl->filter_cnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
1571static void htb_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1572{
1573 struct htb_sched *q = qdisc_priv(sch);
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001574 struct htb_class *cl;
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001575 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 if (arg->stop)
1578 return;
1579
Patrick McHardyf4c1f3e2008-07-05 23:22:35 -07001580 for (i = 0; i < q->clhash.hashsize; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001581 hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (arg->count < arg->skip) {
1583 arg->count++;
1584 continue;
1585 }
1586 if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
1587 arg->stop = 1;
1588 return;
1589 }
1590 arg->count++;
1591 }
1592 }
1593}
1594
Eric Dumazet20fea082007-11-14 01:44:41 -08001595static const struct Qdisc_class_ops htb_class_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 .graft = htb_graft,
1597 .leaf = htb_leaf,
Patrick McHardy256d61b2006-11-29 17:37:05 -08001598 .qlen_notify = htb_qlen_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 .get = htb_get,
1600 .put = htb_put,
1601 .change = htb_change_class,
1602 .delete = htb_delete,
1603 .walk = htb_walk,
Jiri Pirko6529eab2017-05-17 11:07:55 +02001604 .tcf_block = htb_tcf_block,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 .bind_tcf = htb_bind_filter,
1606 .unbind_tcf = htb_unbind_filter,
1607 .dump = htb_dump_class,
1608 .dump_stats = htb_dump_class_stats,
1609};
1610
Eric Dumazet20fea082007-11-14 01:44:41 -08001611static struct Qdisc_ops htb_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 .cl_ops = &htb_class_ops,
1613 .id = "htb",
1614 .priv_size = sizeof(struct htb_sched),
1615 .enqueue = htb_enqueue,
1616 .dequeue = htb_dequeue,
Jarek Poplawski77be1552008-10-31 00:47:01 -07001617 .peek = qdisc_peek_dequeued,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 .init = htb_init,
1619 .reset = htb_reset,
1620 .destroy = htb_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 .dump = htb_dump,
1622 .owner = THIS_MODULE,
1623};
1624
1625static int __init htb_module_init(void)
1626{
Stephen Hemminger87990462006-08-10 23:35:16 -07001627 return register_qdisc(&htb_qdisc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
Stephen Hemminger87990462006-08-10 23:35:16 -07001629static void __exit htb_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Stephen Hemminger87990462006-08-10 23:35:16 -07001631 unregister_qdisc(&htb_qdisc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
Stephen Hemminger87990462006-08-10 23:35:16 -07001633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634module_init(htb_module_init)
1635module_exit(htb_module_exit)
1636MODULE_LICENSE("GPL");