]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/sockbuf.h
OpenSSL: Merge OpenSSL 1.1.1q
[FreeBSD/FreeBSD.git] / sys / sys / sockbuf.h
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  * Socket buffer
79  *
80  * A buffer starts with the fields that are accessed by I/O multiplexing
81  * APIs like select(2), kevent(2) or AIO and thus are shared between different
82  * buffer implementations.  They are protected by the SOCK_RECVBUF_LOCK()
83  * or SOCK_SENDBUF_LOCK() of the owning socket.
84  *
85  * XXX: sb_acc, sb_ccc and sb_mbcnt shall become implementation specific
86  * methods.
87  *
88  * Protocol specific implementations follow in a union.
89  */
90 struct sockbuf {
91         struct  selinfo *sb_sel;        /* process selecting read/write */
92         short   sb_state;               /* socket state on sockbuf */
93         short   sb_flags;               /* flags, see above */
94         u_int   sb_acc;                 /* available chars in buffer */
95         u_int   sb_ccc;                 /* claimed chars in buffer */
96         u_int   sb_mbcnt;               /* chars of mbufs used */
97         u_int   sb_ctl;                 /* non-data chars in buffer */
98         u_int   sb_hiwat;               /* max actual char count */
99         u_int   sb_lowat;               /* low water mark */
100         u_int   sb_mbmax;               /* max chars of mbufs to use */
101         sbintime_t sb_timeo;            /* timeout for read/write */
102         int     (*sb_upcall)(struct socket *, void *, int);
103         void    *sb_upcallarg;
104         TAILQ_HEAD(, kaiocb) sb_aiojobq;        /* pending AIO ops */
105         struct  task sb_aiotask;                /* AIO task */
106         union {
107                 /*
108                  * Classic BSD one-size-fits-all socket buffer, capable of
109                  * doing streams and datagrams. The stream part is able
110                  * to perform special features:
111                  * - not ready data (sendfile)
112                  * - TLS
113                  */
114                 struct {
115                         /* compat: sockbuf lock pointer */
116                         struct  mtx *sb_mtx;
117                         /* first and last mbufs in the chain */
118                         struct  mbuf *sb_mb;
119                         struct  mbuf *sb_mbtail;
120                         /* first mbuf of last record in socket buffer */
121                         struct  mbuf *sb_lastrecord;
122                         /* pointer to data to send next (TCP */
123                         struct  mbuf *sb_sndptr;
124                         /* pointer to first not ready buffer */
125                         struct  mbuf *sb_fnrdy;
126                         /* byte offset of ptr into chain, used with sb_sndptr */
127                         u_int   sb_sndptroff;
128                         /* TLS */
129                         u_int   sb_tlscc;       /* TLS chain characters */
130                         u_int   sb_tlsdcc;      /* characters being decrypted */
131                         struct  mbuf *sb_mtls;  /*  TLS mbuf chain */
132                         struct  mbuf *sb_mtlstail; /* last mbuf in TLS chain */
133                         uint64_t sb_tls_seqno;  /* TLS seqno */
134                         struct  ktls_session *sb_tls_info; /* TLS state */
135                 };
136                 /*
137                  * PF_UNIX/SOCK_DGRAM
138                  *
139                  * Local protocol, thus we should buffer on the receive side
140                  * only.  However, in one to many configuration we don't want
141                  * a single receive buffer to be shared.  So we would link
142                  * send buffers onto receive buffer.  All the fields are locked
143                  * by the receive buffer lock.
144                  */
145                 struct {
146                         /*
147                          * For receive buffer: own queue of this buffer for
148                          * unconnected sends.  For send buffer: queue lended
149                          * to the peer receive buffer, to isolate ourselves
150                          * from other senders.
151                          */
152                         STAILQ_HEAD(, mbuf)     uxdg_mb;
153                         /* For receive buffer: datagram seen via MSG_PEEK. */
154                         struct mbuf             *uxdg_peeked;
155                         /*
156                          * For receive buffer: queue of send buffers of
157                          * connected peers.  For send buffer: linkage on
158                          * connected peer receive buffer queue.
159                          */
160                         union {
161                                 TAILQ_HEAD(, sockbuf)   uxdg_conns;
162                                 TAILQ_ENTRY(sockbuf)    uxdg_clist;
163                         };
164                         /* Counters for this buffer uxdg_mb chain + peeked. */
165                         u_int uxdg_cc;
166                         u_int uxdg_ctl;
167                         u_int uxdg_mbcnt;
168                 };
169         };
170 };
171
172 #endif  /* defined(_KERNEL) || defined(_WANT_SOCKET) */
173 #ifdef _KERNEL
174
175 /* 'which' values for KPIs that operate on one buffer of a socket. */
176 typedef enum { SO_RCV, SO_SND } sb_which;
177
178 /*
179  * Per-socket buffer mutex used to protect most fields in the socket buffer.
180  * These make use of the mutex pointer embedded in struct sockbuf, which
181  * currently just references mutexes in the containing socket.  The
182  * SOCK_SENDBUF_LOCK() etc. macros can be used instead of or in combination with
183  * these locking macros.
184  */
185 #define SOCKBUF_MTX(_sb)                ((_sb)->sb_mtx)
186 #define SOCKBUF_LOCK(_sb)               mtx_lock(SOCKBUF_MTX(_sb))
187 #define SOCKBUF_OWNED(_sb)              mtx_owned(SOCKBUF_MTX(_sb))
188 #define SOCKBUF_UNLOCK(_sb)             mtx_unlock(SOCKBUF_MTX(_sb))
189 #define SOCKBUF_LOCK_ASSERT(_sb)        mtx_assert(SOCKBUF_MTX(_sb), MA_OWNED)
190 #define SOCKBUF_UNLOCK_ASSERT(_sb)      mtx_assert(SOCKBUF_MTX(_sb), MA_NOTOWNED)
191
192 /*
193  * Socket buffer private mbuf(9) flags.
194  */
195 #define M_NOTREADY      M_PROTO1        /* m_data not populated yet */
196 #define M_BLOCKED       M_PROTO2        /* M_NOTREADY in front of m */
197 #define M_NOTAVAIL      (M_NOTREADY | M_BLOCKED)
198
199 void    sbappend(struct sockbuf *sb, struct mbuf *m, int flags);
200 void    sbappend_locked(struct sockbuf *sb, struct mbuf *m, int flags);
201 void    sbappendstream(struct sockbuf *sb, struct mbuf *m, int flags);
202 void    sbappendstream_locked(struct sockbuf *sb, struct mbuf *m, int flags);
203 int     sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa,
204             struct mbuf *m0, struct mbuf *control);
205 int     sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa,
206             struct mbuf *m0, struct mbuf *control);
207 int     sbappendaddr_nospacecheck_locked(struct sockbuf *sb,
208             const struct sockaddr *asa, struct mbuf *m0, struct mbuf *control);
209 void    sbappendcontrol(struct sockbuf *sb, struct mbuf *m0,
210             struct mbuf *control, int flags);
211 void    sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0,
212             struct mbuf *control, int flags);
213 void    sbappendrecord(struct sockbuf *sb, struct mbuf *m0);
214 void    sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0);
215 void    sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n);
216 struct mbuf *
217         sbcreatecontrol(const void *p, u_int size, int type, int level,
218             int wait);
219 void    sbdestroy(struct socket *, sb_which);
220 void    sbdrop(struct sockbuf *sb, int len);
221 void    sbdrop_locked(struct sockbuf *sb, int len);
222 struct mbuf *
223         sbcut_locked(struct sockbuf *sb, int len);
224 void    sbdroprecord(struct sockbuf *sb);
225 void    sbdroprecord_locked(struct sockbuf *sb);
226 void    sbflush(struct sockbuf *sb);
227 void    sbflush_locked(struct sockbuf *sb);
228 void    sbrelease(struct socket *, sb_which);
229 void    sbrelease_locked(struct socket *, sb_which);
230 int     sbsetopt(struct socket *so, int cmd, u_long cc);
231 bool    sbreserve_locked(struct socket *so, sb_which which, u_long cc,
232             struct thread *td);
233 void    sbsndptr_adv(struct sockbuf *sb, struct mbuf *mb, u_int len);
234 struct mbuf *
235         sbsndptr_noadv(struct sockbuf *sb, u_int off, u_int *moff);
236 struct mbuf *
237         sbsndmbuf(struct sockbuf *sb, u_int off, u_int *moff);
238 int     sbwait(struct socket *, sb_which);
239 void    sballoc(struct sockbuf *, struct mbuf *);
240 void    sbfree(struct sockbuf *, struct mbuf *);
241 void    sballoc_ktls_rx(struct sockbuf *sb, struct mbuf *m);
242 void    sbfree_ktls_rx(struct sockbuf *sb, struct mbuf *m);
243 int     sbready(struct sockbuf *, struct mbuf *, int);
244
245 /*
246  * Return how much data is available to be taken out of socket
247  * buffer right now.
248  */
249 static inline u_int
250 sbavail(struct sockbuf *sb)
251 {
252
253 #if 0
254         SOCKBUF_LOCK_ASSERT(sb);
255 #endif
256         return (sb->sb_acc);
257 }
258
259 /*
260  * Return how much data sits there in the socket buffer
261  * It might be that some data is not yet ready to be read.
262  */
263 static inline u_int
264 sbused(struct sockbuf *sb)
265 {
266
267 #if 0
268         SOCKBUF_LOCK_ASSERT(sb);
269 #endif
270         return (sb->sb_ccc);
271 }
272
273 /*
274  * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
275  * This is problematical if the fields are unsigned, as the space might
276  * still be negative (ccc > hiwat or mbcnt > mbmax).
277  */
278 static inline long
279 sbspace(struct sockbuf *sb)
280 {
281         int bleft, mleft;               /* size should match sockbuf fields */
282
283 #if 0
284         SOCKBUF_LOCK_ASSERT(sb);
285 #endif
286
287         if (sb->sb_flags & SB_STOP)
288                 return(0);
289
290         bleft = sb->sb_hiwat - sb->sb_ccc;
291         mleft = sb->sb_mbmax - sb->sb_mbcnt;
292
293         return ((bleft < mleft) ? bleft : mleft);
294 }
295
296 #define SB_EMPTY_FIXUP(sb) do {                                         \
297         if ((sb)->sb_mb == NULL) {                                      \
298                 (sb)->sb_mbtail = NULL;                                 \
299                 (sb)->sb_lastrecord = NULL;                             \
300         }                                                               \
301 } while (/*CONSTCOND*/0)
302
303 #ifdef SOCKBUF_DEBUG
304 void    sblastrecordchk(struct sockbuf *, const char *, int);
305 void    sblastmbufchk(struct sockbuf *, const char *, int);
306 void    sbcheck(struct sockbuf *, const char *, int);
307 #define SBLASTRECORDCHK(sb)     sblastrecordchk((sb), __FILE__, __LINE__)
308 #define SBLASTMBUFCHK(sb)       sblastmbufchk((sb), __FILE__, __LINE__)
309 #define SBCHECK(sb)             sbcheck((sb), __FILE__, __LINE__)
310 #else
311 #define SBLASTRECORDCHK(sb)     do {} while (0)
312 #define SBLASTMBUFCHK(sb)       do {} while (0)
313 #define SBCHECK(sb)             do {} while (0)
314 #endif /* SOCKBUF_DEBUG */
315
316 #endif /* _KERNEL */
317
318 #endif /* _SYS_SOCKBUF_H_ */