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.\" 27fa05d215SGleb Smirnoff.Dd April 18, 2011 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" 48cf827063SPoul-Henning Kamp.Fa "void (*free)(void *opt_arg1, void *opt_arg2)" 49cf827063SPoul-Henning Kamp.Fa "void *opt_arg1" 50cf827063SPoul-Henning Kamp.Fa "void *opt_arg2" 515d4050ccSBosko Milekic.Fa "short flags" 525d4050ccSBosko Milekic.Fa "int type" 53bfd59721SBosko Milekic.Fc 54bfd59721SBosko Milekic.Fn MEXTFREE "struct mbuf *mbuf" 55bfd59721SBosko Milekic.Fn MFREE "struct mbuf *mbuf" "struct mbuf *successor" 56de06f907SNik Clayton.\" 57bfd59721SBosko Milekic.Ss Mbuf utility macros 58f16b3c0dSChad David.Fn mtod "struct mbuf *mbuf" "type" 59bfd59721SBosko Milekic.Fn M_ALIGN "struct mbuf *mbuf" "u_int len" 60bfd59721SBosko Milekic.Fn MH_ALIGN "struct mbuf *mbuf" "u_int len" 61f16b3c0dSChad David.Ft int 62bfd59721SBosko Milekic.Fn M_LEADINGSPACE "struct mbuf *mbuf" 63f16b3c0dSChad David.Ft int 64bfd59721SBosko Milekic.Fn M_TRAILINGSPACE "struct mbuf *mbuf" 65da72b998SYaroslav Tykhiy.Fn M_MOVE_PKTHDR "struct mbuf *to" "struct mbuf *from" 66bfd59721SBosko Milekic.Fn M_PREPEND "struct mbuf *mbuf" "int len" "int how" 67bfd59721SBosko Milekic.Fn MCHTYPE "struct mbuf *mbuf" "u_int type" 68f16b3c0dSChad David.Ft int 695d4050ccSBosko Milekic.Fn M_WRITABLE "struct mbuf *mbuf" 70bfd59721SBosko Milekic.\" 71bfd59721SBosko Milekic.Ss Mbuf allocation functions 72de06f907SNik Clayton.Ft struct mbuf * 73de06f907SNik Clayton.Fn m_get "int how" "int type" 74de06f907SNik Clayton.Ft struct mbuf * 75328c0bbcSBosko Milekic.Fn m_getm "struct mbuf *orig" "int len" "int how" "int type" 76328c0bbcSBosko Milekic.Ft struct mbuf * 776c013706SMaxim Konovalov.Fn m_getcl "int how" "short type" "int flags" 786c013706SMaxim Konovalov.Ft struct mbuf * 79de06f907SNik Clayton.Fn m_getclr "int how" "int type" 80de06f907SNik Clayton.Ft struct mbuf * 81de06f907SNik Clayton.Fn m_gethdr "int how" "int type" 82bfd59721SBosko Milekic.Ft struct mbuf * 83bfd59721SBosko Milekic.Fn m_free "struct mbuf *mbuf" 84de06f907SNik Clayton.Ft void 85de06f907SNik Clayton.Fn m_freem "struct mbuf *mbuf" 86bfd59721SBosko Milekic.\" 87bfd59721SBosko Milekic.Ss Mbuf utility functions 88de06f907SNik Clayton.Ft void 89de06f907SNik Clayton.Fn m_adj "struct mbuf *mbuf" "int len" 909c3fd404SSam Leffler.Ft void 919c3fd404SSam Leffler.Fn m_align "struct mbuf *mbuf" "int len" 92b9def06bSSam Leffler.Ft int 93b9def06bSSam Leffler.Fn m_append "struct mbuf *mbuf" "int len" "c_caddr_t cp" 94de06f907SNik Clayton.Ft struct mbuf * 95de06f907SNik Clayton.Fn m_prepend "struct mbuf *mbuf" "int len" "int how" 96de06f907SNik Clayton.Ft struct mbuf * 977ac139a9SJohn-Mark Gurney.Fn m_copyup "struct mbuf *mbuf" "int len" "int dstoff" 987ac139a9SJohn-Mark Gurney.Ft struct mbuf * 99de06f907SNik Clayton.Fn m_pullup "struct mbuf *mbuf" "int len" 100de06f907SNik Clayton.Ft struct mbuf * 101d305f4b9SBruce M Simpson.Fn m_pulldown "struct mbuf *mbuf" "int offset" "int len" "int *offsetp" 102d305f4b9SBruce M Simpson.Ft struct mbuf * 103de06f907SNik Clayton.Fn m_copym "struct mbuf *mbuf" "int offset" "int len" "int how" 104de06f907SNik Clayton.Ft struct mbuf * 105de06f907SNik Clayton.Fn m_copypacket "struct mbuf *mbuf" "int how" 106de06f907SNik Clayton.Ft struct mbuf * 107de06f907SNik Clayton.Fn m_dup "struct mbuf *mbuf" "int how" 108de06f907SNik Clayton.Ft void 1090d8e4c57SBruce Evans.Fn m_copydata "const struct mbuf *mbuf" "int offset" "int len" "caddr_t buf" 110de06f907SNik Clayton.Ft void 111de06f907SNik Clayton.Fn m_copyback "struct mbuf *mbuf" "int offset" "int len" "caddr_t buf" 112de06f907SNik Clayton.Ft struct mbuf * 113de06f907SNik Clayton.Fo m_devget 114de06f907SNik Clayton.Fa "char *buf" 115de06f907SNik Clayton.Fa "int len" 116de06f907SNik Clayton.Fa "int offset" 117de06f907SNik Clayton.Fa "struct ifnet *ifp" 118de06f907SNik Clayton.Fa "void (*copy)(char *from, caddr_t to, u_int len)" 119de06f907SNik Clayton.Fc 120de06f907SNik Clayton.Ft void 121de06f907SNik Clayton.Fn m_cat "struct mbuf *m" "struct mbuf *n" 122e60fa837SRuslan Ermilov.Ft u_int 1230b1c0994SPoul-Henning Kamp.Fn m_fixhdr "struct mbuf *mbuf" 124da72b998SYaroslav Tykhiy.Ft void 125da72b998SYaroslav Tykhiy.Fn m_dup_pkthdr "struct mbuf *to" "struct mbuf *from" 126da72b998SYaroslav Tykhiy.Ft void 127da72b998SYaroslav Tykhiy.Fn m_move_pkthdr "struct mbuf *to" "struct mbuf *from" 128e60fa837SRuslan Ermilov.Ft u_int 1290b1c0994SPoul-Henning Kamp.Fn m_length "struct mbuf *mbuf" "struct mbuf **last" 130de06f907SNik Clayton.Ft struct mbuf * 131de06f907SNik Clayton.Fn m_split "struct mbuf *mbuf" "int len" "int how" 13202312219SBruce M Simpson.Ft int 13302312219SBruce M Simpson.Fn m_apply "struct mbuf *mbuf" "int off" "int len" "int (*f)(void *arg, void *data, u_int len)" "void *arg" 13402312219SBruce M Simpson.Ft struct mbuf * 13502312219SBruce M Simpson.Fn m_getptr "struct mbuf *mbuf" "int loc" "int *off" 136a61bc764SMike Silbersack.Ft struct mbuf * 137a61bc764SMike Silbersack.Fn m_defrag "struct mbuf *m0" "int how" 13847e2996eSSam Leffler.Ft struct mbuf * 13947e2996eSSam Leffler.Fn m_unshare "struct mbuf *m0" "int how" 140de06f907SNik Clayton.\" 141de06f907SNik Clayton.Sh DESCRIPTION 1427b2fd183SYaroslav TykhiyAn 1437b2fd183SYaroslav Tykhiy.Vt mbuf 1447b2fd183SYaroslav Tykhiyis a basic unit of memory management in the kernel IPC subsystem. 1457b2fd183SYaroslav TykhiyNetwork packets and socket buffers are stored in 1467b2fd183SYaroslav Tykhiy.Vt mbufs . 1477b2fd183SYaroslav TykhiyA network packet may span multiple 1487b2fd183SYaroslav Tykhiy.Vt mbufs 1497b2fd183SYaroslav Tykhiyarranged into a 1507b2fd183SYaroslav Tykhiy.Vt mbuf chain 151c4d9468eSRuslan Ermilov(linked list), 152de06f907SNik Claytonwhich allows adding or trimming 153bfd59721SBosko Milekicnetwork headers with little overhead. 154de06f907SNik Clayton.Pp 1557b2fd183SYaroslav TykhiyWhile a developer should not bother with 1567b2fd183SYaroslav Tykhiy.Vt mbuf 1577b2fd183SYaroslav Tykhiyinternals without serious 158de06f907SNik Claytonreason in order to avoid incompatibilities with future changes, it 1597b2fd183SYaroslav Tykhiyis useful to understand the general structure of an 1607b2fd183SYaroslav Tykhiy.Vt mbuf . 161de06f907SNik Clayton.Pp 1627b2fd183SYaroslav TykhiyAn 1637b2fd183SYaroslav Tykhiy.Vt mbuf 1647b2fd183SYaroslav Tykhiyconsists of a variable-sized header and a small internal 165e73145f6SSheldon Hearnbuffer for data. 1667b2fd183SYaroslav TykhiyThe total size of an 1677b2fd183SYaroslav Tykhiy.Vt mbuf , 168bfd59721SBosko Milekic.Dv MSIZE , 169feccf50cSAlex Dupreis a constant defined in 170feccf50cSAlex Dupre.In sys/param.h . 1717b2fd183SYaroslav TykhiyThe 1727b2fd183SYaroslav Tykhiy.Vt mbuf 1737b2fd183SYaroslav Tykhiyheader includes: 174238d9c59SYaroslav Tykhiy.Bl -tag -width "m_nextpkt" -offset indent 175be82a0bcSYaroslav Tykhiy.It Va m_next 176238d9c59SYaroslav Tykhiy.Pq Vt struct mbuf * 177238d9c59SYaroslav TykhiyA pointer to the next 1787b2fd183SYaroslav Tykhiy.Vt mbuf 1797b2fd183SYaroslav Tykhiyin the 180238d9c59SYaroslav Tykhiy.Vt mbuf chain . 181be82a0bcSYaroslav Tykhiy.It Va m_nextpkt 182238d9c59SYaroslav Tykhiy.Pq Vt struct mbuf * 183238d9c59SYaroslav TykhiyA pointer to the next 1847b2fd183SYaroslav Tykhiy.Vt mbuf chain 185238d9c59SYaroslav Tykhiyin the queue. 186be82a0bcSYaroslav Tykhiy.It Va m_data 187238d9c59SYaroslav Tykhiy.Pq Vt caddr_t 188238d9c59SYaroslav TykhiyA pointer to data attached to this 189238d9c59SYaroslav Tykhiy.Vt mbuf . 190be82a0bcSYaroslav Tykhiy.It Va m_len 191238d9c59SYaroslav Tykhiy.Pq Vt int 192238d9c59SYaroslav TykhiyThe length of the data. 193be82a0bcSYaroslav Tykhiy.It Va m_type 194238d9c59SYaroslav Tykhiy.Pq Vt short 195238d9c59SYaroslav TykhiyThe type of the data. 196be82a0bcSYaroslav Tykhiy.It Va m_flags 197238d9c59SYaroslav Tykhiy.Pq Vt int 198238d9c59SYaroslav TykhiyThe 1997b2fd183SYaroslav Tykhiy.Vt mbuf 200238d9c59SYaroslav Tykhiyflags. 201de06f907SNik Clayton.El 202de06f907SNik Clayton.Pp 2037b2fd183SYaroslav TykhiyThe 2047b2fd183SYaroslav Tykhiy.Vt mbuf 2057b2fd183SYaroslav Tykhiyflag bits are defined as follows: 206de06f907SNik Clayton.Bd -literal 207de06f907SNik Clayton/* mbuf flags */ 208de06f907SNik Clayton#define M_EXT 0x0001 /* has associated external storage */ 209de06f907SNik Clayton#define M_PKTHDR 0x0002 /* start of record */ 210de06f907SNik Clayton#define M_EOR 0x0004 /* end of record */ 2115d4050ccSBosko Milekic#define M_RDONLY 0x0008 /* associated data marked read-only */ 2125d4050ccSBosko Milekic#define M_PROTO1 0x0010 /* protocol-specific */ 2135d4050ccSBosko Milekic#define M_PROTO2 0x0020 /* protocol-specific */ 2145d4050ccSBosko Milekic#define M_PROTO3 0x0040 /* protocol-specific */ 2155d4050ccSBosko Milekic#define M_PROTO4 0x0080 /* protocol-specific */ 2165d4050ccSBosko Milekic#define M_PROTO5 0x0100 /* protocol-specific */ 217f434362aSYaroslav Tykhiy#define M_PROTO6 0x4000 /* protocol-specific (avoid M_BCAST conflict) */ 218f434362aSYaroslav Tykhiy#define M_FREELIST 0x8000 /* mbuf is on the free list */ 219de06f907SNik Clayton 220f434362aSYaroslav Tykhiy/* mbuf pkthdr flags (also stored in m_flags) */ 2215d4050ccSBosko Milekic#define M_BCAST 0x0200 /* send/received as link-level broadcast */ 2225d4050ccSBosko Milekic#define M_MCAST 0x0400 /* send/received as link-level multicast */ 2235d4050ccSBosko Milekic#define M_FRAG 0x0800 /* packet is fragment of larger packet */ 2245d4050ccSBosko Milekic#define M_FIRSTFRAG 0x1000 /* packet is first fragment */ 2255d4050ccSBosko Milekic#define M_LASTFRAG 0x2000 /* packet is last fragment */ 226de06f907SNik Clayton.Ed 227de06f907SNik Clayton.Pp 2287b2fd183SYaroslav TykhiyThe available 2297b2fd183SYaroslav Tykhiy.Vt mbuf 2307b2fd183SYaroslav Tykhiytypes are defined as follows: 231de06f907SNik Clayton.Bd -literal 232de06f907SNik Clayton/* mbuf types */ 233de06f907SNik Clayton#define MT_DATA 1 /* dynamic (data) allocation */ 2341cb58a07SMaxim Konovalov#define MT_HEADER MT_DATA /* packet header */ 235de06f907SNik Clayton#define MT_SONAME 8 /* socket name */ 236de06f907SNik Clayton#define MT_CONTROL 14 /* extra-data protocol message */ 237de06f907SNik Clayton#define MT_OOBDATA 15 /* expedited data */ 238de06f907SNik Clayton.Ed 239de06f907SNik Clayton.Pp 240c3a2dbe9SDag-Erling SmørgravThe available external buffer types are defined as follows: 241c3a2dbe9SDag-Erling Smørgrav.Bd -literal 242c3a2dbe9SDag-Erling Smørgrav/* external buffer types */ 243c3a2dbe9SDag-Erling Smørgrav#define EXT_CLUSTER 1 /* mbuf cluster */ 244c3a2dbe9SDag-Erling Smørgrav#define EXT_SFBUF 2 /* sendfile(2)'s sf_bufs */ 245c3a2dbe9SDag-Erling Smørgrav#define EXT_JUMBOP 3 /* jumbo cluster 4096 bytes */ 246c3a2dbe9SDag-Erling Smørgrav#define EXT_JUMBO9 4 /* jumbo cluster 9216 bytes */ 247c3a2dbe9SDag-Erling Smørgrav#define EXT_JUMBO16 5 /* jumbo cluster 16184 bytes */ 248c3a2dbe9SDag-Erling Smørgrav#define EXT_PACKET 6 /* mbuf+cluster from packet zone */ 249c3a2dbe9SDag-Erling Smørgrav#define EXT_MBUF 7 /* external mbuf reference (M_IOVEC) */ 250c3a2dbe9SDag-Erling Smørgrav#define EXT_NET_DRV 100 /* custom ext_buf provided by net driver(s) */ 251c3a2dbe9SDag-Erling Smørgrav#define EXT_MOD_TYPE 200 /* custom module's ext_buf type */ 252c3a2dbe9SDag-Erling Smørgrav#define EXT_DISPOSABLE 300 /* can throw this buffer away w/page flipping */ 253c3a2dbe9SDag-Erling Smørgrav#define EXT_EXTREF 400 /* has externally maintained ref_cnt ptr */ 254c3a2dbe9SDag-Erling Smørgrav.Ed 255c3a2dbe9SDag-Erling Smørgrav.Pp 256de06f907SNik ClaytonIf the 257de06f907SNik Clayton.Dv M_PKTHDR 258de06f907SNik Claytonflag is set, a 2597b2fd183SYaroslav Tykhiy.Vt struct pkthdr Va m_pkthdr 2607b2fd183SYaroslav Tykhiyis added to the 2617b2fd183SYaroslav Tykhiy.Vt mbuf 2627b2fd183SYaroslav Tykhiyheader. 263e73145f6SSheldon HearnIt contains a pointer to the interface 264de06f907SNik Claytonthe packet has been received from 2657b2fd183SYaroslav Tykhiy.Pq Vt struct ifnet Va *rcvif , 266de06f907SNik Claytonand the total packet length 2677b2fd183SYaroslav Tykhiy.Pq Vt int Va len . 268e85cc3f5SRuslan ErmilovOptionally, it may also contain an attached list of packet tags 269e85cc3f5SRuslan Ermilov.Pq Vt "struct m_tag" . 270e85cc3f5SRuslan ErmilovSee 271e85cc3f5SRuslan Ermilov.Xr mbuf_tags 9 272e85cc3f5SRuslan Ermilovfor details. 2730193eb5eSYaroslav TykhiyFields used in offloading checksum calculation to the hardware are kept in 2740193eb5eSYaroslav Tykhiy.Va m_pkthdr 2750193eb5eSYaroslav Tykhiyas well. 2760193eb5eSYaroslav TykhiySee 2770193eb5eSYaroslav Tykhiy.Sx HARDWARE-ASSISTED CHECKSUM CALCULATION 2780193eb5eSYaroslav Tykhiyfor details. 279de06f907SNik Clayton.Pp 2807b2fd183SYaroslav TykhiyIf small enough, data is stored in the internal data buffer of an 2817b2fd183SYaroslav Tykhiy.Vt mbuf . 2827b2fd183SYaroslav TykhiyIf the data is sufficiently large, another 2837b2fd183SYaroslav Tykhiy.Vt mbuf 2847b2fd183SYaroslav Tykhiymay be added to the 2857b2fd183SYaroslav Tykhiy.Vt mbuf chain , 2867b2fd183SYaroslav Tykhiyor external storage may be associated with the 2877b2fd183SYaroslav Tykhiy.Vt mbuf . 288de06f907SNik Clayton.Dv MHLEN 2897b2fd183SYaroslav Tykhiybytes of data can fit into an 2907b2fd183SYaroslav Tykhiy.Vt mbuf 2917b2fd183SYaroslav Tykhiywith the 292de06f907SNik Clayton.Dv M_PKTHDR 293de06f907SNik Claytonflag set, 294de06f907SNik Clayton.Dv MLEN 295de06f907SNik Claytonbytes can otherwise. 296de06f907SNik Clayton.Pp 2977b2fd183SYaroslav TykhiyIf external storage is being associated with an 2987b2fd183SYaroslav Tykhiy.Vt mbuf , 2997b2fd183SYaroslav Tykhiythe 300be82a0bcSYaroslav Tykhiy.Va m_ext 30125f1a2e9SSheldon Hearnheader is added at the cost of losing the internal data buffer. 302e73145f6SSheldon HearnIt includes a pointer to external storage, the size of the storage, 303e73145f6SSheldon Hearna pointer to a function used for freeing the storage, 304e73145f6SSheldon Hearna pointer to an optional argument that can be passed to the function, 305e73145f6SSheldon Hearnand a pointer to a reference counter. 3067b2fd183SYaroslav TykhiyAn 3077b2fd183SYaroslav Tykhiy.Vt mbuf 3087b2fd183SYaroslav Tykhiyusing external storage has the 309de06f907SNik Clayton.Dv M_EXT 310de06f907SNik Claytonflag set. 311de06f907SNik Clayton.Pp 312bfd59721SBosko MilekicThe system supplies a macro for allocating the desired external storage 313bfd59721SBosko Milekicbuffer, 314bfd59721SBosko Milekic.Dv MEXTADD . 315bfd59721SBosko Milekic.Pp 316bfd59721SBosko MilekicThe allocation and management of the reference counter is handled by the 317e73145f6SSheldon Hearnsubsystem. 318bfd59721SBosko Milekic.Pp 319bfd59721SBosko MilekicThe system also supplies a default type of external storage buffer called an 3207b2fd183SYaroslav Tykhiy.Vt mbuf cluster . 3217b2fd183SYaroslav Tykhiy.Vt Mbuf clusters 3227b2fd183SYaroslav Tykhiycan be allocated and configured with the use of the 323bfd59721SBosko Milekic.Dv MCLGET 324e73145f6SSheldon Hearnmacro. 3257b2fd183SYaroslav TykhiyEach 3267b2fd183SYaroslav Tykhiy.Vt mbuf cluster 3277b2fd183SYaroslav Tykhiyis 328de06f907SNik Clayton.Dv MCLBYTES 329bfd59721SBosko Milekicin size, where MCLBYTES is a machine-dependent constant. 330bfd59721SBosko MilekicThe system defines an advisory macro 331de06f907SNik Clayton.Dv MINCLSIZE , 3327b2fd183SYaroslav Tykhiywhich is the smallest amount of data to put into an 3337b2fd183SYaroslav Tykhiy.Vt mbuf cluster . 334*2e3ee239SKevin LoIt is equal to 335*2e3ee239SKevin Lo.Dv MHLEN 336*2e3ee239SKevin Loplus one. 3377b2fd183SYaroslav TykhiyIt is typically preferable to store data into the data region of an 3387b2fd183SYaroslav Tykhiy.Vt mbuf , 3397b2fd183SYaroslav Tykhiyif size permits, as opposed to allocating a separate 3407b2fd183SYaroslav Tykhiy.Vt mbuf cluster 3417b2fd183SYaroslav Tykhiyto hold the same data. 342de06f907SNik Clayton.\" 343de06f907SNik Clayton.Ss Macros and Functions 344bfd59721SBosko MilekicThere are numerous predefined macros and functions that provide the 345bfd59721SBosko Milekicdeveloper with common utilities. 346de06f907SNik Clayton.\" 347de06f907SNik Clayton.Bl -ohang -offset indent 348de06f907SNik Clayton.It Fn mtod mbuf type 3497b2fd183SYaroslav TykhiyConvert an 3507b2fd183SYaroslav Tykhiy.Fa mbuf 3517b2fd183SYaroslav Tykhiypointer to a data pointer. 3527b2fd183SYaroslav TykhiyThe macro expands to the data pointer cast to the pointer of the specified 3537b2fd183SYaroslav Tykhiy.Fa type . 354de06f907SNik Clayton.Sy Note : 3557b2fd183SYaroslav TykhiyIt is advisable to ensure that there is enough contiguous data in 3567b2fd183SYaroslav Tykhiy.Fa mbuf . 357de06f907SNik ClaytonSee 358de06f907SNik Clayton.Fn m_pullup 359de06f907SNik Claytonfor details. 360de06f907SNik Clayton.It Fn MGET mbuf how type 3617b2fd183SYaroslav TykhiyAllocate an 3627b2fd183SYaroslav Tykhiy.Vt mbuf 3637b2fd183SYaroslav Tykhiyand initialize it to contain internal data. 36425f1a2e9SSheldon Hearn.Fa mbuf 3657b2fd183SYaroslav Tykhiywill point to the allocated 3667b2fd183SYaroslav Tykhiy.Vt mbuf 3677b2fd183SYaroslav Tykhiyon success, or be set to 368de06f907SNik Clayton.Dv NULL 369e73145f6SSheldon Hearnon failure. 370e73145f6SSheldon HearnThe 371de06f907SNik Clayton.Fa how 372de06f907SNik Claytonargument is to be set to 373fa05d215SGleb Smirnoff.Dv M_WAITOK 374de06f907SNik Claytonor 375fa05d215SGleb Smirnoff.Dv M_NOWAIT . 37625f1a2e9SSheldon HearnIt specifies whether the caller is willing to block if necessary. 3777b2fd183SYaroslav TykhiyA number of other functions and macros related to 3787b2fd183SYaroslav Tykhiy.Vt mbufs 3797b2fd183SYaroslav Tykhiyhave the same argument because they may 3807b2fd183SYaroslav Tykhiyat some point need to allocate new 3817b2fd183SYaroslav Tykhiy.Vt mbufs . 382c65b53c5STom Rhodes.Pp 383fa05d215SGleb SmirnoffHistorical 3847b2fd183SYaroslav Tykhiy.Vt mbuf 385fa05d215SGleb Smirnoffallocator (See 386fa05d215SGleb Smirnoff.Sx HISTORY 387fa05d215SGleb Smirnoffsection) used allocation flags 388fa05d215SGleb Smirnoff.Dv M_WAIT 389fa05d215SGleb Smirnoffand 390fa05d215SGleb Smirnoff.Dv M_DONTWAIT . 391fa05d215SGleb SmirnoffThese constants are kept for compatibility 392fa05d215SGleb Smirnoffand their use in new code is discouraged. 393de06f907SNik Clayton.It Fn MGETHDR mbuf how type 3947b2fd183SYaroslav TykhiyAllocate an 3957b2fd183SYaroslav Tykhiy.Vt mbuf 3967b2fd183SYaroslav Tykhiyand initialize it to contain a packet header 397e73145f6SSheldon Hearnand internal data. 398e73145f6SSheldon HearnSee 399de06f907SNik Clayton.Fn MGET 400de06f907SNik Claytonfor details. 401c3a2dbe9SDag-Erling Smørgrav.It Fn MEXTADD mbuf buf size free opt_arg1 opt_arg2 flags type 402c3a2dbe9SDag-Erling SmørgravAssociate externally managed data with 403c3a2dbe9SDag-Erling Smørgrav.Fa mbuf . 404c3a2dbe9SDag-Erling SmørgravAny internal data contained in the mbuf will be discarded, and the 40573bbeaa5SGlen Barber.Dv M_EXT 40673bbeaa5SGlen Barberflag will be set. 407c3a2dbe9SDag-Erling SmørgravThe 408c3a2dbe9SDag-Erling Smørgrav.Fa buf 409c3a2dbe9SDag-Erling Smørgravand 410c3a2dbe9SDag-Erling Smørgrav.Fa size 411c3a2dbe9SDag-Erling Smørgravarguments are the address and length, respectively, of the data. 412c3a2dbe9SDag-Erling SmørgravThe 413c3a2dbe9SDag-Erling Smørgrav.Fa free 414c3a2dbe9SDag-Erling Smørgravargument points to a function which will be called to free the data 415c3a2dbe9SDag-Erling Smørgravwhen the mbuf is freed; it is only used if 416c3a2dbe9SDag-Erling Smørgrav.Fa type 417c3a2dbe9SDag-Erling Smørgravis 418c3a2dbe9SDag-Erling Smørgrav.Dv EXT_EXTREF . 419c3a2dbe9SDag-Erling SmørgravThe 420c3a2dbe9SDag-Erling Smørgrav.Fa opt_arg1 421c3a2dbe9SDag-Erling Smørgravand 422c3a2dbe9SDag-Erling Smørgrav.Fa opt_arg2 423c3a2dbe9SDag-Erling Smørgravarguments will be passed unmodified to 424c3a2dbe9SDag-Erling Smørgrav.Fa free . 425c3a2dbe9SDag-Erling SmørgravThe 426c3a2dbe9SDag-Erling Smørgrav.Fa flags 427c3a2dbe9SDag-Erling Smørgravargument specifies additional 428c3a2dbe9SDag-Erling Smørgrav.Vt mbuf 429c3a2dbe9SDag-Erling Smørgravflags; it is not necessary to specify 430c3a2dbe9SDag-Erling Smørgrav.Dv M_EXT . 431c3a2dbe9SDag-Erling SmørgravFinally, the 432c3a2dbe9SDag-Erling Smørgrav.Fa type 433c3a2dbe9SDag-Erling Smørgravargument specifies the type of external data, which controls how it 434c3a2dbe9SDag-Erling Smørgravwill be disposed of when the 435c3a2dbe9SDag-Erling Smørgrav.Vt mbuf 436c3a2dbe9SDag-Erling Smørgravis freed. 437c3a2dbe9SDag-Erling SmørgravIn most cases, the correct value is 438c3a2dbe9SDag-Erling Smørgrav.Dv EXT_EXTREF . 439de06f907SNik Clayton.It Fn MCLGET mbuf how 4407b2fd183SYaroslav TykhiyAllocate and attach an 4417b2fd183SYaroslav Tykhiy.Vt mbuf cluster 4427b2fd183SYaroslav Tykhiyto 4437b2fd183SYaroslav Tykhiy.Fa mbuf . 444e73145f6SSheldon HearnIf the macro fails, the 445de06f907SNik Clayton.Dv M_EXT 4460227791bSRuslan Ermilovflag will not be set in 4477b2fd183SYaroslav Tykhiy.Fa mbuf . 448da72b998SYaroslav Tykhiy.It Fn M_ALIGN mbuf len 449da72b998SYaroslav TykhiySet the pointer 450da72b998SYaroslav Tykhiy.Fa mbuf->m_data 451da72b998SYaroslav Tykhiyto place an object of the size 452da72b998SYaroslav Tykhiy.Fa len 453da72b998SYaroslav Tykhiyat the end of the internal data area of 454da72b998SYaroslav Tykhiy.Fa mbuf , 455da72b998SYaroslav Tykhiylong word aligned. 456da72b998SYaroslav TykhiyApplicable only if 457da72b998SYaroslav Tykhiy.Fa mbuf 458da72b998SYaroslav Tykhiyis newly allocated with 459da72b998SYaroslav Tykhiy.Fn MGET 460da72b998SYaroslav Tykhiyor 461da72b998SYaroslav Tykhiy.Fn m_get . 462da72b998SYaroslav Tykhiy.It Fn MH_ALIGN mbuf len 463da72b998SYaroslav TykhiyServes the same purpose as 464da72b998SYaroslav Tykhiy.Fn M_ALIGN 465da72b998SYaroslav Tykhiydoes, but only for 466da72b998SYaroslav Tykhiy.Fa mbuf 467da72b998SYaroslav Tykhiynewly allocated with 468da72b998SYaroslav Tykhiy.Fn MGETHDR 469da72b998SYaroslav Tykhiyor 470da72b998SYaroslav Tykhiy.Fn m_gethdr , 471da72b998SYaroslav Tykhiyor initialized by 472da72b998SYaroslav Tykhiy.Fn m_dup_pkthdr 473da72b998SYaroslav Tykhiyor 474da72b998SYaroslav Tykhiy.Fn m_move_pkthdr . 4759c3fd404SSam Leffler.It Fn m_align mbuf len 4769c3fd404SSam LefflerServices the same purpose as 4779c3fd404SSam Leffler.Fn M_ALIGN 4789c3fd404SSam Lefflerbut handles any type of mbuf. 479da72b998SYaroslav Tykhiy.It Fn M_LEADINGSPACE mbuf 480da72b998SYaroslav TykhiyReturns the number of bytes available before the beginning 481da72b998SYaroslav Tykhiyof data in 482da72b998SYaroslav Tykhiy.Fa mbuf . 483da72b998SYaroslav Tykhiy.It Fn M_TRAILINGSPACE mbuf 484da72b998SYaroslav TykhiyReturns the number of bytes available after the end of data in 485da72b998SYaroslav Tykhiy.Fa mbuf . 486de06f907SNik Clayton.It Fn M_PREPEND mbuf len how 4877b2fd183SYaroslav TykhiyThis macro operates on an 4887b2fd183SYaroslav Tykhiy.Vt mbuf chain . 489de06f907SNik ClaytonIt is an optimized wrapper for 490de06f907SNik Clayton.Fn m_prepend 491de06f907SNik Claytonthat can make use of possible empty space before data 4925203edcdSRuslan Ermilov(e.g.\& left after trimming of a link-layer header). 4937b2fd183SYaroslav TykhiyThe new 4947b2fd183SYaroslav Tykhiy.Vt mbuf chain 4957b2fd183SYaroslav Tykhiypointer or 496de06f907SNik Clayton.Dv NULL 497de06f907SNik Claytonis in 498de06f907SNik Clayton.Fa mbuf 499de06f907SNik Claytonafter the call. 500da72b998SYaroslav Tykhiy.It Fn M_MOVE_PKTHDR to from 501da72b998SYaroslav TykhiyUsing this macro is equivalent to calling 502da72b998SYaroslav Tykhiy.Fn m_move_pkthdr to from . 5035d4050ccSBosko Milekic.It Fn M_WRITABLE mbuf 5047b2fd183SYaroslav TykhiyThis macro will evaluate true if 5057b2fd183SYaroslav Tykhiy.Fa mbuf 5067b2fd183SYaroslav Tykhiyis not marked 5075d4050ccSBosko Milekic.Dv M_RDONLY 5087b2fd183SYaroslav Tykhiyand if either 5097b2fd183SYaroslav Tykhiy.Fa mbuf 5107b2fd183SYaroslav Tykhiydoes not contain external storage or, 5115d4050ccSBosko Milekicif it does, 5125d4050ccSBosko Milekicthen if the reference count of the storage is not greater than 1. 5135d4050ccSBosko MilekicThe 5145d4050ccSBosko Milekic.Dv M_RDONLY 5157b2fd183SYaroslav Tykhiyflag can be set in 5167b2fd183SYaroslav Tykhiy.Fa mbuf->m_flags . 5175d4050ccSBosko MilekicThis can be achieved during setup of the external storage, 5185d4050ccSBosko Milekicby passing the 5195d4050ccSBosko Milekic.Dv M_RDONLY 5205d4050ccSBosko Milekicbit as a 5210b39b3ffSRuslan Ermilov.Fa flags 5225d4050ccSBosko Milekicargument to the 5235d4050ccSBosko Milekic.Fn MEXTADD 5247b2fd183SYaroslav Tykhiymacro, or can be directly set in individual 5257b2fd183SYaroslav Tykhiy.Vt mbufs . 526da72b998SYaroslav Tykhiy.It Fn MCHTYPE mbuf type 527da72b998SYaroslav TykhiyChange the type of 528da72b998SYaroslav Tykhiy.Fa mbuf 529da72b998SYaroslav Tykhiyto 530da72b998SYaroslav Tykhiy.Fa type . 531da72b998SYaroslav TykhiyThis is a relatively expensive operation and should be avoided. 532de06f907SNik Clayton.El 533de06f907SNik Clayton.Pp 534de06f907SNik ClaytonThe functions are: 535de06f907SNik Clayton.Bl -ohang -offset indent 536de06f907SNik Clayton.It Fn m_get how type 537de06f907SNik ClaytonA function version of 538bfd59721SBosko Milekic.Fn MGET 539bfd59721SBosko Milekicfor non-critical paths. 540328c0bbcSBosko Milekic.It Fn m_getm orig len how type 541328c0bbcSBosko MilekicAllocate 5420b39b3ffSRuslan Ermilov.Fa len 5437b2fd183SYaroslav Tykhiybytes worth of 5447b2fd183SYaroslav Tykhiy.Vt mbufs 5457b2fd183SYaroslav Tykhiyand 5467b2fd183SYaroslav Tykhiy.Vt mbuf clusters 5477b2fd183SYaroslav Tykhiyif necessary and append the resulting allocated 5487b2fd183SYaroslav Tykhiy.Vt mbuf chain 5497b2fd183SYaroslav Tykhiyto the 5507b2fd183SYaroslav Tykhiy.Vt mbuf chain 5517b2fd183SYaroslav Tykhiy.Fa orig , 5527b2fd183SYaroslav Tykhiyif it is 5530b39b3ffSRuslan Ermilov.No non- Ns Dv NULL . 554328c0bbcSBosko MilekicIf the allocation fails at any point, 5550b39b3ffSRuslan Ermilovfree whatever was allocated and return 5560b39b3ffSRuslan Ermilov.Dv NULL . 557328c0bbcSBosko MilekicIf 5580b39b3ffSRuslan Ermilov.Fa orig 5590b39b3ffSRuslan Ermilovis 5600b39b3ffSRuslan Ermilov.No non- Ns Dv NULL , 561328c0bbcSBosko Milekicit will not be freed. 562328c0bbcSBosko MilekicIt is possible to use 563328c0bbcSBosko Milekic.Fn m_getm 564328c0bbcSBosko Milekicto either append 5650b39b3ffSRuslan Ermilov.Fa len 5667b2fd183SYaroslav Tykhiybytes to an existing 5677b2fd183SYaroslav Tykhiy.Vt mbuf 5687b2fd183SYaroslav Tykhiyor 5697b2fd183SYaroslav Tykhiy.Vt mbuf chain 570328c0bbcSBosko Milekic(for example, one which may be sitting in a pre-allocated ring) 5717b2fd183SYaroslav Tykhiyor to simply perform an all-or-nothing 5727b2fd183SYaroslav Tykhiy.Vt mbuf 5737b2fd183SYaroslav Tykhiyand 5747b2fd183SYaroslav Tykhiy.Vt mbuf cluster 5757b2fd183SYaroslav Tykhiyallocation. 576de06f907SNik Clayton.It Fn m_gethdr how type 577de06f907SNik ClaytonA function version of 578bfd59721SBosko Milekic.Fn MGETHDR 579bfd59721SBosko Milekicfor non-critical paths. 5806c013706SMaxim Konovalov.It Fn m_getcl how type flags 5816c013706SMaxim KonovalovFetch an 5826c013706SMaxim Konovalov.Vt mbuf 5836c013706SMaxim Konovalovwith a 5846c013706SMaxim Konovalov.Vt mbuf cluster 5856c013706SMaxim Konovalovattached to it. 5866c013706SMaxim KonovalovIf one of the allocations fails, the entire allocation fails. 5876c013706SMaxim KonovalovThis routine is the preferred way of fetching both the 5886c013706SMaxim Konovalov.Vt mbuf 5896c013706SMaxim Konovalovand 5906c013706SMaxim Konovalov.Vt mbuf cluster 5916c013706SMaxim Konovalovtogether, as it avoids having to unlock/relock between allocations. 5926c013706SMaxim KonovalovReturns 5936c013706SMaxim Konovalov.Dv NULL 5946c013706SMaxim Konovalovon failure. 595de06f907SNik Clayton.It Fn m_getclr how type 5967b2fd183SYaroslav TykhiyAllocate an 5977b2fd183SYaroslav Tykhiy.Vt mbuf 5987b2fd183SYaroslav Tykhiyand zero out the data region. 59901e7fdafSMaxim Konovalov.It Fn m_free mbuf 60001e7fdafSMaxim KonovalovFrees 60101e7fdafSMaxim Konovalov.Vt mbuf . 602b7131a26SJohn-Mark GurneyReturns 603b7131a26SJohn-Mark Gurney.Va m_next 604b7131a26SJohn-Mark Gurneyof the freed 605b7131a26SJohn-Mark Gurney.Vt mbuf . 606de06f907SNik Clayton.El 607de06f907SNik Clayton.Pp 6087b2fd183SYaroslav TykhiyThe functions below operate on 6097b2fd183SYaroslav Tykhiy.Vt mbuf chains . 610de06f907SNik Clayton.Bl -ohang -offset indent 611de06f907SNik Clayton.It Fn m_freem mbuf 6127b2fd183SYaroslav TykhiyFree an entire 6137b2fd183SYaroslav Tykhiy.Vt mbuf chain , 6147b2fd183SYaroslav Tykhiyincluding any external storage. 615de06f907SNik Clayton.\" 616de06f907SNik Clayton.It Fn m_adj mbuf len 617de06f907SNik ClaytonTrim 618de06f907SNik Clayton.Fa len 6197b2fd183SYaroslav Tykhiybytes from the head of an 6207b2fd183SYaroslav Tykhiy.Vt mbuf chain 6217b2fd183SYaroslav Tykhiyif 622de06f907SNik Clayton.Fa len 623de06f907SNik Claytonis positive, from the tail otherwise. 624de06f907SNik Clayton.\" 625b9def06bSSam Leffler.It Fn m_append mbuf len cp 626b9def06bSSam LefflerAppend 627b9def06bSSam Leffler.Vt len 628b9def06bSSam Lefflerbytes of data 629b9def06bSSam Leffler.Vt cp 630b9def06bSSam Lefflerto the 631b9def06bSSam Leffler.Vt mbuf chain . 632b9def06bSSam LefflerExtend the mbuf chain if the new data does not fit in 633b9def06bSSam Lefflerexisting space. 634b9def06bSSam Leffler.\" 635de06f907SNik Clayton.It Fn m_prepend mbuf len how 6367b2fd183SYaroslav TykhiyAllocate a new 6377b2fd183SYaroslav Tykhiy.Vt mbuf 6387b2fd183SYaroslav Tykhiyand prepend it to the 6397b2fd183SYaroslav Tykhiy.Vt mbuf chain , 6407b2fd183SYaroslav Tykhiyhandle 641de06f907SNik Clayton.Dv M_PKTHDR 642de06f907SNik Claytonproperly. 643de06f907SNik Clayton.Sy Note : 6440227791bSRuslan ErmilovIt does not allocate any 6457b2fd183SYaroslav Tykhiy.Vt mbuf clusters , 6467b2fd183SYaroslav Tykhiyso 647de06f907SNik Clayton.Fa len 648de06f907SNik Claytonmust be less than 649de06f907SNik Clayton.Dv MLEN 650de06f907SNik Claytonor 651de06f907SNik Clayton.Dv MHLEN , 652de06f907SNik Claytondepending on the 653d0353b83SRuslan Ermilov.Dv M_PKTHDR 654d0353b83SRuslan Ermilovflag setting. 655de06f907SNik Clayton.\" 6567ac139a9SJohn-Mark Gurney.It Fn m_copyup mbuf len dstoff 6577ac139a9SJohn-Mark GurneySimilar to 6587ac139a9SJohn-Mark Gurney.Fn m_pullup 6597ac139a9SJohn-Mark Gurneybut copies 6607ac139a9SJohn-Mark Gurney.Fa len 6617ac139a9SJohn-Mark Gurneybytes of data into a new mbuf at 6627ac139a9SJohn-Mark Gurney.Fa dstoff 6637ac139a9SJohn-Mark Gurneybytes into the mbuf. 6647ac139a9SJohn-Mark GurneyThe 6657ac139a9SJohn-Mark Gurney.Fa dstoff 6667ac139a9SJohn-Mark Gurneyargument aligns the data and leaves room for a link layer header. 6674f068961SRuslan ErmilovReturns the new 6687ac139a9SJohn-Mark Gurney.Vt mbuf chain 6697ac139a9SJohn-Mark Gurneyon success, 6707ac139a9SJohn-Mark Gurneyand frees the 6717ac139a9SJohn-Mark Gurney.Vt mbuf chain 6727ac139a9SJohn-Mark Gurneyand returns 6737ac139a9SJohn-Mark Gurney.Dv NULL 6747ac139a9SJohn-Mark Gurneyon failure. 6757ac139a9SJohn-Mark Gurney.Sy Note : 6767ac139a9SJohn-Mark GurneyThe function does not allocate 6777ac139a9SJohn-Mark Gurney.Vt mbuf clusters , 6787ac139a9SJohn-Mark Gurneyso 6797ac139a9SJohn-Mark Gurney.Fa len + dstoff 6807ac139a9SJohn-Mark Gurneymust be less than 6817ac139a9SJohn-Mark Gurney.Dv MHLEN . 6827ac139a9SJohn-Mark Gurney.\" 683de06f907SNik Clayton.It Fn m_pullup mbuf len 684de06f907SNik ClaytonArrange that the first 685de06f907SNik Clayton.Fa len 6867b2fd183SYaroslav Tykhiybytes of an 6877b2fd183SYaroslav Tykhiy.Vt mbuf chain 6887b2fd183SYaroslav Tykhiyare contiguous and lay in the data area of 689de06f907SNik Clayton.Fa mbuf , 690de06f907SNik Claytonso they are accessible with 691de06f907SNik Clayton.Fn mtod mbuf type . 6921009bd1bSJulian ElischerIt is important to remember that this may involve 6931009bd1bSJulian Elischerreallocating some mbufs and moving data so all pointers 6941009bd1bSJulian Elischerreferencing data within the old mbuf chain 6951009bd1bSJulian Elischermust be recalculated or made invalid. 6967b2fd183SYaroslav TykhiyReturn the new 6977b2fd183SYaroslav Tykhiy.Vt mbuf chain 6987b2fd183SYaroslav Tykhiyon success, 699de06f907SNik Clayton.Dv NULL 700de06f907SNik Claytonon failure 7017b2fd183SYaroslav Tykhiy(the 7027b2fd183SYaroslav Tykhiy.Vt mbuf chain 7037b2fd183SYaroslav Tykhiyis freed in this case). 704de06f907SNik Clayton.Sy Note : 7050227791bSRuslan ErmilovIt does not allocate any 7067b2fd183SYaroslav Tykhiy.Vt mbuf clusters , 7077b2fd183SYaroslav Tykhiyso 708de06f907SNik Clayton.Fa len 709de06f907SNik Claytonmust be less than 710de06f907SNik Clayton.Dv MHLEN . 711de06f907SNik Clayton.\" 712d305f4b9SBruce M Simpson.It Fn m_pulldown mbuf offset len offsetp 713d305f4b9SBruce M SimpsonArrange that 714d305f4b9SBruce M Simpson.Fa len 715d305f4b9SBruce M Simpsonbytes between 716d305f4b9SBruce M Simpson.Fa offset 717d305f4b9SBruce M Simpsonand 718d305f4b9SBruce M Simpson.Fa offset + len 719d305f4b9SBruce M Simpsonin the 720d305f4b9SBruce M Simpson.Vt mbuf chain 721d305f4b9SBruce M Simpsonare contiguous and lay in the data area of 722d305f4b9SBruce M Simpson.Fa mbuf , 723d305f4b9SBruce M Simpsonso they are accessible with 724d305f4b9SBruce M Simpson.Fn mtod mbuf type . 72573bbeaa5SGlen Barber.Fa len 72673bbeaa5SGlen Barbermust be smaller than, or equal to, the size of an 727d305f4b9SBruce M Simpson.Vt mbuf cluster . 728d305f4b9SBruce M SimpsonReturn a pointer to an intermediate 729d305f4b9SBruce M Simpson.Vt mbuf 730d305f4b9SBruce M Simpsonin the chain containing the requested region; 731d305f4b9SBruce M Simpsonthe offset in the data region of the 732d305f4b9SBruce M Simpson.Vt mbuf chain 733d305f4b9SBruce M Simpsonto the data contained in the returned mbuf is stored in 734d305f4b9SBruce M Simpson.Fa *offsetp . 735d305f4b9SBruce M SimpsonIf 736d305f4b9SBruce M Simpson.Fa offp 737d305f4b9SBruce M Simpsonis NULL, the region may be accessed using 738d305f4b9SBruce M Simpson.Fn mtod mbuf type . 739d305f4b9SBruce M SimpsonIf 740d305f4b9SBruce M Simpson.Fa offp 741d305f4b9SBruce M Simpsonis non-NULL, the region may be accessed using 742d305f4b9SBruce M Simpson.Fn mtod mbuf uint8_t + *offsetp . 743d305f4b9SBruce M SimpsonThe region of the mbuf chain between its beginning and 744d305f4b9SBruce M Simpson.Fa off 745d305f4b9SBruce M Simpsonis not modified, therefore it is safe to hold pointers to data within 746d305f4b9SBruce M Simpsonthis region before calling 747d305f4b9SBruce M Simpson.Fn m_pulldown . 748d305f4b9SBruce M Simpson.\" 749de06f907SNik Clayton.It Fn m_copym mbuf offset len how 7507b2fd183SYaroslav TykhiyMake a copy of an 7517b2fd183SYaroslav Tykhiy.Vt mbuf chain 7527b2fd183SYaroslav Tykhiystarting 753de06f907SNik Clayton.Fa offset 754de06f907SNik Claytonbytes from the beginning, continuing for 755de06f907SNik Clayton.Fa len 756e73145f6SSheldon Hearnbytes. 757e73145f6SSheldon HearnIf 758de06f907SNik Clayton.Fa len 759de06f907SNik Claytonis 760de06f907SNik Clayton.Dv M_COPYALL , 7617b2fd183SYaroslav Tykhiycopy to the end of the 7627b2fd183SYaroslav Tykhiy.Vt mbuf chain . 763de06f907SNik Clayton.Sy Note : 7647b2fd183SYaroslav TykhiyThe copy is read-only, because the 7657b2fd183SYaroslav Tykhiy.Vt mbuf clusters 7667b2fd183SYaroslav Tykhiyare not copied, only their reference counts are incremented. 767de06f907SNik Clayton.\" 768de06f907SNik Clayton.It Fn m_copypacket mbuf how 769de06f907SNik ClaytonCopy an entire packet including header, which must be present. 770de06f907SNik ClaytonThis is an optimized version of the common case 771de06f907SNik Clayton.Fn m_copym mbuf 0 M_COPYALL how . 772de06f907SNik Clayton.Sy Note : 7737b2fd183SYaroslav Tykhiythe copy is read-only, because the 7747b2fd183SYaroslav Tykhiy.Vt mbuf clusters 7757b2fd183SYaroslav Tykhiyare not copied, only their reference counts are incremented. 776de06f907SNik Clayton.\" 777de06f907SNik Clayton.It Fn m_dup mbuf how 7787b2fd183SYaroslav TykhiyCopy a packet header 7797b2fd183SYaroslav Tykhiy.Vt mbuf chain 7807b2fd183SYaroslav Tykhiyinto a completely new 7817b2fd183SYaroslav Tykhiy.Vt mbuf chain , 7827b2fd183SYaroslav Tykhiyincluding copying any 7837b2fd183SYaroslav Tykhiy.Vt mbuf clusters . 784e73145f6SSheldon HearnUse this instead of 785de06f907SNik Clayton.Fn m_copypacket 7867b2fd183SYaroslav Tykhiywhen you need a writable copy of an 7877b2fd183SYaroslav Tykhiy.Vt mbuf chain . 788de06f907SNik Clayton.\" 789de06f907SNik Clayton.It Fn m_copydata mbuf offset len buf 7907b2fd183SYaroslav TykhiyCopy data from an 7917b2fd183SYaroslav Tykhiy.Vt mbuf chain 7927b2fd183SYaroslav Tykhiystarting 793de06f907SNik Clayton.Fa off 794de06f907SNik Claytonbytes from the beginning, continuing for 795de06f907SNik Clayton.Fa len 796de06f907SNik Claytonbytes, into the indicated buffer 797de06f907SNik Clayton.Fa buf . 798de06f907SNik Clayton.\" 799de06f907SNik Clayton.It Fn m_copyback mbuf offset len buf 800de06f907SNik ClaytonCopy 801de06f907SNik Clayton.Fa len 802de06f907SNik Claytonbytes from the buffer 803de06f907SNik Clayton.Fa buf 8047b2fd183SYaroslav Tykhiyback into the indicated 8057b2fd183SYaroslav Tykhiy.Vt mbuf chain , 806de06f907SNik Claytonstarting at 807de06f907SNik Clayton.Fa offset 8087b2fd183SYaroslav Tykhiybytes from the beginning of the 8097b2fd183SYaroslav Tykhiy.Vt mbuf chain , 8107b2fd183SYaroslav Tykhiyextending the 8117b2fd183SYaroslav Tykhiy.Vt mbuf chain 8127b2fd183SYaroslav Tykhiyif necessary. 813de06f907SNik Clayton.Sy Note : 8140227791bSRuslan ErmilovIt does not allocate any 8157b2fd183SYaroslav Tykhiy.Vt mbuf clusters , 8167b2fd183SYaroslav Tykhiyjust adds 8177b2fd183SYaroslav Tykhiy.Vt mbufs 8187b2fd183SYaroslav Tykhiyto the 8197b2fd183SYaroslav Tykhiy.Vt mbuf chain . 82036a142c4SRuslan ErmilovIt is safe to set 821de06f907SNik Clayton.Fa offset 8227b2fd183SYaroslav Tykhiybeyond the current 8237b2fd183SYaroslav Tykhiy.Vt mbuf chain 8247b2fd183SYaroslav Tykhiyend: zeroed 8257b2fd183SYaroslav Tykhiy.Vt mbufs 8267b2fd183SYaroslav Tykhiywill be allocated to fill the space. 827de06f907SNik Clayton.\" 8285d96084fSYaroslav Tykhiy.It Fn m_length mbuf last 8297b2fd183SYaroslav TykhiyReturn the length of the 8307b2fd183SYaroslav Tykhiy.Vt mbuf chain , 8317b2fd183SYaroslav Tykhiyand optionally a pointer to the last 8327b2fd183SYaroslav Tykhiy.Vt mbuf . 8330b1c0994SPoul-Henning Kamp.\" 834da72b998SYaroslav Tykhiy.It Fn m_dup_pkthdr to from how 835da72b998SYaroslav TykhiyUpon the function's completion, the 836da72b998SYaroslav Tykhiy.Vt mbuf 837da72b998SYaroslav Tykhiy.Fa to 838da72b998SYaroslav Tykhiywill contain an identical copy of 839da72b998SYaroslav Tykhiy.Fa from->m_pkthdr 840da72b998SYaroslav Tykhiyand the per-packet attributes found in the 841da72b998SYaroslav Tykhiy.Vt mbuf chain 842da72b998SYaroslav Tykhiy.Fa from . 843da72b998SYaroslav TykhiyThe 844da72b998SYaroslav Tykhiy.Vt mbuf 845da72b998SYaroslav Tykhiy.Fa from 846da72b998SYaroslav Tykhiymust have the flag 847da72b998SYaroslav Tykhiy.Dv M_PKTHDR 848da72b998SYaroslav Tykhiyinitially set, and 849da72b998SYaroslav Tykhiy.Fa to 850da72b998SYaroslav Tykhiymust be empty on entry. 851da72b998SYaroslav Tykhiy.\" 852da72b998SYaroslav Tykhiy.It Fn m_move_pkthdr to from 853da72b998SYaroslav TykhiyMove 854da72b998SYaroslav Tykhiy.Va m_pkthdr 855da72b998SYaroslav Tykhiyand the per-packet attributes from the 856da72b998SYaroslav Tykhiy.Vt mbuf chain 857da72b998SYaroslav Tykhiy.Fa from 858da72b998SYaroslav Tykhiyto the 859da72b998SYaroslav Tykhiy.Vt mbuf 860da72b998SYaroslav Tykhiy.Fa to . 861da72b998SYaroslav TykhiyThe 862da72b998SYaroslav Tykhiy.Vt mbuf 863da72b998SYaroslav Tykhiy.Fa from 864da72b998SYaroslav Tykhiymust have the flag 865da72b998SYaroslav Tykhiy.Dv M_PKTHDR 866da72b998SYaroslav Tykhiyinitially set, and 867da72b998SYaroslav Tykhiy.Fa to 868da72b998SYaroslav Tykhiymust be empty on entry. 869da72b998SYaroslav TykhiyUpon the function's completion, 870da72b998SYaroslav Tykhiy.Fa from 871da72b998SYaroslav Tykhiywill have the flag 872da72b998SYaroslav Tykhiy.Dv M_PKTHDR 873da72b998SYaroslav Tykhiyand the per-packet attributes cleared. 874da72b998SYaroslav Tykhiy.\" 8755d96084fSYaroslav Tykhiy.It Fn m_fixhdr mbuf 8767b2fd183SYaroslav TykhiySet the packet-header length to the length of the 8777b2fd183SYaroslav Tykhiy.Vt mbuf chain . 8780b1c0994SPoul-Henning Kamp.\" 879de06f907SNik Clayton.It Fn m_devget buf len offset ifp copy 880de06f907SNik ClaytonCopy data from a device local memory pointed to by 881de06f907SNik Clayton.Fa buf 8827b2fd183SYaroslav Tykhiyto an 8837b2fd183SYaroslav Tykhiy.Vt mbuf chain . 884e73145f6SSheldon HearnThe copy is done using a specified copy routine 885de06f907SNik Clayton.Fa copy , 886de06f907SNik Claytonor 887de06f907SNik Clayton.Fn bcopy 888de06f907SNik Claytonif 889de06f907SNik Clayton.Fa copy 890de06f907SNik Claytonis 891de06f907SNik Clayton.Dv NULL . 892de06f907SNik Clayton.\" 893de06f907SNik Clayton.It Fn m_cat m n 894de06f907SNik ClaytonConcatenate 895de06f907SNik Clayton.Fa n 896de06f907SNik Claytonto 897de06f907SNik Clayton.Fa m . 8987b2fd183SYaroslav TykhiyBoth 8997b2fd183SYaroslav Tykhiy.Vt mbuf chains 9007b2fd183SYaroslav Tykhiymust be of the same type. 901de06f907SNik Clayton.Fa N 902de06f907SNik Claytonis still valid after the function returned. 903de06f907SNik Clayton.Sy Note : 904de06f907SNik ClaytonIt does not handle 905de06f907SNik Clayton.Dv M_PKTHDR 906de06f907SNik Claytonand friends. 907de06f907SNik Clayton.\" 908de06f907SNik Clayton.It Fn m_split mbuf len how 9097b2fd183SYaroslav TykhiyPartition an 9107b2fd183SYaroslav Tykhiy.Vt mbuf chain 9117b2fd183SYaroslav Tykhiyin two pieces, returning the tail: 912de06f907SNik Claytonall but the first 913de06f907SNik Clayton.Fa len 914e73145f6SSheldon Hearnbytes. 915e73145f6SSheldon HearnIn case of failure, it returns 916de06f907SNik Clayton.Dv NULL 9177b2fd183SYaroslav Tykhiyand attempts to restore the 9187b2fd183SYaroslav Tykhiy.Vt mbuf chain 9197b2fd183SYaroslav Tykhiyto its original state. 92002312219SBruce M Simpson.\" 92102312219SBruce M Simpson.It Fn m_apply mbuf off len f arg 92202312219SBruce M SimpsonApply a function to an 92302312219SBruce M Simpson.Vt mbuf chain , 92402312219SBruce M Simpsonat offset 92502312219SBruce M Simpson.Fa off , 92602312219SBruce M Simpsonfor length 927f48cbcdeSRuslan Ermilov.Fa len 928f48cbcdeSRuslan Ermilovbytes. 92902312219SBruce M SimpsonTypically used to avoid calls to 93002312219SBruce M Simpson.Fn m_pullup 93102312219SBruce M Simpsonwhich would otherwise be unnecessary or undesirable. 93202312219SBruce M Simpson.Fa arg 93302312219SBruce M Simpsonis a convenience argument which is passed to the callback function 93402312219SBruce M Simpson.Fa f . 93502312219SBruce M Simpson.Pp 93602312219SBruce M SimpsonEach time 93702312219SBruce M Simpson.Fn f 93802312219SBruce M Simpsonis called, it will be passed 93902312219SBruce M Simpson.Fa arg , 94002312219SBruce M Simpsona pointer to the 94102312219SBruce M Simpson.Fa data 94202312219SBruce M Simpsonin the current mbuf, and the length 94302312219SBruce M Simpson.Fa len 94402312219SBruce M Simpsonof the data in this mbuf to which the function should be applied. 94502312219SBruce M Simpson.Pp 94602312219SBruce M SimpsonThe function should return zero to indicate success; 94702312219SBruce M Simpsonotherwise, if an error is indicated, then 94802312219SBruce M Simpson.Fn m_apply 94902312219SBruce M Simpsonwill return the error and stop iterating through the 95002312219SBruce M Simpson.Vt mbuf chain . 95102312219SBruce M Simpson.\" 95202312219SBruce M Simpson.It Fn m_getptr mbuf loc off 95302312219SBruce M SimpsonReturn a pointer to the mbuf containing the data located at 95402312219SBruce M Simpson.Fa loc 95502312219SBruce M Simpsonbytes from the beginning of the 95602312219SBruce M Simpson.Vt mbuf chain . 95702312219SBruce M SimpsonThe corresponding offset into the mbuf will be stored in 95802312219SBruce M Simpson.Fa *off . 959a61bc764SMike Silbersack.It Fn m_defrag m0 how 960f48cbcdeSRuslan ErmilovDefragment an mbuf chain, returning the shortest possible 961bdf86185SMike Silbersackchain of mbufs and clusters. 962bdf86185SMike SilbersackIf allocation fails and this can not be completed, 963bdf86185SMike Silbersack.Dv NULL 964bdf86185SMike Silbersackwill be returned and the original chain will be unchanged. 965bdf86185SMike SilbersackUpon success, the original chain will be freed and the new 966bdf86185SMike Silbersackchain will be returned. 967a61bc764SMike Silbersack.Fa how 968a61bc764SMike Silbersackshould be either 969fa05d215SGleb Smirnoff.Dv M_WAITOK 970a61bc764SMike Silbersackor 971fa05d215SGleb Smirnoff.Dv M_NOWAIT , 972a61bc764SMike Silbersackdepending on the caller's preference. 973a61bc764SMike Silbersack.Pp 974a61bc764SMike SilbersackThis function is especially useful in network drivers, where 975a61bc764SMike Silbersackcertain long mbuf chains must be shortened before being added 976a61bc764SMike Silbersackto TX descriptor lists. 97747e2996eSSam Leffler.It Fn m_unshare m0 how 97847e2996eSSam LefflerCreate a version of the specified mbuf chain whose 97947e2996eSSam Lefflercontents can be safely modified without affecting other users. 98047e2996eSSam LefflerIf allocation fails and this operation can not be completed, 98147e2996eSSam Leffler.Dv NULL 98247e2996eSSam Lefflerwill be returned. 98347e2996eSSam LefflerThe original mbuf chain is always reclaimed and the reference 98447e2996eSSam Lefflercount of any shared mbuf clusters is decremented. 98547e2996eSSam Leffler.Fa how 98647e2996eSSam Lefflershould be either 987fa05d215SGleb Smirnoff.Dv M_WAITOK 98847e2996eSSam Leffleror 989fa05d215SGleb Smirnoff.Dv M_NOWAIT , 99047e2996eSSam Lefflerdepending on the caller's preference. 99147e2996eSSam LefflerAs a side-effect of this process the returned 99247e2996eSSam Lefflermbuf chain may be compacted. 99347e2996eSSam Leffler.Pp 99447e2996eSSam LefflerThis function is especially useful in the transmit path of 99547e2996eSSam Lefflernetwork code, when data must be encrypted or otherwise 99647e2996eSSam Leffleraltered prior to transmission. 9973136363fSRuslan Ermilov.El 9980193eb5eSYaroslav Tykhiy.Sh HARDWARE-ASSISTED CHECKSUM CALCULATION 9990193eb5eSYaroslav TykhiyThis section currently applies to TCP/IP only. 10000193eb5eSYaroslav TykhiyIn order to save the host CPU resources, computing checksums is 10010193eb5eSYaroslav Tykhiyoffloaded to the network interface hardware if possible. 10020193eb5eSYaroslav TykhiyThe 10030193eb5eSYaroslav Tykhiy.Va m_pkthdr 10040193eb5eSYaroslav Tykhiymember of the leading 10050193eb5eSYaroslav Tykhiy.Vt mbuf 10060193eb5eSYaroslav Tykhiyof a packet contains two fields used for that purpose, 10070193eb5eSYaroslav Tykhiy.Vt int Va csum_flags 10080193eb5eSYaroslav Tykhiyand 10090193eb5eSYaroslav Tykhiy.Vt int Va csum_data . 10100193eb5eSYaroslav TykhiyThe meaning of those fields depends on the direction a packet flows in, 10110193eb5eSYaroslav Tykhiyand on whether the packet is fragmented. 10120193eb5eSYaroslav TykhiyHenceforth, 10130193eb5eSYaroslav Tykhiy.Va csum_flags 10140193eb5eSYaroslav Tykhiyor 10150193eb5eSYaroslav Tykhiy.Va csum_data 10160193eb5eSYaroslav Tykhiyof a packet 10170193eb5eSYaroslav Tykhiywill denote the corresponding field of the 10180193eb5eSYaroslav Tykhiy.Va m_pkthdr 10190193eb5eSYaroslav Tykhiymember of the leading 10200193eb5eSYaroslav Tykhiy.Vt mbuf 10210193eb5eSYaroslav Tykhiyin the 10220193eb5eSYaroslav Tykhiy.Vt mbuf chain 10230193eb5eSYaroslav Tykhiycontaining the packet. 10240193eb5eSYaroslav Tykhiy.Pp 10250193eb5eSYaroslav TykhiyOn output, checksum offloading is attempted after the outgoing 10260193eb5eSYaroslav Tykhiyinterface has been determined for a packet. 10270193eb5eSYaroslav TykhiyThe interface-specific field 10280193eb5eSYaroslav Tykhiy.Va ifnet.if_data.ifi_hwassist 10290193eb5eSYaroslav Tykhiy(see 10300193eb5eSYaroslav Tykhiy.Xr ifnet 9 ) 10310193eb5eSYaroslav Tykhiyis consulted for the capabilities of the interface to assist in 10320193eb5eSYaroslav Tykhiycomputing checksums. 10330193eb5eSYaroslav TykhiyThe 10340193eb5eSYaroslav Tykhiy.Va csum_flags 1035f48cbcdeSRuslan Ermilovfield of the packet header is set to indicate which actions the interface 10360193eb5eSYaroslav Tykhiyis supposed to perform on it. 10370193eb5eSYaroslav TykhiyThe actions unsupported by the network interface are done in the 10380193eb5eSYaroslav Tykhiysoftware prior to passing the packet down to the interface driver; 10390193eb5eSYaroslav Tykhiysuch actions will never be requested through 10400193eb5eSYaroslav Tykhiy.Va csum_flags . 10410193eb5eSYaroslav Tykhiy.Pp 10420193eb5eSYaroslav TykhiyThe flags demanding a particular action from an interface are as follows: 10430193eb5eSYaroslav Tykhiy.Bl -tag -width ".Dv CSUM_TCP" -offset indent 10440193eb5eSYaroslav Tykhiy.It Dv CSUM_IP 10450193eb5eSYaroslav TykhiyThe IP header checksum is to be computed and stored in the 10460193eb5eSYaroslav Tykhiycorresponding field of the packet. 10470193eb5eSYaroslav TykhiyThe hardware is expected to know the format of an IP header 10480193eb5eSYaroslav Tykhiyto determine the offset of the IP checksum field. 10490193eb5eSYaroslav Tykhiy.It Dv CSUM_TCP 1050f48cbcdeSRuslan ErmilovThe TCP checksum is to be computed. 1051f48cbcdeSRuslan Ermilov(See below.) 10520193eb5eSYaroslav Tykhiy.It Dv CSUM_UDP 1053f48cbcdeSRuslan ErmilovThe UDP checksum is to be computed. 1054f48cbcdeSRuslan Ermilov(See below.) 10550193eb5eSYaroslav Tykhiy.El 10560193eb5eSYaroslav Tykhiy.Pp 1057fbde705aSYaroslav TykhiyShould a TCP or UDP checksum be offloaded to the hardware, 10580193eb5eSYaroslav Tykhiythe field 10590193eb5eSYaroslav Tykhiy.Va csum_data 10600193eb5eSYaroslav Tykhiywill contain the byte offset of the checksum field relative to the 10610193eb5eSYaroslav Tykhiyend of the IP header. 10620193eb5eSYaroslav TykhiyIn this case, the checksum field will be initially 10630193eb5eSYaroslav Tykhiyset by the TCP/IP module to the checksum of the pseudo header 1064fbde705aSYaroslav Tykhiydefined by the TCP and UDP specifications. 10650193eb5eSYaroslav Tykhiy.Pp 10660193eb5eSYaroslav TykhiyFor outbound packets which have been fragmented 10670193eb5eSYaroslav Tykhiyby the host CPU, the following will also be true, 10680193eb5eSYaroslav Tykhiyregardless of the checksum flag settings: 10690193eb5eSYaroslav Tykhiy.Bl -bullet -offset indent 10700193eb5eSYaroslav Tykhiy.It 10710193eb5eSYaroslav Tykhiyall fragments will have the flag 10720193eb5eSYaroslav Tykhiy.Dv M_FRAG 10730193eb5eSYaroslav Tykhiyset in their 10740193eb5eSYaroslav Tykhiy.Va m_flags 10750193eb5eSYaroslav Tykhiyfield; 10760193eb5eSYaroslav Tykhiy.It 10770193eb5eSYaroslav Tykhiythe first and the last fragments in the chain will have 10780193eb5eSYaroslav Tykhiy.Dv M_FIRSTFRAG 10790193eb5eSYaroslav Tykhiyor 10800193eb5eSYaroslav Tykhiy.Dv M_LASTFRAG 10810193eb5eSYaroslav Tykhiyset in their 10820193eb5eSYaroslav Tykhiy.Va m_flags , 10830193eb5eSYaroslav Tykhiycorrespondingly; 10840193eb5eSYaroslav Tykhiy.It 10850193eb5eSYaroslav Tykhiythe first fragment in the chain will have the total number 10860193eb5eSYaroslav Tykhiyof fragments contained in its 10870193eb5eSYaroslav Tykhiy.Va csum_data 10880193eb5eSYaroslav Tykhiyfield. 10890193eb5eSYaroslav Tykhiy.El 10900193eb5eSYaroslav Tykhiy.Pp 109176f80df6SYaroslav TykhiyThe last rule for fragmented packets takes precedence over the one 109276f80df6SYaroslav Tykhiyfor a TCP or UDP checksum. 109376f80df6SYaroslav TykhiyNevertheless, offloading a TCP or UDP checksum is possible for a 109476f80df6SYaroslav Tykhiyfragmented packet if the flag 109576f80df6SYaroslav Tykhiy.Dv CSUM_IP_FRAGS 109676f80df6SYaroslav Tykhiyis set in the field 109776f80df6SYaroslav Tykhiy.Va ifnet.if_data.ifi_hwassist 109876f80df6SYaroslav Tykhiyassociated with the network interface. 109976f80df6SYaroslav TykhiyHowever, in this case the interface is expected to figure out 110076f80df6SYaroslav Tykhiythe location of the checksum field within the sequence of fragments 110176f80df6SYaroslav Tykhiyby itself because 11020193eb5eSYaroslav Tykhiy.Va csum_data 110376f80df6SYaroslav Tykhiycontains a fragment count instead of a checksum offset value. 11040193eb5eSYaroslav Tykhiy.Pp 11050193eb5eSYaroslav TykhiyOn input, an interface indicates the actions it has performed 11060193eb5eSYaroslav Tykhiyon a packet by setting one or more of the following flags in 11070193eb5eSYaroslav Tykhiy.Va csum_flags 11080193eb5eSYaroslav Tykhiyassociated with the packet: 11090193eb5eSYaroslav Tykhiy.Bl -tag -width ".Dv CSUM_IP_CHECKED" -offset indent 11100193eb5eSYaroslav Tykhiy.It Dv CSUM_IP_CHECKED 11110193eb5eSYaroslav TykhiyThe IP header checksum has been computed. 11120193eb5eSYaroslav Tykhiy.It Dv CSUM_IP_VALID 11130193eb5eSYaroslav TykhiyThe IP header has a valid checksum. 11140193eb5eSYaroslav TykhiyThis flag can appear only in combination with 11150193eb5eSYaroslav Tykhiy.Dv CSUM_IP_CHECKED . 11160193eb5eSYaroslav Tykhiy.It Dv CSUM_DATA_VALID 11170193eb5eSYaroslav TykhiyThe checksum of the data portion of the IP packet has been computed 11180193eb5eSYaroslav Tykhiyand stored in the field 11190193eb5eSYaroslav Tykhiy.Va csum_data 11200193eb5eSYaroslav Tykhiyin network byte order. 11210193eb5eSYaroslav Tykhiy.It Dv CSUM_PSEUDO_HDR 11220193eb5eSYaroslav TykhiyCan be set only along with 11230193eb5eSYaroslav Tykhiy.Dv CSUM_DATA_VALID 11240193eb5eSYaroslav Tykhiyto indicate that the IP data checksum found in 11250193eb5eSYaroslav Tykhiy.Va csum_data 1126fbde705aSYaroslav Tykhiyallows for the pseudo header defined by the TCP and UDP specifications. 11270193eb5eSYaroslav TykhiyOtherwise the checksum of the pseudo header must be calculated by 11280193eb5eSYaroslav Tykhiythe host CPU and added to 11290193eb5eSYaroslav Tykhiy.Va csum_data 1130fbde705aSYaroslav Tykhiyto obtain the final checksum to be used for TCP or UDP validation purposes. 11310193eb5eSYaroslav Tykhiy.El 11320193eb5eSYaroslav Tykhiy.Pp 11330193eb5eSYaroslav TykhiyIf a particular network interface just indicates success or 11340193eb5eSYaroslav Tykhiyfailure of TCP or UDP checksum validation without returning 11350193eb5eSYaroslav Tykhiythe exact value of the checksum to the host CPU, its driver can mark 11360193eb5eSYaroslav Tykhiy.Dv CSUM_DATA_VALID 11370193eb5eSYaroslav Tykhiyand 11380193eb5eSYaroslav Tykhiy.Dv CSUM_PSEUDO_HDR 11390193eb5eSYaroslav Tykhiyin 11400193eb5eSYaroslav Tykhiy.Va csum_flags , 11410193eb5eSYaroslav Tykhiyand set 11420193eb5eSYaroslav Tykhiy.Va csum_data 11430193eb5eSYaroslav Tykhiyto 11440193eb5eSYaroslav Tykhiy.Li 0xFFFF 11450193eb5eSYaroslav Tykhiyhexadecimal to indicate a valid checksum. 1146f48cbcdeSRuslan ErmilovIt is a peculiarity of the algorithm used that the Internet checksum 11470193eb5eSYaroslav Tykhiycalculated over any valid packet will be 11480193eb5eSYaroslav Tykhiy.Li 0xFFFF 11490193eb5eSYaroslav Tykhiyas long as the original checksum field is included. 11500193eb5eSYaroslav Tykhiy.Pp 11510193eb5eSYaroslav TykhiyFor inbound packets which are IP fragments, all 11520193eb5eSYaroslav Tykhiy.Va csum_data 11530193eb5eSYaroslav Tykhiyfields will be summed during reassembly to obtain the final checksum 11540193eb5eSYaroslav Tykhiyvalue passed to an upper layer in the 11550193eb5eSYaroslav Tykhiy.Va csum_data 11560193eb5eSYaroslav Tykhiyfield of the reassembled packet. 11570193eb5eSYaroslav TykhiyThe 11580193eb5eSYaroslav Tykhiy.Va csum_flags 11590193eb5eSYaroslav Tykhiyfields of all fragments will be consolidated using logical AND 11600193eb5eSYaroslav Tykhiyto obtain the final value for 11610193eb5eSYaroslav Tykhiy.Va csum_flags . 11620193eb5eSYaroslav TykhiyThus, in order to successfully 11630193eb5eSYaroslav Tykhiyoffload checksum computation for fragmented data, 11640193eb5eSYaroslav Tykhiyall fragments should have the same value of 11650193eb5eSYaroslav Tykhiy.Va csum_flags . 11663cab047eSMike Silbersack.Sh STRESS TESTING 1167c8185672SRuslan ErmilovWhen running a kernel compiled with the option 1168c8185672SRuslan Ermilov.Dv MBUF_STRESS_TEST , 1169c8185672SRuslan Ermilovthe following 1170c8185672SRuslan Ermilov.Xr sysctl 8 Ns 1171c8185672SRuslan Ermilov-controlled options may be used to create 11723cab047eSMike Silbersackvarious failure/extreme cases for testing of network drivers 11737b2fd183SYaroslav Tykhiyand other parts of the kernel that rely on 11747b2fd183SYaroslav Tykhiy.Vt mbufs . 1175c8185672SRuslan Ermilov.Bl -tag -width ident 1176c8185672SRuslan Ermilov.It Va net.inet.ip.mbuf_frag_size 1177c8185672SRuslan ErmilovCauses 1178c8185672SRuslan Ermilov.Fn ip_output 11797b2fd183SYaroslav Tykhiyto fragment outgoing 11807b2fd183SYaroslav Tykhiy.Vt mbuf chains 11817b2fd183SYaroslav Tykhiyinto fragments of the specified size. 1182c8185672SRuslan ErmilovSetting this variable to 1 is an excellent way to 11837b2fd183SYaroslav Tykhiytest the long 11847b2fd183SYaroslav Tykhiy.Vt mbuf chain 11857b2fd183SYaroslav Tykhiyhandling ability of network drivers. 1186c8185672SRuslan Ermilov.It Va kern.ipc.m_defragrandomfailures 1187c8185672SRuslan ErmilovCauses the function 1188c8185672SRuslan Ermilov.Fn m_defrag 1189c8185672SRuslan Ermilovto randomly fail, returning 1190c8185672SRuslan Ermilov.Dv NULL . 1191c8185672SRuslan ErmilovAny piece of code which uses 1192c8185672SRuslan Ermilov.Fn m_defrag 1193c8185672SRuslan Ermilovshould be tested with this feature. 1194c8185672SRuslan Ermilov.El 1195de06f907SNik Clayton.Sh RETURN VALUES 1196de06f907SNik ClaytonSee above. 11970193eb5eSYaroslav Tykhiy.Sh SEE ALSO 1198e85cc3f5SRuslan Ermilov.Xr ifnet 9 , 1199e85cc3f5SRuslan Ermilov.Xr mbuf_tags 9 1200de06f907SNik Clayton.Sh HISTORY 1201de06f907SNik Clayton.\" Please correct me if I'm wrong 12027b2fd183SYaroslav Tykhiy.Vt Mbufs 12037b2fd183SYaroslav Tykhiyappeared in an early version of 1204753d686dSRuslan Ermilov.Bx . 120553b2520dSTom RhodesBesides being used for network packets, they were used 1206bfd59721SBosko Milekicto store various dynamic structures, such as routing table 1207bfd59721SBosko Milekicentries, interface addresses, protocol control blocks, etc. 1208db430f97SRobert WatsonIn more recent 1209db430f97SRobert Watson.Fx 1210db430f97SRobert Watsonuse of 1211db430f97SRobert Watson.Vt mbufs 1212db430f97SRobert Watsonis almost entirely limited to packet storage, with 1213db430f97SRobert Watson.Xr uma 9 1214db430f97SRobert Watsonzones being used directly to store other network-related memory. 1215db430f97SRobert Watson.Pp 1216db430f97SRobert WatsonHistorically, the 1217db430f97SRobert Watson.Vt mbuf 1218db430f97SRobert Watsonallocator has been a special-purpose memory allocator able to run in 1219db430f97SRobert Watsoninterrupt contexts and allocating from a special kernel address space map. 1220db430f97SRobert WatsonAs of 1221db430f97SRobert Watson.Fx 5.3 , 1222db430f97SRobert Watsonthe 1223db430f97SRobert Watson.Vt mbuf 1224db430f97SRobert Watsonallocator is a wrapper around 1225db430f97SRobert Watson.Xr uma 9 , 1226db430f97SRobert Watsonallowing caching of 1227db430f97SRobert Watson.Vt mbufs , 1228db430f97SRobert Watsonclusters, and 1229db430f97SRobert Watson.Vt mbuf 1230db430f97SRobert Watson+ cluster pairs in per-CPU caches, as well as bringing other benefits of 1231db430f97SRobert Watsonslab allocation. 1232de06f907SNik Clayton.Sh AUTHORS 12336575e6daSRuslan ErmilovThe original 12346575e6daSRuslan Ermilov.Nm 1235571dba6eSHiten Pandyamanual page was written by Yar Tikhiy. 1236db430f97SRobert WatsonThe 1237db430f97SRobert Watson.Xr uma 9 1238db430f97SRobert Watson.Vt mbuf 1239db430f97SRobert Watsonallocator was written by Bosko Milekic. 1240