VirtualBox

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

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use