Statistics
| Branch: | Revision:

root / slirp / tcp_timer.c @ 5fafdf24

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 f0cbd3ec bellard
int        tcp_keepidle = TCPTV_KEEP_IDLE;
40 f0cbd3ec bellard
int        tcp_keepintvl = TCPTV_KEEPINTVL;
41 f0cbd3ec bellard
int        tcp_maxidle;
42 f0cbd3ec bellard
int        so_options = DO_KEEPALIVE;
43 f0cbd3ec bellard
44 f0cbd3ec bellard
struct   tcpstat tcpstat;        /* tcp statistics */
45 f0cbd3ec bellard
u_int32_t        tcp_now;                /* for RFC 1323 timestamps */
46 f0cbd3ec bellard
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 f0cbd3ec bellard
                        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
        tcp_maxidle = TCPTV_KEEPCNT * tcp_keepintvl;
85 f0cbd3ec bellard
        /*
86 f0cbd3ec bellard
         * Search through tcb's and update active timers.
87 f0cbd3ec bellard
         */
88 f0cbd3ec bellard
        ip = tcb.so_next;
89 f0cbd3ec bellard
        if (ip == 0)
90 f0cbd3ec bellard
           return;
91 f0cbd3ec bellard
        for (; ip != &tcb; ip = ipnxt) {
92 f0cbd3ec bellard
                ipnxt = ip->so_next;
93 f0cbd3ec bellard
                tp = sototcpcb(ip);
94 f0cbd3ec bellard
                if (tp == 0)
95 f0cbd3ec bellard
                        continue;
96 f0cbd3ec bellard
                for (i = 0; i < TCPT_NTIMERS; i++) {
97 f0cbd3ec bellard
                        if (tp->t_timer[i] && --tp->t_timer[i] == 0) {
98 f0cbd3ec bellard
                                tcp_timers(tp,i);
99 f0cbd3ec bellard
                                if (ipnxt->so_prev != ip)
100 f0cbd3ec bellard
                                        goto tpgone;
101 f0cbd3ec bellard
                        }
102 f0cbd3ec bellard
                }
103 f0cbd3ec bellard
                tp->t_idle++;
104 f0cbd3ec bellard
                if (tp->t_rtt)
105 f0cbd3ec bellard
                   tp->t_rtt++;
106 f0cbd3ec bellard
tpgone:
107 f0cbd3ec bellard
                ;
108 f0cbd3ec bellard
        }
109 f0cbd3ec bellard
        tcp_iss += TCP_ISSINCR/PR_SLOWHZ;                /* increment iss */
110 f0cbd3ec bellard
#ifdef TCP_COMPAT_42
111 f0cbd3ec bellard
        if ((int)tcp_iss < 0)
112 f0cbd3ec bellard
                tcp_iss = 0;                                /* XXX */
113 f0cbd3ec bellard
#endif
114 f0cbd3ec bellard
        tcp_now++;                                        /* for timestamps */
115 f0cbd3ec bellard
}
116 f0cbd3ec bellard
117 f0cbd3ec bellard
/*
118 f0cbd3ec bellard
 * Cancel all timers for TCP tp.
119 f0cbd3ec bellard
 */
120 f0cbd3ec bellard
void
121 f0cbd3ec bellard
tcp_canceltimers(tp)
122 f0cbd3ec bellard
        struct tcpcb *tp;
