xref: /dragonfly/sys/net/altq/altq_rio.c (revision 16dd80e4)
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 $ */
3 
4 /*
5  * Copyright (C) 1998-2003
6  *	Sony Computer Science Laboratories Inc.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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  *
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
27  * SUCH DAMAGE.
28  */
29 /*
30  * Copyright (c) 1990-1994 Regents of the University of California.
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
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.
48  *
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
59  * SUCH DAMAGE.
60  */
61 
62 #include "opt_altq.h"
63 #include "opt_inet.h"
64 #include "opt_inet6.h"
65 
66 #ifdef ALTQ_RIO	/* rio is enabled by ALTQ_RIO option in opt_altq.h */
67 
68 #include <sys/param.h>
69 #include <sys/malloc.h>
70 #include <sys/mbuf.h>
71 #include <sys/socket.h>
72 #include <sys/systm.h>
73 #include <sys/errno.h>
74 
75 #include <net/if.h>
76 #include <net/ifq_var.h>
77 
78 #include <netinet/in.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #ifdef INET6
82 #include <netinet/ip6.h>
83 #endif
84 
85 #include <net/pf/pfvar.h>
86 #include <net/altq/altq.h>
87 #include <net/altq/altq_red.h>
88 #include <net/altq/altq_rio.h>
89 
90 /*
91  * RIO: RED with IN/OUT bit
92  *   described in
93  *	"Explicit Allocation of Best Effort Packet Delivery Service"
94  *	David D. Clark and Wenjia Fang, MIT Lab for Computer Science
95  *	http://diffserv.lcs.mit.edu/Papers/exp-alloc-ddc-wf.{ps,pdf}
96  *
97  * this implementation is extended to support more than 2 drop precedence
98  * values as described in RFC2597 (Assured Forwarding PHB Group).
99  *
100  */
101 /*
102  * AF DS (differentiated service) codepoints.
103  * (classes can be mapped to CBQ or H-FSC classes.)
104  *
105  *      0   1   2   3   4   5   6   7
106  *    +---+---+---+---+---+---+---+---+
107  *    |   CLASS   |DropPre| 0 |  CU   |
108  *    +---+---+---+---+---+---+---+---+
109  *
110  *    class 1: 001
111  *    class 2: 010
112  *    class 3: 011
113  *    class 4: 100
114  *
115  *    low drop prec:    01
116  *    medium drop prec: 10
117  *    high drop prec:   11
118  */
119 
120 /* normal red parameters */
121 #define	W_WEIGHT	512	/* inverse of weight of EWMA (511/512) */
122 				/* q_weight = 0.00195 */
123 
124 /* red parameters for a slow link */
125 #define	W_WEIGHT_1	128	/* inverse of weight of EWMA (127/128) */
126 				/* q_weight = 0.0078125 */
127 
128 /* red parameters for a very slow link (e.g., dialup) */
129 #define	W_WEIGHT_2	64	/* inverse of weight of EWMA (63/64) */
130 				/* q_weight = 0.015625 */
131 
132 /* fixed-point uses 12-bit decimal places */
133 #define	FP_SHIFT	12	/* fixed-point shift */
134 
135 /* red parameters for drop probability */
136 #define	INV_P_MAX	10	/* inverse of max drop probability */
137 #define	TH_MIN		 5	/* min threshold */
138 #define	TH_MAX		15	/* max threshold */
139 
140 #define	RIO_LIMIT	60	/* default max queue lenght */
141 #define	RIO_STATS		/* collect statistics */
142 
143 /* default rio parameter values */
144 static struct redparams default_rio_params[RIO_NDROPPREC] = {
145   /* th_min,		 th_max,     inv_pmax */
146   { TH_MAX * 2 + TH_MIN, TH_MAX * 3, INV_P_MAX }, /* low drop precedence */
147   { TH_MAX + TH_MIN,	 TH_MAX * 2, INV_P_MAX }, /* medium drop precedence */
148   { TH_MIN,		 TH_MAX,     INV_P_MAX }  /* high drop precedence */
149 };
150 
151 /* internal function prototypes */
152 static int	dscp2index(uint8_t);
153 /* YYY Do we really need this? */
154 #define	AF_DROPPRECMASK		0x18
155 #define	DSCP_MASK	0xfc
156 
157 rio_t *
158 rio_alloc(int weight, struct redparams *params, int flags, int pkttime)
159 {
160 	rio_t *rp;
161 	int w, i;
162 	int npkts_per_sec;
163 
164 	rp = kmalloc(sizeof(*rp), M_ALTQ, M_WAITOK | M_ZERO);
165 
166 	rp->rio_flags = flags;
167 	if (pkttime == 0)
168 		/* default packet time: 1000 bytes / 10Mbps * 8 * 1000000 */
169 		rp->rio_pkttime = 800;
170 	else
171 		rp->rio_pkttime = pkttime;
172 
173 	if (weight != 0)
174 		rp->rio_weight = weight;
175 	else {
176 		/* use default */
177 		rp->rio_weight = W_WEIGHT;
178 
179 		/* when the link is very slow, adjust red parameters */
180 		npkts_per_sec = 1000000 / rp->rio_pkttime;
181 		if (npkts_per_sec < 50) {
182 			/* up to about 400Kbps */
183 			rp->rio_weight = W_WEIGHT_2;
184 		} else if (npkts_per_sec < 300) {
185 			/* up to about 2.4Mbps */
186 			rp->rio_weight = W_WEIGHT_1;
187 		}
188 	}
189 
190 	/* calculate wshift.  weight must be power of 2 */
191 	w = rp->rio_weight;
192 	for (i = 0; w > 1; i++)
193 		w = w >> 1;
194 	rp->rio_wshift = i;
195 	w = 1 << rp->rio_wshift;
196 	if (w != rp->rio_weight) {
197 		kprintf("invalid weight value %d for red! use %d\n",
198 		       rp->rio_weight, w);
199 		rp->rio_weight = w;
200 	}
201 
202 	/* allocate weight table */
203 	rp->rio_wtab = wtab_alloc(rp->rio_weight);
204 
205 	for (i = 0; i < RIO_NDROPPREC; i++) {
206 		struct dropprec_state *prec = &rp->rio_precstate[i];
207 
208 		prec->avg = 0;
209 		prec->idle = 1;
210 
211 		if (params == NULL || params[i].inv_pmax == 0)
212 			prec->inv_pmax = default_rio_params[i].inv_pmax;
213 		else
214 			prec->inv_pmax = params[i].inv_pmax;
215 		if (params == NULL || params[i].th_min == 0)
216 			prec->th_min = default_rio_params[i].th_min;
217 		else
218 			prec->th_min = params[i].th_min;
219 		if (params == NULL || params[i].th_max == 0)
220 			prec->th_max = default_rio_params[i].th_max;
221 		else
222 			prec->th_max = params[i].th_max;
223 
224 		/*
225 		 * th_min_s and th_max_s are scaled versions of th_min
226 		 * and th_max to be compared with avg.
227 		 */
228 		prec->th_min_s = prec->th_min << (rp->rio_wshift + FP_SHIFT);
229 		prec->th_max_s = prec->th_max << (rp->rio_wshift + FP_SHIFT);
230 
231 		/*
232 		 * precompute probability denominator
233 		 *  probd = (2 * (TH_MAX-TH_MIN) / pmax) in fixed-point
234 		 */
235 		prec->probd = (2 * (prec->th_max - prec->th_min)
236 			       * prec->inv_pmax) << FP_SHIFT;
237 
238 		microtime(&prec->last);
239 	}
240 
241 	return (rp);
242 }
243 
244 void
245 rio_destroy(rio_t *rp)
246 {
247 	wtab_destroy(rp->rio_wtab);
248 	kfree(rp, M_ALTQ);
249 }
250 
251 void
252 rio_getstats(rio_t *rp, struct redstats *sp)
253 {
254 	int i;
255 
256 	for (i = 0; i < RIO_NDROPPREC; i++) {
257 		bcopy(&rp->q_stats[i], sp, sizeof(struct redstats));
258 		sp->q_avg = rp->rio_precstate[i].avg >> rp->rio_wshift;
259 		sp++;
260 	}
261 }
262 
263 #if (RIO_NDROPPREC == 3)
264 /*
265  * internally, a drop precedence value is converted to an index
266  * starting from 0.
267  */
268 static int
269 dscp2index(uint8_t dscp)
270 {
271 	int dpindex = dscp & AF_DROPPRECMASK;
272 
273 	if (dpindex == 0)
274 		return (0);
275 	return ((dpindex >> 3) - 1);
276 }
277 #endif
278 
279 #if 1
280 /*
281  * kludge: when a packet is dequeued, we need to know its drop precedence
282  * in order to keep the queue length of each drop precedence.
283  * use m_pkthdr.rcvif to pass this info.
284  */
285 #define	RIOM_SET_PRECINDEX(m, idx)	\
286 	do { (m)->m_pkthdr.rcvif = (struct ifnet *)((long)(idx)); } while (0)
287 #define	RIOM_GET_PRECINDEX(m)	\
288 	({ long idx; idx = (long)((m)->m_pkthdr.rcvif); \
289 	(m)->m_pkthdr.rcvif = NULL; idx; })
290 #endif
291 
292 int
293 rio_addq(rio_t *rp, class_queue_t *q, struct mbuf *m, struct altq_pktattr *pktattr)
294 {
295 	int avg, droptype;
296 	uint8_t dsfield, odsfield;
297 	int dpindex, i, n, t;
298 	struct timeval now;
299 	struct dropprec_state *prec;
300 
301 	dsfield = odsfield = read_dsfield(m, pktattr);
302 	dpindex = dscp2index(dsfield);
303 
304 	/*
305 	 * update avg of the precedence states whose drop precedence
306 	 * is larger than or equal to the drop precedence of the packet
307 	 */
308 	now.tv_sec = 0;
309 	for (i = dpindex; i < RIO_NDROPPREC; i++) {
310 		prec = &rp->rio_precstate[i];
311 		avg = prec->avg;
312 		if (prec->idle) {
313 			prec->idle = 0;
314 			if (now.tv_sec == 0)
315 				microtime(&now);
316 			t = (now.tv_sec - prec->last.tv_sec);
317 			if (t > 60)
318 				avg = 0;
319 			else {
320 				t = t * 1000000 +
321 					(now.tv_usec - prec->last.tv_usec);
322 				n = t / rp->rio_pkttime;
323 				/* calculate (avg = (1 - Wq)^n * avg) */
324 				if (n > 0)
325 					avg = (avg >> FP_SHIFT) *
326 						pow_w(rp->rio_wtab, n);
327 			}
328 		}
329 
330 		/* run estimator. (avg is scaled by WEIGHT in fixed-point) */
331 		avg += (prec->qlen << FP_SHIFT) - (avg >> rp->rio_wshift);
332 		prec->avg = avg;		/* save the new value */
333 		/*
334 		 * count keeps a tally of arriving traffic that has not
335 		 * been dropped.
336 		 */
337 		prec->count++;
338 	}
339 
340 	prec = &rp->rio_precstate[dpindex];
341 	avg = prec->avg;
342 
343 	/* see if we drop early */
344 	droptype = DTYPE_NODROP;
345 	if (avg >= prec->th_min_s && prec->qlen > 1) {
346 		if (avg >= prec->th_max_s) {
347 			/* avg >= th_max: forced drop */
348 			droptype = DTYPE_FORCED;
349 		} else if (prec->old == 0) {
350 			/* first exceeds th_min */
351 			prec->count = 1;
352 			prec->old = 1;
353 		} else if (drop_early((avg - prec->th_min_s) >> rp->rio_wshift,
354 				      prec->probd, prec->count)) {
355 			/* unforced drop by red */
356 			droptype = DTYPE_EARLY;
357 		}
358 	} else {
359 		/* avg < th_min */
360 		prec->old = 0;
361 	}
362 
363 	/*
364 	 * if the queue length hits the hard limit, it's a forced drop.
365 	 */
366 	if (droptype == DTYPE_NODROP && qlen(q) >= qlimit(q))
367 		droptype = DTYPE_FORCED;
368 
369 	if (droptype != DTYPE_NODROP) {
370 		/* always drop incoming packet (as opposed to randomdrop) */
371 		for (i = dpindex; i < RIO_NDROPPREC; i++)
372 			rp->rio_precstate[i].count = 0;
373 #ifdef RIO_STATS
374 		if (droptype == DTYPE_EARLY)
375 			rp->q_stats[dpindex].drop_unforced++;
376 		else
377 			rp->q_stats[dpindex].drop_forced++;
378 		PKTCNTR_ADD(&rp->q_stats[dpindex].drop_cnt, m_pktlen(m));
379 #endif
380 		m_freem(m);
381 		return (-1);
382 	}
383 
384 	for (i = dpindex; i < RIO_NDROPPREC; i++)
385 		rp->rio_precstate[i].qlen++;
386 
387 	/* save drop precedence index in mbuf hdr */
388 	RIOM_SET_PRECINDEX(m, dpindex);
389 
390 	if (rp->rio_flags & RIOF_CLEARDSCP)
391 		dsfield &= ~DSCP_MASK;
392 
393 	if (dsfield != odsfield)
394 		write_dsfield(m, pktattr, dsfield);
395 
396 	_addq(q, m);
397 
398 #ifdef RIO_STATS
399 	PKTCNTR_ADD(&rp->q_stats[dpindex].xmit_cnt, m_pktlen(m));
400 #endif
401 	return (0);
402 }
403 
404 struct mbuf *
405 rio_getq(rio_t *rp, class_queue_t *q)
406 {
407 	struct mbuf *m;
408 	int dpindex, i;
409 
410 	if ((m = _getq(q)) == NULL)
411 		return (NULL);
412 
413 	dpindex = RIOM_GET_PRECINDEX(m);
414 	for (i = dpindex; i < RIO_NDROPPREC; i++) {
415 		if (--rp->rio_precstate[i].qlen == 0) {
416 			if (rp->rio_precstate[i].idle == 0) {
417 				rp->rio_precstate[i].idle = 1;
418 				microtime(&rp->rio_precstate[i].last);
419 			}
420 		}
421 	}
422 	return (m);
423 }
424 
425 #endif /* ALTQ_RIO */
426