1.\" Copyright (c) 2000 FreeBSD Inc. 2.\" All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16.\" ARE DISCLAIMED. IN NO EVENT SHALL [your name] OR CONTRIBUTORS BE LIABLE 17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23.\" SUCH DAMAGE. 24.\" 25.\" $FreeBSD$ 26.\" 27.Dd August 30, 2016 28.Dt MBUF 9 29.Os 30.\" 31.Sh NAME 32.Nm mbuf 33.Nd "memory management in the kernel IPC subsystem" 34.\" 35.Sh SYNOPSIS 36.In sys/param.h 37.In sys/systm.h 38.In sys/mbuf.h 39.\" 40.Ss Mbuf allocation macros 41.Fn MGET "struct mbuf *mbuf" "int how" "short type" 42.Fn MGETHDR "struct mbuf *mbuf" "int how" "short type" 43.Ft int 44.Fn MCLGET "struct mbuf *mbuf" "int how" 45.Fo MEXTADD 46.Fa "struct mbuf *mbuf" 47.Fa "caddr_t buf" 48.Fa "u_int size" 49.Fa "void (*free)(void *opt_arg1, void *opt_arg2)" 50.Fa "void *opt_arg1" 51.Fa "void *opt_arg2" 52.Fa "short flags" 53.Fa "int type" 54.Fc 55.\" 56.Ss Mbuf utility macros 57.Fn mtod "struct mbuf *mbuf" "type" 58.Fn M_ALIGN "struct mbuf *mbuf" "u_int len" 59.Fn MH_ALIGN "struct mbuf *mbuf" "u_int len" 60.Ft int 61.Fn M_LEADINGSPACE "struct mbuf *mbuf" 62.Ft int 63.Fn M_TRAILINGSPACE "struct mbuf *mbuf" 64.Fn M_MOVE_PKTHDR "struct mbuf *to" "struct mbuf *from" 65.Fn M_PREPEND "struct mbuf *mbuf" "int len" "int how" 66.Fn MCHTYPE "struct mbuf *mbuf" "short type" 67.Ft int 68.Fn M_WRITABLE "struct mbuf *mbuf" 69.\" 70.Ss Mbuf allocation functions 71.Ft struct mbuf * 72.Fn m_get "int how" "short type" 73.Ft struct mbuf * 74.Fn m_get2 "int size" "int how" "short type" "int flags" 75.Ft struct mbuf * 76.Fn m_getm "struct mbuf *orig" "int len" "int how" "short type" 77.Ft struct mbuf * 78.Fn m_getjcl "int how" "short type" "int flags" "int size" 79.Ft struct mbuf * 80.Fn m_getcl "int how" "short type" "int flags" 81.Ft struct mbuf * 82.Fn m_getclr "int how" "short type" 83.Ft struct mbuf * 84.Fn m_gethdr "int how" "short type" 85.Ft struct mbuf * 86.Fn m_free "struct mbuf *mbuf" 87.Ft void 88.Fn m_freem "struct mbuf *mbuf" 89.\" 90.Ss Mbuf utility functions 91.Ft void 92.Fn m_adj "struct mbuf *mbuf" "int len" 93.Ft void 94.Fn m_align "struct mbuf *mbuf" "int len" 95.Ft int 96.Fn m_append "struct mbuf *mbuf" "int len" "c_caddr_t cp" 97.Ft struct mbuf * 98.Fn m_prepend "struct mbuf *mbuf" "int len" "int how" 99.Ft struct mbuf * 100.Fn m_copyup "struct mbuf *mbuf" "int len" "int dstoff" 101.Ft struct mbuf * 102.Fn m_pullup "struct mbuf *mbuf" "int len" 103.Ft struct mbuf * 104.Fn m_pulldown "struct mbuf *mbuf" "int offset" "int len" "int *offsetp" 105.Ft struct mbuf * 106.Fn m_copym "struct mbuf *mbuf" "int offset" "int len" "int how" 107.Ft struct mbuf * 108.Fn m_copypacket "struct mbuf *mbuf" "int how" 109.Ft struct mbuf * 110.Fn m_dup "struct mbuf *mbuf" "int how" 111.Ft void 112.Fn m_copydata "const struct mbuf *mbuf" "int offset" "int len" "caddr_t buf" 113.Ft void 114.Fn m_copyback "struct mbuf *mbuf" "int offset" "int len" "caddr_t buf" 115.Ft struct mbuf * 116.Fo m_devget 117.Fa "char *buf" 118.Fa "int len" 119.Fa "int offset" 120.Fa "struct ifnet *ifp" 121.Fa "void (*copy)(char *from, caddr_t to, u_int len)" 122.Fc 123.Ft void 124.Fn m_cat "struct mbuf *m" "struct mbuf *n" 125.Ft void 126.Fn m_catpkt "struct mbuf *m" "struct mbuf *n" 127.Ft u_int 128.Fn m_fixhdr "struct mbuf *mbuf" 129.Ft void 130.Fn m_dup_pkthdr "struct mbuf *to" "struct mbuf *from" 131.Ft void 132.Fn m_move_pkthdr "struct mbuf *to" "struct mbuf *from" 133.Ft u_int 134.Fn m_length "struct mbuf *mbuf" "struct mbuf **last" 135.Ft struct mbuf * 136.Fn m_split "struct mbuf *mbuf" "int len" "int how" 137.Ft int 138.Fn m_apply "struct mbuf *mbuf" "int off" "int len" "int (*f)(void *arg, void *data, u_int len)" "void *arg" 139.Ft struct mbuf * 140.Fn m_getptr "struct mbuf *mbuf" "int loc" "int *off" 141.Ft struct mbuf * 142.Fn m_defrag "struct mbuf *m0" "int how" 143.Ft struct mbuf * 144.Fn m_collapse "struct mbuf *m0" "int how" "int maxfrags" 145.Ft struct mbuf * 146.Fn m_unshare "struct mbuf *m0" "int how" 147.\" 148.Sh DESCRIPTION 149An 150.Vt mbuf 151is a basic unit of memory management in the kernel IPC subsystem. 152Network packets and socket buffers are stored in 153.Vt mbufs . 154A network packet may span multiple 155.Vt mbufs 156arranged into a 157.Vt mbuf chain 158(linked list), 159which allows adding or trimming 160network headers with little overhead. 161.Pp 162While a developer should not bother with 163.Vt mbuf 164internals without serious 165reason in order to avoid incompatibilities with future changes, it 166is useful to understand the general structure of an 167.Vt mbuf . 168.Pp 169An 170.Vt mbuf 171consists of a variable-sized header and a small internal 172buffer for data. 173The total size of an 174.Vt mbuf , 175.Dv MSIZE , 176is a constant defined in 177.In sys/param.h . 178The 179.Vt mbuf 180header includes: 181.Bl -tag -width "m_nextpkt" -offset indent 182.It Va m_next 183.Pq Vt struct mbuf * 184A pointer to the next 185.Vt mbuf 186in the 187.Vt mbuf chain . 188.It Va m_nextpkt 189.Pq Vt struct mbuf * 190A pointer to the next 191.Vt mbuf chain 192in the queue. 193.It Va m_data 194.Pq Vt caddr_t 195A pointer to data attached to this 196.Vt mbuf . 197.It Va m_len 198.Pq Vt int 199The length of the data. 200.It Va m_type 201.Pq Vt short 202The type of the data. 203.It Va m_flags 204.Pq Vt int 205The 206.Vt mbuf 207flags. 208.El 209.Pp 210The 211.Vt mbuf 212flag bits are defined as follows: 213.Bd -literal 214/* mbuf flags */ 215#define M_EXT 0x00000001 /* has associated external storage */ 216#define M_PKTHDR 0x00000002 /* start of record */ 217#define M_EOR 0x00000004 /* end of record */ 218#define M_RDONLY 0x00000008 /* associated data marked read-only */ 219#define M_PROTO1 0x00001000 /* protocol-specific */ 220#define M_PROTO2 0x00002000 /* protocol-specific */ 221#define M_PROTO3 0x00004000 /* protocol-specific */ 222#define M_PROTO4 0x00008000 /* protocol-specific */ 223#define M_PROTO5 0x00010000 /* protocol-specific */ 224#define M_PROTO6 0x00020000 /* protocol-specific */ 225#define M_PROTO7 0x00040000 /* protocol-specific */ 226#define M_PROTO8 0x00080000 /* protocol-specific */ 227#define M_PROTO9 0x00100000 /* protocol-specific */ 228#define M_PROTO10 0x00200000 /* protocol-specific */ 229#define M_PROTO11 0x00400000 /* protocol-specific */ 230#define M_PROTO12 0x00800000 /* protocol-specific */ 231 232/* mbuf pkthdr flags (also stored in m_flags) */ 233#define M_BCAST 0x00000010 /* send/received as link-level broadcast */ 234#define M_MCAST 0x00000020 /* send/received as link-level multicast */ 235.Ed 236.Pp 237The available 238.Vt mbuf 239types are defined as follows: 240.Bd -literal 241/* mbuf types */ 242#define MT_DATA 1 /* dynamic (data) allocation */ 243#define MT_HEADER MT_DATA /* packet header */ 244#define MT_SONAME 8 /* socket name */ 245#define MT_CONTROL 14 /* extra-data protocol message */ 246#define MT_OOBDATA 15 /* expedited data */ 247.Ed 248.Pp 249The available external buffer types are defined as follows: 250.Bd -literal 251/* external buffer types */ 252#define EXT_CLUSTER 1 /* mbuf cluster */ 253#define EXT_SFBUF 2 /* sendfile(2)'s sf_bufs */ 254#define EXT_JUMBOP 3 /* jumbo cluster 4096 bytes */ 255#define EXT_JUMBO9 4 /* jumbo cluster 9216 bytes */ 256#define EXT_JUMBO16 5 /* jumbo cluster 16184 bytes */ 257#define EXT_PACKET 6 /* mbuf+cluster from packet zone */ 258#define EXT_MBUF 7 /* external mbuf reference */ 259#define EXT_NET_DRV 252 /* custom ext_buf provided by net driver(s) */ 260#define EXT_MOD_TYPE 253 /* custom module's ext_buf type */ 261#define EXT_DISPOSABLE 254 /* can throw this buffer away w/page flipping */ 262#define EXT_EXTREF 255 /* has externally maintained ref_cnt ptr */ 263.Ed 264.Pp 265If the 266.Dv M_PKTHDR 267flag is set, a 268.Vt struct pkthdr Va m_pkthdr 269is added to the 270.Vt mbuf 271header. 272It contains a pointer to the interface 273the packet has been received from 274.Pq Vt struct ifnet Va *rcvif , 275and the total packet length 276.Pq Vt int Va len . 277Optionally, it may also contain an attached list of packet tags 278.Pq Vt "struct m_tag" . 279See 280.Xr mbuf_tags 9 281for details. 282Fields used in offloading checksum calculation to the hardware are kept in 283.Va m_pkthdr 284as well. 285See 286.Sx HARDWARE-ASSISTED CHECKSUM CALCULATION 287for details. 288.Pp 289If small enough, data is stored in the internal data buffer of an 290.Vt mbuf . 291If the data is sufficiently large, another 292.Vt mbuf 293may be added to the 294.Vt mbuf chain , 295or external storage may be associated with the 296.Vt mbuf . 297.Dv MHLEN 298bytes of data can fit into an 299.Vt mbuf 300with the 301.Dv M_PKTHDR 302flag set, 303.Dv MLEN 304bytes can otherwise. 305.Pp 306If external storage is being associated with an 307.Vt mbuf , 308the 309.Va m_ext 310header is added at the cost of losing the internal data buffer. 311It includes a pointer to external storage, the size of the storage, 312a pointer to a function used for freeing the storage, 313a pointer to an optional argument that can be passed to the function, 314and a pointer to a reference counter. 315An 316.Vt mbuf 317using external storage has the 318.Dv M_EXT 319flag set. 320.Pp 321The system supplies a macro for allocating the desired external storage 322buffer, 323.Dv MEXTADD . 324.Pp 325The allocation and management of the reference counter is handled by the 326subsystem. 327.Pp 328The system also supplies a default type of external storage buffer called an 329.Vt mbuf cluster . 330.Vt Mbuf clusters 331can be allocated and configured with the use of the 332.Dv MCLGET 333macro. 334Each 335.Vt mbuf cluster 336is 337.Dv MCLBYTES 338in size, where MCLBYTES is a machine-dependent constant. 339The system defines an advisory macro 340.Dv MINCLSIZE , 341which is the smallest amount of data to put into an 342.Vt mbuf cluster . 343It is equal to 344.Dv MHLEN 345plus one. 346It is typically preferable to store data into the data region of an 347.Vt mbuf , 348if size permits, as opposed to allocating a separate 349.Vt mbuf cluster 350to hold the same data. 351.\" 352.Ss Macros and Functions 353There are numerous predefined macros and functions that provide the 354developer with common utilities. 355.\" 356.Bl -ohang -offset indent 357.It Fn mtod mbuf type 358Convert an 359.Fa mbuf 360pointer to a data pointer. 361The macro expands to the data pointer cast to the specified 362.Fa type . 363.Sy Note : 364It is advisable to ensure that there is enough contiguous data in 365.Fa mbuf . 366See 367.Fn m_pullup 368for details. 369.It Fn MGET mbuf how type 370Allocate an 371.Vt mbuf 372and initialize it to contain internal data. 373.Fa mbuf 374will point to the allocated 375.Vt mbuf 376on success, or be set to 377.Dv NULL 378on failure. 379The 380.Fa how 381argument is to be set to 382.Dv M_WAITOK 383or 384.Dv M_NOWAIT . 385It specifies whether the caller is willing to block if necessary. 386A number of other functions and macros related to 387.Vt mbufs 388have the same argument because they may 389at some point need to allocate new 390.Vt mbufs . 391.It Fn MGETHDR mbuf how type 392Allocate an 393.Vt mbuf 394and initialize it to contain a packet header 395and internal data. 396See 397.Fn MGET 398for details. 399.It Fn MEXTADD mbuf buf size free opt_arg1 opt_arg2 flags type 400Associate externally managed data with 401.Fa mbuf . 402Any internal data contained in the mbuf will be discarded, and the 403.Dv M_EXT 404flag will be set. 405The 406.Fa buf 407and 408.Fa size 409arguments are the address and length, respectively, of the data. 410The 411.Fa free 412argument points to a function which will be called to free the data 413when the mbuf is freed; it is only used if 414.Fa type 415is 416.Dv EXT_EXTREF . 417The 418.Fa opt_arg1 419and 420.Fa opt_arg2 421arguments will be passed unmodified to 422.Fa free . 423The 424.Fa flags 425argument specifies additional 426.Vt mbuf 427flags; it is not necessary to specify 428.Dv M_EXT . 429Finally, the 430.Fa type 431argument specifies the type of external data, which controls how it 432will be disposed of when the 433.Vt mbuf 434is freed. 435In most cases, the correct value is 436.Dv EXT_EXTREF . 437.It Fn MCLGET mbuf how 438Allocate and attach an 439.Vt mbuf cluster 440to 441.Fa mbuf . 442On success, a non-zero value returned; otherwise, 0. 443Historically, consumers would check for success by testing the 444.Dv M_EXT 445flag on the mbuf, but this is now discouraged to avoid unnecessary awareness 446of the implementation of external storage in protocol stacks and device 447drivers. 448.It Fn M_ALIGN mbuf len 449Set the pointer 450.Fa mbuf->m_data 451to place an object of the size 452.Fa len 453at the end of the internal data area of 454.Fa mbuf , 455long word aligned. 456Applicable only if 457.Fa mbuf 458is newly allocated with 459.Fn MGET 460or 461.Fn m_get . 462.It Fn MH_ALIGN mbuf len 463Serves the same purpose as 464.Fn M_ALIGN 465does, but only for 466.Fa mbuf 467newly allocated with 468.Fn MGETHDR 469or 470.Fn m_gethdr , 471or initialized by 472.Fn m_dup_pkthdr 473or 474.Fn m_move_pkthdr . 475.It Fn m_align mbuf len 476Services the same purpose as 477.Fn M_ALIGN 478but handles any type of mbuf. 479.It Fn M_LEADINGSPACE mbuf 480Returns the number of bytes available before the beginning 481of data in 482.Fa mbuf . 483.It Fn M_TRAILINGSPACE mbuf 484Returns the number of bytes available after the end of data in 485.Fa mbuf . 486.It Fn M_PREPEND mbuf len how 487This macro operates on an 488.Vt mbuf chain . 489It is an optimized wrapper for 490.Fn m_prepend 491that can make use of possible empty space before data 492(e.g.\& left after trimming of a link-layer header). 493The new 494.Vt mbuf chain 495pointer or 496.Dv NULL 497is in 498.Fa mbuf 499after the call. 500.It Fn M_MOVE_PKTHDR to from 501Using this macro is equivalent to calling 502.Fn m_move_pkthdr to from . 503.It Fn M_WRITABLE mbuf 504This macro will evaluate true if 505.Fa mbuf 506is not marked 507.Dv M_RDONLY 508and if either 509.Fa mbuf 510does not contain external storage or, 511if it does, 512then if the reference count of the storage is not greater than 1. 513The 514.Dv M_RDONLY 515flag can be set in 516.Fa mbuf->m_flags . 517This can be achieved during setup of the external storage, 518by passing the 519.Dv M_RDONLY 520bit as a 521.Fa flags 522argument to the 523.Fn MEXTADD 524macro, or can be directly set in individual 525.Vt mbufs . 526.It Fn MCHTYPE mbuf type 527Change the type of 528.Fa mbuf 529to 530.Fa type . 531This is a relatively expensive operation and should be avoided. 532.El 533.Pp 534The functions are: 535.Bl -ohang -offset indent 536.It Fn m_get how type 537A function version of 538.Fn MGET 539for non-critical paths. 540.It Fn m_get2 size how type flags 541Allocate an 542.Vt mbuf 543with enough space to hold specified amount of data. 544.It Fn m_getm orig len how type 545Allocate 546.Fa len 547bytes worth of 548.Vt mbufs 549and 550.Vt mbuf clusters 551if necessary and append the resulting allocated 552.Vt mbuf chain 553to the 554.Vt mbuf chain 555.Fa orig , 556if it is 557.No non- Ns Dv NULL . 558If the allocation fails at any point, 559free whatever was allocated and return 560.Dv NULL . 561If 562.Fa orig 563is 564.No non- Ns Dv NULL , 565it will not be freed. 566It is possible to use 567.Fn m_getm 568to either append 569.Fa len 570bytes to an existing 571.Vt mbuf 572or 573.Vt mbuf chain 574(for example, one which may be sitting in a pre-allocated ring) 575or to simply perform an all-or-nothing 576.Vt mbuf 577and 578.Vt mbuf cluster 579allocation. 580.It Fn m_gethdr how type 581A function version of 582.Fn MGETHDR 583for non-critical paths. 584.It Fn m_getcl how type flags 585Fetch an 586.Vt mbuf 587with a 588.Vt mbuf cluster 589attached to it. 590If one of the allocations fails, the entire allocation fails. 591This routine is the preferred way of fetching both the 592.Vt mbuf 593and 594.Vt mbuf cluster 595together, as it avoids having to unlock/relock between allocations. 596Returns 597.Dv NULL 598on failure. 599.It Fn m_getjcl how type flags size 600This is like 601.Fn m_getcl 602but it the size of the cluster allocated will be large enough for 603.Fa size 604bytes. 605.It Fn m_getclr how type 606Allocate an 607.Vt mbuf 608and zero out the data region. 609.It Fn m_free mbuf 610Frees 611.Vt mbuf . 612Returns 613.Va m_next 614of the freed 615.Vt mbuf . 616.El 617.Pp 618The functions below operate on 619.Vt mbuf chains . 620.Bl -ohang -offset indent 621.It Fn m_freem mbuf 622Free an entire 623.Vt mbuf chain , 624including any external storage. 625.\" 626.It Fn m_adj mbuf len 627Trim 628.Fa len 629bytes from the head of an 630.Vt mbuf chain 631if 632.Fa len 633is positive, from the tail otherwise. 634.\" 635.It Fn m_append mbuf len cp 636Append 637.Vt len 638bytes of data 639.Vt cp 640to the 641.Vt mbuf chain . 642Extend the mbuf chain if the new data does not fit in 643existing space. 644.\" 645.It Fn m_prepend mbuf len how 646Allocate a new 647.Vt mbuf 648and prepend it to the 649.Vt mbuf chain , 650handle 651.Dv M_PKTHDR 652properly. 653.Sy Note : 654It does not allocate any 655.Vt mbuf clusters , 656so 657.Fa len 658must be less than 659.Dv MLEN 660or 661.Dv MHLEN , 662depending on the 663.Dv M_PKTHDR 664flag setting. 665.\" 666.It Fn m_copyup mbuf len dstoff 667Similar to 668.Fn m_pullup 669but copies 670.Fa len 671bytes of data into a new mbuf at 672.Fa dstoff 673bytes into the mbuf. 674The 675.Fa dstoff 676argument aligns the data and leaves room for a link layer header. 677Returns the new 678.Vt mbuf chain 679on success, 680and frees the 681.Vt mbuf chain 682and returns 683.Dv NULL 684on failure. 685.Sy Note : 686The function does not allocate 687.Vt mbuf clusters , 688so 689.Fa len + dstoff 690must be less than 691.Dv MHLEN . 692.\" 693.It Fn m_pullup mbuf len 694Arrange that the first 695.Fa len 696bytes of an 697.Vt mbuf chain 698are contiguous and lay in the data area of 699.Fa mbuf , 700so they are accessible with 701.Fn mtod mbuf type . 702It is important to remember that this may involve 703reallocating some mbufs and moving data so all pointers 704referencing data within the old mbuf chain 705must be recalculated or made invalid. 706Return the new 707.Vt mbuf chain 708on success, 709.Dv NULL 710on failure 711(the 712.Vt mbuf chain 713is freed in this case). 714.Sy Note : 715It does not allocate any 716.Vt mbuf clusters , 717so 718.Fa len 719must be less than or equal to 720.Dv MHLEN . 721.\" 722.It Fn m_pulldown mbuf offset len offsetp 723Arrange that 724.Fa len 725bytes between 726.Fa offset 727and 728.Fa offset + len 729in the 730.Vt mbuf chain 731are contiguous and lay in the data area of 732.Fa mbuf , 733so they are accessible with 734.Fn mtod mbuf type . 735.Fa len 736must be smaller than, or equal to, the size of an 737.Vt mbuf cluster . 738Return a pointer to an intermediate 739.Vt mbuf 740in the chain containing the requested region; 741the offset in the data region of the 742.Vt mbuf chain 743to the data contained in the returned mbuf is stored in 744.Fa *offsetp . 745If 746.Fa offsetp 747is NULL, the region may be accessed using 748.Fn mtod mbuf type . 749If 750.Fa offsetp 751is non-NULL, the region may be accessed using 752.Fn mtod mbuf uint8_t 753+ *offsetp. 754The region of the mbuf chain between its beginning and 755.Fa offset 756is not modified, therefore it is safe to hold pointers to data within 757this region before calling 758.Fn m_pulldown . 759.\" 760.It Fn m_copym mbuf offset len how 761Make a copy of an 762.Vt mbuf chain 763starting 764.Fa offset 765bytes from the beginning, continuing for 766.Fa len 767bytes. 768If 769.Fa len 770is 771.Dv M_COPYALL , 772copy to the end of the 773.Vt mbuf chain . 774.Sy Note : 775The copy is read-only, because the 776.Vt mbuf clusters 777are not copied, only their reference counts are incremented. 778.\" 779.It Fn m_copypacket mbuf how 780Copy an entire packet including header, which must be present. 781This is an optimized version of the common case 782.Fn m_copym mbuf 0 M_COPYALL how . 783.Sy Note : 784the copy is read-only, because the 785.Vt mbuf clusters 786are not copied, only their reference counts are incremented. 787.\" 788.It Fn m_dup mbuf how 789Copy a packet header 790.Vt mbuf chain 791into a completely new 792.Vt mbuf chain , 793including copying any 794.Vt mbuf clusters . 795Use this instead of 796.Fn m_copypacket 797when you need a writable copy of an 798.Vt mbuf chain . 799.\" 800.It Fn m_copydata mbuf offset len buf 801Copy data from an 802.Vt mbuf chain 803starting 804.Fa off 805bytes from the beginning, continuing for 806.Fa len 807bytes, into the indicated buffer 808.Fa buf . 809.\" 810.It Fn m_copyback mbuf offset len buf 811Copy 812.Fa len 813bytes from the buffer 814.Fa buf 815back into the indicated 816.Vt mbuf chain , 817starting at 818.Fa offset 819bytes from the beginning of the 820.Vt mbuf chain , 821extending the 822.Vt mbuf chain 823if necessary. 824.Sy Note : 825It does not allocate any 826.Vt mbuf clusters , 827just adds 828.Vt mbufs 829to the 830.Vt mbuf chain . 831It is safe to set 832.Fa offset 833beyond the current 834.Vt mbuf chain 835end: zeroed 836.Vt mbufs 837will be allocated to fill the space. 838.\" 839.It Fn m_length mbuf last 840Return the length of the 841.Vt mbuf chain , 842and optionally a pointer to the last 843.Vt mbuf . 844.\" 845.It Fn m_dup_pkthdr to from how 846Upon the function's completion, the 847.Vt mbuf 848.Fa to 849will contain an identical copy of 850.Fa from->m_pkthdr 851and the per-packet attributes found in the 852.Vt mbuf chain 853.Fa from . 854The 855.Vt mbuf 856.Fa from 857must have the flag 858.Dv M_PKTHDR 859initially set, and 860.Fa to 861must be empty on entry. 862.\" 863.It Fn m_move_pkthdr to from 864Move 865.Va m_pkthdr 866and the per-packet attributes from the 867.Vt mbuf chain 868.Fa from 869to the 870.Vt mbuf 871.Fa to . 872The 873.Vt mbuf 874.Fa from 875must have the flag 876.Dv M_PKTHDR 877initially set, and 878.Fa to 879must be empty on entry. 880Upon the function's completion, 881.Fa from 882will have the flag 883.Dv M_PKTHDR 884and the per-packet attributes cleared. 885.\" 886.It Fn m_fixhdr mbuf 887Set the packet-header length to the length of the 888.Vt mbuf chain . 889.\" 890.It Fn m_devget buf len offset ifp copy 891Copy data from a device local memory pointed to by 892.Fa buf 893to an 894.Vt mbuf chain . 895The copy is done using a specified copy routine 896.Fa copy , 897or 898.Fn bcopy 899if 900.Fa copy 901is 902.Dv NULL . 903.\" 904.It Fn m_cat m n 905Concatenate 906.Fa n 907to 908.Fa m . 909Both 910.Vt mbuf chains 911must be of the same type. 912.Fa n 913is not guaranteed to be valid after 914.Fn m_cat 915returns. 916.Fn m_cat 917does not update any packet header fields or free mbuf tags. 918.\" 919.It Fn m_catpkt m n 920A variant of 921.Fn m_cat 922that operates on packets. 923Both 924.Fa m 925and 926.Fa n 927must contain packet headers. 928.Fa n 929is not guaranteed to be valid after 930.Fn m_catpkt 931returns. 932.\" 933.It Fn m_split mbuf len how 934Partition an 935.Vt mbuf chain 936in two pieces, returning the tail: 937all but the first 938.Fa len 939bytes. 940In case of failure, it returns 941.Dv NULL 942and attempts to restore the 943.Vt mbuf chain 944to its original state. 945.\" 946.It Fn m_apply mbuf off len f arg 947Apply a function to an 948.Vt mbuf chain , 949at offset 950.Fa off , 951for length 952.Fa len 953bytes. 954Typically used to avoid calls to 955.Fn m_pullup 956which would otherwise be unnecessary or undesirable. 957.Fa arg 958is a convenience argument which is passed to the callback function 959.Fa f . 960.Pp 961Each time 962.Fn f 963is called, it will be passed 964.Fa arg , 965a pointer to the 966.Fa data 967in the current mbuf, and the length 968.Fa len 969of the data in this mbuf to which the function should be applied. 970.Pp 971The function should return zero to indicate success; 972otherwise, if an error is indicated, then 973.Fn m_apply 974will return the error and stop iterating through the 975.Vt mbuf chain . 976.\" 977.It Fn m_getptr mbuf loc off 978Return a pointer to the mbuf containing the data located at 979.Fa loc 980bytes from the beginning of the 981.Vt mbuf chain . 982The corresponding offset into the mbuf will be stored in 983.Fa *off . 984.It Fn m_defrag m0 how 985Defragment an mbuf chain, returning the shortest possible 986chain of mbufs and clusters. 987If allocation fails and this can not be completed, 988.Dv NULL 989will be returned and the original chain will be unchanged. 990Upon success, the original chain will be freed and the new 991chain will be returned. 992.Fa how 993should be either 994.Dv M_WAITOK 995or 996.Dv M_NOWAIT , 997depending on the caller's preference. 998.Pp 999This function is especially useful in network drivers, where 1000certain long mbuf chains must be shortened before being added 1001to TX descriptor lists. 1002.It Fn m_collapse m0 how maxfrags 1003Defragment an mbuf chain, returning a chain of at most 1004.Fa maxfrags 1005mbufs and clusters. 1006If allocation fails or the chain cannot be collapsed as requested, 1007.Dv NULL 1008will be returned, with the original chain possibly modified. 1009As with 1010.Fn m_defrag , 1011.Fa how 1012should be one of 1013.Dv M_WAITOK 1014or 1015.Dv M_NOWAIT . 1016.It Fn m_unshare m0 how 1017Create a version of the specified mbuf chain whose 1018contents can be safely modified without affecting other users. 1019If allocation fails and this operation can not be completed, 1020.Dv NULL 1021will be returned. 1022The original mbuf chain is always reclaimed and the reference 1023count of any shared mbuf clusters is decremented. 1024.Fa how 1025should be either 1026.Dv M_WAITOK 1027or 1028.Dv M_NOWAIT , 1029depending on the caller's preference. 1030As a side-effect of this process the returned 1031mbuf chain may be compacted. 1032.Pp 1033This function is especially useful in the transmit path of 1034network code, when data must be encrypted or otherwise 1035altered prior to transmission. 1036.El 1037.Sh HARDWARE-ASSISTED CHECKSUM CALCULATION 1038This section currently applies to TCP/IP only. 1039In order to save the host CPU resources, computing checksums is 1040offloaded to the network interface hardware if possible. 1041The 1042.Va m_pkthdr 1043member of the leading 1044.Vt mbuf 1045of a packet contains two fields used for that purpose, 1046.Vt int Va csum_flags 1047and 1048.Vt int Va csum_data . 1049The meaning of those fields depends on the direction a packet flows in, 1050and on whether the packet is fragmented. 1051Henceforth, 1052.Va csum_flags 1053or 1054.Va csum_data 1055of a packet 1056will denote the corresponding field of the 1057.Va m_pkthdr 1058member of the leading 1059.Vt mbuf 1060in the 1061.Vt mbuf chain 1062containing the packet. 1063.Pp 1064On output, checksum offloading is attempted after the outgoing 1065interface has been determined for a packet. 1066The interface-specific field 1067.Va ifnet.if_data.ifi_hwassist 1068(see 1069.Xr ifnet 9 ) 1070is consulted for the capabilities of the interface to assist in 1071computing checksums. 1072The 1073.Va csum_flags 1074field of the packet header is set to indicate which actions the interface 1075is supposed to perform on it. 1076The actions unsupported by the network interface are done in the 1077software prior to passing the packet down to the interface driver; 1078such actions will never be requested through 1079.Va csum_flags . 1080.Pp 1081The flags demanding a particular action from an interface are as follows: 1082.Bl -tag -width ".Dv CSUM_TCP" -offset indent 1083.It Dv CSUM_IP 1084The IP header checksum is to be computed and stored in the 1085corresponding field of the packet. 1086The hardware is expected to know the format of an IP header 1087to determine the offset of the IP checksum field. 1088.It Dv CSUM_TCP 1089The TCP checksum is to be computed. 1090(See below.) 1091.It Dv CSUM_UDP 1092The UDP checksum is to be computed. 1093(See below.) 1094.El 1095.Pp 1096Should a TCP or UDP checksum be offloaded to the hardware, 1097the field 1098.Va csum_data 1099will contain the byte offset of the checksum field relative to the 1100end of the IP header. 1101In this case, the checksum field will be initially 1102set by the TCP/IP module to the checksum of the pseudo header 1103defined by the TCP and UDP specifications. 1104.Pp 1105On input, an interface indicates the actions it has performed 1106on a packet by setting one or more of the following flags in 1107.Va csum_flags 1108associated with the packet: 1109.Bl -tag -width ".Dv CSUM_IP_CHECKED" -offset indent 1110.It Dv CSUM_IP_CHECKED 1111The IP header checksum has been computed. 1112.It Dv CSUM_IP_VALID 1113The IP header has a valid checksum. 1114This flag can appear only in combination with 1115.Dv CSUM_IP_CHECKED . 1116.It Dv CSUM_DATA_VALID 1117The checksum of the data portion of the IP packet has been computed 1118and stored in the field 1119.Va csum_data 1120in network byte order. 1121.It Dv CSUM_PSEUDO_HDR 1122Can be set only along with 1123.Dv CSUM_DATA_VALID 1124to indicate that the IP data checksum found in 1125.Va csum_data 1126allows for the pseudo header defined by the TCP and UDP specifications. 1127Otherwise the checksum of the pseudo header must be calculated by 1128the host CPU and added to 1129.Va csum_data 1130to obtain the final checksum to be used for TCP or UDP validation purposes. 1131.El 1132.Pp 1133If a particular network interface just indicates success or 1134failure of TCP or UDP checksum validation without returning 1135the exact value of the checksum to the host CPU, its driver can mark 1136.Dv CSUM_DATA_VALID 1137and 1138.Dv CSUM_PSEUDO_HDR 1139in 1140.Va csum_flags , 1141and set 1142.Va csum_data 1143to 1144.Li 0xFFFF 1145hexadecimal to indicate a valid checksum. 1146It is a peculiarity of the algorithm used that the Internet checksum 1147calculated over any valid packet will be 1148.Li 0xFFFF 1149as long as the original checksum field is included. 1150.Sh STRESS TESTING 1151When running a kernel compiled with the option 1152.Dv MBUF_STRESS_TEST , 1153the following 1154.Xr sysctl 8 Ns 1155-controlled options may be used to create 1156various failure/extreme cases for testing of network drivers 1157and other parts of the kernel that rely on 1158.Vt mbufs . 1159.Bl -tag -width ident 1160.It Va net.inet.ip.mbuf_frag_size 1161Causes 1162.Fn ip_output 1163to fragment outgoing 1164.Vt mbuf chains 1165into fragments of the specified size. 1166Setting this variable to 1 is an excellent way to 1167test the long 1168.Vt mbuf chain 1169handling ability of network drivers. 1170.It Va kern.ipc.m_defragrandomfailures 1171Causes the function 1172.Fn m_defrag 1173to randomly fail, returning 1174.Dv NULL . 1175Any piece of code which uses 1176.Fn m_defrag 1177should be tested with this feature. 1178.El 1179.Sh RETURN VALUES 1180See above. 1181.Sh SEE ALSO 1182.Xr ifnet 9 , 1183.Xr mbuf_tags 9 1184.Sh HISTORY 1185.\" Please correct me if I'm wrong 1186.Vt Mbufs 1187appeared in an early version of 1188.Bx . 1189Besides being used for network packets, they were used 1190to store various dynamic structures, such as routing table 1191entries, interface addresses, protocol control blocks, etc. 1192In more recent 1193.Fx 1194use of 1195.Vt mbufs 1196is almost entirely limited to packet storage, with 1197.Xr uma 9 1198zones being used directly to store other network-related memory. 1199.Pp 1200Historically, the 1201.Vt mbuf 1202allocator has been a special-purpose memory allocator able to run in 1203interrupt contexts and allocating from a special kernel address space map. 1204As of 1205.Fx 5.3 , 1206the 1207.Vt mbuf 1208allocator is a wrapper around 1209.Xr uma 9 , 1210allowing caching of 1211.Vt mbufs , 1212clusters, and 1213.Vt mbuf 1214+ cluster pairs in per-CPU caches, as well as bringing other benefits of 1215slab allocation. 1216.Sh AUTHORS 1217The original 1218.Nm 1219manual page was written by 1220.An Yar Tikhiy . 1221The 1222.Xr uma 9 1223.Vt mbuf 1224allocator was written by 1225.An Bosko Milekic . 1226