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

1.81    ! kleink      1: /*     $NetBSD: socketvar.h,v 1.80 2004/09/03 18:14:09 darrenr Exp $   */
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.77      he         43: struct proc;
1.29      matt       44: #endif
                     45:
1.27      thorpej    46: TAILQ_HEAD(soqhead, socket);
1.3       mycroft    47:
1.1       cgd        48: /*
1.58      matt       49:  * Variables for socket buffering.
                     50:  */
                     51: struct sockbuf {
1.66      wrstuden   52:        struct selinfo sb_sel;          /* process selecting read/write */
                     53:        struct mowner *sb_mowner;       /* who owns data for this sockbuf */
                     54:        /* When re-zeroing this struct, we zero from sb_startzero to the end */
                     55: #define        sb_startzero    sb_cc
1.58      matt       56:        u_long  sb_cc;                  /* actual chars in buffer */
                     57:        u_long  sb_hiwat;               /* max actual char count */
                     58:        u_long  sb_mbcnt;               /* chars of mbufs used */
                     59:        u_long  sb_mbmax;               /* max chars of mbufs to use */
                     60:        long    sb_lowat;               /* low water mark */
                     61:        struct mbuf *sb_mb;             /* the mbuf chain */
                     62:        struct mbuf *sb_mbtail;         /* the last mbuf in the chain */
                     63:        struct mbuf *sb_lastrecord;     /* first mbuf of last record in
                     64:                                           socket buffer */
1.79      yamt       65:        int     sb_flags;               /* flags, see below */
                     66:        int     sb_timeo;               /* timeout for read/write */
1.80      darrenr    67:        u_long  sb_overflowed;          /* # of drops due to full buffer */
1.58      matt       68: };
                     69:
                     70: #ifndef SB_MAX
                     71: #define        SB_MAX          (256*1024)      /* default for max chars in sockbuf */
                     72: #endif
                     73:
                     74: #define        SB_LOCK         0x01            /* lock on data queue */
                     75: #define        SB_WANT         0x02            /* someone is waiting to lock */
                     76: #define        SB_WAIT         0x04            /* someone is waiting for data/space */
                     77: #define        SB_SEL          0x08            /* someone is selecting */
                     78: #define        SB_ASYNC        0x10            /* ASYNC I/O, need signals */
                     79: #define        SB_UPCALL       0x20            /* someone wants an upcall */
                     80: #define        SB_NOINTR       0x40            /* operations not interruptible */
                     81:        /* XXXLUKEM: 0x80 left for FreeBSD's SB_AIO */
                     82: #define        SB_KNOTE        0x100           /* kernel note attached */
                     83:
                     84: /*
1.1       cgd        85:  * Kernel structure per socket.
                     86:  * Contains send and receive buffer queues,
                     87:  * handle on protocol and pointer to protocol
                     88:  * private data and error information.
                     89:  */
                     90: struct socket {
1.42      lukem      91:        short           so_type;        /* generic type, see socket.h */
                     92:        short           so_options;     /* from socket call, see socket.h */
                     93:        short           so_linger;      /* time to linger while closing */
                     94:        short           so_state;       /* internal state flags SS_*, below */
                     95:        void            *so_pcb;        /* protocol control block */
1.74      matt       96:        const struct protosw *so_proto; /* protocol handle */
1.1       cgd        97: /*
                     98:  * Variables for connection queueing.
                     99:  * Socket where accepts occur is so_head in all subsidiary sockets.
                    100:  * If so_head is 0, socket is not related to an accept.
                    101:  * For head socket so_q0 queues partially completed connections,
                    102:  * while so_q is a queue of connections ready to be accepted.
                    103:  * If a connection is aborted and it has so_head set, then
                    104:  * it has to be pulled out of either so_q0 or so_q.
                    105:  * We allow connections to queue up based on current queue lengths
                    106:  * and limit on number of queued connections for this socket.
                    107:  */
1.42      lukem     108:        struct socket   *so_head;       /* back pointer to accept socket */
                    109:        struct soqhead  *so_onq;        /* queue (q or q0) that we're on */
                    110:        struct soqhead  so_q0;          /* queue of partial connections */
                    111:        struct soqhead  so_q;           /* queue of incoming connections */
1.27      thorpej   112:        TAILQ_ENTRY(socket) so_qe;      /* our queue entry (q or q0) */
1.42      lukem     113:        short           so_q0len;       /* partials on so_q0 */
                    114:        short           so_qlen;        /* number of connections on so_q */
                    115:        short           so_qlimit;      /* max number queued connections */
                    116:        short           so_timeo;       /* connection timeout */
                    117:        u_short         so_error;       /* error affecting connection */
                    118:        pid_t           so_pgid;        /* pgid for signals */
                    119:        u_long          so_oobmark;     /* chars to oob mark */
1.58      matt      120:        struct sockbuf  so_snd;         /* send buffer */
                    121:        struct sockbuf  so_rcv;         /* receive buffer */
1.1       cgd       122:
1.42      lukem     123:        void            *so_internal;   /* Space for svr4 stream data */
1.71      matt      124:        void            (*so_upcall) (struct socket *, caddr_t, int);
1.42      lukem     125:        caddr_t         so_upcallarg;   /* Arg for above */
1.71      matt      126:        int             (*so_send) (struct socket *, struct mbuf *,
1.64      itojun    127:                                        struct uio *, struct mbuf *,
1.75      jonathan  128:                                        struct mbuf *, int, struct proc *);
1.71      matt      129:        int             (*so_receive) (struct socket *,
1.64      itojun    130:                                        struct mbuf **,
                    131:                                        struct uio *, struct mbuf **,
1.71      matt      132:                                        struct mbuf **, int *);
1.58      matt      133:        struct mowner   *so_mowner;     /* who owns mbufs for this socket */
1.42      lukem     134:        uid_t           so_uid;         /* who opened the socket */
1.1       cgd       135: };
                    136:
