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 /* 4067fef78bSLawrence Stewart * An implementation of the CUBIC congestion control algorithm for FreeBSD, 4167fef78bSLawrence Stewart * based on the Internet Draft "draft-rhee-tcpm-cubic-02" by Rhee, Xu and Ha. 4267fef78bSLawrence Stewart * Originally released as part of the NewTCP research project at Swinburne 43891b8ed4SLawrence Stewart * University of Technology's Centre for Advanced Internet Architectures, 44891b8ed4SLawrence Stewart * Melbourne, Australia, which was made possible in part by a grant from the 45891b8ed4SLawrence Stewart * Cisco University Research Program Fund at Community Foundation Silicon 46891b8ed4SLawrence Stewart * Valley. More details are available at: 4767fef78bSLawrence Stewart * http://caia.swin.edu.au/urp/newtcp/ 4867fef78bSLawrence Stewart */ 4967fef78bSLawrence Stewart 5067fef78bSLawrence Stewart #include <sys/param.h> 5167fef78bSLawrence Stewart #include <sys/kernel.h> 52c968c769SMichael Tuexen #include <sys/limits.h> 5367fef78bSLawrence Stewart #include <sys/malloc.h> 5467fef78bSLawrence Stewart #include <sys/module.h> 5567fef78bSLawrence Stewart #include <sys/socket.h> 5667fef78bSLawrence Stewart #include <sys/socketvar.h> 5767fef78bSLawrence Stewart #include <sys/sysctl.h> 5867fef78bSLawrence Stewart #include <sys/systm.h> 5967fef78bSLawrence Stewart 6067fef78bSLawrence Stewart #include <net/vnet.h> 6167fef78bSLawrence Stewart 62b8d60729SRandall Stewart #include <net/route.h> 63b8d60729SRandall Stewart #include <net/route/nhop.h> 64b8d60729SRandall Stewart 65b8d60729SRandall Stewart #include <netinet/in_pcb.h> 662de3e790SGleb Smirnoff #include <netinet/tcp.h> 6767fef78bSLawrence Stewart #include <netinet/tcp_seq.h> 6867fef78bSLawrence Stewart #include <netinet/tcp_timer.h> 6967fef78bSLawrence Stewart #include <netinet/tcp_var.h> 70a9696510SRandall Stewart #include <netinet/tcp_log_buf.h> 71a9696510SRandall Stewart #include <netinet/tcp_hpts.h> 724644fda3SGleb Smirnoff #include <netinet/cc/cc.h> 7367fef78bSLawrence Stewart #include <netinet/cc/cc_cubic.h> 7467fef78bSLawrence Stewart #include <netinet/cc/cc_module.h> 7567fef78bSLawrence Stewart 76f74352fbSRichard Scheffenegger static void cubic_ack_received(struct cc_var *ccv, ccsignal_t type); 7767fef78bSLawrence Stewart static void cubic_cb_destroy(struct cc_var *ccv); 78b8d60729SRandall Stewart static int cubic_cb_init(struct cc_var *ccv, void *ptr); 79f74352fbSRichard Scheffenegger static void cubic_cong_signal(struct cc_var *ccv, ccsignal_t type); 8067fef78bSLawrence Stewart static void cubic_conn_init(struct cc_var *ccv); 8167fef78bSLawrence Stewart static int cubic_mod_init(void); 8267fef78bSLawrence Stewart static void cubic_post_recovery(struct cc_var *ccv); 8367fef78bSLawrence Stewart static void cubic_record_rtt(struct cc_var *ccv); 8437674273SRichard Scheffenegger static void cubic_ssthresh_update(struct cc_var *ccv, uint32_t maxseg); 8535cd141bSMichael Tuexen static void cubic_after_idle(struct cc_var *ccv); 86b8d60729SRandall Stewart static size_t cubic_data_sz(void); 87a9696510SRandall Stewart static void cubic_newround(struct cc_var *ccv, uint32_t round_cnt); 88a9696510SRandall Stewart static void cubic_rttsample(struct cc_var *ccv, uint32_t usec_rtt, 89a9696510SRandall Stewart uint32_t rxtcnt, uint32_t fas); 9067fef78bSLawrence Stewart 9167fef78bSLawrence Stewart struct cc_algo cubic_cc_algo = { 9267fef78bSLawrence Stewart .name = "cubic", 9367fef78bSLawrence Stewart .ack_received = cubic_ack_received, 9467fef78bSLawrence Stewart .cb_destroy = cubic_cb_destroy, 9567fef78bSLawrence Stewart .cb_init = cubic_cb_init, 9667fef78bSLawrence Stewart .cong_signal = cubic_cong_signal, 9767fef78bSLawrence Stewart .conn_init = cubic_conn_init, 9867fef78bSLawrence Stewart .mod_init = cubic_mod_init, 9967fef78bSLawrence Stewart .post_recovery = cubic_post_recovery, 10035cd141bSMichael Tuexen .after_idle = cubic_after_idle, 101a9696510SRandall Stewart .cc_data_sz = cubic_data_sz, 102a9696510SRandall Stewart .rttsample = cubic_rttsample, 103a9696510SRandall Stewart .newround = cubic_newround 10467fef78bSLawrence Stewart }; 10567fef78bSLawrence Stewart 10667fef78bSLawrence Stewart static void 107a9696510SRandall Stewart cubic_log_hystart_event(struct cc_var *ccv, struct cubic *cubicd, uint8_t mod, uint32_t flex1) 108a9696510SRandall Stewart { 109a9696510SRandall Stewart /* 110a9696510SRandall Stewart * Types of logs (mod value) 111a9696510SRandall Stewart * 1 - rtt_thresh in flex1, checking to see if RTT is to great. 112a9696510SRandall Stewart * 2 - rtt is too great, rtt_thresh in flex1. 113a9696510SRandall Stewart * 3 - CSS is active incr in flex1 114a9696510SRandall Stewart * 4 - A new round is beginning flex1 is round count 115a9696510SRandall Stewart * 5 - A new RTT measurement flex1 is the new measurement. 116a9696510SRandall Stewart * 6 - We enter CA ssthresh is also in flex1. 117a9696510SRandall Stewart * 7 - Socket option to change hystart executed opt.val in flex1. 118a9696510SRandall Stewart * 8 - Back out of CSS into SS, flex1 is the css_baseline_minrtt 119a9696510SRandall Stewart * 9 - We enter CA, via an ECN mark. 120a9696510SRandall Stewart * 10 - We enter CA, via a loss. 121a9696510SRandall Stewart * 11 - We have slipped out of SS into CA via cwnd growth. 122a9696510SRandall Stewart * 12 - After idle has re-enabled hystart++ 123a9696510SRandall Stewart */ 124a9696510SRandall Stewart struct tcpcb *tp; 125a9696510SRandall Stewart 126a9696510SRandall Stewart if (hystart_bblogs == 0) 127a9696510SRandall Stewart return; 12800d3b744SMichael Tuexen tp = ccv->tp; 12969c7c811SRandall Stewart if (tcp_bblogging_on(tp)) { 130a9696510SRandall Stewart union tcp_log_stackspecific log; 131a9696510SRandall Stewart struct timeval tv; 132a9696510SRandall Stewart 133a9696510SRandall Stewart memset(&log, 0, sizeof(log)); 134a9696510SRandall Stewart log.u_bbr.flex1 = flex1; 135a9696510SRandall Stewart log.u_bbr.flex2 = cubicd->css_current_round_minrtt; 136a9696510SRandall Stewart log.u_bbr.flex3 = cubicd->css_lastround_minrtt; 137a9696510SRandall Stewart log.u_bbr.flex4 = cubicd->css_rttsample_count; 138a9696510SRandall Stewart log.u_bbr.flex5 = cubicd->css_entered_at_round; 139a9696510SRandall Stewart log.u_bbr.flex6 = cubicd->css_baseline_minrtt; 140a9696510SRandall Stewart /* We only need bottom 16 bits of flags */ 141a9696510SRandall Stewart log.u_bbr.flex7 = cubicd->flags & 0x0000ffff; 142a9696510SRandall Stewart log.u_bbr.flex8 = mod; 143a9696510SRandall Stewart log.u_bbr.epoch = cubicd->css_current_round; 144a9696510SRandall Stewart log.u_bbr.timeStamp = tcp_get_usecs(&tv); 145a9696510SRandall Stewart log.u_bbr.lt_epoch = cubicd->css_fas_at_css_entry; 146a9696510SRandall Stewart log.u_bbr.pkts_out = cubicd->css_last_fas; 147a9696510SRandall Stewart log.u_bbr.delivered = cubicd->css_lowrtt_fas; 148a9696510SRandall Stewart log.u_bbr.pkt_epoch = ccv->flags; 149a9696510SRandall Stewart TCP_LOG_EVENTP(tp, NULL, 1509eb0e832SGleb Smirnoff &tptosocket(tp)->so_rcv, 1519eb0e832SGleb Smirnoff &tptosocket(tp)->so_snd, 152a9696510SRandall Stewart TCP_HYSTART, 0, 153a9696510SRandall Stewart 0, &log, false, &tv); 154a9696510SRandall Stewart } 155a9696510SRandall Stewart } 156a9696510SRandall Stewart 157a9696510SRandall Stewart static void 158a9696510SRandall Stewart cubic_does_slow_start(struct cc_var *ccv, struct cubic *cubicd) 159a9696510SRandall Stewart { 160a9696510SRandall Stewart /* 161a9696510SRandall Stewart * In slow-start with ABC enabled and no RTO in sight? 162a9696510SRandall Stewart * (Must not use abc_l_var > 1 if slow starting after 163a9696510SRandall Stewart * an RTO. On RTO, snd_nxt = snd_una, so the 164a9696510SRandall Stewart * snd_nxt == snd_max check is sufficient to 165a9696510SRandall Stewart * handle this). 166a9696510SRandall Stewart * 167a9696510SRandall Stewart * XXXLAS: Find a way to signal SS after RTO that 168a9696510SRandall Stewart * doesn't rely on tcpcb vars. 169a9696510SRandall Stewart */ 170a9696510SRandall Stewart u_int cw = CCV(ccv, snd_cwnd); 171*22dcc812SRichard Scheffenegger uint32_t mss = tcp_fixed_maxseg(ccv->tp); 172*22dcc812SRichard Scheffenegger u_int incr = mss; 173a9696510SRandall Stewart uint16_t abc_val; 174a9696510SRandall Stewart 175a9696510SRandall Stewart cubicd->flags |= CUBICFLAG_IN_SLOWSTART; 176a9696510SRandall Stewart if (ccv->flags & CCF_USE_LOCAL_ABC) 177a9696510SRandall Stewart abc_val = ccv->labc; 178a9696510SRandall Stewart else 179a9696510SRandall Stewart abc_val = V_tcp_abc_l_var; 180a9696510SRandall Stewart if ((ccv->flags & CCF_HYSTART_ALLOWED) && 181a9696510SRandall Stewart (cubicd->flags & CUBICFLAG_HYSTART_ENABLED) && 182a9696510SRandall Stewart ((cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) == 0)) { 183a9696510SRandall Stewart /* 184a9696510SRandall Stewart * Hystart is allowed and still enabled and we are not yet 185a9696510SRandall Stewart * in CSS. Lets check to see if we can make a decision on 186a9696510SRandall Stewart * if we need to go into CSS. 187a9696510SRandall Stewart */ 188a9696510SRandall Stewart if ((cubicd->css_rttsample_count >= hystart_n_rttsamples) && 189a9696510SRandall Stewart (cubicd->css_current_round_minrtt != 0xffffffff) && 190a9696510SRandall Stewart (cubicd->css_lastround_minrtt != 0xffffffff)) { 191a9696510SRandall Stewart uint32_t rtt_thresh; 192a9696510SRandall Stewart 193a9696510SRandall Stewart /* Clamp (minrtt_thresh, lastround/8, maxrtt_thresh) */ 194a9696510SRandall Stewart rtt_thresh = (cubicd->css_lastround_minrtt >> 3); 195a9696510SRandall Stewart if (rtt_thresh < hystart_minrtt_thresh) 196a9696510SRandall Stewart rtt_thresh = hystart_minrtt_thresh; 197a9696510SRandall Stewart if (rtt_thresh > hystart_maxrtt_thresh) 198a9696510SRandall Stewart rtt_thresh = hystart_maxrtt_thresh; 199a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubicd, 1, rtt_thresh); 200a9696510SRandall Stewart 201a9696510SRandall Stewart if (cubicd->css_current_round_minrtt >= (cubicd->css_lastround_minrtt + rtt_thresh)) { 202a9696510SRandall Stewart /* Enter CSS */ 203a9696510SRandall Stewart cubicd->flags |= CUBICFLAG_HYSTART_IN_CSS; 204a9696510SRandall Stewart cubicd->css_fas_at_css_entry = cubicd->css_lowrtt_fas; 205a9696510SRandall Stewart /* 206a9696510SRandall Stewart * The draft (v4) calls for us to set baseline to css_current_round_min 207a9696510SRandall Stewart * but that can cause an oscillation. We probably shoudl be using 208a9696510SRandall Stewart * css_lastround_minrtt, but the authors insist that will cause 209a9696510SRandall Stewart * issues on exiting early. We will leave the draft version for now 210a9696510SRandall Stewart * but I suspect this is incorrect. 211a9696510SRandall Stewart */ 212a9696510SRandall Stewart cubicd->css_baseline_minrtt = cubicd->css_current_round_minrtt; 213a9696510SRandall Stewart cubicd->css_entered_at_round = cubicd->css_current_round; 214a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubicd, 2, rtt_thresh); 215a9696510SRandall Stewart } 216a9696510SRandall Stewart } 217a9696510SRandall Stewart } 218a9696510SRandall Stewart if (CCV(ccv, snd_nxt) == CCV(ccv, snd_max)) 219a9696510SRandall Stewart incr = min(ccv->bytes_this_ack, 220*22dcc812SRichard Scheffenegger ccv->nsegs * abc_val * mss); 221a9696510SRandall Stewart else 222*22dcc812SRichard Scheffenegger incr = min(ccv->bytes_this_ack, mss); 223a9696510SRandall Stewart 224a9696510SRandall Stewart /* Only if Hystart is enabled will the flag get set */ 225a9696510SRandall Stewart if (cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) { 226a9696510SRandall Stewart incr /= hystart_css_growth_div; 227a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubicd, 3, incr); 228a9696510SRandall Stewart } 229a9696510SRandall Stewart /* ABC is on by default, so incr equals 0 frequently. */ 230a9696510SRandall Stewart if (incr > 0) 231a9696510SRandall Stewart CCV(ccv, snd_cwnd) = min((cw + incr), 232a9696510SRandall Stewart TCP_MAXWIN << CCV(ccv, snd_scale)); 233a9696510SRandall Stewart } 234a9696510SRandall Stewart 235a9696510SRandall Stewart static void 236f74352fbSRichard Scheffenegger cubic_ack_received(struct cc_var *ccv, ccsignal_t type) 23767fef78bSLawrence Stewart { 23867fef78bSLawrence Stewart struct cubic *cubic_data; 239eb5bfdd0SRichard Scheffenegger unsigned long W_est, W_cubic; 240eb5bfdd0SRichard Scheffenegger int usecs_since_epoch; 241*22dcc812SRichard Scheffenegger uint32_t mss = tcp_fixed_maxseg(ccv->tp); 24267fef78bSLawrence Stewart 24367fef78bSLawrence Stewart cubic_data = ccv->cc_data; 24467fef78bSLawrence Stewart cubic_record_rtt(ccv); 24567fef78bSLawrence Stewart 24667fef78bSLawrence Stewart /* 247ad7a0eb1SRichard Scheffenegger * For a regular ACK and we're not in cong/fast recovery and 248ad7a0eb1SRichard Scheffenegger * we're cwnd limited, always recalculate cwnd. 24967fef78bSLawrence Stewart */ 25067fef78bSLawrence Stewart if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) && 251ad7a0eb1SRichard Scheffenegger (ccv->flags & CCF_CWND_LIMITED)) { 25267fef78bSLawrence Stewart /* Use the logic in NewReno ack_received() for slow start. */ 25367fef78bSLawrence Stewart if (CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh) || 254a3aa6f65SCheng Cui cubic_data->min_rtt_usecs == TCPTV_SRTTBASE) { 255a9696510SRandall Stewart cubic_does_slow_start(ccv, cubic_data); 2566907bbaeSRichard Scheffenegger } else { 257a9696510SRandall Stewart if (cubic_data->flags & CUBICFLAG_HYSTART_IN_CSS) { 258a9696510SRandall Stewart /* 259a9696510SRandall Stewart * We have slipped into CA with 260a9696510SRandall Stewart * CSS active. Deactivate all. 261a9696510SRandall Stewart */ 262a9696510SRandall Stewart /* Turn off the CSS flag */ 263a9696510SRandall Stewart cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS; 264a9696510SRandall Stewart /* Disable use of CSS in the future except long idle */ 265a9696510SRandall Stewart cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED; 266a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubic_data, 11, CCV(ccv, snd_ssthresh)); 267a9696510SRandall Stewart } 26837674273SRichard Scheffenegger if ((cubic_data->flags & CUBICFLAG_RTO_EVENT) && 26937674273SRichard Scheffenegger (cubic_data->flags & CUBICFLAG_IN_SLOWSTART)) { 27037674273SRichard Scheffenegger /* RFC8312 Section 4.7 */ 27137674273SRichard Scheffenegger cubic_data->flags &= ~(CUBICFLAG_RTO_EVENT | 27237674273SRichard Scheffenegger CUBICFLAG_IN_SLOWSTART); 273eb5bfdd0SRichard Scheffenegger cubic_data->W_max = CCV(ccv, snd_cwnd); 274038699a8SRichard Scheffenegger cubic_data->t_epoch = ticks; 27537674273SRichard Scheffenegger cubic_data->K = 0; 27637674273SRichard Scheffenegger } else if (cubic_data->flags & (CUBICFLAG_IN_SLOWSTART | 2772bb6dfabSRichard Scheffenegger CUBICFLAG_IN_APPLIMIT)) { 2782bb6dfabSRichard Scheffenegger cubic_data->flags &= ~(CUBICFLAG_IN_SLOWSTART | 2792bb6dfabSRichard Scheffenegger CUBICFLAG_IN_APPLIMIT); 280eb5bfdd0SRichard Scheffenegger cubic_data->t_epoch = ticks; 281*22dcc812SRichard Scheffenegger cubic_data->K = cubic_k(cubic_data->W_max / mss); 2822bb6dfabSRichard Scheffenegger } 283eb5bfdd0SRichard Scheffenegger usecs_since_epoch = (ticks - cubic_data->t_epoch) * tick; 284eb5bfdd0SRichard Scheffenegger if (usecs_since_epoch < 0) { 285c968c769SMichael Tuexen /* 286eb5bfdd0SRichard Scheffenegger * dragging t_epoch along 287c968c769SMichael Tuexen */ 288eb5bfdd0SRichard Scheffenegger usecs_since_epoch = INT_MAX; 289eb5bfdd0SRichard Scheffenegger cubic_data->t_epoch = ticks - INT_MAX; 290c968c769SMichael Tuexen } 291ee450610SCheng Cui 292ee450610SCheng Cui W_est = tf_cwnd(ccv); 293ee450610SCheng Cui 29467fef78bSLawrence Stewart /* 29567fef78bSLawrence Stewart * The mean RTT is used to best reflect the equations in 296ee450610SCheng Cui * the I-D. 29767fef78bSLawrence Stewart */ 298eb5bfdd0SRichard Scheffenegger W_cubic = cubic_cwnd(usecs_since_epoch + 299a3aa6f65SCheng Cui cubic_data->mean_rtt_usecs, 300eb5bfdd0SRichard Scheffenegger cubic_data->W_max, 301*22dcc812SRichard Scheffenegger tcp_fixed_maxseg(ccv->tp), 302a3aa6f65SCheng Cui cubic_data->K); 30367fef78bSLawrence Stewart 304eb5bfdd0SRichard Scheffenegger if (W_cubic < W_est) { 30567fef78bSLawrence Stewart /* 30667fef78bSLawrence Stewart * TCP-friendly region, follow tf 30767fef78bSLawrence Stewart * cwnd growth. 30867fef78bSLawrence Stewart */ 309eb5bfdd0SRichard Scheffenegger CCV(ccv, snd_cwnd) = ulmin(W_est, INT_MAX); 310ee450610SCheng Cui cubic_data->flags |= CUBICFLAG_IN_TF; 311eb5bfdd0SRichard Scheffenegger } else if (CCV(ccv, snd_cwnd) < W_cubic) { 31267fef78bSLawrence Stewart /* 31367fef78bSLawrence Stewart * Concave or convex region, follow CUBIC 31467fef78bSLawrence Stewart * cwnd growth. 315cce999b3SRichard Scheffenegger * Only update snd_cwnd, if it doesn't shrink. 31667fef78bSLawrence Stewart */ 317eb5bfdd0SRichard Scheffenegger CCV(ccv, snd_cwnd) = ulmin(W_cubic, INT_MAX); 318ee450610SCheng Cui cubic_data->flags &= ~CUBICFLAG_IN_TF; 31967fef78bSLawrence Stewart } 32067fef78bSLawrence Stewart 32167fef78bSLawrence Stewart /* 32267fef78bSLawrence Stewart * If we're not in slow start and we're probing for a 32367fef78bSLawrence Stewart * new cwnd limit at the start of a connection 32467fef78bSLawrence Stewart * (happens when hostcache has a relevant entry), 32567fef78bSLawrence Stewart * keep updating our current estimate of the 326eb5bfdd0SRichard Scheffenegger * W_max. 32767fef78bSLawrence Stewart */ 3286907bbaeSRichard Scheffenegger if (((cubic_data->flags & CUBICFLAG_CONG_EVENT) == 0) && 329eb5bfdd0SRichard Scheffenegger cubic_data->W_max < CCV(ccv, snd_cwnd)) { 330eb5bfdd0SRichard Scheffenegger cubic_data->W_max = CCV(ccv, snd_cwnd); 331eb5bfdd0SRichard Scheffenegger cubic_data->K = cubic_k(cubic_data->W_max / 332*22dcc812SRichard Scheffenegger tcp_fixed_maxseg(ccv->tp)); 33367fef78bSLawrence Stewart } 33467fef78bSLawrence Stewart } 3352fda0a6fSRichard Scheffenegger } else if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) && 3362fda0a6fSRichard Scheffenegger !(ccv->flags & CCF_CWND_LIMITED)) { 3372fda0a6fSRichard Scheffenegger cubic_data->flags |= CUBICFLAG_IN_APPLIMIT; 33867fef78bSLawrence Stewart } 3397d87664aSMichael Tuexen } 34067fef78bSLawrence Stewart 34135cd141bSMichael Tuexen /* 342ea6d0de2SRichard Scheffenegger * This is a CUBIC specific implementation of after_idle. 34335cd141bSMichael Tuexen * - Reset cwnd by calling New Reno implementation of after_idle. 344eb5bfdd0SRichard Scheffenegger * - Reset t_epoch. 34535cd141bSMichael Tuexen */ 34635cd141bSMichael Tuexen static void 34735cd141bSMichael Tuexen cubic_after_idle(struct cc_var *ccv) 34835cd141bSMichael Tuexen { 34935cd141bSMichael Tuexen struct cubic *cubic_data; 35035cd141bSMichael Tuexen 35135cd141bSMichael Tuexen cubic_data = ccv->cc_data; 35235cd141bSMichael Tuexen 353eb5bfdd0SRichard Scheffenegger cubic_data->W_max = ulmax(cubic_data->W_max, CCV(ccv, snd_cwnd)); 354*22dcc812SRichard Scheffenegger cubic_data->K = cubic_k(cubic_data->W_max / tcp_fixed_maxseg(ccv->tp)); 355a9696510SRandall Stewart if ((cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) == 0) { 356a9696510SRandall Stewart /* 357a9696510SRandall Stewart * Re-enable hystart if we have been idle. 358a9696510SRandall Stewart */ 359a9696510SRandall Stewart cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS; 360a9696510SRandall Stewart cubic_data->flags |= CUBICFLAG_HYSTART_ENABLED; 361a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubic_data, 12, CCV(ccv, snd_ssthresh)); 362a9696510SRandall Stewart } 363b8d60729SRandall Stewart newreno_cc_after_idle(ccv); 364eb5bfdd0SRichard Scheffenegger cubic_data->t_epoch = ticks; 36535cd141bSMichael Tuexen } 36635cd141bSMichael Tuexen 36767fef78bSLawrence Stewart static void 36867fef78bSLawrence Stewart cubic_cb_destroy(struct cc_var *ccv) 36967fef78bSLawrence Stewart { 370b8d60729SRandall Stewart free(ccv->cc_data, M_CC_MEM); 371b8d60729SRandall Stewart } 372b8d60729SRandall Stewart 373b8d60729SRandall Stewart static size_t 374b8d60729SRandall Stewart cubic_data_sz(void) 375b8d60729SRandall Stewart { 376b8d60729SRandall Stewart return (sizeof(struct cubic)); 37767fef78bSLawrence Stewart } 37867fef78bSLawrence Stewart 37967fef78bSLawrence Stewart static int 380b8d60729SRandall Stewart cubic_cb_init(struct cc_var *ccv, void *ptr) 38167fef78bSLawrence Stewart { 38267fef78bSLawrence Stewart struct cubic *cubic_data; 38367fef78bSLawrence Stewart 38400d3b744SMichael Tuexen INP_WLOCK_ASSERT(tptoinpcb(ccv->tp)); 385b8d60729SRandall Stewart if (ptr == NULL) { 386b8d60729SRandall Stewart cubic_data = malloc(sizeof(struct cubic), M_CC_MEM, M_NOWAIT|M_ZERO); 38767fef78bSLawrence Stewart if (cubic_data == NULL) 38867fef78bSLawrence Stewart return (ENOMEM); 389b8d60729SRandall Stewart } else 390b8d60729SRandall Stewart cubic_data = ptr; 39167fef78bSLawrence Stewart 39267fef78bSLawrence Stewart /* Init some key variables with sensible defaults. */ 393eb5bfdd0SRichard Scheffenegger cubic_data->t_epoch = ticks; 394a3aa6f65SCheng Cui cubic_data->min_rtt_usecs = TCPTV_SRTTBASE; 395a3aa6f65SCheng Cui cubic_data->mean_rtt_usecs = 1; 39667fef78bSLawrence Stewart 39767fef78bSLawrence Stewart ccv->cc_data = cubic_data; 398a9696510SRandall Stewart cubic_data->flags = CUBICFLAG_HYSTART_ENABLED; 399a9696510SRandall Stewart /* At init set both to infinity */ 400a9696510SRandall Stewart cubic_data->css_lastround_minrtt = 0xffffffff; 401a9696510SRandall Stewart cubic_data->css_current_round_minrtt = 0xffffffff; 402a9696510SRandall Stewart cubic_data->css_current_round = 0; 403a9696510SRandall Stewart cubic_data->css_baseline_minrtt = 0xffffffff; 404a9696510SRandall Stewart cubic_data->css_rttsample_count = 0; 405a9696510SRandall Stewart cubic_data->css_entered_at_round = 0; 406a9696510SRandall Stewart cubic_data->css_fas_at_css_entry = 0; 407a9696510SRandall Stewart cubic_data->css_lowrtt_fas = 0; 408a9696510SRandall Stewart cubic_data->css_last_fas = 0; 40967fef78bSLawrence Stewart 41067fef78bSLawrence Stewart return (0); 41167fef78bSLawrence Stewart } 41267fef78bSLawrence Stewart 41367fef78bSLawrence Stewart /* 41467fef78bSLawrence Stewart * Perform any necessary tasks before we enter congestion recovery. 41567fef78bSLawrence Stewart */ 41667fef78bSLawrence Stewart static void 417f74352fbSRichard Scheffenegger cubic_cong_signal(struct cc_var *ccv, ccsignal_t type) 41867fef78bSLawrence Stewart { 41967fef78bSLawrence Stewart struct cubic *cubic_data; 42032a6df57SRichard Scheffenegger uint32_t mss, pipe; 42167fef78bSLawrence Stewart 42267fef78bSLawrence Stewart cubic_data = ccv->cc_data; 42300d3b744SMichael Tuexen mss = tcp_fixed_maxseg(ccv->tp); 42467fef78bSLawrence Stewart 42567fef78bSLawrence Stewart switch (type) { 42667fef78bSLawrence Stewart case CC_NDUPACK: 427a9696510SRandall Stewart if (cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) { 428a9696510SRandall Stewart /* Make sure the flags are all off we had a loss */ 429a9696510SRandall Stewart cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED; 430a9696510SRandall Stewart cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS; 431a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubic_data, 10, CCV(ccv, snd_ssthresh)); 432a9696510SRandall Stewart } 43367fef78bSLawrence Stewart if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) { 43467fef78bSLawrence Stewart if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) { 43537674273SRichard Scheffenegger cubic_ssthresh_update(ccv, mss); 4366907bbaeSRichard Scheffenegger cubic_data->flags |= CUBICFLAG_CONG_EVENT; 437eb5bfdd0SRichard Scheffenegger cubic_data->t_epoch = ticks; 438eb5bfdd0SRichard Scheffenegger cubic_data->K = cubic_k(cubic_data->W_max / mss); 43967fef78bSLawrence Stewart } 44067fef78bSLawrence Stewart ENTER_RECOVERY(CCV(ccv, t_flags)); 44167fef78bSLawrence Stewart } 44267fef78bSLawrence Stewart break; 44367fef78bSLawrence Stewart 44467fef78bSLawrence Stewart case CC_ECN: 445a9696510SRandall Stewart if (cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) { 446a9696510SRandall Stewart /* Make sure the flags are all off we had a loss */ 447a9696510SRandall Stewart cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED; 448a9696510SRandall Stewart cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS; 449a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubic_data, 9, CCV(ccv, snd_ssthresh)); 450a9696510SRandall Stewart } 45167fef78bSLawrence Stewart if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) { 45237674273SRichard Scheffenegger cubic_ssthresh_update(ccv, mss); 4536907bbaeSRichard Scheffenegger cubic_data->flags |= CUBICFLAG_CONG_EVENT; 454eb5bfdd0SRichard Scheffenegger cubic_data->t_epoch = ticks; 455eb5bfdd0SRichard Scheffenegger cubic_data->K = cubic_k(cubic_data->W_max / mss); 45667fef78bSLawrence Stewart CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh); 45767fef78bSLawrence Stewart ENTER_CONGRECOVERY(CCV(ccv, t_flags)); 45867fef78bSLawrence Stewart } 45967fef78bSLawrence Stewart break; 46067fef78bSLawrence Stewart 46167fef78bSLawrence Stewart case CC_RTO: 46237674273SRichard Scheffenegger /* RFC8312 Section 4.7 */ 46337674273SRichard Scheffenegger if (CCV(ccv, t_rxtshift) == 1) { 464eb5bfdd0SRichard Scheffenegger /* 465eb5bfdd0SRichard Scheffenegger * Remember the state only for the first RTO event. This 466eb5bfdd0SRichard Scheffenegger * will help us restore the state to the values seen 467eb5bfdd0SRichard Scheffenegger * at the most recent congestion avoidance stage before 468eb5bfdd0SRichard Scheffenegger * the current RTO event. 469eb5bfdd0SRichard Scheffenegger */ 470eb5bfdd0SRichard Scheffenegger cubic_data->undo_t_epoch = cubic_data->t_epoch; 471eb5bfdd0SRichard Scheffenegger cubic_data->undo_cwnd_epoch = cubic_data->cwnd_epoch; 472eb5bfdd0SRichard Scheffenegger cubic_data->undo_W_est = cubic_data->W_est; 473eb5bfdd0SRichard Scheffenegger cubic_data->undo_cwnd_prior = cubic_data->cwnd_prior; 474eb5bfdd0SRichard Scheffenegger cubic_data->undo_W_max = cubic_data->W_max; 475eb5bfdd0SRichard Scheffenegger cubic_data->undo_K = cubic_data->K; 47632a6df57SRichard Scheffenegger if (V_tcp_do_newsack) { 47700d3b744SMichael Tuexen pipe = tcp_compute_pipe(ccv->tp); 47832a6df57SRichard Scheffenegger } else { 479fcea1cc9SRichard Scheffenegger pipe = CCV(ccv, snd_max) - 48032a6df57SRichard Scheffenegger CCV(ccv, snd_fack) + 48132a6df57SRichard Scheffenegger CCV(ccv, sackhint.sack_bytes_rexmit); 48232a6df57SRichard Scheffenegger } 48332a6df57SRichard Scheffenegger CCV(ccv, snd_ssthresh) = max(2, 48432a6df57SRichard Scheffenegger (((uint64_t)min(CCV(ccv, snd_wnd), pipe) * 48532a6df57SRichard Scheffenegger CUBIC_BETA) >> CUBIC_SHIFT) / mss) * mss; 4861edbb54fSEd Maste } 48737674273SRichard Scheffenegger cubic_data->flags |= CUBICFLAG_CONG_EVENT | CUBICFLAG_RTO_EVENT; 488eb5bfdd0SRichard Scheffenegger cubic_data->undo_W_max = cubic_data->W_max; 48937674273SRichard Scheffenegger CCV(ccv, snd_cwnd) = mss; 49037674273SRichard Scheffenegger break; 49137674273SRichard Scheffenegger 49237674273SRichard Scheffenegger case CC_RTO_ERR: 49337674273SRichard Scheffenegger cubic_data->flags &= ~(CUBICFLAG_CONG_EVENT | CUBICFLAG_RTO_EVENT); 494eb5bfdd0SRichard Scheffenegger cubic_data->K = cubic_data->undo_K; 495eb5bfdd0SRichard Scheffenegger cubic_data->cwnd_prior = cubic_data->undo_cwnd_prior; 496eb5bfdd0SRichard Scheffenegger cubic_data->W_max = cubic_data->undo_W_max; 497eb5bfdd0SRichard Scheffenegger cubic_data->W_est = cubic_data->undo_W_est; 498eb5bfdd0SRichard Scheffenegger cubic_data->cwnd_epoch = cubic_data->undo_cwnd_epoch; 499eb5bfdd0SRichard Scheffenegger cubic_data->t_epoch = cubic_data->undo_t_epoch; 50067fef78bSLawrence Stewart break; 501f74352fbSRichard Scheffenegger default: 502f74352fbSRichard Scheffenegger break; 50367fef78bSLawrence Stewart } 50467fef78bSLawrence Stewart } 50567fef78bSLawrence Stewart 50667fef78bSLawrence Stewart static void 50767fef78bSLawrence Stewart cubic_conn_init(struct cc_var *ccv) 50867fef78bSLawrence Stewart { 50967fef78bSLawrence Stewart struct cubic *cubic_data; 51067fef78bSLawrence Stewart 51167fef78bSLawrence Stewart cubic_data = ccv->cc_data; 51267fef78bSLawrence Stewart 51367fef78bSLawrence Stewart /* 514eb5bfdd0SRichard Scheffenegger * Ensure we have a sane initial value for W_max recorded. Without 51567fef78bSLawrence Stewart * this here bad things happen when entries from the TCP hostcache 51667fef78bSLawrence Stewart * get used. 51767fef78bSLawrence Stewart */ 518eb5bfdd0SRichard Scheffenegger cubic_data->W_max = CCV(ccv, snd_cwnd); 51967fef78bSLawrence Stewart } 52067fef78bSLawrence Stewart 52167fef78bSLawrence Stewart static int 52267fef78bSLawrence Stewart cubic_mod_init(void) 52367fef78bSLawrence Stewart { 52467fef78bSLawrence Stewart return (0); 52567fef78bSLawrence Stewart } 52667fef78bSLawrence Stewart 52767fef78bSLawrence Stewart /* 52867fef78bSLawrence Stewart * Perform any necessary tasks before we exit congestion recovery. 52967fef78bSLawrence Stewart */ 53067fef78bSLawrence Stewart static void 53167fef78bSLawrence Stewart cubic_post_recovery(struct cc_var *ccv) 53267fef78bSLawrence Stewart { 53367fef78bSLawrence Stewart struct cubic *cubic_data; 534f81bc34eSHiren Panchasara int pipe; 535*22dcc812SRichard Scheffenegger uint32_t mss = tcp_fixed_maxseg(ccv->tp); 53667fef78bSLawrence Stewart 53767fef78bSLawrence Stewart cubic_data = ccv->cc_data; 538f81bc34eSHiren Panchasara pipe = 0; 53967fef78bSLawrence Stewart 54067fef78bSLawrence Stewart if (IN_FASTRECOVERY(CCV(ccv, t_flags))) { 54167fef78bSLawrence Stewart /* 54267fef78bSLawrence Stewart * If inflight data is less than ssthresh, set cwnd 54367fef78bSLawrence Stewart * conservatively to avoid a burst of data, as suggested in 54467fef78bSLawrence Stewart * the NewReno RFC. Otherwise, use the CUBIC method. 54567fef78bSLawrence Stewart * 54667fef78bSLawrence Stewart * XXXLAS: Find a way to do this without needing curack 54767fef78bSLawrence Stewart */ 548d1de2b05SRichard Scheffenegger if (V_tcp_do_newsack) 54900d3b744SMichael Tuexen pipe = tcp_compute_pipe(ccv->tp); 550f81bc34eSHiren Panchasara else 551f81bc34eSHiren Panchasara pipe = CCV(ccv, snd_max) - ccv->curack; 552f81bc34eSHiren Panchasara 553f81bc34eSHiren Panchasara if (pipe < CCV(ccv, snd_ssthresh)) 5545cc11a89SMichael Tuexen /* 5555cc11a89SMichael Tuexen * Ensure that cwnd does not collapse to 1 MSS under 5565cc11a89SMichael Tuexen * adverse conditions. Implements RFC6582 5575cc11a89SMichael Tuexen */ 558*22dcc812SRichard Scheffenegger CCV(ccv, snd_cwnd) = max(pipe, mss) + mss; 55967fef78bSLawrence Stewart else 560eb5bfdd0SRichard Scheffenegger /* Update cwnd based on beta and adjusted W_max. */ 561eb5bfdd0SRichard Scheffenegger CCV(ccv, snd_cwnd) = max(((uint64_t)cubic_data->W_max * 56214558b99SRichard Scheffenegger CUBIC_BETA) >> CUBIC_SHIFT, 563*22dcc812SRichard Scheffenegger 2 * mss); 56467fef78bSLawrence Stewart } 56567fef78bSLawrence Stewart 56667fef78bSLawrence Stewart /* Calculate the average RTT between congestion epochs. */ 56747f44cddSLawrence Stewart if (cubic_data->epoch_ack_count > 0 && 568a3aa6f65SCheng Cui cubic_data->sum_rtt_usecs >= cubic_data->epoch_ack_count) { 569a3aa6f65SCheng Cui cubic_data->mean_rtt_usecs = (int)(cubic_data->sum_rtt_usecs / 57067fef78bSLawrence Stewart cubic_data->epoch_ack_count); 57147f44cddSLawrence Stewart } 57267fef78bSLawrence Stewart 57367fef78bSLawrence Stewart cubic_data->epoch_ack_count = 0; 574a3aa6f65SCheng Cui cubic_data->sum_rtt_usecs = 0; 57567fef78bSLawrence Stewart } 57667fef78bSLawrence Stewart 57767fef78bSLawrence Stewart /* 57867fef78bSLawrence Stewart * Record the min RTT and sum samples for the epoch average RTT calculation. 57967fef78bSLawrence Stewart */ 58067fef78bSLawrence Stewart static void 58167fef78bSLawrence Stewart cubic_record_rtt(struct cc_var *ccv) 58267fef78bSLawrence Stewart { 58367fef78bSLawrence Stewart struct cubic *cubic_data; 584a3aa6f65SCheng Cui uint32_t t_srtt_usecs; 58567fef78bSLawrence Stewart 58667fef78bSLawrence Stewart /* Ignore srtt until a min number of samples have been taken. */ 58767fef78bSLawrence Stewart if (CCV(ccv, t_rttupdated) >= CUBIC_MIN_RTT_SAMPLES) { 58867fef78bSLawrence Stewart cubic_data = ccv->cc_data; 58900d3b744SMichael Tuexen t_srtt_usecs = tcp_get_srtt(ccv->tp, 590a3aa6f65SCheng Cui TCP_TMR_GRANULARITY_USEC); 59167fef78bSLawrence Stewart /* 59267fef78bSLawrence Stewart * Record the current SRTT as our minrtt if it's the smallest 59367fef78bSLawrence Stewart * we've seen or minrtt is currently equal to its initialised 59467fef78bSLawrence Stewart * value. 59567fef78bSLawrence Stewart * 59667fef78bSLawrence Stewart * XXXLAS: Should there be some hysteresis for minrtt? 59767fef78bSLawrence Stewart */ 598a3aa6f65SCheng Cui if ((t_srtt_usecs < cubic_data->min_rtt_usecs || 599a3aa6f65SCheng Cui cubic_data->min_rtt_usecs == TCPTV_SRTTBASE)) { 600a3aa6f65SCheng Cui /* A minimal rtt is a single unshifted tick of a ticks 601a3aa6f65SCheng Cui * timer. */ 602a3aa6f65SCheng Cui cubic_data->min_rtt_usecs = max(tick >> TCP_RTT_SHIFT, 603a3aa6f65SCheng Cui t_srtt_usecs); 60467fef78bSLawrence Stewart 60547f44cddSLawrence Stewart /* 60647f44cddSLawrence Stewart * If the connection is within its first congestion 607a3aa6f65SCheng Cui * epoch, ensure we prime mean_rtt_usecs with a 60847f44cddSLawrence Stewart * reasonable value until the epoch average RTT is 60947f44cddSLawrence Stewart * calculated in cubic_post_recovery(). 61047f44cddSLawrence Stewart */ 611a3aa6f65SCheng Cui if (cubic_data->min_rtt_usecs > 612a3aa6f65SCheng Cui cubic_data->mean_rtt_usecs) 613a3aa6f65SCheng Cui cubic_data->mean_rtt_usecs = 614a3aa6f65SCheng Cui cubic_data->min_rtt_usecs; 61547f44cddSLawrence Stewart } 61647f44cddSLawrence Stewart 61767fef78bSLawrence Stewart /* Sum samples for epoch average RTT calculation. */ 618a3aa6f65SCheng Cui cubic_data->sum_rtt_usecs += t_srtt_usecs; 61967fef78bSLawrence Stewart cubic_data->epoch_ack_count++; 62067fef78bSLawrence Stewart } 62167fef78bSLawrence Stewart } 62267fef78bSLawrence Stewart 62367fef78bSLawrence Stewart /* 62467fef78bSLawrence Stewart * Update the ssthresh in the event of congestion. 62567fef78bSLawrence Stewart */ 62667fef78bSLawrence Stewart static void 62737674273SRichard Scheffenegger cubic_ssthresh_update(struct cc_var *ccv, uint32_t maxseg) 62867fef78bSLawrence Stewart { 62967fef78bSLawrence Stewart struct cubic *cubic_data; 63014558b99SRichard Scheffenegger uint32_t ssthresh; 631a459638fSRichard Scheffenegger uint32_t cwnd; 63267fef78bSLawrence Stewart 63367fef78bSLawrence Stewart cubic_data = ccv->cc_data; 634a459638fSRichard Scheffenegger cwnd = CCV(ccv, snd_cwnd); 635a459638fSRichard Scheffenegger 636a459638fSRichard Scheffenegger /* Fast convergence heuristic. */ 637eb5bfdd0SRichard Scheffenegger if (cwnd < cubic_data->W_max) { 638a459638fSRichard Scheffenegger cwnd = ((uint64_t)cwnd * CUBIC_FC_FACTOR) >> CUBIC_SHIFT; 639a459638fSRichard Scheffenegger } 640eb5bfdd0SRichard Scheffenegger cubic_data->undo_W_max = cubic_data->W_max; 641eb5bfdd0SRichard Scheffenegger cubic_data->W_max = cwnd; 64267fef78bSLawrence Stewart 643ee450610SCheng Cui if (cubic_data->flags & CUBICFLAG_IN_TF) { 644ee450610SCheng Cui /* If in the TCP friendly region, follow what newreno does */ 645ee450610SCheng Cui ssthresh = newreno_cc_cwnd_on_multiplicative_decrease(ccv, maxseg); 646ee450610SCheng Cui 647ee450610SCheng Cui } else if ((cubic_data->flags & CUBICFLAG_CONG_EVENT) == 0) { 64867fef78bSLawrence Stewart /* 649a459638fSRichard Scheffenegger * On the first congestion event, set ssthresh to cwnd * 0.5 650ee450610SCheng Cui * and reduce W_max to cwnd * beta. This aligns the cubic 651ee450610SCheng Cui * concave region appropriately. 65267fef78bSLawrence Stewart */ 653a459638fSRichard Scheffenegger ssthresh = cwnd >> 1; 654ee450610SCheng Cui cubic_data->W_max = ((uint64_t)cwnd * CUBIC_BETA) >> CUBIC_SHIFT; 655a459638fSRichard Scheffenegger } else { 656ee450610SCheng Cui /* 657ee450610SCheng Cui * On subsequent congestion events, set ssthresh to cwnd * beta. 658ee450610SCheng Cui */ 659ee450610SCheng Cui ssthresh = ((uint64_t)cwnd * CUBIC_BETA) >> CUBIC_SHIFT; 660a459638fSRichard Scheffenegger } 66137674273SRichard Scheffenegger CCV(ccv, snd_ssthresh) = max(ssthresh, 2 * maxseg); 66267fef78bSLawrence Stewart } 66367fef78bSLawrence Stewart 664a9696510SRandall Stewart static void 665a9696510SRandall Stewart cubic_rttsample(struct cc_var *ccv, uint32_t usec_rtt, uint32_t rxtcnt, uint32_t fas) 666a9696510SRandall Stewart { 667a9696510SRandall Stewart struct cubic *cubicd; 668a9696510SRandall Stewart 669a9696510SRandall Stewart cubicd = ccv->cc_data; 670a9696510SRandall Stewart if (rxtcnt > 1) { 671a9696510SRandall Stewart /* 672a9696510SRandall Stewart * Only look at RTT's that are non-ambiguous. 673a9696510SRandall Stewart */ 674a9696510SRandall Stewart return; 675a9696510SRandall Stewart } 676a9696510SRandall Stewart cubicd->css_rttsample_count++; 677a9696510SRandall Stewart cubicd->css_last_fas = fas; 678a9696510SRandall Stewart if (cubicd->css_current_round_minrtt > usec_rtt) { 679a9696510SRandall Stewart cubicd->css_current_round_minrtt = usec_rtt; 680a9696510SRandall Stewart cubicd->css_lowrtt_fas = cubicd->css_last_fas; 681a9696510SRandall Stewart } 682a9696510SRandall Stewart if ((cubicd->css_rttsample_count >= hystart_n_rttsamples) && 683a9696510SRandall Stewart (cubicd->css_current_round_minrtt != 0xffffffff) && 684e88412d8SRandall Stewart (cubicd->css_current_round_minrtt < cubicd->css_baseline_minrtt) && 685a9696510SRandall Stewart (cubicd->css_lastround_minrtt != 0xffffffff)) { 686a9696510SRandall Stewart /* 687a9696510SRandall Stewart * We were in CSS and the RTT is now less, we 688a9696510SRandall Stewart * entered CSS erroneously. 689a9696510SRandall Stewart */ 690a9696510SRandall Stewart cubicd->flags &= ~CUBICFLAG_HYSTART_IN_CSS; 691a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubicd, 8, cubicd->css_baseline_minrtt); 692a9696510SRandall Stewart cubicd->css_baseline_minrtt = 0xffffffff; 693a9696510SRandall Stewart } 694a9696510SRandall Stewart if (cubicd->flags & CUBICFLAG_HYSTART_ENABLED) 695a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubicd, 5, usec_rtt); 696a9696510SRandall Stewart } 697a9696510SRandall Stewart 698a9696510SRandall Stewart static void 699a9696510SRandall Stewart cubic_newround(struct cc_var *ccv, uint32_t round_cnt) 700a9696510SRandall Stewart { 701a9696510SRandall Stewart struct cubic *cubicd; 702a9696510SRandall Stewart 703a9696510SRandall Stewart cubicd = ccv->cc_data; 704a9696510SRandall Stewart /* We have entered a new round */ 705a9696510SRandall Stewart cubicd->css_lastround_minrtt = cubicd->css_current_round_minrtt; 706a9696510SRandall Stewart cubicd->css_current_round_minrtt = 0xffffffff; 707a9696510SRandall Stewart cubicd->css_rttsample_count = 0; 708a9696510SRandall Stewart cubicd->css_current_round = round_cnt; 709a9696510SRandall Stewart if ((cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) && 710a9696510SRandall Stewart ((round_cnt - cubicd->css_entered_at_round) >= hystart_css_rounds)) { 711a9696510SRandall Stewart /* Enter CA */ 712a9696510SRandall Stewart if (ccv->flags & CCF_HYSTART_CAN_SH_CWND) { 713a9696510SRandall Stewart /* 714a9696510SRandall Stewart * We engage more than snd_ssthresh, engage 715a9696510SRandall Stewart * the brakes!! Though we will stay in SS to 716a9696510SRandall Stewart * creep back up again, so lets leave CSS active 717a9696510SRandall Stewart * and give us hystart_css_rounds more rounds. 718a9696510SRandall Stewart */ 719a9696510SRandall Stewart if (ccv->flags & CCF_HYSTART_CONS_SSTH) { 720a9696510SRandall Stewart CCV(ccv, snd_ssthresh) = ((cubicd->css_lowrtt_fas + cubicd->css_fas_at_css_entry) / 2); 721a9696510SRandall Stewart } else { 722a9696510SRandall Stewart CCV(ccv, snd_ssthresh) = cubicd->css_lowrtt_fas; 723a9696510SRandall Stewart } 724a9696510SRandall Stewart CCV(ccv, snd_cwnd) = cubicd->css_fas_at_css_entry; 725a9696510SRandall Stewart cubicd->css_entered_at_round = round_cnt; 726a9696510SRandall Stewart } else { 727a9696510SRandall Stewart CCV(ccv, snd_ssthresh) = CCV(ccv, snd_cwnd); 728a9696510SRandall Stewart /* Turn off the CSS flag */ 729a9696510SRandall Stewart cubicd->flags &= ~CUBICFLAG_HYSTART_IN_CSS; 730a9696510SRandall Stewart /* Disable use of CSS in the future except long idle */ 731a9696510SRandall Stewart cubicd->flags &= ~CUBICFLAG_HYSTART_ENABLED; 732a9696510SRandall Stewart } 733a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubicd, 6, CCV(ccv, snd_ssthresh)); 734a9696510SRandall Stewart } 735a9696510SRandall Stewart if (cubicd->flags & CUBICFLAG_HYSTART_ENABLED) 736a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubicd, 4, round_cnt); 737a9696510SRandall Stewart } 738a9696510SRandall Stewart 73967fef78bSLawrence Stewart DECLARE_CC_MODULE(cubic, &cubic_cc_algo); 740b8d60729SRandall Stewart MODULE_VERSION(cubic, 2); 741