Merge branch 'vendor/OPENPAM'
[dragonfly.git] / sys / net / altq / altq_hfsc.c
1 /*      $KAME: altq_hfsc.c,v 1.25 2004/04/17 10:54:48 kjc Exp $ */
2 /*      $DragonFly: src/sys/net/altq/altq_hfsc.c,v 1.9 2008/05/14 11:59:23 sephe Exp $ */
3
4 /*
5  * Copyright (c) 1997-1999 Carnegie Mellon University. All Rights Reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software and
8  * its documentation is hereby granted (including for commercial or
9  * for-profit use), provided that both the copyright notice and this
10  * permission notice appear in all copies of the software, derivative
11  * works, or modified versions, and any portions thereof.
12  *
13  * THIS SOFTWARE IS EXPERIMENTAL AND IS KNOWN TO HAVE BUGS, SOME OF
14  * WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON PROVIDES THIS
15  * SOFTWARE IN ITS ``AS IS'' CONDITION, AND ANY EXPRESS OR IMPLIED
16  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
25  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26  * DAMAGE.
27  *
28  * Carnegie Mellon encourages (but does not require) users of this
29  * software to return any improvements or extensions that they make,
30  * and to grant Carnegie Mellon the rights to redistribute these
31  * changes without encumbrance.
32  */
33 /*
34  * H-FSC is described in Proceedings of SIGCOMM'97,
35  * "A Hierarchical Fair Service Curve Algorithm for Link-Sharing,
36  * Real-Time and Priority Service"
37  * by Ion Stoica, Hui Zhang, and T. S. Eugene Ng.
38  *
39  * Oleg Cherevko <olwi@aq.ml.com.ua> added the upperlimit for link-sharing.
40  * when a class has an upperlimit, the fit-time is computed from the
41  * upperlimit service curve.  the link-sharing scheduler does not schedule
42  * a class whose fit-time exceeds the current time.
43  */
44
45 #include "opt_altq.h"
46 #include "opt_inet.h"
47 #include "opt_inet6.h"
48
49 #ifdef ALTQ_HFSC  /* hfsc is enabled by ALTQ_HFSC option in opt_altq.h */
50
51 #include <sys/param.h>
52 #include <sys/malloc.h>
53 #include <sys/mbuf.h>
54 #include <sys/socket.h>
55 #include <sys/systm.h>
56 #include <sys/errno.h>
57 #include <sys/queue.h>
58 #include <sys/thread.h>
59
60 #include <net/if.h>
61 #include <net/ifq_var.h>
62 #include <netinet/in.h>
63
64 #include <net/pf/pfvar.h>
65 #include <net/altq/altq.h>
66 #include <net/altq/altq_hfsc.h>
67
68 #include <sys/thread2.h>
69
70 /*
71  * function prototypes
72  */
73 static int      hfsc_clear_interface(struct hfsc_if *);
74 static int      hfsc_request(struct ifaltq *, int, void *);
75 static void     hfsc_purge(struct hfsc_if *);
76 static struct hfsc_class *hfsc_class_create(struct hfsc_if *,
77                                             struct service_curve *,
78                                             struct service_curve *,
79                                             struct service_curve *,
80                                             struct hfsc_class *, int, int, int);
81 static int      hfsc_class_destroy(struct hfsc_class *);
82 static struct hfsc_class *hfsc_nextclass(struct hfsc_class *);
83 static int      hfsc_enqueue(struct ifaltq *, struct mbuf *,
84                              struct altq_pktattr *);
85 static struct mbuf *hfsc_dequeue(struct ifaltq *, struct mbuf *, int);
86
87 static int      hfsc_addq(struct hfsc_class *, struct mbuf *);
88 static struct mbuf *hfsc_getq(struct hfsc_class *);
89 static struct mbuf *hfsc_pollq(struct hfsc_class *);
90 static void     hfsc_purgeq(struct hfsc_class *);
91
92 static void     update_cfmin(struct hfsc_class *);
93 static void     set_active(struct hfsc_class *, int);
94 static void     set_passive(struct hfsc_class *);
95
96 static void     init_ed(struct hfsc_class *, int);
97 static void     update_ed(struct hfsc_class *, int);
98 static void     update_d(struct hfsc_class *, int);
99 static void     init_vf(struct hfsc_class *, int);
100 static void     update_vf(struct hfsc_class *, int, uint64_t);
101 static ellist_t *ellist_alloc(void);
102 static void     ellist_destroy(ellist_t *);
103 static void     ellist_insert(struct hfsc_class *);
104 static void     ellist_remove(struct hfsc_class *);
105 static void     ellist_update(struct hfsc_class *);
106 struct hfsc_class *ellist_get_mindl(ellist_t *, uint64_t);
107 static actlist_t *actlist_alloc(void);
108 static void     actlist_destroy(actlist_t *);
109 static void     actlist_insert(struct hfsc_class *);
110 static void     actlist_remove(struct hfsc_class *);
111 static void     actlist_update(struct hfsc_class *);
112
113 static struct hfsc_class *actlist_firstfit(struct hfsc_class *, uint64_t);
114
115 static __inline uint64_t        seg_x2y(uint64_t, uint64_t);
116 static __inline uint64_t        seg_y2x(uint64_t, uint64_t);
117 static __inline uint64_t        m2sm(u_int);
118 static __inline uint64_t        m2ism(u_int);
119 static __inline uint64_t        d2dx(u_int);
120 static u_int                    sm2m(uint64_t);
121 static u_int                    dx2d(uint64_t);
122
123 static void     sc2isc(struct service_curve *, struct internal_sc *);
124 static void     rtsc_init(struct runtime_sc *, struct internal_sc *,
125                           uint64_t, uint64_t);
126 static uint64_t rtsc_y2x(struct runtime_sc *, uint64_t);
127 static uint64_t rtsc_x2y(struct runtime_sc *, uint64_t);
128 static void     rtsc_min(struct runtime_sc *, struct internal_sc *,
129                          uint64_t, uint64_t);
130
131 static void     get_class_stats(struct hfsc_classstats *, struct hfsc_class *);
132 static struct hfsc_class *clh_to_clp(struct hfsc_if *, uint32_t);
133
134 /*
135  * macros
136  */
137 #define is_a_parent_class(cl)   ((cl)->cl_children != NULL)
138
139 #define HT_INFINITY     0xffffffffffffffffLL    /* infinite time value */
140
141 int
142 hfsc_pfattach(struct pf_altq *a, struct ifaltq *ifq)
143 {
144         return altq_attach(ifq, ALTQT_HFSC, a->altq_disc,
145             hfsc_enqueue, hfsc_dequeue, hfsc_request, NULL, NULL);
146 }
147
148 int
149 hfsc_add_altq(struct pf_altq *a)
150 {
151         struct hfsc_if *hif;
152         struct ifnet *ifp;
153
154         if ((ifp = ifunit(a->ifname)) == NULL)
155                 return (EINVAL);
156         if (!ifq_is_ready(&ifp->if_snd))
157                 return (ENODEV);
158
159         hif = kmalloc(sizeof(struct hfsc_if), M_ALTQ, M_WAITOK | M_ZERO);
160
161         hif->hif_eligible = ellist_alloc();
162         hif->hif_ifq = &ifp->if_snd;
163         ifq_purge(&ifp->if_snd);
164
165         /* keep the state in pf_altq */
166         a->altq_disc = hif;
167
168         return (0);
169 }
170
171 int
172 hfsc_remove_altq(struct pf_altq *a)
173 {
174         struct hfsc_if *hif;
175
176         if ((hif = a->altq_disc) == NULL)
177                 return (EINVAL);
178         a->altq_disc = NULL;
179
180         hfsc_clear_interface(hif);
181         hfsc_class_destroy(hif->hif_rootclass);
182
183         ellist_destroy(hif->hif_eligible);
184
185         kfree(hif, M_ALTQ);
186
187         return (0);
188 }
189
190 static int
191 hfsc_add_queue_locked(struct pf_altq *a, struct hfsc_if *hif)
192 {
193         struct hfsc_class *cl, *parent;
194         struct hfsc_opts *opts;
195         struct service_curve rtsc, lssc, ulsc;
196
197         KKASSERT(a->qid != 0);
198
199         opts = &a->pq_u.hfsc_opts;
200
201         if (a->parent_qid == HFSC_NULLCLASS_HANDLE && hif->hif_rootclass == NULL)
202                 parent = NULL;
203         else if ((parent = clh_to_clp(hif, a->parent_qid)) == NULL)
204                 return (EINVAL);
205
206         if (clh_to_clp(hif, a->qid) != NULL)
207                 return (EBUSY);
208
209         rtsc.m1 = opts->rtsc_m1;
210         rtsc.d  = opts->rtsc_d;
211         rtsc.m2 = opts->rtsc_m2;
212         lssc.m1 = opts->lssc_m1;
213         lssc.d  = opts->lssc_d;
214         lssc.m2 = opts->lssc_m2;
215         ulsc.m1 = opts->ulsc_m1;
216         ulsc.d  = opts->ulsc_d;
217         ulsc.m2 = opts->ulsc_m2;
218
219         cl = hfsc_class_create(hif, &rtsc, &lssc, &ulsc, parent, a->qlimit,
220                                opts->flags, a->qid);
221         if (cl == NULL)
222                 return (ENOMEM);
223
224         return (0);
225 }
226
227 int
228 hfsc_add_queue(struct pf_altq *a)
229 {
230         struct hfsc_if *hif;
231         struct ifaltq *ifq;
232         int error;
233
234         if (a->qid == 0)
235                 return (EINVAL);
236
237         /* XXX not MP safe */
238         if ((hif = a->altq_disc) == NULL)
239                 return (EINVAL);
240         ifq = hif->hif_ifq;
241
242         ALTQ_LOCK(ifq);
243         error = hfsc_add_queue_locked(a, hif);
244         ALTQ_UNLOCK(ifq);
245
246         return error;
247 }
248
249 static int
250 hfsc_remove_queue_locked(struct pf_altq *a, struct hfsc_if *hif)
251 {
252         struct hfsc_class *cl;
253
254         if ((cl = clh_to_clp(hif, a->qid)) == NULL)
255                 return (EINVAL);
256
257         return (hfsc_class_destroy(cl));
258 }
259
260 int
261 hfsc_remove_queue(struct pf_altq *a)
262 {
263         struct hfsc_if *hif;
264         struct ifaltq *ifq;
265         int error;
266
267         /* XXX not MP safe */
268         if ((hif = a->altq_disc) == NULL)
269                 return (EINVAL);
270         ifq = hif->hif_ifq;
271
272         ALTQ_LOCK(ifq);
273         error = hfsc_remove_queue_locked(a, hif);
274         ALTQ_UNLOCK(ifq);
275
276         return error;
277 }
278
279 int
280 hfsc_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
281 {
282         struct hfsc_if *hif;
283         struct hfsc_class *cl;
284         struct hfsc_classstats stats;
285         struct ifaltq *ifq;
286         int error = 0;
287
288         if (*nbytes < sizeof(stats))
289                 return (EINVAL);
290
291         /* XXX not MP safe */
292         if ((hif = altq_lookup(a->ifname, ALTQT_HFSC)) == NULL)
293                 return (EBADF);
294         ifq = hif->hif_ifq;
295
296         ALTQ_LOCK(ifq);
297
298         if ((cl = clh_to_clp(hif, a->qid)) == NULL) {
299                 ALTQ_UNLOCK(ifq);
300                 return (EINVAL);
301         }
302
303         get_class_stats(&stats, cl);
304
305         ALTQ_UNLOCK(ifq);
306
307         if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
308                 return (error);
309         *nbytes = sizeof(stats);
310         return (0);
311 }
312
313 /*
314  * bring the interface back to the initial state by discarding
315  * all the filters and classes except the root class.
316  */
317 static int
318 hfsc_clear_interface(struct hfsc_if *hif)
319 {
320         struct hfsc_class *cl;
321
322         if (hif->hif_rootclass == NULL)
323                 return (0);
324
325
326         /* clear out the classes */
327         while ((cl = hif->hif_rootclass->cl_children) != NULL) {
328                 /*
329                  * remove the first leaf class found in the hierarchy
330                  * then start over
331                  */
332                 for (; cl != NULL; cl = hfsc_nextclass(cl)) {
333                         if (!is_a_parent_class(cl)) {
334                                 hfsc_class_destroy(cl);
335                                 break;
336                         }
337                 }
338         }
339
340         return (0);
341 }
342
343 static int
344 hfsc_request(struct ifaltq *ifq, int req, void *arg)
345 {
346         struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
347
348         crit_enter();
349         switch (req) {
350         case ALTRQ_PURGE:
351                 hfsc_purge(hif);
352                 break;
353         }
354         crit_exit();
355         return (0);
356 }
357
358 /* discard all the queued packets on the interface */
359 static void
360 hfsc_purge(struct hfsc_if *hif)
361 {
362         struct hfsc_class *cl;
363
364         for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl)) {
365                 if (!qempty(cl->cl_q))
366                         hfsc_purgeq(cl);
367         }
368         if (ifq_is_enabled(hif->hif_ifq))
369                 hif->hif_ifq->ifq_len = 0;
370 }
371
372 struct hfsc_class *
373 hfsc_class_create(struct hfsc_if *hif, struct service_curve *rsc,
374                   struct service_curve *fsc, struct service_curve *usc,
375                   struct hfsc_class *parent, int qlimit, int flags, int qid)
376 {
377         struct hfsc_class *cl, *p;
378         int i;
379
380         if (hif->hif_classes >= HFSC_MAX_CLASSES)
381                 return (NULL);
382
383 #ifndef ALTQ_RED
384         if (flags & HFCF_RED) {
385 #ifdef ALTQ_DEBUG
386                 kprintf("hfsc_class_create: RED not configured for HFSC!\n");
387 #endif
388                 return (NULL);
389         }
390 #endif
391
392         cl = kmalloc(sizeof(*cl), M_ALTQ, M_WAITOK | M_ZERO);
393         cl->cl_q = kmalloc(sizeof(*cl->cl_q), M_ALTQ, M_WAITOK | M_ZERO);
394         cl->cl_actc = actlist_alloc();
395
396         if (qlimit == 0)
397                 qlimit = 50;  /* use default */
398         qlimit(cl->cl_q) = qlimit;
399         qtype(cl->cl_q) = Q_DROPTAIL;
400         qlen(cl->cl_q) = 0;
401         cl->cl_flags = flags;
402 #ifdef ALTQ_RED
403         if (flags & (HFCF_RED|HFCF_RIO)) {
404                 int red_flags, red_pkttime;
405                 u_int m2;
406
407                 m2 = 0;
408                 if (rsc != NULL && rsc->m2 > m2)
409                         m2 = rsc->m2;
410                 if (fsc != NULL && fsc->m2 > m2)
411                         m2 = fsc->m2;
412                 if (usc != NULL && usc->m2 > m2)
413                         m2 = usc->m2;
414
415                 red_flags = 0;
416                 if (flags & HFCF_ECN)
417                         red_flags |= REDF_ECN;
418 #ifdef ALTQ_RIO
419                 if (flags & HFCF_CLEARDSCP)
420                         red_flags |= RIOF_CLEARDSCP;
421 #endif
422                 if (m2 < 8)
423                         red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
424                 else
425                         red_pkttime = (int64_t)hif->hif_ifq->altq_ifp->if_mtu
426                                 * 1000 * 1000 * 1000 / (m2 / 8);
427                 if (flags & HFCF_RED) {
428                         cl->cl_red = red_alloc(0, 0,
429                             qlimit(cl->cl_q) * 10/100,
430                             qlimit(cl->cl_q) * 30/100,
431                             red_flags, red_pkttime);
432                         if (cl->cl_red != NULL)
433                                 qtype(cl->cl_q) = Q_RED;
434                 }
435 #ifdef ALTQ_RIO
436                 else {
437                         cl->cl_red = (red_t *)rio_alloc(0, NULL,
438                             red_flags, red_pkttime);
439                         if (cl->cl_red != NULL)
440                                 qtype(cl->cl_q) = Q_RIO;
441                 }
442 #endif
443         }
444 #endif /* ALTQ_RED */
445
446         if (rsc != NULL && (rsc->m1 != 0 || rsc->m2 != 0)) {
447                 cl->cl_rsc = kmalloc(sizeof(*cl->cl_rsc), M_ALTQ, M_WAITOK);
448                 sc2isc(rsc, cl->cl_rsc);
449                 rtsc_init(&cl->cl_deadline, cl->cl_rsc, 0, 0);
450                 rtsc_init(&cl->cl_eligible, cl->cl_rsc, 0, 0);
451         }
452         if (fsc != NULL && (fsc->m1 != 0 || fsc->m2 != 0)) {
453                 cl->cl_fsc = kmalloc(sizeof(*cl->cl_fsc), M_ALTQ, M_WAITOK);
454                 if (cl->cl_fsc == NULL)
455                         goto err_ret;
456                 sc2isc(fsc, cl->cl_fsc);
457                 rtsc_init(&cl->cl_virtual, cl->cl_fsc, 0, 0);
458         }
459         if (usc != NULL && (usc->m1 != 0 || usc->m2 != 0)) {
460                 cl->cl_usc = kmalloc(sizeof(*cl->cl_usc), M_ALTQ, M_WAITOK);
461                 if (cl->cl_usc == NULL)
462                         goto err_ret;
463                 sc2isc(usc, cl->cl_usc);
464                 rtsc_init(&cl->cl_ulimit, cl->cl_usc, 0, 0);
465         }
466
467         cl->cl_id = hif->hif_classid++;
468         cl->cl_handle = qid;
469         cl->cl_hif = hif;
470         cl->cl_parent = parent;
471
472         crit_enter();
473         hif->hif_classes++;
474
475         /*
476          * find a free slot in the class table.  if the slot matching
477          * the lower bits of qid is free, use this slot.  otherwise,
478          * use the first free slot.
479          */
480         i = qid % HFSC_MAX_CLASSES;
481         if (hif->hif_class_tbl[i] == NULL)
482                 hif->hif_class_tbl[i] = cl;
483         else {
484                 for (i = 0; i < HFSC_MAX_CLASSES; i++) {
485                         if (hif->hif_class_tbl[i] == NULL) {
486                                 hif->hif_class_tbl[i] = cl;
487                                 break;
488                         }
489                 }
490                 if (i == HFSC_MAX_CLASSES) {
491                         crit_exit();
492                         goto err_ret;
493                 }
494         }
495
496         if (flags & HFCF_DEFAULTCLASS)
497                 hif->hif_defaultclass = cl;
498
499         if (parent == NULL) {
500                 /* this is root class */
501                 hif->hif_rootclass = cl;
502         } else if (parent->cl_children == NULL) {
503                 /* add this class to the children list of the parent */
504                 parent->cl_children = cl;
505         } else {
506                 p = parent->cl_children;
507                 while (p->cl_siblings != NULL)
508                         p = p->cl_siblings;
509                 p->cl_siblings = cl;
510         }
511         crit_exit();
512
513         return (cl);
514
515  err_ret:
516         if (cl->cl_actc != NULL)
517                 actlist_destroy(cl->cl_actc);
518         if (cl->cl_red != NULL) {
519 #ifdef ALTQ_RIO
520                 if (q_is_rio(cl->cl_q))
521                         rio_destroy((rio_t *)cl->cl_red);
522 #endif
523 #ifdef ALTQ_RED
524                 if (q_is_red(cl->cl_q))
525                         red_destroy(cl->cl_red);
526 #endif
527         }
528         if (cl->cl_fsc != NULL)
529                 kfree(cl->cl_fsc, M_ALTQ);
530         if (cl->cl_rsc != NULL)
531                 kfree(cl->cl_rsc, M_ALTQ);
532         if (cl->cl_usc != NULL)
533                 kfree(cl->cl_usc, M_ALTQ);
534         if (cl->cl_q != NULL)
535                 kfree(cl->cl_q, M_ALTQ);
536         kfree(cl, M_ALTQ);
537         return (NULL);
538 }
539
540 static int
541 hfsc_class_destroy(struct hfsc_class *cl)
542 {
543         struct hfsc_if *hif = cl->cl_hif;
544         int i;
545
546         if (cl == NULL)
547                 return (0);
548
549         if (is_a_parent_class(cl))
550                 return (EBUSY);
551
552         crit_enter();
553
554         if (!qempty(cl->cl_q))
555                 hfsc_purgeq(cl);
556
557         if (cl->cl_parent == NULL) {
558                 /* this is root class */
559         } else {
560                 struct hfsc_class *p = cl->cl_parent->cl_children;
561
562                 if (p == cl) {
563                         cl->cl_parent->cl_children = cl->cl_siblings;
564                 } else {
565                         do {
566                                 if (p->cl_siblings == cl) {
567                                         p->cl_siblings = cl->cl_siblings;
568                                         break;
569                                 }
570                         } while ((p = p->cl_siblings) != NULL);
571                 }
572                 KKASSERT(p != NULL);
573         }
574
575         for (i = 0; i < HFSC_MAX_CLASSES; i++) {
576                 if (hif->hif_class_tbl[i] == cl) {
577                         hif->hif_class_tbl[i] = NULL;
578                         break;
579                 }
580         }
581
582         hif->hif_classes--;
583         crit_exit();
584
585         actlist_destroy(cl->cl_actc);
586
587         if (cl->cl_red != NULL) {
588 #ifdef ALTQ_RIO
589                 if (q_is_rio(cl->cl_q))
590                         rio_destroy((rio_t *)cl->cl_red);
591 #endif
592 #ifdef ALTQ_RED
593                 if (q_is_red(cl->cl_q))
594                         red_destroy(cl->cl_red);
595 #endif
596         }
597
598         if (cl == hif->hif_rootclass)
599                 hif->hif_rootclass = NULL;
600         if (cl == hif->hif_defaultclass)
601                 hif->hif_defaultclass = NULL;
602         if (cl == hif->hif_pollcache)
603                 hif->hif_pollcache = NULL;
604
605         if (cl->cl_usc != NULL)
606                 kfree(cl->cl_usc, M_ALTQ);
607         if (cl->cl_fsc != NULL)
608                 kfree(cl->cl_fsc, M_ALTQ);
609         if (cl->cl_rsc != NULL)
610                 kfree(cl->cl_rsc, M_ALTQ);
611         kfree(cl->cl_q, M_ALTQ);
612         kfree(cl, M_ALTQ);
613
614         return (0);
615 }
616
617 /*
618  * hfsc_nextclass returns the next class in the tree.
619  *   usage:
620  *      for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl))
621  *              do_something;
622  */
623 static struct hfsc_class *
624 hfsc_nextclass(struct hfsc_class *cl)
625 {
626         if (cl->cl_children != NULL) {
627                 cl = cl->cl_children;
628         } else if (cl->cl_siblings != NULL) {
629                 cl = cl->cl_siblings;
630         } else {
631                 while ((cl = cl->cl_parent) != NULL) {
632                         if (cl->cl_siblings != NULL) {
633                                 cl = cl->cl_siblings;
634                                 break;
635                         }
636                 }
637         }
638
639         return (cl);
640 }
641
642 /*
643  * hfsc_enqueue is an enqueue function to be registered to
644  * (*altq_enqueue) in struct ifaltq.
645  */
646 static int
647 hfsc_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
648 {
649         struct hfsc_if  *hif = (struct hfsc_if *)ifq->altq_disc;
650         struct hfsc_class *cl;
651         int len;
652
653         /* grab class set by classifier */
654         if ((m->m_flags & M_PKTHDR) == 0) {
655                 /* should not happen */
656                 if_printf(ifq->altq_ifp, "altq: packet does not have pkthdr\n");
657                 m_freem(m);
658                 return (ENOBUFS);
659         }
660         crit_enter();
661         if (m->m_pkthdr.fw_flags & PF_MBUF_STRUCTURE)
662                 cl = clh_to_clp(hif, m->m_pkthdr.pf.qid);
663         else
664                 cl = NULL;
665         if (cl == NULL || is_a_parent_class(cl)) {
666                 cl = hif->hif_defaultclass;
667                 if (cl == NULL) {
668                         m_freem(m);
669                         crit_exit();
670                         return (ENOBUFS);
671                 }
672         }
673         cl->cl_pktattr = NULL;
674         len = m_pktlen(m);
675         if (hfsc_addq(cl, m) != 0) {
676                 /* drop occurred.  mbuf was freed in hfsc_addq. */
677                 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, len);
678                 crit_exit();
679                 return (ENOBUFS);
680         }
681         ifq->ifq_len++;
682         cl->cl_hif->hif_packets++;
683
684         /* successfully queued. */
685         if (qlen(cl->cl_q) == 1)
686                 set_active(cl, m_pktlen(m));
687         crit_exit();
688         return (0);
689 }
690
691 /*
692  * hfsc_dequeue is a dequeue function to be registered to
693  * (*altq_dequeue) in struct ifaltq.
694  *
695  * note: ALTDQ_POLL returns the next packet without removing the packet
696  *      from the queue.  ALTDQ_REMOVE is a normal dequeue operation.
697  *      ALTDQ_REMOVE must return the same packet if called immediately
698  *      after ALTDQ_POLL.
699  */
700 static struct mbuf *
701 hfsc_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op)
702 {
703         struct hfsc_if  *hif = (struct hfsc_if *)ifq->altq_disc;
704         struct hfsc_class *cl;
705         struct mbuf *m;
706         int len, next_len;
707         int realtime = 0;
708         uint64_t cur_time;
709
710         if (hif->hif_packets == 0) {
711                 /* no packet in the tree */
712                 return (NULL);
713         }
714
715         crit_enter();
716         cur_time = read_machclk();
717
718         if (op == ALTDQ_REMOVE && hif->hif_pollcache != NULL) {
719                 cl = hif->hif_pollcache;
720                 hif->hif_pollcache = NULL;
721                 /* check if the class was scheduled by real-time criteria */
722                 if (cl->cl_rsc != NULL)
723                         realtime = (cl->cl_e <= cur_time);
724         } else {
725                 /*
726                  * if there are eligible classes, use real-time criteria.
727                  * find the class with the minimum deadline among
728                  * the eligible classes.
729                  */
730                 if ((cl = ellist_get_mindl(hif->hif_eligible, cur_time)) != NULL) {
731                         realtime = 1;
732                 } else {
733 #ifdef ALTQ_DEBUG
734                         int fits = 0;
735 #endif
736                         /*
737                          * use link-sharing criteria
738                          * get the class with the minimum vt in the hierarchy
739                          */
740                         cl = hif->hif_rootclass;
741                         while (is_a_parent_class(cl)) {
742
743                                 cl = actlist_firstfit(cl, cur_time);
744                                 if (cl == NULL) {
745 #ifdef ALTQ_DEBUG
746                                         if (fits > 0)
747                                                 kprintf("%d fit but none found\n",fits);
748 #endif
749                                         m = NULL;
750                                         goto done;
751                                 }
752                                 /*
753                                  * update parent's cl_cvtmin.
754                                  * don't update if the new vt is smaller.
755                                  */
756                                 if (cl->cl_parent->cl_cvtmin < cl->cl_vt)
757                                         cl->cl_parent->cl_cvtmin = cl->cl_vt;
758 #ifdef ALTQ_DEBUG
759                                 fits++;
760 #endif
761                         }
762                 }
763
764                 if (op == ALTDQ_POLL) {
765                         hif->hif_pollcache = cl;
766                         m = hfsc_pollq(cl);
767                         goto done;
768                 }
769         }
770
771         m = hfsc_getq(cl);
772         if (m == NULL)
773                 panic("hfsc_dequeue:");
774         len = m_pktlen(m);
775         cl->cl_hif->hif_packets--;
776         ifq->ifq_len--;
777         PKTCNTR_ADD(&cl->cl_stats.xmit_cnt, len);
778
779         update_vf(cl, len, cur_time);
780         if (realtime)
781                 cl->cl_cumul += len;
782
783         if (!qempty(cl->cl_q)) {
784                 if (cl->cl_rsc != NULL) {
785                         /* update ed */
786                         next_len = m_pktlen(qhead(cl->cl_q));
787
788                         if (realtime)
789                                 update_ed(cl, next_len);
790                         else
791                                 update_d(cl, next_len);
792                 }
793         } else {
794                 /* the class becomes passive */
795                 set_passive(cl);
796         }
797 done:
798         crit_exit();
799         KKASSERT(mpolled == NULL || m == mpolled);
800         return (m);
801 }
802
803 static int
804 hfsc_addq(struct hfsc_class *cl, struct mbuf *m)
805 {
806
807 #ifdef ALTQ_RIO
808         if (q_is_rio(cl->cl_q))
809                 return rio_addq((rio_t *)cl->cl_red, cl->cl_q,
810                                 m, cl->cl_pktattr);
811 #endif
812 #ifdef ALTQ_RED
813         if (q_is_red(cl->cl_q))
814                 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
815 #endif
816         if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
817                 m_freem(m);
818                 return (-1);
819         }
820
821         if (cl->cl_flags & HFCF_CLEARDSCP)
822                 write_dsfield(m, cl->cl_pktattr, 0);
823
824         _addq(cl->cl_q, m);
825
826         return (0);
827 }
828
829 static struct mbuf *
830 hfsc_getq(struct hfsc_class *cl)
831 {
832 #ifdef ALTQ_RIO
833         if (q_is_rio(cl->cl_q))
834                 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
835 #endif
836 #ifdef ALTQ_RED
837         if (q_is_red(cl->cl_q))
838                 return red_getq(cl->cl_red, cl->cl_q);
839 #endif
840         return _getq(cl->cl_q);
841 }
842
843 static struct mbuf *
844 hfsc_pollq(struct hfsc_class *cl)
845 {
846         return qhead(cl->cl_q);
847 }
848
849 static void
850 hfsc_purgeq(struct hfsc_class *cl)
851 {
852         struct mbuf *m;
853
854         if (qempty(cl->cl_q))
855                 return;
856
857         while ((m = _getq(cl->cl_q)) != NULL) {
858                 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, m_pktlen(m));
859                 m_freem(m);
860                 cl->cl_hif->hif_packets--;
861                 cl->cl_hif->hif_ifq->ifq_len--;
862         }
863         KKASSERT(qlen(cl->cl_q) == 0);
864
865         update_vf(cl, 0, 0);    /* remove cl from the actlist */
866         set_passive(cl);
867 }
868
869 static void
870 set_active(struct hfsc_class *cl, int len)
871 {
872         if (cl->cl_rsc != NULL)
873                 init_ed(cl, len);
874         if (cl->cl_fsc != NULL)
875                 init_vf(cl, len);
876
877         cl->cl_stats.period++;
878 }
879
880 static void
881 set_passive(struct hfsc_class *cl)
882 {
883         if (cl->cl_rsc != NULL)
884                 ellist_remove(cl);
885
886         /*
887          * actlist is now handled in update_vf() so that update_vf(cl, 0, 0)
888          * needs to be called explicitly to remove a class from actlist
889          */
890 }
891
892 static void
893 init_ed(struct hfsc_class *cl, int next_len)
894 {
895         uint64_t cur_time;
896
897         cur_time = read_machclk();
898
899         /* update the deadline curve */
900         rtsc_min(&cl->cl_deadline, cl->cl_rsc, cur_time, cl->cl_cumul);
901
902         /*
903          * update the eligible curve.
904          * for concave, it is equal to the deadline curve.
905          * for convex, it is a linear curve with slope m2.
906          */
907         cl->cl_eligible = cl->cl_deadline;
908         if (cl->cl_rsc->sm1 <= cl->cl_rsc->sm2) {
909                 cl->cl_eligible.dx = 0;
910                 cl->cl_eligible.dy = 0;
911         }
912
913         /* compute e and d */
914         cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
915         cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
916
917         ellist_insert(cl);
918 }
919
920 static void
921 update_ed(struct hfsc_class *cl, int next_len)
922 {
923         cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
924         cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
925
926         ellist_update(cl);
927 }
928
929 static void
930 update_d(struct hfsc_class *cl, int next_len)
931 {
932         cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
933 }
934
935 static void
936 init_vf(struct hfsc_class *cl, int len)
937 {
938         struct hfsc_class *max_cl, *p;
939         uint64_t vt, f, cur_time;
940         int go_active;
941
942         cur_time = 0;
943         go_active = 1;
944         for ( ; cl->cl_parent != NULL; cl = cl->cl_parent) {
945                 if (go_active && cl->cl_nactive++ == 0)
946                         go_active = 1;
947                 else
948                         go_active = 0;
949
950                 if (go_active) {
951                         max_cl = actlist_last(cl->cl_parent->cl_actc);
952                         if (max_cl != NULL) {
953                                 /*
954                                  * set vt to the average of the min and max
955                                  * classes.  if the parent's period didn't
956                                  * change, don't decrease vt of the class.
957                                  */
958                                 vt = max_cl->cl_vt;
959                                 if (cl->cl_parent->cl_cvtmin != 0)
960                                         vt = (cl->cl_parent->cl_cvtmin + vt)/2;
961
962                                 if (cl->cl_parent->cl_vtperiod !=
963                                     cl->cl_parentperiod || vt > cl->cl_vt)
964                                         cl->cl_vt = vt;
965                         } else {
966                                 /*
967                                  * first child for a new parent backlog period.
968                                  * add parent's cvtmax to vtoff of children
969                                  * to make a new vt (vtoff + vt) larger than
970                                  * the vt in the last period for all children.
971                                  */
972                                 vt = cl->cl_parent->cl_cvtmax;
973                                 for (p = cl->cl_parent->cl_children; p != NULL;
974                                      p = p->cl_siblings)
975                                         p->cl_vtoff += vt;
976                                 cl->cl_vt = 0;
977                                 cl->cl_parent->cl_cvtmax = 0;
978                                 cl->cl_parent->cl_cvtmin = 0;
979                         }
980                         cl->cl_initvt = cl->cl_vt;
981
982                         /* update the virtual curve */
983                         vt = cl->cl_vt + cl->cl_vtoff;
984                         rtsc_min(&cl->cl_virtual, cl->cl_fsc, vt, cl->cl_total);
985                         if (cl->cl_virtual.x == vt) {
986                                 cl->cl_virtual.x -= cl->cl_vtoff;
987                                 cl->cl_vtoff = 0;
988                         }
989                         cl->cl_vtadj = 0;
990
991                         cl->cl_vtperiod++;  /* increment vt period */
992                         cl->cl_parentperiod = cl->cl_parent->cl_vtperiod;
993                         if (cl->cl_parent->cl_nactive == 0)
994                                 cl->cl_parentperiod++;
995                         cl->cl_f = 0;
996
997                         actlist_insert(cl);
998
999                         if (cl->cl_usc != NULL) {
1000                                 /* class has upper limit curve */
1001                                 if (cur_time == 0)
1002                                         cur_time = read_machclk();
1003
1004                                 /* update the ulimit curve */
1005                                 rtsc_min(&cl->cl_ulimit, cl->cl_usc, cur_time,
1006                                     cl->cl_total);
1007                                 /* compute myf */
1008                                 cl->cl_myf = rtsc_y2x(&cl->cl_ulimit,
1009                                     cl->cl_total);
1010                                 cl->cl_myfadj = 0;
1011                         }
1012                 }
1013
1014                 if (cl->cl_myf > cl->cl_cfmin)
1015                         f = cl->cl_myf;
1016                 else
1017                         f = cl->cl_cfmin;
1018                 if (f != cl->cl_f) {
1019                         cl->cl_f = f;
1020                         update_cfmin(cl->cl_parent);
1021                 }
1022         }
1023 }
1024
1025 static void
1026 update_vf(struct hfsc_class *cl, int len, uint64_t cur_time)
1027 {
1028         uint64_t f, myf_bound, delta;
1029         int go_passive;
1030
1031         go_passive = qempty(cl->cl_q);
1032
1033         for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
1034                 cl->cl_total += len;
1035
1036                 if (cl->cl_fsc == NULL || cl->cl_nactive == 0)
1037                         continue;
1038
1039                 if (go_passive && --cl->cl_nactive == 0)
1040                         go_passive = 1;
1041                 else
1042                         go_passive = 0;
1043
1044                 if (go_passive) {
1045                         /* no more active child, going passive */
1046
1047                         /* update cvtmax of the parent class */
1048                         if (cl->cl_vt > cl->cl_parent->cl_cvtmax)
1049                                 cl->cl_parent->cl_cvtmax = cl->cl_vt;
1050
1051                         /* remove this class from the vt list */
1052                         actlist_remove(cl);
1053
1054                         update_cfmin(cl->cl_parent);
1055
1056                         continue;
1057                 }
1058
1059                 /*
1060                  * update vt and f
1061                  */
1062                 cl->cl_vt = rtsc_y2x(&cl->cl_virtual, cl->cl_total)
1063                     - cl->cl_vtoff + cl->cl_vtadj;
1064
1065                 /*
1066                  * if vt of the class is smaller than cvtmin,
1067                  * the class was skipped in the past due to non-fit.
1068                  * if so, we need to adjust vtadj.
1069                  */
1070                 if (cl->cl_vt < cl->cl_parent->cl_cvtmin) {
1071                         cl->cl_vtadj += cl->cl_parent->cl_cvtmin - cl->cl_vt;
1072                         cl->cl_vt = cl->cl_parent->cl_cvtmin;
1073                 }
1074
1075                 /* update the vt list */
1076                 actlist_update(cl);
1077
1078                 if (cl->cl_usc != NULL) {
1079                         cl->cl_myf = cl->cl_myfadj
1080                             + rtsc_y2x(&cl->cl_ulimit, cl->cl_total);
1081
1082                         /*
1083                          * if myf lags behind by more than one clock tick
1084                          * from the current time, adjust myfadj to prevent
1085                          * a rate-limited class from going greedy.
1086                          * in a steady state under rate-limiting, myf
1087                          * fluctuates within one clock tick.
1088                          */
1089                         myf_bound = cur_time - machclk_per_tick;
1090                         if (cl->cl_myf < myf_bound) {
1091                                 delta = cur_time - cl->cl_myf;
1092                                 cl->cl_myfadj += delta;
1093                                 cl->cl_myf += delta;
1094                         }
1095                 }
1096
1097                 /* cl_f is max(cl_myf, cl_cfmin) */
1098                 if (cl->cl_myf > cl->cl_cfmin)
1099                         f = cl->cl_myf;
1100                 else
1101                         f = cl->cl_cfmin;
1102                 if (f != cl->cl_f) {
1103                         cl->cl_f = f;
1104                         update_cfmin(cl->cl_parent);
1105                 }
1106         }
1107 }
1108
1109 static void
1110 update_cfmin(struct hfsc_class *cl)
1111 {
1112         struct hfsc_class *p;
1113         uint64_t cfmin;
1114
1115         if (TAILQ_EMPTY(cl->cl_actc)) {
1116                 cl->cl_cfmin = 0;
1117                 return;
1118         }
1119         cfmin = HT_INFINITY;
1120         TAILQ_FOREACH(p, cl->cl_actc, cl_actlist) {
1121                 if (p->cl_f == 0) {
1122                         cl->cl_cfmin = 0;
1123                         return;
1124                 }
1125                 if (p->cl_f < cfmin)
1126                         cfmin = p->cl_f;
1127         }
1128         cl->cl_cfmin = cfmin;
1129 }
1130
1131 /*
1132  * TAILQ based ellist and actlist implementation
1133  * (ion wanted to make a calendar queue based implementation)
1134  */
1135 /*
1136  * eligible list holds backlogged classes being sorted by their eligible times.
1137  * there is one eligible list per interface.
1138  */
1139
1140 static ellist_t *
1141 ellist_alloc(void)
1142 {
1143         ellist_t *head;
1144
1145         head = kmalloc(sizeof(ellist_t *), M_ALTQ, M_WAITOK);
1146         TAILQ_INIT(head);
1147         return (head);
1148 }
1149
1150 static void
1151 ellist_destroy(ellist_t *head)
1152 {
1153         kfree(head, M_ALTQ);
1154 }
1155
1156 static void
1157 ellist_insert(struct hfsc_class *cl)
1158 {
1159         struct hfsc_if *hif = cl->cl_hif;
1160         struct hfsc_class *p;
1161
1162         /* check the last entry first */
1163         if ((p = TAILQ_LAST(hif->hif_eligible, _eligible)) == NULL ||
1164             p->cl_e <= cl->cl_e) {
1165                 TAILQ_INSERT_TAIL(hif->hif_eligible, cl, cl_ellist);
1166                 return;
1167         }
1168
1169         TAILQ_FOREACH(p, hif->hif_eligible, cl_ellist) {
1170                 if (cl->cl_e < p->cl_e) {
1171                         TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1172                         return;
1173                 }
1174         }
1175         KKASSERT(0); /* should not reach here */
1176 }
1177
1178 static void
1179 ellist_remove(struct hfsc_class *cl)
1180 {
1181         struct hfsc_if *hif = cl->cl_hif;
1182
1183         TAILQ_REMOVE(hif->hif_eligible, cl, cl_ellist);
1184 }
1185
1186 static void
1187 ellist_update(struct hfsc_class *cl)
1188 {
1189         struct hfsc_if *hif = cl->cl_hif;
1190         struct hfsc_class *p, *last;
1191
1192         /*
1193          * the eligible time of a class increases monotonically.
1194          * if the next entry has a larger eligible time, nothing to do.
1195          */
1196         p = TAILQ_NEXT(cl, cl_ellist);
1197         if (p == NULL || cl->cl_e <= p->cl_e)
1198                 return;
1199
1200         /* check the last entry */
1201         last = TAILQ_LAST(hif->hif_eligible, _eligible);
1202         KKASSERT(last != NULL);
1203         if (last->cl_e <= cl->cl_e) {
1204                 TAILQ_REMOVE(hif->hif_eligible, cl, cl_ellist);
1205                 TAILQ_INSERT_TAIL(hif->hif_eligible, cl, cl_ellist);
1206                 return;
1207         }
1208
1209         /*
1210          * the new position must be between the next entry
1211          * and the last entry
1212          */
1213         while ((p = TAILQ_NEXT(p, cl_ellist)) != NULL) {
1214                 if (cl->cl_e < p->cl_e) {
1215                         TAILQ_REMOVE(hif->hif_eligible, cl, cl_ellist);
1216                         TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1217                         return;
1218                 }
1219         }
1220         KKASSERT(0); /* should not reach here */
1221 }
1222
1223 /* find the class with the minimum deadline among the eligible classes */
1224 struct hfsc_class *
1225 ellist_get_mindl(ellist_t *head, uint64_t cur_time)
1226 {
1227         struct hfsc_class *p, *cl = NULL;
1228
1229         TAILQ_FOREACH(p, head, cl_ellist) {
1230                 if (p->cl_e > cur_time)
1231                         break;
1232                 if (cl == NULL || p->cl_d < cl->cl_d)
1233                         cl = p;
1234         }
1235         return (cl);
1236 }
1237
1238 /*
1239  * active children list holds backlogged child classes being sorted
1240  * by their virtual time.
1241  * each intermediate class has one active children list.
1242  */
1243 static actlist_t *
1244 actlist_alloc(void)
1245 {
1246         actlist_t *head;
1247
1248         head = kmalloc(sizeof(*head), M_ALTQ, M_WAITOK);
1249         TAILQ_INIT(head);
1250         return (head);
1251 }
1252
1253 static void
1254 actlist_destroy(actlist_t *head)
1255 {
1256         kfree(head, M_ALTQ);
1257 }
1258 static void
1259 actlist_insert(struct hfsc_class *cl)
1260 {
1261         struct hfsc_class *p;
1262
1263         /* check the last entry first */
1264         if ((p = TAILQ_LAST(cl->cl_parent->cl_actc, _active)) == NULL
1265             || p->cl_vt <= cl->cl_vt) {
1266                 TAILQ_INSERT_TAIL(cl->cl_parent->cl_actc, cl, cl_actlist);
1267                 return;
1268         }
1269
1270         TAILQ_FOREACH(p, cl->cl_parent->cl_actc, cl_actlist) {
1271                 if (cl->cl_vt < p->cl_vt) {
1272                         TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1273                         return;
1274                 }
1275         }
1276         KKASSERT(0); /* should not reach here */
1277 }
1278
1279 static void
1280 actlist_remove(struct hfsc_class *cl)
1281 {
1282         TAILQ_REMOVE(cl->cl_parent->cl_actc, cl, cl_actlist);
1283 }
1284
1285 static void
1286 actlist_update(struct hfsc_class *cl)
1287 {
1288         struct hfsc_class *p, *last;
1289
1290         /*
1291          * the virtual time of a class increases monotonically during its
1292          * backlogged period.
1293          * if the next entry has a larger virtual time, nothing to do.
1294          */
1295         p = TAILQ_NEXT(cl, cl_actlist);
1296         if (p == NULL || cl->cl_vt < p->cl_vt)
1297                 return;
1298
1299         /* check the last entry */
1300         last = TAILQ_LAST(cl->cl_parent->cl_actc, _active);
1301         KKASSERT(last != NULL);
1302         if (last->cl_vt <= cl->cl_vt) {
1303                 TAILQ_REMOVE(cl->cl_parent->cl_actc, cl, cl_actlist);
1304                 TAILQ_INSERT_TAIL(cl->cl_parent->cl_actc, cl, cl_actlist);
1305                 return;
1306         }
1307
1308         /*
1309          * the new position must be between the next entry
1310          * and the last entry
1311          */
1312         while ((p = TAILQ_NEXT(p, cl_actlist)) != NULL) {
1313                 if (cl->cl_vt < p->cl_vt) {
1314                         TAILQ_REMOVE(cl->cl_parent->cl_actc, cl, cl_actlist);
1315                         TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1316                         return;
1317                 }
1318         }
1319         KKASSERT(0); /* should not reach here */
1320 }
1321
1322 static struct hfsc_class *
1323 actlist_firstfit(struct hfsc_class *cl, uint64_t cur_time)
1324 {
1325         struct hfsc_class *p;
1326
1327         TAILQ_FOREACH(p, cl->cl_actc, cl_actlist) {
1328                 if (p->cl_f <= cur_time)
1329                         return (p);
1330         }
1331         return (NULL);
1332 }
1333
1334 /*
1335  * service curve support functions
1336  *
1337  *  external service curve parameters
1338  *      m: bits/sec
1339  *      d: msec
1340  *  internal service curve parameters
1341  *      sm: (bytes/tsc_interval) << SM_SHIFT
1342  *      ism: (tsc_count/byte) << ISM_SHIFT
1343  *      dx: tsc_count
1344  *
1345  * SM_SHIFT and ISM_SHIFT are scaled in order to keep effective digits.
1346  * we should be able to handle 100K-1Gbps linkspeed with 200Hz-1GHz CPU
1347  * speed.  SM_SHIFT and ISM_SHIFT are selected to have at least 3 effective
1348  * digits in decimal using the following table.
1349  *
1350  *  bits/sec    100Kbps     1Mbps     10Mbps     100Mbps    1Gbps
1351  *  ----------+-------------------------------------------------------
1352  *  bytes/nsec  12.5e-6    125e-6     1250e-6    12500e-6   125000e-6
1353  *  sm(500MHz)  25.0e-6    250e-6     2500e-6    25000e-6   250000e-6
1354  *  sm(200MHz)  62.5e-6    625e-6     6250e-6    62500e-6   625000e-6
1355  *
1356  *  nsec/byte   80000      8000       800        80         8
1357  *  ism(500MHz) 40000      4000       400        40         4
1358  *  ism(200MHz) 16000      1600       160        16         1.6
1359  */
1360 #define SM_SHIFT        24
1361 #define ISM_SHIFT       10
1362
1363 #define SM_MASK         ((1LL << SM_SHIFT) - 1)
1364 #define ISM_MASK        ((1LL << ISM_SHIFT) - 1)
1365
1366 static __inline uint64_t
1367 seg_x2y(uint64_t x, uint64_t sm)
1368 {
1369         uint64_t y;
1370
1371         /*
1372          * compute
1373          *      y = x * sm >> SM_SHIFT
1374          * but divide it for the upper and lower bits to avoid overflow
1375          */
1376         y = (x >> SM_SHIFT) * sm + (((x & SM_MASK) * sm) >> SM_SHIFT);
1377         return (y);
1378 }
1379
1380 static __inline uint64_t
1381 seg_y2x(uint64_t y, uint64_t ism)
1382 {
1383         uint64_t x;
1384
1385         if (y == 0)
1386                 x = 0;
1387         else if (ism == HT_INFINITY)
1388                 x = HT_INFINITY;
1389         else
1390                 x = (y >> ISM_SHIFT) * ism + (((y & ISM_MASK) * ism) >> ISM_SHIFT);
1391
1392         return (x);
1393 }
1394
1395 static __inline uint64_t
1396 m2sm(u_int m)
1397 {
1398         uint64_t sm;
1399
1400         sm = ((uint64_t)m << SM_SHIFT) / 8 / machclk_freq;
1401         return (sm);
1402 }
1403
1404 static __inline uint64_t
1405 m2ism(u_int m)
1406 {
1407         uint64_t ism;
1408
1409         if (m == 0)
1410                 ism = HT_INFINITY;
1411         else
1412                 ism = ((uint64_t)machclk_freq << ISM_SHIFT) * 8 / m;
1413         return (ism);
1414 }
1415
1416 static __inline uint64_t
1417 d2dx(u_int d)
1418 {
1419         uint64_t dx;
1420
1421         dx = ((uint64_t)d * machclk_freq) / 1000;
1422         return (dx);
1423 }
1424
1425 static u_int
1426 sm2m(uint64_t sm)
1427 {
1428         uint64_t m;
1429
1430         m = (sm * 8 * machclk_freq) >> SM_SHIFT;
1431         return ((u_int)m);
1432 }
1433
1434 static u_int
1435 dx2d(uint64_t dx)
1436 {
1437         uint64_t d;
1438
1439         d = dx * 1000 / machclk_freq;
1440         return ((u_int)d);
1441 }
1442
1443 static void
1444 sc2isc(struct service_curve *sc, struct internal_sc *isc)
1445 {
1446         isc->sm1 = m2sm(sc->m1);
1447         isc->ism1 = m2ism(sc->m1);
1448         isc->dx = d2dx(sc->d);
1449         isc->dy = seg_x2y(isc->dx, isc->sm1);
1450         isc->sm2 = m2sm(sc->m2);
1451         isc->ism2 = m2ism(sc->m2);
1452 }
1453
1454 /*
1455  * initialize the runtime service curve with the given internal
1456  * service curve starting at (x, y).
1457  */
1458 static void
1459 rtsc_init(struct runtime_sc *rtsc, struct internal_sc *isc, uint64_t x, uint64_t y)
1460 {
1461         rtsc->x = x;
1462         rtsc->y = y;
1463         rtsc->sm1 = isc->sm1;
1464         rtsc->ism1 = isc->ism1;
1465         rtsc->dx = isc->dx;
1466         rtsc->dy = isc->dy;
1467         rtsc->sm2 = isc->sm2;
1468         rtsc->ism2 = isc->ism2;
1469 }
1470
1471 /*
1472  * calculate the y-projection of the runtime service curve by the
1473  * given x-projection value
1474  */
1475 static uint64_t
1476 rtsc_y2x(struct runtime_sc *rtsc, uint64_t y)
1477 {
1478         uint64_t x;
1479
1480         if (y < rtsc->y) {
1481                 x = rtsc->x;
1482         } else if (y <= rtsc->y + rtsc->dy) {
1483                 /* x belongs to the 1st segment */
1484                 if (rtsc->dy == 0)
1485                         x = rtsc->x + rtsc->dx;
1486                 else
1487                         x = rtsc->x + seg_y2x(y - rtsc->y, rtsc->ism1);
1488         } else {
1489                 /* x belongs to the 2nd segment */
1490                 x = rtsc->x + rtsc->dx
1491                     + seg_y2x(y - rtsc->y - rtsc->dy, rtsc->ism2);
1492         }
1493         return (x);
1494 }
1495
1496 static uint64_t
1497 rtsc_x2y(struct runtime_sc *rtsc, uint64_t x)
1498 {
1499         uint64_t y;
1500
1501         if (x <= rtsc->x) {
1502                 y = rtsc->y;
1503         } else if (x <= rtsc->x + rtsc->dx) {
1504                 /* y belongs to the 1st segment */
1505                 y = rtsc->y + seg_x2y(x - rtsc->x, rtsc->sm1);
1506         } else
1507                 /* y belongs to the 2nd segment */
1508                 y = rtsc->y + rtsc->dy
1509                     + seg_x2y(x - rtsc->x - rtsc->dx, rtsc->sm2);
1510         return (y);
1511 }
1512
1513 /*
1514  * update the runtime service curve by taking the minimum of the current
1515  * runtime service curve and the service curve starting at (x, y).
1516  */
1517 static void
1518 rtsc_min(struct runtime_sc *rtsc, struct internal_sc *isc, uint64_t x, uint64_t y)
1519 {
1520         uint64_t y1, y2, dx, dy;
1521
1522         if (isc->sm1 <= isc->sm2) {
1523                 /* service curve is convex */
1524                 y1 = rtsc_x2y(rtsc, x);
1525                 if (y1 < y)
1526                         /* the current rtsc is smaller */
1527                         return;
1528                 rtsc->x = x;
1529                 rtsc->y = y;
1530                 return;
1531         }
1532
1533         /*
1534          * service curve is concave
1535          * compute the two y values of the current rtsc
1536          *      y1: at x
1537          *      y2: at (x + dx)
1538          */
1539         y1 = rtsc_x2y(rtsc, x);
1540         if (y1 <= y) {
1541                 /* rtsc is below isc, no change to rtsc */
1542                 return;
1543         }
1544
1545         y2 = rtsc_x2y(rtsc, x + isc->dx);
1546         if (y2 >= y + isc->dy) {
1547                 /* rtsc is above isc, replace rtsc by isc */
1548                 rtsc->x = x;
1549                 rtsc->y = y;
1550                 rtsc->dx = isc->dx;
1551                 rtsc->dy = isc->dy;
1552                 return;
1553         }
1554
1555         /*
1556          * the two curves intersect
1557          * compute the offsets (dx, dy) using the reverse
1558          * function of seg_x2y()
1559          *      seg_x2y(dx, sm1) == seg_x2y(dx, sm2) + (y1 - y)
1560          */
1561         dx = ((y1 - y) << SM_SHIFT) / (isc->sm1 - isc->sm2);
1562         /*
1563          * check if (x, y1) belongs to the 1st segment of rtsc.
1564          * if so, add the offset.
1565          */
1566         if (rtsc->x + rtsc->dx > x)
1567                 dx += rtsc->x + rtsc->dx - x;
1568         dy = seg_x2y(dx, isc->sm1);
1569
1570         rtsc->x = x;
1571         rtsc->y = y;
1572         rtsc->dx = dx;
1573         rtsc->dy = dy;
1574 }
1575
1576 static void
1577 get_class_stats(struct hfsc_classstats *sp, struct hfsc_class *cl)
1578 {
1579         sp->class_id = cl->cl_id;
1580         sp->class_handle = cl->cl_handle;
1581
1582         if (cl->cl_rsc != NULL) {
1583                 sp->rsc.m1 = sm2m(cl->cl_rsc->sm1);
1584                 sp->rsc.d = dx2d(cl->cl_rsc->dx);
1585                 sp->rsc.m2 = sm2m(cl->cl_rsc->sm2);
1586         } else {
1587                 sp->rsc.m1 = 0;
1588                 sp->rsc.d = 0;
1589                 sp->rsc.m2 = 0;
1590         }
1591         if (cl->cl_fsc != NULL) {
1592                 sp->fsc.m1 = sm2m(cl->cl_fsc->sm1);
1593                 sp->fsc.d = dx2d(cl->cl_fsc->dx);
1594                 sp->fsc.m2 = sm2m(cl->cl_fsc->sm2);
1595         } else {
1596                 sp->fsc.m1 = 0;
1597                 sp->fsc.d = 0;
1598                 sp->fsc.m2 = 0;
1599         }
1600         if (cl->cl_usc != NULL) {
1601                 sp->usc.m1 = sm2m(cl->cl_usc->sm1);
1602                 sp->usc.d = dx2d(cl->cl_usc->dx);
1603                 sp->usc.m2 = sm2m(cl->cl_usc->sm2);
1604         } else {
1605                 sp->usc.m1 = 0;
1606                 sp->usc.d = 0;
1607                 sp->usc.m2 = 0;
1608         }
1609
1610         sp->total = cl->cl_total;
1611         sp->cumul = cl->cl_cumul;
1612
1613         sp->d = cl->cl_d;
1614         sp->e = cl->cl_e;
1615         sp->vt = cl->cl_vt;
1616         sp->f = cl->cl_f;
1617
1618         sp->initvt = cl->cl_initvt;
1619         sp->vtperiod = cl->cl_vtperiod;
1620         sp->parentperiod = cl->cl_parentperiod;
1621         sp->nactive = cl->cl_nactive;
1622         sp->vtoff = cl->cl_vtoff;
1623         sp->cvtmax = cl->cl_cvtmax;
1624         sp->myf = cl->cl_myf;
1625         sp->cfmin = cl->cl_cfmin;
1626         sp->cvtmin = cl->cl_cvtmin;
1627         sp->myfadj = cl->cl_myfadj;
1628         sp->vtadj = cl->cl_vtadj;
1629
1630         sp->cur_time = read_machclk();
1631         sp->machclk_freq = machclk_freq;
1632
1633         sp->qlength = qlen(cl->cl_q);
1634         sp->qlimit = qlimit(cl->cl_q);
1635         sp->xmit_cnt = cl->cl_stats.xmit_cnt;
1636         sp->drop_cnt = cl->cl_stats.drop_cnt;
1637         sp->period = cl->cl_stats.period;
1638
1639         sp->qtype = qtype(cl->cl_q);
1640 #ifdef ALTQ_RED
1641         if (q_is_red(cl->cl_q))
1642                 red_getstats(cl->cl_red, &sp->red[0]);
1643 #endif
1644 #ifdef ALTQ_RIO
1645         if (q_is_rio(cl->cl_q))
1646                 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
1647 #endif
1648 }
1649
1650 /* convert a class handle to the corresponding class pointer */
1651 static struct hfsc_class *
1652 clh_to_clp(struct hfsc_if *hif, uint32_t chandle)
1653 {
1654         int i;
1655         struct hfsc_class *cl;
1656
1657         if (chandle == 0)
1658                 return (NULL);
1659         /*
1660          * first, try optimistically the slot matching the lower bits of
1661          * the handle.  if it fails, do the linear table search.
1662          */
1663         i = chandle % HFSC_MAX_CLASSES;
1664         if ((cl = hif->hif_class_tbl[i]) != NULL && cl->cl_handle == chandle)
1665                 return (cl);
1666         for (i = 0; i < HFSC_MAX_CLASSES; i++)
1667                 if ((cl = hif->hif_class_tbl[i]) != NULL &&
1668                     cl->cl_handle == chandle)
1669                         return (cl);
1670         return (NULL);
1671 }
1672
1673 #endif /* ALTQ_HFSC */