linprocfs - Introduce /proc/mounts
[dragonfly.git] / sys / net / altq / altq_hfsc.c
blob45c7b4869f04dcbc2223476b2e23db31aa4e813f
1 /* $KAME: altq_hfsc.c,v 1.25 2004/04/17 10:54:48 kjc Exp $ */
2 /* $DragonFly: src/sys/net/altq/altq_hfsc.c,v 1.9 2008/05/14 11:59:23 sephe Exp $ */
4 /*
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
26 * DAMAGE.
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 #include "opt_altq.h"
46 #include "opt_inet.h"
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>
53 #include <sys/mbuf.h>
54 #include <sys/socket.h>
55 #include <sys/systm.h>
56 #include <sys/errno.h>
57 #include <sys/queue.h>
58 #include <sys/thread.h>
60 #include <net/if.h>
61 #include <net/ifq_var.h>
62 #include <netinet/in.h>
64 #include <net/pf/pfvar.h>
65 #include <net/altq/altq.h>
66 #include <net/altq/altq_hfsc.h>
68 #include <sys/thread2.h>
71 * function prototypes
73 static int hfsc_clear_interface(struct hfsc_if *);
74 static int hfsc_request(struct ifaltq *, int, void *);
75 static void hfsc_purge(struct hfsc_if *);
76 static struct hfsc_class *hfsc_class_create(struct hfsc_if *,
77 struct service_curve *,
78 struct service_curve *,
79 struct service_curve *,
80 struct hfsc_class *, int, int, int);
81 static int hfsc_class_destroy(struct hfsc_class *);
82 static struct hfsc_class *hfsc_nextclass(struct hfsc_class *);
83 static int hfsc_enqueue(struct ifaltq *, struct mbuf *,
84 struct altq_pktattr *);
85 static struct mbuf *hfsc_dequeue(struct ifaltq *, struct mbuf *, int);
87 static int hfsc_addq(struct hfsc_class *, struct mbuf *);
88 static struct mbuf *hfsc_getq(struct hfsc_class *);
89 static struct mbuf *hfsc_pollq(struct hfsc_class *);
90 static void hfsc_purgeq(struct hfsc_class *);
92 static void update_cfmin(struct hfsc_class *);
93 static void set_active(struct hfsc_class *, int);
94 static void set_passive(struct hfsc_class *);
96 static void init_ed(struct hfsc_class *, int);
97 static void update_ed(struct hfsc_class *, int);
98 static void update_d(struct hfsc_class *, int);
99 static void init_vf(struct hfsc_class *, int);
100 static void update_vf(struct hfsc_class *, int, uint64_t);
101 static ellist_t *ellist_alloc(void);
102 static void ellist_destroy(ellist_t *);
103 static void ellist_insert(struct hfsc_class *);
104 static void ellist_remove(struct hfsc_class *);
105 static void ellist_update(struct hfsc_class *);
106 struct hfsc_class *ellist_get_mindl(ellist_t *, uint64_t);
107 static actlist_t *actlist_alloc(void);
108 static void actlist_destroy(actlist_t *);
109 static void actlist_insert(struct hfsc_class *);
110 static void actlist_remove(struct hfsc_class *);
111 static void actlist_update(struct hfsc_class *);
113 static struct hfsc_class *actlist_firstfit(struct hfsc_class *, uint64_t);
115 static __inline uint64_t seg_x2y(uint64_t, uint64_t);
116 static __inline uint64_t seg_y2x(uint64_t, uint64_t);
117 static __inline uint64_t m2sm(u_int);
118 static __inline uint64_t m2ism(u_int);
119 static __inline uint64_t d2dx(u_int);
120 static u_int sm2m(uint64_t);
121 static u_int dx2d(uint64_t);
123 static void sc2isc(struct service_curve *, struct internal_sc *);
124 static void rtsc_init(struct runtime_sc *, struct internal_sc *,
125 uint64_t, uint64_t);
126 static uint64_t rtsc_y2x(struct runtime_sc *, uint64_t);
127 static uint64_t rtsc_x2y(struct runtime_sc *, uint64_t);
128 static void rtsc_min(struct runtime_sc *, struct internal_sc *,
129 uint64_t, uint64_t);
131 static void get_class_stats(struct hfsc_classstats *, struct hfsc_class *);
132 static struct hfsc_class *clh_to_clp(struct hfsc_if *, uint32_t);
135 * macros
137 #define is_a_parent_class(cl) ((cl)->cl_children != NULL)
139 #define HT_INFINITY 0xffffffffffffffffLL /* infinite time value */
142 hfsc_pfattach(struct pf_altq *a, struct ifaltq *ifq)
144 return altq_attach(ifq, ALTQT_HFSC, a->altq_disc,
145 hfsc_enqueue, hfsc_dequeue, hfsc_request, NULL, NULL);
149 hfsc_add_altq(struct pf_altq *a)
151 struct hfsc_if *hif;
152 struct ifnet *ifp;
154 if ((ifp = ifunit(a->ifname)) == NULL)
155 return (EINVAL);
156 if (!ifq_is_ready(&ifp->if_snd))
157 return (ENODEV);
159 hif = kmalloc(sizeof(struct hfsc_if), M_ALTQ, M_WAITOK | M_ZERO);
161 hif->hif_eligible = ellist_alloc();
162 hif->hif_ifq = &ifp->if_snd;
163 ifq_purge(&ifp->if_snd);
165 /* keep the state in pf_altq */
166 a->altq_disc = hif;
168 return (0);
172 hfsc_remove_altq(struct pf_altq *a)
174 struct hfsc_if *hif;
176 if ((hif = a->altq_disc) == NULL)
177 return (EINVAL);
178 a->altq_disc = NULL;
180 hfsc_clear_interface(hif);
181 hfsc_class_destroy(hif->hif_rootclass);
183 ellist_destroy(hif->hif_eligible);
185 kfree(hif, M_ALTQ);
187 return (0);
190 static int
191 hfsc_add_queue_locked(struct pf_altq *a, struct hfsc_if *hif)
193 struct hfsc_class *cl, *parent;
194 struct hfsc_opts *opts;
195 struct service_curve rtsc, lssc, ulsc;
197 KKASSERT(a->qid != 0);
199 opts = &a->pq_u.hfsc_opts;
201 if (a->parent_qid == HFSC_NULLCLASS_HANDLE && hif->hif_rootclass == NULL)
202 parent = NULL;
203 else if ((parent = clh_to_clp(hif, a->parent_qid)) == NULL)
204 return (EINVAL);
206 if (clh_to_clp(hif, a->qid) != NULL)
207 return (EBUSY);
209 rtsc.m1 = opts->rtsc_m1;
210 rtsc.d = opts->rtsc_d;
211 rtsc.m2 = opts->rtsc_m2;
212 lssc.m1 = opts->lssc_m1;
213 lssc.d = opts->lssc_d;
214 lssc.m2 = opts->lssc_m2;
215 ulsc.m1 = opts->ulsc_m1;
216 ulsc.d = opts->ulsc_d;
217 ulsc.m2 = opts->ulsc_m2;
219 cl = hfsc_class_create(hif, &rtsc, &lssc, &ulsc, parent, a->qlimit,
220 opts->flags, a->qid);
221 if (cl == NULL)
222 return (ENOMEM);
224 return (0);
228 hfsc_add_queue(struct pf_altq *a)
230 struct hfsc_if *hif;
231 struct ifaltq *ifq;
232 int error;
234 if (a->qid == 0)
235 return (EINVAL);
237 /* XXX not MP safe */
238 if ((hif = a->altq_disc) == NULL)
239 return (EINVAL);
240 ifq = hif->hif_ifq;
242 ALTQ_LOCK(ifq);
243 error = hfsc_add_queue_locked(a, hif);
244 ALTQ_UNLOCK(ifq);
246 return error;
249 static int
250 hfsc_remove_queue_locked(struct pf_altq *a, struct hfsc_if *hif)
252 struct hfsc_class *cl;
254 if ((cl = clh_to_clp(hif, a->qid)) == NULL)
255 return (EINVAL);
257 return (hfsc_class_destroy(cl));
261 hfsc_remove_queue(struct pf_altq *a)
263 struct hfsc_if *hif;
264 struct ifaltq *ifq;
265 int error;
267 /* XXX not MP safe */
268 if ((hif = a->altq_disc) == NULL)
269 return (EINVAL);
270 ifq = hif->hif_ifq;
272 ALTQ_LOCK(ifq);
273 error = hfsc_remove_queue_locked(a, hif);
274 ALTQ_UNLOCK(ifq);
276 return error;
280 hfsc_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
282 struct hfsc_if *hif;
283 struct hfsc_class *cl;
284 struct hfsc_classstats stats;
285 struct ifaltq *ifq;
286 int error = 0;
288 if (*nbytes < sizeof(stats))
289 return (EINVAL);
291 /* XXX not MP safe */
292 if ((hif = altq_lookup(a->ifname, ALTQT_HFSC)) == NULL)
293 return (EBADF);
294 ifq = hif->hif_ifq;
296 ALTQ_LOCK(ifq);
298 if ((cl = clh_to_clp(hif, a->qid)) == NULL) {
299 ALTQ_UNLOCK(ifq);
300 return (EINVAL);
303 get_class_stats(&stats, cl);
305 ALTQ_UNLOCK(ifq);
307 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
308 return (error);
309 *nbytes = sizeof(stats);
310 return (0);
314 * bring the interface back to the initial state by discarding
315 * all the filters and classes except the root class.
317 static int
318 hfsc_clear_interface(struct hfsc_if *hif)
320 struct hfsc_class *cl;
322 if (hif->hif_rootclass == NULL)
323 return (0);
326 /* clear out the classes */
327 while ((cl = hif->hif_rootclass->cl_children) != NULL) {
329 * remove the first leaf class found in the hierarchy
330 * then start over
332 for (; cl != NULL; cl = hfsc_nextclass(cl)) {
333 if (!is_a_parent_class(cl)) {
334 hfsc_class_destroy(cl);
335 break;
340 return (0);
343 static int
344 hfsc_request(struct ifaltq *ifq, int req, void *arg)
346 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
348 crit_enter();
349 switch (req) {
350 case ALTRQ_PURGE:
351 hfsc_purge(hif);
352 break;
354 crit_exit();
355 return (0);
358 /* discard all the queued packets on the interface */
359 static void
360 hfsc_purge(struct hfsc_if *hif)
362 struct hfsc_class *cl;
364 for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl)) {
365 if (!qempty(cl->cl_q))
366 hfsc_purgeq(cl);
368 if (ifq_is_enabled(hif->hif_ifq))
369 hif->hif_ifq->ifq_len = 0;
372 struct hfsc_class *
373 hfsc_class_create(struct hfsc_if *hif, struct service_curve *rsc,
374 struct service_curve *fsc, struct service_curve *usc,
375 struct hfsc_class *parent, int qlimit, int flags, int qid)
377 struct hfsc_class *cl, *p;
378 int i;
380 if (hif->hif_classes >= HFSC_MAX_CLASSES)
381 return (NULL);
383 #ifndef ALTQ_RED
384 if (flags & HFCF_RED) {
385 #ifdef ALTQ_DEBUG
386 kprintf("hfsc_class_create: RED not configured for HFSC!\n");
387 #endif
388 return (NULL);
390 #endif
392 cl = kmalloc(sizeof(*cl), M_ALTQ, M_WAITOK | M_ZERO);
393 cl->cl_q = kmalloc(sizeof(*cl->cl_q), M_ALTQ, M_WAITOK | M_ZERO);
394 cl->cl_actc = actlist_alloc();
396 if (qlimit == 0)
397 qlimit = 50; /* use default */
398 qlimit(cl->cl_q) = qlimit;
399 qtype(cl->cl_q) = Q_DROPTAIL;
400 qlen(cl->cl_q) = 0;
401 cl->cl_flags = flags;
402 #ifdef ALTQ_RED
403 if (flags & (HFCF_RED|HFCF_RIO)) {
404 int red_flags, red_pkttime;
405 u_int m2;
407 m2 = 0;
408 if (rsc != NULL && rsc->m2 > m2)
409 m2 = rsc->m2;
410 if (fsc != NULL && fsc->m2 > m2)
411 m2 = fsc->m2;
412 if (usc != NULL && usc->m2 > m2)
413 m2 = usc->m2;
415 red_flags = 0;
416 if (flags & HFCF_ECN)
417 red_flags |= REDF_ECN;
418 #ifdef ALTQ_RIO
419 if (flags & HFCF_CLEARDSCP)
420 red_flags |= RIOF_CLEARDSCP;
421 #endif
422 if (m2 < 8)
423 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
424 else
425 red_pkttime = (int64_t)hif->hif_ifq->altq_ifp->if_mtu
426 * 1000 * 1000 * 1000 / (m2 / 8);
427 if (flags & HFCF_RED) {
428 cl->cl_red = red_alloc(0, 0,
429 qlimit(cl->cl_q) * 10/100,
430 qlimit(cl->cl_q) * 30/100,
431 red_flags, red_pkttime);
432 if (cl->cl_red != NULL)
433 qtype(cl->cl_q) = Q_RED;
435 #ifdef ALTQ_RIO
436 else {
437 cl->cl_red = (red_t *)rio_alloc(0, NULL,
438 red_flags, red_pkttime);
439 if (cl->cl_red != NULL)
440 qtype(cl->cl_q) = Q_RIO;
442 #endif
444 #endif /* ALTQ_RED */
446 if (rsc != NULL && (rsc->m1 != 0 || rsc->m2 != 0)) {
447 cl->cl_rsc = kmalloc(sizeof(*cl->cl_rsc), M_ALTQ, M_WAITOK);
448 sc2isc(rsc, cl->cl_rsc);
449 rtsc_init(&cl->cl_deadline, cl->cl_rsc, 0, 0);
450 rtsc_init(&cl->cl_eligible, cl->cl_rsc, 0, 0);
452 if (fsc != NULL && (fsc->m1 != 0 || fsc->m2 != 0)) {
453 cl->cl_fsc = kmalloc(sizeof(*cl->cl_fsc), M_ALTQ, M_WAITOK);
454 if (cl->cl_fsc == NULL)
455 goto err_ret;
456 sc2isc(fsc, cl->cl_fsc);
457 rtsc_init(&cl->cl_virtual, cl->cl_fsc, 0, 0);
459 if (usc != NULL && (usc->m1 != 0 || usc->m2 != 0)) {
460 cl->cl_usc = kmalloc(sizeof(*cl->cl_usc), M_ALTQ, M_WAITOK);
461 if (cl->cl_usc == NULL)
462 goto err_ret;
463 sc2isc(usc, cl->cl_usc);
464 rtsc_init(&cl->cl_ulimit, cl->cl_usc, 0, 0);
467 cl->cl_id = hif->hif_classid++;
468 cl->cl_handle = qid;
469 cl->cl_hif = hif;
470 cl->cl_parent = parent;
472 crit_enter();
473 hif->hif_classes++;
476 * find a free slot in the class table. if the slot matching
477 * the lower bits of qid is free, use this slot. otherwise,
478 * use the first free slot.
480 i = qid % HFSC_MAX_CLASSES;
481 if (hif->hif_class_tbl[i] == NULL)
482 hif->hif_class_tbl[i] = cl;
483 else {
484 for (i = 0; i < HFSC_MAX_CLASSES; i++) {
485 if (hif->hif_class_tbl[i] == NULL) {
486 hif->hif_class_tbl[i] = cl;
487 break;
490 if (i == HFSC_MAX_CLASSES) {
491 crit_exit();
492 goto err_ret;
496 if (flags & HFCF_DEFAULTCLASS)
497 hif->hif_defaultclass = cl;
499 if (parent == NULL) {
500 /* this is root class */
501 hif->hif_rootclass = cl;
502 } else if (parent->cl_children == NULL) {
503 /* add this class to the children list of the parent */
504 parent->cl_children = cl;
505 } else {
506 p = parent->cl_children;
507 while (p->cl_siblings != NULL)
508 p = p->cl_siblings;
509 p->cl_siblings = cl;
511 crit_exit();
513 return (cl);
515 err_ret:
516 if (cl->cl_actc != NULL)
517 actlist_destroy(cl->cl_actc);
518 if (cl->cl_red != NULL) {
519 #ifdef ALTQ_RIO
520 if (q_is_rio(cl->cl_q))
521 rio_destroy((rio_t *)cl->cl_red);
522 #endif
523 #ifdef ALTQ_RED
524 if (q_is_red(cl->cl_q))
525 red_destroy(cl->cl_red);
526 #endif
528 if (cl->cl_fsc != NULL)
529 kfree(cl->cl_fsc, M_ALTQ);
530 if (cl->cl_rsc != NULL)
531 kfree(cl->cl_rsc, M_ALTQ);
532 if (cl->cl_usc != NULL)
533 kfree(cl->cl_usc, M_ALTQ);
534 if (cl->cl_q != NULL)
535 kfree(cl->cl_q, M_ALTQ);
536 kfree(cl, M_ALTQ);
537 return (NULL);
540 static int
541 hfsc_class_destroy(struct hfsc_class *cl)
543 int i;
545 if (cl == NULL)
546 return (0);
548 if (is_a_parent_class(cl))
549 return (EBUSY);
551 crit_enter();
553 if (!qempty(cl->cl_q))
554 hfsc_purgeq(cl);
556 if (cl->cl_parent == NULL) {
557 /* this is root class */
558 } else {
559 struct hfsc_class *p = cl->cl_parent->cl_children;
561 if (p == cl) {
562 cl->cl_parent->cl_children = cl->cl_siblings;
563 } else {
564 do {
565 if (p->cl_siblings == cl) {
566 p->cl_siblings = cl->cl_siblings;
567 break;
569 } while ((p = p->cl_siblings) != NULL);
571 KKASSERT(p != NULL);
574 for (i = 0; i < HFSC_MAX_CLASSES; i++) {
575 if (cl->cl_hif->hif_class_tbl[i] == cl) {
576 cl->cl_hif->hif_class_tbl[i] = NULL;
577 break;
581 cl->cl_hif->hif_classes--;
582 crit_exit();
584 actlist_destroy(cl->cl_actc);
586 if (cl->cl_red != NULL) {
587 #ifdef ALTQ_RIO
588 if (q_is_rio(cl->cl_q))
589 rio_destroy((rio_t *)cl->cl_red);
590 #endif
591 #ifdef ALTQ_RED
592 if (q_is_red(cl->cl_q))
593 red_destroy(cl->cl_red);
594 #endif
597 if (cl == cl->cl_hif->hif_rootclass)
598 cl->cl_hif->hif_rootclass = NULL;
599 if (cl == cl->cl_hif->hif_defaultclass)
600 cl->cl_hif->hif_defaultclass = NULL;
602 if (cl->cl_usc != NULL)
603 kfree(cl->cl_usc, M_ALTQ);
604 if (cl->cl_fsc != NULL)
605 kfree(cl->cl_fsc, M_ALTQ);
606 if (cl->cl_rsc != NULL)
607 kfree(cl->cl_rsc, M_ALTQ);
608 kfree(cl->cl_q, M_ALTQ);
609 kfree(cl, M_ALTQ);
611 return (0);
615 * hfsc_nextclass returns the next class in the tree.
616 * usage:
617 * for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl))
618 * do_something;
620 static struct hfsc_class *
621 hfsc_nextclass(struct hfsc_class *cl)
623 if (cl->cl_children != NULL) {
624 cl = cl->cl_children;
625 } else if (cl->cl_siblings != NULL) {
626 cl = cl->cl_siblings;
627 } else {
628 while ((cl = cl->cl_parent) != NULL) {
629 if (cl->cl_siblings != NULL) {
630 cl = cl->cl_siblings;
631 break;
636 return (cl);
640 * hfsc_enqueue is an enqueue function to be registered to
641 * (*altq_enqueue) in struct ifaltq.
643 static int
644 hfsc_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
646 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
647 struct hfsc_class *cl;
648 int len;
650 /* grab class set by classifier */
651 if ((m->m_flags & M_PKTHDR) == 0) {
652 /* should not happen */
653 if_printf(ifq->altq_ifp, "altq: packet does not have pkthdr\n");
654 m_freem(m);
655 return (ENOBUFS);
657 crit_enter();
658 if (m->m_pkthdr.fw_flags & ALTQ_MBUF_TAGGED)
659 cl = clh_to_clp(hif, m->m_pkthdr.altq_qid);
660 else
661 cl = NULL;
662 if (cl == NULL || is_a_parent_class(cl)) {
663 cl = hif->hif_defaultclass;
664 if (cl == NULL) {
665 m_freem(m);
666 crit_exit();
667 return (ENOBUFS);
670 cl->cl_pktattr = NULL;
671 len = m_pktlen(m);
672 if (hfsc_addq(cl, m) != 0) {
673 /* drop occurred. mbuf was freed in hfsc_addq. */
674 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, len);
675 crit_exit();
676 return (ENOBUFS);
678 ifq->ifq_len++;
679 cl->cl_hif->hif_packets++;
681 /* successfully queued. */
682 if (qlen(cl->cl_q) == 1)
683 set_active(cl, m_pktlen(m));
684 crit_exit();
685 return (0);
689 * hfsc_dequeue is a dequeue function to be registered to
690 * (*altq_dequeue) in struct ifaltq.
692 * note: ALTDQ_POLL returns the next packet without removing the packet
693 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
694 * ALTDQ_REMOVE must return the same packet if called immediately
695 * after ALTDQ_POLL.
697 static struct mbuf *
698 hfsc_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op)
700 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc;
701 struct hfsc_class *cl;
702 struct mbuf *m;
703 int len, next_len;
704 int realtime = 0;
705 uint64_t cur_time;
707 if (hif->hif_packets == 0) {
708 /* no packet in the tree */
709 return (NULL);
712 crit_enter();
713 cur_time = read_machclk();
715 if (op == ALTDQ_REMOVE && hif->hif_pollcache != NULL) {
716 cl = hif->hif_pollcache;
717 hif->hif_pollcache = NULL;
718 /* check if the class was scheduled by real-time criteria */
719 if (cl->cl_rsc != NULL)
720 realtime = (cl->cl_e <= cur_time);
721 } else {
723 * if there are eligible classes, use real-time criteria.
724 * find the class with the minimum deadline among
725 * the eligible classes.
727 if ((cl = ellist_get_mindl(hif->hif_eligible, cur_time)) != NULL) {
728 realtime = 1;
729 } else {
730 #ifdef ALTQ_DEBUG
731 int fits = 0;
732 #endif
734 * use link-sharing criteria
735 * get the class with the minimum vt in the hierarchy
737 cl = hif->hif_rootclass;
738 while (is_a_parent_class(cl)) {
740 cl = actlist_firstfit(cl, cur_time);
741 if (cl == NULL) {
742 #ifdef ALTQ_DEBUG
743 if (fits > 0)
744 kprintf("%d fit but none found\n",fits);
745 #endif
746 m = NULL;
747 goto done;
750 * update parent's cl_cvtmin.
751 * don't update if the new vt is smaller.
753 if (cl->cl_parent->cl_cvtmin < cl->cl_vt)
754 cl->cl_parent->cl_cvtmin = cl->cl_vt;
755 #ifdef ALTQ_DEBUG
756 fits++;
757 #endif
761 if (op == ALTDQ_POLL) {
762 hif->hif_pollcache = cl;
763 m = hfsc_pollq(cl);
764 goto done;
768 m = hfsc_getq(cl);
769 if (m == NULL)
770 panic("hfsc_dequeue:");
771 len = m_pktlen(m);
772 cl->cl_hif->hif_packets--;
773 ifq->ifq_len--;
774 PKTCNTR_ADD(&cl->cl_stats.xmit_cnt, len);
776 update_vf(cl, len, cur_time);
777 if (realtime)
778 cl->cl_cumul += len;
780 if (!qempty(cl->cl_q)) {
781 if (cl->cl_rsc != NULL) {
782 /* update ed */
783 next_len = m_pktlen(qhead(cl->cl_q));
785 if (realtime)
786 update_ed(cl, next_len);
787 else
788 update_d(cl, next_len);
790 } else {
791 /* the class becomes passive */
792 set_passive(cl);
794 done:
795 crit_exit();
796 KKASSERT(mpolled == NULL || m == mpolled);
797 return (m);
800 static int
801 hfsc_addq(struct hfsc_class *cl, struct mbuf *m)
804 #ifdef ALTQ_RIO
805 if (q_is_rio(cl->cl_q))
806 return rio_addq((rio_t *)cl->cl_red, cl->cl_q,
807 m, cl->cl_pktattr);
808 #endif
809 #ifdef ALTQ_RED
810 if (q_is_red(cl->cl_q))
811 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
812 #endif
813 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
814 m_freem(m);
815 return (-1);
818 if (cl->cl_flags & HFCF_CLEARDSCP)
819 write_dsfield(m, cl->cl_pktattr, 0);
821 _addq(cl->cl_q, m);
823 return (0);
826 static struct mbuf *
827 hfsc_getq(struct hfsc_class *cl)
829 #ifdef ALTQ_RIO
830 if (q_is_rio(cl->cl_q))
831 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
832 #endif
833 #ifdef ALTQ_RED
834 if (q_is_red(cl->cl_q))
835 return red_getq(cl->cl_red, cl->cl_q);
836 #endif
837 return _getq(cl->cl_q);
840 static struct mbuf *
841 hfsc_pollq(struct hfsc_class *cl)
843 return qhead(cl->cl_q);
846 static void
847 hfsc_purgeq(struct hfsc_class *cl)
849 struct mbuf *m;
851 if (qempty(cl->cl_q))
852 return;
854 while ((m = _getq(cl->cl_q)) != NULL) {
855 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, m_pktlen(m));
856 m_freem(m);
857 cl->cl_hif->hif_packets--;
858 cl->cl_hif->hif_ifq->ifq_len--;
860 KKASSERT(qlen(cl->cl_q) == 0);
862 update_vf(cl, 0, 0); /* remove cl from the actlist */
863 set_passive(cl);
866 static void
867 set_active(struct hfsc_class *cl, int len)
869 if (cl->cl_rsc != NULL)
870 init_ed(cl, len);
871 if (cl->cl_fsc != NULL)
872 init_vf(cl, len);
874 cl->cl_stats.period++;
877 static void
878 set_passive(struct hfsc_class *cl)
880 if (cl->cl_rsc != NULL)
881 ellist_remove(cl);
884 * actlist is now handled in update_vf() so that update_vf(cl, 0, 0)
885 * needs to be called explicitly to remove a class from actlist
889 static void
890 init_ed(struct hfsc_class *cl, int next_len)
892 uint64_t cur_time;
894 cur_time = read_machclk();
896 /* update the deadline curve */
897 rtsc_min(&cl->cl_deadline, cl->cl_rsc, cur_time, cl->cl_cumul);
900 * update the eligible curve.
901 * for concave, it is equal to the deadline curve.
902 * for convex, it is a linear curve with slope m2.
904 cl->cl_eligible = cl->cl_deadline;
905 if (cl->cl_rsc->sm1 <= cl->cl_rsc->sm2) {
906 cl->cl_eligible.dx = 0;
907 cl->cl_eligible.dy = 0;
910 /* compute e and d */
911 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
912 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
914 ellist_insert(cl);
917 static void
918 update_ed(struct hfsc_class *cl, int next_len)
920 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
921 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
923 ellist_update(cl);
926 static void
927 update_d(struct hfsc_class *cl, int next_len)
929 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
932 static void
933 init_vf(struct hfsc_class *cl, int len)
935 struct hfsc_class *max_cl, *p;
936 uint64_t vt, f, cur_time;
937 int go_active;
939 cur_time = 0;
940 go_active = 1;
941 for ( ; cl->cl_parent != NULL; cl = cl->cl_parent) {
942 if (go_active && cl->cl_nactive++ == 0)
943 go_active = 1;
944 else
945 go_active = 0;
947 if (go_active) {
948 max_cl = actlist_last(cl->cl_parent->cl_actc);
949 if (max_cl != NULL) {
951 * set vt to the average of the min and max
952 * classes. if the parent's period didn't
953 * change, don't decrease vt of the class.
955 vt = max_cl->cl_vt;
956 if (cl->cl_parent->cl_cvtmin != 0)
957 vt = (cl->cl_parent->cl_cvtmin + vt)/2;
959 if (cl->cl_parent->cl_vtperiod !=
960 cl->cl_parentperiod || vt > cl->cl_vt)
961 cl->cl_vt = vt;
962 } else {
964 * first child for a new parent backlog period.
965 * add parent's cvtmax to vtoff of children
966 * to make a new vt (vtoff + vt) larger than
967 * the vt in the last period for all children.
969 vt = cl->cl_parent->cl_cvtmax;
970 for (p = cl->cl_parent->cl_children; p != NULL;
971 p = p->cl_siblings)
972 p->cl_vtoff += vt;
973 cl->cl_vt = 0;
974 cl->cl_parent->cl_cvtmax = 0;
975 cl->cl_parent->cl_cvtmin = 0;
977 cl->cl_initvt = cl->cl_vt;
979 /* update the virtual curve */
980 vt = cl->cl_vt + cl->cl_vtoff;
981 rtsc_min(&cl->cl_virtual, cl->cl_fsc, vt, cl->cl_total);
982 if (cl->cl_virtual.x == vt) {
983 cl->cl_virtual.x -= cl->cl_vtoff;
984 cl->cl_vtoff = 0;
986 cl->cl_vtadj = 0;
988 cl->cl_vtperiod++; /* increment vt period */
989 cl->cl_parentperiod = cl->cl_parent->cl_vtperiod;
990 if (cl->cl_parent->cl_nactive == 0)
991 cl->cl_parentperiod++;
992 cl->cl_f = 0;
994 actlist_insert(cl);
996 if (cl->cl_usc != NULL) {
997 /* class has upper limit curve */
998 if (cur_time == 0)
999 cur_time = read_machclk();
1001 /* update the ulimit curve */
1002 rtsc_min(&cl->cl_ulimit, cl->cl_usc, cur_time,
1003 cl->cl_total);
1004 /* compute myf */
1005 cl->cl_myf = rtsc_y2x(&cl->cl_ulimit,
1006 cl->cl_total);
1007 cl->cl_myfadj = 0;
1011 if (cl->cl_myf > cl->cl_cfmin)
1012 f = cl->cl_myf;
1013 else
1014 f = cl->cl_cfmin;
1015 if (f != cl->cl_f) {
1016 cl->cl_f = f;
1017 update_cfmin(cl->cl_parent);
1022 static void
1023 update_vf(struct hfsc_class *cl, int len, uint64_t cur_time)
1025 uint64_t f, myf_bound, delta;
1026 int go_passive;
1028 go_passive = qempty(cl->cl_q);
1030 for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
1031 cl->cl_total += len;
1033 if (cl->cl_fsc == NULL || cl->cl_nactive == 0)
1034 continue;
1036 if (go_passive && --cl->cl_nactive == 0)
1037 go_passive = 1;
1038 else
1039 go_passive = 0;
1041 if (go_passive) {
1042 /* no more active child, going passive */
1044 /* update cvtmax of the parent class */
1045 if (cl->cl_vt > cl->cl_parent->cl_cvtmax)
1046 cl->cl_parent->cl_cvtmax = cl->cl_vt;
1048 /* remove this class from the vt list */
1049 actlist_remove(cl);
1051 update_cfmin(cl->cl_parent);
1053 continue;
1057 * update vt and f
1059 cl->cl_vt = rtsc_y2x(&cl->cl_virtual, cl->cl_total)
1060 - cl->cl_vtoff + cl->cl_vtadj;
1063 * if vt of the class is smaller than cvtmin,
1064 * the class was skipped in the past due to non-fit.
1065 * if so, we need to adjust vtadj.
1067 if (cl->cl_vt < cl->cl_parent->cl_cvtmin) {
1068 cl->cl_vtadj += cl->cl_parent->cl_cvtmin - cl->cl_vt;
1069 cl->cl_vt = cl->cl_parent->cl_cvtmin;
1072 /* update the vt list */
1073 actlist_update(cl);
1075 if (cl->cl_usc != NULL) {
1076 cl->cl_myf = cl->cl_myfadj
1077 + rtsc_y2x(&cl->cl_ulimit, cl->cl_total);
1080 * if myf lags behind by more than one clock tick
1081 * from the current time, adjust myfadj to prevent
1082 * a rate-limited class from going greedy.
1083 * in a steady state under rate-limiting, myf
1084 * fluctuates within one clock tick.
1086 myf_bound = cur_time - machclk_per_tick;
1087 if (cl->cl_myf < myf_bound) {
1088 delta = cur_time - cl->cl_myf;
1089 cl->cl_myfadj += delta;
1090 cl->cl_myf += delta;
1094 /* cl_f is max(cl_myf, cl_cfmin) */
1095 if (cl->cl_myf > cl->cl_cfmin)
1096 f = cl->cl_myf;
1097 else
1098 f = cl->cl_cfmin;
1099 if (f != cl->cl_f) {
1100 cl->cl_f = f;
1101 update_cfmin(cl->cl_parent);
1106 static void
1107 update_cfmin(struct hfsc_class *cl)
1109 struct hfsc_class *p;
1110 uint64_t cfmin;
1112 if (TAILQ_EMPTY(cl->cl_actc)) {
1113 cl->cl_cfmin = 0;
1114 return;
1116 cfmin = HT_INFINITY;
1117 TAILQ_FOREACH(p, cl->cl_actc, cl_actlist) {
1118 if (p->cl_f == 0) {
1119 cl->cl_cfmin = 0;
1120 return;
1122 if (p->cl_f < cfmin)
1123 cfmin = p->cl_f;
1125 cl->cl_cfmin = cfmin;
1129 * TAILQ based ellist and actlist implementation
1130 * (ion wanted to make a calendar queue based implementation)
1133 * eligible list holds backlogged classes being sorted by their eligible times.
1134 * there is one eligible list per interface.
1137 static ellist_t *
1138 ellist_alloc(void)
1140 ellist_t *head;
1142 head = kmalloc(sizeof(ellist_t *), M_ALTQ, M_WAITOK);
1143 TAILQ_INIT(head);
1144 return (head);
1147 static void
1148 ellist_destroy(ellist_t *head)
1150 kfree(head, M_ALTQ);
1153 static void
1154 ellist_insert(struct hfsc_class *cl)
1156 struct hfsc_if *hif = cl->cl_hif;
1157 struct hfsc_class *p;
1159 /* check the last entry first */
1160 if ((p = TAILQ_LAST(hif->hif_eligible, _eligible)) == NULL ||
1161 p->cl_e <= cl->cl_e) {
1162 TAILQ_INSERT_TAIL(hif->hif_eligible, cl, cl_ellist);
1163 return;
1166 TAILQ_FOREACH(p, hif->hif_eligible, cl_ellist) {
1167 if (cl->cl_e < p->cl_e) {
1168 TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1169 return;
1172 KKASSERT(0); /* should not reach here */
1175 static void
1176 ellist_remove(struct hfsc_class *cl)
1178 struct hfsc_if *hif = cl->cl_hif;
1180 TAILQ_REMOVE(hif->hif_eligible, cl, cl_ellist);
1183 static void
1184 ellist_update(struct hfsc_class *cl)
1186 struct hfsc_if *hif = cl->cl_hif;
1187 struct hfsc_class *p, *last;
1190 * the eligible time of a class increases monotonically.
1191 * if the next entry has a larger eligible time, nothing to do.
1193 p = TAILQ_NEXT(cl, cl_ellist);
1194 if (p == NULL || cl->cl_e <= p->cl_e)
1195 return;
1197 /* check the last entry */
1198 last = TAILQ_LAST(hif->hif_eligible, _eligible);
1199 KKASSERT(last != NULL);
1200 if (last->cl_e <= cl->cl_e) {
1201 TAILQ_REMOVE(hif->hif_eligible, cl, cl_ellist);
1202 TAILQ_INSERT_TAIL(hif->hif_eligible, cl, cl_ellist);
1203 return;
1207 * the new position must be between the next entry
1208 * and the last entry
1210 while ((p = TAILQ_NEXT(p, cl_ellist)) != NULL) {
1211 if (cl->cl_e < p->cl_e) {
1212 TAILQ_REMOVE(hif->hif_eligible, cl, cl_ellist);
1213 TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1214 return;
1217 KKASSERT(0); /* should not reach here */
1220 /* find the class with the minimum deadline among the eligible classes */
1221 struct hfsc_class *
1222 ellist_get_mindl(ellist_t *head, uint64_t cur_time)
1224 struct hfsc_class *p, *cl = NULL;
1226 TAILQ_FOREACH(p, head, cl_ellist) {
1227 if (p->cl_e > cur_time)
1228 break;
1229 if (cl == NULL || p->cl_d < cl->cl_d)
1230 cl = p;
1232 return (cl);
1236 * active children list holds backlogged child classes being sorted
1237 * by their virtual time.
1238 * each intermediate class has one active children list.
1240 static actlist_t *
1241 actlist_alloc(void)
1243 actlist_t *head;
1245 head = kmalloc(sizeof(*head), M_ALTQ, M_WAITOK);
1246 TAILQ_INIT(head);
1247 return (head);
1250 static void
1251 actlist_destroy(actlist_t *head)
1253 kfree(head, M_ALTQ);
1255 static void
1256 actlist_insert(struct hfsc_class *cl)
1258 struct hfsc_class *p;
1260 /* check the last entry first */
1261 if ((p = TAILQ_LAST(cl->cl_parent->cl_actc, _active)) == NULL
1262 || p->cl_vt <= cl->cl_vt) {
1263 TAILQ_INSERT_TAIL(cl->cl_parent->cl_actc, cl, cl_actlist);
1264 return;
1267 TAILQ_FOREACH(p, cl->cl_parent->cl_actc, cl_actlist) {
1268 if (cl->cl_vt < p->cl_vt) {
1269 TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1270 return;
1273 KKASSERT(0); /* should not reach here */
1276 static void
1277 actlist_remove(struct hfsc_class *cl)
1279 TAILQ_REMOVE(cl->cl_parent->cl_actc, cl, cl_actlist);
1282 static void
1283 actlist_update(struct hfsc_class *cl)
1285 struct hfsc_class *p, *last;
1288 * the virtual time of a class increases monotonically during its
1289 * backlogged period.
1290 * if the next entry has a larger virtual time, nothing to do.
1292 p = TAILQ_NEXT(cl, cl_actlist);
1293 if (p == NULL || cl->cl_vt < p->cl_vt)
1294 return;
1296 /* check the last entry */
1297 last = TAILQ_LAST(cl->cl_parent->cl_actc, _active);
1298 KKASSERT(last != NULL);
1299 if (last->cl_vt <= cl->cl_vt) {
1300 TAILQ_REMOVE(cl->cl_parent->cl_actc, cl, cl_actlist);
1301 TAILQ_INSERT_TAIL(cl->cl_parent->cl_actc, cl, cl_actlist);
1302 return;
1306 * the new position must be between the next entry
1307 * and the last entry
1309 while ((p = TAILQ_NEXT(p, cl_actlist)) != NULL) {
1310 if (cl->cl_vt < p->cl_vt) {
1311 TAILQ_REMOVE(cl->cl_parent->cl_actc, cl, cl_actlist);
1312 TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1313 return;
1316 KKASSERT(0); /* should not reach here */
1319 static struct hfsc_class *
1320 actlist_firstfit(struct hfsc_class *cl, uint64_t cur_time)
1322 struct hfsc_class *p;
1324 TAILQ_FOREACH(p, cl->cl_actc, cl_actlist) {
1325 if (p->cl_f <= cur_time)
1326 return (p);
1328 return (NULL);
1332 * service curve support functions
1334 * external service curve parameters
1335 * m: bits/sec
1336 * d: msec
1337 * internal service curve parameters
1338 * sm: (bytes/tsc_interval) << SM_SHIFT
1339 * ism: (tsc_count/byte) << ISM_SHIFT
1340 * dx: tsc_count
1342 * SM_SHIFT and ISM_SHIFT are scaled in order to keep effective digits.
1343 * we should be able to handle 100K-1Gbps linkspeed with 200Hz-1GHz CPU
1344 * speed. SM_SHIFT and ISM_SHIFT are selected to have at least 3 effective
1345 * digits in decimal using the following table.
1347 * bits/sec 100Kbps 1Mbps 10Mbps 100Mbps 1Gbps
1348 * ----------+-------------------------------------------------------
1349 * bytes/nsec 12.5e-6 125e-6 1250e-6 12500e-6 125000e-6
1350 * sm(500MHz) 25.0e-6 250e-6 2500e-6 25000e-6 250000e-6
1351 * sm(200MHz) 62.5e-6 625e-6 6250e-6 62500e-6 625000e-6
1353 * nsec/byte 80000 8000 800 80 8
1354 * ism(500MHz) 40000 4000 400 40 4
1355 * ism(200MHz) 16000 1600 160 16 1.6
1357 #define SM_SHIFT 24
1358 #define ISM_SHIFT 10
1360 #define SM_MASK ((1LL << SM_SHIFT) - 1)
1361 #define ISM_MASK ((1LL << ISM_SHIFT) - 1)
1363 static __inline uint64_t
1364 seg_x2y(uint64_t x, uint64_t sm)
1366 uint64_t y;
1369 * compute
1370 * y = x * sm >> SM_SHIFT
1371 * but divide it for the upper and lower bits to avoid overflow
1373 y = (x >> SM_SHIFT) * sm + (((x & SM_MASK) * sm) >> SM_SHIFT);
1374 return (y);
1377 static __inline uint64_t
1378 seg_y2x(uint64_t y, uint64_t ism)
1380 uint64_t x;
1382 if (y == 0)
1383 x = 0;
1384 else if (ism == HT_INFINITY)
1385 x = HT_INFINITY;
1386 else
1387 x = (y >> ISM_SHIFT) * ism + (((y & ISM_MASK) * ism) >> ISM_SHIFT);
1389 return (x);
1392 static __inline uint64_t
1393 m2sm(u_int m)
1395 uint64_t sm;
1397 sm = ((uint64_t)m << SM_SHIFT) / 8 / machclk_freq;
1398 return (sm);
1401 static __inline uint64_t
1402 m2ism(u_int m)
1404 uint64_t ism;
1406 if (m == 0)
1407 ism = HT_INFINITY;
1408 else
1409 ism = ((uint64_t)machclk_freq << ISM_SHIFT) * 8 / m;
1410 return (ism);
1413 static __inline uint64_t
1414 d2dx(u_int d)
1416 uint64_t dx;
1418 dx = ((uint64_t)d * machclk_freq) / 1000;
1419 return (dx);
1422 static u_int
1423 sm2m(uint64_t sm)
1425 uint64_t m;
1427 m = (sm * 8 * machclk_freq) >> SM_SHIFT;
1428 return ((u_int)m);
1431 static u_int
1432 dx2d(uint64_t dx)
1434 uint64_t d;
1436 d = dx * 1000 / machclk_freq;
1437 return ((u_int)d);
1440 static void
1441 sc2isc(struct service_curve *sc, struct internal_sc *isc)
1443 isc->sm1 = m2sm(sc->m1);
1444 isc->ism1 = m2ism(sc->m1);
1445 isc->dx = d2dx(sc->d);
1446 isc->dy = seg_x2y(isc->dx, isc->sm1);
1447 isc->sm2 = m2sm(sc->m2);
1448 isc->ism2 = m2ism(sc->m2);
1452 * initialize the runtime service curve with the given internal
1453 * service curve starting at (x, y).
1455 static void
1456 rtsc_init(struct runtime_sc *rtsc, struct internal_sc *isc, uint64_t x, uint64_t y)
1458 rtsc->x = x;
1459 rtsc->y = y;
1460 rtsc->sm1 = isc->sm1;
1461 rtsc->ism1 = isc->ism1;
1462 rtsc->dx = isc->dx;
1463 rtsc->dy = isc->dy;
1464 rtsc->sm2 = isc->sm2;
1465 rtsc->ism2 = isc->ism2;
1469 * calculate the y-projection of the runtime service curve by the
1470 * given x-projection value
1472 static uint64_t
1473 rtsc_y2x(struct runtime_sc *rtsc, uint64_t y)
1475 uint64_t x;
1477 if (y < rtsc->y) {
1478 x = rtsc->x;
1479 } else if (y <= rtsc->y + rtsc->dy) {
1480 /* x belongs to the 1st segment */
1481 if (rtsc->dy == 0)
1482 x = rtsc->x + rtsc->dx;
1483 else
1484 x = rtsc->x + seg_y2x(y - rtsc->y, rtsc->ism1);
1485 } else {
1486 /* x belongs to the 2nd segment */
1487 x = rtsc->x + rtsc->dx
1488 + seg_y2x(y - rtsc->y - rtsc->dy, rtsc->ism2);
1490 return (x);
1493 static uint64_t
1494 rtsc_x2y(struct runtime_sc *rtsc, uint64_t x)
1496 uint64_t y;
1498 if (x <= rtsc->x) {
1499 y = rtsc->y;
1500 } else if (x <= rtsc->x + rtsc->dx) {
1501 /* y belongs to the 1st segment */
1502 y = rtsc->y + seg_x2y(x - rtsc->x, rtsc->sm1);
1503 } else
1504 /* y belongs to the 2nd segment */
1505 y = rtsc->y + rtsc->dy
1506 + seg_x2y(x - rtsc->x - rtsc->dx, rtsc->sm2);
1507 return (y);
1511 * update the runtime service curve by taking the minimum of the current
1512 * runtime service curve and the service curve starting at (x, y).
1514 static void
1515 rtsc_min(struct runtime_sc *rtsc, struct internal_sc *isc, uint64_t x, uint64_t y)
1517 uint64_t y1, y2, dx, dy;
1519 if (isc->sm1 <= isc->sm2) {
1520 /* service curve is convex */
1521 y1 = rtsc_x2y(rtsc, x);
1522 if (y1 < y)
1523 /* the current rtsc is smaller */
1524 return;
1525 rtsc->x = x;
1526 rtsc->y = y;
1527 return;
1531 * service curve is concave
1532 * compute the two y values of the current rtsc
1533 * y1: at x
1534 * y2: at (x + dx)
1536 y1 = rtsc_x2y(rtsc, x);
1537 if (y1 <= y) {
1538 /* rtsc is below isc, no change to rtsc */
1539 return;
1542 y2 = rtsc_x2y(rtsc, x + isc->dx);
1543 if (y2 >= y + isc->dy) {
1544 /* rtsc is above isc, replace rtsc by isc */
1545 rtsc->x = x;
1546 rtsc->y = y;
1547 rtsc->dx = isc->dx;
1548 rtsc->dy = isc->dy;
1549 return;
1553 * the two curves intersect
1554 * compute the offsets (dx, dy) using the reverse
1555 * function of seg_x2y()
1556 * seg_x2y(dx, sm1) == seg_x2y(dx, sm2) + (y1 - y)
1558 dx = ((y1 - y) << SM_SHIFT) / (isc->sm1 - isc->sm2);
1560 * check if (x, y1) belongs to the 1st segment of rtsc.
1561 * if so, add the offset.
1563 if (rtsc->x + rtsc->dx > x)
1564 dx += rtsc->x + rtsc->dx - x;
1565 dy = seg_x2y(dx, isc->sm1);
1567 rtsc->x = x;
1568 rtsc->y = y;
1569 rtsc->dx = dx;
1570 rtsc->dy = dy;
1573 static void
1574 get_class_stats(struct hfsc_classstats *sp, struct hfsc_class *cl)
1576 sp->class_id = cl->cl_id;
1577 sp->class_handle = cl->cl_handle;
1579 if (cl->cl_rsc != NULL) {
1580 sp->rsc.m1 = sm2m(cl->cl_rsc->sm1);
1581 sp->rsc.d = dx2d(cl->cl_rsc->dx);
1582 sp->rsc.m2 = sm2m(cl->cl_rsc->sm2);
1583 } else {
1584 sp->rsc.m1 = 0;
1585 sp->rsc.d = 0;
1586 sp->rsc.m2 = 0;
1588 if (cl->cl_fsc != NULL) {
1589 sp->fsc.m1 = sm2m(cl->cl_fsc->sm1);
1590 sp->fsc.d = dx2d(cl->cl_fsc->dx);
1591 sp->fsc.m2 = sm2m(cl->cl_fsc->sm2);
1592 } else {
1593 sp->fsc.m1 = 0;
1594 sp->fsc.d = 0;
1595 sp->fsc.m2 = 0;
1597 if (cl->cl_usc != NULL) {
1598 sp->usc.m1 = sm2m(cl->cl_usc->sm1);
1599 sp->usc.d = dx2d(cl->cl_usc->dx);
1600 sp->usc.m2 = sm2m(cl->cl_usc->sm2);
1601 } else {
1602 sp->usc.m1 = 0;
1603 sp->usc.d = 0;
1604 sp->usc.m2 = 0;
1607 sp->total = cl->cl_total;
1608 sp->cumul = cl->cl_cumul;
1610 sp->d = cl->cl_d;
1611 sp->e = cl->cl_e;
1612 sp->vt = cl->cl_vt;
1613 sp->f = cl->cl_f;
1615 sp->initvt = cl->cl_initvt;
1616 sp->vtperiod = cl->cl_vtperiod;
1617 sp->parentperiod = cl->cl_parentperiod;
1618 sp->nactive = cl->cl_nactive;
1619 sp->vtoff = cl->cl_vtoff;
1620 sp->cvtmax = cl->cl_cvtmax;
1621 sp->myf = cl->cl_myf;
1622 sp->cfmin = cl->cl_cfmin;
1623 sp->cvtmin = cl->cl_cvtmin;
1624 sp->myfadj = cl->cl_myfadj;
1625 sp->vtadj = cl->cl_vtadj;
1627 sp->cur_time = read_machclk();
1628 sp->machclk_freq = machclk_freq;
1630 sp->qlength = qlen(cl->cl_q);
1631 sp->qlimit = qlimit(cl->cl_q);
1632 sp->xmit_cnt = cl->cl_stats.xmit_cnt;
1633 sp->drop_cnt = cl->cl_stats.drop_cnt;
1634 sp->period = cl->cl_stats.period;
1636 sp->qtype = qtype(cl->cl_q);
1637 #ifdef ALTQ_RED
1638 if (q_is_red(cl->cl_q))
1639 red_getstats(cl->cl_red, &sp->red[0]);
1640 #endif
1641 #ifdef ALTQ_RIO
1642 if (q_is_rio(cl->cl_q))
1643 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
1644 #endif
1647 /* convert a class handle to the corresponding class pointer */
1648 static struct hfsc_class *
1649 clh_to_clp(struct hfsc_if *hif, uint32_t chandle)
1651 int i;
1652 struct hfsc_class *cl;
1654 if (chandle == 0)
1655 return (NULL);
1657 * first, try optimistically the slot matching the lower bits of
1658 * the handle. if it fails, do the linear table search.
1660 i = chandle % HFSC_MAX_CLASSES;
1661 if ((cl = hif->hif_class_tbl[i]) != NULL && cl->cl_handle == chandle)
1662 return (cl);
1663 for (i = 0; i < HFSC_MAX_CLASSES; i++)
1664 if ((cl = hif->hif_class_tbl[i]) != NULL &&
1665 cl->cl_handle == chandle)
1666 return (cl);
1667 return (NULL);
1670 #endif /* ALTQ_HFSC */