tcp_timer.c revision 1.34
1/* $NetBSD: tcp_timer.c,v 1.34 1998/04/29 05:16:47 thorpej Exp $ */ 2 3/*- 4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation 9 * Facility, NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40/* 41 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 42 * The Regents of the University of California. All rights reserved. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. All advertising materials mentioning features or use of this software 53 * must display the following acknowledgement: 54 * This product includes software developed by the University of 55 * California, Berkeley and its contributors. 56 * 4. Neither the name of the University nor the names of its contributors 57 * may be used to endorse or promote products derived from this software 58 * without specific prior written permission. 59 * 60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 70 * SUCH DAMAGE. 71 * 72 * @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95 73 */ 74 75#ifndef TUBA_INCLUDE 76#include <sys/param.h> 77#include <sys/systm.h> 78#include <sys/malloc.h> 79#include <sys/mbuf.h> 80#include <sys/socket.h> 81#include <sys/socketvar.h> 82#include <sys/protosw.h> 83#include <sys/errno.h> 84 85#include <net/if.h> 86#include <net/route.h> 87 88#include <netinet/in.h> 89#include <netinet/in_systm.h> 90#include <netinet/ip.h> 91#include <netinet/in_pcb.h> 92#include <netinet/ip_var.h> 93#include <netinet/tcp.h> 94#include <netinet/tcp_fsm.h> 95#include <netinet/tcp_seq.h> 96#include <netinet/tcp_timer.h> 97#include <netinet/tcp_var.h> 98#include <netinet/tcpip.h> 99 100int tcp_keepidle = TCPTV_KEEP_IDLE; 101int tcp_keepintvl = TCPTV_KEEPINTVL; 102int tcp_keepcnt = TCPTV_KEEPCNT; /* max idle probes */ 103int tcp_maxpersistidle = TCPTV_KEEP_IDLE; /* max idle time in persist */ 104int tcp_maxidle; 105#else /* TUBA_INCLUDE */ 106 107extern int tcp_keepcnt; 108extern int tcp_maxpersistidle; 109#endif /* TUBA_INCLUDE */ 110 111struct tcp_delack_head tcp_delacks; 112 113/* 114 * Fast timeout routine for processing delayed acks 115 */ 116void 117tcp_fasttimo() 118{ 119 register struct tcpcb *tp, *ntp; 120 int s; 121 122 s = splsoftnet(); 123 for (tp = tcp_delacks.lh_first; tp != NULL; tp = ntp) { 124 /* 125 * If tcp_output() can't transmit the ACK for whatever 126 * reason, it will remain on the queue for the next 127 * time the heartbeat ticks. 128 */ 129 ntp = tp->t_delack.le_next; 130 tp->t_flags |= TF_ACKNOW; 131 (void) tcp_output(tp); 132 } 133 splx(s); 134} 135 136/* 137 * Tcp protocol timeout routine called every 500 ms. 138 * Updates the timers in all active tcb's and 139 * causes finite state machine actions if timers expire. 140 */ 141void 142tcp_slowtimo() 143{ 144 register struct inpcb *inp, *ninp; 145 register struct tcpcb *tp; 146 int s; 147 register long i; 148 static int syn_cache_last = 0; 149 150 s = splsoftnet(); 151 tcp_maxidle = tcp_keepcnt * tcp_keepintvl; 152 /* 153 * Search through tcb's and update active timers. 154 */ 155 inp = tcbtable.inpt_queue.cqh_first; 156 if (inp == (struct inpcb *)0) { /* XXX */ 157 splx(s); 158 return; 159 } 160 for (; inp != (struct inpcb *)&tcbtable.inpt_queue; inp = ninp) { 161 ninp = inp->inp_queue.cqe_next; 162 tp = intotcpcb(inp); 163 if (tp == 0 || tp->t_state == TCPS_LISTEN) 164 continue; 165 for (i = 0; i < TCPT_NTIMERS; i++) { 166 if (tp->t_timer[i] && --tp->t_timer[i] == 0) { 167 (void) tcp_usrreq(tp->t_inpcb->inp_socket, 168 PRU_SLOWTIMO, (struct mbuf *)0, 169 (struct mbuf *)i, (struct mbuf *)0, 170 (struct proc *)0); 171 /* XXX NOT MP SAFE */ 172 if ((ninp == (void *)&tcbtable.inpt_queue && 173 tcbtable.inpt_queue.cqh_last != inp) || 174 ninp->inp_queue.cqe_prev != inp) 175 goto tpgone; 176 } 177 } 178 tp->t_idle++; 179 if (tp->t_rtt) 180 tp->t_rtt++; 181tpgone: 182 ; 183 } 184#if NRND == 0 /* Do we need to do this when using random() ? */ 185 tcp_iss_seq += TCP_ISSINCR; /* increment iss */ 186 if (tcp_compat_42) 187 if ((int)tcp_iss_seq < 0) 188 tcp_iss_seq = 0; /* XXX */ 189#endif 190 tcp_now++; /* for timestamps */ 191 if (++syn_cache_last >= tcp_syn_cache_interval) { 192 syn_cache_timer(syn_cache_last); 193 syn_cache_last = 0; 194 } 195 splx(s); 196} 197#ifndef TUBA_INCLUDE 198 199/* 200 * Cancel all timers for TCP tp. 201 */ 202void 203tcp_canceltimers(tp) 204 struct tcpcb *tp; 205{ 206 register int i; 207 208 for (i = 0; i < TCPT_NTIMERS; i++) 209 tp->t_timer[i] = 0; 210} 211 212int tcp_backoff[TCP_MAXRXTSHIFT + 1] = 213 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 }; 214 215int tcp_totbackoff = 511; /* sum of tcp_backoff[] */ 216 217/* 218 * TCP timer processing. 219 */ 220struct tcpcb * 221tcp_timers(tp, timer) 222 register struct tcpcb *tp; 223 int timer; 224{ 225 short rto; 226 227 switch (timer) { 228 229 /* 230 * 2 MSL timeout in shutdown went off. If we're closed but 231 * still waiting for peer to close and connection has been idle 232 * too long, or if 2MSL time is up from TIME_WAIT, delete connection 233 * control block. Otherwise, check again in a bit. 234 */ 235 case TCPT_2MSL: 236 if (tp->t_state != TCPS_TIME_WAIT && 237 tp->t_idle <= tcp_maxidle) 238 tp->t_timer[TCPT_2MSL] = tcp_keepintvl; 239 else 240 tp = tcp_close(tp); 241 break; 242 243 /* 244 * Retransmission timer went off. Message has not 245 * been acked within retransmit interval. Back off 246 * to a longer retransmit interval and retransmit one segment. 247 */ 248 case TCPT_REXMT: 249 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) { 250 tp->t_rxtshift = TCP_MAXRXTSHIFT; 251 tcpstat.tcps_timeoutdrop++; 252 tp = tcp_drop(tp, tp->t_softerror ? 253 tp->t_softerror : ETIMEDOUT); 254 break; 255 } 256 tcpstat.tcps_rexmttimeo++; 257 rto = TCP_REXMTVAL(tp); 258 if (rto < tp->t_rttmin) 259 rto = tp->t_rttmin; 260 TCPT_RANGESET(tp->t_rxtcur, rto * tcp_backoff[tp->t_rxtshift], 261 tp->t_rttmin, TCPTV_REXMTMAX); 262 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 263 /* 264 * If we are losing and we are trying path MTU discovery, 265 * try turning it off. This will avoid black holes in 266 * the network which suppress or fail to send "packet 267 * too big" ICMP messages. We should ideally do 268 * lots more sophisticated searching to find the right 269 * value here... 270 */ 271 if (ip_mtudisc && tp->t_rxtshift > TCP_MAXRXTSHIFT / 6) { 272 struct inpcb *inp = tp->t_inpcb; 273 struct rtentry *rt = in_pcbrtentry(inp); 274 275 rt->rt_rmx.rmx_locks |= RTV_MTU; 276 } 277 /* 278 * If losing, let the lower level know and try for 279 * a better route. Also, if we backed off this far, 280 * our srtt estimate is probably bogus. Clobber it 281 * so we'll take the next rtt measurement as our srtt; 282 * move the current srtt into rttvar to keep the current 283 * retransmit times until then. 284 */ 285 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { 286 in_losing(tp->t_inpcb); 287 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT); 288 tp->t_srtt = 0; 289 } 290 tp->snd_nxt = tp->snd_una; 291 /* 292 * If timing a segment in this window, stop the timer. 293 */ 294 tp->t_rtt = 0; 295 /* 296 * Remember if we are retransmitting a SYN, because if 297 * we do, set the initial congestion window must be set 298 * to 1 segment. 299 */ 300 if (tp->t_state == TCPS_SYN_SENT) 301 tp->t_flags |= TF_SYN_REXMT; 302 /* 303 * Close the congestion window down to the initial window 304 * (we'll open it by one segment for each ack we get). 305 * Since we probably have a window's worth of unacked 306 * data accumulated, this "slow start" keeps us from 307 * dumping all that data as back-to-back packets (which 308 * might overwhelm an intermediate gateway). 309 * 310 * There are two phases to the opening: Initially we 311 * open by one mss on each ack. This makes the window 312 * size increase exponentially with time. If the 313 * window is larger than the path can handle, this 314 * exponential growth results in dropped packet(s) 315 * almost immediately. To get more time between 316 * drops but still "push" the network to take advantage 317 * of improving conditions, we switch from exponential 318 * to linear window opening at some threshhold size. 319 * For a threshhold, we use half the current window 320 * size, truncated to a multiple of the mss. 321 * 322 * (the minimum cwnd that will give us exponential 323 * growth is 2 mss. We don't allow the threshhold 324 * to go below this.) 325 */ 326 { 327 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_segsz; 328 if (win < 2) 329 win = 2; 330 tp->snd_cwnd = TCP_INITIAL_WINDOW(tcp_init_win, tp->t_segsz); 331 tp->snd_ssthresh = win * tp->t_segsz; 332 tp->t_dupacks = 0; 333 } 334 (void) tcp_output(tp); 335 break; 336 337 /* 338 * Persistance timer into zero window. 339 * Force a byte to be output, if possible. 340 */ 341 case TCPT_PERSIST: 342 /* 343 * Hack: if the peer is dead/unreachable, we do not 344 * time out if the window is closed. After a full 345 * backoff, drop the connection if the idle time 346 * (no responses to probes) reaches the maximum 347 * backoff that we would use if retransmitting. 348 */ 349 rto = TCP_REXMTVAL(tp); 350 if (rto < tp->t_rttmin) 351 rto = tp->t_rttmin; 352 if (tp->t_rxtshift == TCP_MAXRXTSHIFT && 353 (tp->t_idle >= tcp_maxpersistidle || 354 tp->t_idle >= rto * tcp_totbackoff)) { 355 tcpstat.tcps_persistdrops++; 356 tp = tcp_drop(tp, ETIMEDOUT); 357 break; 358 } 359 tcpstat.tcps_persisttimeo++; 360 tcp_setpersist(tp); 361 tp->t_force = 1; 362 (void) tcp_output(tp); 363 tp->t_force = 0; 364 break; 365 366 /* 367 * Keep-alive timer went off; send something 368 * or drop connection if idle for too long. 369 */ 370 case TCPT_KEEP: 371 tcpstat.tcps_keeptimeo++; 372 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0) 373 goto dropit; 374 if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE && 375 tp->t_state <= TCPS_CLOSE_WAIT) { 376 if (tp->t_idle >= tcp_keepidle + tcp_maxidle) 377 goto dropit; 378 /* 379 * Send a packet designed to force a response 380 * if the peer is up and reachable: 381 * either an ACK if the connection is still alive, 382 * or an RST if the peer has closed the connection 383 * due to timeout or reboot. 384 * Using sequence number tp->snd_una-1 385 * causes the transmitted zero-length segment 386 * to lie outside the receive window; 387 * by the protocol spec, this requires the 388 * correspondent TCP to respond. 389 */ 390 tcpstat.tcps_keepprobe++; 391 if (tcp_compat_42) { 392 /* 393 * The keepalive packet must have nonzero 394 * length to get a 4.2 host to respond. 395 */ 396 (void)tcp_respond(tp, tp->t_template, 397 (struct mbuf *)NULL, tp->rcv_nxt - 1, 398 tp->snd_una - 1, 0); 399 } else { 400 (void)tcp_respond(tp, tp->t_template, 401 (struct mbuf *)NULL, tp->rcv_nxt, 402 tp->snd_una - 1, 0); 403 } 404 tp->t_timer[TCPT_KEEP] = tcp_keepintvl; 405 } else 406 tp->t_timer[TCPT_KEEP] = tcp_keepidle; 407 break; 408 dropit: 409 tcpstat.tcps_keepdrops++; 410 tp = tcp_drop(tp, ETIMEDOUT); 411 break; 412 } 413 return (tp); 414} 415#endif /* TUBA_INCLUDE */ 416