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

Cache object: 266b013a6f94b997ec10ecd8088eb34d


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