1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2007-2008 5 * Swinburne University of Technology, Melbourne, Australia. 6 * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org> 7 * Copyright (c) 2010 The FreeBSD Foundation 8 * All rights reserved. 9 * 10 * This software was developed at the Centre for Advanced Internet 11 * Architectures, Swinburne University of Technology, by Lawrence Stewart and 12 * James Healy, made possible in part by a grant from the Cisco University 13 * Research Program Fund at Community Foundation Silicon Valley. 14 * 15 * Portions of this software were developed at the Centre for Advanced 16 * Internet Architectures, Swinburne University of Technology, Melbourne, 17 * Australia by David Hayes under sponsorship from the FreeBSD Foundation. 18 * 19 * Redistribution and use in source and binary forms, with or without 20 * modification, are permitted provided that the following conditions 21 * are met: 22 * 1. Redistributions of source code must retain the above copyright 23 * notice, this list of conditions and the following disclaimer. 24 * 2. Redistributions in binary form must reproduce the above copyright 25 * notice, this list of conditions and the following disclaimer in the 26 * documentation and/or other materials provided with the distribution. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 */ 40 41 /* 42 * This software was first released in 2007 by James Healy and Lawrence Stewart 43 * whilst working on the NewTCP research project at Swinburne University of 44 * Technology's Centre for Advanced Internet Architectures, Melbourne, 45 * Australia, which was made possible in part by a grant from the Cisco 46 * University Research Program Fund at Community Foundation Silicon Valley. 47 * More details are available at: 48 * http://caia.swin.edu.au/urp/newtcp/ 49 */ 50 51 #include <sys/cdefs.h> 52 __FBSDID("$FreeBSD$"); 53 #include <opt_cc.h> 54 #include <sys/param.h> 55 #include <sys/kernel.h> 56 #include <sys/libkern.h> 57 #include <sys/lock.h> 58 #include <sys/malloc.h> 59 #include <sys/module.h> 60 #include <sys/mutex.h> 61 #include <sys/queue.h> 62 #include <sys/rwlock.h> 63 #include <sys/sbuf.h> 64 #include <sys/socket.h> 65 #include <sys/socketvar.h> 66 #include <sys/sysctl.h> 67 68 #include <net/vnet.h> 69 70 #include <netinet/in.h> 71 #include <netinet/in_pcb.h> 72 #include <netinet/tcp.h> 73 #include <netinet/tcp_seq.h> 74 #include <netinet/tcp_var.h> 75 #include <netinet/tcp_log_buf.h> 76 #include <netinet/tcp_hpts.h> 77 #include <netinet/cc/cc.h> 78 #include <netinet/cc/cc_module.h> 79 80 /* 81 * Have a sane default if no CC_DEFAULT is specified in the kernel config file. 82 */ 83 #ifndef CC_DEFAULT 84 #define CC_DEFAULT "newreno" 85 #endif 86 87 MALLOC_DEFINE(M_CC_MEM, "CC Mem", "Congestion Control State memory"); 88 89 /* 90 * List of available cc algorithms on the current system. First element 91 * is used as the system default CC algorithm. 92 */ 93 struct cc_head cc_list = STAILQ_HEAD_INITIALIZER(cc_list); 94 95 /* Protects the cc_list TAILQ. */ 96 struct rwlock cc_list_lock; 97 98 VNET_DEFINE(struct cc_algo *, default_cc_ptr) = NULL; 99 100 VNET_DEFINE(uint32_t, newreno_beta) = 50; 101 #define V_newreno_beta VNET(newreno_beta) 102 103 /* 104 * Sysctl handler to show and change the default CC algorithm. 105 */ 106 static int 107 cc_default_algo(SYSCTL_HANDLER_ARGS) 108 { 109 char default_cc[TCP_CA_NAME_MAX]; 110 struct cc_algo *funcs; 111 int error; 112 113 /* Get the current default: */ 114 CC_LIST_RLOCK(); 115 if (CC_DEFAULT_ALGO() != NULL) 116 strlcpy(default_cc, CC_DEFAULT_ALGO()->name, sizeof(default_cc)); 117 else 118 memset(default_cc, 0, TCP_CA_NAME_MAX); 119 CC_LIST_RUNLOCK(); 120 121 error = sysctl_handle_string(oidp, default_cc, sizeof(default_cc), req); 122 123 /* Check for error or no change */ 124 if (error != 0 || req->newptr == NULL) 125 goto done; 126 127 error = ESRCH; 128 /* Find algo with specified name and set it to default. */ 129 CC_LIST_RLOCK(); 130 STAILQ_FOREACH(funcs, &cc_list, entries) { 131 if (strncmp(default_cc, funcs->name, sizeof(default_cc))) 132 continue; 133 V_default_cc_ptr = funcs; 134 error = 0; 135 break; 136 } 137 CC_LIST_RUNLOCK(); 138 done: 139 return (error); 140 } 141 142 /* 143 * Sysctl handler to display the list of available CC algorithms. 144 */ 145 static int 146 cc_list_available(SYSCTL_HANDLER_ARGS) 147 { 148 struct cc_algo *algo; 149 struct sbuf *s; 150 int err, first, nalgos; 151 152 err = nalgos = 0; 153 first = 1; 154 155 CC_LIST_RLOCK(); 156 STAILQ_FOREACH(algo, &cc_list, entries) { 157 nalgos++; 158 } 159 CC_LIST_RUNLOCK(); 160 if (nalgos == 0) { 161 return (ENOENT); 162 } 163 s = sbuf_new(NULL, NULL, nalgos * TCP_CA_NAME_MAX, SBUF_FIXEDLEN); 164 165 if (s == NULL) 166 return (ENOMEM); 167 168 /* 169 * It is theoretically possible for the CC list to have grown in size 170 * since the call to sbuf_new() and therefore for the sbuf to be too 171 * small. If this were to happen (incredibly unlikely), the sbuf will 172 * reach an overflow condition, sbuf_printf() will return an error and 173 * the sysctl will fail gracefully. 174 */ 175 CC_LIST_RLOCK(); 176 STAILQ_FOREACH(algo, &cc_list, entries) { 177 err = sbuf_printf(s, first ? "%s" : ", %s", algo->name); 178 if (err) { 179 /* Sbuf overflow condition. */ 180 err = EOVERFLOW; 181 break; 182 } 183 first = 0; 184 } 185 CC_LIST_RUNLOCK(); 186 187 if (!err) { 188 sbuf_finish(s); 189 err = sysctl_handle_string(oidp, sbuf_data(s), 0, req); 190 } 191 192 sbuf_delete(s); 193 return (err); 194 } 195 196 /* 197 * Return the number of times a proposed removal_cc is 198 * being used as the default. 199 */ 200 static int 201 cc_check_default(struct cc_algo *remove_cc) 202 { 203 int cnt = 0; 204 VNET_ITERATOR_DECL(vnet_iter); 205 206 CC_LIST_LOCK_ASSERT(); 207 208 VNET_LIST_RLOCK_NOSLEEP(); 209 VNET_FOREACH(vnet_iter) { 210 CURVNET_SET(vnet_iter); 211 if ((CC_DEFAULT_ALGO() != NULL) && 212 strncmp(CC_DEFAULT_ALGO()->name, 213 remove_cc->name, 214 TCP_CA_NAME_MAX) == 0) { 215 cnt++; 216 } 217 CURVNET_RESTORE(); 218 } 219 VNET_LIST_RUNLOCK_NOSLEEP(); 220 return (cnt); 221 } 222 223 /* 224 * Initialise CC subsystem on system boot. 225 */ 226 static void 227 cc_init(void) 228 { 229 CC_LIST_LOCK_INIT(); 230 STAILQ_INIT(&cc_list); 231 } 232 233 /* 234 * Returns non-zero on success, 0 on failure. 235 */ 236 int 237 cc_deregister_algo(struct cc_algo *remove_cc) 238 { 239 struct cc_algo *funcs, *tmpfuncs; 240 int err; 241 242 err = ENOENT; 243 244 /* Remove algo from cc_list so that new connections can't use it. */ 245 CC_LIST_WLOCK(); 246 STAILQ_FOREACH_SAFE(funcs, &cc_list, entries, tmpfuncs) { 247 if (funcs == remove_cc) { 248 if (cc_check_default(remove_cc)) { 249 err = EBUSY; 250 break; 251 } 252 /* Add a temp flag to stop new adds to it */ 253 funcs->flags |= CC_MODULE_BEING_REMOVED; 254 break; 255 } 256 } 257 CC_LIST_WUNLOCK(); 258 err = tcp_ccalgounload(remove_cc); 259 /* 260 * Now back through and we either remove the temp flag 261 * or pull the registration. 262 */ 263 CC_LIST_WLOCK(); 264 STAILQ_FOREACH_SAFE(funcs, &cc_list, entries, tmpfuncs) { 265 if (funcs == remove_cc) { 266 if (err == 0) 267 STAILQ_REMOVE(&cc_list, funcs, cc_algo, entries); 268 else 269 funcs->flags &= ~CC_MODULE_BEING_REMOVED; 270 break; 271 } 272 } 273 CC_LIST_WUNLOCK(); 274 return (err); 275 } 276 277 /* 278 * Returns 0 on success, non-zero on failure. 279 */ 280 int 281 cc_register_algo(struct cc_algo *add_cc) 282 { 283 struct cc_algo *funcs; 284 int err; 285 286 err = 0; 287 288 /* 289 * Iterate over list of registered CC algorithms and make sure 290 * we're not trying to add a duplicate. 291 */ 292 CC_LIST_WLOCK(); 293 STAILQ_FOREACH(funcs, &cc_list, entries) { 294 if (funcs == add_cc || 295 strncmp(funcs->name, add_cc->name, 296 TCP_CA_NAME_MAX) == 0) { 297 err = EEXIST; 298 break; 299 } 300 } 301 /* 302 * The first loaded congestion control module will become 303 * the default until we find the "CC_DEFAULT" defined in 304 * the config (if we do). 305 */ 306 if (!err) { 307 STAILQ_INSERT_TAIL(&cc_list, add_cc, entries); 308 if (strcmp(add_cc->name, CC_DEFAULT) == 0) { 309 V_default_cc_ptr = add_cc; 310 } else if (V_default_cc_ptr == NULL) { 311 V_default_cc_ptr = add_cc; 312 } 313 } 314 CC_LIST_WUNLOCK(); 315 316 return (err); 317 } 318 319 /* 320 * Perform any necessary tasks before we exit congestion recovery. 321 */ 322 void 323 newreno_cc_post_recovery(struct cc_var *ccv) 324 { 325 int pipe; 326 327 if (IN_FASTRECOVERY(CCV(ccv, t_flags))) { 328 /* 329 * Fast recovery will conclude after returning from this 330 * function. Window inflation should have left us with 331 * approximately snd_ssthresh outstanding data. But in case we 332 * would be inclined to send a burst, better to do it via the 333 * slow start mechanism. 334 * 335 * XXXLAS: Find a way to do this without needing curack 336 */ 337 if (V_tcp_do_newsack) 338 pipe = tcp_compute_pipe(ccv->ccvc.tcp); 339 else 340 pipe = CCV(ccv, snd_max) - ccv->curack; 341 if (pipe < CCV(ccv, snd_ssthresh)) 342 /* 343 * Ensure that cwnd does not collapse to 1 MSS under 344 * adverse conditons. Implements RFC6582 345 */ 346 CCV(ccv, snd_cwnd) = max(pipe, CCV(ccv, t_maxseg)) + 347 CCV(ccv, t_maxseg); 348 else 349 CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh); 350 } 351 } 352 353 void 354 newreno_cc_after_idle(struct cc_var *ccv) 355 { 356 uint32_t rw; 357 /* 358 * If we've been idle for more than one retransmit timeout the old 359 * congestion window is no longer current and we have to reduce it to 360 * the restart window before we can transmit again. 361 * 362 * The restart window is the initial window or the last CWND, whichever 363 * is smaller. 364 * 365 * This is done to prevent us from flooding the path with a full CWND at 366 * wirespeed, overloading router and switch buffers along the way. 367 * 368 * See RFC5681 Section 4.1. "Restarting Idle Connections". 369 * 370 * In addition, per RFC2861 Section 2, the ssthresh is set to the 371 * maximum of the former ssthresh or 3/4 of the old cwnd, to 372 * not exit slow-start prematurely. 373 */ 374 rw = tcp_compute_initwnd(tcp_maxseg(ccv->ccvc.tcp)); 375 376 CCV(ccv, snd_ssthresh) = max(CCV(ccv, snd_ssthresh), 377 CCV(ccv, snd_cwnd)-(CCV(ccv, snd_cwnd)>>2)); 378 379 CCV(ccv, snd_cwnd) = min(rw, CCV(ccv, snd_cwnd)); 380 } 381 382 /* 383 * Perform any necessary tasks before we enter congestion recovery. 384 */ 385 void 386 newreno_cc_cong_signal(struct cc_var *ccv, uint32_t type) 387 { 388 uint32_t cwin, factor; 389 u_int mss; 390 391 cwin = CCV(ccv, snd_cwnd); 392 mss = tcp_fixed_maxseg(ccv->ccvc.tcp); 393 /* 394 * Other TCP congestion controls use newreno_cong_signal(), but 395 * with their own private cc_data. Make sure the cc_data is used 396 * correctly. 397 */ 398 factor = V_newreno_beta; 399 400 /* Catch algos which mistakenly leak private signal types. */ 401 KASSERT((type & CC_SIGPRIVMASK) == 0, 402 ("%s: congestion signal type 0x%08x is private\n", __func__, type)); 403 404 cwin = max(((uint64_t)cwin * (uint64_t)factor) / (100ULL * (uint64_t)mss), 405 2) * mss; 406 407 switch (type) { 408 case CC_NDUPACK: 409 if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) { 410 if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) 411 CCV(ccv, snd_ssthresh) = cwin; 412 ENTER_RECOVERY(CCV(ccv, t_flags)); 413 } 414 break; 415 case CC_ECN: 416 if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) { 417 CCV(ccv, snd_ssthresh) = cwin; 418 CCV(ccv, snd_cwnd) = cwin; 419 ENTER_CONGRECOVERY(CCV(ccv, t_flags)); 420 } 421 break; 422 case CC_RTO: 423 CCV(ccv, snd_ssthresh) = max(min(CCV(ccv, snd_wnd), 424 CCV(ccv, snd_cwnd)) / 2 / mss, 425 2) * mss; 426 CCV(ccv, snd_cwnd) = mss; 427 break; 428 } 429 } 430 431 void 432 newreno_cc_ack_received(struct cc_var *ccv, uint16_t type) 433 { 434 if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) && 435 (ccv->flags & CCF_CWND_LIMITED)) { 436 u_int cw = CCV(ccv, snd_cwnd); 437 u_int incr = CCV(ccv, t_maxseg); 438 439 /* 440 * Regular in-order ACK, open the congestion window. 441 * Method depends on which congestion control state we're 442 * in (slow start or cong avoid) and if ABC (RFC 3465) is 443 * enabled. 444 * 445 * slow start: cwnd <= ssthresh 446 * cong avoid: cwnd > ssthresh 447 * 448 * slow start and ABC (RFC 3465): 449 * Grow cwnd exponentially by the amount of data 450 * ACKed capping the max increment per ACK to 451 * (abc_l_var * maxseg) bytes. 452 * 453 * slow start without ABC (RFC 5681): 454 * Grow cwnd exponentially by maxseg per ACK. 455 * 456 * cong avoid and ABC (RFC 3465): 457 * Grow cwnd linearly by maxseg per RTT for each 458 * cwnd worth of ACKed data. 459 * 460 * cong avoid without ABC (RFC 5681): 461 * Grow cwnd linearly by approximately maxseg per RTT using 462 * maxseg^2 / cwnd per ACK as the increment. 463 * If cwnd > maxseg^2, fix the cwnd increment at 1 byte to 464 * avoid capping cwnd. 465 */ 466 if (cw > CCV(ccv, snd_ssthresh)) { 467 if (V_tcp_do_rfc3465) { 468 if (ccv->flags & CCF_ABC_SENTAWND) 469 ccv->flags &= ~CCF_ABC_SENTAWND; 470 else 471 incr = 0; 472 } else 473 incr = max((incr * incr / cw), 1); 474 } else if (V_tcp_do_rfc3465) { 475 /* 476 * In slow-start with ABC enabled and no RTO in sight? 477 * (Must not use abc_l_var > 1 if slow starting after 478 * an RTO. On RTO, snd_nxt = snd_una, so the 479 * snd_nxt == snd_max check is sufficient to 480 * handle this). 481 * 482 * XXXLAS: Find a way to signal SS after RTO that 483 * doesn't rely on tcpcb vars. 484 */ 485 uint16_t abc_val; 486 487 if (ccv->flags & CCF_USE_LOCAL_ABC) 488 abc_val = ccv->labc; 489 else 490 abc_val = V_tcp_abc_l_var; 491 if (CCV(ccv, snd_nxt) == CCV(ccv, snd_max)) 492 incr = min(ccv->bytes_this_ack, 493 ccv->nsegs * abc_val * 494 CCV(ccv, t_maxseg)); 495 else 496 incr = min(ccv->bytes_this_ack, CCV(ccv, t_maxseg)); 497 498 } 499 /* ABC is on by default, so incr equals 0 frequently. */ 500 if (incr > 0) 501 CCV(ccv, snd_cwnd) = min(cw + incr, 502 TCP_MAXWIN << CCV(ccv, snd_scale)); 503 } 504 } 505 506 /* 507 * Handles kld related events. Returns 0 on success, non-zero on failure. 508 */ 509 int 510 cc_modevent(module_t mod, int event_type, void *data) 511 { 512 struct cc_algo *algo; 513 int err; 514 515 err = 0; 516 algo = (struct cc_algo *)data; 517 518 switch(event_type) { 519 case MOD_LOAD: 520 if ((algo->cc_data_sz == NULL) && (algo->cb_init != NULL)) { 521 /* 522 * A module must have a cc_data_sz function 523 * even if it has no data it should return 0. 524 */ 525 printf("Module Load Fails, it lacks a cc_data_sz() function but has a cb_init()!\n"); 526 err = EINVAL; 527 break; 528 } 529 if (algo->mod_init != NULL) 530 err = algo->mod_init(); 531 if (!err) 532 err = cc_register_algo(algo); 533 break; 534 535 case MOD_QUIESCE: 536 case MOD_SHUTDOWN: 537 case MOD_UNLOAD: 538 err = cc_deregister_algo(algo); 539 if (!err && algo->mod_destroy != NULL) 540 algo->mod_destroy(); 541 if (err == ENOENT) 542 err = 0; 543 break; 544 545 default: 546 err = EINVAL; 547 break; 548 } 549 550 return (err); 551 } 552 553 SYSINIT(cc, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, cc_init, NULL); 554 555 /* Declare sysctl tree and populate it. */ 556 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, cc, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 557 "Congestion control related settings"); 558 559 SYSCTL_PROC(_net_inet_tcp_cc, OID_AUTO, algorithm, 560 CTLFLAG_VNET | CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, 561 NULL, 0, cc_default_algo, "A", 562 "Default congestion control algorithm"); 563 564 SYSCTL_PROC(_net_inet_tcp_cc, OID_AUTO, available, 565 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 566 NULL, 0, cc_list_available, "A", 567 "List available congestion control algorithms"); 568 569 VNET_DEFINE(int, cc_do_abe) = 0; 570 SYSCTL_INT(_net_inet_tcp_cc, OID_AUTO, abe, CTLFLAG_VNET | CTLFLAG_RW, 571 &VNET_NAME(cc_do_abe), 0, 572 "Enable draft-ietf-tcpm-alternativebackoff-ecn (TCP Alternative Backoff with ECN)"); 573 574 VNET_DEFINE(int, cc_abe_frlossreduce) = 0; 575 SYSCTL_INT(_net_inet_tcp_cc, OID_AUTO, abe_frlossreduce, CTLFLAG_VNET | CTLFLAG_RW, 576 &VNET_NAME(cc_abe_frlossreduce), 0, 577 "Apply standard beta instead of ABE-beta during ECN-signalled congestion " 578 "recovery episodes if loss also needs to be repaired"); 579