]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/net/bsd_tcpip/v2_0/src/sys/netinet/tcp_timer.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / net / bsd_tcpip / v2_0 / src / sys / netinet / tcp_timer.c
1 //==========================================================================
2 //
3 //      src/sys/netinet/tcp_timer.c
4 //
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
7 //
8 // -------------------------------------------
9 //
10 // Portions of this software may have been derived from OpenBSD, 
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
13 //
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
16 //
17 // -------------------------------------------
18 //
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
21
22 /*
23  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
24  *      The Regents of the University of California.  All rights reserved.
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions
28  * are met:
29  * 1. Redistributions of source code must retain the above copyright
30  *    notice, this list of conditions and the following disclaimer.
31  * 2. Redistributions in binary form must reproduce the above copyright
32  *    notice, this list of conditions and the following disclaimer in the
33  *    documentation and/or other materials provided with the distribution.
34  * 3. All advertising materials mentioning features or use of this software
35  *    must display the following acknowledgement:
36  *      This product includes software developed by the University of
37  *      California, Berkeley and its contributors.
38  * 4. Neither the name of the University nor the names of its contributors
39  *    may be used to endorse or promote products derived from this software
40  *    without specific prior written permission.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  *
54  *      @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95
55  * $FreeBSD: src/sys/netinet/tcp_timer.c,v 1.34.2.11 2001/08/22 00:59:12 silby Exp $
56  */
57
58 #include <sys/param.h>
59 #include <sys/sysctl.h>
60 #include <sys/mbuf.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
63 #include <sys/protosw.h>
64
65 #include <net/route.h>
66
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/in_pcb.h>
70 #ifdef INET6
71 #include <netinet6/in6_pcb.h>
72 #endif
73 #include <netinet/ip_var.h>
74 #include <netinet/tcp.h>
75 #include <netinet/tcp_fsm.h>
76 #include <netinet/tcp_seq.h>
77 #include <netinet/tcp_timer.h>
78 #include <netinet/tcp_var.h>
79 #include <netinet/tcpip.h>
80 #ifdef TCPDEBUG
81 #include <netinet/tcp_debug.h>
82 #endif
83
84 #ifdef CYGPKG_NET_FREEBSD_SYSCTL
85 static int
86 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
87 {
88         int error, s, tt;
89
90         tt = *(int *)oidp->oid_arg1;
91         s = tt * 1000 / hz;
92
93         error = sysctl_handle_int(oidp, &s, 0, req);
94         if (error || !req->newptr)
95                 return (error);
96
97         tt = s * hz / 1000;
98         if (tt < 1)
99                 return (EINVAL);
100
101         *(int *)oidp->oid_arg1 = tt;
102         return (0);
103 }
104 static int
105 sysctl_rexmitmax_rangecheck(SYSCTL_HANDLER_ARGS)
106 {
107         int error, tt;
108
109         tt = *(int *)oidp->oid_arg1;
110
111         error = sysctl_handle_int(oidp, &tt, 0, req);
112         if (error || !req->newptr)
113                 return (error);
114                 
115         if (tt > TCP_MAXRXTSHIFT)
116                 return (EINVAL);
117                 
118         if (tt < 2)
119                 return (EINVAL);
120
121         *(int *)oidp->oid_arg1 = tt;
122         return (0);
123 }
124 #endif
125 int     tcp_keepinit;
126 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
127     &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "");
128
129 int     tcp_keepidle;
130 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
131     &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "");
132
133 int     tcp_keepintvl;
134 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
135     &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "");
136
137 int     tcp_delacktime;
138 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime,
139     CTLTYPE_INT|CTLFLAG_RW, &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
140     "Time before a delayed ACK is sent");
141  
142 int     tcp_msl;
143 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
144     &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
145
146 static int      always_keepalive = 0;
147 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW, 
148     &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
149     
150 int     tcp_rexmit_min;
151 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
152     &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I", "Minimum retransmission timeout");
153     
154 int     tcp_rexmit_shift_max = TCP_MAXRXTSHIFT;
155 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_shift_max, CTLTYPE_INT|CTLFLAG_RW,
156     &tcp_rexmit_shift_max, 0, sysctl_rexmitmax_rangecheck, "I", "Maximum TCP retransmissions");    
157
158 static int      tcp_keepcnt = TCPTV_KEEPCNT;
159         /* max idle probes */
160 int     tcp_maxpersistidle;
161         /* max idle time in persist */
162 int     tcp_maxidle;
163
164 /*
165  * Tcp protocol timeout routine called every 500 ms.
166  * Updates timestamps used for TCP
167  * causes finite state machine actions if timers expire.
168  */
169 void
170 tcp_slowtimo()
171 {
172         int s;
173
174         s = splnet();
175
176         tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
177
178         splx(s);
179 }
180
181 /*
182  * Cancel all timers for TCP tp.
183  */
184 void
185 tcp_canceltimers(tp)
186         struct tcpcb *tp;
187 {
188         callout_stop(tp->tt_2msl);
189         callout_stop(tp->tt_persist);
190         callout_stop(tp->tt_keep);
191         callout_stop(tp->tt_rexmt);
192 }
193
194 int     tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
195     { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
196
197 int     tcp_backoff[TCP_MAXRXTSHIFT + 1] =
198     { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
199
200 static int tcp_totbackoff = 511;        /* sum of tcp_backoff[] */
201
202 /*
203  * TCP timer processing.
204  */
205 void
206 tcp_timer_delack(xtp)
207         void *xtp;
208 {
209         struct tcpcb *tp = xtp;
210         int s;
211
212         s = splnet();
213         if (callout_pending(tp->tt_delack) || !callout_active(tp->tt_delack)) {
214                 splx(s);
215                 return;
216         }
217         callout_deactivate(tp->tt_delack);
218
219         tp->t_flags |= TF_ACKNOW;
220         tcpstat.tcps_delack++;
221         (void) tcp_output(tp);
222         splx(s);
223 }
224
225 void
226 tcp_timer_2msl(xtp)
227         void *xtp;
228 {
229         struct tcpcb *tp = xtp;
230         int s;
231 #ifdef TCPDEBUG
232         int ostate;
233
234         ostate = tp->t_state;
235 #endif
236         s = splnet();
237         if (callout_pending(tp->tt_2msl) || !callout_active(tp->tt_2msl)) {
238                 splx(s);
239                 return;
240         }
241         callout_deactivate(tp->tt_2msl);
242         /*
243          * 2 MSL timeout in shutdown went off.  If we're closed but
244          * still waiting for peer to close and connection has been idle
245          * too long, or if 2MSL time is up from TIME_WAIT, delete connection
246          * control block.  Otherwise, check again in a bit.
247          */
248         if (tp->t_state != TCPS_TIME_WAIT &&
249             (ticks - tp->t_rcvtime) <= tcp_maxidle) {
250                 callout_reset(tp->tt_2msl, tcp_keepintvl,
251                               tcp_timer_2msl, tp);
252         }
253         else {
254                 tp = tcp_close(tp);
255         }
256
257 #ifdef TCPDEBUG
258         if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
259                 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
260                           PRU_SLOWTIMO);
261 #endif
262         splx(s);
263 }
264
265 void
266 tcp_timer_keep(xtp)
267         void *xtp;
268 {
269         struct tcpcb *tp = xtp;
270         struct tcptemp *t_template;
271         int s;
272 #ifdef TCPDEBUG
273         int ostate;
274
275         ostate = tp->t_state;
276 #endif
277         s = splnet();
278         if (callout_pending(tp->tt_keep) || !callout_active(tp->tt_keep)) {
279                 splx(s);
280                 return;
281         }
282         callout_deactivate(tp->tt_keep);
283         /*
284          * Keep-alive timer went off; send something
285          * or drop connection if idle for too long.
286          */
287         tcpstat.tcps_keeptimeo++;
288         if (tp->t_state < TCPS_ESTABLISHED)
289                 goto dropit;
290         if ((always_keepalive ||
291              tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) &&
292             tp->t_state <= TCPS_CLOSING) {
293                 if ((ticks - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle)
294                         goto dropit;
295                 /*
296                  * Send a packet designed to force a response
297                  * if the peer is up and reachable:
298                  * either an ACK if the connection is still alive,
299                  * or an RST if the peer has closed the connection
300                  * due to timeout or reboot.
301                  * Using sequence number tp->snd_una-1
302                  * causes the transmitted zero-length segment
303                  * to lie outside the receive window;
304                  * by the protocol spec, this requires the
305                  * correspondent TCP to respond.
306                  */
307                 tcpstat.tcps_keepprobe++;
308                 t_template = tcp_maketemplate(tp);
309                 if (t_template) {
310                         tcp_respond(tp, t_template->tt_ipgen,
311                                     &t_template->tt_t, (struct mbuf *)NULL,
312                                     tp->rcv_nxt, tp->snd_una - 1, 0);
313                         (void) m_free(dtom(t_template));
314                 }
315                 callout_reset(tp->tt_keep, tcp_keepintvl, tcp_timer_keep, tp);
316         } else
317                 callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
318
319 #ifdef TCPDEBUG
320         if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
321                 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
322                           PRU_SLOWTIMO);
323 #endif
324         splx(s);
325         return;
326
327 dropit:
328         tcpstat.tcps_keepdrops++;
329         tp = tcp_drop(tp, ETIMEDOUT);
330
331 #ifdef TCPDEBUG
332         if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
333                 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
334                           PRU_SLOWTIMO);
335 #endif
336         splx(s);
337 }
338
339 void
340 tcp_timer_persist(xtp)
341         void *xtp;
342 {
343         struct tcpcb *tp = xtp;
344         int s;
345 #ifdef TCPDEBUG
346         int ostate;
347
348         ostate = tp->t_state;
349 #endif
350         s = splnet();
351         if (callout_pending(tp->tt_persist) || !callout_active(tp->tt_persist)){
352                 splx(s);
353                 return;
354         }
355         callout_deactivate(tp->tt_persist);
356         /*
357          * Persistance timer into zero window.
358          * Force a byte to be output, if possible.
359          */
360         tcpstat.tcps_persisttimeo++;
361         /*
362          * Hack: if the peer is dead/unreachable, we do not
363          * time out if the window is closed.  After a full
364          * backoff, drop the connection if the idle time
365          * (no responses to probes) reaches the maximum
366          * backoff that we would use if retransmitting.
367          */
368         if (tp->t_rxtshift == tcp_rexmit_shift_max &&
369             ((ticks - tp->t_rcvtime) >= tcp_maxpersistidle ||
370              (ticks - tp->t_rcvtime) >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
371                 tcpstat.tcps_persistdrop++;
372                 tp = tcp_drop(tp, ETIMEDOUT);
373                 goto out;
374         }
375         tcp_setpersist(tp);
376         tp->t_force = 1;
377         (void) tcp_output(tp);
378         tp->t_force = 0;
379
380 out:
381 #ifdef TCPDEBUG
382         if (tp && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
383                 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
384                           PRU_SLOWTIMO);
385 #endif
386         splx(s);
387 }
388
389 void
390 tcp_timer_rexmt(xtp)
391         void *xtp;
392 {
393         struct tcpcb *tp = xtp;
394         int s;
395         int rexmt;
396 #ifdef TCPDEBUG
397         int ostate;
398
399         ostate = tp->t_state;
400 #endif
401         s = splnet();
402         if (callout_pending(tp->tt_rexmt) || !callout_active(tp->tt_rexmt)) {
403                 splx(s);
404                 return;
405         }
406         callout_deactivate(tp->tt_rexmt);
407         /*
408          * Retransmission timer went off.  Message has not
409          * been acked within retransmit interval.  Back off
410          * to a longer retransmit interval and retransmit one segment.
411          */
412         if (++tp->t_rxtshift > tcp_rexmit_shift_max) {
413                 tp->t_rxtshift = tcp_rexmit_shift_max;
414                 tcpstat.tcps_timeoutdrop++;
415                 tp = tcp_drop(tp, tp->t_softerror ?
416                               tp->t_softerror : ETIMEDOUT);
417                 goto out;
418         }
419         if (tp->t_rxtshift == 1) {
420                 /*
421                  * first retransmit; record ssthresh and cwnd so they can
422                  * be recovered if this turns out to be a "bad" retransmit.
423                  * A retransmit is considered "bad" if an ACK for this 
424                  * segment is received within RTT/2 interval; the assumption
425                  * here is that the ACK was already in flight.  See 
426                  * "On Estimating End-to-End Network Path Properties" by
427                  * Allman and Paxson for more details.
428                  */
429                 tp->snd_cwnd_prev = tp->snd_cwnd;
430                 tp->snd_ssthresh_prev = tp->snd_ssthresh;
431                 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
432         }
433         tcpstat.tcps_rexmttimeo++;
434         if (tp->t_state == TCPS_SYN_SENT)
435                 rexmt = TCP_REXMTVAL(tp) * tcp_syn_backoff[tp->t_rxtshift];
436         else
437                 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
438         TCPT_RANGESET(tp->t_rxtcur, rexmt,
439                       tp->t_rttmin, TCPTV_REXMTMAX);
440         /*
441          * Disable rfc1323 and rfc1644 if we havn't got any response to
442          * our third SYN to work-around some broken terminal servers 
443          * (most of which have hopefully been retired) that have bad VJ 
444          * header compression code which trashes TCP segments containing 
445          * unknown-to-them TCP options.
446          */
447         if ((tp->t_state == TCPS_SYN_SENT) && (tp->t_rxtshift == 3))
448                 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_REQ_CC);
449         /*
450          * If losing, let the lower level know and try for
451          * a better route.  Also, if we backed off this far,
452          * our srtt estimate is probably bogus.  Clobber it
453          * so we'll take the next rtt measurement as our srtt;
454          * move the current srtt into rttvar to keep the current
455          * retransmit times until then.
456          */
457         if (tp->t_rxtshift > tcp_rexmit_shift_max / 4) {
458 #ifdef INET6
459                 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
460                         in6_losing(tp->t_inpcb);
461                 else
462 #endif
463                 in_losing(tp->t_inpcb);
464                 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
465                 tp->t_srtt = 0;
466         }
467         tp->snd_nxt = tp->snd_una;
468         /*
469          * Note:  We overload snd_recover to function also as the
470          * snd_last variable described in RFC 2582
471          */
472         tp->snd_recover = tp->snd_max;
473         /*
474          * Force a segment to be sent.
475          */
476         tp->t_flags |= TF_ACKNOW;
477         /*
478          * If timing a segment in this window, stop the timer.
479          */
480         tp->t_rtttime = 0;
481         /*
482          * Close the congestion window down to one segment
483          * (we'll open it by one segment for each ack we get).
484          * Since we probably have a window's worth of unacked
485          * data accumulated, this "slow start" keeps us from
486          * dumping all that data as back-to-back packets (which
487          * might overwhelm an intermediate gateway).
488          *
489          * There are two phases to the opening: Initially we
490          * open by one mss on each ack.  This makes the window
491          * size increase exponentially with time.  If the
492          * window is larger than the path can handle, this
493          * exponential growth results in dropped packet(s)
494          * almost immediately.  To get more time between
495          * drops but still "push" the network to take advantage
496          * of improving conditions, we switch from exponential
497          * to linear window opening at some threshhold size.
498          * For a threshhold, we use half the current window
499          * size, truncated to a multiple of the mss.
500          *
501          * (the minimum cwnd that will give us exponential
502          * growth is 2 mss.  We don't allow the threshhold
503          * to go below this.)
504          */
505         {
506                 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
507                 if (win < 2)
508                         win = 2;
509                 tp->snd_cwnd = tp->t_maxseg;
510                 tp->snd_ssthresh = win * tp->t_maxseg;
511                 tp->t_dupacks = 0;
512         }
513         (void) tcp_output(tp);
514
515 out:
516 #ifdef TCPDEBUG
517         if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
518                 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
519                           PRU_SLOWTIMO);
520 #endif
521         splx(s);
522 }