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, OID_AUTO, persmin, CTLTYPE_INT|CTLFLAG_RW,
76 &tcp_persmin, 0, sysctl_msec_to_ticks, "I", "minimum persistence interval");
79 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, persmax, CTLTYPE_INT|CTLFLAG_RW,
80 &tcp_persmax, 0, sysctl_msec_to_ticks, "I", "maximum persistence interval");
83 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
84 &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "time to establish connection");
87 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
88 &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "time before keepalive probes begin");
91 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
92 &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "time between keepalive probes");
95 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime, CTLTYPE_INT|CTLFLAG_RW,
96 &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
97 "Time before a delayed ACK is sent");
100 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
101 &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
104 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
105 &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I",
106 "Minimum Retransmission Timeout");
109 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
110 &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I",
111 "Retransmission Timer Slop");
113 static int always_keepalive = 1;
114 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
115 &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
117 int tcp_fast_finwait2_recycle = 0;
118 SYSCTL_INT(_net_inet_tcp, OID_AUTO, fast_finwait2_recycle, CTLFLAG_RW,
119 &tcp_fast_finwait2_recycle, 0,
120 "Recycle closed FIN_WAIT_2 connections faster");
122 int tcp_finwait2_timeout;
123 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, finwait2_timeout, CTLTYPE_INT|CTLFLAG_RW,
124 &tcp_finwait2_timeout, 0, sysctl_msec_to_ticks, "I", "FIN-WAIT2 timeout");
126 int tcp_keepcnt = TCPTV_KEEPCNT;
127 SYSCTL_INT(_net_inet_tcp, OID_AUTO, keepcnt, CTLFLAG_RW, &tcp_keepcnt, 0,
128 "Number of keepalive probes to send");
130 /* max idle probes */
131 int tcp_maxpersistidle;
133 static int tcp_rexmit_drop_options = 0;
134 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rexmit_drop_options, CTLFLAG_RW,
135 &tcp_rexmit_drop_options, 0,
136 "Drop TCP options from 3rd and later retransmitted SYN");
138 static VNET_DEFINE(int, tcp_pmtud_blackhole_detect);
139 #define V_tcp_pmtud_blackhole_detect VNET(tcp_pmtud_blackhole_detect)
140 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_detection,
142 &VNET_NAME(tcp_pmtud_blackhole_detect), 0,
143 "Path MTU Discovery Black Hole Detection Enabled");
145 static VNET_DEFINE(int, tcp_pmtud_blackhole_activated);
146 #define V_tcp_pmtud_blackhole_activated \
147 VNET(tcp_pmtud_blackhole_activated)
148 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_activated,
150 &VNET_NAME(tcp_pmtud_blackhole_activated), 0,
151 "Path MTU Discovery Black Hole Detection, Activation Count");
153 static VNET_DEFINE(int, tcp_pmtud_blackhole_activated_min_mss);
154 #define V_tcp_pmtud_blackhole_activated_min_mss \
155 VNET(tcp_pmtud_blackhole_activated_min_mss)
156 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_activated_min_mss,
158 &VNET_NAME(tcp_pmtud_blackhole_activated_min_mss), 0,
159 "Path MTU Discovery Black Hole Detection, Activation Count at min MSS");
161 static VNET_DEFINE(int, tcp_pmtud_blackhole_failed);
162 #define V_tcp_pmtud_blackhole_failed VNET(tcp_pmtud_blackhole_failed)
163 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_failed,
165 &VNET_NAME(tcp_pmtud_blackhole_failed), 0,
166 "Path MTU Discovery Black Hole Detection, Failure Count");
169 static VNET_DEFINE(int, tcp_pmtud_blackhole_mss) = 1200;
170 #define V_tcp_pmtud_blackhole_mss VNET(tcp_pmtud_blackhole_mss)
171 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_mss,
173 &VNET_NAME(tcp_pmtud_blackhole_mss), 0,
174 "Path MTU Discovery Black Hole Detection lowered MSS");
178 static VNET_DEFINE(int, tcp_v6pmtud_blackhole_mss) = 1220;
179 #define V_tcp_v6pmtud_blackhole_mss VNET(tcp_v6pmtud_blackhole_mss)
180 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, v6pmtud_blackhole_mss,
182 &VNET_NAME(tcp_v6pmtud_blackhole_mss), 0,
183 "Path MTU Discovery IPv6 Black Hole Detection lowered MSS");
186 static int per_cpu_timers = 0;
187 SYSCTL_INT(_net_inet_tcp, OID_AUTO, per_cpu_timers, CTLFLAG_RW,
188 &per_cpu_timers , 0, "run tcp timers on all cpus");
190 #define INP_CPU(inp) (per_cpu_timers ? (!CPU_ABSENT(((inp)->inp_flowid % (mp_maxid+1))) ? \
191 ((inp)->inp_flowid % (mp_maxid+1)) : curcpu) : 0)
194 * Tcp protocol timeout routine called every 500 ms.
195 * Updates timestamps used for TCP
196 * causes finite state machine actions if timers expire.
201 VNET_ITERATOR_DECL(vnet_iter);
203 VNET_LIST_RLOCK_NOSLEEP();
204 VNET_FOREACH(vnet_iter) {
205 CURVNET_SET(vnet_iter);
206 (void) tcp_tw_2msl_scan(0);
209 VNET_LIST_RUNLOCK_NOSLEEP();
212 int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
213 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
215 int tcp_backoff[TCP_MAXRXTSHIFT + 1] =
216 { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
218 static int tcp_totbackoff = 2559; /* sum of tcp_backoff[] */
221 * TCP timer processing.
225 tcp_timer_delack(void *xtp)
227 struct tcpcb *tp = xtp;
229 CURVNET_SET(tp->t_vnet);
232 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
234 if (callout_pending(&tp->t_timers->tt_delack) ||
235 !callout_active(&tp->t_timers->tt_delack)) {
240 callout_deactivate(&tp->t_timers->tt_delack);
241 if ((inp->inp_flags & INP_DROPPED) != 0) {
246 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
247 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
248 KASSERT((tp->t_timers->tt_flags & TT_DELACK) != 0,
249 ("%s: tp %p delack callout should be running", __func__, tp));
251 tp->t_flags |= TF_ACKNOW;
252 TCPSTAT_INC(tcps_delack);
253 (void) tcp_output(tp);
259 tcp_timer_2msl(void *xtp)
261 struct tcpcb *tp = xtp;
263 CURVNET_SET(tp->t_vnet);
267 ostate = tp->t_state;
269 INP_INFO_WLOCK(&V_tcbinfo);
271 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
273 tcp_free_sackholes(tp);
274 if (callout_pending(&tp->t_timers->tt_2msl) ||
275 !callout_active(&tp->t_timers->tt_2msl)) {
276 INP_WUNLOCK(tp->t_inpcb);
277 INP_INFO_WUNLOCK(&V_tcbinfo);
281 callout_deactivate(&tp->t_timers->tt_2msl);
282 if ((inp->inp_flags & INP_DROPPED) != 0) {
284 INP_INFO_WUNLOCK(&V_tcbinfo);
288 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
289 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
290 KASSERT((tp->t_timers->tt_flags & TT_2MSL) != 0,
291 ("%s: tp %p 2msl callout should be running", __func__, tp));
293 * 2 MSL timeout in shutdown went off. If we're closed but
294 * still waiting for peer to close and connection has been idle
295 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
296 * control block. Otherwise, check again in a bit.
298 * If fastrecycle of FIN_WAIT_2, in FIN_WAIT_2 and receiver has closed,
299 * there's no point in hanging onto FIN_WAIT_2 socket. Just close it.
300 * Ignore fact that there were recent incoming segments.
302 if (tcp_fast_finwait2_recycle && tp->t_state == TCPS_FIN_WAIT_2 &&
303 tp->t_inpcb && tp->t_inpcb->inp_socket &&
304 (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE)) {
305 TCPSTAT_INC(tcps_finwait2_drops);
308 if (tp->t_state != TCPS_TIME_WAIT &&
309 ticks - tp->t_rcvtime <= TP_MAXIDLE(tp)) {
310 if (!callout_reset(&tp->t_timers->tt_2msl,
311 TP_KEEPINTVL(tp), tcp_timer_2msl, tp)) {
312 tp->t_timers->tt_flags &= ~TT_2MSL_RST;
319 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
320 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
325 INP_INFO_WUNLOCK(&V_tcbinfo);
330 tcp_timer_keep(void *xtp)
332 struct tcpcb *tp = xtp;
333 struct tcptemp *t_template;
335 CURVNET_SET(tp->t_vnet);
339 ostate = tp->t_state;
341 INP_INFO_WLOCK(&V_tcbinfo);
343 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
345 if (callout_pending(&tp->t_timers->tt_keep) ||
346 !callout_active(&tp->t_timers->tt_keep)) {
348 INP_INFO_WUNLOCK(&V_tcbinfo);
352 callout_deactivate(&tp->t_timers->tt_keep);
353 if ((inp->inp_flags & INP_DROPPED) != 0) {
355 INP_INFO_WUNLOCK(&V_tcbinfo);
359 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
360 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
361 KASSERT((tp->t_timers->tt_flags & TT_KEEP) != 0,
362 ("%s: tp %p keep callout should be running", __func__, tp));
364 * Keep-alive timer went off; send something
365 * or drop connection if idle for too long.
367 TCPSTAT_INC(tcps_keeptimeo);
368 if (tp->t_state < TCPS_ESTABLISHED)
370 if ((always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
371 tp->t_state <= TCPS_CLOSING) {
372 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
375 * Send a packet designed to force a response
376 * if the peer is up and reachable:
377 * either an ACK if the connection is still alive,
378 * or an RST if the peer has closed the connection
379 * due to timeout or reboot.
380 * Using sequence number tp->snd_una-1
381 * causes the transmitted zero-length segment
382 * to lie outside the receive window;
383 * by the protocol spec, this requires the
384 * correspondent TCP to respond.
386 TCPSTAT_INC(tcps_keepprobe);
387 t_template = tcpip_maketemplate(inp);
389 tcp_respond(tp, t_template->tt_ipgen,
390 &t_template->tt_t, (struct mbuf *)NULL,
391 tp->rcv_nxt, tp->snd_una - 1, 0);
392 free(t_template, M_TEMP);
394 if (!callout_reset(&tp->t_timers->tt_keep, TP_KEEPINTVL(tp),
395 tcp_timer_keep, tp)) {
396 tp->t_timers->tt_flags &= ~TT_KEEP_RST;
398 } else if (!callout_reset(&tp->t_timers->tt_keep, TP_KEEPIDLE(tp),
399 tcp_timer_keep, tp)) {
400 tp->t_timers->tt_flags &= ~TT_KEEP_RST;
404 if (inp->inp_socket->so_options & SO_DEBUG)
405 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
409 INP_INFO_WUNLOCK(&V_tcbinfo);
414 TCPSTAT_INC(tcps_keepdrops);
415 tp = tcp_drop(tp, ETIMEDOUT);
418 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
419 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
423 INP_WUNLOCK(tp->t_inpcb);
424 INP_INFO_WUNLOCK(&V_tcbinfo);
429 tcp_timer_persist(void *xtp)
431 struct tcpcb *tp = xtp;
433 CURVNET_SET(tp->t_vnet);
437 ostate = tp->t_state;
439 INP_INFO_WLOCK(&V_tcbinfo);
441 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
443 if (callout_pending(&tp->t_timers->tt_persist) ||
444 !callout_active(&tp->t_timers->tt_persist)) {
446 INP_INFO_WUNLOCK(&V_tcbinfo);
450 callout_deactivate(&tp->t_timers->tt_persist);
451 if ((inp->inp_flags & INP_DROPPED) != 0) {
453 INP_INFO_WUNLOCK(&V_tcbinfo);
457 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
458 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
459 KASSERT((tp->t_timers->tt_flags & TT_PERSIST) != 0,
460 ("%s: tp %p persist callout should be running", __func__, tp));
462 * Persistance timer into zero window.
463 * Force a byte to be output, if possible.
465 TCPSTAT_INC(tcps_persisttimeo);
467 * Hack: if the peer is dead/unreachable, we do not
468 * time out if the window is closed. After a full
469 * backoff, drop the connection if the idle time
470 * (no responses to probes) reaches the maximum
471 * backoff that we would use if retransmitting.
473 if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
474 (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
475 ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
476 TCPSTAT_INC(tcps_persistdrop);
477 tp = tcp_drop(tp, ETIMEDOUT);
481 * If the user has closed the socket then drop a persisting
482 * connection after a much reduced timeout.
484 if (tp->t_state > TCPS_CLOSE_WAIT &&
485 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
486 TCPSTAT_INC(tcps_persistdrop);
487 tp = tcp_drop(tp, ETIMEDOUT);
491 tp->t_flags |= TF_FORCEDATA;
492 (void) tcp_output(tp);
493 tp->t_flags &= ~TF_FORCEDATA;
497 if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
498 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
502 INP_INFO_WUNLOCK(&V_tcbinfo);
507 tcp_timer_rexmt(void * xtp)
509 struct tcpcb *tp = xtp;
510 CURVNET_SET(tp->t_vnet);
517 ostate = tp->t_state;
520 INP_INFO_RLOCK(&V_tcbinfo);
522 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
524 if (callout_pending(&tp->t_timers->tt_rexmt) ||
525 !callout_active(&tp->t_timers->tt_rexmt)) {
527 INP_INFO_RUNLOCK(&V_tcbinfo);
531 callout_deactivate(&tp->t_timers->tt_rexmt);
532 if ((inp->inp_flags & INP_DROPPED) != 0) {
534 INP_INFO_RUNLOCK(&V_tcbinfo);
538 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
539 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
540 KASSERT((tp->t_timers->tt_flags & TT_REXMT) != 0,
541 ("%s: tp %p rexmt callout should be running", __func__, tp));
542 tcp_free_sackholes(tp);
544 * Retransmission timer went off. Message has not
545 * been acked within retransmit interval. Back off
546 * to a longer retransmit interval and retransmit one segment.
548 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
549 tp->t_rxtshift = TCP_MAXRXTSHIFT;
550 TCPSTAT_INC(tcps_timeoutdrop);
552 INP_INFO_RUNLOCK(&V_tcbinfo);
554 INP_INFO_WLOCK(&V_tcbinfo);
556 if (in_pcbrele_wlocked(inp)) {
557 INP_INFO_WUNLOCK(&V_tcbinfo);
561 if (inp->inp_flags & INP_DROPPED) {
563 INP_INFO_WUNLOCK(&V_tcbinfo);
568 tp = tcp_drop(tp, tp->t_softerror ?
569 tp->t_softerror : ETIMEDOUT);
573 INP_INFO_RUNLOCK(&V_tcbinfo);
575 if (tp->t_state == TCPS_SYN_SENT) {
577 * If the SYN was retransmitted, indicate CWND to be
578 * limited to 1 segment in cc_conn_init().
581 } else if (tp->t_rxtshift == 1) {
583 * first retransmit; record ssthresh and cwnd so they can
584 * be recovered if this turns out to be a "bad" retransmit.
585 * A retransmit is considered "bad" if an ACK for this
586 * segment is received within RTT/2 interval; the assumption
587 * here is that the ACK was already in flight. See
588 * "On Estimating End-to-End Network Path Properties" by
589 * Allman and Paxson for more details.
591 tp->snd_cwnd_prev = tp->snd_cwnd;
592 tp->snd_ssthresh_prev = tp->snd_ssthresh;
593 tp->snd_recover_prev = tp->snd_recover;
594 if (IN_FASTRECOVERY(tp->t_flags))
595 tp->t_flags |= TF_WASFRECOVERY;
597 tp->t_flags &= ~TF_WASFRECOVERY;
598 if (IN_CONGRECOVERY(tp->t_flags))
599 tp->t_flags |= TF_WASCRECOVERY;
601 tp->t_flags &= ~TF_WASCRECOVERY;
602 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
603 tp->t_flags |= TF_PREVVALID;
605 tp->t_flags &= ~TF_PREVVALID;
606 TCPSTAT_INC(tcps_rexmttimeo);
607 if ((tp->t_state == TCPS_SYN_SENT) ||
608 (tp->t_state == TCPS_SYN_RECEIVED))
609 rexmt = TCPTV_RTOBASE * tcp_syn_backoff[tp->t_rxtshift];
611 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
612 TCPT_RANGESET(tp->t_rxtcur, rexmt,
613 tp->t_rttmin, TCPTV_REXMTMAX);
616 * We enter the path for PLMTUD if connection is established or, if
617 * connection is FIN_WAIT_1 status, reason for the last is that if
618 * amount of data we send is very small, we could send it in couple of
619 * packets and process straight to FIN. In that case we won't catch
622 if (V_tcp_pmtud_blackhole_detect && (((tp->t_state == TCPS_ESTABLISHED))
623 || (tp->t_state == TCPS_FIN_WAIT_1))) {
630 * Idea here is that at each stage of mtu probe (usually, 1448
631 * -> 1188 -> 524) should be given 2 chances to recover before
632 * further clamping down. 'tp->t_rxtshift % 2 == 0' should
635 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) ==
636 (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) &&
637 (tp->t_rxtshift >= 2 && tp->t_rxtshift % 2 == 0)) {
639 * Enter Path MTU Black-hole Detection mechanism:
640 * - Disable Path MTU Discovery (IP "DF" bit).
641 * - Reduce MTU to lower value than what we
642 * negotiated with peer.
644 /* Record that we may have found a black hole. */
645 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
647 /* Keep track of previous MSS. */
648 optlen = tp->t_maxopd - tp->t_maxseg;
649 tp->t_pmtud_saved_maxopd = tp->t_maxopd;
652 * Reduce the MSS to blackhole value or to the default
653 * in an attempt to retransmit.
656 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? 1 : 0;
658 tp->t_maxopd > V_tcp_v6pmtud_blackhole_mss) {
659 /* Use the sysctl tuneable blackhole MSS. */
660 tp->t_maxopd = V_tcp_v6pmtud_blackhole_mss;
661 V_tcp_pmtud_blackhole_activated++;
663 /* Use the default MSS. */
664 tp->t_maxopd = V_tcp_v6mssdflt;
666 * Disable Path MTU Discovery when we switch to
669 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
670 V_tcp_pmtud_blackhole_activated_min_mss++;
673 #if defined(INET6) && defined(INET)
677 if (tp->t_maxopd > V_tcp_pmtud_blackhole_mss) {
678 /* Use the sysctl tuneable blackhole MSS. */
679 tp->t_maxopd = V_tcp_pmtud_blackhole_mss;
680 V_tcp_pmtud_blackhole_activated++;
682 /* Use the default MSS. */
683 tp->t_maxopd = V_tcp_mssdflt;
685 * Disable Path MTU Discovery when we switch to
688 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
689 V_tcp_pmtud_blackhole_activated_min_mss++;
692 tp->t_maxseg = tp->t_maxopd - optlen;
694 * Reset the slow-start flight size
695 * as it may depend on the new MSS.
697 if (CC_ALGO(tp)->conn_init != NULL)
698 CC_ALGO(tp)->conn_init(tp->ccv);
701 * If further retransmissions are still unsuccessful
702 * with a lowered MTU, maybe this isn't a blackhole and
703 * we restore the previous MSS and blackhole detection
705 * The limit '6' is determined by giving each probe
706 * stage (1448, 1188, 524) 2 chances to recover.
708 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
709 (tp->t_rxtshift > 6)) {
710 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
711 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
712 optlen = tp->t_maxopd - tp->t_maxseg;
713 tp->t_maxopd = tp->t_pmtud_saved_maxopd;
714 tp->t_maxseg = tp->t_maxopd - optlen;
715 V_tcp_pmtud_blackhole_failed++;
717 * Reset the slow-start flight size as it
718 * may depend on the new MSS.
720 if (CC_ALGO(tp)->conn_init != NULL)
721 CC_ALGO(tp)->conn_init(tp->ccv);
727 * Disable RFC1323 and SACK if we haven't got any response to
728 * our third SYN to work-around some broken terminal servers
729 * (most of which have hopefully been retired) that have bad VJ
730 * header compression code which trashes TCP segments containing
731 * unknown-to-them TCP options.
733 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
734 (tp->t_rxtshift == 3))
735 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
737 * If we backed off this far, our srtt estimate is probably bogus.
738 * Clobber it so we'll take the next rtt measurement as our srtt;
739 * move the current srtt into rttvar to keep the current
740 * retransmit times until then.
742 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
744 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
745 in6_losing(tp->t_inpcb);
747 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
750 tp->snd_nxt = tp->snd_una;
751 tp->snd_recover = tp->snd_max;
753 * Force a segment to be sent.
755 tp->t_flags |= TF_ACKNOW;
757 * If timing a segment in this window, stop the timer.
761 cc_cong_signal(tp, NULL, CC_RTO);
763 (void) tcp_output(tp);
767 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
768 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
774 INP_INFO_WUNLOCK(&V_tcbinfo);
779 tcp_timer_activate(struct tcpcb *tp, uint32_t timer_type, u_int delta)
781 struct callout *t_callout;
782 timeout_t *f_callout;
783 struct inpcb *inp = tp->t_inpcb;
784 int cpu = INP_CPU(inp);
788 if (tp->t_flags & TF_TOE)
792 if (tp->t_timers->tt_flags & TT_STOPPED)
795 switch (timer_type) {
797 t_callout = &tp->t_timers->tt_delack;
798 f_callout = tcp_timer_delack;
799 f_reset = TT_DELACK_RST;
802 t_callout = &tp->t_timers->tt_rexmt;
803 f_callout = tcp_timer_rexmt;
804 f_reset = TT_REXMT_RST;
807 t_callout = &tp->t_timers->tt_persist;
808 f_callout = tcp_timer_persist;
809 f_reset = TT_PERSIST_RST;
812 t_callout = &tp->t_timers->tt_keep;
813 f_callout = tcp_timer_keep;
814 f_reset = TT_KEEP_RST;
817 t_callout = &tp->t_timers->tt_2msl;
818 f_callout = tcp_timer_2msl;
819 f_reset = TT_2MSL_RST;
822 panic("tp %p bad timer_type %#x", tp, timer_type);
825 if ((tp->t_timers->tt_flags & timer_type) &&
826 callout_stop(t_callout) &&
827 (tp->t_timers->tt_flags & f_reset)) {
828 tp->t_timers->tt_flags &= ~(timer_type | f_reset);
831 if ((tp->t_timers->tt_flags & timer_type) == 0) {
832 tp->t_timers->tt_flags |= (timer_type | f_reset);
833 callout_reset_on(t_callout, delta, f_callout, tp, cpu);
835 /* Reset already running callout on the same CPU. */
836 if (!callout_reset(t_callout, delta, f_callout, tp)) {
838 * Callout not cancelled, consider it as not
839 * properly restarted. */
840 tp->t_timers->tt_flags &= ~f_reset;
847 tcp_timer_active(struct tcpcb *tp, uint32_t timer_type)
849 struct callout *t_callout;
851 switch (timer_type) {
853 t_callout = &tp->t_timers->tt_delack;
856 t_callout = &tp->t_timers->tt_rexmt;
859 t_callout = &tp->t_timers->tt_persist;
862 t_callout = &tp->t_timers->tt_keep;
865 t_callout = &tp->t_timers->tt_2msl;
868 panic("tp %p bad timer_type %#x", tp, timer_type);
870 return callout_active(t_callout);
874 tcp_timer_stop(struct tcpcb *tp, uint32_t timer_type)
876 struct callout *t_callout;
877 timeout_t *f_callout;
880 tp->t_timers->tt_flags |= TT_STOPPED;
882 switch (timer_type) {
884 t_callout = &tp->t_timers->tt_delack;
885 f_callout = tcp_timer_delack_discard;
886 f_reset = TT_DELACK_RST;
889 t_callout = &tp->t_timers->tt_rexmt;
890 f_callout = tcp_timer_rexmt_discard;
891 f_reset = TT_REXMT_RST;
894 t_callout = &tp->t_timers->tt_persist;
895 f_callout = tcp_timer_persist_discard;
896 f_reset = TT_PERSIST_RST;
899 t_callout = &tp->t_timers->tt_keep;
900 f_callout = tcp_timer_keep_discard;
901 f_reset = TT_KEEP_RST;
904 t_callout = &tp->t_timers->tt_2msl;
905 f_callout = tcp_timer_2msl_discard;
906 f_reset = TT_2MSL_RST;
909 panic("tp %p bad timer_type %#x", tp, timer_type);
912 if (tp->t_timers->tt_flags & timer_type) {
913 if (callout_stop(t_callout) &&
914 (tp->t_timers->tt_flags & f_reset)) {
915 tp->t_timers->tt_flags &= ~(timer_type | f_reset);
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()
927 callout_reset(t_callout, 1, f_callout, tp);
932 #define ticks_to_msecs(t) (1000*(t) / hz)
935 tcp_timer_to_xtimer(struct tcpcb *tp, struct tcp_timer *timer,
936 struct xtcp_timer *xtimer)
940 bzero(xtimer, sizeof(*xtimer));
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);