xref: /freebsd/sys/netinet/tcp_timer.c (revision 09e8dea79366f1e5b3a73e8a271b26e4b6bf2e6a)
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_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 
71 static int
72 sysctl_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 
91 int	tcp_keepinit;
92 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
93     &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "");
94 
95 int	tcp_keepidle;
96 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
97     &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "");
98 
99 int	tcp_keepintvl;
100 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
101     &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "");
102 
103 int	tcp_delacktime;
104 SYSCTL_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 
108 int	tcp_msl;
109 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
110     &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
111 
112 static int	always_keepalive = 1;
113 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
114     &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
115 
116 static int	tcp_keepcnt = TCPTV_KEEPCNT;
117 	/* max idle probes */
118 int	tcp_maxpersistidle;
119 	/* max idle time in persist */
120 int	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  */
127 void
128 tcp_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  */
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_syn_backoff[TCP_MAXRXTSHIFT + 1] =
153     { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
154 
155 int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
156     { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
157 
158 static int tcp_totbackoff = 511;	/* sum of tcp_backoff[] */
159 
160 /*
161  * TCP timer processing.
162  */
163 
164 void
165 tcp_timer_delack(xtp)
166 	void *xtp;
167 {
168 	struct tcpcb *tp = xtp;
169 	int s;
170 	struct inpcb *inp;
171 
172 	s = splnet();
173 	INP_INFO_RLOCK(&tcbinfo);
174 	inp = tp->t_inpcb;
175 	INP_LOCK(inp);
176 	INP_INFO_RUNLOCK(&tcbinfo);
177 	if (callout_pending(tp->tt_delack) || !callout_active(tp->tt_delack)) {
178 		INP_UNLOCK(inp);
179 		splx(s);
180 		return;
181 	}
182 	callout_deactivate(tp->tt_delack);
183 
184 	tp->t_flags |= TF_ACKNOW;
185 	tcpstat.tcps_delack++;
186 	(void) tcp_output(tp);
187 	INP_UNLOCK(inp);
188 	splx(s);
189 }
190 
191 void
192 tcp_timer_2msl(xtp)
193 	void *xtp;
194 {
195 	struct tcpcb *tp = xtp;
196 	int s;
197 	struct inpcb *inp;
198 #ifdef TCPDEBUG
199 	int ostate;
200 
201 	ostate = tp->t_state;
202 #endif
203 	s = splnet();
204 	INP_INFO_WLOCK(&tcbinfo);
205 	inp = tp->t_inpcb;
206 	INP_LOCK(inp);
207 	if (callout_pending(tp->tt_2msl) || !callout_active(tp->tt_2msl)) {
208 		INP_UNLOCK(tp->t_inpcb);
209 		INP_INFO_WUNLOCK(&tcbinfo);
210 		splx(s);
211 		return;
212 	}
213 	callout_deactivate(tp->tt_2msl);
214 	/*
215 	 * 2 MSL timeout in shutdown went off.  If we're closed but
216 	 * still waiting for peer to close and connection has been idle
217 	 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
218 	 * control block.  Otherwise, check again in a bit.
219 	 */
220 	if (tp->t_state != TCPS_TIME_WAIT &&
221 	    (ticks - tp->t_rcvtime) <= tcp_maxidle)
222 		callout_reset(tp->tt_2msl, tcp_keepintvl,
223 			      tcp_timer_2msl, tp);
224 	else
225 		tp = tcp_close(tp);
226 
227 #ifdef TCPDEBUG
228 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
229 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
230 			  PRU_SLOWTIMO);
231 #endif
232 	if (tp)
233 		INP_UNLOCK(inp);
234 	INP_INFO_WUNLOCK(&tcbinfo);
235 	splx(s);
236 }
237 
238 void
239 tcp_timer_keep(xtp)
240 	void *xtp;
241 {
242 	struct tcpcb *tp = xtp;
243 	struct tcptemp *t_template;
244 	int s;
245 	struct inpcb *inp;
246 #ifdef TCPDEBUG
247 	int ostate;
248 
249 	ostate = tp->t_state;
250 #endif
251 	s = splnet();
252 	INP_INFO_WLOCK(&tcbinfo);
253 	inp = tp->t_inpcb;
254 	INP_LOCK(inp);
255 	if (callout_pending(tp->tt_keep) || !callout_active(tp->tt_keep)) {
256 		INP_UNLOCK(inp);
257 		INP_INFO_WUNLOCK(&tcbinfo);
258 		splx(s);
259 		return;
260 	}
261 	callout_deactivate(tp->tt_keep);
262 	/*
263 	 * Keep-alive timer went off; send something
264 	 * or drop connection if idle for too long.
265 	 */
266 	tcpstat.tcps_keeptimeo++;
267 	if (tp->t_state < TCPS_ESTABLISHED)
268 		goto dropit;
269 	if ((always_keepalive ||
270 	     tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) &&
271 	    tp->t_state <= TCPS_CLOSING) {
272 		if ((ticks - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle)
273 			goto dropit;
274 		/*
275 		 * Send a packet designed to force a response
276 		 * if the peer is up and reachable:
277 		 * either an ACK if the connection is still alive,
278 		 * or an RST if the peer has closed the connection
279 		 * due to timeout or reboot.
280 		 * Using sequence number tp->snd_una-1
281 		 * causes the transmitted zero-length segment
282 		 * to lie outside the receive window;
283 		 * by the protocol spec, this requires the
284 		 * correspondent TCP to respond.
285 		 */
286 		tcpstat.tcps_keepprobe++;
287 		t_template = tcp_maketemplate(tp);
288 		if (t_template) {
289 			tcp_respond(tp, t_template->tt_ipgen,
290 				    &t_template->tt_t, (struct mbuf *)NULL,
291 				    tp->rcv_nxt, tp->snd_una - 1, 0);
292 			(void) m_free(dtom(t_template));
293 		}
294 		callout_reset(tp->tt_keep, tcp_keepintvl, tcp_timer_keep, tp);
295 	} else
296 		callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
297 
298 #ifdef TCPDEBUG
299 	if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
300 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
301 			  PRU_SLOWTIMO);
302 #endif
303 	INP_UNLOCK(inp);
304 	INP_INFO_WUNLOCK(&tcbinfo);
305 	splx(s);
306 	return;
307 
308 dropit:
309 	tcpstat.tcps_keepdrops++;
310 	tp = tcp_drop(tp, ETIMEDOUT);
311 
312 #ifdef TCPDEBUG
313 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
314 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
315 			  PRU_SLOWTIMO);
316 #endif
317 	if (tp)
318 		INP_UNLOCK(tp->t_inpcb);
319 	INP_INFO_WUNLOCK(&tcbinfo);
320 	splx(s);
321 }
322 
323 void
324 tcp_timer_persist(xtp)
325 	void *xtp;
326 {
327 	struct tcpcb *tp = xtp;
328 	int s;
329 	struct inpcb *inp;
330 #ifdef TCPDEBUG
331 	int ostate;
332 
333 	ostate = tp->t_state;
334 #endif
335 	s = splnet();
336 	INP_INFO_WLOCK(&tcbinfo);
337 	inp = tp->t_inpcb;
338 	INP_LOCK(inp);
339 	if (callout_pending(tp->tt_persist) || !callout_active(tp->tt_persist)){
340 		INP_UNLOCK(inp);
341 		INP_INFO_WUNLOCK(&tcbinfo);
342 		splx(s);
343 		return;
344 	}
345 	callout_deactivate(tp->tt_persist);
346 	/*
347 	 * Persistance timer into zero window.
348 	 * Force a byte to be output, if possible.
349 	 */
350 	tcpstat.tcps_persisttimeo++;
351 	/*
352 	 * Hack: if the peer is dead/unreachable, we do not
353 	 * time out if the window is closed.  After a full
354 	 * backoff, drop the connection if the idle time
355 	 * (no responses to probes) reaches the maximum
356 	 * backoff that we would use if retransmitting.
357 	 */
358 	if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
359 	    ((ticks - tp->t_rcvtime) >= tcp_maxpersistidle ||
360 	     (ticks - tp->t_rcvtime) >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
361 		tcpstat.tcps_persistdrop++;
362 		tp = tcp_drop(tp, ETIMEDOUT);
363 		goto out;
364 	}
365 	tcp_setpersist(tp);
366 	tp->t_force = 1;
367 	(void) tcp_output(tp);
368 	tp->t_force = 0;
369 
370 out:
371 #ifdef TCPDEBUG
372 	if (tp && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
373 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
374 			  PRU_SLOWTIMO);
375 #endif
376 	if (tp)
377 		INP_UNLOCK(inp);
378 	INP_INFO_WUNLOCK(&tcbinfo);
379 	splx(s);
380 }
381 
382 void
383 tcp_timer_rexmt(xtp)
384 	void *xtp;
385 {
386 	struct tcpcb *tp = xtp;
387 	int s;
388 	int rexmt;
389 	int headlocked;
390 	struct inpcb *inp;
391 #ifdef TCPDEBUG
392 	int ostate;
393 
394 	ostate = tp->t_state;
395 #endif
396 	s = splnet();
397 	INP_INFO_WLOCK(&tcbinfo);
398 	headlocked = 1;
399 	inp = tp->t_inpcb;
400 	INP_LOCK(inp);
401 	if (callout_pending(tp->tt_rexmt) || !callout_active(tp->tt_rexmt)) {
402 		INP_UNLOCK(inp);
403 		INP_INFO_WUNLOCK(&tcbinfo);
404 		splx(s);
405 		return;
406 	}
407 	callout_deactivate(tp->tt_rexmt);
408 	/*
409 	 * Retransmission timer went off.  Message has not
410 	 * been acked within retransmit interval.  Back off
411 	 * to a longer retransmit interval and retransmit one segment.
412 	 */
413 	if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
414 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
415 		tcpstat.tcps_timeoutdrop++;
416 		tp = tcp_drop(tp, tp->t_softerror ?
417 			      tp->t_softerror : ETIMEDOUT);
418 		goto out;
419 	}
420 	INP_INFO_WUNLOCK(&tcbinfo);
421 	headlocked = 0;
422 	if (tp->t_rxtshift == 1) {
423 		/*
424 		 * first retransmit; record ssthresh and cwnd so they can
425 	 	 * be recovered if this turns out to be a "bad" retransmit.
426 		 * A retransmit is considered "bad" if an ACK for this
427 		 * segment is received within RTT/2 interval; the assumption
428 		 * here is that the ACK was already in flight.  See
429 		 * "On Estimating End-to-End Network Path Properties" by
430 		 * Allman and Paxson for more details.
431 		 */
432 		tp->snd_cwnd_prev = tp->snd_cwnd;
433 		tp->snd_ssthresh_prev = tp->snd_ssthresh;
434 		tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
435 	}
436 	tcpstat.tcps_rexmttimeo++;
437 	if (tp->t_state == TCPS_SYN_SENT)
438 		rexmt = TCP_REXMTVAL(tp) * tcp_syn_backoff[tp->t_rxtshift];
439 	else
440 		rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
441 	TCPT_RANGESET(tp->t_rxtcur, rexmt,
442 		      tp->t_rttmin, TCPTV_REXMTMAX);
443 	/*
444 	 * Disable rfc1323 and rfc1644 if we havn't got any response to
445 	 * our third SYN to work-around some broken terminal servers
446 	 * (most of which have hopefully been retired) that have bad VJ
447 	 * header compression code which trashes TCP segments containing
448 	 * unknown-to-them TCP options.
449 	 */
450 	if ((tp->t_state == TCPS_SYN_SENT) && (tp->t_rxtshift == 3))
451 		tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_REQ_CC);
452 	/*
453 	 * If losing, let the lower level know and try for
454 	 * a better route.  Also, if we backed off this far,
455 	 * our srtt estimate is probably bogus.  Clobber it
456 	 * so we'll take the next rtt measurement as our srtt;
457 	 * move the current srtt into rttvar to keep the current
458 	 * retransmit times until then.
459 	 */
460 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
461 #ifdef INET6
462 		if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
463 			in6_losing(tp->t_inpcb);
464 		else
465 #endif
466 		in_losing(tp->t_inpcb);
467 		tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
468 		tp->t_srtt = 0;
469 	}
470 	tp->snd_nxt = tp->snd_una;
471 	/*
472 	 * Note:  We overload snd_recover to function also as the
473 	 * snd_last variable described in RFC 2582
474 	 */
475 	tp->snd_recover = tp->snd_max;
476 	/*
477 	 * Force a segment to be sent.
478 	 */
479 	tp->t_flags |= TF_ACKNOW;
480 	/*
481 	 * If timing a segment in this window, stop the timer.
482 	 */
483 	tp->t_rtttime = 0;
484 	/*
485 	 * Close the congestion window down to one segment
486 	 * (we'll open it by one segment for each ack we get).
487 	 * Since we probably have a window's worth of unacked
488 	 * data accumulated, this "slow start" keeps us from
489 	 * dumping all that data as back-to-back packets (which
490 	 * might overwhelm an intermediate gateway).
491 	 *
492 	 * There are two phases to the opening: Initially we
493 	 * open by one mss on each ack.  This makes the window
494 	 * size increase exponentially with time.  If the
495 	 * window is larger than the path can handle, this
496 	 * exponential growth results in dropped packet(s)
497 	 * almost immediately.  To get more time between
498 	 * drops but still "push" the network to take advantage
499 	 * of improving conditions, we switch from exponential
500 	 * to linear window opening at some threshhold size.
501 	 * For a threshhold, we use half the current window
502 	 * size, truncated to a multiple of the mss.
503 	 *
504 	 * (the minimum cwnd that will give us exponential
505 	 * growth is 2 mss.  We don't allow the threshhold
506 	 * to go below this.)
507 	 */
508 	{
509 		u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
510 		if (win < 2)
511 			win = 2;
512 		tp->snd_cwnd = tp->t_maxseg;
513 		tp->snd_ssthresh = win * tp->t_maxseg;
514 		tp->t_dupacks = 0;
515 	}
516 	(void) tcp_output(tp);
517 
518 out:
519 #ifdef TCPDEBUG
520 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
521 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
522 			  PRU_SLOWTIMO);
523 #endif
524 	if (tp)
525 		INP_UNLOCK(inp);
526 	if (headlocked)
527 		INP_INFO_WUNLOCK(&tcbinfo);
528 	splx(s);
529 }
530