1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 <netinet/tcp.h> 66 #include <netinet/tcp_seq.h> 67 #include <netinet/tcp_timer.h> 68 #include <netinet/tcp_var.h> 69 #include <netinet/cc/cc.h> 70 #include <netinet/cc/cc_cubic.h> 71 #include <netinet/cc/cc_module.h> 72 73 static void cubic_ack_received(struct cc_var *ccv, uint16_t type); 74 static void cubic_cb_destroy(struct cc_var *ccv); 75 static int cubic_cb_init(struct cc_var *ccv); 76 static void cubic_cong_signal(struct cc_var *ccv, uint32_t type); 77 static void cubic_conn_init(struct cc_var *ccv); 78 static int cubic_mod_init(void); 79 static void cubic_post_recovery(struct cc_var *ccv); 80 static void cubic_record_rtt(struct cc_var *ccv); 81 static void cubic_ssthresh_update(struct cc_var *ccv); 82 static void cubic_after_idle(struct cc_var *ccv); 83 84 struct cubic { 85 /* Cubic K in fixed point form with CUBIC_SHIFT worth of precision. */ 86 int64_t K; 87 /* Sum of RTT samples across an epoch in ticks. */ 88 int64_t sum_rtt_ticks; 89 /* cwnd at the most recent congestion event. */ 90 unsigned long max_cwnd; 91 /* cwnd at the previous congestion event. */ 92 unsigned long prev_max_cwnd; 93 /* various flags */ 94 uint32_t flags; 95 #define CUBICFLAG_CONG_EVENT 0x00000001 /* congestion experienced */ 96 #define CUBICFLAG_IN_SLOWSTART 0x00000002 /* in slow start */ 97 #define CUBICFLAG_IN_APPLIMIT 0x00000004 /* application limited */ 98 /* Minimum observed rtt in ticks. */ 99 int min_rtt_ticks; 100 /* Mean observed rtt between congestion epochs. */ 101 int mean_rtt_ticks; 102 /* ACKs since last congestion event. */ 103 int epoch_ack_count; 104 /* Time of last congestion event in ticks. */ 105 int t_last_cong; 106 }; 107 108 static MALLOC_DEFINE(M_CUBIC, "cubic data", 109 "Per connection data required for the CUBIC congestion control algorithm"); 110 111 struct cc_algo cubic_cc_algo = { 112 .name = "cubic", 113 .ack_received = cubic_ack_received, 114 .cb_destroy = cubic_cb_destroy, 115 .cb_init = cubic_cb_init, 116 .cong_signal = cubic_cong_signal, 117 .conn_init = cubic_conn_init, 118 .mod_init = cubic_mod_init, 119 .post_recovery = cubic_post_recovery, 120 .after_idle = cubic_after_idle, 121 }; 122 123 static void 124 cubic_ack_received(struct cc_var *ccv, uint16_t type) 125 { 126 struct cubic *cubic_data; 127 unsigned long w_tf, w_cubic_next; 128 int ticks_since_cong; 129 130 cubic_data = ccv->cc_data; 131 cubic_record_rtt(ccv); 132 133 /* 134 * Regular ACK and we're not in cong/fast recovery and we're cwnd 135 * limited and we're either not doing ABC or are slow starting or are 136 * doing ABC and we've sent a cwnd's worth of bytes. 137 */ 138 if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) && 139 (ccv->flags & CCF_CWND_LIMITED) && (!V_tcp_do_rfc3465 || 140 CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh) || 141 (V_tcp_do_rfc3465 && ccv->flags & CCF_ABC_SENTAWND))) { 142 /* Use the logic in NewReno ack_received() for slow start. */ 143 if (CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh) || 144 cubic_data->min_rtt_ticks == TCPTV_SRTTBASE) { 145 cubic_data->flags |= CUBICFLAG_IN_SLOWSTART; 146 newreno_cc_algo.ack_received(ccv, type); 147 } else { 148 if ((ticks_since_cong = 149 ticks - cubic_data->t_last_cong) < 0) { 150 /* 151 * dragging t_last_cong along 152 */ 153 ticks_since_cong = INT_MAX; 154 cubic_data->t_last_cong = ticks - INT_MAX; 155 } 156 157 if (cubic_data->flags & (CUBICFLAG_IN_SLOWSTART | 158 CUBICFLAG_IN_APPLIMIT)) { 159 cubic_data->flags &= ~(CUBICFLAG_IN_SLOWSTART | 160 CUBICFLAG_IN_APPLIMIT); 161 cubic_data->t_last_cong = ticks; 162 cubic_data->K = 0; 163 } 164 /* 165 * The mean RTT is used to best reflect the equations in 166 * the I-D. Using min_rtt in the tf_cwnd calculation 167 * causes w_tf to grow much faster than it should if the 168 * RTT is dominated by network buffering rather than 169 * propagation delay. 170 */ 171 w_tf = tf_cwnd(ticks_since_cong, 172 cubic_data->mean_rtt_ticks, cubic_data->max_cwnd, 173 CCV(ccv, t_maxseg)); 174 175 w_cubic_next = cubic_cwnd(ticks_since_cong + 176 cubic_data->mean_rtt_ticks, cubic_data->max_cwnd, 177 CCV(ccv, t_maxseg), cubic_data->K); 178 179 ccv->flags &= ~CCF_ABC_SENTAWND; 180 181 if (w_cubic_next < w_tf) { 182 /* 183 * TCP-friendly region, follow tf 184 * cwnd growth. 185 */ 186 if (CCV(ccv, snd_cwnd) < w_tf) 187 CCV(ccv, snd_cwnd) = ulmin(w_tf, INT_MAX); 188 } 189 190 else if (CCV(ccv, snd_cwnd) < w_cubic_next) { 191 /* 192 * Concave or convex region, follow CUBIC 193 * cwnd growth. 194 */ 195 if (V_tcp_do_rfc3465) 196 CCV(ccv, snd_cwnd) = ulmin(w_cubic_next, 197 INT_MAX); 198 else 199 CCV(ccv, snd_cwnd) += ulmax(1, 200 ((ulmin(w_cubic_next, INT_MAX) - 201 CCV(ccv, snd_cwnd)) * 202 CCV(ccv, t_maxseg)) / 203 CCV(ccv, snd_cwnd)); 204 } 205 206 /* 207 * If we're not in slow start and we're probing for a 208 * new cwnd limit at the start of a connection 209 * (happens when hostcache has a relevant entry), 210 * keep updating our current estimate of the 211 * max_cwnd. 212 */ 213 if (((cubic_data->flags & CUBICFLAG_CONG_EVENT) == 0) && 214 cubic_data->max_cwnd < CCV(ccv, snd_cwnd)) { 215 cubic_data->max_cwnd = CCV(ccv, snd_cwnd); 216 cubic_data->K = cubic_k(cubic_data->max_cwnd / 217 CCV(ccv, t_maxseg)); 218 } 219 } 220 } else if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) && 221 !(ccv->flags & CCF_CWND_LIMITED)) { 222 cubic_data->flags |= CUBICFLAG_IN_APPLIMIT; 223 } 224 } 225 226 /* 227 * This is a Cubic specific implementation of after_idle. 228 * - Reset cwnd by calling New Reno implementation of after_idle. 229 * - Reset t_last_cong. 230 */ 231 static void 232 cubic_after_idle(struct cc_var *ccv) 233 { 234 struct cubic *cubic_data; 235 236 cubic_data = ccv->cc_data; 237 238 cubic_data->max_cwnd = ulmax(cubic_data->max_cwnd, CCV(ccv, snd_cwnd)); 239 cubic_data->K = cubic_k(cubic_data->max_cwnd / CCV(ccv, t_maxseg)); 240 241 newreno_cc_algo.after_idle(ccv); 242 cubic_data->t_last_cong = ticks; 243 } 244 245 246 static void 247 cubic_cb_destroy(struct cc_var *ccv) 248 { 249 free(ccv->cc_data, M_CUBIC); 250 } 251 252 static int 253 cubic_cb_init(struct cc_var *ccv) 254 { 255 struct cubic *cubic_data; 256 257 cubic_data = malloc(sizeof(struct cubic), M_CUBIC, M_NOWAIT|M_ZERO); 258 259 if (cubic_data == NULL) 260 return (ENOMEM); 261 262 /* Init some key variables with sensible defaults. */ 263 cubic_data->t_last_cong = ticks; 264 cubic_data->min_rtt_ticks = TCPTV_SRTTBASE; 265 cubic_data->mean_rtt_ticks = 1; 266 267 ccv->cc_data = cubic_data; 268 269 return (0); 270 } 271 272 /* 273 * Perform any necessary tasks before we enter congestion recovery. 274 */ 275 static void 276 cubic_cong_signal(struct cc_var *ccv, uint32_t type) 277 { 278 struct cubic *cubic_data; 279 280 cubic_data = ccv->cc_data; 281 282 switch (type) { 283 case CC_NDUPACK: 284 if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) { 285 if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) { 286 cubic_ssthresh_update(ccv); 287 cubic_data->flags |= CUBICFLAG_CONG_EVENT; 288 cubic_data->prev_max_cwnd = cubic_data->max_cwnd; 289 cubic_data->max_cwnd = CCV(ccv, snd_cwnd); 290 cubic_data->K = cubic_k(cubic_data->max_cwnd / CCV(ccv, t_maxseg)); 291 } 292 ENTER_RECOVERY(CCV(ccv, t_flags)); 293 } 294 break; 295 296 case CC_ECN: 297 if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) { 298 cubic_ssthresh_update(ccv); 299 cubic_data->flags |= CUBICFLAG_CONG_EVENT; 300 cubic_data->prev_max_cwnd = cubic_data->max_cwnd; 301 cubic_data->max_cwnd = CCV(ccv, snd_cwnd); 302 cubic_data->t_last_cong = ticks; 303 cubic_data->K = cubic_k(cubic_data->max_cwnd / CCV(ccv, t_maxseg)); 304 CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh); 305 ENTER_CONGRECOVERY(CCV(ccv, t_flags)); 306 } 307 break; 308 309 case CC_RTO: 310 /* 311 * Grab the current time and record it so we know when the 312 * most recent congestion event was. Only record it when the 313 * timeout has fired more than once, as there is a reasonable 314 * chance the first one is a false alarm and may not indicate 315 * congestion. 316 */ 317 if (CCV(ccv, t_rxtshift) >= 2) { 318 cubic_data->flags |= CUBICFLAG_CONG_EVENT; 319 cubic_data->t_last_cong = ticks; 320 } 321 break; 322 } 323 } 324 325 static void 326 cubic_conn_init(struct cc_var *ccv) 327 { 328 struct cubic *cubic_data; 329 330 cubic_data = ccv->cc_data; 331 332 /* 333 * Ensure we have a sane initial value for max_cwnd recorded. Without 334 * this here bad things happen when entries from the TCP hostcache 335 * get used. 336 */ 337 cubic_data->max_cwnd = CCV(ccv, snd_cwnd); 338 } 339 340 static int 341 cubic_mod_init(void) 342 { 343 return (0); 344 } 345 346 /* 347 * Perform any necessary tasks before we exit congestion recovery. 348 */ 349 static void 350 cubic_post_recovery(struct cc_var *ccv) 351 { 352 struct cubic *cubic_data; 353 int pipe; 354 355 cubic_data = ccv->cc_data; 356 pipe = 0; 357 358 /* Fast convergence heuristic. */ 359 if (cubic_data->max_cwnd < cubic_data->prev_max_cwnd) 360 cubic_data->max_cwnd = (cubic_data->max_cwnd * CUBIC_FC_FACTOR) 361 >> CUBIC_SHIFT; 362 363 if (IN_FASTRECOVERY(CCV(ccv, t_flags))) { 364 /* 365 * If inflight data is less than ssthresh, set cwnd 366 * conservatively to avoid a burst of data, as suggested in 367 * the NewReno RFC. Otherwise, use the CUBIC method. 368 * 369 * XXXLAS: Find a way to do this without needing curack 370 */ 371 if (V_tcp_do_rfc6675_pipe) 372 pipe = tcp_compute_pipe(ccv->ccvc.tcp); 373 else 374 pipe = CCV(ccv, snd_max) - ccv->curack; 375 376 if (pipe < CCV(ccv, snd_ssthresh)) 377 /* 378 * Ensure that cwnd does not collapse to 1 MSS under 379 * adverse conditions. Implements RFC6582 380 */ 381 CCV(ccv, snd_cwnd) = max(pipe, CCV(ccv, t_maxseg)) + 382 CCV(ccv, t_maxseg); 383 else 384 /* Update cwnd based on beta and adjusted max_cwnd. */ 385 CCV(ccv, snd_cwnd) = max(((uint64_t)cubic_data->max_cwnd * 386 CUBIC_BETA) >> CUBIC_SHIFT, 387 2 * CCV(ccv, t_maxseg)); 388 } 389 cubic_data->t_last_cong = ticks; 390 391 /* Calculate the average RTT between congestion epochs. */ 392 if (cubic_data->epoch_ack_count > 0 && 393 cubic_data->sum_rtt_ticks >= cubic_data->epoch_ack_count) { 394 cubic_data->mean_rtt_ticks = (int)(cubic_data->sum_rtt_ticks / 395 cubic_data->epoch_ack_count); 396 } 397 398 cubic_data->epoch_ack_count = 0; 399 cubic_data->sum_rtt_ticks = 0; 400 cubic_data->K = cubic_k(cubic_data->max_cwnd / CCV(ccv, t_maxseg)); 401 } 402 403 /* 404 * Record the min RTT and sum samples for the epoch average RTT calculation. 405 */ 406 static void 407 cubic_record_rtt(struct cc_var *ccv) 408 { 409 struct cubic *cubic_data; 410 int t_srtt_ticks; 411 412 /* Ignore srtt until a min number of samples have been taken. */ 413 if (CCV(ccv, t_rttupdated) >= CUBIC_MIN_RTT_SAMPLES) { 414 cubic_data = ccv->cc_data; 415 t_srtt_ticks = CCV(ccv, t_srtt) / TCP_RTT_SCALE; 416 417 /* 418 * Record the current SRTT as our minrtt if it's the smallest 419 * we've seen or minrtt is currently equal to its initialised 420 * value. 421 * 422 * XXXLAS: Should there be some hysteresis for minrtt? 423 */ 424 if ((t_srtt_ticks < cubic_data->min_rtt_ticks || 425 cubic_data->min_rtt_ticks == TCPTV_SRTTBASE)) { 426 cubic_data->min_rtt_ticks = max(1, t_srtt_ticks); 427 428 /* 429 * If the connection is within its first congestion 430 * epoch, ensure we prime mean_rtt_ticks with a 431 * reasonable value until the epoch average RTT is 432 * calculated in cubic_post_recovery(). 433 */ 434 if (cubic_data->min_rtt_ticks > 435 cubic_data->mean_rtt_ticks) 436 cubic_data->mean_rtt_ticks = 437 cubic_data->min_rtt_ticks; 438 } 439 440 /* Sum samples for epoch average RTT calculation. */ 441 cubic_data->sum_rtt_ticks += t_srtt_ticks; 442 cubic_data->epoch_ack_count++; 443 } 444 } 445 446 /* 447 * Update the ssthresh in the event of congestion. 448 */ 449 static void 450 cubic_ssthresh_update(struct cc_var *ccv) 451 { 452 struct cubic *cubic_data; 453 uint32_t ssthresh; 454 455 cubic_data = ccv->cc_data; 456 457 /* 458 * On the first congestion event, set ssthresh to cwnd * 0.5, on 459 * subsequent congestion events, set it to cwnd * beta. 460 */ 461 if ((cubic_data->flags & CUBICFLAG_CONG_EVENT) == 0) 462 ssthresh = CCV(ccv, snd_cwnd) >> 1; 463 else 464 ssthresh = ((uint64_t)CCV(ccv, snd_cwnd) * 465 CUBIC_BETA) >> CUBIC_SHIFT; 466 CCV(ccv, snd_ssthresh) = max(ssthresh, 2 * CCV(ccv, t_maxseg)); 467 } 468 469 470 DECLARE_CC_MODULE(cubic, &cubic_cc_algo); 471