xref: /illumos-gate/usr/src/uts/common/smbsrv/mbuf.h (revision 45818ee124adeaaf947698996b4f4c722afc6d1f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
28  * All rights reserved.
29  *
30  * Redistribution and use in source and binary forms, with or without
31  * modification, are permitted provided that the following conditions
32  * are met:
33  * 1. Redistributions of source code must retain the above copyright
34  *    notice, this list of conditions and the following disclaimer.
35  * 2. Redistributions in binary form must reproduce the above copyright
36  *    notice, this list of conditions and the following disclaimer in the
37  *    documentation and/or other materials provided with the distribution.
38  * 3. All advertising materials mentioning features or use of this software
39  *    must display the following acknowledgement:
40  *	This product includes software developed by the University of
41  *	California, Berkeley and its contributors.
42  * 4. Neither the name of the University nor the names of its contributors
43  *    may be used to endorse or promote products derived from this software
44  *    without specific prior written permission.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56  * SUCH DAMAGE.
57  *
58  */
59 
60 #ifndef _SMBSRV_MBUF_H
61 #define	_SMBSRV_MBUF_H
62 
63 /*
64  * PBSHORTCUT This file should be removed from the PB port but is required
65  * for now to get it to compile. This file has also been modified.
66  */
67 
68 #include <sys/types.h>
69 #include <sys/param.h>
70 #include <sys/list.h>
71 #include <smbsrv/string.h>
72 #include <smbsrv/alloc.h>
73 
74 #ifdef __cplusplus
75 extern "C" {
76 #endif
77 
78 #define	MSIZE		256
79 #define	MCLBYTES	2048
80 #define	MCLSHIFT	11
81 #define	MCLOFSET	(MCLBYTES - 1)
82 
83 #define	NBPG 4096
84 #define	CLBYTES		NBPG
85 #define	PB_PAGESIZE 4096
86 
87 /*
88  * Mbufs are of a single size, MSIZE (machine/machparam.h), which
89  * includes overhead.  An mbuf may add a single "mbuf cluster" of size
90  * MCLBYTES (also in machine/machparam.h), which has no additional overhead
91  * and is used instead of the internal data area; this is done when
92  * at least MINCLSIZE of data must be stored.
93  */
94 
95 #define	MLEN		(MSIZE - sizeof (struct m_hdr))	/* normal data len */
96 #define	MHLEN		(MLEN - sizeof (struct pkthdr))	/* data len w/pkthdr */
97 
98 #define	MINCLSIZE	(MHLEN + MLEN)	/* smallest amount to put in cluster */
99 
100 /*
101  * Macros for type conversion
102  * mtod(m,t) -	convert mbuf pointer to data pointer of correct type
103  */
104 #define	mtod(m, t)	((t)((m)->m_data))
105 
106 
107 /* header at beginning of each mbuf: */
108 struct m_hdr {
109 	struct	mbuf *mh_next;		/* next buffer in chain */
110 	struct	mbuf *mh_nextpkt;	/* next chain in queue/record */
111 	int	mh_len;			/* amount of data in this mbuf */
112 	caddr_t	mh_data;		/* location of data */
113 	short	mh_type;		/* type of data in this mbuf */
114 	short	mh_flags;		/* flags; see below */
115 };
116 
117 /* record/packet header in first mbuf of chain; valid if M_PKTHDR set */
118 struct	pkthdr {
119 	int	len;		/* total packet length */
120 };
121 
122 
123 /* XXX probably do not need m_ext */
124 
125 /* description of external storage mapped into mbuf, valid if M_EXT set */
126 struct m_ext {
127 	caddr_t	ext_buf;		/* start of buffer */
128 	int	(*ext_ref)();		/* refcount adjust function */
129 	uint_t	ext_size;		/* size of buffer, for ext_free */
130 };
131 
132 typedef struct mbuf {
133 	struct	m_hdr m_hdr;
134 	union {
135 		struct {
136 			struct	pkthdr MH_pkthdr;	/* M_PKTHDR set */
137 			union {
138 				struct	m_ext MH_ext;	/* M_EXT set */
139 				char	MH_databuf[MHLEN];
140 			} MH_dat;
141 		} MH;
142 		char	M_databuf[MLEN];		/* !M_PKTHDR, !M_EXT */
143 	} M_dat;
144 } mbuf_t;
145 
146 #define	m_next		m_hdr.mh_next
147 #define	m_len		m_hdr.mh_len
148 #define	m_data		m_hdr.mh_data
149 #define	m_type		m_hdr.mh_type
150 #define	m_flags		m_hdr.mh_flags
151 #define	m_nextpkt	m_hdr.mh_nextpkt
152 #define	m_act		m_nextpkt
153 #define	m_pkthdr	M_dat.MH.MH_pkthdr
154 #define	m_ext		M_dat.MH.MH_dat.MH_ext
155 #define	m_pktdat	M_dat.MH.MH_dat.MH_databuf
156 #define	m_dat		M_dat.M_databuf
157 
158 /* mbuf flags */
159 #define	M_EXT		0x0001	/* has associated external storage */
160 #define	M_PKTHDR	0x0002	/* start of record */
161 #define	M_EOR		0x0004	/* end of record */
162 
163 /* mbuf pkthdr flags, also in m_flags */
164 #define	M_BCAST		0x0100	/* send/received as link-level broadcast */
165 #define	M_MCAST		0x0200	/* send/received as link-level multicast */
166 
167 /* flags copied when copying m_pkthdr */
168 #define	M_COPYFLAGS	(M_PKTHDR|M_EOR|M_BCAST|M_MCAST)
169 
170 /* XXX probably only need MT_DATA */
171 
172 /* mbuf types */
173 #define	MT_FREE		0	/* should be on free list */
174 #define	MT_DATA		1	/* dynamic (data) allocation */
175 #define	MT_HEADER	2	/* packet header */
176 #define	MT_SOCKET	3	/* socket structure */
177 #define	MT_PCB		4	/* protocol control block */
178 #define	MT_RTABLE	5	/* routing tables */
179 #define	MT_HTABLE	6	/* IMP host tables */
180 #define	MT_ATABLE	7	/* address resolution tables */
181 #define	MT_SONAME	8	/* socket name */
182 #define	MT_SOOPTS	10	/* socket options */
183 #define	MT_FTABLE	11	/* fragment reassembly header */
184 #define	MT_RIGHTS	12	/* access rights */
185 #define	MT_IFADDR	13	/* interface address */
186 #define	MT_CONTROL	14	/* extra-data protocol message */
187 #define	MT_OOBDATA	15	/* expedited data  */
188 
189 /*
190  * flags to malloc: PBSHORTCUT
191  */
192 #define	M_WAITOK	0x0000
193 #define	M_NOWAIT	0x0001
194 
195 /* flags to m_get/MGET */
196 #define	M_DONTWAIT	M_NOWAIT
197 #define	M_WAIT		M_WAITOK
198 
199 
200 /*
201  * mbuf allocation/deallocation macros:
202  *
203  *	MGET(struct mbuf *m, int how, int type)
204  * allocates an mbuf and initializes it to contain internal data.
205  *
206  *	MGETHDR(struct mbuf *m, int how, int type)
207  * allocates an mbuf and initializes it to contain a packet header
208  * and internal data.
209  */
210 
211 #define	MGET(m, how, type) { \
212 	m = MEM_ZALLOC("mbuf", sizeof (struct mbuf)); \
213 	(m)->m_next = (struct mbuf *)NULL; \
214 	(m)->m_nextpkt = (struct mbuf *)NULL; \
215 	(m)->m_data = (m)->m_dat; \
216 	(m)->m_flags = 0; \
217 	(m)->m_type = (short)(type); \
218 }
219 
220 #define	MGETHDR(m, how, type) { \
221 	m = MEM_ZALLOC("mbuf", sizeof (struct mbuf)); \
222 	(m)->m_type = (MT_HEADER); \
223 	(m)->m_next = (struct mbuf *)NULL; \
224 	(m)->m_nextpkt = (struct mbuf *)NULL; \
225 	(m)->m_data = (m)->m_pktdat; \
226 	(m)->m_flags = M_PKTHDR; \
227 }
228 
229 extern	int mclref();
230 extern	int mclrefnoop();
231 #define	MCLGET(m, how) \
232 	{ \
233 		(m)->m_ext.ext_buf = MEM_ZALLOC("mbuf", MCLBYTES);	\
234 		(m)->m_data = (m)->m_ext.ext_buf;		\
235 		(m)->m_flags |= M_EXT;				\
236 		(m)->m_ext.ext_size = MCLBYTES;			\
237 		(m)->m_ext.ext_ref = mclref;			\
238 	}
239 
240 /*
241  * MFREE(struct mbuf *m, struct mbuf *nn)
242  * Free a single mbuf and associated external storage.
243  * Place the successor, if any, in nn.
244  */
245 #define	MFREE(m, nn) \
246 	{ \
247 		if ((m)->m_flags & M_EXT) {		    \
248 			(*((m)->m_ext.ext_ref))((m)->m_ext.ext_buf,	\
249 			    (m)->m_ext.ext_size, -1);			\
250 			(m)->m_ext.ext_buf = 0;				\
251 		}							\
252 		(nn) = (m)->m_next;					\
253 		(m)->m_next = 0;					\
254 		MEM_FREE("mbuf", m);					\
255 	}
256 
257 
258 
259 /*
260  * As above, for mbufs allocated with m_gethdr/MGETHDR
261  * or initialized by M_COPY_PKTHDR.
262  */
263 #define	MH_ALIGN(m, len) \
264 	{ (m)->m_data += (MHLEN - (len)) &~ (sizeof (int32_t) - 1); }
265 
266 #define	SMB_MBC_MAGIC		0x4D42435F
267 #define	SMB_MBC_VALID(p)	ASSERT((p)->mbc_magic == SMB_MBC_MAGIC)
268 
269 typedef struct mbuf_chain {
270 	uint32_t		mbc_magic;
271 	list_node_t		mbc_lnd;
272 	volatile uint32_t	flags;		/* Various flags */
273 	struct mbuf_chain	*shadow_of;	/* I'm shadowing someone */
274 	mbuf_t			*chain;		/* Start of chain */
275 	int32_t			max_bytes;	/* max # of bytes for chain */
276 	int32_t			chain_offset;	/* Current offset into chain */
277 } mbuf_chain_t;
278 
279 mbuf_t *m_free(mbuf_t *);
280 void m_freem(mbuf_t *);
281 int mbc_moveout(mbuf_chain_t *, caddr_t, int, int *);
282 void smb_mbc_init(void);
283 void smb_mbc_fini(void);
284 mbuf_chain_t *smb_mbc_alloc(uint32_t);
285 void smb_mbc_free(mbuf_chain_t *);
286 
287 #ifdef __cplusplus
288 }
289 #endif
290 
291 #endif /* _SMBSRV_MBUF_H */
292