xref: /f-stack/freebsd/netinet/tcp_timer.h (revision 22ce4aff)
1a9643ea8Slogwang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: BSD-3-Clause
3*22ce4affSfengbojiang  *
4a9643ea8Slogwang  * Copyright (c) 1982, 1986, 1993
5a9643ea8Slogwang  *	The Regents of the University of California.  All rights reserved.
6a9643ea8Slogwang  *
7a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
8a9643ea8Slogwang  * modification, are permitted provided that the following conditions
9a9643ea8Slogwang  * are met:
10a9643ea8Slogwang  * 1. Redistributions of source code must retain the above copyright
11a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer.
12a9643ea8Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
13a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer in the
14a9643ea8Slogwang  *    documentation and/or other materials provided with the distribution.
15*22ce4affSfengbojiang  * 3. Neither the name of the University nor the names of its contributors
16a9643ea8Slogwang  *    may be used to endorse or promote products derived from this software
17a9643ea8Slogwang  *    without specific prior written permission.
18a9643ea8Slogwang  *
19a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20a9643ea8Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21a9643ea8Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22a9643ea8Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23a9643ea8Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24a9643ea8Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25a9643ea8Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26a9643ea8Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27a9643ea8Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28a9643ea8Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29a9643ea8Slogwang  * SUCH DAMAGE.
30a9643ea8Slogwang  *
31a9643ea8Slogwang  *	@(#)tcp_timer.h	8.1 (Berkeley) 6/10/93
32a9643ea8Slogwang  * $FreeBSD$
33a9643ea8Slogwang  */
34a9643ea8Slogwang 
35a9643ea8Slogwang #ifndef _NETINET_TCP_TIMER_H_
36a9643ea8Slogwang #define _NETINET_TCP_TIMER_H_
37a9643ea8Slogwang 
38a9643ea8Slogwang /*
39a9643ea8Slogwang  * The TCPT_REXMT timer is used to force retransmissions.
40a9643ea8Slogwang  * The TCP has the TCPT_REXMT timer set whenever segments
41a9643ea8Slogwang  * have been sent for which ACKs are expected but not yet
42a9643ea8Slogwang  * received.  If an ACK is received which advances tp->snd_una,
43a9643ea8Slogwang  * then the retransmit timer is cleared (if there are no more
44a9643ea8Slogwang  * outstanding segments) or reset to the base value (if there
45a9643ea8Slogwang  * are more ACKs expected).  Whenever the retransmit timer goes off,
46a9643ea8Slogwang  * we retransmit one unacknowledged segment, and do a backoff
47a9643ea8Slogwang  * on the retransmit timer.
48a9643ea8Slogwang  *
49a9643ea8Slogwang  * The TCPT_PERSIST timer is used to keep window size information
50a9643ea8Slogwang  * flowing even if the window goes shut.  If all previous transmissions
51a9643ea8Slogwang  * have been acknowledged (so that there are no retransmissions in progress),
52a9643ea8Slogwang  * and the window is too small to bother sending anything, then we start
53a9643ea8Slogwang  * the TCPT_PERSIST timer.  When it expires, if the window is nonzero,
54a9643ea8Slogwang  * we go to transmit state.  Otherwise, at intervals send a single byte
55a9643ea8Slogwang  * into the peer's window to force him to update our window information.
56a9643ea8Slogwang  * We do this at most as often as TCPT_PERSMIN time intervals,
57a9643ea8Slogwang  * but no more frequently than the current estimate of round-trip
58a9643ea8Slogwang  * packet time.  The TCPT_PERSIST timer is cleared whenever we receive
59a9643ea8Slogwang  * a window update from the peer.
60a9643ea8Slogwang  *
61a9643ea8Slogwang  * The TCPT_KEEP timer is used to keep connections alive.  If an
62a9643ea8Slogwang  * connection is idle (no segments received) for TCPTV_KEEP_INIT amount of time,
63a9643ea8Slogwang  * but not yet established, then we drop the connection.  Once the connection
64a9643ea8Slogwang  * is established, if the connection is idle for TCPTV_KEEP_IDLE time
65a9643ea8Slogwang  * (and keepalives have been enabled on the socket), we begin to probe
66a9643ea8Slogwang  * the connection.  We force the peer to send us a segment by sending:
67a9643ea8Slogwang  *	<SEQ=SND.UNA-1><ACK=RCV.NXT><CTL=ACK>
68a9643ea8Slogwang  * This segment is (deliberately) outside the window, and should elicit
69a9643ea8Slogwang  * an ack segment in response from the peer.  If, despite the TCPT_KEEP
70a9643ea8Slogwang  * initiated segments we cannot elicit a response from a peer in TCPT_MAXIDLE
71a9643ea8Slogwang  * amount of time probing, then we drop the connection.
72a9643ea8Slogwang  */
73a9643ea8Slogwang 
74a9643ea8Slogwang /*
75a9643ea8Slogwang  * Time constants.
76a9643ea8Slogwang  */
77a9643ea8Slogwang #define	TCPTV_MSL	( 30*hz)		/* max seg lifetime (hah!) */
78a9643ea8Slogwang #define	TCPTV_SRTTBASE	0			/* base roundtrip time;
79a9643ea8Slogwang 						   if 0, no idea yet */
80*22ce4affSfengbojiang #define	TCPTV_RTOBASE	(  1*hz)		/* assumed RTO if no info */
81a9643ea8Slogwang 
82a9643ea8Slogwang #define	TCPTV_PERSMIN	(  5*hz)		/* minimum persist interval */
83a9643ea8Slogwang #define	TCPTV_PERSMAX	( 60*hz)		/* maximum persist interval */
84a9643ea8Slogwang 
85a9643ea8Slogwang #define	TCPTV_KEEP_INIT	( 75*hz)		/* initial connect keepalive */
86a9643ea8Slogwang #define	TCPTV_KEEP_IDLE	(120*60*hz)		/* dflt time before probing */
87a9643ea8Slogwang #define	TCPTV_KEEPINTVL	( 75*hz)		/* default probe interval */
88a9643ea8Slogwang #define	TCPTV_KEEPCNT	8			/* max probes before drop */
89a9643ea8Slogwang 
90a9643ea8Slogwang #define TCPTV_FINWAIT2_TIMEOUT (60*hz)         /* FIN_WAIT_2 timeout if no receiver */
91a9643ea8Slogwang 
92a9643ea8Slogwang /*
93a9643ea8Slogwang  * Minimum retransmit timer is 3 ticks, for algorithmic stability.
94a9643ea8Slogwang  * TCPT_RANGESET() will add another TCPTV_CPU_VAR to deal with
95a9643ea8Slogwang  * the expected worst-case processing variances by the kernels
96a9643ea8Slogwang  * representing the end points.  Such variances do not always show
97a9643ea8Slogwang  * up in the srtt because the timestamp is often calculated at
98a9643ea8Slogwang  * the interface rather then at the TCP layer.  This value is
99a9643ea8Slogwang  * typically 50ms.  However, it is also possible that delayed
100a9643ea8Slogwang  * acks (typically 100ms) could create issues so we set the slop
101a9643ea8Slogwang  * to 200ms to try to cover it.  Note that, properly speaking,
102a9643ea8Slogwang  * delayed-acks should not create a major issue for interactive
103a9643ea8Slogwang  * environments which 'P'ush the last segment, at least as
104a9643ea8Slogwang  * long as implementations do the required 'at least one ack
105a9643ea8Slogwang  * for every two packets' for the non-interactive streaming case.
106a9643ea8Slogwang  * (maybe the RTO calculation should use 2*RTT instead of RTT
107a9643ea8Slogwang  * to handle the ack-every-other-packet case).
108a9643ea8Slogwang  *
109a9643ea8Slogwang  * The prior minimum of 1*hz (1 second) badly breaks throughput on any
110a9643ea8Slogwang  * networks faster then a modem that has minor (e.g. 1%) packet loss.
111a9643ea8Slogwang  */
112a9643ea8Slogwang #define	TCPTV_MIN	( hz/33 )		/* minimum allowable value */
113a9643ea8Slogwang #define TCPTV_CPU_VAR	( hz/5 )		/* cpu variance allowed (200ms) */
114a9643ea8Slogwang #define	TCPTV_REXMTMAX	( 64*hz)		/* max allowable REXMT value */
115a9643ea8Slogwang 
116a9643ea8Slogwang #define TCPTV_TWTRUNC	8			/* RTO factor to truncate TW */
117a9643ea8Slogwang 
118a9643ea8Slogwang #define	TCP_LINGERTIME	120			/* linger at most 2 minutes */
119a9643ea8Slogwang 
120a9643ea8Slogwang #define	TCP_MAXRXTSHIFT	12			/* maximum retransmits */
121a9643ea8Slogwang 
122*22ce4affSfengbojiang #define	TCPTV_DELACK	( hz/25 )		/* 40ms timeout */
123*22ce4affSfengbojiang 
124*22ce4affSfengbojiang /*
125*22ce4affSfengbojiang  * If we exceed this number of retransmits for a single segment, we'll consider
126*22ce4affSfengbojiang  * the current srtt measurement no longer valid and will recalculate from
127*22ce4affSfengbojiang  * scratch starting with the next ACK.
128*22ce4affSfengbojiang  */
129*22ce4affSfengbojiang #define TCP_RTT_INVALIDATE (TCP_MAXRXTSHIFT / 4)
130a9643ea8Slogwang 
131a9643ea8Slogwang #ifdef	TCPTIMERS
132a9643ea8Slogwang static const char *tcptimers[] =
133a9643ea8Slogwang     { "REXMT", "PERSIST", "KEEP", "2MSL", "DELACK" };
134a9643ea8Slogwang #endif
135a9643ea8Slogwang 
136a9643ea8Slogwang /*
137a9643ea8Slogwang  * Force a time value to be in a certain range.
138a9643ea8Slogwang  */
139a9643ea8Slogwang #define	TCPT_RANGESET(tv, value, tvmin, tvmax) do { \
140a9643ea8Slogwang 	(tv) = (value) + tcp_rexmit_slop; \
141a9643ea8Slogwang 	if ((u_long)(tv) < (u_long)(tvmin)) \
142a9643ea8Slogwang 		(tv) = (tvmin); \
143a9643ea8Slogwang 	if ((u_long)(tv) > (u_long)(tvmax)) \
144a9643ea8Slogwang 		(tv) = (tvmax); \
145a9643ea8Slogwang } while(0)
146a9643ea8Slogwang 
147a9643ea8Slogwang #ifdef _KERNEL
148a9643ea8Slogwang 
149a9643ea8Slogwang struct xtcp_timer;
150a9643ea8Slogwang 
151a9643ea8Slogwang struct tcp_timer {
152a9643ea8Slogwang 	struct	callout tt_rexmt;	/* retransmit timer */
153a9643ea8Slogwang 	struct	callout tt_persist;	/* retransmit persistence */
154a9643ea8Slogwang 	struct	callout tt_keep;	/* keepalive */
155a9643ea8Slogwang 	struct	callout tt_2msl;	/* 2*msl TIME_WAIT timer */
156a9643ea8Slogwang 	struct	callout tt_delack;	/* delayed ACK timer */
157a9643ea8Slogwang 	uint32_t	tt_flags;	/* Timers flags */
158a9643ea8Slogwang 	uint32_t	tt_draincnt;	/* Count being drained */
159a9643ea8Slogwang };
160a9643ea8Slogwang 
161a9643ea8Slogwang /*
162a9643ea8Slogwang  * Flags for the tt_flags field.
163a9643ea8Slogwang  */
164a9643ea8Slogwang #define TT_DELACK	0x0001
165a9643ea8Slogwang #define TT_REXMT	0x0002
166a9643ea8Slogwang #define TT_PERSIST	0x0004
167a9643ea8Slogwang #define TT_KEEP		0x0008
168a9643ea8Slogwang #define TT_2MSL		0x0010
169a9643ea8Slogwang #define TT_MASK		(TT_DELACK|TT_REXMT|TT_PERSIST|TT_KEEP|TT_2MSL)
170a9643ea8Slogwang 
171*22ce4affSfengbojiang /*
172*22ce4affSfengbojiang  * Suspend flags - used when suspending a timer
173*22ce4affSfengbojiang  * from ever running again.
174*22ce4affSfengbojiang  */
175*22ce4affSfengbojiang #define TT_DELACK_SUS	0x0100
176*22ce4affSfengbojiang #define TT_REXMT_SUS	0x0200
177*22ce4affSfengbojiang #define TT_PERSIST_SUS	0x0400
178*22ce4affSfengbojiang #define TT_KEEP_SUS	0x0800
179*22ce4affSfengbojiang #define TT_2MSL_SUS	0x1000
180a9643ea8Slogwang 
181a9643ea8Slogwang #define TT_STOPPED	0x00010000
182a9643ea8Slogwang 
183a9643ea8Slogwang #define	TP_KEEPINIT(tp)	((tp)->t_keepinit ? (tp)->t_keepinit : tcp_keepinit)
184a9643ea8Slogwang #define	TP_KEEPIDLE(tp)	((tp)->t_keepidle ? (tp)->t_keepidle : tcp_keepidle)
185a9643ea8Slogwang #define	TP_KEEPINTVL(tp) ((tp)->t_keepintvl ? (tp)->t_keepintvl : tcp_keepintvl)
186a9643ea8Slogwang #define	TP_KEEPCNT(tp)	((tp)->t_keepcnt ? (tp)->t_keepcnt : tcp_keepcnt)
187a9643ea8Slogwang #define	TP_MAXIDLE(tp)	(TP_KEEPCNT(tp) * TP_KEEPINTVL(tp))
188a9643ea8Slogwang 
189a9643ea8Slogwang extern int tcp_persmin;			/* minimum persist interval */
190a9643ea8Slogwang extern int tcp_persmax;			/* maximum persist interval */
191a9643ea8Slogwang extern int tcp_keepinit;		/* time to establish connection */
192a9643ea8Slogwang extern int tcp_keepidle;		/* time before keepalive probes begin */
193a9643ea8Slogwang extern int tcp_keepintvl;		/* time between keepalive probes */
194a9643ea8Slogwang extern int tcp_keepcnt;			/* number of keepalives */
195a9643ea8Slogwang extern int tcp_delacktime;		/* time before sending a delayed ACK */
196a9643ea8Slogwang extern int tcp_maxpersistidle;
197*22ce4affSfengbojiang extern int tcp_rexmit_initial;
198a9643ea8Slogwang extern int tcp_rexmit_min;
199a9643ea8Slogwang extern int tcp_rexmit_slop;
200a9643ea8Slogwang extern int tcp_msl;
201a9643ea8Slogwang extern int tcp_ttl;			/* time to live for TCP segs */
202a9643ea8Slogwang extern int tcp_backoff[];
203*22ce4affSfengbojiang extern int tcp_totbackoff;
204*22ce4affSfengbojiang extern int tcp_rexmit_drop_options;
205a9643ea8Slogwang 
206a9643ea8Slogwang extern int tcp_finwait2_timeout;
207a9643ea8Slogwang extern int tcp_fast_finwait2_recycle;
208a9643ea8Slogwang 
209*22ce4affSfengbojiang VNET_DECLARE(int, tcp_always_keepalive);
210*22ce4affSfengbojiang #define	V_tcp_always_keepalive		VNET(tcp_always_keepalive)
211*22ce4affSfengbojiang VNET_DECLARE(int, tcp_pmtud_blackhole_detect);
212*22ce4affSfengbojiang #define V_tcp_pmtud_blackhole_detect	VNET(tcp_pmtud_blackhole_detect)
213*22ce4affSfengbojiang VNET_DECLARE(int, tcp_pmtud_blackhole_mss);
214*22ce4affSfengbojiang #define	V_tcp_pmtud_blackhole_mss	VNET(tcp_pmtud_blackhole_mss)
215*22ce4affSfengbojiang VNET_DECLARE(int, tcp_v6pmtud_blackhole_mss);
216*22ce4affSfengbojiang #define V_tcp_v6pmtud_blackhole_mss	VNET(tcp_v6pmtud_blackhole_mss)
217*22ce4affSfengbojiang 
218*22ce4affSfengbojiang void tcp_inpinfo_lock_del(struct inpcb *inp, struct tcpcb *tp);
219*22ce4affSfengbojiang 
220a9643ea8Slogwang void	tcp_timer_init(void);
221a9643ea8Slogwang void	tcp_timer_2msl(void *xtp);
222a9643ea8Slogwang void	tcp_timer_discard(void *);
223a9643ea8Slogwang struct tcptw *
224a9643ea8Slogwang 	tcp_tw_2msl_scan(int reuse);	/* XXX temporary? */
225a9643ea8Slogwang void	tcp_timer_keep(void *xtp);
226a9643ea8Slogwang void	tcp_timer_persist(void *xtp);
227a9643ea8Slogwang void	tcp_timer_rexmt(void *xtp);
228a9643ea8Slogwang void	tcp_timer_delack(void *xtp);
229a9643ea8Slogwang 
230a9643ea8Slogwang #endif /* _KERNEL */
231a9643ea8Slogwang 
232a9643ea8Slogwang #endif /* !_NETINET_TCP_TIMER_H_ */
233