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 2005 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/random.h> 307c478bd9Sstevel@tonic-gate #include <netinet/in.h> 317c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h> 327c478bd9Sstevel@tonic-gate #include <netinet/ip6.h> 337c478bd9Sstevel@tonic-gate #include <inet/common.h> 347c478bd9Sstevel@tonic-gate #include <inet/ip.h> 357c478bd9Sstevel@tonic-gate #include <inet/ip6.h> 367c478bd9Sstevel@tonic-gate #include <ipp/meters/meter_impl.h> 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate /* 397c478bd9Sstevel@tonic-gate * Module : Time Sliding Window meter - tswtclmtr 407c478bd9Sstevel@tonic-gate * Description 417c478bd9Sstevel@tonic-gate * This module implements the metering part of RFC 2859. It accepts the 427c478bd9Sstevel@tonic-gate * committed rate, peak rate and the window for a flow and determines 437c478bd9Sstevel@tonic-gate * if the flow is within the committed/peak rate and assigns the appropriate 447c478bd9Sstevel@tonic-gate * next action. 457c478bd9Sstevel@tonic-gate * The meter provides an estimate of the running average bandwidth for the 467c478bd9Sstevel@tonic-gate * flow over the specified window. It uses probability to benefit TCP flows 477c478bd9Sstevel@tonic-gate * as it reduces the likelihood of dropping multiple packets within a TCP 487c478bd9Sstevel@tonic-gate * window without adversely effecting UDP flows. 497c478bd9Sstevel@tonic-gate */ 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate int tswtcl_debug = 0; 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate /* 547c478bd9Sstevel@tonic-gate * Given a packet and the tswtcl_data it belongs to, this routine meters the 557c478bd9Sstevel@tonic-gate * ToS or DSCP for IPv4 and IPv6 resp. with the values configured for 567c478bd9Sstevel@tonic-gate * the tswtcl_data. 577c478bd9Sstevel@tonic-gate */ 587c478bd9Sstevel@tonic-gate /* ARGSUSED */ 597c478bd9Sstevel@tonic-gate int 607c478bd9Sstevel@tonic-gate tswtcl_process(mblk_t **mpp, tswtcl_data_t *tswtcl_data, 617c478bd9Sstevel@tonic-gate ipp_action_id_t *next_action) 627c478bd9Sstevel@tonic-gate { 637c478bd9Sstevel@tonic-gate ipha_t *ipha; 647c478bd9Sstevel@tonic-gate hrtime_t now; 657c478bd9Sstevel@tonic-gate ip6_t *ip6_hdr; 667c478bd9Sstevel@tonic-gate uint32_t pkt_len; 677c478bd9Sstevel@tonic-gate mblk_t *mp = *mpp; 687c478bd9Sstevel@tonic-gate hrtime_t deltaT; 697c478bd9Sstevel@tonic-gate uint64_t bitsinwin; 707c478bd9Sstevel@tonic-gate uint32_t min = 0, additive, rnd; 717c478bd9Sstevel@tonic-gate tswtcl_cfg_t *cfg_parms = tswtcl_data->cfg_parms; 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate if (mp == NULL) { 747c478bd9Sstevel@tonic-gate tswtcl0dbg(("tswtcl_process: null mp!\n")); 75*1a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&tswtcl_data->epackets); 767c478bd9Sstevel@tonic-gate return (EINVAL); 777c478bd9Sstevel@tonic-gate } 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate if (mp->b_datap->db_type != M_DATA) { 807c478bd9Sstevel@tonic-gate if ((mp->b_cont != NULL) && 817c478bd9Sstevel@tonic-gate (mp->b_cont->b_datap->db_type == M_DATA)) { 827c478bd9Sstevel@tonic-gate mp = mp->b_cont; 837c478bd9Sstevel@tonic-gate } else { 847c478bd9Sstevel@tonic-gate tswtcl0dbg(("tswtcl_process: no data\n")); 85*1a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&tswtcl_data->epackets); 867c478bd9Sstevel@tonic-gate return (EINVAL); 877c478bd9Sstevel@tonic-gate } 887c478bd9Sstevel@tonic-gate } 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate /* Figure out the ToS/Traffic Class and length from the message */ 917c478bd9Sstevel@tonic-gate if ((mp->b_wptr - mp->b_rptr) < IP_SIMPLE_HDR_LENGTH) { 927c478bd9Sstevel@tonic-gate if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { 937c478bd9Sstevel@tonic-gate tswtcl0dbg(("tswtcl_process: pullup error\n")); 94*1a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&tswtcl_data->epackets); 957c478bd9Sstevel@tonic-gate return (EINVAL); 967c478bd9Sstevel@tonic-gate } 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate ipha = (ipha_t *)mp->b_rptr; 997c478bd9Sstevel@tonic-gate if (IPH_HDR_VERSION(ipha) == IPV4_VERSION) { 1007c478bd9Sstevel@tonic-gate pkt_len = ntohs(ipha->ipha_length); 1017c478bd9Sstevel@tonic-gate } else { 1027c478bd9Sstevel@tonic-gate ip6_hdr = (ip6_t *)mp->b_rptr; 1037c478bd9Sstevel@tonic-gate pkt_len = ntohs(ip6_hdr->ip6_plen) + 1047c478bd9Sstevel@tonic-gate ip_hdr_length_v6(mp, ip6_hdr); 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate /* Convert into bits */ 1087c478bd9Sstevel@tonic-gate pkt_len <<= 3; 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate /* Get current time */ 1117c478bd9Sstevel@tonic-gate now = gethrtime(); 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate /* Update the avg_rate and win_front tswtcl_data */ 1147c478bd9Sstevel@tonic-gate mutex_enter(&tswtcl_data->tswtcl_lock); 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate /* avg_rate = bits/sec and window in msec */ 1177c478bd9Sstevel@tonic-gate bitsinwin = ((uint64_t)tswtcl_data->avg_rate * cfg_parms->window / 1187c478bd9Sstevel@tonic-gate 1000) + pkt_len; 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate deltaT = now - tswtcl_data->win_front + cfg_parms->nsecwindow; 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate tswtcl_data->avg_rate = (uint64_t)bitsinwin * METER_SEC_TO_NSEC / 1237c478bd9Sstevel@tonic-gate deltaT; 1247c478bd9Sstevel@tonic-gate tswtcl_data->win_front = now; 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate if (tswtcl_data->avg_rate <= cfg_parms->committed_rate) { 1277c478bd9Sstevel@tonic-gate *next_action = cfg_parms->green_action; 1287c478bd9Sstevel@tonic-gate } else if (tswtcl_data->avg_rate <= cfg_parms->peak_rate) { 1297c478bd9Sstevel@tonic-gate /* 1307c478bd9Sstevel@tonic-gate * Compute the probability: 1317c478bd9Sstevel@tonic-gate * 1327c478bd9Sstevel@tonic-gate * p0 = (avg_rate - committed_rate) / avg_rate 1337c478bd9Sstevel@tonic-gate * 1347c478bd9Sstevel@tonic-gate * Yellow with probability p0 1357c478bd9Sstevel@tonic-gate * Green with probability (1 - p0) 1367c478bd9Sstevel@tonic-gate * 1377c478bd9Sstevel@tonic-gate */ 1387c478bd9Sstevel@tonic-gate uint32_t aminusc; 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate /* Get a random no. betweeen 0 and avg_rate */ 1417c478bd9Sstevel@tonic-gate (void) random_get_pseudo_bytes((uint8_t *)&additive, 1427c478bd9Sstevel@tonic-gate sizeof (additive)); 1437c478bd9Sstevel@tonic-gate rnd = min + (additive % (tswtcl_data->avg_rate - min + 1)); 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate aminusc = tswtcl_data->avg_rate - cfg_parms->committed_rate; 1467c478bd9Sstevel@tonic-gate if (aminusc >= rnd) { 1477c478bd9Sstevel@tonic-gate *next_action = cfg_parms->yellow_action; 1487c478bd9Sstevel@tonic-gate } else { 1497c478bd9Sstevel@tonic-gate *next_action = cfg_parms->green_action; 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate } else { 1527c478bd9Sstevel@tonic-gate /* 1537c478bd9Sstevel@tonic-gate * Compute the probability: 1547c478bd9Sstevel@tonic-gate * 1557c478bd9Sstevel@tonic-gate * p1 = (avg_rate - peak_rate) / avg_rate 1567c478bd9Sstevel@tonic-gate * p2 = (peak_rate - committed_rate) / avg_rate 1577c478bd9Sstevel@tonic-gate * 1587c478bd9Sstevel@tonic-gate * Red with probability p1 1597c478bd9Sstevel@tonic-gate * Yellow with probability p2 1607c478bd9Sstevel@tonic-gate * Green with probability (1 - (p1 + p2)) 1617c478bd9Sstevel@tonic-gate * 1627c478bd9Sstevel@tonic-gate */ 1637c478bd9Sstevel@tonic-gate uint32_t aminusp; 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate /* Get a random no. betweeen 0 and avg_rate */ 1667c478bd9Sstevel@tonic-gate (void) random_get_pseudo_bytes((uint8_t *)&additive, 1677c478bd9Sstevel@tonic-gate sizeof (additive)); 1687c478bd9Sstevel@tonic-gate rnd = min + (additive % (tswtcl_data->avg_rate - min + 1)); 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate aminusp = tswtcl_data->avg_rate - cfg_parms->peak_rate; 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate if (aminusp >= rnd) { 1737c478bd9Sstevel@tonic-gate *next_action = cfg_parms->red_action; 1747c478bd9Sstevel@tonic-gate } else if ((cfg_parms->pminusc + aminusp) >= rnd) { 1757c478bd9Sstevel@tonic-gate *next_action = cfg_parms->yellow_action; 1767c478bd9Sstevel@tonic-gate } else { 1777c478bd9Sstevel@tonic-gate *next_action = cfg_parms->green_action; 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate mutex_exit(&tswtcl_data->tswtcl_lock); 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate /* Update Stats */ 1847c478bd9Sstevel@tonic-gate if (*next_action == cfg_parms->green_action) { 185*1a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&tswtcl_data->green_packets); 1867c478bd9Sstevel@tonic-gate atomic_add_64(&tswtcl_data->green_bits, pkt_len); 1877c478bd9Sstevel@tonic-gate } else if (*next_action == cfg_parms->yellow_action) { 188*1a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&tswtcl_data->yellow_packets); 1897c478bd9Sstevel@tonic-gate atomic_add_64(&tswtcl_data->yellow_bits, pkt_len); 1907c478bd9Sstevel@tonic-gate } else { 1917c478bd9Sstevel@tonic-gate ASSERT(*next_action == cfg_parms->red_action); 192*1a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&tswtcl_data->red_packets); 1937c478bd9Sstevel@tonic-gate atomic_add_64(&tswtcl_data->red_bits, pkt_len); 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate return (0); 1967c478bd9Sstevel@tonic-gate } 197