1de06f907SNik Clayton.\" Copyright (c) 2000 FreeBSD Inc. 2de06f907SNik Clayton.\" All rights reserved. 3de06f907SNik Clayton.\" 4de06f907SNik Clayton.\" Redistribution and use in source and binary forms, with or without 5de06f907SNik Clayton.\" modification, are permitted provided that the following conditions 6de06f907SNik Clayton.\" are met: 7de06f907SNik Clayton.\" 1. Redistributions of source code must retain the above copyright 8de06f907SNik Clayton.\" notice, this list of conditions and the following disclaimer. 9de06f907SNik Clayton.\" 2. Redistributions in binary form must reproduce the above copyright 10de06f907SNik Clayton.\" notice, this list of conditions and the following disclaimer in the 11de06f907SNik Clayton.\" documentation and/or other materials provided with the distribution. 12de06f907SNik Clayton.\" 13de06f907SNik Clayton.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14de06f907SNik Clayton.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15de06f907SNik Clayton.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16de06f907SNik Clayton.\" ARE DISCLAIMED. IN NO EVENT SHALL [your name] OR CONTRIBUTORS BE LIABLE 17de06f907SNik Clayton.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18de06f907SNik Clayton.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19de06f907SNik Clayton.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20de06f907SNik Clayton.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21de06f907SNik Clayton.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22de06f907SNik Clayton.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23de06f907SNik Clayton.\" SUCH DAMAGE. 24de06f907SNik Clayton.\" 25de06f907SNik Clayton.\" $FreeBSD$ 26de06f907SNik Clayton.\" 27de06f907SNik Clayton.Dd October 17, 2000 28de06f907SNik Clayton.Dt MBUF 9 29de06f907SNik Clayton.Os 30de06f907SNik Clayton.\" 31de06f907SNik Clayton.Sh NAME 32de06f907SNik Clayton.Nm mbuf 33de06f907SNik Clayton.Nd "memory management in the kernel IPC subsystem" 34de06f907SNik Clayton.\" 35de06f907SNik Clayton.Sh SYNOPSIS 3632eef9aeSRuslan Ermilov.In sys/param.h 37f16b3c0dSChad David.In sys/systm.h 3832eef9aeSRuslan Ermilov.In sys/mbuf.h 39de06f907SNik Clayton.\" 40bfd59721SBosko Milekic.Ss Mbuf allocation macros 41de06f907SNik Clayton.Fn MGET "struct mbuf *mbuf" "int how" "short type" 42de06f907SNik Clayton.Fn MGETHDR "struct mbuf *mbuf" "int how" "short type" 43de06f907SNik Clayton.Fn MCLGET "struct mbuf *mbuf" "int how" 44bfd59721SBosko Milekic.Fo MEXTADD 45bfd59721SBosko Milekic.Fa "struct mbuf *mbuf" 46bfd59721SBosko Milekic.Fa "caddr_t buf" 47bfd59721SBosko Milekic.Fa "u_int size" 48bfd59721SBosko Milekic.Fa "void (*free)(void *opt_args)" 49bfd59721SBosko Milekic.Fa "void *opt_args" 505d4050ccSBosko Milekic.Fa "short flags" 515d4050ccSBosko Milekic.Fa "int type" 52bfd59721SBosko Milekic.Fc 53bfd59721SBosko Milekic.Fn MEXTFREE "struct mbuf *mbuf" 54bfd59721SBosko Milekic.Fn MEXT_ADD_REF "struct mbuf *mbuf" 55bfd59721SBosko Milekic.Fn MEXT_REM_REF "struct mbuf *mbuf" 56bfd59721SBosko Milekic.Fn MFREE "struct mbuf *mbuf" "struct mbuf *successor" 57de06f907SNik Clayton.\" 58bfd59721SBosko Milekic.Ss Mbuf utility macros 59f16b3c0dSChad David.Fn mtod "struct mbuf *mbuf" "type" 60f16b3c0dSChad David.Ft int 61bfd59721SBosko Milekic.Fn MEXT_IS_REF "struct mbuf *mbuf" 62bfd59721SBosko Milekic.Fn M_ALIGN "struct mbuf *mbuf" "u_int len" 63bfd59721SBosko Milekic.Fn MH_ALIGN "struct mbuf *mbuf" "u_int len" 64f16b3c0dSChad David.Ft int 65bfd59721SBosko Milekic.Fn M_LEADINGSPACE "struct mbuf *mbuf" 66f16b3c0dSChad David.Ft int 67bfd59721SBosko Milekic.Fn M_TRAILINGSPACE "struct mbuf *mbuf" 68da72b998SYaroslav Tykhiy.Fn M_MOVE_PKTHDR "struct mbuf *to" "struct mbuf *from" 69bfd59721SBosko Milekic.Fn M_PREPEND "struct mbuf *mbuf" "int len" "int how" 70bfd59721SBosko Milekic.Fn MCHTYPE "struct mbuf *mbuf" "u_int type" 71f16b3c0dSChad David.Ft int 725d4050ccSBosko Milekic.Fn M_WRITABLE "struct mbuf *mbuf" 73bfd59721SBosko Milekic.\" 74bfd59721SBosko Milekic.Ss Mbuf allocation functions 75de06f907SNik Clayton.Ft struct mbuf * 76de06f907SNik Clayton.Fn m_get "int how" "int type" 77de06f907SNik Clayton.Ft struct mbuf * 78328c0bbcSBosko Milekic.Fn m_getm "struct mbuf *orig" "int len" "int how" "int type" 79328c0bbcSBosko Milekic.Ft struct mbuf * 806c013706SMaxim Konovalov.Fn m_getcl "int how" "short type" "int flags" 816c013706SMaxim Konovalov.Ft struct mbuf * 82de06f907SNik Clayton.Fn m_getclr "int how" "int type" 83de06f907SNik Clayton.Ft struct mbuf * 84de06f907SNik Clayton.Fn m_gethdr "int how" "int type" 85bfd59721SBosko Milekic.Ft struct mbuf * 86bfd59721SBosko Milekic.Fn m_free "struct mbuf *mbuf" 87de06f907SNik Clayton.Ft void 88de06f907SNik Clayton.Fn m_freem "struct mbuf *mbuf" 89bfd59721SBosko Milekic.\" 90bfd59721SBosko Milekic.Ss Mbuf utility functions 91de06f907SNik Clayton.Ft void 92de06f907SNik Clayton.Fn m_adj "struct mbuf *mbuf" "int len" 93de06f907SNik Clayton.Ft struct mbuf * 94de06f907SNik Clayton.Fn m_prepend "struct mbuf *mbuf" "int len" "int how" 95de06f907SNik Clayton.Ft struct mbuf * 96de06f907SNik Clayton.Fn m_pullup "struct mbuf *mbuf" "int len" 97de06f907SNik Clayton.Ft struct mbuf * 98de06f907SNik Clayton.Fn m_copym "struct mbuf *mbuf" "int offset" "int len" "int how" 99de06f907SNik Clayton.Ft struct mbuf * 100de06f907SNik Clayton.Fn m_copypacket "struct mbuf *mbuf" "int how" 101de06f907SNik Clayton.Ft struct mbuf * 102de06f907SNik Clayton.Fn m_dup "struct mbuf *mbuf" "int how" 103de06f907SNik Clayton.Ft void 1040d8e4c57SBruce Evans.Fn m_copydata "const struct mbuf *mbuf" "int offset" "int len" "caddr_t buf" 105de06f907SNik Clayton.Ft void 106de06f907SNik Clayton.Fn m_copyback "struct mbuf *mbuf" "int offset" "int len" "caddr_t buf" 107de06f907SNik Clayton.Ft struct mbuf * 108de06f907SNik Clayton.Fo m_devget 109de06f907SNik Clayton.Fa "char *buf" 110de06f907SNik Clayton.Fa "int len" 111de06f907SNik Clayton.Fa "int offset" 112de06f907SNik Clayton.Fa "struct ifnet *ifp" 113de06f907SNik Clayton.Fa "void (*copy)(char *from, caddr_t to, u_int len)" 114de06f907SNik Clayton.Fc 115de06f907SNik Clayton.Ft void 116de06f907SNik Clayton.Fn m_cat "struct mbuf *m" "struct mbuf *n" 117e60fa837SRuslan Ermilov.Ft u_int 1180b1c0994SPoul-Henning Kamp.Fn m_fixhdr "struct mbuf *mbuf" 119da72b998SYaroslav Tykhiy.Ft void 120da72b998SYaroslav Tykhiy.Fn m_dup_pkthdr "struct mbuf *to" "struct mbuf *from" 121da72b998SYaroslav Tykhiy.Ft void 122da72b998SYaroslav Tykhiy.Fn m_move_pkthdr "struct mbuf *to" "struct mbuf *from" 123e60fa837SRuslan Ermilov.Ft u_int 1240b1c0994SPoul-Henning Kamp.Fn m_length "struct mbuf *mbuf" "struct mbuf **last" 125de06f907SNik Clayton.Ft struct mbuf * 126de06f907SNik Clayton.Fn m_split "struct mbuf *mbuf" "int len" "int how" 12702312219SBruce M Simpson.Ft int 12802312219SBruce M Simpson.Fn m_apply "struct mbuf *mbuf" "int off" "int len" "int (*f)(void *arg, void *data, u_int len)" "void *arg" 12902312219SBruce M Simpson.Ft struct mbuf * 13002312219SBruce M Simpson.Fn m_getptr "struct mbuf *mbuf" "int loc" "int *off" 131a61bc764SMike Silbersack.Ft struct mbuf * 132a61bc764SMike Silbersack.Fn m_defrag "struct mbuf *m0" "int how" 133de06f907SNik Clayton.\" 134de06f907SNik Clayton.Sh DESCRIPTION 1357b2fd183SYaroslav TykhiyAn 1367b2fd183SYaroslav Tykhiy.Vt mbuf 1377b2fd183SYaroslav Tykhiyis a basic unit of memory management in the kernel IPC subsystem. 1387b2fd183SYaroslav TykhiyNetwork packets and socket buffers are stored in 1397b2fd183SYaroslav Tykhiy.Vt mbufs . 1407b2fd183SYaroslav TykhiyA network packet may span multiple 1417b2fd183SYaroslav Tykhiy.Vt mbufs 1427b2fd183SYaroslav Tykhiyarranged into a 1437b2fd183SYaroslav Tykhiy.Vt mbuf chain 144c4d9468eSRuslan Ermilov(linked list), 145de06f907SNik Claytonwhich allows adding or trimming 146bfd59721SBosko Milekicnetwork headers with little overhead. 147de06f907SNik Clayton.Pp 1487b2fd183SYaroslav TykhiyWhile a developer should not bother with 1497b2fd183SYaroslav Tykhiy.Vt mbuf 1507b2fd183SYaroslav Tykhiyinternals without serious 151de06f907SNik Claytonreason in order to avoid incompatibilities with future changes, it 1527b2fd183SYaroslav Tykhiyis useful to understand the general structure of an 1537b2fd183SYaroslav Tykhiy.Vt mbuf . 154de06f907SNik Clayton.Pp 1557b2fd183SYaroslav TykhiyAn 1567b2fd183SYaroslav Tykhiy.Vt mbuf 1577b2fd183SYaroslav Tykhiyconsists of a variable-sized header and a small internal 158e73145f6SSheldon Hearnbuffer for data. 1597b2fd183SYaroslav TykhiyThe total size of an 1607b2fd183SYaroslav Tykhiy.Vt mbuf , 161bfd59721SBosko Milekic.Dv MSIZE , 162feccf50cSAlex Dupreis a constant defined in 163feccf50cSAlex Dupre.In sys/param.h . 1647b2fd183SYaroslav TykhiyThe 1657b2fd183SYaroslav Tykhiy.Vt mbuf 1667b2fd183SYaroslav Tykhiyheader includes: 167de06f907SNik Clayton.Pp 168238d9c59SYaroslav Tykhiy.Bl -tag -width "m_nextpkt" -offset indent 169be82a0bcSYaroslav Tykhiy.It Va m_next 170238d9c59SYaroslav Tykhiy.Pq Vt struct mbuf * 171238d9c59SYaroslav TykhiyA pointer to the next 1727b2fd183SYaroslav Tykhiy.Vt mbuf 1737b2fd183SYaroslav Tykhiyin the 174238d9c59SYaroslav Tykhiy.Vt mbuf chain . 175be82a0bcSYaroslav Tykhiy.It Va m_nextpkt 176238d9c59SYaroslav Tykhiy.Pq Vt struct mbuf * 177238d9c59SYaroslav TykhiyA pointer to the next 1787b2fd183SYaroslav Tykhiy.Vt mbuf chain 179238d9c59SYaroslav Tykhiyin the queue. 180be82a0bcSYaroslav Tykhiy.It Va m_data 181238d9c59SYaroslav Tykhiy.Pq Vt caddr_t 182238d9c59SYaroslav TykhiyA pointer to data attached to this 183238d9c59SYaroslav Tykhiy.Vt mbuf . 184be82a0bcSYaroslav Tykhiy.It Va m_len 185238d9c59SYaroslav Tykhiy.Pq Vt int 186238d9c59SYaroslav TykhiyThe length of the data. 187be82a0bcSYaroslav Tykhiy.It Va m_type 188238d9c59SYaroslav Tykhiy.Pq Vt short 189238d9c59SYaroslav TykhiyThe type of the data. 190be82a0bcSYaroslav Tykhiy.It Va m_flags 191238d9c59SYaroslav Tykhiy.Pq Vt int 192238d9c59SYaroslav TykhiyThe 1937b2fd183SYaroslav Tykhiy.Vt mbuf 194238d9c59SYaroslav Tykhiyflags. 195de06f907SNik Clayton.El 196de06f907SNik Clayton.Pp 1977b2fd183SYaroslav TykhiyThe 1987b2fd183SYaroslav Tykhiy.Vt mbuf 1997b2fd183SYaroslav Tykhiyflag bits are defined as follows: 200de06f907SNik Clayton.Bd -literal 201de06f907SNik Clayton/* mbuf flags */ 202de06f907SNik Clayton#define M_EXT 0x0001 /* has associated external storage */ 203de06f907SNik Clayton#define M_PKTHDR 0x0002 /* start of record */ 204de06f907SNik Clayton#define M_EOR 0x0004 /* end of record */ 2055d4050ccSBosko Milekic#define M_RDONLY 0x0008 /* associated data marked read-only */ 2065d4050ccSBosko Milekic#define M_PROTO1 0x0010 /* protocol-specific */ 2075d4050ccSBosko Milekic#define M_PROTO2 0x0020 /* protocol-specific */ 2085d4050ccSBosko Milekic#define M_PROTO3 0x0040 /* protocol-specific */ 2095d4050ccSBosko Milekic#define M_PROTO4 0x0080 /* protocol-specific */ 2105d4050ccSBosko Milekic#define M_PROTO5 0x0100 /* protocol-specific */ 211f434362aSYaroslav Tykhiy#define M_PROTO6 0x4000 /* protocol-specific (avoid M_BCAST conflict) */ 212f434362aSYaroslav Tykhiy#define M_FREELIST 0x8000 /* mbuf is on the free list */ 213de06f907SNik Clayton 214f434362aSYaroslav Tykhiy/* mbuf pkthdr flags (also stored in m_flags) */ 2155d4050ccSBosko Milekic#define M_BCAST 0x0200 /* send/received as link-level broadcast */ 2165d4050ccSBosko Milekic#define M_MCAST 0x0400 /* send/received as link-level multicast */ 2175d4050ccSBosko Milekic#define M_FRAG 0x0800 /* packet is fragment of larger packet */ 2185d4050ccSBosko Milekic#define M_FIRSTFRAG 0x1000 /* packet is first fragment */ 2195d4050ccSBosko Milekic#define M_LASTFRAG 0x2000 /* packet is last fragment */ 220de06f907SNik Clayton.Ed 221de06f907SNik Clayton.Pp 2227b2fd183SYaroslav TykhiyThe available 2237b2fd183SYaroslav Tykhiy.Vt mbuf 2247b2fd183SYaroslav Tykhiytypes are defined as follows: 225de06f907SNik Clayton.Bd -literal 226de06f907SNik Clayton/* mbuf types */ 227de06f907SNik Clayton#define MT_DATA 1 /* dynamic (data) allocation */ 228de06f907SNik Clayton#define MT_HEADER 2 /* packet header */ 229de06f907SNik Clayton#define MT_SONAME 8 /* socket name */ 230de06f907SNik Clayton#define MT_FTABLE 11 /* fragment reassembly header */ 231d2b73059SYaroslav Tykhiy#define MT_TAG 13 /* volatile metadata associated to pkts */ 232de06f907SNik Clayton#define MT_CONTROL 14 /* extra-data protocol message */ 233de06f907SNik Clayton#define MT_OOBDATA 15 /* expedited data */ 234de06f907SNik Clayton.Ed 235de06f907SNik Clayton.Pp 236de06f907SNik ClaytonIf the 237de06f907SNik Clayton.Dv M_PKTHDR 238de06f907SNik Claytonflag is set, a 2397b2fd183SYaroslav Tykhiy.Vt struct pkthdr Va m_pkthdr 2407b2fd183SYaroslav Tykhiyis added to the 2417b2fd183SYaroslav Tykhiy.Vt mbuf 2427b2fd183SYaroslav Tykhiyheader. 243e73145f6SSheldon HearnIt contains a pointer to the interface 244de06f907SNik Claytonthe packet has been received from 2457b2fd183SYaroslav Tykhiy.Pq Vt struct ifnet Va *rcvif , 246de06f907SNik Claytonand the total packet length 2477b2fd183SYaroslav Tykhiy.Pq Vt int Va len . 2480193eb5eSYaroslav TykhiyFields used in offloading checksum calculation to the hardware are kept in 2490193eb5eSYaroslav Tykhiy.Va m_pkthdr 2500193eb5eSYaroslav Tykhiyas well. 2510193eb5eSYaroslav TykhiySee 2520193eb5eSYaroslav Tykhiy.Sx HARDWARE-ASSISTED CHECKSUM CALCULATION 2530193eb5eSYaroslav Tykhiyfor details. 254de06f907SNik Clayton.Pp 2557b2fd183SYaroslav TykhiyIf small enough, data is stored in the internal data buffer of an 2567b2fd183SYaroslav Tykhiy.Vt mbuf . 2577b2fd183SYaroslav TykhiyIf the data is sufficiently large, another 2587b2fd183SYaroslav Tykhiy.Vt mbuf 2597b2fd183SYaroslav Tykhiymay be added to the 2607b2fd183SYaroslav Tykhiy.Vt mbuf chain , 2617b2fd183SYaroslav Tykhiyor external storage may be associated with the 2627b2fd183SYaroslav Tykhiy.Vt mbuf . 263de06f907SNik Clayton.Dv MHLEN 2647b2fd183SYaroslav Tykhiybytes of data can fit into an 2657b2fd183SYaroslav Tykhiy.Vt mbuf 2667b2fd183SYaroslav Tykhiywith the 267de06f907SNik Clayton.Dv M_PKTHDR 268de06f907SNik Claytonflag set, 269de06f907SNik Clayton.Dv MLEN 270de06f907SNik Claytonbytes can otherwise. 271de06f907SNik Clayton.Pp 2727b2fd183SYaroslav TykhiyIf external storage is being associated with an 2737b2fd183SYaroslav Tykhiy.Vt mbuf , 2747b2fd183SYaroslav Tykhiythe 275be82a0bcSYaroslav Tykhiy.Va m_ext 27625f1a2e9SSheldon Hearnheader is added at the cost of losing the internal data buffer. 277e73145f6SSheldon HearnIt includes a pointer to external storage, the size of the storage, 278e73145f6SSheldon Hearna pointer to a function used for freeing the storage, 279e73145f6SSheldon Hearna pointer to an optional argument that can be passed to the function, 280e73145f6SSheldon Hearnand a pointer to a reference counter. 2817b2fd183SYaroslav TykhiyAn 2827b2fd183SYaroslav Tykhiy.Vt mbuf 2837b2fd183SYaroslav Tykhiyusing external storage has the 284de06f907SNik Clayton.Dv M_EXT 285de06f907SNik Claytonflag set. 286de06f907SNik Clayton.Pp 287bfd59721SBosko MilekicThe system supplies a macro for allocating the desired external storage 288bfd59721SBosko Milekicbuffer, 289bfd59721SBosko Milekic.Dv MEXTADD . 290bfd59721SBosko Milekic.Pp 291bfd59721SBosko MilekicThe allocation and management of the reference counter is handled by the 292e73145f6SSheldon Hearnsubsystem. 293e73145f6SSheldon HearnThe developer can check whether the reference count for the 2947b2fd183SYaroslav Tykhiyexternal storage of a given 2957b2fd183SYaroslav Tykhiy.Vt mbuf 2967b2fd183SYaroslav Tykhiyis greater than 1 with the 297bfd59721SBosko Milekic.Dv MEXT_IS_REF 298e73145f6SSheldon Hearnmacro. 299e73145f6SSheldon HearnSimilarly, the developer can directly add and remove references, 300e73145f6SSheldon Hearnif absolutely necessary, with the use of the 301bfd59721SBosko Milekic.Dv MEXT_ADD_REF 302bfd59721SBosko Milekicand 303bfd59721SBosko Milekic.Dv MEXT_REM_REF 304bfd59721SBosko Milekicmacros. 305bfd59721SBosko Milekic.Pp 306bfd59721SBosko MilekicThe system also supplies a default type of external storage buffer called an 3077b2fd183SYaroslav Tykhiy.Vt mbuf cluster . 3087b2fd183SYaroslav Tykhiy.Vt Mbuf clusters 3097b2fd183SYaroslav Tykhiycan be allocated and configured with the use of the 310bfd59721SBosko Milekic.Dv MCLGET 311e73145f6SSheldon Hearnmacro. 3127b2fd183SYaroslav TykhiyEach 3137b2fd183SYaroslav Tykhiy.Vt mbuf cluster 3147b2fd183SYaroslav Tykhiyis 315de06f907SNik Clayton.Dv MCLBYTES 316bfd59721SBosko Milekicin size, where MCLBYTES is a machine-dependent constant. 317bfd59721SBosko MilekicThe system defines an advisory macro 318de06f907SNik Clayton.Dv MINCLSIZE , 3197b2fd183SYaroslav Tykhiywhich is the smallest amount of data to put into an 3207b2fd183SYaroslav Tykhiy.Vt mbuf cluster . 321de06f907SNik ClaytonIt's equal to the sum of 322de06f907SNik Clayton.Dv MLEN 323de06f907SNik Claytonand 324de06f907SNik Clayton.Dv MHLEN . 3257b2fd183SYaroslav TykhiyIt is typically preferable to store data into the data region of an 3267b2fd183SYaroslav Tykhiy.Vt mbuf , 3277b2fd183SYaroslav Tykhiyif size permits, as opposed to allocating a separate 3287b2fd183SYaroslav Tykhiy.Vt mbuf cluster 3297b2fd183SYaroslav Tykhiyto hold the same data. 330de06f907SNik Clayton.\" 331de06f907SNik Clayton.Ss Macros and Functions 332bfd59721SBosko MilekicThere are numerous predefined macros and functions that provide the 333bfd59721SBosko Milekicdeveloper with common utilities. 334de06f907SNik Clayton.\" 335de06f907SNik Clayton.Bl -ohang -offset indent 336de06f907SNik Clayton.It Fn mtod mbuf type 3377b2fd183SYaroslav TykhiyConvert an 3387b2fd183SYaroslav Tykhiy.Fa mbuf 3397b2fd183SYaroslav Tykhiypointer to a data pointer. 3407b2fd183SYaroslav TykhiyThe macro expands to the data pointer cast to the pointer of the specified 3417b2fd183SYaroslav Tykhiy.Fa type . 342de06f907SNik Clayton.Sy Note : 3437b2fd183SYaroslav TykhiyIt is advisable to ensure that there is enough contiguous data in 3447b2fd183SYaroslav Tykhiy.Fa mbuf . 345de06f907SNik ClaytonSee 346de06f907SNik Clayton.Fn m_pullup 347de06f907SNik Claytonfor details. 348de06f907SNik Clayton.It Fn MGET mbuf how type 3497b2fd183SYaroslav TykhiyAllocate an 3507b2fd183SYaroslav Tykhiy.Vt mbuf 3517b2fd183SYaroslav Tykhiyand initialize it to contain internal data. 35225f1a2e9SSheldon Hearn.Fa mbuf 3537b2fd183SYaroslav Tykhiywill point to the allocated 3547b2fd183SYaroslav Tykhiy.Vt mbuf 3557b2fd183SYaroslav Tykhiyon success, or be set to 356de06f907SNik Clayton.Dv NULL 357e73145f6SSheldon Hearnon failure. 358e73145f6SSheldon HearnThe 359de06f907SNik Clayton.Fa how 360de06f907SNik Claytonargument is to be set to 361c65b53c5STom Rhodes.Dv M_TRYWAIT 362de06f907SNik Claytonor 363c65b53c5STom Rhodes.Dv M_DONTWAIT . 36425f1a2e9SSheldon HearnIt specifies whether the caller is willing to block if necessary. 365e73145f6SSheldon HearnIf 366de06f907SNik Clayton.Fa how 36725f1a2e9SSheldon Hearnis set to 368c65b53c5STom Rhodes.Dv M_TRYWAIT , 36925f1a2e9SSheldon Hearna failed allocation will result in the caller being put 370bfd59721SBosko Milekicto sleep for a designated 37125f1a2e9SSheldon Hearnkern.ipc.mbuf_wait 372c4d9468eSRuslan Ermilov.Xr ( sysctl 8 373c4d9468eSRuslan Ermilovtunable) 37425f1a2e9SSheldon Hearnnumber of ticks. 3757b2fd183SYaroslav TykhiyA number of other functions and macros related to 3767b2fd183SYaroslav Tykhiy.Vt mbufs 3777b2fd183SYaroslav Tykhiyhave the same argument because they may 3787b2fd183SYaroslav Tykhiyat some point need to allocate new 3797b2fd183SYaroslav Tykhiy.Vt mbufs . 380c65b53c5STom Rhodes.Pp 3817b2fd183SYaroslav TykhiyProgrammers should be careful not to confuse the 3827b2fd183SYaroslav Tykhiy.Vt mbuf 3837b2fd183SYaroslav Tykhiyallocation flag 384c65b53c5STom Rhodes.Dv M_DONTWAIT 385c65b53c5STom Rhodeswith the 386c65b53c5STom Rhodes.Xr malloc 9 387c65b53c5STom Rhodesallocation flag, 388c65b53c5STom Rhodes.Dv M_NOWAIT . 389c65b53c5STom RhodesThey are not the same. 390de06f907SNik Clayton.It Fn MGETHDR mbuf how type 3917b2fd183SYaroslav TykhiyAllocate an 3927b2fd183SYaroslav Tykhiy.Vt mbuf 3937b2fd183SYaroslav Tykhiyand initialize it to contain a packet header 394e73145f6SSheldon Hearnand internal data. 395e73145f6SSheldon HearnSee 396de06f907SNik Clayton.Fn MGET 397de06f907SNik Claytonfor details. 398de06f907SNik Clayton.It Fn MCLGET mbuf how 3997b2fd183SYaroslav TykhiyAllocate and attach an 4007b2fd183SYaroslav Tykhiy.Vt mbuf cluster 4017b2fd183SYaroslav Tykhiyto 4027b2fd183SYaroslav Tykhiy.Fa mbuf . 403e73145f6SSheldon HearnIf the macro fails, the 404de06f907SNik Clayton.Dv M_EXT 4057b2fd183SYaroslav Tykhiyflag won't be set in 4067b2fd183SYaroslav Tykhiy.Fa mbuf . 407da72b998SYaroslav Tykhiy.It Fn M_ALIGN mbuf len 408da72b998SYaroslav TykhiySet the pointer 409da72b998SYaroslav Tykhiy.Fa mbuf->m_data 410da72b998SYaroslav Tykhiyto place an object of the size 411da72b998SYaroslav Tykhiy.Fa len 412da72b998SYaroslav Tykhiyat the end of the internal data area of 413da72b998SYaroslav Tykhiy.Fa mbuf , 414da72b998SYaroslav Tykhiylong word aligned. 415da72b998SYaroslav TykhiyApplicable only if 416da72b998SYaroslav Tykhiy.Fa mbuf 417da72b998SYaroslav Tykhiyis newly allocated with 418da72b998SYaroslav Tykhiy.Fn MGET 419da72b998SYaroslav Tykhiyor 420da72b998SYaroslav Tykhiy.Fn m_get . 421da72b998SYaroslav Tykhiy.It Fn MH_ALIGN mbuf len 422da72b998SYaroslav TykhiyServes the same purpose as 423da72b998SYaroslav Tykhiy.Fn M_ALIGN 424da72b998SYaroslav Tykhiydoes, but only for 425da72b998SYaroslav Tykhiy.Fa mbuf 426da72b998SYaroslav Tykhiynewly allocated with 427da72b998SYaroslav Tykhiy.Fn MGETHDR 428da72b998SYaroslav Tykhiyor 429da72b998SYaroslav Tykhiy.Fn m_gethdr , 430da72b998SYaroslav Tykhiyor initialized by 431da72b998SYaroslav Tykhiy.Fn m_dup_pkthdr 432da72b998SYaroslav Tykhiyor 433da72b998SYaroslav Tykhiy.Fn m_move_pkthdr . 434da72b998SYaroslav Tykhiy.It Fn M_LEADINGSPACE mbuf 435da72b998SYaroslav TykhiyReturns the number of bytes available before the beginning 436da72b998SYaroslav Tykhiyof data in 437da72b998SYaroslav Tykhiy.Fa mbuf . 438da72b998SYaroslav Tykhiy.It Fn M_TRAILINGSPACE mbuf 439da72b998SYaroslav TykhiyReturns the number of bytes available after the end of data in 440da72b998SYaroslav Tykhiy.Fa mbuf . 441de06f907SNik Clayton.It Fn M_PREPEND mbuf len how 4427b2fd183SYaroslav TykhiyThis macro operates on an 4437b2fd183SYaroslav Tykhiy.Vt mbuf chain . 444de06f907SNik ClaytonIt is an optimized wrapper for 445de06f907SNik Clayton.Fn m_prepend 446de06f907SNik Claytonthat can make use of possible empty space before data 447c4d9468eSRuslan Ermilov(e.g. left after trimming of a link-layer header). 4487b2fd183SYaroslav TykhiyThe new 4497b2fd183SYaroslav Tykhiy.Vt mbuf chain 4507b2fd183SYaroslav Tykhiypointer or 451de06f907SNik Clayton.Dv NULL 452de06f907SNik Claytonis in 453de06f907SNik Clayton.Fa mbuf 454de06f907SNik Claytonafter the call. 455da72b998SYaroslav Tykhiy.It Fn M_MOVE_PKTHDR to from 456da72b998SYaroslav TykhiyUsing this macro is equivalent to calling 457da72b998SYaroslav Tykhiy.Fn m_move_pkthdr to from . 4585d4050ccSBosko Milekic.It Fn M_WRITABLE mbuf 4597b2fd183SYaroslav TykhiyThis macro will evaluate true if 4607b2fd183SYaroslav Tykhiy.Fa mbuf 4617b2fd183SYaroslav Tykhiyis not marked 4625d4050ccSBosko Milekic.Dv M_RDONLY 4637b2fd183SYaroslav Tykhiyand if either 4647b2fd183SYaroslav Tykhiy.Fa mbuf 4657b2fd183SYaroslav Tykhiydoes not contain external storage or, 4665d4050ccSBosko Milekicif it does, 4675d4050ccSBosko Milekicthen if the reference count of the storage is not greater than 1. 4685d4050ccSBosko MilekicThe 4695d4050ccSBosko Milekic.Dv M_RDONLY 4707b2fd183SYaroslav Tykhiyflag can be set in 4717b2fd183SYaroslav Tykhiy.Fa mbuf->m_flags . 4725d4050ccSBosko MilekicThis can be achieved during setup of the external storage, 4735d4050ccSBosko Milekicby passing the 4745d4050ccSBosko Milekic.Dv M_RDONLY 4755d4050ccSBosko Milekicbit as a 4760b39b3ffSRuslan Ermilov.Fa flags 4775d4050ccSBosko Milekicargument to the 4785d4050ccSBosko Milekic.Fn MEXTADD 4797b2fd183SYaroslav Tykhiymacro, or can be directly set in individual 4807b2fd183SYaroslav Tykhiy.Vt mbufs . 481da72b998SYaroslav Tykhiy.It Fn MCHTYPE mbuf type 482da72b998SYaroslav TykhiyChange the type of 483da72b998SYaroslav Tykhiy.Fa mbuf 484da72b998SYaroslav Tykhiyto 485da72b998SYaroslav Tykhiy.Fa type . 486da72b998SYaroslav TykhiyThis is a relatively expensive operation and should be avoided. 487de06f907SNik Clayton.El 488de06f907SNik Clayton.Pp 489de06f907SNik ClaytonThe functions are: 490de06f907SNik Clayton.Bl -ohang -offset indent 491de06f907SNik Clayton.It Fn m_get how type 492de06f907SNik ClaytonA function version of 493bfd59721SBosko Milekic.Fn MGET 494bfd59721SBosko Milekicfor non-critical paths. 495328c0bbcSBosko Milekic.It Fn m_getm orig len how type 496328c0bbcSBosko MilekicAllocate 4970b39b3ffSRuslan Ermilov.Fa len 4987b2fd183SYaroslav Tykhiybytes worth of 4997b2fd183SYaroslav Tykhiy.Vt mbufs 5007b2fd183SYaroslav Tykhiyand 5017b2fd183SYaroslav Tykhiy.Vt mbuf clusters 5027b2fd183SYaroslav Tykhiyif necessary and append the resulting allocated 5037b2fd183SYaroslav Tykhiy.Vt mbuf chain 5047b2fd183SYaroslav Tykhiyto the 5057b2fd183SYaroslav Tykhiy.Vt mbuf chain 5067b2fd183SYaroslav Tykhiy.Fa orig , 5077b2fd183SYaroslav Tykhiyif it is 5080b39b3ffSRuslan Ermilov.No non- Ns Dv NULL . 509328c0bbcSBosko MilekicIf the allocation fails at any point, 5100b39b3ffSRuslan Ermilovfree whatever was allocated and return 5110b39b3ffSRuslan Ermilov.Dv NULL . 512328c0bbcSBosko MilekicIf 5130b39b3ffSRuslan Ermilov.Fa orig 5140b39b3ffSRuslan Ermilovis 5150b39b3ffSRuslan Ermilov.No non- Ns Dv NULL , 516328c0bbcSBosko Milekicit will not be freed. 517328c0bbcSBosko MilekicIt is possible to use 518328c0bbcSBosko Milekic.Fn m_getm 519328c0bbcSBosko Milekicto either append 5200b39b3ffSRuslan Ermilov.Fa len 5217b2fd183SYaroslav Tykhiybytes to an existing 5227b2fd183SYaroslav Tykhiy.Vt mbuf 5237b2fd183SYaroslav Tykhiyor 5247b2fd183SYaroslav Tykhiy.Vt mbuf chain 525328c0bbcSBosko Milekic(for example, one which may be sitting in a pre-allocated ring) 5267b2fd183SYaroslav Tykhiyor to simply perform an all-or-nothing 5277b2fd183SYaroslav Tykhiy.Vt mbuf 5287b2fd183SYaroslav Tykhiyand 5297b2fd183SYaroslav Tykhiy.Vt mbuf cluster 5307b2fd183SYaroslav Tykhiyallocation. 531de06f907SNik Clayton.It Fn m_gethdr how type 532de06f907SNik ClaytonA function version of 533bfd59721SBosko Milekic.Fn MGETHDR 534bfd59721SBosko Milekicfor non-critical paths. 5356c013706SMaxim Konovalov.It Fn m_getcl how type flags 5366c013706SMaxim KonovalovFetch an 5376c013706SMaxim Konovalov.Vt mbuf 5386c013706SMaxim Konovalovwith a 5396c013706SMaxim Konovalov.Vt mbuf cluster 5406c013706SMaxim Konovalovattached to it. 5416c013706SMaxim KonovalovIf one of the allocations fails, the entire allocation fails. 5426c013706SMaxim KonovalovThis routine is the preferred way of fetching both the 5436c013706SMaxim Konovalov.Vt mbuf 5446c013706SMaxim Konovalovand 5456c013706SMaxim Konovalov.Vt mbuf cluster 5466c013706SMaxim Konovalovtogether, as it avoids having to unlock/relock between allocations. 5476c013706SMaxim KonovalovReturns 5486c013706SMaxim Konovalov.Dv NULL 5496c013706SMaxim Konovalovon failure. 550de06f907SNik Clayton.It Fn m_getclr how type 5517b2fd183SYaroslav TykhiyAllocate an 5527b2fd183SYaroslav Tykhiy.Vt mbuf 5537b2fd183SYaroslav Tykhiyand zero out the data region. 55401e7fdafSMaxim Konovalov.It Fn m_free mbuf 55501e7fdafSMaxim KonovalovFrees 55601e7fdafSMaxim Konovalov.Vt mbuf . 557de06f907SNik Clayton.El 558de06f907SNik Clayton.Pp 5597b2fd183SYaroslav TykhiyThe functions below operate on 5607b2fd183SYaroslav Tykhiy.Vt mbuf chains . 561de06f907SNik Clayton.Bl -ohang -offset indent 562de06f907SNik Clayton.It Fn m_freem mbuf 5637b2fd183SYaroslav TykhiyFree an entire 5647b2fd183SYaroslav Tykhiy.Vt mbuf chain , 5657b2fd183SYaroslav Tykhiyincluding any external storage. 566de06f907SNik Clayton.\" 567de06f907SNik Clayton.It Fn m_adj mbuf len 568de06f907SNik ClaytonTrim 569de06f907SNik Clayton.Fa len 5707b2fd183SYaroslav Tykhiybytes from the head of an 5717b2fd183SYaroslav Tykhiy.Vt mbuf chain 5727b2fd183SYaroslav Tykhiyif 573de06f907SNik Clayton.Fa len 574de06f907SNik Claytonis positive, from the tail otherwise. 575de06f907SNik Clayton.\" 576de06f907SNik Clayton.It Fn m_prepend mbuf len how 5777b2fd183SYaroslav TykhiyAllocate a new 5787b2fd183SYaroslav Tykhiy.Vt mbuf 5797b2fd183SYaroslav Tykhiyand prepend it to the 5807b2fd183SYaroslav Tykhiy.Vt mbuf chain , 5817b2fd183SYaroslav Tykhiyhandle 582de06f907SNik Clayton.Dv M_PKTHDR 583de06f907SNik Claytonproperly. 584de06f907SNik Clayton.Sy Note : 5857b2fd183SYaroslav TykhiyIt doesn't allocate any 5867b2fd183SYaroslav Tykhiy.Vt mbuf clusters , 5877b2fd183SYaroslav Tykhiyso 588de06f907SNik Clayton.Fa len 589de06f907SNik Claytonmust be less than 590de06f907SNik Clayton.Dv MLEN 591de06f907SNik Claytonor 592de06f907SNik Clayton.Dv MHLEN , 593de06f907SNik Claytondepending on the 594d0353b83SRuslan Ermilov.Dv M_PKTHDR 595d0353b83SRuslan Ermilovflag setting. 596de06f907SNik Clayton.\" 597de06f907SNik Clayton.It Fn m_pullup mbuf len 598de06f907SNik ClaytonArrange that the first 599de06f907SNik Clayton.Fa len 6007b2fd183SYaroslav Tykhiybytes of an 6017b2fd183SYaroslav Tykhiy.Vt mbuf chain 6027b2fd183SYaroslav Tykhiyare contiguous and lay in the data area of 603de06f907SNik Clayton.Fa mbuf , 604de06f907SNik Claytonso they are accessible with 605de06f907SNik Clayton.Fn mtod mbuf type . 6067b2fd183SYaroslav TykhiyReturn the new 6077b2fd183SYaroslav Tykhiy.Vt mbuf chain 6087b2fd183SYaroslav Tykhiyon success, 609de06f907SNik Clayton.Dv NULL 610de06f907SNik Claytonon failure 6117b2fd183SYaroslav Tykhiy(the 6127b2fd183SYaroslav Tykhiy.Vt mbuf chain 6137b2fd183SYaroslav Tykhiyis freed in this case). 614de06f907SNik Clayton.Sy Note : 6157b2fd183SYaroslav TykhiyIt doesn't allocate any 6167b2fd183SYaroslav Tykhiy.Vt mbuf clusters , 6177b2fd183SYaroslav Tykhiyso 618de06f907SNik Clayton.Fa len 619de06f907SNik Claytonmust be less than 620de06f907SNik Clayton.Dv MHLEN . 621de06f907SNik Clayton.\" 622de06f907SNik Clayton.It Fn m_copym mbuf offset len how 6237b2fd183SYaroslav TykhiyMake a copy of an 6247b2fd183SYaroslav Tykhiy.Vt mbuf chain 6257b2fd183SYaroslav Tykhiystarting 626de06f907SNik Clayton.Fa offset 627de06f907SNik Claytonbytes from the beginning, continuing for 628de06f907SNik Clayton.Fa len 629e73145f6SSheldon Hearnbytes. 630e73145f6SSheldon HearnIf 631de06f907SNik Clayton.Fa len 632de06f907SNik Claytonis 633de06f907SNik Clayton.Dv M_COPYALL , 6347b2fd183SYaroslav Tykhiycopy to the end of the 6357b2fd183SYaroslav Tykhiy.Vt mbuf chain . 636de06f907SNik Clayton.Sy Note : 6377b2fd183SYaroslav TykhiyThe copy is read-only, because the 6387b2fd183SYaroslav Tykhiy.Vt mbuf clusters 6397b2fd183SYaroslav Tykhiyare not copied, only their reference counts are incremented. 640de06f907SNik Clayton.\" 641de06f907SNik Clayton.It Fn m_copypacket mbuf how 642de06f907SNik ClaytonCopy an entire packet including header, which must be present. 643de06f907SNik ClaytonThis is an optimized version of the common case 644de06f907SNik Clayton.Fn m_copym mbuf 0 M_COPYALL how . 645de06f907SNik Clayton.Sy Note : 6467b2fd183SYaroslav Tykhiythe copy is read-only, because the 6477b2fd183SYaroslav Tykhiy.Vt mbuf clusters 6487b2fd183SYaroslav Tykhiyare not copied, only their reference counts are incremented. 649de06f907SNik Clayton.\" 650de06f907SNik Clayton.It Fn m_dup mbuf how 6517b2fd183SYaroslav TykhiyCopy a packet header 6527b2fd183SYaroslav Tykhiy.Vt mbuf chain 6537b2fd183SYaroslav Tykhiyinto a completely new 6547b2fd183SYaroslav Tykhiy.Vt mbuf chain , 6557b2fd183SYaroslav Tykhiyincluding copying any 6567b2fd183SYaroslav Tykhiy.Vt mbuf clusters . 657e73145f6SSheldon HearnUse this instead of 658de06f907SNik Clayton.Fn m_copypacket 6597b2fd183SYaroslav Tykhiywhen you need a writable copy of an 6607b2fd183SYaroslav Tykhiy.Vt mbuf chain . 661de06f907SNik Clayton.\" 662de06f907SNik Clayton.It Fn m_copydata mbuf offset len buf 6637b2fd183SYaroslav TykhiyCopy data from an 6647b2fd183SYaroslav Tykhiy.Vt mbuf chain 6657b2fd183SYaroslav Tykhiystarting 666de06f907SNik Clayton.Fa off 667de06f907SNik Claytonbytes from the beginning, continuing for 668de06f907SNik Clayton.Fa len 669de06f907SNik Claytonbytes, into the indicated buffer 670de06f907SNik Clayton.Fa buf . 671de06f907SNik Clayton.\" 672de06f907SNik Clayton.It Fn m_copyback mbuf offset len buf 673de06f907SNik ClaytonCopy 674de06f907SNik Clayton.Fa len 675de06f907SNik Claytonbytes from the buffer 676de06f907SNik Clayton.Fa buf 6777b2fd183SYaroslav Tykhiyback into the indicated 6787b2fd183SYaroslav Tykhiy.Vt mbuf chain , 679de06f907SNik Claytonstarting at 680de06f907SNik Clayton.Fa offset 6817b2fd183SYaroslav Tykhiybytes from the beginning of the 6827b2fd183SYaroslav Tykhiy.Vt mbuf chain , 6837b2fd183SYaroslav Tykhiyextending the 6847b2fd183SYaroslav Tykhiy.Vt mbuf chain 6857b2fd183SYaroslav Tykhiyif necessary. 686de06f907SNik Clayton.Sy Note : 6877b2fd183SYaroslav TykhiyIt doesn't allocate any 6887b2fd183SYaroslav Tykhiy.Vt mbuf clusters , 6897b2fd183SYaroslav Tykhiyjust adds 6907b2fd183SYaroslav Tykhiy.Vt mbufs 6917b2fd183SYaroslav Tykhiyto the 6927b2fd183SYaroslav Tykhiy.Vt mbuf chain . 693e73145f6SSheldon HearnIt's safe to set 694de06f907SNik Clayton.Fa offset 6957b2fd183SYaroslav Tykhiybeyond the current 6967b2fd183SYaroslav Tykhiy.Vt mbuf chain 6977b2fd183SYaroslav Tykhiyend: zeroed 6987b2fd183SYaroslav Tykhiy.Vt mbufs 6997b2fd183SYaroslav Tykhiywill be allocated to fill the space. 700de06f907SNik Clayton.\" 7015d96084fSYaroslav Tykhiy.It Fn m_length mbuf last 7027b2fd183SYaroslav TykhiyReturn the length of the 7037b2fd183SYaroslav Tykhiy.Vt mbuf chain , 7047b2fd183SYaroslav Tykhiyand optionally a pointer to the last 7057b2fd183SYaroslav Tykhiy.Vt mbuf . 7060b1c0994SPoul-Henning Kamp.\" 707da72b998SYaroslav Tykhiy.It Fn m_dup_pkthdr to from how 708da72b998SYaroslav TykhiyUpon the function's completion, the 709da72b998SYaroslav Tykhiy.Vt mbuf 710da72b998SYaroslav Tykhiy.Fa to 711da72b998SYaroslav Tykhiywill contain an identical copy of 712da72b998SYaroslav Tykhiy.Fa from->m_pkthdr 713da72b998SYaroslav Tykhiyand the per-packet attributes found in the 714da72b998SYaroslav Tykhiy.Vt mbuf chain 715da72b998SYaroslav Tykhiy.Fa from . 716da72b998SYaroslav TykhiyThe 717da72b998SYaroslav Tykhiy.Vt mbuf 718da72b998SYaroslav Tykhiy.Fa from 719da72b998SYaroslav Tykhiymust have the flag 720da72b998SYaroslav Tykhiy.Dv M_PKTHDR 721da72b998SYaroslav Tykhiyinitially set, and 722da72b998SYaroslav Tykhiy.Fa to 723da72b998SYaroslav Tykhiymust be empty on entry. 724da72b998SYaroslav Tykhiy.\" 725da72b998SYaroslav Tykhiy.It Fn m_move_pkthdr to from 726da72b998SYaroslav TykhiyMove 727da72b998SYaroslav Tykhiy.Va m_pkthdr 728da72b998SYaroslav Tykhiyand the per-packet attributes from the 729da72b998SYaroslav Tykhiy.Vt mbuf chain 730da72b998SYaroslav Tykhiy.Fa from 731da72b998SYaroslav Tykhiyto the 732da72b998SYaroslav Tykhiy.Vt mbuf 733da72b998SYaroslav Tykhiy.Fa to . 734da72b998SYaroslav TykhiyThe 735da72b998SYaroslav Tykhiy.Vt mbuf 736da72b998SYaroslav Tykhiy.Fa from 737da72b998SYaroslav Tykhiymust have the flag 738da72b998SYaroslav Tykhiy.Dv M_PKTHDR 739da72b998SYaroslav Tykhiyinitially set, and 740da72b998SYaroslav Tykhiy.Fa to 741da72b998SYaroslav Tykhiymust be empty on entry. 742da72b998SYaroslav TykhiyUpon the function's completion, 743da72b998SYaroslav Tykhiy.Fa from 744da72b998SYaroslav Tykhiywill have the flag 745da72b998SYaroslav Tykhiy.Dv M_PKTHDR 746da72b998SYaroslav Tykhiyand the per-packet attributes cleared. 747da72b998SYaroslav Tykhiy.\" 7485d96084fSYaroslav Tykhiy.It Fn m_fixhdr mbuf 7497b2fd183SYaroslav TykhiySet the packet-header length to the length of the 7507b2fd183SYaroslav Tykhiy.Vt mbuf chain . 7510b1c0994SPoul-Henning Kamp.\" 752de06f907SNik Clayton.It Fn m_devget buf len offset ifp copy 753de06f907SNik ClaytonCopy data from a device local memory pointed to by 754de06f907SNik Clayton.Fa buf 7557b2fd183SYaroslav Tykhiyto an 7567b2fd183SYaroslav Tykhiy.Vt mbuf chain . 757e73145f6SSheldon HearnThe copy is done using a specified copy routine 758de06f907SNik Clayton.Fa copy , 759de06f907SNik Claytonor 760de06f907SNik Clayton.Fn bcopy 761de06f907SNik Claytonif 762de06f907SNik Clayton.Fa copy 763de06f907SNik Claytonis 764de06f907SNik Clayton.Dv NULL . 765de06f907SNik Clayton.\" 766de06f907SNik Clayton.It Fn m_cat m n 767de06f907SNik ClaytonConcatenate 768de06f907SNik Clayton.Fa n 769de06f907SNik Claytonto 770de06f907SNik Clayton.Fa m . 7717b2fd183SYaroslav TykhiyBoth 7727b2fd183SYaroslav Tykhiy.Vt mbuf chains 7737b2fd183SYaroslav Tykhiymust be of the same type. 774de06f907SNik Clayton.Fa N 775de06f907SNik Claytonis still valid after the function returned. 776de06f907SNik Clayton.Sy Note : 777de06f907SNik ClaytonIt does not handle 778de06f907SNik Clayton.Dv M_PKTHDR 779de06f907SNik Claytonand friends. 780de06f907SNik Clayton.\" 781de06f907SNik Clayton.It Fn m_split mbuf len how 7827b2fd183SYaroslav TykhiyPartition an 7837b2fd183SYaroslav Tykhiy.Vt mbuf chain 7847b2fd183SYaroslav Tykhiyin two pieces, returning the tail: 785de06f907SNik Claytonall but the first 786de06f907SNik Clayton.Fa len 787e73145f6SSheldon Hearnbytes. 788e73145f6SSheldon HearnIn case of failure, it returns 789de06f907SNik Clayton.Dv NULL 7907b2fd183SYaroslav Tykhiyand attempts to restore the 7917b2fd183SYaroslav Tykhiy.Vt mbuf chain 7927b2fd183SYaroslav Tykhiyto its original state. 79302312219SBruce M Simpson.\" 79402312219SBruce M Simpson.It Fn m_apply mbuf off len f arg 79502312219SBruce M SimpsonApply a function to an 79602312219SBruce M Simpson.Vt mbuf chain , 79702312219SBruce M Simpsonat offset 79802312219SBruce M Simpson.Fa off , 79902312219SBruce M Simpsonfor length 800f48cbcdeSRuslan Ermilov.Fa len 801f48cbcdeSRuslan Ermilovbytes. 80202312219SBruce M SimpsonTypically used to avoid calls to 80302312219SBruce M Simpson.Fn m_pullup 80402312219SBruce M Simpsonwhich would otherwise be unnecessary or undesirable. 80502312219SBruce M Simpson.Fa arg 80602312219SBruce M Simpsonis a convenience argument which is passed to the callback function 80702312219SBruce M Simpson.Fa f . 80802312219SBruce M Simpson.Pp 80902312219SBruce M SimpsonEach time 81002312219SBruce M Simpson.Fn f 81102312219SBruce M Simpsonis called, it will be passed 81202312219SBruce M Simpson.Fa arg , 81302312219SBruce M Simpsona pointer to the 81402312219SBruce M Simpson.Fa data 81502312219SBruce M Simpsonin the current mbuf, and the length 81602312219SBruce M Simpson.Fa len 81702312219SBruce M Simpsonof the data in this mbuf to which the function should be applied. 81802312219SBruce M Simpson.Pp 81902312219SBruce M SimpsonThe function should return zero to indicate success; 82002312219SBruce M Simpsonotherwise, if an error is indicated, then 82102312219SBruce M Simpson.Fn m_apply 82202312219SBruce M Simpsonwill return the error and stop iterating through the 82302312219SBruce M Simpson.Vt mbuf chain . 82402312219SBruce M Simpson.\" 82502312219SBruce M Simpson.It Fn m_getptr mbuf loc off 82602312219SBruce M SimpsonReturn a pointer to the mbuf containing the data located at 82702312219SBruce M Simpson.Fa loc 82802312219SBruce M Simpsonbytes from the beginning of the 82902312219SBruce M Simpson.Vt mbuf chain . 83002312219SBruce M SimpsonThe corresponding offset into the mbuf will be stored in 83102312219SBruce M Simpson.Fa *off . 832a61bc764SMike Silbersack.It Fn m_defrag m0 how 833f48cbcdeSRuslan ErmilovDefragment an mbuf chain, returning the shortest possible 834bdf86185SMike Silbersackchain of mbufs and clusters. 835bdf86185SMike SilbersackIf allocation fails and this can not be completed, 836bdf86185SMike Silbersack.Dv NULL 837bdf86185SMike Silbersackwill be returned and the original chain will be unchanged. 838bdf86185SMike SilbersackUpon success, the original chain will be freed and the new 839bdf86185SMike Silbersackchain will be returned. 840a61bc764SMike Silbersack.Fa how 841a61bc764SMike Silbersackshould be either 842a61bc764SMike Silbersack.Dv M_TRYWAIT 843a61bc764SMike Silbersackor 844a61bc764SMike Silbersack.Dv M_DONTWAIT , 845a61bc764SMike Silbersackdepending on the caller's preference. 846a61bc764SMike Silbersack.Pp 847a61bc764SMike SilbersackThis function is especially useful in network drivers, where 848a61bc764SMike Silbersackcertain long mbuf chains must be shortened before being added 849a61bc764SMike Silbersackto TX descriptor lists. 8503136363fSRuslan Ermilov.El 8510193eb5eSYaroslav Tykhiy.Sh HARDWARE-ASSISTED CHECKSUM CALCULATION 8520193eb5eSYaroslav TykhiyThis section currently applies to TCP/IP only. 8530193eb5eSYaroslav TykhiyIn order to save the host CPU resources, computing checksums is 8540193eb5eSYaroslav Tykhiyoffloaded to the network interface hardware if possible. 8550193eb5eSYaroslav TykhiyThe 8560193eb5eSYaroslav Tykhiy.Va m_pkthdr 8570193eb5eSYaroslav Tykhiymember of the leading 8580193eb5eSYaroslav Tykhiy.Vt mbuf 8590193eb5eSYaroslav Tykhiyof a packet contains two fields used for that purpose, 8600193eb5eSYaroslav Tykhiy.Vt int Va csum_flags 8610193eb5eSYaroslav Tykhiyand 8620193eb5eSYaroslav Tykhiy.Vt int Va csum_data . 8630193eb5eSYaroslav TykhiyThe meaning of those fields depends on the direction a packet flows in, 8640193eb5eSYaroslav Tykhiyand on whether the packet is fragmented. 8650193eb5eSYaroslav TykhiyHenceforth, 8660193eb5eSYaroslav Tykhiy.Va csum_flags 8670193eb5eSYaroslav Tykhiyor 8680193eb5eSYaroslav Tykhiy.Va csum_data 8690193eb5eSYaroslav Tykhiyof a packet 8700193eb5eSYaroslav Tykhiywill denote the corresponding field of the 8710193eb5eSYaroslav Tykhiy.Va m_pkthdr 8720193eb5eSYaroslav Tykhiymember of the leading 8730193eb5eSYaroslav Tykhiy.Vt mbuf 8740193eb5eSYaroslav Tykhiyin the 8750193eb5eSYaroslav Tykhiy.Vt mbuf chain 8760193eb5eSYaroslav Tykhiycontaining the packet. 8770193eb5eSYaroslav Tykhiy.Pp 8780193eb5eSYaroslav TykhiyOn output, checksum offloading is attempted after the outgoing 8790193eb5eSYaroslav Tykhiyinterface has been determined for a packet. 8800193eb5eSYaroslav TykhiyThe interface-specific field 8810193eb5eSYaroslav Tykhiy.Va ifnet.if_data.ifi_hwassist 8820193eb5eSYaroslav Tykhiy(see 8830193eb5eSYaroslav Tykhiy.Xr ifnet 9 ) 8840193eb5eSYaroslav Tykhiyis consulted for the capabilities of the interface to assist in 8850193eb5eSYaroslav Tykhiycomputing checksums. 8860193eb5eSYaroslav TykhiyThe 8870193eb5eSYaroslav Tykhiy.Va csum_flags 888f48cbcdeSRuslan Ermilovfield of the packet header is set to indicate which actions the interface 8890193eb5eSYaroslav Tykhiyis supposed to perform on it. 8900193eb5eSYaroslav TykhiyThe actions unsupported by the network interface are done in the 8910193eb5eSYaroslav Tykhiysoftware prior to passing the packet down to the interface driver; 8920193eb5eSYaroslav Tykhiysuch actions will never be requested through 8930193eb5eSYaroslav Tykhiy.Va csum_flags . 8940193eb5eSYaroslav Tykhiy.Pp 8950193eb5eSYaroslav TykhiyThe flags demanding a particular action from an interface are as follows: 8960193eb5eSYaroslav Tykhiy.Bl -tag -width ".Dv CSUM_TCP" -offset indent 8970193eb5eSYaroslav Tykhiy.It Dv CSUM_IP 8980193eb5eSYaroslav TykhiyThe IP header checksum is to be computed and stored in the 8990193eb5eSYaroslav Tykhiycorresponding field of the packet. 9000193eb5eSYaroslav TykhiyThe hardware is expected to know the format of an IP header 9010193eb5eSYaroslav Tykhiyto determine the offset of the IP checksum field. 9020193eb5eSYaroslav Tykhiy.It Dv CSUM_TCP 903f48cbcdeSRuslan ErmilovThe TCP checksum is to be computed. 904f48cbcdeSRuslan Ermilov(See below.) 9050193eb5eSYaroslav Tykhiy.It Dv CSUM_UDP 906f48cbcdeSRuslan ErmilovThe UDP checksum is to be computed. 907f48cbcdeSRuslan Ermilov(See below.) 9080193eb5eSYaroslav Tykhiy.El 9090193eb5eSYaroslav Tykhiy.Pp 910fbde705aSYaroslav TykhiyShould a TCP or UDP checksum be offloaded to the hardware, 9110193eb5eSYaroslav Tykhiythe field 9120193eb5eSYaroslav Tykhiy.Va csum_data 9130193eb5eSYaroslav Tykhiywill contain the byte offset of the checksum field relative to the 9140193eb5eSYaroslav Tykhiyend of the IP header. 9150193eb5eSYaroslav TykhiyIn this case, the checksum field will be initially 9160193eb5eSYaroslav Tykhiyset by the TCP/IP module to the checksum of the pseudo header 917fbde705aSYaroslav Tykhiydefined by the TCP and UDP specifications. 9180193eb5eSYaroslav Tykhiy.Pp 9190193eb5eSYaroslav TykhiyFor outbound packets which have been fragmented 9200193eb5eSYaroslav Tykhiyby the host CPU, the following will also be true, 9210193eb5eSYaroslav Tykhiyregardless of the checksum flag settings: 9220193eb5eSYaroslav Tykhiy.Bl -bullet -offset indent 9230193eb5eSYaroslav Tykhiy.It 9240193eb5eSYaroslav Tykhiyall fragments will have the flag 9250193eb5eSYaroslav Tykhiy.Dv M_FRAG 9260193eb5eSYaroslav Tykhiyset in their 9270193eb5eSYaroslav Tykhiy.Va m_flags 9280193eb5eSYaroslav Tykhiyfield; 9290193eb5eSYaroslav Tykhiy.It 9300193eb5eSYaroslav Tykhiythe first and the last fragments in the chain will have 9310193eb5eSYaroslav Tykhiy.Dv M_FIRSTFRAG 9320193eb5eSYaroslav Tykhiyor 9330193eb5eSYaroslav Tykhiy.Dv M_LASTFRAG 9340193eb5eSYaroslav Tykhiyset in their 9350193eb5eSYaroslav Tykhiy.Va m_flags , 9360193eb5eSYaroslav Tykhiycorrespondingly; 9370193eb5eSYaroslav Tykhiy.It 9380193eb5eSYaroslav Tykhiythe first fragment in the chain will have the total number 9390193eb5eSYaroslav Tykhiyof fragments contained in its 9400193eb5eSYaroslav Tykhiy.Va csum_data 9410193eb5eSYaroslav Tykhiyfield. 9420193eb5eSYaroslav Tykhiy.El 9430193eb5eSYaroslav Tykhiy.Pp 94476f80df6SYaroslav TykhiyThe last rule for fragmented packets takes precedence over the one 94576f80df6SYaroslav Tykhiyfor a TCP or UDP checksum. 94676f80df6SYaroslav TykhiyNevertheless, offloading a TCP or UDP checksum is possible for a 94776f80df6SYaroslav Tykhiyfragmented packet if the flag 94876f80df6SYaroslav Tykhiy.Dv CSUM_IP_FRAGS 94976f80df6SYaroslav Tykhiyis set in the field 95076f80df6SYaroslav Tykhiy.Va ifnet.if_data.ifi_hwassist 95176f80df6SYaroslav Tykhiyassociated with the network interface. 95276f80df6SYaroslav TykhiyHowever, in this case the interface is expected to figure out 95376f80df6SYaroslav Tykhiythe location of the checksum field within the sequence of fragments 95476f80df6SYaroslav Tykhiyby itself because 9550193eb5eSYaroslav Tykhiy.Va csum_data 95676f80df6SYaroslav Tykhiycontains a fragment count instead of a checksum offset value. 9570193eb5eSYaroslav Tykhiy.Pp 9580193eb5eSYaroslav TykhiyOn input, an interface indicates the actions it has performed 9590193eb5eSYaroslav Tykhiyon a packet by setting one or more of the following flags in 9600193eb5eSYaroslav Tykhiy.Va csum_flags 9610193eb5eSYaroslav Tykhiyassociated with the packet: 9620193eb5eSYaroslav Tykhiy.Bl -tag -width ".Dv CSUM_IP_CHECKED" -offset indent 9630193eb5eSYaroslav Tykhiy.It Dv CSUM_IP_CHECKED 9640193eb5eSYaroslav TykhiyThe IP header checksum has been computed. 9650193eb5eSYaroslav Tykhiy.It Dv CSUM_IP_VALID 9660193eb5eSYaroslav TykhiyThe IP header has a valid checksum. 9670193eb5eSYaroslav TykhiyThis flag can appear only in combination with 9680193eb5eSYaroslav Tykhiy.Dv CSUM_IP_CHECKED . 9690193eb5eSYaroslav Tykhiy.It Dv CSUM_DATA_VALID 9700193eb5eSYaroslav TykhiyThe checksum of the data portion of the IP packet has been computed 9710193eb5eSYaroslav Tykhiyand stored in the field 9720193eb5eSYaroslav Tykhiy.Va csum_data 9730193eb5eSYaroslav Tykhiyin network byte order. 9740193eb5eSYaroslav Tykhiy.It Dv CSUM_PSEUDO_HDR 9750193eb5eSYaroslav TykhiyCan be set only along with 9760193eb5eSYaroslav Tykhiy.Dv CSUM_DATA_VALID 9770193eb5eSYaroslav Tykhiyto indicate that the IP data checksum found in 9780193eb5eSYaroslav Tykhiy.Va csum_data 979fbde705aSYaroslav Tykhiyallows for the pseudo header defined by the TCP and UDP specifications. 9800193eb5eSYaroslav TykhiyOtherwise the checksum of the pseudo header must be calculated by 9810193eb5eSYaroslav Tykhiythe host CPU and added to 9820193eb5eSYaroslav Tykhiy.Va csum_data 983fbde705aSYaroslav Tykhiyto obtain the final checksum to be used for TCP or UDP validation purposes. 9840193eb5eSYaroslav Tykhiy.El 9850193eb5eSYaroslav Tykhiy.Pp 9860193eb5eSYaroslav TykhiyIf a particular network interface just indicates success or 9870193eb5eSYaroslav Tykhiyfailure of TCP or UDP checksum validation without returning 9880193eb5eSYaroslav Tykhiythe exact value of the checksum to the host CPU, its driver can mark 9890193eb5eSYaroslav Tykhiy.Dv CSUM_DATA_VALID 9900193eb5eSYaroslav Tykhiyand 9910193eb5eSYaroslav Tykhiy.Dv CSUM_PSEUDO_HDR 9920193eb5eSYaroslav Tykhiyin 9930193eb5eSYaroslav Tykhiy.Va csum_flags , 9940193eb5eSYaroslav Tykhiyand set 9950193eb5eSYaroslav Tykhiy.Va csum_data 9960193eb5eSYaroslav Tykhiyto 9970193eb5eSYaroslav Tykhiy.Li 0xFFFF 9980193eb5eSYaroslav Tykhiyhexadecimal to indicate a valid checksum. 999f48cbcdeSRuslan ErmilovIt is a peculiarity of the algorithm used that the Internet checksum 10000193eb5eSYaroslav Tykhiycalculated over any valid packet will be 10010193eb5eSYaroslav Tykhiy.Li 0xFFFF 10020193eb5eSYaroslav Tykhiyas long as the original checksum field is included. 10030193eb5eSYaroslav Tykhiy.Pp 10040193eb5eSYaroslav TykhiyFor inbound packets which are IP fragments, all 10050193eb5eSYaroslav Tykhiy.Va csum_data 10060193eb5eSYaroslav Tykhiyfields will be summed during reassembly to obtain the final checksum 10070193eb5eSYaroslav Tykhiyvalue passed to an upper layer in the 10080193eb5eSYaroslav Tykhiy.Va csum_data 10090193eb5eSYaroslav Tykhiyfield of the reassembled packet. 10100193eb5eSYaroslav TykhiyThe 10110193eb5eSYaroslav Tykhiy.Va csum_flags 10120193eb5eSYaroslav Tykhiyfields of all fragments will be consolidated using logical AND 10130193eb5eSYaroslav Tykhiyto obtain the final value for 10140193eb5eSYaroslav Tykhiy.Va csum_flags . 10150193eb5eSYaroslav TykhiyThus, in order to successfully 10160193eb5eSYaroslav Tykhiyoffload checksum computation for fragmented data, 10170193eb5eSYaroslav Tykhiyall fragments should have the same value of 10180193eb5eSYaroslav Tykhiy.Va csum_flags . 10193cab047eSMike Silbersack.Sh STRESS TESTING 1020c8185672SRuslan ErmilovWhen running a kernel compiled with the option 1021c8185672SRuslan Ermilov.Dv MBUF_STRESS_TEST , 1022c8185672SRuslan Ermilovthe following 1023c8185672SRuslan Ermilov.Xr sysctl 8 Ns 1024c8185672SRuslan Ermilov-controlled options may be used to create 10253cab047eSMike Silbersackvarious failure/extreme cases for testing of network drivers 10267b2fd183SYaroslav Tykhiyand other parts of the kernel that rely on 10277b2fd183SYaroslav Tykhiy.Vt mbufs . 1028c8185672SRuslan Ermilov.Bl -tag -width ident 1029c8185672SRuslan Ermilov.It Va net.inet.ip.mbuf_frag_size 1030c8185672SRuslan ErmilovCauses 1031c8185672SRuslan Ermilov.Fn ip_output 10327b2fd183SYaroslav Tykhiyto fragment outgoing 10337b2fd183SYaroslav Tykhiy.Vt mbuf chains 10347b2fd183SYaroslav Tykhiyinto fragments of the specified size. 1035c8185672SRuslan ErmilovSetting this variable to 1 is an excellent way to 10367b2fd183SYaroslav Tykhiytest the long 10377b2fd183SYaroslav Tykhiy.Vt mbuf chain 10387b2fd183SYaroslav Tykhiyhandling ability of network drivers. 1039c8185672SRuslan Ermilov.It Va kern.ipc.m_defragrandomfailures 1040c8185672SRuslan ErmilovCauses the function 1041c8185672SRuslan Ermilov.Fn m_defrag 1042c8185672SRuslan Ermilovto randomly fail, returning 1043c8185672SRuslan Ermilov.Dv NULL . 1044c8185672SRuslan ErmilovAny piece of code which uses 1045c8185672SRuslan Ermilov.Fn m_defrag 1046c8185672SRuslan Ermilovshould be tested with this feature. 1047c8185672SRuslan Ermilov.El 1048de06f907SNik Clayton.Sh RETURN VALUES 1049de06f907SNik ClaytonSee above. 10500193eb5eSYaroslav Tykhiy.Sh SEE ALSO 1051f48cbcdeSRuslan Ermilov.Xr ifnet 9 1052de06f907SNik Clayton.Sh HISTORY 1053de06f907SNik Clayton.\" Please correct me if I'm wrong 10547b2fd183SYaroslav Tykhiy.Vt Mbufs 10557b2fd183SYaroslav Tykhiyappeared in an early version of 1056753d686dSRuslan Ermilov.Bx . 105753b2520dSTom RhodesBesides being used for network packets, they were used 1058bfd59721SBosko Milekicto store various dynamic structures, such as routing table 1059bfd59721SBosko Milekicentries, interface addresses, protocol control blocks, etc. 1060de06f907SNik Clayton.Sh AUTHORS 10616575e6daSRuslan ErmilovThe original 10626575e6daSRuslan Ermilov.Nm 10636575e6daSRuslan Ermilovman page was written by Yar Tikhiy. 1064