1 /* $NetBSD: tcp_timer.c,v 1.46 2000/03/30 13:25:10 augustss 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 115 #include <net/if.h> 116 #include <net/route.h> 117 118 #include <netinet/in.h> 119 #include <netinet/in_systm.h> 120 #include <netinet/ip.h> 121 #include <netinet/in_pcb.h> 122 #include <netinet/ip_var.h> 123 124 #ifdef INET6 125 #ifndef INET 126 #include <netinet/in.h> 127 #endif 128 #include <netinet/ip6.h> 129 #include <netinet6/in6_pcb.h> 130 #endif 131 132 #include <netinet/tcp.h> 133 #include <netinet/tcp_fsm.h> 134 #include <netinet/tcp_seq.h> 135 #include <netinet/tcp_timer.h> 136 #include <netinet/tcp_var.h> 137 #include <netinet/tcpip.h> 138 139 int tcp_keepidle = TCPTV_KEEP_IDLE; 140 int tcp_keepintvl = TCPTV_KEEPINTVL; 141 int tcp_keepcnt = TCPTV_KEEPCNT; /* max idle probes */ 142 int tcp_maxpersistidle = TCPTV_KEEP_IDLE; /* max idle time in persist */ 143 int tcp_maxidle; 144 145 struct tcp_delack_head tcp_delacks; 146 147 /* 148 * Fast timeout routine for processing delayed acks 149 */ 150 void 151 tcp_fasttimo() 152 { 153 struct tcpcb *tp, *ntp; 154 int s; 155 156 s = splsoftnet(); 157 for (tp = tcp_delacks.lh_first; tp != NULL; tp = ntp) { 158 /* 159 * If tcp_output() can't transmit the ACK for whatever 160 * reason, it will remain on the queue for the next 161 * time the heartbeat ticks. 162 */ 163 ntp = tp->t_delack.le_next; 164 tp->t_flags |= TF_ACKNOW; 165 (void) tcp_output(tp); 166 } 167 splx(s); 168 } 169 170 /* 171 * Tcp protocol timeout routine called every 500 ms. 172 * Updates the timers in all active tcb's and 173 * causes finite state machine actions if timers expire. 174 */ 175 void 176 tcp_slowtimo() 177 { 178 struct inpcb *inp, *ninp; 179 struct tcpcb *tp; 180 #if defined(INET6) && !defined(TCP6) 181 struct in6pcb *in6p, *nin6p; 182 #endif 183 int s; 184 long i; 185 static int syn_cache_last = 0; 186 int skip, mask; 187 188 skip = mask = 0; 189 190 s = splsoftnet(); 191 tcp_maxidle = tcp_keepcnt * tcp_keepintvl; 192 /* 193 * Search through tcb's and update active timers. 194 */ 195 mask |= 1; 196 inp = tcbtable.inpt_queue.cqh_first; 197 if (inp == (struct inpcb *)0) { /* XXX */ 198 skip |= 1; 199 goto dotcb6; 200 } 201 for (; inp != (struct inpcb *)&tcbtable.inpt_queue; inp = ninp) { 202 ninp = inp->inp_queue.cqe_next; 203 tp = intotcpcb(inp); 204 if (tp == 0 || tp->t_state == TCPS_LISTEN) 205 continue; 206 for (i = 0; i < TCPT_NTIMERS; i++) { 207 if (TCP_TIMER_ISEXPIRED(tp, i)) { 208 TCP_TIMER_DISARM(tp, i); 209 (void) tcp_usrreq(tp->t_inpcb->inp_socket, 210 PRU_SLOWTIMO, (struct mbuf *)0, 211 (struct mbuf *)i, (struct mbuf *)0, 212 (struct proc *)0); 213 /* XXX NOT MP SAFE */ 214 if ((ninp == (void *)&tcbtable.inpt_queue && 215 tcbtable.inpt_queue.cqh_last != inp) || 216 ninp->inp_queue.cqe_prev != inp) 217 goto tpgone; 218 } 219 } 220 tp->t_idle++; 221 if (tp->t_rtt) 222 tp->t_rtt++; 223 tpgone: 224 ; 225 } 226 dotcb6: 227 #if defined(INET6) && !defined(TCP6) 228 mask |= 2; 229 in6p = tcb6.in6p_next; 230 if (in6p == (struct in6pcb *)0) { /* XXX */ 231 skip |= 2; 232 goto doiss; 233 } 234 for (; in6p != (struct in6pcb *)&tcb6; in6p = nin6p) { 235 nin6p = in6p->in6p_next; 236 tp = in6totcpcb(in6p); 237 if (tp == 0 || tp->t_state == TCPS_LISTEN) 238 continue; 239 for (i = 0; i < TCPT_NTIMERS; i++) { 240 if (TCP_TIMER_ISEXPIRED(tp, i)) { 241 TCP_TIMER_DISARM(tp, i); 242 (void) tcp_usrreq(tp->t_in6pcb->in6p_socket, 243 PRU_SLOWTIMO, (struct mbuf *)0, 244 (struct mbuf *)i, (struct mbuf *)0, 245 (struct proc *)0); 246 /* XXX NOT MP SAFE */ 247 if ((nin6p == (void *)&tcb6 && 248 tcb6.in6p_prev != in6p) || 249 nin6p->in6p_prev != in6p) 250 goto tp6gone; 251 } 252 } 253 tp->t_idle++; 254 if (tp->t_rtt) 255 tp->t_rtt++; 256 tp6gone: 257 ; 258 } 259 260 doiss: 261 #endif 262 if (mask == skip) 263 goto done; 264 tcp_iss_seq += TCP_ISSINCR; /* increment iss */ 265 tcp_now++; /* for timestamps */ 266 if (++syn_cache_last >= tcp_syn_cache_interval) { 267 syn_cache_timer(); 268 syn_cache_last = 0; 269 } 270 done: 271 splx(s); 272 } 273 274 /* 275 * Cancel all timers for TCP tp. 276 */ 277 void 278 tcp_canceltimers(tp) 279 struct tcpcb *tp; 280 { 281 int i; 282 283 for (i = 0; i < TCPT_NTIMERS; i++) 284 TCP_TIMER_DISARM(tp, i); 285 } 286 287 int tcp_backoff[TCP_MAXRXTSHIFT + 1] = 288 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 }; 289 290 int tcp_totbackoff = 511; /* sum of tcp_backoff[] */ 291 292 /* 293 * TCP timer processing. 294 */ 295 struct tcpcb * 296 tcp_timers(tp, timer) 297 struct tcpcb *tp; 298 int timer; 299 { 300 short rto; 301 302 switch (timer) { 303 304 /* 305 * 2 MSL timeout in shutdown went off. If we're closed but 306 * still waiting for peer to close and connection has been idle 307 * too long, or if 2MSL time is up from TIME_WAIT, delete connection 308 * control block. Otherwise, check again in a bit. 309 */ 310 case TCPT_2MSL: 311 if (tp->t_state != TCPS_TIME_WAIT && 312 ((tcp_maxidle == 0) || (tp->t_idle <= tcp_maxidle))) 313 TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_keepintvl); 314 else 315 tp = tcp_close(tp); 316 break; 317 318 /* 319 * Retransmission timer went off. Message has not 320 * been acked within retransmit interval. Back off 321 * to a longer retransmit interval and retransmit one segment. 322 */ 323 case TCPT_REXMT: 324 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) { 325 tp->t_rxtshift = TCP_MAXRXTSHIFT; 326 tcpstat.tcps_timeoutdrop++; 327 tp = tcp_drop(tp, tp->t_softerror ? 328 tp->t_softerror : ETIMEDOUT); 329 break; 330 } 331 tcpstat.tcps_rexmttimeo++; 332 rto = TCP_REXMTVAL(tp); 333 if (rto < tp->t_rttmin) 334 rto = tp->t_rttmin; 335 TCPT_RANGESET(tp->t_rxtcur, rto * tcp_backoff[tp->t_rxtshift], 336 tp->t_rttmin, TCPTV_REXMTMAX); 337 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 338 #if 0 339 /* 340 * If we are losing and we are trying path MTU discovery, 341 * try turning it off. This will avoid black holes in 342 * the network which suppress or fail to send "packet 343 * too big" ICMP messages. We should ideally do 344 * lots more sophisticated searching to find the right 345 * value here... 346 */ 347 if (ip_mtudisc && tp->t_rxtshift > TCP_MAXRXTSHIFT / 6) { 348 struct rtentry *rt = NULL; 349 350 if (tp->t_inpcb) 351 rt = in_pcbrtentry(tp->t_inpcb); 352 #ifdef INET6 353 else if (tp->t_in6pcb) 354 rt = in6_pcbrtentry(tp->t_in6pcb); 355 #endif 356 357 /* XXX: Black hole recovery code goes here */ 358 } 359 #endif 360 /* 361 * If losing, let the lower level know and try for 362 * a better route. Also, if we backed off this far, 363 * our srtt estimate is probably bogus. Clobber it 364 * so we'll take the next rtt measurement as our srtt; 365 * move the current srtt into rttvar to keep the current 366 * retransmit times until then. 367 */ 368 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { 369 if (tp->t_inpcb) 370 in_losing(tp->t_inpcb); 371 #ifdef INET6 372 else if (tp->t_in6pcb) 373 in6_losing(tp->t_in6pcb); 374 #endif 375 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT); 376 tp->t_srtt = 0; 377 } 378 tp->snd_nxt = tp->snd_una; 379 /* 380 * If timing a segment in this window, stop the timer. 381 */ 382 tp->t_rtt = 0; 383 /* 384 * Remember if we are retransmitting a SYN, because if 385 * we do, set the initial congestion window must be set 386 * to 1 segment. 387 */ 388 if (tp->t_state == TCPS_SYN_SENT) 389 tp->t_flags |= TF_SYN_REXMT; 390 /* 391 * Close the congestion window down to one segment 392 * (we'll open it by one segment for each ack we get). 393 * Since we probably have a window's worth of unacked 394 * data accumulated, this "slow start" keeps us from 395 * dumping all that data as back-to-back packets (which 396 * might overwhelm an intermediate gateway). 397 * 398 * There are two phases to the opening: Initially we 399 * open by one mss on each ack. This makes the window 400 * size increase exponentially with time. If the 401 * window is larger than the path can handle, this 402 * exponential growth results in dropped packet(s) 403 * almost immediately. To get more time between 404 * drops but still "push" the network to take advantage 405 * of improving conditions, we switch from exponential 406 * to linear window opening at some threshhold size. 407 * For a threshhold, we use half the current window 408 * size, truncated to a multiple of the mss. 409 * 410 * (the minimum cwnd that will give us exponential 411 * growth is 2 mss. We don't allow the threshhold 412 * to go below this.) 413 */ 414 { 415 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_segsz; 416 if (win < 2) 417 win = 2; 418 /* Loss Window MUST be one segment. */ 419 tp->snd_cwnd = tp->t_segsz; 420 tp->snd_ssthresh = win * tp->t_segsz; 421 tp->t_dupacks = 0; 422 } 423 (void) tcp_output(tp); 424 break; 425 426 /* 427 * Persistance timer into zero window. 428 * Force a byte to be output, if possible. 429 */ 430 case TCPT_PERSIST: 431 /* 432 * Hack: if the peer is dead/unreachable, we do not 433 * time out if the window is closed. After a full 434 * backoff, drop the connection if the idle time 435 * (no responses to probes) reaches the maximum 436 * backoff that we would use if retransmitting. 437 */ 438 rto = TCP_REXMTVAL(tp); 439 if (rto < tp->t_rttmin) 440 rto = tp->t_rttmin; 441 if (tp->t_rxtshift == TCP_MAXRXTSHIFT && 442 (tp->t_idle >= tcp_maxpersistidle || 443 tp->t_idle >= rto * tcp_totbackoff)) { 444 tcpstat.tcps_persistdrops++; 445 tp = tcp_drop(tp, ETIMEDOUT); 446 break; 447 } 448 tcpstat.tcps_persisttimeo++; 449 tcp_setpersist(tp); 450 tp->t_force = 1; 451 (void) tcp_output(tp); 452 tp->t_force = 0; 453 break; 454 455 /* 456 * Keep-alive timer went off; send something 457 * or drop connection if idle for too long. 458 */ 459 case TCPT_KEEP: 460 { 461 struct socket *so = NULL; 462 463 tcpstat.tcps_keeptimeo++; 464 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0) 465 goto dropit; 466 if (tp->t_inpcb) 467 so = tp->t_inpcb->inp_socket; 468 #ifdef INET6 469 else if (tp->t_in6pcb) 470 so = tp->t_in6pcb->in6p_socket; 471 #endif 472 if (so->so_options & SO_KEEPALIVE && 473 tp->t_state <= TCPS_CLOSE_WAIT) { 474 if ((tcp_maxidle > 0) && 475 (tp->t_idle >= tcp_keepidle + tcp_maxidle)) 476 goto dropit; 477 /* 478 * Send a packet designed to force a response 479 * if the peer is up and reachable: 480 * either an ACK if the connection is still alive, 481 * or an RST if the peer has closed the connection 482 * due to timeout or reboot. 483 * Using sequence number tp->snd_una-1 484 * causes the transmitted zero-length segment 485 * to lie outside the receive window; 486 * by the protocol spec, this requires the 487 * correspondent TCP to respond. 488 */ 489 tcpstat.tcps_keepprobe++; 490 if (tcp_compat_42) { 491 /* 492 * The keepalive packet must have nonzero 493 * length to get a 4.2 host to respond. 494 */ 495 (void)tcp_respond(tp, tp->t_template, 496 (struct mbuf *)NULL, NULL, tp->rcv_nxt - 1, 497 tp->snd_una - 1, 0); 498 } else { 499 (void)tcp_respond(tp, tp->t_template, 500 (struct mbuf *)NULL, NULL, tp->rcv_nxt, 501 tp->snd_una - 1, 0); 502 } 503 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepintvl); 504 } else 505 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle); 506 break; 507 } 508 dropit: 509 tcpstat.tcps_keepdrops++; 510 tp = tcp_drop(tp, ETIMEDOUT); 511 break; 512 } 513 return (tp); 514 } 515