xref: /freebsd/share/man/man9/mbuf.9 (revision 2a8c860fe3f3bcfc6ba9206f34d067d998d89c7e)
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.\"
27*2a8c860fSRobert Watson.Dd January 5, 2015
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"
43*2a8c860fSRobert Watson.Ft int
44de06f907SNik Clayton.Fn MCLGET "struct mbuf *mbuf" "int how"
45bfd59721SBosko Milekic.Fo MEXTADD
46bfd59721SBosko Milekic.Fa "struct mbuf *mbuf"
47bfd59721SBosko Milekic.Fa "caddr_t buf"
48bfd59721SBosko Milekic.Fa "u_int size"
49cf827063SPoul-Henning Kamp.Fa "void (*free)(void *opt_arg1, void *opt_arg2)"
50cf827063SPoul-Henning Kamp.Fa "void *opt_arg1"
51cf827063SPoul-Henning Kamp.Fa "void *opt_arg2"
525d4050ccSBosko Milekic.Fa "short flags"
535d4050ccSBosko Milekic.Fa "int type"
54bfd59721SBosko Milekic.Fc
55bfd59721SBosko Milekic.Fn MEXTFREE "struct mbuf *mbuf"
56bfd59721SBosko Milekic.Fn MFREE "struct mbuf *mbuf" "struct mbuf *successor"
57de06f907SNik Clayton.\"
58bfd59721SBosko Milekic.Ss Mbuf utility macros
59f16b3c0dSChad David.Fn mtod "struct mbuf *mbuf" "type"
60bfd59721SBosko Milekic.Fn M_ALIGN "struct mbuf *mbuf" "u_int len"
61bfd59721SBosko Milekic.Fn MH_ALIGN "struct mbuf *mbuf" "u_int len"
62f16b3c0dSChad David.Ft int
63bfd59721SBosko Milekic.Fn M_LEADINGSPACE "struct mbuf *mbuf"
64f16b3c0dSChad David.Ft int
65bfd59721SBosko Milekic.Fn M_TRAILINGSPACE "struct mbuf *mbuf"
66da72b998SYaroslav Tykhiy.Fn M_MOVE_PKTHDR "struct mbuf *to" "struct mbuf *from"
67bfd59721SBosko Milekic.Fn M_PREPEND "struct mbuf *mbuf" "int len" "int how"
68ada46fe9SKevin Lo.Fn MCHTYPE "struct mbuf *mbuf" "short type"
69f16b3c0dSChad David.Ft int
705d4050ccSBosko Milekic.Fn M_WRITABLE "struct mbuf *mbuf"
71bfd59721SBosko Milekic.\"
72bfd59721SBosko Milekic.Ss Mbuf allocation functions
73de06f907SNik Clayton.Ft struct mbuf *
74ada46fe9SKevin Lo.Fn m_get "int how" "short type"
75de06f907SNik Clayton.Ft struct mbuf *
7618f169afSKevin Lo.Fn m_get2 "int size" "int how" "short type" "int flags"
7718f169afSKevin Lo.Ft struct mbuf *
78ada46fe9SKevin Lo.Fn m_getm "struct mbuf *orig" "int len" "int how" "short type"
79328c0bbcSBosko Milekic.Ft struct mbuf *
805c35eb22SJohn-Mark Gurney.Fn m_getjcl "int how" "short type" "int flags" "int size"
815c35eb22SJohn-Mark Gurney.Ft struct mbuf *
826c013706SMaxim Konovalov.Fn m_getcl "int how" "short type" "int flags"
836c013706SMaxim Konovalov.Ft struct mbuf *
84ada46fe9SKevin Lo.Fn m_getclr "int how" "short type"
85de06f907SNik Clayton.Ft struct mbuf *
86ada46fe9SKevin Lo.Fn m_gethdr "int how" "short type"
87bfd59721SBosko Milekic.Ft struct mbuf *
88bfd59721SBosko Milekic.Fn m_free "struct mbuf *mbuf"
89de06f907SNik Clayton.Ft void
90de06f907SNik Clayton.Fn m_freem "struct mbuf *mbuf"
91bfd59721SBosko Milekic.\"
92bfd59721SBosko Milekic.Ss Mbuf utility functions
93de06f907SNik Clayton.Ft void
94de06f907SNik Clayton.Fn m_adj "struct mbuf *mbuf" "int len"
959c3fd404SSam Leffler.Ft void
969c3fd404SSam Leffler.Fn m_align "struct mbuf *mbuf" "int len"
97b9def06bSSam Leffler.Ft int
98b9def06bSSam Leffler.Fn m_append "struct mbuf *mbuf" "int len" "c_caddr_t cp"
99de06f907SNik Clayton.Ft struct mbuf *
100de06f907SNik Clayton.Fn m_prepend "struct mbuf *mbuf" "int len" "int how"
101de06f907SNik Clayton.Ft struct mbuf *
1027ac139a9SJohn-Mark Gurney.Fn m_copyup "struct mbuf *mbuf" "int len" "int dstoff"
1037ac139a9SJohn-Mark Gurney.Ft struct mbuf *
104de06f907SNik Clayton.Fn m_pullup "struct mbuf *mbuf" "int len"
105de06f907SNik Clayton.Ft struct mbuf *
106d305f4b9SBruce M Simpson.Fn m_pulldown "struct mbuf *mbuf" "int offset" "int len" "int *offsetp"
107d305f4b9SBruce M Simpson.Ft struct mbuf *
108de06f907SNik Clayton.Fn m_copym "struct mbuf *mbuf" "int offset" "int len" "int how"
109de06f907SNik Clayton.Ft struct mbuf *
110de06f907SNik Clayton.Fn m_copypacket "struct mbuf *mbuf" "int how"
111de06f907SNik Clayton.Ft struct mbuf *
112de06f907SNik Clayton.Fn m_dup "struct mbuf *mbuf" "int how"
113de06f907SNik Clayton.Ft void
1140d8e4c57SBruce Evans.Fn m_copydata "const struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
115de06f907SNik Clayton.Ft void
116de06f907SNik Clayton.Fn m_copyback "struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
117de06f907SNik Clayton.Ft struct mbuf *
118de06f907SNik Clayton.Fo m_devget
119de06f907SNik Clayton.Fa "char *buf"
120de06f907SNik Clayton.Fa "int len"
121de06f907SNik Clayton.Fa "int offset"
122de06f907SNik Clayton.Fa "struct ifnet *ifp"
123de06f907SNik Clayton.Fa "void (*copy)(char *from, caddr_t to, u_int len)"
124de06f907SNik Clayton.Fc
125de06f907SNik Clayton.Ft void
126de06f907SNik Clayton.Fn m_cat "struct mbuf *m" "struct mbuf *n"
127e60fa837SRuslan Ermilov.Ft u_int
1280b1c0994SPoul-Henning Kamp.Fn m_fixhdr "struct mbuf *mbuf"
129da72b998SYaroslav Tykhiy.Ft void
130da72b998SYaroslav Tykhiy.Fn m_dup_pkthdr "struct mbuf *to" "struct mbuf *from"
131da72b998SYaroslav Tykhiy.Ft void
132da72b998SYaroslav Tykhiy.Fn m_move_pkthdr "struct mbuf *to" "struct mbuf *from"
133e60fa837SRuslan Ermilov.Ft u_int
1340b1c0994SPoul-Henning Kamp.Fn m_length "struct mbuf *mbuf" "struct mbuf **last"
135de06f907SNik Clayton.Ft struct mbuf *
136de06f907SNik Clayton.Fn m_split "struct mbuf *mbuf" "int len" "int how"
13702312219SBruce M Simpson.Ft int
13802312219SBruce M Simpson.Fn m_apply "struct mbuf *mbuf" "int off" "int len" "int (*f)(void *arg, void *data, u_int len)" "void *arg"
13902312219SBruce M Simpson.Ft struct mbuf *
14002312219SBruce M Simpson.Fn m_getptr "struct mbuf *mbuf" "int loc" "int *off"
141a61bc764SMike Silbersack.Ft struct mbuf *
142a61bc764SMike Silbersack.Fn m_defrag "struct mbuf *m0" "int how"
14347e2996eSSam Leffler.Ft struct mbuf *
14447e2996eSSam Leffler.Fn m_unshare "struct mbuf *m0" "int how"
145de06f907SNik Clayton.\"
146de06f907SNik Clayton.Sh DESCRIPTION
1477b2fd183SYaroslav TykhiyAn
1487b2fd183SYaroslav Tykhiy.Vt mbuf
1497b2fd183SYaroslav Tykhiyis a basic unit of memory management in the kernel IPC subsystem.
1507b2fd183SYaroslav TykhiyNetwork packets and socket buffers are stored in
1517b2fd183SYaroslav Tykhiy.Vt mbufs .
1527b2fd183SYaroslav TykhiyA network packet may span multiple
1537b2fd183SYaroslav Tykhiy.Vt mbufs
1547b2fd183SYaroslav Tykhiyarranged into a
1557b2fd183SYaroslav Tykhiy.Vt mbuf chain
156c4d9468eSRuslan Ermilov(linked list),
157de06f907SNik Claytonwhich allows adding or trimming
158bfd59721SBosko Milekicnetwork headers with little overhead.
159de06f907SNik Clayton.Pp
1607b2fd183SYaroslav TykhiyWhile a developer should not bother with
1617b2fd183SYaroslav Tykhiy.Vt mbuf
1627b2fd183SYaroslav Tykhiyinternals without serious
163de06f907SNik Claytonreason in order to avoid incompatibilities with future changes, it
1647b2fd183SYaroslav Tykhiyis useful to understand the general structure of an
1657b2fd183SYaroslav Tykhiy.Vt mbuf .
166de06f907SNik Clayton.Pp
1677b2fd183SYaroslav TykhiyAn
1687b2fd183SYaroslav Tykhiy.Vt mbuf
1697b2fd183SYaroslav Tykhiyconsists of a variable-sized header and a small internal
170e73145f6SSheldon Hearnbuffer for data.
1717b2fd183SYaroslav TykhiyThe total size of an
1727b2fd183SYaroslav Tykhiy.Vt mbuf ,
173bfd59721SBosko Milekic.Dv MSIZE ,
174feccf50cSAlex Dupreis a constant defined in
175feccf50cSAlex Dupre.In sys/param.h .
1767b2fd183SYaroslav TykhiyThe
1777b2fd183SYaroslav Tykhiy.Vt mbuf
1787b2fd183SYaroslav Tykhiyheader includes:
179238d9c59SYaroslav Tykhiy.Bl -tag -width "m_nextpkt" -offset indent
180be82a0bcSYaroslav Tykhiy.It Va m_next
181238d9c59SYaroslav Tykhiy.Pq Vt struct mbuf *
182238d9c59SYaroslav TykhiyA pointer to the next
1837b2fd183SYaroslav Tykhiy.Vt mbuf
1847b2fd183SYaroslav Tykhiyin the
185238d9c59SYaroslav Tykhiy.Vt mbuf chain .
186be82a0bcSYaroslav Tykhiy.It Va m_nextpkt
187238d9c59SYaroslav Tykhiy.Pq Vt struct mbuf *
188238d9c59SYaroslav TykhiyA pointer to the next
1897b2fd183SYaroslav Tykhiy.Vt mbuf chain
190238d9c59SYaroslav Tykhiyin the queue.
191be82a0bcSYaroslav Tykhiy.It Va m_data
192238d9c59SYaroslav Tykhiy.Pq Vt caddr_t
193238d9c59SYaroslav TykhiyA pointer to data attached to this
194238d9c59SYaroslav Tykhiy.Vt mbuf .
195be82a0bcSYaroslav Tykhiy.It Va m_len
196238d9c59SYaroslav Tykhiy.Pq Vt int
197238d9c59SYaroslav TykhiyThe length of the data.
198be82a0bcSYaroslav Tykhiy.It Va m_type
199238d9c59SYaroslav Tykhiy.Pq Vt short
200238d9c59SYaroslav TykhiyThe type of the data.
201be82a0bcSYaroslav Tykhiy.It Va m_flags
202238d9c59SYaroslav Tykhiy.Pq Vt int
203238d9c59SYaroslav TykhiyThe
2047b2fd183SYaroslav Tykhiy.Vt mbuf
205238d9c59SYaroslav Tykhiyflags.
206de06f907SNik Clayton.El
207de06f907SNik Clayton.Pp
2087b2fd183SYaroslav TykhiyThe
2097b2fd183SYaroslav Tykhiy.Vt mbuf
2107b2fd183SYaroslav Tykhiyflag bits are defined as follows:
211de06f907SNik Clayton.Bd -literal
212de06f907SNik Clayton/* mbuf flags */
21333d8df4bSKevin Lo#define	M_EXT		0x00000001 /* has associated external storage */
21433d8df4bSKevin Lo#define	M_PKTHDR	0x00000002 /* start of record */
21533d8df4bSKevin Lo#define	M_EOR		0x00000004 /* end of record */
21633d8df4bSKevin Lo#define	M_RDONLY	0x00000008 /* associated data marked read-only */
21733d8df4bSKevin Lo#define	M_PROTO1	0x00001000 /* protocol-specific */
21833d8df4bSKevin Lo#define	M_PROTO2	0x00002000 /* protocol-specific */
21933d8df4bSKevin Lo#define	M_PROTO3	0x00004000 /* protocol-specific */
22033d8df4bSKevin Lo#define	M_PROTO4	0x00008000 /* protocol-specific */
22133d8df4bSKevin Lo#define	M_PROTO5	0x00010000 /* protocol-specific */
22233d8df4bSKevin Lo#define	M_PROTO6	0x00020000 /* protocol-specific */
22333d8df4bSKevin Lo#define	M_PROTO7	0x00040000 /* protocol-specific */
22433d8df4bSKevin Lo#define	M_PROTO8	0x00080000 /* protocol-specific */
22533d8df4bSKevin Lo#define	M_PROTO9	0x00100000 /* protocol-specific */
22633d8df4bSKevin Lo#define	M_PROTO10	0x00200000 /* protocol-specific */
22733d8df4bSKevin Lo#define	M_PROTO11	0x00400000 /* protocol-specific */
22833d8df4bSKevin Lo#define	M_PROTO12	0x00800000 /* protocol-specific */
229de06f907SNik Clayton
230f434362aSYaroslav Tykhiy/* mbuf pkthdr flags (also stored in m_flags) */
23133d8df4bSKevin Lo#define	M_BCAST		0x00000010 /* send/received as link-level broadcast */
23233d8df4bSKevin Lo#define	M_MCAST		0x00000020 /* send/received as link-level multicast */
233de06f907SNik Clayton.Ed
234de06f907SNik Clayton.Pp
2357b2fd183SYaroslav TykhiyThe available
2367b2fd183SYaroslav Tykhiy.Vt mbuf
2377b2fd183SYaroslav Tykhiytypes are defined as follows:
238de06f907SNik Clayton.Bd -literal
239de06f907SNik Clayton/* mbuf types */
240de06f907SNik Clayton#define	MT_DATA		1	/* dynamic (data) allocation */
2411cb58a07SMaxim Konovalov#define	MT_HEADER	MT_DATA	/* packet header */
242de06f907SNik Clayton#define	MT_SONAME	8	/* socket name */
243de06f907SNik Clayton#define	MT_CONTROL	14	/* extra-data protocol message */
244de06f907SNik Clayton#define	MT_OOBDATA	15	/* expedited data */
245de06f907SNik Clayton.Ed
246de06f907SNik Clayton.Pp
247c3a2dbe9SDag-Erling SmørgravThe available external buffer types are defined as follows:
248c3a2dbe9SDag-Erling Smørgrav.Bd -literal
249c3a2dbe9SDag-Erling Smørgrav/* external buffer types */
250c3a2dbe9SDag-Erling Smørgrav#define EXT_CLUSTER	1	/* mbuf cluster */
251c3a2dbe9SDag-Erling Smørgrav#define EXT_SFBUF	2	/* sendfile(2)'s sf_bufs */
252c3a2dbe9SDag-Erling Smørgrav#define EXT_JUMBOP	3	/* jumbo cluster 4096 bytes */
253c3a2dbe9SDag-Erling Smørgrav#define EXT_JUMBO9	4	/* jumbo cluster 9216 bytes */
254c3a2dbe9SDag-Erling Smørgrav#define EXT_JUMBO16	5	/* jumbo cluster 16184 bytes */
255c3a2dbe9SDag-Erling Smørgrav#define EXT_PACKET	6	/* mbuf+cluster from packet zone */
256c3a2dbe9SDag-Erling Smørgrav#define EXT_MBUF	7	/* external mbuf reference (M_IOVEC) */
25733d8df4bSKevin Lo#define EXT_NET_DRV	252	/* custom ext_buf provided by net driver(s) */
25833d8df4bSKevin Lo#define EXT_MOD_TYPE	253	/* custom module's ext_buf type */
25933d8df4bSKevin Lo#define EXT_DISPOSABLE	254	/* can throw this buffer away w/page flipping */
26033d8df4bSKevin Lo#define EXT_EXTREF	255	/* has externally maintained ref_cnt ptr */
261c3a2dbe9SDag-Erling Smørgrav.Ed
262c3a2dbe9SDag-Erling Smørgrav.Pp
263de06f907SNik ClaytonIf the
264de06f907SNik Clayton.Dv M_PKTHDR
265de06f907SNik Claytonflag is set, a
2667b2fd183SYaroslav Tykhiy.Vt struct pkthdr Va m_pkthdr
2677b2fd183SYaroslav Tykhiyis added to the
2687b2fd183SYaroslav Tykhiy.Vt mbuf
2697b2fd183SYaroslav Tykhiyheader.
270e73145f6SSheldon HearnIt contains a pointer to the interface
271de06f907SNik Claytonthe packet has been received from
2727b2fd183SYaroslav Tykhiy.Pq Vt struct ifnet Va *rcvif ,
273de06f907SNik Claytonand the total packet length
2747b2fd183SYaroslav Tykhiy.Pq Vt int Va len .
275e85cc3f5SRuslan ErmilovOptionally, it may also contain an attached list of packet tags
276e85cc3f5SRuslan Ermilov.Pq Vt "struct m_tag" .
277e85cc3f5SRuslan ErmilovSee
278e85cc3f5SRuslan Ermilov.Xr mbuf_tags 9
279e85cc3f5SRuslan Ermilovfor details.
2800193eb5eSYaroslav TykhiyFields used in offloading checksum calculation to the hardware are kept in
2810193eb5eSYaroslav Tykhiy.Va m_pkthdr
2820193eb5eSYaroslav Tykhiyas well.
2830193eb5eSYaroslav TykhiySee
2840193eb5eSYaroslav Tykhiy.Sx HARDWARE-ASSISTED CHECKSUM CALCULATION
2850193eb5eSYaroslav Tykhiyfor details.
286de06f907SNik Clayton.Pp
2877b2fd183SYaroslav TykhiyIf small enough, data is stored in the internal data buffer of an
2887b2fd183SYaroslav Tykhiy.Vt mbuf .
2897b2fd183SYaroslav TykhiyIf the data is sufficiently large, another
2907b2fd183SYaroslav Tykhiy.Vt mbuf
2917b2fd183SYaroslav Tykhiymay be added to the
2927b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
2937b2fd183SYaroslav Tykhiyor external storage may be associated with the
2947b2fd183SYaroslav Tykhiy.Vt mbuf .
295de06f907SNik Clayton.Dv MHLEN
2967b2fd183SYaroslav Tykhiybytes of data can fit into an
2977b2fd183SYaroslav Tykhiy.Vt mbuf
2987b2fd183SYaroslav Tykhiywith the
299de06f907SNik Clayton.Dv M_PKTHDR
300de06f907SNik Claytonflag set,
301de06f907SNik Clayton.Dv MLEN
302de06f907SNik Claytonbytes can otherwise.
303de06f907SNik Clayton.Pp
3047b2fd183SYaroslav TykhiyIf external storage is being associated with an
3057b2fd183SYaroslav Tykhiy.Vt mbuf ,
3067b2fd183SYaroslav Tykhiythe
307be82a0bcSYaroslav Tykhiy.Va m_ext
30825f1a2e9SSheldon Hearnheader is added at the cost of losing the internal data buffer.
309e73145f6SSheldon HearnIt includes a pointer to external storage, the size of the storage,
310e73145f6SSheldon Hearna pointer to a function used for freeing the storage,
311e73145f6SSheldon Hearna pointer to an optional argument that can be passed to the function,
312e73145f6SSheldon Hearnand a pointer to a reference counter.
3137b2fd183SYaroslav TykhiyAn
3147b2fd183SYaroslav Tykhiy.Vt mbuf
3157b2fd183SYaroslav Tykhiyusing external storage has the
316de06f907SNik Clayton.Dv M_EXT
317de06f907SNik Claytonflag set.
318de06f907SNik Clayton.Pp
319bfd59721SBosko MilekicThe system supplies a macro for allocating the desired external storage
320bfd59721SBosko Milekicbuffer,
321bfd59721SBosko Milekic.Dv MEXTADD .
322bfd59721SBosko Milekic.Pp
323bfd59721SBosko MilekicThe allocation and management of the reference counter is handled by the
324e73145f6SSheldon Hearnsubsystem.
325bfd59721SBosko Milekic.Pp
326bfd59721SBosko MilekicThe system also supplies a default type of external storage buffer called an
3277b2fd183SYaroslav Tykhiy.Vt mbuf cluster .
3287b2fd183SYaroslav Tykhiy.Vt Mbuf clusters
3297b2fd183SYaroslav Tykhiycan be allocated and configured with the use of the
330bfd59721SBosko Milekic.Dv MCLGET
331e73145f6SSheldon Hearnmacro.
3327b2fd183SYaroslav TykhiyEach
3337b2fd183SYaroslav Tykhiy.Vt mbuf cluster
3347b2fd183SYaroslav Tykhiyis
335de06f907SNik Clayton.Dv MCLBYTES
336bfd59721SBosko Milekicin size, where MCLBYTES is a machine-dependent constant.
337bfd59721SBosko MilekicThe system defines an advisory macro
338de06f907SNik Clayton.Dv MINCLSIZE ,
3397b2fd183SYaroslav Tykhiywhich is the smallest amount of data to put into an
3407b2fd183SYaroslav Tykhiy.Vt mbuf cluster .
3412e3ee239SKevin LoIt is equal to
3422e3ee239SKevin Lo.Dv MHLEN
3432e3ee239SKevin Loplus one.
3447b2fd183SYaroslav TykhiyIt is typically preferable to store data into the data region of an
3457b2fd183SYaroslav Tykhiy.Vt mbuf ,
3467b2fd183SYaroslav Tykhiyif size permits, as opposed to allocating a separate
3477b2fd183SYaroslav Tykhiy.Vt mbuf cluster
3487b2fd183SYaroslav Tykhiyto hold the same data.
349de06f907SNik Clayton.\"
350de06f907SNik Clayton.Ss Macros and Functions
351bfd59721SBosko MilekicThere are numerous predefined macros and functions that provide the
352bfd59721SBosko Milekicdeveloper with common utilities.
353de06f907SNik Clayton.\"
354de06f907SNik Clayton.Bl -ohang -offset indent
355de06f907SNik Clayton.It Fn mtod mbuf type
3567b2fd183SYaroslav TykhiyConvert an
3577b2fd183SYaroslav Tykhiy.Fa mbuf
3587b2fd183SYaroslav Tykhiypointer to a data pointer.
359d31a9760SJohn-Mark GurneyThe macro expands to the data pointer cast to the specified
3607b2fd183SYaroslav Tykhiy.Fa type .
361de06f907SNik Clayton.Sy Note :
3627b2fd183SYaroslav TykhiyIt is advisable to ensure that there is enough contiguous data in
3637b2fd183SYaroslav Tykhiy.Fa mbuf .
364de06f907SNik ClaytonSee
365de06f907SNik Clayton.Fn m_pullup
366de06f907SNik Claytonfor details.
367de06f907SNik Clayton.It Fn MGET mbuf how type
3687b2fd183SYaroslav TykhiyAllocate an
3697b2fd183SYaroslav Tykhiy.Vt mbuf
3707b2fd183SYaroslav Tykhiyand initialize it to contain internal data.
37125f1a2e9SSheldon Hearn.Fa mbuf
3727b2fd183SYaroslav Tykhiywill point to the allocated
3737b2fd183SYaroslav Tykhiy.Vt mbuf
3747b2fd183SYaroslav Tykhiyon success, or be set to
375de06f907SNik Clayton.Dv NULL
376e73145f6SSheldon Hearnon failure.
377e73145f6SSheldon HearnThe
378de06f907SNik Clayton.Fa how
379de06f907SNik Claytonargument is to be set to
380fa05d215SGleb Smirnoff.Dv M_WAITOK
381de06f907SNik Claytonor
382fa05d215SGleb Smirnoff.Dv M_NOWAIT .
38325f1a2e9SSheldon HearnIt specifies whether the caller is willing to block if necessary.
3847b2fd183SYaroslav TykhiyA number of other functions and macros related to
3857b2fd183SYaroslav Tykhiy.Vt mbufs
3867b2fd183SYaroslav Tykhiyhave the same argument because they may
3877b2fd183SYaroslav Tykhiyat some point need to allocate new
3887b2fd183SYaroslav Tykhiy.Vt mbufs .
389de06f907SNik Clayton.It Fn MGETHDR mbuf how type
3907b2fd183SYaroslav TykhiyAllocate an
3917b2fd183SYaroslav Tykhiy.Vt mbuf
3927b2fd183SYaroslav Tykhiyand initialize it to contain a packet header
393e73145f6SSheldon Hearnand internal data.
394e73145f6SSheldon HearnSee
395de06f907SNik Clayton.Fn MGET
396de06f907SNik Claytonfor details.
397c3a2dbe9SDag-Erling Smørgrav.It Fn MEXTADD mbuf buf size free opt_arg1 opt_arg2 flags type
398c3a2dbe9SDag-Erling SmørgravAssociate externally managed data with
399c3a2dbe9SDag-Erling Smørgrav.Fa mbuf .
400c3a2dbe9SDag-Erling SmørgravAny internal data contained in the mbuf will be discarded, and the
40173bbeaa5SGlen Barber.Dv M_EXT
40273bbeaa5SGlen Barberflag will be set.
403c3a2dbe9SDag-Erling SmørgravThe
404c3a2dbe9SDag-Erling Smørgrav.Fa buf
405c3a2dbe9SDag-Erling Smørgravand
406c3a2dbe9SDag-Erling Smørgrav.Fa size
407c3a2dbe9SDag-Erling Smørgravarguments are the address and length, respectively, of the data.
408c3a2dbe9SDag-Erling SmørgravThe
409c3a2dbe9SDag-Erling Smørgrav.Fa free
410c3a2dbe9SDag-Erling Smørgravargument points to a function which will be called to free the data
411c3a2dbe9SDag-Erling Smørgravwhen the mbuf is freed; it is only used if
412c3a2dbe9SDag-Erling Smørgrav.Fa type
413c3a2dbe9SDag-Erling Smørgravis
414c3a2dbe9SDag-Erling Smørgrav.Dv EXT_EXTREF .
415c3a2dbe9SDag-Erling SmørgravThe
416c3a2dbe9SDag-Erling Smørgrav.Fa opt_arg1
417c3a2dbe9SDag-Erling Smørgravand
418c3a2dbe9SDag-Erling Smørgrav.Fa opt_arg2
419c3a2dbe9SDag-Erling Smørgravarguments will be passed unmodified to
420c3a2dbe9SDag-Erling Smørgrav.Fa free .
421c3a2dbe9SDag-Erling SmørgravThe
422c3a2dbe9SDag-Erling Smørgrav.Fa flags
423c3a2dbe9SDag-Erling Smørgravargument specifies additional
424c3a2dbe9SDag-Erling Smørgrav.Vt mbuf
425c3a2dbe9SDag-Erling Smørgravflags; it is not necessary to specify
426c3a2dbe9SDag-Erling Smørgrav.Dv M_EXT .
427c3a2dbe9SDag-Erling SmørgravFinally, the
428c3a2dbe9SDag-Erling Smørgrav.Fa type
429c3a2dbe9SDag-Erling Smørgravargument specifies the type of external data, which controls how it
430c3a2dbe9SDag-Erling Smørgravwill be disposed of when the
431c3a2dbe9SDag-Erling Smørgrav.Vt mbuf
432c3a2dbe9SDag-Erling Smørgravis freed.
433c3a2dbe9SDag-Erling SmørgravIn most cases, the correct value is
434c3a2dbe9SDag-Erling Smørgrav.Dv EXT_EXTREF .
435de06f907SNik Clayton.It Fn MCLGET mbuf how
4367b2fd183SYaroslav TykhiyAllocate and attach an
4377b2fd183SYaroslav Tykhiy.Vt mbuf cluster
4387b2fd183SYaroslav Tykhiyto
4397b2fd183SYaroslav Tykhiy.Fa mbuf .
440*2a8c860fSRobert WatsonOn success, a non-zero value returned; otherwise, 0.
441*2a8c860fSRobert WatsonHistorically, consumers would check for success by testing the
442de06f907SNik Clayton.Dv M_EXT
443*2a8c860fSRobert Watsonflag on the mbuf, but this is now discouraged to avoid unnecessary awareness
444*2a8c860fSRobert Watsonof the implementation of external storage in protocol stacks and device
445*2a8c860fSRobert Watsondrivers.
446da72b998SYaroslav Tykhiy.It Fn M_ALIGN mbuf len
447da72b998SYaroslav TykhiySet the pointer
448da72b998SYaroslav Tykhiy.Fa mbuf->m_data
449da72b998SYaroslav Tykhiyto place an object of the size
450da72b998SYaroslav Tykhiy.Fa len
451da72b998SYaroslav Tykhiyat the end of the internal data area of
452da72b998SYaroslav Tykhiy.Fa mbuf ,
453da72b998SYaroslav Tykhiylong word aligned.
454da72b998SYaroslav TykhiyApplicable only if
455da72b998SYaroslav Tykhiy.Fa mbuf
456da72b998SYaroslav Tykhiyis newly allocated with
457da72b998SYaroslav Tykhiy.Fn MGET
458da72b998SYaroslav Tykhiyor
459da72b998SYaroslav Tykhiy.Fn m_get .
460da72b998SYaroslav Tykhiy.It Fn MH_ALIGN mbuf len
461da72b998SYaroslav TykhiyServes the same purpose as
462da72b998SYaroslav Tykhiy.Fn M_ALIGN
463da72b998SYaroslav Tykhiydoes, but only for
464da72b998SYaroslav Tykhiy.Fa mbuf
465da72b998SYaroslav Tykhiynewly allocated with
466da72b998SYaroslav Tykhiy.Fn MGETHDR
467da72b998SYaroslav Tykhiyor
468da72b998SYaroslav Tykhiy.Fn m_gethdr ,
469da72b998SYaroslav Tykhiyor initialized by
470da72b998SYaroslav Tykhiy.Fn m_dup_pkthdr
471da72b998SYaroslav Tykhiyor
472da72b998SYaroslav Tykhiy.Fn m_move_pkthdr .
4739c3fd404SSam Leffler.It Fn m_align mbuf len
4749c3fd404SSam LefflerServices the same purpose as
4759c3fd404SSam Leffler.Fn M_ALIGN
4769c3fd404SSam Lefflerbut handles any type of mbuf.
477da72b998SYaroslav Tykhiy.It Fn M_LEADINGSPACE mbuf
478da72b998SYaroslav TykhiyReturns the number of bytes available before the beginning
479da72b998SYaroslav Tykhiyof data in
480da72b998SYaroslav Tykhiy.Fa mbuf .
481da72b998SYaroslav Tykhiy.It Fn M_TRAILINGSPACE mbuf
482da72b998SYaroslav TykhiyReturns the number of bytes available after the end of data in
483da72b998SYaroslav Tykhiy.Fa mbuf .
484de06f907SNik Clayton.It Fn M_PREPEND mbuf len how
4857b2fd183SYaroslav TykhiyThis macro operates on an
4867b2fd183SYaroslav Tykhiy.Vt mbuf chain .
487de06f907SNik ClaytonIt is an optimized wrapper for
488de06f907SNik Clayton.Fn m_prepend
489de06f907SNik Claytonthat can make use of possible empty space before data
4905203edcdSRuslan Ermilov(e.g.\& left after trimming of a link-layer header).
4917b2fd183SYaroslav TykhiyThe new
4927b2fd183SYaroslav Tykhiy.Vt mbuf chain
4937b2fd183SYaroslav Tykhiypointer or
494de06f907SNik Clayton.Dv NULL
495de06f907SNik Claytonis in
496de06f907SNik Clayton.Fa mbuf
497de06f907SNik Claytonafter the call.
498da72b998SYaroslav Tykhiy.It Fn M_MOVE_PKTHDR to from
499da72b998SYaroslav TykhiyUsing this macro is equivalent to calling
500da72b998SYaroslav Tykhiy.Fn m_move_pkthdr to from .
5015d4050ccSBosko Milekic.It Fn M_WRITABLE mbuf
5027b2fd183SYaroslav TykhiyThis macro will evaluate true if
5037b2fd183SYaroslav Tykhiy.Fa mbuf
5047b2fd183SYaroslav Tykhiyis not marked
5055d4050ccSBosko Milekic.Dv M_RDONLY
5067b2fd183SYaroslav Tykhiyand if either
5077b2fd183SYaroslav Tykhiy.Fa mbuf
5087b2fd183SYaroslav Tykhiydoes not contain external storage or,
5095d4050ccSBosko Milekicif it does,
5105d4050ccSBosko Milekicthen if the reference count of the storage is not greater than 1.
5115d4050ccSBosko MilekicThe
5125d4050ccSBosko Milekic.Dv M_RDONLY
5137b2fd183SYaroslav Tykhiyflag can be set in
5147b2fd183SYaroslav Tykhiy.Fa mbuf->m_flags .
5155d4050ccSBosko MilekicThis can be achieved during setup of the external storage,
5165d4050ccSBosko Milekicby passing the
5175d4050ccSBosko Milekic.Dv M_RDONLY
5185d4050ccSBosko Milekicbit as a
5190b39b3ffSRuslan Ermilov.Fa flags
5205d4050ccSBosko Milekicargument to the
5215d4050ccSBosko Milekic.Fn MEXTADD
5227b2fd183SYaroslav Tykhiymacro, or can be directly set in individual
5237b2fd183SYaroslav Tykhiy.Vt mbufs .
524da72b998SYaroslav Tykhiy.It Fn MCHTYPE mbuf type
525da72b998SYaroslav TykhiyChange the type of
526da72b998SYaroslav Tykhiy.Fa mbuf
527da72b998SYaroslav Tykhiyto
528da72b998SYaroslav Tykhiy.Fa type .
529da72b998SYaroslav TykhiyThis is a relatively expensive operation and should be avoided.
530de06f907SNik Clayton.El
531de06f907SNik Clayton.Pp
532de06f907SNik ClaytonThe functions are:
533de06f907SNik Clayton.Bl -ohang -offset indent
534de06f907SNik Clayton.It Fn m_get how type
535de06f907SNik ClaytonA function version of
536bfd59721SBosko Milekic.Fn MGET
537bfd59721SBosko Milekicfor non-critical paths.
53818f169afSKevin Lo.It Fn m_get2 size how type flags
53918f169afSKevin LoAllocate an
54018f169afSKevin Lo.Vt mbuf
54118f169afSKevin Lowith enough space to hold specified amount of data.
542328c0bbcSBosko Milekic.It Fn m_getm orig len how type
543328c0bbcSBosko MilekicAllocate
5440b39b3ffSRuslan Ermilov.Fa len
5457b2fd183SYaroslav Tykhiybytes worth of
5467b2fd183SYaroslav Tykhiy.Vt mbufs
5477b2fd183SYaroslav Tykhiyand
5487b2fd183SYaroslav Tykhiy.Vt mbuf clusters
5497b2fd183SYaroslav Tykhiyif necessary and append the resulting allocated
5507b2fd183SYaroslav Tykhiy.Vt mbuf chain
5517b2fd183SYaroslav Tykhiyto the
5527b2fd183SYaroslav Tykhiy.Vt mbuf chain
5537b2fd183SYaroslav Tykhiy.Fa orig ,
5547b2fd183SYaroslav Tykhiyif it is
5550b39b3ffSRuslan Ermilov.No non- Ns Dv NULL .
556328c0bbcSBosko MilekicIf the allocation fails at any point,
5570b39b3ffSRuslan Ermilovfree whatever was allocated and return
5580b39b3ffSRuslan Ermilov.Dv NULL .
559328c0bbcSBosko MilekicIf
5600b39b3ffSRuslan Ermilov.Fa orig
5610b39b3ffSRuslan Ermilovis
5620b39b3ffSRuslan Ermilov.No non- Ns Dv NULL ,
563328c0bbcSBosko Milekicit will not be freed.
564328c0bbcSBosko MilekicIt is possible to use
565328c0bbcSBosko Milekic.Fn m_getm
566328c0bbcSBosko Milekicto either append
5670b39b3ffSRuslan Ermilov.Fa len
5687b2fd183SYaroslav Tykhiybytes to an existing
5697b2fd183SYaroslav Tykhiy.Vt mbuf
5707b2fd183SYaroslav Tykhiyor
5717b2fd183SYaroslav Tykhiy.Vt mbuf chain
572328c0bbcSBosko Milekic(for example, one which may be sitting in a pre-allocated ring)
5737b2fd183SYaroslav Tykhiyor to simply perform an all-or-nothing
5747b2fd183SYaroslav Tykhiy.Vt mbuf
5757b2fd183SYaroslav Tykhiyand
5767b2fd183SYaroslav Tykhiy.Vt mbuf cluster
5777b2fd183SYaroslav Tykhiyallocation.
578de06f907SNik Clayton.It Fn m_gethdr how type
579de06f907SNik ClaytonA function version of
580bfd59721SBosko Milekic.Fn MGETHDR
581bfd59721SBosko Milekicfor non-critical paths.
5826c013706SMaxim Konovalov.It Fn m_getcl how type flags
5836c013706SMaxim KonovalovFetch an
5846c013706SMaxim Konovalov.Vt mbuf
5856c013706SMaxim Konovalovwith a
5866c013706SMaxim Konovalov.Vt mbuf cluster
5876c013706SMaxim Konovalovattached to it.
5886c013706SMaxim KonovalovIf one of the allocations fails, the entire allocation fails.
5896c013706SMaxim KonovalovThis routine is the preferred way of fetching both the
5906c013706SMaxim Konovalov.Vt mbuf
5916c013706SMaxim Konovalovand
5926c013706SMaxim Konovalov.Vt mbuf cluster
5936c013706SMaxim Konovalovtogether, as it avoids having to unlock/relock between allocations.
5946c013706SMaxim KonovalovReturns
5956c013706SMaxim Konovalov.Dv NULL
5966c013706SMaxim Konovalovon failure.
5975c35eb22SJohn-Mark Gurney.It Fn m_getjcl how type flags size
5985c35eb22SJohn-Mark GurneyThis is like
5995c35eb22SJohn-Mark Gurney.Fn m_getcl
6005c35eb22SJohn-Mark Gurneybut it the size of the cluster allocated will be large enough for
6015c35eb22SJohn-Mark Gurney.Fa size
6025c35eb22SJohn-Mark Gurneybytes.
603de06f907SNik Clayton.It Fn m_getclr how type
6047b2fd183SYaroslav TykhiyAllocate an
6057b2fd183SYaroslav Tykhiy.Vt mbuf
6067b2fd183SYaroslav Tykhiyand zero out the data region.
60701e7fdafSMaxim Konovalov.It Fn m_free mbuf
60801e7fdafSMaxim KonovalovFrees
60901e7fdafSMaxim Konovalov.Vt mbuf .
610b7131a26SJohn-Mark GurneyReturns
611b7131a26SJohn-Mark Gurney.Va m_next
612b7131a26SJohn-Mark Gurneyof the freed
613b7131a26SJohn-Mark Gurney.Vt mbuf .
614de06f907SNik Clayton.El
615de06f907SNik Clayton.Pp
6167b2fd183SYaroslav TykhiyThe functions below operate on
6177b2fd183SYaroslav Tykhiy.Vt mbuf chains .
618de06f907SNik Clayton.Bl -ohang -offset indent
619de06f907SNik Clayton.It Fn m_freem mbuf
6207b2fd183SYaroslav TykhiyFree an entire
6217b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
6227b2fd183SYaroslav Tykhiyincluding any external storage.
623de06f907SNik Clayton.\"
624de06f907SNik Clayton.It Fn m_adj mbuf len
625de06f907SNik ClaytonTrim
626de06f907SNik Clayton.Fa len
6277b2fd183SYaroslav Tykhiybytes from the head of an
6287b2fd183SYaroslav Tykhiy.Vt mbuf chain
6297b2fd183SYaroslav Tykhiyif
630de06f907SNik Clayton.Fa len
631de06f907SNik Claytonis positive, from the tail otherwise.
632de06f907SNik Clayton.\"
633b9def06bSSam Leffler.It Fn m_append mbuf len cp
634b9def06bSSam LefflerAppend
635b9def06bSSam Leffler.Vt len
636b9def06bSSam Lefflerbytes of data
637b9def06bSSam Leffler.Vt cp
638b9def06bSSam Lefflerto the
639b9def06bSSam Leffler.Vt mbuf chain .
640b9def06bSSam LefflerExtend the mbuf chain if the new data does not fit in
641b9def06bSSam Lefflerexisting space.
642b9def06bSSam Leffler.\"
643de06f907SNik Clayton.It Fn m_prepend mbuf len how
6447b2fd183SYaroslav TykhiyAllocate a new
6457b2fd183SYaroslav Tykhiy.Vt mbuf
6467b2fd183SYaroslav Tykhiyand prepend it to the
6477b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
6487b2fd183SYaroslav Tykhiyhandle
649de06f907SNik Clayton.Dv M_PKTHDR
650de06f907SNik Claytonproperly.
651de06f907SNik Clayton.Sy Note :
6520227791bSRuslan ErmilovIt does not allocate any
6537b2fd183SYaroslav Tykhiy.Vt mbuf clusters ,
6547b2fd183SYaroslav Tykhiyso
655de06f907SNik Clayton.Fa len
656de06f907SNik Claytonmust be less than
657de06f907SNik Clayton.Dv MLEN
658de06f907SNik Claytonor
659de06f907SNik Clayton.Dv MHLEN ,
660de06f907SNik Claytondepending on the
661d0353b83SRuslan Ermilov.Dv M_PKTHDR
662d0353b83SRuslan Ermilovflag setting.
663de06f907SNik Clayton.\"
6647ac139a9SJohn-Mark Gurney.It Fn m_copyup mbuf len dstoff
6657ac139a9SJohn-Mark GurneySimilar to
6667ac139a9SJohn-Mark Gurney.Fn m_pullup
6677ac139a9SJohn-Mark Gurneybut copies
6687ac139a9SJohn-Mark Gurney.Fa len
6697ac139a9SJohn-Mark Gurneybytes of data into a new mbuf at
6707ac139a9SJohn-Mark Gurney.Fa dstoff
6717ac139a9SJohn-Mark Gurneybytes into the mbuf.
6727ac139a9SJohn-Mark GurneyThe
6737ac139a9SJohn-Mark Gurney.Fa dstoff
6747ac139a9SJohn-Mark Gurneyargument aligns the data and leaves room for a link layer header.
6754f068961SRuslan ErmilovReturns the new
6767ac139a9SJohn-Mark Gurney.Vt mbuf chain
6777ac139a9SJohn-Mark Gurneyon success,
6787ac139a9SJohn-Mark Gurneyand frees the
6797ac139a9SJohn-Mark Gurney.Vt mbuf chain
6807ac139a9SJohn-Mark Gurneyand returns
6817ac139a9SJohn-Mark Gurney.Dv NULL
6827ac139a9SJohn-Mark Gurneyon failure.
6837ac139a9SJohn-Mark Gurney.Sy Note :
6847ac139a9SJohn-Mark GurneyThe function does not allocate
6857ac139a9SJohn-Mark Gurney.Vt mbuf clusters ,
6867ac139a9SJohn-Mark Gurneyso
6877ac139a9SJohn-Mark Gurney.Fa len + dstoff
6887ac139a9SJohn-Mark Gurneymust be less than
6897ac139a9SJohn-Mark Gurney.Dv MHLEN .
6907ac139a9SJohn-Mark Gurney.\"
691de06f907SNik Clayton.It Fn m_pullup mbuf len
692de06f907SNik ClaytonArrange that the first
693de06f907SNik Clayton.Fa len
6947b2fd183SYaroslav Tykhiybytes of an
6957b2fd183SYaroslav Tykhiy.Vt mbuf chain
6967b2fd183SYaroslav Tykhiyare contiguous and lay in the data area of
697de06f907SNik Clayton.Fa mbuf ,
698de06f907SNik Claytonso they are accessible with
699de06f907SNik Clayton.Fn mtod mbuf type .
7001009bd1bSJulian ElischerIt is important to remember that this may involve
7011009bd1bSJulian Elischerreallocating some mbufs and moving data so all pointers
7021009bd1bSJulian Elischerreferencing data within the old mbuf chain
7031009bd1bSJulian Elischermust be recalculated or made invalid.
7047b2fd183SYaroslav TykhiyReturn the new
7057b2fd183SYaroslav Tykhiy.Vt mbuf chain
7067b2fd183SYaroslav Tykhiyon success,
707de06f907SNik Clayton.Dv NULL
708de06f907SNik Claytonon failure
7097b2fd183SYaroslav Tykhiy(the
7107b2fd183SYaroslav Tykhiy.Vt mbuf chain
7117b2fd183SYaroslav Tykhiyis freed in this case).
712de06f907SNik Clayton.Sy Note :
7130227791bSRuslan ErmilovIt does not allocate any
7147b2fd183SYaroslav Tykhiy.Vt mbuf clusters ,
7157b2fd183SYaroslav Tykhiyso
716de06f907SNik Clayton.Fa len
717969ed8f7SKevin Lomust be less than or equal to
718de06f907SNik Clayton.Dv MHLEN .
719de06f907SNik Clayton.\"
720d305f4b9SBruce M Simpson.It Fn m_pulldown mbuf offset len offsetp
721d305f4b9SBruce M SimpsonArrange that
722d305f4b9SBruce M Simpson.Fa len
723d305f4b9SBruce M Simpsonbytes between
724d305f4b9SBruce M Simpson.Fa offset
725d305f4b9SBruce M Simpsonand
726d305f4b9SBruce M Simpson.Fa offset + len
727d305f4b9SBruce M Simpsonin the
728d305f4b9SBruce M Simpson.Vt mbuf chain
729d305f4b9SBruce M Simpsonare contiguous and lay in the data area of
730d305f4b9SBruce M Simpson.Fa mbuf ,
731d305f4b9SBruce M Simpsonso they are accessible with
732d305f4b9SBruce M Simpson.Fn mtod mbuf type .
73373bbeaa5SGlen Barber.Fa len
73473bbeaa5SGlen Barbermust be smaller than, or equal to, the size of an
735d305f4b9SBruce M Simpson.Vt mbuf cluster .
736d305f4b9SBruce M SimpsonReturn a pointer to an intermediate
737d305f4b9SBruce M Simpson.Vt mbuf
738d305f4b9SBruce M Simpsonin the chain containing the requested region;
739d305f4b9SBruce M Simpsonthe offset in the data region of the
740d305f4b9SBruce M Simpson.Vt mbuf chain
741d305f4b9SBruce M Simpsonto the data contained in the returned mbuf is stored in
742d305f4b9SBruce M Simpson.Fa *offsetp .
743d305f4b9SBruce M SimpsonIf
744e0912e08SJohn-Mark Gurney.Fa offsetp
745d305f4b9SBruce M Simpsonis NULL, the region may be accessed using
746d305f4b9SBruce M Simpson.Fn mtod mbuf type .
747d305f4b9SBruce M SimpsonIf
748e0912e08SJohn-Mark Gurney.Fa offsetp
749d305f4b9SBruce M Simpsonis non-NULL, the region may be accessed using
750e0912e08SJohn-Mark Gurney.Fn mtod mbuf uint8_t
751e0912e08SJohn-Mark Gurney+ *offsetp.
752d305f4b9SBruce M SimpsonThe region of the mbuf chain between its beginning and
753e0912e08SJohn-Mark Gurney.Fa offset
754d305f4b9SBruce M Simpsonis not modified, therefore it is safe to hold pointers to data within
755d305f4b9SBruce M Simpsonthis region before calling
756d305f4b9SBruce M Simpson.Fn m_pulldown .
757d305f4b9SBruce M Simpson.\"
758de06f907SNik Clayton.It Fn m_copym mbuf offset len how
7597b2fd183SYaroslav TykhiyMake a copy of an
7607b2fd183SYaroslav Tykhiy.Vt mbuf chain
7617b2fd183SYaroslav Tykhiystarting
762de06f907SNik Clayton.Fa offset
763de06f907SNik Claytonbytes from the beginning, continuing for
764de06f907SNik Clayton.Fa len
765e73145f6SSheldon Hearnbytes.
766e73145f6SSheldon HearnIf
767de06f907SNik Clayton.Fa len
768de06f907SNik Claytonis
769de06f907SNik Clayton.Dv M_COPYALL ,
7707b2fd183SYaroslav Tykhiycopy to the end of the
7717b2fd183SYaroslav Tykhiy.Vt mbuf chain .
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_copypacket mbuf how
778de06f907SNik ClaytonCopy an entire packet including header, which must be present.
779de06f907SNik ClaytonThis is an optimized version of the common case
780de06f907SNik Clayton.Fn m_copym mbuf 0 M_COPYALL how .
781de06f907SNik Clayton.Sy Note :
7827b2fd183SYaroslav Tykhiythe copy is read-only, because the
7837b2fd183SYaroslav Tykhiy.Vt mbuf clusters
7847b2fd183SYaroslav Tykhiyare not copied, only their reference counts are incremented.
785de06f907SNik Clayton.\"
786de06f907SNik Clayton.It Fn m_dup mbuf how
7877b2fd183SYaroslav TykhiyCopy a packet header
7887b2fd183SYaroslav Tykhiy.Vt mbuf chain
7897b2fd183SYaroslav Tykhiyinto a completely new
7907b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
7917b2fd183SYaroslav Tykhiyincluding copying any
7927b2fd183SYaroslav Tykhiy.Vt mbuf clusters .
793e73145f6SSheldon HearnUse this instead of
794de06f907SNik Clayton.Fn m_copypacket
7957b2fd183SYaroslav Tykhiywhen you need a writable copy of an
7967b2fd183SYaroslav Tykhiy.Vt mbuf chain .
797de06f907SNik Clayton.\"
798de06f907SNik Clayton.It Fn m_copydata mbuf offset len buf
7997b2fd183SYaroslav TykhiyCopy data from an
8007b2fd183SYaroslav Tykhiy.Vt mbuf chain
8017b2fd183SYaroslav Tykhiystarting
802de06f907SNik Clayton.Fa off
803de06f907SNik Claytonbytes from the beginning, continuing for
804de06f907SNik Clayton.Fa len
805de06f907SNik Claytonbytes, into the indicated buffer
806de06f907SNik Clayton.Fa buf .
807de06f907SNik Clayton.\"
808de06f907SNik Clayton.It Fn m_copyback mbuf offset len buf
809de06f907SNik ClaytonCopy
810de06f907SNik Clayton.Fa len
811de06f907SNik Claytonbytes from the buffer
812de06f907SNik Clayton.Fa buf
8137b2fd183SYaroslav Tykhiyback into the indicated
8147b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
815de06f907SNik Claytonstarting at
816de06f907SNik Clayton.Fa offset
8177b2fd183SYaroslav Tykhiybytes from the beginning of the
8187b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
8197b2fd183SYaroslav Tykhiyextending the
8207b2fd183SYaroslav Tykhiy.Vt mbuf chain
8217b2fd183SYaroslav Tykhiyif necessary.
822de06f907SNik Clayton.Sy Note :
8230227791bSRuslan ErmilovIt does not allocate any
8247b2fd183SYaroslav Tykhiy.Vt mbuf clusters ,
8257b2fd183SYaroslav Tykhiyjust adds
8267b2fd183SYaroslav Tykhiy.Vt mbufs
8277b2fd183SYaroslav Tykhiyto the
8287b2fd183SYaroslav Tykhiy.Vt mbuf chain .
82936a142c4SRuslan ErmilovIt is safe to set
830de06f907SNik Clayton.Fa offset
8317b2fd183SYaroslav Tykhiybeyond the current
8327b2fd183SYaroslav Tykhiy.Vt mbuf chain
8337b2fd183SYaroslav Tykhiyend: zeroed
8347b2fd183SYaroslav Tykhiy.Vt mbufs
8357b2fd183SYaroslav Tykhiywill be allocated to fill the space.
836de06f907SNik Clayton.\"
8375d96084fSYaroslav Tykhiy.It Fn m_length mbuf last
8387b2fd183SYaroslav TykhiyReturn the length of the
8397b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
8407b2fd183SYaroslav Tykhiyand optionally a pointer to the last
8417b2fd183SYaroslav Tykhiy.Vt mbuf .
8420b1c0994SPoul-Henning Kamp.\"
843da72b998SYaroslav Tykhiy.It Fn m_dup_pkthdr to from how
844da72b998SYaroslav TykhiyUpon the function's completion, the
845da72b998SYaroslav Tykhiy.Vt mbuf
846da72b998SYaroslav Tykhiy.Fa to
847da72b998SYaroslav Tykhiywill contain an identical copy of
848da72b998SYaroslav Tykhiy.Fa from->m_pkthdr
849da72b998SYaroslav Tykhiyand the per-packet attributes found in the
850da72b998SYaroslav Tykhiy.Vt mbuf chain
851da72b998SYaroslav Tykhiy.Fa from .
852da72b998SYaroslav TykhiyThe
853da72b998SYaroslav Tykhiy.Vt mbuf
854da72b998SYaroslav Tykhiy.Fa from
855da72b998SYaroslav Tykhiymust have the flag
856da72b998SYaroslav Tykhiy.Dv M_PKTHDR
857da72b998SYaroslav Tykhiyinitially set, and
858da72b998SYaroslav Tykhiy.Fa to
859da72b998SYaroslav Tykhiymust be empty on entry.
860da72b998SYaroslav Tykhiy.\"
861da72b998SYaroslav Tykhiy.It Fn m_move_pkthdr to from
862da72b998SYaroslav TykhiyMove
863da72b998SYaroslav Tykhiy.Va m_pkthdr
864da72b998SYaroslav Tykhiyand the per-packet attributes from the
865da72b998SYaroslav Tykhiy.Vt mbuf chain
866da72b998SYaroslav Tykhiy.Fa from
867da72b998SYaroslav Tykhiyto the
868da72b998SYaroslav Tykhiy.Vt mbuf
869da72b998SYaroslav Tykhiy.Fa to .
870da72b998SYaroslav TykhiyThe
871da72b998SYaroslav Tykhiy.Vt mbuf
872da72b998SYaroslav Tykhiy.Fa from
873da72b998SYaroslav Tykhiymust have the flag
874da72b998SYaroslav Tykhiy.Dv M_PKTHDR
875da72b998SYaroslav Tykhiyinitially set, and
876da72b998SYaroslav Tykhiy.Fa to
877da72b998SYaroslav Tykhiymust be empty on entry.
878da72b998SYaroslav TykhiyUpon the function's completion,
879da72b998SYaroslav Tykhiy.Fa from
880da72b998SYaroslav Tykhiywill have the flag
881da72b998SYaroslav Tykhiy.Dv M_PKTHDR
882da72b998SYaroslav Tykhiyand the per-packet attributes cleared.
883da72b998SYaroslav Tykhiy.\"
8845d96084fSYaroslav Tykhiy.It Fn m_fixhdr mbuf
8857b2fd183SYaroslav TykhiySet the packet-header length to the length of the
8867b2fd183SYaroslav Tykhiy.Vt mbuf chain .
8870b1c0994SPoul-Henning Kamp.\"
888de06f907SNik Clayton.It Fn m_devget buf len offset ifp copy
889de06f907SNik ClaytonCopy data from a device local memory pointed to by
890de06f907SNik Clayton.Fa buf
8917b2fd183SYaroslav Tykhiyto an
8927b2fd183SYaroslav Tykhiy.Vt mbuf chain .
893e73145f6SSheldon HearnThe copy is done using a specified copy routine
894de06f907SNik Clayton.Fa copy ,
895de06f907SNik Claytonor
896de06f907SNik Clayton.Fn bcopy
897de06f907SNik Claytonif
898de06f907SNik Clayton.Fa copy
899de06f907SNik Claytonis
900de06f907SNik Clayton.Dv NULL .
901de06f907SNik Clayton.\"
902de06f907SNik Clayton.It Fn m_cat m n
903de06f907SNik ClaytonConcatenate
904de06f907SNik Clayton.Fa n
905de06f907SNik Claytonto
906de06f907SNik Clayton.Fa m .
9077b2fd183SYaroslav TykhiyBoth
9087b2fd183SYaroslav Tykhiy.Vt mbuf chains
9097b2fd183SYaroslav Tykhiymust be of the same type.
910de06f907SNik Clayton.Fa N
911de06f907SNik Claytonis still valid after the function returned.
912de06f907SNik Clayton.Sy Note :
913de06f907SNik ClaytonIt does not handle
914de06f907SNik Clayton.Dv M_PKTHDR
915de06f907SNik Claytonand friends.
916de06f907SNik Clayton.\"
917de06f907SNik Clayton.It Fn m_split mbuf len how
9187b2fd183SYaroslav TykhiyPartition an
9197b2fd183SYaroslav Tykhiy.Vt mbuf chain
9207b2fd183SYaroslav Tykhiyin two pieces, returning the tail:
921de06f907SNik Claytonall but the first
922de06f907SNik Clayton.Fa len
923e73145f6SSheldon Hearnbytes.
924e73145f6SSheldon HearnIn case of failure, it returns
925de06f907SNik Clayton.Dv NULL
9267b2fd183SYaroslav Tykhiyand attempts to restore the
9277b2fd183SYaroslav Tykhiy.Vt mbuf chain
9287b2fd183SYaroslav Tykhiyto its original state.
92902312219SBruce M Simpson.\"
93002312219SBruce M Simpson.It Fn m_apply mbuf off len f arg
93102312219SBruce M SimpsonApply a function to an
93202312219SBruce M Simpson.Vt mbuf chain ,
93302312219SBruce M Simpsonat offset
93402312219SBruce M Simpson.Fa off ,
93502312219SBruce M Simpsonfor length
936f48cbcdeSRuslan Ermilov.Fa len
937f48cbcdeSRuslan Ermilovbytes.
93802312219SBruce M SimpsonTypically used to avoid calls to
93902312219SBruce M Simpson.Fn m_pullup
94002312219SBruce M Simpsonwhich would otherwise be unnecessary or undesirable.
94102312219SBruce M Simpson.Fa arg
94202312219SBruce M Simpsonis a convenience argument which is passed to the callback function
94302312219SBruce M Simpson.Fa f .
94402312219SBruce M Simpson.Pp
94502312219SBruce M SimpsonEach time
94602312219SBruce M Simpson.Fn f
94702312219SBruce M Simpsonis called, it will be passed
94802312219SBruce M Simpson.Fa arg ,
94902312219SBruce M Simpsona pointer to the
95002312219SBruce M Simpson.Fa data
95102312219SBruce M Simpsonin the current mbuf, and the length
95202312219SBruce M Simpson.Fa len
95302312219SBruce M Simpsonof the data in this mbuf to which the function should be applied.
95402312219SBruce M Simpson.Pp
95502312219SBruce M SimpsonThe function should return zero to indicate success;
95602312219SBruce M Simpsonotherwise, if an error is indicated, then
95702312219SBruce M Simpson.Fn m_apply
95802312219SBruce M Simpsonwill return the error and stop iterating through the
95902312219SBruce M Simpson.Vt mbuf chain .
96002312219SBruce M Simpson.\"
96102312219SBruce M Simpson.It Fn m_getptr mbuf loc off
96202312219SBruce M SimpsonReturn a pointer to the mbuf containing the data located at
96302312219SBruce M Simpson.Fa loc
96402312219SBruce M Simpsonbytes from the beginning of the
96502312219SBruce M Simpson.Vt mbuf chain .
96602312219SBruce M SimpsonThe corresponding offset into the mbuf will be stored in
96702312219SBruce M Simpson.Fa *off .
968a61bc764SMike Silbersack.It Fn m_defrag m0 how
969f48cbcdeSRuslan ErmilovDefragment an mbuf chain, returning the shortest possible
970bdf86185SMike Silbersackchain of mbufs and clusters.
971bdf86185SMike SilbersackIf allocation fails and this can not be completed,
972bdf86185SMike Silbersack.Dv NULL
973bdf86185SMike Silbersackwill be returned and the original chain will be unchanged.
974bdf86185SMike SilbersackUpon success, the original chain will be freed and the new
975bdf86185SMike Silbersackchain will be returned.
976a61bc764SMike Silbersack.Fa how
977a61bc764SMike Silbersackshould be either
978fa05d215SGleb Smirnoff.Dv M_WAITOK
979a61bc764SMike Silbersackor
980fa05d215SGleb Smirnoff.Dv M_NOWAIT ,
981a61bc764SMike Silbersackdepending on the caller's preference.
982a61bc764SMike Silbersack.Pp
983a61bc764SMike SilbersackThis function is especially useful in network drivers, where
984a61bc764SMike Silbersackcertain long mbuf chains must be shortened before being added
985a61bc764SMike Silbersackto TX descriptor lists.
98647e2996eSSam Leffler.It Fn m_unshare m0 how
98747e2996eSSam LefflerCreate a version of the specified mbuf chain whose
98847e2996eSSam Lefflercontents can be safely modified without affecting other users.
98947e2996eSSam LefflerIf allocation fails and this operation can not be completed,
99047e2996eSSam Leffler.Dv NULL
99147e2996eSSam Lefflerwill be returned.
99247e2996eSSam LefflerThe original mbuf chain is always reclaimed and the reference
99347e2996eSSam Lefflercount of any shared mbuf clusters is decremented.
99447e2996eSSam Leffler.Fa how
99547e2996eSSam Lefflershould be either
996fa05d215SGleb Smirnoff.Dv M_WAITOK
99747e2996eSSam Leffleror
998fa05d215SGleb Smirnoff.Dv M_NOWAIT ,
99947e2996eSSam Lefflerdepending on the caller's preference.
100047e2996eSSam LefflerAs a side-effect of this process the returned
100147e2996eSSam Lefflermbuf chain may be compacted.
100247e2996eSSam Leffler.Pp
100347e2996eSSam LefflerThis function is especially useful in the transmit path of
100447e2996eSSam Lefflernetwork code, when data must be encrypted or otherwise
100547e2996eSSam Leffleraltered prior to transmission.
10063136363fSRuslan Ermilov.El
10070193eb5eSYaroslav Tykhiy.Sh HARDWARE-ASSISTED CHECKSUM CALCULATION
10080193eb5eSYaroslav TykhiyThis section currently applies to TCP/IP only.
10090193eb5eSYaroslav TykhiyIn order to save the host CPU resources, computing checksums is
10100193eb5eSYaroslav Tykhiyoffloaded to the network interface hardware if possible.
10110193eb5eSYaroslav TykhiyThe
10120193eb5eSYaroslav Tykhiy.Va m_pkthdr
10130193eb5eSYaroslav Tykhiymember of the leading
10140193eb5eSYaroslav Tykhiy.Vt mbuf
10150193eb5eSYaroslav Tykhiyof a packet contains two fields used for that purpose,
10160193eb5eSYaroslav Tykhiy.Vt int Va csum_flags
10170193eb5eSYaroslav Tykhiyand
10180193eb5eSYaroslav Tykhiy.Vt int Va csum_data .
10190193eb5eSYaroslav TykhiyThe meaning of those fields depends on the direction a packet flows in,
10200193eb5eSYaroslav Tykhiyand on whether the packet is fragmented.
10210193eb5eSYaroslav TykhiyHenceforth,
10220193eb5eSYaroslav Tykhiy.Va csum_flags
10230193eb5eSYaroslav Tykhiyor
10240193eb5eSYaroslav Tykhiy.Va csum_data
10250193eb5eSYaroslav Tykhiyof a packet
10260193eb5eSYaroslav Tykhiywill denote the corresponding field of the
10270193eb5eSYaroslav Tykhiy.Va m_pkthdr
10280193eb5eSYaroslav Tykhiymember of the leading
10290193eb5eSYaroslav Tykhiy.Vt mbuf
10300193eb5eSYaroslav Tykhiyin the
10310193eb5eSYaroslav Tykhiy.Vt mbuf chain
10320193eb5eSYaroslav Tykhiycontaining the packet.
10330193eb5eSYaroslav Tykhiy.Pp
10340193eb5eSYaroslav TykhiyOn output, checksum offloading is attempted after the outgoing
10350193eb5eSYaroslav Tykhiyinterface has been determined for a packet.
10360193eb5eSYaroslav TykhiyThe interface-specific field
10370193eb5eSYaroslav Tykhiy.Va ifnet.if_data.ifi_hwassist
10380193eb5eSYaroslav Tykhiy(see
10390193eb5eSYaroslav Tykhiy.Xr ifnet 9 )
10400193eb5eSYaroslav Tykhiyis consulted for the capabilities of the interface to assist in
10410193eb5eSYaroslav Tykhiycomputing checksums.
10420193eb5eSYaroslav TykhiyThe
10430193eb5eSYaroslav Tykhiy.Va csum_flags
1044f48cbcdeSRuslan Ermilovfield of the packet header is set to indicate which actions the interface
10450193eb5eSYaroslav Tykhiyis supposed to perform on it.
10460193eb5eSYaroslav TykhiyThe actions unsupported by the network interface are done in the
10470193eb5eSYaroslav Tykhiysoftware prior to passing the packet down to the interface driver;
10480193eb5eSYaroslav Tykhiysuch actions will never be requested through
10490193eb5eSYaroslav Tykhiy.Va csum_flags .
10500193eb5eSYaroslav Tykhiy.Pp
10510193eb5eSYaroslav TykhiyThe flags demanding a particular action from an interface are as follows:
10520193eb5eSYaroslav Tykhiy.Bl -tag -width ".Dv CSUM_TCP" -offset indent
10530193eb5eSYaroslav Tykhiy.It Dv CSUM_IP
10540193eb5eSYaroslav TykhiyThe IP header checksum is to be computed and stored in the
10550193eb5eSYaroslav Tykhiycorresponding field of the packet.
10560193eb5eSYaroslav TykhiyThe hardware is expected to know the format of an IP header
10570193eb5eSYaroslav Tykhiyto determine the offset of the IP checksum field.
10580193eb5eSYaroslav Tykhiy.It Dv CSUM_TCP
1059f48cbcdeSRuslan ErmilovThe TCP checksum is to be computed.
1060f48cbcdeSRuslan Ermilov(See below.)
10610193eb5eSYaroslav Tykhiy.It Dv CSUM_UDP
1062f48cbcdeSRuslan ErmilovThe UDP checksum is to be computed.
1063f48cbcdeSRuslan Ermilov(See below.)
10640193eb5eSYaroslav Tykhiy.El
10650193eb5eSYaroslav Tykhiy.Pp
1066fbde705aSYaroslav TykhiyShould a TCP or UDP checksum be offloaded to the hardware,
10670193eb5eSYaroslav Tykhiythe field
10680193eb5eSYaroslav Tykhiy.Va csum_data
10690193eb5eSYaroslav Tykhiywill contain the byte offset of the checksum field relative to the
10700193eb5eSYaroslav Tykhiyend of the IP header.
10710193eb5eSYaroslav TykhiyIn this case, the checksum field will be initially
10720193eb5eSYaroslav Tykhiyset by the TCP/IP module to the checksum of the pseudo header
1073fbde705aSYaroslav Tykhiydefined by the TCP and UDP specifications.
10740193eb5eSYaroslav Tykhiy.Pp
10750193eb5eSYaroslav TykhiyOn input, an interface indicates the actions it has performed
10760193eb5eSYaroslav Tykhiyon a packet by setting one or more of the following flags in
10770193eb5eSYaroslav Tykhiy.Va csum_flags
10780193eb5eSYaroslav Tykhiyassociated with the packet:
10790193eb5eSYaroslav Tykhiy.Bl -tag -width ".Dv CSUM_IP_CHECKED" -offset indent
10800193eb5eSYaroslav Tykhiy.It Dv CSUM_IP_CHECKED
10810193eb5eSYaroslav TykhiyThe IP header checksum has been computed.
10820193eb5eSYaroslav Tykhiy.It Dv CSUM_IP_VALID
10830193eb5eSYaroslav TykhiyThe IP header has a valid checksum.
10840193eb5eSYaroslav TykhiyThis flag can appear only in combination with
10850193eb5eSYaroslav Tykhiy.Dv CSUM_IP_CHECKED .
10860193eb5eSYaroslav Tykhiy.It Dv CSUM_DATA_VALID
10870193eb5eSYaroslav TykhiyThe checksum of the data portion of the IP packet has been computed
10880193eb5eSYaroslav Tykhiyand stored in the field
10890193eb5eSYaroslav Tykhiy.Va csum_data
10900193eb5eSYaroslav Tykhiyin network byte order.
10910193eb5eSYaroslav Tykhiy.It Dv CSUM_PSEUDO_HDR
10920193eb5eSYaroslav TykhiyCan be set only along with
10930193eb5eSYaroslav Tykhiy.Dv CSUM_DATA_VALID
10940193eb5eSYaroslav Tykhiyto indicate that the IP data checksum found in
10950193eb5eSYaroslav Tykhiy.Va csum_data
1096fbde705aSYaroslav Tykhiyallows for the pseudo header defined by the TCP and UDP specifications.
10970193eb5eSYaroslav TykhiyOtherwise the checksum of the pseudo header must be calculated by
10980193eb5eSYaroslav Tykhiythe host CPU and added to
10990193eb5eSYaroslav Tykhiy.Va csum_data
1100fbde705aSYaroslav Tykhiyto obtain the final checksum to be used for TCP or UDP validation purposes.
11010193eb5eSYaroslav Tykhiy.El
11020193eb5eSYaroslav Tykhiy.Pp
11030193eb5eSYaroslav TykhiyIf a particular network interface just indicates success or
11040193eb5eSYaroslav Tykhiyfailure of TCP or UDP checksum validation without returning
11050193eb5eSYaroslav Tykhiythe exact value of the checksum to the host CPU, its driver can mark
11060193eb5eSYaroslav Tykhiy.Dv CSUM_DATA_VALID
11070193eb5eSYaroslav Tykhiyand
11080193eb5eSYaroslav Tykhiy.Dv CSUM_PSEUDO_HDR
11090193eb5eSYaroslav Tykhiyin
11100193eb5eSYaroslav Tykhiy.Va csum_flags ,
11110193eb5eSYaroslav Tykhiyand set
11120193eb5eSYaroslav Tykhiy.Va csum_data
11130193eb5eSYaroslav Tykhiyto
11140193eb5eSYaroslav Tykhiy.Li 0xFFFF
11150193eb5eSYaroslav Tykhiyhexadecimal to indicate a valid checksum.
1116f48cbcdeSRuslan ErmilovIt is a peculiarity of the algorithm used that the Internet checksum
11170193eb5eSYaroslav Tykhiycalculated over any valid packet will be
11180193eb5eSYaroslav Tykhiy.Li 0xFFFF
11190193eb5eSYaroslav Tykhiyas long as the original checksum field is included.
11203cab047eSMike Silbersack.Sh STRESS TESTING
1121c8185672SRuslan ErmilovWhen running a kernel compiled with the option
1122c8185672SRuslan Ermilov.Dv MBUF_STRESS_TEST ,
1123c8185672SRuslan Ermilovthe following
1124c8185672SRuslan Ermilov.Xr sysctl 8 Ns
1125c8185672SRuslan Ermilov-controlled options may be used to create
11263cab047eSMike Silbersackvarious failure/extreme cases for testing of network drivers
11277b2fd183SYaroslav Tykhiyand other parts of the kernel that rely on
11287b2fd183SYaroslav Tykhiy.Vt mbufs .
1129c8185672SRuslan Ermilov.Bl -tag -width ident
1130c8185672SRuslan Ermilov.It Va net.inet.ip.mbuf_frag_size
1131c8185672SRuslan ErmilovCauses
1132c8185672SRuslan Ermilov.Fn ip_output
11337b2fd183SYaroslav Tykhiyto fragment outgoing
11347b2fd183SYaroslav Tykhiy.Vt mbuf chains
11357b2fd183SYaroslav Tykhiyinto fragments of the specified size.
1136c8185672SRuslan ErmilovSetting this variable to 1 is an excellent way to
11377b2fd183SYaroslav Tykhiytest the long
11387b2fd183SYaroslav Tykhiy.Vt mbuf chain
11397b2fd183SYaroslav Tykhiyhandling ability of network drivers.
1140c8185672SRuslan Ermilov.It Va kern.ipc.m_defragrandomfailures
1141c8185672SRuslan ErmilovCauses the function
1142c8185672SRuslan Ermilov.Fn m_defrag
1143c8185672SRuslan Ermilovto randomly fail, returning
1144c8185672SRuslan Ermilov.Dv NULL .
1145c8185672SRuslan ErmilovAny piece of code which uses
1146c8185672SRuslan Ermilov.Fn m_defrag
1147c8185672SRuslan Ermilovshould be tested with this feature.
1148c8185672SRuslan Ermilov.El
1149de06f907SNik Clayton.Sh RETURN VALUES
1150de06f907SNik ClaytonSee above.
11510193eb5eSYaroslav Tykhiy.Sh SEE ALSO
1152e85cc3f5SRuslan Ermilov.Xr ifnet 9 ,
1153e85cc3f5SRuslan Ermilov.Xr mbuf_tags 9
1154de06f907SNik Clayton.Sh HISTORY
1155de06f907SNik Clayton.\" Please correct me if I'm wrong
11567b2fd183SYaroslav Tykhiy.Vt Mbufs
11577b2fd183SYaroslav Tykhiyappeared in an early version of
1158753d686dSRuslan Ermilov.Bx .
115953b2520dSTom RhodesBesides being used for network packets, they were used
1160bfd59721SBosko Milekicto store various dynamic structures, such as routing table
1161bfd59721SBosko Milekicentries, interface addresses, protocol control blocks, etc.
1162db430f97SRobert WatsonIn more recent
1163db430f97SRobert Watson.Fx
1164db430f97SRobert Watsonuse of
1165db430f97SRobert Watson.Vt mbufs
1166db430f97SRobert Watsonis almost entirely limited to packet storage, with
1167db430f97SRobert Watson.Xr uma 9
1168db430f97SRobert Watsonzones being used directly to store other network-related memory.
1169db430f97SRobert Watson.Pp
1170db430f97SRobert WatsonHistorically, the
1171db430f97SRobert Watson.Vt mbuf
1172db430f97SRobert Watsonallocator has been a special-purpose memory allocator able to run in
1173db430f97SRobert Watsoninterrupt contexts and allocating from a special kernel address space map.
1174db430f97SRobert WatsonAs of
1175db430f97SRobert Watson.Fx 5.3 ,
1176db430f97SRobert Watsonthe
1177db430f97SRobert Watson.Vt mbuf
1178db430f97SRobert Watsonallocator is a wrapper around
1179db430f97SRobert Watson.Xr uma 9 ,
1180db430f97SRobert Watsonallowing caching of
1181db430f97SRobert Watson.Vt mbufs ,
1182db430f97SRobert Watsonclusters, and
1183db430f97SRobert Watson.Vt mbuf
1184db430f97SRobert Watson+ cluster pairs in per-CPU caches, as well as bringing other benefits of
1185db430f97SRobert Watsonslab allocation.
1186de06f907SNik Clayton.Sh AUTHORS
11876575e6daSRuslan ErmilovThe original
11886575e6daSRuslan Ermilov.Nm
11891e9469d1SChristian Brueffermanual page was written by
11901e9469d1SChristian Brueffer.An Yar Tikhiy .
1191db430f97SRobert WatsonThe
1192db430f97SRobert Watson.Xr uma 9
1193db430f97SRobert Watson.Vt mbuf
11941e9469d1SChristian Bruefferallocator was written by
11951e9469d1SChristian Brueffer.An Bosko Milekic .
1196