tcp_timer.c revision 87499
1/*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 *
33 *	@(#)tcp_timer.c	8.2 (Berkeley) 5/24/95
34 * $FreeBSD: head/sys/netinet/tcp_timer.c 87499 2001-12-07 17:01:28Z rwatson $
35 */
36
37#include "opt_compat.h"
38#include "opt_inet6.h"
39#include "opt_tcpdebug.h"
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
44#include <sys/mbuf.h>
45#include <sys/sysctl.h>
46#include <sys/socket.h>
47#include <sys/socketvar.h>
48#include <sys/protosw.h>
49
50#include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
51
52#include <net/route.h>
53
54#include <netinet/in.h>
55#include <netinet/in_systm.h>
56#include <netinet/in_pcb.h>
57#ifdef INET6
58#include <netinet6/in6_pcb.h>
59#endif
60#include <netinet/ip_var.h>
61#include <netinet/tcp.h>
62#include <netinet/tcp_fsm.h>
63#include <netinet/tcp_seq.h>
64#include <netinet/tcp_timer.h>
65#include <netinet/tcp_var.h>
66#include <netinet/tcpip.h>
67#ifdef TCPDEBUG
68#include <netinet/tcp_debug.h>
69#endif
70
71static int
72sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
73{
74	int error, s, tt;
75
76	tt = *(int *)oidp->oid_arg1;
77	s = tt * 1000 / hz;
78
79	error = sysctl_handle_int(oidp, &s, 0, req);
80	if (error || !req->newptr)
81		return (error);
82
83	tt = s * hz / 1000;
84	if (tt < 1)
85		return (EINVAL);
86
87	*(int *)oidp->oid_arg1 = tt;
88        return (0);
89}
90
91int	tcp_keepinit;
92SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
93    &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "");
94
95int	tcp_keepidle;
96SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
97    &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "");
98
99int	tcp_keepintvl;
100SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
101    &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "");
102
103int	tcp_delacktime;
104SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime,
105    CTLTYPE_INT|CTLFLAG_RW, &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
106    "Time before a delayed ACK is sent");
107
108int	tcp_msl;
109SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
110    &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
111
112static int	always_keepalive = 1;
113SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
114    &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
115
116static int	tcp_keepcnt = TCPTV_KEEPCNT;
117	/* max idle probes */
118int	tcp_maxpersistidle;
119	/* max idle time in persist */
120int	tcp_maxidle;
121
122/*
123 * Tcp protocol timeout routine called every 500 ms.
124 * Updates timestamps used for TCP
125 * causes finite state machine actions if timers expire.
126 */
127void
128tcp_slowtimo()
129{
130	int s;
131
132	s = splnet();
133
134	tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
135
136	splx(s);
137}
138
139/*
140 * Cancel all timers for TCP tp.
141 */
142void
143tcp_canceltimers(tp)
144	struct tcpcb *tp;
145{
146	callout_stop(tp->tt_2msl);
147	callout_stop(tp->tt_persist);
148	callout_stop(tp->tt_keep);
149	callout_stop(tp->tt_rexmt);
150}
151
152int	tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
153    { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
154
155int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
156    { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
157
158static int tcp_totbackoff = 511;	/* sum of tcp_backoff[] */
159
160/*
161 * TCP timer processing.
162 */
163void
164tcp_timer_delack(xtp)
165	void *xtp;
166{
167	struct tcpcb *tp = xtp;
168	int s;
169
170	s = splnet();
171	if (callout_pending(tp->tt_delack) || !callout_active(tp->tt_delack)) {
172		splx(s);
173		return;
174	}
175	callout_deactivate(tp->tt_delack);
176
177	tp->t_flags |= TF_ACKNOW;
178	tcpstat.tcps_delack++;
179	(void) tcp_output(tp);
180	splx(s);
181}
182
183void
184tcp_timer_2msl(xtp)
185	void *xtp;
186{
187	struct tcpcb *tp = xtp;
188	int s;
189#ifdef TCPDEBUG
190	int ostate;
191
192	ostate = tp->t_state;
193#endif
194	s = splnet();
195	if (callout_pending(tp->tt_2msl) || !callout_active(tp->tt_2msl)) {
196		splx(s);
197		return;
198	}
199	callout_deactivate(tp->tt_2msl);
200	/*
201	 * 2 MSL timeout in shutdown went off.  If we're closed but
202	 * still waiting for peer to close and connection has been idle
203	 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
204	 * control block.  Otherwise, check again in a bit.
205	 */
206	if (tp->t_state != TCPS_TIME_WAIT &&
207	    (ticks - tp->t_rcvtime) <= tcp_maxidle)
208		callout_reset(tp->tt_2msl, tcp_keepintvl,
209			      tcp_timer_2msl, tp);
210	else
211		tp = tcp_close(tp);
212
213#ifdef TCPDEBUG
214	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
215		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
216			  PRU_SLOWTIMO);
217#endif
218	splx(s);
219}
220
221void
222tcp_timer_keep(xtp)
223	void *xtp;
224{
225	struct tcpcb *tp = xtp;
226	struct tcptemp *t_template;
227	int s;
228#ifdef TCPDEBUG
229	int ostate;
230
231	ostate = tp->t_state;
232#endif
233	s = splnet();
234	if (callout_pending(tp->tt_keep) || !callout_active(tp->tt_keep)) {
235		splx(s);
236		return;
237	}
238	callout_deactivate(tp->tt_keep);
239	/*
240	 * Keep-alive timer went off; send something
241	 * or drop connection if idle for too long.
242	 */
243	tcpstat.tcps_keeptimeo++;
244	if (tp->t_state < TCPS_ESTABLISHED)
245		goto dropit;
246	if ((always_keepalive ||
247	     tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) &&
248	    tp->t_state <= TCPS_CLOSING) {
249		if ((ticks - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle)
250			goto dropit;
251		/*
252		 * Send a packet designed to force a response
253		 * if the peer is up and reachable:
254		 * either an ACK if the connection is still alive,
255		 * or an RST if the peer has closed the connection
256		 * due to timeout or reboot.
257		 * Using sequence number tp->snd_una-1
258		 * causes the transmitted zero-length segment
259		 * to lie outside the receive window;
260		 * by the protocol spec, this requires the
261		 * correspondent TCP to respond.
262		 */
263		tcpstat.tcps_keepprobe++;
264		t_template = tcp_maketemplate(tp);
265		if (t_template) {
266			tcp_respond(tp, t_template->tt_ipgen,
267				    &t_template->tt_t, (struct mbuf *)NULL,
268				    tp->rcv_nxt, tp->snd_una - 1, 0);
269			(void) m_free(dtom(t_template));
270		}
271		callout_reset(tp->tt_keep, tcp_keepintvl, tcp_timer_keep, tp);
272	} else
273		callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
274
275#ifdef TCPDEBUG
276	if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
277		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
278			  PRU_SLOWTIMO);
279#endif
280	splx(s);
281	return;
282
283dropit:
284	tcpstat.tcps_keepdrops++;
285	tp = tcp_drop(tp, ETIMEDOUT);
286
287#ifdef TCPDEBUG
288	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
289		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
290			  PRU_SLOWTIMO);
291#endif
292	splx(s);
293}
294
295void
296tcp_timer_persist(xtp)
297	void *xtp;
298{
299	struct tcpcb *tp = xtp;
300	int s;
301#ifdef TCPDEBUG
302	int ostate;
303
304	ostate = tp->t_state;
305#endif
306	s = splnet();
307	if (callout_pending(tp->tt_persist) || !callout_active(tp->tt_persist)){
308		splx(s);
309		return;
310	}
311	callout_deactivate(tp->tt_persist);
312	/*
313	 * Persistance timer into zero window.
314	 * Force a byte to be output, if possible.
315	 */
316	tcpstat.tcps_persisttimeo++;
317	/*
318	 * Hack: if the peer is dead/unreachable, we do not
319	 * time out if the window is closed.  After a full
320	 * backoff, drop the connection if the idle time
321	 * (no responses to probes) reaches the maximum
322	 * backoff that we would use if retransmitting.
323	 */
324	if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
325	    ((ticks - tp->t_rcvtime) >= tcp_maxpersistidle ||
326	     (ticks - tp->t_rcvtime) >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
327		tcpstat.tcps_persistdrop++;
328		tp = tcp_drop(tp, ETIMEDOUT);
329		goto out;
330	}
331	tcp_setpersist(tp);
332	tp->t_force = 1;
333	(void) tcp_output(tp);
334	tp->t_force = 0;
335
336out:
337#ifdef TCPDEBUG
338	if (tp && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
339		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
340			  PRU_SLOWTIMO);
341#endif
342	splx(s);
343}
344
345void
346tcp_timer_rexmt(xtp)
347	void *xtp;
348{
349	struct tcpcb *tp = xtp;
350	int s;
351	int rexmt;
352#ifdef TCPDEBUG
353	int ostate;
354
355	ostate = tp->t_state;
356#endif
357	s = splnet();
358	if (callout_pending(tp->tt_rexmt) || !callout_active(tp->tt_rexmt)) {
359		splx(s);
360		return;
361	}
362	callout_deactivate(tp->tt_rexmt);
363	/*
364	 * Retransmission timer went off.  Message has not
365	 * been acked within retransmit interval.  Back off
366	 * to a longer retransmit interval and retransmit one segment.
367	 */
368	if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
369		tp->t_rxtshift = TCP_MAXRXTSHIFT;
370		tcpstat.tcps_timeoutdrop++;
371		tp = tcp_drop(tp, tp->t_softerror ?
372			      tp->t_softerror : ETIMEDOUT);
373		goto out;
374	}
375	if (tp->t_rxtshift == 1) {
376		/*
377		 * first retransmit; record ssthresh and cwnd so they can
378	 	 * be recovered if this turns out to be a "bad" retransmit.
379		 * A retransmit is considered "bad" if an ACK for this
380		 * segment is received within RTT/2 interval; the assumption
381		 * here is that the ACK was already in flight.  See
382		 * "On Estimating End-to-End Network Path Properties" by
383		 * Allman and Paxson for more details.
384		 */
385		tp->snd_cwnd_prev = tp->snd_cwnd;
386		tp->snd_ssthresh_prev = tp->snd_ssthresh;
387		tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
388	}
389	tcpstat.tcps_rexmttimeo++;
390	if (tp->t_state == TCPS_SYN_SENT)
391		rexmt = TCP_REXMTVAL(tp) * tcp_syn_backoff[tp->t_rxtshift];
392	else
393		rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
394	TCPT_RANGESET(tp->t_rxtcur, rexmt,
395		      tp->t_rttmin, TCPTV_REXMTMAX);
396	/*
397	 * Disable rfc1323 and rfc1644 if we havn't got any response to
398	 * our third SYN to work-around some broken terminal servers
399	 * (most of which have hopefully been retired) that have bad VJ
400	 * header compression code which trashes TCP segments containing
401	 * unknown-to-them TCP options.
402	 */
403	if ((tp->t_state == TCPS_SYN_SENT) && (tp->t_rxtshift == 3))
404		tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_REQ_CC);
405	/*
406	 * If losing, let the lower level know and try for
407	 * a better route.  Also, if we backed off this far,
408	 * our srtt estimate is probably bogus.  Clobber it
409	 * so we'll take the next rtt measurement as our srtt;
410	 * move the current srtt into rttvar to keep the current
411	 * retransmit times until then.
412	 */
413	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
414#ifdef INET6
415		if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
416			in6_losing(tp->t_inpcb);
417		else
418#endif
419		in_losing(tp->t_inpcb);
420		tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
421		tp->t_srtt = 0;
422	}
423	tp->snd_nxt = tp->snd_una;
424	/*
425	 * Note:  We overload snd_recover to function also as the
426	 * snd_last variable described in RFC 2582
427	 */
428	tp->snd_recover = tp->snd_max;
429	/*
430	 * Force a segment to be sent.
431	 */
432	tp->t_flags |= TF_ACKNOW;
433	/*
434	 * If timing a segment in this window, stop the timer.
435	 */
436	tp->t_rtttime = 0;
437	/*
438	 * Close the congestion window down to one segment
439	 * (we'll open it by one segment for each ack we get).
440	 * Since we probably have a window's worth of unacked
441	 * data accumulated, this "slow start" keeps us from
442	 * dumping all that data as back-to-back packets (which
443	 * might overwhelm an intermediate gateway).
444	 *
445	 * There are two phases to the opening: Initially we
446	 * open by one mss on each ack.  This makes the window
447	 * size increase exponentially with time.  If the
448	 * window is larger than the path can handle, this
449	 * exponential growth results in dropped packet(s)
450	 * almost immediately.  To get more time between
451	 * drops but still "push" the network to take advantage
452	 * of improving conditions, we switch from exponential
453	 * to linear window opening at some threshhold size.
454	 * For a threshhold, we use half the current window
455	 * size, truncated to a multiple of the mss.
456	 *
457	 * (the minimum cwnd that will give us exponential
458	 * growth is 2 mss.  We don't allow the threshhold
459	 * to go below this.)
460	 */
461	{
462		u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
463		if (win < 2)
464			win = 2;
465		tp->snd_cwnd = tp->t_maxseg;
466		tp->snd_ssthresh = win * tp->t_maxseg;
467		tp->t_dupacks = 0;
468	}
469	(void) tcp_output(tp);
470
471out:
472#ifdef TCPDEBUG
473	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
474		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
475			  PRU_SLOWTIMO);
476#endif
477	splx(s);
478}
479