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 <net/if_var.h>
70 #include <netinet/in.h>
72 #include <netpfil/pf/pf.h>
73 #include <netpfil/pf/pf_altq.h>
74 #include <netpfil/pf/pf_mtag.h>
75 #include <altq/altq.h>
76 #include <altq/altq_hfsc.h>
78 #include <altq/altq_conf.h>
84 static int hfsc_clear_interface(struct hfsc_if *);
85 static int hfsc_request(struct ifaltq *, int, void *);
86 static void hfsc_purge(struct hfsc_if *);
87 static struct hfsc_class *hfsc_class_create(struct hfsc_if *,
88 struct service_curve *, struct service_curve *, struct service_curve *,
89 struct hfsc_class *, int, int, int);
90 static int hfsc_class_destroy(struct hfsc_class *);
91 static struct hfsc_class *hfsc_nextclass(struct hfsc_class *);
92 static int hfsc_enqueue(struct ifaltq *, struct mbuf *,
93 struct altq_pktattr *);
94 static struct mbuf *hfsc_dequeue(struct ifaltq *, int);
96 static int hfsc_addq(struct hfsc_class *, struct mbuf *);
97 static struct mbuf *hfsc_getq(struct hfsc_class *);
98 static struct mbuf *hfsc_pollq(struct hfsc_class *);
99 static void hfsc_purgeq(struct hfsc_class *);
101 static void update_cfmin(struct hfsc_class *);
102 static void set_active(struct hfsc_class *, int);
103 static void set_passive(struct hfsc_class *);
105 static void init_ed(struct hfsc_class *, int);
106 static void update_ed(struct hfsc_class *, int);
107 static void update_d(struct hfsc_class *, int);
108 static void init_vf(struct hfsc_class *, int);
109 static void update_vf(struct hfsc_class *, int, u_int64_t);
110 static void ellist_insert(struct hfsc_class *);
111 static void ellist_remove(struct hfsc_class *);
112 static void ellist_update(struct hfsc_class *);
113 struct hfsc_class *hfsc_get_mindl(struct hfsc_if *, u_int64_t);
114 static void actlist_insert(struct hfsc_class *);
115 static void actlist_remove(struct hfsc_class *);
116 static void actlist_update(struct hfsc_class *);
118 static struct hfsc_class *actlist_firstfit(struct hfsc_class *,
121 static __inline u_int64_t seg_x2y(u_int64_t, u_int64_t);
122 static __inline u_int64_t seg_y2x(u_int64_t, u_int64_t);
123 static __inline u_int64_t m2sm(u_int);
124 static __inline u_int64_t m2ism(u_int);
125 static __inline u_int64_t d2dx(u_int);
126 static u_int sm2m(u_int64_t);
127 static u_int dx2d(u_int64_t);
129 static void sc2isc(struct service_curve *, struct internal_sc *);
130 static void rtsc_init(struct runtime_sc *, struct internal_sc *,
131 u_int64_t, u_int64_t);
132 static u_int64_t rtsc_y2x(struct runtime_sc *, u_int64_t);
133 static u_int64_t rtsc_x2y(struct runtime_sc *, u_int64_t);
134 static void rtsc_min(struct runtime_sc *, struct internal_sc *,
135 u_int64_t, u_int64_t);
137 static void get_class_stats(struct hfsc_classstats *,
138 struct hfsc_class *);
139 static struct hfsc_class *clh_to_clp(struct hfsc_if *, u_int32_t);
143 static struct hfsc_if *hfsc_attach(struct ifaltq *, u_int);
144 static int hfsc_detach(struct hfsc_if *);
145 static int hfsc_class_modify(struct hfsc_class *, struct service_curve *,
146 struct service_curve *, struct service_curve *);
148 static int hfsccmd_if_attach(struct hfsc_attach *);
149 static int hfsccmd_if_detach(struct hfsc_interface *);
150 static int hfsccmd_add_class(struct hfsc_add_class *);
151 static int hfsccmd_delete_class(struct hfsc_delete_class *);
152 static int hfsccmd_modify_class(struct hfsc_modify_class *);
153 static int hfsccmd_add_filter(struct hfsc_add_filter *);
154 static int hfsccmd_delete_filter(struct hfsc_delete_filter *);
155 static int hfsccmd_class_stats(struct hfsc_class_stats *);
158 #endif /* ALTQ3_COMPAT */
163 #define is_a_parent_class(cl) ((cl)->cl_children != NULL)
165 #define HT_INFINITY 0xffffffffffffffffLL /* infinite time value */
168 /* hif_list keeps all hfsc_if's allocated. */
169 static struct hfsc_if *hif_list = NULL;
170 #endif /* ALTQ3_COMPAT */
173 hfsc_pfattach(struct pf_altq *a)
178 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
185 error = altq_attach(&ifp->if_snd, ALTQT_HFSC, a->altq_disc,
186 hfsc_enqueue, hfsc_dequeue, hfsc_request, NULL, NULL);
192 hfsc_add_altq(struct pf_altq *a)
197 if ((ifp = ifunit(a->ifname)) == NULL)
199 if (!ALTQ_IS_READY(&ifp->if_snd))
202 hif = malloc(sizeof(struct hfsc_if), M_DEVBUF, M_NOWAIT | M_ZERO);
206 TAILQ_INIT(&hif->hif_eligible);
207 hif->hif_ifq = &ifp->if_snd;
209 /* keep the state in pf_altq */
216 hfsc_remove_altq(struct pf_altq *a)
220 if ((hif = a->altq_disc) == NULL)
224 (void)hfsc_clear_interface(hif);
225 (void)hfsc_class_destroy(hif->hif_rootclass);
233 hfsc_add_queue(struct pf_altq *a)
236 struct hfsc_class *cl, *parent;
237 struct hfsc_opts *opts;
238 struct service_curve rtsc, lssc, ulsc;
240 if ((hif = a->altq_disc) == NULL)
243 opts = &a->pq_u.hfsc_opts;
245 if (a->parent_qid == HFSC_NULLCLASS_HANDLE &&
246 hif->hif_rootclass == NULL)
248 else if ((parent = clh_to_clp(hif, a->parent_qid)) == NULL)
254 if (clh_to_clp(hif, a->qid) != NULL)
257 rtsc.m1 = opts->rtsc_m1;
258 rtsc.d = opts->rtsc_d;
259 rtsc.m2 = opts->rtsc_m2;
260 lssc.m1 = opts->lssc_m1;
261 lssc.d = opts->lssc_d;
262 lssc.m2 = opts->lssc_m2;
263 ulsc.m1 = opts->ulsc_m1;
264 ulsc.d = opts->ulsc_d;
265 ulsc.m2 = opts->ulsc_m2;
267 cl = hfsc_class_create(hif, &rtsc, &lssc, &ulsc,
268 parent, a->qlimit, opts->flags, a->qid);
276 hfsc_remove_queue(struct pf_altq *a)
279 struct hfsc_class *cl;
281 if ((hif = a->altq_disc) == NULL)
284 if ((cl = clh_to_clp(hif, a->qid)) == NULL)
287 return (hfsc_class_destroy(cl));
291 hfsc_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
294 struct hfsc_class *cl;
295 struct hfsc_classstats stats;
298 if ((hif = altq_lookup(a->ifname, ALTQT_HFSC)) == NULL)
301 if ((cl = clh_to_clp(hif, a->qid)) == NULL)
304 if (*nbytes < sizeof(stats))
307 get_class_stats(&stats, cl);
309 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
311 *nbytes = sizeof(stats);
316 * bring the interface back to the initial state by discarding
317 * all the filters and classes except the root class.
320 hfsc_clear_interface(struct hfsc_if *hif)
322 struct hfsc_class *cl;
325 /* free the filters for this interface */
326 acc_discard_filters(&hif->hif_classifier, NULL, 1);
329 /* clear out the classes */
330 while (hif->hif_rootclass != NULL &&
331 (cl = hif->hif_rootclass->cl_children) != NULL) {
333 * remove the first leaf class found in the hierarchy
336 for (; cl != NULL; cl = hfsc_nextclass(cl)) {
337 if (!is_a_parent_class(cl)) {
338 (void)hfsc_class_destroy(cl);
348 hfsc_request(struct ifaltq *ifq, int req, void *arg)
350 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
352 IFQ_LOCK_ASSERT(ifq);
362 /* discard all the queued packets on the interface */
364 hfsc_purge(struct hfsc_if *hif)
366 struct hfsc_class *cl;
368 for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl))
369 if (!qempty(cl->cl_q))
371 if (ALTQ_IS_ENABLED(hif->hif_ifq))
372 hif->hif_ifq->ifq_len = 0;
376 hfsc_class_create(struct hfsc_if *hif, struct service_curve *rsc,
377 struct service_curve *fsc, struct service_curve *usc,
378 struct hfsc_class *parent, int qlimit, int flags, int qid)
380 struct hfsc_class *cl, *p;
383 if (hif->hif_classes >= HFSC_MAX_CLASSES)
387 if (flags & HFCF_RED) {
389 printf("hfsc_class_create: RED not configured for HFSC!\n");
395 cl = malloc(sizeof(struct hfsc_class), M_DEVBUF, M_NOWAIT | M_ZERO);
399 cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF, M_NOWAIT | M_ZERO);
400 if (cl->cl_q == NULL)
403 TAILQ_INIT(&cl->cl_actc);
406 qlimit = 50; /* use default */
407 qlimit(cl->cl_q) = qlimit;
408 qtype(cl->cl_q) = Q_DROPTAIL;
410 cl->cl_flags = flags;
412 if (flags & (HFCF_RED|HFCF_RIO)) {
413 int red_flags, red_pkttime;
417 if (rsc != NULL && rsc->m2 > m2)
419 if (fsc != NULL && fsc->m2 > m2)
421 if (usc != NULL && usc->m2 > m2)
425 if (flags & HFCF_ECN)
426 red_flags |= REDF_ECN;
428 if (flags & HFCF_CLEARDSCP)
429 red_flags |= RIOF_CLEARDSCP;
432 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
434 red_pkttime = (int64_t)hif->hif_ifq->altq_ifp->if_mtu
435 * 1000 * 1000 * 1000 / (m2 / 8);
436 if (flags & HFCF_RED) {
437 cl->cl_red = red_alloc(0, 0,
438 qlimit(cl->cl_q) * 10/100,
439 qlimit(cl->cl_q) * 30/100,
440 red_flags, red_pkttime);
441 if (cl->cl_red != NULL)
442 qtype(cl->cl_q) = Q_RED;
446 cl->cl_red = (red_t *)rio_alloc(0, NULL,
447 red_flags, red_pkttime);
448 if (cl->cl_red != NULL)
449 qtype(cl->cl_q) = Q_RIO;
453 #endif /* ALTQ_RED */
455 if (rsc != NULL && (rsc->m1 != 0 || rsc->m2 != 0)) {
456 cl->cl_rsc = malloc(sizeof(struct internal_sc),
458 if (cl->cl_rsc == NULL)
460 sc2isc(rsc, cl->cl_rsc);
461 rtsc_init(&cl->cl_deadline, cl->cl_rsc, 0, 0);
462 rtsc_init(&cl->cl_eligible, cl->cl_rsc, 0, 0);
464 if (fsc != NULL && (fsc->m1 != 0 || fsc->m2 != 0)) {
465 cl->cl_fsc = malloc(sizeof(struct internal_sc),
467 if (cl->cl_fsc == NULL)
469 sc2isc(fsc, cl->cl_fsc);
470 rtsc_init(&cl->cl_virtual, cl->cl_fsc, 0, 0);
472 if (usc != NULL && (usc->m1 != 0 || usc->m2 != 0)) {
473 cl->cl_usc = malloc(sizeof(struct internal_sc),
475 if (cl->cl_usc == NULL)
477 sc2isc(usc, cl->cl_usc);
478 rtsc_init(&cl->cl_ulimit, cl->cl_usc, 0, 0);
481 cl->cl_id = hif->hif_classid++;
484 cl->cl_parent = parent;
491 IFQ_LOCK(hif->hif_ifq);
495 * find a free slot in the class table. if the slot matching
496 * the lower bits of qid is free, use this slot. otherwise,
497 * use the first free slot.
499 i = qid % HFSC_MAX_CLASSES;
500 if (hif->hif_class_tbl[i] == NULL)
501 hif->hif_class_tbl[i] = cl;
503 for (i = 0; i < HFSC_MAX_CLASSES; i++)
504 if (hif->hif_class_tbl[i] == NULL) {
505 hif->hif_class_tbl[i] = cl;
508 if (i == HFSC_MAX_CLASSES) {
509 IFQ_UNLOCK(hif->hif_ifq);
515 if (flags & HFCF_DEFAULTCLASS)
516 hif->hif_defaultclass = cl;
518 if (parent == NULL) {
519 /* this is root class */
520 hif->hif_rootclass = cl;
522 /* add this class to the children list of the parent */
523 if ((p = parent->cl_children) == NULL)
524 parent->cl_children = cl;
526 while (p->cl_siblings != NULL)
531 IFQ_UNLOCK(hif->hif_ifq);
537 if (cl->cl_red != NULL) {
539 if (q_is_rio(cl->cl_q))
540 rio_destroy((rio_t *)cl->cl_red);
543 if (q_is_red(cl->cl_q))
544 red_destroy(cl->cl_red);
547 if (cl->cl_fsc != NULL)
548 free(cl->cl_fsc, M_DEVBUF);
549 if (cl->cl_rsc != NULL)
550 free(cl->cl_rsc, M_DEVBUF);
551 if (cl->cl_usc != NULL)
552 free(cl->cl_usc, M_DEVBUF);
553 if (cl->cl_q != NULL)
554 free(cl->cl_q, M_DEVBUF);
560 hfsc_class_destroy(struct hfsc_class *cl)
567 if (is_a_parent_class(cl))
575 IFQ_LOCK(cl->cl_hif->hif_ifq);
578 /* delete filters referencing to this class */
579 acc_discard_filters(&cl->cl_hif->hif_classifier, cl, 0);
580 #endif /* ALTQ3_COMPAT */
582 if (!qempty(cl->cl_q))
585 if (cl->cl_parent == NULL) {
586 /* this is root class */
588 struct hfsc_class *p = cl->cl_parent->cl_children;
591 cl->cl_parent->cl_children = cl->cl_siblings;
593 if (p->cl_siblings == cl) {
594 p->cl_siblings = cl->cl_siblings;
597 } while ((p = p->cl_siblings) != NULL);
601 for (i = 0; i < HFSC_MAX_CLASSES; i++)
602 if (cl->cl_hif->hif_class_tbl[i] == cl) {
603 cl->cl_hif->hif_class_tbl[i] = NULL;
607 cl->cl_hif->hif_classes--;
608 IFQ_UNLOCK(cl->cl_hif->hif_ifq);
611 if (cl->cl_red != NULL) {
613 if (q_is_rio(cl->cl_q))
614 rio_destroy((rio_t *)cl->cl_red);
617 if (q_is_red(cl->cl_q))
618 red_destroy(cl->cl_red);
622 IFQ_LOCK(cl->cl_hif->hif_ifq);
623 if (cl == cl->cl_hif->hif_rootclass)
624 cl->cl_hif->hif_rootclass = NULL;
625 if (cl == cl->cl_hif->hif_defaultclass)
626 cl->cl_hif->hif_defaultclass = NULL;
627 IFQ_UNLOCK(cl->cl_hif->hif_ifq);
629 if (cl->cl_usc != NULL)
630 free(cl->cl_usc, M_DEVBUF);
631 if (cl->cl_fsc != NULL)
632 free(cl->cl_fsc, M_DEVBUF);
633 if (cl->cl_rsc != NULL)
634 free(cl->cl_rsc, M_DEVBUF);
635 free(cl->cl_q, M_DEVBUF);
642 * hfsc_nextclass returns the next class in the tree.
644 * for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl))
647 static struct hfsc_class *
648 hfsc_nextclass(struct hfsc_class *cl)
650 if (cl->cl_children != NULL)
651 cl = cl->cl_children;
652 else if (cl->cl_siblings != NULL)
653 cl = cl->cl_siblings;
655 while ((cl = cl->cl_parent) != NULL)
656 if (cl->cl_siblings) {
657 cl = cl->cl_siblings;
666 * hfsc_enqueue is an enqueue function to be registered to
667 * (*altq_enqueue) in struct ifaltq.
670 hfsc_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
672 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
673 struct hfsc_class *cl;
677 IFQ_LOCK_ASSERT(ifq);
679 /* grab class set by classifier */
680 if ((m->m_flags & M_PKTHDR) == 0) {
681 /* should not happen */
682 printf("altq: packet for %s does not have pkthdr\n",
683 ifq->altq_ifp->if_xname);
688 if ((t = pf_find_mtag(m)) != NULL)
689 cl = clh_to_clp(hif, t->qid);
691 else if ((ifq->altq_flags & ALTQF_CLASSIFY) && pktattr != NULL)
692 cl = pktattr->pattr_class;
694 if (cl == NULL || is_a_parent_class(cl)) {
695 cl = hif->hif_defaultclass;
703 cl->cl_pktattr = pktattr; /* save proto hdr used by ECN */
706 cl->cl_pktattr = NULL;
708 if (hfsc_addq(cl, m) != 0) {
709 /* drop occurred. mbuf was freed in hfsc_addq. */
710 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, len);
714 cl->cl_hif->hif_packets++;
716 /* successfully queued. */
717 if (qlen(cl->cl_q) == 1)
718 set_active(cl, m_pktlen(m));
724 * hfsc_dequeue is a dequeue function to be registered to
725 * (*altq_dequeue) in struct ifaltq.
727 * note: ALTDQ_POLL returns the next packet without removing the packet
728 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
729 * ALTDQ_REMOVE must return the same packet if called immediately
733 hfsc_dequeue(struct ifaltq *ifq, int op)
735 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
736 struct hfsc_class *cl;
742 IFQ_LOCK_ASSERT(ifq);
744 if (hif->hif_packets == 0)
745 /* no packet in the tree */
748 cur_time = read_machclk();
750 if (op == ALTDQ_REMOVE && hif->hif_pollcache != NULL) {
752 cl = hif->hif_pollcache;
753 hif->hif_pollcache = NULL;
754 /* check if the class was scheduled by real-time criteria */
755 if (cl->cl_rsc != NULL)
756 realtime = (cl->cl_e <= cur_time);
759 * if there are eligible classes, use real-time criteria.
760 * find the class with the minimum deadline among
761 * the eligible classes.
763 if ((cl = hfsc_get_mindl(hif, cur_time))
771 * use link-sharing criteria
772 * get the class with the minimum vt in the hierarchy
774 cl = hif->hif_rootclass;
775 while (is_a_parent_class(cl)) {
777 cl = actlist_firstfit(cl, cur_time);
781 printf("%d fit but none found\n",fits);
786 * update parent's cl_cvtmin.
787 * don't update if the new vt is smaller.
789 if (cl->cl_parent->cl_cvtmin < cl->cl_vt)
790 cl->cl_parent->cl_cvtmin = cl->cl_vt;
797 if (op == ALTDQ_POLL) {
798 hif->hif_pollcache = cl;
806 panic("hfsc_dequeue:");
808 cl->cl_hif->hif_packets--;
810 PKTCNTR_ADD(&cl->cl_stats.xmit_cnt, len);
812 update_vf(cl, len, cur_time);
816 if (!qempty(cl->cl_q)) {
817 if (cl->cl_rsc != NULL) {
819 next_len = m_pktlen(qhead(cl->cl_q));
822 update_ed(cl, next_len);
824 update_d(cl, next_len);
827 /* the class becomes passive */
835 hfsc_addq(struct hfsc_class *cl, struct mbuf *m)
839 if (q_is_rio(cl->cl_q))
840 return rio_addq((rio_t *)cl->cl_red, cl->cl_q,
844 if (q_is_red(cl->cl_q))
845 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
847 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
852 if (cl->cl_flags & HFCF_CLEARDSCP)
853 write_dsfield(m, cl->cl_pktattr, 0);
861 hfsc_getq(struct hfsc_class *cl)
864 if (q_is_rio(cl->cl_q))
865 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
868 if (q_is_red(cl->cl_q))
869 return red_getq(cl->cl_red, cl->cl_q);
871 return _getq(cl->cl_q);
875 hfsc_pollq(struct hfsc_class *cl)
877 return qhead(cl->cl_q);
881 hfsc_purgeq(struct hfsc_class *cl)
885 if (qempty(cl->cl_q))
888 while ((m = _getq(cl->cl_q)) != NULL) {
889 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, m_pktlen(m));
891 cl->cl_hif->hif_packets--;
892 IFQ_DEC_LEN(cl->cl_hif->hif_ifq);
894 ASSERT(qlen(cl->cl_q) == 0);
896 update_vf(cl, 0, 0); /* remove cl from the actlist */
901 set_active(struct hfsc_class *cl, int len)
903 if (cl->cl_rsc != NULL)
905 if (cl->cl_fsc != NULL)
908 cl->cl_stats.period++;
912 set_passive(struct hfsc_class *cl)
914 if (cl->cl_rsc != NULL)
918 * actlist is now handled in update_vf() so that update_vf(cl, 0, 0)
919 * needs to be called explicitly to remove a class from actlist
924 init_ed(struct hfsc_class *cl, int next_len)
928 cur_time = read_machclk();
930 /* update the deadline curve */
931 rtsc_min(&cl->cl_deadline, cl->cl_rsc, cur_time, cl->cl_cumul);
934 * update the eligible curve.
935 * for concave, it is equal to the deadline curve.
936 * for convex, it is a linear curve with slope m2.
938 cl->cl_eligible = cl->cl_deadline;
939 if (cl->cl_rsc->sm1 <= cl->cl_rsc->sm2) {
940 cl->cl_eligible.dx = 0;
941 cl->cl_eligible.dy = 0;
944 /* compute e and d */
945 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
946 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
952 update_ed(struct hfsc_class *cl, int next_len)
954 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
955 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
961 update_d(struct hfsc_class *cl, int next_len)
963 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
967 init_vf(struct hfsc_class *cl, int len)
969 struct hfsc_class *max_cl, *p;
970 u_int64_t vt, f, cur_time;
975 for ( ; cl->cl_parent != NULL; cl = cl->cl_parent) {
977 if (go_active && cl->cl_nactive++ == 0)
983 max_cl = TAILQ_LAST(&cl->cl_parent->cl_actc, acthead);
984 if (max_cl != NULL) {
986 * set vt to the average of the min and max
987 * classes. if the parent's period didn't
988 * change, don't decrease vt of the class.
991 if (cl->cl_parent->cl_cvtmin != 0)
992 vt = (cl->cl_parent->cl_cvtmin + vt)/2;
994 if (cl->cl_parent->cl_vtperiod !=
995 cl->cl_parentperiod || vt > cl->cl_vt)
999 * first child for a new parent backlog period.
1000 * add parent's cvtmax to vtoff of children
1001 * to make a new vt (vtoff + vt) larger than
1002 * the vt in the last period for all children.
1004 vt = cl->cl_parent->cl_cvtmax;
1005 for (p = cl->cl_parent->cl_children; p != NULL;
1009 cl->cl_parent->cl_cvtmax = 0;
1010 cl->cl_parent->cl_cvtmin = 0;
1012 cl->cl_initvt = cl->cl_vt;
1014 /* update the virtual curve */
1015 vt = cl->cl_vt + cl->cl_vtoff;
1016 rtsc_min(&cl->cl_virtual, cl->cl_fsc, vt, cl->cl_total);
1017 if (cl->cl_virtual.x == vt) {
1018 cl->cl_virtual.x -= cl->cl_vtoff;
1023 cl->cl_vtperiod++; /* increment vt period */
1024 cl->cl_parentperiod = cl->cl_parent->cl_vtperiod;
1025 if (cl->cl_parent->cl_nactive == 0)
1026 cl->cl_parentperiod++;
1031 if (cl->cl_usc != NULL) {
1032 /* class has upper limit curve */
1034 cur_time = read_machclk();
1036 /* update the ulimit curve */
1037 rtsc_min(&cl->cl_ulimit, cl->cl_usc, cur_time,
1040 cl->cl_myf = rtsc_y2x(&cl->cl_ulimit,
1046 if (cl->cl_myf > cl->cl_cfmin)
1050 if (f != cl->cl_f) {
1052 update_cfmin(cl->cl_parent);
1058 update_vf(struct hfsc_class *cl, int len, u_int64_t cur_time)
1060 u_int64_t f, myf_bound, delta;
1063 go_passive = qempty(cl->cl_q);
1065 for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
1067 cl->cl_total += len;
1069 if (cl->cl_fsc == NULL || cl->cl_nactive == 0)
1072 if (go_passive && --cl->cl_nactive == 0)
1078 /* no more active child, going passive */
1080 /* update cvtmax of the parent class */
1081 if (cl->cl_vt > cl->cl_parent->cl_cvtmax)
1082 cl->cl_parent->cl_cvtmax = cl->cl_vt;
1084 /* remove this class from the vt list */
1087 update_cfmin(cl->cl_parent);
1095 cl->cl_vt = rtsc_y2x(&cl->cl_virtual, cl->cl_total)
1096 - cl->cl_vtoff + cl->cl_vtadj;
1099 * if vt of the class is smaller than cvtmin,
1100 * the class was skipped in the past due to non-fit.
1101 * if so, we need to adjust vtadj.
1103 if (cl->cl_vt < cl->cl_parent->cl_cvtmin) {
1104 cl->cl_vtadj += cl->cl_parent->cl_cvtmin - cl->cl_vt;
1105 cl->cl_vt = cl->cl_parent->cl_cvtmin;
1108 /* update the vt list */
1111 if (cl->cl_usc != NULL) {
1112 cl->cl_myf = cl->cl_myfadj
1113 + rtsc_y2x(&cl->cl_ulimit, cl->cl_total);
1116 * if myf lags behind by more than one clock tick
1117 * from the current time, adjust myfadj to prevent
1118 * a rate-limited class from going greedy.
1119 * in a steady state under rate-limiting, myf
1120 * fluctuates within one clock tick.
1122 myf_bound = cur_time - machclk_per_tick;
1123 if (cl->cl_myf < myf_bound) {
1124 delta = cur_time - cl->cl_myf;
1125 cl->cl_myfadj += delta;
1126 cl->cl_myf += delta;
1130 /* cl_f is max(cl_myf, cl_cfmin) */
1131 if (cl->cl_myf > cl->cl_cfmin)
1135 if (f != cl->cl_f) {
1137 update_cfmin(cl->cl_parent);
1143 update_cfmin(struct hfsc_class *cl)
1145 struct hfsc_class *p;
1148 if (TAILQ_EMPTY(&cl->cl_actc)) {
1152 cfmin = HT_INFINITY;
1153 TAILQ_FOREACH(p, &cl->cl_actc, cl_actlist) {
1158 if (p->cl_f < cfmin)
1161 cl->cl_cfmin = cfmin;
1165 * TAILQ based ellist and actlist implementation
1166 * (ion wanted to make a calendar queue based implementation)
1169 * eligible list holds backlogged classes being sorted by their eligible times.
1170 * there is one eligible list per interface.
1174 ellist_insert(struct hfsc_class *cl)
1176 struct hfsc_if *hif = cl->cl_hif;
1177 struct hfsc_class *p;
1179 /* check the last entry first */
1180 if ((p = TAILQ_LAST(&hif->hif_eligible, elighead)) == NULL ||
1181 p->cl_e <= cl->cl_e) {
1182 TAILQ_INSERT_TAIL(&hif->hif_eligible, cl, cl_ellist);
1186 TAILQ_FOREACH(p, &hif->hif_eligible, cl_ellist) {
1187 if (cl->cl_e < p->cl_e) {
1188 TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1192 ASSERT(0); /* should not reach here */
1196 ellist_remove(struct hfsc_class *cl)
1198 struct hfsc_if *hif = cl->cl_hif;
1200 TAILQ_REMOVE(&hif->hif_eligible, cl, cl_ellist);
1204 ellist_update(struct hfsc_class *cl)
1206 struct hfsc_if *hif = cl->cl_hif;
1207 struct hfsc_class *p, *last;
1210 * the eligible time of a class increases monotonically.
1211 * if the next entry has a larger eligible time, nothing to do.
1213 p = TAILQ_NEXT(cl, cl_ellist);
1214 if (p == NULL || cl->cl_e <= p->cl_e)
1217 /* check the last entry */
1218 last = TAILQ_LAST(&hif->hif_eligible, elighead);
1219 ASSERT(last != NULL);
1220 if (last->cl_e <= cl->cl_e) {
1221 TAILQ_REMOVE(&hif->hif_eligible, cl, cl_ellist);
1222 TAILQ_INSERT_TAIL(&hif->hif_eligible, cl, cl_ellist);
1227 * the new position must be between the next entry
1228 * and the last entry
1230 while ((p = TAILQ_NEXT(p, cl_ellist)) != NULL) {
1231 if (cl->cl_e < p->cl_e) {
1232 TAILQ_REMOVE(&hif->hif_eligible, cl, cl_ellist);
1233 TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1237 ASSERT(0); /* should not reach here */
1240 /* find the class with the minimum deadline among the eligible classes */
1242 hfsc_get_mindl(struct hfsc_if *hif, u_int64_t cur_time)
1244 struct hfsc_class *p, *cl = NULL;
1246 TAILQ_FOREACH(p, &hif->hif_eligible, cl_ellist) {
1247 if (p->cl_e > cur_time)
1249 if (cl == NULL || p->cl_d < cl->cl_d)
1256 * active children list holds backlogged child classes being sorted
1257 * by their virtual time.
1258 * each intermediate class has one active children list.
1262 actlist_insert(struct hfsc_class *cl)
1264 struct hfsc_class *p;
1266 /* check the last entry first */
1267 if ((p = TAILQ_LAST(&cl->cl_parent->cl_actc, acthead)) == NULL
1268 || p->cl_vt <= cl->cl_vt) {
1269 TAILQ_INSERT_TAIL(&cl->cl_parent->cl_actc, cl, cl_actlist);
1273 TAILQ_FOREACH(p, &cl->cl_parent->cl_actc, cl_actlist) {
1274 if (cl->cl_vt < p->cl_vt) {
1275 TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1279 ASSERT(0); /* should not reach here */
1283 actlist_remove(struct hfsc_class *cl)
1285 TAILQ_REMOVE(&cl->cl_parent->cl_actc, cl, cl_actlist);
1289 actlist_update(struct hfsc_class *cl)
1291 struct hfsc_class *p, *last;
1294 * the virtual time of a class increases monotonically during its
1295 * backlogged period.
1296 * if the next entry has a larger virtual time, nothing to do.
1298 p = TAILQ_NEXT(cl, cl_actlist);
1299 if (p == NULL || cl->cl_vt < p->cl_vt)
1302 /* check the last entry */
1303 last = TAILQ_LAST(&cl->cl_parent->cl_actc, acthead);
1304 ASSERT(last != NULL);
1305 if (last->cl_vt <= cl->cl_vt) {
1306 TAILQ_REMOVE(&cl->cl_parent->cl_actc, cl, cl_actlist);
1307 TAILQ_INSERT_TAIL(&cl->cl_parent->cl_actc, cl, cl_actlist);
1312 * the new position must be between the next entry
1313 * and the last entry
1315 while ((p = TAILQ_NEXT(p, cl_actlist)) != NULL) {
1316 if (cl->cl_vt < p->cl_vt) {
1317 TAILQ_REMOVE(&cl->cl_parent->cl_actc, cl, cl_actlist);
1318 TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1322 ASSERT(0); /* should not reach here */
1325 static struct hfsc_class *
1326 actlist_firstfit(struct hfsc_class *cl, u_int64_t cur_time)
1328 struct hfsc_class *p;
1330 TAILQ_FOREACH(p, &cl->cl_actc, cl_actlist) {
1331 if (p->cl_f <= cur_time)
1338 * service curve support functions
1340 * external service curve parameters
1343 * internal service curve parameters
1344 * sm: (bytes/tsc_interval) << SM_SHIFT
1345 * ism: (tsc_count/byte) << ISM_SHIFT
1348 * SM_SHIFT and ISM_SHIFT are scaled in order to keep effective digits.
1349 * we should be able to handle 100K-1Gbps linkspeed with 200Hz-1GHz CPU
1350 * speed. SM_SHIFT and ISM_SHIFT are selected to have at least 3 effective
1351 * digits in decimal using the following table.
1353 * bits/sec 100Kbps 1Mbps 10Mbps 100Mbps 1Gbps
1354 * ----------+-------------------------------------------------------
1355 * bytes/nsec 12.5e-6 125e-6 1250e-6 12500e-6 125000e-6
1356 * sm(500MHz) 25.0e-6 250e-6 2500e-6 25000e-6 250000e-6
1357 * sm(200MHz) 62.5e-6 625e-6 6250e-6 62500e-6 625000e-6
1359 * nsec/byte 80000 8000 800 80 8
1360 * ism(500MHz) 40000 4000 400 40 4
1361 * ism(200MHz) 16000 1600 160 16 1.6
1364 #define ISM_SHIFT 10
1366 #define SM_MASK ((1LL << SM_SHIFT) - 1)
1367 #define ISM_MASK ((1LL << ISM_SHIFT) - 1)
1369 static __inline u_int64_t
1370 seg_x2y(u_int64_t x, u_int64_t sm)
1376 * y = x * sm >> SM_SHIFT
1377 * but divide it for the upper and lower bits to avoid overflow
1379 y = (x >> SM_SHIFT) * sm + (((x & SM_MASK) * sm) >> SM_SHIFT);
1383 static __inline u_int64_t
1384 seg_y2x(u_int64_t y, u_int64_t ism)
1390 else if (ism == HT_INFINITY)
1393 x = (y >> ISM_SHIFT) * ism
1394 + (((y & ISM_MASK) * ism) >> ISM_SHIFT);
1399 static __inline u_int64_t
1404 sm = ((u_int64_t)m << SM_SHIFT) / 8 / machclk_freq;
1408 static __inline u_int64_t
1416 ism = ((u_int64_t)machclk_freq << ISM_SHIFT) * 8 / m;
1420 static __inline u_int64_t
1425 dx = ((u_int64_t)d * machclk_freq) / 1000;
1434 m = (sm * 8 * machclk_freq) >> SM_SHIFT;
1443 d = dx * 1000 / machclk_freq;
1448 sc2isc(struct service_curve *sc, struct internal_sc *isc)
1450 isc->sm1 = m2sm(sc->m1);
1451 isc->ism1 = m2ism(sc->m1);
1452 isc->dx = d2dx(sc->d);
1453 isc->dy = seg_x2y(isc->dx, isc->sm1);
1454 isc->sm2 = m2sm(sc->m2);
1455 isc->ism2 = m2ism(sc->m2);
1459 * initialize the runtime service curve with the given internal
1460 * service curve starting at (x, y).
1463 rtsc_init(struct runtime_sc *rtsc, struct internal_sc * isc, u_int64_t x,
1468 rtsc->sm1 = isc->sm1;
1469 rtsc->ism1 = isc->ism1;
1472 rtsc->sm2 = isc->sm2;
1473 rtsc->ism2 = isc->ism2;
1477 * calculate the y-projection of the runtime service curve by the
1478 * given x-projection value
1481 rtsc_y2x(struct runtime_sc *rtsc, u_int64_t y)
1487 else if (y <= rtsc->y + rtsc->dy) {
1488 /* x belongs to the 1st segment */
1490 x = rtsc->x + rtsc->dx;
1492 x = rtsc->x + seg_y2x(y - rtsc->y, rtsc->ism1);
1494 /* x belongs to the 2nd segment */
1495 x = rtsc->x + rtsc->dx
1496 + seg_y2x(y - rtsc->y - rtsc->dy, rtsc->ism2);
1502 rtsc_x2y(struct runtime_sc *rtsc, u_int64_t x)
1508 else if (x <= rtsc->x + rtsc->dx)
1509 /* y belongs to the 1st segment */
1510 y = rtsc->y + seg_x2y(x - rtsc->x, rtsc->sm1);
1512 /* y belongs to the 2nd segment */
1513 y = rtsc->y + rtsc->dy
1514 + seg_x2y(x - rtsc->x - rtsc->dx, rtsc->sm2);
1519 * update the runtime service curve by taking the minimum of the current
1520 * runtime service curve and the service curve starting at (x, y).
1523 rtsc_min(struct runtime_sc *rtsc, struct internal_sc *isc, u_int64_t x,
1526 u_int64_t y1, y2, dx, dy;
1528 if (isc->sm1 <= isc->sm2) {
1529 /* service curve is convex */
1530 y1 = rtsc_x2y(rtsc, x);
1532 /* the current rtsc is smaller */
1540 * service curve is concave
1541 * compute the two y values of the current rtsc
1545 y1 = rtsc_x2y(rtsc, x);
1547 /* rtsc is below isc, no change to rtsc */
1551 y2 = rtsc_x2y(rtsc, x + isc->dx);
1552 if (y2 >= y + isc->dy) {
1553 /* rtsc is above isc, replace rtsc by isc */
1562 * the two curves intersect
1563 * compute the offsets (dx, dy) using the reverse
1564 * function of seg_x2y()
1565 * seg_x2y(dx, sm1) == seg_x2y(dx, sm2) + (y1 - y)
1567 dx = ((y1 - y) << SM_SHIFT) / (isc->sm1 - isc->sm2);
1569 * check if (x, y1) belongs to the 1st segment of rtsc.
1570 * if so, add the offset.
1572 if (rtsc->x + rtsc->dx > x)
1573 dx += rtsc->x + rtsc->dx - x;
1574 dy = seg_x2y(dx, isc->sm1);
1584 get_class_stats(struct hfsc_classstats *sp, struct hfsc_class *cl)
1586 sp->class_id = cl->cl_id;
1587 sp->class_handle = cl->cl_handle;
1589 if (cl->cl_rsc != NULL) {
1590 sp->rsc.m1 = sm2m(cl->cl_rsc->sm1);
1591 sp->rsc.d = dx2d(cl->cl_rsc->dx);
1592 sp->rsc.m2 = sm2m(cl->cl_rsc->sm2);
1598 if (cl->cl_fsc != NULL) {
1599 sp->fsc.m1 = sm2m(cl->cl_fsc->sm1);
1600 sp->fsc.d = dx2d(cl->cl_fsc->dx);
1601 sp->fsc.m2 = sm2m(cl->cl_fsc->sm2);
1607 if (cl->cl_usc != NULL) {
1608 sp->usc.m1 = sm2m(cl->cl_usc->sm1);
1609 sp->usc.d = dx2d(cl->cl_usc->dx);
1610 sp->usc.m2 = sm2m(cl->cl_usc->sm2);
1617 sp->total = cl->cl_total;
1618 sp->cumul = cl->cl_cumul;
1625 sp->initvt = cl->cl_initvt;
1626 sp->vtperiod = cl->cl_vtperiod;
1627 sp->parentperiod = cl->cl_parentperiod;
1628 sp->nactive = cl->cl_nactive;
1629 sp->vtoff = cl->cl_vtoff;
1630 sp->cvtmax = cl->cl_cvtmax;
1631 sp->myf = cl->cl_myf;
1632 sp->cfmin = cl->cl_cfmin;
1633 sp->cvtmin = cl->cl_cvtmin;
1634 sp->myfadj = cl->cl_myfadj;
1635 sp->vtadj = cl->cl_vtadj;
1637 sp->cur_time = read_machclk();
1638 sp->machclk_freq = machclk_freq;
1640 sp->qlength = qlen(cl->cl_q);
1641 sp->qlimit = qlimit(cl->cl_q);
1642 sp->xmit_cnt = cl->cl_stats.xmit_cnt;
1643 sp->drop_cnt = cl->cl_stats.drop_cnt;
1644 sp->period = cl->cl_stats.period;
1646 sp->qtype = qtype(cl->cl_q);
1648 if (q_is_red(cl->cl_q))
1649 red_getstats(cl->cl_red, &sp->red[0]);
1652 if (q_is_rio(cl->cl_q))
1653 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
1657 /* convert a class handle to the corresponding class pointer */
1658 static struct hfsc_class *
1659 clh_to_clp(struct hfsc_if *hif, u_int32_t chandle)
1662 struct hfsc_class *cl;
1667 * first, try optimistically the slot matching the lower bits of
1668 * the handle. if it fails, do the linear table search.
1670 i = chandle % HFSC_MAX_CLASSES;
1671 if ((cl = hif->hif_class_tbl[i]) != NULL && cl->cl_handle == chandle)
1673 for (i = 0; i < HFSC_MAX_CLASSES; i++)
1674 if ((cl = hif->hif_class_tbl[i]) != NULL &&
1675 cl->cl_handle == chandle)
1681 static struct hfsc_if *
1682 hfsc_attach(ifq, bandwidth)
1686 struct hfsc_if *hif;
1688 hif = malloc(sizeof(struct hfsc_if), M_DEVBUF, M_WAITOK);
1691 bzero(hif, sizeof(struct hfsc_if));
1693 hif->hif_eligible = ellist_alloc();
1694 if (hif->hif_eligible == NULL) {
1695 free(hif, M_DEVBUF);
1701 /* add this state to the hfsc list */
1702 hif->hif_next = hif_list;
1710 struct hfsc_if *hif;
1712 (void)hfsc_clear_interface(hif);
1713 (void)hfsc_class_destroy(hif->hif_rootclass);
1715 /* remove this interface from the hif list */
1716 if (hif_list == hif)
1717 hif_list = hif->hif_next;
1721 for (h = hif_list; h != NULL; h = h->hif_next)
1722 if (h->hif_next == hif) {
1723 h->hif_next = hif->hif_next;
1729 ellist_destroy(hif->hif_eligible);
1731 free(hif, M_DEVBUF);
1737 hfsc_class_modify(cl, rsc, fsc, usc)
1738 struct hfsc_class *cl;
1739 struct service_curve *rsc, *fsc, *usc;
1741 struct internal_sc *rsc_tmp, *fsc_tmp, *usc_tmp;
1745 rsc_tmp = fsc_tmp = usc_tmp = NULL;
1746 if (rsc != NULL && (rsc->m1 != 0 || rsc->m2 != 0) &&
1747 cl->cl_rsc == NULL) {
1748 rsc_tmp = malloc(sizeof(struct internal_sc),
1749 M_DEVBUF, M_WAITOK);
1750 if (rsc_tmp == NULL)
1753 if (fsc != NULL && (fsc->m1 != 0 || fsc->m2 != 0) &&
1754 cl->cl_fsc == NULL) {
1755 fsc_tmp = malloc(sizeof(struct internal_sc),
1756 M_DEVBUF, M_WAITOK);
1757 if (fsc_tmp == NULL) {
1762 if (usc != NULL && (usc->m1 != 0 || usc->m2 != 0) &&
1763 cl->cl_usc == NULL) {
1764 usc_tmp = malloc(sizeof(struct internal_sc),
1765 M_DEVBUF, M_WAITOK);
1766 if (usc_tmp == NULL) {
1773 cur_time = read_machclk();
1779 IFQ_LOCK(cl->cl_hif->hif_ifq);
1782 if (rsc->m1 == 0 && rsc->m2 == 0) {
1783 if (cl->cl_rsc != NULL) {
1784 if (!qempty(cl->cl_q))
1786 free(cl->cl_rsc, M_DEVBUF);
1790 if (cl->cl_rsc == NULL)
1791 cl->cl_rsc = rsc_tmp;
1792 sc2isc(rsc, cl->cl_rsc);
1793 rtsc_init(&cl->cl_deadline, cl->cl_rsc, cur_time,
1795 cl->cl_eligible = cl->cl_deadline;
1796 if (cl->cl_rsc->sm1 <= cl->cl_rsc->sm2) {
1797 cl->cl_eligible.dx = 0;
1798 cl->cl_eligible.dy = 0;
1804 if (fsc->m1 == 0 && fsc->m2 == 0) {
1805 if (cl->cl_fsc != NULL) {
1806 if (!qempty(cl->cl_q))
1808 free(cl->cl_fsc, M_DEVBUF);
1812 if (cl->cl_fsc == NULL)
1813 cl->cl_fsc = fsc_tmp;
1814 sc2isc(fsc, cl->cl_fsc);
1815 rtsc_init(&cl->cl_virtual, cl->cl_fsc, cl->cl_vt,
1821 if (usc->m1 == 0 && usc->m2 == 0) {
1822 if (cl->cl_usc != NULL) {
1823 free(cl->cl_usc, M_DEVBUF);
1828 if (cl->cl_usc == NULL)
1829 cl->cl_usc = usc_tmp;
1830 sc2isc(usc, cl->cl_usc);
1831 rtsc_init(&cl->cl_ulimit, cl->cl_usc, cur_time,
1836 if (!qempty(cl->cl_q)) {
1837 if (cl->cl_rsc != NULL)
1838 update_ed(cl, m_pktlen(qhead(cl->cl_q)));
1839 if (cl->cl_fsc != NULL)
1840 update_vf(cl, 0, cur_time);
1841 /* is this enough? */
1844 IFQ_UNLOCK(cl->cl_hif->hif_ifq);
1851 * hfsc device interface
1854 hfscopen(dev, flag, fmt, p)
1857 #if (__FreeBSD_version > 500000)
1863 if (machclk_freq == 0)
1866 if (machclk_freq == 0) {
1867 printf("hfsc: no cpu clock available!\n");
1871 /* everything will be done when the queueing scheme is attached. */
1876 hfscclose(dev, flag, fmt, p)
1879 #if (__FreeBSD_version > 500000)
1885 struct hfsc_if *hif;
1888 while ((hif = hif_list) != NULL) {
1890 if (ALTQ_IS_ENABLED(hif->hif_ifq))
1891 altq_disable(hif->hif_ifq);
1893 err = altq_detach(hif->hif_ifq);
1895 err = hfsc_detach(hif);
1896 if (err != 0 && error == 0)
1904 hfscioctl(dev, cmd, addr, flag, p)
1909 #if (__FreeBSD_version > 500000)
1915 struct hfsc_if *hif;
1916 struct hfsc_interface *ifacep;
1919 /* check super-user privilege */
1924 #if (__FreeBSD_version > 700000)
1925 if ((error = priv_check(p, PRIV_ALTQ_MANAGE)) != 0)
1927 #elsif (__FreeBSD_version > 400000)
1928 if ((error = suser(p)) != 0)
1931 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1939 case HFSC_IF_ATTACH:
1940 error = hfsccmd_if_attach((struct hfsc_attach *)addr);
1943 case HFSC_IF_DETACH:
1944 error = hfsccmd_if_detach((struct hfsc_interface *)addr);
1949 case HFSC_CLEAR_HIERARCHY:
1950 ifacep = (struct hfsc_interface *)addr;
1951 if ((hif = altq_lookup(ifacep->hfsc_ifname,
1952 ALTQT_HFSC)) == NULL) {
1960 if (hif->hif_defaultclass == NULL) {
1962 printf("hfsc: no default class\n");
1967 error = altq_enable(hif->hif_ifq);
1971 error = altq_disable(hif->hif_ifq);
1974 case HFSC_CLEAR_HIERARCHY:
1975 hfsc_clear_interface(hif);
1980 case HFSC_ADD_CLASS:
1981 error = hfsccmd_add_class((struct hfsc_add_class *)addr);
1984 case HFSC_DEL_CLASS:
1985 error = hfsccmd_delete_class((struct hfsc_delete_class *)addr);
1988 case HFSC_MOD_CLASS:
1989 error = hfsccmd_modify_class((struct hfsc_modify_class *)addr);
1992 case HFSC_ADD_FILTER:
1993 error = hfsccmd_add_filter((struct hfsc_add_filter *)addr);
1996 case HFSC_DEL_FILTER:
1997 error = hfsccmd_delete_filter((struct hfsc_delete_filter *)addr);
2001 error = hfsccmd_class_stats((struct hfsc_class_stats *)addr);
2012 hfsccmd_if_attach(ap)
2013 struct hfsc_attach *ap;
2015 struct hfsc_if *hif;
2019 if ((ifp = ifunit(ap->iface.hfsc_ifname)) == NULL)
2022 if ((hif = hfsc_attach(&ifp->if_snd, ap->bandwidth)) == NULL)
2026 * set HFSC to this ifnet structure.
2028 if ((error = altq_attach(&ifp->if_snd, ALTQT_HFSC, hif,
2029 hfsc_enqueue, hfsc_dequeue, hfsc_request,
2030 &hif->hif_classifier, acc_classify)) != 0)
2031 (void)hfsc_detach(hif);
2037 hfsccmd_if_detach(ap)
2038 struct hfsc_interface *ap;
2040 struct hfsc_if *hif;
2043 if ((hif = altq_lookup(ap->hfsc_ifname, ALTQT_HFSC)) == NULL)
2046 if (ALTQ_IS_ENABLED(hif->hif_ifq))
2047 altq_disable(hif->hif_ifq);
2049 if ((error = altq_detach(hif->hif_ifq)))
2052 return hfsc_detach(hif);
2056 hfsccmd_add_class(ap)
2057 struct hfsc_add_class *ap;
2059 struct hfsc_if *hif;
2060 struct hfsc_class *cl, *parent;
2063 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2066 if (ap->parent_handle == HFSC_NULLCLASS_HANDLE &&
2067 hif->hif_rootclass == NULL)
2069 else if ((parent = clh_to_clp(hif, ap->parent_handle)) == NULL)
2072 /* assign a class handle (use a free slot number for now) */
2073 for (i = 1; i < HFSC_MAX_CLASSES; i++)
2074 if (hif->hif_class_tbl[i] == NULL)
2076 if (i == HFSC_MAX_CLASSES)
2079 if ((cl = hfsc_class_create(hif, &ap->service_curve, NULL, NULL,
2080 parent, ap->qlimit, ap->flags, i)) == NULL)
2083 /* return a class handle to the user */
2084 ap->class_handle = i;
2090 hfsccmd_delete_class(ap)
2091 struct hfsc_delete_class *ap;
2093 struct hfsc_if *hif;
2094 struct hfsc_class *cl;
2096 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2099 if ((cl = clh_to_clp(hif, ap->class_handle)) == NULL)
2102 return hfsc_class_destroy(cl);
2106 hfsccmd_modify_class(ap)
2107 struct hfsc_modify_class *ap;
2109 struct hfsc_if *hif;
2110 struct hfsc_class *cl;
2111 struct service_curve *rsc = NULL;
2112 struct service_curve *fsc = NULL;
2113 struct service_curve *usc = NULL;
2115 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2118 if ((cl = clh_to_clp(hif, ap->class_handle)) == NULL)
2121 if (ap->sctype & HFSC_REALTIMESC)
2122 rsc = &ap->service_curve;
2123 if (ap->sctype & HFSC_LINKSHARINGSC)
2124 fsc = &ap->service_curve;
2125 if (ap->sctype & HFSC_UPPERLIMITSC)
2126 usc = &ap->service_curve;
2128 return hfsc_class_modify(cl, rsc, fsc, usc);
2132 hfsccmd_add_filter(ap)
2133 struct hfsc_add_filter *ap;
2135 struct hfsc_if *hif;
2136 struct hfsc_class *cl;
2138 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2141 if ((cl = clh_to_clp(hif, ap->class_handle)) == NULL)
2144 if (is_a_parent_class(cl)) {
2146 printf("hfsccmd_add_filter: not a leaf class!\n");
2151 return acc_add_filter(&hif->hif_classifier, &ap->filter,
2152 cl, &ap->filter_handle);
2156 hfsccmd_delete_filter(ap)
2157 struct hfsc_delete_filter *ap;
2159 struct hfsc_if *hif;
2161 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2164 return acc_delete_filter(&hif->hif_classifier,
2169 hfsccmd_class_stats(ap)
2170 struct hfsc_class_stats *ap;
2172 struct hfsc_if *hif;
2173 struct hfsc_class *cl;
2174 struct hfsc_classstats stats, *usp;
2175 int n, nclasses, error;
2177 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2180 ap->cur_time = read_machclk();
2181 ap->machclk_freq = machclk_freq;
2182 ap->hif_classes = hif->hif_classes;
2183 ap->hif_packets = hif->hif_packets;
2185 /* skip the first N classes in the tree */
2186 nclasses = ap->nskip;
2187 for (cl = hif->hif_rootclass, n = 0; cl != NULL && n < nclasses;
2188 cl = hfsc_nextclass(cl), n++)
2193 /* then, read the next N classes in the tree */
2194 nclasses = ap->nclasses;
2196 for (n = 0; cl != NULL && n < nclasses; cl = hfsc_nextclass(cl), n++) {
2198 get_class_stats(&stats, cl);
2200 if ((error = copyout((caddr_t)&stats, (caddr_t)usp++,
2201 sizeof(stats))) != 0)
2212 static struct altqsw hfsc_sw =
2213 {"hfsc", hfscopen, hfscclose, hfscioctl};
2215 ALTQ_MODULE(altq_hfsc, ALTQT_HFSC, &hfsc_sw);
2216 MODULE_DEPEND(altq_hfsc, altq_red, 1, 1, 1);
2217 MODULE_DEPEND(altq_hfsc, altq_rio, 1, 1, 1);
2219 #endif /* KLD_MODULE */
2220 #endif /* ALTQ3_COMPAT */
2222 #endif /* ALTQ_HFSC */