xref: /freebsd/sys/netinet/tcp_timer.c (revision bd81e07d2761cf1c13063eb49a5c0cb4a6951318)
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)tcp_timer.c	8.2 (Berkeley) 5/24/95
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_inet.h"
36 #include "opt_inet6.h"
37 #include "opt_tcpdebug.h"
38 #include "opt_rss.h"
39 
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/mbuf.h>
44 #include <sys/mutex.h>
45 #include <sys/protosw.h>
46 #include <sys/smp.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
51 
52 #include <net/if.h>
53 #include <net/route.h>
54 #include <net/rss_config.h>
55 #include <net/vnet.h>
56 #include <net/netisr.h>
57 
58 #include <netinet/cc.h>
59 #include <netinet/in.h>
60 #include <netinet/in_pcb.h>
61 #include <netinet/in_rss.h>
62 #include <netinet/in_systm.h>
63 #ifdef INET6
64 #include <netinet6/in6_pcb.h>
65 #endif
66 #include <netinet/ip_var.h>
67 #include <netinet/tcp_fsm.h>
68 #include <netinet/tcp_timer.h>
69 #include <netinet/tcp_var.h>
70 #ifdef INET6
71 #include <netinet6/tcp6_var.h>
72 #endif
73 #include <netinet/tcpip.h>
74 #ifdef TCPDEBUG
75 #include <netinet/tcp_debug.h>
76 #endif
77 
78 int	tcp_keepinit;
79 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
80     &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "time to establish connection");
81 
82 int	tcp_keepidle;
83 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
84     &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "time before keepalive probes begin");
85 
86 int	tcp_keepintvl;
87 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
88     &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "time between keepalive probes");
89 
90 int	tcp_delacktime;
91 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime, CTLTYPE_INT|CTLFLAG_RW,
92     &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
93     "Time before a delayed ACK is sent");
94 
95 int	tcp_msl;
96 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
97     &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
98 
99 int	tcp_rexmit_min;
100 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
101     &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I",
102     "Minimum Retransmission Timeout");
103 
104 int	tcp_rexmit_slop;
105 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
106     &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I",
107     "Retransmission Timer Slop");
108 
109 static int	always_keepalive = 1;
110 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
111     &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
112 
113 int    tcp_fast_finwait2_recycle = 0;
114 SYSCTL_INT(_net_inet_tcp, OID_AUTO, fast_finwait2_recycle, CTLFLAG_RW,
115     &tcp_fast_finwait2_recycle, 0,
116     "Recycle closed FIN_WAIT_2 connections faster");
117 
118 int    tcp_finwait2_timeout;
119 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, finwait2_timeout, CTLTYPE_INT|CTLFLAG_RW,
120     &tcp_finwait2_timeout, 0, sysctl_msec_to_ticks, "I", "FIN-WAIT2 timeout");
121 
122 int	tcp_keepcnt = TCPTV_KEEPCNT;
123 SYSCTL_INT(_net_inet_tcp, OID_AUTO, keepcnt, CTLFLAG_RW, &tcp_keepcnt, 0,
124     "Number of keepalive probes to send");
125 
126 	/* max idle probes */
127 int	tcp_maxpersistidle;
128 
129 static int	tcp_rexmit_drop_options = 0;
130 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rexmit_drop_options, CTLFLAG_RW,
131     &tcp_rexmit_drop_options, 0,
132     "Drop TCP options from 3rd and later retransmitted SYN");
133 
134 static VNET_DEFINE(int, tcp_pmtud_blackhole_detect);
135 #define	V_tcp_pmtud_blackhole_detect	VNET(tcp_pmtud_blackhole_detect)
136 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_detection,
137     CTLFLAG_RW|CTLFLAG_VNET,
138     &VNET_NAME(tcp_pmtud_blackhole_detect), 0,
139     "Path MTU Discovery Black Hole Detection Enabled");
140 
141 static VNET_DEFINE(int, tcp_pmtud_blackhole_activated);
142 #define	V_tcp_pmtud_blackhole_activated \
143     VNET(tcp_pmtud_blackhole_activated)
144 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_activated,
145     CTLFLAG_RD|CTLFLAG_VNET,
146     &VNET_NAME(tcp_pmtud_blackhole_activated), 0,
147     "Path MTU Discovery Black Hole Detection, Activation Count");
148 
149 static VNET_DEFINE(int, tcp_pmtud_blackhole_activated_min_mss);
150 #define	V_tcp_pmtud_blackhole_activated_min_mss \
151     VNET(tcp_pmtud_blackhole_activated_min_mss)
152 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_activated_min_mss,
153     CTLFLAG_RD|CTLFLAG_VNET,
154     &VNET_NAME(tcp_pmtud_blackhole_activated_min_mss), 0,
155     "Path MTU Discovery Black Hole Detection, Activation Count at min MSS");
156 
157 static VNET_DEFINE(int, tcp_pmtud_blackhole_failed);
158 #define	V_tcp_pmtud_blackhole_failed	VNET(tcp_pmtud_blackhole_failed)
159 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_failed,
160     CTLFLAG_RD|CTLFLAG_VNET,
161     &VNET_NAME(tcp_pmtud_blackhole_failed), 0,
162     "Path MTU Discovery Black Hole Detection, Failure Count");
163 
164 #ifdef INET
165 static VNET_DEFINE(int, tcp_pmtud_blackhole_mss) = 1200;
166 #define	V_tcp_pmtud_blackhole_mss	VNET(tcp_pmtud_blackhole_mss)
167 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_mss,
168     CTLFLAG_RW|CTLFLAG_VNET,
169     &VNET_NAME(tcp_pmtud_blackhole_mss), 0,
170     "Path MTU Discovery Black Hole Detection lowered MSS");
171 #endif
172 
173 #ifdef INET6
174 static VNET_DEFINE(int, tcp_v6pmtud_blackhole_mss) = 1220;
175 #define	V_tcp_v6pmtud_blackhole_mss	VNET(tcp_v6pmtud_blackhole_mss)
176 SYSCTL_INT(_net_inet_tcp, OID_AUTO, v6pmtud_blackhole_mss,
177     CTLFLAG_RW|CTLFLAG_VNET,
178     &VNET_NAME(tcp_v6pmtud_blackhole_mss), 0,
179     "Path MTU Discovery IPv6 Black Hole Detection lowered MSS");
180 #endif
181 
182 #ifdef	RSS
183 static int	per_cpu_timers = 1;
184 #else
185 static int	per_cpu_timers = 0;
186 #endif
187 SYSCTL_INT(_net_inet_tcp, OID_AUTO, per_cpu_timers, CTLFLAG_RW,
188     &per_cpu_timers , 0, "run tcp timers on all cpus");
189 
190 #if 0
191 #define	INP_CPU(inp)	(per_cpu_timers ? (!CPU_ABSENT(((inp)->inp_flowid % (mp_maxid+1))) ? \
192 		((inp)->inp_flowid % (mp_maxid+1)) : curcpu) : 0)
193 #endif
194 
195 /*
196  * Map the given inp to a CPU id.
197  *
198  * This queries RSS if it's compiled in, else it defaults to the current
199  * CPU ID.
200  */
201 static inline int
202 inp_to_cpuid(struct inpcb *inp)
203 {
204 	u_int cpuid;
205 
206 #ifdef	RSS
207 	if (per_cpu_timers) {
208 		cpuid = rss_hash2cpuid(inp->inp_flowid, inp->inp_flowtype);
209 		if (cpuid == NETISR_CPUID_NONE)
210 			return (curcpu);	/* XXX */
211 		else
212 			return (cpuid);
213 	}
214 #else
215 	/* Legacy, pre-RSS behaviour */
216 	if (per_cpu_timers) {
217 		/*
218 		 * We don't have a flowid -> cpuid mapping, so cheat and
219 		 * just map unknown cpuids to curcpu.  Not the best, but
220 		 * apparently better than defaulting to swi 0.
221 		 */
222 		cpuid = inp->inp_flowid % (mp_maxid + 1);
223 		if (! CPU_ABSENT(cpuid))
224 			return (cpuid);
225 		return (curcpu);
226 	}
227 #endif
228 	/* Default for RSS and non-RSS - cpuid 0 */
229 	else {
230 		return (0);
231 	}
232 }
233 
234 /*
235  * Tcp protocol timeout routine called every 500 ms.
236  * Updates timestamps used for TCP
237  * causes finite state machine actions if timers expire.
238  */
239 void
240 tcp_slowtimo(void)
241 {
242 	VNET_ITERATOR_DECL(vnet_iter);
243 
244 	VNET_LIST_RLOCK_NOSLEEP();
245 	VNET_FOREACH(vnet_iter) {
246 		CURVNET_SET(vnet_iter);
247 		(void) tcp_tw_2msl_scan(0);
248 		CURVNET_RESTORE();
249 	}
250 	VNET_LIST_RUNLOCK_NOSLEEP();
251 }
252 
253 int	tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
254     { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
255 
256 int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
257     { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
258 
259 static int tcp_totbackoff = 2559;	/* sum of tcp_backoff[] */
260 
261 /*
262  * TCP timer processing.
263  */
264 
265 void
266 tcp_timer_delack(void *xtp)
267 {
268 	struct tcpcb *tp = xtp;
269 	struct inpcb *inp;
270 	CURVNET_SET(tp->t_vnet);
271 
272 	inp = tp->t_inpcb;
273 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
274 	INP_WLOCK(inp);
275 	if (callout_pending(&tp->t_timers->tt_delack) ||
276 	    !callout_active(&tp->t_timers->tt_delack)) {
277 		INP_WUNLOCK(inp);
278 		CURVNET_RESTORE();
279 		return;
280 	}
281 	callout_deactivate(&tp->t_timers->tt_delack);
282 	if ((inp->inp_flags & INP_DROPPED) != 0) {
283 		INP_WUNLOCK(inp);
284 		CURVNET_RESTORE();
285 		return;
286 	}
287 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
288 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
289 	KASSERT((tp->t_timers->tt_flags & TT_DELACK) != 0,
290 		("%s: tp %p delack callout should be running", __func__, tp));
291 
292 	tp->t_flags |= TF_ACKNOW;
293 	TCPSTAT_INC(tcps_delack);
294 	(void) tcp_output(tp);
295 	INP_WUNLOCK(inp);
296 	CURVNET_RESTORE();
297 }
298 
299 void
300 tcp_timer_2msl(void *xtp)
301 {
302 	struct tcpcb *tp = xtp;
303 	struct inpcb *inp;
304 	CURVNET_SET(tp->t_vnet);
305 #ifdef TCPDEBUG
306 	int ostate;
307 
308 	ostate = tp->t_state;
309 #endif
310 	INP_INFO_RLOCK(&V_tcbinfo);
311 	inp = tp->t_inpcb;
312 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
313 	INP_WLOCK(inp);
314 	tcp_free_sackholes(tp);
315 	if (callout_pending(&tp->t_timers->tt_2msl) ||
316 	    !callout_active(&tp->t_timers->tt_2msl)) {
317 		INP_WUNLOCK(tp->t_inpcb);
318 		INP_INFO_RUNLOCK(&V_tcbinfo);
319 		CURVNET_RESTORE();
320 		return;
321 	}
322 	callout_deactivate(&tp->t_timers->tt_2msl);
323 	if ((inp->inp_flags & INP_DROPPED) != 0) {
324 		INP_WUNLOCK(inp);
325 		INP_INFO_RUNLOCK(&V_tcbinfo);
326 		CURVNET_RESTORE();
327 		return;
328 	}
329 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
330 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
331 	KASSERT((tp->t_timers->tt_flags & TT_2MSL) != 0,
332 		("%s: tp %p 2msl callout should be running", __func__, tp));
333 	/*
334 	 * 2 MSL timeout in shutdown went off.  If we're closed but
335 	 * still waiting for peer to close and connection has been idle
336 	 * too long delete connection control block.  Otherwise, check
337 	 * again in a bit.
338 	 *
339 	 * If in TIME_WAIT state just ignore as this timeout is handled in
340 	 * tcp_tw_2msl_scan().
341 	 *
342 	 * If fastrecycle of FIN_WAIT_2, in FIN_WAIT_2 and receiver has closed,
343 	 * there's no point in hanging onto FIN_WAIT_2 socket. Just close it.
344 	 * Ignore fact that there were recent incoming segments.
345 	 */
346 	if ((inp->inp_flags & INP_TIMEWAIT) != 0) {
347 		INP_WUNLOCK(inp);
348 		INP_INFO_RUNLOCK(&V_tcbinfo);
349 		CURVNET_RESTORE();
350 		return;
351 	}
352 	if (tcp_fast_finwait2_recycle && tp->t_state == TCPS_FIN_WAIT_2 &&
353 	    tp->t_inpcb && tp->t_inpcb->inp_socket &&
354 	    (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE)) {
355 		TCPSTAT_INC(tcps_finwait2_drops);
356 		tp = tcp_close(tp);
357 	} else {
358 		if (ticks - tp->t_rcvtime <= TP_MAXIDLE(tp))
359 		       callout_reset(&tp->t_timers->tt_2msl,
360 			   TP_KEEPINTVL(tp), tcp_timer_2msl, tp);
361 		else
362 		       tp = tcp_close(tp);
363        }
364 
365 #ifdef TCPDEBUG
366 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
367 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
368 			  PRU_SLOWTIMO);
369 #endif
370 	if (tp != NULL)
371 		INP_WUNLOCK(inp);
372 	INP_INFO_RUNLOCK(&V_tcbinfo);
373 	CURVNET_RESTORE();
374 }
375 
376 void
377 tcp_timer_keep(void *xtp)
378 {
379 	struct tcpcb *tp = xtp;
380 	struct tcptemp *t_template;
381 	struct inpcb *inp;
382 	CURVNET_SET(tp->t_vnet);
383 #ifdef TCPDEBUG
384 	int ostate;
385 
386 	ostate = tp->t_state;
387 #endif
388 	INP_INFO_RLOCK(&V_tcbinfo);
389 	inp = tp->t_inpcb;
390 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
391 	INP_WLOCK(inp);
392 	if (callout_pending(&tp->t_timers->tt_keep) ||
393 	    !callout_active(&tp->t_timers->tt_keep)) {
394 		INP_WUNLOCK(inp);
395 		INP_INFO_RUNLOCK(&V_tcbinfo);
396 		CURVNET_RESTORE();
397 		return;
398 	}
399 	callout_deactivate(&tp->t_timers->tt_keep);
400 	if ((inp->inp_flags & INP_DROPPED) != 0) {
401 		INP_WUNLOCK(inp);
402 		INP_INFO_RUNLOCK(&V_tcbinfo);
403 		CURVNET_RESTORE();
404 		return;
405 	}
406 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
407 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
408 	KASSERT((tp->t_timers->tt_flags & TT_KEEP) != 0,
409 		("%s: tp %p keep callout should be running", __func__, tp));
410 	/*
411 	 * Keep-alive timer went off; send something
412 	 * or drop connection if idle for too long.
413 	 */
414 	TCPSTAT_INC(tcps_keeptimeo);
415 	if (tp->t_state < TCPS_ESTABLISHED)
416 		goto dropit;
417 	if ((always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
418 	    tp->t_state <= TCPS_CLOSING) {
419 		if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
420 			goto dropit;
421 		/*
422 		 * Send a packet designed to force a response
423 		 * if the peer is up and reachable:
424 		 * either an ACK if the connection is still alive,
425 		 * or an RST if the peer has closed the connection
426 		 * due to timeout or reboot.
427 		 * Using sequence number tp->snd_una-1
428 		 * causes the transmitted zero-length segment
429 		 * to lie outside the receive window;
430 		 * by the protocol spec, this requires the
431 		 * correspondent TCP to respond.
432 		 */
433 		TCPSTAT_INC(tcps_keepprobe);
434 		t_template = tcpip_maketemplate(inp);
435 		if (t_template) {
436 			tcp_respond(tp, t_template->tt_ipgen,
437 				    &t_template->tt_t, (struct mbuf *)NULL,
438 				    tp->rcv_nxt, tp->snd_una - 1, 0);
439 			free(t_template, M_TEMP);
440 		}
441 		callout_reset(&tp->t_timers->tt_keep, TP_KEEPINTVL(tp),
442 		    tcp_timer_keep, tp);
443 	} else
444 		callout_reset(&tp->t_timers->tt_keep, TP_KEEPIDLE(tp),
445 		    tcp_timer_keep, tp);
446 
447 #ifdef TCPDEBUG
448 	if (inp->inp_socket->so_options & SO_DEBUG)
449 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
450 			  PRU_SLOWTIMO);
451 #endif
452 	INP_WUNLOCK(inp);
453 	INP_INFO_RUNLOCK(&V_tcbinfo);
454 	CURVNET_RESTORE();
455 	return;
456 
457 dropit:
458 	TCPSTAT_INC(tcps_keepdrops);
459 	tp = tcp_drop(tp, ETIMEDOUT);
460 
461 #ifdef TCPDEBUG
462 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
463 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
464 			  PRU_SLOWTIMO);
465 #endif
466 	if (tp != NULL)
467 		INP_WUNLOCK(tp->t_inpcb);
468 	INP_INFO_RUNLOCK(&V_tcbinfo);
469 	CURVNET_RESTORE();
470 }
471 
472 void
473 tcp_timer_persist(void *xtp)
474 {
475 	struct tcpcb *tp = xtp;
476 	struct inpcb *inp;
477 	CURVNET_SET(tp->t_vnet);
478 #ifdef TCPDEBUG
479 	int ostate;
480 
481 	ostate = tp->t_state;
482 #endif
483 	INP_INFO_RLOCK(&V_tcbinfo);
484 	inp = tp->t_inpcb;
485 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
486 	INP_WLOCK(inp);
487 	if (callout_pending(&tp->t_timers->tt_persist) ||
488 	    !callout_active(&tp->t_timers->tt_persist)) {
489 		INP_WUNLOCK(inp);
490 		INP_INFO_RUNLOCK(&V_tcbinfo);
491 		CURVNET_RESTORE();
492 		return;
493 	}
494 	callout_deactivate(&tp->t_timers->tt_persist);
495 	if ((inp->inp_flags & INP_DROPPED) != 0) {
496 		INP_WUNLOCK(inp);
497 		INP_INFO_RUNLOCK(&V_tcbinfo);
498 		CURVNET_RESTORE();
499 		return;
500 	}
501 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
502 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
503 	KASSERT((tp->t_timers->tt_flags & TT_PERSIST) != 0,
504 		("%s: tp %p persist callout should be running", __func__, tp));
505 	/*
506 	 * Persistance timer into zero window.
507 	 * Force a byte to be output, if possible.
508 	 */
509 	TCPSTAT_INC(tcps_persisttimeo);
510 	/*
511 	 * Hack: if the peer is dead/unreachable, we do not
512 	 * time out if the window is closed.  After a full
513 	 * backoff, drop the connection if the idle time
514 	 * (no responses to probes) reaches the maximum
515 	 * backoff that we would use if retransmitting.
516 	 */
517 	if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
518 	    (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
519 	     ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
520 		TCPSTAT_INC(tcps_persistdrop);
521 		tp = tcp_drop(tp, ETIMEDOUT);
522 		goto out;
523 	}
524 	/*
525 	 * If the user has closed the socket then drop a persisting
526 	 * connection after a much reduced timeout.
527 	 */
528 	if (tp->t_state > TCPS_CLOSE_WAIT &&
529 	    (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
530 		TCPSTAT_INC(tcps_persistdrop);
531 		tp = tcp_drop(tp, ETIMEDOUT);
532 		goto out;
533 	}
534 	tcp_setpersist(tp);
535 	tp->t_flags |= TF_FORCEDATA;
536 	(void) tcp_output(tp);
537 	tp->t_flags &= ~TF_FORCEDATA;
538 
539 out:
540 #ifdef TCPDEBUG
541 	if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
542 		tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
543 #endif
544 	if (tp != NULL)
545 		INP_WUNLOCK(inp);
546 	INP_INFO_RUNLOCK(&V_tcbinfo);
547 	CURVNET_RESTORE();
548 }
549 
550 void
551 tcp_timer_rexmt(void * xtp)
552 {
553 	struct tcpcb *tp = xtp;
554 	CURVNET_SET(tp->t_vnet);
555 	int rexmt;
556 	int headlocked;
557 	struct inpcb *inp;
558 #ifdef TCPDEBUG
559 	int ostate;
560 
561 	ostate = tp->t_state;
562 #endif
563 
564 	INP_INFO_RLOCK(&V_tcbinfo);
565 	inp = tp->t_inpcb;
566 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
567 	INP_WLOCK(inp);
568 	if (callout_pending(&tp->t_timers->tt_rexmt) ||
569 	    !callout_active(&tp->t_timers->tt_rexmt)) {
570 		INP_WUNLOCK(inp);
571 		INP_INFO_RUNLOCK(&V_tcbinfo);
572 		CURVNET_RESTORE();
573 		return;
574 	}
575 	callout_deactivate(&tp->t_timers->tt_rexmt);
576 	if ((inp->inp_flags & INP_DROPPED) != 0) {
577 		INP_WUNLOCK(inp);
578 		INP_INFO_RUNLOCK(&V_tcbinfo);
579 		CURVNET_RESTORE();
580 		return;
581 	}
582 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
583 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
584 	KASSERT((tp->t_timers->tt_flags & TT_REXMT) != 0,
585 		("%s: tp %p rexmt callout should be running", __func__, tp));
586 	tcp_free_sackholes(tp);
587 	/*
588 	 * Retransmission timer went off.  Message has not
589 	 * been acked within retransmit interval.  Back off
590 	 * to a longer retransmit interval and retransmit one segment.
591 	 */
592 	if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
593 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
594 		TCPSTAT_INC(tcps_timeoutdrop);
595 
596 		tp = tcp_drop(tp, tp->t_softerror ?
597 			      tp->t_softerror : ETIMEDOUT);
598 		headlocked = 1;
599 		goto out;
600 	}
601 	INP_INFO_RUNLOCK(&V_tcbinfo);
602 	headlocked = 0;
603 	if (tp->t_state == TCPS_SYN_SENT) {
604 		/*
605 		 * If the SYN was retransmitted, indicate CWND to be
606 		 * limited to 1 segment in cc_conn_init().
607 		 */
608 		tp->snd_cwnd = 1;
609 	} else if (tp->t_rxtshift == 1) {
610 		/*
611 		 * first retransmit; record ssthresh and cwnd so they can
612 		 * be recovered if this turns out to be a "bad" retransmit.
613 		 * A retransmit is considered "bad" if an ACK for this
614 		 * segment is received within RTT/2 interval; the assumption
615 		 * here is that the ACK was already in flight.  See
616 		 * "On Estimating End-to-End Network Path Properties" by
617 		 * Allman and Paxson for more details.
618 		 */
619 		tp->snd_cwnd_prev = tp->snd_cwnd;
620 		tp->snd_ssthresh_prev = tp->snd_ssthresh;
621 		tp->snd_recover_prev = tp->snd_recover;
622 		if (IN_FASTRECOVERY(tp->t_flags))
623 			tp->t_flags |= TF_WASFRECOVERY;
624 		else
625 			tp->t_flags &= ~TF_WASFRECOVERY;
626 		if (IN_CONGRECOVERY(tp->t_flags))
627 			tp->t_flags |= TF_WASCRECOVERY;
628 		else
629 			tp->t_flags &= ~TF_WASCRECOVERY;
630 		tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
631 		tp->t_flags |= TF_PREVVALID;
632 	} else
633 		tp->t_flags &= ~TF_PREVVALID;
634 	TCPSTAT_INC(tcps_rexmttimeo);
635 	if (tp->t_state == TCPS_SYN_SENT)
636 		rexmt = TCPTV_RTOBASE * tcp_syn_backoff[tp->t_rxtshift];
637 	else
638 		rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
639 	TCPT_RANGESET(tp->t_rxtcur, rexmt,
640 		      tp->t_rttmin, TCPTV_REXMTMAX);
641 
642 	/*
643 	 * We enter the path for PLMTUD if connection is established or, if
644 	 * connection is FIN_WAIT_1 status, reason for the last is that if
645 	 * amount of data we send is very small, we could send it in couple of
646 	 * packets and process straight to FIN. In that case we won't catch
647 	 * ESTABLISHED state.
648 	 */
649 	if (V_tcp_pmtud_blackhole_detect && (((tp->t_state == TCPS_ESTABLISHED))
650 	    || (tp->t_state == TCPS_FIN_WAIT_1))) {
651 		int optlen;
652 #ifdef INET6
653 		int isipv6;
654 #endif
655 
656 		if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) ==
657 		    (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) &&
658 		    (tp->t_rxtshift <= 2)) {
659 			/*
660 			 * Enter Path MTU Black-hole Detection mechanism:
661 			 * - Disable Path MTU Discovery (IP "DF" bit).
662 			 * - Reduce MTU to lower value than what we
663 			 *   negotiated with peer.
664 			 */
665 			/* Record that we may have found a black hole. */
666 			tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
667 
668 			/* Keep track of previous MSS. */
669 			optlen = tp->t_maxopd - tp->t_maxseg;
670 			tp->t_pmtud_saved_maxopd = tp->t_maxopd;
671 
672 			/*
673 			 * Reduce the MSS to blackhole value or to the default
674 			 * in an attempt to retransmit.
675 			 */
676 #ifdef INET6
677 			isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? 1 : 0;
678 			if (isipv6 &&
679 			    tp->t_maxopd > V_tcp_v6pmtud_blackhole_mss) {
680 				/* Use the sysctl tuneable blackhole MSS. */
681 				tp->t_maxopd = V_tcp_v6pmtud_blackhole_mss;
682 				V_tcp_pmtud_blackhole_activated++;
683 			} else if (isipv6) {
684 				/* Use the default MSS. */
685 				tp->t_maxopd = V_tcp_v6mssdflt;
686 				/*
687 				 * Disable Path MTU Discovery when we switch to
688 				 * minmss.
689 				 */
690 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
691 				V_tcp_pmtud_blackhole_activated_min_mss++;
692 			}
693 #endif
694 #if defined(INET6) && defined(INET)
695 			else
696 #endif
697 #ifdef INET
698 			if (tp->t_maxopd > V_tcp_pmtud_blackhole_mss) {
699 				/* Use the sysctl tuneable blackhole MSS. */
700 				tp->t_maxopd = V_tcp_pmtud_blackhole_mss;
701 				V_tcp_pmtud_blackhole_activated++;
702 			} else {
703 				/* Use the default MSS. */
704 				tp->t_maxopd = V_tcp_mssdflt;
705 				/*
706 				 * Disable Path MTU Discovery when we switch to
707 				 * minmss.
708 				 */
709 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
710 				V_tcp_pmtud_blackhole_activated_min_mss++;
711 			}
712 #endif
713 			tp->t_maxseg = tp->t_maxopd - optlen;
714 			/*
715 			 * Reset the slow-start flight size
716 			 * as it may depend on the new MSS.
717 			 */
718 			if (CC_ALGO(tp)->conn_init != NULL)
719 				CC_ALGO(tp)->conn_init(tp->ccv);
720 		} else {
721 			/*
722 			 * If further retransmissions are still unsuccessful
723 			 * with a lowered MTU, maybe this isn't a blackhole and
724 			 * we restore the previous MSS and blackhole detection
725 			 * flags.
726 			 */
727 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
728 			    (tp->t_rxtshift > 4)) {
729 				tp->t_flags2 |= TF2_PLPMTU_PMTUD;
730 				tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
731 				optlen = tp->t_maxopd - tp->t_maxseg;
732 				tp->t_maxopd = tp->t_pmtud_saved_maxopd;
733 				tp->t_maxseg = tp->t_maxopd - optlen;
734 				V_tcp_pmtud_blackhole_failed++;
735 				/*
736 				 * Reset the slow-start flight size as it
737 				 * may depend on the new MSS.
738 				 */
739 				if (CC_ALGO(tp)->conn_init != NULL)
740 					CC_ALGO(tp)->conn_init(tp->ccv);
741 			}
742 		}
743 	}
744 
745 	/*
746 	 * Disable RFC1323 and SACK if we haven't got any response to
747 	 * our third SYN to work-around some broken terminal servers
748 	 * (most of which have hopefully been retired) that have bad VJ
749 	 * header compression code which trashes TCP segments containing
750 	 * unknown-to-them TCP options.
751 	 */
752 	if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
753 	    (tp->t_rxtshift == 3))
754 		tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
755 	/*
756 	 * If we backed off this far, our srtt estimate is probably bogus.
757 	 * Clobber it so we'll take the next rtt measurement as our srtt;
758 	 * move the current srtt into rttvar to keep the current
759 	 * retransmit times until then.
760 	 */
761 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
762 #ifdef INET6
763 		if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
764 			in6_losing(tp->t_inpcb);
765 #endif
766 		tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
767 		tp->t_srtt = 0;
768 	}
769 	tp->snd_nxt = tp->snd_una;
770 	tp->snd_recover = tp->snd_max;
771 	/*
772 	 * Force a segment to be sent.
773 	 */
774 	tp->t_flags |= TF_ACKNOW;
775 	/*
776 	 * If timing a segment in this window, stop the timer.
777 	 */
778 	tp->t_rtttime = 0;
779 
780 	cc_cong_signal(tp, NULL, CC_RTO);
781 
782 	(void) tcp_output(tp);
783 
784 out:
785 #ifdef TCPDEBUG
786 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
787 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
788 			  PRU_SLOWTIMO);
789 #endif
790 	if (tp != NULL)
791 		INP_WUNLOCK(inp);
792 	if (headlocked)
793 		INP_INFO_RUNLOCK(&V_tcbinfo);
794 	CURVNET_RESTORE();
795 }
796 
797 void
798 tcp_timer_activate(struct tcpcb *tp, uint32_t timer_type, u_int delta)
799 {
800 	struct callout *t_callout;
801 	timeout_t *f_callout;
802 	struct inpcb *inp = tp->t_inpcb;
803 	int cpu = inp_to_cpuid(inp);
804 
805 #ifdef TCP_OFFLOAD
806 	if (tp->t_flags & TF_TOE)
807 		return;
808 #endif
809 
810 	if (tp->t_timers->tt_flags & TT_STOPPED)
811 		return;
812 
813 	switch (timer_type) {
814 		case TT_DELACK:
815 			t_callout = &tp->t_timers->tt_delack;
816 			f_callout = tcp_timer_delack;
817 			break;
818 		case TT_REXMT:
819 			t_callout = &tp->t_timers->tt_rexmt;
820 			f_callout = tcp_timer_rexmt;
821 			break;
822 		case TT_PERSIST:
823 			t_callout = &tp->t_timers->tt_persist;
824 			f_callout = tcp_timer_persist;
825 			break;
826 		case TT_KEEP:
827 			t_callout = &tp->t_timers->tt_keep;
828 			f_callout = tcp_timer_keep;
829 			break;
830 		case TT_2MSL:
831 			t_callout = &tp->t_timers->tt_2msl;
832 			f_callout = tcp_timer_2msl;
833 			break;
834 		default:
835 			panic("tp %p bad timer_type %#x", tp, timer_type);
836 		}
837 	if (delta == 0) {
838 		if ((tp->t_timers->tt_flags & timer_type) &&
839 		    callout_stop(t_callout)) {
840 			tp->t_timers->tt_flags &= ~timer_type;
841 		}
842 	} else {
843 		if ((tp->t_timers->tt_flags & timer_type) == 0) {
844 			tp->t_timers->tt_flags |= timer_type;
845 			callout_reset_on(t_callout, delta, f_callout, tp, cpu);
846 		} else {
847 			/* Reset already running callout on the same CPU. */
848 			callout_reset(t_callout, delta, f_callout, tp);
849 		}
850 	}
851 }
852 
853 int
854 tcp_timer_active(struct tcpcb *tp, uint32_t timer_type)
855 {
856 	struct callout *t_callout;
857 
858 	switch (timer_type) {
859 		case TT_DELACK:
860 			t_callout = &tp->t_timers->tt_delack;
861 			break;
862 		case TT_REXMT:
863 			t_callout = &tp->t_timers->tt_rexmt;
864 			break;
865 		case TT_PERSIST:
866 			t_callout = &tp->t_timers->tt_persist;
867 			break;
868 		case TT_KEEP:
869 			t_callout = &tp->t_timers->tt_keep;
870 			break;
871 		case TT_2MSL:
872 			t_callout = &tp->t_timers->tt_2msl;
873 			break;
874 		default:
875 			panic("tp %p bad timer_type %#x", tp, timer_type);
876 		}
877 	return callout_active(t_callout);
878 }
879 
880 void
881 tcp_timer_stop(struct tcpcb *tp, uint32_t timer_type)
882 {
883 	struct callout *t_callout;
884 	timeout_t *f_callout;
885 
886 	tp->t_timers->tt_flags |= TT_STOPPED;
887 
888 	switch (timer_type) {
889 		case TT_DELACK:
890 			t_callout = &tp->t_timers->tt_delack;
891 			f_callout = tcp_timer_delack_discard;
892 			break;
893 		case TT_REXMT:
894 			t_callout = &tp->t_timers->tt_rexmt;
895 			f_callout = tcp_timer_rexmt_discard;
896 			break;
897 		case TT_PERSIST:
898 			t_callout = &tp->t_timers->tt_persist;
899 			f_callout = tcp_timer_persist_discard;
900 			break;
901 		case TT_KEEP:
902 			t_callout = &tp->t_timers->tt_keep;
903 			f_callout = tcp_timer_keep_discard;
904 			break;
905 		case TT_2MSL:
906 			t_callout = &tp->t_timers->tt_2msl;
907 			f_callout = tcp_timer_2msl_discard;
908 			break;
909 		default:
910 			panic("tp %p bad timer_type %#x", tp, timer_type);
911 		}
912 
913 	if (tp->t_timers->tt_flags & timer_type) {
914 		if (callout_stop(t_callout)) {
915 			tp->t_timers->tt_flags &= ~timer_type;
916 		} else {
917 			/*
918 			 * Can't stop the callout, defer tcpcb actual deletion
919 			 * to the last tcp timer discard callout.
920 			 * The TT_STOPPED flag will ensure that no tcp timer
921 			 * callouts can be restarted on our behalf, and
922 			 * past this point currently running callouts waiting
923 			 * on inp lock will return right away after the
924 			 * classical check for callout reset/stop events:
925 			 * callout_pending() || !callout_active()
926 			 */
927 			callout_reset(t_callout, 1, f_callout, tp);
928 		}
929 	}
930 }
931 
932 #define	ticks_to_msecs(t)	(1000*(t) / hz)
933 
934 void
935 tcp_timer_to_xtimer(struct tcpcb *tp, struct tcp_timer *timer,
936     struct xtcp_timer *xtimer)
937 {
938 	sbintime_t now;
939 
940 	bzero(xtimer, sizeof(*xtimer));
941 	if (timer == NULL)
942 		return;
943 	now = getsbinuptime();
944 	if (callout_active(&timer->tt_delack))
945 		xtimer->tt_delack = (timer->tt_delack.c_time - now) / SBT_1MS;
946 	if (callout_active(&timer->tt_rexmt))
947 		xtimer->tt_rexmt = (timer->tt_rexmt.c_time - now) / SBT_1MS;
948 	if (callout_active(&timer->tt_persist))
949 		xtimer->tt_persist = (timer->tt_persist.c_time - now) / SBT_1MS;
950 	if (callout_active(&timer->tt_keep))
951 		xtimer->tt_keep = (timer->tt_keep.c_time - now) / SBT_1MS;
952 	if (callout_active(&timer->tt_2msl))
953 		xtimer->tt_2msl = (timer->tt_2msl.c_time - now) / SBT_1MS;
954 	xtimer->t_rcvtime = ticks_to_msecs(ticks - tp->t_rcvtime);
955 }
956