VirtualBox

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

Last change on this file since 28587 was 28449, checked in by vboxsync, 15 years ago

NAT: slirp file headers

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

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