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"
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/cc.h>
59 #include <netinet/in.h>
60 #include <netinet/in_kdtrace.h>
61 #include <netinet/in_pcb.h>
62 #include <netinet/in_rss.h>
63 #include <netinet/in_systm.h>
65 #include <netinet6/in6_pcb.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/tcp_fsm.h>
69 #include <netinet/tcp_timer.h>
70 #include <netinet/tcp_var.h>
72 #include <netinet6/tcp6_var.h>
74 #include <netinet/tcpip.h>
76 #include <netinet/tcp_debug.h>
80 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
81 &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "time to establish connection");
84 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
85 &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "time before keepalive probes begin");
88 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
89 &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "time between keepalive probes");
92 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime, CTLTYPE_INT|CTLFLAG_RW,
93 &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
94 "Time before a delayed ACK is sent");
97 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
98 &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
101 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
102 &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I",
103 "Minimum Retransmission Timeout");
106 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
107 &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I",
108 "Retransmission Timer Slop");
110 static int always_keepalive = 1;
111 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
112 &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
114 int tcp_fast_finwait2_recycle = 0;
115 SYSCTL_INT(_net_inet_tcp, OID_AUTO, fast_finwait2_recycle, CTLFLAG_RW,
116 &tcp_fast_finwait2_recycle, 0,
117 "Recycle closed FIN_WAIT_2 connections faster");
119 int tcp_finwait2_timeout;
120 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, finwait2_timeout, CTLTYPE_INT|CTLFLAG_RW,
121 &tcp_finwait2_timeout, 0, sysctl_msec_to_ticks, "I", "FIN-WAIT2 timeout");
123 int tcp_keepcnt = TCPTV_KEEPCNT;
124 SYSCTL_INT(_net_inet_tcp, OID_AUTO, keepcnt, CTLFLAG_RW, &tcp_keepcnt, 0,
125 "Number of keepalive probes to send");
127 /* max idle probes */
128 int tcp_maxpersistidle;
130 static int tcp_rexmit_drop_options = 0;
131 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rexmit_drop_options, CTLFLAG_RW,
132 &tcp_rexmit_drop_options, 0,
133 "Drop TCP options from 3rd and later retransmitted SYN");
135 static VNET_DEFINE(int, tcp_pmtud_blackhole_detect);
136 #define V_tcp_pmtud_blackhole_detect VNET(tcp_pmtud_blackhole_detect)
137 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_detection,
138 CTLFLAG_RW|CTLFLAG_VNET,
139 &VNET_NAME(tcp_pmtud_blackhole_detect), 0,
140 "Path MTU Discovery Black Hole Detection Enabled");
142 static VNET_DEFINE(int, tcp_pmtud_blackhole_activated);
143 #define V_tcp_pmtud_blackhole_activated \
144 VNET(tcp_pmtud_blackhole_activated)
145 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_activated,
146 CTLFLAG_RD|CTLFLAG_VNET,
147 &VNET_NAME(tcp_pmtud_blackhole_activated), 0,
148 "Path MTU Discovery Black Hole Detection, Activation Count");
150 static VNET_DEFINE(int, tcp_pmtud_blackhole_activated_min_mss);
151 #define V_tcp_pmtud_blackhole_activated_min_mss \
152 VNET(tcp_pmtud_blackhole_activated_min_mss)
153 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_activated_min_mss,
154 CTLFLAG_RD|CTLFLAG_VNET,
155 &VNET_NAME(tcp_pmtud_blackhole_activated_min_mss), 0,
156 "Path MTU Discovery Black Hole Detection, Activation Count at min MSS");
158 static VNET_DEFINE(int, tcp_pmtud_blackhole_failed);
159 #define V_tcp_pmtud_blackhole_failed VNET(tcp_pmtud_blackhole_failed)
160 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_failed,
161 CTLFLAG_RD|CTLFLAG_VNET,
162 &VNET_NAME(tcp_pmtud_blackhole_failed), 0,
163 "Path MTU Discovery Black Hole Detection, Failure Count");
166 static VNET_DEFINE(int, tcp_pmtud_blackhole_mss) = 1200;
167 #define V_tcp_pmtud_blackhole_mss VNET(tcp_pmtud_blackhole_mss)
168 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_mss,
169 CTLFLAG_RW|CTLFLAG_VNET,
170 &VNET_NAME(tcp_pmtud_blackhole_mss), 0,
171 "Path MTU Discovery Black Hole Detection lowered MSS");
175 static VNET_DEFINE(int, tcp_v6pmtud_blackhole_mss) = 1220;
176 #define V_tcp_v6pmtud_blackhole_mss VNET(tcp_v6pmtud_blackhole_mss)
177 SYSCTL_INT(_net_inet_tcp, OID_AUTO, v6pmtud_blackhole_mss,
178 CTLFLAG_RW|CTLFLAG_VNET,
179 &VNET_NAME(tcp_v6pmtud_blackhole_mss), 0,
180 "Path MTU Discovery IPv6 Black Hole Detection lowered MSS");
184 static int per_cpu_timers = 1;
186 static int per_cpu_timers = 0;
188 SYSCTL_INT(_net_inet_tcp, OID_AUTO, per_cpu_timers, CTLFLAG_RW,
189 &per_cpu_timers , 0, "run tcp timers on all cpus");
192 #define INP_CPU(inp) (per_cpu_timers ? (!CPU_ABSENT(((inp)->inp_flowid % (mp_maxid+1))) ? \
193 ((inp)->inp_flowid % (mp_maxid+1)) : curcpu) : 0)
197 * Map the given inp to a CPU id.
199 * This queries RSS if it's compiled in, else it defaults to the current
203 inp_to_cpuid(struct inpcb *inp)
208 if (per_cpu_timers) {
209 cpuid = rss_hash2cpuid(inp->inp_flowid, inp->inp_flowtype);
210 if (cpuid == NETISR_CPUID_NONE)
211 return (curcpu); /* XXX */
216 /* Legacy, pre-RSS behaviour */
217 if (per_cpu_timers) {
219 * We don't have a flowid -> cpuid mapping, so cheat and
220 * just map unknown cpuids to curcpu. Not the best, but
221 * apparently better than defaulting to swi 0.
223 cpuid = inp->inp_flowid % (mp_maxid + 1);
224 if (! CPU_ABSENT(cpuid))
229 /* Default for RSS and non-RSS - cpuid 0 */
236 * Tcp protocol timeout routine called every 500 ms.
237 * Updates timestamps used for TCP
238 * causes finite state machine actions if timers expire.
243 VNET_ITERATOR_DECL(vnet_iter);
245 VNET_LIST_RLOCK_NOSLEEP();
246 VNET_FOREACH(vnet_iter) {
247 CURVNET_SET(vnet_iter);
248 (void) tcp_tw_2msl_scan(0);
251 VNET_LIST_RUNLOCK_NOSLEEP();
254 int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
255 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
257 int tcp_backoff[TCP_MAXRXTSHIFT + 1] =
258 { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
260 static int tcp_totbackoff = 2559; /* sum of tcp_backoff[] */
263 * TCP timer processing.
267 tcp_timer_delack(void *xtp)
269 struct tcpcb *tp = xtp;
271 CURVNET_SET(tp->t_vnet);
274 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
276 if (callout_pending(&tp->t_timers->tt_delack) ||
277 !callout_active(&tp->t_timers->tt_delack)) {
282 callout_deactivate(&tp->t_timers->tt_delack);
283 if ((inp->inp_flags & INP_DROPPED) != 0) {
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_DELACK) != 0,
291 ("%s: tp %p delack callout should be running", __func__, tp));
293 tp->t_flags |= TF_ACKNOW;
294 TCPSTAT_INC(tcps_delack);
295 (void) tcp_output(tp);
301 tcp_timer_2msl(void *xtp)
303 struct tcpcb *tp = xtp;
305 CURVNET_SET(tp->t_vnet);
309 ostate = tp->t_state;
311 INP_INFO_RLOCK(&V_tcbinfo);
313 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
315 tcp_free_sackholes(tp);
316 if (callout_pending(&tp->t_timers->tt_2msl) ||
317 !callout_active(&tp->t_timers->tt_2msl)) {
318 INP_WUNLOCK(tp->t_inpcb);
319 INP_INFO_RUNLOCK(&V_tcbinfo);
323 callout_deactivate(&tp->t_timers->tt_2msl);
324 if ((inp->inp_flags & INP_DROPPED) != 0) {
326 INP_INFO_RUNLOCK(&V_tcbinfo);
330 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
331 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
332 KASSERT((tp->t_timers->tt_flags & TT_2MSL) != 0,
333 ("%s: tp %p 2msl callout should be running", __func__, tp));
335 * 2 MSL timeout in shutdown went off. If we're closed but
336 * still waiting for peer to close and connection has been idle
337 * too long delete connection control block. Otherwise, check
340 * If in TIME_WAIT state just ignore as this timeout is handled in
341 * tcp_tw_2msl_scan().
343 * If fastrecycle of FIN_WAIT_2, in FIN_WAIT_2 and receiver has closed,
344 * there's no point in hanging onto FIN_WAIT_2 socket. Just close it.
345 * Ignore fact that there were recent incoming segments.
347 if ((inp->inp_flags & INP_TIMEWAIT) != 0) {
349 INP_INFO_RUNLOCK(&V_tcbinfo);
353 if (tcp_fast_finwait2_recycle && tp->t_state == TCPS_FIN_WAIT_2 &&
354 tp->t_inpcb && tp->t_inpcb->inp_socket &&
355 (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE)) {
356 TCPSTAT_INC(tcps_finwait2_drops);
359 if (ticks - tp->t_rcvtime <= TP_MAXIDLE(tp)) {
360 if (!callout_reset(&tp->t_timers->tt_2msl,
361 TP_KEEPINTVL(tp), tcp_timer_2msl, tp)) {
362 tp->t_timers->tt_flags &= ~TT_2MSL_RST;
369 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
370 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
373 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
377 INP_INFO_RUNLOCK(&V_tcbinfo);
382 tcp_timer_keep(void *xtp)
384 struct tcpcb *tp = xtp;
385 struct tcptemp *t_template;
387 CURVNET_SET(tp->t_vnet);
391 ostate = tp->t_state;
393 INP_INFO_RLOCK(&V_tcbinfo);
395 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
397 if (callout_pending(&tp->t_timers->tt_keep) ||
398 !callout_active(&tp->t_timers->tt_keep)) {
400 INP_INFO_RUNLOCK(&V_tcbinfo);
404 callout_deactivate(&tp->t_timers->tt_keep);
405 if ((inp->inp_flags & INP_DROPPED) != 0) {
407 INP_INFO_RUNLOCK(&V_tcbinfo);
411 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
412 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
413 KASSERT((tp->t_timers->tt_flags & TT_KEEP) != 0,
414 ("%s: tp %p keep callout should be running", __func__, tp));
416 * Keep-alive timer went off; send something
417 * or drop connection if idle for too long.
419 TCPSTAT_INC(tcps_keeptimeo);
420 if (tp->t_state < TCPS_ESTABLISHED)
422 if ((always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
423 tp->t_state <= TCPS_CLOSING) {
424 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
427 * Send a packet designed to force a response
428 * if the peer is up and reachable:
429 * either an ACK if the connection is still alive,
430 * or an RST if the peer has closed the connection
431 * due to timeout or reboot.
432 * Using sequence number tp->snd_una-1
433 * causes the transmitted zero-length segment
434 * to lie outside the receive window;
435 * by the protocol spec, this requires the
436 * correspondent TCP to respond.
438 TCPSTAT_INC(tcps_keepprobe);
439 t_template = tcpip_maketemplate(inp);
441 tcp_respond(tp, t_template->tt_ipgen,
442 &t_template->tt_t, (struct mbuf *)NULL,
443 tp->rcv_nxt, tp->snd_una - 1, 0);
444 free(t_template, M_TEMP);
446 if (!callout_reset(&tp->t_timers->tt_keep, TP_KEEPINTVL(tp),
447 tcp_timer_keep, tp)) {
448 tp->t_timers->tt_flags &= ~TT_KEEP_RST;
450 } else if (!callout_reset(&tp->t_timers->tt_keep, TP_KEEPIDLE(tp),
451 tcp_timer_keep, tp)) {
452 tp->t_timers->tt_flags &= ~TT_KEEP_RST;
456 if (inp->inp_socket->so_options & SO_DEBUG)
457 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
460 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
462 INP_INFO_RUNLOCK(&V_tcbinfo);
467 TCPSTAT_INC(tcps_keepdrops);
468 tp = tcp_drop(tp, ETIMEDOUT);
471 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
472 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
475 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
477 INP_WUNLOCK(tp->t_inpcb);
478 INP_INFO_RUNLOCK(&V_tcbinfo);
483 tcp_timer_persist(void *xtp)
485 struct tcpcb *tp = xtp;
487 CURVNET_SET(tp->t_vnet);
491 ostate = tp->t_state;
493 INP_INFO_RLOCK(&V_tcbinfo);
495 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
497 if (callout_pending(&tp->t_timers->tt_persist) ||
498 !callout_active(&tp->t_timers->tt_persist)) {
500 INP_INFO_RUNLOCK(&V_tcbinfo);
504 callout_deactivate(&tp->t_timers->tt_persist);
505 if ((inp->inp_flags & INP_DROPPED) != 0) {
507 INP_INFO_RUNLOCK(&V_tcbinfo);
511 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
512 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
513 KASSERT((tp->t_timers->tt_flags & TT_PERSIST) != 0,
514 ("%s: tp %p persist callout should be running", __func__, tp));
516 * Persistance timer into zero window.
517 * Force a byte to be output, if possible.
519 TCPSTAT_INC(tcps_persisttimeo);
521 * Hack: if the peer is dead/unreachable, we do not
522 * time out if the window is closed. After a full
523 * backoff, drop the connection if the idle time
524 * (no responses to probes) reaches the maximum
525 * backoff that we would use if retransmitting.
527 if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
528 (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
529 ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
530 TCPSTAT_INC(tcps_persistdrop);
531 tp = tcp_drop(tp, ETIMEDOUT);
535 * If the user has closed the socket then drop a persisting
536 * connection after a much reduced timeout.
538 if (tp->t_state > TCPS_CLOSE_WAIT &&
539 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
540 TCPSTAT_INC(tcps_persistdrop);
541 tp = tcp_drop(tp, ETIMEDOUT);
545 tp->t_flags |= TF_FORCEDATA;
546 (void) tcp_output(tp);
547 tp->t_flags &= ~TF_FORCEDATA;
551 if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
552 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
554 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
557 INP_INFO_RUNLOCK(&V_tcbinfo);
562 tcp_timer_rexmt(void * xtp)
564 struct tcpcb *tp = xtp;
565 CURVNET_SET(tp->t_vnet);
572 ostate = tp->t_state;
575 INP_INFO_RLOCK(&V_tcbinfo);
577 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
579 if (callout_pending(&tp->t_timers->tt_rexmt) ||
580 !callout_active(&tp->t_timers->tt_rexmt)) {
582 INP_INFO_RUNLOCK(&V_tcbinfo);
586 callout_deactivate(&tp->t_timers->tt_rexmt);
587 if ((inp->inp_flags & INP_DROPPED) != 0) {
589 INP_INFO_RUNLOCK(&V_tcbinfo);
593 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
594 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
595 KASSERT((tp->t_timers->tt_flags & TT_REXMT) != 0,
596 ("%s: tp %p rexmt callout should be running", __func__, tp));
597 tcp_free_sackholes(tp);
599 * Retransmission timer went off. Message has not
600 * been acked within retransmit interval. Back off
601 * to a longer retransmit interval and retransmit one segment.
603 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
604 tp->t_rxtshift = TCP_MAXRXTSHIFT;
605 TCPSTAT_INC(tcps_timeoutdrop);
607 tp = tcp_drop(tp, tp->t_softerror ?
608 tp->t_softerror : ETIMEDOUT);
612 INP_INFO_RUNLOCK(&V_tcbinfo);
614 if (tp->t_state == TCPS_SYN_SENT) {
616 * If the SYN was retransmitted, indicate CWND to be
617 * limited to 1 segment in cc_conn_init().
620 } else if (tp->t_rxtshift == 1) {
622 * first retransmit; record ssthresh and cwnd so they can
623 * be recovered if this turns out to be a "bad" retransmit.
624 * A retransmit is considered "bad" if an ACK for this
625 * segment is received within RTT/2 interval; the assumption
626 * here is that the ACK was already in flight. See
627 * "On Estimating End-to-End Network Path Properties" by
628 * Allman and Paxson for more details.
630 tp->snd_cwnd_prev = tp->snd_cwnd;
631 tp->snd_ssthresh_prev = tp->snd_ssthresh;
632 tp->snd_recover_prev = tp->snd_recover;
633 if (IN_FASTRECOVERY(tp->t_flags))
634 tp->t_flags |= TF_WASFRECOVERY;
636 tp->t_flags &= ~TF_WASFRECOVERY;
637 if (IN_CONGRECOVERY(tp->t_flags))
638 tp->t_flags |= TF_WASCRECOVERY;
640 tp->t_flags &= ~TF_WASCRECOVERY;
641 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
642 tp->t_flags |= TF_PREVVALID;
644 tp->t_flags &= ~TF_PREVVALID;
645 TCPSTAT_INC(tcps_rexmttimeo);
646 if (tp->t_state == TCPS_SYN_SENT)
647 rexmt = TCPTV_RTOBASE * tcp_syn_backoff[tp->t_rxtshift];
649 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
650 TCPT_RANGESET(tp->t_rxtcur, rexmt,
651 tp->t_rttmin, TCPTV_REXMTMAX);
654 * We enter the path for PLMTUD if connection is established or, if
655 * connection is FIN_WAIT_1 status, reason for the last is that if
656 * amount of data we send is very small, we could send it in couple of
657 * packets and process straight to FIN. In that case we won't catch
660 if (V_tcp_pmtud_blackhole_detect && (((tp->t_state == TCPS_ESTABLISHED))
661 || (tp->t_state == TCPS_FIN_WAIT_1))) {
668 * Idea here is that at each stage of mtu probe (usually, 1448
669 * -> 1188 -> 524) should be given 2 chances to recover before
670 * further clamping down. 'tp->t_rxtshift % 2 == 0' should
673 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) ==
674 (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) &&
675 (tp->t_rxtshift >= 2 && tp->t_rxtshift % 2 == 0)) {
677 * Enter Path MTU Black-hole Detection mechanism:
678 * - Disable Path MTU Discovery (IP "DF" bit).
679 * - Reduce MTU to lower value than what we
680 * negotiated with peer.
682 /* Record that we may have found a black hole. */
683 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
685 /* Keep track of previous MSS. */
686 optlen = tp->t_maxopd - tp->t_maxseg;
687 tp->t_pmtud_saved_maxopd = tp->t_maxopd;
690 * Reduce the MSS to blackhole value or to the default
691 * in an attempt to retransmit.
694 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? 1 : 0;
696 tp->t_maxopd > V_tcp_v6pmtud_blackhole_mss) {
697 /* Use the sysctl tuneable blackhole MSS. */
698 tp->t_maxopd = V_tcp_v6pmtud_blackhole_mss;
699 V_tcp_pmtud_blackhole_activated++;
701 /* Use the default MSS. */
702 tp->t_maxopd = V_tcp_v6mssdflt;
704 * Disable Path MTU Discovery when we switch to
707 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
708 V_tcp_pmtud_blackhole_activated_min_mss++;
711 #if defined(INET6) && defined(INET)
715 if (tp->t_maxopd > V_tcp_pmtud_blackhole_mss) {
716 /* Use the sysctl tuneable blackhole MSS. */
717 tp->t_maxopd = V_tcp_pmtud_blackhole_mss;
718 V_tcp_pmtud_blackhole_activated++;
720 /* Use the default MSS. */
721 tp->t_maxopd = V_tcp_mssdflt;
723 * Disable Path MTU Discovery when we switch to
726 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
727 V_tcp_pmtud_blackhole_activated_min_mss++;
730 tp->t_maxseg = tp->t_maxopd - optlen;
732 * Reset the slow-start flight size
733 * as it may depend on the new MSS.
735 if (CC_ALGO(tp)->conn_init != NULL)
736 CC_ALGO(tp)->conn_init(tp->ccv);
739 * If further retransmissions are still unsuccessful
740 * with a lowered MTU, maybe this isn't a blackhole and
741 * we restore the previous MSS and blackhole detection
743 * The limit '6' is determined by giving each probe
744 * stage (1448, 1188, 524) 2 chances to recover.
746 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
747 (tp->t_rxtshift > 6)) {
748 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
749 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
750 optlen = tp->t_maxopd - tp->t_maxseg;
751 tp->t_maxopd = tp->t_pmtud_saved_maxopd;
752 tp->t_maxseg = tp->t_maxopd - optlen;
753 V_tcp_pmtud_blackhole_failed++;
755 * Reset the slow-start flight size as it
756 * may depend on the new MSS.
758 if (CC_ALGO(tp)->conn_init != NULL)
759 CC_ALGO(tp)->conn_init(tp->ccv);
765 * Disable RFC1323 and SACK if we haven't got any response to
766 * our third SYN to work-around some broken terminal servers
767 * (most of which have hopefully been retired) that have bad VJ
768 * header compression code which trashes TCP segments containing
769 * unknown-to-them TCP options.
771 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
772 (tp->t_rxtshift == 3))
773 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
775 * If we backed off this far, our srtt estimate is probably bogus.
776 * Clobber it so we'll take the next rtt measurement as our srtt;
777 * move the current srtt into rttvar to keep the current
778 * retransmit times until then.
780 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
782 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
783 in6_losing(tp->t_inpcb);
785 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
788 tp->snd_nxt = tp->snd_una;
789 tp->snd_recover = tp->snd_max;
791 * Force a segment to be sent.
793 tp->t_flags |= TF_ACKNOW;
795 * If timing a segment in this window, stop the timer.
799 cc_cong_signal(tp, NULL, CC_RTO);
801 (void) tcp_output(tp);
805 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
806 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
809 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
813 INP_INFO_RUNLOCK(&V_tcbinfo);
818 tcp_timer_activate(struct tcpcb *tp, uint32_t timer_type, u_int delta)
820 struct callout *t_callout;
821 timeout_t *f_callout;
822 struct inpcb *inp = tp->t_inpcb;
823 int cpu = inp_to_cpuid(inp);
827 if (tp->t_flags & TF_TOE)
831 if (tp->t_timers->tt_flags & TT_STOPPED)
834 switch (timer_type) {
836 t_callout = &tp->t_timers->tt_delack;
837 f_callout = tcp_timer_delack;
838 f_reset = TT_DELACK_RST;
841 t_callout = &tp->t_timers->tt_rexmt;
842 f_callout = tcp_timer_rexmt;
843 f_reset = TT_REXMT_RST;
846 t_callout = &tp->t_timers->tt_persist;
847 f_callout = tcp_timer_persist;
848 f_reset = TT_PERSIST_RST;
851 t_callout = &tp->t_timers->tt_keep;
852 f_callout = tcp_timer_keep;
853 f_reset = TT_KEEP_RST;
856 t_callout = &tp->t_timers->tt_2msl;
857 f_callout = tcp_timer_2msl;
858 f_reset = TT_2MSL_RST;
861 panic("tp %p bad timer_type %#x", tp, timer_type);
864 if ((tp->t_timers->tt_flags & timer_type) &&
865 callout_stop(t_callout) &&
866 (tp->t_timers->tt_flags & f_reset)) {
867 tp->t_timers->tt_flags &= ~(timer_type | f_reset);
870 if ((tp->t_timers->tt_flags & timer_type) == 0) {
871 tp->t_timers->tt_flags |= (timer_type | f_reset);
872 callout_reset_on(t_callout, delta, f_callout, tp, cpu);
874 /* Reset already running callout on the same CPU. */
875 if (!callout_reset(t_callout, delta, f_callout, tp)) {
877 * Callout not cancelled, consider it as not
878 * properly restarted. */
879 tp->t_timers->tt_flags &= ~f_reset;
886 tcp_timer_active(struct tcpcb *tp, uint32_t timer_type)
888 struct callout *t_callout;
890 switch (timer_type) {
892 t_callout = &tp->t_timers->tt_delack;
895 t_callout = &tp->t_timers->tt_rexmt;
898 t_callout = &tp->t_timers->tt_persist;
901 t_callout = &tp->t_timers->tt_keep;
904 t_callout = &tp->t_timers->tt_2msl;
907 panic("tp %p bad timer_type %#x", tp, timer_type);
909 return callout_active(t_callout);
913 tcp_timer_stop(struct tcpcb *tp, uint32_t timer_type)
915 struct callout *t_callout;
916 timeout_t *f_callout;
919 tp->t_timers->tt_flags |= TT_STOPPED;
921 switch (timer_type) {
923 t_callout = &tp->t_timers->tt_delack;
924 f_callout = tcp_timer_delack_discard;
925 f_reset = TT_DELACK_RST;
928 t_callout = &tp->t_timers->tt_rexmt;
929 f_callout = tcp_timer_rexmt_discard;
930 f_reset = TT_REXMT_RST;
933 t_callout = &tp->t_timers->tt_persist;
934 f_callout = tcp_timer_persist_discard;
935 f_reset = TT_PERSIST_RST;
938 t_callout = &tp->t_timers->tt_keep;
939 f_callout = tcp_timer_keep_discard;
940 f_reset = TT_KEEP_RST;
943 t_callout = &tp->t_timers->tt_2msl;
944 f_callout = tcp_timer_2msl_discard;
945 f_reset = TT_2MSL_RST;
948 panic("tp %p bad timer_type %#x", tp, timer_type);
951 if (tp->t_timers->tt_flags & timer_type) {
952 if (callout_stop(t_callout) &&
953 (tp->t_timers->tt_flags & f_reset)) {
954 tp->t_timers->tt_flags &= ~(timer_type | f_reset);
957 * Can't stop the callout, defer tcpcb actual deletion
958 * to the last tcp timer discard callout.
959 * The TT_STOPPED flag will ensure that no tcp timer
960 * callouts can be restarted on our behalf, and
961 * past this point currently running callouts waiting
962 * on inp lock will return right away after the
963 * classical check for callout reset/stop events:
964 * callout_pending() || !callout_active()
966 callout_reset(t_callout, 1, f_callout, tp);
971 #define ticks_to_msecs(t) (1000*(t) / hz)
974 tcp_timer_to_xtimer(struct tcpcb *tp, struct tcp_timer *timer,
975 struct xtcp_timer *xtimer)
979 bzero(xtimer, sizeof(*xtimer));
982 now = getsbinuptime();
983 if (callout_active(&timer->tt_delack))
984 xtimer->tt_delack = (timer->tt_delack.c_time - now) / SBT_1MS;
985 if (callout_active(&timer->tt_rexmt))
986 xtimer->tt_rexmt = (timer->tt_rexmt.c_time - now) / SBT_1MS;
987 if (callout_active(&timer->tt_persist))
988 xtimer->tt_persist = (timer->tt_persist.c_time - now) / SBT_1MS;
989 if (callout_active(&timer->tt_keep))
990 xtimer->tt_keep = (timer->tt_keep.c_time - now) / SBT_1MS;
991 if (callout_active(&timer->tt_2msl))
992 xtimer->tt_2msl = (timer->tt_2msl.c_time - now) / SBT_1MS;
993 xtimer->t_rcvtime = ticks_to_msecs(ticks - tp->t_rcvtime);