blob: 6329d7d0d3340b91bba315d8f89864538ca2763f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2003 Patrick McHardy, <kaber@trash.net>
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 2
7 * of the License, or (at your option) any later version.
8 *
9 * 2003-10-17 - Ported from altq
10 */
11/*
12 * Copyright (c) 1997-1999 Carnegie Mellon University. All Rights Reserved.
13 *
14 * Permission to use, copy, modify, and distribute this software and
15 * its documentation is hereby granted (including for commercial or
16 * for-profit use), provided that both the copyright notice and this
17 * permission notice appear in all copies of the software, derivative
18 * works, or modified versions, and any portions thereof.
19 *
20 * THIS SOFTWARE IS EXPERIMENTAL AND IS KNOWN TO HAVE BUGS, SOME OF
21 * WHICH MAY HAVE SERIOUS CONSEQUENCES. CARNEGIE MELLON PROVIDES THIS
22 * SOFTWARE IN ITS ``AS IS'' CONDITION, AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
28 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
33 * DAMAGE.
34 *
35 * Carnegie Mellon encourages (but does not require) users of this
36 * software to return any improvements or extensions that they make,
37 * and to grant Carnegie Mellon the rights to redistribute these
38 * changes without encumbrance.
39 */
40/*
41 * H-FSC is described in Proceedings of SIGCOMM'97,
42 * "A Hierarchical Fair Service Curve Algorithm for Link-Sharing,
43 * Real-Time and Priority Service"
44 * by Ion Stoica, Hui Zhang, and T. S. Eugene Ng.
45 *
46 * Oleg Cherevko <olwi@aq.ml.com.ua> added the upperlimit for link-sharing.
47 * when a class has an upperlimit, the fit-time is computed from the
48 * upperlimit service curve. the link-sharing scheduler does not schedule
49 * a class whose fit-time exceeds the current time.
50 */
51
52#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/module.h>
54#include <linux/types.h>
55#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/compiler.h>
57#include <linux/spinlock.h>
58#include <linux/skbuff.h>
59#include <linux/string.h>
60#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/list.h>
62#include <linux/rbtree.h>
63#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/rtnetlink.h>
65#include <linux/pkt_sched.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070066#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <net/pkt_sched.h>
68#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <asm/div64.h>
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/*
72 * kernel internal service curve representation:
73 * coordinates are given by 64 bit unsigned integers.
74 * x-axis: unit is clock count.
75 * y-axis: unit is byte.
76 *
77 * The service curve parameters are converted to the internal
78 * representation. The slope values are scaled to avoid overflow.
79 * the inverse slope values as well as the y-projection of the 1st
Anand Gadiyarfd589a82009-07-16 17:13:03 +020080 * segment are kept in order to avoid 64-bit divide operations
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * that are expensive on 32-bit architectures.
82 */
83
Eric Dumazetcc7ec452011-01-19 19:26:56 +000084struct internal_sc {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 u64 sm1; /* scaled slope of the 1st segment */
86 u64 ism1; /* scaled inverse-slope of the 1st segment */
87 u64 dx; /* the x-projection of the 1st segment */
88 u64 dy; /* the y-projection of the 1st segment */
89 u64 sm2; /* scaled slope of the 2nd segment */
90 u64 ism2; /* scaled inverse-slope of the 2nd segment */
91};
92
93/* runtime service curve */
Eric Dumazetcc7ec452011-01-19 19:26:56 +000094struct runtime_sc {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 u64 x; /* current starting position on x-axis */
96 u64 y; /* current starting position on y-axis */
97 u64 sm1; /* scaled slope of the 1st segment */
98 u64 ism1; /* scaled inverse-slope of the 1st segment */
99 u64 dx; /* the x-projection of the 1st segment */
100 u64 dy; /* the y-projection of the 1st segment */
101 u64 sm2; /* scaled slope of the 2nd segment */
102 u64 ism2; /* scaled inverse-slope of the 2nd segment */
103};
104
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000105enum hfsc_class_flags {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 HFSC_RSC = 0x1,
107 HFSC_FSC = 0x2,
108 HFSC_USC = 0x4
109};
110
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000111struct hfsc_class {
Patrick McHardybe0d39d2008-07-05 23:21:47 -0700112 struct Qdisc_class_common cl_common;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 unsigned int refcnt; /* usage count */
114
Eric Dumazetc1a8f1f2009-08-16 09:36:49 +0000115 struct gnet_stats_basic_packed bstats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 struct gnet_stats_queue qstats;
Eric Dumazet45203a32013-06-06 08:43:22 -0700117 struct gnet_stats_rate_est64 rate_est;
John Fastabend25d8c0d2014-09-12 20:05:27 -0700118 struct tcf_proto __rcu *filter_list; /* filter list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 unsigned int filter_cnt; /* filter count */
Florian Westphalbba7eb52016-07-04 16:22:20 +0200120 unsigned int level; /* class level in hierarchy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 struct hfsc_sched *sched; /* scheduler data */
123 struct hfsc_class *cl_parent; /* parent class */
124 struct list_head siblings; /* sibling classes */
125 struct list_head children; /* child classes */
126 struct Qdisc *qdisc; /* leaf qdisc */
127
128 struct rb_node el_node; /* qdisc's eligible tree member */
129 struct rb_root vt_tree; /* active children sorted by cl_vt */
130 struct rb_node vt_node; /* parent's vt_tree member */
131 struct rb_root cf_tree; /* active children sorted by cl_f */
132 struct rb_node cf_node; /* parent's cf_heap member */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 u64 cl_total; /* total work in bytes */
135 u64 cl_cumul; /* cumulative work in bytes done by
136 real-time criteria */
137
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000138 u64 cl_d; /* deadline*/
139 u64 cl_e; /* eligible time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 u64 cl_vt; /* virtual time */
141 u64 cl_f; /* time when this class will fit for
142 link-sharing, max(myf, cfmin) */
143 u64 cl_myf; /* my fit-time (calculated from this
144 class's own upperlimit curve) */
145 u64 cl_myfadj; /* my fit-time adjustment (to cancel
146 history dependence) */
147 u64 cl_cfmin; /* earliest children's fit-time (used
148 with cl_myf to obtain cl_f) */
149 u64 cl_cvtmin; /* minimal virtual time among the
150 children fit for link-sharing
151 (monotonic within a period) */
152 u64 cl_vtadj; /* intra-period cumulative vt
153 adjustment */
Michal Soltys678a6242016-08-03 00:44:54 +0200154 u64 cl_cvtoff; /* largest virtual time seen among
155 the children */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 struct internal_sc cl_rsc; /* internal real-time service curve */
158 struct internal_sc cl_fsc; /* internal fair service curve */
159 struct internal_sc cl_usc; /* internal upperlimit service curve */
160 struct runtime_sc cl_deadline; /* deadline curve */
161 struct runtime_sc cl_eligible; /* eligible curve */
162 struct runtime_sc cl_virtual; /* virtual curve */
163 struct runtime_sc cl_ulimit; /* upperlimit curve */
164
Florian Westphalbba7eb52016-07-04 16:22:20 +0200165 u8 cl_flags; /* which curves are valid */
166 u32 cl_vtperiod; /* vt period sequence number */
167 u32 cl_parentperiod;/* parent's vt period sequence number*/
168 u32 cl_nactive; /* number of active children */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169};
170
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000171struct hfsc_sched {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 u16 defcls; /* default class id */
173 struct hfsc_class root; /* root class */
Patrick McHardybe0d39d2008-07-05 23:21:47 -0700174 struct Qdisc_class_hash clhash; /* class hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 struct rb_root eligible; /* eligible tree */
Patrick McHardyed2b2292007-03-16 01:19:33 -0700176 struct qdisc_watchdog watchdog; /* watchdog timer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179#define HT_INFINITY 0xffffffffffffffffULL /* infinite time value */
180
181
182/*
183 * eligible tree holds backlogged classes being sorted by their eligible times.
184 * there is one eligible tree per hfsc instance.
185 */
186
187static void
188eltree_insert(struct hfsc_class *cl)
189{
190 struct rb_node **p = &cl->sched->eligible.rb_node;
191 struct rb_node *parent = NULL;
192 struct hfsc_class *cl1;
193
194 while (*p != NULL) {
195 parent = *p;
196 cl1 = rb_entry(parent, struct hfsc_class, el_node);
197 if (cl->cl_e >= cl1->cl_e)
198 p = &parent->rb_right;
199 else
200 p = &parent->rb_left;
201 }
202 rb_link_node(&cl->el_node, parent, p);
203 rb_insert_color(&cl->el_node, &cl->sched->eligible);
204}
205
206static inline void
207eltree_remove(struct hfsc_class *cl)
208{
209 rb_erase(&cl->el_node, &cl->sched->eligible);
210}
211
212static inline void
213eltree_update(struct hfsc_class *cl)
214{
215 eltree_remove(cl);
216 eltree_insert(cl);
217}
218
219/* find the class with the minimum deadline among the eligible classes */
220static inline struct hfsc_class *
221eltree_get_mindl(struct hfsc_sched *q, u64 cur_time)
222{
223 struct hfsc_class *p, *cl = NULL;
224 struct rb_node *n;
225
226 for (n = rb_first(&q->eligible); n != NULL; n = rb_next(n)) {
227 p = rb_entry(n, struct hfsc_class, el_node);
228 if (p->cl_e > cur_time)
229 break;
230 if (cl == NULL || p->cl_d < cl->cl_d)
231 cl = p;
232 }
233 return cl;
234}
235
236/* find the class with minimum eligible time among the eligible classes */
237static inline struct hfsc_class *
238eltree_get_minel(struct hfsc_sched *q)
239{
240 struct rb_node *n;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 n = rb_first(&q->eligible);
243 if (n == NULL)
244 return NULL;
245 return rb_entry(n, struct hfsc_class, el_node);
246}
247
248/*
249 * vttree holds holds backlogged child classes being sorted by their virtual
250 * time. each intermediate class has one vttree.
251 */
252static void
253vttree_insert(struct hfsc_class *cl)
254{
255 struct rb_node **p = &cl->cl_parent->vt_tree.rb_node;
256 struct rb_node *parent = NULL;
257 struct hfsc_class *cl1;
258
259 while (*p != NULL) {
260 parent = *p;
261 cl1 = rb_entry(parent, struct hfsc_class, vt_node);
262 if (cl->cl_vt >= cl1->cl_vt)
263 p = &parent->rb_right;
264 else
265 p = &parent->rb_left;
266 }
267 rb_link_node(&cl->vt_node, parent, p);
268 rb_insert_color(&cl->vt_node, &cl->cl_parent->vt_tree);
269}
270
271static inline void
272vttree_remove(struct hfsc_class *cl)
273{
274 rb_erase(&cl->vt_node, &cl->cl_parent->vt_tree);
275}
276
277static inline void
278vttree_update(struct hfsc_class *cl)
279{
280 vttree_remove(cl);
281 vttree_insert(cl);
282}
283
284static inline struct hfsc_class *
285vttree_firstfit(struct hfsc_class *cl, u64 cur_time)
286{
287 struct hfsc_class *p;
288 struct rb_node *n;
289
290 for (n = rb_first(&cl->vt_tree); n != NULL; n = rb_next(n)) {
291 p = rb_entry(n, struct hfsc_class, vt_node);
292 if (p->cl_f <= cur_time)
293 return p;
294 }
295 return NULL;
296}
297
298/*
299 * get the leaf class with the minimum vt in the hierarchy
300 */
301static struct hfsc_class *
302vttree_get_minvt(struct hfsc_class *cl, u64 cur_time)
303{
304 /* if root-class's cfmin is bigger than cur_time nothing to do */
305 if (cl->cl_cfmin > cur_time)
306 return NULL;
307
308 while (cl->level > 0) {
309 cl = vttree_firstfit(cl, cur_time);
310 if (cl == NULL)
311 return NULL;
312 /*
313 * update parent's cl_cvtmin.
314 */
315 if (cl->cl_parent->cl_cvtmin < cl->cl_vt)
316 cl->cl_parent->cl_cvtmin = cl->cl_vt;
317 }
318 return cl;
319}
320
321static void
322cftree_insert(struct hfsc_class *cl)
323{
324 struct rb_node **p = &cl->cl_parent->cf_tree.rb_node;
325 struct rb_node *parent = NULL;
326 struct hfsc_class *cl1;
327
328 while (*p != NULL) {
329 parent = *p;
330 cl1 = rb_entry(parent, struct hfsc_class, cf_node);
331 if (cl->cl_f >= cl1->cl_f)
332 p = &parent->rb_right;
333 else
334 p = &parent->rb_left;
335 }
336 rb_link_node(&cl->cf_node, parent, p);
337 rb_insert_color(&cl->cf_node, &cl->cl_parent->cf_tree);
338}
339
340static inline void
341cftree_remove(struct hfsc_class *cl)
342{
343 rb_erase(&cl->cf_node, &cl->cl_parent->cf_tree);
344}
345
346static inline void
347cftree_update(struct hfsc_class *cl)
348{
349 cftree_remove(cl);
350 cftree_insert(cl);
351}
352
353/*
354 * service curve support functions
355 *
356 * external service curve parameters
357 * m: bps
358 * d: us
359 * internal service curve parameters
360 * sm: (bytes/psched_us) << SM_SHIFT
361 * ism: (psched_us/byte) << ISM_SHIFT
362 * dx: psched_us
363 *
Jarek Poplawski728bf092009-06-08 22:05:00 +0000364 * The clock source resolution with ktime and PSCHED_SHIFT 10 is 1.024us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *
366 * sm and ism are scaled in order to keep effective digits.
367 * SM_SHIFT and ISM_SHIFT are selected to keep at least 4 effective
368 * digits in decimal using the following table.
369 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 * bits/sec 100Kbps 1Mbps 10Mbps 100Mbps 1Gbps
371 * ------------+-------------------------------------------------------
Patrick McHardy641b9e02007-03-16 01:18:42 -0700372 * bytes/1.024us 12.8e-3 128e-3 1280e-3 12800e-3 128000e-3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 *
Patrick McHardy641b9e02007-03-16 01:18:42 -0700374 * 1.024us/byte 78.125 7.8125 0.78125 0.078125 0.0078125
Jarek Poplawski728bf092009-06-08 22:05:00 +0000375 *
376 * So, for PSCHED_SHIFT 10 we need: SM_SHIFT 20, ISM_SHIFT 18.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 */
Jarek Poplawski728bf092009-06-08 22:05:00 +0000378#define SM_SHIFT (30 - PSCHED_SHIFT)
379#define ISM_SHIFT (8 + PSCHED_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381#define SM_MASK ((1ULL << SM_SHIFT) - 1)
382#define ISM_MASK ((1ULL << ISM_SHIFT) - 1)
383
384static inline u64
385seg_x2y(u64 x, u64 sm)
386{
387 u64 y;
388
389 /*
390 * compute
391 * y = x * sm >> SM_SHIFT
392 * but divide it for the upper and lower bits to avoid overflow
393 */
394 y = (x >> SM_SHIFT) * sm + (((x & SM_MASK) * sm) >> SM_SHIFT);
395 return y;
396}
397
398static inline u64
399seg_y2x(u64 y, u64 ism)
400{
401 u64 x;
402
403 if (y == 0)
404 x = 0;
405 else if (ism == HT_INFINITY)
406 x = HT_INFINITY;
407 else {
408 x = (y >> ISM_SHIFT) * ism
409 + (((y & ISM_MASK) * ism) >> ISM_SHIFT);
410 }
411 return x;
412}
413
414/* Convert m (bps) into sm (bytes/psched us) */
415static u64
416m2sm(u32 m)
417{
418 u64 sm;
419
420 sm = ((u64)m << SM_SHIFT);
Patrick McHardy00c04af2007-03-16 01:23:02 -0700421 sm += PSCHED_TICKS_PER_SEC - 1;
422 do_div(sm, PSCHED_TICKS_PER_SEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return sm;
424}
425
426/* convert m (bps) into ism (psched us/byte) */
427static u64
428m2ism(u32 m)
429{
430 u64 ism;
431
432 if (m == 0)
433 ism = HT_INFINITY;
434 else {
Patrick McHardy00c04af2007-03-16 01:23:02 -0700435 ism = ((u64)PSCHED_TICKS_PER_SEC << ISM_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 ism += m - 1;
437 do_div(ism, m);
438 }
439 return ism;
440}
441
442/* convert d (us) into dx (psched us) */
443static u64
444d2dx(u32 d)
445{
446 u64 dx;
447
Patrick McHardy00c04af2007-03-16 01:23:02 -0700448 dx = ((u64)d * PSCHED_TICKS_PER_SEC);
Patrick McHardy538e43a2006-01-08 22:12:03 -0800449 dx += USEC_PER_SEC - 1;
450 do_div(dx, USEC_PER_SEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return dx;
452}
453
454/* convert sm (bytes/psched us) into m (bps) */
455static u32
456sm2m(u64 sm)
457{
458 u64 m;
459
Patrick McHardy00c04af2007-03-16 01:23:02 -0700460 m = (sm * PSCHED_TICKS_PER_SEC) >> SM_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return (u32)m;
462}
463
464/* convert dx (psched us) into d (us) */
465static u32
466dx2d(u64 dx)
467{
468 u64 d;
469
Patrick McHardy538e43a2006-01-08 22:12:03 -0800470 d = dx * USEC_PER_SEC;
Patrick McHardy00c04af2007-03-16 01:23:02 -0700471 do_div(d, PSCHED_TICKS_PER_SEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return (u32)d;
473}
474
475static void
476sc2isc(struct tc_service_curve *sc, struct internal_sc *isc)
477{
478 isc->sm1 = m2sm(sc->m1);
479 isc->ism1 = m2ism(sc->m1);
480 isc->dx = d2dx(sc->d);
481 isc->dy = seg_x2y(isc->dx, isc->sm1);
482 isc->sm2 = m2sm(sc->m2);
483 isc->ism2 = m2ism(sc->m2);
484}
485
486/*
487 * initialize the runtime service curve with the given internal
488 * service curve starting at (x, y).
489 */
490static void
491rtsc_init(struct runtime_sc *rtsc, struct internal_sc *isc, u64 x, u64 y)
492{
493 rtsc->x = x;
494 rtsc->y = y;
495 rtsc->sm1 = isc->sm1;
496 rtsc->ism1 = isc->ism1;
497 rtsc->dx = isc->dx;
498 rtsc->dy = isc->dy;
499 rtsc->sm2 = isc->sm2;
500 rtsc->ism2 = isc->ism2;
501}
502
503/*
504 * calculate the y-projection of the runtime service curve by the
505 * given x-projection value
506 */
507static u64
508rtsc_y2x(struct runtime_sc *rtsc, u64 y)
509{
510 u64 x;
511
512 if (y < rtsc->y)
513 x = rtsc->x;
514 else if (y <= rtsc->y + rtsc->dy) {
515 /* x belongs to the 1st segment */
516 if (rtsc->dy == 0)
517 x = rtsc->x + rtsc->dx;
518 else
519 x = rtsc->x + seg_y2x(y - rtsc->y, rtsc->ism1);
520 } else {
521 /* x belongs to the 2nd segment */
522 x = rtsc->x + rtsc->dx
523 + seg_y2x(y - rtsc->y - rtsc->dy, rtsc->ism2);
524 }
525 return x;
526}
527
528static u64
529rtsc_x2y(struct runtime_sc *rtsc, u64 x)
530{
531 u64 y;
532
533 if (x <= rtsc->x)
534 y = rtsc->y;
535 else if (x <= rtsc->x + rtsc->dx)
536 /* y belongs to the 1st segment */
537 y = rtsc->y + seg_x2y(x - rtsc->x, rtsc->sm1);
538 else
539 /* y belongs to the 2nd segment */
540 y = rtsc->y + rtsc->dy
541 + seg_x2y(x - rtsc->x - rtsc->dx, rtsc->sm2);
542 return y;
543}
544
545/*
546 * update the runtime service curve by taking the minimum of the current
547 * runtime service curve and the service curve starting at (x, y).
548 */
549static void
550rtsc_min(struct runtime_sc *rtsc, struct internal_sc *isc, u64 x, u64 y)
551{
552 u64 y1, y2, dx, dy;
553 u32 dsm;
554
555 if (isc->sm1 <= isc->sm2) {
556 /* service curve is convex */
557 y1 = rtsc_x2y(rtsc, x);
558 if (y1 < y)
559 /* the current rtsc is smaller */
560 return;
561 rtsc->x = x;
562 rtsc->y = y;
563 return;
564 }
565
566 /*
567 * service curve is concave
568 * compute the two y values of the current rtsc
569 * y1: at x
570 * y2: at (x + dx)
571 */
572 y1 = rtsc_x2y(rtsc, x);
573 if (y1 <= y) {
574 /* rtsc is below isc, no change to rtsc */
575 return;
576 }
577
578 y2 = rtsc_x2y(rtsc, x + isc->dx);
579 if (y2 >= y + isc->dy) {
580 /* rtsc is above isc, replace rtsc by isc */
581 rtsc->x = x;
582 rtsc->y = y;
583 rtsc->dx = isc->dx;
584 rtsc->dy = isc->dy;
585 return;
586 }
587
588 /*
589 * the two curves intersect
590 * compute the offsets (dx, dy) using the reverse
591 * function of seg_x2y()
592 * seg_x2y(dx, sm1) == seg_x2y(dx, sm2) + (y1 - y)
593 */
594 dx = (y1 - y) << SM_SHIFT;
595 dsm = isc->sm1 - isc->sm2;
596 do_div(dx, dsm);
597 /*
598 * check if (x, y1) belongs to the 1st segment of rtsc.
599 * if so, add the offset.
600 */
601 if (rtsc->x + rtsc->dx > x)
602 dx += rtsc->x + rtsc->dx - x;
603 dy = seg_x2y(dx, isc->sm1);
604
605 rtsc->x = x;
606 rtsc->y = y;
607 rtsc->dx = dx;
608 rtsc->dy = dy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611static void
612init_ed(struct hfsc_class *cl, unsigned int next_len)
613{
Patrick McHardy3bebcda2007-03-23 11:29:25 -0700614 u64 cur_time = psched_get_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 /* update the deadline curve */
617 rtsc_min(&cl->cl_deadline, &cl->cl_rsc, cur_time, cl->cl_cumul);
618
619 /*
620 * update the eligible curve.
621 * for concave, it is equal to the deadline curve.
622 * for convex, it is a linear curve with slope m2.
623 */
624 cl->cl_eligible = cl->cl_deadline;
625 if (cl->cl_rsc.sm1 <= cl->cl_rsc.sm2) {
626 cl->cl_eligible.dx = 0;
627 cl->cl_eligible.dy = 0;
628 }
629
630 /* compute e and d */
631 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
632 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
633
634 eltree_insert(cl);
635}
636
637static void
638update_ed(struct hfsc_class *cl, unsigned int next_len)
639{
640 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
641 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
642
643 eltree_update(cl);
644}
645
646static inline void
647update_d(struct hfsc_class *cl, unsigned int next_len)
648{
649 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
650}
651
652static inline void
653update_cfmin(struct hfsc_class *cl)
654{
655 struct rb_node *n = rb_first(&cl->cf_tree);
656 struct hfsc_class *p;
657
658 if (n == NULL) {
659 cl->cl_cfmin = 0;
660 return;
661 }
662 p = rb_entry(n, struct hfsc_class, cf_node);
663 cl->cl_cfmin = p->cl_f;
664}
665
666static void
667init_vf(struct hfsc_class *cl, unsigned int len)
668{
669 struct hfsc_class *max_cl;
670 struct rb_node *n;
671 u64 vt, f, cur_time;
672 int go_active;
673
674 cur_time = 0;
675 go_active = 1;
676 for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
677 if (go_active && cl->cl_nactive++ == 0)
678 go_active = 1;
679 else
680 go_active = 0;
681
682 if (go_active) {
683 n = rb_last(&cl->cl_parent->vt_tree);
684 if (n != NULL) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000685 max_cl = rb_entry(n, struct hfsc_class, vt_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /*
687 * set vt to the average of the min and max
688 * classes. if the parent's period didn't
689 * change, don't decrease vt of the class.
690 */
691 vt = max_cl->cl_vt;
692 if (cl->cl_parent->cl_cvtmin != 0)
693 vt = (cl->cl_parent->cl_cvtmin + vt)/2;
694
695 if (cl->cl_parent->cl_vtperiod !=
696 cl->cl_parentperiod || vt > cl->cl_vt)
697 cl->cl_vt = vt;
698 } else {
699 /*
700 * first child for a new parent backlog period.
Michal Soltys678a6242016-08-03 00:44:54 +0200701 * initialize cl_vt to the highest value seen
702 * among the siblings. this is analogous to
703 * what cur_time would provide in realtime case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 */
Michal Soltys678a6242016-08-03 00:44:54 +0200705 cl->cl_vt = cl->cl_parent->cl_cvtoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 cl->cl_parent->cl_cvtmin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 /* update the virtual curve */
Michal Soltys678a6242016-08-03 00:44:54 +0200710 rtsc_min(&cl->cl_virtual, &cl->cl_fsc, cl->cl_vt, cl->cl_total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 cl->cl_vtadj = 0;
712
713 cl->cl_vtperiod++; /* increment vt period */
714 cl->cl_parentperiod = cl->cl_parent->cl_vtperiod;
715 if (cl->cl_parent->cl_nactive == 0)
716 cl->cl_parentperiod++;
717 cl->cl_f = 0;
718
719 vttree_insert(cl);
720 cftree_insert(cl);
721
722 if (cl->cl_flags & HFSC_USC) {
723 /* class has upper limit curve */
724 if (cur_time == 0)
Patrick McHardy3bebcda2007-03-23 11:29:25 -0700725 cur_time = psched_get_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 /* update the ulimit curve */
728 rtsc_min(&cl->cl_ulimit, &cl->cl_usc, cur_time,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900729 cl->cl_total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /* compute myf */
731 cl->cl_myf = rtsc_y2x(&cl->cl_ulimit,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900732 cl->cl_total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 cl->cl_myfadj = 0;
734 }
735 }
736
737 f = max(cl->cl_myf, cl->cl_cfmin);
738 if (f != cl->cl_f) {
739 cl->cl_f = f;
740 cftree_update(cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
Michal Soltys3b2eb612010-08-30 11:34:10 +0000742 update_cfmin(cl->cl_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744}
745
746static void
747update_vf(struct hfsc_class *cl, unsigned int len, u64 cur_time)
748{
749 u64 f; /* , myf_bound, delta; */
750 int go_passive = 0;
751
752 if (cl->qdisc->q.qlen == 0 && cl->cl_flags & HFSC_FSC)
753 go_passive = 1;
754
755 for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
756 cl->cl_total += len;
757
758 if (!(cl->cl_flags & HFSC_FSC) || cl->cl_nactive == 0)
759 continue;
760
761 if (go_passive && --cl->cl_nactive == 0)
762 go_passive = 1;
763 else
764 go_passive = 0;
765
Michal Soltysab12cb42016-06-30 02:26:47 +0200766 /* update vt */
Michal Soltys678a6242016-08-03 00:44:54 +0200767 cl->cl_vt = rtsc_y2x(&cl->cl_virtual, cl->cl_total) + cl->cl_vtadj;
Michal Soltysab12cb42016-06-30 02:26:47 +0200768
769 /*
770 * if vt of the class is smaller than cvtmin,
771 * the class was skipped in the past due to non-fit.
772 * if so, we need to adjust vtadj.
773 */
774 if (cl->cl_vt < cl->cl_parent->cl_cvtmin) {
775 cl->cl_vtadj += cl->cl_parent->cl_cvtmin - cl->cl_vt;
776 cl->cl_vt = cl->cl_parent->cl_cvtmin;
777 }
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (go_passive) {
780 /* no more active child, going passive */
781
Michal Soltys678a6242016-08-03 00:44:54 +0200782 /* update cvtoff of the parent class */
783 if (cl->cl_vt > cl->cl_parent->cl_cvtoff)
784 cl->cl_parent->cl_cvtoff = cl->cl_vt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 /* remove this class from the vt tree */
787 vttree_remove(cl);
788
789 cftree_remove(cl);
790 update_cfmin(cl->cl_parent);
791
792 continue;
793 }
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /* update the vt tree */
796 vttree_update(cl);
797
Michal Soltysab12cb42016-06-30 02:26:47 +0200798 /* update f */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (cl->cl_flags & HFSC_USC) {
800 cl->cl_myf = cl->cl_myfadj + rtsc_y2x(&cl->cl_ulimit,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900801 cl->cl_total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802#if 0
803 /*
804 * This code causes classes to stay way under their
805 * limit when multiple classes are used at gigabit
806 * speed. needs investigation. -kaber
807 */
808 /*
809 * if myf lags behind by more than one clock tick
810 * from the current time, adjust myfadj to prevent
811 * a rate-limited class from going greedy.
812 * in a steady state under rate-limiting, myf
813 * fluctuates within one clock tick.
814 */
815 myf_bound = cur_time - PSCHED_JIFFIE2US(1);
816 if (cl->cl_myf < myf_bound) {
817 delta = cur_time - cl->cl_myf;
818 cl->cl_myfadj += delta;
819 cl->cl_myf += delta;
820 }
821#endif
822 }
823
824 f = max(cl->cl_myf, cl->cl_cfmin);
825 if (f != cl->cl_f) {
826 cl->cl_f = f;
827 cftree_update(cl);
828 update_cfmin(cl->cl_parent);
829 }
830 }
831}
832
833static void
834set_active(struct hfsc_class *cl, unsigned int len)
835{
836 if (cl->cl_flags & HFSC_RSC)
837 init_ed(cl, len);
838 if (cl->cl_flags & HFSC_FSC)
839 init_vf(cl, len);
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
843static void
844set_passive(struct hfsc_class *cl)
845{
846 if (cl->cl_flags & HFSC_RSC)
847 eltree_remove(cl);
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /*
850 * vttree is now handled in update_vf() so that update_vf(cl, 0, 0)
851 * needs to be called explicitly to remove a class from vttree.
852 */
853}
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855static unsigned int
856qdisc_peek_len(struct Qdisc *sch)
857{
858 struct sk_buff *skb;
859 unsigned int len;
860
Jarek Poplawski03c05f02008-10-31 00:46:19 -0700861 skb = sch->ops->peek(sch);
Michal Soltysd1d0fc52016-06-30 02:26:45 +0200862 if (unlikely(skb == NULL)) {
Jarek Poplawskib00355d2009-02-01 01:12:42 -0800863 qdisc_warn_nonwc("qdisc_peek_len", sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return 0;
865 }
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700866 len = qdisc_pkt_len(skb);
Jarek Poplawski03c05f02008-10-31 00:46:19 -0700867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return len;
869}
870
871static void
872hfsc_purge_queue(struct Qdisc *sch, struct hfsc_class *cl)
873{
874 unsigned int len = cl->qdisc->q.qlen;
WANG Cong2ccccf52016-02-25 14:55:01 -0800875 unsigned int backlog = cl->qdisc->qstats.backlog;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 qdisc_reset(cl->qdisc);
WANG Cong2ccccf52016-02-25 14:55:01 -0800878 qdisc_tree_reduce_backlog(cl->qdisc, len, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
881static void
882hfsc_adjust_levels(struct hfsc_class *cl)
883{
884 struct hfsc_class *p;
885 unsigned int level;
886
887 do {
888 level = 0;
889 list_for_each_entry(p, &cl->children, siblings) {
Patrick McHardy210525d2006-05-11 12:22:03 -0700890 if (p->level >= level)
891 level = p->level + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
Patrick McHardy210525d2006-05-11 12:22:03 -0700893 cl->level = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 } while ((cl = cl->cl_parent) != NULL);
895}
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897static inline struct hfsc_class *
898hfsc_find_class(u32 classid, struct Qdisc *sch)
899{
900 struct hfsc_sched *q = qdisc_priv(sch);
Patrick McHardybe0d39d2008-07-05 23:21:47 -0700901 struct Qdisc_class_common *clc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Patrick McHardybe0d39d2008-07-05 23:21:47 -0700903 clc = qdisc_class_find(&q->clhash, classid);
904 if (clc == NULL)
905 return NULL;
906 return container_of(clc, struct hfsc_class, cl_common);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909static void
910hfsc_change_rsc(struct hfsc_class *cl, struct tc_service_curve *rsc,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900911 u64 cur_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
913 sc2isc(rsc, &cl->cl_rsc);
914 rtsc_init(&cl->cl_deadline, &cl->cl_rsc, cur_time, cl->cl_cumul);
915 cl->cl_eligible = cl->cl_deadline;
916 if (cl->cl_rsc.sm1 <= cl->cl_rsc.sm2) {
917 cl->cl_eligible.dx = 0;
918 cl->cl_eligible.dy = 0;
919 }
920 cl->cl_flags |= HFSC_RSC;
921}
922
923static void
924hfsc_change_fsc(struct hfsc_class *cl, struct tc_service_curve *fsc)
925{
926 sc2isc(fsc, &cl->cl_fsc);
Michal Soltys678a6242016-08-03 00:44:54 +0200927 rtsc_init(&cl->cl_virtual, &cl->cl_fsc, cl->cl_vt, cl->cl_total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 cl->cl_flags |= HFSC_FSC;
929}
930
931static void
932hfsc_change_usc(struct hfsc_class *cl, struct tc_service_curve *usc,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900933 u64 cur_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 sc2isc(usc, &cl->cl_usc);
936 rtsc_init(&cl->cl_ulimit, &cl->cl_usc, cur_time, cl->cl_total);
937 cl->cl_flags |= HFSC_USC;
938}
939
Patrick McHardy27a34212008-01-23 20:35:39 -0800940static const struct nla_policy hfsc_policy[TCA_HFSC_MAX + 1] = {
941 [TCA_HFSC_RSC] = { .len = sizeof(struct tc_service_curve) },
942 [TCA_HFSC_FSC] = { .len = sizeof(struct tc_service_curve) },
943 [TCA_HFSC_USC] = { .len = sizeof(struct tc_service_curve) },
944};
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946static int
947hfsc_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
Patrick McHardy1e904742008-01-22 22:11:17 -0800948 struct nlattr **tca, unsigned long *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 struct hfsc_sched *q = qdisc_priv(sch);
951 struct hfsc_class *cl = (struct hfsc_class *)*arg;
952 struct hfsc_class *parent = NULL;
Patrick McHardy1e904742008-01-22 22:11:17 -0800953 struct nlattr *opt = tca[TCA_OPTIONS];
954 struct nlattr *tb[TCA_HFSC_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 struct tc_service_curve *rsc = NULL, *fsc = NULL, *usc = NULL;
956 u64 cur_time;
Patrick McHardycee63722008-01-23 20:33:32 -0800957 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Patrick McHardycee63722008-01-23 20:33:32 -0800959 if (opt == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return -EINVAL;
961
Patrick McHardy27a34212008-01-23 20:35:39 -0800962 err = nla_parse_nested(tb, TCA_HFSC_MAX, opt, hfsc_policy);
Patrick McHardycee63722008-01-23 20:33:32 -0800963 if (err < 0)
964 return err;
965
Patrick McHardy1e904742008-01-22 22:11:17 -0800966 if (tb[TCA_HFSC_RSC]) {
Patrick McHardy1e904742008-01-22 22:11:17 -0800967 rsc = nla_data(tb[TCA_HFSC_RSC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (rsc->m1 == 0 && rsc->m2 == 0)
969 rsc = NULL;
970 }
971
Patrick McHardy1e904742008-01-22 22:11:17 -0800972 if (tb[TCA_HFSC_FSC]) {
Patrick McHardy1e904742008-01-22 22:11:17 -0800973 fsc = nla_data(tb[TCA_HFSC_FSC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (fsc->m1 == 0 && fsc->m2 == 0)
975 fsc = NULL;
976 }
977
Patrick McHardy1e904742008-01-22 22:11:17 -0800978 if (tb[TCA_HFSC_USC]) {
Patrick McHardy1e904742008-01-22 22:11:17 -0800979 usc = nla_data(tb[TCA_HFSC_USC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (usc->m1 == 0 && usc->m2 == 0)
981 usc = NULL;
982 }
983
984 if (cl != NULL) {
985 if (parentid) {
Patrick McHardybe0d39d2008-07-05 23:21:47 -0700986 if (cl->cl_parent &&
987 cl->cl_parent->cl_common.classid != parentid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return -EINVAL;
989 if (cl->cl_parent == NULL && parentid != TC_H_ROOT)
990 return -EINVAL;
991 }
Patrick McHardy3bebcda2007-03-23 11:29:25 -0700992 cur_time = psched_get_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800994 if (tca[TCA_RATE]) {
John Fastabend22e0f8b2014-09-28 11:52:56 -0700995 err = gen_replace_estimator(&cl->bstats, NULL,
996 &cl->rate_est,
Eric Dumazetedb09eb2016-06-06 09:37:16 -0700997 NULL,
998 qdisc_root_sleeping_running(sch),
John Fastabend22e0f8b2014-09-28 11:52:56 -0700999 tca[TCA_RATE]);
Stephen Hemminger71bcb092008-11-25 21:13:31 -08001000 if (err)
1001 return err;
1002 }
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 sch_tree_lock(sch);
1005 if (rsc != NULL)
1006 hfsc_change_rsc(cl, rsc, cur_time);
1007 if (fsc != NULL)
1008 hfsc_change_fsc(cl, fsc);
1009 if (usc != NULL)
1010 hfsc_change_usc(cl, usc, cur_time);
1011
1012 if (cl->qdisc->q.qlen != 0) {
1013 if (cl->cl_flags & HFSC_RSC)
1014 update_ed(cl, qdisc_peek_len(cl->qdisc));
1015 if (cl->cl_flags & HFSC_FSC)
1016 update_vf(cl, 0, cur_time);
1017 }
1018 sch_tree_unlock(sch);
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return 0;
1021 }
1022
1023 if (parentid == TC_H_ROOT)
1024 return -EEXIST;
1025
1026 parent = &q->root;
1027 if (parentid) {
1028 parent = hfsc_find_class(parentid, sch);
1029 if (parent == NULL)
1030 return -ENOENT;
1031 }
1032
1033 if (classid == 0 || TC_H_MAJ(classid ^ sch->handle) != 0)
1034 return -EINVAL;
1035 if (hfsc_find_class(classid, sch))
1036 return -EEXIST;
1037
1038 if (rsc == NULL && fsc == NULL)
1039 return -EINVAL;
1040
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001041 cl = kzalloc(sizeof(struct hfsc_class), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (cl == NULL)
1043 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Stephen Hemminger71bcb092008-11-25 21:13:31 -08001045 if (tca[TCA_RATE]) {
John Fastabend22e0f8b2014-09-28 11:52:56 -07001046 err = gen_new_estimator(&cl->bstats, NULL, &cl->rate_est,
Eric Dumazetedb09eb2016-06-06 09:37:16 -07001047 NULL,
1048 qdisc_root_sleeping_running(sch),
Stephen Hemminger71bcb092008-11-25 21:13:31 -08001049 tca[TCA_RATE]);
1050 if (err) {
1051 kfree(cl);
1052 return err;
1053 }
1054 }
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (rsc != NULL)
1057 hfsc_change_rsc(cl, rsc, 0);
1058 if (fsc != NULL)
1059 hfsc_change_fsc(cl, fsc);
1060 if (usc != NULL)
1061 hfsc_change_usc(cl, usc, 0);
1062
Patrick McHardybe0d39d2008-07-05 23:21:47 -07001063 cl->cl_common.classid = classid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 cl->refcnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 cl->sched = q;
1066 cl->cl_parent = parent;
Changli Gao3511c912010-10-16 13:04:08 +00001067 cl->qdisc = qdisc_create_dflt(sch->dev_queue,
David S. Millerbb949fb2008-07-08 16:55:56 -07001068 &pfifo_qdisc_ops, classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (cl->qdisc == NULL)
1070 cl->qdisc = &noop_qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 INIT_LIST_HEAD(&cl->children);
1072 cl->vt_tree = RB_ROOT;
1073 cl->cf_tree = RB_ROOT;
1074
1075 sch_tree_lock(sch);
Patrick McHardybe0d39d2008-07-05 23:21:47 -07001076 qdisc_class_hash_insert(&q->clhash, &cl->cl_common);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 list_add_tail(&cl->siblings, &parent->children);
1078 if (parent->level == 0)
1079 hfsc_purge_queue(sch, parent);
1080 hfsc_adjust_levels(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 sch_tree_unlock(sch);
1082
Patrick McHardybe0d39d2008-07-05 23:21:47 -07001083 qdisc_class_hash_grow(sch, &q->clhash);
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 *arg = (unsigned long)cl;
1086 return 0;
1087}
1088
1089static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090hfsc_destroy_class(struct Qdisc *sch, struct hfsc_class *cl)
1091{
1092 struct hfsc_sched *q = qdisc_priv(sch);
1093
Patrick McHardyff31ab52008-07-01 19:52:38 -07001094 tcf_destroy_chain(&cl->filter_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 qdisc_destroy(cl->qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 gen_kill_estimator(&cl->bstats, &cl->rate_est);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (cl != &q->root)
1098 kfree(cl);
1099}
1100
1101static int
1102hfsc_delete_class(struct Qdisc *sch, unsigned long arg)
1103{
1104 struct hfsc_sched *q = qdisc_priv(sch);
1105 struct hfsc_class *cl = (struct hfsc_class *)arg;
1106
1107 if (cl->level > 0 || cl->filter_cnt > 0 || cl == &q->root)
1108 return -EBUSY;
1109
1110 sch_tree_lock(sch);
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 list_del(&cl->siblings);
1113 hfsc_adjust_levels(cl->cl_parent);
Patrick McHardyc38c83c2007-03-27 14:04:24 -07001114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 hfsc_purge_queue(sch, cl);
Patrick McHardybe0d39d2008-07-05 23:21:47 -07001116 qdisc_class_hash_remove(&q->clhash, &cl->cl_common);
Patrick McHardyc38c83c2007-03-27 14:04:24 -07001117
Jarek Poplawski7cd0a632009-03-15 20:00:19 -07001118 BUG_ON(--cl->refcnt == 0);
1119 /*
1120 * This shouldn't happen: we "hold" one cops->get() when called
1121 * from tc_ctl_tclass; the destroy method is done from cops->put().
1122 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 sch_tree_unlock(sch);
1125 return 0;
1126}
1127
1128static struct hfsc_class *
1129hfsc_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
1130{
1131 struct hfsc_sched *q = qdisc_priv(sch);
Patrick McHardya2f79222010-05-14 08:08:14 +00001132 struct hfsc_class *head, *cl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 struct tcf_result res;
1134 struct tcf_proto *tcf;
1135 int result;
1136
1137 if (TC_H_MAJ(skb->priority ^ sch->handle) == 0 &&
1138 (cl = hfsc_find_class(skb->priority, sch)) != NULL)
1139 if (cl->level == 0)
1140 return cl;
1141
Jarek Poplawskic27f3392008-08-04 22:39:11 -07001142 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
Patrick McHardya2f79222010-05-14 08:08:14 +00001143 head = &q->root;
John Fastabend25d8c0d2014-09-12 20:05:27 -07001144 tcf = rcu_dereference_bh(q->root.filter_list);
Daniel Borkmann3b3ae882015-08-26 23:00:06 +02001145 while (tcf && (result = tc_classify(skb, tcf, &res, false)) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146#ifdef CONFIG_NET_CLS_ACT
1147 switch (result) {
1148 case TC_ACT_QUEUED:
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001149 case TC_ACT_STOLEN:
Jarek Poplawski378a2f02008-08-04 22:31:03 -07001150 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001151 case TC_ACT_SHOT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return NULL;
1153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154#endif
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001155 cl = (struct hfsc_class *)res.class;
1156 if (!cl) {
1157 cl = hfsc_find_class(res.classid, sch);
1158 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 break; /* filter selected invalid classid */
Patrick McHardya2f79222010-05-14 08:08:14 +00001160 if (cl->level >= head->level)
1161 break; /* filter may only point downwards */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163
1164 if (cl->level == 0)
1165 return cl; /* hit leaf class */
1166
1167 /* apply inner filter chain */
John Fastabend25d8c0d2014-09-12 20:05:27 -07001168 tcf = rcu_dereference_bh(cl->filter_list);
Patrick McHardya2f79222010-05-14 08:08:14 +00001169 head = cl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
1171
1172 /* classification failed, try default class */
1173 cl = hfsc_find_class(TC_H_MAKE(TC_H_MAJ(sch->handle), q->defcls), sch);
1174 if (cl == NULL || cl->level > 0)
1175 return NULL;
1176
1177 return cl;
1178}
1179
1180static int
1181hfsc_graft_class(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001182 struct Qdisc **old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 struct hfsc_class *cl = (struct hfsc_class *)arg;
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (cl->level > 0)
1187 return -EINVAL;
1188 if (new == NULL) {
Changli Gao3511c912010-10-16 13:04:08 +00001189 new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
Patrick McHardybe0d39d2008-07-05 23:21:47 -07001190 cl->cl_common.classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (new == NULL)
1192 new = &noop_qdisc;
1193 }
1194
WANG Cong86a79962016-02-25 14:55:00 -08001195 *old = qdisc_replace(sch, new, &cl->qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return 0;
1197}
1198
1199static struct Qdisc *
1200hfsc_class_leaf(struct Qdisc *sch, unsigned long arg)
1201{
1202 struct hfsc_class *cl = (struct hfsc_class *)arg;
1203
Patrick McHardy5b9a9cc2009-09-04 06:41:17 +00001204 if (cl->level == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return cl->qdisc;
1206
1207 return NULL;
1208}
1209
Patrick McHardyf973b912006-11-29 17:36:43 -08001210static void
1211hfsc_qlen_notify(struct Qdisc *sch, unsigned long arg)
1212{
1213 struct hfsc_class *cl = (struct hfsc_class *)arg;
1214
1215 if (cl->qdisc->q.qlen == 0) {
1216 update_vf(cl, 0, 0);
1217 set_passive(cl);
1218 }
1219}
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221static unsigned long
1222hfsc_get_class(struct Qdisc *sch, u32 classid)
1223{
1224 struct hfsc_class *cl = hfsc_find_class(classid, sch);
1225
1226 if (cl != NULL)
1227 cl->refcnt++;
1228
1229 return (unsigned long)cl;
1230}
1231
1232static void
1233hfsc_put_class(struct Qdisc *sch, unsigned long arg)
1234{
1235 struct hfsc_class *cl = (struct hfsc_class *)arg;
1236
1237 if (--cl->refcnt == 0)
1238 hfsc_destroy_class(sch, cl);
1239}
1240
1241static unsigned long
1242hfsc_bind_tcf(struct Qdisc *sch, unsigned long parent, u32 classid)
1243{
1244 struct hfsc_class *p = (struct hfsc_class *)parent;
1245 struct hfsc_class *cl = hfsc_find_class(classid, sch);
1246
1247 if (cl != NULL) {
1248 if (p != NULL && p->level <= cl->level)
1249 return 0;
1250 cl->filter_cnt++;
1251 }
1252
1253 return (unsigned long)cl;
1254}
1255
1256static void
1257hfsc_unbind_tcf(struct Qdisc *sch, unsigned long arg)
1258{
1259 struct hfsc_class *cl = (struct hfsc_class *)arg;
1260
1261 cl->filter_cnt--;
1262}
1263
John Fastabend25d8c0d2014-09-12 20:05:27 -07001264static struct tcf_proto __rcu **
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265hfsc_tcf_chain(struct Qdisc *sch, unsigned long arg)
1266{
1267 struct hfsc_sched *q = qdisc_priv(sch);
1268 struct hfsc_class *cl = (struct hfsc_class *)arg;
1269
1270 if (cl == NULL)
1271 cl = &q->root;
1272
1273 return &cl->filter_list;
1274}
1275
1276static int
1277hfsc_dump_sc(struct sk_buff *skb, int attr, struct internal_sc *sc)
1278{
1279 struct tc_service_curve tsc;
1280
1281 tsc.m1 = sm2m(sc->sm1);
1282 tsc.d = dx2d(sc->dx);
1283 tsc.m2 = sm2m(sc->sm2);
David S. Miller1b34ec42012-03-29 05:11:39 -04001284 if (nla_put(skb, attr, sizeof(tsc), &tsc))
1285 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 return skb->len;
1288
Patrick McHardy1e904742008-01-22 22:11:17 -08001289 nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return -1;
1291}
1292
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001293static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294hfsc_dump_curves(struct sk_buff *skb, struct hfsc_class *cl)
1295{
1296 if ((cl->cl_flags & HFSC_RSC) &&
1297 (hfsc_dump_sc(skb, TCA_HFSC_RSC, &cl->cl_rsc) < 0))
Patrick McHardy1e904742008-01-22 22:11:17 -08001298 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 if ((cl->cl_flags & HFSC_FSC) &&
1301 (hfsc_dump_sc(skb, TCA_HFSC_FSC, &cl->cl_fsc) < 0))
Patrick McHardy1e904742008-01-22 22:11:17 -08001302 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 if ((cl->cl_flags & HFSC_USC) &&
1305 (hfsc_dump_sc(skb, TCA_HFSC_USC, &cl->cl_usc) < 0))
Patrick McHardy1e904742008-01-22 22:11:17 -08001306 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 return skb->len;
1309
Patrick McHardy1e904742008-01-22 22:11:17 -08001310 nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return -1;
1312}
1313
1314static int
1315hfsc_dump_class(struct Qdisc *sch, unsigned long arg, struct sk_buff *skb,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001316 struct tcmsg *tcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 struct hfsc_class *cl = (struct hfsc_class *)arg;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001319 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Patrick McHardybe0d39d2008-07-05 23:21:47 -07001321 tcm->tcm_parent = cl->cl_parent ? cl->cl_parent->cl_common.classid :
1322 TC_H_ROOT;
1323 tcm->tcm_handle = cl->cl_common.classid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (cl->level == 0)
1325 tcm->tcm_info = cl->qdisc->handle;
1326
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001327 nest = nla_nest_start(skb, TCA_OPTIONS);
1328 if (nest == NULL)
1329 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (hfsc_dump_curves(skb, cl) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001331 goto nla_put_failure;
Yang Yingliangd59b7d82014-03-12 10:20:32 +08001332 return nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Patrick McHardy1e904742008-01-22 22:11:17 -08001334 nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001335 nla_nest_cancel(skb, nest);
Thomas Grafbc3ed282008-06-03 16:36:54 -07001336 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
1339static int
1340hfsc_dump_class_stats(struct Qdisc *sch, unsigned long arg,
1341 struct gnet_dump *d)
1342{
1343 struct hfsc_class *cl = (struct hfsc_class *)arg;
1344 struct tc_hfsc_stats xstats;
1345
Eric Dumazetf5a59b72011-12-23 05:19:20 +00001346 cl->qstats.backlog = cl->qdisc->qstats.backlog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 xstats.level = cl->level;
1348 xstats.period = cl->cl_vtperiod;
1349 xstats.work = cl->cl_total;
1350 xstats.rtwork = cl->cl_cumul;
1351
Eric Dumazetedb09eb2016-06-06 09:37:16 -07001352 if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch), d, NULL, &cl->bstats) < 0 ||
Eric Dumazetd250a5f2009-10-02 10:32:18 +00001353 gnet_stats_copy_rate_est(d, &cl->bstats, &cl->rate_est) < 0 ||
John Fastabendb0ab6f92014-09-28 11:54:24 -07001354 gnet_stats_copy_queue(d, NULL, &cl->qstats, cl->qdisc->q.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return -1;
1356
1357 return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
1358}
1359
1360
1361
1362static void
1363hfsc_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1364{
1365 struct hfsc_sched *q = qdisc_priv(sch);
1366 struct hfsc_class *cl;
1367 unsigned int i;
1368
1369 if (arg->stop)
1370 return;
1371
Patrick McHardybe0d39d2008-07-05 23:21:47 -07001372 for (i = 0; i < q->clhash.hashsize; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001373 hlist_for_each_entry(cl, &q->clhash.hash[i],
Patrick McHardybe0d39d2008-07-05 23:21:47 -07001374 cl_common.hnode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (arg->count < arg->skip) {
1376 arg->count++;
1377 continue;
1378 }
1379 if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
1380 arg->stop = 1;
1381 return;
1382 }
1383 arg->count++;
1384 }
1385 }
1386}
1387
1388static void
Patrick McHardyed2b2292007-03-16 01:19:33 -07001389hfsc_schedule_watchdog(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 struct hfsc_sched *q = qdisc_priv(sch);
1392 struct hfsc_class *cl;
1393 u64 next_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001395 cl = eltree_get_minel(q);
1396 if (cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 next_time = cl->cl_e;
1398 if (q->root.cl_cfmin != 0) {
1399 if (next_time == 0 || next_time > q->root.cl_cfmin)
1400 next_time = q->root.cl_cfmin;
1401 }
Patrick McHardy3d50f232007-02-13 12:36:57 -08001402 WARN_ON(next_time == 0);
Patrick McHardyed2b2292007-03-16 01:19:33 -07001403 qdisc_watchdog_schedule(&q->watchdog, next_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
1406static int
Patrick McHardy1e904742008-01-22 22:11:17 -08001407hfsc_init_qdisc(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 struct hfsc_sched *q = qdisc_priv(sch);
1410 struct tc_hfsc_qopt *qopt;
Patrick McHardybe0d39d2008-07-05 23:21:47 -07001411 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Patrick McHardy1e904742008-01-22 22:11:17 -08001413 if (opt == NULL || nla_len(opt) < sizeof(*qopt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return -EINVAL;
Patrick McHardy1e904742008-01-22 22:11:17 -08001415 qopt = nla_data(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 q->defcls = qopt->defcls;
Patrick McHardybe0d39d2008-07-05 23:21:47 -07001418 err = qdisc_class_hash_init(&q->clhash);
1419 if (err < 0)
1420 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 q->eligible = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Patrick McHardybe0d39d2008-07-05 23:21:47 -07001423 q->root.cl_common.classid = sch->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 q->root.refcnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 q->root.sched = q;
Changli Gao3511c912010-10-16 13:04:08 +00001426 q->root.qdisc = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
Patrick McHardy9f9afec2006-11-29 17:35:18 -08001427 sch->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (q->root.qdisc == NULL)
1429 q->root.qdisc = &noop_qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 INIT_LIST_HEAD(&q->root.children);
1431 q->root.vt_tree = RB_ROOT;
1432 q->root.cf_tree = RB_ROOT;
1433
Patrick McHardybe0d39d2008-07-05 23:21:47 -07001434 qdisc_class_hash_insert(&q->clhash, &q->root.cl_common);
1435 qdisc_class_hash_grow(sch, &q->clhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Patrick McHardyed2b2292007-03-16 01:19:33 -07001437 qdisc_watchdog_init(&q->watchdog, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 return 0;
1440}
1441
1442static int
Patrick McHardy1e904742008-01-22 22:11:17 -08001443hfsc_change_qdisc(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
1445 struct hfsc_sched *q = qdisc_priv(sch);
1446 struct tc_hfsc_qopt *qopt;
1447
Patrick McHardy1e904742008-01-22 22:11:17 -08001448 if (opt == NULL || nla_len(opt) < sizeof(*qopt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return -EINVAL;
Patrick McHardy1e904742008-01-22 22:11:17 -08001450 qopt = nla_data(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 sch_tree_lock(sch);
1453 q->defcls = qopt->defcls;
1454 sch_tree_unlock(sch);
1455
1456 return 0;
1457}
1458
1459static void
1460hfsc_reset_class(struct hfsc_class *cl)
1461{
1462 cl->cl_total = 0;
1463 cl->cl_cumul = 0;
1464 cl->cl_d = 0;
1465 cl->cl_e = 0;
1466 cl->cl_vt = 0;
1467 cl->cl_vtadj = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 cl->cl_cvtmin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 cl->cl_cvtoff = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 cl->cl_vtperiod = 0;
1471 cl->cl_parentperiod = 0;
1472 cl->cl_f = 0;
1473 cl->cl_myf = 0;
1474 cl->cl_myfadj = 0;
1475 cl->cl_cfmin = 0;
1476 cl->cl_nactive = 0;
1477
1478 cl->vt_tree = RB_ROOT;
1479 cl->cf_tree = RB_ROOT;
1480 qdisc_reset(cl->qdisc);
1481
1482 if (cl->cl_flags & HFSC_RSC)
1483 rtsc_init(&cl->cl_deadline, &cl->cl_rsc, 0, 0);
1484 if (cl->cl_flags & HFSC_FSC)
1485 rtsc_init(&cl->cl_virtual, &cl->cl_fsc, 0, 0);
1486 if (cl->cl_flags & HFSC_USC)
1487 rtsc_init(&cl->cl_ulimit, &cl->cl_usc, 0, 0);
1488}
1489
1490static void
1491hfsc_reset_qdisc(struct Qdisc *sch)
1492{
1493 struct hfsc_sched *q = qdisc_priv(sch);
1494 struct hfsc_class *cl;
1495 unsigned int i;
1496
Patrick McHardybe0d39d2008-07-05 23:21:47 -07001497 for (i = 0; i < q->clhash.hashsize; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001498 hlist_for_each_entry(cl, &q->clhash.hash[i], cl_common.hnode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 hfsc_reset_class(cl);
1500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 q->eligible = RB_ROOT;
Patrick McHardyed2b2292007-03-16 01:19:33 -07001502 qdisc_watchdog_cancel(&q->watchdog);
WANG Cong357cc9b2016-06-01 16:15:15 -07001503 sch->qstats.backlog = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 sch->q.qlen = 0;
1505}
1506
1507static void
1508hfsc_destroy_qdisc(struct Qdisc *sch)
1509{
1510 struct hfsc_sched *q = qdisc_priv(sch);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001511 struct hlist_node *next;
Patrick McHardybe0d39d2008-07-05 23:21:47 -07001512 struct hfsc_class *cl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 unsigned int i;
1514
Patrick McHardybe0d39d2008-07-05 23:21:47 -07001515 for (i = 0; i < q->clhash.hashsize; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001516 hlist_for_each_entry(cl, &q->clhash.hash[i], cl_common.hnode)
Patrick McHardya4aebb82008-07-01 19:53:09 -07001517 tcf_destroy_chain(&cl->filter_list);
1518 }
Patrick McHardybe0d39d2008-07-05 23:21:47 -07001519 for (i = 0; i < q->clhash.hashsize; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001520 hlist_for_each_entry_safe(cl, next, &q->clhash.hash[i],
Patrick McHardybe0d39d2008-07-05 23:21:47 -07001521 cl_common.hnode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 hfsc_destroy_class(sch, cl);
1523 }
Patrick McHardybe0d39d2008-07-05 23:21:47 -07001524 qdisc_class_hash_destroy(&q->clhash);
Patrick McHardyed2b2292007-03-16 01:19:33 -07001525 qdisc_watchdog_cancel(&q->watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
1528static int
1529hfsc_dump_qdisc(struct Qdisc *sch, struct sk_buff *skb)
1530{
1531 struct hfsc_sched *q = qdisc_priv(sch);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001532 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 struct tc_hfsc_qopt qopt;
1534
1535 qopt.defcls = q->defcls;
David S. Miller1b34ec42012-03-29 05:11:39 -04001536 if (nla_put(skb, TCA_OPTIONS, sizeof(qopt), &qopt))
1537 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return skb->len;
1539
Patrick McHardy1e904742008-01-22 22:11:17 -08001540 nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001541 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return -1;
1543}
1544
1545static int
Eric Dumazet520ac302016-06-21 23:16:49 -07001546hfsc_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 struct hfsc_class *cl;
Ingo Molnardc0a0012008-11-25 16:50:02 -08001549 int uninitialized_var(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 cl = hfsc_classify(skb, sch, &err);
1552 if (cl == NULL) {
Jarek Poplawskic27f3392008-08-04 22:39:11 -07001553 if (err & __NET_XMIT_BYPASS)
John Fastabend25331d62014-09-28 11:53:29 -07001554 qdisc_qstats_drop(sch);
Eric Dumazet520ac302016-06-21 23:16:49 -07001555 __qdisc_drop(skb, to_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return err;
1557 }
1558
Eric Dumazet520ac302016-06-21 23:16:49 -07001559 err = qdisc_enqueue(skb, cl->qdisc, to_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (unlikely(err != NET_XMIT_SUCCESS)) {
Jarek Poplawski378a2f02008-08-04 22:31:03 -07001561 if (net_xmit_drop_count(err)) {
1562 cl->qstats.drops++;
John Fastabend25331d62014-09-28 11:53:29 -07001563 qdisc_qstats_drop(sch);
Jarek Poplawski378a2f02008-08-04 22:31:03 -07001564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return err;
1566 }
1567
Michal Soltys12d0ad32016-06-30 02:26:44 +02001568 if (cl->qdisc->q.qlen == 1) {
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -07001569 set_active(cl, qdisc_pkt_len(skb));
Michal Soltys12d0ad32016-06-30 02:26:44 +02001570 /*
1571 * If this is the first packet, isolate the head so an eventual
1572 * head drop before the first dequeue operation has no chance
1573 * to invalidate the deadline.
1574 */
1575 if (cl->cl_flags & HFSC_RSC)
1576 cl->qdisc->ops->peek(cl->qdisc);
1577
1578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
WANG Cong357cc9b2016-06-01 16:15:15 -07001580 qdisc_qstats_backlog_inc(sch, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 sch->q.qlen++;
1582
1583 return NET_XMIT_SUCCESS;
1584}
1585
1586static struct sk_buff *
1587hfsc_dequeue(struct Qdisc *sch)
1588{
1589 struct hfsc_sched *q = qdisc_priv(sch);
1590 struct hfsc_class *cl;
1591 struct sk_buff *skb;
1592 u64 cur_time;
1593 unsigned int next_len;
1594 int realtime = 0;
1595
1596 if (sch->q.qlen == 0)
1597 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Patrick McHardy3bebcda2007-03-23 11:29:25 -07001599 cur_time = psched_get_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 /*
1602 * if there are eligible classes, use real-time criteria.
1603 * find the class with the minimum deadline among
1604 * the eligible classes.
1605 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001606 cl = eltree_get_mindl(q, cur_time);
1607 if (cl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 realtime = 1;
1609 } else {
1610 /*
1611 * use link-sharing criteria
1612 * get the class with the minimum vt in the hierarchy
1613 */
1614 cl = vttree_get_minvt(&q->root, cur_time);
1615 if (cl == NULL) {
John Fastabend25331d62014-09-28 11:53:29 -07001616 qdisc_qstats_overlimit(sch);
Patrick McHardyed2b2292007-03-16 01:19:33 -07001617 hfsc_schedule_watchdog(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return NULL;
1619 }
1620 }
1621
Jarek Poplawski77be1552008-10-31 00:47:01 -07001622 skb = qdisc_dequeue_peeked(cl->qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (skb == NULL) {
Jarek Poplawskib00355d2009-02-01 01:12:42 -08001624 qdisc_warn_nonwc("HFSC", cl->qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return NULL;
1626 }
1627
Eric Dumazet2dd875f2012-05-10 05:36:34 +00001628 bstats_update(&cl->bstats, skb);
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -07001629 update_vf(cl, qdisc_pkt_len(skb), cur_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 if (realtime)
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -07001631 cl->cl_cumul += qdisc_pkt_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 if (cl->qdisc->q.qlen != 0) {
1634 if (cl->cl_flags & HFSC_RSC) {
1635 /* update ed */
1636 next_len = qdisc_peek_len(cl->qdisc);
1637 if (realtime)
1638 update_ed(cl, next_len);
1639 else
1640 update_d(cl, next_len);
1641 }
1642 } else {
1643 /* the class becomes passive */
1644 set_passive(cl);
1645 }
1646
Eric Dumazet9190b3b2011-01-20 23:31:33 -08001647 qdisc_bstats_update(sch, skb);
WANG Cong357cc9b2016-06-01 16:15:15 -07001648 qdisc_qstats_backlog_dec(sch, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 sch->q.qlen--;
1650
1651 return skb;
1652}
1653
Eric Dumazet20fea082007-11-14 01:44:41 -08001654static const struct Qdisc_class_ops hfsc_class_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 .change = hfsc_change_class,
1656 .delete = hfsc_delete_class,
1657 .graft = hfsc_graft_class,
1658 .leaf = hfsc_class_leaf,
Patrick McHardyf973b912006-11-29 17:36:43 -08001659 .qlen_notify = hfsc_qlen_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 .get = hfsc_get_class,
1661 .put = hfsc_put_class,
1662 .bind_tcf = hfsc_bind_tcf,
1663 .unbind_tcf = hfsc_unbind_tcf,
1664 .tcf_chain = hfsc_tcf_chain,
1665 .dump = hfsc_dump_class,
1666 .dump_stats = hfsc_dump_class_stats,
1667 .walk = hfsc_walk
1668};
1669
Eric Dumazet20fea082007-11-14 01:44:41 -08001670static struct Qdisc_ops hfsc_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 .id = "hfsc",
1672 .init = hfsc_init_qdisc,
1673 .change = hfsc_change_qdisc,
1674 .reset = hfsc_reset_qdisc,
1675 .destroy = hfsc_destroy_qdisc,
1676 .dump = hfsc_dump_qdisc,
1677 .enqueue = hfsc_enqueue,
1678 .dequeue = hfsc_dequeue,
Jarek Poplawski77be1552008-10-31 00:47:01 -07001679 .peek = qdisc_peek_dequeued,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 .cl_ops = &hfsc_class_ops,
1681 .priv_size = sizeof(struct hfsc_sched),
1682 .owner = THIS_MODULE
1683};
1684
1685static int __init
1686hfsc_init(void)
1687{
1688 return register_qdisc(&hfsc_qdisc_ops);
1689}
1690
1691static void __exit
1692hfsc_cleanup(void)
1693{
1694 unregister_qdisc(&hfsc_qdisc_ops);
1695}
1696
1697MODULE_LICENSE("GPL");
1698module_init(hfsc_init);
1699module_exit(hfsc_cleanup);