1 //==========================================================================
3 // src/sys/netinet/tcp_timer.c
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
8 // -------------------------------------------
10 // Portions of this software may have been derived from OpenBSD,
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
17 // -------------------------------------------
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
23 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95
55 * $FreeBSD: src/sys/netinet/tcp_timer.c,v 1.34.2.11 2001/08/22 00:59:12 silby Exp $
58 #include <sys/param.h>
59 #include <sys/sysctl.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
63 #include <sys/protosw.h>
65 #include <net/route.h>
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/in_pcb.h>
71 #include <netinet6/in6_pcb.h>
73 #include <netinet/ip_var.h>
74 #include <netinet/tcp.h>
75 #include <netinet/tcp_fsm.h>
76 #include <netinet/tcp_seq.h>
77 #include <netinet/tcp_timer.h>
78 #include <netinet/tcp_var.h>
79 #include <netinet/tcpip.h>
81 #include <netinet/tcp_debug.h>
84 #ifdef CYGPKG_NET_FREEBSD_SYSCTL
86 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
90 tt = *(int *)oidp->oid_arg1;
93 error = sysctl_handle_int(oidp, &s, 0, req);
94 if (error || !req->newptr)
101 *(int *)oidp->oid_arg1 = tt;
105 sysctl_rexmitmax_rangecheck(SYSCTL_HANDLER_ARGS)
109 tt = *(int *)oidp->oid_arg1;
111 error = sysctl_handle_int(oidp, &tt, 0, req);
112 if (error || !req->newptr)
115 if (tt > TCP_MAXRXTSHIFT)
121 *(int *)oidp->oid_arg1 = tt;
126 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
127 &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "");
130 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
131 &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "");
134 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
135 &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "");
138 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime,
139 CTLTYPE_INT|CTLFLAG_RW, &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
140 "Time before a delayed ACK is sent");
143 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
144 &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
146 static int always_keepalive = 0;
147 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
148 &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
151 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
152 &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I", "Minimum retransmission timeout");
154 int tcp_rexmit_shift_max = TCP_MAXRXTSHIFT;
155 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_shift_max, CTLTYPE_INT|CTLFLAG_RW,
156 &tcp_rexmit_shift_max, 0, sysctl_rexmitmax_rangecheck, "I", "Maximum TCP retransmissions");
158 static int tcp_keepcnt = TCPTV_KEEPCNT;
159 /* max idle probes */
160 int tcp_maxpersistidle;
161 /* max idle time in persist */
165 * Tcp protocol timeout routine called every 500 ms.
166 * Updates timestamps used for TCP
167 * causes finite state machine actions if timers expire.
176 tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
182 * Cancel all timers for TCP tp.
188 callout_stop(tp->tt_2msl);
189 callout_stop(tp->tt_persist);
190 callout_stop(tp->tt_keep);
191 callout_stop(tp->tt_rexmt);
194 int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
195 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
197 int tcp_backoff[TCP_MAXRXTSHIFT + 1] =
198 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
200 static int tcp_totbackoff = 511; /* sum of tcp_backoff[] */
203 * TCP timer processing.
206 tcp_timer_delack(xtp)
209 struct tcpcb *tp = xtp;
213 if (callout_pending(tp->tt_delack) || !callout_active(tp->tt_delack)) {
217 callout_deactivate(tp->tt_delack);
219 tp->t_flags |= TF_ACKNOW;
220 tcpstat.tcps_delack++;
221 (void) tcp_output(tp);
229 struct tcpcb *tp = xtp;
234 ostate = tp->t_state;
237 if (callout_pending(tp->tt_2msl) || !callout_active(tp->tt_2msl)) {
241 callout_deactivate(tp->tt_2msl);
243 * 2 MSL timeout in shutdown went off. If we're closed but
244 * still waiting for peer to close and connection has been idle
245 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
246 * control block. Otherwise, check again in a bit.
248 if (tp->t_state != TCPS_TIME_WAIT &&
249 (ticks - tp->t_rcvtime) <= tcp_maxidle) {
250 callout_reset(tp->tt_2msl, tcp_keepintvl,
258 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
259 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
269 struct tcpcb *tp = xtp;
270 struct tcptemp *t_template;
275 ostate = tp->t_state;
278 if (callout_pending(tp->tt_keep) || !callout_active(tp->tt_keep)) {
282 callout_deactivate(tp->tt_keep);
284 * Keep-alive timer went off; send something
285 * or drop connection if idle for too long.
287 tcpstat.tcps_keeptimeo++;
288 if (tp->t_state < TCPS_ESTABLISHED)
290 if ((always_keepalive ||
291 tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) &&
292 tp->t_state <= TCPS_CLOSING) {
293 if ((ticks - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle)
296 * Send a packet designed to force a response
297 * if the peer is up and reachable:
298 * either an ACK if the connection is still alive,
299 * or an RST if the peer has closed the connection
300 * due to timeout or reboot.
301 * Using sequence number tp->snd_una-1
302 * causes the transmitted zero-length segment
303 * to lie outside the receive window;
304 * by the protocol spec, this requires the
305 * correspondent TCP to respond.
307 tcpstat.tcps_keepprobe++;
308 t_template = tcp_maketemplate(tp);
310 tcp_respond(tp, t_template->tt_ipgen,
311 &t_template->tt_t, (struct mbuf *)NULL,
312 tp->rcv_nxt, tp->snd_una - 1, 0);
313 (void) m_free(dtom(t_template));
315 callout_reset(tp->tt_keep, tcp_keepintvl, tcp_timer_keep, tp);
317 callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
320 if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
321 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
328 tcpstat.tcps_keepdrops++;
329 tp = tcp_drop(tp, ETIMEDOUT);
332 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
333 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
340 tcp_timer_persist(xtp)
343 struct tcpcb *tp = xtp;
348 ostate = tp->t_state;
351 if (callout_pending(tp->tt_persist) || !callout_active(tp->tt_persist)){
355 callout_deactivate(tp->tt_persist);
357 * Persistance timer into zero window.
358 * Force a byte to be output, if possible.
360 tcpstat.tcps_persisttimeo++;
362 * Hack: if the peer is dead/unreachable, we do not
363 * time out if the window is closed. After a full
364 * backoff, drop the connection if the idle time
365 * (no responses to probes) reaches the maximum
366 * backoff that we would use if retransmitting.
368 if (tp->t_rxtshift == tcp_rexmit_shift_max &&
369 ((ticks - tp->t_rcvtime) >= tcp_maxpersistidle ||
370 (ticks - tp->t_rcvtime) >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
371 tcpstat.tcps_persistdrop++;
372 tp = tcp_drop(tp, ETIMEDOUT);
377 (void) tcp_output(tp);
382 if (tp && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
383 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
393 struct tcpcb *tp = xtp;
399 ostate = tp->t_state;
402 if (callout_pending(tp->tt_rexmt) || !callout_active(tp->tt_rexmt)) {
406 callout_deactivate(tp->tt_rexmt);
408 * Retransmission timer went off. Message has not
409 * been acked within retransmit interval. Back off
410 * to a longer retransmit interval and retransmit one segment.
412 if (++tp->t_rxtshift > tcp_rexmit_shift_max) {
413 tp->t_rxtshift = tcp_rexmit_shift_max;
414 tcpstat.tcps_timeoutdrop++;
415 tp = tcp_drop(tp, tp->t_softerror ?
416 tp->t_softerror : ETIMEDOUT);
419 if (tp->t_rxtshift == 1) {
421 * first retransmit; record ssthresh and cwnd so they can
422 * be recovered if this turns out to be a "bad" retransmit.
423 * A retransmit is considered "bad" if an ACK for this
424 * segment is received within RTT/2 interval; the assumption
425 * here is that the ACK was already in flight. See
426 * "On Estimating End-to-End Network Path Properties" by
427 * Allman and Paxson for more details.
429 tp->snd_cwnd_prev = tp->snd_cwnd;
430 tp->snd_ssthresh_prev = tp->snd_ssthresh;
431 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
433 tcpstat.tcps_rexmttimeo++;
434 if (tp->t_state == TCPS_SYN_SENT)
435 rexmt = TCP_REXMTVAL(tp) * tcp_syn_backoff[tp->t_rxtshift];
437 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
438 TCPT_RANGESET(tp->t_rxtcur, rexmt,
439 tp->t_rttmin, TCPTV_REXMTMAX);
441 * Disable rfc1323 and rfc1644 if we havn't got any response to
442 * our third SYN to work-around some broken terminal servers
443 * (most of which have hopefully been retired) that have bad VJ
444 * header compression code which trashes TCP segments containing
445 * unknown-to-them TCP options.
447 if ((tp->t_state == TCPS_SYN_SENT) && (tp->t_rxtshift == 3))
448 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_REQ_CC);
450 * If losing, let the lower level know and try for
451 * a better route. Also, if we backed off this far,
452 * our srtt estimate is probably bogus. Clobber it
453 * so we'll take the next rtt measurement as our srtt;
454 * move the current srtt into rttvar to keep the current
455 * retransmit times until then.
457 if (tp->t_rxtshift > tcp_rexmit_shift_max / 4) {
459 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
460 in6_losing(tp->t_inpcb);
463 in_losing(tp->t_inpcb);
464 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
467 tp->snd_nxt = tp->snd_una;
469 * Note: We overload snd_recover to function also as the
470 * snd_last variable described in RFC 2582
472 tp->snd_recover = tp->snd_max;
474 * Force a segment to be sent.
476 tp->t_flags |= TF_ACKNOW;
478 * If timing a segment in this window, stop the timer.
482 * Close the congestion window down to one segment
483 * (we'll open it by one segment for each ack we get).
484 * Since we probably have a window's worth of unacked
485 * data accumulated, this "slow start" keeps us from
486 * dumping all that data as back-to-back packets (which
487 * might overwhelm an intermediate gateway).
489 * There are two phases to the opening: Initially we
490 * open by one mss on each ack. This makes the window
491 * size increase exponentially with time. If the
492 * window is larger than the path can handle, this
493 * exponential growth results in dropped packet(s)
494 * almost immediately. To get more time between
495 * drops but still "push" the network to take advantage
496 * of improving conditions, we switch from exponential
497 * to linear window opening at some threshhold size.
498 * For a threshhold, we use half the current window
499 * size, truncated to a multiple of the mss.
501 * (the minimum cwnd that will give us exponential
502 * growth is 2 mss. We don't allow the threshhold
506 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
509 tp->snd_cwnd = tp->t_maxseg;
510 tp->snd_ssthresh = win * tp->t_maxseg;
513 (void) tcp_output(tp);
517 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
518 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,