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