The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet/tcp_timer.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 4. Neither the name of the University nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD: releng/10.2/sys/netinet/tcp_timer.c 284261 2015-06-11 13:44:06Z jch $");
   34 
   35 #include "opt_inet.h"
   36 #include "opt_inet6.h"
   37 #include "opt_tcpdebug.h"
   38 
   39 #include <sys/param.h>
   40 #include <sys/kernel.h>
   41 #include <sys/lock.h>
   42 #include <sys/mbuf.h>
   43 #include <sys/mutex.h>
   44 #include <sys/protosw.h>
   45 #include <sys/smp.h>
   46 #include <sys/socket.h>
   47 #include <sys/socketvar.h>
   48 #include <sys/sysctl.h>
   49 #include <sys/systm.h>
   50 
   51 #include <net/if.h>
   52 #include <net/route.h>
   53 #include <net/vnet.h>
   54 
   55 #include <netinet/cc.h>
   56 #include <netinet/in.h>
   57 #include <netinet/in_pcb.h>
   58 #include <netinet/in_systm.h>
   59 #ifdef INET6
   60 #include <netinet6/in6_pcb.h>
   61 #endif
   62 #include <netinet/ip_var.h>
   63 #include <netinet/tcp_fsm.h>
   64 #include <netinet/tcp_timer.h>
   65 #include <netinet/tcp_var.h>
   66 #ifdef INET6
   67 #include <netinet6/tcp6_var.h>
   68 #endif
   69 #include <netinet/tcpip.h>
   70 #ifdef TCPDEBUG
   71 #include <netinet/tcp_debug.h>
   72 #endif
   73 
   74 int     tcp_keepinit;
   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");
   77 
   78 int     tcp_keepidle;
   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");
   81 
   82 int     tcp_keepintvl;
   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");
   85 
   86 int     tcp_delacktime;
   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");
   90 
   91 int     tcp_msl;
   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");
   94 
   95 int     tcp_rexmit_min;
   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");
   99 
  100 int     tcp_rexmit_slop;
  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");
  104 
  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");
  108 
  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");
  113 
  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");
  117 
  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");
  121 
  122         /* max idle probes */
  123 int     tcp_maxpersistidle;
  124 
  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");
  129 
  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,
  133     CTLFLAG_RW,
  134     &VNET_NAME(tcp_pmtud_blackhole_detect), 0,
  135     "Path MTU Discovery Black Hole Detection Enabled");
  136 
  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,
  141     CTLFLAG_RD,
  142     &VNET_NAME(tcp_pmtud_blackhole_activated), 0,
  143     "Path MTU Discovery Black Hole Detection, Activation Count");
  144 
  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,
  149     CTLFLAG_RD,
  150     &VNET_NAME(tcp_pmtud_blackhole_activated_min_mss), 0,
  151     "Path MTU Discovery Black Hole Detection, Activation Count at min MSS");
  152 
  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,
  156     CTLFLAG_RD,
  157     &VNET_NAME(tcp_pmtud_blackhole_failed), 0,
  158     "Path MTU Discovery Black Hole Detection, Failure Count");
  159 
  160 #ifdef INET
  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,
  164     CTLFLAG_RW,
  165     &VNET_NAME(tcp_pmtud_blackhole_mss), 0,
  166     "Path MTU Discovery Black Hole Detection lowered MSS");
  167 #endif
  168 
  169 #ifdef INET6
  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,
  173     CTLFLAG_RW,
  174     &VNET_NAME(tcp_v6pmtud_blackhole_mss), 0,
  175     "Path MTU Discovery IPv6 Black Hole Detection lowered MSS");
  176 #endif
  177 
  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");
  181 
  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)
  184 
  185 /*
  186  * Tcp protocol timeout routine called every 500 ms.
  187  * Updates timestamps used for TCP
  188  * causes finite state machine actions if timers expire.
  189  */
  190 void
  191 tcp_slowtimo(void)
  192 {
  193         VNET_ITERATOR_DECL(vnet_iter);
  194 
  195         VNET_LIST_RLOCK_NOSLEEP();
  196         VNET_FOREACH(vnet_iter) {
  197                 CURVNET_SET(vnet_iter);
  198                 (void) tcp_tw_2msl_scan(0);
  199                 CURVNET_RESTORE();
  200         }
  201         VNET_LIST_RUNLOCK_NOSLEEP();
  202 }
  203 
  204 int     tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
  205     { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
  206 
  207 int     tcp_backoff[TCP_MAXRXTSHIFT + 1] =
  208     { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
  209 
  210 static int tcp_totbackoff = 2559;       /* sum of tcp_backoff[] */
  211 
  212 /*
  213  * TCP timer processing.
  214  */
  215 
  216 void
  217 tcp_timer_delack(void *xtp)
  218 {
  219         struct tcpcb *tp = xtp;
  220         struct inpcb *inp;
  221         CURVNET_SET(tp->t_vnet);
  222 
  223         inp = tp->t_inpcb;
  224         KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
  225         INP_WLOCK(inp);
  226         if (callout_pending(&tp->t_timers->tt_delack) ||
  227             !callout_active(&tp->t_timers->tt_delack)) {
  228                 INP_WUNLOCK(inp);
  229                 CURVNET_RESTORE();
  230                 return;
  231         }
  232         callout_deactivate(&tp->t_timers->tt_delack);
  233         if ((inp->inp_flags & INP_DROPPED) != 0) {
  234                 INP_WUNLOCK(inp);
  235                 CURVNET_RESTORE();
  236                 return;
  237         }
  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));
  242 
  243         tp->t_flags |= TF_ACKNOW;
  244         TCPSTAT_INC(tcps_delack);
  245         (void) tcp_output(tp);
  246         INP_WUNLOCK(inp);
  247         CURVNET_RESTORE();
  248 }
  249 
  250 void
  251 tcp_timer_2msl(void *xtp)
  252 {
  253         struct tcpcb *tp = xtp;
  254         struct inpcb *inp;
  255         CURVNET_SET(tp->t_vnet);
  256 #ifdef TCPDEBUG
  257         int ostate;
  258 
  259         ostate = tp->t_state;
  260 #endif
  261         INP_INFO_WLOCK(&V_tcbinfo);
  262         inp = tp->t_inpcb;
  263         KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
  264         INP_WLOCK(inp);
  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);
  270                 CURVNET_RESTORE();
  271                 return;
  272         }
  273         callout_deactivate(&tp->t_timers->tt_2msl);
  274         if ((inp->inp_flags & INP_DROPPED) != 0) {
  275                 INP_WUNLOCK(inp);
  276                 INP_INFO_WUNLOCK(&V_tcbinfo);
  277                 CURVNET_RESTORE();
  278                 return;
  279         }
  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));
  284         /*
  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.
  289          *
  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.
  293          */
  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);
  298                 tp = tcp_close(tp);             
  299         } else {
  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;
  305                         }
  306                 } else
  307                        tp = tcp_close(tp);
  308        }
  309 
  310 #ifdef TCPDEBUG
  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,
  313                           PRU_SLOWTIMO);
  314 #endif
  315         if (tp != NULL)
  316                 INP_WUNLOCK(inp);
  317         INP_INFO_WUNLOCK(&V_tcbinfo);
  318         CURVNET_RESTORE();
  319 }
  320 
  321 void
  322 tcp_timer_keep(void *xtp)
  323 {
  324         struct tcpcb *tp = xtp;
  325         struct tcptemp *t_template;
  326         struct inpcb *inp;
  327         CURVNET_SET(tp->t_vnet);
  328 #ifdef TCPDEBUG
  329         int ostate;
  330 
  331         ostate = tp->t_state;
  332 #endif
  333         INP_INFO_WLOCK(&V_tcbinfo);
  334         inp = tp->t_inpcb;
  335         KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
  336         INP_WLOCK(inp);
  337         if (callout_pending(&tp->t_timers->tt_keep) ||
  338             !callout_active(&tp->t_timers->tt_keep)) {
  339                 INP_WUNLOCK(inp);
  340                 INP_INFO_WUNLOCK(&V_tcbinfo);
  341                 CURVNET_RESTORE();
  342                 return;
  343         }
  344         callout_deactivate(&tp->t_timers->tt_keep);
  345         if ((inp->inp_flags & INP_DROPPED) != 0) {
  346                 INP_WUNLOCK(inp);
  347                 INP_INFO_WUNLOCK(&V_tcbinfo);
  348                 CURVNET_RESTORE();
  349                 return;
  350         }
  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));
  355         /*
  356          * Keep-alive timer went off; send something
  357          * or drop connection if idle for too long.
  358          */
  359         TCPSTAT_INC(tcps_keeptimeo);
  360         if (tp->t_state < TCPS_ESTABLISHED)
  361                 goto dropit;
  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))
  365                         goto dropit;
  366                 /*
  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.
  377                  */
  378                 TCPSTAT_INC(tcps_keepprobe);
  379                 t_template = tcpip_maketemplate(inp);
  380                 if (t_template) {
  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);
  385                 }
  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;
  389                 }
  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;
  393                 }
  394 
  395 #ifdef TCPDEBUG
  396         if (inp->inp_socket->so_options & SO_DEBUG)
  397                 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
  398                           PRU_SLOWTIMO);
  399 #endif
  400         INP_WUNLOCK(inp);
  401         INP_INFO_WUNLOCK(&V_tcbinfo);
  402         CURVNET_RESTORE();
  403         return;
  404 
  405 dropit:
  406         TCPSTAT_INC(tcps_keepdrops);
  407         tp = tcp_drop(tp, ETIMEDOUT);
  408 
  409 #ifdef TCPDEBUG
  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,
  412                           PRU_SLOWTIMO);
  413 #endif
  414         if (tp != NULL)
  415                 INP_WUNLOCK(tp->t_inpcb);
  416         INP_INFO_WUNLOCK(&V_tcbinfo);
  417         CURVNET_RESTORE();
  418 }
  419 
  420 void
  421 tcp_timer_persist(void *xtp)
  422 {
  423         struct tcpcb *tp = xtp;
  424         struct inpcb *inp;
  425         CURVNET_SET(tp->t_vnet);
  426 #ifdef TCPDEBUG
  427         int ostate;
  428 
  429         ostate = tp->t_state;
  430 #endif
  431         INP_INFO_WLOCK(&V_tcbinfo);
  432         inp = tp->t_inpcb;
  433         KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
  434         INP_WLOCK(inp);
  435         if (callout_pending(&tp->t_timers->tt_persist) ||
  436             !callout_active(&tp->t_timers->tt_persist)) {
  437                 INP_WUNLOCK(inp);
  438                 INP_INFO_WUNLOCK(&V_tcbinfo);
  439                 CURVNET_RESTORE();
  440                 return;
  441         }
  442         callout_deactivate(&tp->t_timers->tt_persist);
  443         if ((inp->inp_flags & INP_DROPPED) != 0) {
  444                 INP_WUNLOCK(inp);
  445                 INP_INFO_WUNLOCK(&V_tcbinfo);
  446                 CURVNET_RESTORE();
  447                 return;
  448         }
  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));
  453         /*
  454          * Persistance timer into zero window.
  455          * Force a byte to be output, if possible.
  456          */
  457         TCPSTAT_INC(tcps_persisttimeo);
  458         /*
  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.
  464          */
  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);
  470                 goto out;
  471         }
  472         /*
  473          * If the user has closed the socket then drop a persisting
  474          * connection after a much reduced timeout.
  475          */
  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);
  480                 goto out;
  481         }
  482         tcp_setpersist(tp);
  483         tp->t_flags |= TF_FORCEDATA;
  484         (void) tcp_output(tp);
  485         tp->t_flags &= ~TF_FORCEDATA;
  486 
  487 out:
  488 #ifdef TCPDEBUG
  489         if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
  490                 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
  491 #endif
  492         if (tp != NULL)
  493                 INP_WUNLOCK(inp);
  494         INP_INFO_WUNLOCK(&V_tcbinfo);
  495         CURVNET_RESTORE();
  496 }
  497 
  498 void
  499 tcp_timer_rexmt(void * xtp)
  500 {
  501         struct tcpcb *tp = xtp;
  502         CURVNET_SET(tp->t_vnet);
  503         int rexmt;
  504         int headlocked;
  505         struct inpcb *inp;
  506 #ifdef TCPDEBUG
  507         int ostate;
  508 
  509         ostate = tp->t_state;
  510 #endif
  511 
  512         INP_INFO_RLOCK(&V_tcbinfo);
  513         inp = tp->t_inpcb;
  514         KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
  515         INP_WLOCK(inp);
  516         if (callout_pending(&tp->t_timers->tt_rexmt) ||
  517             !callout_active(&tp->t_timers->tt_rexmt)) {
  518                 INP_WUNLOCK(inp);
  519                 INP_INFO_RUNLOCK(&V_tcbinfo);
  520                 CURVNET_RESTORE();
  521                 return;
  522         }
  523         callout_deactivate(&tp->t_timers->tt_rexmt);
  524         if ((inp->inp_flags & INP_DROPPED) != 0) {
  525                 INP_WUNLOCK(inp);
  526                 INP_INFO_RUNLOCK(&V_tcbinfo);
  527                 CURVNET_RESTORE();
  528                 return;
  529         }
  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);
  535         /*
  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.
  539          */
  540         if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
  541                 tp->t_rxtshift = TCP_MAXRXTSHIFT;
  542                 TCPSTAT_INC(tcps_timeoutdrop);
  543                 in_pcbref(inp);
  544                 INP_INFO_RUNLOCK(&V_tcbinfo);
  545                 INP_WUNLOCK(inp);
  546                 INP_INFO_WLOCK(&V_tcbinfo);
  547                 INP_WLOCK(inp);
  548                 if (in_pcbrele_wlocked(inp)) {
  549                         INP_INFO_WUNLOCK(&V_tcbinfo);
  550                         CURVNET_RESTORE();
  551                         return;
  552                 }
  553                 if (inp->inp_flags & INP_DROPPED) {
  554                         INP_WUNLOCK(inp);
  555                         INP_INFO_WUNLOCK(&V_tcbinfo);
  556                         CURVNET_RESTORE();
  557                         return;
  558                 }
  559 
  560                 tp = tcp_drop(tp, tp->t_softerror ?
  561                               tp->t_softerror : ETIMEDOUT);
  562                 headlocked = 1;
  563                 goto out;
  564         }
  565         INP_INFO_RUNLOCK(&V_tcbinfo);
  566         headlocked = 0;
  567         if (tp->t_state == TCPS_SYN_SENT) {
  568                 /*
  569                  * If the SYN was retransmitted, indicate CWND to be
  570                  * limited to 1 segment in cc_conn_init().
  571                  */
  572                 tp->snd_cwnd = 1;
  573         } else if (tp->t_rxtshift == 1) {
  574                 /*
  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.
  582                  */
  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;
  588                 else
  589                         tp->t_flags &= ~TF_WASFRECOVERY;
  590                 if (IN_CONGRECOVERY(tp->t_flags))
  591                         tp->t_flags |= TF_WASCRECOVERY;
  592                 else
  593                         tp->t_flags &= ~TF_WASCRECOVERY;
  594                 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
  595                 tp->t_flags |= TF_PREVVALID;
  596         } else
  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];
  601         else
  602                 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
  603         TCPT_RANGESET(tp->t_rxtcur, rexmt,
  604                       tp->t_rttmin, TCPTV_REXMTMAX);
  605 
  606         /*
  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
  611          * ESTABLISHED state.
  612          */
  613         if (V_tcp_pmtud_blackhole_detect && (((tp->t_state == TCPS_ESTABLISHED))
  614             || (tp->t_state == TCPS_FIN_WAIT_1))) {
  615                 int optlen;
  616 #ifdef INET6
  617                 int isipv6;
  618 #endif
  619 
  620                 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) ==
  621                     (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) &&
  622                     (tp->t_rxtshift <= 2)) {
  623                         /*
  624                          * Enter Path MTU Black-hole Detection mechanism:
  625                          * - Disable Path MTU Discovery (IP "DF" bit).
  626                          * - Reduce MTU to lower value than what we
  627                          *   negotiated with peer.
  628                          */
  629                         /* Record that we may have found a black hole. */
  630                         tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
  631 
  632                         /* Keep track of previous MSS. */
  633                         optlen = tp->t_maxopd - tp->t_maxseg;
  634                         tp->t_pmtud_saved_maxopd = tp->t_maxopd;
  635 
  636                         /* 
  637                          * Reduce the MSS to blackhole value or to the default
  638                          * in an attempt to retransmit.
  639                          */
  640 #ifdef INET6
  641                         isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? 1 : 0;
  642                         if (isipv6 &&
  643                             tp->t_maxopd > V_tcp_v6pmtud_blackhole_mss) {
  644                                 /* Use the sysctl tuneable blackhole MSS. */
  645                                 tp->t_maxopd = V_tcp_v6pmtud_blackhole_mss;
  646                                 V_tcp_pmtud_blackhole_activated++;
  647                         } else if (isipv6) {
  648                                 /* Use the default MSS. */
  649                                 tp->t_maxopd = V_tcp_v6mssdflt;
  650                                 /*
  651                                  * Disable Path MTU Discovery when we switch to
  652                                  * minmss.
  653                                  */
  654                                 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
  655                                 V_tcp_pmtud_blackhole_activated_min_mss++;
  656                         }
  657 #endif
  658 #if defined(INET6) && defined(INET)
  659                         else
  660 #endif
  661 #ifdef INET
  662                         if (tp->t_maxopd > V_tcp_pmtud_blackhole_mss) {
  663                                 /* Use the sysctl tuneable blackhole MSS. */
  664                                 tp->t_maxopd = V_tcp_pmtud_blackhole_mss;
  665                                 V_tcp_pmtud_blackhole_activated++;
  666                         } else {
  667                                 /* Use the default MSS. */
  668                                 tp->t_maxopd = V_tcp_mssdflt;
  669                                 /*
  670                                  * Disable Path MTU Discovery when we switch to
  671                                  * minmss.
  672                                  */
  673                                 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
  674                                 V_tcp_pmtud_blackhole_activated_min_mss++;
  675                         }
  676 #endif
  677                         tp->t_maxseg = tp->t_maxopd - optlen;
  678                         /*
  679                          * Reset the slow-start flight size
  680                          * as it may depend on the new MSS.
  681                          */
  682                         if (CC_ALGO(tp)->conn_init != NULL)
  683                                 CC_ALGO(tp)->conn_init(tp->ccv);
  684                 } else {
  685                         /*
  686                          * If further retransmissions are still unsuccessful
  687                          * with a lowered MTU, maybe this isn't a blackhole and
  688                          * we restore the previous MSS and blackhole detection
  689                          * flags.
  690                          */
  691                         if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
  692                             (tp->t_rxtshift > 4)) {
  693                                 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
  694                                 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
  695                                 optlen = tp->t_maxopd - tp->t_maxseg;
  696                                 tp->t_maxopd = tp->t_pmtud_saved_maxopd;
  697                                 tp->t_maxseg = tp->t_maxopd - optlen;
  698                                 V_tcp_pmtud_blackhole_failed++;
  699                                 /*
  700                                  * Reset the slow-start flight size as it
  701                                  * may depend on the new MSS.
  702                                  */
  703                                 if (CC_ALGO(tp)->conn_init != NULL)
  704                                         CC_ALGO(tp)->conn_init(tp->ccv);
  705                         }
  706                 }
  707         }
  708 
  709         /*
  710          * Disable RFC1323 and SACK if we haven't got any response to
  711          * our third SYN to work-around some broken terminal servers
  712          * (most of which have hopefully been retired) that have bad VJ
  713          * header compression code which trashes TCP segments containing
  714          * unknown-to-them TCP options.
  715          */
  716         if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
  717             (tp->t_rxtshift == 3))
  718                 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
  719         /*
  720          * If we backed off this far, our srtt estimate is probably bogus.
  721          * Clobber it so we'll take the next rtt measurement as our srtt;
  722          * move the current srtt into rttvar to keep the current
  723          * retransmit times until then.
  724          */
  725         if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
  726 #ifdef INET6
  727                 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
  728                         in6_losing(tp->t_inpcb);
  729 #endif
  730                 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
  731                 tp->t_srtt = 0;
  732         }
  733         tp->snd_nxt = tp->snd_una;
  734         tp->snd_recover = tp->snd_max;
  735         /*
  736          * Force a segment to be sent.
  737          */
  738         tp->t_flags |= TF_ACKNOW;
  739         /*
  740          * If timing a segment in this window, stop the timer.
  741          */
  742         tp->t_rtttime = 0;
  743 
  744         cc_cong_signal(tp, NULL, CC_RTO);
  745 
  746         (void) tcp_output(tp);
  747 
  748 out:
  749 #ifdef TCPDEBUG
  750         if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
  751                 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
  752                           PRU_SLOWTIMO);
  753 #endif
  754         if (tp != NULL)
  755                 INP_WUNLOCK(inp);
  756         if (headlocked)
  757                 INP_INFO_WUNLOCK(&V_tcbinfo);
  758         CURVNET_RESTORE();
  759 }
  760 
  761 void
  762 tcp_timer_activate(struct tcpcb *tp, uint32_t timer_type, u_int delta)
  763 {
  764         struct callout *t_callout;
  765         timeout_t *f_callout;
  766         struct inpcb *inp = tp->t_inpcb;
  767         int cpu = INP_CPU(inp);
  768         uint32_t f_reset;
  769 
  770 #ifdef TCP_OFFLOAD
  771         if (tp->t_flags & TF_TOE)
  772                 return;
  773 #endif
  774 
  775         if (tp->t_timers->tt_flags & TT_STOPPED)
  776                 return;
  777 
  778         switch (timer_type) {
  779                 case TT_DELACK:
  780                         t_callout = &tp->t_timers->tt_delack;
  781                         f_callout = tcp_timer_delack;
  782                         f_reset = TT_DELACK_RST;
  783                         break;
  784                 case TT_REXMT:
  785                         t_callout = &tp->t_timers->tt_rexmt;
  786                         f_callout = tcp_timer_rexmt;
  787                         f_reset = TT_REXMT_RST;
  788                         break;
  789                 case TT_PERSIST:
  790                         t_callout = &tp->t_timers->tt_persist;
  791                         f_callout = tcp_timer_persist;
  792                         f_reset = TT_PERSIST_RST;
  793                         break;
  794                 case TT_KEEP:
  795                         t_callout = &tp->t_timers->tt_keep;
  796                         f_callout = tcp_timer_keep;
  797                         f_reset = TT_KEEP_RST;
  798                         break;
  799                 case TT_2MSL:
  800                         t_callout = &tp->t_timers->tt_2msl;
  801                         f_callout = tcp_timer_2msl;
  802                         f_reset = TT_2MSL_RST;
  803                         break;
  804                 default:
  805                         panic("tp %p bad timer_type %#x", tp, timer_type);
  806                 }
  807         if (delta == 0) {
  808                 if ((tp->t_timers->tt_flags & timer_type) &&
  809                     callout_stop(t_callout) &&
  810                     (tp->t_timers->tt_flags & f_reset)) {
  811                         tp->t_timers->tt_flags &= ~(timer_type | f_reset);
  812                 }
  813         } else {
  814                 if ((tp->t_timers->tt_flags & timer_type) == 0) {
  815                         tp->t_timers->tt_flags |= (timer_type | f_reset);
  816                         callout_reset_on(t_callout, delta, f_callout, tp, cpu);
  817                 } else {
  818                         /* Reset already running callout on the same CPU. */
  819                         if (!callout_reset(t_callout, delta, f_callout, tp)) {
  820                                 /*
  821                                  * Callout not cancelled, consider it as not
  822                                  * properly restarted. */
  823                                 tp->t_timers->tt_flags &= ~f_reset;
  824                         }
  825                 }
  826         }
  827 }
  828 
  829 int
  830 tcp_timer_active(struct tcpcb *tp, uint32_t timer_type)
  831 {
  832         struct callout *t_callout;
  833 
  834         switch (timer_type) {
  835                 case TT_DELACK:
  836                         t_callout = &tp->t_timers->tt_delack;
  837                         break;
  838                 case TT_REXMT:
  839                         t_callout = &tp->t_timers->tt_rexmt;
  840                         break;
  841                 case TT_PERSIST:
  842                         t_callout = &tp->t_timers->tt_persist;
  843                         break;
  844                 case TT_KEEP:
  845                         t_callout = &tp->t_timers->tt_keep;
  846                         break;
  847                 case TT_2MSL:
  848                         t_callout = &tp->t_timers->tt_2msl;
  849                         break;
  850                 default:
  851                         panic("tp %p bad timer_type %#x", tp, timer_type);
  852                 }
  853         return callout_active(t_callout);
  854 }
  855 
  856 void
  857 tcp_timer_stop(struct tcpcb *tp, uint32_t timer_type)
  858 {
  859         struct callout *t_callout;
  860         timeout_t *f_callout;
  861         uint32_t f_reset;
  862 
  863         tp->t_timers->tt_flags |= TT_STOPPED;
  864 
  865         switch (timer_type) {
  866                 case TT_DELACK:
  867                         t_callout = &tp->t_timers->tt_delack;
  868                         f_callout = tcp_timer_delack_discard;
  869                         f_reset = TT_DELACK_RST;
  870                         break;
  871                 case TT_REXMT:
  872                         t_callout = &tp->t_timers->tt_rexmt;
  873                         f_callout = tcp_timer_rexmt_discard;
  874                         f_reset = TT_REXMT_RST;
  875                         break;
  876                 case TT_PERSIST:
  877                         t_callout = &tp->t_timers->tt_persist;
  878                         f_callout = tcp_timer_persist_discard;
  879                         f_reset = TT_PERSIST_RST;
  880                         break;
  881                 case TT_KEEP:
  882                         t_callout = &tp->t_timers->tt_keep;
  883                         f_callout = tcp_timer_keep_discard;
  884                         f_reset = TT_KEEP_RST;
  885                         break;
  886                 case TT_2MSL:
  887                         t_callout = &tp->t_timers->tt_2msl;
  888                         f_callout = tcp_timer_2msl_discard;
  889                         f_reset = TT_2MSL_RST;
  890                         break;
  891                 default:
  892                         panic("tp %p bad timer_type %#x", tp, timer_type);
  893                 }
  894 
  895         if (tp->t_timers->tt_flags & timer_type) {
  896                 if (callout_stop(t_callout) &&
  897                     (tp->t_timers->tt_flags & f_reset)) {
  898                         tp->t_timers->tt_flags &= ~(timer_type | f_reset);
  899                 } else {
  900                         /*
  901                          * Can't stop the callout, defer tcpcb actual deletion
  902                          * to the last tcp timer discard callout.
  903                          * The TT_STOPPED flag will ensure that no tcp timer
  904                          * callouts can be restarted on our behalf, and
  905                          * past this point currently running callouts waiting
  906                          * on inp lock will return right away after the
  907                          * classical check for callout reset/stop events:
  908                          * callout_pending() || !callout_active()
  909                          */
  910                         callout_reset(t_callout, 1, f_callout, tp);
  911                 }
  912         }
  913 }
  914 
  915 #define ticks_to_msecs(t)       (1000*(t) / hz)
  916 
  917 void
  918 tcp_timer_to_xtimer(struct tcpcb *tp, struct tcp_timer *timer,
  919     struct xtcp_timer *xtimer)
  920 {
  921         sbintime_t now;
  922 
  923         bzero(xtimer, sizeof(*xtimer));
  924         if (timer == NULL)
  925                 return;
  926         now = getsbinuptime();
  927         if (callout_active(&timer->tt_delack))
  928                 xtimer->tt_delack = (timer->tt_delack.c_time - now) / SBT_1MS;
  929         if (callout_active(&timer->tt_rexmt))
  930                 xtimer->tt_rexmt = (timer->tt_rexmt.c_time - now) / SBT_1MS;
  931         if (callout_active(&timer->tt_persist))
  932                 xtimer->tt_persist = (timer->tt_persist.c_time - now) / SBT_1MS;
  933         if (callout_active(&timer->tt_keep))
  934                 xtimer->tt_keep = (timer->tt_keep.c_time - now) / SBT_1MS;
  935         if (callout_active(&timer->tt_2msl))
  936                 xtimer->tt_2msl = (timer->tt_2msl.c_time - now) / SBT_1MS;
  937         xtimer->t_rcvtime = ticks_to_msecs(ticks - tp->t_rcvtime);
  938 }

Cache object: 873f00181b81b6a810dcf585783f1084


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.