167fef78bSLawrence Stewart /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3fe267a55SPedro F. Giffuni * 467fef78bSLawrence Stewart * Copyright (c) 2008-2010 Lawrence Stewart <lstewart@freebsd.org> 567fef78bSLawrence Stewart * Copyright (c) 2010 The FreeBSD Foundation 667fef78bSLawrence Stewart * All rights reserved. 767fef78bSLawrence Stewart * 867fef78bSLawrence Stewart * This software was developed by Lawrence Stewart while studying at the Centre 9891b8ed4SLawrence Stewart * for Advanced Internet Architectures, Swinburne University of Technology, made 10891b8ed4SLawrence Stewart * possible in part by a grant from the Cisco University Research Program Fund 11891b8ed4SLawrence Stewart * at Community Foundation Silicon Valley. 1267fef78bSLawrence Stewart * 1367fef78bSLawrence Stewart * Portions of this software were developed at the Centre for Advanced 1467fef78bSLawrence Stewart * Internet Architectures, Swinburne University of Technology, Melbourne, 1567fef78bSLawrence Stewart * Australia by David Hayes under sponsorship from the FreeBSD Foundation. 1667fef78bSLawrence Stewart * 1767fef78bSLawrence Stewart * Redistribution and use in source and binary forms, with or without 1867fef78bSLawrence Stewart * modification, are permitted provided that the following conditions 1967fef78bSLawrence Stewart * are met: 2067fef78bSLawrence Stewart * 1. Redistributions of source code must retain the above copyright 2167fef78bSLawrence Stewart * notice, this list of conditions and the following disclaimer. 2267fef78bSLawrence Stewart * 2. Redistributions in binary form must reproduce the above copyright 2367fef78bSLawrence Stewart * notice, this list of conditions and the following disclaimer in the 2467fef78bSLawrence Stewart * documentation and/or other materials provided with the distribution. 2567fef78bSLawrence Stewart * 2667fef78bSLawrence Stewart * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2767fef78bSLawrence Stewart * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2867fef78bSLawrence Stewart * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2967fef78bSLawrence Stewart * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 3067fef78bSLawrence Stewart * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3167fef78bSLawrence Stewart * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3267fef78bSLawrence Stewart * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3367fef78bSLawrence Stewart * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3467fef78bSLawrence Stewart * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3567fef78bSLawrence Stewart * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3667fef78bSLawrence Stewart * SUCH DAMAGE. 3767fef78bSLawrence Stewart */ 3867fef78bSLawrence Stewart 3967fef78bSLawrence Stewart #ifndef _NETINET_CC_CUBIC_H_ 4067fef78bSLawrence Stewart #define _NETINET_CC_CUBIC_H_ 4167fef78bSLawrence Stewart 42c968c769SMichael Tuexen #include <sys/limits.h> 43c968c769SMichael Tuexen 4467fef78bSLawrence Stewart /* Number of bits of precision for fixed point math calcs. */ 4567fef78bSLawrence Stewart #define CUBIC_SHIFT 8 4667fef78bSLawrence Stewart 4767fef78bSLawrence Stewart #define CUBIC_SHIFT_4 32 4867fef78bSLawrence Stewart 4967fef78bSLawrence Stewart /* 0.5 << CUBIC_SHIFT. */ 5067fef78bSLawrence Stewart #define RENO_BETA 128 5167fef78bSLawrence Stewart 5268ff29afSSean Bruno /* ~0.7 << CUBIC_SHIFT. */ 5368ff29afSSean Bruno #define CUBIC_BETA 179 5467fef78bSLawrence Stewart 5568ff29afSSean Bruno /* ~0.3 << CUBIC_SHIFT. */ 5668ff29afSSean Bruno #define ONE_SUB_CUBIC_BETA 77 5767fef78bSLawrence Stewart 5867fef78bSLawrence Stewart /* 3 * ONE_SUB_CUBIC_BETA. */ 5968ff29afSSean Bruno #define THREE_X_PT3 231 6067fef78bSLawrence Stewart 6167fef78bSLawrence Stewart /* (2 << CUBIC_SHIFT) - ONE_SUB_CUBIC_BETA. */ 6268ff29afSSean Bruno #define TWO_SUB_PT3 435 6367fef78bSLawrence Stewart 6467fef78bSLawrence Stewart /* ~0.4 << CUBIC_SHIFT. */ 6567fef78bSLawrence Stewart #define CUBIC_C_FACTOR 102 6667fef78bSLawrence Stewart 6768ff29afSSean Bruno /* CUBIC fast convergence factor: (1+beta_cubic)/2. */ 6868ff29afSSean Bruno #define CUBIC_FC_FACTOR 217 6967fef78bSLawrence Stewart 7067fef78bSLawrence Stewart /* Don't trust s_rtt until this many rtt samples have been taken. */ 7167fef78bSLawrence Stewart #define CUBIC_MIN_RTT_SAMPLES 8 7267fef78bSLawrence Stewart 73c968c769SMichael Tuexen /* 74c968c769SMichael Tuexen * (2^21)^3 is long max. Dividing (2^63) by Cubic_C_factor 75c968c769SMichael Tuexen * and taking cube-root yields 448845 as the effective useful limit 76c968c769SMichael Tuexen */ 77c968c769SMichael Tuexen #define CUBED_ROOT_MAX_ULONG 448845 78c968c769SMichael Tuexen 79a9696510SRandall Stewart /* Flags used in the cubic structure */ 80a9696510SRandall Stewart #define CUBICFLAG_CONG_EVENT 0x00000001 /* congestion experienced */ 81a9696510SRandall Stewart #define CUBICFLAG_IN_SLOWSTART 0x00000002 /* in slow start */ 82a9696510SRandall Stewart #define CUBICFLAG_IN_APPLIMIT 0x00000004 /* application limited */ 83a9696510SRandall Stewart #define CUBICFLAG_RTO_EVENT 0x00000008 /* RTO experienced */ 84a9696510SRandall Stewart #define CUBICFLAG_HYSTART_ENABLED 0x00000010 /* Hystart++ is enabled */ 85a9696510SRandall Stewart #define CUBICFLAG_HYSTART_IN_CSS 0x00000020 /* We are in Hystart++ CSS */ 86*ee450610SCheng Cui #define CUBICFLAG_IN_TF 0x00000040 /* We are in TCP friendly region */ 87a9696510SRandall Stewart 88a9696510SRandall Stewart /* Kernel only bits */ 89a9696510SRandall Stewart #ifdef _KERNEL 90a9696510SRandall Stewart struct cubic { 91ea6d0de2SRichard Scheffenegger /* CUBIC K in fixed point form with CUBIC_SHIFT worth of precision. */ 92a9696510SRandall Stewart int64_t K; 93a3aa6f65SCheng Cui /* Sum of RTT samples across an epoch in usecs. */ 94a3aa6f65SCheng Cui int64_t sum_rtt_usecs; 95eb5bfdd0SRichard Scheffenegger /* Size of cwnd just before cwnd was reduced in the last congestion event */ 96eb5bfdd0SRichard Scheffenegger uint64_t W_max; 97eb5bfdd0SRichard Scheffenegger /* An estimate for the congestion window in the Reno-friendly region */ 98eb5bfdd0SRichard Scheffenegger uint64_t W_est; 99eb5bfdd0SRichard Scheffenegger /* The cwnd at the beginning of the current congestion avoidance stage */ 100eb5bfdd0SRichard Scheffenegger uint64_t cwnd_epoch; 101eb5bfdd0SRichard Scheffenegger /* 102eb5bfdd0SRichard Scheffenegger * Size of cwnd at the time of setting ssthresh most recently, 103eb5bfdd0SRichard Scheffenegger * either upon exiting the first slow start, or just before cwnd 104eb5bfdd0SRichard Scheffenegger * was reduced in the last congestion event 105eb5bfdd0SRichard Scheffenegger */ 106eb5bfdd0SRichard Scheffenegger uint64_t cwnd_prior; 107a9696510SRandall Stewart /* various flags */ 108a9696510SRandall Stewart uint32_t flags; 109a3aa6f65SCheng Cui /* Minimum observed rtt in usecs. */ 110a3aa6f65SCheng Cui int min_rtt_usecs; 111a9696510SRandall Stewart /* Mean observed rtt between congestion epochs. */ 112a3aa6f65SCheng Cui int mean_rtt_usecs; 113a9696510SRandall Stewart /* ACKs since last congestion event. */ 114a9696510SRandall Stewart int epoch_ack_count; 115eb5bfdd0SRichard Scheffenegger /* Timestamp (in ticks) at which the current CA epoch started. */ 116eb5bfdd0SRichard Scheffenegger int t_epoch; 117eb5bfdd0SRichard Scheffenegger /* Timestamp (in ticks) at which the previous CA epoch started. */ 118eb5bfdd0SRichard Scheffenegger int undo_t_epoch; 119eb5bfdd0SRichard Scheffenegger /* Few variables to restore the state after RTO_ERR */ 120eb5bfdd0SRichard Scheffenegger int64_t undo_K; 121eb5bfdd0SRichard Scheffenegger uint64_t undo_cwnd_prior; 122eb5bfdd0SRichard Scheffenegger uint64_t undo_W_max; 123eb5bfdd0SRichard Scheffenegger uint64_t undo_W_est; 124eb5bfdd0SRichard Scheffenegger uint64_t undo_cwnd_epoch; 125a9696510SRandall Stewart uint32_t css_baseline_minrtt; 126a9696510SRandall Stewart uint32_t css_current_round_minrtt; 127a9696510SRandall Stewart uint32_t css_lastround_minrtt; 128a9696510SRandall Stewart uint32_t css_rttsample_count; 129a9696510SRandall Stewart uint32_t css_entered_at_round; 130a9696510SRandall Stewart uint32_t css_current_round; 131a9696510SRandall Stewart uint32_t css_fas_at_css_entry; 132a9696510SRandall Stewart uint32_t css_lowrtt_fas; 133a9696510SRandall Stewart uint32_t css_last_fas; 134a9696510SRandall Stewart }; 135a9696510SRandall Stewart #endif 136a9696510SRandall Stewart 13767fef78bSLawrence Stewart /* Userland only bits. */ 13867fef78bSLawrence Stewart #ifndef _KERNEL 13967fef78bSLawrence Stewart 14067fef78bSLawrence Stewart extern int hz; 14167fef78bSLawrence Stewart 14267fef78bSLawrence Stewart /* 14367fef78bSLawrence Stewart * Implementation based on the formulae found in the CUBIC Internet Draft 14468ff29afSSean Bruno * "draft-ietf-tcpm-cubic-04". 14567fef78bSLawrence Stewart * 14667fef78bSLawrence Stewart */ 14767fef78bSLawrence Stewart 14867fef78bSLawrence Stewart static __inline float 14967fef78bSLawrence Stewart theoretical_cubic_k(double wmax_pkts) 15067fef78bSLawrence Stewart { 15167fef78bSLawrence Stewart double C; 15267fef78bSLawrence Stewart 15367fef78bSLawrence Stewart C = 0.4; 15467fef78bSLawrence Stewart 15568ff29afSSean Bruno return (pow((wmax_pkts * 0.3) / C, (1.0 / 3.0)) * pow(2, CUBIC_SHIFT)); 15667fef78bSLawrence Stewart } 15767fef78bSLawrence Stewart 15867fef78bSLawrence Stewart static __inline unsigned long 159eb5bfdd0SRichard Scheffenegger theoretical_cubic_cwnd(int ticks_since_epoch, unsigned long wmax, uint32_t smss) 16067fef78bSLawrence Stewart { 16167fef78bSLawrence Stewart double C, wmax_pkts; 16267fef78bSLawrence Stewart 16367fef78bSLawrence Stewart C = 0.4; 16467fef78bSLawrence Stewart wmax_pkts = wmax / (double)smss; 16567fef78bSLawrence Stewart 16667fef78bSLawrence Stewart return (smss * (wmax_pkts + 167eb5bfdd0SRichard Scheffenegger (C * pow(ticks_since_epoch / (double)hz - 16867fef78bSLawrence Stewart theoretical_cubic_k(wmax_pkts) / pow(2, CUBIC_SHIFT), 3.0)))); 16967fef78bSLawrence Stewart } 17067fef78bSLawrence Stewart 17167fef78bSLawrence Stewart static __inline unsigned long 172eb5bfdd0SRichard Scheffenegger theoretical_reno_cwnd(int ticks_since_epoch, int rtt_ticks, unsigned long wmax, 17367fef78bSLawrence Stewart uint32_t smss) 17467fef78bSLawrence Stewart { 17567fef78bSLawrence Stewart 176eb5bfdd0SRichard Scheffenegger return ((wmax * 0.5) + ((ticks_since_epoch / (float)rtt_ticks) * smss)); 17767fef78bSLawrence Stewart } 17867fef78bSLawrence Stewart 17967fef78bSLawrence Stewart static __inline unsigned long 180eb5bfdd0SRichard Scheffenegger theoretical_tf_cwnd(int ticks_since_epoch, int rtt_ticks, unsigned long wmax, 18167fef78bSLawrence Stewart uint32_t smss) 18267fef78bSLawrence Stewart { 18367fef78bSLawrence Stewart 18468ff29afSSean Bruno return ((wmax * 0.7) + ((3 * 0.3) / (2 - 0.3) * 185eb5bfdd0SRichard Scheffenegger (ticks_since_epoch / (float)rtt_ticks) * smss)); 18667fef78bSLawrence Stewart } 18767fef78bSLawrence Stewart 18867fef78bSLawrence Stewart #endif /* !_KERNEL */ 18967fef78bSLawrence Stewart 19067fef78bSLawrence Stewart /* 19167fef78bSLawrence Stewart * Compute the CUBIC K value used in the cwnd calculation, using an 19267fef78bSLawrence Stewart * implementation of eqn 2 in the I-D. The method used 19367fef78bSLawrence Stewart * here is adapted from Apple Computer Technical Report #KT-32. 19467fef78bSLawrence Stewart */ 19567fef78bSLawrence Stewart static __inline int64_t 19667fef78bSLawrence Stewart cubic_k(unsigned long wmax_pkts) 19767fef78bSLawrence Stewart { 19867fef78bSLawrence Stewart int64_t s, K; 19967fef78bSLawrence Stewart uint16_t p; 20067fef78bSLawrence Stewart 20167fef78bSLawrence Stewart K = s = 0; 20267fef78bSLawrence Stewart p = 0; 20367fef78bSLawrence Stewart 20467fef78bSLawrence Stewart /* (wmax * beta)/C with CUBIC_SHIFT worth of precision. */ 20567fef78bSLawrence Stewart s = ((wmax_pkts * ONE_SUB_CUBIC_BETA) << CUBIC_SHIFT) / CUBIC_C_FACTOR; 20667fef78bSLawrence Stewart 20767fef78bSLawrence Stewart /* Rebase s to be between 1 and 1/8 with a shift of CUBIC_SHIFT. */ 20867fef78bSLawrence Stewart while (s >= 256) { 20967fef78bSLawrence Stewart s >>= 3; 21067fef78bSLawrence Stewart p++; 21167fef78bSLawrence Stewart } 21267fef78bSLawrence Stewart 21367fef78bSLawrence Stewart /* 21467fef78bSLawrence Stewart * Some magic constants taken from the Apple TR with appropriate 21567fef78bSLawrence Stewart * shifts: 275 == 1.072302 << CUBIC_SHIFT, 98 == 0.3812513 << 21667fef78bSLawrence Stewart * CUBIC_SHIFT, 120 == 0.46946116 << CUBIC_SHIFT. 21767fef78bSLawrence Stewart */ 21867fef78bSLawrence Stewart K = (((s * 275) >> CUBIC_SHIFT) + 98) - 21967fef78bSLawrence Stewart (((s * s * 120) >> CUBIC_SHIFT) >> CUBIC_SHIFT); 22067fef78bSLawrence Stewart 22167fef78bSLawrence Stewart /* Multiply by 2^p to undo the rebasing of s from above. */ 22267fef78bSLawrence Stewart return (K <<= p); 22367fef78bSLawrence Stewart } 22467fef78bSLawrence Stewart 22567fef78bSLawrence Stewart /* 22667fef78bSLawrence Stewart * Compute the new cwnd value using an implementation of eqn 1 from the I-D. 22767fef78bSLawrence Stewart * Thanks to Kip Macy for help debugging this function. 22851e712f8SHiren Panchasara * 22951e712f8SHiren Panchasara * XXXLAS: Characterise bounds for overflow. 23067fef78bSLawrence Stewart */ 23167fef78bSLawrence Stewart static __inline unsigned long 232eb5bfdd0SRichard Scheffenegger cubic_cwnd(int usecs_since_epoch, unsigned long wmax, uint32_t smss, int64_t K) 23367fef78bSLawrence Stewart { 23467fef78bSLawrence Stewart int64_t cwnd; 23567fef78bSLawrence Stewart 23667fef78bSLawrence Stewart /* K is in fixed point form with CUBIC_SHIFT worth of precision. */ 23767fef78bSLawrence Stewart 23867fef78bSLawrence Stewart /* t - K, with CUBIC_SHIFT worth of precision. */ 239eb5bfdd0SRichard Scheffenegger cwnd = (((int64_t)usecs_since_epoch << CUBIC_SHIFT) - (K * hz * tick)) / 240a3aa6f65SCheng Cui (hz * tick); 241c968c769SMichael Tuexen 242c968c769SMichael Tuexen if (cwnd > CUBED_ROOT_MAX_ULONG) 243c968c769SMichael Tuexen return INT_MAX; 244c968c769SMichael Tuexen if (cwnd < -CUBED_ROOT_MAX_ULONG) 245c968c769SMichael Tuexen return 0; 24667fef78bSLawrence Stewart 24767fef78bSLawrence Stewart /* (t - K)^3, with CUBIC_SHIFT^3 worth of precision. */ 24867fef78bSLawrence Stewart cwnd *= (cwnd * cwnd); 24967fef78bSLawrence Stewart 25067fef78bSLawrence Stewart /* 25167fef78bSLawrence Stewart * C(t - K)^3 + wmax 25267fef78bSLawrence Stewart * The down shift by CUBIC_SHIFT_4 is because cwnd has 4 lots of 25367fef78bSLawrence Stewart * CUBIC_SHIFT included in the value. 3 from the cubing of cwnd above, 25467fef78bSLawrence Stewart * and an extra from multiplying through by CUBIC_C_FACTOR. 25567fef78bSLawrence Stewart */ 25667fef78bSLawrence Stewart 257c968c769SMichael Tuexen cwnd = ((cwnd * CUBIC_C_FACTOR) >> CUBIC_SHIFT_4) * smss + wmax; 258c968c769SMichael Tuexen 259c968c769SMichael Tuexen /* 260c968c769SMichael Tuexen * for negative cwnd, limiting to zero as lower bound 261c968c769SMichael Tuexen */ 262c968c769SMichael Tuexen return (lmax(0,cwnd)); 26367fef78bSLawrence Stewart } 26467fef78bSLawrence Stewart 26567fef78bSLawrence Stewart /* 266a3aa6f65SCheng Cui * Compute an approximation of the NewReno cwnd some number of usecs after a 26767fef78bSLawrence Stewart * congestion event. RTT should be the average RTT estimate for the path 26867fef78bSLawrence Stewart * measured over the previous congestion epoch and wmax is the value of cwnd at 26967fef78bSLawrence Stewart * the last congestion event. The "TCP friendly" concept in the CUBIC I-D is 27067fef78bSLawrence Stewart * rather tricky to understand and it turns out this function is not required. 27167fef78bSLawrence Stewart * It is left here for reference. 272a3aa6f65SCheng Cui * 273a3aa6f65SCheng Cui * XXX: Not used 27467fef78bSLawrence Stewart */ 27567fef78bSLawrence Stewart static __inline unsigned long 276eb5bfdd0SRichard Scheffenegger reno_cwnd(int usecs_since_epoch, int rtt_usecs, unsigned long wmax, 27767fef78bSLawrence Stewart uint32_t smss) 27867fef78bSLawrence Stewart { 27967fef78bSLawrence Stewart 28067fef78bSLawrence Stewart /* 28167fef78bSLawrence Stewart * For NewReno, beta = 0.5, therefore: W_tcp(t) = wmax*0.5 + t/RTT 28267fef78bSLawrence Stewart * W_tcp(t) deals with cwnd/wmax in pkts, so because our cwnd is in 28367fef78bSLawrence Stewart * bytes, we have to multiply by smss. 28467fef78bSLawrence Stewart */ 285eb5bfdd0SRichard Scheffenegger return (((wmax * RENO_BETA) + (((usecs_since_epoch * smss) 286a3aa6f65SCheng Cui << CUBIC_SHIFT) / rtt_usecs)) >> CUBIC_SHIFT); 28767fef78bSLawrence Stewart } 28867fef78bSLawrence Stewart 28967fef78bSLawrence Stewart /* 290*ee450610SCheng Cui * Compute the "TCP friendly" cwnd by newreno in congestion avoidance state. 29167fef78bSLawrence Stewart */ 29267fef78bSLawrence Stewart static __inline unsigned long 293*ee450610SCheng Cui tf_cwnd(struct cc_var *ccv) 29467fef78bSLawrence Stewart { 295*ee450610SCheng Cui /* newreno is "TCP friendly" */ 296*ee450610SCheng Cui return newreno_cc_cwnd_in_cong_avoid(ccv); 29767fef78bSLawrence Stewart } 29867fef78bSLawrence Stewart 29967fef78bSLawrence Stewart #endif /* _NETINET_CC_CUBIC_H_ */ 300