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 */ 74 struct 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 */ 89 struct 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 */ 102 struct 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 */ 113 struct 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 */ 137 struct 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 */ 152 struct 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 /* will csum IP fragments */ 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 287 #define CSUM_IP_CHECKED 0x0100 /* did csum IP */ 288 #define CSUM_IP_VALID 0x0200 /* ... the csum is valid */ 289 #define CSUM_DATA_VALID 0x0400 /* csum_data field is valid */ 290 #define CSUM_PSEUDO_HDR 0x0800 /* csum_data has pseudo hdr */ 291 #define CSUM_SCTP_VALID 0x1000 /* SCTP checksum is valid */ 292 293 #define CSUM_DELAY_DATA (CSUM_TCP | CSUM_UDP) 294 #define CSUM_DELAY_IP (CSUM_IP) /* XXX add ipv6 here too? */ 295 296 /* 297 * mbuf types. 298 */ 299 #define MT_NOTMBUF 0 /* USED INTERNALLY ONLY! Object is not mbuf */ 300 #define MT_DATA 1 /* dynamic (data) allocation */ 301 #define MT_HEADER MT_DATA /* packet header, use M_PKTHDR instead */ 302 #define MT_SONAME 8 /* socket name */ 303 #define MT_CONTROL 14 /* extra-data protocol message */ 304 #define MT_OOBDATA 15 /* expedited data */ 305 #define MT_NTYPES 16 /* number of mbuf types for mbtypes[] */ 306 307 #define MT_NOINIT 255 /* Not a type but a flag to allocate 308 a non-initialized mbuf */ 309 310 #define MB_NOTAGS 0x1UL /* no tags attached to mbuf */ 311 312 /* 313 * General mbuf allocator statistics structure. 314 * 315 * Many of these statistics are no longer used; we instead track many 316 * allocator statistics through UMA's built in statistics mechanism. 317 */ 318 struct mbstat { 319 u_long m_mbufs; /* XXX */ 320 u_long m_mclusts; /* XXX */ 321 322 u_long m_drain; /* times drained protocols for space */ 323 u_long m_mcfail; /* XXX: times m_copym failed */ 324 u_long m_mpfail; /* XXX: times m_pullup failed */ 325 u_long m_msize; /* length of an mbuf */ 326 u_long m_mclbytes; /* length of an mbuf cluster */ 327 u_long m_minclsize; /* min length of data to allocate a cluster */ 328 u_long m_mlen; /* length of data in an mbuf */ 329 u_long m_mhlen; /* length of data in a header mbuf */ 330 331 /* Number of mbtypes (gives # elems in mbtypes[] array) */ 332 short m_numtypes; 333 334 /* XXX: Sendfile stats should eventually move to their own struct */ 335 u_long sf_iocnt; /* times sendfile had to do disk I/O */ 336 u_long sf_allocfail; /* times sfbuf allocation failed */ 337 u_long sf_allocwait; /* times sfbuf allocation had to wait */ 338 }; 339 340 /* 341 * Flags specifying how an allocation should be made. 342 * 343 * The flag to use is as follows: 344 * - M_DONTWAIT or M_NOWAIT from an interrupt handler to not block allocation. 345 * - M_WAIT or M_WAITOK from wherever it is safe to block. 346 * 347 * M_DONTWAIT/M_NOWAIT means that we will not block the thread explicitly and 348 * if we cannot allocate immediately we may return NULL, whereas 349 * M_WAIT/M_WAITOK means that if we cannot allocate resources we 350 * will block until they are available, and thus never return NULL. 351 * 352 * XXX Eventually just phase this out to use M_WAITOK/M_NOWAIT. 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 391 extern uma_zone_t zone_mbuf; 392 extern uma_zone_t zone_clust; 393 extern uma_zone_t zone_pack; 394 extern uma_zone_t zone_jumbop; 395 extern uma_zone_t zone_jumbo9; 396 extern uma_zone_t zone_jumbo16; 397 extern uma_zone_t zone_ext_refcnt; 398 399 static __inline struct mbuf *m_getcl(int how, short type, int flags); 400 static __inline struct mbuf *m_get(int how, short type); 401 static __inline struct mbuf *m_get2(int how, short type, int flags, 402 int size); 403 static __inline struct mbuf *m_gethdr(int how, short type); 404 static __inline struct mbuf *m_getjcl(int how, short type, int flags, 405 int size); 406 static __inline struct mbuf *m_getclr(int how, short type); /* XXX */ 407 static __inline int m_init(struct mbuf *m, uma_zone_t zone, 408 int size, int how, short type, int flags); 409 static __inline struct mbuf *m_free(struct mbuf *m); 410 static __inline void m_clget(struct mbuf *m, int how); 411 static __inline void *m_cljget(struct mbuf *m, int how, int size); 412 static __inline void m_chtype(struct mbuf *m, short new_type); 413 void mb_free_ext(struct mbuf *); 414 static __inline struct mbuf *m_last(struct mbuf *m); 415 int m_pkthdr_init(struct mbuf *m, int how); 416 417 static __inline int 418 m_gettype(int size) 419 { 420 int type; 421 422 switch (size) { 423 case MSIZE: 424 type = EXT_MBUF; 425 break; 426 case MCLBYTES: 427 type = EXT_CLUSTER; 428 break; 429 #if MJUMPAGESIZE != MCLBYTES 430 case MJUMPAGESIZE: 431 type = EXT_JUMBOP; 432 break; 433 #endif 434 case MJUM9BYTES: 435 type = EXT_JUMBO9; 436 break; 437 case MJUM16BYTES: 438 type = EXT_JUMBO16; 439 break; 440 default: 441 panic("%s: invalid cluster size", __func__); 442 } 443 444 return (type); 445 } 446 447 static __inline uma_zone_t 448 m_getzone(int size) 449 { 450 uma_zone_t zone; 451 452 switch (size) { 453 case MCLBYTES: 454 zone = zone_clust; 455 break; 456 #if MJUMPAGESIZE != MCLBYTES 457 case MJUMPAGESIZE: 458 zone = zone_jumbop; 459 break; 460 #endif 461 case MJUM9BYTES: 462 zone = zone_jumbo9; 463 break; 464 case MJUM16BYTES: 465 zone = zone_jumbo16; 466 break; 467 default: 468 panic("%s: invalid cluster size", __func__); 469 } 470 471 return (zone); 472 } 473 474 /* 475 * Initialize an mbuf with linear storage. 476 * 477 * Inline because the consumer text overhead will be roughly the same to 478 * initialize or call a function with this many parameters and M_PKTHDR 479 * should go away with constant propagation for !MGETHDR. 480 */ 481 static __inline int 482 m_init(struct mbuf *m, uma_zone_t zone, int size, int how, short type, 483 int flags) 484 { 485 int error; 486 487 m->m_next = NULL; 488 m->m_nextpkt = NULL; 489 m->m_data = m->m_dat; 490 m->m_len = 0; 491 m->m_flags = flags; 492 m->m_type = type; 493 if (flags & M_PKTHDR) { 494 if ((error = m_pkthdr_init(m, how)) != 0) 495 return (error); 496 } 497 498 return (0); 499 } 500 501 static __inline struct mbuf * 502 m_get(int how, short type) 503 { 504 struct mb_args args; 505 506 args.flags = 0; 507 args.type = type; 508 return ((struct mbuf *)(uma_zalloc_arg(zone_mbuf, &args, how))); 509 } 510 511 /* 512 * XXX This should be deprecated, very little use. 513 */ 514 static __inline struct mbuf * 515 m_getclr(int how, short type) 516 { 517 struct mbuf *m; 518 struct mb_args args; 519 520 args.flags = 0; 521 args.type = type; 522 m = uma_zalloc_arg(zone_mbuf, &args, how); 523 if (m != NULL) 524 bzero(m->m_data, MLEN); 525 return (m); 526 } 527 528 static __inline struct mbuf * 529 m_gethdr(int how, short type) 530 { 531 struct mb_args args; 532 533 args.flags = M_PKTHDR; 534 args.type = type; 535 return ((struct mbuf *)(uma_zalloc_arg(zone_mbuf, &args, how))); 536 } 537 538 static __inline struct mbuf * 539 m_getcl(int how, short type, int flags) 540 { 541 struct mb_args args; 542 543 args.flags = flags; 544 args.type = type; 545 return ((struct mbuf *)(uma_zalloc_arg(zone_pack, &args, how))); 546 } 547 548 /* 549 * m_get2() allocates minimum mbuf that would fit "size" argument. 550 * 551 * XXX: This is rather large, should be real function maybe. 552 */ 553 static __inline struct mbuf * 554 m_get2(int how, short type, int flags, int size) 555 { 556 struct mb_args args; 557 struct mbuf *m, *n; 558 uma_zone_t zone; 559 560 args.flags = flags; 561 args.type = type; 562 563 if (size <= MHLEN || (size <= MLEN && (flags & M_PKTHDR) == 0)) 564 return ((struct mbuf *)(uma_zalloc_arg(zone_mbuf, &args, how))); 565 if (size <= MCLBYTES) 566 return ((struct mbuf *)(uma_zalloc_arg(zone_pack, &args, how))); 567 568 if (size > MJUM16BYTES) 569 return (NULL); 570 571 m = uma_zalloc_arg(zone_mbuf, &args, how); 572 if (m == NULL) 573 return (NULL); 574 575 #if MJUMPAGESIZE != MCLBYTES 576 if (size <= MJUMPAGESIZE) 577 zone = zone_jumbop; 578 else 579 #endif 580 if (size <= MJUM9BYTES) 581 zone = zone_jumbo9; 582 else 583 zone = zone_jumbo16; 584 585 n = uma_zalloc_arg(zone, m, how); 586 if (n == NULL) { 587 uma_zfree(zone_mbuf, m); 588 return (NULL); 589 } 590 591 return (m); 592 } 593 594 /* 595 * m_getjcl() returns an mbuf with a cluster of the specified size attached. 596 * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES. 597 * 598 * XXX: This is rather large, should be real function maybe. 599 */ 600 static __inline struct mbuf * 601 m_getjcl(int how, short type, int flags, int size) 602 { 603 struct mb_args args; 604 struct mbuf *m, *n; 605 uma_zone_t zone; 606 607 if (size == MCLBYTES) 608 return m_getcl(how, type, flags); 609 610 args.flags = flags; 611 args.type = type; 612 613 m = uma_zalloc_arg(zone_mbuf, &args, how); 614 if (m == NULL) 615 return (NULL); 616 617 zone = m_getzone(size); 618 n = uma_zalloc_arg(zone, m, how); 619 if (n == NULL) { 620 uma_zfree(zone_mbuf, m); 621 return (NULL); 622 } 623 return (m); 624 } 625 626 static __inline void 627 m_free_fast(struct mbuf *m) 628 { 629 #ifdef INVARIANTS 630 if (m->m_flags & M_PKTHDR) 631 KASSERT(SLIST_EMPTY(&m->m_pkthdr.tags), ("doing fast free of mbuf with tags")); 632 #endif 633 634 uma_zfree_arg(zone_mbuf, m, (void *)MB_NOTAGS); 635 } 636 637 static __inline struct mbuf * 638 m_free(struct mbuf *m) 639 { 640 struct mbuf *n = m->m_next; 641 642 if (m->m_flags & M_EXT) 643 mb_free_ext(m); 644 else if ((m->m_flags & M_NOFREE) == 0) 645 uma_zfree(zone_mbuf, m); 646 return (n); 647 } 648 649 static __inline void 650 m_clget(struct mbuf *m, int how) 651 { 652 653 if (m->m_flags & M_EXT) 654 printf("%s: %p mbuf already has cluster\n", __func__, m); 655 m->m_ext.ext_buf = (char *)NULL; 656 uma_zalloc_arg(zone_clust, m, how); 657 /* 658 * On a cluster allocation failure, drain the packet zone and retry, 659 * we might be able to loosen a few clusters up on the drain. 660 */ 661 if ((how & M_NOWAIT) && (m->m_ext.ext_buf == NULL)) { 662 zone_drain(zone_pack); 663 uma_zalloc_arg(zone_clust, m, how); 664 } 665 } 666 667 /* 668 * m_cljget() is different from m_clget() as it can allocate clusters without 669 * attaching them to an mbuf. In that case the return value is the pointer 670 * to the cluster of the requested size. If an mbuf was specified, it gets 671 * the cluster attached to it and the return value can be safely ignored. 672 * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES. 673 */ 674 static __inline void * 675 m_cljget(struct mbuf *m, int how, int size) 676 { 677 uma_zone_t zone; 678 679 if (m && m->m_flags & M_EXT) 680 printf("%s: %p mbuf already has cluster\n", __func__, m); 681 if (m != NULL) 682 m->m_ext.ext_buf = NULL; 683 684 zone = m_getzone(size); 685 return (uma_zalloc_arg(zone, m, how)); 686 } 687 688 static __inline void 689 m_cljset(struct mbuf *m, void *cl, int type) 690 { 691 uma_zone_t zone; 692 int size; 693 694 switch (type) { 695 case EXT_CLUSTER: 696 size = MCLBYTES; 697 zone = zone_clust; 698 break; 699 #if MJUMPAGESIZE != MCLBYTES 700 case EXT_JUMBOP: 701 size = MJUMPAGESIZE; 702 zone = zone_jumbop; 703 break; 704 #endif 705 case EXT_JUMBO9: 706 size = MJUM9BYTES; 707 zone = zone_jumbo9; 708 break; 709 case EXT_JUMBO16: 710 size = MJUM16BYTES; 711 zone = zone_jumbo16; 712 break; 713 default: 714 panic("%s: unknown cluster type", __func__); 715 break; 716 } 717 718 m->m_data = m->m_ext.ext_buf = cl; 719 m->m_ext.ext_free = m->m_ext.ext_arg1 = m->m_ext.ext_arg2 = NULL; 720 m->m_ext.ext_size = size; 721 m->m_ext.ext_type = type; 722 m->m_ext.ref_cnt = uma_find_refcnt(zone, cl); 723 m->m_flags |= M_EXT; 724 725 } 726 727 static __inline void 728 m_chtype(struct mbuf *m, short new_type) 729 { 730 731 m->m_type = new_type; 732 } 733 734 static __inline struct mbuf * 735 m_last(struct mbuf *m) 736 { 737 738 while (m->m_next) 739 m = m->m_next; 740 return (m); 741 } 742 743 extern void (*m_addr_chg_pf_p)(struct mbuf *m); 744 745 static __inline void 746 m_addr_changed(struct mbuf *m) 747 { 748 749 if (m_addr_chg_pf_p) 750 m_addr_chg_pf_p(m); 751 } 752 753 /* 754 * mbuf, cluster, and external object allocation macros (for compatibility 755 * purposes). 756 */ 757 #define M_MOVE_PKTHDR(to, from) m_move_pkthdr((to), (from)) 758 #define MGET(m, how, type) ((m) = m_get((how), (type))) 759 #define MGETHDR(m, how, type) ((m) = m_gethdr((how), (type))) 760 #define MCLGET(m, how) m_clget((m), (how)) 761 #define MEXTADD(m, buf, size, free, arg1, arg2, flags, type) \ 762 m_extadd((m), (caddr_t)(buf), (size), (free),(arg1),(arg2),(flags), (type)) 763 #define m_getm(m, len, how, type) \ 764 m_getm2((m), (len), (how), (type), M_PKTHDR) 765 766 /* 767 * Evaluate TRUE if it's safe to write to the mbuf m's data region (this can 768 * be both the local data payload, or an external buffer area, depending on 769 * whether M_EXT is set). 770 */ 771 #define M_WRITABLE(m) (!((m)->m_flags & M_RDONLY) && \ 772 (!(((m)->m_flags & M_EXT)) || \ 773 (*((m)->m_ext.ref_cnt) == 1)) ) \ 774 775 /* Check if the supplied mbuf has a packet header, or else panic. */ 776 #define M_ASSERTPKTHDR(m) \ 777 KASSERT((m) != NULL && (m)->m_flags & M_PKTHDR, \ 778 ("%s: no mbuf packet header!", __func__)) 779 780 /* 781 * Ensure that the supplied mbuf is a valid, non-free mbuf. 782 * 783 * XXX: Broken at the moment. Need some UMA magic to make it work again. 784 */ 785 #define M_ASSERTVALID(m) \ 786 KASSERT((((struct mbuf *)m)->m_flags & 0) == 0, \ 787 ("%s: attempted use of a free mbuf!", __func__)) 788 789 /* 790 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place an 791 * object of the specified size at the end of the mbuf, longword aligned. 792 */ 793 #define M_ALIGN(m, len) do { \ 794 KASSERT(!((m)->m_flags & (M_PKTHDR|M_EXT)), \ 795 ("%s: M_ALIGN not normal mbuf", __func__)); \ 796 KASSERT((m)->m_data == (m)->m_dat, \ 797 ("%s: M_ALIGN not a virgin mbuf", __func__)); \ 798 (m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1); \ 799 } while (0) 800 801 /* 802 * As above, for mbufs allocated with m_gethdr/MGETHDR or initialized by 803 * M_DUP/MOVE_PKTHDR. 804 */ 805 #define MH_ALIGN(m, len) do { \ 806 KASSERT((m)->m_flags & M_PKTHDR && !((m)->m_flags & M_EXT), \ 807 ("%s: MH_ALIGN not PKTHDR mbuf", __func__)); \ 808 KASSERT((m)->m_data == (m)->m_pktdat, \ 809 ("%s: MH_ALIGN not a virgin mbuf", __func__)); \ 810 (m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1); \ 811 } while (0) 812 813 /* 814 * Compute the amount of space available before the current start of data in 815 * an mbuf. 816 * 817 * The M_WRITABLE() is a temporary, conservative safety measure: the burden 818 * of checking writability of the mbuf data area rests solely with the caller. 819 */ 820 #define M_LEADINGSPACE(m) \ 821 ((m)->m_flags & M_EXT ? \ 822 (M_WRITABLE(m) ? (m)->m_data - (m)->m_ext.ext_buf : 0): \ 823 (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \ 824 (m)->m_data - (m)->m_dat) 825 826 /* 827 * Compute the amount of space available after the end of data in an mbuf. 828 * 829 * The M_WRITABLE() is a temporary, conservative safety measure: the burden 830 * of checking writability of the mbuf data area rests solely with the caller. 831 */ 832 #define M_TRAILINGSPACE(m) \ 833 ((m)->m_flags & M_EXT ? \ 834 (M_WRITABLE(m) ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size \ 835 - ((m)->m_data + (m)->m_len) : 0) : \ 836 &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len)) 837 838 /* 839 * Arrange to prepend space of size plen to mbuf m. If a new mbuf must be 840 * allocated, how specifies whether to wait. If the allocation fails, the 841 * original mbuf chain is freed and m is set to NULL. 842 */ 843 #define M_PREPEND(m, plen, how) do { \ 844 struct mbuf **_mmp = &(m); \ 845 struct mbuf *_mm = *_mmp; \ 846 int _mplen = (plen); \ 847 int __mhow = (how); \ 848 \ 849 MBUF_CHECKSLEEP(how); \ 850 if (M_LEADINGSPACE(_mm) >= _mplen) { \ 851 _mm->m_data -= _mplen; \ 852 _mm->m_len += _mplen; \ 853 } else \ 854 _mm = m_prepend(_mm, _mplen, __mhow); \ 855 if (_mm != NULL && _mm->m_flags & M_PKTHDR) \ 856 _mm->m_pkthdr.len += _mplen; \ 857 *_mmp = _mm; \ 858 } while (0) 859 860 /* 861 * Change mbuf to new type. This is a relatively expensive operation and 862 * should be avoided. 863 */ 864 #define MCHTYPE(m, t) m_chtype((m), (t)) 865 866 /* Length to m_copy to copy all. */ 867 #define M_COPYALL 1000000000 868 869 /* Compatibility with 4.3. */ 870 #define m_copy(m, o, l) m_copym((m), (o), (l), M_DONTWAIT) 871 872 extern int max_datalen; /* MHLEN - max_hdr */ 873 extern int max_hdr; /* Largest link + protocol header */ 874 extern int max_linkhdr; /* Largest link-level header */ 875 extern int max_protohdr; /* Largest protocol header */ 876 extern struct mbstat mbstat; /* General mbuf stats/infos */ 877 extern int nmbclusters; /* Maximum number of clusters */ 878 879 struct uio; 880 881 void m_adj(struct mbuf *, int); 882 void m_align(struct mbuf *, int); 883 int m_apply(struct mbuf *, int, int, 884 int (*)(void *, void *, u_int), void *); 885 int m_append(struct mbuf *, int, c_caddr_t); 886 void m_cat(struct mbuf *, struct mbuf *); 887 void m_extadd(struct mbuf *, caddr_t, u_int, 888 void (*)(void *, void *), void *, void *, int, int); 889 struct mbuf *m_collapse(struct mbuf *, int, int); 890 void m_copyback(struct mbuf *, int, int, c_caddr_t); 891 void m_copydata(const struct mbuf *, int, int, caddr_t); 892 struct mbuf *m_copym(struct mbuf *, int, int, int); 893 struct mbuf *m_copymdata(struct mbuf *, struct mbuf *, 894 int, int, int, int); 895 struct mbuf *m_copypacket(struct mbuf *, int); 896 void m_copy_pkthdr(struct mbuf *, struct mbuf *); 897 struct mbuf *m_copyup(struct mbuf *n, int len, int dstoff); 898 struct mbuf *m_defrag(struct mbuf *, int); 899 void m_demote(struct mbuf *, int); 900 struct mbuf *m_devget(char *, int, int, struct ifnet *, 901 void (*)(char *, caddr_t, u_int)); 902 struct mbuf *m_dup(struct mbuf *, int); 903 int m_dup_pkthdr(struct mbuf *, struct mbuf *, int); 904 u_int m_fixhdr(struct mbuf *); 905 struct mbuf *m_fragment(struct mbuf *, int, int); 906 void m_freem(struct mbuf *); 907 struct mbuf *m_getm2(struct mbuf *, int, int, short, int); 908 struct mbuf *m_getptr(struct mbuf *, int, int *); 909 u_int m_length(struct mbuf *, struct mbuf **); 910 int m_mbuftouio(struct uio *, struct mbuf *, int); 911 void m_move_pkthdr(struct mbuf *, struct mbuf *); 912 struct mbuf *m_prepend(struct mbuf *, int, int); 913 void m_print(const struct mbuf *, int); 914 struct mbuf *m_pulldown(struct mbuf *, int, int, int *); 915 struct mbuf *m_pullup(struct mbuf *, int); 916 int m_sanity(struct mbuf *, int); 917 struct mbuf *m_split(struct mbuf *, int, int); 918 struct mbuf *m_uiotombuf(struct uio *, int, int, int, int); 919 struct mbuf *m_unshare(struct mbuf *, int how); 920 921 /*- 922 * Network packets may have annotations attached by affixing a list of 923 * "packet tags" to the pkthdr structure. Packet tags are dynamically 924 * allocated semi-opaque data structures that have a fixed header 925 * (struct m_tag) that specifies the size of the memory block and a 926 * <cookie,type> pair that identifies it. The cookie is a 32-bit unique 927 * unsigned value used to identify a module or ABI. By convention this value 928 * is chosen as the date+time that the module is created, expressed as the 929 * number of seconds since the epoch (e.g., using date -u +'%s'). The type 930 * value is an ABI/module-specific value that identifies a particular 931 * annotation and is private to the module. For compatibility with systems 932 * like OpenBSD that define packet tags w/o an ABI/module cookie, the value 933 * PACKET_ABI_COMPAT is used to implement m_tag_get and m_tag_find 934 * compatibility shim functions and several tag types are defined below. 935 * Users that do not require compatibility should use a private cookie value 936 * so that packet tag-related definitions can be maintained privately. 937 * 938 * Note that the packet tag returned by m_tag_alloc has the default memory 939 * alignment implemented by malloc. To reference private data one can use a 940 * construct like: 941 * 942 * struct m_tag *mtag = m_tag_alloc(...); 943 * struct foo *p = (struct foo *)(mtag+1); 944 * 945 * if the alignment of struct m_tag is sufficient for referencing members of 946 * struct foo. Otherwise it is necessary to embed struct m_tag within the 947 * private data structure to insure proper alignment; e.g., 948 * 949 * struct foo { 950 * struct m_tag tag; 951 * ... 952 * }; 953 * struct foo *p = (struct foo *) m_tag_alloc(...); 954 * struct m_tag *mtag = &p->tag; 955 */ 956 957 /* 958 * Persistent tags stay with an mbuf until the mbuf is reclaimed. Otherwise 959 * tags are expected to ``vanish'' when they pass through a network 960 * interface. For most interfaces this happens normally as the tags are 961 * reclaimed when the mbuf is free'd. However in some special cases 962 * reclaiming must be done manually. An example is packets that pass through 963 * the loopback interface. Also, one must be careful to do this when 964 * ``turning around'' packets (e.g., icmp_reflect). 965 * 966 * To mark a tag persistent bit-or this flag in when defining the tag id. 967 * The tag will then be treated as described above. 968 */ 969 #define MTAG_PERSISTENT 0x800 970 971 #define PACKET_TAG_NONE 0 /* Nadda */ 972 973 /* Packet tags for use with PACKET_ABI_COMPAT. */ 974 #define PACKET_TAG_IPSEC_IN_DONE 1 /* IPsec applied, in */ 975 #define PACKET_TAG_IPSEC_OUT_DONE 2 /* IPsec applied, out */ 976 #define PACKET_TAG_IPSEC_IN_CRYPTO_DONE 3 /* NIC IPsec crypto done */ 977 #define PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED 4 /* NIC IPsec crypto req'ed */ 978 #define PACKET_TAG_IPSEC_IN_COULD_DO_CRYPTO 5 /* NIC notifies IPsec */ 979 #define PACKET_TAG_IPSEC_PENDING_TDB 6 /* Reminder to do IPsec */ 980 #define PACKET_TAG_BRIDGE 7 /* Bridge processing done */ 981 #define PACKET_TAG_GIF 8 /* GIF processing done */ 982 #define PACKET_TAG_GRE 9 /* GRE processing done */ 983 #define PACKET_TAG_IN_PACKET_CHECKSUM 10 /* NIC checksumming done */ 984 #define PACKET_TAG_ENCAP 11 /* Encap. processing */ 985 #define PACKET_TAG_IPSEC_SOCKET 12 /* IPSEC socket ref */ 986 #define PACKET_TAG_IPSEC_HISTORY 13 /* IPSEC history */ 987 #define PACKET_TAG_IPV6_INPUT 14 /* IPV6 input processing */ 988 #define PACKET_TAG_DUMMYNET 15 /* dummynet info */ 989 #define PACKET_TAG_DIVERT 17 /* divert info */ 990 #define PACKET_TAG_IPFORWARD 18 /* ipforward info */ 991 #define PACKET_TAG_MACLABEL (19 | MTAG_PERSISTENT) /* MAC label */ 992 #define PACKET_TAG_PF 21 /* PF + ALTQ information */ 993 #define PACKET_TAG_RTSOCKFAM 25 /* rtsock sa family */ 994 #define PACKET_TAG_IPOPTIONS 27 /* Saved IP options */ 995 #define PACKET_TAG_CARP 28 /* CARP info */ 996 #define PACKET_TAG_IPSEC_NAT_T_PORTS 29 /* two uint16_t */ 997 #define PACKET_TAG_ND_OUTGOING 30 /* ND outgoing */ 998 999 /* Specific cookies and tags. */ 1000 1001 /* Packet tag routines. */ 1002 struct m_tag *m_tag_alloc(u_int32_t, int, int, int); 1003 void m_tag_delete(struct mbuf *, struct m_tag *); 1004 void m_tag_delete_chain(struct mbuf *, struct m_tag *); 1005 void m_tag_free_default(struct m_tag *); 1006 struct m_tag *m_tag_locate(struct mbuf *, u_int32_t, int, struct m_tag *); 1007 struct m_tag *m_tag_copy(struct m_tag *, int); 1008 int m_tag_copy_chain(struct mbuf *, struct mbuf *, int); 1009 void m_tag_delete_nonpersistent(struct mbuf *); 1010 1011 /* 1012 * Initialize the list of tags associated with an mbuf. 1013 */ 1014 static __inline void 1015 m_tag_init(struct mbuf *m) 1016 { 1017 1018 SLIST_INIT(&m->m_pkthdr.tags); 1019 } 1020 1021 /* 1022 * Set up the contents of a tag. Note that this does not fill in the free 1023 * method; the caller is expected to do that. 1024 * 1025 * XXX probably should be called m_tag_init, but that was already taken. 1026 */ 1027 static __inline void 1028 m_tag_setup(struct m_tag *t, u_int32_t cookie, int type, int len) 1029 { 1030 1031 t->m_tag_id = type; 1032 t->m_tag_len = len; 1033 t->m_tag_cookie = cookie; 1034 } 1035 1036 /* 1037 * Reclaim resources associated with a tag. 1038 */ 1039 static __inline void 1040 m_tag_free(struct m_tag *t) 1041 { 1042 1043 (*t->m_tag_free)(t); 1044 } 1045 1046 /* 1047 * Return the first tag associated with an mbuf. 1048 */ 1049 static __inline struct m_tag * 1050 m_tag_first(struct mbuf *m) 1051 { 1052 1053 return (SLIST_FIRST(&m->m_pkthdr.tags)); 1054 } 1055 1056 /* 1057 * Return the next tag in the list of tags associated with an mbuf. 1058 */ 1059 static __inline struct m_tag * 1060 m_tag_next(struct mbuf *m, struct m_tag *t) 1061 { 1062 1063 return (SLIST_NEXT(t, m_tag_link)); 1064 } 1065 1066 /* 1067 * Prepend a tag to the list of tags associated with an mbuf. 1068 */ 1069 static __inline void 1070 m_tag_prepend(struct mbuf *m, struct m_tag *t) 1071 { 1072 1073 SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link); 1074 } 1075 1076 /* 1077 * Unlink a tag from the list of tags associated with an mbuf. 1078 */ 1079 static __inline void 1080 m_tag_unlink(struct mbuf *m, struct m_tag *t) 1081 { 1082 1083 SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link); 1084 } 1085 1086 /* These are for OpenBSD compatibility. */ 1087 #define MTAG_ABI_COMPAT 0 /* compatibility ABI */ 1088 1089 static __inline struct m_tag * 1090 m_tag_get(int type, int length, int wait) 1091 { 1092 return (m_tag_alloc(MTAG_ABI_COMPAT, type, length, wait)); 1093 } 1094 1095 static __inline struct m_tag * 1096 m_tag_find(struct mbuf *m, int type, struct m_tag *start) 1097 { 1098 return (SLIST_EMPTY(&m->m_pkthdr.tags) ? (struct m_tag *)NULL : 1099 m_tag_locate(m, MTAG_ABI_COMPAT, type, start)); 1100 } 1101 1102 /* XXX temporary FIB methods probably eventually use tags.*/ 1103 #define M_FIBSHIFT 28 1104 #define M_FIBMASK 0x0F 1105 1106 /* get the fib from an mbuf and if it is not set, return the default */ 1107 #define M_GETFIB(_m) \ 1108 ((((_m)->m_flags & M_FIB) >> M_FIBSHIFT) & M_FIBMASK) 1109 1110 #define M_SETFIB(_m, _fib) do { \ 1111 _m->m_flags &= ~M_FIB; \ 1112 _m->m_flags |= (((_fib) << M_FIBSHIFT) & M_FIB); \ 1113 } while (0) 1114 1115 #endif /* _KERNEL */ 1116 1117 #ifdef MBUF_PROFILING 1118 void m_profile(struct mbuf *m); 1119 #define M_PROFILE(m) m_profile(m) 1120 #else 1121 #define M_PROFILE(m) 1122 #endif 1123 1124 1125 #endif /* !_SYS_MBUF_H_ */ 1126