1.54      thorpej   137: #define        SB_EMPTY_FIXUP(sb)                                              \
1.52      thorpej   138: do {                                                                   \
                    139:        if ((sb)->sb_mb == NULL) {                                      \
                    140:                (sb)->sb_mbtail = NULL;                                 \
                    141:                (sb)->sb_lastrecord = NULL;                             \
                    142:        }                                                               \
                    143: } while (/*CONSTCOND*/0)
                    144:
1.1       cgd       145: /*
                    146:  * Socket state bits.
                    147:  */
                    148: #define        SS_NOFDREF              0x001   /* no file table ref any more */
                    149: #define        SS_ISCONNECTED          0x002   /* socket connected to a peer */
                    150: #define        SS_ISCONNECTING         0x004   /* in process of connecting to peer */
                    151: #define        SS_ISDISCONNECTING      0x008   /* in process of disconnecting */
                    152: #define        SS_CANTSENDMORE         0x010   /* can't send more data to peer */
                    153: #define        SS_CANTRCVMORE          0x020   /* can't receive more data from peer */
                    154: #define        SS_RCVATMARK            0x040   /* at mark on input */
1.33      mycroft   155: #define        SS_ISDISCONNECTED       0x800   /* socket disconnected from peer */
1.1       cgd       156:
1.19      mycroft   157: #define        SS_NBIO                 0x080   /* non-blocking ops */
                    158: #define        SS_ASYNC                0x100   /* async i/o notify */
                    159: #define        SS_ISCONFIRMING         0x200   /* deciding to accept connection req */
1.42      lukem     160: #define        SS_MORETOCOME           0x400   /*
                    161:                                         * hint from sosend to lower layer;
                    162:                                         * more data coming
                    163:                                         */
1.46      manu      164: #define        SS_ISAPIPE              0x800 /* socket is implementing a pipe */
1.1       cgd       165:
                    166:
                    167: /*
                    168:  * Macros for sockets and socket buffering.
                    169:  */
1.15      mycroft   170:
                    171: /*
                    172:  * Do we need to notify the other side when I/O is possible?
                    173:  */
1.42      lukem     174: #define        sb_notify(sb)   (((sb)->sb_flags & \
1.55      jdolecek  175:        (SB_WAIT | SB_SEL | SB_ASYNC | SB_UPCALL | SB_KNOTE)) != 0)
1.1       cgd       176:
                    177: /*
                    178:  * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
                    179:  * This is problematical if the fields are unsigned, as the space might
                    180:  * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
1.50      jdolecek  181:  * overflow and return 0.
1.1       cgd       182:  */
                    183: #define        sbspace(sb) \
1.50      jdolecek  184:        (lmin((sb)->sb_hiwat - (sb)->sb_cc, (sb)->sb_mbmax - (sb)->sb_mbcnt))
1.1       cgd       185:
                    186: /* do we have to send all at once on a socket? */
                    187: #define        sosendallatonce(so) \
1.42      lukem     188:        ((so)->so_proto->pr_flags & PR_ATOMIC)
1.1       cgd       189:
                    190: /* can we read something from so? */
                    191: #define        soreadable(so) \
1.42      lukem     192:        ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
                    193:            ((so)->so_state & SS_CANTRCVMORE) || \
                    194:            (so)->so_qlen || (so)->so_error)
1.1       cgd       195:
                    196: /* can we write something to so? */
1.56      wiz       197: #define        sowritable(so) \
1.42      lukem     198:        ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
                    199:            (((so)->so_state&SS_ISCONNECTED) || \
                    200:              ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
                    201:         ((so)->so_state & SS_CANTSENDMORE) || \
                    202:         (so)->so_error)
1.1       cgd       203:
                    204: /* adjust counters in sb reflecting allocation of m */
