11eaf0ac3Slogwang /*- 2*d4a07e70Sfengbojiang * SPDX-License-Identifier: BSD-3-Clause 3*d4a07e70Sfengbojiang * 41eaf0ac3Slogwang * Copyright (c) 1982, 1986, 1993 51eaf0ac3Slogwang * The Regents of the University of California. All rights reserved. 61eaf0ac3Slogwang * 71eaf0ac3Slogwang * Redistribution and use in source and binary forms, with or without 81eaf0ac3Slogwang * modification, are permitted provided that the following conditions 91eaf0ac3Slogwang * are met: 101eaf0ac3Slogwang * 1. Redistributions of source code must retain the above copyright 111eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer. 121eaf0ac3Slogwang * 2. Redistributions in binary form must reproduce the above copyright 131eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer in the 141eaf0ac3Slogwang * documentation and/or other materials provided with the distribution. 15*d4a07e70Sfengbojiang * 3. Neither the name of the University nor the names of its contributors 161eaf0ac3Slogwang * may be used to endorse or promote products derived from this software 171eaf0ac3Slogwang * without specific prior written permission. 181eaf0ac3Slogwang * 191eaf0ac3Slogwang * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 201eaf0ac3Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 211eaf0ac3Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 221eaf0ac3Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 231eaf0ac3Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 241eaf0ac3Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 251eaf0ac3Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 261eaf0ac3Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 271eaf0ac3Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 281eaf0ac3Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 291eaf0ac3Slogwang * SUCH DAMAGE. 301eaf0ac3Slogwang * 311eaf0ac3Slogwang * @(#)tcp_timer.h 8.1 (Berkeley) 6/10/93 321eaf0ac3Slogwang * $FreeBSD$ 331eaf0ac3Slogwang */ 341eaf0ac3Slogwang 351eaf0ac3Slogwang #ifndef _NETINET_TCP_TIMER_H_ 361eaf0ac3Slogwang #define _NETINET_TCP_TIMER_H_ 371eaf0ac3Slogwang 381eaf0ac3Slogwang /* 391eaf0ac3Slogwang * The TCPT_REXMT timer is used to force retransmissions. 401eaf0ac3Slogwang * The TCP has the TCPT_REXMT timer set whenever segments 411eaf0ac3Slogwang * have been sent for which ACKs are expected but not yet 421eaf0ac3Slogwang * received. If an ACK is received which advances tp->snd_una, 431eaf0ac3Slogwang * then the retransmit timer is cleared (if there are no more 441eaf0ac3Slogwang * outstanding segments) or reset to the base value (if there 451eaf0ac3Slogwang * are more ACKs expected). Whenever the retransmit timer goes off, 461eaf0ac3Slogwang * we retransmit one unacknowledged segment, and do a backoff 471eaf0ac3Slogwang * on the retransmit timer. 481eaf0ac3Slogwang * 491eaf0ac3Slogwang * The TCPT_PERSIST timer is used to keep window size information 501eaf0ac3Slogwang * flowing even if the window goes shut. If all previous transmissions 511eaf0ac3Slogwang * have been acknowledged (so that there are no retransmissions in progress), 521eaf0ac3Slogwang * and the window is too small to bother sending anything, then we start 531eaf0ac3Slogwang * the TCPT_PERSIST timer. When it expires, if the window is nonzero, 541eaf0ac3Slogwang * we go to transmit state. Otherwise, at intervals send a single byte 551eaf0ac3Slogwang * into the peer's window to force him to update our window information. 561eaf0ac3Slogwang * We do this at most as often as TCPT_PERSMIN time intervals, 571eaf0ac3Slogwang * but no more frequently than the current estimate of round-trip 581eaf0ac3Slogwang * packet time. The TCPT_PERSIST timer is cleared whenever we receive 591eaf0ac3Slogwang * a window update from the peer. 601eaf0ac3Slogwang * 611eaf0ac3Slogwang * The TCPT_KEEP timer is used to keep connections alive. If an 621eaf0ac3Slogwang * connection is idle (no segments received) for TCPTV_KEEP_INIT amount of time, 631eaf0ac3Slogwang * but not yet established, then we drop the connection. Once the connection 641eaf0ac3Slogwang * is established, if the connection is idle for TCPTV_KEEP_IDLE time 651eaf0ac3Slogwang * (and keepalives have been enabled on the socket), we begin to probe 661eaf0ac3Slogwang * the connection. We force the peer to send us a segment by sending: 671eaf0ac3Slogwang * <SEQ=SND.UNA-1><ACK=RCV.NXT><CTL=ACK> 681eaf0ac3Slogwang * This segment is (deliberately) outside the window, and should elicit 691eaf0ac3Slogwang * an ack segment in response from the peer. If, despite the TCPT_KEEP 701eaf0ac3Slogwang * initiated segments we cannot elicit a response from a peer in TCPT_MAXIDLE 711eaf0ac3Slogwang * amount of time probing, then we drop the connection. 721eaf0ac3Slogwang */ 731eaf0ac3Slogwang 741eaf0ac3Slogwang /* 751eaf0ac3Slogwang * Time constants. 761eaf0ac3Slogwang */ 771eaf0ac3Slogwang #define TCPTV_MSL ( 30*hz) /* max seg lifetime (hah!) */ 781eaf0ac3Slogwang #define TCPTV_SRTTBASE 0 /* base roundtrip time; 791eaf0ac3Slogwang if 0, no idea yet */ 80*d4a07e70Sfengbojiang #define TCPTV_RTOBASE ( 1*hz) /* assumed RTO if no info */ 811eaf0ac3Slogwang 821eaf0ac3Slogwang #define TCPTV_PERSMIN ( 5*hz) /* minimum persist interval */ 831eaf0ac3Slogwang #define TCPTV_PERSMAX ( 60*hz) /* maximum persist interval */ 841eaf0ac3Slogwang 851eaf0ac3Slogwang #define TCPTV_KEEP_INIT ( 75*hz) /* initial connect keepalive */ 861eaf0ac3Slogwang #define TCPTV_KEEP_IDLE (120*60*hz) /* dflt time before probing */ 871eaf0ac3Slogwang #define TCPTV_KEEPINTVL ( 75*hz) /* default probe interval */ 881eaf0ac3Slogwang #define TCPTV_KEEPCNT 8 /* max probes before drop */ 891eaf0ac3Slogwang 901eaf0ac3Slogwang #define TCPTV_FINWAIT2_TIMEOUT (60*hz) /* FIN_WAIT_2 timeout if no receiver */ 911eaf0ac3Slogwang 921eaf0ac3Slogwang /* 931eaf0ac3Slogwang * Minimum retransmit timer is 3 ticks, for algorithmic stability. 941eaf0ac3Slogwang * TCPT_RANGESET() will add another TCPTV_CPU_VAR to deal with 951eaf0ac3Slogwang * the expected worst-case processing variances by the kernels 961eaf0ac3Slogwang * representing the end points. Such variances do not always show 971eaf0ac3Slogwang * up in the srtt because the timestamp is often calculated at 981eaf0ac3Slogwang * the interface rather then at the TCP layer. This value is 991eaf0ac3Slogwang * typically 50ms. However, it is also possible that delayed 1001eaf0ac3Slogwang * acks (typically 100ms) could create issues so we set the slop 1011eaf0ac3Slogwang * to 200ms to try to cover it. Note that, properly speaking, 1021eaf0ac3Slogwang * delayed-acks should not create a major issue for interactive 1031eaf0ac3Slogwang * environments which 'P'ush the last segment, at least as 1041eaf0ac3Slogwang * long as implementations do the required 'at least one ack 1051eaf0ac3Slogwang * for every two packets' for the non-interactive streaming case. 1061eaf0ac3Slogwang * (maybe the RTO calculation should use 2*RTT instead of RTT 1071eaf0ac3Slogwang * to handle the ack-every-other-packet case). 1081eaf0ac3Slogwang * 1091eaf0ac3Slogwang * The prior minimum of 1*hz (1 second) badly breaks throughput on any 1101eaf0ac3Slogwang * networks faster then a modem that has minor (e.g. 1%) packet loss. 1111eaf0ac3Slogwang */ 1121eaf0ac3Slogwang #define TCPTV_MIN ( hz/33 ) /* minimum allowable value */ 1131eaf0ac3Slogwang #define TCPTV_CPU_VAR ( hz/5 ) /* cpu variance allowed (200ms) */ 1141eaf0ac3Slogwang #define TCPTV_REXMTMAX ( 64*hz) /* max allowable REXMT value */ 1151eaf0ac3Slogwang 1161eaf0ac3Slogwang #define TCPTV_TWTRUNC 8 /* RTO factor to truncate TW */ 1171eaf0ac3Slogwang 1181eaf0ac3Slogwang #define TCP_LINGERTIME 120 /* linger at most 2 minutes */ 1191eaf0ac3Slogwang 1201eaf0ac3Slogwang #define TCP_MAXRXTSHIFT 12 /* maximum retransmits */ 1211eaf0ac3Slogwang 122*d4a07e70Sfengbojiang #define TCPTV_DELACK ( hz/25 ) /* 40ms timeout */ 123*d4a07e70Sfengbojiang 124*d4a07e70Sfengbojiang /* 125*d4a07e70Sfengbojiang * If we exceed this number of retransmits for a single segment, we'll consider 126*d4a07e70Sfengbojiang * the current srtt measurement no longer valid and will recalculate from 127*d4a07e70Sfengbojiang * scratch starting with the next ACK. 128*d4a07e70Sfengbojiang */ 129*d4a07e70Sfengbojiang #define TCP_RTT_INVALIDATE (TCP_MAXRXTSHIFT / 4) 1301eaf0ac3Slogwang 1311eaf0ac3Slogwang #ifdef TCPTIMERS 1321eaf0ac3Slogwang static const char *tcptimers[] = 1331eaf0ac3Slogwang { "REXMT", "PERSIST", "KEEP", "2MSL", "DELACK" }; 1341eaf0ac3Slogwang #endif 1351eaf0ac3Slogwang 1361eaf0ac3Slogwang /* 1371eaf0ac3Slogwang * Force a time value to be in a certain range. 1381eaf0ac3Slogwang */ 1391eaf0ac3Slogwang #define TCPT_RANGESET(tv, value, tvmin, tvmax) do { \ 1401eaf0ac3Slogwang (tv) = (value) + tcp_rexmit_slop; \ 1411eaf0ac3Slogwang if ((u_long)(tv) < (u_long)(tvmin)) \ 1421eaf0ac3Slogwang (tv) = (tvmin); \ 1431eaf0ac3Slogwang if ((u_long)(tv) > (u_long)(tvmax)) \ 1441eaf0ac3Slogwang (tv) = (tvmax); \ 1451eaf0ac3Slogwang } while(0) 1461eaf0ac3Slogwang 1471eaf0ac3Slogwang #ifdef _KERNEL 1481eaf0ac3Slogwang 1491eaf0ac3Slogwang struct xtcp_timer; 1501eaf0ac3Slogwang 1511eaf0ac3Slogwang struct tcp_timer { 1521eaf0ac3Slogwang struct callout tt_rexmt; /* retransmit timer */ 1531eaf0ac3Slogwang struct callout tt_persist; /* retransmit persistence */ 1541eaf0ac3Slogwang struct callout tt_keep; /* keepalive */ 1551eaf0ac3Slogwang struct callout tt_2msl; /* 2*msl TIME_WAIT timer */ 1561eaf0ac3Slogwang struct callout tt_delack; /* delayed ACK timer */ 1571eaf0ac3Slogwang uint32_t tt_flags; /* Timers flags */ 1581eaf0ac3Slogwang uint32_t tt_draincnt; /* Count being drained */ 1591eaf0ac3Slogwang }; 1601eaf0ac3Slogwang 1611eaf0ac3Slogwang /* 1621eaf0ac3Slogwang * Flags for the tt_flags field. 1631eaf0ac3Slogwang */ 1641eaf0ac3Slogwang #define TT_DELACK 0x0001 1651eaf0ac3Slogwang #define TT_REXMT 0x0002 1661eaf0ac3Slogwang #define TT_PERSIST 0x0004 1671eaf0ac3Slogwang #define TT_KEEP 0x0008 1681eaf0ac3Slogwang #define TT_2MSL 0x0010 1691eaf0ac3Slogwang #define TT_MASK (TT_DELACK|TT_REXMT|TT_PERSIST|TT_KEEP|TT_2MSL) 1701eaf0ac3Slogwang 171*d4a07e70Sfengbojiang /* 172*d4a07e70Sfengbojiang * Suspend flags - used when suspending a timer 173*d4a07e70Sfengbojiang * from ever running again. 174*d4a07e70Sfengbojiang */ 175*d4a07e70Sfengbojiang #define TT_DELACK_SUS 0x0100 176*d4a07e70Sfengbojiang #define TT_REXMT_SUS 0x0200 177*d4a07e70Sfengbojiang #define TT_PERSIST_SUS 0x0400 178*d4a07e70Sfengbojiang #define TT_KEEP_SUS 0x0800 179*d4a07e70Sfengbojiang #define TT_2MSL_SUS 0x1000 1801eaf0ac3Slogwang 1811eaf0ac3Slogwang #define TT_STOPPED 0x00010000 1821eaf0ac3Slogwang 1831eaf0ac3Slogwang #define TP_KEEPINIT(tp) ((tp)->t_keepinit ? (tp)->t_keepinit : tcp_keepinit) 1841eaf0ac3Slogwang #define TP_KEEPIDLE(tp) ((tp)->t_keepidle ? (tp)->t_keepidle : tcp_keepidle) 1851eaf0ac3Slogwang #define TP_KEEPINTVL(tp) ((tp)->t_keepintvl ? (tp)->t_keepintvl : tcp_keepintvl) 1861eaf0ac3Slogwang #define TP_KEEPCNT(tp) ((tp)->t_keepcnt ? (tp)->t_keepcnt : tcp_keepcnt) 1871eaf0ac3Slogwang #define TP_MAXIDLE(tp) (TP_KEEPCNT(tp) * TP_KEEPINTVL(tp)) 1881eaf0ac3Slogwang 1891eaf0ac3Slogwang extern int tcp_persmin; /* minimum persist interval */ 1901eaf0ac3Slogwang extern int tcp_persmax; /* maximum persist interval */ 1911eaf0ac3Slogwang extern int tcp_keepinit; /* time to establish connection */ 1921eaf0ac3Slogwang extern int tcp_keepidle; /* time before keepalive probes begin */ 1931eaf0ac3Slogwang extern int tcp_keepintvl; /* time between keepalive probes */ 1941eaf0ac3Slogwang extern int tcp_keepcnt; /* number of keepalives */ 1951eaf0ac3Slogwang extern int tcp_delacktime; /* time before sending a delayed ACK */ 1961eaf0ac3Slogwang extern int tcp_maxpersistidle; 197*d4a07e70Sfengbojiang extern int tcp_rexmit_initial; 1981eaf0ac3Slogwang extern int tcp_rexmit_min; 1991eaf0ac3Slogwang extern int tcp_rexmit_slop; 2001eaf0ac3Slogwang extern int tcp_msl; 2011eaf0ac3Slogwang extern int tcp_ttl; /* time to live for TCP segs */ 2021eaf0ac3Slogwang extern int tcp_backoff[]; 203*d4a07e70Sfengbojiang extern int tcp_totbackoff; 204*d4a07e70Sfengbojiang extern int tcp_rexmit_drop_options; 2051eaf0ac3Slogwang 2061eaf0ac3Slogwang extern int tcp_finwait2_timeout; 2071eaf0ac3Slogwang extern int tcp_fast_finwait2_recycle; 2081eaf0ac3Slogwang 209*d4a07e70Sfengbojiang VNET_DECLARE(int, tcp_always_keepalive); 210*d4a07e70Sfengbojiang #define V_tcp_always_keepalive VNET(tcp_always_keepalive) 211*d4a07e70Sfengbojiang VNET_DECLARE(int, tcp_pmtud_blackhole_detect); 212*d4a07e70Sfengbojiang #define V_tcp_pmtud_blackhole_detect VNET(tcp_pmtud_blackhole_detect) 213*d4a07e70Sfengbojiang VNET_DECLARE(int, tcp_pmtud_blackhole_mss); 214*d4a07e70Sfengbojiang #define V_tcp_pmtud_blackhole_mss VNET(tcp_pmtud_blackhole_mss) 215*d4a07e70Sfengbojiang VNET_DECLARE(int, tcp_v6pmtud_blackhole_mss); 216*d4a07e70Sfengbojiang #define V_tcp_v6pmtud_blackhole_mss VNET(tcp_v6pmtud_blackhole_mss) 217*d4a07e70Sfengbojiang 218*d4a07e70Sfengbojiang void tcp_inpinfo_lock_del(struct inpcb *inp, struct tcpcb *tp); 219*d4a07e70Sfengbojiang 2201eaf0ac3Slogwang void tcp_timer_init(void); 2211eaf0ac3Slogwang void tcp_timer_2msl(void *xtp); 2221eaf0ac3Slogwang void tcp_timer_discard(void *); 2231eaf0ac3Slogwang struct tcptw * 2241eaf0ac3Slogwang tcp_tw_2msl_scan(int reuse); /* XXX temporary? */ 2251eaf0ac3Slogwang void tcp_timer_keep(void *xtp); 2261eaf0ac3Slogwang void tcp_timer_persist(void *xtp); 2271eaf0ac3Slogwang void tcp_timer_rexmt(void *xtp); 2281eaf0ac3Slogwang void tcp_timer_delack(void *xtp); 2291eaf0ac3Slogwang 2301eaf0ac3Slogwang #endif /* _KERNEL */ 2311eaf0ac3Slogwang 2321eaf0ac3Slogwang #endif /* !_NETINET_TCP_TIMER_H_ */ 233