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