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