Statistics
| Branch: | Revision:

root / slirp / tcp_timer.c @ dc5d0b3d

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