[NET_SCHED]: sch_cbq: use hrtimer for delay_timer
[linux-2.6.22.y-op.git] / net / sched / sch_hfsc.c
blob3cc2714fd5ae4888f1f1c16216af573134edb680
1 /*
2 * Copyright (c) 2003 Patrick McHardy, <kaber@trash.net>
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.
9 * 2003-10-17 - Ported from altq
12 * Copyright (c) 1997-1999 Carnegie Mellon University. All Rights Reserved.
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.
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.
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.
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.
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.
52 #include <linux/kernel.h>
53 #include <linux/module.h>
54 #include <linux/types.h>
55 #include <linux/errno.h>
56 #include <linux/jiffies.h>
57 #include <linux/compiler.h>
58 #include <linux/spinlock.h>
59 #include <linux/skbuff.h>
60 #include <linux/string.h>
61 #include <linux/slab.h>
62 #include <linux/list.h>
63 #include <linux/rbtree.h>
64 #include <linux/init.h>
65 #include <linux/netdevice.h>
66 #include <linux/rtnetlink.h>
67 #include <linux/pkt_sched.h>
68 #include <net/pkt_sched.h>
69 #include <net/pkt_cls.h>
70 #include <asm/system.h>
71 #include <asm/div64.h>
74 * kernel internal service curve representation:
75 * coordinates are given by 64 bit unsigned integers.
76 * x-axis: unit is clock count.
77 * y-axis: unit is byte.
79 * The service curve parameters are converted to the internal
80 * representation. The slope values are scaled to avoid overflow.
81 * the inverse slope values as well as the y-projection of the 1st
82 * segment are kept in order to to avoid 64-bit divide operations
83 * that are expensive on 32-bit architectures.
86 struct internal_sc
88 u64 sm1; /* scaled slope of the 1st segment */
89 u64 ism1; /* scaled inverse-slope of the 1st segment */
90 u64 dx; /* the x-projection of the 1st segment */
91 u64 dy; /* the y-projection of the 1st segment */
92 u64 sm2; /* scaled slope of the 2nd segment */
93 u64 ism2; /* scaled inverse-slope of the 2nd segment */
96 /* runtime service curve */
97 struct runtime_sc
99 u64 x; /* current starting position on x-axis */
100 u64 y; /* current starting position on y-axis */
101 u64 sm1; /* scaled slope of the 1st segment */
102 u64 ism1; /* scaled inverse-slope of the 1st segment */
103 u64 dx; /* the x-projection of the 1st segment */
104 u64 dy; /* the y-projection of the 1st segment */
105 u64 sm2; /* scaled slope of the 2nd segment */
106 u64 ism2; /* scaled inverse-slope of the 2nd segment */
109 enum hfsc_class_flags
111 HFSC_RSC = 0x1,
112 HFSC_FSC = 0x2,
113 HFSC_USC = 0x4
116 struct hfsc_class
118 u32 classid; /* class id */
119 unsigned int refcnt; /* usage count */
121 struct gnet_stats_basic bstats;
122 struct gnet_stats_queue qstats;
123 struct gnet_stats_rate_est rate_est;
124 spinlock_t *stats_lock;
125 unsigned int level; /* class level in hierarchy */
126 struct tcf_proto *filter_list; /* filter list */
127 unsigned int filter_cnt; /* filter count */
129 struct hfsc_sched *sched; /* scheduler data */
130 struct hfsc_class *cl_parent; /* parent class */
131 struct list_head siblings; /* sibling classes */
132 struct list_head children; /* child classes */
133 struct Qdisc *qdisc; /* leaf qdisc */
135 struct rb_node el_node; /* qdisc's eligible tree member */
136 struct rb_root vt_tree; /* active children sorted by cl_vt */
137 struct rb_node vt_node; /* parent's vt_tree member */
138 struct rb_root cf_tree; /* active children sorted by cl_f */
139 struct rb_node cf_node; /* parent's cf_heap member */
140 struct list_head hlist; /* hash list member */
141 struct list_head dlist; /* drop list member */
143 u64 cl_total; /* total work in bytes */
144 u64 cl_cumul; /* cumulative work in bytes done by
145 real-time criteria */
147 u64 cl_d; /* deadline*/
148 u64 cl_e; /* eligible time */
149 u64 cl_vt; /* virtual time */
150 u64 cl_f; /* time when this class will fit for
151 link-sharing, max(myf, cfmin) */
152 u64 cl_myf; /* my fit-time (calculated from this
153 class's own upperlimit curve) */
154 u64 cl_myfadj; /* my fit-time adjustment (to cancel
155 history dependence) */
156 u64 cl_cfmin; /* earliest children's fit-time (used
157 with cl_myf to obtain cl_f) */
158 u64 cl_cvtmin; /* minimal virtual time among the
159 children fit for link-sharing
160 (monotonic within a period) */
161 u64 cl_vtadj; /* intra-period cumulative vt
162 adjustment */
163 u64 cl_vtoff; /* inter-period cumulative vt offset */
164 u64 cl_cvtmax; /* max child's vt in the last period */
165 u64 cl_cvtoff; /* cumulative cvtmax of all periods */
166 u64 cl_pcvtoff; /* parent's cvtoff at initalization
167 time */
169 struct internal_sc cl_rsc; /* internal real-time service curve */
170 struct internal_sc cl_fsc; /* internal fair service curve */
171 struct internal_sc cl_usc; /* internal upperlimit service curve */
172 struct runtime_sc cl_deadline; /* deadline curve */
173 struct runtime_sc cl_eligible; /* eligible curve */
174 struct runtime_sc cl_virtual; /* virtual curve */
175 struct runtime_sc cl_ulimit; /* upperlimit curve */
177 unsigned long cl_flags; /* which curves are valid */
178 unsigned long cl_vtperiod; /* vt period sequence number */
179 unsigned long cl_parentperiod;/* parent's vt period sequence number*/
180 unsigned long cl_nactive; /* number of active children */
183 #define HFSC_HSIZE 16
185 struct hfsc_sched
187 u16 defcls; /* default class id */
188 struct hfsc_class root; /* root class */
189 struct list_head clhash[HFSC_HSIZE]; /* class hash */
190 struct rb_root eligible; /* eligible tree */
191 struct list_head droplist; /* active leaf class list (for
192 dropping) */
193 struct sk_buff_head requeue; /* requeued packet */
194 struct qdisc_watchdog watchdog; /* watchdog timer */
197 #define HT_INFINITY 0xffffffffffffffffULL /* infinite time value */
201 * eligible tree holds backlogged classes being sorted by their eligible times.
202 * there is one eligible tree per hfsc instance.
205 static void
206 eltree_insert(struct hfsc_class *cl)
208 struct rb_node **p = &cl->sched->eligible.rb_node;
209 struct rb_node *parent = NULL;
210 struct hfsc_class *cl1;
212 while (*p != NULL) {
213 parent = *p;
214 cl1 = rb_entry(parent, struct hfsc_class, el_node);
215 if (cl->cl_e >= cl1->cl_e)
216 p = &parent->rb_right;
217 else
218 p = &parent->rb_left;
220 rb_link_node(&cl->el_node, parent, p);
221 rb_insert_color(&cl->el_node, &cl->sched->eligible);
224 static inline void
225 eltree_remove(struct hfsc_class *cl)
227 rb_erase(&cl->el_node, &cl->sched->eligible);
230 static inline void
231 eltree_update(struct hfsc_class *cl)
233 eltree_remove(cl);
234 eltree_insert(cl);
237 /* find the class with the minimum deadline among the eligible classes */
238 static inline struct hfsc_class *
239 eltree_get_mindl(struct hfsc_sched *q, u64 cur_time)
241 struct hfsc_class *p, *cl = NULL;
242 struct rb_node *n;
244 for (n = rb_first(&q->eligible); n != NULL; n = rb_next(n)) {
245 p = rb_entry(n, struct hfsc_class, el_node);
246 if (p->cl_e > cur_time)
247 break;
248 if (cl == NULL || p->cl_d < cl->cl_d)
249 cl = p;
251 return cl;
254 /* find the class with minimum eligible time among the eligible classes */
255 static inline struct hfsc_class *
256 eltree_get_minel(struct hfsc_sched *q)
258 struct rb_node *n;
260 n = rb_first(&q->eligible);
261 if (n == NULL)
262 return NULL;
263 return rb_entry(n, struct hfsc_class, el_node);
267 * vttree holds holds backlogged child classes being sorted by their virtual
268 * time. each intermediate class has one vttree.
270 static void
271 vttree_insert(struct hfsc_class *cl)
273 struct rb_node **p = &cl->cl_parent->vt_tree.rb_node;
274 struct rb_node *parent = NULL;
275 struct hfsc_class *cl1;
277 while (*p != NULL) {
278 parent = *p;
279 cl1 = rb_entry(parent, struct hfsc_class, vt_node);
280 if (cl->cl_vt >= cl1->cl_vt)
281 p = &parent->rb_right;
282 else
283 p = &parent->rb_left;
285 rb_link_node(&cl->vt_node, parent, p);
286 rb_insert_color(&cl->vt_node, &cl->cl_parent->vt_tree);
289 static inline void
290 vttree_remove(struct hfsc_class *cl)
292 rb_erase(&cl->vt_node, &cl->cl_parent->vt_tree);
295 static inline void
296 vttree_update(struct hfsc_class *cl)
298 vttree_remove(cl);
299 vttree_insert(cl);
302 static inline struct hfsc_class *
303 vttree_firstfit(struct hfsc_class *cl, u64 cur_time)
305 struct hfsc_class *p;
306 struct rb_node *n;
308 for (n = rb_first(&cl->vt_tree); n != NULL; n = rb_next(n)) {
309 p = rb_entry(n, struct hfsc_class, vt_node);
310 if (p->cl_f <= cur_time)
311 return p;
313 return NULL;
317 * get the leaf class with the minimum vt in the hierarchy
319 static struct hfsc_class *
320 vttree_get_minvt(struct hfsc_class *cl, u64 cur_time)
322 /* if root-class's cfmin is bigger than cur_time nothing to do */
323 if (cl->cl_cfmin > cur_time)
324 return NULL;
326 while (cl->level > 0) {
327 cl = vttree_firstfit(cl, cur_time);
328 if (cl == NULL)
329 return NULL;
331 * update parent's cl_cvtmin.
333 if (cl->cl_parent->cl_cvtmin < cl->cl_vt)
334 cl->cl_parent->cl_cvtmin = cl->cl_vt;
336 return cl;
339 static void
340 cftree_insert(struct hfsc_class *cl)
342 struct rb_node **p = &cl->cl_parent->cf_tree.rb_node;
343 struct rb_node *parent = NULL;
344 struct hfsc_class *cl1;
346 while (*p != NULL) {
347 parent = *p;
348 cl1 = rb_entry(parent, struct hfsc_class, cf_node);
349 if (cl->cl_f >= cl1->cl_f)
350 p = &parent->rb_right;
351 else
352 p = &parent->rb_left;
354 rb_link_node(&cl->cf_node, parent, p);
355 rb_insert_color(&cl->cf_node, &cl->cl_parent->cf_tree);
358 static inline void
359 cftree_remove(struct hfsc_class *cl)
361 rb_erase(&cl->cf_node, &cl->cl_parent->cf_tree);
364 static inline void
365 cftree_update(struct hfsc_class *cl)
367 cftree_remove(cl);
368 cftree_insert(cl);
372 * service curve support functions
374 * external service curve parameters
375 * m: bps
376 * d: us
377 * internal service curve parameters
378 * sm: (bytes/psched_us) << SM_SHIFT
379 * ism: (psched_us/byte) << ISM_SHIFT
380 * dx: psched_us
382 * The clock source resolution with ktime is 1.024us.
384 * sm and ism are scaled in order to keep effective digits.
385 * SM_SHIFT and ISM_SHIFT are selected to keep at least 4 effective
386 * digits in decimal using the following table.
388 * bits/sec 100Kbps 1Mbps 10Mbps 100Mbps 1Gbps
389 * ------------+-------------------------------------------------------
390 * bytes/1.024us 12.8e-3 128e-3 1280e-3 12800e-3 128000e-3
392 * 1.024us/byte 78.125 7.8125 0.78125 0.078125 0.0078125
394 #define SM_SHIFT 20
395 #define ISM_SHIFT 18
397 #define SM_MASK ((1ULL << SM_SHIFT) - 1)
398 #define ISM_MASK ((1ULL << ISM_SHIFT) - 1)
400 static inline u64
401 seg_x2y(u64 x, u64 sm)
403 u64 y;
406 * compute
407 * y = x * sm >> SM_SHIFT
408 * but divide it for the upper and lower bits to avoid overflow
410 y = (x >> SM_SHIFT) * sm + (((x & SM_MASK) * sm) >> SM_SHIFT);
411 return y;
414 static inline u64
415 seg_y2x(u64 y, u64 ism)
417 u64 x;
419 if (y == 0)
420 x = 0;
421 else if (ism == HT_INFINITY)
422 x = HT_INFINITY;
423 else {
424 x = (y >> ISM_SHIFT) * ism
425 + (((y & ISM_MASK) * ism) >> ISM_SHIFT);
427 return x;
430 /* Convert m (bps) into sm (bytes/psched us) */
431 static u64
432 m2sm(u32 m)
434 u64 sm;
436 sm = ((u64)m << SM_SHIFT);
437 sm += PSCHED_JIFFIE2US(HZ) - 1;
438 do_div(sm, PSCHED_JIFFIE2US(HZ));
439 return sm;
442 /* convert m (bps) into ism (psched us/byte) */
443 static u64
444 m2ism(u32 m)
446 u64 ism;
448 if (m == 0)
449 ism = HT_INFINITY;
450 else {
451 ism = ((u64)PSCHED_JIFFIE2US(HZ) << ISM_SHIFT);
452 ism += m - 1;
453 do_div(ism, m);
455 return ism;
458 /* convert d (us) into dx (psched us) */
459 static u64
460 d2dx(u32 d)
462 u64 dx;
464 dx = ((u64)d * PSCHED_JIFFIE2US(HZ));
465 dx += USEC_PER_SEC - 1;
466 do_div(dx, USEC_PER_SEC);
467 return dx;
470 /* convert sm (bytes/psched us) into m (bps) */
471 static u32
472 sm2m(u64 sm)
474 u64 m;
476 m = (sm * PSCHED_JIFFIE2US(HZ)) >> SM_SHIFT;
477 return (u32)m;
480 /* convert dx (psched us) into d (us) */
481 static u32
482 dx2d(u64 dx)
484 u64 d;
486 d = dx * USEC_PER_SEC;
487 do_div(d, PSCHED_JIFFIE2US(HZ));
488 return (u32)d;
491 static void
492 sc2isc(struct tc_service_curve *sc, struct internal_sc *isc)
494 isc->sm1 = m2sm(sc->m1);
495 isc->ism1 = m2ism(sc->m1);
496 isc->dx = d2dx(sc->d);
497 isc->dy = seg_x2y(isc->dx, isc->sm1);
498 isc->sm2 = m2sm(sc->m2);
499 isc->ism2 = m2ism(sc->m2);
503 * initialize the runtime service curve with the given internal
504 * service curve starting at (x, y).
506 static void
507 rtsc_init(struct runtime_sc *rtsc, struct internal_sc *isc, u64 x, u64 y)
509 rtsc->x = x;
510 rtsc->y = y;
511 rtsc->sm1 = isc->sm1;
512 rtsc->ism1 = isc->ism1;
513 rtsc->dx = isc->dx;
514 rtsc->dy = isc->dy;
515 rtsc->sm2 = isc->sm2;
516 rtsc->ism2 = isc->ism2;
520 * calculate the y-projection of the runtime service curve by the
521 * given x-projection value
523 static u64
524 rtsc_y2x(struct runtime_sc *rtsc, u64 y)
526 u64 x;
528 if (y < rtsc->y)
529 x = rtsc->x;
530 else if (y <= rtsc->y + rtsc->dy) {
531 /* x belongs to the 1st segment */
532 if (rtsc->dy == 0)
533 x = rtsc->x + rtsc->dx;
534 else
535 x = rtsc->x + seg_y2x(y - rtsc->y, rtsc->ism1);
536 } else {
537 /* x belongs to the 2nd segment */
538 x = rtsc->x + rtsc->dx
539 + seg_y2x(y - rtsc->y - rtsc->dy, rtsc->ism2);
541 return x;
544 static u64
545 rtsc_x2y(struct runtime_sc *rtsc, u64 x)
547 u64 y;
549 if (x <= rtsc->x)
550 y = rtsc->y;
551 else if (x <= rtsc->x + rtsc->dx)
552 /* y belongs to the 1st segment */
553 y = rtsc->y + seg_x2y(x - rtsc->x, rtsc->sm1);
554 else
555 /* y belongs to the 2nd segment */
556 y = rtsc->y + rtsc->dy
557 + seg_x2y(x - rtsc->x - rtsc->dx, rtsc->sm2);
558 return y;
562 * update the runtime service curve by taking the minimum of the current
563 * runtime service curve and the service curve starting at (x, y).
565 static void
566 rtsc_min(struct runtime_sc *rtsc, struct internal_sc *isc, u64 x, u64 y)
568 u64 y1, y2, dx, dy;
569 u32 dsm;
571 if (isc->sm1 <= isc->sm2) {
572 /* service curve is convex */
573 y1 = rtsc_x2y(rtsc, x);
574 if (y1 < y)
575 /* the current rtsc is smaller */
576 return;
577 rtsc->x = x;
578 rtsc->y = y;
579 return;
583 * service curve is concave
584 * compute the two y values of the current rtsc
585 * y1: at x
586 * y2: at (x + dx)
588 y1 = rtsc_x2y(rtsc, x);
589 if (y1 <= y) {
590 /* rtsc is below isc, no change to rtsc */
591 return;
594 y2 = rtsc_x2y(rtsc, x + isc->dx);
595 if (y2 >= y + isc->dy) {
596 /* rtsc is above isc, replace rtsc by isc */
597 rtsc->x = x;
598 rtsc->y = y;
599 rtsc->dx = isc->dx;
600 rtsc->dy = isc->dy;
601 return;
605 * the two curves intersect
606 * compute the offsets (dx, dy) using the reverse
607 * function of seg_x2y()
608 * seg_x2y(dx, sm1) == seg_x2y(dx, sm2) + (y1 - y)
610 dx = (y1 - y) << SM_SHIFT;
611 dsm = isc->sm1 - isc->sm2;
612 do_div(dx, dsm);
614 * check if (x, y1) belongs to the 1st segment of rtsc.
615 * if so, add the offset.
617 if (rtsc->x + rtsc->dx > x)
618 dx += rtsc->x + rtsc->dx - x;
619 dy = seg_x2y(dx, isc->sm1);
621 rtsc->x = x;
622 rtsc->y = y;
623 rtsc->dx = dx;
624 rtsc->dy = dy;
625 return;
628 static void
629 init_ed(struct hfsc_class *cl, unsigned int next_len)
631 u64 cur_time;
633 PSCHED_GET_TIME(cur_time);
635 /* update the deadline curve */
636 rtsc_min(&cl->cl_deadline, &cl->cl_rsc, cur_time, cl->cl_cumul);
639 * update the eligible curve.
640 * for concave, it is equal to the deadline curve.
641 * for convex, it is a linear curve with slope m2.
643 cl->cl_eligible = cl->cl_deadline;
644 if (cl->cl_rsc.sm1 <= cl->cl_rsc.sm2) {
645 cl->cl_eligible.dx = 0;
646 cl->cl_eligible.dy = 0;
649 /* compute e and d */
650 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
651 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
653 eltree_insert(cl);
656 static void
657 update_ed(struct hfsc_class *cl, unsigned int next_len)
659 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
660 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
662 eltree_update(cl);
665 static inline void
666 update_d(struct hfsc_class *cl, unsigned int next_len)
668 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
671 static inline void
672 update_cfmin(struct hfsc_class *cl)
674 struct rb_node *n = rb_first(&cl->cf_tree);
675 struct hfsc_class *p;
677 if (n == NULL) {
678 cl->cl_cfmin = 0;
679 return;
681 p = rb_entry(n, struct hfsc_class, cf_node);
682 cl->cl_cfmin = p->cl_f;
685 static void
686 init_vf(struct hfsc_class *cl, unsigned int len)
688 struct hfsc_class *max_cl;
689 struct rb_node *n;
690 u64 vt, f, cur_time;
691 int go_active;
693 cur_time = 0;
694 go_active = 1;
695 for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
696 if (go_active && cl->cl_nactive++ == 0)
697 go_active = 1;
698 else
699 go_active = 0;
701 if (go_active) {
702 n = rb_last(&cl->cl_parent->vt_tree);
703 if (n != NULL) {
704 max_cl = rb_entry(n, struct hfsc_class,vt_node);
706 * set vt to the average of the min and max
707 * classes. if the parent's period didn't
708 * change, don't decrease vt of the class.
710 vt = max_cl->cl_vt;
711 if (cl->cl_parent->cl_cvtmin != 0)
712 vt = (cl->cl_parent->cl_cvtmin + vt)/2;
714 if (cl->cl_parent->cl_vtperiod !=
715 cl->cl_parentperiod || vt > cl->cl_vt)
716 cl->cl_vt = vt;
717 } else {
719 * first child for a new parent backlog period.
720 * add parent's cvtmax to cvtoff to make a new
721 * vt (vtoff + vt) larger than the vt in the
722 * last period for all children.
724 vt = cl->cl_parent->cl_cvtmax;
725 cl->cl_parent->cl_cvtoff += vt;
726 cl->cl_parent->cl_cvtmax = 0;
727 cl->cl_parent->cl_cvtmin = 0;
728 cl->cl_vt = 0;
731 cl->cl_vtoff = cl->cl_parent->cl_cvtoff -
732 cl->cl_pcvtoff;
734 /* update the virtual curve */
735 vt = cl->cl_vt + cl->cl_vtoff;
736 rtsc_min(&cl->cl_virtual, &cl->cl_fsc, vt,
737 cl->cl_total);
738 if (cl->cl_virtual.x == vt) {
739 cl->cl_virtual.x -= cl->cl_vtoff;
740 cl->cl_vtoff = 0;
742 cl->cl_vtadj = 0;
744 cl->cl_vtperiod++; /* increment vt period */
745 cl->cl_parentperiod = cl->cl_parent->cl_vtperiod;
746 if (cl->cl_parent->cl_nactive == 0)
747 cl->cl_parentperiod++;
748 cl->cl_f = 0;
750 vttree_insert(cl);
751 cftree_insert(cl);
753 if (cl->cl_flags & HFSC_USC) {
754 /* class has upper limit curve */
755 if (cur_time == 0)
756 PSCHED_GET_TIME(cur_time);
758 /* update the ulimit curve */
759 rtsc_min(&cl->cl_ulimit, &cl->cl_usc, cur_time,
760 cl->cl_total);
761 /* compute myf */
762 cl->cl_myf = rtsc_y2x(&cl->cl_ulimit,
763 cl->cl_total);
764 cl->cl_myfadj = 0;
768 f = max(cl->cl_myf, cl->cl_cfmin);
769 if (f != cl->cl_f) {
770 cl->cl_f = f;
771 cftree_update(cl);
772 update_cfmin(cl->cl_parent);
777 static void
778 update_vf(struct hfsc_class *cl, unsigned int len, u64 cur_time)
780 u64 f; /* , myf_bound, delta; */
781 int go_passive = 0;
783 if (cl->qdisc->q.qlen == 0 && cl->cl_flags & HFSC_FSC)
784 go_passive = 1;
786 for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
787 cl->cl_total += len;
789 if (!(cl->cl_flags & HFSC_FSC) || cl->cl_nactive == 0)
790 continue;
792 if (go_passive && --cl->cl_nactive == 0)
793 go_passive = 1;
794 else
795 go_passive = 0;
797 if (go_passive) {
798 /* no more active child, going passive */
800 /* update cvtmax of the parent class */
801 if (cl->cl_vt > cl->cl_parent->cl_cvtmax)
802 cl->cl_parent->cl_cvtmax = cl->cl_vt;
804 /* remove this class from the vt tree */
805 vttree_remove(cl);
807 cftree_remove(cl);
808 update_cfmin(cl->cl_parent);
810 continue;
814 * update vt and f
816 cl->cl_vt = rtsc_y2x(&cl->cl_virtual, cl->cl_total)
817 - cl->cl_vtoff + cl->cl_vtadj;
820 * if vt of the class is smaller than cvtmin,
821 * the class was skipped in the past due to non-fit.
822 * if so, we need to adjust vtadj.
824 if (cl->cl_vt < cl->cl_parent->cl_cvtmin) {
825 cl->cl_vtadj += cl->cl_parent->cl_cvtmin - cl->cl_vt;
826 cl->cl_vt = cl->cl_parent->cl_cvtmin;
829 /* update the vt tree */
830 vttree_update(cl);
832 if (cl->cl_flags & HFSC_USC) {
833 cl->cl_myf = cl->cl_myfadj + rtsc_y2x(&cl->cl_ulimit,
834 cl->cl_total);
835 #if 0
837 * This code causes classes to stay way under their
838 * limit when multiple classes are used at gigabit
839 * speed. needs investigation. -kaber
842 * if myf lags behind by more than one clock tick
843 * from the current time, adjust myfadj to prevent
844 * a rate-limited class from going greedy.
845 * in a steady state under rate-limiting, myf
846 * fluctuates within one clock tick.
848 myf_bound = cur_time - PSCHED_JIFFIE2US(1);
849 if (cl->cl_myf < myf_bound) {
850 delta = cur_time - cl->cl_myf;
851 cl->cl_myfadj += delta;
852 cl->cl_myf += delta;
854 #endif
857 f = max(cl->cl_myf, cl->cl_cfmin);
858 if (f != cl->cl_f) {
859 cl->cl_f = f;
860 cftree_update(cl);
861 update_cfmin(cl->cl_parent);
866 static void
867 set_active(struct hfsc_class *cl, unsigned int len)
869 if (cl->cl_flags & HFSC_RSC)
870 init_ed(cl, len);
871 if (cl->cl_flags & HFSC_FSC)
872 init_vf(cl, len);
874 list_add_tail(&cl->dlist, &cl->sched->droplist);
877 static void
878 set_passive(struct hfsc_class *cl)
880 if (cl->cl_flags & HFSC_RSC)
881 eltree_remove(cl);
883 list_del(&cl->dlist);
886 * vttree is now handled in update_vf() so that update_vf(cl, 0, 0)
887 * needs to be called explicitly to remove a class from vttree.
892 * hack to get length of first packet in queue.
894 static unsigned int
895 qdisc_peek_len(struct Qdisc *sch)
897 struct sk_buff *skb;
898 unsigned int len;
900 skb = sch->dequeue(sch);
901 if (skb == NULL) {
902 if (net_ratelimit())
903 printk("qdisc_peek_len: non work-conserving qdisc ?\n");
904 return 0;
906 len = skb->len;
907 if (unlikely(sch->ops->requeue(skb, sch) != NET_XMIT_SUCCESS)) {
908 if (net_ratelimit())
909 printk("qdisc_peek_len: failed to requeue\n");
910 qdisc_tree_decrease_qlen(sch, 1);
911 return 0;
913 return len;
916 static void
917 hfsc_purge_queue(struct Qdisc *sch, struct hfsc_class *cl)
919 unsigned int len = cl->qdisc->q.qlen;
921 qdisc_reset(cl->qdisc);
922 qdisc_tree_decrease_qlen(cl->qdisc, len);
925 static void
926 hfsc_adjust_levels(struct hfsc_class *cl)
928 struct hfsc_class *p;
929 unsigned int level;
931 do {
932 level = 0;
933 list_for_each_entry(p, &cl->children, siblings) {
934 if (p->level >= level)
935 level = p->level + 1;
937 cl->level = level;
938 } while ((cl = cl->cl_parent) != NULL);
941 static inline unsigned int
942 hfsc_hash(u32 h)
944 h ^= h >> 8;
945 h ^= h >> 4;
947 return h & (HFSC_HSIZE - 1);
950 static inline struct hfsc_class *
951 hfsc_find_class(u32 classid, struct Qdisc *sch)
953 struct hfsc_sched *q = qdisc_priv(sch);
954 struct hfsc_class *cl;
956 list_for_each_entry(cl, &q->clhash[hfsc_hash(classid)], hlist) {
957 if (cl->classid == classid)
958 return cl;
960 return NULL;
963 static void
964 hfsc_change_rsc(struct hfsc_class *cl, struct tc_service_curve *rsc,
965 u64 cur_time)
967 sc2isc(rsc, &cl->cl_rsc);
968 rtsc_init(&cl->cl_deadline, &cl->cl_rsc, cur_time, cl->cl_cumul);
969 cl->cl_eligible = cl->cl_deadline;
970 if (cl->cl_rsc.sm1 <= cl->cl_rsc.sm2) {
971 cl->cl_eligible.dx = 0;
972 cl->cl_eligible.dy = 0;
974 cl->cl_flags |= HFSC_RSC;
977 static void
978 hfsc_change_fsc(struct hfsc_class *cl, struct tc_service_curve *fsc)
980 sc2isc(fsc, &cl->cl_fsc);
981 rtsc_init(&cl->cl_virtual, &cl->cl_fsc, cl->cl_vt, cl->cl_total);
982 cl->cl_flags |= HFSC_FSC;
985 static void
986 hfsc_change_usc(struct hfsc_class *cl, struct tc_service_curve *usc,
987 u64 cur_time)
989 sc2isc(usc, &cl->cl_usc);
990 rtsc_init(&cl->cl_ulimit, &cl->cl_usc, cur_time, cl->cl_total);
991 cl->cl_flags |= HFSC_USC;
994 static int
995 hfsc_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
996 struct rtattr **tca, unsigned long *arg)
998 struct hfsc_sched *q = qdisc_priv(sch);
999 struct hfsc_class *cl = (struct hfsc_class *)*arg;
1000 struct hfsc_class *parent = NULL;
1001 struct rtattr *opt = tca[TCA_OPTIONS-1];
1002 struct rtattr *tb[TCA_HFSC_MAX];
1003 struct tc_service_curve *rsc = NULL, *fsc = NULL, *usc = NULL;
1004 u64 cur_time;
1006 if (opt == NULL || rtattr_parse_nested(tb, TCA_HFSC_MAX, opt))
1007 return -EINVAL;
1009 if (tb[TCA_HFSC_RSC-1]) {
1010 if (RTA_PAYLOAD(tb[TCA_HFSC_RSC-1]) < sizeof(*rsc))
1011 return -EINVAL;
1012 rsc = RTA_DATA(tb[TCA_HFSC_RSC-1]);
1013 if (rsc->m1 == 0 && rsc->m2 == 0)
1014 rsc = NULL;
1017 if (tb[TCA_HFSC_FSC-1]) {
1018 if (RTA_PAYLOAD(tb[TCA_HFSC_FSC-1]) < sizeof(*fsc))
1019 return -EINVAL;
1020 fsc = RTA_DATA(tb[TCA_HFSC_FSC-1]);
1021 if (fsc->m1 == 0 && fsc->m2 == 0)
1022 fsc = NULL;
1025 if (tb[TCA_HFSC_USC-1]) {
1026 if (RTA_PAYLOAD(tb[TCA_HFSC_USC-1]) < sizeof(*usc))
1027 return -EINVAL;
1028 usc = RTA_DATA(tb[TCA_HFSC_USC-1]);
1029 if (usc->m1 == 0 && usc->m2 == 0)
1030 usc = NULL;
1033 if (cl != NULL) {
1034 if (parentid) {
1035 if (cl->cl_parent && cl->cl_parent->classid != parentid)
1036 return -EINVAL;
1037 if (cl->cl_parent == NULL && parentid != TC_H_ROOT)
1038 return -EINVAL;
1040 PSCHED_GET_TIME(cur_time);
1042 sch_tree_lock(sch);
1043 if (rsc != NULL)
1044 hfsc_change_rsc(cl, rsc, cur_time);
1045 if (fsc != NULL)
1046 hfsc_change_fsc(cl, fsc);
1047 if (usc != NULL)
1048 hfsc_change_usc(cl, usc, cur_time);
1050 if (cl->qdisc->q.qlen != 0) {
1051 if (cl->cl_flags & HFSC_RSC)
1052 update_ed(cl, qdisc_peek_len(cl->qdisc));
1053 if (cl->cl_flags & HFSC_FSC)
1054 update_vf(cl, 0, cur_time);
1056 sch_tree_unlock(sch);
1058 #ifdef CONFIG_NET_ESTIMATOR
1059 if (tca[TCA_RATE-1])
1060 gen_replace_estimator(&cl->bstats, &cl->rate_est,
1061 cl->stats_lock, tca[TCA_RATE-1]);
1062 #endif
1063 return 0;
1066 if (parentid == TC_H_ROOT)
1067 return -EEXIST;
1069 parent = &q->root;
1070 if (parentid) {
1071 parent = hfsc_find_class(parentid, sch);
1072 if (parent == NULL)
1073 return -ENOENT;
1076 if (classid == 0 || TC_H_MAJ(classid ^ sch->handle) != 0)
1077 return -EINVAL;
1078 if (hfsc_find_class(classid, sch))
1079 return -EEXIST;
1081 if (rsc == NULL && fsc == NULL)
1082 return -EINVAL;
1084 cl = kzalloc(sizeof(struct hfsc_class), GFP_KERNEL);
1085 if (cl == NULL)
1086 return -ENOBUFS;
1088 if (rsc != NULL)
1089 hfsc_change_rsc(cl, rsc, 0);
1090 if (fsc != NULL)
1091 hfsc_change_fsc(cl, fsc);
1092 if (usc != NULL)
1093 hfsc_change_usc(cl, usc, 0);
1095 cl->refcnt = 1;
1096 cl->classid = classid;
1097 cl->sched = q;
1098 cl->cl_parent = parent;
1099 cl->qdisc = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops, classid);
1100 if (cl->qdisc == NULL)
1101 cl->qdisc = &noop_qdisc;
1102 cl->stats_lock = &sch->dev->queue_lock;
1103 INIT_LIST_HEAD(&cl->children);
1104 cl->vt_tree = RB_ROOT;
1105 cl->cf_tree = RB_ROOT;
1107 sch_tree_lock(sch);
1108 list_add_tail(&cl->hlist, &q->clhash[hfsc_hash(classid)]);
1109 list_add_tail(&cl->siblings, &parent->children);
1110 if (parent->level == 0)
1111 hfsc_purge_queue(sch, parent);
1112 hfsc_adjust_levels(parent);
1113 cl->cl_pcvtoff = parent->cl_cvtoff;
1114 sch_tree_unlock(sch);
1116 #ifdef CONFIG_NET_ESTIMATOR
1117 if (tca[TCA_RATE-1])
1118 gen_new_estimator(&cl->bstats, &cl->rate_est,
1119 cl->stats_lock, tca[TCA_RATE-1]);
1120 #endif
1121 *arg = (unsigned long)cl;
1122 return 0;
1125 static void
1126 hfsc_destroy_filters(struct tcf_proto **fl)
1128 struct tcf_proto *tp;
1130 while ((tp = *fl) != NULL) {
1131 *fl = tp->next;
1132 tcf_destroy(tp);
1136 static void
1137 hfsc_destroy_class(struct Qdisc *sch, struct hfsc_class *cl)
1139 struct hfsc_sched *q = qdisc_priv(sch);
1141 hfsc_destroy_filters(&cl->filter_list);
1142 qdisc_destroy(cl->qdisc);
1143 #ifdef CONFIG_NET_ESTIMATOR
1144 gen_kill_estimator(&cl->bstats, &cl->rate_est);
1145 #endif
1146 if (cl != &q->root)
1147 kfree(cl);
1150 static int
1151 hfsc_delete_class(struct Qdisc *sch, unsigned long arg)
1153 struct hfsc_sched *q = qdisc_priv(sch);
1154 struct hfsc_class *cl = (struct hfsc_class *)arg;
1156 if (cl->level > 0 || cl->filter_cnt > 0 || cl == &q->root)
1157 return -EBUSY;
1159 sch_tree_lock(sch);
1161 list_del(&cl->siblings);
1162 hfsc_adjust_levels(cl->cl_parent);
1164 hfsc_purge_queue(sch, cl);
1165 list_del(&cl->hlist);
1167 if (--cl->refcnt == 0)
1168 hfsc_destroy_class(sch, cl);
1170 sch_tree_unlock(sch);
1171 return 0;
1174 static struct hfsc_class *
1175 hfsc_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
1177 struct hfsc_sched *q = qdisc_priv(sch);
1178 struct hfsc_class *cl;
1179 struct tcf_result res;
1180 struct tcf_proto *tcf;
1181 int result;
1183 if (TC_H_MAJ(skb->priority ^ sch->handle) == 0 &&
1184 (cl = hfsc_find_class(skb->priority, sch)) != NULL)
1185 if (cl->level == 0)
1186 return cl;
1188 *qerr = NET_XMIT_BYPASS;
1189 tcf = q->root.filter_list;
1190 while (tcf && (result = tc_classify(skb, tcf, &res)) >= 0) {
1191 #ifdef CONFIG_NET_CLS_ACT
1192 switch (result) {
1193 case TC_ACT_QUEUED:
1194 case TC_ACT_STOLEN:
1195 *qerr = NET_XMIT_SUCCESS;
1196 case TC_ACT_SHOT:
1197 return NULL;
1199 #elif defined(CONFIG_NET_CLS_POLICE)
1200 if (result == TC_POLICE_SHOT)
1201 return NULL;
1202 #endif
1203 if ((cl = (struct hfsc_class *)res.class) == NULL) {
1204 if ((cl = hfsc_find_class(res.classid, sch)) == NULL)
1205 break; /* filter selected invalid classid */
1208 if (cl->level == 0)
1209 return cl; /* hit leaf class */
1211 /* apply inner filter chain */
1212 tcf = cl->filter_list;
1215 /* classification failed, try default class */
1216 cl = hfsc_find_class(TC_H_MAKE(TC_H_MAJ(sch->handle), q->defcls), sch);
1217 if (cl == NULL || cl->level > 0)
1218 return NULL;
1220 return cl;
1223 static int
1224 hfsc_graft_class(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
1225 struct Qdisc **old)
1227 struct hfsc_class *cl = (struct hfsc_class *)arg;
1229 if (cl == NULL)
1230 return -ENOENT;
1231 if (cl->level > 0)
1232 return -EINVAL;
1233 if (new == NULL) {
1234 new = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
1235 cl->classid);
1236 if (new == NULL)
1237 new = &noop_qdisc;
1240 sch_tree_lock(sch);
1241 hfsc_purge_queue(sch, cl);
1242 *old = xchg(&cl->qdisc, new);
1243 sch_tree_unlock(sch);
1244 return 0;
1247 static struct Qdisc *
1248 hfsc_class_leaf(struct Qdisc *sch, unsigned long arg)
1250 struct hfsc_class *cl = (struct hfsc_class *)arg;
1252 if (cl != NULL && cl->level == 0)
1253 return cl->qdisc;
1255 return NULL;
1258 static void
1259 hfsc_qlen_notify(struct Qdisc *sch, unsigned long arg)
1261 struct hfsc_class *cl = (struct hfsc_class *)arg;
1263 if (cl->qdisc->q.qlen == 0) {
1264 update_vf(cl, 0, 0);
1265 set_passive(cl);
1269 static unsigned long
1270 hfsc_get_class(struct Qdisc *sch, u32 classid)
1272 struct hfsc_class *cl = hfsc_find_class(classid, sch);
1274 if (cl != NULL)
1275 cl->refcnt++;
1277 return (unsigned long)cl;
1280 static void
1281 hfsc_put_class(struct Qdisc *sch, unsigned long arg)
1283 struct hfsc_class *cl = (struct hfsc_class *)arg;
1285 if (--cl->refcnt == 0)
1286 hfsc_destroy_class(sch, cl);
1289 static unsigned long
1290 hfsc_bind_tcf(struct Qdisc *sch, unsigned long parent, u32 classid)
1292 struct hfsc_class *p = (struct hfsc_class *)parent;
1293 struct hfsc_class *cl = hfsc_find_class(classid, sch);
1295 if (cl != NULL) {
1296 if (p != NULL && p->level <= cl->level)
1297 return 0;
1298 cl->filter_cnt++;
1301 return (unsigned long)cl;
1304 static void
1305 hfsc_unbind_tcf(struct Qdisc *sch, unsigned long arg)
1307 struct hfsc_class *cl = (struct hfsc_class *)arg;
1309 cl->filter_cnt--;
1312 static struct tcf_proto **
1313 hfsc_tcf_chain(struct Qdisc *sch, unsigned long arg)
1315 struct hfsc_sched *q = qdisc_priv(sch);
1316 struct hfsc_class *cl = (struct hfsc_class *)arg;
1318 if (cl == NULL)
1319 cl = &q->root;
1321 return &cl->filter_list;
1324 static int
1325 hfsc_dump_sc(struct sk_buff *skb, int attr, struct internal_sc *sc)
1327 struct tc_service_curve tsc;
1329 tsc.m1 = sm2m(sc->sm1);
1330 tsc.d = dx2d(sc->dx);
1331 tsc.m2 = sm2m(sc->sm2);
1332 RTA_PUT(skb, attr, sizeof(tsc), &tsc);
1334 return skb->len;
1336 rtattr_failure:
1337 return -1;
1340 static inline int
1341 hfsc_dump_curves(struct sk_buff *skb, struct hfsc_class *cl)
1343 if ((cl->cl_flags & HFSC_RSC) &&
1344 (hfsc_dump_sc(skb, TCA_HFSC_RSC, &cl->cl_rsc) < 0))
1345 goto rtattr_failure;
1347 if ((cl->cl_flags & HFSC_FSC) &&
1348 (hfsc_dump_sc(skb, TCA_HFSC_FSC, &cl->cl_fsc) < 0))
1349 goto rtattr_failure;
1351 if ((cl->cl_flags & HFSC_USC) &&
1352 (hfsc_dump_sc(skb, TCA_HFSC_USC, &cl->cl_usc) < 0))
1353 goto rtattr_failure;
1355 return skb->len;
1357 rtattr_failure:
1358 return -1;
1361 static int
1362 hfsc_dump_class(struct Qdisc *sch, unsigned long arg, struct sk_buff *skb,
1363 struct tcmsg *tcm)
1365 struct hfsc_class *cl = (struct hfsc_class *)arg;
1366 unsigned char *b = skb->tail;
1367 struct rtattr *rta = (struct rtattr *)b;
1369 tcm->tcm_parent = cl->cl_parent ? cl->cl_parent->classid : TC_H_ROOT;
1370 tcm->tcm_handle = cl->classid;
1371 if (cl->level == 0)
1372 tcm->tcm_info = cl->qdisc->handle;
1374 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
1375 if (hfsc_dump_curves(skb, cl) < 0)
1376 goto rtattr_failure;
1377 rta->rta_len = skb->tail - b;
1378 return skb->len;
1380 rtattr_failure:
1381 skb_trim(skb, b - skb->data);
1382 return -1;
1385 static int
1386 hfsc_dump_class_stats(struct Qdisc *sch, unsigned long arg,
1387 struct gnet_dump *d)
1389 struct hfsc_class *cl = (struct hfsc_class *)arg;
1390 struct tc_hfsc_stats xstats;
1392 cl->qstats.qlen = cl->qdisc->q.qlen;
1393 xstats.level = cl->level;
1394 xstats.period = cl->cl_vtperiod;
1395 xstats.work = cl->cl_total;
1396 xstats.rtwork = cl->cl_cumul;
1398 if (gnet_stats_copy_basic(d, &cl->bstats) < 0 ||
1399 #ifdef CONFIG_NET_ESTIMATOR
1400 gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
1401 #endif
1402 gnet_stats_copy_queue(d, &cl->qstats) < 0)
1403 return -1;
1405 return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
1410 static void
1411 hfsc_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1413 struct hfsc_sched *q = qdisc_priv(sch);
1414 struct hfsc_class *cl;
1415 unsigned int i;
1417 if (arg->stop)
1418 return;
1420 for (i = 0; i < HFSC_HSIZE; i++) {
1421 list_for_each_entry(cl, &q->clhash[i], hlist) {
1422 if (arg->count < arg->skip) {
1423 arg->count++;
1424 continue;
1426 if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
1427 arg->stop = 1;
1428 return;
1430 arg->count++;
1435 static void
1436 hfsc_schedule_watchdog(struct Qdisc *sch)
1438 struct hfsc_sched *q = qdisc_priv(sch);
1439 struct hfsc_class *cl;
1440 u64 next_time = 0;
1442 if ((cl = eltree_get_minel(q)) != NULL)
1443 next_time = cl->cl_e;
1444 if (q->root.cl_cfmin != 0) {
1445 if (next_time == 0 || next_time > q->root.cl_cfmin)
1446 next_time = q->root.cl_cfmin;
1448 WARN_ON(next_time == 0);
1449 qdisc_watchdog_schedule(&q->watchdog, next_time);
1452 static int
1453 hfsc_init_qdisc(struct Qdisc *sch, struct rtattr *opt)
1455 struct hfsc_sched *q = qdisc_priv(sch);
1456 struct tc_hfsc_qopt *qopt;
1457 unsigned int i;
1459 if (opt == NULL || RTA_PAYLOAD(opt) < sizeof(*qopt))
1460 return -EINVAL;
1461 qopt = RTA_DATA(opt);
1463 sch->stats_lock = &sch->dev->queue_lock;
1465 q->defcls = qopt->defcls;
1466 for (i = 0; i < HFSC_HSIZE; i++)
1467 INIT_LIST_HEAD(&q->clhash[i]);
1468 q->eligible = RB_ROOT;
1469 INIT_LIST_HEAD(&q->droplist);
1470 skb_queue_head_init(&q->requeue);
1472 q->root.refcnt = 1;
1473 q->root.classid = sch->handle;
1474 q->root.sched = q;
1475 q->root.qdisc = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
1476 sch->handle);
1477 if (q->root.qdisc == NULL)
1478 q->root.qdisc = &noop_qdisc;
1479 q->root.stats_lock = &sch->dev->queue_lock;
1480 INIT_LIST_HEAD(&q->root.children);
1481 q->root.vt_tree = RB_ROOT;
1482 q->root.cf_tree = RB_ROOT;
1484 list_add(&q->root.hlist, &q->clhash[hfsc_hash(q->root.classid)]);
1486 qdisc_watchdog_init(&q->watchdog, sch);
1488 return 0;
1491 static int
1492 hfsc_change_qdisc(struct Qdisc *sch, struct rtattr *opt)
1494 struct hfsc_sched *q = qdisc_priv(sch);
1495 struct tc_hfsc_qopt *qopt;
1497 if (opt == NULL || RTA_PAYLOAD(opt) < sizeof(*qopt))
1498 return -EINVAL;
1499 qopt = RTA_DATA(opt);
1501 sch_tree_lock(sch);
1502 q->defcls = qopt->defcls;
1503 sch_tree_unlock(sch);
1505 return 0;
1508 static void
1509 hfsc_reset_class(struct hfsc_class *cl)
1511 cl->cl_total = 0;
1512 cl->cl_cumul = 0;
1513 cl->cl_d = 0;
1514 cl->cl_e = 0;
1515 cl->cl_vt = 0;
1516 cl->cl_vtadj = 0;
1517 cl->cl_vtoff = 0;
1518 cl->cl_cvtmin = 0;
1519 cl->cl_cvtmax = 0;
1520 cl->cl_cvtoff = 0;
1521 cl->cl_pcvtoff = 0;
1522 cl->cl_vtperiod = 0;
1523 cl->cl_parentperiod = 0;
1524 cl->cl_f = 0;
1525 cl->cl_myf = 0;
1526 cl->cl_myfadj = 0;
1527 cl->cl_cfmin = 0;
1528 cl->cl_nactive = 0;
1530 cl->vt_tree = RB_ROOT;
1531 cl->cf_tree = RB_ROOT;
1532 qdisc_reset(cl->qdisc);
1534 if (cl->cl_flags & HFSC_RSC)
1535 rtsc_init(&cl->cl_deadline, &cl->cl_rsc, 0, 0);
1536 if (cl->cl_flags & HFSC_FSC)
1537 rtsc_init(&cl->cl_virtual, &cl->cl_fsc, 0, 0);
1538 if (cl->cl_flags & HFSC_USC)
1539 rtsc_init(&cl->cl_ulimit, &cl->cl_usc, 0, 0);
1542 static void
1543 hfsc_reset_qdisc(struct Qdisc *sch)
1545 struct hfsc_sched *q = qdisc_priv(sch);
1546 struct hfsc_class *cl;
1547 unsigned int i;
1549 for (i = 0; i < HFSC_HSIZE; i++) {
1550 list_for_each_entry(cl, &q->clhash[i], hlist)
1551 hfsc_reset_class(cl);
1553 __skb_queue_purge(&q->requeue);
1554 q->eligible = RB_ROOT;
1555 INIT_LIST_HEAD(&q->droplist);
1556 qdisc_watchdog_cancel(&q->watchdog);
1557 sch->q.qlen = 0;
1560 static void
1561 hfsc_destroy_qdisc(struct Qdisc *sch)
1563 struct hfsc_sched *q = qdisc_priv(sch);
1564 struct hfsc_class *cl, *next;
1565 unsigned int i;
1567 for (i = 0; i < HFSC_HSIZE; i++) {
1568 list_for_each_entry_safe(cl, next, &q->clhash[i], hlist)
1569 hfsc_destroy_class(sch, cl);
1571 __skb_queue_purge(&q->requeue);
1572 qdisc_watchdog_cancel(&q->watchdog);
1575 static int
1576 hfsc_dump_qdisc(struct Qdisc *sch, struct sk_buff *skb)
1578 struct hfsc_sched *q = qdisc_priv(sch);
1579 unsigned char *b = skb->tail;
1580 struct tc_hfsc_qopt qopt;
1582 qopt.defcls = q->defcls;
1583 RTA_PUT(skb, TCA_OPTIONS, sizeof(qopt), &qopt);
1584 return skb->len;
1586 rtattr_failure:
1587 skb_trim(skb, b - skb->data);
1588 return -1;
1591 static int
1592 hfsc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
1594 struct hfsc_class *cl;
1595 unsigned int len;
1596 int err;
1598 cl = hfsc_classify(skb, sch, &err);
1599 if (cl == NULL) {
1600 if (err == NET_XMIT_BYPASS)
1601 sch->qstats.drops++;
1602 kfree_skb(skb);
1603 return err;
1606 len = skb->len;
1607 err = cl->qdisc->enqueue(skb, cl->qdisc);
1608 if (unlikely(err != NET_XMIT_SUCCESS)) {
1609 cl->qstats.drops++;
1610 sch->qstats.drops++;
1611 return err;
1614 if (cl->qdisc->q.qlen == 1)
1615 set_active(cl, len);
1617 cl->bstats.packets++;
1618 cl->bstats.bytes += len;
1619 sch->bstats.packets++;
1620 sch->bstats.bytes += len;
1621 sch->q.qlen++;
1623 return NET_XMIT_SUCCESS;
1626 static struct sk_buff *
1627 hfsc_dequeue(struct Qdisc *sch)
1629 struct hfsc_sched *q = qdisc_priv(sch);
1630 struct hfsc_class *cl;
1631 struct sk_buff *skb;
1632 u64 cur_time;
1633 unsigned int next_len;
1634 int realtime = 0;
1636 if (sch->q.qlen == 0)
1637 return NULL;
1638 if ((skb = __skb_dequeue(&q->requeue)))
1639 goto out;
1641 PSCHED_GET_TIME(cur_time);
1644 * if there are eligible classes, use real-time criteria.
1645 * find the class with the minimum deadline among
1646 * the eligible classes.
1648 if ((cl = eltree_get_mindl(q, cur_time)) != NULL) {
1649 realtime = 1;
1650 } else {
1652 * use link-sharing criteria
1653 * get the class with the minimum vt in the hierarchy
1655 cl = vttree_get_minvt(&q->root, cur_time);
1656 if (cl == NULL) {
1657 sch->qstats.overlimits++;
1658 hfsc_schedule_watchdog(sch);
1659 return NULL;
1663 skb = cl->qdisc->dequeue(cl->qdisc);
1664 if (skb == NULL) {
1665 if (net_ratelimit())
1666 printk("HFSC: Non-work-conserving qdisc ?\n");
1667 return NULL;
1670 update_vf(cl, skb->len, cur_time);
1671 if (realtime)
1672 cl->cl_cumul += skb->len;
1674 if (cl->qdisc->q.qlen != 0) {
1675 if (cl->cl_flags & HFSC_RSC) {
1676 /* update ed */
1677 next_len = qdisc_peek_len(cl->qdisc);
1678 if (realtime)
1679 update_ed(cl, next_len);
1680 else
1681 update_d(cl, next_len);
1683 } else {
1684 /* the class becomes passive */
1685 set_passive(cl);
1688 out:
1689 sch->flags &= ~TCQ_F_THROTTLED;
1690 sch->q.qlen--;
1692 return skb;
1695 static int
1696 hfsc_requeue(struct sk_buff *skb, struct Qdisc *sch)
1698 struct hfsc_sched *q = qdisc_priv(sch);
1700 __skb_queue_head(&q->requeue, skb);
1701 sch->q.qlen++;
1702 sch->qstats.requeues++;
1703 return NET_XMIT_SUCCESS;
1706 static unsigned int
1707 hfsc_drop(struct Qdisc *sch)
1709 struct hfsc_sched *q = qdisc_priv(sch);
1710 struct hfsc_class *cl;
1711 unsigned int len;
1713 list_for_each_entry(cl, &q->droplist, dlist) {
1714 if (cl->qdisc->ops->drop != NULL &&
1715 (len = cl->qdisc->ops->drop(cl->qdisc)) > 0) {
1716 if (cl->qdisc->q.qlen == 0) {
1717 update_vf(cl, 0, 0);
1718 set_passive(cl);
1719 } else {
1720 list_move_tail(&cl->dlist, &q->droplist);
1722 cl->qstats.drops++;
1723 sch->qstats.drops++;
1724 sch->q.qlen--;
1725 return len;
1728 return 0;
1731 static struct Qdisc_class_ops hfsc_class_ops = {
1732 .change = hfsc_change_class,
1733 .delete = hfsc_delete_class,
1734 .graft = hfsc_graft_class,
1735 .leaf = hfsc_class_leaf,
1736 .qlen_notify = hfsc_qlen_notify,
1737 .get = hfsc_get_class,
1738 .put = hfsc_put_class,
1739 .bind_tcf = hfsc_bind_tcf,
1740 .unbind_tcf = hfsc_unbind_tcf,
1741 .tcf_chain = hfsc_tcf_chain,
1742 .dump = hfsc_dump_class,
1743 .dump_stats = hfsc_dump_class_stats,
1744 .walk = hfsc_walk
1747 static struct Qdisc_ops hfsc_qdisc_ops = {
1748 .id = "hfsc",
1749 .init = hfsc_init_qdisc,
1750 .change = hfsc_change_qdisc,
1751 .reset = hfsc_reset_qdisc,
1752 .destroy = hfsc_destroy_qdisc,
1753 .dump = hfsc_dump_qdisc,
1754 .enqueue = hfsc_enqueue,
1755 .dequeue = hfsc_dequeue,
1756 .requeue = hfsc_requeue,
1757 .drop = hfsc_drop,
1758 .cl_ops = &hfsc_class_ops,
1759 .priv_size = sizeof(struct hfsc_sched),
1760 .owner = THIS_MODULE
1763 static int __init
1764 hfsc_init(void)
1766 return register_qdisc(&hfsc_qdisc_ops);
1769 static void __exit
1770 hfsc_cleanup(void)
1772 unregister_qdisc(&hfsc_qdisc_ops);
1775 MODULE_LICENSE("GPL");
1776 module_init(hfsc_init);
1777 module_exit(hfsc_cleanup);