1 /* 2 * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved. 3 * Copyright (c) 2003, 2004 The DragonFly Project. All rights reserved. 4 * 5 * This code is derived from software contributed to The DragonFly Project 6 * by Jeffrey M. Hsu. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of The DragonFly Project nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific, prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 /* 35 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 36 * The Regents of the University of California. All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by the University of 49 * California, Berkeley and its contributors. 50 * 4. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 * 66 * @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95 67 * $FreeBSD: src/sys/netinet/tcp_timer.c,v 1.34.2.14 2003/02/03 02:33:41 hsu Exp $ 68 * $DragonFly: src/sys/netinet/tcp_timer.c,v 1.17 2008/03/30 20:39:01 dillon Exp $ 69 */ 70 71 #include "opt_compat.h" 72 #include "opt_inet6.h" 73 #include "opt_tcpdebug.h" 74 75 #include <sys/param.h> 76 #include <sys/systm.h> 77 #include <sys/kernel.h> 78 #include <sys/mbuf.h> 79 #include <sys/sysctl.h> 80 #include <sys/socket.h> 81 #include <sys/socketvar.h> 82 #include <sys/protosw.h> 83 #include <sys/thread.h> 84 #include <sys/globaldata.h> 85 #include <sys/thread2.h> 86 87 #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 88 89 #include <net/route.h> 90 91 #include <netinet/in.h> 92 #include <netinet/in_systm.h> 93 #include <netinet/in_pcb.h> 94 #ifdef INET6 95 #include <netinet6/in6_pcb.h> 96 #endif 97 #include <netinet/ip_var.h> 98 #include <netinet/tcp.h> 99 #include <netinet/tcp_fsm.h> 100 #include <netinet/tcp_seq.h> 101 #include <netinet/tcp_timer.h> 102 #include <netinet/tcp_var.h> 103 #include <netinet/tcpip.h> 104 #ifdef TCPDEBUG 105 #include <netinet/tcp_debug.h> 106 #endif 107 108 static int 109 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS) 110 { 111 int error, s, tt; 112 113 tt = *(int *)oidp->oid_arg1; 114 s = (int)((int64_t)tt * 1000 / hz); 115 116 error = sysctl_handle_int(oidp, &s, 0, req); 117 if (error || !req->newptr) 118 return (error); 119 120 tt = (int)((int64_t)s * hz / 1000); 121 if (tt < 1) 122 return (EINVAL); 123 124 *(int *)oidp->oid_arg1 = tt; 125 return (0); 126 } 127 128 int tcp_keepinit; 129 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW, 130 &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", ""); 131 132 int tcp_keepidle; 133 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW, 134 &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", ""); 135 136 int tcp_keepintvl; 137 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW, 138 &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", ""); 139 140 int tcp_delacktime; 141 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime, 142 CTLTYPE_INT|CTLFLAG_RW, &tcp_delacktime, 0, sysctl_msec_to_ticks, "I", 143 "Time before a delayed ACK is sent"); 144 145 int tcp_msl; 146 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW, 147 &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime"); 148 149 int tcp_rexmit_min; 150 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW, 151 &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I", "Minimum Retransmission Timeout"); 152 153 int tcp_rexmit_slop; 154 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW, 155 &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I", 156 "Retransmission Timer Slop"); 157 158 static int always_keepalive = 1; 159 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW, 160 &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections"); 161 162 static int tcp_keepcnt = TCPTV_KEEPCNT; 163 /* max idle probes */ 164 int tcp_maxpersistidle; 165 /* max idle time in persist */ 166 int tcp_maxidle; 167 168 /* 169 * Tcp protocol timeout routine called every 500 ms. 170 * Updates timestamps used for TCP 171 * causes finite state machine actions if timers expire. 172 */ 173 void 174 tcp_slowtimo(void) 175 { 176 crit_enter(); 177 tcp_maxidle = tcp_keepcnt * tcp_keepintvl; 178 crit_exit(); 179 } 180 181 /* 182 * Cancel all timers for TCP tp. 183 */ 184 void 185 tcp_canceltimers(struct tcpcb *tp) 186 { 187 callout_stop(tp->tt_2msl); 188 callout_stop(tp->tt_persist); 189 callout_stop(tp->tt_keep); 190 callout_stop(tp->tt_rexmt); 191 } 192 193 int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] = 194 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 }; 195 196 int tcp_backoff[TCP_MAXRXTSHIFT + 1] = 197 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 }; 198 199 static int tcp_totbackoff = 511; /* sum of tcp_backoff[] */ 200 201 /* 202 * TCP timer processing. 203 */ 204 void 205 tcp_timer_delack(void *xtp) 206 { 207 struct tcpcb *tp = xtp; 208 209 crit_enter(); 210 if (callout_pending(tp->tt_delack) || !callout_active(tp->tt_delack)) { 211 crit_exit(); 212 return; 213 } 214 callout_deactivate(tp->tt_delack); 215 216 tp->t_flags |= TF_ACKNOW; 217 tcpstat.tcps_delack++; 218 tcp_output(tp); 219 crit_exit(); 220 } 221 222 void 223 tcp_timer_2msl(void *xtp) 224 { 225 struct tcpcb *tp = xtp; 226 #ifdef TCPDEBUG 227 int ostate; 228 229 ostate = tp->t_state; 230 #endif 231 crit_enter(); 232 if (callout_pending(tp->tt_2msl) || !callout_active(tp->tt_2msl)) { 233 crit_exit(); 234 return; 235 } 236 callout_deactivate(tp->tt_2msl); 237 /* 238 * 2 MSL timeout in shutdown went off. If we're closed but 239 * still waiting for peer to close and connection has been idle 240 * too long, or if 2MSL time is up from TIME_WAIT, delete connection 241 * control block. Otherwise, check again in a bit. 242 */ 243 if (tp->t_state != TCPS_TIME_WAIT && 244 (ticks - tp->t_rcvtime) <= tcp_maxidle) 245 callout_reset(tp->tt_2msl, tcp_keepintvl, 246 tcp_timer_2msl, tp); 247 else 248 tp = tcp_close(tp); 249 250 #ifdef TCPDEBUG 251 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 252 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO); 253 #endif 254 crit_exit(); 255 } 256 257 void 258 tcp_timer_keep(void *xtp) 259 { 260 struct tcpcb *tp = xtp; 261 struct tcptemp *t_template; 262 #ifdef TCPDEBUG 263 int ostate; 264 265 ostate = tp->t_state; 266 #endif 267 crit_enter(); 268 if (callout_pending(tp->tt_keep) || !callout_active(tp->tt_keep)) { 269 crit_exit(); 270 return; 271 } 272 callout_deactivate(tp->tt_keep); 273 /* 274 * Keep-alive timer went off; send something 275 * or drop connection if idle for too long. 276 */ 277 tcpstat.tcps_keeptimeo++; 278 if (tp->t_state < TCPS_ESTABLISHED) 279 goto dropit; 280 if ((always_keepalive || 281 tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) && 282 tp->t_state <= TCPS_CLOSING) { 283 if ((ticks - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle) 284 goto dropit; 285 /* 286 * Send a packet designed to force a response 287 * if the peer is up and reachable: 288 * either an ACK if the connection is still alive, 289 * or an RST if the peer has closed the connection 290 * due to timeout or reboot. 291 * Using sequence number tp->snd_una-1 292 * causes the transmitted zero-length segment 293 * to lie outside the receive window; 294 * by the protocol spec, this requires the 295 * correspondent TCP to respond. 296 */ 297 tcpstat.tcps_keepprobe++; 298 t_template = tcp_maketemplate(tp); 299 if (t_template) { 300 tcp_respond(tp, t_template->tt_ipgen, 301 &t_template->tt_t, (struct mbuf *)NULL, 302 tp->rcv_nxt, tp->snd_una - 1, 0); 303 tcp_freetemplate(t_template); 304 } 305 callout_reset(tp->tt_keep, tcp_keepintvl, tcp_timer_keep, tp); 306 } else 307 callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp); 308 309 #ifdef TCPDEBUG 310 if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 311 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO); 312 #endif 313 crit_exit(); 314 return; 315 316 dropit: 317 tcpstat.tcps_keepdrops++; 318 tp = tcp_drop(tp, ETIMEDOUT); 319 320 #ifdef TCPDEBUG 321 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 322 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO); 323 #endif 324 crit_exit(); 325 } 326 327 void 328 tcp_timer_persist(void *xtp) 329 { 330 struct tcpcb *tp = xtp; 331 #ifdef TCPDEBUG 332 int ostate; 333 334 ostate = tp->t_state; 335 #endif 336 crit_enter(); 337 if (callout_pending(tp->tt_persist) || !callout_active(tp->tt_persist)){ 338 crit_exit(); 339 return; 340 } 341 callout_deactivate(tp->tt_persist); 342 /* 343 * Persistance timer into zero window. 344 * Force a byte to be output, if possible. 345 */ 346 tcpstat.tcps_persisttimeo++; 347 /* 348 * Hack: if the peer is dead/unreachable, we do not 349 * time out if the window is closed. After a full 350 * backoff, drop the connection if the idle time 351 * (no responses to probes) reaches the maximum 352 * backoff that we would use if retransmitting. 353 */ 354 if (tp->t_rxtshift == TCP_MAXRXTSHIFT && 355 ((ticks - tp->t_rcvtime) >= tcp_maxpersistidle || 356 (ticks - tp->t_rcvtime) >= TCP_REXMTVAL(tp) * tcp_totbackoff)) { 357 tcpstat.tcps_persistdrop++; 358 tp = tcp_drop(tp, ETIMEDOUT); 359 goto out; 360 } 361 tcp_setpersist(tp); 362 tp->t_flags |= TF_FORCE; 363 tcp_output(tp); 364 tp->t_flags &= ~TF_FORCE; 365 366 out: 367 #ifdef TCPDEBUG 368 if (tp && tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 369 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO); 370 #endif 371 crit_exit(); 372 } 373 374 void 375 tcp_save_congestion_state(struct tcpcb *tp) 376 { 377 tp->snd_cwnd_prev = tp->snd_cwnd; 378 tp->snd_wacked_prev = tp->snd_wacked; 379 tp->snd_ssthresh_prev = tp->snd_ssthresh; 380 tp->snd_recover_prev = tp->snd_recover; 381 if (IN_FASTRECOVERY(tp)) 382 tp->t_flags |= TF_WASFRECOVERY; 383 else 384 tp->t_flags &= ~TF_WASFRECOVERY; 385 if (tp->t_flags & TF_RCVD_TSTMP) { 386 tp->t_rexmtTS = ticks; 387 tp->t_flags |= TF_FIRSTACCACK; 388 } 389 #ifdef later 390 tcp_sack_save_scoreboard(&tp->scb); 391 #endif 392 } 393 394 void 395 tcp_revert_congestion_state(struct tcpcb *tp) 396 { 397 tp->snd_cwnd = tp->snd_cwnd_prev; 398 tp->snd_wacked = tp->snd_wacked_prev; 399 tp->snd_ssthresh = tp->snd_ssthresh_prev; 400 tp->snd_recover = tp->snd_recover_prev; 401 if (tp->t_flags & TF_WASFRECOVERY) 402 ENTER_FASTRECOVERY(tp); 403 if (tp->t_flags & TF_FASTREXMT) { 404 ++tcpstat.tcps_sndfastrexmitbad; 405 if (tp->t_flags & TF_EARLYREXMT) 406 ++tcpstat.tcps_sndearlyrexmitbad; 407 } else 408 ++tcpstat.tcps_sndrtobad; 409 tp->t_badrxtwin = 0; 410 tp->t_rxtshift = 0; 411 tp->snd_nxt = tp->snd_max; 412 #ifdef later 413 tcp_sack_revert_scoreboard(&tp->scb, tp->snd_una); 414 #endif 415 } 416 417 void 418 tcp_timer_rexmt(void *xtp) 419 { 420 struct tcpcb *tp = xtp; 421 int rexmt; 422 #ifdef TCPDEBUG 423 int ostate; 424 425 ostate = tp->t_state; 426 #endif 427 crit_enter(); 428 if (callout_pending(tp->tt_rexmt) || !callout_active(tp->tt_rexmt)) { 429 crit_exit(); 430 return; 431 } 432 callout_deactivate(tp->tt_rexmt); 433 /* 434 * Retransmission timer went off. Message has not 435 * been acked within retransmit interval. Back off 436 * to a longer retransmit interval and retransmit one segment. 437 */ 438 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) { 439 tp->t_rxtshift = TCP_MAXRXTSHIFT; 440 tcpstat.tcps_timeoutdrop++; 441 tp = tcp_drop(tp, tp->t_softerror ? 442 tp->t_softerror : ETIMEDOUT); 443 goto out; 444 } 445 if (tp->t_rxtshift == 1) { 446 /* 447 * first retransmit; record ssthresh and cwnd so they can 448 * be recovered if this turns out to be a "bad" retransmit. 449 * A retransmit is considered "bad" if an ACK for this 450 * segment is received within RTT/2 interval; the assumption 451 * here is that the ACK was already in flight. See 452 * "On Estimating End-to-End Network Path Properties" by 453 * Allman and Paxson for more details. 454 */ 455 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1)); 456 tcp_save_congestion_state(tp); 457 tp->t_flags &= ~(TF_FASTREXMT | TF_EARLYREXMT); 458 } 459 /* Throw away SACK blocks on a RTO, as specified by RFC2018. */ 460 tcp_sack_cleanup(&tp->scb); 461 tcpstat.tcps_rexmttimeo++; 462 if (tp->t_state == TCPS_SYN_SENT) 463 rexmt = TCP_REXMTVAL(tp) * tcp_syn_backoff[tp->t_rxtshift]; 464 else 465 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift]; 466 TCPT_RANGESET(tp->t_rxtcur, rexmt, 467 tp->t_rttmin, TCPTV_REXMTMAX); 468 /* 469 * Disable rfc1323 and rfc1644 if we havn't got any response to 470 * our third SYN to work-around some broken terminal servers 471 * (most of which have hopefully been retired) that have bad VJ 472 * header compression code which trashes TCP segments containing 473 * unknown-to-them TCP options. 474 */ 475 if ((tp->t_state == TCPS_SYN_SENT) && (tp->t_rxtshift == 3)) 476 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_REQ_CC); 477 /* 478 * If losing, let the lower level know and try for 479 * a better route. Also, if we backed off this far, 480 * our srtt estimate is probably bogus. Clobber it 481 * so we'll take the next rtt measurement as our srtt; 482 * move the current srtt into rttvar to keep the current 483 * retransmit times until then. 484 */ 485 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { 486 #ifdef INET6 487 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) 488 in6_losing(tp->t_inpcb); 489 else 490 #endif 491 in_losing(tp->t_inpcb); 492 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT); 493 tp->t_srtt = 0; 494 } 495 tp->snd_nxt = tp->snd_una; 496 tp->rexmt_high = tp->snd_una; 497 tp->snd_recover = tp->snd_max; 498 /* 499 * Force a segment to be sent. 500 */ 501 tp->t_flags |= TF_ACKNOW; 502 /* 503 * If timing a segment in this window, stop the timer. 504 */ 505 tp->t_rtttime = 0; 506 /* 507 * Close the congestion window down to one segment 508 * (we'll open it by one segment for each ack we get). 509 * Since we probably have a window's worth of unacked 510 * data accumulated, this "slow start" keeps us from 511 * dumping all that data as back-to-back packets (which 512 * might overwhelm an intermediate gateway). 513 * 514 * There are two phases to the opening: Initially we 515 * open by one mss on each ack. This makes the window 516 * size increase exponentially with time. If the 517 * window is larger than the path can handle, this 518 * exponential growth results in dropped packet(s) 519 * almost immediately. To get more time between 520 * drops but still "push" the network to take advantage 521 * of improving conditions, we switch from exponential 522 * to linear window opening at some threshhold size. 523 * For a threshhold, we use half the current window 524 * size, truncated to a multiple of the mss. 525 * 526 * (the minimum cwnd that will give us exponential 527 * growth is 2 mss. We don't allow the threshhold 528 * to go below this.) 529 */ 530 { 531 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg; 532 533 if (win < 2) 534 win = 2; 535 tp->snd_cwnd = tp->t_maxseg; 536 tp->snd_wacked = 0; 537 tp->snd_ssthresh = win * tp->t_maxseg; 538 tp->t_dupacks = 0; 539 } 540 EXIT_FASTRECOVERY(tp); 541 tcp_output(tp); 542 543 out: 544 #ifdef TCPDEBUG 545 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 546 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO); 547 #endif 548 crit_exit(); 549 } 550