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 /* 40 * An implementation of the CUBIC congestion control algorithm for FreeBSD, 41 * based on the Internet RFC9438 by Xu, Ha, Rhee, Goel, and Eggert. 42 * Originally released as part of the NewTCP research project at Swinburne 43 * University of Technology's Centre for Advanced Internet Architectures, 44 * Melbourne, Australia, which was made possible in part by a grant from the 45 * Cisco University Research Program Fund at Community Foundation Silicon 46 * Valley. More details are available at: 47 * http://caia.swin.edu.au/urp/newtcp/ 48 */ 49 50 #include <sys/param.h> 51 #include <sys/kernel.h> 52 #include <sys/limits.h> 53 #include <sys/malloc.h> 54 #include <sys/module.h> 55 #include <sys/socket.h> 56 #include <sys/socketvar.h> 57 #include <sys/sysctl.h> 58 #include <sys/systm.h> 59 60 #include <net/vnet.h> 61 62 #include <net/route.h> 63 #include <net/route/nhop.h> 64 65 #include <netinet/in_pcb.h> 66 #include <netinet/tcp.h> 67 #include <netinet/tcp_seq.h> 68 #include <netinet/tcp_timer.h> 69 #include <netinet/tcp_var.h> 70 #include <netinet/tcp_log_buf.h> 71 #include <netinet/tcp_hpts.h> 72 #include <netinet/cc/cc.h> 73 #include <netinet/cc/cc_cubic.h> 74 #include <netinet/cc/cc_module.h> 75 76 static void cubic_ack_received(struct cc_var *ccv, ccsignal_t type); 77 static void cubic_cb_destroy(struct cc_var *ccv); 78 static int cubic_cb_init(struct cc_var *ccv, void *ptr); 79 static void cubic_cong_signal(struct cc_var *ccv, ccsignal_t type); 80 static void cubic_conn_init(struct cc_var *ccv); 81 static int cubic_mod_init(void); 82 static void cubic_post_recovery(struct cc_var *ccv); 83 static void cubic_record_rtt(struct cc_var *ccv); 84 static uint32_t cubic_get_ssthresh(struct cc_var *ccv, uint32_t maxseg); 85 static void cubic_after_idle(struct cc_var *ccv); 86 static size_t cubic_data_sz(void); 87 static void cubic_newround(struct cc_var *ccv, uint32_t round_cnt); 88 static void cubic_rttsample(struct cc_var *ccv, uint32_t usec_rtt, 89 uint32_t rxtcnt, uint32_t fas); 90 91 struct cc_algo cubic_cc_algo = { 92 .name = "cubic", 93 .ack_received = cubic_ack_received, 94 .cb_destroy = cubic_cb_destroy, 95 .cb_init = cubic_cb_init, 96 .cong_signal = cubic_cong_signal, 97 .conn_init = cubic_conn_init, 98 .mod_init = cubic_mod_init, 99 .post_recovery = cubic_post_recovery, 100 .after_idle = cubic_after_idle, 101 .cc_data_sz = cubic_data_sz, 102 .rttsample = cubic_rttsample, 103 .newround = cubic_newround 104 }; 105 106 static void 107 cubic_log_hystart_event(struct cc_var *ccv, struct cubic *cubicd, uint8_t mod, uint32_t flex1) 108 { 109 /* 110 * Types of logs (mod value) 111 * 1 - rtt_thresh in flex1, checking to see if RTT is to great. 112 * 2 - rtt is too great, rtt_thresh in flex1. 113 * 3 - CSS is active incr in flex1 114 * 4 - A new round is beginning flex1 is round count 115 * 5 - A new RTT measurement flex1 is the new measurement. 116 * 6 - We enter CA ssthresh is also in flex1. 117 * 7 - Socket option to change hystart executed opt.val in flex1. 118 * 8 - Back out of CSS into SS, flex1 is the css_baseline_minrtt 119 * 9 - We enter CA, via an ECN mark. 120 * 10 - We enter CA, via a loss. 121 * 11 - We have slipped out of SS into CA via cwnd growth. 122 * 12 - After idle has re-enabled hystart++ 123 */ 124 struct tcpcb *tp; 125 126 if (hystart_bblogs == 0) 127 return; 128 tp = ccv->tp; 129 if (tcp_bblogging_on(tp)) { 130 union tcp_log_stackspecific log; 131 struct timeval tv; 132 133 memset(&log, 0, sizeof(log)); 134 log.u_bbr.flex1 = flex1; 135 log.u_bbr.flex2 = cubicd->css_current_round_minrtt; 136 log.u_bbr.flex3 = cubicd->css_lastround_minrtt; 137 log.u_bbr.flex4 = cubicd->css_rttsample_count; 138 log.u_bbr.flex5 = cubicd->css_entered_at_round; 139 log.u_bbr.flex6 = cubicd->css_baseline_minrtt; 140 /* We only need bottom 16 bits of flags */ 141 log.u_bbr.flex7 = cubicd->flags & 0x0000ffff; 142 log.u_bbr.flex8 = mod; 143 log.u_bbr.epoch = cubicd->css_current_round; 144 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 145 log.u_bbr.lt_epoch = cubicd->css_fas_at_css_entry; 146 log.u_bbr.pkts_out = cubicd->css_last_fas; 147 log.u_bbr.delivered = cubicd->css_lowrtt_fas; 148 log.u_bbr.pkt_epoch = ccv->flags; 149 TCP_LOG_EVENTP(tp, NULL, 150 &tptosocket(tp)->so_rcv, 151 &tptosocket(tp)->so_snd, 152 TCP_HYSTART, 0, 153 0, &log, false, &tv); 154 } 155 } 156 157 static void 158 cubic_does_slow_start(struct cc_var *ccv, struct cubic *cubicd) 159 { 160 /* 161 * In slow-start with ABC enabled and no RTO in sight? 162 * (Must not use abc_l_var > 1 if slow starting after 163 * an RTO. On RTO, snd_nxt = snd_una, so the 164 * snd_nxt == snd_max check is sufficient to 165 * handle this). 166 * 167 * XXXLAS: Find a way to signal SS after RTO that 168 * doesn't rely on tcpcb vars. 169 */ 170 u_int cw = CCV(ccv, snd_cwnd); 171 uint32_t mss = tcp_fixed_maxseg(ccv->tp); 172 u_int incr = mss; 173 uint16_t abc_val; 174 175 cubicd->flags |= CUBICFLAG_IN_SLOWSTART; 176 if (ccv->flags & CCF_USE_LOCAL_ABC) 177 abc_val = ccv->labc; 178 else 179 abc_val = V_tcp_abc_l_var; 180 if ((ccv->flags & CCF_HYSTART_ALLOWED) && 181 (cubicd->flags & CUBICFLAG_HYSTART_ENABLED) && 182 ((cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) == 0)) { 183 /* 184 * Hystart is allowed and still enabled and we are not yet 185 * in CSS. Lets check to see if we can make a decision on 186 * if we need to go into CSS. 187 */ 188 if ((cubicd->css_rttsample_count >= hystart_n_rttsamples) && 189 (cubicd->css_current_round_minrtt != 0xffffffff) && 190 (cubicd->css_lastround_minrtt != 0xffffffff)) { 191 uint32_t rtt_thresh; 192 193 /* Clamp (minrtt_thresh, lastround/8, maxrtt_thresh) */ 194 rtt_thresh = (cubicd->css_lastround_minrtt >> 3); 195 if (rtt_thresh < hystart_minrtt_thresh) 196 rtt_thresh = hystart_minrtt_thresh; 197 if (rtt_thresh > hystart_maxrtt_thresh) 198 rtt_thresh = hystart_maxrtt_thresh; 199 cubic_log_hystart_event(ccv, cubicd, 1, rtt_thresh); 200 201 if (cubicd->css_current_round_minrtt >= (cubicd->css_lastround_minrtt + rtt_thresh)) { 202 /* Enter CSS */ 203 cubicd->flags |= CUBICFLAG_HYSTART_IN_CSS; 204 cubicd->css_fas_at_css_entry = cubicd->css_lowrtt_fas; 205 /* 206 * The draft (v4) calls for us to set baseline to css_current_round_min 207 * but that can cause an oscillation. We probably shoudl be using 208 * css_lastround_minrtt, but the authors insist that will cause 209 * issues on exiting early. We will leave the draft version for now 210 * but I suspect this is incorrect. 211 */ 212 cubicd->css_baseline_minrtt = cubicd->css_current_round_minrtt; 213 cubicd->css_entered_at_round = cubicd->css_current_round; 214 cubic_log_hystart_event(ccv, cubicd, 2, rtt_thresh); 215 } 216 } 217 } 218 if (CCV(ccv, snd_nxt) == CCV(ccv, snd_max)) 219 incr = min(ccv->bytes_this_ack, 220 ccv->nsegs * abc_val * mss); 221 else 222 incr = min(ccv->bytes_this_ack, mss); 223 224 /* Only if Hystart is enabled will the flag get set */ 225 if (cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) { 226 incr /= hystart_css_growth_div; 227 cubic_log_hystart_event(ccv, cubicd, 3, incr); 228 } 229 /* ABC is on by default, so incr equals 0 frequently. */ 230 if (incr > 0) 231 CCV(ccv, snd_cwnd) = min((cw + incr), 232 TCP_MAXWIN << CCV(ccv, snd_scale)); 233 } 234 235 static void 236 cubic_ack_received(struct cc_var *ccv, ccsignal_t type) 237 { 238 struct cubic *cubic_data; 239 uint32_t W_est, W_cubic, cwin, target, incr; 240 int usecs_since_epoch; 241 uint32_t mss = tcp_fixed_maxseg(ccv->tp); 242 243 cwin = CCV(ccv, snd_cwnd); 244 cubic_data = ccv->cc_data; 245 cubic_record_rtt(ccv); 246 247 /* 248 * For a regular ACK and we're not in cong/fast recovery and 249 * we're cwnd limited, always recalculate cwnd. 250 */ 251 if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) && 252 (ccv->flags & CCF_CWND_LIMITED)) { 253 /* Use the logic in NewReno ack_received() for slow start. */ 254 if (cwin <= CCV(ccv, snd_ssthresh) || 255 cubic_data->min_rtt_usecs == TCPTV_SRTTBASE) { 256 cubic_does_slow_start(ccv, cubic_data); 257 } else { 258 if (cubic_data->flags & CUBICFLAG_HYSTART_IN_CSS) { 259 /* 260 * We have slipped into CA with 261 * CSS active. Deactivate all. 262 */ 263 /* Turn off the CSS flag */ 264 cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS; 265 /* Disable use of CSS in the future except long idle */ 266 cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED; 267 cubic_log_hystart_event(ccv, cubic_data, 11, CCV(ccv, snd_ssthresh)); 268 } 269 if (cubic_data->flags & (CUBICFLAG_IN_SLOWSTART | 270 CUBICFLAG_CONG_EVENT | 271 CUBICFLAG_IN_APPLIMIT)) { 272 /* 273 * At the beginning of the current congestion 274 * avoidance stage, The epoch variables 275 * (t_epoch, cwnd_epoch, K) are updated in the 276 * following three cases: 277 * 1) just exited the slow start 278 * 2) after a congestion event 279 * 3) application-limited 280 */ 281 cubic_data->t_epoch = ticks; 282 cubic_data->cwnd_epoch = cwin; 283 cubic_data->K = cubic_k(cubic_data->W_max / mss, 284 cubic_data->cwnd_epoch / mss); 285 cubic_data->flags &= ~(CUBICFLAG_IN_SLOWSTART | 286 CUBICFLAG_CONG_EVENT | 287 CUBICFLAG_IN_APPLIMIT); 288 289 if (cubic_data->flags & CUBICFLAG_RTO_EVENT) { 290 /* RFC9438 Section 4.8: Timeout */ 291 cubic_data->flags &= ~CUBICFLAG_RTO_EVENT; 292 cubic_data->W_max = cwin; 293 cubic_data->K = 0; 294 } 295 } 296 usecs_since_epoch = (ticks - cubic_data->t_epoch) * tick; 297 if (usecs_since_epoch < 0) { 298 /* 299 * dragging t_epoch along 300 */ 301 usecs_since_epoch = INT_MAX; 302 cubic_data->t_epoch = ticks - INT_MAX; 303 } 304 W_est = tf_cwnd(ccv); 305 /* 306 * The mean RTT is used to best reflect the equations. 307 */ 308 W_cubic = cubic_cwnd(usecs_since_epoch + 309 cubic_data->mean_rtt_usecs, 310 cubic_data->W_max, 311 mss, 312 cubic_data->K); 313 314 if (W_cubic < W_est) { 315 /* RFC9438 Section 4.3: Reno-friendly region */ 316 CCV(ccv, snd_cwnd) = W_est; 317 cubic_data->flags |= CUBICFLAG_IN_TF; 318 } else { 319 /* 320 * RFC9438 Section 4.4 or 4.5: 321 * Concave or Convex Region 322 */ 323 if (W_cubic < cwin) { 324 target = cwin; 325 } else if (W_cubic > ((cwin * 3) >> 1)) { 326 target = (cwin * 3) >> 1; 327 } else { 328 target = W_cubic; 329 } 330 incr = (((target - cwin) << CUBIC_SHIFT) / 331 cwin * mss) >> CUBIC_SHIFT; 332 CCV(ccv, snd_cwnd) = cwin + incr; 333 } 334 } 335 } else if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) && 336 !(ccv->flags & CCF_CWND_LIMITED)) { 337 cubic_data->flags |= CUBICFLAG_IN_APPLIMIT; 338 } 339 } 340 341 /* 342 * This is a CUBIC specific implementation of after_idle. 343 * - Reset cwnd by calling New Reno implementation of after_idle. 344 * - Reset t_epoch. 345 */ 346 static void 347 cubic_after_idle(struct cc_var *ccv) 348 { 349 struct cubic *cubic_data = ccv->cc_data; 350 uint32_t mss = tcp_fixed_maxseg(ccv->tp); 351 352 cubic_data->W_max = ulmax(cubic_data->W_max, CCV(ccv, snd_cwnd)); 353 cubic_data->K = cubic_k(cubic_data->W_max / mss, cubic_data->cwnd_epoch / mss); 354 if ((cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) == 0) { 355 /* 356 * Re-enable hystart if we have been idle. 357 */ 358 cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS; 359 cubic_data->flags |= CUBICFLAG_HYSTART_ENABLED; 360 cubic_log_hystart_event(ccv, cubic_data, 12, CCV(ccv, snd_ssthresh)); 361 } 362 newreno_cc_after_idle(ccv); 363 cubic_data->t_epoch = ticks; 364 } 365 366 static void 367 cubic_cb_destroy(struct cc_var *ccv) 368 { 369 free(ccv->cc_data, M_CC_MEM); 370 } 371 372 static size_t 373 cubic_data_sz(void) 374 { 375 return (sizeof(struct cubic)); 376 } 377 378 static int 379 cubic_cb_init(struct cc_var *ccv, void *ptr) 380 { 381 struct cubic *cubic_data; 382 383 INP_WLOCK_ASSERT(tptoinpcb(ccv->tp)); 384 if (ptr == NULL) { 385 cubic_data = malloc(sizeof(struct cubic), M_CC_MEM, M_NOWAIT|M_ZERO); 386 if (cubic_data == NULL) 387 return (ENOMEM); 388 } else 389 cubic_data = ptr; 390 391 /* Init some key variables with sensible defaults. */ 392 cubic_data->t_epoch = 0; 393 cubic_data->cwnd_epoch = 0; 394 cubic_data->K = 0; 395 cubic_data->min_rtt_usecs = TCPTV_SRTTBASE; 396 cubic_data->mean_rtt_usecs = 1; 397 398 ccv->cc_data = cubic_data; 399 cubic_data->flags = CUBICFLAG_HYSTART_ENABLED; 400 /* At init set both to infinity */ 401 cubic_data->css_lastround_minrtt = 0xffffffff; 402 cubic_data->css_current_round_minrtt = 0xffffffff; 403 cubic_data->css_current_round = 0; 404 cubic_data->css_baseline_minrtt = 0xffffffff; 405 cubic_data->css_rttsample_count = 0; 406 cubic_data->css_entered_at_round = 0; 407 cubic_data->css_fas_at_css_entry = 0; 408 cubic_data->css_lowrtt_fas = 0; 409 cubic_data->css_last_fas = 0; 410 411 return (0); 412 } 413 414 /* 415 * Perform any necessary tasks before we enter congestion recovery. 416 */ 417 static void 418 cubic_cong_signal(struct cc_var *ccv, ccsignal_t type) 419 { 420 struct cubic *cubic_data; 421 uint32_t mss, pipe, ssthresh; 422 423 cubic_data = ccv->cc_data; 424 mss = tcp_fixed_maxseg(ccv->tp); 425 426 switch (type) { 427 case CC_NDUPACK: 428 if (cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) { 429 /* Make sure the flags are all off we had a loss */ 430 cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED; 431 cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS; 432 cubic_log_hystart_event(ccv, cubic_data, 10, CCV(ccv, snd_ssthresh)); 433 } 434 if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) { 435 if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) { 436 ssthresh = cubic_get_ssthresh(ccv, mss); 437 CCV(ccv, snd_ssthresh) = max(ssthresh, 2 * mss); 438 /* 439 * The congestion flag will recalculate K at the 440 * beginning of the congestion avoidance stage. 441 */ 442 cubic_data->flags |= CUBICFLAG_CONG_EVENT; 443 } 444 ENTER_RECOVERY(CCV(ccv, t_flags)); 445 } 446 break; 447 448 case CC_ECN: 449 if (cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) { 450 /* Make sure the flags are all off we had a loss */ 451 cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED; 452 cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS; 453 cubic_log_hystart_event(ccv, cubic_data, 9, CCV(ccv, snd_ssthresh)); 454 } 455 if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) { 456 ssthresh = cubic_get_ssthresh(ccv, mss); 457 CCV(ccv, snd_ssthresh) = max(ssthresh, 2 * mss); 458 CCV(ccv, snd_cwnd) = max(ssthresh, mss); 459 /* 460 * The congestion flag will recalculate K at the 461 * beginning of the congestion avoidance stage. 462 */ 463 cubic_data->flags |= CUBICFLAG_CONG_EVENT; 464 ENTER_CONGRECOVERY(CCV(ccv, t_flags)); 465 } 466 break; 467 468 case CC_RTO: 469 /* RFC9438 Section 4.8: Timeout */ 470 if (CCV(ccv, t_rxtshift) == 1) { 471 /* 472 * Remember the state only for the first RTO event. This 473 * will help us restore the state to the values seen 474 * at the most recent congestion avoidance stage before 475 * the current RTO event. 476 */ 477 cubic_data->undo_t_epoch = cubic_data->t_epoch; 478 cubic_data->undo_cwnd_epoch = cubic_data->cwnd_epoch; 479 cubic_data->undo_W_max = cubic_data->W_max; 480 cubic_data->undo_K = cubic_data->K; 481 pipe = tcp_compute_pipe(ccv->tp); 482 CCV(ccv, snd_ssthresh) = max(2, 483 (((uint64_t)min(CCV(ccv, snd_wnd), pipe) * 484 CUBIC_BETA) >> CUBIC_SHIFT) / mss) * mss; 485 } 486 /* 487 * The RTO flag will recalculate K at the 488 * beginning of the congestion avoidance stage. 489 */ 490 cubic_data->flags |= CUBICFLAG_RTO_EVENT; 491 CCV(ccv, snd_cwnd) = mss; 492 break; 493 494 case CC_RTO_ERR: 495 cubic_data->flags &= ~CUBICFLAG_RTO_EVENT; 496 cubic_data->K = cubic_data->undo_K; 497 cubic_data->W_max = cubic_data->undo_W_max; 498 cubic_data->cwnd_epoch = cubic_data->undo_cwnd_epoch; 499 cubic_data->t_epoch = cubic_data->undo_t_epoch; 500 break; 501 default: 502 break; 503 } 504 } 505 506 static void 507 cubic_conn_init(struct cc_var *ccv) 508 { 509 struct cubic *cubic_data; 510 511 cubic_data = ccv->cc_data; 512 513 /* 514 * Ensure we have a sane initial value for W_max recorded. Without 515 * this here bad things happen when entries from the TCP hostcache 516 * get used. 517 */ 518 cubic_data->W_max = UINT_MAX; 519 } 520 521 static int 522 cubic_mod_init(void) 523 { 524 return (0); 525 } 526 527 /* 528 * Perform any necessary tasks before we exit congestion recovery. 529 */ 530 static void 531 cubic_post_recovery(struct cc_var *ccv) 532 { 533 struct cubic *cubic_data; 534 int pipe; 535 uint32_t mss = tcp_fixed_maxseg(ccv->tp); 536 537 cubic_data = ccv->cc_data; 538 pipe = 0; 539 540 if (IN_FASTRECOVERY(CCV(ccv, t_flags))) { 541 /* 542 * If inflight data is less than ssthresh, set cwnd 543 * conservatively to avoid a burst of data, as suggested in 544 * the NewReno RFC. Otherwise, use the CUBIC method. 545 */ 546 pipe = tcp_compute_pipe(ccv->tp); 547 if (pipe < CCV(ccv, snd_ssthresh)) 548 /* 549 * Ensure that cwnd does not collapse to 1 MSS under 550 * adverse conditions. Implements RFC6582 551 */ 552 CCV(ccv, snd_cwnd) = max(pipe, mss) + mss; 553 else 554 /* Update cwnd based on beta and adjusted W_max. */ 555 CCV(ccv, snd_cwnd) = max(((uint64_t)cubic_data->W_max * 556 CUBIC_BETA) >> CUBIC_SHIFT, 557 2 * mss); 558 } 559 560 /* Calculate the average RTT between congestion epochs. */ 561 if (cubic_data->epoch_ack_count > 0 && 562 cubic_data->sum_rtt_usecs >= cubic_data->epoch_ack_count) { 563 cubic_data->mean_rtt_usecs = (int)(cubic_data->sum_rtt_usecs / 564 cubic_data->epoch_ack_count); 565 } 566 567 cubic_data->epoch_ack_count = 0; 568 cubic_data->sum_rtt_usecs = 0; 569 } 570 571 /* 572 * Record the min RTT and sum samples for the epoch average RTT calculation. 573 */ 574 static void 575 cubic_record_rtt(struct cc_var *ccv) 576 { 577 struct cubic *cubic_data; 578 uint32_t t_srtt_usecs; 579 580 /* Ignore srtt until a min number of samples have been taken. */ 581 if (CCV(ccv, t_rttupdated) >= CUBIC_MIN_RTT_SAMPLES) { 582 cubic_data = ccv->cc_data; 583 t_srtt_usecs = tcp_get_srtt(ccv->tp, 584 TCP_TMR_GRANULARITY_USEC); 585 /* 586 * Record the current SRTT as our minrtt if it's the smallest 587 * we've seen or minrtt is currently equal to its initialised 588 * value. 589 * 590 * XXXLAS: Should there be some hysteresis for minrtt? 591 */ 592 if ((t_srtt_usecs < cubic_data->min_rtt_usecs || 593 cubic_data->min_rtt_usecs == TCPTV_SRTTBASE)) { 594 /* A minimal rtt is a single unshifted tick of a ticks 595 * timer. */ 596 cubic_data->min_rtt_usecs = max(tick >> TCP_RTT_SHIFT, 597 t_srtt_usecs); 598 599 /* 600 * If the connection is within its first congestion 601 * epoch, ensure we prime mean_rtt_usecs with a 602 * reasonable value until the epoch average RTT is 603 * calculated in cubic_post_recovery(). 604 */ 605 if (cubic_data->min_rtt_usecs > 606 cubic_data->mean_rtt_usecs) 607 cubic_data->mean_rtt_usecs = 608 cubic_data->min_rtt_usecs; 609 } 610 611 /* Sum samples for epoch average RTT calculation. */ 612 cubic_data->sum_rtt_usecs += t_srtt_usecs; 613 cubic_data->epoch_ack_count++; 614 } 615 } 616 617 /* 618 * Return the new value for ssthresh in the event of a congestion. 619 */ 620 static uint32_t 621 cubic_get_ssthresh(struct cc_var *ccv, uint32_t maxseg) 622 { 623 struct cubic *cubic_data; 624 uint32_t cwnd, pipe; 625 626 cubic_data = ccv->cc_data; 627 cwnd = CCV(ccv, snd_cwnd); 628 629 /* RFC9438 Section 4.7: Fast convergence */ 630 if (cwnd < cubic_data->W_max) { 631 cwnd = ((uint64_t)cwnd * CUBIC_FC_FACTOR) >> CUBIC_SHIFT; 632 } 633 cubic_data->W_max = cwnd; 634 635 if (cubic_data->flags & CUBICFLAG_IN_TF) { 636 /* If in the TCP friendly region, follow what newreno does. */ 637 return (newreno_cc_cwnd_on_multiplicative_decrease(ccv, maxseg)); 638 639 } else { 640 /* 641 * RFC9438 Section 4.6: Multiplicative Decrease 642 * Outside the TCP friendly region, set ssthresh to the size of 643 * inflight_size * beta. 644 */ 645 pipe = tcp_compute_pipe(ccv->tp); 646 return ((pipe * CUBIC_BETA) >> CUBIC_SHIFT); 647 } 648 } 649 650 static void 651 cubic_rttsample(struct cc_var *ccv, uint32_t usec_rtt, uint32_t rxtcnt, uint32_t fas) 652 { 653 struct cubic *cubicd; 654 655 cubicd = ccv->cc_data; 656 if (rxtcnt > 1) { 657 /* 658 * Only look at RTT's that are non-ambiguous. 659 */ 660 return; 661 } 662 cubicd->css_rttsample_count++; 663 cubicd->css_last_fas = fas; 664 if (cubicd->css_current_round_minrtt > usec_rtt) { 665 cubicd->css_current_round_minrtt = usec_rtt; 666 cubicd->css_lowrtt_fas = cubicd->css_last_fas; 667 } 668 if ((cubicd->css_rttsample_count >= hystart_n_rttsamples) && 669 (cubicd->css_current_round_minrtt != 0xffffffff) && 670 (cubicd->css_current_round_minrtt < cubicd->css_baseline_minrtt) && 671 (cubicd->css_lastround_minrtt != 0xffffffff)) { 672 /* 673 * We were in CSS and the RTT is now less, we 674 * entered CSS erroneously. 675 */ 676 cubicd->flags &= ~CUBICFLAG_HYSTART_IN_CSS; 677 cubic_log_hystart_event(ccv, cubicd, 8, cubicd->css_baseline_minrtt); 678 cubicd->css_baseline_minrtt = 0xffffffff; 679 } 680 if (cubicd->flags & CUBICFLAG_HYSTART_ENABLED) 681 cubic_log_hystart_event(ccv, cubicd, 5, usec_rtt); 682 } 683 684 static void 685 cubic_newround(struct cc_var *ccv, uint32_t round_cnt) 686 { 687 struct cubic *cubicd; 688 689 cubicd = ccv->cc_data; 690 /* We have entered a new round */ 691 cubicd->css_lastround_minrtt = cubicd->css_current_round_minrtt; 692 cubicd->css_current_round_minrtt = 0xffffffff; 693 cubicd->css_rttsample_count = 0; 694 cubicd->css_current_round = round_cnt; 695 if ((cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) && 696 ((round_cnt - cubicd->css_entered_at_round) >= hystart_css_rounds)) { 697 /* Enter CA */ 698 if (ccv->flags & CCF_HYSTART_CAN_SH_CWND) { 699 /* 700 * We engage more than snd_ssthresh, engage 701 * the brakes!! Though we will stay in SS to 702 * creep back up again, so lets leave CSS active 703 * and give us hystart_css_rounds more rounds. 704 */ 705 if (ccv->flags & CCF_HYSTART_CONS_SSTH) { 706 CCV(ccv, snd_ssthresh) = ((cubicd->css_lowrtt_fas + cubicd->css_fas_at_css_entry) / 2); 707 } else { 708 CCV(ccv, snd_ssthresh) = cubicd->css_lowrtt_fas; 709 } 710 CCV(ccv, snd_cwnd) = cubicd->css_fas_at_css_entry; 711 cubicd->css_entered_at_round = round_cnt; 712 } else { 713 CCV(ccv, snd_ssthresh) = CCV(ccv, snd_cwnd); 714 /* Turn off the CSS flag */ 715 cubicd->flags &= ~CUBICFLAG_HYSTART_IN_CSS; 716 /* Disable use of CSS in the future except long idle */ 717 cubicd->flags &= ~CUBICFLAG_HYSTART_ENABLED; 718 } 719 cubic_log_hystart_event(ccv, cubicd, 6, CCV(ccv, snd_ssthresh)); 720 } 721 if (cubicd->flags & CUBICFLAG_HYSTART_ENABLED) 722 cubic_log_hystart_event(ccv, cubicd, 4, round_cnt); 723 } 724 725 DECLARE_CC_MODULE(cubic, &cubic_cc_algo); 726 MODULE_VERSION(cubic, 2); 727