[SK_BUFF]: Convert skb->tail to sk_buff_data_t
[powerpc.git] / net / sched / sch_hfsc.c
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/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>
72
73 /*
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.
78  *
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.
84  */
85
86 struct internal_sc
87 {
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 */
94 };
95
96 /* runtime service curve */
97 struct runtime_sc
98 {
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 */
107 };
108
109 enum hfsc_class_flags
110 {
111         HFSC_RSC = 0x1,
112         HFSC_FSC = 0x2,
113         HFSC_USC = 0x4
114 };
115
116 struct hfsc_class
117 {
118         u32             classid;        /* class id */
119         unsigned int    refcnt;         /* usage count */
120
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 */
128
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 */
134
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 */
142
143         u64     cl_total;               /* total work in bytes */
144         u64     cl_cumul;               /* cumulative work in bytes done by
145                                            real-time criteria */
146
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 */
168
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 */
176
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 */
181 };
182
183 #define HFSC_HSIZE      16
184
185 struct hfsc_sched
186 {
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 */
195 };
196
197 #define HT_INFINITY     0xffffffffffffffffULL   /* infinite time value */
198
199
200 /*
201  * eligible tree holds backlogged classes being sorted by their eligible times.
202  * there is one eligible tree per hfsc instance.
203  */
204
205 static void
206 eltree_insert(struct hfsc_class *cl)
207 {
208         struct rb_node **p = &cl->sched->eligible.rb_node;
209         struct rb_node *parent = NULL;
210         struct hfsc_class *cl1;
211
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;
219         }
220         rb_link_node(&cl->el_node, parent, p);
221         rb_insert_color(&cl->el_node, &cl->sched->eligible);
222 }
223
224 static inline void
225 eltree_remove(struct hfsc_class *cl)
226 {
227         rb_erase(&cl->el_node, &cl->sched->eligible);
228 }
229
230 static inline void
231 eltree_update(struct hfsc_class *cl)
232 {
233         eltree_remove(cl);
234         eltree_insert(cl);
235 }
236
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)
240 {
241         struct hfsc_class *p, *cl = NULL;
242         struct rb_node *n;
243
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;
250         }
251         return cl;
252 }
253
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)
257 {
258         struct rb_node *n;
259
260         n = rb_first(&q->eligible);
261         if (n == NULL)
262                 return NULL;
263         return rb_entry(n, struct hfsc_class, el_node);
264 }
265
266 /*
267  * vttree holds holds backlogged child classes being sorted by their virtual
268  * time. each intermediate class has one vttree.
269  */
270 static void
271 vttree_insert(struct hfsc_class *cl)
272 {
273         struct rb_node **p = &cl->cl_parent->vt_tree.rb_node;
274         struct rb_node *parent = NULL;
275         struct hfsc_class *cl1;
276
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;
284         }
285         rb_link_node(&cl->vt_node, parent, p);
286         rb_insert_color(&cl->vt_node, &cl->cl_parent->vt_tree);
287 }
288
289 static inline void
290 vttree_remove(struct hfsc_class *cl)
291 {
292         rb_erase(&cl->vt_node, &cl->cl_parent->vt_tree);
293 }
294
295 static inline void
296 vttree_update(struct hfsc_class *cl)
297 {
298         vttree_remove(cl);
299         vttree_insert(cl);
300 }
301
302 static inline struct hfsc_class *
303 vttree_firstfit(struct hfsc_class *cl, u64 cur_time)
304 {
305         struct hfsc_class *p;
306         struct rb_node *n;
307
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;
312         }
313         return NULL;
314 }
315
316 /*
317  * get the leaf class with the minimum vt in the hierarchy
318  */
319 static struct hfsc_class *
320 vttree_get_minvt(struct hfsc_class *cl, u64 cur_time)
321 {
322         /* if root-class's cfmin is bigger than cur_time nothing to do */
323         if (cl->cl_cfmin > cur_time)
324                 return NULL;
325
326         while (cl->level > 0) {
327                 cl = vttree_firstfit(cl, cur_time);
328                 if (cl == NULL)
329                         return NULL;
330                 /*
331                  * update parent's cl_cvtmin.
332                  */
333                 if (cl->cl_parent->cl_cvtmin < cl->cl_vt)
334                         cl->cl_parent->cl_cvtmin = cl->cl_vt;
335         }
336         return cl;
337 }
338
339 static void
340 cftree_insert(struct hfsc_class *cl)
341 {
342         struct rb_node **p = &cl->cl_parent->cf_tree.rb_node;
343         struct rb_node *parent = NULL;
344         struct hfsc_class *cl1;
345
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;
353         }
354         rb_link_node(&cl->cf_node, parent, p);
355         rb_insert_color(&cl->cf_node, &cl->cl_parent->cf_tree);
356 }
357
358 static inline void
359 cftree_remove(struct hfsc_class *cl)
360 {
361         rb_erase(&cl->cf_node, &cl->cl_parent->cf_tree);
362 }
363
364 static inline void
365 cftree_update(struct hfsc_class *cl)
366 {
367         cftree_remove(cl);
368         cftree_insert(cl);
369 }
370
371 /*
372  * service curve support functions
373  *
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
381  *
382  * The clock source resolution with ktime is 1.024us.
383  *
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.
387  *
388  *  bits/sec      100Kbps     1Mbps     10Mbps     100Mbps    1Gbps
389  *  ------------+-------------------------------------------------------
390  *  bytes/1.024us 12.8e-3    128e-3     1280e-3    12800e-3   128000e-3
391  *
392  *  1.024us/byte  78.125     7.8125     0.78125    0.078125   0.0078125
393  */
394 #define SM_SHIFT        20
395 #define ISM_SHIFT       18
396
397 #define SM_MASK         ((1ULL << SM_SHIFT) - 1)
398 #define ISM_MASK        ((1ULL << ISM_SHIFT) - 1)
399
400 static inline u64
401 seg_x2y(u64 x, u64 sm)
402 {
403         u64 y;
404
405         /*
406          * compute
407          *      y = x * sm >> SM_SHIFT
408          * but divide it for the upper and lower bits to avoid overflow
409          */
410         y = (x >> SM_SHIFT) * sm + (((x & SM_MASK) * sm) >> SM_SHIFT);
411         return y;
412 }
413
414 static inline u64
415 seg_y2x(u64 y, u64 ism)
416 {
417         u64 x;
418
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);
426         }
427         return x;
428 }
429
430 /* Convert m (bps) into sm (bytes/psched us) */
431 static u64
432 m2sm(u32 m)
433 {
434         u64 sm;
435
436         sm = ((u64)m << SM_SHIFT);
437         sm += PSCHED_TICKS_PER_SEC - 1;
438         do_div(sm, PSCHED_TICKS_PER_SEC);
439         return sm;
440 }
441
442 /* convert m (bps) into ism (psched us/byte) */
443 static u64
444 m2ism(u32 m)
445 {
446         u64 ism;
447
448         if (m == 0)
449                 ism = HT_INFINITY;
450         else {
451                 ism = ((u64)PSCHED_TICKS_PER_SEC << ISM_SHIFT);
452                 ism += m - 1;
453                 do_div(ism, m);
454         }
455         return ism;
456 }
457
458 /* convert d (us) into dx (psched us) */
459 static u64
460 d2dx(u32 d)
461 {
462         u64 dx;
463
464         dx = ((u64)d * PSCHED_TICKS_PER_SEC);
465         dx += USEC_PER_SEC - 1;
466         do_div(dx, USEC_PER_SEC);
467         return dx;
468 }
469
470 /* convert sm (bytes/psched us) into m (bps) */
471 static u32
472 sm2m(u64 sm)
473 {
474         u64 m;
475
476         m = (sm * PSCHED_TICKS_PER_SEC) >> SM_SHIFT;
477         return (u32)m;
478 }
479
480 /* convert dx (psched us) into d (us) */
481 static u32
482 dx2d(u64 dx)
483 {
484         u64 d;
485
486         d = dx * USEC_PER_SEC;
487         do_div(d, PSCHED_TICKS_PER_SEC);
488         return (u32)d;
489 }
490
491 static void
492 sc2isc(struct tc_service_curve *sc, struct internal_sc *isc)
493 {
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);
500 }
501
502 /*
503  * initialize the runtime service curve with the given internal
504  * service curve starting at (x, y).
505  */
506 static void
507 rtsc_init(struct runtime_sc *rtsc, struct internal_sc *isc, u64 x, u64 y)
508 {
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;
517 }
518
519 /*
520  * calculate the y-projection of the runtime service curve by the
521  * given x-projection value
522  */
523 static u64
524 rtsc_y2x(struct runtime_sc *rtsc, u64 y)
525 {
526         u64 x;
527
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);
540         }
541         return x;
542 }
543
544 static u64
545 rtsc_x2y(struct runtime_sc *rtsc, u64 x)
546 {
547         u64 y;
548
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;
559 }
560
561 /*
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).
564  */
565 static void
566 rtsc_min(struct runtime_sc *rtsc, struct internal_sc *isc, u64 x, u64 y)
567 {
568         u64 y1, y2, dx, dy;
569         u32 dsm;
570
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;
580         }
581
582         /*
583          * service curve is concave
584          * compute the two y values of the current rtsc
585          *      y1: at x
586          *      y2: at (x + dx)
587          */
588         y1 = rtsc_x2y(rtsc, x);
589         if (y1 <= y) {
590                 /* rtsc is below isc, no change to rtsc */
591                 return;
592         }
593
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;
602         }
603
604         /*
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)
609          */
610         dx = (y1 - y) << SM_SHIFT;
611         dsm = isc->sm1 - isc->sm2;
612         do_div(dx, dsm);
613         /*
614          * check if (x, y1) belongs to the 1st segment of rtsc.
615          * if so, add the offset.
616          */
617         if (rtsc->x + rtsc->dx > x)
618                 dx += rtsc->x + rtsc->dx - x;
619         dy = seg_x2y(dx, isc->sm1);
620
621         rtsc->x = x;
622         rtsc->y = y;
623         rtsc->dx = dx;
624         rtsc->dy = dy;
625         return;
626 }
627
628 static void
629 init_ed(struct hfsc_class *cl, unsigned int next_len)
630 {
631         u64 cur_time;
632
633         PSCHED_GET_TIME(cur_time);
634
635         /* update the deadline curve */
636         rtsc_min(&cl->cl_deadline, &cl->cl_rsc, cur_time, cl->cl_cumul);
637
638         /*
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.
642          */
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;
647         }
648
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);
652
653         eltree_insert(cl);
654 }
655
656 static void
657 update_ed(struct hfsc_class *cl, unsigned int next_len)
658 {
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);
661
662         eltree_update(cl);
663 }
664
665 static inline void
666 update_d(struct hfsc_class *cl, unsigned int next_len)
667 {
668         cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
669 }
670
671 static inline void
672 update_cfmin(struct hfsc_class *cl)
673 {
674         struct rb_node *n = rb_first(&cl->cf_tree);
675         struct hfsc_class *p;
676
677         if (n == NULL) {
678                 cl->cl_cfmin = 0;
679                 return;
680         }
681         p = rb_entry(n, struct hfsc_class, cf_node);
682         cl->cl_cfmin = p->cl_f;
683 }
684
685 static void
686 init_vf(struct hfsc_class *cl, unsigned int len)
687 {
688         struct hfsc_class *max_cl;
689         struct rb_node *n;
690         u64 vt, f, cur_time;
691         int go_active;
692
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;
700
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);
705                                 /*
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.
709                                  */
710                                 vt = max_cl->cl_vt;
711                                 if (cl->cl_parent->cl_cvtmin != 0)
712                                         vt = (cl->cl_parent->cl_cvtmin + vt)/2;
713
714                                 if (cl->cl_parent->cl_vtperiod !=
715                                     cl->cl_parentperiod || vt > cl->cl_vt)
716                                         cl->cl_vt = vt;
717                         } else {
718                                 /*
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.
723                                  */
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;
729                         }
730
731                         cl->cl_vtoff = cl->cl_parent->cl_cvtoff -
732                                                         cl->cl_pcvtoff;
733
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;
741                         }
742                         cl->cl_vtadj = 0;
743
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;
749
750                         vttree_insert(cl);
751                         cftree_insert(cl);
752
753                         if (cl->cl_flags & HFSC_USC) {
754                                 /* class has upper limit curve */
755                                 if (cur_time == 0)
756                                         PSCHED_GET_TIME(cur_time);
757
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;
765                         }
766                 }
767
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);
773                 }
774         }
775 }
776
777 static void
778 update_vf(struct hfsc_class *cl, unsigned int len, u64 cur_time)
779 {
780         u64 f; /* , myf_bound, delta; */
781         int go_passive = 0;
782
783         if (cl->qdisc->q.qlen == 0 && cl->cl_flags & HFSC_FSC)
784                 go_passive = 1;
785
786         for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
787                 cl->cl_total += len;
788
789                 if (!(cl->cl_flags & HFSC_FSC) || cl->cl_nactive == 0)
790                         continue;
791
792                 if (go_passive && --cl->cl_nactive == 0)
793                         go_passive = 1;
794                 else
795                         go_passive = 0;
796
797                 if (go_passive) {
798                         /* no more active child, going passive */
799
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;
803
804                         /* remove this class from the vt tree */
805                         vttree_remove(cl);
806
807                         cftree_remove(cl);
808                         update_cfmin(cl->cl_parent);
809
810                         continue;
811                 }
812
813                 /*
814                  * update vt and f
815                  */
816                 cl->cl_vt = rtsc_y2x(&cl->cl_virtual, cl->cl_total)
817                             - cl->cl_vtoff + cl->cl_vtadj;
818
819                 /*
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.
823                  */
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;
827                 }
828
829                 /* update the vt tree */
830                 vttree_update(cl);
831
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
836                         /*
837                          * This code causes classes to stay way under their
838                          * limit when multiple classes are used at gigabit
839                          * speed. needs investigation. -kaber
840                          */
841                         /*
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.
847                          */
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;
853                         }
854 #endif
855                 }
856
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);
862                 }
863         }
864 }
865
866 static void
867 set_active(struct hfsc_class *cl, unsigned int len)
868 {
869         if (cl->cl_flags & HFSC_RSC)
870                 init_ed(cl, len);
871         if (cl->cl_flags & HFSC_FSC)
872                 init_vf(cl, len);
873
874         list_add_tail(&cl->dlist, &cl->sched->droplist);
875 }
876
877 static void
878 set_passive(struct hfsc_class *cl)
879 {
880         if (cl->cl_flags & HFSC_RSC)
881                 eltree_remove(cl);
882
883         list_del(&cl->dlist);
884
885         /*
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.
888          */
889 }
890
891 /*
892  * hack to get length of first packet in queue.
893  */
894 static unsigned int
895 qdisc_peek_len(struct Qdisc *sch)
896 {
897         struct sk_buff *skb;
898         unsigned int len;
899
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;
905         }
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;
912         }
913         return len;
914 }
915
916 static void
917 hfsc_purge_queue(struct Qdisc *sch, struct hfsc_class *cl)
918 {
919         unsigned int len = cl->qdisc->q.qlen;
920
921         qdisc_reset(cl->qdisc);
922         qdisc_tree_decrease_qlen(cl->qdisc, len);
923 }
924
925 static void
926 hfsc_adjust_levels(struct hfsc_class *cl)
927 {
928         struct hfsc_class *p;
929         unsigned int level;
930
931         do {
932                 level = 0;
933                 list_for_each_entry(p, &cl->children, siblings) {
934                         if (p->level >= level)
935                                 level = p->level + 1;
936                 }
937                 cl->level = level;
938         } while ((cl = cl->cl_parent) != NULL);
939 }
940
941 static inline unsigned int
942 hfsc_hash(u32 h)
943 {
944         h ^= h >> 8;
945         h ^= h >> 4;
946
947         return h & (HFSC_HSIZE - 1);
948 }
949
950 static inline struct hfsc_class *
951 hfsc_find_class(u32 classid, struct Qdisc *sch)
952 {
953         struct hfsc_sched *q = qdisc_priv(sch);
954         struct hfsc_class *cl;
955
956         list_for_each_entry(cl, &q->clhash[hfsc_hash(classid)], hlist) {
957                 if (cl->classid == classid)
958                         return cl;
959         }
960         return NULL;
961 }
962
963 static void
964 hfsc_change_rsc(struct hfsc_class *cl, struct tc_service_curve *rsc,
965                 u64 cur_time)
966 {
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;
973         }
974         cl->cl_flags |= HFSC_RSC;
975 }
976
977 static void
978 hfsc_change_fsc(struct hfsc_class *cl, struct tc_service_curve *fsc)
979 {
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;
983 }
984
985 static void
986 hfsc_change_usc(struct hfsc_class *cl, struct tc_service_curve *usc,
987                 u64 cur_time)
988 {
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;
992 }
993
994 static int
995 hfsc_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
996                   struct rtattr **tca, unsigned long *arg)
997 {
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;
1005
1006         if (opt == NULL || rtattr_parse_nested(tb, TCA_HFSC_MAX, opt))
1007                 return -EINVAL;
1008
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;
1015         }
1016
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;
1023         }
1024
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;
1031         }
1032
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;
1039                 }
1040                 PSCHED_GET_TIME(cur_time);
1041
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);
1049
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);
1055                 }
1056                 sch_tree_unlock(sch);
1057
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;
1064         }
1065
1066         if (parentid == TC_H_ROOT)
1067                 return -EEXIST;
1068
1069         parent = &q->root;
1070         if (parentid) {
1071                 parent = hfsc_find_class(parentid, sch);
1072                 if (parent == NULL)
1073                         return -ENOENT;
1074         }
1075
1076         if (classid == 0 || TC_H_MAJ(classid ^ sch->handle) != 0)
1077                 return -EINVAL;
1078         if (hfsc_find_class(classid, sch))
1079                 return -EEXIST;
1080
1081         if (rsc == NULL && fsc == NULL)
1082                 return -EINVAL;
1083
1084         cl = kzalloc(sizeof(struct hfsc_class), GFP_KERNEL);
1085         if (cl == NULL)
1086                 return -ENOBUFS;
1087
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);
1094
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;
1106
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);
1115
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;
1123 }
1124
1125 static void
1126 hfsc_destroy_filters(struct tcf_proto **fl)
1127 {
1128         struct tcf_proto *tp;
1129
1130         while ((tp = *fl) != NULL) {
1131                 *fl = tp->next;
1132                 tcf_destroy(tp);
1133         }
1134 }
1135
1136 static void
1137 hfsc_destroy_class(struct Qdisc *sch, struct hfsc_class *cl)
1138 {
1139         struct hfsc_sched *q = qdisc_priv(sch);
1140
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);
1148 }
1149
1150 static int
1151 hfsc_delete_class(struct Qdisc *sch, unsigned long arg)
1152 {
1153         struct hfsc_sched *q = qdisc_priv(sch);
1154         struct hfsc_class *cl = (struct hfsc_class *)arg;
1155
1156         if (cl->level > 0 || cl->filter_cnt > 0 || cl == &q->root)
1157                 return -EBUSY;
1158
1159         sch_tree_lock(sch);
1160
1161         list_del(&cl->siblings);
1162         hfsc_adjust_levels(cl->cl_parent);
1163
1164         hfsc_purge_queue(sch, cl);
1165         list_del(&cl->hlist);
1166
1167         if (--cl->refcnt == 0)
1168                 hfsc_destroy_class(sch, cl);
1169
1170         sch_tree_unlock(sch);
1171         return 0;
1172 }
1173
1174 static struct hfsc_class *
1175 hfsc_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
1176 {
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;
1182
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;
1187
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;
1198                 }
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 */
1206                 }
1207
1208                 if (cl->level == 0)
1209                         return cl; /* hit leaf class */
1210
1211                 /* apply inner filter chain */
1212                 tcf = cl->filter_list;
1213         }
1214
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;
1219
1220         return cl;
1221 }
1222
1223 static int
1224 hfsc_graft_class(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
1225                  struct Qdisc **old)
1226 {
1227         struct hfsc_class *cl = (struct hfsc_class *)arg;
1228
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;
1238         }
1239
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;
1245 }
1246
1247 static struct Qdisc *
1248 hfsc_class_leaf(struct Qdisc *sch, unsigned long arg)
1249 {
1250         struct hfsc_class *cl = (struct hfsc_class *)arg;
1251
1252         if (cl != NULL && cl->level == 0)
1253                 return cl->qdisc;
1254
1255         return NULL;
1256 }
1257
1258 static void
1259 hfsc_qlen_notify(struct Qdisc *sch, unsigned long arg)
1260 {
1261         struct hfsc_class *cl = (struct hfsc_class *)arg;
1262
1263         if (cl->qdisc->q.qlen == 0) {
1264                 update_vf(cl, 0, 0);
1265                 set_passive(cl);
1266         }
1267 }
1268
1269 static unsigned long
1270 hfsc_get_class(struct Qdisc *sch, u32 classid)
1271 {
1272         struct hfsc_class *cl = hfsc_find_class(classid, sch);
1273
1274         if (cl != NULL)
1275                 cl->refcnt++;
1276
1277         return (unsigned long)cl;
1278 }
1279
1280 static void
1281 hfsc_put_class(struct Qdisc *sch, unsigned long arg)
1282 {
1283         struct hfsc_class *cl = (struct hfsc_class *)arg;
1284
1285         if (--cl->refcnt == 0)
1286                 hfsc_destroy_class(sch, cl);
1287 }
1288
1289 static unsigned long
1290 hfsc_bind_tcf(struct Qdisc *sch, unsigned long parent, u32 classid)
1291 {
1292         struct hfsc_class *p = (struct hfsc_class *)parent;
1293         struct hfsc_class *cl = hfsc_find_class(classid, sch);
1294
1295         if (cl != NULL) {
1296                 if (p != NULL && p->level <= cl->level)
1297                         return 0;
1298                 cl->filter_cnt++;
1299         }
1300
1301         return (unsigned long)cl;
1302 }
1303
1304 static void
1305 hfsc_unbind_tcf(struct Qdisc *sch, unsigned long arg)
1306 {
1307         struct hfsc_class *cl = (struct hfsc_class *)arg;
1308
1309         cl->filter_cnt--;
1310 }
1311
1312 static struct tcf_proto **
1313 hfsc_tcf_chain(struct Qdisc *sch, unsigned long arg)
1314 {
1315         struct hfsc_sched *q = qdisc_priv(sch);
1316         struct hfsc_class *cl = (struct hfsc_class *)arg;
1317
1318         if (cl == NULL)
1319                 cl = &q->root;
1320
1321         return &cl->filter_list;
1322 }
1323
1324 static int
1325 hfsc_dump_sc(struct sk_buff *skb, int attr, struct internal_sc *sc)
1326 {
1327         struct tc_service_curve tsc;
1328
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);
1333
1334         return skb->len;
1335
1336  rtattr_failure:
1337         return -1;
1338 }
1339
1340 static inline int
1341 hfsc_dump_curves(struct sk_buff *skb, struct hfsc_class *cl)
1342 {
1343         if ((cl->cl_flags & HFSC_RSC) &&
1344             (hfsc_dump_sc(skb, TCA_HFSC_RSC, &cl->cl_rsc) < 0))
1345                 goto rtattr_failure;
1346
1347         if ((cl->cl_flags & HFSC_FSC) &&
1348             (hfsc_dump_sc(skb, TCA_HFSC_FSC, &cl->cl_fsc) < 0))
1349                 goto rtattr_failure;
1350
1351         if ((cl->cl_flags & HFSC_USC) &&
1352             (hfsc_dump_sc(skb, TCA_HFSC_USC, &cl->cl_usc) < 0))
1353                 goto rtattr_failure;
1354
1355         return skb->len;
1356
1357  rtattr_failure:
1358         return -1;
1359 }
1360
1361 static int
1362 hfsc_dump_class(struct Qdisc *sch, unsigned long arg, struct sk_buff *skb,
1363                 struct tcmsg *tcm)
1364 {
1365         struct hfsc_class *cl = (struct hfsc_class *)arg;
1366         unsigned char *b = skb_tail_pointer(skb);
1367         struct rtattr *rta = (struct rtattr *)b;
1368
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;
1373
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_pointer(skb) - b;
1378         return skb->len;
1379
1380  rtattr_failure:
1381         skb_trim(skb, b - skb->data);
1382         return -1;
1383 }
1384
1385 static int
1386 hfsc_dump_class_stats(struct Qdisc *sch, unsigned long arg,
1387         struct gnet_dump *d)
1388 {
1389         struct hfsc_class *cl = (struct hfsc_class *)arg;
1390         struct tc_hfsc_stats xstats;
1391
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;
1397
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;
1404
1405         return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
1406 }
1407
1408
1409
1410 static void
1411 hfsc_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1412 {
1413         struct hfsc_sched *q = qdisc_priv(sch);
1414         struct hfsc_class *cl;
1415         unsigned int i;
1416
1417         if (arg->stop)
1418                 return;
1419
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;
1425                         }
1426                         if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
1427                                 arg->stop = 1;
1428                                 return;
1429                         }
1430                         arg->count++;
1431                 }
1432         }
1433 }
1434
1435 static void
1436 hfsc_schedule_watchdog(struct Qdisc *sch)
1437 {
1438         struct hfsc_sched *q = qdisc_priv(sch);
1439         struct hfsc_class *cl;
1440         u64 next_time = 0;
1441
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;
1447         }
1448         WARN_ON(next_time == 0);
1449         qdisc_watchdog_schedule(&q->watchdog, next_time);
1450 }
1451
1452 static int
1453 hfsc_init_qdisc(struct Qdisc *sch, struct rtattr *opt)
1454 {
1455         struct hfsc_sched *q = qdisc_priv(sch);
1456         struct tc_hfsc_qopt *qopt;
1457         unsigned int i;
1458
1459         if (opt == NULL || RTA_PAYLOAD(opt) < sizeof(*qopt))
1460                 return -EINVAL;
1461         qopt = RTA_DATA(opt);
1462
1463         sch->stats_lock = &sch->dev->queue_lock;
1464
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);
1471
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;
1483
1484         list_add(&q->root.hlist, &q->clhash[hfsc_hash(q->root.classid)]);
1485
1486         qdisc_watchdog_init(&q->watchdog, sch);
1487
1488         return 0;
1489 }
1490
1491 static int
1492 hfsc_change_qdisc(struct Qdisc *sch, struct rtattr *opt)
1493 {
1494         struct hfsc_sched *q = qdisc_priv(sch);
1495         struct tc_hfsc_qopt *qopt;
1496
1497         if (opt == NULL || RTA_PAYLOAD(opt) < sizeof(*qopt))
1498                 return -EINVAL;
1499         qopt = RTA_DATA(opt);
1500
1501         sch_tree_lock(sch);
1502         q->defcls = qopt->defcls;
1503         sch_tree_unlock(sch);
1504
1505         return 0;
1506 }
1507
1508 static void
1509 hfsc_reset_class(struct hfsc_class *cl)
1510 {
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;
1529
1530         cl->vt_tree = RB_ROOT;
1531         cl->cf_tree = RB_ROOT;
1532         qdisc_reset(cl->qdisc);
1533
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);
1540 }
1541
1542 static void
1543 hfsc_reset_qdisc(struct Qdisc *sch)
1544 {
1545         struct hfsc_sched *q = qdisc_priv(sch);
1546         struct hfsc_class *cl;
1547         unsigned int i;
1548
1549         for (i = 0; i < HFSC_HSIZE; i++) {
1550                 list_for_each_entry(cl, &q->clhash[i], hlist)
1551                         hfsc_reset_class(cl);
1552         }
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;
1558 }
1559
1560 static void
1561 hfsc_destroy_qdisc(struct Qdisc *sch)
1562 {
1563         struct hfsc_sched *q = qdisc_priv(sch);
1564         struct hfsc_class *cl, *next;
1565         unsigned int i;
1566
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);
1570         }
1571         __skb_queue_purge(&q->requeue);
1572         qdisc_watchdog_cancel(&q->watchdog);
1573 }
1574
1575 static int
1576 hfsc_dump_qdisc(struct Qdisc *sch, struct sk_buff *skb)
1577 {
1578         struct hfsc_sched *q = qdisc_priv(sch);
1579         unsigned char *b = skb_tail_pointer(skb);
1580         struct tc_hfsc_qopt qopt;
1581
1582         qopt.defcls = q->defcls;
1583         RTA_PUT(skb, TCA_OPTIONS, sizeof(qopt), &qopt);
1584         return skb->len;
1585
1586  rtattr_failure:
1587         skb_trim(skb, b - skb->data);
1588         return -1;
1589 }
1590
1591 static int
1592 hfsc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
1593 {
1594         struct hfsc_class *cl;
1595         unsigned int len;
1596         int err;
1597
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;
1604         }
1605
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;
1612         }
1613
1614         if (cl->qdisc->q.qlen == 1)
1615                 set_active(cl, len);
1616
1617         cl->bstats.packets++;
1618         cl->bstats.bytes += len;
1619         sch->bstats.packets++;
1620         sch->bstats.bytes += len;
1621         sch->q.qlen++;
1622
1623         return NET_XMIT_SUCCESS;
1624 }
1625
1626 static struct sk_buff *
1627 hfsc_dequeue(struct Qdisc *sch)
1628 {
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;
1635
1636         if (sch->q.qlen == 0)
1637                 return NULL;
1638         if ((skb = __skb_dequeue(&q->requeue)))
1639                 goto out;
1640
1641         PSCHED_GET_TIME(cur_time);
1642
1643         /*
1644          * if there are eligible classes, use real-time criteria.
1645          * find the class with the minimum deadline among
1646          * the eligible classes.
1647          */
1648         if ((cl = eltree_get_mindl(q, cur_time)) != NULL) {
1649                 realtime = 1;
1650         } else {
1651                 /*
1652                  * use link-sharing criteria
1653                  * get the class with the minimum vt in the hierarchy
1654                  */
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;
1660                 }
1661         }
1662
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;
1668         }
1669
1670         update_vf(cl, skb->len, cur_time);
1671         if (realtime)
1672                 cl->cl_cumul += skb->len;
1673
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);
1682                 }
1683         } else {
1684                 /* the class becomes passive */
1685                 set_passive(cl);
1686         }
1687
1688  out:
1689         sch->flags &= ~TCQ_F_THROTTLED;
1690         sch->q.qlen--;
1691
1692         return skb;
1693 }
1694
1695 static int
1696 hfsc_requeue(struct sk_buff *skb, struct Qdisc *sch)
1697 {
1698         struct hfsc_sched *q = qdisc_priv(sch);
1699
1700         __skb_queue_head(&q->requeue, skb);
1701         sch->q.qlen++;
1702         sch->qstats.requeues++;
1703         return NET_XMIT_SUCCESS;
1704 }
1705
1706 static unsigned int
1707 hfsc_drop(struct Qdisc *sch)
1708 {
1709         struct hfsc_sched *q = qdisc_priv(sch);
1710         struct hfsc_class *cl;
1711         unsigned int len;
1712
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);
1721                         }
1722                         cl->qstats.drops++;
1723                         sch->qstats.drops++;
1724                         sch->q.qlen--;
1725                         return len;
1726                 }
1727         }
1728         return 0;
1729 }
1730
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
1745 };
1746
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
1761 };
1762
1763 static int __init
1764 hfsc_init(void)
1765 {
1766         return register_qdisc(&hfsc_qdisc_ops);
1767 }
1768
1769 static void __exit
1770 hfsc_cleanup(void)
1771 {
1772         unregister_qdisc(&hfsc_qdisc_ops);
1773 }
1774
1775 MODULE_LICENSE("GPL");
1776 module_init(hfsc_init);
1777 module_exit(hfsc_cleanup);