xref: /freebsd/sys/netinet/cc/cc_newreno.c (revision 56e53cb8ef000c3ef72337a4095987a932cdedef)
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
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 
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
55 
56 #include <sys/param.h>
57 #include <sys/kernel.h>
58 #include <sys/malloc.h>
59 #include <sys/module.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/sysctl.h>
63 #include <sys/systm.h>
64 
65 #include <net/vnet.h>
66 
67 #include <netinet/tcp.h>
68 #include <netinet/tcp_seq.h>
69 #include <netinet/tcp_var.h>
70 #include <netinet/cc/cc.h>
71 #include <netinet/cc/cc_module.h>
72 
73 static void	newreno_ack_received(struct cc_var *ccv, uint16_t type);
74 static void	newreno_after_idle(struct cc_var *ccv);
75 static void	newreno_cong_signal(struct cc_var *ccv, uint32_t type);
76 static void	newreno_post_recovery(struct cc_var *ccv);
77 
78 struct cc_algo newreno_cc_algo = {
79 	.name = "newreno",
80 	.ack_received = newreno_ack_received,
81 	.after_idle = newreno_after_idle,
82 	.cong_signal = newreno_cong_signal,
83 	.post_recovery = newreno_post_recovery,
84 };
85 
86 static void
87 newreno_ack_received(struct cc_var *ccv, uint16_t type)
88 {
89 	if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
90 	    (ccv->flags & CCF_CWND_LIMITED)) {
91 		u_int cw = CCV(ccv, snd_cwnd);
92 		u_int incr = CCV(ccv, t_maxseg);
93 
94 		/*
95 		 * Regular in-order ACK, open the congestion window.
96 		 * Method depends on which congestion control state we're
97 		 * in (slow start or cong avoid) and if ABC (RFC 3465) is
98 		 * enabled.
99 		 *
100 		 * slow start: cwnd <= ssthresh
101 		 * cong avoid: cwnd > ssthresh
102 		 *
103 		 * slow start and ABC (RFC 3465):
104 		 *   Grow cwnd exponentially by the amount of data
105 		 *   ACKed capping the max increment per ACK to
106 		 *   (abc_l_var * maxseg) bytes.
107 		 *
108 		 * slow start without ABC (RFC 5681):
109 		 *   Grow cwnd exponentially by maxseg per ACK.
110 		 *
111 		 * cong avoid and ABC (RFC 3465):
112 		 *   Grow cwnd linearly by maxseg per RTT for each
113 		 *   cwnd worth of ACKed data.
114 		 *
115 		 * cong avoid without ABC (RFC 5681):
116 		 *   Grow cwnd linearly by approximately maxseg per RTT using
117 		 *   maxseg^2 / cwnd per ACK as the increment.
118 		 *   If cwnd > maxseg^2, fix the cwnd increment at 1 byte to
119 		 *   avoid capping cwnd.
120 		 */
121 		if (cw > CCV(ccv, snd_ssthresh)) {
122 			if (V_tcp_do_rfc3465) {
123 				if (ccv->flags & CCF_ABC_SENTAWND)
124 					ccv->flags &= ~CCF_ABC_SENTAWND;
125 				else
126 					incr = 0;
127 			} else
128 				incr = max((incr * incr / cw), 1);
129 		} else if (V_tcp_do_rfc3465) {
130 			/*
131 			 * In slow-start with ABC enabled and no RTO in sight?
132 			 * (Must not use abc_l_var > 1 if slow starting after
133 			 * an RTO. On RTO, snd_nxt = snd_una, so the
134 			 * snd_nxt == snd_max check is sufficient to
135 			 * handle this).
136 			 *
137 			 * XXXLAS: Find a way to signal SS after RTO that
138 			 * doesn't rely on tcpcb vars.
139 			 */
140 			if (CCV(ccv, snd_nxt) == CCV(ccv, snd_max))
141 				incr = min(ccv->bytes_this_ack,
142 				    ccv->nsegs * V_tcp_abc_l_var *
143 				    CCV(ccv, t_maxseg));
144 			else
145 				incr = min(ccv->bytes_this_ack, CCV(ccv, t_maxseg));
146 		}
147 		/* ABC is on by default, so incr equals 0 frequently. */
148 		if (incr > 0)
149 			CCV(ccv, snd_cwnd) = min(cw + incr,
150 			    TCP_MAXWIN << CCV(ccv, snd_scale));
151 	}
152 }
153 
154 static void
155 newreno_after_idle(struct cc_var *ccv)
156 {
157 	int rw;
158 
159 	/*
160 	 * If we've been idle for more than one retransmit timeout the old
161 	 * congestion window is no longer current and we have to reduce it to
162 	 * the restart window before we can transmit again.
163 	 *
164 	 * The restart window is the initial window or the last CWND, whichever
165 	 * is smaller.
166 	 *
167 	 * This is done to prevent us from flooding the path with a full CWND at
168 	 * wirespeed, overloading router and switch buffers along the way.
169 	 *
170 	 * See RFC5681 Section 4.1. "Restarting Idle Connections".
171 	 */
172 	if (V_tcp_do_rfc3390)
173 		rw = min(4 * CCV(ccv, t_maxseg),
174 		    max(2 * CCV(ccv, t_maxseg), 4380));
175 	else
176 		rw = CCV(ccv, t_maxseg) * 2;
177 
178 	CCV(ccv, snd_cwnd) = min(rw, CCV(ccv, snd_cwnd));
179 }
180 
181 /*
182  * Perform any necessary tasks before we enter congestion recovery.
183  */
184 static void
185 newreno_cong_signal(struct cc_var *ccv, uint32_t type)
186 {
187 	u_int win;
188 
189 	/* Catch algos which mistakenly leak private signal types. */
190 	KASSERT((type & CC_SIGPRIVMASK) == 0,
191 	    ("%s: congestion signal type 0x%08x is private\n", __func__, type));
192 
193 	win = max(CCV(ccv, snd_cwnd) / 2 / CCV(ccv, t_maxseg), 2) *
194 	    CCV(ccv, t_maxseg);
195 
196 	switch (type) {
197 	case CC_NDUPACK:
198 		if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) {
199 			if (!IN_CONGRECOVERY(CCV(ccv, t_flags)))
200 				CCV(ccv, snd_ssthresh) = win;
201 			ENTER_RECOVERY(CCV(ccv, t_flags));
202 		}
203 		break;
204 	case CC_ECN:
205 		if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
206 			CCV(ccv, snd_ssthresh) = win;
207 			CCV(ccv, snd_cwnd) = win;
208 			ENTER_CONGRECOVERY(CCV(ccv, t_flags));
209 		}
210 		break;
211 	}
212 }
213 
214 /*
215  * Perform any necessary tasks before we exit congestion recovery.
216  */
217 static void
218 newreno_post_recovery(struct cc_var *ccv)
219 {
220 	int pipe;
221 	pipe = 0;
222 
223 	if (IN_FASTRECOVERY(CCV(ccv, t_flags))) {
224 		/*
225 		 * Fast recovery will conclude after returning from this
226 		 * function. Window inflation should have left us with
227 		 * approximately snd_ssthresh outstanding data. But in case we
228 		 * would be inclined to send a burst, better to do it via the
229 		 * slow start mechanism.
230 		 *
231 		 * XXXLAS: Find a way to do this without needing curack
232 		 */
233 		if (V_tcp_do_rfc6675_pipe)
234 			pipe = tcp_compute_pipe(ccv->ccvc.tcp);
235 		else
236 			pipe = CCV(ccv, snd_max) - ccv->curack;
237 
238 		if (pipe < CCV(ccv, snd_ssthresh))
239 			CCV(ccv, snd_cwnd) = pipe + CCV(ccv, t_maxseg);
240 		else
241 			CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh);
242 	}
243 }
244 
245 
246 DECLARE_CC_MODULE(newreno, &newreno_cc_algo);
247