7 * with the License.
8 *
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.
13 *
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]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 #pragma ident "%Z%%M% %I% %E% SMI"
28
29 #include <sys/types.h>
30 #include <sys/kmem.h>
31 #include <sys/conf.h>
32 #include <sys/sysmacros.h>
33 #include <netinet/in.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/ip6.h>
36 #include <inet/common.h>
37 #include <inet/ip.h>
38 #include <inet/ip6.h>
39 #include <ipp/meters/meter_impl.h>
40
41 /*
42 * Module : Single or Two Rate Metering module - tokenmt
43 * Description
44 * This module implements the metering part of RFC 2698 & 2697. It accepts the
45 * committed rate, peak rate (optional), committed burst and peak burst for a
46 * flow and determines if the flow is within the cfgd. rates and assigns
47 * next action appropriately..
48 * If the peak rate is provided this acts as a two rate meter (RFC 2698), else
67 /*
68 * Given a packet and the tokenmt_data it belongs to, this routine meters the
69 * ToS or DSCP for IPv4 and IPv6 resp. with the values configured for
70 * the tokenmt_data.
71 */
72 int
73 tokenmt_process(mblk_t **mpp, tokenmt_data_t *tokenmt_data,
74 ipp_action_id_t *next_action)
75 {
76 uint8_t dscp;
77 ipha_t *ipha;
78 ip6_t *ip6_hdr;
79 uint32_t pkt_len;
80 mblk_t *mp = *mpp;
81 hrtime_t now;
82 enum meter_colour colour;
83 tokenmt_cfg_t *cfg_parms = tokenmt_data->cfg_parms;
84
85 if (mp == NULL) {
86 tokenmt0dbg(("tokenmt_process: null mp!\n"));
87 atomic_add_64(&tokenmt_data->epackets, 1);
88 return (EINVAL);
89 }
90
91 if (mp->b_datap->db_type != M_DATA) {
92 if ((mp->b_cont != NULL) &&
93 (mp->b_cont->b_datap->db_type == M_DATA)) {
94 mp = mp->b_cont;
95 } else {
96 tokenmt0dbg(("tokenmt_process: no data\n"));
97 atomic_add_64(&tokenmt_data->epackets, 1);
98 return (EINVAL);
99 }
100 }
101
102 /* Figure out the ToS/Traffic Class and length from the message */
103 if ((mp->b_wptr - mp->b_rptr) < IP_SIMPLE_HDR_LENGTH) {
104 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
105 tokenmt0dbg(("tokenmt_process: pullup error\n"));
106 atomic_add_64(&tokenmt_data->epackets, 1);
107 return (EINVAL);
108 }
109 }
110 ipha = (ipha_t *)mp->b_rptr;
111 if (IPH_HDR_VERSION(ipha) == IPV4_VERSION) {
112 /* discard last 2 unused bits */
113 dscp = ipha->ipha_type_of_service;
114 pkt_len = ntohs(ipha->ipha_length);
115 } else {
116 ip6_hdr = (ip6_t *)mp->b_rptr;
117 /* discard ECN bits */
118 dscp = __IPV6_TCLASS_FROM_FLOW(ip6_hdr->ip6_vcf);
119 pkt_len = ntohs(ip6_hdr->ip6_plen) +
120 ip_hdr_length_v6(mp, ip6_hdr);
121 }
122
123 /* Convert into bits */
124 pkt_len <<= 3;
125
126 now = gethrtime();
201 (pkt_len > tokenmt_data->committed_tokens)) {
202 /*
203 * Can't do this if yellow_action is not
204 * configured.
205 */
206 ASSERT(cfg_parms->yellow_action !=
207 TOKENMT_NO_ACTION);
208 tokenmt_data->peak_tokens -= pkt_len;
209 *next_action = cfg_parms->yellow_action;
210 } else {
211 tokenmt_data->committed_tokens -= pkt_len;
212 tokenmt_data->peak_tokens -= pkt_len;
213 *next_action = cfg_parms->green_action;
214 }
215 }
216 }
217 mutex_exit(&tokenmt_data->tokenmt_lock);
218
219 /* Update Stats */
220 if (*next_action == cfg_parms->green_action) {
221 atomic_add_64(&tokenmt_data->green_packets, 1);
222 atomic_add_64(&tokenmt_data->green_bits, pkt_len);
223 } else if (*next_action == cfg_parms->yellow_action) {
224 atomic_add_64(&tokenmt_data->yellow_packets, 1);
225 atomic_add_64(&tokenmt_data->yellow_bits, pkt_len);
226 } else {
227 ASSERT(*next_action == cfg_parms->red_action);
228 atomic_add_64(&tokenmt_data->red_packets, 1);
229 atomic_add_64(&tokenmt_data->red_bits, pkt_len);
230 }
231
232 return (0);
233 }
234
235 void
236 tokenmt_update_tokens(tokenmt_data_t *tokenmt_data, hrtime_t now)
237 {
238 tokenmt_cfg_t *cfg_parms = (tokenmt_cfg_t *)tokenmt_data->cfg_parms;
239 hrtime_t diff = now - tokenmt_data->last_seen;
240 uint64_t tokens;
241
242 switch (cfg_parms->tokenmt_type) {
243 case SRTCL_TOKENMT:
244 tokens = (cfg_parms->committed_rate * diff) /
245 METER_SEC_TO_NSEC;
246
247 /*
248 * Add tokens at the committed rate to
|
7 * with the License.
8 *
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.
13 *
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]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 #include <sys/types.h>
28 #include <sys/kmem.h>
29 #include <sys/conf.h>
30 #include <sys/sysmacros.h>
31 #include <netinet/in.h>
32 #include <netinet/in_systm.h>
33 #include <netinet/ip6.h>
34 #include <inet/common.h>
35 #include <inet/ip.h>
36 #include <inet/ip6.h>
37 #include <ipp/meters/meter_impl.h>
38
39 /*
40 * Module : Single or Two Rate Metering module - tokenmt
41 * Description
42 * This module implements the metering part of RFC 2698 & 2697. It accepts the
43 * committed rate, peak rate (optional), committed burst and peak burst for a
44 * flow and determines if the flow is within the cfgd. rates and assigns
45 * next action appropriately..
46 * If the peak rate is provided this acts as a two rate meter (RFC 2698), else
65 /*
66 * Given a packet and the tokenmt_data it belongs to, this routine meters the
67 * ToS or DSCP for IPv4 and IPv6 resp. with the values configured for
68 * the tokenmt_data.
69 */
70 int
71 tokenmt_process(mblk_t **mpp, tokenmt_data_t *tokenmt_data,
72 ipp_action_id_t *next_action)
73 {
74 uint8_t dscp;
75 ipha_t *ipha;
76 ip6_t *ip6_hdr;
77 uint32_t pkt_len;
78 mblk_t *mp = *mpp;
79 hrtime_t now;
80 enum meter_colour colour;
81 tokenmt_cfg_t *cfg_parms = tokenmt_data->cfg_parms;
82
83 if (mp == NULL) {
84 tokenmt0dbg(("tokenmt_process: null mp!\n"));
85 atomic_inc_64(&tokenmt_data->epackets);
86 return (EINVAL);
87 }
88
89 if (mp->b_datap->db_type != M_DATA) {
90 if ((mp->b_cont != NULL) &&
91 (mp->b_cont->b_datap->db_type == M_DATA)) {
92 mp = mp->b_cont;
93 } else {
94 tokenmt0dbg(("tokenmt_process: no data\n"));
95 atomic_inc_64(&tokenmt_data->epackets);
96 return (EINVAL);
97 }
98 }
99
100 /* Figure out the ToS/Traffic Class and length from the message */
101 if ((mp->b_wptr - mp->b_rptr) < IP_SIMPLE_HDR_LENGTH) {
102 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
103 tokenmt0dbg(("tokenmt_process: pullup error\n"));
104 atomic_inc_64(&tokenmt_data->epackets);
105 return (EINVAL);
106 }
107 }
108 ipha = (ipha_t *)mp->b_rptr;
109 if (IPH_HDR_VERSION(ipha) == IPV4_VERSION) {
110 /* discard last 2 unused bits */
111 dscp = ipha->ipha_type_of_service;
112 pkt_len = ntohs(ipha->ipha_length);
113 } else {
114 ip6_hdr = (ip6_t *)mp->b_rptr;
115 /* discard ECN bits */
116 dscp = __IPV6_TCLASS_FROM_FLOW(ip6_hdr->ip6_vcf);
117 pkt_len = ntohs(ip6_hdr->ip6_plen) +
118 ip_hdr_length_v6(mp, ip6_hdr);
119 }
120
121 /* Convert into bits */
122 pkt_len <<= 3;
123
124 now = gethrtime();
199 (pkt_len > tokenmt_data->committed_tokens)) {
200 /*
201 * Can't do this if yellow_action is not
202 * configured.
203 */
204 ASSERT(cfg_parms->yellow_action !=
205 TOKENMT_NO_ACTION);
206 tokenmt_data->peak_tokens -= pkt_len;
207 *next_action = cfg_parms->yellow_action;
208 } else {
209 tokenmt_data->committed_tokens -= pkt_len;
210 tokenmt_data->peak_tokens -= pkt_len;
211 *next_action = cfg_parms->green_action;
212 }
213 }
214 }
215 mutex_exit(&tokenmt_data->tokenmt_lock);
216
217 /* Update Stats */
218 if (*next_action == cfg_parms->green_action) {
219 atomic_inc_64(&tokenmt_data->green_packets);
220 atomic_add_64(&tokenmt_data->green_bits, pkt_len);
221 } else if (*next_action == cfg_parms->yellow_action) {
222 atomic_inc_64(&tokenmt_data->yellow_packets);
223 atomic_add_64(&tokenmt_data->yellow_bits, pkt_len);
224 } else {
225 ASSERT(*next_action == cfg_parms->red_action);
226 atomic_inc_64(&tokenmt_data->red_packets);
227 atomic_add_64(&tokenmt_data->red_bits, pkt_len);
228 }
229
230 return (0);
231 }
232
233 void
234 tokenmt_update_tokens(tokenmt_data_t *tokenmt_data, hrtime_t now)
235 {
236 tokenmt_cfg_t *cfg_parms = (tokenmt_cfg_t *)tokenmt_data->cfg_parms;
237 hrtime_t diff = now - tokenmt_data->last_seen;
238 uint64_t tokens;
239
240 switch (cfg_parms->tokenmt_type) {
241 case SRTCL_TOKENMT:
242 tokens = (cfg_parms->committed_rate * diff) /
243 METER_SEC_TO_NSEC;
244
245 /*
246 * Add tokens at the committed rate to
|