Statistics
| Branch: | Revision:

root / slirp / tcp_timer.c @ 5850586c

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