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