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 October 17, 2000 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_args)" 49.Fa "void *opt_args" 50.Fa "short flags" 51.Fa "int type" 52.Fc 53.Fn MEXTFREE "struct mbuf *mbuf" 54.Fn MEXT_ADD_REF "struct mbuf *mbuf" 55.Fn MEXT_REM_REF "struct mbuf *mbuf" 56.Fn MFREE "struct mbuf *mbuf" "struct mbuf *successor" 57.\" 58.Ss Mbuf utility macros 59.Ft void * 60.Fn mtod "struct mbuf *mbuf" "type" 61.Ft int 62.Fn MEXT_IS_REF "struct mbuf *mbuf" 63.Fn M_COPY_PKTHDR "struct mbuf *to" "struct mbuf *from" 64.Fn M_ALIGN "struct mbuf *mbuf" "u_int len" 65.Fn MH_ALIGN "struct mbuf *mbuf" "u_int len" 66.Ft int 67.Fn M_LEADINGSPACE "struct mbuf *mbuf" 68.Ft int 69.Fn M_TRAILINGSPACE "struct mbuf *mbuf" 70.Fn M_PREPEND "struct mbuf *mbuf" "int len" "int how" 71.Fn MCHTYPE "struct mbuf *mbuf" "u_int type" 72.Ft int 73.Fn M_WRITABLE "struct mbuf *mbuf" 74.\" 75.Ss Mbuf allocation functions 76.Ft struct mbuf * 77.Fn m_get "int how" "int type" 78.Ft struct mbuf * 79.Fn m_getm "struct mbuf *orig" "int len" "int how" "int type" 80.Ft struct mbuf * 81.Fn m_getclr "int how" "int type" 82.Ft struct mbuf * 83.Fn m_gethdr "int how" "int type" 84.Ft struct mbuf * 85.Fn m_free "struct mbuf *mbuf" 86.Ft void 87.Fn m_freem "struct mbuf *mbuf" 88.\" 89.Ss Mbuf utility functions 90.Ft void 91.Fn m_adj "struct mbuf *mbuf" "int len" 92.Ft struct mbuf * 93.Fn m_prepend "struct mbuf *mbuf" "int len" "int how" 94.Ft struct mbuf * 95.Fn m_pullup "struct mbuf *mbuf" "int len" 96.Ft struct mbuf * 97.Fn m_copym "struct mbuf *mbuf" "int offset" "int len" "int how" 98.Ft struct mbuf * 99.Fn m_copypacket "struct mbuf *mbuf" "int how" 100.Ft struct mbuf * 101.Fn m_dup "struct mbuf *mbuf" "int how" 102.Ft void 103.Fn m_copydata "const struct mbuf *mbuf" "int offset" "int len" "caddr_t buf" 104.Ft void 105.Fn m_copyback "struct mbuf *mbuf" "int offset" "int len" "caddr_t buf" 106.Ft struct mbuf * 107.Fo m_devget 108.Fa "char *buf" 109.Fa "int len" 110.Fa "int offset" 111.Fa "struct ifnet *ifp" 112.Fa "void (*copy)(char *from, caddr_t to, u_int len)" 113.Fc 114.Ft void 115.Fn m_cat "struct mbuf *m" "struct mbuf *n" 116.Ft struct mbuf * 117.Fn m_split "struct mbuf *mbuf" "int len" "int how" 118.\" 119.Sh DESCRIPTION 120An mbuf is a basic unit of memory management in the kernel IPC subsystem. 121Network packets and socket buffers are stored in mbufs. 122A network packet may span multiple mbufs arranged into a chain 123(linked list), 124which allows adding or trimming 125network headers with little overhead. 126.Pp 127While a developer should not bother with mbuf internals without serious 128reason in order to avoid incompatibilities with future changes, it 129is useful to understand the mbuf's general structure. 130.Pp 131An mbuf consists of a variable-sized header and a small internal 132buffer for data. 133The mbuf's total size, 134.Dv MSIZE , 135is a machine-dependent constant defined in 136.Pa machine/param.h . 137The mbuf header includes: 138.Pp 139.Bl -tag -width "m_nextpkt" -compact -offset indent 140.It Fa m_next 141a pointer to the next buffer in the chain 142.It Fa m_nextpkt 143a pointer to the next chain in the queue 144.It Fa m_data 145a pointer to the data 146.It Fa m_len 147the length of the data 148.It Fa m_type 149the type of data 150.It Fa m_flags 151the mbuf flags 152.El 153.Pp 154The mbuf flag bits are defined as follows: 155.Bd -literal 156/* mbuf flags */ 157#define M_EXT 0x0001 /* has associated external storage */ 158#define M_PKTHDR 0x0002 /* start of record */ 159#define M_EOR 0x0004 /* end of record */ 160#define M_RDONLY 0x0008 /* associated data marked read-only */ 161#define M_PROTO1 0x0010 /* protocol-specific */ 162#define M_PROTO2 0x0020 /* protocol-specific */ 163#define M_PROTO3 0x0040 /* protocol-specific */ 164#define M_PROTO4 0x0080 /* protocol-specific */ 165#define M_PROTO5 0x0100 /* protocol-specific */ 166 167/* mbuf pkthdr flags, also in m_flags */ 168#define M_BCAST 0x0200 /* send/received as link-level broadcast */ 169#define M_MCAST 0x0400 /* send/received as link-level multicast */ 170#define M_FRAG 0x0800 /* packet is fragment of larger packet */ 171#define M_FIRSTFRAG 0x1000 /* packet is first fragment */ 172#define M_LASTFRAG 0x2000 /* packet is last fragment */ 173.Ed 174.Pp 175The available mbuf types are defined as follows: 176.Bd -literal 177/* mbuf types */ 178#define MT_FREE 0 /* should be on free list */ 179#define MT_DATA 1 /* dynamic (data) allocation */ 180#define MT_HEADER 2 /* packet header */ 181#define MT_SONAME 8 /* socket name */ 182#define MT_FTABLE 11 /* fragment reassembly header */ 183#define MT_CONTROL 14 /* extra-data protocol message */ 184#define MT_OOBDATA 15 /* expedited data */ 185.Ed 186.Pp 187If the 188.Dv M_PKTHDR 189flag is set, a 190.Li struct pkthdr m_pkthdr 191is added to the mbuf header. 192It contains a pointer to the interface 193the packet has been received from 194.Pq Fa struct ifnet *rcvif , 195and the total packet length 196.Pq Fa int len . 197.Pp 198If small enough, data is stored in the mbuf's internal data buffer. 199If the data is sufficiently large, another mbuf may be added to the chain, 200or external storage may be associated with the mbuf. 201.Dv MHLEN 202bytes of data can fit into an mbuf with the 203.Dv M_PKTHDR 204flag set, 205.Dv MLEN 206bytes can otherwise. 207.Pp 208If external storage is being associated with an mbuf, the 209.Dv m_ext 210header is added at the cost of losing the internal data buffer. 211It includes a pointer to external storage, the size of the storage, 212a pointer to a function used for freeing the storage, 213a pointer to an optional argument that can be passed to the function, 214and a pointer to a reference counter. 215An mbuf using external storage has the 216.Dv M_EXT 217flag set. 218.Pp 219The system supplies a macro for allocating the desired external storage 220buffer, 221.Dv MEXTADD . 222.Pp 223The allocation and management of the reference counter is handled by the 224subsystem. 225The developer can check whether the reference count for the 226given mbuf's external storage is greater than 1 with the 227.Dv MEXT_IS_REF 228macro. 229Similarly, the developer can directly add and remove references, 230if absolutely necessary, with the use of the 231.Dv MEXT_ADD_REF 232and 233.Dv MEXT_REM_REF 234macros. 235.Pp 236The system also supplies a default type of external storage buffer called an 237.Dq mbuf cluster . 238Mbuf clusters can be allocated and configured with the use of the 239.Dv MCLGET 240macro. 241Each cluster is 242.Dv MCLBYTES 243in size, where MCLBYTES is a machine-dependent constant. 244The system defines an advisory macro 245.Dv MINCLSIZE , 246which is the smallest amount of data to put into a cluster. 247It's equal to the sum of 248.Dv MLEN 249and 250.Dv MHLEN . 251It is typically preferable to store data into an mbuf's data region, if size 252permits, as opposed to allocating a separate mbuf cluster to hold the same 253data. 254.\" 255.Ss Macros and Functions 256There are numerous predefined macros and functions that provide the 257developer with common utilities. 258.\" 259.Bl -ohang -offset indent 260.It Fn mtod mbuf type 261Convert an mbuf pointer to a data pointer. 262The macro expands to the data pointer cast to the pointer of the specified type. 263.Sy Note : 264It is advisable to ensure that there is enough contiguous data in the mbuf. 265See 266.Fn m_pullup 267for details. 268.It Fn MGET mbuf how type 269Allocate an mbuf and initialize it to contain internal data. 270.Fa mbuf 271will point to the allocated mbuf on success, or be set to 272.Dv NULL 273on failure. 274The 275.Fa how 276argument is to be set to 277.Dv M_TRYWAIT 278or 279.Dv M_DONTWAIT . 280It specifies whether the caller is willing to block if necessary. 281If 282.Fa how 283is set to 284.Dv M_TRYWAIT , 285a failed allocation will result in the caller being put 286to sleep for a designated 287kern.ipc.mbuf_wait 288.Xr ( sysctl 8 289tunable) 290number of ticks. 291A number of other mbuf-related 292functions and macros have the same argument because they may 293at some point need to allocate new mbufs. 294.It Fn MGETHDR mbuf how type 295Allocate an mbuf and initialize it to contain a packet header 296and internal data. 297See 298.Fn MGET 299for details. 300.It Fn MCLGET mbuf how 301Allocate and attach an mbuf cluster to an mbuf. 302If the macro fails, the 303.Dv M_EXT 304flag won't be set in the mbuf. 305.It Fn M_PREPEND mbuf len how 306This macro operates on an mbuf chain. 307It is an optimized wrapper for 308.Fn m_prepend 309that can make use of possible empty space before data 310(e.g. left after trimming of a link-layer header). 311The new chain pointer or 312.Dv NULL 313is in 314.Fa mbuf 315after the call. 316.It Fn M_WRITABLE mbuf 317This macro will evaluate true if the mbuf is not marked 318.Dv M_RDONLY 319and if either the mbuf does not contain external storage or, 320if it does, 321then if the reference count of the storage is not greater than 1. 322The 323.Dv M_RDONLY 324flag can be set in the mbuf's 325.Dv m_flags . 326This can be achieved during setup of the external storage, 327by passing the 328.Dv M_RDONLY 329bit as a 330.Fa flags 331argument to the 332.Fn MEXTADD 333macro, or can be directly set in individual mbufs. 334.El 335.Pp 336The functions are: 337.Bl -ohang -offset indent 338.It Fn m_get how type 339A function version of 340.Fn MGET 341for non-critical paths. 342.It Fn m_getm orig len how type 343Allocate 344.Fa len 345bytes worth of mbufs and mbuf clusters if necessary and append the resulting 346allocated chain to the 347.Fa orig 348mbuf chain, if it is 349.No non- Ns Dv NULL . 350If the allocation fails at any point, 351free whatever was allocated and return 352.Dv NULL . 353If 354.Fa orig 355is 356.No non- Ns Dv NULL , 357it will not be freed. 358It is possible to use 359.Fn m_getm 360to either append 361.Fa len 362bytes to an existing mbuf or mbuf chain 363(for example, one which may be sitting in a pre-allocated ring) 364or to simply perform an all-or-nothing mbuf and mbuf cluster allocation. 365.It Fn m_gethdr how type 366A function version of 367.Fn MGETHDR 368for non-critical paths. 369.It Fn m_getclr how type 370Allocate an mbuf and zero out the data region. 371.El 372.Pp 373The functions below operate on mbuf chains. 374.Bl -ohang -offset indent 375.It Fn m_freem mbuf 376Free an entire mbuf chain, including any external 377storage. 378.\" 379.It Fn m_adj mbuf len 380Trim 381.Fa len 382bytes from the head of an mbuf chain if 383.Fa len 384is positive, from the tail otherwise. 385.\" 386.It Fn m_prepend mbuf len how 387Allocate a new mbuf and prepend it to the chain, handle 388.Dv M_PKTHDR 389properly. 390.Sy Note : 391It doesn't allocate any clusters, so 392.Fa len 393must be less than 394.Dv MLEN 395or 396.Dv MHLEN , 397depending on the 398.Dv M_PKTHDR 399flag setting. 400.\" 401.It Fn m_pullup mbuf len 402Arrange that the first 403.Fa len 404bytes of an mbuf chain are contiguous and lay in the data area of 405.Fa mbuf , 406so they are accessible with 407.Fn mtod mbuf type . 408Return the new chain on success, 409.Dv NULL 410on failure 411(the chain is freed in this case). 412.Sy Note : 413It doesn't allocate any clusters, so 414.Fa len 415must be less than 416.Dv MHLEN . 417.\" 418.It Fn m_copym mbuf offset len how 419Make a copy of an mbuf chain starting 420.Fa offset 421bytes from the beginning, continuing for 422.Fa len 423bytes. 424If 425.Fa len 426is 427.Dv M_COPYALL , 428copy to the end of the mbuf chain. 429.Sy Note : 430The copy is read-only, because clusters are not 431copied, only their reference counts are incremented. 432.\" 433.It Fn m_copypacket mbuf how 434Copy an entire packet including header, which must be present. 435This is an optimized version of the common case 436.Fn m_copym mbuf 0 M_COPYALL how . 437.Sy Note : 438the copy is read-only, because clusters are not 439copied, only their reference counts are incremented. 440.\" 441.It Fn m_dup mbuf how 442Copy a packet header mbuf chain into a completely new chain, including 443copying any mbuf clusters. 444Use this instead of 445.Fn m_copypacket 446when you need a writable copy of an mbuf chain. 447.\" 448.It Fn m_copydata mbuf offset len buf 449Copy data from an mbuf chain starting 450.Fa off 451bytes from the beginning, continuing for 452.Fa len 453bytes, into the indicated buffer 454.Fa buf . 455.\" 456.It Fn m_copyback mbuf offset len buf 457Copy 458.Fa len 459bytes from the buffer 460.Fa buf 461back into the indicated mbuf chain, 462starting at 463.Fa offset 464bytes from the beginning of the chain, extending the mbuf chain if necessary. 465.Sy Note : 466It doesn't allocate any clusters, just adds mbufs to the chain. 467It's safe to set 468.Fa offset 469beyond the current chain end: zeroed mbufs will be allocated to fill the 470space. 471.\" 472.It Fn m_devget buf len offset ifp copy 473Copy data from a device local memory pointed to by 474.Fa buf 475to an mbuf chain. 476The copy is done using a specified copy routine 477.Fa copy , 478or 479.Fn bcopy 480if 481.Fa copy 482is 483.Dv NULL . 484.\" 485.It Fn m_cat m n 486Concatenate 487.Fa n 488to 489.Fa m . 490Both chains must be of the same type. 491.Fa N 492is still valid after the function returned. 493.Sy Note : 494It does not handle 495.Dv M_PKTHDR 496and friends. 497.\" 498.It Fn m_split mbuf len how 499Partition an mbuf chain in two pieces, returning the tail: 500all but the first 501.Fa len 502bytes. 503In case of failure, it returns 504.Dv NULL 505and attempts to restore the chain to its original state. 506.El 507.Sh RETURN VALUES 508See above. 509.Sh HISTORY 510.\" Please correct me if I'm wrong 511Mbufs appeared in an early version of 512.Bx . 513Besides for being used for network packets, they were used 514to store various dynamic structures, such as routing table 515entries, interface addresses, protocol control blocks, etc. 516.Sh AUTHORS 517The original 518.Nm 519man page was written by Yar Tikhiy. 520