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