VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/tcp_timer.c@ 41408

Last change on this file since 41408 was 41408, checked in by vboxsync, 13 years ago

NAT: don't waste time on tcp connections without connection information.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.5 KB
Line 
1/* $Id: tcp_timer.c 41408 2012-05-22 18:05:05Z 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 */
62void
63tcp_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 */
91void
92tcp_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++;
124tpgone:
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 */
139void
140tcp_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
148const 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 */
156struct tcpcb *
157tcp_timers(PNATState pData, register struct tcpcb *tp, int timer)
158{
159 register int rexmt;
160 int fUninitiolizedTemplate = 0;
161
162 LogFlowFunc(("ENTER: tp:%R[tcpcb793], timer:%d\n", tp, timer));
163 fUninitiolizedTemplate = RT_BOOL(( tp->t_template.ti_src.s_addr == INADDR_ANY
164 || tp->t_template.ti_dst.s_addr == INADDR_ANY));
165 if (fUninitiolizedTemplate)
166 {
167 tp = tcp_drop(pData, tp, 0);
168 return tp;
169 }
170
171 switch (timer)
172 {
173 /*
174 * 2 MSL timeout in shutdown went off. If we're closed but
175 * still waiting for peer to close and connection has been idle
176 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
177 * control block. Otherwise, check again in a bit.
178 */
179 case TCPT_2MSL:
180 if (tp->t_state != TCPS_TIME_WAIT &&
181 tp->t_idle <= tcp_maxidle)
182 tp->t_timer[TCPT_2MSL] = tcp_keepintvl;
183 else
184 tp = tcp_close(pData, tp);
185 break;
186
187 /*
188 * Retransmission timer went off. Message has not
189 * been acked within retransmit interval. Back off
190 * to a longer retransmit interval and retransmit one segment.
191 */
192 case TCPT_REXMT:
193 STAM_COUNTER_INC(&pData->StatTCP_retransmit);
194 /*
195 * XXX If a packet has timed out, then remove all the queued
196 * packets for that session.
197 */
198 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT)
199 {
200 /*
201 * This is a hack to suit our terminal server here at the uni of canberra
202 * since they have trouble with zeroes... It usually lets them through
203 * unharmed, but under some conditions, it'll eat the zeros. If we
204 * keep retransmitting it, it'll keep eating the zeroes, so we keep
205 * retransmitting, and eventually the connection dies...
206 * (this only happens on incoming data)
207 *
208 * So, if we were gonna drop the connection from too many retransmits,
209 * don't... instead halve the t_maxseg, which might break up the NULLs and
210 * let them through
211 *
212 * *sigh*
213 */
214 tp->t_maxseg >>= 1;
215 if (tp->t_maxseg < 32)
216 {
217 /*
218 * We tried our best, now the connection must die!
219 */
220 tp->t_rxtshift = TCP_MAXRXTSHIFT;
221 tcpstat.tcps_timeoutdrop++;
222 tp = tcp_drop(pData, tp, tp->t_softerror);
223 /* tp->t_softerror : ETIMEDOUT); */ /* XXX */
224 return (tp); /* XXX */
225 }
226
227 /*
228 * Set rxtshift to 6, which is still at the maximum
229 * backoff time
230 */
231 tp->t_rxtshift = 6;
232 }
233 tcpstat.tcps_rexmttimeo++;
234 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
235 TCPT_RANGESET(tp->t_rxtcur, rexmt,
236 (short)tp->t_rttmin, TCPTV_REXMTMAX); /* XXX */
237 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
238 /*
239 * If losing, let the lower level know and try for
240 * a better route. Also, if we backed off this far,
241 * our srtt estimate is probably bogus. Clobber it
242 * so we'll take the next rtt measurement as our srtt;
243 * move the current srtt into rttvar to keep the current
244 * retransmit times until then.
245 */
246 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4)
247 {
248/* in_losing(tp->t_inpcb); */
249 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
250 tp->t_srtt = 0;
251 }
252 tp->snd_nxt = tp->snd_una;
253 /*
254 * If timing a segment in this window, stop the timer.
255 */
256 tp->t_rtt = 0;
257 /*
258 * Close the congestion window down to one segment
259 * (we'll open it by one segment for each ack we get).
260 * Since we probably have a window's worth of unacked
261 * data accumulated, this "slow start" keeps us from
262 * dumping all that data as back-to-back packets (which
263 * might overwhelm an intermediate gateway).
264 *
265 * There are two phases to the opening: Initially we
266 * open by one mss on each ack. This makes the window
267 * size increase exponentially with time. If the
268 * window is larger than the path can handle, this
269 * exponential growth results in dropped packet(s)
270 * almost immediately. To get more time between
271 * drops but still "push" the network to take advantage
272 * of improving conditions, we switch from exponential
273 * to linear window opening at some threshold size.
274 * For a threshold, we use half the current window
275 * size, truncated to a multiple of the mss.
276 *
277 * (the minimum cwnd that will give us exponential
278 * growth is 2 mss. We don't allow the threshold
279 * to go below this.)
280 */
281 {
282 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
283 if (win < 2)
284 win = 2;
285 tp->snd_cwnd = tp->t_maxseg;
286 tp->snd_ssthresh = win * tp->t_maxseg;
287 tp->t_dupacks = 0;
288 }
289 (void) tcp_output(pData, tp);
290 break;
291
292 /*
293 * Persistence timer into zero window.
294 * Force a byte to be output, if possible.
295 */
296 case TCPT_PERSIST:
297 tcpstat.tcps_persisttimeo++;
298 tcp_setpersist(tp);
299 tp->t_force = 1;
300 (void) tcp_output(pData, tp);
301 tp->t_force = 0;
302 break;
303
304 /*
305 * Keep-alive timer went off; send something
306 * or drop connection if idle for too long.
307 */
308 case TCPT_KEEP:
309 tcpstat.tcps_keeptimeo++;
310 if (tp->t_state < TCPS_ESTABLISHED)
311 goto dropit;
312/* if (tp->t_socket->so_options & SO_KEEPALIVE && */
313 if ((so_options) && tp->t_state <= TCPS_CLOSE_WAIT)
314 {
315 if (tp->t_idle >= tcp_keepidle + tcp_maxidle)
316 goto dropit;
317 /*
318 * Send a packet designed to force a response
319 * if the peer is up and reachable:
320 * either an ACK if the connection is still alive,
321 * or an RST if the peer has closed the connection
322 * due to timeout or reboot.
323 * Using sequence number tp->snd_una-1
324 * causes the transmitted zero-length segment
325 * to lie outside the receive window;
326 * by the protocol spec, this requires the
327 * correspondent TCP to respond.
328 */
329 tcpstat.tcps_keepprobe++;
330#ifdef TCP_COMPAT_42
331 /*
332 * The keepalive packet must have nonzero length
333 * to get a 4.2 host to respond.
334 */
335 tcp_respond(tp, &tp->t_template, (struct mbuf *)NULL,
336 tp->rcv_nxt - 1, tp->snd_una - 1, 0);
337#else
338 tcp_respond(pData, tp, &tp->t_template, (struct mbuf *)NULL,
339 tp->rcv_nxt, tp->snd_una - 1, 0);
340#endif
341 tp->t_timer[TCPT_KEEP] = tcp_keepintvl;
342 }
343 else
344 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
345 break;
346
347 dropit:
348 tcpstat.tcps_keepdrops++;
349 tp = tcp_drop(pData, tp, 0); /* ETIMEDOUT); */
350 break;
351 }
352
353 return tp;
354}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette