1 /* $KAME: altq_hfsc.c,v 1.25 2004/04/17 10:54:48 kjc Exp $ */
2 /* $DragonFly: src/sys/net/altq/altq_hfsc.c,v 1.5 2005/11/22 00:24:35 dillon Exp $ */
5 * Copyright (c) 1997-1999 Carnegie Mellon University. All Rights Reserved.
7 * Permission to use, copy, modify, and distribute this software and
8 * its documentation is hereby granted (including for commercial or
9 * for-profit use), provided that both the copyright notice and this
10 * permission notice appear in all copies of the software, derivative
11 * works, or modified versions, and any portions thereof.
13 * THIS SOFTWARE IS EXPERIMENTAL AND IS KNOWN TO HAVE BUGS, SOME OF
14 * WHICH MAY HAVE SERIOUS CONSEQUENCES. CARNEGIE MELLON PROVIDES THIS
15 * SOFTWARE IN ITS ``AS IS'' CONDITION, AND ANY EXPRESS OR IMPLIED
16 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
25 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
28 * Carnegie Mellon encourages (but does not require) users of this
29 * software to return any improvements or extensions that they make,
30 * and to grant Carnegie Mellon the rights to redistribute these
31 * changes without encumbrance.
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 #include <sys/thread.h>
61 #include <net/ifq_var.h>
62 #include <netinet/in.h>
64 #include <net/pf/pfvar.h>
65 #include <net/altq/altq.h>
66 #include <net/altq/altq_hfsc.h>
68 #include <sys/thread2.h>
73 static int hfsc_clear_interface(struct hfsc_if *);
74 static int hfsc_request(struct ifaltq *, int, void *);
75 static void hfsc_purge(struct hfsc_if *);
76 static struct hfsc_class *hfsc_class_create(struct hfsc_if *,
77 struct service_curve *,
78 struct service_curve *,
79 struct service_curve *,
80 struct hfsc_class *, int, int, int);
81 static int hfsc_class_destroy(struct hfsc_class *);
82 static struct hfsc_class *hfsc_nextclass(struct hfsc_class *);
83 static int hfsc_enqueue(struct ifaltq *, struct mbuf *,
84 struct altq_pktattr *);
85 static struct mbuf *hfsc_dequeue(struct ifaltq *, struct mbuf *, int);
87 static int hfsc_addq(struct hfsc_class *, struct mbuf *);
88 static struct mbuf *hfsc_getq(struct hfsc_class *);
89 static struct mbuf *hfsc_pollq(struct hfsc_class *);
90 static void hfsc_purgeq(struct hfsc_class *);
92 static void update_cfmin(struct hfsc_class *);
93 static void set_active(struct hfsc_class *, int);
94 static void set_passive(struct hfsc_class *);
96 static void init_ed(struct hfsc_class *, int);
97 static void update_ed(struct hfsc_class *, int);
98 static void update_d(struct hfsc_class *, int);
99 static void init_vf(struct hfsc_class *, int);
100 static void update_vf(struct hfsc_class *, int, uint64_t);
101 static ellist_t *ellist_alloc(void);
102 static void ellist_destroy(ellist_t *);
103 static void ellist_insert(struct hfsc_class *);
104 static void ellist_remove(struct hfsc_class *);
105 static void ellist_update(struct hfsc_class *);
106 struct hfsc_class *ellist_get_mindl(ellist_t *, uint64_t);
107 static actlist_t *actlist_alloc(void);
108 static void actlist_destroy(actlist_t *);
109 static void actlist_insert(struct hfsc_class *);
110 static void actlist_remove(struct hfsc_class *);
111 static void actlist_update(struct hfsc_class *);
113 static struct hfsc_class *actlist_firstfit(struct hfsc_class *, uint64_t);
115 static __inline uint64_t seg_x2y(uint64_t, uint64_t);
116 static __inline uint64_t seg_y2x(uint64_t, uint64_t);
117 static __inline uint64_t m2sm(u_int);
118 static __inline uint64_t m2ism(u_int);
119 static __inline uint64_t d2dx(u_int);
120 static u_int sm2m(uint64_t);
121 static u_int dx2d(uint64_t);
123 static void sc2isc(struct service_curve *, struct internal_sc *);
124 static void rtsc_init(struct runtime_sc *, struct internal_sc *,
126 static uint64_t rtsc_y2x(struct runtime_sc *, uint64_t);
127 static uint64_t rtsc_x2y(struct runtime_sc *, uint64_t);
128 static void rtsc_min(struct runtime_sc *, struct internal_sc *,
131 static void get_class_stats(struct hfsc_classstats *, struct hfsc_class *);
132 static struct hfsc_class *clh_to_clp(struct hfsc_if *, uint32_t);
137 #define is_a_parent_class(cl) ((cl)->cl_children != NULL)
139 #define HT_INFINITY 0xffffffffffffffffLL /* infinite time value */
142 hfsc_pfattach(struct pf_altq *a)
147 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
150 error = altq_attach(&ifp->if_snd, ALTQT_HFSC, a->altq_disc,
151 hfsc_enqueue, hfsc_dequeue, hfsc_request, NULL, NULL);
157 hfsc_add_altq(struct pf_altq *a)
162 if ((ifp = ifunit(a->ifname)) == NULL)
164 if (!ifq_is_ready(&ifp->if_snd))
167 hif = malloc(sizeof(struct hfsc_if), M_ALTQ, M_WAITOK | M_ZERO);
169 hif->hif_eligible = ellist_alloc();
170 hif->hif_ifq = &ifp->if_snd;
172 /* keep the state in pf_altq */
179 hfsc_remove_altq(struct pf_altq *a)
183 if ((hif = a->altq_disc) == NULL)
187 hfsc_clear_interface(hif);
188 hfsc_class_destroy(hif->hif_rootclass);
190 ellist_destroy(hif->hif_eligible);
198 hfsc_add_queue(struct pf_altq *a)
201 struct hfsc_class *cl, *parent;
202 struct hfsc_opts *opts;
203 struct service_curve rtsc, lssc, ulsc;
205 if ((hif = a->altq_disc) == NULL)
208 opts = &a->pq_u.hfsc_opts;
210 if (a->parent_qid == HFSC_NULLCLASS_HANDLE && hif->hif_rootclass == NULL)
212 else if ((parent = clh_to_clp(hif, a->parent_qid)) == NULL)
218 if (clh_to_clp(hif, a->qid) != NULL)
221 rtsc.m1 = opts->rtsc_m1;
222 rtsc.d = opts->rtsc_d;
223 rtsc.m2 = opts->rtsc_m2;
224 lssc.m1 = opts->lssc_m1;
225 lssc.d = opts->lssc_d;
226 lssc.m2 = opts->lssc_m2;
227 ulsc.m1 = opts->ulsc_m1;
228 ulsc.d = opts->ulsc_d;
229 ulsc.m2 = opts->ulsc_m2;
231 cl = hfsc_class_create(hif, &rtsc, &lssc, &ulsc, parent, a->qlimit,
232 opts->flags, a->qid);
240 hfsc_remove_queue(struct pf_altq *a)
243 struct hfsc_class *cl;
245 if ((hif = a->altq_disc) == NULL)
248 if ((cl = clh_to_clp(hif, a->qid)) == NULL)
251 return (hfsc_class_destroy(cl));
255 hfsc_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
258 struct hfsc_class *cl;
259 struct hfsc_classstats stats;
262 if ((hif = altq_lookup(a->ifname, ALTQT_HFSC)) == NULL)
265 if ((cl = clh_to_clp(hif, a->qid)) == NULL)
268 if (*nbytes < sizeof(stats))
271 get_class_stats(&stats, cl);
273 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
275 *nbytes = sizeof(stats);
280 * bring the interface back to the initial state by discarding
281 * all the filters and classes except the root class.
284 hfsc_clear_interface(struct hfsc_if *hif)
286 struct hfsc_class *cl;
288 if (hif->hif_rootclass == NULL)
292 /* clear out the classes */
293 while ((cl = hif->hif_rootclass->cl_children) != NULL) {
295 * remove the first leaf class found in the hierarchy
298 for (; cl != NULL; cl = hfsc_nextclass(cl)) {
299 if (!is_a_parent_class(cl)) {
300 hfsc_class_destroy(cl);
310 hfsc_request(struct ifaltq *ifq, int req, void *arg)
312 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
324 /* discard all the queued packets on the interface */
326 hfsc_purge(struct hfsc_if *hif)
328 struct hfsc_class *cl;
330 for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl)) {
331 if (!qempty(cl->cl_q))
334 if (ifq_is_enabled(hif->hif_ifq))
335 hif->hif_ifq->ifq_len = 0;
339 hfsc_class_create(struct hfsc_if *hif, struct service_curve *rsc,
340 struct service_curve *fsc, struct service_curve *usc,
341 struct hfsc_class *parent, int qlimit, int flags, int qid)
343 struct hfsc_class *cl, *p;
346 if (hif->hif_classes >= HFSC_MAX_CLASSES)
350 if (flags & HFCF_RED) {
352 printf("hfsc_class_create: RED not configured for HFSC!\n");
358 cl = malloc(sizeof(*cl), M_ALTQ, M_WAITOK | M_ZERO);
359 cl->cl_q = malloc(sizeof(*cl->cl_q), M_ALTQ, M_WAITOK | M_ZERO);
360 cl->cl_actc = actlist_alloc();
363 qlimit = 50; /* use default */
364 qlimit(cl->cl_q) = qlimit;
365 qtype(cl->cl_q) = Q_DROPTAIL;
367 cl->cl_flags = flags;
369 if (flags & (HFCF_RED|HFCF_RIO)) {
370 int red_flags, red_pkttime;
374 if (rsc != NULL && rsc->m2 > m2)
376 if (fsc != NULL && fsc->m2 > m2)
378 if (usc != NULL && usc->m2 > m2)
382 if (flags & HFCF_ECN)
383 red_flags |= REDF_ECN;
385 if (flags & HFCF_CLEARDSCP)
386 red_flags |= RIOF_CLEARDSCP;
389 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
391 red_pkttime = (int64_t)hif->hif_ifq->altq_ifp->if_mtu
392 * 1000 * 1000 * 1000 / (m2 / 8);
393 if (flags & HFCF_RED) {
394 cl->cl_red = red_alloc(0, 0,
395 qlimit(cl->cl_q) * 10/100,
396 qlimit(cl->cl_q) * 30/100,
397 red_flags, red_pkttime);
398 if (cl->cl_red != NULL)
399 qtype(cl->cl_q) = Q_RED;
403 cl->cl_red = (red_t *)rio_alloc(0, NULL,
404 red_flags, red_pkttime);
405 if (cl->cl_red != NULL)
406 qtype(cl->cl_q) = Q_RIO;
410 #endif /* ALTQ_RED */
412 if (rsc != NULL && (rsc->m1 != 0 || rsc->m2 != 0)) {
413 cl->cl_rsc = malloc(sizeof(*cl->cl_rsc), M_ALTQ, M_WAITOK);
414 sc2isc(rsc, cl->cl_rsc);
415 rtsc_init(&cl->cl_deadline, cl->cl_rsc, 0, 0);
416 rtsc_init(&cl->cl_eligible, cl->cl_rsc, 0, 0);
418 if (fsc != NULL && (fsc->m1 != 0 || fsc->m2 != 0)) {
419 cl->cl_fsc = malloc(sizeof(*cl->cl_fsc), M_ALTQ, M_WAITOK);
420 if (cl->cl_fsc == NULL)
422 sc2isc(fsc, cl->cl_fsc);
423 rtsc_init(&cl->cl_virtual, cl->cl_fsc, 0, 0);
425 if (usc != NULL && (usc->m1 != 0 || usc->m2 != 0)) {
426 cl->cl_usc = malloc(sizeof(*cl->cl_usc), M_ALTQ, M_WAITOK);
427 if (cl->cl_usc == NULL)
429 sc2isc(usc, cl->cl_usc);
430 rtsc_init(&cl->cl_ulimit, cl->cl_usc, 0, 0);
433 cl->cl_id = hif->hif_classid++;
436 cl->cl_parent = parent;
442 * find a free slot in the class table. if the slot matching
443 * the lower bits of qid is free, use this slot. otherwise,
444 * use the first free slot.
446 i = qid % HFSC_MAX_CLASSES;
447 if (hif->hif_class_tbl[i] == NULL)
448 hif->hif_class_tbl[i] = cl;
450 for (i = 0; i < HFSC_MAX_CLASSES; i++) {
451 if (hif->hif_class_tbl[i] == NULL) {
452 hif->hif_class_tbl[i] = cl;
456 if (i == HFSC_MAX_CLASSES) {
462 if (flags & HFCF_DEFAULTCLASS)
463 hif->hif_defaultclass = cl;
465 if (parent == NULL) {
466 /* this is root class */
467 hif->hif_rootclass = cl;
468 } else if (parent->cl_children == NULL) {
469 /* add this class to the children list of the parent */
470 parent->cl_children = cl;
472 p = parent->cl_children;
473 while (p->cl_siblings != NULL)
482 if (cl->cl_actc != NULL)
483 actlist_destroy(cl->cl_actc);
484 if (cl->cl_red != NULL) {
486 if (q_is_rio(cl->cl_q))
487 rio_destroy((rio_t *)cl->cl_red);
490 if (q_is_red(cl->cl_q))
491 red_destroy(cl->cl_red);
494 if (cl->cl_fsc != NULL)
495 free(cl->cl_fsc, M_ALTQ);
496 if (cl->cl_rsc != NULL)
497 free(cl->cl_rsc, M_ALTQ);
498 if (cl->cl_usc != NULL)
499 free(cl->cl_usc, M_ALTQ);
500 if (cl->cl_q != NULL)
501 free(cl->cl_q, M_ALTQ);
507 hfsc_class_destroy(struct hfsc_class *cl)
514 if (is_a_parent_class(cl))
519 if (!qempty(cl->cl_q))
522 if (cl->cl_parent == NULL) {
523 /* this is root class */
525 struct hfsc_class *p = cl->cl_parent->cl_children;
528 cl->cl_parent->cl_children = cl->cl_siblings;
531 if (p->cl_siblings == cl) {
532 p->cl_siblings = cl->cl_siblings;
535 } while ((p = p->cl_siblings) != NULL);
540 for (i = 0; i < HFSC_MAX_CLASSES; i++) {
541 if (cl->cl_hif->hif_class_tbl[i] == cl) {
542 cl->cl_hif->hif_class_tbl[i] = NULL;
547 cl->cl_hif->hif_classes--;
550 actlist_destroy(cl->cl_actc);
552 if (cl->cl_red != NULL) {
554 if (q_is_rio(cl->cl_q))
555 rio_destroy((rio_t *)cl->cl_red);
558 if (q_is_red(cl->cl_q))
559 red_destroy(cl->cl_red);
563 if (cl == cl->cl_hif->hif_rootclass)
564 cl->cl_hif->hif_rootclass = NULL;
565 if (cl == cl->cl_hif->hif_defaultclass)
566 cl->cl_hif->hif_defaultclass = NULL;
568 if (cl->cl_usc != NULL)
569 free(cl->cl_usc, M_ALTQ);
570 if (cl->cl_fsc != NULL)
571 free(cl->cl_fsc, M_ALTQ);
572 if (cl->cl_rsc != NULL)
573 free(cl->cl_rsc, M_ALTQ);
574 free(cl->cl_q, M_ALTQ);
581 * hfsc_nextclass returns the next class in the tree.
583 * for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl))
586 static struct hfsc_class *
587 hfsc_nextclass(struct hfsc_class *cl)
589 if (cl->cl_children != NULL) {
590 cl = cl->cl_children;
591 } else if (cl->cl_siblings != NULL) {
592 cl = cl->cl_siblings;
594 while ((cl = cl->cl_parent) != NULL) {
595 if (cl->cl_siblings != NULL) {
596 cl = cl->cl_siblings;
606 * hfsc_enqueue is an enqueue function to be registered to
607 * (*altq_enqueue) in struct ifaltq.
610 hfsc_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
612 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
613 struct hfsc_class *cl;
616 /* grab class set by classifier */
617 if ((m->m_flags & M_PKTHDR) == 0) {
618 /* should not happen */
619 if_printf(ifq->altq_ifp, "altq: packet does not have pkthdr\n");
624 if (m->m_pkthdr.fw_flags & ALTQ_MBUF_TAGGED)
625 cl = clh_to_clp(hif, m->m_pkthdr.altq_qid);
628 if (cl == NULL || is_a_parent_class(cl)) {
629 cl = hif->hif_defaultclass;
636 cl->cl_pktattr = NULL;
638 if (hfsc_addq(cl, m) != 0) {
639 /* drop occurred. mbuf was freed in hfsc_addq. */
640 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, len);
645 cl->cl_hif->hif_packets++;
647 /* successfully queued. */
648 if (qlen(cl->cl_q) == 1)
649 set_active(cl, m_pktlen(m));
655 * hfsc_dequeue is a dequeue function to be registered to
656 * (*altq_dequeue) in struct ifaltq.
658 * note: ALTDQ_POLL returns the next packet without removing the packet
659 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
660 * ALTDQ_REMOVE must return the same packet if called immediately
664 hfsc_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op)
666 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
667 struct hfsc_class *cl;
673 if (hif->hif_packets == 0) {
674 /* no packet in the tree */
679 cur_time = read_machclk();
681 if (op == ALTDQ_REMOVE && hif->hif_pollcache != NULL) {
682 cl = hif->hif_pollcache;
683 hif->hif_pollcache = NULL;
684 /* check if the class was scheduled by real-time criteria */
685 if (cl->cl_rsc != NULL)
686 realtime = (cl->cl_e <= cur_time);
689 * if there are eligible classes, use real-time criteria.
690 * find the class with the minimum deadline among
691 * the eligible classes.
693 if ((cl = ellist_get_mindl(hif->hif_eligible, cur_time)) != NULL) {
700 * use link-sharing criteria
701 * get the class with the minimum vt in the hierarchy
703 cl = hif->hif_rootclass;
704 while (is_a_parent_class(cl)) {
706 cl = actlist_firstfit(cl, cur_time);
710 printf("%d fit but none found\n",fits);
716 * update parent's cl_cvtmin.
717 * don't update if the new vt is smaller.
719 if (cl->cl_parent->cl_cvtmin < cl->cl_vt)
720 cl->cl_parent->cl_cvtmin = cl->cl_vt;
727 if (op == ALTDQ_POLL) {
728 hif->hif_pollcache = cl;
736 panic("hfsc_dequeue:");
738 cl->cl_hif->hif_packets--;
740 PKTCNTR_ADD(&cl->cl_stats.xmit_cnt, len);
742 update_vf(cl, len, cur_time);
746 if (!qempty(cl->cl_q)) {
747 if (cl->cl_rsc != NULL) {
749 next_len = m_pktlen(qhead(cl->cl_q));
752 update_ed(cl, next_len);
754 update_d(cl, next_len);
757 /* the class becomes passive */
762 KKASSERT(mpolled == NULL || m == mpolled);
767 hfsc_addq(struct hfsc_class *cl, struct mbuf *m)
771 if (q_is_rio(cl->cl_q))
772 return rio_addq((rio_t *)cl->cl_red, cl->cl_q,
776 if (q_is_red(cl->cl_q))
777 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
779 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
784 if (cl->cl_flags & HFCF_CLEARDSCP)
785 write_dsfield(m, cl->cl_pktattr, 0);
793 hfsc_getq(struct hfsc_class *cl)
796 if (q_is_rio(cl->cl_q))
797 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
800 if (q_is_red(cl->cl_q))
801 return red_getq(cl->cl_red, cl->cl_q);
803 return _getq(cl->cl_q);
807 hfsc_pollq(struct hfsc_class *cl)
809 return qhead(cl->cl_q);
813 hfsc_purgeq(struct hfsc_class *cl)
817 if (qempty(cl->cl_q))
820 while ((m = _getq(cl->cl_q)) != NULL) {
821 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, m_pktlen(m));
823 cl->cl_hif->hif_packets--;
824 cl->cl_hif->hif_ifq->ifq_len--;
826 KKASSERT(qlen(cl->cl_q) == 0);
828 update_vf(cl, 0, 0); /* remove cl from the actlist */
833 set_active(struct hfsc_class *cl, int len)
835 if (cl->cl_rsc != NULL)
837 if (cl->cl_fsc != NULL)
840 cl->cl_stats.period++;
844 set_passive(struct hfsc_class *cl)
846 if (cl->cl_rsc != NULL)
850 * actlist is now handled in update_vf() so that update_vf(cl, 0, 0)
851 * needs to be called explicitly to remove a class from actlist
856 init_ed(struct hfsc_class *cl, int next_len)
860 cur_time = read_machclk();
862 /* update the deadline curve */
863 rtsc_min(&cl->cl_deadline, cl->cl_rsc, cur_time, cl->cl_cumul);
866 * update the eligible curve.
867 * for concave, it is equal to the deadline curve.
868 * for convex, it is a linear curve with slope m2.
870 cl->cl_eligible = cl->cl_deadline;
871 if (cl->cl_rsc->sm1 <= cl->cl_rsc->sm2) {
872 cl->cl_eligible.dx = 0;
873 cl->cl_eligible.dy = 0;
876 /* compute e and d */
877 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
878 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
884 update_ed(struct hfsc_class *cl, int next_len)
886 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
887 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
893 update_d(struct hfsc_class *cl, int next_len)
895 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
899 init_vf(struct hfsc_class *cl, int len)
901 struct hfsc_class *max_cl, *p;
902 uint64_t vt, f, cur_time;
907 for ( ; cl->cl_parent != NULL; cl = cl->cl_parent) {
908 if (go_active && cl->cl_nactive++ == 0)
914 max_cl = actlist_last(cl->cl_parent->cl_actc);
915 if (max_cl != NULL) {
917 * set vt to the average of the min and max
918 * classes. if the parent's period didn't
919 * change, don't decrease vt of the class.
922 if (cl->cl_parent->cl_cvtmin != 0)
923 vt = (cl->cl_parent->cl_cvtmin + vt)/2;
925 if (cl->cl_parent->cl_vtperiod !=
926 cl->cl_parentperiod || vt > cl->cl_vt)
930 * first child for a new parent backlog period.
931 * add parent's cvtmax to vtoff of children
932 * to make a new vt (vtoff + vt) larger than
933 * the vt in the last period for all children.
935 vt = cl->cl_parent->cl_cvtmax;
936 for (p = cl->cl_parent->cl_children; p != NULL;
940 cl->cl_parent->cl_cvtmax = 0;
941 cl->cl_parent->cl_cvtmin = 0;
943 cl->cl_initvt = cl->cl_vt;
945 /* update the virtual curve */
946 vt = cl->cl_vt + cl->cl_vtoff;
947 rtsc_min(&cl->cl_virtual, cl->cl_fsc, vt, cl->cl_total);
948 if (cl->cl_virtual.x == vt) {
949 cl->cl_virtual.x -= cl->cl_vtoff;
954 cl->cl_vtperiod++; /* increment vt period */
955 cl->cl_parentperiod = cl->cl_parent->cl_vtperiod;
956 if (cl->cl_parent->cl_nactive == 0)
957 cl->cl_parentperiod++;
962 if (cl->cl_usc != NULL) {
963 /* class has upper limit curve */
965 cur_time = read_machclk();
967 /* update the ulimit curve */
968 rtsc_min(&cl->cl_ulimit, cl->cl_usc, cur_time,
971 cl->cl_myf = rtsc_y2x(&cl->cl_ulimit,
977 if (cl->cl_myf > cl->cl_cfmin)
983 update_cfmin(cl->cl_parent);
989 update_vf(struct hfsc_class *cl, int len, uint64_t cur_time)
991 uint64_t f, myf_bound, delta;
994 go_passive = qempty(cl->cl_q);
996 for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
999 if (cl->cl_fsc == NULL || cl->cl_nactive == 0)
1002 if (go_passive && --cl->cl_nactive == 0)
1008 /* no more active child, going passive */
1010 /* update cvtmax of the parent class */
1011 if (cl->cl_vt > cl->cl_parent->cl_cvtmax)
1012 cl->cl_parent->cl_cvtmax = cl->cl_vt;
1014 /* remove this class from the vt list */
1017 update_cfmin(cl->cl_parent);
1025 cl->cl_vt = rtsc_y2x(&cl->cl_virtual, cl->cl_total)
1026 - cl->cl_vtoff + cl->cl_vtadj;
1029 * if vt of the class is smaller than cvtmin,
1030 * the class was skipped in the past due to non-fit.
1031 * if so, we need to adjust vtadj.
1033 if (cl->cl_vt < cl->cl_parent->cl_cvtmin) {
1034 cl->cl_vtadj += cl->cl_parent->cl_cvtmin - cl->cl_vt;
1035 cl->cl_vt = cl->cl_parent->cl_cvtmin;
1038 /* update the vt list */
1041 if (cl->cl_usc != NULL) {
1042 cl->cl_myf = cl->cl_myfadj
1043 + rtsc_y2x(&cl->cl_ulimit, cl->cl_total);
1046 * if myf lags behind by more than one clock tick
1047 * from the current time, adjust myfadj to prevent
1048 * a rate-limited class from going greedy.
1049 * in a steady state under rate-limiting, myf
1050 * fluctuates within one clock tick.
1052 myf_bound = cur_time - machclk_per_tick;
1053 if (cl->cl_myf < myf_bound) {
1054 delta = cur_time - cl->cl_myf;
1055 cl->cl_myfadj += delta;
1056 cl->cl_myf += delta;
1060 /* cl_f is max(cl_myf, cl_cfmin) */
1061 if (cl->cl_myf > cl->cl_cfmin)
1065 if (f != cl->cl_f) {
1067 update_cfmin(cl->cl_parent);
1073 update_cfmin(struct hfsc_class *cl)
1075 struct hfsc_class *p;
1078 if (TAILQ_EMPTY(cl->cl_actc)) {
1082 cfmin = HT_INFINITY;
1083 TAILQ_FOREACH(p, cl->cl_actc, cl_actlist) {
1088 if (p->cl_f < cfmin)
1091 cl->cl_cfmin = cfmin;
1095 * TAILQ based ellist and actlist implementation
1096 * (ion wanted to make a calendar queue based implementation)
1099 * eligible list holds backlogged classes being sorted by their eligible times.
1100 * there is one eligible list per interface.
1108 head = malloc(sizeof(ellist_t *), M_ALTQ, M_WAITOK);
1114 ellist_destroy(ellist_t *head)
1120 ellist_insert(struct hfsc_class *cl)
1122 struct hfsc_if *hif = cl->cl_hif;
1123 struct hfsc_class *p;
1125 /* check the last entry first */
1126 if ((p = TAILQ_LAST(hif->hif_eligible, _eligible)) == NULL ||
1127 p->cl_e <= cl->cl_e) {
1128 TAILQ_INSERT_TAIL(hif->hif_eligible, cl, cl_ellist);
1132 TAILQ_FOREACH(p, hif->hif_eligible, cl_ellist) {
1133 if (cl->cl_e < p->cl_e) {
1134 TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1138 KKASSERT(0); /* should not reach here */
1142 ellist_remove(struct hfsc_class *cl)
1144 struct hfsc_if *hif = cl->cl_hif;
1146 TAILQ_REMOVE(hif->hif_eligible, cl, cl_ellist);
1150 ellist_update(struct hfsc_class *cl)
1152 struct hfsc_if *hif = cl->cl_hif;
1153 struct hfsc_class *p, *last;
1156 * the eligible time of a class increases monotonically.
1157 * if the next entry has a larger eligible time, nothing to do.
1159 p = TAILQ_NEXT(cl, cl_ellist);
1160 if (p == NULL || cl->cl_e <= p->cl_e)
1163 /* check the last entry */
1164 last = TAILQ_LAST(hif->hif_eligible, _eligible);
1165 KKASSERT(last != NULL);
1166 if (last->cl_e <= cl->cl_e) {
1167 TAILQ_REMOVE(hif->hif_eligible, cl, cl_ellist);
1168 TAILQ_INSERT_TAIL(hif->hif_eligible, cl, cl_ellist);
1173 * the new position must be between the next entry
1174 * and the last entry
1176 while ((p = TAILQ_NEXT(p, cl_ellist)) != NULL) {
1177 if (cl->cl_e < p->cl_e) {
1178 TAILQ_REMOVE(hif->hif_eligible, cl, cl_ellist);
1179 TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1183 KKASSERT(0); /* should not reach here */
1186 /* find the class with the minimum deadline among the eligible classes */
1188 ellist_get_mindl(ellist_t *head, uint64_t cur_time)
1190 struct hfsc_class *p, *cl = NULL;
1192 TAILQ_FOREACH(p, head, cl_ellist) {
1193 if (p->cl_e > cur_time)
1195 if (cl == NULL || p->cl_d < cl->cl_d)
1202 * active children list holds backlogged child classes being sorted
1203 * by their virtual time.
1204 * each intermediate class has one active children list.
1211 head = malloc(sizeof(*head), M_ALTQ, M_WAITOK);
1217 actlist_destroy(actlist_t *head)
1222 actlist_insert(struct hfsc_class *cl)
1224 struct hfsc_class *p;
1226 /* check the last entry first */
1227 if ((p = TAILQ_LAST(cl->cl_parent->cl_actc, _active)) == NULL
1228 || p->cl_vt <= cl->cl_vt) {
1229 TAILQ_INSERT_TAIL(cl->cl_parent->cl_actc, cl, cl_actlist);
1233 TAILQ_FOREACH(p, cl->cl_parent->cl_actc, cl_actlist) {
1234 if (cl->cl_vt < p->cl_vt) {
1235 TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1239 KKASSERT(0); /* should not reach here */
1243 actlist_remove(struct hfsc_class *cl)
1245 TAILQ_REMOVE(cl->cl_parent->cl_actc, cl, cl_actlist);
1249 actlist_update(struct hfsc_class *cl)
1251 struct hfsc_class *p, *last;
1254 * the virtual time of a class increases monotonically during its
1255 * backlogged period.
1256 * if the next entry has a larger virtual time, nothing to do.
1258 p = TAILQ_NEXT(cl, cl_actlist);
1259 if (p == NULL || cl->cl_vt < p->cl_vt)
1262 /* check the last entry */
1263 last = TAILQ_LAST(cl->cl_parent->cl_actc, _active);
1264 KKASSERT(last != NULL);
1265 if (last->cl_vt <= cl->cl_vt) {
1266 TAILQ_REMOVE(cl->cl_parent->cl_actc, cl, cl_actlist);
1267 TAILQ_INSERT_TAIL(cl->cl_parent->cl_actc, cl, cl_actlist);
1272 * the new position must be between the next entry
1273 * and the last entry
1275 while ((p = TAILQ_NEXT(p, cl_actlist)) != NULL) {
1276 if (cl->cl_vt < p->cl_vt) {
1277 TAILQ_REMOVE(cl->cl_parent->cl_actc, cl, cl_actlist);
1278 TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1282 KKASSERT(0); /* should not reach here */
1285 static struct hfsc_class *
1286 actlist_firstfit(struct hfsc_class *cl, uint64_t cur_time)
1288 struct hfsc_class *p;
1290 TAILQ_FOREACH(p, cl->cl_actc, cl_actlist) {
1291 if (p->cl_f <= cur_time)
1298 * service curve support functions
1300 * external service curve parameters
1303 * internal service curve parameters
1304 * sm: (bytes/tsc_interval) << SM_SHIFT
1305 * ism: (tsc_count/byte) << ISM_SHIFT
1308 * SM_SHIFT and ISM_SHIFT are scaled in order to keep effective digits.
1309 * we should be able to handle 100K-1Gbps linkspeed with 200Hz-1GHz CPU
1310 * speed. SM_SHIFT and ISM_SHIFT are selected to have at least 3 effective
1311 * digits in decimal using the following table.
1313 * bits/sec 100Kbps 1Mbps 10Mbps 100Mbps 1Gbps
1314 * ----------+-------------------------------------------------------
1315 * bytes/nsec 12.5e-6 125e-6 1250e-6 12500e-6 125000e-6
1316 * sm(500MHz) 25.0e-6 250e-6 2500e-6 25000e-6 250000e-6
1317 * sm(200MHz) 62.5e-6 625e-6 6250e-6 62500e-6 625000e-6
1319 * nsec/byte 80000 8000 800 80 8
1320 * ism(500MHz) 40000 4000 400 40 4
1321 * ism(200MHz) 16000 1600 160 16 1.6
1324 #define ISM_SHIFT 10
1326 #define SM_MASK ((1LL << SM_SHIFT) - 1)
1327 #define ISM_MASK ((1LL << ISM_SHIFT) - 1)
1329 static __inline uint64_t
1330 seg_x2y(uint64_t x, uint64_t sm)
1336 * y = x * sm >> SM_SHIFT
1337 * but divide it for the upper and lower bits to avoid overflow
1339 y = (x >> SM_SHIFT) * sm + (((x & SM_MASK) * sm) >> SM_SHIFT);
1343 static __inline uint64_t
1344 seg_y2x(uint64_t y, uint64_t ism)
1350 else if (ism == HT_INFINITY)
1353 x = (y >> ISM_SHIFT) * ism + (((y & ISM_MASK) * ism) >> ISM_SHIFT);
1358 static __inline uint64_t
1363 sm = ((uint64_t)m << SM_SHIFT) / 8 / machclk_freq;
1367 static __inline uint64_t
1375 ism = ((uint64_t)machclk_freq << ISM_SHIFT) * 8 / m;
1379 static __inline uint64_t
1384 dx = ((uint64_t)d * machclk_freq) / 1000;
1393 m = (sm * 8 * machclk_freq) >> SM_SHIFT;
1402 d = dx * 1000 / machclk_freq;
1407 sc2isc(struct service_curve *sc, struct internal_sc *isc)
1409 isc->sm1 = m2sm(sc->m1);
1410 isc->ism1 = m2ism(sc->m1);
1411 isc->dx = d2dx(sc->d);
1412 isc->dy = seg_x2y(isc->dx, isc->sm1);
1413 isc->sm2 = m2sm(sc->m2);
1414 isc->ism2 = m2ism(sc->m2);
1418 * initialize the runtime service curve with the given internal
1419 * service curve starting at (x, y).
1422 rtsc_init(struct runtime_sc *rtsc, struct internal_sc *isc, uint64_t x, uint64_t y)
1426 rtsc->sm1 = isc->sm1;
1427 rtsc->ism1 = isc->ism1;
1430 rtsc->sm2 = isc->sm2;
1431 rtsc->ism2 = isc->ism2;
1435 * calculate the y-projection of the runtime service curve by the
1436 * given x-projection value
1439 rtsc_y2x(struct runtime_sc *rtsc, uint64_t y)
1445 } else if (y <= rtsc->y + rtsc->dy) {
1446 /* x belongs to the 1st segment */
1448 x = rtsc->x + rtsc->dx;
1450 x = rtsc->x + seg_y2x(y - rtsc->y, rtsc->ism1);
1452 /* x belongs to the 2nd segment */
1453 x = rtsc->x + rtsc->dx
1454 + seg_y2x(y - rtsc->y - rtsc->dy, rtsc->ism2);
1460 rtsc_x2y(struct runtime_sc *rtsc, uint64_t x)
1466 } else if (x <= rtsc->x + rtsc->dx) {
1467 /* y belongs to the 1st segment */
1468 y = rtsc->y + seg_x2y(x - rtsc->x, rtsc->sm1);
1470 /* y belongs to the 2nd segment */
1471 y = rtsc->y + rtsc->dy
1472 + seg_x2y(x - rtsc->x - rtsc->dx, rtsc->sm2);
1477 * update the runtime service curve by taking the minimum of the current
1478 * runtime service curve and the service curve starting at (x, y).
1481 rtsc_min(struct runtime_sc *rtsc, struct internal_sc *isc, uint64_t x, uint64_t y)
1483 uint64_t y1, y2, dx, dy;
1485 if (isc->sm1 <= isc->sm2) {
1486 /* service curve is convex */
1487 y1 = rtsc_x2y(rtsc, x);
1489 /* the current rtsc is smaller */
1497 * service curve is concave
1498 * compute the two y values of the current rtsc
1502 y1 = rtsc_x2y(rtsc, x);
1504 /* rtsc is below isc, no change to rtsc */
1508 y2 = rtsc_x2y(rtsc, x + isc->dx);
1509 if (y2 >= y + isc->dy) {
1510 /* rtsc is above isc, replace rtsc by isc */
1519 * the two curves intersect
1520 * compute the offsets (dx, dy) using the reverse
1521 * function of seg_x2y()
1522 * seg_x2y(dx, sm1) == seg_x2y(dx, sm2) + (y1 - y)
1524 dx = ((y1 - y) << SM_SHIFT) / (isc->sm1 - isc->sm2);
1526 * check if (x, y1) belongs to the 1st segment of rtsc.
1527 * if so, add the offset.
1529 if (rtsc->x + rtsc->dx > x)
1530 dx += rtsc->x + rtsc->dx - x;
1531 dy = seg_x2y(dx, isc->sm1);
1540 get_class_stats(struct hfsc_classstats *sp, struct hfsc_class *cl)
1542 sp->class_id = cl->cl_id;
1543 sp->class_handle = cl->cl_handle;
1545 if (cl->cl_rsc != NULL) {
1546 sp->rsc.m1 = sm2m(cl->cl_rsc->sm1);
1547 sp->rsc.d = dx2d(cl->cl_rsc->dx);
1548 sp->rsc.m2 = sm2m(cl->cl_rsc->sm2);
1554 if (cl->cl_fsc != NULL) {
1555 sp->fsc.m1 = sm2m(cl->cl_fsc->sm1);
1556 sp->fsc.d = dx2d(cl->cl_fsc->dx);
1557 sp->fsc.m2 = sm2m(cl->cl_fsc->sm2);
1563 if (cl->cl_usc != NULL) {
1564 sp->usc.m1 = sm2m(cl->cl_usc->sm1);
1565 sp->usc.d = dx2d(cl->cl_usc->dx);
1566 sp->usc.m2 = sm2m(cl->cl_usc->sm2);
1573 sp->total = cl->cl_total;
1574 sp->cumul = cl->cl_cumul;
1581 sp->initvt = cl->cl_initvt;
1582 sp->vtperiod = cl->cl_vtperiod;
1583 sp->parentperiod = cl->cl_parentperiod;
1584 sp->nactive = cl->cl_nactive;
1585 sp->vtoff = cl->cl_vtoff;
1586 sp->cvtmax = cl->cl_cvtmax;
1587 sp->myf = cl->cl_myf;
1588 sp->cfmin = cl->cl_cfmin;
1589 sp->cvtmin = cl->cl_cvtmin;
1590 sp->myfadj = cl->cl_myfadj;
1591 sp->vtadj = cl->cl_vtadj;
1593 sp->cur_time = read_machclk();
1594 sp->machclk_freq = machclk_freq;
1596 sp->qlength = qlen(cl->cl_q);
1597 sp->qlimit = qlimit(cl->cl_q);
1598 sp->xmit_cnt = cl->cl_stats.xmit_cnt;
1599 sp->drop_cnt = cl->cl_stats.drop_cnt;
1600 sp->period = cl->cl_stats.period;
1602 sp->qtype = qtype(cl->cl_q);
1604 if (q_is_red(cl->cl_q))
1605 red_getstats(cl->cl_red, &sp->red[0]);
1608 if (q_is_rio(cl->cl_q))
1609 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
1613 /* convert a class handle to the corresponding class pointer */
1614 static struct hfsc_class *
1615 clh_to_clp(struct hfsc_if *hif, uint32_t chandle)
1618 struct hfsc_class *cl;
1623 * first, try optimistically the slot matching the lower bits of
1624 * the handle. if it fails, do the linear table search.
1626 i = chandle % HFSC_MAX_CLASSES;
1627 if ((cl = hif->hif_class_tbl[i]) != NULL && cl->cl_handle == chandle)
1629 for (i = 0; i < HFSC_MAX_CLASSES; i++)
1630 if ((cl = hif->hif_class_tbl[i]) != NULL &&
1631 cl->cl_handle == chandle)
1636 #endif /* ALTQ_HFSC */