11eaf0ac3Slogwang /*-
2*e2e96b59Sagerguo * SPDX-License-Identifier: BSD-3-Clause
3*e2e96b59Sagerguo *
41eaf0ac3Slogwang * Copyright (c) 1982, 1986, 1990, 1993
51eaf0ac3Slogwang * The Regents of the University of California. All rights reserved.
61eaf0ac3Slogwang *
71eaf0ac3Slogwang * Redistribution and use in source and binary forms, with or without
81eaf0ac3Slogwang * modification, are permitted provided that the following conditions
91eaf0ac3Slogwang * are met:
101eaf0ac3Slogwang * 1. Redistributions of source code must retain the above copyright
111eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer.
121eaf0ac3Slogwang * 2. Redistributions in binary form must reproduce the above copyright
131eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer in the
141eaf0ac3Slogwang * documentation and/or other materials provided with the distribution.
15*e2e96b59Sagerguo * 3. Neither the name of the University nor the names of its contributors
161eaf0ac3Slogwang * may be used to endorse or promote products derived from this software
171eaf0ac3Slogwang * without specific prior written permission.
181eaf0ac3Slogwang *
191eaf0ac3Slogwang * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
201eaf0ac3Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211eaf0ac3Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221eaf0ac3Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
231eaf0ac3Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241eaf0ac3Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251eaf0ac3Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261eaf0ac3Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271eaf0ac3Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281eaf0ac3Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291eaf0ac3Slogwang * SUCH DAMAGE.
301eaf0ac3Slogwang *
311eaf0ac3Slogwang * @(#)socketvar.h 8.3 (Berkeley) 2/19/95
321eaf0ac3Slogwang *
331eaf0ac3Slogwang * $FreeBSD$
341eaf0ac3Slogwang */
351eaf0ac3Slogwang #ifndef _SYS_SOCKBUF_H_
361eaf0ac3Slogwang #define _SYS_SOCKBUF_H_
371eaf0ac3Slogwang
381eaf0ac3Slogwang /*
39*e2e96b59Sagerguo * Constants for sb_flags field of struct sockbuf/xsockbuf.
401eaf0ac3Slogwang */
41*e2e96b59Sagerguo #define SB_TLS_RX 0x01 /* using KTLS on RX */
42*e2e96b59Sagerguo #define SB_TLS_RX_RUNNING 0x02 /* KTLS RX operation running */
431eaf0ac3Slogwang #define SB_WAIT 0x04 /* someone is waiting for data/space */
441eaf0ac3Slogwang #define SB_SEL 0x08 /* someone is selecting */
451eaf0ac3Slogwang #define SB_ASYNC 0x10 /* ASYNC I/O, need signals */
461eaf0ac3Slogwang #define SB_UPCALL 0x20 /* someone wants an upcall */
471eaf0ac3Slogwang #define SB_NOINTR 0x40 /* operations not interruptible */
481eaf0ac3Slogwang #define SB_AIO 0x80 /* AIO operations queued */
491eaf0ac3Slogwang #define SB_KNOTE 0x100 /* kernel note attached */
501eaf0ac3Slogwang #define SB_NOCOALESCE 0x200 /* don't coalesce new data into existing mbufs */
511eaf0ac3Slogwang #define SB_IN_TOE 0x400 /* socket buffer is in the middle of an operation */
521eaf0ac3Slogwang #define SB_AUTOSIZE 0x800 /* automatically size socket buffer */
531eaf0ac3Slogwang #define SB_STOP 0x1000 /* backpressure indicator */
541eaf0ac3Slogwang #define SB_AIO_RUNNING 0x2000 /* AIO operation running */
55*e2e96b59Sagerguo #define SB_TLS_IFNET 0x4000 /* has used / is using ifnet KTLS */
561eaf0ac3Slogwang
571eaf0ac3Slogwang #define SBS_CANTSENDMORE 0x0010 /* can't send more data to peer */
581eaf0ac3Slogwang #define SBS_CANTRCVMORE 0x0020 /* can't receive more data from peer */
591eaf0ac3Slogwang #define SBS_RCVATMARK 0x0040 /* at mark on input */
601eaf0ac3Slogwang
61*e2e96b59Sagerguo #if defined(_KERNEL) || defined(_WANT_SOCKET)
62*e2e96b59Sagerguo #include <sys/_lock.h>
63*e2e96b59Sagerguo #include <sys/_mutex.h>
64*e2e96b59Sagerguo #include <sys/_sx.h>
65*e2e96b59Sagerguo #include <sys/_task.h>
66*e2e96b59Sagerguo
67*e2e96b59Sagerguo #define SB_MAX (2*1024*1024) /* default for max chars in sockbuf */
68*e2e96b59Sagerguo
69*e2e96b59Sagerguo struct ktls_session;
701eaf0ac3Slogwang struct mbuf;
711eaf0ac3Slogwang struct sockaddr;
721eaf0ac3Slogwang struct socket;
731eaf0ac3Slogwang struct thread;
74*e2e96b59Sagerguo struct selinfo;
751eaf0ac3Slogwang
761eaf0ac3Slogwang /*
771eaf0ac3Slogwang * Variables for socket buffering.
781eaf0ac3Slogwang *
791eaf0ac3Slogwang * Locking key to struct sockbuf:
801eaf0ac3Slogwang * (a) locked by SOCKBUF_LOCK().
81*e2e96b59Sagerguo * (b) locked by sblock()
821eaf0ac3Slogwang */
831eaf0ac3Slogwang struct sockbuf {
841eaf0ac3Slogwang struct mtx sb_mtx; /* sockbuf lock */
851eaf0ac3Slogwang struct sx sb_sx; /* prevent I/O interlacing */
86*e2e96b59Sagerguo struct selinfo *sb_sel; /* process selecting read/write */
871eaf0ac3Slogwang short sb_state; /* (a) socket state on sockbuf */
881eaf0ac3Slogwang #define sb_startzero sb_mb
891eaf0ac3Slogwang struct mbuf *sb_mb; /* (a) the mbuf chain */
901eaf0ac3Slogwang struct mbuf *sb_mbtail; /* (a) the last mbuf in the chain */
911eaf0ac3Slogwang struct mbuf *sb_lastrecord; /* (a) first mbuf of last
921eaf0ac3Slogwang * record in socket buffer */
931eaf0ac3Slogwang struct mbuf *sb_sndptr; /* (a) pointer into mbuf chain */
941eaf0ac3Slogwang struct mbuf *sb_fnrdy; /* (a) pointer to first not ready buffer */
951eaf0ac3Slogwang u_int sb_sndptroff; /* (a) byte offset of ptr into chain */
961eaf0ac3Slogwang u_int sb_acc; /* (a) available chars in buffer */
971eaf0ac3Slogwang u_int sb_ccc; /* (a) claimed chars in buffer */
981eaf0ac3Slogwang u_int sb_hiwat; /* (a) max actual char count */
991eaf0ac3Slogwang u_int sb_mbcnt; /* (a) chars of mbufs used */
1001eaf0ac3Slogwang u_int sb_mcnt; /* (a) number of mbufs in buffer */
1011eaf0ac3Slogwang u_int sb_ccnt; /* (a) number of clusters in buffer */
1021eaf0ac3Slogwang u_int sb_mbmax; /* (a) max chars of mbufs to use */
1031eaf0ac3Slogwang u_int sb_ctl; /* (a) non-data chars in buffer */
104*e2e96b59Sagerguo u_int sb_tlscc; /* (a) TLS chain characters */
105*e2e96b59Sagerguo u_int sb_tlsdcc; /* (a) TLS characters being decrypted */
1061eaf0ac3Slogwang int sb_lowat; /* (a) low water mark */
1071eaf0ac3Slogwang sbintime_t sb_timeo; /* (a) timeout for read/write */
108*e2e96b59Sagerguo uint64_t sb_tls_seqno; /* (a) TLS seqno */
109*e2e96b59Sagerguo struct ktls_session *sb_tls_info; /* (a + b) TLS state */
110*e2e96b59Sagerguo struct mbuf *sb_mtls; /* (a) TLS mbuf chain */
111*e2e96b59Sagerguo struct mbuf *sb_mtlstail; /* (a) last mbuf in TLS chain */
112*e2e96b59Sagerguo short sb_flags; /* (a) flags, see above */
1131eaf0ac3Slogwang int (*sb_upcall)(struct socket *, void *, int); /* (a) */
1141eaf0ac3Slogwang void *sb_upcallarg; /* (a) */
1151eaf0ac3Slogwang TAILQ_HEAD(, kaiocb) sb_aiojobq; /* (a) pending AIO ops */
1161eaf0ac3Slogwang struct task sb_aiotask; /* AIO task */
1171eaf0ac3Slogwang };
1181eaf0ac3Slogwang
119*e2e96b59Sagerguo #endif /* defined(_KERNEL) || defined(_WANT_SOCKET) */
1201eaf0ac3Slogwang #ifdef _KERNEL
1211eaf0ac3Slogwang
1221eaf0ac3Slogwang /*
1231eaf0ac3Slogwang * Per-socket buffer mutex used to protect most fields in the socket
1241eaf0ac3Slogwang * buffer.
1251eaf0ac3Slogwang */
1261eaf0ac3Slogwang #define SOCKBUF_MTX(_sb) (&(_sb)->sb_mtx)
1271eaf0ac3Slogwang #define SOCKBUF_LOCK_INIT(_sb, _name) \
1281eaf0ac3Slogwang mtx_init(SOCKBUF_MTX(_sb), _name, NULL, MTX_DEF)
1291eaf0ac3Slogwang #define SOCKBUF_LOCK_DESTROY(_sb) mtx_destroy(SOCKBUF_MTX(_sb))
1301eaf0ac3Slogwang #define SOCKBUF_LOCK(_sb) mtx_lock(SOCKBUF_MTX(_sb))
1311eaf0ac3Slogwang #define SOCKBUF_OWNED(_sb) mtx_owned(SOCKBUF_MTX(_sb))
1321eaf0ac3Slogwang #define SOCKBUF_UNLOCK(_sb) mtx_unlock(SOCKBUF_MTX(_sb))
1331eaf0ac3Slogwang #define SOCKBUF_LOCK_ASSERT(_sb) mtx_assert(SOCKBUF_MTX(_sb), MA_OWNED)
1341eaf0ac3Slogwang #define SOCKBUF_UNLOCK_ASSERT(_sb) mtx_assert(SOCKBUF_MTX(_sb), MA_NOTOWNED)
1351eaf0ac3Slogwang
1361eaf0ac3Slogwang /*
1371eaf0ac3Slogwang * Socket buffer private mbuf(9) flags.
1381eaf0ac3Slogwang */
1391eaf0ac3Slogwang #define M_NOTREADY M_PROTO1 /* m_data not populated yet */
1401eaf0ac3Slogwang #define M_BLOCKED M_PROTO2 /* M_NOTREADY in front of m */
1411eaf0ac3Slogwang #define M_NOTAVAIL (M_NOTREADY | M_BLOCKED)
1421eaf0ac3Slogwang
1431eaf0ac3Slogwang void sbappend(struct sockbuf *sb, struct mbuf *m, int flags);
1441eaf0ac3Slogwang void sbappend_locked(struct sockbuf *sb, struct mbuf *m, int flags);
1451eaf0ac3Slogwang void sbappendstream(struct sockbuf *sb, struct mbuf *m, int flags);
1461eaf0ac3Slogwang void sbappendstream_locked(struct sockbuf *sb, struct mbuf *m, int flags);
1471eaf0ac3Slogwang int sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa,
1481eaf0ac3Slogwang struct mbuf *m0, struct mbuf *control);
1491eaf0ac3Slogwang int sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa,
1501eaf0ac3Slogwang struct mbuf *m0, struct mbuf *control);
1511eaf0ac3Slogwang int sbappendaddr_nospacecheck_locked(struct sockbuf *sb,
1521eaf0ac3Slogwang const struct sockaddr *asa, struct mbuf *m0, struct mbuf *control);
153*e2e96b59Sagerguo void sbappendcontrol(struct sockbuf *sb, struct mbuf *m0,
154*e2e96b59Sagerguo struct mbuf *control, int flags);
155*e2e96b59Sagerguo void sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0,
156*e2e96b59Sagerguo struct mbuf *control, int flags);
1571eaf0ac3Slogwang void sbappendrecord(struct sockbuf *sb, struct mbuf *m0);
1581eaf0ac3Slogwang void sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0);
1591eaf0ac3Slogwang void sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n);
1601eaf0ac3Slogwang struct mbuf *
1611eaf0ac3Slogwang sbcreatecontrol(caddr_t p, int size, int type, int level);
162*e2e96b59Sagerguo struct mbuf *
163*e2e96b59Sagerguo sbcreatecontrol_how(void *p, int size, int type, int level,
164*e2e96b59Sagerguo int wait);
1651eaf0ac3Slogwang void sbdestroy(struct sockbuf *sb, struct socket *so);
1661eaf0ac3Slogwang void sbdrop(struct sockbuf *sb, int len);
1671eaf0ac3Slogwang void sbdrop_locked(struct sockbuf *sb, int len);
1681eaf0ac3Slogwang struct mbuf *
1691eaf0ac3Slogwang sbcut_locked(struct sockbuf *sb, int len);
1701eaf0ac3Slogwang void sbdroprecord(struct sockbuf *sb);
1711eaf0ac3Slogwang void sbdroprecord_locked(struct sockbuf *sb);
1721eaf0ac3Slogwang void sbflush(struct sockbuf *sb);
1731eaf0ac3Slogwang void sbflush_locked(struct sockbuf *sb);
1741eaf0ac3Slogwang void sbrelease(struct sockbuf *sb, struct socket *so);
1751eaf0ac3Slogwang void sbrelease_internal(struct sockbuf *sb, struct socket *so);
1761eaf0ac3Slogwang void sbrelease_locked(struct sockbuf *sb, struct socket *so);
177*e2e96b59Sagerguo int sbsetopt(struct socket *so, int cmd, u_long cc);
1781eaf0ac3Slogwang int sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so,
1791eaf0ac3Slogwang struct thread *td);
180*e2e96b59Sagerguo void sbsndptr_adv(struct sockbuf *sb, struct mbuf *mb, u_int len);
1811eaf0ac3Slogwang struct mbuf *
182*e2e96b59Sagerguo sbsndptr_noadv(struct sockbuf *sb, u_int off, u_int *moff);
1831eaf0ac3Slogwang struct mbuf *
1841eaf0ac3Slogwang sbsndmbuf(struct sockbuf *sb, u_int off, u_int *moff);
1851eaf0ac3Slogwang int sbwait(struct sockbuf *sb);
1861eaf0ac3Slogwang int sblock(struct sockbuf *sb, int flags);
1871eaf0ac3Slogwang void sbunlock(struct sockbuf *sb);
1881eaf0ac3Slogwang void sballoc(struct sockbuf *, struct mbuf *);
1891eaf0ac3Slogwang void sbfree(struct sockbuf *, struct mbuf *);
190*e2e96b59Sagerguo void sballoc_ktls_rx(struct sockbuf *sb, struct mbuf *m);
191*e2e96b59Sagerguo void sbfree_ktls_rx(struct sockbuf *sb, struct mbuf *m);
1921eaf0ac3Slogwang int sbready(struct sockbuf *, struct mbuf *, int);
1931eaf0ac3Slogwang
1941eaf0ac3Slogwang /*
1951eaf0ac3Slogwang * Return how much data is available to be taken out of socket
1961eaf0ac3Slogwang * buffer right now.
1971eaf0ac3Slogwang */
1981eaf0ac3Slogwang static inline u_int
sbavail(struct sockbuf * sb)1991eaf0ac3Slogwang sbavail(struct sockbuf *sb)
2001eaf0ac3Slogwang {
2011eaf0ac3Slogwang
2021eaf0ac3Slogwang #if 0
2031eaf0ac3Slogwang SOCKBUF_LOCK_ASSERT(sb);
2041eaf0ac3Slogwang #endif
2051eaf0ac3Slogwang return (sb->sb_acc);
2061eaf0ac3Slogwang }
2071eaf0ac3Slogwang
2081eaf0ac3Slogwang /*
2091eaf0ac3Slogwang * Return how much data sits there in the socket buffer
2101eaf0ac3Slogwang * It might be that some data is not yet ready to be read.
2111eaf0ac3Slogwang */
2121eaf0ac3Slogwang static inline u_int
sbused(struct sockbuf * sb)2131eaf0ac3Slogwang sbused(struct sockbuf *sb)
2141eaf0ac3Slogwang {
2151eaf0ac3Slogwang
2161eaf0ac3Slogwang #if 0
2171eaf0ac3Slogwang SOCKBUF_LOCK_ASSERT(sb);
2181eaf0ac3Slogwang #endif
2191eaf0ac3Slogwang return (sb->sb_ccc);
2201eaf0ac3Slogwang }
2211eaf0ac3Slogwang
2221eaf0ac3Slogwang /*
2231eaf0ac3Slogwang * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
2241eaf0ac3Slogwang * This is problematical if the fields are unsigned, as the space might
2251eaf0ac3Slogwang * still be negative (ccc > hiwat or mbcnt > mbmax).
2261eaf0ac3Slogwang */
2271eaf0ac3Slogwang static inline long
sbspace(struct sockbuf * sb)2281eaf0ac3Slogwang sbspace(struct sockbuf *sb)
2291eaf0ac3Slogwang {
2301eaf0ac3Slogwang int bleft, mleft; /* size should match sockbuf fields */
2311eaf0ac3Slogwang
2321eaf0ac3Slogwang #if 0
2331eaf0ac3Slogwang SOCKBUF_LOCK_ASSERT(sb);
2341eaf0ac3Slogwang #endif
2351eaf0ac3Slogwang
2361eaf0ac3Slogwang if (sb->sb_flags & SB_STOP)
2371eaf0ac3Slogwang return(0);
2381eaf0ac3Slogwang
2391eaf0ac3Slogwang bleft = sb->sb_hiwat - sb->sb_ccc;
2401eaf0ac3Slogwang mleft = sb->sb_mbmax - sb->sb_mbcnt;
2411eaf0ac3Slogwang
2421eaf0ac3Slogwang return ((bleft < mleft) ? bleft : mleft);
2431eaf0ac3Slogwang }
2441eaf0ac3Slogwang
2451eaf0ac3Slogwang #define SB_EMPTY_FIXUP(sb) do { \
2461eaf0ac3Slogwang if ((sb)->sb_mb == NULL) { \
2471eaf0ac3Slogwang (sb)->sb_mbtail = NULL; \
2481eaf0ac3Slogwang (sb)->sb_lastrecord = NULL; \
2491eaf0ac3Slogwang } \
2501eaf0ac3Slogwang } while (/*CONSTCOND*/0)
2511eaf0ac3Slogwang
2521eaf0ac3Slogwang #ifdef SOCKBUF_DEBUG
2531eaf0ac3Slogwang void sblastrecordchk(struct sockbuf *, const char *, int);
2541eaf0ac3Slogwang void sblastmbufchk(struct sockbuf *, const char *, int);
2551eaf0ac3Slogwang void sbcheck(struct sockbuf *, const char *, int);
2561eaf0ac3Slogwang #define SBLASTRECORDCHK(sb) sblastrecordchk((sb), __FILE__, __LINE__)
2571eaf0ac3Slogwang #define SBLASTMBUFCHK(sb) sblastmbufchk((sb), __FILE__, __LINE__)
2581eaf0ac3Slogwang #define SBCHECK(sb) sbcheck((sb), __FILE__, __LINE__)
2591eaf0ac3Slogwang #else
2601eaf0ac3Slogwang #define SBLASTRECORDCHK(sb) do {} while (0)
2611eaf0ac3Slogwang #define SBLASTMBUFCHK(sb) do {} while (0)
2621eaf0ac3Slogwang #define SBCHECK(sb) do {} while (0)
2631eaf0ac3Slogwang #endif /* SOCKBUF_DEBUG */
2641eaf0ac3Slogwang
2651eaf0ac3Slogwang #endif /* _KERNEL */
2661eaf0ac3Slogwang
2671eaf0ac3Slogwang #endif /* _SYS_SOCKBUF_H_ */
268