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