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