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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95
37 #include "opt_inet6.h"
38 #include "opt_tcpdebug.h"
40 #include <sys/param.h>
41 #include <sys/kernel.h>
44 #include <sys/mutex.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
51 #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_msec_to_ticks(SYSCTL_HANDLER_ARGS)
74 tt = *(int *)oidp->oid_arg1;
75 s = (int)((int64_t)tt * 1000 / hz);
77 error = sysctl_handle_int(oidp, &s, 0, req);
78 if (error || !req->newptr)
81 tt = (int)((int64_t)s * hz / 1000);
85 *(int *)oidp->oid_arg1 = tt;
90 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
91 &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "");
94 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
95 &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "");
98 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
99 &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "");
102 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime,
103 CTLTYPE_INT|CTLFLAG_RW, &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
104 "Time before a delayed ACK is sent");
107 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
108 &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
111 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
112 &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I", "Minimum Retransmission Timeout");
115 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
116 &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I", "Retransmission Timer Slop");
118 static int always_keepalive = 1;
119 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
120 &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
122 static int tcp_keepcnt = TCPTV_KEEPCNT;
123 /* max idle probes */
124 int tcp_maxpersistidle;
125 /* max idle time in persist */
129 * Tcp protocol timeout routine called every 500 ms.
130 * Updates timestamps used for TCP
131 * causes finite state machine actions if timers expire.
139 tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
141 INP_INFO_WLOCK(&tcbinfo);
142 (void) tcp_timer_2msl_tw(0);
143 INP_INFO_WUNLOCK(&tcbinfo);
147 * Cancel all timers for TCP tp.
153 callout_stop(tp->tt_2msl);
154 callout_stop(tp->tt_persist);
155 callout_stop(tp->tt_keep);
156 callout_stop(tp->tt_rexmt);
159 int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
160 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
162 int tcp_backoff[TCP_MAXRXTSHIFT + 1] =
163 { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
165 static int tcp_totbackoff = 2559; /* sum of tcp_backoff[] */
168 * TCP timer processing.
172 tcp_timer_delack(xtp)
175 struct tcpcb *tp = xtp;
180 INP_INFO_RLOCK(&tcbinfo);
183 INP_INFO_RUNLOCK(&tcbinfo);
188 INP_INFO_RUNLOCK(&tcbinfo);
189 if (callout_pending(tp->tt_delack) || !callout_active(tp->tt_delack)) {
194 callout_deactivate(tp->tt_delack);
196 tp->t_flags |= TF_ACKNOW;
197 tcpstat.tcps_delack++;
198 (void) tcp_output(tp);
207 struct tcpcb *tp = xtp;
213 ostate = tp->t_state;
216 INP_INFO_WLOCK(&tcbinfo);
219 INP_INFO_WUNLOCK(&tcbinfo);
224 if (callout_pending(tp->tt_2msl) || !callout_active(tp->tt_2msl)) {
225 INP_UNLOCK(tp->t_inpcb);
226 INP_INFO_WUNLOCK(&tcbinfo);
230 callout_deactivate(tp->tt_2msl);
232 * 2 MSL timeout in shutdown went off. If we're closed but
233 * still waiting for peer to close and connection has been idle
234 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
235 * control block. Otherwise, check again in a bit.
237 if (tp->t_state != TCPS_TIME_WAIT &&
238 (ticks - tp->t_rcvtime) <= tcp_maxidle)
239 callout_reset(tp->tt_2msl, tcp_keepintvl,
245 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
246 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
251 INP_INFO_WUNLOCK(&tcbinfo);
256 LIST_HEAD(, tcptw) tw_list;
257 struct tcptw tw_tail;
259 #define TWLIST_NLISTS 2
260 static struct twlist twl_2msl[TWLIST_NLISTS];
261 static struct twlist *tw_2msl_list[] = { &twl_2msl[0], &twl_2msl[1], NULL };
269 for (i = 0; i < TWLIST_NLISTS; i++) {
271 LIST_INIT(&twl->tw_list);
272 LIST_INSERT_HEAD(&twl->tw_list, &twl->tw_tail, tw_2msl);
277 tcp_timer_2msl_reset(struct tcptw *tw, int timeo)
280 struct tcptw *tw_tail;
282 if (tw->tw_time != 0)
283 LIST_REMOVE(tw, tw_2msl);
284 tw->tw_time = timeo + ticks;
285 i = timeo > tcp_msl ? 1 : 0;
286 tw_tail = &twl_2msl[i].tw_tail;
287 LIST_INSERT_BEFORE(tw_tail, tw, tw_2msl);
291 tcp_timer_2msl_stop(struct tcptw *tw)
294 if (tw->tw_time != 0)
295 LIST_REMOVE(tw, tw_2msl);
299 tcp_timer_2msl_tw(int reuse)
301 struct tcptw *tw, *tw_tail;
305 for (i = 0; i < 2; i++) {
306 twl = tw_2msl_list[i];
307 tw_tail = &twl->tw_tail;
309 tw = LIST_FIRST(&twl->tw_list);
310 if (tw == tw_tail || (!reuse && tw->tw_time > ticks))
312 INP_LOCK(tw->tw_inpcb);
313 if (tcp_twclose(tw, reuse) != NULL)
324 struct tcpcb *tp = xtp;
325 struct tcptemp *t_template;
331 ostate = tp->t_state;
334 INP_INFO_WLOCK(&tcbinfo);
337 INP_INFO_WUNLOCK(&tcbinfo);
342 if (callout_pending(tp->tt_keep) || !callout_active(tp->tt_keep)) {
344 INP_INFO_WUNLOCK(&tcbinfo);
348 callout_deactivate(tp->tt_keep);
350 * Keep-alive timer went off; send something
351 * or drop connection if idle for too long.
353 tcpstat.tcps_keeptimeo++;
354 if (tp->t_state < TCPS_ESTABLISHED)
356 if ((always_keepalive ||
357 tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) &&
358 tp->t_state <= TCPS_CLOSING) {
359 if ((ticks - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle)
362 * Send a packet designed to force a response
363 * if the peer is up and reachable:
364 * either an ACK if the connection is still alive,
365 * or an RST if the peer has closed the connection
366 * due to timeout or reboot.
367 * Using sequence number tp->snd_una-1
368 * causes the transmitted zero-length segment
369 * to lie outside the receive window;
370 * by the protocol spec, this requires the
371 * correspondent TCP to respond.
373 tcpstat.tcps_keepprobe++;
374 t_template = tcpip_maketemplate(inp);
376 tcp_respond(tp, t_template->tt_ipgen,
377 &t_template->tt_t, (struct mbuf *)NULL,
378 tp->rcv_nxt, tp->snd_una - 1, 0);
379 (void) m_free(dtom(t_template));
381 callout_reset(tp->tt_keep, tcp_keepintvl, tcp_timer_keep, tp);
383 callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
386 if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
387 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
391 INP_INFO_WUNLOCK(&tcbinfo);
396 tcpstat.tcps_keepdrops++;
397 tp = tcp_drop(tp, ETIMEDOUT);
400 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
401 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
405 INP_UNLOCK(tp->t_inpcb);
406 INP_INFO_WUNLOCK(&tcbinfo);
411 tcp_timer_persist(xtp)
414 struct tcpcb *tp = xtp;
420 ostate = tp->t_state;
423 INP_INFO_WLOCK(&tcbinfo);
426 INP_INFO_WUNLOCK(&tcbinfo);
431 if (callout_pending(tp->tt_persist) || !callout_active(tp->tt_persist)){
433 INP_INFO_WUNLOCK(&tcbinfo);
437 callout_deactivate(tp->tt_persist);
439 * Persistance timer into zero window.
440 * Force a byte to be output, if possible.
442 tcpstat.tcps_persisttimeo++;
444 * Hack: if the peer is dead/unreachable, we do not
445 * time out if the window is closed. After a full
446 * backoff, drop the connection if the idle time
447 * (no responses to probes) reaches the maximum
448 * backoff that we would use if retransmitting.
450 if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
451 ((ticks - tp->t_rcvtime) >= tcp_maxpersistidle ||
452 (ticks - tp->t_rcvtime) >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
453 tcpstat.tcps_persistdrop++;
454 tp = tcp_drop(tp, ETIMEDOUT);
459 (void) tcp_output(tp);
464 if (tp && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
465 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
470 INP_INFO_WUNLOCK(&tcbinfo);
478 struct tcpcb *tp = xtp;
486 ostate = tp->t_state;
489 INP_INFO_WLOCK(&tcbinfo);
493 INP_INFO_WUNLOCK(&tcbinfo);
498 if (callout_pending(tp->tt_rexmt) || !callout_active(tp->tt_rexmt)) {
500 INP_INFO_WUNLOCK(&tcbinfo);
504 callout_deactivate(tp->tt_rexmt);
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 and rfc1644 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|TF_REQ_CC);
555 * If losing, let the lower level know and try for
556 * a better route. Also, if we backed off this far,
557 * our srtt estimate is probably bogus. Clobber it
558 * so we'll take the next rtt measurement as our srtt;
559 * move the current srtt into rttvar to keep the current
560 * retransmit times until then.
562 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
564 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
565 in6_losing(tp->t_inpcb);
568 in_losing(tp->t_inpcb);
569 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
572 tp->snd_nxt = tp->snd_una;
573 tp->snd_recover = tp->snd_max;
575 * Force a segment to be sent.
577 tp->t_flags |= TF_ACKNOW;
579 * If timing a segment in this window, stop the timer.
583 * Close the congestion window down to one segment
584 * (we'll open it by one segment for each ack we get).
585 * Since we probably have a window's worth of unacked
586 * data accumulated, this "slow start" keeps us from
587 * dumping all that data as back-to-back packets (which
588 * might overwhelm an intermediate gateway).
590 * There are two phases to the opening: Initially we
591 * open by one mss on each ack. This makes the window
592 * size increase exponentially with time. If the
593 * window is larger than the path can handle, this
594 * exponential growth results in dropped packet(s)
595 * almost immediately. To get more time between
596 * drops but still "push" the network to take advantage
597 * of improving conditions, we switch from exponential
598 * to linear window opening at some threshhold size.
599 * For a threshhold, we use half the current window
600 * size, truncated to a multiple of the mss.
602 * (the minimum cwnd that will give us exponential
603 * growth is 2 mss. We don't allow the threshhold
607 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
610 tp->snd_cwnd = tp->t_maxseg;
611 tp->snd_ssthresh = win * tp->t_maxseg;
614 EXIT_FASTRECOVERY(tp);
615 (void) tcp_output(tp);
619 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
620 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
626 INP_INFO_WUNLOCK(&tcbinfo);