tcp_timer.h revision 287304
177298Sobrien/*-
277298Sobrien * Copyright (c) 1982, 1986, 1993
378828Sobrien *	The Regents of the University of California.  All rights reserved.
477298Sobrien *
577298Sobrien * Redistribution and use in source and binary forms, with or without
677298Sobrien * modification, are permitted provided that the following conditions
777298Sobrien * are met:
877298Sobrien * 1. Redistributions of source code must retain the above copyright
977298Sobrien *    notice, this list of conditions and the following disclaimer.
1077298Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1177298Sobrien *    notice, this list of conditions and the following disclaimer in the
1277298Sobrien *    documentation and/or other materials provided with the distribution.
1377298Sobrien * 4. Neither the name of the University nor the names of its contributors
1477298Sobrien *    may be used to endorse or promote products derived from this software
1577298Sobrien *    without specific prior written permission.
1677298Sobrien *
1777298Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1877298Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19218822Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2077298Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2177298Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2277298Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2377298Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2477298Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2577298Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2677298Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2777298Sobrien * SUCH DAMAGE.
2877298Sobrien *
2977298Sobrien *	@(#)tcp_timer.h	8.1 (Berkeley) 6/10/93
30218822Sdim * $FreeBSD: head/sys/netinet/tcp_timer.h 287304 2015-08-30 13:44:39Z jch $
31218822Sdim */
3277298Sobrien
3377298Sobrien#ifndef _NETINET_TCP_TIMER_H_
3477298Sobrien#define _NETINET_TCP_TIMER_H_
3577298Sobrien
3677298Sobrien/*
3777298Sobrien * The TCPT_REXMT timer is used to force retransmissions.
3877298Sobrien * The TCP has the TCPT_REXMT timer set whenever segments
3977298Sobrien * have been sent for which ACKs are expected but not yet
4077298Sobrien * received.  If an ACK is received which advances tp->snd_una,
4177298Sobrien * then the retransmit timer is cleared (if there are no more
42218822Sdim * outstanding segments) or reset to the base value (if there
4377298Sobrien * are more ACKs expected).  Whenever the retransmit timer goes off,
4477298Sobrien * we retransmit one unacknowledged segment, and do a backoff
4577298Sobrien * on the retransmit timer.
4677298Sobrien *
4777298Sobrien * The TCPT_PERSIST timer is used to keep window size information
4877298Sobrien * flowing even if the window goes shut.  If all previous transmissions
4977298Sobrien * have been acknowledged (so that there are no retransmissions in progress),
5077298Sobrien * and the window is too small to bother sending anything, then we start
5177298Sobrien * the TCPT_PERSIST timer.  When it expires, if the window is nonzero,
5277298Sobrien * we go to transmit state.  Otherwise, at intervals send a single byte
5377298Sobrien * into the peer's window to force him to update our window information.
5477298Sobrien * We do this at most as often as TCPT_PERSMIN time intervals,
5577298Sobrien * but no more frequently than the current estimate of round-trip
5677298Sobrien * packet time.  The TCPT_PERSIST timer is cleared whenever we receive
5777298Sobrien * a window update from the peer.
5877298Sobrien *
5977298Sobrien * The TCPT_KEEP timer is used to keep connections alive.  If an
6077298Sobrien * connection is idle (no segments received) for TCPTV_KEEP_INIT amount of time,
6177298Sobrien * but not yet established, then we drop the connection.  Once the connection
6277298Sobrien * is established, if the connection is idle for TCPTV_KEEP_IDLE time
6377298Sobrien * (and keepalives have been enabled on the socket), we begin to probe
6477298Sobrien * the connection.  We force the peer to send us a segment by sending:
6577298Sobrien *	<SEQ=SND.UNA-1><ACK=RCV.NXT><CTL=ACK>
6677298Sobrien * This segment is (deliberately) outside the window, and should elicit
6777298Sobrien * an ack segment in response from the peer.  If, despite the TCPT_KEEP
6877298Sobrien * initiated segments we cannot elicit a response from a peer in TCPT_MAXIDLE
6977298Sobrien * amount of time probing, then we drop the connection.
70218822Sdim */
71218822Sdim
72218822Sdim/*
7377298Sobrien * Time constants.
7477298Sobrien */
7577298Sobrien#define	TCPTV_MSL	( 30*hz)		/* max seg lifetime (hah!) */
7677298Sobrien#define	TCPTV_SRTTBASE	0			/* base roundtrip time;
7777298Sobrien						   if 0, no idea yet */
7877298Sobrien#define	TCPTV_RTOBASE	(  3*hz)		/* assumed RTO if no info */
7977298Sobrien
8077298Sobrien#define	TCPTV_PERSMIN	(  5*hz)		/* retransmit persistence */
8177298Sobrien#define	TCPTV_PERSMAX	( 60*hz)		/* maximum persist interval */
8277298Sobrien
8377298Sobrien#define	TCPTV_KEEP_INIT	( 75*hz)		/* initial connect keepalive */
8477298Sobrien#define	TCPTV_KEEP_IDLE	(120*60*hz)		/* dflt time before probing */
85218822Sdim#define	TCPTV_KEEPINTVL	( 75*hz)		/* default probe interval */
8677298Sobrien#define	TCPTV_KEEPCNT	8			/* max probes before drop */
8777298Sobrien
8877298Sobrien#define TCPTV_FINWAIT2_TIMEOUT (60*hz)         /* FIN_WAIT_2 timeout if no receiver */
8977298Sobrien
9077298Sobrien/*
9177298Sobrien * Minimum retransmit timer is 3 ticks, for algorithmic stability.
9277298Sobrien * TCPT_RANGESET() will add another TCPTV_CPU_VAR to deal with
9377298Sobrien * the expected worst-case processing variances by the kernels
9477298Sobrien * representing the end points.  Such variances do not always show
95218822Sdim * up in the srtt because the timestamp is often calculated at
9677298Sobrien * the interface rather then at the TCP layer.  This value is
9777298Sobrien * typically 50ms.  However, it is also possible that delayed
9877298Sobrien * acks (typically 100ms) could create issues so we set the slop
9977298Sobrien * to 200ms to try to cover it.  Note that, properly speaking,
10077298Sobrien * delayed-acks should not create a major issue for interactive
101218822Sdim * environments which 'P'ush the last segment, at least as
102218822Sdim * long as implementations do the required 'at least one ack
10377298Sobrien * for every two packets' for the non-interactive streaming case.
10477298Sobrien * (maybe the RTO calculation should use 2*RTT instead of RTT
10577298Sobrien * to handle the ack-every-other-packet case).
10677298Sobrien *
10777298Sobrien * The prior minimum of 1*hz (1 second) badly breaks throughput on any
108218822Sdim * networks faster then a modem that has minor (e.g. 1%) packet loss.
109218822Sdim */
11077298Sobrien#define	TCPTV_MIN	( hz/33 )		/* minimum allowable value */
11177298Sobrien#define TCPTV_CPU_VAR	( hz/5 )		/* cpu variance allowed (200ms) */
11277298Sobrien#define	TCPTV_REXMTMAX	( 64*hz)		/* max allowable REXMT value */
11377298Sobrien
11477298Sobrien#define TCPTV_TWTRUNC	8			/* RTO factor to truncate TW */
11577298Sobrien
11677298Sobrien#define	TCP_LINGERTIME	120			/* linger at most 2 minutes */
11777298Sobrien
118218822Sdim#define	TCP_MAXRXTSHIFT	12			/* maximum retransmits */
11977298Sobrien
12077298Sobrien#define	TCPTV_DELACK	( hz/10 )		/* 100ms timeout */
12177298Sobrien
12277298Sobrien#ifdef	TCPTIMERS
12377298Sobrienstatic const char *tcptimers[] =
12477298Sobrien    { "REXMT", "PERSIST", "KEEP", "2MSL", "DELACK" };
12577298Sobrien#endif
12677298Sobrien
127218822Sdim/*
12877298Sobrien * Force a time value to be in a certain range.
12977298Sobrien */
13077298Sobrien#define	TCPT_RANGESET(tv, value, tvmin, tvmax) do { \
13177298Sobrien	(tv) = (value) + tcp_rexmit_slop; \
13277298Sobrien	if ((u_long)(tv) < (u_long)(tvmin)) \
133218822Sdim		(tv) = (tvmin); \
13477298Sobrien	if ((u_long)(tv) > (u_long)(tvmax)) \
13577298Sobrien		(tv) = (tvmax); \
13677298Sobrien} while(0)
13777298Sobrien
13877298Sobrien#ifdef _KERNEL
139218822Sdim
14077298Sobrienstruct xtcp_timer;
14177298Sobrien
142struct tcp_timer {
143	struct	callout tt_rexmt;	/* retransmit timer */
144	struct	callout tt_persist;	/* retransmit persistence */
145	struct	callout tt_keep;	/* keepalive */
146	struct	callout tt_2msl;	/* 2*msl TIME_WAIT timer */
147	struct	callout tt_delack;	/* delayed ACK timer */
148	uint32_t	tt_flags;	/* Timers flags */
149	uint32_t	tt_spare;	/* TDB */
150};
151
152/*
153 * Flags for the tt_flags field.
154 */
155#define TT_DELACK	0x0001
156#define TT_REXMT	0x0002
157#define TT_PERSIST	0x0004
158#define TT_KEEP		0x0008
159#define TT_2MSL		0x0010
160#define TT_MASK		(TT_DELACK|TT_REXMT|TT_PERSIST|TT_KEEP|TT_2MSL)
161
162#define TT_DELACK_RST	0x0100
163#define TT_REXMT_RST	0x0200
164#define TT_PERSIST_RST	0x0400
165#define TT_KEEP_RST	0x0800
166#define TT_2MSL_RST	0x1000
167
168#define TT_STOPPED	0x00010000
169
170#define	TP_KEEPINIT(tp)	((tp)->t_keepinit ? (tp)->t_keepinit : tcp_keepinit)
171#define	TP_KEEPIDLE(tp)	((tp)->t_keepidle ? (tp)->t_keepidle : tcp_keepidle)
172#define	TP_KEEPINTVL(tp) ((tp)->t_keepintvl ? (tp)->t_keepintvl : tcp_keepintvl)
173#define	TP_KEEPCNT(tp)	((tp)->t_keepcnt ? (tp)->t_keepcnt : tcp_keepcnt)
174#define	TP_MAXIDLE(tp)	(TP_KEEPCNT(tp) * TP_KEEPINTVL(tp))
175
176extern int tcp_keepinit;		/* time to establish connection */
177extern int tcp_keepidle;		/* time before keepalive probes begin */
178extern int tcp_keepintvl;		/* time between keepalive probes */
179extern int tcp_keepcnt;			/* number of keepalives */
180extern int tcp_delacktime;		/* time before sending a delayed ACK */
181extern int tcp_maxpersistidle;
182extern int tcp_rexmit_min;
183extern int tcp_rexmit_slop;
184extern int tcp_msl;
185extern int tcp_ttl;			/* time to live for TCP segs */
186extern int tcp_backoff[];
187extern int tcp_syn_backoff[];
188
189extern int tcp_finwait2_timeout;
190extern int tcp_fast_finwait2_recycle;
191
192void	tcp_timer_init(void);
193void	tcp_timer_2msl(void *xtp);
194struct tcptw *
195	tcp_tw_2msl_scan(int reuse);	/* XXX temporary? */
196void	tcp_timer_keep(void *xtp);
197void	tcp_timer_persist(void *xtp);
198void	tcp_timer_rexmt(void *xtp);
199void	tcp_timer_delack(void *xtp);
200void	tcp_timer_2msl_discard(void *xtp);
201void	tcp_timer_keep_discard(void *xtp);
202void	tcp_timer_persist_discard(void *xtp);
203void	tcp_timer_rexmt_discard(void *xtp);
204void	tcp_timer_delack_discard(void *xtp);
205void	tcp_timer_to_xtimer(struct tcpcb *tp, struct tcp_timer *timer,
206	struct xtcp_timer *xtimer);
207
208#endif /* _KERNEL */
209
210#endif /* !_NETINET_TCP_TIMER_H_ */
211