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 * 3. 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"
40 #include <sys/param.h>
41 #include <sys/kernel.h>
44 #include <sys/mutex.h>
45 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
53 #include <net/route.h>
54 #include <net/rss_config.h>
56 #include <net/netisr.h>
58 #include <netinet/in.h>
59 #include <netinet/in_kdtrace.h>
60 #include <netinet/in_pcb.h>
61 #include <netinet/in_rss.h>
62 #include <netinet/in_systm.h>
64 #include <netinet6/in6_pcb.h>
66 #include <netinet/ip_var.h>
67 #include <netinet/tcp.h>
68 #include <netinet/tcp_fsm.h>
69 #include <netinet/tcp_timer.h>
70 #include <netinet/tcp_var.h>
71 #include <netinet/cc/cc.h>
73 #include <netinet6/tcp6_var.h>
75 #include <netinet/tcpip.h>
77 #include <netinet/tcp_debug.h>
81 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, persmin, CTLTYPE_INT|CTLFLAG_RW,
82 &tcp_persmin, 0, sysctl_msec_to_ticks, "I", "minimum persistence interval");
85 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, persmax, CTLTYPE_INT|CTLFLAG_RW,
86 &tcp_persmax, 0, sysctl_msec_to_ticks, "I", "maximum persistence interval");
89 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
90 &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "time to establish connection");
93 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
94 &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "time before keepalive probes begin");
97 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
98 &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "time between keepalive probes");
101 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime, CTLTYPE_INT|CTLFLAG_RW,
102 &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
103 "Time before a delayed ACK is sent");
106 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
107 &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
110 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
111 &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I",
112 "Minimum Retransmission Timeout");
115 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
116 &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I",
117 "Retransmission Timer Slop");
119 static int always_keepalive = 1;
120 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
121 &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
123 int tcp_fast_finwait2_recycle = 0;
124 SYSCTL_INT(_net_inet_tcp, OID_AUTO, fast_finwait2_recycle, CTLFLAG_RW,
125 &tcp_fast_finwait2_recycle, 0,
126 "Recycle closed FIN_WAIT_2 connections faster");
128 int tcp_finwait2_timeout;
129 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, finwait2_timeout, CTLTYPE_INT|CTLFLAG_RW,
130 &tcp_finwait2_timeout, 0, sysctl_msec_to_ticks, "I", "FIN-WAIT2 timeout");
132 int tcp_keepcnt = TCPTV_KEEPCNT;
133 SYSCTL_INT(_net_inet_tcp, OID_AUTO, keepcnt, CTLFLAG_RW, &tcp_keepcnt, 0,
134 "Number of keepalive probes to send");
136 /* max idle probes */
137 int tcp_maxpersistidle;
139 static int tcp_rexmit_drop_options = 0;
140 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rexmit_drop_options, CTLFLAG_RW,
141 &tcp_rexmit_drop_options, 0,
142 "Drop TCP options from 3rd and later retransmitted SYN");
144 VNET_DEFINE(int, tcp_pmtud_blackhole_detect);
145 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_detection,
146 CTLFLAG_RW|CTLFLAG_VNET,
147 &VNET_NAME(tcp_pmtud_blackhole_detect), 0,
148 "Path MTU Discovery Black Hole Detection Enabled");
151 VNET_DEFINE(int, tcp_pmtud_blackhole_mss) = 1200;
152 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_mss,
153 CTLFLAG_RW|CTLFLAG_VNET,
154 &VNET_NAME(tcp_pmtud_blackhole_mss), 0,
155 "Path MTU Discovery Black Hole Detection lowered MSS");
159 VNET_DEFINE(int, tcp_v6pmtud_blackhole_mss) = 1220;
160 SYSCTL_INT(_net_inet_tcp, OID_AUTO, v6pmtud_blackhole_mss,
161 CTLFLAG_RW|CTLFLAG_VNET,
162 &VNET_NAME(tcp_v6pmtud_blackhole_mss), 0,
163 "Path MTU Discovery IPv6 Black Hole Detection lowered MSS");
167 static int per_cpu_timers = 1;
169 static int per_cpu_timers = 0;
171 SYSCTL_INT(_net_inet_tcp, OID_AUTO, per_cpu_timers, CTLFLAG_RW,
172 &per_cpu_timers , 0, "run tcp timers on all cpus");
175 #define INP_CPU(inp) (per_cpu_timers ? (!CPU_ABSENT(((inp)->inp_flowid % (mp_maxid+1))) ? \
176 ((inp)->inp_flowid % (mp_maxid+1)) : curcpu) : 0)
180 * Map the given inp to a CPU id.
182 * This queries RSS if it's compiled in, else it defaults to the current
186 inp_to_cpuid(struct inpcb *inp)
191 if (per_cpu_timers) {
192 cpuid = rss_hash2cpuid(inp->inp_flowid, inp->inp_flowtype);
193 if (cpuid == NETISR_CPUID_NONE)
194 return (curcpu); /* XXX */
199 /* Legacy, pre-RSS behaviour */
200 if (per_cpu_timers) {
202 * We don't have a flowid -> cpuid mapping, so cheat and
203 * just map unknown cpuids to curcpu. Not the best, but
204 * apparently better than defaulting to swi 0.
206 cpuid = inp->inp_flowid % (mp_maxid + 1);
207 if (! CPU_ABSENT(cpuid))
212 /* Default for RSS and non-RSS - cpuid 0 */
219 * Tcp protocol timeout routine called every 500 ms.
220 * Updates timestamps used for TCP
221 * causes finite state machine actions if timers expire.
226 VNET_ITERATOR_DECL(vnet_iter);
228 VNET_LIST_RLOCK_NOSLEEP();
229 VNET_FOREACH(vnet_iter) {
230 CURVNET_SET(vnet_iter);
231 (void) tcp_tw_2msl_scan(0);
234 VNET_LIST_RUNLOCK_NOSLEEP();
237 int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
238 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
240 int tcp_backoff[TCP_MAXRXTSHIFT + 1] =
241 { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
243 static int tcp_totbackoff = 2559; /* sum of tcp_backoff[] */
246 * TCP timer processing.
250 tcp_timer_delack(void *xtp)
252 struct tcpcb *tp = xtp;
254 CURVNET_SET(tp->t_vnet);
257 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
259 if (callout_pending(&tp->t_timers->tt_delack) ||
260 !callout_active(&tp->t_timers->tt_delack)) {
265 callout_deactivate(&tp->t_timers->tt_delack);
266 if ((inp->inp_flags & INP_DROPPED) != 0) {
271 tp->t_flags |= TF_ACKNOW;
272 TCPSTAT_INC(tcps_delack);
273 (void) tp->t_fb->tfb_tcp_output(tp);
279 * When a timer wants to remove a TCB it must
280 * hold the INP_INFO_RLOCK(). The timer function
281 * should only have grabbed the INP_WLOCK() when
282 * it entered. To safely switch to holding both the
283 * INP_INFO_RLOCK() and the INP_WLOCK() we must first
284 * grab a reference on the inp, which will hold the inp
285 * so that it can't be removed. We then unlock the INP_WLOCK(),
286 * and grab the INP_INFO_RLOCK() lock. Once we have the INP_INFO_RLOCK()
287 * we proceed again to get the INP_WLOCK() (this preserves proper
288 * lock order). After acquiring the INP_WLOCK we must check if someone
289 * else deleted the pcb i.e. the inp_flags check.
290 * If so we return 1 otherwise we return 0.
292 * No matter what the tcp_inpinfo_lock_add() function
293 * returns the caller must afterwards call tcp_inpinfo_lock_del()
294 * to drop the locks and reference properly.
298 tcp_inpinfo_lock_add(struct inpcb *inp)
302 INP_INFO_RLOCK(&V_tcbinfo);
304 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
312 tcp_inpinfo_lock_del(struct inpcb *inp, struct tcpcb *tp)
314 INP_INFO_RUNLOCK(&V_tcbinfo);
315 if (inp && (tp == NULL)) {
317 * If tcp_close/drop() gets called and tp
318 * returns NULL, then the function dropped
319 * the inp lock, we hold a reference keeping
320 * this around, so we must re-aquire the
321 * INP_WLOCK() in order to proceed with
322 * our dropping the inp reference.
326 if (inp && in_pcbrele_wlocked(inp) == 0)
331 tcp_timer_2msl(void *xtp)
333 struct tcpcb *tp = xtp;
335 CURVNET_SET(tp->t_vnet);
339 ostate = tp->t_state;
342 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
344 tcp_free_sackholes(tp);
345 if (callout_pending(&tp->t_timers->tt_2msl) ||
346 !callout_active(&tp->t_timers->tt_2msl)) {
347 INP_WUNLOCK(tp->t_inpcb);
351 callout_deactivate(&tp->t_timers->tt_2msl);
352 if ((inp->inp_flags & INP_DROPPED) != 0) {
357 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
358 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
360 * 2 MSL timeout in shutdown went off. If we're closed but
361 * still waiting for peer to close and connection has been idle
362 * too long delete connection control block. Otherwise, check
365 * If in TIME_WAIT state just ignore as this timeout is handled in
366 * tcp_tw_2msl_scan().
368 * If fastrecycle of FIN_WAIT_2, in FIN_WAIT_2 and receiver has closed,
369 * there's no point in hanging onto FIN_WAIT_2 socket. Just close it.
370 * Ignore fact that there were recent incoming segments.
372 if ((inp->inp_flags & INP_TIMEWAIT) != 0) {
377 if (tcp_fast_finwait2_recycle && tp->t_state == TCPS_FIN_WAIT_2 &&
378 tp->t_inpcb && tp->t_inpcb->inp_socket &&
379 (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE)) {
380 TCPSTAT_INC(tcps_finwait2_drops);
381 if (tcp_inpinfo_lock_add(inp)) {
382 tcp_inpinfo_lock_del(inp, tp);
386 tcp_inpinfo_lock_del(inp, tp);
389 if (ticks - tp->t_rcvtime <= TP_MAXIDLE(tp)) {
390 callout_reset(&tp->t_timers->tt_2msl,
391 TP_KEEPINTVL(tp), tcp_timer_2msl, tp);
393 if (tcp_inpinfo_lock_add(inp)) {
394 tcp_inpinfo_lock_del(inp, tp);
398 tcp_inpinfo_lock_del(inp, tp);
404 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
405 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
408 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
417 tcp_timer_keep(void *xtp)
419 struct tcpcb *tp = xtp;
420 struct tcptemp *t_template;
422 CURVNET_SET(tp->t_vnet);
426 ostate = tp->t_state;
429 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
431 if (callout_pending(&tp->t_timers->tt_keep) ||
432 !callout_active(&tp->t_timers->tt_keep)) {
437 callout_deactivate(&tp->t_timers->tt_keep);
438 if ((inp->inp_flags & INP_DROPPED) != 0) {
443 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
444 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
447 * Because we don't regularly reset the keepalive callout in
448 * the ESTABLISHED state, it may be that we don't actually need
449 * to send a keepalive yet. If that occurs, schedule another
450 * call for the next time the keepalive timer might expire.
452 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
455 idletime = ticks - tp->t_rcvtime;
456 if (idletime < TP_KEEPIDLE(tp)) {
457 callout_reset(&tp->t_timers->tt_keep,
458 TP_KEEPIDLE(tp) - idletime, tcp_timer_keep, tp);
466 * Keep-alive timer went off; send something
467 * or drop connection if idle for too long.
469 TCPSTAT_INC(tcps_keeptimeo);
470 if (tp->t_state < TCPS_ESTABLISHED)
472 if ((always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
473 tp->t_state <= TCPS_CLOSING) {
474 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
477 * Send a packet designed to force a response
478 * if the peer is up and reachable:
479 * either an ACK if the connection is still alive,
480 * or an RST if the peer has closed the connection
481 * due to timeout or reboot.
482 * Using sequence number tp->snd_una-1
483 * causes the transmitted zero-length segment
484 * to lie outside the receive window;
485 * by the protocol spec, this requires the
486 * correspondent TCP to respond.
488 TCPSTAT_INC(tcps_keepprobe);
489 t_template = tcpip_maketemplate(inp);
491 tcp_respond(tp, t_template->tt_ipgen,
492 &t_template->tt_t, (struct mbuf *)NULL,
493 tp->rcv_nxt, tp->snd_una - 1, 0);
494 free(t_template, M_TEMP);
496 callout_reset(&tp->t_timers->tt_keep, TP_KEEPINTVL(tp),
499 callout_reset(&tp->t_timers->tt_keep, TP_KEEPIDLE(tp),
503 if (inp->inp_socket->so_options & SO_DEBUG)
504 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
507 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
513 TCPSTAT_INC(tcps_keepdrops);
515 if (tcp_inpinfo_lock_add(inp)) {
516 tcp_inpinfo_lock_del(inp, tp);
519 tp = tcp_drop(tp, ETIMEDOUT);
522 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
523 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
526 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
527 tcp_inpinfo_lock_del(inp, tp);
533 tcp_timer_persist(void *xtp)
535 struct tcpcb *tp = xtp;
537 CURVNET_SET(tp->t_vnet);
541 ostate = tp->t_state;
544 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
546 if (callout_pending(&tp->t_timers->tt_persist) ||
547 !callout_active(&tp->t_timers->tt_persist)) {
552 callout_deactivate(&tp->t_timers->tt_persist);
553 if ((inp->inp_flags & INP_DROPPED) != 0) {
558 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
559 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
561 * Persistence timer into zero window.
562 * Force a byte to be output, if possible.
564 TCPSTAT_INC(tcps_persisttimeo);
566 * Hack: if the peer is dead/unreachable, we do not
567 * time out if the window is closed. After a full
568 * backoff, drop the connection if the idle time
569 * (no responses to probes) reaches the maximum
570 * backoff that we would use if retransmitting.
572 if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
573 (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
574 ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
575 TCPSTAT_INC(tcps_persistdrop);
576 if (tcp_inpinfo_lock_add(inp)) {
577 tcp_inpinfo_lock_del(inp, tp);
580 tp = tcp_drop(tp, ETIMEDOUT);
581 tcp_inpinfo_lock_del(inp, tp);
585 * If the user has closed the socket then drop a persisting
586 * connection after a much reduced timeout.
588 if (tp->t_state > TCPS_CLOSE_WAIT &&
589 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
590 TCPSTAT_INC(tcps_persistdrop);
591 if (tcp_inpinfo_lock_add(inp)) {
592 tcp_inpinfo_lock_del(inp, tp);
595 tp = tcp_drop(tp, ETIMEDOUT);
596 tcp_inpinfo_lock_del(inp, tp);
600 tp->t_flags |= TF_FORCEDATA;
601 (void) tp->t_fb->tfb_tcp_output(tp);
602 tp->t_flags &= ~TF_FORCEDATA;
605 if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
606 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
608 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
615 tcp_timer_rexmt(void * xtp)
617 struct tcpcb *tp = xtp;
618 CURVNET_SET(tp->t_vnet);
624 ostate = tp->t_state;
627 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
629 if (callout_pending(&tp->t_timers->tt_rexmt) ||
630 !callout_active(&tp->t_timers->tt_rexmt)) {
635 callout_deactivate(&tp->t_timers->tt_rexmt);
636 if ((inp->inp_flags & INP_DROPPED) != 0) {
641 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
642 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
643 tcp_free_sackholes(tp);
644 if (tp->t_fb->tfb_tcp_rexmit_tmr) {
645 /* The stack has a timer action too. */
646 (*tp->t_fb->tfb_tcp_rexmit_tmr)(tp);
649 * Retransmission timer went off. Message has not
650 * been acked within retransmit interval. Back off
651 * to a longer retransmit interval and retransmit one segment.
653 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
654 tp->t_rxtshift = TCP_MAXRXTSHIFT;
655 TCPSTAT_INC(tcps_timeoutdrop);
656 if (tcp_inpinfo_lock_add(inp)) {
657 tcp_inpinfo_lock_del(inp, tp);
660 tp = tcp_drop(tp, tp->t_softerror ?
661 tp->t_softerror : ETIMEDOUT);
662 tcp_inpinfo_lock_del(inp, tp);
665 if (tp->t_state == TCPS_SYN_SENT) {
667 * If the SYN was retransmitted, indicate CWND to be
668 * limited to 1 segment in cc_conn_init().
671 } else if (tp->t_rxtshift == 1) {
673 * first retransmit; record ssthresh and cwnd so they can
674 * be recovered if this turns out to be a "bad" retransmit.
675 * A retransmit is considered "bad" if an ACK for this
676 * segment is received within RTT/2 interval; the assumption
677 * here is that the ACK was already in flight. See
678 * "On Estimating End-to-End Network Path Properties" by
679 * Allman and Paxson for more details.
681 tp->snd_cwnd_prev = tp->snd_cwnd;
682 tp->snd_ssthresh_prev = tp->snd_ssthresh;
683 tp->snd_recover_prev = tp->snd_recover;
684 if (IN_FASTRECOVERY(tp->t_flags))
685 tp->t_flags |= TF_WASFRECOVERY;
687 tp->t_flags &= ~TF_WASFRECOVERY;
688 if (IN_CONGRECOVERY(tp->t_flags))
689 tp->t_flags |= TF_WASCRECOVERY;
691 tp->t_flags &= ~TF_WASCRECOVERY;
692 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
693 tp->t_flags |= TF_PREVVALID;
695 tp->t_flags &= ~TF_PREVVALID;
696 TCPSTAT_INC(tcps_rexmttimeo);
697 if ((tp->t_state == TCPS_SYN_SENT) ||
698 (tp->t_state == TCPS_SYN_RECEIVED))
699 rexmt = TCPTV_RTOBASE * tcp_syn_backoff[tp->t_rxtshift];
701 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
702 TCPT_RANGESET(tp->t_rxtcur, rexmt,
703 tp->t_rttmin, TCPTV_REXMTMAX);
706 * We enter the path for PLMTUD if connection is established or, if
707 * connection is FIN_WAIT_1 status, reason for the last is that if
708 * amount of data we send is very small, we could send it in couple of
709 * packets and process straight to FIN. In that case we won't catch
712 if (V_tcp_pmtud_blackhole_detect && (((tp->t_state == TCPS_ESTABLISHED))
713 || (tp->t_state == TCPS_FIN_WAIT_1))) {
719 * Idea here is that at each stage of mtu probe (usually, 1448
720 * -> 1188 -> 524) should be given 2 chances to recover before
721 * further clamping down. 'tp->t_rxtshift % 2 == 0' should
724 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) ==
725 (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) &&
726 (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 &&
727 tp->t_rxtshift % 2 == 0)) {
729 * Enter Path MTU Black-hole Detection mechanism:
730 * - Disable Path MTU Discovery (IP "DF" bit).
731 * - Reduce MTU to lower value than what we
732 * negotiated with peer.
734 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) {
735 /* Record that we may have found a black hole. */
736 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
737 /* Keep track of previous MSS. */
738 tp->t_pmtud_saved_maxseg = tp->t_maxseg;
742 * Reduce the MSS to blackhole value or to the default
743 * in an attempt to retransmit.
746 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? 1 : 0;
748 tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) {
749 /* Use the sysctl tuneable blackhole MSS. */
750 tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
751 TCPSTAT_INC(tcps_pmtud_blackhole_activated);
753 /* Use the default MSS. */
754 tp->t_maxseg = V_tcp_v6mssdflt;
756 * Disable Path MTU Discovery when we switch to
759 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
760 TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
763 #if defined(INET6) && defined(INET)
767 if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) {
768 /* Use the sysctl tuneable blackhole MSS. */
769 tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
770 TCPSTAT_INC(tcps_pmtud_blackhole_activated);
772 /* Use the default MSS. */
773 tp->t_maxseg = V_tcp_mssdflt;
775 * Disable Path MTU Discovery when we switch to
778 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
779 TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
783 * Reset the slow-start flight size
784 * as it may depend on the new MSS.
786 if (CC_ALGO(tp)->conn_init != NULL)
787 CC_ALGO(tp)->conn_init(tp->ccv);
790 * If further retransmissions are still unsuccessful
791 * with a lowered MTU, maybe this isn't a blackhole and
792 * we restore the previous MSS and blackhole detection
794 * The limit '6' is determined by giving each probe
795 * stage (1448, 1188, 524) 2 chances to recover.
797 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
798 (tp->t_rxtshift >= 6)) {
799 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
800 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
801 tp->t_maxseg = tp->t_pmtud_saved_maxseg;
802 TCPSTAT_INC(tcps_pmtud_blackhole_failed);
804 * Reset the slow-start flight size as it
805 * may depend on the new MSS.
807 if (CC_ALGO(tp)->conn_init != NULL)
808 CC_ALGO(tp)->conn_init(tp->ccv);
814 * Disable RFC1323 and SACK if we haven't got any response to
815 * our third SYN to work-around some broken terminal servers
816 * (most of which have hopefully been retired) that have bad VJ
817 * header compression code which trashes TCP segments containing
818 * unknown-to-them TCP options.
820 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
821 (tp->t_rxtshift == 3))
822 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
824 * If we backed off this far, notify the L3 protocol that we're having
825 * connection problems.
827 if (tp->t_rxtshift > TCP_RTT_INVALIDATE) {
829 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
830 in6_losing(tp->t_inpcb);
833 in_losing(tp->t_inpcb);
835 tp->snd_nxt = tp->snd_una;
836 tp->snd_recover = tp->snd_max;
838 * Force a segment to be sent.
840 tp->t_flags |= TF_ACKNOW;
842 * If timing a segment in this window, stop the timer.
846 cc_cong_signal(tp, NULL, CC_RTO);
848 (void) tp->t_fb->tfb_tcp_output(tp);
851 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
852 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
855 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
862 tcp_timer_activate(struct tcpcb *tp, uint32_t timer_type, u_int delta)
864 struct callout *t_callout;
865 timeout_t *f_callout;
866 struct inpcb *inp = tp->t_inpcb;
867 int cpu = inp_to_cpuid(inp);
870 if (tp->t_flags & TF_TOE)
874 if (tp->t_timers->tt_flags & TT_STOPPED)
877 switch (timer_type) {
879 t_callout = &tp->t_timers->tt_delack;
880 f_callout = tcp_timer_delack;
883 t_callout = &tp->t_timers->tt_rexmt;
884 f_callout = tcp_timer_rexmt;
887 t_callout = &tp->t_timers->tt_persist;
888 f_callout = tcp_timer_persist;
891 t_callout = &tp->t_timers->tt_keep;
892 f_callout = tcp_timer_keep;
895 t_callout = &tp->t_timers->tt_2msl;
896 f_callout = tcp_timer_2msl;
899 if (tp->t_fb->tfb_tcp_timer_activate) {
900 tp->t_fb->tfb_tcp_timer_activate(tp, timer_type, delta);
903 panic("tp %p bad timer_type %#x", tp, timer_type);
906 callout_stop(t_callout);
908 callout_reset_on(t_callout, delta, f_callout, tp, cpu);
913 tcp_timer_active(struct tcpcb *tp, uint32_t timer_type)
915 struct callout *t_callout;
917 switch (timer_type) {
919 t_callout = &tp->t_timers->tt_delack;
922 t_callout = &tp->t_timers->tt_rexmt;
925 t_callout = &tp->t_timers->tt_persist;
928 t_callout = &tp->t_timers->tt_keep;
931 t_callout = &tp->t_timers->tt_2msl;
934 if (tp->t_fb->tfb_tcp_timer_active) {
935 return(tp->t_fb->tfb_tcp_timer_active(tp, timer_type));
937 panic("tp %p bad timer_type %#x", tp, timer_type);
939 return callout_active(t_callout);
943 tcp_timer_stop(struct tcpcb *tp, uint32_t timer_type)
945 struct callout *t_callout;
947 tp->t_timers->tt_flags |= TT_STOPPED;
948 switch (timer_type) {
950 t_callout = &tp->t_timers->tt_delack;
953 t_callout = &tp->t_timers->tt_rexmt;
956 t_callout = &tp->t_timers->tt_persist;
959 t_callout = &tp->t_timers->tt_keep;
962 t_callout = &tp->t_timers->tt_2msl;
965 if (tp->t_fb->tfb_tcp_timer_stop) {
967 * XXXrrs we need to look at this with the
968 * stop case below (flags).
970 tp->t_fb->tfb_tcp_timer_stop(tp, timer_type);
973 panic("tp %p bad timer_type %#x", tp, timer_type);
976 if (callout_async_drain(t_callout, tcp_timer_discard) == 0) {
978 * Can't stop the callout, defer tcpcb actual deletion
979 * to the last one. We do this using the async drain
980 * function and incrementing the count in
982 tp->t_timers->tt_draincnt++;