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);
139 #define is_a_parent_class(cl) ((cl)->cl_children != NULL)
141 #define HT_INFINITY 0xffffffffffffffffULL /* infinite time value */
144 hfsc_pfattach(struct pf_altq *a)
149 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
152 error = altq_attach(&ifp->if_snd, ALTQT_HFSC, a->altq_disc,
153 hfsc_enqueue, hfsc_dequeue, hfsc_request, NULL, NULL);
159 hfsc_add_altq(struct ifnet *ifp, struct pf_altq *a)
165 if (!ALTQ_IS_READY(&ifp->if_snd))
168 hif = malloc(sizeof(struct hfsc_if), M_DEVBUF, M_NOWAIT | M_ZERO);
172 TAILQ_INIT(&hif->hif_eligible);
173 hif->hif_ifq = &ifp->if_snd;
175 /* keep the state in pf_altq */
182 hfsc_remove_altq(struct pf_altq *a)
186 if ((hif = a->altq_disc) == NULL)
190 (void)hfsc_clear_interface(hif);
191 (void)hfsc_class_destroy(hif->hif_rootclass);
199 hfsc_add_queue(struct pf_altq *a)
202 struct hfsc_class *cl, *parent;
203 struct hfsc_opts_v1 *opts;
204 struct service_curve rtsc, lssc, ulsc;
206 if ((hif = a->altq_disc) == NULL)
209 opts = &a->pq_u.hfsc_opts;
211 if (a->parent_qid == HFSC_NULLCLASS_HANDLE &&
212 hif->hif_rootclass == NULL)
214 else if ((parent = clh_to_clp(hif, a->parent_qid)) == NULL)
220 if (clh_to_clp(hif, a->qid) != NULL)
223 rtsc.m1 = opts->rtsc_m1;
224 rtsc.d = opts->rtsc_d;
225 rtsc.m2 = opts->rtsc_m2;
226 lssc.m1 = opts->lssc_m1;
227 lssc.d = opts->lssc_d;
228 lssc.m2 = opts->lssc_m2;
229 ulsc.m1 = opts->ulsc_m1;
230 ulsc.d = opts->ulsc_d;
231 ulsc.m2 = opts->ulsc_m2;
233 cl = hfsc_class_create(hif, &rtsc, &lssc, &ulsc,
234 parent, a->qlimit, opts->flags, a->qid);
242 hfsc_remove_queue(struct pf_altq *a)
245 struct hfsc_class *cl;
247 if ((hif = a->altq_disc) == NULL)
250 if ((cl = clh_to_clp(hif, a->qid)) == NULL)
253 return (hfsc_class_destroy(cl));
257 hfsc_getqstats(struct pf_altq *a, void *ubuf, int *nbytes, int version)
260 struct hfsc_class *cl;
262 struct hfsc_classstats_v0 v0;
263 struct hfsc_classstats_v1 v1;
268 if ((hif = altq_lookup(a->ifname, ALTQT_HFSC)) == NULL)
271 if ((cl = clh_to_clp(hif, a->qid)) == NULL)
274 if (version > HFSC_STATS_VERSION)
277 memset(&stats, 0, sizeof(stats));
280 get_class_stats_v0(&stats.v0, cl);
281 stats_size = sizeof(struct hfsc_classstats_v0);
284 get_class_stats_v1(&stats.v1, cl);
285 stats_size = sizeof(struct hfsc_classstats_v1);
289 if (*nbytes < stats_size)
292 if ((error = copyout((caddr_t)&stats, ubuf, stats_size)) != 0)
294 *nbytes = stats_size;
299 * bring the interface back to the initial state by discarding
300 * all the filters and classes except the root class.
303 hfsc_clear_interface(struct hfsc_if *hif)
305 struct hfsc_class *cl;
307 /* clear out the classes */
308 while (hif->hif_rootclass != NULL &&
309 (cl = hif->hif_rootclass->cl_children) != NULL) {
311 * remove the first leaf class found in the hierarchy
314 for (; cl != NULL; cl = hfsc_nextclass(cl)) {
315 if (!is_a_parent_class(cl)) {
316 (void)hfsc_class_destroy(cl);
326 hfsc_request(struct ifaltq *ifq, int req, void *arg)
328 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
330 IFQ_LOCK_ASSERT(ifq);
340 /* discard all the queued packets on the interface */
342 hfsc_purge(struct hfsc_if *hif)
344 struct hfsc_class *cl;
346 for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl))
347 if (!qempty(cl->cl_q))
349 if (ALTQ_IS_ENABLED(hif->hif_ifq))
350 hif->hif_ifq->ifq_len = 0;
354 hfsc_class_create(struct hfsc_if *hif, struct service_curve *rsc,
355 struct service_curve *fsc, struct service_curve *usc,
356 struct hfsc_class *parent, int qlimit, int flags, int qid)
358 struct hfsc_class *cl, *p;
361 if (hif->hif_classes >= HFSC_MAX_CLASSES)
365 if (flags & HFCF_RED) {
367 printf("hfsc_class_create: RED not configured for HFSC!\n");
373 if (flags & HFCF_CODEL) {
375 printf("hfsc_class_create: CODEL not configured for HFSC!\n");
381 cl = malloc(sizeof(struct hfsc_class), M_DEVBUF, M_NOWAIT | M_ZERO);
385 cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF, M_NOWAIT | M_ZERO);
386 if (cl->cl_q == NULL)
389 TAILQ_INIT(&cl->cl_actc);
392 qlimit = 50; /* use default */
393 qlimit(cl->cl_q) = qlimit;
394 qtype(cl->cl_q) = Q_DROPTAIL;
397 cl->cl_flags = flags;
399 if (flags & (HFCF_RED|HFCF_RIO)) {
400 int red_flags, red_pkttime;
404 if (rsc != NULL && rsc->m2 > m2)
406 if (fsc != NULL && fsc->m2 > m2)
408 if (usc != NULL && usc->m2 > m2)
412 if (flags & HFCF_ECN)
413 red_flags |= REDF_ECN;
415 if (flags & HFCF_CLEARDSCP)
416 red_flags |= RIOF_CLEARDSCP;
419 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
421 red_pkttime = (int64_t)hif->hif_ifq->altq_ifp->if_mtu
422 * 1000 * 1000 * 1000 / (m2 / 8);
423 if (flags & HFCF_RED) {
424 cl->cl_red = red_alloc(0, 0,
425 qlimit(cl->cl_q) * 10/100,
426 qlimit(cl->cl_q) * 30/100,
427 red_flags, red_pkttime);
428 if (cl->cl_red != NULL)
429 qtype(cl->cl_q) = Q_RED;
433 cl->cl_red = (red_t *)rio_alloc(0, NULL,
434 red_flags, red_pkttime);
435 if (cl->cl_red != NULL)
436 qtype(cl->cl_q) = Q_RIO;
440 #endif /* ALTQ_RED */
442 if (flags & HFCF_CODEL) {
443 cl->cl_codel = codel_alloc(5, 100, 0);
444 if (cl->cl_codel != NULL)
445 qtype(cl->cl_q) = Q_CODEL;
449 if (rsc != NULL && (rsc->m1 != 0 || rsc->m2 != 0)) {
450 cl->cl_rsc = malloc(sizeof(struct internal_sc),
452 if (cl->cl_rsc == NULL)
454 sc2isc(rsc, cl->cl_rsc);
455 rtsc_init(&cl->cl_deadline, cl->cl_rsc, 0, 0);
456 rtsc_init(&cl->cl_eligible, cl->cl_rsc, 0, 0);
458 if (fsc != NULL && (fsc->m1 != 0 || fsc->m2 != 0)) {
459 cl->cl_fsc = malloc(sizeof(struct internal_sc),
461 if (cl->cl_fsc == NULL)
463 sc2isc(fsc, cl->cl_fsc);
464 rtsc_init(&cl->cl_virtual, cl->cl_fsc, 0, 0);
466 if (usc != NULL && (usc->m1 != 0 || usc->m2 != 0)) {
467 cl->cl_usc = malloc(sizeof(struct internal_sc),
469 if (cl->cl_usc == NULL)
471 sc2isc(usc, cl->cl_usc);
472 rtsc_init(&cl->cl_ulimit, cl->cl_usc, 0, 0);
475 cl->cl_id = hif->hif_classid++;
478 cl->cl_parent = parent;
481 IFQ_LOCK(hif->hif_ifq);
485 * find a free slot in the class table. if the slot matching
486 * the lower bits of qid is free, use this slot. otherwise,
487 * use the first free slot.
489 i = qid % HFSC_MAX_CLASSES;
490 if (hif->hif_class_tbl[i] == NULL)
491 hif->hif_class_tbl[i] = cl;
493 for (i = 0; i < HFSC_MAX_CLASSES; i++)
494 if (hif->hif_class_tbl[i] == NULL) {
495 hif->hif_class_tbl[i] = cl;
498 if (i == HFSC_MAX_CLASSES) {
499 IFQ_UNLOCK(hif->hif_ifq);
506 if (flags & HFCF_DEFAULTCLASS)
507 hif->hif_defaultclass = cl;
509 if (parent == NULL) {
510 /* this is root class */
511 hif->hif_rootclass = cl;
513 /* add this class to the children list of the parent */
514 if ((p = parent->cl_children) == NULL)
515 parent->cl_children = cl;
517 while (p->cl_siblings != NULL)
522 IFQ_UNLOCK(hif->hif_ifq);
528 if (cl->cl_red != NULL) {
530 if (q_is_rio(cl->cl_q))
531 rio_destroy((rio_t *)cl->cl_red);
534 if (q_is_red(cl->cl_q))
535 red_destroy(cl->cl_red);
538 if (q_is_codel(cl->cl_q))
539 codel_destroy(cl->cl_codel);
542 if (cl->cl_fsc != NULL)
543 free(cl->cl_fsc, M_DEVBUF);
544 if (cl->cl_rsc != NULL)
545 free(cl->cl_rsc, M_DEVBUF);
546 if (cl->cl_usc != NULL)
547 free(cl->cl_usc, M_DEVBUF);
548 if (cl->cl_q != NULL)
549 free(cl->cl_q, M_DEVBUF);
555 hfsc_class_destroy(struct hfsc_class *cl)
562 if (is_a_parent_class(cl))
566 IFQ_LOCK(cl->cl_hif->hif_ifq);
568 if (!qempty(cl->cl_q))
571 if (cl->cl_parent == NULL) {
572 /* this is root class */
574 struct hfsc_class *p = cl->cl_parent->cl_children;
577 cl->cl_parent->cl_children = cl->cl_siblings;
579 if (p->cl_siblings == cl) {
580 p->cl_siblings = cl->cl_siblings;
583 } while ((p = p->cl_siblings) != NULL);
587 cl->cl_hif->hif_class_tbl[cl->cl_slot] = NULL;
588 cl->cl_hif->hif_classes--;
589 IFQ_UNLOCK(cl->cl_hif->hif_ifq);
592 if (cl->cl_red != NULL) {
594 if (q_is_rio(cl->cl_q))
595 rio_destroy((rio_t *)cl->cl_red);
598 if (q_is_red(cl->cl_q))
599 red_destroy(cl->cl_red);
602 if (q_is_codel(cl->cl_q))
603 codel_destroy(cl->cl_codel);
607 IFQ_LOCK(cl->cl_hif->hif_ifq);
608 if (cl == cl->cl_hif->hif_rootclass)
609 cl->cl_hif->hif_rootclass = NULL;
610 if (cl == cl->cl_hif->hif_defaultclass)
611 cl->cl_hif->hif_defaultclass = NULL;
612 IFQ_UNLOCK(cl->cl_hif->hif_ifq);
614 if (cl->cl_usc != NULL)
615 free(cl->cl_usc, M_DEVBUF);
616 if (cl->cl_fsc != NULL)
617 free(cl->cl_fsc, M_DEVBUF);
618 if (cl->cl_rsc != NULL)
619 free(cl->cl_rsc, M_DEVBUF);
620 free(cl->cl_q, M_DEVBUF);
627 * hfsc_nextclass returns the next class in the tree.
629 * for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl))
632 static struct hfsc_class *
633 hfsc_nextclass(struct hfsc_class *cl)
635 if (cl->cl_children != NULL)
636 cl = cl->cl_children;
637 else if (cl->cl_siblings != NULL)
638 cl = cl->cl_siblings;
640 while ((cl = cl->cl_parent) != NULL)
641 if (cl->cl_siblings) {
642 cl = cl->cl_siblings;
651 * hfsc_enqueue is an enqueue function to be registered to
652 * (*altq_enqueue) in struct ifaltq.
655 hfsc_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
657 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
658 struct hfsc_class *cl;
662 IFQ_LOCK_ASSERT(ifq);
664 /* grab class set by classifier */
665 if ((m->m_flags & M_PKTHDR) == 0) {
666 /* should not happen */
667 printf("altq: packet for %s does not have pkthdr\n",
668 ifq->altq_ifp->if_xname);
673 if ((t = pf_find_mtag(m)) != NULL)
674 cl = clh_to_clp(hif, t->qid);
675 if (cl == NULL || is_a_parent_class(cl)) {
676 cl = hif->hif_defaultclass;
682 cl->cl_pktattr = NULL;
684 if (hfsc_addq(cl, m) != 0) {
685 /* drop occurred. mbuf was freed in hfsc_addq. */
686 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, len);
690 cl->cl_hif->hif_packets++;
692 /* successfully queued. */
693 if (qlen(cl->cl_q) == 1)
694 set_active(cl, m_pktlen(m));
700 * hfsc_dequeue is a dequeue function to be registered to
701 * (*altq_dequeue) in struct ifaltq.
703 * note: ALTDQ_POLL returns the next packet without removing the packet
704 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
705 * ALTDQ_REMOVE must return the same packet if called immediately
709 hfsc_dequeue(struct ifaltq *ifq, int op)
711 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
712 struct hfsc_class *cl;
718 IFQ_LOCK_ASSERT(ifq);
720 if (hif->hif_packets == 0)
721 /* no packet in the tree */
724 cur_time = read_machclk();
726 if (op == ALTDQ_REMOVE && hif->hif_pollcache != NULL) {
727 cl = hif->hif_pollcache;
728 hif->hif_pollcache = NULL;
729 /* check if the class was scheduled by real-time criteria */
730 if (cl->cl_rsc != NULL)
731 realtime = (cl->cl_e <= cur_time);
734 * if there are eligible classes, use real-time criteria.
735 * find the class with the minimum deadline among
736 * the eligible classes.
738 if ((cl = hfsc_get_mindl(hif, cur_time))
746 * use link-sharing criteria
747 * get the class with the minimum vt in the hierarchy
749 cl = hif->hif_rootclass;
750 while (is_a_parent_class(cl)) {
751 cl = actlist_firstfit(cl, cur_time);
755 printf("%d fit but none found\n",fits);
760 * update parent's cl_cvtmin.
761 * don't update if the new vt is smaller.
763 if (cl->cl_parent->cl_cvtmin < cl->cl_vt)
764 cl->cl_parent->cl_cvtmin = cl->cl_vt;
771 if (op == ALTDQ_POLL) {
772 hif->hif_pollcache = cl;
780 panic("hfsc_dequeue:");
782 cl->cl_hif->hif_packets--;
784 PKTCNTR_ADD(&cl->cl_stats.xmit_cnt, len);
786 update_vf(cl, len, cur_time);
790 if (!qempty(cl->cl_q)) {
791 if (cl->cl_rsc != NULL) {
793 next_len = m_pktlen(qhead(cl->cl_q));
796 update_ed(cl, next_len);
798 update_d(cl, next_len);
801 /* the class becomes passive */
809 hfsc_addq(struct hfsc_class *cl, struct mbuf *m)
813 if (q_is_rio(cl->cl_q))
814 return rio_addq((rio_t *)cl->cl_red, cl->cl_q,
818 if (q_is_red(cl->cl_q))
819 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
822 if (q_is_codel(cl->cl_q))
823 return codel_addq(cl->cl_codel, cl->cl_q, m);
825 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
830 if (cl->cl_flags & HFCF_CLEARDSCP)
831 write_dsfield(m, cl->cl_pktattr, 0);
839 hfsc_getq(struct hfsc_class *cl)
842 if (q_is_rio(cl->cl_q))
843 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
846 if (q_is_red(cl->cl_q))
847 return red_getq(cl->cl_red, cl->cl_q);
850 if (q_is_codel(cl->cl_q))
851 return codel_getq(cl->cl_codel, cl->cl_q);
853 return _getq(cl->cl_q);
857 hfsc_pollq(struct hfsc_class *cl)
859 return qhead(cl->cl_q);
863 hfsc_purgeq(struct hfsc_class *cl)
867 if (qempty(cl->cl_q))
870 while ((m = _getq(cl->cl_q)) != NULL) {
871 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, m_pktlen(m));
873 cl->cl_hif->hif_packets--;
874 IFQ_DEC_LEN(cl->cl_hif->hif_ifq);
876 ASSERT(qlen(cl->cl_q) == 0);
878 update_vf(cl, 0, 0); /* remove cl from the actlist */
883 set_active(struct hfsc_class *cl, int len)
885 if (cl->cl_rsc != NULL)
887 if (cl->cl_fsc != NULL)
890 cl->cl_stats.period++;
894 set_passive(struct hfsc_class *cl)
896 if (cl->cl_rsc != NULL)
900 * actlist is now handled in update_vf() so that update_vf(cl, 0, 0)
901 * needs to be called explicitly to remove a class from actlist
906 init_ed(struct hfsc_class *cl, int next_len)
910 cur_time = read_machclk();
912 /* update the deadline curve */
913 rtsc_min(&cl->cl_deadline, cl->cl_rsc, cur_time, cl->cl_cumul);
916 * update the eligible curve.
917 * for concave, it is equal to the deadline curve.
918 * for convex, it is a linear curve with slope m2.
920 cl->cl_eligible = cl->cl_deadline;
921 if (cl->cl_rsc->sm1 <= cl->cl_rsc->sm2) {
922 cl->cl_eligible.dx = 0;
923 cl->cl_eligible.dy = 0;
926 /* compute e and d */
927 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
928 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
934 update_ed(struct hfsc_class *cl, int next_len)
936 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
937 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
943 update_d(struct hfsc_class *cl, int next_len)
945 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
949 init_vf(struct hfsc_class *cl, int len)
951 struct hfsc_class *max_cl, *p;
952 u_int64_t vt, f, cur_time;
957 for ( ; cl->cl_parent != NULL; cl = cl->cl_parent) {
958 if (go_active && cl->cl_nactive++ == 0)
964 max_cl = TAILQ_LAST(&cl->cl_parent->cl_actc, acthead);
965 if (max_cl != NULL) {
967 * set vt to the average of the min and max
968 * classes. if the parent's period didn't
969 * change, don't decrease vt of the class.
972 if (cl->cl_parent->cl_cvtmin != 0)
973 vt = (cl->cl_parent->cl_cvtmin + vt)/2;
975 if (cl->cl_parent->cl_vtperiod !=
976 cl->cl_parentperiod || vt > cl->cl_vt)
980 * first child for a new parent backlog period.
981 * add parent's cvtmax to vtoff of children
982 * to make a new vt (vtoff + vt) larger than
983 * the vt in the last period for all children.
985 vt = cl->cl_parent->cl_cvtmax;
986 for (p = cl->cl_parent->cl_children; p != NULL;
990 cl->cl_parent->cl_cvtmax = 0;
991 cl->cl_parent->cl_cvtmin = 0;
993 cl->cl_initvt = cl->cl_vt;
995 /* update the virtual curve */
996 vt = cl->cl_vt + cl->cl_vtoff;
997 rtsc_min(&cl->cl_virtual, cl->cl_fsc, vt, cl->cl_total);
998 if (cl->cl_virtual.x == vt) {
999 cl->cl_virtual.x -= cl->cl_vtoff;
1004 cl->cl_vtperiod++; /* increment vt period */
1005 cl->cl_parentperiod = cl->cl_parent->cl_vtperiod;
1006 if (cl->cl_parent->cl_nactive == 0)
1007 cl->cl_parentperiod++;
1012 if (cl->cl_usc != NULL) {
1013 /* class has upper limit curve */
1015 cur_time = read_machclk();
1017 /* update the ulimit curve */
1018 rtsc_min(&cl->cl_ulimit, cl->cl_usc, cur_time,
1021 cl->cl_myf = rtsc_y2x(&cl->cl_ulimit,
1027 if (cl->cl_myf > cl->cl_cfmin)
1031 if (f != cl->cl_f) {
1033 update_cfmin(cl->cl_parent);
1039 update_vf(struct hfsc_class *cl, int len, u_int64_t cur_time)
1041 u_int64_t f, myf_bound, delta;
1044 go_passive = qempty(cl->cl_q);
1046 for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
1047 cl->cl_total += len;
1049 if (cl->cl_fsc == NULL || cl->cl_nactive == 0)
1052 if (go_passive && --cl->cl_nactive == 0)
1058 /* no more active child, going passive */
1060 /* update cvtmax of the parent class */
1061 if (cl->cl_vt > cl->cl_parent->cl_cvtmax)
1062 cl->cl_parent->cl_cvtmax = cl->cl_vt;
1064 /* remove this class from the vt list */
1067 update_cfmin(cl->cl_parent);
1075 cl->cl_vt = rtsc_y2x(&cl->cl_virtual, cl->cl_total)
1076 - cl->cl_vtoff + cl->cl_vtadj;
1079 * if vt of the class is smaller than cvtmin,
1080 * the class was skipped in the past due to non-fit.
1081 * if so, we need to adjust vtadj.
1083 if (cl->cl_vt < cl->cl_parent->cl_cvtmin) {
1084 cl->cl_vtadj += cl->cl_parent->cl_cvtmin - cl->cl_vt;
1085 cl->cl_vt = cl->cl_parent->cl_cvtmin;
1088 /* update the vt list */
1091 if (cl->cl_usc != NULL) {
1092 cl->cl_myf = cl->cl_myfadj
1093 + rtsc_y2x(&cl->cl_ulimit, cl->cl_total);
1096 * if myf lags behind by more than one clock tick
1097 * from the current time, adjust myfadj to prevent
1098 * a rate-limited class from going greedy.
1099 * in a steady state under rate-limiting, myf
1100 * fluctuates within one clock tick.
1102 myf_bound = cur_time - machclk_per_tick;
1103 if (cl->cl_myf < myf_bound) {
1104 delta = cur_time - cl->cl_myf;
1105 cl->cl_myfadj += delta;
1106 cl->cl_myf += delta;
1110 /* cl_f is max(cl_myf, cl_cfmin) */
1111 if (cl->cl_myf > cl->cl_cfmin)
1115 if (f != cl->cl_f) {
1117 update_cfmin(cl->cl_parent);
1123 update_cfmin(struct hfsc_class *cl)
1125 struct hfsc_class *p;
1128 if (TAILQ_EMPTY(&cl->cl_actc)) {
1132 cfmin = HT_INFINITY;
1133 TAILQ_FOREACH(p, &cl->cl_actc, cl_actlist) {
1138 if (p->cl_f < cfmin)
1141 cl->cl_cfmin = cfmin;
1145 * TAILQ based ellist and actlist implementation
1146 * (ion wanted to make a calendar queue based implementation)
1149 * eligible list holds backlogged classes being sorted by their eligible times.
1150 * there is one eligible list per interface.
1154 ellist_insert(struct hfsc_class *cl)
1156 struct hfsc_if *hif = cl->cl_hif;
1157 struct hfsc_class *p;
1159 /* check the last entry first */
1160 if ((p = TAILQ_LAST(&hif->hif_eligible, elighead)) == NULL ||
1161 p->cl_e <= cl->cl_e) {
1162 TAILQ_INSERT_TAIL(&hif->hif_eligible, cl, cl_ellist);
1166 TAILQ_FOREACH(p, &hif->hif_eligible, cl_ellist) {
1167 if (cl->cl_e < p->cl_e) {
1168 TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1172 ASSERT(0); /* should not reach here */
1176 ellist_remove(struct hfsc_class *cl)
1178 struct hfsc_if *hif = cl->cl_hif;
1180 TAILQ_REMOVE(&hif->hif_eligible, cl, cl_ellist);
1184 ellist_update(struct hfsc_class *cl)
1186 struct hfsc_if *hif = cl->cl_hif;
1187 struct hfsc_class *p, *last;
1190 * the eligible time of a class increases monotonically.
1191 * if the next entry has a larger eligible time, nothing to do.
1193 p = TAILQ_NEXT(cl, cl_ellist);
1194 if (p == NULL || cl->cl_e <= p->cl_e)
1197 /* check the last entry */
1198 last = TAILQ_LAST(&hif->hif_eligible, elighead);
1199 ASSERT(last != NULL);
1200 if (last->cl_e <= cl->cl_e) {
1201 TAILQ_REMOVE(&hif->hif_eligible, cl, cl_ellist);
1202 TAILQ_INSERT_TAIL(&hif->hif_eligible, cl, cl_ellist);
1207 * the new position must be between the next entry
1208 * and the last entry
1210 while ((p = TAILQ_NEXT(p, cl_ellist)) != NULL) {
1211 if (cl->cl_e < p->cl_e) {
1212 TAILQ_REMOVE(&hif->hif_eligible, cl, cl_ellist);
1213 TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1217 ASSERT(0); /* should not reach here */
1220 /* find the class with the minimum deadline among the eligible classes */
1222 hfsc_get_mindl(struct hfsc_if *hif, u_int64_t cur_time)
1224 struct hfsc_class *p, *cl = NULL;
1226 TAILQ_FOREACH(p, &hif->hif_eligible, cl_ellist) {
1227 if (p->cl_e > cur_time)
1229 if (cl == NULL || p->cl_d < cl->cl_d)
1236 * active children list holds backlogged child classes being sorted
1237 * by their virtual time.
1238 * each intermediate class has one active children list.
1242 actlist_insert(struct hfsc_class *cl)
1244 struct hfsc_class *p;
1246 /* check the last entry first */
1247 if ((p = TAILQ_LAST(&cl->cl_parent->cl_actc, acthead)) == NULL
1248 || p->cl_vt <= cl->cl_vt) {
1249 TAILQ_INSERT_TAIL(&cl->cl_parent->cl_actc, cl, cl_actlist);
1253 TAILQ_FOREACH(p, &cl->cl_parent->cl_actc, cl_actlist) {
1254 if (cl->cl_vt < p->cl_vt) {
1255 TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1259 ASSERT(0); /* should not reach here */
1263 actlist_remove(struct hfsc_class *cl)
1265 TAILQ_REMOVE(&cl->cl_parent->cl_actc, cl, cl_actlist);
1269 actlist_update(struct hfsc_class *cl)
1271 struct hfsc_class *p, *last;
1274 * the virtual time of a class increases monotonically during its
1275 * backlogged period.
1276 * if the next entry has a larger virtual time, nothing to do.
1278 p = TAILQ_NEXT(cl, cl_actlist);
1279 if (p == NULL || cl->cl_vt < p->cl_vt)
1282 /* check the last entry */
1283 last = TAILQ_LAST(&cl->cl_parent->cl_actc, acthead);
1284 ASSERT(last != NULL);
1285 if (last->cl_vt <= cl->cl_vt) {
1286 TAILQ_REMOVE(&cl->cl_parent->cl_actc, cl, cl_actlist);
1287 TAILQ_INSERT_TAIL(&cl->cl_parent->cl_actc, cl, cl_actlist);
1292 * the new position must be between the next entry
1293 * and the last entry
1295 while ((p = TAILQ_NEXT(p, cl_actlist)) != NULL) {
1296 if (cl->cl_vt < p->cl_vt) {
1297 TAILQ_REMOVE(&cl->cl_parent->cl_actc, cl, cl_actlist);
1298 TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1302 ASSERT(0); /* should not reach here */
1305 static struct hfsc_class *
1306 actlist_firstfit(struct hfsc_class *cl, u_int64_t cur_time)
1308 struct hfsc_class *p;
1310 TAILQ_FOREACH(p, &cl->cl_actc, cl_actlist) {
1311 if (p->cl_f <= cur_time)
1318 * service curve support functions
1320 * external service curve parameters
1323 * internal service curve parameters
1324 * sm: (bytes/machclk tick) << SM_SHIFT
1325 * ism: (machclk ticks/byte) << ISM_SHIFT
1328 * SM_SHIFT and ISM_SHIFT are scaled in order to keep effective digits. we
1329 * should be able to handle 100K-100Gbps linkspeed with 256 MHz machclk
1330 * frequency and at least 3 effective digits in decimal.
1334 #define ISM_SHIFT 14
1336 #define SM_MASK ((1LL << SM_SHIFT) - 1)
1337 #define ISM_MASK ((1LL << ISM_SHIFT) - 1)
1339 static __inline u_int64_t
1340 seg_x2y(u_int64_t x, u_int64_t sm)
1346 * y = x * sm >> SM_SHIFT
1347 * but divide it for the upper and lower bits to avoid overflow
1349 y = (x >> SM_SHIFT) * sm + (((x & SM_MASK) * sm) >> SM_SHIFT);
1353 static __inline u_int64_t
1354 seg_y2x(u_int64_t y, u_int64_t ism)
1360 else if (ism == HT_INFINITY)
1363 x = (y >> ISM_SHIFT) * ism
1364 + (((y & ISM_MASK) * ism) >> ISM_SHIFT);
1369 static __inline u_int64_t
1374 sm = (m << SM_SHIFT) / 8 / machclk_freq;
1378 static __inline u_int64_t
1386 ism = ((u_int64_t)machclk_freq << ISM_SHIFT) * 8 / m;
1390 static __inline u_int64_t
1395 dx = ((u_int64_t)d * machclk_freq) / 1000;
1404 m = (sm * 8 * machclk_freq) >> SM_SHIFT;
1413 d = dx * 1000 / machclk_freq;
1418 sc2isc(struct service_curve *sc, struct internal_sc *isc)
1420 isc->sm1 = m2sm(sc->m1);
1421 isc->ism1 = m2ism(sc->m1);
1422 isc->dx = d2dx(sc->d);
1423 isc->dy = seg_x2y(isc->dx, isc->sm1);
1424 isc->sm2 = m2sm(sc->m2);
1425 isc->ism2 = m2ism(sc->m2);
1429 * initialize the runtime service curve with the given internal
1430 * service curve starting at (x, y).
1433 rtsc_init(struct runtime_sc *rtsc, struct internal_sc * isc, u_int64_t x,
1438 rtsc->sm1 = isc->sm1;
1439 rtsc->ism1 = isc->ism1;
1442 rtsc->sm2 = isc->sm2;
1443 rtsc->ism2 = isc->ism2;
1447 * calculate the y-projection of the runtime service curve by the
1448 * given x-projection value
1451 rtsc_y2x(struct runtime_sc *rtsc, u_int64_t y)
1457 else if (y <= rtsc->y + rtsc->dy) {
1458 /* x belongs to the 1st segment */
1460 x = rtsc->x + rtsc->dx;
1462 x = rtsc->x + seg_y2x(y - rtsc->y, rtsc->ism1);
1464 /* x belongs to the 2nd segment */
1465 x = rtsc->x + rtsc->dx
1466 + seg_y2x(y - rtsc->y - rtsc->dy, rtsc->ism2);
1472 rtsc_x2y(struct runtime_sc *rtsc, u_int64_t x)
1478 else if (x <= rtsc->x + rtsc->dx)
1479 /* y belongs to the 1st segment */
1480 y = rtsc->y + seg_x2y(x - rtsc->x, rtsc->sm1);
1482 /* y belongs to the 2nd segment */
1483 y = rtsc->y + rtsc->dy
1484 + seg_x2y(x - rtsc->x - rtsc->dx, rtsc->sm2);
1489 * update the runtime service curve by taking the minimum of the current
1490 * runtime service curve and the service curve starting at (x, y).
1493 rtsc_min(struct runtime_sc *rtsc, struct internal_sc *isc, u_int64_t x,
1496 u_int64_t y1, y2, dx, dy;
1498 if (isc->sm1 <= isc->sm2) {
1499 /* service curve is convex */
1500 y1 = rtsc_x2y(rtsc, x);
1502 /* the current rtsc is smaller */
1510 * service curve is concave
1511 * compute the two y values of the current rtsc
1515 y1 = rtsc_x2y(rtsc, x);
1517 /* rtsc is below isc, no change to rtsc */
1521 y2 = rtsc_x2y(rtsc, x + isc->dx);
1522 if (y2 >= y + isc->dy) {
1523 /* rtsc is above isc, replace rtsc by isc */
1532 * the two curves intersect
1533 * compute the offsets (dx, dy) using the reverse
1534 * function of seg_x2y()
1535 * seg_x2y(dx, sm1) == seg_x2y(dx, sm2) + (y1 - y)
1537 dx = ((y1 - y) << SM_SHIFT) / (isc->sm1 - isc->sm2);
1539 * check if (x, y1) belongs to the 1st segment of rtsc.
1540 * if so, add the offset.
1542 if (rtsc->x + rtsc->dx > x)
1543 dx += rtsc->x + rtsc->dx - x;
1544 dy = seg_x2y(dx, isc->sm1);
1554 get_class_stats_v0(struct hfsc_classstats_v0 *sp, struct hfsc_class *cl)
1556 sp->class_id = cl->cl_id;
1557 sp->class_handle = cl->cl_handle;
1559 #define SATU32(x) (u_int32_t)uqmin((x), UINT_MAX)
1561 if (cl->cl_rsc != NULL) {
1562 sp->rsc.m1 = SATU32(sm2m(cl->cl_rsc->sm1));
1563 sp->rsc.d = dx2d(cl->cl_rsc->dx);
1564 sp->rsc.m2 = SATU32(sm2m(cl->cl_rsc->sm2));
1570 if (cl->cl_fsc != NULL) {
1571 sp->fsc.m1 = SATU32(sm2m(cl->cl_fsc->sm1));
1572 sp->fsc.d = dx2d(cl->cl_fsc->dx);
1573 sp->fsc.m2 = SATU32(sm2m(cl->cl_fsc->sm2));
1579 if (cl->cl_usc != NULL) {
1580 sp->usc.m1 = SATU32(sm2m(cl->cl_usc->sm1));
1581 sp->usc.d = dx2d(cl->cl_usc->dx);
1582 sp->usc.m2 = SATU32(sm2m(cl->cl_usc->sm2));
1591 sp->total = cl->cl_total;
1592 sp->cumul = cl->cl_cumul;
1599 sp->initvt = cl->cl_initvt;
1600 sp->vtperiod = cl->cl_vtperiod;
1601 sp->parentperiod = cl->cl_parentperiod;
1602 sp->nactive = cl->cl_nactive;
1603 sp->vtoff = cl->cl_vtoff;
1604 sp->cvtmax = cl->cl_cvtmax;
1605 sp->myf = cl->cl_myf;
1606 sp->cfmin = cl->cl_cfmin;
1607 sp->cvtmin = cl->cl_cvtmin;
1608 sp->myfadj = cl->cl_myfadj;
1609 sp->vtadj = cl->cl_vtadj;
1611 sp->cur_time = read_machclk();
1612 sp->machclk_freq = machclk_freq;
1614 sp->qlength = qlen(cl->cl_q);
1615 sp->qlimit = qlimit(cl->cl_q);
1616 sp->xmit_cnt = cl->cl_stats.xmit_cnt;
1617 sp->drop_cnt = cl->cl_stats.drop_cnt;
1618 sp->period = cl->cl_stats.period;
1620 sp->qtype = qtype(cl->cl_q);
1622 if (q_is_red(cl->cl_q))
1623 red_getstats(cl->cl_red, &sp->red[0]);
1626 if (q_is_rio(cl->cl_q))
1627 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
1630 if (q_is_codel(cl->cl_q))
1631 codel_getstats(cl->cl_codel, &sp->codel);
1636 get_class_stats_v1(struct hfsc_classstats_v1 *sp, struct hfsc_class *cl)
1638 sp->class_id = cl->cl_id;
1639 sp->class_handle = cl->cl_handle;
1641 if (cl->cl_rsc != NULL) {
1642 sp->rsc.m1 = sm2m(cl->cl_rsc->sm1);
1643 sp->rsc.d = dx2d(cl->cl_rsc->dx);
1644 sp->rsc.m2 = sm2m(cl->cl_rsc->sm2);
1650 if (cl->cl_fsc != NULL) {
1651 sp->fsc.m1 = sm2m(cl->cl_fsc->sm1);
1652 sp->fsc.d = dx2d(cl->cl_fsc->dx);
1653 sp->fsc.m2 = sm2m(cl->cl_fsc->sm2);
1659 if (cl->cl_usc != NULL) {
1660 sp->usc.m1 = sm2m(cl->cl_usc->sm1);
1661 sp->usc.d = dx2d(cl->cl_usc->dx);
1662 sp->usc.m2 = sm2m(cl->cl_usc->sm2);
1669 sp->total = cl->cl_total;
1670 sp->cumul = cl->cl_cumul;
1677 sp->initvt = cl->cl_initvt;
1678 sp->vtperiod = cl->cl_vtperiod;
1679 sp->parentperiod = cl->cl_parentperiod;
1680 sp->nactive = cl->cl_nactive;
1681 sp->vtoff = cl->cl_vtoff;
1682 sp->cvtmax = cl->cl_cvtmax;
1683 sp->myf = cl->cl_myf;
1684 sp->cfmin = cl->cl_cfmin;
1685 sp->cvtmin = cl->cl_cvtmin;
1686 sp->myfadj = cl->cl_myfadj;
1687 sp->vtadj = cl->cl_vtadj;
1689 sp->cur_time = read_machclk();
1690 sp->machclk_freq = machclk_freq;
1692 sp->qlength = qlen(cl->cl_q);
1693 sp->qlimit = qlimit(cl->cl_q);
1694 sp->xmit_cnt = cl->cl_stats.xmit_cnt;
1695 sp->drop_cnt = cl->cl_stats.drop_cnt;
1696 sp->period = cl->cl_stats.period;
1698 sp->qtype = qtype(cl->cl_q);
1700 if (q_is_red(cl->cl_q))
1701 red_getstats(cl->cl_red, &sp->red[0]);
1704 if (q_is_rio(cl->cl_q))
1705 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
1708 if (q_is_codel(cl->cl_q))
1709 codel_getstats(cl->cl_codel, &sp->codel);
1713 /* convert a class handle to the corresponding class pointer */
1714 static struct hfsc_class *
1715 clh_to_clp(struct hfsc_if *hif, u_int32_t chandle)
1718 struct hfsc_class *cl;
1723 * first, try optimistically the slot matching the lower bits of
1724 * the handle. if it fails, do the linear table search.
1726 i = chandle % HFSC_MAX_CLASSES;
1727 if ((cl = hif->hif_class_tbl[i]) != NULL && cl->cl_handle == chandle)
1729 for (i = 0; i < HFSC_MAX_CLASSES; i++)
1730 if ((cl = hif->hif_class_tbl[i]) != NULL &&
1731 cl->cl_handle == chandle)
1736 #endif /* ALTQ_HFSC */