2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_inet6.h"
36 #include "opt_tcpdebug.h"
38 #include <sys/param.h>
39 #include <sys/kernel.h>
42 #include <sys/mutex.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
48 #include <sys/vimage.h>
50 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <netinet/in_pcb.h>
54 #include <netinet/in_systm.h>
56 #include <netinet6/in6_pcb.h>
58 #include <netinet/ip_var.h>
59 #include <netinet/tcp.h>
60 #include <netinet/tcp_fsm.h>
61 #include <netinet/tcp_timer.h>
62 #include <netinet/tcp_var.h>
63 #include <netinet/tcpip.h>
65 #include <netinet/tcp_debug.h>
69 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
70 &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "time to establish connection");
73 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
74 &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "time before keepalive probes begin");
77 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
78 &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "time between keepalive probes");
81 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime, CTLTYPE_INT|CTLFLAG_RW,
82 &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
83 "Time before a delayed ACK is sent");
86 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
87 &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
90 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
91 &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I",
92 "Minimum Retransmission Timeout");
95 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
96 &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I",
97 "Retransmission Timer Slop");
99 static int always_keepalive = 1;
100 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
101 &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
103 int tcp_fast_finwait2_recycle = 0;
104 SYSCTL_INT(_net_inet_tcp, OID_AUTO, fast_finwait2_recycle, CTLFLAG_RW,
105 &tcp_fast_finwait2_recycle, 0,
106 "Recycle closed FIN_WAIT_2 connections faster");
108 int tcp_finwait2_timeout;
109 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, finwait2_timeout, CTLTYPE_INT|CTLFLAG_RW,
110 &tcp_finwait2_timeout, 0, sysctl_msec_to_ticks, "I", "FIN-WAIT2 timeout");
113 static int tcp_keepcnt = TCPTV_KEEPCNT;
114 /* max idle probes */
115 int tcp_maxpersistidle;
116 /* max idle time in persist */
120 * Tcp protocol timeout routine called every 500 ms.
121 * Updates timestamps used for TCP
122 * causes finite state machine actions if timers expire.
128 tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
129 INP_INFO_WLOCK(&V_tcbinfo);
130 (void) tcp_tw_2msl_scan(0);
131 INP_INFO_WUNLOCK(&V_tcbinfo);
134 int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
135 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
137 int tcp_backoff[TCP_MAXRXTSHIFT + 1] =
138 { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
140 static int tcp_totbackoff = 2559; /* sum of tcp_backoff[] */
142 static int tcp_timer_race;
143 SYSCTL_INT(_net_inet_tcp, OID_AUTO, timer_race, CTLFLAG_RD, &tcp_timer_race,
144 0, "Count of t_inpcb races on tcp_discardcb");
147 * TCP timer processing.
151 tcp_timer_delack(void *xtp)
153 struct tcpcb *tp = xtp;
156 INP_INFO_RLOCK(&V_tcbinfo);
159 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
160 * tear-down mean we need it as a work-around for races between
161 * timers and tcp_discardcb().
163 * KASSERT(inp != NULL, ("tcp_timer_delack: inp == NULL"));
167 INP_INFO_RUNLOCK(&V_tcbinfo);
171 INP_INFO_RUNLOCK(&V_tcbinfo);
172 if ((inp->inp_vflag & INP_DROPPED) || callout_pending(&tp->t_timers->tt_delack)
173 || !callout_active(&tp->t_timers->tt_delack)) {
177 callout_deactivate(&tp->t_timers->tt_delack);
179 tp->t_flags |= TF_ACKNOW;
180 V_tcpstat.tcps_delack++;
181 (void) tcp_output(tp);
186 tcp_timer_2msl(void *xtp)
188 struct tcpcb *tp = xtp;
193 ostate = tp->t_state;
196 * XXXRW: Does this actually happen?
198 INP_INFO_WLOCK(&V_tcbinfo);
201 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
202 * tear-down mean we need it as a work-around for races between
203 * timers and tcp_discardcb().
205 * KASSERT(inp != NULL, ("tcp_timer_2msl: inp == NULL"));
209 INP_INFO_WUNLOCK(&V_tcbinfo);
213 tcp_free_sackholes(tp);
214 if ((inp->inp_vflag & INP_DROPPED) || callout_pending(&tp->t_timers->tt_2msl) ||
215 !callout_active(&tp->t_timers->tt_2msl)) {
216 INP_WUNLOCK(tp->t_inpcb);
217 INP_INFO_WUNLOCK(&V_tcbinfo);
220 callout_deactivate(&tp->t_timers->tt_2msl);
222 * 2 MSL timeout in shutdown went off. If we're closed but
223 * still waiting for peer to close and connection has been idle
224 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
225 * control block. Otherwise, check again in a bit.
227 * If fastrecycle of FIN_WAIT_2, in FIN_WAIT_2 and receiver has closed,
228 * there's no point in hanging onto FIN_WAIT_2 socket. Just close it.
229 * Ignore fact that there were recent incoming segments.
231 if (tcp_fast_finwait2_recycle && tp->t_state == TCPS_FIN_WAIT_2 &&
232 tp->t_inpcb && tp->t_inpcb->inp_socket &&
233 (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE)) {
234 V_tcpstat.tcps_finwait2_drops++;
237 if (tp->t_state != TCPS_TIME_WAIT &&
238 (ticks - tp->t_rcvtime) <= tcp_maxidle)
239 callout_reset(&tp->t_timers->tt_2msl, tcp_keepintvl,
246 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
247 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
252 INP_INFO_WUNLOCK(&V_tcbinfo);
256 tcp_timer_keep(void *xtp)
258 struct tcpcb *tp = xtp;
259 struct tcptemp *t_template;
264 ostate = tp->t_state;
266 INP_INFO_WLOCK(&V_tcbinfo);
269 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
270 * tear-down mean we need it as a work-around for races between
271 * timers and tcp_discardcb().
273 * KASSERT(inp != NULL, ("tcp_timer_keep: inp == NULL"));
277 INP_INFO_WUNLOCK(&V_tcbinfo);
281 if ((inp->inp_vflag & INP_DROPPED) || callout_pending(&tp->t_timers->tt_keep)
282 || !callout_active(&tp->t_timers->tt_keep)) {
284 INP_INFO_WUNLOCK(&V_tcbinfo);
287 callout_deactivate(&tp->t_timers->tt_keep);
289 * Keep-alive timer went off; send something
290 * or drop connection if idle for too long.
292 V_tcpstat.tcps_keeptimeo++;
293 if (tp->t_state < TCPS_ESTABLISHED)
295 if ((always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
296 tp->t_state <= TCPS_CLOSING) {
297 if ((ticks - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle)
300 * Send a packet designed to force a response
301 * if the peer is up and reachable:
302 * either an ACK if the connection is still alive,
303 * or an RST if the peer has closed the connection
304 * due to timeout or reboot.
305 * Using sequence number tp->snd_una-1
306 * causes the transmitted zero-length segment
307 * to lie outside the receive window;
308 * by the protocol spec, this requires the
309 * correspondent TCP to respond.
311 V_tcpstat.tcps_keepprobe++;
312 t_template = tcpip_maketemplate(inp);
314 tcp_respond(tp, t_template->tt_ipgen,
315 &t_template->tt_t, (struct mbuf *)NULL,
316 tp->rcv_nxt, tp->snd_una - 1, 0);
317 free(t_template, M_TEMP);
319 callout_reset(&tp->t_timers->tt_keep, tcp_keepintvl, tcp_timer_keep, tp);
321 callout_reset(&tp->t_timers->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
324 if (inp->inp_socket->so_options & SO_DEBUG)
325 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
329 INP_INFO_WUNLOCK(&V_tcbinfo);
333 V_tcpstat.tcps_keepdrops++;
334 tp = tcp_drop(tp, ETIMEDOUT);
337 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
338 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
342 INP_WUNLOCK(tp->t_inpcb);
343 INP_INFO_WUNLOCK(&V_tcbinfo);
347 tcp_timer_persist(void *xtp)
349 struct tcpcb *tp = xtp;
354 ostate = tp->t_state;
356 INP_INFO_WLOCK(&V_tcbinfo);
359 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
360 * tear-down mean we need it as a work-around for races between
361 * timers and tcp_discardcb().
363 * KASSERT(inp != NULL, ("tcp_timer_persist: inp == NULL"));
367 INP_INFO_WUNLOCK(&V_tcbinfo);
371 if ((inp->inp_vflag & INP_DROPPED) || callout_pending(&tp->t_timers->tt_persist)
372 || !callout_active(&tp->t_timers->tt_persist)) {
374 INP_INFO_WUNLOCK(&V_tcbinfo);
377 callout_deactivate(&tp->t_timers->tt_persist);
379 * Persistance timer into zero window.
380 * Force a byte to be output, if possible.
382 V_tcpstat.tcps_persisttimeo++;
384 * Hack: if the peer is dead/unreachable, we do not
385 * time out if the window is closed. After a full
386 * backoff, drop the connection if the idle time
387 * (no responses to probes) reaches the maximum
388 * backoff that we would use if retransmitting.
390 if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
391 ((ticks - tp->t_rcvtime) >= tcp_maxpersistidle ||
392 (ticks - tp->t_rcvtime) >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
393 V_tcpstat.tcps_persistdrop++;
394 tp = tcp_drop(tp, ETIMEDOUT);
398 tp->t_flags |= TF_FORCEDATA;
399 (void) tcp_output(tp);
400 tp->t_flags &= ~TF_FORCEDATA;
404 if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
405 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
409 INP_INFO_WUNLOCK(&V_tcbinfo);
413 tcp_timer_rexmt(void * xtp)
415 struct tcpcb *tp = xtp;
422 ostate = tp->t_state;
424 INP_INFO_WLOCK(&V_tcbinfo);
428 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
429 * tear-down mean we need it as a work-around for races between
430 * timers and tcp_discardcb().
432 * KASSERT(inp != NULL, ("tcp_timer_rexmt: inp == NULL"));
436 INP_INFO_WUNLOCK(&V_tcbinfo);
440 if ((inp->inp_vflag & INP_DROPPED) || callout_pending(&tp->t_timers->tt_rexmt)
441 || !callout_active(&tp->t_timers->tt_rexmt)) {
443 INP_INFO_WUNLOCK(&V_tcbinfo);
446 callout_deactivate(&tp->t_timers->tt_rexmt);
447 tcp_free_sackholes(tp);
449 * Retransmission timer went off. Message has not
450 * been acked within retransmit interval. Back off
451 * to a longer retransmit interval and retransmit one segment.
453 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
454 tp->t_rxtshift = TCP_MAXRXTSHIFT;
455 V_tcpstat.tcps_timeoutdrop++;
456 tp = tcp_drop(tp, tp->t_softerror ?
457 tp->t_softerror : ETIMEDOUT);
460 INP_INFO_WUNLOCK(&V_tcbinfo);
462 if (tp->t_rxtshift == 1) {
464 * first retransmit; record ssthresh and cwnd so they can
465 * be recovered if this turns out to be a "bad" retransmit.
466 * A retransmit is considered "bad" if an ACK for this
467 * segment is received within RTT/2 interval; the assumption
468 * here is that the ACK was already in flight. See
469 * "On Estimating End-to-End Network Path Properties" by
470 * Allman and Paxson for more details.
472 tp->snd_cwnd_prev = tp->snd_cwnd;
473 tp->snd_ssthresh_prev = tp->snd_ssthresh;
474 tp->snd_recover_prev = tp->snd_recover;
475 if (IN_FASTRECOVERY(tp))
476 tp->t_flags |= TF_WASFRECOVERY;
478 tp->t_flags &= ~TF_WASFRECOVERY;
479 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
481 V_tcpstat.tcps_rexmttimeo++;
482 if (tp->t_state == TCPS_SYN_SENT)
483 rexmt = TCP_REXMTVAL(tp) * tcp_syn_backoff[tp->t_rxtshift];
485 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
486 TCPT_RANGESET(tp->t_rxtcur, rexmt,
487 tp->t_rttmin, TCPTV_REXMTMAX);
489 * Disable rfc1323 if we havn't got any response to
490 * our third SYN to work-around some broken terminal servers
491 * (most of which have hopefully been retired) that have bad VJ
492 * header compression code which trashes TCP segments containing
493 * unknown-to-them TCP options.
495 if ((tp->t_state == TCPS_SYN_SENT) && (tp->t_rxtshift == 3))
496 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP);
498 * If we backed off this far, our srtt estimate is probably bogus.
499 * Clobber it so we'll take the next rtt measurement as our srtt;
500 * move the current srtt into rttvar to keep the current
501 * retransmit times until then.
503 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
505 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
506 in6_losing(tp->t_inpcb);
509 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
512 tp->snd_nxt = tp->snd_una;
513 tp->snd_recover = tp->snd_max;
515 * Force a segment to be sent.
517 tp->t_flags |= TF_ACKNOW;
519 * If timing a segment in this window, stop the timer.
523 * Close the congestion window down to one segment
524 * (we'll open it by one segment for each ack we get).
525 * Since we probably have a window's worth of unacked
526 * data accumulated, this "slow start" keeps us from
527 * dumping all that data as back-to-back packets (which
528 * might overwhelm an intermediate gateway).
530 * There are two phases to the opening: Initially we
531 * open by one mss on each ack. This makes the window
532 * size increase exponentially with time. If the
533 * window is larger than the path can handle, this
534 * exponential growth results in dropped packet(s)
535 * almost immediately. To get more time between
536 * drops but still "push" the network to take advantage
537 * of improving conditions, we switch from exponential
538 * to linear window opening at some threshhold size.
539 * For a threshhold, we use half the current window
540 * size, truncated to a multiple of the mss.
542 * (the minimum cwnd that will give us exponential
543 * growth is 2 mss. We don't allow the threshhold
547 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
550 tp->snd_cwnd = tp->t_maxseg;
551 tp->snd_ssthresh = win * tp->t_maxseg;
554 EXIT_FASTRECOVERY(tp);
555 (void) tcp_output(tp);
559 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
560 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
566 INP_INFO_WUNLOCK(&V_tcbinfo);
570 tcp_timer_activate(struct tcpcb *tp, int timer_type, u_int delta)
572 struct callout *t_callout;
575 switch (timer_type) {
577 t_callout = &tp->t_timers->tt_delack;
578 f_callout = tcp_timer_delack;
581 t_callout = &tp->t_timers->tt_rexmt;
582 f_callout = tcp_timer_rexmt;
585 t_callout = &tp->t_timers->tt_persist;
586 f_callout = tcp_timer_persist;
589 t_callout = &tp->t_timers->tt_keep;
590 f_callout = tcp_timer_keep;
593 t_callout = &tp->t_timers->tt_2msl;
594 f_callout = tcp_timer_2msl;
597 panic("bad timer_type");
600 callout_stop(t_callout);
602 callout_reset(t_callout, delta, f_callout, tp);
607 tcp_timer_active(struct tcpcb *tp, int timer_type)
609 struct callout *t_callout;
611 switch (timer_type) {
613 t_callout = &tp->t_timers->tt_delack;
616 t_callout = &tp->t_timers->tt_rexmt;
619 t_callout = &tp->t_timers->tt_persist;
622 t_callout = &tp->t_timers->tt_keep;
625 t_callout = &tp->t_timers->tt_2msl;
628 panic("bad timer_type");
630 return callout_active(t_callout);