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