blob: 53d45e029c36d46b9e4f0d1005ef98c468fdd085 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * net/sched/sch_cbq.c Class-Based Queueing discipline.
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/types.h>
11#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/skbuff.h>
Patrick McHardy0ba48052007-07-02 22:49:07 -070015#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <net/pkt_sched.h>
Jiri Pirkocf1facd2017-02-09 14:38:56 +010017#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19
20/* Class-Based Queueing (CBQ) algorithm.
21 =======================================
22
23 Sources: [1] Sally Floyd and Van Jacobson, "Link-sharing and Resource
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090024 Management Models for Packet Networks",
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 IEEE/ACM Transactions on Networking, Vol.3, No.4, 1995
26
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090027 [2] Sally Floyd, "Notes on CBQ and Guaranteed Service", 1995
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090029 [3] Sally Floyd, "Notes on Class-Based Queueing: Setting
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 Parameters", 1996
31
32 [4] Sally Floyd and Michael Speer, "Experimental Results
33 for Class-Based Queueing", 1998, not published.
34
35 -----------------------------------------------------------------------
36
37 Algorithm skeleton was taken from NS simulator cbq.cc.
38 If someone wants to check this code against the LBL version,
39 he should take into account that ONLY the skeleton was borrowed,
40 the implementation is different. Particularly:
41
42 --- The WRR algorithm is different. Our version looks more
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090043 reasonable (I hope) and works when quanta are allowed to be
44 less than MTU, which is always the case when real time classes
45 have small rates. Note, that the statement of [3] is
46 incomplete, delay may actually be estimated even if class
47 per-round allotment is less than MTU. Namely, if per-round
48 allotment is W*r_i, and r_1+...+r_k = r < 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 delay_i <= ([MTU/(W*r_i)]*W*r + W*r + k*MTU)/B
51
52 In the worst case we have IntServ estimate with D = W*r+k*MTU
53 and C = MTU*r. The proof (if correct at all) is trivial.
54
55
56 --- It seems that cbq-2.0 is not very accurate. At least, I cannot
57 interpret some places, which look like wrong translations
58 from NS. Anyone is advised to find these differences
59 and explain to me, why I am wrong 8).
60
61 --- Linux has no EOI event, so that we cannot estimate true class
62 idle time. Workaround is to consider the next dequeue event
63 as sign that previous packet is finished. This is wrong because of
64 internal device queueing, but on a permanently loaded link it is true.
65 Moreover, combined with clock integrator, this scheme looks
66 very close to an ideal solution. */
67
68struct cbq_sched_data;
69
70
Eric Dumazetcc7ec452011-01-19 19:26:56 +000071struct cbq_class {
Patrick McHardyd77fea22008-07-05 23:22:05 -070072 struct Qdisc_class_common common;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 struct cbq_class *next_alive; /* next class with backlog in this priority band */
74
75/* Parameters */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 unsigned char priority; /* class priority */
77 unsigned char priority2; /* priority to be used after overlimit */
78 unsigned char ewma_log; /* time constant for idle time calculation */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 u32 defmap;
81
82 /* Link-sharing scheduler parameters */
83 long maxidle; /* Class parameters: see below. */
84 long offtime;
85 long minidle;
86 u32 avpkt;
87 struct qdisc_rate_table *R_tab;
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 /* General scheduler (WRR) parameters */
90 long allot;
91 long quantum; /* Allotment per WRR round */
92 long weight; /* Relative allotment: see below */
93
94 struct Qdisc *qdisc; /* Ptr to CBQ discipline */
95 struct cbq_class *split; /* Ptr to split node */
96 struct cbq_class *share; /* Ptr to LS parent in the class tree */
97 struct cbq_class *tparent; /* Ptr to tree parent in the class tree */
98 struct cbq_class *borrow; /* NULL if class is bandwidth limited;
99 parent otherwise */
100 struct cbq_class *sibling; /* Sibling chain */
101 struct cbq_class *children; /* Pointer to children chain */
102
103 struct Qdisc *q; /* Elementary queueing discipline */
104
105
106/* Variables */
107 unsigned char cpriority; /* Effective priority */
108 unsigned char delayed;
109 unsigned char level; /* level of the class in hierarchy:
110 0 for leaf classes, and maximal
111 level of children + 1 for nodes.
112 */
113
114 psched_time_t last; /* Last end of service */
115 psched_time_t undertime;
116 long avgidle;
117 long deficit; /* Saved deficit for WRR */
Patrick McHardy1a13cb62007-03-16 01:22:20 -0700118 psched_time_t penalized;
Eric Dumazetc1a8f1f2009-08-16 09:36:49 +0000119 struct gnet_stats_basic_packed bstats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 struct gnet_stats_queue qstats;
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800121 struct net_rate_estimator __rcu *rate_est;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 struct tc_cbq_xstats xstats;
123
John Fastabend25d8c0d2014-09-12 20:05:27 -0700124 struct tcf_proto __rcu *filter_list;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200125 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int filters;
128
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000129 struct cbq_class *defaults[TC_PRIO_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000132struct cbq_sched_data {
Patrick McHardyd77fea22008-07-05 23:22:05 -0700133 struct Qdisc_class_hash clhash; /* Hash table of all classes */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000134 int nclasses[TC_CBQ_MAXPRIO + 1];
135 unsigned int quanta[TC_CBQ_MAXPRIO + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 struct cbq_class link;
138
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000139 unsigned int activemask;
140 struct cbq_class *active[TC_CBQ_MAXPRIO + 1]; /* List of all classes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 with backlog */
142
Patrick McHardyc3bc7cf2007-07-15 00:03:05 -0700143#ifdef CONFIG_NET_CLS_ACT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 struct cbq_class *rx_class;
145#endif
146 struct cbq_class *tx_class;
147 struct cbq_class *tx_borrowed;
148 int tx_len;
149 psched_time_t now; /* Cached timestamp */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000150 unsigned int pmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
David S. Miller2fbd3da2009-09-01 17:59:25 -0700152 struct hrtimer delay_timer;
Patrick McHardy88a99352007-03-16 01:21:11 -0700153 struct qdisc_watchdog watchdog; /* Watchdog timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 started when CBQ has
155 backlog, but cannot
156 transmit just now */
Patrick McHardy88a99352007-03-16 01:21:11 -0700157 psched_tdiff_t wd_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 int toplevel;
159 u32 hgenerator;
160};
161
162
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000163#define L2T(cl, len) qdisc_l2t((cl)->R_tab, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000165static inline struct cbq_class *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166cbq_class_lookup(struct cbq_sched_data *q, u32 classid)
167{
Patrick McHardyd77fea22008-07-05 23:22:05 -0700168 struct Qdisc_class_common *clc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Patrick McHardyd77fea22008-07-05 23:22:05 -0700170 clc = qdisc_class_find(&q->clhash, classid);
171 if (clc == NULL)
172 return NULL;
173 return container_of(clc, struct cbq_class, common);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Patrick McHardyc3bc7cf2007-07-15 00:03:05 -0700176#ifdef CONFIG_NET_CLS_ACT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178static struct cbq_class *
179cbq_reclassify(struct sk_buff *skb, struct cbq_class *this)
180{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000181 struct cbq_class *cl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000183 for (cl = this->tparent; cl; cl = cl->tparent) {
184 struct cbq_class *new = cl->defaults[TC_PRIO_BESTEFFORT];
185
186 if (new != NULL && new != this)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return new;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return NULL;
190}
191
192#endif
193
194/* Classify packet. The procedure is pretty complicated, but
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000195 * it allows us to combine link sharing and priority scheduling
196 * transparently.
197 *
198 * Namely, you can put link sharing rules (f.e. route based) at root of CBQ,
199 * so that it resolves to split nodes. Then packets are classified
200 * by logical priority, or a more specific classifier may be attached
201 * to the split node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 */
203
204static struct cbq_class *
205cbq_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
206{
207 struct cbq_sched_data *q = qdisc_priv(sch);
208 struct cbq_class *head = &q->link;
209 struct cbq_class **defmap;
210 struct cbq_class *cl = NULL;
211 u32 prio = skb->priority;
John Fastabend25d8c0d2014-09-12 20:05:27 -0700212 struct tcf_proto *fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 struct tcf_result res;
214
215 /*
216 * Step 1. If skb->priority points to one of our classes, use it.
217 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000218 if (TC_H_MAJ(prio ^ sch->handle) == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 (cl = cbq_class_lookup(q, prio)) != NULL)
220 return cl;
221
Jarek Poplawskic27f3392008-08-04 22:39:11 -0700222 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 for (;;) {
224 int result = 0;
225 defmap = head->defaults;
226
John Fastabend25d8c0d2014-09-12 20:05:27 -0700227 fl = rcu_dereference_bh(head->filter_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /*
229 * Step 2+n. Apply classifier.
230 */
Jiri Pirko87d83092017-05-17 11:07:54 +0200231 result = tcf_classify(skb, fl, &res, true);
John Fastabend25d8c0d2014-09-12 20:05:27 -0700232 if (!fl || result < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 goto fallback;
234
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000235 cl = (void *)res.class;
236 if (!cl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (TC_H_MAJ(res.classid))
238 cl = cbq_class_lookup(q, res.classid);
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000239 else if ((cl = defmap[res.classid & TC_PRIO_MAX]) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 cl = defmap[TC_PRIO_BESTEFFORT];
241
Eric Dumazetbdfc87f2012-09-11 13:11:12 +0000242 if (cl == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 goto fallback;
244 }
Eric Dumazetbdfc87f2012-09-11 13:11:12 +0000245 if (cl->level >= head->level)
246 goto fallback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247#ifdef CONFIG_NET_CLS_ACT
248 switch (result) {
249 case TC_ACT_QUEUED:
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900250 case TC_ACT_STOLEN:
Jiri Pirkoe25ea212017-06-06 14:12:02 +0200251 case TC_ACT_TRAP:
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700252 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
Gustavo A. R. Silva964201d2020-07-07 12:21:38 -0500253 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 case TC_ACT_SHOT:
255 return NULL;
Patrick McHardy73ca4912007-07-15 00:02:31 -0700256 case TC_ACT_RECLASSIFY:
257 return cbq_reclassify(skb, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259#endif
260 if (cl->level == 0)
261 return cl;
262
263 /*
264 * Step 3+n. If classifier selected a link sharing class,
265 * apply agency specific classifier.
266 * Repeat this procdure until we hit a leaf node.
267 */
268 head = cl;
269 }
270
271fallback:
272 cl = head;
273
274 /*
275 * Step 4. No success...
276 */
277 if (TC_H_MAJ(prio) == 0 &&
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000278 !(cl = head->defaults[prio & TC_PRIO_MAX]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 !(cl = head->defaults[TC_PRIO_BESTEFFORT]))
280 return head;
281
282 return cl;
283}
284
285/*
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000286 * A packet has just been enqueued on the empty class.
287 * cbq_activate_class adds it to the tail of active class list
288 * of its priority band.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 */
290
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000291static inline void cbq_activate_class(struct cbq_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
294 int prio = cl->cpriority;
295 struct cbq_class *cl_tail;
296
297 cl_tail = q->active[prio];
298 q->active[prio] = cl;
299
300 if (cl_tail != NULL) {
301 cl->next_alive = cl_tail->next_alive;
302 cl_tail->next_alive = cl;
303 } else {
304 cl->next_alive = cl;
305 q->activemask |= (1<<prio);
306 }
307}
308
309/*
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000310 * Unlink class from active chain.
311 * Note that this same procedure is done directly in cbq_dequeue*
312 * during round-robin procedure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 */
314
315static void cbq_deactivate_class(struct cbq_class *this)
316{
317 struct cbq_sched_data *q = qdisc_priv(this->qdisc);
318 int prio = this->cpriority;
319 struct cbq_class *cl;
320 struct cbq_class *cl_prev = q->active[prio];
321
322 do {
323 cl = cl_prev->next_alive;
324 if (cl == this) {
325 cl_prev->next_alive = cl->next_alive;
326 cl->next_alive = NULL;
327
328 if (cl == q->active[prio]) {
329 q->active[prio] = cl_prev;
330 if (cl == q->active[prio]) {
331 q->active[prio] = NULL;
332 q->activemask &= ~(1<<prio);
333 return;
334 }
335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return;
337 }
338 } while ((cl_prev = cl) != q->active[prio]);
339}
340
341static void
342cbq_mark_toplevel(struct cbq_sched_data *q, struct cbq_class *cl)
343{
344 int toplevel = q->toplevel;
345
Eric Dumazetcca605d2016-06-10 16:41:37 -0700346 if (toplevel > cl->level) {
Vasily Averin7201c1d2014-08-14 12:27:59 +0400347 psched_time_t now = psched_get_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 do {
Patrick McHardy104e0872007-03-23 11:28:07 -0700350 if (cl->undertime < now) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 q->toplevel = cl->level;
352 return;
353 }
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000354 } while ((cl = cl->borrow) != NULL && toplevel > cl->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356}
357
358static int
Eric Dumazet520ac302016-06-21 23:16:49 -0700359cbq_enqueue(struct sk_buff *skb, struct Qdisc *sch,
360 struct sk_buff **to_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 struct cbq_sched_data *q = qdisc_priv(sch);
Kees Cook3f649ab2020-06-03 13:09:38 -0700363 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct cbq_class *cl = cbq_classify(skb, sch, &ret);
365
Patrick McHardyc3bc7cf2007-07-15 00:03:05 -0700366#ifdef CONFIG_NET_CLS_ACT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 q->rx_class = cl;
368#endif
369 if (cl == NULL) {
Jarek Poplawskic27f3392008-08-04 22:39:11 -0700370 if (ret & __NET_XMIT_BYPASS)
John Fastabend25331d62014-09-28 11:53:29 -0700371 qdisc_qstats_drop(sch);
Eric Dumazet520ac302016-06-21 23:16:49 -0700372 __qdisc_drop(skb, to_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return ret;
374 }
375
Eric Dumazet520ac302016-06-21 23:16:49 -0700376 ret = qdisc_enqueue(skb, cl->q, to_free);
Jussi Kivilinna5f861732008-07-20 00:08:04 -0700377 if (ret == NET_XMIT_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 sch->q.qlen++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 cbq_mark_toplevel(q, cl);
380 if (!cl->next_alive)
381 cbq_activate_class(cl);
382 return ret;
383 }
384
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700385 if (net_xmit_drop_count(ret)) {
John Fastabend25331d62014-09-28 11:53:29 -0700386 qdisc_qstats_drop(sch);
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700387 cbq_mark_toplevel(q, cl);
388 cl->qstats.drops++;
389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return ret;
391}
392
Florian Westphalc3498d32016-06-09 00:27:39 +0200393/* Overlimit action: penalize leaf class by adding offtime */
394static void cbq_overlimit(struct cbq_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
Patrick McHardy8edc0c32007-03-23 11:28:55 -0700397 psched_tdiff_t delay = cl->undertime - q->now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 if (!cl->delayed) {
400 delay += cl->offtime;
401
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900402 /*
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000403 * Class goes to sleep, so that it will have no
404 * chance to work avgidle. Let's forgive it 8)
405 *
406 * BTW cbq-2.0 has a crap in this
407 * place, apparently they forgot to shift it by cl->ewma_log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 */
409 if (cl->avgidle < 0)
410 delay -= (-cl->avgidle) - ((-cl->avgidle) >> cl->ewma_log);
411 if (cl->avgidle < cl->minidle)
412 cl->avgidle = cl->minidle;
413 if (delay <= 0)
414 delay = 1;
Patrick McHardy7c59e252007-03-23 11:27:45 -0700415 cl->undertime = q->now + delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 cl->xstats.overactions++;
418 cl->delayed = 1;
419 }
420 if (q->wd_expires == 0 || q->wd_expires > delay)
421 q->wd_expires = delay;
422
423 /* Dirty work! We must schedule wakeups based on
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000424 * real available rate, rather than leaf rate,
425 * which may be tiny (even zero).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 */
427 if (q->toplevel == TC_CBQ_MAXLEVEL) {
428 struct cbq_class *b;
429 psched_tdiff_t base_delay = q->wd_expires;
430
431 for (b = cl->borrow; b; b = b->borrow) {
Patrick McHardy8edc0c32007-03-23 11:28:55 -0700432 delay = b->undertime - q->now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (delay < base_delay) {
434 if (delay <= 0)
435 delay = 1;
436 base_delay = delay;
437 }
438 }
439
440 q->wd_expires = base_delay;
441 }
442}
443
Patrick McHardy1a13cb62007-03-16 01:22:20 -0700444static psched_tdiff_t cbq_undelay_prio(struct cbq_sched_data *q, int prio,
445 psched_time_t now)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 struct cbq_class *cl;
448 struct cbq_class *cl_prev = q->active[prio];
Patrick McHardy1a13cb62007-03-16 01:22:20 -0700449 psched_time_t sched = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 if (cl_prev == NULL)
Patrick McHardye9054a32007-03-16 01:21:40 -0700452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 do {
455 cl = cl_prev->next_alive;
Patrick McHardy1a13cb62007-03-16 01:22:20 -0700456 if (now - cl->penalized > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 cl_prev->next_alive = cl->next_alive;
458 cl->next_alive = NULL;
459 cl->cpriority = cl->priority;
460 cl->delayed = 0;
461 cbq_activate_class(cl);
462
463 if (cl == q->active[prio]) {
464 q->active[prio] = cl_prev;
465 if (cl == q->active[prio]) {
466 q->active[prio] = NULL;
467 return 0;
468 }
469 }
470
471 cl = cl_prev->next_alive;
Patrick McHardy1a13cb62007-03-16 01:22:20 -0700472 } else if (sched - cl->penalized > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 sched = cl->penalized;
474 } while ((cl_prev = cl) != q->active[prio]);
475
Patrick McHardy1a13cb62007-03-16 01:22:20 -0700476 return sched - now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Patrick McHardy1a13cb62007-03-16 01:22:20 -0700479static enum hrtimer_restart cbq_undelay(struct hrtimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Patrick McHardy1a13cb62007-03-16 01:22:20 -0700481 struct cbq_sched_data *q = container_of(timer, struct cbq_sched_data,
David S. Miller2fbd3da2009-09-01 17:59:25 -0700482 delay_timer);
Patrick McHardy1a13cb62007-03-16 01:22:20 -0700483 struct Qdisc *sch = q->watchdog.qdisc;
484 psched_time_t now;
485 psched_tdiff_t delay = 0;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000486 unsigned int pmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Patrick McHardy3bebcda2007-03-23 11:29:25 -0700488 now = psched_get_time();
Patrick McHardy1a13cb62007-03-16 01:22:20 -0700489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 pmask = q->pmask;
491 q->pmask = 0;
492
493 while (pmask) {
494 int prio = ffz(~pmask);
Patrick McHardy1a13cb62007-03-16 01:22:20 -0700495 psched_tdiff_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 pmask &= ~(1<<prio);
498
Patrick McHardy1a13cb62007-03-16 01:22:20 -0700499 tmp = cbq_undelay_prio(q, prio, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (tmp > 0) {
501 q->pmask |= 1<<prio;
502 if (tmp < delay || delay == 0)
503 delay = tmp;
504 }
505 }
506
507 if (delay) {
Patrick McHardy1a13cb62007-03-16 01:22:20 -0700508 ktime_t time;
509
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100510 time = 0;
Jarek Poplawskica44d6e2009-06-15 02:31:47 -0700511 time = ktime_add_ns(time, PSCHED_TICKS2NS(now + delay));
Eric Dumazet4a8e3202014-09-20 18:01:30 -0700512 hrtimer_start(&q->delay_timer, time, HRTIMER_MODE_ABS_PINNED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
David S. Miller8608db02008-08-18 20:51:18 -0700515 __netif_schedule(qdisc_root(sch));
Patrick McHardy1a13cb62007-03-16 01:22:20 -0700516 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900519/*
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000520 * It is mission critical procedure.
521 *
522 * We "regenerate" toplevel cutoff, if transmitting class
523 * has backlog and it is not regulated. It is not part of
524 * original CBQ description, but looks more reasonable.
525 * Probably, it is wrong. This question needs further investigation.
526 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000528static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529cbq_update_toplevel(struct cbq_sched_data *q, struct cbq_class *cl,
530 struct cbq_class *borrowed)
531{
532 if (cl && q->toplevel >= borrowed->level) {
533 if (cl->q->q.qlen > 1) {
534 do {
Patrick McHardya0849802007-03-23 11:28:30 -0700535 if (borrowed->undertime == PSCHED_PASTPERFECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 q->toplevel = borrowed->level;
537 return;
538 }
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000539 } while ((borrowed = borrowed->borrow) != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900541#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /* It is not necessary now. Uncommenting it
543 will save CPU cycles, but decrease fairness.
544 */
545 q->toplevel = TC_CBQ_MAXLEVEL;
546#endif
547 }
548}
549
550static void
551cbq_update(struct cbq_sched_data *q)
552{
553 struct cbq_class *this = q->tx_class;
554 struct cbq_class *cl = this;
555 int len = q->tx_len;
Vasily Averin73d0f372014-08-14 12:27:47 +0400556 psched_time_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 q->tx_class = NULL;
Vasily Averin73d0f372014-08-14 12:27:47 +0400559 /* Time integrator. We calculate EOS time
560 * by adding expected packet transmission time.
561 */
562 now = q->now + L2T(&q->link, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 for ( ; cl; cl = cl->share) {
565 long avgidle = cl->avgidle;
566 long idle;
567
568 cl->bstats.packets++;
569 cl->bstats.bytes += len;
570
571 /*
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000572 * (now - last) is total time between packet right edges.
573 * (last_pktlen/rate) is "virtual" busy time, so that
574 *
575 * idle = (now - last) - last_pktlen/rate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 */
577
Vasily Averin73d0f372014-08-14 12:27:47 +0400578 idle = now - cl->last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if ((unsigned long)idle > 128*1024*1024) {
580 avgidle = cl->maxidle;
581 } else {
582 idle -= L2T(cl, len);
583
584 /* true_avgidle := (1-W)*true_avgidle + W*idle,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000585 * where W=2^{-ewma_log}. But cl->avgidle is scaled:
586 * cl->avgidle == true_avgidle/W,
587 * hence:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 */
589 avgidle += idle - (avgidle>>cl->ewma_log);
590 }
591
592 if (avgidle <= 0) {
593 /* Overlimit or at-limit */
594
595 if (avgidle < cl->minidle)
596 avgidle = cl->minidle;
597
598 cl->avgidle = avgidle;
599
600 /* Calculate expected time, when this class
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000601 * will be allowed to send.
602 * It will occur, when:
603 * (1-W)*true_avgidle + W*delay = 0, i.e.
604 * idle = (1/W - 1)*(-true_avgidle)
605 * or
606 * idle = (1 - W)*(-cl->avgidle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
608 idle = (-avgidle) - ((-avgidle) >> cl->ewma_log);
609
610 /*
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000611 * That is not all.
612 * To maintain the rate allocated to the class,
613 * we add to undertime virtual clock,
614 * necessary to complete transmitted packet.
615 * (len/phys_bandwidth has been already passed
616 * to the moment of cbq_update)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 */
618
619 idle -= L2T(&q->link, len);
620 idle += L2T(cl, len);
621
Vasily Averin73d0f372014-08-14 12:27:47 +0400622 cl->undertime = now + idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 } else {
624 /* Underlimit */
625
Patrick McHardya0849802007-03-23 11:28:30 -0700626 cl->undertime = PSCHED_PASTPERFECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (avgidle > cl->maxidle)
628 cl->avgidle = cl->maxidle;
629 else
630 cl->avgidle = avgidle;
631 }
Vasily Averin73d0f372014-08-14 12:27:47 +0400632 if ((s64)(now - cl->last) > 0)
633 cl->last = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635
636 cbq_update_toplevel(q, this, q->tx_borrowed);
637}
638
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000639static inline struct cbq_class *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640cbq_under_limit(struct cbq_class *cl)
641{
642 struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
643 struct cbq_class *this_cl = cl;
644
645 if (cl->tparent == NULL)
646 return cl;
647
Patrick McHardya0849802007-03-23 11:28:30 -0700648 if (cl->undertime == PSCHED_PASTPERFECT || q->now >= cl->undertime) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 cl->delayed = 0;
650 return cl;
651 }
652
653 do {
654 /* It is very suspicious place. Now overlimit
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000655 * action is generated for not bounded classes
656 * only if link is completely congested.
657 * Though it is in agree with ancestor-only paradigm,
658 * it looks very stupid. Particularly,
659 * it means that this chunk of code will either
660 * never be called or result in strong amplification
661 * of burstiness. Dangerous, silly, and, however,
662 * no another solution exists.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000664 cl = cl->borrow;
665 if (!cl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 this_cl->qstats.overlimits++;
Florian Westphalc3498d32016-06-09 00:27:39 +0200667 cbq_overlimit(this_cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return NULL;
669 }
670 if (cl->level > q->toplevel)
671 return NULL;
Patrick McHardya0849802007-03-23 11:28:30 -0700672 } while (cl->undertime != PSCHED_PASTPERFECT && q->now < cl->undertime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 cl->delayed = 0;
675 return cl;
676}
677
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000678static inline struct sk_buff *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679cbq_dequeue_prio(struct Qdisc *sch, int prio)
680{
681 struct cbq_sched_data *q = qdisc_priv(sch);
682 struct cbq_class *cl_tail, *cl_prev, *cl;
683 struct sk_buff *skb;
684 int deficit;
685
686 cl_tail = cl_prev = q->active[prio];
687 cl = cl_prev->next_alive;
688
689 do {
690 deficit = 0;
691
692 /* Start round */
693 do {
694 struct cbq_class *borrow = cl;
695
696 if (cl->q->q.qlen &&
697 (borrow = cbq_under_limit(cl)) == NULL)
698 goto skip_class;
699
700 if (cl->deficit <= 0) {
701 /* Class exhausted its allotment per
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000702 * this round. Switch to the next one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 */
704 deficit = 1;
705 cl->deficit += cl->quantum;
706 goto next_class;
707 }
708
709 skb = cl->q->dequeue(cl->q);
710
711 /* Class did not give us any skb :-(
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000712 * It could occur even if cl->q->q.qlen != 0
713 * f.e. if cl->q == "tbf"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 */
715 if (skb == NULL)
716 goto skip_class;
717
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700718 cl->deficit -= qdisc_pkt_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 q->tx_class = cl;
720 q->tx_borrowed = borrow;
721 if (borrow != cl) {
722#ifndef CBQ_XSTATS_BORROWS_BYTES
723 borrow->xstats.borrows++;
724 cl->xstats.borrows++;
725#else
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700726 borrow->xstats.borrows += qdisc_pkt_len(skb);
727 cl->xstats.borrows += qdisc_pkt_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728#endif
729 }
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700730 q->tx_len = qdisc_pkt_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 if (cl->deficit <= 0) {
733 q->active[prio] = cl;
734 cl = cl->next_alive;
735 cl->deficit += cl->quantum;
736 }
737 return skb;
738
739skip_class:
740 if (cl->q->q.qlen == 0 || prio != cl->cpriority) {
741 /* Class is empty or penalized.
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000742 * Unlink it from active chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 */
744 cl_prev->next_alive = cl->next_alive;
745 cl->next_alive = NULL;
746
747 /* Did cl_tail point to it? */
748 if (cl == cl_tail) {
749 /* Repair it! */
750 cl_tail = cl_prev;
751
752 /* Was it the last class in this band? */
753 if (cl == cl_tail) {
754 /* Kill the band! */
755 q->active[prio] = NULL;
756 q->activemask &= ~(1<<prio);
757 if (cl->q->q.qlen)
758 cbq_activate_class(cl);
759 return NULL;
760 }
761
762 q->active[prio] = cl_tail;
763 }
764 if (cl->q->q.qlen)
765 cbq_activate_class(cl);
766
767 cl = cl_prev;
768 }
769
770next_class:
771 cl_prev = cl;
772 cl = cl->next_alive;
773 } while (cl_prev != cl_tail);
774 } while (deficit);
775
776 q->active[prio] = cl_prev;
777
778 return NULL;
779}
780
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000781static inline struct sk_buff *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782cbq_dequeue_1(struct Qdisc *sch)
783{
784 struct cbq_sched_data *q = qdisc_priv(sch);
785 struct sk_buff *skb;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000786 unsigned int activemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000788 activemask = q->activemask & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 while (activemask) {
790 int prio = ffz(~activemask);
791 activemask &= ~(1<<prio);
792 skb = cbq_dequeue_prio(sch, prio);
793 if (skb)
794 return skb;
795 }
796 return NULL;
797}
798
799static struct sk_buff *
800cbq_dequeue(struct Qdisc *sch)
801{
802 struct sk_buff *skb;
803 struct cbq_sched_data *q = qdisc_priv(sch);
804 psched_time_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Patrick McHardy3bebcda2007-03-23 11:29:25 -0700806 now = psched_get_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Vasily Averin73d0f372014-08-14 12:27:47 +0400808 if (q->tx_class)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 cbq_update(q);
Vasily Averin73d0f372014-08-14 12:27:47 +0400810
811 q->now = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 for (;;) {
814 q->wd_expires = 0;
815
816 skb = cbq_dequeue_1(sch);
817 if (skb) {
Eric Dumazet9190b3b2011-01-20 23:31:33 -0800818 qdisc_bstats_update(sch, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 sch->q.qlen--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return skb;
821 }
822
823 /* All the classes are overlimit.
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000824 *
825 * It is possible, if:
826 *
827 * 1. Scheduler is empty.
828 * 2. Toplevel cutoff inhibited borrowing.
829 * 3. Root class is overlimit.
830 *
831 * Reset 2d and 3d conditions and retry.
832 *
833 * Note, that NS and cbq-2.0 are buggy, peeking
834 * an arbitrary class is appropriate for ancestor-only
835 * sharing, but not for toplevel algorithm.
836 *
837 * Our version is better, but slower, because it requires
838 * two passes, but it is unavoidable with top-level sharing.
839 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 if (q->toplevel == TC_CBQ_MAXLEVEL &&
Patrick McHardya0849802007-03-23 11:28:30 -0700842 q->link.undertime == PSCHED_PASTPERFECT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 break;
844
845 q->toplevel = TC_CBQ_MAXLEVEL;
Patrick McHardya0849802007-03-23 11:28:30 -0700846 q->link.undertime = PSCHED_PASTPERFECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848
849 /* No packets in scheduler or nobody wants to give them to us :-(
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000850 * Sigh... start watchdog timer in the last case.
851 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 if (sch->q.qlen) {
John Fastabend25331d62014-09-28 11:53:29 -0700854 qdisc_qstats_overlimit(sch);
Patrick McHardy88a99352007-03-16 01:21:11 -0700855 if (q->wd_expires)
856 qdisc_watchdog_schedule(&q->watchdog,
Patrick McHardybb239ac2007-03-16 12:31:28 -0700857 now + q->wd_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
859 return NULL;
860}
861
862/* CBQ class maintanance routines */
863
864static void cbq_adjust_levels(struct cbq_class *this)
865{
866 if (this == NULL)
867 return;
868
869 do {
870 int level = 0;
871 struct cbq_class *cl;
872
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000873 cl = this->children;
874 if (cl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 do {
876 if (cl->level > level)
877 level = cl->level;
878 } while ((cl = cl->sibling) != this->children);
879 }
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000880 this->level = level + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 } while ((this = this->tparent) != NULL);
882}
883
884static void cbq_normalize_quanta(struct cbq_sched_data *q, int prio)
885{
886 struct cbq_class *cl;
Patrick McHardyd77fea22008-07-05 23:22:05 -0700887 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 if (q->quanta[prio] == 0)
890 return;
891
Patrick McHardyd77fea22008-07-05 23:22:05 -0700892 for (h = 0; h < q->clhash.hashsize; h++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800893 hlist_for_each_entry(cl, &q->clhash.hash[h], common.hnode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /* BUGGGG... Beware! This expression suffer of
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000895 * arithmetic overflows!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 */
897 if (cl->priority == prio) {
898 cl->quantum = (cl->weight*cl->allot*q->nclasses[prio])/
899 q->quanta[prio];
900 }
Yang Yingliang833fa742013-12-10 20:55:32 +0800901 if (cl->quantum <= 0 ||
902 cl->quantum > 32*qdisc_dev(cl->qdisc)->mtu) {
Yang Yingliangc17988a2013-12-23 17:38:58 +0800903 pr_warn("CBQ: class %08x has bad quantum==%ld, repaired.\n",
904 cl->common.classid, cl->quantum);
David S. Miller5ce2d482008-07-08 17:06:30 -0700905 cl->quantum = qdisc_dev(cl->qdisc)->mtu/2 + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907 }
908 }
909}
910
911static void cbq_sync_defmap(struct cbq_class *cl)
912{
913 struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
914 struct cbq_class *split = cl->split;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000915 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 int i;
917
918 if (split == NULL)
919 return;
920
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000921 for (i = 0; i <= TC_PRIO_MAX; i++) {
922 if (split->defaults[i] == cl && !(cl->defmap & (1<<i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 split->defaults[i] = NULL;
924 }
925
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000926 for (i = 0; i <= TC_PRIO_MAX; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int level = split->level;
928
929 if (split->defaults[i])
930 continue;
931
Patrick McHardyd77fea22008-07-05 23:22:05 -0700932 for (h = 0; h < q->clhash.hashsize; h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 struct cbq_class *c;
934
Sasha Levinb67bfe02013-02-27 17:06:00 -0800935 hlist_for_each_entry(c, &q->clhash.hash[h],
Patrick McHardyd77fea22008-07-05 23:22:05 -0700936 common.hnode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (c->split == split && c->level < level &&
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000938 c->defmap & (1<<i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 split->defaults[i] = c;
940 level = c->level;
941 }
942 }
943 }
944 }
945}
946
947static void cbq_change_defmap(struct cbq_class *cl, u32 splitid, u32 def, u32 mask)
948{
949 struct cbq_class *split = NULL;
950
951 if (splitid == 0) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000952 split = cl->split;
953 if (!split)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return;
Patrick McHardyd77fea22008-07-05 23:22:05 -0700955 splitid = split->common.classid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957
Patrick McHardyd77fea22008-07-05 23:22:05 -0700958 if (split == NULL || split->common.classid != splitid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 for (split = cl->tparent; split; split = split->tparent)
Patrick McHardyd77fea22008-07-05 23:22:05 -0700960 if (split->common.classid == splitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 break;
962 }
963
964 if (split == NULL)
965 return;
966
967 if (cl->split != split) {
968 cl->defmap = 0;
969 cbq_sync_defmap(cl);
970 cl->split = split;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000971 cl->defmap = def & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 } else
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000973 cl->defmap = (cl->defmap & ~mask) | (def & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 cbq_sync_defmap(cl);
976}
977
978static void cbq_unlink_class(struct cbq_class *this)
979{
980 struct cbq_class *cl, **clp;
981 struct cbq_sched_data *q = qdisc_priv(this->qdisc);
982
Patrick McHardyd77fea22008-07-05 23:22:05 -0700983 qdisc_class_hash_remove(&q->clhash, &this->common);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 if (this->tparent) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000986 clp = &this->sibling;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 cl = *clp;
988 do {
989 if (cl == this) {
990 *clp = cl->sibling;
991 break;
992 }
993 clp = &cl->sibling;
994 } while ((cl = *clp) != this->sibling);
995
996 if (this->tparent->children == this) {
997 this->tparent->children = this->sibling;
998 if (this->sibling == this)
999 this->tparent->children = NULL;
1000 }
1001 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001002 WARN_ON(this->sibling != this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004}
1005
1006static void cbq_link_class(struct cbq_class *this)
1007{
1008 struct cbq_sched_data *q = qdisc_priv(this->qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 struct cbq_class *parent = this->tparent;
1010
1011 this->sibling = this;
Patrick McHardyd77fea22008-07-05 23:22:05 -07001012 qdisc_class_hash_insert(&q->clhash, &this->common);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 if (parent == NULL)
1015 return;
1016
1017 if (parent->children == NULL) {
1018 parent->children = this;
1019 } else {
1020 this->sibling = parent->children->sibling;
1021 parent->children->sibling = this;
1022 }
1023}
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025static void
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001026cbq_reset(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
1028 struct cbq_sched_data *q = qdisc_priv(sch);
1029 struct cbq_class *cl;
1030 int prio;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001031 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 q->activemask = 0;
1034 q->pmask = 0;
1035 q->tx_class = NULL;
1036 q->tx_borrowed = NULL;
Patrick McHardy88a99352007-03-16 01:21:11 -07001037 qdisc_watchdog_cancel(&q->watchdog);
David S. Miller2fbd3da2009-09-01 17:59:25 -07001038 hrtimer_cancel(&q->delay_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 q->toplevel = TC_CBQ_MAXLEVEL;
Patrick McHardy3bebcda2007-03-23 11:29:25 -07001040 q->now = psched_get_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 for (prio = 0; prio <= TC_CBQ_MAXPRIO; prio++)
1043 q->active[prio] = NULL;
1044
Patrick McHardyd77fea22008-07-05 23:22:05 -07001045 for (h = 0; h < q->clhash.hashsize; h++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001046 hlist_for_each_entry(cl, &q->clhash.hash[h], common.hnode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 qdisc_reset(cl->q);
1048
1049 cl->next_alive = NULL;
Patrick McHardya0849802007-03-23 11:28:30 -07001050 cl->undertime = PSCHED_PASTPERFECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 cl->avgidle = cl->maxidle;
1052 cl->deficit = cl->quantum;
1053 cl->cpriority = cl->priority;
1054 }
1055 }
1056 sch->q.qlen = 0;
1057}
1058
1059
1060static int cbq_set_lss(struct cbq_class *cl, struct tc_cbq_lssopt *lss)
1061{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001062 if (lss->change & TCF_CBQ_LSS_FLAGS) {
1063 cl->share = (lss->flags & TCF_CBQ_LSS_ISOLATED) ? NULL : cl->tparent;
1064 cl->borrow = (lss->flags & TCF_CBQ_LSS_BOUNDED) ? NULL : cl->tparent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001066 if (lss->change & TCF_CBQ_LSS_EWMA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 cl->ewma_log = lss->ewma_log;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001068 if (lss->change & TCF_CBQ_LSS_AVPKT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 cl->avpkt = lss->avpkt;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001070 if (lss->change & TCF_CBQ_LSS_MINIDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 cl->minidle = -(long)lss->minidle;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001072 if (lss->change & TCF_CBQ_LSS_MAXIDLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 cl->maxidle = lss->maxidle;
1074 cl->avgidle = lss->maxidle;
1075 }
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001076 if (lss->change & TCF_CBQ_LSS_OFFTIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 cl->offtime = lss->offtime;
1078 return 0;
1079}
1080
1081static void cbq_rmprio(struct cbq_sched_data *q, struct cbq_class *cl)
1082{
1083 q->nclasses[cl->priority]--;
1084 q->quanta[cl->priority] -= cl->weight;
1085 cbq_normalize_quanta(q, cl->priority);
1086}
1087
1088static void cbq_addprio(struct cbq_sched_data *q, struct cbq_class *cl)
1089{
1090 q->nclasses[cl->priority]++;
1091 q->quanta[cl->priority] += cl->weight;
1092 cbq_normalize_quanta(q, cl->priority);
1093}
1094
1095static int cbq_set_wrr(struct cbq_class *cl, struct tc_cbq_wrropt *wrr)
1096{
1097 struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
1098
1099 if (wrr->allot)
1100 cl->allot = wrr->allot;
1101 if (wrr->weight)
1102 cl->weight = wrr->weight;
1103 if (wrr->priority) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001104 cl->priority = wrr->priority - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 cl->cpriority = cl->priority;
1106 if (cl->priority >= cl->priority2)
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001107 cl->priority2 = TC_CBQ_MAXPRIO - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
1109
1110 cbq_addprio(q, cl);
1111 return 0;
1112}
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114static int cbq_set_fopt(struct cbq_class *cl, struct tc_cbq_fopt *fopt)
1115{
1116 cbq_change_defmap(cl, fopt->split, fopt->defmap, fopt->defchange);
1117 return 0;
1118}
1119
Patrick McHardy27a34212008-01-23 20:35:39 -08001120static const struct nla_policy cbq_policy[TCA_CBQ_MAX + 1] = {
1121 [TCA_CBQ_LSSOPT] = { .len = sizeof(struct tc_cbq_lssopt) },
1122 [TCA_CBQ_WRROPT] = { .len = sizeof(struct tc_cbq_wrropt) },
1123 [TCA_CBQ_FOPT] = { .len = sizeof(struct tc_cbq_fopt) },
1124 [TCA_CBQ_OVL_STRATEGY] = { .len = sizeof(struct tc_cbq_ovl) },
1125 [TCA_CBQ_RATE] = { .len = sizeof(struct tc_ratespec) },
1126 [TCA_CBQ_RTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
1127 [TCA_CBQ_POLICE] = { .len = sizeof(struct tc_cbq_police) },
1128};
1129
Eric Dumazete9789c72019-09-26 18:24:43 -07001130static int cbq_opt_parse(struct nlattr *tb[TCA_CBQ_MAX + 1],
1131 struct nlattr *opt,
1132 struct netlink_ext_ack *extack)
1133{
1134 int err;
1135
1136 if (!opt) {
1137 NL_SET_ERR_MSG(extack, "CBQ options are required for this operation");
1138 return -EINVAL;
1139 }
1140
1141 err = nla_parse_nested_deprecated(tb, TCA_CBQ_MAX, opt,
1142 cbq_policy, extack);
1143 if (err < 0)
1144 return err;
1145
1146 if (tb[TCA_CBQ_WRROPT]) {
1147 const struct tc_cbq_wrropt *wrr = nla_data(tb[TCA_CBQ_WRROPT]);
1148
1149 if (wrr->priority > TC_CBQ_MAXPRIO) {
1150 NL_SET_ERR_MSG(extack, "priority is bigger than TC_CBQ_MAXPRIO");
1151 err = -EINVAL;
1152 }
1153 }
1154 return err;
1155}
1156
Alexander Aringe63d7df2017-12-20 12:35:13 -05001157static int cbq_init(struct Qdisc *sch, struct nlattr *opt,
1158 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
1160 struct cbq_sched_data *q = qdisc_priv(sch);
Patrick McHardy1e904742008-01-22 22:11:17 -08001161 struct nlattr *tb[TCA_CBQ_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 struct tc_ratespec *r;
Patrick McHardycee63722008-01-23 20:33:32 -08001163 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Nikolay Aleksandrov3501d052017-08-30 12:49:01 +03001165 qdisc_watchdog_init(&q->watchdog, sch);
1166 hrtimer_init(&q->delay_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
1167 q->delay_timer.function = cbq_undelay;
1168
Eric Dumazete9789c72019-09-26 18:24:43 -07001169 err = cbq_opt_parse(tb, opt, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001170 if (err < 0)
1171 return err;
1172
Alexander Aring62a6de62017-12-20 12:35:22 -05001173 if (!tb[TCA_CBQ_RTAB] || !tb[TCA_CBQ_RATE]) {
1174 NL_SET_ERR_MSG(extack, "Rate specification missing or incomplete");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return -EINVAL;
Alexander Aring62a6de62017-12-20 12:35:22 -05001176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Patrick McHardy1e904742008-01-22 22:11:17 -08001178 r = nla_data(tb[TCA_CBQ_RATE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Alexander Aringe9bc3fa2017-12-20 12:35:18 -05001180 q->link.R_tab = qdisc_get_rtab(r, tb[TCA_CBQ_RTAB], extack);
Alexander Aringac8ef4a2017-12-20 12:35:11 -05001181 if (!q->link.R_tab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return -EINVAL;
1183
Alexander Aring8d1a77f2017-12-20 12:35:19 -05001184 err = tcf_block_get(&q->link.block, &q->link.filter_list, sch, extack);
Jiri Pirkod51aae62017-11-27 18:37:21 +01001185 if (err)
1186 goto put_rtab;
1187
Patrick McHardyd77fea22008-07-05 23:22:05 -07001188 err = qdisc_class_hash_init(&q->clhash);
1189 if (err < 0)
Jiri Pirkod51aae62017-11-27 18:37:21 +01001190 goto put_block;
Patrick McHardyd77fea22008-07-05 23:22:05 -07001191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 q->link.sibling = &q->link;
Patrick McHardyd77fea22008-07-05 23:22:05 -07001193 q->link.common.classid = sch->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 q->link.qdisc = sch;
Changli Gao3511c912010-10-16 13:04:08 +00001195 q->link.q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
Alexander Aringa38a98822017-12-20 12:35:21 -05001196 sch->handle, NULL);
Changli Gao3511c912010-10-16 13:04:08 +00001197 if (!q->link.q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 q->link.q = &noop_qdisc;
Jiri Kosina49b49972017-03-08 16:03:32 +01001199 else
1200 qdisc_hash_add(q->link.q, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001202 q->link.priority = TC_CBQ_MAXPRIO - 1;
1203 q->link.priority2 = TC_CBQ_MAXPRIO - 1;
1204 q->link.cpriority = TC_CBQ_MAXPRIO - 1;
David S. Miller5ce2d482008-07-08 17:06:30 -07001205 q->link.allot = psched_mtu(qdisc_dev(sch));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 q->link.quantum = q->link.allot;
1207 q->link.weight = q->link.R_tab->rate.rate;
1208
1209 q->link.ewma_log = TC_CBQ_DEF_EWMA;
1210 q->link.avpkt = q->link.allot/2;
1211 q->link.minidle = -0x7FFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 q->toplevel = TC_CBQ_MAXLEVEL;
Patrick McHardy3bebcda2007-03-23 11:29:25 -07001214 q->now = psched_get_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 cbq_link_class(&q->link);
1217
Patrick McHardy1e904742008-01-22 22:11:17 -08001218 if (tb[TCA_CBQ_LSSOPT])
1219 cbq_set_lss(&q->link, nla_data(tb[TCA_CBQ_LSSOPT]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 cbq_addprio(q, &q->link);
1222 return 0;
Patrick McHardyd77fea22008-07-05 23:22:05 -07001223
Jiri Pirkod51aae62017-11-27 18:37:21 +01001224put_block:
1225 tcf_block_put(q->link.block);
1226
Patrick McHardyd77fea22008-07-05 23:22:05 -07001227put_rtab:
1228 qdisc_put_rtab(q->link.R_tab);
1229 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001232static int cbq_dump_rate(struct sk_buff *skb, struct cbq_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001234 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
David S. Miller1b34ec42012-03-29 05:11:39 -04001236 if (nla_put(skb, TCA_CBQ_RATE, sizeof(cl->R_tab->rate), &cl->R_tab->rate))
1237 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return skb->len;
1239
Patrick McHardy1e904742008-01-22 22:11:17 -08001240nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001241 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return -1;
1243}
1244
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001245static int cbq_dump_lss(struct sk_buff *skb, struct cbq_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001247 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 struct tc_cbq_lssopt opt;
1249
1250 opt.flags = 0;
1251 if (cl->borrow == NULL)
1252 opt.flags |= TCF_CBQ_LSS_BOUNDED;
1253 if (cl->share == NULL)
1254 opt.flags |= TCF_CBQ_LSS_ISOLATED;
1255 opt.ewma_log = cl->ewma_log;
1256 opt.level = cl->level;
1257 opt.avpkt = cl->avpkt;
1258 opt.maxidle = cl->maxidle;
1259 opt.minidle = (u32)(-cl->minidle);
1260 opt.offtime = cl->offtime;
1261 opt.change = ~0;
David S. Miller1b34ec42012-03-29 05:11:39 -04001262 if (nla_put(skb, TCA_CBQ_LSSOPT, sizeof(opt), &opt))
1263 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return skb->len;
1265
Patrick McHardy1e904742008-01-22 22:11:17 -08001266nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001267 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return -1;
1269}
1270
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001271static int cbq_dump_wrr(struct sk_buff *skb, struct cbq_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001273 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 struct tc_cbq_wrropt opt;
1275
David S. Millera0db8562013-07-30 00:16:21 -07001276 memset(&opt, 0, sizeof(opt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 opt.flags = 0;
1278 opt.allot = cl->allot;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001279 opt.priority = cl->priority + 1;
1280 opt.cpriority = cl->cpriority + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 opt.weight = cl->weight;
David S. Miller1b34ec42012-03-29 05:11:39 -04001282 if (nla_put(skb, TCA_CBQ_WRROPT, sizeof(opt), &opt))
1283 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return skb->len;
1285
Patrick McHardy1e904742008-01-22 22:11:17 -08001286nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001287 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return -1;
1289}
1290
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001291static int cbq_dump_fopt(struct sk_buff *skb, struct cbq_class *cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001293 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 struct tc_cbq_fopt opt;
1295
1296 if (cl->split || cl->defmap) {
Patrick McHardyd77fea22008-07-05 23:22:05 -07001297 opt.split = cl->split ? cl->split->common.classid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 opt.defmap = cl->defmap;
1299 opt.defchange = ~0;
David S. Miller1b34ec42012-03-29 05:11:39 -04001300 if (nla_put(skb, TCA_CBQ_FOPT, sizeof(opt), &opt))
1301 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
1303 return skb->len;
1304
Patrick McHardy1e904742008-01-22 22:11:17 -08001305nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001306 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return -1;
1308}
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310static int cbq_dump_attr(struct sk_buff *skb, struct cbq_class *cl)
1311{
1312 if (cbq_dump_lss(skb, cl) < 0 ||
1313 cbq_dump_rate(skb, cl) < 0 ||
1314 cbq_dump_wrr(skb, cl) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 cbq_dump_fopt(skb, cl) < 0)
1316 return -1;
1317 return 0;
1318}
1319
1320static int cbq_dump(struct Qdisc *sch, struct sk_buff *skb)
1321{
1322 struct cbq_sched_data *q = qdisc_priv(sch);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001323 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001325 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001326 if (nest == NULL)
1327 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (cbq_dump_attr(skb, &q->link) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001329 goto nla_put_failure;
Yang Yingliangd59b7d82014-03-12 10:20:32 +08001330 return nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Patrick McHardy1e904742008-01-22 22:11:17 -08001332nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001333 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return -1;
1335}
1336
1337static int
1338cbq_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
1339{
1340 struct cbq_sched_data *q = qdisc_priv(sch);
1341
1342 q->link.xstats.avgidle = q->link.avgidle;
1343 return gnet_stats_copy_app(d, &q->link.xstats, sizeof(q->link.xstats));
1344}
1345
1346static int
1347cbq_dump_class(struct Qdisc *sch, unsigned long arg,
1348 struct sk_buff *skb, struct tcmsg *tcm)
1349{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001350 struct cbq_class *cl = (struct cbq_class *)arg;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001351 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 if (cl->tparent)
Patrick McHardyd77fea22008-07-05 23:22:05 -07001354 tcm->tcm_parent = cl->tparent->common.classid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 else
1356 tcm->tcm_parent = TC_H_ROOT;
Patrick McHardyd77fea22008-07-05 23:22:05 -07001357 tcm->tcm_handle = cl->common.classid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 tcm->tcm_info = cl->q->handle;
1359
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001360 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001361 if (nest == NULL)
1362 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (cbq_dump_attr(skb, cl) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001364 goto nla_put_failure;
Yang Yingliangd59b7d82014-03-12 10:20:32 +08001365 return nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Patrick McHardy1e904742008-01-22 22:11:17 -08001367nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001368 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return -1;
1370}
1371
1372static int
1373cbq_dump_class_stats(struct Qdisc *sch, unsigned long arg,
1374 struct gnet_dump *d)
1375{
1376 struct cbq_sched_data *q = qdisc_priv(sch);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001377 struct cbq_class *cl = (struct cbq_class *)arg;
Paolo Abeni5dd431b2019-03-28 16:53:12 +01001378 __u32 qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 cl->xstats.avgidle = cl->avgidle;
1381 cl->xstats.undertime = 0;
Paolo Abeni5dd431b2019-03-28 16:53:12 +01001382 qdisc_qstats_qlen_backlog(cl->q, &qlen, &cl->qstats.backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Patrick McHardya0849802007-03-23 11:28:30 -07001384 if (cl->undertime != PSCHED_PASTPERFECT)
Patrick McHardy8edc0c32007-03-23 11:28:55 -07001385 cl->xstats.undertime = cl->undertime - q->now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Eric Dumazetedb09eb2016-06-06 09:37:16 -07001387 if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
1388 d, NULL, &cl->bstats) < 0 ||
Eric Dumazet1c0d32f2016-12-04 09:48:16 -08001389 gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
Paolo Abeni5dd431b2019-03-28 16:53:12 +01001390 gnet_stats_copy_queue(d, NULL, &cl->qstats, qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return -1;
1392
1393 return gnet_stats_copy_app(d, &cl->xstats, sizeof(cl->xstats));
1394}
1395
1396static int cbq_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
Alexander Aring653d6fd2017-12-20 12:35:17 -05001397 struct Qdisc **old, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001399 struct cbq_class *cl = (struct cbq_class *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Patrick McHardy5b9a9cc2009-09-04 06:41:17 +00001401 if (new == NULL) {
Alexander Aringa38a98822017-12-20 12:35:21 -05001402 new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
1403 cl->common.classid, extack);
Patrick McHardy5b9a9cc2009-09-04 06:41:17 +00001404 if (new == NULL)
1405 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
Patrick McHardy5b9a9cc2009-09-04 06:41:17 +00001407
WANG Cong86a79962016-02-25 14:55:00 -08001408 *old = qdisc_replace(sch, new, &cl->q);
Patrick McHardy5b9a9cc2009-09-04 06:41:17 +00001409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001412static struct Qdisc *cbq_leaf(struct Qdisc *sch, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001414 struct cbq_class *cl = (struct cbq_class *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Patrick McHardy5b9a9cc2009-09-04 06:41:17 +00001416 return cl->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
Jarek Poplawskia37ef2e32006-12-08 00:25:55 -08001419static void cbq_qlen_notify(struct Qdisc *sch, unsigned long arg)
1420{
1421 struct cbq_class *cl = (struct cbq_class *)arg;
1422
Konstantin Khlebnikov95946652017-08-15 16:39:59 +03001423 cbq_deactivate_class(cl);
Jarek Poplawskia37ef2e32006-12-08 00:25:55 -08001424}
1425
WANG Cong143976c2017-08-24 16:51:29 -07001426static unsigned long cbq_find(struct Qdisc *sch, u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 struct cbq_sched_data *q = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
WANG Cong143976c2017-08-24 16:51:29 -07001430 return (unsigned long)cbq_class_lookup(q, classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433static void cbq_destroy_class(struct Qdisc *sch, struct cbq_class *cl)
1434{
1435 struct cbq_sched_data *q = qdisc_priv(sch);
1436
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001437 WARN_ON(cl->filters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Jiri Pirko6529eab2017-05-17 11:07:55 +02001439 tcf_block_put(cl->block);
Vlad Buslov86bd4462018-09-24 19:22:50 +03001440 qdisc_put(cl->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 qdisc_put_rtab(cl->R_tab);
Eric Dumazet1c0d32f2016-12-04 09:48:16 -08001442 gen_kill_estimator(&cl->rate_est);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (cl != &q->link)
1444 kfree(cl);
1445}
1446
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001447static void cbq_destroy(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
1449 struct cbq_sched_data *q = qdisc_priv(sch);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001450 struct hlist_node *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 struct cbq_class *cl;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001452 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Patrick McHardyc3bc7cf2007-07-15 00:03:05 -07001454#ifdef CONFIG_NET_CLS_ACT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 q->rx_class = NULL;
1456#endif
1457 /*
1458 * Filters must be destroyed first because we don't destroy the
1459 * classes from root to leafs which means that filters can still
1460 * be bound to classes which have been destroyed already. --TGR '04
1461 */
Patrick McHardyd77fea22008-07-05 23:22:05 -07001462 for (h = 0; h < q->clhash.hashsize; h++) {
Konstantin Khlebnikov89890422017-08-15 16:35:21 +03001463 hlist_for_each_entry(cl, &q->clhash.hash[h], common.hnode) {
Jiri Pirko6529eab2017-05-17 11:07:55 +02001464 tcf_block_put(cl->block);
Konstantin Khlebnikov89890422017-08-15 16:35:21 +03001465 cl->block = NULL;
1466 }
Patrick McHardyb00b4bf2007-06-05 16:06:59 -07001467 }
Patrick McHardyd77fea22008-07-05 23:22:05 -07001468 for (h = 0; h < q->clhash.hashsize; h++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001469 hlist_for_each_entry_safe(cl, next, &q->clhash.hash[h],
Patrick McHardyd77fea22008-07-05 23:22:05 -07001470 common.hnode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 cbq_destroy_class(sch, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
Patrick McHardyd77fea22008-07-05 23:22:05 -07001473 qdisc_class_hash_destroy(&q->clhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476static int
Patrick McHardy1e904742008-01-22 22:11:17 -08001477cbq_change_class(struct Qdisc *sch, u32 classid, u32 parentid, struct nlattr **tca,
Alexander Aring793d81d2017-12-20 12:35:15 -05001478 unsigned long *arg, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
1480 int err;
1481 struct cbq_sched_data *q = qdisc_priv(sch);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001482 struct cbq_class *cl = (struct cbq_class *)*arg;
Patrick McHardy1e904742008-01-22 22:11:17 -08001483 struct nlattr *opt = tca[TCA_OPTIONS];
1484 struct nlattr *tb[TCA_CBQ_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 struct cbq_class *parent;
1486 struct qdisc_rate_table *rtab = NULL;
1487
Eric Dumazete9789c72019-09-26 18:24:43 -07001488 err = cbq_opt_parse(tb, opt, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001489 if (err < 0)
1490 return err;
1491
Alexander Aring62a6de62017-12-20 12:35:22 -05001492 if (tb[TCA_CBQ_OVL_STRATEGY] || tb[TCA_CBQ_POLICE]) {
1493 NL_SET_ERR_MSG(extack, "Neither overlimit strategy nor policing attributes can be used for changing class params");
Florian Westphalc3498d32016-06-09 00:27:39 +02001494 return -EOPNOTSUPP;
Alexander Aring62a6de62017-12-20 12:35:22 -05001495 }
Florian Westphalc3498d32016-06-09 00:27:39 +02001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (cl) {
1498 /* Check parent */
1499 if (parentid) {
Patrick McHardyd77fea22008-07-05 23:22:05 -07001500 if (cl->tparent &&
Alexander Aring62a6de62017-12-20 12:35:22 -05001501 cl->tparent->common.classid != parentid) {
1502 NL_SET_ERR_MSG(extack, "Invalid parent id");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return -EINVAL;
Alexander Aring62a6de62017-12-20 12:35:22 -05001504 }
1505 if (!cl->tparent && parentid != TC_H_ROOT) {
1506 NL_SET_ERR_MSG(extack, "Parent must be root");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return -EINVAL;
Alexander Aring62a6de62017-12-20 12:35:22 -05001508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
1510
Patrick McHardy1e904742008-01-22 22:11:17 -08001511 if (tb[TCA_CBQ_RATE]) {
Stephen Hemminger71bcb092008-11-25 21:13:31 -08001512 rtab = qdisc_get_rtab(nla_data(tb[TCA_CBQ_RATE]),
Alexander Aringe9bc3fa2017-12-20 12:35:18 -05001513 tb[TCA_CBQ_RTAB], extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (rtab == NULL)
1515 return -EINVAL;
1516 }
1517
Stephen Hemminger71bcb092008-11-25 21:13:31 -08001518 if (tca[TCA_RATE]) {
John Fastabend22e0f8b2014-09-28 11:52:56 -07001519 err = gen_replace_estimator(&cl->bstats, NULL,
1520 &cl->rate_est,
Eric Dumazetedb09eb2016-06-06 09:37:16 -07001521 NULL,
1522 qdisc_root_sleeping_running(sch),
Stephen Hemminger71bcb092008-11-25 21:13:31 -08001523 tca[TCA_RATE]);
1524 if (err) {
Alexander Aring62a6de62017-12-20 12:35:22 -05001525 NL_SET_ERR_MSG(extack, "Failed to replace specified rate estimator");
Yang Yingliang79c11f22013-12-17 15:29:17 +08001526 qdisc_put_rtab(rtab);
Stephen Hemminger71bcb092008-11-25 21:13:31 -08001527 return err;
1528 }
1529 }
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 /* Change class parameters */
1532 sch_tree_lock(sch);
1533
1534 if (cl->next_alive != NULL)
1535 cbq_deactivate_class(cl);
1536
1537 if (rtab) {
Patrick McHardyb94c8af2008-11-20 04:11:36 -08001538 qdisc_put_rtab(cl->R_tab);
1539 cl->R_tab = rtab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
1541
Patrick McHardy1e904742008-01-22 22:11:17 -08001542 if (tb[TCA_CBQ_LSSOPT])
1543 cbq_set_lss(cl, nla_data(tb[TCA_CBQ_LSSOPT]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Patrick McHardy1e904742008-01-22 22:11:17 -08001545 if (tb[TCA_CBQ_WRROPT]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 cbq_rmprio(q, cl);
Patrick McHardy1e904742008-01-22 22:11:17 -08001547 cbq_set_wrr(cl, nla_data(tb[TCA_CBQ_WRROPT]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549
Patrick McHardy1e904742008-01-22 22:11:17 -08001550 if (tb[TCA_CBQ_FOPT])
1551 cbq_set_fopt(cl, nla_data(tb[TCA_CBQ_FOPT]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 if (cl->q->q.qlen)
1554 cbq_activate_class(cl);
1555
1556 sch_tree_unlock(sch);
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return 0;
1559 }
1560
1561 if (parentid == TC_H_ROOT)
1562 return -EINVAL;
1563
Alexander Aring62a6de62017-12-20 12:35:22 -05001564 if (!tb[TCA_CBQ_WRROPT] || !tb[TCA_CBQ_RATE] || !tb[TCA_CBQ_LSSOPT]) {
1565 NL_SET_ERR_MSG(extack, "One of the following attributes MUST be specified: WRR, rate or link sharing");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return -EINVAL;
Alexander Aring62a6de62017-12-20 12:35:22 -05001567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Alexander Aringe9bc3fa2017-12-20 12:35:18 -05001569 rtab = qdisc_get_rtab(nla_data(tb[TCA_CBQ_RATE]), tb[TCA_CBQ_RTAB],
1570 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (rtab == NULL)
1572 return -EINVAL;
1573
1574 if (classid) {
1575 err = -EINVAL;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001576 if (TC_H_MAJ(classid ^ sch->handle) ||
Alexander Aring62a6de62017-12-20 12:35:22 -05001577 cbq_class_lookup(q, classid)) {
1578 NL_SET_ERR_MSG(extack, "Specified class not found");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 goto failure;
Alexander Aring62a6de62017-12-20 12:35:22 -05001580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 } else {
1582 int i;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001583 classid = TC_H_MAKE(sch->handle, 0x8000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001585 for (i = 0; i < 0x8000; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (++q->hgenerator >= 0x8000)
1587 q->hgenerator = 1;
1588 if (cbq_class_lookup(q, classid|q->hgenerator) == NULL)
1589 break;
1590 }
1591 err = -ENOSR;
Alexander Aring62a6de62017-12-20 12:35:22 -05001592 if (i >= 0x8000) {
1593 NL_SET_ERR_MSG(extack, "Unable to generate classid");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 goto failure;
Alexander Aring62a6de62017-12-20 12:35:22 -05001595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 classid = classid|q->hgenerator;
1597 }
1598
1599 parent = &q->link;
1600 if (parentid) {
1601 parent = cbq_class_lookup(q, parentid);
1602 err = -EINVAL;
Alexander Aring62a6de62017-12-20 12:35:22 -05001603 if (!parent) {
1604 NL_SET_ERR_MSG(extack, "Failed to find parentid");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 goto failure;
Alexander Aring62a6de62017-12-20 12:35:22 -05001606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
1608
1609 err = -ENOBUFS;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001610 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (cl == NULL)
1612 goto failure;
Stephen Hemminger71bcb092008-11-25 21:13:31 -08001613
Alexander Aring8d1a77f2017-12-20 12:35:19 -05001614 err = tcf_block_get(&cl->block, &cl->filter_list, sch, extack);
Jiri Pirko6529eab2017-05-17 11:07:55 +02001615 if (err) {
1616 kfree(cl);
1617 return err;
1618 }
1619
Stephen Hemminger71bcb092008-11-25 21:13:31 -08001620 if (tca[TCA_RATE]) {
John Fastabend22e0f8b2014-09-28 11:52:56 -07001621 err = gen_new_estimator(&cl->bstats, NULL, &cl->rate_est,
Eric Dumazetedb09eb2016-06-06 09:37:16 -07001622 NULL,
1623 qdisc_root_sleeping_running(sch),
Stephen Hemminger71bcb092008-11-25 21:13:31 -08001624 tca[TCA_RATE]);
1625 if (err) {
Alexander Aring62a6de62017-12-20 12:35:22 -05001626 NL_SET_ERR_MSG(extack, "Couldn't create new estimator");
Jiri Pirko6529eab2017-05-17 11:07:55 +02001627 tcf_block_put(cl->block);
Stephen Hemminger71bcb092008-11-25 21:13:31 -08001628 kfree(cl);
1629 goto failure;
1630 }
1631 }
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 cl->R_tab = rtab;
1634 rtab = NULL;
Alexander Aringa38a98822017-12-20 12:35:21 -05001635 cl->q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops, classid,
1636 NULL);
Changli Gao3511c912010-10-16 13:04:08 +00001637 if (!cl->q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 cl->q = &noop_qdisc;
Jiri Kosina49b49972017-03-08 16:03:32 +01001639 else
1640 qdisc_hash_add(cl->q, true);
1641
Patrick McHardyd77fea22008-07-05 23:22:05 -07001642 cl->common.classid = classid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 cl->tparent = parent;
1644 cl->qdisc = sch;
1645 cl->allot = parent->allot;
1646 cl->quantum = cl->allot;
1647 cl->weight = cl->R_tab->rate.rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 sch_tree_lock(sch);
1650 cbq_link_class(cl);
1651 cl->borrow = cl->tparent;
1652 if (cl->tparent != &q->link)
1653 cl->share = cl->tparent;
1654 cbq_adjust_levels(parent);
1655 cl->minidle = -0x7FFFFFFF;
Patrick McHardy1e904742008-01-22 22:11:17 -08001656 cbq_set_lss(cl, nla_data(tb[TCA_CBQ_LSSOPT]));
1657 cbq_set_wrr(cl, nla_data(tb[TCA_CBQ_WRROPT]));
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001658 if (cl->ewma_log == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 cl->ewma_log = q->link.ewma_log;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001660 if (cl->maxidle == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 cl->maxidle = q->link.maxidle;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001662 if (cl->avpkt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 cl->avpkt = q->link.avpkt;
Patrick McHardy1e904742008-01-22 22:11:17 -08001664 if (tb[TCA_CBQ_FOPT])
1665 cbq_set_fopt(cl, nla_data(tb[TCA_CBQ_FOPT]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 sch_tree_unlock(sch);
1667
Patrick McHardyd77fea22008-07-05 23:22:05 -07001668 qdisc_class_hash_grow(sch, &q->clhash);
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 *arg = (unsigned long)cl;
1671 return 0;
1672
1673failure:
1674 qdisc_put_rtab(rtab);
1675 return err;
1676}
1677
1678static int cbq_delete(struct Qdisc *sch, unsigned long arg)
1679{
1680 struct cbq_sched_data *q = qdisc_priv(sch);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001681 struct cbq_class *cl = (struct cbq_class *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 if (cl->filters || cl->children || cl == &q->link)
1684 return -EBUSY;
1685
1686 sch_tree_lock(sch);
1687
Paolo Abenie5f0e8f2019-03-28 16:53:13 +01001688 qdisc_purge_queue(cl->q);
Jarek Poplawskia37ef2e32006-12-08 00:25:55 -08001689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (cl->next_alive)
1691 cbq_deactivate_class(cl);
1692
1693 if (q->tx_borrowed == cl)
1694 q->tx_borrowed = q->tx_class;
1695 if (q->tx_class == cl) {
1696 q->tx_class = NULL;
1697 q->tx_borrowed = NULL;
1698 }
Patrick McHardyc3bc7cf2007-07-15 00:03:05 -07001699#ifdef CONFIG_NET_CLS_ACT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (q->rx_class == cl)
1701 q->rx_class = NULL;
1702#endif
1703
1704 cbq_unlink_class(cl);
1705 cbq_adjust_levels(cl->tparent);
1706 cl->defmap = 0;
1707 cbq_sync_defmap(cl);
1708
1709 cbq_rmprio(q, cl);
1710 sch_tree_unlock(sch);
1711
WANG Cong143976c2017-08-24 16:51:29 -07001712 cbq_destroy_class(sch, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 return 0;
1714}
1715
Alexander Aringcbaacc42017-12-20 12:35:16 -05001716static struct tcf_block *cbq_tcf_block(struct Qdisc *sch, unsigned long arg,
1717 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
1719 struct cbq_sched_data *q = qdisc_priv(sch);
1720 struct cbq_class *cl = (struct cbq_class *)arg;
1721
1722 if (cl == NULL)
1723 cl = &q->link;
1724
Jiri Pirko6529eab2017-05-17 11:07:55 +02001725 return cl->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
1728static unsigned long cbq_bind_filter(struct Qdisc *sch, unsigned long parent,
1729 u32 classid)
1730{
1731 struct cbq_sched_data *q = qdisc_priv(sch);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001732 struct cbq_class *p = (struct cbq_class *)parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 struct cbq_class *cl = cbq_class_lookup(q, classid);
1734
1735 if (cl) {
1736 if (p && p->level <= cl->level)
1737 return 0;
1738 cl->filters++;
1739 return (unsigned long)cl;
1740 }
1741 return 0;
1742}
1743
1744static void cbq_unbind_filter(struct Qdisc *sch, unsigned long arg)
1745{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001746 struct cbq_class *cl = (struct cbq_class *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 cl->filters--;
1749}
1750
1751static void cbq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1752{
1753 struct cbq_sched_data *q = qdisc_priv(sch);
Patrick McHardyd77fea22008-07-05 23:22:05 -07001754 struct cbq_class *cl;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001755 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 if (arg->stop)
1758 return;
1759
Patrick McHardyd77fea22008-07-05 23:22:05 -07001760 for (h = 0; h < q->clhash.hashsize; h++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001761 hlist_for_each_entry(cl, &q->clhash.hash[h], common.hnode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 if (arg->count < arg->skip) {
1763 arg->count++;
1764 continue;
1765 }
1766 if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
1767 arg->stop = 1;
1768 return;
1769 }
1770 arg->count++;
1771 }
1772 }
1773}
1774
Eric Dumazet20fea082007-11-14 01:44:41 -08001775static const struct Qdisc_class_ops cbq_class_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 .graft = cbq_graft,
1777 .leaf = cbq_leaf,
Jarek Poplawskia37ef2e32006-12-08 00:25:55 -08001778 .qlen_notify = cbq_qlen_notify,
WANG Cong143976c2017-08-24 16:51:29 -07001779 .find = cbq_find,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 .change = cbq_change_class,
1781 .delete = cbq_delete,
1782 .walk = cbq_walk,
Jiri Pirko6529eab2017-05-17 11:07:55 +02001783 .tcf_block = cbq_tcf_block,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 .bind_tcf = cbq_bind_filter,
1785 .unbind_tcf = cbq_unbind_filter,
1786 .dump = cbq_dump_class,
1787 .dump_stats = cbq_dump_class_stats,
1788};
1789
Eric Dumazet20fea082007-11-14 01:44:41 -08001790static struct Qdisc_ops cbq_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 .next = NULL,
1792 .cl_ops = &cbq_class_ops,
1793 .id = "cbq",
1794 .priv_size = sizeof(struct cbq_sched_data),
1795 .enqueue = cbq_enqueue,
1796 .dequeue = cbq_dequeue,
Jarek Poplawski77be1552008-10-31 00:47:01 -07001797 .peek = qdisc_peek_dequeued,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 .init = cbq_init,
1799 .reset = cbq_reset,
1800 .destroy = cbq_destroy,
1801 .change = NULL,
1802 .dump = cbq_dump,
1803 .dump_stats = cbq_dump_stats,
1804 .owner = THIS_MODULE,
1805};
1806
1807static int __init cbq_module_init(void)
1808{
1809 return register_qdisc(&cbq_qdisc_ops);
1810}
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001811static void __exit cbq_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
1813 unregister_qdisc(&cbq_qdisc_ops);
1814}
1815module_init(cbq_module_init)
1816module_exit(cbq_module_exit)
1817MODULE_LICENSE("GPL");