1.42      lukem     205: #define        sballoc(sb, m)                                                  \
                    206: do {                                                                   \
                    207:        (sb)->sb_cc += (m)->m_len;                                      \
                    208:        (sb)->sb_mbcnt += MSIZE;                                        \
                    209:        if ((m)->m_flags & M_EXT)                                       \
                    210:                (sb)->sb_mbcnt += (m)->m_ext.ext_size;                  \
1.45      lukem     211: } while (/* CONSTCOND */ 0)
1.1       cgd       212:
                    213: /* adjust counters in sb reflecting freeing of m */
1.42      lukem     214: #define        sbfree(sb, m)                                                   \
                    215: do {                                                                   \
                    216:        (sb)->sb_cc -= (m)->m_len;                                      \
                    217:        (sb)->sb_mbcnt -= MSIZE;                                        \
                    218:        if ((m)->m_flags & M_EXT)                                       \
                    219:                (sb)->sb_mbcnt -= (m)->m_ext.ext_size;                  \
1.45      lukem     220: } while (/* CONSTCOND */ 0)
1.1       cgd       221:
                    222: /*
                    223:  * Set lock on sockbuf sb; sleep if lock is already held.
                    224:  * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
                    225:  * Returns error without lock if sleep is interrupted.
                    226:  */
1.42      lukem     227: #define        sblock(sb, wf)                                                  \
                    228:        ((sb)->sb_flags & SB_LOCK ?                                     \
                    229:            (((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) :          \
                    230:            ((sb)->sb_flags |= SB_LOCK), 0)
1.1       cgd       231:
                    232: /* release lock on sockbuf sb */
1.42      lukem     233: #define        sbunlock(sb)                                                    \
                    234: do {                                                                   \
                    235:        (sb)->sb_flags &= ~SB_LOCK;                                     \
                    236:        if ((sb)->sb_flags & SB_WANT) {                                 \
                    237:                (sb)->sb_flags &= ~SB_WANT;                             \
                    238:                wakeup((caddr_t)&(sb)->sb_flags);                       \
                    239:        }                                                               \
1.45      lukem     240: } while (/* CONSTCOND */ 0)
1.42      lukem     241:
                    242: #define        sorwakeup(so)                                                   \
                    243: do {                                                                   \
                    244:        if (sb_notify(&(so)->so_rcv))                                   \
1.67      christos  245:                sowakeup((so), &(so)->so_rcv, POLL_IN);                         \
1.45      lukem     246: } while (/* CONSTCOND */ 0)
1.42      lukem     247:
                    248: #define        sowwakeup(so)                                                   \
                    249: do {                                                                   \
                    250:        if (sb_notify(&(so)->so_snd))                                   \
1.67      christos  251:                sowakeup((so), &(so)->so_snd, POLL_OUT);                \
1.45      lukem     252: } while (/* CONSTCOND */ 0)
1.1       cgd       253:
1.14      jtc       254: #ifdef _KERNEL
1.42      lukem     255: extern u_long          sb_max;
1.63      ragge     256: extern int             somaxkva;
1.1       cgd       257: /* to catch callers missing new second argument to sonewconn: */
                    258: #define        sonewconn(head, connstatus)     sonewconn1((head), (connstatus))
                    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;
                    267: struct proc;
                    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.64      itojun    275: int    soo_read(struct file *, off_t *, struct uio *, struct ucred *, int);
                    276: int    soo_write(struct file *, off_t *, struct uio *, struct ucred *, int);
                    277: int    soo_fcntl(struct file *, u_int cmd, void *, struct proc *);
                    278: int    soo_ioctl(struct file *, u_long cmd, void *, struct proc *);
                    279: int    soo_poll(struct file *, int, struct proc *);
                    280: int    soo_kqfilter(struct file *, struct knote *);
                    281: int    soo_close(struct file *, struct proc *);
                    282: int    soo_stat(struct file *, struct stat *, struct proc *);
                    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.64      itojun    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 *);
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 *);
                    307: int    sobind(struct socket *, struct mbuf *, struct proc *);
                    308: void   socantrcvmore(struct socket *);
                    309: void   socantsendmore(struct socket *);
                    310: int    soclose(struct socket *);
1.75      jonathan  311: int    soconnect(struct socket *, struct mbuf *, struct proc *);
1.64      itojun    312: int    soconnect2(struct socket *, struct socket *);
1.75      jonathan  313: int    socreate(int, struct socket **, int, int, struct proc *);
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.64      itojun    324:        sonewconn1(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 *,
1.75      jonathan  332:            struct mbuf *, struct mbuf *, int, struct proc *);
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.62      fvdl      338: int    sendit(struct proc *, int, struct msghdr *, int, register_t *);
                    339: int    recvit(struct proc *, int, struct msghdr *, caddr_t, 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);
                    362: void   soloanfree(struct mbuf *, caddr_t, 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>