1 /* $KAME: altq_rio.c,v 1.17 2003/07/10 12:07:49 kjc Exp $ */
2 /* $DragonFly: src/sys/net/altq/altq_rio.c,v 1.3 2006/12/22 23:44:55 swildner Exp $ */
5 * Copyright (C) 1998-2003
6 * Sony Computer Science Laboratories Inc. All rights reserved.
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.
17 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Copyright (c) 1990-1994 Regents of the University of California.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the Computer Systems
44 * Engineering Group at Lawrence Berkeley Laboratory.
45 * 4. Neither the name of the University nor of the Laboratory may be used
46 * to endorse or promote products derived from this software without
47 * specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 #include "opt_inet6.h"
66 #ifdef ALTQ_RIO /* rio is enabled by ALTQ_RIO option in opt_altq.h */
68 #include <sys/param.h>
69 #include <sys/malloc.h>
71 #include <sys/socket.h>
72 #include <sys/systm.h>
73 #include <sys/errno.h>
77 #include <netinet/in.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/ip.h>
81 #include <netinet/ip6.h>
84 #include <net/pf/pfvar.h>
85 #include <net/altq/altq.h>
86 #include <net/altq/altq_red.h>
87 #include <net/altq/altq_rio.h>
90 * RIO: RED with IN/OUT bit
92 * "Explicit Allocation of Best Effort Packet Delivery Service"
93 * David D. Clark and Wenjia Fang, MIT Lab for Computer Science
94 * http://diffserv.lcs.mit.edu/Papers/exp-alloc-ddc-wf.{ps,pdf}
96 * this implementation is extended to support more than 2 drop precedence
97 * values as described in RFC2597 (Assured Forwarding PHB Group).
101 * AF DS (differentiated service) codepoints.
102 * (classes can be mapped to CBQ or H-FSC classes.)
105 * +---+---+---+---+---+---+---+---+
106 * | CLASS |DropPre| 0 | CU |
107 * +---+---+---+---+---+---+---+---+
115 * medium drop prec: 10
119 /* normal red parameters */
120 #define W_WEIGHT 512 /* inverse of weight of EWMA (511/512) */
121 /* q_weight = 0.00195 */
123 /* red parameters for a slow link */
124 #define W_WEIGHT_1 128 /* inverse of weight of EWMA (127/128) */
125 /* q_weight = 0.0078125 */
127 /* red parameters for a very slow link (e.g., dialup) */
128 #define W_WEIGHT_2 64 /* inverse of weight of EWMA (63/64) */
129 /* q_weight = 0.015625 */
131 /* fixed-point uses 12-bit decimal places */
132 #define FP_SHIFT 12 /* fixed-point shift */
134 /* red parameters for drop probability */
135 #define INV_P_MAX 10 /* inverse of max drop probability */
136 #define TH_MIN 5 /* min threshold */
137 #define TH_MAX 15 /* max threshold */
139 #define RIO_LIMIT 60 /* default max queue lenght */
140 #define RIO_STATS /* collect statistics */
142 /* default rio parameter values */
143 static struct redparams default_rio_params[RIO_NDROPPREC] = {
144 /* th_min, th_max, inv_pmax */
145 { TH_MAX * 2 + TH_MIN, TH_MAX * 3, INV_P_MAX }, /* low drop precedence */
146 { TH_MAX + TH_MIN, TH_MAX * 2, INV_P_MAX }, /* medium drop precedence */
147 { TH_MIN, TH_MAX, INV_P_MAX } /* high drop precedence */
150 /* internal function prototypes */
151 static int dscp2index(uint8_t);
152 /* YYY Do we really need this? */
153 #define AF_DROPPRECMASK 0x18
154 #define DSCP_MASK 0xfc
157 rio_alloc(int weight, struct redparams *params, int flags, int pkttime)
163 rp = kmalloc(sizeof(*rp), M_ALTQ, M_WAITOK | M_ZERO);
165 rp->rio_flags = flags;
167 /* default packet time: 1000 bytes / 10Mbps * 8 * 1000000 */
168 rp->rio_pkttime = 800;
170 rp->rio_pkttime = pkttime;
173 rp->rio_weight = weight;
176 rp->rio_weight = W_WEIGHT;
178 /* when the link is very slow, adjust red parameters */
179 npkts_per_sec = 1000000 / rp->rio_pkttime;
180 if (npkts_per_sec < 50) {
181 /* up to about 400Kbps */
182 rp->rio_weight = W_WEIGHT_2;
183 } else if (npkts_per_sec < 300) {
184 /* up to about 2.4Mbps */
185 rp->rio_weight = W_WEIGHT_1;
189 /* calculate wshift. weight must be power of 2 */
191 for (i = 0; w > 1; i++)
194 w = 1 << rp->rio_wshift;
195 if (w != rp->rio_weight) {
196 kprintf("invalid weight value %d for red! use %d\n",
201 /* allocate weight table */
202 rp->rio_wtab = wtab_alloc(rp->rio_weight);
204 for (i = 0; i < RIO_NDROPPREC; i++) {
205 struct dropprec_state *prec = &rp->rio_precstate[i];
210 if (params == NULL || params[i].inv_pmax == 0)
211 prec->inv_pmax = default_rio_params[i].inv_pmax;
213 prec->inv_pmax = params[i].inv_pmax;
214 if (params == NULL || params[i].th_min == 0)
215 prec->th_min = default_rio_params[i].th_min;
217 prec->th_min = params[i].th_min;
218 if (params == NULL || params[i].th_max == 0)
219 prec->th_max = default_rio_params[i].th_max;
221 prec->th_max = params[i].th_max;
224 * th_min_s and th_max_s are scaled versions of th_min
225 * and th_max to be compared with avg.
227 prec->th_min_s = prec->th_min << (rp->rio_wshift + FP_SHIFT);
228 prec->th_max_s = prec->th_max << (rp->rio_wshift + FP_SHIFT);
231 * precompute probability denominator
232 * probd = (2 * (TH_MAX-TH_MIN) / pmax) in fixed-point
234 prec->probd = (2 * (prec->th_max - prec->th_min)
235 * prec->inv_pmax) << FP_SHIFT;
237 microtime(&prec->last);
244 rio_destroy(rio_t *rp)
246 wtab_destroy(rp->rio_wtab);
251 rio_getstats(rio_t *rp, struct redstats *sp)
255 for (i = 0; i < RIO_NDROPPREC; i++) {
256 bcopy(&rp->q_stats[i], sp, sizeof(struct redstats));
257 sp->q_avg = rp->rio_precstate[i].avg >> rp->rio_wshift;
262 #if (RIO_NDROPPREC == 3)
264 * internally, a drop precedence value is converted to an index
268 dscp2index(uint8_t dscp)
270 int dpindex = dscp & AF_DROPPRECMASK;
274 return ((dpindex >> 3) - 1);
280 * kludge: when a packet is dequeued, we need to know its drop precedence
281 * in order to keep the queue length of each drop precedence.
282 * use m_pkthdr.rcvif to pass this info.
284 #define RIOM_SET_PRECINDEX(m, idx) \
285 do { (m)->m_pkthdr.rcvif = (struct ifnet *)((long)(idx)); } while (0)
286 #define RIOM_GET_PRECINDEX(m) \
287 ({ long idx; idx = (long)((m)->m_pkthdr.rcvif); \
288 (m)->m_pkthdr.rcvif = NULL; idx; })
292 rio_addq(rio_t *rp, class_queue_t *q, struct mbuf *m, struct altq_pktattr *pktattr)
295 uint8_t dsfield, odsfield;
296 int dpindex, i, n, t;
298 struct dropprec_state *prec;
300 dsfield = odsfield = read_dsfield(m, pktattr);
301 dpindex = dscp2index(dsfield);
304 * update avg of the precedence states whose drop precedence
305 * is larger than or equal to the drop precedence of the packet
308 for (i = dpindex; i < RIO_NDROPPREC; i++) {
309 prec = &rp->rio_precstate[i];
315 t = (now.tv_sec - prec->last.tv_sec);
320 (now.tv_usec - prec->last.tv_usec);
321 n = t / rp->rio_pkttime;
322 /* calculate (avg = (1 - Wq)^n * avg) */
324 avg = (avg >> FP_SHIFT) *
325 pow_w(rp->rio_wtab, n);
329 /* run estimator. (avg is scaled by WEIGHT in fixed-point) */
330 avg += (prec->qlen << FP_SHIFT) - (avg >> rp->rio_wshift);
331 prec->avg = avg; /* save the new value */
333 * count keeps a tally of arriving traffic that has not
339 prec = &rp->rio_precstate[dpindex];
342 /* see if we drop early */
343 droptype = DTYPE_NODROP;
344 if (avg >= prec->th_min_s && prec->qlen > 1) {
345 if (avg >= prec->th_max_s) {
346 /* avg >= th_max: forced drop */
347 droptype = DTYPE_FORCED;
348 } else if (prec->old == 0) {
349 /* first exceeds th_min */
352 } else if (drop_early((avg - prec->th_min_s) >> rp->rio_wshift,
353 prec->probd, prec->count)) {
354 /* unforced drop by red */
355 droptype = DTYPE_EARLY;
363 * if the queue length hits the hard limit, it's a forced drop.
365 if (droptype == DTYPE_NODROP && qlen(q) >= qlimit(q))
366 droptype = DTYPE_FORCED;
368 if (droptype != DTYPE_NODROP) {
369 /* always drop incoming packet (as opposed to randomdrop) */
370 for (i = dpindex; i < RIO_NDROPPREC; i++)
371 rp->rio_precstate[i].count = 0;
373 if (droptype == DTYPE_EARLY)
374 rp->q_stats[dpindex].drop_unforced++;
376 rp->q_stats[dpindex].drop_forced++;
377 PKTCNTR_ADD(&rp->q_stats[dpindex].drop_cnt, m_pktlen(m));
383 for (i = dpindex; i < RIO_NDROPPREC; i++)
384 rp->rio_precstate[i].qlen++;
386 /* save drop precedence index in mbuf hdr */
387 RIOM_SET_PRECINDEX(m, dpindex);
389 if (rp->rio_flags & RIOF_CLEARDSCP)
390 dsfield &= ~DSCP_MASK;
392 if (dsfield != odsfield)
393 write_dsfield(m, pktattr, dsfield);
398 PKTCNTR_ADD(&rp->q_stats[dpindex].xmit_cnt, m_pktlen(m));
404 rio_getq(rio_t *rp, class_queue_t *q)
409 if ((m = _getq(q)) == NULL)
412 dpindex = RIOM_GET_PRECINDEX(m);
413 for (i = dpindex; i < RIO_NDROPPREC; i++) {
414 if (--rp->rio_precstate[i].qlen == 0) {
415 if (rp->rio_precstate[i].idle == 0) {
416 rp->rio_precstate[i].idle = 1;
417 microtime(&rp->rio_precstate[i].last);
424 #endif /* ALTQ_RIO */