xref: /freebsd/sys/netinet/tcp_timer.c (revision 3fe92528afe8313fecf48822dde74bad5e380f48)
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)tcp_timer.c	8.2 (Berkeley) 5/24/95
30  * $FreeBSD$
31  */
32 
33 #include "opt_inet6.h"
34 #include "opt_tcpdebug.h"
35 #include "opt_tcp_sack.h"
36 
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/mbuf.h>
41 #include <sys/mutex.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/sysctl.h>
46 #include <sys/systm.h>
47 
48 #include <net/route.h>
49 
50 #include <netinet/in.h>
51 #include <netinet/in_pcb.h>
52 #include <netinet/in_systm.h>
53 #ifdef INET6
54 #include <netinet6/in6_pcb.h>
55 #endif
56 #include <netinet/ip_var.h>
57 #include <netinet/tcp.h>
58 #include <netinet/tcp_fsm.h>
59 #include <netinet/tcp_timer.h>
60 #include <netinet/tcp_var.h>
61 #include <netinet/tcpip.h>
62 #ifdef TCPDEBUG
63 #include <netinet/tcp_debug.h>
64 #endif
65 
66 int	tcp_keepinit;
67 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
68     &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "");
69 
70 int	tcp_keepidle;
71 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
72     &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "");
73 
74 int	tcp_keepintvl;
75 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
76     &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "");
77 
78 int	tcp_delacktime;
79 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime,
80     CTLTYPE_INT|CTLFLAG_RW, &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
81     "Time before a delayed ACK is sent");
82 
83 int	tcp_msl;
84 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
85     &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
86 
87 int	tcp_rexmit_min;
88 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
89     &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I", "Minimum Retransmission Timeout");
90 
91 int	tcp_rexmit_slop;
92 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
93     &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I", "Retransmission Timer Slop");
94 
95 static int	always_keepalive = 1;
96 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
97     &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
98 
99 static int	tcp_keepcnt = TCPTV_KEEPCNT;
100 	/* max idle probes */
101 int	tcp_maxpersistidle;
102 	/* max idle time in persist */
103 int	tcp_maxidle;
104 
105 /*
106  * Tcp protocol timeout routine called every 500 ms.
107  * Updates timestamps used for TCP
108  * causes finite state machine actions if timers expire.
109  */
110 void
111 tcp_slowtimo()
112 {
113 
114 	tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
115 	INP_INFO_WLOCK(&tcbinfo);
116 	(void) tcp_timer_2msl_tw(0);
117 	INP_INFO_WUNLOCK(&tcbinfo);
118 }
119 
120 int	tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
121     { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
122 
123 int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
124     { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
125 
126 static int tcp_totbackoff = 2559;	/* sum of tcp_backoff[] */
127 
128 static int tcp_timer_race;
129 SYSCTL_INT(_net_inet_tcp, OID_AUTO, timer_race, CTLFLAG_RD, &tcp_timer_race,
130     0, "Count of t_inpcb races on tcp_discardcb");
131 
132 /*
133  * TCP timer processing.
134  */
135 
136 void
137 tcp_timer_delack(xtp)
138 	void *xtp;
139 {
140 	struct tcpcb *tp = xtp;
141 	struct inpcb *inp;
142 
143 	INP_INFO_RLOCK(&tcbinfo);
144 	inp = tp->t_inpcb;
145 	/*
146 	 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
147 	 * tear-down mean we need it as a work-around for races between
148 	 * timers and tcp_discardcb().
149 	 *
150 	 * KASSERT(inp != NULL, ("tcp_timer_delack: inp == NULL"));
151 	 */
152 	if (inp == NULL) {
153 		tcp_timer_race++;
154 		INP_INFO_RUNLOCK(&tcbinfo);
155 		return;
156 	}
157 	INP_LOCK(inp);
158 	INP_INFO_RUNLOCK(&tcbinfo);
159 	if ((inp->inp_vflag & INP_DROPPED) || callout_pending(tp->tt_delack)
160 	    || !callout_active(tp->tt_delack)) {
161 		INP_UNLOCK(inp);
162 		return;
163 	}
164 	callout_deactivate(tp->tt_delack);
165 
166 	tp->t_flags |= TF_ACKNOW;
167 	tcpstat.tcps_delack++;
168 	(void) tcp_output(tp);
169 	INP_UNLOCK(inp);
170 }
171 
172 void
173 tcp_timer_2msl(xtp)
174 	void *xtp;
175 {
176 	struct tcpcb *tp = xtp;
177 	struct inpcb *inp;
178 #ifdef TCPDEBUG
179 	int ostate;
180 
181 	ostate = tp->t_state;
182 #endif
183 	/*
184 	 * XXXRW: Does this actually happen?
185 	 */
186 	INP_INFO_WLOCK(&tcbinfo);
187 	inp = tp->t_inpcb;
188 	/*
189 	 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
190 	 * tear-down mean we need it as a work-around for races between
191 	 * timers and tcp_discardcb().
192 	 *
193 	 * KASSERT(inp != NULL, ("tcp_timer_2msl: inp == NULL"));
194 	 */
195 	if (inp == NULL) {
196 		tcp_timer_race++;
197 		INP_INFO_WUNLOCK(&tcbinfo);
198 		return;
199 	}
200 	INP_LOCK(inp);
201 	tcp_free_sackholes(tp);
202 	if ((inp->inp_vflag & INP_DROPPED) || callout_pending(tp->tt_2msl) ||
203 	    !callout_active(tp->tt_2msl)) {
204 		INP_UNLOCK(tp->t_inpcb);
205 		INP_INFO_WUNLOCK(&tcbinfo);
206 		return;
207 	}
208 	callout_deactivate(tp->tt_2msl);
209 	/*
210 	 * 2 MSL timeout in shutdown went off.  If we're closed but
211 	 * still waiting for peer to close and connection has been idle
212 	 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
213 	 * control block.  Otherwise, check again in a bit.
214 	 */
215 	if (tp->t_state != TCPS_TIME_WAIT &&
216 	    (ticks - tp->t_rcvtime) <= tcp_maxidle)
217 		callout_reset(tp->tt_2msl, tcp_keepintvl,
218 			      tcp_timer_2msl, tp);
219 	else
220 		tp = tcp_close(tp);
221 
222 #ifdef TCPDEBUG
223 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
224 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
225 			  PRU_SLOWTIMO);
226 #endif
227 	if (tp != NULL)
228 		INP_UNLOCK(inp);
229 	INP_INFO_WUNLOCK(&tcbinfo);
230 }
231 
232 /*
233  * The timed wait queue contains references to each of the TCP sessions
234  * currently in the TIME_WAIT state.  The queue pointers, including the
235  * queue pointers in each tcptw structure, are protected using the global
236  * tcbinfo lock, which must be held over queue iteration and modification.
237  */
238 static TAILQ_HEAD(, tcptw)	twq_2msl;
239 
240 void
241 tcp_timer_init(void)
242 {
243 
244 	TAILQ_INIT(&twq_2msl);
245 }
246 
247 void
248 tcp_timer_2msl_reset(struct tcptw *tw, int rearm)
249 {
250 
251 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
252 	INP_LOCK_ASSERT(tw->tw_inpcb);
253 	if (rearm)
254 		TAILQ_REMOVE(&twq_2msl, tw, tw_2msl);
255 	tw->tw_time = ticks + 2 * tcp_msl;
256 	TAILQ_INSERT_TAIL(&twq_2msl, tw, tw_2msl);
257 }
258 
259 void
260 tcp_timer_2msl_stop(struct tcptw *tw)
261 {
262 
263 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
264 	TAILQ_REMOVE(&twq_2msl, tw, tw_2msl);
265 }
266 
267 struct tcptw *
268 tcp_timer_2msl_tw(int reuse)
269 {
270 	struct tcptw *tw;
271 
272 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
273 	for (;;) {
274 		tw = TAILQ_FIRST(&twq_2msl);
275 		if (tw == NULL || (!reuse && tw->tw_time > ticks))
276 			break;
277 		INP_LOCK(tw->tw_inpcb);
278 		tcp_twclose(tw, reuse);
279 		if (reuse)
280 			return (tw);
281 	}
282 	return (NULL);
283 }
284 
285 void
286 tcp_timer_keep(xtp)
287 	void *xtp;
288 {
289 	struct tcpcb *tp = xtp;
290 	struct tcptemp *t_template;
291 	struct inpcb *inp;
292 #ifdef TCPDEBUG
293 	int ostate;
294 
295 	ostate = tp->t_state;
296 #endif
297 	INP_INFO_WLOCK(&tcbinfo);
298 	inp = tp->t_inpcb;
299 	/*
300 	 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
301 	 * tear-down mean we need it as a work-around for races between
302 	 * timers and tcp_discardcb().
303 	 *
304 	 * KASSERT(inp != NULL, ("tcp_timer_keep: inp == NULL"));
305 	 */
306 	if (inp == NULL) {
307 		tcp_timer_race++;
308 		INP_INFO_WUNLOCK(&tcbinfo);
309 		return;
310 	}
311 	INP_LOCK(inp);
312 	if ((inp->inp_vflag & INP_DROPPED) || callout_pending(tp->tt_keep)
313 	    || !callout_active(tp->tt_keep)) {
314 		INP_UNLOCK(inp);
315 		INP_INFO_WUNLOCK(&tcbinfo);
316 		return;
317 	}
318 	callout_deactivate(tp->tt_keep);
319 	/*
320 	 * Keep-alive timer went off; send something
321 	 * or drop connection if idle for too long.
322 	 */
323 	tcpstat.tcps_keeptimeo++;
324 	if (tp->t_state < TCPS_ESTABLISHED)
325 		goto dropit;
326 	if ((always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
327 	    tp->t_state <= TCPS_CLOSING) {
328 		if ((ticks - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle)
329 			goto dropit;
330 		/*
331 		 * Send a packet designed to force a response
332 		 * if the peer is up and reachable:
333 		 * either an ACK if the connection is still alive,
334 		 * or an RST if the peer has closed the connection
335 		 * due to timeout or reboot.
336 		 * Using sequence number tp->snd_una-1
337 		 * causes the transmitted zero-length segment
338 		 * to lie outside the receive window;
339 		 * by the protocol spec, this requires the
340 		 * correspondent TCP to respond.
341 		 */
342 		tcpstat.tcps_keepprobe++;
343 		t_template = tcpip_maketemplate(inp);
344 		if (t_template) {
345 			tcp_respond(tp, t_template->tt_ipgen,
346 				    &t_template->tt_t, (struct mbuf *)NULL,
347 				    tp->rcv_nxt, tp->snd_una - 1, 0);
348 			(void) m_free(dtom(t_template));
349 		}
350 		callout_reset(tp->tt_keep, tcp_keepintvl, tcp_timer_keep, tp);
351 	} else
352 		callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
353 
354 #ifdef TCPDEBUG
355 	if (inp->inp_socket->so_options & SO_DEBUG)
356 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
357 			  PRU_SLOWTIMO);
358 #endif
359 	INP_UNLOCK(inp);
360 	INP_INFO_WUNLOCK(&tcbinfo);
361 	return;
362 
363 dropit:
364 	tcpstat.tcps_keepdrops++;
365 	tp = tcp_drop(tp, ETIMEDOUT);
366 
367 #ifdef TCPDEBUG
368 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
369 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
370 			  PRU_SLOWTIMO);
371 #endif
372 	if (tp != NULL)
373 		INP_UNLOCK(tp->t_inpcb);
374 	INP_INFO_WUNLOCK(&tcbinfo);
375 }
376 
377 void
378 tcp_timer_persist(xtp)
379 	void *xtp;
380 {
381 	struct tcpcb *tp = xtp;
382 	struct inpcb *inp;
383 #ifdef TCPDEBUG
384 	int ostate;
385 
386 	ostate = tp->t_state;
387 #endif
388 	INP_INFO_WLOCK(&tcbinfo);
389 	inp = tp->t_inpcb;
390 	/*
391 	 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
392 	 * tear-down mean we need it as a work-around for races between
393 	 * timers and tcp_discardcb().
394 	 *
395 	 * KASSERT(inp != NULL, ("tcp_timer_persist: inp == NULL"));
396 	 */
397 	if (inp == NULL) {
398 		tcp_timer_race++;
399 		INP_INFO_WUNLOCK(&tcbinfo);
400 		return;
401 	}
402 	INP_LOCK(inp);
403 	if ((inp->inp_vflag & INP_DROPPED) || callout_pending(tp->tt_persist)
404 	    || !callout_active(tp->tt_persist)) {
405 		INP_UNLOCK(inp);
406 		INP_INFO_WUNLOCK(&tcbinfo);
407 		return;
408 	}
409 	callout_deactivate(tp->tt_persist);
410 	/*
411 	 * Persistance timer into zero window.
412 	 * Force a byte to be output, if possible.
413 	 */
414 	tcpstat.tcps_persisttimeo++;
415 	/*
416 	 * Hack: if the peer is dead/unreachable, we do not
417 	 * time out if the window is closed.  After a full
418 	 * backoff, drop the connection if the idle time
419 	 * (no responses to probes) reaches the maximum
420 	 * backoff that we would use if retransmitting.
421 	 */
422 	if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
423 	    ((ticks - tp->t_rcvtime) >= tcp_maxpersistidle ||
424 	     (ticks - tp->t_rcvtime) >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
425 		tcpstat.tcps_persistdrop++;
426 		tp = tcp_drop(tp, ETIMEDOUT);
427 		goto out;
428 	}
429 	tcp_setpersist(tp);
430 	tp->t_flags |= TF_FORCEDATA;
431 	(void) tcp_output(tp);
432 	tp->t_flags &= ~TF_FORCEDATA;
433 
434 out:
435 #ifdef TCPDEBUG
436 	if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
437 		tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
438 #endif
439 	if (tp != NULL)
440 		INP_UNLOCK(inp);
441 	INP_INFO_WUNLOCK(&tcbinfo);
442 }
443 
444 void
445 tcp_timer_rexmt(xtp)
446 	void *xtp;
447 {
448 	struct tcpcb *tp = xtp;
449 	int rexmt;
450 	int headlocked;
451 	struct inpcb *inp;
452 #ifdef TCPDEBUG
453 	int ostate;
454 
455 	ostate = tp->t_state;
456 #endif
457 	INP_INFO_WLOCK(&tcbinfo);
458 	headlocked = 1;
459 	inp = tp->t_inpcb;
460 	/*
461 	 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
462 	 * tear-down mean we need it as a work-around for races between
463 	 * timers and tcp_discardcb().
464 	 *
465 	 * KASSERT(inp != NULL, ("tcp_timer_rexmt: inp == NULL"));
466 	 */
467 	if (inp == NULL) {
468 		tcp_timer_race++;
469 		INP_INFO_WUNLOCK(&tcbinfo);
470 		return;
471 	}
472 	INP_LOCK(inp);
473 	if ((inp->inp_vflag & INP_DROPPED) || callout_pending(tp->tt_rexmt)
474 	    || !callout_active(tp->tt_rexmt)) {
475 		INP_UNLOCK(inp);
476 		INP_INFO_WUNLOCK(&tcbinfo);
477 		return;
478 	}
479 	callout_deactivate(tp->tt_rexmt);
480 	tcp_free_sackholes(tp);
481 	/*
482 	 * Retransmission timer went off.  Message has not
483 	 * been acked within retransmit interval.  Back off
484 	 * to a longer retransmit interval and retransmit one segment.
485 	 */
486 	if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
487 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
488 		tcpstat.tcps_timeoutdrop++;
489 		tp = tcp_drop(tp, tp->t_softerror ?
490 			      tp->t_softerror : ETIMEDOUT);
491 		goto out;
492 	}
493 	INP_INFO_WUNLOCK(&tcbinfo);
494 	headlocked = 0;
495 	if (tp->t_rxtshift == 1) {
496 		/*
497 		 * first retransmit; record ssthresh and cwnd so they can
498 		 * be recovered if this turns out to be a "bad" retransmit.
499 		 * A retransmit is considered "bad" if an ACK for this
500 		 * segment is received within RTT/2 interval; the assumption
501 		 * here is that the ACK was already in flight.  See
502 		 * "On Estimating End-to-End Network Path Properties" by
503 		 * Allman and Paxson for more details.
504 		 */
505 		tp->snd_cwnd_prev = tp->snd_cwnd;
506 		tp->snd_ssthresh_prev = tp->snd_ssthresh;
507 		tp->snd_recover_prev = tp->snd_recover;
508 		if (IN_FASTRECOVERY(tp))
509 		  tp->t_flags |= TF_WASFRECOVERY;
510 		else
511 		  tp->t_flags &= ~TF_WASFRECOVERY;
512 		tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
513 	}
514 	tcpstat.tcps_rexmttimeo++;
515 	if (tp->t_state == TCPS_SYN_SENT)
516 		rexmt = TCP_REXMTVAL(tp) * tcp_syn_backoff[tp->t_rxtshift];
517 	else
518 		rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
519 	TCPT_RANGESET(tp->t_rxtcur, rexmt,
520 		      tp->t_rttmin, TCPTV_REXMTMAX);
521 	/*
522 	 * Disable rfc1323 if we havn't got any response to
523 	 * our third SYN to work-around some broken terminal servers
524 	 * (most of which have hopefully been retired) that have bad VJ
525 	 * header compression code which trashes TCP segments containing
526 	 * unknown-to-them TCP options.
527 	 */
528 	if ((tp->t_state == TCPS_SYN_SENT) && (tp->t_rxtshift == 3))
529 		tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP);
530 	/*
531 	 * If we backed off this far, our srtt estimate is probably bogus.
532 	 * Clobber it so we'll take the next rtt measurement as our srtt;
533 	 * move the current srtt into rttvar to keep the current
534 	 * retransmit times until then.
535 	 */
536 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
537 #ifdef INET6
538 		if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
539 			in6_losing(tp->t_inpcb);
540 		else
541 #endif
542 		tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
543 		tp->t_srtt = 0;
544 	}
545 	tp->snd_nxt = tp->snd_una;
546 	tp->snd_recover = tp->snd_max;
547 	/*
548 	 * Force a segment to be sent.
549 	 */
550 	tp->t_flags |= TF_ACKNOW;
551 	/*
552 	 * If timing a segment in this window, stop the timer.
553 	 */
554 	tp->t_rtttime = 0;
555 	/*
556 	 * Close the congestion window down to one segment
557 	 * (we'll open it by one segment for each ack we get).
558 	 * Since we probably have a window's worth of unacked
559 	 * data accumulated, this "slow start" keeps us from
560 	 * dumping all that data as back-to-back packets (which
561 	 * might overwhelm an intermediate gateway).
562 	 *
563 	 * There are two phases to the opening: Initially we
564 	 * open by one mss on each ack.  This makes the window
565 	 * size increase exponentially with time.  If the
566 	 * window is larger than the path can handle, this
567 	 * exponential growth results in dropped packet(s)
568 	 * almost immediately.  To get more time between
569 	 * drops but still "push" the network to take advantage
570 	 * of improving conditions, we switch from exponential
571 	 * to linear window opening at some threshhold size.
572 	 * For a threshhold, we use half the current window
573 	 * size, truncated to a multiple of the mss.
574 	 *
575 	 * (the minimum cwnd that will give us exponential
576 	 * growth is 2 mss.  We don't allow the threshhold
577 	 * to go below this.)
578 	 */
579 	{
580 		u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
581 		if (win < 2)
582 			win = 2;
583 		tp->snd_cwnd = tp->t_maxseg;
584 		tp->snd_ssthresh = win * tp->t_maxseg;
585 		tp->t_dupacks = 0;
586 	}
587 	EXIT_FASTRECOVERY(tp);
588 	(void) tcp_output(tp);
589 
590 out:
591 #ifdef TCPDEBUG
592 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
593 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
594 			  PRU_SLOWTIMO);
595 #endif
596 	if (tp != NULL)
597 		INP_UNLOCK(inp);
598 	if (headlocked)
599 		INP_INFO_WUNLOCK(&tcbinfo);
600 }
601