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