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