[BACK]Return to socketvar.h CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / sys

Annotation of src/sys/sys/socketvar.h, Revision 1.94

1.94    ! yamt        1: /*     $NetBSD$        */
1.11      cgd         2:
1.1       cgd         3: /*-
1.8       mycroft     4:  * Copyright (c) 1982, 1986, 1990, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd         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.
1.65      agc        15:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        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:  *
1.28      fvdl       31:  *     @(#)socketvar.h 8.3 (Berkeley) 2/19/95
1.1       cgd        32:  */
                     33:
1.24      mikel      34: #ifndef _SYS_SOCKETVAR_H_
1.42      lukem      35: #define        _SYS_SOCKETVAR_H_
1.24      mikel      36:
1.81      kleink     37: #include <sys/select.h>
                     38: #include <sys/selinfo.h>               /* for struct selinfo */
1.27      thorpej    39: #include <sys/queue.h>
                     40:
1.38      simonb     41: #if !defined(_KERNEL) || defined(LKM)
1.29      matt       42: struct uio;
1.85      christos   43: struct lwp;
1.82      christos   44: struct uidinfo;
1.29      matt       45: #endif
                     46:
1.27      thorpej    47: TAILQ_HEAD(soqhead, socket);
1.3       mycroft    48:
1.1       cgd        49: /*
1.58      matt       50:  * Variables for socket buffering.
                     51:  */
                     52: struct sockbuf {
1.66      wrstuden   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
1.58      matt       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 */
1.79      yamt       66:        int     sb_flags;               /* flags, see below */
                     67:        int     sb_timeo;               /* timeout for read/write */
1.80      darrenr    68:        u_long  sb_overflowed;          /* # of drops due to full buffer */
1.58      matt       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: /*
1.1       cgd        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 {
1.42      lukem      92:        short           so_type;        /* generic type, see socket.h */
                     93:        short           so_options;     /* from socket call, see socket.h */
1.92      christos   94:        u_short         so_linger;      /* time to linger while closing */
1.42      lukem      95:        short           so_state;       /* internal state flags SS_*, below */
                     96:        void            *so_pcb;        /* protocol control block */
1.74      matt       97:        const struct protosw *so_proto; /* protocol handle */
1.1       cgd        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:  */
1.42      lukem     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 */
1.27      thorpej   113:        TAILQ_ENTRY(socket) so_qe;      /* our queue entry (q or q0) */
1.42      lukem     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 */
1.58      matt      121:        struct sockbuf  so_snd;         /* send buffer */
                    122:        struct sockbuf  so_rcv;         /* receive buffer */
1.1       cgd       123:
1.42      lukem     124:        void            *so_internal;   /* Space for svr4 stream data */
1.93      christos  125:        void            (*so_upcall) (struct socket *, void *, int);
                    126:        void *          so_upcallarg;   /* Arg for above */
1.71      matt      127:        int             (*so_send) (struct socket *, struct mbuf *,
1.64      itojun    128:                                        struct uio *, struct mbuf *,
1.83      christos  129:                                        struct mbuf *, int, struct lwp *);
1.71      matt      130:        int             (*so_receive) (struct socket *,
1.64      itojun    131:                                        struct mbuf **,
                    132:                                        struct uio *, struct mbuf **,
1.71      matt      133:                                        struct mbuf **, int *);
1.58      matt      134:        struct mowner   *so_mowner;     /* who owns mbufs for this socket */
1.82      christos  135:        struct uidinfo  *so_uidinfo;    /* who opened the socket */
1.1       cgd       136: };
                    137:
1.54      thorpej   138: #define        SB_EMPTY_FIXUP(sb)                                              \
1.52      thorpej   139: do {                                                                   \
                    140:        if ((sb)->sb_mb == NULL) {                                      \
                    141:                (sb)->sb_mbtail = NULL;                                 \
                    142:                (sb)->sb_lastrecord = NULL;                             \
                    143:        }                                                               \
                    144: } while (/*CONSTCOND*/0)
                    145:
