2 /* $KAME: altq_hfsc.c,v 1.24 2003/12/05 05:40:46 kjc 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.
45 #if defined(__FreeBSD__) || defined(__NetBSD__)
49 #include "opt_inet6.h"
51 #endif /* __FreeBSD__ || __NetBSD__ */
53 #ifdef ALTQ_HFSC /* hfsc is enabled by ALTQ_HFSC option in opt_altq.h */
55 #include <sys/param.h>
56 #include <sys/malloc.h>
58 #include <sys/socket.h>
59 #include <sys/systm.h>
60 #include <sys/errno.h>
61 #include <sys/queue.h>
62 #if 1 /* ALTQ3_COMPAT */
63 #include <sys/sockio.h>
65 #include <sys/kernel.h>
66 #endif /* ALTQ3_COMPAT */
69 #include <netinet/in.h>
71 #include <net/pfvar.h>
72 #include <altq/altq.h>
73 #include <altq/altq_hfsc.h>
75 #include <altq/altq_conf.h>
81 static int hfsc_clear_interface(struct hfsc_if *);
82 static int hfsc_request(struct ifaltq *, int, void *);
83 static void hfsc_purge(struct hfsc_if *);
84 static struct hfsc_class *hfsc_class_create(struct hfsc_if *,
85 struct service_curve *, struct service_curve *, struct service_curve *,
86 struct hfsc_class *, int, int, int);
87 static int hfsc_class_destroy(struct hfsc_class *);
88 static struct hfsc_class *hfsc_nextclass(struct hfsc_class *);
89 static int hfsc_enqueue(struct ifaltq *, struct mbuf *,
90 struct altq_pktattr *);
91 static struct mbuf *hfsc_dequeue(struct ifaltq *, int);
93 static int hfsc_addq(struct hfsc_class *, struct mbuf *);
94 static struct mbuf *hfsc_getq(struct hfsc_class *);
95 static struct mbuf *hfsc_pollq(struct hfsc_class *);
96 static void hfsc_purgeq(struct hfsc_class *);
98 static void update_cfmin(struct hfsc_class *);
99 static void set_active(struct hfsc_class *, int);
100 static void set_passive(struct hfsc_class *);
102 static void init_ed(struct hfsc_class *, int);
103 static void update_ed(struct hfsc_class *, int);
104 static void update_d(struct hfsc_class *, int);
105 static void init_vf(struct hfsc_class *, int);
106 static void update_vf(struct hfsc_class *, int, u_int64_t);
107 static void ellist_insert(struct hfsc_class *);
108 static void ellist_remove(struct hfsc_class *);
109 static void ellist_update(struct hfsc_class *);
110 struct hfsc_class *hfsc_get_mindl(struct hfsc_if *, u_int64_t);
111 static void actlist_insert(struct hfsc_class *);
112 static void actlist_remove(struct hfsc_class *);
113 static void actlist_update(struct hfsc_class *);
115 static struct hfsc_class *actlist_firstfit(struct hfsc_class *,
118 static __inline u_int64_t seg_x2y(u_int64_t, u_int64_t);
119 static __inline u_int64_t seg_y2x(u_int64_t, u_int64_t);
120 static __inline u_int64_t m2sm(u_int);
121 static __inline u_int64_t m2ism(u_int);
122 static __inline u_int64_t d2dx(u_int);
123 static u_int sm2m(u_int64_t);
124 static u_int dx2d(u_int64_t);
126 static void sc2isc(struct service_curve *, struct internal_sc *);
127 static void rtsc_init(struct runtime_sc *, struct internal_sc *,
128 u_int64_t, u_int64_t);
129 static u_int64_t rtsc_y2x(struct runtime_sc *, u_int64_t);
130 static u_int64_t rtsc_x2y(struct runtime_sc *, u_int64_t);
131 static void rtsc_min(struct runtime_sc *, struct internal_sc *,
132 u_int64_t, u_int64_t);
134 static void get_class_stats(struct hfsc_classstats *,
135 struct hfsc_class *);
136 static struct hfsc_class *clh_to_clp(struct hfsc_if *, u_int32_t);
140 static struct hfsc_if *hfsc_attach(struct ifaltq *, u_int);
141 static int hfsc_detach(struct hfsc_if *);
142 static int hfsc_class_modify(struct hfsc_class *, struct service_curve *,
143 struct service_curve *, struct service_curve *);
145 static int hfsccmd_if_attach(struct hfsc_attach *);
146 static int hfsccmd_if_detach(struct hfsc_interface *);
147 static int hfsccmd_add_class(struct hfsc_add_class *);
148 static int hfsccmd_delete_class(struct hfsc_delete_class *);
149 static int hfsccmd_modify_class(struct hfsc_modify_class *);
150 static int hfsccmd_add_filter(struct hfsc_add_filter *);
151 static int hfsccmd_delete_filter(struct hfsc_delete_filter *);
152 static int hfsccmd_class_stats(struct hfsc_class_stats *);
155 #endif /* ALTQ3_COMPAT */
160 #define is_a_parent_class(cl) ((cl)->cl_children != NULL)
162 #define HT_INFINITY 0xffffffffffffffffLL /* infinite time value */
165 /* hif_list keeps all hfsc_if's allocated. */
166 static struct hfsc_if *hif_list = NULL;
167 #endif /* ALTQ3_COMPAT */
170 hfsc_pfattach(struct pf_altq *a)
175 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
182 error = altq_attach(&ifp->if_snd, ALTQT_HFSC, a->altq_disc,
183 hfsc_enqueue, hfsc_dequeue, hfsc_request, NULL, NULL);
189 hfsc_add_altq(struct pf_altq *a)
194 if ((ifp = ifunit(a->ifname)) == NULL)
196 if (!ALTQ_IS_READY(&ifp->if_snd))
199 hif = malloc(sizeof(struct hfsc_if), M_DEVBUF, M_NOWAIT | M_ZERO);
203 TAILQ_INIT(&hif->hif_eligible);
204 hif->hif_ifq = &ifp->if_snd;
206 /* keep the state in pf_altq */
213 hfsc_remove_altq(struct pf_altq *a)
217 if ((hif = a->altq_disc) == NULL)
221 (void)hfsc_clear_interface(hif);
222 (void)hfsc_class_destroy(hif->hif_rootclass);
230 hfsc_add_queue(struct pf_altq *a)
233 struct hfsc_class *cl, *parent;
234 struct hfsc_opts *opts;
235 struct service_curve rtsc, lssc, ulsc;
237 if ((hif = a->altq_disc) == NULL)
240 opts = &a->pq_u.hfsc_opts;
242 if (a->parent_qid == HFSC_NULLCLASS_HANDLE &&
243 hif->hif_rootclass == NULL)
245 else if ((parent = clh_to_clp(hif, a->parent_qid)) == NULL)
251 if (clh_to_clp(hif, a->qid) != NULL)
254 rtsc.m1 = opts->rtsc_m1;
255 rtsc.d = opts->rtsc_d;
256 rtsc.m2 = opts->rtsc_m2;
257 lssc.m1 = opts->lssc_m1;
258 lssc.d = opts->lssc_d;
259 lssc.m2 = opts->lssc_m2;
260 ulsc.m1 = opts->ulsc_m1;
261 ulsc.d = opts->ulsc_d;
262 ulsc.m2 = opts->ulsc_m2;
264 cl = hfsc_class_create(hif, &rtsc, &lssc, &ulsc,
265 parent, a->qlimit, opts->flags, a->qid);
273 hfsc_remove_queue(struct pf_altq *a)
276 struct hfsc_class *cl;
278 if ((hif = a->altq_disc) == NULL)
281 if ((cl = clh_to_clp(hif, a->qid)) == NULL)
284 return (hfsc_class_destroy(cl));
288 hfsc_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
291 struct hfsc_class *cl;
292 struct hfsc_classstats stats;
295 if ((hif = altq_lookup(a->ifname, ALTQT_HFSC)) == NULL)
298 if ((cl = clh_to_clp(hif, a->qid)) == NULL)
301 if (*nbytes < sizeof(stats))
304 get_class_stats(&stats, cl);
306 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
308 *nbytes = sizeof(stats);
313 * bring the interface back to the initial state by discarding
314 * all the filters and classes except the root class.
317 hfsc_clear_interface(struct hfsc_if *hif)
319 struct hfsc_class *cl;
322 /* free the filters for this interface */
323 acc_discard_filters(&hif->hif_classifier, NULL, 1);
326 /* clear out the classes */
327 while (hif->hif_rootclass != NULL &&
328 (cl = hif->hif_rootclass->cl_children) != NULL) {
330 * remove the first leaf class found in the hierarchy
333 for (; cl != NULL; cl = hfsc_nextclass(cl)) {
334 if (!is_a_parent_class(cl)) {
335 (void)hfsc_class_destroy(cl);
345 hfsc_request(struct ifaltq *ifq, int req, void *arg)
347 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
349 IFQ_LOCK_ASSERT(ifq);
359 /* discard all the queued packets on the interface */
361 hfsc_purge(struct hfsc_if *hif)
363 struct hfsc_class *cl;
365 for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl))
366 if (!qempty(cl->cl_q))
368 if (ALTQ_IS_ENABLED(hif->hif_ifq))
369 hif->hif_ifq->ifq_len = 0;
373 hfsc_class_create(struct hfsc_if *hif, struct service_curve *rsc,
374 struct service_curve *fsc, struct service_curve *usc,
375 struct hfsc_class *parent, int qlimit, int flags, int qid)
377 struct hfsc_class *cl, *p;
380 if (hif->hif_classes >= HFSC_MAX_CLASSES)
384 if (flags & HFCF_RED) {
386 printf("hfsc_class_create: RED not configured for HFSC!\n");
392 cl = malloc(sizeof(struct hfsc_class), M_DEVBUF, M_NOWAIT | M_ZERO);
396 cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF, M_NOWAIT | M_ZERO);
397 if (cl->cl_q == NULL)
400 TAILQ_INIT(&cl->cl_actc);
403 qlimit = 50; /* use default */
404 qlimit(cl->cl_q) = qlimit;
405 qtype(cl->cl_q) = Q_DROPTAIL;
407 cl->cl_flags = flags;
409 if (flags & (HFCF_RED|HFCF_RIO)) {
410 int red_flags, red_pkttime;
414 if (rsc != NULL && rsc->m2 > m2)
416 if (fsc != NULL && fsc->m2 > m2)
418 if (usc != NULL && usc->m2 > m2)
422 if (flags & HFCF_ECN)
423 red_flags |= REDF_ECN;
425 if (flags & HFCF_CLEARDSCP)
426 red_flags |= RIOF_CLEARDSCP;
429 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
431 red_pkttime = (int64_t)hif->hif_ifq->altq_ifp->if_mtu
432 * 1000 * 1000 * 1000 / (m2 / 8);
433 if (flags & HFCF_RED) {
434 cl->cl_red = red_alloc(0, 0,
435 qlimit(cl->cl_q) * 10/100,
436 qlimit(cl->cl_q) * 30/100,
437 red_flags, red_pkttime);
438 if (cl->cl_red != NULL)
439 qtype(cl->cl_q) = Q_RED;
443 cl->cl_red = (red_t *)rio_alloc(0, NULL,
444 red_flags, red_pkttime);
445 if (cl->cl_red != NULL)
446 qtype(cl->cl_q) = Q_RIO;
450 #endif /* ALTQ_RED */
452 if (rsc != NULL && (rsc->m1 != 0 || rsc->m2 != 0)) {
453 cl->cl_rsc = malloc(sizeof(struct internal_sc),
455 if (cl->cl_rsc == NULL)
457 sc2isc(rsc, cl->cl_rsc);
458 rtsc_init(&cl->cl_deadline, cl->cl_rsc, 0, 0);
459 rtsc_init(&cl->cl_eligible, cl->cl_rsc, 0, 0);
461 if (fsc != NULL && (fsc->m1 != 0 || fsc->m2 != 0)) {
462 cl->cl_fsc = malloc(sizeof(struct internal_sc),
464 if (cl->cl_fsc == NULL)
466 sc2isc(fsc, cl->cl_fsc);
467 rtsc_init(&cl->cl_virtual, cl->cl_fsc, 0, 0);
469 if (usc != NULL && (usc->m1 != 0 || usc->m2 != 0)) {
470 cl->cl_usc = malloc(sizeof(struct internal_sc),
472 if (cl->cl_usc == NULL)
474 sc2isc(usc, cl->cl_usc);
475 rtsc_init(&cl->cl_ulimit, cl->cl_usc, 0, 0);
478 cl->cl_id = hif->hif_classid++;
481 cl->cl_parent = parent;
488 IFQ_LOCK(hif->hif_ifq);
492 * find a free slot in the class table. if the slot matching
493 * the lower bits of qid is free, use this slot. otherwise,
494 * use the first free slot.
496 i = qid % HFSC_MAX_CLASSES;
497 if (hif->hif_class_tbl[i] == NULL)
498 hif->hif_class_tbl[i] = cl;
500 for (i = 0; i < HFSC_MAX_CLASSES; i++)
501 if (hif->hif_class_tbl[i] == NULL) {
502 hif->hif_class_tbl[i] = cl;
505 if (i == HFSC_MAX_CLASSES) {
506 IFQ_UNLOCK(hif->hif_ifq);
512 if (flags & HFCF_DEFAULTCLASS)
513 hif->hif_defaultclass = cl;
515 if (parent == NULL) {
516 /* this is root class */
517 hif->hif_rootclass = cl;
519 /* add this class to the children list of the parent */
520 if ((p = parent->cl_children) == NULL)
521 parent->cl_children = cl;
523 while (p->cl_siblings != NULL)
528 IFQ_UNLOCK(hif->hif_ifq);
534 if (cl->cl_red != NULL) {
536 if (q_is_rio(cl->cl_q))
537 rio_destroy((rio_t *)cl->cl_red);
540 if (q_is_red(cl->cl_q))
541 red_destroy(cl->cl_red);
544 if (cl->cl_fsc != NULL)
545 free(cl->cl_fsc, M_DEVBUF);
546 if (cl->cl_rsc != NULL)
547 free(cl->cl_rsc, M_DEVBUF);
548 if (cl->cl_usc != NULL)
549 free(cl->cl_usc, M_DEVBUF);
550 if (cl->cl_q != NULL)
551 free(cl->cl_q, M_DEVBUF);
557 hfsc_class_destroy(struct hfsc_class *cl)
564 if (is_a_parent_class(cl))
572 IFQ_LOCK(cl->cl_hif->hif_ifq);
575 /* delete filters referencing to this class */
576 acc_discard_filters(&cl->cl_hif->hif_classifier, cl, 0);
577 #endif /* ALTQ3_COMPAT */
579 if (!qempty(cl->cl_q))
582 if (cl->cl_parent == NULL) {
583 /* this is root class */
585 struct hfsc_class *p = cl->cl_parent->cl_children;
588 cl->cl_parent->cl_children = cl->cl_siblings;
590 if (p->cl_siblings == cl) {
591 p->cl_siblings = cl->cl_siblings;
594 } while ((p = p->cl_siblings) != NULL);
598 for (i = 0; i < HFSC_MAX_CLASSES; i++)
599 if (cl->cl_hif->hif_class_tbl[i] == cl) {
600 cl->cl_hif->hif_class_tbl[i] = NULL;
604 cl->cl_hif->hif_classes--;
605 IFQ_UNLOCK(cl->cl_hif->hif_ifq);
608 if (cl->cl_red != NULL) {
610 if (q_is_rio(cl->cl_q))
611 rio_destroy((rio_t *)cl->cl_red);
614 if (q_is_red(cl->cl_q))
615 red_destroy(cl->cl_red);
619 IFQ_LOCK(cl->cl_hif->hif_ifq);
620 if (cl == cl->cl_hif->hif_rootclass)
621 cl->cl_hif->hif_rootclass = NULL;
622 if (cl == cl->cl_hif->hif_defaultclass)
623 cl->cl_hif->hif_defaultclass = NULL;
624 IFQ_UNLOCK(cl->cl_hif->hif_ifq);
626 if (cl->cl_usc != NULL)
627 free(cl->cl_usc, M_DEVBUF);
628 if (cl->cl_fsc != NULL)
629 free(cl->cl_fsc, M_DEVBUF);
630 if (cl->cl_rsc != NULL)
631 free(cl->cl_rsc, M_DEVBUF);
632 free(cl->cl_q, M_DEVBUF);
639 * hfsc_nextclass returns the next class in the tree.
641 * for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl))
644 static struct hfsc_class *
645 hfsc_nextclass(struct hfsc_class *cl)
647 if (cl->cl_children != NULL)
648 cl = cl->cl_children;
649 else if (cl->cl_siblings != NULL)
650 cl = cl->cl_siblings;
652 while ((cl = cl->cl_parent) != NULL)
653 if (cl->cl_siblings) {
654 cl = cl->cl_siblings;
663 * hfsc_enqueue is an enqueue function to be registered to
664 * (*altq_enqueue) in struct ifaltq.
667 hfsc_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
669 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
670 struct hfsc_class *cl;
674 IFQ_LOCK_ASSERT(ifq);
676 /* grab class set by classifier */
677 if ((m->m_flags & M_PKTHDR) == 0) {
678 /* should not happen */
679 printf("altq: packet for %s does not have pkthdr\n",
680 ifq->altq_ifp->if_xname);
685 if ((t = pf_find_mtag(m)) != NULL)
686 cl = clh_to_clp(hif, t->qid);
688 else if ((ifq->altq_flags & ALTQF_CLASSIFY) && pktattr != NULL)
689 cl = pktattr->pattr_class;
691 if (cl == NULL || is_a_parent_class(cl)) {
692 cl = hif->hif_defaultclass;
700 cl->cl_pktattr = pktattr; /* save proto hdr used by ECN */
703 cl->cl_pktattr = NULL;
705 if (hfsc_addq(cl, m) != 0) {
706 /* drop occurred. mbuf was freed in hfsc_addq. */
707 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, len);
711 cl->cl_hif->hif_packets++;
713 /* successfully queued. */
714 if (qlen(cl->cl_q) == 1)
715 set_active(cl, m_pktlen(m));
721 * hfsc_dequeue is a dequeue function to be registered to
722 * (*altq_dequeue) in struct ifaltq.
724 * note: ALTDQ_POLL returns the next packet without removing the packet
725 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
726 * ALTDQ_REMOVE must return the same packet if called immediately
730 hfsc_dequeue(struct ifaltq *ifq, int op)
732 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
733 struct hfsc_class *cl;
739 IFQ_LOCK_ASSERT(ifq);
741 if (hif->hif_packets == 0)
742 /* no packet in the tree */
745 cur_time = read_machclk();
747 if (op == ALTDQ_REMOVE && hif->hif_pollcache != NULL) {
749 cl = hif->hif_pollcache;
750 hif->hif_pollcache = NULL;
751 /* check if the class was scheduled by real-time criteria */
752 if (cl->cl_rsc != NULL)
753 realtime = (cl->cl_e <= cur_time);
756 * if there are eligible classes, use real-time criteria.
757 * find the class with the minimum deadline among
758 * the eligible classes.
760 if ((cl = hfsc_get_mindl(hif, cur_time))
768 * use link-sharing criteria
769 * get the class with the minimum vt in the hierarchy
771 cl = hif->hif_rootclass;
772 while (is_a_parent_class(cl)) {
774 cl = actlist_firstfit(cl, cur_time);
778 printf("%d fit but none found\n",fits);
783 * update parent's cl_cvtmin.
784 * don't update if the new vt is smaller.
786 if (cl->cl_parent->cl_cvtmin < cl->cl_vt)
787 cl->cl_parent->cl_cvtmin = cl->cl_vt;
794 if (op == ALTDQ_POLL) {
795 hif->hif_pollcache = cl;
803 panic("hfsc_dequeue:");
805 cl->cl_hif->hif_packets--;
807 PKTCNTR_ADD(&cl->cl_stats.xmit_cnt, len);
809 update_vf(cl, len, cur_time);
813 if (!qempty(cl->cl_q)) {
814 if (cl->cl_rsc != NULL) {
816 next_len = m_pktlen(qhead(cl->cl_q));
819 update_ed(cl, next_len);
821 update_d(cl, next_len);
824 /* the class becomes passive */
832 hfsc_addq(struct hfsc_class *cl, struct mbuf *m)
836 if (q_is_rio(cl->cl_q))
837 return rio_addq((rio_t *)cl->cl_red, cl->cl_q,
841 if (q_is_red(cl->cl_q))
842 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
844 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
849 if (cl->cl_flags & HFCF_CLEARDSCP)
850 write_dsfield(m, cl->cl_pktattr, 0);
858 hfsc_getq(struct hfsc_class *cl)
861 if (q_is_rio(cl->cl_q))
862 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
865 if (q_is_red(cl->cl_q))
866 return red_getq(cl->cl_red, cl->cl_q);
868 return _getq(cl->cl_q);
872 hfsc_pollq(struct hfsc_class *cl)
874 return qhead(cl->cl_q);
878 hfsc_purgeq(struct hfsc_class *cl)
882 if (qempty(cl->cl_q))
885 while ((m = _getq(cl->cl_q)) != NULL) {
886 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, m_pktlen(m));
888 cl->cl_hif->hif_packets--;
889 IFQ_DEC_LEN(cl->cl_hif->hif_ifq);
891 ASSERT(qlen(cl->cl_q) == 0);
893 update_vf(cl, 0, 0); /* remove cl from the actlist */
898 set_active(struct hfsc_class *cl, int len)
900 if (cl->cl_rsc != NULL)
902 if (cl->cl_fsc != NULL)
905 cl->cl_stats.period++;
909 set_passive(struct hfsc_class *cl)
911 if (cl->cl_rsc != NULL)
915 * actlist is now handled in update_vf() so that update_vf(cl, 0, 0)
916 * needs to be called explicitly to remove a class from actlist
921 init_ed(struct hfsc_class *cl, int next_len)
925 cur_time = read_machclk();
927 /* update the deadline curve */
928 rtsc_min(&cl->cl_deadline, cl->cl_rsc, cur_time, cl->cl_cumul);
931 * update the eligible curve.
932 * for concave, it is equal to the deadline curve.
933 * for convex, it is a linear curve with slope m2.
935 cl->cl_eligible = cl->cl_deadline;
936 if (cl->cl_rsc->sm1 <= cl->cl_rsc->sm2) {
937 cl->cl_eligible.dx = 0;
938 cl->cl_eligible.dy = 0;
941 /* compute e and d */
942 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
943 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
949 update_ed(struct hfsc_class *cl, int next_len)
951 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
952 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
958 update_d(struct hfsc_class *cl, int next_len)
960 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
964 init_vf(struct hfsc_class *cl, int len)
966 struct hfsc_class *max_cl, *p;
967 u_int64_t vt, f, cur_time;
972 for ( ; cl->cl_parent != NULL; cl = cl->cl_parent) {
974 if (go_active && cl->cl_nactive++ == 0)
980 max_cl = TAILQ_LAST(&cl->cl_parent->cl_actc, acthead);
981 if (max_cl != NULL) {
983 * set vt to the average of the min and max
984 * classes. if the parent's period didn't
985 * change, don't decrease vt of the class.
988 if (cl->cl_parent->cl_cvtmin != 0)
989 vt = (cl->cl_parent->cl_cvtmin + vt)/2;
991 if (cl->cl_parent->cl_vtperiod !=
992 cl->cl_parentperiod || vt > cl->cl_vt)
996 * first child for a new parent backlog period.
997 * add parent's cvtmax to vtoff of children
998 * to make a new vt (vtoff + vt) larger than
999 * the vt in the last period for all children.
1001 vt = cl->cl_parent->cl_cvtmax;
1002 for (p = cl->cl_parent->cl_children; p != NULL;
1006 cl->cl_parent->cl_cvtmax = 0;
1007 cl->cl_parent->cl_cvtmin = 0;
1009 cl->cl_initvt = cl->cl_vt;
1011 /* update the virtual curve */
1012 vt = cl->cl_vt + cl->cl_vtoff;
1013 rtsc_min(&cl->cl_virtual, cl->cl_fsc, vt, cl->cl_total);
1014 if (cl->cl_virtual.x == vt) {
1015 cl->cl_virtual.x -= cl->cl_vtoff;
1020 cl->cl_vtperiod++; /* increment vt period */
1021 cl->cl_parentperiod = cl->cl_parent->cl_vtperiod;
1022 if (cl->cl_parent->cl_nactive == 0)
1023 cl->cl_parentperiod++;
1028 if (cl->cl_usc != NULL) {
1029 /* class has upper limit curve */
1031 cur_time = read_machclk();
1033 /* update the ulimit curve */
1034 rtsc_min(&cl->cl_ulimit, cl->cl_usc, cur_time,
1037 cl->cl_myf = rtsc_y2x(&cl->cl_ulimit,
1043 if (cl->cl_myf > cl->cl_cfmin)
1047 if (f != cl->cl_f) {
1049 update_cfmin(cl->cl_parent);
1055 update_vf(struct hfsc_class *cl, int len, u_int64_t cur_time)
1057 u_int64_t f, myf_bound, delta;
1060 go_passive = qempty(cl->cl_q);
1062 for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
1064 cl->cl_total += len;
1066 if (cl->cl_fsc == NULL || cl->cl_nactive == 0)
1069 if (go_passive && --cl->cl_nactive == 0)
1075 /* no more active child, going passive */
1077 /* update cvtmax of the parent class */
1078 if (cl->cl_vt > cl->cl_parent->cl_cvtmax)
1079 cl->cl_parent->cl_cvtmax = cl->cl_vt;
1081 /* remove this class from the vt list */
1084 update_cfmin(cl->cl_parent);
1092 cl->cl_vt = rtsc_y2x(&cl->cl_virtual, cl->cl_total)
1093 - cl->cl_vtoff + cl->cl_vtadj;
1096 * if vt of the class is smaller than cvtmin,
1097 * the class was skipped in the past due to non-fit.
1098 * if so, we need to adjust vtadj.
1100 if (cl->cl_vt < cl->cl_parent->cl_cvtmin) {
1101 cl->cl_vtadj += cl->cl_parent->cl_cvtmin - cl->cl_vt;
1102 cl->cl_vt = cl->cl_parent->cl_cvtmin;
1105 /* update the vt list */
1108 if (cl->cl_usc != NULL) {
1109 cl->cl_myf = cl->cl_myfadj
1110 + rtsc_y2x(&cl->cl_ulimit, cl->cl_total);
1113 * if myf lags behind by more than one clock tick
1114 * from the current time, adjust myfadj to prevent
1115 * a rate-limited class from going greedy.
1116 * in a steady state under rate-limiting, myf
1117 * fluctuates within one clock tick.
1119 myf_bound = cur_time - machclk_per_tick;
1120 if (cl->cl_myf < myf_bound) {
1121 delta = cur_time - cl->cl_myf;
1122 cl->cl_myfadj += delta;
1123 cl->cl_myf += delta;
1127 /* cl_f is max(cl_myf, cl_cfmin) */
1128 if (cl->cl_myf > cl->cl_cfmin)
1132 if (f != cl->cl_f) {
1134 update_cfmin(cl->cl_parent);
1140 update_cfmin(struct hfsc_class *cl)
1142 struct hfsc_class *p;
1145 if (TAILQ_EMPTY(&cl->cl_actc)) {
1149 cfmin = HT_INFINITY;
1150 TAILQ_FOREACH(p, &cl->cl_actc, cl_actlist) {
1155 if (p->cl_f < cfmin)
1158 cl->cl_cfmin = cfmin;
1162 * TAILQ based ellist and actlist implementation
1163 * (ion wanted to make a calendar queue based implementation)
1166 * eligible list holds backlogged classes being sorted by their eligible times.
1167 * there is one eligible list per interface.
1171 ellist_insert(struct hfsc_class *cl)
1173 struct hfsc_if *hif = cl->cl_hif;
1174 struct hfsc_class *p;
1176 /* check the last entry first */
1177 if ((p = TAILQ_LAST(&hif->hif_eligible, elighead)) == NULL ||
1178 p->cl_e <= cl->cl_e) {
1179 TAILQ_INSERT_TAIL(&hif->hif_eligible, cl, cl_ellist);
1183 TAILQ_FOREACH(p, &hif->hif_eligible, cl_ellist) {
1184 if (cl->cl_e < p->cl_e) {
1185 TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1189 ASSERT(0); /* should not reach here */
1193 ellist_remove(struct hfsc_class *cl)
1195 struct hfsc_if *hif = cl->cl_hif;
1197 TAILQ_REMOVE(&hif->hif_eligible, cl, cl_ellist);
1201 ellist_update(struct hfsc_class *cl)
1203 struct hfsc_if *hif = cl->cl_hif;
1204 struct hfsc_class *p, *last;
1207 * the eligible time of a class increases monotonically.
1208 * if the next entry has a larger eligible time, nothing to do.
1210 p = TAILQ_NEXT(cl, cl_ellist);
1211 if (p == NULL || cl->cl_e <= p->cl_e)
1214 /* check the last entry */
1215 last = TAILQ_LAST(&hif->hif_eligible, elighead);
1216 ASSERT(last != NULL);
1217 if (last->cl_e <= cl->cl_e) {
1218 TAILQ_REMOVE(&hif->hif_eligible, cl, cl_ellist);
1219 TAILQ_INSERT_TAIL(&hif->hif_eligible, cl, cl_ellist);
1224 * the new position must be between the next entry
1225 * and the last entry
1227 while ((p = TAILQ_NEXT(p, cl_ellist)) != NULL) {
1228 if (cl->cl_e < p->cl_e) {
1229 TAILQ_REMOVE(&hif->hif_eligible, cl, cl_ellist);
1230 TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1234 ASSERT(0); /* should not reach here */
1237 /* find the class with the minimum deadline among the eligible classes */
1239 hfsc_get_mindl(struct hfsc_if *hif, u_int64_t cur_time)
1241 struct hfsc_class *p, *cl = NULL;
1243 TAILQ_FOREACH(p, &hif->hif_eligible, cl_ellist) {
1244 if (p->cl_e > cur_time)
1246 if (cl == NULL || p->cl_d < cl->cl_d)
1253 * active children list holds backlogged child classes being sorted
1254 * by their virtual time.
1255 * each intermediate class has one active children list.
1259 actlist_insert(struct hfsc_class *cl)
1261 struct hfsc_class *p;
1263 /* check the last entry first */
1264 if ((p = TAILQ_LAST(&cl->cl_parent->cl_actc, acthead)) == NULL
1265 || p->cl_vt <= cl->cl_vt) {
1266 TAILQ_INSERT_TAIL(&cl->cl_parent->cl_actc, cl, cl_actlist);
1270 TAILQ_FOREACH(p, &cl->cl_parent->cl_actc, cl_actlist) {
1271 if (cl->cl_vt < p->cl_vt) {
1272 TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1276 ASSERT(0); /* should not reach here */
1280 actlist_remove(struct hfsc_class *cl)
1282 TAILQ_REMOVE(&cl->cl_parent->cl_actc, cl, cl_actlist);
1286 actlist_update(struct hfsc_class *cl)
1288 struct hfsc_class *p, *last;
1291 * the virtual time of a class increases monotonically during its
1292 * backlogged period.
1293 * if the next entry has a larger virtual time, nothing to do.
1295 p = TAILQ_NEXT(cl, cl_actlist);
1296 if (p == NULL || cl->cl_vt < p->cl_vt)
1299 /* check the last entry */
1300 last = TAILQ_LAST(&cl->cl_parent->cl_actc, acthead);
1301 ASSERT(last != NULL);
1302 if (last->cl_vt <= cl->cl_vt) {
1303 TAILQ_REMOVE(&cl->cl_parent->cl_actc, cl, cl_actlist);
1304 TAILQ_INSERT_TAIL(&cl->cl_parent->cl_actc, cl, cl_actlist);
1309 * the new position must be between the next entry
1310 * and the last entry
1312 while ((p = TAILQ_NEXT(p, cl_actlist)) != NULL) {
1313 if (cl->cl_vt < p->cl_vt) {
1314 TAILQ_REMOVE(&cl->cl_parent->cl_actc, cl, cl_actlist);
1315 TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1319 ASSERT(0); /* should not reach here */
1322 static struct hfsc_class *
1323 actlist_firstfit(struct hfsc_class *cl, u_int64_t cur_time)
1325 struct hfsc_class *p;
1327 TAILQ_FOREACH(p, &cl->cl_actc, cl_actlist) {
1328 if (p->cl_f <= cur_time)
1335 * service curve support functions
1337 * external service curve parameters
1340 * internal service curve parameters
1341 * sm: (bytes/tsc_interval) << SM_SHIFT
1342 * ism: (tsc_count/byte) << ISM_SHIFT
1345 * SM_SHIFT and ISM_SHIFT are scaled in order to keep effective digits.
1346 * we should be able to handle 100K-1Gbps linkspeed with 200Hz-1GHz CPU
1347 * speed. SM_SHIFT and ISM_SHIFT are selected to have at least 3 effective
1348 * digits in decimal using the following table.
1350 * bits/sec 100Kbps 1Mbps 10Mbps 100Mbps 1Gbps
1351 * ----------+-------------------------------------------------------
1352 * bytes/nsec 12.5e-6 125e-6 1250e-6 12500e-6 125000e-6
1353 * sm(500MHz) 25.0e-6 250e-6 2500e-6 25000e-6 250000e-6
1354 * sm(200MHz) 62.5e-6 625e-6 6250e-6 62500e-6 625000e-6
1356 * nsec/byte 80000 8000 800 80 8
1357 * ism(500MHz) 40000 4000 400 40 4
1358 * ism(200MHz) 16000 1600 160 16 1.6
1361 #define ISM_SHIFT 10
1363 #define SM_MASK ((1LL << SM_SHIFT) - 1)
1364 #define ISM_MASK ((1LL << ISM_SHIFT) - 1)
1366 static __inline u_int64_t
1367 seg_x2y(u_int64_t x, u_int64_t sm)
1373 * y = x * sm >> SM_SHIFT
1374 * but divide it for the upper and lower bits to avoid overflow
1376 y = (x >> SM_SHIFT) * sm + (((x & SM_MASK) * sm) >> SM_SHIFT);
1380 static __inline u_int64_t
1381 seg_y2x(u_int64_t y, u_int64_t ism)
1387 else if (ism == HT_INFINITY)
1390 x = (y >> ISM_SHIFT) * ism
1391 + (((y & ISM_MASK) * ism) >> ISM_SHIFT);
1396 static __inline u_int64_t
1401 sm = ((u_int64_t)m << SM_SHIFT) / 8 / machclk_freq;
1405 static __inline u_int64_t
1413 ism = ((u_int64_t)machclk_freq << ISM_SHIFT) * 8 / m;
1417 static __inline u_int64_t
1422 dx = ((u_int64_t)d * machclk_freq) / 1000;
1431 m = (sm * 8 * machclk_freq) >> SM_SHIFT;
1440 d = dx * 1000 / machclk_freq;
1445 sc2isc(struct service_curve *sc, struct internal_sc *isc)
1447 isc->sm1 = m2sm(sc->m1);
1448 isc->ism1 = m2ism(sc->m1);
1449 isc->dx = d2dx(sc->d);
1450 isc->dy = seg_x2y(isc->dx, isc->sm1);
1451 isc->sm2 = m2sm(sc->m2);
1452 isc->ism2 = m2ism(sc->m2);
1456 * initialize the runtime service curve with the given internal
1457 * service curve starting at (x, y).
1460 rtsc_init(struct runtime_sc *rtsc, struct internal_sc * isc, u_int64_t x,
1465 rtsc->sm1 = isc->sm1;
1466 rtsc->ism1 = isc->ism1;
1469 rtsc->sm2 = isc->sm2;
1470 rtsc->ism2 = isc->ism2;
1474 * calculate the y-projection of the runtime service curve by the
1475 * given x-projection value
1478 rtsc_y2x(struct runtime_sc *rtsc, u_int64_t y)
1484 else if (y <= rtsc->y + rtsc->dy) {
1485 /* x belongs to the 1st segment */
1487 x = rtsc->x + rtsc->dx;
1489 x = rtsc->x + seg_y2x(y - rtsc->y, rtsc->ism1);
1491 /* x belongs to the 2nd segment */
1492 x = rtsc->x + rtsc->dx
1493 + seg_y2x(y - rtsc->y - rtsc->dy, rtsc->ism2);
1499 rtsc_x2y(struct runtime_sc *rtsc, u_int64_t x)
1505 else if (x <= rtsc->x + rtsc->dx)
1506 /* y belongs to the 1st segment */
1507 y = rtsc->y + seg_x2y(x - rtsc->x, rtsc->sm1);
1509 /* y belongs to the 2nd segment */
1510 y = rtsc->y + rtsc->dy
1511 + seg_x2y(x - rtsc->x - rtsc->dx, rtsc->sm2);
1516 * update the runtime service curve by taking the minimum of the current
1517 * runtime service curve and the service curve starting at (x, y).
1520 rtsc_min(struct runtime_sc *rtsc, struct internal_sc *isc, u_int64_t x,
1523 u_int64_t y1, y2, dx, dy;
1525 if (isc->sm1 <= isc->sm2) {
1526 /* service curve is convex */
1527 y1 = rtsc_x2y(rtsc, x);
1529 /* the current rtsc is smaller */
1537 * service curve is concave
1538 * compute the two y values of the current rtsc
1542 y1 = rtsc_x2y(rtsc, x);
1544 /* rtsc is below isc, no change to rtsc */
1548 y2 = rtsc_x2y(rtsc, x + isc->dx);
1549 if (y2 >= y + isc->dy) {
1550 /* rtsc is above isc, replace rtsc by isc */
1559 * the two curves intersect
1560 * compute the offsets (dx, dy) using the reverse
1561 * function of seg_x2y()
1562 * seg_x2y(dx, sm1) == seg_x2y(dx, sm2) + (y1 - y)
1564 dx = ((y1 - y) << SM_SHIFT) / (isc->sm1 - isc->sm2);
1566 * check if (x, y1) belongs to the 1st segment of rtsc.
1567 * if so, add the offset.
1569 if (rtsc->x + rtsc->dx > x)
1570 dx += rtsc->x + rtsc->dx - x;
1571 dy = seg_x2y(dx, isc->sm1);
1581 get_class_stats(struct hfsc_classstats *sp, struct hfsc_class *cl)
1583 sp->class_id = cl->cl_id;
1584 sp->class_handle = cl->cl_handle;
1586 if (cl->cl_rsc != NULL) {
1587 sp->rsc.m1 = sm2m(cl->cl_rsc->sm1);
1588 sp->rsc.d = dx2d(cl->cl_rsc->dx);
1589 sp->rsc.m2 = sm2m(cl->cl_rsc->sm2);
1595 if (cl->cl_fsc != NULL) {
1596 sp->fsc.m1 = sm2m(cl->cl_fsc->sm1);
1597 sp->fsc.d = dx2d(cl->cl_fsc->dx);
1598 sp->fsc.m2 = sm2m(cl->cl_fsc->sm2);
1604 if (cl->cl_usc != NULL) {
1605 sp->usc.m1 = sm2m(cl->cl_usc->sm1);
1606 sp->usc.d = dx2d(cl->cl_usc->dx);
1607 sp->usc.m2 = sm2m(cl->cl_usc->sm2);
1614 sp->total = cl->cl_total;
1615 sp->cumul = cl->cl_cumul;
1622 sp->initvt = cl->cl_initvt;
1623 sp->vtperiod = cl->cl_vtperiod;
1624 sp->parentperiod = cl->cl_parentperiod;
1625 sp->nactive = cl->cl_nactive;
1626 sp->vtoff = cl->cl_vtoff;
1627 sp->cvtmax = cl->cl_cvtmax;
1628 sp->myf = cl->cl_myf;
1629 sp->cfmin = cl->cl_cfmin;
1630 sp->cvtmin = cl->cl_cvtmin;
1631 sp->myfadj = cl->cl_myfadj;
1632 sp->vtadj = cl->cl_vtadj;
1634 sp->cur_time = read_machclk();
1635 sp->machclk_freq = machclk_freq;
1637 sp->qlength = qlen(cl->cl_q);
1638 sp->qlimit = qlimit(cl->cl_q);
1639 sp->xmit_cnt = cl->cl_stats.xmit_cnt;
1640 sp->drop_cnt = cl->cl_stats.drop_cnt;
1641 sp->period = cl->cl_stats.period;
1643 sp->qtype = qtype(cl->cl_q);
1645 if (q_is_red(cl->cl_q))
1646 red_getstats(cl->cl_red, &sp->red[0]);
1649 if (q_is_rio(cl->cl_q))
1650 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
1654 /* convert a class handle to the corresponding class pointer */
1655 static struct hfsc_class *
1656 clh_to_clp(struct hfsc_if *hif, u_int32_t chandle)
1659 struct hfsc_class *cl;
1664 * first, try optimistically the slot matching the lower bits of
1665 * the handle. if it fails, do the linear table search.
1667 i = chandle % HFSC_MAX_CLASSES;
1668 if ((cl = hif->hif_class_tbl[i]) != NULL && cl->cl_handle == chandle)
1670 for (i = 0; i < HFSC_MAX_CLASSES; i++)
1671 if ((cl = hif->hif_class_tbl[i]) != NULL &&
1672 cl->cl_handle == chandle)
1678 static struct hfsc_if *
1679 hfsc_attach(ifq, bandwidth)
1683 struct hfsc_if *hif;
1685 hif = malloc(sizeof(struct hfsc_if), M_DEVBUF, M_WAITOK);
1688 bzero(hif, sizeof(struct hfsc_if));
1690 hif->hif_eligible = ellist_alloc();
1691 if (hif->hif_eligible == NULL) {
1692 free(hif, M_DEVBUF);
1698 /* add this state to the hfsc list */
1699 hif->hif_next = hif_list;
1707 struct hfsc_if *hif;
1709 (void)hfsc_clear_interface(hif);
1710 (void)hfsc_class_destroy(hif->hif_rootclass);
1712 /* remove this interface from the hif list */
1713 if (hif_list == hif)
1714 hif_list = hif->hif_next;
1718 for (h = hif_list; h != NULL; h = h->hif_next)
1719 if (h->hif_next == hif) {
1720 h->hif_next = hif->hif_next;
1726 ellist_destroy(hif->hif_eligible);
1728 free(hif, M_DEVBUF);
1734 hfsc_class_modify(cl, rsc, fsc, usc)
1735 struct hfsc_class *cl;
1736 struct service_curve *rsc, *fsc, *usc;
1738 struct internal_sc *rsc_tmp, *fsc_tmp, *usc_tmp;
1742 rsc_tmp = fsc_tmp = usc_tmp = NULL;
1743 if (rsc != NULL && (rsc->m1 != 0 || rsc->m2 != 0) &&
1744 cl->cl_rsc == NULL) {
1745 rsc_tmp = malloc(sizeof(struct internal_sc),
1746 M_DEVBUF, M_WAITOK);
1747 if (rsc_tmp == NULL)
1750 if (fsc != NULL && (fsc->m1 != 0 || fsc->m2 != 0) &&
1751 cl->cl_fsc == NULL) {
1752 fsc_tmp = malloc(sizeof(struct internal_sc),
1753 M_DEVBUF, M_WAITOK);
1754 if (fsc_tmp == NULL) {
1759 if (usc != NULL && (usc->m1 != 0 || usc->m2 != 0) &&
1760 cl->cl_usc == NULL) {
1761 usc_tmp = malloc(sizeof(struct internal_sc),
1762 M_DEVBUF, M_WAITOK);
1763 if (usc_tmp == NULL) {
1770 cur_time = read_machclk();
1776 IFQ_LOCK(cl->cl_hif->hif_ifq);
1779 if (rsc->m1 == 0 && rsc->m2 == 0) {
1780 if (cl->cl_rsc != NULL) {
1781 if (!qempty(cl->cl_q))
1783 free(cl->cl_rsc, M_DEVBUF);
1787 if (cl->cl_rsc == NULL)
1788 cl->cl_rsc = rsc_tmp;
1789 sc2isc(rsc, cl->cl_rsc);
1790 rtsc_init(&cl->cl_deadline, cl->cl_rsc, cur_time,
1792 cl->cl_eligible = cl->cl_deadline;
1793 if (cl->cl_rsc->sm1 <= cl->cl_rsc->sm2) {
1794 cl->cl_eligible.dx = 0;
1795 cl->cl_eligible.dy = 0;
1801 if (fsc->m1 == 0 && fsc->m2 == 0) {
1802 if (cl->cl_fsc != NULL) {
1803 if (!qempty(cl->cl_q))
1805 free(cl->cl_fsc, M_DEVBUF);
1809 if (cl->cl_fsc == NULL)
1810 cl->cl_fsc = fsc_tmp;
1811 sc2isc(fsc, cl->cl_fsc);
1812 rtsc_init(&cl->cl_virtual, cl->cl_fsc, cl->cl_vt,
1818 if (usc->m1 == 0 && usc->m2 == 0) {
1819 if (cl->cl_usc != NULL) {
1820 free(cl->cl_usc, M_DEVBUF);
1825 if (cl->cl_usc == NULL)
1826 cl->cl_usc = usc_tmp;
1827 sc2isc(usc, cl->cl_usc);
1828 rtsc_init(&cl->cl_ulimit, cl->cl_usc, cur_time,
1833 if (!qempty(cl->cl_q)) {
1834 if (cl->cl_rsc != NULL)
1835 update_ed(cl, m_pktlen(qhead(cl->cl_q)));
1836 if (cl->cl_fsc != NULL)
1837 update_vf(cl, 0, cur_time);
1838 /* is this enough? */
1841 IFQ_UNLOCK(cl->cl_hif->hif_ifq);
1848 * hfsc device interface
1851 hfscopen(dev, flag, fmt, p)
1854 #if (__FreeBSD_version > 500000)
1860 if (machclk_freq == 0)
1863 if (machclk_freq == 0) {
1864 printf("hfsc: no cpu clock available!\n");
1868 /* everything will be done when the queueing scheme is attached. */
1873 hfscclose(dev, flag, fmt, p)
1876 #if (__FreeBSD_version > 500000)
1882 struct hfsc_if *hif;
1885 while ((hif = hif_list) != NULL) {
1887 if (ALTQ_IS_ENABLED(hif->hif_ifq))
1888 altq_disable(hif->hif_ifq);
1890 err = altq_detach(hif->hif_ifq);
1892 err = hfsc_detach(hif);
1893 if (err != 0 && error == 0)
1901 hfscioctl(dev, cmd, addr, flag, p)
1906 #if (__FreeBSD_version > 500000)
1912 struct hfsc_if *hif;
1913 struct hfsc_interface *ifacep;
1916 /* check super-user privilege */
1921 #if (__FreeBSD_version > 700000)
1922 if ((error = priv_check(p, PRIV_ALTQ_MANAGE)) != 0)
1924 #elsif (__FreeBSD_version > 400000)
1925 if ((error = suser(p)) != 0)
1928 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1936 case HFSC_IF_ATTACH:
1937 error = hfsccmd_if_attach((struct hfsc_attach *)addr);
1940 case HFSC_IF_DETACH:
1941 error = hfsccmd_if_detach((struct hfsc_interface *)addr);
1946 case HFSC_CLEAR_HIERARCHY:
1947 ifacep = (struct hfsc_interface *)addr;
1948 if ((hif = altq_lookup(ifacep->hfsc_ifname,
1949 ALTQT_HFSC)) == NULL) {
1957 if (hif->hif_defaultclass == NULL) {
1959 printf("hfsc: no default class\n");
1964 error = altq_enable(hif->hif_ifq);
1968 error = altq_disable(hif->hif_ifq);
1971 case HFSC_CLEAR_HIERARCHY:
1972 hfsc_clear_interface(hif);
1977 case HFSC_ADD_CLASS:
1978 error = hfsccmd_add_class((struct hfsc_add_class *)addr);
1981 case HFSC_DEL_CLASS:
1982 error = hfsccmd_delete_class((struct hfsc_delete_class *)addr);
1985 case HFSC_MOD_CLASS:
1986 error = hfsccmd_modify_class((struct hfsc_modify_class *)addr);
1989 case HFSC_ADD_FILTER:
1990 error = hfsccmd_add_filter((struct hfsc_add_filter *)addr);
1993 case HFSC_DEL_FILTER:
1994 error = hfsccmd_delete_filter((struct hfsc_delete_filter *)addr);
1998 error = hfsccmd_class_stats((struct hfsc_class_stats *)addr);
2009 hfsccmd_if_attach(ap)
2010 struct hfsc_attach *ap;
2012 struct hfsc_if *hif;
2016 if ((ifp = ifunit(ap->iface.hfsc_ifname)) == NULL)
2019 if ((hif = hfsc_attach(&ifp->if_snd, ap->bandwidth)) == NULL)
2023 * set HFSC to this ifnet structure.
2025 if ((error = altq_attach(&ifp->if_snd, ALTQT_HFSC, hif,
2026 hfsc_enqueue, hfsc_dequeue, hfsc_request,
2027 &hif->hif_classifier, acc_classify)) != 0)
2028 (void)hfsc_detach(hif);
2034 hfsccmd_if_detach(ap)
2035 struct hfsc_interface *ap;
2037 struct hfsc_if *hif;
2040 if ((hif = altq_lookup(ap->hfsc_ifname, ALTQT_HFSC)) == NULL)
2043 if (ALTQ_IS_ENABLED(hif->hif_ifq))
2044 altq_disable(hif->hif_ifq);
2046 if ((error = altq_detach(hif->hif_ifq)))
2049 return hfsc_detach(hif);
2053 hfsccmd_add_class(ap)
2054 struct hfsc_add_class *ap;
2056 struct hfsc_if *hif;
2057 struct hfsc_class *cl, *parent;
2060 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2063 if (ap->parent_handle == HFSC_NULLCLASS_HANDLE &&
2064 hif->hif_rootclass == NULL)
2066 else if ((parent = clh_to_clp(hif, ap->parent_handle)) == NULL)
2069 /* assign a class handle (use a free slot number for now) */
2070 for (i = 1; i < HFSC_MAX_CLASSES; i++)
2071 if (hif->hif_class_tbl[i] == NULL)
2073 if (i == HFSC_MAX_CLASSES)
2076 if ((cl = hfsc_class_create(hif, &ap->service_curve, NULL, NULL,
2077 parent, ap->qlimit, ap->flags, i)) == NULL)
2080 /* return a class handle to the user */
2081 ap->class_handle = i;
2087 hfsccmd_delete_class(ap)
2088 struct hfsc_delete_class *ap;
2090 struct hfsc_if *hif;
2091 struct hfsc_class *cl;
2093 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2096 if ((cl = clh_to_clp(hif, ap->class_handle)) == NULL)
2099 return hfsc_class_destroy(cl);
2103 hfsccmd_modify_class(ap)
2104 struct hfsc_modify_class *ap;
2106 struct hfsc_if *hif;
2107 struct hfsc_class *cl;
2108 struct service_curve *rsc = NULL;
2109 struct service_curve *fsc = NULL;
2110 struct service_curve *usc = NULL;
2112 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2115 if ((cl = clh_to_clp(hif, ap->class_handle)) == NULL)
2118 if (ap->sctype & HFSC_REALTIMESC)
2119 rsc = &ap->service_curve;
2120 if (ap->sctype & HFSC_LINKSHARINGSC)
2121 fsc = &ap->service_curve;
2122 if (ap->sctype & HFSC_UPPERLIMITSC)
2123 usc = &ap->service_curve;
2125 return hfsc_class_modify(cl, rsc, fsc, usc);
2129 hfsccmd_add_filter(ap)
2130 struct hfsc_add_filter *ap;
2132 struct hfsc_if *hif;
2133 struct hfsc_class *cl;
2135 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2138 if ((cl = clh_to_clp(hif, ap->class_handle)) == NULL)
2141 if (is_a_parent_class(cl)) {
2143 printf("hfsccmd_add_filter: not a leaf class!\n");
2148 return acc_add_filter(&hif->hif_classifier, &ap->filter,
2149 cl, &ap->filter_handle);
2153 hfsccmd_delete_filter(ap)
2154 struct hfsc_delete_filter *ap;
2156 struct hfsc_if *hif;
2158 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2161 return acc_delete_filter(&hif->hif_classifier,
2166 hfsccmd_class_stats(ap)
2167 struct hfsc_class_stats *ap;
2169 struct hfsc_if *hif;
2170 struct hfsc_class *cl;
2171 struct hfsc_classstats stats, *usp;
2172 int n, nclasses, error;
2174 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2177 ap->cur_time = read_machclk();
2178 ap->machclk_freq = machclk_freq;
2179 ap->hif_classes = hif->hif_classes;
2180 ap->hif_packets = hif->hif_packets;
2182 /* skip the first N classes in the tree */
2183 nclasses = ap->nskip;
2184 for (cl = hif->hif_rootclass, n = 0; cl != NULL && n < nclasses;
2185 cl = hfsc_nextclass(cl), n++)
2190 /* then, read the next N classes in the tree */
2191 nclasses = ap->nclasses;
2193 for (n = 0; cl != NULL && n < nclasses; cl = hfsc_nextclass(cl), n++) {
2195 get_class_stats(&stats, cl);
2197 if ((error = copyout((caddr_t)&stats, (caddr_t)usp++,
2198 sizeof(stats))) != 0)
2209 static struct altqsw hfsc_sw =
2210 {"hfsc", hfscopen, hfscclose, hfscioctl};
2212 ALTQ_MODULE(altq_hfsc, ALTQT_HFSC, &hfsc_sw);
2213 MODULE_DEPEND(altq_hfsc, altq_red, 1, 1, 1);
2214 MODULE_DEPEND(altq_hfsc, altq_rio, 1, 1, 1);
2216 #endif /* KLD_MODULE */
2217 #endif /* ALTQ3_COMPAT */
2219 #endif /* ALTQ_HFSC */