tcp_congctl.c revision 1.12.6.4 1 1.12.6.4 yamt /* $NetBSD: tcp_congctl.c,v 1.12.6.4 2008/03/17 09:15:41 yamt Exp $ */
2 1.12.6.2 yamt
3 1.12.6.2 yamt /*-
4 1.12.6.2 yamt * Copyright (c) 1997, 1998, 1999, 2001, 2005, 2006 The NetBSD Foundation, Inc.
5 1.12.6.2 yamt * All rights reserved.
6 1.12.6.2 yamt *
7 1.12.6.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.12.6.2 yamt * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
9 1.12.6.2 yamt * Facility, NASA Ames Research Center.
10 1.12.6.2 yamt * This code is derived from software contributed to The NetBSD Foundation
11 1.12.6.2 yamt * by Charles M. Hannum.
12 1.12.6.2 yamt * This code is derived from software contributed to The NetBSD Foundation
13 1.12.6.2 yamt * by Rui Paulo.
14 1.12.6.2 yamt *
15 1.12.6.2 yamt * Redistribution and use in source and binary forms, with or without
16 1.12.6.2 yamt * modification, are permitted provided that the following conditions
17 1.12.6.2 yamt * are met:
18 1.12.6.2 yamt * 1. Redistributions of source code must retain the above copyright
19 1.12.6.2 yamt * notice, this list of conditions and the following disclaimer.
20 1.12.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
21 1.12.6.2 yamt * notice, this list of conditions and the following disclaimer in the
22 1.12.6.2 yamt * documentation and/or other materials provided with the distribution.
23 1.12.6.2 yamt * 3. All advertising materials mentioning features or use of this software
24 1.12.6.2 yamt * must display the following acknowledgement:
25 1.12.6.2 yamt * This product includes software developed by the NetBSD
26 1.12.6.2 yamt * Foundation, Inc. and its contributors.
27 1.12.6.2 yamt * 4. Neither the name of The NetBSD Foundation nor the names of its
28 1.12.6.2 yamt * contributors may be used to endorse or promote products derived
29 1.12.6.2 yamt * from this software without specific prior written permission.
30 1.12.6.2 yamt *
31 1.12.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
32 1.12.6.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
33 1.12.6.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 1.12.6.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
35 1.12.6.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 1.12.6.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 1.12.6.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 1.12.6.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 1.12.6.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 1.12.6.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 1.12.6.2 yamt * POSSIBILITY OF SUCH DAMAGE.
42 1.12.6.2 yamt */
43 1.12.6.2 yamt
44 1.12.6.2 yamt /*
45 1.12.6.2 yamt * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
46 1.12.6.2 yamt * All rights reserved.
47 1.12.6.2 yamt *
48 1.12.6.2 yamt * Redistribution and use in source and binary forms, with or without
49 1.12.6.2 yamt * modification, are permitted provided that the following conditions
50 1.12.6.2 yamt * are met:
51 1.12.6.2 yamt * 1. Redistributions of source code must retain the above copyright
52 1.12.6.2 yamt * notice, this list of conditions and the following disclaimer.
53 1.12.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
54 1.12.6.2 yamt * notice, this list of conditions and the following disclaimer in the
55 1.12.6.2 yamt * documentation and/or other materials provided with the distribution.
56 1.12.6.2 yamt * 3. Neither the name of the project nor the names of its contributors
57 1.12.6.2 yamt * may be used to endorse or promote products derived from this software
58 1.12.6.2 yamt * without specific prior written permission.
59 1.12.6.2 yamt *
60 1.12.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
61 1.12.6.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 1.12.6.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 1.12.6.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
64 1.12.6.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 1.12.6.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 1.12.6.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 1.12.6.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 1.12.6.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 1.12.6.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 1.12.6.2 yamt * SUCH DAMAGE.
71 1.12.6.2 yamt */
72 1.12.6.2 yamt
73 1.12.6.2 yamt /*
74 1.12.6.2 yamt * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995
75 1.12.6.2 yamt *
76 1.12.6.2 yamt * NRL grants permission for redistribution and use in source and binary
77 1.12.6.2 yamt * forms, with or without modification, of the software and documentation
78 1.12.6.2 yamt * created at NRL provided that the following conditions are met:
79 1.12.6.2 yamt *
80 1.12.6.2 yamt * 1. Redistributions of source code must retain the above copyright
81 1.12.6.2 yamt * notice, this list of conditions and the following disclaimer.
82 1.12.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
83 1.12.6.2 yamt * notice, this list of conditions and the following disclaimer in the
84 1.12.6.2 yamt * documentation and/or other materials provided with the distribution.
85 1.12.6.2 yamt * 3. All advertising materials mentioning features or use of this software
86 1.12.6.2 yamt * must display the following acknowledgements:
87 1.12.6.2 yamt * This product includes software developed by the University of
88 1.12.6.2 yamt * California, Berkeley and its contributors.
89 1.12.6.2 yamt * This product includes software developed at the Information
90 1.12.6.2 yamt * Technology Division, US Naval Research Laboratory.
91 1.12.6.2 yamt * 4. Neither the name of the NRL nor the names of its contributors
92 1.12.6.2 yamt * may be used to endorse or promote products derived from this software
93 1.12.6.2 yamt * without specific prior written permission.
94 1.12.6.2 yamt *
95 1.12.6.2 yamt * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
96 1.12.6.2 yamt * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
97 1.12.6.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
98 1.12.6.2 yamt * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR
99 1.12.6.2 yamt * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
100 1.12.6.2 yamt * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
101 1.12.6.2 yamt * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
102 1.12.6.2 yamt * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
103 1.12.6.2 yamt * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
104 1.12.6.2 yamt * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
105 1.12.6.2 yamt * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
106 1.12.6.2 yamt *
107 1.12.6.2 yamt * The views and conclusions contained in the software and documentation
108 1.12.6.2 yamt * are those of the authors and should not be interpreted as representing
109 1.12.6.2 yamt * official policies, either expressed or implied, of the US Naval
110 1.12.6.2 yamt * Research Laboratory (NRL).
111 1.12.6.2 yamt */
112 1.12.6.2 yamt
113 1.12.6.2 yamt /*
114 1.12.6.2 yamt * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
115 1.12.6.2 yamt * The Regents of the University of California. All rights reserved.
116 1.12.6.2 yamt *
117 1.12.6.2 yamt * Redistribution and use in source and binary forms, with or without
118 1.12.6.2 yamt * modification, are permitted provided that the following conditions
119 1.12.6.2 yamt * are met:
120 1.12.6.2 yamt * 1. Redistributions of source code must retain the above copyright
121 1.12.6.2 yamt * notice, this list of conditions and the following disclaimer.
122 1.12.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
123 1.12.6.2 yamt * notice, this list of conditions and the following disclaimer in the
124 1.12.6.2 yamt * documentation and/or other materials provided with the distribution.
125 1.12.6.2 yamt * 3. Neither the name of the University nor the names of its contributors
126 1.12.6.2 yamt * may be used to endorse or promote products derived from this software
127 1.12.6.2 yamt * without specific prior written permission.
128 1.12.6.2 yamt *
129 1.12.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
130 1.12.6.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
131 1.12.6.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
132 1.12.6.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
133 1.12.6.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
134 1.12.6.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
135 1.12.6.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
136 1.12.6.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
137 1.12.6.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
138 1.12.6.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
139 1.12.6.2 yamt * SUCH DAMAGE.
140 1.12.6.2 yamt *
141 1.12.6.2 yamt * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
142 1.12.6.2 yamt */
143 1.12.6.2 yamt
144 1.12.6.2 yamt #include <sys/cdefs.h>
145 1.12.6.4 yamt __KERNEL_RCSID(0, "$NetBSD: tcp_congctl.c,v 1.12.6.4 2008/03/17 09:15:41 yamt Exp $");
146 1.12.6.2 yamt
147 1.12.6.2 yamt #include "opt_inet.h"
148 1.12.6.2 yamt #include "opt_tcp_debug.h"
149 1.12.6.2 yamt #include "opt_tcp_congctl.h"
150 1.12.6.2 yamt
151 1.12.6.2 yamt #include <sys/param.h>
152 1.12.6.2 yamt #include <sys/systm.h>
153 1.12.6.2 yamt #include <sys/malloc.h>
154 1.12.6.2 yamt #include <sys/mbuf.h>
155 1.12.6.2 yamt #include <sys/protosw.h>
156 1.12.6.2 yamt #include <sys/socket.h>
157 1.12.6.2 yamt #include <sys/socketvar.h>
158 1.12.6.2 yamt #include <sys/errno.h>
159 1.12.6.2 yamt #include <sys/syslog.h>
160 1.12.6.2 yamt #include <sys/pool.h>
161 1.12.6.2 yamt #include <sys/domain.h>
162 1.12.6.2 yamt #include <sys/kernel.h>
163 1.12.6.3 yamt #include <sys/mutex.h>
164 1.12.6.2 yamt
165 1.12.6.2 yamt #include <net/if.h>
166 1.12.6.2 yamt #include <net/route.h>
167 1.12.6.2 yamt
168 1.12.6.2 yamt #include <netinet/in.h>
169 1.12.6.2 yamt #include <netinet/in_systm.h>
170 1.12.6.2 yamt #include <netinet/ip.h>
171 1.12.6.2 yamt #include <netinet/in_pcb.h>
172 1.12.6.2 yamt #include <netinet/in_var.h>
173 1.12.6.2 yamt #include <netinet/ip_var.h>
174 1.12.6.2 yamt
175 1.12.6.2 yamt #ifdef INET6
176 1.12.6.2 yamt #ifndef INET
177 1.12.6.2 yamt #include <netinet/in.h>
178 1.12.6.2 yamt #endif
179 1.12.6.2 yamt #include <netinet/ip6.h>
180 1.12.6.2 yamt #include <netinet6/ip6_var.h>
181 1.12.6.2 yamt #include <netinet6/in6_pcb.h>
182 1.12.6.2 yamt #include <netinet6/ip6_var.h>
183 1.12.6.2 yamt #include <netinet6/in6_var.h>
184 1.12.6.2 yamt #include <netinet/icmp6.h>
185 1.12.6.2 yamt #include <netinet6/nd6.h>
186 1.12.6.2 yamt #endif
187 1.12.6.2 yamt
188 1.12.6.2 yamt #include <netinet/tcp.h>
189 1.12.6.2 yamt #include <netinet/tcp_fsm.h>
190 1.12.6.2 yamt #include <netinet/tcp_seq.h>
191 1.12.6.2 yamt #include <netinet/tcp_timer.h>
192 1.12.6.2 yamt #include <netinet/tcp_var.h>
193 1.12.6.2 yamt #include <netinet/tcpip.h>
194 1.12.6.2 yamt #include <netinet/tcp_congctl.h>
195 1.12.6.2 yamt #ifdef TCP_DEBUG
196 1.12.6.2 yamt #include <netinet/tcp_debug.h>
197 1.12.6.2 yamt #endif
198 1.12.6.2 yamt
199 1.12.6.2 yamt /*
200 1.12.6.2 yamt * TODO:
201 1.12.6.2 yamt * consider separating the actual implementations in another file.
202 1.12.6.2 yamt */
203 1.12.6.2 yamt
204 1.12.6.2 yamt static int tcp_reno_fast_retransmit(struct tcpcb *, const struct tcphdr *);
205 1.12.6.2 yamt static void tcp_reno_slow_retransmit(struct tcpcb *);
206 1.12.6.2 yamt static void tcp_reno_fast_retransmit_newack(struct tcpcb *,
207 1.12.6.2 yamt const struct tcphdr *);
208 1.12.6.2 yamt static void tcp_reno_newack(struct tcpcb *, const struct tcphdr *);
209 1.12.6.2 yamt static void tcp_reno_congestion_exp(struct tcpcb *tp);
210 1.12.6.2 yamt
211 1.12.6.2 yamt static int tcp_newreno_fast_retransmit(struct tcpcb *, const struct tcphdr *);
212 1.12.6.2 yamt static void tcp_newreno_fast_retransmit_newack(struct tcpcb *,
213 1.12.6.2 yamt const struct tcphdr *);
214 1.12.6.2 yamt static void tcp_newreno_newack(struct tcpcb *, const struct tcphdr *);
215 1.12.6.2 yamt
216 1.12.6.2 yamt
217 1.12.6.2 yamt static void tcp_congctl_fillnames(void);
218 1.12.6.2 yamt
219 1.12.6.2 yamt extern int tcprexmtthresh;
220 1.12.6.2 yamt
221 1.12.6.2 yamt MALLOC_DEFINE(M_TCPCONGCTL, "tcpcongctl", "TCP congestion control structures");
222 1.12.6.2 yamt
223 1.12.6.4 yamt /* currently selected global congestion control */
224 1.12.6.4 yamt char tcp_congctl_global_name[TCPCC_MAXLEN];
225 1.12.6.4 yamt
226 1.12.6.4 yamt /* available global congestion control algorithms */
227 1.12.6.4 yamt char tcp_congctl_avail[10 * TCPCC_MAXLEN];
228 1.12.6.4 yamt
229 1.12.6.2 yamt /*
230 1.12.6.2 yamt * Used to list the available congestion control algorithms.
231 1.12.6.2 yamt */
232 1.12.6.4 yamt TAILQ_HEAD(, tcp_congctlent) tcp_congctlhd =
233 1.12.6.4 yamt TAILQ_HEAD_INITIALIZER(tcp_congctlhd);
234 1.12.6.4 yamt
235 1.12.6.4 yamt static struct tcp_congctlent * tcp_congctl_global;
236 1.12.6.2 yamt
237 1.12.6.3 yamt static kmutex_t tcp_congctl_mtx;
238 1.12.6.2 yamt
239 1.12.6.2 yamt void
240 1.12.6.2 yamt tcp_congctl_init(void)
241 1.12.6.2 yamt {
242 1.12.6.2 yamt int r;
243 1.12.6.2 yamt
244 1.12.6.3 yamt mutex_init(&tcp_congctl_mtx, MUTEX_DEFAULT, IPL_NONE);
245 1.12.6.2 yamt
246 1.12.6.2 yamt /* Base algorithms. */
247 1.12.6.2 yamt r = tcp_congctl_register("reno", &tcp_reno_ctl);
248 1.12.6.2 yamt KASSERT(r == 0);
249 1.12.6.2 yamt r = tcp_congctl_register("newreno", &tcp_newreno_ctl);
250 1.12.6.2 yamt KASSERT(r == 0);
251 1.12.6.2 yamt
252 1.12.6.2 yamt /* NewReno is the default. */
253 1.12.6.2 yamt #ifndef TCP_CONGCTL_DEFAULT
254 1.12.6.2 yamt #define TCP_CONGCTL_DEFAULT "newreno"
255 1.12.6.2 yamt #endif
256 1.12.6.2 yamt
257 1.12.6.2 yamt r = tcp_congctl_select(NULL, TCP_CONGCTL_DEFAULT);
258 1.12.6.2 yamt KASSERT(r == 0);
259 1.12.6.2 yamt }
260 1.12.6.2 yamt
261 1.12.6.2 yamt /*
262 1.12.6.2 yamt * Register a congestion algorithm and select it if we have none.
263 1.12.6.2 yamt */
264 1.12.6.2 yamt int
265 1.12.6.4 yamt tcp_congctl_register(const char *name, const struct tcp_congctl *tcc)
266 1.12.6.2 yamt {
267 1.12.6.2 yamt struct tcp_congctlent *ntcc, *tccp;
268 1.12.6.2 yamt
269 1.12.6.2 yamt TAILQ_FOREACH(tccp, &tcp_congctlhd, congctl_ent)
270 1.12.6.2 yamt if (!strcmp(name, tccp->congctl_name)) {
271 1.12.6.2 yamt /* name already registered */
272 1.12.6.2 yamt return EEXIST;
273 1.12.6.2 yamt }
274 1.12.6.2 yamt
275 1.12.6.4 yamt ntcc = malloc(sizeof(*ntcc), M_TCPCONGCTL, M_WAITOK|M_ZERO);
276 1.12.6.2 yamt
277 1.12.6.2 yamt strlcpy(ntcc->congctl_name, name, sizeof(ntcc->congctl_name) - 1);
278 1.12.6.2 yamt ntcc->congctl_ctl = tcc;
279 1.12.6.2 yamt
280 1.12.6.2 yamt TAILQ_INSERT_TAIL(&tcp_congctlhd, ntcc, congctl_ent);
281 1.12.6.2 yamt tcp_congctl_fillnames();
282 1.12.6.2 yamt
283 1.12.6.2 yamt if (TAILQ_FIRST(&tcp_congctlhd) == ntcc)
284 1.12.6.2 yamt tcp_congctl_select(NULL, name);
285 1.12.6.2 yamt
286 1.12.6.2 yamt return 0;
287 1.12.6.2 yamt }
288 1.12.6.2 yamt
289 1.12.6.2 yamt int
290 1.12.6.2 yamt tcp_congctl_unregister(const char *name)
291 1.12.6.2 yamt {
292 1.12.6.2 yamt struct tcp_congctlent *tccp, *rtccp;
293 1.12.6.2 yamt unsigned int size;
294 1.12.6.2 yamt
295 1.12.6.2 yamt rtccp = NULL;
296 1.12.6.2 yamt size = 0;
297 1.12.6.2 yamt TAILQ_FOREACH(tccp, &tcp_congctlhd, congctl_ent) {
298 1.12.6.2 yamt if (!strcmp(name, tccp->congctl_name))
299 1.12.6.2 yamt rtccp = tccp;
300 1.12.6.2 yamt size++;
301 1.12.6.2 yamt }
302 1.12.6.2 yamt
303 1.12.6.2 yamt if (!rtccp)
304 1.12.6.2 yamt return ENOENT;
305 1.12.6.2 yamt
306 1.12.6.4 yamt if (size <= 1 || tcp_congctl_global == rtccp || rtccp->congctl_refcnt)
307 1.12.6.2 yamt return EBUSY;
308 1.12.6.2 yamt
309 1.12.6.2 yamt TAILQ_REMOVE(&tcp_congctlhd, rtccp, congctl_ent);
310 1.12.6.2 yamt free(rtccp, M_TCPCONGCTL);
311 1.12.6.2 yamt tcp_congctl_fillnames();
312 1.12.6.2 yamt
313 1.12.6.2 yamt return 0;
314 1.12.6.2 yamt }
315 1.12.6.2 yamt
316 1.12.6.2 yamt /*
317 1.12.6.2 yamt * Select a congestion algorithm by name.
318 1.12.6.2 yamt */
319 1.12.6.2 yamt int
320 1.12.6.2 yamt tcp_congctl_select(struct tcpcb *tp, const char *name)
321 1.12.6.2 yamt {
322 1.12.6.4 yamt struct tcp_congctlent *tccp, *old_tccp, *new_tccp;
323 1.12.6.4 yamt bool old_found, new_found;
324 1.12.6.2 yamt
325 1.12.6.2 yamt KASSERT(name);
326 1.12.6.2 yamt
327 1.12.6.4 yamt old_found = (tp == NULL || tp->t_congctl == NULL);
328 1.12.6.4 yamt old_tccp = NULL;
329 1.12.6.4 yamt new_found = false;
330 1.12.6.4 yamt new_tccp = NULL;
331 1.12.6.4 yamt
332 1.12.6.4 yamt TAILQ_FOREACH(tccp, &tcp_congctlhd, congctl_ent) {
333 1.12.6.4 yamt if (!old_found && tccp->congctl_ctl == tp->t_congctl) {
334 1.12.6.4 yamt old_tccp = tccp;
335 1.12.6.4 yamt old_found = true;
336 1.12.6.4 yamt }
337 1.12.6.4 yamt
338 1.12.6.4 yamt if (!new_found && !strcmp(name, tccp->congctl_name)) {
339 1.12.6.4 yamt new_tccp = tccp;
340 1.12.6.4 yamt new_found = true;
341 1.12.6.4 yamt }
342 1.12.6.4 yamt
343 1.12.6.4 yamt if (new_found && old_found) {
344 1.12.6.2 yamt if (tp) {
345 1.12.6.3 yamt mutex_enter(&tcp_congctl_mtx);
346 1.12.6.4 yamt if (old_tccp)
347 1.12.6.4 yamt old_tccp->congctl_refcnt--;
348 1.12.6.4 yamt tp->t_congctl = new_tccp->congctl_ctl;
349 1.12.6.4 yamt new_tccp->congctl_refcnt++;
350 1.12.6.3 yamt mutex_exit(&tcp_congctl_mtx);
351 1.12.6.2 yamt } else {
352 1.12.6.4 yamt tcp_congctl_global = new_tccp;
353 1.12.6.2 yamt strlcpy(tcp_congctl_global_name,
354 1.12.6.4 yamt new_tccp->congctl_name,
355 1.12.6.2 yamt sizeof(tcp_congctl_global_name) - 1);
356 1.12.6.2 yamt }
357 1.12.6.2 yamt return 0;
358 1.12.6.2 yamt }
359 1.12.6.4 yamt }
360 1.12.6.4 yamt
361 1.12.6.2 yamt return EINVAL;
362 1.12.6.2 yamt }
363 1.12.6.2 yamt
364 1.12.6.4 yamt void
365 1.12.6.4 yamt tcp_congctl_release(struct tcpcb *tp)
366 1.12.6.4 yamt {
367 1.12.6.4 yamt struct tcp_congctlent *tccp;
368 1.12.6.4 yamt
369 1.12.6.4 yamt KASSERT(tp->t_congctl);
370 1.12.6.4 yamt
371 1.12.6.4 yamt TAILQ_FOREACH(tccp, &tcp_congctlhd, congctl_ent) {
372 1.12.6.4 yamt if (tccp->congctl_ctl == tp->t_congctl) {
373 1.12.6.4 yamt tccp->congctl_refcnt--;
374 1.12.6.4 yamt return;
375 1.12.6.4 yamt }
376 1.12.6.4 yamt }
377 1.12.6.4 yamt }
378 1.12.6.4 yamt
379 1.12.6.2 yamt /*
380 1.12.6.2 yamt * Returns the name of a congestion algorithm.
381 1.12.6.2 yamt */
382 1.12.6.2 yamt const char *
383 1.12.6.2 yamt tcp_congctl_bystruct(const struct tcp_congctl *tcc)
384 1.12.6.2 yamt {
385 1.12.6.2 yamt struct tcp_congctlent *tccp;
386 1.12.6.2 yamt
387 1.12.6.2 yamt KASSERT(tcc);
388 1.12.6.2 yamt
389 1.12.6.2 yamt TAILQ_FOREACH(tccp, &tcp_congctlhd, congctl_ent)
390 1.12.6.2 yamt if (tccp->congctl_ctl == tcc)
391 1.12.6.2 yamt return tccp->congctl_name;
392 1.12.6.2 yamt
393 1.12.6.2 yamt return NULL;
394 1.12.6.2 yamt }
395 1.12.6.2 yamt
396 1.12.6.2 yamt static void
397 1.12.6.2 yamt tcp_congctl_fillnames(void)
398 1.12.6.2 yamt {
399 1.12.6.2 yamt struct tcp_congctlent *tccp;
400 1.12.6.2 yamt const char *delim = " ";
401 1.12.6.2 yamt
402 1.12.6.2 yamt tcp_congctl_avail[0] = '\0';
403 1.12.6.2 yamt TAILQ_FOREACH(tccp, &tcp_congctlhd, congctl_ent) {
404 1.12.6.2 yamt strlcat(tcp_congctl_avail, tccp->congctl_name,
405 1.12.6.2 yamt sizeof(tcp_congctl_avail) - 1);
406 1.12.6.2 yamt if (TAILQ_NEXT(tccp, congctl_ent))
407 1.12.6.2 yamt strlcat(tcp_congctl_avail, delim,
408 1.12.6.2 yamt sizeof(tcp_congctl_avail) - 1);
409 1.12.6.2 yamt }
410 1.12.6.2 yamt
411 1.12.6.2 yamt }
412 1.12.6.2 yamt
413 1.12.6.2 yamt /* ------------------------------------------------------------------------ */
414 1.12.6.2 yamt
415 1.12.6.2 yamt /*
416 1.12.6.2 yamt * TCP/Reno congestion control.
417 1.12.6.2 yamt */
418 1.12.6.2 yamt static void
419 1.12.6.2 yamt tcp_reno_congestion_exp(struct tcpcb *tp)
420 1.12.6.2 yamt {
421 1.12.6.2 yamt u_int win;
422 1.12.6.2 yamt
423 1.12.6.2 yamt /*
424 1.12.6.2 yamt * Halve the congestion window and reduce the
425 1.12.6.2 yamt * slow start threshold.
426 1.12.6.2 yamt */
427 1.12.6.2 yamt win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_segsz;
428 1.12.6.2 yamt if (win < 2)
429 1.12.6.2 yamt win = 2;
430 1.12.6.2 yamt
431 1.12.6.2 yamt tp->snd_ssthresh = win * tp->t_segsz;
432 1.12.6.2 yamt tp->snd_recover = tp->snd_max;
433 1.12.6.2 yamt tp->snd_cwnd = tp->snd_ssthresh;
434 1.12.6.2 yamt
435 1.12.6.2 yamt /*
436 1.12.6.2 yamt * When using TCP ECN, notify the peer that
437 1.12.6.2 yamt * we reduced the cwnd.
438 1.12.6.2 yamt */
439 1.12.6.2 yamt if (TCP_ECN_ALLOWED(tp))
440 1.12.6.2 yamt tp->t_flags |= TF_ECN_SND_CWR;
441 1.12.6.2 yamt }
442 1.12.6.2 yamt
443 1.12.6.2 yamt
444 1.12.6.2 yamt
445 1.12.6.2 yamt static int
446 1.12.6.2 yamt tcp_reno_fast_retransmit(struct tcpcb *tp, const struct tcphdr *th)
447 1.12.6.2 yamt {
448 1.12.6.2 yamt /*
449 1.12.6.2 yamt * We know we're losing at the current
450 1.12.6.2 yamt * window size so do congestion avoidance
451 1.12.6.2 yamt * (set ssthresh to half the current window
452 1.12.6.2 yamt * and pull our congestion window back to
453 1.12.6.2 yamt * the new ssthresh).
454 1.12.6.2 yamt *
455 1.12.6.2 yamt * Dup acks mean that packets have left the
456 1.12.6.2 yamt * network (they're now cached at the receiver)
457 1.12.6.2 yamt * so bump cwnd by the amount in the receiver
458 1.12.6.2 yamt * to keep a constant cwnd packets in the
459 1.12.6.2 yamt * network.
460 1.12.6.2 yamt *
461 1.12.6.2 yamt * If we are using TCP/SACK, then enter
462 1.12.6.2 yamt * Fast Recovery if the receiver SACKs
463 1.12.6.2 yamt * data that is tcprexmtthresh * MSS
464 1.12.6.2 yamt * bytes past the last ACKed segment,
465 1.12.6.2 yamt * irrespective of the number of DupAcks.
466 1.12.6.2 yamt */
467 1.12.6.2 yamt
468 1.12.6.2 yamt tcp_seq onxt;
469 1.12.6.2 yamt
470 1.12.6.2 yamt onxt = tp->snd_nxt;
471 1.12.6.2 yamt tcp_reno_congestion_exp(tp);
472 1.12.6.2 yamt tp->t_partialacks = 0;
473 1.12.6.2 yamt TCP_TIMER_DISARM(tp, TCPT_REXMT);
474 1.12.6.2 yamt tp->t_rtttime = 0;
475 1.12.6.2 yamt if (TCP_SACK_ENABLED(tp)) {
476 1.12.6.2 yamt tp->t_dupacks = tcprexmtthresh;
477 1.12.6.2 yamt tp->sack_newdata = tp->snd_nxt;
478 1.12.6.2 yamt tp->snd_cwnd = tp->t_segsz;
479 1.12.6.2 yamt (void) tcp_output(tp);
480 1.12.6.2 yamt return 0;
481 1.12.6.2 yamt }
482 1.12.6.2 yamt tp->snd_nxt = th->th_ack;
483 1.12.6.2 yamt tp->snd_cwnd = tp->t_segsz;
484 1.12.6.2 yamt (void) tcp_output(tp);
485 1.12.6.2 yamt tp->snd_cwnd = tp->snd_ssthresh + tp->t_segsz * tp->t_dupacks;
486 1.12.6.2 yamt if (SEQ_GT(onxt, tp->snd_nxt))
487 1.12.6.2 yamt tp->snd_nxt = onxt;
488 1.12.6.2 yamt
489 1.12.6.2 yamt return 0;
490 1.12.6.2 yamt }
491 1.12.6.2 yamt
492 1.12.6.2 yamt static void
493 1.12.6.2 yamt tcp_reno_slow_retransmit(struct tcpcb *tp)
494 1.12.6.2 yamt {
495 1.12.6.2 yamt u_int win;
496 1.12.6.2 yamt
497 1.12.6.2 yamt /*
498 1.12.6.2 yamt * Close the congestion window down to one segment
499 1.12.6.2 yamt * (we'll open it by one segment for each ack we get).
500 1.12.6.2 yamt * Since we probably have a window's worth of unacked
501 1.12.6.2 yamt * data accumulated, this "slow start" keeps us from
502 1.12.6.2 yamt * dumping all that data as back-to-back packets (which
503 1.12.6.2 yamt * might overwhelm an intermediate gateway).
504 1.12.6.2 yamt *
505 1.12.6.2 yamt * There are two phases to the opening: Initially we
506 1.12.6.2 yamt * open by one mss on each ack. This makes the window
507 1.12.6.2 yamt * size increase exponentially with time. If the
508 1.12.6.2 yamt * window is larger than the path can handle, this
509 1.12.6.2 yamt * exponential growth results in dropped packet(s)
510 1.12.6.2 yamt * almost immediately. To get more time between
511 1.12.6.2 yamt * drops but still "push" the network to take advantage
512 1.12.6.2 yamt * of improving conditions, we switch from exponential
513 1.12.6.2 yamt * to linear window opening at some threshhold size.
514 1.12.6.2 yamt * For a threshhold, we use half the current window
515 1.12.6.2 yamt * size, truncated to a multiple of the mss.
516 1.12.6.2 yamt *
517 1.12.6.2 yamt * (the minimum cwnd that will give us exponential
518 1.12.6.2 yamt * growth is 2 mss. We don't allow the threshhold
519 1.12.6.2 yamt * to go below this.)
520 1.12.6.2 yamt */
521 1.12.6.2 yamt
522 1.12.6.2 yamt win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_segsz;
523 1.12.6.2 yamt if (win < 2)
524 1.12.6.2 yamt win = 2;
525 1.12.6.2 yamt /* Loss Window MUST be one segment. */
526 1.12.6.2 yamt tp->snd_cwnd = tp->t_segsz;
527 1.12.6.2 yamt tp->snd_ssthresh = win * tp->t_segsz;
528 1.12.6.2 yamt tp->t_partialacks = -1;
529 1.12.6.2 yamt tp->t_dupacks = 0;
530 1.12.6.2 yamt tp->t_bytes_acked = 0;
531 1.12.6.2 yamt }
532 1.12.6.2 yamt
533 1.12.6.2 yamt static void
534 1.12.6.2 yamt tcp_reno_fast_retransmit_newack(struct tcpcb *tp,
535 1.12.6.2 yamt const struct tcphdr *th)
536 1.12.6.2 yamt {
537 1.12.6.2 yamt if (tp->t_partialacks < 0) {
538 1.12.6.2 yamt /*
539 1.12.6.2 yamt * We were not in fast recovery. Reset the duplicate ack
540 1.12.6.2 yamt * counter.
541 1.12.6.2 yamt */
542 1.12.6.2 yamt tp->t_dupacks = 0;
543 1.12.6.2 yamt } else {
544 1.12.6.2 yamt /*
545 1.12.6.2 yamt * Clamp the congestion window to the crossover point and
546 1.12.6.2 yamt * exit fast recovery.
547 1.12.6.2 yamt */
548 1.12.6.2 yamt if (tp->snd_cwnd > tp->snd_ssthresh)
549 1.12.6.2 yamt tp->snd_cwnd = tp->snd_ssthresh;
550 1.12.6.2 yamt tp->t_partialacks = -1;
551 1.12.6.2 yamt tp->t_dupacks = 0;
552 1.12.6.2 yamt tp->t_bytes_acked = 0;
553 1.12.6.2 yamt }
554 1.12.6.2 yamt }
555 1.12.6.2 yamt
556 1.12.6.2 yamt static void
557 1.12.6.2 yamt tcp_reno_newack(struct tcpcb *tp, const struct tcphdr *th)
558 1.12.6.2 yamt {
559 1.12.6.2 yamt /*
560 1.12.6.2 yamt * When new data is acked, open the congestion window.
561 1.12.6.2 yamt */
562 1.12.6.2 yamt
563 1.12.6.2 yamt u_int cw = tp->snd_cwnd;
564 1.12.6.2 yamt u_int incr = tp->t_segsz;
565 1.12.6.2 yamt
566 1.12.6.2 yamt if (tcp_do_abc) {
567 1.12.6.2 yamt
568 1.12.6.2 yamt /*
569 1.12.6.2 yamt * RFC 3465 Appropriate Byte Counting (ABC)
570 1.12.6.2 yamt */
571 1.12.6.2 yamt
572 1.12.6.2 yamt int acked = th->th_ack - tp->snd_una;
573 1.12.6.2 yamt
574 1.12.6.2 yamt if (cw >= tp->snd_ssthresh) {
575 1.12.6.2 yamt tp->t_bytes_acked += acked;
576 1.12.6.2 yamt if (tp->t_bytes_acked >= cw) {
577 1.12.6.2 yamt /* Time to increase the window. */
578 1.12.6.2 yamt tp->t_bytes_acked -= cw;
579 1.12.6.2 yamt } else {
580 1.12.6.2 yamt /* No need to increase yet. */
581 1.12.6.2 yamt incr = 0;
582 1.12.6.2 yamt }
583 1.12.6.2 yamt } else {
584 1.12.6.2 yamt /*
585 1.12.6.2 yamt * use 2*SMSS or 1*SMSS for the "L" param,
586 1.12.6.2 yamt * depending on sysctl setting.
587 1.12.6.2 yamt *
588 1.12.6.2 yamt * (See RFC 3465 2.3 Choosing the Limit)
589 1.12.6.2 yamt */
590 1.12.6.2 yamt u_int abc_lim;
591 1.12.6.2 yamt
592 1.12.6.2 yamt abc_lim = (tcp_abc_aggressive == 0 ||
593 1.12.6.2 yamt tp->snd_nxt != tp->snd_max) ? incr : incr * 2;
594 1.12.6.2 yamt incr = min(acked, abc_lim);
595 1.12.6.2 yamt }
596 1.12.6.2 yamt } else {
597 1.12.6.2 yamt
598 1.12.6.2 yamt /*
599 1.12.6.2 yamt * If the window gives us less than ssthresh packets
600 1.12.6.2 yamt * in flight, open exponentially (segsz per packet).
601 1.12.6.2 yamt * Otherwise open linearly: segsz per window
602 1.12.6.2 yamt * (segsz^2 / cwnd per packet).
603 1.12.6.2 yamt */
604 1.12.6.2 yamt
605 1.12.6.2 yamt if (cw >= tp->snd_ssthresh) {
606 1.12.6.2 yamt incr = incr * incr / cw;
607 1.12.6.2 yamt }
608 1.12.6.2 yamt }
609 1.12.6.2 yamt
610 1.12.6.2 yamt tp->snd_cwnd = min(cw + incr, TCP_MAXWIN << tp->snd_scale);
611 1.12.6.2 yamt }
612 1.12.6.2 yamt
613 1.12.6.4 yamt const struct tcp_congctl tcp_reno_ctl = {
614 1.12.6.2 yamt .fast_retransmit = tcp_reno_fast_retransmit,
615 1.12.6.2 yamt .slow_retransmit = tcp_reno_slow_retransmit,
616 1.12.6.2 yamt .fast_retransmit_newack = tcp_reno_fast_retransmit_newack,
617 1.12.6.2 yamt .newack = tcp_reno_newack,
618 1.12.6.2 yamt .cong_exp = tcp_reno_congestion_exp,
619 1.12.6.2 yamt };
620 1.12.6.2 yamt
621 1.12.6.2 yamt /*
622 1.12.6.2 yamt * TCP/NewReno Congestion control.
623 1.12.6.2 yamt */
624 1.12.6.2 yamt static int
625 1.12.6.2 yamt tcp_newreno_fast_retransmit(struct tcpcb *tp, const struct tcphdr *th)
626 1.12.6.2 yamt {
627 1.12.6.2 yamt if (SEQ_LT(th->th_ack, tp->snd_high)) {
628 1.12.6.2 yamt /*
629 1.12.6.2 yamt * False fast retransmit after timeout.
630 1.12.6.2 yamt * Do not enter fast recovery
631 1.12.6.2 yamt */
632 1.12.6.2 yamt tp->t_dupacks = 0;
633 1.12.6.2 yamt return 1;
634 1.12.6.2 yamt } else {
635 1.12.6.2 yamt /*
636 1.12.6.2 yamt * Fast retransmit is same as reno.
637 1.12.6.2 yamt */
638 1.12.6.2 yamt return tcp_reno_fast_retransmit(tp, th);
639 1.12.6.2 yamt }
640 1.12.6.2 yamt
641 1.12.6.2 yamt return 0;
642 1.12.6.2 yamt }
643 1.12.6.2 yamt
644 1.12.6.2 yamt /*
645 1.12.6.2 yamt * Implement the NewReno response to a new ack, checking for partial acks in
646 1.12.6.2 yamt * fast recovery.
647 1.12.6.2 yamt */
648 1.12.6.2 yamt static void
649 1.12.6.2 yamt tcp_newreno_fast_retransmit_newack(struct tcpcb *tp, const struct tcphdr *th)
650 1.12.6.2 yamt {
651 1.12.6.2 yamt if (tp->t_partialacks < 0) {
652 1.12.6.2 yamt /*
653 1.12.6.2 yamt * We were not in fast recovery. Reset the duplicate ack
654 1.12.6.2 yamt * counter.
655 1.12.6.2 yamt */
656 1.12.6.2 yamt tp->t_dupacks = 0;
657 1.12.6.2 yamt } else if (SEQ_LT(th->th_ack, tp->snd_recover)) {
658 1.12.6.2 yamt /*
659 1.12.6.2 yamt * This is a partial ack. Retransmit the first unacknowledged
660 1.12.6.2 yamt * segment and deflate the congestion window by the amount of
661 1.12.6.2 yamt * acknowledged data. Do not exit fast recovery.
662 1.12.6.2 yamt */
663 1.12.6.2 yamt tcp_seq onxt = tp->snd_nxt;
664 1.12.6.2 yamt u_long ocwnd = tp->snd_cwnd;
665 1.12.6.2 yamt
666 1.12.6.2 yamt /*
667 1.12.6.2 yamt * snd_una has not yet been updated and the socket's send
668 1.12.6.2 yamt * buffer has not yet drained off the ACK'd data, so we
669 1.12.6.2 yamt * have to leave snd_una as it was to get the correct data
670 1.12.6.2 yamt * offset in tcp_output().
671 1.12.6.2 yamt */
672 1.12.6.2 yamt if (++tp->t_partialacks == 1)
673 1.12.6.2 yamt TCP_TIMER_DISARM(tp, TCPT_REXMT);
674 1.12.6.2 yamt tp->t_rtttime = 0;
675 1.12.6.2 yamt tp->snd_nxt = th->th_ack;
676 1.12.6.2 yamt /*
677 1.12.6.2 yamt * Set snd_cwnd to one segment beyond ACK'd offset. snd_una
678 1.12.6.2 yamt * is not yet updated when we're called.
679 1.12.6.2 yamt */
680 1.12.6.2 yamt tp->snd_cwnd = tp->t_segsz + (th->th_ack - tp->snd_una);
681 1.12.6.2 yamt (void) tcp_output(tp);
682 1.12.6.2 yamt tp->snd_cwnd = ocwnd;
683 1.12.6.2 yamt if (SEQ_GT(onxt, tp->snd_nxt))
684 1.12.6.2 yamt tp->snd_nxt = onxt;
685 1.12.6.2 yamt /*
686 1.12.6.2 yamt * Partial window deflation. Relies on fact that tp->snd_una
687 1.12.6.2 yamt * not updated yet.
688 1.12.6.2 yamt */
689 1.12.6.2 yamt tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_segsz);
690 1.12.6.2 yamt } else {
691 1.12.6.2 yamt /*
692 1.12.6.2 yamt * Complete ack. Inflate the congestion window to ssthresh
693 1.12.6.2 yamt * and exit fast recovery.
694 1.12.6.2 yamt *
695 1.12.6.2 yamt * Window inflation should have left us with approx.
696 1.12.6.2 yamt * snd_ssthresh outstanding data. But in case we
697 1.12.6.2 yamt * would be inclined to send a burst, better to do
698 1.12.6.2 yamt * it via the slow start mechanism.
699 1.12.6.2 yamt */
700 1.12.6.2 yamt if (SEQ_SUB(tp->snd_max, th->th_ack) < tp->snd_ssthresh)
701 1.12.6.2 yamt tp->snd_cwnd = SEQ_SUB(tp->snd_max, th->th_ack)
702 1.12.6.2 yamt + tp->t_segsz;
703 1.12.6.2 yamt else
704 1.12.6.2 yamt tp->snd_cwnd = tp->snd_ssthresh;
705 1.12.6.2 yamt tp->t_partialacks = -1;
706 1.12.6.2 yamt tp->t_dupacks = 0;
707 1.12.6.2 yamt tp->t_bytes_acked = 0;
708 1.12.6.2 yamt }
709 1.12.6.2 yamt }
710 1.12.6.2 yamt
711 1.12.6.2 yamt static void
712 1.12.6.2 yamt tcp_newreno_newack(struct tcpcb *tp, const struct tcphdr *th)
713 1.12.6.2 yamt {
714 1.12.6.2 yamt /*
715 1.12.6.2 yamt * If we are still in fast recovery (meaning we are using
716 1.12.6.2 yamt * NewReno and we have only received partial acks), do not
717 1.12.6.2 yamt * inflate the window yet.
718 1.12.6.2 yamt */
719 1.12.6.2 yamt if (tp->t_partialacks < 0)
720 1.12.6.2 yamt tcp_reno_newack(tp, th);
721 1.12.6.2 yamt }
722 1.12.6.2 yamt
723 1.12.6.2 yamt
724 1.12.6.4 yamt const struct tcp_congctl tcp_newreno_ctl = {
725 1.12.6.2 yamt .fast_retransmit = tcp_newreno_fast_retransmit,
726 1.12.6.2 yamt .slow_retransmit = tcp_reno_slow_retransmit,
727 1.12.6.2 yamt .fast_retransmit_newack = tcp_newreno_fast_retransmit_newack,
728 1.12.6.2 yamt .newack = tcp_newreno_newack,
729 1.12.6.2 yamt .cong_exp = tcp_reno_congestion_exp,
730 1.12.6.2 yamt };
731 1.12.6.2 yamt
732 1.12.6.2 yamt
733