1.1       cgd       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 */
1.33      mycroft   156: #define        SS_ISDISCONNECTED       0x800   /* socket disconnected from peer */
1.1       cgd       157:
1.19      mycroft   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 */
1.42      lukem     161: #define        SS_MORETOCOME           0x400   /*
                    162:                                         * hint from sosend to lower layer;
                    163:                                         * more data coming
                    164:                                         */
1.94    ! yamt      165: #define        SS_ISAPIPE              0x1000  /* socket is implementing a pipe */
1.1       cgd       166:
                    167:
                    168: /*
                    169:  * Macros for sockets and socket buffering.
                    170:  */
1.15      mycroft   171:
                    172: /*
                    173:  * Do we need to notify the other side when I/O is possible?
                    174:  */
1.42      lukem     175: #define        sb_notify(sb)   (((sb)->sb_flags & \
1.55      jdolecek  176:        (SB_WAIT | SB_SEL | SB_ASYNC | SB_UPCALL | SB_KNOTE)) != 0)
1.1       cgd       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
1.50      jdolecek  182:  * overflow and return 0.
1.1       cgd       183:  */
                    184: #define        sbspace(sb) \
1.50      jdolecek  185:        (lmin((sb)->sb_hiwat - (sb)->sb_cc, (sb)->sb_mbmax - (sb)->sb_mbcnt))
1.1       cgd       186:
                    187: /* do we have to send all at once on a socket? */
                    188: #define        sosendallatonce(so) \
1.42      lukem     189:        ((so)->so_proto->pr_flags & PR_ATOMIC)
1.1       cgd       190:
                    191: /* can we read something from so? */
                    192: #define        soreadable(so) \
1.42      lukem     193:        ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
                    194:            ((so)->so_state & SS_CANTRCVMORE) || \
                    195:            (so)->so_qlen || (so)->so_error)
1.1       cgd       196:
                    197: /* can we write something to so? */
1.56      wiz       198: #define        sowritable(so) \
1.42      lukem     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)
1.1       cgd       204:
                    205: /* adjust counters in sb reflecting allocation of m */
1.42      lukem     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;                  \
1.45      lukem     212: } while (/* CONSTCOND */ 0)
1.1       cgd       213:
                    214: /* adjust counters in sb reflecting freeing of m */
1.42      lukem     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;                  \
1.45      lukem     221: } while (/* CONSTCOND */ 0)
1.1       cgd       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:  */
1.42      lukem     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)
1.1       cgd       232:
                    233: /* release lock on sockbuf sb */
1.42      lukem     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;                             \
1.93      christos  239:                wakeup((void *)&(sb)->sb_flags);                        \
1.42      lukem     240:        }                                                               \
1.45      lukem     241: } while (/* CONSTCOND */ 0)
1.42      lukem     242:
                    243: #define        sorwakeup(so)                                                   \
                    244: do {                                                                   \
                    245:        if (sb_notify(&(so)->so_rcv))                                   \
1.67      christos  246:                sowakeup((so), &(so)->so_rcv, POLL_IN);                         \
1.45      lukem     247: } while (/* CONSTCOND */ 0)
1.42      lukem     248:
                    249: #define        sowwakeup(so)                                                   \
                    250: do {                                                                   \
                    251:        if (sb_notify(&(so)->so_snd))                                   \
1.67      christos  252:                sowakeup((so), &(so)->so_snd, POLL_OUT);                \
1.45      lukem     253: } while (/* CONSTCOND */ 0)
1.1       cgd       254:
1.14      jtc       255: #ifdef _KERNEL
1.42      lukem     256: extern u_long          sb_max;
1.63      ragge     257: extern int             somaxkva;
1.88      yamt      258: extern int             sock_loan_thresh;
1.1       cgd       259:
                    260: /* strings for sleep message: */
1.42      lukem     261: extern const char      netio[], netcon[], netcls[];
1.1       cgd       262:
1.42      lukem     263: extern struct pool     socket_pool;
1.31      thorpej   264:
1.17      christos  265: struct mbuf;
                    266: struct sockaddr;
