1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/sctp/src/user_mbuf.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,438 @@ 1.4 +/*- 1.5 + * Copyright (c) 1982, 1986, 1988, 1993 1.6 + * The Regents of the University of California. 1.7 + * All rights reserved. 1.8 + * 1.9 + * Redistribution and use in source and binary forms, with or without 1.10 + * modification, are permitted provided that the following conditions 1.11 + * are met: 1.12 + * 1. Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 2. Redistributions in binary form must reproduce the above copyright 1.15 + * notice, this list of conditions and the following disclaimer in the 1.16 + * documentation and/or other materials provided with the distribution. 1.17 + * 3. Neither the name of the University nor the names of its contributors 1.18 + * may be used to endorse or promote products derived from this software 1.19 + * without specific prior written permission. 1.20 + * 1.21 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1.22 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.23 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.24 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 1.25 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1.26 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 1.27 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 1.28 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 1.29 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 1.30 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 1.31 + * SUCH DAMAGE. 1.32 + * 1.33 + */ 1.34 + 1.35 +#ifndef _USER_MBUF_H_ 1.36 +#define _USER_MBUF_H_ 1.37 + 1.38 +/* __Userspace__ header file for mbufs */ 1.39 +#include <stdio.h> 1.40 +#if !defined(SCTP_SIMPLE_ALLOCATOR) 1.41 +#include "umem.h" 1.42 +#endif 1.43 +#include "user_malloc.h" 1.44 +#include "netinet/sctp_os_userspace.h" 1.45 + 1.46 +#define USING_MBUF_CONSTRUCTOR 0 1.47 + 1.48 +/* For Linux */ 1.49 +#ifndef MSIZE 1.50 +#define MSIZE 256 1.51 +/* #define MSIZE 1024 */ 1.52 +#endif 1.53 +#ifndef MCLBYTES 1.54 +#define MCLBYTES 2048 1.55 +#endif 1.56 + 1.57 +struct mbuf * m_gethdr(int how, short type); 1.58 +struct mbuf * m_get(int how, short type); 1.59 +struct mbuf * m_free(struct mbuf *m); 1.60 +void m_clget(struct mbuf *m, int how); 1.61 + 1.62 + 1.63 +/* mbuf initialization function */ 1.64 +void mbuf_init(void *); 1.65 + 1.66 +#define M_MOVE_PKTHDR(to, from) m_move_pkthdr((to), (from)) 1.67 +#define MGET(m, how, type) ((m) = m_get((how), (type))) 1.68 +#define MGETHDR(m, how, type) ((m) = m_gethdr((how), (type))) 1.69 +#define MCLGET(m, how) m_clget((m), (how)) 1.70 + 1.71 + 1.72 +#define M_HDR_PAD ((sizeof(intptr_t)==4) ? 2 : 6) /* modified for __Userspace__ */ 1.73 + 1.74 +/* Length to m_copy to copy all. */ 1.75 +#define M_COPYALL 1000000000 1.76 + 1.77 +/* umem_cache_t is defined in user_include/umem.h as 1.78 + * typedef struct umem_cache umem_cache_t; 1.79 + * Note:umem_zone_t is a pointer. 1.80 + */ 1.81 +#if defined(SCTP_SIMPLE_ALLOCATOR) 1.82 +typedef size_t sctp_zone_t; 1.83 +#else 1.84 +typedef umem_cache_t *sctp_zone_t; 1.85 +#endif 1.86 + 1.87 +extern sctp_zone_t zone_mbuf; 1.88 +extern sctp_zone_t zone_clust; 1.89 +extern sctp_zone_t zone_ext_refcnt; 1.90 + 1.91 +/*- 1.92 + * Macros for type conversion: 1.93 + * mtod(m, t) -- Convert mbuf pointer to data pointer of correct type. 1.94 + * dtom(x) -- Convert data pointer within mbuf to mbuf pointer (XXX). 1.95 + */ 1.96 +#define mtod(m, t) ((t)((m)->m_data)) 1.97 +#define dtom(x) ((struct mbuf *)((intptr_t)(x) & ~(MSIZE-1))) 1.98 + 1.99 +struct mb_args { 1.100 + int flags; /* Flags for mbuf being allocated */ 1.101 + short type; /* Type of mbuf being allocated */ 1.102 +}; 1.103 + 1.104 +struct clust_args { 1.105 + struct mbuf * parent_mbuf; 1.106 +}; 1.107 + 1.108 +struct mbuf * m_split(struct mbuf *, int, int); 1.109 +void m_cat(struct mbuf *m, struct mbuf *n); 1.110 +void m_adj(struct mbuf *, int); 1.111 +void mb_free_ext(struct mbuf *); 1.112 +void m_freem(struct mbuf *); 1.113 +struct m_tag *m_tag_alloc(u_int32_t, int, int, int); 1.114 +struct mbuf *m_copym(struct mbuf *, int, int, int); 1.115 +void m_copyback(struct mbuf *, int, int, caddr_t); 1.116 +struct mbuf *m_pullup(struct mbuf *, int); 1.117 +struct mbuf *m_pulldown(struct mbuf *, int off, int len, int *offp); 1.118 +int m_dup_pkthdr(struct mbuf *, struct mbuf *, int); 1.119 +struct m_tag *m_tag_copy(struct m_tag *, int); 1.120 +int m_tag_copy_chain(struct mbuf *, struct mbuf *, int); 1.121 +struct mbuf *m_prepend(struct mbuf *, int, int); 1.122 +void m_copydata(const struct mbuf *, int, int, caddr_t); 1.123 + 1.124 +#define MBUF_MEM_NAME "mbuf" 1.125 +#define MBUF_CLUSTER_MEM_NAME "mbuf_cluster" 1.126 +#define MBUF_EXTREFCNT_MEM_NAME "mbuf_ext_refcnt" 1.127 + 1.128 +#define MT_NOINIT 255 /* Not a type but a flag to allocate 1.129 + a non-initialized mbuf */ 1.130 + 1.131 +/* 1.132 + * General mbuf allocator statistics structure. 1.133 + * __Userspace__ mbstat may be useful for gathering statistics. 1.134 + * In the kernel many of these statistics are no longer used as 1.135 + * they track allocator statistics through kernel UMA's built in statistics mechanism. 1.136 + */ 1.137 +struct mbstat { 1.138 + u_long m_mbufs; /* XXX */ 1.139 + u_long m_mclusts; /* XXX */ 1.140 + 1.141 + u_long m_drain; /* times drained protocols for space */ 1.142 + u_long m_mcfail; /* XXX: times m_copym failed */ 1.143 + u_long m_mpfail; /* XXX: times m_pullup failed */ 1.144 + u_long m_msize; /* length of an mbuf */ 1.145 + u_long m_mclbytes; /* length of an mbuf cluster */ 1.146 + u_long m_minclsize; /* min length of data to allocate a cluster */ 1.147 + u_long m_mlen; /* length of data in an mbuf */ 1.148 + u_long m_mhlen; /* length of data in a header mbuf */ 1.149 + 1.150 + /* Number of mbtypes (gives # elems in mbtypes[] array: */ 1.151 + short m_numtypes; 1.152 + 1.153 + /* XXX: Sendfile stats should eventually move to their own struct */ 1.154 + u_long sf_iocnt; /* times sendfile had to do disk I/O */ 1.155 + u_long sf_allocfail; /* times sfbuf allocation failed */ 1.156 + u_long sf_allocwait; /* times sfbuf allocation had to wait */ 1.157 +}; 1.158 + 1.159 + 1.160 +/* 1.161 + * Mbufs are of a single size, MSIZE (sys/param.h), which includes overhead. 1.162 + * An mbuf may add a single "mbuf cluster" of size MCLBYTES (also in 1.163 + * sys/param.h), which has no additional overhead and is used instead of the 1.164 + * internal data area; this is done when at least MINCLSIZE of data must be 1.165 + * stored. Additionally, it is possible to allocate a separate buffer 1.166 + * externally and attach it to the mbuf in a way similar to that of mbuf 1.167 + * clusters. 1.168 + */ 1.169 +#define MLEN ((int)(MSIZE - sizeof(struct m_hdr))) /* normal data len */ 1.170 +#define MHLEN ((int)(MLEN - sizeof(struct pkthdr))) /* data len w/pkthdr */ 1.171 +#define MINCLSIZE ((int)(MHLEN + 1)) /* smallest amount to put in cluster */ 1.172 +#define M_MAXCOMPRESS (MHLEN / 2) /* max amount to copy for compression */ 1.173 + 1.174 + 1.175 +/* 1.176 + * Header present at the beginning of every mbuf. 1.177 + */ 1.178 +struct m_hdr { 1.179 + struct mbuf *mh_next; /* next buffer in chain */ 1.180 + struct mbuf *mh_nextpkt; /* next chain in queue/record */ 1.181 + caddr_t mh_data; /* location of data */ 1.182 + int mh_len; /* amount of data in this mbuf */ 1.183 + int mh_flags; /* flags; see below */ 1.184 + short mh_type; /* type of data in this mbuf */ 1.185 + uint8_t pad[M_HDR_PAD];/* word align */ 1.186 +}; 1.187 + 1.188 +/* 1.189 + * Packet tag structure (see below for details). 1.190 + */ 1.191 +struct m_tag { 1.192 + SLIST_ENTRY(m_tag) m_tag_link; /* List of packet tags */ 1.193 + u_int16_t m_tag_id; /* Tag ID */ 1.194 + u_int16_t m_tag_len; /* Length of data */ 1.195 + u_int32_t m_tag_cookie; /* ABI/Module ID */ 1.196 + void (*m_tag_free)(struct m_tag *); 1.197 +}; 1.198 + 1.199 +/* 1.200 + * Record/packet header in first mbuf of chain; valid only if M_PKTHDR is set. 1.201 + */ 1.202 +struct pkthdr { 1.203 + struct ifnet *rcvif; /* rcv interface */ 1.204 + /* variables for ip and tcp reassembly */ 1.205 + void *header; /* pointer to packet header */ 1.206 + int len; /* total packet length */ 1.207 + /* variables for hardware checksum */ 1.208 + int csum_flags; /* flags regarding checksum */ 1.209 + int csum_data; /* data field used by csum routines */ 1.210 + u_int16_t tso_segsz; /* TSO segment size */ 1.211 + u_int16_t ether_vtag; /* Ethernet 802.1p+q vlan tag */ 1.212 + SLIST_HEAD(packet_tags, m_tag) tags; /* list of packet tags */ 1.213 +}; 1.214 + 1.215 +/* 1.216 + * Description of external storage mapped into mbuf; valid only if M_EXT is 1.217 + * set. 1.218 + */ 1.219 +struct m_ext { 1.220 + caddr_t ext_buf; /* start of buffer */ 1.221 + void (*ext_free) /* free routine if not the usual */ 1.222 + (void *, void *); 1.223 + void *ext_args; /* optional argument pointer */ 1.224 + u_int ext_size; /* size of buffer, for ext_free */ 1.225 + volatile u_int *ref_cnt; /* pointer to ref count info */ 1.226 + int ext_type; /* type of external storage */ 1.227 +}; 1.228 + 1.229 + 1.230 +/* 1.231 + * The core of the mbuf object along with some shortcut defined for practical 1.232 + * purposes. 1.233 + */ 1.234 +struct mbuf { 1.235 + struct m_hdr m_hdr; 1.236 + union { 1.237 + struct { 1.238 + struct pkthdr MH_pkthdr; /* M_PKTHDR set */ 1.239 + union { 1.240 + struct m_ext MH_ext; /* M_EXT set */ 1.241 + char MH_databuf[MHLEN]; 1.242 + } MH_dat; 1.243 + } MH; 1.244 + char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */ 1.245 + } M_dat; 1.246 +}; 1.247 + 1.248 +#define m_next m_hdr.mh_next 1.249 +#define m_len m_hdr.mh_len 1.250 +#define m_data m_hdr.mh_data 1.251 +#define m_type m_hdr.mh_type 1.252 +#define m_flags m_hdr.mh_flags 1.253 +#define m_nextpkt m_hdr.mh_nextpkt 1.254 +#define m_act m_nextpkt 1.255 +#define m_pkthdr M_dat.MH.MH_pkthdr 1.256 +#define m_ext M_dat.MH.MH_dat.MH_ext 1.257 +#define m_pktdat M_dat.MH.MH_dat.MH_databuf 1.258 +#define m_dat M_dat.M_databuf 1.259 + 1.260 + 1.261 +/* 1.262 + * mbuf flags. 1.263 + */ 1.264 +#define M_EXT 0x0001 /* has associated external storage */ 1.265 +#define M_PKTHDR 0x0002 /* start of record */ 1.266 +#define M_EOR 0x0004 /* end of record */ 1.267 +#define M_RDONLY 0x0008 /* associated data is marked read-only */ 1.268 +#define M_PROTO1 0x0010 /* protocol-specific */ 1.269 +#define M_PROTO2 0x0020 /* protocol-specific */ 1.270 +#define M_PROTO3 0x0040 /* protocol-specific */ 1.271 +#define M_PROTO4 0x0080 /* protocol-specific */ 1.272 +#define M_PROTO5 0x0100 /* protocol-specific */ 1.273 +#define M_SKIP_FIREWALL 0x4000 /* skip firewall processing */ 1.274 +#define M_FREELIST 0x8000 /* mbuf is on the free list */ 1.275 + 1.276 + 1.277 +/* 1.278 + * Flags copied when copying m_pkthdr. 1.279 + */ 1.280 +#define M_COPYFLAGS (M_PKTHDR|M_EOR|M_RDONLY|M_PROTO1|M_PROTO1|M_PROTO2|\ 1.281 + M_PROTO3|M_PROTO4|M_PROTO5|M_SKIP_FIREWALL|\ 1.282 + M_BCAST|M_MCAST|M_FRAG|M_FIRSTFRAG|M_LASTFRAG|\ 1.283 + M_VLANTAG|M_PROMISC) 1.284 + 1.285 + 1.286 +/* 1.287 + * mbuf pkthdr flags (also stored in m_flags). 1.288 + */ 1.289 +#define M_BCAST 0x0200 /* send/received as link-level broadcast */ 1.290 +#define M_MCAST 0x0400 /* send/received as link-level multicast */ 1.291 +#define M_FRAG 0x0800 /* packet is a fragment of a larger packet */ 1.292 +#define M_FIRSTFRAG 0x1000 /* packet is first fragment */ 1.293 +#define M_LASTFRAG 0x2000 /* packet is last fragment */ 1.294 +#define M_VLANTAG 0x10000 /* ether_vtag is valid */ 1.295 +#define M_PROMISC 0x20000 /* packet was not for us */ 1.296 +#define M_NOFREE 0x40000 /* do not free mbuf - it is embedded in the cluster */ 1.297 + 1.298 + 1.299 +/* 1.300 + * External buffer types: identify ext_buf type. 1.301 + */ 1.302 +#define EXT_CLUSTER 1 /* mbuf cluster */ 1.303 +#define EXT_SFBUF 2 /* sendfile(2)'s sf_bufs */ 1.304 +#define EXT_JUMBOP 3 /* jumbo cluster 4096 bytes */ 1.305 +#define EXT_JUMBO9 4 /* jumbo cluster 9216 bytes */ 1.306 +#define EXT_JUMBO16 5 /* jumbo cluster 16184 bytes */ 1.307 +#define EXT_PACKET 6 /* mbuf+cluster from packet zone */ 1.308 +#define EXT_MBUF 7 /* external mbuf reference (M_IOVEC) */ 1.309 +#define EXT_NET_DRV 100 /* custom ext_buf provided by net driver(s) */ 1.310 +#define EXT_MOD_TYPE 200 /* custom module's ext_buf type */ 1.311 +#define EXT_DISPOSABLE 300 /* can throw this buffer away w/page flipping */ 1.312 +#define EXT_EXTREF 400 /* has externally maintained ref_cnt ptr */ 1.313 + 1.314 + 1.315 +/* 1.316 + * mbuf types. 1.317 + */ 1.318 +#define MT_NOTMBUF 0 /* USED INTERNALLY ONLY! Object is not mbuf */ 1.319 +#define MT_DATA 1 /* dynamic (data) allocation */ 1.320 +#define MT_HEADER MT_DATA /* packet header, use M_PKTHDR instead */ 1.321 +#define MT_SONAME 8 /* socket name */ 1.322 +#define MT_CONTROL 14 /* extra-data protocol message */ 1.323 +#define MT_OOBDATA 15 /* expedited data */ 1.324 +#define MT_NTYPES 16 /* number of mbuf types for mbtypes[] */ 1.325 + 1.326 +#define MT_NOINIT 255 /* Not a type but a flag to allocate 1.327 + a non-initialized mbuf */ 1.328 + 1.329 +/* 1.330 + * __Userspace__ flags like M_NOWAIT are defined in malloc.h 1.331 + * Flags like these are used in functions like uma_zalloc() 1.332 + * but don't have an equivalent in userland umem 1.333 + * Flags specifying how an allocation should be made. 1.334 + * 1.335 + * The flag to use is as follows: 1.336 + * - M_DONTWAIT or M_NOWAIT from an interrupt handler to not block allocation. 1.337 + * - M_WAIT or M_WAITOK or M_TRYWAIT from wherever it is safe to block. 1.338 + * 1.339 + * M_DONTWAIT/M_NOWAIT means that we will not block the thread explicitly and 1.340 + * if we cannot allocate immediately we may return NULL, whereas 1.341 + * M_WAIT/M_WAITOK/M_TRYWAIT means that if we cannot allocate resources we 1.342 + * will block until they are available, and thus never return NULL. 1.343 + * 1.344 + * XXX Eventually just phase this out to use M_WAITOK/M_NOWAIT. 1.345 + */ 1.346 +#define MBTOM(how) (how) 1.347 + 1.348 +void m_tag_delete(struct mbuf *, struct m_tag *); 1.349 +void m_tag_delete_chain(struct mbuf *, struct m_tag *); 1.350 +void m_move_pkthdr(struct mbuf *, struct mbuf *); 1.351 +void m_tag_free_default(struct m_tag *); 1.352 + 1.353 +extern int max_linkhdr; /* Largest link-level header */ 1.354 +extern int max_protohdr; /* Size of largest protocol layer header. See user_mbuf.c */ 1.355 + 1.356 +extern struct mbstat mbstat; /* General mbuf stats/infos */ 1.357 + 1.358 + 1.359 +/* 1.360 + * Evaluate TRUE if it's safe to write to the mbuf m's data region (this can 1.361 + * be both the local data payload, or an external buffer area, depending on 1.362 + * whether M_EXT is set). 1.363 + */ 1.364 +#define M_WRITABLE(m) (!((m)->m_flags & M_RDONLY) && \ 1.365 + (!(((m)->m_flags & M_EXT)) || \ 1.366 + (*((m)->m_ext.ref_cnt) == 1)) ) \ 1.367 + 1.368 + 1.369 +/* 1.370 + * Compute the amount of space available before the current start of data in 1.371 + * an mbuf. 1.372 + * 1.373 + * The M_WRITABLE() is a temporary, conservative safety measure: the burden 1.374 + * of checking writability of the mbuf data area rests solely with the caller. 1.375 + */ 1.376 +#define M_LEADINGSPACE(m) \ 1.377 + ((m)->m_flags & M_EXT ? \ 1.378 + (M_WRITABLE(m) ? (m)->m_data - (m)->m_ext.ext_buf : 0): \ 1.379 + (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \ 1.380 + (m)->m_data - (m)->m_dat) 1.381 + 1.382 +/* 1.383 + * Compute the amount of space available after the end of data in an mbuf. 1.384 + * 1.385 + * The M_WRITABLE() is a temporary, conservative safety measure: the burden 1.386 + * of checking writability of the mbuf data area rests solely with the caller. 1.387 + */ 1.388 +#define M_TRAILINGSPACE(m) \ 1.389 + ((m)->m_flags & M_EXT ? \ 1.390 + (M_WRITABLE(m) ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size \ 1.391 + - ((m)->m_data + (m)->m_len) : 0) : \ 1.392 + &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len)) 1.393 + 1.394 + 1.395 + 1.396 +/* 1.397 + * Arrange to prepend space of size plen to mbuf m. If a new mbuf must be 1.398 + * allocated, how specifies whether to wait. If the allocation fails, the 1.399 + * original mbuf chain is freed and m is set to NULL. 1.400 + */ 1.401 +#define M_PREPEND(m, plen, how) do { \ 1.402 + struct mbuf **_mmp = &(m); \ 1.403 + struct mbuf *_mm = *_mmp; \ 1.404 + int _mplen = (plen); \ 1.405 + int __mhow = (how); \ 1.406 + \ 1.407 + if (M_LEADINGSPACE(_mm) >= _mplen) { \ 1.408 + _mm->m_data -= _mplen; \ 1.409 + _mm->m_len += _mplen; \ 1.410 + } else \ 1.411 + _mm = m_prepend(_mm, _mplen, __mhow); \ 1.412 + if (_mm != NULL && _mm->m_flags & M_PKTHDR) \ 1.413 + _mm->m_pkthdr.len += _mplen; \ 1.414 + *_mmp = _mm; \ 1.415 +} while (0) 1.416 + 1.417 +/* 1.418 + * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place an 1.419 + * object of the specified size at the end of the mbuf, longword aligned. 1.420 + */ 1.421 +#define M_ALIGN(m, len) do { \ 1.422 + KASSERT(!((m)->m_flags & (M_PKTHDR|M_EXT)), \ 1.423 + ("%s: M_ALIGN not normal mbuf", __func__)); \ 1.424 + KASSERT((m)->m_data == (m)->m_dat, \ 1.425 + ("%s: M_ALIGN not a virgin mbuf", __func__)); \ 1.426 + (m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1); \ 1.427 +} while (0) 1.428 + 1.429 +/* 1.430 + * As above, for mbufs allocated with m_gethdr/MGETHDR or initialized by 1.431 + * M_DUP/MOVE_PKTHDR. 1.432 + */ 1.433 +#define MH_ALIGN(m, len) do { \ 1.434 + KASSERT((m)->m_flags & M_PKTHDR && !((m)->m_flags & M_EXT), \ 1.435 + ("%s: MH_ALIGN not PKTHDR mbuf", __func__)); \ 1.436 + KASSERT((m)->m_data == (m)->m_pktdat, \ 1.437 + ("%s: MH_ALIGN not a virgin mbuf", __func__)); \ 1.438 + (m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1); \ 1.439 +} while (0) 1.440 + 1.441 +#endif