1 /* 2 * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)tcp_timer.c 7.21 (Berkeley) 02/12/93 8 */ 9 10 #ifndef TUBA_INCLUDE 11 #include <sys/param.h> 12 #include <sys/systm.h> 13 #include <sys/malloc.h> 14 #include <sys/mbuf.h> 15 #include <sys/socket.h> 16 #include <sys/socketvar.h> 17 #include <sys/protosw.h> 18 #include <sys/errno.h> 19 20 #include <net/if.h> 21 #include <net/route.h> 22 23 #include <netinet/in.h> 24 #include <netinet/in_systm.h> 25 #include <netinet/ip.h> 26 #include <netinet/in_pcb.h> 27 #include <netinet/ip_var.h> 28 #include <netinet/tcp.h> 29 #include <netinet/tcp_fsm.h> 30 #include <netinet/tcp_seq.h> 31 #include <netinet/tcp_timer.h> 32 #include <netinet/tcp_var.h> 33 #include <netinet/tcpip.h> 34 35 int tcp_keepidle = TCPTV_KEEP_IDLE; 36 int tcp_keepintvl = TCPTV_KEEPINTVL; 37 int tcp_maxidle; 38 #endif /* TUBA_INCLUDE */ 39 /* 40 * Fast timeout routine for processing delayed acks 41 */ 42 tcp_fasttimo() 43 { 44 register struct inpcb *inp; 45 register struct tcpcb *tp; 46 int s = splnet(); 47 48 inp = tcb.inp_next; 49 if (inp) 50 for (; inp != &tcb; inp = inp->inp_next) 51 if ((tp = (struct tcpcb *)inp->inp_ppcb) && 52 (tp->t_flags & TF_DELACK)) { 53 tp->t_flags &= ~TF_DELACK; 54 tp->t_flags |= TF_ACKNOW; 55 tcpstat.tcps_delack++; 56 (void) tcp_output(tp); 57 } 58 splx(s); 59 } 60 61 /* 62 * Tcp protocol timeout routine called every 500 ms. 63 * Updates the timers in all active tcb's and 64 * causes finite state machine actions if timers expire. 65 */ 66 tcp_slowtimo() 67 { 68 register struct inpcb *ip, *ipnxt; 69 register struct tcpcb *tp; 70 int s = splnet(); 71 register int i; 72 73 tcp_maxidle = TCPTV_KEEPCNT * tcp_keepintvl; 74 /* 75 * Search through tcb's and update active timers. 76 */ 77 ip = tcb.inp_next; 78 if (ip == 0) { 79 splx(s); 80 return; 81 } 82 for (; ip != &tcb; ip = ipnxt) { 83 ipnxt = ip->inp_next; 84 tp = intotcpcb(ip); 85 if (tp == 0) 86 continue; 87 for (i = 0; i < TCPT_NTIMERS; i++) { 88 if (tp->t_timer[i] && --tp->t_timer[i] == 0) { 89 (void) tcp_usrreq(tp->t_inpcb->inp_socket, 90 PRU_SLOWTIMO, (struct mbuf *)0, 91 (struct mbuf *)i, (struct mbuf *)0); 92 if (ipnxt->inp_prev != ip) 93 goto tpgone; 94 } 95 } 96 tp->t_idle++; 97 if (tp->t_rtt) 98 tp->t_rtt++; 99 tpgone: 100 ; 101 } 102 tcp_iss += TCP_ISSINCR/PR_SLOWHZ; /* increment iss */ 103 #ifdef TCP_COMPAT_42 104 if ((int)tcp_iss < 0) 105 tcp_iss = 0; /* XXX */ 106 #endif 107 tcp_now++; /* for timestamps */ 108 splx(s); 109 } 110 #ifndef TUBA_INCLUDE 111 112 /* 113 * Cancel all timers for TCP tp. 114 */ 115 tcp_canceltimers(tp) 116 struct tcpcb *tp; 117 { 118 register int i; 119 120 for (i = 0; i < TCPT_NTIMERS; i++) 121 tp->t_timer[i] = 0; 122 } 123 124 int tcp_backoff[TCP_MAXRXTSHIFT + 1] = 125 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 }; 126 127 /* 128 * TCP timer processing. 129 */ 130 struct tcpcb * 131 tcp_timers(tp, timer) 132 register struct tcpcb *tp; 133 int timer; 134 { 135 register int rexmt; 136 137 switch (timer) { 138 139 /* 140 * 2 MSL timeout in shutdown went off. If we're closed but 141 * still waiting for peer to close and connection has been idle 142 * too long, or if 2MSL time is up from TIME_WAIT, delete connection 143 * control block. Otherwise, check again in a bit. 144 */ 145 case TCPT_2MSL: 146 if (tp->t_state != TCPS_TIME_WAIT && 147 tp->t_idle <= tcp_maxidle) 148 tp->t_timer[TCPT_2MSL] = tcp_keepintvl; 149 else 150 tp = tcp_close(tp); 151 break; 152 153 /* 154 * Retransmission timer went off. Message has not 155 * been acked within retransmit interval. Back off 156 * to a longer retransmit interval and retransmit one segment. 157 */ 158 case TCPT_REXMT: 159 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) { 160 tp->t_rxtshift = TCP_MAXRXTSHIFT; 161 tcpstat.tcps_timeoutdrop++; 162 tp = tcp_drop(tp, tp->t_softerror ? 163 tp->t_softerror : ETIMEDOUT); 164 break; 165 } 166 tcpstat.tcps_rexmttimeo++; 167 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift]; 168 TCPT_RANGESET(tp->t_rxtcur, rexmt, 169 tp->t_rttmin, TCPTV_REXMTMAX); 170 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 171 /* 172 * If losing, let the lower level know and try for 173 * a better route. Also, if we backed off this far, 174 * our srtt estimate is probably bogus. Clobber it 175 * so we'll take the next rtt measurement as our srtt; 176 * move the current srtt into rttvar to keep the current 177 * retransmit times until then. 178 */ 179 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { 180 in_losing(tp->t_inpcb); 181 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT); 182 tp->t_srtt = 0; 183 } 184 tp->snd_nxt = tp->snd_una; 185 /* 186 * If timing a segment in this window, stop the timer. 187 */ 188 tp->t_rtt = 0; 189 /* 190 * Close the congestion window down to one segment 191 * (we'll open it by one segment for each ack we get). 192 * Since we probably have a window's worth of unacked 193 * data accumulated, this "slow start" keeps us from 194 * dumping all that data as back-to-back packets (which 195 * might overwhelm an intermediate gateway). 196 * 197 * There are two phases to the opening: Initially we 198 * open by one mss on each ack. This makes the window 199 * size increase exponentially with time. If the 200 * window is larger than the path can handle, this 201 * exponential growth results in dropped packet(s) 202 * almost immediately. To get more time between 203 * drops but still "push" the network to take advantage 204 * of improving conditions, we switch from exponential 205 * to linear window opening at some threshhold size. 206 * For a threshhold, we use half the current window 207 * size, truncated to a multiple of the mss. 208 * 209 * (the minimum cwnd that will give us exponential 210 * growth is 2 mss. We don't allow the threshhold 211 * to go below this.) 212 */ 213 { 214 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg; 215 if (win < 2) 216 win = 2; 217 tp->snd_cwnd = tp->t_maxseg; 218 tp->snd_ssthresh = win * tp->t_maxseg; 219 tp->t_dupacks = 0; 220 } 221 (void) tcp_output(tp); 222 break; 223 224 /* 225 * Persistance timer into zero window. 226 * Force a byte to be output, if possible. 227 */ 228 case TCPT_PERSIST: 229 tcpstat.tcps_persisttimeo++; 230 tcp_setpersist(tp); 231 tp->t_force = 1; 232 (void) tcp_output(tp); 233 tp->t_force = 0; 234 break; 235 236 /* 237 * Keep-alive timer went off; send something 238 * or drop connection if idle for too long. 239 */ 240 case TCPT_KEEP: 241 tcpstat.tcps_keeptimeo++; 242 if (tp->t_state < TCPS_ESTABLISHED) 243 goto dropit; 244 if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE && 245 tp->t_state <= TCPS_CLOSE_WAIT) { 246 if (tp->t_idle >= tcp_keepidle + tcp_maxidle) 247 goto dropit; 248 /* 249 * Send a packet designed to force a response 250 * if the peer is up and reachable: 251 * either an ACK if the connection is still alive, 252 * or an RST if the peer has closed the connection 253 * due to timeout or reboot. 254 * Using sequence number tp->snd_una-1 255 * causes the transmitted zero-length segment 256 * to lie outside the receive window; 257 * by the protocol spec, this requires the 258 * correspondent TCP to respond. 259 */ 260 tcpstat.tcps_keepprobe++; 261 #ifdef TCP_COMPAT_42 262 /* 263 * The keepalive packet must have nonzero length 264 * to get a 4.2 host to respond. 265 */ 266 tcp_respond(tp, tp->t_template, (struct mbuf *)NULL, 267 tp->rcv_nxt - 1, tp->snd_una - 1, 0); 268 #else 269 tcp_respond(tp, tp->t_template, (struct mbuf *)NULL, 270 tp->rcv_nxt, tp->snd_una - 1, 0); 271 #endif 272 tp->t_timer[TCPT_KEEP] = tcp_keepintvl; 273 } else 274 tp->t_timer[TCPT_KEEP] = tcp_keepidle; 275 break; 276 dropit: 277 tcpstat.tcps_keepdrops++; 278 tp = tcp_drop(tp, ETIMEDOUT); 279 break; 280 } 281 return (tp); 282 } 283 #endif /* TUBA_INCLUDE */ 284