2 * Copyright (c) 1991-1997 Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the Network Research
16 * Group at Lawrence Berkeley Laboratory.
17 * 4. Neither the name of the University nor of the Laboratory may be used
18 * to endorse or promote products derived from this software without
19 * specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * LBL code modified by speer@eng.sun.com, May 1977.
34 * For questions and/or comments, please send mail to cbq@ee.lbl.gov
36 * @(#)rm_class.c 1.48 97/12/05 SMI
37 * $KAME: altq_rmclass.c,v 1.19 2005/04/13 03:44:25 suz Exp $
42 #include "opt_inet6.h"
43 #ifdef ALTQ_CBQ /* cbq is enabled by ALTQ_CBQ option in opt_altq.h */
45 #include <sys/param.h>
46 #include <sys/malloc.h>
48 #include <sys/socket.h>
49 #include <sys/systm.h>
50 #include <sys/errno.h>
53 #include <sys/kernel.h>
57 #include <net/if_var.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
64 #include <net/altq/if_altq.h>
65 #include <net/altq/altq.h>
66 #include <net/altq/altq_codel.h>
67 #include <net/altq/altq_rmclass.h>
68 #include <net/altq/altq_rmclass_debug.h>
69 #include <net/altq/altq_red.h>
70 #include <net/altq/altq_rio.h>
76 #define reset_cutoff(ifd) { ifd->cutoff_ = RM_MAXDEPTH; }
82 static int rmc_satisfied(struct rm_class *, struct timeval *);
83 static void rmc_wrr_set_weights(struct rm_ifdat *);
84 static void rmc_depth_compute(struct rm_class *);
85 static void rmc_depth_recompute(rm_class_t *);
87 static mbuf_t *_rmc_wrr_dequeue_next(struct rm_ifdat *, int);
88 static mbuf_t *_rmc_prr_dequeue_next(struct rm_ifdat *, int);
90 static int _rmc_addq(rm_class_t *, mbuf_t *);
91 static void _rmc_dropq(rm_class_t *);
92 static mbuf_t *_rmc_getq(rm_class_t *);
93 static mbuf_t *_rmc_pollq(rm_class_t *);
95 static int rmc_under_limit(struct rm_class *, struct timeval *);
96 static void rmc_tl_satisfied(struct rm_ifdat *, struct timeval *);
97 static void rmc_drop_action(struct rm_class *);
98 static void rmc_restart(struct rm_class *);
99 static void rmc_root_overlimit(struct rm_class *, struct rm_class *);
101 #define BORROW_OFFTIME
103 * BORROW_OFFTIME (experimental):
104 * borrow the offtime of the class borrowing from.
105 * the reason is that when its own offtime is set, the class is unable
106 * to borrow much, especially when cutoff is taking effect.
107 * but when the borrowed class is overloaded (advidle is close to minidle),
108 * use the borrowing class's offtime to avoid overload.
110 #define ADJUST_CUTOFF
112 * ADJUST_CUTOFF (experimental):
113 * if no underlimit class is found due to cutoff, increase cutoff and
114 * retry the scheduling loop.
115 * also, don't invoke delay_actions while cutoff is taking effect,
116 * since a sleeping class won't have a chance to be scheduled in the
119 * now heuristics for setting the top-level variable (cutoff_) becomes:
120 * 1. if a packet arrives for a not-overlimit class, set cutoff
121 * to the depth of the class.
122 * 2. if cutoff is i, and a packet arrives for an overlimit class
123 * with an underlimit ancestor at a lower level than i (say j),
124 * then set cutoff to j.
125 * 3. at scheduling a packet, if there is no underlimit class
126 * due to the current cutoff level, increase cutoff by 1 and
127 * then try to schedule again.
132 * rmc_newclass(...) - Create a new resource management class at priority
133 * 'pri' on the interface given by 'ifd'.
135 * nsecPerByte is the data rate of the interface in nanoseconds/byte.
136 * E.g., 800 for a 10Mb/s ethernet. If the class gets less
137 * than 100% of the bandwidth, this number should be the
138 * 'effective' rate for the class. Let f be the
139 * bandwidth fraction allocated to this class, and let
140 * nsPerByte be the data rate of the output link in
141 * nanoseconds/byte. Then nsecPerByte is set to
142 * nsPerByte / f. E.g., 1600 (= 800 / .5)
143 * for a class that gets 50% of an ethernet's bandwidth.
145 * action the routine to call when the class is over limit.
147 * maxq max allowable queue size for class (in packets).
149 * parent parent class pointer.
151 * borrow class to borrow from (should be either 'parent' or null).
153 * maxidle max value allowed for class 'idle' time estimate (this
154 * parameter determines how large an initial burst of packets
155 * can be before overlimit action is invoked.
157 * offtime how long 'delay' action will delay when class goes over
158 * limit (this parameter determines the steady-state burst
159 * size when a class is running over its limit).
161 * Maxidle and offtime have to be computed from the following: If the
162 * average packet size is s, the bandwidth fraction allocated to this
163 * class is f, we want to allow b packet bursts, and the gain of the
164 * averaging filter is g (= 1 - 2^(-RM_FILTER_GAIN)), then:
166 * ptime = s * nsPerByte * (1 - f) / f
167 * maxidle = ptime * (1 - g^b) / g^b
168 * minidle = -ptime * (1 / (f - 1))
169 * offtime = ptime * (1 + 1/(1 - g) * (1 - g^(b - 1)) / g^(b - 1)
171 * Operationally, it's convenient to specify maxidle & offtime in units
172 * independent of the link bandwidth so the maxidle & offtime passed to
173 * this routine are the above values multiplied by 8*f/(1000*nsPerByte).
174 * (The constant factor is a scale factor needed to make the parameters
175 * integers. This scaling also means that the 'unscaled' values of
176 * maxidle*nsecPerByte/8 and offtime*nsecPerByte/8 will be in microseconds,
177 * not nanoseconds.) Also note that the 'idle' filter computation keeps
178 * an estimate scaled upward by 2^RM_FILTER_GAIN so the passed value of
179 * maxidle also must be scaled upward by this value. Thus, the passed
180 * values for maxidle and offtime can be computed as follows:
182 * maxidle = maxidle * 2^RM_FILTER_GAIN * 8 / (1000 * nsecPerByte)
183 * offtime = offtime * 8 / (1000 * nsecPerByte)
185 * When USE_HRTIME is employed, then maxidle and offtime become:
186 * maxidle = maxilde * (8.0 / nsecPerByte);
187 * offtime = offtime * (8.0 / nsecPerByte);
190 rmc_newclass(int pri, struct rm_ifdat *ifd, u_int nsecPerByte,
191 void (*action)(rm_class_t *, rm_class_t *), int maxq,
192 struct rm_class *parent, struct rm_class *borrow, u_int maxidle,
193 int minidle, u_int offtime, int pktsize, int flags)
196 struct rm_class *peer;
199 if (pri >= RM_MAXPRIO)
202 if (flags & RMCF_RED) {
204 printf("rmc_newclass: RED not configured for CBQ!\n");
210 if (flags & RMCF_RIO) {
212 printf("rmc_newclass: RIO not configured for CBQ!\n");
218 if (flags & RMCF_CODEL) {
220 printf("rmc_newclass: CODEL not configured for CBQ!\n");
226 cl = malloc(sizeof(struct rm_class), M_DEVBUF, M_NOWAIT | M_ZERO);
229 CALLOUT_INIT(&cl->callout_);
230 cl->q_ = malloc(sizeof(class_queue_t), M_DEVBUF, M_NOWAIT | M_ZERO);
231 if (cl->q_ == NULL) {
237 * Class initialization.
239 cl->children_ = NULL;
240 cl->parent_ = parent;
241 cl->borrow_ = borrow;
245 cl->allotment_ = RM_NS_PER_SEC / nsecPerByte; /* Bytes per sec */
248 cl->ns_per_byte_ = nsecPerByte;
250 qlimit(cl->q_) = maxq;
251 qtype(cl->q_) = Q_DROPHEAD;
255 #if 1 /* minidle is also scaled in ALTQ */
256 cl->minidle_ = (minidle * (int)nsecPerByte) / 8;
257 if (cl->minidle_ > 0)
260 cl->minidle_ = minidle;
262 cl->maxidle_ = (maxidle * nsecPerByte) / 8;
263 if (cl->maxidle_ == 0)
265 #if 1 /* offtime is also scaled in ALTQ */
266 cl->avgidle_ = cl->maxidle_;
267 cl->offtime_ = ((offtime * nsecPerByte) / 8) >> RM_FILTER_GAIN;
268 if (cl->offtime_ == 0)
272 cl->offtime_ = (offtime * nsecPerByte) / 8;
274 cl->overlimit = action;
277 if (flags & (RMCF_RED|RMCF_RIO)) {
278 int red_flags, red_pkttime;
281 if (flags & RMCF_ECN)
282 red_flags |= REDF_ECN;
283 if (flags & RMCF_FLOWVALVE)
284 red_flags |= REDF_FLOWVALVE;
286 if (flags & RMCF_CLEARDSCP)
287 red_flags |= RIOF_CLEARDSCP;
289 red_pkttime = nsecPerByte * pktsize / 1000;
291 if (flags & RMCF_RED) {
292 cl->red_ = red_alloc(0, 0,
293 qlimit(cl->q_) * 10/100,
294 qlimit(cl->q_) * 30/100,
295 red_flags, red_pkttime);
296 if (cl->red_ != NULL)
297 qtype(cl->q_) = Q_RED;
301 cl->red_ = (red_t *)rio_alloc(0, NULL,
302 red_flags, red_pkttime);
303 if (cl->red_ != NULL)
304 qtype(cl->q_) = Q_RIO;
308 #endif /* ALTQ_RED */
310 if (flags & RMCF_CODEL) {
311 cl->codel_ = codel_alloc(5, 100, 0);
312 if (cl->codel_ != NULL)
313 qtype(cl->q_) = Q_CODEL;
318 * put the class into the class tree
322 if ((peer = ifd->active_[pri]) != NULL) {
323 /* find the last class at this pri */
325 while (peer->peer_ != ifd->active_[pri])
329 ifd->active_[pri] = cl;
334 cl->next_ = parent->children_;
335 parent->children_ = cl;
340 * Compute the depth of this class and its ancestors in the class
343 rmc_depth_compute(cl);
346 * If CBQ's WRR is enabled, then initialize the class WRR state.
350 ifd->alloc_[pri] += cl->allotment_;
351 rmc_wrr_set_weights(ifd);
353 IFQ_UNLOCK(ifd->ifq_);
359 rmc_modclass(struct rm_class *cl, u_int nsecPerByte, int maxq, u_int maxidle,
360 int minidle, u_int offtime, int pktsize)
362 struct rm_ifdat *ifd;
367 old_allotment = cl->allotment_;
371 cl->allotment_ = RM_NS_PER_SEC / nsecPerByte; /* Bytes per sec */
373 cl->ns_per_byte_ = nsecPerByte;
375 qlimit(cl->q_) = maxq;
377 #if 1 /* minidle is also scaled in ALTQ */
378 cl->minidle_ = (minidle * nsecPerByte) / 8;
379 if (cl->minidle_ > 0)
382 cl->minidle_ = minidle;
384 cl->maxidle_ = (maxidle * nsecPerByte) / 8;
385 if (cl->maxidle_ == 0)
387 #if 1 /* offtime is also scaled in ALTQ */
388 cl->avgidle_ = cl->maxidle_;
389 cl->offtime_ = ((offtime * nsecPerByte) / 8) >> RM_FILTER_GAIN;
390 if (cl->offtime_ == 0)
394 cl->offtime_ = (offtime * nsecPerByte) / 8;
398 * If CBQ's WRR is enabled, then initialize the class WRR state.
401 ifd->alloc_[cl->pri_] += cl->allotment_ - old_allotment;
402 rmc_wrr_set_weights(ifd);
404 IFQ_UNLOCK(ifd->ifq_);
411 * rmc_wrr_set_weights(struct rm_ifdat *ifdat) - This function computes
412 * the appropriate run robin weights for the CBQ weighted round robin
419 rmc_wrr_set_weights(struct rm_ifdat *ifd)
422 struct rm_class *cl, *clh;
424 for (i = 0; i < RM_MAXPRIO; i++) {
426 * This is inverted from that of the simulator to
427 * maintain precision.
429 if (ifd->num_[i] == 0)
432 ifd->M_[i] = ifd->alloc_[i] /
433 (ifd->num_[i] * ifd->maxpkt_);
435 * Compute the weighted allotment for each class.
436 * This takes the expensive div instruction out
437 * of the main loop for the wrr scheduling path.
438 * These only get recomputed when a class comes or
441 if (ifd->active_[i] != NULL) {
442 clh = cl = ifd->active_[i];
444 /* safe-guard for slow link or alloc_ == 0 */
446 cl->w_allotment_ = 0;
448 cl->w_allotment_ = cl->allotment_ /
451 } while ((cl != NULL) && (cl != clh));
457 rmc_get_weight(struct rm_ifdat *ifd, int pri)
459 if ((pri >= 0) && (pri < RM_MAXPRIO))
460 return (ifd->M_[pri]);
467 * rmc_depth_compute(struct rm_class *cl) - This function computes the
468 * appropriate depth of class 'cl' and its ancestors.
474 rmc_depth_compute(struct rm_class *cl)
476 rm_class_t *t = cl, *p;
479 * Recompute the depth for the branch of the tree.
483 if (p && (t->depth_ >= p->depth_)) {
484 p->depth_ = t->depth_ + 1;
493 * rmc_depth_recompute(struct rm_class *cl) - This function re-computes
494 * the depth of the tree after a class has been deleted.
500 rmc_depth_recompute(rm_class_t *cl)
507 if ((t = p->children_) == NULL) {
513 if (t->depth_ > cdepth)
518 if (p->depth_ == cdepth + 1)
519 /* no change to this parent */
522 p->depth_ = cdepth + 1;
530 if (cl->depth_ >= 1) {
531 if (cl->children_ == NULL) {
533 } else if ((t = cl->children_) != NULL) {
535 if (t->children_ != NULL)
536 rmc_depth_recompute(t);
540 rmc_depth_compute(cl);
547 * rmc_delete_class(struct rm_ifdat *ifdat, struct rm_class *cl) - This
548 * function deletes a class from the link-sharing structure and frees
549 * all resources associated with the class.
555 rmc_delete_class(struct rm_ifdat *ifd, struct rm_class *cl)
557 struct rm_class *p, *head, *previous;
560 ASSERT(cl->children_ == NULL);
563 CALLOUT_STOP(&cl->callout_);
568 * Free packets in the packet queue.
569 * XXX - this may not be a desired behavior. Packets should be
575 * If the class has a parent, then remove the class from the
576 * class from the parent's children chain.
578 if (cl->parent_ != NULL) {
579 head = cl->parent_->children_;
581 if (head->next_ == NULL) {
583 cl->parent_->children_ = NULL;
584 cl->parent_->leaf_ = 1;
585 } else while (p != NULL) {
588 cl->parent_->children_ = cl->next_;
590 previous->next_ = cl->next_;
601 * Delete class from class priority peer list.
603 if ((p = ifd->active_[cl->pri_]) != NULL) {
605 * If there is more than one member of this priority
606 * level, then look for class(cl) in the priority level.
609 while (p->peer_ != cl)
611 p->peer_ = cl->peer_;
613 if (ifd->active_[cl->pri_] == cl)
614 ifd->active_[cl->pri_] = cl->peer_;
617 ifd->active_[cl->pri_] = NULL;
622 * Recompute the WRR weights.
625 ifd->alloc_[cl->pri_] -= cl->allotment_;
626 ifd->num_[cl->pri_]--;
627 rmc_wrr_set_weights(ifd);
631 * Re-compute the depth of the tree.
634 rmc_depth_recompute(cl->parent_);
636 rmc_depth_recompute(ifd->root_);
639 IFQ_UNLOCK(ifd->ifq_);
643 * Free the class structure.
645 if (cl->red_ != NULL) {
647 if (q_is_rio(cl->q_))
648 rio_destroy((rio_t *)cl->red_);
651 if (q_is_red(cl->q_))
652 red_destroy(cl->red_);
655 if (q_is_codel(cl->q_))
656 codel_destroy(cl->codel_);
659 free(cl->q_, M_DEVBUF);
666 * rmc_init(...) - Initialize the resource management data structures
667 * associated with the output portion of interface 'ifp'. 'ifd' is
668 * where the structures will be built (for backwards compatibility, the
669 * structures aren't kept in the ifnet struct). 'nsecPerByte'
670 * gives the link speed (inverse of bandwidth) in nanoseconds/byte.
671 * 'restart' is the driver-specific routine that the generic 'delay
672 * until under limit' action will call to restart output. `maxq'
673 * is the queue size of the 'link' & 'default' classes. 'maxqueued'
674 * is the maximum number of packets that the resource management
675 * code will allow to be queued 'downstream' (this is typically 1).
681 rmc_init(struct ifaltq *ifq, struct rm_ifdat *ifd, u_int nsecPerByte,
682 void (*restart)(struct ifaltq *), int maxq, int maxqueued, u_int maxidle,
683 int minidle, u_int offtime, int flags)
688 * Initialize the CBQ tracing/debug facility.
692 bzero((char *)ifd, sizeof (*ifd));
693 mtu = ifq->altq_ifp->if_mtu;
695 ifd->restart = restart;
696 ifd->maxqueued_ = maxqueued;
697 ifd->ns_per_byte_ = nsecPerByte;
699 ifd->wrr_ = (flags & RMCF_WRR) ? 1 : 0;
700 ifd->efficient_ = (flags & RMCF_EFFICIENT) ? 1 : 0;
702 ifd->maxiftime_ = mtu * nsecPerByte / 1000 * 16;
703 if (mtu * nsecPerByte > 10 * 1000000)
704 ifd->maxiftime_ /= 4;
708 CBQTRACE(rmc_init, 'INIT', ifd->cutoff_);
711 * Initialize the CBQ's WRR state.
713 for (i = 0; i < RM_MAXPRIO; i++) {
718 ifd->active_[i] = NULL;
722 * Initialize current packet state.
726 for (i = 0; i < RM_MAXQUEUED; i++) {
727 ifd->class_[i] = NULL;
729 ifd->borrowed_[i] = NULL;
733 * Create the root class of the link-sharing structure.
735 if ((ifd->root_ = rmc_newclass(0, ifd,
737 rmc_root_overlimit, maxq, 0, 0,
738 maxidle, minidle, offtime,
740 printf("rmc_init: root class not allocated\n");
743 ifd->root_->depth_ = 0;
748 * rmc_queue_packet(struct rm_class *cl, mbuf_t *m) - Add packet given by
749 * mbuf 'm' to queue for resource class 'cl'. This routine is called
750 * by a driver's if_output routine. This routine must be called with
751 * output packet completion interrupts locked out (to avoid racing with
754 * Returns: 0 on successful queueing
755 * -1 when packet drop occurs
758 rmc_queue_packet(struct rm_class *cl, mbuf_t *m)
761 struct rm_ifdat *ifd = cl->ifdat_;
763 int is_empty = qempty(cl->q_);
766 if (ifd->cutoff_ > 0) {
767 if (TV_LT(&cl->undertime_, &now)) {
768 if (ifd->cutoff_ > cl->depth_)
769 ifd->cutoff_ = cl->depth_;
770 CBQTRACE(rmc_queue_packet, 'ffoc', cl->depth_);
775 * the class is overlimit. if the class has
776 * underlimit ancestors, set cutoff to the lowest
779 struct rm_class *borrow = cl->borrow_;
781 while (borrow != NULL &&
782 borrow->depth_ < ifd->cutoff_) {
783 if (TV_LT(&borrow->undertime_, &now)) {
784 ifd->cutoff_ = borrow->depth_;
785 CBQTRACE(rmc_queue_packet, 'ffob', ifd->cutoff_);
788 borrow = borrow->borrow_;
792 else if ((ifd->cutoff_ > 1) && cl->borrow_) {
793 if (TV_LT(&cl->borrow_->undertime_, &now)) {
794 ifd->cutoff_ = cl->borrow_->depth_;
795 CBQTRACE(rmc_queue_packet, 'ffob',
796 cl->borrow_->depth_);
802 if (_rmc_addq(cl, m) < 0)
807 CBQTRACE(rmc_queue_packet, 'ytpe', cl->stats_.handle);
811 if (qlen(cl->q_) > qlimit(cl->q_)) {
812 /* note: qlimit can be set to 0 or 1 */
821 * rmc_tl_satisfied(struct rm_ifdat *ifd, struct timeval *now) - Check all
822 * classes to see if there are satified.
826 rmc_tl_satisfied(struct rm_ifdat *ifd, struct timeval *now)
831 for (i = RM_MAXPRIO - 1; i >= 0; i--) {
832 if ((bp = ifd->active_[i]) != NULL) {
835 if (!rmc_satisfied(p, now)) {
836 ifd->cutoff_ = p->depth_;
848 * rmc_satisfied - Return 1 of the class is satisfied. O, otherwise.
852 rmc_satisfied(struct rm_class *cl, struct timeval *now)
858 if (TV_LT(now, &cl->undertime_))
860 if (cl->depth_ == 0) {
861 if (!cl->sleeping_ && (qlen(cl->q_) > cl->qthresh_))
866 if (cl->children_ != NULL) {
869 if (!rmc_satisfied(p, now))
879 * Return 1 if class 'cl' is under limit or can borrow from a parent,
880 * 0 if overlimit. As a side-effect, this routine will invoke the
881 * class overlimit action if the class if overlimit.
885 rmc_under_limit(struct rm_class *cl, struct timeval *now)
889 struct rm_ifdat *ifd = cl->ifdat_;
891 ifd->borrowed_[ifd->qi_] = NULL;
893 * If cl is the root class, then always return that it is
894 * underlimit. Otherwise, check to see if the class is underlimit.
896 if (cl->parent_ == NULL)
900 if (TV_LT(now, &cl->undertime_))
903 CALLOUT_STOP(&cl->callout_);
905 cl->undertime_.tv_sec = 0;
910 while (cl->undertime_.tv_sec && TV_LT(now, &cl->undertime_)) {
911 if (((cl = cl->borrow_) == NULL) ||
912 (cl->depth_ > ifd->cutoff_)) {
915 /* cutoff is taking effect, just
916 return false without calling
920 #ifdef BORROW_OFFTIME
922 * check if the class can borrow offtime too.
923 * borrow offtime from the top of the borrow
924 * chain if the top class is not overloaded.
927 /* cutoff is taking effect, use this class as top. */
929 CBQTRACE(rmc_under_limit, 'ffou', ifd->cutoff_);
931 if (top != NULL && top->avgidle_ == top->minidle_)
934 (p->overlimit)(p, top);
937 (p->overlimit)(p, NULL);
945 ifd->borrowed_[ifd->qi_] = cl;
950 * _rmc_wrr_dequeue_next() - This is scheduler for WRR as opposed to
951 * Packet-by-packet round robin.
953 * The heart of the weighted round-robin scheduler, which decides which
954 * class next gets to send a packet. Highest priority first, then
955 * weighted round-robin within priorites.
957 * Each able-to-send class gets to send until its byte allocation is
958 * exhausted. Thus, the active pointer is only changed after a class has
959 * exhausted its allocation.
961 * If the scheduler finds no class that is underlimit or able to borrow,
962 * then the first class found that had a nonzero queue and is allowed to
963 * borrow gets to send.
967 _rmc_wrr_dequeue_next(struct rm_ifdat *ifd, int op)
969 struct rm_class *cl = NULL, *first = NULL;
978 * if the driver polls the top of the queue and then removes
979 * the polled packet, we must return the same packet.
981 if (op == ALTDQ_REMOVE && ifd->pollcache_) {
982 cl = ifd->pollcache_;
984 if (ifd->efficient_) {
985 /* check if this class is overlimit */
986 if (cl->undertime_.tv_sec != 0 &&
987 rmc_under_limit(cl, &now) == 0)
990 ifd->pollcache_ = NULL;
994 /* mode == ALTDQ_POLL || pollcache == NULL */
995 ifd->pollcache_ = NULL;
996 ifd->borrowed_[ifd->qi_] = NULL;
1001 for (cpri = RM_MAXPRIO - 1; cpri >= 0; cpri--) {
1002 if (ifd->na_[cpri] == 0)
1006 * Loop through twice for a priority level, if some class
1007 * was unable to send a packet the first round because
1008 * of the weighted round-robin mechanism.
1009 * During the second loop at this level, deficit==2.
1010 * (This second loop is not needed if for every class,
1011 * "M[cl->pri_])" times "cl->allotment" is greater than
1012 * the byte size for the largest packet in the class.)
1015 cl = ifd->active_[cpri];
1018 if ((deficit < 2) && (cl->bytes_alloc_ <= 0))
1019 cl->bytes_alloc_ += cl->w_allotment_;
1020 if (!qempty(cl->q_)) {
1021 if ((cl->undertime_.tv_sec == 0) ||
1022 rmc_under_limit(cl, &now)) {
1023 if (cl->bytes_alloc_ > 0 || deficit > 1)
1026 /* underlimit but no alloc */
1029 ifd->borrowed_[ifd->qi_] = NULL;
1032 else if (first == NULL && cl->borrow_ != NULL)
1033 first = cl; /* borrowing candidate */
1036 cl->bytes_alloc_ = 0;
1038 } while (cl != ifd->active_[cpri]);
1041 /* first loop found an underlimit class with deficit */
1042 /* Loop on same priority level, with new deficit. */
1048 #ifdef ADJUST_CUTOFF
1050 * no underlimit class found. if cutoff is taking effect,
1051 * increase cutoff and try again.
1053 if (first != NULL && ifd->cutoff_ < ifd->root_->depth_) {
1055 CBQTRACE(_rmc_wrr_dequeue_next, 'ojda', ifd->cutoff_);
1058 #endif /* ADJUST_CUTOFF */
1060 * If LINK_EFFICIENCY is turned on, then the first overlimit
1061 * class we encounter will send a packet if all the classes
1062 * of the link-sharing structure are overlimit.
1065 CBQTRACE(_rmc_wrr_dequeue_next, 'otsr', ifd->cutoff_);
1067 if (!ifd->efficient_ || first == NULL)
1072 #if 0 /* too time-consuming for nothing */
1074 CALLOUT_STOP(&cl->callout_);
1076 cl->undertime_.tv_sec = 0;
1078 ifd->borrowed_[ifd->qi_] = cl->borrow_;
1079 ifd->cutoff_ = cl->borrow_->depth_;
1082 * Deque the packet and do the book keeping...
1085 if (op == ALTDQ_REMOVE) {
1088 panic("_rmc_wrr_dequeue_next");
1093 * Update class statistics and link data.
1095 if (cl->bytes_alloc_ > 0)
1096 cl->bytes_alloc_ -= m_pktlen(m);
1098 if ((cl->bytes_alloc_ <= 0) || first == cl)
1099 ifd->active_[cl->pri_] = cl->peer_;
1101 ifd->active_[cl->pri_] = cl;
1103 ifd->class_[ifd->qi_] = cl;
1104 ifd->curlen_[ifd->qi_] = m_pktlen(m);
1105 ifd->now_[ifd->qi_] = now;
1106 ifd->qi_ = (ifd->qi_ + 1) % ifd->maxqueued_;
1109 /* mode == ALTDQ_PPOLL */
1111 ifd->pollcache_ = cl;
1117 * Dequeue & return next packet from the highest priority class that
1118 * has a packet to send & has enough allocation to send it. This
1119 * routine is called by a driver whenever it needs a new packet to
1123 _rmc_prr_dequeue_next(struct rm_ifdat *ifd, int op)
1127 struct rm_class *cl, *first = NULL;
1133 * if the driver polls the top of the queue and then removes
1134 * the polled packet, we must return the same packet.
1136 if (op == ALTDQ_REMOVE && ifd->pollcache_) {
1137 cl = ifd->pollcache_;
1139 ifd->pollcache_ = NULL;
1142 /* mode == ALTDQ_POLL || pollcache == NULL */
1143 ifd->pollcache_ = NULL;
1144 ifd->borrowed_[ifd->qi_] = NULL;
1146 #ifdef ADJUST_CUTOFF
1149 for (cpri = RM_MAXPRIO - 1; cpri >= 0; cpri--) {
1150 if (ifd->na_[cpri] == 0)
1152 cl = ifd->active_[cpri];
1155 if (!qempty(cl->q_)) {
1156 if ((cl->undertime_.tv_sec == 0) ||
1157 rmc_under_limit(cl, &now))
1159 if (first == NULL && cl->borrow_ != NULL)
1163 } while (cl != ifd->active_[cpri]);
1166 #ifdef ADJUST_CUTOFF
1168 * no underlimit class found. if cutoff is taking effect, increase
1169 * cutoff and try again.
1171 if (first != NULL && ifd->cutoff_ < ifd->root_->depth_) {
1175 #endif /* ADJUST_CUTOFF */
1177 * If LINK_EFFICIENCY is turned on, then the first overlimit
1178 * class we encounter will send a packet if all the classes
1179 * of the link-sharing structure are overlimit.
1182 if (!ifd->efficient_ || first == NULL)
1187 #if 0 /* too time-consuming for nothing */
1189 CALLOUT_STOP(&cl->callout_);
1191 cl->undertime_.tv_sec = 0;
1193 ifd->borrowed_[ifd->qi_] = cl->borrow_;
1194 ifd->cutoff_ = cl->borrow_->depth_;
1197 * Deque the packet and do the book keeping...
1200 if (op == ALTDQ_REMOVE) {
1203 panic("_rmc_prr_dequeue_next");
1207 ifd->active_[cpri] = cl->peer_;
1209 ifd->class_[ifd->qi_] = cl;
1210 ifd->curlen_[ifd->qi_] = m_pktlen(m);
1211 ifd->now_[ifd->qi_] = now;
1212 ifd->qi_ = (ifd->qi_ + 1) % ifd->maxqueued_;
1215 /* mode == ALTDQ_POLL */
1217 ifd->pollcache_ = cl;
1224 * rmc_dequeue_next(struct rm_ifdat *ifd, struct timeval *now) - this function
1225 * is invoked by the packet driver to get the next packet to be
1226 * dequeued and output on the link. If WRR is enabled, then the
1227 * WRR dequeue next routine will determine the next packet to sent.
1228 * Otherwise, packet-by-packet round robin is invoked.
1230 * Returns: NULL, if a packet is not available or if all
1231 * classes are overlimit.
1233 * Otherwise, Pointer to the next packet.
1237 rmc_dequeue_next(struct rm_ifdat *ifd, int mode)
1239 if (ifd->queued_ >= ifd->maxqueued_)
1242 return (_rmc_wrr_dequeue_next(ifd, mode));
1244 return (_rmc_prr_dequeue_next(ifd, mode));
1248 * Update the utilization estimate for the packet that just completed.
1249 * The packet's class & the parent(s) of that class all get their
1250 * estimators updated. This routine is called by the driver's output-
1251 * packet-completion interrupt service routine.
1255 * a macro to approximate "divide by 1000" that gives 0.000999,
1256 * if a value has enough effective digits.
1257 * (on pentium, mul takes 9 cycles but div takes 46!)
1259 #define NSEC_TO_USEC(t) (((t) >> 10) + ((t) >> 16) + ((t) >> 17))
1261 rmc_update_class_util(struct rm_ifdat *ifd)
1263 int idle, avgidle, pktlen;
1264 int pkt_time, tidle;
1265 rm_class_t *cl, *borrowed;
1266 rm_class_t *borrows;
1267 struct timeval *nowp;
1270 * Get the most recent completed class.
1272 if ((cl = ifd->class_[ifd->qo_]) == NULL)
1275 pktlen = ifd->curlen_[ifd->qo_];
1276 borrowed = ifd->borrowed_[ifd->qo_];
1279 PKTCNTR_ADD(&cl->stats_.xmit_cnt, pktlen);
1282 * Run estimator on class and its ancestors.
1285 * rm_update_class_util is designed to be called when the
1286 * transfer is completed from a xmit complete interrupt,
1287 * but most drivers don't implement an upcall for that.
1288 * so, just use estimated completion time.
1289 * as a result, ifd->qi_ and ifd->qo_ are always synced.
1291 nowp = &ifd->now_[ifd->qo_];
1292 /* get pkt_time (for link) in usec */
1293 #if 1 /* use approximation */
1294 pkt_time = ifd->curlen_[ifd->qo_] * ifd->ns_per_byte_;
1295 pkt_time = NSEC_TO_USEC(pkt_time);
1297 pkt_time = ifd->curlen_[ifd->qo_] * ifd->ns_per_byte_ / 1000;
1299 #if 1 /* ALTQ4PPP */
1300 if (TV_LT(nowp, &ifd->ifnow_)) {
1304 * make sure the estimated completion time does not go
1305 * too far. it can happen when the link layer supports
1306 * data compression or the interface speed is set to
1307 * a much lower value.
1309 TV_DELTA(&ifd->ifnow_, nowp, iftime);
1310 if (iftime+pkt_time < ifd->maxiftime_) {
1311 TV_ADD_DELTA(&ifd->ifnow_, pkt_time, &ifd->ifnow_);
1313 TV_ADD_DELTA(nowp, ifd->maxiftime_, &ifd->ifnow_);
1316 TV_ADD_DELTA(nowp, pkt_time, &ifd->ifnow_);
1319 if (TV_LT(nowp, &ifd->ifnow_)) {
1320 TV_ADD_DELTA(&ifd->ifnow_, pkt_time, &ifd->ifnow_);
1322 TV_ADD_DELTA(nowp, pkt_time, &ifd->ifnow_);
1326 while (cl != NULL) {
1327 TV_DELTA(&ifd->ifnow_, &cl->last_, idle);
1328 if (idle >= 2000000)
1330 * this class is idle enough, reset avgidle.
1331 * (TV_DELTA returns 2000000 us when delta is large.)
1333 cl->avgidle_ = cl->maxidle_;
1335 /* get pkt_time (for class) in usec */
1336 #if 1 /* use approximation */
1337 pkt_time = pktlen * cl->ns_per_byte_;
1338 pkt_time = NSEC_TO_USEC(pkt_time);
1340 pkt_time = pktlen * cl->ns_per_byte_ / 1000;
1344 avgidle = cl->avgidle_;
1345 avgidle += idle - (avgidle >> RM_FILTER_GAIN);
1346 cl->avgidle_ = avgidle;
1348 /* Are we overlimit ? */
1350 CBQTRACE(rmc_update_class_util, 'milo', cl->stats_.handle);
1353 * need some lower bound for avgidle, otherwise
1354 * a borrowing class gets unbounded penalty.
1356 if (avgidle < cl->minidle_)
1357 avgidle = cl->avgidle_ = cl->minidle_;
1359 /* set next idle to make avgidle 0 */
1361 (((1 - RM_POWER) * avgidle) >> RM_FILTER_GAIN);
1362 TV_ADD_DELTA(nowp, tidle, &cl->undertime_);
1366 (avgidle > cl->maxidle_) ? cl->maxidle_ : avgidle;
1367 cl->undertime_.tv_sec = 0;
1368 if (cl->sleeping_) {
1369 CALLOUT_STOP(&cl->callout_);
1374 if (borrows != NULL) {
1376 ++cl->stats_.borrows;
1380 cl->last_ = ifd->ifnow_;
1381 cl->last_pkttime_ = pkt_time;
1384 if (cl->parent_ == NULL) {
1385 /* take stats of root class */
1386 PKTCNTR_ADD(&cl->stats_.xmit_cnt, pktlen);
1394 * Check to see if cutoff needs to set to a new level.
1396 cl = ifd->class_[ifd->qo_];
1397 if (borrowed && (ifd->cutoff_ >= borrowed->depth_)) {
1399 if ((qlen(cl->q_) <= 0) || TV_LT(nowp, &borrowed->undertime_)) {
1400 rmc_tl_satisfied(ifd, nowp);
1401 CBQTRACE(rmc_update_class_util, 'broe', ifd->cutoff_);
1403 ifd->cutoff_ = borrowed->depth_;
1404 CBQTRACE(rmc_update_class_util, 'ffob', borrowed->depth_);
1407 if ((qlen(cl->q_) <= 1) || TV_LT(&now, &borrowed->undertime_)) {
1410 rmc_tl_satisfied(ifd, &now);
1412 CBQTRACE(rmc_update_class_util, 'broe', ifd->cutoff_);
1414 ifd->cutoff_ = borrowed->depth_;
1415 CBQTRACE(rmc_update_class_util, 'ffob', borrowed->depth_);
1421 * Release class slot
1423 ifd->borrowed_[ifd->qo_] = NULL;
1424 ifd->class_[ifd->qo_] = NULL;
1425 ifd->qo_ = (ifd->qo_ + 1) % ifd->maxqueued_;
1431 * rmc_drop_action(struct rm_class *cl) - Generic (not protocol-specific)
1432 * over-limit action routines. These get invoked by rmc_under_limit()
1433 * if a class with packets to send if over its bandwidth limit & can't
1434 * borrow from a parent class.
1440 rmc_drop_action(struct rm_class *cl)
1442 struct rm_ifdat *ifd = cl->ifdat_;
1444 ASSERT(qlen(cl->q_) > 0);
1447 ifd->na_[cl->pri_]--;
1450 void rmc_dropall(struct rm_class *cl)
1452 struct rm_ifdat *ifd = cl->ifdat_;
1454 if (!qempty(cl->q_)) {
1457 ifd->na_[cl->pri_]--;
1461 #if (__FreeBSD_version > 300000)
1462 /* hzto() is removed from FreeBSD-3.0 */
1463 static int hzto(struct timeval *);
1472 t2.tv_sec = tv->tv_sec - t2.tv_sec;
1473 t2.tv_usec = tv->tv_usec - t2.tv_usec;
1474 return (tvtohz(&t2));
1476 #endif /* __FreeBSD_version > 300000 */
1480 * rmc_delay_action(struct rm_class *cl) - This function is the generic CBQ
1481 * delay action routine. It is invoked via rmc_under_limit when the
1482 * packet is discoverd to be overlimit.
1484 * If the delay action is result of borrow class being overlimit, then
1485 * delay for the offtime of the borrowing class that is overlimit.
1491 rmc_delay_action(struct rm_class *cl, struct rm_class *borrow)
1493 int delay, t, extradelay;
1495 cl->stats_.overactions++;
1496 TV_DELTA(&cl->undertime_, &cl->overtime_, delay);
1497 #ifndef BORROW_OFFTIME
1498 delay += cl->offtime_;
1501 if (!cl->sleeping_) {
1502 CBQTRACE(rmc_delay_action, 'yled', cl->stats_.handle);
1503 #ifdef BORROW_OFFTIME
1505 extradelay = borrow->offtime_;
1508 extradelay = cl->offtime_;
1512 * XXX recalculate suspend time:
1513 * current undertime is (tidle + pkt_time) calculated
1514 * from the last transmission.
1515 * tidle: time required to bring avgidle back to 0
1516 * pkt_time: target waiting time for this class
1517 * we need to replace pkt_time by offtime
1519 extradelay -= cl->last_pkttime_;
1521 if (extradelay > 0) {
1522 TV_ADD_DELTA(&cl->undertime_, extradelay, &cl->undertime_);
1523 delay += extradelay;
1527 cl->stats_.delays++;
1530 * Since packets are phased randomly with respect to the
1531 * clock, 1 tick (the next clock tick) can be an arbitrarily
1532 * short time so we have to wait for at least two ticks.
1533 * NOTE: If there's no other traffic, we need the timer as
1534 * a 'backstop' to restart this class.
1536 if (delay > tick * 2) {
1537 /* FreeBSD rounds up the tick */
1538 t = hzto(&cl->undertime_);
1541 CALLOUT_RESET(&cl->callout_, t,
1542 (timeout_t *)rmc_restart, (caddr_t)cl);
1548 * rmc_restart() - is just a helper routine for rmc_delay_action -- it is
1549 * called by the system timer code & is responsible checking if the
1550 * class is still sleeping (it might have been restarted as a side
1551 * effect of the queue scan on a packet arrival) and, if so, restarting
1552 * output for the class. Inspecting the class state & restarting output
1553 * require locking the class structure. In general the driver is
1554 * responsible for locking but this is the only routine that is not
1555 * called directly or indirectly from the interface driver so it has
1556 * know about system locking conventions. Under bsd, locking is done
1557 * by raising IPL to splimp so that's what's implemented here. On a
1558 * different system this would probably need to be changed.
1564 rmc_restart(struct rm_class *cl)
1566 struct rm_ifdat *ifd = cl->ifdat_;
1570 IFQ_LOCK(ifd->ifq_);
1571 if (cl->sleeping_) {
1573 cl->undertime_.tv_sec = 0;
1575 if (ifd->queued_ < ifd->maxqueued_ && ifd->restart != NULL) {
1576 CBQTRACE(rmc_restart, 'trts', cl->stats_.handle);
1577 (ifd->restart)(ifd->ifq_);
1580 IFQ_UNLOCK(ifd->ifq_);
1586 * rmc_root_overlimit(struct rm_class *cl) - This the generic overlimit
1587 * handling routine for the root class of the link sharing structure.
1593 rmc_root_overlimit(struct rm_class *cl, struct rm_class *borrow)
1595 panic("rmc_root_overlimit");
1599 * Packet Queue handling routines. Eventually, this is to localize the
1600 * effects on the code whether queues are red queues or droptail
1605 _rmc_addq(rm_class_t *cl, mbuf_t *m)
1608 if (q_is_rio(cl->q_))
1609 return rio_addq((rio_t *)cl->red_, cl->q_, m, cl->pktattr_);
1612 if (q_is_red(cl->q_))
1613 return red_addq(cl->red_, cl->q_, m, cl->pktattr_);
1614 #endif /* ALTQ_RED */
1616 if (q_is_codel(cl->q_))
1617 return codel_addq(cl->codel_, cl->q_, m);
1620 if (cl->flags_ & RMCF_CLEARDSCP)
1621 write_dsfield(m, cl->pktattr_, 0);
1627 /* note: _rmc_dropq is not called for red */
1629 _rmc_dropq(rm_class_t *cl)
1633 if ((m = _getq(cl->q_)) != NULL)
1638 _rmc_getq(rm_class_t *cl)
1641 if (q_is_rio(cl->q_))
1642 return rio_getq((rio_t *)cl->red_, cl->q_);
1645 if (q_is_red(cl->q_))
1646 return red_getq(cl->red_, cl->q_);
1649 if (q_is_codel(cl->q_))
1650 return codel_getq(cl->codel_, cl->q_);
1652 return _getq(cl->q_);
1656 _rmc_pollq(rm_class_t *cl)
1658 return qhead(cl->q_);
1663 struct cbqtrace cbqtrace_buffer[NCBQTRACE+1];
1664 struct cbqtrace *cbqtrace_ptr = NULL;
1668 * DDB hook to trace cbq events:
1669 * the last 1024 events are held in a circular buffer.
1670 * use "call cbqtrace_dump(N)" to display 20 events from Nth event.
1672 void cbqtrace_dump(int);
1673 static char *rmc_funcname(void *);
1675 static struct rmc_funcs {
1680 rmc_init, "rmc_init",
1681 rmc_queue_packet, "rmc_queue_packet",
1682 rmc_under_limit, "rmc_under_limit",
1683 rmc_update_class_util, "rmc_update_class_util",
1684 rmc_delay_action, "rmc_delay_action",
1685 rmc_restart, "rmc_restart",
1686 _rmc_wrr_dequeue_next, "_rmc_wrr_dequeue_next",
1690 static char *rmc_funcname(void *func)
1692 struct rmc_funcs *fp;
1694 for (fp = rmc_funcs; fp->func != NULL; fp++)
1695 if (fp->func == func)
1700 void cbqtrace_dump(int counter)
1705 counter = counter % NCBQTRACE;
1706 p = (int *)&cbqtrace_buffer[counter];
1708 for (i=0; i<20; i++) {
1709 printf("[0x%x] ", *p++);
1710 printf("%s: ", rmc_funcname((void *)*p++));
1712 printf("%c%c%c%c: ", cp[0], cp[1], cp[2], cp[3]);
1713 printf("%d\n",*p++);
1715 if (p >= (int *)&cbqtrace_buffer[NCBQTRACE])
1716 p = (int *)cbqtrace_buffer;
1719 #endif /* CBQ_TRACE */
1720 #endif /* ALTQ_CBQ */
1722 #if defined(ALTQ_CBQ) || defined(ALTQ_RED) || defined(ALTQ_RIO) || \
1723 defined(ALTQ_HFSC) || defined(ALTQ_PRIQ) || defined(ALTQ_CODEL)
1724 #if !defined(__GNUC__) || defined(ALTQ_DEBUG)
1727 _addq(class_queue_t *q, mbuf_t *m)
1731 if ((m0 = qtail(q)) != NULL)
1732 m->m_nextpkt = m0->m_nextpkt;
1741 _getq(class_queue_t *q)
1745 if ((m = qtail(q)) == NULL)
1747 if ((m0 = m->m_nextpkt) != m)
1748 m->m_nextpkt = m0->m_nextpkt;
1750 ASSERT(qlen(q) == 1);
1754 m0->m_nextpkt = NULL;
1758 /* drop a packet at the tail of the queue */
1760 _getq_tail(class_queue_t *q)
1762 mbuf_t *m, *m0, *prev;
1764 if ((m = m0 = qtail(q)) == NULL)
1770 prev->m_nextpkt = m->m_nextpkt;
1772 ASSERT(qlen(q) == 1);
1777 m->m_nextpkt = NULL;
1781 /* randomly select a packet in the queue */
1783 _getq_random(class_queue_t *q)
1788 if ((m = qtail(q)) == NULL)
1790 if (m->m_nextpkt == m) {
1791 ASSERT(qlen(q) == 1);
1794 struct mbuf *prev = NULL;
1796 n = arc4random() % qlen(q) + 1;
1797 for (i = 0; i < n; i++) {
1801 prev->m_nextpkt = m->m_nextpkt;
1806 m->m_nextpkt = NULL;
1811 _removeq(class_queue_t *q, mbuf_t *m)
1820 prev->m_nextpkt = m->m_nextpkt;
1823 else if (qtail(q) == m)
1829 _flushq(class_queue_t *q)
1833 while ((m = _getq(q)) != NULL)
1835 ASSERT(qlen(q) == 0);
1838 #endif /* !__GNUC__ || ALTQ_DEBUG */
1839 #endif /* ALTQ_CBQ || ALTQ_RED || ALTQ_RIO || ALTQ_HFSC || ALTQ_PRIQ */