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