tcp_timer.c revision 1.54
1/* $NetBSD: tcp_timer.c,v 1.54 2001/09/10 22:45:46 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, 2001 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#include "opt_tcp_debug.h" 106 107#include <sys/param.h> 108#include <sys/systm.h> 109#include <sys/malloc.h> 110#include <sys/mbuf.h> 111#include <sys/socket.h> 112#include <sys/socketvar.h> 113#include <sys/protosw.h> 114#include <sys/errno.h> 115#include <sys/kernel.h> 116 117#include <net/if.h> 118#include <net/route.h> 119 120#include <netinet/in.h> 121#include <netinet/in_systm.h> 122#include <netinet/ip.h> 123#include <netinet/in_pcb.h> 124#include <netinet/ip_var.h> 125 126#ifdef INET6 127#ifndef INET 128#include <netinet/in.h> 129#endif 130#include <netinet/ip6.h> 131#include <netinet6/in6_pcb.h> 132#endif 133 134#include <netinet/tcp.h> 135#include <netinet/tcp_fsm.h> 136#include <netinet/tcp_seq.h> 137#include <netinet/tcp_timer.h> 138#include <netinet/tcp_var.h> 139#include <netinet/tcpip.h> 140#ifdef TCP_DEBUG 141#include <netinet/tcp_debug.h> 142#endif 143 144/* 145 * Various tunable timer parameters. These are initialized in tcp_init(), 146 * unless they are patched. 147 */ 148int tcp_keepidle = 0; 149int tcp_keepintvl = 0; 150int tcp_keepcnt = 0; /* max idle probes */ 151int tcp_maxpersistidle = 0; /* max idle time in persist */ 152int tcp_maxidle; /* computed in tcp_slowtimo() */ 153 154/* 155 * Time to delay the ACK. This is initialized in tcp_init(), unless 156 * its patched. 157 */ 158int tcp_delack_ticks = 0; 159 160void tcp_timer_rexmt(void *); 161void tcp_timer_persist(void *); 162void tcp_timer_keep(void *); 163void tcp_timer_2msl(void *); 164 165tcp_timer_func_t tcp_timer_funcs[TCPT_NTIMERS] = { 166 tcp_timer_rexmt, 167 tcp_timer_persist, 168 tcp_timer_keep, 169 tcp_timer_2msl, 170}; 171 172/* 173 * Timer state initialization, called from tcp_init(). 174 */ 175void 176tcp_timer_init(void) 177{ 178 179 if (tcp_keepidle == 0) 180 tcp_keepidle = TCPTV_KEEP_IDLE; 181 182 if (tcp_keepintvl == 0) 183 tcp_keepintvl = TCPTV_KEEPINTVL; 184 185 if (tcp_keepcnt == 0) 186 tcp_keepcnt = TCPTV_KEEPCNT; 187 188 if (tcp_maxpersistidle == 0) 189 tcp_maxpersistidle = TCPTV_KEEP_IDLE; 190 191 if (tcp_delack_ticks == 0) 192 tcp_delack_ticks = TCP_DELACK_TICKS; 193} 194 195/* 196 * Callout to process delayed ACKs for a TCPCB. 197 */ 198void 199tcp_delack(void *arg) 200{ 201 struct tcpcb *tp = arg; 202 int s; 203 204 /* 205 * If tcp_output() wasn't able to transmit the ACK 206 * for whatever reason, it will restart the delayed 207 * ACK callout. 208 */ 209 210 s = splsoftnet(); 211 tp->t_flags |= TF_ACKNOW; 212 (void) tcp_output(tp); 213 splx(s); 214} 215 216/* 217 * Tcp protocol timeout routine called every 500 ms. 218 * Updates the timers in all active tcb's and 219 * causes finite state machine actions if timers expire. 220 */ 221void 222tcp_slowtimo() 223{ 224 static int syn_cache_last = 0; 225 int s; 226 227 s = splsoftnet(); 228 tcp_maxidle = tcp_keepcnt * tcp_keepintvl; 229 tcp_iss_seq += TCP_ISSINCR; /* increment iss */ 230 tcp_now++; /* for timestamps */ 231 if (++syn_cache_last >= tcp_syn_cache_interval) { 232 syn_cache_timer(); 233 syn_cache_last = 0; 234 } 235 splx(s); 236} 237 238/* 239 * Cancel all timers for TCP tp. 240 */ 241void 242tcp_canceltimers(tp) 243 struct tcpcb *tp; 244{ 245 int i; 246 247 for (i = 0; i < TCPT_NTIMERS; i++) 248 TCP_TIMER_DISARM(tp, i); 249} 250 251int tcp_backoff[TCP_MAXRXTSHIFT + 1] = 252 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 }; 253 254int tcp_totbackoff = 511; /* sum of tcp_backoff[] */ 255 256/* 257 * TCP timer processing. 258 */ 259 260void 261tcp_timer_rexmt(void *arg) 262{ 263 struct tcpcb *tp = arg; 264 uint32_t rto; 265 int s; 266#ifdef TCP_DEBUG 267 struct socket *so; 268 short ostate; 269#endif 270 271 s = splsoftnet(); 272 273 callout_deactivate(&tp->t_timer[TCPT_REXMT]); 274 275#ifdef TCP_DEBUG 276#ifdef INET 277 if (tp->t_inpcb) 278 so = tp->t_inpcb->inp_socket; 279#endif 280#ifdef INET6 281 if (tp->t_in6pcb) 282 so = tp->t_in6pcb->in6p_socket; 283#endif 284 ostate = tp->t_state; 285#endif /* TCP_DEBUG */ 286 287 /* 288 * Retransmission timer went off. Message has not 289 * been acked within retransmit interval. Back off 290 * to a longer retransmit interval and retransmit one segment. 291 */ 292 293 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) { 294 tp->t_rxtshift = TCP_MAXRXTSHIFT; 295 tcpstat.tcps_timeoutdrop++; 296 tp = tcp_drop(tp, tp->t_softerror ? 297 tp->t_softerror : ETIMEDOUT); 298 goto out; 299 } 300 tcpstat.tcps_rexmttimeo++; 301 rto = TCP_REXMTVAL(tp); 302 if (rto < tp->t_rttmin) 303 rto = tp->t_rttmin; 304 TCPT_RANGESET(tp->t_rxtcur, rto * tcp_backoff[tp->t_rxtshift], 305 tp->t_rttmin, TCPTV_REXMTMAX); 306 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 307#if 0 308 /* 309 * If we are losing and we are trying path MTU discovery, 310 * try turning it off. This will avoid black holes in 311 * the network which suppress or fail to send "packet 312 * too big" ICMP messages. We should ideally do 313 * lots more sophisticated searching to find the right 314 * value here... 315 */ 316 if (ip_mtudisc && tp->t_rxtshift > TCP_MAXRXTSHIFT / 6) { 317 struct rtentry *rt = NULL; 318 319#ifdef INET 320 if (tp->t_inpcb) 321 rt = in_pcbrtentry(tp->t_inpcb); 322#endif 323#ifdef INET6 324 if (tp->t_in6pcb) 325 rt = in6_pcbrtentry(tp->t_in6pcb); 326#endif 327 328 /* XXX: Black hole recovery code goes here */ 329 } 330#endif /* 0 */ 331 /* 332 * If losing, let the lower level know and try for 333 * a better route. Also, if we backed off this far, 334 * our srtt estimate is probably bogus. Clobber it 335 * so we'll take the next rtt measurement as our srtt; 336 * move the current srtt into rttvar to keep the current 337 * retransmit times until then. 338 */ 339 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { 340#ifdef INET 341 if (tp->t_inpcb) 342 in_losing(tp->t_inpcb); 343#endif 344#ifdef INET6 345 if (tp->t_in6pcb) 346 in6_losing(tp->t_in6pcb); 347#endif 348 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT); 349 tp->t_srtt = 0; 350 } 351 tp->snd_nxt = tp->snd_una; 352 /* 353 * If timing a segment in this window, stop the timer. 354 */ 355 tp->t_rtttime = 0; 356 /* 357 * Remember if we are retransmitting a SYN, because if 358 * we do, set the initial congestion window must be set 359 * to 1 segment. 360 */ 361 if (tp->t_state == TCPS_SYN_SENT) 362 tp->t_flags |= TF_SYN_REXMT; 363 /* 364 * Close the congestion window down to one segment 365 * (we'll open it by one segment for each ack we get). 366 * Since we probably have a window's worth of unacked 367 * data accumulated, this "slow start" keeps us from 368 * dumping all that data as back-to-back packets (which 369 * might overwhelm an intermediate gateway). 370 * 371 * There are two phases to the opening: Initially we 372 * open by one mss on each ack. This makes the window 373 * size increase exponentially with time. If the 374 * window is larger than the path can handle, this 375 * exponential growth results in dropped packet(s) 376 * almost immediately. To get more time between 377 * drops but still "push" the network to take advantage 378 * of improving conditions, we switch from exponential 379 * to linear window opening at some threshhold size. 380 * For a threshhold, we use half the current window 381 * size, truncated to a multiple of the mss. 382 * 383 * (the minimum cwnd that will give us exponential 384 * growth is 2 mss. We don't allow the threshhold 385 * to go below this.) 386 */ 387 { 388 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_segsz; 389 if (win < 2) 390 win = 2; 391 /* Loss Window MUST be one segment. */ 392 tp->snd_cwnd = tp->t_segsz; 393 tp->snd_ssthresh = win * tp->t_segsz; 394 tp->t_dupacks = 0; 395 } 396 (void) tcp_output(tp); 397 398 out: 399#ifdef TCP_DEBUG 400 if (tp && so->so_options & SO_DEBUG) 401 tcp_trace(TA_USER, ostate, tp, NULL, 402 PRU_SLOWTIMO | (TCPT_REXMT << 8)); 403#endif 404 splx(s); 405} 406 407void 408tcp_timer_persist(void *arg) 409{ 410 struct tcpcb *tp = arg; 411 struct socket *so; 412 uint32_t rto; 413 int s; 414#ifdef TCP_DEBUG 415 short ostate; 416#endif 417 418 s = splsoftnet(); 419 420 callout_deactivate(&tp->t_timer[TCPT_PERSIST]); 421 422#ifdef INET 423 if (tp->t_inpcb) 424 so = tp->t_inpcb->inp_socket; 425#endif 426#ifdef INET6 427 if (tp->t_in6pcb) 428 so = tp->t_in6pcb->in6p_socket; 429#endif 430 431#ifdef TCP_DEBUG 432 ostate = tp->t_state; 433#endif 434 435 /* 436 * Persistance timer into zero window. 437 * Force a byte to be output, if possible. 438 */ 439 440 /* 441 * Hack: if the peer is dead/unreachable, we do not 442 * time out if the window is closed. After a full 443 * backoff, drop the connection if the idle time 444 * (no responses to probes) reaches the maximum 445 * backoff that we would use if retransmitting. 446 */ 447 rto = TCP_REXMTVAL(tp); 448 if (rto < tp->t_rttmin) 449 rto = tp->t_rttmin; 450 if (tp->t_rxtshift == TCP_MAXRXTSHIFT && 451 ((tcp_now - tp->t_rcvtime) >= tcp_maxpersistidle || 452 (tcp_now - tp->t_rcvtime) >= rto * tcp_totbackoff)) { 453 tcpstat.tcps_persistdrops++; 454 tp = tcp_drop(tp, ETIMEDOUT); 455 goto out; 456 } 457 tcpstat.tcps_persisttimeo++; 458 tcp_setpersist(tp); 459 tp->t_force = 1; 460 (void) tcp_output(tp); 461 tp->t_force = 0; 462 463 out: 464#ifdef TCP_DEBUG 465 if (tp && so->so_options & SO_DEBUG) 466 tcp_trace(TA_USER, ostate, tp, NULL, 467 PRU_SLOWTIMO | (TCPT_PERSIST << 8)); 468#endif 469 splx(s); 470} 471 472void 473tcp_timer_keep(void *arg) 474{ 475 struct tcpcb *tp = arg; 476 struct socket *so; 477 int s; 478#ifdef TCP_DEBUG 479 short ostate; 480#endif 481 482 s = splsoftnet(); 483 484 callout_deactivate(&tp->t_timer[TCPT_KEEP]); 485 486#ifdef TCP_DEBUG 487 ostate = tp->t_state; 488#endif /* TCP_DEBUG */ 489 490 /* 491 * Keep-alive timer went off; send something 492 * or drop connection if idle for too long. 493 */ 494 495 tcpstat.tcps_keeptimeo++; 496 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0) 497 goto dropit; 498#ifdef INET 499 if (tp->t_inpcb) 500 so = tp->t_inpcb->inp_socket; 501#endif 502#ifdef INET6 503 if (tp->t_in6pcb) 504 so = tp->t_in6pcb->in6p_socket; 505#endif 506 if (so->so_options & SO_KEEPALIVE && 507 tp->t_state <= TCPS_CLOSE_WAIT) { 508 if ((tcp_maxidle > 0) && 509 ((tcp_now - tp->t_rcvtime) >= 510 tcp_keepidle + tcp_maxidle)) 511 goto dropit; 512 /* 513 * Send a packet designed to force a response 514 * if the peer is up and reachable: 515 * either an ACK if the connection is still alive, 516 * or an RST if the peer has closed the connection 517 * due to timeout or reboot. 518 * Using sequence number tp->snd_una-1 519 * causes the transmitted zero-length segment 520 * to lie outside the receive window; 521 * by the protocol spec, this requires the 522 * correspondent TCP to respond. 523 */ 524 tcpstat.tcps_keepprobe++; 525 if (tcp_compat_42) { 526 /* 527 * The keepalive packet must have nonzero 528 * length to get a 4.2 host to respond. 529 */ 530 (void)tcp_respond(tp, tp->t_template, 531 (struct mbuf *)NULL, NULL, tp->rcv_nxt - 1, 532 tp->snd_una - 1, 0); 533 } else { 534 (void)tcp_respond(tp, tp->t_template, 535 (struct mbuf *)NULL, NULL, tp->rcv_nxt, 536 tp->snd_una - 1, 0); 537 } 538 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepintvl); 539 } else 540 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle); 541 542#ifdef TCP_DEBUG 543 if (tp && so->so_options & SO_DEBUG) 544 tcp_trace(TA_USER, ostate, tp, NULL, 545 PRU_SLOWTIMO | (TCPT_KEEP << 8)); 546#endif 547 splx(s); 548 return; 549 550 dropit: 551 tcpstat.tcps_keepdrops++; 552 (void) tcp_drop(tp, ETIMEDOUT); 553 splx(s); 554} 555 556void 557tcp_timer_2msl(void *arg) 558{ 559 struct tcpcb *tp = arg; 560 struct socket *so; 561 int s; 562#ifdef TCP_DEBUG 563 short ostate; 564#endif 565 566 s = splsoftnet(); 567 568 callout_deactivate(&tp->t_timer[TCPT_2MSL]); 569 570#ifdef INET 571 if (tp->t_inpcb) 572 so = tp->t_inpcb->inp_socket; 573#endif 574#ifdef INET6 575 if (tp->t_in6pcb) 576 so = tp->t_in6pcb->in6p_socket; 577#endif 578 579#ifdef TCP_DEBUG 580 ostate = tp->t_state; 581#endif 582 583 /* 584 * 2 MSL timeout in shutdown went off. If we're closed but 585 * still waiting for peer to close and connection has been idle 586 * too long, or if 2MSL time is up from TIME_WAIT, delete connection 587 * control block. Otherwise, check again in a bit. 588 */ 589 if (tp->t_state != TCPS_TIME_WAIT && 590 ((tcp_maxidle == 0) || ((tcp_now - tp->t_rcvtime) <= tcp_maxidle))) 591 TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_keepintvl); 592 else 593 tp = tcp_close(tp); 594 595#ifdef TCP_DEBUG 596 if (tp && so->so_options & SO_DEBUG) 597 tcp_trace(TA_USER, ostate, tp, NULL, 598 PRU_SLOWTIMO | (TCPT_2MSL << 8)); 599#endif 600 splx(s); 601} 602