xref: /freebsd/sys/netinet/cc/cc_newreno.c (revision 1603881667360c015f6685131f2f25474fa67a72)
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 		ccv->cc_data = nreno;
115 	}
116 
117 	return (nreno);
118 }
119 
120 static void
121 newreno_cb_destroy(struct cc_var *ccv)
122 {
123 	free(ccv->cc_data, M_NEWRENO);
124 }
125 
126 static void
127 newreno_ack_received(struct cc_var *ccv, uint16_t type)
128 {
129 	if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
130 	    (ccv->flags & CCF_CWND_LIMITED)) {
131 		u_int cw = CCV(ccv, snd_cwnd);
132 		u_int incr = CCV(ccv, t_maxseg);
133 
134 		/*
135 		 * Regular in-order ACK, open the congestion window.
136 		 * Method depends on which congestion control state we're
137 		 * in (slow start or cong avoid) and if ABC (RFC 3465) is
138 		 * enabled.
139 		 *
140 		 * slow start: cwnd <= ssthresh
141 		 * cong avoid: cwnd > ssthresh
142 		 *
143 		 * slow start and ABC (RFC 3465):
144 		 *   Grow cwnd exponentially by the amount of data
145 		 *   ACKed capping the max increment per ACK to
146 		 *   (abc_l_var * maxseg) bytes.
147 		 *
148 		 * slow start without ABC (RFC 5681):
149 		 *   Grow cwnd exponentially by maxseg per ACK.
150 		 *
151 		 * cong avoid and ABC (RFC 3465):
152 		 *   Grow cwnd linearly by maxseg per RTT for each
153 		 *   cwnd worth of ACKed data.
154 		 *
155 		 * cong avoid without ABC (RFC 5681):
156 		 *   Grow cwnd linearly by approximately maxseg per RTT using
157 		 *   maxseg^2 / cwnd per ACK as the increment.
158 		 *   If cwnd > maxseg^2, fix the cwnd increment at 1 byte to
159 		 *   avoid capping cwnd.
160 		 */
161 		if (cw > CCV(ccv, snd_ssthresh)) {
162 			if (V_tcp_do_rfc3465) {
163 				if (ccv->flags & CCF_ABC_SENTAWND)
164 					ccv->flags &= ~CCF_ABC_SENTAWND;
165 				else
166 					incr = 0;
167 			} else
168 				incr = max((incr * incr / cw), 1);
169 		} else if (V_tcp_do_rfc3465) {
170 			/*
171 			 * In slow-start with ABC enabled and no RTO in sight?
172 			 * (Must not use abc_l_var > 1 if slow starting after
173 			 * an RTO. On RTO, snd_nxt = snd_una, so the
174 			 * snd_nxt == snd_max check is sufficient to
175 			 * handle this).
176 			 *
177 			 * XXXLAS: Find a way to signal SS after RTO that
178 			 * doesn't rely on tcpcb vars.
179 			 */
180 			uint16_t abc_val;
181 
182 			if (ccv->flags & CCF_USE_LOCAL_ABC)
183 				abc_val = ccv->labc;
184 			else
185 				abc_val = V_tcp_abc_l_var;
186 			if (CCV(ccv, snd_nxt) == CCV(ccv, snd_max))
187 				incr = min(ccv->bytes_this_ack,
188 				    ccv->nsegs * abc_val *
189 				    CCV(ccv, t_maxseg));
190 			else
191 				incr = min(ccv->bytes_this_ack, CCV(ccv, t_maxseg));
192 		}
193 		/* ABC is on by default, so incr equals 0 frequently. */
194 		if (incr > 0)
195 			CCV(ccv, snd_cwnd) = min(cw + incr,
196 			    TCP_MAXWIN << CCV(ccv, snd_scale));
197 	}
198 }
199 
200 static void
201 newreno_after_idle(struct cc_var *ccv)
202 {
203 	uint32_t rw;
204 
205 	/*
206 	 * If we've been idle for more than one retransmit timeout the old
207 	 * congestion window is no longer current and we have to reduce it to
208 	 * the restart window before we can transmit again.
209 	 *
210 	 * The restart window is the initial window or the last CWND, whichever
211 	 * is smaller.
212 	 *
213 	 * This is done to prevent us from flooding the path with a full CWND at
214 	 * wirespeed, overloading router and switch buffers along the way.
215 	 *
216 	 * See RFC5681 Section 4.1. "Restarting Idle Connections".
217 	 *
218 	 * In addition, per RFC2861 Section 2, the ssthresh is set to the
219 	 * maximum of the former ssthresh or 3/4 of the old cwnd, to
220 	 * not exit slow-start prematurely.
221 	 */
222 	rw = tcp_compute_initwnd(tcp_maxseg(ccv->ccvc.tcp));
223 
224 	CCV(ccv, snd_ssthresh) = max(CCV(ccv, snd_ssthresh),
225 	    CCV(ccv, snd_cwnd)-(CCV(ccv, snd_cwnd)>>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 = tcp_fixed_maxseg(ccv->ccvc.tcp);
242 	/*
243 	 * Other TCP congestion controls use newreno_cong_signal(), but
244 	 * with their own private cc_data. Make sure the cc_data is used
245 	 * correctly.
246 	 */
247 	nreno = (CC_ALGO(ccv->ccvc.tcp) == &newreno_cc_algo) ? ccv->cc_data : NULL;
248 	beta = (nreno == NULL) ? V_newreno_beta : nreno->beta;
249 	beta_ecn = (nreno == NULL) ? V_newreno_beta_ecn : nreno->beta_ecn;
250 
251 	/*
252 	 * Note that we only change the backoff for ECN if the
253 	 * global sysctl V_cc_do_abe is set <or> the stack itself
254 	 * has set a flag in our newreno_flags (due to pacing) telling
255 	 * us to use the lower valued back-off.
256 	 */
257 	if (V_cc_do_abe ||
258 	    (nreno && (nreno->newreno_flags & CC_NEWRENO_BETA_ECN) && (type == CC_ECN)))
259 		factor = beta_ecn;
260 	else
261 		factor = beta;
262 
263 	/* Catch algos which mistakenly leak private signal types. */
264 	KASSERT((type & CC_SIGPRIVMASK) == 0,
265 	    ("%s: congestion signal type 0x%08x is private\n", __func__, type));
266 
267 	cwin = max(((uint64_t)cwin * (uint64_t)factor) / (100ULL * (uint64_t)mss),
268 	    2) * mss;
269 
270 	switch (type) {
271 	case CC_NDUPACK:
272 		if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) {
273 			if (IN_CONGRECOVERY(CCV(ccv, t_flags) &&
274 			    V_cc_do_abe && V_cc_abe_frlossreduce)) {
275 				CCV(ccv, snd_ssthresh) =
276 				    ((uint64_t)CCV(ccv, snd_ssthresh) *
277 				     (uint64_t)beta) / (uint64_t)beta_ecn;
278 			}
279 			if (!IN_CONGRECOVERY(CCV(ccv, t_flags)))
280 				CCV(ccv, snd_ssthresh) = cwin;
281 			ENTER_RECOVERY(CCV(ccv, t_flags));
282 		}
283 		break;
284 	case CC_ECN:
285 		if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
286 			CCV(ccv, snd_ssthresh) = cwin;
287 			CCV(ccv, snd_cwnd) = cwin;
288 			ENTER_CONGRECOVERY(CCV(ccv, t_flags));
289 		}
290 		break;
291 	case CC_RTO:
292 		CCV(ccv, snd_ssthresh) = max(min(CCV(ccv, snd_wnd),
293 						 CCV(ccv, snd_cwnd)) / 2 / mss,
294 					     2) * mss;
295 		CCV(ccv, snd_cwnd) = mss;
296 		break;
297 	}
298 }
299 
300 /*
301  * Perform any necessary tasks before we exit congestion recovery.
302  */
303 static void
304 newreno_post_recovery(struct cc_var *ccv)
305 {
306 	int pipe;
307 
308 	if (IN_FASTRECOVERY(CCV(ccv, t_flags))) {
309 		/*
310 		 * Fast recovery will conclude after returning from this
311 		 * function. Window inflation should have left us with
312 		 * approximately snd_ssthresh outstanding data. But in case we
313 		 * would be inclined to send a burst, better to do it via the
314 		 * slow start mechanism.
315 		 *
316 		 * XXXLAS: Find a way to do this without needing curack
317 		 */
318 		if (V_tcp_do_newsack)
319 			pipe = tcp_compute_pipe(ccv->ccvc.tcp);
320 		else
321 			pipe = CCV(ccv, snd_max) - ccv->curack;
322 
323 		if (pipe < CCV(ccv, snd_ssthresh))
324 			/*
325 			 * Ensure that cwnd does not collapse to 1 MSS under
326 			 * adverse conditons. Implements RFC6582
327 			 */
328 			CCV(ccv, snd_cwnd) = max(pipe, CCV(ccv, t_maxseg)) +
329 			    CCV(ccv, t_maxseg);
330 		else
331 			CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh);
332 	}
333 }
334 
335 static int
336 newreno_ctl_output(struct cc_var *ccv, struct sockopt *sopt, void *buf)
337 {
338 	struct newreno *nreno;
339 	struct cc_newreno_opts *opt;
340 
341 	if (sopt->sopt_valsize != sizeof(struct cc_newreno_opts))
342 		return (EMSGSIZE);
343 
344 	if (CC_ALGO(ccv->ccvc.tcp) != &newreno_cc_algo)
345 		return (ENOPROTOOPT);
346 
347 	nreno = ccv->cc_data;
348 	opt = buf;
349 
350 	switch (sopt->sopt_dir) {
351 	case SOPT_SET:
352 		/* We cannot set without cc_data memory. */
353 		if (nreno == NULL) {
354 			nreno = newreno_malloc(ccv);
355 			if (nreno == NULL)
356 				return (ENOMEM);
357 		}
358 		switch (opt->name) {
359 		case CC_NEWRENO_BETA:
360 			nreno->beta = opt->val;
361 			break;
362 		case CC_NEWRENO_BETA_ECN:
363 			if ((!V_cc_do_abe) && ((nreno->newreno_flags & CC_NEWRENO_BETA_ECN) == 0))
364 				return (EACCES);
365 			nreno->beta_ecn = opt->val;
366 			break;
367 		default:
368 			return (ENOPROTOOPT);
369 		}
370 		break;
371 	case SOPT_GET:
372 		switch (opt->name) {
373 		case CC_NEWRENO_BETA:
374 			opt->val = (nreno == NULL) ?
375 			    V_newreno_beta : nreno->beta;
376 			break;
377 		case CC_NEWRENO_BETA_ECN:
378 			opt->val = (nreno == NULL) ?
379 			    V_newreno_beta_ecn : nreno->beta_ecn;
380 			break;
381 		default:
382 			return (ENOPROTOOPT);
383 		}
384 		break;
385 	default:
386 		return (EINVAL);
387 	}
388 
389 	return (0);
390 }
391 
392 static int
393 newreno_beta_handler(SYSCTL_HANDLER_ARGS)
394 {
395 	int error;
396 	uint32_t new;
397 
398 	new = *(uint32_t *)arg1;
399 	error = sysctl_handle_int(oidp, &new, 0, req);
400 	if (error == 0 && req->newptr != NULL ) {
401 		if (arg1 == &VNET_NAME(newreno_beta_ecn) && !V_cc_do_abe)
402 			error = EACCES;
403 		else if (new == 0 || new > 100)
404 			error = EINVAL;
405 		else
406 			*(uint32_t *)arg1 = new;
407 	}
408 
409 	return (error);
410 }
411 
412 SYSCTL_DECL(_net_inet_tcp_cc_newreno);
413 SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, newreno,
414     CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
415     "New Reno related settings");
416 
417 SYSCTL_PROC(_net_inet_tcp_cc_newreno, OID_AUTO, beta,
418     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
419     &VNET_NAME(newreno_beta), 3, &newreno_beta_handler, "IU",
420     "New Reno beta, specified as number between 1 and 100");
421 
422 SYSCTL_PROC(_net_inet_tcp_cc_newreno, OID_AUTO, beta_ecn,
423     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
424     &VNET_NAME(newreno_beta_ecn), 3, &newreno_beta_handler, "IU",
425     "New Reno beta ecn, specified as number between 1 and 100");
426 
427 DECLARE_CC_MODULE(newreno, &newreno_cc_algo);
428 MODULE_VERSION(newreno, 1);
429