xref: /dflybsd-src/sys/netinet/tcp_timer.c (revision bf22d4c1f95f57623b2b3030738e116d3a547284)
1 /*
2  * Copyright (c) 2003-2004 Jeffrey Hsu.  All rights reserved.
3  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the University of
17  *	California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)tcp_timer.c	8.2 (Berkeley) 5/24/95
35  * $FreeBSD: src/sys/netinet/tcp_timer.c,v 1.34.2.14 2003/02/03 02:33:41 hsu Exp $
36  * $DragonFly: src/sys/netinet/tcp_timer.c,v 1.7 2004/04/07 17:01:25 dillon Exp $
37  */
38 
39 #include "opt_compat.h"
40 #include "opt_inet6.h"
41 #include "opt_tcpdebug.h"
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/mbuf.h>
47 #include <sys/sysctl.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/protosw.h>
51 #include <sys/thread.h>
52 #include <sys/globaldata.h>
53 
54 #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
55 
56 #include <net/route.h>
57 
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/in_pcb.h>
61 #ifdef INET6
62 #include <netinet6/in6_pcb.h>
63 #endif
64 #include <netinet/ip_var.h>
65 #include <netinet/tcp.h>
66 #include <netinet/tcp_fsm.h>
67 #include <netinet/tcp_seq.h>
68 #include <netinet/tcp_timer.h>
69 #include <netinet/tcp_var.h>
70 #include <netinet/tcpip.h>
71 #ifdef TCPDEBUG
72 #include <netinet/tcp_debug.h>
73 #endif
74 
75 static int
76 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
77 {
78 	int error, s, tt;
79 
80 	tt = *(int *)oidp->oid_arg1;
81 	s = (int)((int64_t)tt * 1000 / hz);
82 
83 	error = sysctl_handle_int(oidp, &s, 0, req);
84 	if (error || !req->newptr)
85 		return (error);
86 
87 	tt = (int)((int64_t)s * hz / 1000);
88 	if (tt < 1)
89 		return (EINVAL);
90 
91 	*(int *)oidp->oid_arg1 = tt;
92         return (0);
93 }
94 
95 int	tcp_keepinit;
96 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
97     &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "");
98 
99 int	tcp_keepidle;
100 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
101     &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "");
102 
103 int	tcp_keepintvl;
104 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
105     &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "");
106 
107 int	tcp_delacktime;
108 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime,
109     CTLTYPE_INT|CTLFLAG_RW, &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
110     "Time before a delayed ACK is sent");
111 
112 int	tcp_msl;
113 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
114     &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
115 
116 int	tcp_rexmit_min;
117 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
118     &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I", "Minimum Retransmission Timeout");
119 
120 int	tcp_rexmit_slop;
121 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
122     &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I", "Retransmission Timer Slop");
123 
124 static int	always_keepalive = 0;
125 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
126     &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
127 
128 static int	tcp_keepcnt = TCPTV_KEEPCNT;
129 	/* max idle probes */
130 int	tcp_maxpersistidle;
131 	/* max idle time in persist */
132 int	tcp_maxidle;
133 
134 /*
135  * Tcp protocol timeout routine called every 500 ms.
136  * Updates timestamps used for TCP
137  * causes finite state machine actions if timers expire.
138  */
139 void
140 tcp_slowtimo()
141 {
142 	int s;
143 
144 	s = splnet();
145 
146 	tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
147 
148 	splx(s);
149 }
150 
151 /*
152  * Cancel all timers for TCP tp.
153  */
154 void
155 tcp_canceltimers(tp)
156 	struct tcpcb *tp;
157 {
158 	callout_stop(tp->tt_2msl);
159 	callout_stop(tp->tt_persist);
160 	callout_stop(tp->tt_keep);
161 	callout_stop(tp->tt_rexmt);
162 }
163 
164 int	tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
165     { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
166 
167 int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
168     { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
169 
170 static int tcp_totbackoff = 511;	/* sum of tcp_backoff[] */
171 
172 /*
173  * TCP timer processing.
174  */
175 void
176 tcp_timer_delack(xtp)
177 	void *xtp;
178 {
179 	struct tcpcb *tp = xtp;
180 	int s;
181 
182 	s = splnet();
183 	if (callout_pending(tp->tt_delack) || !callout_active(tp->tt_delack)) {
184 		splx(s);
185 		return;
186 	}
187 	callout_deactivate(tp->tt_delack);
188 
189 	tp->t_flags |= TF_ACKNOW;
190 	tcpstat.tcps_delack++;
191 	(void) tcp_output(tp);
192 	splx(s);
193 }
194 
195 void
196 tcp_timer_2msl(xtp)
197 	void *xtp;
198 {
199 	struct tcpcb *tp = xtp;
200 	int s;
201 #ifdef TCPDEBUG
202 	int ostate;
203 
204 	ostate = tp->t_state;
205 #endif
206 	s = splnet();
207 	if (callout_pending(tp->tt_2msl) || !callout_active(tp->tt_2msl)) {
208 		splx(s);
209 		return;
210 	}
211 	callout_deactivate(tp->tt_2msl);
212 	/*
213 	 * 2 MSL timeout in shutdown went off.  If we're closed but
214 	 * still waiting for peer to close and connection has been idle
215 	 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
216 	 * control block.  Otherwise, check again in a bit.
217 	 */
218 	if (tp->t_state != TCPS_TIME_WAIT &&
219 	    (ticks - tp->t_rcvtime) <= tcp_maxidle)
220 		callout_reset(tp->tt_2msl, tcp_keepintvl,
221 			      tcp_timer_2msl, tp);
222 	else
223 		tp = tcp_close(tp);
224 
225 #ifdef TCPDEBUG
226 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
227 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
228 			  PRU_SLOWTIMO);
229 #endif
230 	splx(s);
231 }
232 
233 void
234 tcp_timer_keep(xtp)
235 	void *xtp;
236 {
237 	struct tcpcb *tp = xtp;
238 	struct tcptemp *t_template;
239 	int s;
240 #ifdef TCPDEBUG
241 	int ostate;
242 
243 	ostate = tp->t_state;
244 #endif
245 	s = splnet();
246 	if (callout_pending(tp->tt_keep) || !callout_active(tp->tt_keep)) {
247 		splx(s);
248 		return;
249 	}
250 	callout_deactivate(tp->tt_keep);
251 	/*
252 	 * Keep-alive timer went off; send something
253 	 * or drop connection if idle for too long.
254 	 */
255 	tcpstat.tcps_keeptimeo++;
256 	if (tp->t_state < TCPS_ESTABLISHED)
257 		goto dropit;
258 	if ((always_keepalive ||
259 	     tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) &&
260 	    tp->t_state <= TCPS_CLOSING) {
261 		if ((ticks - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle)
262 			goto dropit;
263 		/*
264 		 * Send a packet designed to force a response
265 		 * if the peer is up and reachable:
266 		 * either an ACK if the connection is still alive,
267 		 * or an RST if the peer has closed the connection
268 		 * due to timeout or reboot.
269 		 * Using sequence number tp->snd_una-1
270 		 * causes the transmitted zero-length segment
271 		 * to lie outside the receive window;
272 		 * by the protocol spec, this requires the
273 		 * correspondent TCP to respond.
274 		 */
275 		tcpstat.tcps_keepprobe++;
276 		t_template = tcp_maketemplate(tp);
277 		if (t_template) {
278 			tcp_respond(tp, t_template->tt_ipgen,
279 				    &t_template->tt_t, (struct mbuf *)NULL,
280 				    tp->rcv_nxt, tp->snd_una - 1, 0);
281 			(void) m_free(dtom(t_template));
282 		}
283 		callout_reset(tp->tt_keep, tcp_keepintvl, tcp_timer_keep, tp);
284 	} else
285 		callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
286 
287 #ifdef TCPDEBUG
288 	if (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 	return;
294 
295 dropit:
296 	tcpstat.tcps_keepdrops++;
297 	tp = tcp_drop(tp, ETIMEDOUT);
298 
299 #ifdef TCPDEBUG
300 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
301 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
302 			  PRU_SLOWTIMO);
303 #endif
304 	splx(s);
305 }
306 
307 void
308 tcp_timer_persist(xtp)
309 	void *xtp;
310 {
311 	struct tcpcb *tp = xtp;
312 	int s;
313 #ifdef TCPDEBUG
314 	int ostate;
315 
316 	ostate = tp->t_state;
317 #endif
318 	s = splnet();
319 	if (callout_pending(tp->tt_persist) || !callout_active(tp->tt_persist)){
320 		splx(s);
321 		return;
322 	}
323 	callout_deactivate(tp->tt_persist);
324 	/*
325 	 * Persistance timer into zero window.
326 	 * Force a byte to be output, if possible.
327 	 */
328 	tcpstat.tcps_persisttimeo++;
329 	/*
330 	 * Hack: if the peer is dead/unreachable, we do not
331 	 * time out if the window is closed.  After a full
332 	 * backoff, drop the connection if the idle time
333 	 * (no responses to probes) reaches the maximum
334 	 * backoff that we would use if retransmitting.
335 	 */
336 	if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
337 	    ((ticks - tp->t_rcvtime) >= tcp_maxpersistidle ||
338 	     (ticks - tp->t_rcvtime) >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
339 		tcpstat.tcps_persistdrop++;
340 		tp = tcp_drop(tp, ETIMEDOUT);
341 		goto out;
342 	}
343 	tcp_setpersist(tp);
344 	tp->t_force = 1;
345 	(void) tcp_output(tp);
346 	tp->t_force = 0;
347 
348 out:
349 #ifdef TCPDEBUG
350 	if (tp && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
351 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
352 			  PRU_SLOWTIMO);
353 #endif
354 	splx(s);
355 }
356 
357 void
358 tcp_save_congestion_state(struct tcpcb *tp)
359 {
360 	tp->snd_cwnd_prev = tp->snd_cwnd;
361 	tp->snd_ssthresh_prev = tp->snd_ssthresh;
362 	tp->snd_recover_prev = tp->snd_recover;
363 	if (IN_FASTRECOVERY(tp))
364 	  tp->t_flags |= TF_WASFRECOVERY;
365 	else
366 	  tp->t_flags &= ~TF_WASFRECOVERY;
367 	if (tp->t_flags & TF_RCVD_TSTMP) {
368 		tp->t_rexmtTS = ticks;
369 		tp->t_flags |= TF_FIRSTACCACK;
370 	}
371 }
372 
373 void
374 tcp_revert_congestion_state(struct tcpcb *tp)
375 {
376 	tp->snd_cwnd = tp->snd_cwnd_prev;
377 	tp->snd_ssthresh = tp->snd_ssthresh_prev;
378 	tp->snd_recover = tp->snd_recover_prev;
379 	if (tp->t_flags & TF_WASFRECOVERY)
380 	    ENTER_FASTRECOVERY(tp);
381 	if (tp->t_flags & TF_FASTREXMT) {
382 		++tcpstat.tcps_sndfastrexmitbad;
383 		if (tp->t_flags & TF_EARLYREXMT)
384 			++tcpstat.tcps_sndearlyrexmitbad;
385 	} else
386 		++tcpstat.tcps_sndrtobad;
387 	tp->t_badrxtwin = 0;
388 	tp->t_rxtshift = 0;
389 	tp->snd_nxt = tp->snd_max;
390 }
391 
392 void
393 tcp_timer_rexmt(xtp)
394 	void *xtp;
395 {
396 	struct tcpcb *tp = xtp;
397 	int s;
398 	int rexmt;
399 #ifdef TCPDEBUG
400 	int ostate;
401 
402 	ostate = tp->t_state;
403 #endif
404 	s = splnet();
405 	if (callout_pending(tp->tt_rexmt) || !callout_active(tp->tt_rexmt)) {
406 		splx(s);
407 		return;
408 	}
409 	callout_deactivate(tp->tt_rexmt);
410 	/*
411 	 * Retransmission timer went off.  Message has not
412 	 * been acked within retransmit interval.  Back off
413 	 * to a longer retransmit interval and retransmit one segment.
414 	 */
415 	if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
416 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
417 		tcpstat.tcps_timeoutdrop++;
418 		tp = tcp_drop(tp, tp->t_softerror ?
419 			      tp->t_softerror : ETIMEDOUT);
420 		goto out;
421 	}
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->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
433 		tcp_save_congestion_state(tp);
434 		tp->t_flags &= ~(TF_FASTREXMT | TF_EARLYREXMT);
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 	tp->snd_recover = tp->snd_max;
472 	/*
473 	 * Force a segment to be sent.
474 	 */
475 	tp->t_flags |= TF_ACKNOW;
476 	/*
477 	 * If timing a segment in this window, stop the timer.
478 	 */
479 	tp->t_rtttime = 0;
480 	/*
481 	 * Close the congestion window down to one segment
482 	 * (we'll open it by one segment for each ack we get).
483 	 * Since we probably have a window's worth of unacked
484 	 * data accumulated, this "slow start" keeps us from
485 	 * dumping all that data as back-to-back packets (which
486 	 * might overwhelm an intermediate gateway).
487 	 *
488 	 * There are two phases to the opening: Initially we
489 	 * open by one mss on each ack.  This makes the window
490 	 * size increase exponentially with time.  If the
491 	 * window is larger than the path can handle, this
492 	 * exponential growth results in dropped packet(s)
493 	 * almost immediately.  To get more time between
494 	 * drops but still "push" the network to take advantage
495 	 * of improving conditions, we switch from exponential
496 	 * to linear window opening at some threshhold size.
497 	 * For a threshhold, we use half the current window
498 	 * size, truncated to a multiple of the mss.
499 	 *
500 	 * (the minimum cwnd that will give us exponential
501 	 * growth is 2 mss.  We don't allow the threshhold
502 	 * to go below this.)
503 	 */
504 	{
505 		u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
506 		if (win < 2)
507 			win = 2;
508 		tp->snd_cwnd = tp->t_maxseg;
509 		tp->snd_ssthresh = win * tp->t_maxseg;
510 		tp->t_dupacks = 0;
511 	}
512 	EXIT_FASTRECOVERY(tp);
513 	(void) tcp_output(tp);
514 
515 out:
516 #ifdef TCPDEBUG
517 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
518 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
519 			  PRU_SLOWTIMO);
520 #endif
521 	splx(s);
522 }
523