4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <sys/types.h>
29 #include <sys/random.h>
30 #include <netinet/in.h>
31 #include <netinet/in_systm.h>
32 #include <netinet/ip6.h>
33 #include <inet/common.h>
36 #include <ipp/meters/meter_impl.h>
39 * Module : Time Sliding Window meter - tswtclmtr
41 * This module implements the metering part of RFC 2859. It accepts the
42 * committed rate, peak rate and the window for a flow and determines
43 * if the flow is within the committed/peak rate and assigns the appropriate
45 * The meter provides an estimate of the running average bandwidth for the
46 * flow over the specified window. It uses probability to benefit TCP flows
47 * as it reduces the likelihood of dropping multiple packets within a TCP
48 * window without adversely effecting UDP flows.
54 * Given a packet and the tswtcl_data it belongs to, this routine meters the
55 * ToS or DSCP for IPv4 and IPv6 resp. with the values configured for
60 tswtcl_process(mblk_t
**mpp
, tswtcl_data_t
*tswtcl_data
,
61 ipp_action_id_t
*next_action
)
70 uint32_t min
= 0, additive
, rnd
;
71 tswtcl_cfg_t
*cfg_parms
= tswtcl_data
->cfg_parms
;
74 tswtcl0dbg(("tswtcl_process: null mp!\n"));
75 atomic_inc_64(&tswtcl_data
->epackets
);
79 if (mp
->b_datap
->db_type
!= M_DATA
) {
80 if ((mp
->b_cont
!= NULL
) &&
81 (mp
->b_cont
->b_datap
->db_type
== M_DATA
)) {
84 tswtcl0dbg(("tswtcl_process: no data\n"));
85 atomic_inc_64(&tswtcl_data
->epackets
);
90 /* Figure out the ToS/Traffic Class and length from the message */
91 if ((mp
->b_wptr
- mp
->b_rptr
) < IP_SIMPLE_HDR_LENGTH
) {
92 if (!pullupmsg(mp
, IP_SIMPLE_HDR_LENGTH
)) {
93 tswtcl0dbg(("tswtcl_process: pullup error\n"));
94 atomic_inc_64(&tswtcl_data
->epackets
);
98 ipha
= (ipha_t
*)mp
->b_rptr
;
99 if (IPH_HDR_VERSION(ipha
) == IPV4_VERSION
) {
100 pkt_len
= ntohs(ipha
->ipha_length
);
102 ip6_hdr
= (ip6_t
*)mp
->b_rptr
;
103 pkt_len
= ntohs(ip6_hdr
->ip6_plen
) +
104 ip_hdr_length_v6(mp
, ip6_hdr
);
107 /* Convert into bits */
110 /* Get current time */
113 /* Update the avg_rate and win_front tswtcl_data */
114 mutex_enter(&tswtcl_data
->tswtcl_lock
);
116 /* avg_rate = bits/sec and window in msec */
117 bitsinwin
= ((uint64_t)tswtcl_data
->avg_rate
* cfg_parms
->window
/
120 deltaT
= now
- tswtcl_data
->win_front
+ cfg_parms
->nsecwindow
;
122 tswtcl_data
->avg_rate
= (uint64_t)bitsinwin
* METER_SEC_TO_NSEC
/
124 tswtcl_data
->win_front
= now
;
126 if (tswtcl_data
->avg_rate
<= cfg_parms
->committed_rate
) {
127 *next_action
= cfg_parms
->green_action
;
128 } else if (tswtcl_data
->avg_rate
<= cfg_parms
->peak_rate
) {
130 * Compute the probability:
132 * p0 = (avg_rate - committed_rate) / avg_rate
134 * Yellow with probability p0
135 * Green with probability (1 - p0)
140 /* Get a random no. betweeen 0 and avg_rate */
141 (void) random_get_pseudo_bytes((uint8_t *)&additive
,
143 rnd
= min
+ (additive
% (tswtcl_data
->avg_rate
- min
+ 1));
145 aminusc
= tswtcl_data
->avg_rate
- cfg_parms
->committed_rate
;
146 if (aminusc
>= rnd
) {
147 *next_action
= cfg_parms
->yellow_action
;
149 *next_action
= cfg_parms
->green_action
;
153 * Compute the probability:
155 * p1 = (avg_rate - peak_rate) / avg_rate
156 * p2 = (peak_rate - committed_rate) / avg_rate
158 * Red with probability p1
159 * Yellow with probability p2
160 * Green with probability (1 - (p1 + p2))
165 /* Get a random no. betweeen 0 and avg_rate */
166 (void) random_get_pseudo_bytes((uint8_t *)&additive
,
168 rnd
= min
+ (additive
% (tswtcl_data
->avg_rate
- min
+ 1));
170 aminusp
= tswtcl_data
->avg_rate
- cfg_parms
->peak_rate
;
172 if (aminusp
>= rnd
) {
173 *next_action
= cfg_parms
->red_action
;
174 } else if ((cfg_parms
->pminusc
+ aminusp
) >= rnd
) {
175 *next_action
= cfg_parms
->yellow_action
;
177 *next_action
= cfg_parms
->green_action
;
181 mutex_exit(&tswtcl_data
->tswtcl_lock
);
184 if (*next_action
== cfg_parms
->green_action
) {
185 atomic_inc_64(&tswtcl_data
->green_packets
);
186 atomic_add_64(&tswtcl_data
->green_bits
, pkt_len
);
187 } else if (*next_action
== cfg_parms
->yellow_action
) {
188 atomic_inc_64(&tswtcl_data
->yellow_packets
);
189 atomic_add_64(&tswtcl_data
->yellow_bits
, pkt_len
);
191 ASSERT(*next_action
== cfg_parms
->red_action
);
192 atomic_inc_64(&tswtcl_data
->red_packets
);
193 atomic_add_64(&tswtcl_data
->red_bits
, pkt_len
);