xref: /netbsd-src/sys/netinet/tcp_timer.c (revision 7c7c171d130af9949261bc7dce2150a03c3d239c)
1 /*	$NetBSD: tcp_timer.c,v 1.32 1998/03/31 22:49:10 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
9  * Facility, NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
42  *	The Regents of the University of California.  All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. All advertising materials mentioning features or use of this software
53  *    must display the following acknowledgement:
54  *	This product includes software developed by the University of
55  *	California, Berkeley and its contributors.
56  * 4. Neither the name of the University nor the names of its contributors
57  *    may be used to endorse or promote products derived from this software
58  *    without specific prior written permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70  * SUCH DAMAGE.
71  *
72  *	@(#)tcp_timer.c	8.2 (Berkeley) 5/24/95
73  */
74 
75 #include "opt_tcp_compat_42.h"
76 
77 #ifndef TUBA_INCLUDE
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/malloc.h>
81 #include <sys/mbuf.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/protosw.h>
85 #include <sys/errno.h>
86 
87 #include <net/if.h>
88 #include <net/route.h>
89 
90 #include <netinet/in.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/ip.h>
93 #include <netinet/in_pcb.h>
94 #include <netinet/ip_var.h>
95 #include <netinet/tcp.h>
96 #include <netinet/tcp_fsm.h>
97 #include <netinet/tcp_seq.h>
98 #include <netinet/tcp_timer.h>
99 #include <netinet/tcp_var.h>
100 #include <netinet/tcpip.h>
101 
102 int	tcp_keepidle = TCPTV_KEEP_IDLE;
103 int	tcp_keepintvl = TCPTV_KEEPINTVL;
104 int	tcp_keepcnt = TCPTV_KEEPCNT;		/* max idle probes */
105 int	tcp_maxpersistidle = TCPTV_KEEP_IDLE;	/* max idle time in persist */
106 int	tcp_maxidle;
107 #else /* TUBA_INCLUDE */
108 
109 extern	int tcp_keepcnt;
110 extern	int tcp_maxpersistidle;
111 #endif /* TUBA_INCLUDE */
112 
113 struct tcp_delack_head tcp_delacks;
114 
115 /*
116  * Fast timeout routine for processing delayed acks
117  */
118 void
119 tcp_fasttimo()
120 {
121 	register struct tcpcb *tp, *ntp;
122 	int s;
123 
124 	s = splsoftnet();
125 	for (tp = tcp_delacks.lh_first; tp != NULL; tp = ntp) {
126 		/*
127 		 * If tcp_output() can't transmit the ACK for whatever
128 		 * reason, it will remain on the queue for the next
129 		 * time the heartbeat ticks.
130 		 */
131 		ntp = tp->t_delack.le_next;
132 		tp->t_flags |= TF_ACKNOW;
133 		(void) tcp_output(tp);
134 	}
135 	splx(s);
136 }
137 
138 /*
139  * Tcp protocol timeout routine called every 500 ms.
140  * Updates the timers in all active tcb's and
141  * causes finite state machine actions if timers expire.
142  */
143 void
144 tcp_slowtimo()
145 {
146 	register struct inpcb *inp, *ninp;
147 	register struct tcpcb *tp;
148 	int s;
149 	register long i;
150 	static int syn_cache_last = 0;
151 
152 	s = splsoftnet();
153 	tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
154 	/*
155 	 * Search through tcb's and update active timers.
156 	 */
157 	inp = tcbtable.inpt_queue.cqh_first;
158 	if (inp == (struct inpcb *)0) {				/* XXX */
159 		splx(s);
160 		return;
161 	}
162 	for (; inp != (struct inpcb *)&tcbtable.inpt_queue; inp = ninp) {
163 		ninp = inp->inp_queue.cqe_next;
164 		tp = intotcpcb(inp);
165 		if (tp == 0 || tp->t_state == TCPS_LISTEN)
166 			continue;
167 		for (i = 0; i < TCPT_NTIMERS; i++) {
168 			if (tp->t_timer[i] && --tp->t_timer[i] == 0) {
169 				(void) tcp_usrreq(tp->t_inpcb->inp_socket,
170 				    PRU_SLOWTIMO, (struct mbuf *)0,
171 				    (struct mbuf *)i, (struct mbuf *)0,
172 				    (struct proc *)0);
173 				/* XXX NOT MP SAFE */
174 				if ((ninp == (void *)&tcbtable.inpt_queue &&
175 				    tcbtable.inpt_queue.cqh_last != inp) ||
176 				    ninp->inp_queue.cqe_prev != inp)
177 					goto tpgone;
178 			}
179 		}
180 		tp->t_idle++;
181 		if (tp->t_rtt)
182 			tp->t_rtt++;
183 tpgone:
184 		;
185 	}
186 #if NRND == 0 /* Do we need to do this when using random() ? */
187 	tcp_iss_seq += TCP_ISSINCR;			/* increment iss */
188 #ifdef TCP_COMPAT_42
189 	if ((int)tcp_iss_seq < 0)
190 		tcp_iss_seq = 0;			/* XXX */
191 #endif
192 #endif
193 	tcp_now++;					/* for timestamps */
194 	if (++syn_cache_last >= tcp_syn_cache_interval) {
195 		syn_cache_timer(syn_cache_last);
196 		syn_cache_last = 0;
197 	}
198 	splx(s);
199 }
200 #ifndef TUBA_INCLUDE
201 
202 /*
203  * Cancel all timers for TCP tp.
204  */
205 void
206 tcp_canceltimers(tp)
207 	struct tcpcb *tp;
208 {
209 	register int i;
210 
211 	for (i = 0; i < TCPT_NTIMERS; i++)
212 		tp->t_timer[i] = 0;
213 }
214 
215 int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
216     { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
217 
218 int	tcp_totbackoff = 511;	/* sum of tcp_backoff[] */
219 
220 /*
221  * TCP timer processing.
222  */
223 struct tcpcb *
224 tcp_timers(tp, timer)
225 	register struct tcpcb *tp;
226 	int timer;
227 {
228 	short	rto;
229 
230 	switch (timer) {
231 
232 	/*
233 	 * 2 MSL timeout in shutdown went off.  If we're closed but
234 	 * still waiting for peer to close and connection has been idle
235 	 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
236 	 * control block.  Otherwise, check again in a bit.
237 	 */
238 	case TCPT_2MSL:
239 		if (tp->t_state != TCPS_TIME_WAIT &&
240 		    tp->t_idle <= tcp_maxidle)
241 			tp->t_timer[TCPT_2MSL] = tcp_keepintvl;
242 		else
243 			tp = tcp_close(tp);
244 		break;
245 
246 	/*
247 	 * Retransmission timer went off.  Message has not
248 	 * been acked within retransmit interval.  Back off
249 	 * to a longer retransmit interval and retransmit one segment.
250 	 */
251 	case TCPT_REXMT:
252 		if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
253 			tp->t_rxtshift = TCP_MAXRXTSHIFT;
254 			tcpstat.tcps_timeoutdrop++;
255 			tp = tcp_drop(tp, tp->t_softerror ?
256 			    tp->t_softerror : ETIMEDOUT);
257 			break;
258 		}
259 		tcpstat.tcps_rexmttimeo++;
260 		rto = TCP_REXMTVAL(tp);
261 		if (rto < tp->t_rttmin)
262 			rto = tp->t_rttmin;
263 		TCPT_RANGESET(tp->t_rxtcur, rto * tcp_backoff[tp->t_rxtshift],
264 		    tp->t_rttmin, TCPTV_REXMTMAX);
265 		tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
266 		/*
267 		 * If losing, let the lower level know and try for
268 		 * a better route.  Also, if we backed off this far,
269 		 * our srtt estimate is probably bogus.  Clobber it
270 		 * so we'll take the next rtt measurement as our srtt;
271 		 * move the current srtt into rttvar to keep the current
272 		 * retransmit times until then.
273 		 */
274 		if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
275 			in_losing(tp->t_inpcb);
276 			tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
277 			tp->t_srtt = 0;
278 		}
279 		tp->snd_nxt = tp->snd_una;
280 		/*
281 		 * If timing a segment in this window, stop the timer.
282 		 */
283 		tp->t_rtt = 0;
284 		/*
285 		 * Remember if we are retransmitting a SYN, because if
286 		 * we do, set the initial congestion window must be set
287 		 * to 1 segment.
288 		 */
289 		if (tp->t_state == TCPS_SYN_SENT)
290 			tp->t_flags |= TF_SYN_REXMT;
291 		/*
292 		 * Close the congestion window down to the initial window
293 		 * (we'll open it by one segment for each ack we get).
294 		 * Since we probably have a window's worth of unacked
295 		 * data accumulated, this "slow start" keeps us from
296 		 * dumping all that data as back-to-back packets (which
297 		 * might overwhelm an intermediate gateway).
298 		 *
299 		 * There are two phases to the opening: Initially we
300 		 * open by one mss on each ack.  This makes the window
301 		 * size increase exponentially with time.  If the
302 		 * window is larger than the path can handle, this
303 		 * exponential growth results in dropped packet(s)
304 		 * almost immediately.  To get more time between
305 		 * drops but still "push" the network to take advantage
306 		 * of improving conditions, we switch from exponential
307 		 * to linear window opening at some threshhold size.
308 		 * For a threshhold, we use half the current window
309 		 * size, truncated to a multiple of the mss.
310 		 *
311 		 * (the minimum cwnd that will give us exponential
312 		 * growth is 2 mss.  We don't allow the threshhold
313 		 * to go below this.)
314 		 */
315 		{
316 		u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_segsz;
317 		if (win < 2)
318 			win = 2;
319 		tp->snd_cwnd = TCP_INITIAL_WINDOW(tcp_init_win, tp->t_segsz);
320 		tp->snd_ssthresh = win * tp->t_segsz;
321 		tp->t_dupacks = 0;
322 		}
323 		(void) tcp_output(tp);
324 		break;
325 
326 	/*
327 	 * Persistance timer into zero window.
328 	 * Force a byte to be output, if possible.
329 	 */
330 	case TCPT_PERSIST:
331 		/*
332 		 * Hack: if the peer is dead/unreachable, we do not
333 		 * time out if the window is closed.  After a full
334 		 * backoff, drop the connection if the idle time
335 		 * (no responses to probes) reaches the maximum
336 		 * backoff that we would use if retransmitting.
337 		 */
338 		rto = TCP_REXMTVAL(tp);
339 		if (rto < tp->t_rttmin)
340 			rto = tp->t_rttmin;
341 		if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
342 		    (tp->t_idle >= tcp_maxpersistidle ||
343 		    tp->t_idle >= rto * tcp_totbackoff)) {
344 			tcpstat.tcps_persistdrops++;
345 			tp = tcp_drop(tp, ETIMEDOUT);
346 			break;
347 		}
348 		tcpstat.tcps_persisttimeo++;
349 		tcp_setpersist(tp);
350 		tp->t_force = 1;
351 		(void) tcp_output(tp);
352 		tp->t_force = 0;
353 		break;
354 
355 	/*
356 	 * Keep-alive timer went off; send something
357 	 * or drop connection if idle for too long.
358 	 */
359 	case TCPT_KEEP:
360 		tcpstat.tcps_keeptimeo++;
361 		if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
362 			goto dropit;
363 		if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE &&
364 		    tp->t_state <= TCPS_CLOSE_WAIT) {
365 		    	if (tp->t_idle >= tcp_keepidle + tcp_maxidle)
366 				goto dropit;
367 			/*
368 			 * Send a packet designed to force a response
369 			 * if the peer is up and reachable:
370 			 * either an ACK if the connection is still alive,
371 			 * or an RST if the peer has closed the connection
372 			 * due to timeout or reboot.
373 			 * Using sequence number tp->snd_una-1
374 			 * causes the transmitted zero-length segment
375 			 * to lie outside the receive window;
376 			 * by the protocol spec, this requires the
377 			 * correspondent TCP to respond.
378 			 */
379 			tcpstat.tcps_keepprobe++;
380 #ifdef TCP_COMPAT_42
381 			/*
382 			 * The keepalive packet must have nonzero length
383 			 * to get a 4.2 host to respond.
384 			 */
385 			(void)tcp_respond(tp, tp->t_template,
386 			    (struct mbuf *)NULL, tp->rcv_nxt - 1,
387 			    tp->snd_una - 1, 0);
388 #else
389 			(void)tcp_respond(tp, tp->t_template,
390 			    (struct mbuf *)NULL, tp->rcv_nxt,
391 			    tp->snd_una - 1, 0);
392 #endif
393 			tp->t_timer[TCPT_KEEP] = tcp_keepintvl;
394 		} else
395 			tp->t_timer[TCPT_KEEP] = tcp_keepidle;
396 		break;
397 	dropit:
398 		tcpstat.tcps_keepdrops++;
399 		tp = tcp_drop(tp, ETIMEDOUT);
400 		break;
401 	}
402 	return (tp);
403 }
404 #endif /* TUBA_INCLUDE */
405