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