sockbuf.h revision 181066
1275970Scy/*- 2275970Scy * Copyright (c) 1982, 1986, 1990, 1993 3275970Scy * The Regents of the University of California. All rights reserved. 4275970Scy * 5275970Scy * Redistribution and use in source and binary forms, with or without 6275970Scy * modification, are permitted provided that the following conditions 7275970Scy * are met: 8275970Scy * 1. Redistributions of source code must retain the above copyright 9275970Scy * notice, this list of conditions and the following disclaimer. 10275970Scy * 2. Redistributions in binary form must reproduce the above copyright 11275970Scy * notice, this list of conditions and the following disclaimer in the 12275970Scy * documentation and/or other materials provided with the distribution. 13275970Scy * 4. Neither the name of the University nor the names of its contributors 14275970Scy * may be used to endorse or promote products derived from this software 15275970Scy * without specific prior written permission. 16275970Scy * 17275970Scy * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18275970Scy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19275970Scy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20275970Scy * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21275970Scy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22275970Scy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23275970Scy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24275970Scy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25275970Scy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26275970Scy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27275970Scy * SUCH DAMAGE. 28275970Scy * 29275970Scy * @(#)socketvar.h 8.3 (Berkeley) 2/19/95 30275970Scy * 31275970Scy * $FreeBSD: head/sys/sys/sockbuf.h 181066 2008-07-31 20:27:50Z kmacy $ 32275970Scy */ 33275970Scy#ifndef _SYS_SOCKBUF_H_ 34275970Scy#define _SYS_SOCKBUF_H_ 35275970Scy#include <sys/selinfo.h> /* for struct selinfo */ 36301301Sdelphij#include <sys/_lock.h> 37275970Scy#include <sys/_mutex.h> 38275970Scy#include <sys/_sx.h> 39275970Scy 40275970Scy#define SB_MAX (256*1024) /* default for max chars in sockbuf */ 41275970Scy 42275970Scy/* 43275970Scy * Constants for sb_flags field of struct sockbuf. 44275970Scy */ 45275970Scy#define SB_WAIT 0x04 /* someone is waiting for data/space */ 46275970Scy#define SB_SEL 0x08 /* someone is selecting */ 47275970Scy#define SB_ASYNC 0x10 /* ASYNC I/O, need signals */ 48275970Scy#define SB_UPCALL 0x20 /* someone wants an upcall */ 49275970Scy#define SB_NOINTR 0x40 /* operations not interruptible */ 50275970Scy#define SB_AIO 0x80 /* AIO operations queued */ 51275970Scy#define SB_KNOTE 0x100 /* kernel note attached */ 52275970Scy#define SB_NOCOALESCE 0x200 /* don't coalesce new data into existing mbufs */ 53275970Scy#define SB_IN_TOE 0x400 /* socket buffer is in the middle of an operation */ 54275970Scy#define SB_AUTOSIZE 0x800 /* automatically size socket buffer */ 55275970Scy 56275970Scy#define SBS_CANTSENDMORE 0x0010 /* can't send more data to peer */ 57275970Scy#define SBS_CANTRCVMORE 0x0020 /* can't receive more data from peer */ 58275970Scy#define SBS_RCVATMARK 0x0040 /* at mark on input */ 59275970Scy 60275970Scystruct mbuf; 61275970Scystruct sockaddr; 62275970Scystruct socket; 63275970Scystruct thread; 64275970Scy 65275970Scystruct xsockbuf { 66275970Scy u_int sb_cc; 67 u_int sb_hiwat; 68 u_int sb_mbcnt; 69 u_int sb_mcnt; 70 u_int sb_ccnt; 71 u_int sb_mbmax; 72 int sb_lowat; 73 int sb_timeo; 74 short sb_flags; 75}; 76 77/* 78 * Variables for socket buffering. 79 */ 80struct sockbuf { 81 struct selinfo sb_sel; /* process selecting read/write */ 82 struct mtx sb_mtx; /* sockbuf lock */ 83 struct sx sb_sx; /* prevent I/O interlacing */ 84 short sb_state; /* (c/d) socket state on sockbuf */ 85#define sb_startzero sb_mb 86 struct mbuf *sb_mb; /* (c/d) the mbuf chain */ 87 struct mbuf *sb_mbtail; /* (c/d) the last mbuf in the chain */ 88 struct mbuf *sb_lastrecord; /* (c/d) first mbuf of last 89 * record in socket buffer */ 90 struct mbuf *sb_sndptr; /* (c/d) pointer into mbuf chain */ 91 u_int sb_sndptroff; /* (c/d) byte offset of ptr into chain */ 92 u_int sb_cc; /* (c/d) actual chars in buffer */ 93 u_int sb_hiwat; /* (c/d) max actual char count */ 94 u_int sb_mbcnt; /* (c/d) chars of mbufs used */ 95 u_int sb_mcnt; /* (c/d) number of mbufs in buffer */ 96 u_int sb_ccnt; /* (c/d) number of clusters in buffer */ 97 u_int sb_mbmax; /* (c/d) max chars of mbufs to use */ 98 u_int sb_ctl; /* (c/d) non-data chars in buffer */ 99 int sb_lowat; /* (c/d) low water mark */ 100 int sb_timeo; /* (c/d) timeout for read/write */ 101 short sb_flags; /* (c/d) flags, see below */ 102}; 103 104#ifdef _KERNEL 105 106/* 107 * Per-socket buffer mutex used to protect most fields in the socket 108 * buffer. 109 */ 110#define SOCKBUF_MTX(_sb) (&(_sb)->sb_mtx) 111#define SOCKBUF_LOCK_INIT(_sb, _name) \ 112 mtx_init(SOCKBUF_MTX(_sb), _name, NULL, MTX_DEF) 113#define SOCKBUF_LOCK_DESTROY(_sb) mtx_destroy(SOCKBUF_MTX(_sb)) 114#define SOCKBUF_LOCK(_sb) mtx_lock(SOCKBUF_MTX(_sb)) 115#define SOCKBUF_OWNED(_sb) mtx_owned(SOCKBUF_MTX(_sb)) 116#define SOCKBUF_UNLOCK(_sb) mtx_unlock(SOCKBUF_MTX(_sb)) 117#define SOCKBUF_LOCK_ASSERT(_sb) mtx_assert(SOCKBUF_MTX(_sb), MA_OWNED) 118#define SOCKBUF_UNLOCK_ASSERT(_sb) mtx_assert(SOCKBUF_MTX(_sb), MA_NOTOWNED) 119 120void sbappend(struct sockbuf *sb, struct mbuf *m); 121void sbappend_locked(struct sockbuf *sb, struct mbuf *m); 122void sbappendstream(struct sockbuf *sb, struct mbuf *m); 123void sbappendstream_locked(struct sockbuf *sb, struct mbuf *m); 124int sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa, 125 struct mbuf *m0, struct mbuf *control); 126int sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa, 127 struct mbuf *m0, struct mbuf *control); 128int sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, 129 struct mbuf *control); 130int sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0, 131 struct mbuf *control); 132void sbappendrecord(struct sockbuf *sb, struct mbuf *m0); 133void sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0); 134void sbcheck(struct sockbuf *sb); 135void sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n); 136struct mbuf * 137 sbcreatecontrol(caddr_t p, int size, int type, int level); 138void sbdestroy(struct sockbuf *sb, struct socket *so); 139void sbdrop(struct sockbuf *sb, int len); 140void sbdrop_locked(struct sockbuf *sb, int len); 141void sbdroprecord(struct sockbuf *sb); 142void sbdroprecord_locked(struct sockbuf *sb); 143void sbflush(struct sockbuf *sb); 144void sbflush_locked(struct sockbuf *sb); 145void sbrelease(struct sockbuf *sb, struct socket *so); 146void sbrelease_internal(struct sockbuf *sb, struct socket *so); 147void sbrelease_locked(struct sockbuf *sb, struct socket *so); 148int sbreserve(struct sockbuf *sb, u_long cc, struct socket *so, 149 struct thread *td); 150int sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so, 151 struct thread *td); 152struct mbuf * 153 sbsndptr(struct sockbuf *sb, u_int off, u_int len, u_int *moff); 154void sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb); 155int sbwait(struct sockbuf *sb); 156int sblock(struct sockbuf *sb, int flags); 157void sbunlock(struct sockbuf *sb); 158 159/* 160 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)? 161 * This is problematical if the fields are unsigned, as the space might 162 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect 163 * overflow and return 0. Should use "lmin" but it doesn't exist now. 164 */ 165#define sbspace(sb) \ 166 ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \ 167 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt))) 168 169/* adjust counters in sb reflecting allocation of m */ 170#define sballoc(sb, m) { \ 171 (sb)->sb_cc += (m)->m_len; \ 172 if ((m)->m_type != MT_DATA && (m)->m_type != MT_OOBDATA) \ 173 (sb)->sb_ctl += (m)->m_len; \ 174 (sb)->sb_mbcnt += MSIZE; \ 175 (sb)->sb_mcnt += 1; \ 176 if ((m)->m_flags & M_EXT) { \ 177 (sb)->sb_mbcnt += (m)->m_ext.ext_size; \ 178 (sb)->sb_ccnt += 1; \ 179 } \ 180} 181 182/* adjust counters in sb reflecting freeing of m */ 183#define sbfree(sb, m) { \ 184 (sb)->sb_cc -= (m)->m_len; \ 185 if ((m)->m_type != MT_DATA && (m)->m_type != MT_OOBDATA) \ 186 (sb)->sb_ctl -= (m)->m_len; \ 187 (sb)->sb_mbcnt -= MSIZE; \ 188 (sb)->sb_mcnt -= 1; \ 189 if ((m)->m_flags & M_EXT) { \ 190 (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \ 191 (sb)->sb_ccnt -= 1; \ 192 } \ 193 if ((sb)->sb_sndptr == (m)) { \ 194 (sb)->sb_sndptr = NULL; \ 195 (sb)->sb_sndptroff = 0; \ 196 } \ 197 if ((sb)->sb_sndptroff != 0) \ 198 (sb)->sb_sndptroff -= (m)->m_len; \ 199} 200 201#define SB_EMPTY_FIXUP(sb) do { \ 202 if ((sb)->sb_mb == NULL) { \ 203 (sb)->sb_mbtail = NULL; \ 204 (sb)->sb_lastrecord = NULL; \ 205 } \ 206} while (/*CONSTCOND*/0) 207 208#ifdef SOCKBUF_DEBUG 209void sblastrecordchk(struct sockbuf *, const char *, int); 210#define SBLASTRECORDCHK(sb) sblastrecordchk((sb), __FILE__, __LINE__) 211 212void sblastmbufchk(struct sockbuf *, const char *, int); 213#define SBLASTMBUFCHK(sb) sblastmbufchk((sb), __FILE__, __LINE__) 214#else 215#define SBLASTRECORDCHK(sb) /* nothing */ 216#define SBLASTMBUFCHK(sb) /* nothing */ 217#endif /* SOCKBUF_DEBUG */ 218 219#endif /* _KERNEL */ 220 221#endif /* _SYS_SOCKBUF_H_ */ 222