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
33 #include "opt_inet6.h"
34 #include "opt_tcpdebug.h"
35 #include "opt_tcp_sack.h"
37 #include <sys/param.h>
38 #include <sys/kernel.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>
48 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netinet/in_pcb.h>
52 #include <netinet/in_systm.h>
54 #include <netinet6/in6_pcb.h>
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>
63 #include <netinet/tcp_debug.h>
67 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
68 &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "");
71 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
72 &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "");
75 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
76 &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "");
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");
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");
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");
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");
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");
99 static int tcp_keepcnt = TCPTV_KEEPCNT;
100 /* max idle probes */
101 int tcp_maxpersistidle;
102 /* max idle time in persist */
106 * Tcp protocol timeout routine called every 500 ms.
107 * Updates timestamps used for TCP
108 * causes finite state machine actions if timers expire.
114 tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
115 INP_INFO_WLOCK(&tcbinfo);
116 (void) tcp_timer_2msl_tw(0);
117 INP_INFO_WUNLOCK(&tcbinfo);
120 int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
121 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
123 int tcp_backoff[TCP_MAXRXTSHIFT + 1] =
124 { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
126 static int tcp_totbackoff = 2559; /* sum of tcp_backoff[] */
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");
133 * TCP timer processing.
137 tcp_timer_delack(xtp)
140 struct tcpcb *tp = xtp;
143 INP_INFO_RLOCK(&tcbinfo);
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().
150 * KASSERT(inp != NULL, ("tcp_timer_delack: inp == NULL"));
154 INP_INFO_RUNLOCK(&tcbinfo);
158 INP_INFO_RUNLOCK(&tcbinfo);
159 if ((inp->inp_vflag & INP_DROPPED) || callout_pending(tp->tt_delack)
160 || !callout_active(tp->tt_delack)) {
164 callout_deactivate(tp->tt_delack);
166 tp->t_flags |= TF_ACKNOW;
167 tcpstat.tcps_delack++;
168 (void) tcp_output(tp);
176 struct tcpcb *tp = xtp;
181 ostate = tp->t_state;
184 * XXXRW: Does this actually happen?
186 INP_INFO_WLOCK(&tcbinfo);
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().
193 * KASSERT(inp != NULL, ("tcp_timer_2msl: inp == NULL"));
197 INP_INFO_WUNLOCK(&tcbinfo);
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);
208 callout_deactivate(tp->tt_2msl);
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.
215 if (tp->t_state != TCPS_TIME_WAIT &&
216 (ticks - tp->t_rcvtime) <= tcp_maxidle)
217 callout_reset(tp->tt_2msl, tcp_keepintvl,
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,
229 INP_INFO_WUNLOCK(&tcbinfo);
233 * The timed wait lists contain references to each of the TCP sessions
234 * currently TIME_WAIT state. The list pointers, including the list pointers
235 * in each tcptw structure, are protected using the global tcbinfo lock,
236 * which must be held over list iteration and modification.
239 LIST_HEAD(, tcptw) tw_list;
240 struct tcptw tw_tail;
242 #define TWLIST_NLISTS 2
243 static struct twlist twl_2msl[TWLIST_NLISTS];
244 static struct twlist *tw_2msl_list[] = { &twl_2msl[0], &twl_2msl[1], NULL };
252 for (i = 0; i < TWLIST_NLISTS; i++) {
254 LIST_INIT(&twl->tw_list);
255 LIST_INSERT_HEAD(&twl->tw_list, &twl->tw_tail, tw_2msl);
260 tcp_timer_2msl_reset(struct tcptw *tw, int timeo, int rearm)
263 struct tcptw *tw_tail;
265 INP_INFO_WLOCK_ASSERT(&tcbinfo);
266 INP_LOCK_ASSERT(tw->tw_inpcb);
268 LIST_REMOVE(tw, tw_2msl);
269 tw->tw_time = timeo + ticks;
270 i = timeo > tcp_msl ? 1 : 0;
271 tw_tail = &twl_2msl[i].tw_tail;
272 LIST_INSERT_BEFORE(tw_tail, tw, tw_2msl);
276 tcp_timer_2msl_stop(struct tcptw *tw)
279 INP_INFO_WLOCK_ASSERT(&tcbinfo);
280 LIST_REMOVE(tw, tw_2msl);
284 tcp_timer_2msl_tw(int reuse)
286 struct tcptw *tw, *tw_tail;
290 INP_INFO_WLOCK_ASSERT(&tcbinfo);
291 for (i = 0; i < TWLIST_NLISTS; i++) {
292 twl = tw_2msl_list[i];
293 tw_tail = &twl->tw_tail;
296 tw = LIST_FIRST(&twl->tw_list);
297 if (tw == tw_tail || (!reuse && tw->tw_time > ticks))
299 INP_LOCK(tw->tw_inpcb);
300 tcp_twclose(tw, reuse);
313 struct tcpcb *tp = xtp;
314 struct tcptemp *t_template;
319 ostate = tp->t_state;
321 INP_INFO_WLOCK(&tcbinfo);
324 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
325 * tear-down mean we need it as a work-around for races between
326 * timers and tcp_discardcb().
328 * KASSERT(inp != NULL, ("tcp_timer_keep: inp == NULL"));
332 INP_INFO_WUNLOCK(&tcbinfo);
336 if ((inp->inp_vflag & INP_DROPPED) || callout_pending(tp->tt_keep)
337 || !callout_active(tp->tt_keep)) {
339 INP_INFO_WUNLOCK(&tcbinfo);
342 callout_deactivate(tp->tt_keep);
344 * Keep-alive timer went off; send something
345 * or drop connection if idle for too long.
347 tcpstat.tcps_keeptimeo++;
348 if (tp->t_state < TCPS_ESTABLISHED)
350 if ((always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
351 tp->t_state <= TCPS_CLOSING) {
352 if ((ticks - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle)
355 * Send a packet designed to force a response
356 * if the peer is up and reachable:
357 * either an ACK if the connection is still alive,
358 * or an RST if the peer has closed the connection
359 * due to timeout or reboot.
360 * Using sequence number tp->snd_una-1
361 * causes the transmitted zero-length segment
362 * to lie outside the receive window;
363 * by the protocol spec, this requires the
364 * correspondent TCP to respond.
366 tcpstat.tcps_keepprobe++;
367 t_template = tcpip_maketemplate(inp);
369 tcp_respond(tp, t_template->tt_ipgen,
370 &t_template->tt_t, (struct mbuf *)NULL,
371 tp->rcv_nxt, tp->snd_una - 1, 0);
372 (void) m_free(dtom(t_template));
374 callout_reset(tp->tt_keep, tcp_keepintvl, tcp_timer_keep, tp);
376 callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
379 if (inp->inp_socket->so_options & SO_DEBUG)
380 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
384 INP_INFO_WUNLOCK(&tcbinfo);
388 tcpstat.tcps_keepdrops++;
389 tp = tcp_drop(tp, ETIMEDOUT);
392 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
393 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
397 INP_UNLOCK(tp->t_inpcb);
398 INP_INFO_WUNLOCK(&tcbinfo);
402 tcp_timer_persist(xtp)
405 struct tcpcb *tp = xtp;
410 ostate = tp->t_state;
412 INP_INFO_WLOCK(&tcbinfo);
415 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
416 * tear-down mean we need it as a work-around for races between
417 * timers and tcp_discardcb().
419 * KASSERT(inp != NULL, ("tcp_timer_persist: inp == NULL"));
423 INP_INFO_WUNLOCK(&tcbinfo);
427 if ((inp->inp_vflag & INP_DROPPED) || callout_pending(tp->tt_persist)
428 || !callout_active(tp->tt_persist)) {
430 INP_INFO_WUNLOCK(&tcbinfo);
433 callout_deactivate(tp->tt_persist);
435 * Persistance timer into zero window.
436 * Force a byte to be output, if possible.
438 tcpstat.tcps_persisttimeo++;
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.
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);
454 tp->t_flags |= TF_FORCEDATA;
455 (void) tcp_output(tp);
456 tp->t_flags &= ~TF_FORCEDATA;
460 if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
461 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
465 INP_INFO_WUNLOCK(&tcbinfo);
472 struct tcpcb *tp = xtp;
479 ostate = tp->t_state;
481 INP_INFO_WLOCK(&tcbinfo);
485 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
486 * tear-down mean we need it as a work-around for races between
487 * timers and tcp_discardcb().
489 * KASSERT(inp != NULL, ("tcp_timer_rexmt: inp == NULL"));
493 INP_INFO_WUNLOCK(&tcbinfo);
497 if ((inp->inp_vflag & INP_DROPPED) || callout_pending(tp->tt_rexmt)
498 || !callout_active(tp->tt_rexmt)) {
500 INP_INFO_WUNLOCK(&tcbinfo);
503 callout_deactivate(tp->tt_rexmt);
504 tcp_free_sackholes(tp);
506 * Retransmission timer went off. Message has not
507 * been acked within retransmit interval. Back off
508 * to a longer retransmit interval and retransmit one segment.
510 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
511 tp->t_rxtshift = TCP_MAXRXTSHIFT;
512 tcpstat.tcps_timeoutdrop++;
513 tp = tcp_drop(tp, tp->t_softerror ?
514 tp->t_softerror : ETIMEDOUT);
517 INP_INFO_WUNLOCK(&tcbinfo);
519 if (tp->t_rxtshift == 1) {
521 * first retransmit; record ssthresh and cwnd so they can
522 * be recovered if this turns out to be a "bad" retransmit.
523 * A retransmit is considered "bad" if an ACK for this
524 * segment is received within RTT/2 interval; the assumption
525 * here is that the ACK was already in flight. See
526 * "On Estimating End-to-End Network Path Properties" by
527 * Allman and Paxson for more details.
529 tp->snd_cwnd_prev = tp->snd_cwnd;
530 tp->snd_ssthresh_prev = tp->snd_ssthresh;
531 tp->snd_recover_prev = tp->snd_recover;
532 if (IN_FASTRECOVERY(tp))
533 tp->t_flags |= TF_WASFRECOVERY;
535 tp->t_flags &= ~TF_WASFRECOVERY;
536 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
538 tcpstat.tcps_rexmttimeo++;
539 if (tp->t_state == TCPS_SYN_SENT)
540 rexmt = TCP_REXMTVAL(tp) * tcp_syn_backoff[tp->t_rxtshift];
542 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
543 TCPT_RANGESET(tp->t_rxtcur, rexmt,
544 tp->t_rttmin, TCPTV_REXMTMAX);
546 * Disable rfc1323 if we havn't got any response to
547 * our third SYN to work-around some broken terminal servers
548 * (most of which have hopefully been retired) that have bad VJ
549 * header compression code which trashes TCP segments containing
550 * unknown-to-them TCP options.
552 if ((tp->t_state == TCPS_SYN_SENT) && (tp->t_rxtshift == 3))
553 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP);
555 * If we backed off this far, our srtt estimate is probably bogus.
556 * Clobber it so we'll take the next rtt measurement as our srtt;
557 * move the current srtt into rttvar to keep the current
558 * retransmit times until then.
560 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
562 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
563 in6_losing(tp->t_inpcb);
566 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
569 tp->snd_nxt = tp->snd_una;
570 tp->snd_recover = tp->snd_max;
572 * Force a segment to be sent.
574 tp->t_flags |= TF_ACKNOW;
576 * If timing a segment in this window, stop the timer.
580 * Close the congestion window down to one segment
581 * (we'll open it by one segment for each ack we get).
582 * Since we probably have a window's worth of unacked
583 * data accumulated, this "slow start" keeps us from
584 * dumping all that data as back-to-back packets (which
585 * might overwhelm an intermediate gateway).
587 * There are two phases to the opening: Initially we
588 * open by one mss on each ack. This makes the window
589 * size increase exponentially with time. If the
590 * window is larger than the path can handle, this
591 * exponential growth results in dropped packet(s)
592 * almost immediately. To get more time between
593 * drops but still "push" the network to take advantage
594 * of improving conditions, we switch from exponential
595 * to linear window opening at some threshhold size.
596 * For a threshhold, we use half the current window
597 * size, truncated to a multiple of the mss.
599 * (the minimum cwnd that will give us exponential
600 * growth is 2 mss. We don't allow the threshhold
604 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
607 tp->snd_cwnd = tp->t_maxseg;
608 tp->snd_ssthresh = win * tp->t_maxseg;
611 EXIT_FASTRECOVERY(tp);
612 (void) tcp_output(tp);
616 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
617 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
623 INP_INFO_WUNLOCK(&tcbinfo);