1 | /* $Id: tcp_timer.c 37936 2011-07-14 03:54:41Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * NAT - TCP timers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*
|
---|
19 | * This code is based on:
|
---|
20 | *
|
---|
21 | * Copyright (c) 1982, 1986, 1988, 1990, 1993
|
---|
22 | * The Regents of the University of California. All rights reserved.
|
---|
23 | *
|
---|
24 | * Redistribution and use in source and binary forms, with or without
|
---|
25 | * modification, are permitted provided that the following conditions
|
---|
26 | * are met:
|
---|
27 | * 1. Redistributions of source code must retain the above copyright
|
---|
28 | * notice, this list of conditions and the following disclaimer.
|
---|
29 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
30 | * notice, this list of conditions and the following disclaimer in the
|
---|
31 | * documentation and/or other materials provided with the distribution.
|
---|
32 | * 3. All advertising materials mentioning features or use of this software
|
---|
33 | * must display the following acknowledgement:
|
---|
34 | * This product includes software developed by the University of
|
---|
35 | * California, Berkeley and its contributors.
|
---|
36 | * 4. Neither the name of the University nor the names of its contributors
|
---|
37 | * may be used to endorse or promote products derived from this software
|
---|
38 | * without specific prior written permission.
|
---|
39 | *
|
---|
40 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
50 | * SUCH DAMAGE.
|
---|
51 | *
|
---|
52 | * @(#)tcp_timer.c 8.1 (Berkeley) 6/10/93
|
---|
53 | * tcp_timer.c,v 1.2 1994/08/02 07:49:10 davidg Exp
|
---|
54 | */
|
---|
55 |
|
---|
56 | #include <slirp.h>
|
---|
57 |
|
---|
58 |
|
---|
59 | /*
|
---|
60 | * Fast timeout routine for processing delayed acks
|
---|
61 | */
|
---|
62 | void
|
---|
63 | tcp_fasttimo(PNATState pData)
|
---|
64 | {
|
---|
65 | register struct socket *so, *so_next;
|
---|
66 | register struct tcpcb *tp;
|
---|
67 |
|
---|
68 | LogFlowFuncEnter();
|
---|
69 |
|
---|
70 | so = tcb.so_next;
|
---|
71 | if (so)
|
---|
72 | QSOCKET_FOREACH (so, so_next, tcp)
|
---|
73 | /* { */
|
---|
74 | if ( (tp = (struct tcpcb *)so->so_tcpcb)
|
---|
75 | && (tp->t_flags & TF_DELACK))
|
---|
76 | {
|
---|
77 | tp->t_flags &= ~TF_DELACK;
|
---|
78 | tp->t_flags |= TF_ACKNOW;
|
---|
79 | tcpstat.tcps_delack++;
|
---|
80 | TCP_OUTPUT(pData, tp);
|
---|
81 | }
|
---|
82 | LOOP_LABEL(tcp, so, so_next);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * Tcp protocol timeout routine called every 500 ms.
|
---|
88 | * Updates the timers in all active tcb's and
|
---|
89 | * causes finite state machine actions if timers expire.
|
---|
90 | */
|
---|
91 | void
|
---|
92 | tcp_slowtimo(PNATState pData)
|
---|
93 | {
|
---|
94 | register struct socket *ip, *ipnxt;
|
---|
95 | register struct tcpcb *tp;
|
---|
96 | register int i;
|
---|
97 |
|
---|
98 | LogFlowFuncEnter();
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * Search through tcb's and update active timers.
|
---|
102 | */
|
---|
103 | ip = tcb.so_next;
|
---|
104 | if (ip == 0)
|
---|
105 | return;
|
---|
106 | QSOCKET_FOREACH(ip, ipnxt, tcp)
|
---|
107 | /* { */
|
---|
108 | ipnxt = ip->so_next;
|
---|
109 | tp = sototcpcb(ip);
|
---|
110 | if (tp == 0)
|
---|
111 | CONTINUE(tcp);
|
---|
112 | for (i = 0; i < TCPT_NTIMERS; i++)
|
---|
113 | {
|
---|
114 | if (tp->t_timer[i] && --tp->t_timer[i] == 0)
|
---|
115 | {
|
---|
116 | tcp_timers(pData, tp, i);
|
---|
117 | if (ipnxt->so_prev != ip)
|
---|
118 | goto tpgone;
|
---|
119 | }
|
---|
120 | }
|
---|
121 | tp->t_idle++;
|
---|
122 | if (tp->t_rtt)
|
---|
123 | tp->t_rtt++;
|
---|
124 | tpgone:
|
---|
125 | ;
|
---|
126 | LOOP_LABEL(tcp, ip, ipnxt);
|
---|
127 | }
|
---|
128 | tcp_iss += TCP_ISSINCR / PR_SLOWHZ; /* increment iss */
|
---|
129 | #ifdef TCP_COMPAT_42
|
---|
130 | if ((int)tcp_iss < 0)
|
---|
131 | tcp_iss = 0; /* XXX */
|
---|
132 | #endif
|
---|
133 | tcp_now++; /* for timestamps */
|
---|
134 | }
|
---|
135 |
|
---|
136 | /*
|
---|
137 | * Cancel all timers for TCP tp.
|
---|
138 | */
|
---|
139 | void
|
---|
140 | tcp_canceltimers(struct tcpcb *tp)
|
---|
141 | {
|
---|
142 | register int i;
|
---|
143 |
|
---|
144 | for (i = 0; i < TCPT_NTIMERS; i++)
|
---|
145 | tp->t_timer[i] = 0;
|
---|
146 | }
|
---|
147 |
|
---|
148 | const int tcp_backoff[TCP_MAXRXTSHIFT + 1] =
|
---|
149 | {
|
---|
150 | 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64
|
---|
151 | };
|
---|
152 |
|
---|
153 | /*
|
---|
154 | * TCP timer processing.
|
---|
155 | */
|
---|
156 | struct tcpcb *
|
---|
157 | tcp_timers(PNATState pData, register struct tcpcb *tp, int timer)
|
---|
158 | {
|
---|
159 | register int rexmt;
|
---|
160 |
|
---|
161 | LogFlowFunc(("ENTER: tp:%R[tcpcb793], timer:%d\n", tp, timer));
|
---|
162 |
|
---|
163 | switch (timer)
|
---|
164 | {
|
---|
165 | /*
|
---|
166 | * 2 MSL timeout in shutdown went off. If we're closed but
|
---|
167 | * still waiting for peer to close and connection has been idle
|
---|
168 | * too long, or if 2MSL time is up from TIME_WAIT, delete connection
|
---|
169 | * control block. Otherwise, check again in a bit.
|
---|
170 | */
|
---|
171 | case TCPT_2MSL:
|
---|
172 | if (tp->t_state != TCPS_TIME_WAIT &&
|
---|
173 | tp->t_idle <= tcp_maxidle)
|
---|
174 | tp->t_timer[TCPT_2MSL] = tcp_keepintvl;
|
---|
175 | else
|
---|
176 | tp = tcp_close(pData, tp);
|
---|
177 | break;
|
---|
178 |
|
---|
179 | /*
|
---|
180 | * Retransmission timer went off. Message has not
|
---|
181 | * been acked within retransmit interval. Back off
|
---|
182 | * to a longer retransmit interval and retransmit one segment.
|
---|
183 | */
|
---|
184 | case TCPT_REXMT:
|
---|
185 | STAM_COUNTER_INC(&pData->StatTCP_retransmit);
|
---|
186 | /*
|
---|
187 | * XXX If a packet has timed out, then remove all the queued
|
---|
188 | * packets for that session.
|
---|
189 | */
|
---|
190 | if (++tp->t_rxtshift > TCP_MAXRXTSHIFT)
|
---|
191 | {
|
---|
192 | /*
|
---|
193 | * This is a hack to suit our terminal server here at the uni of canberra
|
---|
194 | * since they have trouble with zeroes... It usually lets them through
|
---|
195 | * unharmed, but under some conditions, it'll eat the zeros. If we
|
---|
196 | * keep retransmitting it, it'll keep eating the zeroes, so we keep
|
---|
197 | * retransmitting, and eventually the connection dies...
|
---|
198 | * (this only happens on incoming data)
|
---|
199 | *
|
---|
200 | * So, if we were gonna drop the connection from too many retransmits,
|
---|
201 | * don't... instead halve the t_maxseg, which might break up the NULLs and
|
---|
202 | * let them through
|
---|
203 | *
|
---|
204 | * *sigh*
|
---|
205 | */
|
---|
206 | tp->t_maxseg >>= 1;
|
---|
207 | if (tp->t_maxseg < 32)
|
---|
208 | {
|
---|
209 | /*
|
---|
210 | * We tried our best, now the connection must die!
|
---|
211 | */
|
---|
212 | tp->t_rxtshift = TCP_MAXRXTSHIFT;
|
---|
213 | tcpstat.tcps_timeoutdrop++;
|
---|
214 | tp = tcp_drop(pData, tp, tp->t_softerror);
|
---|
215 | /* tp->t_softerror : ETIMEDOUT); */ /* XXX */
|
---|
216 | return (tp); /* XXX */
|
---|
217 | }
|
---|
218 |
|
---|
219 | /*
|
---|
220 | * Set rxtshift to 6, which is still at the maximum
|
---|
221 | * backoff time
|
---|
222 | */
|
---|
223 | tp->t_rxtshift = 6;
|
---|
224 | }
|
---|
225 | tcpstat.tcps_rexmttimeo++;
|
---|
226 | rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
|
---|
227 | TCPT_RANGESET(tp->t_rxtcur, rexmt,
|
---|
228 | (short)tp->t_rttmin, TCPTV_REXMTMAX); /* XXX */
|
---|
229 | tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
|
---|
230 | /*
|
---|
231 | * If losing, let the lower level know and try for
|
---|
232 | * a better route. Also, if we backed off this far,
|
---|
233 | * our srtt estimate is probably bogus. Clobber it
|
---|
234 | * so we'll take the next rtt measurement as our srtt;
|
---|
235 | * move the current srtt into rttvar to keep the current
|
---|
236 | * retransmit times until then.
|
---|
237 | */
|
---|
238 | if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4)
|
---|
239 | {
|
---|
240 | /* in_losing(tp->t_inpcb); */
|
---|
241 | tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
|
---|
242 | tp->t_srtt = 0;
|
---|
243 | }
|
---|
244 | tp->snd_nxt = tp->snd_una;
|
---|
245 | /*
|
---|
246 | * If timing a segment in this window, stop the timer.
|
---|
247 | */
|
---|
248 | tp->t_rtt = 0;
|
---|
249 | /*
|
---|
250 | * Close the congestion window down to one segment
|
---|
251 | * (we'll open it by one segment for each ack we get).
|
---|
252 | * Since we probably have a window's worth of unacked
|
---|
253 | * data accumulated, this "slow start" keeps us from
|
---|
254 | * dumping all that data as back-to-back packets (which
|
---|
255 | * might overwhelm an intermediate gateway).
|
---|
256 | *
|
---|
257 | * There are two phases to the opening: Initially we
|
---|
258 | * open by one mss on each ack. This makes the window
|
---|
259 | * size increase exponentially with time. If the
|
---|
260 | * window is larger than the path can handle, this
|
---|
261 | * exponential growth results in dropped packet(s)
|
---|
262 | * almost immediately. To get more time between
|
---|
263 | * drops but still "push" the network to take advantage
|
---|
264 | * of improving conditions, we switch from exponential
|
---|
265 | * to linear window opening at some threshold size.
|
---|
266 | * For a threshold, we use half the current window
|
---|
267 | * size, truncated to a multiple of the mss.
|
---|
268 | *
|
---|
269 | * (the minimum cwnd that will give us exponential
|
---|
270 | * growth is 2 mss. We don't allow the threshold
|
---|
271 | * to go below this.)
|
---|
272 | */
|
---|
273 | {
|
---|
274 | u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
|
---|
275 | if (win < 2)
|
---|
276 | win = 2;
|
---|
277 | tp->snd_cwnd = tp->t_maxseg;
|
---|
278 | tp->snd_ssthresh = win * tp->t_maxseg;
|
---|
279 | tp->t_dupacks = 0;
|
---|
280 | }
|
---|
281 | (void) tcp_output(pData, tp);
|
---|
282 | break;
|
---|
283 |
|
---|
284 | /*
|
---|
285 | * Persistence timer into zero window.
|
---|
286 | * Force a byte to be output, if possible.
|
---|
287 | */
|
---|
288 | case TCPT_PERSIST:
|
---|
289 | tcpstat.tcps_persisttimeo++;
|
---|
290 | tcp_setpersist(tp);
|
---|
291 | tp->t_force = 1;
|
---|
292 | (void) tcp_output(pData, tp);
|
---|
293 | tp->t_force = 0;
|
---|
294 | break;
|
---|
295 |
|
---|
296 | /*
|
---|
297 | * Keep-alive timer went off; send something
|
---|
298 | * or drop connection if idle for too long.
|
---|
299 | */
|
---|
300 | case TCPT_KEEP:
|
---|
301 | tcpstat.tcps_keeptimeo++;
|
---|
302 | if (tp->t_state < TCPS_ESTABLISHED)
|
---|
303 | goto dropit;
|
---|
304 | /* if (tp->t_socket->so_options & SO_KEEPALIVE && */
|
---|
305 | if ((so_options) && tp->t_state <= TCPS_CLOSE_WAIT)
|
---|
306 | {
|
---|
307 | if (tp->t_idle >= tcp_keepidle + tcp_maxidle)
|
---|
308 | goto dropit;
|
---|
309 | /*
|
---|
310 | * Send a packet designed to force a response
|
---|
311 | * if the peer is up and reachable:
|
---|
312 | * either an ACK if the connection is still alive,
|
---|
313 | * or an RST if the peer has closed the connection
|
---|
314 | * due to timeout or reboot.
|
---|
315 | * Using sequence number tp->snd_una-1
|
---|
316 | * causes the transmitted zero-length segment
|
---|
317 | * to lie outside the receive window;
|
---|
318 | * by the protocol spec, this requires the
|
---|
319 | * correspondent TCP to respond.
|
---|
320 | */
|
---|
321 | tcpstat.tcps_keepprobe++;
|
---|
322 | #ifdef TCP_COMPAT_42
|
---|
323 | /*
|
---|
324 | * The keepalive packet must have nonzero length
|
---|
325 | * to get a 4.2 host to respond.
|
---|
326 | */
|
---|
327 | tcp_respond(tp, &tp->t_template, (struct mbuf *)NULL,
|
---|
328 | tp->rcv_nxt - 1, tp->snd_una - 1, 0);
|
---|
329 | #else
|
---|
330 | tcp_respond(pData, tp, &tp->t_template, (struct mbuf *)NULL,
|
---|
331 | tp->rcv_nxt, tp->snd_una - 1, 0);
|
---|
332 | #endif
|
---|
333 | tp->t_timer[TCPT_KEEP] = tcp_keepintvl;
|
---|
334 | }
|
---|
335 | else
|
---|
336 | tp->t_timer[TCPT_KEEP] = tcp_keepidle;
|
---|
337 | break;
|
---|
338 |
|
---|
339 | dropit:
|
---|
340 | tcpstat.tcps_keepdrops++;
|
---|
341 | tp = tcp_drop(pData, tp, 0); /* ETIMEDOUT); */
|
---|
342 | break;
|
---|
343 | }
|
---|
344 |
|
---|
345 | return tp;
|
---|
346 | }
|
---|