xref: /csrg-svn/sys/netinet/tcp_timer.c (revision 17362)
1*17362Skarels /*	tcp_timer.c	6.5	84/11/14	*/
25069Swnj 
317064Sbloom #include "param.h"
417064Sbloom #include "systm.h"
517064Sbloom #include "mbuf.h"
617064Sbloom #include "socket.h"
717064Sbloom #include "socketvar.h"
817064Sbloom #include "protosw.h"
917064Sbloom #include "errno.h"
108697Sroot 
118697Sroot #include "../net/if.h"
128697Sroot #include "../net/route.h"
1310896Ssam 
1417064Sbloom #include "in.h"
1517064Sbloom #include "in_pcb.h"
1617064Sbloom #include "in_systm.h"
1717064Sbloom #include "ip.h"
1817064Sbloom #include "ip_var.h"
1917064Sbloom #include "tcp.h"
2017064Sbloom #include "tcp_fsm.h"
2117064Sbloom #include "tcp_seq.h"
2217064Sbloom #include "tcp_timer.h"
2317064Sbloom #include "tcp_var.h"
2417064Sbloom #include "tcpip.h"
255069Swnj 
265442Swnj int	tcpnodelack = 0;
275069Swnj /*
285069Swnj  * Fast timeout routine for processing delayed acks
295069Swnj  */
305069Swnj tcp_fasttimo()
315069Swnj {
325284Sroot 	register struct inpcb *inp;
335284Sroot 	register struct tcpcb *tp;
345284Sroot 	int s = splnet();
355069Swnj 
365691Swnj 	inp = tcb.inp_next;
375691Swnj 	if (inp)
385691Swnj 	for (; inp != &tcb; inp = inp->inp_next)
395284Sroot 		if ((tp = (struct tcpcb *)inp->inp_ppcb) &&
405284Sroot 		    (tp->t_flags & TF_DELACK)) {
415284Sroot 			tp->t_flags &= ~TF_DELACK;
425284Sroot 			tp->t_flags |= TF_ACKNOW;
435284Sroot 			(void) tcp_output(tp);
445284Sroot 		}
455284Sroot 	splx(s);
465069Swnj }
475069Swnj 
485069Swnj /*
495069Swnj  * Tcp protocol timeout routine called every 500 ms.
505069Swnj  * Updates the timers in all active tcb's and
515069Swnj  * causes finite state machine actions if timers expire.
525069Swnj  */
535069Swnj tcp_slowtimo()
545069Swnj {
556212Swnj 	register struct inpcb *ip, *ipnxt;
565069Swnj 	register struct tcpcb *tp;
575069Swnj 	int s = splnet();
585069Swnj 	register int i;
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 	}
6817315Skarels 	for (; ip != &tcb; ip = ipnxt) {
6917315Skarels 		ipnxt = ip->inp_next;
705069Swnj 		tp = intotcpcb(ip);
715260Swnj 		if (tp == 0)
725260Swnj 			continue;
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,
7712764Ssam 				    (struct mbuf *)i, (struct mbuf *)0);
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:
8617315Skarels 		;
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 
1005075Swnj 	for (i = 0; i < TCPT_NTIMERS; i++)
1015075Swnj 		tp->t_timer[i] = 0;
1025069Swnj }
1035069Swnj 
1046474Sroot float	tcp_backoff[TCP_MAXRXTSHIFT] =
1056474Sroot     { 1.0, 1.2, 1.4, 1.7, 2.0, 3.0, 5.0, 8.0, 16.0, 32.0 };
1066474Sroot int	tcpexprexmtbackoff = 0;
1075069Swnj /*
1085165Swnj  * TCP timer processing.
1095069Swnj  */
11010396Ssam struct tcpcb *
1115075Swnj tcp_timers(tp, timer)
1125069Swnj 	register struct tcpcb *tp;
1135075Swnj 	int timer;
1145069Swnj {
1155069Swnj 
1165089Swnj 	switch (timer) {
1175069Swnj 
1185165Swnj 	/*
1195165Swnj 	 * 2 MSL timeout in shutdown went off.  Delete connection
1205165Swnj 	 * control block.
1215165Swnj 	 */
1225075Swnj 	case TCPT_2MSL:
12310396Ssam 		tp = tcp_close(tp);
12410396Ssam 		break;
1255069Swnj 
1265165Swnj 	/*
1275165Swnj 	 * Retransmission timer went off.  Message has not
1285165Swnj 	 * been acked within retransmit interval.  Back off
129*17362Skarels 	 * to a longer retransmit interval and retransmit one segment.
1305165Swnj 	 */
1315075Swnj 	case TCPT_REXMT:
1325165Swnj 		tp->t_rxtshift++;
1335691Swnj 		if (tp->t_rxtshift > TCP_MAXRXTSHIFT) {
13410396Ssam 			tp = tcp_drop(tp, ETIMEDOUT);
13510396Ssam 			break;
1365691Swnj 		}
137*17362Skarels 		/*
138*17362Skarels 		 * If losing, let the lower level know
139*17362Skarels 		 * and try for a better route.
140*17362Skarels 		 */
141*17362Skarels 		if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 2)
142*17362Skarels 			in_rtchange(tp->t_inpcb);
1435165Swnj 		TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
1445952Swnj 		    (int)tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
1456474Sroot 		if (tcpexprexmtbackoff) {
1466474Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
1476474Sroot 			    tp->t_timer[TCPT_REXMT] << tp->t_rxtshift,
1486474Sroot 			    TCPTV_MIN, TCPTV_MAX);
1496474Sroot 		} else {
1506474Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
1516474Sroot 			    tp->t_timer[TCPT_REXMT] *
1526474Sroot 			        tcp_backoff[tp->t_rxtshift - 1],
1536474Sroot 			    TCPTV_MIN, TCPTV_MAX);
1546474Sroot 		}
1555165Swnj 		tp->snd_nxt = tp->snd_una;
15617314Skarels 		/*
15717314Skarels 		 * If timing a segment in this window, stop the timer.
15817314Skarels 		 */
15917314Skarels 		if (tp->t_rtt && SEQ_GT(tp->t_rtseq, tp->snd_una))
16017314Skarels 			tp->t_rtt = 0;
1615165Swnj 		(void) tcp_output(tp);
16210396Ssam 		break;
1635069Swnj 
1645165Swnj 	/*
1655165Swnj 	 * Persistance timer into zero window.
1665165Swnj 	 * Force a byte to be output, if possible.
1675165Swnj 	 */
1685075Swnj 	case TCPT_PERSIST:
1697156Swnj 		tcp_setpersist(tp);
1705165Swnj 		tp->t_force = 1;
1715165Swnj 		(void) tcp_output(tp);
1725165Swnj 		tp->t_force = 0;
17310396Ssam 		break;
1745069Swnj 
1755165Swnj 	/*
1765165Swnj 	 * Keep-alive timer went off; send something
1775165Swnj 	 * or drop connection if idle for too long.
1785165Swnj 	 */
1795075Swnj 	case TCPT_KEEP:
1806304Sroot 		if (tp->t_state < TCPS_ESTABLISHED)
1816304Sroot 			goto dropit;
1826267Sroot 		if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) {
1836304Sroot 		    	if (tp->t_idle >= TCPTV_MAXIDLE)
1846304Sroot 				goto dropit;
1856267Sroot 			/*
1866267Sroot 			 * Saying tp->rcv_nxt-1 lies about what
1876267Sroot 			 * we have received, and by the protocol spec
1886267Sroot 			 * requires the correspondent TCP to respond.
1896267Sroot 			 * Saying tp->snd_una-1 causes the transmitted
1906267Sroot 			 * byte to lie outside the receive window; this
1916267Sroot 			 * is important because we don't necessarily
1926267Sroot 			 * have a byte in the window to send (consider
1936267Sroot 			 * a one-way stream!)
1946267Sroot 			 */
1955392Swnj 			tcp_respond(tp,
1966267Sroot 			    tp->t_template, tp->rcv_nxt-1, tp->snd_una-1, 0);
1976267Sroot 		} else
1986212Swnj 			tp->t_idle = 0;
1995165Swnj 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
20010396Ssam 		break;
2016304Sroot 	dropit:
20210396Ssam 		tp = tcp_drop(tp, ETIMEDOUT);
20310396Ssam 		break;
2045069Swnj 	}
20510396Ssam 	return (tp);
2065069Swnj }
207