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