xref: /freebsd/share/man/man9/mbuf.9 (revision 36a142c455a41172df398f10eb3e110ca29fbf72)
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.\"
27b7131a26SJohn-Mark Gurney.Dd August 27, 2004
28de06f907SNik Clayton.Dt MBUF 9
29de06f907SNik Clayton.Os
30de06f907SNik Clayton.\"
31de06f907SNik Clayton.Sh NAME
32de06f907SNik Clayton.Nm mbuf
33de06f907SNik Clayton.Nd "memory management in the kernel IPC subsystem"
34de06f907SNik Clayton.\"
35de06f907SNik Clayton.Sh SYNOPSIS
3632eef9aeSRuslan Ermilov.In sys/param.h
37f16b3c0dSChad David.In sys/systm.h
3832eef9aeSRuslan Ermilov.In sys/mbuf.h
39de06f907SNik Clayton.\"
40bfd59721SBosko Milekic.Ss Mbuf allocation macros
41de06f907SNik Clayton.Fn MGET "struct mbuf *mbuf" "int how" "short type"
42de06f907SNik Clayton.Fn MGETHDR "struct mbuf *mbuf" "int how" "short type"
43de06f907SNik Clayton.Fn MCLGET "struct mbuf *mbuf" "int how"
44bfd59721SBosko Milekic.Fo MEXTADD
45bfd59721SBosko Milekic.Fa "struct mbuf *mbuf"
46bfd59721SBosko Milekic.Fa "caddr_t buf"
47bfd59721SBosko Milekic.Fa "u_int size"
48bfd59721SBosko Milekic.Fa "void (*free)(void *opt_args)"
49bfd59721SBosko Milekic.Fa "void *opt_args"
505d4050ccSBosko Milekic.Fa "short flags"
515d4050ccSBosko Milekic.Fa "int type"
52bfd59721SBosko Milekic.Fc
53bfd59721SBosko Milekic.Fn MEXTFREE "struct mbuf *mbuf"
54bfd59721SBosko Milekic.Fn MEXT_ADD_REF "struct mbuf *mbuf"
55bfd59721SBosko Milekic.Fn MEXT_REM_REF "struct mbuf *mbuf"
56bfd59721SBosko Milekic.Fn MFREE "struct mbuf *mbuf" "struct mbuf *successor"
57de06f907SNik Clayton.\"
58bfd59721SBosko Milekic.Ss Mbuf utility macros
59f16b3c0dSChad David.Fn mtod "struct mbuf *mbuf" "type"
60f16b3c0dSChad David.Ft int
61bfd59721SBosko Milekic.Fn MEXT_IS_REF "struct mbuf *mbuf"
62bfd59721SBosko Milekic.Fn M_ALIGN "struct mbuf *mbuf" "u_int len"
63bfd59721SBosko Milekic.Fn MH_ALIGN "struct mbuf *mbuf" "u_int len"
64f16b3c0dSChad David.Ft int
65bfd59721SBosko Milekic.Fn M_LEADINGSPACE "struct mbuf *mbuf"
66f16b3c0dSChad David.Ft int
67bfd59721SBosko Milekic.Fn M_TRAILINGSPACE "struct mbuf *mbuf"
68da72b998SYaroslav Tykhiy.Fn M_MOVE_PKTHDR "struct mbuf *to" "struct mbuf *from"
69bfd59721SBosko Milekic.Fn M_PREPEND "struct mbuf *mbuf" "int len" "int how"
70bfd59721SBosko Milekic.Fn MCHTYPE "struct mbuf *mbuf" "u_int type"
71f16b3c0dSChad David.Ft int
725d4050ccSBosko Milekic.Fn M_WRITABLE "struct mbuf *mbuf"
73bfd59721SBosko Milekic.\"
74bfd59721SBosko Milekic.Ss Mbuf allocation functions
75de06f907SNik Clayton.Ft struct mbuf *
76de06f907SNik Clayton.Fn m_get "int how" "int type"
77de06f907SNik Clayton.Ft struct mbuf *
78328c0bbcSBosko Milekic.Fn m_getm "struct mbuf *orig" "int len" "int how" "int type"
79328c0bbcSBosko Milekic.Ft struct mbuf *
806c013706SMaxim Konovalov.Fn m_getcl "int how" "short type" "int flags"
816c013706SMaxim Konovalov.Ft struct mbuf *
82de06f907SNik Clayton.Fn m_getclr "int how" "int type"
83de06f907SNik Clayton.Ft struct mbuf *
84de06f907SNik Clayton.Fn m_gethdr "int how" "int type"
85bfd59721SBosko Milekic.Ft struct mbuf *
86bfd59721SBosko Milekic.Fn m_free "struct mbuf *mbuf"
87de06f907SNik Clayton.Ft void
88de06f907SNik Clayton.Fn m_freem "struct mbuf *mbuf"
89bfd59721SBosko Milekic.\"
90bfd59721SBosko Milekic.Ss Mbuf utility functions
91de06f907SNik Clayton.Ft void
92de06f907SNik Clayton.Fn m_adj "struct mbuf *mbuf" "int len"
93b9def06bSSam Leffler.Ft int
94b9def06bSSam Leffler.Fn m_append "struct mbuf *mbuf" "int len" "c_caddr_t cp"
95de06f907SNik Clayton.Ft struct mbuf *
96de06f907SNik Clayton.Fn m_prepend "struct mbuf *mbuf" "int len" "int how"
97de06f907SNik Clayton.Ft struct mbuf *
98de06f907SNik Clayton.Fn m_pullup "struct mbuf *mbuf" "int len"
99de06f907SNik Clayton.Ft struct mbuf *
100de06f907SNik Clayton.Fn m_copym "struct mbuf *mbuf" "int offset" "int len" "int how"
101de06f907SNik Clayton.Ft struct mbuf *
102de06f907SNik Clayton.Fn m_copypacket "struct mbuf *mbuf" "int how"
103de06f907SNik Clayton.Ft struct mbuf *
104de06f907SNik Clayton.Fn m_dup "struct mbuf *mbuf" "int how"
105de06f907SNik Clayton.Ft void
1060d8e4c57SBruce Evans.Fn m_copydata "const struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
107de06f907SNik Clayton.Ft void
108de06f907SNik Clayton.Fn m_copyback "struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
109de06f907SNik Clayton.Ft struct mbuf *
110de06f907SNik Clayton.Fo m_devget
111de06f907SNik Clayton.Fa "char *buf"
112de06f907SNik Clayton.Fa "int len"
113de06f907SNik Clayton.Fa "int offset"
114de06f907SNik Clayton.Fa "struct ifnet *ifp"
115de06f907SNik Clayton.Fa "void (*copy)(char *from, caddr_t to, u_int len)"
116de06f907SNik Clayton.Fc
117de06f907SNik Clayton.Ft void
118de06f907SNik Clayton.Fn m_cat "struct mbuf *m" "struct mbuf *n"
119e60fa837SRuslan Ermilov.Ft u_int
1200b1c0994SPoul-Henning Kamp.Fn m_fixhdr "struct mbuf *mbuf"
121da72b998SYaroslav Tykhiy.Ft void
122da72b998SYaroslav Tykhiy.Fn m_dup_pkthdr "struct mbuf *to" "struct mbuf *from"
123da72b998SYaroslav Tykhiy.Ft void
124da72b998SYaroslav Tykhiy.Fn m_move_pkthdr "struct mbuf *to" "struct mbuf *from"
125e60fa837SRuslan Ermilov.Ft u_int
1260b1c0994SPoul-Henning Kamp.Fn m_length "struct mbuf *mbuf" "struct mbuf **last"
127de06f907SNik Clayton.Ft struct mbuf *
128de06f907SNik Clayton.Fn m_split "struct mbuf *mbuf" "int len" "int how"
12902312219SBruce M Simpson.Ft int
13002312219SBruce M Simpson.Fn m_apply "struct mbuf *mbuf" "int off" "int len" "int (*f)(void *arg, void *data, u_int len)" "void *arg"
13102312219SBruce M Simpson.Ft struct mbuf *
13202312219SBruce M Simpson.Fn m_getptr "struct mbuf *mbuf" "int loc" "int *off"
133a61bc764SMike Silbersack.Ft struct mbuf *
134a61bc764SMike Silbersack.Fn m_defrag "struct mbuf *m0" "int how"
135de06f907SNik Clayton.\"
136de06f907SNik Clayton.Sh DESCRIPTION
1377b2fd183SYaroslav TykhiyAn
1387b2fd183SYaroslav Tykhiy.Vt mbuf
1397b2fd183SYaroslav Tykhiyis a basic unit of memory management in the kernel IPC subsystem.
1407b2fd183SYaroslav TykhiyNetwork packets and socket buffers are stored in
1417b2fd183SYaroslav Tykhiy.Vt mbufs .
1427b2fd183SYaroslav TykhiyA network packet may span multiple
1437b2fd183SYaroslav Tykhiy.Vt mbufs
1447b2fd183SYaroslav Tykhiyarranged into a
1457b2fd183SYaroslav Tykhiy.Vt mbuf chain
146c4d9468eSRuslan Ermilov(linked list),
147de06f907SNik Claytonwhich allows adding or trimming
148bfd59721SBosko Milekicnetwork headers with little overhead.
149de06f907SNik Clayton.Pp
1507b2fd183SYaroslav TykhiyWhile a developer should not bother with
1517b2fd183SYaroslav Tykhiy.Vt mbuf
1527b2fd183SYaroslav Tykhiyinternals without serious
153de06f907SNik Claytonreason in order to avoid incompatibilities with future changes, it
1547b2fd183SYaroslav Tykhiyis useful to understand the general structure of an
1557b2fd183SYaroslav Tykhiy.Vt mbuf .
156de06f907SNik Clayton.Pp
1577b2fd183SYaroslav TykhiyAn
1587b2fd183SYaroslav Tykhiy.Vt mbuf
1597b2fd183SYaroslav Tykhiyconsists of a variable-sized header and a small internal
160e73145f6SSheldon Hearnbuffer for data.
1617b2fd183SYaroslav TykhiyThe total size of an
1627b2fd183SYaroslav Tykhiy.Vt mbuf ,
163bfd59721SBosko Milekic.Dv MSIZE ,
164feccf50cSAlex Dupreis a constant defined in
165feccf50cSAlex Dupre.In sys/param.h .
1667b2fd183SYaroslav TykhiyThe
1677b2fd183SYaroslav Tykhiy.Vt mbuf
1687b2fd183SYaroslav Tykhiyheader includes:
169de06f907SNik Clayton.Pp
170238d9c59SYaroslav Tykhiy.Bl -tag -width "m_nextpkt" -offset indent
171be82a0bcSYaroslav Tykhiy.It Va m_next
172238d9c59SYaroslav Tykhiy.Pq Vt struct mbuf *
173238d9c59SYaroslav TykhiyA pointer to the next
1747b2fd183SYaroslav Tykhiy.Vt mbuf
1757b2fd183SYaroslav Tykhiyin the
176238d9c59SYaroslav Tykhiy.Vt mbuf chain .
177be82a0bcSYaroslav Tykhiy.It Va m_nextpkt
178238d9c59SYaroslav Tykhiy.Pq Vt struct mbuf *
179238d9c59SYaroslav TykhiyA pointer to the next
1807b2fd183SYaroslav Tykhiy.Vt mbuf chain
181238d9c59SYaroslav Tykhiyin the queue.
182be82a0bcSYaroslav Tykhiy.It Va m_data
183238d9c59SYaroslav Tykhiy.Pq Vt caddr_t
184238d9c59SYaroslav TykhiyA pointer to data attached to this
185238d9c59SYaroslav Tykhiy.Vt mbuf .
186be82a0bcSYaroslav Tykhiy.It Va m_len
187238d9c59SYaroslav Tykhiy.Pq Vt int
188238d9c59SYaroslav TykhiyThe length of the data.
189be82a0bcSYaroslav Tykhiy.It Va m_type
190238d9c59SYaroslav Tykhiy.Pq Vt short
191238d9c59SYaroslav TykhiyThe type of the data.
192be82a0bcSYaroslav Tykhiy.It Va m_flags
193238d9c59SYaroslav Tykhiy.Pq Vt int
194238d9c59SYaroslav TykhiyThe
1957b2fd183SYaroslav Tykhiy.Vt mbuf
196238d9c59SYaroslav Tykhiyflags.
197de06f907SNik Clayton.El
198de06f907SNik Clayton.Pp
1997b2fd183SYaroslav TykhiyThe
2007b2fd183SYaroslav Tykhiy.Vt mbuf
2017b2fd183SYaroslav Tykhiyflag bits are defined as follows:
202de06f907SNik Clayton.Bd -literal
203de06f907SNik Clayton/* mbuf flags */
204de06f907SNik Clayton#define	M_EXT		0x0001	/* has associated external storage */
205de06f907SNik Clayton#define	M_PKTHDR	0x0002	/* start of record */
206de06f907SNik Clayton#define	M_EOR		0x0004	/* end of record */
2075d4050ccSBosko Milekic#define	M_RDONLY	0x0008	/* associated data marked read-only */
2085d4050ccSBosko Milekic#define	M_PROTO1	0x0010	/* protocol-specific */
2095d4050ccSBosko Milekic#define	M_PROTO2	0x0020 	/* protocol-specific */
2105d4050ccSBosko Milekic#define	M_PROTO3	0x0040	/* protocol-specific */
2115d4050ccSBosko Milekic#define	M_PROTO4	0x0080	/* protocol-specific */
2125d4050ccSBosko Milekic#define	M_PROTO5	0x0100	/* protocol-specific */
213f434362aSYaroslav Tykhiy#define	M_PROTO6	0x4000	/* protocol-specific (avoid M_BCAST conflict) */
214f434362aSYaroslav Tykhiy#define	M_FREELIST	0x8000	/* mbuf is on the free list */
215de06f907SNik Clayton
216f434362aSYaroslav Tykhiy/* mbuf pkthdr flags (also stored in m_flags) */
2175d4050ccSBosko Milekic#define	M_BCAST		0x0200	/* send/received as link-level broadcast */
2185d4050ccSBosko Milekic#define	M_MCAST		0x0400	/* send/received as link-level multicast */
2195d4050ccSBosko Milekic#define	M_FRAG		0x0800	/* packet is fragment of larger packet */
2205d4050ccSBosko Milekic#define	M_FIRSTFRAG	0x1000	/* packet is first fragment */
2215d4050ccSBosko Milekic#define	M_LASTFRAG	0x2000	/* packet is last fragment */
222de06f907SNik Clayton.Ed
223de06f907SNik Clayton.Pp
2247b2fd183SYaroslav TykhiyThe available
2257b2fd183SYaroslav Tykhiy.Vt mbuf
2267b2fd183SYaroslav Tykhiytypes are defined as follows:
227de06f907SNik Clayton.Bd -literal
228de06f907SNik Clayton/* mbuf types */
229de06f907SNik Clayton#define	MT_DATA		1	/* dynamic (data) allocation */
230de06f907SNik Clayton#define	MT_HEADER	2	/* packet header */
231de06f907SNik Clayton#define	MT_SONAME	8	/* socket name */
232de06f907SNik Clayton#define	MT_FTABLE	11	/* fragment reassembly header */
233de06f907SNik Clayton#define	MT_CONTROL	14	/* extra-data protocol message */
234de06f907SNik Clayton#define	MT_OOBDATA	15	/* expedited data */
235de06f907SNik Clayton.Ed
236de06f907SNik Clayton.Pp
237de06f907SNik ClaytonIf the
238de06f907SNik Clayton.Dv M_PKTHDR
239de06f907SNik Claytonflag is set, a
2407b2fd183SYaroslav Tykhiy.Vt struct pkthdr Va m_pkthdr
2417b2fd183SYaroslav Tykhiyis added to the
2427b2fd183SYaroslav Tykhiy.Vt mbuf
2437b2fd183SYaroslav Tykhiyheader.
244e73145f6SSheldon HearnIt contains a pointer to the interface
245de06f907SNik Claytonthe packet has been received from
2467b2fd183SYaroslav Tykhiy.Pq Vt struct ifnet Va *rcvif ,
247de06f907SNik Claytonand the total packet length
2487b2fd183SYaroslav Tykhiy.Pq Vt int Va len .
249e85cc3f5SRuslan ErmilovOptionally, it may also contain an attached list of packet tags
250e85cc3f5SRuslan Ermilov.Pq Vt "struct m_tag" .
251e85cc3f5SRuslan ErmilovSee
252e85cc3f5SRuslan Ermilov.Xr mbuf_tags 9
253e85cc3f5SRuslan Ermilovfor details.
2540193eb5eSYaroslav TykhiyFields used in offloading checksum calculation to the hardware are kept in
2550193eb5eSYaroslav Tykhiy.Va m_pkthdr
2560193eb5eSYaroslav Tykhiyas well.
2570193eb5eSYaroslav TykhiySee
2580193eb5eSYaroslav Tykhiy.Sx HARDWARE-ASSISTED CHECKSUM CALCULATION
2590193eb5eSYaroslav Tykhiyfor details.
260de06f907SNik Clayton.Pp
2617b2fd183SYaroslav TykhiyIf small enough, data is stored in the internal data buffer of an
2627b2fd183SYaroslav Tykhiy.Vt mbuf .
2637b2fd183SYaroslav TykhiyIf the data is sufficiently large, another
2647b2fd183SYaroslav Tykhiy.Vt mbuf
2657b2fd183SYaroslav Tykhiymay be added to the
2667b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
2677b2fd183SYaroslav Tykhiyor external storage may be associated with the
2687b2fd183SYaroslav Tykhiy.Vt mbuf .
269de06f907SNik Clayton.Dv MHLEN
2707b2fd183SYaroslav Tykhiybytes of data can fit into an
2717b2fd183SYaroslav Tykhiy.Vt mbuf
2727b2fd183SYaroslav Tykhiywith the
273de06f907SNik Clayton.Dv M_PKTHDR
274de06f907SNik Claytonflag set,
275de06f907SNik Clayton.Dv MLEN
276de06f907SNik Claytonbytes can otherwise.
277de06f907SNik Clayton.Pp
2787b2fd183SYaroslav TykhiyIf external storage is being associated with an
2797b2fd183SYaroslav Tykhiy.Vt mbuf ,
2807b2fd183SYaroslav Tykhiythe
281be82a0bcSYaroslav Tykhiy.Va m_ext
28225f1a2e9SSheldon Hearnheader is added at the cost of losing the internal data buffer.
283e73145f6SSheldon HearnIt includes a pointer to external storage, the size of the storage,
284e73145f6SSheldon Hearna pointer to a function used for freeing the storage,
285e73145f6SSheldon Hearna pointer to an optional argument that can be passed to the function,
286e73145f6SSheldon Hearnand a pointer to a reference counter.
2877b2fd183SYaroslav TykhiyAn
2887b2fd183SYaroslav Tykhiy.Vt mbuf
2897b2fd183SYaroslav Tykhiyusing external storage has the
290de06f907SNik Clayton.Dv M_EXT
291de06f907SNik Claytonflag set.
292de06f907SNik Clayton.Pp
293bfd59721SBosko MilekicThe system supplies a macro for allocating the desired external storage
294bfd59721SBosko Milekicbuffer,
295bfd59721SBosko Milekic.Dv MEXTADD .
296bfd59721SBosko Milekic.Pp
297bfd59721SBosko MilekicThe allocation and management of the reference counter is handled by the
298e73145f6SSheldon Hearnsubsystem.
299e73145f6SSheldon HearnThe developer can check whether the reference count for the
3007b2fd183SYaroslav Tykhiyexternal storage of a given
3017b2fd183SYaroslav Tykhiy.Vt mbuf
3027b2fd183SYaroslav Tykhiyis greater than 1 with the
303bfd59721SBosko Milekic.Dv MEXT_IS_REF
304e73145f6SSheldon Hearnmacro.
305e73145f6SSheldon HearnSimilarly, the developer can directly add and remove references,
306e73145f6SSheldon Hearnif absolutely necessary, with the use of the
307bfd59721SBosko Milekic.Dv MEXT_ADD_REF
308bfd59721SBosko Milekicand
309bfd59721SBosko Milekic.Dv MEXT_REM_REF
310bfd59721SBosko Milekicmacros.
311bfd59721SBosko Milekic.Pp
312bfd59721SBosko MilekicThe system also supplies a default type of external storage buffer called an
3137b2fd183SYaroslav Tykhiy.Vt mbuf cluster .
3147b2fd183SYaroslav Tykhiy.Vt Mbuf clusters
3157b2fd183SYaroslav Tykhiycan be allocated and configured with the use of the
316bfd59721SBosko Milekic.Dv MCLGET
317e73145f6SSheldon Hearnmacro.
3187b2fd183SYaroslav TykhiyEach
3197b2fd183SYaroslav Tykhiy.Vt mbuf cluster
3207b2fd183SYaroslav Tykhiyis
321de06f907SNik Clayton.Dv MCLBYTES
322bfd59721SBosko Milekicin size, where MCLBYTES is a machine-dependent constant.
323bfd59721SBosko MilekicThe system defines an advisory macro
324de06f907SNik Clayton.Dv MINCLSIZE ,
3257b2fd183SYaroslav Tykhiywhich is the smallest amount of data to put into an
3267b2fd183SYaroslav Tykhiy.Vt mbuf cluster .
32736a142c4SRuslan ErmilovIt is equal to the sum of
328de06f907SNik Clayton.Dv MLEN
329de06f907SNik Claytonand
330de06f907SNik Clayton.Dv MHLEN .
3317b2fd183SYaroslav TykhiyIt is typically preferable to store data into the data region of an
3327b2fd183SYaroslav Tykhiy.Vt mbuf ,
3337b2fd183SYaroslav Tykhiyif size permits, as opposed to allocating a separate
3347b2fd183SYaroslav Tykhiy.Vt mbuf cluster
3357b2fd183SYaroslav Tykhiyto hold the same data.
336de06f907SNik Clayton.\"
337de06f907SNik Clayton.Ss Macros and Functions
338bfd59721SBosko MilekicThere are numerous predefined macros and functions that provide the
339bfd59721SBosko Milekicdeveloper with common utilities.
340de06f907SNik Clayton.\"
341de06f907SNik Clayton.Bl -ohang -offset indent
342de06f907SNik Clayton.It Fn mtod mbuf type
3437b2fd183SYaroslav TykhiyConvert an
3447b2fd183SYaroslav Tykhiy.Fa mbuf
3457b2fd183SYaroslav Tykhiypointer to a data pointer.
3467b2fd183SYaroslav TykhiyThe macro expands to the data pointer cast to the pointer of the specified
3477b2fd183SYaroslav Tykhiy.Fa type .
348de06f907SNik Clayton.Sy Note :
3497b2fd183SYaroslav TykhiyIt is advisable to ensure that there is enough contiguous data in
3507b2fd183SYaroslav Tykhiy.Fa mbuf .
351de06f907SNik ClaytonSee
352de06f907SNik Clayton.Fn m_pullup
353de06f907SNik Claytonfor details.
354de06f907SNik Clayton.It Fn MGET mbuf how type
3557b2fd183SYaroslav TykhiyAllocate an
3567b2fd183SYaroslav Tykhiy.Vt mbuf
3577b2fd183SYaroslav Tykhiyand initialize it to contain internal data.
35825f1a2e9SSheldon Hearn.Fa mbuf
3597b2fd183SYaroslav Tykhiywill point to the allocated
3607b2fd183SYaroslav Tykhiy.Vt mbuf
3617b2fd183SYaroslav Tykhiyon success, or be set to
362de06f907SNik Clayton.Dv NULL
363e73145f6SSheldon Hearnon failure.
364e73145f6SSheldon HearnThe
365de06f907SNik Clayton.Fa how
366de06f907SNik Claytonargument is to be set to
367c65b53c5STom Rhodes.Dv M_TRYWAIT
368de06f907SNik Claytonor
369c65b53c5STom Rhodes.Dv M_DONTWAIT .
37025f1a2e9SSheldon HearnIt specifies whether the caller is willing to block if necessary.
371e73145f6SSheldon HearnIf
372de06f907SNik Clayton.Fa how
37325f1a2e9SSheldon Hearnis set to
374c65b53c5STom Rhodes.Dv M_TRYWAIT ,
37525f1a2e9SSheldon Hearna failed allocation will result in the caller being put
376bfd59721SBosko Milekicto sleep for a designated
37725f1a2e9SSheldon Hearnkern.ipc.mbuf_wait
378c4d9468eSRuslan Ermilov.Xr ( sysctl 8
379c4d9468eSRuslan Ermilovtunable)
38025f1a2e9SSheldon Hearnnumber of ticks.
3817b2fd183SYaroslav TykhiyA number of other functions and macros related to
3827b2fd183SYaroslav Tykhiy.Vt mbufs
3837b2fd183SYaroslav Tykhiyhave the same argument because they may
3847b2fd183SYaroslav Tykhiyat some point need to allocate new
3857b2fd183SYaroslav Tykhiy.Vt mbufs .
386c65b53c5STom Rhodes.Pp
3877b2fd183SYaroslav TykhiyProgrammers should be careful not to confuse the
3887b2fd183SYaroslav Tykhiy.Vt mbuf
3897b2fd183SYaroslav Tykhiyallocation flag
390c65b53c5STom Rhodes.Dv M_DONTWAIT
391c65b53c5STom Rhodeswith the
392c65b53c5STom Rhodes.Xr malloc 9
393c65b53c5STom Rhodesallocation flag,
394c65b53c5STom Rhodes.Dv M_NOWAIT .
395c65b53c5STom RhodesThey are not the same.
396de06f907SNik Clayton.It Fn MGETHDR mbuf how type
3977b2fd183SYaroslav TykhiyAllocate an
3987b2fd183SYaroslav Tykhiy.Vt mbuf
3997b2fd183SYaroslav Tykhiyand initialize it to contain a packet header
400e73145f6SSheldon Hearnand internal data.
401e73145f6SSheldon HearnSee
402de06f907SNik Clayton.Fn MGET
403de06f907SNik Claytonfor details.
404de06f907SNik Clayton.It Fn MCLGET mbuf how
4057b2fd183SYaroslav TykhiyAllocate and attach an
4067b2fd183SYaroslav Tykhiy.Vt mbuf cluster
4077b2fd183SYaroslav Tykhiyto
4087b2fd183SYaroslav Tykhiy.Fa mbuf .
409e73145f6SSheldon HearnIf the macro fails, the
410de06f907SNik Clayton.Dv M_EXT
4110227791bSRuslan Ermilovflag will not be set in
4127b2fd183SYaroslav Tykhiy.Fa mbuf .
413da72b998SYaroslav Tykhiy.It Fn M_ALIGN mbuf len
414da72b998SYaroslav TykhiySet the pointer
415da72b998SYaroslav Tykhiy.Fa mbuf->m_data
416da72b998SYaroslav Tykhiyto place an object of the size
417da72b998SYaroslav Tykhiy.Fa len
418da72b998SYaroslav Tykhiyat the end of the internal data area of
419da72b998SYaroslav Tykhiy.Fa mbuf ,
420da72b998SYaroslav Tykhiylong word aligned.
421da72b998SYaroslav TykhiyApplicable only if
422da72b998SYaroslav Tykhiy.Fa mbuf
423da72b998SYaroslav Tykhiyis newly allocated with
424da72b998SYaroslav Tykhiy.Fn MGET
425da72b998SYaroslav Tykhiyor
426da72b998SYaroslav Tykhiy.Fn m_get .
427da72b998SYaroslav Tykhiy.It Fn MH_ALIGN mbuf len
428da72b998SYaroslav TykhiyServes the same purpose as
429da72b998SYaroslav Tykhiy.Fn M_ALIGN
430da72b998SYaroslav Tykhiydoes, but only for
431da72b998SYaroslav Tykhiy.Fa mbuf
432da72b998SYaroslav Tykhiynewly allocated with
433da72b998SYaroslav Tykhiy.Fn MGETHDR
434da72b998SYaroslav Tykhiyor
435da72b998SYaroslav Tykhiy.Fn m_gethdr ,
436da72b998SYaroslav Tykhiyor initialized by
437da72b998SYaroslav Tykhiy.Fn m_dup_pkthdr
438da72b998SYaroslav Tykhiyor
439da72b998SYaroslav Tykhiy.Fn m_move_pkthdr .
440da72b998SYaroslav Tykhiy.It Fn M_LEADINGSPACE mbuf
441da72b998SYaroslav TykhiyReturns the number of bytes available before the beginning
442da72b998SYaroslav Tykhiyof data in
443da72b998SYaroslav Tykhiy.Fa mbuf .
444da72b998SYaroslav Tykhiy.It Fn M_TRAILINGSPACE mbuf
445da72b998SYaroslav TykhiyReturns the number of bytes available after the end of data in
446da72b998SYaroslav Tykhiy.Fa mbuf .
447de06f907SNik Clayton.It Fn M_PREPEND mbuf len how
4487b2fd183SYaroslav TykhiyThis macro operates on an
4497b2fd183SYaroslav Tykhiy.Vt mbuf chain .
450de06f907SNik ClaytonIt is an optimized wrapper for
451de06f907SNik Clayton.Fn m_prepend
452de06f907SNik Claytonthat can make use of possible empty space before data
4535203edcdSRuslan Ermilov(e.g.\& left after trimming of a link-layer header).
4547b2fd183SYaroslav TykhiyThe new
4557b2fd183SYaroslav Tykhiy.Vt mbuf chain
4567b2fd183SYaroslav Tykhiypointer or
457de06f907SNik Clayton.Dv NULL
458de06f907SNik Claytonis in
459de06f907SNik Clayton.Fa mbuf
460de06f907SNik Claytonafter the call.
461da72b998SYaroslav Tykhiy.It Fn M_MOVE_PKTHDR to from
462da72b998SYaroslav TykhiyUsing this macro is equivalent to calling
463da72b998SYaroslav Tykhiy.Fn m_move_pkthdr to from .
4645d4050ccSBosko Milekic.It Fn M_WRITABLE mbuf
4657b2fd183SYaroslav TykhiyThis macro will evaluate true if
4667b2fd183SYaroslav Tykhiy.Fa mbuf
4677b2fd183SYaroslav Tykhiyis not marked
4685d4050ccSBosko Milekic.Dv M_RDONLY
4697b2fd183SYaroslav Tykhiyand if either
4707b2fd183SYaroslav Tykhiy.Fa mbuf
4717b2fd183SYaroslav Tykhiydoes not contain external storage or,
4725d4050ccSBosko Milekicif it does,
4735d4050ccSBosko Milekicthen if the reference count of the storage is not greater than 1.
4745d4050ccSBosko MilekicThe
4755d4050ccSBosko Milekic.Dv M_RDONLY
4767b2fd183SYaroslav Tykhiyflag can be set in
4777b2fd183SYaroslav Tykhiy.Fa mbuf->m_flags .
4785d4050ccSBosko MilekicThis can be achieved during setup of the external storage,
4795d4050ccSBosko Milekicby passing the
4805d4050ccSBosko Milekic.Dv M_RDONLY
4815d4050ccSBosko Milekicbit as a
4820b39b3ffSRuslan Ermilov.Fa flags
4835d4050ccSBosko Milekicargument to the
4845d4050ccSBosko Milekic.Fn MEXTADD
4857b2fd183SYaroslav Tykhiymacro, or can be directly set in individual
4867b2fd183SYaroslav Tykhiy.Vt mbufs .
487da72b998SYaroslav Tykhiy.It Fn MCHTYPE mbuf type
488da72b998SYaroslav TykhiyChange the type of
489da72b998SYaroslav Tykhiy.Fa mbuf
490da72b998SYaroslav Tykhiyto
491da72b998SYaroslav Tykhiy.Fa type .
492da72b998SYaroslav TykhiyThis is a relatively expensive operation and should be avoided.
493de06f907SNik Clayton.El
494de06f907SNik Clayton.Pp
495de06f907SNik ClaytonThe functions are:
496de06f907SNik Clayton.Bl -ohang -offset indent
497de06f907SNik Clayton.It Fn m_get how type
498de06f907SNik ClaytonA function version of
499bfd59721SBosko Milekic.Fn MGET
500bfd59721SBosko Milekicfor non-critical paths.
501328c0bbcSBosko Milekic.It Fn m_getm orig len how type
502328c0bbcSBosko MilekicAllocate
5030b39b3ffSRuslan Ermilov.Fa len
5047b2fd183SYaroslav Tykhiybytes worth of
5057b2fd183SYaroslav Tykhiy.Vt mbufs
5067b2fd183SYaroslav Tykhiyand
5077b2fd183SYaroslav Tykhiy.Vt mbuf clusters
5087b2fd183SYaroslav Tykhiyif necessary and append the resulting allocated
5097b2fd183SYaroslav Tykhiy.Vt mbuf chain
5107b2fd183SYaroslav Tykhiyto the
5117b2fd183SYaroslav Tykhiy.Vt mbuf chain
5127b2fd183SYaroslav Tykhiy.Fa orig ,
5137b2fd183SYaroslav Tykhiyif it is
5140b39b3ffSRuslan Ermilov.No non- Ns Dv NULL .
515328c0bbcSBosko MilekicIf the allocation fails at any point,
5160b39b3ffSRuslan Ermilovfree whatever was allocated and return
5170b39b3ffSRuslan Ermilov.Dv NULL .
518328c0bbcSBosko MilekicIf
5190b39b3ffSRuslan Ermilov.Fa orig
5200b39b3ffSRuslan Ermilovis
5210b39b3ffSRuslan Ermilov.No non- Ns Dv NULL ,
522328c0bbcSBosko Milekicit will not be freed.
523328c0bbcSBosko MilekicIt is possible to use
524328c0bbcSBosko Milekic.Fn m_getm
525328c0bbcSBosko Milekicto either append
5260b39b3ffSRuslan Ermilov.Fa len
5277b2fd183SYaroslav Tykhiybytes to an existing
5287b2fd183SYaroslav Tykhiy.Vt mbuf
5297b2fd183SYaroslav Tykhiyor
5307b2fd183SYaroslav Tykhiy.Vt mbuf chain
531328c0bbcSBosko Milekic(for example, one which may be sitting in a pre-allocated ring)
5327b2fd183SYaroslav Tykhiyor to simply perform an all-or-nothing
5337b2fd183SYaroslav Tykhiy.Vt mbuf
5347b2fd183SYaroslav Tykhiyand
5357b2fd183SYaroslav Tykhiy.Vt mbuf cluster
5367b2fd183SYaroslav Tykhiyallocation.
537de06f907SNik Clayton.It Fn m_gethdr how type
538de06f907SNik ClaytonA function version of
539bfd59721SBosko Milekic.Fn MGETHDR
540bfd59721SBosko Milekicfor non-critical paths.
5416c013706SMaxim Konovalov.It Fn m_getcl how type flags
5426c013706SMaxim KonovalovFetch an
5436c013706SMaxim Konovalov.Vt mbuf
5446c013706SMaxim Konovalovwith a
5456c013706SMaxim Konovalov.Vt mbuf cluster
5466c013706SMaxim Konovalovattached to it.
5476c013706SMaxim KonovalovIf one of the allocations fails, the entire allocation fails.
5486c013706SMaxim KonovalovThis routine is the preferred way of fetching both the
5496c013706SMaxim Konovalov.Vt mbuf
5506c013706SMaxim Konovalovand
5516c013706SMaxim Konovalov.Vt mbuf cluster
5526c013706SMaxim Konovalovtogether, as it avoids having to unlock/relock between allocations.
5536c013706SMaxim KonovalovReturns
5546c013706SMaxim Konovalov.Dv NULL
5556c013706SMaxim Konovalovon failure.
556de06f907SNik Clayton.It Fn m_getclr how type
5577b2fd183SYaroslav TykhiyAllocate an
5587b2fd183SYaroslav Tykhiy.Vt mbuf
5597b2fd183SYaroslav Tykhiyand zero out the data region.
56001e7fdafSMaxim Konovalov.It Fn m_free mbuf
56101e7fdafSMaxim KonovalovFrees
56201e7fdafSMaxim Konovalov.Vt mbuf .
563b7131a26SJohn-Mark GurneyReturns
564b7131a26SJohn-Mark Gurney.Va m_next
565b7131a26SJohn-Mark Gurneyof the freed
566b7131a26SJohn-Mark Gurney.Vt mbuf .
567de06f907SNik Clayton.El
568de06f907SNik Clayton.Pp
5697b2fd183SYaroslav TykhiyThe functions below operate on
5707b2fd183SYaroslav Tykhiy.Vt mbuf chains .
571de06f907SNik Clayton.Bl -ohang -offset indent
572de06f907SNik Clayton.It Fn m_freem mbuf
5737b2fd183SYaroslav TykhiyFree an entire
5747b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
5757b2fd183SYaroslav Tykhiyincluding any external storage.
576de06f907SNik Clayton.\"
577de06f907SNik Clayton.It Fn m_adj mbuf len
578de06f907SNik ClaytonTrim
579de06f907SNik Clayton.Fa len
5807b2fd183SYaroslav Tykhiybytes from the head of an
5817b2fd183SYaroslav Tykhiy.Vt mbuf chain
5827b2fd183SYaroslav Tykhiyif
583de06f907SNik Clayton.Fa len
584de06f907SNik Claytonis positive, from the tail otherwise.
585de06f907SNik Clayton.\"
586b9def06bSSam Leffler.It Fn m_append mbuf len cp
587b9def06bSSam LefflerAppend
588b9def06bSSam Leffler.Vt len
589b9def06bSSam Lefflerbytes of data
590b9def06bSSam Leffler.Vt cp
591b9def06bSSam Lefflerto the
592b9def06bSSam Leffler.Vt mbuf chain .
593b9def06bSSam LefflerExtend the mbuf chain if the new data does not fit in
594b9def06bSSam Lefflerexisting space.
595b9def06bSSam Leffler.\"
596de06f907SNik Clayton.It Fn m_prepend mbuf len how
5977b2fd183SYaroslav TykhiyAllocate a new
5987b2fd183SYaroslav Tykhiy.Vt mbuf
5997b2fd183SYaroslav Tykhiyand prepend it to the
6007b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
6017b2fd183SYaroslav Tykhiyhandle
602de06f907SNik Clayton.Dv M_PKTHDR
603de06f907SNik Claytonproperly.
604de06f907SNik Clayton.Sy Note :
6050227791bSRuslan ErmilovIt does not allocate any
6067b2fd183SYaroslav Tykhiy.Vt mbuf clusters ,
6077b2fd183SYaroslav Tykhiyso
608de06f907SNik Clayton.Fa len
609de06f907SNik Claytonmust be less than
610de06f907SNik Clayton.Dv MLEN
611de06f907SNik Claytonor
612de06f907SNik Clayton.Dv MHLEN ,
613de06f907SNik Claytondepending on the
614d0353b83SRuslan Ermilov.Dv M_PKTHDR
615d0353b83SRuslan Ermilovflag setting.
616de06f907SNik Clayton.\"
617de06f907SNik Clayton.It Fn m_pullup mbuf len
618de06f907SNik ClaytonArrange that the first
619de06f907SNik Clayton.Fa len
6207b2fd183SYaroslav Tykhiybytes of an
6217b2fd183SYaroslav Tykhiy.Vt mbuf chain
6227b2fd183SYaroslav Tykhiyare contiguous and lay in the data area of
623de06f907SNik Clayton.Fa mbuf ,
624de06f907SNik Claytonso they are accessible with
625de06f907SNik Clayton.Fn mtod mbuf type .
6267b2fd183SYaroslav TykhiyReturn the new
6277b2fd183SYaroslav Tykhiy.Vt mbuf chain
6287b2fd183SYaroslav Tykhiyon success,
629de06f907SNik Clayton.Dv NULL
630de06f907SNik Claytonon failure
6317b2fd183SYaroslav Tykhiy(the
6327b2fd183SYaroslav Tykhiy.Vt mbuf chain
6337b2fd183SYaroslav Tykhiyis freed in this case).
634de06f907SNik Clayton.Sy Note :
6350227791bSRuslan ErmilovIt does not allocate any
6367b2fd183SYaroslav Tykhiy.Vt mbuf clusters ,
6377b2fd183SYaroslav Tykhiyso
638de06f907SNik Clayton.Fa len
639de06f907SNik Claytonmust be less than
640de06f907SNik Clayton.Dv MHLEN .
641de06f907SNik Clayton.\"
642de06f907SNik Clayton.It Fn m_copym mbuf offset len how
6437b2fd183SYaroslav TykhiyMake a copy of an
6447b2fd183SYaroslav Tykhiy.Vt mbuf chain
6457b2fd183SYaroslav Tykhiystarting
646de06f907SNik Clayton.Fa offset
647de06f907SNik Claytonbytes from the beginning, continuing for
648de06f907SNik Clayton.Fa len
649e73145f6SSheldon Hearnbytes.
650e73145f6SSheldon HearnIf
651de06f907SNik Clayton.Fa len
652de06f907SNik Claytonis
653de06f907SNik Clayton.Dv M_COPYALL ,
6547b2fd183SYaroslav Tykhiycopy to the end of the
6557b2fd183SYaroslav Tykhiy.Vt mbuf chain .
656de06f907SNik Clayton.Sy Note :
6577b2fd183SYaroslav TykhiyThe copy is read-only, because the
6587b2fd183SYaroslav Tykhiy.Vt mbuf clusters
6597b2fd183SYaroslav Tykhiyare not copied, only their reference counts are incremented.
660de06f907SNik Clayton.\"
661de06f907SNik Clayton.It Fn m_copypacket mbuf how
662de06f907SNik ClaytonCopy an entire packet including header, which must be present.
663de06f907SNik ClaytonThis is an optimized version of the common case
664de06f907SNik Clayton.Fn m_copym mbuf 0 M_COPYALL how .
665de06f907SNik Clayton.Sy Note :
6667b2fd183SYaroslav Tykhiythe copy is read-only, because the
6677b2fd183SYaroslav Tykhiy.Vt mbuf clusters
6687b2fd183SYaroslav Tykhiyare not copied, only their reference counts are incremented.
669de06f907SNik Clayton.\"
670de06f907SNik Clayton.It Fn m_dup mbuf how
6717b2fd183SYaroslav TykhiyCopy a packet header
6727b2fd183SYaroslav Tykhiy.Vt mbuf chain
6737b2fd183SYaroslav Tykhiyinto a completely new
6747b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
6757b2fd183SYaroslav Tykhiyincluding copying any
6767b2fd183SYaroslav Tykhiy.Vt mbuf clusters .
677e73145f6SSheldon HearnUse this instead of
678de06f907SNik Clayton.Fn m_copypacket
6797b2fd183SYaroslav Tykhiywhen you need a writable copy of an
6807b2fd183SYaroslav Tykhiy.Vt mbuf chain .
681de06f907SNik Clayton.\"
682de06f907SNik Clayton.It Fn m_copydata mbuf offset len buf
6837b2fd183SYaroslav TykhiyCopy data from an
6847b2fd183SYaroslav Tykhiy.Vt mbuf chain
6857b2fd183SYaroslav Tykhiystarting
686de06f907SNik Clayton.Fa off
687de06f907SNik Claytonbytes from the beginning, continuing for
688de06f907SNik Clayton.Fa len
689de06f907SNik Claytonbytes, into the indicated buffer
690de06f907SNik Clayton.Fa buf .
691de06f907SNik Clayton.\"
692de06f907SNik Clayton.It Fn m_copyback mbuf offset len buf
693de06f907SNik ClaytonCopy
694de06f907SNik Clayton.Fa len
695de06f907SNik Claytonbytes from the buffer
696de06f907SNik Clayton.Fa buf
6977b2fd183SYaroslav Tykhiyback into the indicated
6987b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
699de06f907SNik Claytonstarting at
700de06f907SNik Clayton.Fa offset
7017b2fd183SYaroslav Tykhiybytes from the beginning of the
7027b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
7037b2fd183SYaroslav Tykhiyextending the
7047b2fd183SYaroslav Tykhiy.Vt mbuf chain
7057b2fd183SYaroslav Tykhiyif necessary.
706de06f907SNik Clayton.Sy Note :
7070227791bSRuslan ErmilovIt does not allocate any
7087b2fd183SYaroslav Tykhiy.Vt mbuf clusters ,
7097b2fd183SYaroslav Tykhiyjust adds
7107b2fd183SYaroslav Tykhiy.Vt mbufs
7117b2fd183SYaroslav Tykhiyto the
7127b2fd183SYaroslav Tykhiy.Vt mbuf chain .
71336a142c4SRuslan ErmilovIt is safe to set
714de06f907SNik Clayton.Fa offset
7157b2fd183SYaroslav Tykhiybeyond the current
7167b2fd183SYaroslav Tykhiy.Vt mbuf chain
7177b2fd183SYaroslav Tykhiyend: zeroed
7187b2fd183SYaroslav Tykhiy.Vt mbufs
7197b2fd183SYaroslav Tykhiywill be allocated to fill the space.
720de06f907SNik Clayton.\"
7215d96084fSYaroslav Tykhiy.It Fn m_length mbuf last
7227b2fd183SYaroslav TykhiyReturn the length of the
7237b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
7247b2fd183SYaroslav Tykhiyand optionally a pointer to the last
7257b2fd183SYaroslav Tykhiy.Vt mbuf .
7260b1c0994SPoul-Henning Kamp.\"
727da72b998SYaroslav Tykhiy.It Fn m_dup_pkthdr to from how
728da72b998SYaroslav TykhiyUpon the function's completion, the
729da72b998SYaroslav Tykhiy.Vt mbuf
730da72b998SYaroslav Tykhiy.Fa to
731da72b998SYaroslav Tykhiywill contain an identical copy of
732da72b998SYaroslav Tykhiy.Fa from->m_pkthdr
733da72b998SYaroslav Tykhiyand the per-packet attributes found in the
734da72b998SYaroslav Tykhiy.Vt mbuf chain
735da72b998SYaroslav Tykhiy.Fa from .
736da72b998SYaroslav TykhiyThe
737da72b998SYaroslav Tykhiy.Vt mbuf
738da72b998SYaroslav Tykhiy.Fa from
739da72b998SYaroslav Tykhiymust have the flag
740da72b998SYaroslav Tykhiy.Dv M_PKTHDR
741da72b998SYaroslav Tykhiyinitially set, and
742da72b998SYaroslav Tykhiy.Fa to
743da72b998SYaroslav Tykhiymust be empty on entry.
744da72b998SYaroslav Tykhiy.\"
745da72b998SYaroslav Tykhiy.It Fn m_move_pkthdr to from
746da72b998SYaroslav TykhiyMove
747da72b998SYaroslav Tykhiy.Va m_pkthdr
748da72b998SYaroslav Tykhiyand the per-packet attributes from the
749da72b998SYaroslav Tykhiy.Vt mbuf chain
750da72b998SYaroslav Tykhiy.Fa from
751da72b998SYaroslav Tykhiyto the
752da72b998SYaroslav Tykhiy.Vt mbuf
753da72b998SYaroslav Tykhiy.Fa to .
754da72b998SYaroslav TykhiyThe
755da72b998SYaroslav Tykhiy.Vt mbuf
756da72b998SYaroslav Tykhiy.Fa from
757da72b998SYaroslav Tykhiymust have the flag
758da72b998SYaroslav Tykhiy.Dv M_PKTHDR
759da72b998SYaroslav Tykhiyinitially set, and
760da72b998SYaroslav Tykhiy.Fa to
761da72b998SYaroslav Tykhiymust be empty on entry.
762da72b998SYaroslav TykhiyUpon the function's completion,
763da72b998SYaroslav Tykhiy.Fa from
764da72b998SYaroslav Tykhiywill have the flag
765da72b998SYaroslav Tykhiy.Dv M_PKTHDR
766da72b998SYaroslav Tykhiyand the per-packet attributes cleared.
767da72b998SYaroslav Tykhiy.\"
7685d96084fSYaroslav Tykhiy.It Fn m_fixhdr mbuf
7697b2fd183SYaroslav TykhiySet the packet-header length to the length of the
7707b2fd183SYaroslav Tykhiy.Vt mbuf chain .
7710b1c0994SPoul-Henning Kamp.\"
772de06f907SNik Clayton.It Fn m_devget buf len offset ifp copy
773de06f907SNik ClaytonCopy data from a device local memory pointed to by
774de06f907SNik Clayton.Fa buf
7757b2fd183SYaroslav Tykhiyto an
7767b2fd183SYaroslav Tykhiy.Vt mbuf chain .
777e73145f6SSheldon HearnThe copy is done using a specified copy routine
778de06f907SNik Clayton.Fa copy ,
779de06f907SNik Claytonor
780de06f907SNik Clayton.Fn bcopy
781de06f907SNik Claytonif
782de06f907SNik Clayton.Fa copy
783de06f907SNik Claytonis
784de06f907SNik Clayton.Dv NULL .
785de06f907SNik Clayton.\"
786de06f907SNik Clayton.It Fn m_cat m n
787de06f907SNik ClaytonConcatenate
788de06f907SNik Clayton.Fa n
789de06f907SNik Claytonto
790de06f907SNik Clayton.Fa m .
7917b2fd183SYaroslav TykhiyBoth
7927b2fd183SYaroslav Tykhiy.Vt mbuf chains
7937b2fd183SYaroslav Tykhiymust be of the same type.
794de06f907SNik Clayton.Fa N
795de06f907SNik Claytonis still valid after the function returned.
796de06f907SNik Clayton.Sy Note :
797de06f907SNik ClaytonIt does not handle
798de06f907SNik Clayton.Dv M_PKTHDR
799de06f907SNik Claytonand friends.
800de06f907SNik Clayton.\"
801de06f907SNik Clayton.It Fn m_split mbuf len how
8027b2fd183SYaroslav TykhiyPartition an
8037b2fd183SYaroslav Tykhiy.Vt mbuf chain
8047b2fd183SYaroslav Tykhiyin two pieces, returning the tail:
805de06f907SNik Claytonall but the first
806de06f907SNik Clayton.Fa len
807e73145f6SSheldon Hearnbytes.
808e73145f6SSheldon HearnIn case of failure, it returns
809de06f907SNik Clayton.Dv NULL
8107b2fd183SYaroslav Tykhiyand attempts to restore the
8117b2fd183SYaroslav Tykhiy.Vt mbuf chain
8127b2fd183SYaroslav Tykhiyto its original state.
81302312219SBruce M Simpson.\"
81402312219SBruce M Simpson.It Fn m_apply mbuf off len f arg
81502312219SBruce M SimpsonApply a function to an
81602312219SBruce M Simpson.Vt mbuf chain ,
81702312219SBruce M Simpsonat offset
81802312219SBruce M Simpson.Fa off ,
81902312219SBruce M Simpsonfor length
820f48cbcdeSRuslan Ermilov.Fa len
821f48cbcdeSRuslan Ermilovbytes.
82202312219SBruce M SimpsonTypically used to avoid calls to
82302312219SBruce M Simpson.Fn m_pullup
82402312219SBruce M Simpsonwhich would otherwise be unnecessary or undesirable.
82502312219SBruce M Simpson.Fa arg
82602312219SBruce M Simpsonis a convenience argument which is passed to the callback function
82702312219SBruce M Simpson.Fa f .
82802312219SBruce M Simpson.Pp
82902312219SBruce M SimpsonEach time
83002312219SBruce M Simpson.Fn f
83102312219SBruce M Simpsonis called, it will be passed
83202312219SBruce M Simpson.Fa arg ,
83302312219SBruce M Simpsona pointer to the
83402312219SBruce M Simpson.Fa data
83502312219SBruce M Simpsonin the current mbuf, and the length
83602312219SBruce M Simpson.Fa len
83702312219SBruce M Simpsonof the data in this mbuf to which the function should be applied.
83802312219SBruce M Simpson.Pp
83902312219SBruce M SimpsonThe function should return zero to indicate success;
84002312219SBruce M Simpsonotherwise, if an error is indicated, then
84102312219SBruce M Simpson.Fn m_apply
84202312219SBruce M Simpsonwill return the error and stop iterating through the
84302312219SBruce M Simpson.Vt mbuf chain .
84402312219SBruce M Simpson.\"
84502312219SBruce M Simpson.It Fn m_getptr mbuf loc off
84602312219SBruce M SimpsonReturn a pointer to the mbuf containing the data located at
84702312219SBruce M Simpson.Fa loc
84802312219SBruce M Simpsonbytes from the beginning of the
84902312219SBruce M Simpson.Vt mbuf chain .
85002312219SBruce M SimpsonThe corresponding offset into the mbuf will be stored in
85102312219SBruce M Simpson.Fa *off .
852a61bc764SMike Silbersack.It Fn m_defrag m0 how
853f48cbcdeSRuslan ErmilovDefragment an mbuf chain, returning the shortest possible
854bdf86185SMike Silbersackchain of mbufs and clusters.
855bdf86185SMike SilbersackIf allocation fails and this can not be completed,
856bdf86185SMike Silbersack.Dv NULL
857bdf86185SMike Silbersackwill be returned and the original chain will be unchanged.
858bdf86185SMike SilbersackUpon success, the original chain will be freed and the new
859bdf86185SMike Silbersackchain will be returned.
860a61bc764SMike Silbersack.Fa how
861a61bc764SMike Silbersackshould be either
862a61bc764SMike Silbersack.Dv M_TRYWAIT
863a61bc764SMike Silbersackor
864a61bc764SMike Silbersack.Dv M_DONTWAIT ,
865a61bc764SMike Silbersackdepending on the caller's preference.
866a61bc764SMike Silbersack.Pp
867a61bc764SMike SilbersackThis function is especially useful in network drivers, where
868a61bc764SMike Silbersackcertain long mbuf chains must be shortened before being added
869a61bc764SMike Silbersackto TX descriptor lists.
8703136363fSRuslan Ermilov.El
8710193eb5eSYaroslav Tykhiy.Sh HARDWARE-ASSISTED CHECKSUM CALCULATION
8720193eb5eSYaroslav TykhiyThis section currently applies to TCP/IP only.
8730193eb5eSYaroslav TykhiyIn order to save the host CPU resources, computing checksums is
8740193eb5eSYaroslav Tykhiyoffloaded to the network interface hardware if possible.
8750193eb5eSYaroslav TykhiyThe
8760193eb5eSYaroslav Tykhiy.Va m_pkthdr
8770193eb5eSYaroslav Tykhiymember of the leading
8780193eb5eSYaroslav Tykhiy.Vt mbuf
8790193eb5eSYaroslav Tykhiyof a packet contains two fields used for that purpose,
8800193eb5eSYaroslav Tykhiy.Vt int Va csum_flags
8810193eb5eSYaroslav Tykhiyand
8820193eb5eSYaroslav Tykhiy.Vt int Va csum_data .
8830193eb5eSYaroslav TykhiyThe meaning of those fields depends on the direction a packet flows in,
8840193eb5eSYaroslav Tykhiyand on whether the packet is fragmented.
8850193eb5eSYaroslav TykhiyHenceforth,
8860193eb5eSYaroslav Tykhiy.Va csum_flags
8870193eb5eSYaroslav Tykhiyor
8880193eb5eSYaroslav Tykhiy.Va csum_data
8890193eb5eSYaroslav Tykhiyof a packet
8900193eb5eSYaroslav Tykhiywill denote the corresponding field of the
8910193eb5eSYaroslav Tykhiy.Va m_pkthdr
8920193eb5eSYaroslav Tykhiymember of the leading
8930193eb5eSYaroslav Tykhiy.Vt mbuf
8940193eb5eSYaroslav Tykhiyin the
8950193eb5eSYaroslav Tykhiy.Vt mbuf chain
8960193eb5eSYaroslav Tykhiycontaining the packet.
8970193eb5eSYaroslav Tykhiy.Pp
8980193eb5eSYaroslav TykhiyOn output, checksum offloading is attempted after the outgoing
8990193eb5eSYaroslav Tykhiyinterface has been determined for a packet.
9000193eb5eSYaroslav TykhiyThe interface-specific field
9010193eb5eSYaroslav Tykhiy.Va ifnet.if_data.ifi_hwassist
9020193eb5eSYaroslav Tykhiy(see
9030193eb5eSYaroslav Tykhiy.Xr ifnet 9 )
9040193eb5eSYaroslav Tykhiyis consulted for the capabilities of the interface to assist in
9050193eb5eSYaroslav Tykhiycomputing checksums.
9060193eb5eSYaroslav TykhiyThe
9070193eb5eSYaroslav Tykhiy.Va csum_flags
908f48cbcdeSRuslan Ermilovfield of the packet header is set to indicate which actions the interface
9090193eb5eSYaroslav Tykhiyis supposed to perform on it.
9100193eb5eSYaroslav TykhiyThe actions unsupported by the network interface are done in the
9110193eb5eSYaroslav Tykhiysoftware prior to passing the packet down to the interface driver;
9120193eb5eSYaroslav Tykhiysuch actions will never be requested through
9130193eb5eSYaroslav Tykhiy.Va csum_flags .
9140193eb5eSYaroslav Tykhiy.Pp
9150193eb5eSYaroslav TykhiyThe flags demanding a particular action from an interface are as follows:
9160193eb5eSYaroslav Tykhiy.Bl -tag -width ".Dv CSUM_TCP" -offset indent
9170193eb5eSYaroslav Tykhiy.It Dv CSUM_IP
9180193eb5eSYaroslav TykhiyThe IP header checksum is to be computed and stored in the
9190193eb5eSYaroslav Tykhiycorresponding field of the packet.
9200193eb5eSYaroslav TykhiyThe hardware is expected to know the format of an IP header
9210193eb5eSYaroslav Tykhiyto determine the offset of the IP checksum field.
9220193eb5eSYaroslav Tykhiy.It Dv CSUM_TCP
923f48cbcdeSRuslan ErmilovThe TCP checksum is to be computed.
924f48cbcdeSRuslan Ermilov(See below.)
9250193eb5eSYaroslav Tykhiy.It Dv CSUM_UDP
926f48cbcdeSRuslan ErmilovThe UDP checksum is to be computed.
927f48cbcdeSRuslan Ermilov(See below.)
9280193eb5eSYaroslav Tykhiy.El
9290193eb5eSYaroslav Tykhiy.Pp
930fbde705aSYaroslav TykhiyShould a TCP or UDP checksum be offloaded to the hardware,
9310193eb5eSYaroslav Tykhiythe field
9320193eb5eSYaroslav Tykhiy.Va csum_data
9330193eb5eSYaroslav Tykhiywill contain the byte offset of the checksum field relative to the
9340193eb5eSYaroslav Tykhiyend of the IP header.
9350193eb5eSYaroslav TykhiyIn this case, the checksum field will be initially
9360193eb5eSYaroslav Tykhiyset by the TCP/IP module to the checksum of the pseudo header
937fbde705aSYaroslav Tykhiydefined by the TCP and UDP specifications.
9380193eb5eSYaroslav Tykhiy.Pp
9390193eb5eSYaroslav TykhiyFor outbound packets which have been fragmented
9400193eb5eSYaroslav Tykhiyby the host CPU, the following will also be true,
9410193eb5eSYaroslav Tykhiyregardless of the checksum flag settings:
9420193eb5eSYaroslav Tykhiy.Bl -bullet -offset indent
9430193eb5eSYaroslav Tykhiy.It
9440193eb5eSYaroslav Tykhiyall fragments will have the flag
9450193eb5eSYaroslav Tykhiy.Dv M_FRAG
9460193eb5eSYaroslav Tykhiyset in their
9470193eb5eSYaroslav Tykhiy.Va m_flags
9480193eb5eSYaroslav Tykhiyfield;
9490193eb5eSYaroslav Tykhiy.It
9500193eb5eSYaroslav Tykhiythe first and the last fragments in the chain will have
9510193eb5eSYaroslav Tykhiy.Dv M_FIRSTFRAG
9520193eb5eSYaroslav Tykhiyor
9530193eb5eSYaroslav Tykhiy.Dv M_LASTFRAG
9540193eb5eSYaroslav Tykhiyset in their
9550193eb5eSYaroslav Tykhiy.Va m_flags ,
9560193eb5eSYaroslav Tykhiycorrespondingly;
9570193eb5eSYaroslav Tykhiy.It
9580193eb5eSYaroslav Tykhiythe first fragment in the chain will have the total number
9590193eb5eSYaroslav Tykhiyof fragments contained in its
9600193eb5eSYaroslav Tykhiy.Va csum_data
9610193eb5eSYaroslav Tykhiyfield.
9620193eb5eSYaroslav Tykhiy.El
9630193eb5eSYaroslav Tykhiy.Pp
96476f80df6SYaroslav TykhiyThe last rule for fragmented packets takes precedence over the one
96576f80df6SYaroslav Tykhiyfor a TCP or UDP checksum.
96676f80df6SYaroslav TykhiyNevertheless, offloading a TCP or UDP checksum is possible for a
96776f80df6SYaroslav Tykhiyfragmented packet if the flag
96876f80df6SYaroslav Tykhiy.Dv CSUM_IP_FRAGS
96976f80df6SYaroslav Tykhiyis set in the field
97076f80df6SYaroslav Tykhiy.Va ifnet.if_data.ifi_hwassist
97176f80df6SYaroslav Tykhiyassociated with the network interface.
97276f80df6SYaroslav TykhiyHowever, in this case the interface is expected to figure out
97376f80df6SYaroslav Tykhiythe location of the checksum field within the sequence of fragments
97476f80df6SYaroslav Tykhiyby itself because
9750193eb5eSYaroslav Tykhiy.Va csum_data
97676f80df6SYaroslav Tykhiycontains a fragment count instead of a checksum offset value.
9770193eb5eSYaroslav Tykhiy.Pp
9780193eb5eSYaroslav TykhiyOn input, an interface indicates the actions it has performed
9790193eb5eSYaroslav Tykhiyon a packet by setting one or more of the following flags in
9800193eb5eSYaroslav Tykhiy.Va csum_flags
9810193eb5eSYaroslav Tykhiyassociated with the packet:
9820193eb5eSYaroslav Tykhiy.Bl -tag -width ".Dv CSUM_IP_CHECKED" -offset indent
9830193eb5eSYaroslav Tykhiy.It Dv CSUM_IP_CHECKED
9840193eb5eSYaroslav TykhiyThe IP header checksum has been computed.
9850193eb5eSYaroslav Tykhiy.It Dv CSUM_IP_VALID
9860193eb5eSYaroslav TykhiyThe IP header has a valid checksum.
9870193eb5eSYaroslav TykhiyThis flag can appear only in combination with
9880193eb5eSYaroslav Tykhiy.Dv CSUM_IP_CHECKED .
9890193eb5eSYaroslav Tykhiy.It Dv CSUM_DATA_VALID
9900193eb5eSYaroslav TykhiyThe checksum of the data portion of the IP packet has been computed
9910193eb5eSYaroslav Tykhiyand stored in the field
9920193eb5eSYaroslav Tykhiy.Va csum_data
9930193eb5eSYaroslav Tykhiyin network byte order.
9940193eb5eSYaroslav Tykhiy.It Dv CSUM_PSEUDO_HDR
9950193eb5eSYaroslav TykhiyCan be set only along with
9960193eb5eSYaroslav Tykhiy.Dv CSUM_DATA_VALID
9970193eb5eSYaroslav Tykhiyto indicate that the IP data checksum found in
9980193eb5eSYaroslav Tykhiy.Va csum_data
999fbde705aSYaroslav Tykhiyallows for the pseudo header defined by the TCP and UDP specifications.
10000193eb5eSYaroslav TykhiyOtherwise the checksum of the pseudo header must be calculated by
10010193eb5eSYaroslav Tykhiythe host CPU and added to
10020193eb5eSYaroslav Tykhiy.Va csum_data
1003fbde705aSYaroslav Tykhiyto obtain the final checksum to be used for TCP or UDP validation purposes.
10040193eb5eSYaroslav Tykhiy.El
10050193eb5eSYaroslav Tykhiy.Pp
10060193eb5eSYaroslav TykhiyIf a particular network interface just indicates success or
10070193eb5eSYaroslav Tykhiyfailure of TCP or UDP checksum validation without returning
10080193eb5eSYaroslav Tykhiythe exact value of the checksum to the host CPU, its driver can mark
10090193eb5eSYaroslav Tykhiy.Dv CSUM_DATA_VALID
10100193eb5eSYaroslav Tykhiyand
10110193eb5eSYaroslav Tykhiy.Dv CSUM_PSEUDO_HDR
10120193eb5eSYaroslav Tykhiyin
10130193eb5eSYaroslav Tykhiy.Va csum_flags ,
10140193eb5eSYaroslav Tykhiyand set
10150193eb5eSYaroslav Tykhiy.Va csum_data
10160193eb5eSYaroslav Tykhiyto
10170193eb5eSYaroslav Tykhiy.Li 0xFFFF
10180193eb5eSYaroslav Tykhiyhexadecimal to indicate a valid checksum.
1019f48cbcdeSRuslan ErmilovIt is a peculiarity of the algorithm used that the Internet checksum
10200193eb5eSYaroslav Tykhiycalculated over any valid packet will be
10210193eb5eSYaroslav Tykhiy.Li 0xFFFF
10220193eb5eSYaroslav Tykhiyas long as the original checksum field is included.
10230193eb5eSYaroslav Tykhiy.Pp
10240193eb5eSYaroslav TykhiyFor inbound packets which are IP fragments, all
10250193eb5eSYaroslav Tykhiy.Va csum_data
10260193eb5eSYaroslav Tykhiyfields will be summed during reassembly to obtain the final checksum
10270193eb5eSYaroslav Tykhiyvalue passed to an upper layer in the
10280193eb5eSYaroslav Tykhiy.Va csum_data
10290193eb5eSYaroslav Tykhiyfield of the reassembled packet.
10300193eb5eSYaroslav TykhiyThe
10310193eb5eSYaroslav Tykhiy.Va csum_flags
10320193eb5eSYaroslav Tykhiyfields of all fragments will be consolidated using logical AND
10330193eb5eSYaroslav Tykhiyto obtain the final value for
10340193eb5eSYaroslav Tykhiy.Va csum_flags .
10350193eb5eSYaroslav TykhiyThus, in order to successfully
10360193eb5eSYaroslav Tykhiyoffload checksum computation for fragmented data,
10370193eb5eSYaroslav Tykhiyall fragments should have the same value of
10380193eb5eSYaroslav Tykhiy.Va csum_flags .
10393cab047eSMike Silbersack.Sh STRESS TESTING
1040c8185672SRuslan ErmilovWhen running a kernel compiled with the option
1041c8185672SRuslan Ermilov.Dv MBUF_STRESS_TEST ,
1042c8185672SRuslan Ermilovthe following
1043c8185672SRuslan Ermilov.Xr sysctl 8 Ns
1044c8185672SRuslan Ermilov-controlled options may be used to create
10453cab047eSMike Silbersackvarious failure/extreme cases for testing of network drivers
10467b2fd183SYaroslav Tykhiyand other parts of the kernel that rely on
10477b2fd183SYaroslav Tykhiy.Vt mbufs .
1048c8185672SRuslan Ermilov.Bl -tag -width ident
1049c8185672SRuslan Ermilov.It Va net.inet.ip.mbuf_frag_size
1050c8185672SRuslan ErmilovCauses
1051c8185672SRuslan Ermilov.Fn ip_output
10527b2fd183SYaroslav Tykhiyto fragment outgoing
10537b2fd183SYaroslav Tykhiy.Vt mbuf chains
10547b2fd183SYaroslav Tykhiyinto fragments of the specified size.
1055c8185672SRuslan ErmilovSetting this variable to 1 is an excellent way to
10567b2fd183SYaroslav Tykhiytest the long
10577b2fd183SYaroslav Tykhiy.Vt mbuf chain
10587b2fd183SYaroslav Tykhiyhandling ability of network drivers.
1059c8185672SRuslan Ermilov.It Va kern.ipc.m_defragrandomfailures
1060c8185672SRuslan ErmilovCauses the function
1061c8185672SRuslan Ermilov.Fn m_defrag
1062c8185672SRuslan Ermilovto randomly fail, returning
1063c8185672SRuslan Ermilov.Dv NULL .
1064c8185672SRuslan ErmilovAny piece of code which uses
1065c8185672SRuslan Ermilov.Fn m_defrag
1066c8185672SRuslan Ermilovshould be tested with this feature.
1067c8185672SRuslan Ermilov.El
1068de06f907SNik Clayton.Sh RETURN VALUES
1069de06f907SNik ClaytonSee above.
10700193eb5eSYaroslav Tykhiy.Sh SEE ALSO
1071e85cc3f5SRuslan Ermilov.Xr ifnet 9 ,
1072e85cc3f5SRuslan Ermilov.Xr mbuf_tags 9
1073de06f907SNik Clayton.Sh HISTORY
1074de06f907SNik Clayton.\" Please correct me if I'm wrong
10757b2fd183SYaroslav Tykhiy.Vt Mbufs
10767b2fd183SYaroslav Tykhiyappeared in an early version of
1077753d686dSRuslan Ermilov.Bx .
107853b2520dSTom RhodesBesides being used for network packets, they were used
1079bfd59721SBosko Milekicto store various dynamic structures, such as routing table
1080bfd59721SBosko Milekicentries, interface addresses, protocol control blocks, etc.
1081de06f907SNik Clayton.Sh AUTHORS
10826575e6daSRuslan ErmilovThe original
10836575e6daSRuslan Ermilov.Nm
10846575e6daSRuslan Ermilovman page was written by Yar Tikhiy.
1085