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