user_mbuf.h (14828B)
1 /*- 2 * Copyright (c) 1982, 1986, 1988, 1993 3 * The Regents of the University of California. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of the University nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 */ 31 32 #ifndef _USER_MBUF_H_ 33 #define _USER_MBUF_H_ 34 35 /* __Userspace__ header file for mbufs */ 36 #include <stdio.h> 37 #if !defined(SCTP_SIMPLE_ALLOCATOR) 38 #include "umem.h" 39 #endif 40 #include "user_malloc.h" 41 #include "netinet/sctp_os_userspace.h" 42 43 #define USING_MBUF_CONSTRUCTOR 0 44 45 /* For Linux */ 46 #ifndef MSIZE 47 #define MSIZE 256 48 /* #define MSIZE 1024 */ 49 #endif 50 #ifndef MCLBYTES 51 #define MCLBYTES 2048 52 #endif 53 54 struct mbuf * m_gethdr(int how, short type); 55 struct mbuf * m_get(int how, short type); 56 struct mbuf * m_free(struct mbuf *m); 57 void m_clget(struct mbuf *m, int how); 58 struct mbuf * m_getm2(struct mbuf *m, int len, int how, short type, int flags, int allonebuf); 59 struct mbuf *m_uiotombuf(struct uio *uio, int how, int len, int align, int flags); 60 u_int m_length(struct mbuf *m0, struct mbuf **last); 61 struct mbuf *m_last(struct mbuf *m); 62 63 /* mbuf initialization function */ 64 void mbuf_initialize(void *); 65 66 #define M_MOVE_PKTHDR(to, from) m_move_pkthdr((to), (from)) 67 #define MGET(m, how, type) ((m) = m_get((how), (type))) 68 #define MGETHDR(m, how, type) ((m) = m_gethdr((how), (type))) 69 #define MCLGET(m, how) m_clget((m), (how)) 70 71 72 #define M_HDR_PAD ((sizeof(intptr_t)==4) ? 2 : 6) /* modified for __Userspace__ */ 73 74 /* Length to m_copy to copy all. */ 75 #define M_COPYALL 1000000000 76 77 /* umem_cache_t is defined in user_include/umem.h as 78 * typedef struct umem_cache umem_cache_t; 79 * Note:umem_zone_t is a pointer. 80 */ 81 #if defined(SCTP_SIMPLE_ALLOCATOR) 82 typedef size_t sctp_zone_t; 83 #else 84 typedef umem_cache_t *sctp_zone_t; 85 #endif 86 87 extern sctp_zone_t zone_mbuf; 88 extern sctp_zone_t zone_clust; 89 extern sctp_zone_t zone_ext_refcnt; 90 91 /*- 92 * Macros for type conversion: 93 * mtod(m, t) -- Convert mbuf pointer to data pointer of correct type. 94 * dtom(x) -- Convert data pointer within mbuf to mbuf pointer (XXX). 95 */ 96 #define mtod(m, t) ((t)((m)->m_data)) 97 #define dtom(x) ((struct mbuf *)((intptr_t)(x) & ~(MSIZE-1))) 98 99 struct mb_args { 100 int flags; /* Flags for mbuf being allocated */ 101 short type; /* Type of mbuf being allocated */ 102 }; 103 104 struct clust_args { 105 struct mbuf * parent_mbuf; 106 }; 107 108 struct mbuf * m_split(struct mbuf *, int, int); 109 void m_cat(struct mbuf *m, struct mbuf *n); 110 void m_adj(struct mbuf *, int); 111 void mb_free_ext(struct mbuf *); 112 void m_freem(struct mbuf *); 113 struct m_tag *m_tag_alloc(uint32_t, int, int, int); 114 struct mbuf *m_copym(struct mbuf *, int, int, int); 115 void m_copyback(struct mbuf *, int, int, caddr_t); 116 int m_apply(struct mbuf *, int, int, int (*)(void *, void *, u_int), void *arg); 117 struct mbuf *m_pullup(struct mbuf *, int); 118 struct mbuf *m_pulldown(struct mbuf *, int off, int len, int *offp); 119 int m_dup_pkthdr(struct mbuf *, struct mbuf *, int); 120 struct m_tag *m_tag_copy(struct m_tag *, int); 121 int m_tag_copy_chain(struct mbuf *, struct mbuf *, int); 122 struct mbuf *m_prepend(struct mbuf *, int, int); 123 void m_copydata(const struct mbuf *, int, int, caddr_t); 124 125 #define MBUF_MEM_NAME "mbuf" 126 #define MBUF_CLUSTER_MEM_NAME "mbuf_cluster" 127 #define MBUF_EXTREFCNT_MEM_NAME "mbuf_ext_refcnt" 128 129 /* 130 * Mbufs are of a single size, MSIZE (sys/param.h), which includes overhead. 131 * An mbuf may add a single "mbuf cluster" of size MCLBYTES (also in 132 * sys/param.h), which has no additional overhead and is used instead of the 133 * internal data area; this is done when at least MINCLSIZE of data must be 134 * stored. Additionally, it is possible to allocate a separate buffer 135 * externally and attach it to the mbuf in a way similar to that of mbuf 136 * clusters. 137 */ 138 #define MLEN ((int)(MSIZE - sizeof(struct m_hdr))) /* normal data len */ 139 #define MHLEN ((int)(MLEN - sizeof(struct pkthdr))) /* data len w/pkthdr */ 140 #define MINCLSIZE ((int)(MHLEN + 1)) /* smallest amount to put in cluster */ 141 #define M_MAXCOMPRESS (MHLEN / 2) /* max amount to copy for compression */ 142 143 144 /* 145 * Header present at the beginning of every mbuf. 146 */ 147 struct m_hdr { 148 struct mbuf *mh_next; /* next buffer in chain */ 149 struct mbuf *mh_nextpkt; /* next chain in queue/record */ 150 caddr_t mh_data; /* location of data */ 151 int mh_len; /* amount of data in this mbuf */ 152 int mh_flags; /* flags; see below */ 153 short mh_type; /* type of data in this mbuf */ 154 uint8_t pad[M_HDR_PAD];/* word align */ 155 }; 156 157 /* 158 * Packet tag structure (see below for details). 159 */ 160 struct m_tag { 161 SLIST_ENTRY(m_tag) m_tag_link; /* List of packet tags */ 162 uint16_t m_tag_id; /* Tag ID */ 163 uint16_t m_tag_len; /* Length of data */ 164 uint32_t m_tag_cookie; /* ABI/Module ID */ 165 void (*m_tag_free)(struct m_tag *); 166 }; 167 168 /* 169 * Record/packet header in first mbuf of chain; valid only if M_PKTHDR is set. 170 */ 171 struct pkthdr { 172 struct ifnet *rcvif; /* rcv interface */ 173 /* variables for ip and tcp reassembly */ 174 void *header; /* pointer to packet header */ 175 int len; /* total packet length */ 176 /* variables for hardware checksum */ 177 int csum_flags; /* flags regarding checksum */ 178 int csum_data; /* data field used by csum routines */ 179 uint16_t tso_segsz; /* TSO segment size */ 180 uint16_t ether_vtag; /* Ethernet 802.1p+q vlan tag */ 181 SLIST_HEAD(packet_tags, m_tag) tags; /* list of packet tags */ 182 }; 183 184 /* 185 * Description of external storage mapped into mbuf; valid only if M_EXT is 186 * set. 187 */ 188 struct m_ext { 189 caddr_t ext_buf; /* start of buffer */ 190 void (*ext_free) /* free routine if not the usual */ 191 (void *, void *); 192 void *ext_args; /* optional argument pointer */ 193 u_int ext_size; /* size of buffer, for ext_free */ 194 volatile u_int *ref_cnt; /* pointer to ref count info */ 195 int ext_type; /* type of external storage */ 196 }; 197 198 199 /* 200 * The core of the mbuf object along with some shortcut defined for practical 201 * purposes. 202 */ 203 struct mbuf { 204 struct m_hdr m_hdr; 205 union { 206 struct { 207 struct pkthdr MH_pkthdr; /* M_PKTHDR set */ 208 union { 209 struct m_ext MH_ext; /* M_EXT set */ 210 char MH_databuf[MHLEN]; 211 } MH_dat; 212 } MH; 213 char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */ 214 } M_dat; 215 }; 216 217 #define m_next m_hdr.mh_next 218 #define m_len m_hdr.mh_len 219 #define m_data m_hdr.mh_data 220 #define m_type m_hdr.mh_type 221 #define m_flags m_hdr.mh_flags 222 #define m_nextpkt m_hdr.mh_nextpkt 223 #define m_act m_nextpkt 224 #define m_pkthdr M_dat.MH.MH_pkthdr 225 #define m_ext M_dat.MH.MH_dat.MH_ext 226 #define m_pktdat M_dat.MH.MH_dat.MH_databuf 227 #define m_dat M_dat.M_databuf 228 229 230 /* 231 * mbuf flags. 232 */ 233 #define M_EXT 0x0001 /* has associated external storage */ 234 #define M_PKTHDR 0x0002 /* start of record */ 235 #define M_EOR 0x0004 /* end of record */ 236 #define M_RDONLY 0x0008 /* associated data is marked read-only */ 237 #define M_PROTO1 0x0010 /* protocol-specific */ 238 #define M_PROTO2 0x0020 /* protocol-specific */ 239 #define M_PROTO3 0x0040 /* protocol-specific */ 240 #define M_PROTO4 0x0080 /* protocol-specific */ 241 #define M_PROTO5 0x0100 /* protocol-specific */ 242 #define M_FREELIST 0x8000 /* mbuf is on the free list */ 243 244 245 /* 246 * Flags copied when copying m_pkthdr. 247 */ 248 #define M_COPYFLAGS (M_PKTHDR|M_EOR|M_RDONLY|M_PROTO1|M_PROTO1|M_PROTO2|\ 249 M_PROTO3|M_PROTO4|M_PROTO5|\ 250 M_BCAST|M_MCAST|M_FRAG|M_FIRSTFRAG|M_LASTFRAG|\ 251 M_VLANTAG|M_PROMISC) 252 253 254 /* 255 * mbuf pkthdr flags (also stored in m_flags). 256 */ 257 #define M_BCAST 0x0200 /* send/received as link-level broadcast */ 258 #define M_MCAST 0x0400 /* send/received as link-level multicast */ 259 #define M_FRAG 0x0800 /* packet is a fragment of a larger packet */ 260 #define M_FIRSTFRAG 0x1000 /* packet is first fragment */ 261 #define M_LASTFRAG 0x2000 /* packet is last fragment */ 262 #define M_VLANTAG 0x10000 /* ether_vtag is valid */ 263 #define M_PROMISC 0x20000 /* packet was not for us */ 264 #define M_NOFREE 0x40000 /* do not free mbuf - it is embedded in the cluster */ 265 266 267 /* 268 * External buffer types: identify ext_buf type. 269 */ 270 #define EXT_CLUSTER 1 /* mbuf cluster */ 271 #define EXT_SFBUF 2 /* sendfile(2)'s sf_bufs */ 272 #define EXT_JUMBOP 3 /* jumbo cluster 4096 bytes */ 273 #define EXT_JUMBO9 4 /* jumbo cluster 9216 bytes */ 274 #define EXT_JUMBO16 5 /* jumbo cluster 16184 bytes */ 275 #define EXT_PACKET 6 /* mbuf+cluster from packet zone */ 276 #define EXT_MBUF 7 /* external mbuf reference (M_IOVEC) */ 277 #define EXT_NET_DRV 100 /* custom ext_buf provided by net driver(s) */ 278 #define EXT_MOD_TYPE 200 /* custom module's ext_buf type */ 279 #define EXT_DISPOSABLE 300 /* can throw this buffer away w/page flipping */ 280 #define EXT_EXTREF 400 /* has externally maintained ref_cnt ptr */ 281 282 283 /* 284 * mbuf types. 285 */ 286 #define MT_NOTMBUF 0 /* USED INTERNALLY ONLY! Object is not mbuf */ 287 #define MT_DATA 1 /* dynamic (data) allocation */ 288 #define MT_HEADER MT_DATA /* packet header, use M_PKTHDR instead */ 289 #define MT_SONAME 8 /* socket name */ 290 #define MT_CONTROL 14 /* extra-data protocol message */ 291 #define MT_OOBDATA 15 /* expedited data */ 292 #define MT_NTYPES 16 /* number of mbuf types for mbtypes[] */ 293 294 /* 295 * __Userspace__ flags like M_NOWAIT are defined in malloc.h 296 * Flags like these are used in functions like uma_zalloc() 297 * but don't have an equivalent in userland umem 298 * Flags specifying how an allocation should be made. 299 * 300 * The flag to use is as follows: 301 * - M_DONTWAIT or M_NOWAIT from an interrupt handler to not block allocation. 302 * - M_WAIT or M_WAITOK or M_TRYWAIT from wherever it is safe to block. 303 * 304 * M_DONTWAIT/M_NOWAIT means that we will not block the thread explicitly and 305 * if we cannot allocate immediately we may return NULL, whereas 306 * M_WAIT/M_WAITOK/M_TRYWAIT means that if we cannot allocate resources we 307 * will block until they are available, and thus never return NULL. 308 * 309 * XXX Eventually just phase this out to use M_WAITOK/M_NOWAIT. 310 */ 311 #define MBTOM(how) (how) 312 313 void m_tag_delete(struct mbuf *, struct m_tag *); 314 void m_tag_delete_chain(struct mbuf *, struct m_tag *); 315 void m_move_pkthdr(struct mbuf *, struct mbuf *); 316 void m_tag_free_default(struct m_tag *); 317 318 extern int max_linkhdr; /* Largest link-level header */ 319 extern int max_protohdr; /* Size of largest protocol layer header. See user_mbuf.c */ 320 321 /* 322 * Evaluate TRUE if it's safe to write to the mbuf m's data region (this can 323 * be both the local data payload, or an external buffer area, depending on 324 * whether M_EXT is set). 325 */ 326 #define M_WRITABLE(m) (!((m)->m_flags & M_RDONLY) && \ 327 (!(((m)->m_flags & M_EXT)) || \ 328 (*((m)->m_ext.ref_cnt) == 1)) ) \ 329 330 /* Check if the supplied mbuf has a packet header, or else panic. */ 331 #define M_ASSERTPKTHDR(m) \ 332 KASSERT((m) != NULL && (m)->m_flags & M_PKTHDR, \ 333 ("%s: no mbuf packet header!", __func__)) 334 335 /* 336 * Compute the amount of space available before the current start of data in 337 * an mbuf. 338 * 339 * The M_WRITABLE() is a temporary, conservative safety measure: the burden 340 * of checking writability of the mbuf data area rests solely with the caller. 341 */ 342 #define M_LEADINGSPACE(m) \ 343 (((m)->m_flags & M_EXT) ? \ 344 (M_WRITABLE(m) ? (m)->m_data - (m)->m_ext.ext_buf : 0): \ 345 ((m)->m_flags & M_PKTHDR)? (m)->m_data - (m)->m_pktdat : \ 346 (m)->m_data - (m)->m_dat) 347 348 /* 349 * Compute the amount of space available after the end of data in an mbuf. 350 * 351 * The M_WRITABLE() is a temporary, conservative safety measure: the burden 352 * of checking writability of the mbuf data area rests solely with the caller. 353 */ 354 #define M_TRAILINGSPACE(m) \ 355 (((m)->m_flags & M_EXT) ? \ 356 (M_WRITABLE(m) ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size \ 357 - ((m)->m_data + (m)->m_len) : 0) : \ 358 &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len)) 359 360 361 362 /* 363 * Arrange to prepend space of size plen to mbuf m. If a new mbuf must be 364 * allocated, how specifies whether to wait. If the allocation fails, the 365 * original mbuf chain is freed and m is set to NULL. 366 */ 367 #define M_PREPEND(m, plen, how) do { \ 368 struct mbuf **_mmp = &(m); \ 369 struct mbuf *_mm = *_mmp; \ 370 int _mplen = (plen); \ 371 int __mhow = (how); \ 372 \ 373 if (M_LEADINGSPACE(_mm) >= _mplen) { \ 374 _mm->m_data -= _mplen; \ 375 _mm->m_len += _mplen; \ 376 } else \ 377 _mm = m_prepend(_mm, _mplen, __mhow); \ 378 if (_mm != NULL && _mm->m_flags & M_PKTHDR) \ 379 _mm->m_pkthdr.len += _mplen; \ 380 *_mmp = _mm; \ 381 } while (0) 382 383 /* 384 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place an 385 * object of the specified size at the end of the mbuf, longword aligned. 386 */ 387 #define M_ALIGN(m, len) do { \ 388 KASSERT(!((m)->m_flags & (M_PKTHDR|M_EXT)), \ 389 ("%s: M_ALIGN not normal mbuf", __func__)); \ 390 KASSERT((m)->m_data == (m)->m_dat, \ 391 ("%s: M_ALIGN not a virgin mbuf", __func__)); \ 392 (m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1); \ 393 } while (0) 394 395 /* 396 * As above, for mbufs allocated with m_gethdr/MGETHDR or initialized by 397 * M_DUP/MOVE_PKTHDR. 398 */ 399 #define MH_ALIGN(m, len) do { \ 400 KASSERT((m)->m_flags & M_PKTHDR && !((m)->m_flags & M_EXT), \ 401 ("%s: MH_ALIGN not PKTHDR mbuf", __func__)); \ 402 KASSERT((m)->m_data == (m)->m_pktdat, \ 403 ("%s: MH_ALIGN not a virgin mbuf", __func__)); \ 404 (m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1); \ 405 } while (0) 406 407 #define M_SIZE(m) \ 408 (((m)->m_flags & M_EXT) ? (m)->m_ext.ext_size : \ 409 ((m)->m_flags & M_PKTHDR) ? MHLEN : \ 410 MLEN) 411 412 #endif