xref: /linux/net/sched/sch_hfsc.c (revision 0b897fbd900e12a08baa3d1a0457944046a882ea)
1 /*
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>
53 #include <linux/module.h>
54 #include <linux/types.h>
55 #include <linux/errno.h>
56 #include <linux/compiler.h>
57 #include <linux/spinlock.h>
58 #include <linux/skbuff.h>
59 #include <linux/string.h>
60 #include <linux/slab.h>
61 #include <linux/list.h>
62 #include <linux/rbtree.h>
63 #include <linux/init.h>
64 #include <linux/rtnetlink.h>
65 #include <linux/pkt_sched.h>
66 #include <net/netlink.h>
67 #include <net/pkt_sched.h>
68 #include <net/pkt_cls.h>
69 #include <asm/div64.h>
70 
71 /*
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
80  *   segment are kept in order to avoid 64-bit divide operations
81  *   that are expensive on 32-bit architectures.
82  */
83 
84 struct internal_sc {
85 	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 */
94 struct runtime_sc {
95 	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 
105 enum hfsc_class_flags {
106 	HFSC_RSC = 0x1,
107 	HFSC_FSC = 0x2,
108 	HFSC_USC = 0x4
109 };
110 
111 struct hfsc_class {
112 	struct Qdisc_class_common cl_common;
113 
114 	struct gnet_stats_basic_sync bstats;
115 	struct gnet_stats_queue qstats;
116 	struct net_rate_estimator __rcu *rate_est;
117 	struct tcf_proto __rcu *filter_list; /* filter list */
118 	struct tcf_block *block;
119 	unsigned int	level;		/* class level in hierarchy */
120 
121 	struct hfsc_sched *sched;	/* scheduler data */
122 	struct hfsc_class *cl_parent;	/* parent class */
123 	struct list_head siblings;	/* sibling classes */
124 	struct list_head children;	/* child classes */
125 	struct Qdisc	*qdisc;		/* leaf qdisc */
126 
127 	struct rb_node el_node;		/* qdisc's eligible tree member */
128 	struct rb_root vt_tree;		/* active children sorted by cl_vt */
129 	struct rb_node vt_node;		/* parent's vt_tree member */
130 	struct rb_root cf_tree;		/* active children sorted by cl_f */
131 	struct rb_node cf_node;		/* parent's cf_heap member */
132 
133 	u64	cl_total;		/* total work in bytes */
134 	u64	cl_cumul;		/* cumulative work in bytes done by
135 					   real-time criteria */
136 
137 	u64	cl_d;			/* deadline*/
138 	u64	cl_e;			/* eligible time */
139 	u64	cl_vt;			/* virtual time */
140 	u64	cl_f;			/* time when this class will fit for
141 					   link-sharing, max(myf, cfmin) */
142 	u64	cl_myf;			/* my fit-time (calculated from this
143 					   class's own upperlimit curve) */
144 	u64	cl_cfmin;		/* earliest children's fit-time (used
145 					   with cl_myf to obtain cl_f) */
146 	u64	cl_cvtmin;		/* minimal virtual time among the
147 					   children fit for link-sharing
148 					   (monotonic within a period) */
149 	u64	cl_vtadj;		/* intra-period cumulative vt
150 					   adjustment */
151 	u64	cl_cvtoff;		/* largest virtual time seen among
152 					   the children */
153 
154 	struct internal_sc cl_rsc;	/* internal real-time service curve */
155 	struct internal_sc cl_fsc;	/* internal fair service curve */
156 	struct internal_sc cl_usc;	/* internal upperlimit service curve */
157 	struct runtime_sc cl_deadline;	/* deadline curve */
158 	struct runtime_sc cl_eligible;	/* eligible curve */
159 	struct runtime_sc cl_virtual;	/* virtual curve */
160 	struct runtime_sc cl_ulimit;	/* upperlimit curve */
161 
162 	u8		cl_flags;	/* which curves are valid */
163 	u32		cl_vtperiod;	/* vt period sequence number */
164 	u32		cl_parentperiod;/* parent's vt period sequence number*/
165 	u32		cl_nactive;	/* number of active children */
166 };
167 
168 struct hfsc_sched {
169 	u16	defcls;				/* default class id */
170 	struct hfsc_class root;			/* root class */
171 	struct Qdisc_class_hash clhash;		/* class hash */
172 	struct rb_root eligible;		/* eligible tree */
173 	struct qdisc_watchdog watchdog;		/* watchdog timer */
174 };
175 
176 #define	HT_INFINITY	0xffffffffffffffffULL	/* infinite time value */
177 
178 
179 /*
180  * eligible tree holds backlogged classes being sorted by their eligible times.
181  * there is one eligible tree per hfsc instance.
182  */
183 
184 static void
185 eltree_insert(struct hfsc_class *cl)
186 {
187 	struct rb_node **p = &cl->sched->eligible.rb_node;
188 	struct rb_node *parent = NULL;
189 	struct hfsc_class *cl1;
190 
191 	while (*p != NULL) {
192 		parent = *p;
193 		cl1 = rb_entry(parent, struct hfsc_class, el_node);
194 		if (cl->cl_e >= cl1->cl_e)
195 			p = &parent->rb_right;
196 		else
197 			p = &parent->rb_left;
198 	}
199 	rb_link_node(&cl->el_node, parent, p);
200 	rb_insert_color(&cl->el_node, &cl->sched->eligible);
201 }
202 
203 static inline void
204 eltree_remove(struct hfsc_class *cl)
205 {
206 	if (!RB_EMPTY_NODE(&cl->el_node)) {
207 		rb_erase(&cl->el_node, &cl->sched->eligible);
208 		RB_CLEAR_NODE(&cl->el_node);
209 	}
210 }
211 
212 static inline void
213 eltree_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 */
220 static inline struct hfsc_class *
221 eltree_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 */
237 static inline struct hfsc_class *
238 eltree_get_minel(struct hfsc_sched *q)
239 {
240 	struct rb_node *n;
241 
242 	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  */
252 static void
253 vttree_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 
271 static inline void
272 vttree_remove(struct hfsc_class *cl)
273 {
274 	rb_erase(&cl->vt_node, &cl->cl_parent->vt_tree);
275 }
276 
277 static inline void
278 vttree_update(struct hfsc_class *cl)
279 {
280 	vttree_remove(cl);
281 	vttree_insert(cl);
282 }
283 
284 static inline struct hfsc_class *
285 vttree_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  */
301 static struct hfsc_class *
302 vttree_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 
321 static void
322 cftree_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 
340 static inline void
341 cftree_remove(struct hfsc_class *cl)
342 {
343 	rb_erase(&cl->cf_node, &cl->cl_parent->cf_tree);
344 }
345 
346 static inline void
347 cftree_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  *
364  * The clock source resolution with ktime and PSCHED_SHIFT 10 is 1.024us.
365  *
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  *
370  *  bits/sec      100Kbps     1Mbps     10Mbps     100Mbps    1Gbps
371  *  ------------+-------------------------------------------------------
372  *  bytes/1.024us 12.8e-3    128e-3     1280e-3    12800e-3   128000e-3
373  *
374  *  1.024us/byte  78.125     7.8125     0.78125    0.078125   0.0078125
375  *
376  * So, for PSCHED_SHIFT 10 we need: SM_SHIFT 20, ISM_SHIFT 18.
377  */
378 #define	SM_SHIFT	(30 - PSCHED_SHIFT)
379 #define	ISM_SHIFT	(8 + PSCHED_SHIFT)
380 
381 #define	SM_MASK		((1ULL << SM_SHIFT) - 1)
382 #define	ISM_MASK	((1ULL << ISM_SHIFT) - 1)
383 
384 static inline u64
385 seg_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 
398 static inline u64
399 seg_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) */
415 static u64
416 m2sm(u32 m)
417 {
418 	u64 sm;
419 
420 	sm = ((u64)m << SM_SHIFT);
421 	sm += PSCHED_TICKS_PER_SEC - 1;
422 	do_div(sm, PSCHED_TICKS_PER_SEC);
423 	return sm;
424 }
425 
426 /* convert m (bps) into ism (psched us/byte) */
427 static u64
428 m2ism(u32 m)
429 {
430 	u64 ism;
431 
432 	if (m == 0)
433 		ism = HT_INFINITY;
434 	else {
435 		ism = ((u64)PSCHED_TICKS_PER_SEC << ISM_SHIFT);
436 		ism += m - 1;
437 		do_div(ism, m);
438 	}
439 	return ism;
440 }
441 
442 /* convert d (us) into dx (psched us) */
443 static u64
444 d2dx(u32 d)
445 {
446 	u64 dx;
447 
448 	dx = ((u64)d * PSCHED_TICKS_PER_SEC);
449 	dx += USEC_PER_SEC - 1;
450 	do_div(dx, USEC_PER_SEC);
451 	return dx;
452 }
453 
454 /* convert sm (bytes/psched us) into m (bps) */
455 static u32
456 sm2m(u64 sm)
457 {
458 	u64 m;
459 
460 	m = (sm * PSCHED_TICKS_PER_SEC) >> SM_SHIFT;
461 	return (u32)m;
462 }
463 
464 /* convert dx (psched us) into d (us) */
465 static u32
466 dx2d(u64 dx)
467 {
468 	u64 d;
469 
470 	d = dx * USEC_PER_SEC;
471 	do_div(d, PSCHED_TICKS_PER_SEC);
472 	return (u32)d;
473 }
474 
475 static void
476 sc2isc(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  */
490 static void
491 rtsc_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  */
507 static u64
508 rtsc_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 
528 static u64
529 rtsc_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  */
549 static void
550 rtsc_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;
609 }
610 
611 static void
612 init_ed(struct hfsc_class *cl, unsigned int next_len)
613 {
614 	u64 cur_time = psched_get_time();
615 
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 
637 static void
638 update_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 
646 static inline void
647 update_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 
652 static inline void
653 update_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 
666 static void
667 init_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) {
685 				max_cl = rb_entry(n, struct hfsc_class, vt_node);
686 				/*
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.
701 				 * 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.
704 				 */
705 				cl->cl_vt = cl->cl_parent->cl_cvtoff;
706 				cl->cl_parent->cl_cvtmin = 0;
707 			}
708 
709 			/* update the virtual curve */
710 			rtsc_min(&cl->cl_virtual, &cl->cl_fsc, cl->cl_vt, cl->cl_total);
711 			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)
725 					cur_time = psched_get_time();
726 
727 				/* update the ulimit curve */
728 				rtsc_min(&cl->cl_ulimit, &cl->cl_usc, cur_time,
729 					 cl->cl_total);
730 				/* compute myf */
731 				cl->cl_myf = rtsc_y2x(&cl->cl_ulimit,
732 						      cl->cl_total);
733 			}
734 		}
735 
736 		f = max(cl->cl_myf, cl->cl_cfmin);
737 		if (f != cl->cl_f) {
738 			cl->cl_f = f;
739 			cftree_update(cl);
740 		}
741 		update_cfmin(cl->cl_parent);
742 	}
743 }
744 
745 static void
746 update_vf(struct hfsc_class *cl, unsigned int len, u64 cur_time)
747 {
748 	u64 f; /* , myf_bound, delta; */
749 	int go_passive = 0;
750 
751 	if (cl->qdisc->q.qlen == 0 && cl->cl_flags & HFSC_FSC)
752 		go_passive = 1;
753 
754 	for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
755 		cl->cl_total += len;
756 
757 		if (!(cl->cl_flags & HFSC_FSC) || cl->cl_nactive == 0)
758 			continue;
759 
760 		if (go_passive && --cl->cl_nactive == 0)
761 			go_passive = 1;
762 		else
763 			go_passive = 0;
764 
765 		/* update vt */
766 		cl->cl_vt = rtsc_y2x(&cl->cl_virtual, cl->cl_total) + cl->cl_vtadj;
767 
768 		/*
769 		 * if vt of the class is smaller than cvtmin,
770 		 * the class was skipped in the past due to non-fit.
771 		 * if so, we need to adjust vtadj.
772 		 */
773 		if (cl->cl_vt < cl->cl_parent->cl_cvtmin) {
774 			cl->cl_vtadj += cl->cl_parent->cl_cvtmin - cl->cl_vt;
775 			cl->cl_vt = cl->cl_parent->cl_cvtmin;
776 		}
777 
778 		if (go_passive) {
779 			/* no more active child, going passive */
780 
781 			/* update cvtoff of the parent class */
782 			if (cl->cl_vt > cl->cl_parent->cl_cvtoff)
783 				cl->cl_parent->cl_cvtoff = cl->cl_vt;
784 
785 			/* remove this class from the vt tree */
786 			vttree_remove(cl);
787 
788 			cftree_remove(cl);
789 			update_cfmin(cl->cl_parent);
790 
791 			continue;
792 		}
793 
794 		/* update the vt tree */
795 		vttree_update(cl);
796 
797 		/* update f */
798 		if (cl->cl_flags & HFSC_USC) {
799 			cl->cl_myf = rtsc_y2x(&cl->cl_ulimit, cl->cl_total);
800 #if 0
801 			cl->cl_myf = cl->cl_myfadj + rtsc_y2x(&cl->cl_ulimit,
802 							      cl->cl_total);
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 
833 static unsigned int
834 qdisc_peek_len(struct Qdisc *sch)
835 {
836 	struct sk_buff *skb;
837 	unsigned int len;
838 
839 	skb = sch->ops->peek(sch);
840 	if (unlikely(skb == NULL)) {
841 		qdisc_warn_nonwc("qdisc_peek_len", sch);
842 		return 0;
843 	}
844 	len = qdisc_pkt_len(skb);
845 
846 	return len;
847 }
848 
849 static void
850 hfsc_adjust_levels(struct hfsc_class *cl)
851 {
852 	struct hfsc_class *p;
853 	unsigned int level;
854 
855 	do {
856 		level = 0;
857 		list_for_each_entry(p, &cl->children, siblings) {
858 			if (p->level >= level)
859 				level = p->level + 1;
860 		}
861 		cl->level = level;
862 	} while ((cl = cl->cl_parent) != NULL);
863 }
864 
865 static inline struct hfsc_class *
866 hfsc_find_class(u32 classid, struct Qdisc *sch)
867 {
868 	struct hfsc_sched *q = qdisc_priv(sch);
869 	struct Qdisc_class_common *clc;
870 
871 	clc = qdisc_class_find(&q->clhash, classid);
872 	if (clc == NULL)
873 		return NULL;
874 	return container_of(clc, struct hfsc_class, cl_common);
875 }
876 
877 static void
878 hfsc_change_rsc(struct hfsc_class *cl, struct tc_service_curve *rsc,
879 		u64 cur_time)
880 {
881 	sc2isc(rsc, &cl->cl_rsc);
882 	rtsc_init(&cl->cl_deadline, &cl->cl_rsc, cur_time, cl->cl_cumul);
883 	cl->cl_eligible = cl->cl_deadline;
884 	if (cl->cl_rsc.sm1 <= cl->cl_rsc.sm2) {
885 		cl->cl_eligible.dx = 0;
886 		cl->cl_eligible.dy = 0;
887 	}
888 	cl->cl_flags |= HFSC_RSC;
889 }
890 
891 static void
892 hfsc_change_fsc(struct hfsc_class *cl, struct tc_service_curve *fsc)
893 {
894 	sc2isc(fsc, &cl->cl_fsc);
895 	rtsc_init(&cl->cl_virtual, &cl->cl_fsc, cl->cl_vt, cl->cl_total);
896 	cl->cl_flags |= HFSC_FSC;
897 }
898 
899 static void
900 hfsc_change_usc(struct hfsc_class *cl, struct tc_service_curve *usc,
901 		u64 cur_time)
902 {
903 	sc2isc(usc, &cl->cl_usc);
904 	rtsc_init(&cl->cl_ulimit, &cl->cl_usc, cur_time, cl->cl_total);
905 	cl->cl_flags |= HFSC_USC;
906 }
907 
908 static void
909 hfsc_upgrade_rt(struct hfsc_class *cl)
910 {
911 	cl->cl_fsc = cl->cl_rsc;
912 	rtsc_init(&cl->cl_virtual, &cl->cl_fsc, cl->cl_vt, cl->cl_total);
913 	cl->cl_flags |= HFSC_FSC;
914 }
915 
916 static const struct nla_policy hfsc_policy[TCA_HFSC_MAX + 1] = {
917 	[TCA_HFSC_RSC]	= { .len = sizeof(struct tc_service_curve) },
918 	[TCA_HFSC_FSC]	= { .len = sizeof(struct tc_service_curve) },
919 	[TCA_HFSC_USC]	= { .len = sizeof(struct tc_service_curve) },
920 };
921 
922 static int
923 hfsc_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
924 		  struct nlattr **tca, unsigned long *arg,
925 		  struct netlink_ext_ack *extack)
926 {
927 	struct hfsc_sched *q = qdisc_priv(sch);
928 	struct hfsc_class *cl = (struct hfsc_class *)*arg;
929 	struct hfsc_class *parent = NULL;
930 	struct nlattr *opt = tca[TCA_OPTIONS];
931 	struct nlattr *tb[TCA_HFSC_MAX + 1];
932 	struct tc_service_curve *rsc = NULL, *fsc = NULL, *usc = NULL;
933 	u64 cur_time;
934 	int err;
935 
936 	if (opt == NULL)
937 		return -EINVAL;
938 
939 	err = nla_parse_nested_deprecated(tb, TCA_HFSC_MAX, opt, hfsc_policy,
940 					  NULL);
941 	if (err < 0)
942 		return err;
943 
944 	if (tb[TCA_HFSC_RSC]) {
945 		rsc = nla_data(tb[TCA_HFSC_RSC]);
946 		if (rsc->m1 == 0 && rsc->m2 == 0)
947 			rsc = NULL;
948 	}
949 
950 	if (tb[TCA_HFSC_FSC]) {
951 		fsc = nla_data(tb[TCA_HFSC_FSC]);
952 		if (fsc->m1 == 0 && fsc->m2 == 0)
953 			fsc = NULL;
954 	}
955 
956 	if (tb[TCA_HFSC_USC]) {
957 		usc = nla_data(tb[TCA_HFSC_USC]);
958 		if (usc->m1 == 0 && usc->m2 == 0)
959 			usc = NULL;
960 	}
961 
962 	if (cl != NULL) {
963 		int old_flags;
964 
965 		if (parentid) {
966 			if (cl->cl_parent &&
967 			    cl->cl_parent->cl_common.classid != parentid)
968 				return -EINVAL;
969 			if (cl->cl_parent == NULL && parentid != TC_H_ROOT)
970 				return -EINVAL;
971 		}
972 		cur_time = psched_get_time();
973 
974 		if (tca[TCA_RATE]) {
975 			err = gen_replace_estimator(&cl->bstats, NULL,
976 						    &cl->rate_est,
977 						    NULL,
978 						    true,
979 						    tca[TCA_RATE]);
980 			if (err)
981 				return err;
982 		}
983 
984 		sch_tree_lock(sch);
985 		old_flags = cl->cl_flags;
986 
987 		if (rsc != NULL)
988 			hfsc_change_rsc(cl, rsc, cur_time);
989 		if (fsc != NULL)
990 			hfsc_change_fsc(cl, fsc);
991 		if (usc != NULL)
992 			hfsc_change_usc(cl, usc, cur_time);
993 
994 		if (cl->qdisc->q.qlen != 0) {
995 			int len = qdisc_peek_len(cl->qdisc);
996 
997 			if (cl->cl_flags & HFSC_RSC) {
998 				if (old_flags & HFSC_RSC)
999 					update_ed(cl, len);
1000 				else
1001 					init_ed(cl, len);
1002 			}
1003 
1004 			if (cl->cl_flags & HFSC_FSC) {
1005 				if (old_flags & HFSC_FSC)
1006 					update_vf(cl, 0, cur_time);
1007 				else
1008 					init_vf(cl, len);
1009 			}
1010 		}
1011 		sch_tree_unlock(sch);
1012 
1013 		return 0;
1014 	}
1015 
1016 	if (parentid == TC_H_ROOT)
1017 		return -EEXIST;
1018 
1019 	parent = &q->root;
1020 	if (parentid) {
1021 		parent = hfsc_find_class(parentid, sch);
1022 		if (parent == NULL)
1023 			return -ENOENT;
1024 	}
1025 
1026 	if (classid == 0 || TC_H_MAJ(classid ^ sch->handle) != 0)
1027 		return -EINVAL;
1028 	if (hfsc_find_class(classid, sch))
1029 		return -EEXIST;
1030 
1031 	if (rsc == NULL && fsc == NULL)
1032 		return -EINVAL;
1033 
1034 	cl = kzalloc(sizeof(struct hfsc_class), GFP_KERNEL);
1035 	if (cl == NULL)
1036 		return -ENOBUFS;
1037 
1038 	err = tcf_block_get(&cl->block, &cl->filter_list, sch, extack);
1039 	if (err) {
1040 		kfree(cl);
1041 		return err;
1042 	}
1043 
1044 	if (tca[TCA_RATE]) {
1045 		err = gen_new_estimator(&cl->bstats, NULL, &cl->rate_est,
1046 					NULL, true, tca[TCA_RATE]);
1047 		if (err) {
1048 			tcf_block_put(cl->block);
1049 			kfree(cl);
1050 			return err;
1051 		}
1052 	}
1053 
1054 	if (rsc != NULL)
1055 		hfsc_change_rsc(cl, rsc, 0);
1056 	if (fsc != NULL)
1057 		hfsc_change_fsc(cl, fsc);
1058 	if (usc != NULL)
1059 		hfsc_change_usc(cl, usc, 0);
1060 
1061 	cl->cl_common.classid = classid;
1062 	cl->sched     = q;
1063 	cl->cl_parent = parent;
1064 	cl->qdisc = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
1065 				      classid, NULL);
1066 	if (cl->qdisc == NULL)
1067 		cl->qdisc = &noop_qdisc;
1068 	else
1069 		qdisc_hash_add(cl->qdisc, true);
1070 	INIT_LIST_HEAD(&cl->children);
1071 	cl->vt_tree = RB_ROOT;
1072 	cl->cf_tree = RB_ROOT;
1073 
1074 	sch_tree_lock(sch);
1075 	/* Check if the inner class is a misconfigured 'rt' */
1076 	if (!(parent->cl_flags & HFSC_FSC) && parent != &q->root) {
1077 		NL_SET_ERR_MSG(extack,
1078 			       "Forced curve change on parent 'rt' to 'sc'");
1079 		hfsc_upgrade_rt(parent);
1080 	}
1081 	qdisc_class_hash_insert(&q->clhash, &cl->cl_common);
1082 	list_add_tail(&cl->siblings, &parent->children);
1083 	if (parent->level == 0)
1084 		qdisc_purge_queue(parent->qdisc);
1085 	hfsc_adjust_levels(parent);
1086 	sch_tree_unlock(sch);
1087 
1088 	qdisc_class_hash_grow(sch, &q->clhash);
1089 
1090 	*arg = (unsigned long)cl;
1091 	return 0;
1092 }
1093 
1094 static void
1095 hfsc_destroy_class(struct Qdisc *sch, struct hfsc_class *cl)
1096 {
1097 	struct hfsc_sched *q = qdisc_priv(sch);
1098 
1099 	tcf_block_put(cl->block);
1100 	qdisc_put(cl->qdisc);
1101 	gen_kill_estimator(&cl->rate_est);
1102 	if (cl != &q->root)
1103 		kfree(cl);
1104 }
1105 
1106 static int
1107 hfsc_delete_class(struct Qdisc *sch, unsigned long arg,
1108 		  struct netlink_ext_ack *extack)
1109 {
1110 	struct hfsc_sched *q = qdisc_priv(sch);
1111 	struct hfsc_class *cl = (struct hfsc_class *)arg;
1112 
1113 	if (cl->level > 0 || qdisc_class_in_use(&cl->cl_common) ||
1114 	    cl == &q->root) {
1115 		NL_SET_ERR_MSG(extack, "HFSC class in use");
1116 		return -EBUSY;
1117 	}
1118 
1119 	sch_tree_lock(sch);
1120 
1121 	list_del(&cl->siblings);
1122 	hfsc_adjust_levels(cl->cl_parent);
1123 
1124 	qdisc_purge_queue(cl->qdisc);
1125 	qdisc_class_hash_remove(&q->clhash, &cl->cl_common);
1126 
1127 	sch_tree_unlock(sch);
1128 
1129 	hfsc_destroy_class(sch, cl);
1130 	return 0;
1131 }
1132 
1133 static struct hfsc_class *
1134 hfsc_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
1135 {
1136 	struct hfsc_sched *q = qdisc_priv(sch);
1137 	struct hfsc_class *head, *cl;
1138 	struct tcf_result res;
1139 	struct tcf_proto *tcf;
1140 	int result;
1141 
1142 	if (TC_H_MAJ(skb->priority ^ sch->handle) == 0 &&
1143 	    (cl = hfsc_find_class(skb->priority, sch)) != NULL)
1144 		if (cl->level == 0)
1145 			return cl;
1146 
1147 	*qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
1148 	head = &q->root;
1149 	tcf = rcu_dereference_bh(q->root.filter_list);
1150 	while (tcf && (result = tcf_classify(skb, NULL, tcf, &res, false)) >= 0) {
1151 #ifdef CONFIG_NET_CLS_ACT
1152 		switch (result) {
1153 		case TC_ACT_QUEUED:
1154 		case TC_ACT_STOLEN:
1155 		case TC_ACT_TRAP:
1156 			*qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
1157 			fallthrough;
1158 		case TC_ACT_SHOT:
1159 			return NULL;
1160 		}
1161 #endif
1162 		cl = (struct hfsc_class *)res.class;
1163 		if (!cl) {
1164 			cl = hfsc_find_class(res.classid, sch);
1165 			if (!cl)
1166 				break; /* filter selected invalid classid */
1167 			if (cl->level >= head->level)
1168 				break; /* filter may only point downwards */
1169 		}
1170 
1171 		if (cl->level == 0)
1172 			return cl; /* hit leaf class */
1173 
1174 		/* apply inner filter chain */
1175 		tcf = rcu_dereference_bh(cl->filter_list);
1176 		head = cl;
1177 	}
1178 
1179 	/* classification failed, try default class */
1180 	cl = hfsc_find_class(TC_H_MAKE(TC_H_MAJ(sch->handle),
1181 				       READ_ONCE(q->defcls)), sch);
1182 	if (cl == NULL || cl->level > 0)
1183 		return NULL;
1184 
1185 	return cl;
1186 }
1187 
1188 static int
1189 hfsc_graft_class(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
1190 		 struct Qdisc **old, struct netlink_ext_ack *extack)
1191 {
1192 	struct hfsc_class *cl = (struct hfsc_class *)arg;
1193 
1194 	if (cl->level > 0)
1195 		return -EINVAL;
1196 	if (new == NULL) {
1197 		new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
1198 					cl->cl_common.classid, NULL);
1199 		if (new == NULL)
1200 			new = &noop_qdisc;
1201 	}
1202 
1203 	*old = qdisc_replace(sch, new, &cl->qdisc);
1204 	return 0;
1205 }
1206 
1207 static struct Qdisc *
1208 hfsc_class_leaf(struct Qdisc *sch, unsigned long arg)
1209 {
1210 	struct hfsc_class *cl = (struct hfsc_class *)arg;
1211 
1212 	if (cl->level == 0)
1213 		return cl->qdisc;
1214 
1215 	return NULL;
1216 }
1217 
1218 static void
1219 hfsc_qlen_notify(struct Qdisc *sch, unsigned long arg)
1220 {
1221 	struct hfsc_class *cl = (struct hfsc_class *)arg;
1222 
1223 	/* vttree is now handled in update_vf() so that update_vf(cl, 0, 0)
1224 	 * needs to be called explicitly to remove a class from vttree.
1225 	 */
1226 	if (cl->cl_nactive)
1227 		update_vf(cl, 0, 0);
1228 	if (cl->cl_flags & HFSC_RSC)
1229 		eltree_remove(cl);
1230 }
1231 
1232 static unsigned long
1233 hfsc_search_class(struct Qdisc *sch, u32 classid)
1234 {
1235 	return (unsigned long)hfsc_find_class(classid, sch);
1236 }
1237 
1238 static unsigned long
1239 hfsc_bind_tcf(struct Qdisc *sch, unsigned long parent, u32 classid)
1240 {
1241 	struct hfsc_class *p = (struct hfsc_class *)parent;
1242 	struct hfsc_class *cl = hfsc_find_class(classid, sch);
1243 
1244 	if (cl != NULL) {
1245 		if (p != NULL && p->level <= cl->level)
1246 			return 0;
1247 		qdisc_class_get(&cl->cl_common);
1248 	}
1249 
1250 	return (unsigned long)cl;
1251 }
1252 
1253 static void
1254 hfsc_unbind_tcf(struct Qdisc *sch, unsigned long arg)
1255 {
1256 	struct hfsc_class *cl = (struct hfsc_class *)arg;
1257 
1258 	qdisc_class_put(&cl->cl_common);
1259 }
1260 
1261 static struct tcf_block *hfsc_tcf_block(struct Qdisc *sch, unsigned long arg,
1262 					struct netlink_ext_ack *extack)
1263 {
1264 	struct hfsc_sched *q = qdisc_priv(sch);
1265 	struct hfsc_class *cl = (struct hfsc_class *)arg;
1266 
1267 	if (cl == NULL)
1268 		cl = &q->root;
1269 
1270 	return cl->block;
1271 }
1272 
1273 static int
1274 hfsc_dump_sc(struct sk_buff *skb, int attr, struct internal_sc *sc)
1275 {
1276 	struct tc_service_curve tsc;
1277 
1278 	tsc.m1 = sm2m(sc->sm1);
1279 	tsc.d  = dx2d(sc->dx);
1280 	tsc.m2 = sm2m(sc->sm2);
1281 	if (nla_put(skb, attr, sizeof(tsc), &tsc))
1282 		goto nla_put_failure;
1283 
1284 	return skb->len;
1285 
1286  nla_put_failure:
1287 	return -1;
1288 }
1289 
1290 static int
1291 hfsc_dump_curves(struct sk_buff *skb, struct hfsc_class *cl)
1292 {
1293 	if ((cl->cl_flags & HFSC_RSC) &&
1294 	    (hfsc_dump_sc(skb, TCA_HFSC_RSC, &cl->cl_rsc) < 0))
1295 		goto nla_put_failure;
1296 
1297 	if ((cl->cl_flags & HFSC_FSC) &&
1298 	    (hfsc_dump_sc(skb, TCA_HFSC_FSC, &cl->cl_fsc) < 0))
1299 		goto nla_put_failure;
1300 
1301 	if ((cl->cl_flags & HFSC_USC) &&
1302 	    (hfsc_dump_sc(skb, TCA_HFSC_USC, &cl->cl_usc) < 0))
1303 		goto nla_put_failure;
1304 
1305 	return skb->len;
1306 
1307  nla_put_failure:
1308 	return -1;
1309 }
1310 
1311 static int
1312 hfsc_dump_class(struct Qdisc *sch, unsigned long arg, struct sk_buff *skb,
1313 		struct tcmsg *tcm)
1314 {
1315 	struct hfsc_class *cl = (struct hfsc_class *)arg;
1316 	struct nlattr *nest;
1317 
1318 	tcm->tcm_parent = cl->cl_parent ? cl->cl_parent->cl_common.classid :
1319 					  TC_H_ROOT;
1320 	tcm->tcm_handle = cl->cl_common.classid;
1321 	if (cl->level == 0)
1322 		tcm->tcm_info = cl->qdisc->handle;
1323 
1324 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
1325 	if (nest == NULL)
1326 		goto nla_put_failure;
1327 	if (hfsc_dump_curves(skb, cl) < 0)
1328 		goto nla_put_failure;
1329 	return nla_nest_end(skb, nest);
1330 
1331  nla_put_failure:
1332 	nla_nest_cancel(skb, nest);
1333 	return -EMSGSIZE;
1334 }
1335 
1336 static int
1337 hfsc_dump_class_stats(struct Qdisc *sch, unsigned long arg,
1338 	struct gnet_dump *d)
1339 {
1340 	struct hfsc_class *cl = (struct hfsc_class *)arg;
1341 	struct tc_hfsc_stats xstats;
1342 	__u32 qlen;
1343 
1344 	qdisc_qstats_qlen_backlog(cl->qdisc, &qlen, &cl->qstats.backlog);
1345 	xstats.level   = cl->level;
1346 	xstats.period  = cl->cl_vtperiod;
1347 	xstats.work    = cl->cl_total;
1348 	xstats.rtwork  = cl->cl_cumul;
1349 
1350 	if (gnet_stats_copy_basic(d, NULL, &cl->bstats, true) < 0 ||
1351 	    gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
1352 	    gnet_stats_copy_queue(d, NULL, &cl->qstats, qlen) < 0)
1353 		return -1;
1354 
1355 	return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
1356 }
1357 
1358 
1359 
1360 static void
1361 hfsc_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1362 {
1363 	struct hfsc_sched *q = qdisc_priv(sch);
1364 	struct hfsc_class *cl;
1365 	unsigned int i;
1366 
1367 	if (arg->stop)
1368 		return;
1369 
1370 	for (i = 0; i < q->clhash.hashsize; i++) {
1371 		hlist_for_each_entry(cl, &q->clhash.hash[i],
1372 				     cl_common.hnode) {
1373 			if (!tc_qdisc_stats_dump(sch, (unsigned long)cl, arg))
1374 				return;
1375 		}
1376 	}
1377 }
1378 
1379 static void
1380 hfsc_schedule_watchdog(struct Qdisc *sch)
1381 {
1382 	struct hfsc_sched *q = qdisc_priv(sch);
1383 	struct hfsc_class *cl;
1384 	u64 next_time = 0;
1385 
1386 	cl = eltree_get_minel(q);
1387 	if (cl)
1388 		next_time = cl->cl_e;
1389 	if (q->root.cl_cfmin != 0) {
1390 		if (next_time == 0 || next_time > q->root.cl_cfmin)
1391 			next_time = q->root.cl_cfmin;
1392 	}
1393 	if (next_time)
1394 		qdisc_watchdog_schedule(&q->watchdog, next_time);
1395 }
1396 
1397 static int
1398 hfsc_init_qdisc(struct Qdisc *sch, struct nlattr *opt,
1399 		struct netlink_ext_ack *extack)
1400 {
1401 	struct hfsc_sched *q = qdisc_priv(sch);
1402 	struct tc_hfsc_qopt *qopt;
1403 	int err;
1404 
1405 	qdisc_watchdog_init(&q->watchdog, sch);
1406 
1407 	if (!opt || nla_len(opt) < sizeof(*qopt))
1408 		return -EINVAL;
1409 	qopt = nla_data(opt);
1410 
1411 	q->defcls = qopt->defcls;
1412 	err = qdisc_class_hash_init(&q->clhash);
1413 	if (err < 0)
1414 		return err;
1415 	q->eligible = RB_ROOT;
1416 
1417 	err = tcf_block_get(&q->root.block, &q->root.filter_list, sch, extack);
1418 	if (err)
1419 		return err;
1420 
1421 	gnet_stats_basic_sync_init(&q->root.bstats);
1422 	q->root.cl_common.classid = sch->handle;
1423 	q->root.sched   = q;
1424 	q->root.qdisc = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
1425 					  sch->handle, NULL);
1426 	if (q->root.qdisc == NULL)
1427 		q->root.qdisc = &noop_qdisc;
1428 	else
1429 		qdisc_hash_add(q->root.qdisc, true);
1430 	INIT_LIST_HEAD(&q->root.children);
1431 	q->root.vt_tree = RB_ROOT;
1432 	q->root.cf_tree = RB_ROOT;
1433 
1434 	qdisc_class_hash_insert(&q->clhash, &q->root.cl_common);
1435 	qdisc_class_hash_grow(sch, &q->clhash);
1436 
1437 	return 0;
1438 }
1439 
1440 static int
1441 hfsc_change_qdisc(struct Qdisc *sch, struct nlattr *opt,
1442 		  struct netlink_ext_ack *extack)
1443 {
1444 	struct hfsc_sched *q = qdisc_priv(sch);
1445 	struct tc_hfsc_qopt *qopt;
1446 
1447 	if (nla_len(opt) < sizeof(*qopt))
1448 		return -EINVAL;
1449 	qopt = nla_data(opt);
1450 
1451 	WRITE_ONCE(q->defcls, qopt->defcls);
1452 
1453 	return 0;
1454 }
1455 
1456 static void
1457 hfsc_reset_class(struct hfsc_class *cl)
1458 {
1459 	cl->cl_total        = 0;
1460 	cl->cl_cumul        = 0;
1461 	cl->cl_d            = 0;
1462 	cl->cl_e            = 0;
1463 	cl->cl_vt           = 0;
1464 	cl->cl_vtadj        = 0;
1465 	cl->cl_cvtmin       = 0;
1466 	cl->cl_cvtoff       = 0;
1467 	cl->cl_vtperiod     = 0;
1468 	cl->cl_parentperiod = 0;
1469 	cl->cl_f            = 0;
1470 	cl->cl_myf          = 0;
1471 	cl->cl_cfmin        = 0;
1472 	cl->cl_nactive      = 0;
1473 
1474 	cl->vt_tree = RB_ROOT;
1475 	cl->cf_tree = RB_ROOT;
1476 	qdisc_reset(cl->qdisc);
1477 
1478 	if (cl->cl_flags & HFSC_RSC)
1479 		rtsc_init(&cl->cl_deadline, &cl->cl_rsc, 0, 0);
1480 	if (cl->cl_flags & HFSC_FSC)
1481 		rtsc_init(&cl->cl_virtual, &cl->cl_fsc, 0, 0);
1482 	if (cl->cl_flags & HFSC_USC)
1483 		rtsc_init(&cl->cl_ulimit, &cl->cl_usc, 0, 0);
1484 }
1485 
1486 static void
1487 hfsc_reset_qdisc(struct Qdisc *sch)
1488 {
1489 	struct hfsc_sched *q = qdisc_priv(sch);
1490 	struct hfsc_class *cl;
1491 	unsigned int i;
1492 
1493 	for (i = 0; i < q->clhash.hashsize; i++) {
1494 		hlist_for_each_entry(cl, &q->clhash.hash[i], cl_common.hnode)
1495 			hfsc_reset_class(cl);
1496 	}
1497 	q->eligible = RB_ROOT;
1498 	qdisc_watchdog_cancel(&q->watchdog);
1499 }
1500 
1501 static void
1502 hfsc_destroy_qdisc(struct Qdisc *sch)
1503 {
1504 	struct hfsc_sched *q = qdisc_priv(sch);
1505 	struct hlist_node *next;
1506 	struct hfsc_class *cl;
1507 	unsigned int i;
1508 
1509 	for (i = 0; i < q->clhash.hashsize; i++) {
1510 		hlist_for_each_entry(cl, &q->clhash.hash[i], cl_common.hnode) {
1511 			tcf_block_put(cl->block);
1512 			cl->block = NULL;
1513 		}
1514 	}
1515 	for (i = 0; i < q->clhash.hashsize; i++) {
1516 		hlist_for_each_entry_safe(cl, next, &q->clhash.hash[i],
1517 					  cl_common.hnode)
1518 			hfsc_destroy_class(sch, cl);
1519 	}
1520 	qdisc_class_hash_destroy(&q->clhash);
1521 	qdisc_watchdog_cancel(&q->watchdog);
1522 }
1523 
1524 static int
1525 hfsc_dump_qdisc(struct Qdisc *sch, struct sk_buff *skb)
1526 {
1527 	struct hfsc_sched *q = qdisc_priv(sch);
1528 	unsigned char *b = skb_tail_pointer(skb);
1529 	struct tc_hfsc_qopt qopt;
1530 
1531 	qopt.defcls = READ_ONCE(q->defcls);
1532 	if (nla_put(skb, TCA_OPTIONS, sizeof(qopt), &qopt))
1533 		goto nla_put_failure;
1534 	return skb->len;
1535 
1536  nla_put_failure:
1537 	nlmsg_trim(skb, b);
1538 	return -1;
1539 }
1540 
1541 static int
1542 hfsc_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free)
1543 {
1544 	unsigned int len = qdisc_pkt_len(skb);
1545 	struct hfsc_class *cl;
1546 	int err;
1547 	bool first;
1548 
1549 	cl = hfsc_classify(skb, sch, &err);
1550 	if (cl == NULL) {
1551 		if (err & __NET_XMIT_BYPASS)
1552 			qdisc_qstats_drop(sch);
1553 		__qdisc_drop(skb, to_free);
1554 		return err;
1555 	}
1556 
1557 	first = !cl->qdisc->q.qlen;
1558 	err = qdisc_enqueue(skb, cl->qdisc, to_free);
1559 	if (unlikely(err != NET_XMIT_SUCCESS)) {
1560 		if (net_xmit_drop_count(err)) {
1561 			cl->qstats.drops++;
1562 			qdisc_qstats_drop(sch);
1563 		}
1564 		return err;
1565 	}
1566 
1567 	if (first) {
1568 		if (cl->cl_flags & HFSC_RSC)
1569 			init_ed(cl, len);
1570 		if (cl->cl_flags & HFSC_FSC)
1571 			init_vf(cl, len);
1572 		/*
1573 		 * If this is the first packet, isolate the head so an eventual
1574 		 * head drop before the first dequeue operation has no chance
1575 		 * to invalidate the deadline.
1576 		 */
1577 		if (cl->cl_flags & HFSC_RSC)
1578 			cl->qdisc->ops->peek(cl->qdisc);
1579 
1580 	}
1581 
1582 	sch->qstats.backlog += len;
1583 	sch->q.qlen++;
1584 
1585 	return NET_XMIT_SUCCESS;
1586 }
1587 
1588 static struct sk_buff *
1589 hfsc_dequeue(struct Qdisc *sch)
1590 {
1591 	struct hfsc_sched *q = qdisc_priv(sch);
1592 	struct hfsc_class *cl;
1593 	struct sk_buff *skb;
1594 	u64 cur_time;
1595 	unsigned int next_len;
1596 	int realtime = 0;
1597 
1598 	if (sch->q.qlen == 0)
1599 		return NULL;
1600 
1601 	cur_time = psched_get_time();
1602 
1603 	/*
1604 	 * if there are eligible classes, use real-time criteria.
1605 	 * find the class with the minimum deadline among
1606 	 * the eligible classes.
1607 	 */
1608 	cl = eltree_get_mindl(q, cur_time);
1609 	if (cl) {
1610 		realtime = 1;
1611 	} else {
1612 		/*
1613 		 * use link-sharing criteria
1614 		 * get the class with the minimum vt in the hierarchy
1615 		 */
1616 		cl = vttree_get_minvt(&q->root, cur_time);
1617 		if (cl == NULL) {
1618 			qdisc_qstats_overlimit(sch);
1619 			hfsc_schedule_watchdog(sch);
1620 			return NULL;
1621 		}
1622 	}
1623 
1624 	skb = qdisc_dequeue_peeked(cl->qdisc);
1625 	if (skb == NULL) {
1626 		qdisc_warn_nonwc("HFSC", cl->qdisc);
1627 		return NULL;
1628 	}
1629 
1630 	bstats_update(&cl->bstats, skb);
1631 	update_vf(cl, qdisc_pkt_len(skb), cur_time);
1632 	if (realtime)
1633 		cl->cl_cumul += qdisc_pkt_len(skb);
1634 
1635 	if (cl->cl_flags & HFSC_RSC) {
1636 		if (cl->qdisc->q.qlen != 0) {
1637 			/* update ed */
1638 			next_len = qdisc_peek_len(cl->qdisc);
1639 			if (realtime)
1640 				update_ed(cl, next_len);
1641 			else
1642 				update_d(cl, next_len);
1643 		} else {
1644 			/* the class becomes passive */
1645 			eltree_remove(cl);
1646 		}
1647 	}
1648 
1649 	qdisc_bstats_update(sch, skb);
1650 	qdisc_qstats_backlog_dec(sch, skb);
1651 	sch->q.qlen--;
1652 
1653 	return skb;
1654 }
1655 
1656 static const struct Qdisc_class_ops hfsc_class_ops = {
1657 	.change		= hfsc_change_class,
1658 	.delete		= hfsc_delete_class,
1659 	.graft		= hfsc_graft_class,
1660 	.leaf		= hfsc_class_leaf,
1661 	.qlen_notify	= hfsc_qlen_notify,
1662 	.find		= hfsc_search_class,
1663 	.bind_tcf	= hfsc_bind_tcf,
1664 	.unbind_tcf	= hfsc_unbind_tcf,
1665 	.tcf_block	= hfsc_tcf_block,
1666 	.dump		= hfsc_dump_class,
1667 	.dump_stats	= hfsc_dump_class_stats,
1668 	.walk		= hfsc_walk
1669 };
1670 
1671 static struct Qdisc_ops hfsc_qdisc_ops __read_mostly = {
1672 	.id		= "hfsc",
1673 	.init		= hfsc_init_qdisc,
1674 	.change		= hfsc_change_qdisc,
1675 	.reset		= hfsc_reset_qdisc,
1676 	.destroy	= hfsc_destroy_qdisc,
1677 	.dump		= hfsc_dump_qdisc,
1678 	.enqueue	= hfsc_enqueue,
1679 	.dequeue	= hfsc_dequeue,
1680 	.peek		= qdisc_peek_dequeued,
1681 	.cl_ops		= &hfsc_class_ops,
1682 	.priv_size	= sizeof(struct hfsc_sched),
1683 	.owner		= THIS_MODULE
1684 };
1685 MODULE_ALIAS_NET_SCH("hfsc");
1686 
1687 static int __init
1688 hfsc_init(void)
1689 {
1690 	return register_qdisc(&hfsc_qdisc_ops);
1691 }
1692 
1693 static void __exit
1694 hfsc_cleanup(void)
1695 {
1696 	unregister_qdisc(&hfsc_qdisc_ops);
1697 }
1698 
1699 MODULE_LICENSE("GPL");
1700 MODULE_DESCRIPTION("Hierarchical Fair Service Curve scheduler");
1701 module_init(hfsc_init);
1702 module_exit(hfsc_cleanup);
1703