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 /* 228934SJose.Borrego@Sun.COM * Copyright 2009 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 /* 635331Samw * PBSHORTCUT This file should be removed from the PB port but is required 645331Samw * for now to get it to compile. This file has also been modified. 655331Samw */ 665331Samw 675331Samw #include <sys/types.h> 685331Samw #include <sys/param.h> 698934SJose.Borrego@Sun.COM #include <sys/list.h> 70*10966SJordan.Brown@Sun.COM #include <smbsrv/string.h> 715331Samw #include <smbsrv/alloc.h> 725331Samw 735331Samw #ifdef __cplusplus 745331Samw extern "C" { 755331Samw #endif 765331Samw 775331Samw #define MSIZE 256 785331Samw #define MCLBYTES 2048 795331Samw #define MCLSHIFT 11 805331Samw #define MCLOFSET (MCLBYTES - 1) 815331Samw 825331Samw #define NBPG 4096 835331Samw #define CLBYTES NBPG 845331Samw #define PB_PAGESIZE 4096 855331Samw 865331Samw /* 875331Samw * Mbufs are of a single size, MSIZE (machine/machparam.h), which 885331Samw * includes overhead. An mbuf may add a single "mbuf cluster" of size 895331Samw * MCLBYTES (also in machine/machparam.h), which has no additional overhead 905331Samw * and is used instead of the internal data area; this is done when 915331Samw * at least MINCLSIZE of data must be stored. 925331Samw */ 935331Samw 945331Samw #define MLEN (MSIZE - sizeof (struct m_hdr)) /* normal data len */ 955331Samw #define MHLEN (MLEN - sizeof (struct pkthdr)) /* data len w/pkthdr */ 965331Samw 975331Samw #define MINCLSIZE (MHLEN + MLEN) /* smallest amount to put in cluster */ 985331Samw 995331Samw /* 1005331Samw * Macros for type conversion 1015331Samw * mtod(m,t) - convert mbuf pointer to data pointer of correct type 1025331Samw */ 1035331Samw #define mtod(m, t) ((t)((m)->m_data)) 1045331Samw 1055331Samw 1065331Samw /* header at beginning of each mbuf: */ 1075331Samw struct m_hdr { 1085331Samw struct mbuf *mh_next; /* next buffer in chain */ 1095331Samw struct mbuf *mh_nextpkt; /* next chain in queue/record */ 1105331Samw int mh_len; /* amount of data in this mbuf */ 1115331Samw caddr_t mh_data; /* location of data */ 1125331Samw short mh_type; /* type of data in this mbuf */ 1135331Samw short mh_flags; /* flags; see below */ 1145331Samw }; 1155331Samw 1165331Samw /* record/packet header in first mbuf of chain; valid if M_PKTHDR set */ 1175331Samw struct pkthdr { 1185331Samw int len; /* total packet length */ 1195331Samw }; 1205331Samw 1215331Samw 1225331Samw /* XXX probably do not need m_ext */ 1235331Samw 1245331Samw /* description of external storage mapped into mbuf, valid if M_EXT set */ 1255331Samw struct m_ext { 1265331Samw caddr_t ext_buf; /* start of buffer */ 1275331Samw int (*ext_ref)(); /* refcount adjust function */ 1285331Samw uint_t ext_size; /* size of buffer, for ext_free */ 1295331Samw }; 1305331Samw 1316496Sjb150015 typedef struct mbuf { 1325331Samw struct m_hdr m_hdr; 1335331Samw union { 1345331Samw struct { 1355331Samw struct pkthdr MH_pkthdr; /* M_PKTHDR set */ 1365331Samw union { 1375331Samw struct m_ext MH_ext; /* M_EXT set */ 1385331Samw char MH_databuf[MHLEN]; 1395331Samw } MH_dat; 1405331Samw } MH; 1415331Samw char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */ 1425331Samw } M_dat; 1436496Sjb150015 } mbuf_t; 1446496Sjb150015 1455331Samw #define m_next m_hdr.mh_next 1465331Samw #define m_len m_hdr.mh_len 1475331Samw #define m_data m_hdr.mh_data 1485331Samw #define m_type m_hdr.mh_type 1495331Samw #define m_flags m_hdr.mh_flags 1505331Samw #define m_nextpkt m_hdr.mh_nextpkt 1515331Samw #define m_act m_nextpkt 1525331Samw #define m_pkthdr M_dat.MH.MH_pkthdr 1535331Samw #define m_ext M_dat.MH.MH_dat.MH_ext 1545331Samw #define m_pktdat M_dat.MH.MH_dat.MH_databuf 1555331Samw #define m_dat M_dat.M_databuf 1565331Samw 1575331Samw /* mbuf flags */ 1585331Samw #define M_EXT 0x0001 /* has associated external storage */ 1595331Samw #define M_PKTHDR 0x0002 /* start of record */ 1605331Samw #define M_EOR 0x0004 /* end of record */ 1615331Samw 1625331Samw /* mbuf pkthdr flags, also in m_flags */ 1635331Samw #define M_BCAST 0x0100 /* send/received as link-level broadcast */ 1645331Samw #define M_MCAST 0x0200 /* send/received as link-level multicast */ 1655331Samw 1665331Samw /* flags copied when copying m_pkthdr */ 1675331Samw #define M_COPYFLAGS (M_PKTHDR|M_EOR|M_BCAST|M_MCAST) 1685331Samw 1695331Samw /* XXX probably only need MT_DATA */ 1705331Samw 1715331Samw /* mbuf types */ 1725331Samw #define MT_FREE 0 /* should be on free list */ 1735331Samw #define MT_DATA 1 /* dynamic (data) allocation */ 1745331Samw #define MT_HEADER 2 /* packet header */ 1755331Samw #define MT_SOCKET 3 /* socket structure */ 1765331Samw #define MT_PCB 4 /* protocol control block */ 1775331Samw #define MT_RTABLE 5 /* routing tables */ 1785331Samw #define MT_HTABLE 6 /* IMP host tables */ 1795331Samw #define MT_ATABLE 7 /* address resolution tables */ 1805331Samw #define MT_SONAME 8 /* socket name */ 1815331Samw #define MT_SOOPTS 10 /* socket options */ 1825331Samw #define MT_FTABLE 11 /* fragment reassembly header */ 1835331Samw #define MT_RIGHTS 12 /* access rights */ 1845331Samw #define MT_IFADDR 13 /* interface address */ 1855331Samw #define MT_CONTROL 14 /* extra-data protocol message */ 1865331Samw #define MT_OOBDATA 15 /* expedited data */ 1875331Samw 1885331Samw /* 1895331Samw * flags to malloc: PBSHORTCUT 1905331Samw */ 1915331Samw #define M_WAITOK 0x0000 1925331Samw #define M_NOWAIT 0x0001 1935331Samw 1945331Samw /* flags to m_get/MGET */ 1955331Samw #define M_DONTWAIT M_NOWAIT 1965331Samw #define M_WAIT M_WAITOK 1975331Samw 1985331Samw 1995331Samw /* 2005331Samw * mbuf allocation/deallocation macros: 2015331Samw * 2025331Samw * MGET(struct mbuf *m, int how, int type) 2035331Samw * allocates an mbuf and initializes it to contain internal data. 2045331Samw * 2055331Samw * MGETHDR(struct mbuf *m, int how, int type) 2065331Samw * allocates an mbuf and initializes it to contain a packet header 2075331Samw * and internal data. 2085331Samw */ 2095331Samw 2105331Samw #define MGET(m, how, type) { \ 2115331Samw m = MEM_ZALLOC("mbuf", sizeof (struct mbuf)); \ 2125331Samw (m)->m_next = (struct mbuf *)NULL; \ 2135331Samw (m)->m_nextpkt = (struct mbuf *)NULL; \ 2145331Samw (m)->m_data = (m)->m_dat; \ 2155331Samw (m)->m_flags = 0; \ 2165331Samw (m)->m_type = (short)(type); \ 2175331Samw } 2185331Samw 2195331Samw #define MGETHDR(m, how, type) { \ 2205331Samw m = MEM_ZALLOC("mbuf", sizeof (struct mbuf)); \ 2215331Samw (m)->m_type = (MT_HEADER); \ 2225331Samw (m)->m_next = (struct mbuf *)NULL; \ 2235331Samw (m)->m_nextpkt = (struct mbuf *)NULL; \ 2245331Samw (m)->m_data = (m)->m_pktdat; \ 2255331Samw (m)->m_flags = M_PKTHDR; \ 2265331Samw } 2275331Samw 2285331Samw extern int mclref(); 2295331Samw extern int mclrefnoop(); 2305331Samw #define MCLGET(m, how) \ 2315331Samw { \ 2325331Samw (m)->m_ext.ext_buf = MEM_ZALLOC("mbuf", MCLBYTES); \ 2335331Samw (m)->m_data = (m)->m_ext.ext_buf; \ 2345331Samw (m)->m_flags |= M_EXT; \ 2355331Samw (m)->m_ext.ext_size = MCLBYTES; \ 2365331Samw (m)->m_ext.ext_ref = mclref; \ 2375331Samw } 2385331Samw 2395331Samw /* 2405331Samw * MFREE(struct mbuf *m, struct mbuf *nn) 2415331Samw * Free a single mbuf and associated external storage. 2425331Samw * Place the successor, if any, in nn. 2435331Samw */ 2445331Samw #define MFREE(m, nn) \ 2455331Samw { \ 2465331Samw if ((m)->m_flags & M_EXT) { \ 2475331Samw (*((m)->m_ext.ext_ref))((m)->m_ext.ext_buf, \ 2485331Samw (m)->m_ext.ext_size, -1); \ 2495331Samw (m)->m_ext.ext_buf = 0; \ 2505331Samw } \ 2515331Samw (nn) = (m)->m_next; \ 2525331Samw (m)->m_next = 0; \ 2535331Samw MEM_FREE("mbuf", m); \ 2545331Samw } 2555331Samw 2565331Samw 2575331Samw 2585331Samw /* 2595331Samw * As above, for mbufs allocated with m_gethdr/MGETHDR 2605331Samw * or initialized by M_COPY_PKTHDR. 2615331Samw */ 2625331Samw #define MH_ALIGN(m, len) \ 2635331Samw { (m)->m_data += (MHLEN - (len)) &~ (sizeof (int32_t) - 1); } 2645331Samw 2658934SJose.Borrego@Sun.COM #define SMB_MBC_MAGIC 0x4D42435F 2668934SJose.Borrego@Sun.COM #define SMB_MBC_VALID(p) ASSERT((p)->mbc_magic == SMB_MBC_MAGIC) 2678934SJose.Borrego@Sun.COM 2686496Sjb150015 typedef struct mbuf_chain { 2698934SJose.Borrego@Sun.COM uint32_t mbc_magic; 2708934SJose.Borrego@Sun.COM list_node_t mbc_lnd; 2716496Sjb150015 volatile uint32_t flags; /* Various flags */ 2726496Sjb150015 struct mbuf_chain *shadow_of; /* I'm shadowing someone */ 2736496Sjb150015 mbuf_t *chain; /* Start of chain */ 2746496Sjb150015 int32_t max_bytes; /* max # of bytes for chain */ 2756496Sjb150015 int32_t chain_offset; /* Current offset into chain */ 2766496Sjb150015 } mbuf_chain_t; 2776496Sjb150015 2786496Sjb150015 mbuf_t *m_free(mbuf_t *); 2796496Sjb150015 void m_freem(mbuf_t *); 2806496Sjb150015 int mbc_moveout(mbuf_chain_t *, caddr_t, int, int *); 2818934SJose.Borrego@Sun.COM int smb_mbc_init(void); 2828934SJose.Borrego@Sun.COM void smb_mbc_fini(void); 2838934SJose.Borrego@Sun.COM mbuf_chain_t *smb_mbc_alloc(uint32_t); 2848934SJose.Borrego@Sun.COM void smb_mbc_free(mbuf_chain_t *); 2856496Sjb150015 2865331Samw #ifdef __cplusplus 2875331Samw } 2885331Samw #endif 2895331Samw 2905331Samw #endif /* _SMBSRV_MBUF_H */ 291