2 * Copyright (c) 1997-1999 Carnegie Mellon University. All Rights Reserved.
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation is hereby granted (including for commercial or
6 * for-profit use), provided that both the copyright notice and this
7 * permission notice appear in all copies of the software, derivative
8 * works, or modified versions, and any portions thereof.
10 * THIS SOFTWARE IS EXPERIMENTAL AND IS KNOWN TO HAVE BUGS, SOME OF
11 * WHICH MAY HAVE SERIOUS CONSEQUENCES. CARNEGIE MELLON PROVIDES THIS
12 * SOFTWARE IN ITS ``AS IS'' CONDITION, AND ANY EXPRESS OR IMPLIED
13 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
14 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 * DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
16 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
18 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
19 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
20 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
22 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
25 * Carnegie Mellon encourages (but does not require) users of this
26 * software to return any improvements or extensions that they make,
27 * and to grant Carnegie Mellon the rights to redistribute these
28 * changes without encumbrance.
30 * $KAME: altq_hfsc.c,v 1.24 2003/12/05 05:40:46 kjc Exp $
34 * H-FSC is described in Proceedings of SIGCOMM'97,
35 * "A Hierarchical Fair Service Curve Algorithm for Link-Sharing,
36 * Real-Time and Priority Service"
37 * by Ion Stoica, Hui Zhang, and T. S. Eugene Ng.
39 * Oleg Cherevko <olwi@aq.ml.com.ua> added the upperlimit for link-sharing.
40 * when a class has an upperlimit, the fit-time is computed from the
41 * upperlimit service curve. the link-sharing scheduler does not schedule
42 * a class whose fit-time exceeds the current time.
47 #include "opt_inet6.h"
49 #ifdef ALTQ_HFSC /* hfsc is enabled by ALTQ_HFSC option in opt_altq.h */
51 #include <sys/param.h>
52 #include <sys/malloc.h>
54 #include <sys/socket.h>
55 #include <sys/systm.h>
56 #include <sys/errno.h>
57 #include <sys/queue.h>
58 #if 1 /* ALTQ3_COMPAT */
59 #include <sys/sockio.h>
61 #include <sys/kernel.h>
62 #endif /* ALTQ3_COMPAT */
65 #include <net/if_var.h>
66 #include <netinet/in.h>
68 #include <netpfil/pf/pf.h>
69 #include <netpfil/pf/pf_altq.h>
70 #include <netpfil/pf/pf_mtag.h>
71 #include <net/altq/altq.h>
72 #include <net/altq/altq_hfsc.h>
74 #include <net/altq/altq_conf.h>
80 static int hfsc_clear_interface(struct hfsc_if *);
81 static int hfsc_request(struct ifaltq *, int, void *);
82 static void hfsc_purge(struct hfsc_if *);
83 static struct hfsc_class *hfsc_class_create(struct hfsc_if *,
84 struct service_curve *, struct service_curve *, struct service_curve *,
85 struct hfsc_class *, int, int, int);
86 static int hfsc_class_destroy(struct hfsc_class *);
87 static struct hfsc_class *hfsc_nextclass(struct hfsc_class *);
88 static int hfsc_enqueue(struct ifaltq *, struct mbuf *,
89 struct altq_pktattr *);
90 static struct mbuf *hfsc_dequeue(struct ifaltq *, int);
92 static int hfsc_addq(struct hfsc_class *, struct mbuf *);
93 static struct mbuf *hfsc_getq(struct hfsc_class *);
94 static struct mbuf *hfsc_pollq(struct hfsc_class *);
95 static void hfsc_purgeq(struct hfsc_class *);
97 static void update_cfmin(struct hfsc_class *);
98 static void set_active(struct hfsc_class *, int);
99 static void set_passive(struct hfsc_class *);
101 static void init_ed(struct hfsc_class *, int);
102 static void update_ed(struct hfsc_class *, int);
103 static void update_d(struct hfsc_class *, int);
104 static void init_vf(struct hfsc_class *, int);
105 static void update_vf(struct hfsc_class *, int, u_int64_t);
106 static void ellist_insert(struct hfsc_class *);
107 static void ellist_remove(struct hfsc_class *);
108 static void ellist_update(struct hfsc_class *);
109 struct hfsc_class *hfsc_get_mindl(struct hfsc_if *, u_int64_t);
110 static void actlist_insert(struct hfsc_class *);
111 static void actlist_remove(struct hfsc_class *);
112 static void actlist_update(struct hfsc_class *);
114 static struct hfsc_class *actlist_firstfit(struct hfsc_class *,
117 static __inline u_int64_t seg_x2y(u_int64_t, u_int64_t);
118 static __inline u_int64_t seg_y2x(u_int64_t, u_int64_t);
119 static __inline u_int64_t m2sm(u_int);
120 static __inline u_int64_t m2ism(u_int);
121 static __inline u_int64_t d2dx(u_int);
122 static u_int sm2m(u_int64_t);
123 static u_int dx2d(u_int64_t);
125 static void sc2isc(struct service_curve *, struct internal_sc *);
126 static void rtsc_init(struct runtime_sc *, struct internal_sc *,
127 u_int64_t, u_int64_t);
128 static u_int64_t rtsc_y2x(struct runtime_sc *, u_int64_t);
129 static u_int64_t rtsc_x2y(struct runtime_sc *, u_int64_t);
130 static void rtsc_min(struct runtime_sc *, struct internal_sc *,
131 u_int64_t, u_int64_t);
133 static void get_class_stats(struct hfsc_classstats *,
134 struct hfsc_class *);
135 static struct hfsc_class *clh_to_clp(struct hfsc_if *, u_int32_t);
139 static struct hfsc_if *hfsc_attach(struct ifaltq *, u_int);
140 static int hfsc_detach(struct hfsc_if *);
141 static int hfsc_class_modify(struct hfsc_class *, struct service_curve *,
142 struct service_curve *, struct service_curve *);
144 static int hfsccmd_if_attach(struct hfsc_attach *);
145 static int hfsccmd_if_detach(struct hfsc_interface *);
146 static int hfsccmd_add_class(struct hfsc_add_class *);
147 static int hfsccmd_delete_class(struct hfsc_delete_class *);
148 static int hfsccmd_modify_class(struct hfsc_modify_class *);
149 static int hfsccmd_add_filter(struct hfsc_add_filter *);
150 static int hfsccmd_delete_filter(struct hfsc_delete_filter *);
151 static int hfsccmd_class_stats(struct hfsc_class_stats *);
154 #endif /* ALTQ3_COMPAT */
159 #define is_a_parent_class(cl) ((cl)->cl_children != NULL)
161 #define HT_INFINITY 0xffffffffffffffffLL /* infinite time value */
164 /* hif_list keeps all hfsc_if's allocated. */
165 static struct hfsc_if *hif_list = NULL;
166 #endif /* ALTQ3_COMPAT */
169 hfsc_pfattach(struct pf_altq *a)
174 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
177 error = altq_attach(&ifp->if_snd, ALTQT_HFSC, a->altq_disc,
178 hfsc_enqueue, hfsc_dequeue, hfsc_request, NULL, NULL);
184 hfsc_add_altq(struct pf_altq *a)
189 if ((ifp = ifunit(a->ifname)) == NULL)
191 if (!ALTQ_IS_READY(&ifp->if_snd))
194 hif = malloc(sizeof(struct hfsc_if), M_DEVBUF, M_NOWAIT | M_ZERO);
198 TAILQ_INIT(&hif->hif_eligible);
199 hif->hif_ifq = &ifp->if_snd;
201 /* keep the state in pf_altq */
208 hfsc_remove_altq(struct pf_altq *a)
212 if ((hif = a->altq_disc) == NULL)
216 (void)hfsc_clear_interface(hif);
217 (void)hfsc_class_destroy(hif->hif_rootclass);
225 hfsc_add_queue(struct pf_altq *a)
228 struct hfsc_class *cl, *parent;
229 struct hfsc_opts *opts;
230 struct service_curve rtsc, lssc, ulsc;
232 if ((hif = a->altq_disc) == NULL)
235 opts = &a->pq_u.hfsc_opts;
237 if (a->parent_qid == HFSC_NULLCLASS_HANDLE &&
238 hif->hif_rootclass == NULL)
240 else if ((parent = clh_to_clp(hif, a->parent_qid)) == NULL)
246 if (clh_to_clp(hif, a->qid) != NULL)
249 rtsc.m1 = opts->rtsc_m1;
250 rtsc.d = opts->rtsc_d;
251 rtsc.m2 = opts->rtsc_m2;
252 lssc.m1 = opts->lssc_m1;
253 lssc.d = opts->lssc_d;
254 lssc.m2 = opts->lssc_m2;
255 ulsc.m1 = opts->ulsc_m1;
256 ulsc.d = opts->ulsc_d;
257 ulsc.m2 = opts->ulsc_m2;
259 cl = hfsc_class_create(hif, &rtsc, &lssc, &ulsc,
260 parent, a->qlimit, opts->flags, a->qid);
268 hfsc_remove_queue(struct pf_altq *a)
271 struct hfsc_class *cl;
273 if ((hif = a->altq_disc) == NULL)
276 if ((cl = clh_to_clp(hif, a->qid)) == NULL)
279 return (hfsc_class_destroy(cl));
283 hfsc_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
286 struct hfsc_class *cl;
287 struct hfsc_classstats stats;
290 if ((hif = altq_lookup(a->ifname, ALTQT_HFSC)) == NULL)
293 if ((cl = clh_to_clp(hif, a->qid)) == NULL)
296 if (*nbytes < sizeof(stats))
299 get_class_stats(&stats, cl);
301 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
303 *nbytes = sizeof(stats);
308 * bring the interface back to the initial state by discarding
309 * all the filters and classes except the root class.
312 hfsc_clear_interface(struct hfsc_if *hif)
314 struct hfsc_class *cl;
317 /* free the filters for this interface */
318 acc_discard_filters(&hif->hif_classifier, NULL, 1);
321 /* clear out the classes */
322 while (hif->hif_rootclass != NULL &&
323 (cl = hif->hif_rootclass->cl_children) != NULL) {
325 * remove the first leaf class found in the hierarchy
328 for (; cl != NULL; cl = hfsc_nextclass(cl)) {
329 if (!is_a_parent_class(cl)) {
330 (void)hfsc_class_destroy(cl);
340 hfsc_request(struct ifaltq *ifq, int req, void *arg)
342 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
344 IFQ_LOCK_ASSERT(ifq);
354 /* discard all the queued packets on the interface */
356 hfsc_purge(struct hfsc_if *hif)
358 struct hfsc_class *cl;
360 for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl))
361 if (!qempty(cl->cl_q))
363 if (ALTQ_IS_ENABLED(hif->hif_ifq))
364 hif->hif_ifq->ifq_len = 0;
368 hfsc_class_create(struct hfsc_if *hif, struct service_curve *rsc,
369 struct service_curve *fsc, struct service_curve *usc,
370 struct hfsc_class *parent, int qlimit, int flags, int qid)
372 struct hfsc_class *cl, *p;
375 if (hif->hif_classes >= HFSC_MAX_CLASSES)
379 if (flags & HFCF_RED) {
381 printf("hfsc_class_create: RED not configured for HFSC!\n");
387 cl = malloc(sizeof(struct hfsc_class), M_DEVBUF, M_NOWAIT | M_ZERO);
391 cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF, M_NOWAIT | M_ZERO);
392 if (cl->cl_q == NULL)
395 TAILQ_INIT(&cl->cl_actc);
398 qlimit = 50; /* use default */
399 qlimit(cl->cl_q) = qlimit;
400 qtype(cl->cl_q) = Q_DROPTAIL;
402 cl->cl_flags = flags;
404 if (flags & (HFCF_RED|HFCF_RIO)) {
405 int red_flags, red_pkttime;
409 if (rsc != NULL && rsc->m2 > m2)
411 if (fsc != NULL && fsc->m2 > m2)
413 if (usc != NULL && usc->m2 > m2)
417 if (flags & HFCF_ECN)
418 red_flags |= REDF_ECN;
420 if (flags & HFCF_CLEARDSCP)
421 red_flags |= RIOF_CLEARDSCP;
424 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
426 red_pkttime = (int64_t)hif->hif_ifq->altq_ifp->if_mtu
427 * 1000 * 1000 * 1000 / (m2 / 8);
428 if (flags & HFCF_RED) {
429 cl->cl_red = red_alloc(0, 0,
430 qlimit(cl->cl_q) * 10/100,
431 qlimit(cl->cl_q) * 30/100,
432 red_flags, red_pkttime);
433 if (cl->cl_red != NULL)
434 qtype(cl->cl_q) = Q_RED;
438 cl->cl_red = (red_t *)rio_alloc(0, NULL,
439 red_flags, red_pkttime);
440 if (cl->cl_red != NULL)
441 qtype(cl->cl_q) = Q_RIO;
445 #endif /* ALTQ_RED */
447 if (rsc != NULL && (rsc->m1 != 0 || rsc->m2 != 0)) {
448 cl->cl_rsc = malloc(sizeof(struct internal_sc),
450 if (cl->cl_rsc == NULL)
452 sc2isc(rsc, cl->cl_rsc);
453 rtsc_init(&cl->cl_deadline, cl->cl_rsc, 0, 0);
454 rtsc_init(&cl->cl_eligible, cl->cl_rsc, 0, 0);
456 if (fsc != NULL && (fsc->m1 != 0 || fsc->m2 != 0)) {
457 cl->cl_fsc = malloc(sizeof(struct internal_sc),
459 if (cl->cl_fsc == NULL)
461 sc2isc(fsc, cl->cl_fsc);
462 rtsc_init(&cl->cl_virtual, cl->cl_fsc, 0, 0);
464 if (usc != NULL && (usc->m1 != 0 || usc->m2 != 0)) {
465 cl->cl_usc = malloc(sizeof(struct internal_sc),
467 if (cl->cl_usc == NULL)
469 sc2isc(usc, cl->cl_usc);
470 rtsc_init(&cl->cl_ulimit, cl->cl_usc, 0, 0);
473 cl->cl_id = hif->hif_classid++;
476 cl->cl_parent = parent;
479 IFQ_LOCK(hif->hif_ifq);
483 * find a free slot in the class table. if the slot matching
484 * the lower bits of qid is free, use this slot. otherwise,
485 * use the first free slot.
487 i = qid % HFSC_MAX_CLASSES;
488 if (hif->hif_class_tbl[i] == NULL)
489 hif->hif_class_tbl[i] = cl;
491 for (i = 0; i < HFSC_MAX_CLASSES; i++)
492 if (hif->hif_class_tbl[i] == NULL) {
493 hif->hif_class_tbl[i] = cl;
496 if (i == HFSC_MAX_CLASSES) {
497 IFQ_UNLOCK(hif->hif_ifq);
503 if (flags & HFCF_DEFAULTCLASS)
504 hif->hif_defaultclass = cl;
506 if (parent == NULL) {
507 /* this is root class */
508 hif->hif_rootclass = cl;
510 /* add this class to the children list of the parent */
511 if ((p = parent->cl_children) == NULL)
512 parent->cl_children = cl;
514 while (p->cl_siblings != NULL)
519 IFQ_UNLOCK(hif->hif_ifq);
525 if (cl->cl_red != NULL) {
527 if (q_is_rio(cl->cl_q))
528 rio_destroy((rio_t *)cl->cl_red);
531 if (q_is_red(cl->cl_q))
532 red_destroy(cl->cl_red);
535 if (cl->cl_fsc != NULL)
536 free(cl->cl_fsc, M_DEVBUF);
537 if (cl->cl_rsc != NULL)
538 free(cl->cl_rsc, M_DEVBUF);
539 if (cl->cl_usc != NULL)
540 free(cl->cl_usc, M_DEVBUF);
541 if (cl->cl_q != NULL)
542 free(cl->cl_q, M_DEVBUF);
548 hfsc_class_destroy(struct hfsc_class *cl)
555 if (is_a_parent_class(cl))
559 IFQ_LOCK(cl->cl_hif->hif_ifq);
562 /* delete filters referencing to this class */
563 acc_discard_filters(&cl->cl_hif->hif_classifier, cl, 0);
564 #endif /* ALTQ3_COMPAT */
566 if (!qempty(cl->cl_q))
569 if (cl->cl_parent == NULL) {
570 /* this is root class */
572 struct hfsc_class *p = cl->cl_parent->cl_children;
575 cl->cl_parent->cl_children = cl->cl_siblings;
577 if (p->cl_siblings == cl) {
578 p->cl_siblings = cl->cl_siblings;
581 } while ((p = p->cl_siblings) != NULL);
585 for (i = 0; i < HFSC_MAX_CLASSES; i++)
586 if (cl->cl_hif->hif_class_tbl[i] == cl) {
587 cl->cl_hif->hif_class_tbl[i] = NULL;
591 cl->cl_hif->hif_classes--;
592 IFQ_UNLOCK(cl->cl_hif->hif_ifq);
595 if (cl->cl_red != NULL) {
597 if (q_is_rio(cl->cl_q))
598 rio_destroy((rio_t *)cl->cl_red);
601 if (q_is_red(cl->cl_q))
602 red_destroy(cl->cl_red);
606 IFQ_LOCK(cl->cl_hif->hif_ifq);
607 if (cl == cl->cl_hif->hif_rootclass)
608 cl->cl_hif->hif_rootclass = NULL;
609 if (cl == cl->cl_hif->hif_defaultclass)
610 cl->cl_hif->hif_defaultclass = NULL;
611 IFQ_UNLOCK(cl->cl_hif->hif_ifq);
613 if (cl->cl_usc != NULL)
614 free(cl->cl_usc, M_DEVBUF);
615 if (cl->cl_fsc != NULL)
616 free(cl->cl_fsc, M_DEVBUF);
617 if (cl->cl_rsc != NULL)
618 free(cl->cl_rsc, M_DEVBUF);
619 free(cl->cl_q, M_DEVBUF);
626 * hfsc_nextclass returns the next class in the tree.
628 * for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl))
631 static struct hfsc_class *
632 hfsc_nextclass(struct hfsc_class *cl)
634 if (cl->cl_children != NULL)
635 cl = cl->cl_children;
636 else if (cl->cl_siblings != NULL)
637 cl = cl->cl_siblings;
639 while ((cl = cl->cl_parent) != NULL)
640 if (cl->cl_siblings) {
641 cl = cl->cl_siblings;
650 * hfsc_enqueue is an enqueue function to be registered to
651 * (*altq_enqueue) in struct ifaltq.
654 hfsc_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
656 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
657 struct hfsc_class *cl;
661 IFQ_LOCK_ASSERT(ifq);
663 /* grab class set by classifier */
664 if ((m->m_flags & M_PKTHDR) == 0) {
665 /* should not happen */
666 printf("altq: packet for %s does not have pkthdr\n",
667 ifq->altq_ifp->if_xname);
672 if ((t = pf_find_mtag(m)) != NULL)
673 cl = clh_to_clp(hif, t->qid);
675 else if ((ifq->altq_flags & ALTQF_CLASSIFY) && pktattr != NULL)
676 cl = pktattr->pattr_class;
678 if (cl == NULL || is_a_parent_class(cl)) {
679 cl = hif->hif_defaultclass;
687 cl->cl_pktattr = pktattr; /* save proto hdr used by ECN */
690 cl->cl_pktattr = NULL;
692 if (hfsc_addq(cl, m) != 0) {
693 /* drop occurred. mbuf was freed in hfsc_addq. */
694 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, len);
698 cl->cl_hif->hif_packets++;
700 /* successfully queued. */
701 if (qlen(cl->cl_q) == 1)
702 set_active(cl, m_pktlen(m));
708 * hfsc_dequeue is a dequeue function to be registered to
709 * (*altq_dequeue) in struct ifaltq.
711 * note: ALTDQ_POLL returns the next packet without removing the packet
712 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
713 * ALTDQ_REMOVE must return the same packet if called immediately
717 hfsc_dequeue(struct ifaltq *ifq, int op)
719 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
720 struct hfsc_class *cl;
726 IFQ_LOCK_ASSERT(ifq);
728 if (hif->hif_packets == 0)
729 /* no packet in the tree */
732 cur_time = read_machclk();
734 if (op == ALTDQ_REMOVE && hif->hif_pollcache != NULL) {
736 cl = hif->hif_pollcache;
737 hif->hif_pollcache = NULL;
738 /* check if the class was scheduled by real-time criteria */
739 if (cl->cl_rsc != NULL)
740 realtime = (cl->cl_e <= cur_time);
743 * if there are eligible classes, use real-time criteria.
744 * find the class with the minimum deadline among
745 * the eligible classes.
747 if ((cl = hfsc_get_mindl(hif, cur_time))
755 * use link-sharing criteria
756 * get the class with the minimum vt in the hierarchy
758 cl = hif->hif_rootclass;
759 while (is_a_parent_class(cl)) {
761 cl = actlist_firstfit(cl, cur_time);
765 printf("%d fit but none found\n",fits);
770 * update parent's cl_cvtmin.
771 * don't update if the new vt is smaller.
773 if (cl->cl_parent->cl_cvtmin < cl->cl_vt)
774 cl->cl_parent->cl_cvtmin = cl->cl_vt;
781 if (op == ALTDQ_POLL) {
782 hif->hif_pollcache = cl;
790 panic("hfsc_dequeue:");
792 cl->cl_hif->hif_packets--;
794 PKTCNTR_ADD(&cl->cl_stats.xmit_cnt, len);
796 update_vf(cl, len, cur_time);
800 if (!qempty(cl->cl_q)) {
801 if (cl->cl_rsc != NULL) {
803 next_len = m_pktlen(qhead(cl->cl_q));
806 update_ed(cl, next_len);
808 update_d(cl, next_len);
811 /* the class becomes passive */
819 hfsc_addq(struct hfsc_class *cl, struct mbuf *m)
823 if (q_is_rio(cl->cl_q))
824 return rio_addq((rio_t *)cl->cl_red, cl->cl_q,
828 if (q_is_red(cl->cl_q))
829 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
831 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
836 if (cl->cl_flags & HFCF_CLEARDSCP)
837 write_dsfield(m, cl->cl_pktattr, 0);
845 hfsc_getq(struct hfsc_class *cl)
848 if (q_is_rio(cl->cl_q))
849 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
852 if (q_is_red(cl->cl_q))
853 return red_getq(cl->cl_red, cl->cl_q);
855 return _getq(cl->cl_q);
859 hfsc_pollq(struct hfsc_class *cl)
861 return qhead(cl->cl_q);
865 hfsc_purgeq(struct hfsc_class *cl)
869 if (qempty(cl->cl_q))
872 while ((m = _getq(cl->cl_q)) != NULL) {
873 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, m_pktlen(m));
875 cl->cl_hif->hif_packets--;
876 IFQ_DEC_LEN(cl->cl_hif->hif_ifq);
878 ASSERT(qlen(cl->cl_q) == 0);
880 update_vf(cl, 0, 0); /* remove cl from the actlist */
885 set_active(struct hfsc_class *cl, int len)
887 if (cl->cl_rsc != NULL)
889 if (cl->cl_fsc != NULL)
892 cl->cl_stats.period++;
896 set_passive(struct hfsc_class *cl)
898 if (cl->cl_rsc != NULL)
902 * actlist is now handled in update_vf() so that update_vf(cl, 0, 0)
903 * needs to be called explicitly to remove a class from actlist
908 init_ed(struct hfsc_class *cl, int next_len)
912 cur_time = read_machclk();
914 /* update the deadline curve */
915 rtsc_min(&cl->cl_deadline, cl->cl_rsc, cur_time, cl->cl_cumul);
918 * update the eligible curve.
919 * for concave, it is equal to the deadline curve.
920 * for convex, it is a linear curve with slope m2.
922 cl->cl_eligible = cl->cl_deadline;
923 if (cl->cl_rsc->sm1 <= cl->cl_rsc->sm2) {
924 cl->cl_eligible.dx = 0;
925 cl->cl_eligible.dy = 0;
928 /* compute e and d */
929 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
930 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
936 update_ed(struct hfsc_class *cl, int next_len)
938 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
939 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
945 update_d(struct hfsc_class *cl, int next_len)
947 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
951 init_vf(struct hfsc_class *cl, int len)
953 struct hfsc_class *max_cl, *p;
954 u_int64_t vt, f, cur_time;
959 for ( ; cl->cl_parent != NULL; cl = cl->cl_parent) {
961 if (go_active && cl->cl_nactive++ == 0)
967 max_cl = TAILQ_LAST(&cl->cl_parent->cl_actc, acthead);
968 if (max_cl != NULL) {
970 * set vt to the average of the min and max
971 * classes. if the parent's period didn't
972 * change, don't decrease vt of the class.
975 if (cl->cl_parent->cl_cvtmin != 0)
976 vt = (cl->cl_parent->cl_cvtmin + vt)/2;
978 if (cl->cl_parent->cl_vtperiod !=
979 cl->cl_parentperiod || vt > cl->cl_vt)
983 * first child for a new parent backlog period.
984 * add parent's cvtmax to vtoff of children
985 * to make a new vt (vtoff + vt) larger than
986 * the vt in the last period for all children.
988 vt = cl->cl_parent->cl_cvtmax;
989 for (p = cl->cl_parent->cl_children; p != NULL;
993 cl->cl_parent->cl_cvtmax = 0;
994 cl->cl_parent->cl_cvtmin = 0;
996 cl->cl_initvt = cl->cl_vt;
998 /* update the virtual curve */
999 vt = cl->cl_vt + cl->cl_vtoff;
1000 rtsc_min(&cl->cl_virtual, cl->cl_fsc, vt, cl->cl_total);
1001 if (cl->cl_virtual.x == vt) {
1002 cl->cl_virtual.x -= cl->cl_vtoff;
1007 cl->cl_vtperiod++; /* increment vt period */
1008 cl->cl_parentperiod = cl->cl_parent->cl_vtperiod;
1009 if (cl->cl_parent->cl_nactive == 0)
1010 cl->cl_parentperiod++;
1015 if (cl->cl_usc != NULL) {
1016 /* class has upper limit curve */
1018 cur_time = read_machclk();
1020 /* update the ulimit curve */
1021 rtsc_min(&cl->cl_ulimit, cl->cl_usc, cur_time,
1024 cl->cl_myf = rtsc_y2x(&cl->cl_ulimit,
1030 if (cl->cl_myf > cl->cl_cfmin)
1034 if (f != cl->cl_f) {
1036 update_cfmin(cl->cl_parent);
1042 update_vf(struct hfsc_class *cl, int len, u_int64_t cur_time)
1044 u_int64_t f, myf_bound, delta;
1047 go_passive = qempty(cl->cl_q);
1049 for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
1051 cl->cl_total += len;
1053 if (cl->cl_fsc == NULL || cl->cl_nactive == 0)
1056 if (go_passive && --cl->cl_nactive == 0)
1062 /* no more active child, going passive */
1064 /* update cvtmax of the parent class */
1065 if (cl->cl_vt > cl->cl_parent->cl_cvtmax)
1066 cl->cl_parent->cl_cvtmax = cl->cl_vt;
1068 /* remove this class from the vt list */
1071 update_cfmin(cl->cl_parent);
1079 cl->cl_vt = rtsc_y2x(&cl->cl_virtual, cl->cl_total)
1080 - cl->cl_vtoff + cl->cl_vtadj;
1083 * if vt of the class is smaller than cvtmin,
1084 * the class was skipped in the past due to non-fit.
1085 * if so, we need to adjust vtadj.
1087 if (cl->cl_vt < cl->cl_parent->cl_cvtmin) {
1088 cl->cl_vtadj += cl->cl_parent->cl_cvtmin - cl->cl_vt;
1089 cl->cl_vt = cl->cl_parent->cl_cvtmin;
1092 /* update the vt list */
1095 if (cl->cl_usc != NULL) {
1096 cl->cl_myf = cl->cl_myfadj
1097 + rtsc_y2x(&cl->cl_ulimit, cl->cl_total);
1100 * if myf lags behind by more than one clock tick
1101 * from the current time, adjust myfadj to prevent
1102 * a rate-limited class from going greedy.
1103 * in a steady state under rate-limiting, myf
1104 * fluctuates within one clock tick.
1106 myf_bound = cur_time - machclk_per_tick;
1107 if (cl->cl_myf < myf_bound) {
1108 delta = cur_time - cl->cl_myf;
1109 cl->cl_myfadj += delta;
1110 cl->cl_myf += delta;
1114 /* cl_f is max(cl_myf, cl_cfmin) */
1115 if (cl->cl_myf > cl->cl_cfmin)
1119 if (f != cl->cl_f) {
1121 update_cfmin(cl->cl_parent);
1127 update_cfmin(struct hfsc_class *cl)
1129 struct hfsc_class *p;
1132 if (TAILQ_EMPTY(&cl->cl_actc)) {
1136 cfmin = HT_INFINITY;
1137 TAILQ_FOREACH(p, &cl->cl_actc, cl_actlist) {
1142 if (p->cl_f < cfmin)
1145 cl->cl_cfmin = cfmin;
1149 * TAILQ based ellist and actlist implementation
1150 * (ion wanted to make a calendar queue based implementation)
1153 * eligible list holds backlogged classes being sorted by their eligible times.
1154 * there is one eligible list per interface.
1158 ellist_insert(struct hfsc_class *cl)
1160 struct hfsc_if *hif = cl->cl_hif;
1161 struct hfsc_class *p;
1163 /* check the last entry first */
1164 if ((p = TAILQ_LAST(&hif->hif_eligible, elighead)) == NULL ||
1165 p->cl_e <= cl->cl_e) {
1166 TAILQ_INSERT_TAIL(&hif->hif_eligible, cl, cl_ellist);
1170 TAILQ_FOREACH(p, &hif->hif_eligible, cl_ellist) {
1171 if (cl->cl_e < p->cl_e) {
1172 TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1176 ASSERT(0); /* should not reach here */
1180 ellist_remove(struct hfsc_class *cl)
1182 struct hfsc_if *hif = cl->cl_hif;
1184 TAILQ_REMOVE(&hif->hif_eligible, cl, cl_ellist);
1188 ellist_update(struct hfsc_class *cl)
1190 struct hfsc_if *hif = cl->cl_hif;
1191 struct hfsc_class *p, *last;
1194 * the eligible time of a class increases monotonically.
1195 * if the next entry has a larger eligible time, nothing to do.
1197 p = TAILQ_NEXT(cl, cl_ellist);
1198 if (p == NULL || cl->cl_e <= p->cl_e)
1201 /* check the last entry */
1202 last = TAILQ_LAST(&hif->hif_eligible, elighead);
1203 ASSERT(last != NULL);
1204 if (last->cl_e <= cl->cl_e) {
1205 TAILQ_REMOVE(&hif->hif_eligible, cl, cl_ellist);
1206 TAILQ_INSERT_TAIL(&hif->hif_eligible, cl, cl_ellist);
1211 * the new position must be between the next entry
1212 * and the last entry
1214 while ((p = TAILQ_NEXT(p, cl_ellist)) != NULL) {
1215 if (cl->cl_e < p->cl_e) {
1216 TAILQ_REMOVE(&hif->hif_eligible, cl, cl_ellist);
1217 TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1221 ASSERT(0); /* should not reach here */
1224 /* find the class with the minimum deadline among the eligible classes */
1226 hfsc_get_mindl(struct hfsc_if *hif, u_int64_t cur_time)
1228 struct hfsc_class *p, *cl = NULL;
1230 TAILQ_FOREACH(p, &hif->hif_eligible, cl_ellist) {
1231 if (p->cl_e > cur_time)
1233 if (cl == NULL || p->cl_d < cl->cl_d)
1240 * active children list holds backlogged child classes being sorted
1241 * by their virtual time.
1242 * each intermediate class has one active children list.
1246 actlist_insert(struct hfsc_class *cl)
1248 struct hfsc_class *p;
1250 /* check the last entry first */
1251 if ((p = TAILQ_LAST(&cl->cl_parent->cl_actc, acthead)) == NULL
1252 || p->cl_vt <= cl->cl_vt) {
1253 TAILQ_INSERT_TAIL(&cl->cl_parent->cl_actc, cl, cl_actlist);
1257 TAILQ_FOREACH(p, &cl->cl_parent->cl_actc, cl_actlist) {
1258 if (cl->cl_vt < p->cl_vt) {
1259 TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1263 ASSERT(0); /* should not reach here */
1267 actlist_remove(struct hfsc_class *cl)
1269 TAILQ_REMOVE(&cl->cl_parent->cl_actc, cl, cl_actlist);
1273 actlist_update(struct hfsc_class *cl)
1275 struct hfsc_class *p, *last;
1278 * the virtual time of a class increases monotonically during its
1279 * backlogged period.
1280 * if the next entry has a larger virtual time, nothing to do.
1282 p = TAILQ_NEXT(cl, cl_actlist);
1283 if (p == NULL || cl->cl_vt < p->cl_vt)
1286 /* check the last entry */
1287 last = TAILQ_LAST(&cl->cl_parent->cl_actc, acthead);
1288 ASSERT(last != NULL);
1289 if (last->cl_vt <= cl->cl_vt) {
1290 TAILQ_REMOVE(&cl->cl_parent->cl_actc, cl, cl_actlist);
1291 TAILQ_INSERT_TAIL(&cl->cl_parent->cl_actc, cl, cl_actlist);
1296 * the new position must be between the next entry
1297 * and the last entry
1299 while ((p = TAILQ_NEXT(p, cl_actlist)) != NULL) {
1300 if (cl->cl_vt < p->cl_vt) {
1301 TAILQ_REMOVE(&cl->cl_parent->cl_actc, cl, cl_actlist);
1302 TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1306 ASSERT(0); /* should not reach here */
1309 static struct hfsc_class *
1310 actlist_firstfit(struct hfsc_class *cl, u_int64_t cur_time)
1312 struct hfsc_class *p;
1314 TAILQ_FOREACH(p, &cl->cl_actc, cl_actlist) {
1315 if (p->cl_f <= cur_time)
1322 * service curve support functions
1324 * external service curve parameters
1327 * internal service curve parameters
1328 * sm: (bytes/tsc_interval) << SM_SHIFT
1329 * ism: (tsc_count/byte) << ISM_SHIFT
1332 * SM_SHIFT and ISM_SHIFT are scaled in order to keep effective digits.
1333 * we should be able to handle 100K-1Gbps linkspeed with 200Hz-1GHz CPU
1334 * speed. SM_SHIFT and ISM_SHIFT are selected to have at least 3 effective
1335 * digits in decimal using the following table.
1337 * bits/sec 100Kbps 1Mbps 10Mbps 100Mbps 1Gbps
1338 * ----------+-------------------------------------------------------
1339 * bytes/nsec 12.5e-6 125e-6 1250e-6 12500e-6 125000e-6
1340 * sm(500MHz) 25.0e-6 250e-6 2500e-6 25000e-6 250000e-6
1341 * sm(200MHz) 62.5e-6 625e-6 6250e-6 62500e-6 625000e-6
1343 * nsec/byte 80000 8000 800 80 8
1344 * ism(500MHz) 40000 4000 400 40 4
1345 * ism(200MHz) 16000 1600 160 16 1.6
1348 #define ISM_SHIFT 10
1350 #define SM_MASK ((1LL << SM_SHIFT) - 1)
1351 #define ISM_MASK ((1LL << ISM_SHIFT) - 1)
1353 static __inline u_int64_t
1354 seg_x2y(u_int64_t x, u_int64_t sm)
1360 * y = x * sm >> SM_SHIFT
1361 * but divide it for the upper and lower bits to avoid overflow
1363 y = (x >> SM_SHIFT) * sm + (((x & SM_MASK) * sm) >> SM_SHIFT);
1367 static __inline u_int64_t
1368 seg_y2x(u_int64_t y, u_int64_t ism)
1374 else if (ism == HT_INFINITY)
1377 x = (y >> ISM_SHIFT) * ism
1378 + (((y & ISM_MASK) * ism) >> ISM_SHIFT);
1383 static __inline u_int64_t
1388 sm = ((u_int64_t)m << SM_SHIFT) / 8 / machclk_freq;
1392 static __inline u_int64_t
1400 ism = ((u_int64_t)machclk_freq << ISM_SHIFT) * 8 / m;
1404 static __inline u_int64_t
1409 dx = ((u_int64_t)d * machclk_freq) / 1000;
1418 m = (sm * 8 * machclk_freq) >> SM_SHIFT;
1427 d = dx * 1000 / machclk_freq;
1432 sc2isc(struct service_curve *sc, struct internal_sc *isc)
1434 isc->sm1 = m2sm(sc->m1);
1435 isc->ism1 = m2ism(sc->m1);
1436 isc->dx = d2dx(sc->d);
1437 isc->dy = seg_x2y(isc->dx, isc->sm1);
1438 isc->sm2 = m2sm(sc->m2);
1439 isc->ism2 = m2ism(sc->m2);
1443 * initialize the runtime service curve with the given internal
1444 * service curve starting at (x, y).
1447 rtsc_init(struct runtime_sc *rtsc, struct internal_sc * isc, u_int64_t x,
1452 rtsc->sm1 = isc->sm1;
1453 rtsc->ism1 = isc->ism1;
1456 rtsc->sm2 = isc->sm2;
1457 rtsc->ism2 = isc->ism2;
1461 * calculate the y-projection of the runtime service curve by the
1462 * given x-projection value
1465 rtsc_y2x(struct runtime_sc *rtsc, u_int64_t y)
1471 else if (y <= rtsc->y + rtsc->dy) {
1472 /* x belongs to the 1st segment */
1474 x = rtsc->x + rtsc->dx;
1476 x = rtsc->x + seg_y2x(y - rtsc->y, rtsc->ism1);
1478 /* x belongs to the 2nd segment */
1479 x = rtsc->x + rtsc->dx
1480 + seg_y2x(y - rtsc->y - rtsc->dy, rtsc->ism2);
1486 rtsc_x2y(struct runtime_sc *rtsc, u_int64_t x)
1492 else if (x <= rtsc->x + rtsc->dx)
1493 /* y belongs to the 1st segment */
1494 y = rtsc->y + seg_x2y(x - rtsc->x, rtsc->sm1);
1496 /* y belongs to the 2nd segment */
1497 y = rtsc->y + rtsc->dy
1498 + seg_x2y(x - rtsc->x - rtsc->dx, rtsc->sm2);
1503 * update the runtime service curve by taking the minimum of the current
1504 * runtime service curve and the service curve starting at (x, y).
1507 rtsc_min(struct runtime_sc *rtsc, struct internal_sc *isc, u_int64_t x,
1510 u_int64_t y1, y2, dx, dy;
1512 if (isc->sm1 <= isc->sm2) {
1513 /* service curve is convex */
1514 y1 = rtsc_x2y(rtsc, x);
1516 /* the current rtsc is smaller */
1524 * service curve is concave
1525 * compute the two y values of the current rtsc
1529 y1 = rtsc_x2y(rtsc, x);
1531 /* rtsc is below isc, no change to rtsc */
1535 y2 = rtsc_x2y(rtsc, x + isc->dx);
1536 if (y2 >= y + isc->dy) {
1537 /* rtsc is above isc, replace rtsc by isc */
1546 * the two curves intersect
1547 * compute the offsets (dx, dy) using the reverse
1548 * function of seg_x2y()
1549 * seg_x2y(dx, sm1) == seg_x2y(dx, sm2) + (y1 - y)
1551 dx = ((y1 - y) << SM_SHIFT) / (isc->sm1 - isc->sm2);
1553 * check if (x, y1) belongs to the 1st segment of rtsc.
1554 * if so, add the offset.
1556 if (rtsc->x + rtsc->dx > x)
1557 dx += rtsc->x + rtsc->dx - x;
1558 dy = seg_x2y(dx, isc->sm1);
1568 get_class_stats(struct hfsc_classstats *sp, struct hfsc_class *cl)
1570 sp->class_id = cl->cl_id;
1571 sp->class_handle = cl->cl_handle;
1573 if (cl->cl_rsc != NULL) {
1574 sp->rsc.m1 = sm2m(cl->cl_rsc->sm1);
1575 sp->rsc.d = dx2d(cl->cl_rsc->dx);
1576 sp->rsc.m2 = sm2m(cl->cl_rsc->sm2);
1582 if (cl->cl_fsc != NULL) {
1583 sp->fsc.m1 = sm2m(cl->cl_fsc->sm1);
1584 sp->fsc.d = dx2d(cl->cl_fsc->dx);
1585 sp->fsc.m2 = sm2m(cl->cl_fsc->sm2);
1591 if (cl->cl_usc != NULL) {
1592 sp->usc.m1 = sm2m(cl->cl_usc->sm1);
1593 sp->usc.d = dx2d(cl->cl_usc->dx);
1594 sp->usc.m2 = sm2m(cl->cl_usc->sm2);
1601 sp->total = cl->cl_total;
1602 sp->cumul = cl->cl_cumul;
1609 sp->initvt = cl->cl_initvt;
1610 sp->vtperiod = cl->cl_vtperiod;
1611 sp->parentperiod = cl->cl_parentperiod;
1612 sp->nactive = cl->cl_nactive;
1613 sp->vtoff = cl->cl_vtoff;
1614 sp->cvtmax = cl->cl_cvtmax;
1615 sp->myf = cl->cl_myf;
1616 sp->cfmin = cl->cl_cfmin;
1617 sp->cvtmin = cl->cl_cvtmin;
1618 sp->myfadj = cl->cl_myfadj;
1619 sp->vtadj = cl->cl_vtadj;
1621 sp->cur_time = read_machclk();
1622 sp->machclk_freq = machclk_freq;
1624 sp->qlength = qlen(cl->cl_q);
1625 sp->qlimit = qlimit(cl->cl_q);
1626 sp->xmit_cnt = cl->cl_stats.xmit_cnt;
1627 sp->drop_cnt = cl->cl_stats.drop_cnt;
1628 sp->period = cl->cl_stats.period;
1630 sp->qtype = qtype(cl->cl_q);
1632 if (q_is_red(cl->cl_q))
1633 red_getstats(cl->cl_red, &sp->red[0]);
1636 if (q_is_rio(cl->cl_q))
1637 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
1641 /* convert a class handle to the corresponding class pointer */
1642 static struct hfsc_class *
1643 clh_to_clp(struct hfsc_if *hif, u_int32_t chandle)
1646 struct hfsc_class *cl;
1651 * first, try optimistically the slot matching the lower bits of
1652 * the handle. if it fails, do the linear table search.
1654 i = chandle % HFSC_MAX_CLASSES;
1655 if ((cl = hif->hif_class_tbl[i]) != NULL && cl->cl_handle == chandle)
1657 for (i = 0; i < HFSC_MAX_CLASSES; i++)
1658 if ((cl = hif->hif_class_tbl[i]) != NULL &&
1659 cl->cl_handle == chandle)
1665 static struct hfsc_if *
1666 hfsc_attach(ifq, bandwidth)
1670 struct hfsc_if *hif;
1672 hif = malloc(sizeof(struct hfsc_if), M_DEVBUF, M_WAITOK);
1675 bzero(hif, sizeof(struct hfsc_if));
1677 hif->hif_eligible = ellist_alloc();
1678 if (hif->hif_eligible == NULL) {
1679 free(hif, M_DEVBUF);
1685 /* add this state to the hfsc list */
1686 hif->hif_next = hif_list;
1694 struct hfsc_if *hif;
1696 (void)hfsc_clear_interface(hif);
1697 (void)hfsc_class_destroy(hif->hif_rootclass);
1699 /* remove this interface from the hif list */
1700 if (hif_list == hif)
1701 hif_list = hif->hif_next;
1705 for (h = hif_list; h != NULL; h = h->hif_next)
1706 if (h->hif_next == hif) {
1707 h->hif_next = hif->hif_next;
1713 ellist_destroy(hif->hif_eligible);
1715 free(hif, M_DEVBUF);
1721 hfsc_class_modify(cl, rsc, fsc, usc)
1722 struct hfsc_class *cl;
1723 struct service_curve *rsc, *fsc, *usc;
1725 struct internal_sc *rsc_tmp, *fsc_tmp, *usc_tmp;
1729 rsc_tmp = fsc_tmp = usc_tmp = NULL;
1730 if (rsc != NULL && (rsc->m1 != 0 || rsc->m2 != 0) &&
1731 cl->cl_rsc == NULL) {
1732 rsc_tmp = malloc(sizeof(struct internal_sc),
1733 M_DEVBUF, M_WAITOK);
1734 if (rsc_tmp == NULL)
1737 if (fsc != NULL && (fsc->m1 != 0 || fsc->m2 != 0) &&
1738 cl->cl_fsc == NULL) {
1739 fsc_tmp = malloc(sizeof(struct internal_sc),
1740 M_DEVBUF, M_WAITOK);
1741 if (fsc_tmp == NULL) {
1746 if (usc != NULL && (usc->m1 != 0 || usc->m2 != 0) &&
1747 cl->cl_usc == NULL) {
1748 usc_tmp = malloc(sizeof(struct internal_sc),
1749 M_DEVBUF, M_WAITOK);
1750 if (usc_tmp == NULL) {
1757 cur_time = read_machclk();
1759 IFQ_LOCK(cl->cl_hif->hif_ifq);
1762 if (rsc->m1 == 0 && rsc->m2 == 0) {
1763 if (cl->cl_rsc != NULL) {
1764 if (!qempty(cl->cl_q))
1766 free(cl->cl_rsc, M_DEVBUF);
1770 if (cl->cl_rsc == NULL)
1771 cl->cl_rsc = rsc_tmp;
1772 sc2isc(rsc, cl->cl_rsc);
1773 rtsc_init(&cl->cl_deadline, cl->cl_rsc, cur_time,
1775 cl->cl_eligible = cl->cl_deadline;
1776 if (cl->cl_rsc->sm1 <= cl->cl_rsc->sm2) {
1777 cl->cl_eligible.dx = 0;
1778 cl->cl_eligible.dy = 0;
1784 if (fsc->m1 == 0 && fsc->m2 == 0) {
1785 if (cl->cl_fsc != NULL) {
1786 if (!qempty(cl->cl_q))
1788 free(cl->cl_fsc, M_DEVBUF);
1792 if (cl->cl_fsc == NULL)
1793 cl->cl_fsc = fsc_tmp;
1794 sc2isc(fsc, cl->cl_fsc);
1795 rtsc_init(&cl->cl_virtual, cl->cl_fsc, cl->cl_vt,
1801 if (usc->m1 == 0 && usc->m2 == 0) {
1802 if (cl->cl_usc != NULL) {
1803 free(cl->cl_usc, M_DEVBUF);
1808 if (cl->cl_usc == NULL)
1809 cl->cl_usc = usc_tmp;
1810 sc2isc(usc, cl->cl_usc);
1811 rtsc_init(&cl->cl_ulimit, cl->cl_usc, cur_time,
1816 if (!qempty(cl->cl_q)) {
1817 if (cl->cl_rsc != NULL)
1818 update_ed(cl, m_pktlen(qhead(cl->cl_q)));
1819 if (cl->cl_fsc != NULL)
1820 update_vf(cl, 0, cur_time);
1821 /* is this enough? */
1824 IFQ_UNLOCK(cl->cl_hif->hif_ifq);
1831 * hfsc device interface
1834 hfscopen(dev, flag, fmt, p)
1837 #if (__FreeBSD_version > 500000)
1843 if (machclk_freq == 0)
1846 if (machclk_freq == 0) {
1847 printf("hfsc: no cpu clock available!\n");
1851 /* everything will be done when the queueing scheme is attached. */
1856 hfscclose(dev, flag, fmt, p)
1859 #if (__FreeBSD_version > 500000)
1865 struct hfsc_if *hif;
1868 while ((hif = hif_list) != NULL) {
1870 if (ALTQ_IS_ENABLED(hif->hif_ifq))
1871 altq_disable(hif->hif_ifq);
1873 err = altq_detach(hif->hif_ifq);
1875 err = hfsc_detach(hif);
1876 if (err != 0 && error == 0)
1884 hfscioctl(dev, cmd, addr, flag, p)
1889 #if (__FreeBSD_version > 500000)
1895 struct hfsc_if *hif;
1896 struct hfsc_interface *ifacep;
1899 /* check super-user privilege */
1904 #if (__FreeBSD_version > 700000)
1905 if ((error = priv_check(p, PRIV_ALTQ_MANAGE)) != 0)
1907 #elsif (__FreeBSD_version > 400000)
1908 if ((error = suser(p)) != 0)
1911 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1919 case HFSC_IF_ATTACH:
1920 error = hfsccmd_if_attach((struct hfsc_attach *)addr);
1923 case HFSC_IF_DETACH:
1924 error = hfsccmd_if_detach((struct hfsc_interface *)addr);
1929 case HFSC_CLEAR_HIERARCHY:
1930 ifacep = (struct hfsc_interface *)addr;
1931 if ((hif = altq_lookup(ifacep->hfsc_ifname,
1932 ALTQT_HFSC)) == NULL) {
1940 if (hif->hif_defaultclass == NULL) {
1942 printf("hfsc: no default class\n");
1947 error = altq_enable(hif->hif_ifq);
1951 error = altq_disable(hif->hif_ifq);
1954 case HFSC_CLEAR_HIERARCHY:
1955 hfsc_clear_interface(hif);
1960 case HFSC_ADD_CLASS:
1961 error = hfsccmd_add_class((struct hfsc_add_class *)addr);
1964 case HFSC_DEL_CLASS:
1965 error = hfsccmd_delete_class((struct hfsc_delete_class *)addr);
1968 case HFSC_MOD_CLASS:
1969 error = hfsccmd_modify_class((struct hfsc_modify_class *)addr);
1972 case HFSC_ADD_FILTER:
1973 error = hfsccmd_add_filter((struct hfsc_add_filter *)addr);
1976 case HFSC_DEL_FILTER:
1977 error = hfsccmd_delete_filter((struct hfsc_delete_filter *)addr);
1981 error = hfsccmd_class_stats((struct hfsc_class_stats *)addr);
1992 hfsccmd_if_attach(ap)
1993 struct hfsc_attach *ap;
1995 struct hfsc_if *hif;
1999 if ((ifp = ifunit(ap->iface.hfsc_ifname)) == NULL)
2002 if ((hif = hfsc_attach(&ifp->if_snd, ap->bandwidth)) == NULL)
2006 * set HFSC to this ifnet structure.
2008 if ((error = altq_attach(&ifp->if_snd, ALTQT_HFSC, hif,
2009 hfsc_enqueue, hfsc_dequeue, hfsc_request,
2010 &hif->hif_classifier, acc_classify)) != 0)
2011 (void)hfsc_detach(hif);
2017 hfsccmd_if_detach(ap)
2018 struct hfsc_interface *ap;
2020 struct hfsc_if *hif;
2023 if ((hif = altq_lookup(ap->hfsc_ifname, ALTQT_HFSC)) == NULL)
2026 if (ALTQ_IS_ENABLED(hif->hif_ifq))
2027 altq_disable(hif->hif_ifq);
2029 if ((error = altq_detach(hif->hif_ifq)))
2032 return hfsc_detach(hif);
2036 hfsccmd_add_class(ap)
2037 struct hfsc_add_class *ap;
2039 struct hfsc_if *hif;
2040 struct hfsc_class *cl, *parent;
2043 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2046 if (ap->parent_handle == HFSC_NULLCLASS_HANDLE &&
2047 hif->hif_rootclass == NULL)
2049 else if ((parent = clh_to_clp(hif, ap->parent_handle)) == NULL)
2052 /* assign a class handle (use a free slot number for now) */
2053 for (i = 1; i < HFSC_MAX_CLASSES; i++)
2054 if (hif->hif_class_tbl[i] == NULL)
2056 if (i == HFSC_MAX_CLASSES)
2059 if ((cl = hfsc_class_create(hif, &ap->service_curve, NULL, NULL,
2060 parent, ap->qlimit, ap->flags, i)) == NULL)
2063 /* return a class handle to the user */
2064 ap->class_handle = i;
2070 hfsccmd_delete_class(ap)
2071 struct hfsc_delete_class *ap;
2073 struct hfsc_if *hif;
2074 struct hfsc_class *cl;
2076 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2079 if ((cl = clh_to_clp(hif, ap->class_handle)) == NULL)
2082 return hfsc_class_destroy(cl);
2086 hfsccmd_modify_class(ap)
2087 struct hfsc_modify_class *ap;
2089 struct hfsc_if *hif;
2090 struct hfsc_class *cl;
2091 struct service_curve *rsc = NULL;
2092 struct service_curve *fsc = NULL;
2093 struct service_curve *usc = NULL;
2095 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2098 if ((cl = clh_to_clp(hif, ap->class_handle)) == NULL)
2101 if (ap->sctype & HFSC_REALTIMESC)
2102 rsc = &ap->service_curve;
2103 if (ap->sctype & HFSC_LINKSHARINGSC)
2104 fsc = &ap->service_curve;
2105 if (ap->sctype & HFSC_UPPERLIMITSC)
2106 usc = &ap->service_curve;
2108 return hfsc_class_modify(cl, rsc, fsc, usc);
2112 hfsccmd_add_filter(ap)
2113 struct hfsc_add_filter *ap;
2115 struct hfsc_if *hif;
2116 struct hfsc_class *cl;
2118 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2121 if ((cl = clh_to_clp(hif, ap->class_handle)) == NULL)
2124 if (is_a_parent_class(cl)) {
2126 printf("hfsccmd_add_filter: not a leaf class!\n");
2131 return acc_add_filter(&hif->hif_classifier, &ap->filter,
2132 cl, &ap->filter_handle);
2136 hfsccmd_delete_filter(ap)
2137 struct hfsc_delete_filter *ap;
2139 struct hfsc_if *hif;
2141 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2144 return acc_delete_filter(&hif->hif_classifier,
2149 hfsccmd_class_stats(ap)
2150 struct hfsc_class_stats *ap;
2152 struct hfsc_if *hif;
2153 struct hfsc_class *cl;
2154 struct hfsc_classstats stats, *usp;
2155 int n, nclasses, error;
2157 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2160 ap->cur_time = read_machclk();
2161 ap->machclk_freq = machclk_freq;
2162 ap->hif_classes = hif->hif_classes;
2163 ap->hif_packets = hif->hif_packets;
2165 /* skip the first N classes in the tree */
2166 nclasses = ap->nskip;
2167 for (cl = hif->hif_rootclass, n = 0; cl != NULL && n < nclasses;
2168 cl = hfsc_nextclass(cl), n++)
2173 /* then, read the next N classes in the tree */
2174 nclasses = ap->nclasses;
2176 for (n = 0; cl != NULL && n < nclasses; cl = hfsc_nextclass(cl), n++) {
2178 get_class_stats(&stats, cl);
2180 if ((error = copyout((caddr_t)&stats, (caddr_t)usp++,
2181 sizeof(stats))) != 0)
2192 static struct altqsw hfsc_sw =
2193 {"hfsc", hfscopen, hfscclose, hfscioctl};
2195 ALTQ_MODULE(altq_hfsc, ALTQT_HFSC, &hfsc_sw);
2196 MODULE_DEPEND(altq_hfsc, altq_red, 1, 1, 1);
2197 MODULE_DEPEND(altq_hfsc, altq_rio, 1, 1, 1);
2199 #endif /* KLD_MODULE */
2200 #endif /* ALTQ3_COMPAT */
2202 #endif /* ALTQ_HFSC */