xref: /titanic_52/usr/src/uts/common/smbsrv/mbuf.h (revision b6c3f7863936abeae522e48a13887dddeb691a45)
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 2007 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
63 
64 /*
65  * PBSHORTCUT This file should be removed from the PB port but is required
66  * for now to get it to compile. This file has also been modified.
67  */
68 
69 #include <sys/types.h>
70 #include <sys/param.h>
71 #include <smbsrv/smb_i18n.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 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 };
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 #ifdef __cplusplus
266 }
267 #endif
268 
269 #endif /* _SMBSRV_MBUF_H */
270