xref: /csrg-svn/sys/netinet/tcp_timer.c (revision 7156)
1*7156Swnj /*	tcp_timer.c	4.22	82/06/12	*/
25069Swnj 
35069Swnj #include "../h/param.h"
45069Swnj #include "../h/systm.h"
55069Swnj #include "../h/mbuf.h"
65069Swnj #include "../h/socket.h"
75069Swnj #include "../h/socketvar.h"
85069Swnj #include "../h/protosw.h"
95089Swnj #include "../net/in.h"
106353Ssam #include "../net/route.h"
115089Swnj #include "../net/in_pcb.h"
125089Swnj #include "../net/in_systm.h"
135069Swnj #include "../net/if.h"
145069Swnj #include "../net/ip.h"
155069Swnj #include "../net/ip_var.h"
165069Swnj #include "../net/tcp.h"
175069Swnj #include "../net/tcp_fsm.h"
185089Swnj #include "../net/tcp_seq.h"
195089Swnj #include "../net/tcp_timer.h"
205069Swnj #include "../net/tcp_var.h"
215089Swnj #include "../net/tcpip.h"
225112Swnj #include "../errno.h"
235069Swnj 
245442Swnj int	tcpnodelack = 0;
255069Swnj /*
265069Swnj  * Fast timeout routine for processing delayed acks
275069Swnj  */
285069Swnj tcp_fasttimo()
295069Swnj {
305284Sroot 	register struct inpcb *inp;
315284Sroot 	register struct tcpcb *tp;
325284Sroot 	int s = splnet();
335284Sroot COUNT(TCP_FASTTIMO);
345069Swnj 
355691Swnj 	inp = tcb.inp_next;
365691Swnj 	if (inp)
375691Swnj 	for (; inp != &tcb; inp = inp->inp_next)
385284Sroot 		if ((tp = (struct tcpcb *)inp->inp_ppcb) &&
395284Sroot 		    (tp->t_flags & TF_DELACK)) {
405284Sroot 			tp->t_flags &= ~TF_DELACK;
415284Sroot 			tp->t_flags |= TF_ACKNOW;
425284Sroot 			(void) tcp_output(tp);
435284Sroot 		}
445284Sroot 	splx(s);
455069Swnj }
465069Swnj 
475069Swnj /*
485069Swnj  * Tcp protocol timeout routine called every 500 ms.
495069Swnj  * Updates the timers in all active tcb's and
505069Swnj  * causes finite state machine actions if timers expire.
515069Swnj  */
525069Swnj tcp_slowtimo()
535069Swnj {
546212Swnj 	register struct inpcb *ip, *ipnxt;
555069Swnj 	register struct tcpcb *tp;
565069Swnj 	int s = splnet();
575069Swnj 	register int i;
585089Swnj COUNT(TCP_SLOWTIMO);
595069Swnj 
605069Swnj 	/*
615069Swnj 	 * Search through tcb's and update active timers.
625069Swnj 	 */
635245Sroot 	ip = tcb.inp_next;
645245Sroot 	if (ip == 0) {
655245Sroot 		splx(s);
665245Sroot 		return;
675245Sroot 	}
686212Swnj 	while (ip != &tcb) {
695069Swnj 		tp = intotcpcb(ip);
705260Swnj 		if (tp == 0)
715260Swnj 			continue;
726212Swnj 		ipnxt = ip->inp_next;
735075Swnj 		for (i = 0; i < TCPT_NTIMERS; i++) {
746212Swnj 			if (tp->t_timer[i] && --tp->t_timer[i] == 0) {
755069Swnj 				(void) tcp_usrreq(tp->t_inpcb->inp_socket,
765069Swnj 				    PRU_SLOWTIMO, (struct mbuf *)0,
775069Swnj 				    (caddr_t)i);
786212Swnj 				if (ipnxt->inp_prev != ip)
796212Swnj 					goto tpgone;
806212Swnj 			}
815069Swnj 		}
825165Swnj 		tp->t_idle++;
835165Swnj 		if (tp->t_rtt)
845165Swnj 			tp->t_rtt++;
856212Swnj tpgone:
866212Swnj 		ip = ipnxt;
875069Swnj 	}
885075Swnj 	tcp_iss += TCP_ISSINCR/PR_SLOWHZ;		/* increment iss */
895069Swnj 	splx(s);
905069Swnj }
915069Swnj 
925069Swnj /*
935075Swnj  * Cancel all timers for TCP tp.
945069Swnj  */
955089Swnj tcp_canceltimers(tp)
965069Swnj 	struct tcpcb *tp;
975069Swnj {
985069Swnj 	register int i;
995069Swnj 
1005089Swnj COUNT(TCP_CANCELTIMERS);
1015075Swnj 	for (i = 0; i < TCPT_NTIMERS; i++)
1025075Swnj 		tp->t_timer[i] = 0;
1035069Swnj }
1045069Swnj 
1056474Sroot float	tcp_backoff[TCP_MAXRXTSHIFT] =
1066474Sroot     { 1.0, 1.2, 1.4, 1.7, 2.0, 3.0, 5.0, 8.0, 16.0, 32.0 };
1076474Sroot int	tcprexmtprint = 0;
1086474Sroot int	tcpexprexmtbackoff = 0;
1095069Swnj /*
1105165Swnj  * TCP timer processing.
1115069Swnj  */
1125075Swnj tcp_timers(tp, timer)
1135069Swnj 	register struct tcpcb *tp;
1145075Swnj 	int timer;
1155069Swnj {
1165069Swnj 
1175069Swnj COUNT(TCP_TIMERS);
1185089Swnj 	switch (timer) {
1195069Swnj 
1205165Swnj 	/*
1215165Swnj 	 * 2 MSL timeout in shutdown went off.  Delete connection
1225165Swnj 	 * control block.
1235165Swnj 	 */
1245075Swnj 	case TCPT_2MSL:
1255075Swnj 		tcp_close(tp);
1265075Swnj 		return;
1275069Swnj 
1285165Swnj 	/*
1295165Swnj 	 * Retransmission timer went off.  Message has not
1305165Swnj 	 * been acked within retransmit interval.  Back off
1315165Swnj 	 * to a longer retransmit interval and retransmit all
1325165Swnj 	 * unacknowledged messages in the window.
1335165Swnj 	 */
1345075Swnj 	case TCPT_REXMT:
1355165Swnj 		tp->t_rxtshift++;
1365691Swnj 		if (tp->t_rxtshift > TCP_MAXRXTSHIFT) {
1375691Swnj 			tcp_drop(tp, ETIMEDOUT);
1385691Swnj 			return;
1395691Swnj 		}
1405165Swnj 		TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
1415952Swnj 		    (int)tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
1426474Sroot 		if (tcpexprexmtbackoff) {
1436474Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
1446474Sroot 			    tp->t_timer[TCPT_REXMT] << tp->t_rxtshift,
1456474Sroot 			    TCPTV_MIN, TCPTV_MAX);
1466474Sroot 		} else {
1476474Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
1486474Sroot 			    tp->t_timer[TCPT_REXMT] *
1496474Sroot 			        tcp_backoff[tp->t_rxtshift - 1],
1506474Sroot 			    TCPTV_MIN, TCPTV_MAX);
1516474Sroot 		}
1525952Swnj if (tcprexmtprint)
1535952Swnj printf("rexmt set to %d\n", tp->t_timer[TCPT_REXMT]);
1545165Swnj 		tp->snd_nxt = tp->snd_una;
1555165Swnj 		/* this only transmits one segment! */
1565165Swnj 		(void) tcp_output(tp);
1575075Swnj 		return;
1585069Swnj 
1595165Swnj 	/*
1605165Swnj 	 * Persistance timer into zero window.
1615165Swnj 	 * Force a byte to be output, if possible.
1625165Swnj 	 */
1635075Swnj 	case TCPT_PERSIST:
164*7156Swnj 		tcp_setpersist(tp);
1655165Swnj 		tp->t_force = 1;
1665165Swnj 		(void) tcp_output(tp);
1675165Swnj 		tp->t_force = 0;
1685089Swnj 		return;
1695069Swnj 
1705165Swnj 	/*
1715165Swnj 	 * Keep-alive timer went off; send something
1725165Swnj 	 * or drop connection if idle for too long.
1735165Swnj 	 */
1745075Swnj 	case TCPT_KEEP:
1756304Sroot 		if (tp->t_state < TCPS_ESTABLISHED)
1766304Sroot 			goto dropit;
1776267Sroot 		if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) {
1786304Sroot 		    	if (tp->t_idle >= TCPTV_MAXIDLE)
1796304Sroot 				goto dropit;
1806267Sroot 			/*
1816267Sroot 			 * Saying tp->rcv_nxt-1 lies about what
1826267Sroot 			 * we have received, and by the protocol spec
1836267Sroot 			 * requires the correspondent TCP to respond.
1846267Sroot 			 * Saying tp->snd_una-1 causes the transmitted
1856267Sroot 			 * byte to lie outside the receive window; this
1866267Sroot 			 * is important because we don't necessarily
1876267Sroot 			 * have a byte in the window to send (consider
1886267Sroot 			 * a one-way stream!)
1896267Sroot 			 */
1905392Swnj 			tcp_respond(tp,
1916267Sroot 			    tp->t_template, tp->rcv_nxt-1, tp->snd_una-1, 0);
1926267Sroot 		} else
1936212Swnj 			tp->t_idle = 0;
1945165Swnj 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
1955075Swnj 		return;
1966304Sroot 	dropit:
1976304Sroot 		tcp_drop(tp, ETIMEDOUT);
1986304Sroot 		return;
1995442Swnj 
2005442Swnj #ifdef TCPTRUEOOB
2015442Swnj 	/*
2025442Swnj 	 * Out-of-band data retransmit timer.
2035442Swnj 	 */
2045442Swnj 	case TCPT_OOBREXMT:
2055442Swnj 		if (tp->t_flags & TF_NOOPT)
2065442Swnj 			return;
2075442Swnj 		(void) tcp_output(tp);
2085442Swnj 		TCPT_RANGESET(tp->t_timer[TCPT_OOBREXMT],
2095442Swnj 		    2 * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
2105442Swnj 		return;
2115442Swnj #endif
2125069Swnj 	}
2135069Swnj }
214