xref: /dragonfly/sys/netinet/tcp_timer.c (revision 20c2db9a)
1 /*
2  * Copyright (c) 2003, 2004 Jeffrey M. Hsu.  All rights reserved.
3  * Copyright (c) 2003, 2004 The DragonFly Project.  All rights reserved.
4  *
5  * This code is derived from software contributed to The DragonFly Project
6  * by Jeffrey M. Hsu.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of The DragonFly Project nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific, prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /*
35  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
36  *	The Regents of the University of California.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by the University of
49  *	California, Berkeley and its contributors.
50  * 4. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *	@(#)tcp_timer.c	8.2 (Berkeley) 5/24/95
67  * $FreeBSD: src/sys/netinet/tcp_timer.c,v 1.34.2.14 2003/02/03 02:33:41 hsu Exp $
68  * $DragonFly: src/sys/netinet/tcp_timer.c,v 1.17 2008/03/30 20:39:01 dillon Exp $
69  */
70 
71 #include "opt_compat.h"
72 #include "opt_inet6.h"
73 #include "opt_tcpdebug.h"
74 
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/mbuf.h>
79 #include <sys/sysctl.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
82 #include <sys/protosw.h>
83 #include <sys/thread.h>
84 #include <sys/globaldata.h>
85 #include <sys/thread2.h>
86 #include <sys/msgport2.h>
87 
88 #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
89 
90 #include <net/route.h>
91 #include <net/netmsg2.h>
92 
93 #include <netinet/in.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/in_pcb.h>
96 #ifdef INET6
97 #include <netinet6/in6_pcb.h>
98 #endif
99 #include <netinet/ip_var.h>
100 #include <netinet/tcp.h>
101 #include <netinet/tcp_fsm.h>
102 #include <netinet/tcp_seq.h>
103 #include <netinet/tcp_timer.h>
104 #include <netinet/tcp_timer2.h>
105 #include <netinet/tcp_var.h>
106 #include <netinet/tcpip.h>
107 #ifdef TCPDEBUG
108 #include <netinet/tcp_debug.h>
109 #endif
110 
111 #define TCP_TIMER_REXMT		0x01
112 #define TCP_TIMER_PERSIST	0x02
113 #define TCP_TIMER_KEEP		0x04
114 #define TCP_TIMER_2MSL		0x08
115 #define TCP_TIMER_DELACK	0x10
116 
117 static struct tcpcb	*tcp_timer_rexmt_handler(struct tcpcb *);
118 static struct tcpcb	*tcp_timer_persist_handler(struct tcpcb *);
119 static struct tcpcb	*tcp_timer_keep_handler(struct tcpcb *);
120 static struct tcpcb	*tcp_timer_2msl_handler(struct tcpcb *);
121 static struct tcpcb	*tcp_timer_delack_handler(struct tcpcb *);
122 
123 static const struct tcp_timer {
124 	uint32_t	tt_task;
125 	struct tcpcb	*(*tt_handler)(struct tcpcb *);
126 } tcp_timer_handlers[] = {
127 	{ TCP_TIMER_DELACK,	tcp_timer_delack_handler },
128 	{ TCP_TIMER_REXMT,	tcp_timer_rexmt_handler },
129 	{ TCP_TIMER_PERSIST,	tcp_timer_persist_handler },
130 	{ TCP_TIMER_KEEP,	tcp_timer_keep_handler },
131 	{ TCP_TIMER_2MSL,	tcp_timer_2msl_handler },
132 	{ 0, NULL }
133 };
134 
135 static int
136 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
137 {
138 	int error, s, tt;
139 
140 	tt = *(int *)oidp->oid_arg1;
141 	s = (int)((int64_t)tt * 1000 / hz);
142 
143 	error = sysctl_handle_int(oidp, &s, 0, req);
144 	if (error || !req->newptr)
145 		return (error);
146 
147 	tt = (int)((int64_t)s * hz / 1000);
148 	if (tt < 1)
149 		return (EINVAL);
150 
151 	*(int *)oidp->oid_arg1 = tt;
152 	return (0);
153 }
154 
155 int	tcp_keepinit;
156 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
157     &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "Time to establish TCP connection");
158 
159 int	tcp_keepidle;
160 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
161     &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "Time before TCP keepalive probes begin");
162 
163 int	tcp_keepintvl;
164 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
165     &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "Time between TCP keepalive probes");
166 
167 int	tcp_delacktime;
168 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime,
169     CTLTYPE_INT|CTLFLAG_RW, &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
170     "Time before a delayed ACK is sent");
171 
172 int	tcp_msl;
173 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
174     &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
175 
176 int	tcp_rexmit_min;
177 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
178     &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I", "Minimum Retransmission Timeout");
179 
180 int	tcp_rexmit_slop;
181 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
182     &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I",
183     "Retransmission Timer Slop");
184 
185 static int	always_keepalive = 1;
186 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
187     &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
188 
189 /* max idle probes */
190 int	tcp_keepcnt = TCPTV_KEEPCNT;
191 SYSCTL_INT(_net_inet_tcp, OID_AUTO, keepcnt, CTLFLAG_RW,
192     &tcp_keepcnt, 0, "Maximum number of keepalive probes to be sent");
193 
194 static int tcp_do_eifel_response = 1;
195 SYSCTL_INT(_net_inet_tcp, OID_AUTO, eifel_response, CTLFLAG_RW,
196     &tcp_do_eifel_response, 0, "Eifel response algorithm (RFC 4015)");
197 
198 int tcp_eifel_rtoinc = 2;
199 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, eifel_rtoinc, CTLTYPE_INT|CTLFLAG_RW,
200     &tcp_eifel_rtoinc, 0, sysctl_msec_to_ticks, "I",
201     "Eifel response RTO increment");
202 
203 /* max idle time in persist */
204 int	tcp_maxpersistidle;
205 
206 /*
207  * Cancel all timers for TCP tp.
208  */
209 void
210 tcp_canceltimers(struct tcpcb *tp)
211 {
212 	tcp_callout_stop(tp, tp->tt_2msl);
213 	tcp_callout_stop(tp, tp->tt_persist);
214 	tcp_callout_stop(tp, tp->tt_keep);
215 	tcp_callout_stop(tp, tp->tt_rexmt);
216 }
217 
218 /*
219  * Caller should be in critical section
220  */
221 static void
222 tcp_send_timermsg(struct tcpcb *tp, uint32_t task)
223 {
224 	struct netmsg_tcp_timer *tmsg = tp->tt_msg;
225 
226 	KKASSERT(tmsg != NULL && tmsg->tt_cpuid == mycpuid &&
227 		 tmsg->tt_tcb != NULL);
228 
229 	tmsg->tt_tasks |= task;
230 	if (tmsg->tt_msg.lmsg.ms_flags & MSGF_DONE)
231 		lwkt_sendmsg(tmsg->tt_msgport, &tmsg->tt_msg.lmsg);
232 }
233 
234 int	tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
235     { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
236 
237 int	tcp_syn_backoff_low[TCP_MAXRXTSHIFT + 1] =
238     { 1, 1, 2, 4, 8, 8, 16, 16, 32, 64, 64, 64, 64 };
239 
240 int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
241     { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
242 
243 static int tcp_totbackoff = 511;	/* sum of tcp_backoff[] */
244 
245 /* Caller should be in critical section */
246 static struct tcpcb *
247 tcp_timer_delack_handler(struct tcpcb *tp)
248 {
249 	tp->t_flags |= TF_ACKNOW;
250 	tcpstat.tcps_delack++;
251 	tcp_output(tp);
252 	return tp;
253 }
254 
255 /*
256  * TCP timer processing.
257  */
258 void
259 tcp_timer_delack(void *xtp)
260 {
261 	struct tcpcb *tp = xtp;
262 	struct callout *co = &tp->tt_delack->tc_callout;
263 
264 	crit_enter();
265 	if (callout_pending(co) || !callout_active(co)) {
266 		crit_exit();
267 		return;
268 	}
269 	callout_deactivate(co);
270 	tcp_send_timermsg(tp, TCP_TIMER_DELACK);
271 	crit_exit();
272 }
273 
274 /* Caller should be in critical section */
275 static struct tcpcb *
276 tcp_timer_2msl_handler(struct tcpcb *tp)
277 {
278 #ifdef TCPDEBUG
279 	int ostate;
280 #endif
281 
282 #ifdef TCPDEBUG
283 	ostate = tp->t_state;
284 #endif
285 	/*
286 	 * 2 MSL timeout in shutdown went off.  If we're closed but
287 	 * still waiting for peer to close and connection has been idle
288 	 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
289 	 * control block.  Otherwise, check again in a bit.
290 	 */
291 	if (tp->t_state != TCPS_TIME_WAIT &&
292 	    (ticks - tp->t_rcvtime) <= tp->t_maxidle) {
293 		tcp_callout_reset(tp, tp->tt_2msl, tp->t_keepintvl,
294 				  tcp_timer_2msl);
295 	} else {
296 		tp = tcp_close(tp);
297 	}
298 
299 #ifdef TCPDEBUG
300 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
301 		tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
302 #endif
303 	return tp;
304 }
305 
306 void
307 tcp_timer_2msl(void *xtp)
308 {
309 	struct tcpcb *tp = xtp;
310 	struct callout *co = &tp->tt_2msl->tc_callout;
311 
312 	crit_enter();
313 	if (callout_pending(co) || !callout_active(co)) {
314 		crit_exit();
315 		return;
316 	}
317 	callout_deactivate(co);
318 	tcp_send_timermsg(tp, TCP_TIMER_2MSL);
319 	crit_exit();
320 }
321 
322 /* Caller should be in critical section */
323 static struct tcpcb *
324 tcp_timer_keep_handler(struct tcpcb *tp)
325 {
326 	struct tcptemp *t_template;
327 #ifdef TCPDEBUG
328 	int ostate = tp->t_state;
329 #endif
330 
331 	/*
332 	 * Keep-alive timer went off; send something
333 	 * or drop connection if idle for too long.
334 	 */
335 	tcpstat.tcps_keeptimeo++;
336 	if (tp->t_state < TCPS_ESTABLISHED)
337 		goto dropit;
338 	if ((always_keepalive || (tp->t_flags & TF_KEEPALIVE) ||
339 	     (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE)) &&
340 	    tp->t_state <= TCPS_CLOSING) {
341 		if ((ticks - tp->t_rcvtime) >= tp->t_keepidle + tp->t_maxidle)
342 			goto dropit;
343 		/*
344 		 * Send a packet designed to force a response
345 		 * if the peer is up and reachable:
346 		 * either an ACK if the connection is still alive,
347 		 * or an RST if the peer has closed the connection
348 		 * due to timeout or reboot.
349 		 * Using sequence number tp->snd_una-1
350 		 * causes the transmitted zero-length segment
351 		 * to lie outside the receive window;
352 		 * by the protocol spec, this requires the
353 		 * correspondent TCP to respond.
354 		 */
355 		tcpstat.tcps_keepprobe++;
356 		t_template = tcp_maketemplate(tp);
357 		if (t_template) {
358 			tcp_respond(tp, t_template->tt_ipgen,
359 				    &t_template->tt_t, NULL,
360 				    tp->rcv_nxt, tp->snd_una - 1, 0);
361 			tcp_freetemplate(t_template);
362 		}
363 		tcp_callout_reset(tp, tp->tt_keep, tp->t_keepintvl,
364 				  tcp_timer_keep);
365 	} else {
366 		tcp_callout_reset(tp, tp->tt_keep, tp->t_keepidle,
367 				  tcp_timer_keep);
368 	}
369 
370 #ifdef TCPDEBUG
371 	if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
372 		tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
373 #endif
374 	return tp;
375 
376 dropit:
377 	tcpstat.tcps_keepdrops++;
378 	tp = tcp_drop(tp, ETIMEDOUT);
379 
380 #ifdef TCPDEBUG
381 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
382 		tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
383 #endif
384 	return tp;
385 }
386 
387 void
388 tcp_timer_keep(void *xtp)
389 {
390 	struct tcpcb *tp = xtp;
391 	struct callout *co = &tp->tt_keep->tc_callout;
392 
393 	crit_enter();
394 	if (callout_pending(co) || !callout_active(co)) {
395 		crit_exit();
396 		return;
397 	}
398 	callout_deactivate(co);
399 	tcp_send_timermsg(tp, TCP_TIMER_KEEP);
400 	crit_exit();
401 }
402 
403 /* Caller should be in critical section */
404 static struct tcpcb *
405 tcp_timer_persist_handler(struct tcpcb *tp)
406 {
407 #ifdef TCPDEBUG
408 	int ostate;
409 #endif
410 
411 #ifdef TCPDEBUG
412 	ostate = tp->t_state;
413 #endif
414 	/*
415 	 * Persistance timer into zero window.
416 	 * Force a byte to be output, if possible.
417 	 */
418 	tcpstat.tcps_persisttimeo++;
419 	/*
420 	 * Hack: if the peer is dead/unreachable, we do not
421 	 * time out if the window is closed.  After a full
422 	 * backoff, drop the connection if the idle time
423 	 * (no responses to probes) reaches the maximum
424 	 * backoff that we would use if retransmitting.
425 	 */
426 	if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
427 	    ((ticks - tp->t_rcvtime) >= tcp_maxpersistidle ||
428 	     (ticks - tp->t_rcvtime) >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
429 		tcpstat.tcps_persistdrop++;
430 		tp = tcp_drop(tp, ETIMEDOUT);
431 		goto out;
432 	}
433 	tcp_setpersist(tp);
434 	tp->t_flags |= TF_FORCE;
435 	tcp_output(tp);
436 	tp->t_flags &= ~TF_FORCE;
437 
438 out:
439 #ifdef TCPDEBUG
440 	if (tp && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
441 		tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
442 #endif
443 	return tp;
444 }
445 
446 void
447 tcp_timer_persist(void *xtp)
448 {
449 	struct tcpcb *tp = xtp;
450 	struct callout *co = &tp->tt_persist->tc_callout;
451 
452 	crit_enter();
453 	if (callout_pending(co) || !callout_active(co)){
454 		crit_exit();
455 		return;
456 	}
457 	callout_deactivate(co);
458 	tcp_send_timermsg(tp, TCP_TIMER_PERSIST);
459 	crit_exit();
460 }
461 
462 void
463 tcp_save_congestion_state(struct tcpcb *tp)
464 {
465 	/*
466 	 * Record connection's current states so that they could be
467 	 * recovered, if this turns out to be a spurious retransmit.
468 	 */
469 	tp->snd_cwnd_prev = tp->snd_cwnd;
470 	tp->snd_wacked_prev = tp->snd_wacked;
471 	tp->snd_ssthresh_prev = tp->snd_ssthresh;
472 	tp->snd_recover_prev = tp->snd_recover;
473 
474 	/*
475 	 * State for Eifel response after spurious timeout retransmit
476 	 * is detected.  We save the current value of snd_max even if
477 	 * we are called from fast retransmit code, so if RTO needs
478 	 * rebase, it will be rebased using the RTT of segment that
479 	 * is not sent during possible congestion.
480 	 */
481 	tp->snd_max_prev = tp->snd_max;
482 
483 	if (IN_FASTRECOVERY(tp))
484 		tp->rxt_flags |= TRXT_F_WASFRECOVERY;
485 	else
486 		tp->rxt_flags &= ~TRXT_F_WASFRECOVERY;
487 	if (tp->t_flags & TF_RCVD_TSTMP) {
488 		/* States for Eifel detection */
489 		tp->t_rexmtTS = ticks;
490 		tp->rxt_flags |= TRXT_F_FIRSTACCACK;
491 	}
492 #ifdef later
493 	tcp_sack_save_scoreboard(&tp->scb);
494 #endif
495 }
496 
497 void
498 tcp_revert_congestion_state(struct tcpcb *tp)
499 {
500 	tp->snd_cwnd = tp->snd_cwnd_prev;
501 	tp->snd_wacked = tp->snd_wacked_prev;
502 	tp->snd_ssthresh = tp->snd_ssthresh_prev;
503 	tp->snd_recover = tp->snd_recover_prev;
504 	if (tp->rxt_flags & TRXT_F_WASFRECOVERY)
505 		ENTER_FASTRECOVERY(tp);
506 	if (tp->rxt_flags & TRXT_F_FASTREXMT) {
507 		++tcpstat.tcps_sndfastrexmitbad;
508 		if (tp->rxt_flags & TRXT_F_EARLYREXMT)
509 			++tcpstat.tcps_sndearlyrexmitbad;
510 	} else {
511 		++tcpstat.tcps_sndrtobad;
512 		tp->snd_last = ticks;
513 		if (tcp_do_eifel_response)
514 			tp->rxt_flags |= TRXT_F_REBASERTO;
515 	}
516 	tp->t_badrxtwin = 0;
517 	tp->t_rxtshift = 0;
518 	tp->snd_nxt = tp->snd_max;
519 #ifdef later
520 	tcp_sack_revert_scoreboard(&tp->scb, tp->snd_una);
521 #endif
522 }
523 
524 /* Caller should be in critical section */
525 static struct tcpcb *
526 tcp_timer_rexmt_handler(struct tcpcb *tp)
527 {
528 	int rexmt;
529 #ifdef TCPDEBUG
530 	int ostate;
531 #endif
532 
533 #ifdef TCPDEBUG
534 	ostate = tp->t_state;
535 #endif
536 	/*
537 	 * Retransmission timer went off.  Message has not
538 	 * been acked within retransmit interval.  Back off
539 	 * to a longer retransmit interval and retransmit one segment.
540 	 */
541 	if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
542 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
543 		tcpstat.tcps_timeoutdrop++;
544 		tp = tcp_drop(tp, tp->t_softerror ?
545 			      tp->t_softerror : ETIMEDOUT);
546 		goto out;
547 	}
548 	if (tp->t_rxtshift == 1) {
549 		/*
550 		 * First retransmit.
551 		 */
552 
553 		/*
554 		 * State for "RTT based spurious timeout retransmit detection"
555 		 *
556 		 * RTT based spurious timeout retransmit detection:
557 		 * A retransmit is considered spurious if an ACK for this
558 		 * segment is received within RTT/2 interval; the assumption
559 		 * here is that the ACK was already in flight.  See
560 		 * "On Estimating End-to-End Network Path Properties" by
561 		 * Allman and Paxson for more details.
562 		 */
563 		tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
564 
565 		/*
566 		 * States for Eifel response after spurious timeout retransmit
567 		 * is detected.
568 		 */
569 		tp->t_rxtcur_prev = tp->t_rxtcur;
570 		tp->t_srtt_prev = tp->t_srtt +
571 		    (tcp_eifel_rtoinc << TCP_RTT_SHIFT);
572 		tp->t_rttvar_prev = tp->t_rttvar;
573 
574 		tcp_save_congestion_state(tp);
575 		tp->rxt_flags &= ~(TRXT_F_FASTREXMT | TRXT_F_EARLYREXMT |
576 		    TRXT_F_REBASERTO);
577 	}
578 	if (tp->t_state == TCPS_SYN_SENT || tp->t_state == TCPS_SYN_RECEIVED) {
579 		/*
580 		 * Record the time that we spent in SYN or SYN|ACK
581 		 * retransmition.
582 		 *
583 		 * Needed by RFC3390 and RFC6298.
584 		 */
585 		tp->t_rxtsyn += tp->t_rxtcur;
586 	}
587 	/* Throw away SACK blocks on a RTO, as specified by RFC2018. */
588 	tcp_sack_discard(tp);
589 	tcpstat.tcps_rexmttimeo++;
590 	if (tp->t_state == TCPS_SYN_SENT) {
591 		if (tcp_low_rtobase) {
592 			rexmt = TCP_REXMTVAL(tp) *
593 				tcp_syn_backoff_low[tp->t_rxtshift];
594 		} else {
595 			rexmt = TCP_REXMTVAL(tp) *
596 				tcp_syn_backoff[tp->t_rxtshift];
597 		}
598 	} else {
599 		rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
600 	}
601 	TCPT_RANGESET(tp->t_rxtcur, rexmt,
602 		      tp->t_rttmin, TCPTV_REXMTMAX);
603 	/*
604 	 * If losing, let the lower level know and try for
605 	 * a better route.  Also, if we backed off this far,
606 	 * our srtt estimate is probably bogus.  Clobber it
607 	 * so we'll take the next rtt measurement as our srtt;
608 	 * move the current srtt into rttvar to keep the current
609 	 * retransmit times until then.
610 	 */
611 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
612 #ifdef INET6
613 		if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
614 			in6_losing(tp->t_inpcb);
615 		else
616 #endif
617 		in_losing(tp->t_inpcb);
618 		tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
619 		tp->t_srtt = 0;
620 	}
621 	tp->snd_nxt = tp->snd_una;
622 	tp->snd_recover = tp->snd_max;
623 	/*
624 	 * Force a segment to be sent.
625 	 */
626 	tp->t_flags |= TF_ACKNOW;
627 	/*
628 	 * If timing a segment in this window, stop the timer.
629 	 */
630 	tp->t_rtttime = 0;
631 	/*
632 	 * Close the congestion window down to one segment
633 	 * (we'll open it by one segment for each ack we get).
634 	 * Since we probably have a window's worth of unacked
635 	 * data accumulated, this "slow start" keeps us from
636 	 * dumping all that data as back-to-back packets (which
637 	 * might overwhelm an intermediate gateway).
638 	 *
639 	 * There are two phases to the opening: Initially we
640 	 * open by one mss on each ack.  This makes the window
641 	 * size increase exponentially with time.  If the
642 	 * window is larger than the path can handle, this
643 	 * exponential growth results in dropped packet(s)
644 	 * almost immediately.  To get more time between
645 	 * drops but still "push" the network to take advantage
646 	 * of improving conditions, we switch from exponential
647 	 * to linear window opening at some threshhold size.
648 	 * For a threshhold, we use half the current window
649 	 * size, truncated to a multiple of the mss.
650 	 *
651 	 * (the minimum cwnd that will give us exponential
652 	 * growth is 2 mss.  We don't allow the threshhold
653 	 * to go below this.)
654 	 */
655 	{
656 		u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
657 
658 		if (win < 2)
659 			win = 2;
660 		tp->snd_cwnd = tp->t_maxseg;
661 		tp->snd_wacked = 0;
662 		tp->snd_ssthresh = win * tp->t_maxseg;
663 		tp->t_dupacks = 0;
664 	}
665 	EXIT_FASTRECOVERY(tp);
666 	tcp_output(tp);
667 
668 out:
669 #ifdef TCPDEBUG
670 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
671 		tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
672 #endif
673 	return tp;
674 }
675 
676 void
677 tcp_timer_rexmt(void *xtp)
678 {
679 	struct tcpcb *tp = xtp;
680 	struct callout *co = &tp->tt_rexmt->tc_callout;
681 
682 	crit_enter();
683 	if (callout_pending(co) || !callout_active(co)) {
684 		crit_exit();
685 		return;
686 	}
687 	callout_deactivate(co);
688 	tcp_send_timermsg(tp, TCP_TIMER_REXMT);
689 	crit_exit();
690 }
691 
692 static void
693 tcp_timer_handler(netmsg_t msg)
694 {
695 	struct netmsg_tcp_timer *tmsg = (struct netmsg_tcp_timer *)msg;
696 	const struct tcp_timer *tt;
697 	struct tcpcb *tp;
698 
699 	crit_enter();
700 
701 	KKASSERT(tmsg->tt_cpuid == mycpuid && tmsg->tt_tcb != NULL);
702 	tp = tmsg->tt_tcb;
703 
704 	/* Save pending tasks and reset the tasks in message */
705 	tmsg->tt_running_tasks = tmsg->tt_tasks;
706 	tmsg->tt_prev_tasks = tmsg->tt_tasks;
707 	tmsg->tt_tasks = 0;
708 
709 	/* Reply ASAP */
710 	lwkt_replymsg(&tmsg->tt_msg.lmsg, 0);
711 
712 	if (tmsg->tt_running_tasks == 0) {
713 		/*
714 		 * All of the timers are cancelled when the message
715 		 * is pending; bail out.
716 		 */
717 		crit_exit();
718 		return;
719 	}
720 
721 	for (tt = tcp_timer_handlers; tt->tt_handler != NULL; ++tt) {
722 		if ((tmsg->tt_running_tasks & tt->tt_task) == 0)
723 			continue;
724 
725 		tmsg->tt_running_tasks &= ~tt->tt_task;
726 		tp = tt->tt_handler(tp);
727 		if (tp == NULL)
728 			break;
729 
730 		if (tmsg->tt_running_tasks == 0) /* nothing left to do */
731 			break;
732 	}
733 
734 	crit_exit();
735 }
736 
737 void
738 tcp_create_timermsg(struct tcpcb *tp, struct lwkt_port *msgport)
739 {
740 	struct netmsg_tcp_timer *tmsg = tp->tt_msg;
741 
742 	netmsg_init(&tmsg->tt_msg, NULL, &netisr_adone_rport,
743 		    MSGF_DROPABLE | MSGF_PRIORITY, tcp_timer_handler);
744 	tmsg->tt_cpuid = mycpuid;
745 	tmsg->tt_msgport = msgport;
746 	tmsg->tt_tcb = tp;
747 	tmsg->tt_tasks = 0;
748 }
749 
750 void
751 tcp_destroy_timermsg(struct tcpcb *tp)
752 {
753 	struct netmsg_tcp_timer *tmsg = tp->tt_msg;
754 
755 	if (tmsg == NULL ||		/* listen socket */
756 	    tmsg->tt_tcb == NULL)	/* only tcp_attach() is called */
757 		return;
758 
759 	KKASSERT(tmsg->tt_cpuid == mycpuid);
760 
761 	/*
762 	 * This message is still pending to be processed;
763 	 * drop it.  Optimized.
764 	 */
765 	crit_enter();
766 	if ((tmsg->tt_msg.lmsg.ms_flags & MSGF_DONE) == 0) {
767 		lwkt_dropmsg(&tmsg->tt_msg.lmsg);
768 	}
769 	crit_exit();
770 }
771 
772 static __inline void
773 tcp_callout_init(struct tcp_callout *tc, uint32_t task)
774 {
775 	callout_init_mp(&tc->tc_callout);
776 	tc->tc_task = task;
777 }
778 
779 void
780 tcp_inittimers(struct tcpcb *tp)
781 {
782 	tcp_callout_init(tp->tt_rexmt, TCP_TIMER_REXMT);
783 	tcp_callout_init(tp->tt_persist, TCP_TIMER_PERSIST);
784 	tcp_callout_init(tp->tt_keep, TCP_TIMER_KEEP);
785 	tcp_callout_init(tp->tt_2msl, TCP_TIMER_2MSL);
786 	tcp_callout_init(tp->tt_delack, TCP_TIMER_DELACK);
787 }
788