1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 5 * The Regents of the University of California. 6 * Copyright (c) 2007-2008,2010,2014 7 * Swinburne University of Technology, Melbourne, Australia. 8 * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org> 9 * Copyright (c) 2010 The FreeBSD Foundation 10 * All rights reserved. 11 * 12 * This software was developed at the Centre for Advanced Internet 13 * Architectures, Swinburne University of Technology, by Lawrence Stewart, James 14 * Healy and David Hayes, made possible in part by a grant from the Cisco 15 * University Research Program Fund at Community Foundation Silicon Valley. 16 * 17 * Portions of this software were developed at the Centre for Advanced 18 * Internet Architectures, Swinburne University of Technology, Melbourne, 19 * Australia by David Hayes under sponsorship from the FreeBSD Foundation. 20 * 21 * Redistribution and use in source and binary forms, with or without 22 * modification, are permitted provided that the following conditions 23 * are met: 24 * 1. Redistributions of source code must retain the above copyright 25 * notice, this list of conditions and the following disclaimer. 26 * 2. Redistributions in binary form must reproduce the above copyright 27 * notice, this list of conditions and the following disclaimer in the 28 * documentation and/or other materials provided with the distribution. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40 * SUCH DAMAGE. 41 */ 42 43 /* 44 * This software was first released in 2007 by James Healy and Lawrence Stewart 45 * whilst working on the NewTCP research project at Swinburne University of 46 * Technology's Centre for Advanced Internet Architectures, Melbourne, 47 * Australia, which was made possible in part by a grant from the Cisco 48 * University Research Program Fund at Community Foundation Silicon Valley. 49 * More details are available at: 50 * http://caia.swin.edu.au/urp/newtcp/ 51 * 52 * Dec 2014 garmitage@swin.edu.au 53 * Borrowed code fragments from cc_cdg.c to add modifiable beta 54 * via sysctls. 55 * 56 */ 57 58 #include <sys/cdefs.h> 59 __FBSDID("$FreeBSD$"); 60 61 #include <sys/param.h> 62 #include <sys/kernel.h> 63 #include <sys/malloc.h> 64 #include <sys/module.h> 65 #include <sys/socket.h> 66 #include <sys/socketvar.h> 67 #include <sys/sysctl.h> 68 #include <sys/systm.h> 69 70 #include <net/vnet.h> 71 72 #include <netinet/tcp.h> 73 #include <netinet/tcp_seq.h> 74 #include <netinet/tcp_var.h> 75 #include <netinet/cc/cc.h> 76 #include <netinet/cc/cc_module.h> 77 #include <netinet/cc/cc_newreno.h> 78 79 static MALLOC_DEFINE(M_NEWRENO, "newreno data", 80 "newreno beta values"); 81 82 static void newreno_cb_destroy(struct cc_var *ccv); 83 static void newreno_ack_received(struct cc_var *ccv, uint16_t type); 84 static void newreno_after_idle(struct cc_var *ccv); 85 static void newreno_cong_signal(struct cc_var *ccv, uint32_t type); 86 static void newreno_post_recovery(struct cc_var *ccv); 87 static int newreno_ctl_output(struct cc_var *ccv, struct sockopt *sopt, void *buf); 88 89 VNET_DEFINE(uint32_t, newreno_beta) = 50; 90 VNET_DEFINE(uint32_t, newreno_beta_ecn) = 80; 91 #define V_newreno_beta VNET(newreno_beta) 92 #define V_newreno_beta_ecn VNET(newreno_beta_ecn) 93 94 struct cc_algo newreno_cc_algo = { 95 .name = "newreno", 96 .cb_destroy = newreno_cb_destroy, 97 .ack_received = newreno_ack_received, 98 .after_idle = newreno_after_idle, 99 .cong_signal = newreno_cong_signal, 100 .post_recovery = newreno_post_recovery, 101 .ctl_output = newreno_ctl_output, 102 }; 103 104 static inline struct newreno * 105 newreno_malloc(struct cc_var *ccv) 106 { 107 struct newreno *nreno; 108 109 nreno = malloc(sizeof(struct newreno), M_NEWRENO, M_NOWAIT); 110 if (nreno != NULL) { 111 /* NB: nreno is not zeroed, so initialise all fields. */ 112 nreno->beta = V_newreno_beta; 113 nreno->beta_ecn = V_newreno_beta_ecn; 114 nreno->newreno_flags = 0; 115 ccv->cc_data = nreno; 116 } 117 118 return (nreno); 119 } 120 121 static void 122 newreno_cb_destroy(struct cc_var *ccv) 123 { 124 free(ccv->cc_data, M_NEWRENO); 125 } 126 127 static void 128 newreno_ack_received(struct cc_var *ccv, uint16_t type) 129 { 130 if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) && 131 (ccv->flags & CCF_CWND_LIMITED)) { 132 u_int cw = CCV(ccv, snd_cwnd); 133 u_int incr = CCV(ccv, t_maxseg); 134 135 /* 136 * Regular in-order ACK, open the congestion window. 137 * Method depends on which congestion control state we're 138 * in (slow start or cong avoid) and if ABC (RFC 3465) is 139 * enabled. 140 * 141 * slow start: cwnd <= ssthresh 142 * cong avoid: cwnd > ssthresh 143 * 144 * slow start and ABC (RFC 3465): 145 * Grow cwnd exponentially by the amount of data 146 * ACKed capping the max increment per ACK to 147 * (abc_l_var * maxseg) bytes. 148 * 149 * slow start without ABC (RFC 5681): 150 * Grow cwnd exponentially by maxseg per ACK. 151 * 152 * cong avoid and ABC (RFC 3465): 153 * Grow cwnd linearly by maxseg per RTT for each 154 * cwnd worth of ACKed data. 155 * 156 * cong avoid without ABC (RFC 5681): 157 * Grow cwnd linearly by approximately maxseg per RTT using 158 * maxseg^2 / cwnd per ACK as the increment. 159 * If cwnd > maxseg^2, fix the cwnd increment at 1 byte to 160 * avoid capping cwnd. 161 */ 162 if (cw > CCV(ccv, snd_ssthresh)) { 163 if (V_tcp_do_rfc3465) { 164 if (ccv->flags & CCF_ABC_SENTAWND) 165 ccv->flags &= ~CCF_ABC_SENTAWND; 166 else 167 incr = 0; 168 } else 169 incr = max((incr * incr / cw), 1); 170 } else if (V_tcp_do_rfc3465) { 171 /* 172 * In slow-start with ABC enabled and no RTO in sight? 173 * (Must not use abc_l_var > 1 if slow starting after 174 * an RTO. On RTO, snd_nxt = snd_una, so the 175 * snd_nxt == snd_max check is sufficient to 176 * handle this). 177 * 178 * XXXLAS: Find a way to signal SS after RTO that 179 * doesn't rely on tcpcb vars. 180 */ 181 uint16_t abc_val; 182 183 if (ccv->flags & CCF_USE_LOCAL_ABC) 184 abc_val = ccv->labc; 185 else 186 abc_val = V_tcp_abc_l_var; 187 if (CCV(ccv, snd_nxt) == CCV(ccv, snd_max)) 188 incr = min(ccv->bytes_this_ack, 189 ccv->nsegs * abc_val * 190 CCV(ccv, t_maxseg)); 191 else 192 incr = min(ccv->bytes_this_ack, CCV(ccv, t_maxseg)); 193 } 194 /* ABC is on by default, so incr equals 0 frequently. */ 195 if (incr > 0) 196 CCV(ccv, snd_cwnd) = min(cw + incr, 197 TCP_MAXWIN << CCV(ccv, snd_scale)); 198 } 199 } 200 201 static void 202 newreno_after_idle(struct cc_var *ccv) 203 { 204 uint32_t rw; 205 206 /* 207 * If we've been idle for more than one retransmit timeout the old 208 * congestion window is no longer current and we have to reduce it to 209 * the restart window before we can transmit again. 210 * 211 * The restart window is the initial window or the last CWND, whichever 212 * is smaller. 213 * 214 * This is done to prevent us from flooding the path with a full CWND at 215 * wirespeed, overloading router and switch buffers along the way. 216 * 217 * See RFC5681 Section 4.1. "Restarting Idle Connections". 218 * 219 * In addition, per RFC2861 Section 2, the ssthresh is set to the 220 * maximum of the former ssthresh or 3/4 of the old cwnd, to 221 * not exit slow-start prematurely. 222 */ 223 rw = tcp_compute_initwnd(tcp_maxseg(ccv->ccvc.tcp)); 224 225 CCV(ccv, snd_ssthresh) = max(CCV(ccv, snd_ssthresh), 226 CCV(ccv, snd_cwnd)-(CCV(ccv, snd_cwnd)>>2)); 227 228 CCV(ccv, snd_cwnd) = min(rw, CCV(ccv, snd_cwnd)); 229 } 230 231 /* 232 * Perform any necessary tasks before we enter congestion recovery. 233 */ 234 static void 235 newreno_cong_signal(struct cc_var *ccv, uint32_t type) 236 { 237 struct newreno *nreno; 238 uint32_t beta, beta_ecn, cwin, factor; 239 u_int mss; 240 241 cwin = CCV(ccv, snd_cwnd); 242 mss = tcp_fixed_maxseg(ccv->ccvc.tcp); 243 /* 244 * Other TCP congestion controls use newreno_cong_signal(), but 245 * with their own private cc_data. Make sure the cc_data is used 246 * correctly. 247 */ 248 nreno = (CC_ALGO(ccv->ccvc.tcp) == &newreno_cc_algo) ? ccv->cc_data : NULL; 249 beta = (nreno == NULL) ? V_newreno_beta : nreno->beta; 250 beta_ecn = (nreno == NULL) ? V_newreno_beta_ecn : nreno->beta_ecn; 251 252 /* 253 * Note that we only change the backoff for ECN if the 254 * global sysctl V_cc_do_abe is set <or> the stack itself 255 * has set a flag in our newreno_flags (due to pacing) telling 256 * us to use the lower valued back-off. 257 */ 258 if ((type == CC_ECN) && 259 (V_cc_do_abe || 260 ((nreno != NULL) && (nreno->newreno_flags & CC_NEWRENO_BETA_ECN)))) 261 factor = beta_ecn; 262 else 263 factor = beta; 264 265 /* Catch algos which mistakenly leak private signal types. */ 266 KASSERT((type & CC_SIGPRIVMASK) == 0, 267 ("%s: congestion signal type 0x%08x is private\n", __func__, type)); 268 269 cwin = max(((uint64_t)cwin * (uint64_t)factor) / (100ULL * (uint64_t)mss), 270 2) * mss; 271 272 switch (type) { 273 case CC_NDUPACK: 274 if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) { 275 if (IN_CONGRECOVERY(CCV(ccv, t_flags) && 276 V_cc_do_abe && V_cc_abe_frlossreduce)) { 277 CCV(ccv, snd_ssthresh) = 278 ((uint64_t)CCV(ccv, snd_ssthresh) * 279 (uint64_t)beta) / (uint64_t)beta_ecn; 280 } 281 if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) 282 CCV(ccv, snd_ssthresh) = cwin; 283 ENTER_RECOVERY(CCV(ccv, t_flags)); 284 } 285 break; 286 case CC_ECN: 287 if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) { 288 CCV(ccv, snd_ssthresh) = cwin; 289 CCV(ccv, snd_cwnd) = cwin; 290 ENTER_CONGRECOVERY(CCV(ccv, t_flags)); 291 } 292 break; 293 case CC_RTO: 294 CCV(ccv, snd_ssthresh) = max(min(CCV(ccv, snd_wnd), 295 CCV(ccv, snd_cwnd)) / 2 / mss, 296 2) * mss; 297 CCV(ccv, snd_cwnd) = mss; 298 break; 299 } 300 } 301 302 /* 303 * Perform any necessary tasks before we exit congestion recovery. 304 */ 305 static void 306 newreno_post_recovery(struct cc_var *ccv) 307 { 308 int pipe; 309 310 if (IN_FASTRECOVERY(CCV(ccv, t_flags))) { 311 /* 312 * Fast recovery will conclude after returning from this 313 * function. Window inflation should have left us with 314 * approximately snd_ssthresh outstanding data. But in case we 315 * would be inclined to send a burst, better to do it via the 316 * slow start mechanism. 317 * 318 * XXXLAS: Find a way to do this without needing curack 319 */ 320 if (V_tcp_do_newsack) 321 pipe = tcp_compute_pipe(ccv->ccvc.tcp); 322 else 323 pipe = CCV(ccv, snd_max) - ccv->curack; 324 325 if (pipe < CCV(ccv, snd_ssthresh)) 326 /* 327 * Ensure that cwnd does not collapse to 1 MSS under 328 * adverse conditons. Implements RFC6582 329 */ 330 CCV(ccv, snd_cwnd) = max(pipe, CCV(ccv, t_maxseg)) + 331 CCV(ccv, t_maxseg); 332 else 333 CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh); 334 } 335 } 336 337 static int 338 newreno_ctl_output(struct cc_var *ccv, struct sockopt *sopt, void *buf) 339 { 340 struct newreno *nreno; 341 struct cc_newreno_opts *opt; 342 343 if (sopt->sopt_valsize != sizeof(struct cc_newreno_opts)) 344 return (EMSGSIZE); 345 346 if (CC_ALGO(ccv->ccvc.tcp) != &newreno_cc_algo) 347 return (ENOPROTOOPT); 348 349 nreno = ccv->cc_data; 350 opt = buf; 351 352 switch (sopt->sopt_dir) { 353 case SOPT_SET: 354 /* We cannot set without cc_data memory. */ 355 if (nreno == NULL) { 356 nreno = newreno_malloc(ccv); 357 if (nreno == NULL) 358 return (ENOMEM); 359 } 360 switch (opt->name) { 361 case CC_NEWRENO_BETA: 362 nreno->beta = opt->val; 363 break; 364 case CC_NEWRENO_BETA_ECN: 365 if ((!V_cc_do_abe) && ((nreno->newreno_flags & CC_NEWRENO_BETA_ECN) == 0)) 366 return (EACCES); 367 nreno->beta_ecn = opt->val; 368 break; 369 default: 370 return (ENOPROTOOPT); 371 } 372 break; 373 case SOPT_GET: 374 switch (opt->name) { 375 case CC_NEWRENO_BETA: 376 opt->val = (nreno == NULL) ? 377 V_newreno_beta : nreno->beta; 378 break; 379 case CC_NEWRENO_BETA_ECN: 380 opt->val = (nreno == NULL) ? 381 V_newreno_beta_ecn : nreno->beta_ecn; 382 break; 383 default: 384 return (ENOPROTOOPT); 385 } 386 break; 387 default: 388 return (EINVAL); 389 } 390 391 return (0); 392 } 393 394 static int 395 newreno_beta_handler(SYSCTL_HANDLER_ARGS) 396 { 397 int error; 398 uint32_t new; 399 400 new = *(uint32_t *)arg1; 401 error = sysctl_handle_int(oidp, &new, 0, req); 402 if (error == 0 && req->newptr != NULL ) { 403 if (arg1 == &VNET_NAME(newreno_beta_ecn) && !V_cc_do_abe) 404 error = EACCES; 405 else if (new == 0 || new > 100) 406 error = EINVAL; 407 else 408 *(uint32_t *)arg1 = new; 409 } 410 411 return (error); 412 } 413 414 SYSCTL_DECL(_net_inet_tcp_cc_newreno); 415 SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, newreno, 416 CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 417 "New Reno related settings"); 418 419 SYSCTL_PROC(_net_inet_tcp_cc_newreno, OID_AUTO, beta, 420 CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 421 &VNET_NAME(newreno_beta), 3, &newreno_beta_handler, "IU", 422 "New Reno beta, specified as number between 1 and 100"); 423 424 SYSCTL_PROC(_net_inet_tcp_cc_newreno, OID_AUTO, beta_ecn, 425 CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 426 &VNET_NAME(newreno_beta_ecn), 3, &newreno_beta_handler, "IU", 427 "New Reno beta ecn, specified as number between 1 and 100"); 428 429 DECLARE_CC_MODULE(newreno, &newreno_cc_algo); 430 MODULE_VERSION(newreno, 1); 431