xref: /original-bsd/sys/netinet/tcp_timer.c (revision 8477994b)
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)tcp_timer.c	7.18 (Berkeley) 06/28/90
8  */
9 
10 #include "param.h"
11 #include "systm.h"
12 #include "malloc.h"
13 #include "mbuf.h"
14 #include "socket.h"
15 #include "socketvar.h"
16 #include "protosw.h"
17 #include "errno.h"
18 
19 #include "../net/if.h"
20 #include "../net/route.h"
21 
22 #include "in.h"
23 #include "in_systm.h"
24 #include "ip.h"
25 #include "in_pcb.h"
26 #include "ip_var.h"
27 #include "tcp.h"
28 #include "tcp_fsm.h"
29 #include "tcp_seq.h"
30 #include "tcp_timer.h"
31 #include "tcp_var.h"
32 #include "tcpip.h"
33 
34 int	tcp_keepidle = TCPTV_KEEP_IDLE;
35 int	tcp_keepintvl = TCPTV_KEEPINTVL;
36 int	tcp_maxidle;
37 /*
38  * Fast timeout routine for processing delayed acks
39  */
40 tcp_fasttimo()
41 {
42 	register struct inpcb *inp;
43 	register struct tcpcb *tp;
44 	int s = splnet();
45 
46 	inp = tcb.inp_next;
47 	if (inp)
48 	for (; inp != &tcb; inp = inp->inp_next)
49 		if ((tp = (struct tcpcb *)inp->inp_ppcb) &&
50 		    (tp->t_flags & TF_DELACK)) {
51 			tp->t_flags &= ~TF_DELACK;
52 			tp->t_flags |= TF_ACKNOW;
53 			tcpstat.tcps_delack++;
54 			(void) tcp_output(tp);
55 		}
56 	splx(s);
57 }
58 
59 /*
60  * Tcp protocol timeout routine called every 500 ms.
61  * Updates the timers in all active tcb's and
62  * causes finite state machine actions if timers expire.
63  */
64 tcp_slowtimo()
65 {
66 	register struct inpcb *ip, *ipnxt;
67 	register struct tcpcb *tp;
68 	int s = splnet();
69 	register int i;
70 
71 	tcp_maxidle = TCPTV_KEEPCNT * tcp_keepintvl;
72 	/*
73 	 * Search through tcb's and update active timers.
74 	 */
75 	ip = tcb.inp_next;
76 	if (ip == 0) {
77 		splx(s);
78 		return;
79 	}
80 	for (; ip != &tcb; ip = ipnxt) {
81 		ipnxt = ip->inp_next;
82 		tp = intotcpcb(ip);
83 		if (tp == 0)
84 			continue;
85 		for (i = 0; i < TCPT_NTIMERS; i++) {
86 			if (tp->t_timer[i] && --tp->t_timer[i] == 0) {
87 				(void) tcp_usrreq(tp->t_inpcb->inp_socket,
88 				    PRU_SLOWTIMO, (struct mbuf *)0,
89 				    (struct mbuf *)i, (struct mbuf *)0);
90 				if (ipnxt->inp_prev != ip)
91 					goto tpgone;
92 			}
93 		}
94 		tp->t_idle++;
95 		if (tp->t_rtt)
96 			tp->t_rtt++;
97 tpgone:
98 		;
99 	}
100 	tcp_iss += TCP_ISSINCR/PR_SLOWHZ;		/* increment iss */
101 #ifdef TCP_COMPAT_42
102 	if ((int)tcp_iss < 0)
103 		tcp_iss = 0;				/* XXX */
104 #endif
105 	splx(s);
106 }
107 
108 /*
109  * Cancel all timers for TCP tp.
110  */
111 tcp_canceltimers(tp)
112 	struct tcpcb *tp;
113 {
114 	register int i;
115 
116 	for (i = 0; i < TCPT_NTIMERS; i++)
117 		tp->t_timer[i] = 0;
118 }
119 
120 int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
121     { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
122 
123 /*
124  * TCP timer processing.
125  */
126 struct tcpcb *
127 tcp_timers(tp, timer)
128 	register struct tcpcb *tp;
129 	int timer;
130 {
131 	register int rexmt;
132 
133 	switch (timer) {
134 
135 	/*
136 	 * 2 MSL timeout in shutdown went off.  If we're closed but
137 	 * still waiting for peer to close and connection has been idle
138 	 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
139 	 * control block.  Otherwise, check again in a bit.
140 	 */
141 	case TCPT_2MSL:
142 		if (tp->t_state != TCPS_TIME_WAIT &&
143 		    tp->t_idle <= tcp_maxidle)
144 			tp->t_timer[TCPT_2MSL] = tcp_keepintvl;
145 		else
146 			tp = tcp_close(tp);
147 		break;
148 
149 	/*
150 	 * Retransmission timer went off.  Message has not
151 	 * been acked within retransmit interval.  Back off
152 	 * to a longer retransmit interval and retransmit one segment.
153 	 */
154 	case TCPT_REXMT:
155 		if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
156 			tp->t_rxtshift = TCP_MAXRXTSHIFT;
157 			tcpstat.tcps_timeoutdrop++;
158 			tp = tcp_drop(tp, tp->t_softerror ?
159 			    tp->t_softerror : ETIMEDOUT);
160 			break;
161 		}
162 		tcpstat.tcps_rexmttimeo++;
163 		rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
164 		TCPT_RANGESET(tp->t_rxtcur, rexmt,
165 		    tp->t_rttmin, TCPTV_REXMTMAX);
166 		tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
167 		/*
168 		 * If losing, let the lower level know and try for
169 		 * a better route.  Also, if we backed off this far,
170 		 * our srtt estimate is probably bogus.  Clobber it
171 		 * so we'll take the next rtt measurement as our srtt;
172 		 * move the current srtt into rttvar to keep the current
173 		 * retransmit times until then.
174 		 */
175 		if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
176 			in_losing(tp->t_inpcb);
177 			tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
178 			tp->t_srtt = 0;
179 		}
180 		tp->snd_nxt = tp->snd_una;
181 		/*
182 		 * If timing a segment in this window, stop the timer.
183 		 */
184 		tp->t_rtt = 0;
185 		/*
186 		 * Close the congestion window down to one segment
187 		 * (we'll open it by one segment for each ack we get).
188 		 * Since we probably have a window's worth of unacked
189 		 * data accumulated, this "slow start" keeps us from
190 		 * dumping all that data as back-to-back packets (which
191 		 * might overwhelm an intermediate gateway).
192 		 *
193 		 * There are two phases to the opening: Initially we
194 		 * open by one mss on each ack.  This makes the window
195 		 * size increase exponentially with time.  If the
196 		 * window is larger than the path can handle, this
197 		 * exponential growth results in dropped packet(s)
198 		 * almost immediately.  To get more time between
199 		 * drops but still "push" the network to take advantage
200 		 * of improving conditions, we switch from exponential
201 		 * to linear window opening at some threshhold size.
202 		 * For a threshhold, we use half the current window
203 		 * size, truncated to a multiple of the mss.
204 		 *
205 		 * (the minimum cwnd that will give us exponential
206 		 * growth is 2 mss.  We don't allow the threshhold
207 		 * to go below this.)
208 		 */
209 		{
210 		u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
211 		if (win < 2)
212 			win = 2;
213 		tp->snd_cwnd = tp->t_maxseg;
214 		tp->snd_ssthresh = win * tp->t_maxseg;
215 		tp->t_dupacks = 0;
216 		}
217 		(void) tcp_output(tp);
218 		break;
219 
220 	/*
221 	 * Persistance timer into zero window.
222 	 * Force a byte to be output, if possible.
223 	 */
224 	case TCPT_PERSIST:
225 		tcpstat.tcps_persisttimeo++;
226 		tcp_setpersist(tp);
227 		tp->t_force = 1;
228 		(void) tcp_output(tp);
229 		tp->t_force = 0;
230 		break;
231 
232 	/*
233 	 * Keep-alive timer went off; send something
234 	 * or drop connection if idle for too long.
235 	 */
236 	case TCPT_KEEP:
237 		tcpstat.tcps_keeptimeo++;
238 		if (tp->t_state < TCPS_ESTABLISHED)
239 			goto dropit;
240 		if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE &&
241 		    tp->t_state <= TCPS_CLOSE_WAIT) {
242 		    	if (tp->t_idle >= tcp_keepidle + tcp_maxidle)
243 				goto dropit;
244 			/*
245 			 * Send a packet designed to force a response
246 			 * if the peer is up and reachable:
247 			 * either an ACK if the connection is still alive,
248 			 * or an RST if the peer has closed the connection
249 			 * due to timeout or reboot.
250 			 * Using sequence number tp->snd_una-1
251 			 * causes the transmitted zero-length segment
252 			 * to lie outside the receive window;
253 			 * by the protocol spec, this requires the
254 			 * correspondent TCP to respond.
255 			 */
256 			tcpstat.tcps_keepprobe++;
257 #ifdef TCP_COMPAT_42
258 			/*
259 			 * The keepalive packet must have nonzero length
260 			 * to get a 4.2 host to respond.
261 			 */
262 			tcp_respond(tp, tp->t_template, (struct mbuf *)NULL,
263 			    tp->rcv_nxt - 1, tp->snd_una - 1, 0);
264 #else
265 			tcp_respond(tp, tp->t_template, (struct mbuf *)NULL,
266 			    tp->rcv_nxt, tp->snd_una - 1, 0);
267 #endif
268 			tp->t_timer[TCPT_KEEP] = tcp_keepintvl;
269 		} else
270 			tp->t_timer[TCPT_KEEP] = tcp_keepidle;
271 		break;
272 	dropit:
273 		tcpstat.tcps_keepdrops++;
274 		tp = tcp_drop(tp, ETIMEDOUT);
275 		break;
276 	}
277 	return (tp);
278 }
279