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