xref: /csrg-svn/sys/netinet/tcp_timer.c (revision 12764)
1*12764Ssam /*	tcp_timer.c	4.32	83/05/27	*/
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"
910896Ssam #include "../h/errno.h"
108697Sroot 
118697Sroot #include "../net/if.h"
128697Sroot #include "../net/route.h"
1310896Ssam 
148404Swnj #include "../netinet/in.h"
158404Swnj #include "../netinet/in_pcb.h"
168404Swnj #include "../netinet/in_systm.h"
178404Swnj #include "../netinet/ip.h"
188404Swnj #include "../netinet/ip_var.h"
198404Swnj #include "../netinet/tcp.h"
208404Swnj #include "../netinet/tcp_fsm.h"
218404Swnj #include "../netinet/tcp_seq.h"
228404Swnj #include "../netinet/tcp_timer.h"
238404Swnj #include "../netinet/tcp_var.h"
248404Swnj #include "../netinet/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 	}
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,
77*12764Ssam 				    (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:
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 
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
1295165Swnj 	 * to a longer retransmit interval and retransmit all
1305165Swnj 	 * unacknowledged messages in the window.
1315165Swnj 	 */
1325075Swnj 	case TCPT_REXMT:
1335165Swnj 		tp->t_rxtshift++;
1345691Swnj 		if (tp->t_rxtshift > TCP_MAXRXTSHIFT) {
13510396Ssam 			tp = tcp_drop(tp, ETIMEDOUT);
13610396Ssam 			break;
1375691Swnj 		}
1385165Swnj 		TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
1395952Swnj 		    (int)tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
1406474Sroot 		if (tcpexprexmtbackoff) {
1416474Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
1426474Sroot 			    tp->t_timer[TCPT_REXMT] << tp->t_rxtshift,
1436474Sroot 			    TCPTV_MIN, TCPTV_MAX);
1446474Sroot 		} else {
1456474Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
1466474Sroot 			    tp->t_timer[TCPT_REXMT] *
1476474Sroot 			        tcp_backoff[tp->t_rxtshift - 1],
1486474Sroot 			    TCPTV_MIN, TCPTV_MAX);
1496474Sroot 		}
1505165Swnj 		tp->snd_nxt = tp->snd_una;
1515165Swnj 		/* this only transmits one segment! */
1525165Swnj 		(void) tcp_output(tp);
15310396Ssam 		break;
1545069Swnj 
1555165Swnj 	/*
1565165Swnj 	 * Persistance timer into zero window.
1575165Swnj 	 * Force a byte to be output, if possible.
1585165Swnj 	 */
1595075Swnj 	case TCPT_PERSIST:
1607156Swnj 		tcp_setpersist(tp);
1615165Swnj 		tp->t_force = 1;
1625165Swnj 		(void) tcp_output(tp);
1635165Swnj 		tp->t_force = 0;
16410396Ssam 		break;
1655069Swnj 
1665165Swnj 	/*
1675165Swnj 	 * Keep-alive timer went off; send something
1685165Swnj 	 * or drop connection if idle for too long.
1695165Swnj 	 */
1705075Swnj 	case TCPT_KEEP:
1716304Sroot 		if (tp->t_state < TCPS_ESTABLISHED)
1726304Sroot 			goto dropit;
1736267Sroot 		if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) {
1746304Sroot 		    	if (tp->t_idle >= TCPTV_MAXIDLE)
1756304Sroot 				goto dropit;
1766267Sroot 			/*
1776267Sroot 			 * Saying tp->rcv_nxt-1 lies about what
1786267Sroot 			 * we have received, and by the protocol spec
1796267Sroot 			 * requires the correspondent TCP to respond.
1806267Sroot 			 * Saying tp->snd_una-1 causes the transmitted
1816267Sroot 			 * byte to lie outside the receive window; this
1826267Sroot 			 * is important because we don't necessarily
1836267Sroot 			 * have a byte in the window to send (consider
1846267Sroot 			 * a one-way stream!)
1856267Sroot 			 */
1865392Swnj 			tcp_respond(tp,
1876267Sroot 			    tp->t_template, tp->rcv_nxt-1, tp->snd_una-1, 0);
1886267Sroot 		} else
1896212Swnj 			tp->t_idle = 0;
1905165Swnj 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
19110396Ssam 		break;
1926304Sroot 	dropit:
19310396Ssam 		tp = tcp_drop(tp, ETIMEDOUT);
19410396Ssam 		break;
1955069Swnj 	}
19610396Ssam 	return (tp);
1975069Swnj }
198