xref: /csrg-svn/sys/netinet/tcp_timer.c (revision 34855)
123194Smckusick /*
229153Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
332789Sbostic  * All rights reserved.
423194Smckusick  *
532789Sbostic  * Redistribution and use in source and binary forms are permitted
6*34855Sbostic  * provided that the above copyright notice and this paragraph are
7*34855Sbostic  * duplicated in all such forms and that any documentation,
8*34855Sbostic  * advertising materials, and other materials related to such
9*34855Sbostic  * distribution and use acknowledge that the software was developed
10*34855Sbostic  * by the University of California, Berkeley.  The name of the
11*34855Sbostic  * University may not be used to endorse or promote products derived
12*34855Sbostic  * from this software without specific prior written permission.
13*34855Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34855Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34855Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1632789Sbostic  *
17*34855Sbostic  *	@(#)tcp_timer.c	7.14 (Berkeley) 06/29/88
1823194Smckusick  */
195069Swnj 
2017064Sbloom #include "param.h"
2117064Sbloom #include "systm.h"
2217064Sbloom #include "mbuf.h"
2317064Sbloom #include "socket.h"
2417064Sbloom #include "socketvar.h"
2517064Sbloom #include "protosw.h"
2617064Sbloom #include "errno.h"
278697Sroot 
288697Sroot #include "../net/if.h"
298697Sroot #include "../net/route.h"
3010896Ssam 
3117064Sbloom #include "in.h"
3217064Sbloom #include "in_pcb.h"
3317064Sbloom #include "in_systm.h"
3417064Sbloom #include "ip.h"
3517064Sbloom #include "ip_var.h"
3617064Sbloom #include "tcp.h"
3717064Sbloom #include "tcp_fsm.h"
3817064Sbloom #include "tcp_seq.h"
3917064Sbloom #include "tcp_timer.h"
4017064Sbloom #include "tcp_var.h"
4117064Sbloom #include "tcpip.h"
425069Swnj 
4333746Skarels int	tcp_keepidle = TCPTV_KEEP_IDLE;
4433746Skarels int	tcp_keepintvl = TCPTV_KEEPINTVL;
4533746Skarels int	tcp_maxidle;
465069Swnj /*
475069Swnj  * Fast timeout routine for processing delayed acks
485069Swnj  */
495069Swnj tcp_fasttimo()
505069Swnj {
515284Sroot 	register struct inpcb *inp;
525284Sroot 	register struct tcpcb *tp;
535284Sroot 	int s = splnet();
545069Swnj 
555691Swnj 	inp = tcb.inp_next;
565691Swnj 	if (inp)
575691Swnj 	for (; inp != &tcb; inp = inp->inp_next)
585284Sroot 		if ((tp = (struct tcpcb *)inp->inp_ppcb) &&
595284Sroot 		    (tp->t_flags & TF_DELACK)) {
605284Sroot 			tp->t_flags &= ~TF_DELACK;
615284Sroot 			tp->t_flags |= TF_ACKNOW;
6230524Skarels 			tcpstat.tcps_delack++;
635284Sroot 			(void) tcp_output(tp);
645284Sroot 		}
655284Sroot 	splx(s);
665069Swnj }
675069Swnj 
685069Swnj /*
695069Swnj  * Tcp protocol timeout routine called every 500 ms.
705069Swnj  * Updates the timers in all active tcb's and
715069Swnj  * causes finite state machine actions if timers expire.
725069Swnj  */
735069Swnj tcp_slowtimo()
745069Swnj {
756212Swnj 	register struct inpcb *ip, *ipnxt;
765069Swnj 	register struct tcpcb *tp;
775069Swnj 	int s = splnet();
785069Swnj 	register int i;
795069Swnj 
8033746Skarels 	tcp_maxidle = TCPTV_KEEPCNT * tcp_keepintvl;
815069Swnj 	/*
825069Swnj 	 * Search through tcb's and update active timers.
835069Swnj 	 */
845245Sroot 	ip = tcb.inp_next;
855245Sroot 	if (ip == 0) {
865245Sroot 		splx(s);
875245Sroot 		return;
885245Sroot 	}
8917315Skarels 	for (; ip != &tcb; ip = ipnxt) {
9017315Skarels 		ipnxt = ip->inp_next;
915069Swnj 		tp = intotcpcb(ip);
925260Swnj 		if (tp == 0)
935260Swnj 			continue;
945075Swnj 		for (i = 0; i < TCPT_NTIMERS; i++) {
956212Swnj 			if (tp->t_timer[i] && --tp->t_timer[i] == 0) {
965069Swnj 				(void) tcp_usrreq(tp->t_inpcb->inp_socket,
975069Swnj 				    PRU_SLOWTIMO, (struct mbuf *)0,
9812764Ssam 				    (struct mbuf *)i, (struct mbuf *)0);
996212Swnj 				if (ipnxt->inp_prev != ip)
1006212Swnj 					goto tpgone;
1016212Swnj 			}
1025069Swnj 		}
1035165Swnj 		tp->t_idle++;
1045165Swnj 		if (tp->t_rtt)
1055165Swnj 			tp->t_rtt++;
1066212Swnj tpgone:
10717315Skarels 		;
1085069Swnj 	}
1095075Swnj 	tcp_iss += TCP_ISSINCR/PR_SLOWHZ;		/* increment iss */
11027059Skarels #ifdef TCP_COMPAT_42
11126118Skarels 	if ((int)tcp_iss < 0)
11226118Skarels 		tcp_iss = 0;				/* XXX */
11326118Skarels #endif
1145069Swnj 	splx(s);
1155069Swnj }
1165069Swnj 
1175069Swnj /*
1185075Swnj  * Cancel all timers for TCP tp.
1195069Swnj  */
1205089Swnj tcp_canceltimers(tp)
1215069Swnj 	struct tcpcb *tp;
1225069Swnj {
1235069Swnj 	register int i;
1245069Swnj 
1255075Swnj 	for (i = 0; i < TCPT_NTIMERS; i++)
1265075Swnj 		tp->t_timer[i] = 0;
1275069Swnj }
1285069Swnj 
12932034Skarels int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
13032034Skarels     { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
13130762Skarels 
1325069Swnj /*
1335165Swnj  * TCP timer processing.
1345069Swnj  */
13510396Ssam struct tcpcb *
1365075Swnj tcp_timers(tp, timer)
1375069Swnj 	register struct tcpcb *tp;
1385075Swnj 	int timer;
1395069Swnj {
14025888Skarels 	register int rexmt;
1415069Swnj 
1425089Swnj 	switch (timer) {
1435069Swnj 
1445165Swnj 	/*
14524819Skarels 	 * 2 MSL timeout in shutdown went off.  If we're closed but
14624819Skarels 	 * still waiting for peer to close and connection has been idle
14724819Skarels 	 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
14824819Skarels 	 * control block.  Otherwise, check again in a bit.
1495165Swnj 	 */
1505075Swnj 	case TCPT_2MSL:
15124819Skarels 		if (tp->t_state != TCPS_TIME_WAIT &&
15233746Skarels 		    tp->t_idle <= tcp_maxidle)
15333746Skarels 			tp->t_timer[TCPT_2MSL] = tcp_keepintvl;
15424819Skarels 		else
15524819Skarels 			tp = tcp_close(tp);
15610396Ssam 		break;
1575069Swnj 
1585165Swnj 	/*
1595165Swnj 	 * Retransmission timer went off.  Message has not
1605165Swnj 	 * been acked within retransmit interval.  Back off
16117362Skarels 	 * to a longer retransmit interval and retransmit one segment.
1625165Swnj 	 */
1635075Swnj 	case TCPT_REXMT:
16432034Skarels 		if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
16532034Skarels 			tp->t_rxtshift = TCP_MAXRXTSHIFT;
16630524Skarels 			tcpstat.tcps_timeoutdrop++;
16710396Ssam 			tp = tcp_drop(tp, ETIMEDOUT);
16810396Ssam 			break;
1695691Swnj 		}
17030524Skarels 		tcpstat.tcps_rexmttimeo++;
17131726Skarels 		rexmt = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
17231726Skarels 		rexmt *= tcp_backoff[tp->t_rxtshift];
17332034Skarels 		TCPT_RANGESET(tp->t_rxtcur, rexmt, TCPTV_MIN, TCPTV_REXMTMAX);
17432034Skarels 		tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
17526989Skarels 		/*
17632034Skarels 		 * If losing, let the lower level know and try for
17732034Skarels 		 * a better route.  Also, if we backed off this far,
17832034Skarels 		 * our srtt estimate is probably bogus.  Clobber it
17932034Skarels 		 * so we'll take the next rtt measurement as our srtt;
18032034Skarels 		 * move the current srtt into rttvar to keep the current
18132034Skarels 		 * retransmit times until then.
18226989Skarels 		 */
18332034Skarels 		if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
18426989Skarels 			in_losing(tp->t_inpcb);
18532034Skarels 			tp->t_rttvar += (tp->t_srtt >> 2);
18632034Skarels 			tp->t_srtt = 0;
18732034Skarels 		}
1885165Swnj 		tp->snd_nxt = tp->snd_una;
18917314Skarels 		/*
19031726Skarels 		 * If timing a segment in this window, stop the timer.
19117314Skarels 		 */
19231726Skarels 		tp->t_rtt = 0;
19331442Skarels 		/*
19431442Skarels 		 * Close the congestion window down to one segment
19531442Skarels 		 * (we'll open it by one segment for each ack we get).
19631442Skarels 		 * Since we probably have a window's worth of unacked
19731442Skarels 		 * data accumulated, this "slow start" keeps us from
19831442Skarels 		 * dumping all that data as back-to-back packets (which
19931442Skarels 		 * might overwhelm an intermediate gateway).
20032100Skarels 		 *
20132100Skarels 		 * There are two phases to the opening: Initially we
20232100Skarels 		 * open by one mss on each ack.  This makes the window
20332100Skarels 		 * size increase exponentially with time.  If the
20432100Skarels 		 * window is larger than the path can handle, this
20532100Skarels 		 * exponential growth results in dropped packet(s)
20632100Skarels 		 * almost immediately.  To get more time between
20732100Skarels 		 * drops but still "push" the network to take advantage
20832100Skarels 		 * of improving conditions, we switch from exponential
20932100Skarels 		 * to linear window opening at some threshhold size.
21032100Skarels 		 * For a threshhold, we use half the current window
21132100Skarels 		 * size, truncated to a multiple of the mss.
21232100Skarels 		 *
21332100Skarels 		 * (the minimum cwnd that will give us exponential
21432100Skarels 		 * growth is 2 mss.  We don't allow the threshhold
21532100Skarels 		 * to go below this.)
21631442Skarels 		 */
21732100Skarels 		{
21832100Skarels 		u_int win = MIN(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
21932100Skarels 		if (win < 2)
22032100Skarels 			win = 2;
22131442Skarels 		tp->snd_cwnd = tp->t_maxseg;
22232100Skarels 		tp->snd_ssthresh = win * tp->t_maxseg;
22332100Skarels 		}
2245165Swnj 		(void) tcp_output(tp);
22510396Ssam 		break;
2265069Swnj 
2275165Swnj 	/*
2285165Swnj 	 * Persistance timer into zero window.
2295165Swnj 	 * Force a byte to be output, if possible.
2305165Swnj 	 */
2315075Swnj 	case TCPT_PERSIST:
23230524Skarels 		tcpstat.tcps_persisttimeo++;
2337156Swnj 		tcp_setpersist(tp);
2345165Swnj 		tp->t_force = 1;
2355165Swnj 		(void) tcp_output(tp);
2365165Swnj 		tp->t_force = 0;
23710396Ssam 		break;
2385069Swnj 
2395165Swnj 	/*
2405165Swnj 	 * Keep-alive timer went off; send something
2415165Swnj 	 * or drop connection if idle for too long.
2425165Swnj 	 */
2435075Swnj 	case TCPT_KEEP:
24430524Skarels 		tcpstat.tcps_keeptimeo++;
2456304Sroot 		if (tp->t_state < TCPS_ESTABLISHED)
2466304Sroot 			goto dropit;
24727193Skarels 		if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE &&
24827193Skarels 		    tp->t_state <= TCPS_CLOSE_WAIT) {
24933746Skarels 		    	if (tp->t_idle >= tcp_keepidle + tcp_maxidle)
2506304Sroot 				goto dropit;
2516267Sroot 			/*
25230524Skarels 			 * Send a packet designed to force a response
25330524Skarels 			 * if the peer is up and reachable:
25430524Skarels 			 * either an ACK if the connection is still alive,
25530524Skarels 			 * or an RST if the peer has closed the connection
25630524Skarels 			 * due to timeout or reboot.
25730524Skarels 			 * Using sequence number tp->snd_una-1
25830524Skarels 			 * causes the transmitted zero-length segment
25930524Skarels 			 * to lie outside the receive window;
26030524Skarels 			 * by the protocol spec, this requires the
26130524Skarels 			 * correspondent TCP to respond.
2626267Sroot 			 */
26330524Skarels 			tcpstat.tcps_keepprobe++;
26431727Skarels #ifdef TCP_COMPAT_42
26531727Skarels 			/*
26631727Skarels 			 * The keepalive packet must have nonzero length
26731727Skarels 			 * to get a 4.2 host to respond.
26831727Skarels 			 */
26930762Skarels 			tcp_respond(tp, tp->t_template,
27031727Skarels 			    tp->rcv_nxt - 1, tp->snd_una - 1, 0);
27131727Skarels #else
27231727Skarels 			tcp_respond(tp, tp->t_template,
27331727Skarels 			    tp->rcv_nxt, tp->snd_una - 1, 0);
27431727Skarels #endif
27533746Skarels 			tp->t_timer[TCPT_KEEP] = tcp_keepintvl;
27633746Skarels 		} else
27733746Skarels 			tp->t_timer[TCPT_KEEP] = tcp_keepidle;
27810396Ssam 		break;
2796304Sroot 	dropit:
28030524Skarels 		tcpstat.tcps_keepdrops++;
28110396Ssam 		tp = tcp_drop(tp, ETIMEDOUT);
28210396Ssam 		break;
2835069Swnj 	}
28410396Ssam 	return (tp);
2855069Swnj }
286