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
|
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 90int tcp_keepinit; 91SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW, 92 &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", ""); 93 94int tcp_keepidle; 95SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW, 96 &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", ""); 97 98int tcp_keepintvl; 99SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW, 100 &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", ""); 101 102int tcp_delacktime; 103SYSCTL_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 107int tcp_msl; 108SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW, 109 &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime"); 110 111static int always_keepalive = 0; 112SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW, 113 &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections"); 114 115static int tcp_keepcnt = TCPTV_KEEPCNT; 116 /* max idle probes */ 117int tcp_maxpersistidle; 118 /* max idle time in persist */ 119int 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 */ 126void 127tcp_slowtimo() 128{ 129 int s; 130 131 s = splnet(); 132 133 tcp_maxidle = tcp_keepcnt * tcp_keepintvl; 134 135 tcp_iss += TCP_ISSINCR/PR_SLOWHZ; /* increment iss */ 136#ifdef TCP_COMPAT_42 137 if ((int)tcp_iss < 0) 138 tcp_iss = TCP_ISSINCR; /* XXX */ 139#endif 140 splx(s); 141} 142 143/* 144 * Cancel all timers for TCP tp. 145 */ 146void 147tcp_canceltimers(tp) 148 struct tcpcb *tp; 149{ 150 callout_stop(tp->tt_2msl); 151 callout_stop(tp->tt_persist); 152 callout_stop(tp->tt_keep); 153 callout_stop(tp->tt_rexmt); 154} 155 156int tcp_backoff[TCP_MAXRXTSHIFT + 1] = 157 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 }; 158 159static int tcp_totbackoff = 511; /* sum of tcp_backoff[] */ 160 161/* 162 * TCP timer processing. 163 */ 164void 165tcp_timer_delack(xtp) 166 void *xtp; 167{ 168 struct tcpcb *tp = xtp; 169 int s; 170 171 s = splnet(); 172 if (callout_pending(tp->tt_delack)) { 173 splx(s); 174 return; 175 } 176 callout_deactivate(tp->tt_delack); 177 178 tp->t_flags |= TF_ACKNOW; 179 tcpstat.tcps_delack++; 180 (void) tcp_output(tp); 181 splx(s); 182} 183 184void 185tcp_timer_2msl(xtp) 186 void *xtp; 187{ 188 struct tcpcb *tp = xtp; 189 int s; 190#ifdef TCPDEBUG 191 int ostate; 192 193 ostate = tp->t_state; 194#endif 195 s = splnet(); 196 if (callout_pending(tp->tt_2msl)) { 197 splx(s); 198 return; 199 } 200 callout_deactivate(tp->tt_2msl); 201 /* 202 * 2 MSL timeout in shutdown went off. If we're closed but 203 * still waiting for peer to close and connection has been idle 204 * too long, or if 2MSL time is up from TIME_WAIT, delete connection 205 * control block. Otherwise, check again in a bit. 206 */ 207 if (tp->t_state != TCPS_TIME_WAIT && 208 (ticks - tp->t_rcvtime) <= tcp_maxidle) 209 callout_reset(tp->tt_2msl, tcp_keepintvl, 210 tcp_timer_2msl, tp); 211 else 212 tp = tcp_close(tp); 213 214#ifdef TCPDEBUG 215 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 216 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0, 217 PRU_SLOWTIMO); 218#endif 219 splx(s); 220} 221 222void 223tcp_timer_keep(xtp) 224 void *xtp; 225{ 226 struct tcpcb *tp = xtp; 227 int s; 228#ifdef TCPDEBUG 229 int ostate; 230 231 ostate = tp->t_state; 232#endif 233 s = splnet(); 234 if (callout_pending(tp->tt_keep)) { 235 splx(s); 236 return; 237 } 238 callout_deactivate(tp->tt_keep); 239 /* 240 * Keep-alive timer went off; send something 241 * or drop connection if idle for too long. 242 */ 243 tcpstat.tcps_keeptimeo++; 244 if (tp->t_state < TCPS_ESTABLISHED) 245 goto dropit; 246 if ((always_keepalive || 247 tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) && 248 tp->t_state <= TCPS_CLOSING) { 249 if ((ticks - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle) 250 goto dropit; 251 /* 252 * Send a packet designed to force a response 253 * if the peer is up and reachable: 254 * either an ACK if the connection is still alive, 255 * or an RST if the peer has closed the connection 256 * due to timeout or reboot. 257 * Using sequence number tp->snd_una-1 258 * causes the transmitted zero-length segment 259 * to lie outside the receive window; 260 * by the protocol spec, this requires the 261 * correspondent TCP to respond. 262 */ 263 tcpstat.tcps_keepprobe++; 264#ifdef TCP_COMPAT_42 265 /* 266 * The keepalive packet must have nonzero length 267 * to get a 4.2 host to respond. 268 */ 269 tcp_respond(tp, tp->t_template->tt_ipgen, 270 &tp->t_template->tt_t, (struct mbuf *)NULL, 271 tp->rcv_nxt - 1, tp->snd_una - 1, 0); 272#else 273 tcp_respond(tp, tp->t_template->tt_ipgen, 274 &tp->t_template->tt_t, (struct mbuf *)NULL, 275 tp->rcv_nxt, tp->snd_una - 1, 0); 276#endif 277 callout_reset(tp->tt_keep, tcp_keepintvl, tcp_timer_keep, tp); 278 } else 279 callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp); 280 281#ifdef TCPDEBUG 282 if (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 return; 288 289dropit: 290 tcpstat.tcps_keepdrops++; 291 tp = tcp_drop(tp, ETIMEDOUT); 292 293#ifdef TCPDEBUG 294 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 295 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0, 296 PRU_SLOWTIMO); 297#endif 298 splx(s); 299} 300 301void 302tcp_timer_persist(xtp) 303 void *xtp; 304{ 305 struct tcpcb *tp = xtp; 306 int s; 307#ifdef TCPDEBUG 308 int ostate; 309 310 ostate = tp->t_state; 311#endif 312 s = splnet(); 313 if (callout_pending(tp->tt_persist)) { 314 splx(s); 315 return; 316 } 317 callout_deactivate(tp->tt_persist); 318 /* 319 * Persistance timer into zero window. 320 * Force a byte to be output, if possible. 321 */ 322 tcpstat.tcps_persisttimeo++; 323 /* 324 * Hack: if the peer is dead/unreachable, we do not 325 * time out if the window is closed. After a full 326 * backoff, drop the connection if the idle time 327 * (no responses to probes) reaches the maximum 328 * backoff that we would use if retransmitting. 329 */ 330 if (tp->t_rxtshift == TCP_MAXRXTSHIFT && 331 ((ticks - tp->t_rcvtime) >= tcp_maxpersistidle || 332 (ticks - tp->t_rcvtime) >= TCP_REXMTVAL(tp) * tcp_totbackoff)) { 333 tcpstat.tcps_persistdrop++; 334 tp = tcp_drop(tp, ETIMEDOUT); 335 goto out; 336 } 337 tcp_setpersist(tp); 338 tp->t_force = 1; 339 (void) tcp_output(tp); 340 tp->t_force = 0; 341 342out: 343#ifdef TCPDEBUG 344 if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 345 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0, 346 PRU_SLOWTIMO); 347#endif 348 splx(s); 349} 350 351void 352tcp_timer_rexmt(xtp) 353 void *xtp; 354{ 355 struct tcpcb *tp = xtp; 356 int s; 357 int rexmt; 358#ifdef TCPDEBUG 359 int ostate; 360 361 ostate = tp->t_state; 362#endif 363 s = splnet(); 364 if (callout_pending(tp->tt_rexmt)) { 365 splx(s); 366 return; 367 } 368 callout_deactivate(tp->tt_rexmt); 369 /* 370 * Retransmission timer went off. Message has not 371 * been acked within retransmit interval. Back off 372 * to a longer retransmit interval and retransmit one segment. 373 */ 374 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) { 375 tp->t_rxtshift = TCP_MAXRXTSHIFT; 376 tcpstat.tcps_timeoutdrop++; 377 tp = tcp_drop(tp, tp->t_softerror ? 378 tp->t_softerror : ETIMEDOUT); 379 goto out; 380 } 381 if (tp->t_rxtshift == 1) { 382 /* 383 * first retransmit; record ssthresh and cwnd so they can 384 * be recovered if this turns out to be a "bad" retransmit. 385 * A retransmit is considered "bad" if an ACK for this 386 * segment is received within RTT/2 interval; the assumption 387 * here is that the ACK was already in flight. See 388 * "On Estimating End-to-End Network Path Properties" by 389 * Allman and Paxson for more details. 390 */ 391 tp->snd_cwnd_prev = tp->snd_cwnd; 392 tp->snd_ssthresh_prev = tp->snd_ssthresh; 393 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1)); 394 } 395 tcpstat.tcps_rexmttimeo++; 396 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift]; 397 TCPT_RANGESET(tp->t_rxtcur, rexmt, 398 tp->t_rttmin, TCPTV_REXMTMAX); 399 /* 400 * If losing, let the lower level know and try for 401 * a better route. Also, if we backed off this far, 402 * our srtt estimate is probably bogus. Clobber it 403 * so we'll take the next rtt measurement as our srtt; 404 * move the current srtt into rttvar to keep the current 405 * retransmit times until then. 406 */ 407 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { 408#ifdef INET6 409 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) 410 in6_losing(tp->t_inpcb); 411 else 412#endif 413 in_losing(tp->t_inpcb); 414 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT); 415 tp->t_srtt = 0; 416 } 417 tp->snd_nxt = tp->snd_una; 418 /* 419 * Note: We overload snd_recover to function also as the 420 * snd_last variable described in RFC 2582 421 */ 422 tp->snd_recover = tp->snd_max; 423 /* 424 * Force a segment to be sent. 425 */ 426 tp->t_flags |= TF_ACKNOW; 427 /* 428 * If timing a segment in this window, stop the timer. 429 */ 430 tp->t_rtttime = 0; 431 /* 432 * Close the congestion window down to one segment 433 * (we'll open it by one segment for each ack we get). 434 * Since we probably have a window's worth of unacked 435 * data accumulated, this "slow start" keeps us from 436 * dumping all that data as back-to-back packets (which 437 * might overwhelm an intermediate gateway). 438 * 439 * There are two phases to the opening: Initially we 440 * open by one mss on each ack. This makes the window 441 * size increase exponentially with time. If the 442 * window is larger than the path can handle, this 443 * exponential growth results in dropped packet(s) 444 * almost immediately. To get more time between 445 * drops but still "push" the network to take advantage 446 * of improving conditions, we switch from exponential 447 * to linear window opening at some threshhold size. 448 * For a threshhold, we use half the current window 449 * size, truncated to a multiple of the mss. 450 * 451 * (the minimum cwnd that will give us exponential 452 * growth is 2 mss. We don't allow the threshhold 453 * to go below this.) 454 */ 455 { 456 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg; 457 if (win < 2) 458 win = 2; 459 tp->snd_cwnd = tp->t_maxseg; 460 tp->snd_ssthresh = win * tp->t_maxseg; 461 tp->t_dupacks = 0; 462 } 463 (void) tcp_output(tp); 464 465out: 466#ifdef TCPDEBUG 467 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 468 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0, 469 PRU_SLOWTIMO); 470#endif 471 splx(s); 472}
|