xref: /onnv-gate/usr/src/uts/common/smbsrv/mbuf.h (revision 6496:76a38b87ee13)
15331Samw /*
25331Samw  * CDDL HEADER START
35331Samw  *
45331Samw  * The contents of this file are subject to the terms of the
55331Samw  * Common Development and Distribution License (the "License").
65331Samw  * You may not use this file except in compliance with the License.
75331Samw  *
85331Samw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95331Samw  * or http://www.opensolaris.org/os/licensing.
105331Samw  * See the License for the specific language governing permissions
115331Samw  * and limitations under the License.
125331Samw  *
135331Samw  * When distributing Covered Code, include this CDDL HEADER in each
145331Samw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155331Samw  * If applicable, add the following below this CDDL HEADER, with the
165331Samw  * fields enclosed by brackets "[]" replaced with your own identifying
175331Samw  * information: Portions Copyright [yyyy] [name of copyright owner]
185331Samw  *
195331Samw  * CDDL HEADER END
205331Samw  */
215331Samw /*
22*6496Sjb150015  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
235331Samw  * Use is subject to license terms.
245331Samw  */
255331Samw /*
265331Samw  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
275331Samw  * All rights reserved.
285331Samw  *
295331Samw  * Redistribution and use in source and binary forms, with or without
305331Samw  * modification, are permitted provided that the following conditions
315331Samw  * are met:
325331Samw  * 1. Redistributions of source code must retain the above copyright
335331Samw  *    notice, this list of conditions and the following disclaimer.
345331Samw  * 2. Redistributions in binary form must reproduce the above copyright
355331Samw  *    notice, this list of conditions and the following disclaimer in the
365331Samw  *    documentation and/or other materials provided with the distribution.
375331Samw  * 3. All advertising materials mentioning features or use of this software
385331Samw  *    must display the following acknowledgement:
395331Samw  *	This product includes software developed by the University of
405331Samw  *	California, Berkeley and its contributors.
415331Samw  * 4. Neither the name of the University nor the names of its contributors
425331Samw  *    may be used to endorse or promote products derived from this software
435331Samw  *    without specific prior written permission.
445331Samw  *
455331Samw  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
465331Samw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
475331Samw  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
485331Samw  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
495331Samw  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
505331Samw  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
515331Samw  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
525331Samw  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
535331Samw  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
545331Samw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
555331Samw  * SUCH DAMAGE.
565331Samw  *
575331Samw  */
585331Samw 
595331Samw #ifndef _SMBSRV_MBUF_H
605331Samw #define	_SMBSRV_MBUF_H
615331Samw 
625331Samw #pragma ident	"%Z%%M%	%I%	%E% SMI"
635331Samw 
645331Samw /*
655331Samw  * PBSHORTCUT This file should be removed from the PB port but is required
665331Samw  * for now to get it to compile. This file has also been modified.
675331Samw  */
685331Samw 
695331Samw #include <sys/types.h>
705331Samw #include <sys/param.h>
715331Samw #include <smbsrv/smb_i18n.h>
725331Samw #include <smbsrv/alloc.h>
735331Samw 
745331Samw #ifdef __cplusplus
755331Samw extern "C" {
765331Samw #endif
775331Samw 
785331Samw #define	MSIZE		256
795331Samw #define	MCLBYTES	2048
805331Samw #define	MCLSHIFT	11
815331Samw #define	MCLOFSET	(MCLBYTES - 1)
825331Samw 
835331Samw #define	NBPG 4096
845331Samw #define	CLBYTES		NBPG
855331Samw #define	PB_PAGESIZE 4096
865331Samw 
875331Samw /*
885331Samw  * Mbufs are of a single size, MSIZE (machine/machparam.h), which
895331Samw  * includes overhead.  An mbuf may add a single "mbuf cluster" of size
905331Samw  * MCLBYTES (also in machine/machparam.h), which has no additional overhead
915331Samw  * and is used instead of the internal data area; this is done when
925331Samw  * at least MINCLSIZE of data must be stored.
935331Samw  */
945331Samw 
955331Samw #define	MLEN		(MSIZE - sizeof (struct m_hdr))	/* normal data len */
965331Samw #define	MHLEN		(MLEN - sizeof (struct pkthdr))	/* data len w/pkthdr */
975331Samw 
985331Samw #define	MINCLSIZE	(MHLEN + MLEN)	/* smallest amount to put in cluster */
995331Samw 
1005331Samw /*
1015331Samw  * Macros for type conversion
1025331Samw  * mtod(m,t) -	convert mbuf pointer to data pointer of correct type
1035331Samw  */
1045331Samw #define	mtod(m, t)	((t)((m)->m_data))
1055331Samw 
1065331Samw 
1075331Samw /* header at beginning of each mbuf: */
1085331Samw struct m_hdr {
1095331Samw 	struct	mbuf *mh_next;		/* next buffer in chain */
1105331Samw 	struct	mbuf *mh_nextpkt;	/* next chain in queue/record */
1115331Samw 	int	mh_len;			/* amount of data in this mbuf */
1125331Samw 	caddr_t	mh_data;		/* location of data */
1135331Samw 	short	mh_type;		/* type of data in this mbuf */
1145331Samw 	short	mh_flags;		/* flags; see below */
1155331Samw };
1165331Samw 
1175331Samw /* record/packet header in first mbuf of chain; valid if M_PKTHDR set */
1185331Samw struct	pkthdr {
1195331Samw 	int	len;		/* total packet length */
1205331Samw };
1215331Samw 
1225331Samw 
1235331Samw /* XXX probably do not need m_ext */
1245331Samw 
1255331Samw /* description of external storage mapped into mbuf, valid if M_EXT set */
1265331Samw struct m_ext {
1275331Samw 	caddr_t	ext_buf;		/* start of buffer */
1285331Samw 	int	(*ext_ref)();		/* refcount adjust function */
1295331Samw 	uint_t	ext_size;		/* size of buffer, for ext_free */
1305331Samw };
1315331Samw 
132*6496Sjb150015 typedef struct mbuf {
1335331Samw 	struct	m_hdr m_hdr;
1345331Samw 	union {
1355331Samw 		struct {
1365331Samw 			struct	pkthdr MH_pkthdr;	/* M_PKTHDR set */
1375331Samw 			union {
1385331Samw 				struct	m_ext MH_ext;	/* M_EXT set */
1395331Samw 				char	MH_databuf[MHLEN];
1405331Samw 			} MH_dat;
1415331Samw 		} MH;
1425331Samw 		char	M_databuf[MLEN];		/* !M_PKTHDR, !M_EXT */
1435331Samw 	} M_dat;
144*6496Sjb150015 } mbuf_t;
145*6496Sjb150015 
1465331Samw #define	m_next		m_hdr.mh_next
1475331Samw #define	m_len		m_hdr.mh_len
1485331Samw #define	m_data		m_hdr.mh_data
1495331Samw #define	m_type		m_hdr.mh_type
1505331Samw #define	m_flags		m_hdr.mh_flags
1515331Samw #define	m_nextpkt	m_hdr.mh_nextpkt
1525331Samw #define	m_act		m_nextpkt
1535331Samw #define	m_pkthdr	M_dat.MH.MH_pkthdr
1545331Samw #define	m_ext		M_dat.MH.MH_dat.MH_ext
1555331Samw #define	m_pktdat	M_dat.MH.MH_dat.MH_databuf
1565331Samw #define	m_dat		M_dat.M_databuf
1575331Samw 
1585331Samw /* mbuf flags */
1595331Samw #define	M_EXT		0x0001	/* has associated external storage */
1605331Samw #define	M_PKTHDR	0x0002	/* start of record */
1615331Samw #define	M_EOR		0x0004	/* end of record */
1625331Samw 
1635331Samw /* mbuf pkthdr flags, also in m_flags */
1645331Samw #define	M_BCAST		0x0100	/* send/received as link-level broadcast */
1655331Samw #define	M_MCAST		0x0200	/* send/received as link-level multicast */
1665331Samw 
1675331Samw /* flags copied when copying m_pkthdr */
1685331Samw #define	M_COPYFLAGS	(M_PKTHDR|M_EOR|M_BCAST|M_MCAST)
1695331Samw 
1705331Samw /* XXX probably only need MT_DATA */
1715331Samw 
1725331Samw /* mbuf types */
1735331Samw #define	MT_FREE		0	/* should be on free list */
1745331Samw #define	MT_DATA		1	/* dynamic (data) allocation */
1755331Samw #define	MT_HEADER	2	/* packet header */
1765331Samw #define	MT_SOCKET	3	/* socket structure */
1775331Samw #define	MT_PCB		4	/* protocol control block */
1785331Samw #define	MT_RTABLE	5	/* routing tables */
1795331Samw #define	MT_HTABLE	6	/* IMP host tables */
1805331Samw #define	MT_ATABLE	7	/* address resolution tables */
1815331Samw #define	MT_SONAME	8	/* socket name */
1825331Samw #define	MT_SOOPTS	10	/* socket options */
1835331Samw #define	MT_FTABLE	11	/* fragment reassembly header */
1845331Samw #define	MT_RIGHTS	12	/* access rights */
1855331Samw #define	MT_IFADDR	13	/* interface address */
1865331Samw #define	MT_CONTROL	14	/* extra-data protocol message */
1875331Samw #define	MT_OOBDATA	15	/* expedited data  */
1885331Samw 
1895331Samw /*
1905331Samw  * flags to malloc: PBSHORTCUT
1915331Samw  */
1925331Samw #define	M_WAITOK	0x0000
1935331Samw #define	M_NOWAIT	0x0001
1945331Samw 
1955331Samw /* flags to m_get/MGET */
1965331Samw #define	M_DONTWAIT	M_NOWAIT
1975331Samw #define	M_WAIT		M_WAITOK
1985331Samw 
1995331Samw 
2005331Samw /*
2015331Samw  * mbuf allocation/deallocation macros:
2025331Samw  *
2035331Samw  *	MGET(struct mbuf *m, int how, int type)
2045331Samw  * allocates an mbuf and initializes it to contain internal data.
2055331Samw  *
2065331Samw  *	MGETHDR(struct mbuf *m, int how, int type)
2075331Samw  * allocates an mbuf and initializes it to contain a packet header
2085331Samw  * and internal data.
2095331Samw  */
2105331Samw 
2115331Samw #define	MGET(m, how, type) { \
2125331Samw 	m = MEM_ZALLOC("mbuf", sizeof (struct mbuf)); \
2135331Samw 	(m)->m_next = (struct mbuf *)NULL; \
2145331Samw 	(m)->m_nextpkt = (struct mbuf *)NULL; \
2155331Samw 	(m)->m_data = (m)->m_dat; \
2165331Samw 	(m)->m_flags = 0; \
2175331Samw 	(m)->m_type = (short)(type); \
2185331Samw }
2195331Samw 
2205331Samw #define	MGETHDR(m, how, type) { \
2215331Samw 	m = MEM_ZALLOC("mbuf", sizeof (struct mbuf)); \
2225331Samw 	(m)->m_type = (MT_HEADER); \
2235331Samw 	(m)->m_next = (struct mbuf *)NULL; \
2245331Samw 	(m)->m_nextpkt = (struct mbuf *)NULL; \
2255331Samw 	(m)->m_data = (m)->m_pktdat; \
2265331Samw 	(m)->m_flags = M_PKTHDR; \
2275331Samw }
2285331Samw 
2295331Samw extern	int mclref();
2305331Samw extern	int mclrefnoop();
2315331Samw #define	MCLGET(m, how) \
2325331Samw 	{ \
2335331Samw 		(m)->m_ext.ext_buf = MEM_ZALLOC("mbuf", MCLBYTES);	\
2345331Samw 		(m)->m_data = (m)->m_ext.ext_buf;		\
2355331Samw 		(m)->m_flags |= M_EXT;				\
2365331Samw 		(m)->m_ext.ext_size = MCLBYTES;			\
2375331Samw 		(m)->m_ext.ext_ref = mclref;			\
2385331Samw 	}
2395331Samw 
2405331Samw /*
2415331Samw  * MFREE(struct mbuf *m, struct mbuf *nn)
2425331Samw  * Free a single mbuf and associated external storage.
2435331Samw  * Place the successor, if any, in nn.
2445331Samw  */
2455331Samw #define	MFREE(m, nn) \
2465331Samw 	{ \
2475331Samw 		if ((m)->m_flags & M_EXT) {		    \
2485331Samw 			(*((m)->m_ext.ext_ref))((m)->m_ext.ext_buf,	\
2495331Samw 			    (m)->m_ext.ext_size, -1);			\
2505331Samw 			(m)->m_ext.ext_buf = 0;				\
2515331Samw 		}							\
2525331Samw 		(nn) = (m)->m_next;					\
2535331Samw 		(m)->m_next = 0;					\
2545331Samw 		MEM_FREE("mbuf", m);					\
2555331Samw 	}
2565331Samw 
2575331Samw 
2585331Samw 
2595331Samw /*
2605331Samw  * As above, for mbufs allocated with m_gethdr/MGETHDR
2615331Samw  * or initialized by M_COPY_PKTHDR.
2625331Samw  */
2635331Samw #define	MH_ALIGN(m, len) \
2645331Samw 	{ (m)->m_data += (MHLEN - (len)) &~ (sizeof (int32_t) - 1); }
2655331Samw 
266*6496Sjb150015 typedef struct mbuf_chain {
267*6496Sjb150015 	volatile uint32_t	flags;		/* Various flags */
268*6496Sjb150015 	struct mbuf_chain	*shadow_of;	/* I'm shadowing someone */
269*6496Sjb150015 	mbuf_t			*chain;		/* Start of chain */
270*6496Sjb150015 	int32_t			max_bytes;	/* max # of bytes for chain */
271*6496Sjb150015 	int32_t			chain_offset;	/* Current offset into chain */
272*6496Sjb150015 } mbuf_chain_t;
273*6496Sjb150015 
274*6496Sjb150015 mbuf_t *m_free(mbuf_t *);
275*6496Sjb150015 void m_freem(mbuf_t *);
276*6496Sjb150015 int mbc_moveout(mbuf_chain_t *, caddr_t, int, int *);
277*6496Sjb150015 
2785331Samw #ifdef __cplusplus
2795331Samw }
2805331Samw #endif
2815331Samw 
2825331Samw #endif /* _SMBSRV_MBUF_H */
283