xref: /original-bsd/sys/netinet/tcp_timer.c (revision 7e7b101a)
1 /*	tcp_timer.c	6.5	84/11/14	*/
2 
3 #include "param.h"
4 #include "systm.h"
5 #include "mbuf.h"
6 #include "socket.h"
7 #include "socketvar.h"
8 #include "protosw.h"
9 #include "errno.h"
10 
11 #include "../net/if.h"
12 #include "../net/route.h"
13 
14 #include "in.h"
15 #include "in_pcb.h"
16 #include "in_systm.h"
17 #include "ip.h"
18 #include "ip_var.h"
19 #include "tcp.h"
20 #include "tcp_fsm.h"
21 #include "tcp_seq.h"
22 #include "tcp_timer.h"
23 #include "tcp_var.h"
24 #include "tcpip.h"
25 
26 int	tcpnodelack = 0;
27 /*
28  * Fast timeout routine for processing delayed acks
29  */
30 tcp_fasttimo()
31 {
32 	register struct inpcb *inp;
33 	register struct tcpcb *tp;
34 	int s = splnet();
35 
36 	inp = tcb.inp_next;
37 	if (inp)
38 	for (; inp != &tcb; inp = inp->inp_next)
39 		if ((tp = (struct tcpcb *)inp->inp_ppcb) &&
40 		    (tp->t_flags & TF_DELACK)) {
41 			tp->t_flags &= ~TF_DELACK;
42 			tp->t_flags |= TF_ACKNOW;
43 			(void) tcp_output(tp);
44 		}
45 	splx(s);
46 }
47 
48 /*
49  * Tcp protocol timeout routine called every 500 ms.
50  * Updates the timers in all active tcb's and
51  * causes finite state machine actions if timers expire.
52  */
53 tcp_slowtimo()
54 {
55 	register struct inpcb *ip, *ipnxt;
56 	register struct tcpcb *tp;
57 	int s = splnet();
58 	register int i;
59 
60 	/*
61 	 * Search through tcb's and update active timers.
62 	 */
63 	ip = tcb.inp_next;
64 	if (ip == 0) {
65 		splx(s);
66 		return;
67 	}
68 	for (; ip != &tcb; ip = ipnxt) {
69 		ipnxt = ip->inp_next;
70 		tp = intotcpcb(ip);
71 		if (tp == 0)
72 			continue;
73 		for (i = 0; i < TCPT_NTIMERS; i++) {
74 			if (tp->t_timer[i] && --tp->t_timer[i] == 0) {
75 				(void) tcp_usrreq(tp->t_inpcb->inp_socket,
76 				    PRU_SLOWTIMO, (struct mbuf *)0,
77 				    (struct mbuf *)i, (struct mbuf *)0);
78 				if (ipnxt->inp_prev != ip)
79 					goto tpgone;
80 			}
81 		}
82 		tp->t_idle++;
83 		if (tp->t_rtt)
84 			tp->t_rtt++;
85 tpgone:
86 		;
87 	}
88 	tcp_iss += TCP_ISSINCR/PR_SLOWHZ;		/* increment iss */
89 	splx(s);
90 }
91 
92 /*
93  * Cancel all timers for TCP tp.
94  */
95 tcp_canceltimers(tp)
96 	struct tcpcb *tp;
97 {
98 	register int i;
99 
100 	for (i = 0; i < TCPT_NTIMERS; i++)
101 		tp->t_timer[i] = 0;
102 }
103 
104 float	tcp_backoff[TCP_MAXRXTSHIFT] =
105     { 1.0, 1.2, 1.4, 1.7, 2.0, 3.0, 5.0, 8.0, 16.0, 32.0 };
106 int	tcpexprexmtbackoff = 0;
107 /*
108  * TCP timer processing.
109  */
110 struct tcpcb *
111 tcp_timers(tp, timer)
112 	register struct tcpcb *tp;
113 	int timer;
114 {
115 
116 	switch (timer) {
117 
118 	/*
119 	 * 2 MSL timeout in shutdown went off.  Delete connection
120 	 * control block.
121 	 */
122 	case TCPT_2MSL:
123 		tp = tcp_close(tp);
124 		break;
125 
126 	/*
127 	 * Retransmission timer went off.  Message has not
128 	 * been acked within retransmit interval.  Back off
129 	 * to a longer retransmit interval and retransmit one segment.
130 	 */
131 	case TCPT_REXMT:
132 		tp->t_rxtshift++;
133 		if (tp->t_rxtshift > TCP_MAXRXTSHIFT) {
134 			tp = tcp_drop(tp, ETIMEDOUT);
135 			break;
136 		}
137 		/*
138 		 * If losing, let the lower level know
139 		 * and try for a better route.
140 		 */
141 		if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 2)
142 			in_rtchange(tp->t_inpcb);
143 		TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
144 		    (int)tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
145 		if (tcpexprexmtbackoff) {
146 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
147 			    tp->t_timer[TCPT_REXMT] << tp->t_rxtshift,
148 			    TCPTV_MIN, TCPTV_MAX);
149 		} else {
150 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
151 			    tp->t_timer[TCPT_REXMT] *
152 			        tcp_backoff[tp->t_rxtshift - 1],
153 			    TCPTV_MIN, TCPTV_MAX);
154 		}
155 		tp->snd_nxt = tp->snd_una;
156 		/*
157 		 * If timing a segment in this window, stop the timer.
158 		 */
159 		if (tp->t_rtt && SEQ_GT(tp->t_rtseq, tp->snd_una))
160 			tp->t_rtt = 0;
161 		(void) tcp_output(tp);
162 		break;
163 
164 	/*
165 	 * Persistance timer into zero window.
166 	 * Force a byte to be output, if possible.
167 	 */
168 	case TCPT_PERSIST:
169 		tcp_setpersist(tp);
170 		tp->t_force = 1;
171 		(void) tcp_output(tp);
172 		tp->t_force = 0;
173 		break;
174 
175 	/*
176 	 * Keep-alive timer went off; send something
177 	 * or drop connection if idle for too long.
178 	 */
179 	case TCPT_KEEP:
180 		if (tp->t_state < TCPS_ESTABLISHED)
181 			goto dropit;
182 		if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) {
183 		    	if (tp->t_idle >= TCPTV_MAXIDLE)
184 				goto dropit;
185 			/*
186 			 * Saying tp->rcv_nxt-1 lies about what
187 			 * we have received, and by the protocol spec
188 			 * requires the correspondent TCP to respond.
189 			 * Saying tp->snd_una-1 causes the transmitted
190 			 * byte to lie outside the receive window; this
191 			 * is important because we don't necessarily
192 			 * have a byte in the window to send (consider
193 			 * a one-way stream!)
194 			 */
195 			tcp_respond(tp,
196 			    tp->t_template, tp->rcv_nxt-1, tp->snd_una-1, 0);
197 		} else
198 			tp->t_idle = 0;
199 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
200 		break;
201 	dropit:
202 		tp = tcp_drop(tp, ETIMEDOUT);
203 		break;
204 	}
205 	return (tp);
206 }
207