PageRenderTime 42ms CodeModel.GetById 23ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/share/man/man9/mbuf.9

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