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/sys/socketvar.h

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 /*      $NetBSD: socketvar.h,v 1.91.2.1 2007/07/09 09:54:09 liamjfoy Exp $      */
    2 
    3 /*-
    4  * Copyright (c) 1982, 1986, 1990, 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. Neither the name of the University nor the names of its contributors
   16  *    may be used to endorse or promote products derived from this software
   17  *    without specific prior written permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  *
   31  *      @(#)socketvar.h 8.3 (Berkeley) 2/19/95
   32  */
   33 
   34 #ifndef _SYS_SOCKETVAR_H_
   35 #define _SYS_SOCKETVAR_H_
   36 
   37 #include <sys/select.h>
   38 #include <sys/selinfo.h>                /* for struct selinfo */
   39 #include <sys/queue.h>
   40 
   41 #if !defined(_KERNEL) || defined(LKM)
   42 struct uio;
   43 struct lwp;
   44 struct uidinfo;
   45 #endif
   46 
   47 TAILQ_HEAD(soqhead, socket);
   48 
   49 /*
   50  * Variables for socket buffering.
   51  */
   52 struct sockbuf {
   53         struct selinfo sb_sel;          /* process selecting read/write */
   54         struct mowner *sb_mowner;       /* who owns data for this sockbuf */
   55         /* When re-zeroing this struct, we zero from sb_startzero to the end */
   56 #define sb_startzero    sb_cc
   57         u_long  sb_cc;                  /* actual chars in buffer */
   58         u_long  sb_hiwat;               /* max actual char count */
   59         u_long  sb_mbcnt;               /* chars of mbufs used */
   60         u_long  sb_mbmax;               /* max chars of mbufs to use */
   61         long    sb_lowat;               /* low water mark */
   62         struct mbuf *sb_mb;             /* the mbuf chain */
   63         struct mbuf *sb_mbtail;         /* the last mbuf in the chain */
   64         struct mbuf *sb_lastrecord;     /* first mbuf of last record in
   65                                            socket buffer */
   66         int     sb_flags;               /* flags, see below */
   67         int     sb_timeo;               /* timeout for read/write */
   68         u_long  sb_overflowed;          /* # of drops due to full buffer */
   69 };
   70 
   71 #ifndef SB_MAX
   72 #define SB_MAX          (256*1024)      /* default for max chars in sockbuf */
   73 #endif
   74 
   75 #define SB_LOCK         0x01            /* lock on data queue */
   76 #define SB_WANT         0x02            /* someone is waiting to lock */
   77 #define SB_WAIT         0x04            /* someone is waiting for data/space */
   78 #define SB_SEL          0x08            /* someone is selecting */
   79 #define SB_ASYNC        0x10            /* ASYNC I/O, need signals */
   80 #define SB_UPCALL       0x20            /* someone wants an upcall */
   81 #define SB_NOINTR       0x40            /* operations not interruptible */
   82         /* XXXLUKEM: 0x80 left for FreeBSD's SB_AIO */
   83 #define SB_KNOTE        0x100           /* kernel note attached */
   84 
   85 /*
   86  * Kernel structure per socket.
   87  * Contains send and receive buffer queues,
   88  * handle on protocol and pointer to protocol
   89  * private data and error information.
   90  */
   91 struct socket {
   92         short           so_type;        /* generic type, see socket.h */
   93         short           so_options;     /* from socket call, see socket.h */
   94         short           so_linger;      /* time to linger while closing */
   95         short           so_state;       /* internal state flags SS_*, below */
   96         void            *so_pcb;        /* protocol control block */
   97         const struct protosw *so_proto; /* protocol handle */
   98 /*
   99  * Variables for connection queueing.
  100  * Socket where accepts occur is so_head in all subsidiary sockets.
  101  * If so_head is 0, socket is not related to an accept.
  102  * For head socket so_q0 queues partially completed connections,
  103  * while so_q is a queue of connections ready to be accepted.
  104  * If a connection is aborted and it has so_head set, then
  105  * it has to be pulled out of either so_q0 or so_q.
  106  * We allow connections to queue up based on current queue lengths
  107  * and limit on number of queued connections for this socket.
  108  */
  109         struct socket   *so_head;       /* back pointer to accept socket */
  110         struct soqhead  *so_onq;        /* queue (q or q0) that we're on */
  111         struct soqhead  so_q0;          /* queue of partial connections */
  112         struct soqhead  so_q;           /* queue of incoming connections */
  113         TAILQ_ENTRY(socket) so_qe;      /* our queue entry (q or q0) */
  114         short           so_q0len;       /* partials on so_q0 */
  115         short           so_qlen;        /* number of connections on so_q */
  116         short           so_qlimit;      /* max number queued connections */
  117         short           so_timeo;       /* connection timeout */
  118         u_short         so_error;       /* error affecting connection */
  119         pid_t           so_pgid;        /* pgid for signals */
  120         u_long          so_oobmark;     /* chars to oob mark */
  121         struct sockbuf  so_snd;         /* send buffer */
  122         struct sockbuf  so_rcv;         /* receive buffer */
  123 
  124         void            *so_internal;   /* Space for svr4 stream data */
  125         void            (*so_upcall) (struct socket *, caddr_t, int);
  126         caddr_t         so_upcallarg;   /* Arg for above */
  127         int             (*so_send) (struct socket *, struct mbuf *,
  128                                         struct uio *, struct mbuf *,
  129                                         struct mbuf *, int, struct lwp *);
  130         int             (*so_receive) (struct socket *,
  131                                         struct mbuf **,
  132                                         struct uio *, struct mbuf **,
  133                                         struct mbuf **, int *);
  134         struct mowner   *so_mowner;     /* who owns mbufs for this socket */
  135         struct uidinfo  *so_uidinfo;    /* who opened the socket */
  136 };
  137 
  138 #define SB_EMPTY_FIXUP(sb)                                              \
  139 do {                                                                    \
  140         if ((sb)->sb_mb == NULL) {                                      \
  141                 (sb)->sb_mbtail = NULL;                                 \
  142                 (sb)->sb_lastrecord = NULL;                             \
  143         }                                                               \
  144 } while (/*CONSTCOND*/0)
  145 
  146 /*
  147  * Socket state bits.
  148  */
  149 #define SS_NOFDREF              0x001   /* no file table ref any more */
  150 #define SS_ISCONNECTED          0x002   /* socket connected to a peer */
  151 #define SS_ISCONNECTING         0x004   /* in process of connecting to peer */
  152 #define SS_ISDISCONNECTING      0x008   /* in process of disconnecting */
  153 #define SS_CANTSENDMORE         0x010   /* can't send more data to peer */
  154 #define SS_CANTRCVMORE          0x020   /* can't receive more data from peer */
  155 #define SS_RCVATMARK            0x040   /* at mark on input */
  156 #define SS_ISDISCONNECTED       0x800   /* socket disconnected from peer */
  157 
  158 #define SS_NBIO                 0x080   /* non-blocking ops */
  159 #define SS_ASYNC                0x100   /* async i/o notify */
  160 #define SS_ISCONFIRMING         0x200   /* deciding to accept connection req */
  161 #define SS_MORETOCOME           0x400   /*
  162                                          * hint from sosend to lower layer;
  163                                          * more data coming
  164                                          */
  165 #define SS_ISAPIPE              0x800   /* socket is implementing a pipe */
  166 
  167 
  168 /*
  169  * Macros for sockets and socket buffering.
  170  */
  171 
  172 /*
  173  * Do we need to notify the other side when I/O is possible?
  174  */
  175 #define sb_notify(sb)   (((sb)->sb_flags & \
  176         (SB_WAIT | SB_SEL | SB_ASYNC | SB_UPCALL | SB_KNOTE)) != 0)
  177 
  178 /*
  179  * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
  180  * This is problematical if the fields are unsigned, as the space might
  181  * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
  182  * overflow and return 0.
  183  */
  184 #define sbspace(sb) \
  185         (lmin((sb)->sb_hiwat - (sb)->sb_cc, (sb)->sb_mbmax - (sb)->sb_mbcnt))
  186 
  187 /* do we have to send all at once on a socket? */
  188 #define sosendallatonce(so) \
  189         ((so)->so_proto->pr_flags & PR_ATOMIC)
  190 
  191 /* can we read something from so? */
  192 #define soreadable(so) \
  193         ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
  194             ((so)->so_state & SS_CANTRCVMORE) || \
  195             (so)->so_qlen || (so)->so_error)
  196 
  197 /* can we write something to so? */
  198 #define sowritable(so) \
  199         ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
  200             (((so)->so_state&SS_ISCONNECTED) || \
  201               ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
  202          ((so)->so_state & SS_CANTSENDMORE) || \
  203          (so)->so_error)
  204 
  205 /* adjust counters in sb reflecting allocation of m */
  206 #define sballoc(sb, m)                                                  \
  207 do {                                                                    \
  208         (sb)->sb_cc += (m)->m_len;                                      \
  209         (sb)->sb_mbcnt += MSIZE;                                        \
  210         if ((m)->m_flags & M_EXT)                                       \
  211                 (sb)->sb_mbcnt += (m)->m_ext.ext_size;                  \
  212 } while (/* CONSTCOND */ 0)
  213 
  214 /* adjust counters in sb reflecting freeing of m */
  215 #define sbfree(sb, m)                                                   \
  216 do {                                                                    \
  217         (sb)->sb_cc -= (m)->m_len;                                      \
  218         (sb)->sb_mbcnt -= MSIZE;                                        \
  219         if ((m)->m_flags & M_EXT)                                       \
  220                 (sb)->sb_mbcnt -= (m)->m_ext.ext_size;                  \
  221 } while (/* CONSTCOND */ 0)
  222 
  223 /*
  224  * Set lock on sockbuf sb; sleep if lock is already held.
  225  * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
  226  * Returns error without lock if sleep is interrupted.
  227  */
  228 #define sblock(sb, wf)                                                  \
  229         ((sb)->sb_flags & SB_LOCK ?                                     \
  230             (((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) :          \
  231             ((sb)->sb_flags |= SB_LOCK, 0))
  232 
  233 /* release lock on sockbuf sb */
  234 #define sbunlock(sb)                                                    \
  235 do {                                                                    \
  236         (sb)->sb_flags &= ~SB_LOCK;                                     \
  237         if ((sb)->sb_flags & SB_WANT) {                                 \
  238                 (sb)->sb_flags &= ~SB_WANT;                             \
  239                 wakeup((caddr_t)&(sb)->sb_flags);                       \
  240         }                                                               \
  241 } while (/* CONSTCOND */ 0)
  242 
  243 #define sorwakeup(so)                                                   \
  244 do {                                                                    \
  245         if (sb_notify(&(so)->so_rcv))                                   \
  246                 sowakeup((so), &(so)->so_rcv, POLL_IN);                         \
  247 } while (/* CONSTCOND */ 0)
  248 
  249 #define sowwakeup(so)                                                   \
  250 do {                                                                    \
  251         if (sb_notify(&(so)->so_snd))                                   \
  252                 sowakeup((so), &(so)->so_snd, POLL_OUT);                \
  253 } while (/* CONSTCOND */ 0)
  254 
  255 #ifdef _KERNEL
  256 extern u_long           sb_max;
  257 extern int              somaxkva;
  258 extern int              sock_loan_thresh;
  259 
  260 /* strings for sleep message: */
  261 extern const char       netio[], netcon[], netcls[];
  262 
  263 extern struct pool      socket_pool;
  264 
  265 struct mbuf;
  266 struct sockaddr;
  267 struct lwp;
  268 struct msghdr;
  269 struct stat;
  270 struct knote;
  271 
  272 /*
  273  * File operations on sockets.
  274  */
  275 int     soo_read(struct file *, off_t *, struct uio *, kauth_cred_t, int);
  276 int     soo_write(struct file *, off_t *, struct uio *, kauth_cred_t, int);
  277 int     soo_fcntl(struct file *, u_int cmd, void *, struct lwp *);
  278 int     soo_ioctl(struct file *, u_long cmd, void *, struct lwp *);
  279 int     soo_poll(struct file *, int, struct lwp *);
  280 int     soo_kqfilter(struct file *, struct knote *);
  281 int     soo_close(struct file *, struct lwp *);
  282 int     soo_stat(struct file *, struct stat *, struct lwp *);
  283 void    sbappend(struct sockbuf *, struct mbuf *);
  284 void    sbappendstream(struct sockbuf *, struct mbuf *);
  285 int     sbappendaddr(struct sockbuf *, const struct sockaddr *, struct mbuf *,
  286             struct mbuf *);
  287 int     sbappendaddrchain(struct sockbuf *, const struct sockaddr *,
  288              struct mbuf *, int);
  289 int     sbappendcontrol(struct sockbuf *, struct mbuf *, struct mbuf *);
  290 void    sbappendrecord(struct sockbuf *, struct mbuf *);
  291 void    sbcheck(struct sockbuf *);
  292 void    sbcompress(struct sockbuf *, struct mbuf *, struct mbuf *);
  293 struct mbuf *
  294         sbcreatecontrol(caddr_t, int, int, int);
  295 void    sbdrop(struct sockbuf *, int);
  296 void    sbdroprecord(struct sockbuf *);
  297 void    sbflush(struct sockbuf *);
  298 void    sbinsertoob(struct sockbuf *, struct mbuf *);
  299 void    sbrelease(struct sockbuf *, struct socket *);
  300 int     sbreserve(struct sockbuf *, u_long, struct socket *);
  301 int     sbwait(struct sockbuf *);
  302 int     sb_lock(struct sockbuf *);
  303 int     sb_max_set(u_long);
  304 void    soinit(void);
  305 int     soabort(struct socket *);
  306 int     soaccept(struct socket *, struct mbuf *);
  307 int     sobind(struct socket *, struct mbuf *, struct lwp *);
  308 void    socantrcvmore(struct socket *);
  309 void    socantsendmore(struct socket *);
  310 int     soclose(struct socket *);
  311 int     soconnect(struct socket *, struct mbuf *, struct lwp *);
  312 int     soconnect2(struct socket *, struct socket *);
  313 int     socreate(int, struct socket **, int, int, struct lwp *);
  314 int     sodisconnect(struct socket *);
  315 void    sofree(struct socket *);
  316 int     sogetopt(struct socket *, int, int, struct mbuf **);
  317 void    sohasoutofband(struct socket *);
  318 void    soisconnected(struct socket *);
  319 void    soisconnecting(struct socket *);
  320 void    soisdisconnected(struct socket *);
  321 void    soisdisconnecting(struct socket *);
  322 int     solisten(struct socket *, int);
  323 struct socket *
  324         sonewconn(struct socket *, int);
  325 void    soqinsque(struct socket *, struct socket *, int);
  326 int     soqremque(struct socket *, int);
  327 int     soreceive(struct socket *, struct mbuf **, struct uio *,
  328             struct mbuf **, struct mbuf **, int *);
  329 int     soreserve(struct socket *, u_long, u_long);
  330 void    sorflush(struct socket *);
  331 int     sosend(struct socket *, struct mbuf *, struct uio *,
  332             struct mbuf *, struct mbuf *, int, struct lwp *);
  333 int     sosetopt(struct socket *, int, int, struct mbuf *);
  334 int     soshutdown(struct socket *, int);
  335 void    sowakeup(struct socket *, struct sockbuf *, int);
  336 int     sockargs(struct mbuf **, const void *, size_t, int);
  337 
  338 int     sendit(struct lwp *, int, struct msghdr *, int, register_t *);
  339 int     recvit(struct lwp *, int, struct msghdr *, caddr_t, register_t *);
  340 
  341 #ifdef SOCKBUF_DEBUG
  342 /*
  343  * SBLASTRECORDCHK: check sb->sb_lastrecord is maintained correctly.
  344  * SBLASTMBUFCHK: check sb->sb_mbtail is maintained correctly.
  345  *
  346  * => panic if the socket buffer is inconsistent.
  347  * => 'where' is used for a panic message.
  348  */
  349 void    sblastrecordchk(struct sockbuf *, const char *);
  350 #define SBLASTRECORDCHK(sb, where)      sblastrecordchk((sb), (where))
  351 
  352 void    sblastmbufchk(struct sockbuf *, const char *);
  353 #define SBLASTMBUFCHK(sb, where)        sblastmbufchk((sb), (where))
  354 #else
  355 #define SBLASTRECORDCHK(sb, where)      /* nothing */
  356 #define SBLASTMBUFCHK(sb, where)        /* nothing */
  357 #endif /* SOCKBUF_DEBUG */
  358 
  359 /* sosend loan */
  360 vaddr_t sokvaalloc(vsize_t, struct socket *);
  361 void    sokvafree(vaddr_t, vsize_t);
  362 void    soloanfree(struct mbuf *, caddr_t, size_t, void *);
  363 
  364 /*
  365  * Values for socket-buffer-append priority argument to sbappendaddrchain().
  366  * The following flags are reserved for future implementation:
  367  *
  368  *  SB_PRIO_NONE:  honour normal socket-buffer limits.
  369  *
  370  *  SB_PRIO_ONESHOT_OVERFLOW:  if the socket has any space,
  371  *      deliver the entire chain. Intended for large requests
  372  *      that should be delivered in their entirety, or not at all.
  373  *
  374  * SB_PRIO_OVERDRAFT:  allow a small (2*MLEN) overflow, over and
  375  *      aboce normal socket limits. Intended messages indicating
  376  *      buffer overflow in earlier normal/lower-priority messages .
  377  *
  378  * SB_PRIO_BESTEFFORT: Ignore  limits entirely.  Intended only for
  379  *      kernel-generated messages to specially-marked scokets which
  380  *      require "reliable" delivery, nd where the source socket/protocol
  381  *      message generator enforce some hard limit (but possibly well
  382  *      above kern.sbmax). It is entirely up to the in-kernel source to
  383  *      avoid complete mbuf exhaustion or DoS scenarios.
  384  */
  385 #define SB_PRIO_NONE            0
  386 #define SB_PRIO_ONESHOT_OVERFLOW 1
  387 #define SB_PRIO_OVERDRAFT       2
  388 #define SB_PRIO_BESTEFFORT      3
  389 
  390 #endif /* _KERNEL */
  391 
  392 #endif /* !_SYS_SOCKETVAR_H_ */

Cache object: 9be6acd1ae9319580d811da52e442a3a


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