xref: /freebsd/sys/netinet/tcp_timer.c (revision f35e5d0ef0a10ebda81a076bbd838d12b916dab5)
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$
35  */
36 
37 #include "opt_compat.h"
38 #include "opt_tcpdebug.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/sysctl.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/protosw.h>
47 
48 #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
49 
50 #include <net/route.h>
51 
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/in_pcb.h>
55 #include <netinet/ip_var.h>
56 #include <netinet/tcp.h>
57 #include <netinet/tcp_fsm.h>
58 #include <netinet/tcp_seq.h>
59 #include <netinet/tcp_timer.h>
60 #include <netinet/tcp_var.h>
61 #include <netinet/tcpip.h>
62 #ifdef TCPDEBUG
63 #include <netinet/tcp_debug.h>
64 #endif
65 
66 static int
67 sysctl_msec_to_ticks SYSCTL_HANDLER_ARGS
68 {
69 	int error, s, tt;
70 
71 	tt = *(int *)oidp->oid_arg1;
72 	s = tt * 1000 / hz;
73 
74 	error = sysctl_handle_int(oidp, &s, 0, req);
75 	if (error || !req->newptr)
76 		return (error);
77 
78 	tt = s * hz / 1000;
79 	if (tt < 1)
80 		return (EINVAL);
81 
82 	*(int *)oidp->oid_arg1 = tt;
83         return (0);
84 }
85 
86 int	tcp_keepinit;
87 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
88     &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "");
89 
90 int	tcp_keepidle;
91 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
92     &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "");
93 
94 int	tcp_keepintvl;
95 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
96     &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "");
97 
98 int	tcp_delacktime;
99 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime,
100     CTLTYPE_INT|CTLFLAG_RW, &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
101     "Time before a delayed ACK is sent");
102 
103 int	tcp_msl;
104 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
105     &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
106 
107 static int	always_keepalive = 0;
108 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
109     &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
110 
111 static int	tcp_keepcnt = TCPTV_KEEPCNT;
112 	/* max idle probes */
113 int	tcp_maxpersistidle;
114 	/* max idle time in persist */
115 int	tcp_maxidle;
116 
117 /*
118  * Tcp protocol timeout routine called every 500 ms.
119  * Updates timestamps used for TCP
120  * causes finite state machine actions if timers expire.
121  */
122 void
123 tcp_slowtimo()
124 {
125 	int s;
126 
127 	s = splnet();
128 
129 	tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
130 
131 	tcp_iss += TCP_ISSINCR/PR_SLOWHZ;		/* increment iss */
132 #ifdef TCP_COMPAT_42
133 	if ((int)tcp_iss < 0)
134 		tcp_iss = TCP_ISSINCR;			/* XXX */
135 #endif
136 	splx(s);
137 }
138 
139 /*
140  * Cancel all timers for TCP tp.
141  */
142 void
143 tcp_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 
152 int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
153     { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
154 
155 static int tcp_totbackoff = 511;	/* sum of tcp_backoff[] */
156 
157 /*
158  * TCP timer processing.
159  */
160 void
161 tcp_timer_delack(xtp)
162 	void *xtp;
163 {
164 	struct tcpcb *tp = xtp;
165 	int s;
166 
167 	s = splnet();
168 	if (callout_pending(tp->tt_delack)) {
169 		splx(s);
170 		return;
171 	}
172 	callout_deactivate(tp->tt_delack);
173 
174 	tp->t_flags |= TF_ACKNOW;
175 	tcpstat.tcps_delack++;
176 	(void) tcp_output(tp);
177 	splx(s);
178 }
179 
180 void
181 tcp_timer_2msl(xtp)
182 	void *xtp;
183 {
184 	struct tcpcb *tp = xtp;
185 	int s;
186 #ifdef TCPDEBUG
187 	int ostate;
188 
189 	ostate = tp->t_state;
190 #endif
191 	s = splnet();
192 	if (callout_pending(tp->tt_2msl)) {
193 		splx(s);
194 		return;
195 	}
196 	callout_deactivate(tp->tt_2msl);
197 	/*
198 	 * 2 MSL timeout in shutdown went off.  If we're closed but
199 	 * still waiting for peer to close and connection has been idle
200 	 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
201 	 * control block.  Otherwise, check again in a bit.
202 	 */
203 	if (tp->t_state != TCPS_TIME_WAIT &&
204 	    (ticks - tp->t_rcvtime) <= tcp_maxidle)
205 		callout_reset(tp->tt_2msl, tcp_keepintvl,
206 			      tcp_timer_2msl, tp);
207 	else
208 		tp = tcp_close(tp);
209 
210 #ifdef TCPDEBUG
211 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
212 		tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0,
213 			  PRU_SLOWTIMO);
214 #endif
215 	splx(s);
216 }
217 
218 void
219 tcp_timer_keep(xtp)
220 	void *xtp;
221 {
222 	struct tcpcb *tp = xtp;
223 	int s;
224 #ifdef TCPDEBUG
225 	int ostate;
226 
227 	ostate = tp->t_state;
228 #endif
229 	s = splnet();
230 	if (callout_pending(tp->tt_keep)) {
231 		splx(s);
232 		return;
233 	}
234 	callout_deactivate(tp->tt_keep);
235 	/*
236 	 * Keep-alive timer went off; send something
237 	 * or drop connection if idle for too long.
238 	 */
239 	tcpstat.tcps_keeptimeo++;
240 	if (tp->t_state < TCPS_ESTABLISHED)
241 		goto dropit;
242 	if ((always_keepalive ||
243 	     tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) &&
244 	    tp->t_state <= TCPS_CLOSING) {
245 		if ((ticks - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle)
246 			goto dropit;
247 		/*
248 		 * Send a packet designed to force a response
249 		 * if the peer is up and reachable:
250 		 * either an ACK if the connection is still alive,
251 		 * or an RST if the peer has closed the connection
252 		 * due to timeout or reboot.
253 		 * Using sequence number tp->snd_una-1
254 		 * causes the transmitted zero-length segment
255 		 * to lie outside the receive window;
256 		 * by the protocol spec, this requires the
257 		 * correspondent TCP to respond.
258 		 */
259 		tcpstat.tcps_keepprobe++;
260 #ifdef TCP_COMPAT_42
261 		/*
262 		 * The keepalive packet must have nonzero length
263 		 * to get a 4.2 host to respond.
264 		 */
265 		tcp_respond(tp, tp->t_template, (struct mbuf *)NULL,
266 			    tp->rcv_nxt - 1, tp->snd_una - 1, 0);
267 #else
268 		tcp_respond(tp, tp->t_template, (struct mbuf *)NULL,
269 			    tp->rcv_nxt, tp->snd_una - 1, 0);
270 #endif
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, (struct tcpiphdr *)0,
278 			  PRU_SLOWTIMO);
279 #endif
280 	splx(s);
281 	return;
282 
283 dropit:
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, (struct tcpiphdr *)0,
290 			  PRU_SLOWTIMO);
291 #endif
292 	splx(s);
293 }
294 
295 void
296 tcp_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)) {
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 
336 out:
337 #ifdef TCPDEBUG
338 	if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
339 		tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0,
340 			  PRU_SLOWTIMO);
341 #endif
342 	splx(s);
343 }
344 
345 void
346 tcp_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)) {
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 	rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
391 	TCPT_RANGESET(tp->t_rxtcur, rexmt,
392 		      tp->t_rttmin, TCPTV_REXMTMAX);
393 	/*
394 	 * If losing, let the lower level know and try for
395 	 * a better route.  Also, if we backed off this far,
396 	 * our srtt estimate is probably bogus.  Clobber it
397 	 * so we'll take the next rtt measurement as our srtt;
398 	 * move the current srtt into rttvar to keep the current
399 	 * retransmit times until then.
400 	 */
401 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
402 		in_losing(tp->t_inpcb);
403 		tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
404 		tp->t_srtt = 0;
405 	}
406 	tp->snd_nxt = tp->snd_una;
407 	/*
408 	 * Force a segment to be sent.
409 	 */
410 	tp->t_flags |= TF_ACKNOW;
411 	/*
412 	 * If timing a segment in this window, stop the timer.
413 	 */
414 	tp->t_rtttime = 0;
415 	/*
416 	 * Close the congestion window down to one segment
417 	 * (we'll open it by one segment for each ack we get).
418 	 * Since we probably have a window's worth of unacked
419 	 * data accumulated, this "slow start" keeps us from
420 	 * dumping all that data as back-to-back packets (which
421 	 * might overwhelm an intermediate gateway).
422 	 *
423 	 * There are two phases to the opening: Initially we
424 	 * open by one mss on each ack.  This makes the window
425 	 * size increase exponentially with time.  If the
426 	 * window is larger than the path can handle, this
427 	 * exponential growth results in dropped packet(s)
428 	 * almost immediately.  To get more time between
429 	 * drops but still "push" the network to take advantage
430 	 * of improving conditions, we switch from exponential
431 	 * to linear window opening at some threshhold size.
432 	 * For a threshhold, we use half the current window
433 	 * size, truncated to a multiple of the mss.
434 	 *
435 	 * (the minimum cwnd that will give us exponential
436 	 * growth is 2 mss.  We don't allow the threshhold
437 	 * to go below this.)
438 	 */
439 	{
440 		u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
441 		if (win < 2)
442 			win = 2;
443 		tp->snd_cwnd = tp->t_maxseg;
444 		tp->snd_ssthresh = win * tp->t_maxseg;
445 		tp->t_dupacks = 0;
446 	}
447 	(void) tcp_output(tp);
448 
449 out:
450 #ifdef TCPDEBUG
451 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
452 		tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0,
453 			  PRU_SLOWTIMO);
454 #endif
455 	splx(s);
456 }
457