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