Statistics
| Branch: | Revision:

root / slirp / tcp_timer.c @ 9f4facbc

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