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/sockbuf.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 /*-
    2  * SPDX-License-Identifier: BSD-3-Clause
    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  * $FreeBSD: releng/12.0/sys/sys/sockbuf.h 337328 2018-08-04 20:26:54Z markj $
   34  */
   35 #ifndef _SYS_SOCKBUF_H_
   36 #define _SYS_SOCKBUF_H_
   37 
   38 /*
   39  * Constants for sb_flags field of struct sockbuf/xsockbuf.
   40  */
   41 #define SB_WAIT         0x04            /* someone is waiting for data/space */
   42 #define SB_SEL          0x08            /* someone is selecting */
   43 #define SB_ASYNC        0x10            /* ASYNC I/O, need signals */
   44 #define SB_UPCALL       0x20            /* someone wants an upcall */
   45 #define SB_NOINTR       0x40            /* operations not interruptible */
   46 #define SB_AIO          0x80            /* AIO operations queued */
   47 #define SB_KNOTE        0x100           /* kernel note attached */
   48 #define SB_NOCOALESCE   0x200           /* don't coalesce new data into existing mbufs */
   49 #define SB_IN_TOE       0x400           /* socket buffer is in the middle of an operation */
   50 #define SB_AUTOSIZE     0x800           /* automatically size socket buffer */
   51 #define SB_STOP         0x1000          /* backpressure indicator */
   52 #define SB_AIO_RUNNING  0x2000          /* AIO operation running */
   53 
   54 #define SBS_CANTSENDMORE        0x0010  /* can't send more data to peer */
   55 #define SBS_CANTRCVMORE         0x0020  /* can't receive more data from peer */
   56 #define SBS_RCVATMARK           0x0040  /* at mark on input */
   57 
   58 #if defined(_KERNEL) || defined(_WANT_SOCKET)
   59 #include <sys/_lock.h>
   60 #include <sys/_mutex.h>
   61 #include <sys/_sx.h>
   62 #include <sys/_task.h>
   63 
   64 #define SB_MAX          (2*1024*1024)   /* default for max chars in sockbuf */
   65 
   66 struct mbuf;
   67 struct sockaddr;
   68 struct socket;
   69 struct thread;
   70 struct selinfo;
   71 
   72 /*
   73  * Variables for socket buffering.
   74  *
   75  * Locking key to struct sockbuf:
   76  * (a) locked by SOCKBUF_LOCK().
   77  */
   78 struct  sockbuf {
   79         struct  mtx sb_mtx;             /* sockbuf lock */
   80         struct  sx sb_sx;               /* prevent I/O interlacing */
   81         struct  selinfo *sb_sel;        /* process selecting read/write */
   82         short   sb_state;       /* (a) socket state on sockbuf */
   83 #define sb_startzero    sb_mb
   84         struct  mbuf *sb_mb;    /* (a) the mbuf chain */
   85         struct  mbuf *sb_mbtail; /* (a) the last mbuf in the chain */
   86         struct  mbuf *sb_lastrecord;    /* (a) first mbuf of last
   87                                          * record in socket buffer */
   88         struct  mbuf *sb_sndptr; /* (a) pointer into mbuf chain */
   89         struct  mbuf *sb_fnrdy; /* (a) pointer to first not ready buffer */
   90         u_int   sb_sndptroff;   /* (a) byte offset of ptr into chain */
   91         u_int   sb_acc;         /* (a) available chars in buffer */
   92         u_int   sb_ccc;         /* (a) claimed chars in buffer */
   93         u_int   sb_hiwat;       /* (a) max actual char count */
   94         u_int   sb_mbcnt;       /* (a) chars of mbufs used */
   95         u_int   sb_mcnt;        /* (a) number of mbufs in buffer */
   96         u_int   sb_ccnt;        /* (a) number of clusters in buffer */
   97         u_int   sb_mbmax;       /* (a) max chars of mbufs to use */
   98         u_int   sb_ctl;         /* (a) non-data chars in buffer */
   99         int     sb_lowat;       /* (a) low water mark */
  100         sbintime_t      sb_timeo;       /* (a) timeout for read/write */
  101         short   sb_flags;       /* (a) flags, see below */
  102         int     (*sb_upcall)(struct socket *, void *, int); /* (a) */
  103         void    *sb_upcallarg;  /* (a) */
  104         TAILQ_HEAD(, kaiocb) sb_aiojobq; /* (a) pending AIO ops */
  105         struct  task sb_aiotask; /* AIO task */
  106 };
  107 
  108 #endif  /* defined(_KERNEL) || defined(_WANT_SOCKET) */
  109 #ifdef _KERNEL
  110 
  111 /*
  112  * Per-socket buffer mutex used to protect most fields in the socket
  113  * buffer.
  114  */
  115 #define SOCKBUF_MTX(_sb)                (&(_sb)->sb_mtx)
  116 #define SOCKBUF_LOCK_INIT(_sb, _name) \
  117         mtx_init(SOCKBUF_MTX(_sb), _name, NULL, MTX_DEF)
  118 #define SOCKBUF_LOCK_DESTROY(_sb)       mtx_destroy(SOCKBUF_MTX(_sb))
  119 #define SOCKBUF_LOCK(_sb)               mtx_lock(SOCKBUF_MTX(_sb))
  120 #define SOCKBUF_OWNED(_sb)              mtx_owned(SOCKBUF_MTX(_sb))
  121 #define SOCKBUF_UNLOCK(_sb)             mtx_unlock(SOCKBUF_MTX(_sb))
  122 #define SOCKBUF_LOCK_ASSERT(_sb)        mtx_assert(SOCKBUF_MTX(_sb), MA_OWNED)
  123 #define SOCKBUF_UNLOCK_ASSERT(_sb)      mtx_assert(SOCKBUF_MTX(_sb), MA_NOTOWNED)
  124 
  125 /*
  126  * Socket buffer private mbuf(9) flags.
  127  */
  128 #define M_NOTREADY      M_PROTO1        /* m_data not populated yet */
  129 #define M_BLOCKED       M_PROTO2        /* M_NOTREADY in front of m */
  130 #define M_NOTAVAIL      (M_NOTREADY | M_BLOCKED)
  131 
  132 void    sbappend(struct sockbuf *sb, struct mbuf *m, int flags);
  133 void    sbappend_locked(struct sockbuf *sb, struct mbuf *m, int flags);
  134 void    sbappendstream(struct sockbuf *sb, struct mbuf *m, int flags);
  135 void    sbappendstream_locked(struct sockbuf *sb, struct mbuf *m, int flags);
  136 int     sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa,
  137             struct mbuf *m0, struct mbuf *control);
  138 int     sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa,
  139             struct mbuf *m0, struct mbuf *control);
  140 int     sbappendaddr_nospacecheck_locked(struct sockbuf *sb,
  141             const struct sockaddr *asa, struct mbuf *m0, struct mbuf *control);
  142 void    sbappendcontrol(struct sockbuf *sb, struct mbuf *m0,
  143             struct mbuf *control);
  144 void    sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0,
  145             struct mbuf *control);
  146 void    sbappendrecord(struct sockbuf *sb, struct mbuf *m0);
  147 void    sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0);
  148 void    sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n);
  149 struct mbuf *
  150         sbcreatecontrol(caddr_t p, int size, int type, int level);
  151 void    sbdestroy(struct sockbuf *sb, struct socket *so);
  152 void    sbdrop(struct sockbuf *sb, int len);
  153 void    sbdrop_locked(struct sockbuf *sb, int len);
  154 struct mbuf *
  155         sbcut_locked(struct sockbuf *sb, int len);
  156 void    sbdroprecord(struct sockbuf *sb);
  157 void    sbdroprecord_locked(struct sockbuf *sb);
  158 void    sbflush(struct sockbuf *sb);
  159 void    sbflush_locked(struct sockbuf *sb);
  160 void    sbrelease(struct sockbuf *sb, struct socket *so);
  161 void    sbrelease_internal(struct sockbuf *sb, struct socket *so);
  162 void    sbrelease_locked(struct sockbuf *sb, struct socket *so);
  163 int     sbsetopt(struct socket *so, int cmd, u_long cc);
  164 int     sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so,
  165             struct thread *td);
  166 struct mbuf *
  167         sbsndptr(struct sockbuf *sb, u_int off, u_int len, u_int *moff);
  168 struct mbuf *
  169         sbsndptr_noadv(struct sockbuf *sb, u_int off, u_int *moff);
  170 void
  171         sbsndptr_adv(struct sockbuf *sb, struct mbuf *mb, u_int len);
  172 struct mbuf *
  173         sbsndmbuf(struct sockbuf *sb, u_int off, u_int *moff);
  174 int     sbwait(struct sockbuf *sb);
  175 int     sblock(struct sockbuf *sb, int flags);
  176 void    sbunlock(struct sockbuf *sb);
  177 void    sballoc(struct sockbuf *, struct mbuf *);
  178 void    sbfree(struct sockbuf *, struct mbuf *);
  179 int     sbready(struct sockbuf *, struct mbuf *, int);
  180 
  181 /*
  182  * Return how much data is available to be taken out of socket
  183  * buffer right now.
  184  */
  185 static inline u_int
  186 sbavail(struct sockbuf *sb)
  187 {
  188 
  189 #if 0
  190         SOCKBUF_LOCK_ASSERT(sb);
  191 #endif
  192         return (sb->sb_acc);
  193 }
  194 
  195 /*
  196  * Return how much data sits there in the socket buffer
  197  * It might be that some data is not yet ready to be read.
  198  */
  199 static inline u_int
  200 sbused(struct sockbuf *sb)
  201 {
  202 
  203 #if 0
  204         SOCKBUF_LOCK_ASSERT(sb);
  205 #endif
  206         return (sb->sb_ccc);
  207 }
  208 
  209 /*
  210  * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
  211  * This is problematical if the fields are unsigned, as the space might
  212  * still be negative (ccc > hiwat or mbcnt > mbmax).
  213  */
  214 static inline long
  215 sbspace(struct sockbuf *sb)
  216 {
  217         int bleft, mleft;               /* size should match sockbuf fields */
  218 
  219 #if 0
  220         SOCKBUF_LOCK_ASSERT(sb);
  221 #endif
  222 
  223         if (sb->sb_flags & SB_STOP)
  224                 return(0);
  225 
  226         bleft = sb->sb_hiwat - sb->sb_ccc;
  227         mleft = sb->sb_mbmax - sb->sb_mbcnt;
  228 
  229         return ((bleft < mleft) ? bleft : mleft);
  230 }
  231 
  232 #define SB_EMPTY_FIXUP(sb) do {                                         \
  233         if ((sb)->sb_mb == NULL) {                                      \
  234                 (sb)->sb_mbtail = NULL;                                 \
  235                 (sb)->sb_lastrecord = NULL;                             \
  236         }                                                               \
  237 } while (/*CONSTCOND*/0)
  238 
  239 #ifdef SOCKBUF_DEBUG
  240 void    sblastrecordchk(struct sockbuf *, const char *, int);
  241 void    sblastmbufchk(struct sockbuf *, const char *, int);
  242 void    sbcheck(struct sockbuf *, const char *, int);
  243 #define SBLASTRECORDCHK(sb)     sblastrecordchk((sb), __FILE__, __LINE__)
  244 #define SBLASTMBUFCHK(sb)       sblastmbufchk((sb), __FILE__, __LINE__)
  245 #define SBCHECK(sb)             sbcheck((sb), __FILE__, __LINE__)
  246 #else
  247 #define SBLASTRECORDCHK(sb)     do {} while (0)
  248 #define SBLASTMBUFCHK(sb)       do {} while (0)
  249 #define SBCHECK(sb)             do {} while (0)
  250 #endif /* SOCKBUF_DEBUG */
  251 
  252 #endif /* _KERNEL */
  253 
  254 #endif /* _SYS_SOCKBUF_H_ */

Cache object: 0d5c2d36c82d02c203a5c7192fdbc84a


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