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.\" 25854e1fd6SJohn Baldwin.Dd December 28, 2023 26de06f907SNik Clayton.Dt MBUF 9 27de06f907SNik Clayton.Os 28de06f907SNik Clayton.\" 29de06f907SNik Clayton.Sh NAME 30de06f907SNik Clayton.Nm mbuf 31de06f907SNik Clayton.Nd "memory management in the kernel IPC subsystem" 32de06f907SNik Clayton.\" 33de06f907SNik Clayton.Sh SYNOPSIS 3432eef9aeSRuslan Ermilov.In sys/param.h 35f16b3c0dSChad David.In sys/systm.h 3632eef9aeSRuslan Ermilov.In sys/mbuf.h 37de06f907SNik Clayton.\" 38bfd59721SBosko Milekic.Ss Mbuf allocation macros 39de06f907SNik Clayton.Fn MGET "struct mbuf *mbuf" "int how" "short type" 40de06f907SNik Clayton.Fn MGETHDR "struct mbuf *mbuf" "int how" "short type" 412a8c860fSRobert Watson.Ft int 42de06f907SNik Clayton.Fn MCLGET "struct mbuf *mbuf" "int how" 43bfd59721SBosko Milekic.Fo MEXTADD 44bfd59721SBosko Milekic.Fa "struct mbuf *mbuf" 45e8fd18f3SGleb Smirnoff.Fa "char *buf" 46bfd59721SBosko Milekic.Fa "u_int size" 47e8fd18f3SGleb Smirnoff.Fa "void (*free)(struct mbuf *)" 48cf827063SPoul-Henning Kamp.Fa "void *opt_arg1" 49cf827063SPoul-Henning Kamp.Fa "void *opt_arg2" 50e8fd18f3SGleb Smirnoff.Fa "int flags" 515d4050ccSBosko Milekic.Fa "int type" 52bfd59721SBosko Milekic.Fc 53de06f907SNik Clayton.\" 54bfd59721SBosko Milekic.Ss Mbuf utility macros 55854e1fd6SJohn Baldwin.Ft type 56f16b3c0dSChad David.Fn mtod "struct mbuf *mbuf" "type" 57854e1fd6SJohn Baldwin.Ft void * 58854e1fd6SJohn Baldwin.Fn mtodo "struct mbuf *mbuf" "offset" 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" 67ada46fe9SKevin Lo.Fn MCHTYPE "struct mbuf *mbuf" "short 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 * 73ada46fe9SKevin Lo.Fn m_get "int how" "short type" 74de06f907SNik Clayton.Ft struct mbuf * 7518f169afSKevin Lo.Fn m_get2 "int size" "int how" "short type" "int flags" 7618f169afSKevin Lo.Ft struct mbuf * 77a051ca72SKristof Provost.Fn m_get3 "int size" "int how" "short type" "int flags" 78a051ca72SKristof Provost.Ft struct mbuf * 79ada46fe9SKevin Lo.Fn m_getm "struct mbuf *orig" "int len" "int how" "short type" 80328c0bbcSBosko Milekic.Ft struct mbuf * 815c35eb22SJohn-Mark Gurney.Fn m_getjcl "int how" "short type" "int flags" "int size" 825c35eb22SJohn-Mark Gurney.Ft struct mbuf * 836c013706SMaxim Konovalov.Fn m_getcl "int how" "short type" "int flags" 846c013706SMaxim Konovalov.Ft struct mbuf * 85ada46fe9SKevin Lo.Fn m_gethdr "int how" "short type" 86bfd59721SBosko Milekic.Ft struct mbuf * 87bfd59721SBosko Milekic.Fn m_free "struct mbuf *mbuf" 88de06f907SNik Clayton.Ft void 89de06f907SNik Clayton.Fn m_freem "struct mbuf *mbuf" 90bfd59721SBosko Milekic.\" 91bfd59721SBosko Milekic.Ss Mbuf utility functions 92de06f907SNik Clayton.Ft void 93de06f907SNik Clayton.Fn m_adj "struct mbuf *mbuf" "int len" 949c3fd404SSam Leffler.Ft void 959c3fd404SSam Leffler.Fn m_align "struct mbuf *mbuf" "int len" 96b9def06bSSam Leffler.Ft int 97b9def06bSSam Leffler.Fn m_append "struct mbuf *mbuf" "int len" "c_caddr_t cp" 98de06f907SNik Clayton.Ft struct mbuf * 99de06f907SNik Clayton.Fn m_prepend "struct mbuf *mbuf" "int len" "int how" 100de06f907SNik Clayton.Ft struct mbuf * 1017ac139a9SJohn-Mark Gurney.Fn m_copyup "struct mbuf *mbuf" "int len" "int dstoff" 1027ac139a9SJohn-Mark Gurney.Ft struct mbuf * 103de06f907SNik Clayton.Fn m_pullup "struct mbuf *mbuf" "int len" 104de06f907SNik Clayton.Ft struct mbuf * 105d305f4b9SBruce M Simpson.Fn m_pulldown "struct mbuf *mbuf" "int offset" "int len" "int *offsetp" 106d305f4b9SBruce M Simpson.Ft struct mbuf * 107de06f907SNik Clayton.Fn m_copym "struct mbuf *mbuf" "int offset" "int len" "int how" 108de06f907SNik Clayton.Ft struct mbuf * 109de06f907SNik Clayton.Fn m_copypacket "struct mbuf *mbuf" "int how" 110de06f907SNik Clayton.Ft struct mbuf * 1111af4a585SAndriy Voskoboinyk.Fn m_dup "const struct mbuf *mbuf" "int how" 112de06f907SNik Clayton.Ft void 1130d8e4c57SBruce Evans.Fn m_copydata "const struct mbuf *mbuf" "int offset" "int len" "caddr_t buf" 114de06f907SNik Clayton.Ft void 115de06f907SNik Clayton.Fn m_copyback "struct mbuf *mbuf" "int offset" "int len" "caddr_t buf" 116de06f907SNik Clayton.Ft struct mbuf * 117de06f907SNik Clayton.Fo m_devget 118de06f907SNik Clayton.Fa "char *buf" 119de06f907SNik Clayton.Fa "int len" 120de06f907SNik Clayton.Fa "int offset" 121de06f907SNik Clayton.Fa "struct ifnet *ifp" 122de06f907SNik Clayton.Fa "void (*copy)(char *from, caddr_t to, u_int len)" 123de06f907SNik Clayton.Fc 124de06f907SNik Clayton.Ft void 125de06f907SNik Clayton.Fn m_cat "struct mbuf *m" "struct mbuf *n" 126a3f6b029SMark Johnston.Ft void 127a3f6b029SMark Johnston.Fn m_catpkt "struct mbuf *m" "struct mbuf *n" 128e60fa837SRuslan Ermilov.Ft u_int 1290b1c0994SPoul-Henning Kamp.Fn m_fixhdr "struct mbuf *mbuf" 1301af4a585SAndriy Voskoboinyk.Ft int 1311af4a585SAndriy Voskoboinyk.Fn m_dup_pkthdr "struct mbuf *to" "const struct mbuf *from" "int how" 132da72b998SYaroslav Tykhiy.Ft void 133da72b998SYaroslav Tykhiy.Fn m_move_pkthdr "struct mbuf *to" "struct mbuf *from" 134e60fa837SRuslan Ermilov.Ft u_int 1350b1c0994SPoul-Henning Kamp.Fn m_length "struct mbuf *mbuf" "struct mbuf **last" 136de06f907SNik Clayton.Ft struct mbuf * 137de06f907SNik Clayton.Fn m_split "struct mbuf *mbuf" "int len" "int how" 13802312219SBruce M Simpson.Ft int 13902312219SBruce M Simpson.Fn m_apply "struct mbuf *mbuf" "int off" "int len" "int (*f)(void *arg, void *data, u_int len)" "void *arg" 14002312219SBruce M Simpson.Ft struct mbuf * 14102312219SBruce M Simpson.Fn m_getptr "struct mbuf *mbuf" "int loc" "int *off" 142a61bc764SMike Silbersack.Ft struct mbuf * 143a61bc764SMike Silbersack.Fn m_defrag "struct mbuf *m0" "int how" 14447e2996eSSam Leffler.Ft struct mbuf * 145f66b6464SMark Johnston.Fn m_collapse "struct mbuf *m0" "int how" "int maxfrags" 146f66b6464SMark Johnston.Ft struct mbuf * 14747e2996eSSam Leffler.Fn m_unshare "struct mbuf *m0" "int how" 148de06f907SNik Clayton.\" 149de06f907SNik Clayton.Sh DESCRIPTION 1507b2fd183SYaroslav TykhiyAn 1517b2fd183SYaroslav Tykhiy.Vt mbuf 1527b2fd183SYaroslav Tykhiyis a basic unit of memory management in the kernel IPC subsystem. 1537b2fd183SYaroslav TykhiyNetwork packets and socket buffers are stored in 1547b2fd183SYaroslav Tykhiy.Vt mbufs . 1557b2fd183SYaroslav TykhiyA network packet may span multiple 1567b2fd183SYaroslav Tykhiy.Vt mbufs 1577b2fd183SYaroslav Tykhiyarranged into a 1587b2fd183SYaroslav Tykhiy.Vt mbuf chain 159c4d9468eSRuslan Ermilov(linked list), 160de06f907SNik Claytonwhich allows adding or trimming 161bfd59721SBosko Milekicnetwork headers with little overhead. 162de06f907SNik Clayton.Pp 1637b2fd183SYaroslav TykhiyWhile a developer should not bother with 1647b2fd183SYaroslav Tykhiy.Vt mbuf 1657b2fd183SYaroslav Tykhiyinternals without serious 166de06f907SNik Claytonreason in order to avoid incompatibilities with future changes, it 1677b2fd183SYaroslav Tykhiyis useful to understand the general structure of an 1687b2fd183SYaroslav Tykhiy.Vt mbuf . 169de06f907SNik Clayton.Pp 1707b2fd183SYaroslav TykhiyAn 1717b2fd183SYaroslav Tykhiy.Vt mbuf 1727b2fd183SYaroslav Tykhiyconsists of a variable-sized header and a small internal 173e73145f6SSheldon Hearnbuffer for data. 1747b2fd183SYaroslav TykhiyThe total size of an 1757b2fd183SYaroslav Tykhiy.Vt mbuf , 176bfd59721SBosko Milekic.Dv MSIZE , 177feccf50cSAlex Dupreis a constant defined in 178feccf50cSAlex Dupre.In sys/param.h . 1797b2fd183SYaroslav TykhiyThe 1807b2fd183SYaroslav Tykhiy.Vt mbuf 1817b2fd183SYaroslav Tykhiyheader includes: 182238d9c59SYaroslav Tykhiy.Bl -tag -width "m_nextpkt" -offset indent 183be82a0bcSYaroslav Tykhiy.It Va m_next 184238d9c59SYaroslav Tykhiy.Pq Vt struct mbuf * 185238d9c59SYaroslav TykhiyA pointer to the next 1867b2fd183SYaroslav Tykhiy.Vt mbuf 1877b2fd183SYaroslav Tykhiyin the 188238d9c59SYaroslav Tykhiy.Vt mbuf chain . 189be82a0bcSYaroslav Tykhiy.It Va m_nextpkt 190238d9c59SYaroslav Tykhiy.Pq Vt struct mbuf * 191238d9c59SYaroslav TykhiyA pointer to the next 1927b2fd183SYaroslav Tykhiy.Vt mbuf chain 193238d9c59SYaroslav Tykhiyin the queue. 194be82a0bcSYaroslav Tykhiy.It Va m_data 195238d9c59SYaroslav Tykhiy.Pq Vt caddr_t 196238d9c59SYaroslav TykhiyA pointer to data attached to this 197238d9c59SYaroslav Tykhiy.Vt mbuf . 198be82a0bcSYaroslav Tykhiy.It Va m_len 199238d9c59SYaroslav Tykhiy.Pq Vt int 200238d9c59SYaroslav TykhiyThe length of the data. 201be82a0bcSYaroslav Tykhiy.It Va m_type 202238d9c59SYaroslav Tykhiy.Pq Vt short 203238d9c59SYaroslav TykhiyThe type of the data. 204be82a0bcSYaroslav Tykhiy.It Va m_flags 205238d9c59SYaroslav Tykhiy.Pq Vt int 206238d9c59SYaroslav TykhiyThe 2077b2fd183SYaroslav Tykhiy.Vt mbuf 208238d9c59SYaroslav Tykhiyflags. 209de06f907SNik Clayton.El 210de06f907SNik Clayton.Pp 2117b2fd183SYaroslav TykhiyThe 2127b2fd183SYaroslav Tykhiy.Vt mbuf 2137b2fd183SYaroslav Tykhiyflag bits are defined as follows: 214de06f907SNik Clayton.Bd -literal 21533d8df4bSKevin Lo#define M_EXT 0x00000001 /* has associated external storage */ 21633d8df4bSKevin Lo#define M_PKTHDR 0x00000002 /* start of record */ 21733d8df4bSKevin Lo#define M_EOR 0x00000004 /* end of record */ 21833d8df4bSKevin Lo#define M_RDONLY 0x00000008 /* associated data marked read-only */ 21977312354SJohn Baldwin#define M_BCAST 0x00000010 /* send/received as link-level broadcast */ 22077312354SJohn Baldwin#define M_MCAST 0x00000020 /* send/received as link-level multicast */ 22177312354SJohn Baldwin#define M_PROMISC 0x00000040 /* packet was not for us */ 22277312354SJohn Baldwin#define M_VLANTAG 0x00000080 /* ether_vtag is valid */ 223cf6ff85fSGleb Smirnoff#define M_EXTPG 0x00000100 /* has array of unmapped pages and TLS */ 224cf6ff85fSGleb Smirnoff#define M_NOFREE 0x00000200 /* do not free mbuf, embedded in cluster */ 22577312354SJohn Baldwin#define M_TSTMP 0x00000400 /* rcv_tstmp field is valid */ 22677312354SJohn Baldwin#define M_TSTMP_HPREC 0x00000800 /* rcv_tstmp is high-prec, typically 22777312354SJohn Baldwin hw-stamped on port (useful for IEEE 1588 22877312354SJohn Baldwin and 802.1AS) */ 22977312354SJohn Baldwin 23033d8df4bSKevin Lo#define M_PROTO1 0x00001000 /* protocol-specific */ 23133d8df4bSKevin Lo#define M_PROTO2 0x00002000 /* protocol-specific */ 23233d8df4bSKevin Lo#define M_PROTO3 0x00004000 /* protocol-specific */ 23333d8df4bSKevin Lo#define M_PROTO4 0x00008000 /* protocol-specific */ 23433d8df4bSKevin Lo#define M_PROTO5 0x00010000 /* protocol-specific */ 23533d8df4bSKevin Lo#define M_PROTO6 0x00020000 /* protocol-specific */ 23633d8df4bSKevin Lo#define M_PROTO7 0x00040000 /* protocol-specific */ 23733d8df4bSKevin Lo#define M_PROTO8 0x00080000 /* protocol-specific */ 23833d8df4bSKevin Lo#define M_PROTO9 0x00100000 /* protocol-specific */ 23933d8df4bSKevin Lo#define M_PROTO10 0x00200000 /* protocol-specific */ 24033d8df4bSKevin Lo#define M_PROTO11 0x00400000 /* protocol-specific */ 24133d8df4bSKevin Lo#define M_PROTO12 0x00800000 /* protocol-specific */ 242de06f907SNik Clayton.Ed 243de06f907SNik Clayton.Pp 2447b2fd183SYaroslav TykhiyThe available 2457b2fd183SYaroslav Tykhiy.Vt mbuf 2467b2fd183SYaroslav Tykhiytypes are defined as follows: 247de06f907SNik Clayton.Bd -literal 248de06f907SNik Clayton#define MT_DATA 1 /* dynamic (data) allocation */ 2491cb58a07SMaxim Konovalov#define MT_HEADER MT_DATA /* packet header */ 25077312354SJohn Baldwin 25177312354SJohn Baldwin#define MT_VENDOR1 4 /* for vendor-internal use */ 25277312354SJohn Baldwin#define MT_VENDOR2 5 /* for vendor-internal use */ 25377312354SJohn Baldwin#define MT_VENDOR3 6 /* for vendor-internal use */ 25477312354SJohn Baldwin#define MT_VENDOR4 7 /* for vendor-internal use */ 25577312354SJohn Baldwin 256de06f907SNik Clayton#define MT_SONAME 8 /* socket name */ 25777312354SJohn Baldwin 25877312354SJohn Baldwin#define MT_EXP1 9 /* for experimental use */ 25977312354SJohn Baldwin#define MT_EXP2 10 /* for experimental use */ 26077312354SJohn Baldwin#define MT_EXP3 11 /* for experimental use */ 26177312354SJohn Baldwin#define MT_EXP4 12 /* for experimental use */ 26277312354SJohn Baldwin 263de06f907SNik Clayton#define MT_CONTROL 14 /* extra-data protocol message */ 26477312354SJohn Baldwin#define MT_EXTCONTROL 15 /* control message with externalized contents */ 26577312354SJohn Baldwin#define MT_OOBDATA 16 /* expedited data */ 266de06f907SNik Clayton.Ed 267de06f907SNik Clayton.Pp 268c3a2dbe9SDag-Erling SmørgravThe available external buffer types are defined as follows: 269c3a2dbe9SDag-Erling Smørgrav.Bd -literal 270c3a2dbe9SDag-Erling Smørgrav#define EXT_CLUSTER 1 /* mbuf cluster */ 271c3a2dbe9SDag-Erling Smørgrav#define EXT_SFBUF 2 /* sendfile(2)'s sf_bufs */ 272c3a2dbe9SDag-Erling Smørgrav#define EXT_JUMBOP 3 /* jumbo cluster 4096 bytes */ 273c3a2dbe9SDag-Erling Smørgrav#define EXT_JUMBO9 4 /* jumbo cluster 9216 bytes */ 274c3a2dbe9SDag-Erling Smørgrav#define EXT_JUMBO16 5 /* jumbo cluster 16184 bytes */ 275c3a2dbe9SDag-Erling Smørgrav#define EXT_PACKET 6 /* mbuf+cluster from packet zone */ 276a951a788SKevin Lo#define EXT_MBUF 7 /* external mbuf reference */ 27777312354SJohn Baldwin#define EXT_RXRING 8 /* data in NIC receive ring */ 27882334850SJohn Baldwin#define EXT_PGS 9 /* array of unmapped pages */ 27977312354SJohn Baldwin 28077312354SJohn Baldwin#define EXT_VENDOR1 224 /* for vendor-internal use */ 28177312354SJohn Baldwin#define EXT_VENDOR2 225 /* for vendor-internal use */ 28277312354SJohn Baldwin#define EXT_VENDOR3 226 /* for vendor-internal use */ 28377312354SJohn Baldwin#define EXT_VENDOR4 227 /* for vendor-internal use */ 28477312354SJohn Baldwin 28577312354SJohn Baldwin#define EXT_EXP1 244 /* for experimental use */ 28677312354SJohn Baldwin#define EXT_EXP2 245 /* for experimental use */ 28777312354SJohn Baldwin#define EXT_EXP3 246 /* for experimental use */ 28877312354SJohn Baldwin#define EXT_EXP4 247 /* for experimental use */ 28977312354SJohn Baldwin 29033d8df4bSKevin Lo#define EXT_NET_DRV 252 /* custom ext_buf provided by net driver(s) */ 29133d8df4bSKevin Lo#define EXT_MOD_TYPE 253 /* custom module's ext_buf type */ 29233d8df4bSKevin Lo#define EXT_DISPOSABLE 254 /* can throw this buffer away w/page flipping */ 29333d8df4bSKevin Lo#define EXT_EXTREF 255 /* has externally maintained ref_cnt ptr */ 294c3a2dbe9SDag-Erling Smørgrav.Ed 295c3a2dbe9SDag-Erling Smørgrav.Pp 296de06f907SNik ClaytonIf the 297de06f907SNik Clayton.Dv M_PKTHDR 298de06f907SNik Claytonflag is set, a 2997b2fd183SYaroslav Tykhiy.Vt struct pkthdr Va m_pkthdr 3007b2fd183SYaroslav Tykhiyis added to the 3017b2fd183SYaroslav Tykhiy.Vt mbuf 3027b2fd183SYaroslav Tykhiyheader. 303e73145f6SSheldon HearnIt contains a pointer to the interface 304de06f907SNik Claytonthe packet has been received from 3057b2fd183SYaroslav Tykhiy.Pq Vt struct ifnet Va *rcvif , 306de06f907SNik Claytonand the total packet length 3077b2fd183SYaroslav Tykhiy.Pq Vt int Va len . 308e85cc3f5SRuslan ErmilovOptionally, it may also contain an attached list of packet tags 309e85cc3f5SRuslan Ermilov.Pq Vt "struct m_tag" . 310e85cc3f5SRuslan ErmilovSee 311e85cc3f5SRuslan Ermilov.Xr mbuf_tags 9 312e85cc3f5SRuslan Ermilovfor details. 3130193eb5eSYaroslav TykhiyFields used in offloading checksum calculation to the hardware are kept in 3140193eb5eSYaroslav Tykhiy.Va m_pkthdr 3150193eb5eSYaroslav Tykhiyas well. 3160193eb5eSYaroslav TykhiySee 3170193eb5eSYaroslav Tykhiy.Sx HARDWARE-ASSISTED CHECKSUM CALCULATION 3180193eb5eSYaroslav Tykhiyfor details. 319de06f907SNik Clayton.Pp 3207b2fd183SYaroslav TykhiyIf small enough, data is stored in the internal data buffer of an 3217b2fd183SYaroslav Tykhiy.Vt mbuf . 3227b2fd183SYaroslav TykhiyIf the data is sufficiently large, another 3237b2fd183SYaroslav Tykhiy.Vt mbuf 3247b2fd183SYaroslav Tykhiymay be added to the 3257b2fd183SYaroslav Tykhiy.Vt mbuf chain , 3267b2fd183SYaroslav Tykhiyor external storage may be associated with the 3277b2fd183SYaroslav Tykhiy.Vt mbuf . 328de06f907SNik Clayton.Dv MHLEN 3297b2fd183SYaroslav Tykhiybytes of data can fit into an 3307b2fd183SYaroslav Tykhiy.Vt mbuf 3317b2fd183SYaroslav Tykhiywith the 332de06f907SNik Clayton.Dv M_PKTHDR 333de06f907SNik Claytonflag set, 334de06f907SNik Clayton.Dv MLEN 335de06f907SNik Claytonbytes can otherwise. 336de06f907SNik Clayton.Pp 3377b2fd183SYaroslav TykhiyIf external storage is being associated with an 3387b2fd183SYaroslav Tykhiy.Vt mbuf , 3397b2fd183SYaroslav Tykhiythe 340be82a0bcSYaroslav Tykhiy.Va m_ext 34125f1a2e9SSheldon Hearnheader is added at the cost of losing the internal data buffer. 342e73145f6SSheldon HearnIt includes a pointer to external storage, the size of the storage, 343e73145f6SSheldon Hearna pointer to a function used for freeing the storage, 344e73145f6SSheldon Hearna pointer to an optional argument that can be passed to the function, 345e73145f6SSheldon Hearnand a pointer to a reference counter. 3467b2fd183SYaroslav TykhiyAn 3477b2fd183SYaroslav Tykhiy.Vt mbuf 3487b2fd183SYaroslav Tykhiyusing external storage has the 349de06f907SNik Clayton.Dv M_EXT 350de06f907SNik Claytonflag set. 351de06f907SNik Clayton.Pp 352bfd59721SBosko MilekicThe system supplies a macro for allocating the desired external storage 353bfd59721SBosko Milekicbuffer, 354bfd59721SBosko Milekic.Dv MEXTADD . 355bfd59721SBosko Milekic.Pp 356bfd59721SBosko MilekicThe allocation and management of the reference counter is handled by the 357e73145f6SSheldon Hearnsubsystem. 358bfd59721SBosko Milekic.Pp 359bfd59721SBosko MilekicThe system also supplies a default type of external storage buffer called an 3607b2fd183SYaroslav Tykhiy.Vt mbuf cluster . 3617b2fd183SYaroslav Tykhiy.Vt Mbuf clusters 3627b2fd183SYaroslav Tykhiycan be allocated and configured with the use of the 363bfd59721SBosko Milekic.Dv MCLGET 364e73145f6SSheldon Hearnmacro. 3657b2fd183SYaroslav TykhiyEach 3667b2fd183SYaroslav Tykhiy.Vt mbuf cluster 3677b2fd183SYaroslav Tykhiyis 368de06f907SNik Clayton.Dv MCLBYTES 369bfd59721SBosko Milekicin size, where MCLBYTES is a machine-dependent constant. 370bfd59721SBosko MilekicThe system defines an advisory macro 371de06f907SNik Clayton.Dv MINCLSIZE , 3727b2fd183SYaroslav Tykhiywhich is the smallest amount of data to put into an 3737b2fd183SYaroslav Tykhiy.Vt mbuf cluster . 3742e3ee239SKevin LoIt is equal to 3752e3ee239SKevin Lo.Dv MHLEN 3762e3ee239SKevin Loplus one. 3777b2fd183SYaroslav TykhiyIt is typically preferable to store data into the data region of an 3787b2fd183SYaroslav Tykhiy.Vt mbuf , 3797b2fd183SYaroslav Tykhiyif size permits, as opposed to allocating a separate 3807b2fd183SYaroslav Tykhiy.Vt mbuf cluster 3817b2fd183SYaroslav Tykhiyto hold the same data. 382de06f907SNik Clayton.\" 383de06f907SNik Clayton.Ss Macros and Functions 384bfd59721SBosko MilekicThere are numerous predefined macros and functions that provide the 385bfd59721SBosko Milekicdeveloper with common utilities. 386de06f907SNik Clayton.\" 387de06f907SNik Clayton.Bl -ohang -offset indent 388de06f907SNik Clayton.It Fn mtod mbuf type 3897b2fd183SYaroslav TykhiyConvert an 3907b2fd183SYaroslav Tykhiy.Fa mbuf 3917b2fd183SYaroslav Tykhiypointer to a data pointer. 392d31a9760SJohn-Mark GurneyThe macro expands to the data pointer cast to the specified 3937b2fd183SYaroslav Tykhiy.Fa type . 394de06f907SNik Clayton.Sy Note : 3957b2fd183SYaroslav TykhiyIt is advisable to ensure that there is enough contiguous data in 3967b2fd183SYaroslav Tykhiy.Fa mbuf . 397de06f907SNik ClaytonSee 398de06f907SNik Clayton.Fn m_pullup 399de06f907SNik Claytonfor details. 400854e1fd6SJohn Baldwin.It Fn mtodo mbuf offset 401854e1fd6SJohn BaldwinReturn a data pointer at an offset (in bytes) into the data attached to 402854e1fd6SJohn Baldwin.Fa mbuf . 403854e1fd6SJohn BaldwinReturns a 404854e1fd6SJohn Baldwin.Ft void * 405854e1fd6SJohn Baldwinpointer . 406854e1fd6SJohn Baldwin.Sy Note : 407854e1fd6SJohn BaldwinThe caller must ensure that the offset is in bounds of the attached data. 408de06f907SNik Clayton.It Fn MGET mbuf how type 4097b2fd183SYaroslav TykhiyAllocate an 4107b2fd183SYaroslav Tykhiy.Vt mbuf 4117b2fd183SYaroslav Tykhiyand initialize it to contain internal data. 41225f1a2e9SSheldon Hearn.Fa mbuf 4137b2fd183SYaroslav Tykhiywill point to the allocated 4147b2fd183SYaroslav Tykhiy.Vt mbuf 4157b2fd183SYaroslav Tykhiyon success, or be set to 416de06f907SNik Clayton.Dv NULL 417e73145f6SSheldon Hearnon failure. 418e73145f6SSheldon HearnThe 419de06f907SNik Clayton.Fa how 420de06f907SNik Claytonargument is to be set to 421fa05d215SGleb Smirnoff.Dv M_WAITOK 422de06f907SNik Claytonor 423fa05d215SGleb Smirnoff.Dv M_NOWAIT . 42425f1a2e9SSheldon HearnIt specifies whether the caller is willing to block if necessary. 4257b2fd183SYaroslav TykhiyA number of other functions and macros related to 4267b2fd183SYaroslav Tykhiy.Vt mbufs 4277b2fd183SYaroslav Tykhiyhave the same argument because they may 4287b2fd183SYaroslav Tykhiyat some point need to allocate new 4297b2fd183SYaroslav Tykhiy.Vt mbufs . 430de06f907SNik Clayton.It Fn MGETHDR mbuf how type 4317b2fd183SYaroslav TykhiyAllocate an 4327b2fd183SYaroslav Tykhiy.Vt mbuf 4337b2fd183SYaroslav Tykhiyand initialize it to contain a packet header 434e73145f6SSheldon Hearnand internal data. 435e73145f6SSheldon HearnSee 436de06f907SNik Clayton.Fn MGET 437de06f907SNik Claytonfor details. 438c3a2dbe9SDag-Erling Smørgrav.It Fn MEXTADD mbuf buf size free opt_arg1 opt_arg2 flags type 439c3a2dbe9SDag-Erling SmørgravAssociate externally managed data with 440c3a2dbe9SDag-Erling Smørgrav.Fa mbuf . 441c3a2dbe9SDag-Erling SmørgravAny internal data contained in the mbuf will be discarded, and the 44273bbeaa5SGlen Barber.Dv M_EXT 44373bbeaa5SGlen Barberflag will be set. 444c3a2dbe9SDag-Erling SmørgravThe 445c3a2dbe9SDag-Erling Smørgrav.Fa buf 446c3a2dbe9SDag-Erling Smørgravand 447c3a2dbe9SDag-Erling Smørgrav.Fa size 448c3a2dbe9SDag-Erling Smørgravarguments are the address and length, respectively, of the data. 449c3a2dbe9SDag-Erling SmørgravThe 450c3a2dbe9SDag-Erling Smørgrav.Fa free 451c3a2dbe9SDag-Erling Smørgravargument points to a function which will be called to free the data 452c3a2dbe9SDag-Erling Smørgravwhen the mbuf is freed; it is only used if 453c3a2dbe9SDag-Erling Smørgrav.Fa type 454c3a2dbe9SDag-Erling Smørgravis 455c3a2dbe9SDag-Erling Smørgrav.Dv EXT_EXTREF . 456c3a2dbe9SDag-Erling SmørgravThe 457c3a2dbe9SDag-Erling Smørgrav.Fa opt_arg1 458c3a2dbe9SDag-Erling Smørgravand 459c3a2dbe9SDag-Erling Smørgrav.Fa opt_arg2 460e8fd18f3SGleb Smirnoffarguments will be saved in 461e8fd18f3SGleb Smirnoff.Va ext_arg1 462e8fd18f3SGleb Smirnoffand 463e8fd18f3SGleb Smirnoff.Va ext_arg2 464e8fd18f3SGleb Smirnofffields of the 465e8fd18f3SGleb Smirnoff.Va struct m_ext 466e8fd18f3SGleb Smirnoffof the mbuf. 467c3a2dbe9SDag-Erling SmørgravThe 468c3a2dbe9SDag-Erling Smørgrav.Fa flags 469c3a2dbe9SDag-Erling Smørgravargument specifies additional 470c3a2dbe9SDag-Erling Smørgrav.Vt mbuf 471c3a2dbe9SDag-Erling Smørgravflags; it is not necessary to specify 472c3a2dbe9SDag-Erling Smørgrav.Dv M_EXT . 473c3a2dbe9SDag-Erling SmørgravFinally, the 474c3a2dbe9SDag-Erling Smørgrav.Fa type 475c3a2dbe9SDag-Erling Smørgravargument specifies the type of external data, which controls how it 476c3a2dbe9SDag-Erling Smørgravwill be disposed of when the 477c3a2dbe9SDag-Erling Smørgrav.Vt mbuf 478c3a2dbe9SDag-Erling Smørgravis freed. 479c3a2dbe9SDag-Erling SmørgravIn most cases, the correct value is 480c3a2dbe9SDag-Erling Smørgrav.Dv EXT_EXTREF . 481de06f907SNik Clayton.It Fn MCLGET mbuf how 4827b2fd183SYaroslav TykhiyAllocate and attach an 4837b2fd183SYaroslav Tykhiy.Vt mbuf cluster 4847b2fd183SYaroslav Tykhiyto 4857b2fd183SYaroslav Tykhiy.Fa mbuf . 4862a8c860fSRobert WatsonOn success, a non-zero value returned; otherwise, 0. 4872a8c860fSRobert WatsonHistorically, consumers would check for success by testing the 488de06f907SNik Clayton.Dv M_EXT 4892a8c860fSRobert Watsonflag on the mbuf, but this is now discouraged to avoid unnecessary awareness 4902a8c860fSRobert Watsonof the implementation of external storage in protocol stacks and device 4912a8c860fSRobert Watsondrivers. 492da72b998SYaroslav Tykhiy.It Fn M_ALIGN mbuf len 493da72b998SYaroslav TykhiySet the pointer 494da72b998SYaroslav Tykhiy.Fa mbuf->m_data 495da72b998SYaroslav Tykhiyto place an object of the size 496da72b998SYaroslav Tykhiy.Fa len 497da72b998SYaroslav Tykhiyat the end of the internal data area of 498da72b998SYaroslav Tykhiy.Fa mbuf , 499da72b998SYaroslav Tykhiylong word aligned. 500da72b998SYaroslav TykhiyApplicable only if 501da72b998SYaroslav Tykhiy.Fa mbuf 502da72b998SYaroslav Tykhiyis newly allocated with 503da72b998SYaroslav Tykhiy.Fn MGET 504da72b998SYaroslav Tykhiyor 505da72b998SYaroslav Tykhiy.Fn m_get . 506da72b998SYaroslav Tykhiy.It Fn MH_ALIGN mbuf len 507da72b998SYaroslav TykhiyServes the same purpose as 508da72b998SYaroslav Tykhiy.Fn M_ALIGN 509da72b998SYaroslav Tykhiydoes, but only for 510da72b998SYaroslav Tykhiy.Fa mbuf 511da72b998SYaroslav Tykhiynewly allocated with 512da72b998SYaroslav Tykhiy.Fn MGETHDR 513da72b998SYaroslav Tykhiyor 514da72b998SYaroslav Tykhiy.Fn m_gethdr , 515da72b998SYaroslav Tykhiyor initialized by 516da72b998SYaroslav Tykhiy.Fn m_dup_pkthdr 517da72b998SYaroslav Tykhiyor 518da72b998SYaroslav Tykhiy.Fn m_move_pkthdr . 5199c3fd404SSam Leffler.It Fn m_align mbuf len 5209c3fd404SSam LefflerServices the same purpose as 5219c3fd404SSam Leffler.Fn M_ALIGN 5229c3fd404SSam Lefflerbut handles any type of mbuf. 523da72b998SYaroslav Tykhiy.It Fn M_LEADINGSPACE mbuf 524da72b998SYaroslav TykhiyReturns the number of bytes available before the beginning 525da72b998SYaroslav Tykhiyof data in 526da72b998SYaroslav Tykhiy.Fa mbuf . 527da72b998SYaroslav Tykhiy.It Fn M_TRAILINGSPACE mbuf 528da72b998SYaroslav TykhiyReturns the number of bytes available after the end of data in 529da72b998SYaroslav Tykhiy.Fa mbuf . 530de06f907SNik Clayton.It Fn M_PREPEND mbuf len how 5317b2fd183SYaroslav TykhiyThis macro operates on an 5327b2fd183SYaroslav Tykhiy.Vt mbuf chain . 533de06f907SNik ClaytonIt is an optimized wrapper for 534de06f907SNik Clayton.Fn m_prepend 535de06f907SNik Claytonthat can make use of possible empty space before data 5365203edcdSRuslan Ermilov(e.g.\& left after trimming of a link-layer header). 5377b2fd183SYaroslav TykhiyThe new 5387b2fd183SYaroslav Tykhiy.Vt mbuf chain 5397b2fd183SYaroslav Tykhiypointer or 540de06f907SNik Clayton.Dv NULL 541de06f907SNik Claytonis in 542de06f907SNik Clayton.Fa mbuf 543de06f907SNik Claytonafter the call. 544da72b998SYaroslav Tykhiy.It Fn M_MOVE_PKTHDR to from 545da72b998SYaroslav TykhiyUsing this macro is equivalent to calling 546da72b998SYaroslav Tykhiy.Fn m_move_pkthdr to from . 5475d4050ccSBosko Milekic.It Fn M_WRITABLE mbuf 5487b2fd183SYaroslav TykhiyThis macro will evaluate true if 5497b2fd183SYaroslav Tykhiy.Fa mbuf 5507b2fd183SYaroslav Tykhiyis not marked 5515d4050ccSBosko Milekic.Dv M_RDONLY 5527b2fd183SYaroslav Tykhiyand if either 5537b2fd183SYaroslav Tykhiy.Fa mbuf 5547b2fd183SYaroslav Tykhiydoes not contain external storage or, 5555d4050ccSBosko Milekicif it does, 5565d4050ccSBosko Milekicthen if the reference count of the storage is not greater than 1. 5575d4050ccSBosko MilekicThe 5585d4050ccSBosko Milekic.Dv M_RDONLY 5597b2fd183SYaroslav Tykhiyflag can be set in 5607b2fd183SYaroslav Tykhiy.Fa mbuf->m_flags . 5615d4050ccSBosko MilekicThis can be achieved during setup of the external storage, 5625d4050ccSBosko Milekicby passing the 5635d4050ccSBosko Milekic.Dv M_RDONLY 5645d4050ccSBosko Milekicbit as a 5650b39b3ffSRuslan Ermilov.Fa flags 5665d4050ccSBosko Milekicargument to the 5675d4050ccSBosko Milekic.Fn MEXTADD 5687b2fd183SYaroslav Tykhiymacro, or can be directly set in individual 5697b2fd183SYaroslav Tykhiy.Vt mbufs . 570da72b998SYaroslav Tykhiy.It Fn MCHTYPE mbuf type 571da72b998SYaroslav TykhiyChange the type of 572da72b998SYaroslav Tykhiy.Fa mbuf 573da72b998SYaroslav Tykhiyto 574da72b998SYaroslav Tykhiy.Fa type . 575da72b998SYaroslav TykhiyThis is a relatively expensive operation and should be avoided. 576de06f907SNik Clayton.El 577de06f907SNik Clayton.Pp 578de06f907SNik ClaytonThe functions are: 579de06f907SNik Clayton.Bl -ohang -offset indent 580de06f907SNik Clayton.It Fn m_get how type 581de06f907SNik ClaytonA function version of 582bfd59721SBosko Milekic.Fn MGET 583bfd59721SBosko Milekicfor non-critical paths. 58418f169afSKevin Lo.It Fn m_get2 size how type flags 58518f169afSKevin LoAllocate an 58618f169afSKevin Lo.Vt mbuf 58718f169afSKevin Lowith enough space to hold specified amount of data. 588f052fda5SGordon BerglingIf the size is larger than 58992b56ebaSJohn-Mark Gurney.Dv MJUMPAGESIZE , NULL 59092b56ebaSJohn-Mark Gurneywill be returned. 591a051ca72SKristof Provost.It Fn m_get3 size how type flags 592a051ca72SKristof ProvostAllocate an 593a051ca72SKristof Provost.Vt mbuf 594a051ca72SKristof Provostwith enough space to hold specified amount of data. 595f052fda5SGordon BerglingIf the size is larger than 596a051ca72SKristof Provost.Dv MJUM16BYTES, NULL 597a051ca72SKristof Provostwill be returned. 598328c0bbcSBosko Milekic.It Fn m_getm orig len how type 599328c0bbcSBosko MilekicAllocate 6000b39b3ffSRuslan Ermilov.Fa len 6017b2fd183SYaroslav Tykhiybytes worth of 6027b2fd183SYaroslav Tykhiy.Vt mbufs 6037b2fd183SYaroslav Tykhiyand 6047b2fd183SYaroslav Tykhiy.Vt mbuf clusters 6057b2fd183SYaroslav Tykhiyif necessary and append the resulting allocated 6067b2fd183SYaroslav Tykhiy.Vt mbuf chain 6077b2fd183SYaroslav Tykhiyto the 6087b2fd183SYaroslav Tykhiy.Vt mbuf chain 6097b2fd183SYaroslav Tykhiy.Fa orig , 6107b2fd183SYaroslav Tykhiyif it is 6110b39b3ffSRuslan Ermilov.No non- Ns Dv NULL . 612328c0bbcSBosko MilekicIf the allocation fails at any point, 6130b39b3ffSRuslan Ermilovfree whatever was allocated and return 6140b39b3ffSRuslan Ermilov.Dv NULL . 615328c0bbcSBosko MilekicIf 6160b39b3ffSRuslan Ermilov.Fa orig 6170b39b3ffSRuslan Ermilovis 6180b39b3ffSRuslan Ermilov.No non- Ns Dv NULL , 619328c0bbcSBosko Milekicit will not be freed. 620328c0bbcSBosko MilekicIt is possible to use 621328c0bbcSBosko Milekic.Fn m_getm 622328c0bbcSBosko Milekicto either append 6230b39b3ffSRuslan Ermilov.Fa len 6247b2fd183SYaroslav Tykhiybytes to an existing 6257b2fd183SYaroslav Tykhiy.Vt mbuf 6267b2fd183SYaroslav Tykhiyor 6277b2fd183SYaroslav Tykhiy.Vt mbuf chain 628328c0bbcSBosko Milekic(for example, one which may be sitting in a pre-allocated ring) 6297b2fd183SYaroslav Tykhiyor to simply perform an all-or-nothing 6307b2fd183SYaroslav Tykhiy.Vt mbuf 6317b2fd183SYaroslav Tykhiyand 6327b2fd183SYaroslav Tykhiy.Vt mbuf cluster 6337b2fd183SYaroslav Tykhiyallocation. 634de06f907SNik Clayton.It Fn m_gethdr how type 635de06f907SNik ClaytonA function version of 636bfd59721SBosko Milekic.Fn MGETHDR 637bfd59721SBosko Milekicfor non-critical paths. 6386c013706SMaxim Konovalov.It Fn m_getcl how type flags 6396c013706SMaxim KonovalovFetch an 6406c013706SMaxim Konovalov.Vt mbuf 6416c013706SMaxim Konovalovwith a 6426c013706SMaxim Konovalov.Vt mbuf cluster 6436c013706SMaxim Konovalovattached to it. 6446c013706SMaxim KonovalovIf one of the allocations fails, the entire allocation fails. 6456c013706SMaxim KonovalovThis routine is the preferred way of fetching both the 6466c013706SMaxim Konovalov.Vt mbuf 6476c013706SMaxim Konovalovand 6486c013706SMaxim Konovalov.Vt mbuf cluster 6496c013706SMaxim Konovalovtogether, as it avoids having to unlock/relock between allocations. 6506c013706SMaxim KonovalovReturns 6516c013706SMaxim Konovalov.Dv NULL 6526c013706SMaxim Konovalovon failure. 6535c35eb22SJohn-Mark Gurney.It Fn m_getjcl how type flags size 6545c35eb22SJohn-Mark GurneyThis is like 6555c35eb22SJohn-Mark Gurney.Fn m_getcl 656b6dd8b71SJohn-Mark Gurneybut the specified 6575c35eb22SJohn-Mark Gurney.Fa size 658b6dd8b71SJohn-Mark Gurneyof the cluster to be allocated must be one of 659b6dd8b71SJohn-Mark Gurney.Dv MCLBYTES , MJUMPAGESIZE , MJUM9BYTES , 660b6dd8b71SJohn-Mark Gurneyor 661b6dd8b71SJohn-Mark Gurney.Dv MJUM16BYTES . 66201e7fdafSMaxim Konovalov.It Fn m_free mbuf 66301e7fdafSMaxim KonovalovFrees 66401e7fdafSMaxim Konovalov.Vt mbuf . 665b7131a26SJohn-Mark GurneyReturns 666b7131a26SJohn-Mark Gurney.Va m_next 667b7131a26SJohn-Mark Gurneyof the freed 668b7131a26SJohn-Mark Gurney.Vt mbuf . 669de06f907SNik Clayton.El 670de06f907SNik Clayton.Pp 6717b2fd183SYaroslav TykhiyThe functions below operate on 6727b2fd183SYaroslav Tykhiy.Vt mbuf chains . 673de06f907SNik Clayton.Bl -ohang -offset indent 674de06f907SNik Clayton.It Fn m_freem mbuf 6757b2fd183SYaroslav TykhiyFree an entire 6767b2fd183SYaroslav Tykhiy.Vt mbuf chain , 6777b2fd183SYaroslav Tykhiyincluding any external storage. 678de06f907SNik Clayton.\" 679de06f907SNik Clayton.It Fn m_adj mbuf len 680de06f907SNik ClaytonTrim 681de06f907SNik Clayton.Fa len 6827b2fd183SYaroslav Tykhiybytes from the head of an 6837b2fd183SYaroslav Tykhiy.Vt mbuf chain 6847b2fd183SYaroslav Tykhiyif 685de06f907SNik Clayton.Fa len 686de06f907SNik Claytonis positive, from the tail otherwise. 687de06f907SNik Clayton.\" 688b9def06bSSam Leffler.It Fn m_append mbuf len cp 689b9def06bSSam LefflerAppend 690b9def06bSSam Leffler.Vt len 691b9def06bSSam Lefflerbytes of data 692b9def06bSSam Leffler.Vt cp 693b9def06bSSam Lefflerto the 694b9def06bSSam Leffler.Vt mbuf chain . 695b9def06bSSam LefflerExtend the mbuf chain if the new data does not fit in 696b9def06bSSam Lefflerexisting space. 697b9def06bSSam Leffler.\" 698de06f907SNik Clayton.It Fn m_prepend mbuf len how 6997b2fd183SYaroslav TykhiyAllocate a new 7007b2fd183SYaroslav Tykhiy.Vt mbuf 7017b2fd183SYaroslav Tykhiyand prepend it to the 7027b2fd183SYaroslav Tykhiy.Vt mbuf chain , 7037b2fd183SYaroslav Tykhiyhandle 704de06f907SNik Clayton.Dv M_PKTHDR 705de06f907SNik Claytonproperly. 706de06f907SNik Clayton.Sy Note : 7070227791bSRuslan ErmilovIt does not allocate any 7087b2fd183SYaroslav Tykhiy.Vt mbuf clusters , 7097b2fd183SYaroslav Tykhiyso 710de06f907SNik Clayton.Fa len 711de06f907SNik Claytonmust be less than 712de06f907SNik Clayton.Dv MLEN 713de06f907SNik Claytonor 714de06f907SNik Clayton.Dv MHLEN , 715de06f907SNik Claytondepending on the 716d0353b83SRuslan Ermilov.Dv M_PKTHDR 717d0353b83SRuslan Ermilovflag setting. 718de06f907SNik Clayton.\" 7197ac139a9SJohn-Mark Gurney.It Fn m_copyup mbuf len dstoff 7207ac139a9SJohn-Mark GurneySimilar to 7217ac139a9SJohn-Mark Gurney.Fn m_pullup 7227ac139a9SJohn-Mark Gurneybut copies 7237ac139a9SJohn-Mark Gurney.Fa len 7247ac139a9SJohn-Mark Gurneybytes of data into a new mbuf at 7257ac139a9SJohn-Mark Gurney.Fa dstoff 7267ac139a9SJohn-Mark Gurneybytes into the mbuf. 7277ac139a9SJohn-Mark GurneyThe 7287ac139a9SJohn-Mark Gurney.Fa dstoff 7297ac139a9SJohn-Mark Gurneyargument aligns the data and leaves room for a link layer header. 7304f068961SRuslan ErmilovReturns the new 7317ac139a9SJohn-Mark Gurney.Vt mbuf chain 7327ac139a9SJohn-Mark Gurneyon success, 7337ac139a9SJohn-Mark Gurneyand frees the 7347ac139a9SJohn-Mark Gurney.Vt mbuf chain 7357ac139a9SJohn-Mark Gurneyand returns 7367ac139a9SJohn-Mark Gurney.Dv NULL 7377ac139a9SJohn-Mark Gurneyon failure. 7387ac139a9SJohn-Mark Gurney.Sy Note : 7397ac139a9SJohn-Mark GurneyThe function does not allocate 7407ac139a9SJohn-Mark Gurney.Vt mbuf clusters , 7417ac139a9SJohn-Mark Gurneyso 7427ac139a9SJohn-Mark Gurney.Fa len + dstoff 7437ac139a9SJohn-Mark Gurneymust be less than 7447ac139a9SJohn-Mark Gurney.Dv MHLEN . 7457ac139a9SJohn-Mark Gurney.\" 746de06f907SNik Clayton.It Fn m_pullup mbuf len 747de06f907SNik ClaytonArrange that the first 748de06f907SNik Clayton.Fa len 7497b2fd183SYaroslav Tykhiybytes of an 7507b2fd183SYaroslav Tykhiy.Vt mbuf chain 7517b2fd183SYaroslav Tykhiyare contiguous and lay in the data area of 752de06f907SNik Clayton.Fa mbuf , 753de06f907SNik Claytonso they are accessible with 754de06f907SNik Clayton.Fn mtod mbuf type . 7551009bd1bSJulian ElischerIt is important to remember that this may involve 7561009bd1bSJulian Elischerreallocating some mbufs and moving data so all pointers 7571009bd1bSJulian Elischerreferencing data within the old mbuf chain 7581009bd1bSJulian Elischermust be recalculated or made invalid. 7597b2fd183SYaroslav TykhiyReturn the new 7607b2fd183SYaroslav Tykhiy.Vt mbuf chain 7617b2fd183SYaroslav Tykhiyon success, 762de06f907SNik Clayton.Dv NULL 763de06f907SNik Claytonon failure 7647b2fd183SYaroslav Tykhiy(the 7657b2fd183SYaroslav Tykhiy.Vt mbuf chain 7667b2fd183SYaroslav Tykhiyis freed in this case). 767de06f907SNik Clayton.Sy Note : 7680227791bSRuslan ErmilovIt does not allocate any 7697b2fd183SYaroslav Tykhiy.Vt mbuf clusters , 7707b2fd183SYaroslav Tykhiyso 771de06f907SNik Clayton.Fa len 772969ed8f7SKevin Lomust be less than or equal to 773de06f907SNik Clayton.Dv MHLEN . 774de06f907SNik Clayton.\" 775d305f4b9SBruce M Simpson.It Fn m_pulldown mbuf offset len offsetp 776d305f4b9SBruce M SimpsonArrange that 777d305f4b9SBruce M Simpson.Fa len 778d305f4b9SBruce M Simpsonbytes between 779d305f4b9SBruce M Simpson.Fa offset 780d305f4b9SBruce M Simpsonand 781d305f4b9SBruce M Simpson.Fa offset + len 782d305f4b9SBruce M Simpsonin the 783d305f4b9SBruce M Simpson.Vt mbuf chain 784d305f4b9SBruce M Simpsonare contiguous and lay in the data area of 785d305f4b9SBruce M Simpson.Fa mbuf , 786d305f4b9SBruce M Simpsonso they are accessible with 787854e1fd6SJohn Baldwin.Fn mtod 788854e1fd6SJohn Baldwinor 789854e1fd6SJohn Baldwin.Fn mtodo . 79073bbeaa5SGlen Barber.Fa len 79173bbeaa5SGlen Barbermust be smaller than, or equal to, the size of an 792d305f4b9SBruce M Simpson.Vt mbuf cluster . 793d305f4b9SBruce M SimpsonReturn a pointer to an intermediate 794d305f4b9SBruce M Simpson.Vt mbuf 795d305f4b9SBruce M Simpsonin the chain containing the requested region; 796d305f4b9SBruce M Simpsonthe offset in the data region of the 797d305f4b9SBruce M Simpson.Vt mbuf chain 798d305f4b9SBruce M Simpsonto the data contained in the returned mbuf is stored in 799d305f4b9SBruce M Simpson.Fa *offsetp . 800d305f4b9SBruce M SimpsonIf 801e0912e08SJohn-Mark Gurney.Fa offsetp 802d305f4b9SBruce M Simpsonis NULL, the region may be accessed using 803854e1fd6SJohn Baldwin.Fn mtod mbuf type 804854e1fd6SJohn Baldwinor 805854e1fd6SJohn Baldwin.Fn mtodo mbuf 0 . 806d305f4b9SBruce M SimpsonIf 807e0912e08SJohn-Mark Gurney.Fa offsetp 808d305f4b9SBruce M Simpsonis non-NULL, the region may be accessed using 809854e1fd6SJohn Baldwin.Fn mtodo mbuf *offsetp . 810d305f4b9SBruce M SimpsonThe region of the mbuf chain between its beginning and 811e0912e08SJohn-Mark Gurney.Fa offset 812d305f4b9SBruce M Simpsonis not modified, therefore it is safe to hold pointers to data within 813d305f4b9SBruce M Simpsonthis region before calling 814d305f4b9SBruce M Simpson.Fn m_pulldown . 815d305f4b9SBruce M Simpson.\" 816de06f907SNik Clayton.It Fn m_copym mbuf offset len how 8177b2fd183SYaroslav TykhiyMake a copy of an 8187b2fd183SYaroslav Tykhiy.Vt mbuf chain 8197b2fd183SYaroslav Tykhiystarting 820de06f907SNik Clayton.Fa offset 821de06f907SNik Claytonbytes from the beginning, continuing for 822de06f907SNik Clayton.Fa len 823e73145f6SSheldon Hearnbytes. 824e73145f6SSheldon HearnIf 825de06f907SNik Clayton.Fa len 826de06f907SNik Claytonis 827de06f907SNik Clayton.Dv M_COPYALL , 8287b2fd183SYaroslav Tykhiycopy to the end of the 8297b2fd183SYaroslav Tykhiy.Vt mbuf chain . 830de06f907SNik Clayton.Sy Note : 8317b2fd183SYaroslav TykhiyThe copy is read-only, because the 8327b2fd183SYaroslav Tykhiy.Vt mbuf clusters 8337b2fd183SYaroslav Tykhiyare not copied, only their reference counts are incremented. 834de06f907SNik Clayton.\" 835de06f907SNik Clayton.It Fn m_copypacket mbuf how 836de06f907SNik ClaytonCopy an entire packet including header, which must be present. 837de06f907SNik ClaytonThis is an optimized version of the common case 838de06f907SNik Clayton.Fn m_copym mbuf 0 M_COPYALL how . 839de06f907SNik Clayton.Sy Note : 8407b2fd183SYaroslav Tykhiythe copy is read-only, because the 8417b2fd183SYaroslav Tykhiy.Vt mbuf clusters 8427b2fd183SYaroslav Tykhiyare not copied, only their reference counts are incremented. 843de06f907SNik Clayton.\" 844de06f907SNik Clayton.It Fn m_dup mbuf how 8457b2fd183SYaroslav TykhiyCopy a packet header 8467b2fd183SYaroslav Tykhiy.Vt mbuf chain 8477b2fd183SYaroslav Tykhiyinto a completely new 8487b2fd183SYaroslav Tykhiy.Vt mbuf chain , 8497b2fd183SYaroslav Tykhiyincluding copying any 8507b2fd183SYaroslav Tykhiy.Vt mbuf clusters . 851e73145f6SSheldon HearnUse this instead of 852de06f907SNik Clayton.Fn m_copypacket 8537b2fd183SYaroslav Tykhiywhen you need a writable copy of an 8547b2fd183SYaroslav Tykhiy.Vt mbuf chain . 855de06f907SNik Clayton.\" 856de06f907SNik Clayton.It Fn m_copydata mbuf offset len buf 8577b2fd183SYaroslav TykhiyCopy data from an 8587b2fd183SYaroslav Tykhiy.Vt mbuf chain 8597b2fd183SYaroslav Tykhiystarting 860de06f907SNik Clayton.Fa off 861de06f907SNik Claytonbytes from the beginning, continuing for 862de06f907SNik Clayton.Fa len 863de06f907SNik Claytonbytes, into the indicated buffer 864de06f907SNik Clayton.Fa buf . 865de06f907SNik Clayton.\" 866de06f907SNik Clayton.It Fn m_copyback mbuf offset len buf 867de06f907SNik ClaytonCopy 868de06f907SNik Clayton.Fa len 869de06f907SNik Claytonbytes from the buffer 870de06f907SNik Clayton.Fa buf 8717b2fd183SYaroslav Tykhiyback into the indicated 8727b2fd183SYaroslav Tykhiy.Vt mbuf chain , 873de06f907SNik Claytonstarting at 874de06f907SNik Clayton.Fa offset 8757b2fd183SYaroslav Tykhiybytes from the beginning of the 8767b2fd183SYaroslav Tykhiy.Vt mbuf chain , 8777b2fd183SYaroslav Tykhiyextending the 8787b2fd183SYaroslav Tykhiy.Vt mbuf chain 8797b2fd183SYaroslav Tykhiyif necessary. 880de06f907SNik Clayton.Sy Note : 8810227791bSRuslan ErmilovIt does not allocate any 8827b2fd183SYaroslav Tykhiy.Vt mbuf clusters , 8837b2fd183SYaroslav Tykhiyjust adds 8847b2fd183SYaroslav Tykhiy.Vt mbufs 8857b2fd183SYaroslav Tykhiyto the 8867b2fd183SYaroslav Tykhiy.Vt mbuf chain . 88736a142c4SRuslan ErmilovIt is safe to set 888de06f907SNik Clayton.Fa offset 8897b2fd183SYaroslav Tykhiybeyond the current 8907b2fd183SYaroslav Tykhiy.Vt mbuf chain 8917b2fd183SYaroslav Tykhiyend: zeroed 8927b2fd183SYaroslav Tykhiy.Vt mbufs 8937b2fd183SYaroslav Tykhiywill be allocated to fill the space. 894de06f907SNik Clayton.\" 8955d96084fSYaroslav Tykhiy.It Fn m_length mbuf last 8967b2fd183SYaroslav TykhiyReturn the length of the 8977b2fd183SYaroslav Tykhiy.Vt mbuf chain , 8987b2fd183SYaroslav Tykhiyand optionally a pointer to the last 8997b2fd183SYaroslav Tykhiy.Vt mbuf . 9000b1c0994SPoul-Henning Kamp.\" 901da72b998SYaroslav Tykhiy.It Fn m_dup_pkthdr to from how 902da72b998SYaroslav TykhiyUpon the function's completion, the 903da72b998SYaroslav Tykhiy.Vt mbuf 904da72b998SYaroslav Tykhiy.Fa to 905da72b998SYaroslav Tykhiywill contain an identical copy of 906da72b998SYaroslav Tykhiy.Fa from->m_pkthdr 907da72b998SYaroslav Tykhiyand the per-packet attributes found in the 908da72b998SYaroslav Tykhiy.Vt mbuf chain 909da72b998SYaroslav Tykhiy.Fa from . 910da72b998SYaroslav TykhiyThe 911da72b998SYaroslav Tykhiy.Vt mbuf 912da72b998SYaroslav Tykhiy.Fa from 913da72b998SYaroslav Tykhiymust have the flag 914da72b998SYaroslav Tykhiy.Dv M_PKTHDR 915da72b998SYaroslav Tykhiyinitially set, and 916da72b998SYaroslav Tykhiy.Fa to 917da72b998SYaroslav Tykhiymust be empty on entry. 918da72b998SYaroslav Tykhiy.\" 919da72b998SYaroslav Tykhiy.It Fn m_move_pkthdr to from 920da72b998SYaroslav TykhiyMove 921da72b998SYaroslav Tykhiy.Va m_pkthdr 922da72b998SYaroslav Tykhiyand the per-packet attributes from the 923da72b998SYaroslav Tykhiy.Vt mbuf chain 924da72b998SYaroslav Tykhiy.Fa from 925da72b998SYaroslav Tykhiyto the 926da72b998SYaroslav Tykhiy.Vt mbuf 927da72b998SYaroslav Tykhiy.Fa to . 928da72b998SYaroslav TykhiyThe 929da72b998SYaroslav Tykhiy.Vt mbuf 930da72b998SYaroslav Tykhiy.Fa from 931da72b998SYaroslav Tykhiymust have the flag 932da72b998SYaroslav Tykhiy.Dv M_PKTHDR 933da72b998SYaroslav Tykhiyinitially set, and 934da72b998SYaroslav Tykhiy.Fa to 935da72b998SYaroslav Tykhiymust be empty on entry. 936da72b998SYaroslav TykhiyUpon the function's completion, 937da72b998SYaroslav Tykhiy.Fa from 938da72b998SYaroslav Tykhiywill have the flag 939da72b998SYaroslav Tykhiy.Dv M_PKTHDR 940da72b998SYaroslav Tykhiyand the per-packet attributes cleared. 941da72b998SYaroslav Tykhiy.\" 9425d96084fSYaroslav Tykhiy.It Fn m_fixhdr mbuf 9437b2fd183SYaroslav TykhiySet the packet-header length to the length of the 9447b2fd183SYaroslav Tykhiy.Vt mbuf chain . 9450b1c0994SPoul-Henning Kamp.\" 946de06f907SNik Clayton.It Fn m_devget buf len offset ifp copy 947de06f907SNik ClaytonCopy data from a device local memory pointed to by 948de06f907SNik Clayton.Fa buf 9497b2fd183SYaroslav Tykhiyto an 9507b2fd183SYaroslav Tykhiy.Vt mbuf chain . 951e73145f6SSheldon HearnThe copy is done using a specified copy routine 952de06f907SNik Clayton.Fa copy , 953de06f907SNik Claytonor 954de06f907SNik Clayton.Fn bcopy 955de06f907SNik Claytonif 956de06f907SNik Clayton.Fa copy 957de06f907SNik Claytonis 958de06f907SNik Clayton.Dv NULL . 959de06f907SNik Clayton.\" 960de06f907SNik Clayton.It Fn m_cat m n 961de06f907SNik ClaytonConcatenate 962de06f907SNik Clayton.Fa n 963de06f907SNik Claytonto 964de06f907SNik Clayton.Fa m . 9657b2fd183SYaroslav TykhiyBoth 9667b2fd183SYaroslav Tykhiy.Vt mbuf chains 9677b2fd183SYaroslav Tykhiymust be of the same type. 968a3f6b029SMark Johnston.Fa n 969a3f6b029SMark Johnstonis not guaranteed to be valid after 970a3f6b029SMark Johnston.Fn m_cat 971a3f6b029SMark Johnstonreturns. 972a3f6b029SMark Johnston.Fn m_cat 973a3f6b029SMark Johnstondoes not update any packet header fields or free mbuf tags. 974a3f6b029SMark Johnston.\" 975a3f6b029SMark Johnston.It Fn m_catpkt m n 976a3f6b029SMark JohnstonA variant of 977a3f6b029SMark Johnston.Fn m_cat 978a3f6b029SMark Johnstonthat operates on packets. 979a3f6b029SMark JohnstonBoth 980a3f6b029SMark Johnston.Fa m 981a3f6b029SMark Johnstonand 982a3f6b029SMark Johnston.Fa n 983a3f6b029SMark Johnstonmust contain packet headers. 984a3f6b029SMark Johnston.Fa n 985a3f6b029SMark Johnstonis not guaranteed to be valid after 986a3f6b029SMark Johnston.Fn m_catpkt 987a3f6b029SMark Johnstonreturns. 988de06f907SNik Clayton.\" 989de06f907SNik Clayton.It Fn m_split mbuf len how 9907b2fd183SYaroslav TykhiyPartition an 9917b2fd183SYaroslav Tykhiy.Vt mbuf chain 9927b2fd183SYaroslav Tykhiyin two pieces, returning the tail: 993de06f907SNik Claytonall but the first 994de06f907SNik Clayton.Fa len 995e73145f6SSheldon Hearnbytes. 996e73145f6SSheldon HearnIn case of failure, it returns 997de06f907SNik Clayton.Dv NULL 9987b2fd183SYaroslav Tykhiyand attempts to restore the 9997b2fd183SYaroslav Tykhiy.Vt mbuf chain 10007b2fd183SYaroslav Tykhiyto its original state. 100102312219SBruce M Simpson.\" 100202312219SBruce M Simpson.It Fn m_apply mbuf off len f arg 100302312219SBruce M SimpsonApply a function to an 100402312219SBruce M Simpson.Vt mbuf chain , 100502312219SBruce M Simpsonat offset 100602312219SBruce M Simpson.Fa off , 100702312219SBruce M Simpsonfor length 1008f48cbcdeSRuslan Ermilov.Fa len 1009f48cbcdeSRuslan Ermilovbytes. 101002312219SBruce M SimpsonTypically used to avoid calls to 101102312219SBruce M Simpson.Fn m_pullup 101202312219SBruce M Simpsonwhich would otherwise be unnecessary or undesirable. 101302312219SBruce M Simpson.Fa arg 101402312219SBruce M Simpsonis a convenience argument which is passed to the callback function 101502312219SBruce M Simpson.Fa f . 101602312219SBruce M Simpson.Pp 101702312219SBruce M SimpsonEach time 101802312219SBruce M Simpson.Fn f 101902312219SBruce M Simpsonis called, it will be passed 102002312219SBruce M Simpson.Fa arg , 102102312219SBruce M Simpsona pointer to the 102202312219SBruce M Simpson.Fa data 102302312219SBruce M Simpsonin the current mbuf, and the length 102402312219SBruce M Simpson.Fa len 102502312219SBruce M Simpsonof the data in this mbuf to which the function should be applied. 102602312219SBruce M Simpson.Pp 102702312219SBruce M SimpsonThe function should return zero to indicate success; 102802312219SBruce M Simpsonotherwise, if an error is indicated, then 102902312219SBruce M Simpson.Fn m_apply 103002312219SBruce M Simpsonwill return the error and stop iterating through the 103102312219SBruce M Simpson.Vt mbuf chain . 103202312219SBruce M Simpson.\" 103302312219SBruce M Simpson.It Fn m_getptr mbuf loc off 103402312219SBruce M SimpsonReturn a pointer to the mbuf containing the data located at 103502312219SBruce M Simpson.Fa loc 103602312219SBruce M Simpsonbytes from the beginning of the 103702312219SBruce M Simpson.Vt mbuf chain . 103802312219SBruce M SimpsonThe corresponding offset into the mbuf will be stored in 103902312219SBruce M Simpson.Fa *off . 1040a61bc764SMike Silbersack.It Fn m_defrag m0 how 1041f48cbcdeSRuslan ErmilovDefragment an mbuf chain, returning the shortest possible 1042bdf86185SMike Silbersackchain of mbufs and clusters. 1043bdf86185SMike SilbersackIf allocation fails and this can not be completed, 1044bdf86185SMike Silbersack.Dv NULL 1045bdf86185SMike Silbersackwill be returned and the original chain will be unchanged. 1046bdf86185SMike SilbersackUpon success, the original chain will be freed and the new 1047bdf86185SMike Silbersackchain will be returned. 1048a61bc764SMike Silbersack.Fa how 1049a61bc764SMike Silbersackshould be either 1050fa05d215SGleb Smirnoff.Dv M_WAITOK 1051a61bc764SMike Silbersackor 1052fa05d215SGleb Smirnoff.Dv M_NOWAIT , 1053a61bc764SMike Silbersackdepending on the caller's preference. 1054a61bc764SMike Silbersack.Pp 1055a61bc764SMike SilbersackThis function is especially useful in network drivers, where 1056a61bc764SMike Silbersackcertain long mbuf chains must be shortened before being added 1057a61bc764SMike Silbersackto TX descriptor lists. 1058f66b6464SMark Johnston.It Fn m_collapse m0 how maxfrags 1059f66b6464SMark JohnstonDefragment an mbuf chain, returning a chain of at most 1060f66b6464SMark Johnston.Fa maxfrags 1061f66b6464SMark Johnstonmbufs and clusters. 1062f66b6464SMark JohnstonIf allocation fails or the chain cannot be collapsed as requested, 1063f66b6464SMark Johnston.Dv NULL 1064f66b6464SMark Johnstonwill be returned, with the original chain possibly modified. 1065f66b6464SMark JohnstonAs with 1066f66b6464SMark Johnston.Fn m_defrag , 1067f66b6464SMark Johnston.Fa how 1068f66b6464SMark Johnstonshould be one of 1069f66b6464SMark Johnston.Dv M_WAITOK 1070f66b6464SMark Johnstonor 1071f66b6464SMark Johnston.Dv M_NOWAIT . 107247e2996eSSam Leffler.It Fn m_unshare m0 how 107347e2996eSSam LefflerCreate a version of the specified mbuf chain whose 107447e2996eSSam Lefflercontents can be safely modified without affecting other users. 107547e2996eSSam LefflerIf allocation fails and this operation can not be completed, 107647e2996eSSam Leffler.Dv NULL 107747e2996eSSam Lefflerwill be returned. 107847e2996eSSam LefflerThe original mbuf chain is always reclaimed and the reference 107947e2996eSSam Lefflercount of any shared mbuf clusters is decremented. 108047e2996eSSam Leffler.Fa how 108147e2996eSSam Lefflershould be either 1082fa05d215SGleb Smirnoff.Dv M_WAITOK 108347e2996eSSam Leffleror 1084fa05d215SGleb Smirnoff.Dv M_NOWAIT , 108547e2996eSSam Lefflerdepending on the caller's preference. 108647e2996eSSam LefflerAs a side-effect of this process the returned 108747e2996eSSam Lefflermbuf chain may be compacted. 108847e2996eSSam Leffler.Pp 108947e2996eSSam LefflerThis function is especially useful in the transmit path of 109047e2996eSSam Lefflernetwork code, when data must be encrypted or otherwise 109147e2996eSSam Leffleraltered prior to transmission. 10923136363fSRuslan Ermilov.El 10930193eb5eSYaroslav Tykhiy.Sh HARDWARE-ASSISTED CHECKSUM CALCULATION 10940193eb5eSYaroslav TykhiyThis section currently applies to TCP/IP only. 10950193eb5eSYaroslav TykhiyIn order to save the host CPU resources, computing checksums is 10960193eb5eSYaroslav Tykhiyoffloaded to the network interface hardware if possible. 10970193eb5eSYaroslav TykhiyThe 10980193eb5eSYaroslav Tykhiy.Va m_pkthdr 10990193eb5eSYaroslav Tykhiymember of the leading 11000193eb5eSYaroslav Tykhiy.Vt mbuf 11010193eb5eSYaroslav Tykhiyof a packet contains two fields used for that purpose, 11020193eb5eSYaroslav Tykhiy.Vt int Va csum_flags 11030193eb5eSYaroslav Tykhiyand 11040193eb5eSYaroslav Tykhiy.Vt int Va csum_data . 11050193eb5eSYaroslav TykhiyThe meaning of those fields depends on the direction a packet flows in, 11060193eb5eSYaroslav Tykhiyand on whether the packet is fragmented. 11070193eb5eSYaroslav TykhiyHenceforth, 11080193eb5eSYaroslav Tykhiy.Va csum_flags 11090193eb5eSYaroslav Tykhiyor 11100193eb5eSYaroslav Tykhiy.Va csum_data 11110193eb5eSYaroslav Tykhiyof a packet 11120193eb5eSYaroslav Tykhiywill denote the corresponding field of the 11130193eb5eSYaroslav Tykhiy.Va m_pkthdr 11140193eb5eSYaroslav Tykhiymember of the leading 11150193eb5eSYaroslav Tykhiy.Vt mbuf 11160193eb5eSYaroslav Tykhiyin the 11170193eb5eSYaroslav Tykhiy.Vt mbuf chain 11180193eb5eSYaroslav Tykhiycontaining the packet. 11190193eb5eSYaroslav Tykhiy.Pp 11200193eb5eSYaroslav TykhiyOn output, checksum offloading is attempted after the outgoing 11210193eb5eSYaroslav Tykhiyinterface has been determined for a packet. 11220193eb5eSYaroslav TykhiyThe interface-specific field 11230193eb5eSYaroslav Tykhiy.Va ifnet.if_data.ifi_hwassist 11240193eb5eSYaroslav Tykhiy(see 11250193eb5eSYaroslav Tykhiy.Xr ifnet 9 ) 11260193eb5eSYaroslav Tykhiyis consulted for the capabilities of the interface to assist in 11270193eb5eSYaroslav Tykhiycomputing checksums. 11280193eb5eSYaroslav TykhiyThe 11290193eb5eSYaroslav Tykhiy.Va csum_flags 1130f48cbcdeSRuslan Ermilovfield of the packet header is set to indicate which actions the interface 11310193eb5eSYaroslav Tykhiyis supposed to perform on it. 11320193eb5eSYaroslav TykhiyThe actions unsupported by the network interface are done in the 11330193eb5eSYaroslav Tykhiysoftware prior to passing the packet down to the interface driver; 11340193eb5eSYaroslav Tykhiysuch actions will never be requested through 11350193eb5eSYaroslav Tykhiy.Va csum_flags . 11360193eb5eSYaroslav Tykhiy.Pp 11370193eb5eSYaroslav TykhiyThe flags demanding a particular action from an interface are as follows: 11380193eb5eSYaroslav Tykhiy.Bl -tag -width ".Dv CSUM_TCP" -offset indent 11390193eb5eSYaroslav Tykhiy.It Dv CSUM_IP 11400193eb5eSYaroslav TykhiyThe IP header checksum is to be computed and stored in the 11410193eb5eSYaroslav Tykhiycorresponding field of the packet. 11420193eb5eSYaroslav TykhiyThe hardware is expected to know the format of an IP header 11430193eb5eSYaroslav Tykhiyto determine the offset of the IP checksum field. 11440193eb5eSYaroslav Tykhiy.It Dv CSUM_TCP 1145f48cbcdeSRuslan ErmilovThe TCP checksum is to be computed. 1146f48cbcdeSRuslan Ermilov(See below.) 11470193eb5eSYaroslav Tykhiy.It Dv CSUM_UDP 1148f48cbcdeSRuslan ErmilovThe UDP checksum is to be computed. 1149f48cbcdeSRuslan Ermilov(See below.) 11500193eb5eSYaroslav Tykhiy.El 11510193eb5eSYaroslav Tykhiy.Pp 1152fbde705aSYaroslav TykhiyShould a TCP or UDP checksum be offloaded to the hardware, 11530193eb5eSYaroslav Tykhiythe field 11540193eb5eSYaroslav Tykhiy.Va csum_data 11550193eb5eSYaroslav Tykhiywill contain the byte offset of the checksum field relative to the 11560193eb5eSYaroslav Tykhiyend of the IP header. 11570193eb5eSYaroslav TykhiyIn this case, the checksum field will be initially 11580193eb5eSYaroslav Tykhiyset by the TCP/IP module to the checksum of the pseudo header 1159fbde705aSYaroslav Tykhiydefined by the TCP and UDP specifications. 11600193eb5eSYaroslav Tykhiy.Pp 11610193eb5eSYaroslav TykhiyOn input, an interface indicates the actions it has performed 11620193eb5eSYaroslav Tykhiyon a packet by setting one or more of the following flags in 11630193eb5eSYaroslav Tykhiy.Va csum_flags 11640193eb5eSYaroslav Tykhiyassociated with the packet: 11650193eb5eSYaroslav Tykhiy.Bl -tag -width ".Dv CSUM_IP_CHECKED" -offset indent 11660193eb5eSYaroslav Tykhiy.It Dv CSUM_IP_CHECKED 11670193eb5eSYaroslav TykhiyThe IP header checksum has been computed. 11680193eb5eSYaroslav Tykhiy.It Dv CSUM_IP_VALID 11690193eb5eSYaroslav TykhiyThe IP header has a valid checksum. 11700193eb5eSYaroslav TykhiyThis flag can appear only in combination with 11710193eb5eSYaroslav Tykhiy.Dv CSUM_IP_CHECKED . 11720193eb5eSYaroslav Tykhiy.It Dv CSUM_DATA_VALID 11730193eb5eSYaroslav TykhiyThe checksum of the data portion of the IP packet has been computed 11740193eb5eSYaroslav Tykhiyand stored in the field 11750193eb5eSYaroslav Tykhiy.Va csum_data 11760193eb5eSYaroslav Tykhiyin network byte order. 11770193eb5eSYaroslav Tykhiy.It Dv CSUM_PSEUDO_HDR 11780193eb5eSYaroslav TykhiyCan be set only along with 11790193eb5eSYaroslav Tykhiy.Dv CSUM_DATA_VALID 11800193eb5eSYaroslav Tykhiyto indicate that the IP data checksum found in 11810193eb5eSYaroslav Tykhiy.Va csum_data 1182fbde705aSYaroslav Tykhiyallows for the pseudo header defined by the TCP and UDP specifications. 11830193eb5eSYaroslav TykhiyOtherwise the checksum of the pseudo header must be calculated by 11840193eb5eSYaroslav Tykhiythe host CPU and added to 11850193eb5eSYaroslav Tykhiy.Va csum_data 1186fbde705aSYaroslav Tykhiyto obtain the final checksum to be used for TCP or UDP validation purposes. 11870193eb5eSYaroslav Tykhiy.El 11880193eb5eSYaroslav Tykhiy.Pp 11890193eb5eSYaroslav TykhiyIf a particular network interface just indicates success or 11900193eb5eSYaroslav Tykhiyfailure of TCP or UDP checksum validation without returning 11910193eb5eSYaroslav Tykhiythe exact value of the checksum to the host CPU, its driver can mark 11920193eb5eSYaroslav Tykhiy.Dv CSUM_DATA_VALID 11930193eb5eSYaroslav Tykhiyand 11940193eb5eSYaroslav Tykhiy.Dv CSUM_PSEUDO_HDR 11950193eb5eSYaroslav Tykhiyin 11960193eb5eSYaroslav Tykhiy.Va csum_flags , 11970193eb5eSYaroslav Tykhiyand set 11980193eb5eSYaroslav Tykhiy.Va csum_data 11990193eb5eSYaroslav Tykhiyto 12000193eb5eSYaroslav Tykhiy.Li 0xFFFF 12010193eb5eSYaroslav Tykhiyhexadecimal to indicate a valid checksum. 1202f48cbcdeSRuslan ErmilovIt is a peculiarity of the algorithm used that the Internet checksum 12030193eb5eSYaroslav Tykhiycalculated over any valid packet will be 12040193eb5eSYaroslav Tykhiy.Li 0xFFFF 12050193eb5eSYaroslav Tykhiyas long as the original checksum field is included. 12063cab047eSMike Silbersack.Sh STRESS TESTING 1207c8185672SRuslan ErmilovWhen running a kernel compiled with the option 1208c8185672SRuslan Ermilov.Dv MBUF_STRESS_TEST , 1209c8185672SRuslan Ermilovthe following 1210c8185672SRuslan Ermilov.Xr sysctl 8 Ns 1211c8185672SRuslan Ermilov-controlled options may be used to create 12123cab047eSMike Silbersackvarious failure/extreme cases for testing of network drivers 12137b2fd183SYaroslav Tykhiyand other parts of the kernel that rely on 12147b2fd183SYaroslav Tykhiy.Vt mbufs . 1215*9fd66e40SGraham Percival.Bl -tag -width indent 1216c8185672SRuslan Ermilov.It Va net.inet.ip.mbuf_frag_size 1217c8185672SRuslan ErmilovCauses 1218c8185672SRuslan Ermilov.Fn ip_output 12197b2fd183SYaroslav Tykhiyto fragment outgoing 12207b2fd183SYaroslav Tykhiy.Vt mbuf chains 12217b2fd183SYaroslav Tykhiyinto fragments of the specified size. 1222c8185672SRuslan ErmilovSetting this variable to 1 is an excellent way to 12237b2fd183SYaroslav Tykhiytest the long 12247b2fd183SYaroslav Tykhiy.Vt mbuf chain 12257b2fd183SYaroslav Tykhiyhandling ability of network drivers. 1226c8185672SRuslan Ermilov.It Va kern.ipc.m_defragrandomfailures 1227c8185672SRuslan ErmilovCauses the function 1228c8185672SRuslan Ermilov.Fn m_defrag 1229c8185672SRuslan Ermilovto randomly fail, returning 1230c8185672SRuslan Ermilov.Dv NULL . 1231c8185672SRuslan ErmilovAny piece of code which uses 1232c8185672SRuslan Ermilov.Fn m_defrag 1233c8185672SRuslan Ermilovshould be tested with this feature. 1234c8185672SRuslan Ermilov.El 1235de06f907SNik Clayton.Sh RETURN VALUES 1236de06f907SNik ClaytonSee above. 12370193eb5eSYaroslav Tykhiy.Sh SEE ALSO 1238e85cc3f5SRuslan Ermilov.Xr ifnet 9 , 1239e85cc3f5SRuslan Ermilov.Xr mbuf_tags 9 12409048d9a9SGordon Bergling.Rs 12419048d9a9SGordon Bergling.\" 4.4BSD SMM:18 12429048d9a9SGordon Bergling.%A S. J. Leffler 12439048d9a9SGordon Bergling.%A W. N. Joy 12449048d9a9SGordon Bergling.%A R. S. Fabry 12459048d9a9SGordon Bergling.%A M. J. Karels 12469048d9a9SGordon Bergling.%T Networking Implementation Notes 12479048d9a9SGordon Bergling.%B 4.4BSD System Manager's Manual (SMM) 12489048d9a9SGordon Bergling.Re 1249de06f907SNik Clayton.Sh HISTORY 1250de06f907SNik Clayton.\" Please correct me if I'm wrong 12517b2fd183SYaroslav Tykhiy.Vt Mbufs 12527b2fd183SYaroslav Tykhiyappeared in an early version of 1253753d686dSRuslan Ermilov.Bx . 125453b2520dSTom RhodesBesides being used for network packets, they were used 1255bfd59721SBosko Milekicto store various dynamic structures, such as routing table 1256bfd59721SBosko Milekicentries, interface addresses, protocol control blocks, etc. 1257db430f97SRobert WatsonIn more recent 1258db430f97SRobert Watson.Fx 1259db430f97SRobert Watsonuse of 1260db430f97SRobert Watson.Vt mbufs 1261db430f97SRobert Watsonis almost entirely limited to packet storage, with 1262db430f97SRobert Watson.Xr uma 9 1263db430f97SRobert Watsonzones being used directly to store other network-related memory. 1264db430f97SRobert Watson.Pp 1265db430f97SRobert WatsonHistorically, the 1266db430f97SRobert Watson.Vt mbuf 1267db430f97SRobert Watsonallocator has been a special-purpose memory allocator able to run in 1268db430f97SRobert Watsoninterrupt contexts and allocating from a special kernel address space map. 1269db430f97SRobert WatsonAs of 1270db430f97SRobert Watson.Fx 5.3 , 1271db430f97SRobert Watsonthe 1272db430f97SRobert Watson.Vt mbuf 1273db430f97SRobert Watsonallocator is a wrapper around 1274db430f97SRobert Watson.Xr uma 9 , 1275db430f97SRobert Watsonallowing caching of 1276db430f97SRobert Watson.Vt mbufs , 1277db430f97SRobert Watsonclusters, and 1278db430f97SRobert Watson.Vt mbuf 1279db430f97SRobert Watson+ cluster pairs in per-CPU caches, as well as bringing other benefits of 1280db430f97SRobert Watsonslab allocation. 1281de06f907SNik Clayton.Sh AUTHORS 12826575e6daSRuslan ErmilovThe original 12836575e6daSRuslan Ermilov.Nm 12841e9469d1SChristian Brueffermanual page was written by 12851e9469d1SChristian Brueffer.An Yar Tikhiy . 1286db430f97SRobert WatsonThe 1287db430f97SRobert Watson.Xr uma 9 1288db430f97SRobert Watson.Vt mbuf 12891e9469d1SChristian Bruefferallocator was written by 12901e9469d1SChristian Brueffer.An Bosko Milekic . 1291