2 /* $KAME: altq_rio.c,v 1.17 2003/07/10 12:07:49 kjc Exp $ */
5 * Copyright (C) 1998-2003
6 * Sony Computer Science Laboratories Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 * Copyright (c) 1990-1994 Regents of the University of California.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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.
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
62 #if defined(__FreeBSD__) || defined(__NetBSD__)
64 #if (__FreeBSD__ != 2)
67 #include "opt_inet6.h"
70 #endif /* __FreeBSD__ || __NetBSD__ */
71 #ifdef ALTQ_RIO /* rio is enabled by ALTQ_RIO option in opt_altq.h */
73 #include <sys/param.h>
74 #include <sys/malloc.h>
76 #include <sys/socket.h>
77 #include <sys/systm.h>
78 #include <sys/errno.h>
79 #if 1 /* ALTQ3_COMPAT */
81 #include <sys/sockio.h>
82 #include <sys/kernel.h>
87 #include <netinet/in.h>
88 #include <netinet/in_systm.h>
89 #include <netinet/ip.h>
91 #include <netinet/ip6.h>
94 #include <net/pfvar.h>
95 #include <altq/altq.h>
96 #include <altq/altq_cdnr.h>
97 #include <altq/altq_red.h>
98 #include <altq/altq_rio.h>
100 #include <altq/altq_conf.h>
104 * RIO: RED with IN/OUT bit
106 * "Explicit Allocation of Best Effort Packet Delivery Service"
107 * David D. Clark and Wenjia Fang, MIT Lab for Computer Science
108 * http://diffserv.lcs.mit.edu/Papers/exp-alloc-ddc-wf.{ps,pdf}
110 * this implementation is extended to support more than 2 drop precedence
111 * values as described in RFC2597 (Assured Forwarding PHB Group).
115 * AF DS (differentiated service) codepoints.
116 * (classes can be mapped to CBQ or H-FSC classes.)
119 * +---+---+---+---+---+---+---+---+
120 * | CLASS |DropPre| 0 | CU |
121 * +---+---+---+---+---+---+---+---+
129 * medium drop prec: 10
133 /* normal red parameters */
134 #define W_WEIGHT 512 /* inverse of weight of EWMA (511/512) */
135 /* q_weight = 0.00195 */
137 /* red parameters for a slow link */
138 #define W_WEIGHT_1 128 /* inverse of weight of EWMA (127/128) */
139 /* q_weight = 0.0078125 */
141 /* red parameters for a very slow link (e.g., dialup) */
142 #define W_WEIGHT_2 64 /* inverse of weight of EWMA (63/64) */
143 /* q_weight = 0.015625 */
145 /* fixed-point uses 12-bit decimal places */
146 #define FP_SHIFT 12 /* fixed-point shift */
148 /* red parameters for drop probability */
149 #define INV_P_MAX 10 /* inverse of max drop probability */
150 #define TH_MIN 5 /* min threshold */
151 #define TH_MAX 15 /* max threshold */
153 #define RIO_LIMIT 60 /* default max queue lenght */
154 #define RIO_STATS /* collect statistics */
156 #define TV_DELTA(a, b, delta) { \
159 delta = (a)->tv_usec - (b)->tv_usec; \
160 if ((xxs = (a)->tv_sec - (b)->tv_sec) != 0) { \
163 } else if (xxs > 4) { \
167 delta += xxs * 1000000; \
168 } else while (xxs > 0) { \
176 /* rio_list keeps all rio_queue_t's allocated. */
177 static rio_queue_t *rio_list = NULL;
179 /* default rio parameter values */
180 static struct redparams default_rio_params[RIO_NDROPPREC] = {
181 /* th_min, th_max, inv_pmax */
182 { TH_MAX * 2 + TH_MIN, TH_MAX * 3, INV_P_MAX }, /* low drop precedence */
183 { TH_MAX + TH_MIN, TH_MAX * 2, INV_P_MAX }, /* medium drop precedence */
184 { TH_MIN, TH_MAX, INV_P_MAX } /* high drop precedence */
187 /* internal function prototypes */
188 static int dscp2index(u_int8_t);
190 static int rio_enqueue(struct ifaltq *, struct mbuf *, struct altq_pktattr *);
191 static struct mbuf *rio_dequeue(struct ifaltq *, int);
192 static int rio_request(struct ifaltq *, int, void *);
193 static int rio_detach(rio_queue_t *);
196 * rio device interface
200 #endif /* ALTQ3_COMPAT */
203 rio_alloc(int weight, struct redparams *params, int flags, int pkttime)
209 rp = malloc(sizeof(rio_t), M_DEVBUF, M_WAITOK);
212 bzero(rp, sizeof(rio_t));
214 rp->rio_flags = flags;
216 /* default packet time: 1000 bytes / 10Mbps * 8 * 1000000 */
217 rp->rio_pkttime = 800;
219 rp->rio_pkttime = pkttime;
222 rp->rio_weight = weight;
225 rp->rio_weight = W_WEIGHT;
227 /* when the link is very slow, adjust red parameters */
228 npkts_per_sec = 1000000 / rp->rio_pkttime;
229 if (npkts_per_sec < 50) {
230 /* up to about 400Kbps */
231 rp->rio_weight = W_WEIGHT_2;
232 } else if (npkts_per_sec < 300) {
233 /* up to about 2.4Mbps */
234 rp->rio_weight = W_WEIGHT_1;
238 /* calculate wshift. weight must be power of 2 */
240 for (i = 0; w > 1; i++)
243 w = 1 << rp->rio_wshift;
244 if (w != rp->rio_weight) {
245 printf("invalid weight value %d for red! use %d\n",
250 /* allocate weight table */
251 rp->rio_wtab = wtab_alloc(rp->rio_weight);
253 for (i = 0; i < RIO_NDROPPREC; i++) {
254 struct dropprec_state *prec = &rp->rio_precstate[i];
259 if (params == NULL || params[i].inv_pmax == 0)
260 prec->inv_pmax = default_rio_params[i].inv_pmax;
262 prec->inv_pmax = params[i].inv_pmax;
263 if (params == NULL || params[i].th_min == 0)
264 prec->th_min = default_rio_params[i].th_min;
266 prec->th_min = params[i].th_min;
267 if (params == NULL || params[i].th_max == 0)
268 prec->th_max = default_rio_params[i].th_max;
270 prec->th_max = params[i].th_max;
273 * th_min_s and th_max_s are scaled versions of th_min
274 * and th_max to be compared with avg.
276 prec->th_min_s = prec->th_min << (rp->rio_wshift + FP_SHIFT);
277 prec->th_max_s = prec->th_max << (rp->rio_wshift + FP_SHIFT);
280 * precompute probability denominator
281 * probd = (2 * (TH_MAX-TH_MIN) / pmax) in fixed-point
283 prec->probd = (2 * (prec->th_max - prec->th_min)
284 * prec->inv_pmax) << FP_SHIFT;
286 microtime(&prec->last);
293 rio_destroy(rio_t *rp)
295 wtab_destroy(rp->rio_wtab);
300 rio_getstats(rio_t *rp, struct redstats *sp)
304 for (i = 0; i < RIO_NDROPPREC; i++) {
305 bcopy(&rp->q_stats[i], sp, sizeof(struct redstats));
306 sp->q_avg = rp->rio_precstate[i].avg >> rp->rio_wshift;
311 #if (RIO_NDROPPREC == 3)
313 * internally, a drop precedence value is converted to an index
317 dscp2index(u_int8_t dscp)
319 int dpindex = dscp & AF_DROPPRECMASK;
323 return ((dpindex >> 3) - 1);
329 * kludge: when a packet is dequeued, we need to know its drop precedence
330 * in order to keep the queue length of each drop precedence.
331 * use m_pkthdr.rcvif to pass this info.
333 #define RIOM_SET_PRECINDEX(m, idx) \
334 do { (m)->m_pkthdr.rcvif = (void *)((long)(idx)); } while (0)
335 #define RIOM_GET_PRECINDEX(m) \
336 ({ long idx; idx = (long)((m)->m_pkthdr.rcvif); \
337 (m)->m_pkthdr.rcvif = NULL; idx; })
341 rio_addq(rio_t *rp, class_queue_t *q, struct mbuf *m,
342 struct altq_pktattr *pktattr)
345 u_int8_t dsfield, odsfield;
346 int dpindex, i, n, t;
348 struct dropprec_state *prec;
350 dsfield = odsfield = read_dsfield(m, pktattr);
351 dpindex = dscp2index(dsfield);
354 * update avg of the precedence states whose drop precedence
355 * is larger than or equal to the drop precedence of the packet
358 for (i = dpindex; i < RIO_NDROPPREC; i++) {
359 prec = &rp->rio_precstate[i];
365 t = (now.tv_sec - prec->last.tv_sec);
370 (now.tv_usec - prec->last.tv_usec);
371 n = t / rp->rio_pkttime;
372 /* calculate (avg = (1 - Wq)^n * avg) */
374 avg = (avg >> FP_SHIFT) *
375 pow_w(rp->rio_wtab, n);
379 /* run estimator. (avg is scaled by WEIGHT in fixed-point) */
380 avg += (prec->qlen << FP_SHIFT) - (avg >> rp->rio_wshift);
381 prec->avg = avg; /* save the new value */
383 * count keeps a tally of arriving traffic that has not
389 prec = &rp->rio_precstate[dpindex];
392 /* see if we drop early */
393 droptype = DTYPE_NODROP;
394 if (avg >= prec->th_min_s && prec->qlen > 1) {
395 if (avg >= prec->th_max_s) {
396 /* avg >= th_max: forced drop */
397 droptype = DTYPE_FORCED;
398 } else if (prec->old == 0) {
399 /* first exceeds th_min */
402 } else if (drop_early((avg - prec->th_min_s) >> rp->rio_wshift,
403 prec->probd, prec->count)) {
404 /* unforced drop by red */
405 droptype = DTYPE_EARLY;
413 * if the queue length hits the hard limit, it's a forced drop.
415 if (droptype == DTYPE_NODROP && qlen(q) >= qlimit(q))
416 droptype = DTYPE_FORCED;
418 if (droptype != DTYPE_NODROP) {
419 /* always drop incoming packet (as opposed to randomdrop) */
420 for (i = dpindex; i < RIO_NDROPPREC; i++)
421 rp->rio_precstate[i].count = 0;
423 if (droptype == DTYPE_EARLY)
424 rp->q_stats[dpindex].drop_unforced++;
426 rp->q_stats[dpindex].drop_forced++;
427 PKTCNTR_ADD(&rp->q_stats[dpindex].drop_cnt, m_pktlen(m));
433 for (i = dpindex; i < RIO_NDROPPREC; i++)
434 rp->rio_precstate[i].qlen++;
436 /* save drop precedence index in mbuf hdr */
437 RIOM_SET_PRECINDEX(m, dpindex);
439 if (rp->rio_flags & RIOF_CLEARDSCP)
440 dsfield &= ~DSCP_MASK;
442 if (dsfield != odsfield)
443 write_dsfield(m, pktattr, dsfield);
448 PKTCNTR_ADD(&rp->q_stats[dpindex].xmit_cnt, m_pktlen(m));
454 rio_getq(rio_t *rp, class_queue_t *q)
459 if ((m = _getq(q)) == NULL)
462 dpindex = RIOM_GET_PRECINDEX(m);
463 for (i = dpindex; i < RIO_NDROPPREC; i++) {
464 if (--rp->rio_precstate[i].qlen == 0) {
465 if (rp->rio_precstate[i].idle == 0) {
466 rp->rio_precstate[i].idle = 1;
467 microtime(&rp->rio_precstate[i].last);
476 rioopen(dev, flag, fmt, p)
479 #if (__FreeBSD_version > 500000)
485 /* everything will be done when the queueing scheme is attached. */
490 rioclose(dev, flag, fmt, p)
493 #if (__FreeBSD_version > 500000)
502 while ((rqp = rio_list) != NULL) {
504 err = rio_detach(rqp);
505 if (err != 0 && error == 0)
513 rioioctl(dev, cmd, addr, flag, p)
518 #if (__FreeBSD_version > 500000)
525 struct rio_interface *ifacep;
529 /* check super-user privilege */
534 #if (__FreeBSD_version > 700000)
535 if ((error = priv_check(p, PRIV_ALTQ_MANAGE)) != 0)
537 #elsif (__FreeBSD_version > 400000)
538 if ((error = suser(p)) != 0)
541 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
550 ifacep = (struct rio_interface *)addr;
551 if ((rqp = altq_lookup(ifacep->rio_ifname, ALTQT_RIO)) == NULL) {
555 error = altq_enable(rqp->rq_ifq);
559 ifacep = (struct rio_interface *)addr;
560 if ((rqp = altq_lookup(ifacep->rio_ifname, ALTQT_RIO)) == NULL) {
564 error = altq_disable(rqp->rq_ifq);
568 ifp = ifunit(((struct rio_interface *)addr)->rio_ifname);
574 /* allocate and initialize rio_queue_t */
575 rqp = malloc(sizeof(rio_queue_t), M_DEVBUF, M_WAITOK);
580 bzero(rqp, sizeof(rio_queue_t));
582 rqp->rq_q = malloc(sizeof(class_queue_t),
584 if (rqp->rq_q == NULL) {
589 bzero(rqp->rq_q, sizeof(class_queue_t));
591 rqp->rq_rio = rio_alloc(0, NULL, 0, 0);
592 if (rqp->rq_rio == NULL) {
593 free(rqp->rq_q, M_DEVBUF);
599 rqp->rq_ifq = &ifp->if_snd;
600 qtail(rqp->rq_q) = NULL;
602 qlimit(rqp->rq_q) = RIO_LIMIT;
603 qtype(rqp->rq_q) = Q_RIO;
606 * set RIO to this ifnet structure.
608 error = altq_attach(rqp->rq_ifq, ALTQT_RIO, rqp,
609 rio_enqueue, rio_dequeue, rio_request,
612 rio_destroy(rqp->rq_rio);
613 free(rqp->rq_q, M_DEVBUF);
618 /* add this state to the rio list */
619 rqp->rq_next = rio_list;
624 ifacep = (struct rio_interface *)addr;
625 if ((rqp = altq_lookup(ifacep->rio_ifname, ALTQT_RIO)) == NULL) {
629 error = rio_detach(rqp);
634 struct rio_stats *q_stats;
638 q_stats = (struct rio_stats *)addr;
639 if ((rqp = altq_lookup(q_stats->iface.rio_ifname,
640 ALTQT_RIO)) == NULL) {
647 q_stats->q_limit = qlimit(rqp->rq_q);
648 q_stats->weight = rp->rio_weight;
649 q_stats->flags = rp->rio_flags;
651 for (i = 0; i < RIO_NDROPPREC; i++) {
652 q_stats->q_len[i] = rp->rio_precstate[i].qlen;
653 bcopy(&rp->q_stats[i], &q_stats->q_stats[i],
654 sizeof(struct redstats));
655 q_stats->q_stats[i].q_avg =
656 rp->rio_precstate[i].avg >> rp->rio_wshift;
658 q_stats->q_params[i].inv_pmax
659 = rp->rio_precstate[i].inv_pmax;
660 q_stats->q_params[i].th_min
661 = rp->rio_precstate[i].th_min;
662 q_stats->q_params[i].th_max
663 = rp->rio_precstate[i].th_max;
665 } while (/*CONSTCOND*/ 0);
674 fc = (struct rio_conf *)addr;
675 if ((rqp = altq_lookup(fc->iface.rio_ifname,
676 ALTQT_RIO)) == NULL) {
681 new = rio_alloc(fc->rio_weight, &fc->q_params[0],
682 fc->rio_flags, fc->rio_pkttime);
694 limit = fc->rio_limit;
695 if (limit < fc->q_params[RIO_NDROPPREC-1].th_max)
696 limit = fc->q_params[RIO_NDROPPREC-1].th_max;
697 qlimit(rqp->rq_q) = limit;
699 rio_destroy(rqp->rq_rio);
704 /* write back new values */
705 fc->rio_limit = limit;
706 for (i = 0; i < RIO_NDROPPREC; i++) {
707 fc->q_params[i].inv_pmax =
708 rqp->rq_rio->rio_precstate[i].inv_pmax;
709 fc->q_params[i].th_min =
710 rqp->rq_rio->rio_precstate[i].th_min;
711 fc->q_params[i].th_max =
712 rqp->rq_rio->rio_precstate[i].th_max;
714 } while (/*CONSTCOND*/ 0);
717 case RIO_SETDEFAULTS:
719 struct redparams *rp;
722 rp = (struct redparams *)addr;
723 for (i = 0; i < RIO_NDROPPREC; i++)
724 default_rio_params[i] = rp[i];
725 } while (/*CONSTCOND*/ 0);
743 if (ALTQ_IS_ENABLED(rqp->rq_ifq))
744 altq_disable(rqp->rq_ifq);
746 if ((error = altq_detach(rqp->rq_ifq)))
750 rio_list = rqp->rq_next;
752 for (tmp = rio_list; tmp != NULL; tmp = tmp->rq_next)
753 if (tmp->rq_next == rqp) {
754 tmp->rq_next = rqp->rq_next;
758 printf("rio_detach: no state found in rio_list!\n");
761 rio_destroy(rqp->rq_rio);
762 free(rqp->rq_q, M_DEVBUF);
768 * rio support routines
771 rio_request(ifq, req, arg)
776 rio_queue_t *rqp = (rio_queue_t *)ifq->altq_disc;
778 IFQ_LOCK_ASSERT(ifq);
783 if (ALTQ_IS_ENABLED(ifq))
793 * returns: 0 when successfully queued.
794 * ENOBUFS when drop occurs.
797 rio_enqueue(ifq, m, pktattr)
800 struct altq_pktattr *pktattr;
802 rio_queue_t *rqp = (rio_queue_t *)ifq->altq_disc;
805 IFQ_LOCK_ASSERT(ifq);
807 if (rio_addq(rqp->rq_rio, rqp->rq_q, m, pktattr) == 0)
816 * must be called in splimp.
818 * returns: mbuf dequeued.
819 * NULL when no packet is available in the queue.
827 rio_queue_t *rqp = (rio_queue_t *)ifq->altq_disc;
828 struct mbuf *m = NULL;
830 IFQ_LOCK_ASSERT(ifq);
832 if (op == ALTDQ_POLL)
833 return qhead(rqp->rq_q);
835 m = rio_getq(rqp->rq_rio, rqp->rq_q);
843 static struct altqsw rio_sw =
844 {"rio", rioopen, rioclose, rioioctl};
846 ALTQ_MODULE(altq_rio, ALTQT_RIO, &rio_sw);
847 MODULE_VERSION(altq_rio, 1);
848 MODULE_DEPEND(altq_rio, altq_red, 1, 1, 1);
850 #endif /* KLD_MODULE */
851 #endif /* ALTQ3_COMPAT */
853 #endif /* ALTQ_RIO */