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 * @(#)mbuf.h 8.5 (Berkeley) 2/19/95 31 * $FreeBSD$ 32 */ 33 34#ifndef _SYS_MBUF_H_ 35#define _SYS_MBUF_H_ 36 37/* XXX: These includes suck. Sorry! */ 38#include <sys/queue.h> 39#ifdef _KERNEL 40#include <sys/systm.h> 41#include <vm/uma.h> 42#ifdef WITNESS 43#include <sys/lock.h> 44#endif 45#endif 46 47/* 48 * Mbufs are of a single size, MSIZE (sys/param.h), which includes overhead. 49 * An mbuf may add a single "mbuf cluster" of size MCLBYTES (also in 50 * sys/param.h), which has no additional overhead and is used instead of the 51 * internal data area; this is done when at least MINCLSIZE of data must be 52 * stored. Additionally, it is possible to allocate a separate buffer 53 * externally and attach it to the mbuf in a way similar to that of mbuf 54 * clusters. 55 */ 56#define MLEN (MSIZE - sizeof(struct m_hdr)) /* normal data len */ 57#define MHLEN (MLEN - sizeof(struct pkthdr)) /* data len w/pkthdr */ 58#define MINCLSIZE (MHLEN + 1) /* smallest amount to put in cluster */ 59#define M_MAXCOMPRESS (MHLEN / 2) /* max amount to copy for compression */ 60 61#ifdef _KERNEL 62/*- 63 * Macro for type conversion: convert mbuf pointer to data pointer of correct 64 * type: 65 * 66 * mtod(m, t) -- Convert mbuf pointer to data pointer of correct type. 67 */ 68#define mtod(m, t) ((t)((m)->m_data)) 69 70/* 71 * Argument structure passed to UMA routines during mbuf and packet 72 * allocations. 73 */ 74struct mb_args { 75 int flags; /* Flags for mbuf being allocated */ 76 short type; /* Type of mbuf being allocated */ 77}; 78#endif /* _KERNEL */ 79 80#if defined(__LP64__) 81#define M_HDR_PAD 6 82#else 83#define M_HDR_PAD 2 84#endif 85 86/* 87 * Header present at the beginning of every mbuf. 88 */ 89struct m_hdr { 90 struct mbuf *mh_next; /* next buffer in chain */ 91 struct mbuf *mh_nextpkt; /* next chain in queue/record */ 92 caddr_t mh_data; /* location of data */ 93 int mh_len; /* amount of data in this mbuf */ 94 int mh_flags; /* flags; see below */ 95 short mh_type; /* type of data in this mbuf */ 96 uint8_t pad[M_HDR_PAD];/* word align */ 97}; 98 99/* 100 * Packet tag structure (see below for details). 101 */ 102struct m_tag { 103 SLIST_ENTRY(m_tag) m_tag_link; /* List of packet tags */ 104 u_int16_t m_tag_id; /* Tag ID */ 105 u_int16_t m_tag_len; /* Length of data */ 106 u_int32_t m_tag_cookie; /* ABI/Module ID */ 107 void (*m_tag_free)(struct m_tag *); 108}; 109 110/* 111 * Record/packet header in first mbuf of chain; valid only if M_PKTHDR is set. 112 */ 113struct pkthdr { 114 struct ifnet *rcvif; /* rcv interface */ 115 /* variables for ip and tcp reassembly */ 116 void *header; /* pointer to packet header */ 117 int len; /* total packet length */ 118 uint32_t flowid; /* packet's 4-tuple system 119 * flow identifier 120 */ 121 /* variables for hardware checksum */ 122 int csum_flags; /* flags regarding checksum */ 123 int csum_data; /* data field used by csum routines */ 124 u_int16_t tso_segsz; /* TSO segment size */ 125 union { 126 u_int16_t vt_vtag; /* Ethernet 802.1p+q vlan tag */ 127 u_int16_t vt_nrecs; /* # of IGMPv3 records in this chain */ 128 } PH_vt; 129 SLIST_HEAD(packet_tags, m_tag) tags; /* list of packet tags */ 130}; 131#define ether_vtag PH_vt.vt_vtag 132 133/* 134 * Description of external storage mapped into mbuf; valid only if M_EXT is 135 * set. 136 */ 137struct m_ext { 138 caddr_t ext_buf; /* start of buffer */ 139 void (*ext_free) /* free routine if not the usual */ 140 (void *, void *); 141 void *ext_arg1; /* optional argument pointer */ 142 void *ext_arg2; /* optional argument pointer */ 143 u_int ext_size; /* size of buffer, for ext_free */ 144 volatile u_int *ref_cnt; /* pointer to ref count info */ 145 int ext_type; /* type of external storage */ 146}; 147 148/* 149 * The core of the mbuf object along with some shortcut defines for practical 150 * purposes. 151 */ 152struct mbuf { 153 struct m_hdr m_hdr; 154 union { 155 struct { 156 struct pkthdr MH_pkthdr; /* M_PKTHDR set */ 157 union { 158 struct m_ext MH_ext; /* M_EXT set */ 159 char MH_databuf[MHLEN]; 160 } MH_dat; 161 } MH; 162 char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */ 163 } M_dat; 164}; 165#define m_next m_hdr.mh_next 166#define m_len m_hdr.mh_len 167#define m_data m_hdr.mh_data 168#define m_type m_hdr.mh_type 169#define m_flags m_hdr.mh_flags 170#define m_nextpkt m_hdr.mh_nextpkt 171#define m_act m_nextpkt 172#define m_pkthdr M_dat.MH.MH_pkthdr 173#define m_ext M_dat.MH.MH_dat.MH_ext 174#define m_pktdat M_dat.MH.MH_dat.MH_databuf 175#define m_dat M_dat.M_databuf 176 177/* 178 * mbuf flags. 179 */ 180#define M_EXT 0x00000001 /* has associated external storage */ 181#define M_PKTHDR 0x00000002 /* start of record */ 182#define M_EOR 0x00000004 /* end of record */ 183#define M_RDONLY 0x00000008 /* associated data is marked read-only */ 184#define M_PROTO1 0x00000010 /* protocol-specific */ 185#define M_PROTO2 0x00000020 /* protocol-specific */ 186#define M_PROTO3 0x00000040 /* protocol-specific */ 187#define M_PROTO4 0x00000080 /* protocol-specific */ 188#define M_PROTO5 0x00000100 /* protocol-specific */ 189#define M_BCAST 0x00000200 /* send/received as link-level broadcast */ 190#define M_MCAST 0x00000400 /* send/received as link-level multicast */ 191#define M_FRAG 0x00000800 /* packet is a fragment of a larger packet */ 192#define M_FIRSTFRAG 0x00001000 /* packet is first fragment */ 193#define M_LASTFRAG 0x00002000 /* packet is last fragment */ 194#define M_SKIP_FIREWALL 0x00004000 /* skip firewall processing */ 195#define M_FREELIST 0x00008000 /* mbuf is on the free list */ 196#define M_VLANTAG 0x00010000 /* ether_vtag is valid */ 197#define M_PROMISC 0x00020000 /* packet was not for us */ 198#define M_NOFREE 0x00040000 /* do not free mbuf, embedded in cluster */ 199#define M_PROTO6 0x00080000 /* protocol-specific */ 200#define M_PROTO7 0x00100000 /* protocol-specific */ 201#define M_PROTO8 0x00200000 /* protocol-specific */ 202#define M_FLOWID 0x00400000 /* deprecated: flowid is valid */ 203#define M_HASHTYPEBITS 0x0F000000 /* mask of bits holding flowid hash type */ 204 205/* 206 * For RELENG_{6,7} steal these flags for limited multiple routing table 207 * support. In RELENG_8 and beyond, use just one flag and a tag. 208 */ 209#define M_FIB 0xF0000000 /* steal some bits to store fib number. */ 210 211#define M_NOTIFICATION M_PROTO5 /* SCTP notification */ 212 213/* 214 * Flags to purge when crossing layers. 215 */ 216#define M_PROTOFLAGS \ 217 (M_PROTO1|M_PROTO2|M_PROTO3|M_PROTO4|M_PROTO5|M_PROTO6|M_PROTO7|M_PROTO8) 218 219/* 220 * Network interface cards are able to hash protocol fields (such as IPv4 221 * addresses and TCP port numbers) classify packets into flows. These flows 222 * can then be used to maintain ordering while delivering packets to the OS 223 * via parallel input queues, as well as to provide a stateless affinity 224 * model. NIC drivers can pass up the hash via m->m_pkthdr.flowid, and set 225 * m_flag fields to indicate how the hash should be interpreted by the 226 * network stack. 227 * 228 * Most NICs support RSS, which provides ordering and explicit affinity, and 229 * use the hash m_flag bits to indicate what header fields were covered by 230 * the hash. M_HASHTYPE_OPAQUE can be set by non-RSS cards or configurations 231 * that provide an opaque flow identifier, allowing for ordering and 232 * distribution without explicit affinity. 233 */ 234#define M_HASHTYPE_SHIFT 24 235#define M_HASHTYPE_NONE 0x0 236#define M_HASHTYPE_RSS_IPV4 0x1 /* IPv4 2-tuple */ 237#define M_HASHTYPE_RSS_TCP_IPV4 0x2 /* TCPv4 4-tuple */ 238#define M_HASHTYPE_RSS_IPV6 0x3 /* IPv6 2-tuple */ 239#define M_HASHTYPE_RSS_TCP_IPV6 0x4 /* TCPv6 4-tuple */ 240#define M_HASHTYPE_RSS_IPV6_EX 0x5 /* IPv6 2-tuple + ext hdrs */ 241#define M_HASHTYPE_RSS_TCP_IPV6_EX 0x6 /* TCPv6 4-tiple + ext hdrs */ 242#define M_HASHTYPE_OPAQUE 0xf /* ordering, not affinity */ 243 244#define M_HASHTYPE_CLEAR(m) (m)->m_flags &= ~(M_HASHTYPEBITS) 245#define M_HASHTYPE_GET(m) (((m)->m_flags & M_HASHTYPEBITS) >> \ 246 M_HASHTYPE_SHIFT) 247#define M_HASHTYPE_SET(m, v) do { \ 248 (m)->m_flags &= ~M_HASHTYPEBITS; \ 249 (m)->m_flags |= ((v) << M_HASHTYPE_SHIFT); \ 250} while (0) 251#define M_HASHTYPE_TEST(m, v) (M_HASHTYPE_GET(m) == (v)) 252 253/* 254 * Flags preserved when copying m_pkthdr. 255 */ 256#define M_COPYFLAGS \ 257 (M_PKTHDR|M_EOR|M_RDONLY|M_PROTOFLAGS|M_SKIP_FIREWALL|M_BCAST|M_MCAST|\ 258 M_FRAG|M_FIRSTFRAG|M_LASTFRAG|M_VLANTAG|M_PROMISC|M_FIB|M_HASHTYPEBITS) 259 260/* 261 * External buffer types: identify ext_buf type. 262 */ 263#define EXT_CLUSTER 1 /* mbuf cluster */ 264#define EXT_SFBUF 2 /* sendfile(2)'s sf_bufs */ 265#define EXT_JUMBOP 3 /* jumbo cluster 4096 bytes */ 266#define EXT_JUMBO9 4 /* jumbo cluster 9216 bytes */ 267#define EXT_JUMBO16 5 /* jumbo cluster 16184 bytes */ 268#define EXT_PACKET 6 /* mbuf+cluster from packet zone */ 269#define EXT_MBUF 7 /* external mbuf reference (M_IOVEC) */ 270#define EXT_NET_DRV 100 /* custom ext_buf provided by net driver(s) */ 271#define EXT_MOD_TYPE 200 /* custom module's ext_buf type */ 272#define EXT_DISPOSABLE 300 /* can throw this buffer away w/page flipping */ 273#define EXT_EXTREF 400 /* has externally maintained ref_cnt ptr */ 274 275/* 276 * Flags indicating hw checksum support and sw checksum requirements. This 277 * field can be directly tested against if_data.ifi_hwassist. 278 */ 279#define CSUM_IP 0x0001 /* will csum IP */ 280#define CSUM_TCP 0x0002 /* will csum TCP */ 281#define CSUM_UDP 0x0004 /* will csum UDP */ 282#define CSUM_IP_FRAGS 0x0008 /* removed, left for compat */ 283#define CSUM_FRAGMENT 0x0010 /* will do IP fragmentation */ 284#define CSUM_TSO 0x0020 /* will do TSO */ 285#define CSUM_SCTP 0x0040 /* will csum SCTP */ 286#define CSUM_SCTP_IPV6 0x0080 /* will csum IPv6/SCTP */ 287 288#define CSUM_IP_CHECKED 0x0100 /* did csum IP */ 289#define CSUM_IP_VALID 0x0200 /* ... the csum is valid */ 290#define CSUM_DATA_VALID 0x0400 /* csum_data field is valid */ 291#define CSUM_PSEUDO_HDR 0x0800 /* csum_data has pseudo hdr */ 292#define CSUM_SCTP_VALID 0x1000 /* SCTP checksum is valid */ 293#define CSUM_UDP_IPV6 0x2000 /* will csum IPv6/UDP */ 294#define CSUM_TCP_IPV6 0x4000 /* will csum IPv6/TCP */ 295/* CSUM_TSO_IPV6 0x8000 will do IPv6/TSO */ 296 297/* CSUM_FRAGMENT_IPV6 0x10000 will do IPv6 fragementation */ 298 299#define CSUM_DELAY_DATA_IPV6 (CSUM_TCP_IPV6 | CSUM_UDP_IPV6) 300#define CSUM_DATA_VALID_IPV6 CSUM_DATA_VALID 301 302#define CSUM_DELAY_DATA (CSUM_TCP | CSUM_UDP) 303#define CSUM_DELAY_IP (CSUM_IP) /* Only v4, no v6 IP hdr csum */ 304 305/* 306 * mbuf types. 307 */ 308#define MT_NOTMBUF 0 /* USED INTERNALLY ONLY! Object is not mbuf */ 309#define MT_DATA 1 /* dynamic (data) allocation */ 310#define MT_HEADER MT_DATA /* packet header, use M_PKTHDR instead */ 311#define MT_SONAME 8 /* socket name */ 312#define MT_CONTROL 14 /* extra-data protocol message */ 313#define MT_OOBDATA 15 /* expedited data */ 314#define MT_NTYPES 16 /* number of mbuf types for mbtypes[] */ 315 316#define MT_NOINIT 255 /* Not a type but a flag to allocate 317 a non-initialized mbuf */ 318 319#define MB_NOTAGS 0x1UL /* no tags attached to mbuf */ 320 321/* 322 * General mbuf allocator statistics structure. 323 * 324 * Many of these statistics are no longer used; we instead track many 325 * allocator statistics through UMA's built in statistics mechanism. 326 */ 327struct mbstat { 328 u_long m_mbufs; /* XXX */ 329 u_long m_mclusts; /* XXX */ 330 331 u_long m_drain; /* times drained protocols for space */ 332 u_long m_mcfail; /* XXX: times m_copym failed */ 333 u_long m_mpfail; /* XXX: times m_pullup failed */ 334 u_long m_msize; /* length of an mbuf */ 335 u_long m_mclbytes; /* length of an mbuf cluster */ 336 u_long m_minclsize; /* min length of data to allocate a cluster */ 337 u_long m_mlen; /* length of data in an mbuf */ 338 u_long m_mhlen; /* length of data in a header mbuf */ 339 340 /* Number of mbtypes (gives # elems in mbtypes[] array) */ 341 short m_numtypes; 342 343 /* XXX: Sendfile stats should eventually move to their own struct */ 344 u_long sf_iocnt; /* times sendfile had to do disk I/O */ 345 u_long sf_allocfail; /* times sfbuf allocation failed */ 346 u_long sf_allocwait; /* times sfbuf allocation had to wait */ 347}; 348 349/* 350 * Flags specifying how an allocation should be made. 351 * 352 * The flag to use is as follows: 353 * - M_NOWAIT (M_DONTWAIT) from an interrupt handler to not block allocation. 354 * - M_WAITOK (M_WAIT) from wherever it is safe to block. 355 * 356 * M_DONTWAIT/M_NOWAIT means that we will not block the thread explicitly and 357 * if we cannot allocate immediately we may return NULL, whereas 358 * M_WAIT/M_WAITOK means that if we cannot allocate resources we 359 * will block until they are available, and thus never return NULL. 360 * 361 * XXX Eventually just phase this out to use M_WAITOK/M_NOWAIT. 362 */ 363#define MBTOM(how) (how) 364#define M_DONTWAIT M_NOWAIT 365#define M_TRYWAIT M_WAITOK 366#define M_WAIT M_WAITOK 367 368/* 369 * String names of mbuf-related UMA(9) and malloc(9) types. Exposed to 370 * !_KERNEL so that monitoring tools can look up the zones with 371 * libmemstat(3). 372 */ 373#define MBUF_MEM_NAME "mbuf" 374#define MBUF_CLUSTER_MEM_NAME "mbuf_cluster" 375#define MBUF_PACKET_MEM_NAME "mbuf_packet" 376#define MBUF_JUMBOP_MEM_NAME "mbuf_jumbo_page" 377#define MBUF_JUMBO9_MEM_NAME "mbuf_jumbo_9k" 378#define MBUF_JUMBO16_MEM_NAME "mbuf_jumbo_16k" 379#define MBUF_TAG_MEM_NAME "mbuf_tag" 380#define MBUF_EXTREFCNT_MEM_NAME "mbuf_ext_refcnt" 381 382#ifdef _KERNEL 383 384#ifdef WITNESS 385#define MBUF_CHECKSLEEP(how) do { \ 386 if (how == M_WAITOK) \ 387 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, \ 388 "Sleeping in \"%s\"", __func__); \ 389} while (0) 390#else 391#define MBUF_CHECKSLEEP(how) 392#endif 393 394/* 395 * Network buffer allocation API 396 * 397 * The rest of it is defined in kern/kern_mbuf.c 398 */ 399extern uma_zone_t zone_mbuf; 400extern uma_zone_t zone_clust; 401extern uma_zone_t zone_pack; 402extern uma_zone_t zone_jumbop; 403extern uma_zone_t zone_jumbo9; 404extern uma_zone_t zone_jumbo16; 405extern uma_zone_t zone_ext_refcnt; 406 407static __inline struct mbuf *m_getcl(int how, short type, int flags); 408static __inline struct mbuf *m_get(int how, short type); 409static __inline struct mbuf *m_gethdr(int how, short type); 410static __inline struct mbuf *m_getjcl(int how, short type, int flags, 411 int size); 412static __inline struct mbuf *m_getclr(int how, short type); /* XXX */ 413static __inline int m_init(struct mbuf *m, uma_zone_t zone, 414 int size, int how, short type, int flags); 415static __inline struct mbuf *m_free(struct mbuf *m); 416static __inline void m_clget(struct mbuf *m, int how); 417static __inline void *m_cljget(struct mbuf *m, int how, int size); 418static __inline void m_chtype(struct mbuf *m, short new_type); 419void mb_free_ext(struct mbuf *); 420static __inline struct mbuf *m_last(struct mbuf *m); 421int m_pkthdr_init(struct mbuf *m, int how); 422 423static __inline int 424m_gettype(int size) 425{ 426 int type; 427 428 switch (size) { 429 case MSIZE: 430 type = EXT_MBUF; 431 break; 432 case MCLBYTES: 433 type = EXT_CLUSTER; 434 break; 435#if MJUMPAGESIZE != MCLBYTES 436 case MJUMPAGESIZE: 437 type = EXT_JUMBOP; 438 break; 439#endif 440 case MJUM9BYTES: 441 type = EXT_JUMBO9; 442 break; 443 case MJUM16BYTES: 444 type = EXT_JUMBO16; 445 break; 446 default: 447 panic("%s: m_getjcl: invalid cluster size", __func__); 448 } 449 450 return (type); 451} 452 453/* 454 * Associated an external reference counted buffer with an mbuf. 455 */ 456static __inline void 457m_extaddref(struct mbuf *m, caddr_t buf, u_int size, u_int *ref_cnt, 458 void (*freef)(void *, void *), void *arg1, void *arg2) 459{ 460 461 KASSERT(ref_cnt != NULL, ("%s: ref_cnt not provided", __func__)); 462 463 atomic_add_int(ref_cnt, 1); 464 m->m_flags |= M_EXT; 465 m->m_ext.ext_buf = buf; 466 m->m_ext.ref_cnt = ref_cnt; 467 m->m_data = m->m_ext.ext_buf; 468 m->m_ext.ext_size = size; 469 m->m_ext.ext_free = freef; 470 m->m_ext.ext_arg1 = arg1; 471 m->m_ext.ext_arg2 = arg2; 472 m->m_ext.ext_type = EXT_EXTREF; 473} 474 475static __inline uma_zone_t 476m_getzone(int size) 477{ 478 uma_zone_t zone; 479 480 switch (size) { 481 case MSIZE: 482 zone = zone_mbuf; 483 break; 484 case MCLBYTES: 485 zone = zone_clust; 486 break; 487#if MJUMPAGESIZE != MCLBYTES 488 case MJUMPAGESIZE: 489 zone = zone_jumbop; 490 break; 491#endif 492 case MJUM9BYTES: 493 zone = zone_jumbo9; 494 break; 495 case MJUM16BYTES: 496 zone = zone_jumbo16; 497 break; 498 default: 499 panic("%s: m_getjcl: invalid cluster type", __func__); 500 } 501 502 return (zone); 503} 504 505/* 506 * Initialize an mbuf with linear storage. 507 * 508 * Inline because the consumer text overhead will be roughly the same to 509 * initialize or call a function with this many parameters and M_PKTHDR 510 * should go away with constant propagation for !MGETHDR. 511 */ 512static __inline int 513m_init(struct mbuf *m, uma_zone_t zone, int size, int how, short type, 514 int flags) 515{ 516 int error; 517 518 m->m_next = NULL; 519 m->m_nextpkt = NULL; 520 m->m_data = m->m_dat; 521 m->m_len = 0; 522 m->m_flags = flags; 523 m->m_type = type; 524 if (flags & M_PKTHDR) { 525 if ((error = m_pkthdr_init(m, how)) != 0) 526 return (error); 527 } 528 529 return (0); 530} 531 532static __inline struct mbuf * 533m_get(int how, short type) 534{ 535 struct mb_args args; 536 537 args.flags = 0; 538 args.type = type; 539 return ((struct mbuf *)(uma_zalloc_arg(zone_mbuf, &args, how))); 540} 541 542/* 543 * XXX This should be deprecated, very little use. 544 */ 545static __inline struct mbuf * 546m_getclr(int how, short type) 547{ 548 struct mbuf *m; 549 struct mb_args args; 550 551 args.flags = 0; 552 args.type = type; 553 m = uma_zalloc_arg(zone_mbuf, &args, how); 554 if (m != NULL) 555 bzero(m->m_data, MLEN); 556 return (m); 557} 558 559static __inline struct mbuf * 560m_gethdr(int how, short type) 561{ 562 struct mb_args args; 563 564 args.flags = M_PKTHDR; 565 args.type = type; 566 return ((struct mbuf *)(uma_zalloc_arg(zone_mbuf, &args, how))); 567} 568 569static __inline struct mbuf * 570m_getcl(int how, short type, int flags) 571{ 572 struct mb_args args; 573 574 args.flags = flags; 575 args.type = type; 576 return ((struct mbuf *)(uma_zalloc_arg(zone_pack, &args, how))); 577} 578 579/* 580 * m_getjcl() returns an mbuf with a cluster of the specified size attached. 581 * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES. 582 * 583 * XXX: This is rather large, should be real function maybe. 584 */ 585static __inline struct mbuf * 586m_getjcl(int how, short type, int flags, int size) 587{ 588 struct mb_args args; 589 struct mbuf *m, *n; 590 uma_zone_t zone; 591 592 if (size == MCLBYTES) 593 return m_getcl(how, type, flags); 594 595 args.flags = flags; 596 args.type = type; 597 598 m = uma_zalloc_arg(zone_mbuf, &args, how); 599 if (m == NULL) 600 return (NULL); 601 602 zone = m_getzone(size); 603 n = uma_zalloc_arg(zone, m, how); 604 if (n == NULL) { 605 uma_zfree(zone_mbuf, m); 606 return (NULL); 607 } 608 return (m); 609} 610 611static __inline void 612m_free_fast(struct mbuf *m) 613{ 614#ifdef INVARIANTS 615 if (m->m_flags & M_PKTHDR) 616 KASSERT(SLIST_EMPTY(&m->m_pkthdr.tags), ("doing fast free of mbuf with tags")); 617#endif 618 619 uma_zfree_arg(zone_mbuf, m, (void *)MB_NOTAGS); 620} 621 622static __inline struct mbuf * 623m_free(struct mbuf *m) 624{ 625 struct mbuf *n = m->m_next; 626 627 if (m->m_flags & M_EXT) 628 mb_free_ext(m); 629 else if ((m->m_flags & M_NOFREE) == 0) 630 uma_zfree(zone_mbuf, m); 631 return (n); 632} 633 634static __inline void 635m_clget(struct mbuf *m, int how) 636{ 637 638 if (m->m_flags & M_EXT) 639 printf("%s: %p mbuf already has cluster\n", __func__, m); 640 m->m_ext.ext_buf = (char *)NULL; 641 uma_zalloc_arg(zone_clust, m, how); 642 /* 643 * On a cluster allocation failure, drain the packet zone and retry, 644 * we might be able to loosen a few clusters up on the drain. 645 */ 646 if ((how & M_NOWAIT) && (m->m_ext.ext_buf == NULL)) { 647 zone_drain(zone_pack); 648 uma_zalloc_arg(zone_clust, m, how); 649 } 650} 651 652/* 653 * m_cljget() is different from m_clget() as it can allocate clusters without 654 * attaching them to an mbuf. In that case the return value is the pointer 655 * to the cluster of the requested size. If an mbuf was specified, it gets 656 * the cluster attached to it and the return value can be safely ignored. 657 * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES. 658 */ 659static __inline void * 660m_cljget(struct mbuf *m, int how, int size) 661{ 662 uma_zone_t zone; 663 664 if (m && m->m_flags & M_EXT) 665 printf("%s: %p mbuf already has cluster\n", __func__, m); 666 if (m != NULL) 667 m->m_ext.ext_buf = NULL; 668 669 zone = m_getzone(size); 670 return (uma_zalloc_arg(zone, m, how)); 671} 672 673static __inline void 674m_cljset(struct mbuf *m, void *cl, int type) 675{ 676 uma_zone_t zone; 677 int size; 678 679 switch (type) { 680 case EXT_CLUSTER: 681 size = MCLBYTES; 682 zone = zone_clust; 683 break; 684#if MJUMPAGESIZE != MCLBYTES 685 case EXT_JUMBOP: 686 size = MJUMPAGESIZE; 687 zone = zone_jumbop; 688 break; 689#endif 690 case EXT_JUMBO9: 691 size = MJUM9BYTES; 692 zone = zone_jumbo9; 693 break; 694 case EXT_JUMBO16: 695 size = MJUM16BYTES; 696 zone = zone_jumbo16; 697 break; 698 default: 699 panic("unknown cluster type"); 700 break; 701 } 702 703 m->m_data = m->m_ext.ext_buf = cl; 704 m->m_ext.ext_free = m->m_ext.ext_arg1 = m->m_ext.ext_arg2 = NULL; 705 m->m_ext.ext_size = size; 706 m->m_ext.ext_type = type; 707 m->m_ext.ref_cnt = uma_find_refcnt(zone, cl); 708 m->m_flags |= M_EXT; 709 710} 711 712static __inline void 713m_chtype(struct mbuf *m, short new_type) 714{ 715 716 m->m_type = new_type; 717} 718 719static __inline struct mbuf * 720m_last(struct mbuf *m) 721{ 722 723 while (m->m_next) 724 m = m->m_next; 725 return (m); 726} 727 728extern void (*m_addr_chg_pf_p)(struct mbuf *m); 729 730static __inline void 731m_addr_changed(struct mbuf *m) 732{ 733 734 if (m_addr_chg_pf_p) 735 m_addr_chg_pf_p(m); 736} 737 738/* 739 * mbuf, cluster, and external object allocation macros (for compatibility 740 * purposes). 741 */ 742#define M_MOVE_PKTHDR(to, from) m_move_pkthdr((to), (from)) 743#define MGET(m, how, type) ((m) = m_get((how), (type))) 744#define MGETHDR(m, how, type) ((m) = m_gethdr((how), (type))) 745#define MCLGET(m, how) m_clget((m), (how)) 746#define MEXTADD(m, buf, size, free, arg1, arg2, flags, type) \ 747 m_extadd((m), (caddr_t)(buf), (size), (free),(arg1),(arg2),(flags), (type)) 748#define m_getm(m, len, how, type) \ 749 m_getm2((m), (len), (how), (type), M_PKTHDR) 750 751/* 752 * Evaluate TRUE if it's safe to write to the mbuf m's data region (this can 753 * be both the local data payload, or an external buffer area, depending on 754 * whether M_EXT is set). 755 */ 756#define M_WRITABLE(m) (!((m)->m_flags & M_RDONLY) && \ 757 (!(((m)->m_flags & M_EXT)) || \ 758 (*((m)->m_ext.ref_cnt) == 1)) ) \ 759 760/* Check if the supplied mbuf has a packet header, or else panic. */ 761#define M_ASSERTPKTHDR(m) \ 762 KASSERT((m) != NULL && (m)->m_flags & M_PKTHDR, \ 763 ("%s: no mbuf packet header!", __func__)) 764 765/* 766 * Ensure that the supplied mbuf is a valid, non-free mbuf. 767 * 768 * XXX: Broken at the moment. Need some UMA magic to make it work again. 769 */ 770#define M_ASSERTVALID(m) \ 771 KASSERT((((struct mbuf *)m)->m_flags & 0) == 0, \ 772 ("%s: attempted use of a free mbuf!", __func__)) 773 774/* 775 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place an 776 * object of the specified size at the end of the mbuf, longword aligned. 777 */ 778#define M_ALIGN(m, len) do { \ 779 KASSERT(!((m)->m_flags & (M_PKTHDR|M_EXT)), \ 780 ("%s: M_ALIGN not normal mbuf", __func__)); \ 781 KASSERT((m)->m_data == (m)->m_dat, \ 782 ("%s: M_ALIGN not a virgin mbuf", __func__)); \ 783 (m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1); \ 784} while (0) 785 786/* 787 * As above, for mbufs allocated with m_gethdr/MGETHDR or initialized by 788 * M_DUP/MOVE_PKTHDR. 789 */ 790#define MH_ALIGN(m, len) do { \ 791 KASSERT((m)->m_flags & M_PKTHDR && !((m)->m_flags & M_EXT), \ 792 ("%s: MH_ALIGN not PKTHDR mbuf", __func__)); \ 793 KASSERT((m)->m_data == (m)->m_pktdat, \ 794 ("%s: MH_ALIGN not a virgin mbuf", __func__)); \ 795 (m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1); \ 796} while (0) 797 798/* 799 * Compute the amount of space available before the current start of data in 800 * an mbuf. 801 * 802 * The M_WRITABLE() is a temporary, conservative safety measure: the burden 803 * of checking writability of the mbuf data area rests solely with the caller. 804 */ 805#define M_LEADINGSPACE(m) \ 806 ((m)->m_flags & M_EXT ? \ 807 (M_WRITABLE(m) ? (m)->m_data - (m)->m_ext.ext_buf : 0): \ 808 (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \ 809 (m)->m_data - (m)->m_dat) 810 811/* 812 * Compute the amount of space available after the end of data in an mbuf. 813 * 814 * The M_WRITABLE() is a temporary, conservative safety measure: the burden 815 * of checking writability of the mbuf data area rests solely with the caller. 816 */ 817#define M_TRAILINGSPACE(m) \ 818 ((m)->m_flags & M_EXT ? \ 819 (M_WRITABLE(m) ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size \ 820 - ((m)->m_data + (m)->m_len) : 0) : \ 821 &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len)) 822 823/* 824 * Arrange to prepend space of size plen to mbuf m. If a new mbuf must be 825 * allocated, how specifies whether to wait. If the allocation fails, the 826 * original mbuf chain is freed and m is set to NULL. 827 */ 828#define M_PREPEND(m, plen, how) do { \ 829 struct mbuf **_mmp = &(m); \ 830 struct mbuf *_mm = *_mmp; \ 831 int _mplen = (plen); \ 832 int __mhow = (how); \ 833 \ 834 MBUF_CHECKSLEEP(how); \ 835 if (M_LEADINGSPACE(_mm) >= _mplen) { \ 836 _mm->m_data -= _mplen; \ 837 _mm->m_len += _mplen; \ 838 } else \ 839 _mm = m_prepend(_mm, _mplen, __mhow); \ 840 if (_mm != NULL && _mm->m_flags & M_PKTHDR) \ 841 _mm->m_pkthdr.len += _mplen; \ 842 *_mmp = _mm; \ 843} while (0) 844 845/* 846 * Change mbuf to new type. This is a relatively expensive operation and 847 * should be avoided. 848 */ 849#define MCHTYPE(m, t) m_chtype((m), (t)) 850 851/* Length to m_copy to copy all. */ 852#define M_COPYALL 1000000000 853 854/* Compatibility with 4.3. */ 855#define m_copy(m, o, l) m_copym((m), (o), (l), M_DONTWAIT) 856 857extern int max_datalen; /* MHLEN - max_hdr */ 858extern int max_hdr; /* Largest link + protocol header */ 859extern int max_linkhdr; /* Largest link-level header */ 860extern int max_protohdr; /* Largest protocol header */ 861extern struct mbstat mbstat; /* General mbuf stats/infos */ 862extern int nmbclusters; /* Maximum number of clusters */ 863 864struct uio; 865 866void m_adj(struct mbuf *, int); 867void m_align(struct mbuf *, int); 868int m_apply(struct mbuf *, int, int, 869 int (*)(void *, void *, u_int), void *); 870int m_append(struct mbuf *, int, c_caddr_t); 871void m_cat(struct mbuf *, struct mbuf *); 872void m_extadd(struct mbuf *, caddr_t, u_int, 873 void (*)(void *, void *), void *, void *, int, int); 874struct mbuf *m_collapse(struct mbuf *, int, int); 875void m_copyback(struct mbuf *, int, int, c_caddr_t); 876void m_copydata(const struct mbuf *, int, int, caddr_t); 877struct mbuf *m_copym(struct mbuf *, int, int, int); 878struct mbuf *m_copymdata(struct mbuf *, struct mbuf *, 879 int, int, int, int); 880struct mbuf *m_copypacket(struct mbuf *, int); 881void m_copy_pkthdr(struct mbuf *, struct mbuf *); 882struct mbuf *m_copyup(struct mbuf *n, int len, int dstoff); 883struct mbuf *m_defrag(struct mbuf *, int); 884void m_demote(struct mbuf *, int); 885struct mbuf *m_devget(char *, int, int, struct ifnet *, 886 void (*)(char *, caddr_t, u_int)); 887struct mbuf *m_dup(struct mbuf *, int); 888int m_dup_pkthdr(struct mbuf *, struct mbuf *, int); 889u_int m_fixhdr(struct mbuf *); 890struct mbuf *m_fragment(struct mbuf *, int, int); 891void m_freem(struct mbuf *); 892struct mbuf *m_getm2(struct mbuf *, int, int, short, int); 893struct mbuf *m_getptr(struct mbuf *, int, int *); 894u_int m_length(struct mbuf *, struct mbuf **); 895int m_mbuftouio(struct uio *, struct mbuf *, int); 896void m_move_pkthdr(struct mbuf *, struct mbuf *); 897struct mbuf *m_prepend(struct mbuf *, int, int); 898void m_print(const struct mbuf *, int); 899struct mbuf *m_pulldown(struct mbuf *, int, int, int *); 900struct mbuf *m_pullup(struct mbuf *, int); 901int m_sanity(struct mbuf *, int); 902struct mbuf *m_split(struct mbuf *, int, int); 903struct mbuf *m_uiotombuf(struct uio *, int, int, int, int); 904struct mbuf *m_unshare(struct mbuf *, int how); 905 906/*- 907 * Network packets may have annotations attached by affixing a list of 908 * "packet tags" to the pkthdr structure. Packet tags are dynamically 909 * allocated semi-opaque data structures that have a fixed header 910 * (struct m_tag) that specifies the size of the memory block and a 911 * <cookie,type> pair that identifies it. The cookie is a 32-bit unique 912 * unsigned value used to identify a module or ABI. By convention this value 913 * is chosen as the date+time that the module is created, expressed as the 914 * number of seconds since the epoch (e.g., using date -u +'%s'). The type 915 * value is an ABI/module-specific value that identifies a particular 916 * annotation and is private to the module. For compatibility with systems 917 * like OpenBSD that define packet tags w/o an ABI/module cookie, the value 918 * PACKET_ABI_COMPAT is used to implement m_tag_get and m_tag_find 919 * compatibility shim functions and several tag types are defined below. 920 * Users that do not require compatibility should use a private cookie value 921 * so that packet tag-related definitions can be maintained privately. 922 * 923 * Note that the packet tag returned by m_tag_alloc has the default memory 924 * alignment implemented by malloc. To reference private data one can use a 925 * construct like: 926 * 927 * struct m_tag *mtag = m_tag_alloc(...); 928 * struct foo *p = (struct foo *)(mtag+1); 929 * 930 * if the alignment of struct m_tag is sufficient for referencing members of 931 * struct foo. Otherwise it is necessary to embed struct m_tag within the 932 * private data structure to insure proper alignment; e.g., 933 * 934 * struct foo { 935 * struct m_tag tag; 936 * ... 937 * }; 938 * struct foo *p = (struct foo *) m_tag_alloc(...); 939 * struct m_tag *mtag = &p->tag; 940 */ 941 942/* 943 * Persistent tags stay with an mbuf until the mbuf is reclaimed. Otherwise 944 * tags are expected to ``vanish'' when they pass through a network 945 * interface. For most interfaces this happens normally as the tags are 946 * reclaimed when the mbuf is free'd. However in some special cases 947 * reclaiming must be done manually. An example is packets that pass through 948 * the loopback interface. Also, one must be careful to do this when 949 * ``turning around'' packets (e.g., icmp_reflect). 950 * 951 * To mark a tag persistent bit-or this flag in when defining the tag id. 952 * The tag will then be treated as described above. 953 */ 954#define MTAG_PERSISTENT 0x800 955 956#define PACKET_TAG_NONE 0 /* Nadda */ 957 958/* Packet tags for use with PACKET_ABI_COMPAT. */ 959#define PACKET_TAG_IPSEC_IN_DONE 1 /* IPsec applied, in */ 960#define PACKET_TAG_IPSEC_OUT_DONE 2 /* IPsec applied, out */ 961#define PACKET_TAG_IPSEC_IN_CRYPTO_DONE 3 /* NIC IPsec crypto done */ 962#define PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED 4 /* NIC IPsec crypto req'ed */ 963#define PACKET_TAG_IPSEC_IN_COULD_DO_CRYPTO 5 /* NIC notifies IPsec */ 964#define PACKET_TAG_IPSEC_PENDING_TDB 6 /* Reminder to do IPsec */ 965#define PACKET_TAG_BRIDGE 7 /* Bridge processing done */ 966#define PACKET_TAG_GIF 8 /* GIF processing done */ 967#define PACKET_TAG_GRE 9 /* GRE processing done */ 968#define PACKET_TAG_IN_PACKET_CHECKSUM 10 /* NIC checksumming done */ 969#define PACKET_TAG_ENCAP 11 /* Encap. processing */ 970#define PACKET_TAG_IPSEC_SOCKET 12 /* IPSEC socket ref */ 971#define PACKET_TAG_IPSEC_HISTORY 13 /* IPSEC history */ 972#define PACKET_TAG_IPV6_INPUT 14 /* IPV6 input processing */ 973#define PACKET_TAG_DUMMYNET 15 /* dummynet info */ 974#define PACKET_TAG_DIVERT 17 /* divert info */ 975#define PACKET_TAG_IPFORWARD 18 /* ipforward info */ 976#define PACKET_TAG_MACLABEL (19 | MTAG_PERSISTENT) /* MAC label */ 977#define PACKET_TAG_PF 21 /* PF + ALTQ information */ 978#define PACKET_TAG_RTSOCKFAM 25 /* rtsock sa family */ 979#define PACKET_TAG_IPOPTIONS 27 /* Saved IP options */ 980#define PACKET_TAG_CARP 28 /* CARP info */ 981#define PACKET_TAG_IPSEC_NAT_T_PORTS 29 /* two uint16_t */ 982#define PACKET_TAG_ND_OUTGOING 30 /* ND outgoing */ 983 984/* Specific cookies and tags. */ 985 986/* Packet tag routines. */ 987struct m_tag *m_tag_alloc(u_int32_t, int, int, int); 988void m_tag_delete(struct mbuf *, struct m_tag *); 989void m_tag_delete_chain(struct mbuf *, struct m_tag *); 990void m_tag_free_default(struct m_tag *); 991struct m_tag *m_tag_locate(struct mbuf *, u_int32_t, int, struct m_tag *); 992struct m_tag *m_tag_copy(struct m_tag *, int); 993int m_tag_copy_chain(struct mbuf *, struct mbuf *, int); 994void m_tag_delete_nonpersistent(struct mbuf *); 995 996/* 997 * Initialize the list of tags associated with an mbuf. 998 */ 999static __inline void 1000m_tag_init(struct mbuf *m) 1001{ 1002 1003 SLIST_INIT(&m->m_pkthdr.tags); 1004} 1005 1006/* 1007 * Set up the contents of a tag. Note that this does not fill in the free 1008 * method; the caller is expected to do that. 1009 * 1010 * XXX probably should be called m_tag_init, but that was already taken. 1011 */ 1012static __inline void 1013m_tag_setup(struct m_tag *t, u_int32_t cookie, int type, int len) 1014{ 1015 1016 t->m_tag_id = type; 1017 t->m_tag_len = len; 1018 t->m_tag_cookie = cookie; 1019} 1020 1021/* 1022 * Reclaim resources associated with a tag. 1023 */ 1024static __inline void 1025m_tag_free(struct m_tag *t) 1026{ 1027 1028 (*t->m_tag_free)(t); 1029} 1030 1031/* 1032 * Return the first tag associated with an mbuf. 1033 */ 1034static __inline struct m_tag * 1035m_tag_first(struct mbuf *m) 1036{ 1037 1038 return (SLIST_FIRST(&m->m_pkthdr.tags)); 1039} 1040 1041/* 1042 * Return the next tag in the list of tags associated with an mbuf. 1043 */ 1044static __inline struct m_tag * 1045m_tag_next(struct mbuf *m, struct m_tag *t) 1046{ 1047 1048 return (SLIST_NEXT(t, m_tag_link)); 1049} 1050 1051/* 1052 * Prepend a tag to the list of tags associated with an mbuf. 1053 */ 1054static __inline void 1055m_tag_prepend(struct mbuf *m, struct m_tag *t) 1056{ 1057 1058 SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link); 1059} 1060 1061/* 1062 * Unlink a tag from the list of tags associated with an mbuf. 1063 */ 1064static __inline void 1065m_tag_unlink(struct mbuf *m, struct m_tag *t) 1066{ 1067 1068 SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link); 1069} 1070 1071/* These are for OpenBSD compatibility. */ 1072#define MTAG_ABI_COMPAT 0 /* compatibility ABI */ 1073 1074static __inline struct m_tag * 1075m_tag_get(int type, int length, int wait) 1076{ 1077 return (m_tag_alloc(MTAG_ABI_COMPAT, type, length, wait)); 1078} 1079 1080static __inline struct m_tag * 1081m_tag_find(struct mbuf *m, int type, struct m_tag *start) 1082{ 1083 return (SLIST_EMPTY(&m->m_pkthdr.tags) ? (struct m_tag *)NULL : 1084 m_tag_locate(m, MTAG_ABI_COMPAT, type, start)); 1085} 1086 1087/* XXX temporary FIB methods probably eventually use tags.*/ 1088#define M_FIBSHIFT 28 1089#define M_FIBMASK 0x0F 1090 1091/* get the fib from an mbuf and if it is not set, return the default */ 1092#define M_GETFIB(_m) \ 1093 ((((_m)->m_flags & M_FIB) >> M_FIBSHIFT) & M_FIBMASK) 1094 1095#define M_SETFIB(_m, _fib) do { \ 1096 _m->m_flags &= ~M_FIB; \ 1097 _m->m_flags |= (((_fib) << M_FIBSHIFT) & M_FIB); \ 1098} while (0) 1099 1100#endif /* _KERNEL */ 1101 1102#ifdef MBUF_PROFILING 1103 void m_profile(struct mbuf *m); 1104 #define M_PROFILE(m) m_profile(m) 1105#else 1106 #define M_PROFILE(m) 1107#endif 1108 1109 1110#endif /* !_SYS_MBUF_H_ */ 1111