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__)
47 #if (__FreeBSD__ != 2)
50 #include "opt_inet6.h"
53 #endif /* __FreeBSD__ || __NetBSD__ */
55 #ifdef ALTQ_HFSC /* hfsc is enabled by ALTQ_HFSC option in opt_altq.h */
57 #include <sys/param.h>
58 #include <sys/malloc.h>
60 #include <sys/socket.h>
61 #include <sys/systm.h>
62 #include <sys/errno.h>
63 #include <sys/queue.h>
64 #if 1 /* ALTQ3_COMPAT */
65 #include <sys/sockio.h>
67 #include <sys/kernel.h>
68 #endif /* ALTQ3_COMPAT */
71 #include <netinet/in.h>
73 #include <net/pfvar.h>
74 #include <altq/altq.h>
75 #include <altq/altq_hfsc.h>
77 #include <altq/altq_conf.h>
83 static int hfsc_clear_interface(struct hfsc_if *);
84 static int hfsc_request(struct ifaltq *, int, void *);
85 static void hfsc_purge(struct hfsc_if *);
86 static struct hfsc_class *hfsc_class_create(struct hfsc_if *,
87 struct service_curve *, struct service_curve *, struct service_curve *,
88 struct hfsc_class *, int, int, int);
89 static int hfsc_class_destroy(struct hfsc_class *);
90 static struct hfsc_class *hfsc_nextclass(struct hfsc_class *);
91 static int hfsc_enqueue(struct ifaltq *, struct mbuf *,
92 struct altq_pktattr *);
93 static struct mbuf *hfsc_dequeue(struct ifaltq *, int);
95 static int hfsc_addq(struct hfsc_class *, struct mbuf *);
96 static struct mbuf *hfsc_getq(struct hfsc_class *);
97 static struct mbuf *hfsc_pollq(struct hfsc_class *);
98 static void hfsc_purgeq(struct hfsc_class *);
100 static void update_cfmin(struct hfsc_class *);
101 static void set_active(struct hfsc_class *, int);
102 static void set_passive(struct hfsc_class *);
104 static void init_ed(struct hfsc_class *, int);
105 static void update_ed(struct hfsc_class *, int);
106 static void update_d(struct hfsc_class *, int);
107 static void init_vf(struct hfsc_class *, int);
108 static void update_vf(struct hfsc_class *, int, u_int64_t);
109 static ellist_t *ellist_alloc(void);
110 static void ellist_destroy(ellist_t *);
111 static void ellist_insert(struct hfsc_class *);
112 static void ellist_remove(struct hfsc_class *);
113 static void ellist_update(struct hfsc_class *);
114 struct hfsc_class *ellist_get_mindl(ellist_t *, u_int64_t);
115 static actlist_t *actlist_alloc(void);
116 static void actlist_destroy(actlist_t *);
117 static void actlist_insert(struct hfsc_class *);
118 static void actlist_remove(struct hfsc_class *);
119 static void actlist_update(struct hfsc_class *);
121 static struct hfsc_class *actlist_firstfit(struct hfsc_class *,
124 static __inline u_int64_t seg_x2y(u_int64_t, u_int64_t);
125 static __inline u_int64_t seg_y2x(u_int64_t, u_int64_t);
126 static __inline u_int64_t m2sm(u_int);
127 static __inline u_int64_t m2ism(u_int);
128 static __inline u_int64_t d2dx(u_int);
129 static u_int sm2m(u_int64_t);
130 static u_int dx2d(u_int64_t);
132 static void sc2isc(struct service_curve *, struct internal_sc *);
133 static void rtsc_init(struct runtime_sc *, struct internal_sc *,
134 u_int64_t, u_int64_t);
135 static u_int64_t rtsc_y2x(struct runtime_sc *, u_int64_t);
136 static u_int64_t rtsc_x2y(struct runtime_sc *, u_int64_t);
137 static void rtsc_min(struct runtime_sc *, struct internal_sc *,
138 u_int64_t, u_int64_t);
140 static void get_class_stats(struct hfsc_classstats *,
141 struct hfsc_class *);
142 static struct hfsc_class *clh_to_clp(struct hfsc_if *, u_int32_t);
146 static struct hfsc_if *hfsc_attach(struct ifaltq *, u_int);
147 static int hfsc_detach(struct hfsc_if *);
148 static int hfsc_class_modify(struct hfsc_class *, struct service_curve *,
149 struct service_curve *, struct service_curve *);
151 static int hfsccmd_if_attach(struct hfsc_attach *);
152 static int hfsccmd_if_detach(struct hfsc_interface *);
153 static int hfsccmd_add_class(struct hfsc_add_class *);
154 static int hfsccmd_delete_class(struct hfsc_delete_class *);
155 static int hfsccmd_modify_class(struct hfsc_modify_class *);
156 static int hfsccmd_add_filter(struct hfsc_add_filter *);
157 static int hfsccmd_delete_filter(struct hfsc_delete_filter *);
158 static int hfsccmd_class_stats(struct hfsc_class_stats *);
161 #endif /* ALTQ3_COMPAT */
166 #define is_a_parent_class(cl) ((cl)->cl_children != NULL)
168 #define HT_INFINITY 0xffffffffffffffffLL /* infinite time value */
171 /* hif_list keeps all hfsc_if's allocated. */
172 static struct hfsc_if *hif_list = NULL;
173 #endif /* ALTQ3_COMPAT */
176 hfsc_pfattach(struct pf_altq *a)
181 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
188 error = altq_attach(&ifp->if_snd, ALTQT_HFSC, a->altq_disc,
189 hfsc_enqueue, hfsc_dequeue, hfsc_request, NULL, NULL);
195 hfsc_add_altq(struct pf_altq *a)
200 if ((ifp = ifunit(a->ifname)) == NULL)
202 if (!ALTQ_IS_READY(&ifp->if_snd))
205 hif = malloc(sizeof(struct hfsc_if), M_DEVBUF, M_WAITOK);
208 bzero(hif, sizeof(struct hfsc_if));
210 hif->hif_eligible = ellist_alloc();
211 if (hif->hif_eligible == NULL) {
216 hif->hif_ifq = &ifp->if_snd;
218 /* keep the state in pf_altq */
225 hfsc_remove_altq(struct pf_altq *a)
229 if ((hif = a->altq_disc) == NULL)
233 (void)hfsc_clear_interface(hif);
234 (void)hfsc_class_destroy(hif->hif_rootclass);
236 ellist_destroy(hif->hif_eligible);
244 hfsc_add_queue(struct pf_altq *a)
247 struct hfsc_class *cl, *parent;
248 struct hfsc_opts *opts;
249 struct service_curve rtsc, lssc, ulsc;
251 if ((hif = a->altq_disc) == NULL)
254 opts = &a->pq_u.hfsc_opts;
256 if (a->parent_qid == HFSC_NULLCLASS_HANDLE &&
257 hif->hif_rootclass == NULL)
259 else if ((parent = clh_to_clp(hif, a->parent_qid)) == NULL)
265 if (clh_to_clp(hif, a->qid) != NULL)
268 rtsc.m1 = opts->rtsc_m1;
269 rtsc.d = opts->rtsc_d;
270 rtsc.m2 = opts->rtsc_m2;
271 lssc.m1 = opts->lssc_m1;
272 lssc.d = opts->lssc_d;
273 lssc.m2 = opts->lssc_m2;
274 ulsc.m1 = opts->ulsc_m1;
275 ulsc.d = opts->ulsc_d;
276 ulsc.m2 = opts->ulsc_m2;
278 cl = hfsc_class_create(hif, &rtsc, &lssc, &ulsc,
279 parent, a->qlimit, opts->flags, a->qid);
287 hfsc_remove_queue(struct pf_altq *a)
290 struct hfsc_class *cl;
292 if ((hif = a->altq_disc) == NULL)
295 if ((cl = clh_to_clp(hif, a->qid)) == NULL)
298 return (hfsc_class_destroy(cl));
302 hfsc_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
305 struct hfsc_class *cl;
306 struct hfsc_classstats stats;
309 if ((hif = altq_lookup(a->ifname, ALTQT_HFSC)) == NULL)
312 if ((cl = clh_to_clp(hif, a->qid)) == NULL)
315 if (*nbytes < sizeof(stats))
318 get_class_stats(&stats, cl);
320 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
322 *nbytes = sizeof(stats);
327 * bring the interface back to the initial state by discarding
328 * all the filters and classes except the root class.
331 hfsc_clear_interface(struct hfsc_if *hif)
333 struct hfsc_class *cl;
336 /* free the filters for this interface */
337 acc_discard_filters(&hif->hif_classifier, NULL, 1);
340 /* clear out the classes */
341 while (hif->hif_rootclass != NULL &&
342 (cl = hif->hif_rootclass->cl_children) != NULL) {
344 * remove the first leaf class found in the hierarchy
347 for (; cl != NULL; cl = hfsc_nextclass(cl)) {
348 if (!is_a_parent_class(cl)) {
349 (void)hfsc_class_destroy(cl);
359 hfsc_request(struct ifaltq *ifq, int req, void *arg)
361 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
363 IFQ_LOCK_ASSERT(ifq);
373 /* discard all the queued packets on the interface */
375 hfsc_purge(struct hfsc_if *hif)
377 struct hfsc_class *cl;
379 for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl))
380 if (!qempty(cl->cl_q))
382 if (ALTQ_IS_ENABLED(hif->hif_ifq))
383 hif->hif_ifq->ifq_len = 0;
387 hfsc_class_create(struct hfsc_if *hif, struct service_curve *rsc,
388 struct service_curve *fsc, struct service_curve *usc,
389 struct hfsc_class *parent, int qlimit, int flags, int qid)
391 struct hfsc_class *cl, *p;
394 if (hif->hif_classes >= HFSC_MAX_CLASSES)
398 if (flags & HFCF_RED) {
400 printf("hfsc_class_create: RED not configured for HFSC!\n");
406 cl = malloc(sizeof(struct hfsc_class), M_DEVBUF, M_WAITOK);
409 bzero(cl, sizeof(struct hfsc_class));
411 cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF, M_WAITOK);
412 if (cl->cl_q == NULL)
414 bzero(cl->cl_q, sizeof(class_queue_t));
416 cl->cl_actc = actlist_alloc();
417 if (cl->cl_actc == NULL)
421 qlimit = 50; /* use default */
422 qlimit(cl->cl_q) = qlimit;
423 qtype(cl->cl_q) = Q_DROPTAIL;
425 cl->cl_flags = flags;
427 if (flags & (HFCF_RED|HFCF_RIO)) {
428 int red_flags, red_pkttime;
432 if (rsc != NULL && rsc->m2 > m2)
434 if (fsc != NULL && fsc->m2 > m2)
436 if (usc != NULL && usc->m2 > m2)
440 if (flags & HFCF_ECN)
441 red_flags |= REDF_ECN;
443 if (flags & HFCF_CLEARDSCP)
444 red_flags |= RIOF_CLEARDSCP;
447 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
449 red_pkttime = (int64_t)hif->hif_ifq->altq_ifp->if_mtu
450 * 1000 * 1000 * 1000 / (m2 / 8);
451 if (flags & HFCF_RED) {
452 cl->cl_red = red_alloc(0, 0,
453 qlimit(cl->cl_q) * 10/100,
454 qlimit(cl->cl_q) * 30/100,
455 red_flags, red_pkttime);
456 if (cl->cl_red != NULL)
457 qtype(cl->cl_q) = Q_RED;
461 cl->cl_red = (red_t *)rio_alloc(0, NULL,
462 red_flags, red_pkttime);
463 if (cl->cl_red != NULL)
464 qtype(cl->cl_q) = Q_RIO;
468 #endif /* ALTQ_RED */
470 if (rsc != NULL && (rsc->m1 != 0 || rsc->m2 != 0)) {
471 cl->cl_rsc = malloc(sizeof(struct internal_sc),
473 if (cl->cl_rsc == NULL)
475 sc2isc(rsc, cl->cl_rsc);
476 rtsc_init(&cl->cl_deadline, cl->cl_rsc, 0, 0);
477 rtsc_init(&cl->cl_eligible, cl->cl_rsc, 0, 0);
479 if (fsc != NULL && (fsc->m1 != 0 || fsc->m2 != 0)) {
480 cl->cl_fsc = malloc(sizeof(struct internal_sc),
482 if (cl->cl_fsc == NULL)
484 sc2isc(fsc, cl->cl_fsc);
485 rtsc_init(&cl->cl_virtual, cl->cl_fsc, 0, 0);
487 if (usc != NULL && (usc->m1 != 0 || usc->m2 != 0)) {
488 cl->cl_usc = malloc(sizeof(struct internal_sc),
490 if (cl->cl_usc == NULL)
492 sc2isc(usc, cl->cl_usc);
493 rtsc_init(&cl->cl_ulimit, cl->cl_usc, 0, 0);
496 cl->cl_id = hif->hif_classid++;
499 cl->cl_parent = parent;
506 IFQ_LOCK(hif->hif_ifq);
510 * find a free slot in the class table. if the slot matching
511 * the lower bits of qid is free, use this slot. otherwise,
512 * use the first free slot.
514 i = qid % HFSC_MAX_CLASSES;
515 if (hif->hif_class_tbl[i] == NULL)
516 hif->hif_class_tbl[i] = cl;
518 for (i = 0; i < HFSC_MAX_CLASSES; i++)
519 if (hif->hif_class_tbl[i] == NULL) {
520 hif->hif_class_tbl[i] = cl;
523 if (i == HFSC_MAX_CLASSES) {
524 IFQ_UNLOCK(hif->hif_ifq);
530 if (flags & HFCF_DEFAULTCLASS)
531 hif->hif_defaultclass = cl;
533 if (parent == NULL) {
534 /* this is root class */
535 hif->hif_rootclass = cl;
537 /* add this class to the children list of the parent */
538 if ((p = parent->cl_children) == NULL)
539 parent->cl_children = cl;
541 while (p->cl_siblings != NULL)
546 IFQ_UNLOCK(hif->hif_ifq);
552 if (cl->cl_actc != NULL)
553 actlist_destroy(cl->cl_actc);
554 if (cl->cl_red != NULL) {
556 if (q_is_rio(cl->cl_q))
557 rio_destroy((rio_t *)cl->cl_red);
560 if (q_is_red(cl->cl_q))
561 red_destroy(cl->cl_red);
564 if (cl->cl_fsc != NULL)
565 free(cl->cl_fsc, M_DEVBUF);
566 if (cl->cl_rsc != NULL)
567 free(cl->cl_rsc, M_DEVBUF);
568 if (cl->cl_usc != NULL)
569 free(cl->cl_usc, M_DEVBUF);
570 if (cl->cl_q != NULL)
571 free(cl->cl_q, M_DEVBUF);
577 hfsc_class_destroy(struct hfsc_class *cl)
584 if (is_a_parent_class(cl))
592 IFQ_LOCK(cl->cl_hif->hif_ifq);
595 /* delete filters referencing to this class */
596 acc_discard_filters(&cl->cl_hif->hif_classifier, cl, 0);
597 #endif /* ALTQ3_COMPAT */
599 if (!qempty(cl->cl_q))
602 if (cl->cl_parent == NULL) {
603 /* this is root class */
605 struct hfsc_class *p = cl->cl_parent->cl_children;
608 cl->cl_parent->cl_children = cl->cl_siblings;
610 if (p->cl_siblings == cl) {
611 p->cl_siblings = cl->cl_siblings;
614 } while ((p = p->cl_siblings) != NULL);
618 for (i = 0; i < HFSC_MAX_CLASSES; i++)
619 if (cl->cl_hif->hif_class_tbl[i] == cl) {
620 cl->cl_hif->hif_class_tbl[i] = NULL;
624 cl->cl_hif->hif_classes--;
625 IFQ_UNLOCK(cl->cl_hif->hif_ifq);
628 actlist_destroy(cl->cl_actc);
630 if (cl->cl_red != NULL) {
632 if (q_is_rio(cl->cl_q))
633 rio_destroy((rio_t *)cl->cl_red);
636 if (q_is_red(cl->cl_q))
637 red_destroy(cl->cl_red);
641 IFQ_LOCK(cl->cl_hif->hif_ifq);
642 if (cl == cl->cl_hif->hif_rootclass)
643 cl->cl_hif->hif_rootclass = NULL;
644 if (cl == cl->cl_hif->hif_defaultclass)
645 cl->cl_hif->hif_defaultclass = NULL;
646 IFQ_UNLOCK(cl->cl_hif->hif_ifq);
648 if (cl->cl_usc != NULL)
649 free(cl->cl_usc, M_DEVBUF);
650 if (cl->cl_fsc != NULL)
651 free(cl->cl_fsc, M_DEVBUF);
652 if (cl->cl_rsc != NULL)
653 free(cl->cl_rsc, M_DEVBUF);
654 free(cl->cl_q, M_DEVBUF);
661 * hfsc_nextclass returns the next class in the tree.
663 * for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl))
666 static struct hfsc_class *
667 hfsc_nextclass(struct hfsc_class *cl)
669 if (cl->cl_children != NULL)
670 cl = cl->cl_children;
671 else if (cl->cl_siblings != NULL)
672 cl = cl->cl_siblings;
674 while ((cl = cl->cl_parent) != NULL)
675 if (cl->cl_siblings) {
676 cl = cl->cl_siblings;
685 * hfsc_enqueue is an enqueue function to be registered to
686 * (*altq_enqueue) in struct ifaltq.
689 hfsc_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
691 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
692 struct hfsc_class *cl;
696 IFQ_LOCK_ASSERT(ifq);
698 /* grab class set by classifier */
699 if ((m->m_flags & M_PKTHDR) == 0) {
700 /* should not happen */
701 #if defined(__NetBSD__) || defined(__OpenBSD__)\
702 || (defined(__FreeBSD__) && __FreeBSD_version >= 501113)
703 printf("altq: packet for %s does not have pkthdr\n",
704 ifq->altq_ifp->if_xname);
706 printf("altq: packet for %s%d does not have pkthdr\n",
707 ifq->altq_ifp->if_name, ifq->altq_ifp->if_unit);
713 if ((t = pf_find_mtag(m)) != NULL)
714 cl = clh_to_clp(hif, t->qid);
716 else if ((ifq->altq_flags & ALTQF_CLASSIFY) && pktattr != NULL)
717 cl = pktattr->pattr_class;
719 if (cl == NULL || is_a_parent_class(cl)) {
720 cl = hif->hif_defaultclass;
728 cl->cl_pktattr = pktattr; /* save proto hdr used by ECN */
731 cl->cl_pktattr = NULL;
733 if (hfsc_addq(cl, m) != 0) {
734 /* drop occurred. mbuf was freed in hfsc_addq. */
735 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, len);
739 cl->cl_hif->hif_packets++;
741 /* successfully queued. */
742 if (qlen(cl->cl_q) == 1)
743 set_active(cl, m_pktlen(m));
749 * hfsc_dequeue is a dequeue function to be registered to
750 * (*altq_dequeue) in struct ifaltq.
752 * note: ALTDQ_POLL returns the next packet without removing the packet
753 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
754 * ALTDQ_REMOVE must return the same packet if called immediately
758 hfsc_dequeue(struct ifaltq *ifq, int op)
760 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
761 struct hfsc_class *cl;
767 IFQ_LOCK_ASSERT(ifq);
769 if (hif->hif_packets == 0)
770 /* no packet in the tree */
773 cur_time = read_machclk();
775 if (op == ALTDQ_REMOVE && hif->hif_pollcache != NULL) {
777 cl = hif->hif_pollcache;
778 hif->hif_pollcache = NULL;
779 /* check if the class was scheduled by real-time criteria */
780 if (cl->cl_rsc != NULL)
781 realtime = (cl->cl_e <= cur_time);
784 * if there are eligible classes, use real-time criteria.
785 * find the class with the minimum deadline among
786 * the eligible classes.
788 if ((cl = ellist_get_mindl(hif->hif_eligible, cur_time))
796 * use link-sharing criteria
797 * get the class with the minimum vt in the hierarchy
799 cl = hif->hif_rootclass;
800 while (is_a_parent_class(cl)) {
802 cl = actlist_firstfit(cl, cur_time);
806 printf("%d fit but none found\n",fits);
811 * update parent's cl_cvtmin.
812 * don't update if the new vt is smaller.
814 if (cl->cl_parent->cl_cvtmin < cl->cl_vt)
815 cl->cl_parent->cl_cvtmin = cl->cl_vt;
822 if (op == ALTDQ_POLL) {
823 hif->hif_pollcache = cl;
831 panic("hfsc_dequeue:");
833 cl->cl_hif->hif_packets--;
835 PKTCNTR_ADD(&cl->cl_stats.xmit_cnt, len);
837 update_vf(cl, len, cur_time);
841 if (!qempty(cl->cl_q)) {
842 if (cl->cl_rsc != NULL) {
844 next_len = m_pktlen(qhead(cl->cl_q));
847 update_ed(cl, next_len);
849 update_d(cl, next_len);
852 /* the class becomes passive */
860 hfsc_addq(struct hfsc_class *cl, struct mbuf *m)
864 if (q_is_rio(cl->cl_q))
865 return rio_addq((rio_t *)cl->cl_red, cl->cl_q,
869 if (q_is_red(cl->cl_q))
870 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
872 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
877 if (cl->cl_flags & HFCF_CLEARDSCP)
878 write_dsfield(m, cl->cl_pktattr, 0);
886 hfsc_getq(struct hfsc_class *cl)
889 if (q_is_rio(cl->cl_q))
890 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
893 if (q_is_red(cl->cl_q))
894 return red_getq(cl->cl_red, cl->cl_q);
896 return _getq(cl->cl_q);
900 hfsc_pollq(struct hfsc_class *cl)
902 return qhead(cl->cl_q);
906 hfsc_purgeq(struct hfsc_class *cl)
910 if (qempty(cl->cl_q))
913 while ((m = _getq(cl->cl_q)) != NULL) {
914 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, m_pktlen(m));
916 cl->cl_hif->hif_packets--;
917 IFQ_DEC_LEN(cl->cl_hif->hif_ifq);
919 ASSERT(qlen(cl->cl_q) == 0);
921 update_vf(cl, 0, 0); /* remove cl from the actlist */
926 set_active(struct hfsc_class *cl, int len)
928 if (cl->cl_rsc != NULL)
930 if (cl->cl_fsc != NULL)
933 cl->cl_stats.period++;
937 set_passive(struct hfsc_class *cl)
939 if (cl->cl_rsc != NULL)
943 * actlist is now handled in update_vf() so that update_vf(cl, 0, 0)
944 * needs to be called explicitly to remove a class from actlist
949 init_ed(struct hfsc_class *cl, int next_len)
953 cur_time = read_machclk();
955 /* update the deadline curve */
956 rtsc_min(&cl->cl_deadline, cl->cl_rsc, cur_time, cl->cl_cumul);
959 * update the eligible curve.
960 * for concave, it is equal to the deadline curve.
961 * for convex, it is a linear curve with slope m2.
963 cl->cl_eligible = cl->cl_deadline;
964 if (cl->cl_rsc->sm1 <= cl->cl_rsc->sm2) {
965 cl->cl_eligible.dx = 0;
966 cl->cl_eligible.dy = 0;
969 /* compute e and d */
970 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
971 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
977 update_ed(struct hfsc_class *cl, int next_len)
979 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
980 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
986 update_d(struct hfsc_class *cl, int next_len)
988 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
992 init_vf(struct hfsc_class *cl, int len)
994 struct hfsc_class *max_cl, *p;
995 u_int64_t vt, f, cur_time;
1000 for ( ; cl->cl_parent != NULL; cl = cl->cl_parent) {
1002 if (go_active && cl->cl_nactive++ == 0)
1008 max_cl = actlist_last(cl->cl_parent->cl_actc);
1009 if (max_cl != NULL) {
1011 * set vt to the average of the min and max
1012 * classes. if the parent's period didn't
1013 * change, don't decrease vt of the class.
1016 if (cl->cl_parent->cl_cvtmin != 0)
1017 vt = (cl->cl_parent->cl_cvtmin + vt)/2;
1019 if (cl->cl_parent->cl_vtperiod !=
1020 cl->cl_parentperiod || vt > cl->cl_vt)
1024 * first child for a new parent backlog period.
1025 * add parent's cvtmax to vtoff of children
1026 * to make a new vt (vtoff + vt) larger than
1027 * the vt in the last period for all children.
1029 vt = cl->cl_parent->cl_cvtmax;
1030 for (p = cl->cl_parent->cl_children; p != NULL;
1034 cl->cl_parent->cl_cvtmax = 0;
1035 cl->cl_parent->cl_cvtmin = 0;
1037 cl->cl_initvt = cl->cl_vt;
1039 /* update the virtual curve */
1040 vt = cl->cl_vt + cl->cl_vtoff;
1041 rtsc_min(&cl->cl_virtual, cl->cl_fsc, vt, cl->cl_total);
1042 if (cl->cl_virtual.x == vt) {
1043 cl->cl_virtual.x -= cl->cl_vtoff;
1048 cl->cl_vtperiod++; /* increment vt period */
1049 cl->cl_parentperiod = cl->cl_parent->cl_vtperiod;
1050 if (cl->cl_parent->cl_nactive == 0)
1051 cl->cl_parentperiod++;
1056 if (cl->cl_usc != NULL) {
1057 /* class has upper limit curve */
1059 cur_time = read_machclk();
1061 /* update the ulimit curve */
1062 rtsc_min(&cl->cl_ulimit, cl->cl_usc, cur_time,
1065 cl->cl_myf = rtsc_y2x(&cl->cl_ulimit,
1071 if (cl->cl_myf > cl->cl_cfmin)
1075 if (f != cl->cl_f) {
1077 update_cfmin(cl->cl_parent);
1083 update_vf(struct hfsc_class *cl, int len, u_int64_t cur_time)
1085 u_int64_t f, myf_bound, delta;
1088 go_passive = qempty(cl->cl_q);
1090 for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
1092 cl->cl_total += len;
1094 if (cl->cl_fsc == NULL || cl->cl_nactive == 0)
1097 if (go_passive && --cl->cl_nactive == 0)
1103 /* no more active child, going passive */
1105 /* update cvtmax of the parent class */
1106 if (cl->cl_vt > cl->cl_parent->cl_cvtmax)
1107 cl->cl_parent->cl_cvtmax = cl->cl_vt;
1109 /* remove this class from the vt list */
1112 update_cfmin(cl->cl_parent);
1120 cl->cl_vt = rtsc_y2x(&cl->cl_virtual, cl->cl_total)
1121 - cl->cl_vtoff + cl->cl_vtadj;
1124 * if vt of the class is smaller than cvtmin,
1125 * the class was skipped in the past due to non-fit.
1126 * if so, we need to adjust vtadj.
1128 if (cl->cl_vt < cl->cl_parent->cl_cvtmin) {
1129 cl->cl_vtadj += cl->cl_parent->cl_cvtmin - cl->cl_vt;
1130 cl->cl_vt = cl->cl_parent->cl_cvtmin;
1133 /* update the vt list */
1136 if (cl->cl_usc != NULL) {
1137 cl->cl_myf = cl->cl_myfadj
1138 + rtsc_y2x(&cl->cl_ulimit, cl->cl_total);
1141 * if myf lags behind by more than one clock tick
1142 * from the current time, adjust myfadj to prevent
1143 * a rate-limited class from going greedy.
1144 * in a steady state under rate-limiting, myf
1145 * fluctuates within one clock tick.
1147 myf_bound = cur_time - machclk_per_tick;
1148 if (cl->cl_myf < myf_bound) {
1149 delta = cur_time - cl->cl_myf;
1150 cl->cl_myfadj += delta;
1151 cl->cl_myf += delta;
1155 /* cl_f is max(cl_myf, cl_cfmin) */
1156 if (cl->cl_myf > cl->cl_cfmin)
1160 if (f != cl->cl_f) {
1162 update_cfmin(cl->cl_parent);
1168 update_cfmin(struct hfsc_class *cl)
1170 struct hfsc_class *p;
1173 if (TAILQ_EMPTY(cl->cl_actc)) {
1177 cfmin = HT_INFINITY;
1178 TAILQ_FOREACH(p, cl->cl_actc, cl_actlist) {
1183 if (p->cl_f < cfmin)
1186 cl->cl_cfmin = cfmin;
1190 * TAILQ based ellist and actlist implementation
1191 * (ion wanted to make a calendar queue based implementation)
1194 * eligible list holds backlogged classes being sorted by their eligible times.
1195 * there is one eligible list per interface.
1203 head = malloc(sizeof(ellist_t), M_DEVBUF, M_WAITOK);
1209 ellist_destroy(ellist_t *head)
1211 free(head, M_DEVBUF);
1215 ellist_insert(struct hfsc_class *cl)
1217 struct hfsc_if *hif = cl->cl_hif;
1218 struct hfsc_class *p;
1220 /* check the last entry first */
1221 if ((p = TAILQ_LAST(hif->hif_eligible, _eligible)) == NULL ||
1222 p->cl_e <= cl->cl_e) {
1223 TAILQ_INSERT_TAIL(hif->hif_eligible, cl, cl_ellist);
1227 TAILQ_FOREACH(p, hif->hif_eligible, cl_ellist) {
1228 if (cl->cl_e < p->cl_e) {
1229 TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1233 ASSERT(0); /* should not reach here */
1237 ellist_remove(struct hfsc_class *cl)
1239 struct hfsc_if *hif = cl->cl_hif;
1241 TAILQ_REMOVE(hif->hif_eligible, cl, cl_ellist);
1245 ellist_update(struct hfsc_class *cl)
1247 struct hfsc_if *hif = cl->cl_hif;
1248 struct hfsc_class *p, *last;
1251 * the eligible time of a class increases monotonically.
1252 * if the next entry has a larger eligible time, nothing to do.
1254 p = TAILQ_NEXT(cl, cl_ellist);
1255 if (p == NULL || cl->cl_e <= p->cl_e)
1258 /* check the last entry */
1259 last = TAILQ_LAST(hif->hif_eligible, _eligible);
1260 ASSERT(last != NULL);
1261 if (last->cl_e <= cl->cl_e) {
1262 TAILQ_REMOVE(hif->hif_eligible, cl, cl_ellist);
1263 TAILQ_INSERT_TAIL(hif->hif_eligible, cl, cl_ellist);
1268 * the new position must be between the next entry
1269 * and the last entry
1271 while ((p = TAILQ_NEXT(p, cl_ellist)) != NULL) {
1272 if (cl->cl_e < p->cl_e) {
1273 TAILQ_REMOVE(hif->hif_eligible, cl, cl_ellist);
1274 TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1278 ASSERT(0); /* should not reach here */
1281 /* find the class with the minimum deadline among the eligible classes */
1283 ellist_get_mindl(ellist_t *head, u_int64_t cur_time)
1285 struct hfsc_class *p, *cl = NULL;
1287 TAILQ_FOREACH(p, head, cl_ellist) {
1288 if (p->cl_e > cur_time)
1290 if (cl == NULL || p->cl_d < cl->cl_d)
1297 * active children list holds backlogged child classes being sorted
1298 * by their virtual time.
1299 * each intermediate class has one active children list.
1306 head = malloc(sizeof(actlist_t), M_DEVBUF, M_WAITOK);
1312 actlist_destroy(actlist_t *head)
1314 free(head, M_DEVBUF);
1317 actlist_insert(struct hfsc_class *cl)
1319 struct hfsc_class *p;
1321 /* check the last entry first */
1322 if ((p = TAILQ_LAST(cl->cl_parent->cl_actc, _active)) == NULL
1323 || p->cl_vt <= cl->cl_vt) {
1324 TAILQ_INSERT_TAIL(cl->cl_parent->cl_actc, cl, cl_actlist);
1328 TAILQ_FOREACH(p, cl->cl_parent->cl_actc, cl_actlist) {
1329 if (cl->cl_vt < p->cl_vt) {
1330 TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1334 ASSERT(0); /* should not reach here */
1338 actlist_remove(struct hfsc_class *cl)
1340 TAILQ_REMOVE(cl->cl_parent->cl_actc, cl, cl_actlist);
1344 actlist_update(struct hfsc_class *cl)
1346 struct hfsc_class *p, *last;
1349 * the virtual time of a class increases monotonically during its
1350 * backlogged period.
1351 * if the next entry has a larger virtual time, nothing to do.
1353 p = TAILQ_NEXT(cl, cl_actlist);
1354 if (p == NULL || cl->cl_vt < p->cl_vt)
1357 /* check the last entry */
1358 last = TAILQ_LAST(cl->cl_parent->cl_actc, _active);
1359 ASSERT(last != NULL);
1360 if (last->cl_vt <= cl->cl_vt) {
1361 TAILQ_REMOVE(cl->cl_parent->cl_actc, cl, cl_actlist);
1362 TAILQ_INSERT_TAIL(cl->cl_parent->cl_actc, cl, cl_actlist);
1367 * the new position must be between the next entry
1368 * and the last entry
1370 while ((p = TAILQ_NEXT(p, cl_actlist)) != NULL) {
1371 if (cl->cl_vt < p->cl_vt) {
1372 TAILQ_REMOVE(cl->cl_parent->cl_actc, cl, cl_actlist);
1373 TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1377 ASSERT(0); /* should not reach here */
1380 static struct hfsc_class *
1381 actlist_firstfit(struct hfsc_class *cl, u_int64_t cur_time)
1383 struct hfsc_class *p;
1385 TAILQ_FOREACH(p, cl->cl_actc, cl_actlist) {
1386 if (p->cl_f <= cur_time)
1393 * service curve support functions
1395 * external service curve parameters
1398 * internal service curve parameters
1399 * sm: (bytes/tsc_interval) << SM_SHIFT
1400 * ism: (tsc_count/byte) << ISM_SHIFT
1403 * SM_SHIFT and ISM_SHIFT are scaled in order to keep effective digits.
1404 * we should be able to handle 100K-1Gbps linkspeed with 200Hz-1GHz CPU
1405 * speed. SM_SHIFT and ISM_SHIFT are selected to have at least 3 effective
1406 * digits in decimal using the following table.
1408 * bits/sec 100Kbps 1Mbps 10Mbps 100Mbps 1Gbps
1409 * ----------+-------------------------------------------------------
1410 * bytes/nsec 12.5e-6 125e-6 1250e-6 12500e-6 125000e-6
1411 * sm(500MHz) 25.0e-6 250e-6 2500e-6 25000e-6 250000e-6
1412 * sm(200MHz) 62.5e-6 625e-6 6250e-6 62500e-6 625000e-6
1414 * nsec/byte 80000 8000 800 80 8
1415 * ism(500MHz) 40000 4000 400 40 4
1416 * ism(200MHz) 16000 1600 160 16 1.6
1419 #define ISM_SHIFT 10
1421 #define SM_MASK ((1LL << SM_SHIFT) - 1)
1422 #define ISM_MASK ((1LL << ISM_SHIFT) - 1)
1424 static __inline u_int64_t
1425 seg_x2y(u_int64_t x, u_int64_t sm)
1431 * y = x * sm >> SM_SHIFT
1432 * but divide it for the upper and lower bits to avoid overflow
1434 y = (x >> SM_SHIFT) * sm + (((x & SM_MASK) * sm) >> SM_SHIFT);
1438 static __inline u_int64_t
1439 seg_y2x(u_int64_t y, u_int64_t ism)
1445 else if (ism == HT_INFINITY)
1448 x = (y >> ISM_SHIFT) * ism
1449 + (((y & ISM_MASK) * ism) >> ISM_SHIFT);
1454 static __inline u_int64_t
1459 sm = ((u_int64_t)m << SM_SHIFT) / 8 / machclk_freq;
1463 static __inline u_int64_t
1471 ism = ((u_int64_t)machclk_freq << ISM_SHIFT) * 8 / m;
1475 static __inline u_int64_t
1480 dx = ((u_int64_t)d * machclk_freq) / 1000;
1489 m = (sm * 8 * machclk_freq) >> SM_SHIFT;
1498 d = dx * 1000 / machclk_freq;
1503 sc2isc(struct service_curve *sc, struct internal_sc *isc)
1505 isc->sm1 = m2sm(sc->m1);
1506 isc->ism1 = m2ism(sc->m1);
1507 isc->dx = d2dx(sc->d);
1508 isc->dy = seg_x2y(isc->dx, isc->sm1);
1509 isc->sm2 = m2sm(sc->m2);
1510 isc->ism2 = m2ism(sc->m2);
1514 * initialize the runtime service curve with the given internal
1515 * service curve starting at (x, y).
1518 rtsc_init(struct runtime_sc *rtsc, struct internal_sc * isc, u_int64_t x,
1523 rtsc->sm1 = isc->sm1;
1524 rtsc->ism1 = isc->ism1;
1527 rtsc->sm2 = isc->sm2;
1528 rtsc->ism2 = isc->ism2;
1532 * calculate the y-projection of the runtime service curve by the
1533 * given x-projection value
1536 rtsc_y2x(struct runtime_sc *rtsc, u_int64_t y)
1542 else if (y <= rtsc->y + rtsc->dy) {
1543 /* x belongs to the 1st segment */
1545 x = rtsc->x + rtsc->dx;
1547 x = rtsc->x + seg_y2x(y - rtsc->y, rtsc->ism1);
1549 /* x belongs to the 2nd segment */
1550 x = rtsc->x + rtsc->dx
1551 + seg_y2x(y - rtsc->y - rtsc->dy, rtsc->ism2);
1557 rtsc_x2y(struct runtime_sc *rtsc, u_int64_t x)
1563 else if (x <= rtsc->x + rtsc->dx)
1564 /* y belongs to the 1st segment */
1565 y = rtsc->y + seg_x2y(x - rtsc->x, rtsc->sm1);
1567 /* y belongs to the 2nd segment */
1568 y = rtsc->y + rtsc->dy
1569 + seg_x2y(x - rtsc->x - rtsc->dx, rtsc->sm2);
1574 * update the runtime service curve by taking the minimum of the current
1575 * runtime service curve and the service curve starting at (x, y).
1578 rtsc_min(struct runtime_sc *rtsc, struct internal_sc *isc, u_int64_t x,
1581 u_int64_t y1, y2, dx, dy;
1583 if (isc->sm1 <= isc->sm2) {
1584 /* service curve is convex */
1585 y1 = rtsc_x2y(rtsc, x);
1587 /* the current rtsc is smaller */
1595 * service curve is concave
1596 * compute the two y values of the current rtsc
1600 y1 = rtsc_x2y(rtsc, x);
1602 /* rtsc is below isc, no change to rtsc */
1606 y2 = rtsc_x2y(rtsc, x + isc->dx);
1607 if (y2 >= y + isc->dy) {
1608 /* rtsc is above isc, replace rtsc by isc */
1617 * the two curves intersect
1618 * compute the offsets (dx, dy) using the reverse
1619 * function of seg_x2y()
1620 * seg_x2y(dx, sm1) == seg_x2y(dx, sm2) + (y1 - y)
1622 dx = ((y1 - y) << SM_SHIFT) / (isc->sm1 - isc->sm2);
1624 * check if (x, y1) belongs to the 1st segment of rtsc.
1625 * if so, add the offset.
1627 if (rtsc->x + rtsc->dx > x)
1628 dx += rtsc->x + rtsc->dx - x;
1629 dy = seg_x2y(dx, isc->sm1);
1639 get_class_stats(struct hfsc_classstats *sp, struct hfsc_class *cl)
1641 sp->class_id = cl->cl_id;
1642 sp->class_handle = cl->cl_handle;
1644 if (cl->cl_rsc != NULL) {
1645 sp->rsc.m1 = sm2m(cl->cl_rsc->sm1);
1646 sp->rsc.d = dx2d(cl->cl_rsc->dx);
1647 sp->rsc.m2 = sm2m(cl->cl_rsc->sm2);
1653 if (cl->cl_fsc != NULL) {
1654 sp->fsc.m1 = sm2m(cl->cl_fsc->sm1);
1655 sp->fsc.d = dx2d(cl->cl_fsc->dx);
1656 sp->fsc.m2 = sm2m(cl->cl_fsc->sm2);
1662 if (cl->cl_usc != NULL) {
1663 sp->usc.m1 = sm2m(cl->cl_usc->sm1);
1664 sp->usc.d = dx2d(cl->cl_usc->dx);
1665 sp->usc.m2 = sm2m(cl->cl_usc->sm2);
1672 sp->total = cl->cl_total;
1673 sp->cumul = cl->cl_cumul;
1680 sp->initvt = cl->cl_initvt;
1681 sp->vtperiod = cl->cl_vtperiod;
1682 sp->parentperiod = cl->cl_parentperiod;
1683 sp->nactive = cl->cl_nactive;
1684 sp->vtoff = cl->cl_vtoff;
1685 sp->cvtmax = cl->cl_cvtmax;
1686 sp->myf = cl->cl_myf;
1687 sp->cfmin = cl->cl_cfmin;
1688 sp->cvtmin = cl->cl_cvtmin;
1689 sp->myfadj = cl->cl_myfadj;
1690 sp->vtadj = cl->cl_vtadj;
1692 sp->cur_time = read_machclk();
1693 sp->machclk_freq = machclk_freq;
1695 sp->qlength = qlen(cl->cl_q);
1696 sp->qlimit = qlimit(cl->cl_q);
1697 sp->xmit_cnt = cl->cl_stats.xmit_cnt;
1698 sp->drop_cnt = cl->cl_stats.drop_cnt;
1699 sp->period = cl->cl_stats.period;
1701 sp->qtype = qtype(cl->cl_q);
1703 if (q_is_red(cl->cl_q))
1704 red_getstats(cl->cl_red, &sp->red[0]);
1707 if (q_is_rio(cl->cl_q))
1708 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
1712 /* convert a class handle to the corresponding class pointer */
1713 static struct hfsc_class *
1714 clh_to_clp(struct hfsc_if *hif, u_int32_t chandle)
1717 struct hfsc_class *cl;
1722 * first, try optimistically the slot matching the lower bits of
1723 * the handle. if it fails, do the linear table search.
1725 i = chandle % HFSC_MAX_CLASSES;
1726 if ((cl = hif->hif_class_tbl[i]) != NULL && cl->cl_handle == chandle)
1728 for (i = 0; i < HFSC_MAX_CLASSES; i++)
1729 if ((cl = hif->hif_class_tbl[i]) != NULL &&
1730 cl->cl_handle == chandle)
1736 static struct hfsc_if *
1737 hfsc_attach(ifq, bandwidth)
1741 struct hfsc_if *hif;
1743 hif = malloc(sizeof(struct hfsc_if), M_DEVBUF, M_WAITOK);
1746 bzero(hif, sizeof(struct hfsc_if));
1748 hif->hif_eligible = ellist_alloc();
1749 if (hif->hif_eligible == NULL) {
1750 free(hif, M_DEVBUF);
1756 /* add this state to the hfsc list */
1757 hif->hif_next = hif_list;
1765 struct hfsc_if *hif;
1767 (void)hfsc_clear_interface(hif);
1768 (void)hfsc_class_destroy(hif->hif_rootclass);
1770 /* remove this interface from the hif list */
1771 if (hif_list == hif)
1772 hif_list = hif->hif_next;
1776 for (h = hif_list; h != NULL; h = h->hif_next)
1777 if (h->hif_next == hif) {
1778 h->hif_next = hif->hif_next;
1784 ellist_destroy(hif->hif_eligible);
1786 free(hif, M_DEVBUF);
1792 hfsc_class_modify(cl, rsc, fsc, usc)
1793 struct hfsc_class *cl;
1794 struct service_curve *rsc, *fsc, *usc;
1796 struct internal_sc *rsc_tmp, *fsc_tmp, *usc_tmp;
1800 rsc_tmp = fsc_tmp = usc_tmp = NULL;
1801 if (rsc != NULL && (rsc->m1 != 0 || rsc->m2 != 0) &&
1802 cl->cl_rsc == NULL) {
1803 rsc_tmp = malloc(sizeof(struct internal_sc),
1804 M_DEVBUF, M_WAITOK);
1805 if (rsc_tmp == NULL)
1808 if (fsc != NULL && (fsc->m1 != 0 || fsc->m2 != 0) &&
1809 cl->cl_fsc == NULL) {
1810 fsc_tmp = malloc(sizeof(struct internal_sc),
1811 M_DEVBUF, M_WAITOK);
1812 if (fsc_tmp == NULL) {
1817 if (usc != NULL && (usc->m1 != 0 || usc->m2 != 0) &&
1818 cl->cl_usc == NULL) {
1819 usc_tmp = malloc(sizeof(struct internal_sc),
1820 M_DEVBUF, M_WAITOK);
1821 if (usc_tmp == NULL) {
1828 cur_time = read_machclk();
1834 IFQ_LOCK(cl->cl_hif->hif_ifq);
1837 if (rsc->m1 == 0 && rsc->m2 == 0) {
1838 if (cl->cl_rsc != NULL) {
1839 if (!qempty(cl->cl_q))
1841 free(cl->cl_rsc, M_DEVBUF);
1845 if (cl->cl_rsc == NULL)
1846 cl->cl_rsc = rsc_tmp;
1847 sc2isc(rsc, cl->cl_rsc);
1848 rtsc_init(&cl->cl_deadline, cl->cl_rsc, cur_time,
1850 cl->cl_eligible = cl->cl_deadline;
1851 if (cl->cl_rsc->sm1 <= cl->cl_rsc->sm2) {
1852 cl->cl_eligible.dx = 0;
1853 cl->cl_eligible.dy = 0;
1859 if (fsc->m1 == 0 && fsc->m2 == 0) {
1860 if (cl->cl_fsc != NULL) {
1861 if (!qempty(cl->cl_q))
1863 free(cl->cl_fsc, M_DEVBUF);
1867 if (cl->cl_fsc == NULL)
1868 cl->cl_fsc = fsc_tmp;
1869 sc2isc(fsc, cl->cl_fsc);
1870 rtsc_init(&cl->cl_virtual, cl->cl_fsc, cl->cl_vt,
1876 if (usc->m1 == 0 && usc->m2 == 0) {
1877 if (cl->cl_usc != NULL) {
1878 free(cl->cl_usc, M_DEVBUF);
1883 if (cl->cl_usc == NULL)
1884 cl->cl_usc = usc_tmp;
1885 sc2isc(usc, cl->cl_usc);
1886 rtsc_init(&cl->cl_ulimit, cl->cl_usc, cur_time,
1891 if (!qempty(cl->cl_q)) {
1892 if (cl->cl_rsc != NULL)
1893 update_ed(cl, m_pktlen(qhead(cl->cl_q)));
1894 if (cl->cl_fsc != NULL)
1895 update_vf(cl, 0, cur_time);
1896 /* is this enough? */
1899 IFQ_UNLOCK(cl->cl_hif->hif_ifq);
1906 * hfsc device interface
1909 hfscopen(dev, flag, fmt, p)
1912 #if (__FreeBSD_version > 500000)
1918 if (machclk_freq == 0)
1921 if (machclk_freq == 0) {
1922 printf("hfsc: no cpu clock available!\n");
1926 /* everything will be done when the queueing scheme is attached. */
1931 hfscclose(dev, flag, fmt, p)
1934 #if (__FreeBSD_version > 500000)
1940 struct hfsc_if *hif;
1943 while ((hif = hif_list) != NULL) {
1945 if (ALTQ_IS_ENABLED(hif->hif_ifq))
1946 altq_disable(hif->hif_ifq);
1948 err = altq_detach(hif->hif_ifq);
1950 err = hfsc_detach(hif);
1951 if (err != 0 && error == 0)
1959 hfscioctl(dev, cmd, addr, flag, p)
1964 #if (__FreeBSD_version > 500000)
1970 struct hfsc_if *hif;
1971 struct hfsc_interface *ifacep;
1974 /* check super-user privilege */
1979 #if (__FreeBSD_version > 700000)
1980 if ((error = priv_check(p, PRIV_ALTQ_MANAGE)) != 0)
1982 #elsif (__FreeBSD_version > 400000)
1983 if ((error = suser(p)) != 0)
1986 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1994 case HFSC_IF_ATTACH:
1995 error = hfsccmd_if_attach((struct hfsc_attach *)addr);
1998 case HFSC_IF_DETACH:
1999 error = hfsccmd_if_detach((struct hfsc_interface *)addr);
2004 case HFSC_CLEAR_HIERARCHY:
2005 ifacep = (struct hfsc_interface *)addr;
2006 if ((hif = altq_lookup(ifacep->hfsc_ifname,
2007 ALTQT_HFSC)) == NULL) {
2015 if (hif->hif_defaultclass == NULL) {
2017 printf("hfsc: no default class\n");
2022 error = altq_enable(hif->hif_ifq);
2026 error = altq_disable(hif->hif_ifq);
2029 case HFSC_CLEAR_HIERARCHY:
2030 hfsc_clear_interface(hif);
2035 case HFSC_ADD_CLASS:
2036 error = hfsccmd_add_class((struct hfsc_add_class *)addr);
2039 case HFSC_DEL_CLASS:
2040 error = hfsccmd_delete_class((struct hfsc_delete_class *)addr);
2043 case HFSC_MOD_CLASS:
2044 error = hfsccmd_modify_class((struct hfsc_modify_class *)addr);
2047 case HFSC_ADD_FILTER:
2048 error = hfsccmd_add_filter((struct hfsc_add_filter *)addr);
2051 case HFSC_DEL_FILTER:
2052 error = hfsccmd_delete_filter((struct hfsc_delete_filter *)addr);
2056 error = hfsccmd_class_stats((struct hfsc_class_stats *)addr);
2067 hfsccmd_if_attach(ap)
2068 struct hfsc_attach *ap;
2070 struct hfsc_if *hif;
2074 if ((ifp = ifunit(ap->iface.hfsc_ifname)) == NULL)
2077 if ((hif = hfsc_attach(&ifp->if_snd, ap->bandwidth)) == NULL)
2081 * set HFSC to this ifnet structure.
2083 if ((error = altq_attach(&ifp->if_snd, ALTQT_HFSC, hif,
2084 hfsc_enqueue, hfsc_dequeue, hfsc_request,
2085 &hif->hif_classifier, acc_classify)) != 0)
2086 (void)hfsc_detach(hif);
2092 hfsccmd_if_detach(ap)
2093 struct hfsc_interface *ap;
2095 struct hfsc_if *hif;
2098 if ((hif = altq_lookup(ap->hfsc_ifname, ALTQT_HFSC)) == NULL)
2101 if (ALTQ_IS_ENABLED(hif->hif_ifq))
2102 altq_disable(hif->hif_ifq);
2104 if ((error = altq_detach(hif->hif_ifq)))
2107 return hfsc_detach(hif);
2111 hfsccmd_add_class(ap)
2112 struct hfsc_add_class *ap;
2114 struct hfsc_if *hif;
2115 struct hfsc_class *cl, *parent;
2118 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2121 if (ap->parent_handle == HFSC_NULLCLASS_HANDLE &&
2122 hif->hif_rootclass == NULL)
2124 else if ((parent = clh_to_clp(hif, ap->parent_handle)) == NULL)
2127 /* assign a class handle (use a free slot number for now) */
2128 for (i = 1; i < HFSC_MAX_CLASSES; i++)
2129 if (hif->hif_class_tbl[i] == NULL)
2131 if (i == HFSC_MAX_CLASSES)
2134 if ((cl = hfsc_class_create(hif, &ap->service_curve, NULL, NULL,
2135 parent, ap->qlimit, ap->flags, i)) == NULL)
2138 /* return a class handle to the user */
2139 ap->class_handle = i;
2145 hfsccmd_delete_class(ap)
2146 struct hfsc_delete_class *ap;
2148 struct hfsc_if *hif;
2149 struct hfsc_class *cl;
2151 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2154 if ((cl = clh_to_clp(hif, ap->class_handle)) == NULL)
2157 return hfsc_class_destroy(cl);
2161 hfsccmd_modify_class(ap)
2162 struct hfsc_modify_class *ap;
2164 struct hfsc_if *hif;
2165 struct hfsc_class *cl;
2166 struct service_curve *rsc = NULL;
2167 struct service_curve *fsc = NULL;
2168 struct service_curve *usc = NULL;
2170 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2173 if ((cl = clh_to_clp(hif, ap->class_handle)) == NULL)
2176 if (ap->sctype & HFSC_REALTIMESC)
2177 rsc = &ap->service_curve;
2178 if (ap->sctype & HFSC_LINKSHARINGSC)
2179 fsc = &ap->service_curve;
2180 if (ap->sctype & HFSC_UPPERLIMITSC)
2181 usc = &ap->service_curve;
2183 return hfsc_class_modify(cl, rsc, fsc, usc);
2187 hfsccmd_add_filter(ap)
2188 struct hfsc_add_filter *ap;
2190 struct hfsc_if *hif;
2191 struct hfsc_class *cl;
2193 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2196 if ((cl = clh_to_clp(hif, ap->class_handle)) == NULL)
2199 if (is_a_parent_class(cl)) {
2201 printf("hfsccmd_add_filter: not a leaf class!\n");
2206 return acc_add_filter(&hif->hif_classifier, &ap->filter,
2207 cl, &ap->filter_handle);
2211 hfsccmd_delete_filter(ap)
2212 struct hfsc_delete_filter *ap;
2214 struct hfsc_if *hif;
2216 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2219 return acc_delete_filter(&hif->hif_classifier,
2224 hfsccmd_class_stats(ap)
2225 struct hfsc_class_stats *ap;
2227 struct hfsc_if *hif;
2228 struct hfsc_class *cl;
2229 struct hfsc_classstats stats, *usp;
2230 int n, nclasses, error;
2232 if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2235 ap->cur_time = read_machclk();
2236 ap->machclk_freq = machclk_freq;
2237 ap->hif_classes = hif->hif_classes;
2238 ap->hif_packets = hif->hif_packets;
2240 /* skip the first N classes in the tree */
2241 nclasses = ap->nskip;
2242 for (cl = hif->hif_rootclass, n = 0; cl != NULL && n < nclasses;
2243 cl = hfsc_nextclass(cl), n++)
2248 /* then, read the next N classes in the tree */
2249 nclasses = ap->nclasses;
2251 for (n = 0; cl != NULL && n < nclasses; cl = hfsc_nextclass(cl), n++) {
2253 get_class_stats(&stats, cl);
2255 if ((error = copyout((caddr_t)&stats, (caddr_t)usp++,
2256 sizeof(stats))) != 0)
2267 static struct altqsw hfsc_sw =
2268 {"hfsc", hfscopen, hfscclose, hfscioctl};
2270 ALTQ_MODULE(altq_hfsc, ALTQT_HFSC, &hfsc_sw);
2271 MODULE_DEPEND(altq_hfsc, altq_red, 1, 1, 1);
2272 MODULE_DEPEND(altq_hfsc, altq_rio, 1, 1, 1);
2274 #endif /* KLD_MODULE */
2275 #endif /* ALTQ3_COMPAT */
2277 #endif /* ALTQ_HFSC */