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