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