ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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/config.h>
54 #include <linux/module.h>
55 #include <linux/types.h>
56 #include <linux/errno.h>
57 #include <linux/jiffies.h>
58 #include <linux/compiler.h>
59 #include <linux/spinlock.h>
60 #include <linux/skbuff.h>
61 #include <linux/string.h>
62 #include <linux/slab.h>
63 #include <linux/timer.h>
64 #include <linux/list.h>
65 #include <linux/init.h>
66 #include <linux/netdevice.h>
67 #include <linux/rtnetlink.h>
68 #include <linux/pkt_sched.h>
69 #include <net/pkt_sched.h>
70 #include <net/pkt_cls.h>
71 #include <asm/system.h>
72 #include <asm/div64.h>
73
74 #define HFSC_DEBUG 1
75
76 /*
77  * kernel internal service curve representation:
78  *   coordinates are given by 64 bit unsigned integers.
79  *   x-axis: unit is clock count.
80  *   y-axis: unit is byte.
81  *
82  *   The service curve parameters are converted to the internal
83  *   representation. The slope values are scaled to avoid overflow.
84  *   the inverse slope values as well as the y-projection of the 1st
85  *   segment are kept in order to to avoid 64-bit divide operations
86  *   that are expensive on 32-bit architectures.
87  */
88
89 struct internal_sc
90 {
91         u64     sm1;    /* scaled slope of the 1st segment */
92         u64     ism1;   /* scaled inverse-slope of the 1st segment */
93         u64     dx;     /* the x-projection of the 1st segment */
94         u64     dy;     /* the y-projection of the 1st segment */
95         u64     sm2;    /* scaled slope of the 2nd segment */
96         u64     ism2;   /* scaled inverse-slope of the 2nd segment */
97 };
98
99 /* runtime service curve */
100 struct runtime_sc
101 {
102         u64     x;      /* current starting position on x-axis */
103         u64     y;      /* current starting position on y-axis */
104         u64     sm1;    /* scaled slope of the 1st segment */
105         u64     ism1;   /* scaled inverse-slope of the 1st segment */
106         u64     dx;     /* the x-projection of the 1st segment */
107         u64     dy;     /* the y-projection of the 1st segment */
108         u64     sm2;    /* scaled slope of the 2nd segment */
109         u64     ism2;   /* scaled inverse-slope of the 2nd segment */
110 };
111
112 enum hfsc_class_flags
113 {
114         HFSC_RSC = 0x1,
115         HFSC_FSC = 0x2,
116         HFSC_USC = 0x4
117 };
118
119 struct hfsc_class
120 {
121         u32             classid;        /* class id */
122         unsigned int    refcnt;         /* usage count */
123
124         struct tc_stats stats;          /* generic statistics */
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 list_head actlist;       /* active children list */
136         struct list_head alist;         /* active children list member */
137         struct list_head ellist;        /* eligible list member */
138         struct list_head hlist;         /* hash list member */
139         struct list_head dlist;         /* drop list member */
140
141         u64     cl_total;               /* total work in bytes */
142         u64     cl_cumul;               /* cumulative work in bytes done by
143                                            real-time criteria */
144
145         u64     cl_d;                   /* deadline*/
146         u64     cl_e;                   /* eligible time */
147         u64     cl_vt;                  /* virtual time */
148         u64     cl_f;                   /* time when this class will fit for
149                                            link-sharing, max(myf, cfmin) */
150         u64     cl_myf;                 /* my fit-time (calculated from this
151                                            class's own upperlimit curve) */
152         u64     cl_myfadj;              /* my fit-time adjustment (to cancel
153                                            history dependence) */
154         u64     cl_cfmin;               /* earliest children's fit-time (used
155                                            with cl_myf to obtain cl_f) */
156         u64     cl_cvtmin;              /* minimal virtual time among the
157                                            children fit for link-sharing
158                                            (monotonic within a period) */
159         u64     cl_vtadj;               /* intra-period cumulative vt
160                                            adjustment */
161         u64     cl_vtoff;               /* inter-period cumulative vt offset */
162         u64     cl_cvtmax;              /* max child's vt in the last period */
163
164         struct internal_sc cl_rsc;      /* internal real-time service curve */
165         struct internal_sc cl_fsc;      /* internal fair service curve */
166         struct internal_sc cl_usc;      /* internal upperlimit service curve */
167         struct runtime_sc cl_deadline;  /* deadline curve */
168         struct runtime_sc cl_eligible;  /* eligible curve */
169         struct runtime_sc cl_virtual;   /* virtual curve */
170         struct runtime_sc cl_ulimit;    /* upperlimit curve */
171
172         unsigned long   cl_flags;       /* which curves are valid */
173         unsigned long   cl_vtperiod;    /* vt period sequence number */
174         unsigned long   cl_parentperiod;/* parent's vt period sequence number*/
175         unsigned long   cl_nactive;     /* number of active children */
176 };
177
178 #define HFSC_HSIZE      16
179
180 struct hfsc_sched
181 {
182         u16     defcls;                         /* default class id */
183         struct hfsc_class root;                 /* root class */
184         struct list_head clhash[HFSC_HSIZE];    /* class hash */
185         struct list_head eligible;              /* eligible list */
186         struct list_head droplist;              /* active leaf class list (for
187                                                    dropping) */
188         struct sk_buff_head requeue;            /* requeued packet */
189         struct timer_list wd_timer;             /* watchdog timer */
190 };
191
192 /*
193  * macros
194  */
195 #if PSCHED_CLOCK_SOURCE == PSCHED_GETTIMEOFDAY
196 #include <linux/time.h>
197 #undef PSCHED_GET_TIME
198 #define PSCHED_GET_TIME(stamp)                                          \
199 do {                                                                    \
200         struct timeval tv;                                              \
201         do_gettimeofday(&tv);                                           \
202         (stamp) = 1000000ULL * tv.tv_sec + tv.tv_usec;                  \
203 } while (0)
204 #endif
205
206 #if HFSC_DEBUG
207 #define ASSERT(cond)                                                    \
208 do {                                                                    \
209         if (unlikely(!(cond)))                                          \
210                 printk("assertion %s failed at %s:%i (%s)\n",           \
211                        #cond, __FILE__, __LINE__, __FUNCTION__);        \
212 } while (0)
213 #else
214 #define ASSERT(cond)
215 #endif /* HFSC_DEBUG */
216
217 #define HT_INFINITY     0xffffffffffffffffULL   /* infinite time value */
218
219
220 /*
221  * eligible list holds backlogged classes being sorted by their eligible times.
222  * there is one eligible list per hfsc instance.
223  */
224
225 static void
226 ellist_insert(struct hfsc_class *cl)
227 {
228         struct list_head *head = &cl->sched->eligible;
229         struct hfsc_class *p;
230
231         /* check the last entry first */
232         if (list_empty(head) ||
233             ((p = list_entry(head->prev, struct hfsc_class, ellist)) &&
234              p->cl_e <= cl->cl_e)) {
235                 list_add_tail(&cl->ellist, head);
236                 return;
237         }
238
239         list_for_each_entry(p, head, ellist) {
240                 if (cl->cl_e < p->cl_e) {
241                         /* insert cl before p */
242                         list_add_tail(&cl->ellist, &p->ellist);
243                         return;
244                 }
245         }
246         ASSERT(0); /* should not reach here */
247 }
248
249 static inline void
250 ellist_remove(struct hfsc_class *cl)
251 {
252         list_del(&cl->ellist);
253 }
254
255 static void
256 ellist_update(struct hfsc_class *cl)
257 {
258         struct list_head *head = &cl->sched->eligible;
259         struct hfsc_class *p, *last;
260
261         /*
262          * the eligible time of a class increases monotonically.
263          * if the next entry has a larger eligible time, nothing to do.
264          */
265         if (cl->ellist.next == head ||
266             ((p = list_entry(cl->ellist.next, struct hfsc_class, ellist)) &&
267              cl->cl_e <= p->cl_e))
268                 return;
269
270         /* check the last entry */
271         last = list_entry(head->prev, struct hfsc_class, ellist);
272         if (last->cl_e <= cl->cl_e) {
273                 list_move_tail(&cl->ellist, head);
274                 return;
275         }
276
277         /*
278          * the new position must be between the next entry
279          * and the last entry
280          */
281         list_for_each_entry_continue(p, head, ellist) {
282                 if (cl->cl_e < p->cl_e) {
283                         list_move_tail(&cl->ellist, &p->ellist);
284                         return;
285                 }
286         }
287         ASSERT(0); /* should not reach here */
288 }
289
290 /* find the class with the minimum deadline among the eligible classes */
291 static inline struct hfsc_class *
292 ellist_get_mindl(struct list_head *head, u64 cur_time)
293 {
294         struct hfsc_class *p, *cl = NULL;
295
296         list_for_each_entry(p, head, ellist) {
297                 if (p->cl_e > cur_time)
298                         break;
299                 if (cl == NULL || p->cl_d < cl->cl_d)
300                         cl = p;
301         }
302         return cl;
303 }
304
305 /* find the class with minimum eligible time among the eligible classes */
306 static inline struct hfsc_class *
307 ellist_get_minel(struct list_head *head)
308 {
309         if (list_empty(head))
310                 return NULL;
311         return list_entry(head->next, struct hfsc_class, ellist);
312 }
313
314 /*
315  * active children list holds backlogged child classes being sorted
316  * by their virtual time. each intermediate class has one active
317  * children list.
318  */
319 static void
320 actlist_insert(struct hfsc_class *cl)
321 {
322         struct list_head *head = &cl->cl_parent->actlist;
323         struct hfsc_class *p;
324
325         /* check the last entry first */
326         if (list_empty(head) ||
327             ((p = list_entry(head->prev, struct hfsc_class, alist)) &&
328              p->cl_vt <= cl->cl_vt)) {
329                 list_add_tail(&cl->alist, head);
330                 return;
331         }
332
333         list_for_each_entry(p, head, alist) {
334                 if (cl->cl_vt < p->cl_vt) {
335                         /* insert cl before p */
336                         list_add_tail(&cl->alist, &p->alist);
337                         return;
338                 }
339         }
340         ASSERT(0); /* should not reach here */
341 }
342
343 static inline void
344 actlist_remove(struct hfsc_class *cl)
345 {
346         list_del(&cl->alist);
347 }
348
349 static void
350 actlist_update(struct hfsc_class *cl)
351 {
352         struct list_head *head = &cl->cl_parent->actlist;
353         struct hfsc_class *p, *last;
354
355         /*
356          * the virtual time of a class increases monotonically.
357          * if the next entry has a larger virtual time, nothing to do.
358          */
359         if (cl->alist.next == head ||
360             ((p = list_entry(cl->alist.next, struct hfsc_class, alist)) &&
361              cl->cl_vt <= p->cl_vt))
362                 return;
363
364         /* check the last entry */
365         last = list_entry(head->prev, struct hfsc_class, alist);
366         if (last->cl_vt <= cl->cl_vt) {
367                 list_move_tail(&cl->alist, head);
368                 return;
369         }
370
371         /*
372          * the new position must be between the next entry
373          * and the last entry
374          */
375         list_for_each_entry_continue(p, head, alist) {
376                 if (cl->cl_vt < p->cl_vt) {
377                         list_move_tail(&cl->alist, &p->alist);
378                         return;
379                 }
380         }
381         ASSERT(0); /* should not reach here */
382 }
383
384 static inline struct hfsc_class *
385 actlist_firstfit(struct hfsc_class *cl, u64 cur_time)
386 {
387         struct hfsc_class *p;
388
389         list_for_each_entry(p, &cl->actlist, alist) {
390                 if (p->cl_f <= cur_time) {
391                         return p;
392                 }
393         }
394         return NULL;
395 }
396
397 /*
398  * get the leaf class with the minimum vt in the hierarchy
399  */
400 static struct hfsc_class *
401 actlist_get_minvt(struct hfsc_class *cl, u64 cur_time)
402 {
403         /* if root-class's cfmin is bigger than cur_time nothing to do */
404         if (cl->cl_cfmin > cur_time)
405                 return NULL;
406
407         while (cl->level > 0) {
408                 cl = actlist_firstfit(cl, cur_time);
409                 if (cl == NULL)
410                         return NULL;
411                 /*
412                  * update parent's cl_cvtmin.
413                  */
414                 if (cl->cl_parent->cl_cvtmin < cl->cl_vt)
415                         cl->cl_parent->cl_cvtmin = cl->cl_vt;
416         }
417         return cl;
418 }
419
420 /*
421  * service curve support functions
422  *
423  *  external service curve parameters
424  *      m: bps
425  *      d: us
426  *  internal service curve parameters
427  *      sm: (bytes/psched_us) << SM_SHIFT
428  *      ism: (psched_us/byte) << ISM_SHIFT
429  *      dx: psched_us
430  *
431  * Time source resolution
432  *  PSCHED_JIFFIES: for 48<=HZ<=1534 resolution is between 0.63us and 1.27us.
433  *  PSCHED_CPU: resolution is between 0.5us and 1us.
434  *  PSCHED_GETTIMEOFDAY: resolution is exactly 1us.
435  *
436  * sm and ism are scaled in order to keep effective digits.
437  * SM_SHIFT and ISM_SHIFT are selected to keep at least 4 effective
438  * digits in decimal using the following table.
439  *
440  * Note: We can afford the additional accuracy (altq hfsc keeps at most
441  * 3 effective digits) thanks to the fact that linux clock is bounded
442  * much more tightly.
443  *
444  *  bits/sec      100Kbps     1Mbps     10Mbps     100Mbps    1Gbps
445  *  ------------+-------------------------------------------------------
446  *  bytes/0.5us   6.25e-3    62.5e-3    625e-3     6250e-e    62500e-3
447  *  bytes/us      12.5e-3    125e-3     1250e-3    12500e-3   125000e-3
448  *  bytes/1.27us  15.875e-3  158.75e-3  1587.5e-3  15875e-3   158750e-3
449  *
450  *  0.5us/byte    160        16         1.6        0.16       0.016
451  *  us/byte       80         8          0.8        0.08       0.008
452  *  1.27us/byte   63         6.3        0.63       0.063      0.0063
453  */
454 #define SM_SHIFT        20
455 #define ISM_SHIFT       18
456
457 #define SM_MASK         ((1ULL << SM_SHIFT) - 1)
458 #define ISM_MASK        ((1ULL << ISM_SHIFT) - 1)
459
460 static inline u64
461 seg_x2y(u64 x, u64 sm)
462 {
463         u64 y;
464
465         /*
466          * compute
467          *      y = x * sm >> SM_SHIFT
468          * but divide it for the upper and lower bits to avoid overflow
469          */
470         y = (x >> SM_SHIFT) * sm + (((x & SM_MASK) * sm) >> SM_SHIFT);
471         return y;
472 }
473
474 static inline u64
475 seg_y2x(u64 y, u64 ism)
476 {
477         u64 x;
478
479         if (y == 0)
480                 x = 0;
481         else if (ism == HT_INFINITY)
482                 x = HT_INFINITY;
483         else {
484                 x = (y >> ISM_SHIFT) * ism
485                     + (((y & ISM_MASK) * ism) >> ISM_SHIFT);
486         }
487         return x;
488 }
489
490 /* Convert m (bps) into sm (bytes/psched us) */
491 static u64
492 m2sm(u32 m)
493 {
494         u64 sm;
495
496         sm = ((u64)m << SM_SHIFT);
497         sm += PSCHED_JIFFIE2US(HZ) - 1;
498         do_div(sm, PSCHED_JIFFIE2US(HZ));
499         return sm;
500 }
501
502 /* convert m (bps) into ism (psched us/byte) */
503 static u64
504 m2ism(u32 m)
505 {
506         u64 ism;
507
508         if (m == 0)
509                 ism = HT_INFINITY;
510         else {
511                 ism = ((u64)PSCHED_JIFFIE2US(HZ) << ISM_SHIFT);
512                 ism += m - 1;
513                 do_div(ism, m);
514         }
515         return ism;
516 }
517
518 /* convert d (us) into dx (psched us) */
519 static u64
520 d2dx(u32 d)
521 {
522         u64 dx;
523
524         dx = ((u64)d * PSCHED_JIFFIE2US(HZ));
525         dx += 1000000 - 1;
526         do_div(dx, 1000000);
527         return dx;
528 }
529
530 /* convert sm (bytes/psched us) into m (bps) */
531 static u32
532 sm2m(u64 sm)
533 {
534         u64 m;
535
536         m = (sm * PSCHED_JIFFIE2US(HZ)) >> SM_SHIFT;
537         return (u32)m;
538 }
539
540 /* convert dx (psched us) into d (us) */
541 static u32
542 dx2d(u64 dx)
543 {
544         u64 d;
545
546         d = dx * 1000000;
547         do_div(d, PSCHED_JIFFIE2US(HZ));
548         return (u32)d;
549 }
550
551 static void
552 sc2isc(struct tc_service_curve *sc, struct internal_sc *isc)
553 {
554         isc->sm1  = m2sm(sc->m1);
555         isc->ism1 = m2ism(sc->m1);
556         isc->dx   = d2dx(sc->d);
557         isc->dy   = seg_x2y(isc->dx, isc->sm1);
558         isc->sm2  = m2sm(sc->m2);
559         isc->ism2 = m2ism(sc->m2);
560 }
561
562 /*
563  * initialize the runtime service curve with the given internal
564  * service curve starting at (x, y).
565  */
566 static void
567 rtsc_init(struct runtime_sc *rtsc, struct internal_sc *isc, u64 x, u64 y)
568 {
569         rtsc->x    = x;
570         rtsc->y    = y;
571         rtsc->sm1  = isc->sm1;
572         rtsc->ism1 = isc->ism1;
573         rtsc->dx   = isc->dx;
574         rtsc->dy   = isc->dy;
575         rtsc->sm2  = isc->sm2;
576         rtsc->ism2 = isc->ism2;
577 }
578
579 /*
580  * calculate the y-projection of the runtime service curve by the
581  * given x-projection value
582  */
583 static u64
584 rtsc_y2x(struct runtime_sc *rtsc, u64 y)
585 {
586         u64 x;
587
588         if (y < rtsc->y)
589                 x = rtsc->x;
590         else if (y <= rtsc->y + rtsc->dy) {
591                 /* x belongs to the 1st segment */
592                 if (rtsc->dy == 0)
593                         x = rtsc->x + rtsc->dx;
594                 else
595                         x = rtsc->x + seg_y2x(y - rtsc->y, rtsc->ism1);
596         } else {
597                 /* x belongs to the 2nd segment */
598                 x = rtsc->x + rtsc->dx
599                     + seg_y2x(y - rtsc->y - rtsc->dy, rtsc->ism2);
600         }
601         return x;
602 }
603
604 static u64
605 rtsc_x2y(struct runtime_sc *rtsc, u64 x)
606 {
607         u64 y;
608
609         if (x <= rtsc->x)
610                 y = rtsc->y;
611         else if (x <= rtsc->x + rtsc->dx)
612                 /* y belongs to the 1st segment */
613                 y = rtsc->y + seg_x2y(x - rtsc->x, rtsc->sm1);
614         else
615                 /* y belongs to the 2nd segment */
616                 y = rtsc->y + rtsc->dy
617                     + seg_x2y(x - rtsc->x - rtsc->dx, rtsc->sm2);
618         return y;
619 }
620
621 /*
622  * update the runtime service curve by taking the minimum of the current
623  * runtime service curve and the service curve starting at (x, y).
624  */
625 static void
626 rtsc_min(struct runtime_sc *rtsc, struct internal_sc *isc, u64 x, u64 y)
627 {
628         u64 y1, y2, dx, dy;
629         u32 dsm;
630
631         if (isc->sm1 <= isc->sm2) {
632                 /* service curve is convex */
633                 y1 = rtsc_x2y(rtsc, x);
634                 if (y1 < y)
635                         /* the current rtsc is smaller */
636                         return;
637                 rtsc->x = x;
638                 rtsc->y = y;
639                 return;
640         }
641
642         /*
643          * service curve is concave
644          * compute the two y values of the current rtsc
645          *      y1: at x
646          *      y2: at (x + dx)
647          */
648         y1 = rtsc_x2y(rtsc, x);
649         if (y1 <= y) {
650                 /* rtsc is below isc, no change to rtsc */
651                 return;
652         }
653
654         y2 = rtsc_x2y(rtsc, x + isc->dx);
655         if (y2 >= y + isc->dy) {
656                 /* rtsc is above isc, replace rtsc by isc */
657                 rtsc->x = x;
658                 rtsc->y = y;
659                 rtsc->dx = isc->dx;
660                 rtsc->dy = isc->dy;
661                 return;
662         }
663
664         /*
665          * the two curves intersect
666          * compute the offsets (dx, dy) using the reverse
667          * function of seg_x2y()
668          *      seg_x2y(dx, sm1) == seg_x2y(dx, sm2) + (y1 - y)
669          */
670         dx = (y1 - y) << SM_SHIFT;
671         dsm = isc->sm1 - isc->sm2;
672         do_div(dx, dsm);
673         /*
674          * check if (x, y1) belongs to the 1st segment of rtsc.
675          * if so, add the offset.
676          */
677         if (rtsc->x + rtsc->dx > x)
678                 dx += rtsc->x + rtsc->dx - x;
679         dy = seg_x2y(dx, isc->sm1);
680
681         rtsc->x = x;
682         rtsc->y = y;
683         rtsc->dx = dx;
684         rtsc->dy = dy;
685         return;
686 }
687
688 static void
689 init_ed(struct hfsc_class *cl, unsigned int next_len)
690 {
691         u64 cur_time;
692
693         PSCHED_GET_TIME(cur_time);
694
695         /* update the deadline curve */
696         rtsc_min(&cl->cl_deadline, &cl->cl_rsc, cur_time, cl->cl_cumul);
697
698         /*
699          * update the eligible curve.
700          * for concave, it is equal to the deadline curve.
701          * for convex, it is a linear curve with slope m2.
702          */
703         cl->cl_eligible = cl->cl_deadline;
704         if (cl->cl_rsc.sm1 <= cl->cl_rsc.sm2) {
705                 cl->cl_eligible.dx = 0;
706                 cl->cl_eligible.dy = 0;
707         }
708
709         /* compute e and d */
710         cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
711         cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
712
713         ellist_insert(cl);
714 }
715
716 static void
717 update_ed(struct hfsc_class *cl, unsigned int next_len)
718 {
719         cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
720         cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
721
722         ellist_update(cl);
723 }
724
725 static inline void
726 update_d(struct hfsc_class *cl, unsigned int next_len)
727 {
728         cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
729 }
730
731 static void
732 update_cfmin(struct hfsc_class *cl)
733 {
734         struct hfsc_class *p;
735         u64 cfmin;
736
737         if (list_empty(&cl->actlist)) {
738                 cl->cl_cfmin = 0;
739                 return;
740         }
741         cfmin = HT_INFINITY;
742         list_for_each_entry(p, &cl->actlist, alist) {
743                 if (p->cl_f == 0) {
744                         cl->cl_cfmin = 0;
745                         return;
746                 }
747                 if (p->cl_f < cfmin)
748                         cfmin = p->cl_f;
749         }
750         cl->cl_cfmin = cfmin;
751 }
752
753 static void
754 init_vf(struct hfsc_class *cl, unsigned int len)
755 {
756         struct hfsc_class *max_cl, *p;
757         u64 vt, f, cur_time;
758         int go_active;
759
760         cur_time = 0;
761         go_active = 1;
762         for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
763                 if (go_active && cl->cl_nactive++ == 0)
764                         go_active = 1;
765                 else
766                         go_active = 0;
767
768                 if (go_active) {
769                         if (!list_empty(&cl->cl_parent->actlist)) {
770                                 max_cl = list_entry(cl->cl_parent->actlist.prev,
771                                                     struct hfsc_class, alist);
772                                 /*
773                                  * set vt to the average of the min and max
774                                  * classes.  if the parent's period didn't
775                                  * change, don't decrease vt of the class.
776                                  */
777                                 vt = max_cl->cl_vt;
778                                 if (cl->cl_parent->cl_cvtmin != 0)
779                                         vt = (cl->cl_parent->cl_cvtmin + vt)/2;
780
781                                 if (cl->cl_parent->cl_vtperiod !=
782                                     cl->cl_parentperiod || vt > cl->cl_vt)
783                                         cl->cl_vt = vt;
784                         } else {
785                                 /*
786                                  * first child for a new parent backlog period.
787                                  * add parent's cvtmax to vtoff of children
788                                  * to make a new vt (vtoff + vt) larger than
789                                  * the vt in the last period for all children.
790                                  */
791                                 vt = cl->cl_parent->cl_cvtmax;
792                                 list_for_each_entry(p, &cl->cl_parent->children,
793                                                                        siblings)
794                                         p->cl_vtoff += vt;
795                                 cl->cl_vt = 0;
796                                 cl->cl_parent->cl_cvtmax = 0;
797                                 cl->cl_parent->cl_cvtmin = 0;
798                         }
799
800                         /* update the virtual curve */
801                         vt = cl->cl_vt + cl->cl_vtoff;
802                         rtsc_min(&cl->cl_virtual, &cl->cl_fsc, vt,
803                                                       cl->cl_total);
804                         if (cl->cl_virtual.x == vt) {
805                                 cl->cl_virtual.x -= cl->cl_vtoff;
806                                 cl->cl_vtoff = 0;
807                         }
808                         cl->cl_vtadj = 0;
809
810                         cl->cl_vtperiod++;  /* increment vt period */
811                         cl->cl_parentperiod = cl->cl_parent->cl_vtperiod;
812                         if (cl->cl_parent->cl_nactive == 0)
813                                 cl->cl_parentperiod++;
814                         cl->cl_f = 0;
815
816                         actlist_insert(cl);
817
818                         if (cl->cl_flags & HFSC_USC) {
819                                 /* class has upper limit curve */
820                                 if (cur_time == 0)
821                                         PSCHED_GET_TIME(cur_time);
822
823                                 /* update the ulimit curve */
824                                 rtsc_min(&cl->cl_ulimit, &cl->cl_usc, cur_time,
825                                          cl->cl_total);
826                                 /* compute myf */
827                                 cl->cl_myf = rtsc_y2x(&cl->cl_ulimit,
828                                                       cl->cl_total);
829                                 cl->cl_myfadj = 0;
830                         }
831                 }
832
833                 f = max(cl->cl_myf, cl->cl_cfmin);
834                 if (f != cl->cl_f) {
835                         cl->cl_f = f;
836                         update_cfmin(cl->cl_parent);
837                 }
838         }
839 }
840
841 static void
842 update_vf(struct hfsc_class *cl, unsigned int len, u64 cur_time)
843 {
844         u64 f; /* , myf_bound, delta; */
845         int go_passive = 0;
846
847         if (cl->qdisc->q.qlen == 0 && cl->cl_flags & HFSC_FSC)
848                 go_passive = 1;
849
850         for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
851                 cl->cl_total += len;
852
853                 if (!(cl->cl_flags & HFSC_FSC) || cl->cl_nactive == 0)
854                         continue;
855
856                 if (go_passive && --cl->cl_nactive == 0)
857                         go_passive = 1;
858                 else
859                         go_passive = 0;
860
861                 if (go_passive) {
862                         /* no more active child, going passive */
863
864                         /* update cvtmax of the parent class */
865                         if (cl->cl_vt > cl->cl_parent->cl_cvtmax)
866                                 cl->cl_parent->cl_cvtmax = cl->cl_vt;
867
868                         /* remove this class from the vt list */
869                         actlist_remove(cl);
870
871                         update_cfmin(cl->cl_parent);
872
873                         continue;
874                 }
875
876                 /*
877                  * update vt and f
878                  */
879                 cl->cl_vt = rtsc_y2x(&cl->cl_virtual, cl->cl_total)
880                             - cl->cl_vtoff + cl->cl_vtadj;
881
882                 /*
883                  * if vt of the class is smaller than cvtmin,
884                  * the class was skipped in the past due to non-fit.
885                  * if so, we need to adjust vtadj.
886                  */
887                 if (cl->cl_vt < cl->cl_parent->cl_cvtmin) {
888                         cl->cl_vtadj += cl->cl_parent->cl_cvtmin - cl->cl_vt;
889                         cl->cl_vt = cl->cl_parent->cl_cvtmin;
890                 }
891
892                 /* update the vt list */
893                 actlist_update(cl);
894
895                 if (cl->cl_flags & HFSC_USC) {
896                         cl->cl_myf = cl->cl_myfadj + rtsc_y2x(&cl->cl_ulimit,
897                                                               cl->cl_total);
898 #if 0
899                         /*
900                          * This code causes classes to stay way under their
901                          * limit when multiple classes are used at gigabit
902                          * speed. needs investigation. -kaber
903                          */
904                         /*
905                          * if myf lags behind by more than one clock tick
906                          * from the current time, adjust myfadj to prevent
907                          * a rate-limited class from going greedy.
908                          * in a steady state under rate-limiting, myf
909                          * fluctuates within one clock tick.
910                          */
911                         myf_bound = cur_time - PSCHED_JIFFIE2US(1);
912                         if (cl->cl_myf < myf_bound) {
913                                 delta = cur_time - cl->cl_myf;
914                                 cl->cl_myfadj += delta;
915                                 cl->cl_myf += delta;
916                         }
917 #endif
918                 }
919
920                 f = max(cl->cl_myf, cl->cl_cfmin);
921                 if (f != cl->cl_f) {
922                         cl->cl_f = f;
923                         update_cfmin(cl->cl_parent);
924                 }
925         }
926 }
927
928 static void
929 set_active(struct hfsc_class *cl, unsigned int len)
930 {
931         if (cl->cl_flags & HFSC_RSC)
932                 init_ed(cl, len);
933         if (cl->cl_flags & HFSC_FSC)
934                 init_vf(cl, len);
935
936         list_add_tail(&cl->dlist, &cl->sched->droplist);
937 }
938
939 static void
940 set_passive(struct hfsc_class *cl)
941 {
942         if (cl->cl_flags & HFSC_RSC)
943                 ellist_remove(cl);
944
945         list_del(&cl->dlist);
946
947         /*
948          * actlist is now handled in update_vf() so that update_vf(cl, 0, 0)
949          * needs to be called explicitly to remove a class from actlist
950          */
951 }
952
953 /*
954  * hack to get length of first packet in queue.
955  */
956 static unsigned int
957 qdisc_peek_len(struct Qdisc *sch)
958 {
959         struct sk_buff *skb;
960         unsigned int len;
961
962         skb = sch->dequeue(sch);
963         if (skb == NULL) {
964                 if (net_ratelimit())
965                         printk("qdisc_peek_len: non work-conserving qdisc ?\n");
966                 return 0;
967         }
968         len = skb->len;
969         if (unlikely(sch->ops->requeue(skb, sch) != NET_XMIT_SUCCESS)) {
970                 if (net_ratelimit())
971                         printk("qdisc_peek_len: failed to requeue\n");
972                 return 0;
973         }
974         return len;
975 }
976
977 static void
978 hfsc_purge_queue(struct Qdisc *sch, struct hfsc_class *cl)
979 {
980         unsigned int len = cl->qdisc->q.qlen;
981
982         qdisc_reset(cl->qdisc);
983         if (len > 0) {
984                 update_vf(cl, 0, 0);
985                 set_passive(cl);
986                 sch->q.qlen -= len;
987         }
988 }
989
990 static void
991 hfsc_adjust_levels(struct hfsc_class *cl)
992 {
993         struct hfsc_class *p;
994         unsigned int level;
995
996         do {
997                 level = 0;
998                 list_for_each_entry(p, &cl->children, siblings) {
999                         if (p->level > level)
1000                                 level = p->level;
1001                 }
1002                 cl->level = level + 1;
1003         } while ((cl = cl->cl_parent) != NULL);
1004 }
1005
1006 static inline unsigned int
1007 hfsc_hash(u32 h)
1008 {
1009         h ^= h >> 8;
1010         h ^= h >> 4;
1011
1012         return h & (HFSC_HSIZE - 1);
1013 }
1014
1015 static inline struct hfsc_class *
1016 hfsc_find_class(u32 classid, struct Qdisc *sch)
1017 {
1018         struct hfsc_sched *q = (struct hfsc_sched *)sch->data;
1019         struct hfsc_class *cl;
1020
1021         list_for_each_entry(cl, &q->clhash[hfsc_hash(classid)], hlist) {
1022                 if (cl->classid == classid)
1023                         return cl;
1024         }
1025         return NULL;
1026 }
1027
1028 static void
1029 hfsc_change_rsc(struct hfsc_class *cl, struct tc_service_curve *rsc,
1030                 u64 cur_time)
1031 {
1032         sc2isc(rsc, &cl->cl_rsc);
1033         rtsc_init(&cl->cl_deadline, &cl->cl_rsc, cur_time, cl->cl_cumul);
1034         cl->cl_eligible = cl->cl_deadline;
1035         if (cl->cl_rsc.sm1 <= cl->cl_rsc.sm2) {
1036                 cl->cl_eligible.dx = 0;
1037                 cl->cl_eligible.dy = 0;
1038         }
1039         cl->cl_flags |= HFSC_RSC;
1040 }
1041
1042 static void
1043 hfsc_change_fsc(struct hfsc_class *cl, struct tc_service_curve *fsc)
1044 {
1045         sc2isc(fsc, &cl->cl_fsc);
1046         rtsc_init(&cl->cl_virtual, &cl->cl_fsc, cl->cl_vt, cl->cl_total);
1047         cl->cl_flags |= HFSC_FSC;
1048 }
1049
1050 static void
1051 hfsc_change_usc(struct hfsc_class *cl, struct tc_service_curve *usc,
1052                 u64 cur_time)
1053 {
1054         sc2isc(usc, &cl->cl_usc);
1055         rtsc_init(&cl->cl_ulimit, &cl->cl_usc, cur_time, cl->cl_total);
1056         cl->cl_flags |= HFSC_USC;
1057 }
1058
1059 static int
1060 hfsc_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
1061                   struct rtattr **tca, unsigned long *arg)
1062 {
1063         struct hfsc_sched *q = (struct hfsc_sched *)sch->data;
1064         struct hfsc_class *cl = (struct hfsc_class *)*arg;
1065         struct hfsc_class *parent = NULL;
1066         struct rtattr *opt = tca[TCA_OPTIONS-1];
1067         struct rtattr *tb[TCA_HFSC_MAX];
1068         struct tc_service_curve *rsc = NULL, *fsc = NULL, *usc = NULL;
1069         u64 cur_time;
1070
1071         if (opt == NULL ||
1072             rtattr_parse(tb, TCA_HFSC_MAX, RTA_DATA(opt), RTA_PAYLOAD(opt)))
1073                 return -EINVAL;
1074
1075         if (tb[TCA_HFSC_RSC-1]) {
1076                 if (RTA_PAYLOAD(tb[TCA_HFSC_RSC-1]) < sizeof(*rsc))
1077                         return -EINVAL;
1078                 rsc = RTA_DATA(tb[TCA_HFSC_RSC-1]);
1079                 if (rsc->m1 == 0 && rsc->m2 == 0)
1080                         rsc = NULL;
1081         }
1082
1083         if (tb[TCA_HFSC_FSC-1]) {
1084                 if (RTA_PAYLOAD(tb[TCA_HFSC_FSC-1]) < sizeof(*fsc))
1085                         return -EINVAL;
1086                 fsc = RTA_DATA(tb[TCA_HFSC_FSC-1]);
1087                 if (fsc->m1 == 0 && fsc->m2 == 0)
1088                         fsc = NULL;
1089         }
1090
1091         if (tb[TCA_HFSC_USC-1]) {
1092                 if (RTA_PAYLOAD(tb[TCA_HFSC_USC-1]) < sizeof(*usc))
1093                         return -EINVAL;
1094                 usc = RTA_DATA(tb[TCA_HFSC_USC-1]);
1095                 if (usc->m1 == 0 && usc->m2 == 0)
1096                         usc = NULL;
1097         }
1098
1099         if (cl != NULL) {
1100                 if (parentid) {
1101                         if (cl->cl_parent && cl->cl_parent->classid != parentid)
1102                                 return -EINVAL;
1103                         if (cl->cl_parent == NULL && parentid != TC_H_ROOT)
1104                                 return -EINVAL;
1105                 }
1106                 PSCHED_GET_TIME(cur_time);
1107
1108                 sch_tree_lock(sch);
1109                 if (rsc != NULL)
1110                         hfsc_change_rsc(cl, rsc, cur_time);
1111                 if (fsc != NULL)
1112                         hfsc_change_fsc(cl, fsc);
1113                 if (usc != NULL)
1114                         hfsc_change_usc(cl, usc, cur_time);
1115
1116                 if (cl->qdisc->q.qlen != 0) {
1117                         if (cl->cl_flags & HFSC_RSC)
1118                                 update_ed(cl, qdisc_peek_len(cl->qdisc));
1119                         if (cl->cl_flags & HFSC_FSC)
1120                                 update_vf(cl, 0, cur_time);
1121                 }
1122                 sch_tree_unlock(sch);
1123
1124 #ifdef CONFIG_NET_ESTIMATOR
1125                 if (tca[TCA_RATE-1]) {
1126                         qdisc_kill_estimator(&cl->stats);
1127                         qdisc_new_estimator(&cl->stats, tca[TCA_RATE-1]);
1128                 }
1129 #endif
1130                 return 0;
1131         }
1132
1133         if (parentid == TC_H_ROOT)
1134                 return -EEXIST;
1135
1136         parent = &q->root;
1137         if (parentid) {
1138                 parent = hfsc_find_class(parentid, sch);
1139                 if (parent == NULL)
1140                         return -ENOENT;
1141         }
1142
1143         if (classid == 0 || TC_H_MAJ(classid ^ sch->handle) != 0)
1144                 return -EINVAL;
1145         if (hfsc_find_class(classid, sch))
1146                 return -EEXIST;
1147
1148         if (rsc == NULL && fsc == NULL)
1149                 return -EINVAL;
1150
1151         cl = kmalloc(sizeof(struct hfsc_class), GFP_KERNEL);
1152         if (cl == NULL)
1153                 return -ENOBUFS;
1154         memset(cl, 0, sizeof(struct hfsc_class));
1155
1156         if (rsc != NULL)
1157                 hfsc_change_rsc(cl, rsc, 0);
1158         if (fsc != NULL)
1159                 hfsc_change_fsc(cl, fsc);
1160         if (usc != NULL)
1161                 hfsc_change_usc(cl, usc, 0);
1162
1163         cl->refcnt    = 1;
1164         cl->classid   = classid;
1165         cl->sched     = q;
1166         cl->cl_parent = parent;
1167         cl->qdisc = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
1168         if (cl->qdisc == NULL)
1169                 cl->qdisc = &noop_qdisc;
1170         cl->stats.lock = &sch->dev->queue_lock;
1171         INIT_LIST_HEAD(&cl->children);
1172         INIT_LIST_HEAD(&cl->actlist);
1173
1174         sch_tree_lock(sch);
1175         list_add_tail(&cl->hlist, &q->clhash[hfsc_hash(classid)]);
1176         list_add_tail(&cl->siblings, &parent->children);
1177         if (parent->level == 0)
1178                 hfsc_purge_queue(sch, parent);
1179         hfsc_adjust_levels(parent);
1180         sch_tree_unlock(sch);
1181
1182 #ifdef CONFIG_NET_ESTIMATOR
1183         if (tca[TCA_RATE-1])
1184                 qdisc_new_estimator(&cl->stats, tca[TCA_RATE-1]);
1185 #endif
1186         *arg = (unsigned long)cl;
1187         return 0;
1188 }
1189
1190 static void
1191 hfsc_destroy_filters(struct tcf_proto **fl)
1192 {
1193         struct tcf_proto *tp;
1194
1195         while ((tp = *fl) != NULL) {
1196                 *fl = tp->next;
1197                 tcf_destroy(tp);
1198         }
1199 }
1200
1201 static void
1202 hfsc_destroy_class(struct Qdisc *sch, struct hfsc_class *cl)
1203 {
1204         struct hfsc_sched *q = (struct hfsc_sched *)sch->data;
1205
1206         hfsc_destroy_filters(&cl->filter_list);
1207         qdisc_destroy(cl->qdisc);
1208 #ifdef CONFIG_NET_ESTIMATOR
1209         qdisc_kill_estimator(&cl->stats);
1210 #endif
1211         if (cl != &q->root)
1212                 kfree(cl);
1213 }
1214
1215 static int
1216 hfsc_delete_class(struct Qdisc *sch, unsigned long arg)
1217 {
1218         struct hfsc_sched *q = (struct hfsc_sched *)sch->data;
1219         struct hfsc_class *cl = (struct hfsc_class *)arg;
1220
1221         if (cl->level > 0 || cl->filter_cnt > 0 || cl == &q->root)
1222                 return -EBUSY;
1223
1224         sch_tree_lock(sch);
1225
1226         list_del(&cl->hlist);
1227         list_del(&cl->siblings);
1228         hfsc_adjust_levels(cl->cl_parent);
1229         hfsc_purge_queue(sch, cl);
1230         if (--cl->refcnt == 0)
1231                 hfsc_destroy_class(sch, cl);
1232
1233         sch_tree_unlock(sch);
1234         return 0;
1235 }
1236
1237 static struct hfsc_class *
1238 hfsc_classify(struct sk_buff *skb, struct Qdisc *sch)
1239 {
1240         struct hfsc_sched *q = (struct hfsc_sched *)sch->data;
1241         struct hfsc_class *cl;
1242         struct tcf_result res;
1243         struct tcf_proto *tcf;
1244         int result;
1245
1246         if (TC_H_MAJ(skb->priority ^ sch->handle) == 0 &&
1247             (cl = hfsc_find_class(skb->priority, sch)) != NULL)
1248                 if (cl->level == 0)
1249                         return cl;
1250
1251         tcf = q->root.filter_list;
1252         while (tcf && (result = tc_classify(skb, tcf, &res)) >= 0) {
1253 #ifdef CONFIG_NET_CLS_POLICE
1254                 if (result == TC_POLICE_SHOT)
1255                         return NULL;
1256 #endif
1257                 if ((cl = (struct hfsc_class *)res.class) == NULL) {
1258                         if ((cl = hfsc_find_class(res.classid, sch)) == NULL)
1259                                 break; /* filter selected invalid classid */
1260                 }
1261
1262                 if (cl->level == 0)
1263                         return cl; /* hit leaf class */
1264
1265                 /* apply inner filter chain */
1266                 tcf = cl->filter_list;
1267         }
1268
1269         /* classification failed, try default class */
1270         cl = hfsc_find_class(TC_H_MAKE(TC_H_MAJ(sch->handle), q->defcls), sch);
1271         if (cl == NULL || cl->level > 0)
1272                 return NULL;
1273
1274         return cl;
1275 }
1276
1277 static int
1278 hfsc_graft_class(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
1279                  struct Qdisc **old)
1280 {
1281         struct hfsc_class *cl = (struct hfsc_class *)arg;
1282
1283         if (cl == NULL)
1284                 return -ENOENT;
1285         if (cl->level > 0)
1286                 return -EINVAL;
1287         if (new == NULL) {
1288                 new = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
1289                 if (new == NULL)
1290                         new = &noop_qdisc;
1291         }
1292
1293         sch_tree_lock(sch);
1294         hfsc_purge_queue(sch, cl);
1295         *old = xchg(&cl->qdisc, new);
1296         sch_tree_unlock(sch);
1297         return 0;
1298 }
1299
1300 static struct Qdisc *
1301 hfsc_class_leaf(struct Qdisc *sch, unsigned long arg)
1302 {
1303         struct hfsc_class *cl = (struct hfsc_class *)arg;
1304
1305         if (cl != NULL && cl->level == 0)
1306                 return cl->qdisc;
1307
1308         return NULL;
1309 }
1310
1311 static unsigned long
1312 hfsc_get_class(struct Qdisc *sch, u32 classid)
1313 {
1314         struct hfsc_class *cl = hfsc_find_class(classid, sch);
1315
1316         if (cl != NULL)
1317                 cl->refcnt++;
1318
1319         return (unsigned long)cl;
1320 }
1321
1322 static void
1323 hfsc_put_class(struct Qdisc *sch, unsigned long arg)
1324 {
1325         struct hfsc_class *cl = (struct hfsc_class *)arg;
1326
1327         if (--cl->refcnt == 0)
1328                 hfsc_destroy_class(sch, cl);
1329 }
1330
1331 static unsigned long
1332 hfsc_bind_tcf(struct Qdisc *sch, unsigned long parent, u32 classid)
1333 {
1334         struct hfsc_class *p = (struct hfsc_class *)parent;
1335         struct hfsc_class *cl = hfsc_find_class(classid, sch);
1336
1337         if (cl != NULL) {
1338                 if (p != NULL && p->level <= cl->level)
1339                         return 0;
1340                 cl->filter_cnt++;
1341         }
1342
1343         return (unsigned long)cl;
1344 }
1345
1346 static void
1347 hfsc_unbind_tcf(struct Qdisc *sch, unsigned long arg)
1348 {
1349         struct hfsc_class *cl = (struct hfsc_class *)arg;
1350
1351         cl->filter_cnt--;
1352 }
1353
1354 static struct tcf_proto **
1355 hfsc_tcf_chain(struct Qdisc *sch, unsigned long arg)
1356 {
1357         struct hfsc_sched *q = (struct hfsc_sched *)sch->data;
1358         struct hfsc_class *cl = (struct hfsc_class *)arg;
1359
1360         if (cl == NULL)
1361                 cl = &q->root;
1362
1363         return &cl->filter_list;
1364 }
1365
1366 static int
1367 hfsc_dump_sc(struct sk_buff *skb, int attr, struct internal_sc *sc)
1368 {
1369         struct tc_service_curve tsc;
1370
1371         tsc.m1 = sm2m(sc->sm1);
1372         tsc.d  = dx2d(sc->dx);
1373         tsc.m2 = sm2m(sc->sm2);
1374         RTA_PUT(skb, attr, sizeof(tsc), &tsc);
1375
1376         return skb->len;
1377
1378  rtattr_failure:
1379         return -1;
1380 }
1381
1382 static inline int
1383 hfsc_dump_curves(struct sk_buff *skb, struct hfsc_class *cl)
1384 {
1385         if ((cl->cl_flags & HFSC_RSC) &&
1386             (hfsc_dump_sc(skb, TCA_HFSC_RSC, &cl->cl_rsc) < 0))
1387                 goto rtattr_failure;
1388
1389         if ((cl->cl_flags & HFSC_FSC) &&
1390             (hfsc_dump_sc(skb, TCA_HFSC_FSC, &cl->cl_fsc) < 0))
1391                 goto rtattr_failure;
1392
1393         if ((cl->cl_flags & HFSC_USC) &&
1394             (hfsc_dump_sc(skb, TCA_HFSC_USC, &cl->cl_usc) < 0))
1395                 goto rtattr_failure;
1396
1397         return skb->len;
1398
1399  rtattr_failure:
1400         return -1;
1401 }
1402
1403 static inline int
1404 hfsc_dump_stats(struct sk_buff *skb, struct hfsc_class *cl)
1405 {
1406         cl->stats.qlen = cl->qdisc->q.qlen;
1407         if (qdisc_copy_stats(skb, &cl->stats) < 0)
1408                 goto rtattr_failure;
1409
1410         return skb->len;
1411
1412  rtattr_failure:
1413         return -1;
1414 }
1415
1416 static inline int
1417 hfsc_dump_xstats(struct sk_buff *skb, struct hfsc_class *cl)
1418 {
1419         struct tc_hfsc_stats xstats;
1420
1421         xstats.level  = cl->level;
1422         xstats.period = cl->cl_vtperiod;
1423         xstats.work   = cl->cl_total;
1424         xstats.rtwork = cl->cl_cumul;
1425         RTA_PUT(skb, TCA_XSTATS, sizeof(xstats), &xstats);
1426
1427         return skb->len;
1428
1429  rtattr_failure:
1430         return -1;
1431 }
1432
1433 static int
1434 hfsc_dump_class(struct Qdisc *sch, unsigned long arg, struct sk_buff *skb,
1435                 struct tcmsg *tcm)
1436 {
1437         struct hfsc_class *cl = (struct hfsc_class *)arg;
1438         unsigned char *b = skb->tail;
1439         struct rtattr *rta = (struct rtattr *)b;
1440
1441         tcm->tcm_parent = cl->cl_parent ? cl->cl_parent->classid : TC_H_ROOT;
1442         tcm->tcm_handle = cl->classid;
1443         if (cl->level == 0)
1444                 tcm->tcm_info = cl->qdisc->handle;
1445
1446         RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
1447         if (hfsc_dump_curves(skb, cl) < 0)
1448                 goto rtattr_failure;
1449         rta->rta_len = skb->tail - b;
1450
1451         if ((hfsc_dump_stats(skb, cl) < 0) ||
1452             (hfsc_dump_xstats(skb, cl) < 0))
1453                 goto rtattr_failure;
1454
1455         return skb->len;
1456
1457  rtattr_failure:
1458         skb_trim(skb, b - skb->data);
1459         return -1;
1460 }
1461
1462 static void
1463 hfsc_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1464 {
1465         struct hfsc_sched *q = (struct hfsc_sched *)sch->data;
1466         struct hfsc_class *cl;
1467         unsigned int i;
1468
1469         if (arg->stop)
1470                 return;
1471
1472         for (i = 0; i < HFSC_HSIZE; i++) {
1473                 list_for_each_entry(cl, &q->clhash[i], hlist) {
1474                         if (arg->count < arg->skip) {
1475                                 arg->count++;
1476                                 continue;
1477                         }
1478                         if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
1479                                 arg->stop = 1;
1480                                 return;
1481                         }
1482                         arg->count++;
1483                 }
1484         }
1485 }
1486
1487 static void
1488 hfsc_watchdog(unsigned long arg)
1489 {
1490         struct Qdisc *sch = (struct Qdisc *)arg;
1491
1492         sch->flags &= ~TCQ_F_THROTTLED;
1493         netif_schedule(sch->dev);
1494 }
1495
1496 static void
1497 hfsc_schedule_watchdog(struct Qdisc *sch, u64 cur_time)
1498 {
1499         struct hfsc_sched *q = (struct hfsc_sched *)sch->data;
1500         struct hfsc_class *cl;
1501         u64 next_time = 0;
1502         long delay;
1503
1504         if ((cl = ellist_get_minel(&q->eligible)) != NULL)
1505                 next_time = cl->cl_e;
1506         if (q->root.cl_cfmin != 0) {
1507                 if (next_time == 0 || next_time > q->root.cl_cfmin)
1508                         next_time = q->root.cl_cfmin;
1509         }
1510         ASSERT(next_time != 0);
1511         delay = next_time - cur_time;
1512         delay = PSCHED_US2JIFFIE(delay);
1513
1514         sch->flags |= TCQ_F_THROTTLED;
1515         mod_timer(&q->wd_timer, jiffies + delay);
1516 }
1517
1518 static int
1519 hfsc_init_qdisc(struct Qdisc *sch, struct rtattr *opt)
1520 {
1521         struct hfsc_sched *q = (struct hfsc_sched *)sch->data;
1522         struct tc_hfsc_qopt *qopt;
1523         unsigned int i;
1524
1525         if (opt == NULL || RTA_PAYLOAD(opt) < sizeof(*qopt))
1526                 return -EINVAL;
1527         qopt = RTA_DATA(opt);
1528
1529         memset(q, 0, sizeof(struct hfsc_sched));
1530         sch->stats.lock = &sch->dev->queue_lock;
1531
1532         q->defcls = qopt->defcls;
1533         for (i = 0; i < HFSC_HSIZE; i++)
1534                 INIT_LIST_HEAD(&q->clhash[i]);
1535         INIT_LIST_HEAD(&q->eligible);
1536         INIT_LIST_HEAD(&q->droplist);
1537         skb_queue_head_init(&q->requeue);
1538
1539         q->root.refcnt  = 1;
1540         q->root.classid = sch->handle;
1541         q->root.sched   = q;
1542         q->root.qdisc = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
1543         if (q->root.qdisc == NULL)
1544                 q->root.qdisc = &noop_qdisc;
1545         q->root.stats.lock = &sch->dev->queue_lock;
1546         INIT_LIST_HEAD(&q->root.children);
1547         INIT_LIST_HEAD(&q->root.actlist);
1548
1549         list_add(&q->root.hlist, &q->clhash[hfsc_hash(q->root.classid)]);
1550
1551         init_timer(&q->wd_timer);
1552         q->wd_timer.function = hfsc_watchdog;
1553         q->wd_timer.data = (unsigned long)sch;
1554
1555         return 0;
1556 }
1557
1558 static int
1559 hfsc_change_qdisc(struct Qdisc *sch, struct rtattr *opt)
1560 {
1561         struct hfsc_sched *q = (struct hfsc_sched *)sch->data;
1562         struct tc_hfsc_qopt *qopt;
1563
1564         if (opt == NULL || RTA_PAYLOAD(opt) < sizeof(*qopt))
1565                 return -EINVAL;
1566         qopt = RTA_DATA(opt);
1567
1568         sch_tree_lock(sch);
1569         q->defcls = qopt->defcls;
1570         sch_tree_unlock(sch);
1571
1572         return 0;
1573 }
1574
1575 static void
1576 hfsc_reset_class(struct hfsc_class *cl)
1577 {
1578         cl->cl_total        = 0;
1579         cl->cl_cumul        = 0;
1580         cl->cl_d            = 0;
1581         cl->cl_e            = 0;
1582         cl->cl_vt           = 0;
1583         cl->cl_vtadj        = 0;
1584         cl->cl_vtoff        = 0;
1585         cl->cl_cvtmin       = 0;
1586         cl->cl_cvtmax       = 0;
1587         cl->cl_vtperiod     = 0;
1588         cl->cl_parentperiod = 0;
1589         cl->cl_f            = 0;
1590         cl->cl_myf          = 0;
1591         cl->cl_myfadj       = 0;
1592         cl->cl_cfmin        = 0;
1593         cl->cl_nactive      = 0;
1594         INIT_LIST_HEAD(&cl->actlist);
1595         qdisc_reset(cl->qdisc);
1596
1597         if (cl->cl_flags & HFSC_RSC)
1598                 rtsc_init(&cl->cl_deadline, &cl->cl_rsc, 0, 0);
1599         if (cl->cl_flags & HFSC_FSC)
1600                 rtsc_init(&cl->cl_virtual, &cl->cl_fsc, 0, 0);
1601         if (cl->cl_flags & HFSC_USC)
1602                 rtsc_init(&cl->cl_ulimit, &cl->cl_usc, 0, 0);
1603 }
1604
1605 static void
1606 hfsc_reset_qdisc(struct Qdisc *sch)
1607 {
1608         struct hfsc_sched *q = (struct hfsc_sched *)sch->data;
1609         struct hfsc_class *cl;
1610         unsigned int i;
1611
1612         for (i = 0; i < HFSC_HSIZE; i++) {
1613                 list_for_each_entry(cl, &q->clhash[i], hlist)
1614                         hfsc_reset_class(cl);
1615         }
1616         __skb_queue_purge(&q->requeue);
1617         INIT_LIST_HEAD(&q->eligible);
1618         INIT_LIST_HEAD(&q->droplist);
1619         del_timer(&q->wd_timer);
1620         sch->flags &= ~TCQ_F_THROTTLED;
1621         sch->q.qlen = 0;
1622 }
1623
1624 static void
1625 hfsc_destroy_qdisc(struct Qdisc *sch)
1626 {
1627         struct hfsc_sched *q = (struct hfsc_sched *)sch->data;
1628         struct hfsc_class *cl, *next;
1629         unsigned int i;
1630
1631         for (i = 0; i < HFSC_HSIZE; i++) {
1632                 list_for_each_entry_safe(cl, next, &q->clhash[i], hlist)
1633                         hfsc_destroy_class(sch, cl);
1634         }
1635         __skb_queue_purge(&q->requeue);
1636         del_timer(&q->wd_timer);
1637 }
1638
1639 static int
1640 hfsc_dump_qdisc(struct Qdisc *sch, struct sk_buff *skb)
1641 {
1642         struct hfsc_sched *q = (struct hfsc_sched *)sch->data;
1643         unsigned char *b = skb->tail;
1644         struct tc_hfsc_qopt qopt;
1645
1646         qopt.defcls = q->defcls;
1647         RTA_PUT(skb, TCA_OPTIONS, sizeof(qopt), &qopt);
1648
1649         sch->stats.qlen = sch->q.qlen;
1650         if (qdisc_copy_stats(skb, &sch->stats) < 0)
1651                 goto rtattr_failure;
1652
1653         return skb->len;
1654
1655  rtattr_failure:
1656         skb_trim(skb, b - skb->data);
1657         return -1;
1658 }
1659
1660 static int
1661 hfsc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
1662 {
1663         struct hfsc_class *cl = hfsc_classify(skb, sch);
1664         unsigned int len = skb->len;
1665         int err;
1666
1667         if (cl == NULL) {
1668                 kfree_skb(skb);
1669                 sch->stats.drops++;
1670                 return NET_XMIT_DROP;
1671         }
1672
1673         err = cl->qdisc->enqueue(skb, cl->qdisc);
1674         if (unlikely(err != NET_XMIT_SUCCESS)) {
1675                 cl->stats.drops++;
1676                 sch->stats.drops++;
1677                 return err;
1678         }
1679
1680         if (cl->qdisc->q.qlen == 1)
1681                 set_active(cl, len);
1682
1683         cl->stats.packets++;
1684         cl->stats.bytes += len;
1685         sch->stats.packets++;
1686         sch->stats.bytes += len;
1687         sch->q.qlen++;
1688
1689         return NET_XMIT_SUCCESS;
1690 }
1691
1692 static struct sk_buff *
1693 hfsc_dequeue(struct Qdisc *sch)
1694 {
1695         struct hfsc_sched *q = (struct hfsc_sched *)sch->data;
1696         struct hfsc_class *cl;
1697         struct sk_buff *skb;
1698         u64 cur_time;
1699         unsigned int next_len;
1700         int realtime = 0;
1701
1702         if (sch->q.qlen == 0)
1703                 return NULL;
1704         if ((skb = __skb_dequeue(&q->requeue)))
1705                 goto out;
1706
1707         PSCHED_GET_TIME(cur_time);
1708
1709         /*
1710          * if there are eligible classes, use real-time criteria.
1711          * find the class with the minimum deadline among
1712          * the eligible classes.
1713          */
1714         if ((cl = ellist_get_mindl(&q->eligible, cur_time)) != NULL) {
1715                 realtime = 1;
1716         } else {
1717                 /*
1718                  * use link-sharing criteria
1719                  * get the class with the minimum vt in the hierarchy
1720                  */
1721                 cl = actlist_get_minvt(&q->root, cur_time);
1722                 if (cl == NULL) {
1723                         sch->stats.overlimits++;
1724                         if (!netif_queue_stopped(sch->dev))
1725                                 hfsc_schedule_watchdog(sch, cur_time);
1726                         return NULL;
1727                 }
1728         }
1729
1730         skb = cl->qdisc->dequeue(cl->qdisc);
1731         if (skb == NULL) {
1732                 if (net_ratelimit())
1733                         printk("HFSC: Non-work-conserving qdisc ?\n");
1734                 return NULL;
1735         }
1736
1737         update_vf(cl, skb->len, cur_time);
1738         if (realtime)
1739                 cl->cl_cumul += skb->len;
1740
1741         if (cl->qdisc->q.qlen != 0) {
1742                 if (cl->cl_flags & HFSC_RSC) {
1743                         /* update ed */
1744                         next_len = qdisc_peek_len(cl->qdisc);
1745                         if (realtime)
1746                                 update_ed(cl, next_len);
1747                         else
1748                                 update_d(cl, next_len);
1749                 }
1750         } else {
1751                 /* the class becomes passive */
1752                 set_passive(cl);
1753         }
1754
1755  out:
1756         sch->flags &= ~TCQ_F_THROTTLED;
1757         sch->q.qlen--;
1758
1759         return skb;
1760 }
1761
1762 static int
1763 hfsc_requeue(struct sk_buff *skb, struct Qdisc *sch)
1764 {
1765         struct hfsc_sched *q = (struct hfsc_sched *)sch->data;
1766
1767         __skb_queue_head(&q->requeue, skb);
1768         sch->q.qlen++;
1769         return NET_XMIT_SUCCESS;
1770 }
1771
1772 static unsigned int
1773 hfsc_drop(struct Qdisc *sch)
1774 {
1775         struct hfsc_sched *q = (struct hfsc_sched *)sch->data;
1776         struct hfsc_class *cl;
1777         unsigned int len;
1778
1779         list_for_each_entry(cl, &q->droplist, dlist) {
1780                 if (cl->qdisc->ops->drop != NULL &&
1781                     (len = cl->qdisc->ops->drop(cl->qdisc)) > 0) {
1782                         if (cl->qdisc->q.qlen == 0) {
1783                                 update_vf(cl, 0, 0);
1784                                 set_passive(cl);
1785                         } else {
1786                                 list_move_tail(&cl->dlist, &q->droplist);
1787                         }
1788                         cl->stats.drops++;
1789                         sch->stats.drops++;
1790                         sch->q.qlen--;
1791                         return len;
1792                 }
1793         }
1794         return 0;
1795 }
1796
1797 static struct Qdisc_class_ops hfsc_class_ops = {
1798         .change         = hfsc_change_class,
1799         .delete         = hfsc_delete_class,
1800         .graft          = hfsc_graft_class,
1801         .leaf           = hfsc_class_leaf,
1802         .get            = hfsc_get_class,
1803         .put            = hfsc_put_class,
1804         .bind_tcf       = hfsc_bind_tcf,
1805         .unbind_tcf     = hfsc_unbind_tcf,
1806         .tcf_chain      = hfsc_tcf_chain,
1807         .dump           = hfsc_dump_class,
1808         .walk           = hfsc_walk
1809 };
1810
1811 static struct Qdisc_ops hfsc_qdisc_ops = {
1812         .id             = "hfsc",
1813         .init           = hfsc_init_qdisc,
1814         .change         = hfsc_change_qdisc,
1815         .reset          = hfsc_reset_qdisc,
1816         .destroy        = hfsc_destroy_qdisc,
1817         .dump           = hfsc_dump_qdisc,
1818         .enqueue        = hfsc_enqueue,
1819         .dequeue        = hfsc_dequeue,
1820         .requeue        = hfsc_requeue,
1821         .drop           = hfsc_drop,
1822         .cl_ops         = &hfsc_class_ops,
1823         .priv_size      = sizeof(struct hfsc_sched),
1824         .owner          = THIS_MODULE
1825 };
1826
1827 static int __init
1828 hfsc_init(void)
1829 {
1830         return register_qdisc(&hfsc_qdisc_ops);
1831 }
1832
1833 static void __exit
1834 hfsc_cleanup(void)
1835 {
1836         unregister_qdisc(&hfsc_qdisc_ops);
1837 }
1838
1839 MODULE_LICENSE("GPL");
1840 module_init(hfsc_init);
1841 module_exit(hfsc_cleanup);