2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/net/altq/altq_fairq.c,v 1.2 2008/05/14 11:59:23 sephe Exp $
37 * Matt: I gutted altq_priq.c and used it as a skeleton on which to build
38 * fairq. The fairq algorithm is completely different then priq, of course,
39 * but because I used priq's skeleton I believe I should include priq's
42 * Copyright (C) 2000-2003
43 * Sony Computer Science Laboratories Inc. All rights reserved.
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
54 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * FAIRQ - take traffic classified by keep state (hashed into
69 * pf->state_hash) and bucketize it. Fairly extract
70 * the first packet from each bucket in a round-robin fashion.
72 * TODO - better overall qlimit support (right now it is per-bucket).
73 * - NOTE: red etc is per bucket, not overall.
74 * - better service curve support.
78 * altq on em0 fairq bandwidth 650Kb queue { std, bulk }
79 * queue std priority 3 bandwidth 200Kb \
80 * fairq (buckets 64, default, hogs 1Kb) qlimit 50
81 * queue bulk priority 2 bandwidth 100Kb \
82 * fairq (buckets 64, hogs 1Kb) qlimit 50
84 * NOTE: When the aggregate bandwidth is less than the link bandwidth
85 * any remaining bandwidth is dynamically assigned using the
86 * existing bandwidth specs as weightings.
88 * pass out on em0 from any to any keep state queue std
89 * pass out on em0 inet proto tcp ..... port ... keep state queue bulk
93 #include "opt_inet6.h"
95 #ifdef ALTQ_FAIRQ /* fairq is enabled in the kernel conf */
97 #include <sys/param.h>
98 #include <sys/malloc.h>
100 #include <sys/socket.h>
101 #include <sys/sockio.h>
102 #include <sys/systm.h>
103 #include <sys/proc.h>
104 #include <sys/errno.h>
105 #include <sys/kernel.h>
106 #include <sys/queue.h>
107 #include <sys/thread.h>
110 #include <net/ifq_var.h>
111 #include <netinet/in.h>
113 #include <net/pf/pfvar.h>
114 #include <net/altq/altq.h>
115 #include <net/altq/altq_fairq.h>
117 #include <sys/thread2.h>
119 #define FAIRQ_SUBQ_INDEX ALTQ_SUBQ_INDEX_DEFAULT
120 #define FAIRQ_LOCK(ifq) \
121 ALTQ_SQ_LOCK(&(ifq)->altq_subq[FAIRQ_SUBQ_INDEX])
122 #define FAIRQ_UNLOCK(ifq) \
123 ALTQ_SQ_UNLOCK(&(ifq)->altq_subq[FAIRQ_SUBQ_INDEX])
126 * function prototypes
128 static int fairq_clear_interface(struct fairq_if *);
129 static int fairq_request(struct ifaltq_subque *, int, void *);
130 static void fairq_purge(struct fairq_if *);
131 static struct fairq_class *fairq_class_create(struct fairq_if *, int,
132 int, u_int, struct fairq_opts *, int);
133 static int fairq_class_destroy(struct fairq_class *);
134 static int fairq_enqueue(struct ifaltq_subque *, struct mbuf *,
135 struct altq_pktattr *);
136 static struct mbuf *fairq_dequeue(struct ifaltq_subque *, int);
138 static int fairq_addq(struct fairq_class *, struct mbuf *, int hash);
139 static struct mbuf *fairq_getq(struct fairq_class *, uint64_t);
140 static struct mbuf *fairq_pollq(struct fairq_class *, uint64_t, int *);
141 static fairq_bucket_t *fairq_selectq(struct fairq_class *);
142 static void fairq_purgeq(struct fairq_class *);
144 static void get_class_stats(struct fairq_classstats *,
145 struct fairq_class *);
146 static struct fairq_class *clh_to_clp(struct fairq_if *, uint32_t);
149 fairq_pfattach(struct pf_altq *a, struct ifaltq *ifq)
151 return altq_attach(ifq, ALTQT_FAIRQ, a->altq_disc, ifq_mapsubq_default,
152 fairq_enqueue, fairq_dequeue, fairq_request, NULL, NULL);
156 fairq_add_altq(struct pf_altq *a)
158 struct fairq_if *pif;
163 if ((ifp = ifunit(a->ifname)) == NULL) {
167 if (!ifq_is_ready(&ifp->if_snd)) {
172 pif = kmalloc(sizeof(*pif), M_ALTQ, M_WAITOK | M_ZERO);
173 pif->pif_bandwidth = a->ifbandwidth;
174 pif->pif_maxpri = -1;
175 pif->pif_ifq = &ifp->if_snd;
176 ifq_purge_all(&ifp->if_snd);
180 /* keep the state in pf_altq */
187 fairq_remove_altq(struct pf_altq *a)
189 struct fairq_if *pif;
191 if ((pif = a->altq_disc) == NULL)
195 fairq_clear_interface(pif);
202 fairq_add_queue_locked(struct pf_altq *a, struct fairq_if *pif)
204 struct fairq_class *cl;
206 KKASSERT(a->priority < FAIRQ_MAXPRI);
207 KKASSERT(a->qid != 0);
209 if (pif->pif_classes[a->priority] != NULL)
211 if (clh_to_clp(pif, a->qid) != NULL)
214 cl = fairq_class_create(pif, a->priority, a->qlimit, a->bandwidth,
215 &a->pq_u.fairq_opts, a->qid);
223 fairq_add_queue(struct pf_altq *a)
225 struct fairq_if *pif;
229 /* check parameters */
230 if (a->priority >= FAIRQ_MAXPRI)
235 /* XXX not MP safe */
236 if ((pif = a->altq_disc) == NULL)
241 error = fairq_add_queue_locked(a, pif);
248 fairq_remove_queue_locked(struct pf_altq *a, struct fairq_if *pif)
250 struct fairq_class *cl;
252 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
255 return (fairq_class_destroy(cl));
259 fairq_remove_queue(struct pf_altq *a)
261 struct fairq_if *pif;
265 /* XXX not MP safe */
266 if ((pif = a->altq_disc) == NULL)
271 error = fairq_remove_queue_locked(a, pif);
278 fairq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
280 struct fairq_if *pif;
281 struct fairq_class *cl;
282 struct fairq_classstats stats;
286 if (*nbytes < sizeof(stats))
291 /* XXX not MP safe */
292 if ((pif = altq_lookup(a->ifname, ALTQT_FAIRQ)) == NULL) {
300 if ((cl = clh_to_clp(pif, a->qid)) == NULL) {
306 get_class_stats(&stats, cl);
312 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
314 *nbytes = sizeof(stats);
319 * bring the interface back to the initial state by discarding
320 * all the filters and classes.
323 fairq_clear_interface(struct fairq_if *pif)
325 struct fairq_class *cl;
328 /* clear out the classes */
329 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
330 if ((cl = pif->pif_classes[pri]) != NULL)
331 fairq_class_destroy(cl);
338 fairq_request(struct ifaltq_subque *ifsq, int req, void *arg)
340 struct ifaltq *ifq = ifsq->ifsq_altq;
341 struct fairq_if *pif = (struct fairq_if *)ifq->altq_disc;
346 if (ifsq_get_index(ifsq) == FAIRQ_SUBQ_INDEX) {
350 * Race happened, the unrelated subqueue was
351 * picked during the packet scheduler transition.
353 ifsq_classic_request(ifsq, ALTRQ_PURGE, NULL);
361 /* discard all the queued packets on the interface */
363 fairq_purge(struct fairq_if *pif)
365 struct fairq_class *cl;
368 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
369 if ((cl = pif->pif_classes[pri]) != NULL && cl->cl_head)
372 if (ifq_is_enabled(pif->pif_ifq))
373 ALTQ_SQ_CNTR_RESET(&pif->pif_ifq->altq_subq[FAIRQ_SUBQ_INDEX]);
376 static struct fairq_class *
377 fairq_class_create(struct fairq_if *pif, int pri, int qlimit,
378 u_int bandwidth, struct fairq_opts *opts, int qid)
380 struct fairq_class *cl;
381 int flags = opts->flags;
382 u_int nbuckets = opts->nbuckets;
386 if (flags & FARF_RED) {
388 kprintf("fairq_class_create: RED not configured for FAIRQ!\n");
395 if (nbuckets > FAIRQ_MAX_BUCKETS)
396 nbuckets = FAIRQ_MAX_BUCKETS;
397 /* enforce power-of-2 size */
398 while ((nbuckets ^ (nbuckets - 1)) != ((nbuckets << 1) - 1))
401 if ((cl = pif->pif_classes[pri]) != NULL) {
402 /* modify the class instead of creating a new one */
408 if (cl->cl_qtype == Q_RIO)
409 rio_destroy((rio_t *)cl->cl_red);
412 if (cl->cl_qtype == Q_RED)
413 red_destroy(cl->cl_red);
416 cl = kmalloc(sizeof(*cl), M_ALTQ, M_WAITOK | M_ZERO);
417 cl->cl_nbuckets = nbuckets;
418 cl->cl_nbucket_mask = nbuckets - 1;
420 cl->cl_buckets = kmalloc(sizeof(*cl->cl_buckets) *
422 M_ALTQ, M_WAITOK | M_ZERO);
426 pif->pif_classes[pri] = cl;
427 if (flags & FARF_DEFAULTCLASS)
428 pif->pif_default = cl;
430 qlimit = 50; /* use default */
431 cl->cl_qlimit = qlimit;
432 for (i = 0; i < cl->cl_nbuckets; ++i) {
433 qlimit(&cl->cl_buckets[i].queue) = qlimit;
435 cl->cl_bandwidth = bandwidth / 8; /* cvt to bytes per second */
436 cl->cl_qtype = Q_DROPTAIL;
437 cl->cl_flags = flags & FARF_USERFLAGS;
439 if (pri > pif->pif_maxpri)
440 pif->pif_maxpri = pri;
443 cl->cl_hogs_m1 = opts->hogs_m1 / 8;
444 cl->cl_lssc_m1 = opts->lssc_m1 / 8; /* NOT YET USED */
445 cl->cl_bw_current = 0;
448 if (flags & (FARF_RED|FARF_RIO)) {
449 int red_flags, red_pkttime;
452 if (flags & FARF_ECN)
453 red_flags |= REDF_ECN;
455 if (flags & FARF_CLEARDSCP)
456 red_flags |= RIOF_CLEARDSCP;
458 if (pif->pif_bandwidth < 8)
459 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
462 (int64_t)pif->pif_ifq->altq_ifp->if_mtu *
463 (1000 * 1000 * 1000) /
464 (pif->pif_bandwidth / 8 + 1);
466 if (flags & FARF_RIO) {
467 cl->cl_red = (red_t *)rio_alloc(0, NULL,
468 red_flags, red_pkttime);
469 if (cl->cl_red != NULL)
470 cl->cl_qtype = Q_RIO;
473 if (flags & FARF_RED) {
474 cl->cl_red = red_alloc(0, 0,
475 cl->cl_qlimit * 10 / 100,
476 cl->cl_qlimit * 30 / 100,
477 red_flags, red_pkttime);
478 if (cl->cl_red != NULL)
479 cl->cl_qtype = Q_RED;
482 #endif /* ALTQ_RED */
488 fairq_class_destroy(struct fairq_class *cl)
490 struct fairq_if *pif;
499 pif->pif_classes[cl->cl_pri] = NULL;
500 if (pif->pif_poll_cache == cl)
501 pif->pif_poll_cache = NULL;
502 if (pif->pif_maxpri == cl->cl_pri) {
503 for (pri = cl->cl_pri; pri >= 0; pri--)
504 if (pif->pif_classes[pri] != NULL) {
505 pif->pif_maxpri = pri;
509 pif->pif_maxpri = -1;
513 if (cl->cl_red != NULL) {
515 if (cl->cl_qtype == Q_RIO)
516 rio_destroy((rio_t *)cl->cl_red);
519 if (cl->cl_qtype == Q_RED)
520 red_destroy(cl->cl_red);
523 kfree(cl->cl_buckets, M_ALTQ);
524 cl->cl_head = NULL; /* sanity */
525 cl->cl_buckets = NULL; /* sanity */
532 * fairq_enqueue is an enqueue function to be registered to
533 * (*ifsq_enqueue) in struct ifaltq_subque.
536 fairq_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m,
537 struct altq_pktattr *pktattr)
539 struct ifaltq *ifq = ifsq->ifsq_altq;
540 struct fairq_if *pif = (struct fairq_if *)ifq->altq_disc;
541 struct fairq_class *cl;
546 if (ifsq_get_index(ifsq) != FAIRQ_SUBQ_INDEX) {
548 * Race happened, the unrelated subqueue was
549 * picked during the packet scheduler transition.
551 ifsq_classic_request(ifsq, ALTRQ_PURGE, NULL);
558 /* grab class set by classifier */
560 if (m->m_pkthdr.fw_flags & PF_MBUF_STRUCTURE) {
561 cl = clh_to_clp(pif, m->m_pkthdr.pf.qid);
562 if (m->m_pkthdr.pf.flags & PF_TAG_STATE_HASHED)
563 hash = (int)m->m_pkthdr.pf.state_hash;
571 cl = pif->pif_default;
578 cl->cl_flags |= FARF_HAS_PACKETS;
579 cl->cl_pktattr = NULL;
581 if (fairq_addq(cl, m, hash) != 0) {
582 /* drop occurred. mbuf was freed in fairq_addq. */
583 PKTCNTR_ADD(&cl->cl_dropcnt, len);
587 ALTQ_SQ_PKTCNT_INC(ifsq);
595 * fairq_dequeue is a dequeue function to be registered to
596 * (*ifsq_dequeue) in struct ifaltq_subque.
598 * note: ALTDQ_POLL returns the next packet without removing the packet
599 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
602 fairq_dequeue(struct ifaltq_subque *ifsq, int op)
604 struct ifaltq *ifq = ifsq->ifsq_altq;
605 struct fairq_if *pif = (struct fairq_if *)ifq->altq_disc;
606 struct fairq_class *cl;
607 struct fairq_class *best_cl;
610 uint64_t cur_time = read_machclk();
616 if (ifsq_get_index(ifsq) != FAIRQ_SUBQ_INDEX) {
618 * Race happened, the unrelated subqueue was
619 * picked during the packet scheduler transition.
621 ifsq_classic_request(ifsq, ALTRQ_PURGE, NULL);
625 if (ifsq_is_empty(ifsq)) {
626 /* no packet in the queue */
631 if (pif->pif_poll_cache && op == ALTDQ_REMOVE) {
632 best_cl = pif->pif_poll_cache;
633 m = fairq_getq(best_cl, cur_time);
634 pif->pif_poll_cache = NULL;
636 ALTQ_SQ_PKTCNT_DEC(ifsq);
637 PKTCNTR_ADD(&best_cl->cl_xmitcnt, m_pktlen(m));
642 best_scale = 0xFFFFFFFFFFFFFFFFLLU;
644 for (pri = pif->pif_maxpri; pri >= 0; pri--) {
645 if ((cl = pif->pif_classes[pri]) == NULL)
647 if ((cl->cl_flags & FARF_HAS_PACKETS) == 0)
649 m = fairq_pollq(cl, cur_time, &hit_limit);
651 cl->cl_flags &= ~FARF_HAS_PACKETS;
656 * We can halt the search immediately if the queue
657 * did not hit its bandwidth limit.
659 if (hit_limit == 0) {
666 * Otherwise calculate the scale factor and select
667 * the queue with the lowest scale factor. This
668 * apportions any unused bandwidth weighted by
669 * the relative bandwidth specification.
671 * scale = (bw / max) with a multiple of 256.
673 * The calculation is refactored to reduce the
674 * chance of overflow.
676 scale = cl->cl_bw_current * 16 /
677 (cl->cl_bandwidth / 16 + 1);
678 if (best_scale > scale) {
685 if (op == ALTDQ_POLL) {
688 * Don't use poll cache; the poll/dequeue
689 * model is no longer applicable to SMP
697 * The dequeue at (+) will hit the poll
698 * cache set by CPU-B.
700 pif->pif_poll_cache = best_cl;
703 } else if (best_cl) {
704 m = fairq_getq(best_cl, cur_time);
705 KKASSERT(best_m == m);
706 ALTQ_SQ_PKTCNT_DEC(ifsq);
707 PKTCNTR_ADD(&best_cl->cl_xmitcnt, m_pktlen(m));
717 fairq_addq(struct fairq_class *cl, struct mbuf *m, int hash)
724 * If the packet doesn't have any keep state put it on the end of
725 * our queue. XXX this can result in out of order delivery.
729 b = cl->cl_head->prev;
731 b = &cl->cl_buckets[0];
733 hindex = hash & cl->cl_nbucket_mask;
734 b = &cl->cl_buckets[hindex];
738 * Add the bucket to the end of the circular list of active buckets.
740 * As a special case we add the bucket to the beginning of the list
741 * instead of the end if it was not previously on the list and if
742 * its traffic is less then the hog level.
744 if (b->in_use == 0) {
746 if (cl->cl_head == NULL) {
752 b->next = cl->cl_head;
753 b->prev = cl->cl_head->prev;
757 if (b->bw_delta && cl->cl_hogs_m1) {
758 bw = b->bw_bytes * machclk_freq / b->bw_delta;
759 if (bw < cl->cl_hogs_m1) {
768 if (cl->cl_qtype == Q_RIO)
769 return rio_addq((rio_t *)cl->cl_red, &b->queue, m, cl->cl_pktattr);
772 if (cl->cl_qtype == Q_RED)
773 return red_addq(cl->cl_red, &b->queue, m, cl->cl_pktattr);
775 if (qlen(&b->queue) >= qlimit(&b->queue)) {
780 if (cl->cl_flags & FARF_CLEARDSCP)
781 write_dsfield(m, cl->cl_pktattr, 0);
789 fairq_getq(struct fairq_class *cl, uint64_t cur_time)
794 b = fairq_selectq(cl);
798 else if (cl->cl_qtype == Q_RIO)
799 m = rio_getq((rio_t *)cl->cl_red, &b->queue);
802 else if (cl->cl_qtype == Q_RED)
803 m = red_getq(cl->cl_red, &b->queue);
806 m = _getq(&b->queue);
809 * Calculate the BW change
815 * Per-class bandwidth calculation
817 delta = (cur_time - cl->cl_last_time);
818 if (delta > machclk_freq * 8)
819 delta = machclk_freq * 8;
820 cl->cl_bw_delta += delta;
821 cl->cl_bw_bytes += m->m_pkthdr.len;
822 cl->cl_last_time = cur_time;
825 * Cap delta at ~machclk_freq to avoid overflows.
827 if (cl->cl_bw_delta > machclk_freq) {
828 uint64_t f = cl->cl_bw_delta * 32 / machclk_freq;
829 cl->cl_bw_delta = cl->cl_bw_delta * 16 / f;
830 cl->cl_bw_bytes = cl->cl_bw_bytes * 16 / f;
834 * Per-bucket bandwidth calculation.
836 delta = (cur_time - b->last_time);
837 if (delta > machclk_freq * 8)
838 delta = machclk_freq * 8;
839 b->bw_delta += delta;
840 b->bw_bytes += m->m_pkthdr.len;
841 b->last_time = cur_time;
844 * Cap bw_delta at ~machclk_freq to avoid overflows.
846 if (b->bw_delta > machclk_freq) {
847 uint64_t f = b->bw_delta * 32 / machclk_freq;
848 b->bw_delta = b->bw_delta * 16 / f;
849 b->bw_bytes = b->bw_bytes * 16 / f;
856 * Figure out what the next packet would be if there were no limits. If
857 * this class hits its bandwidth limit *hit_limit is set to no-zero, otherwise
858 * it is set to 0. A non-NULL mbuf is returned either way.
861 fairq_pollq(struct fairq_class *cl, uint64_t cur_time, int *hit_limit)
869 b = fairq_selectq(cl);
872 m = qhead(&b->queue);
873 cl->cl_advanced = 1; /* so next select/get doesn't re-advance */
876 * Did this packet exceed the class bandwidth?
878 * Calculate the bandwidth component of the packet in bytes/sec.
879 * Avoid overflows when machclk_freq is very high.
881 delta = cur_time - cl->cl_last_time;
882 if (delta > machclk_freq * 8)
883 delta = machclk_freq * 8;
884 cl->cl_bw_delta += delta;
885 cl->cl_last_time = cur_time;
887 if (cl->cl_bw_delta) {
888 bw = (cl->cl_bw_bytes + m->m_pkthdr.len) *
889 machclk_freq / cl->cl_bw_delta;
890 if (bw > cl->cl_bandwidth)
892 cl->cl_bw_current = bw;
894 kprintf("BW %6lld relative to %6llu %d queue %p\n",
895 bw, cl->cl_bandwidth, *hit_limit, b);
902 * Locate the next queue we want to pull a packet out of. This code
903 * is also responsible for removing empty buckets from the circular list.
907 fairq_selectq(struct fairq_class *cl)
912 while ((b = cl->cl_head) != NULL) {
914 * Remove empty queues from consideration
916 if (qempty(&b->queue)) {
918 cl->cl_head = b->next;
920 if (cl->cl_head == b) {
923 b->next->prev = b->prev;
924 b->prev->next = b->next;
930 * Advance the round robin. Queues with bandwidths less
931 * then the hog bandwidth are allowed to burst.
933 * Don't advance twice if the previous head emptied.
935 if (cl->cl_advanced) {
939 if (cl->cl_hogs_m1 == 0) {
940 cl->cl_head = b->next;
941 } else if (b->bw_delta) {
942 bw = b->bw_bytes * machclk_freq / b->bw_delta;
943 if (bw >= cl->cl_hogs_m1)
944 cl->cl_head = b->next;
948 * Return the (possibly new) head.
957 fairq_purgeq(struct fairq_class *cl)
962 while ((b = fairq_selectq(cl)) != NULL) {
963 while ((m = _getq(&b->queue)) != NULL) {
964 PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m));
967 KKASSERT(qlen(&b->queue) == 0);
972 get_class_stats(struct fairq_classstats *sp, struct fairq_class *cl)
976 sp->class_handle = cl->cl_handle;
977 sp->qlimit = cl->cl_qlimit;
978 sp->xmit_cnt = cl->cl_xmitcnt;
979 sp->drop_cnt = cl->cl_dropcnt;
980 sp->qtype = cl->cl_qtype;
986 sp->qlength += qlen(&b->queue);
988 } while (b != cl->cl_head);
992 if (cl->cl_qtype == Q_RED)
993 red_getstats(cl->cl_red, &sp->red[0]);
996 if (cl->cl_qtype == Q_RIO)
997 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
1001 /* convert a class handle to the corresponding class pointer */
1002 static struct fairq_class *
1003 clh_to_clp(struct fairq_if *pif, uint32_t chandle)
1005 struct fairq_class *cl;
1011 for (idx = pif->pif_maxpri; idx >= 0; idx--)
1012 if ((cl = pif->pif_classes[idx]) != NULL &&
1013 cl->cl_handle == chandle)
1019 #endif /* ALTQ_FAIRQ */