xref: /freebsd-src/sys/netinet/tcp_timer.c (revision 950ab1e470be54d17d7d9c8ed4261f803fef7d71)
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 static int
67 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
68 {
69 	int error, s, tt;
70 
71 	tt = *(int *)oidp->oid_arg1;
72 	s = (int)((int64_t)tt * 1000 / hz);
73 
74 	error = sysctl_handle_int(oidp, &s, 0, req);
75 	if (error || !req->newptr)
76 		return (error);
77 
78 	tt = (int)((int64_t)s * hz / 1000);
79 	if (tt < 1)
80 		return (EINVAL);
81 
82 	*(int *)oidp->oid_arg1 = tt;
83 	return (0);
84 }
85 
86 int	tcp_keepinit;
87 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
88     &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "");
89 
90 int	tcp_keepidle;
91 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
92     &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "");
93 
94 int	tcp_keepintvl;
95 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
96     &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "");
97 
98 int	tcp_delacktime;
99 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime,
100     CTLTYPE_INT|CTLFLAG_RW, &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
101     "Time before a delayed ACK is sent");
102 
103 int	tcp_msl;
104 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
105     &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
106 
107 int	tcp_rexmit_min;
108 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
109     &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I", "Minimum Retransmission Timeout");
110 
111 int	tcp_rexmit_slop;
112 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
113     &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I", "Retransmission Timer Slop");
114 
115 static int	always_keepalive = 1;
116 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
117     &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
118 
119 static int	tcp_keepcnt = TCPTV_KEEPCNT;
120 	/* max idle probes */
121 int	tcp_maxpersistidle;
122 	/* max idle time in persist */
123 int	tcp_maxidle;
124 
125 /*
126  * Tcp protocol timeout routine called every 500 ms.
127  * Updates timestamps used for TCP
128  * causes finite state machine actions if timers expire.
129  */
130 void
131 tcp_slowtimo()
132 {
133 
134 	tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
135 	INP_INFO_WLOCK(&tcbinfo);
136 	(void) tcp_timer_2msl_tw(0);
137 	INP_INFO_WUNLOCK(&tcbinfo);
138 }
139 
140 /*
141  * Cancel all timers for TCP tp.
142  *
143  * XXXRW: This appears to be unused.
144  */
145 void
146 tcp_canceltimers(tp)
147 	struct tcpcb *tp;
148 {
149 	callout_stop(tp->tt_2msl);
150 	callout_stop(tp->tt_persist);
151 	callout_stop(tp->tt_keep);
152 	callout_stop(tp->tt_rexmt);
153 }
154 
155 int	tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
156     { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
157 
158 int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
159     { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
160 
161 static int tcp_totbackoff = 2559;	/* sum of tcp_backoff[] */
162 
163 /*
164  * TCP timer processing.
165  */
166 
167 void
168 tcp_timer_delack(xtp)
169 	void *xtp;
170 {
171 	struct tcpcb *tp = xtp;
172 	struct inpcb *inp;
173 
174 	INP_INFO_RLOCK(&tcbinfo);
175 	inp = tp->t_inpcb;
176 	if (inp == NULL) {
177 		INP_INFO_RUNLOCK(&tcbinfo);
178 		return;
179 	}
180 	INP_LOCK(inp);
181 	INP_INFO_RUNLOCK(&tcbinfo);
182 	if (callout_pending(tp->tt_delack) || !callout_active(tp->tt_delack)) {
183 		INP_UNLOCK(inp);
184 		return;
185 	}
186 	callout_deactivate(tp->tt_delack);
187 
188 	tp->t_flags |= TF_ACKNOW;
189 	tcpstat.tcps_delack++;
190 	(void) tcp_output(tp);
191 	INP_UNLOCK(inp);
192 }
193 
194 void
195 tcp_timer_2msl(xtp)
196 	void *xtp;
197 {
198 	struct tcpcb *tp = xtp;
199 	struct inpcb *inp;
200 #ifdef TCPDEBUG
201 	int ostate;
202 
203 	ostate = tp->t_state;
204 #endif
205 	INP_INFO_WLOCK(&tcbinfo);
206 	inp = tp->t_inpcb;
207 	if (inp == NULL) {
208 		INP_INFO_WUNLOCK(&tcbinfo);
209 		return;
210 	}
211 	INP_LOCK(inp);
212 	tcp_free_sackholes(tp);
213 	if (callout_pending(tp->tt_2msl) || !callout_active(tp->tt_2msl)) {
214 		INP_UNLOCK(tp->t_inpcb);
215 		INP_INFO_WUNLOCK(&tcbinfo);
216 		return;
217 	}
218 	callout_deactivate(tp->tt_2msl);
219 	/*
220 	 * 2 MSL timeout in shutdown went off.  If we're closed but
221 	 * still waiting for peer to close and connection has been idle
222 	 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
223 	 * control block.  Otherwise, check again in a bit.
224 	 */
225 	if (tp->t_state != TCPS_TIME_WAIT &&
226 	    (ticks - tp->t_rcvtime) <= tcp_maxidle)
227 		callout_reset(tp->tt_2msl, tcp_keepintvl,
228 			      tcp_timer_2msl, tp);
229 	else
230 		tp = tcp_close(tp);
231 
232 #ifdef TCPDEBUG
233 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
234 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
235 			  PRU_SLOWTIMO);
236 #endif
237 	if (tp)
238 		INP_UNLOCK(inp);
239 	INP_INFO_WUNLOCK(&tcbinfo);
240 }
241 
242 /*
243  * The timed wait lists contain references to each of the TCP sessions
244  * currently TIME_WAIT state.  The list pointers, including the list pointers
245  * in each tcptw structure, are protected using the global tcbinfo lock,
246  * which must be held over list iteration and modification.
247  */
248 struct twlist {
249 	LIST_HEAD(, tcptw)	tw_list;
250 	struct tcptw	tw_tail;
251 };
252 #define TWLIST_NLISTS	2
253 static struct twlist twl_2msl[TWLIST_NLISTS];
254 static struct twlist *tw_2msl_list[] = { &twl_2msl[0], &twl_2msl[1], NULL };
255 
256 void
257 tcp_timer_init(void)
258 {
259 	int i;
260 	struct twlist *twl;
261 
262 	for (i = 0; i < TWLIST_NLISTS; i++) {
263 		twl = &twl_2msl[i];
264 		LIST_INIT(&twl->tw_list);
265 		LIST_INSERT_HEAD(&twl->tw_list, &twl->tw_tail, tw_2msl);
266 	}
267 }
268 
269 void
270 tcp_timer_2msl_reset(struct tcptw *tw, int timeo)
271 {
272 	int i;
273 	struct tcptw *tw_tail;
274 
275 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
276 	INP_LOCK_ASSERT(tw->tw_inpcb);
277 	if (tw->tw_time != 0)
278 		LIST_REMOVE(tw, tw_2msl);
279 	tw->tw_time = timeo + ticks;
280 	i = timeo > tcp_msl ? 1 : 0;
281 	tw_tail = &twl_2msl[i].tw_tail;
282 	LIST_INSERT_BEFORE(tw_tail, tw, tw_2msl);
283 }
284 
285 void
286 tcp_timer_2msl_stop(struct tcptw *tw)
287 {
288 
289 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
290 	if (tw->tw_time != 0)
291 		LIST_REMOVE(tw, tw_2msl);
292 }
293 
294 struct tcptw *
295 tcp_timer_2msl_tw(int reuse)
296 {
297 	struct tcptw *tw, *tw_tail;
298 	struct twlist *twl;
299 	int i;
300 
301 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
302 	for (i = 0; i < 2; i++) {
303 		twl = tw_2msl_list[i];
304 		tw_tail = &twl->tw_tail;
305 		for (;;) {
306 			tw = LIST_FIRST(&twl->tw_list);
307 			if (tw == tw_tail || (!reuse && tw->tw_time > ticks))
308 				break;
309 			INP_LOCK(tw->tw_inpcb);
310 			if (tcp_twclose(tw, reuse) != NULL)
311 				return (tw);
312 		}
313 	}
314 	return (NULL);
315 }
316 
317 void
318 tcp_timer_keep(xtp)
319 	void *xtp;
320 {
321 	struct tcpcb *tp = xtp;
322 	struct tcptemp *t_template;
323 	int s;
324 	struct inpcb *inp;
325 #ifdef TCPDEBUG
326 	int ostate;
327 
328 	ostate = tp->t_state;
329 #endif
330 	s = splnet();
331 	INP_INFO_WLOCK(&tcbinfo);
332 	inp = tp->t_inpcb;
333 	if (!inp) {
334 		INP_INFO_WUNLOCK(&tcbinfo);
335 		splx(s);
336 		return;
337 	}
338 	INP_LOCK(inp);
339 	if (callout_pending(tp->tt_keep) || !callout_active(tp->tt_keep)) {
340 		INP_UNLOCK(inp);
341 		INP_INFO_WUNLOCK(&tcbinfo);
342 		splx(s);
343 		return;
344 	}
345 	callout_deactivate(tp->tt_keep);
346 	/*
347 	 * Keep-alive timer went off; send something
348 	 * or drop connection if idle for too long.
349 	 */
350 	tcpstat.tcps_keeptimeo++;
351 	if (tp->t_state < TCPS_ESTABLISHED)
352 		goto dropit;
353 	if ((always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
354 	    tp->t_state <= TCPS_CLOSING) {
355 		if ((ticks - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle)
356 			goto dropit;
357 		/*
358 		 * Send a packet designed to force a response
359 		 * if the peer is up and reachable:
360 		 * either an ACK if the connection is still alive,
361 		 * or an RST if the peer has closed the connection
362 		 * due to timeout or reboot.
363 		 * Using sequence number tp->snd_una-1
364 		 * causes the transmitted zero-length segment
365 		 * to lie outside the receive window;
366 		 * by the protocol spec, this requires the
367 		 * correspondent TCP to respond.
368 		 */
369 		tcpstat.tcps_keepprobe++;
370 		t_template = tcpip_maketemplate(inp);
371 		if (t_template) {
372 			tcp_respond(tp, t_template->tt_ipgen,
373 				    &t_template->tt_t, (struct mbuf *)NULL,
374 				    tp->rcv_nxt, tp->snd_una - 1, 0);
375 			(void) m_free(dtom(t_template));
376 		}
377 		callout_reset(tp->tt_keep, tcp_keepintvl, tcp_timer_keep, tp);
378 	} else
379 		callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
380 
381 #ifdef TCPDEBUG
382 	if (inp->inp_socket->so_options & SO_DEBUG)
383 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
384 			  PRU_SLOWTIMO);
385 #endif
386 	INP_UNLOCK(inp);
387 	INP_INFO_WUNLOCK(&tcbinfo);
388 	splx(s);
389 	return;
390 
391 dropit:
392 	tcpstat.tcps_keepdrops++;
393 	tp = tcp_drop(tp, ETIMEDOUT);
394 
395 #ifdef TCPDEBUG
396 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
397 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
398 			  PRU_SLOWTIMO);
399 #endif
400 	if (tp)
401 		INP_UNLOCK(tp->t_inpcb);
402 	INP_INFO_WUNLOCK(&tcbinfo);
403 	splx(s);
404 }
405 
406 void
407 tcp_timer_persist(xtp)
408 	void *xtp;
409 {
410 	struct tcpcb *tp = xtp;
411 	int s;
412 	struct inpcb *inp;
413 #ifdef TCPDEBUG
414 	int ostate;
415 
416 	ostate = tp->t_state;
417 #endif
418 	s = splnet();
419 	INP_INFO_WLOCK(&tcbinfo);
420 	inp = tp->t_inpcb;
421 	if (!inp) {
422 		INP_INFO_WUNLOCK(&tcbinfo);
423 		splx(s);
424 		return;
425 	}
426 	INP_LOCK(inp);
427 	if (callout_pending(tp->tt_persist) || !callout_active(tp->tt_persist)){
428 		INP_UNLOCK(inp);
429 		INP_INFO_WUNLOCK(&tcbinfo);
430 		splx(s);
431 		return;
432 	}
433 	callout_deactivate(tp->tt_persist);
434 	/*
435 	 * Persistance timer into zero window.
436 	 * Force a byte to be output, if possible.
437 	 */
438 	tcpstat.tcps_persisttimeo++;
439 	/*
440 	 * Hack: if the peer is dead/unreachable, we do not
441 	 * time out if the window is closed.  After a full
442 	 * backoff, drop the connection if the idle time
443 	 * (no responses to probes) reaches the maximum
444 	 * backoff that we would use if retransmitting.
445 	 */
446 	if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
447 	    ((ticks - tp->t_rcvtime) >= tcp_maxpersistidle ||
448 	     (ticks - tp->t_rcvtime) >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
449 		tcpstat.tcps_persistdrop++;
450 		tp = tcp_drop(tp, ETIMEDOUT);
451 		goto out;
452 	}
453 	tcp_setpersist(tp);
454 	tp->t_force = 1;
455 	(void) tcp_output(tp);
456 	tp->t_force = 0;
457 
458 out:
459 #ifdef TCPDEBUG
460 	if (tp && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
461 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
462 			  PRU_SLOWTIMO);
463 #endif
464 	if (tp)
465 		INP_UNLOCK(inp);
466 	INP_INFO_WUNLOCK(&tcbinfo);
467 	splx(s);
468 }
469 
470 void
471 tcp_timer_rexmt(xtp)
472 	void *xtp;
473 {
474 	struct tcpcb *tp = xtp;
475 	int s;
476 	int rexmt;
477 	int headlocked;
478 	struct inpcb *inp;
479 #ifdef TCPDEBUG
480 	int ostate;
481 
482 	ostate = tp->t_state;
483 #endif
484 	s = splnet();
485 	INP_INFO_WLOCK(&tcbinfo);
486 	headlocked = 1;
487 	inp = tp->t_inpcb;
488 	if (!inp) {
489 		INP_INFO_WUNLOCK(&tcbinfo);
490 		splx(s);
491 		return;
492 	}
493 	INP_LOCK(inp);
494 	if (callout_pending(tp->tt_rexmt) || !callout_active(tp->tt_rexmt)) {
495 		INP_UNLOCK(inp);
496 		INP_INFO_WUNLOCK(&tcbinfo);
497 		splx(s);
498 		return;
499 	}
500 	callout_deactivate(tp->tt_rexmt);
501 	tcp_free_sackholes(tp);
502 	/*
503 	 * Retransmission timer went off.  Message has not
504 	 * been acked within retransmit interval.  Back off
505 	 * to a longer retransmit interval and retransmit one segment.
506 	 */
507 	if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
508 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
509 		tcpstat.tcps_timeoutdrop++;
510 		tp = tcp_drop(tp, tp->t_softerror ?
511 			      tp->t_softerror : ETIMEDOUT);
512 		goto out;
513 	}
514 	INP_INFO_WUNLOCK(&tcbinfo);
515 	headlocked = 0;
516 	if (tp->t_rxtshift == 1) {
517 		/*
518 		 * first retransmit; record ssthresh and cwnd so they can
519 		 * be recovered if this turns out to be a "bad" retransmit.
520 		 * A retransmit is considered "bad" if an ACK for this
521 		 * segment is received within RTT/2 interval; the assumption
522 		 * here is that the ACK was already in flight.  See
523 		 * "On Estimating End-to-End Network Path Properties" by
524 		 * Allman and Paxson for more details.
525 		 */
526 		tp->snd_cwnd_prev = tp->snd_cwnd;
527 		tp->snd_ssthresh_prev = tp->snd_ssthresh;
528 		tp->snd_recover_prev = tp->snd_recover;
529 		if (IN_FASTRECOVERY(tp))
530 		  tp->t_flags |= TF_WASFRECOVERY;
531 		else
532 		  tp->t_flags &= ~TF_WASFRECOVERY;
533 		tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
534 	}
535 	tcpstat.tcps_rexmttimeo++;
536 	if (tp->t_state == TCPS_SYN_SENT)
537 		rexmt = TCP_REXMTVAL(tp) * tcp_syn_backoff[tp->t_rxtshift];
538 	else
539 		rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
540 	TCPT_RANGESET(tp->t_rxtcur, rexmt,
541 		      tp->t_rttmin, TCPTV_REXMTMAX);
542 	/*
543 	 * Disable rfc1323 if we havn't got any response to
544 	 * our third SYN to work-around some broken terminal servers
545 	 * (most of which have hopefully been retired) that have bad VJ
546 	 * header compression code which trashes TCP segments containing
547 	 * unknown-to-them TCP options.
548 	 */
549 	if ((tp->t_state == TCPS_SYN_SENT) && (tp->t_rxtshift == 3))
550 		tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP);
551 	/*
552 	 * If we backed off this far, our srtt estimate is probably bogus.
553 	 * Clobber it so we'll take the next rtt measurement as our srtt;
554 	 * move the current srtt into rttvar to keep the current
555 	 * retransmit times until then.
556 	 */
557 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
558 #ifdef INET6
559 		if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
560 			in6_losing(tp->t_inpcb);
561 		else
562 #endif
563 		tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
564 		tp->t_srtt = 0;
565 	}
566 	tp->snd_nxt = tp->snd_una;
567 	tp->snd_recover = tp->snd_max;
568 	/*
569 	 * Force a segment to be sent.
570 	 */
571 	tp->t_flags |= TF_ACKNOW;
572 	/*
573 	 * If timing a segment in this window, stop the timer.
574 	 */
575 	tp->t_rtttime = 0;
576 	/*
577 	 * Close the congestion window down to one segment
578 	 * (we'll open it by one segment for each ack we get).
579 	 * Since we probably have a window's worth of unacked
580 	 * data accumulated, this "slow start" keeps us from
581 	 * dumping all that data as back-to-back packets (which
582 	 * might overwhelm an intermediate gateway).
583 	 *
584 	 * There are two phases to the opening: Initially we
585 	 * open by one mss on each ack.  This makes the window
586 	 * size increase exponentially with time.  If the
587 	 * window is larger than the path can handle, this
588 	 * exponential growth results in dropped packet(s)
589 	 * almost immediately.  To get more time between
590 	 * drops but still "push" the network to take advantage
591 	 * of improving conditions, we switch from exponential
592 	 * to linear window opening at some threshhold size.
593 	 * For a threshhold, we use half the current window
594 	 * size, truncated to a multiple of the mss.
595 	 *
596 	 * (the minimum cwnd that will give us exponential
597 	 * growth is 2 mss.  We don't allow the threshhold
598 	 * to go below this.)
599 	 */
600 	{
601 		u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
602 		if (win < 2)
603 			win = 2;
604 		tp->snd_cwnd = tp->t_maxseg;
605 		tp->snd_ssthresh = win * tp->t_maxseg;
606 		tp->t_dupacks = 0;
607 	}
608 	EXIT_FASTRECOVERY(tp);
609 	(void) tcp_output(tp);
610 
611 out:
612 #ifdef TCPDEBUG
613 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
614 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
615 			  PRU_SLOWTIMO);
616 #endif
617 	if (tp)
618 		INP_UNLOCK(inp);
619 	if (headlocked)
620 		INP_INFO_WUNLOCK(&tcbinfo);
621 	splx(s);
622 }
623