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/kern/uipc_socket2.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
    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  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This product includes software developed by the University of
   16  *      California, Berkeley and its contributors.
   17  * 4. Neither the name of the University nor the names of its contributors
   18  *    may be used to endorse or promote products derived from this software
   19  *    without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  *
   33  *      @(#)uipc_socket2.c      8.1 (Berkeley) 6/10/93
   34  * $FreeBSD: releng/5.0/sys/kern/uipc_socket2.c 106473 2002-11-05 18:52:25Z kbyanc $
   35  */
   36 
   37 #include "opt_mac.h"
   38 #include "opt_param.h"
   39 
   40 #include <sys/param.h>
   41 #include <sys/aio.h> /* for aio_swake proto */
   42 #include <sys/domain.h>
   43 #include <sys/event.h>
   44 #include <sys/file.h>   /* for maxfiles */
   45 #include <sys/kernel.h>
   46 #include <sys/lock.h>
   47 #include <sys/mac.h>
   48 #include <sys/malloc.h>
   49 #include <sys/mbuf.h>
   50 #include <sys/mutex.h>
   51 #include <sys/proc.h>
   52 #include <sys/protosw.h>
   53 #include <sys/resourcevar.h>
   54 #include <sys/signalvar.h>
   55 #include <sys/socket.h>
   56 #include <sys/socketvar.h>
   57 #include <sys/stat.h>
   58 #include <sys/sysctl.h>
   59 #include <sys/systm.h>
   60 
   61 int     maxsockets;
   62 
   63 void (*aio_swake)(struct socket *, struct sockbuf *);
   64 
   65 /*
   66  * Primitive routines for operating on sockets and socket buffers
   67  */
   68 
   69 u_long  sb_max = SB_MAX;
   70 u_long  sb_max_adj =
   71     SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */
   72 
   73 static  u_long sb_efficiency = 8;       /* parameter for sbreserve() */
   74 
   75 /*
   76  * Procedures to manipulate state flags of socket
   77  * and do appropriate wakeups.  Normal sequence from the
   78  * active (originating) side is that soisconnecting() is
   79  * called during processing of connect() call,
   80  * resulting in an eventual call to soisconnected() if/when the
   81  * connection is established.  When the connection is torn down
   82  * soisdisconnecting() is called during processing of disconnect() call,
   83  * and soisdisconnected() is called when the connection to the peer
   84  * is totally severed.  The semantics of these routines are such that
   85  * connectionless protocols can call soisconnected() and soisdisconnected()
   86  * only, bypassing the in-progress calls when setting up a ``connection''
   87  * takes no time.
   88  *
   89  * From the passive side, a socket is created with
   90  * two queues of sockets: so_incomp for connections in progress
   91  * and so_comp for connections already made and awaiting user acceptance.
   92  * As a protocol is preparing incoming connections, it creates a socket
   93  * structure queued on so_incomp by calling sonewconn().  When the connection
   94  * is established, soisconnected() is called, and transfers the
   95  * socket structure to so_comp, making it available to accept().
   96  *
   97  * If a socket is closed with sockets on either
   98  * so_incomp or so_comp, these sockets are dropped.
   99  *
  100  * If higher level protocols are implemented in
  101  * the kernel, the wakeups done here will sometimes
  102  * cause software-interrupt process scheduling.
  103  */
  104 
  105 void
  106 soisconnecting(so)
  107         register struct socket *so;
  108 {
  109 
  110         so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
  111         so->so_state |= SS_ISCONNECTING;
  112 }
  113 
  114 void
  115 soisconnected(so)
  116         struct socket *so;
  117 {
  118         struct socket *head = so->so_head;
  119 
  120         so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
  121         so->so_state |= SS_ISCONNECTED;
  122         if (head && (so->so_state & SS_INCOMP)) {
  123                 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
  124                         so->so_upcall = head->so_accf->so_accept_filter->accf_callback;
  125                         so->so_upcallarg = head->so_accf->so_accept_filter_arg;
  126                         so->so_rcv.sb_flags |= SB_UPCALL;
  127                         so->so_options &= ~SO_ACCEPTFILTER;
  128                         so->so_upcall(so, so->so_upcallarg, 0);
  129                         return;
  130                 }
  131                 TAILQ_REMOVE(&head->so_incomp, so, so_list);
  132                 head->so_incqlen--;
  133                 so->so_state &= ~SS_INCOMP;
  134                 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
  135                 head->so_qlen++;
  136                 so->so_state |= SS_COMP;
  137                 sorwakeup(head);
  138                 wakeup_one(&head->so_timeo);
  139         } else {
  140                 wakeup(&so->so_timeo);
  141                 sorwakeup(so);
  142                 sowwakeup(so);
  143         }
  144 }
  145 
  146 void
  147 soisdisconnecting(so)
  148         register struct socket *so;
  149 {
  150 
  151         so->so_state &= ~SS_ISCONNECTING;
  152         so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
  153         wakeup(&so->so_timeo);
  154         sowwakeup(so);
  155         sorwakeup(so);
  156 }
  157 
  158 void
  159 soisdisconnected(so)
  160         register struct socket *so;
  161 {
  162 
  163         so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
  164         so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
  165         wakeup(&so->so_timeo);
  166         sbdrop(&so->so_snd, so->so_snd.sb_cc);
  167         sowwakeup(so);
  168         sorwakeup(so);
  169 }
  170 
  171 /*
  172  * When an attempt at a new connection is noted on a socket
  173  * which accepts connections, sonewconn is called.  If the
  174  * connection is possible (subject to space constraints, etc.)
  175  * then we allocate a new structure, propoerly linked into the
  176  * data structure of the original socket, and return this.
  177  * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
  178  *
  179  * note: the ref count on the socket is 0 on return
  180  */
  181 struct socket *
  182 sonewconn(head, connstatus)
  183         register struct socket *head;
  184         int connstatus;
  185 {
  186         register struct socket *so;
  187 
  188         if (head->so_qlen > 3 * head->so_qlimit / 2)
  189                 return ((struct socket *)0);
  190         so = soalloc(0);
  191         if (so == NULL)
  192                 return ((struct socket *)0);
  193         if ((head->so_options & SO_ACCEPTFILTER) != 0)
  194                 connstatus = 0;
  195         so->so_head = head;
  196         so->so_type = head->so_type;
  197         so->so_options = head->so_options &~ SO_ACCEPTCONN;
  198         so->so_linger = head->so_linger;
  199         so->so_state = head->so_state | SS_NOFDREF;
  200         so->so_proto = head->so_proto;
  201         so->so_timeo = head->so_timeo;
  202         so->so_cred = crhold(head->so_cred);
  203 #ifdef MAC
  204         mac_create_socket_from_socket(head, so);
  205 #endif
  206         if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
  207             (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
  208                 sodealloc(so);
  209                 return ((struct socket *)0);
  210         }
  211 
  212         if (connstatus) {
  213                 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
  214                 so->so_state |= SS_COMP;
  215                 head->so_qlen++;
  216         } else {
  217                 if (head->so_incqlen > head->so_qlimit) {
  218                         struct socket *sp;
  219                         sp = TAILQ_FIRST(&head->so_incomp);
  220                         (void) soabort(sp);
  221                 }
  222                 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
  223                 so->so_state |= SS_INCOMP;
  224                 head->so_incqlen++;
  225         }
  226         if (connstatus) {
  227                 sorwakeup(head);
  228                 wakeup(&head->so_timeo);
  229                 so->so_state |= connstatus;
  230         }
  231         return (so);
  232 }
  233 
  234 /*
  235  * Socantsendmore indicates that no more data will be sent on the
  236  * socket; it would normally be applied to a socket when the user
  237  * informs the system that no more data is to be sent, by the protocol
  238  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
  239  * will be received, and will normally be applied to the socket by a
  240  * protocol when it detects that the peer will send no more data.
  241  * Data queued for reading in the socket may yet be read.
  242  */
  243 
  244 void
  245 socantsendmore(so)
  246         struct socket *so;
  247 {
  248 
  249         so->so_state |= SS_CANTSENDMORE;
  250         sowwakeup(so);
  251 }
  252 
  253 void
  254 socantrcvmore(so)
  255         struct socket *so;
  256 {
  257 
  258         so->so_state |= SS_CANTRCVMORE;
  259         sorwakeup(so);
  260 }
  261 
  262 /*
  263  * Wait for data to arrive at/drain from a socket buffer.
  264  */
  265 int
  266 sbwait(sb)
  267         struct sockbuf *sb;
  268 {
  269 
  270         sb->sb_flags |= SB_WAIT;
  271         return (tsleep(&sb->sb_cc,
  272             (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
  273             sb->sb_timeo));
  274 }
  275 
  276 /*
  277  * Lock a sockbuf already known to be locked;
  278  * return any error returned from sleep (EINTR).
  279  */
  280 int
  281 sb_lock(sb)
  282         register struct sockbuf *sb;
  283 {
  284         int error;
  285 
  286         while (sb->sb_flags & SB_LOCK) {
  287                 sb->sb_flags |= SB_WANT;
  288                 error = tsleep(&sb->sb_flags,
  289                     (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH,
  290                     "sblock", 0);
  291                 if (error)
  292                         return (error);
  293         }
  294         sb->sb_flags |= SB_LOCK;
  295         return (0);
  296 }
  297 
  298 /*
  299  * Wakeup processes waiting on a socket buffer.
  300  * Do asynchronous notification via SIGIO
  301  * if the socket has the SS_ASYNC flag set.
  302  */
  303 void
  304 sowakeup(so, sb)
  305         register struct socket *so;
  306         register struct sockbuf *sb;
  307 {
  308 
  309         selwakeup(&sb->sb_sel);
  310         sb->sb_flags &= ~SB_SEL;
  311         if (sb->sb_flags & SB_WAIT) {
  312                 sb->sb_flags &= ~SB_WAIT;
  313                 wakeup(&sb->sb_cc);
  314         }
  315         if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
  316                 pgsigio(&so->so_sigio, SIGIO, 0);
  317         if (sb->sb_flags & SB_UPCALL)
  318                 (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT);
  319         if (sb->sb_flags & SB_AIO)
  320                 aio_swake(so, sb);
  321         KNOTE(&sb->sb_sel.si_note, 0);
  322 }
  323 
  324 /*
  325  * Socket buffer (struct sockbuf) utility routines.
  326  *
  327  * Each socket contains two socket buffers: one for sending data and
  328  * one for receiving data.  Each buffer contains a queue of mbufs,
  329  * information about the number of mbufs and amount of data in the
  330  * queue, and other fields allowing select() statements and notification
  331  * on data availability to be implemented.
  332  *
  333  * Data stored in a socket buffer is maintained as a list of records.
  334  * Each record is a list of mbufs chained together with the m_next
  335  * field.  Records are chained together with the m_nextpkt field. The upper
  336  * level routine soreceive() expects the following conventions to be
  337  * observed when placing information in the receive buffer:
  338  *
  339  * 1. If the protocol requires each message be preceded by the sender's
  340  *    name, then a record containing that name must be present before
  341  *    any associated data (mbuf's must be of type MT_SONAME).
  342  * 2. If the protocol supports the exchange of ``access rights'' (really
  343  *    just additional data associated with the message), and there are
  344  *    ``rights'' to be received, then a record containing this data
  345  *    should be present (mbuf's must be of type MT_RIGHTS).
  346  * 3. If a name or rights record exists, then it must be followed by
  347  *    a data record, perhaps of zero length.
  348  *
  349  * Before using a new socket structure it is first necessary to reserve
  350  * buffer space to the socket, by calling sbreserve().  This should commit
  351  * some of the available buffer space in the system buffer pool for the
  352  * socket (currently, it does nothing but enforce limits).  The space
  353  * should be released by calling sbrelease() when the socket is destroyed.
  354  */
  355 
  356 int
  357 soreserve(so, sndcc, rcvcc)
  358         register struct socket *so;
  359         u_long sndcc, rcvcc;
  360 {
  361         struct thread *td = curthread;
  362 
  363         if (sbreserve(&so->so_snd, sndcc, so, td) == 0)
  364                 goto bad;
  365         if (sbreserve(&so->so_rcv, rcvcc, so, td) == 0)
  366                 goto bad2;
  367         if (so->so_rcv.sb_lowat == 0)
  368                 so->so_rcv.sb_lowat = 1;
  369         if (so->so_snd.sb_lowat == 0)
  370                 so->so_snd.sb_lowat = MCLBYTES;
  371         if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
  372                 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
  373         return (0);
  374 bad2:
  375         sbrelease(&so->so_snd, so);
  376 bad:
  377         return (ENOBUFS);
  378 }
  379 
  380 static int
  381 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
  382 {
  383         int error = 0;
  384         u_long old_sb_max = sb_max;
  385 
  386         error = SYSCTL_OUT(req, arg1, sizeof(int));
  387         if (error || !req->newptr)
  388                 return (error);
  389         error = SYSCTL_IN(req, arg1, sizeof(int));
  390         if (error)
  391                 return (error);
  392         if (sb_max < MSIZE + MCLBYTES) {
  393                 sb_max = old_sb_max;
  394                 return (EINVAL);
  395         }
  396         sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES);
  397         return (0);
  398 }
  399         
  400 /*
  401  * Allot mbufs to a sockbuf.
  402  * Attempt to scale mbmax so that mbcnt doesn't become limiting
  403  * if buffering efficiency is near the normal case.
  404  */
  405 int
  406 sbreserve(sb, cc, so, td)
  407         struct sockbuf *sb;
  408         u_long cc;
  409         struct socket *so;
  410         struct thread *td;
  411 {
  412 
  413         /*
  414          * td will only be NULL when we're in an interrupt
  415          * (e.g. in tcp_input())
  416          */
  417         if (cc > sb_max_adj)
  418                 return (0);
  419         if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
  420             td ? td->td_proc->p_rlimit[RLIMIT_SBSIZE].rlim_cur : RLIM_INFINITY)) {
  421                 return (0);
  422         }
  423         sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
  424         if (sb->sb_lowat > sb->sb_hiwat)
  425                 sb->sb_lowat = sb->sb_hiwat;
  426         return (1);
  427 }
  428 
  429 /*
  430  * Free mbufs held by a socket, and reserved mbuf space.
  431  */
  432 void
  433 sbrelease(sb, so)
  434         struct sockbuf *sb;
  435         struct socket *so;
  436 {
  437 
  438         sbflush(sb);
  439         (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
  440             RLIM_INFINITY);
  441         sb->sb_mbmax = 0;
  442 }
  443 
  444 /*
  445  * Routines to add and remove
  446  * data from an mbuf queue.
  447  *
  448  * The routines sbappend() or sbappendrecord() are normally called to
  449  * append new mbufs to a socket buffer, after checking that adequate
  450  * space is available, comparing the function sbspace() with the amount
  451  * of data to be added.  sbappendrecord() differs from sbappend() in
  452  * that data supplied is treated as the beginning of a new record.
  453  * To place a sender's address, optional access rights, and data in a
  454  * socket receive buffer, sbappendaddr() should be used.  To place
  455  * access rights and data in a socket receive buffer, sbappendrights()
  456  * should be used.  In either case, the new data begins a new record.
  457  * Note that unlike sbappend() and sbappendrecord(), these routines check
  458  * for the caller that there will be enough space to store the data.
  459  * Each fails if there is not enough space, or if it cannot find mbufs
  460  * to store additional information in.
  461  *
  462  * Reliable protocols may use the socket send buffer to hold data
  463  * awaiting acknowledgement.  Data is normally copied from a socket
  464  * send buffer in a protocol with m_copy for output to a peer,
  465  * and then removing the data from the socket buffer with sbdrop()
  466  * or sbdroprecord() when the data is acknowledged by the peer.
  467  */
  468 
  469 /*
  470  * Append mbuf chain m to the last record in the
  471  * socket buffer sb.  The additional space associated
  472  * the mbuf chain is recorded in sb.  Empty mbufs are
  473  * discarded and mbufs are compacted where possible.
  474  */
  475 void
  476 sbappend(sb, m)
  477         struct sockbuf *sb;
  478         struct mbuf *m;
  479 {
  480         register struct mbuf *n;
  481 
  482         if (m == 0)
  483                 return;
  484         n = sb->sb_mb;
  485         if (n) {
  486                 while (n->m_nextpkt)
  487                         n = n->m_nextpkt;
  488                 do {
  489                         if (n->m_flags & M_EOR) {
  490                                 sbappendrecord(sb, m); /* XXXXXX!!!! */
  491                                 return;
  492                         }
  493                 } while (n->m_next && (n = n->m_next));
  494         }
  495         sbcompress(sb, m, n);
  496 }
  497 
  498 #ifdef SOCKBUF_DEBUG
  499 void
  500 sbcheck(sb)
  501         struct sockbuf *sb;
  502 {
  503         struct mbuf *m;
  504         struct mbuf *n = 0;
  505         u_long len = 0, mbcnt = 0;
  506 
  507         for (m = sb->sb_mb; m; m = n) {
  508             n = m->m_nextpkt;
  509             for (; m; m = m->m_next) {
  510                 len += m->m_len;
  511                 mbcnt += MSIZE;
  512                 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
  513                         mbcnt += m->m_ext.ext_size;
  514             }
  515         }
  516         if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
  517                 printf("cc %ld != %ld || mbcnt %ld != %ld\n", len, sb->sb_cc,
  518                     mbcnt, sb->sb_mbcnt);
  519                 panic("sbcheck");
  520         }
  521 }
  522 #endif
  523 
  524 /*
  525  * As above, except the mbuf chain
  526  * begins a new record.
  527  */
  528 void
  529 sbappendrecord(sb, m0)
  530         register struct sockbuf *sb;
  531         register struct mbuf *m0;
  532 {
  533         register struct mbuf *m;
  534 
  535         if (m0 == 0)
  536                 return;
  537         m = sb->sb_mb;
  538         if (m)
  539                 while (m->m_nextpkt)
  540                         m = m->m_nextpkt;
  541         /*
  542          * Put the first mbuf on the queue.
  543          * Note this permits zero length records.
  544          */
  545         sballoc(sb, m0);
  546         if (m)
  547                 m->m_nextpkt = m0;
  548         else
  549                 sb->sb_mb = m0;
  550         m = m0->m_next;
  551         m0->m_next = 0;
  552         if (m && (m0->m_flags & M_EOR)) {
  553                 m0->m_flags &= ~M_EOR;
  554                 m->m_flags |= M_EOR;
  555         }
  556         sbcompress(sb, m, m0);
  557 }
  558 
  559 /*
  560  * As above except that OOB data
  561  * is inserted at the beginning of the sockbuf,
  562  * but after any other OOB data.
  563  */
  564 void
  565 sbinsertoob(sb, m0)
  566         register struct sockbuf *sb;
  567         register struct mbuf *m0;
  568 {
  569         register struct mbuf *m;
  570         register struct mbuf **mp;
  571 
  572         if (m0 == 0)
  573                 return;
  574         for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
  575             m = *mp;
  576             again:
  577                 switch (m->m_type) {
  578 
  579                 case MT_OOBDATA:
  580                         continue;               /* WANT next train */
  581 
  582                 case MT_CONTROL:
  583                         m = m->m_next;
  584                         if (m)
  585                                 goto again;     /* inspect THIS train further */
  586                 }
  587                 break;
  588         }
  589         /*
  590          * Put the first mbuf on the queue.
  591          * Note this permits zero length records.
  592          */
  593         sballoc(sb, m0);
  594         m0->m_nextpkt = *mp;
  595         *mp = m0;
  596         m = m0->m_next;
  597         m0->m_next = 0;
  598         if (m && (m0->m_flags & M_EOR)) {
  599                 m0->m_flags &= ~M_EOR;
  600                 m->m_flags |= M_EOR;
  601         }
  602         sbcompress(sb, m, m0);
  603 }
  604 
  605 /*
  606  * Append address and data, and optionally, control (ancillary) data
  607  * to the receive queue of a socket.  If present,
  608  * m0 must include a packet header with total length.
  609  * Returns 0 if no space in sockbuf or insufficient mbufs.
  610  */
  611 int
  612 sbappendaddr(sb, asa, m0, control)
  613         struct sockbuf *sb;
  614         struct sockaddr *asa;
  615         struct mbuf *m0, *control;
  616 {
  617         struct mbuf *m, *n;
  618         int space = asa->sa_len;
  619 
  620         if (m0 && (m0->m_flags & M_PKTHDR) == 0)
  621                 panic("sbappendaddr");
  622         if (m0)
  623                 space += m0->m_pkthdr.len;
  624         space += m_length(control, &n);
  625         if (space > sbspace(sb))
  626                 return (0);
  627         if (asa->sa_len > MLEN)
  628                 return (0);
  629         MGET(m, M_DONTWAIT, MT_SONAME);
  630         if (m == 0)
  631                 return (0);
  632         m->m_len = asa->sa_len;
  633         bcopy(asa, mtod(m, caddr_t), asa->sa_len);
  634         if (n)
  635                 n->m_next = m0;         /* concatenate data to control */
  636         else
  637                 control = m0;
  638         m->m_next = control;
  639         for (n = m; n; n = n->m_next)
  640                 sballoc(sb, n);
  641         n = sb->sb_mb;
  642         if (n) {
  643                 while (n->m_nextpkt)
  644                         n = n->m_nextpkt;
  645                 n->m_nextpkt = m;
  646         } else
  647                 sb->sb_mb = m;
  648         return (1);
  649 }
  650 
  651 int
  652 sbappendcontrol(sb, m0, control)
  653         struct sockbuf *sb;
  654         struct mbuf *control, *m0;
  655 {
  656         struct mbuf *m, *n;
  657         int space;
  658 
  659         if (control == 0)
  660                 panic("sbappendcontrol");
  661         space = m_length(control, &n) + m_length(m0, NULL);
  662         if (space > sbspace(sb))
  663                 return (0);
  664         n->m_next = m0;                 /* concatenate data to control */
  665         for (m = control; m; m = m->m_next)
  666                 sballoc(sb, m);
  667         n = sb->sb_mb;
  668         if (n) {
  669                 while (n->m_nextpkt)
  670                         n = n->m_nextpkt;
  671                 n->m_nextpkt = control;
  672         } else
  673                 sb->sb_mb = control;
  674         return (1);
  675 }
  676 
  677 /*
  678  * Compress mbuf chain m into the socket
  679  * buffer sb following mbuf n.  If n
  680  * is null, the buffer is presumed empty.
  681  */
  682 void
  683 sbcompress(sb, m, n)
  684         register struct sockbuf *sb;
  685         register struct mbuf *m, *n;
  686 {
  687         register int eor = 0;
  688         register struct mbuf *o;
  689 
  690         while (m) {
  691                 eor |= m->m_flags & M_EOR;
  692                 if (m->m_len == 0 &&
  693                     (eor == 0 ||
  694                      (((o = m->m_next) || (o = n)) &&
  695                       o->m_type == m->m_type))) {
  696                         m = m_free(m);
  697                         continue;
  698                 }
  699                 if (n && (n->m_flags & M_EOR) == 0 &&
  700                     M_WRITABLE(n) &&
  701                     m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
  702                     m->m_len <= M_TRAILINGSPACE(n) &&
  703                     n->m_type == m->m_type) {
  704                         bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
  705                             (unsigned)m->m_len);
  706                         n->m_len += m->m_len;
  707                         sb->sb_cc += m->m_len;
  708                         if (m->m_type != MT_DATA) /* XXX: Probably don't need.*/
  709                                 sb->sb_ctl += m->m_len;
  710                         m = m_free(m);
  711                         continue;
  712                 }
  713                 if (n)
  714                         n->m_next = m;
  715                 else
  716                         sb->sb_mb = m;
  717                 sballoc(sb, m);
  718                 n = m;
  719                 m->m_flags &= ~M_EOR;
  720                 m = m->m_next;
  721                 n->m_next = 0;
  722         }
  723         if (eor) {
  724                 if (n)
  725                         n->m_flags |= eor;
  726                 else
  727                         printf("semi-panic: sbcompress\n");
  728         }
  729 }
  730 
  731 /*
  732  * Free all mbufs in a sockbuf.
  733  * Check that all resources are reclaimed.
  734  */
  735 void
  736 sbflush(sb)
  737         register struct sockbuf *sb;
  738 {
  739 
  740         if (sb->sb_flags & SB_LOCK)
  741                 panic("sbflush: locked");
  742         while (sb->sb_mbcnt) {
  743                 /*
  744                  * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
  745                  * we would loop forever. Panic instead.
  746                  */
  747                 if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len))
  748                         break;
  749                 sbdrop(sb, (int)sb->sb_cc);
  750         }
  751         if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt)
  752                 panic("sbflush: cc %u || mb %p || mbcnt %u", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt);
  753 }
  754 
  755 /*
  756  * Drop data from (the front of) a sockbuf.
  757  */
  758 void
  759 sbdrop(sb, len)
  760         register struct sockbuf *sb;
  761         register int len;
  762 {
  763         register struct mbuf *m;
  764         struct mbuf *next;
  765 
  766         next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
  767         while (len > 0) {
  768                 if (m == 0) {
  769                         if (next == 0)
  770                                 panic("sbdrop");
  771                         m = next;
  772                         next = m->m_nextpkt;
  773                         continue;
  774                 }
  775                 if (m->m_len > len) {
  776                         m->m_len -= len;
  777                         m->m_data += len;
  778                         sb->sb_cc -= len;
  779                         if (m->m_type != MT_DATA)
  780                                 sb->sb_ctl -= len;
  781                         break;
  782                 }
  783                 len -= m->m_len;
  784                 sbfree(sb, m);
  785                 m = m_free(m);
  786         }
  787         while (m && m->m_len == 0) {
  788                 sbfree(sb, m);
  789                 m = m_free(m);
  790         }
  791         if (m) {
  792                 sb->sb_mb = m;
  793                 m->m_nextpkt = next;
  794         } else
  795                 sb->sb_mb = next;
  796 }
  797 
  798 /*
  799  * Drop a record off the front of a sockbuf
  800  * and move the next record to the front.
  801  */
  802 void
  803 sbdroprecord(sb)
  804         register struct sockbuf *sb;
  805 {
  806         register struct mbuf *m;
  807 
  808         m = sb->sb_mb;
  809         if (m) {
  810                 sb->sb_mb = m->m_nextpkt;
  811                 do {
  812                         sbfree(sb, m);
  813                         m = m_free(m);
  814                 } while (m);
  815         }
  816 }
  817 
  818 /*
  819  * Create a "control" mbuf containing the specified data
  820  * with the specified type for presentation on a socket buffer.
  821  */
  822 struct mbuf *
  823 sbcreatecontrol(p, size, type, level)
  824         caddr_t p;
  825         register int size;
  826         int type, level;
  827 {
  828         register struct cmsghdr *cp;
  829         struct mbuf *m;
  830 
  831         if (CMSG_SPACE((u_int)size) > MCLBYTES)
  832                 return ((struct mbuf *) NULL);
  833         if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
  834                 return ((struct mbuf *) NULL);
  835         if (CMSG_SPACE((u_int)size) > MLEN) {
  836                 MCLGET(m, M_DONTWAIT);
  837                 if ((m->m_flags & M_EXT) == 0) {
  838                         m_free(m);
  839                         return ((struct mbuf *) NULL);
  840                 }
  841         }
  842         cp = mtod(m, struct cmsghdr *);
  843         m->m_len = 0;
  844         KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m),
  845             ("sbcreatecontrol: short mbuf"));
  846         if (p != NULL)
  847                 (void)memcpy(CMSG_DATA(cp), p, size);
  848         m->m_len = CMSG_SPACE(size);
  849         cp->cmsg_len = CMSG_LEN(size);
  850         cp->cmsg_level = level;
  851         cp->cmsg_type = type;
  852         return (m);
  853 }
  854 
  855 /*
  856  * Some routines that return EOPNOTSUPP for entry points that are not
  857  * supported by a protocol.  Fill in as needed.
  858  */
  859 int
  860 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
  861 {
  862         return EOPNOTSUPP;
  863 }
  864 
  865 int
  866 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
  867 {
  868         return EOPNOTSUPP;
  869 }
  870 
  871 int
  872 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
  873 {
  874         return EOPNOTSUPP;
  875 }
  876 
  877 int
  878 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
  879                     struct ifnet *ifp, struct thread *td)
  880 {
  881         return EOPNOTSUPP;
  882 }
  883 
  884 int
  885 pru_listen_notsupp(struct socket *so, struct thread *td)
  886 {
  887         return EOPNOTSUPP;
  888 }
  889 
  890 int
  891 pru_rcvd_notsupp(struct socket *so, int flags)
  892 {
  893         return EOPNOTSUPP;
  894 }
  895 
  896 int
  897 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
  898 {
  899         return EOPNOTSUPP;
  900 }
  901 
  902 /*
  903  * This isn't really a ``null'' operation, but it's the default one
  904  * and doesn't do anything destructive.
  905  */
  906 int
  907 pru_sense_null(struct socket *so, struct stat *sb)
  908 {
  909         sb->st_blksize = so->so_snd.sb_hiwat;
  910         return 0;
  911 }
  912 
  913 /*
  914  * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
  915  */
  916 struct sockaddr *
  917 dup_sockaddr(sa, canwait)
  918         struct sockaddr *sa;
  919         int canwait;
  920 {
  921         struct sockaddr *sa2;
  922 
  923         MALLOC(sa2, struct sockaddr *, sa->sa_len, M_SONAME, 
  924                canwait ? M_WAITOK : M_NOWAIT);
  925         if (sa2)
  926                 bcopy(sa, sa2, sa->sa_len);
  927         return sa2;
  928 }
  929 
  930 /*
  931  * Create an external-format (``xsocket'') structure using the information
  932  * in the kernel-format socket structure pointed to by so.  This is done
  933  * to reduce the spew of irrelevant information over this interface,
  934  * to isolate user code from changes in the kernel structure, and
  935  * potentially to provide information-hiding if we decide that
  936  * some of this information should be hidden from users.
  937  */
  938 void
  939 sotoxsocket(struct socket *so, struct xsocket *xso)
  940 {
  941         xso->xso_len = sizeof *xso;
  942         xso->xso_so = so;
  943         xso->so_type = so->so_type;
  944         xso->so_options = so->so_options;
  945         xso->so_linger = so->so_linger;
  946         xso->so_state = so->so_state;
  947         xso->so_pcb = so->so_pcb;
  948         xso->xso_protocol = so->so_proto->pr_protocol;
  949         xso->xso_family = so->so_proto->pr_domain->dom_family;
  950         xso->so_qlen = so->so_qlen;
  951         xso->so_incqlen = so->so_incqlen;
  952         xso->so_qlimit = so->so_qlimit;
  953         xso->so_timeo = so->so_timeo;
  954         xso->so_error = so->so_error;
  955         xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
  956         xso->so_oobmark = so->so_oobmark;
  957         sbtoxsockbuf(&so->so_snd, &xso->so_snd);
  958         sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
  959         xso->so_uid = so->so_cred->cr_uid;
  960 }
  961 
  962 /*
  963  * This does the same for sockbufs.  Note that the xsockbuf structure,
  964  * since it is always embedded in a socket, does not include a self
  965  * pointer nor a length.  We make this entry point public in case
  966  * some other mechanism needs it.
  967  */
  968 void
  969 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
  970 {
  971         xsb->sb_cc = sb->sb_cc;
  972         xsb->sb_hiwat = sb->sb_hiwat;
  973         xsb->sb_mbcnt = sb->sb_mbcnt;
  974         xsb->sb_mbmax = sb->sb_mbmax;
  975         xsb->sb_lowat = sb->sb_lowat;
  976         xsb->sb_flags = sb->sb_flags;
  977         xsb->sb_timeo = sb->sb_timeo;
  978 }
  979 
  980 /*
  981  * Here is the definition of some of the basic objects in the kern.ipc
  982  * branch of the MIB.
  983  */
  984 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
  985 
  986 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
  987 static int dummy;
  988 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
  989 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_INT|CTLFLAG_RW, 
  990     &sb_max, 0, sysctl_handle_sb_max, "I", "Maximum socket buffer size");
  991 SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD, 
  992     &maxsockets, 0, "Maximum number of sockets avaliable");
  993 SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
  994     &sb_efficiency, 0, "");
  995 
  996 /*
  997  * Initialise maxsockets 
  998  */
  999 static void init_maxsockets(void *ignored)
 1000 {
 1001         TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
 1002         maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
 1003 }
 1004 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);

Cache object: a7166084c1c0c734856976a35d89d9e9


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