2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
37 #include "opt_tcpdebug.h"
39 #include <sys/param.h>
40 #include <sys/kernel.h>
43 #include <sys/mutex.h>
44 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
52 #include <net/route.h>
55 #include <netinet/cc.h>
56 #include <netinet/in.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/in_systm.h>
60 #include <netinet6/in6_pcb.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/tcp_fsm.h>
64 #include <netinet/tcp_timer.h>
65 #include <netinet/tcp_var.h>
67 #include <netinet6/tcp6_var.h>
69 #include <netinet/tcpip.h>
71 #include <netinet/tcp_debug.h>
75 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
76 &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "time to establish connection");
79 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
80 &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "time before keepalive probes begin");
83 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
84 &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "time between keepalive probes");
87 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime, CTLTYPE_INT|CTLFLAG_RW,
88 &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
89 "Time before a delayed ACK is sent");
92 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
93 &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
96 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
97 &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I",
98 "Minimum Retransmission Timeout");
101 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
102 &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I",
103 "Retransmission Timer Slop");
105 static int always_keepalive = 1;
106 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
107 &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
109 int tcp_fast_finwait2_recycle = 0;
110 SYSCTL_INT(_net_inet_tcp, OID_AUTO, fast_finwait2_recycle, CTLFLAG_RW,
111 &tcp_fast_finwait2_recycle, 0,
112 "Recycle closed FIN_WAIT_2 connections faster");
114 int tcp_finwait2_timeout;
115 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, finwait2_timeout, CTLTYPE_INT|CTLFLAG_RW,
116 &tcp_finwait2_timeout, 0, sysctl_msec_to_ticks, "I", "FIN-WAIT2 timeout");
118 int tcp_keepcnt = TCPTV_KEEPCNT;
119 SYSCTL_INT(_net_inet_tcp, OID_AUTO, keepcnt, CTLFLAG_RW, &tcp_keepcnt, 0,
120 "Number of keepalive probes to send");
122 /* max idle probes */
123 int tcp_maxpersistidle;
125 static int tcp_rexmit_drop_options = 0;
126 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rexmit_drop_options, CTLFLAG_RW,
127 &tcp_rexmit_drop_options, 0,
128 "Drop TCP options from 3rd and later retransmitted SYN");
130 static VNET_DEFINE(int, tcp_pmtud_blackhole_detect);
131 #define V_tcp_pmtud_blackhole_detect VNET(tcp_pmtud_blackhole_detect)
132 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_detection,
134 &VNET_NAME(tcp_pmtud_blackhole_detect), 0,
135 "Path MTU Discovery Black Hole Detection Enabled");
137 static VNET_DEFINE(int, tcp_pmtud_blackhole_activated);
138 #define V_tcp_pmtud_blackhole_activated \
139 VNET(tcp_pmtud_blackhole_activated)
140 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_activated,
142 &VNET_NAME(tcp_pmtud_blackhole_activated), 0,
143 "Path MTU Discovery Black Hole Detection, Activation Count");
145 static VNET_DEFINE(int, tcp_pmtud_blackhole_activated_min_mss);
146 #define V_tcp_pmtud_blackhole_activated_min_mss \
147 VNET(tcp_pmtud_blackhole_activated_min_mss)
148 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_activated_min_mss,
150 &VNET_NAME(tcp_pmtud_blackhole_activated_min_mss), 0,
151 "Path MTU Discovery Black Hole Detection, Activation Count at min MSS");
153 static VNET_DEFINE(int, tcp_pmtud_blackhole_failed);
154 #define V_tcp_pmtud_blackhole_failed VNET(tcp_pmtud_blackhole_failed)
155 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_failed,
157 &VNET_NAME(tcp_pmtud_blackhole_failed), 0,
158 "Path MTU Discovery Black Hole Detection, Failure Count");
161 static VNET_DEFINE(int, tcp_pmtud_blackhole_mss) = 1200;
162 #define V_tcp_pmtud_blackhole_mss VNET(tcp_pmtud_blackhole_mss)
163 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_mss,
165 &VNET_NAME(tcp_pmtud_blackhole_mss), 0,
166 "Path MTU Discovery Black Hole Detection lowered MSS");
170 static VNET_DEFINE(int, tcp_v6pmtud_blackhole_mss) = 1220;
171 #define V_tcp_v6pmtud_blackhole_mss VNET(tcp_v6pmtud_blackhole_mss)
172 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, v6pmtud_blackhole_mss,
174 &VNET_NAME(tcp_v6pmtud_blackhole_mss), 0,
175 "Path MTU Discovery IPv6 Black Hole Detection lowered MSS");
178 static int per_cpu_timers = 0;
179 SYSCTL_INT(_net_inet_tcp, OID_AUTO, per_cpu_timers, CTLFLAG_RW,
180 &per_cpu_timers , 0, "run tcp timers on all cpus");
182 #define INP_CPU(inp) (per_cpu_timers ? (!CPU_ABSENT(((inp)->inp_flowid % (mp_maxid+1))) ? \
183 ((inp)->inp_flowid % (mp_maxid+1)) : curcpu) : 0)
186 * Tcp protocol timeout routine called every 500 ms.
187 * Updates timestamps used for TCP
188 * causes finite state machine actions if timers expire.
193 VNET_ITERATOR_DECL(vnet_iter);
195 VNET_LIST_RLOCK_NOSLEEP();
196 VNET_FOREACH(vnet_iter) {
197 CURVNET_SET(vnet_iter);
198 (void) tcp_tw_2msl_scan(0);
201 VNET_LIST_RUNLOCK_NOSLEEP();
204 int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
205 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
207 int tcp_backoff[TCP_MAXRXTSHIFT + 1] =
208 { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
210 static int tcp_totbackoff = 2559; /* sum of tcp_backoff[] */
213 * TCP timer processing.
217 tcp_timer_delack(void *xtp)
219 struct tcpcb *tp = xtp;
221 CURVNET_SET(tp->t_vnet);
224 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
226 if (callout_pending(&tp->t_timers->tt_delack) ||
227 !callout_active(&tp->t_timers->tt_delack)) {
232 callout_deactivate(&tp->t_timers->tt_delack);
233 if ((inp->inp_flags & INP_DROPPED) != 0) {
238 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
239 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
240 KASSERT((tp->t_timers->tt_flags & TT_DELACK) != 0,
241 ("%s: tp %p delack callout should be running", __func__, tp));
243 tp->t_flags |= TF_ACKNOW;
244 TCPSTAT_INC(tcps_delack);
245 (void) tcp_output(tp);
251 tcp_timer_2msl(void *xtp)
253 struct tcpcb *tp = xtp;
255 CURVNET_SET(tp->t_vnet);
259 ostate = tp->t_state;
261 INP_INFO_WLOCK(&V_tcbinfo);
263 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
265 tcp_free_sackholes(tp);
266 if (callout_pending(&tp->t_timers->tt_2msl) ||
267 !callout_active(&tp->t_timers->tt_2msl)) {
268 INP_WUNLOCK(tp->t_inpcb);
269 INP_INFO_WUNLOCK(&V_tcbinfo);
273 callout_deactivate(&tp->t_timers->tt_2msl);
274 if ((inp->inp_flags & INP_DROPPED) != 0) {
276 INP_INFO_WUNLOCK(&V_tcbinfo);
280 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
281 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
282 KASSERT((tp->t_timers->tt_flags & TT_2MSL) != 0,
283 ("%s: tp %p 2msl callout should be running", __func__, tp));
285 * 2 MSL timeout in shutdown went off. If we're closed but
286 * still waiting for peer to close and connection has been idle
287 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
288 * control block. Otherwise, check again in a bit.
290 * If fastrecycle of FIN_WAIT_2, in FIN_WAIT_2 and receiver has closed,
291 * there's no point in hanging onto FIN_WAIT_2 socket. Just close it.
292 * Ignore fact that there were recent incoming segments.
294 if (tcp_fast_finwait2_recycle && tp->t_state == TCPS_FIN_WAIT_2 &&
295 tp->t_inpcb && tp->t_inpcb->inp_socket &&
296 (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE)) {
297 TCPSTAT_INC(tcps_finwait2_drops);
300 if (tp->t_state != TCPS_TIME_WAIT &&
301 ticks - tp->t_rcvtime <= TP_MAXIDLE(tp)) {
302 if (!callout_reset(&tp->t_timers->tt_2msl,
303 TP_KEEPINTVL(tp), tcp_timer_2msl, tp)) {
304 tp->t_timers->tt_flags &= ~TT_2MSL_RST;
311 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
312 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
317 INP_INFO_WUNLOCK(&V_tcbinfo);
322 tcp_timer_keep(void *xtp)
324 struct tcpcb *tp = xtp;
325 struct tcptemp *t_template;
327 CURVNET_SET(tp->t_vnet);
331 ostate = tp->t_state;
333 INP_INFO_WLOCK(&V_tcbinfo);
335 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
337 if (callout_pending(&tp->t_timers->tt_keep) ||
338 !callout_active(&tp->t_timers->tt_keep)) {
340 INP_INFO_WUNLOCK(&V_tcbinfo);
344 callout_deactivate(&tp->t_timers->tt_keep);
345 if ((inp->inp_flags & INP_DROPPED) != 0) {
347 INP_INFO_WUNLOCK(&V_tcbinfo);
351 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
352 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
353 KASSERT((tp->t_timers->tt_flags & TT_KEEP) != 0,
354 ("%s: tp %p keep callout should be running", __func__, tp));
356 * Keep-alive timer went off; send something
357 * or drop connection if idle for too long.
359 TCPSTAT_INC(tcps_keeptimeo);
360 if (tp->t_state < TCPS_ESTABLISHED)
362 if ((always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
363 tp->t_state <= TCPS_CLOSING) {
364 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
367 * Send a packet designed to force a response
368 * if the peer is up and reachable:
369 * either an ACK if the connection is still alive,
370 * or an RST if the peer has closed the connection
371 * due to timeout or reboot.
372 * Using sequence number tp->snd_una-1
373 * causes the transmitted zero-length segment
374 * to lie outside the receive window;
375 * by the protocol spec, this requires the
376 * correspondent TCP to respond.
378 TCPSTAT_INC(tcps_keepprobe);
379 t_template = tcpip_maketemplate(inp);
381 tcp_respond(tp, t_template->tt_ipgen,
382 &t_template->tt_t, (struct mbuf *)NULL,
383 tp->rcv_nxt, tp->snd_una - 1, 0);
384 free(t_template, M_TEMP);
386 if (!callout_reset(&tp->t_timers->tt_keep, TP_KEEPINTVL(tp),
387 tcp_timer_keep, tp)) {
388 tp->t_timers->tt_flags &= ~TT_KEEP_RST;
390 } else if (!callout_reset(&tp->t_timers->tt_keep, TP_KEEPIDLE(tp),
391 tcp_timer_keep, tp)) {
392 tp->t_timers->tt_flags &= ~TT_KEEP_RST;
396 if (inp->inp_socket->so_options & SO_DEBUG)
397 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
401 INP_INFO_WUNLOCK(&V_tcbinfo);
406 TCPSTAT_INC(tcps_keepdrops);
407 tp = tcp_drop(tp, ETIMEDOUT);
410 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
411 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
415 INP_WUNLOCK(tp->t_inpcb);
416 INP_INFO_WUNLOCK(&V_tcbinfo);
421 tcp_timer_persist(void *xtp)
423 struct tcpcb *tp = xtp;
425 CURVNET_SET(tp->t_vnet);
429 ostate = tp->t_state;
431 INP_INFO_WLOCK(&V_tcbinfo);
433 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
435 if (callout_pending(&tp->t_timers->tt_persist) ||
436 !callout_active(&tp->t_timers->tt_persist)) {
438 INP_INFO_WUNLOCK(&V_tcbinfo);
442 callout_deactivate(&tp->t_timers->tt_persist);
443 if ((inp->inp_flags & INP_DROPPED) != 0) {
445 INP_INFO_WUNLOCK(&V_tcbinfo);
449 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
450 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
451 KASSERT((tp->t_timers->tt_flags & TT_PERSIST) != 0,
452 ("%s: tp %p persist callout should be running", __func__, tp));
454 * Persistance timer into zero window.
455 * Force a byte to be output, if possible.
457 TCPSTAT_INC(tcps_persisttimeo);
459 * Hack: if the peer is dead/unreachable, we do not
460 * time out if the window is closed. After a full
461 * backoff, drop the connection if the idle time
462 * (no responses to probes) reaches the maximum
463 * backoff that we would use if retransmitting.
465 if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
466 (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
467 ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
468 TCPSTAT_INC(tcps_persistdrop);
469 tp = tcp_drop(tp, ETIMEDOUT);
473 * If the user has closed the socket then drop a persisting
474 * connection after a much reduced timeout.
476 if (tp->t_state > TCPS_CLOSE_WAIT &&
477 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
478 TCPSTAT_INC(tcps_persistdrop);
479 tp = tcp_drop(tp, ETIMEDOUT);
483 tp->t_flags |= TF_FORCEDATA;
484 (void) tcp_output(tp);
485 tp->t_flags &= ~TF_FORCEDATA;
489 if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
490 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
494 INP_INFO_WUNLOCK(&V_tcbinfo);
499 tcp_timer_rexmt(void * xtp)
501 struct tcpcb *tp = xtp;
502 CURVNET_SET(tp->t_vnet);
509 ostate = tp->t_state;
512 INP_INFO_RLOCK(&V_tcbinfo);
514 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
516 if (callout_pending(&tp->t_timers->tt_rexmt) ||
517 !callout_active(&tp->t_timers->tt_rexmt)) {
519 INP_INFO_RUNLOCK(&V_tcbinfo);
523 callout_deactivate(&tp->t_timers->tt_rexmt);
524 if ((inp->inp_flags & INP_DROPPED) != 0) {
526 INP_INFO_RUNLOCK(&V_tcbinfo);
530 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
531 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
532 KASSERT((tp->t_timers->tt_flags & TT_REXMT) != 0,
533 ("%s: tp %p rexmt callout should be running", __func__, tp));
534 tcp_free_sackholes(tp);
536 * Retransmission timer went off. Message has not
537 * been acked within retransmit interval. Back off
538 * to a longer retransmit interval and retransmit one segment.
540 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
541 tp->t_rxtshift = TCP_MAXRXTSHIFT;
542 TCPSTAT_INC(tcps_timeoutdrop);
544 INP_INFO_RUNLOCK(&V_tcbinfo);
546 INP_INFO_WLOCK(&V_tcbinfo);
548 if (in_pcbrele_wlocked(inp)) {
549 INP_INFO_WUNLOCK(&V_tcbinfo);
553 if (inp->inp_flags & INP_DROPPED) {
555 INP_INFO_WUNLOCK(&V_tcbinfo);
560 tp = tcp_drop(tp, tp->t_softerror ?
561 tp->t_softerror : ETIMEDOUT);
565 INP_INFO_RUNLOCK(&V_tcbinfo);
567 if (tp->t_state == TCPS_SYN_SENT) {
569 * If the SYN was retransmitted, indicate CWND to be
570 * limited to 1 segment in cc_conn_init().
573 } else if (tp->t_rxtshift == 1) {
575 * first retransmit; record ssthresh and cwnd so they can
576 * be recovered if this turns out to be a "bad" retransmit.
577 * A retransmit is considered "bad" if an ACK for this
578 * segment is received within RTT/2 interval; the assumption
579 * here is that the ACK was already in flight. See
580 * "On Estimating End-to-End Network Path Properties" by
581 * Allman and Paxson for more details.
583 tp->snd_cwnd_prev = tp->snd_cwnd;
584 tp->snd_ssthresh_prev = tp->snd_ssthresh;
585 tp->snd_recover_prev = tp->snd_recover;
586 if (IN_FASTRECOVERY(tp->t_flags))
587 tp->t_flags |= TF_WASFRECOVERY;
589 tp->t_flags &= ~TF_WASFRECOVERY;
590 if (IN_CONGRECOVERY(tp->t_flags))
591 tp->t_flags |= TF_WASCRECOVERY;
593 tp->t_flags &= ~TF_WASCRECOVERY;
594 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
595 tp->t_flags |= TF_PREVVALID;
597 tp->t_flags &= ~TF_PREVVALID;
598 TCPSTAT_INC(tcps_rexmttimeo);
599 if (tp->t_state == TCPS_SYN_SENT)
600 rexmt = TCPTV_RTOBASE * tcp_syn_backoff[tp->t_rxtshift];
602 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
603 TCPT_RANGESET(tp->t_rxtcur, rexmt,
604 tp->t_rttmin, TCPTV_REXMTMAX);
607 * We enter the path for PLMTUD if connection is established or, if
608 * connection is FIN_WAIT_1 status, reason for the last is that if
609 * amount of data we send is very small, we could send it in couple of
610 * packets and process straight to FIN. In that case we won't catch
613 if (V_tcp_pmtud_blackhole_detect && (((tp->t_state == TCPS_ESTABLISHED))
614 || (tp->t_state == TCPS_FIN_WAIT_1))) {
621 * Idea here is that at each stage of mtu probe (usually, 1448
622 * -> 1188 -> 524) should be given 2 chances to recover before
623 * further clamping down. 'tp->t_rxtshift % 2 == 0' should
626 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) ==
627 (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) &&
628 (tp->t_rxtshift >= 2 && tp->t_rxtshift % 2 == 0)) {
630 * Enter Path MTU Black-hole Detection mechanism:
631 * - Disable Path MTU Discovery (IP "DF" bit).
632 * - Reduce MTU to lower value than what we
633 * negotiated with peer.
635 /* Record that we may have found a black hole. */
636 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
638 /* Keep track of previous MSS. */
639 optlen = tp->t_maxopd - tp->t_maxseg;
640 tp->t_pmtud_saved_maxopd = tp->t_maxopd;
643 * Reduce the MSS to blackhole value or to the default
644 * in an attempt to retransmit.
647 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? 1 : 0;
649 tp->t_maxopd > V_tcp_v6pmtud_blackhole_mss) {
650 /* Use the sysctl tuneable blackhole MSS. */
651 tp->t_maxopd = V_tcp_v6pmtud_blackhole_mss;
652 V_tcp_pmtud_blackhole_activated++;
654 /* Use the default MSS. */
655 tp->t_maxopd = V_tcp_v6mssdflt;
657 * Disable Path MTU Discovery when we switch to
660 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
661 V_tcp_pmtud_blackhole_activated_min_mss++;
664 #if defined(INET6) && defined(INET)
668 if (tp->t_maxopd > V_tcp_pmtud_blackhole_mss) {
669 /* Use the sysctl tuneable blackhole MSS. */
670 tp->t_maxopd = V_tcp_pmtud_blackhole_mss;
671 V_tcp_pmtud_blackhole_activated++;
673 /* Use the default MSS. */
674 tp->t_maxopd = V_tcp_mssdflt;
676 * Disable Path MTU Discovery when we switch to
679 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
680 V_tcp_pmtud_blackhole_activated_min_mss++;
683 tp->t_maxseg = tp->t_maxopd - optlen;
685 * Reset the slow-start flight size
686 * as it may depend on the new MSS.
688 if (CC_ALGO(tp)->conn_init != NULL)
689 CC_ALGO(tp)->conn_init(tp->ccv);
692 * If further retransmissions are still unsuccessful
693 * with a lowered MTU, maybe this isn't a blackhole and
694 * we restore the previous MSS and blackhole detection
696 * The limit '6' is determined by giving each probe
697 * stage (1448, 1188, 524) 2 chances to recover.
699 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
700 (tp->t_rxtshift > 6)) {
701 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
702 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
703 optlen = tp->t_maxopd - tp->t_maxseg;
704 tp->t_maxopd = tp->t_pmtud_saved_maxopd;
705 tp->t_maxseg = tp->t_maxopd - optlen;
706 V_tcp_pmtud_blackhole_failed++;
708 * Reset the slow-start flight size as it
709 * may depend on the new MSS.
711 if (CC_ALGO(tp)->conn_init != NULL)
712 CC_ALGO(tp)->conn_init(tp->ccv);
718 * Disable RFC1323 and SACK if we haven't got any response to
719 * our third SYN to work-around some broken terminal servers
720 * (most of which have hopefully been retired) that have bad VJ
721 * header compression code which trashes TCP segments containing
722 * unknown-to-them TCP options.
724 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
725 (tp->t_rxtshift == 3))
726 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
728 * If we backed off this far, our srtt estimate is probably bogus.
729 * Clobber it so we'll take the next rtt measurement as our srtt;
730 * move the current srtt into rttvar to keep the current
731 * retransmit times until then.
733 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
735 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
736 in6_losing(tp->t_inpcb);
738 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
741 tp->snd_nxt = tp->snd_una;
742 tp->snd_recover = tp->snd_max;
744 * Force a segment to be sent.
746 tp->t_flags |= TF_ACKNOW;
748 * If timing a segment in this window, stop the timer.
752 cc_cong_signal(tp, NULL, CC_RTO);
754 (void) tcp_output(tp);
758 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
759 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
765 INP_INFO_WUNLOCK(&V_tcbinfo);
770 tcp_timer_activate(struct tcpcb *tp, uint32_t timer_type, u_int delta)
772 struct callout *t_callout;
773 timeout_t *f_callout;
774 struct inpcb *inp = tp->t_inpcb;
775 int cpu = INP_CPU(inp);
779 if (tp->t_flags & TF_TOE)
783 if (tp->t_timers->tt_flags & TT_STOPPED)
786 switch (timer_type) {
788 t_callout = &tp->t_timers->tt_delack;
789 f_callout = tcp_timer_delack;
790 f_reset = TT_DELACK_RST;
793 t_callout = &tp->t_timers->tt_rexmt;
794 f_callout = tcp_timer_rexmt;
795 f_reset = TT_REXMT_RST;
798 t_callout = &tp->t_timers->tt_persist;
799 f_callout = tcp_timer_persist;
800 f_reset = TT_PERSIST_RST;
803 t_callout = &tp->t_timers->tt_keep;
804 f_callout = tcp_timer_keep;
805 f_reset = TT_KEEP_RST;
808 t_callout = &tp->t_timers->tt_2msl;
809 f_callout = tcp_timer_2msl;
810 f_reset = TT_2MSL_RST;
813 panic("tp %p bad timer_type %#x", tp, timer_type);
816 if ((tp->t_timers->tt_flags & timer_type) &&
817 callout_stop(t_callout) &&
818 (tp->t_timers->tt_flags & f_reset)) {
819 tp->t_timers->tt_flags &= ~(timer_type | f_reset);
822 if ((tp->t_timers->tt_flags & timer_type) == 0) {
823 tp->t_timers->tt_flags |= (timer_type | f_reset);
824 callout_reset_on(t_callout, delta, f_callout, tp, cpu);
826 /* Reset already running callout on the same CPU. */
827 if (!callout_reset(t_callout, delta, f_callout, tp)) {
829 * Callout not cancelled, consider it as not
830 * properly restarted. */
831 tp->t_timers->tt_flags &= ~f_reset;
838 tcp_timer_active(struct tcpcb *tp, uint32_t timer_type)
840 struct callout *t_callout;
842 switch (timer_type) {
844 t_callout = &tp->t_timers->tt_delack;
847 t_callout = &tp->t_timers->tt_rexmt;
850 t_callout = &tp->t_timers->tt_persist;
853 t_callout = &tp->t_timers->tt_keep;
856 t_callout = &tp->t_timers->tt_2msl;
859 panic("tp %p bad timer_type %#x", tp, timer_type);
861 return callout_active(t_callout);
865 tcp_timer_stop(struct tcpcb *tp, uint32_t timer_type)
867 struct callout *t_callout;
868 timeout_t *f_callout;
871 tp->t_timers->tt_flags |= TT_STOPPED;
873 switch (timer_type) {
875 t_callout = &tp->t_timers->tt_delack;
876 f_callout = tcp_timer_delack_discard;
877 f_reset = TT_DELACK_RST;
880 t_callout = &tp->t_timers->tt_rexmt;
881 f_callout = tcp_timer_rexmt_discard;
882 f_reset = TT_REXMT_RST;
885 t_callout = &tp->t_timers->tt_persist;
886 f_callout = tcp_timer_persist_discard;
887 f_reset = TT_PERSIST_RST;
890 t_callout = &tp->t_timers->tt_keep;
891 f_callout = tcp_timer_keep_discard;
892 f_reset = TT_KEEP_RST;
895 t_callout = &tp->t_timers->tt_2msl;
896 f_callout = tcp_timer_2msl_discard;
897 f_reset = TT_2MSL_RST;
900 panic("tp %p bad timer_type %#x", tp, timer_type);
903 if (tp->t_timers->tt_flags & timer_type) {
904 if (callout_stop(t_callout) &&
905 (tp->t_timers->tt_flags & f_reset)) {
906 tp->t_timers->tt_flags &= ~(timer_type | f_reset);
909 * Can't stop the callout, defer tcpcb actual deletion
910 * to the last tcp timer discard callout.
911 * The TT_STOPPED flag will ensure that no tcp timer
912 * callouts can be restarted on our behalf, and
913 * past this point currently running callouts waiting
914 * on inp lock will return right away after the
915 * classical check for callout reset/stop events:
916 * callout_pending() || !callout_active()
918 callout_reset(t_callout, 1, f_callout, tp);
923 #define ticks_to_msecs(t) (1000*(t) / hz)
926 tcp_timer_to_xtimer(struct tcpcb *tp, struct tcp_timer *timer,
927 struct xtcp_timer *xtimer)
931 bzero(xtimer, sizeof(*xtimer));
934 now = getsbinuptime();
935 if (callout_active(&timer->tt_delack))
936 xtimer->tt_delack = (timer->tt_delack.c_time - now) / SBT_1MS;
937 if (callout_active(&timer->tt_rexmt))
938 xtimer->tt_rexmt = (timer->tt_rexmt.c_time - now) / SBT_1MS;
939 if (callout_active(&timer->tt_persist))
940 xtimer->tt_persist = (timer->tt_persist.c_time - now) / SBT_1MS;
941 if (callout_active(&timer->tt_keep))
942 xtimer->tt_keep = (timer->tt_keep.c_time - now) / SBT_1MS;
943 if (callout_active(&timer->tt_2msl))
944 xtimer->tt_2msl = (timer->tt_2msl.c_time - now) / SBT_1MS;
945 xtimer->t_rcvtime = ticks_to_msecs(ticks - tp->t_rcvtime);