17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2002 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #include <sys/types.h> 287c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 297c478bd9Sstevel@tonic-gate #include <sys/conf.h> 307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 317c478bd9Sstevel@tonic-gate #include <netinet/in.h> 327c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h> 337c478bd9Sstevel@tonic-gate #include <netinet/ip6.h> 347c478bd9Sstevel@tonic-gate #include <inet/common.h> 357c478bd9Sstevel@tonic-gate #include <inet/ip.h> 367c478bd9Sstevel@tonic-gate #include <inet/ip6.h> 377c478bd9Sstevel@tonic-gate #include <ipp/meters/meter_impl.h> 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate /* 407c478bd9Sstevel@tonic-gate * Module : Single or Two Rate Metering module - tokenmt 417c478bd9Sstevel@tonic-gate * Description 427c478bd9Sstevel@tonic-gate * This module implements the metering part of RFC 2698 & 2697. It accepts the 437c478bd9Sstevel@tonic-gate * committed rate, peak rate (optional), committed burst and peak burst for a 447c478bd9Sstevel@tonic-gate * flow and determines if the flow is within the cfgd. rates and assigns 457c478bd9Sstevel@tonic-gate * next action appropriately.. 467c478bd9Sstevel@tonic-gate * If the peak rate is provided this acts as a two rate meter (RFC 2698), else 477c478bd9Sstevel@tonic-gate * a single rate meter (RFC 2697). If this is a two rate meter, then 487c478bd9Sstevel@tonic-gate * the outcome is either green, red or yellow. Else if this a single rate 497c478bd9Sstevel@tonic-gate * meter and the peak burst size is not provided, the outcome is either 507c478bd9Sstevel@tonic-gate * green or red. 517c478bd9Sstevel@tonic-gate * Internally, it maintains 2 token buckets, Tc & Tp, each filled with 527c478bd9Sstevel@tonic-gate * tokens equal to committed burst & peak burst respectively initially. 537c478bd9Sstevel@tonic-gate * When a packet arrives, tokens in Tc or Tp are updated at the committed 547c478bd9Sstevel@tonic-gate * or the peak rate up to a maximum of the committed or peak burst size. 557c478bd9Sstevel@tonic-gate * If there are enough tokens in Tc, the packet is Green, else if there are 567c478bd9Sstevel@tonic-gate * enough tokens in Tp, the packet is Yellow, else the packet is Red. In case 577c478bd9Sstevel@tonic-gate * of Green and Yellow packets, Tc and/or Tp is updated accordingly. 587c478bd9Sstevel@tonic-gate */ 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate int tokenmt_debug = 0; 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate /* Updating tokens */ 637c478bd9Sstevel@tonic-gate static void tokenmt_update_tokens(tokenmt_data_t *, hrtime_t); 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate /* 667c478bd9Sstevel@tonic-gate * Given a packet and the tokenmt_data it belongs to, this routine meters the 677c478bd9Sstevel@tonic-gate * ToS or DSCP for IPv4 and IPv6 resp. with the values configured for 687c478bd9Sstevel@tonic-gate * the tokenmt_data. 697c478bd9Sstevel@tonic-gate */ 707c478bd9Sstevel@tonic-gate int 717c478bd9Sstevel@tonic-gate tokenmt_process(mblk_t **mpp, tokenmt_data_t *tokenmt_data, 727c478bd9Sstevel@tonic-gate ipp_action_id_t *next_action) 737c478bd9Sstevel@tonic-gate { 747c478bd9Sstevel@tonic-gate uint8_t dscp; 757c478bd9Sstevel@tonic-gate ipha_t *ipha; 767c478bd9Sstevel@tonic-gate ip6_t *ip6_hdr; 777c478bd9Sstevel@tonic-gate uint32_t pkt_len; 787c478bd9Sstevel@tonic-gate mblk_t *mp = *mpp; 797c478bd9Sstevel@tonic-gate hrtime_t now; 807c478bd9Sstevel@tonic-gate enum meter_colour colour; 817c478bd9Sstevel@tonic-gate tokenmt_cfg_t *cfg_parms = tokenmt_data->cfg_parms; 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate if (mp == NULL) { 847c478bd9Sstevel@tonic-gate tokenmt0dbg(("tokenmt_process: null mp!\n")); 85*1a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&tokenmt_data->epackets); 867c478bd9Sstevel@tonic-gate return (EINVAL); 877c478bd9Sstevel@tonic-gate } 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate if (mp->b_datap->db_type != M_DATA) { 907c478bd9Sstevel@tonic-gate if ((mp->b_cont != NULL) && 917c478bd9Sstevel@tonic-gate (mp->b_cont->b_datap->db_type == M_DATA)) { 927c478bd9Sstevel@tonic-gate mp = mp->b_cont; 937c478bd9Sstevel@tonic-gate } else { 947c478bd9Sstevel@tonic-gate tokenmt0dbg(("tokenmt_process: no data\n")); 95*1a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&tokenmt_data->epackets); 967c478bd9Sstevel@tonic-gate return (EINVAL); 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate /* Figure out the ToS/Traffic Class and length from the message */ 1017c478bd9Sstevel@tonic-gate if ((mp->b_wptr - mp->b_rptr) < IP_SIMPLE_HDR_LENGTH) { 1027c478bd9Sstevel@tonic-gate if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { 1037c478bd9Sstevel@tonic-gate tokenmt0dbg(("tokenmt_process: pullup error\n")); 104*1a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&tokenmt_data->epackets); 1057c478bd9Sstevel@tonic-gate return (EINVAL); 1067c478bd9Sstevel@tonic-gate } 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate ipha = (ipha_t *)mp->b_rptr; 1097c478bd9Sstevel@tonic-gate if (IPH_HDR_VERSION(ipha) == IPV4_VERSION) { 1107c478bd9Sstevel@tonic-gate /* discard last 2 unused bits */ 1117c478bd9Sstevel@tonic-gate dscp = ipha->ipha_type_of_service; 1127c478bd9Sstevel@tonic-gate pkt_len = ntohs(ipha->ipha_length); 1137c478bd9Sstevel@tonic-gate } else { 1147c478bd9Sstevel@tonic-gate ip6_hdr = (ip6_t *)mp->b_rptr; 1157c478bd9Sstevel@tonic-gate /* discard ECN bits */ 1167c478bd9Sstevel@tonic-gate dscp = __IPV6_TCLASS_FROM_FLOW(ip6_hdr->ip6_vcf); 1177c478bd9Sstevel@tonic-gate pkt_len = ntohs(ip6_hdr->ip6_plen) + 1187c478bd9Sstevel@tonic-gate ip_hdr_length_v6(mp, ip6_hdr); 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate /* Convert into bits */ 1227c478bd9Sstevel@tonic-gate pkt_len <<= 3; 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate now = gethrtime(); 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate mutex_enter(&tokenmt_data->tokenmt_lock); 1277c478bd9Sstevel@tonic-gate /* Update the token counts */ 1287c478bd9Sstevel@tonic-gate tokenmt_update_tokens(tokenmt_data, now); 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate /* 1317c478bd9Sstevel@tonic-gate * Figure out the drop preced. for the pkt. Need to be careful here 1327c478bd9Sstevel@tonic-gate * because if the mode is set to COLOUR_AWARE, then the dscp value 1337c478bd9Sstevel@tonic-gate * is used regardless of whether it was explicitly set or not. 1347c478bd9Sstevel@tonic-gate * If the value is defaulted to 000 (drop precd.) then the pkt 1357c478bd9Sstevel@tonic-gate * will always be coloured RED. 1367c478bd9Sstevel@tonic-gate */ 1377c478bd9Sstevel@tonic-gate if (cfg_parms->tokenmt_type == SRTCL_TOKENMT) { 1387c478bd9Sstevel@tonic-gate if (!cfg_parms->colour_aware) { 1397c478bd9Sstevel@tonic-gate if (pkt_len <= tokenmt_data->committed_tokens) { 1407c478bd9Sstevel@tonic-gate tokenmt_data->committed_tokens -= pkt_len; 1417c478bd9Sstevel@tonic-gate *next_action = cfg_parms->green_action; 1427c478bd9Sstevel@tonic-gate } else if (pkt_len <= tokenmt_data->peak_tokens) { 1437c478bd9Sstevel@tonic-gate /* 1447c478bd9Sstevel@tonic-gate * Can't do this if yellow_action is not 1457c478bd9Sstevel@tonic-gate * configured. 1467c478bd9Sstevel@tonic-gate */ 1477c478bd9Sstevel@tonic-gate ASSERT(cfg_parms->yellow_action != 1487c478bd9Sstevel@tonic-gate TOKENMT_NO_ACTION); 1497c478bd9Sstevel@tonic-gate tokenmt_data->peak_tokens -= pkt_len; 1507c478bd9Sstevel@tonic-gate *next_action = cfg_parms->yellow_action; 1517c478bd9Sstevel@tonic-gate } else { 1527c478bd9Sstevel@tonic-gate *next_action = cfg_parms->red_action; 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate } else { 1557c478bd9Sstevel@tonic-gate colour = cfg_parms->dscp_to_colour[dscp >> 2]; 1567c478bd9Sstevel@tonic-gate if ((colour == TOKENMT_GREEN) && 1577c478bd9Sstevel@tonic-gate (pkt_len <= tokenmt_data->committed_tokens)) { 1587c478bd9Sstevel@tonic-gate tokenmt_data->committed_tokens -= pkt_len; 1597c478bd9Sstevel@tonic-gate *next_action = cfg_parms->green_action; 1607c478bd9Sstevel@tonic-gate } else if (((colour == TOKENMT_GREEN) || 1617c478bd9Sstevel@tonic-gate (colour == TOKENMT_YELLOW)) && 1627c478bd9Sstevel@tonic-gate (pkt_len <= tokenmt_data->peak_tokens)) { 1637c478bd9Sstevel@tonic-gate /* 1647c478bd9Sstevel@tonic-gate * Can't do this if yellow_action is not 1657c478bd9Sstevel@tonic-gate * configured. 1667c478bd9Sstevel@tonic-gate */ 1677c478bd9Sstevel@tonic-gate ASSERT(cfg_parms->yellow_action != 1687c478bd9Sstevel@tonic-gate TOKENMT_NO_ACTION); 1697c478bd9Sstevel@tonic-gate tokenmt_data->peak_tokens -= pkt_len; 1707c478bd9Sstevel@tonic-gate *next_action = cfg_parms->yellow_action; 1717c478bd9Sstevel@tonic-gate } else { 1727c478bd9Sstevel@tonic-gate *next_action = cfg_parms->red_action; 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate } else { 1767c478bd9Sstevel@tonic-gate if (!cfg_parms->colour_aware) { 1777c478bd9Sstevel@tonic-gate if (pkt_len > tokenmt_data->peak_tokens) { 1787c478bd9Sstevel@tonic-gate *next_action = cfg_parms->red_action; 1797c478bd9Sstevel@tonic-gate } else if (pkt_len > tokenmt_data->committed_tokens) { 1807c478bd9Sstevel@tonic-gate /* 1817c478bd9Sstevel@tonic-gate * Can't do this if yellow_action is not 1827c478bd9Sstevel@tonic-gate * configured. 1837c478bd9Sstevel@tonic-gate */ 1847c478bd9Sstevel@tonic-gate ASSERT(cfg_parms->yellow_action != 1857c478bd9Sstevel@tonic-gate TOKENMT_NO_ACTION); 1867c478bd9Sstevel@tonic-gate tokenmt_data->peak_tokens -= pkt_len; 1877c478bd9Sstevel@tonic-gate *next_action = cfg_parms->yellow_action; 1887c478bd9Sstevel@tonic-gate } else { 1897c478bd9Sstevel@tonic-gate tokenmt_data->committed_tokens -= pkt_len; 1907c478bd9Sstevel@tonic-gate tokenmt_data->peak_tokens -= pkt_len; 1917c478bd9Sstevel@tonic-gate *next_action = cfg_parms->green_action; 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate } else { 1947c478bd9Sstevel@tonic-gate colour = cfg_parms->dscp_to_colour[dscp >> 2]; 1957c478bd9Sstevel@tonic-gate if ((colour == TOKENMT_RED) || 1967c478bd9Sstevel@tonic-gate (pkt_len > tokenmt_data->peak_tokens)) { 1977c478bd9Sstevel@tonic-gate *next_action = cfg_parms->red_action; 1987c478bd9Sstevel@tonic-gate } else if ((colour == TOKENMT_YELLOW) || 1997c478bd9Sstevel@tonic-gate (pkt_len > tokenmt_data->committed_tokens)) { 2007c478bd9Sstevel@tonic-gate /* 2017c478bd9Sstevel@tonic-gate * Can't do this if yellow_action is not 2027c478bd9Sstevel@tonic-gate * configured. 2037c478bd9Sstevel@tonic-gate */ 2047c478bd9Sstevel@tonic-gate ASSERT(cfg_parms->yellow_action != 2057c478bd9Sstevel@tonic-gate TOKENMT_NO_ACTION); 2067c478bd9Sstevel@tonic-gate tokenmt_data->peak_tokens -= pkt_len; 2077c478bd9Sstevel@tonic-gate *next_action = cfg_parms->yellow_action; 2087c478bd9Sstevel@tonic-gate } else { 2097c478bd9Sstevel@tonic-gate tokenmt_data->committed_tokens -= pkt_len; 2107c478bd9Sstevel@tonic-gate tokenmt_data->peak_tokens -= pkt_len; 2117c478bd9Sstevel@tonic-gate *next_action = cfg_parms->green_action; 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate mutex_exit(&tokenmt_data->tokenmt_lock); 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate /* Update Stats */ 2187c478bd9Sstevel@tonic-gate if (*next_action == cfg_parms->green_action) { 219*1a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&tokenmt_data->green_packets); 2207c478bd9Sstevel@tonic-gate atomic_add_64(&tokenmt_data->green_bits, pkt_len); 2217c478bd9Sstevel@tonic-gate } else if (*next_action == cfg_parms->yellow_action) { 222*1a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&tokenmt_data->yellow_packets); 2237c478bd9Sstevel@tonic-gate atomic_add_64(&tokenmt_data->yellow_bits, pkt_len); 2247c478bd9Sstevel@tonic-gate } else { 2257c478bd9Sstevel@tonic-gate ASSERT(*next_action == cfg_parms->red_action); 226*1a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&tokenmt_data->red_packets); 2277c478bd9Sstevel@tonic-gate atomic_add_64(&tokenmt_data->red_bits, pkt_len); 2287c478bd9Sstevel@tonic-gate } 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate return (0); 2317c478bd9Sstevel@tonic-gate } 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate void 2347c478bd9Sstevel@tonic-gate tokenmt_update_tokens(tokenmt_data_t *tokenmt_data, hrtime_t now) 2357c478bd9Sstevel@tonic-gate { 2367c478bd9Sstevel@tonic-gate tokenmt_cfg_t *cfg_parms = (tokenmt_cfg_t *)tokenmt_data->cfg_parms; 2377c478bd9Sstevel@tonic-gate hrtime_t diff = now - tokenmt_data->last_seen; 2387c478bd9Sstevel@tonic-gate uint64_t tokens; 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate switch (cfg_parms->tokenmt_type) { 2417c478bd9Sstevel@tonic-gate case SRTCL_TOKENMT: 2427c478bd9Sstevel@tonic-gate tokens = (cfg_parms->committed_rate * diff) / 2437c478bd9Sstevel@tonic-gate METER_SEC_TO_NSEC; 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate /* 2467c478bd9Sstevel@tonic-gate * Add tokens at the committed rate to 2477c478bd9Sstevel@tonic-gate * committed_tokens. If they are in excess of 2487c478bd9Sstevel@tonic-gate * the committed burst, add the excess to 2497c478bd9Sstevel@tonic-gate * peak_tokens, capped to peak_burst. 2507c478bd9Sstevel@tonic-gate */ 2517c478bd9Sstevel@tonic-gate if ((tokenmt_data->committed_tokens + tokens) > 2527c478bd9Sstevel@tonic-gate cfg_parms->committed_burst) { 2537c478bd9Sstevel@tonic-gate tokens = tokenmt_data->committed_tokens 2547c478bd9Sstevel@tonic-gate + tokens - 2557c478bd9Sstevel@tonic-gate cfg_parms->committed_burst; 2567c478bd9Sstevel@tonic-gate tokenmt_data->committed_tokens = 2577c478bd9Sstevel@tonic-gate cfg_parms->committed_burst; 2587c478bd9Sstevel@tonic-gate tokenmt_data->peak_tokens = 2597c478bd9Sstevel@tonic-gate MIN(cfg_parms->peak_burst, 2607c478bd9Sstevel@tonic-gate tokenmt_data->peak_tokens + 2617c478bd9Sstevel@tonic-gate tokens); 2627c478bd9Sstevel@tonic-gate } else { 2637c478bd9Sstevel@tonic-gate tokenmt_data->committed_tokens += 2647c478bd9Sstevel@tonic-gate tokens; 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate break; 2677c478bd9Sstevel@tonic-gate case TRTCL_TOKENMT: 2687c478bd9Sstevel@tonic-gate /* Fill at the committed rate */ 2697c478bd9Sstevel@tonic-gate tokens = (diff * cfg_parms->committed_rate) / 2707c478bd9Sstevel@tonic-gate METER_SEC_TO_NSEC; 2717c478bd9Sstevel@tonic-gate tokenmt_data->committed_tokens = 2727c478bd9Sstevel@tonic-gate MIN(cfg_parms->committed_burst, 2737c478bd9Sstevel@tonic-gate tokenmt_data->committed_tokens + tokens); 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate /* Fill at the peak rate */ 2767c478bd9Sstevel@tonic-gate tokens = (diff * cfg_parms->peak_rate) / 2777c478bd9Sstevel@tonic-gate METER_SEC_TO_NSEC; 2787c478bd9Sstevel@tonic-gate tokenmt_data->peak_tokens = 2797c478bd9Sstevel@tonic-gate MIN(cfg_parms->peak_burst, 2807c478bd9Sstevel@tonic-gate tokenmt_data->peak_tokens + tokens); 2817c478bd9Sstevel@tonic-gate break; 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate tokenmt_data->last_seen = now; 2847c478bd9Sstevel@tonic-gate } 285