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