1 /* $NetBSD: altq_rmclass.c,v 1.20 2007/03/04 05:59:02 christos Exp $ */
2 /* $KAME: altq_rmclass.c,v 1.19 2005/04/13 03:44:25 suz Exp $ */
5 * Copyright (c) 1991-1997 Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * 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 the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the Network Research
19 * Group at Lawrence Berkeley Laboratory.
20 * 4. Neither the name of the University nor of the Laboratory may be used
21 * to endorse or promote products derived from this software without
22 * specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * LBL code modified by speer@eng.sun.com, May 1977.
37 * For questions and/or comments, please send mail to cbq@ee.lbl.gov
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: altq_rmclass.c,v 1.20 2007/03/04 05:59:02 christos Exp $");
43 /* #ident "@(#)rm_class.c 1.48 97/12/05 SMI" */
50 #ifdef ALTQ_CBQ /* cbq is enabled by ALTQ_CBQ option in opt_altq.h */
52 #include <sys/param.h>
53 #include <sys/malloc.h>
55 #include <sys/socket.h>
56 #include <sys/systm.h>
57 #include <sys/errno.h>
60 #include <sys/kernel.h>
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/ip.h>
70 #include <altq/altq.h>
71 #include <altq/altq_rmclass.h>
72 #include <altq/altq_rmclass_debug.h>
73 #include <altq/altq_red.h>
74 #include <altq/altq_rio.h>
80 #define reset_cutoff(ifd) { ifd->cutoff_ = RM_MAXDEPTH; }
86 static int rmc_satisfied(struct rm_class
*, struct timeval
*);
87 static void rmc_wrr_set_weights(struct rm_ifdat
*);
88 static void rmc_depth_compute(struct rm_class
*);
89 static void rmc_depth_recompute(rm_class_t
*);
91 static mbuf_t
*_rmc_wrr_dequeue_next(struct rm_ifdat
*, int);
92 static mbuf_t
*_rmc_prr_dequeue_next(struct rm_ifdat
*, int);
94 static int _rmc_addq(rm_class_t
*, mbuf_t
*);
95 static void _rmc_dropq(rm_class_t
*);
96 static mbuf_t
*_rmc_getq(rm_class_t
*);
97 static mbuf_t
*_rmc_pollq(rm_class_t
*);
99 static int rmc_under_limit(struct rm_class
*, struct timeval
*);
100 static void rmc_tl_satisfied(struct rm_ifdat
*, struct timeval
*);
101 static void rmc_drop_action(struct rm_class
*);
102 static void rmc_restart(struct rm_class
*);
103 static void rmc_root_overlimit(struct rm_class
*, struct rm_class
*);
105 #define BORROW_OFFTIME
107 * BORROW_OFFTIME (experimental):
108 * borrow the offtime of the class borrowing from.
109 * the reason is that when its own offtime is set, the class is unable
110 * to borrow much, especially when cutoff is taking effect.
111 * but when the borrowed class is overloaded (advidle is close to minidle),
112 * use the borrowing class's offtime to avoid overload.
114 #define ADJUST_CUTOFF
116 * ADJUST_CUTOFF (experimental):
117 * if no underlimit class is found due to cutoff, increase cutoff and
118 * retry the scheduling loop.
119 * also, don't invoke delay_actions while cutoff is taking effect,
120 * since a sleeping class won't have a chance to be scheduled in the
123 * now heuristics for setting the top-level variable (cutoff_) becomes:
124 * 1. if a packet arrives for a not-overlimit class, set cutoff
125 * to the depth of the class.
126 * 2. if cutoff is i, and a packet arrives for an overlimit class
127 * with an underlimit ancestor at a lower level than i (say j),
128 * then set cutoff to j.
129 * 3. at scheduling a packet, if there is no underlimit class
130 * due to the current cutoff level, increase cutoff by 1 and
131 * then try to schedule again.
136 * rmc_newclass(...) - Create a new resource management class at priority
137 * 'pri' on the interface given by 'ifd'.
139 * nsecPerByte is the data rate of the interface in nanoseconds/byte.
140 * E.g., 800 for a 10Mb/s ethernet. If the class gets less
141 * than 100% of the bandwidth, this number should be the
142 * 'effective' rate for the class. Let f be the
143 * bandwidth fraction allocated to this class, and let
144 * nsPerByte be the data rate of the output link in
145 * nanoseconds/byte. Then nsecPerByte is set to
146 * nsPerByte / f. E.g., 1600 (= 800 / .5)
147 * for a class that gets 50% of an ethernet's bandwidth.
149 * action the routine to call when the class is over limit.
151 * maxq max allowable queue size for class (in packets).
153 * parent parent class pointer.
155 * borrow class to borrow from (should be either 'parent' or null).
157 * maxidle max value allowed for class 'idle' time estimate (this
158 * parameter determines how large an initial burst of packets
159 * can be before overlimit action is invoked.
161 * offtime how long 'delay' action will delay when class goes over
162 * limit (this parameter determines the steady-state burst
163 * size when a class is running over its limit).
165 * Maxidle and offtime have to be computed from the following: If the
166 * average packet size is s, the bandwidth fraction allocated to this
167 * class is f, we want to allow b packet bursts, and the gain of the
168 * averaging filter is g (= 1 - 2^(-RM_FILTER_GAIN)), then:
170 * ptime = s * nsPerByte * (1 - f) / f
171 * maxidle = ptime * (1 - g^b) / g^b
172 * minidle = -ptime * (1 / (f - 1))
173 * offtime = ptime * (1 + 1/(1 - g) * (1 - g^(b - 1)) / g^(b - 1)
175 * Operationally, it's convenient to specify maxidle & offtime in units
176 * independent of the link bandwidth so the maxidle & offtime passed to
177 * this routine are the above values multiplied by 8*f/(1000*nsPerByte).
178 * (The constant factor is a scale factor needed to make the parameters
179 * integers. This scaling also means that the 'unscaled' values of
180 * maxidle*nsecPerByte/8 and offtime*nsecPerByte/8 will be in microseconds,
181 * not nanoseconds.) Also note that the 'idle' filter computation keeps
182 * an estimate scaled upward by 2^RM_FILTER_GAIN so the passed value of
183 * maxidle also must be scaled upward by this value. Thus, the passed
184 * values for maxidle and offtime can be computed as follows:
186 * maxidle = maxidle * 2^RM_FILTER_GAIN * 8 / (1000 * nsecPerByte)
187 * offtime = offtime * 8 / (1000 * nsecPerByte)
189 * When USE_HRTIME is employed, then maxidle and offtime become:
190 * maxidle = maxilde * (8.0 / nsecPerByte);
191 * offtime = offtime * (8.0 / nsecPerByte);
194 rmc_newclass(int pri
, struct rm_ifdat
*ifd
, u_int nsecPerByte
,
195 void (*action
)(rm_class_t
*, rm_class_t
*), int maxq
,
196 struct rm_class
*parent
, struct rm_class
*borrow
, u_int maxidle
,
197 int minidle
, u_int offtime
, int pktsize
, int flags
)
200 struct rm_class
*peer
;
203 if (pri
>= RM_MAXPRIO
)
206 if (flags
& RMCF_RED
) {
208 printf("rmc_newclass: RED not configured for CBQ!\n");
214 if (flags
& RMCF_RIO
) {
216 printf("rmc_newclass: RIO not configured for CBQ!\n");
222 cl
= malloc(sizeof(struct rm_class
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
225 CALLOUT_INIT(&cl
->callout_
);
227 cl
->q_
= malloc(sizeof(class_queue_t
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
228 if (cl
->q_
== NULL
) {
234 * Class initialization.
236 cl
->children_
= NULL
;
237 cl
->parent_
= parent
;
238 cl
->borrow_
= borrow
;
242 cl
->allotment_
= RM_NS_PER_SEC
/ nsecPerByte
; /* Bytes per sec */
245 cl
->ns_per_byte_
= nsecPerByte
;
247 qlimit(cl
->q_
) = maxq
;
248 qtype(cl
->q_
) = Q_DROPHEAD
;
252 #if 1 /* minidle is also scaled in ALTQ */
253 cl
->minidle_
= (minidle
* (int)nsecPerByte
) / 8;
254 if (cl
->minidle_
> 0)
257 cl
->minidle_
= minidle
;
259 cl
->maxidle_
= (maxidle
* nsecPerByte
) / 8;
260 if (cl
->maxidle_
== 0)
262 #if 1 /* offtime is also scaled in ALTQ */
263 cl
->avgidle_
= cl
->maxidle_
;
264 cl
->offtime_
= ((offtime
* nsecPerByte
) / 8) >> RM_FILTER_GAIN
;
265 if (cl
->offtime_
== 0)
269 cl
->offtime_
= (offtime
* nsecPerByte
) / 8;
271 cl
->overlimit
= action
;
274 if (flags
& (RMCF_RED
|RMCF_RIO
)) {
275 int red_flags
, red_pkttime
;
278 if (flags
& RMCF_ECN
)
279 red_flags
|= REDF_ECN
;
280 if (flags
& RMCF_FLOWVALVE
)
281 red_flags
|= REDF_FLOWVALVE
;
283 if (flags
& RMCF_CLEARDSCP
)
284 red_flags
|= RIOF_CLEARDSCP
;
286 red_pkttime
= nsecPerByte
* pktsize
/ 1000;
288 if (flags
& RMCF_RED
) {
289 cl
->red_
= red_alloc(0, 0,
290 qlimit(cl
->q_
) * 10/100,
291 qlimit(cl
->q_
) * 30/100,
292 red_flags
, red_pkttime
);
293 if (cl
->red_
!= NULL
)
294 qtype(cl
->q_
) = Q_RED
;
298 cl
->red_
= (red_t
*)rio_alloc(0, NULL
,
299 red_flags
, red_pkttime
);
300 if (cl
->red_
!= NULL
)
301 qtype(cl
->q_
) = Q_RIO
;
305 #endif /* ALTQ_RED */
308 * put the class into the class tree
311 if ((peer
= ifd
->active_
[pri
]) != NULL
) {
312 /* find the last class at this pri */
314 while (peer
->peer_
!= ifd
->active_
[pri
])
318 ifd
->active_
[pri
] = cl
;
323 cl
->next_
= parent
->children_
;
324 parent
->children_
= cl
;
329 * Compute the depth of this class and its ancestors in the class
332 rmc_depth_compute(cl
);
335 * If CBQ's WRR is enabled, then initialize the class WRR state.
339 ifd
->alloc_
[pri
] += cl
->allotment_
;
340 rmc_wrr_set_weights(ifd
);
347 rmc_modclass(struct rm_class
*cl
, u_int nsecPerByte
, int maxq
, u_int maxidle
,
348 int minidle
, u_int offtime
, int pktsize
)
350 struct rm_ifdat
*ifd
;
355 old_allotment
= cl
->allotment_
;
358 cl
->allotment_
= RM_NS_PER_SEC
/ nsecPerByte
; /* Bytes per sec */
360 cl
->ns_per_byte_
= nsecPerByte
;
362 qlimit(cl
->q_
) = maxq
;
364 #if 1 /* minidle is also scaled in ALTQ */
365 cl
->minidle_
= (minidle
* nsecPerByte
) / 8;
366 if (cl
->minidle_
> 0)
369 cl
->minidle_
= minidle
;
371 cl
->maxidle_
= (maxidle
* nsecPerByte
) / 8;
372 if (cl
->maxidle_
== 0)
374 #if 1 /* offtime is also scaled in ALTQ */
375 cl
->avgidle_
= cl
->maxidle_
;
376 cl
->offtime_
= ((offtime
* nsecPerByte
) / 8) >> RM_FILTER_GAIN
;
377 if (cl
->offtime_
== 0)
381 cl
->offtime_
= (offtime
* nsecPerByte
) / 8;
385 * If CBQ's WRR is enabled, then initialize the class WRR state.
388 ifd
->alloc_
[cl
->pri_
] += cl
->allotment_
- old_allotment
;
389 rmc_wrr_set_weights(ifd
);
397 * rmc_wrr_set_weights(struct rm_ifdat *ifdat) - This function computes
398 * the appropriate run robin weights for the CBQ weighted round robin
405 rmc_wrr_set_weights(struct rm_ifdat
*ifd
)
408 struct rm_class
*cl
, *clh
;
410 for (i
= 0; i
< RM_MAXPRIO
; i
++) {
412 * This is inverted from that of the simulator to
413 * maintain precision.
415 if (ifd
->num_
[i
] == 0)
418 ifd
->M_
[i
] = ifd
->alloc_
[i
] /
419 (ifd
->num_
[i
] * ifd
->maxpkt_
);
421 * Compute the weighted allotment for each class.
422 * This takes the expensive div instruction out
423 * of the main loop for the wrr scheduling path.
424 * These only get recomputed when a class comes or
427 if (ifd
->active_
[i
] != NULL
) {
428 clh
= cl
= ifd
->active_
[i
];
430 /* safe-guard for slow link or alloc_ == 0 */
432 cl
->w_allotment_
= 0;
434 cl
->w_allotment_
= cl
->allotment_
/
437 } while ((cl
!= NULL
) && (cl
!= clh
));
443 rmc_get_weight(struct rm_ifdat
*ifd
, int pri
)
445 if ((pri
>= 0) && (pri
< RM_MAXPRIO
))
446 return (ifd
->M_
[pri
]);
453 * rmc_depth_compute(struct rm_class *cl) - This function computes the
454 * appropriate depth of class 'cl' and its ancestors.
460 rmc_depth_compute(struct rm_class
*cl
)
462 rm_class_t
*t
= cl
, *p
;
465 * Recompute the depth for the branch of the tree.
469 if (p
&& (t
->depth_
>= p
->depth_
)) {
470 p
->depth_
= t
->depth_
+ 1;
479 * rmc_depth_recompute(struct rm_class *cl) - This function re-computes
480 * the depth of the tree after a class has been deleted.
486 rmc_depth_recompute(rm_class_t
*cl
)
493 if ((t
= p
->children_
) == NULL
) {
499 if (t
->depth_
> cdepth
)
504 if (p
->depth_
== cdepth
+ 1)
505 /* no change to this parent */
508 p
->depth_
= cdepth
+ 1;
516 if (cl
->depth_
>= 1) {
517 if (cl
->children_
== NULL
) {
519 } else if ((t
= cl
->children_
) != NULL
) {
521 if (t
->children_
!= NULL
)
522 rmc_depth_recompute(t
);
526 rmc_depth_compute(cl
);
533 * rmc_delete_class(struct rm_ifdat *ifdat, struct rm_class *cl) - This
534 * function deletes a class from the link-sharing structure and frees
535 * all resources associated with the class.
541 rmc_delete_class(struct rm_ifdat
*ifd
, struct rm_class
*cl
)
543 struct rm_class
*p
, *head
, *previous
;
546 ASSERT(cl
->children_
== NULL
);
549 CALLOUT_STOP(&cl
->callout_
);
553 * Free packets in the packet queue.
554 * XXX - this may not be a desired behavior. Packets should be
560 * If the class has a parent, then remove the class from the
561 * class from the parent's children chain.
563 if (cl
->parent_
!= NULL
) {
564 head
= cl
->parent_
->children_
;
566 if (head
->next_
== NULL
) {
568 cl
->parent_
->children_
= NULL
;
569 cl
->parent_
->leaf_
= 1;
570 } else while (p
!= NULL
) {
573 cl
->parent_
->children_
= cl
->next_
;
575 previous
->next_
= cl
->next_
;
586 * Delete class from class priority peer list.
588 if ((p
= ifd
->active_
[cl
->pri_
]) != NULL
) {
590 * If there is more than one member of this priority
591 * level, then look for class(cl) in the priority level.
594 while (p
->peer_
!= cl
)
596 p
->peer_
= cl
->peer_
;
598 if (ifd
->active_
[cl
->pri_
] == cl
)
599 ifd
->active_
[cl
->pri_
] = cl
->peer_
;
602 ifd
->active_
[cl
->pri_
] = NULL
;
607 * Recompute the WRR weights.
610 ifd
->alloc_
[cl
->pri_
] -= cl
->allotment_
;
611 ifd
->num_
[cl
->pri_
]--;
612 rmc_wrr_set_weights(ifd
);
616 * Re-compute the depth of the tree.
619 rmc_depth_recompute(cl
->parent_
);
621 rmc_depth_recompute(ifd
->root_
);
627 * Free the class structure.
629 if (cl
->red_
!= NULL
) {
631 if (q_is_rio(cl
->q_
))
632 rio_destroy((rio_t
*)cl
->red_
);
635 if (q_is_red(cl
->q_
))
636 red_destroy(cl
->red_
);
639 free(cl
->q_
, M_DEVBUF
);
646 * rmc_init(...) - Initialize the resource management data structures
647 * associated with the output portion of interface 'ifp'. 'ifd' is
648 * where the structures will be built (for backwards compatibility, the
649 * structures aren't kept in the ifnet struct). 'nsecPerByte'
650 * gives the link speed (inverse of bandwidth) in nanoseconds/byte.
651 * 'restart' is the driver-specific routine that the generic 'delay
652 * until under limit' action will call to restart output. `maxq'
653 * is the queue size of the 'link' & 'default' classes. 'maxqueued'
654 * is the maximum number of packets that the resource management
655 * code will allow to be queued 'downstream' (this is typically 1).
657 * Returns: 0 on success
661 rmc_init(struct ifaltq
*ifq
, struct rm_ifdat
*ifd
, u_int nsecPerByte
,
662 void (*restart
)(struct ifaltq
*), int maxq
, int maxqueued
, u_int maxidle
,
663 int minidle
, u_int offtime
, int flags
)
668 * Initialize the CBQ tracing/debug facility.
672 mtu
= ifq
->altq_ifp
->if_mtu
;
674 printf("altq: %s: invalid MTU (interface not initialized?)\n",
675 ifq
->altq_ifp
->if_xname
);
679 (void)memset((char *)ifd
, 0, sizeof (*ifd
));
681 ifd
->restart
= restart
;
682 ifd
->maxqueued_
= maxqueued
;
683 ifd
->ns_per_byte_
= nsecPerByte
;
685 ifd
->wrr_
= (flags
& RMCF_WRR
) ? 1 : 0;
686 ifd
->efficient_
= (flags
& RMCF_EFFICIENT
) ? 1 : 0;
688 ifd
->maxiftime_
= mtu
* nsecPerByte
/ 1000 * 16;
689 if (mtu
* nsecPerByte
> 10 * 1000000)
690 ifd
->maxiftime_
/= 4;
694 CBQTRACE(rmc_init
, 'INIT', ifd
->cutoff_
);
697 * Initialize the CBQ's WRR state.
699 for (i
= 0; i
< RM_MAXPRIO
; i
++) {
704 ifd
->active_
[i
] = NULL
;
708 * Initialize current packet state.
712 for (i
= 0; i
< RM_MAXQUEUED
; i
++) {
713 ifd
->class_
[i
] = NULL
;
715 ifd
->borrowed_
[i
] = NULL
;
719 * Create the root class of the link-sharing structure.
721 if ((ifd
->root_
= rmc_newclass(0, ifd
,
723 rmc_root_overlimit
, maxq
, 0, 0,
724 maxidle
, minidle
, offtime
,
726 printf("rmc_init: root class not allocated\n");
729 ifd
->root_
->depth_
= 0;
736 * rmc_queue_packet(struct rm_class *cl, mbuf_t *m) - Add packet given by
737 * mbuf 'm' to queue for resource class 'cl'. This routine is called
738 * by a driver's if_output routine. This routine must be called with
739 * output packet completion interrupts locked out (to avoid racing with
742 * Returns: 0 on successful queueing
743 * -1 when packet drop occurs
746 rmc_queue_packet(struct rm_class
*cl
, mbuf_t
*m
)
749 struct rm_ifdat
*ifd
= cl
->ifdat_
;
751 int is_empty
= qempty(cl
->q_
);
754 if (ifd
->cutoff_
> 0) {
755 if (TV_LT(&cl
->undertime_
, &now
)) {
756 if (ifd
->cutoff_
> cl
->depth_
)
757 ifd
->cutoff_
= cl
->depth_
;
758 CBQTRACE(rmc_queue_packet
, 'ffoc', cl
->depth_
);
763 * the class is overlimit. if the class has
764 * underlimit ancestors, set cutoff to the lowest
767 struct rm_class
*borrow
= cl
->borrow_
;
769 while (borrow
!= NULL
&&
770 borrow
->depth_
< ifd
->cutoff_
) {
771 if (TV_LT(&borrow
->undertime_
, &now
)) {
772 ifd
->cutoff_
= borrow
->depth_
;
773 CBQTRACE(rmc_queue_packet
, 'ffob', ifd
->cutoff_
);
776 borrow
= borrow
->borrow_
;
780 else if ((ifd
->cutoff_
> 1) && cl
->borrow_
) {
781 if (TV_LT(&cl
->borrow_
->undertime_
, &now
)) {
782 ifd
->cutoff_
= cl
->borrow_
->depth_
;
783 CBQTRACE(rmc_queue_packet
, 'ffob',
784 cl
->borrow_
->depth_
);
790 if (_rmc_addq(cl
, m
) < 0)
795 CBQTRACE(rmc_queue_packet
, 'ytpe', cl
->stats_
.handle
);
799 if (qlen(cl
->q_
) > qlimit(cl
->q_
)) {
800 /* note: qlimit can be set to 0 or 1 */
809 * rmc_tl_satisfied(struct rm_ifdat *ifd, struct timeval *now) - Check all
810 * classes to see if there are satified.
814 rmc_tl_satisfied(struct rm_ifdat
*ifd
, struct timeval
*now
)
819 for (i
= RM_MAXPRIO
- 1; i
>= 0; i
--) {
820 if ((bp
= ifd
->active_
[i
]) != NULL
) {
823 if (!rmc_satisfied(p
, now
)) {
824 ifd
->cutoff_
= p
->depth_
;
836 * rmc_satisfied - Return 1 of the class is satisfied. O, otherwise.
840 rmc_satisfied(struct rm_class
*cl
, struct timeval
*now
)
846 if (TV_LT(now
, &cl
->undertime_
))
848 if (cl
->depth_
== 0) {
849 if (!cl
->sleeping_
&& (qlen(cl
->q_
) > cl
->qthresh_
))
854 if (cl
->children_
!= NULL
) {
857 if (!rmc_satisfied(p
, now
))
867 * Return 1 if class 'cl' is under limit or can borrow from a parent,
868 * 0 if overlimit. As a side-effect, this routine will invoke the
869 * class overlimit action if the class if overlimit.
873 rmc_under_limit(struct rm_class
*cl
, struct timeval
*now
)
877 struct rm_ifdat
*ifd
= cl
->ifdat_
;
879 ifd
->borrowed_
[ifd
->qi_
] = NULL
;
881 * If cl is the root class, then always return that it is
882 * underlimit. Otherwise, check to see if the class is underlimit.
884 if (cl
->parent_
== NULL
)
888 if (TV_LT(now
, &cl
->undertime_
))
891 CALLOUT_STOP(&cl
->callout_
);
893 cl
->undertime_
.tv_sec
= 0;
898 while (cl
->undertime_
.tv_sec
&& TV_LT(now
, &cl
->undertime_
)) {
899 if (((cl
= cl
->borrow_
) == NULL
) ||
900 (cl
->depth_
> ifd
->cutoff_
)) {
903 /* cutoff is taking effect, just
904 return false without calling
908 #ifdef BORROW_OFFTIME
910 * check if the class can borrow offtime too.
911 * borrow offtime from the top of the borrow
912 * chain if the top class is not overloaded.
915 /* cutoff is taking effect, use this class as top. */
917 CBQTRACE(rmc_under_limit
, 'ffou', ifd
->cutoff_
);
919 if (top
!= NULL
&& top
->avgidle_
== top
->minidle_
)
922 (p
->overlimit
)(p
, top
);
925 (p
->overlimit
)(p
, NULL
);
933 ifd
->borrowed_
[ifd
->qi_
] = cl
;
938 * _rmc_wrr_dequeue_next() - This is scheduler for WRR as opposed to
939 * Packet-by-packet round robin.
941 * The heart of the weighted round-robin scheduler, which decides which
942 * class next gets to send a packet. Highest priority first, then
943 * weighted round-robin within priorites.
945 * Each able-to-send class gets to send until its byte allocation is
946 * exhausted. Thus, the active pointer is only changed after a class has
947 * exhausted its allocation.
949 * If the scheduler finds no class that is underlimit or able to borrow,
950 * then the first class found that had a nonzero queue and is allowed to
951 * borrow gets to send.
955 _rmc_wrr_dequeue_next(struct rm_ifdat
*ifd
, int op
)
957 struct rm_class
*cl
= NULL
, *first
= NULL
;
966 * if the driver polls the top of the queue and then removes
967 * the polled packet, we must return the same packet.
969 if (op
== ALTDQ_REMOVE
&& ifd
->pollcache_
) {
970 cl
= ifd
->pollcache_
;
972 if (ifd
->efficient_
) {
973 /* check if this class is overlimit */
974 if (cl
->undertime_
.tv_sec
!= 0 &&
975 rmc_under_limit(cl
, &now
) == 0)
978 ifd
->pollcache_
= NULL
;
982 /* mode == ALTDQ_POLL || pollcache == NULL */
983 ifd
->pollcache_
= NULL
;
984 ifd
->borrowed_
[ifd
->qi_
] = NULL
;
989 for (cpri
= RM_MAXPRIO
- 1; cpri
>= 0; cpri
--) {
990 if (ifd
->na_
[cpri
] == 0)
994 * Loop through twice for a priority level, if some class
995 * was unable to send a packet the first round because
996 * of the weighted round-robin mechanism.
997 * During the second loop at this level, deficit==2.
998 * (This second loop is not needed if for every class,
999 * "M[cl->pri_])" times "cl->allotment" is greater than
1000 * the byte size for the largest packet in the class.)
1003 cl
= ifd
->active_
[cpri
];
1006 if ((deficit
< 2) && (cl
->bytes_alloc_
<= 0))
1007 cl
->bytes_alloc_
+= cl
->w_allotment_
;
1008 if (!qempty(cl
->q_
)) {
1009 if ((cl
->undertime_
.tv_sec
== 0) ||
1010 rmc_under_limit(cl
, &now
)) {
1011 if (cl
->bytes_alloc_
> 0 || deficit
> 1)
1014 /* underlimit but no alloc */
1017 ifd
->borrowed_
[ifd
->qi_
] = NULL
;
1020 else if (first
== NULL
&& cl
->borrow_
!= NULL
)
1021 first
= cl
; /* borrowing candidate */
1024 cl
->bytes_alloc_
= 0;
1026 } while (cl
!= ifd
->active_
[cpri
]);
1029 /* first loop found an underlimit class with deficit */
1030 /* Loop on same priority level, with new deficit. */
1036 #ifdef ADJUST_CUTOFF
1038 * no underlimit class found. if cutoff is taking effect,
1039 * increase cutoff and try again.
1041 if (first
!= NULL
&& ifd
->cutoff_
< ifd
->root_
->depth_
) {
1043 CBQTRACE(_rmc_wrr_dequeue_next
, 'ojda', ifd
->cutoff_
);
1046 #endif /* ADJUST_CUTOFF */
1048 * If LINK_EFFICIENCY is turned on, then the first overlimit
1049 * class we encounter will send a packet if all the classes
1050 * of the link-sharing structure are overlimit.
1053 CBQTRACE(_rmc_wrr_dequeue_next
, 'otsr', ifd
->cutoff_
);
1055 if (!ifd
->efficient_
|| first
== NULL
)
1060 #if 0 /* too time-consuming for nothing */
1062 CALLOUT_STOP(&cl
->callout_
);
1064 cl
->undertime_
.tv_sec
= 0;
1066 ifd
->borrowed_
[ifd
->qi_
] = cl
->borrow_
;
1067 ifd
->cutoff_
= cl
->borrow_
->depth_
;
1070 * Deque the packet and do the book keeping...
1073 if (op
== ALTDQ_REMOVE
) {
1076 panic("_rmc_wrr_dequeue_next");
1081 * Update class statistics and link data.
1083 if (cl
->bytes_alloc_
> 0)
1084 cl
->bytes_alloc_
-= m_pktlen(m
);
1086 if ((cl
->bytes_alloc_
<= 0) || first
== cl
)
1087 ifd
->active_
[cl
->pri_
] = cl
->peer_
;
1089 ifd
->active_
[cl
->pri_
] = cl
;
1091 ifd
->class_
[ifd
->qi_
] = cl
;
1092 ifd
->curlen_
[ifd
->qi_
] = m_pktlen(m
);
1093 ifd
->now_
[ifd
->qi_
] = now
;
1094 ifd
->qi_
= (ifd
->qi_
+ 1) % ifd
->maxqueued_
;
1097 /* mode == ALTDQ_PPOLL */
1099 ifd
->pollcache_
= cl
;
1105 * Dequeue & return next packet from the highest priority class that
1106 * has a packet to send & has enough allocation to send it. This
1107 * routine is called by a driver whenever it needs a new packet to
1111 _rmc_prr_dequeue_next(struct rm_ifdat
*ifd
, int op
)
1115 struct rm_class
*cl
, *first
= NULL
;
1121 * if the driver polls the top of the queue and then removes
1122 * the polled packet, we must return the same packet.
1124 if (op
== ALTDQ_REMOVE
&& ifd
->pollcache_
) {
1125 cl
= ifd
->pollcache_
;
1127 ifd
->pollcache_
= NULL
;
1130 /* mode == ALTDQ_POLL || pollcache == NULL */
1131 ifd
->pollcache_
= NULL
;
1132 ifd
->borrowed_
[ifd
->qi_
] = NULL
;
1134 #ifdef ADJUST_CUTOFF
1137 for (cpri
= RM_MAXPRIO
- 1; cpri
>= 0; cpri
--) {
1138 if (ifd
->na_
[cpri
] == 0)
1140 cl
= ifd
->active_
[cpri
];
1143 if (!qempty(cl
->q_
)) {
1144 if ((cl
->undertime_
.tv_sec
== 0) ||
1145 rmc_under_limit(cl
, &now
))
1147 if (first
== NULL
&& cl
->borrow_
!= NULL
)
1151 } while (cl
!= ifd
->active_
[cpri
]);
1154 #ifdef ADJUST_CUTOFF
1156 * no underlimit class found. if cutoff is taking effect, increase
1157 * cutoff and try again.
1159 if (first
!= NULL
&& ifd
->cutoff_
< ifd
->root_
->depth_
) {
1163 #endif /* ADJUST_CUTOFF */
1165 * If LINK_EFFICIENCY is turned on, then the first overlimit
1166 * class we encounter will send a packet if all the classes
1167 * of the link-sharing structure are overlimit.
1170 if (!ifd
->efficient_
|| first
== NULL
)
1175 #if 0 /* too time-consuming for nothing */
1177 CALLOUT_STOP(&cl
->callout_
);
1179 cl
->undertime_
.tv_sec
= 0;
1181 ifd
->borrowed_
[ifd
->qi_
] = cl
->borrow_
;
1182 ifd
->cutoff_
= cl
->borrow_
->depth_
;
1185 * Deque the packet and do the book keeping...
1188 if (op
== ALTDQ_REMOVE
) {
1191 panic("_rmc_prr_dequeue_next");
1195 ifd
->active_
[cpri
] = cl
->peer_
;
1197 ifd
->class_
[ifd
->qi_
] = cl
;
1198 ifd
->curlen_
[ifd
->qi_
] = m_pktlen(m
);
1199 ifd
->now_
[ifd
->qi_
] = now
;
1200 ifd
->qi_
= (ifd
->qi_
+ 1) % ifd
->maxqueued_
;
1203 /* mode == ALTDQ_POLL */
1205 ifd
->pollcache_
= cl
;
1212 * rmc_dequeue_next(struct rm_ifdat *ifd, struct timeval *now) - this function
1213 * is invoked by the packet driver to get the next packet to be
1214 * dequeued and output on the link. If WRR is enabled, then the
1215 * WRR dequeue next routine will determine the next packet to sent.
1216 * Otherwise, packet-by-packet round robin is invoked.
1218 * Returns: NULL, if a packet is not available or if all
1219 * classes are overlimit.
1221 * Otherwise, Pointer to the next packet.
1225 rmc_dequeue_next(struct rm_ifdat
*ifd
, int mode
)
1227 if (ifd
->queued_
>= ifd
->maxqueued_
)
1230 return (_rmc_wrr_dequeue_next(ifd
, mode
));
1232 return (_rmc_prr_dequeue_next(ifd
, mode
));
1236 * Update the utilization estimate for the packet that just completed.
1237 * The packet's class & the parent(s) of that class all get their
1238 * estimators updated. This routine is called by the driver's output-
1239 * packet-completion interrupt service routine.
1243 * a macro to approximate "divide by 1000" that gives 0.000999,
1244 * if a value has enough effective digits.
1245 * (on pentium, mul takes 9 cycles but div takes 46!)
1247 #define NSEC_TO_USEC(t) (((t) >> 10) + ((t) >> 16) + ((t) >> 17))
1249 rmc_update_class_util(struct rm_ifdat
*ifd
)
1251 int idle
, avgidle
, pktlen
;
1252 int pkt_time
, tidle
;
1253 rm_class_t
*cl
, *borrowed
;
1254 rm_class_t
*borrows
;
1255 struct timeval
*nowp
;
1258 * Get the most recent completed class.
1260 if ((cl
= ifd
->class_
[ifd
->qo_
]) == NULL
)
1263 pktlen
= ifd
->curlen_
[ifd
->qo_
];
1264 borrowed
= ifd
->borrowed_
[ifd
->qo_
];
1267 PKTCNTR_ADD(&cl
->stats_
.xmit_cnt
, pktlen
);
1270 * Run estimator on class and its ancestors.
1273 * rm_update_class_util is designed to be called when the
1274 * transfer is completed from a xmit complete interrupt,
1275 * but most drivers don't implement an upcall for that.
1276 * so, just use estimated completion time.
1277 * as a result, ifd->qi_ and ifd->qo_ are always synced.
1279 nowp
= &ifd
->now_
[ifd
->qo_
];
1280 /* get pkt_time (for link) in usec */
1281 #if 1 /* use approximation */
1282 pkt_time
= ifd
->curlen_
[ifd
->qo_
] * ifd
->ns_per_byte_
;
1283 pkt_time
= NSEC_TO_USEC(pkt_time
);
1285 pkt_time
= ifd
->curlen_
[ifd
->qo_
] * ifd
->ns_per_byte_
/ 1000;
1287 #if 1 /* ALTQ4PPP */
1288 if (TV_LT(nowp
, &ifd
->ifnow_
)) {
1292 * make sure the estimated completion time does not go
1293 * too far. it can happen when the link layer supports
1294 * data compression or the interface speed is set to
1295 * a much lower value.
1297 TV_DELTA(&ifd
->ifnow_
, nowp
, iftime
);
1298 if (iftime
+pkt_time
< ifd
->maxiftime_
) {
1299 TV_ADD_DELTA(&ifd
->ifnow_
, pkt_time
, &ifd
->ifnow_
);
1301 TV_ADD_DELTA(nowp
, ifd
->maxiftime_
, &ifd
->ifnow_
);
1304 TV_ADD_DELTA(nowp
, pkt_time
, &ifd
->ifnow_
);
1307 if (TV_LT(nowp
, &ifd
->ifnow_
)) {
1308 TV_ADD_DELTA(&ifd
->ifnow_
, pkt_time
, &ifd
->ifnow_
);
1310 TV_ADD_DELTA(nowp
, pkt_time
, &ifd
->ifnow_
);
1314 while (cl
!= NULL
) {
1315 TV_DELTA(&ifd
->ifnow_
, &cl
->last_
, idle
);
1316 if (idle
>= 2000000)
1318 * this class is idle enough, reset avgidle.
1319 * (TV_DELTA returns 2000000 us when delta is large.)
1321 cl
->avgidle_
= cl
->maxidle_
;
1323 /* get pkt_time (for class) in usec */
1324 #if 1 /* use approximation */
1325 pkt_time
= pktlen
* cl
->ns_per_byte_
;
1326 pkt_time
= NSEC_TO_USEC(pkt_time
);
1328 pkt_time
= pktlen
* cl
->ns_per_byte_
/ 1000;
1332 avgidle
= cl
->avgidle_
;
1333 avgidle
+= idle
- (avgidle
>> RM_FILTER_GAIN
);
1334 cl
->avgidle_
= avgidle
;
1336 /* Are we overlimit ? */
1338 CBQTRACE(rmc_update_class_util
, 'milo', cl
->stats_
.handle
);
1341 * need some lower bound for avgidle, otherwise
1342 * a borrowing class gets unbounded penalty.
1344 if (avgidle
< cl
->minidle_
)
1345 avgidle
= cl
->avgidle_
= cl
->minidle_
;
1347 /* set next idle to make avgidle 0 */
1349 (((1 - RM_POWER
) * avgidle
) >> RM_FILTER_GAIN
);
1350 TV_ADD_DELTA(nowp
, tidle
, &cl
->undertime_
);
1354 (avgidle
> cl
->maxidle_
) ? cl
->maxidle_
: avgidle
;
1355 cl
->undertime_
.tv_sec
= 0;
1356 if (cl
->sleeping_
) {
1357 CALLOUT_STOP(&cl
->callout_
);
1362 if (borrows
!= NULL
) {
1364 ++cl
->stats_
.borrows
;
1368 cl
->last_
= ifd
->ifnow_
;
1369 cl
->last_pkttime_
= pkt_time
;
1372 if (cl
->parent_
== NULL
) {
1373 /* take stats of root class */
1374 PKTCNTR_ADD(&cl
->stats_
.xmit_cnt
, pktlen
);
1382 * Check to see if cutoff needs to set to a new level.
1384 cl
= ifd
->class_
[ifd
->qo_
];
1385 if (borrowed
&& (ifd
->cutoff_
>= borrowed
->depth_
)) {
1387 if ((qlen(cl
->q_
) <= 0) || TV_LT(nowp
, &borrowed
->undertime_
)) {
1388 rmc_tl_satisfied(ifd
, nowp
);
1389 CBQTRACE(rmc_update_class_util
, 'broe', ifd
->cutoff_
);
1391 ifd
->cutoff_
= borrowed
->depth_
;
1392 CBQTRACE(rmc_update_class_util
, 'ffob', borrowed
->depth_
);
1395 if ((qlen(cl
->q_
) <= 1) || TV_LT(&now
, &borrowed
->undertime_
)) {
1398 rmc_tl_satisfied(ifd
, &now
);
1400 CBQTRACE(rmc_update_class_util
, 'broe', ifd
->cutoff_
);
1402 ifd
->cutoff_
= borrowed
->depth_
;
1403 CBQTRACE(rmc_update_class_util
, 'ffob', borrowed
->depth_
);
1409 * Release class slot
1411 ifd
->borrowed_
[ifd
->qo_
] = NULL
;
1412 ifd
->class_
[ifd
->qo_
] = NULL
;
1413 ifd
->qo_
= (ifd
->qo_
+ 1) % ifd
->maxqueued_
;
1419 * rmc_drop_action(struct rm_class *cl) - Generic (not protocol-specific)
1420 * over-limit action routines. These get invoked by rmc_under_limit()
1421 * if a class with packets to send if over its bandwidth limit & can't
1422 * borrow from a parent class.
1428 rmc_drop_action(struct rm_class
*cl
)
1430 struct rm_ifdat
*ifd
= cl
->ifdat_
;
1432 ASSERT(qlen(cl
->q_
) > 0);
1435 ifd
->na_
[cl
->pri_
]--;
1439 rmc_dropall(struct rm_class
*cl
)
1441 struct rm_ifdat
*ifd
= cl
->ifdat_
;
1443 if (!qempty(cl
->q_
)) {
1446 ifd
->na_
[cl
->pri_
]--;
1450 #if (__FreeBSD_version > 300000)
1451 static int tvhzto(struct timeval
*);
1454 tvhzto(struct timeval
*tv
)
1459 t2
.tv_sec
= tv
->tv_sec
- t2
.tv_sec
;
1460 t2
.tv_usec
= tv
->tv_usec
- t2
.tv_usec
;
1461 return (tvtohz(&t2
));
1463 #endif /* __FreeBSD_version > 300000 */
1467 * rmc_delay_action(struct rm_class *cl) - This function is the generic CBQ
1468 * delay action routine. It is invoked via rmc_under_limit when the
1469 * packet is discoverd to be overlimit.
1471 * If the delay action is result of borrow class being overlimit, then
1472 * delay for the offtime of the borrowing class that is overlimit.
1478 rmc_delay_action(struct rm_class
*cl
, struct rm_class
*borrow
)
1480 int ndelay
, t
, extradelay
;
1482 cl
->stats_
.overactions
++;
1483 TV_DELTA(&cl
->undertime_
, &cl
->overtime_
, ndelay
);
1484 #ifndef BORROW_OFFTIME
1485 ndelay
+= cl
->offtime_
;
1488 if (!cl
->sleeping_
) {
1489 CBQTRACE(rmc_delay_action
, 'yled', cl
->stats_
.handle
);
1490 #ifdef BORROW_OFFTIME
1492 extradelay
= borrow
->offtime_
;
1495 extradelay
= cl
->offtime_
;
1499 * XXX recalculate suspend time:
1500 * current undertime is (tidle + pkt_time) calculated
1501 * from the last transmission.
1502 * tidle: time required to bring avgidle back to 0
1503 * pkt_time: target waiting time for this class
1504 * we need to replace pkt_time by offtime
1506 extradelay
-= cl
->last_pkttime_
;
1508 if (extradelay
> 0) {
1509 TV_ADD_DELTA(&cl
->undertime_
, extradelay
, &cl
->undertime_
);
1510 ndelay
+= extradelay
;
1514 cl
->stats_
.delays
++;
1517 * Since packets are phased randomly with respect to the
1518 * clock, 1 tick (the next clock tick) can be an arbitrarily
1519 * short time so we have to wait for at least two ticks.
1520 * NOTE: If there's no other traffic, we need the timer as
1521 * a 'backstop' to restart this class.
1523 if (ndelay
> tick
* 2) {
1525 /* FreeBSD rounds up the tick */
1526 t
= tvhzto(&cl
->undertime_
);
1528 /* other BSDs round down the tick */
1529 t
= tvhzto(&cl
->undertime_
) + 1;
1533 CALLOUT_RESET(&cl
->callout_
, t
,
1534 (timeout_t
*)rmc_restart
, (void *)cl
);
1540 * rmc_restart() - is just a helper routine for rmc_delay_action -- it is
1541 * called by the system timer code & is responsible checking if the
1542 * class is still sleeping (it might have been restarted as a side
1543 * effect of the queue scan on a packet arrival) and, if so, restarting
1544 * output for the class. Inspecting the class state & restarting output
1545 * require locking the class structure. In general the driver is
1546 * responsible for locking but this is the only routine that is not
1547 * called directly or indirectly from the interface driver so it has
1548 * know about system locking conventions. Under bsd, locking is done
1549 * by raising IPL to splnet so that's what's implemented here. On a
1550 * different system this would probably need to be changed.
1556 rmc_restart(struct rm_class
*cl
)
1558 struct rm_ifdat
*ifd
= cl
->ifdat_
;
1562 if (cl
->sleeping_
) {
1564 cl
->undertime_
.tv_sec
= 0;
1566 if (ifd
->queued_
< ifd
->maxqueued_
&& ifd
->restart
!= NULL
) {
1567 CBQTRACE(rmc_restart
, 'trts', cl
->stats_
.handle
);
1568 (ifd
->restart
)(ifd
->ifq_
);
1576 * rmc_root_overlimit(struct rm_class *cl) - This the generic overlimit
1577 * handling routine for the root class of the link sharing structure.
1583 rmc_root_overlimit(struct rm_class
*cl
,
1584 struct rm_class
*borrow
)
1586 panic("rmc_root_overlimit");
1590 * Packet Queue handling routines. Eventually, this is to localize the
1591 * effects on the code whether queues are red queues or droptail
1596 _rmc_addq(rm_class_t
*cl
, mbuf_t
*m
)
1599 if (q_is_rio(cl
->q_
))
1600 return rio_addq((rio_t
*)cl
->red_
, cl
->q_
, m
, cl
->pktattr_
);
1603 if (q_is_red(cl
->q_
))
1604 return red_addq(cl
->red_
, cl
->q_
, m
, cl
->pktattr_
);
1605 #endif /* ALTQ_RED */
1607 if (cl
->flags_
& RMCF_CLEARDSCP
)
1608 write_dsfield(m
, cl
->pktattr_
, 0);
1614 /* note: _rmc_dropq is not called for red */
1616 _rmc_dropq(rm_class_t
*cl
)
1620 if ((m
= _getq(cl
->q_
)) != NULL
)
1625 _rmc_getq(rm_class_t
*cl
)
1628 if (q_is_rio(cl
->q_
))
1629 return rio_getq((rio_t
*)cl
->red_
, cl
->q_
);
1632 if (q_is_red(cl
->q_
))
1633 return red_getq(cl
->red_
, cl
->q_
);
1635 return _getq(cl
->q_
);
1639 _rmc_pollq(rm_class_t
*cl
)
1641 return qhead(cl
->q_
);
1646 struct cbqtrace cbqtrace_buffer
[NCBQTRACE
+1];
1647 struct cbqtrace
*cbqtrace_ptr
= NULL
;
1651 * DDB hook to trace cbq events:
1652 * the last 1024 events are held in a circular buffer.
1653 * use "call cbqtrace_dump(N)" to display 20 events from Nth event.
1655 void cbqtrace_dump(int);
1656 static char *rmc_funcname(void *);
1658 static struct rmc_funcs
{
1663 rmc_init
, "rmc_init",
1664 rmc_queue_packet
, "rmc_queue_packet",
1665 rmc_under_limit
, "rmc_under_limit",
1666 rmc_update_class_util
, "rmc_update_class_util",
1667 rmc_delay_action
, "rmc_delay_action",
1668 rmc_restart
, "rmc_restart",
1669 _rmc_wrr_dequeue_next
, "_rmc_wrr_dequeue_next",
1674 rmc_funcname(void *func
)
1676 struct rmc_funcs
*fp
;
1678 for (fp
= rmc_funcs
; fp
->func
!= NULL
; fp
++)
1679 if (fp
->func
== func
)
1685 cbqtrace_dump(int counter
)
1690 counter
= counter
% NCBQTRACE
;
1691 p
= (int *)&cbqtrace_buffer
[counter
];
1693 for (i
=0; i
<20; i
++) {
1694 printf("[0x%x] ", *p
++);
1695 printf("%s: ", rmc_funcname((void *)*p
++));
1697 printf("%c%c%c%c: ", cp
[0], cp
[1], cp
[2], cp
[3]);
1698 printf("%d\n",*p
++);
1700 if (p
>= (int *)&cbqtrace_buffer
[NCBQTRACE
])
1701 p
= (int *)cbqtrace_buffer
;
1704 #endif /* CBQ_TRACE */
1705 #endif /* ALTQ_CBQ */
1707 #if defined(ALTQ_CBQ) || defined(ALTQ_RED) || defined(ALTQ_RIO) || defined(ALTQ_HFSC) || defined(ALTQ_PRIQ)
1708 #if !defined(__GNUC__) || defined(ALTQ_DEBUG)
1711 _addq(class_queue_t
*q
, mbuf_t
*m
)
1715 if ((m0
= qtail(q
)) != NULL
)
1716 m
->m_nextpkt
= m0
->m_nextpkt
;
1725 _getq(class_queue_t
*q
)
1729 if ((m
= qtail(q
)) == NULL
)
1731 if ((m0
= m
->m_nextpkt
) != m
)
1732 m
->m_nextpkt
= m0
->m_nextpkt
;
1734 ASSERT(qlen(q
) == 1);
1738 m0
->m_nextpkt
= NULL
;
1742 /* drop a packet at the tail of the queue */
1744 _getq_tail(class_queue_t
*q
)
1746 mbuf_t
*m
, *m0
, *prev
;
1748 if ((m
= m0
= qtail(q
)) == NULL
)
1754 prev
->m_nextpkt
= m
->m_nextpkt
;
1756 ASSERT(qlen(q
) == 1);
1761 m
->m_nextpkt
= NULL
;
1765 /* randomly select a packet in the queue */
1767 _getq_random(class_queue_t
*q
)
1772 if ((m
= qtail(q
)) == NULL
)
1774 if (m
->m_nextpkt
== m
) {
1775 ASSERT(qlen(q
) == 1);
1778 struct mbuf
*prev
= NULL
;
1780 n
= arc4random() % qlen(q
) + 1;
1781 for (i
= 0; i
< n
; i
++) {
1785 prev
->m_nextpkt
= m
->m_nextpkt
;
1790 m
->m_nextpkt
= NULL
;
1795 _removeq(class_queue_t
*q
, mbuf_t
*m
)
1804 prev
->m_nextpkt
= m
->m_nextpkt
;
1807 else if (qtail(q
) == m
)
1813 _flushq(class_queue_t
*q
)
1817 while ((m
= _getq(q
)) != NULL
)
1819 ASSERT(qlen(q
) == 0);
1822 #endif /* !__GNUC__ || ALTQ_DEBUG */
1823 #endif /* ALTQ_CBQ || ALTQ_RED || ALTQ_RIO || ALTQ_HFSC || ALTQ_PRIQ */