2 * Copyright (c) 2007-2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* $OpenBSD: altq_rio.c,v 1.11 2007/09/13 20:40:02 chl Exp $ */
30 /* $KAME: altq_rio.c,v 1.8 2000/12/14 08:12:46 thorpej Exp $ */
33 * Copyright (C) 1998-2003
34 * Sony Computer Science Laboratories Inc. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * Copyright (c) 1990-1994 Regents of the University of California.
59 * All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 * 2. Redistributions in binary form must reproduce the above copyright
67 * notice, this list of conditions and the following disclaimer in the
68 * documentation and/or other materials provided with the distribution.
69 * 3. All advertising materials mentioning features or use of this software
70 * must display the following acknowledgement:
71 * This product includes software developed by the Computer Systems
72 * Engineering Group at Lawrence Berkeley Laboratory.
73 * 4. Neither the name of the University nor of the Laboratory may be used
74 * to endorse or promote products derived from this software without
75 * specific prior written permission.
77 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
90 #include <sys/cdefs.h>
94 #include <sys/param.h>
95 #include <sys/malloc.h>
97 #include <sys/socket.h>
98 #include <sys/systm.h>
99 #include <sys/errno.h>
100 #include <sys/kauth.h>
101 #include <sys/kauth.h>
103 #include <kern/zalloc.h>
107 #include <netinet/in.h>
108 #include <netinet/in_systm.h>
109 #include <netinet/ip.h>
111 #include <netinet/ip6.h>
114 #include <net/classq/classq_red.h>
115 #include <net/classq/classq_rio.h>
118 * RIO: RED with IN/OUT bit
120 * "Explicit Allocation of Best Effort Packet Delivery Service"
121 * David D. Clark and Wenjia Fang, MIT Lab for Computer Science
122 * http://diffserv.lcs.mit.edu/Papers/exp-alloc-ddc-wf.{ps,pdf}
124 * this implementation is extended to support more than 2 drop precedence
125 * values as described in RFC2597 (Assured Forwarding PHB Group).
129 * AF DS (differentiated service) codepoints.
130 * (classes can be mapped to CBQ or H-FSC classes.)
133 * +---+---+---+---+---+---+---+---+
134 * | CLASS |DropPre| 0 | CU |
135 * +---+---+---+---+---+---+---+---+
143 * medium drop prec: 10
147 /* normal red parameters */
148 #define W_WEIGHT 512 /* inverse of weight of EWMA (511/512) */
149 /* q_weight = 0.00195 */
151 /* red parameters for a slow link */
152 #define W_WEIGHT_1 128 /* inverse of weight of EWMA (127/128) */
153 /* q_weight = 0.0078125 */
155 /* red parameters for a very slow link (e.g., dialup) */
156 #define W_WEIGHT_2 64 /* inverse of weight of EWMA (63/64) */
157 /* q_weight = 0.015625 */
159 /* fixed-point uses 12-bit decimal places */
160 #define FP_SHIFT 12 /* fixed-point shift */
162 /* red parameters for drop probability */
163 #define INV_P_MAX 10 /* inverse of max drop probability */
164 #define TH_MIN 5 /* min threshold */
165 #define TH_MAX 15 /* max threshold */
167 #define RIO_LIMIT 60 /* default max queue lenght */
169 /* default rio parameter values */
170 static struct redparams default_rio_params
[RIO_NDROPPREC
] = {
171 /* th_min, th_max, inv_pmax */
172 { TH_MAX
* 2 + TH_MIN
, TH_MAX
* 3, INV_P_MAX
}, /* low drop precedence */
173 { TH_MAX
+ TH_MIN
, TH_MAX
* 2, INV_P_MAX
}, /* medium drop precedence */
174 { TH_MIN
, TH_MAX
, INV_P_MAX
} /* high drop precedence */
177 #define RIO_ZONE_MAX 32 /* maximum elements in zone */
178 #define RIO_ZONE_NAME "classq_rio" /* zone name */
180 static unsigned int rio_size
; /* size of zone element */
181 static struct zone
*rio_zone
; /* zone for rio */
183 /* internal function prototypes */
184 static struct mbuf
*rio_getq_flow(struct rio
*, class_queue_t
*,
185 u_int32_t
, boolean_t
);
186 static int dscp2index(u_int8_t
);
191 _CASSERT(RIOF_ECN4
== CLASSQF_ECN4
);
192 _CASSERT(RIOF_ECN6
== CLASSQF_ECN6
);
194 rio_size
= sizeof (rio_t
);
195 rio_zone
= zinit(rio_size
, RIO_ZONE_MAX
* rio_size
,
197 if (rio_zone
== NULL
) {
198 panic("%s: failed allocating %s", __func__
, RIO_ZONE_NAME
);
201 zone_change(rio_zone
, Z_EXPAND
, TRUE
);
202 zone_change(rio_zone
, Z_CALLERACCT
, TRUE
);
206 rio_alloc(struct ifnet
*ifp
, int weight
, struct redparams
*params
,
207 int flags
, int pkttime
)
215 rp
= zalloc(rio_zone
);
220 rp
->rio_flags
= (flags
& RIOF_USERFLAGS
);
224 /* default packet time: 1000 bytes / 10Mbps * 8 * 1000000 */
225 rp
->rio_pkttime
= 800;
227 rp
->rio_pkttime
= pkttime
;
230 rp
->rio_weight
= weight
;
233 rp
->rio_weight
= W_WEIGHT
;
235 /* when the link is very slow, adjust red parameters */
236 npkts_per_sec
= 1000000 / rp
->rio_pkttime
;
237 if (npkts_per_sec
< 50) {
238 /* up to about 400Kbps */
239 rp
->rio_weight
= W_WEIGHT_2
;
240 } else if (npkts_per_sec
< 300) {
241 /* up to about 2.4Mbps */
242 rp
->rio_weight
= W_WEIGHT_1
;
246 /* calculate wshift. weight must be power of 2 */
248 for (i
= 0; w
> 1; i
++)
251 w
= 1 << rp
->rio_wshift
;
252 if (w
!= rp
->rio_weight
) {
253 printf("invalid weight value %d for red! use %d\n",
258 /* allocate weight table */
259 rp
->rio_wtab
= wtab_alloc(rp
->rio_weight
);
260 if (rp
->rio_wtab
== NULL
) {
265 for (i
= 0; i
< RIO_NDROPPREC
; i
++) {
266 struct dropprec_state
*prec
= &rp
->rio_precstate
[i
];
271 if (params
== NULL
|| params
[i
].inv_pmax
== 0)
272 prec
->inv_pmax
= default_rio_params
[i
].inv_pmax
;
274 prec
->inv_pmax
= params
[i
].inv_pmax
;
275 if (params
== NULL
|| params
[i
].th_min
== 0)
276 prec
->th_min
= default_rio_params
[i
].th_min
;
278 prec
->th_min
= params
[i
].th_min
;
279 if (params
== NULL
|| params
[i
].th_max
== 0)
280 prec
->th_max
= default_rio_params
[i
].th_max
;
282 prec
->th_max
= params
[i
].th_max
;
285 * th_min_s and th_max_s are scaled versions of th_min
286 * and th_max to be compared with avg.
288 prec
->th_min_s
= prec
->th_min
<< (rp
->rio_wshift
+ FP_SHIFT
);
289 prec
->th_max_s
= prec
->th_max
<< (rp
->rio_wshift
+ FP_SHIFT
);
292 * precompute probability denominator
293 * probd = (2 * (TH_MAX-TH_MIN) / pmax) in fixed-point
295 prec
->probd
= (2 * (prec
->th_max
- prec
->th_min
) *
296 prec
->inv_pmax
) << FP_SHIFT
;
298 microuptime(&prec
->last
);
305 rio_destroy(rio_t
*rp
)
307 if (rp
->rio_wtab
!= NULL
) {
308 wtab_destroy(rp
->rio_wtab
);
315 rio_getstats(rio_t
*rp
, struct red_stats
*sp
)
319 for (i
= 0; i
< RIO_NDROPPREC
; i
++) {
320 bcopy(&rp
->q_stats
[i
], sp
, sizeof (struct red_stats
));
321 sp
->q_avg
= rp
->rio_precstate
[i
].avg
>> rp
->rio_wshift
;
326 #if (RIO_NDROPPREC == 3)
328 * internally, a drop precedence value is converted to an index
332 dscp2index(u_int8_t dscp
)
334 #define AF_DROPPRECMASK 0x18
336 int dpindex
= dscp
& AF_DROPPRECMASK
;
340 return ((dpindex
>> 3) - 1);
344 #define RIOM_SET_PRECINDEX(t, idx) do { \
345 (t)->pftag_qpriv32 = (idx); \
348 #define RIOM_GET_PRECINDEX(t) \
349 ({ u_int32_t idx; idx = (t)->pftag_qpriv32; \
350 RIOM_SET_PRECINDEX(t, 0); idx; })
353 rio_addq(rio_t
*rp
, class_queue_t
*q
, struct mbuf
*m
, struct pf_mtag
*tag
)
355 #define DSCP_MASK 0xfc
357 u_int8_t dsfield
, odsfield
;
358 int dpindex
, i
, n
, t
;
360 struct dropprec_state
*prec
;
362 dsfield
= odsfield
= read_dsfield(m
, tag
);
363 dpindex
= dscp2index(dsfield
);
366 * update avg of the precedence states whose drop precedence
367 * is larger than or equal to the drop precedence of the packet
370 for (i
= dpindex
; i
< RIO_NDROPPREC
; i
++) {
371 prec
= &rp
->rio_precstate
[i
];
377 t
= (now
.tv_sec
- prec
->last
.tv_sec
);
382 (now
.tv_usec
- prec
->last
.tv_usec
);
383 n
= t
/ rp
->rio_pkttime
;
384 /* calculate (avg = (1 - Wq)^n * avg) */
386 avg
= (avg
>> FP_SHIFT
) *
387 pow_w(rp
->rio_wtab
, n
);
392 /* run estimator. (avg is scaled by WEIGHT in fixed-point) */
393 avg
+= (prec
->qlen
<< FP_SHIFT
) - (avg
>> rp
->rio_wshift
);
394 prec
->avg
= avg
; /* save the new value */
396 * count keeps a tally of arriving traffic that has not
402 prec
= &rp
->rio_precstate
[dpindex
];
405 /* see if we drop early */
406 droptype
= DTYPE_NODROP
;
407 if (avg
>= prec
->th_min_s
&& prec
->qlen
> 1) {
408 if (avg
>= prec
->th_max_s
) {
409 /* avg >= th_max: forced drop */
410 droptype
= DTYPE_FORCED
;
411 } else if (prec
->old
== 0) {
412 /* first exceeds th_min */
415 } else if (drop_early((avg
- prec
->th_min_s
) >> rp
->rio_wshift
,
416 prec
->probd
, prec
->count
)) {
417 /* unforced drop by red */
418 droptype
= DTYPE_EARLY
;
426 * if the queue length hits the hard limit, it's a forced drop.
428 if (droptype
== DTYPE_NODROP
&& qlen(q
) >= qlimit(q
))
429 droptype
= DTYPE_FORCED
;
431 if (droptype
!= DTYPE_NODROP
) {
432 /* always drop incoming packet (as opposed to randomdrop) */
433 for (i
= dpindex
; i
< RIO_NDROPPREC
; i
++)
434 rp
->rio_precstate
[i
].count
= 0;
436 if (droptype
== DTYPE_EARLY
)
437 rp
->q_stats
[dpindex
].drop_unforced
++;
439 rp
->q_stats
[dpindex
].drop_forced
++;
441 IFCQ_CONVERT_LOCK(&rp
->rio_ifp
->if_snd
);
443 return (CLASSQEQ_DROPPED
);
446 for (i
= dpindex
; i
< RIO_NDROPPREC
; i
++)
447 rp
->rio_precstate
[i
].qlen
++;
449 /* save drop precedence index in mbuf hdr */
450 RIOM_SET_PRECINDEX(tag
, dpindex
);
452 if (rp
->rio_flags
& RIOF_CLEARDSCP
)
453 dsfield
&= ~DSCP_MASK
;
455 if (dsfield
!= odsfield
)
456 write_dsfield(m
, tag
, dsfield
);
460 return (CLASSQEQ_SUCCESS
);
464 rio_getq_flow(struct rio
*rp
, class_queue_t
*q
, u_int32_t flow
, boolean_t purge
)
466 #pragma unused(purge)
470 /* flow of 0 means head of queue */
471 if ((m
= ((flow
== 0) ? _getq(q
) : _getq_flow(q
, flow
))) == NULL
)
474 VERIFY(m
->m_flags
& M_PKTHDR
);
476 dpindex
= RIOM_GET_PRECINDEX(m_pftag(m
));
477 for (i
= dpindex
; i
< RIO_NDROPPREC
; i
++) {
478 if (--rp
->rio_precstate
[i
].qlen
== 0) {
479 if (rp
->rio_precstate
[i
].idle
== 0) {
480 rp
->rio_precstate
[i
].idle
= 1;
481 microuptime(&rp
->rio_precstate
[i
].last
);
489 rio_getq(rio_t
*rp
, class_queue_t
*q
)
491 return (rio_getq_flow(rp
, q
, 0, FALSE
));
495 rio_purgeq(struct rio
*rp
, class_queue_t
*q
, u_int32_t flow
, u_int32_t
*packets
,
498 u_int32_t cnt
= 0, len
= 0;
501 IFCQ_CONVERT_LOCK(&rp
->rio_ifp
->if_snd
);
503 while ((m
= rio_getq_flow(rp
, q
, flow
, TRUE
)) != NULL
) {
516 rio_updateq(rio_t
*rp
, cqev_t ev
)
518 #pragma unused(rp, ev)
519 /* nothing for now */
523 rio_suspendq(rio_t
*rp
, class_queue_t
*q
, boolean_t on
)
525 #pragma unused(rp, q, on)
528 #endif /* CLASSQ_RIO */