xref: /freebsd/sys/net/altq/altq_rio.c (revision 2ff63af9b88c7413b7d71715b5532625752a248e)
1da8ae05dSGleb Smirnoff /*-
2772e66a6SGleb Smirnoff  * Copyright (C) 1998-2003
3772e66a6SGleb Smirnoff  *	Sony Computer Science Laboratories Inc.  All rights reserved.
4772e66a6SGleb Smirnoff  *
5772e66a6SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
6772e66a6SGleb Smirnoff  * modification, are permitted provided that the following conditions
7772e66a6SGleb Smirnoff  * are met:
8772e66a6SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
9772e66a6SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
10772e66a6SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
11772e66a6SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
12772e66a6SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
13772e66a6SGleb Smirnoff  *
14772e66a6SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
15772e66a6SGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16772e66a6SGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17772e66a6SGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
18772e66a6SGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19772e66a6SGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20772e66a6SGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21772e66a6SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22772e66a6SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23772e66a6SGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24772e66a6SGleb Smirnoff  * SUCH DAMAGE.
25772e66a6SGleb Smirnoff  */
26da8ae05dSGleb Smirnoff /*-
27772e66a6SGleb Smirnoff  * Copyright (c) 1990-1994 Regents of the University of California.
28772e66a6SGleb Smirnoff  * All rights reserved.
29772e66a6SGleb Smirnoff  *
30772e66a6SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
31772e66a6SGleb Smirnoff  * modification, are permitted provided that the following conditions
32772e66a6SGleb Smirnoff  * are met:
33772e66a6SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
34772e66a6SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
35772e66a6SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
36772e66a6SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
37772e66a6SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
38772e66a6SGleb Smirnoff  * 3. All advertising materials mentioning features or use of this software
39772e66a6SGleb Smirnoff  *    must display the following acknowledgement:
40772e66a6SGleb Smirnoff  *	This product includes software developed by the Computer Systems
41772e66a6SGleb Smirnoff  *	Engineering Group at Lawrence Berkeley Laboratory.
42772e66a6SGleb Smirnoff  * 4. Neither the name of the University nor of the Laboratory may be used
43772e66a6SGleb Smirnoff  *    to endorse or promote products derived from this software without
44772e66a6SGleb Smirnoff  *    specific prior written permission.
45772e66a6SGleb Smirnoff  *
46772e66a6SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47772e66a6SGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48772e66a6SGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49772e66a6SGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50772e66a6SGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51772e66a6SGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52772e66a6SGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53772e66a6SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54772e66a6SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55772e66a6SGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56772e66a6SGleb Smirnoff  * SUCH DAMAGE.
57da8ae05dSGleb Smirnoff  *
58da8ae05dSGleb Smirnoff  * $KAME: altq_rio.c,v 1.17 2003/07/10 12:07:49 kjc Exp $
59772e66a6SGleb Smirnoff  */
60772e66a6SGleb Smirnoff 
61772e66a6SGleb Smirnoff #include "opt_altq.h"
62772e66a6SGleb Smirnoff #include "opt_inet.h"
63772e66a6SGleb Smirnoff #include "opt_inet6.h"
64772e66a6SGleb Smirnoff #ifdef ALTQ_RIO	/* rio is enabled by ALTQ_RIO option in opt_altq.h */
65772e66a6SGleb Smirnoff 
66772e66a6SGleb Smirnoff #include <sys/param.h>
67772e66a6SGleb Smirnoff #include <sys/malloc.h>
68772e66a6SGleb Smirnoff #include <sys/mbuf.h>
69772e66a6SGleb Smirnoff #include <sys/socket.h>
70772e66a6SGleb Smirnoff #include <sys/systm.h>
71772e66a6SGleb Smirnoff #include <sys/errno.h>
72772e66a6SGleb Smirnoff #if 1 /* ALTQ3_COMPAT */
73772e66a6SGleb Smirnoff #include <sys/proc.h>
74772e66a6SGleb Smirnoff #include <sys/sockio.h>
75772e66a6SGleb Smirnoff #include <sys/kernel.h>
76772e66a6SGleb Smirnoff #endif
77772e66a6SGleb Smirnoff 
78772e66a6SGleb Smirnoff #include <net/if.h>
79772e66a6SGleb Smirnoff #include <net/if_var.h>
80772e66a6SGleb Smirnoff 
81772e66a6SGleb Smirnoff #include <netinet/in.h>
82772e66a6SGleb Smirnoff #include <netinet/in_systm.h>
83772e66a6SGleb Smirnoff #include <netinet/ip.h>
84772e66a6SGleb Smirnoff #ifdef INET6
85772e66a6SGleb Smirnoff #include <netinet/ip6.h>
86772e66a6SGleb Smirnoff #endif
87772e66a6SGleb Smirnoff 
88772e66a6SGleb Smirnoff #include <netpfil/pf/pf.h>
89772e66a6SGleb Smirnoff #include <netpfil/pf/pf_altq.h>
90772e66a6SGleb Smirnoff #include <net/altq/altq.h>
91772e66a6SGleb Smirnoff #include <net/altq/altq_cdnr.h>
92772e66a6SGleb Smirnoff #include <net/altq/altq_red.h>
93772e66a6SGleb Smirnoff #include <net/altq/altq_rio.h>
94772e66a6SGleb Smirnoff 
95772e66a6SGleb Smirnoff /*
96772e66a6SGleb Smirnoff  * RIO: RED with IN/OUT bit
97772e66a6SGleb Smirnoff  *   described in
98772e66a6SGleb Smirnoff  *	"Explicit Allocation of Best Effort Packet Delivery Service"
99772e66a6SGleb Smirnoff  *	David D. Clark and Wenjia Fang, MIT Lab for Computer Science
100772e66a6SGleb Smirnoff  *	http://diffserv.lcs.mit.edu/Papers/exp-alloc-ddc-wf.{ps,pdf}
101772e66a6SGleb Smirnoff  *
102772e66a6SGleb Smirnoff  * this implementation is extended to support more than 2 drop precedence
103772e66a6SGleb Smirnoff  * values as described in RFC2597 (Assured Forwarding PHB Group).
104772e66a6SGleb Smirnoff  *
105772e66a6SGleb Smirnoff  */
106772e66a6SGleb Smirnoff /*
107772e66a6SGleb Smirnoff  * AF DS (differentiated service) codepoints.
108772e66a6SGleb Smirnoff  * (classes can be mapped to CBQ or H-FSC classes.)
109772e66a6SGleb Smirnoff  *
110772e66a6SGleb Smirnoff  *      0   1   2   3   4   5   6   7
111772e66a6SGleb Smirnoff  *    +---+---+---+---+---+---+---+---+
112772e66a6SGleb Smirnoff  *    |   CLASS   |DropPre| 0 |  CU   |
113772e66a6SGleb Smirnoff  *    +---+---+---+---+---+---+---+---+
114772e66a6SGleb Smirnoff  *
115772e66a6SGleb Smirnoff  *    class 1: 001
116772e66a6SGleb Smirnoff  *    class 2: 010
117772e66a6SGleb Smirnoff  *    class 3: 011
118772e66a6SGleb Smirnoff  *    class 4: 100
119772e66a6SGleb Smirnoff  *
120772e66a6SGleb Smirnoff  *    low drop prec:    01
121772e66a6SGleb Smirnoff  *    medium drop prec: 10
122772e66a6SGleb Smirnoff  *    high drop prec:   01
123772e66a6SGleb Smirnoff  */
124772e66a6SGleb Smirnoff 
125772e66a6SGleb Smirnoff /* normal red parameters */
126772e66a6SGleb Smirnoff #define	W_WEIGHT	512	/* inverse of weight of EWMA (511/512) */
127772e66a6SGleb Smirnoff 				/* q_weight = 0.00195 */
128772e66a6SGleb Smirnoff 
129772e66a6SGleb Smirnoff /* red parameters for a slow link */
130772e66a6SGleb Smirnoff #define	W_WEIGHT_1	128	/* inverse of weight of EWMA (127/128) */
131772e66a6SGleb Smirnoff 				/* q_weight = 0.0078125 */
132772e66a6SGleb Smirnoff 
133772e66a6SGleb Smirnoff /* red parameters for a very slow link (e.g., dialup) */
134772e66a6SGleb Smirnoff #define	W_WEIGHT_2	64	/* inverse of weight of EWMA (63/64) */
135772e66a6SGleb Smirnoff 				/* q_weight = 0.015625 */
136772e66a6SGleb Smirnoff 
137772e66a6SGleb Smirnoff /* fixed-point uses 12-bit decimal places */
138772e66a6SGleb Smirnoff #define	FP_SHIFT	12	/* fixed-point shift */
139772e66a6SGleb Smirnoff 
140772e66a6SGleb Smirnoff /* red parameters for drop probability */
141772e66a6SGleb Smirnoff #define	INV_P_MAX	10	/* inverse of max drop probability */
142772e66a6SGleb Smirnoff #define	TH_MIN		 5	/* min threshold */
143772e66a6SGleb Smirnoff #define	TH_MAX		15	/* max threshold */
144772e66a6SGleb Smirnoff 
145a4641f4eSPedro F. Giffuni #define	RIO_LIMIT	60	/* default max queue length */
146772e66a6SGleb Smirnoff #define	RIO_STATS		/* collect statistics */
147772e66a6SGleb Smirnoff 
148772e66a6SGleb Smirnoff #define	TV_DELTA(a, b, delta) {					\
149*3e85b721SEd Maste 	int	xxs;						\
150772e66a6SGleb Smirnoff 								\
151772e66a6SGleb Smirnoff 	delta = (a)->tv_usec - (b)->tv_usec; 			\
152772e66a6SGleb Smirnoff 	if ((xxs = (a)->tv_sec - (b)->tv_sec) != 0) { 		\
153772e66a6SGleb Smirnoff 		if (xxs < 0) { 					\
154772e66a6SGleb Smirnoff 			delta = 60000000;			\
155772e66a6SGleb Smirnoff 		} else if (xxs > 4)  {				\
156772e66a6SGleb Smirnoff 			if (xxs > 60)				\
157772e66a6SGleb Smirnoff 				delta = 60000000;		\
158772e66a6SGleb Smirnoff 			else					\
159772e66a6SGleb Smirnoff 				delta += xxs * 1000000;		\
160772e66a6SGleb Smirnoff 		} else while (xxs > 0) {			\
161772e66a6SGleb Smirnoff 			delta += 1000000;			\
162772e66a6SGleb Smirnoff 			xxs--;					\
163772e66a6SGleb Smirnoff 		}						\
164772e66a6SGleb Smirnoff 	}							\
165772e66a6SGleb Smirnoff }
166772e66a6SGleb Smirnoff 
167772e66a6SGleb Smirnoff /* default rio parameter values */
168772e66a6SGleb Smirnoff static struct redparams default_rio_params[RIO_NDROPPREC] = {
169772e66a6SGleb Smirnoff   /* th_min,		 th_max,     inv_pmax */
170772e66a6SGleb Smirnoff   { TH_MAX * 2 + TH_MIN, TH_MAX * 3, INV_P_MAX }, /* low drop precedence */
171772e66a6SGleb Smirnoff   { TH_MAX + TH_MIN,	 TH_MAX * 2, INV_P_MAX }, /* medium drop precedence */
172772e66a6SGleb Smirnoff   { TH_MIN,		 TH_MAX,     INV_P_MAX }  /* high drop precedence */
173772e66a6SGleb Smirnoff };
174772e66a6SGleb Smirnoff 
175772e66a6SGleb Smirnoff /* internal function prototypes */
176772e66a6SGleb Smirnoff static int dscp2index(u_int8_t);
177772e66a6SGleb Smirnoff 
178772e66a6SGleb Smirnoff rio_t *
rio_alloc(int weight,struct redparams * params,int flags,int pkttime)179772e66a6SGleb Smirnoff rio_alloc(int weight, struct redparams *params, int flags, int pkttime)
180772e66a6SGleb Smirnoff {
181772e66a6SGleb Smirnoff 	rio_t	*rp;
182772e66a6SGleb Smirnoff 	int	 w, i;
183772e66a6SGleb Smirnoff 	int	 npkts_per_sec;
184772e66a6SGleb Smirnoff 
185772e66a6SGleb Smirnoff 	rp = malloc(sizeof(rio_t), M_DEVBUF, M_NOWAIT | M_ZERO);
186772e66a6SGleb Smirnoff 	if (rp == NULL)
187772e66a6SGleb Smirnoff 		return (NULL);
188772e66a6SGleb Smirnoff 
189772e66a6SGleb Smirnoff 	rp->rio_flags = flags;
190772e66a6SGleb Smirnoff 	if (pkttime == 0)
191772e66a6SGleb Smirnoff 		/* default packet time: 1000 bytes / 10Mbps * 8 * 1000000 */
192772e66a6SGleb Smirnoff 		rp->rio_pkttime = 800;
193772e66a6SGleb Smirnoff 	else
194772e66a6SGleb Smirnoff 		rp->rio_pkttime = pkttime;
195772e66a6SGleb Smirnoff 
196772e66a6SGleb Smirnoff 	if (weight != 0)
197772e66a6SGleb Smirnoff 		rp->rio_weight = weight;
198772e66a6SGleb Smirnoff 	else {
199772e66a6SGleb Smirnoff 		/* use default */
200772e66a6SGleb Smirnoff 		rp->rio_weight = W_WEIGHT;
201772e66a6SGleb Smirnoff 
202772e66a6SGleb Smirnoff 		/* when the link is very slow, adjust red parameters */
203772e66a6SGleb Smirnoff 		npkts_per_sec = 1000000 / rp->rio_pkttime;
204772e66a6SGleb Smirnoff 		if (npkts_per_sec < 50) {
205772e66a6SGleb Smirnoff 			/* up to about 400Kbps */
206772e66a6SGleb Smirnoff 			rp->rio_weight = W_WEIGHT_2;
207772e66a6SGleb Smirnoff 		} else if (npkts_per_sec < 300) {
208772e66a6SGleb Smirnoff 			/* up to about 2.4Mbps */
209772e66a6SGleb Smirnoff 			rp->rio_weight = W_WEIGHT_1;
210772e66a6SGleb Smirnoff 		}
211772e66a6SGleb Smirnoff 	}
212772e66a6SGleb Smirnoff 
213772e66a6SGleb Smirnoff 	/* calculate wshift.  weight must be power of 2 */
214772e66a6SGleb Smirnoff 	w = rp->rio_weight;
215772e66a6SGleb Smirnoff 	for (i = 0; w > 1; i++)
216772e66a6SGleb Smirnoff 		w = w >> 1;
217772e66a6SGleb Smirnoff 	rp->rio_wshift = i;
218772e66a6SGleb Smirnoff 	w = 1 << rp->rio_wshift;
219772e66a6SGleb Smirnoff 	if (w != rp->rio_weight) {
220772e66a6SGleb Smirnoff 		printf("invalid weight value %d for red! use %d\n",
221772e66a6SGleb Smirnoff 		       rp->rio_weight, w);
222772e66a6SGleb Smirnoff 		rp->rio_weight = w;
223772e66a6SGleb Smirnoff 	}
224772e66a6SGleb Smirnoff 
225772e66a6SGleb Smirnoff 	/* allocate weight table */
226772e66a6SGleb Smirnoff 	rp->rio_wtab = wtab_alloc(rp->rio_weight);
227772e66a6SGleb Smirnoff 
228772e66a6SGleb Smirnoff 	for (i = 0; i < RIO_NDROPPREC; i++) {
229772e66a6SGleb Smirnoff 		struct dropprec_state *prec = &rp->rio_precstate[i];
230772e66a6SGleb Smirnoff 
231772e66a6SGleb Smirnoff 		prec->avg = 0;
232772e66a6SGleb Smirnoff 		prec->idle = 1;
233772e66a6SGleb Smirnoff 
234772e66a6SGleb Smirnoff 		if (params == NULL || params[i].inv_pmax == 0)
235772e66a6SGleb Smirnoff 			prec->inv_pmax = default_rio_params[i].inv_pmax;
236772e66a6SGleb Smirnoff 		else
237772e66a6SGleb Smirnoff 			prec->inv_pmax = params[i].inv_pmax;
238772e66a6SGleb Smirnoff 		if (params == NULL || params[i].th_min == 0)
239772e66a6SGleb Smirnoff 			prec->th_min = default_rio_params[i].th_min;
240772e66a6SGleb Smirnoff 		else
241772e66a6SGleb Smirnoff 			prec->th_min = params[i].th_min;
242772e66a6SGleb Smirnoff 		if (params == NULL || params[i].th_max == 0)
243772e66a6SGleb Smirnoff 			prec->th_max = default_rio_params[i].th_max;
244772e66a6SGleb Smirnoff 		else
245772e66a6SGleb Smirnoff 			prec->th_max = params[i].th_max;
246772e66a6SGleb Smirnoff 
247772e66a6SGleb Smirnoff 		/*
248772e66a6SGleb Smirnoff 		 * th_min_s and th_max_s are scaled versions of th_min
249772e66a6SGleb Smirnoff 		 * and th_max to be compared with avg.
250772e66a6SGleb Smirnoff 		 */
251772e66a6SGleb Smirnoff 		prec->th_min_s = prec->th_min << (rp->rio_wshift + FP_SHIFT);
252772e66a6SGleb Smirnoff 		prec->th_max_s = prec->th_max << (rp->rio_wshift + FP_SHIFT);
253772e66a6SGleb Smirnoff 
254772e66a6SGleb Smirnoff 		/*
255772e66a6SGleb Smirnoff 		 * precompute probability denominator
256772e66a6SGleb Smirnoff 		 *  probd = (2 * (TH_MAX-TH_MIN) / pmax) in fixed-point
257772e66a6SGleb Smirnoff 		 */
258772e66a6SGleb Smirnoff 		prec->probd = (2 * (prec->th_max - prec->th_min)
259772e66a6SGleb Smirnoff 			       * prec->inv_pmax) << FP_SHIFT;
260772e66a6SGleb Smirnoff 
261772e66a6SGleb Smirnoff 		microtime(&prec->last);
262772e66a6SGleb Smirnoff 	}
263772e66a6SGleb Smirnoff 
264772e66a6SGleb Smirnoff 	return (rp);
265772e66a6SGleb Smirnoff }
266772e66a6SGleb Smirnoff 
267772e66a6SGleb Smirnoff void
rio_destroy(rio_t * rp)268772e66a6SGleb Smirnoff rio_destroy(rio_t *rp)
269772e66a6SGleb Smirnoff {
270772e66a6SGleb Smirnoff 	wtab_destroy(rp->rio_wtab);
271772e66a6SGleb Smirnoff 	free(rp, M_DEVBUF);
272772e66a6SGleb Smirnoff }
273772e66a6SGleb Smirnoff 
274772e66a6SGleb Smirnoff void
rio_getstats(rio_t * rp,struct redstats * sp)275772e66a6SGleb Smirnoff rio_getstats(rio_t *rp, struct redstats *sp)
276772e66a6SGleb Smirnoff {
277772e66a6SGleb Smirnoff 	int	i;
278772e66a6SGleb Smirnoff 
279772e66a6SGleb Smirnoff 	for (i = 0; i < RIO_NDROPPREC; i++) {
280772e66a6SGleb Smirnoff 		bcopy(&rp->q_stats[i], sp, sizeof(struct redstats));
281772e66a6SGleb Smirnoff 		sp->q_avg = rp->rio_precstate[i].avg >> rp->rio_wshift;
282772e66a6SGleb Smirnoff 		sp++;
283772e66a6SGleb Smirnoff 	}
284772e66a6SGleb Smirnoff }
285772e66a6SGleb Smirnoff 
286772e66a6SGleb Smirnoff #if (RIO_NDROPPREC == 3)
287772e66a6SGleb Smirnoff /*
288772e66a6SGleb Smirnoff  * internally, a drop precedence value is converted to an index
289772e66a6SGleb Smirnoff  * starting from 0.
290772e66a6SGleb Smirnoff  */
291772e66a6SGleb Smirnoff static int
dscp2index(u_int8_t dscp)292772e66a6SGleb Smirnoff dscp2index(u_int8_t dscp)
293772e66a6SGleb Smirnoff {
294772e66a6SGleb Smirnoff 	int	dpindex = dscp & AF_DROPPRECMASK;
295772e66a6SGleb Smirnoff 
296772e66a6SGleb Smirnoff 	if (dpindex == 0)
297772e66a6SGleb Smirnoff 		return (0);
298772e66a6SGleb Smirnoff 	return ((dpindex >> 3) - 1);
299772e66a6SGleb Smirnoff }
300772e66a6SGleb Smirnoff #endif
301772e66a6SGleb Smirnoff 
302772e66a6SGleb Smirnoff #if 1
303772e66a6SGleb Smirnoff /*
304772e66a6SGleb Smirnoff  * kludge: when a packet is dequeued, we need to know its drop precedence
305772e66a6SGleb Smirnoff  * in order to keep the queue length of each drop precedence.
306772e66a6SGleb Smirnoff  * use m_pkthdr.rcvif to pass this info.
307772e66a6SGleb Smirnoff  */
308772e66a6SGleb Smirnoff #define	RIOM_SET_PRECINDEX(m, idx)	\
309772e66a6SGleb Smirnoff 	do { (m)->m_pkthdr.rcvif = (void *)((long)(idx)); } while (0)
310772e66a6SGleb Smirnoff #define	RIOM_GET_PRECINDEX(m)	\
311772e66a6SGleb Smirnoff 	({ long idx; idx = (long)((m)->m_pkthdr.rcvif); \
312772e66a6SGleb Smirnoff 	(m)->m_pkthdr.rcvif = NULL; idx; })
313772e66a6SGleb Smirnoff #endif
314772e66a6SGleb Smirnoff 
315772e66a6SGleb Smirnoff int
rio_addq(rio_t * rp,class_queue_t * q,struct mbuf * m,struct altq_pktattr * pktattr)316772e66a6SGleb Smirnoff rio_addq(rio_t *rp, class_queue_t *q, struct mbuf *m,
317772e66a6SGleb Smirnoff     struct altq_pktattr *pktattr)
318772e66a6SGleb Smirnoff {
319772e66a6SGleb Smirnoff 	int			 avg, droptype;
320772e66a6SGleb Smirnoff 	u_int8_t		 dsfield, odsfield;
321772e66a6SGleb Smirnoff 	int			 dpindex, i, n, t;
322772e66a6SGleb Smirnoff 	struct timeval		 now;
323772e66a6SGleb Smirnoff 	struct dropprec_state	*prec;
324772e66a6SGleb Smirnoff 
325772e66a6SGleb Smirnoff 	dsfield = odsfield = read_dsfield(m, pktattr);
326772e66a6SGleb Smirnoff 	dpindex = dscp2index(dsfield);
327772e66a6SGleb Smirnoff 
328772e66a6SGleb Smirnoff 	/*
329772e66a6SGleb Smirnoff 	 * update avg of the precedence states whose drop precedence
330772e66a6SGleb Smirnoff 	 * is larger than or equal to the drop precedence of the packet
331772e66a6SGleb Smirnoff 	 */
332772e66a6SGleb Smirnoff 	now.tv_sec = 0;
333772e66a6SGleb Smirnoff 	for (i = dpindex; i < RIO_NDROPPREC; i++) {
334772e66a6SGleb Smirnoff 		prec = &rp->rio_precstate[i];
335772e66a6SGleb Smirnoff 		avg = prec->avg;
336772e66a6SGleb Smirnoff 		if (prec->idle) {
337772e66a6SGleb Smirnoff 			prec->idle = 0;
338772e66a6SGleb Smirnoff 			if (now.tv_sec == 0)
339772e66a6SGleb Smirnoff 				microtime(&now);
340772e66a6SGleb Smirnoff 			t = (now.tv_sec - prec->last.tv_sec);
341772e66a6SGleb Smirnoff 			if (t > 60)
342772e66a6SGleb Smirnoff 				avg = 0;
343772e66a6SGleb Smirnoff 			else {
344772e66a6SGleb Smirnoff 				t = t * 1000000 +
345772e66a6SGleb Smirnoff 					(now.tv_usec - prec->last.tv_usec);
346772e66a6SGleb Smirnoff 				n = t / rp->rio_pkttime;
347772e66a6SGleb Smirnoff 				/* calculate (avg = (1 - Wq)^n * avg) */
348772e66a6SGleb Smirnoff 				if (n > 0)
349772e66a6SGleb Smirnoff 					avg = (avg >> FP_SHIFT) *
350772e66a6SGleb Smirnoff 						pow_w(rp->rio_wtab, n);
351772e66a6SGleb Smirnoff 			}
352772e66a6SGleb Smirnoff 		}
353772e66a6SGleb Smirnoff 
354772e66a6SGleb Smirnoff 		/* run estimator. (avg is scaled by WEIGHT in fixed-point) */
355772e66a6SGleb Smirnoff 		avg += (prec->qlen << FP_SHIFT) - (avg >> rp->rio_wshift);
356772e66a6SGleb Smirnoff 		prec->avg = avg;		/* save the new value */
357772e66a6SGleb Smirnoff 		/*
358772e66a6SGleb Smirnoff 		 * count keeps a tally of arriving traffic that has not
359772e66a6SGleb Smirnoff 		 * been dropped.
360772e66a6SGleb Smirnoff 		 */
361772e66a6SGleb Smirnoff 		prec->count++;
362772e66a6SGleb Smirnoff 	}
363772e66a6SGleb Smirnoff 
364772e66a6SGleb Smirnoff 	prec = &rp->rio_precstate[dpindex];
365772e66a6SGleb Smirnoff 	avg = prec->avg;
366772e66a6SGleb Smirnoff 
367772e66a6SGleb Smirnoff 	/* see if we drop early */
368772e66a6SGleb Smirnoff 	droptype = DTYPE_NODROP;
369772e66a6SGleb Smirnoff 	if (avg >= prec->th_min_s && prec->qlen > 1) {
370772e66a6SGleb Smirnoff 		if (avg >= prec->th_max_s) {
371772e66a6SGleb Smirnoff 			/* avg >= th_max: forced drop */
372772e66a6SGleb Smirnoff 			droptype = DTYPE_FORCED;
373772e66a6SGleb Smirnoff 		} else if (prec->old == 0) {
374772e66a6SGleb Smirnoff 			/* first exceeds th_min */
375772e66a6SGleb Smirnoff 			prec->count = 1;
376772e66a6SGleb Smirnoff 			prec->old = 1;
377772e66a6SGleb Smirnoff 		} else if (drop_early((avg - prec->th_min_s) >> rp->rio_wshift,
378772e66a6SGleb Smirnoff 				      prec->probd, prec->count)) {
379772e66a6SGleb Smirnoff 			/* unforced drop by red */
380772e66a6SGleb Smirnoff 			droptype = DTYPE_EARLY;
381772e66a6SGleb Smirnoff 		}
382772e66a6SGleb Smirnoff 	} else {
383772e66a6SGleb Smirnoff 		/* avg < th_min */
384772e66a6SGleb Smirnoff 		prec->old = 0;
385772e66a6SGleb Smirnoff 	}
386772e66a6SGleb Smirnoff 
387772e66a6SGleb Smirnoff 	/*
388772e66a6SGleb Smirnoff 	 * if the queue length hits the hard limit, it's a forced drop.
389772e66a6SGleb Smirnoff 	 */
390772e66a6SGleb Smirnoff 	if (droptype == DTYPE_NODROP && qlen(q) >= qlimit(q))
391772e66a6SGleb Smirnoff 		droptype = DTYPE_FORCED;
392772e66a6SGleb Smirnoff 
393772e66a6SGleb Smirnoff 	if (droptype != DTYPE_NODROP) {
394772e66a6SGleb Smirnoff 		/* always drop incoming packet (as opposed to randomdrop) */
395772e66a6SGleb Smirnoff 		for (i = dpindex; i < RIO_NDROPPREC; i++)
396772e66a6SGleb Smirnoff 			rp->rio_precstate[i].count = 0;
397772e66a6SGleb Smirnoff #ifdef RIO_STATS
398772e66a6SGleb Smirnoff 		if (droptype == DTYPE_EARLY)
399772e66a6SGleb Smirnoff 			rp->q_stats[dpindex].drop_unforced++;
400772e66a6SGleb Smirnoff 		else
401772e66a6SGleb Smirnoff 			rp->q_stats[dpindex].drop_forced++;
402772e66a6SGleb Smirnoff 		PKTCNTR_ADD(&rp->q_stats[dpindex].drop_cnt, m_pktlen(m));
403772e66a6SGleb Smirnoff #endif
404772e66a6SGleb Smirnoff 		m_freem(m);
405772e66a6SGleb Smirnoff 		return (-1);
406772e66a6SGleb Smirnoff 	}
407772e66a6SGleb Smirnoff 
408772e66a6SGleb Smirnoff 	for (i = dpindex; i < RIO_NDROPPREC; i++)
409772e66a6SGleb Smirnoff 		rp->rio_precstate[i].qlen++;
410772e66a6SGleb Smirnoff 
411772e66a6SGleb Smirnoff 	/* save drop precedence index in mbuf hdr */
412772e66a6SGleb Smirnoff 	RIOM_SET_PRECINDEX(m, dpindex);
413772e66a6SGleb Smirnoff 
414772e66a6SGleb Smirnoff 	if (rp->rio_flags & RIOF_CLEARDSCP)
415772e66a6SGleb Smirnoff 		dsfield &= ~DSCP_MASK;
416772e66a6SGleb Smirnoff 
417772e66a6SGleb Smirnoff 	if (dsfield != odsfield)
418772e66a6SGleb Smirnoff 		write_dsfield(m, pktattr, dsfield);
419772e66a6SGleb Smirnoff 
420772e66a6SGleb Smirnoff 	_addq(q, m);
421772e66a6SGleb Smirnoff 
422772e66a6SGleb Smirnoff #ifdef RIO_STATS
423772e66a6SGleb Smirnoff 	PKTCNTR_ADD(&rp->q_stats[dpindex].xmit_cnt, m_pktlen(m));
424772e66a6SGleb Smirnoff #endif
425772e66a6SGleb Smirnoff 	return (0);
426772e66a6SGleb Smirnoff }
427772e66a6SGleb Smirnoff 
428772e66a6SGleb Smirnoff struct mbuf *
rio_getq(rio_t * rp,class_queue_t * q)429772e66a6SGleb Smirnoff rio_getq(rio_t *rp, class_queue_t *q)
430772e66a6SGleb Smirnoff {
431772e66a6SGleb Smirnoff 	struct mbuf	*m;
432772e66a6SGleb Smirnoff 	int		 dpindex, i;
433772e66a6SGleb Smirnoff 
434772e66a6SGleb Smirnoff 	if ((m = _getq(q)) == NULL)
435772e66a6SGleb Smirnoff 		return NULL;
436772e66a6SGleb Smirnoff 
437772e66a6SGleb Smirnoff 	dpindex = RIOM_GET_PRECINDEX(m);
438772e66a6SGleb Smirnoff 	for (i = dpindex; i < RIO_NDROPPREC; i++) {
439772e66a6SGleb Smirnoff 		if (--rp->rio_precstate[i].qlen == 0) {
440772e66a6SGleb Smirnoff 			if (rp->rio_precstate[i].idle == 0) {
441772e66a6SGleb Smirnoff 				rp->rio_precstate[i].idle = 1;
442772e66a6SGleb Smirnoff 				microtime(&rp->rio_precstate[i].last);
443772e66a6SGleb Smirnoff 			}
444772e66a6SGleb Smirnoff 		}
445772e66a6SGleb Smirnoff 	}
446772e66a6SGleb Smirnoff 	return (m);
447772e66a6SGleb Smirnoff }
448772e66a6SGleb Smirnoff 
449772e66a6SGleb Smirnoff #endif /* ALTQ_RIO */
450