14bff34e3Sthurlow /* 24bff34e3Sthurlow * Copyright (c) 2000, 2001 Boris Popov 34bff34e3Sthurlow * All rights reserved. 44bff34e3Sthurlow * 54bff34e3Sthurlow * Redistribution and use in source and binary forms, with or without 64bff34e3Sthurlow * modification, are permitted provided that the following conditions 74bff34e3Sthurlow * are met: 84bff34e3Sthurlow * 1. Redistributions of source code must retain the above copyright 94bff34e3Sthurlow * notice, this list of conditions and the following disclaimer. 104bff34e3Sthurlow * 2. Redistributions in binary form must reproduce the above copyright 114bff34e3Sthurlow * notice, this list of conditions and the following disclaimer in the 124bff34e3Sthurlow * documentation and/or other materials provided with the distribution. 134bff34e3Sthurlow * 3. All advertising materials mentioning features or use of this software 144bff34e3Sthurlow * must display the following acknowledgement: 154bff34e3Sthurlow * This product includes software developed by Boris Popov. 164bff34e3Sthurlow * 4. Neither the name of the author nor the names of any co-contributors 174bff34e3Sthurlow * may be used to endorse or promote products derived from this software 184bff34e3Sthurlow * without specific prior written permission. 194bff34e3Sthurlow * 204bff34e3Sthurlow * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 214bff34e3Sthurlow * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 224bff34e3Sthurlow * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 234bff34e3Sthurlow * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 244bff34e3Sthurlow * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 254bff34e3Sthurlow * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 264bff34e3Sthurlow * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 274bff34e3Sthurlow * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 284bff34e3Sthurlow * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 294bff34e3Sthurlow * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 304bff34e3Sthurlow * SUCH DAMAGE. 314bff34e3Sthurlow * 324bff34e3Sthurlow * $FreeBSD: src/sys/sys/mchain.h,v 1.1 2001/02/24 15:44:30 bp Exp $ 334bff34e3Sthurlow */ 34613a2f6bSGordon Ross 354bff34e3Sthurlow /* 36613a2f6bSGordon Ross * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 374bff34e3Sthurlow * Use is subject to license terms. 384bff34e3Sthurlow */ 394bff34e3Sthurlow 404bff34e3Sthurlow #ifndef _MCHAIN_H_ 414bff34e3Sthurlow #define _MCHAIN_H_ 424bff34e3Sthurlow 434bff34e3Sthurlow #include <sys/types.h> 444bff34e3Sthurlow #include <sys/isa_defs.h> 454bff34e3Sthurlow #include <sys/byteorder.h> 464bff34e3Sthurlow 474bff34e3Sthurlow #ifdef _LITTLE_ENDIAN 484bff34e3Sthurlow 494bff34e3Sthurlow /* little-endian values on little-endian */ 504bff34e3Sthurlow #define htoles(x) ((uint16_t)(x)) 514bff34e3Sthurlow #define letohs(x) ((uint16_t)(x)) 524bff34e3Sthurlow #define htolel(x) ((uint32_t)(x)) 534bff34e3Sthurlow #define letohl(x) ((uint32_t)(x)) 544bff34e3Sthurlow #define htoleq(x) ((uint64_t)(x)) 554bff34e3Sthurlow #define letohq(x) ((uint64_t)(x)) 564bff34e3Sthurlow 574bff34e3Sthurlow /* 584bff34e3Sthurlow * big-endian values on little-endian (swap) 594bff34e3Sthurlow * 604bff34e3Sthurlow * Use the BSWAP macros because they're fastest, and they're 614bff34e3Sthurlow * available in all environments where we use this header. 624bff34e3Sthurlow */ 634bff34e3Sthurlow #define htobes(x) BSWAP_16(x) 644bff34e3Sthurlow #define betohs(x) BSWAP_16(x) 654bff34e3Sthurlow #define htobel(x) BSWAP_32(x) 664bff34e3Sthurlow #define betohl(x) BSWAP_32(x) 674bff34e3Sthurlow #define htobeq(x) BSWAP_64(x) 684bff34e3Sthurlow #define betohq(x) BSWAP_64(x) 694bff34e3Sthurlow 704bff34e3Sthurlow #else /* (BYTE_ORDER == LITTLE_ENDIAN) */ 714bff34e3Sthurlow 724bff34e3Sthurlow /* little-endian values on big-endian (swap) */ 734bff34e3Sthurlow #define letohs(x) BSWAP_16(x) 744bff34e3Sthurlow #define htoles(x) BSWAP_16(x) 754bff34e3Sthurlow #define letohl(x) BSWAP_32(x) 764bff34e3Sthurlow #define htolel(x) BSWAP_32(x) 774bff34e3Sthurlow #define letohq(x) BSWAP_64(x) 784bff34e3Sthurlow #define htoleq(x) BSWAP_64(x) 794bff34e3Sthurlow 804bff34e3Sthurlow /* big-endian values on big-endian */ 814bff34e3Sthurlow #define htobes(x) ((uint16_t)(x)) 824bff34e3Sthurlow #define betohs(x) ((uint16_t)(x)) 834bff34e3Sthurlow #define htobel(x) ((uint32_t)(x)) 844bff34e3Sthurlow #define betohl(x) ((uint32_t)(x)) 854bff34e3Sthurlow #define htobeq(x) ((uint64_t)(x)) 864bff34e3Sthurlow #define betohq(x) ((uint64_t)(x)) 874bff34e3Sthurlow #endif /* (BYTE_ORDER == LITTLE_ENDIAN) */ 884bff34e3Sthurlow 894bff34e3Sthurlow 90*02d09e03SGordon Ross /* 91*02d09e03SGordon Ross * Additions for Solaris to replace things that came from 92*02d09e03SGordon Ross * <sys/mbuf.h> in the Darwin code. These are mostly just 93*02d09e03SGordon Ross * wrappers for streams functions. See: subr_mchain.c 94*02d09e03SGordon Ross */ 95*02d09e03SGordon Ross 964bff34e3Sthurlow #ifdef _KERNEL 974bff34e3Sthurlow 98*02d09e03SGordon Ross /* 99*02d09e03SGordon Ross * BSD-style mbuf "shim" for kernel code. Note, this 100*02d09e03SGordon Ross * does NOT implement BSD mbufs in the kernel. Rather, 101*02d09e03SGordon Ross * macros and wrapper functions are used so that code 102*02d09e03SGordon Ross * fomerly using mbuf_t now use STREAMS mblk_t instead. 103*02d09e03SGordon Ross */ 104*02d09e03SGordon Ross 105*02d09e03SGordon Ross #include <sys/stream.h> /* mblk_t */ 106*02d09e03SGordon Ross typedef mblk_t mbuf_t; 107*02d09e03SGordon Ross 1084bff34e3Sthurlow /* BEGIN CSTYLED */ 1094bff34e3Sthurlow /* 1104bff34e3Sthurlow * BSD-style mbufs, vs SysV-style mblks: 1114bff34e3Sthurlow * One big difference: the mbuf payload is: 1124bff34e3Sthurlow * m_data ... (m_data + m_len) 1134bff34e3Sthurlow * In Unix STREAMS, the mblk payload is: 1144bff34e3Sthurlow * b_rptr ... b_wptr 1154bff34e3Sthurlow * 1164bff34e3Sthurlow * Here are some handy conversion notes: 1174bff34e3Sthurlow * 1184bff34e3Sthurlow * struct mbuf struct mblk 1194bff34e3Sthurlow * m->m_next m->b_cont 1204bff34e3Sthurlow * m->m_nextpkt m->b_next 1214bff34e3Sthurlow * m->m_data m->b_rptr 1224bff34e3Sthurlow * m->m_len MBLKL(m) 1234bff34e3Sthurlow * m->m_dat[] m->b_datap->db_base 1244bff34e3Sthurlow * &m->m_dat[MLEN] m->b_datap->db_lim 1254bff34e3Sthurlow * M_TRAILINGSPACE(m) MBLKTAIL(m) 1264bff34e3Sthurlow * m_freem(m) freemsg(m) 1274bff34e3Sthurlow * 1284bff34e3Sthurlow * Note that mbufs chains also have a special "packet" header, 1294bff34e3Sthurlow * which has the length of the whole message. In STREAMS one 1304bff34e3Sthurlow * typically just calls msgdsize(m) to get that. 1314bff34e3Sthurlow */ 1324bff34e3Sthurlow /* END CSTYLED */ 1334bff34e3Sthurlow 134*02d09e03SGordon Ross #define mtod(m, t) ((t)((m)->b_rptr)) 135*02d09e03SGordon Ross 136*02d09e03SGordon Ross /* length arg for m_copym to "copy all" */ 137*02d09e03SGordon Ross #define M_COPYALL -1 138*02d09e03SGordon Ross 139*02d09e03SGordon Ross mblk_t *m_copym(mblk_t *, int, int, int); 140*02d09e03SGordon Ross mblk_t *m_pullup(mblk_t *, int); 141*02d09e03SGordon Ross mblk_t *m_split(mblk_t *, int, int); 142*02d09e03SGordon Ross void m_cat(mblk_t *, mblk_t *); 143*02d09e03SGordon Ross #define m_freem(x) freemsg(x) 144*02d09e03SGordon Ross mblk_t *m_getblk(int, int); 145*02d09e03SGordon Ross int m_fixhdr(mblk_t *m); 146*02d09e03SGordon Ross 147*02d09e03SGordon Ross #else /* _KERNEL */ 148*02d09e03SGordon Ross 149*02d09e03SGordon Ross /* 150*02d09e03SGordon Ross * BSD-style mbuf work-alike, for user-level. 151*02d09e03SGordon Ross * See libsmbfs mbuf.c 152*02d09e03SGordon Ross */ 153*02d09e03SGordon Ross typedef struct mbuf { 154*02d09e03SGordon Ross int m_len; 155*02d09e03SGordon Ross int m_maxlen; 156*02d09e03SGordon Ross char *m_data; 157*02d09e03SGordon Ross struct mbuf *m_next; 158*02d09e03SGordon Ross } mbuf_t; 159*02d09e03SGordon Ross 160*02d09e03SGordon Ross #define mtod(m, t) ((t)(m)->m_data) 161*02d09e03SGordon Ross 162*02d09e03SGordon Ross int m_get(int, mbuf_t **); 163*02d09e03SGordon Ross void m_freem(mbuf_t *); 164*02d09e03SGordon Ross 165*02d09e03SGordon Ross #endif /* _KERNEL */ 166*02d09e03SGordon Ross 167*02d09e03SGordon Ross /* 168*02d09e03SGordon Ross * BSD-style mbchain/mdchain work-alike 169*02d09e03SGordon Ross */ 1704bff34e3Sthurlow 1714bff34e3Sthurlow /* 1724bff34e3Sthurlow * Type of copy for mb_{put|get}_mem() 1734bff34e3Sthurlow */ 1744bff34e3Sthurlow #define MB_MSYSTEM 0 /* use bcopy() */ 1754bff34e3Sthurlow #define MB_MUSER 1 /* use copyin()/copyout() */ 1764bff34e3Sthurlow #define MB_MINLINE 2 /* use an inline copy loop */ 1774bff34e3Sthurlow #define MB_MZERO 3 /* bzero(), mb_put_mem only */ 1784bff34e3Sthurlow #define MB_MCUSTOM 4 /* use an user defined function */ 1794bff34e3Sthurlow 180*02d09e03SGordon Ross #ifdef _KERNEL 181*02d09e03SGordon Ross 1824bff34e3Sthurlow struct mbchain { 1834bff34e3Sthurlow mblk_t *mb_top; 1844bff34e3Sthurlow mblk_t *mb_cur; 1854bff34e3Sthurlow uint_t mb_count; 1864bff34e3Sthurlow }; 1874bff34e3Sthurlow typedef struct mbchain mbchain_t; 1884bff34e3Sthurlow 1894bff34e3Sthurlow struct mdchain { 1904bff34e3Sthurlow mblk_t *md_top; /* head of mblk chain */ 1914bff34e3Sthurlow mblk_t *md_cur; /* current mblk */ 192*02d09e03SGordon Ross uchar_t *md_pos; /* position in md_cur */ 193*02d09e03SGordon Ross /* NB: md_pos is same type as mblk_t b_rptr, b_wptr members. */ 1944bff34e3Sthurlow }; 1954bff34e3Sthurlow typedef struct mdchain mdchain_t; 1964bff34e3Sthurlow 197*02d09e03SGordon Ross mblk_t *mb_detach(mbchain_t *mbp); 198*02d09e03SGordon Ross int mb_fixhdr(mbchain_t *mbp); 199*02d09e03SGordon Ross int mb_put_uio(mbchain_t *mbp, uio_t *uiop, size_t size); 2004bff34e3Sthurlow 201*02d09e03SGordon Ross void md_append_record(mdchain_t *mdp, mblk_t *top); 202*02d09e03SGordon Ross void md_next_record(mdchain_t *mdp); 203*02d09e03SGordon Ross int md_get_uio(mdchain_t *mdp, uio_t *uiop, size_t size); 2044bff34e3Sthurlow 205*02d09e03SGordon Ross #else /* _KERNEL */ 2064bff34e3Sthurlow 2074bff34e3Sthurlow /* 208*02d09e03SGordon Ross * user-level code uses the same struct for both (MB, MD) 2094bff34e3Sthurlow */ 210*02d09e03SGordon Ross typedef struct mbdata { 211*02d09e03SGordon Ross mbuf_t *mb_top; /* head of mbuf chain */ 212*02d09e03SGordon Ross mbuf_t *mb_cur; /* current mbuf */ 213*02d09e03SGordon Ross char *mb_pos; /* position in mb_cur (get) */ 214*02d09e03SGordon Ross /* NB: mb_pos is same type as mbuf_t m_data member. */ 215*02d09e03SGordon Ross int mb_count; /* bytes marshalled (put) */ 216*02d09e03SGordon Ross } mbdata_t; 217*02d09e03SGordon Ross typedef struct mbdata mbchain_t; 218*02d09e03SGordon Ross typedef struct mbdata mdchain_t; 2194bff34e3Sthurlow 220*02d09e03SGordon Ross #endif /* _KERNEL */ 2214bff34e3Sthurlow 222*02d09e03SGordon Ross int mb_init(mbchain_t *); 223*02d09e03SGordon Ross void mb_initm(mbchain_t *, mbuf_t *); 224*02d09e03SGordon Ross void mb_done(mbchain_t *); 225*02d09e03SGordon Ross void *mb_reserve(mbchain_t *, int size); 2264bff34e3Sthurlow 227*02d09e03SGordon Ross int mb_put_padbyte(mbchain_t *mbp); 228*02d09e03SGordon Ross int mb_put_uint8(mbchain_t *, uint8_t); 229*02d09e03SGordon Ross int mb_put_uint16be(mbchain_t *, uint16_t); 230*02d09e03SGordon Ross int mb_put_uint16le(mbchain_t *, uint16_t); 231*02d09e03SGordon Ross int mb_put_uint32be(mbchain_t *, uint32_t); 232*02d09e03SGordon Ross int mb_put_uint32le(mbchain_t *, uint32_t); 233*02d09e03SGordon Ross int mb_put_uint64be(mbchain_t *, uint64_t); 234*02d09e03SGordon Ross int mb_put_uint64le(mbchain_t *, uint64_t); 235*02d09e03SGordon Ross int mb_put_mem(mbchain_t *, const void *, int, int); 236*02d09e03SGordon Ross int mb_put_mbuf(mbchain_t *, mbuf_t *); 2374bff34e3Sthurlow 238*02d09e03SGordon Ross int md_init(mdchain_t *mdp); 239*02d09e03SGordon Ross void md_initm(mdchain_t *mbp, mbuf_t *m); 240*02d09e03SGordon Ross void md_done(mdchain_t *mdp); 241*02d09e03SGordon Ross 242*02d09e03SGordon Ross int md_get_uint8(mdchain_t *, uint8_t *); 243*02d09e03SGordon Ross int md_get_uint16be(mdchain_t *, uint16_t *); 244*02d09e03SGordon Ross int md_get_uint16le(mdchain_t *, uint16_t *); 245*02d09e03SGordon Ross int md_get_uint32be(mdchain_t *, uint32_t *); 246*02d09e03SGordon Ross int md_get_uint32le(mdchain_t *, uint32_t *); 247*02d09e03SGordon Ross int md_get_uint64be(mdchain_t *, uint64_t *); 248*02d09e03SGordon Ross int md_get_uint64le(mdchain_t *, uint64_t *); 249*02d09e03SGordon Ross int md_get_mem(mdchain_t *, void *, int, int); 250*02d09e03SGordon Ross int md_get_mbuf(mdchain_t *, int, mbuf_t **); 251*02d09e03SGordon Ross 2524bff34e3Sthurlow #endif /* !_MCHAIN_H_ */ 253