xref: /onnv-gate/usr/src/uts/common/ipp/meters/tswtcl.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/kmem.h>
31*0Sstevel@tonic-gate #include <sys/random.h>
32*0Sstevel@tonic-gate #include <netinet/in.h>
33*0Sstevel@tonic-gate #include <netinet/in_systm.h>
34*0Sstevel@tonic-gate #include <netinet/ip6.h>
35*0Sstevel@tonic-gate #include <inet/common.h>
36*0Sstevel@tonic-gate #include <inet/ip.h>
37*0Sstevel@tonic-gate #include <inet/ip6.h>
38*0Sstevel@tonic-gate #include <ipp/meters/meter_impl.h>
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate /*
41*0Sstevel@tonic-gate  * Module : Time Sliding Window meter - tswtclmtr
42*0Sstevel@tonic-gate  * Description
43*0Sstevel@tonic-gate  * This module implements the metering part of RFC 2859. It accepts the
44*0Sstevel@tonic-gate  * committed rate, peak rate and the window for a flow and determines
45*0Sstevel@tonic-gate  * if the flow is within the committed/peak rate and assigns the appropriate
46*0Sstevel@tonic-gate  * next action.
47*0Sstevel@tonic-gate  * The meter provides an estimate of the running average bandwidth for the
48*0Sstevel@tonic-gate  * flow over the specified window. It uses probability to benefit TCP flows
49*0Sstevel@tonic-gate  * as it reduces the likelihood of dropping multiple packets within a TCP
50*0Sstevel@tonic-gate  * window without adversely effecting UDP flows.
51*0Sstevel@tonic-gate  */
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate int tswtcl_debug = 0;
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate /*
56*0Sstevel@tonic-gate  * Given a packet and the tswtcl_data it belongs to, this routine meters the
57*0Sstevel@tonic-gate  * ToS or DSCP for IPv4 and IPv6 resp. with the values configured for
58*0Sstevel@tonic-gate  * the tswtcl_data.
59*0Sstevel@tonic-gate  */
60*0Sstevel@tonic-gate /* ARGSUSED */
61*0Sstevel@tonic-gate int
tswtcl_process(mblk_t ** mpp,tswtcl_data_t * tswtcl_data,ipp_action_id_t * next_action)62*0Sstevel@tonic-gate tswtcl_process(mblk_t **mpp, tswtcl_data_t *tswtcl_data,
63*0Sstevel@tonic-gate     ipp_action_id_t *next_action)
64*0Sstevel@tonic-gate {
65*0Sstevel@tonic-gate 	ipha_t *ipha;
66*0Sstevel@tonic-gate 	hrtime_t now;
67*0Sstevel@tonic-gate 	ip6_t *ip6_hdr;
68*0Sstevel@tonic-gate 	uint32_t pkt_len;
69*0Sstevel@tonic-gate 	mblk_t *mp = *mpp;
70*0Sstevel@tonic-gate 	hrtime_t deltaT;
71*0Sstevel@tonic-gate 	uint64_t bitsinwin;
72*0Sstevel@tonic-gate 	uint32_t min = 0, additive, rnd;
73*0Sstevel@tonic-gate 	tswtcl_cfg_t *cfg_parms = tswtcl_data->cfg_parms;
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	if (mp == NULL) {
76*0Sstevel@tonic-gate 		tswtcl0dbg(("tswtcl_process: null mp!\n"));
77*0Sstevel@tonic-gate 		atomic_add_64(&tswtcl_data->epackets, 1);
78*0Sstevel@tonic-gate 		return (EINVAL);
79*0Sstevel@tonic-gate 	}
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	if (mp->b_datap->db_type != M_DATA) {
82*0Sstevel@tonic-gate 		if ((mp->b_cont != NULL) &&
83*0Sstevel@tonic-gate 		    (mp->b_cont->b_datap->db_type == M_DATA)) {
84*0Sstevel@tonic-gate 			mp = mp->b_cont;
85*0Sstevel@tonic-gate 		} else {
86*0Sstevel@tonic-gate 			tswtcl0dbg(("tswtcl_process: no data\n"));
87*0Sstevel@tonic-gate 			atomic_add_64(&tswtcl_data->epackets, 1);
88*0Sstevel@tonic-gate 			return (EINVAL);
89*0Sstevel@tonic-gate 		}
90*0Sstevel@tonic-gate 	}
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	/* Figure out the ToS/Traffic Class and length from the message */
93*0Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) < IP_SIMPLE_HDR_LENGTH) {
94*0Sstevel@tonic-gate 		if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
95*0Sstevel@tonic-gate 			tswtcl0dbg(("tswtcl_process: pullup error\n"));
96*0Sstevel@tonic-gate 			atomic_add_64(&tswtcl_data->epackets, 1);
97*0Sstevel@tonic-gate 			return (EINVAL);
98*0Sstevel@tonic-gate 		}
99*0Sstevel@tonic-gate 	}
100*0Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
101*0Sstevel@tonic-gate 	if (IPH_HDR_VERSION(ipha) == IPV4_VERSION) {
102*0Sstevel@tonic-gate 		pkt_len = ntohs(ipha->ipha_length);
103*0Sstevel@tonic-gate 	} else {
104*0Sstevel@tonic-gate 		ip6_hdr = (ip6_t *)mp->b_rptr;
105*0Sstevel@tonic-gate 		pkt_len = ntohs(ip6_hdr->ip6_plen) +
106*0Sstevel@tonic-gate 		    ip_hdr_length_v6(mp, ip6_hdr);
107*0Sstevel@tonic-gate 	}
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	/* Convert into bits */
110*0Sstevel@tonic-gate 	pkt_len <<= 3;
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	/* Get current time */
113*0Sstevel@tonic-gate 	now = gethrtime();
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	/* Update the avg_rate and win_front tswtcl_data */
116*0Sstevel@tonic-gate 	mutex_enter(&tswtcl_data->tswtcl_lock);
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	/* avg_rate = bits/sec and window in msec */
119*0Sstevel@tonic-gate 	bitsinwin = ((uint64_t)tswtcl_data->avg_rate * cfg_parms->window /
120*0Sstevel@tonic-gate 	    1000) + pkt_len;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	deltaT = now - tswtcl_data->win_front + cfg_parms->nsecwindow;
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	tswtcl_data->avg_rate = (uint64_t)bitsinwin * METER_SEC_TO_NSEC /
125*0Sstevel@tonic-gate 	    deltaT;
126*0Sstevel@tonic-gate 	tswtcl_data->win_front = now;
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	if (tswtcl_data->avg_rate <= cfg_parms->committed_rate) {
129*0Sstevel@tonic-gate 		*next_action = cfg_parms->green_action;
130*0Sstevel@tonic-gate 	} else if (tswtcl_data->avg_rate <= cfg_parms->peak_rate) {
131*0Sstevel@tonic-gate 		/*
132*0Sstevel@tonic-gate 		 * Compute the probability:
133*0Sstevel@tonic-gate 		 *
134*0Sstevel@tonic-gate 		 * p0 = (avg_rate - committed_rate) / avg_rate
135*0Sstevel@tonic-gate 		 *
136*0Sstevel@tonic-gate 		 * Yellow with probability p0
137*0Sstevel@tonic-gate 		 * Green with probability (1 - p0)
138*0Sstevel@tonic-gate 		 *
139*0Sstevel@tonic-gate 		 */
140*0Sstevel@tonic-gate 		uint32_t aminusc;
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 		/* Get a random no. betweeen 0 and avg_rate */
143*0Sstevel@tonic-gate 		(void) random_get_pseudo_bytes((uint8_t *)&additive,
144*0Sstevel@tonic-gate 		    sizeof (additive));
145*0Sstevel@tonic-gate 		rnd = min + (additive % (tswtcl_data->avg_rate - min + 1));
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 		aminusc = tswtcl_data->avg_rate - cfg_parms->committed_rate;
148*0Sstevel@tonic-gate 		if (aminusc >= rnd) {
149*0Sstevel@tonic-gate 			*next_action = cfg_parms->yellow_action;
150*0Sstevel@tonic-gate 		} else {
151*0Sstevel@tonic-gate 			*next_action = cfg_parms->green_action;
152*0Sstevel@tonic-gate 		}
153*0Sstevel@tonic-gate 	} else {
154*0Sstevel@tonic-gate 		/*
155*0Sstevel@tonic-gate 		 * Compute the probability:
156*0Sstevel@tonic-gate 		 *
157*0Sstevel@tonic-gate 		 * p1 = (avg_rate - peak_rate) / avg_rate
158*0Sstevel@tonic-gate 		 * p2 = (peak_rate - committed_rate) / avg_rate
159*0Sstevel@tonic-gate 		 *
160*0Sstevel@tonic-gate 		 * Red with probability p1
161*0Sstevel@tonic-gate 		 * Yellow with probability p2
162*0Sstevel@tonic-gate 		 * Green with probability (1 - (p1 + p2))
163*0Sstevel@tonic-gate 		 *
164*0Sstevel@tonic-gate 		 */
165*0Sstevel@tonic-gate 		uint32_t  aminusp;
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 		/* Get a random no. betweeen 0 and avg_rate */
168*0Sstevel@tonic-gate 		(void) random_get_pseudo_bytes((uint8_t *)&additive,
169*0Sstevel@tonic-gate 		    sizeof (additive));
170*0Sstevel@tonic-gate 		rnd = min + (additive % (tswtcl_data->avg_rate - min + 1));
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 		aminusp = tswtcl_data->avg_rate - cfg_parms->peak_rate;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 		if (aminusp >= rnd) {
175*0Sstevel@tonic-gate 			*next_action = cfg_parms->red_action;
176*0Sstevel@tonic-gate 		} else if ((cfg_parms->pminusc + aminusp) >= rnd) {
177*0Sstevel@tonic-gate 			*next_action = cfg_parms->yellow_action;
178*0Sstevel@tonic-gate 		} else {
179*0Sstevel@tonic-gate 			*next_action = cfg_parms->green_action;
180*0Sstevel@tonic-gate 		}
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	}
183*0Sstevel@tonic-gate 	mutex_exit(&tswtcl_data->tswtcl_lock);
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	/* Update Stats */
186*0Sstevel@tonic-gate 	if (*next_action == cfg_parms->green_action) {
187*0Sstevel@tonic-gate 		atomic_add_64(&tswtcl_data->green_packets, 1);
188*0Sstevel@tonic-gate 		atomic_add_64(&tswtcl_data->green_bits, pkt_len);
189*0Sstevel@tonic-gate 	} else if (*next_action == cfg_parms->yellow_action) {
190*0Sstevel@tonic-gate 		atomic_add_64(&tswtcl_data->yellow_packets, 1);
191*0Sstevel@tonic-gate 		atomic_add_64(&tswtcl_data->yellow_bits, pkt_len);
192*0Sstevel@tonic-gate 	} else {
193*0Sstevel@tonic-gate 		ASSERT(*next_action == cfg_parms->red_action);
194*0Sstevel@tonic-gate 		atomic_add_64(&tswtcl_data->red_packets, 1);
195*0Sstevel@tonic-gate 		atomic_add_64(&tswtcl_data->red_bits, pkt_len);
196*0Sstevel@tonic-gate 	}
197*0Sstevel@tonic-gate 	return (0);
198*0Sstevel@tonic-gate }
199