1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2008-2010 Lawrence Stewart <lstewart@freebsd.org> 5 * Copyright (c) 2010 The FreeBSD Foundation 6 * All rights reserved. 7 * 8 * This software was developed by Lawrence Stewart while studying at the Centre 9 * for Advanced Internet Architectures, Swinburne University of Technology, made 10 * possible in part by a grant from the Cisco University Research Program Fund 11 * at Community Foundation Silicon Valley. 12 * 13 * Portions of this software were developed at the Centre for Advanced 14 * Internet Architectures, Swinburne University of Technology, Melbourne, 15 * Australia by David Hayes under sponsorship from the FreeBSD Foundation. 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions 19 * are met: 20 * 1. Redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer. 22 * 2. Redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in the 24 * documentation and/or other materials provided with the distribution. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #ifndef _NETINET_CC_CUBIC_H_ 40 #define _NETINET_CC_CUBIC_H_ 41 42 #include <sys/limits.h> 43 44 /* Number of bits of precision for fixed point math calcs. */ 45 #define CUBIC_SHIFT 8 46 47 #define CUBIC_SHIFT_4 32 48 49 /* 0.5 << CUBIC_SHIFT. */ 50 #define RENO_BETA 128 51 52 /* ~0.7 << CUBIC_SHIFT. */ 53 #define CUBIC_BETA 179 54 55 /* ~0.3 << CUBIC_SHIFT. */ 56 #define ONE_SUB_CUBIC_BETA 77 57 58 /* 3 * ONE_SUB_CUBIC_BETA. */ 59 #define THREE_X_PT3 231 60 61 /* (2 << CUBIC_SHIFT) - ONE_SUB_CUBIC_BETA. */ 62 #define TWO_SUB_PT3 435 63 64 /* ~0.4 << CUBIC_SHIFT. */ 65 #define CUBIC_C_FACTOR 102 66 67 /* CUBIC fast convergence factor: (1+beta_cubic)/2. */ 68 #define CUBIC_FC_FACTOR 217 69 70 /* Don't trust s_rtt until this many rtt samples have been taken. */ 71 #define CUBIC_MIN_RTT_SAMPLES 8 72 73 /* 74 * (2^21)^3 is long max. Dividing (2^63) by Cubic_C_factor 75 * and taking cube-root yields 448845 as the effective useful limit 76 */ 77 #define CUBED_ROOT_MAX_ULONG 448845 78 79 /* Flags used in the cubic structure */ 80 #define CUBICFLAG_CONG_EVENT 0x00000001 /* congestion experienced */ 81 #define CUBICFLAG_IN_SLOWSTART 0x00000002 /* in slow start */ 82 #define CUBICFLAG_IN_APPLIMIT 0x00000004 /* application limited */ 83 #define CUBICFLAG_RTO_EVENT 0x00000008 /* RTO experienced */ 84 #define CUBICFLAG_HYSTART_ENABLED 0x00000010 /* Hystart++ is enabled */ 85 #define CUBICFLAG_HYSTART_IN_CSS 0x00000020 /* We are in Hystart++ CSS */ 86 #define CUBICFLAG_IN_TF 0x00000040 /* We are in TCP friendly region */ 87 88 /* Kernel only bits */ 89 #ifdef _KERNEL 90 struct cubic { 91 /* CUBIC K in fixed point form with CUBIC_SHIFT worth of precision. */ 92 int64_t K; 93 /* Sum of RTT samples across an epoch in usecs. */ 94 int64_t sum_rtt_usecs; 95 /* Size of cwnd just before cwnd was reduced in the last congestion event */ 96 uint64_t W_max; 97 /* An estimate for the congestion window in the Reno-friendly region */ 98 uint64_t W_est; 99 /* The cwnd at the beginning of the current congestion avoidance stage */ 100 uint64_t cwnd_epoch; 101 /* 102 * Size of cwnd at the time of setting ssthresh most recently, 103 * either upon exiting the first slow start, or just before cwnd 104 * was reduced in the last congestion event 105 */ 106 uint64_t cwnd_prior; 107 /* various flags */ 108 uint32_t flags; 109 /* Minimum observed rtt in usecs. */ 110 int min_rtt_usecs; 111 /* Mean observed rtt between congestion epochs. */ 112 int mean_rtt_usecs; 113 /* ACKs since last congestion event. */ 114 int epoch_ack_count; 115 /* Timestamp (in ticks) at which the current CA epoch started. */ 116 int t_epoch; 117 /* Timestamp (in ticks) at which the previous CA epoch started. */ 118 int undo_t_epoch; 119 /* Few variables to restore the state after RTO_ERR */ 120 int64_t undo_K; 121 uint64_t undo_cwnd_prior; 122 uint64_t undo_W_max; 123 uint64_t undo_W_est; 124 uint64_t undo_cwnd_epoch; 125 uint32_t css_baseline_minrtt; 126 uint32_t css_current_round_minrtt; 127 uint32_t css_lastround_minrtt; 128 uint32_t css_rttsample_count; 129 uint32_t css_entered_at_round; 130 uint32_t css_current_round; 131 uint32_t css_fas_at_css_entry; 132 uint32_t css_lowrtt_fas; 133 uint32_t css_last_fas; 134 }; 135 #endif 136 137 /* Userland only bits. */ 138 #ifndef _KERNEL 139 140 extern int hz; 141 142 /* 143 * Implementation based on the formulae found in the CUBIC Internet Draft 144 * "draft-ietf-tcpm-cubic-04". 145 * 146 */ 147 148 static __inline float 149 theoretical_cubic_k(double wmax_pkts) 150 { 151 double C; 152 153 C = 0.4; 154 155 return (pow((wmax_pkts * 0.3) / C, (1.0 / 3.0)) * pow(2, CUBIC_SHIFT)); 156 } 157 158 static __inline unsigned long 159 theoretical_cubic_cwnd(int ticks_since_epoch, unsigned long wmax, uint32_t smss) 160 { 161 double C, wmax_pkts; 162 163 C = 0.4; 164 wmax_pkts = wmax / (double)smss; 165 166 return (smss * (wmax_pkts + 167 (C * pow(ticks_since_epoch / (double)hz - 168 theoretical_cubic_k(wmax_pkts) / pow(2, CUBIC_SHIFT), 3.0)))); 169 } 170 171 static __inline unsigned long 172 theoretical_reno_cwnd(int ticks_since_epoch, int rtt_ticks, unsigned long wmax, 173 uint32_t smss) 174 { 175 176 return ((wmax * 0.5) + ((ticks_since_epoch / (float)rtt_ticks) * smss)); 177 } 178 179 static __inline unsigned long 180 theoretical_tf_cwnd(int ticks_since_epoch, int rtt_ticks, unsigned long wmax, 181 uint32_t smss) 182 { 183 184 return ((wmax * 0.7) + ((3 * 0.3) / (2 - 0.3) * 185 (ticks_since_epoch / (float)rtt_ticks) * smss)); 186 } 187 188 #endif /* !_KERNEL */ 189 190 /* 191 * Compute the CUBIC K value used in the cwnd calculation, using an 192 * implementation of eqn 2 in the I-D. The method used 193 * here is adapted from Apple Computer Technical Report #KT-32. 194 */ 195 static __inline int64_t 196 cubic_k(unsigned long wmax_pkts) 197 { 198 int64_t s, K; 199 uint16_t p; 200 201 K = s = 0; 202 p = 0; 203 204 /* (wmax * beta)/C with CUBIC_SHIFT worth of precision. */ 205 s = ((wmax_pkts * ONE_SUB_CUBIC_BETA) << CUBIC_SHIFT) / CUBIC_C_FACTOR; 206 207 /* Rebase s to be between 1 and 1/8 with a shift of CUBIC_SHIFT. */ 208 while (s >= 256) { 209 s >>= 3; 210 p++; 211 } 212 213 /* 214 * Some magic constants taken from the Apple TR with appropriate 215 * shifts: 275 == 1.072302 << CUBIC_SHIFT, 98 == 0.3812513 << 216 * CUBIC_SHIFT, 120 == 0.46946116 << CUBIC_SHIFT. 217 */ 218 K = (((s * 275) >> CUBIC_SHIFT) + 98) - 219 (((s * s * 120) >> CUBIC_SHIFT) >> CUBIC_SHIFT); 220 221 /* Multiply by 2^p to undo the rebasing of s from above. */ 222 return (K <<= p); 223 } 224 225 /* 226 * Compute the new cwnd value using an implementation of eqn 1 from the I-D. 227 * Thanks to Kip Macy for help debugging this function. 228 * 229 * XXXLAS: Characterise bounds for overflow. 230 */ 231 static __inline unsigned long 232 cubic_cwnd(int usecs_since_epoch, unsigned long wmax, uint32_t smss, int64_t K) 233 { 234 int64_t cwnd; 235 236 /* K is in fixed point form with CUBIC_SHIFT worth of precision. */ 237 238 /* t - K, with CUBIC_SHIFT worth of precision. */ 239 cwnd = (((int64_t)usecs_since_epoch << CUBIC_SHIFT) - (K * hz * tick)) / 240 (hz * tick); 241 242 if (cwnd > CUBED_ROOT_MAX_ULONG) 243 return INT_MAX; 244 if (cwnd < -CUBED_ROOT_MAX_ULONG) 245 return 0; 246 247 /* (t - K)^3, with CUBIC_SHIFT^3 worth of precision. */ 248 cwnd *= (cwnd * cwnd); 249 250 /* 251 * C(t - K)^3 + wmax 252 * The down shift by CUBIC_SHIFT_4 is because cwnd has 4 lots of 253 * CUBIC_SHIFT included in the value. 3 from the cubing of cwnd above, 254 * and an extra from multiplying through by CUBIC_C_FACTOR. 255 */ 256 257 cwnd = ((cwnd * CUBIC_C_FACTOR) >> CUBIC_SHIFT_4) * smss + wmax; 258 259 /* 260 * for negative cwnd, limiting to zero as lower bound 261 */ 262 return (lmax(0,cwnd)); 263 } 264 265 /* 266 * Compute an approximation of the NewReno cwnd some number of usecs after a 267 * congestion event. RTT should be the average RTT estimate for the path 268 * measured over the previous congestion epoch and wmax is the value of cwnd at 269 * the last congestion event. The "TCP friendly" concept in the CUBIC I-D is 270 * rather tricky to understand and it turns out this function is not required. 271 * It is left here for reference. 272 * 273 * XXX: Not used 274 */ 275 static __inline unsigned long 276 reno_cwnd(int usecs_since_epoch, int rtt_usecs, unsigned long wmax, 277 uint32_t smss) 278 { 279 280 /* 281 * For NewReno, beta = 0.5, therefore: W_tcp(t) = wmax*0.5 + t/RTT 282 * W_tcp(t) deals with cwnd/wmax in pkts, so because our cwnd is in 283 * bytes, we have to multiply by smss. 284 */ 285 return (((wmax * RENO_BETA) + (((usecs_since_epoch * smss) 286 << CUBIC_SHIFT) / rtt_usecs)) >> CUBIC_SHIFT); 287 } 288 289 /* 290 * Compute the "TCP friendly" cwnd by newreno in congestion avoidance state. 291 */ 292 static __inline unsigned long 293 tf_cwnd(struct cc_var *ccv) 294 { 295 /* newreno is "TCP friendly" */ 296 return newreno_cc_cwnd_in_cong_avoid(ccv); 297 } 298 299 #endif /* _NETINET_CC_CUBIC_H_ */ 300