1 /*
   2  * Copyright (c) 2008-2010 Lawrence Stewart <lstewart@freebsd.org>
   3  * Copyright (c) 2010 The FreeBSD Foundation
   4  * All rights reserved.
   5  * Copyright (c) 2017 by Delphix. All rights reserved.
   6  *
   7  * This software was developed by Lawrence Stewart while studying at the Centre
   8  * for Advanced Internet Architectures, Swinburne University of Technology, made
   9  * possible in part by a grant from the Cisco University Research Program Fund
  10  * at Community Foundation Silicon Valley.
  11  *
  12  * Portions of this software were developed at the Centre for Advanced
  13  * Internet Architectures, Swinburne University of Technology, Melbourne,
  14  * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
  15  *
  16  * Redistribution and use in source and binary forms, with or without
  17  * modification, are permitted provided that the following conditions
  18  * are met:
  19  * 1. Redistributions of source code must retain the above copyright
  20  *    notice, this list of conditions and the following disclaimer.
  21  * 2. Redistributions in binary form must reproduce the above copyright
  22  *    notice, this list of conditions and the following disclaimer in the
  23  *    documentation and/or other materials provided with the distribution.
  24  *
  25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35  * SUCH DAMAGE.
  36  *
  37  * $FreeBSD$
  38  */
  39 
  40 #ifndef _NETINET_CC_CUBIC_H_
  41 #define _NETINET_CC_CUBIC_H_
  42 
  43 /* Number of bits of precision for fixed point math calcs. */
  44 #define CUBIC_SHIFT             8
  45 
  46 #define CUBIC_SHIFT_4           32
  47 
  48 /* 0.5 << CUBIC_SHIFT. */
  49 #define RENO_BETA               128
  50 
  51 /* ~0.8 << CUBIC_SHIFT. */
  52 #define CUBIC_BETA              204
  53 
  54 /* ~0.2 << CUBIC_SHIFT. */
  55 #define ONE_SUB_CUBIC_BETA      51
  56 
  57 /* 3 * ONE_SUB_CUBIC_BETA. */
  58 #define THREE_X_PT2             153
  59 
  60 /* (2 << CUBIC_SHIFT) - ONE_SUB_CUBIC_BETA. */
  61 #define TWO_SUB_PT2             461
  62 
  63 /* ~0.4 << CUBIC_SHIFT. */
  64 #define CUBIC_C_FACTOR          102
  65 
  66 /* CUBIC fast convergence factor: ~0.9 << CUBIC_SHIFT. */
  67 #define CUBIC_FC_FACTOR         230
  68 
  69 /* Don't trust s_rtt until this many rtt samples have been taken. */
  70 #define CUBIC_MIN_RTT_SAMPLES   8
  71 
  72 /* Userland only bits. */
  73 #ifndef _KERNEL
  74 
  75 extern int hz;
  76 
  77 /*
  78  * Implementation based on the formulae found in the CUBIC Internet Draft
  79  * "draft-rhee-tcpm-cubic-02".
  80  *
  81  * Note BETA used in cc_cubic is equal to (1-beta) in the I-D
  82  */
  83 
  84 static __inline float
  85 theoretical_cubic_k(double wmax_pkts)
  86 {
  87         double C;
  88 
  89         C = 0.4;
  90 
  91         return (pow((wmax_pkts * 0.2) / C, (1.0 / 3.0)) * pow(2, CUBIC_SHIFT));
  92 }
  93 
  94 static __inline uint32_t
  95 theoretical_cubic_cwnd(int ticks_since_cong, uint32_t wmax, uint32_t smss)
  96 {
  97         double C, wmax_pkts;
  98 
  99         C = 0.4;
 100         wmax_pkts = wmax / (double)smss;
 101 
 102         return (smss * (wmax_pkts +
 103             (C * pow(ticks_since_cong / (double)hz -
 104             theoretical_cubic_k(wmax_pkts) / pow(2, CUBIC_SHIFT), 3.0))));
 105 }
 106 
 107 static __inline uint32_t
 108 theoretical_reno_cwnd(int ticks_since_cong, int rtt_ticks, uint32_t wmax,
 109     uint32_t smss)
 110 {
 111 
 112         return ((wmax * 0.5) + ((ticks_since_cong / (float)rtt_ticks) * smss));
 113 }
 114 
 115 static __inline uint32_t
 116 theoretical_tf_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
 117     uint32_t smss)
 118 {
 119 
 120         return ((wmax * 0.8) + ((3 * 0.2) / (2 - 0.2) *
 121             (ticks_since_cong / (float)rtt_ticks) * smss));
 122 }
 123 
 124 #endif /* !_KERNEL */
 125 
 126 /*
 127  * Compute the CUBIC K value used in the cwnd calculation, using an
 128  * implementation of eqn 2 in the I-D. The method used
 129  * here is adapted from Apple Computer Technical Report #KT-32.
 130  */
 131 static __inline int64_t
 132 cubic_k(uint32_t wmax_pkts)
 133 {
 134         int64_t s, K;
 135         uint16_t p;
 136 
 137         K = s = 0;
 138         p = 0;
 139 
 140         /* (wmax * beta)/C with CUBIC_SHIFT worth of precision. */
 141         s = ((wmax_pkts * ONE_SUB_CUBIC_BETA) << CUBIC_SHIFT) / CUBIC_C_FACTOR;
 142 
 143         /* Rebase s to be between 1 and 1/8 with a shift of CUBIC_SHIFT. */
 144         while (s >= 256) {
 145                 s >>= 3;
 146                 p++;
 147         }
 148 
 149         /*
 150          * Some magic constants taken from the Apple TR with appropriate
 151          * shifts: 275 == 1.072302 << CUBIC_SHIFT, 98 == 0.3812513 <<
 152          * CUBIC_SHIFT, 120 == 0.46946116 << CUBIC_SHIFT.
 153          */
 154         K = (((s * 275) >> CUBIC_SHIFT) + 98) -
 155             (((s * s * 120) >> CUBIC_SHIFT) >> CUBIC_SHIFT);
 156 
 157         /* Multiply by 2^p to undo the rebasing of s from above. */
 158         return (K <<= p);
 159 }
 160 
 161 /*
 162  * Compute the new cwnd value using an implementation of eqn 1 from the I-D.
 163  * Thanks to Kip Macy for help debugging this function.
 164  *
 165  * XXXLAS: Characterise bounds for overflow.
 166  */
 167 static __inline uint32_t
 168 cubic_cwnd(int ticks_since_cong, uint32_t wmax, uint32_t smss, int64_t K)
 169 {
 170         int64_t cwnd;
 171 
 172         /* K is in fixed point form with CUBIC_SHIFT worth of precision. */
 173 
 174         /* t - K, with CUBIC_SHIFT worth of precision. */
 175         cwnd = ((int64_t)(ticks_since_cong << CUBIC_SHIFT) - (K * hz)) / hz;
 176 
 177         /* (t - K)^3, with CUBIC_SHIFT^3 worth of precision. */
 178         cwnd *= (cwnd * cwnd);
 179 
 180         /*
 181          * C(t - K)^3 + wmax
 182          * The down shift by CUBIC_SHIFT_4 is because cwnd has 4 lots of
 183          * CUBIC_SHIFT included in the value. 3 from the cubing of cwnd above,
 184          * and an extra from multiplying through by CUBIC_C_FACTOR.
 185          */
 186         cwnd = ((cwnd * CUBIC_C_FACTOR * smss) >> CUBIC_SHIFT_4) + wmax;
 187 
 188         return ((uint32_t)cwnd);
 189 }
 190 
 191 /*
 192  * Compute an approximation of the "TCP friendly" cwnd some number of ticks
 193  * after a congestion event that is designed to yield the same average cwnd as
 194  * NewReno while using CUBIC's beta of 0.8. RTT should be the average RTT
 195  * estimate for the path measured over the previous congestion epoch and wmax is
 196  * the value of cwnd at the last congestion event.
 197  */
 198 static __inline uint32_t
 199 tf_cwnd(int ticks_since_cong, int rtt_ticks, uint32_t wmax,
 200     uint32_t smss)
 201 {
 202 
 203         /* Equation 4 of I-D. */
 204         return (((wmax * CUBIC_BETA) + (((THREE_X_PT2 * ticks_since_cong *
 205             smss) << CUBIC_SHIFT) / TWO_SUB_PT2 / rtt_ticks)) >> CUBIC_SHIFT);
 206 }
 207 
 208 #endif /* _NETINET_CC_CUBIC_H_ */