Statistics
| Branch: | Revision:

root / slirp / tcp_timer.c @ 9c22a623

History | View | Annotate | Download (9.2 kB)

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