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 #define SB_TLS_RX_RESYNC 0x8000 /* KTLS RX lost HW sync */ 57 58 #define SBS_CANTSENDMORE 0x0010 /* can't send more data to peer */ 59 #define SBS_CANTRCVMORE 0x0020 /* can't receive more data from peer */ 60 #define SBS_RCVATMARK 0x0040 /* at mark on input */ 61 62 #if defined(_KERNEL) || defined(_WANT_SOCKET) 63 #include <sys/_lock.h> 64 #include <sys/_mutex.h> 65 #include <sys/_sx.h> 66 #include <sys/_task.h> 67 68 #define SB_MAX (2*1024*1024) /* default for max chars in sockbuf */ 69 70 struct ktls_session; 71 struct mbuf; 72 struct sockaddr; 73 struct socket; 74 struct thread; 75 struct selinfo; 76 77 /* 78 * Variables for socket buffering. 79 * 80 * Locking key to struct sockbuf: 81 * (a) locked by SOCKBUF_LOCK(). 82 */ 83 struct sockbuf { 84 struct mtx *sb_mtx; /* sockbuf lock */ 85 struct selinfo *sb_sel; /* process selecting read/write */ 86 short sb_state; /* (a) socket state on sockbuf */ 87 short sb_flags; /* (a) flags, see above */ 88 struct mbuf *sb_mb; /* (a) the mbuf chain */ 89 struct mbuf *sb_mbtail; /* (a) the last mbuf in the chain */ 90 struct mbuf *sb_lastrecord; /* (a) first mbuf of last 91 * record in socket buffer */ 92 struct mbuf *sb_sndptr; /* (a) pointer into mbuf chain */ 93 struct mbuf *sb_fnrdy; /* (a) pointer to first not ready buffer */ 94 u_int sb_sndptroff; /* (a) byte offset of ptr into chain */ 95 u_int sb_acc; /* (a) available chars in buffer */ 96 u_int sb_ccc; /* (a) claimed chars in buffer */ 97 u_int sb_hiwat; /* (a) max actual char count */ 98 u_int sb_mbcnt; /* (a) chars of mbufs used */ 99 u_int sb_mbmax; /* (a) max chars of mbufs to use */ 100 u_int sb_ctl; /* (a) non-data chars in buffer */ 101 u_int sb_tlscc; /* (a) TLS chain characters */ 102 u_int sb_tlsdcc; /* (a) TLS characters being decrypted */ 103 int sb_lowat; /* (a) low water mark */ 104 sbintime_t sb_timeo; /* (a) timeout for read/write */ 105 struct mbuf *sb_mtls; /* (a) TLS mbuf chain */ 106 struct mbuf *sb_mtlstail; /* (a) last mbuf in TLS chain */ 107 int (*sb_upcall)(struct socket *, void *, int); /* (a) */ 108 void *sb_upcallarg; /* (a) */ 109 uint64_t sb_tls_seqno; /* (a) TLS seqno */ 110 struct ktls_session *sb_tls_info; /* (a + b) TLS state */ 111 TAILQ_HEAD(, kaiocb) sb_aiojobq; /* (a) pending AIO ops */ 112 struct task sb_aiotask; /* AIO task */ 113 }; 114 115 #endif /* defined(_KERNEL) || defined(_WANT_SOCKET) */ 116 #ifdef _KERNEL 117 118 /* 'which' values for KPIs that operate on one buffer of a socket. */ 119 typedef enum { SO_RCV, SO_SND } sb_which; 120 121 /* 122 * Per-socket buffer mutex used to protect most fields in the socket buffer. 123 * These make use of the mutex pointer embedded in struct sockbuf, which 124 * currently just references mutexes in the containing socket. The 125 * SOCK_SENDBUF_LOCK() etc. macros can be used instead of or in combination with 126 * these locking macros. 127 */ 128 #define SOCKBUF_MTX(_sb) ((_sb)->sb_mtx) 129 #define SOCKBUF_LOCK(_sb) mtx_lock(SOCKBUF_MTX(_sb)) 130 #define SOCKBUF_OWNED(_sb) mtx_owned(SOCKBUF_MTX(_sb)) 131 #define SOCKBUF_UNLOCK(_sb) mtx_unlock(SOCKBUF_MTX(_sb)) 132 #define SOCKBUF_LOCK_ASSERT(_sb) mtx_assert(SOCKBUF_MTX(_sb), MA_OWNED) 133 #define SOCKBUF_UNLOCK_ASSERT(_sb) mtx_assert(SOCKBUF_MTX(_sb), MA_NOTOWNED) 134 135 /* 136 * Socket buffer private mbuf(9) flags. 137 */ 138 #define M_NOTREADY M_PROTO1 /* m_data not populated yet */ 139 #define M_BLOCKED M_PROTO2 /* M_NOTREADY in front of m */ 140 #define M_NOTAVAIL (M_NOTREADY | M_BLOCKED) 141 142 void sbappend(struct sockbuf *sb, struct mbuf *m, int flags); 143 void sbappend_locked(struct sockbuf *sb, struct mbuf *m, int flags); 144 void sbappendstream(struct sockbuf *sb, struct mbuf *m, int flags); 145 void sbappendstream_locked(struct sockbuf *sb, struct mbuf *m, int flags); 146 int sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa, 147 struct mbuf *m0, struct mbuf *control); 148 int sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa, 149 struct mbuf *m0, struct mbuf *control); 150 int sbappendaddr_nospacecheck_locked(struct sockbuf *sb, 151 const struct sockaddr *asa, struct mbuf *m0, struct mbuf *control); 152 void sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, 153 struct mbuf *control, int flags); 154 void sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0, 155 struct mbuf *control, int flags); 156 void sbappendrecord(struct sockbuf *sb, struct mbuf *m0); 157 void sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0); 158 void sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n); 159 struct mbuf * 160 sbcreatecontrol(const void *p, u_int size, int type, int level, 161 int wait); 162 void sbdestroy(struct socket *, sb_which); 163 void sbdrop(struct sockbuf *sb, int len); 164 void sbdrop_locked(struct sockbuf *sb, int len); 165 struct mbuf * 166 sbcut_locked(struct sockbuf *sb, int len); 167 void sbdroprecord(struct sockbuf *sb); 168 void sbdroprecord_locked(struct sockbuf *sb); 169 void sbflush(struct sockbuf *sb); 170 void sbflush_locked(struct sockbuf *sb); 171 void sbrelease(struct socket *, sb_which); 172 void sbrelease_locked(struct socket *, sb_which); 173 int sbsetopt(struct socket *so, int cmd, u_long cc); 174 bool sbreserve_locked(struct socket *so, sb_which which, u_long cc, 175 struct thread *td); 176 void sbsndptr_adv(struct sockbuf *sb, struct mbuf *mb, u_int len); 177 struct mbuf * 178 sbsndptr_noadv(struct sockbuf *sb, u_int off, u_int *moff); 179 struct mbuf * 180 sbsndmbuf(struct sockbuf *sb, u_int off, u_int *moff); 181 int sbwait(struct socket *, sb_which); 182 void sballoc(struct sockbuf *, struct mbuf *); 183 void sbfree(struct sockbuf *, struct mbuf *); 184 void sballoc_ktls_rx(struct sockbuf *sb, struct mbuf *m); 185 void sbfree_ktls_rx(struct sockbuf *sb, struct mbuf *m); 186 int sbready(struct sockbuf *, struct mbuf *, int); 187 188 /* 189 * Return how much data is available to be taken out of socket 190 * buffer right now. 191 */ 192 static inline u_int 193 sbavail(struct sockbuf *sb) 194 { 195 196 #if 0 197 SOCKBUF_LOCK_ASSERT(sb); 198 #endif 199 return (sb->sb_acc); 200 } 201 202 /* 203 * Return how much data sits there in the socket buffer 204 * It might be that some data is not yet ready to be read. 205 */ 206 static inline u_int 207 sbused(struct sockbuf *sb) 208 { 209 210 #if 0 211 SOCKBUF_LOCK_ASSERT(sb); 212 #endif 213 return (sb->sb_ccc); 214 } 215 216 /* 217 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)? 218 * This is problematical if the fields are unsigned, as the space might 219 * still be negative (ccc > hiwat or mbcnt > mbmax). 220 */ 221 static inline long 222 sbspace(struct sockbuf *sb) 223 { 224 int bleft, mleft; /* size should match sockbuf fields */ 225 226 #if 0 227 SOCKBUF_LOCK_ASSERT(sb); 228 #endif 229 230 if (sb->sb_flags & SB_STOP) 231 return(0); 232 233 bleft = sb->sb_hiwat - sb->sb_ccc; 234 mleft = sb->sb_mbmax - sb->sb_mbcnt; 235 236 return ((bleft < mleft) ? bleft : mleft); 237 } 238 239 #define SB_EMPTY_FIXUP(sb) do { \ 240 if ((sb)->sb_mb == NULL) { \ 241 (sb)->sb_mbtail = NULL; \ 242 (sb)->sb_lastrecord = NULL; \ 243 } \ 244 } while (/*CONSTCOND*/0) 245 246 #ifdef SOCKBUF_DEBUG 247 void sblastrecordchk(struct sockbuf *, const char *, int); 248 void sblastmbufchk(struct sockbuf *, const char *, int); 249 void sbcheck(struct sockbuf *, const char *, int); 250 #define SBLASTRECORDCHK(sb) sblastrecordchk((sb), __FILE__, __LINE__) 251 #define SBLASTMBUFCHK(sb) sblastmbufchk((sb), __FILE__, __LINE__) 252 #define SBCHECK(sb) sbcheck((sb), __FILE__, __LINE__) 253 #else 254 #define SBLASTRECORDCHK(sb) do {} while (0) 255 #define SBLASTMBUFCHK(sb) do {} while (0) 256 #define SBCHECK(sb) do {} while (0) 257 #endif /* SOCKBUF_DEBUG */ 258 259 #endif /* _KERNEL */ 260 261 #endif /* _SYS_SOCKBUF_H_ */ 262