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