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