xref: /csrg-svn/sys/netinet/tcp_timer.c (revision 10896)
1*10896Ssam /*	tcp_timer.c	4.31	83/02/10	*/
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"
9*10896Ssam #include "../h/errno.h"
108697Sroot 
118697Sroot #include "../net/if.h"
128697Sroot #include "../net/route.h"
13*10896Ssam 
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,
778640Sroot 				    (struct mbuf *)i,
788697Sroot 				    (struct socketopt *)0);
796212Swnj 				if (ipnxt->inp_prev != ip)
806212Swnj 					goto tpgone;
816212Swnj 			}
825069Swnj 		}
835165Swnj 		tp->t_idle++;
845165Swnj 		if (tp->t_rtt)
855165Swnj 			tp->t_rtt++;
866212Swnj tpgone:
876212Swnj 		ip = ipnxt;
885069Swnj 	}
895075Swnj 	tcp_iss += TCP_ISSINCR/PR_SLOWHZ;		/* increment iss */
905069Swnj 	splx(s);
915069Swnj }
925069Swnj 
935069Swnj /*
945075Swnj  * Cancel all timers for TCP tp.
955069Swnj  */
965089Swnj tcp_canceltimers(tp)
975069Swnj 	struct tcpcb *tp;
985069Swnj {
995069Swnj 	register int i;
1005069Swnj 
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	tcpexprexmtbackoff = 0;
1085069Swnj /*
1095165Swnj  * TCP timer processing.
1105069Swnj  */
11110396Ssam struct tcpcb *
1125075Swnj tcp_timers(tp, timer)
1135069Swnj 	register struct tcpcb *tp;
1145075Swnj 	int timer;
1155069Swnj {
1165069Swnj 
1175089Swnj 	switch (timer) {
1185069Swnj 
1195165Swnj 	/*
1205165Swnj 	 * 2 MSL timeout in shutdown went off.  Delete connection
1215165Swnj 	 * control block.
1225165Swnj 	 */
1235075Swnj 	case TCPT_2MSL:
12410396Ssam 		tp = tcp_close(tp);
12510396Ssam 		break;
1265069Swnj 
1275165Swnj 	/*
1285165Swnj 	 * Retransmission timer went off.  Message has not
1295165Swnj 	 * been acked within retransmit interval.  Back off
1305165Swnj 	 * to a longer retransmit interval and retransmit all
1315165Swnj 	 * unacknowledged messages in the window.
1325165Swnj 	 */
1335075Swnj 	case TCPT_REXMT:
1345165Swnj 		tp->t_rxtshift++;
1355691Swnj 		if (tp->t_rxtshift > TCP_MAXRXTSHIFT) {
13610396Ssam 			tp = tcp_drop(tp, ETIMEDOUT);
13710396Ssam 			break;
1385691Swnj 		}
1395165Swnj 		TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
1405952Swnj 		    (int)tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
1416474Sroot 		if (tcpexprexmtbackoff) {
1426474Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
1436474Sroot 			    tp->t_timer[TCPT_REXMT] << tp->t_rxtshift,
1446474Sroot 			    TCPTV_MIN, TCPTV_MAX);
1456474Sroot 		} else {
1466474Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
1476474Sroot 			    tp->t_timer[TCPT_REXMT] *
1486474Sroot 			        tcp_backoff[tp->t_rxtshift - 1],
1496474Sroot 			    TCPTV_MIN, TCPTV_MAX);
1506474Sroot 		}
1515165Swnj 		tp->snd_nxt = tp->snd_una;
1525165Swnj 		/* this only transmits one segment! */
1535165Swnj 		(void) tcp_output(tp);
15410396Ssam 		break;
1555069Swnj 
1565165Swnj 	/*
1575165Swnj 	 * Persistance timer into zero window.
1585165Swnj 	 * Force a byte to be output, if possible.
1595165Swnj 	 */
1605075Swnj 	case TCPT_PERSIST:
1617156Swnj 		tcp_setpersist(tp);
1625165Swnj 		tp->t_force = 1;
1635165Swnj 		(void) tcp_output(tp);
1645165Swnj 		tp->t_force = 0;
16510396Ssam 		break;
1665069Swnj 
1675165Swnj 	/*
1685165Swnj 	 * Keep-alive timer went off; send something
1695165Swnj 	 * or drop connection if idle for too long.
1705165Swnj 	 */
1715075Swnj 	case TCPT_KEEP:
1726304Sroot 		if (tp->t_state < TCPS_ESTABLISHED)
1736304Sroot 			goto dropit;
1746267Sroot 		if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) {
1756304Sroot 		    	if (tp->t_idle >= TCPTV_MAXIDLE)
1766304Sroot 				goto dropit;
1776267Sroot 			/*
1786267Sroot 			 * Saying tp->rcv_nxt-1 lies about what
1796267Sroot 			 * we have received, and by the protocol spec
1806267Sroot 			 * requires the correspondent TCP to respond.
1816267Sroot 			 * Saying tp->snd_una-1 causes the transmitted
1826267Sroot 			 * byte to lie outside the receive window; this
1836267Sroot 			 * is important because we don't necessarily
1846267Sroot 			 * have a byte in the window to send (consider
1856267Sroot 			 * a one-way stream!)
1866267Sroot 			 */
1875392Swnj 			tcp_respond(tp,
1886267Sroot 			    tp->t_template, tp->rcv_nxt-1, tp->snd_una-1, 0);
1896267Sroot 		} else
1906212Swnj 			tp->t_idle = 0;
1915165Swnj 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
19210396Ssam 		break;
1936304Sroot 	dropit:
19410396Ssam 		tp = tcp_drop(tp, ETIMEDOUT);
19510396Ssam 		break;
1965069Swnj 	}
19710396Ssam 	return (tp);
1985069Swnj }
199