Print this page
5045 use atomic_{inc,dec}_* instead of atomic_add_*
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/ipp/meters/tokenmt.c
+++ new/usr/src/uts/common/ipp/meters/tokenmt.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License, Version 1.0 only
6 6 * (the "License"). You may not use this file except in compliance
7 7 * with the License.
8 8 *
9 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 10 * or http://www.opensolaris.org/os/licensing.
11 11 * See the License for the specific language governing permissions
12 12 * and limitations under the License.
13 13 *
14 14 * When distributing Covered Code, include this CDDL HEADER in each
15 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 16 * If applicable, add the following below this CDDL HEADER, with the
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
17 17 * fields enclosed by brackets "[]" replaced with your own identifying
18 18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 19 *
20 20 * CDDL HEADER END
21 21 */
22 22 /*
23 23 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 -#pragma ident "%Z%%M% %I% %E% SMI"
28 -
29 27 #include <sys/types.h>
30 28 #include <sys/kmem.h>
31 29 #include <sys/conf.h>
32 30 #include <sys/sysmacros.h>
33 31 #include <netinet/in.h>
34 32 #include <netinet/in_systm.h>
35 33 #include <netinet/ip6.h>
36 34 #include <inet/common.h>
37 35 #include <inet/ip.h>
38 36 #include <inet/ip6.h>
39 37 #include <ipp/meters/meter_impl.h>
40 38
41 39 /*
42 40 * Module : Single or Two Rate Metering module - tokenmt
43 41 * Description
44 42 * This module implements the metering part of RFC 2698 & 2697. It accepts the
45 43 * committed rate, peak rate (optional), committed burst and peak burst for a
46 44 * flow and determines if the flow is within the cfgd. rates and assigns
47 45 * next action appropriately..
48 46 * If the peak rate is provided this acts as a two rate meter (RFC 2698), else
49 47 * a single rate meter (RFC 2697). If this is a two rate meter, then
50 48 * the outcome is either green, red or yellow. Else if this a single rate
51 49 * meter and the peak burst size is not provided, the outcome is either
52 50 * green or red.
53 51 * Internally, it maintains 2 token buckets, Tc & Tp, each filled with
54 52 * tokens equal to committed burst & peak burst respectively initially.
55 53 * When a packet arrives, tokens in Tc or Tp are updated at the committed
56 54 * or the peak rate up to a maximum of the committed or peak burst size.
57 55 * If there are enough tokens in Tc, the packet is Green, else if there are
58 56 * enough tokens in Tp, the packet is Yellow, else the packet is Red. In case
59 57 * of Green and Yellow packets, Tc and/or Tp is updated accordingly.
60 58 */
61 59
62 60 int tokenmt_debug = 0;
63 61
64 62 /* Updating tokens */
65 63 static void tokenmt_update_tokens(tokenmt_data_t *, hrtime_t);
66 64
67 65 /*
68 66 * Given a packet and the tokenmt_data it belongs to, this routine meters the
69 67 * ToS or DSCP for IPv4 and IPv6 resp. with the values configured for
70 68 * the tokenmt_data.
71 69 */
72 70 int
73 71 tokenmt_process(mblk_t **mpp, tokenmt_data_t *tokenmt_data,
74 72 ipp_action_id_t *next_action)
75 73 {
76 74 uint8_t dscp;
↓ open down ↓ |
38 lines elided |
↑ open up ↑ |
77 75 ipha_t *ipha;
78 76 ip6_t *ip6_hdr;
79 77 uint32_t pkt_len;
80 78 mblk_t *mp = *mpp;
81 79 hrtime_t now;
82 80 enum meter_colour colour;
83 81 tokenmt_cfg_t *cfg_parms = tokenmt_data->cfg_parms;
84 82
85 83 if (mp == NULL) {
86 84 tokenmt0dbg(("tokenmt_process: null mp!\n"));
87 - atomic_add_64(&tokenmt_data->epackets, 1);
85 + atomic_inc_64(&tokenmt_data->epackets);
88 86 return (EINVAL);
89 87 }
90 88
91 89 if (mp->b_datap->db_type != M_DATA) {
92 90 if ((mp->b_cont != NULL) &&
93 91 (mp->b_cont->b_datap->db_type == M_DATA)) {
94 92 mp = mp->b_cont;
95 93 } else {
96 94 tokenmt0dbg(("tokenmt_process: no data\n"));
97 - atomic_add_64(&tokenmt_data->epackets, 1);
95 + atomic_inc_64(&tokenmt_data->epackets);
98 96 return (EINVAL);
99 97 }
100 98 }
101 99
102 100 /* Figure out the ToS/Traffic Class and length from the message */
103 101 if ((mp->b_wptr - mp->b_rptr) < IP_SIMPLE_HDR_LENGTH) {
104 102 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
105 103 tokenmt0dbg(("tokenmt_process: pullup error\n"));
106 - atomic_add_64(&tokenmt_data->epackets, 1);
104 + atomic_inc_64(&tokenmt_data->epackets);
107 105 return (EINVAL);
108 106 }
109 107 }
110 108 ipha = (ipha_t *)mp->b_rptr;
111 109 if (IPH_HDR_VERSION(ipha) == IPV4_VERSION) {
112 110 /* discard last 2 unused bits */
113 111 dscp = ipha->ipha_type_of_service;
114 112 pkt_len = ntohs(ipha->ipha_length);
115 113 } else {
116 114 ip6_hdr = (ip6_t *)mp->b_rptr;
117 115 /* discard ECN bits */
118 116 dscp = __IPV6_TCLASS_FROM_FLOW(ip6_hdr->ip6_vcf);
119 117 pkt_len = ntohs(ip6_hdr->ip6_plen) +
120 118 ip_hdr_length_v6(mp, ip6_hdr);
121 119 }
122 120
123 121 /* Convert into bits */
124 122 pkt_len <<= 3;
125 123
126 124 now = gethrtime();
127 125
128 126 mutex_enter(&tokenmt_data->tokenmt_lock);
129 127 /* Update the token counts */
130 128 tokenmt_update_tokens(tokenmt_data, now);
131 129
132 130 /*
133 131 * Figure out the drop preced. for the pkt. Need to be careful here
134 132 * because if the mode is set to COLOUR_AWARE, then the dscp value
135 133 * is used regardless of whether it was explicitly set or not.
136 134 * If the value is defaulted to 000 (drop precd.) then the pkt
137 135 * will always be coloured RED.
138 136 */
139 137 if (cfg_parms->tokenmt_type == SRTCL_TOKENMT) {
140 138 if (!cfg_parms->colour_aware) {
141 139 if (pkt_len <= tokenmt_data->committed_tokens) {
142 140 tokenmt_data->committed_tokens -= pkt_len;
143 141 *next_action = cfg_parms->green_action;
144 142 } else if (pkt_len <= tokenmt_data->peak_tokens) {
145 143 /*
146 144 * Can't do this if yellow_action is not
147 145 * configured.
148 146 */
149 147 ASSERT(cfg_parms->yellow_action !=
150 148 TOKENMT_NO_ACTION);
151 149 tokenmt_data->peak_tokens -= pkt_len;
152 150 *next_action = cfg_parms->yellow_action;
153 151 } else {
154 152 *next_action = cfg_parms->red_action;
155 153 }
156 154 } else {
157 155 colour = cfg_parms->dscp_to_colour[dscp >> 2];
158 156 if ((colour == TOKENMT_GREEN) &&
159 157 (pkt_len <= tokenmt_data->committed_tokens)) {
160 158 tokenmt_data->committed_tokens -= pkt_len;
161 159 *next_action = cfg_parms->green_action;
162 160 } else if (((colour == TOKENMT_GREEN) ||
163 161 (colour == TOKENMT_YELLOW)) &&
164 162 (pkt_len <= tokenmt_data->peak_tokens)) {
165 163 /*
166 164 * Can't do this if yellow_action is not
167 165 * configured.
168 166 */
169 167 ASSERT(cfg_parms->yellow_action !=
170 168 TOKENMT_NO_ACTION);
171 169 tokenmt_data->peak_tokens -= pkt_len;
172 170 *next_action = cfg_parms->yellow_action;
173 171 } else {
174 172 *next_action = cfg_parms->red_action;
175 173 }
176 174 }
177 175 } else {
178 176 if (!cfg_parms->colour_aware) {
179 177 if (pkt_len > tokenmt_data->peak_tokens) {
180 178 *next_action = cfg_parms->red_action;
181 179 } else if (pkt_len > tokenmt_data->committed_tokens) {
182 180 /*
183 181 * Can't do this if yellow_action is not
184 182 * configured.
185 183 */
186 184 ASSERT(cfg_parms->yellow_action !=
187 185 TOKENMT_NO_ACTION);
188 186 tokenmt_data->peak_tokens -= pkt_len;
189 187 *next_action = cfg_parms->yellow_action;
190 188 } else {
191 189 tokenmt_data->committed_tokens -= pkt_len;
192 190 tokenmt_data->peak_tokens -= pkt_len;
193 191 *next_action = cfg_parms->green_action;
194 192 }
195 193 } else {
196 194 colour = cfg_parms->dscp_to_colour[dscp >> 2];
197 195 if ((colour == TOKENMT_RED) ||
198 196 (pkt_len > tokenmt_data->peak_tokens)) {
199 197 *next_action = cfg_parms->red_action;
200 198 } else if ((colour == TOKENMT_YELLOW) ||
201 199 (pkt_len > tokenmt_data->committed_tokens)) {
202 200 /*
203 201 * Can't do this if yellow_action is not
204 202 * configured.
205 203 */
206 204 ASSERT(cfg_parms->yellow_action !=
207 205 TOKENMT_NO_ACTION);
208 206 tokenmt_data->peak_tokens -= pkt_len;
209 207 *next_action = cfg_parms->yellow_action;
210 208 } else {
↓ open down ↓ |
94 lines elided |
↑ open up ↑ |
211 209 tokenmt_data->committed_tokens -= pkt_len;
212 210 tokenmt_data->peak_tokens -= pkt_len;
213 211 *next_action = cfg_parms->green_action;
214 212 }
215 213 }
216 214 }
217 215 mutex_exit(&tokenmt_data->tokenmt_lock);
218 216
219 217 /* Update Stats */
220 218 if (*next_action == cfg_parms->green_action) {
221 - atomic_add_64(&tokenmt_data->green_packets, 1);
219 + atomic_inc_64(&tokenmt_data->green_packets);
222 220 atomic_add_64(&tokenmt_data->green_bits, pkt_len);
223 221 } else if (*next_action == cfg_parms->yellow_action) {
224 - atomic_add_64(&tokenmt_data->yellow_packets, 1);
222 + atomic_inc_64(&tokenmt_data->yellow_packets);
225 223 atomic_add_64(&tokenmt_data->yellow_bits, pkt_len);
226 224 } else {
227 225 ASSERT(*next_action == cfg_parms->red_action);
228 - atomic_add_64(&tokenmt_data->red_packets, 1);
226 + atomic_inc_64(&tokenmt_data->red_packets);
229 227 atomic_add_64(&tokenmt_data->red_bits, pkt_len);
230 228 }
231 229
232 230 return (0);
233 231 }
234 232
235 233 void
236 234 tokenmt_update_tokens(tokenmt_data_t *tokenmt_data, hrtime_t now)
237 235 {
238 236 tokenmt_cfg_t *cfg_parms = (tokenmt_cfg_t *)tokenmt_data->cfg_parms;
239 237 hrtime_t diff = now - tokenmt_data->last_seen;
240 238 uint64_t tokens;
241 239
242 240 switch (cfg_parms->tokenmt_type) {
243 241 case SRTCL_TOKENMT:
244 242 tokens = (cfg_parms->committed_rate * diff) /
245 243 METER_SEC_TO_NSEC;
246 244
247 245 /*
248 246 * Add tokens at the committed rate to
249 247 * committed_tokens. If they are in excess of
250 248 * the committed burst, add the excess to
251 249 * peak_tokens, capped to peak_burst.
252 250 */
253 251 if ((tokenmt_data->committed_tokens + tokens) >
254 252 cfg_parms->committed_burst) {
255 253 tokens = tokenmt_data->committed_tokens
256 254 + tokens -
257 255 cfg_parms->committed_burst;
258 256 tokenmt_data->committed_tokens =
259 257 cfg_parms->committed_burst;
260 258 tokenmt_data->peak_tokens =
261 259 MIN(cfg_parms->peak_burst,
262 260 tokenmt_data->peak_tokens +
263 261 tokens);
264 262 } else {
265 263 tokenmt_data->committed_tokens +=
266 264 tokens;
267 265 }
268 266 break;
269 267 case TRTCL_TOKENMT:
270 268 /* Fill at the committed rate */
271 269 tokens = (diff * cfg_parms->committed_rate) /
272 270 METER_SEC_TO_NSEC;
273 271 tokenmt_data->committed_tokens =
274 272 MIN(cfg_parms->committed_burst,
275 273 tokenmt_data->committed_tokens + tokens);
276 274
277 275 /* Fill at the peak rate */
278 276 tokens = (diff * cfg_parms->peak_rate) /
279 277 METER_SEC_TO_NSEC;
280 278 tokenmt_data->peak_tokens =
281 279 MIN(cfg_parms->peak_burst,
282 280 tokenmt_data->peak_tokens + tokens);
283 281 break;
284 282 }
285 283 tokenmt_data->last_seen = now;
286 284 }
↓ open down ↓ |
48 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX