tcp_timer.c revision 1.39 1 /* $NetBSD: tcp_timer.c,v 1.39 1998/05/11 20:52:18 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 <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/malloc.h>
78 #include <sys/mbuf.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/protosw.h>
82 #include <sys/errno.h>
83
84 #include <net/if.h>
85 #include <net/route.h>
86
87 #include <netinet/in.h>
88 #include <netinet/in_systm.h>
89 #include <netinet/ip.h>
90 #include <netinet/in_pcb.h>
91 #include <netinet/ip_var.h>
92 #include <netinet/tcp.h>
93 #include <netinet/tcp_fsm.h>
94 #include <netinet/tcp_seq.h>
95 #include <netinet/tcp_timer.h>
96 #include <netinet/tcp_var.h>
97 #include <netinet/tcpip.h>
98
99 int tcp_keepidle = TCPTV_KEEP_IDLE;
100 int tcp_keepintvl = TCPTV_KEEPINTVL;
101 int tcp_keepcnt = TCPTV_KEEPCNT; /* max idle probes */
102 int tcp_maxpersistidle = TCPTV_KEEP_IDLE; /* max idle time in persist */
103 int tcp_maxidle;
104
105 struct tcp_delack_head tcp_delacks;
106
107 /*
108 * Fast timeout routine for processing delayed acks
109 */
110 void
111 tcp_fasttimo()
112 {
113 register struct tcpcb *tp, *ntp;
114 int s;
115
116 s = splsoftnet();
117 for (tp = tcp_delacks.lh_first; tp != NULL; tp = ntp) {
118 /*
119 * If tcp_output() can't transmit the ACK for whatever
120 * reason, it will remain on the queue for the next
121 * time the heartbeat ticks.
122 */
123 ntp = tp->t_delack.le_next;
124 tp->t_flags |= TF_ACKNOW;
125 (void) tcp_output(tp);
126 }
127 splx(s);
128 }
129
130 /*
131 * Tcp protocol timeout routine called every 500 ms.
132 * Updates the timers in all active tcb's and
133 * causes finite state machine actions if timers expire.
134 */
135 void
136 tcp_slowtimo()
137 {
138 register struct inpcb *inp, *ninp;
139 register struct tcpcb *tp;
140 int s;
141 register long i;
142 static int syn_cache_last = 0;
143
144 s = splsoftnet();
145 tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
146 /*
147 * Search through tcb's and update active timers.
148 */
149 inp = tcbtable.inpt_queue.cqh_first;
150 if (inp == (struct inpcb *)0) { /* XXX */
151 splx(s);
152 return;
153 }
154 for (; inp != (struct inpcb *)&tcbtable.inpt_queue; inp = ninp) {
155 ninp = inp->inp_queue.cqe_next;
156 tp = intotcpcb(inp);
157 if (tp == 0 || tp->t_state == TCPS_LISTEN)
158 continue;
159 for (i = 0; i < TCPT_NTIMERS; i++) {
160 if (TCP_TIMER_ISEXPIRED(tp, i)) {
161 TCP_TIMER_DISARM(tp, i);
162 (void) tcp_usrreq(tp->t_inpcb->inp_socket,
163 PRU_SLOWTIMO, (struct mbuf *)0,
164 (struct mbuf *)i, (struct mbuf *)0,
165 (struct proc *)0);
166 /* XXX NOT MP SAFE */
167 if ((ninp == (void *)&tcbtable.inpt_queue &&
168 tcbtable.inpt_queue.cqh_last != inp) ||
169 ninp->inp_queue.cqe_prev != inp)
170 goto tpgone;
171 }
172 }
173 tp->t_idle++;
174 if (tp->t_rtt)
175 tp->t_rtt++;
176 tpgone:
177 ;
178 }
179 #if NRND == 0 /* Do we need to do this when using random() ? */
180 tcp_iss_seq += TCP_ISSINCR; /* increment iss */
181 if (tcp_compat_42)
182 if ((int)tcp_iss_seq < 0)
183 tcp_iss_seq = 0; /* XXX */
184 #endif
185 tcp_now++; /* for timestamps */
186 if (++syn_cache_last >= tcp_syn_cache_interval) {
187 syn_cache_timer();
188 syn_cache_last = 0;
189 }
190 splx(s);
191 }
192
193 /*
194 * Cancel all timers for TCP tp.
195 */
196 void
197 tcp_canceltimers(tp)
198 struct tcpcb *tp;
199 {
200 register int i;
201
202 for (i = 0; i < TCPT_NTIMERS; i++)
203 TCP_TIMER_DISARM(tp, i);
204 }
205
206 int tcp_backoff[TCP_MAXRXTSHIFT + 1] =
207 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
208
209 int tcp_totbackoff = 511; /* sum of tcp_backoff[] */
210
211 /*
212 * TCP timer processing.
213 */
214 struct tcpcb *
215 tcp_timers(tp, timer)
216 register struct tcpcb *tp;
217 int timer;
218 {
219 short rto;
220
221 switch (timer) {
222
223 /*
224 * 2 MSL timeout in shutdown went off. If we're closed but
225 * still waiting for peer to close and connection has been idle
226 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
227 * control block. Otherwise, check again in a bit.
228 */
229 case TCPT_2MSL:
230 if (tp->t_state != TCPS_TIME_WAIT &&
231 tp->t_idle <= tcp_maxidle)
232 TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_keepintvl);
233 else
234 tp = tcp_close(tp);
235 break;
236
237 /*
238 * Retransmission timer went off. Message has not
239 * been acked within retransmit interval. Back off
240 * to a longer retransmit interval and retransmit one segment.
241 */
242 case TCPT_REXMT:
243 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
244 tp->t_rxtshift = TCP_MAXRXTSHIFT;
245 tcpstat.tcps_timeoutdrop++;
246 tp = tcp_drop(tp, tp->t_softerror ?
247 tp->t_softerror : ETIMEDOUT);
248 break;
249 }
250 tcpstat.tcps_rexmttimeo++;
251 rto = TCP_REXMTVAL(tp);
252 if (rto < tp->t_rttmin)
253 rto = tp->t_rttmin;
254 TCPT_RANGESET(tp->t_rxtcur, rto * tcp_backoff[tp->t_rxtshift],
255 tp->t_rttmin, TCPTV_REXMTMAX);
256 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
257 #if 0
258 /*
259 * If we are losing and we are trying path MTU discovery,
260 * try turning it off. This will avoid black holes in
261 * the network which suppress or fail to send "packet
262 * too big" ICMP messages. We should ideally do
263 * lots more sophisticated searching to find the right
264 * value here...
265 */
266 if (ip_mtudisc && tp->t_rxtshift > TCP_MAXRXTSHIFT / 6) {
267 struct inpcb *inp = tp->t_inpcb;
268 struct rtentry *rt = in_pcbrtentry(inp);
269
270 /* XXX: Black hole recovery code goes here */
271 }
272 #endif
273 /*
274 * If losing, let the lower level know and try for
275 * a better route. Also, if we backed off this far,
276 * our srtt estimate is probably bogus. Clobber it
277 * so we'll take the next rtt measurement as our srtt;
278 * move the current srtt into rttvar to keep the current
279 * retransmit times until then.
280 */
281 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
282 in_losing(tp->t_inpcb);
283 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
284 tp->t_srtt = 0;
285 }
286 tp->snd_nxt = tp->snd_una;
287 /*
288 * If timing a segment in this window, stop the timer.
289 */
290 tp->t_rtt = 0;
291 /*
292 * Remember if we are retransmitting a SYN, because if
293 * we do, set the initial congestion window must be set
294 * to 1 segment.
295 */
296 if (tp->t_state == TCPS_SYN_SENT)
297 tp->t_flags |= TF_SYN_REXMT;
298 /*
299 * Close the congestion window down to the initial window
300 * (we'll open it by one segment for each ack we get).
301 * Since we probably have a window's worth of unacked
302 * data accumulated, this "slow start" keeps us from
303 * dumping all that data as back-to-back packets (which
304 * might overwhelm an intermediate gateway).
305 *
306 * There are two phases to the opening: Initially we
307 * open by one mss on each ack. This makes the window
308 * size increase exponentially with time. If the
309 * window is larger than the path can handle, this
310 * exponential growth results in dropped packet(s)
311 * almost immediately. To get more time between
312 * drops but still "push" the network to take advantage
313 * of improving conditions, we switch from exponential
314 * to linear window opening at some threshhold size.
315 * For a threshhold, we use half the current window
316 * size, truncated to a multiple of the mss.
317 *
318 * (the minimum cwnd that will give us exponential
319 * growth is 2 mss. We don't allow the threshhold
320 * to go below this.)
321 */
322 {
323 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_segsz;
324 if (win < 2)
325 win = 2;
326 tp->snd_cwnd = TCP_INITIAL_WINDOW(tcp_init_win, tp->t_segsz);
327 tp->snd_ssthresh = win * tp->t_segsz;
328 tp->t_dupacks = 0;
329 }
330 (void) tcp_output(tp);
331 break;
332
333 /*
334 * Persistance timer into zero window.
335 * Force a byte to be output, if possible.
336 */
337 case TCPT_PERSIST:
338 /*
339 * Hack: if the peer is dead/unreachable, we do not
340 * time out if the window is closed. After a full
341 * backoff, drop the connection if the idle time
342 * (no responses to probes) reaches the maximum
343 * backoff that we would use if retransmitting.
344 */
345 rto = TCP_REXMTVAL(tp);
346 if (rto < tp->t_rttmin)
347 rto = tp->t_rttmin;
348 if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
349 (tp->t_idle >= tcp_maxpersistidle ||
350 tp->t_idle >= rto * tcp_totbackoff)) {
351 tcpstat.tcps_persistdrops++;
352 tp = tcp_drop(tp, ETIMEDOUT);
353 break;
354 }
355 tcpstat.tcps_persisttimeo++;
356 tcp_setpersist(tp);
357 tp->t_force = 1;
358 (void) tcp_output(tp);
359 tp->t_force = 0;
360 break;
361
362 /*
363 * Keep-alive timer went off; send something
364 * or drop connection if idle for too long.
365 */
366 case TCPT_KEEP:
367 tcpstat.tcps_keeptimeo++;
368 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
369 goto dropit;
370 if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE &&
371 tp->t_state <= TCPS_CLOSE_WAIT) {
372 if (tp->t_idle >= tcp_keepidle + tcp_maxidle)
373 goto dropit;
374 /*
375 * Send a packet designed to force a response
376 * if the peer is up and reachable:
377 * either an ACK if the connection is still alive,
378 * or an RST if the peer has closed the connection
379 * due to timeout or reboot.
380 * Using sequence number tp->snd_una-1
381 * causes the transmitted zero-length segment
382 * to lie outside the receive window;
383 * by the protocol spec, this requires the
384 * correspondent TCP to respond.
385 */
386 tcpstat.tcps_keepprobe++;
387 if (tcp_compat_42) {
388 /*
389 * The keepalive packet must have nonzero
390 * length to get a 4.2 host to respond.
391 */
392 (void)tcp_respond(tp, tp->t_template,
393 (struct mbuf *)NULL, tp->rcv_nxt - 1,
394 tp->snd_una - 1, 0);
395 } else {
396 (void)tcp_respond(tp, tp->t_template,
397 (struct mbuf *)NULL, tp->rcv_nxt,
398 tp->snd_una - 1, 0);
399 }
400 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepintvl);
401 } else
402 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
403 break;
404 dropit:
405 tcpstat.tcps_keepdrops++;
406 tp = tcp_drop(tp, ETIMEDOUT);
407 break;
408 }
409 return (tp);
410 }
411