xref: /csrg-svn/sys/netinet/tcp_timer.c (revision 57947)
123194Smckusick /*
244379Skarels  * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California.
332789Sbostic  * All rights reserved.
423194Smckusick  *
544489Sbostic  * %sccs.include.redist.c%
632789Sbostic  *
7*57947Ssklower  *	@(#)tcp_timer.c	7.21 (Berkeley) 02/12/93
823194Smckusick  */
95069Swnj 
10*57947Ssklower #ifndef TUBA_INCLUDE
1156531Sbostic #include <sys/param.h>
1256531Sbostic #include <sys/systm.h>
1356531Sbostic #include <sys/malloc.h>
1456531Sbostic #include <sys/mbuf.h>
1556531Sbostic #include <sys/socket.h>
1656531Sbostic #include <sys/socketvar.h>
1756531Sbostic #include <sys/protosw.h>
1856531Sbostic #include <sys/errno.h>
198697Sroot 
2056531Sbostic #include <net/if.h>
2156531Sbostic #include <net/route.h>
2210896Ssam 
2356531Sbostic #include <netinet/in.h>
2456531Sbostic #include <netinet/in_systm.h>
2556531Sbostic #include <netinet/ip.h>
2656531Sbostic #include <netinet/in_pcb.h>
2756531Sbostic #include <netinet/ip_var.h>
2856531Sbostic #include <netinet/tcp.h>
2956531Sbostic #include <netinet/tcp_fsm.h>
3056531Sbostic #include <netinet/tcp_seq.h>
3156531Sbostic #include <netinet/tcp_timer.h>
3256531Sbostic #include <netinet/tcp_var.h>
3356531Sbostic #include <netinet/tcpip.h>
345069Swnj 
3533746Skarels int	tcp_keepidle = TCPTV_KEEP_IDLE;
3633746Skarels int	tcp_keepintvl = TCPTV_KEEPINTVL;
3733746Skarels int	tcp_maxidle;
38*57947Ssklower #endif /* TUBA_INCLUDE */
395069Swnj /*
405069Swnj  * Fast timeout routine for processing delayed acks
415069Swnj  */
425069Swnj tcp_fasttimo()
435069Swnj {
445284Sroot 	register struct inpcb *inp;
455284Sroot 	register struct tcpcb *tp;
465284Sroot 	int s = splnet();
475069Swnj 
485691Swnj 	inp = tcb.inp_next;
495691Swnj 	if (inp)
505691Swnj 	for (; inp != &tcb; inp = inp->inp_next)
515284Sroot 		if ((tp = (struct tcpcb *)inp->inp_ppcb) &&
525284Sroot 		    (tp->t_flags & TF_DELACK)) {
535284Sroot 			tp->t_flags &= ~TF_DELACK;
545284Sroot 			tp->t_flags |= TF_ACKNOW;
5530524Skarels 			tcpstat.tcps_delack++;
565284Sroot 			(void) tcp_output(tp);
575284Sroot 		}
585284Sroot 	splx(s);
595069Swnj }
605069Swnj 
615069Swnj /*
625069Swnj  * Tcp protocol timeout routine called every 500 ms.
635069Swnj  * Updates the timers in all active tcb's and
645069Swnj  * causes finite state machine actions if timers expire.
655069Swnj  */
665069Swnj tcp_slowtimo()
675069Swnj {
686212Swnj 	register struct inpcb *ip, *ipnxt;
695069Swnj 	register struct tcpcb *tp;
705069Swnj 	int s = splnet();
715069Swnj 	register int i;
725069Swnj 
7333746Skarels 	tcp_maxidle = TCPTV_KEEPCNT * tcp_keepintvl;
745069Swnj 	/*
755069Swnj 	 * Search through tcb's and update active timers.
765069Swnj 	 */
775245Sroot 	ip = tcb.inp_next;
785245Sroot 	if (ip == 0) {
795245Sroot 		splx(s);
805245Sroot 		return;
815245Sroot 	}
8217315Skarels 	for (; ip != &tcb; ip = ipnxt) {
8317315Skarels 		ipnxt = ip->inp_next;
845069Swnj 		tp = intotcpcb(ip);
855260Swnj 		if (tp == 0)
865260Swnj 			continue;
875075Swnj 		for (i = 0; i < TCPT_NTIMERS; i++) {
886212Swnj 			if (tp->t_timer[i] && --tp->t_timer[i] == 0) {
895069Swnj 				(void) tcp_usrreq(tp->t_inpcb->inp_socket,
905069Swnj 				    PRU_SLOWTIMO, (struct mbuf *)0,
9112764Ssam 				    (struct mbuf *)i, (struct mbuf *)0);
926212Swnj 				if (ipnxt->inp_prev != ip)
936212Swnj 					goto tpgone;
946212Swnj 			}
955069Swnj 		}
965165Swnj 		tp->t_idle++;
975165Swnj 		if (tp->t_rtt)
985165Swnj 			tp->t_rtt++;
996212Swnj tpgone:
10017315Skarels 		;
1015069Swnj 	}
1025075Swnj 	tcp_iss += TCP_ISSINCR/PR_SLOWHZ;		/* increment iss */
10327059Skarels #ifdef TCP_COMPAT_42
10426118Skarels 	if ((int)tcp_iss < 0)
10526118Skarels 		tcp_iss = 0;				/* XXX */
10626118Skarels #endif
10757433Sandrew 	tcp_now++;					/* for timestamps */
1085069Swnj 	splx(s);
1095069Swnj }
110*57947Ssklower #ifndef TUBA_INCLUDE
1115069Swnj 
1125069Swnj /*
1135075Swnj  * Cancel all timers for TCP tp.
1145069Swnj  */
1155089Swnj tcp_canceltimers(tp)
1165069Swnj 	struct tcpcb *tp;
1175069Swnj {
1185069Swnj 	register int i;
1195069Swnj 
1205075Swnj 	for (i = 0; i < TCPT_NTIMERS; i++)
1215075Swnj 		tp->t_timer[i] = 0;
1225069Swnj }
1235069Swnj 
12432034Skarels int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
12532034Skarels     { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
12630762Skarels 
1275069Swnj /*
1285165Swnj  * TCP timer processing.
1295069Swnj  */
13010396Ssam struct tcpcb *
1315075Swnj tcp_timers(tp, timer)
1325069Swnj 	register struct tcpcb *tp;
1335075Swnj 	int timer;
1345069Swnj {
13525888Skarels 	register int rexmt;
1365069Swnj 
1375089Swnj 	switch (timer) {
1385069Swnj 
1395165Swnj 	/*
14024819Skarels 	 * 2 MSL timeout in shutdown went off.  If we're closed but
14124819Skarels 	 * still waiting for peer to close and connection has been idle
14224819Skarels 	 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
14324819Skarels 	 * control block.  Otherwise, check again in a bit.
1445165Swnj 	 */
1455075Swnj 	case TCPT_2MSL:
14624819Skarels 		if (tp->t_state != TCPS_TIME_WAIT &&
14733746Skarels 		    tp->t_idle <= tcp_maxidle)
14833746Skarels 			tp->t_timer[TCPT_2MSL] = tcp_keepintvl;
14924819Skarels 		else
15024819Skarels 			tp = tcp_close(tp);
15110396Ssam 		break;
1525069Swnj 
1535165Swnj 	/*
1545165Swnj 	 * Retransmission timer went off.  Message has not
1555165Swnj 	 * been acked within retransmit interval.  Back off
15617362Skarels 	 * to a longer retransmit interval and retransmit one segment.
1575165Swnj 	 */
1585075Swnj 	case TCPT_REXMT:
15932034Skarels 		if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
16032034Skarels 			tp->t_rxtshift = TCP_MAXRXTSHIFT;
16130524Skarels 			tcpstat.tcps_timeoutdrop++;
16244379Skarels 			tp = tcp_drop(tp, tp->t_softerror ?
16344379Skarels 			    tp->t_softerror : ETIMEDOUT);
16410396Ssam 			break;
1655691Swnj 		}
16630524Skarels 		tcpstat.tcps_rexmttimeo++;
16744379Skarels 		rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
16844379Skarels 		TCPT_RANGESET(tp->t_rxtcur, rexmt,
16944379Skarels 		    tp->t_rttmin, TCPTV_REXMTMAX);
17032034Skarels 		tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
17126989Skarels 		/*
17232034Skarels 		 * If losing, let the lower level know and try for
17332034Skarels 		 * a better route.  Also, if we backed off this far,
17432034Skarels 		 * our srtt estimate is probably bogus.  Clobber it
17532034Skarels 		 * so we'll take the next rtt measurement as our srtt;
17632034Skarels 		 * move the current srtt into rttvar to keep the current
17732034Skarels 		 * retransmit times until then.
17826989Skarels 		 */
17932034Skarels 		if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
18026989Skarels 			in_losing(tp->t_inpcb);
18144379Skarels 			tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
18232034Skarels 			tp->t_srtt = 0;
18332034Skarels 		}
1845165Swnj 		tp->snd_nxt = tp->snd_una;
18517314Skarels 		/*
18631726Skarels 		 * If timing a segment in this window, stop the timer.
18717314Skarels 		 */
18831726Skarels 		tp->t_rtt = 0;
18931442Skarels 		/*
19031442Skarels 		 * Close the congestion window down to one segment
19131442Skarels 		 * (we'll open it by one segment for each ack we get).
19231442Skarels 		 * Since we probably have a window's worth of unacked
19331442Skarels 		 * data accumulated, this "slow start" keeps us from
19431442Skarels 		 * dumping all that data as back-to-back packets (which
19531442Skarels 		 * might overwhelm an intermediate gateway).
19632100Skarels 		 *
19732100Skarels 		 * There are two phases to the opening: Initially we
19832100Skarels 		 * open by one mss on each ack.  This makes the window
19932100Skarels 		 * size increase exponentially with time.  If the
20032100Skarels 		 * window is larger than the path can handle, this
20132100Skarels 		 * exponential growth results in dropped packet(s)
20232100Skarels 		 * almost immediately.  To get more time between
20332100Skarels 		 * drops but still "push" the network to take advantage
20432100Skarels 		 * of improving conditions, we switch from exponential
20532100Skarels 		 * to linear window opening at some threshhold size.
20632100Skarels 		 * For a threshhold, we use half the current window
20732100Skarels 		 * size, truncated to a multiple of the mss.
20832100Skarels 		 *
20932100Skarels 		 * (the minimum cwnd that will give us exponential
21032100Skarels 		 * growth is 2 mss.  We don't allow the threshhold
21132100Skarels 		 * to go below this.)
21231442Skarels 		 */
21332100Skarels 		{
21444379Skarels 		u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
21532100Skarels 		if (win < 2)
21632100Skarels 			win = 2;
21731442Skarels 		tp->snd_cwnd = tp->t_maxseg;
21832100Skarels 		tp->snd_ssthresh = win * tp->t_maxseg;
21944379Skarels 		tp->t_dupacks = 0;
22032100Skarels 		}
2215165Swnj 		(void) tcp_output(tp);
22210396Ssam 		break;
2235069Swnj 
2245165Swnj 	/*
2255165Swnj 	 * Persistance timer into zero window.
2265165Swnj 	 * Force a byte to be output, if possible.
2275165Swnj 	 */
2285075Swnj 	case TCPT_PERSIST:
22930524Skarels 		tcpstat.tcps_persisttimeo++;
2307156Swnj 		tcp_setpersist(tp);
2315165Swnj 		tp->t_force = 1;
2325165Swnj 		(void) tcp_output(tp);
2335165Swnj 		tp->t_force = 0;
23410396Ssam 		break;
2355069Swnj 
2365165Swnj 	/*
2375165Swnj 	 * Keep-alive timer went off; send something
2385165Swnj 	 * or drop connection if idle for too long.
2395165Swnj 	 */
2405075Swnj 	case TCPT_KEEP:
24130524Skarels 		tcpstat.tcps_keeptimeo++;
2426304Sroot 		if (tp->t_state < TCPS_ESTABLISHED)
2436304Sroot 			goto dropit;
24427193Skarels 		if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE &&
24527193Skarels 		    tp->t_state <= TCPS_CLOSE_WAIT) {
24633746Skarels 		    	if (tp->t_idle >= tcp_keepidle + tcp_maxidle)
2476304Sroot 				goto dropit;
2486267Sroot 			/*
24930524Skarels 			 * Send a packet designed to force a response
25030524Skarels 			 * if the peer is up and reachable:
25130524Skarels 			 * either an ACK if the connection is still alive,
25230524Skarels 			 * or an RST if the peer has closed the connection
25330524Skarels 			 * due to timeout or reboot.
25430524Skarels 			 * Using sequence number tp->snd_una-1
25530524Skarels 			 * causes the transmitted zero-length segment
25630524Skarels 			 * to lie outside the receive window;
25730524Skarels 			 * by the protocol spec, this requires the
25830524Skarels 			 * correspondent TCP to respond.
2596267Sroot 			 */
26030524Skarels 			tcpstat.tcps_keepprobe++;
26131727Skarels #ifdef TCP_COMPAT_42
26231727Skarels 			/*
26331727Skarels 			 * The keepalive packet must have nonzero length
26431727Skarels 			 * to get a 4.2 host to respond.
26531727Skarels 			 */
26637321Skarels 			tcp_respond(tp, tp->t_template, (struct mbuf *)NULL,
26731727Skarels 			    tp->rcv_nxt - 1, tp->snd_una - 1, 0);
26831727Skarels #else
26937321Skarels 			tcp_respond(tp, tp->t_template, (struct mbuf *)NULL,
27031727Skarels 			    tp->rcv_nxt, tp->snd_una - 1, 0);
27131727Skarels #endif
27233746Skarels 			tp->t_timer[TCPT_KEEP] = tcp_keepintvl;
27333746Skarels 		} else
27433746Skarels 			tp->t_timer[TCPT_KEEP] = tcp_keepidle;
27510396Ssam 		break;
2766304Sroot 	dropit:
27730524Skarels 		tcpstat.tcps_keepdrops++;
27810396Ssam 		tp = tcp_drop(tp, ETIMEDOUT);
27910396Ssam 		break;
2805069Swnj 	}
28110396Ssam 	return (tp);
2825069Swnj }
283*57947Ssklower #endif /* TUBA_INCLUDE */
284