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>
50 #include <net/route.h>
53 #include <netinet/in.h>
54 #include <netinet/in_pcb.h>
55 #include <netinet/in_systm.h>
57 #include <netinet6/in6_pcb.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/tcp.h>
61 #include <netinet/tcp_fsm.h>
62 #include <netinet/tcp_timer.h>
63 #include <netinet/tcp_var.h>
64 #include <netinet/tcpip.h>
66 #include <netinet/tcp_debug.h>
70 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
71 &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "time to establish connection");
74 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
75 &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "time before keepalive probes begin");
78 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
79 &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "time between keepalive probes");
82 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime, CTLTYPE_INT|CTLFLAG_RW,
83 &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
84 "Time before a delayed ACK is sent");
87 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
88 &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
91 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
92 &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I",
93 "Minimum Retransmission Timeout");
96 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
97 &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I",
98 "Retransmission Timer Slop");
100 static int always_keepalive = 1;
101 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
102 &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
104 int tcp_fast_finwait2_recycle = 0;
105 SYSCTL_INT(_net_inet_tcp, OID_AUTO, fast_finwait2_recycle, CTLFLAG_RW,
106 &tcp_fast_finwait2_recycle, 0,
107 "Recycle closed FIN_WAIT_2 connections faster");
109 int tcp_finwait2_timeout;
110 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, finwait2_timeout, CTLTYPE_INT|CTLFLAG_RW,
111 &tcp_finwait2_timeout, 0, sysctl_msec_to_ticks, "I", "FIN-WAIT2 timeout");
114 static int tcp_keepcnt = TCPTV_KEEPCNT;
115 /* max idle probes */
116 int tcp_maxpersistidle;
117 /* max idle time in persist */
121 * Tcp protocol timeout routine called every 500 ms.
122 * Updates timestamps used for TCP
123 * causes finite state machine actions if timers expire.
128 VNET_ITERATOR_DECL(vnet_iter);
130 VNET_LIST_RLOCK_NOSLEEP();
131 VNET_FOREACH(vnet_iter) {
132 CURVNET_SET(vnet_iter);
133 tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
134 INP_INFO_WLOCK(&V_tcbinfo);
135 (void) tcp_tw_2msl_scan(0);
136 INP_INFO_WUNLOCK(&V_tcbinfo);
139 VNET_LIST_RUNLOCK_NOSLEEP();
142 int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
143 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
145 int tcp_backoff[TCP_MAXRXTSHIFT + 1] =
146 { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
148 static int tcp_totbackoff = 2559; /* sum of tcp_backoff[] */
150 static int tcp_timer_race;
151 SYSCTL_INT(_net_inet_tcp, OID_AUTO, timer_race, CTLFLAG_RD, &tcp_timer_race,
152 0, "Count of t_inpcb races on tcp_discardcb");
155 * TCP timer processing.
159 tcp_timer_delack(void *xtp)
161 struct tcpcb *tp = xtp;
163 CURVNET_SET(tp->t_vnet);
167 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
168 * tear-down mean we need it as a work-around for races between
169 * timers and tcp_discardcb().
171 * KASSERT(inp != NULL, ("tcp_timer_delack: inp == NULL"));
179 if ((inp->inp_flags & INP_DROPPED) || callout_pending(&tp->t_timers->tt_delack)
180 || !callout_active(&tp->t_timers->tt_delack)) {
185 callout_deactivate(&tp->t_timers->tt_delack);
187 tp->t_flags |= TF_ACKNOW;
188 TCPSTAT_INC(tcps_delack);
189 (void) tcp_output(tp);
195 tcp_timer_2msl(void *xtp)
197 struct tcpcb *tp = xtp;
199 CURVNET_SET(tp->t_vnet);
203 ostate = tp->t_state;
206 * XXXRW: Does this actually happen?
208 INP_INFO_WLOCK(&V_tcbinfo);
211 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
212 * tear-down mean we need it as a work-around for races between
213 * timers and tcp_discardcb().
215 * KASSERT(inp != NULL, ("tcp_timer_2msl: inp == NULL"));
219 INP_INFO_WUNLOCK(&V_tcbinfo);
224 tcp_free_sackholes(tp);
225 if ((inp->inp_flags & INP_DROPPED) || callout_pending(&tp->t_timers->tt_2msl) ||
226 !callout_active(&tp->t_timers->tt_2msl)) {
227 INP_WUNLOCK(tp->t_inpcb);
228 INP_INFO_WUNLOCK(&V_tcbinfo);
232 callout_deactivate(&tp->t_timers->tt_2msl);
234 * 2 MSL timeout in shutdown went off. If we're closed but
235 * still waiting for peer to close and connection has been idle
236 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
237 * control block. Otherwise, check again in a bit.
239 * If fastrecycle of FIN_WAIT_2, in FIN_WAIT_2 and receiver has closed,
240 * there's no point in hanging onto FIN_WAIT_2 socket. Just close it.
241 * Ignore fact that there were recent incoming segments.
243 if (tcp_fast_finwait2_recycle && tp->t_state == TCPS_FIN_WAIT_2 &&
244 tp->t_inpcb && tp->t_inpcb->inp_socket &&
245 (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE)) {
246 TCPSTAT_INC(tcps_finwait2_drops);
249 if (tp->t_state != TCPS_TIME_WAIT &&
250 ticks - tp->t_rcvtime <= tcp_maxidle)
251 callout_reset(&tp->t_timers->tt_2msl, tcp_keepintvl,
258 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
259 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
264 INP_INFO_WUNLOCK(&V_tcbinfo);
269 tcp_timer_keep(void *xtp)
271 struct tcpcb *tp = xtp;
272 struct tcptemp *t_template;
274 CURVNET_SET(tp->t_vnet);
278 ostate = tp->t_state;
280 INP_INFO_WLOCK(&V_tcbinfo);
283 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
284 * tear-down mean we need it as a work-around for races between
285 * timers and tcp_discardcb().
287 * KASSERT(inp != NULL, ("tcp_timer_keep: inp == NULL"));
291 INP_INFO_WUNLOCK(&V_tcbinfo);
296 if ((inp->inp_flags & INP_DROPPED) || callout_pending(&tp->t_timers->tt_keep)
297 || !callout_active(&tp->t_timers->tt_keep)) {
299 INP_INFO_WUNLOCK(&V_tcbinfo);
303 callout_deactivate(&tp->t_timers->tt_keep);
305 * Keep-alive timer went off; send something
306 * or drop connection if idle for too long.
308 TCPSTAT_INC(tcps_keeptimeo);
309 if (tp->t_state < TCPS_ESTABLISHED)
311 if ((always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
312 tp->t_state <= TCPS_CLOSING) {
313 if (ticks - tp->t_rcvtime >= tcp_keepidle + tcp_maxidle)
316 * Send a packet designed to force a response
317 * if the peer is up and reachable:
318 * either an ACK if the connection is still alive,
319 * or an RST if the peer has closed the connection
320 * due to timeout or reboot.
321 * Using sequence number tp->snd_una-1
322 * causes the transmitted zero-length segment
323 * to lie outside the receive window;
324 * by the protocol spec, this requires the
325 * correspondent TCP to respond.
327 TCPSTAT_INC(tcps_keepprobe);
328 t_template = tcpip_maketemplate(inp);
330 tcp_respond(tp, t_template->tt_ipgen,
331 &t_template->tt_t, (struct mbuf *)NULL,
332 tp->rcv_nxt, tp->snd_una - 1, 0);
333 free(t_template, M_TEMP);
335 callout_reset(&tp->t_timers->tt_keep, tcp_keepintvl, tcp_timer_keep, tp);
337 callout_reset(&tp->t_timers->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
340 if (inp->inp_socket->so_options & SO_DEBUG)
341 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
345 INP_INFO_WUNLOCK(&V_tcbinfo);
350 TCPSTAT_INC(tcps_keepdrops);
351 tp = tcp_drop(tp, ETIMEDOUT);
354 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
355 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
359 INP_WUNLOCK(tp->t_inpcb);
360 INP_INFO_WUNLOCK(&V_tcbinfo);
365 tcp_timer_persist(void *xtp)
367 struct tcpcb *tp = xtp;
369 CURVNET_SET(tp->t_vnet);
373 ostate = tp->t_state;
375 INP_INFO_WLOCK(&V_tcbinfo);
378 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
379 * tear-down mean we need it as a work-around for races between
380 * timers and tcp_discardcb().
382 * KASSERT(inp != NULL, ("tcp_timer_persist: inp == NULL"));
386 INP_INFO_WUNLOCK(&V_tcbinfo);
391 if ((inp->inp_flags & INP_DROPPED) || callout_pending(&tp->t_timers->tt_persist)
392 || !callout_active(&tp->t_timers->tt_persist)) {
394 INP_INFO_WUNLOCK(&V_tcbinfo);
398 callout_deactivate(&tp->t_timers->tt_persist);
400 * Persistance timer into zero window.
401 * Force a byte to be output, if possible.
403 TCPSTAT_INC(tcps_persisttimeo);
405 * Hack: if the peer is dead/unreachable, we do not
406 * time out if the window is closed. After a full
407 * backoff, drop the connection if the idle time
408 * (no responses to probes) reaches the maximum
409 * backoff that we would use if retransmitting.
411 if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
412 (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
413 ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
414 TCPSTAT_INC(tcps_persistdrop);
415 tp = tcp_drop(tp, ETIMEDOUT);
419 tp->t_flags |= TF_FORCEDATA;
420 (void) tcp_output(tp);
421 tp->t_flags &= ~TF_FORCEDATA;
425 if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
426 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
430 INP_INFO_WUNLOCK(&V_tcbinfo);
435 tcp_timer_rexmt(void * xtp)
437 struct tcpcb *tp = xtp;
438 CURVNET_SET(tp->t_vnet);
445 ostate = tp->t_state;
447 INP_INFO_WLOCK(&V_tcbinfo);
451 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
452 * tear-down mean we need it as a work-around for races between
453 * timers and tcp_discardcb().
455 * KASSERT(inp != NULL, ("tcp_timer_rexmt: inp == NULL"));
459 INP_INFO_WUNLOCK(&V_tcbinfo);
464 if ((inp->inp_flags & INP_DROPPED) || callout_pending(&tp->t_timers->tt_rexmt)
465 || !callout_active(&tp->t_timers->tt_rexmt)) {
467 INP_INFO_WUNLOCK(&V_tcbinfo);
471 callout_deactivate(&tp->t_timers->tt_rexmt);
472 tcp_free_sackholes(tp);
474 * Retransmission timer went off. Message has not
475 * been acked within retransmit interval. Back off
476 * to a longer retransmit interval and retransmit one segment.
478 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
479 tp->t_rxtshift = TCP_MAXRXTSHIFT;
480 TCPSTAT_INC(tcps_timeoutdrop);
481 tp = tcp_drop(tp, tp->t_softerror ?
482 tp->t_softerror : ETIMEDOUT);
485 INP_INFO_WUNLOCK(&V_tcbinfo);
487 if (tp->t_rxtshift == 1) {
489 * first retransmit; record ssthresh and cwnd so they can
490 * be recovered if this turns out to be a "bad" retransmit.
491 * A retransmit is considered "bad" if an ACK for this
492 * segment is received within RTT/2 interval; the assumption
493 * here is that the ACK was already in flight. See
494 * "On Estimating End-to-End Network Path Properties" by
495 * Allman and Paxson for more details.
497 tp->snd_cwnd_prev = tp->snd_cwnd;
498 tp->snd_ssthresh_prev = tp->snd_ssthresh;
499 tp->snd_recover_prev = tp->snd_recover;
500 if (IN_FASTRECOVERY(tp))
501 tp->t_flags |= TF_WASFRECOVERY;
503 tp->t_flags &= ~TF_WASFRECOVERY;
504 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
506 TCPSTAT_INC(tcps_rexmttimeo);
507 if (tp->t_state == TCPS_SYN_SENT)
508 rexmt = TCP_REXMTVAL(tp) * tcp_syn_backoff[tp->t_rxtshift];
510 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
511 TCPT_RANGESET(tp->t_rxtcur, rexmt,
512 tp->t_rttmin, TCPTV_REXMTMAX);
514 * Disable rfc1323 if we havn't got any response to
515 * our third SYN to work-around some broken terminal servers
516 * (most of which have hopefully been retired) that have bad VJ
517 * header compression code which trashes TCP segments containing
518 * unknown-to-them TCP options.
520 if ((tp->t_state == TCPS_SYN_SENT) && (tp->t_rxtshift == 3))
521 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP);
523 * If we backed off this far, our srtt estimate is probably bogus.
524 * Clobber it so we'll take the next rtt measurement as our srtt;
525 * move the current srtt into rttvar to keep the current
526 * retransmit times until then.
528 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
530 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
531 in6_losing(tp->t_inpcb);
534 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
537 tp->snd_nxt = tp->snd_una;
538 tp->snd_recover = tp->snd_max;
540 * Force a segment to be sent.
542 tp->t_flags |= TF_ACKNOW;
544 * If timing a segment in this window, stop the timer.
548 * Close the congestion window down to one segment
549 * (we'll open it by one segment for each ack we get).
550 * Since we probably have a window's worth of unacked
551 * data accumulated, this "slow start" keeps us from
552 * dumping all that data as back-to-back packets (which
553 * might overwhelm an intermediate gateway).
555 * There are two phases to the opening: Initially we
556 * open by one mss on each ack. This makes the window
557 * size increase exponentially with time. If the
558 * window is larger than the path can handle, this
559 * exponential growth results in dropped packet(s)
560 * almost immediately. To get more time between
561 * drops but still "push" the network to take advantage
562 * of improving conditions, we switch from exponential
563 * to linear window opening at some threshhold size.
564 * For a threshhold, we use half the current window
565 * size, truncated to a multiple of the mss.
567 * (the minimum cwnd that will give us exponential
568 * growth is 2 mss. We don't allow the threshhold
572 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
575 tp->snd_cwnd = tp->t_maxseg;
576 tp->snd_ssthresh = win * tp->t_maxseg;
579 EXIT_FASTRECOVERY(tp);
580 tp->t_bytes_acked = 0;
581 (void) tcp_output(tp);
585 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
586 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
592 INP_INFO_WUNLOCK(&V_tcbinfo);
597 tcp_timer_activate(struct tcpcb *tp, int timer_type, u_int delta)
599 struct callout *t_callout;
602 switch (timer_type) {
604 t_callout = &tp->t_timers->tt_delack;
605 f_callout = tcp_timer_delack;
608 t_callout = &tp->t_timers->tt_rexmt;
609 f_callout = tcp_timer_rexmt;
612 t_callout = &tp->t_timers->tt_persist;
613 f_callout = tcp_timer_persist;
616 t_callout = &tp->t_timers->tt_keep;
617 f_callout = tcp_timer_keep;
620 t_callout = &tp->t_timers->tt_2msl;
621 f_callout = tcp_timer_2msl;
624 panic("bad timer_type");
627 callout_stop(t_callout);
629 callout_reset(t_callout, delta, f_callout, tp);
634 tcp_timer_active(struct tcpcb *tp, int timer_type)
636 struct callout *t_callout;
638 switch (timer_type) {
640 t_callout = &tp->t_timers->tt_delack;
643 t_callout = &tp->t_timers->tt_rexmt;
646 t_callout = &tp->t_timers->tt_persist;
649 t_callout = &tp->t_timers->tt_keep;
652 t_callout = &tp->t_timers->tt_2msl;
655 panic("bad timer_type");
657 return callout_active(t_callout);