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