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 #define CAST_PTR_INT(X) (*((int*)(X))) 83 84 static void newreno_cb_destroy(struct cc_var *ccv); 85 static void newreno_ack_received(struct cc_var *ccv, uint16_t type); 86 static void newreno_after_idle(struct cc_var *ccv); 87 static void newreno_cong_signal(struct cc_var *ccv, uint32_t type); 88 static void newreno_post_recovery(struct cc_var *ccv); 89 static int newreno_ctl_output(struct cc_var *ccv, struct sockopt *sopt, void *buf); 90 91 static VNET_DEFINE(uint32_t, newreno_beta) = 50; 92 static VNET_DEFINE(uint32_t, newreno_beta_ecn) = 80; 93 #define V_newreno_beta VNET(newreno_beta) 94 #define V_newreno_beta_ecn VNET(newreno_beta_ecn) 95 96 struct cc_algo newreno_cc_algo = { 97 .name = "newreno", 98 .cb_destroy = newreno_cb_destroy, 99 .ack_received = newreno_ack_received, 100 .after_idle = newreno_after_idle, 101 .cong_signal = newreno_cong_signal, 102 .post_recovery = newreno_post_recovery, 103 .ctl_output = newreno_ctl_output, 104 }; 105 106 struct newreno { 107 uint32_t beta; 108 uint32_t beta_ecn; 109 }; 110 111 static inline struct newreno * 112 newreno_malloc(struct cc_var *ccv) 113 { 114 struct newreno *nreno; 115 116 nreno = malloc(sizeof(struct newreno), M_NEWRENO, M_NOWAIT); 117 if (nreno != NULL) { 118 /* NB: nreno is not zeroed, so initialise all fields. */ 119 nreno->beta = V_newreno_beta; 120 nreno->beta_ecn = V_newreno_beta_ecn; 121 ccv->cc_data = nreno; 122 } 123 124 return (nreno); 125 } 126 127 static void 128 newreno_cb_destroy(struct cc_var *ccv) 129 { 130 131 if (ccv->cc_data != NULL) 132 free(ccv->cc_data, M_NEWRENO); 133 } 134 135 static void 136 newreno_ack_received(struct cc_var *ccv, uint16_t type) 137 { 138 if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) && 139 (ccv->flags & CCF_CWND_LIMITED)) { 140 u_int cw = CCV(ccv, snd_cwnd); 141 u_int incr = CCV(ccv, t_maxseg); 142 143 /* 144 * Regular in-order ACK, open the congestion window. 145 * Method depends on which congestion control state we're 146 * in (slow start or cong avoid) and if ABC (RFC 3465) is 147 * enabled. 148 * 149 * slow start: cwnd <= ssthresh 150 * cong avoid: cwnd > ssthresh 151 * 152 * slow start and ABC (RFC 3465): 153 * Grow cwnd exponentially by the amount of data 154 * ACKed capping the max increment per ACK to 155 * (abc_l_var * maxseg) bytes. 156 * 157 * slow start without ABC (RFC 5681): 158 * Grow cwnd exponentially by maxseg per ACK. 159 * 160 * cong avoid and ABC (RFC 3465): 161 * Grow cwnd linearly by maxseg per RTT for each 162 * cwnd worth of ACKed data. 163 * 164 * cong avoid without ABC (RFC 5681): 165 * Grow cwnd linearly by approximately maxseg per RTT using 166 * maxseg^2 / cwnd per ACK as the increment. 167 * If cwnd > maxseg^2, fix the cwnd increment at 1 byte to 168 * avoid capping cwnd. 169 */ 170 if (cw > CCV(ccv, snd_ssthresh)) { 171 if (V_tcp_do_rfc3465) { 172 if (ccv->flags & CCF_ABC_SENTAWND) 173 ccv->flags &= ~CCF_ABC_SENTAWND; 174 else 175 incr = 0; 176 } else 177 incr = max((incr * incr / cw), 1); 178 } else if (V_tcp_do_rfc3465) { 179 /* 180 * In slow-start with ABC enabled and no RTO in sight? 181 * (Must not use abc_l_var > 1 if slow starting after 182 * an RTO. On RTO, snd_nxt = snd_una, so the 183 * snd_nxt == snd_max check is sufficient to 184 * handle this). 185 * 186 * XXXLAS: Find a way to signal SS after RTO that 187 * doesn't rely on tcpcb vars. 188 */ 189 if (CCV(ccv, snd_nxt) == CCV(ccv, snd_max)) 190 incr = min(ccv->bytes_this_ack, 191 ccv->nsegs * V_tcp_abc_l_var * 192 CCV(ccv, t_maxseg)); 193 else 194 incr = min(ccv->bytes_this_ack, CCV(ccv, t_maxseg)); 195 } 196 /* ABC is on by default, so incr equals 0 frequently. */ 197 if (incr > 0) 198 CCV(ccv, snd_cwnd) = min(cw + incr, 199 TCP_MAXWIN << CCV(ccv, snd_scale)); 200 } 201 } 202 203 static void 204 newreno_after_idle(struct cc_var *ccv) 205 { 206 int rw; 207 208 /* 209 * If we've been idle for more than one retransmit timeout the old 210 * congestion window is no longer current and we have to reduce it to 211 * the restart window before we can transmit again. 212 * 213 * The restart window is the initial window or the last CWND, whichever 214 * is smaller. 215 * 216 * This is done to prevent us from flooding the path with a full CWND at 217 * wirespeed, overloading router and switch buffers along the way. 218 * 219 * See RFC5681 Section 4.1. "Restarting Idle Connections". 220 */ 221 if (V_tcp_do_rfc3390) 222 rw = min(4 * CCV(ccv, t_maxseg), 223 max(2 * CCV(ccv, t_maxseg), 4380)); 224 else 225 rw = CCV(ccv, t_maxseg) * 2; 226 227 CCV(ccv, snd_cwnd) = min(rw, CCV(ccv, snd_cwnd)); 228 } 229 230 /* 231 * Perform any necessary tasks before we enter congestion recovery. 232 */ 233 static void 234 newreno_cong_signal(struct cc_var *ccv, uint32_t type) 235 { 236 struct newreno *nreno; 237 uint32_t beta, beta_ecn, cwin, factor; 238 u_int mss; 239 240 cwin = CCV(ccv, snd_cwnd); 241 mss = CCV(ccv, t_maxseg); 242 nreno = ccv->cc_data; 243 beta = (nreno == NULL) ? V_newreno_beta : nreno->beta; 244 beta_ecn = (nreno == NULL) ? V_newreno_beta_ecn : nreno->beta_ecn; 245 if (V_cc_do_abe && type == CC_ECN) 246 factor = beta_ecn; 247 else 248 factor = beta; 249 250 /* Catch algos which mistakenly leak private signal types. */ 251 KASSERT((type & CC_SIGPRIVMASK) == 0, 252 ("%s: congestion signal type 0x%08x is private\n", __func__, type)); 253 254 cwin = max(((uint64_t)cwin * (uint64_t)factor) / (100ULL * (uint64_t)mss), 255 2) * mss; 256 257 switch (type) { 258 case CC_NDUPACK: 259 if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) { 260 if (IN_CONGRECOVERY(CCV(ccv, t_flags) && 261 V_cc_do_abe && V_cc_abe_frlossreduce)) { 262 CCV(ccv, snd_ssthresh) = 263 ((uint64_t)CCV(ccv, snd_ssthresh) * 264 (uint64_t)beta) / 265 (100ULL * (uint64_t)beta_ecn); 266 } 267 if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) 268 CCV(ccv, snd_ssthresh) = cwin; 269 ENTER_RECOVERY(CCV(ccv, t_flags)); 270 } 271 break; 272 case CC_ECN: 273 if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) { 274 CCV(ccv, snd_ssthresh) = cwin; 275 CCV(ccv, snd_cwnd) = cwin; 276 ENTER_CONGRECOVERY(CCV(ccv, t_flags)); 277 } 278 break; 279 } 280 } 281 282 /* 283 * Perform any necessary tasks before we exit congestion recovery. 284 */ 285 static void 286 newreno_post_recovery(struct cc_var *ccv) 287 { 288 int pipe; 289 290 if (IN_FASTRECOVERY(CCV(ccv, t_flags))) { 291 /* 292 * Fast recovery will conclude after returning from this 293 * function. Window inflation should have left us with 294 * approximately snd_ssthresh outstanding data. But in case we 295 * would be inclined to send a burst, better to do it via the 296 * slow start mechanism. 297 * 298 * XXXLAS: Find a way to do this without needing curack 299 */ 300 if (V_tcp_do_rfc6675_pipe) 301 pipe = tcp_compute_pipe(ccv->ccvc.tcp); 302 else 303 pipe = CCV(ccv, snd_max) - ccv->curack; 304 305 if (pipe < CCV(ccv, snd_ssthresh)) 306 CCV(ccv, snd_cwnd) = pipe + CCV(ccv, t_maxseg); 307 else 308 CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh); 309 } 310 } 311 312 static int 313 newreno_ctl_output(struct cc_var *ccv, struct sockopt *sopt, void *buf) 314 { 315 struct newreno *nreno; 316 struct cc_newreno_opts *opt; 317 318 if (sopt->sopt_valsize != sizeof(struct cc_newreno_opts)) 319 return (EMSGSIZE); 320 321 nreno = ccv->cc_data; 322 opt = buf; 323 324 switch (sopt->sopt_dir) { 325 case SOPT_SET: 326 /* We cannot set without cc_data memory. */ 327 if (nreno == NULL) { 328 nreno = newreno_malloc(ccv); 329 if (nreno == NULL) 330 return (ENOMEM); 331 } 332 switch (opt->name) { 333 case CC_NEWRENO_BETA: 334 nreno->beta = opt->val; 335 break; 336 case CC_NEWRENO_BETA_ECN: 337 if (!V_cc_do_abe) 338 return (EACCES); 339 nreno->beta_ecn = opt->val; 340 break; 341 default: 342 return (ENOPROTOOPT); 343 } 344 break; 345 case SOPT_GET: 346 switch (opt->name) { 347 case CC_NEWRENO_BETA: 348 opt->val = (nreno == NULL) ? 349 V_newreno_beta : nreno->beta; 350 break; 351 case CC_NEWRENO_BETA_ECN: 352 opt->val = (nreno == NULL) ? 353 V_newreno_beta_ecn : nreno->beta_ecn; 354 break; 355 default: 356 return (ENOPROTOOPT); 357 } 358 break; 359 default: 360 return (EINVAL); 361 } 362 363 return (0); 364 } 365 366 static int 367 newreno_beta_handler(SYSCTL_HANDLER_ARGS) 368 { 369 370 if (req->newptr != NULL ) { 371 if (arg1 == &VNET_NAME(newreno_beta_ecn) && !V_cc_do_abe) 372 return (EACCES); 373 if (CAST_PTR_INT(req->newptr) <= 0 || CAST_PTR_INT(req->newptr) > 100) 374 return (EINVAL); 375 } 376 377 return (sysctl_handle_int(oidp, arg1, arg2, req)); 378 } 379 380 SYSCTL_DECL(_net_inet_tcp_cc_newreno); 381 SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, newreno, CTLFLAG_RW, NULL, 382 "New Reno related settings"); 383 384 SYSCTL_PROC(_net_inet_tcp_cc_newreno, OID_AUTO, beta, 385 CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW, 386 &VNET_NAME(newreno_beta), 3, &newreno_beta_handler, "IU", 387 "New Reno beta, specified as number between 1 and 100"); 388 389 SYSCTL_PROC(_net_inet_tcp_cc_newreno, OID_AUTO, beta_ecn, 390 CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW, 391 &VNET_NAME(newreno_beta_ecn), 3, &newreno_beta_handler, "IU", 392 "New Reno beta ecn, specified as number between 1 and 100"); 393 394 DECLARE_CC_MODULE(newreno, &newreno_cc_algo); 395