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_reass.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, 1994, 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_input.c 8.12 (Berkeley) 5/24/95
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD: releng/7.4/sys/netinet/tcp_reass.c 214890 2010-11-06 15:49:59Z lstewart $");
   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/malloc.h>
   42 #include <sys/mbuf.h>
   43 #include <sys/socket.h>
   44 #include <sys/socketvar.h>
   45 #include <sys/sysctl.h>
   46 #include <sys/syslog.h>
   47 #include <sys/systm.h>
   48 
   49 #include <vm/uma.h>
   50 
   51 #include <net/if.h>
   52 #include <net/route.h>
   53 
   54 #include <netinet/in.h>
   55 #include <netinet/in_pcb.h>
   56 #include <netinet/in_systm.h>
   57 #include <netinet/in_var.h>
   58 #include <netinet/ip.h>
   59 #include <netinet/ip_var.h>
   60 #include <netinet/ip_options.h>
   61 #include <netinet/ip6.h>
   62 #include <netinet6/in6_pcb.h>
   63 #include <netinet6/ip6_var.h>
   64 #include <netinet6/nd6.h>
   65 #include <netinet/tcp.h>
   66 #include <netinet/tcp_fsm.h>
   67 #include <netinet/tcp_seq.h>
   68 #include <netinet/tcp_timer.h>
   69 #include <netinet/tcp_var.h>
   70 #include <netinet6/tcp6_var.h>
   71 #include <netinet/tcpip.h>
   72 #ifdef TCPDEBUG
   73 #include <netinet/tcp_debug.h>
   74 #endif /* TCPDEBUG */
   75 
   76 static int tcp_reass_sysctl_maxseg(SYSCTL_HANDLER_ARGS);
   77 static int tcp_reass_sysctl_qsize(SYSCTL_HANDLER_ARGS);
   78 
   79 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0,
   80     "TCP Segment Reassembly Queue");
   81 
   82 static int tcp_reass_maxseg = 0;
   83 SYSCTL_PROC(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RDTUN,
   84     &tcp_reass_maxseg, 0, &tcp_reass_sysctl_maxseg, "I",
   85     "Global maximum number of TCP Segments in Reassembly Queue");
   86 
   87 static int tcp_reass_qsize = 0;
   88 SYSCTL_PROC(_net_inet_tcp_reass, OID_AUTO, cursegments, CTLFLAG_RD,
   89     &tcp_reass_qsize, 0, &tcp_reass_sysctl_qsize, "I",
   90     "Global number of TCP Segments currently in Reassembly Queue");
   91 
   92 static int tcp_reass_overflows = 0;
   93 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows, CTLFLAG_RD,
   94     &tcp_reass_overflows, 0,
   95     "Global number of TCP Segment Reassembly Queue Overflows");
   96 
   97 static uma_zone_t       tcp_reass_zone;
   98 
   99 /* Initialize TCP reassembly queue */
  100 static void
  101 tcp_reass_zone_change(void *tag)
  102 {
  103 
  104         tcp_reass_maxseg = nmbclusters / 16;
  105         uma_zone_set_max(tcp_reass_zone, tcp_reass_maxseg);
  106 }
  107 
  108 void
  109 tcp_reass_init(void)
  110 {
  111 
  112         tcp_reass_maxseg = nmbclusters / 16;
  113         TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments",
  114             &tcp_reass_maxseg);
  115         tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent),
  116             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  117         uma_zone_set_max(tcp_reass_zone, tcp_reass_maxseg);
  118         EVENTHANDLER_REGISTER(nmbclusters_change,
  119             tcp_reass_zone_change, NULL, EVENTHANDLER_PRI_ANY);
  120 }
  121 
  122 void
  123 tcp_reass_flush(struct tcpcb *tp)
  124 {
  125         struct tseg_qent *qe;
  126 
  127         INP_WLOCK_ASSERT(tp->t_inpcb);
  128 
  129         while ((qe = LIST_FIRST(&tp->t_segq)) != NULL) {
  130                 LIST_REMOVE(qe, tqe_q);
  131                 m_freem(qe->tqe_m);
  132                 uma_zfree(tcp_reass_zone, qe);
  133                 tp->t_segqlen--;
  134         }
  135 
  136         KASSERT((tp->t_segqlen == 0),
  137             ("TCP reass queue %p segment count is %d instead of 0 after flush.",
  138             tp, tp->t_segqlen));
  139 }
  140 
  141 static int
  142 tcp_reass_sysctl_maxseg(SYSCTL_HANDLER_ARGS)
  143 {
  144         tcp_reass_maxseg = uma_zone_get_max(tcp_reass_zone);
  145         return (sysctl_handle_int(oidp, arg1, arg2, req));
  146 }
  147 
  148 static int
  149 tcp_reass_sysctl_qsize(SYSCTL_HANDLER_ARGS)
  150 {
  151         tcp_reass_qsize = uma_zone_get_cur(tcp_reass_zone);
  152         return (sysctl_handle_int(oidp, arg1, arg2, req));
  153 }
  154 
  155 int
  156 tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m)
  157 {
  158         struct tseg_qent *q;
  159         struct tseg_qent *p = NULL;
  160         struct tseg_qent *nq;
  161         struct tseg_qent *te = NULL;
  162         struct socket *so = tp->t_inpcb->inp_socket;
  163         int flags;
  164 
  165         INP_WLOCK_ASSERT(tp->t_inpcb);
  166 
  167         /*
  168          * XXX: tcp_reass() is rather inefficient with its data structures
  169          * and should be rewritten (see NetBSD for optimizations).  While
  170          * doing that it should move to its own file tcp_reass.c.
  171          */
  172 
  173         /*
  174          * Call with th==NULL after become established to
  175          * force pre-ESTABLISHED data up to user socket.
  176          */
  177         if (th == NULL)
  178                 goto present;
  179 
  180         /*
  181          * Limit the number of segments that can be queued to reduce the
  182          * potential for mbuf exhaustion. For best performance, we want to be
  183          * able to queue a full window's worth of segments. The size of the
  184          * socket receive buffer determines our advertised window and grows
  185          * automatically when socket buffer autotuning is enabled. Use it as the
  186          * basis for our queue limit.
  187          * Always let the missing segment through which caused this queue.
  188          * NB: Access to the socket buffer is left intentionally unlocked as we
  189          * can tolerate stale information here.
  190          *
  191          * XXXLAS: Using sbspace(so->so_rcv) instead of so->so_rcv.sb_hiwat
  192          * should work but causes packets to be dropped when they shouldn't.
  193          * Investigate why and re-evaluate the below limit after the behaviour
  194          * is understood.
  195          */
  196         if (th->th_seq != tp->rcv_nxt &&
  197              tp->t_segqlen >= (so->so_rcv.sb_hiwat / tp->t_maxseg) + 1) {
  198                 tcp_reass_overflows++;
  199                 tcpstat.tcps_rcvmemdrop++;
  200                 m_freem(m);
  201                 *tlenp = 0;
  202                 return (0);
  203         }
  204 
  205         /*
  206          * Allocate a new queue entry. If we can't, or hit the zone limit
  207          * just drop the pkt.
  208          */
  209         te = uma_zalloc(tcp_reass_zone, M_NOWAIT);
  210         if (te == NULL) {
  211                 tcpstat.tcps_rcvmemdrop++;
  212                 m_freem(m);
  213                 *tlenp = 0;
  214                 return (0);
  215         }
  216         tp->t_segqlen++;
  217 
  218         /*
  219          * Find a segment which begins after this one does.
  220          */
  221         LIST_FOREACH(q, &tp->t_segq, tqe_q) {
  222                 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
  223                         break;
  224                 p = q;
  225         }
  226 
  227         /*
  228          * If there is a preceding segment, it may provide some of
  229          * our data already.  If so, drop the data from the incoming
  230          * segment.  If it provides all of our data, drop us.
  231          */
  232         if (p != NULL) {
  233                 int i;
  234                 /* conversion to int (in i) handles seq wraparound */
  235                 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
  236                 if (i > 0) {
  237                         if (i >= *tlenp) {
  238                                 tcpstat.tcps_rcvduppack++;
  239                                 tcpstat.tcps_rcvdupbyte += *tlenp;
  240                                 m_freem(m);
  241                                 uma_zfree(tcp_reass_zone, te);
  242                                 tp->t_segqlen--;
  243                                 /*
  244                                  * Try to present any queued data
  245                                  * at the left window edge to the user.
  246                                  * This is needed after the 3-WHS
  247                                  * completes.
  248                                  */
  249                                 goto present;   /* ??? */
  250                         }
  251                         m_adj(m, i);
  252                         *tlenp -= i;
  253                         th->th_seq += i;
  254                 }
  255         }
  256         tcpstat.tcps_rcvoopack++;
  257         tcpstat.tcps_rcvoobyte += *tlenp;
  258 
  259         /*
  260          * While we overlap succeeding segments trim them or,
  261          * if they are completely covered, dequeue them.
  262          */
  263         while (q) {
  264                 int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
  265                 if (i <= 0)
  266                         break;
  267                 if (i < q->tqe_len) {
  268                         q->tqe_th->th_seq += i;
  269                         q->tqe_len -= i;
  270                         m_adj(q->tqe_m, i);
  271                         break;
  272                 }
  273 
  274                 nq = LIST_NEXT(q, tqe_q);
  275                 LIST_REMOVE(q, tqe_q);
  276                 m_freem(q->tqe_m);
  277                 uma_zfree(tcp_reass_zone, q);
  278                 tp->t_segqlen--;
  279                 q = nq;
  280         }
  281 
  282         /* Insert the new segment queue entry into place. */
  283         te->tqe_m = m;
  284         te->tqe_th = th;
  285         te->tqe_len = *tlenp;
  286 
  287         if (p == NULL) {
  288                 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
  289         } else {
  290                 LIST_INSERT_AFTER(p, te, tqe_q);
  291         }
  292 
  293 present:
  294         /*
  295          * Present data to user, advancing rcv_nxt through
  296          * completed sequence space.
  297          */
  298         if (!TCPS_HAVEESTABLISHED(tp->t_state))
  299                 return (0);
  300         q = LIST_FIRST(&tp->t_segq);
  301         if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
  302                 return (0);
  303         SOCKBUF_LOCK(&so->so_rcv);
  304         do {
  305                 tp->rcv_nxt += q->tqe_len;
  306                 flags = q->tqe_th->th_flags & TH_FIN;
  307                 nq = LIST_NEXT(q, tqe_q);
  308                 LIST_REMOVE(q, tqe_q);
  309                 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
  310                         m_freem(q->tqe_m);
  311                 else
  312                         sbappendstream_locked(&so->so_rcv, q->tqe_m);
  313                 uma_zfree(tcp_reass_zone, q);
  314                 tp->t_segqlen--;
  315                 q = nq;
  316         } while (q && q->tqe_th->th_seq == tp->rcv_nxt);
  317         ND6_HINT(tp);
  318         sorwakeup_locked(so);
  319         return (flags);
  320 }

Cache object: 3878e5968a95763e6f769fa3815bccd3


[ 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.