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 ifnet *ifp, struct pf_altq *a)
168 if (!ALTQ_IS_READY(&ifp->if_snd))
171 hif = malloc(sizeof(struct hfsc_if), M_DEVBUF, M_NOWAIT | M_ZERO);
175 TAILQ_INIT(&hif->hif_eligible);
176 hif->hif_ifq = &ifp->if_snd;
178 /* keep the state in pf_altq */
185 hfsc_remove_altq(struct pf_altq *a)
189 if ((hif = a->altq_disc) == NULL)
193 (void)hfsc_clear_interface(hif);
194 (void)hfsc_class_destroy(hif->hif_rootclass);
202 hfsc_add_queue(struct pf_altq *a)
205 struct hfsc_class *cl, *parent;
206 struct hfsc_opts_v1 *opts;
207 struct service_curve rtsc, lssc, ulsc;
209 if ((hif = a->altq_disc) == NULL)
212 opts = &a->pq_u.hfsc_opts;
214 if (a->parent_qid == HFSC_NULLCLASS_HANDLE &&
215 hif->hif_rootclass == NULL)
217 else if ((parent = clh_to_clp(hif, a->parent_qid)) == NULL)
223 if (clh_to_clp(hif, a->qid) != NULL)
226 rtsc.m1 = opts->rtsc_m1;
227 rtsc.d = opts->rtsc_d;
228 rtsc.m2 = opts->rtsc_m2;
229 lssc.m1 = opts->lssc_m1;
230 lssc.d = opts->lssc_d;
231 lssc.m2 = opts->lssc_m2;
232 ulsc.m1 = opts->ulsc_m1;
233 ulsc.d = opts->ulsc_d;
234 ulsc.m2 = opts->ulsc_m2;
236 cl = hfsc_class_create(hif, &rtsc, &lssc, &ulsc,
237 parent, a->qlimit, opts->flags, a->qid);
245 hfsc_remove_queue(struct pf_altq *a)
248 struct hfsc_class *cl;
250 if ((hif = a->altq_disc) == NULL)
253 if ((cl = clh_to_clp(hif, a->qid)) == NULL)
256 return (hfsc_class_destroy(cl));
260 hfsc_getqstats(struct pf_altq *a, void *ubuf, int *nbytes, int version)
263 struct hfsc_class *cl;
265 struct hfsc_classstats_v0 v0;
266 struct hfsc_classstats_v1 v1;
271 if ((hif = altq_lookup(a->ifname, ALTQT_HFSC)) == NULL)
274 if ((cl = clh_to_clp(hif, a->qid)) == NULL)
277 if (version > HFSC_STATS_VERSION)
280 memset(&stats, 0, sizeof(stats));
283 get_class_stats_v0(&stats.v0, cl);
284 stats_size = sizeof(struct hfsc_classstats_v0);
287 get_class_stats_v1(&stats.v1, cl);
288 stats_size = sizeof(struct hfsc_classstats_v1);
292 if (*nbytes < stats_size)
295 if ((error = copyout((caddr_t)&stats, ubuf, stats_size)) != 0)
297 *nbytes = stats_size;
302 * bring the interface back to the initial state by discarding
303 * all the filters and classes except the root class.
306 hfsc_clear_interface(struct hfsc_if *hif)
308 struct hfsc_class *cl;
311 /* clear out the classes */
312 while (hif->hif_rootclass != NULL &&
313 (cl = hif->hif_rootclass->cl_children) != NULL) {
315 * remove the first leaf class found in the hierarchy
318 for (; cl != NULL; cl = hfsc_nextclass(cl)) {
319 if (!is_a_parent_class(cl)) {
320 (void)hfsc_class_destroy(cl);
330 hfsc_request(struct ifaltq *ifq, int req, void *arg)
332 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
334 IFQ_LOCK_ASSERT(ifq);
344 /* discard all the queued packets on the interface */
346 hfsc_purge(struct hfsc_if *hif)
348 struct hfsc_class *cl;
350 for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl))
351 if (!qempty(cl->cl_q))
353 if (ALTQ_IS_ENABLED(hif->hif_ifq))
354 hif->hif_ifq->ifq_len = 0;
358 hfsc_class_create(struct hfsc_if *hif, struct service_curve *rsc,
359 struct service_curve *fsc, struct service_curve *usc,
360 struct hfsc_class *parent, int qlimit, int flags, int qid)
362 struct hfsc_class *cl, *p;
365 if (hif->hif_classes >= HFSC_MAX_CLASSES)
369 if (flags & HFCF_RED) {
371 printf("hfsc_class_create: RED not configured for HFSC!\n");
377 if (flags & HFCF_CODEL) {
379 printf("hfsc_class_create: CODEL not configured for HFSC!\n");
385 cl = malloc(sizeof(struct hfsc_class), M_DEVBUF, M_NOWAIT | M_ZERO);
389 cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF, M_NOWAIT | M_ZERO);
390 if (cl->cl_q == NULL)
393 TAILQ_INIT(&cl->cl_actc);
396 qlimit = 50; /* use default */
397 qlimit(cl->cl_q) = qlimit;
398 qtype(cl->cl_q) = Q_DROPTAIL;
401 cl->cl_flags = flags;
403 if (flags & (HFCF_RED|HFCF_RIO)) {
404 int red_flags, red_pkttime;
408 if (rsc != NULL && rsc->m2 > m2)
410 if (fsc != NULL && fsc->m2 > m2)
412 if (usc != NULL && usc->m2 > m2)
416 if (flags & HFCF_ECN)
417 red_flags |= REDF_ECN;
419 if (flags & HFCF_CLEARDSCP)
420 red_flags |= RIOF_CLEARDSCP;
423 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
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;
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;
444 #endif /* ALTQ_RED */
446 if (flags & HFCF_CODEL) {
447 cl->cl_codel = codel_alloc(5, 100, 0);
448 if (cl->cl_codel != NULL)
449 qtype(cl->cl_q) = Q_CODEL;
453 if (rsc != NULL && (rsc->m1 != 0 || rsc->m2 != 0)) {
454 cl->cl_rsc = malloc(sizeof(struct internal_sc),
456 if (cl->cl_rsc == NULL)
458 sc2isc(rsc, cl->cl_rsc);
459 rtsc_init(&cl->cl_deadline, cl->cl_rsc, 0, 0);
460 rtsc_init(&cl->cl_eligible, cl->cl_rsc, 0, 0);
462 if (fsc != NULL && (fsc->m1 != 0 || fsc->m2 != 0)) {
463 cl->cl_fsc = malloc(sizeof(struct internal_sc),
465 if (cl->cl_fsc == NULL)
467 sc2isc(fsc, cl->cl_fsc);
468 rtsc_init(&cl->cl_virtual, cl->cl_fsc, 0, 0);
470 if (usc != NULL && (usc->m1 != 0 || usc->m2 != 0)) {
471 cl->cl_usc = malloc(sizeof(struct internal_sc),
473 if (cl->cl_usc == NULL)
475 sc2isc(usc, cl->cl_usc);
476 rtsc_init(&cl->cl_ulimit, cl->cl_usc, 0, 0);
479 cl->cl_id = hif->hif_classid++;
482 cl->cl_parent = parent;
485 IFQ_LOCK(hif->hif_ifq);
489 * find a free slot in the class table. if the slot matching
490 * the lower bits of qid is free, use this slot. otherwise,
491 * use the first free slot.
493 i = qid % HFSC_MAX_CLASSES;
494 if (hif->hif_class_tbl[i] == NULL)
495 hif->hif_class_tbl[i] = cl;
497 for (i = 0; i < HFSC_MAX_CLASSES; i++)
498 if (hif->hif_class_tbl[i] == NULL) {
499 hif->hif_class_tbl[i] = cl;
502 if (i == HFSC_MAX_CLASSES) {
503 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 cl->cl_hif->hif_class_tbl[cl->cl_slot] = NULL;
593 cl->cl_hif->hif_classes--;
594 IFQ_UNLOCK(cl->cl_hif->hif_ifq);
597 if (cl->cl_red != NULL) {
599 if (q_is_rio(cl->cl_q))
600 rio_destroy((rio_t *)cl->cl_red);
603 if (q_is_red(cl->cl_q))
604 red_destroy(cl->cl_red);
607 if (q_is_codel(cl->cl_q))
608 codel_destroy(cl->cl_codel);
612 IFQ_LOCK(cl->cl_hif->hif_ifq);
613 if (cl == cl->cl_hif->hif_rootclass)
614 cl->cl_hif->hif_rootclass = NULL;
615 if (cl == cl->cl_hif->hif_defaultclass)
616 cl->cl_hif->hif_defaultclass = NULL;
617 IFQ_UNLOCK(cl->cl_hif->hif_ifq);
619 if (cl->cl_usc != NULL)
620 free(cl->cl_usc, M_DEVBUF);
621 if (cl->cl_fsc != NULL)
622 free(cl->cl_fsc, M_DEVBUF);
623 if (cl->cl_rsc != NULL)
624 free(cl->cl_rsc, M_DEVBUF);
625 free(cl->cl_q, M_DEVBUF);
632 * hfsc_nextclass returns the next class in the tree.
634 * for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl))
637 static struct hfsc_class *
638 hfsc_nextclass(struct hfsc_class *cl)
640 if (cl->cl_children != NULL)
641 cl = cl->cl_children;
642 else if (cl->cl_siblings != NULL)
643 cl = cl->cl_siblings;
645 while ((cl = cl->cl_parent) != NULL)
646 if (cl->cl_siblings) {
647 cl = cl->cl_siblings;
656 * hfsc_enqueue is an enqueue function to be registered to
657 * (*altq_enqueue) in struct ifaltq.
660 hfsc_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
662 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
663 struct hfsc_class *cl;
667 IFQ_LOCK_ASSERT(ifq);
669 /* grab class set by classifier */
670 if ((m->m_flags & M_PKTHDR) == 0) {
671 /* should not happen */
672 printf("altq: packet for %s does not have pkthdr\n",
673 ifq->altq_ifp->if_xname);
678 if ((t = pf_find_mtag(m)) != NULL)
679 cl = clh_to_clp(hif, t->qid);
680 if (cl == NULL || is_a_parent_class(cl)) {
681 cl = hif->hif_defaultclass;
687 cl->cl_pktattr = NULL;
689 if (hfsc_addq(cl, m) != 0) {
690 /* drop occurred. mbuf was freed in hfsc_addq. */
691 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, len);
695 cl->cl_hif->hif_packets++;
697 /* successfully queued. */
698 if (qlen(cl->cl_q) == 1)
699 set_active(cl, m_pktlen(m));
705 * hfsc_dequeue is a dequeue function to be registered to
706 * (*altq_dequeue) in struct ifaltq.
708 * note: ALTDQ_POLL returns the next packet without removing the packet
709 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
710 * ALTDQ_REMOVE must return the same packet if called immediately
714 hfsc_dequeue(struct ifaltq *ifq, int op)
716 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
717 struct hfsc_class *cl;
723 IFQ_LOCK_ASSERT(ifq);
725 if (hif->hif_packets == 0)
726 /* no packet in the tree */
729 cur_time = read_machclk();
731 if (op == ALTDQ_REMOVE && hif->hif_pollcache != NULL) {
733 cl = hif->hif_pollcache;
734 hif->hif_pollcache = NULL;
735 /* check if the class was scheduled by real-time criteria */
736 if (cl->cl_rsc != NULL)
737 realtime = (cl->cl_e <= cur_time);
740 * if there are eligible classes, use real-time criteria.
741 * find the class with the minimum deadline among
742 * the eligible classes.
744 if ((cl = hfsc_get_mindl(hif, cur_time))
752 * use link-sharing criteria
753 * get the class with the minimum vt in the hierarchy
755 cl = hif->hif_rootclass;
756 while (is_a_parent_class(cl)) {
758 cl = actlist_firstfit(cl, cur_time);
762 printf("%d fit but none found\n",fits);
767 * update parent's cl_cvtmin.
768 * don't update if the new vt is smaller.
770 if (cl->cl_parent->cl_cvtmin < cl->cl_vt)
771 cl->cl_parent->cl_cvtmin = cl->cl_vt;
778 if (op == ALTDQ_POLL) {
779 hif->hif_pollcache = cl;
787 panic("hfsc_dequeue:");
789 cl->cl_hif->hif_packets--;
791 PKTCNTR_ADD(&cl->cl_stats.xmit_cnt, len);
793 update_vf(cl, len, cur_time);
797 if (!qempty(cl->cl_q)) {
798 if (cl->cl_rsc != NULL) {
800 next_len = m_pktlen(qhead(cl->cl_q));
803 update_ed(cl, next_len);
805 update_d(cl, next_len);
808 /* the class becomes passive */
816 hfsc_addq(struct hfsc_class *cl, struct mbuf *m)
820 if (q_is_rio(cl->cl_q))
821 return rio_addq((rio_t *)cl->cl_red, cl->cl_q,
825 if (q_is_red(cl->cl_q))
826 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
829 if (q_is_codel(cl->cl_q))
830 return codel_addq(cl->cl_codel, cl->cl_q, m);
832 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
837 if (cl->cl_flags & HFCF_CLEARDSCP)
838 write_dsfield(m, cl->cl_pktattr, 0);
846 hfsc_getq(struct hfsc_class *cl)
849 if (q_is_rio(cl->cl_q))
850 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
853 if (q_is_red(cl->cl_q))
854 return red_getq(cl->cl_red, cl->cl_q);
857 if (q_is_codel(cl->cl_q))
858 return codel_getq(cl->cl_codel, cl->cl_q);
860 return _getq(cl->cl_q);
864 hfsc_pollq(struct hfsc_class *cl)
866 return qhead(cl->cl_q);
870 hfsc_purgeq(struct hfsc_class *cl)
874 if (qempty(cl->cl_q))
877 while ((m = _getq(cl->cl_q)) != NULL) {
878 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, m_pktlen(m));
880 cl->cl_hif->hif_packets--;
881 IFQ_DEC_LEN(cl->cl_hif->hif_ifq);
883 ASSERT(qlen(cl->cl_q) == 0);
885 update_vf(cl, 0, 0); /* remove cl from the actlist */
890 set_active(struct hfsc_class *cl, int len)
892 if (cl->cl_rsc != NULL)
894 if (cl->cl_fsc != NULL)
897 cl->cl_stats.period++;
901 set_passive(struct hfsc_class *cl)
903 if (cl->cl_rsc != NULL)
907 * actlist is now handled in update_vf() so that update_vf(cl, 0, 0)
908 * needs to be called explicitly to remove a class from actlist
913 init_ed(struct hfsc_class *cl, int next_len)
917 cur_time = read_machclk();
919 /* update the deadline curve */
920 rtsc_min(&cl->cl_deadline, cl->cl_rsc, cur_time, cl->cl_cumul);
923 * update the eligible curve.
924 * for concave, it is equal to the deadline curve.
925 * for convex, it is a linear curve with slope m2.
927 cl->cl_eligible = cl->cl_deadline;
928 if (cl->cl_rsc->sm1 <= cl->cl_rsc->sm2) {
929 cl->cl_eligible.dx = 0;
930 cl->cl_eligible.dy = 0;
933 /* compute e and d */
934 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
935 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
941 update_ed(struct hfsc_class *cl, int next_len)
943 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
944 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
950 update_d(struct hfsc_class *cl, int next_len)
952 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
956 init_vf(struct hfsc_class *cl, int len)
958 struct hfsc_class *max_cl, *p;
959 u_int64_t vt, f, cur_time;
964 for ( ; cl->cl_parent != NULL; cl = cl->cl_parent) {
966 if (go_active && cl->cl_nactive++ == 0)
972 max_cl = TAILQ_LAST(&cl->cl_parent->cl_actc, acthead);
973 if (max_cl != NULL) {
975 * set vt to the average of the min and max
976 * classes. if the parent's period didn't
977 * change, don't decrease vt of the class.
980 if (cl->cl_parent->cl_cvtmin != 0)
981 vt = (cl->cl_parent->cl_cvtmin + vt)/2;
983 if (cl->cl_parent->cl_vtperiod !=
984 cl->cl_parentperiod || vt > cl->cl_vt)
988 * first child for a new parent backlog period.
989 * add parent's cvtmax to vtoff of children
990 * to make a new vt (vtoff + vt) larger than
991 * the vt in the last period for all children.
993 vt = cl->cl_parent->cl_cvtmax;
994 for (p = cl->cl_parent->cl_children; p != NULL;
998 cl->cl_parent->cl_cvtmax = 0;
999 cl->cl_parent->cl_cvtmin = 0;
1001 cl->cl_initvt = cl->cl_vt;
1003 /* update the virtual curve */
1004 vt = cl->cl_vt + cl->cl_vtoff;
1005 rtsc_min(&cl->cl_virtual, cl->cl_fsc, vt, cl->cl_total);
1006 if (cl->cl_virtual.x == vt) {
1007 cl->cl_virtual.x -= cl->cl_vtoff;
1012 cl->cl_vtperiod++; /* increment vt period */
1013 cl->cl_parentperiod = cl->cl_parent->cl_vtperiod;
1014 if (cl->cl_parent->cl_nactive == 0)
1015 cl->cl_parentperiod++;
1020 if (cl->cl_usc != NULL) {
1021 /* class has upper limit curve */
1023 cur_time = read_machclk();
1025 /* update the ulimit curve */
1026 rtsc_min(&cl->cl_ulimit, cl->cl_usc, cur_time,
1029 cl->cl_myf = rtsc_y2x(&cl->cl_ulimit,
1035 if (cl->cl_myf > cl->cl_cfmin)
1039 if (f != cl->cl_f) {
1041 update_cfmin(cl->cl_parent);
1047 update_vf(struct hfsc_class *cl, int len, u_int64_t cur_time)
1049 u_int64_t f, myf_bound, delta;
1052 go_passive = qempty(cl->cl_q);
1054 for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
1056 cl->cl_total += len;
1058 if (cl->cl_fsc == NULL || cl->cl_nactive == 0)
1061 if (go_passive && --cl->cl_nactive == 0)
1067 /* no more active child, going passive */
1069 /* update cvtmax of the parent class */
1070 if (cl->cl_vt > cl->cl_parent->cl_cvtmax)
1071 cl->cl_parent->cl_cvtmax = cl->cl_vt;
1073 /* remove this class from the vt list */
1076 update_cfmin(cl->cl_parent);
1084 cl->cl_vt = rtsc_y2x(&cl->cl_virtual, cl->cl_total)
1085 - cl->cl_vtoff + cl->cl_vtadj;
1088 * if vt of the class is smaller than cvtmin,
1089 * the class was skipped in the past due to non-fit.
1090 * if so, we need to adjust vtadj.
1092 if (cl->cl_vt < cl->cl_parent->cl_cvtmin) {
1093 cl->cl_vtadj += cl->cl_parent->cl_cvtmin - cl->cl_vt;
1094 cl->cl_vt = cl->cl_parent->cl_cvtmin;
1097 /* update the vt list */
1100 if (cl->cl_usc != NULL) {
1101 cl->cl_myf = cl->cl_myfadj
1102 + rtsc_y2x(&cl->cl_ulimit, cl->cl_total);
1105 * if myf lags behind by more than one clock tick
1106 * from the current time, adjust myfadj to prevent
1107 * a rate-limited class from going greedy.
1108 * in a steady state under rate-limiting, myf
1109 * fluctuates within one clock tick.
1111 myf_bound = cur_time - machclk_per_tick;
1112 if (cl->cl_myf < myf_bound) {
1113 delta = cur_time - cl->cl_myf;
1114 cl->cl_myfadj += delta;
1115 cl->cl_myf += delta;
1119 /* cl_f is max(cl_myf, cl_cfmin) */
1120 if (cl->cl_myf > cl->cl_cfmin)
1124 if (f != cl->cl_f) {
1126 update_cfmin(cl->cl_parent);
1132 update_cfmin(struct hfsc_class *cl)
1134 struct hfsc_class *p;
1137 if (TAILQ_EMPTY(&cl->cl_actc)) {
1141 cfmin = HT_INFINITY;
1142 TAILQ_FOREACH(p, &cl->cl_actc, cl_actlist) {
1147 if (p->cl_f < cfmin)
1150 cl->cl_cfmin = cfmin;
1154 * TAILQ based ellist and actlist implementation
1155 * (ion wanted to make a calendar queue based implementation)
1158 * eligible list holds backlogged classes being sorted by their eligible times.
1159 * there is one eligible list per interface.
1163 ellist_insert(struct hfsc_class *cl)
1165 struct hfsc_if *hif = cl->cl_hif;
1166 struct hfsc_class *p;
1168 /* check the last entry first */
1169 if ((p = TAILQ_LAST(&hif->hif_eligible, elighead)) == NULL ||
1170 p->cl_e <= cl->cl_e) {
1171 TAILQ_INSERT_TAIL(&hif->hif_eligible, cl, cl_ellist);
1175 TAILQ_FOREACH(p, &hif->hif_eligible, cl_ellist) {
1176 if (cl->cl_e < p->cl_e) {
1177 TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1181 ASSERT(0); /* should not reach here */
1185 ellist_remove(struct hfsc_class *cl)
1187 struct hfsc_if *hif = cl->cl_hif;
1189 TAILQ_REMOVE(&hif->hif_eligible, cl, cl_ellist);
1193 ellist_update(struct hfsc_class *cl)
1195 struct hfsc_if *hif = cl->cl_hif;
1196 struct hfsc_class *p, *last;
1199 * the eligible time of a class increases monotonically.
1200 * if the next entry has a larger eligible time, nothing to do.
1202 p = TAILQ_NEXT(cl, cl_ellist);
1203 if (p == NULL || cl->cl_e <= p->cl_e)
1206 /* check the last entry */
1207 last = TAILQ_LAST(&hif->hif_eligible, elighead);
1208 ASSERT(last != NULL);
1209 if (last->cl_e <= cl->cl_e) {
1210 TAILQ_REMOVE(&hif->hif_eligible, cl, cl_ellist);
1211 TAILQ_INSERT_TAIL(&hif->hif_eligible, cl, cl_ellist);
1216 * the new position must be between the next entry
1217 * and the last entry
1219 while ((p = TAILQ_NEXT(p, cl_ellist)) != NULL) {
1220 if (cl->cl_e < p->cl_e) {
1221 TAILQ_REMOVE(&hif->hif_eligible, cl, cl_ellist);
1222 TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1226 ASSERT(0); /* should not reach here */
1229 /* find the class with the minimum deadline among the eligible classes */
1231 hfsc_get_mindl(struct hfsc_if *hif, u_int64_t cur_time)
1233 struct hfsc_class *p, *cl = NULL;
1235 TAILQ_FOREACH(p, &hif->hif_eligible, cl_ellist) {
1236 if (p->cl_e > cur_time)
1238 if (cl == NULL || p->cl_d < cl->cl_d)
1245 * active children list holds backlogged child classes being sorted
1246 * by their virtual time.
1247 * each intermediate class has one active children list.
1251 actlist_insert(struct hfsc_class *cl)
1253 struct hfsc_class *p;
1255 /* check the last entry first */
1256 if ((p = TAILQ_LAST(&cl->cl_parent->cl_actc, acthead)) == NULL
1257 || p->cl_vt <= cl->cl_vt) {
1258 TAILQ_INSERT_TAIL(&cl->cl_parent->cl_actc, cl, cl_actlist);
1262 TAILQ_FOREACH(p, &cl->cl_parent->cl_actc, cl_actlist) {
1263 if (cl->cl_vt < p->cl_vt) {
1264 TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1268 ASSERT(0); /* should not reach here */
1272 actlist_remove(struct hfsc_class *cl)
1274 TAILQ_REMOVE(&cl->cl_parent->cl_actc, cl, cl_actlist);
1278 actlist_update(struct hfsc_class *cl)
1280 struct hfsc_class *p, *last;
1283 * the virtual time of a class increases monotonically during its
1284 * backlogged period.
1285 * if the next entry has a larger virtual time, nothing to do.
1287 p = TAILQ_NEXT(cl, cl_actlist);
1288 if (p == NULL || cl->cl_vt < p->cl_vt)
1291 /* check the last entry */
1292 last = TAILQ_LAST(&cl->cl_parent->cl_actc, acthead);
1293 ASSERT(last != NULL);
1294 if (last->cl_vt <= cl->cl_vt) {
1295 TAILQ_REMOVE(&cl->cl_parent->cl_actc, cl, cl_actlist);
1296 TAILQ_INSERT_TAIL(&cl->cl_parent->cl_actc, cl, cl_actlist);
1301 * the new position must be between the next entry
1302 * and the last entry
1304 while ((p = TAILQ_NEXT(p, cl_actlist)) != NULL) {
1305 if (cl->cl_vt < p->cl_vt) {
1306 TAILQ_REMOVE(&cl->cl_parent->cl_actc, cl, cl_actlist);
1307 TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1311 ASSERT(0); /* should not reach here */
1314 static struct hfsc_class *
1315 actlist_firstfit(struct hfsc_class *cl, u_int64_t cur_time)
1317 struct hfsc_class *p;
1319 TAILQ_FOREACH(p, &cl->cl_actc, cl_actlist) {
1320 if (p->cl_f <= cur_time)
1327 * service curve support functions
1329 * external service curve parameters
1332 * internal service curve parameters
1333 * sm: (bytes/machclk tick) << SM_SHIFT
1334 * ism: (machclk ticks/byte) << ISM_SHIFT
1337 * SM_SHIFT and ISM_SHIFT are scaled in order to keep effective digits. we
1338 * should be able to handle 100K-100Gbps linkspeed with 256 MHz machclk
1339 * frequency and at least 3 effective digits in decimal.
1343 #define ISM_SHIFT 14
1345 #define SM_MASK ((1LL << SM_SHIFT) - 1)
1346 #define ISM_MASK ((1LL << ISM_SHIFT) - 1)
1348 static __inline u_int64_t
1349 seg_x2y(u_int64_t x, u_int64_t sm)
1355 * y = x * sm >> SM_SHIFT
1356 * but divide it for the upper and lower bits to avoid overflow
1358 y = (x >> SM_SHIFT) * sm + (((x & SM_MASK) * sm) >> SM_SHIFT);
1362 static __inline u_int64_t
1363 seg_y2x(u_int64_t y, u_int64_t ism)
1369 else if (ism == HT_INFINITY)
1372 x = (y >> ISM_SHIFT) * ism
1373 + (((y & ISM_MASK) * ism) >> ISM_SHIFT);
1378 static __inline u_int64_t
1383 sm = (m << SM_SHIFT) / 8 / machclk_freq;
1387 static __inline u_int64_t
1395 ism = ((u_int64_t)machclk_freq << ISM_SHIFT) * 8 / m;
1399 static __inline u_int64_t
1404 dx = ((u_int64_t)d * machclk_freq) / 1000;
1413 m = (sm * 8 * machclk_freq) >> SM_SHIFT;
1422 d = dx * 1000 / machclk_freq;
1427 sc2isc(struct service_curve *sc, struct internal_sc *isc)
1429 isc->sm1 = m2sm(sc->m1);
1430 isc->ism1 = m2ism(sc->m1);
1431 isc->dx = d2dx(sc->d);
1432 isc->dy = seg_x2y(isc->dx, isc->sm1);
1433 isc->sm2 = m2sm(sc->m2);
1434 isc->ism2 = m2ism(sc->m2);
1438 * initialize the runtime service curve with the given internal
1439 * service curve starting at (x, y).
1442 rtsc_init(struct runtime_sc *rtsc, struct internal_sc * isc, u_int64_t x,
1447 rtsc->sm1 = isc->sm1;
1448 rtsc->ism1 = isc->ism1;
1451 rtsc->sm2 = isc->sm2;
1452 rtsc->ism2 = isc->ism2;
1456 * calculate the y-projection of the runtime service curve by the
1457 * given x-projection value
1460 rtsc_y2x(struct runtime_sc *rtsc, u_int64_t y)
1466 else if (y <= rtsc->y + rtsc->dy) {
1467 /* x belongs to the 1st segment */
1469 x = rtsc->x + rtsc->dx;
1471 x = rtsc->x + seg_y2x(y - rtsc->y, rtsc->ism1);
1473 /* x belongs to the 2nd segment */
1474 x = rtsc->x + rtsc->dx
1475 + seg_y2x(y - rtsc->y - rtsc->dy, rtsc->ism2);
1481 rtsc_x2y(struct runtime_sc *rtsc, u_int64_t x)
1487 else if (x <= rtsc->x + rtsc->dx)
1488 /* y belongs to the 1st segment */
1489 y = rtsc->y + seg_x2y(x - rtsc->x, rtsc->sm1);
1491 /* y belongs to the 2nd segment */
1492 y = rtsc->y + rtsc->dy
1493 + seg_x2y(x - rtsc->x - rtsc->dx, rtsc->sm2);
1498 * update the runtime service curve by taking the minimum of the current
1499 * runtime service curve and the service curve starting at (x, y).
1502 rtsc_min(struct runtime_sc *rtsc, struct internal_sc *isc, u_int64_t x,
1505 u_int64_t y1, y2, dx, dy;
1507 if (isc->sm1 <= isc->sm2) {
1508 /* service curve is convex */
1509 y1 = rtsc_x2y(rtsc, x);
1511 /* the current rtsc is smaller */
1519 * service curve is concave
1520 * compute the two y values of the current rtsc
1524 y1 = rtsc_x2y(rtsc, x);
1526 /* rtsc is below isc, no change to rtsc */
1530 y2 = rtsc_x2y(rtsc, x + isc->dx);
1531 if (y2 >= y + isc->dy) {
1532 /* rtsc is above isc, replace rtsc by isc */
1541 * the two curves intersect
1542 * compute the offsets (dx, dy) using the reverse
1543 * function of seg_x2y()
1544 * seg_x2y(dx, sm1) == seg_x2y(dx, sm2) + (y1 - y)
1546 dx = ((y1 - y) << SM_SHIFT) / (isc->sm1 - isc->sm2);
1548 * check if (x, y1) belongs to the 1st segment of rtsc.
1549 * if so, add the offset.
1551 if (rtsc->x + rtsc->dx > x)
1552 dx += rtsc->x + rtsc->dx - x;
1553 dy = seg_x2y(dx, isc->sm1);
1563 get_class_stats_v0(struct hfsc_classstats_v0 *sp, struct hfsc_class *cl)
1565 sp->class_id = cl->cl_id;
1566 sp->class_handle = cl->cl_handle;
1568 #define SATU32(x) (u_int32_t)uqmin((x), UINT_MAX)
1570 if (cl->cl_rsc != NULL) {
1571 sp->rsc.m1 = SATU32(sm2m(cl->cl_rsc->sm1));
1572 sp->rsc.d = dx2d(cl->cl_rsc->dx);
1573 sp->rsc.m2 = SATU32(sm2m(cl->cl_rsc->sm2));
1579 if (cl->cl_fsc != NULL) {
1580 sp->fsc.m1 = SATU32(sm2m(cl->cl_fsc->sm1));
1581 sp->fsc.d = dx2d(cl->cl_fsc->dx);
1582 sp->fsc.m2 = SATU32(sm2m(cl->cl_fsc->sm2));
1588 if (cl->cl_usc != NULL) {
1589 sp->usc.m1 = SATU32(sm2m(cl->cl_usc->sm1));
1590 sp->usc.d = dx2d(cl->cl_usc->dx);
1591 sp->usc.m2 = SATU32(sm2m(cl->cl_usc->sm2));
1600 sp->total = cl->cl_total;
1601 sp->cumul = cl->cl_cumul;
1608 sp->initvt = cl->cl_initvt;
1609 sp->vtperiod = cl->cl_vtperiod;
1610 sp->parentperiod = cl->cl_parentperiod;
1611 sp->nactive = cl->cl_nactive;
1612 sp->vtoff = cl->cl_vtoff;
1613 sp->cvtmax = cl->cl_cvtmax;
1614 sp->myf = cl->cl_myf;
1615 sp->cfmin = cl->cl_cfmin;
1616 sp->cvtmin = cl->cl_cvtmin;
1617 sp->myfadj = cl->cl_myfadj;
1618 sp->vtadj = cl->cl_vtadj;
1620 sp->cur_time = read_machclk();
1621 sp->machclk_freq = machclk_freq;
1623 sp->qlength = qlen(cl->cl_q);
1624 sp->qlimit = qlimit(cl->cl_q);
1625 sp->xmit_cnt = cl->cl_stats.xmit_cnt;
1626 sp->drop_cnt = cl->cl_stats.drop_cnt;
1627 sp->period = cl->cl_stats.period;
1629 sp->qtype = qtype(cl->cl_q);
1631 if (q_is_red(cl->cl_q))
1632 red_getstats(cl->cl_red, &sp->red[0]);
1635 if (q_is_rio(cl->cl_q))
1636 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
1639 if (q_is_codel(cl->cl_q))
1640 codel_getstats(cl->cl_codel, &sp->codel);
1645 get_class_stats_v1(struct hfsc_classstats_v1 *sp, struct hfsc_class *cl)
1647 sp->class_id = cl->cl_id;
1648 sp->class_handle = cl->cl_handle;
1650 if (cl->cl_rsc != NULL) {
1651 sp->rsc.m1 = sm2m(cl->cl_rsc->sm1);
1652 sp->rsc.d = dx2d(cl->cl_rsc->dx);
1653 sp->rsc.m2 = sm2m(cl->cl_rsc->sm2);
1659 if (cl->cl_fsc != NULL) {
1660 sp->fsc.m1 = sm2m(cl->cl_fsc->sm1);
1661 sp->fsc.d = dx2d(cl->cl_fsc->dx);
1662 sp->fsc.m2 = sm2m(cl->cl_fsc->sm2);
1668 if (cl->cl_usc != NULL) {
1669 sp->usc.m1 = sm2m(cl->cl_usc->sm1);
1670 sp->usc.d = dx2d(cl->cl_usc->dx);
1671 sp->usc.m2 = sm2m(cl->cl_usc->sm2);
1678 sp->total = cl->cl_total;
1679 sp->cumul = cl->cl_cumul;
1686 sp->initvt = cl->cl_initvt;
1687 sp->vtperiod = cl->cl_vtperiod;
1688 sp->parentperiod = cl->cl_parentperiod;
1689 sp->nactive = cl->cl_nactive;
1690 sp->vtoff = cl->cl_vtoff;
1691 sp->cvtmax = cl->cl_cvtmax;
1692 sp->myf = cl->cl_myf;
1693 sp->cfmin = cl->cl_cfmin;
1694 sp->cvtmin = cl->cl_cvtmin;
1695 sp->myfadj = cl->cl_myfadj;
1696 sp->vtadj = cl->cl_vtadj;
1698 sp->cur_time = read_machclk();
1699 sp->machclk_freq = machclk_freq;
1701 sp->qlength = qlen(cl->cl_q);
1702 sp->qlimit = qlimit(cl->cl_q);
1703 sp->xmit_cnt = cl->cl_stats.xmit_cnt;
1704 sp->drop_cnt = cl->cl_stats.drop_cnt;
1705 sp->period = cl->cl_stats.period;
1707 sp->qtype = qtype(cl->cl_q);
1709 if (q_is_red(cl->cl_q))
1710 red_getstats(cl->cl_red, &sp->red[0]);
1713 if (q_is_rio(cl->cl_q))
1714 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
1717 if (q_is_codel(cl->cl_q))
1718 codel_getstats(cl->cl_codel, &sp->codel);
1722 /* convert a class handle to the corresponding class pointer */
1723 static struct hfsc_class *
1724 clh_to_clp(struct hfsc_if *hif, u_int32_t chandle)
1727 struct hfsc_class *cl;
1732 * first, try optimistically the slot matching the lower bits of
1733 * the handle. if it fails, do the linear table search.
1735 i = chandle % HFSC_MAX_CLASSES;
1736 if ((cl = hif->hif_class_tbl[i]) != NULL && cl->cl_handle == chandle)
1738 for (i = 0; i < HFSC_MAX_CLASSES; i++)
1739 if ((cl = hif->hif_class_tbl[i]) != NULL &&
1740 cl->cl_handle == chandle)
1746 #endif /* ALTQ_HFSC */