2 * Copyright (c) 1997-1999 Carnegie Mellon University. All Rights Reserved.
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation is hereby granted (including for commercial or
6 * for-profit use), provided that both the copyright notice and this
7 * permission notice appear in all copies of the software, derivative
8 * works, or modified versions, and any portions thereof.
10 * THIS SOFTWARE IS EXPERIMENTAL AND IS KNOWN TO HAVE BUGS, SOME OF
11 * WHICH MAY HAVE SERIOUS CONSEQUENCES. CARNEGIE MELLON PROVIDES THIS
12 * SOFTWARE IN ITS ``AS IS'' CONDITION, AND ANY EXPRESS OR IMPLIED
13 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
14 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 * DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
16 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
18 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
19 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
20 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
22 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
25 * Carnegie Mellon encourages (but does not require) users of this
26 * software to return any improvements or extensions that they make,
27 * and to grant Carnegie Mellon the rights to redistribute these
28 * changes without encumbrance.
30 * $KAME: altq_hfsc.c,v 1.24 2003/12/05 05:40:46 kjc Exp $
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.
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.
47 #include "opt_inet6.h"
49 #ifdef ALTQ_HFSC /* hfsc is enabled by ALTQ_HFSC option in opt_altq.h */
51 #include <sys/param.h>
52 #include <sys/malloc.h>
54 #include <sys/socket.h>
55 #include <sys/systm.h>
56 #include <sys/errno.h>
57 #include <sys/queue.h>
58 #if 1 /* ALTQ3_COMPAT */
59 #include <sys/sockio.h>
61 #include <sys/kernel.h>
62 #endif /* ALTQ3_COMPAT */
65 #include <net/if_var.h>
66 #include <netinet/in.h>
68 #include <netpfil/pf/pf.h>
69 #include <netpfil/pf/pf_altq.h>
70 #include <netpfil/pf/pf_mtag.h>
71 #include <net/altq/altq.h>
72 #include <net/altq/altq_hfsc.h>
77 static int hfsc_clear_interface(struct hfsc_if *);
78 static int hfsc_request(struct ifaltq *, int, void *);
79 static void hfsc_purge(struct hfsc_if *);
80 static struct hfsc_class *hfsc_class_create(struct hfsc_if *,
81 struct service_curve *, struct service_curve *, struct service_curve *,
82 struct hfsc_class *, int, int, int);
83 static int hfsc_class_destroy(struct hfsc_class *);
84 static struct hfsc_class *hfsc_nextclass(struct hfsc_class *);
85 static int hfsc_enqueue(struct ifaltq *, struct mbuf *,
86 struct altq_pktattr *);
87 static struct mbuf *hfsc_dequeue(struct ifaltq *, int);
89 static int hfsc_addq(struct hfsc_class *, struct mbuf *);
90 static struct mbuf *hfsc_getq(struct hfsc_class *);
91 static struct mbuf *hfsc_pollq(struct hfsc_class *);
92 static void hfsc_purgeq(struct hfsc_class *);
94 static void update_cfmin(struct hfsc_class *);
95 static void set_active(struct hfsc_class *, int);
96 static void set_passive(struct hfsc_class *);
98 static void init_ed(struct hfsc_class *, int);
99 static void update_ed(struct hfsc_class *, int);
100 static void update_d(struct hfsc_class *, int);
101 static void init_vf(struct hfsc_class *, int);
102 static void update_vf(struct hfsc_class *, int, u_int64_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 *hfsc_get_mindl(struct hfsc_if *, u_int64_t);
107 static void actlist_insert(struct hfsc_class *);
108 static void actlist_remove(struct hfsc_class *);
109 static void actlist_update(struct hfsc_class *);
111 static struct hfsc_class *actlist_firstfit(struct hfsc_class *,
114 static __inline u_int64_t seg_x2y(u_int64_t, u_int64_t);
115 static __inline u_int64_t seg_y2x(u_int64_t, u_int64_t);
116 static __inline u_int64_t m2sm(u_int64_t);
117 static __inline u_int64_t m2ism(u_int64_t);
118 static __inline u_int64_t d2dx(u_int);
119 static u_int64_t sm2m(u_int64_t);
120 static u_int dx2d(u_int64_t);
122 static void sc2isc(struct service_curve *, struct internal_sc *);
123 static void rtsc_init(struct runtime_sc *, struct internal_sc *,
124 u_int64_t, u_int64_t);
125 static u_int64_t rtsc_y2x(struct runtime_sc *, u_int64_t);
126 static u_int64_t rtsc_x2y(struct runtime_sc *, u_int64_t);
127 static void rtsc_min(struct runtime_sc *, struct internal_sc *,
128 u_int64_t, u_int64_t);
130 static void get_class_stats_v0(struct hfsc_classstats_v0 *,
131 struct hfsc_class *);
132 static void get_class_stats_v1(struct hfsc_classstats_v1 *,
133 struct hfsc_class *);
134 static struct hfsc_class *clh_to_clp(struct hfsc_if *, u_int32_t);
141 #define is_a_parent_class(cl) ((cl)->cl_children != NULL)
143 #define HT_INFINITY 0xffffffffffffffffULL /* infinite time value */
147 hfsc_pfattach(struct pf_altq *a)
152 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
155 error = altq_attach(&ifp->if_snd, ALTQT_HFSC, a->altq_disc,
156 hfsc_enqueue, hfsc_dequeue, hfsc_request, NULL, NULL);
162 hfsc_add_altq(struct pf_altq *a)
167 if ((ifp = ifunit(a->ifname)) == NULL)
169 if (!ALTQ_IS_READY(&ifp->if_snd))
172 hif = malloc(sizeof(struct hfsc_if), M_DEVBUF, M_NOWAIT | M_ZERO);
176 TAILQ_INIT(&hif->hif_eligible);
177 hif->hif_ifq = &ifp->if_snd;
179 /* keep the state in pf_altq */
186 hfsc_remove_altq(struct pf_altq *a)
190 if ((hif = a->altq_disc) == NULL)
194 (void)hfsc_clear_interface(hif);
195 (void)hfsc_class_destroy(hif->hif_rootclass);
203 hfsc_add_queue(struct pf_altq *a)
206 struct hfsc_class *cl, *parent;
207 struct hfsc_opts_v1 *opts;
208 struct service_curve rtsc, lssc, ulsc;
210 if ((hif = a->altq_disc) == NULL)
213 opts = &a->pq_u.hfsc_opts;
215 if (a->parent_qid == HFSC_NULLCLASS_HANDLE &&
216 hif->hif_rootclass == NULL)
218 else if ((parent = clh_to_clp(hif, a->parent_qid)) == NULL)
224 if (clh_to_clp(hif, a->qid) != NULL)
227 rtsc.m1 = opts->rtsc_m1;
228 rtsc.d = opts->rtsc_d;
229 rtsc.m2 = opts->rtsc_m2;
230 lssc.m1 = opts->lssc_m1;
231 lssc.d = opts->lssc_d;
232 lssc.m2 = opts->lssc_m2;
233 ulsc.m1 = opts->ulsc_m1;
234 ulsc.d = opts->ulsc_d;
235 ulsc.m2 = opts->ulsc_m2;
237 cl = hfsc_class_create(hif, &rtsc, &lssc, &ulsc,
238 parent, a->qlimit, opts->flags, a->qid);
246 hfsc_remove_queue(struct pf_altq *a)
249 struct hfsc_class *cl;
251 if ((hif = a->altq_disc) == NULL)
254 if ((cl = clh_to_clp(hif, a->qid)) == NULL)
257 return (hfsc_class_destroy(cl));
261 hfsc_getqstats(struct pf_altq *a, void *ubuf, int *nbytes, int version)
264 struct hfsc_class *cl;
266 struct hfsc_classstats_v0 v0;
267 struct hfsc_classstats_v1 v1;
272 if ((hif = altq_lookup(a->ifname, ALTQT_HFSC)) == NULL)
275 if ((cl = clh_to_clp(hif, a->qid)) == NULL)
278 if (version > HFSC_STATS_VERSION)
281 memset(&stats, 0, sizeof(stats));
284 get_class_stats_v0(&stats.v0, cl);
285 stats_size = sizeof(struct hfsc_classstats_v0);
288 get_class_stats_v1(&stats.v1, cl);
289 stats_size = sizeof(struct hfsc_classstats_v1);
293 if (*nbytes < stats_size)
296 if ((error = copyout((caddr_t)&stats, ubuf, stats_size)) != 0)
298 *nbytes = stats_size;
303 * bring the interface back to the initial state by discarding
304 * all the filters and classes except the root class.
307 hfsc_clear_interface(struct hfsc_if *hif)
309 struct hfsc_class *cl;
312 /* clear out the classes */
313 while (hif->hif_rootclass != NULL &&
314 (cl = hif->hif_rootclass->cl_children) != NULL) {
316 * remove the first leaf class found in the hierarchy
319 for (; cl != NULL; cl = hfsc_nextclass(cl)) {
320 if (!is_a_parent_class(cl)) {
321 (void)hfsc_class_destroy(cl);
331 hfsc_request(struct ifaltq *ifq, int req, void *arg)
333 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
335 IFQ_LOCK_ASSERT(ifq);
345 /* discard all the queued packets on the interface */
347 hfsc_purge(struct hfsc_if *hif)
349 struct hfsc_class *cl;
351 for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl))
352 if (!qempty(cl->cl_q))
354 if (ALTQ_IS_ENABLED(hif->hif_ifq))
355 hif->hif_ifq->ifq_len = 0;
359 hfsc_class_create(struct hfsc_if *hif, struct service_curve *rsc,
360 struct service_curve *fsc, struct service_curve *usc,
361 struct hfsc_class *parent, int qlimit, int flags, int qid)
363 struct hfsc_class *cl, *p;
366 if (hif->hif_classes >= HFSC_MAX_CLASSES)
370 if (flags & HFCF_RED) {
372 printf("hfsc_class_create: RED not configured for HFSC!\n");
378 if (flags & HFCF_CODEL) {
380 printf("hfsc_class_create: CODEL not configured for HFSC!\n");
386 cl = malloc(sizeof(struct hfsc_class), M_DEVBUF, M_NOWAIT | M_ZERO);
390 cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF, M_NOWAIT | M_ZERO);
391 if (cl->cl_q == NULL)
394 TAILQ_INIT(&cl->cl_actc);
397 qlimit = 50; /* use default */
398 qlimit(cl->cl_q) = qlimit;
399 qtype(cl->cl_q) = Q_DROPTAIL;
402 cl->cl_flags = flags;
404 if (flags & (HFCF_RED|HFCF_RIO)) {
405 int red_flags, red_pkttime;
409 if (rsc != NULL && rsc->m2 > m2)
411 if (fsc != NULL && fsc->m2 > m2)
413 if (usc != NULL && usc->m2 > m2)
417 if (flags & HFCF_ECN)
418 red_flags |= REDF_ECN;
420 if (flags & HFCF_CLEARDSCP)
421 red_flags |= RIOF_CLEARDSCP;
424 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
426 red_pkttime = (int64_t)hif->hif_ifq->altq_ifp->if_mtu
427 * 1000 * 1000 * 1000 / (m2 / 8);
428 if (flags & HFCF_RED) {
429 cl->cl_red = red_alloc(0, 0,
430 qlimit(cl->cl_q) * 10/100,
431 qlimit(cl->cl_q) * 30/100,
432 red_flags, red_pkttime);
433 if (cl->cl_red != NULL)
434 qtype(cl->cl_q) = Q_RED;
438 cl->cl_red = (red_t *)rio_alloc(0, NULL,
439 red_flags, red_pkttime);
440 if (cl->cl_red != NULL)
441 qtype(cl->cl_q) = Q_RIO;
445 #endif /* ALTQ_RED */
447 if (flags & HFCF_CODEL) {
448 cl->cl_codel = codel_alloc(5, 100, 0);
449 if (cl->cl_codel != NULL)
450 qtype(cl->cl_q) = Q_CODEL;
454 if (rsc != NULL && (rsc->m1 != 0 || rsc->m2 != 0)) {
455 cl->cl_rsc = malloc(sizeof(struct internal_sc),
457 if (cl->cl_rsc == NULL)
459 sc2isc(rsc, cl->cl_rsc);
460 rtsc_init(&cl->cl_deadline, cl->cl_rsc, 0, 0);
461 rtsc_init(&cl->cl_eligible, cl->cl_rsc, 0, 0);
463 if (fsc != NULL && (fsc->m1 != 0 || fsc->m2 != 0)) {
464 cl->cl_fsc = malloc(sizeof(struct internal_sc),
466 if (cl->cl_fsc == NULL)
468 sc2isc(fsc, cl->cl_fsc);
469 rtsc_init(&cl->cl_virtual, cl->cl_fsc, 0, 0);
471 if (usc != NULL && (usc->m1 != 0 || usc->m2 != 0)) {
472 cl->cl_usc = malloc(sizeof(struct internal_sc),
474 if (cl->cl_usc == NULL)
476 sc2isc(usc, cl->cl_usc);
477 rtsc_init(&cl->cl_ulimit, cl->cl_usc, 0, 0);
480 cl->cl_id = hif->hif_classid++;
483 cl->cl_parent = parent;
486 IFQ_LOCK(hif->hif_ifq);
490 * find a free slot in the class table. if the slot matching
491 * the lower bits of qid is free, use this slot. otherwise,
492 * use the first free slot.
494 i = qid % HFSC_MAX_CLASSES;
495 if (hif->hif_class_tbl[i] == NULL)
496 hif->hif_class_tbl[i] = cl;
498 for (i = 0; i < HFSC_MAX_CLASSES; i++)
499 if (hif->hif_class_tbl[i] == NULL) {
500 hif->hif_class_tbl[i] = cl;
503 if (i == HFSC_MAX_CLASSES) {
504 IFQ_UNLOCK(hif->hif_ifq);
510 if (flags & HFCF_DEFAULTCLASS)
511 hif->hif_defaultclass = cl;
513 if (parent == NULL) {
514 /* this is root class */
515 hif->hif_rootclass = cl;
517 /* add this class to the children list of the parent */
518 if ((p = parent->cl_children) == NULL)
519 parent->cl_children = cl;
521 while (p->cl_siblings != NULL)
526 IFQ_UNLOCK(hif->hif_ifq);
532 if (cl->cl_red != NULL) {
534 if (q_is_rio(cl->cl_q))
535 rio_destroy((rio_t *)cl->cl_red);
538 if (q_is_red(cl->cl_q))
539 red_destroy(cl->cl_red);
542 if (q_is_codel(cl->cl_q))
543 codel_destroy(cl->cl_codel);
546 if (cl->cl_fsc != NULL)
547 free(cl->cl_fsc, M_DEVBUF);
548 if (cl->cl_rsc != NULL)
549 free(cl->cl_rsc, M_DEVBUF);
550 if (cl->cl_usc != NULL)
551 free(cl->cl_usc, M_DEVBUF);
552 if (cl->cl_q != NULL)
553 free(cl->cl_q, M_DEVBUF);
559 hfsc_class_destroy(struct hfsc_class *cl)
566 if (is_a_parent_class(cl))
570 IFQ_LOCK(cl->cl_hif->hif_ifq);
573 if (!qempty(cl->cl_q))
576 if (cl->cl_parent == NULL) {
577 /* this is root class */
579 struct hfsc_class *p = cl->cl_parent->cl_children;
582 cl->cl_parent->cl_children = cl->cl_siblings;
584 if (p->cl_siblings == cl) {
585 p->cl_siblings = cl->cl_siblings;
588 } while ((p = p->cl_siblings) != NULL);
592 for (i = 0; i < HFSC_MAX_CLASSES; i++)
593 if (cl->cl_hif->hif_class_tbl[i] == cl) {
594 cl->cl_hif->hif_class_tbl[i] = NULL;
598 cl->cl_hif->hif_classes--;
599 IFQ_UNLOCK(cl->cl_hif->hif_ifq);
602 if (cl->cl_red != NULL) {
604 if (q_is_rio(cl->cl_q))
605 rio_destroy((rio_t *)cl->cl_red);
608 if (q_is_red(cl->cl_q))
609 red_destroy(cl->cl_red);
612 if (q_is_codel(cl->cl_q))
613 codel_destroy(cl->cl_codel);
617 IFQ_LOCK(cl->cl_hif->hif_ifq);
618 if (cl == cl->cl_hif->hif_rootclass)
619 cl->cl_hif->hif_rootclass = NULL;
620 if (cl == cl->cl_hif->hif_defaultclass)
621 cl->cl_hif->hif_defaultclass = NULL;
622 IFQ_UNLOCK(cl->cl_hif->hif_ifq);
624 if (cl->cl_usc != NULL)
625 free(cl->cl_usc, M_DEVBUF);
626 if (cl->cl_fsc != NULL)
627 free(cl->cl_fsc, M_DEVBUF);
628 if (cl->cl_rsc != NULL)
629 free(cl->cl_rsc, M_DEVBUF);
630 free(cl->cl_q, M_DEVBUF);
637 * hfsc_nextclass returns the next class in the tree.
639 * for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl))
642 static struct hfsc_class *
643 hfsc_nextclass(struct hfsc_class *cl)
645 if (cl->cl_children != NULL)
646 cl = cl->cl_children;
647 else if (cl->cl_siblings != NULL)
648 cl = cl->cl_siblings;
650 while ((cl = cl->cl_parent) != NULL)
651 if (cl->cl_siblings) {
652 cl = cl->cl_siblings;
661 * hfsc_enqueue is an enqueue function to be registered to
662 * (*altq_enqueue) in struct ifaltq.
665 hfsc_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
667 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
668 struct hfsc_class *cl;
672 IFQ_LOCK_ASSERT(ifq);
674 /* grab class set by classifier */
675 if ((m->m_flags & M_PKTHDR) == 0) {
676 /* should not happen */
677 printf("altq: packet for %s does not have pkthdr\n",
678 ifq->altq_ifp->if_xname);
683 if ((t = pf_find_mtag(m)) != NULL)
684 cl = clh_to_clp(hif, t->qid);
685 if (cl == NULL || is_a_parent_class(cl)) {
686 cl = hif->hif_defaultclass;
692 cl->cl_pktattr = NULL;
694 if (hfsc_addq(cl, m) != 0) {
695 /* drop occurred. mbuf was freed in hfsc_addq. */
696 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, len);
700 cl->cl_hif->hif_packets++;
702 /* successfully queued. */
703 if (qlen(cl->cl_q) == 1)
704 set_active(cl, m_pktlen(m));
710 * hfsc_dequeue is a dequeue function to be registered to
711 * (*altq_dequeue) in struct ifaltq.
713 * note: ALTDQ_POLL returns the next packet without removing the packet
714 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
715 * ALTDQ_REMOVE must return the same packet if called immediately
719 hfsc_dequeue(struct ifaltq *ifq, int op)
721 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
722 struct hfsc_class *cl;
728 IFQ_LOCK_ASSERT(ifq);
730 if (hif->hif_packets == 0)
731 /* no packet in the tree */
734 cur_time = read_machclk();
736 if (op == ALTDQ_REMOVE && hif->hif_pollcache != NULL) {
738 cl = hif->hif_pollcache;
739 hif->hif_pollcache = NULL;
740 /* check if the class was scheduled by real-time criteria */
741 if (cl->cl_rsc != NULL)
742 realtime = (cl->cl_e <= cur_time);
745 * if there are eligible classes, use real-time criteria.
746 * find the class with the minimum deadline among
747 * the eligible classes.
749 if ((cl = hfsc_get_mindl(hif, cur_time))
757 * use link-sharing criteria
758 * get the class with the minimum vt in the hierarchy
760 cl = hif->hif_rootclass;
761 while (is_a_parent_class(cl)) {
763 cl = actlist_firstfit(cl, cur_time);
767 printf("%d fit but none found\n",fits);
772 * update parent's cl_cvtmin.
773 * don't update if the new vt is smaller.
775 if (cl->cl_parent->cl_cvtmin < cl->cl_vt)
776 cl->cl_parent->cl_cvtmin = cl->cl_vt;
783 if (op == ALTDQ_POLL) {
784 hif->hif_pollcache = cl;
792 panic("hfsc_dequeue:");
794 cl->cl_hif->hif_packets--;
796 PKTCNTR_ADD(&cl->cl_stats.xmit_cnt, len);
798 update_vf(cl, len, cur_time);
802 if (!qempty(cl->cl_q)) {
803 if (cl->cl_rsc != NULL) {
805 next_len = m_pktlen(qhead(cl->cl_q));
808 update_ed(cl, next_len);
810 update_d(cl, next_len);
813 /* the class becomes passive */
821 hfsc_addq(struct hfsc_class *cl, struct mbuf *m)
825 if (q_is_rio(cl->cl_q))
826 return rio_addq((rio_t *)cl->cl_red, cl->cl_q,
830 if (q_is_red(cl->cl_q))
831 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
834 if (q_is_codel(cl->cl_q))
835 return codel_addq(cl->cl_codel, cl->cl_q, m);
837 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
842 if (cl->cl_flags & HFCF_CLEARDSCP)
843 write_dsfield(m, cl->cl_pktattr, 0);
851 hfsc_getq(struct hfsc_class *cl)
854 if (q_is_rio(cl->cl_q))
855 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
858 if (q_is_red(cl->cl_q))
859 return red_getq(cl->cl_red, cl->cl_q);
862 if (q_is_codel(cl->cl_q))
863 return codel_getq(cl->cl_codel, cl->cl_q);
865 return _getq(cl->cl_q);
869 hfsc_pollq(struct hfsc_class *cl)
871 return qhead(cl->cl_q);
875 hfsc_purgeq(struct hfsc_class *cl)
879 if (qempty(cl->cl_q))
882 while ((m = _getq(cl->cl_q)) != NULL) {
883 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, m_pktlen(m));
885 cl->cl_hif->hif_packets--;
886 IFQ_DEC_LEN(cl->cl_hif->hif_ifq);
888 ASSERT(qlen(cl->cl_q) == 0);
890 update_vf(cl, 0, 0); /* remove cl from the actlist */
895 set_active(struct hfsc_class *cl, int len)
897 if (cl->cl_rsc != NULL)
899 if (cl->cl_fsc != NULL)
902 cl->cl_stats.period++;
906 set_passive(struct hfsc_class *cl)
908 if (cl->cl_rsc != NULL)
912 * actlist is now handled in update_vf() so that update_vf(cl, 0, 0)
913 * needs to be called explicitly to remove a class from actlist
918 init_ed(struct hfsc_class *cl, int next_len)
922 cur_time = read_machclk();
924 /* update the deadline curve */
925 rtsc_min(&cl->cl_deadline, cl->cl_rsc, cur_time, cl->cl_cumul);
928 * update the eligible curve.
929 * for concave, it is equal to the deadline curve.
930 * for convex, it is a linear curve with slope m2.
932 cl->cl_eligible = cl->cl_deadline;
933 if (cl->cl_rsc->sm1 <= cl->cl_rsc->sm2) {
934 cl->cl_eligible.dx = 0;
935 cl->cl_eligible.dy = 0;
938 /* compute e and d */
939 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
940 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
946 update_ed(struct hfsc_class *cl, int next_len)
948 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
949 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
955 update_d(struct hfsc_class *cl, int next_len)
957 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
961 init_vf(struct hfsc_class *cl, int len)
963 struct hfsc_class *max_cl, *p;
964 u_int64_t vt, f, cur_time;
969 for ( ; cl->cl_parent != NULL; cl = cl->cl_parent) {
971 if (go_active && cl->cl_nactive++ == 0)
977 max_cl = TAILQ_LAST(&cl->cl_parent->cl_actc, acthead);
978 if (max_cl != NULL) {
980 * set vt to the average of the min and max
981 * classes. if the parent's period didn't
982 * change, don't decrease vt of the class.
985 if (cl->cl_parent->cl_cvtmin != 0)
986 vt = (cl->cl_parent->cl_cvtmin + vt)/2;
988 if (cl->cl_parent->cl_vtperiod !=
989 cl->cl_parentperiod || vt > cl->cl_vt)
993 * first child for a new parent backlog period.
994 * add parent's cvtmax to vtoff of children
995 * to make a new vt (vtoff + vt) larger than
996 * the vt in the last period for all children.
998 vt = cl->cl_parent->cl_cvtmax;
999 for (p = cl->cl_parent->cl_children; p != NULL;
1003 cl->cl_parent->cl_cvtmax = 0;
1004 cl->cl_parent->cl_cvtmin = 0;
1006 cl->cl_initvt = cl->cl_vt;
1008 /* update the virtual curve */
1009 vt = cl->cl_vt + cl->cl_vtoff;
1010 rtsc_min(&cl->cl_virtual, cl->cl_fsc, vt, cl->cl_total);
1011 if (cl->cl_virtual.x == vt) {
1012 cl->cl_virtual.x -= cl->cl_vtoff;
1017 cl->cl_vtperiod++; /* increment vt period */
1018 cl->cl_parentperiod = cl->cl_parent->cl_vtperiod;
1019 if (cl->cl_parent->cl_nactive == 0)
1020 cl->cl_parentperiod++;
1025 if (cl->cl_usc != NULL) {
1026 /* class has upper limit curve */
1028 cur_time = read_machclk();
1030 /* update the ulimit curve */
1031 rtsc_min(&cl->cl_ulimit, cl->cl_usc, cur_time,
1034 cl->cl_myf = rtsc_y2x(&cl->cl_ulimit,
1040 if (cl->cl_myf > cl->cl_cfmin)
1044 if (f != cl->cl_f) {
1046 update_cfmin(cl->cl_parent);
1052 update_vf(struct hfsc_class *cl, int len, u_int64_t cur_time)
1054 u_int64_t f, myf_bound, delta;
1057 go_passive = qempty(cl->cl_q);
1059 for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
1061 cl->cl_total += len;
1063 if (cl->cl_fsc == NULL || cl->cl_nactive == 0)
1066 if (go_passive && --cl->cl_nactive == 0)
1072 /* no more active child, going passive */
1074 /* update cvtmax of the parent class */
1075 if (cl->cl_vt > cl->cl_parent->cl_cvtmax)
1076 cl->cl_parent->cl_cvtmax = cl->cl_vt;
1078 /* remove this class from the vt list */
1081 update_cfmin(cl->cl_parent);
1089 cl->cl_vt = rtsc_y2x(&cl->cl_virtual, cl->cl_total)
1090 - cl->cl_vtoff + cl->cl_vtadj;
1093 * if vt of the class is smaller than cvtmin,
1094 * the class was skipped in the past due to non-fit.
1095 * if so, we need to adjust vtadj.
1097 if (cl->cl_vt < cl->cl_parent->cl_cvtmin) {
1098 cl->cl_vtadj += cl->cl_parent->cl_cvtmin - cl->cl_vt;
1099 cl->cl_vt = cl->cl_parent->cl_cvtmin;
1102 /* update the vt list */
1105 if (cl->cl_usc != NULL) {
1106 cl->cl_myf = cl->cl_myfadj
1107 + rtsc_y2x(&cl->cl_ulimit, cl->cl_total);
1110 * if myf lags behind by more than one clock tick
1111 * from the current time, adjust myfadj to prevent
1112 * a rate-limited class from going greedy.
1113 * in a steady state under rate-limiting, myf
1114 * fluctuates within one clock tick.
1116 myf_bound = cur_time - machclk_per_tick;
1117 if (cl->cl_myf < myf_bound) {
1118 delta = cur_time - cl->cl_myf;
1119 cl->cl_myfadj += delta;
1120 cl->cl_myf += delta;
1124 /* cl_f is max(cl_myf, cl_cfmin) */
1125 if (cl->cl_myf > cl->cl_cfmin)
1129 if (f != cl->cl_f) {
1131 update_cfmin(cl->cl_parent);
1137 update_cfmin(struct hfsc_class *cl)
1139 struct hfsc_class *p;
1142 if (TAILQ_EMPTY(&cl->cl_actc)) {
1146 cfmin = HT_INFINITY;
1147 TAILQ_FOREACH(p, &cl->cl_actc, cl_actlist) {
1152 if (p->cl_f < cfmin)
1155 cl->cl_cfmin = cfmin;
1159 * TAILQ based ellist and actlist implementation
1160 * (ion wanted to make a calendar queue based implementation)
1163 * eligible list holds backlogged classes being sorted by their eligible times.
1164 * there is one eligible list per interface.
1168 ellist_insert(struct hfsc_class *cl)
1170 struct hfsc_if *hif = cl->cl_hif;
1171 struct hfsc_class *p;
1173 /* check the last entry first */
1174 if ((p = TAILQ_LAST(&hif->hif_eligible, elighead)) == NULL ||
1175 p->cl_e <= cl->cl_e) {
1176 TAILQ_INSERT_TAIL(&hif->hif_eligible, cl, cl_ellist);
1180 TAILQ_FOREACH(p, &hif->hif_eligible, cl_ellist) {
1181 if (cl->cl_e < p->cl_e) {
1182 TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1186 ASSERT(0); /* should not reach here */
1190 ellist_remove(struct hfsc_class *cl)
1192 struct hfsc_if *hif = cl->cl_hif;
1194 TAILQ_REMOVE(&hif->hif_eligible, cl, cl_ellist);
1198 ellist_update(struct hfsc_class *cl)
1200 struct hfsc_if *hif = cl->cl_hif;
1201 struct hfsc_class *p, *last;
1204 * the eligible time of a class increases monotonically.
1205 * if the next entry has a larger eligible time, nothing to do.
1207 p = TAILQ_NEXT(cl, cl_ellist);
1208 if (p == NULL || cl->cl_e <= p->cl_e)
1211 /* check the last entry */
1212 last = TAILQ_LAST(&hif->hif_eligible, elighead);
1213 ASSERT(last != NULL);
1214 if (last->cl_e <= cl->cl_e) {
1215 TAILQ_REMOVE(&hif->hif_eligible, cl, cl_ellist);
1216 TAILQ_INSERT_TAIL(&hif->hif_eligible, cl, cl_ellist);
1221 * the new position must be between the next entry
1222 * and the last entry
1224 while ((p = TAILQ_NEXT(p, cl_ellist)) != NULL) {
1225 if (cl->cl_e < p->cl_e) {
1226 TAILQ_REMOVE(&hif->hif_eligible, cl, cl_ellist);
1227 TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1231 ASSERT(0); /* should not reach here */
1234 /* find the class with the minimum deadline among the eligible classes */
1236 hfsc_get_mindl(struct hfsc_if *hif, u_int64_t cur_time)
1238 struct hfsc_class *p, *cl = NULL;
1240 TAILQ_FOREACH(p, &hif->hif_eligible, cl_ellist) {
1241 if (p->cl_e > cur_time)
1243 if (cl == NULL || p->cl_d < cl->cl_d)
1250 * active children list holds backlogged child classes being sorted
1251 * by their virtual time.
1252 * each intermediate class has one active children list.
1256 actlist_insert(struct hfsc_class *cl)
1258 struct hfsc_class *p;
1260 /* check the last entry first */
1261 if ((p = TAILQ_LAST(&cl->cl_parent->cl_actc, acthead)) == NULL
1262 || p->cl_vt <= cl->cl_vt) {
1263 TAILQ_INSERT_TAIL(&cl->cl_parent->cl_actc, cl, cl_actlist);
1267 TAILQ_FOREACH(p, &cl->cl_parent->cl_actc, cl_actlist) {
1268 if (cl->cl_vt < p->cl_vt) {
1269 TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1273 ASSERT(0); /* should not reach here */
1277 actlist_remove(struct hfsc_class *cl)
1279 TAILQ_REMOVE(&cl->cl_parent->cl_actc, cl, cl_actlist);
1283 actlist_update(struct hfsc_class *cl)
1285 struct hfsc_class *p, *last;
1288 * the virtual time of a class increases monotonically during its
1289 * backlogged period.
1290 * if the next entry has a larger virtual time, nothing to do.
1292 p = TAILQ_NEXT(cl, cl_actlist);
1293 if (p == NULL || cl->cl_vt < p->cl_vt)
1296 /* check the last entry */
1297 last = TAILQ_LAST(&cl->cl_parent->cl_actc, acthead);
1298 ASSERT(last != NULL);
1299 if (last->cl_vt <= cl->cl_vt) {
1300 TAILQ_REMOVE(&cl->cl_parent->cl_actc, cl, cl_actlist);
1301 TAILQ_INSERT_TAIL(&cl->cl_parent->cl_actc, cl, cl_actlist);
1306 * the new position must be between the next entry
1307 * and the last entry
1309 while ((p = TAILQ_NEXT(p, cl_actlist)) != NULL) {
1310 if (cl->cl_vt < p->cl_vt) {
1311 TAILQ_REMOVE(&cl->cl_parent->cl_actc, cl, cl_actlist);
1312 TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1316 ASSERT(0); /* should not reach here */
1319 static struct hfsc_class *
1320 actlist_firstfit(struct hfsc_class *cl, u_int64_t cur_time)
1322 struct hfsc_class *p;
1324 TAILQ_FOREACH(p, &cl->cl_actc, cl_actlist) {
1325 if (p->cl_f <= cur_time)
1332 * service curve support functions
1334 * external service curve parameters
1337 * internal service curve parameters
1338 * sm: (bytes/machclk tick) << SM_SHIFT
1339 * ism: (machclk ticks/byte) << ISM_SHIFT
1342 * SM_SHIFT and ISM_SHIFT are scaled in order to keep effective digits. we
1343 * should be able to handle 100K-100Gbps linkspeed with 256 MHz machclk
1344 * frequency and at least 3 effective digits in decimal.
1348 #define ISM_SHIFT 14
1350 #define SM_MASK ((1LL << SM_SHIFT) - 1)
1351 #define ISM_MASK ((1LL << ISM_SHIFT) - 1)
1353 static __inline u_int64_t
1354 seg_x2y(u_int64_t x, u_int64_t sm)
1360 * y = x * sm >> SM_SHIFT
1361 * but divide it for the upper and lower bits to avoid overflow
1363 y = (x >> SM_SHIFT) * sm + (((x & SM_MASK) * sm) >> SM_SHIFT);
1367 static __inline u_int64_t
1368 seg_y2x(u_int64_t y, u_int64_t ism)
1374 else if (ism == HT_INFINITY)
1377 x = (y >> ISM_SHIFT) * ism
1378 + (((y & ISM_MASK) * ism) >> ISM_SHIFT);
1383 static __inline u_int64_t
1388 sm = (m << SM_SHIFT) / 8 / machclk_freq;
1392 static __inline u_int64_t
1400 ism = ((u_int64_t)machclk_freq << ISM_SHIFT) * 8 / m;
1404 static __inline u_int64_t
1409 dx = ((u_int64_t)d * machclk_freq) / 1000;
1418 m = (sm * 8 * machclk_freq) >> SM_SHIFT;
1427 d = dx * 1000 / machclk_freq;
1432 sc2isc(struct service_curve *sc, struct internal_sc *isc)
1434 isc->sm1 = m2sm(sc->m1);
1435 isc->ism1 = m2ism(sc->m1);
1436 isc->dx = d2dx(sc->d);
1437 isc->dy = seg_x2y(isc->dx, isc->sm1);
1438 isc->sm2 = m2sm(sc->m2);
1439 isc->ism2 = m2ism(sc->m2);
1443 * initialize the runtime service curve with the given internal
1444 * service curve starting at (x, y).
1447 rtsc_init(struct runtime_sc *rtsc, struct internal_sc * isc, u_int64_t x,
1452 rtsc->sm1 = isc->sm1;
1453 rtsc->ism1 = isc->ism1;
1456 rtsc->sm2 = isc->sm2;
1457 rtsc->ism2 = isc->ism2;
1461 * calculate the y-projection of the runtime service curve by the
1462 * given x-projection value
1465 rtsc_y2x(struct runtime_sc *rtsc, u_int64_t y)
1471 else if (y <= rtsc->y + rtsc->dy) {
1472 /* x belongs to the 1st segment */
1474 x = rtsc->x + rtsc->dx;
1476 x = rtsc->x + seg_y2x(y - rtsc->y, rtsc->ism1);
1478 /* x belongs to the 2nd segment */
1479 x = rtsc->x + rtsc->dx
1480 + seg_y2x(y - rtsc->y - rtsc->dy, rtsc->ism2);
1486 rtsc_x2y(struct runtime_sc *rtsc, u_int64_t x)
1492 else if (x <= rtsc->x + rtsc->dx)
1493 /* y belongs to the 1st segment */
1494 y = rtsc->y + seg_x2y(x - rtsc->x, rtsc->sm1);
1496 /* y belongs to the 2nd segment */
1497 y = rtsc->y + rtsc->dy
1498 + seg_x2y(x - rtsc->x - rtsc->dx, rtsc->sm2);
1503 * update the runtime service curve by taking the minimum of the current
1504 * runtime service curve and the service curve starting at (x, y).
1507 rtsc_min(struct runtime_sc *rtsc, struct internal_sc *isc, u_int64_t x,
1510 u_int64_t y1, y2, dx, dy;
1512 if (isc->sm1 <= isc->sm2) {
1513 /* service curve is convex */
1514 y1 = rtsc_x2y(rtsc, x);
1516 /* the current rtsc is smaller */
1524 * service curve is concave
1525 * compute the two y values of the current rtsc
1529 y1 = rtsc_x2y(rtsc, x);
1531 /* rtsc is below isc, no change to rtsc */
1535 y2 = rtsc_x2y(rtsc, x + isc->dx);
1536 if (y2 >= y + isc->dy) {
1537 /* rtsc is above isc, replace rtsc by isc */
1546 * the two curves intersect
1547 * compute the offsets (dx, dy) using the reverse
1548 * function of seg_x2y()
1549 * seg_x2y(dx, sm1) == seg_x2y(dx, sm2) + (y1 - y)
1551 dx = ((y1 - y) << SM_SHIFT) / (isc->sm1 - isc->sm2);
1553 * check if (x, y1) belongs to the 1st segment of rtsc.
1554 * if so, add the offset.
1556 if (rtsc->x + rtsc->dx > x)
1557 dx += rtsc->x + rtsc->dx - x;
1558 dy = seg_x2y(dx, isc->sm1);
1568 get_class_stats_v0(struct hfsc_classstats_v0 *sp, struct hfsc_class *cl)
1570 sp->class_id = cl->cl_id;
1571 sp->class_handle = cl->cl_handle;
1573 #define SATU32(x) (u_int32_t)uqmin((x), UINT_MAX)
1575 if (cl->cl_rsc != NULL) {
1576 sp->rsc.m1 = SATU32(sm2m(cl->cl_rsc->sm1));
1577 sp->rsc.d = dx2d(cl->cl_rsc->dx);
1578 sp->rsc.m2 = SATU32(sm2m(cl->cl_rsc->sm2));
1584 if (cl->cl_fsc != NULL) {
1585 sp->fsc.m1 = SATU32(sm2m(cl->cl_fsc->sm1));
1586 sp->fsc.d = dx2d(cl->cl_fsc->dx);
1587 sp->fsc.m2 = SATU32(sm2m(cl->cl_fsc->sm2));
1593 if (cl->cl_usc != NULL) {
1594 sp->usc.m1 = SATU32(sm2m(cl->cl_usc->sm1));
1595 sp->usc.d = dx2d(cl->cl_usc->dx);
1596 sp->usc.m2 = SATU32(sm2m(cl->cl_usc->sm2));
1605 sp->total = cl->cl_total;
1606 sp->cumul = cl->cl_cumul;
1613 sp->initvt = cl->cl_initvt;
1614 sp->vtperiod = cl->cl_vtperiod;
1615 sp->parentperiod = cl->cl_parentperiod;
1616 sp->nactive = cl->cl_nactive;
1617 sp->vtoff = cl->cl_vtoff;
1618 sp->cvtmax = cl->cl_cvtmax;
1619 sp->myf = cl->cl_myf;
1620 sp->cfmin = cl->cl_cfmin;
1621 sp->cvtmin = cl->cl_cvtmin;
1622 sp->myfadj = cl->cl_myfadj;
1623 sp->vtadj = cl->cl_vtadj;
1625 sp->cur_time = read_machclk();
1626 sp->machclk_freq = machclk_freq;
1628 sp->qlength = qlen(cl->cl_q);
1629 sp->qlimit = qlimit(cl->cl_q);
1630 sp->xmit_cnt = cl->cl_stats.xmit_cnt;
1631 sp->drop_cnt = cl->cl_stats.drop_cnt;
1632 sp->period = cl->cl_stats.period;
1634 sp->qtype = qtype(cl->cl_q);
1636 if (q_is_red(cl->cl_q))
1637 red_getstats(cl->cl_red, &sp->red[0]);
1640 if (q_is_rio(cl->cl_q))
1641 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
1644 if (q_is_codel(cl->cl_q))
1645 codel_getstats(cl->cl_codel, &sp->codel);
1650 get_class_stats_v1(struct hfsc_classstats_v1 *sp, struct hfsc_class *cl)
1652 sp->class_id = cl->cl_id;
1653 sp->class_handle = cl->cl_handle;
1655 if (cl->cl_rsc != NULL) {
1656 sp->rsc.m1 = sm2m(cl->cl_rsc->sm1);
1657 sp->rsc.d = dx2d(cl->cl_rsc->dx);
1658 sp->rsc.m2 = sm2m(cl->cl_rsc->sm2);
1664 if (cl->cl_fsc != NULL) {
1665 sp->fsc.m1 = sm2m(cl->cl_fsc->sm1);
1666 sp->fsc.d = dx2d(cl->cl_fsc->dx);
1667 sp->fsc.m2 = sm2m(cl->cl_fsc->sm2);
1673 if (cl->cl_usc != NULL) {
1674 sp->usc.m1 = sm2m(cl->cl_usc->sm1);
1675 sp->usc.d = dx2d(cl->cl_usc->dx);
1676 sp->usc.m2 = sm2m(cl->cl_usc->sm2);
1683 sp->total = cl->cl_total;
1684 sp->cumul = cl->cl_cumul;
1691 sp->initvt = cl->cl_initvt;
1692 sp->vtperiod = cl->cl_vtperiod;
1693 sp->parentperiod = cl->cl_parentperiod;
1694 sp->nactive = cl->cl_nactive;
1695 sp->vtoff = cl->cl_vtoff;
1696 sp->cvtmax = cl->cl_cvtmax;
1697 sp->myf = cl->cl_myf;
1698 sp->cfmin = cl->cl_cfmin;
1699 sp->cvtmin = cl->cl_cvtmin;
1700 sp->myfadj = cl->cl_myfadj;
1701 sp->vtadj = cl->cl_vtadj;
1703 sp->cur_time = read_machclk();
1704 sp->machclk_freq = machclk_freq;
1706 sp->qlength = qlen(cl->cl_q);
1707 sp->qlimit = qlimit(cl->cl_q);
1708 sp->xmit_cnt = cl->cl_stats.xmit_cnt;
1709 sp->drop_cnt = cl->cl_stats.drop_cnt;
1710 sp->period = cl->cl_stats.period;
1712 sp->qtype = qtype(cl->cl_q);
1714 if (q_is_red(cl->cl_q))
1715 red_getstats(cl->cl_red, &sp->red[0]);
1718 if (q_is_rio(cl->cl_q))
1719 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
1722 if (q_is_codel(cl->cl_q))
1723 codel_getstats(cl->cl_codel, &sp->codel);
1727 /* convert a class handle to the corresponding class pointer */
1728 static struct hfsc_class *
1729 clh_to_clp(struct hfsc_if *hif, u_int32_t chandle)
1732 struct hfsc_class *cl;
1737 * first, try optimistically the slot matching the lower bits of
1738 * the handle. if it fails, do the linear table search.
1740 i = chandle % HFSC_MAX_CLASSES;
1741 if ((cl = hif->hif_class_tbl[i]) != NULL && cl->cl_handle == chandle)
1743 for (i = 0; i < HFSC_MAX_CLASSES; i++)
1744 if ((cl = hif->hif_class_tbl[i]) != NULL &&
1745 cl->cl_handle == chandle)
1751 #endif /* ALTQ_HFSC */