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.
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.
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]
22 * Copyright 2019 Joyent, Inc.
23 * Copyright 2011-2021 Tintri by DDN, Inc. All rights reserved.
24 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
29 * All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 #ifndef _SMBSRV_MBUF_H
62 #define _SMBSRV_MBUF_H
65 * This mbuf simulation should be replaced with (native) mblk_t support.
68 #include <sys/types.h>
69 #include <sys/param.h>
71 #include <smbsrv/string.h>
81 * Mbufs are of a single size, MSIZE (machine/machparam.h), which
82 * includes overhead. An mbuf may add a single "mbuf cluster" of size
83 * MCLBYTES (also in machine/machparam.h), which has no additional overhead
84 * and is used instead of the internal data area; this is done when
85 * at least MINCLSIZE of data must be stored.
88 #define MLEN (MSIZE - sizeof (struct m_hdr)) /* normal data len */
89 #define MHLEN (MLEN - sizeof (struct pkthdr)) /* data len w/pkthdr */
91 #define MINCLSIZE (MHLEN + MLEN) /* smallest amount to put in cluster */
94 * How much prepend space to put in the first mbuf of a chain.
95 * In SMB we only prepend a 4-byte NBT header, but let's keep the
96 * data part aligned the same as M_ALIGN() does below (8-byte)
98 #define MH_PREPEND_SPACE 8
101 * Macros for type conversion
102 * mtod(m,t) - convert mbuf pointer to data pointer of correct type
104 #define mtod(m, t) ((t)((m)->m_data))
107 typedef void m_ext_free_t(struct mbuf
*);
109 /* header at beginning of each mbuf: */
111 struct mbuf
*mh_next
; /* next buffer in chain */
112 struct mbuf
*mh_nextpkt
; /* next chain in queue/record */
113 int mh_len
; /* amount of data in this mbuf */
114 caddr_t mh_data
; /* location of data */
115 short mh_type
; /* type of data in this mbuf */
116 short mh_flags
; /* flags; see below */
119 /* record/packet header in first mbuf of chain; valid if M_PKTHDR set */
121 int len
; /* total packet length */
125 /* description of external storage mapped into mbuf, valid if M_EXT set */
127 caddr_t ext_buf
; /* start of external buffer */
128 uint_t ext_size
; /* size of external buffer */
129 m_ext_free_t
*ext_free
; /* free function */
130 void *ext_arg1
; /* free func arg */
133 typedef struct mbuf
{
137 struct pkthdr MH_pkthdr
; /* M_PKTHDR set */
139 struct m_ext MH_ext
; /* M_EXT set */
140 char MH_databuf
[MHLEN
];
143 char M_databuf
[MLEN
]; /* !M_PKTHDR, !M_EXT */
147 #define m_next m_hdr.mh_next
148 #define m_len m_hdr.mh_len
149 #define m_data m_hdr.mh_data
150 #define m_type m_hdr.mh_type
151 #define m_flags m_hdr.mh_flags
152 #define m_nextpkt m_hdr.mh_nextpkt
153 #define m_act m_nextpkt
154 #define m_pkthdr M_dat.MH.MH_pkthdr
155 #define m_ext M_dat.MH.MH_dat.MH_ext
156 #define m_pktdat M_dat.MH.MH_dat.MH_databuf
157 #define m_dat M_dat.M_databuf
160 #define M_EXT 0x0001 /* has associated external storage */
161 #define M_PKTHDR 0x0002 /* start of record */
162 #define M_EOR 0x0004 /* end of record */
164 /* mbuf pkthdr flags, also in m_flags */
165 #define M_BCAST 0x0100 /* send/received as link-level broadcast */
166 #define M_MCAST 0x0200 /* send/received as link-level multicast */
168 /* flags copied when copying m_pkthdr */
169 #define M_COPYFLAGS (M_PKTHDR|M_EOR|M_BCAST|M_MCAST)
171 /* XXX probably only need MT_DATA */
174 #define MT_FREE 0 /* should be on free list */
175 #define MT_DATA 1 /* dynamic (data) allocation */
176 #define MT_HEADER 2 /* packet header */
177 #define MT_SOCKET 3 /* socket structure */
178 #define MT_PCB 4 /* protocol control block */
179 #define MT_RTABLE 5 /* routing tables */
180 #define MT_HTABLE 6 /* IMP host tables */
181 #define MT_ATABLE 7 /* address resolution tables */
182 #define MT_SONAME 8 /* socket name */
183 #define MT_SOOPTS 10 /* socket options */
184 #define MT_FTABLE 11 /* fragment reassembly header */
185 #define MT_RIGHTS 12 /* access rights */
186 #define MT_IFADDR 13 /* interface address */
187 #define MT_CONTROL 14 /* extra-data protocol message */
188 #define MT_OOBDATA 15 /* expedited data */
191 * flags to malloc: PBSHORTCUT
193 #define M_WAITOK 0x0000
194 #define M_NOWAIT 0x0001
196 /* flags to m_get/MGET */
197 #define M_DONTWAIT M_NOWAIT
198 #define M_WAIT M_WAITOK
203 * mbuf allocation/deallocation macros:
205 * MGET(struct mbuf *m, int how, int type)
206 * allocates an mbuf and initializes it to contain internal data.
208 * MGETHDR(struct mbuf *m, int how, int type)
209 * allocates an mbuf and initializes it to contain a packet header
213 #define MGET(m, how, type) { \
214 m = smb_mbuf_alloc(); \
215 (m)->m_next = (struct mbuf *)NULL; \
216 (m)->m_nextpkt = (struct mbuf *)NULL; \
217 (m)->m_data = (m)->m_dat; \
219 (m)->m_type = (short)(type); \
222 #define MGETHDR(m, how, type) { \
223 m = smb_mbuf_alloc(); \
224 (m)->m_type = (MT_HEADER); \
225 (m)->m_next = (struct mbuf *)NULL; \
226 (m)->m_nextpkt = (struct mbuf *)NULL; \
227 (m)->m_data = (m)->m_pktdat; \
228 (m)->m_flags = M_PKTHDR; \
231 #define MCLGET(m, how) \
233 (m)->m_ext.ext_buf = smb_mbufcl_alloc(); \
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_free = smb_mbufcl_free; \
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.
245 #define MFREE(m, nn) \
247 if ((m)->m_flags & M_EXT) { \
248 (m)->m_ext.ext_free(m); \
249 (m)->m_ext.ext_buf = NULL; \
251 (nn) = (m)->m_next; \
257 * Set the m_data pointer of a newly allocated mbuf to place an object of the
258 * specified size at the end of the mbuf, longword aligned.
260 #define M_ALIGN(m, len) \
261 { (m)->m_data += (MLEN - (len)) &~ (sizeof (int64_t) - 1); }
264 * As above, for mbufs allocated with m_gethdr/MGETHDR
265 * or initialized by M_COPY_PKTHDR.
267 #define MH_ALIGN(m, len) \
268 { (m)->m_data += (MHLEN - (len)) &~ (sizeof (int64_t) - 1); }
271 * Return the address of the start of the buffer associated with an mbuf,
272 * handling external storage, packet-header mbufs, and regular data mbufs.
273 * BSD calls this M_START but that conflicts with stream.h (sigh)
275 #define MB_START(m) \
276 (((m)->m_flags & M_EXT) ? (m)->m_ext.ext_buf : \
277 ((m)->m_flags & M_PKTHDR) ? &(m)->m_pktdat[0] : \
281 * Return the size of the buffer associated with an mbuf, handling external
282 * storage, packet-header mbufs, and regular data mbufs.
285 (((m)->m_flags & M_EXT) ? (m)->m_ext.ext_size : \
286 ((m)->m_flags & M_PKTHDR) ? MHLEN : MLEN)
289 * Compute the amount of space available before the current start of data in
292 #define M_LEADINGSPACE(m) ((m)->m_data - MB_START(m))
295 * Compute the amount of space available after the end of data in an mbuf.
297 #define M_TRAILINGSPACE(m) \
298 ((MB_START(m) + M_SIZE(m)) - ((m)->m_data + (m)->m_len))
302 #define SMB_MBC_MAGIC 0x4D42435F
303 #define SMB_MBC_VALID(p) ASSERT((p)->mbc_magic == SMB_MBC_MAGIC)
305 typedef struct mbuf_chain
{
307 volatile uint32_t flags
; /* Various flags */
308 struct mbuf_chain
*shadow_of
; /* I'm shadowing someone */
309 mbuf_t
*chain
; /* Start of chain */
310 int32_t max_bytes
; /* max # of bytes for chain */
311 int32_t chain_offset
; /* Current offset into chain */
314 mbuf_t
*smb_mbuf_alloc_ext(caddr_t
, int, m_ext_free_t
, void *);
316 mbuf_t
*smb_mbuf_alloc(void);
317 void smb_mbuf_free(mbuf_t
*);
319 void *smb_mbufcl_alloc(void);
320 void smb_mbufcl_free(mbuf_t
*);
322 mbuf_t
*m_prepend(struct mbuf
*m
, int plen
, int how
);
323 void m_adjust(mbuf_t
*, int);
324 mbuf_t
*m_free(mbuf_t
*);
325 void m_freem(mbuf_t
*);
326 void smb_mbc_init(void);
327 void smb_mbc_fini(void);
328 mbuf_chain_t
*smb_mbc_alloc(uint32_t);
329 void smb_mbc_free(mbuf_chain_t
*);
335 #endif /* _SMBSRV_MBUF_H */