1180948Skmacy/*- 2180948Skmacy * Copyright (c) 1982, 1986, 1990, 1993 3180948Skmacy * The Regents of the University of California. All rights reserved. 4180948Skmacy * 5180948Skmacy * Redistribution and use in source and binary forms, with or without 6180948Skmacy * modification, are permitted provided that the following conditions 7180948Skmacy * are met: 8180948Skmacy * 1. Redistributions of source code must retain the above copyright 9180948Skmacy * notice, this list of conditions and the following disclaimer. 10180948Skmacy * 2. Redistributions in binary form must reproduce the above copyright 11180948Skmacy * notice, this list of conditions and the following disclaimer in the 12180948Skmacy * documentation and/or other materials provided with the distribution. 13180948Skmacy * 4. Neither the name of the University nor the names of its contributors 14180948Skmacy * may be used to endorse or promote products derived from this software 15180948Skmacy * without specific prior written permission. 16180948Skmacy * 17180948Skmacy * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18180948Skmacy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19180948Skmacy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20180948Skmacy * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21180948Skmacy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22180948Skmacy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23180948Skmacy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24180948Skmacy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25180948Skmacy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26180948Skmacy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27180948Skmacy * SUCH DAMAGE. 28180948Skmacy * 29180948Skmacy * @(#)socketvar.h 8.3 (Berkeley) 2/19/95 30180948Skmacy * 31180948Skmacy * $FreeBSD$ 32180948Skmacy */ 33180948Skmacy#ifndef _SYS_SOCKBUF_H_ 34180948Skmacy#define _SYS_SOCKBUF_H_ 35181066Skmacy#include <sys/selinfo.h> /* for struct selinfo */ 36181066Skmacy#include <sys/_lock.h> 37181066Skmacy#include <sys/_mutex.h> 38181066Skmacy#include <sys/_sx.h> 39180948Skmacy 40225169Sbz#define SB_MAX (2*1024*1024) /* default for max chars in sockbuf */ 41180948Skmacy 42180948Skmacy/* 43180948Skmacy * Constants for sb_flags field of struct sockbuf. 44180948Skmacy */ 45180948Skmacy#define SB_WAIT 0x04 /* someone is waiting for data/space */ 46180948Skmacy#define SB_SEL 0x08 /* someone is selecting */ 47180948Skmacy#define SB_ASYNC 0x10 /* ASYNC I/O, need signals */ 48180948Skmacy#define SB_UPCALL 0x20 /* someone wants an upcall */ 49180948Skmacy#define SB_NOINTR 0x40 /* operations not interruptible */ 50180948Skmacy#define SB_AIO 0x80 /* AIO operations queued */ 51180948Skmacy#define SB_KNOTE 0x100 /* kernel note attached */ 52180948Skmacy#define SB_NOCOALESCE 0x200 /* don't coalesce new data into existing mbufs */ 53180948Skmacy#define SB_IN_TOE 0x400 /* socket buffer is in the middle of an operation */ 54180948Skmacy#define SB_AUTOSIZE 0x800 /* automatically size socket buffer */ 55264080Sasomers#define SB_STOP 0x1000 /* backpressure indicator */ 56180948Skmacy 57180948Skmacy#define SBS_CANTSENDMORE 0x0010 /* can't send more data to peer */ 58180948Skmacy#define SBS_CANTRCVMORE 0x0020 /* can't receive more data from peer */ 59180948Skmacy#define SBS_RCVATMARK 0x0040 /* at mark on input */ 60180948Skmacy 61180971Skmacystruct mbuf; 62180971Skmacystruct sockaddr; 63180948Skmacystruct socket; 64180971Skmacystruct thread; 65180948Skmacy 66180948Skmacystruct xsockbuf { 67180948Skmacy u_int sb_cc; 68180948Skmacy u_int sb_hiwat; 69180948Skmacy u_int sb_mbcnt; 70180948Skmacy u_int sb_mcnt; 71180948Skmacy u_int sb_ccnt; 72180948Skmacy u_int sb_mbmax; 73180948Skmacy int sb_lowat; 74180948Skmacy int sb_timeo; 75180948Skmacy short sb_flags; 76180948Skmacy}; 77180948Skmacy 78180948Skmacy/* 79180948Skmacy * Variables for socket buffering. 80180948Skmacy */ 81180948Skmacystruct sockbuf { 82180948Skmacy struct selinfo sb_sel; /* process selecting read/write */ 83180948Skmacy struct mtx sb_mtx; /* sockbuf lock */ 84180948Skmacy struct sx sb_sx; /* prevent I/O interlacing */ 85180948Skmacy short sb_state; /* (c/d) socket state on sockbuf */ 86180948Skmacy#define sb_startzero sb_mb 87180948Skmacy struct mbuf *sb_mb; /* (c/d) the mbuf chain */ 88180948Skmacy struct mbuf *sb_mbtail; /* (c/d) the last mbuf in the chain */ 89180948Skmacy struct mbuf *sb_lastrecord; /* (c/d) first mbuf of last 90180948Skmacy * record in socket buffer */ 91180948Skmacy struct mbuf *sb_sndptr; /* (c/d) pointer into mbuf chain */ 92180948Skmacy u_int sb_sndptroff; /* (c/d) byte offset of ptr into chain */ 93180948Skmacy u_int sb_cc; /* (c/d) actual chars in buffer */ 94180948Skmacy u_int sb_hiwat; /* (c/d) max actual char count */ 95180948Skmacy u_int sb_mbcnt; /* (c/d) chars of mbufs used */ 96180948Skmacy u_int sb_mcnt; /* (c/d) number of mbufs in buffer */ 97180948Skmacy u_int sb_ccnt; /* (c/d) number of clusters in buffer */ 98180948Skmacy u_int sb_mbmax; /* (c/d) max chars of mbufs to use */ 99180948Skmacy u_int sb_ctl; /* (c/d) non-data chars in buffer */ 100180948Skmacy int sb_lowat; /* (c/d) low water mark */ 101255138Sdavide sbintime_t sb_timeo; /* (c/d) timeout for read/write */ 102180948Skmacy short sb_flags; /* (c/d) flags, see below */ 103193272Sjhb int (*sb_upcall)(struct socket *, void *, int); /* (c/d) */ 104193272Sjhb void *sb_upcallarg; /* (c/d) */ 105180948Skmacy}; 106180971Skmacy 107180970Scognet#ifdef _KERNEL 108180970Scognet 109181066Skmacy/* 110181066Skmacy * Per-socket buffer mutex used to protect most fields in the socket 111181066Skmacy * buffer. 112181066Skmacy */ 113181066Skmacy#define SOCKBUF_MTX(_sb) (&(_sb)->sb_mtx) 114181066Skmacy#define SOCKBUF_LOCK_INIT(_sb, _name) \ 115181066Skmacy mtx_init(SOCKBUF_MTX(_sb), _name, NULL, MTX_DEF) 116181066Skmacy#define SOCKBUF_LOCK_DESTROY(_sb) mtx_destroy(SOCKBUF_MTX(_sb)) 117181066Skmacy#define SOCKBUF_LOCK(_sb) mtx_lock(SOCKBUF_MTX(_sb)) 118181066Skmacy#define SOCKBUF_OWNED(_sb) mtx_owned(SOCKBUF_MTX(_sb)) 119181066Skmacy#define SOCKBUF_UNLOCK(_sb) mtx_unlock(SOCKBUF_MTX(_sb)) 120181066Skmacy#define SOCKBUF_LOCK_ASSERT(_sb) mtx_assert(SOCKBUF_MTX(_sb), MA_OWNED) 121181066Skmacy#define SOCKBUF_UNLOCK_ASSERT(_sb) mtx_assert(SOCKBUF_MTX(_sb), MA_NOTOWNED) 122181066Skmacy 123180948Skmacyvoid sbappend(struct sockbuf *sb, struct mbuf *m); 124180948Skmacyvoid sbappend_locked(struct sockbuf *sb, struct mbuf *m); 125180948Skmacyvoid sbappendstream(struct sockbuf *sb, struct mbuf *m); 126180948Skmacyvoid sbappendstream_locked(struct sockbuf *sb, struct mbuf *m); 127180948Skmacyint sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa, 128180948Skmacy struct mbuf *m0, struct mbuf *control); 129180948Skmacyint sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa, 130180948Skmacy struct mbuf *m0, struct mbuf *control); 131263820Sasomersint sbappendaddr_nospacecheck_locked(struct sockbuf *sb, 132263820Sasomers const struct sockaddr *asa, struct mbuf *m0, struct mbuf *control); 133180948Skmacyint sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, 134180948Skmacy struct mbuf *control); 135180948Skmacyint sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0, 136180948Skmacy struct mbuf *control); 137180948Skmacyvoid sbappendrecord(struct sockbuf *sb, struct mbuf *m0); 138180948Skmacyvoid sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0); 139180948Skmacyvoid sbcheck(struct sockbuf *sb); 140180948Skmacyvoid sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n); 141180948Skmacystruct mbuf * 142180948Skmacy sbcreatecontrol(caddr_t p, int size, int type, int level); 143180948Skmacyvoid sbdestroy(struct sockbuf *sb, struct socket *so); 144180948Skmacyvoid sbdrop(struct sockbuf *sb, int len); 145180948Skmacyvoid sbdrop_locked(struct sockbuf *sb, int len); 146256185Sglebiusstruct mbuf * 147256185Sglebius sbcut_locked(struct sockbuf *sb, int len); 148180948Skmacyvoid sbdroprecord(struct sockbuf *sb); 149180948Skmacyvoid sbdroprecord_locked(struct sockbuf *sb); 150180948Skmacyvoid sbflush(struct sockbuf *sb); 151180948Skmacyvoid sbflush_locked(struct sockbuf *sb); 152180948Skmacyvoid sbrelease(struct sockbuf *sb, struct socket *so); 153180948Skmacyvoid sbrelease_internal(struct sockbuf *sb, struct socket *so); 154180948Skmacyvoid sbrelease_locked(struct sockbuf *sb, struct socket *so); 155180948Skmacyint sbreserve(struct sockbuf *sb, u_long cc, struct socket *so, 156180948Skmacy struct thread *td); 157180948Skmacyint sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so, 158180948Skmacy struct thread *td); 159180948Skmacystruct mbuf * 160180948Skmacy sbsndptr(struct sockbuf *sb, u_int off, u_int len, u_int *moff); 161274043Shselaskystruct mbuf * 162274043Shselasky sbsndmbuf(struct sockbuf *sb, u_int off, u_int *moff); 163180948Skmacyvoid sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb); 164180948Skmacyint sbwait(struct sockbuf *sb); 165180948Skmacyint sblock(struct sockbuf *sb, int flags); 166180948Skmacyvoid sbunlock(struct sockbuf *sb); 167180948Skmacy 168180948Skmacy/* 169180948Skmacy * How much space is there in a socket buffer (so->so_snd or so->so_rcv)? 170180948Skmacy * This is problematical if the fields are unsigned, as the space might 171180948Skmacy * still be negative (cc > hiwat or mbcnt > mbmax). Should detect 172180948Skmacy * overflow and return 0. Should use "lmin" but it doesn't exist now. 173180948Skmacy */ 174264080Sasomersstatic __inline 175264080Sasomerslong 176264080Sasomerssbspace(struct sockbuf *sb) 177264080Sasomers{ 178279930Ssjg int bleft, mleft; /* size should match sockbuf fields */ 179180948Skmacy 180264080Sasomers if (sb->sb_flags & SB_STOP) 181264080Sasomers return(0); 182264080Sasomers bleft = sb->sb_hiwat - sb->sb_cc; 183264080Sasomers mleft = sb->sb_mbmax - sb->sb_mbcnt; 184264080Sasomers return((bleft < mleft) ? bleft : mleft); 185264080Sasomers} 186264080Sasomers 187180948Skmacy/* adjust counters in sb reflecting allocation of m */ 188180948Skmacy#define sballoc(sb, m) { \ 189180948Skmacy (sb)->sb_cc += (m)->m_len; \ 190180948Skmacy if ((m)->m_type != MT_DATA && (m)->m_type != MT_OOBDATA) \ 191180948Skmacy (sb)->sb_ctl += (m)->m_len; \ 192180948Skmacy (sb)->sb_mbcnt += MSIZE; \ 193180948Skmacy (sb)->sb_mcnt += 1; \ 194180948Skmacy if ((m)->m_flags & M_EXT) { \ 195180948Skmacy (sb)->sb_mbcnt += (m)->m_ext.ext_size; \ 196180948Skmacy (sb)->sb_ccnt += 1; \ 197180948Skmacy } \ 198180948Skmacy} 199180948Skmacy 200180948Skmacy/* adjust counters in sb reflecting freeing of m */ 201180948Skmacy#define sbfree(sb, m) { \ 202180948Skmacy (sb)->sb_cc -= (m)->m_len; \ 203180948Skmacy if ((m)->m_type != MT_DATA && (m)->m_type != MT_OOBDATA) \ 204180948Skmacy (sb)->sb_ctl -= (m)->m_len; \ 205180948Skmacy (sb)->sb_mbcnt -= MSIZE; \ 206180948Skmacy (sb)->sb_mcnt -= 1; \ 207180948Skmacy if ((m)->m_flags & M_EXT) { \ 208180948Skmacy (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \ 209180948Skmacy (sb)->sb_ccnt -= 1; \ 210180948Skmacy } \ 211180948Skmacy if ((sb)->sb_sndptr == (m)) { \ 212180948Skmacy (sb)->sb_sndptr = NULL; \ 213180948Skmacy (sb)->sb_sndptroff = 0; \ 214180948Skmacy } \ 215180948Skmacy if ((sb)->sb_sndptroff != 0) \ 216180948Skmacy (sb)->sb_sndptroff -= (m)->m_len; \ 217180948Skmacy} 218180948Skmacy 219180948Skmacy#define SB_EMPTY_FIXUP(sb) do { \ 220180948Skmacy if ((sb)->sb_mb == NULL) { \ 221180948Skmacy (sb)->sb_mbtail = NULL; \ 222180948Skmacy (sb)->sb_lastrecord = NULL; \ 223180948Skmacy } \ 224180948Skmacy} while (/*CONSTCOND*/0) 225180948Skmacy 226180948Skmacy#ifdef SOCKBUF_DEBUG 227180948Skmacyvoid sblastrecordchk(struct sockbuf *, const char *, int); 228180948Skmacy#define SBLASTRECORDCHK(sb) sblastrecordchk((sb), __FILE__, __LINE__) 229180948Skmacy 230180948Skmacyvoid sblastmbufchk(struct sockbuf *, const char *, int); 231180948Skmacy#define SBLASTMBUFCHK(sb) sblastmbufchk((sb), __FILE__, __LINE__) 232180948Skmacy#else 233180948Skmacy#define SBLASTRECORDCHK(sb) /* nothing */ 234180948Skmacy#define SBLASTMBUFCHK(sb) /* nothing */ 235180948Skmacy#endif /* SOCKBUF_DEBUG */ 236180948Skmacy 237180970Scognet#endif /* _KERNEL */ 238180970Scognet 239180948Skmacy#endif /* _SYS_SOCKBUF_H_ */ 240