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