Print this page
5045 use atomic_{inc,dec}_* instead of atomic_add_*


   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 2005 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/random.h>
  32 #include <netinet/in.h>
  33 #include <netinet/in_systm.h>
  34 #include <netinet/ip6.h>
  35 #include <inet/common.h>
  36 #include <inet/ip.h>
  37 #include <inet/ip6.h>
  38 #include <ipp/meters/meter_impl.h>
  39 
  40 /*
  41  * Module : Time Sliding Window meter - tswtclmtr
  42  * Description
  43  * This module implements the metering part of RFC 2859. It accepts the
  44  * committed rate, peak rate and the window for a flow and determines
  45  * if the flow is within the committed/peak rate and assigns the appropriate
  46  * next action.
  47  * The meter provides an estimate of the running average bandwidth for the
  48  * flow over the specified window. It uses probability to benefit TCP flows


  57  * ToS or DSCP for IPv4 and IPv6 resp. with the values configured for
  58  * the tswtcl_data.
  59  */
  60 /* ARGSUSED */
  61 int
  62 tswtcl_process(mblk_t **mpp, tswtcl_data_t *tswtcl_data,
  63     ipp_action_id_t *next_action)
  64 {
  65         ipha_t *ipha;
  66         hrtime_t now;
  67         ip6_t *ip6_hdr;
  68         uint32_t pkt_len;
  69         mblk_t *mp = *mpp;
  70         hrtime_t deltaT;
  71         uint64_t bitsinwin;
  72         uint32_t min = 0, additive, rnd;
  73         tswtcl_cfg_t *cfg_parms = tswtcl_data->cfg_parms;
  74 
  75         if (mp == NULL) {
  76                 tswtcl0dbg(("tswtcl_process: null mp!\n"));
  77                 atomic_add_64(&tswtcl_data->epackets, 1);
  78                 return (EINVAL);
  79         }
  80 
  81         if (mp->b_datap->db_type != M_DATA) {
  82                 if ((mp->b_cont != NULL) &&
  83                     (mp->b_cont->b_datap->db_type == M_DATA)) {
  84                         mp = mp->b_cont;
  85                 } else {
  86                         tswtcl0dbg(("tswtcl_process: no data\n"));
  87                         atomic_add_64(&tswtcl_data->epackets, 1);
  88                         return (EINVAL);
  89                 }
  90         }
  91 
  92         /* Figure out the ToS/Traffic Class and length from the message */
  93         if ((mp->b_wptr - mp->b_rptr) < IP_SIMPLE_HDR_LENGTH) {
  94                 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
  95                         tswtcl0dbg(("tswtcl_process: pullup error\n"));
  96                         atomic_add_64(&tswtcl_data->epackets, 1);
  97                         return (EINVAL);
  98                 }
  99         }
 100         ipha = (ipha_t *)mp->b_rptr;
 101         if (IPH_HDR_VERSION(ipha) == IPV4_VERSION) {
 102                 pkt_len = ntohs(ipha->ipha_length);
 103         } else {
 104                 ip6_hdr = (ip6_t *)mp->b_rptr;
 105                 pkt_len = ntohs(ip6_hdr->ip6_plen) +
 106                     ip_hdr_length_v6(mp, ip6_hdr);
 107         }
 108 
 109         /* Convert into bits */
 110         pkt_len <<= 3;
 111 
 112         /* Get current time */
 113         now = gethrtime();
 114 
 115         /* Update the avg_rate and win_front tswtcl_data */
 116         mutex_enter(&tswtcl_data->tswtcl_lock);


 167                 /* Get a random no. betweeen 0 and avg_rate */
 168                 (void) random_get_pseudo_bytes((uint8_t *)&additive,
 169                     sizeof (additive));
 170                 rnd = min + (additive % (tswtcl_data->avg_rate - min + 1));
 171 
 172                 aminusp = tswtcl_data->avg_rate - cfg_parms->peak_rate;
 173 
 174                 if (aminusp >= rnd) {
 175                         *next_action = cfg_parms->red_action;
 176                 } else if ((cfg_parms->pminusc + aminusp) >= rnd) {
 177                         *next_action = cfg_parms->yellow_action;
 178                 } else {
 179                         *next_action = cfg_parms->green_action;
 180                 }
 181 
 182         }
 183         mutex_exit(&tswtcl_data->tswtcl_lock);
 184 
 185         /* Update Stats */
 186         if (*next_action == cfg_parms->green_action) {
 187                 atomic_add_64(&tswtcl_data->green_packets, 1);
 188                 atomic_add_64(&tswtcl_data->green_bits, pkt_len);
 189         } else if (*next_action == cfg_parms->yellow_action) {
 190                 atomic_add_64(&tswtcl_data->yellow_packets, 1);
 191                 atomic_add_64(&tswtcl_data->yellow_bits, pkt_len);
 192         } else {
 193                 ASSERT(*next_action == cfg_parms->red_action);
 194                 atomic_add_64(&tswtcl_data->red_packets, 1);
 195                 atomic_add_64(&tswtcl_data->red_bits, pkt_len);
 196         }
 197         return (0);
 198 }


   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 2005 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/random.h>
  30 #include <netinet/in.h>
  31 #include <netinet/in_systm.h>
  32 #include <netinet/ip6.h>
  33 #include <inet/common.h>
  34 #include <inet/ip.h>
  35 #include <inet/ip6.h>
  36 #include <ipp/meters/meter_impl.h>
  37 
  38 /*
  39  * Module : Time Sliding Window meter - tswtclmtr
  40  * Description
  41  * This module implements the metering part of RFC 2859. It accepts the
  42  * committed rate, peak rate and the window for a flow and determines
  43  * if the flow is within the committed/peak rate and assigns the appropriate
  44  * next action.
  45  * The meter provides an estimate of the running average bandwidth for the
  46  * flow over the specified window. It uses probability to benefit TCP flows


  55  * ToS or DSCP for IPv4 and IPv6 resp. with the values configured for
  56  * the tswtcl_data.
  57  */
  58 /* ARGSUSED */
  59 int
  60 tswtcl_process(mblk_t **mpp, tswtcl_data_t *tswtcl_data,
  61     ipp_action_id_t *next_action)
  62 {
  63         ipha_t *ipha;
  64         hrtime_t now;
  65         ip6_t *ip6_hdr;
  66         uint32_t pkt_len;
  67         mblk_t *mp = *mpp;
  68         hrtime_t deltaT;
  69         uint64_t bitsinwin;
  70         uint32_t min = 0, additive, rnd;
  71         tswtcl_cfg_t *cfg_parms = tswtcl_data->cfg_parms;
  72 
  73         if (mp == NULL) {
  74                 tswtcl0dbg(("tswtcl_process: null mp!\n"));
  75                 atomic_inc_64(&tswtcl_data->epackets);
  76                 return (EINVAL);
  77         }
  78 
  79         if (mp->b_datap->db_type != M_DATA) {
  80                 if ((mp->b_cont != NULL) &&
  81                     (mp->b_cont->b_datap->db_type == M_DATA)) {
  82                         mp = mp->b_cont;
  83                 } else {
  84                         tswtcl0dbg(("tswtcl_process: no data\n"));
  85                         atomic_inc_64(&tswtcl_data->epackets);
  86                         return (EINVAL);
  87                 }
  88         }
  89 
  90         /* Figure out the ToS/Traffic Class and length from the message */
  91         if ((mp->b_wptr - mp->b_rptr) < IP_SIMPLE_HDR_LENGTH) {
  92                 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
  93                         tswtcl0dbg(("tswtcl_process: pullup error\n"));
  94                         atomic_inc_64(&tswtcl_data->epackets);
  95                         return (EINVAL);
  96                 }
  97         }
  98         ipha = (ipha_t *)mp->b_rptr;
  99         if (IPH_HDR_VERSION(ipha) == IPV4_VERSION) {
 100                 pkt_len = ntohs(ipha->ipha_length);
 101         } else {
 102                 ip6_hdr = (ip6_t *)mp->b_rptr;
 103                 pkt_len = ntohs(ip6_hdr->ip6_plen) +
 104                     ip_hdr_length_v6(mp, ip6_hdr);
 105         }
 106 
 107         /* Convert into bits */
 108         pkt_len <<= 3;
 109 
 110         /* Get current time */
 111         now = gethrtime();
 112 
 113         /* Update the avg_rate and win_front tswtcl_data */
 114         mutex_enter(&tswtcl_data->tswtcl_lock);


 165                 /* Get a random no. betweeen 0 and avg_rate */
 166                 (void) random_get_pseudo_bytes((uint8_t *)&additive,
 167                     sizeof (additive));
 168                 rnd = min + (additive % (tswtcl_data->avg_rate - min + 1));
 169 
 170                 aminusp = tswtcl_data->avg_rate - cfg_parms->peak_rate;
 171 
 172                 if (aminusp >= rnd) {
 173                         *next_action = cfg_parms->red_action;
 174                 } else if ((cfg_parms->pminusc + aminusp) >= rnd) {
 175                         *next_action = cfg_parms->yellow_action;
 176                 } else {
 177                         *next_action = cfg_parms->green_action;
 178                 }
 179 
 180         }
 181         mutex_exit(&tswtcl_data->tswtcl_lock);
 182 
 183         /* Update Stats */
 184         if (*next_action == cfg_parms->green_action) {
 185                 atomic_inc_64(&tswtcl_data->green_packets);
 186                 atomic_add_64(&tswtcl_data->green_bits, pkt_len);
 187         } else if (*next_action == cfg_parms->yellow_action) {
 188                 atomic_inc_64(&tswtcl_data->yellow_packets);
 189                 atomic_add_64(&tswtcl_data->yellow_bits, pkt_len);
 190         } else {
 191                 ASSERT(*next_action == cfg_parms->red_action);
 192                 atomic_inc_64(&tswtcl_data->red_packets);
 193                 atomic_add_64(&tswtcl_data->red_bits, pkt_len);
 194         }
 195         return (0);
 196 }