altq_rio.c revision 1.17 1 1.17 christos /* $NetBSD: altq_rio.c,v 1.17 2006/11/16 01:32:37 christos Exp $ */
2 1.14 peter /* $KAME: altq_rio.c,v 1.19 2005/04/13 03:44:25 suz Exp $ */
3 1.1 thorpej
4 1.1 thorpej /*
5 1.14 peter * Copyright (C) 1998-2003
6 1.1 thorpej * Sony Computer Science Laboratories Inc. All rights reserved.
7 1.1 thorpej *
8 1.1 thorpej * Redistribution and use in source and binary forms, with or without
9 1.1 thorpej * modification, are permitted provided that the following conditions
10 1.1 thorpej * are met:
11 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
12 1.1 thorpej * notice, this list of conditions and the following disclaimer.
13 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
15 1.1 thorpej * documentation and/or other materials provided with the distribution.
16 1.1 thorpej *
17 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
18 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
21 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 thorpej * SUCH DAMAGE.
28 1.1 thorpej */
29 1.1 thorpej /*
30 1.1 thorpej * Copyright (c) 1990-1994 Regents of the University of California.
31 1.1 thorpej * All rights reserved.
32 1.1 thorpej *
33 1.1 thorpej * Redistribution and use in source and binary forms, with or without
34 1.1 thorpej * modification, are permitted provided that the following conditions
35 1.1 thorpej * are met:
36 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
37 1.1 thorpej * notice, this list of conditions and the following disclaimer.
38 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
39 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
40 1.1 thorpej * documentation and/or other materials provided with the distribution.
41 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
42 1.1 thorpej * must display the following acknowledgement:
43 1.1 thorpej * This product includes software developed by the Computer Systems
44 1.1 thorpej * Engineering Group at Lawrence Berkeley Laboratory.
45 1.1 thorpej * 4. Neither the name of the University nor of the Laboratory may be used
46 1.1 thorpej * to endorse or promote products derived from this software without
47 1.1 thorpej * specific prior written permission.
48 1.1 thorpej *
49 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 1.1 thorpej * SUCH DAMAGE.
60 1.1 thorpej */
61 1.4 lukem
62 1.4 lukem #include <sys/cdefs.h>
63 1.17 christos __KERNEL_RCSID(0, "$NetBSD: altq_rio.c,v 1.17 2006/11/16 01:32:37 christos Exp $");
64 1.1 thorpej
65 1.14 peter #ifdef _KERNEL_OPT
66 1.1 thorpej #include "opt_altq.h"
67 1.1 thorpej #include "opt_inet.h"
68 1.15 peter #include "pf.h"
69 1.1 thorpej #endif
70 1.14 peter
71 1.1 thorpej #ifdef ALTQ_RIO /* rio is enabled by ALTQ_RIO option in opt_altq.h */
72 1.1 thorpej
73 1.1 thorpej #include <sys/param.h>
74 1.1 thorpej #include <sys/malloc.h>
75 1.1 thorpej #include <sys/mbuf.h>
76 1.1 thorpej #include <sys/socket.h>
77 1.1 thorpej #include <sys/systm.h>
78 1.14 peter #include <sys/errno.h>
79 1.14 peter #include <sys/kauth.h>
80 1.14 peter #if 1 /* ALTQ3_COMPAT */
81 1.1 thorpej #include <sys/proc.h>
82 1.14 peter #include <sys/sockio.h>
83 1.1 thorpej #include <sys/kernel.h>
84 1.14 peter #endif
85 1.11 christos #include <sys/kauth.h>
86 1.1 thorpej
87 1.1 thorpej #include <net/if.h>
88 1.1 thorpej
89 1.1 thorpej #include <netinet/in.h>
90 1.1 thorpej #include <netinet/in_systm.h>
91 1.1 thorpej #include <netinet/ip.h>
92 1.1 thorpej #ifdef INET6
93 1.1 thorpej #include <netinet/ip6.h>
94 1.1 thorpej #endif
95 1.1 thorpej
96 1.15 peter #if NPF > 0
97 1.14 peter #include <net/pfvar.h>
98 1.15 peter #endif
99 1.1 thorpej #include <altq/altq.h>
100 1.1 thorpej #include <altq/altq_cdnr.h>
101 1.1 thorpej #include <altq/altq_red.h>
102 1.1 thorpej #include <altq/altq_rio.h>
103 1.14 peter #ifdef ALTQ3_COMPAT
104 1.14 peter #include <altq/altq_conf.h>
105 1.14 peter #endif
106 1.1 thorpej
107 1.1 thorpej /*
108 1.1 thorpej * RIO: RED with IN/OUT bit
109 1.1 thorpej * described in
110 1.1 thorpej * "Explicit Allocation of Best Effort Packet Delivery Service"
111 1.1 thorpej * David D. Clark and Wenjia Fang, MIT Lab for Computer Science
112 1.1 thorpej * http://diffserv.lcs.mit.edu/Papers/exp-alloc-ddc-wf.{ps,pdf}
113 1.1 thorpej *
114 1.1 thorpej * this implementation is extended to support more than 2 drop precedence
115 1.1 thorpej * values as described in RFC2597 (Assured Forwarding PHB Group).
116 1.1 thorpej *
117 1.1 thorpej */
118 1.1 thorpej /*
119 1.1 thorpej * AF DS (differentiated service) codepoints.
120 1.1 thorpej * (classes can be mapped to CBQ or H-FSC classes.)
121 1.7 perry *
122 1.1 thorpej * 0 1 2 3 4 5 6 7
123 1.1 thorpej * +---+---+---+---+---+---+---+---+
124 1.1 thorpej * | CLASS |DropPre| 0 | CU |
125 1.1 thorpej * +---+---+---+---+---+---+---+---+
126 1.1 thorpej *
127 1.1 thorpej * class 1: 001
128 1.1 thorpej * class 2: 010
129 1.1 thorpej * class 3: 011
130 1.1 thorpej * class 4: 100
131 1.1 thorpej *
132 1.1 thorpej * low drop prec: 01
133 1.1 thorpej * medium drop prec: 10
134 1.14 peter * high drop prec: 11
135 1.1 thorpej */
136 1.1 thorpej
137 1.1 thorpej /* normal red parameters */
138 1.1 thorpej #define W_WEIGHT 512 /* inverse of weight of EWMA (511/512) */
139 1.1 thorpej /* q_weight = 0.00195 */
140 1.1 thorpej
141 1.1 thorpej /* red parameters for a slow link */
142 1.1 thorpej #define W_WEIGHT_1 128 /* inverse of weight of EWMA (127/128) */
143 1.1 thorpej /* q_weight = 0.0078125 */
144 1.1 thorpej
145 1.1 thorpej /* red parameters for a very slow link (e.g., dialup) */
146 1.1 thorpej #define W_WEIGHT_2 64 /* inverse of weight of EWMA (63/64) */
147 1.1 thorpej /* q_weight = 0.015625 */
148 1.1 thorpej
149 1.1 thorpej /* fixed-point uses 12-bit decimal places */
150 1.1 thorpej #define FP_SHIFT 12 /* fixed-point shift */
151 1.1 thorpej
152 1.1 thorpej /* red parameters for drop probability */
153 1.1 thorpej #define INV_P_MAX 10 /* inverse of max drop probability */
154 1.1 thorpej #define TH_MIN 5 /* min threshold */
155 1.1 thorpej #define TH_MAX 15 /* max threshold */
156 1.1 thorpej
157 1.14 peter #define RIO_LIMIT 60 /* default max queue lenght */
158 1.14 peter #define RIO_STATS /* collect statistics */
159 1.1 thorpej
160 1.1 thorpej #define TV_DELTA(a, b, delta) { \
161 1.1 thorpej register int xxs; \
162 1.1 thorpej \
163 1.1 thorpej delta = (a)->tv_usec - (b)->tv_usec; \
164 1.1 thorpej if ((xxs = (a)->tv_sec - (b)->tv_sec) != 0) { \
165 1.1 thorpej if (xxs < 0) { \
166 1.1 thorpej delta = 60000000; \
167 1.1 thorpej } else if (xxs > 4) { \
168 1.1 thorpej if (xxs > 60) \
169 1.1 thorpej delta = 60000000; \
170 1.1 thorpej else \
171 1.1 thorpej delta += xxs * 1000000; \
172 1.1 thorpej } else while (xxs > 0) { \
173 1.1 thorpej delta += 1000000; \
174 1.1 thorpej xxs--; \
175 1.1 thorpej } \
176 1.1 thorpej } \
177 1.1 thorpej }
178 1.1 thorpej
179 1.14 peter #ifdef ALTQ3_COMPAT
180 1.1 thorpej /* rio_list keeps all rio_queue_t's allocated. */
181 1.1 thorpej static rio_queue_t *rio_list = NULL;
182 1.14 peter #endif
183 1.1 thorpej /* default rio parameter values */
184 1.1 thorpej static struct redparams default_rio_params[RIO_NDROPPREC] = {
185 1.1 thorpej /* th_min, th_max, inv_pmax */
186 1.1 thorpej { TH_MAX * 2 + TH_MIN, TH_MAX * 3, INV_P_MAX }, /* low drop precedence */
187 1.1 thorpej { TH_MAX + TH_MIN, TH_MAX * 2, INV_P_MAX }, /* medium drop precedence */
188 1.1 thorpej { TH_MIN, TH_MAX, INV_P_MAX } /* high drop precedence */
189 1.1 thorpej };
190 1.1 thorpej
191 1.1 thorpej /* internal function prototypes */
192 1.14 peter static int dscp2index(u_int8_t);
193 1.14 peter #ifdef ALTQ3_COMPAT
194 1.14 peter static int rio_enqueue(struct ifaltq *, struct mbuf *, struct altq_pktattr *);
195 1.14 peter static struct mbuf *rio_dequeue(struct ifaltq *, int);
196 1.14 peter static int rio_request(struct ifaltq *, int, void *);
197 1.14 peter static int rio_detach(rio_queue_t *);
198 1.14 peter
199 1.14 peter /*
200 1.14 peter * rio device interface
201 1.14 peter */
202 1.14 peter altqdev_decl(rio);
203 1.14 peter
204 1.14 peter #endif /* ALTQ3_COMPAT */
205 1.14 peter
206 1.14 peter rio_t *
207 1.14 peter rio_alloc(int weight, struct redparams *params, int flags, int pkttime)
208 1.14 peter {
209 1.14 peter rio_t *rp;
210 1.14 peter int w, i;
211 1.14 peter int npkts_per_sec;
212 1.14 peter
213 1.14 peter rp = malloc(sizeof(rio_t), M_DEVBUF, M_WAITOK|M_ZERO);
214 1.14 peter if (rp == NULL)
215 1.14 peter return (NULL);
216 1.14 peter
217 1.14 peter rp->rio_flags = flags;
218 1.14 peter if (pkttime == 0)
219 1.14 peter /* default packet time: 1000 bytes / 10Mbps * 8 * 1000000 */
220 1.14 peter rp->rio_pkttime = 800;
221 1.14 peter else
222 1.14 peter rp->rio_pkttime = pkttime;
223 1.14 peter
224 1.14 peter if (weight != 0)
225 1.14 peter rp->rio_weight = weight;
226 1.14 peter else {
227 1.14 peter /* use default */
228 1.14 peter rp->rio_weight = W_WEIGHT;
229 1.14 peter
230 1.14 peter /* when the link is very slow, adjust red parameters */
231 1.14 peter npkts_per_sec = 1000000 / rp->rio_pkttime;
232 1.14 peter if (npkts_per_sec < 50) {
233 1.14 peter /* up to about 400Kbps */
234 1.14 peter rp->rio_weight = W_WEIGHT_2;
235 1.14 peter } else if (npkts_per_sec < 300) {
236 1.14 peter /* up to about 2.4Mbps */
237 1.14 peter rp->rio_weight = W_WEIGHT_1;
238 1.14 peter }
239 1.14 peter }
240 1.14 peter
241 1.14 peter /* calculate wshift. weight must be power of 2 */
242 1.14 peter w = rp->rio_weight;
243 1.14 peter for (i = 0; w > 1; i++)
244 1.14 peter w = w >> 1;
245 1.14 peter rp->rio_wshift = i;
246 1.14 peter w = 1 << rp->rio_wshift;
247 1.14 peter if (w != rp->rio_weight) {
248 1.14 peter printf("invalid weight value %d for red! use %d\n",
249 1.14 peter rp->rio_weight, w);
250 1.14 peter rp->rio_weight = w;
251 1.14 peter }
252 1.14 peter
253 1.14 peter /* allocate weight table */
254 1.14 peter rp->rio_wtab = wtab_alloc(rp->rio_weight);
255 1.14 peter
256 1.14 peter for (i = 0; i < RIO_NDROPPREC; i++) {
257 1.14 peter struct dropprec_state *prec = &rp->rio_precstate[i];
258 1.14 peter
259 1.14 peter prec->avg = 0;
260 1.14 peter prec->idle = 1;
261 1.14 peter
262 1.14 peter if (params == NULL || params[i].inv_pmax == 0)
263 1.14 peter prec->inv_pmax = default_rio_params[i].inv_pmax;
264 1.14 peter else
265 1.14 peter prec->inv_pmax = params[i].inv_pmax;
266 1.14 peter if (params == NULL || params[i].th_min == 0)
267 1.14 peter prec->th_min = default_rio_params[i].th_min;
268 1.14 peter else
269 1.14 peter prec->th_min = params[i].th_min;
270 1.14 peter if (params == NULL || params[i].th_max == 0)
271 1.14 peter prec->th_max = default_rio_params[i].th_max;
272 1.14 peter else
273 1.14 peter prec->th_max = params[i].th_max;
274 1.14 peter
275 1.14 peter /*
276 1.14 peter * th_min_s and th_max_s are scaled versions of th_min
277 1.14 peter * and th_max to be compared with avg.
278 1.14 peter */
279 1.14 peter prec->th_min_s = prec->th_min << (rp->rio_wshift + FP_SHIFT);
280 1.14 peter prec->th_max_s = prec->th_max << (rp->rio_wshift + FP_SHIFT);
281 1.14 peter
282 1.14 peter /*
283 1.14 peter * precompute probability denominator
284 1.14 peter * probd = (2 * (TH_MAX-TH_MIN) / pmax) in fixed-point
285 1.14 peter */
286 1.14 peter prec->probd = (2 * (prec->th_max - prec->th_min)
287 1.14 peter * prec->inv_pmax) << FP_SHIFT;
288 1.14 peter
289 1.14 peter microtime(&prec->last);
290 1.14 peter }
291 1.14 peter
292 1.14 peter return (rp);
293 1.14 peter }
294 1.14 peter
295 1.14 peter void
296 1.14 peter rio_destroy(rio_t *rp)
297 1.14 peter {
298 1.14 peter wtab_destroy(rp->rio_wtab);
299 1.14 peter free(rp, M_DEVBUF);
300 1.14 peter }
301 1.14 peter
302 1.14 peter void
303 1.14 peter rio_getstats(rio_t *rp, struct redstats *sp)
304 1.14 peter {
305 1.14 peter int i;
306 1.14 peter
307 1.14 peter for (i = 0; i < RIO_NDROPPREC; i++) {
308 1.14 peter bcopy(&rp->q_stats[i], sp, sizeof(struct redstats));
309 1.14 peter sp->q_avg = rp->rio_precstate[i].avg >> rp->rio_wshift;
310 1.14 peter sp++;
311 1.14 peter }
312 1.14 peter }
313 1.14 peter
314 1.14 peter #if (RIO_NDROPPREC == 3)
315 1.14 peter /*
316 1.14 peter * internally, a drop precedence value is converted to an index
317 1.14 peter * starting from 0.
318 1.14 peter */
319 1.14 peter static int
320 1.14 peter dscp2index(u_int8_t dscp)
321 1.14 peter {
322 1.14 peter int dpindex = dscp & AF_DROPPRECMASK;
323 1.14 peter
324 1.14 peter if (dpindex == 0)
325 1.14 peter return (0);
326 1.14 peter return ((dpindex >> 3) - 1);
327 1.14 peter }
328 1.14 peter #endif
329 1.14 peter
330 1.14 peter #if 1
331 1.14 peter /*
332 1.14 peter * kludge: when a packet is dequeued, we need to know its drop precedence
333 1.14 peter * in order to keep the queue length of each drop precedence.
334 1.14 peter * use m_pkthdr.rcvif to pass this info.
335 1.14 peter */
336 1.14 peter #define RIOM_SET_PRECINDEX(m, idx) \
337 1.14 peter do { (m)->m_pkthdr.rcvif = (struct ifnet *)((long)(idx)); } while (0)
338 1.14 peter #define RIOM_GET_PRECINDEX(m) \
339 1.14 peter ({ long idx; idx = (long)((m)->m_pkthdr.rcvif); \
340 1.14 peter (m)->m_pkthdr.rcvif = NULL; idx; })
341 1.14 peter #endif
342 1.14 peter
343 1.14 peter int
344 1.14 peter rio_addq(rio_t *rp, class_queue_t *q, struct mbuf *m,
345 1.14 peter struct altq_pktattr *pktattr)
346 1.14 peter {
347 1.14 peter int avg, droptype;
348 1.14 peter u_int8_t dsfield, odsfield;
349 1.14 peter int dpindex, i, n, t;
350 1.14 peter struct timeval now;
351 1.14 peter struct dropprec_state *prec;
352 1.14 peter
353 1.14 peter dsfield = odsfield = read_dsfield(m, pktattr);
354 1.14 peter dpindex = dscp2index(dsfield);
355 1.14 peter
356 1.14 peter /*
357 1.14 peter * update avg of the precedence states whose drop precedence
358 1.14 peter * is larger than or equal to the drop precedence of the packet
359 1.14 peter */
360 1.14 peter now.tv_sec = 0;
361 1.14 peter for (i = dpindex; i < RIO_NDROPPREC; i++) {
362 1.14 peter prec = &rp->rio_precstate[i];
363 1.14 peter avg = prec->avg;
364 1.14 peter if (prec->idle) {
365 1.14 peter prec->idle = 0;
366 1.14 peter if (now.tv_sec == 0)
367 1.14 peter microtime(&now);
368 1.14 peter t = (now.tv_sec - prec->last.tv_sec);
369 1.14 peter if (t > 60)
370 1.14 peter avg = 0;
371 1.14 peter else {
372 1.14 peter t = t * 1000000 +
373 1.14 peter (now.tv_usec - prec->last.tv_usec);
374 1.14 peter n = t / rp->rio_pkttime;
375 1.14 peter /* calculate (avg = (1 - Wq)^n * avg) */
376 1.14 peter if (n > 0)
377 1.14 peter avg = (avg >> FP_SHIFT) *
378 1.14 peter pow_w(rp->rio_wtab, n);
379 1.14 peter }
380 1.14 peter }
381 1.14 peter
382 1.14 peter /* run estimator. (avg is scaled by WEIGHT in fixed-point) */
383 1.14 peter avg += (prec->qlen << FP_SHIFT) - (avg >> rp->rio_wshift);
384 1.14 peter prec->avg = avg; /* save the new value */
385 1.14 peter /*
386 1.14 peter * count keeps a tally of arriving traffic that has not
387 1.14 peter * been dropped.
388 1.14 peter */
389 1.14 peter prec->count++;
390 1.14 peter }
391 1.14 peter
392 1.14 peter prec = &rp->rio_precstate[dpindex];
393 1.14 peter avg = prec->avg;
394 1.14 peter
395 1.14 peter /* see if we drop early */
396 1.14 peter droptype = DTYPE_NODROP;
397 1.14 peter if (avg >= prec->th_min_s && prec->qlen > 1) {
398 1.14 peter if (avg >= prec->th_max_s) {
399 1.14 peter /* avg >= th_max: forced drop */
400 1.14 peter droptype = DTYPE_FORCED;
401 1.14 peter } else if (prec->old == 0) {
402 1.14 peter /* first exceeds th_min */
403 1.14 peter prec->count = 1;
404 1.14 peter prec->old = 1;
405 1.14 peter } else if (drop_early((avg - prec->th_min_s) >> rp->rio_wshift,
406 1.14 peter prec->probd, prec->count)) {
407 1.14 peter /* unforced drop by red */
408 1.14 peter droptype = DTYPE_EARLY;
409 1.14 peter }
410 1.14 peter } else {
411 1.14 peter /* avg < th_min */
412 1.14 peter prec->old = 0;
413 1.14 peter }
414 1.14 peter
415 1.14 peter /*
416 1.14 peter * if the queue length hits the hard limit, it's a forced drop.
417 1.14 peter */
418 1.14 peter if (droptype == DTYPE_NODROP && qlen(q) >= qlimit(q))
419 1.14 peter droptype = DTYPE_FORCED;
420 1.14 peter
421 1.14 peter if (droptype != DTYPE_NODROP) {
422 1.14 peter /* always drop incoming packet (as opposed to randomdrop) */
423 1.14 peter for (i = dpindex; i < RIO_NDROPPREC; i++)
424 1.14 peter rp->rio_precstate[i].count = 0;
425 1.14 peter #ifdef RIO_STATS
426 1.14 peter if (droptype == DTYPE_EARLY)
427 1.14 peter rp->q_stats[dpindex].drop_unforced++;
428 1.14 peter else
429 1.14 peter rp->q_stats[dpindex].drop_forced++;
430 1.14 peter PKTCNTR_ADD(&rp->q_stats[dpindex].drop_cnt, m_pktlen(m));
431 1.14 peter #endif
432 1.14 peter m_freem(m);
433 1.14 peter return (-1);
434 1.14 peter }
435 1.14 peter
436 1.14 peter for (i = dpindex; i < RIO_NDROPPREC; i++)
437 1.14 peter rp->rio_precstate[i].qlen++;
438 1.14 peter
439 1.14 peter /* save drop precedence index in mbuf hdr */
440 1.14 peter RIOM_SET_PRECINDEX(m, dpindex);
441 1.14 peter
442 1.14 peter if (rp->rio_flags & RIOF_CLEARDSCP)
443 1.14 peter dsfield &= ~DSCP_MASK;
444 1.14 peter
445 1.14 peter if (dsfield != odsfield)
446 1.14 peter write_dsfield(m, pktattr, dsfield);
447 1.14 peter
448 1.14 peter _addq(q, m);
449 1.14 peter
450 1.14 peter #ifdef RIO_STATS
451 1.14 peter PKTCNTR_ADD(&rp->q_stats[dpindex].xmit_cnt, m_pktlen(m));
452 1.14 peter #endif
453 1.14 peter return (0);
454 1.14 peter }
455 1.14 peter
456 1.14 peter struct mbuf *
457 1.14 peter rio_getq(rio_t *rp, class_queue_t *q)
458 1.14 peter {
459 1.14 peter struct mbuf *m;
460 1.14 peter int dpindex, i;
461 1.14 peter
462 1.14 peter if ((m = _getq(q)) == NULL)
463 1.14 peter return NULL;
464 1.14 peter
465 1.14 peter dpindex = RIOM_GET_PRECINDEX(m);
466 1.14 peter for (i = dpindex; i < RIO_NDROPPREC; i++) {
467 1.14 peter if (--rp->rio_precstate[i].qlen == 0) {
468 1.14 peter if (rp->rio_precstate[i].idle == 0) {
469 1.14 peter rp->rio_precstate[i].idle = 1;
470 1.14 peter microtime(&rp->rio_precstate[i].last);
471 1.14 peter }
472 1.14 peter }
473 1.14 peter }
474 1.14 peter return (m);
475 1.14 peter }
476 1.1 thorpej
477 1.14 peter #ifdef ALTQ3_COMPAT
478 1.1 thorpej int
479 1.17 christos rioopen(dev_t dev, int flag, int fmt,
480 1.17 christos struct lwp *l)
481 1.1 thorpej {
482 1.1 thorpej /* everything will be done when the queueing scheme is attached. */
483 1.1 thorpej return 0;
484 1.1 thorpej }
485 1.1 thorpej
486 1.1 thorpej int
487 1.17 christos rioclose(dev_t dev, int flag, int fmt,
488 1.17 christos struct lwp *l)
489 1.1 thorpej {
490 1.1 thorpej rio_queue_t *rqp;
491 1.1 thorpej int err, error = 0;
492 1.1 thorpej
493 1.1 thorpej while ((rqp = rio_list) != NULL) {
494 1.1 thorpej /* destroy all */
495 1.1 thorpej err = rio_detach(rqp);
496 1.1 thorpej if (err != 0 && error == 0)
497 1.1 thorpej error = err;
498 1.1 thorpej }
499 1.1 thorpej
500 1.1 thorpej return error;
501 1.1 thorpej }
502 1.1 thorpej
503 1.1 thorpej int
504 1.17 christos rioioctl(dev_t dev, ioctlcmd_t cmd, caddr_t addr, int flag,
505 1.13 christos struct lwp *l)
506 1.1 thorpej {
507 1.1 thorpej rio_queue_t *rqp;
508 1.1 thorpej struct rio_interface *ifacep;
509 1.1 thorpej struct ifnet *ifp;
510 1.1 thorpej int error = 0;
511 1.1 thorpej
512 1.1 thorpej /* check super-user privilege */
513 1.1 thorpej switch (cmd) {
514 1.1 thorpej case RIO_GETSTATS:
515 1.1 thorpej break;
516 1.1 thorpej default:
517 1.1 thorpej #if (__FreeBSD_version > 400000)
518 1.1 thorpej if ((error = suser(p)) != 0)
519 1.1 thorpej return (error);
520 1.1 thorpej #else
521 1.16 elad if ((error = kauth_authorize_network(l->l_cred,
522 1.16 elad KAUTH_NETWORK_ALTQ, KAUTH_REQ_NETWORK_ALTQ_RIO, NULL,
523 1.16 elad NULL, NULL)) != 0)
524 1.1 thorpej return (error);
525 1.1 thorpej #endif
526 1.1 thorpej break;
527 1.1 thorpej }
528 1.7 perry
529 1.1 thorpej switch (cmd) {
530 1.1 thorpej
531 1.1 thorpej case RIO_ENABLE:
532 1.1 thorpej ifacep = (struct rio_interface *)addr;
533 1.1 thorpej if ((rqp = altq_lookup(ifacep->rio_ifname, ALTQT_RIO)) == NULL) {
534 1.1 thorpej error = EBADF;
535 1.1 thorpej break;
536 1.1 thorpej }
537 1.1 thorpej error = altq_enable(rqp->rq_ifq);
538 1.1 thorpej break;
539 1.1 thorpej
540 1.1 thorpej case RIO_DISABLE:
541 1.1 thorpej ifacep = (struct rio_interface *)addr;
542 1.1 thorpej if ((rqp = altq_lookup(ifacep->rio_ifname, ALTQT_RIO)) == NULL) {
543 1.1 thorpej error = EBADF;
544 1.1 thorpej break;
545 1.1 thorpej }
546 1.1 thorpej error = altq_disable(rqp->rq_ifq);
547 1.1 thorpej break;
548 1.1 thorpej
549 1.1 thorpej case RIO_IF_ATTACH:
550 1.1 thorpej ifp = ifunit(((struct rio_interface *)addr)->rio_ifname);
551 1.1 thorpej if (ifp == NULL) {
552 1.1 thorpej error = ENXIO;
553 1.1 thorpej break;
554 1.1 thorpej }
555 1.1 thorpej
556 1.1 thorpej /* allocate and initialize rio_queue_t */
557 1.9 christos rqp = malloc(sizeof(rio_queue_t), M_DEVBUF, M_WAITOK|M_ZERO);
558 1.1 thorpej if (rqp == NULL) {
559 1.1 thorpej error = ENOMEM;
560 1.1 thorpej break;
561 1.1 thorpej }
562 1.1 thorpej
563 1.9 christos rqp->rq_q = malloc(sizeof(class_queue_t), M_DEVBUF,
564 1.9 christos M_WAITOK|M_ZERO);
565 1.1 thorpej if (rqp->rq_q == NULL) {
566 1.9 christos free(rqp, M_DEVBUF);
567 1.1 thorpej error = ENOMEM;
568 1.1 thorpej break;
569 1.1 thorpej }
570 1.1 thorpej
571 1.1 thorpej rqp->rq_rio = rio_alloc(0, NULL, 0, 0);
572 1.1 thorpej if (rqp->rq_rio == NULL) {
573 1.9 christos free(rqp->rq_q, M_DEVBUF);
574 1.9 christos free(rqp, M_DEVBUF);
575 1.1 thorpej error = ENOMEM;
576 1.1 thorpej break;
577 1.1 thorpej }
578 1.1 thorpej
579 1.1 thorpej rqp->rq_ifq = &ifp->if_snd;
580 1.1 thorpej qtail(rqp->rq_q) = NULL;
581 1.1 thorpej qlen(rqp->rq_q) = 0;
582 1.1 thorpej qlimit(rqp->rq_q) = RIO_LIMIT;
583 1.1 thorpej qtype(rqp->rq_q) = Q_RIO;
584 1.1 thorpej
585 1.1 thorpej /*
586 1.1 thorpej * set RIO to this ifnet structure.
587 1.1 thorpej */
588 1.1 thorpej error = altq_attach(rqp->rq_ifq, ALTQT_RIO, rqp,
589 1.1 thorpej rio_enqueue, rio_dequeue, rio_request,
590 1.1 thorpej NULL, NULL);
591 1.1 thorpej if (error) {
592 1.1 thorpej rio_destroy(rqp->rq_rio);
593 1.9 christos free(rqp->rq_q, M_DEVBUF);
594 1.9 christos free(rqp, M_DEVBUF);
595 1.1 thorpej break;
596 1.1 thorpej }
597 1.1 thorpej
598 1.1 thorpej /* add this state to the rio list */
599 1.1 thorpej rqp->rq_next = rio_list;
600 1.1 thorpej rio_list = rqp;
601 1.1 thorpej break;
602 1.1 thorpej
603 1.1 thorpej case RIO_IF_DETACH:
604 1.1 thorpej ifacep = (struct rio_interface *)addr;
605 1.1 thorpej if ((rqp = altq_lookup(ifacep->rio_ifname, ALTQT_RIO)) == NULL) {
606 1.1 thorpej error = EBADF;
607 1.1 thorpej break;
608 1.1 thorpej }
609 1.1 thorpej error = rio_detach(rqp);
610 1.1 thorpej break;
611 1.1 thorpej
612 1.1 thorpej case RIO_GETSTATS:
613 1.1 thorpej do {
614 1.1 thorpej struct rio_stats *q_stats;
615 1.1 thorpej rio_t *rp;
616 1.1 thorpej int i;
617 1.1 thorpej
618 1.1 thorpej q_stats = (struct rio_stats *)addr;
619 1.1 thorpej if ((rqp = altq_lookup(q_stats->iface.rio_ifname,
620 1.1 thorpej ALTQT_RIO)) == NULL) {
621 1.1 thorpej error = EBADF;
622 1.1 thorpej break;
623 1.1 thorpej }
624 1.1 thorpej
625 1.1 thorpej rp = rqp->rq_rio;
626 1.1 thorpej
627 1.1 thorpej q_stats->q_limit = qlimit(rqp->rq_q);
628 1.1 thorpej q_stats->weight = rp->rio_weight;
629 1.1 thorpej q_stats->flags = rp->rio_flags;
630 1.1 thorpej
631 1.1 thorpej for (i = 0; i < RIO_NDROPPREC; i++) {
632 1.1 thorpej q_stats->q_len[i] = rp->rio_precstate[i].qlen;
633 1.14 peter bcopy(&rp->q_stats[i], &q_stats->q_stats[i],
634 1.14 peter sizeof(struct redstats));
635 1.1 thorpej q_stats->q_stats[i].q_avg =
636 1.1 thorpej rp->rio_precstate[i].avg >> rp->rio_wshift;
637 1.1 thorpej
638 1.1 thorpej q_stats->q_params[i].inv_pmax
639 1.1 thorpej = rp->rio_precstate[i].inv_pmax;
640 1.1 thorpej q_stats->q_params[i].th_min
641 1.1 thorpej = rp->rio_precstate[i].th_min;
642 1.1 thorpej q_stats->q_params[i].th_max
643 1.1 thorpej = rp->rio_precstate[i].th_max;
644 1.1 thorpej }
645 1.14 peter } while (/*CONSTCOND*/ 0);
646 1.1 thorpej break;
647 1.1 thorpej
648 1.1 thorpej case RIO_CONFIG:
649 1.1 thorpej do {
650 1.1 thorpej struct rio_conf *fc;
651 1.1 thorpej rio_t *new;
652 1.1 thorpej int s, limit, i;
653 1.1 thorpej
654 1.1 thorpej fc = (struct rio_conf *)addr;
655 1.1 thorpej if ((rqp = altq_lookup(fc->iface.rio_ifname,
656 1.1 thorpej ALTQT_RIO)) == NULL) {
657 1.1 thorpej error = EBADF;
658 1.1 thorpej break;
659 1.1 thorpej }
660 1.1 thorpej
661 1.1 thorpej new = rio_alloc(fc->rio_weight, &fc->q_params[0],
662 1.1 thorpej fc->rio_flags, fc->rio_pkttime);
663 1.1 thorpej if (new == NULL) {
664 1.1 thorpej error = ENOMEM;
665 1.1 thorpej break;
666 1.1 thorpej }
667 1.1 thorpej
668 1.3 thorpej s = splnet();
669 1.1 thorpej _flushq(rqp->rq_q);
670 1.1 thorpej limit = fc->rio_limit;
671 1.1 thorpej if (limit < fc->q_params[RIO_NDROPPREC-1].th_max)
672 1.1 thorpej limit = fc->q_params[RIO_NDROPPREC-1].th_max;
673 1.1 thorpej qlimit(rqp->rq_q) = limit;
674 1.1 thorpej
675 1.1 thorpej rio_destroy(rqp->rq_rio);
676 1.1 thorpej rqp->rq_rio = new;
677 1.1 thorpej
678 1.1 thorpej splx(s);
679 1.1 thorpej
680 1.1 thorpej /* write back new values */
681 1.1 thorpej fc->rio_limit = limit;
682 1.1 thorpej for (i = 0; i < RIO_NDROPPREC; i++) {
683 1.1 thorpej fc->q_params[i].inv_pmax =
684 1.1 thorpej rqp->rq_rio->rio_precstate[i].inv_pmax;
685 1.1 thorpej fc->q_params[i].th_min =
686 1.1 thorpej rqp->rq_rio->rio_precstate[i].th_min;
687 1.1 thorpej fc->q_params[i].th_max =
688 1.1 thorpej rqp->rq_rio->rio_precstate[i].th_max;
689 1.1 thorpej }
690 1.14 peter } while (/*CONSTCOND*/ 0);
691 1.1 thorpej break;
692 1.1 thorpej
693 1.1 thorpej case RIO_SETDEFAULTS:
694 1.1 thorpej do {
695 1.1 thorpej struct redparams *rp;
696 1.1 thorpej int i;
697 1.1 thorpej
698 1.1 thorpej rp = (struct redparams *)addr;
699 1.1 thorpej for (i = 0; i < RIO_NDROPPREC; i++)
700 1.1 thorpej default_rio_params[i] = rp[i];
701 1.14 peter } while (/*CONSTCOND*/ 0);
702 1.1 thorpej break;
703 1.1 thorpej
704 1.1 thorpej default:
705 1.1 thorpej error = EINVAL;
706 1.1 thorpej break;
707 1.1 thorpej }
708 1.1 thorpej
709 1.1 thorpej return error;
710 1.1 thorpej }
711 1.1 thorpej
712 1.1 thorpej static int
713 1.14 peter rio_detach(rio_queue_t *rqp)
714 1.1 thorpej {
715 1.1 thorpej rio_queue_t *tmp;
716 1.1 thorpej int error = 0;
717 1.1 thorpej
718 1.1 thorpej if (ALTQ_IS_ENABLED(rqp->rq_ifq))
719 1.1 thorpej altq_disable(rqp->rq_ifq);
720 1.1 thorpej
721 1.1 thorpej if ((error = altq_detach(rqp->rq_ifq)))
722 1.1 thorpej return (error);
723 1.1 thorpej
724 1.1 thorpej if (rio_list == rqp)
725 1.1 thorpej rio_list = rqp->rq_next;
726 1.1 thorpej else {
727 1.1 thorpej for (tmp = rio_list; tmp != NULL; tmp = tmp->rq_next)
728 1.1 thorpej if (tmp->rq_next == rqp) {
729 1.1 thorpej tmp->rq_next = rqp->rq_next;
730 1.1 thorpej break;
731 1.1 thorpej }
732 1.1 thorpej if (tmp == NULL)
733 1.1 thorpej printf("rio_detach: no state found in rio_list!\n");
734 1.1 thorpej }
735 1.1 thorpej
736 1.1 thorpej rio_destroy(rqp->rq_rio);
737 1.9 christos free(rqp->rq_q, M_DEVBUF);
738 1.9 christos free(rqp, M_DEVBUF);
739 1.1 thorpej return (error);
740 1.1 thorpej }
741 1.1 thorpej
742 1.1 thorpej /*
743 1.1 thorpej * rio support routines
744 1.1 thorpej */
745 1.1 thorpej static int
746 1.17 christos rio_request(struct ifaltq *ifq, int req, void *arg)
747 1.1 thorpej {
748 1.1 thorpej rio_queue_t *rqp = (rio_queue_t *)ifq->altq_disc;
749 1.1 thorpej
750 1.1 thorpej switch (req) {
751 1.1 thorpej case ALTRQ_PURGE:
752 1.1 thorpej _flushq(rqp->rq_q);
753 1.1 thorpej if (ALTQ_IS_ENABLED(ifq))
754 1.1 thorpej ifq->ifq_len = 0;
755 1.1 thorpej break;
756 1.1 thorpej }
757 1.1 thorpej return (0);
758 1.1 thorpej }
759 1.1 thorpej
760 1.1 thorpej /*
761 1.1 thorpej * enqueue routine:
762 1.1 thorpej *
763 1.1 thorpej * returns: 0 when successfully queued.
764 1.1 thorpej * ENOBUFS when drop occurs.
765 1.1 thorpej */
766 1.1 thorpej static int
767 1.14 peter rio_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
768 1.1 thorpej {
769 1.1 thorpej rio_queue_t *rqp = (rio_queue_t *)ifq->altq_disc;
770 1.1 thorpej int error = 0;
771 1.1 thorpej
772 1.1 thorpej if (rio_addq(rqp->rq_rio, rqp->rq_q, m, pktattr) == 0)
773 1.1 thorpej ifq->ifq_len++;
774 1.1 thorpej else
775 1.1 thorpej error = ENOBUFS;
776 1.1 thorpej return error;
777 1.1 thorpej }
778 1.1 thorpej
779 1.1 thorpej /*
780 1.1 thorpej * dequeue routine:
781 1.3 thorpej * must be called in splnet.
782 1.1 thorpej *
783 1.1 thorpej * returns: mbuf dequeued.
784 1.1 thorpej * NULL when no packet is available in the queue.
785 1.1 thorpej */
786 1.1 thorpej
787 1.1 thorpej static struct mbuf *
788 1.14 peter rio_dequeue(struct ifaltq *ifq, int op)
789 1.1 thorpej {
790 1.1 thorpej rio_queue_t *rqp = (rio_queue_t *)ifq->altq_disc;
791 1.1 thorpej struct mbuf *m = NULL;
792 1.1 thorpej
793 1.1 thorpej if (op == ALTDQ_POLL)
794 1.1 thorpej return qhead(rqp->rq_q);
795 1.1 thorpej
796 1.1 thorpej m = rio_getq(rqp->rq_rio, rqp->rq_q);
797 1.1 thorpej if (m != NULL)
798 1.1 thorpej ifq->ifq_len--;
799 1.1 thorpej return m;
800 1.1 thorpej }
801 1.1 thorpej
802 1.1 thorpej #ifdef KLD_MODULE
803 1.1 thorpej
804 1.1 thorpej static struct altqsw rio_sw =
805 1.1 thorpej {"rio", rioopen, rioclose, rioioctl};
806 1.1 thorpej
807 1.1 thorpej ALTQ_MODULE(altq_rio, ALTQT_RIO, &rio_sw);
808 1.14 peter MODULE_VERSION(altq_rio, 1);
809 1.14 peter MODULE_DEPEND(altq_rio, altq_red, 1, 1, 1);
810 1.1 thorpej
811 1.1 thorpej #endif /* KLD_MODULE */
812 1.14 peter #endif /* ALTQ3_COMPAT */
813 1.1 thorpej
814 1.1 thorpej #endif /* ALTQ_RIO */
815