michael@0: /*- michael@0: * Copyright (c) 1982, 1986, 1988, 1993 michael@0: * The Regents of the University of California. michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. Neither the name of the University nor the names of its contributors michael@0: * may be used to endorse or promote products derived from this software michael@0: * without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND michael@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE michael@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL michael@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS michael@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) michael@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT michael@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY michael@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF michael@0: * SUCH DAMAGE. michael@0: * michael@0: */ michael@0: michael@0: #ifndef _USER_MBUF_H_ michael@0: #define _USER_MBUF_H_ michael@0: michael@0: /* __Userspace__ header file for mbufs */ michael@0: #include michael@0: #if !defined(SCTP_SIMPLE_ALLOCATOR) michael@0: #include "umem.h" michael@0: #endif michael@0: #include "user_malloc.h" michael@0: #include "netinet/sctp_os_userspace.h" michael@0: michael@0: #define USING_MBUF_CONSTRUCTOR 0 michael@0: michael@0: /* For Linux */ michael@0: #ifndef MSIZE michael@0: #define MSIZE 256 michael@0: /* #define MSIZE 1024 */ michael@0: #endif michael@0: #ifndef MCLBYTES michael@0: #define MCLBYTES 2048 michael@0: #endif michael@0: michael@0: struct mbuf * m_gethdr(int how, short type); michael@0: struct mbuf * m_get(int how, short type); michael@0: struct mbuf * m_free(struct mbuf *m); michael@0: void m_clget(struct mbuf *m, int how); michael@0: michael@0: michael@0: /* mbuf initialization function */ michael@0: void mbuf_init(void *); michael@0: michael@0: #define M_MOVE_PKTHDR(to, from) m_move_pkthdr((to), (from)) michael@0: #define MGET(m, how, type) ((m) = m_get((how), (type))) michael@0: #define MGETHDR(m, how, type) ((m) = m_gethdr((how), (type))) michael@0: #define MCLGET(m, how) m_clget((m), (how)) michael@0: michael@0: michael@0: #define M_HDR_PAD ((sizeof(intptr_t)==4) ? 2 : 6) /* modified for __Userspace__ */ michael@0: michael@0: /* Length to m_copy to copy all. */ michael@0: #define M_COPYALL 1000000000 michael@0: michael@0: /* umem_cache_t is defined in user_include/umem.h as michael@0: * typedef struct umem_cache umem_cache_t; michael@0: * Note:umem_zone_t is a pointer. michael@0: */ michael@0: #if defined(SCTP_SIMPLE_ALLOCATOR) michael@0: typedef size_t sctp_zone_t; michael@0: #else michael@0: typedef umem_cache_t *sctp_zone_t; michael@0: #endif michael@0: michael@0: extern sctp_zone_t zone_mbuf; michael@0: extern sctp_zone_t zone_clust; michael@0: extern sctp_zone_t zone_ext_refcnt; michael@0: michael@0: /*- michael@0: * Macros for type conversion: michael@0: * mtod(m, t) -- Convert mbuf pointer to data pointer of correct type. michael@0: * dtom(x) -- Convert data pointer within mbuf to mbuf pointer (XXX). michael@0: */ michael@0: #define mtod(m, t) ((t)((m)->m_data)) michael@0: #define dtom(x) ((struct mbuf *)((intptr_t)(x) & ~(MSIZE-1))) michael@0: michael@0: struct mb_args { michael@0: int flags; /* Flags for mbuf being allocated */ michael@0: short type; /* Type of mbuf being allocated */ michael@0: }; michael@0: michael@0: struct clust_args { michael@0: struct mbuf * parent_mbuf; michael@0: }; michael@0: michael@0: struct mbuf * m_split(struct mbuf *, int, int); michael@0: void m_cat(struct mbuf *m, struct mbuf *n); michael@0: void m_adj(struct mbuf *, int); michael@0: void mb_free_ext(struct mbuf *); michael@0: void m_freem(struct mbuf *); michael@0: struct m_tag *m_tag_alloc(u_int32_t, int, int, int); michael@0: struct mbuf *m_copym(struct mbuf *, int, int, int); michael@0: void m_copyback(struct mbuf *, int, int, caddr_t); michael@0: struct mbuf *m_pullup(struct mbuf *, int); michael@0: struct mbuf *m_pulldown(struct mbuf *, int off, int len, int *offp); michael@0: int m_dup_pkthdr(struct mbuf *, struct mbuf *, int); michael@0: struct m_tag *m_tag_copy(struct m_tag *, int); michael@0: int m_tag_copy_chain(struct mbuf *, struct mbuf *, int); michael@0: struct mbuf *m_prepend(struct mbuf *, int, int); michael@0: void m_copydata(const struct mbuf *, int, int, caddr_t); michael@0: michael@0: #define MBUF_MEM_NAME "mbuf" michael@0: #define MBUF_CLUSTER_MEM_NAME "mbuf_cluster" michael@0: #define MBUF_EXTREFCNT_MEM_NAME "mbuf_ext_refcnt" michael@0: michael@0: #define MT_NOINIT 255 /* Not a type but a flag to allocate michael@0: a non-initialized mbuf */ michael@0: michael@0: /* michael@0: * General mbuf allocator statistics structure. michael@0: * __Userspace__ mbstat may be useful for gathering statistics. michael@0: * In the kernel many of these statistics are no longer used as michael@0: * they track allocator statistics through kernel UMA's built in statistics mechanism. michael@0: */ michael@0: struct mbstat { michael@0: u_long m_mbufs; /* XXX */ michael@0: u_long m_mclusts; /* XXX */ michael@0: michael@0: u_long m_drain; /* times drained protocols for space */ michael@0: u_long m_mcfail; /* XXX: times m_copym failed */ michael@0: u_long m_mpfail; /* XXX: times m_pullup failed */ michael@0: u_long m_msize; /* length of an mbuf */ michael@0: u_long m_mclbytes; /* length of an mbuf cluster */ michael@0: u_long m_minclsize; /* min length of data to allocate a cluster */ michael@0: u_long m_mlen; /* length of data in an mbuf */ michael@0: u_long m_mhlen; /* length of data in a header mbuf */ michael@0: michael@0: /* Number of mbtypes (gives # elems in mbtypes[] array: */ michael@0: short m_numtypes; michael@0: michael@0: /* XXX: Sendfile stats should eventually move to their own struct */ michael@0: u_long sf_iocnt; /* times sendfile had to do disk I/O */ michael@0: u_long sf_allocfail; /* times sfbuf allocation failed */ michael@0: u_long sf_allocwait; /* times sfbuf allocation had to wait */ michael@0: }; michael@0: michael@0: michael@0: /* michael@0: * Mbufs are of a single size, MSIZE (sys/param.h), which includes overhead. michael@0: * An mbuf may add a single "mbuf cluster" of size MCLBYTES (also in michael@0: * sys/param.h), which has no additional overhead and is used instead of the michael@0: * internal data area; this is done when at least MINCLSIZE of data must be michael@0: * stored. Additionally, it is possible to allocate a separate buffer michael@0: * externally and attach it to the mbuf in a way similar to that of mbuf michael@0: * clusters. michael@0: */ michael@0: #define MLEN ((int)(MSIZE - sizeof(struct m_hdr))) /* normal data len */ michael@0: #define MHLEN ((int)(MLEN - sizeof(struct pkthdr))) /* data len w/pkthdr */ michael@0: #define MINCLSIZE ((int)(MHLEN + 1)) /* smallest amount to put in cluster */ michael@0: #define M_MAXCOMPRESS (MHLEN / 2) /* max amount to copy for compression */ michael@0: michael@0: michael@0: /* michael@0: * Header present at the beginning of every mbuf. michael@0: */ michael@0: struct m_hdr { michael@0: struct mbuf *mh_next; /* next buffer in chain */ michael@0: struct mbuf *mh_nextpkt; /* next chain in queue/record */ michael@0: caddr_t mh_data; /* location of data */ michael@0: int mh_len; /* amount of data in this mbuf */ michael@0: int mh_flags; /* flags; see below */ michael@0: short mh_type; /* type of data in this mbuf */ michael@0: uint8_t pad[M_HDR_PAD];/* word align */ michael@0: }; michael@0: michael@0: /* michael@0: * Packet tag structure (see below for details). michael@0: */ michael@0: struct m_tag { michael@0: SLIST_ENTRY(m_tag) m_tag_link; /* List of packet tags */ michael@0: u_int16_t m_tag_id; /* Tag ID */ michael@0: u_int16_t m_tag_len; /* Length of data */ michael@0: u_int32_t m_tag_cookie; /* ABI/Module ID */ michael@0: void (*m_tag_free)(struct m_tag *); michael@0: }; michael@0: michael@0: /* michael@0: * Record/packet header in first mbuf of chain; valid only if M_PKTHDR is set. michael@0: */ michael@0: struct pkthdr { michael@0: struct ifnet *rcvif; /* rcv interface */ michael@0: /* variables for ip and tcp reassembly */ michael@0: void *header; /* pointer to packet header */ michael@0: int len; /* total packet length */ michael@0: /* variables for hardware checksum */ michael@0: int csum_flags; /* flags regarding checksum */ michael@0: int csum_data; /* data field used by csum routines */ michael@0: u_int16_t tso_segsz; /* TSO segment size */ michael@0: u_int16_t ether_vtag; /* Ethernet 802.1p+q vlan tag */ michael@0: SLIST_HEAD(packet_tags, m_tag) tags; /* list of packet tags */ michael@0: }; michael@0: michael@0: /* michael@0: * Description of external storage mapped into mbuf; valid only if M_EXT is michael@0: * set. michael@0: */ michael@0: struct m_ext { michael@0: caddr_t ext_buf; /* start of buffer */ michael@0: void (*ext_free) /* free routine if not the usual */ michael@0: (void *, void *); michael@0: void *ext_args; /* optional argument pointer */ michael@0: u_int ext_size; /* size of buffer, for ext_free */ michael@0: volatile u_int *ref_cnt; /* pointer to ref count info */ michael@0: int ext_type; /* type of external storage */ michael@0: }; michael@0: michael@0: michael@0: /* michael@0: * The core of the mbuf object along with some shortcut defined for practical michael@0: * purposes. michael@0: */ michael@0: struct mbuf { michael@0: struct m_hdr m_hdr; michael@0: union { michael@0: struct { michael@0: struct pkthdr MH_pkthdr; /* M_PKTHDR set */ michael@0: union { michael@0: struct m_ext MH_ext; /* M_EXT set */ michael@0: char MH_databuf[MHLEN]; michael@0: } MH_dat; michael@0: } MH; michael@0: char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */ michael@0: } M_dat; michael@0: }; michael@0: michael@0: #define m_next m_hdr.mh_next michael@0: #define m_len m_hdr.mh_len michael@0: #define m_data m_hdr.mh_data michael@0: #define m_type m_hdr.mh_type michael@0: #define m_flags m_hdr.mh_flags michael@0: #define m_nextpkt m_hdr.mh_nextpkt michael@0: #define m_act m_nextpkt michael@0: #define m_pkthdr M_dat.MH.MH_pkthdr michael@0: #define m_ext M_dat.MH.MH_dat.MH_ext michael@0: #define m_pktdat M_dat.MH.MH_dat.MH_databuf michael@0: #define m_dat M_dat.M_databuf michael@0: michael@0: michael@0: /* michael@0: * mbuf flags. michael@0: */ michael@0: #define M_EXT 0x0001 /* has associated external storage */ michael@0: #define M_PKTHDR 0x0002 /* start of record */ michael@0: #define M_EOR 0x0004 /* end of record */ michael@0: #define M_RDONLY 0x0008 /* associated data is marked read-only */ michael@0: #define M_PROTO1 0x0010 /* protocol-specific */ michael@0: #define M_PROTO2 0x0020 /* protocol-specific */ michael@0: #define M_PROTO3 0x0040 /* protocol-specific */ michael@0: #define M_PROTO4 0x0080 /* protocol-specific */ michael@0: #define M_PROTO5 0x0100 /* protocol-specific */ michael@0: #define M_SKIP_FIREWALL 0x4000 /* skip firewall processing */ michael@0: #define M_FREELIST 0x8000 /* mbuf is on the free list */ michael@0: michael@0: michael@0: /* michael@0: * Flags copied when copying m_pkthdr. michael@0: */ michael@0: #define M_COPYFLAGS (M_PKTHDR|M_EOR|M_RDONLY|M_PROTO1|M_PROTO1|M_PROTO2|\ michael@0: M_PROTO3|M_PROTO4|M_PROTO5|M_SKIP_FIREWALL|\ michael@0: M_BCAST|M_MCAST|M_FRAG|M_FIRSTFRAG|M_LASTFRAG|\ michael@0: M_VLANTAG|M_PROMISC) michael@0: michael@0: michael@0: /* michael@0: * mbuf pkthdr flags (also stored in m_flags). michael@0: */ michael@0: #define M_BCAST 0x0200 /* send/received as link-level broadcast */ michael@0: #define M_MCAST 0x0400 /* send/received as link-level multicast */ michael@0: #define M_FRAG 0x0800 /* packet is a fragment of a larger packet */ michael@0: #define M_FIRSTFRAG 0x1000 /* packet is first fragment */ michael@0: #define M_LASTFRAG 0x2000 /* packet is last fragment */ michael@0: #define M_VLANTAG 0x10000 /* ether_vtag is valid */ michael@0: #define M_PROMISC 0x20000 /* packet was not for us */ michael@0: #define M_NOFREE 0x40000 /* do not free mbuf - it is embedded in the cluster */ michael@0: michael@0: michael@0: /* michael@0: * External buffer types: identify ext_buf type. michael@0: */ michael@0: #define EXT_CLUSTER 1 /* mbuf cluster */ michael@0: #define EXT_SFBUF 2 /* sendfile(2)'s sf_bufs */ michael@0: #define EXT_JUMBOP 3 /* jumbo cluster 4096 bytes */ michael@0: #define EXT_JUMBO9 4 /* jumbo cluster 9216 bytes */ michael@0: #define EXT_JUMBO16 5 /* jumbo cluster 16184 bytes */ michael@0: #define EXT_PACKET 6 /* mbuf+cluster from packet zone */ michael@0: #define EXT_MBUF 7 /* external mbuf reference (M_IOVEC) */ michael@0: #define EXT_NET_DRV 100 /* custom ext_buf provided by net driver(s) */ michael@0: #define EXT_MOD_TYPE 200 /* custom module's ext_buf type */ michael@0: #define EXT_DISPOSABLE 300 /* can throw this buffer away w/page flipping */ michael@0: #define EXT_EXTREF 400 /* has externally maintained ref_cnt ptr */ michael@0: michael@0: michael@0: /* michael@0: * mbuf types. michael@0: */ michael@0: #define MT_NOTMBUF 0 /* USED INTERNALLY ONLY! Object is not mbuf */ michael@0: #define MT_DATA 1 /* dynamic (data) allocation */ michael@0: #define MT_HEADER MT_DATA /* packet header, use M_PKTHDR instead */ michael@0: #define MT_SONAME 8 /* socket name */ michael@0: #define MT_CONTROL 14 /* extra-data protocol message */ michael@0: #define MT_OOBDATA 15 /* expedited data */ michael@0: #define MT_NTYPES 16 /* number of mbuf types for mbtypes[] */ michael@0: michael@0: #define MT_NOINIT 255 /* Not a type but a flag to allocate michael@0: a non-initialized mbuf */ michael@0: michael@0: /* michael@0: * __Userspace__ flags like M_NOWAIT are defined in malloc.h michael@0: * Flags like these are used in functions like uma_zalloc() michael@0: * but don't have an equivalent in userland umem michael@0: * Flags specifying how an allocation should be made. michael@0: * michael@0: * The flag to use is as follows: michael@0: * - M_DONTWAIT or M_NOWAIT from an interrupt handler to not block allocation. michael@0: * - M_WAIT or M_WAITOK or M_TRYWAIT from wherever it is safe to block. michael@0: * michael@0: * M_DONTWAIT/M_NOWAIT means that we will not block the thread explicitly and michael@0: * if we cannot allocate immediately we may return NULL, whereas michael@0: * M_WAIT/M_WAITOK/M_TRYWAIT means that if we cannot allocate resources we michael@0: * will block until they are available, and thus never return NULL. michael@0: * michael@0: * XXX Eventually just phase this out to use M_WAITOK/M_NOWAIT. michael@0: */ michael@0: #define MBTOM(how) (how) michael@0: michael@0: void m_tag_delete(struct mbuf *, struct m_tag *); michael@0: void m_tag_delete_chain(struct mbuf *, struct m_tag *); michael@0: void m_move_pkthdr(struct mbuf *, struct mbuf *); michael@0: void m_tag_free_default(struct m_tag *); michael@0: michael@0: extern int max_linkhdr; /* Largest link-level header */ michael@0: extern int max_protohdr; /* Size of largest protocol layer header. See user_mbuf.c */ michael@0: michael@0: extern struct mbstat mbstat; /* General mbuf stats/infos */ michael@0: michael@0: michael@0: /* michael@0: * Evaluate TRUE if it's safe to write to the mbuf m's data region (this can michael@0: * be both the local data payload, or an external buffer area, depending on michael@0: * whether M_EXT is set). michael@0: */ michael@0: #define M_WRITABLE(m) (!((m)->m_flags & M_RDONLY) && \ michael@0: (!(((m)->m_flags & M_EXT)) || \ michael@0: (*((m)->m_ext.ref_cnt) == 1)) ) \ michael@0: michael@0: michael@0: /* michael@0: * Compute the amount of space available before the current start of data in michael@0: * an mbuf. michael@0: * michael@0: * The M_WRITABLE() is a temporary, conservative safety measure: the burden michael@0: * of checking writability of the mbuf data area rests solely with the caller. michael@0: */ michael@0: #define M_LEADINGSPACE(m) \ michael@0: ((m)->m_flags & M_EXT ? \ michael@0: (M_WRITABLE(m) ? (m)->m_data - (m)->m_ext.ext_buf : 0): \ michael@0: (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \ michael@0: (m)->m_data - (m)->m_dat) michael@0: michael@0: /* michael@0: * Compute the amount of space available after the end of data in an mbuf. michael@0: * michael@0: * The M_WRITABLE() is a temporary, conservative safety measure: the burden michael@0: * of checking writability of the mbuf data area rests solely with the caller. michael@0: */ michael@0: #define M_TRAILINGSPACE(m) \ michael@0: ((m)->m_flags & M_EXT ? \ michael@0: (M_WRITABLE(m) ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size \ michael@0: - ((m)->m_data + (m)->m_len) : 0) : \ michael@0: &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len)) michael@0: michael@0: michael@0: michael@0: /* michael@0: * Arrange to prepend space of size plen to mbuf m. If a new mbuf must be michael@0: * allocated, how specifies whether to wait. If the allocation fails, the michael@0: * original mbuf chain is freed and m is set to NULL. michael@0: */ michael@0: #define M_PREPEND(m, plen, how) do { \ michael@0: struct mbuf **_mmp = &(m); \ michael@0: struct mbuf *_mm = *_mmp; \ michael@0: int _mplen = (plen); \ michael@0: int __mhow = (how); \ michael@0: \ michael@0: if (M_LEADINGSPACE(_mm) >= _mplen) { \ michael@0: _mm->m_data -= _mplen; \ michael@0: _mm->m_len += _mplen; \ michael@0: } else \ michael@0: _mm = m_prepend(_mm, _mplen, __mhow); \ michael@0: if (_mm != NULL && _mm->m_flags & M_PKTHDR) \ michael@0: _mm->m_pkthdr.len += _mplen; \ michael@0: *_mmp = _mm; \ michael@0: } while (0) michael@0: michael@0: /* michael@0: * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place an michael@0: * object of the specified size at the end of the mbuf, longword aligned. michael@0: */ michael@0: #define M_ALIGN(m, len) do { \ michael@0: KASSERT(!((m)->m_flags & (M_PKTHDR|M_EXT)), \ michael@0: ("%s: M_ALIGN not normal mbuf", __func__)); \ michael@0: KASSERT((m)->m_data == (m)->m_dat, \ michael@0: ("%s: M_ALIGN not a virgin mbuf", __func__)); \ michael@0: (m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1); \ michael@0: } while (0) michael@0: michael@0: /* michael@0: * As above, for mbufs allocated with m_gethdr/MGETHDR or initialized by michael@0: * M_DUP/MOVE_PKTHDR. michael@0: */ michael@0: #define MH_ALIGN(m, len) do { \ michael@0: KASSERT((m)->m_flags & M_PKTHDR && !((m)->m_flags & M_EXT), \ michael@0: ("%s: MH_ALIGN not PKTHDR mbuf", __func__)); \ michael@0: KASSERT((m)->m_data == (m)->m_pktdat, \ michael@0: ("%s: MH_ALIGN not a virgin mbuf", __func__)); \ michael@0: (m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1); \ michael@0: } while (0) michael@0: michael@0: #endif