1c398230bSWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 9df8bae1dSRodney W. Grimes * are met: 10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 17df8bae1dSRodney W. Grimes * without specific prior written permission. 18df8bae1dSRodney W. Grimes * 19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29df8bae1dSRodney W. Grimes * SUCH DAMAGE. 30df8bae1dSRodney W. Grimes * 31df8bae1dSRodney W. Grimes * @(#)tcp_timer.h 8.1 (Berkeley) 6/10/93 32c3aac50fSPeter Wemm * $FreeBSD$ 33df8bae1dSRodney W. Grimes */ 34df8bae1dSRodney W. Grimes 35707f139eSPaul Richards #ifndef _NETINET_TCP_TIMER_H_ 36707f139eSPaul Richards #define _NETINET_TCP_TIMER_H_ 37707f139eSPaul Richards 38df8bae1dSRodney W. Grimes /* 39df8bae1dSRodney W. Grimes * The TCPT_REXMT timer is used to force retransmissions. 40df8bae1dSRodney W. Grimes * The TCP has the TCPT_REXMT timer set whenever segments 41df8bae1dSRodney W. Grimes * have been sent for which ACKs are expected but not yet 42df8bae1dSRodney W. Grimes * received. If an ACK is received which advances tp->snd_una, 43df8bae1dSRodney W. Grimes * then the retransmit timer is cleared (if there are no more 44df8bae1dSRodney W. Grimes * outstanding segments) or reset to the base value (if there 45df8bae1dSRodney W. Grimes * are more ACKs expected). Whenever the retransmit timer goes off, 46df8bae1dSRodney W. Grimes * we retransmit one unacknowledged segment, and do a backoff 47df8bae1dSRodney W. Grimes * on the retransmit timer. 48df8bae1dSRodney W. Grimes * 49df8bae1dSRodney W. Grimes * The TCPT_PERSIST timer is used to keep window size information 50df8bae1dSRodney W. Grimes * flowing even if the window goes shut. If all previous transmissions 51df8bae1dSRodney W. Grimes * have been acknowledged (so that there are no retransmissions in progress), 52df8bae1dSRodney W. Grimes * and the window is too small to bother sending anything, then we start 53df8bae1dSRodney W. Grimes * the TCPT_PERSIST timer. When it expires, if the window is nonzero, 54df8bae1dSRodney W. Grimes * we go to transmit state. Otherwise, at intervals send a single byte 55df8bae1dSRodney W. Grimes * into the peer's window to force him to update our window information. 56df8bae1dSRodney W. Grimes * We do this at most as often as TCPT_PERSMIN time intervals, 57df8bae1dSRodney W. Grimes * but no more frequently than the current estimate of round-trip 58df8bae1dSRodney W. Grimes * packet time. The TCPT_PERSIST timer is cleared whenever we receive 59df8bae1dSRodney W. Grimes * a window update from the peer. 60df8bae1dSRodney W. Grimes * 61df8bae1dSRodney W. Grimes * The TCPT_KEEP timer is used to keep connections alive. If an 62df8bae1dSRodney W. Grimes * connection is idle (no segments received) for TCPTV_KEEP_INIT amount of time, 63df8bae1dSRodney W. Grimes * but not yet established, then we drop the connection. Once the connection 64df8bae1dSRodney W. Grimes * is established, if the connection is idle for TCPTV_KEEP_IDLE time 65df8bae1dSRodney W. Grimes * (and keepalives have been enabled on the socket), we begin to probe 66df8bae1dSRodney W. Grimes * the connection. We force the peer to send us a segment by sending: 67df8bae1dSRodney W. Grimes * <SEQ=SND.UNA-1><ACK=RCV.NXT><CTL=ACK> 68df8bae1dSRodney W. Grimes * This segment is (deliberately) outside the window, and should elicit 69df8bae1dSRodney W. Grimes * an ack segment in response from the peer. If, despite the TCPT_KEEP 70df8bae1dSRodney W. Grimes * initiated segments we cannot elicit a response from a peer in TCPT_MAXIDLE 71df8bae1dSRodney W. Grimes * amount of time probing, then we drop the connection. 72df8bae1dSRodney W. Grimes */ 73df8bae1dSRodney W. Grimes 74df8bae1dSRodney W. Grimes /* 75df8bae1dSRodney W. Grimes * Time constants. 76df8bae1dSRodney W. Grimes */ 779b8b58e0SJonathan Lemon #define TCPTV_MSL ( 30*hz) /* max seg lifetime (hah!) */ 78df8bae1dSRodney W. Grimes #define TCPTV_SRTTBASE 0 /* base roundtrip time; 79df8bae1dSRodney W. Grimes if 0, no idea yet */ 803b853844SMichael Tuexen #define TCPTV_RTOBASE ( 1*hz) /* assumed RTO if no info */ 81df8bae1dSRodney W. Grimes 820645c604SHiren Panchasara #define TCPTV_PERSMIN ( 5*hz) /* minimum persist interval */ 839b8b58e0SJonathan Lemon #define TCPTV_PERSMAX ( 60*hz) /* maximum persist interval */ 84df8bae1dSRodney W. Grimes 859b8b58e0SJonathan Lemon #define TCPTV_KEEP_INIT ( 75*hz) /* initial connect keepalive */ 869b8b58e0SJonathan Lemon #define TCPTV_KEEP_IDLE (120*60*hz) /* dflt time before probing */ 879b8b58e0SJonathan Lemon #define TCPTV_KEEPINTVL ( 75*hz) /* default probe interval */ 88df8bae1dSRodney W. Grimes #define TCPTV_KEEPCNT 8 /* max probes before drop */ 89*08af8aacSRandall Stewart #define TCPTV_MAXUNACKTIME 0 /* max time without making progress */ 90df8bae1dSRodney W. Grimes 917c72af87SMohan Srinivasan #define TCPTV_FINWAIT2_TIMEOUT (60*hz) /* FIN_WAIT_2 timeout if no receiver */ 927c72af87SMohan Srinivasan 9322fd54d4SMatthew Dillon /* 9422fd54d4SMatthew Dillon * Minimum retransmit timer is 3 ticks, for algorithmic stability. 95701bec5aSMatthew Dillon * TCPT_RANGESET() will add another TCPTV_CPU_VAR to deal with 96701bec5aSMatthew Dillon * the expected worst-case processing variances by the kernels 97701bec5aSMatthew Dillon * representing the end points. Such variances do not always show 98701bec5aSMatthew Dillon * up in the srtt because the timestamp is often calculated at 99701bec5aSMatthew Dillon * the interface rather then at the TCP layer. This value is 100701bec5aSMatthew Dillon * typically 50ms. However, it is also possible that delayed 101701bec5aSMatthew Dillon * acks (typically 100ms) could create issues so we set the slop 102701bec5aSMatthew Dillon * to 200ms to try to cover it. Note that, properly speaking, 103701bec5aSMatthew Dillon * delayed-acks should not create a major issue for interactive 104701bec5aSMatthew Dillon * environments which 'P'ush the last segment, at least as 105701bec5aSMatthew Dillon * long as implementations do the required 'at least one ack 106701bec5aSMatthew Dillon * for every two packets' for the non-interactive streaming case. 107701bec5aSMatthew Dillon * (maybe the RTO calculation should use 2*RTT instead of RTT 108701bec5aSMatthew Dillon * to handle the ack-every-other-packet case). 109701bec5aSMatthew Dillon * 110701bec5aSMatthew Dillon * The prior minimum of 1*hz (1 second) badly breaks throughput on any 111701bec5aSMatthew Dillon * networks faster then a modem that has minor (e.g. 1%) packet loss. 11222fd54d4SMatthew Dillon */ 113c4a184bdSPeter Wemm #define TCPTV_MIN ( hz/33 ) /* minimum allowable value */ 114701bec5aSMatthew Dillon #define TCPTV_CPU_VAR ( hz/5 ) /* cpu variance allowed (200ms) */ 1159b8b58e0SJonathan Lemon #define TCPTV_REXMTMAX ( 64*hz) /* max allowable REXMT value */ 116df8bae1dSRodney W. Grimes 117eb6ad696SGarrett Wollman #define TCPTV_TWTRUNC 8 /* RTO factor to truncate TW */ 118eb6ad696SGarrett Wollman 119df8bae1dSRodney W. Grimes #define TCP_MAXRXTSHIFT 12 /* maximum retransmits */ 120df8bae1dSRodney W. Grimes 121d7ca3f78SRichard Scheffenegger #define TCPTV_DELACK ( hz/25 ) /* 40ms timeout */ 1229b8b58e0SJonathan Lemon 1235ede40dcSRyan Stone /* 1245ede40dcSRyan Stone * If we exceed this number of retransmits for a single segment, we'll consider 1255ede40dcSRyan Stone * the current srtt measurement no longer valid and will recalculate from 1265ede40dcSRyan Stone * scratch starting with the next ACK. 1275ede40dcSRyan Stone */ 1285ede40dcSRyan Stone #define TCP_RTT_INVALIDATE (TCP_MAXRXTSHIFT / 4) 1295ede40dcSRyan Stone 130df8bae1dSRodney W. Grimes #ifdef TCPTIMERS 131e88894d3SAlfred Perlstein static const char *tcptimers[] = 13213feab82SAndre Oppermann { "REXMT", "PERSIST", "KEEP", "2MSL", "DELACK" }; 133df8bae1dSRodney W. Grimes #endif 134df8bae1dSRodney W. Grimes 1357480de43SRuslan Ermilov /* 1367480de43SRuslan Ermilov * Force a time value to be in a certain range. 1377480de43SRuslan Ermilov */ 1387480de43SRuslan Ermilov #define TCPT_RANGESET(tv, value, tvmin, tvmax) do { \ 1397480de43SRuslan Ermilov (tv) = (value) + tcp_rexmit_slop; \ 1407480de43SRuslan Ermilov if ((u_long)(tv) < (u_long)(tvmin)) \ 1417480de43SRuslan Ermilov (tv) = (tvmin); \ 1427480de43SRuslan Ermilov if ((u_long)(tv) > (u_long)(tvmax)) \ 1437480de43SRuslan Ermilov (tv) = (tvmax); \ 1447480de43SRuslan Ermilov } while(0) 1457480de43SRuslan Ermilov 1467480de43SRuslan Ermilov #ifdef _KERNEL 1477480de43SRuslan Ermilov 148b8614722SMike Silbersack struct xtcp_timer; 149b8614722SMike Silbersack 150e2f2059fSMike Silbersack struct tcp_timer { 151e2f2059fSMike Silbersack struct callout tt_rexmt; /* retransmit timer */ 152e2f2059fSMike Silbersack struct callout tt_persist; /* retransmit persistence */ 153e2f2059fSMike Silbersack struct callout tt_keep; /* keepalive */ 154e2f2059fSMike Silbersack struct callout tt_2msl; /* 2*msl TIME_WAIT timer */ 155e2f2059fSMike Silbersack struct callout tt_delack; /* delayed ACK timer */ 1565571f9cfSJulien Charbon uint32_t tt_flags; /* Timers flags */ 157e5ad6456SRandall Stewart uint32_t tt_draincnt; /* Count being drained */ 158e2f2059fSMike Silbersack }; 1595571f9cfSJulien Charbon 1605571f9cfSJulien Charbon /* 1615571f9cfSJulien Charbon * Flags for the tt_flags field. 1625571f9cfSJulien Charbon */ 1635571f9cfSJulien Charbon #define TT_DELACK 0x0001 1645571f9cfSJulien Charbon #define TT_REXMT 0x0002 1655571f9cfSJulien Charbon #define TT_PERSIST 0x0004 1665571f9cfSJulien Charbon #define TT_KEEP 0x0008 1675571f9cfSJulien Charbon #define TT_2MSL 0x0010 1685571f9cfSJulien Charbon #define TT_MASK (TT_DELACK|TT_REXMT|TT_PERSIST|TT_KEEP|TT_2MSL) 1695571f9cfSJulien Charbon 17089e560f4SRandall Stewart /* 17189e560f4SRandall Stewart * Suspend flags - used when suspending a timer 17289e560f4SRandall Stewart * from ever running again. 17389e560f4SRandall Stewart */ 17489e560f4SRandall Stewart #define TT_DELACK_SUS 0x0100 17589e560f4SRandall Stewart #define TT_REXMT_SUS 0x0200 17689e560f4SRandall Stewart #define TT_PERSIST_SUS 0x0400 17789e560f4SRandall Stewart #define TT_KEEP_SUS 0x0800 17889e560f4SRandall Stewart #define TT_2MSL_SUS 0x1000 179d6de19acSJulien Charbon 1805571f9cfSJulien Charbon #define TT_STOPPED 0x00010000 181b8152ba7SAndre Oppermann 1829077f387SGleb Smirnoff #define TP_KEEPINIT(tp) ((tp)->t_keepinit ? (tp)->t_keepinit : tcp_keepinit) 1839077f387SGleb Smirnoff #define TP_KEEPIDLE(tp) ((tp)->t_keepidle ? (tp)->t_keepidle : tcp_keepidle) 1849077f387SGleb Smirnoff #define TP_KEEPINTVL(tp) ((tp)->t_keepintvl ? (tp)->t_keepintvl : tcp_keepintvl) 1859077f387SGleb Smirnoff #define TP_KEEPCNT(tp) ((tp)->t_keepcnt ? (tp)->t_keepcnt : tcp_keepcnt) 1869077f387SGleb Smirnoff #define TP_MAXIDLE(tp) (TP_KEEPCNT(tp) * TP_KEEPINTVL(tp)) 187*08af8aacSRandall Stewart #define TP_MAXUNACKTIME(tp) \ 188*08af8aacSRandall Stewart ((tp)->t_maxunacktime ? (tp)->t_maxunacktime : tcp_maxunacktime) 189*08af8aacSRandall Stewart 190*08af8aacSRandall Stewart /* 191*08af8aacSRandall Stewart * Obtain the time until the restransmit timer should fire. 192*08af8aacSRandall Stewart * This macro ensures the restransmit timer fires at the earlier of the 193*08af8aacSRandall Stewart * t_rxtcur value or the time the maxunacktime would be exceeded. 194*08af8aacSRandall Stewart */ 195*08af8aacSRandall Stewart #define TP_RXTCUR(tp) \ 196*08af8aacSRandall Stewart ((TP_MAXUNACKTIME(tp) == 0 || tp->t_acktime == 0) ? tp->t_rxtcur : \ 197*08af8aacSRandall Stewart max(1, min(tp->t_rxtcur, tp->t_acktime + TP_MAXUNACKTIME(tp) - ticks))) 1989077f387SGleb Smirnoff 1990645c604SHiren Panchasara extern int tcp_persmin; /* minimum persist interval */ 2000645c604SHiren Panchasara extern int tcp_persmax; /* maximum persist interval */ 2017b40aa32SPaul Traina extern int tcp_keepinit; /* time to establish connection */ 202df8bae1dSRodney W. Grimes extern int tcp_keepidle; /* time before keepalive probes begin */ 2039b8b58e0SJonathan Lemon extern int tcp_keepintvl; /* time between keepalive probes */ 2049077f387SGleb Smirnoff extern int tcp_keepcnt; /* number of keepalives */ 2059b8b58e0SJonathan Lemon extern int tcp_delacktime; /* time before sending a delayed ACK */ 206*08af8aacSRandall Stewart extern int tcp_maxunacktime; /* max time without making progress */ 2079b8b58e0SJonathan Lemon extern int tcp_maxpersistidle; 2080999766dSMichael Tuexen extern int tcp_rexmit_initial; 209701bec5aSMatthew Dillon extern int tcp_rexmit_min; 210701bec5aSMatthew Dillon extern int tcp_rexmit_slop; 211df8bae1dSRodney W. Grimes extern int tcp_ttl; /* time to live for TCP segs */ 212df8bae1dSRodney W. Grimes extern int tcp_backoff[]; 21389e560f4SRandall Stewart extern int tcp_totbackoff; 21489e560f4SRandall Stewart extern int tcp_rexmit_drop_options; 215707f139eSPaul Richards 2167c72af87SMohan Srinivasan extern int tcp_finwait2_timeout; 2177c72af87SMohan Srinivasan extern int tcp_fast_finwait2_recycle; 2187c72af87SMohan Srinivasan 219334fc582SBjoern A. Zeeb VNET_DECLARE(int, tcp_always_keepalive); 220334fc582SBjoern A. Zeeb #define V_tcp_always_keepalive VNET(tcp_always_keepalive) 221e29c55e4SGleb Smirnoff VNET_DECLARE(int, tcp_pmtud_blackhole_detect); 222e29c55e4SGleb Smirnoff #define V_tcp_pmtud_blackhole_detect VNET(tcp_pmtud_blackhole_detect) 223e29c55e4SGleb Smirnoff VNET_DECLARE(int, tcp_pmtud_blackhole_mss); 224e29c55e4SGleb Smirnoff #define V_tcp_pmtud_blackhole_mss VNET(tcp_pmtud_blackhole_mss) 225e29c55e4SGleb Smirnoff VNET_DECLARE(int, tcp_v6pmtud_blackhole_mss); 226e29c55e4SGleb Smirnoff #define V_tcp_v6pmtud_blackhole_mss VNET(tcp_v6pmtud_blackhole_mss) 227c2c8e360SAlexander V. Chernikov VNET_DECLARE(int, tcp_msl); 228c2c8e360SAlexander V. Chernikov #define V_tcp_msl VNET(tcp_msl) 229e29c55e4SGleb Smirnoff 230b07fef50SRandall Stewart void tcp_inpinfo_lock_del(struct inpcb *inp, struct tcpcb *tp); 231b07fef50SRandall Stewart 232607b0b0cSJonathan Lemon void tcp_timer_init(void); 23385d94372SRobert Watson void tcp_timer_2msl(void *xtp); 2342104448fSAndre Oppermann struct tcptw * 235cea40c48SJulien Charbon tcp_tw_2msl_scan(int reuse); /* XXX temporary? */ 23685d94372SRobert Watson void tcp_timer_keep(void *xtp); 23785d94372SRobert Watson void tcp_timer_persist(void *xtp); 23885d94372SRobert Watson void tcp_timer_rexmt(void *xtp); 23985d94372SRobert Watson void tcp_timer_delack(void *xtp); 2409b8b58e0SJonathan Lemon 241664a31e4SPeter Wemm #endif /* _KERNEL */ 2429b8b58e0SJonathan Lemon 2439b8b58e0SJonathan Lemon #endif /* !_NETINET_TCP_TIMER_H_ */ 244