xref: /netbsd-src/sys/netinet/tcp_timer.h (revision 4952945bc9cb52c2df497fce04918dc9b58b3838)
1*4952945bSriastradh /*	$NetBSD: tcp_timer.h,v 1.30 2019/08/06 15:48:18 riastradh Exp $	*/
24745c7f2Sthorpej 
34745c7f2Sthorpej /*-
45640dcbbSmycroft  * Copyright (c) 2001, 2005 The NetBSD Foundation, Inc.
54745c7f2Sthorpej  * All rights reserved.
64745c7f2Sthorpej  *
74745c7f2Sthorpej  * This code is derived from software contributed to The NetBSD Foundation
84745c7f2Sthorpej  * by Jason R. Thorpe of Wasabi Systems, Inc.
95640dcbbSmycroft  * This code is derived from software contributed to The NetBSD Foundation
105640dcbbSmycroft  * by Charles M. Hannum.
114745c7f2Sthorpej  *
124745c7f2Sthorpej  * Redistribution and use in source and binary forms, with or without
134745c7f2Sthorpej  * modification, are permitted provided that the following conditions
144745c7f2Sthorpej  * are met:
154745c7f2Sthorpej  * 1. Redistributions of source code must retain the above copyright
164745c7f2Sthorpej  *    notice, this list of conditions and the following disclaimer.
174745c7f2Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
184745c7f2Sthorpej  *    notice, this list of conditions and the following disclaimer in the
194745c7f2Sthorpej  *    documentation and/or other materials provided with the distribution.
204745c7f2Sthorpej  *
214745c7f2Sthorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
224745c7f2Sthorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
234745c7f2Sthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
244745c7f2Sthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
254745c7f2Sthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
264745c7f2Sthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
274745c7f2Sthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
284745c7f2Sthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
294745c7f2Sthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
304745c7f2Sthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
314745c7f2Sthorpej  * POSSIBILITY OF SUCH DAMAGE.
324745c7f2Sthorpej  */
33cf92afd6Scgd 
3461f28255Scgd /*
3507b4f2abSmycroft  * Copyright (c) 1982, 1986, 1993
3607b4f2abSmycroft  *	The Regents of the University of California.  All rights reserved.
3761f28255Scgd  *
3861f28255Scgd  * Redistribution and use in source and binary forms, with or without
3961f28255Scgd  * modification, are permitted provided that the following conditions
4061f28255Scgd  * are met:
4161f28255Scgd  * 1. Redistributions of source code must retain the above copyright
4261f28255Scgd  *    notice, this list of conditions and the following disclaimer.
4361f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
4461f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
4561f28255Scgd  *    documentation and/or other materials provided with the distribution.
46aad01611Sagc  * 3. Neither the name of the University nor the names of its contributors
4761f28255Scgd  *    may be used to endorse or promote products derived from this software
4861f28255Scgd  *    without specific prior written permission.
4961f28255Scgd  *
5061f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5161f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5261f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5361f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5461f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5561f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5661f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5761f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5861f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5961f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6061f28255Scgd  * SUCH DAMAGE.
6161f28255Scgd  *
62cf92afd6Scgd  *	@(#)tcp_timer.h	8.1 (Berkeley) 6/10/93
6361f28255Scgd  */
6461f28255Scgd 
65f73530baSperry #ifndef _NETINET_TCP_TIMER_H_
66f73530baSperry #define _NETINET_TCP_TIMER_H_
67f73530baSperry 
6861f28255Scgd /*
6961f28255Scgd  * Definitions of the TCP timers.  These timers are counted
7061f28255Scgd  * down PR_SLOWHZ times a second.
7161f28255Scgd  */
7261f28255Scgd #define	TCPT_NTIMERS	4
7361f28255Scgd 
7461f28255Scgd #define	TCPT_REXMT	0		/* retransmit */
7561f28255Scgd #define	TCPT_PERSIST	1		/* retransmit persistance */
7661f28255Scgd #define	TCPT_KEEP	2		/* keep alive */
7761f28255Scgd #define	TCPT_2MSL	3		/* 2*msl quiet time timer */
7861f28255Scgd 
7961f28255Scgd /*
8061f28255Scgd  * The TCPT_REXMT timer is used to force retransmissions.
8161f28255Scgd  * The TCP has the TCPT_REXMT timer set whenever segments
8261f28255Scgd  * have been sent for which ACKs are expected but not yet
8361f28255Scgd  * received.  If an ACK is received which advances tp->snd_una,
8461f28255Scgd  * then the retransmit timer is cleared (if there are no more
8561f28255Scgd  * outstanding segments) or reset to the base value (if there
8661f28255Scgd  * are more ACKs expected).  Whenever the retransmit timer goes off,
8761f28255Scgd  * we retransmit one unacknowledged segment, and do a backoff
8861f28255Scgd  * on the retransmit timer.
8961f28255Scgd  *
9061f28255Scgd  * The TCPT_PERSIST timer is used to keep window size information
9161f28255Scgd  * flowing even if the window goes shut.  If all previous transmissions
9261f28255Scgd  * have been acknowledged (so that there are no retransmissions in progress),
9361f28255Scgd  * and the window is too small to bother sending anything, then we start
9461f28255Scgd  * the TCPT_PERSIST timer.  When it expires, if the window is nonzero,
9561f28255Scgd  * we go to transmit state.  Otherwise, at intervals send a single byte
9661f28255Scgd  * into the peer's window to force him to update our window information.
9761f28255Scgd  * We do this at most as often as TCPT_PERSMIN time intervals,
9861f28255Scgd  * but no more frequently than the current estimate of round-trip
9961f28255Scgd  * packet time.  The TCPT_PERSIST timer is cleared whenever we receive
10061f28255Scgd  * a window update from the peer.
10161f28255Scgd  *
10261f28255Scgd  * The TCPT_KEEP timer is used to keep connections alive.  If an
10361f28255Scgd  * connection is idle (no segments received) for TCPTV_KEEP_INIT amount of time,
10461f28255Scgd  * but not yet established, then we drop the connection.  Once the connection
10561f28255Scgd  * is established, if the connection is idle for TCPTV_KEEP_IDLE time
10661f28255Scgd  * (and keepalives have been enabled on the socket), we begin to probe
10761f28255Scgd  * the connection.  We force the peer to send us a segment by sending:
10861f28255Scgd  *	<SEQ=SND.UNA-1><ACK=RCV.NXT><CTL=ACK>
10961f28255Scgd  * This segment is (deliberately) outside the window, and should elicit
11061f28255Scgd  * an ack segment in response from the peer.  If, despite the TCPT_KEEP
11161f28255Scgd  * initiated segments we cannot elicit a response from a peer in TCPT_MAXIDLE
11261f28255Scgd  * amount of time probing, then we drop the connection.
11361f28255Scgd  */
11461f28255Scgd 
11561f28255Scgd /*
11661f28255Scgd  * Time constants.
117f641bea5Sgdt  * All TCPTV_* constants are in units of slow ticks (typically 500 ms).
11861f28255Scgd  */
11961f28255Scgd #define	TCPTV_MSL	( 30*PR_SLOWHZ)		/* max seg lifetime (hah!) */
12061f28255Scgd #define	TCPTV_SRTTBASE	0			/* base roundtrip time;
12161f28255Scgd 						   if 0, no idea yet */
12261f28255Scgd #define	TCPTV_SRTTDFLT	(  3*PR_SLOWHZ)		/* assumed RTT if no info */
12361f28255Scgd 
12461f28255Scgd #define	TCPTV_PERSMIN	(  5*PR_SLOWHZ)		/* retransmit persistance */
12561f28255Scgd #define	TCPTV_PERSMAX	( 60*PR_SLOWHZ)		/* maximum persist interval */
12661f28255Scgd 
12761f28255Scgd #define	TCPTV_KEEP_INIT	( 75*PR_SLOWHZ)		/* initial connect keep alive */
12861f28255Scgd #define	TCPTV_KEEP_IDLE	(120*60*PR_SLOWHZ)	/* dflt time before probing */
12961f28255Scgd #define	TCPTV_KEEPINTVL	( 75*PR_SLOWHZ)		/* default probe interval */
13061f28255Scgd #define	TCPTV_KEEPCNT	8			/* max probes before drop */
13161f28255Scgd 
13261f28255Scgd #define	TCPTV_MIN	(  1*PR_SLOWHZ)		/* minimum allowable value */
13361f28255Scgd #define	TCPTV_REXMTMAX	( 64*PR_SLOWHZ)		/* max allowable REXMT value */
13461f28255Scgd 
13561f28255Scgd #define	TCP_LINGERTIME	120			/* linger at most 2 minutes */
13661f28255Scgd 
13761f28255Scgd #define	TCP_MAXRXTSHIFT	12			/* maximum retransmits */
13861f28255Scgd 
1390ca69791Sgdt /* Acks are delayed for 1 second; constant is in fast ticks. */
140783db900Sthorpej #define	TCP_DELACK_TICKS (hz / PR_FASTHZ)	/* time to delay ACK */
141783db900Sthorpej 
14261f28255Scgd #ifdef	TCPTIMERS
143c8636820Sriz const char *tcptimers[] =
14461f28255Scgd     { "REXMT", "PERSIST", "KEEP", "2MSL" };
14561f28255Scgd #endif
14661f28255Scgd 
14761f28255Scgd /*
1483d9c4277Sthorpej  * Init, arm, disarm, and test TCP timers.
14934e34c98Sthorpej  */
1503d9c4277Sthorpej #define	TCP_TIMER_INIT(tp, timer)					\
1512ee9a875Sthorpej 	callout_setfunc(&(tp)->t_timer[(timer)],			\
1522ee9a875Sthorpej 	    tcp_timer_funcs[(timer)], (tp))
1533d9c4277Sthorpej 
154f641bea5Sgdt /*
155f641bea5Sgdt  * nticks is given in units of slow timeouts,
156f641bea5Sgdt  * typically 500 ms (with PR_SLOWHZ at 2).
157f641bea5Sgdt  */
15834e34c98Sthorpej #define	TCP_TIMER_ARM(tp, timer, nticks)				\
1592ee9a875Sthorpej 	callout_schedule(&(tp)->t_timer[(timer)],			\
1602ee9a875Sthorpej 	    (nticks) * (hz / PR_SLOWHZ))
16134e34c98Sthorpej 
16234e34c98Sthorpej #define	TCP_TIMER_DISARM(tp, timer)					\
1636d0e813fSthorpej 	callout_stop(&(tp)->t_timer[(timer)])
16434e34c98Sthorpej 
16534e34c98Sthorpej #define	TCP_TIMER_ISARMED(tp, timer)					\
1665640dcbbSmycroft 	callout_active(&(tp)->t_timer[(timer)])
16734e34c98Sthorpej 
168*4952945bSriastradh #define	TCP_TIMER_MAXTICKS						      \
169*4952945bSriastradh 	(INT_MAX / (hz / PR_SLOWHZ))
170*4952945bSriastradh 
171*4952945bSriastradh #define	TCP_MAXMSL							      \
172*4952945bSriastradh 	(TCP_TIMER_MAXTICKS / 2)
173*4952945bSriastradh 
17434e34c98Sthorpej /*
17561f28255Scgd  * Force a time value to be in a certain range.
17661f28255Scgd  */
17761f28255Scgd #define	TCPT_RANGESET(tv, value, tvmin, tvmax) { \
17861f28255Scgd 	(tv) = (value); \
17961f28255Scgd 	if ((tv) < (tvmin)) \
18061f28255Scgd 		(tv) = (tvmin); \
181b91b1cdbSjeremy 	if ((tv) > (tvmax)) \
18261f28255Scgd 		(tv) = (tvmax); \
18361f28255Scgd }
18461f28255Scgd 
1857c042338Sjtc #ifdef _KERNEL
18645e02f5eSthorpej typedef void (*tcp_timer_func_t)(void *);
18745e02f5eSthorpej 
18847577dcaSmatt extern const tcp_timer_func_t tcp_timer_funcs[TCPT_NTIMERS];
18945e02f5eSthorpej 
190eeff1895Schristos extern u_int tcp_keepinit;		/* time before initial connection times out */
191eeff1895Schristos extern u_int tcp_keepidle;		/* time before keepalive probes begin */
192eeff1895Schristos extern u_int tcp_keepintvl;		/* time between keepalive probes */
193eeff1895Schristos extern u_int tcp_keepcnt;		/* number of keepalives, 0=infty */
194413e5cb8Sthorpej extern int tcp_maxpersistidle;		/* max idle time in persist */
19561f28255Scgd extern int tcp_ttl;			/* time to live for TCP segs */
19647577dcaSmatt extern const int tcp_backoff[];
197413e5cb8Sthorpej 
198413e5cb8Sthorpej void	tcp_timer_init(void);
199be75d649Sozaki-r void	tcp_slowtimo_init(void);
20061f28255Scgd #endif
201f73530baSperry 
2029702e987Selad #endif /* !_NETINET_TCP_TIMER_H_ */
203