xref: /dragonfly/sys/sys/sockbuf.h (revision 03517d4e)
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  * 3. 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  * $FreeBSD: src/sys/sys/socketvar.h,v 1.46.2.10 2003/08/24 08:24:39 hsu Exp $
31  * $DragonFly: src/sys/sys/sockbuf.h,v 1.1 2007/04/22 01:13:17 dillon Exp $
32  */
33 
34 #ifndef _SYS_SOCKBUF_H_
35 #define _SYS_SOCKBUF_H_
36 
37 #ifndef _SYS_TYPES_H_
38 #include <sys/types.h>
39 #endif
40 
41 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
42 
43 /*
44  * Generic socket buffer for keeping track of mbuf chains.  These
45  * are used primarily to manipulate mbuf chains in standalone pieces
46  * of code.
47  */
48 struct sockbuf {
49 	u_long	sb_cc;		/* actual chars in buffer */
50 	u_long	sb_mbcnt;	/* chars of mbufs used */
51 	u_long	sb_cc_prealloc;
52 	u_long	sb_mbcnt_prealloc;
53 	u_long	sb_climit;	/* data limit when used for I/O */
54 	struct	mbuf *sb_mb;	/* the mbuf chain */
55 	struct	mbuf *sb_lastmbuf;	/* last mbuf in sb_mb */
56 	struct	mbuf *sb_lastrecord;	/* last record in sb_mb
57 					 * valid <=> sb_mb non-NULL */
58 };
59 
60 #define SB_MAX		(512*1024)	/* default for max chars in sockbuf */
61 
62 #endif
63 
64 #ifdef _KERNEL
65 
66 #include <machine/atomic.h>
67 #ifndef _SYS_MBUF_H_
68 #include <sys/mbuf.h>
69 #endif
70 
71 /*
72  * Macros for sockets and socket buffering.
73  */
74 
75 #ifdef SOCKBUF_DEBUG
76 #define sbcheck(sb)	_sbcheck(sb)
77 #else
78 #define sbcheck(sb)
79 #endif
80 
81 /* adjust counters in sb reflecting allocation of m */
82 #define	sballoc(sb, m) { \
83 	(sb)->sb_cc += (m)->m_len; \
84 	(sb)->sb_mbcnt += MSIZE; \
85 	if ((m)->m_flags & M_EXT) \
86 		(sb)->sb_mbcnt += (m)->m_ext.ext_size; \
87 }
88 
89 /* adjust counters in sb reflecting allocation of m */
90 #define	sbprealloc(sb, m) { \
91 	u_long __mbcnt_sz; \
92  \
93 	atomic_add_long(&((sb)->sb_cc_prealloc), (m)->m_len); \
94  \
95 	__mbcnt_sz = MSIZE; \
96 	if ((m)->m_flags & M_EXT) \
97 		__mbcnt_sz += (m)->m_ext.ext_size; \
98 	atomic_add_long(&((sb)->sb_mbcnt_prealloc), __mbcnt_sz); \
99 }
100 
101 /* adjust counters in sb reflecting freeing of m */
102 #define	sbfree(sb, m) { \
103 	u_long __mbcnt_sz; \
104  \
105 	(sb)->sb_cc -= (m)->m_len; \
106 	atomic_subtract_long(&((sb)->sb_cc_prealloc), (m)->m_len); \
107  \
108 	__mbcnt_sz = MSIZE; \
109 	if ((m)->m_flags & M_EXT) \
110 		__mbcnt_sz += (m)->m_ext.ext_size; \
111 	(sb)->sb_mbcnt -= __mbcnt_sz; \
112 	atomic_subtract_long(&((sb)->sb_mbcnt_prealloc), __mbcnt_sz); \
113 }
114 
115 static __inline void
116 sbinit(struct sockbuf *sb, u_long climit)
117 {
118 	sb->sb_cc = 0;
119 	sb->sb_mbcnt = 0;
120 	sb->sb_cc_prealloc = 0;
121 	sb->sb_mbcnt_prealloc = 0;
122 	sb->sb_climit = climit;
123 	sb->sb_mb = NULL;
124 	sb->sb_lastmbuf = NULL;
125 	sb->sb_lastrecord = NULL;
126 }
127 
128 void	sbappend (struct sockbuf *sb, struct mbuf *m);
129 int	sbappendaddr (struct sockbuf *sb, const struct sockaddr *asa,
130 	    struct mbuf *m0, struct mbuf *control);
131 int	sbappendcontrol (struct sockbuf *sb, struct mbuf *m0,
132 	    struct mbuf *control);
133 void	sbappendrecord (struct sockbuf *sb, struct mbuf *m0);
134 void	sbappendstream (struct sockbuf *sb, struct mbuf *m);
135 void	_sbcheck (struct sockbuf *sb);
136 void	sbcompress (struct sockbuf *sb, struct mbuf *m, struct mbuf *n);
137 struct mbuf *
138 	sbcreatecontrol (const void *p, size_t size, int type, int level);
139 void	sbdrop (struct sockbuf *sb, int len);
140 void	sbdroprecord (struct sockbuf *sb);
141 struct mbuf *
142 	sbunlinkmbuf (struct sockbuf *, struct mbuf *, struct mbuf **);
143 void	sbflush (struct sockbuf *sb);
144 
145 #endif /* _KERNEL */
146 
147 #endif /* !_SYS_SOCKBUF_H_ */
148