sockbuf.h revision 331722
1/*-
2 * Copyright (c) 1982, 1986, 1990, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	@(#)socketvar.h	8.3 (Berkeley) 2/19/95
30 *
31 * $FreeBSD: stable/11/sys/sys/sockbuf.h 331722 2018-03-29 02:50:57Z eadler $
32 */
33#ifndef _SYS_SOCKBUF_H_
34#define _SYS_SOCKBUF_H_
35#include <sys/selinfo.h>		/* for struct selinfo */
36#include <sys/_lock.h>
37#include <sys/_mutex.h>
38#include <sys/_sx.h>
39#include <sys/_task.h>
40
41#define	SB_MAX		(2*1024*1024)	/* default for max chars in sockbuf */
42
43/*
44 * Constants for sb_flags field of struct sockbuf.
45 */
46#define	SB_WAIT		0x04		/* someone is waiting for data/space */
47#define	SB_SEL		0x08		/* someone is selecting */
48#define	SB_ASYNC	0x10		/* ASYNC I/O, need signals */
49#define	SB_UPCALL	0x20		/* someone wants an upcall */
50#define	SB_NOINTR	0x40		/* operations not interruptible */
51#define	SB_AIO		0x80		/* AIO operations queued */
52#define	SB_KNOTE	0x100		/* kernel note attached */
53#define	SB_NOCOALESCE	0x200		/* don't coalesce new data into existing mbufs */
54#define	SB_IN_TOE	0x400		/* socket buffer is in the middle of an operation */
55#define	SB_AUTOSIZE	0x800		/* automatically size socket buffer */
56#define	SB_STOP		0x1000		/* backpressure indicator */
57#define	SB_AIO_RUNNING	0x2000		/* AIO operation running */
58
59#define	SBS_CANTSENDMORE	0x0010	/* can't send more data to peer */
60#define	SBS_CANTRCVMORE		0x0020	/* can't receive more data from peer */
61#define	SBS_RCVATMARK		0x0040	/* at mark on input */
62
63struct mbuf;
64struct sockaddr;
65struct socket;
66struct thread;
67
68struct	xsockbuf {
69	u_int	sb_cc;
70	u_int	sb_hiwat;
71	u_int	sb_mbcnt;
72	u_int   sb_mcnt;
73	u_int   sb_ccnt;
74	u_int	sb_mbmax;
75	int	sb_lowat;
76	int	sb_timeo;
77	short	sb_flags;
78};
79
80/*
81 * Variables for socket buffering.
82 *
83 * Locking key to struct sockbuf:
84 * (a) locked by SOCKBUF_LOCK().
85 */
86struct	sockbuf {
87	struct	selinfo sb_sel;	/* process selecting read/write */
88	struct	mtx sb_mtx;	/* sockbuf lock */
89	struct	sx sb_sx;	/* prevent I/O interlacing */
90	short	sb_state;	/* (a) socket state on sockbuf */
91#define	sb_startzero	sb_mb
92	struct	mbuf *sb_mb;	/* (a) the mbuf chain */
93	struct	mbuf *sb_mbtail; /* (a) the last mbuf in the chain */
94	struct	mbuf *sb_lastrecord;	/* (a) first mbuf of last
95					 * record in socket buffer */
96	struct	mbuf *sb_sndptr; /* (a) pointer into mbuf chain */
97	struct	mbuf *sb_fnrdy;	/* (a) pointer to first not ready buffer */
98	u_int	sb_sndptroff;	/* (a) byte offset of ptr into chain */
99	u_int	sb_acc;		/* (a) available chars in buffer */
100	u_int	sb_ccc;		/* (a) claimed chars in buffer */
101	u_int	sb_hiwat;	/* (a) max actual char count */
102	u_int	sb_mbcnt;	/* (a) chars of mbufs used */
103	u_int   sb_mcnt;        /* (a) number of mbufs in buffer */
104	u_int   sb_ccnt;        /* (a) number of clusters in buffer */
105	u_int	sb_mbmax;	/* (a) max chars of mbufs to use */
106	u_int	sb_ctl;		/* (a) non-data chars in buffer */
107	int	sb_lowat;	/* (a) low water mark */
108	sbintime_t	sb_timeo;	/* (a) timeout for read/write */
109	short	sb_flags;	/* (a) flags, see below */
110	int	(*sb_upcall)(struct socket *, void *, int); /* (a) */
111	void	*sb_upcallarg;	/* (a) */
112	TAILQ_HEAD(, kaiocb) sb_aiojobq; /* (a) pending AIO ops */
113	struct	task sb_aiotask; /* AIO task */
114};
115
116#ifdef _KERNEL
117
118/*
119 * Per-socket buffer mutex used to protect most fields in the socket
120 * buffer.
121 */
122#define	SOCKBUF_MTX(_sb)		(&(_sb)->sb_mtx)
123#define	SOCKBUF_LOCK_INIT(_sb, _name) \
124	mtx_init(SOCKBUF_MTX(_sb), _name, NULL, MTX_DEF)
125#define	SOCKBUF_LOCK_DESTROY(_sb)	mtx_destroy(SOCKBUF_MTX(_sb))
126#define	SOCKBUF_LOCK(_sb)		mtx_lock(SOCKBUF_MTX(_sb))
127#define	SOCKBUF_OWNED(_sb)		mtx_owned(SOCKBUF_MTX(_sb))
128#define	SOCKBUF_UNLOCK(_sb)		mtx_unlock(SOCKBUF_MTX(_sb))
129#define	SOCKBUF_LOCK_ASSERT(_sb)	mtx_assert(SOCKBUF_MTX(_sb), MA_OWNED)
130#define	SOCKBUF_UNLOCK_ASSERT(_sb)	mtx_assert(SOCKBUF_MTX(_sb), MA_NOTOWNED)
131
132/*
133 * Socket buffer private mbuf(9) flags.
134 */
135#define	M_NOTREADY	M_PROTO1	/* m_data not populated yet */
136#define	M_BLOCKED	M_PROTO2	/* M_NOTREADY in front of m */
137#define	M_NOTAVAIL	(M_NOTREADY | M_BLOCKED)
138
139void	sbappend(struct sockbuf *sb, struct mbuf *m, int flags);
140void	sbappend_locked(struct sockbuf *sb, struct mbuf *m, int flags);
141void	sbappendstream(struct sockbuf *sb, struct mbuf *m, int flags);
142void	sbappendstream_locked(struct sockbuf *sb, struct mbuf *m, int flags);
143int	sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa,
144	    struct mbuf *m0, struct mbuf *control);
145int	sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa,
146	    struct mbuf *m0, struct mbuf *control);
147int	sbappendaddr_nospacecheck_locked(struct sockbuf *sb,
148	    const struct sockaddr *asa, struct mbuf *m0, struct mbuf *control);
149int	sbappendcontrol(struct sockbuf *sb, struct mbuf *m0,
150	    struct mbuf *control);
151int	sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0,
152	    struct mbuf *control);
153void	sbappendrecord(struct sockbuf *sb, struct mbuf *m0);
154void	sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0);
155void	sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n);
156struct mbuf *
157	sbcreatecontrol(caddr_t p, int size, int type, int level);
158void	sbdestroy(struct sockbuf *sb, struct socket *so);
159void	sbdrop(struct sockbuf *sb, int len);
160void	sbdrop_locked(struct sockbuf *sb, int len);
161struct mbuf *
162	sbcut_locked(struct sockbuf *sb, int len);
163void	sbdroprecord(struct sockbuf *sb);
164void	sbdroprecord_locked(struct sockbuf *sb);
165void	sbflush(struct sockbuf *sb);
166void	sbflush_locked(struct sockbuf *sb);
167void	sbrelease(struct sockbuf *sb, struct socket *so);
168void	sbrelease_internal(struct sockbuf *sb, struct socket *so);
169void	sbrelease_locked(struct sockbuf *sb, struct socket *so);
170int	sbreserve(struct sockbuf *sb, u_long cc, struct socket *so,
171	    struct thread *td);
172int	sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so,
173	    struct thread *td);
174struct mbuf *
175	sbsndptr(struct sockbuf *sb, u_int off, u_int len, u_int *moff);
176struct mbuf *
177	sbsndmbuf(struct sockbuf *sb, u_int off, u_int *moff);
178void	sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb);
179int	sbwait(struct sockbuf *sb);
180int	sblock(struct sockbuf *sb, int flags);
181void	sbunlock(struct sockbuf *sb);
182void	sballoc(struct sockbuf *, struct mbuf *);
183void	sbfree(struct sockbuf *, struct mbuf *);
184int	sbready(struct sockbuf *, struct mbuf *, int);
185
186/*
187 * Return how much data is available to be taken out of socket
188 * buffer right now.
189 */
190static inline u_int
191sbavail(struct sockbuf *sb)
192{
193
194#if 0
195	SOCKBUF_LOCK_ASSERT(sb);
196#endif
197	return (sb->sb_acc);
198}
199
200/*
201 * Return how much data sits there in the socket buffer
202 * It might be that some data is not yet ready to be read.
203 */
204static inline u_int
205sbused(struct sockbuf *sb)
206{
207
208#if 0
209	SOCKBUF_LOCK_ASSERT(sb);
210#endif
211	return (sb->sb_ccc);
212}
213
214/*
215 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
216 * This is problematical if the fields are unsigned, as the space might
217 * still be negative (ccc > hiwat or mbcnt > mbmax).
218 */
219static inline long
220sbspace(struct sockbuf *sb)
221{
222	int bleft, mleft;		/* size should match sockbuf fields */
223
224#if 0
225	SOCKBUF_LOCK_ASSERT(sb);
226#endif
227
228	if (sb->sb_flags & SB_STOP)
229		return(0);
230
231	bleft = sb->sb_hiwat - sb->sb_ccc;
232	mleft = sb->sb_mbmax - sb->sb_mbcnt;
233
234	return ((bleft < mleft) ? bleft : mleft);
235}
236
237#define SB_EMPTY_FIXUP(sb) do {						\
238	if ((sb)->sb_mb == NULL) {					\
239		(sb)->sb_mbtail = NULL;					\
240		(sb)->sb_lastrecord = NULL;				\
241	}								\
242} while (/*CONSTCOND*/0)
243
244#ifdef SOCKBUF_DEBUG
245void	sblastrecordchk(struct sockbuf *, const char *, int);
246void	sblastmbufchk(struct sockbuf *, const char *, int);
247void	sbcheck(struct sockbuf *, const char *, int);
248#define	SBLASTRECORDCHK(sb)	sblastrecordchk((sb), __FILE__, __LINE__)
249#define	SBLASTMBUFCHK(sb)	sblastmbufchk((sb), __FILE__, __LINE__)
250#define	SBCHECK(sb)		sbcheck((sb), __FILE__, __LINE__)
251#else
252#define	SBLASTRECORDCHK(sb)	do {} while (0)
253#define	SBLASTMBUFCHK(sb)	do {} while (0)
254#define	SBCHECK(sb)		do {} while (0)
255#endif /* SOCKBUF_DEBUG */
256
257#endif /* _KERNEL */
258
259#endif /* _SYS_SOCKBUF_H_ */
260