123 f0cbd3ec bellard
{
124 f0cbd3ec bellard
        register int i;
125 f0cbd3ec bellard
126 f0cbd3ec bellard
        for (i = 0; i < TCPT_NTIMERS; i++)
127 f0cbd3ec bellard
                tp->t_timer[i] = 0;
128 f0cbd3ec bellard
}
129 f0cbd3ec bellard
130 f0cbd3ec bellard
int        tcp_backoff[TCP_MAXRXTSHIFT + 1] =
131 f0cbd3ec bellard
   { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
132 f0cbd3ec bellard
133 f0cbd3ec bellard
/*
134 f0cbd3ec bellard
 * TCP timer processing.
135 f0cbd3ec bellard
 */
136 f0cbd3ec bellard
struct tcpcb *
137 f0cbd3ec bellard
tcp_timers(tp, timer)
138 f0cbd3ec bellard
        register struct tcpcb *tp;
139 f0cbd3ec bellard
        int timer;
140 f0cbd3ec bellard
{
141 f0cbd3ec bellard
        register int rexmt;
142 5fafdf24 ths
143 f0cbd3ec bellard
        DEBUG_CALL("tcp_timers");
144 5fafdf24 ths
145 f0cbd3ec bellard
        switch (timer) {
146 f0cbd3ec bellard
147 f0cbd3ec bellard
        /*
148 f0cbd3ec bellard
         * 2 MSL timeout in shutdown went off.  If we're closed but
149 f0cbd3ec bellard
         * still waiting for peer to close and connection has been idle
150 f0cbd3ec bellard
         * too long, or if 2MSL time is up from TIME_WAIT, delete connection
151 f0cbd3ec bellard
         * control block.  Otherwise, check again in a bit.
152 f0cbd3ec bellard
         */
153 f0cbd3ec bellard
        case TCPT_2MSL:
154 f0cbd3ec bellard
                if (tp->t_state != TCPS_TIME_WAIT &&
155 f0cbd3ec bellard
                    tp->t_idle <= tcp_maxidle)
156 f0cbd3ec bellard
                        tp->t_timer[TCPT_2MSL] = tcp_keepintvl;
157 f0cbd3ec bellard
                else
158 f0cbd3ec bellard
                        tp = tcp_close(tp);
159 f0cbd3ec bellard
                break;
160 f0cbd3ec bellard
161 f0cbd3ec bellard
        /*
162 f0cbd3ec bellard
         * Retransmission timer went off.  Message has not
163 f0cbd3ec bellard
         * been acked within retransmit interval.  Back off
164 f0cbd3ec bellard
         * to a longer retransmit interval and retransmit one segment.
165 f0cbd3ec bellard
         */
166 f0cbd3ec bellard
        case TCPT_REXMT:
167 5fafdf24 ths
        
168 f0cbd3ec bellard
                /*
169 f0cbd3ec bellard
                 * XXXXX If a packet has timed out, then remove all the queued
170 f0cbd3ec bellard
                 * packets for that session.
171 f0cbd3ec bellard
                 */
172 5fafdf24 ths
        
173 f0cbd3ec bellard
                if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
174 f0cbd3ec bellard
                        /*
175 f0cbd3ec bellard
                         * This is a hack to suit our terminal server here at the uni of canberra
176 f0cbd3ec bellard
                         * since they have trouble with zeroes... It usually lets them through
177 f0cbd3ec bellard
                         * unharmed, but under some conditions, it'll eat the zeros.  If we
178 f0cbd3ec bellard
                         * keep retransmitting it, it'll keep eating the zeroes, so we keep
179 f0cbd3ec bellard
                         * retransmitting, and eventually the connection dies...
180 f0cbd3ec bellard
                         * (this only happens on incoming data)
181 5fafdf24 ths
                         *
182 f0cbd3ec bellard
                         * So, if we were gonna drop the connection from too many retransmits,
183 f0cbd3ec bellard
                         * don't... instead halve the t_maxseg, which might break up the NULLs and
184 f0cbd3ec bellard
                         * let them through
185 5fafdf24 ths
                         *
186 f0cbd3ec bellard
                         * *sigh*
187 f0cbd3ec bellard
                         */
188 5fafdf24 ths
                
189 f0cbd3ec bellard
                        tp->t_maxseg >>= 1;
190 f0cbd3ec bellard
                        if (tp->t_maxseg < 32) {
191 f0cbd3ec bellard
                                /*
192 f0cbd3ec bellard
                                 * We tried our best, now the connection must die!
193 f0cbd3ec bellard
                                 */
194 f0cbd3ec bellard
                                tp->t_rxtshift = TCP_MAXRXTSHIFT;
195 f0cbd3ec bellard
                                tcpstat.tcps_timeoutdrop++;
196 f0cbd3ec bellard
                                tp = tcp_drop(tp, tp->t_softerror);
197 f0cbd3ec bellard
                                /* tp->t_softerror : ETIMEDOUT); */ /* XXX */
198 f0cbd3ec bellard
                                return (tp); /* XXX */
199 f0cbd3ec bellard
                        }
200 5fafdf24 ths
                
201 f0cbd3ec bellard
                        /*
202 f0cbd3ec bellard
                         * Set rxtshift to 6, which is still at the maximum
203 f0cbd3ec bellard
                         * backoff time
204 f0cbd3ec bellard
                         */
205 f0cbd3ec bellard
                        tp->t_rxtshift = 6;
206 f0cbd3ec bellard
                }
207 f0cbd3ec bellard
                tcpstat.tcps_rexmttimeo++;
208 f0cbd3ec bellard
                rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
209 f0cbd3ec bellard
                TCPT_RANGESET(tp->t_rxtcur, rexmt,
210 f0cbd3ec bellard
                    (short)tp->t_rttmin, TCPTV_REXMTMAX); /* XXX */
211 f0cbd3ec bellard
                tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
212 f0cbd3ec bellard
                /*
213 f0cbd3ec bellard
                 * If losing, let the lower level know and try for
214 f0cbd3ec bellard
                 * a better route.  Also, if we backed off this far,
215 f0cbd3ec bellard
                 * our srtt estimate is probably bogus.  Clobber it
216 f0cbd3ec bellard
                 * so we'll take the next rtt measurement as our srtt;
217 f0cbd3ec bellard
                 * move the current srtt into rttvar to keep the current
218 f0cbd3ec bellard
                 * retransmit times until then.
219 f0cbd3ec bellard
                 */
220 f0cbd3ec bellard
                if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
221 f0cbd3ec bellard
/*                        in_losing(tp->t_inpcb); */
222 f0cbd3ec bellard
                        tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
223 f0cbd3ec bellard
                        tp->t_srtt = 0;
224 f0cbd3ec bellard
                }
225 f0cbd3ec bellard
                tp->snd_nxt = tp->snd_una;
226 f0cbd3ec bellard
                /*
227 f0cbd3ec bellard
                 * If timing a segment in this window, stop the timer.
228 f0cbd3ec bellard
                 */
229 f0cbd3ec bellard
                tp->t_rtt = 0;
230 f0cbd3ec bellard
                /*
231 f0cbd3ec bellard
                 * Close the congestion window down to one segment
232 f0cbd3ec bellard
                 * (we'll open it by one segment for each ack we get).
233 f0cbd3ec bellard
                 * Since we probably have a window's worth of unacked
234 f0cbd3ec bellard
                 * data accumulated, this "slow start" keeps us from
235 f0cbd3ec bellard
                 * dumping all that data as back-to-back packets (which
236 f0cbd3ec bellard
                 * might overwhelm an intermediate gateway).
237 f0cbd3ec bellard
                 *
238 f0cbd3ec bellard
                 * There are two phases to the opening: Initially we
239 f0cbd3ec bellard
                 * open by one mss on each ack.  This makes the window
240 f0cbd3ec bellard
                 * size increase exponentially with time.  If the
241 f0cbd3ec bellard
                 * window is larger than the path can handle, this
242 f0cbd3ec bellard
                 * exponential growth results in dropped packet(s)
243 5fafdf24 ths
                 * almost immediately.  To get more time between
244 f0cbd3ec bellard
                 * drops but still "push" the network to take advantage
245 f0cbd3ec bellard
                 * of improving conditions, we switch from exponential
246 f0cbd3ec bellard
                 * to linear window opening at some threshold size.
247 f0cbd3ec bellard
                 * For a threshold, we use half the current window
248 f0cbd3ec bellard
                 * size, truncated to a multiple of the mss.
249 f0cbd3ec bellard
                 *
250 f0cbd3ec bellard
                 * (the minimum cwnd that will give us exponential
251 f0cbd3ec bellard
                 * growth is 2 mss.  We don't allow the threshold
252 f0cbd3ec bellard
                 * to go below this.)
253 f0cbd3ec bellard
                 */
254 f0cbd3ec bellard
                {
255 f0cbd3ec bellard
                u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
256 f0cbd3ec bellard
                if (win < 2)
257 f0cbd3ec bellard
                        win = 2;
258 f0cbd3ec bellard
                tp->snd_cwnd = tp->t_maxseg;
259 f0cbd3ec bellard
                tp->snd_ssthresh = win * tp->t_maxseg;
260 f0cbd3ec bellard
                tp->t_dupacks = 0;
261 f0cbd3ec bellard
                }
262 f0cbd3ec bellard
                (void) tcp_output(tp);
263 f0cbd3ec bellard
                break;
264 f0cbd3ec bellard
265 f0cbd3ec bellard
        /*
266 f0cbd3ec bellard
         * Persistence timer into zero window.
267 f0cbd3ec bellard
         * Force a byte to be output, if possible.
268 f0cbd3ec bellard
         */
269 f0cbd3ec bellard
        case TCPT_PERSIST:
270 f0cbd3ec bellard
                tcpstat.tcps_persisttimeo++;
271 f0cbd3ec bellard
                tcp_setpersist(tp);
272 f0cbd3ec bellard
                tp->t_force = 1;
273 f0cbd3ec bellard
                (void) tcp_output(tp);
274 f0cbd3ec bellard
                tp->t_force = 0;
275 f0cbd3ec bellard
                break;
276 f0cbd3ec bellard
277 f0cbd3ec bellard
        /*
278 f0cbd3ec bellard
         * Keep-alive timer went off; send something
279 f0cbd3ec bellard
         * or drop connection if idle for too long.
280 f0cbd3ec bellard
         */
281 f0cbd3ec bellard
        case TCPT_KEEP:
282 f0cbd3ec bellard
                tcpstat.tcps_keeptimeo++;
283 f0cbd3ec bellard
                if (tp->t_state < TCPS_ESTABLISHED)
284 f0cbd3ec bellard
                        goto dropit;
285 f0cbd3ec bellard
286 f0cbd3ec bellard
/*                if (tp->t_socket->so_options & SO_KEEPALIVE && */
287 f0cbd3ec bellard
                if ((so_options) && tp->t_state <= TCPS_CLOSE_WAIT) {
288 f0cbd3ec bellard
                            if (tp->t_idle >= tcp_keepidle + tcp_maxidle)
289 f0cbd3ec bellard
                                goto dropit;
290 f0cbd3ec bellard
                        /*
291 f0cbd3ec bellard
                         * Send a packet designed to force a response
292 f0cbd3ec bellard
                         * if the peer is up and reachable:
293 f0cbd3ec bellard
                         * either an ACK if the connection is still alive,
294 f0cbd3ec bellard
                         * or an RST if the peer has closed the connection
295 f0cbd3ec bellard
                         * due to timeout or reboot.
296 f0cbd3ec bellard
                         * Using sequence number tp->snd_una-1
297 f0cbd3ec bellard
                         * causes the transmitted zero-length segment
298 f0cbd3ec bellard
                         * to lie outside the receive window;
299 f0cbd3ec bellard
                         * by the protocol spec, this requires the
300 f0cbd3ec bellard
                         * correspondent TCP to respond.
301 f0cbd3ec bellard
                         */
302 f0cbd3ec bellard
                        tcpstat.tcps_keepprobe++;
303 f0cbd3ec bellard
#ifdef TCP_COMPAT_42
304 f0cbd3ec bellard
                        /*
305 f0cbd3ec bellard
                         * The keepalive packet must have nonzero length
306 f0cbd3ec bellard
                         * to get a 4.2 host to respond.
307 f0cbd3ec bellard
                         */
308 f0cbd3ec bellard
                        tcp_respond(tp, &tp->t_template, (struct mbuf *)NULL,
309 f0cbd3ec bellard
                            tp->rcv_nxt - 1, tp->snd_una - 1, 0);
310 f0cbd3ec bellard
#else
311 f0cbd3ec bellard
                        tcp_respond(tp, &tp->t_template, (struct mbuf *)NULL,
312 f0cbd3ec bellard
                            tp->rcv_nxt, tp->snd_una - 1, 0);
313 f0cbd3ec bellard
#endif
314 f0cbd3ec bellard
                        tp->t_timer[TCPT_KEEP] = tcp_keepintvl;
315 f0cbd3ec bellard
                } else
316 f0cbd3ec bellard
                        tp->t_timer[TCPT_KEEP] = tcp_keepidle;
317 f0cbd3ec bellard
                break;
318 f0cbd3ec bellard
319 f0cbd3ec bellard
        dropit:
320 f0cbd3ec bellard
                tcpstat.tcps_keepdrops++;
321 f0cbd3ec bellard
                tp = tcp_drop(tp, 0); /* ETIMEDOUT); */
322 f0cbd3ec bellard
                break;
323 f0cbd3ec bellard
        }
324 f0cbd3ec bellard
325 f0cbd3ec bellard
        return (tp);
326 f0cbd3ec bellard
}