1.84      christos  267: struct lwp;
1.17      christos  268: struct msghdr;
                    269: struct stat;
1.55      jdolecek  270: struct knote;
1.17      christos  271:
1.1       cgd       272: /*
                    273:  * File operations on sockets.
                    274:  */
1.87      elad      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);
1.83      christos  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 *);
1.64      itojun    280: int    soo_kqfilter(struct file *, struct knote *);
1.83      christos  281: int    soo_close(struct file *, struct lwp *);
                    282: int    soo_stat(struct file *, struct stat *, struct lwp *);
1.64      itojun    283: void   sbappend(struct sockbuf *, struct mbuf *);
                    284: void   sbappendstream(struct sockbuf *, struct mbuf *);
1.73      matt      285: int    sbappendaddr(struct sockbuf *, const struct sockaddr *, struct mbuf *,
1.64      itojun    286:            struct mbuf *);
1.76      jonathan  287: int    sbappendaddrchain(struct sockbuf *, const struct sockaddr *,
                    288:             struct mbuf *, int);
1.64      itojun    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 *);
1.23      thorpej   293: struct mbuf *
1.93      christos  294:        sbcreatecontrol(void *, int, int, int);
1.64      itojun    295: void   sbdrop(struct sockbuf *, int);
                    296: void   sbdroprecord(struct sockbuf *);
                    297: void   sbflush(struct sockbuf *);
                    298: void   sbinsertoob(struct sockbuf *, struct mbuf *);
1.70      christos  299: void   sbrelease(struct sockbuf *, struct socket *);
                    300: int    sbreserve(struct sockbuf *, u_long, struct socket *);
1.64      itojun    301: int    sbwait(struct sockbuf *);
                    302: int    sb_lock(struct sockbuf *);
1.68      thorpej   303: int    sb_max_set(u_long);
1.42      lukem     304: void   soinit(void);
1.64      itojun    305: int    soabort(struct socket *);
                    306: int    soaccept(struct socket *, struct mbuf *);
1.83      christos  307: int    sobind(struct socket *, struct mbuf *, struct lwp *);
1.64      itojun    308: void   socantrcvmore(struct socket *);
                    309: void   socantsendmore(struct socket *);
                    310: int    soclose(struct socket *);
1.83      christos  311: int    soconnect(struct socket *, struct mbuf *, struct lwp *);
1.64      itojun    312: int    soconnect2(struct socket *, struct socket *);
1.83      christos  313: int    socreate(int, struct socket **, int, int, struct lwp *);
1.64      itojun    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);
1.7       mycroft   323: struct socket *
1.89      plunky    324:        sonewconn(struct socket *, int);
1.64      itojun    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 *,
1.83      christos  332:            struct mbuf *, struct mbuf *, int, struct lwp *);
1.64      itojun    333: int    sosetopt(struct socket *, int, int, struct mbuf *);
                    334: int    soshutdown(struct socket *, int);
1.67      christos  335: void   sowakeup(struct socket *, struct sockbuf *, int);
1.49      matt      336: int    sockargs(struct mbuf **, const void *, size_t, int);
1.42      lukem     337:
1.83      christos  338: int    sendit(struct lwp *, int, struct msghdr *, int, register_t *);
1.93      christos  339: int    recvit(struct lwp *, int, struct msghdr *, void *, register_t *);
1.52      thorpej   340:
                    341: #ifdef SOCKBUF_DEBUG
1.78      yamt      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:  */
1.52      thorpej   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 */
1.60      yamt      358:
                    359: /* sosend loan */
                    360: vaddr_t        sokvaalloc(vsize_t, struct socket *);
                    361: void   sokvafree(vaddr_t, vsize_t);
1.93      christos  362: void   soloanfree(struct mbuf *, void *, size_t, void *);
1.17      christos  363:
1.76      jonathan  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:
1.14      jtc       390: #endif /* _KERNEL */
1.24      mikel     391:
                    392: #endif /* !_SYS_SOCKETVAR_H_ */

CVSweb <webmaster@jp.NetBSD.org>