xref: /f-stack/freebsd/netinet/tcp_timer.c (revision 22ce4aff)
1a9643ea8Slogwang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: BSD-3-Clause
3*22ce4affSfengbojiang  *
4a9643ea8Slogwang  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
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.c	8.2 (Berkeley) 5/24/95
32a9643ea8Slogwang  */
33a9643ea8Slogwang 
34a9643ea8Slogwang #include <sys/cdefs.h>
35a9643ea8Slogwang __FBSDID("$FreeBSD$");
36a9643ea8Slogwang 
37a9643ea8Slogwang #include "opt_inet.h"
38a9643ea8Slogwang #include "opt_inet6.h"
39a9643ea8Slogwang #include "opt_tcpdebug.h"
40a9643ea8Slogwang #include "opt_rss.h"
41a9643ea8Slogwang 
42a9643ea8Slogwang #include <sys/param.h>
43a9643ea8Slogwang #include <sys/kernel.h>
44a9643ea8Slogwang #include <sys/lock.h>
45a9643ea8Slogwang #include <sys/mbuf.h>
46a9643ea8Slogwang #include <sys/mutex.h>
47a9643ea8Slogwang #include <sys/protosw.h>
48a9643ea8Slogwang #include <sys/smp.h>
49a9643ea8Slogwang #include <sys/socket.h>
50a9643ea8Slogwang #include <sys/socketvar.h>
51a9643ea8Slogwang #include <sys/sysctl.h>
52a9643ea8Slogwang #include <sys/systm.h>
53a9643ea8Slogwang 
54a9643ea8Slogwang #include <net/if.h>
55a9643ea8Slogwang #include <net/route.h>
56a9643ea8Slogwang #include <net/rss_config.h>
57a9643ea8Slogwang #include <net/vnet.h>
58a9643ea8Slogwang #include <net/netisr.h>
59a9643ea8Slogwang 
60a9643ea8Slogwang #include <netinet/in.h>
61a9643ea8Slogwang #include <netinet/in_kdtrace.h>
62a9643ea8Slogwang #include <netinet/in_pcb.h>
63a9643ea8Slogwang #include <netinet/in_rss.h>
64a9643ea8Slogwang #include <netinet/in_systm.h>
65a9643ea8Slogwang #ifdef INET6
66a9643ea8Slogwang #include <netinet6/in6_pcb.h>
67a9643ea8Slogwang #endif
68a9643ea8Slogwang #include <netinet/ip_var.h>
69a9643ea8Slogwang #include <netinet/tcp.h>
70a9643ea8Slogwang #include <netinet/tcp_fsm.h>
71*22ce4affSfengbojiang #include <netinet/tcp_log_buf.h>
72a9643ea8Slogwang #include <netinet/tcp_timer.h>
73a9643ea8Slogwang #include <netinet/tcp_var.h>
74*22ce4affSfengbojiang #include <netinet/tcp_seq.h>
75a9643ea8Slogwang #include <netinet/cc/cc.h>
76a9643ea8Slogwang #ifdef INET6
77a9643ea8Slogwang #include <netinet6/tcp6_var.h>
78a9643ea8Slogwang #endif
79a9643ea8Slogwang #include <netinet/tcpip.h>
80a9643ea8Slogwang #ifdef TCPDEBUG
81a9643ea8Slogwang #include <netinet/tcp_debug.h>
82a9643ea8Slogwang #endif
83a9643ea8Slogwang 
84a9643ea8Slogwang int    tcp_persmin;
85*22ce4affSfengbojiang SYSCTL_PROC(_net_inet_tcp, OID_AUTO, persmin,
86*22ce4affSfengbojiang     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
87*22ce4affSfengbojiang     &tcp_persmin, 0, sysctl_msec_to_ticks, "I",
88*22ce4affSfengbojiang     "minimum persistence interval");
89a9643ea8Slogwang 
90a9643ea8Slogwang int    tcp_persmax;
91*22ce4affSfengbojiang SYSCTL_PROC(_net_inet_tcp, OID_AUTO, persmax,
92*22ce4affSfengbojiang     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
93*22ce4affSfengbojiang     &tcp_persmax, 0, sysctl_msec_to_ticks, "I",
94*22ce4affSfengbojiang     "maximum persistence interval");
95a9643ea8Slogwang 
96a9643ea8Slogwang int	tcp_keepinit;
97*22ce4affSfengbojiang SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit,
98*22ce4affSfengbojiang     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
99*22ce4affSfengbojiang     &tcp_keepinit, 0, sysctl_msec_to_ticks, "I",
100*22ce4affSfengbojiang     "time to establish connection");
101a9643ea8Slogwang 
102a9643ea8Slogwang int	tcp_keepidle;
103*22ce4affSfengbojiang SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle,
104*22ce4affSfengbojiang     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
105*22ce4affSfengbojiang     &tcp_keepidle, 0, sysctl_msec_to_ticks, "I",
106*22ce4affSfengbojiang     "time before keepalive probes begin");
107a9643ea8Slogwang 
108a9643ea8Slogwang int	tcp_keepintvl;
109*22ce4affSfengbojiang SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl,
110*22ce4affSfengbojiang     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
111*22ce4affSfengbojiang     &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I",
112*22ce4affSfengbojiang     "time between keepalive probes");
113a9643ea8Slogwang 
114a9643ea8Slogwang int	tcp_delacktime;
115*22ce4affSfengbojiang SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime,
116*22ce4affSfengbojiang     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
117a9643ea8Slogwang     &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
118a9643ea8Slogwang     "Time before a delayed ACK is sent");
119a9643ea8Slogwang 
120a9643ea8Slogwang int	tcp_msl;
121*22ce4affSfengbojiang SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl,
122*22ce4affSfengbojiang     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
123*22ce4affSfengbojiang     &tcp_msl, 0, sysctl_msec_to_ticks, "I",
124*22ce4affSfengbojiang     "Maximum segment lifetime");
125*22ce4affSfengbojiang 
126*22ce4affSfengbojiang int	tcp_rexmit_initial;
127*22ce4affSfengbojiang SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_initial,
128*22ce4affSfengbojiang    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
129*22ce4affSfengbojiang     &tcp_rexmit_initial, 0, sysctl_msec_to_ticks, "I",
130*22ce4affSfengbojiang     "Initial Retransmission Timeout");
131a9643ea8Slogwang 
132a9643ea8Slogwang int	tcp_rexmit_min;
133*22ce4affSfengbojiang SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min,
134*22ce4affSfengbojiang     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
135a9643ea8Slogwang     &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I",
136a9643ea8Slogwang     "Minimum Retransmission Timeout");
137a9643ea8Slogwang 
138a9643ea8Slogwang int	tcp_rexmit_slop;
139*22ce4affSfengbojiang SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop,
140*22ce4affSfengbojiang     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
141a9643ea8Slogwang     &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I",
142a9643ea8Slogwang     "Retransmission Timer Slop");
143a9643ea8Slogwang 
144*22ce4affSfengbojiang VNET_DEFINE(int, tcp_always_keepalive) = 1;
145*22ce4affSfengbojiang SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_VNET|CTLFLAG_RW,
146*22ce4affSfengbojiang     &VNET_NAME(tcp_always_keepalive) , 0,
147*22ce4affSfengbojiang     "Assume SO_KEEPALIVE on all TCP connections");
148a9643ea8Slogwang 
149a9643ea8Slogwang int    tcp_fast_finwait2_recycle = 0;
150a9643ea8Slogwang SYSCTL_INT(_net_inet_tcp, OID_AUTO, fast_finwait2_recycle, CTLFLAG_RW,
151a9643ea8Slogwang     &tcp_fast_finwait2_recycle, 0,
152a9643ea8Slogwang     "Recycle closed FIN_WAIT_2 connections faster");
153a9643ea8Slogwang 
154a9643ea8Slogwang int    tcp_finwait2_timeout;
155*22ce4affSfengbojiang SYSCTL_PROC(_net_inet_tcp, OID_AUTO, finwait2_timeout,
156*22ce4affSfengbojiang     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
157*22ce4affSfengbojiang     &tcp_finwait2_timeout, 0, sysctl_msec_to_ticks, "I",
158*22ce4affSfengbojiang     "FIN-WAIT2 timeout");
159a9643ea8Slogwang 
160a9643ea8Slogwang int	tcp_keepcnt = TCPTV_KEEPCNT;
161a9643ea8Slogwang SYSCTL_INT(_net_inet_tcp, OID_AUTO, keepcnt, CTLFLAG_RW, &tcp_keepcnt, 0,
162a9643ea8Slogwang     "Number of keepalive probes to send");
163a9643ea8Slogwang 
164a9643ea8Slogwang 	/* max idle probes */
165a9643ea8Slogwang int	tcp_maxpersistidle;
166a9643ea8Slogwang 
167*22ce4affSfengbojiang int	tcp_rexmit_drop_options = 0;
168a9643ea8Slogwang SYSCTL_INT(_net_inet_tcp, OID_AUTO, rexmit_drop_options, CTLFLAG_RW,
169a9643ea8Slogwang     &tcp_rexmit_drop_options, 0,
170a9643ea8Slogwang     "Drop TCP options from 3rd and later retransmitted SYN");
171a9643ea8Slogwang 
172*22ce4affSfengbojiang VNET_DEFINE(int, tcp_pmtud_blackhole_detect);
173a9643ea8Slogwang SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_detection,
174a9643ea8Slogwang     CTLFLAG_RW|CTLFLAG_VNET,
175a9643ea8Slogwang     &VNET_NAME(tcp_pmtud_blackhole_detect), 0,
176a9643ea8Slogwang     "Path MTU Discovery Black Hole Detection Enabled");
177a9643ea8Slogwang 
178a9643ea8Slogwang #ifdef INET
179*22ce4affSfengbojiang VNET_DEFINE(int, tcp_pmtud_blackhole_mss) = 1200;
180a9643ea8Slogwang SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_mss,
181a9643ea8Slogwang     CTLFLAG_RW|CTLFLAG_VNET,
182a9643ea8Slogwang     &VNET_NAME(tcp_pmtud_blackhole_mss), 0,
183a9643ea8Slogwang     "Path MTU Discovery Black Hole Detection lowered MSS");
184a9643ea8Slogwang #endif
185a9643ea8Slogwang 
186a9643ea8Slogwang #ifdef INET6
187*22ce4affSfengbojiang VNET_DEFINE(int, tcp_v6pmtud_blackhole_mss) = 1220;
188a9643ea8Slogwang SYSCTL_INT(_net_inet_tcp, OID_AUTO, v6pmtud_blackhole_mss,
189a9643ea8Slogwang     CTLFLAG_RW|CTLFLAG_VNET,
190a9643ea8Slogwang     &VNET_NAME(tcp_v6pmtud_blackhole_mss), 0,
191a9643ea8Slogwang     "Path MTU Discovery IPv6 Black Hole Detection lowered MSS");
192a9643ea8Slogwang #endif
193a9643ea8Slogwang 
194a9643ea8Slogwang #ifdef	RSS
195a9643ea8Slogwang static int	per_cpu_timers = 1;
196a9643ea8Slogwang #else
197a9643ea8Slogwang static int	per_cpu_timers = 0;
198a9643ea8Slogwang #endif
199a9643ea8Slogwang SYSCTL_INT(_net_inet_tcp, OID_AUTO, per_cpu_timers, CTLFLAG_RW,
200a9643ea8Slogwang     &per_cpu_timers , 0, "run tcp timers on all cpus");
201a9643ea8Slogwang 
202a9643ea8Slogwang /*
203a9643ea8Slogwang  * Map the given inp to a CPU id.
204a9643ea8Slogwang  *
205a9643ea8Slogwang  * This queries RSS if it's compiled in, else it defaults to the current
206a9643ea8Slogwang  * CPU ID.
207a9643ea8Slogwang  */
208*22ce4affSfengbojiang inline int
inp_to_cpuid(struct inpcb * inp)209a9643ea8Slogwang inp_to_cpuid(struct inpcb *inp)
210a9643ea8Slogwang {
211a9643ea8Slogwang 	u_int cpuid;
212a9643ea8Slogwang 
213a9643ea8Slogwang #ifdef	RSS
214a9643ea8Slogwang 	if (per_cpu_timers) {
215a9643ea8Slogwang 		cpuid = rss_hash2cpuid(inp->inp_flowid, inp->inp_flowtype);
216a9643ea8Slogwang 		if (cpuid == NETISR_CPUID_NONE)
217a9643ea8Slogwang 			return (curcpu);	/* XXX */
218a9643ea8Slogwang 		else
219a9643ea8Slogwang 			return (cpuid);
220a9643ea8Slogwang 	}
221a9643ea8Slogwang #else
222a9643ea8Slogwang 	/* Legacy, pre-RSS behaviour */
223a9643ea8Slogwang 	if (per_cpu_timers) {
224a9643ea8Slogwang 		/*
225a9643ea8Slogwang 		 * We don't have a flowid -> cpuid mapping, so cheat and
226a9643ea8Slogwang 		 * just map unknown cpuids to curcpu.  Not the best, but
227a9643ea8Slogwang 		 * apparently better than defaulting to swi 0.
228a9643ea8Slogwang 		 */
229a9643ea8Slogwang 		cpuid = inp->inp_flowid % (mp_maxid + 1);
230a9643ea8Slogwang 		if (! CPU_ABSENT(cpuid))
231a9643ea8Slogwang 			return (cpuid);
232a9643ea8Slogwang 		return (curcpu);
233a9643ea8Slogwang 	}
234a9643ea8Slogwang #endif
235a9643ea8Slogwang 	/* Default for RSS and non-RSS - cpuid 0 */
236a9643ea8Slogwang 	else {
237a9643ea8Slogwang 		return (0);
238a9643ea8Slogwang 	}
239a9643ea8Slogwang }
240a9643ea8Slogwang 
241a9643ea8Slogwang /*
242a9643ea8Slogwang  * Tcp protocol timeout routine called every 500 ms.
243a9643ea8Slogwang  * Updates timestamps used for TCP
244a9643ea8Slogwang  * causes finite state machine actions if timers expire.
245a9643ea8Slogwang  */
246a9643ea8Slogwang void
tcp_slowtimo(void)247a9643ea8Slogwang tcp_slowtimo(void)
248a9643ea8Slogwang {
249a9643ea8Slogwang 	VNET_ITERATOR_DECL(vnet_iter);
250a9643ea8Slogwang 
251a9643ea8Slogwang 	VNET_LIST_RLOCK_NOSLEEP();
252a9643ea8Slogwang 	VNET_FOREACH(vnet_iter) {
253a9643ea8Slogwang 		CURVNET_SET(vnet_iter);
254a9643ea8Slogwang 		(void) tcp_tw_2msl_scan(0);
255a9643ea8Slogwang 		CURVNET_RESTORE();
256a9643ea8Slogwang 	}
257a9643ea8Slogwang 	VNET_LIST_RUNLOCK_NOSLEEP();
258a9643ea8Slogwang }
259a9643ea8Slogwang 
260a9643ea8Slogwang int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
261a9643ea8Slogwang     { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
262a9643ea8Slogwang 
263*22ce4affSfengbojiang int tcp_totbackoff = 2559;	/* sum of tcp_backoff[] */
264a9643ea8Slogwang 
265a9643ea8Slogwang /*
266a9643ea8Slogwang  * TCP timer processing.
267a9643ea8Slogwang  */
268a9643ea8Slogwang 
269a9643ea8Slogwang void
tcp_timer_delack(void * xtp)270a9643ea8Slogwang tcp_timer_delack(void *xtp)
271a9643ea8Slogwang {
272*22ce4affSfengbojiang 	struct epoch_tracker et;
273a9643ea8Slogwang 	struct tcpcb *tp = xtp;
274a9643ea8Slogwang 	struct inpcb *inp;
275a9643ea8Slogwang 	CURVNET_SET(tp->t_vnet);
276a9643ea8Slogwang 
277a9643ea8Slogwang 	inp = tp->t_inpcb;
278a9643ea8Slogwang 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
279a9643ea8Slogwang 	INP_WLOCK(inp);
280a9643ea8Slogwang 	if (callout_pending(&tp->t_timers->tt_delack) ||
281a9643ea8Slogwang 	    !callout_active(&tp->t_timers->tt_delack)) {
282a9643ea8Slogwang 		INP_WUNLOCK(inp);
283a9643ea8Slogwang 		CURVNET_RESTORE();
284a9643ea8Slogwang 		return;
285a9643ea8Slogwang 	}
286a9643ea8Slogwang 	callout_deactivate(&tp->t_timers->tt_delack);
287a9643ea8Slogwang 	if ((inp->inp_flags & INP_DROPPED) != 0) {
288a9643ea8Slogwang 		INP_WUNLOCK(inp);
289a9643ea8Slogwang 		CURVNET_RESTORE();
290a9643ea8Slogwang 		return;
291a9643ea8Slogwang 	}
292a9643ea8Slogwang 	tp->t_flags |= TF_ACKNOW;
293a9643ea8Slogwang 	TCPSTAT_INC(tcps_delack);
294*22ce4affSfengbojiang 	NET_EPOCH_ENTER(et);
295a9643ea8Slogwang 	(void) tp->t_fb->tfb_tcp_output(tp);
296a9643ea8Slogwang 	INP_WUNLOCK(inp);
297*22ce4affSfengbojiang 	NET_EPOCH_EXIT(et);
298a9643ea8Slogwang 	CURVNET_RESTORE();
299a9643ea8Slogwang }
300a9643ea8Slogwang 
301a9643ea8Slogwang void
tcp_inpinfo_lock_del(struct inpcb * inp,struct tcpcb * tp)302*22ce4affSfengbojiang tcp_inpinfo_lock_del(struct inpcb *inp, struct tcpcb *tp)
303*22ce4affSfengbojiang {
304*22ce4affSfengbojiang 	if (inp && tp != NULL)
305*22ce4affSfengbojiang 		INP_WUNLOCK(inp);
306*22ce4affSfengbojiang }
307*22ce4affSfengbojiang 
308*22ce4affSfengbojiang void
tcp_timer_2msl(void * xtp)309a9643ea8Slogwang tcp_timer_2msl(void *xtp)
310a9643ea8Slogwang {
311a9643ea8Slogwang 	struct tcpcb *tp = xtp;
312a9643ea8Slogwang 	struct inpcb *inp;
313*22ce4affSfengbojiang 	struct epoch_tracker et;
314a9643ea8Slogwang 	CURVNET_SET(tp->t_vnet);
315a9643ea8Slogwang #ifdef TCPDEBUG
316a9643ea8Slogwang 	int ostate;
317a9643ea8Slogwang 
318a9643ea8Slogwang 	ostate = tp->t_state;
319a9643ea8Slogwang #endif
320a9643ea8Slogwang 	inp = tp->t_inpcb;
321a9643ea8Slogwang 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
322a9643ea8Slogwang 	INP_WLOCK(inp);
323a9643ea8Slogwang 	tcp_free_sackholes(tp);
324a9643ea8Slogwang 	if (callout_pending(&tp->t_timers->tt_2msl) ||
325a9643ea8Slogwang 	    !callout_active(&tp->t_timers->tt_2msl)) {
326a9643ea8Slogwang 		INP_WUNLOCK(tp->t_inpcb);
327a9643ea8Slogwang 		CURVNET_RESTORE();
328a9643ea8Slogwang 		return;
329a9643ea8Slogwang 	}
330a9643ea8Slogwang 	callout_deactivate(&tp->t_timers->tt_2msl);
331a9643ea8Slogwang 	if ((inp->inp_flags & INP_DROPPED) != 0) {
332a9643ea8Slogwang 		INP_WUNLOCK(inp);
333a9643ea8Slogwang 		CURVNET_RESTORE();
334a9643ea8Slogwang 		return;
335a9643ea8Slogwang 	}
336a9643ea8Slogwang 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
337a9643ea8Slogwang 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
338a9643ea8Slogwang 	/*
339a9643ea8Slogwang 	 * 2 MSL timeout in shutdown went off.  If we're closed but
340a9643ea8Slogwang 	 * still waiting for peer to close and connection has been idle
341a9643ea8Slogwang 	 * too long delete connection control block.  Otherwise, check
342a9643ea8Slogwang 	 * again in a bit.
343a9643ea8Slogwang 	 *
344a9643ea8Slogwang 	 * If in TIME_WAIT state just ignore as this timeout is handled in
345a9643ea8Slogwang 	 * tcp_tw_2msl_scan().
346a9643ea8Slogwang 	 *
347a9643ea8Slogwang 	 * If fastrecycle of FIN_WAIT_2, in FIN_WAIT_2 and receiver has closed,
348a9643ea8Slogwang 	 * there's no point in hanging onto FIN_WAIT_2 socket. Just close it.
349a9643ea8Slogwang 	 * Ignore fact that there were recent incoming segments.
350a9643ea8Slogwang 	 */
351a9643ea8Slogwang 	if ((inp->inp_flags & INP_TIMEWAIT) != 0) {
352a9643ea8Slogwang 		INP_WUNLOCK(inp);
353a9643ea8Slogwang 		CURVNET_RESTORE();
354a9643ea8Slogwang 		return;
355a9643ea8Slogwang 	}
356a9643ea8Slogwang 	if (tcp_fast_finwait2_recycle && tp->t_state == TCPS_FIN_WAIT_2 &&
357a9643ea8Slogwang 	    tp->t_inpcb && tp->t_inpcb->inp_socket &&
358a9643ea8Slogwang 	    (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE)) {
359a9643ea8Slogwang 		TCPSTAT_INC(tcps_finwait2_drops);
360*22ce4affSfengbojiang 		if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
361*22ce4affSfengbojiang 			tcp_inpinfo_lock_del(inp, tp);
362*22ce4affSfengbojiang 			goto out;
363*22ce4affSfengbojiang 		}
364*22ce4affSfengbojiang 		NET_EPOCH_ENTER(et);
365a9643ea8Slogwang 		tp = tcp_close(tp);
366*22ce4affSfengbojiang 		NET_EPOCH_EXIT(et);
367*22ce4affSfengbojiang 		tcp_inpinfo_lock_del(inp, tp);
368*22ce4affSfengbojiang 		goto out;
369a9643ea8Slogwang 	} else {
370a9643ea8Slogwang 		if (ticks - tp->t_rcvtime <= TP_MAXIDLE(tp)) {
371*22ce4affSfengbojiang 			callout_reset(&tp->t_timers->tt_2msl,
372*22ce4affSfengbojiang 				      TP_KEEPINTVL(tp), tcp_timer_2msl, tp);
373*22ce4affSfengbojiang 		} else {
374*22ce4affSfengbojiang 			if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
375*22ce4affSfengbojiang 				tcp_inpinfo_lock_del(inp, tp);
376*22ce4affSfengbojiang 				goto out;
377a9643ea8Slogwang 			}
378*22ce4affSfengbojiang 			NET_EPOCH_ENTER(et);
379a9643ea8Slogwang 			tp = tcp_close(tp);
380*22ce4affSfengbojiang 			NET_EPOCH_EXIT(et);
381*22ce4affSfengbojiang 			tcp_inpinfo_lock_del(inp, tp);
382*22ce4affSfengbojiang 			goto out;
383*22ce4affSfengbojiang 		}
384a9643ea8Slogwang 	}
385a9643ea8Slogwang 
386a9643ea8Slogwang #ifdef TCPDEBUG
387a9643ea8Slogwang 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
388a9643ea8Slogwang 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
389a9643ea8Slogwang 			  PRU_SLOWTIMO);
390a9643ea8Slogwang #endif
391a9643ea8Slogwang 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
392a9643ea8Slogwang 
393a9643ea8Slogwang 	if (tp != NULL)
394a9643ea8Slogwang 		INP_WUNLOCK(inp);
395*22ce4affSfengbojiang out:
396a9643ea8Slogwang 	CURVNET_RESTORE();
397a9643ea8Slogwang }
398a9643ea8Slogwang 
399a9643ea8Slogwang void
tcp_timer_keep(void * xtp)400a9643ea8Slogwang tcp_timer_keep(void *xtp)
401a9643ea8Slogwang {
402a9643ea8Slogwang 	struct tcpcb *tp = xtp;
403a9643ea8Slogwang 	struct tcptemp *t_template;
404a9643ea8Slogwang 	struct inpcb *inp;
405*22ce4affSfengbojiang 	struct epoch_tracker et;
406a9643ea8Slogwang 	CURVNET_SET(tp->t_vnet);
407a9643ea8Slogwang #ifdef TCPDEBUG
408a9643ea8Slogwang 	int ostate;
409a9643ea8Slogwang 
410a9643ea8Slogwang 	ostate = tp->t_state;
411a9643ea8Slogwang #endif
412a9643ea8Slogwang 	inp = tp->t_inpcb;
413a9643ea8Slogwang 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
414a9643ea8Slogwang 	INP_WLOCK(inp);
415a9643ea8Slogwang 	if (callout_pending(&tp->t_timers->tt_keep) ||
416a9643ea8Slogwang 	    !callout_active(&tp->t_timers->tt_keep)) {
417a9643ea8Slogwang 		INP_WUNLOCK(inp);
418a9643ea8Slogwang 		CURVNET_RESTORE();
419a9643ea8Slogwang 		return;
420a9643ea8Slogwang 	}
421a9643ea8Slogwang 	callout_deactivate(&tp->t_timers->tt_keep);
422a9643ea8Slogwang 	if ((inp->inp_flags & INP_DROPPED) != 0) {
423a9643ea8Slogwang 		INP_WUNLOCK(inp);
424a9643ea8Slogwang 		CURVNET_RESTORE();
425a9643ea8Slogwang 		return;
426a9643ea8Slogwang 	}
427a9643ea8Slogwang 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
428a9643ea8Slogwang 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
429*22ce4affSfengbojiang 
430*22ce4affSfengbojiang 	/*
431*22ce4affSfengbojiang 	 * Because we don't regularly reset the keepalive callout in
432*22ce4affSfengbojiang 	 * the ESTABLISHED state, it may be that we don't actually need
433*22ce4affSfengbojiang 	 * to send a keepalive yet. If that occurs, schedule another
434*22ce4affSfengbojiang 	 * call for the next time the keepalive timer might expire.
435*22ce4affSfengbojiang 	 */
436*22ce4affSfengbojiang 	if (TCPS_HAVEESTABLISHED(tp->t_state)) {
437*22ce4affSfengbojiang 		u_int idletime;
438*22ce4affSfengbojiang 
439*22ce4affSfengbojiang 		idletime = ticks - tp->t_rcvtime;
440*22ce4affSfengbojiang 		if (idletime < TP_KEEPIDLE(tp)) {
441*22ce4affSfengbojiang 			callout_reset(&tp->t_timers->tt_keep,
442*22ce4affSfengbojiang 			    TP_KEEPIDLE(tp) - idletime, tcp_timer_keep, tp);
443*22ce4affSfengbojiang 			INP_WUNLOCK(inp);
444*22ce4affSfengbojiang 			CURVNET_RESTORE();
445*22ce4affSfengbojiang 			return;
446*22ce4affSfengbojiang 		}
447*22ce4affSfengbojiang 	}
448*22ce4affSfengbojiang 
449a9643ea8Slogwang 	/*
450a9643ea8Slogwang 	 * Keep-alive timer went off; send something
451a9643ea8Slogwang 	 * or drop connection if idle for too long.
452a9643ea8Slogwang 	 */
453a9643ea8Slogwang 	TCPSTAT_INC(tcps_keeptimeo);
454a9643ea8Slogwang 	if (tp->t_state < TCPS_ESTABLISHED)
455a9643ea8Slogwang 		goto dropit;
456*22ce4affSfengbojiang 	if ((V_tcp_always_keepalive ||
457*22ce4affSfengbojiang 	    inp->inp_socket->so_options & SO_KEEPALIVE) &&
458a9643ea8Slogwang 	    tp->t_state <= TCPS_CLOSING) {
459a9643ea8Slogwang 		if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
460a9643ea8Slogwang 			goto dropit;
461a9643ea8Slogwang 		/*
462a9643ea8Slogwang 		 * Send a packet designed to force a response
463a9643ea8Slogwang 		 * if the peer is up and reachable:
464a9643ea8Slogwang 		 * either an ACK if the connection is still alive,
465a9643ea8Slogwang 		 * or an RST if the peer has closed the connection
466a9643ea8Slogwang 		 * due to timeout or reboot.
467a9643ea8Slogwang 		 * Using sequence number tp->snd_una-1
468a9643ea8Slogwang 		 * causes the transmitted zero-length segment
469a9643ea8Slogwang 		 * to lie outside the receive window;
470a9643ea8Slogwang 		 * by the protocol spec, this requires the
471a9643ea8Slogwang 		 * correspondent TCP to respond.
472a9643ea8Slogwang 		 */
473a9643ea8Slogwang 		TCPSTAT_INC(tcps_keepprobe);
474a9643ea8Slogwang 		t_template = tcpip_maketemplate(inp);
475a9643ea8Slogwang 		if (t_template) {
476*22ce4affSfengbojiang 			NET_EPOCH_ENTER(et);
477a9643ea8Slogwang 			tcp_respond(tp, t_template->tt_ipgen,
478a9643ea8Slogwang 				    &t_template->tt_t, (struct mbuf *)NULL,
479a9643ea8Slogwang 				    tp->rcv_nxt, tp->snd_una - 1, 0);
480*22ce4affSfengbojiang 			NET_EPOCH_EXIT(et);
481a9643ea8Slogwang 			free(t_template, M_TEMP);
482a9643ea8Slogwang 		}
483*22ce4affSfengbojiang 		callout_reset(&tp->t_timers->tt_keep, TP_KEEPINTVL(tp),
484*22ce4affSfengbojiang 			      tcp_timer_keep, tp);
485*22ce4affSfengbojiang 	} else
486*22ce4affSfengbojiang 		callout_reset(&tp->t_timers->tt_keep, TP_KEEPIDLE(tp),
487*22ce4affSfengbojiang 			      tcp_timer_keep, tp);
488a9643ea8Slogwang 
489a9643ea8Slogwang #ifdef TCPDEBUG
490a9643ea8Slogwang 	if (inp->inp_socket->so_options & SO_DEBUG)
491a9643ea8Slogwang 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
492a9643ea8Slogwang 			  PRU_SLOWTIMO);
493a9643ea8Slogwang #endif
494a9643ea8Slogwang 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
495a9643ea8Slogwang 	INP_WUNLOCK(inp);
496a9643ea8Slogwang 	CURVNET_RESTORE();
497a9643ea8Slogwang 	return;
498a9643ea8Slogwang 
499a9643ea8Slogwang dropit:
500a9643ea8Slogwang 	TCPSTAT_INC(tcps_keepdrops);
501*22ce4affSfengbojiang 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
502*22ce4affSfengbojiang 		tcp_inpinfo_lock_del(inp, tp);
503*22ce4affSfengbojiang 		goto out;
504*22ce4affSfengbojiang 	}
505*22ce4affSfengbojiang 	NET_EPOCH_ENTER(et);
506a9643ea8Slogwang 	tp = tcp_drop(tp, ETIMEDOUT);
507a9643ea8Slogwang 
508a9643ea8Slogwang #ifdef TCPDEBUG
509a9643ea8Slogwang 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
510a9643ea8Slogwang 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
511a9643ea8Slogwang 			  PRU_SLOWTIMO);
512a9643ea8Slogwang #endif
513a9643ea8Slogwang 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
514*22ce4affSfengbojiang 	NET_EPOCH_EXIT(et);
515*22ce4affSfengbojiang 	tcp_inpinfo_lock_del(inp, tp);
516*22ce4affSfengbojiang  out:
517a9643ea8Slogwang 	CURVNET_RESTORE();
518a9643ea8Slogwang }
519a9643ea8Slogwang 
520a9643ea8Slogwang void
tcp_timer_persist(void * xtp)521a9643ea8Slogwang tcp_timer_persist(void *xtp)
522a9643ea8Slogwang {
523a9643ea8Slogwang 	struct tcpcb *tp = xtp;
524a9643ea8Slogwang 	struct inpcb *inp;
525*22ce4affSfengbojiang 	struct epoch_tracker et;
526a9643ea8Slogwang 	CURVNET_SET(tp->t_vnet);
527a9643ea8Slogwang #ifdef TCPDEBUG
528a9643ea8Slogwang 	int ostate;
529a9643ea8Slogwang 
530a9643ea8Slogwang 	ostate = tp->t_state;
531a9643ea8Slogwang #endif
532a9643ea8Slogwang 	inp = tp->t_inpcb;
533a9643ea8Slogwang 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
534a9643ea8Slogwang 	INP_WLOCK(inp);
535a9643ea8Slogwang 	if (callout_pending(&tp->t_timers->tt_persist) ||
536a9643ea8Slogwang 	    !callout_active(&tp->t_timers->tt_persist)) {
537a9643ea8Slogwang 		INP_WUNLOCK(inp);
538a9643ea8Slogwang 		CURVNET_RESTORE();
539a9643ea8Slogwang 		return;
540a9643ea8Slogwang 	}
541a9643ea8Slogwang 	callout_deactivate(&tp->t_timers->tt_persist);
542a9643ea8Slogwang 	if ((inp->inp_flags & INP_DROPPED) != 0) {
543a9643ea8Slogwang 		INP_WUNLOCK(inp);
544a9643ea8Slogwang 		CURVNET_RESTORE();
545a9643ea8Slogwang 		return;
546a9643ea8Slogwang 	}
547a9643ea8Slogwang 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
548a9643ea8Slogwang 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
549a9643ea8Slogwang 	/*
550a9643ea8Slogwang 	 * Persistence timer into zero window.
551a9643ea8Slogwang 	 * Force a byte to be output, if possible.
552a9643ea8Slogwang 	 */
553a9643ea8Slogwang 	TCPSTAT_INC(tcps_persisttimeo);
554a9643ea8Slogwang 	/*
555a9643ea8Slogwang 	 * Hack: if the peer is dead/unreachable, we do not
556a9643ea8Slogwang 	 * time out if the window is closed.  After a full
557a9643ea8Slogwang 	 * backoff, drop the connection if the idle time
558a9643ea8Slogwang 	 * (no responses to probes) reaches the maximum
559a9643ea8Slogwang 	 * backoff that we would use if retransmitting.
560a9643ea8Slogwang 	 */
561a9643ea8Slogwang 	if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
562a9643ea8Slogwang 	    (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
563a9643ea8Slogwang 	     ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
564a9643ea8Slogwang 		TCPSTAT_INC(tcps_persistdrop);
565*22ce4affSfengbojiang 		if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
566*22ce4affSfengbojiang 			tcp_inpinfo_lock_del(inp, tp);
567*22ce4affSfengbojiang 			goto out;
568*22ce4affSfengbojiang 		}
569*22ce4affSfengbojiang 		NET_EPOCH_ENTER(et);
570a9643ea8Slogwang 		tp = tcp_drop(tp, ETIMEDOUT);
571*22ce4affSfengbojiang 		NET_EPOCH_EXIT(et);
572*22ce4affSfengbojiang 		tcp_inpinfo_lock_del(inp, tp);
573a9643ea8Slogwang 		goto out;
574a9643ea8Slogwang 	}
575a9643ea8Slogwang 	/*
576a9643ea8Slogwang 	 * If the user has closed the socket then drop a persisting
577a9643ea8Slogwang 	 * connection after a much reduced timeout.
578a9643ea8Slogwang 	 */
579a9643ea8Slogwang 	if (tp->t_state > TCPS_CLOSE_WAIT &&
580a9643ea8Slogwang 	    (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
581a9643ea8Slogwang 		TCPSTAT_INC(tcps_persistdrop);
582*22ce4affSfengbojiang 		if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
583*22ce4affSfengbojiang 			tcp_inpinfo_lock_del(inp, tp);
584*22ce4affSfengbojiang 			goto out;
585*22ce4affSfengbojiang 		}
586*22ce4affSfengbojiang 		NET_EPOCH_ENTER(et);
587a9643ea8Slogwang 		tp = tcp_drop(tp, ETIMEDOUT);
588*22ce4affSfengbojiang 		NET_EPOCH_EXIT(et);
589*22ce4affSfengbojiang 		tcp_inpinfo_lock_del(inp, tp);
590a9643ea8Slogwang 		goto out;
591a9643ea8Slogwang 	}
592a9643ea8Slogwang 	tcp_setpersist(tp);
593a9643ea8Slogwang 	tp->t_flags |= TF_FORCEDATA;
594*22ce4affSfengbojiang 	NET_EPOCH_ENTER(et);
595a9643ea8Slogwang 	(void) tp->t_fb->tfb_tcp_output(tp);
596*22ce4affSfengbojiang 	NET_EPOCH_EXIT(et);
597a9643ea8Slogwang 	tp->t_flags &= ~TF_FORCEDATA;
598a9643ea8Slogwang 
599a9643ea8Slogwang #ifdef TCPDEBUG
600a9643ea8Slogwang 	if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
601a9643ea8Slogwang 		tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
602a9643ea8Slogwang #endif
603a9643ea8Slogwang 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
604a9643ea8Slogwang 	INP_WUNLOCK(inp);
605*22ce4affSfengbojiang out:
606a9643ea8Slogwang 	CURVNET_RESTORE();
607a9643ea8Slogwang }
608a9643ea8Slogwang 
609a9643ea8Slogwang void
tcp_timer_rexmt(void * xtp)610a9643ea8Slogwang tcp_timer_rexmt(void * xtp)
611a9643ea8Slogwang {
612a9643ea8Slogwang 	struct tcpcb *tp = xtp;
613a9643ea8Slogwang 	CURVNET_SET(tp->t_vnet);
614a9643ea8Slogwang 	int rexmt;
615a9643ea8Slogwang 	struct inpcb *inp;
616*22ce4affSfengbojiang 	struct epoch_tracker et;
617*22ce4affSfengbojiang 	bool isipv6;
618a9643ea8Slogwang #ifdef TCPDEBUG
619a9643ea8Slogwang 	int ostate;
620a9643ea8Slogwang 
621a9643ea8Slogwang 	ostate = tp->t_state;
622a9643ea8Slogwang #endif
623a9643ea8Slogwang 	inp = tp->t_inpcb;
624a9643ea8Slogwang 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
625a9643ea8Slogwang 	INP_WLOCK(inp);
626a9643ea8Slogwang 	if (callout_pending(&tp->t_timers->tt_rexmt) ||
627a9643ea8Slogwang 	    !callout_active(&tp->t_timers->tt_rexmt)) {
628a9643ea8Slogwang 		INP_WUNLOCK(inp);
629a9643ea8Slogwang 		CURVNET_RESTORE();
630a9643ea8Slogwang 		return;
631a9643ea8Slogwang 	}
632a9643ea8Slogwang 	callout_deactivate(&tp->t_timers->tt_rexmt);
633a9643ea8Slogwang 	if ((inp->inp_flags & INP_DROPPED) != 0) {
634a9643ea8Slogwang 		INP_WUNLOCK(inp);
635a9643ea8Slogwang 		CURVNET_RESTORE();
636a9643ea8Slogwang 		return;
637a9643ea8Slogwang 	}
638a9643ea8Slogwang 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
639a9643ea8Slogwang 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
640a9643ea8Slogwang 	tcp_free_sackholes(tp);
641*22ce4affSfengbojiang 	TCP_LOG_EVENT(tp, NULL, NULL, NULL, TCP_LOG_RTO, 0, 0, NULL, false);
642a9643ea8Slogwang 	if (tp->t_fb->tfb_tcp_rexmit_tmr) {
643a9643ea8Slogwang 		/* The stack has a timer action too. */
644a9643ea8Slogwang 		(*tp->t_fb->tfb_tcp_rexmit_tmr)(tp);
645a9643ea8Slogwang 	}
646a9643ea8Slogwang 	/*
647a9643ea8Slogwang 	 * Retransmission timer went off.  Message has not
648a9643ea8Slogwang 	 * been acked within retransmit interval.  Back off
649a9643ea8Slogwang 	 * to a longer retransmit interval and retransmit one segment.
650a9643ea8Slogwang 	 */
651a9643ea8Slogwang 	if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
652a9643ea8Slogwang 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
653a9643ea8Slogwang 		TCPSTAT_INC(tcps_timeoutdrop);
654*22ce4affSfengbojiang 		if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
655*22ce4affSfengbojiang 			tcp_inpinfo_lock_del(inp, tp);
656a9643ea8Slogwang 			goto out;
657a9643ea8Slogwang 		}
658*22ce4affSfengbojiang 		NET_EPOCH_ENTER(et);
659*22ce4affSfengbojiang 		tp = tcp_drop(tp, ETIMEDOUT);
660*22ce4affSfengbojiang 		NET_EPOCH_EXIT(et);
661*22ce4affSfengbojiang 		tcp_inpinfo_lock_del(inp, tp);
662*22ce4affSfengbojiang 		goto out;
663*22ce4affSfengbojiang 	}
664a9643ea8Slogwang 	if (tp->t_state == TCPS_SYN_SENT) {
665a9643ea8Slogwang 		/*
666a9643ea8Slogwang 		 * If the SYN was retransmitted, indicate CWND to be
667a9643ea8Slogwang 		 * limited to 1 segment in cc_conn_init().
668a9643ea8Slogwang 		 */
669a9643ea8Slogwang 		tp->snd_cwnd = 1;
670a9643ea8Slogwang 	} else if (tp->t_rxtshift == 1) {
671a9643ea8Slogwang 		/*
672a9643ea8Slogwang 		 * first retransmit; record ssthresh and cwnd so they can
673a9643ea8Slogwang 		 * be recovered if this turns out to be a "bad" retransmit.
674a9643ea8Slogwang 		 * A retransmit is considered "bad" if an ACK for this
675a9643ea8Slogwang 		 * segment is received within RTT/2 interval; the assumption
676a9643ea8Slogwang 		 * here is that the ACK was already in flight.  See
677a9643ea8Slogwang 		 * "On Estimating End-to-End Network Path Properties" by
678a9643ea8Slogwang 		 * Allman and Paxson for more details.
679a9643ea8Slogwang 		 */
680a9643ea8Slogwang 		tp->snd_cwnd_prev = tp->snd_cwnd;
681a9643ea8Slogwang 		tp->snd_ssthresh_prev = tp->snd_ssthresh;
682a9643ea8Slogwang 		tp->snd_recover_prev = tp->snd_recover;
683a9643ea8Slogwang 		if (IN_FASTRECOVERY(tp->t_flags))
684a9643ea8Slogwang 			tp->t_flags |= TF_WASFRECOVERY;
685a9643ea8Slogwang 		else
686a9643ea8Slogwang 			tp->t_flags &= ~TF_WASFRECOVERY;
687a9643ea8Slogwang 		if (IN_CONGRECOVERY(tp->t_flags))
688a9643ea8Slogwang 			tp->t_flags |= TF_WASCRECOVERY;
689a9643ea8Slogwang 		else
690a9643ea8Slogwang 			tp->t_flags &= ~TF_WASCRECOVERY;
691*22ce4affSfengbojiang 		if ((tp->t_flags & TF_RCVD_TSTMP) == 0)
692a9643ea8Slogwang 			tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
693*22ce4affSfengbojiang 		/* In the event that we've negotiated timestamps
694*22ce4affSfengbojiang 		 * badrxtwin will be set to the value that we set
695*22ce4affSfengbojiang 		 * the retransmitted packet's to_tsval to by tcp_output
696*22ce4affSfengbojiang 		 */
697a9643ea8Slogwang 		tp->t_flags |= TF_PREVVALID;
698a9643ea8Slogwang 	} else
699a9643ea8Slogwang 		tp->t_flags &= ~TF_PREVVALID;
700a9643ea8Slogwang 	TCPSTAT_INC(tcps_rexmttimeo);
701a9643ea8Slogwang 	if ((tp->t_state == TCPS_SYN_SENT) ||
702a9643ea8Slogwang 	    (tp->t_state == TCPS_SYN_RECEIVED))
703*22ce4affSfengbojiang 		rexmt = tcp_rexmit_initial * tcp_backoff[tp->t_rxtshift];
704a9643ea8Slogwang 	else
705a9643ea8Slogwang 		rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
706a9643ea8Slogwang 	TCPT_RANGESET(tp->t_rxtcur, rexmt,
707a9643ea8Slogwang 		      tp->t_rttmin, TCPTV_REXMTMAX);
708a9643ea8Slogwang 
709a9643ea8Slogwang 	/*
710a9643ea8Slogwang 	 * We enter the path for PLMTUD if connection is established or, if
711a9643ea8Slogwang 	 * connection is FIN_WAIT_1 status, reason for the last is that if
712a9643ea8Slogwang 	 * amount of data we send is very small, we could send it in couple of
713a9643ea8Slogwang 	 * packets and process straight to FIN. In that case we won't catch
714a9643ea8Slogwang 	 * ESTABLISHED state.
715a9643ea8Slogwang 	 */
716a9643ea8Slogwang #ifdef INET6
717*22ce4affSfengbojiang 	isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? true : false;
718*22ce4affSfengbojiang #else
719*22ce4affSfengbojiang 	isipv6 = false;
720a9643ea8Slogwang #endif
721*22ce4affSfengbojiang 	if (((V_tcp_pmtud_blackhole_detect == 1) ||
722*22ce4affSfengbojiang 	    (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) ||
723*22ce4affSfengbojiang 	    (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) &&
724*22ce4affSfengbojiang 	    ((tp->t_state == TCPS_ESTABLISHED) ||
725*22ce4affSfengbojiang 	    (tp->t_state == TCPS_FIN_WAIT_1))) {
726*22ce4affSfengbojiang 		if (tp->t_rxtshift == 1) {
727a9643ea8Slogwang 			/*
728*22ce4affSfengbojiang 			 * We enter blackhole detection after the first
729*22ce4affSfengbojiang 			 * unsuccessful timer based retransmission.
730*22ce4affSfengbojiang 			 * Then we reduce up to two times the MSS, each
731*22ce4affSfengbojiang 			 * candidate giving two tries of retransmissions.
732*22ce4affSfengbojiang 			 * But we give a candidate only two tries, if it
733*22ce4affSfengbojiang 			 * actually reduces the MSS.
734a9643ea8Slogwang 			 */
735*22ce4affSfengbojiang 			tp->t_blackhole_enter = 2;
736*22ce4affSfengbojiang 			tp->t_blackhole_exit = tp->t_blackhole_enter;
737*22ce4affSfengbojiang 			if (isipv6) {
738*22ce4affSfengbojiang #ifdef INET6
739*22ce4affSfengbojiang 				if (tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss)
740*22ce4affSfengbojiang 					tp->t_blackhole_exit += 2;
741*22ce4affSfengbojiang 				if (tp->t_maxseg > V_tcp_v6mssdflt &&
742*22ce4affSfengbojiang 				    V_tcp_v6pmtud_blackhole_mss > V_tcp_v6mssdflt)
743*22ce4affSfengbojiang 					tp->t_blackhole_exit += 2;
744*22ce4affSfengbojiang #endif
745*22ce4affSfengbojiang 			} else {
746*22ce4affSfengbojiang #ifdef INET
747*22ce4affSfengbojiang 				if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss)
748*22ce4affSfengbojiang 					tp->t_blackhole_exit += 2;
749*22ce4affSfengbojiang 				if (tp->t_maxseg > V_tcp_mssdflt &&
750*22ce4affSfengbojiang 				    V_tcp_pmtud_blackhole_mss > V_tcp_mssdflt)
751*22ce4affSfengbojiang 					tp->t_blackhole_exit += 2;
752*22ce4affSfengbojiang #endif
753*22ce4affSfengbojiang 			}
754*22ce4affSfengbojiang 		}
755a9643ea8Slogwang 		if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) ==
756a9643ea8Slogwang 		    (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) &&
757*22ce4affSfengbojiang 		    (tp->t_rxtshift >= tp->t_blackhole_enter &&
758*22ce4affSfengbojiang 		    tp->t_rxtshift < tp->t_blackhole_exit &&
759*22ce4affSfengbojiang 		    (tp->t_rxtshift - tp->t_blackhole_enter) % 2 == 0)) {
760a9643ea8Slogwang 			/*
761a9643ea8Slogwang 			 * Enter Path MTU Black-hole Detection mechanism:
762a9643ea8Slogwang 			 * - Disable Path MTU Discovery (IP "DF" bit).
763a9643ea8Slogwang 			 * - Reduce MTU to lower value than what we
764a9643ea8Slogwang 			 *   negotiated with peer.
765a9643ea8Slogwang 			 */
766*22ce4affSfengbojiang 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) {
767a9643ea8Slogwang 				/* Record that we may have found a black hole. */
768a9643ea8Slogwang 				tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
769a9643ea8Slogwang 				/* Keep track of previous MSS. */
770a9643ea8Slogwang 				tp->t_pmtud_saved_maxseg = tp->t_maxseg;
771*22ce4affSfengbojiang 			}
772a9643ea8Slogwang 
773a9643ea8Slogwang 			/*
774a9643ea8Slogwang 			 * Reduce the MSS to blackhole value or to the default
775a9643ea8Slogwang 			 * in an attempt to retransmit.
776a9643ea8Slogwang 			 */
777a9643ea8Slogwang #ifdef INET6
778a9643ea8Slogwang 			if (isipv6 &&
779*22ce4affSfengbojiang 			    tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss &&
780*22ce4affSfengbojiang 			    V_tcp_v6pmtud_blackhole_mss > V_tcp_v6mssdflt) {
781a9643ea8Slogwang 				/* Use the sysctl tuneable blackhole MSS. */
782a9643ea8Slogwang 				tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
783*22ce4affSfengbojiang 				TCPSTAT_INC(tcps_pmtud_blackhole_activated);
784a9643ea8Slogwang 			} else if (isipv6) {
785a9643ea8Slogwang 				/* Use the default MSS. */
786a9643ea8Slogwang 				tp->t_maxseg = V_tcp_v6mssdflt;
787a9643ea8Slogwang 				/*
788a9643ea8Slogwang 				 * Disable Path MTU Discovery when we switch to
789a9643ea8Slogwang 				 * minmss.
790a9643ea8Slogwang 				 */
791a9643ea8Slogwang 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
792*22ce4affSfengbojiang 				TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
793a9643ea8Slogwang 			}
794a9643ea8Slogwang #endif
795a9643ea8Slogwang #if defined(INET6) && defined(INET)
796a9643ea8Slogwang 			else
797a9643ea8Slogwang #endif
798a9643ea8Slogwang #ifdef INET
799*22ce4affSfengbojiang 			if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss &&
800*22ce4affSfengbojiang 			    V_tcp_pmtud_blackhole_mss > V_tcp_mssdflt) {
801a9643ea8Slogwang 				/* Use the sysctl tuneable blackhole MSS. */
802a9643ea8Slogwang 				tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
803*22ce4affSfengbojiang 				TCPSTAT_INC(tcps_pmtud_blackhole_activated);
804a9643ea8Slogwang 			} else {
805a9643ea8Slogwang 				/* Use the default MSS. */
806a9643ea8Slogwang 				tp->t_maxseg = V_tcp_mssdflt;
807a9643ea8Slogwang 				/*
808a9643ea8Slogwang 				 * Disable Path MTU Discovery when we switch to
809a9643ea8Slogwang 				 * minmss.
810a9643ea8Slogwang 				 */
811a9643ea8Slogwang 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
812*22ce4affSfengbojiang 				TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
813a9643ea8Slogwang 			}
814a9643ea8Slogwang #endif
815a9643ea8Slogwang 			/*
816a9643ea8Slogwang 			 * Reset the slow-start flight size
817a9643ea8Slogwang 			 * as it may depend on the new MSS.
818a9643ea8Slogwang 			 */
819a9643ea8Slogwang 			if (CC_ALGO(tp)->conn_init != NULL)
820a9643ea8Slogwang 				CC_ALGO(tp)->conn_init(tp->ccv);
821a9643ea8Slogwang 		} else {
822a9643ea8Slogwang 			/*
823a9643ea8Slogwang 			 * If further retransmissions are still unsuccessful
824a9643ea8Slogwang 			 * with a lowered MTU, maybe this isn't a blackhole and
825a9643ea8Slogwang 			 * we restore the previous MSS and blackhole detection
826a9643ea8Slogwang 			 * flags.
827a9643ea8Slogwang 			 */
828a9643ea8Slogwang 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
829*22ce4affSfengbojiang 			    (tp->t_rxtshift >= tp->t_blackhole_exit)) {
830a9643ea8Slogwang 				tp->t_flags2 |= TF2_PLPMTU_PMTUD;
831a9643ea8Slogwang 				tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
832a9643ea8Slogwang 				tp->t_maxseg = tp->t_pmtud_saved_maxseg;
833*22ce4affSfengbojiang 				TCPSTAT_INC(tcps_pmtud_blackhole_failed);
834a9643ea8Slogwang 				/*
835a9643ea8Slogwang 				 * Reset the slow-start flight size as it
836a9643ea8Slogwang 				 * may depend on the new MSS.
837a9643ea8Slogwang 				 */
838a9643ea8Slogwang 				if (CC_ALGO(tp)->conn_init != NULL)
839a9643ea8Slogwang 					CC_ALGO(tp)->conn_init(tp->ccv);
840a9643ea8Slogwang 			}
841a9643ea8Slogwang 		}
842a9643ea8Slogwang 	}
843a9643ea8Slogwang 
844a9643ea8Slogwang 	/*
845a9643ea8Slogwang 	 * Disable RFC1323 and SACK if we haven't got any response to
846a9643ea8Slogwang 	 * our third SYN to work-around some broken terminal servers
847a9643ea8Slogwang 	 * (most of which have hopefully been retired) that have bad VJ
848a9643ea8Slogwang 	 * header compression code which trashes TCP segments containing
849a9643ea8Slogwang 	 * unknown-to-them TCP options.
850a9643ea8Slogwang 	 */
851a9643ea8Slogwang 	if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
852a9643ea8Slogwang 	    (tp->t_rxtshift == 3))
853a9643ea8Slogwang 		tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
854a9643ea8Slogwang 	/*
855*22ce4affSfengbojiang 	 * If we backed off this far, notify the L3 protocol that we're having
856*22ce4affSfengbojiang 	 * connection problems.
857a9643ea8Slogwang 	 */
858*22ce4affSfengbojiang 	if (tp->t_rxtshift > TCP_RTT_INVALIDATE) {
859a9643ea8Slogwang #ifdef INET6
860a9643ea8Slogwang 		if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
861a9643ea8Slogwang 			in6_losing(tp->t_inpcb);
862a9643ea8Slogwang 		else
863a9643ea8Slogwang #endif
864a9643ea8Slogwang 			in_losing(tp->t_inpcb);
865a9643ea8Slogwang 	}
866a9643ea8Slogwang 	tp->snd_nxt = tp->snd_una;
867a9643ea8Slogwang 	tp->snd_recover = tp->snd_max;
868a9643ea8Slogwang 	/*
869a9643ea8Slogwang 	 * Force a segment to be sent.
870a9643ea8Slogwang 	 */
871a9643ea8Slogwang 	tp->t_flags |= TF_ACKNOW;
872a9643ea8Slogwang 	/*
873a9643ea8Slogwang 	 * If timing a segment in this window, stop the timer.
874a9643ea8Slogwang 	 */
875a9643ea8Slogwang 	tp->t_rtttime = 0;
876a9643ea8Slogwang 
877a9643ea8Slogwang 	cc_cong_signal(tp, NULL, CC_RTO);
878*22ce4affSfengbojiang 	NET_EPOCH_ENTER(et);
879a9643ea8Slogwang 	(void) tp->t_fb->tfb_tcp_output(tp);
880*22ce4affSfengbojiang 	NET_EPOCH_EXIT(et);
881a9643ea8Slogwang #ifdef TCPDEBUG
882a9643ea8Slogwang 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
883a9643ea8Slogwang 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
884a9643ea8Slogwang 			  PRU_SLOWTIMO);
885a9643ea8Slogwang #endif
886a9643ea8Slogwang 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
887a9643ea8Slogwang 	INP_WUNLOCK(inp);
888*22ce4affSfengbojiang out:
889a9643ea8Slogwang 	CURVNET_RESTORE();
890a9643ea8Slogwang }
891a9643ea8Slogwang 
892a9643ea8Slogwang void
tcp_timer_activate(struct tcpcb * tp,uint32_t timer_type,u_int delta)893a9643ea8Slogwang tcp_timer_activate(struct tcpcb *tp, uint32_t timer_type, u_int delta)
894a9643ea8Slogwang {
895a9643ea8Slogwang 	struct callout *t_callout;
896*22ce4affSfengbojiang 	callout_func_t *f_callout;
897a9643ea8Slogwang 	struct inpcb *inp = tp->t_inpcb;
898a9643ea8Slogwang 	int cpu = inp_to_cpuid(inp);
899a9643ea8Slogwang 
900a9643ea8Slogwang #ifdef TCP_OFFLOAD
901a9643ea8Slogwang 	if (tp->t_flags & TF_TOE)
902a9643ea8Slogwang 		return;
903a9643ea8Slogwang #endif
904a9643ea8Slogwang 
905a9643ea8Slogwang 	if (tp->t_timers->tt_flags & TT_STOPPED)
906a9643ea8Slogwang 		return;
907a9643ea8Slogwang 
908a9643ea8Slogwang 	switch (timer_type) {
909a9643ea8Slogwang 		case TT_DELACK:
910a9643ea8Slogwang 			t_callout = &tp->t_timers->tt_delack;
911a9643ea8Slogwang 			f_callout = tcp_timer_delack;
912a9643ea8Slogwang 			break;
913a9643ea8Slogwang 		case TT_REXMT:
914a9643ea8Slogwang 			t_callout = &tp->t_timers->tt_rexmt;
915a9643ea8Slogwang 			f_callout = tcp_timer_rexmt;
916a9643ea8Slogwang 			break;
917a9643ea8Slogwang 		case TT_PERSIST:
918a9643ea8Slogwang 			t_callout = &tp->t_timers->tt_persist;
919a9643ea8Slogwang 			f_callout = tcp_timer_persist;
920a9643ea8Slogwang 			break;
921a9643ea8Slogwang 		case TT_KEEP:
922a9643ea8Slogwang 			t_callout = &tp->t_timers->tt_keep;
923a9643ea8Slogwang 			f_callout = tcp_timer_keep;
924a9643ea8Slogwang 			break;
925a9643ea8Slogwang 		case TT_2MSL:
926a9643ea8Slogwang 			t_callout = &tp->t_timers->tt_2msl;
927a9643ea8Slogwang 			f_callout = tcp_timer_2msl;
928a9643ea8Slogwang 			break;
929a9643ea8Slogwang 		default:
930a9643ea8Slogwang 			if (tp->t_fb->tfb_tcp_timer_activate) {
931a9643ea8Slogwang 				tp->t_fb->tfb_tcp_timer_activate(tp, timer_type, delta);
932a9643ea8Slogwang 				return;
933a9643ea8Slogwang 			}
934a9643ea8Slogwang 			panic("tp %p bad timer_type %#x", tp, timer_type);
935a9643ea8Slogwang 		}
936a9643ea8Slogwang 	if (delta == 0) {
937*22ce4affSfengbojiang 		callout_stop(t_callout);
938a9643ea8Slogwang 	} else {
939a9643ea8Slogwang 		callout_reset_on(t_callout, delta, f_callout, tp, cpu);
940a9643ea8Slogwang 	}
941a9643ea8Slogwang }
942a9643ea8Slogwang 
943a9643ea8Slogwang int
tcp_timer_active(struct tcpcb * tp,uint32_t timer_type)944a9643ea8Slogwang tcp_timer_active(struct tcpcb *tp, uint32_t timer_type)
945a9643ea8Slogwang {
946a9643ea8Slogwang 	struct callout *t_callout;
947a9643ea8Slogwang 
948a9643ea8Slogwang 	switch (timer_type) {
949a9643ea8Slogwang 		case TT_DELACK:
950a9643ea8Slogwang 			t_callout = &tp->t_timers->tt_delack;
951a9643ea8Slogwang 			break;
952a9643ea8Slogwang 		case TT_REXMT:
953a9643ea8Slogwang 			t_callout = &tp->t_timers->tt_rexmt;
954a9643ea8Slogwang 			break;
955a9643ea8Slogwang 		case TT_PERSIST:
956a9643ea8Slogwang 			t_callout = &tp->t_timers->tt_persist;
957a9643ea8Slogwang 			break;
958a9643ea8Slogwang 		case TT_KEEP:
959a9643ea8Slogwang 			t_callout = &tp->t_timers->tt_keep;
960a9643ea8Slogwang 			break;
961a9643ea8Slogwang 		case TT_2MSL:
962a9643ea8Slogwang 			t_callout = &tp->t_timers->tt_2msl;
963a9643ea8Slogwang 			break;
964a9643ea8Slogwang 		default:
965a9643ea8Slogwang 			if (tp->t_fb->tfb_tcp_timer_active) {
966a9643ea8Slogwang 				return(tp->t_fb->tfb_tcp_timer_active(tp, timer_type));
967a9643ea8Slogwang 			}
968a9643ea8Slogwang 			panic("tp %p bad timer_type %#x", tp, timer_type);
969a9643ea8Slogwang 		}
970a9643ea8Slogwang 	return callout_active(t_callout);
971a9643ea8Slogwang }
972a9643ea8Slogwang 
973*22ce4affSfengbojiang /*
974*22ce4affSfengbojiang  * Stop the timer from running, and apply a flag
975*22ce4affSfengbojiang  * against the timer_flags that will force the
976*22ce4affSfengbojiang  * timer never to run. The flag is needed to assure
977*22ce4affSfengbojiang  * a race does not leave it running and cause
978*22ce4affSfengbojiang  * the timer to possibly restart itself (keep and persist
979*22ce4affSfengbojiang  * especially do this).
980*22ce4affSfengbojiang  */
981*22ce4affSfengbojiang int
tcp_timer_suspend(struct tcpcb * tp,uint32_t timer_type)982*22ce4affSfengbojiang tcp_timer_suspend(struct tcpcb *tp, uint32_t timer_type)
983*22ce4affSfengbojiang {
984*22ce4affSfengbojiang 	struct callout *t_callout;
985*22ce4affSfengbojiang 	uint32_t t_flags;
986*22ce4affSfengbojiang 
987*22ce4affSfengbojiang 	switch (timer_type) {
988*22ce4affSfengbojiang 		case TT_DELACK:
989*22ce4affSfengbojiang 			t_flags = TT_DELACK_SUS;
990*22ce4affSfengbojiang 			t_callout = &tp->t_timers->tt_delack;
991*22ce4affSfengbojiang 			break;
992*22ce4affSfengbojiang 		case TT_REXMT:
993*22ce4affSfengbojiang 			t_flags = TT_REXMT_SUS;
994*22ce4affSfengbojiang 			t_callout = &tp->t_timers->tt_rexmt;
995*22ce4affSfengbojiang 			break;
996*22ce4affSfengbojiang 		case TT_PERSIST:
997*22ce4affSfengbojiang 			t_flags = TT_PERSIST_SUS;
998*22ce4affSfengbojiang 			t_callout = &tp->t_timers->tt_persist;
999*22ce4affSfengbojiang 			break;
1000*22ce4affSfengbojiang 		case TT_KEEP:
1001*22ce4affSfengbojiang 			t_flags = TT_KEEP_SUS;
1002*22ce4affSfengbojiang 			t_callout = &tp->t_timers->tt_keep;
1003*22ce4affSfengbojiang 			break;
1004*22ce4affSfengbojiang 		case TT_2MSL:
1005*22ce4affSfengbojiang 			t_flags = TT_2MSL_SUS;
1006*22ce4affSfengbojiang 			t_callout = &tp->t_timers->tt_2msl;
1007*22ce4affSfengbojiang 			break;
1008*22ce4affSfengbojiang 		default:
1009*22ce4affSfengbojiang 			panic("tp:%p bad timer_type 0x%x", tp, timer_type);
1010*22ce4affSfengbojiang 	}
1011*22ce4affSfengbojiang 	tp->t_timers->tt_flags |= t_flags;
1012*22ce4affSfengbojiang 	return (callout_stop(t_callout));
1013*22ce4affSfengbojiang }
1014*22ce4affSfengbojiang 
1015*22ce4affSfengbojiang void
tcp_timers_unsuspend(struct tcpcb * tp,uint32_t timer_type)1016*22ce4affSfengbojiang tcp_timers_unsuspend(struct tcpcb *tp, uint32_t timer_type)
1017*22ce4affSfengbojiang {
1018*22ce4affSfengbojiang 	switch (timer_type) {
1019*22ce4affSfengbojiang 		case TT_DELACK:
1020*22ce4affSfengbojiang 			if (tp->t_timers->tt_flags & TT_DELACK_SUS) {
1021*22ce4affSfengbojiang 				tp->t_timers->tt_flags &= ~TT_DELACK_SUS;
1022*22ce4affSfengbojiang 				if (tp->t_flags & TF_DELACK) {
1023*22ce4affSfengbojiang 					/* Delayed ack timer should be up activate a timer */
1024*22ce4affSfengbojiang 					tp->t_flags &= ~TF_DELACK;
1025*22ce4affSfengbojiang 					tcp_timer_activate(tp, TT_DELACK,
1026*22ce4affSfengbojiang 					    tcp_delacktime);
1027*22ce4affSfengbojiang 				}
1028*22ce4affSfengbojiang 			}
1029*22ce4affSfengbojiang 			break;
1030*22ce4affSfengbojiang 		case TT_REXMT:
1031*22ce4affSfengbojiang 			if (tp->t_timers->tt_flags & TT_REXMT_SUS) {
1032*22ce4affSfengbojiang 				tp->t_timers->tt_flags &= ~TT_REXMT_SUS;
1033*22ce4affSfengbojiang 				if (SEQ_GT(tp->snd_max, tp->snd_una) &&
1034*22ce4affSfengbojiang 				    (tcp_timer_active((tp), TT_PERSIST) == 0) &&
1035*22ce4affSfengbojiang 				    tp->snd_wnd) {
1036*22ce4affSfengbojiang 					/* We have outstanding data activate a timer */
1037*22ce4affSfengbojiang 					tcp_timer_activate(tp, TT_REXMT,
1038*22ce4affSfengbojiang                                             tp->t_rxtcur);
1039*22ce4affSfengbojiang 				}
1040*22ce4affSfengbojiang 			}
1041*22ce4affSfengbojiang 			break;
1042*22ce4affSfengbojiang 		case TT_PERSIST:
1043*22ce4affSfengbojiang 			if (tp->t_timers->tt_flags & TT_PERSIST_SUS) {
1044*22ce4affSfengbojiang 				tp->t_timers->tt_flags &= ~TT_PERSIST_SUS;
1045*22ce4affSfengbojiang 				if (tp->snd_wnd == 0) {
1046*22ce4affSfengbojiang 					/* Activate the persists timer */
1047*22ce4affSfengbojiang 					tp->t_rxtshift = 0;
1048*22ce4affSfengbojiang 					tcp_setpersist(tp);
1049*22ce4affSfengbojiang 				}
1050*22ce4affSfengbojiang 			}
1051*22ce4affSfengbojiang 			break;
1052*22ce4affSfengbojiang 		case TT_KEEP:
1053*22ce4affSfengbojiang 			if (tp->t_timers->tt_flags & TT_KEEP_SUS) {
1054*22ce4affSfengbojiang 				tp->t_timers->tt_flags &= ~TT_KEEP_SUS;
1055*22ce4affSfengbojiang 				tcp_timer_activate(tp, TT_KEEP,
1056*22ce4affSfengbojiang 					    TCPS_HAVEESTABLISHED(tp->t_state) ?
1057*22ce4affSfengbojiang 					    TP_KEEPIDLE(tp) : TP_KEEPINIT(tp));
1058*22ce4affSfengbojiang 			}
1059*22ce4affSfengbojiang 			break;
1060*22ce4affSfengbojiang 		case TT_2MSL:
1061*22ce4affSfengbojiang 			if (tp->t_timers->tt_flags &= TT_2MSL_SUS) {
1062*22ce4affSfengbojiang 				tp->t_timers->tt_flags &= ~TT_2MSL_SUS;
1063*22ce4affSfengbojiang 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1064*22ce4affSfengbojiang 				    ((tp->t_inpcb->inp_socket == NULL) ||
1065*22ce4affSfengbojiang 				     (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE))) {
1066*22ce4affSfengbojiang 					/* Star the 2MSL timer */
1067*22ce4affSfengbojiang 					tcp_timer_activate(tp, TT_2MSL,
1068*22ce4affSfengbojiang 					    (tcp_fast_finwait2_recycle) ?
1069*22ce4affSfengbojiang 					    tcp_finwait2_timeout : TP_MAXIDLE(tp));
1070*22ce4affSfengbojiang 				}
1071*22ce4affSfengbojiang 			}
1072*22ce4affSfengbojiang 			break;
1073*22ce4affSfengbojiang 		default:
1074*22ce4affSfengbojiang 			panic("tp:%p bad timer_type 0x%x", tp, timer_type);
1075*22ce4affSfengbojiang 	}
1076*22ce4affSfengbojiang }
1077*22ce4affSfengbojiang 
1078a9643ea8Slogwang void
tcp_timer_stop(struct tcpcb * tp,uint32_t timer_type)1079a9643ea8Slogwang tcp_timer_stop(struct tcpcb *tp, uint32_t timer_type)
1080a9643ea8Slogwang {
1081a9643ea8Slogwang 	struct callout *t_callout;
1082a9643ea8Slogwang 
1083a9643ea8Slogwang 	tp->t_timers->tt_flags |= TT_STOPPED;
1084a9643ea8Slogwang 	switch (timer_type) {
1085a9643ea8Slogwang 		case TT_DELACK:
1086a9643ea8Slogwang 			t_callout = &tp->t_timers->tt_delack;
1087a9643ea8Slogwang 			break;
1088a9643ea8Slogwang 		case TT_REXMT:
1089a9643ea8Slogwang 			t_callout = &tp->t_timers->tt_rexmt;
1090a9643ea8Slogwang 			break;
1091a9643ea8Slogwang 		case TT_PERSIST:
1092a9643ea8Slogwang 			t_callout = &tp->t_timers->tt_persist;
1093a9643ea8Slogwang 			break;
1094a9643ea8Slogwang 		case TT_KEEP:
1095a9643ea8Slogwang 			t_callout = &tp->t_timers->tt_keep;
1096a9643ea8Slogwang 			break;
1097a9643ea8Slogwang 		case TT_2MSL:
1098a9643ea8Slogwang 			t_callout = &tp->t_timers->tt_2msl;
1099a9643ea8Slogwang 			break;
1100a9643ea8Slogwang 		default:
1101a9643ea8Slogwang 			if (tp->t_fb->tfb_tcp_timer_stop) {
1102a9643ea8Slogwang 				/*
1103a9643ea8Slogwang 				 * XXXrrs we need to look at this with the
1104a9643ea8Slogwang 				 * stop case below (flags).
1105a9643ea8Slogwang 				 */
1106a9643ea8Slogwang 				tp->t_fb->tfb_tcp_timer_stop(tp, timer_type);
1107a9643ea8Slogwang 				return;
1108a9643ea8Slogwang 			}
1109a9643ea8Slogwang 			panic("tp %p bad timer_type %#x", tp, timer_type);
1110a9643ea8Slogwang 		}
1111a9643ea8Slogwang 
1112a9643ea8Slogwang 	if (callout_async_drain(t_callout, tcp_timer_discard) == 0) {
1113a9643ea8Slogwang 		/*
1114a9643ea8Slogwang 		 * Can't stop the callout, defer tcpcb actual deletion
1115a9643ea8Slogwang 		 * to the last one. We do this using the async drain
1116a9643ea8Slogwang 		 * function and incrementing the count in
1117a9643ea8Slogwang 		 */
1118a9643ea8Slogwang 		tp->t_timers->tt_draincnt++;
1119a9643ea8Slogwang 	}
1120a9643ea8Slogwang }
1121