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