xref: /freebsd/sys/sys/sockbuf.h (revision f05cddf9)
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$
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 
40 #define	SB_MAX		(2*1024*1024)	/* default for max chars in sockbuf */
41 
42 /*
43  * Constants for sb_flags field of struct sockbuf.
44  */
45 #define	SB_WAIT		0x04		/* someone is waiting for data/space */
46 #define	SB_SEL		0x08		/* someone is selecting */
47 #define	SB_ASYNC	0x10		/* ASYNC I/O, need signals */
48 #define	SB_UPCALL	0x20		/* someone wants an upcall */
49 #define	SB_NOINTR	0x40		/* operations not interruptible */
50 #define	SB_AIO		0x80		/* AIO operations queued */
51 #define	SB_KNOTE	0x100		/* kernel note attached */
52 #define	SB_NOCOALESCE	0x200		/* don't coalesce new data into existing mbufs */
53 #define	SB_IN_TOE	0x400		/* socket buffer is in the middle of an operation */
54 #define	SB_AUTOSIZE	0x800		/* automatically size socket buffer */
55 
56 #define	SBS_CANTSENDMORE	0x0010	/* can't send more data to peer */
57 #define	SBS_CANTRCVMORE		0x0020	/* can't receive more data from peer */
58 #define	SBS_RCVATMARK		0x0040	/* at mark on input */
59 
60 struct mbuf;
61 struct sockaddr;
62 struct socket;
63 struct thread;
64 
65 struct	xsockbuf {
66 	u_int	sb_cc;
67 	u_int	sb_hiwat;
68 	u_int	sb_mbcnt;
69 	u_int   sb_mcnt;
70 	u_int   sb_ccnt;
71 	u_int	sb_mbmax;
72 	int	sb_lowat;
73 	int	sb_timeo;
74 	short	sb_flags;
75 };
76 
77 /*
78  * Variables for socket buffering.
79  */
80 struct	sockbuf {
81 	struct	selinfo sb_sel;	/* process selecting read/write */
82 	struct	mtx sb_mtx;	/* sockbuf lock */
83 	struct	sx sb_sx;	/* prevent I/O interlacing */
84 	short	sb_state;	/* (c/d) socket state on sockbuf */
85 #define	sb_startzero	sb_mb
86 	struct	mbuf *sb_mb;	/* (c/d) the mbuf chain */
87 	struct	mbuf *sb_mbtail; /* (c/d) the last mbuf in the chain */
88 	struct	mbuf *sb_lastrecord;	/* (c/d) first mbuf of last
89 					 * record in socket buffer */
90 	struct	mbuf *sb_sndptr; /* (c/d) pointer into mbuf chain */
91 	u_int	sb_sndptroff;	/* (c/d) byte offset of ptr into chain */
92 	u_int	sb_cc;		/* (c/d) actual chars in buffer */
93 	u_int	sb_hiwat;	/* (c/d) max actual char count */
94 	u_int	sb_mbcnt;	/* (c/d) chars of mbufs used */
95 	u_int   sb_mcnt;        /* (c/d) number of mbufs in buffer */
96 	u_int   sb_ccnt;        /* (c/d) number of clusters in buffer */
97 	u_int	sb_mbmax;	/* (c/d) max chars of mbufs to use */
98 	u_int	sb_ctl;		/* (c/d) non-data chars in buffer */
99 	int	sb_lowat;	/* (c/d) low water mark */
100 	int	sb_timeo;	/* (c/d) timeout for read/write */
101 	short	sb_flags;	/* (c/d) flags, see below */
102 	int	(*sb_upcall)(struct socket *, void *, int); /* (c/d) */
103 	void	*sb_upcallarg;	/* (c/d) */
104 };
105 
106 #ifdef _KERNEL
107 
108 /*
109  * Per-socket buffer mutex used to protect most fields in the socket
110  * buffer.
111  */
112 #define	SOCKBUF_MTX(_sb)		(&(_sb)->sb_mtx)
113 #define	SOCKBUF_LOCK_INIT(_sb, _name) \
114 	mtx_init(SOCKBUF_MTX(_sb), _name, NULL, MTX_DEF)
115 #define	SOCKBUF_LOCK_DESTROY(_sb)	mtx_destroy(SOCKBUF_MTX(_sb))
116 #define	SOCKBUF_LOCK(_sb)		mtx_lock(SOCKBUF_MTX(_sb))
117 #define	SOCKBUF_OWNED(_sb)		mtx_owned(SOCKBUF_MTX(_sb))
118 #define	SOCKBUF_UNLOCK(_sb)		mtx_unlock(SOCKBUF_MTX(_sb))
119 #define	SOCKBUF_LOCK_ASSERT(_sb)	mtx_assert(SOCKBUF_MTX(_sb), MA_OWNED)
120 #define	SOCKBUF_UNLOCK_ASSERT(_sb)	mtx_assert(SOCKBUF_MTX(_sb), MA_NOTOWNED)
121 
122 void	sbappend(struct sockbuf *sb, struct mbuf *m);
123 void	sbappend_locked(struct sockbuf *sb, struct mbuf *m);
124 void	sbappendstream(struct sockbuf *sb, struct mbuf *m);
125 void	sbappendstream_locked(struct sockbuf *sb, struct mbuf *m);
126 int	sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa,
127 	    struct mbuf *m0, struct mbuf *control);
128 int	sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa,
129 	    struct mbuf *m0, struct mbuf *control);
130 int	sbappendcontrol(struct sockbuf *sb, struct mbuf *m0,
131 	    struct mbuf *control);
132 int	sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0,
133 	    struct mbuf *control);
134 void	sbappendrecord(struct sockbuf *sb, struct mbuf *m0);
135 void	sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0);
136 void	sbcheck(struct sockbuf *sb);
137 void	sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n);
138 struct mbuf *
139 	sbcreatecontrol(caddr_t p, int size, int type, int level);
140 void	sbdestroy(struct sockbuf *sb, struct socket *so);
141 void	sbdrop(struct sockbuf *sb, int len);
142 void	sbdrop_locked(struct sockbuf *sb, int len);
143 void	sbdroprecord(struct sockbuf *sb);
144 void	sbdroprecord_locked(struct sockbuf *sb);
145 void	sbflush(struct sockbuf *sb);
146 void	sbflush_locked(struct sockbuf *sb);
147 void	sbrelease(struct sockbuf *sb, struct socket *so);
148 void	sbrelease_internal(struct sockbuf *sb, struct socket *so);
149 void	sbrelease_locked(struct sockbuf *sb, struct socket *so);
150 int	sbreserve(struct sockbuf *sb, u_long cc, struct socket *so,
151 	    struct thread *td);
152 int	sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so,
153 	    struct thread *td);
154 struct mbuf *
155 	sbsndptr(struct sockbuf *sb, u_int off, u_int len, u_int *moff);
156 void	sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb);
157 int	sbwait(struct sockbuf *sb);
158 int	sblock(struct sockbuf *sb, int flags);
159 void	sbunlock(struct sockbuf *sb);
160 
161 /*
162  * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
163  * This is problematical if the fields are unsigned, as the space might
164  * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
165  * overflow and return 0.  Should use "lmin" but it doesn't exist now.
166  */
167 #define	sbspace(sb) \
168     ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \
169 	 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
170 
171 /* adjust counters in sb reflecting allocation of m */
172 #define	sballoc(sb, m) { \
173 	(sb)->sb_cc += (m)->m_len; \
174 	if ((m)->m_type != MT_DATA && (m)->m_type != MT_OOBDATA) \
175 		(sb)->sb_ctl += (m)->m_len; \
176 	(sb)->sb_mbcnt += MSIZE; \
177 	(sb)->sb_mcnt += 1; \
178 	if ((m)->m_flags & M_EXT) { \
179 		(sb)->sb_mbcnt += (m)->m_ext.ext_size; \
180 		(sb)->sb_ccnt += 1; \
181 	} \
182 }
183 
184 /* adjust counters in sb reflecting freeing of m */
185 #define	sbfree(sb, m) { \
186 	(sb)->sb_cc -= (m)->m_len; \
187 	if ((m)->m_type != MT_DATA && (m)->m_type != MT_OOBDATA) \
188 		(sb)->sb_ctl -= (m)->m_len; \
189 	(sb)->sb_mbcnt -= MSIZE; \
190 	(sb)->sb_mcnt -= 1; \
191 	if ((m)->m_flags & M_EXT) { \
192 		(sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
193 		(sb)->sb_ccnt -= 1; \
194 	} \
195 	if ((sb)->sb_sndptr == (m)) { \
196 		(sb)->sb_sndptr = NULL; \
197 		(sb)->sb_sndptroff = 0; \
198 	} \
199 	if ((sb)->sb_sndptroff != 0) \
200 		(sb)->sb_sndptroff -= (m)->m_len; \
201 }
202 
203 #define SB_EMPTY_FIXUP(sb) do {						\
204 	if ((sb)->sb_mb == NULL) {					\
205 		(sb)->sb_mbtail = NULL;					\
206 		(sb)->sb_lastrecord = NULL;				\
207 	}								\
208 } while (/*CONSTCOND*/0)
209 
210 #ifdef SOCKBUF_DEBUG
211 void	sblastrecordchk(struct sockbuf *, const char *, int);
212 #define	SBLASTRECORDCHK(sb)	sblastrecordchk((sb), __FILE__, __LINE__)
213 
214 void	sblastmbufchk(struct sockbuf *, const char *, int);
215 #define	SBLASTMBUFCHK(sb)	sblastmbufchk((sb), __FILE__, __LINE__)
216 #else
217 #define	SBLASTRECORDCHK(sb)      /* nothing */
218 #define	SBLASTMBUFCHK(sb)        /* nothing */
219 #endif /* SOCKBUF_DEBUG */
220 
221 #endif /* _KERNEL */
222 
223 #endif /* _SYS_SOCKBUF_H_ */
224