xref: /titanic_44/usr/src/uts/common/netsmb/mchain.h (revision 75ce41a57ff334bd8fe2cb9ed51eea835892f944)
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 /*
36  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37  * Use is subject to license terms.
38  */
39 
40 #ifndef _MCHAIN_H_
41 #define	_MCHAIN_H_
42 
43 #include <sys/types.h>
44 #include <sys/isa_defs.h>
45 #include <sys/byteorder.h>
46 
47 #ifdef _LITTLE_ENDIAN
48 
49 /* little-endian values on little-endian */
50 #define	htoles(x)	((uint16_t)(x))
51 #define	letohs(x)	((uint16_t)(x))
52 #define	htolel(x)	((uint32_t)(x))
53 #define	letohl(x)	((uint32_t)(x))
54 #define	htoleq(x)	((uint64_t)(x))
55 #define	letohq(x)	((uint64_t)(x))
56 
57 /*
58  * big-endian values on little-endian (swap)
59  *
60  * Use the BSWAP macros because they're fastest, and they're
61  * available in all environments where we use this header.
62  */
63 #define	htobes(x)	BSWAP_16(x)
64 #define	betohs(x)	BSWAP_16(x)
65 #define	htobel(x)	BSWAP_32(x)
66 #define	betohl(x)	BSWAP_32(x)
67 #define	htobeq(x)	BSWAP_64(x)
68 #define	betohq(x)	BSWAP_64(x)
69 
70 #else	/* (BYTE_ORDER == LITTLE_ENDIAN) */
71 
72 /* little-endian values on big-endian (swap) */
73 #define	letohs(x) 	BSWAP_16(x)
74 #define	htoles(x) 	BSWAP_16(x)
75 #define	letohl(x) 	BSWAP_32(x)
76 #define	htolel(x) 	BSWAP_32(x)
77 #define	letohq(x)	BSWAP_64(x)
78 #define	htoleq(x)	BSWAP_64(x)
79 
80 /* big-endian values on big-endian */
81 #define	htobes(x)	((uint16_t)(x))
82 #define	betohs(x)	((uint16_t)(x))
83 #define	htobel(x)	((uint32_t)(x))
84 #define	betohl(x)	((uint32_t)(x))
85 #define	htobeq(x)	((uint64_t)(x))
86 #define	betohq(x)	((uint64_t)(x))
87 #endif	/* (BYTE_ORDER == LITTLE_ENDIAN) */
88 
89 
90 #ifdef _KERNEL
91 
92 /* BEGIN CSTYLED */
93 /*
94  * BSD-style mbufs, vs SysV-style mblks:
95  * One big difference: the mbuf payload is:
96  *   m_data ... (m_data + m_len)
97  * In Unix STREAMS, the mblk payload is:
98  *   b_rptr ... b_wptr
99  *
100  * Here are some handy conversion notes:
101  *
102  * struct mbuf                     struct mblk
103  *   m->m_next                       m->b_cont
104  *   m->m_nextpkt                    m->b_next
105  *   m->m_data                       m->b_rptr
106  *   m->m_len                        MBLKL(m)
107  *   m->m_dat[]                      m->b_datap->db_base
108  *   &m->m_dat[MLEN]                 m->b_datap->db_lim
109  *   M_TRAILINGSPACE(m)              MBLKTAIL(m)
110  *   m_freem(m)                      freemsg(m)
111  *
112  * Note that mbufs chains also have a special "packet" header,
113  * which has the length of the whole message.  In STREAMS one
114  * typically just calls msgdsize(m) to get that.
115  */
116 /* END CSTYLED */
117 
118 #include <sys/stream.h> /* mblk_t */
119 
120 /*
121  * Type of copy for mb_{put|get}_mem()
122  */
123 #define	MB_MSYSTEM	0		/* use bcopy() */
124 #define	MB_MUSER	1		/* use copyin()/copyout() */
125 #define	MB_MINLINE	2		/* use an inline copy loop */
126 #define	MB_MZERO	3		/* bzero(), mb_put_mem only */
127 #define	MB_MCUSTOM	4		/* use an user defined function */
128 
129 struct mbchain {
130 	mblk_t *mb_top;
131 	mblk_t *mb_cur;
132 	uint_t mb_count;
133 };
134 typedef struct mbchain mbchain_t;
135 
136 struct mdchain {
137 	mblk_t *md_top;		/* head of mblk chain */
138 	mblk_t *md_cur;		/* current mblk */
139 	uchar_t *md_pos;		/* offset in the current mblk */
140 };
141 typedef struct mdchain mdchain_t;
142 
143 int  m_fixhdr(mblk_t *m);
144 
145 int  mb_init(struct mbchain *mbp);
146 void mb_initm(struct mbchain *mbp, mblk_t *m);
147 void mb_done(struct mbchain *mbp);
148 mblk_t *mb_detach(struct mbchain *mbp);
149 int  mb_fixhdr(struct mbchain *mbp);
150 void *mb_reserve(struct mbchain *mbp, int size);
151 
152 int  mb_put_padbyte(struct mbchain *mbp);
153 int  mb_put_uint8(struct mbchain *mbp, uint8_t x);
154 int  mb_put_uint16be(struct mbchain *mbp, uint16_t x);
155 int  mb_put_uint16le(struct mbchain *mbp, uint16_t x);
156 int  mb_put_uint32be(struct mbchain *mbp, uint32_t x);
157 int  mb_put_uint32le(struct mbchain *mbp, uint32_t x);
158 int  mb_put_uint64be(struct mbchain *mbp, uint64_t x);
159 int  mb_put_uint64le(struct mbchain *mbp, uint64_t x);
160 int  mb_put_mem(struct mbchain *mbp, const void *src, int size, int type);
161 
162 int  mb_put_mbuf(struct mbchain *mbp, mblk_t *m);
163 int  mb_put_uio(struct mbchain *mbp, uio_t *uiop, size_t size);
164 
165 int  md_init(struct mdchain *mdp);
166 void md_initm(struct mdchain *mbp, mblk_t *m);
167 void md_done(struct mdchain *mdp);
168 void md_append_record(struct mdchain *mdp, mblk_t *top);
169 int  md_next_record(struct mdchain *mdp);
170 int  md_get_uint8(struct mdchain *mdp, uint8_t *x);
171 int  md_get_uint16le(struct mdchain *mdp, uint16_t *x);
172 int  md_get_uint16be(struct mdchain *mdp, uint16_t *x);
173 int  md_get_uint32be(struct mdchain *mdp, uint32_t *x);
174 int  md_get_uint32le(struct mdchain *mdp, uint32_t *x);
175 int  md_get_uint64be(struct mdchain *mdp, uint64_t *x);
176 int  md_get_uint64le(struct mdchain *mdp, uint64_t *x);
177 int  md_get_mem(struct mdchain *mdp, void *vdst, int size, int type);
178 int  md_get_mbuf(struct mdchain *mdp, int size, mblk_t **m);
179 int  md_get_uio(struct mdchain *mdp, uio_t *uiop, size_t size);
180 
181 /*
182  * Additions for Solaris to replace things that came from
183  * <sys/mbuf.h> in the Darwin code.  These are mostly just
184  * wrappers for streams functions.  See: subr_mchain.c
185  */
186 
187 #define	mtod(m, t) ((t)((m)->b_rptr))
188 
189 /* length to m_copym to copy all */
190 #define	M_COPYALL		-1
191 
192 mblk_t *m_copym(mblk_t *, int, int, int);
193 mblk_t *m_pullup(mblk_t *, int);
194 mblk_t *m_split(mblk_t *, int, int);
195 void m_cat(mblk_t *, mblk_t *);
196 #define	m_freem(x)	freemsg(x)
197 mblk_t *m_getblk(int, int);
198 
199 #endif	/* ifdef _KERNEL */
200 #endif	/* !_MCHAIN_H_ */
201