mchain.h revision 6711:d684034ad960
1/*
2 * Copyright (c) 2000, 2001 Boris Popov
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *    This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $FreeBSD: src/sys/sys/mchain.h,v 1.1 2001/02/24 15:44:30 bp Exp $
33 */
34/*
35 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
36 * Use is subject to license terms.
37 */
38
39#ifndef _MCHAIN_H_
40#define	_MCHAIN_H_
41
42#pragma ident	"%Z%%M%	%I%	%E% SMI"
43
44#include <sys/types.h>
45#include <sys/isa_defs.h>
46#include <sys/byteorder.h>
47
48#ifdef _LITTLE_ENDIAN
49
50/* little-endian values on little-endian */
51#define	htoles(x)	((uint16_t)(x))
52#define	letohs(x)	((uint16_t)(x))
53#define	htolel(x)	((uint32_t)(x))
54#define	letohl(x)	((uint32_t)(x))
55#define	htoleq(x)	((uint64_t)(x))
56#define	letohq(x)	((uint64_t)(x))
57
58/*
59 * big-endian values on little-endian (swap)
60 *
61 * Use the BSWAP macros because they're fastest, and they're
62 * available in all environments where we use this header.
63 */
64#define	htobes(x)	BSWAP_16(x)
65#define	betohs(x)	BSWAP_16(x)
66#define	htobel(x)	BSWAP_32(x)
67#define	betohl(x)	BSWAP_32(x)
68#define	htobeq(x)	BSWAP_64(x)
69#define	betohq(x)	BSWAP_64(x)
70
71#else	/* (BYTE_ORDER == LITTLE_ENDIAN) */
72
73/* little-endian values on big-endian (swap) */
74#define	letohs(x) 	BSWAP_16(x)
75#define	htoles(x) 	BSWAP_16(x)
76#define	letohl(x) 	BSWAP_32(x)
77#define	htolel(x) 	BSWAP_32(x)
78#define	letohq(x)	BSWAP_64(x)
79#define	htoleq(x)	BSWAP_64(x)
80
81/* big-endian values on big-endian */
82#define	htobes(x)	((uint16_t)(x))
83#define	betohs(x)	((uint16_t)(x))
84#define	htobel(x)	((uint32_t)(x))
85#define	betohl(x)	((uint32_t)(x))
86#define	htobeq(x)	((uint64_t)(x))
87#define	betohq(x)	((uint64_t)(x))
88#endif	/* (BYTE_ORDER == LITTLE_ENDIAN) */
89
90
91#ifdef _KERNEL
92
93/* BEGIN CSTYLED */
94/*
95 * BSD-style mbufs, vs SysV-style mblks:
96 * One big difference: the mbuf payload is:
97 *   m_data ... (m_data + m_len)
98 * In Unix STREAMS, the mblk payload is:
99 *   b_rptr ... b_wptr
100 *
101 * Here are some handy conversion notes:
102 *
103 * struct mbuf                     struct mblk
104 *   m->m_next                       m->b_cont
105 *   m->m_nextpkt                    m->b_next
106 *   m->m_data                       m->b_rptr
107 *   m->m_len                        MBLKL(m)
108 *   m->m_dat[]                      m->b_datap->db_base
109 *   &m->m_dat[MLEN]                 m->b_datap->db_lim
110 *   M_TRAILINGSPACE(m)              MBLKTAIL(m)
111 *   m_freem(m)                      freemsg(m)
112 *
113 * Note that mbufs chains also have a special "packet" header,
114 * which has the length of the whole message.  In STREAMS one
115 * typically just calls msgdsize(m) to get that.
116 */
117/* END CSTYLED */
118
119#include <sys/stream.h> /* mblk_t */
120
121/*
122 * Type of copy for mb_{put|get}_mem()
123 */
124#define	MB_MSYSTEM	0		/* use bcopy() */
125#define	MB_MUSER	1		/* use copyin()/copyout() */
126#define	MB_MINLINE	2		/* use an inline copy loop */
127#define	MB_MZERO	3		/* bzero(), mb_put_mem only */
128#define	MB_MCUSTOM	4		/* use an user defined function */
129
130struct mbchain {
131	mblk_t *mb_top;
132	mblk_t *mb_cur;
133	uint_t mb_count;
134};
135typedef struct mbchain mbchain_t;
136
137struct mdchain {
138	mblk_t *md_top;		/* head of mblk chain */
139	mblk_t *md_cur;		/* current mblk */
140	uchar_t *md_pos;		/* offset in the current mblk */
141};
142typedef struct mdchain mdchain_t;
143
144int  m_fixhdr(mblk_t *m);
145
146int  mb_init(struct mbchain *mbp);
147void mb_initm(struct mbchain *mbp, mblk_t *m);
148void mb_done(struct mbchain *mbp);
149mblk_t *mb_detach(struct mbchain *mbp);
150int  mb_fixhdr(struct mbchain *mbp);
151void *mb_reserve(struct mbchain *mbp, int size);
152
153int  mb_put_padbyte(struct mbchain *mbp);
154int  mb_put_uint8(struct mbchain *mbp, uint8_t x);
155int  mb_put_uint16be(struct mbchain *mbp, uint16_t x);
156int  mb_put_uint16le(struct mbchain *mbp, uint16_t x);
157int  mb_put_uint32be(struct mbchain *mbp, uint32_t x);
158int  mb_put_uint32le(struct mbchain *mbp, uint32_t x);
159int  mb_put_uint64be(struct mbchain *mbp, uint64_t x);
160int  mb_put_uint64le(struct mbchain *mbp, uint64_t x);
161int  mb_put_mem(struct mbchain *mbp, const char *src, int size, int type);
162
163int  mb_put_mbuf(struct mbchain *mbp, mblk_t *m);
164int  mb_put_uio(struct mbchain *mbp, uio_t *uiop, int size);
165
166int  md_init(struct mdchain *mdp);
167void md_initm(struct mdchain *mbp, mblk_t *m);
168void md_done(struct mdchain *mdp);
169void md_append_record(struct mdchain *mdp, mblk_t *top);
170int  md_next_record(struct mdchain *mdp);
171int  md_get_uint8(struct mdchain *mdp, uint8_t *x);
172int  md_get_uint16(struct mdchain *mdp, uint16_t *x);
173int  md_get_uint16le(struct mdchain *mdp, uint16_t *x);
174int  md_get_uint16be(struct mdchain *mdp, uint16_t *x);
175int  md_get_uint32(struct mdchain *mdp, uint32_t *x);
176int  md_get_uint32be(struct mdchain *mdp, uint32_t *x);
177int  md_get_uint32le(struct mdchain *mdp, uint32_t *x);
178int  md_get_uint64(struct mdchain *mdp, uint64_t *x);
179int  md_get_uint64be(struct mdchain *mdp, uint64_t *x);
180int  md_get_uint64le(struct mdchain *mdp, uint64_t *x);
181int  md_get_mem(struct mdchain *mdp, caddr_t target, int size, int type);
182int  md_get_mbuf(struct mdchain *mdp, int size, mblk_t **m);
183int  md_get_uio(struct mdchain *mdp, uio_t *uiop, int size);
184
185/*
186 * Additions for Solaris to replace things that came from
187 * <sys/mbuf.h> in the Darwin code.  These are mostly just
188 * wrappers for streams functions.  See: subr_mchain.c
189 */
190
191#define	mtod(m, t) ((t)((m)->b_rptr))
192
193/* length to m_copym to copy all */
194#define	M_COPYALL		-1
195
196mblk_t *m_copym(mblk_t *, int, int, int);
197mblk_t *m_pullup(mblk_t *, int);
198mblk_t *m_split(mblk_t *, int, int);
199void m_cat(mblk_t *, mblk_t *);
200#define	m_freem(x)	freemsg(x)
201mblk_t *m_getblk(int, int);
202
203#endif	/* ifdef _KERNEL */
204#endif	/* !_MCHAIN_H_ */
205