xref: /freebsd/share/man/man9/mbuf.9 (revision f48cbcdecc184a010a859a59e965fa7a33880c8b)
1de06f907SNik Clayton.\" Copyright (c) 2000 FreeBSD Inc.
2de06f907SNik Clayton.\" All rights reserved.
3de06f907SNik Clayton.\"
4de06f907SNik Clayton.\" Redistribution and use in source and binary forms, with or without
5de06f907SNik Clayton.\" modification, are permitted provided that the following conditions
6de06f907SNik Clayton.\" are met:
7de06f907SNik Clayton.\" 1. Redistributions of source code must retain the above copyright
8de06f907SNik Clayton.\"    notice, this list of conditions and the following disclaimer.
9de06f907SNik Clayton.\" 2. Redistributions in binary form must reproduce the above copyright
10de06f907SNik Clayton.\"    notice, this list of conditions and the following disclaimer in the
11de06f907SNik Clayton.\"    documentation and/or other materials provided with the distribution.
12de06f907SNik Clayton.\"
13de06f907SNik Clayton.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14de06f907SNik Clayton.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15de06f907SNik Clayton.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16de06f907SNik Clayton.\" ARE DISCLAIMED.  IN NO EVENT SHALL [your name] OR CONTRIBUTORS BE LIABLE
17de06f907SNik Clayton.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18de06f907SNik Clayton.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19de06f907SNik Clayton.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20de06f907SNik Clayton.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21de06f907SNik Clayton.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22de06f907SNik Clayton.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23de06f907SNik Clayton.\" SUCH DAMAGE.
24de06f907SNik Clayton.\"
25de06f907SNik Clayton.\" $FreeBSD$
26de06f907SNik Clayton.\"
27de06f907SNik Clayton.Dd October 17, 2000
28de06f907SNik Clayton.Dt MBUF 9
29de06f907SNik Clayton.Os
30de06f907SNik Clayton.\"
31de06f907SNik Clayton.Sh NAME
32de06f907SNik Clayton.Nm mbuf
33de06f907SNik Clayton.Nd "memory management in the kernel IPC subsystem"
34de06f907SNik Clayton.\"
35de06f907SNik Clayton.Sh SYNOPSIS
3632eef9aeSRuslan Ermilov.In sys/param.h
37f16b3c0dSChad David.In sys/systm.h
3832eef9aeSRuslan Ermilov.In sys/mbuf.h
39de06f907SNik Clayton.\"
40bfd59721SBosko Milekic.Ss Mbuf allocation macros
41de06f907SNik Clayton.Fn MGET "struct mbuf *mbuf" "int how" "short type"
42de06f907SNik Clayton.Fn MGETHDR "struct mbuf *mbuf" "int how" "short type"
43de06f907SNik Clayton.Fn MCLGET "struct mbuf *mbuf" "int how"
44bfd59721SBosko Milekic.Fo MEXTADD
45bfd59721SBosko Milekic.Fa "struct mbuf *mbuf"
46bfd59721SBosko Milekic.Fa "caddr_t buf"
47bfd59721SBosko Milekic.Fa "u_int size"
48bfd59721SBosko Milekic.Fa "void (*free)(void *opt_args)"
49bfd59721SBosko Milekic.Fa "void *opt_args"
505d4050ccSBosko Milekic.Fa "short flags"
515d4050ccSBosko Milekic.Fa "int type"
52bfd59721SBosko Milekic.Fc
53bfd59721SBosko Milekic.Fn MEXTFREE "struct mbuf *mbuf"
54bfd59721SBosko Milekic.Fn MEXT_ADD_REF "struct mbuf *mbuf"
55bfd59721SBosko Milekic.Fn MEXT_REM_REF "struct mbuf *mbuf"
56bfd59721SBosko Milekic.Fn MFREE "struct mbuf *mbuf" "struct mbuf *successor"
57de06f907SNik Clayton.\"
58bfd59721SBosko Milekic.Ss Mbuf utility macros
59f16b3c0dSChad David.Fn mtod "struct mbuf *mbuf" "type"
60f16b3c0dSChad David.Ft int
61bfd59721SBosko Milekic.Fn MEXT_IS_REF "struct mbuf *mbuf"
62bfd59721SBosko Milekic.Fn M_ALIGN "struct mbuf *mbuf" "u_int len"
63bfd59721SBosko Milekic.Fn MH_ALIGN "struct mbuf *mbuf" "u_int len"
64f16b3c0dSChad David.Ft int
65bfd59721SBosko Milekic.Fn M_LEADINGSPACE "struct mbuf *mbuf"
66f16b3c0dSChad David.Ft int
67bfd59721SBosko Milekic.Fn M_TRAILINGSPACE "struct mbuf *mbuf"
68da72b998SYaroslav Tykhiy.Fn M_MOVE_PKTHDR "struct mbuf *to" "struct mbuf *from"
69bfd59721SBosko Milekic.Fn M_PREPEND "struct mbuf *mbuf" "int len" "int how"
70bfd59721SBosko Milekic.Fn MCHTYPE "struct mbuf *mbuf" "u_int type"
71f16b3c0dSChad David.Ft int
725d4050ccSBosko Milekic.Fn M_WRITABLE "struct mbuf *mbuf"
73bfd59721SBosko Milekic.\"
74bfd59721SBosko Milekic.Ss Mbuf allocation functions
75de06f907SNik Clayton.Ft struct mbuf *
76de06f907SNik Clayton.Fn m_get "int how" "int type"
77de06f907SNik Clayton.Ft struct mbuf *
78328c0bbcSBosko Milekic.Fn m_getm "struct mbuf *orig" "int len" "int how" "int type"
79328c0bbcSBosko Milekic.Ft struct mbuf *
80de06f907SNik Clayton.Fn m_getclr "int how" "int type"
81de06f907SNik Clayton.Ft struct mbuf *
82de06f907SNik Clayton.Fn m_gethdr "int how" "int type"
83bfd59721SBosko Milekic.Ft struct mbuf *
84bfd59721SBosko Milekic.Fn m_free "struct mbuf *mbuf"
85de06f907SNik Clayton.Ft void
86de06f907SNik Clayton.Fn m_freem "struct mbuf *mbuf"
87bfd59721SBosko Milekic.\"
88bfd59721SBosko Milekic.Ss Mbuf utility functions
89de06f907SNik Clayton.Ft void
90de06f907SNik Clayton.Fn m_adj "struct mbuf *mbuf" "int len"
91de06f907SNik Clayton.Ft struct mbuf *
92de06f907SNik Clayton.Fn m_prepend "struct mbuf *mbuf" "int len" "int how"
93de06f907SNik Clayton.Ft struct mbuf *
94de06f907SNik Clayton.Fn m_pullup "struct mbuf *mbuf" "int len"
95de06f907SNik Clayton.Ft struct mbuf *
96de06f907SNik Clayton.Fn m_copym "struct mbuf *mbuf" "int offset" "int len" "int how"
97de06f907SNik Clayton.Ft struct mbuf *
98de06f907SNik Clayton.Fn m_copypacket "struct mbuf *mbuf" "int how"
99de06f907SNik Clayton.Ft struct mbuf *
100de06f907SNik Clayton.Fn m_dup "struct mbuf *mbuf" "int how"
101de06f907SNik Clayton.Ft void
1020d8e4c57SBruce Evans.Fn m_copydata "const struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
103de06f907SNik Clayton.Ft void
104de06f907SNik Clayton.Fn m_copyback "struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
105de06f907SNik Clayton.Ft struct mbuf *
106de06f907SNik Clayton.Fo m_devget
107de06f907SNik Clayton.Fa "char *buf"
108de06f907SNik Clayton.Fa "int len"
109de06f907SNik Clayton.Fa "int offset"
110de06f907SNik Clayton.Fa "struct ifnet *ifp"
111de06f907SNik Clayton.Fa "void (*copy)(char *from, caddr_t to, u_int len)"
112de06f907SNik Clayton.Fc
113de06f907SNik Clayton.Ft void
114de06f907SNik Clayton.Fn m_cat "struct mbuf *m" "struct mbuf *n"
115e60fa837SRuslan Ermilov.Ft u_int
1160b1c0994SPoul-Henning Kamp.Fn m_fixhdr "struct mbuf *mbuf"
117da72b998SYaroslav Tykhiy.Ft void
118da72b998SYaroslav Tykhiy.Fn m_dup_pkthdr "struct mbuf *to" "struct mbuf *from"
119da72b998SYaroslav Tykhiy.Ft void
120da72b998SYaroslav Tykhiy.Fn m_move_pkthdr "struct mbuf *to" "struct mbuf *from"
121e60fa837SRuslan Ermilov.Ft u_int
1220b1c0994SPoul-Henning Kamp.Fn m_length "struct mbuf *mbuf" "struct mbuf **last"
123de06f907SNik Clayton.Ft struct mbuf *
124de06f907SNik Clayton.Fn m_split "struct mbuf *mbuf" "int len" "int how"
12502312219SBruce M Simpson.Ft int
12602312219SBruce M Simpson.Fn m_apply "struct mbuf *mbuf" "int off" "int len" "int (*f)(void *arg, void *data, u_int len)" "void *arg"
12702312219SBruce M Simpson.Ft struct mbuf *
12802312219SBruce M Simpson.Fn m_getptr "struct mbuf *mbuf" "int loc" "int *off"
129a61bc764SMike Silbersack.Ft struct mbuf *
130a61bc764SMike Silbersack.Fn m_defrag "struct mbuf *m0" "int how"
131de06f907SNik Clayton.\"
132de06f907SNik Clayton.Sh DESCRIPTION
1337b2fd183SYaroslav TykhiyAn
1347b2fd183SYaroslav Tykhiy.Vt mbuf
1357b2fd183SYaroslav Tykhiyis a basic unit of memory management in the kernel IPC subsystem.
1367b2fd183SYaroslav TykhiyNetwork packets and socket buffers are stored in
1377b2fd183SYaroslav Tykhiy.Vt mbufs .
1387b2fd183SYaroslav TykhiyA network packet may span multiple
1397b2fd183SYaroslav Tykhiy.Vt mbufs
1407b2fd183SYaroslav Tykhiyarranged into a
1417b2fd183SYaroslav Tykhiy.Vt mbuf chain
142c4d9468eSRuslan Ermilov(linked list),
143de06f907SNik Claytonwhich allows adding or trimming
144bfd59721SBosko Milekicnetwork headers with little overhead.
145de06f907SNik Clayton.Pp
1467b2fd183SYaroslav TykhiyWhile a developer should not bother with
1477b2fd183SYaroslav Tykhiy.Vt mbuf
1487b2fd183SYaroslav Tykhiyinternals without serious
149de06f907SNik Claytonreason in order to avoid incompatibilities with future changes, it
1507b2fd183SYaroslav Tykhiyis useful to understand the general structure of an
1517b2fd183SYaroslav Tykhiy.Vt mbuf .
152de06f907SNik Clayton.Pp
1537b2fd183SYaroslav TykhiyAn
1547b2fd183SYaroslav Tykhiy.Vt mbuf
1557b2fd183SYaroslav Tykhiyconsists of a variable-sized header and a small internal
156e73145f6SSheldon Hearnbuffer for data.
1577b2fd183SYaroslav TykhiyThe total size of an
1587b2fd183SYaroslav Tykhiy.Vt mbuf ,
159bfd59721SBosko Milekic.Dv MSIZE ,
160feccf50cSAlex Dupreis a constant defined in
161feccf50cSAlex Dupre.In sys/param.h .
1627b2fd183SYaroslav TykhiyThe
1637b2fd183SYaroslav Tykhiy.Vt mbuf
1647b2fd183SYaroslav Tykhiyheader includes:
165de06f907SNik Clayton.Pp
166238d9c59SYaroslav Tykhiy.Bl -tag -width "m_nextpkt" -offset indent
167be82a0bcSYaroslav Tykhiy.It Va m_next
168238d9c59SYaroslav Tykhiy.Pq Vt struct mbuf *
169238d9c59SYaroslav TykhiyA pointer to the next
1707b2fd183SYaroslav Tykhiy.Vt mbuf
1717b2fd183SYaroslav Tykhiyin the
172238d9c59SYaroslav Tykhiy.Vt mbuf chain .
173be82a0bcSYaroslav Tykhiy.It Va m_nextpkt
174238d9c59SYaroslav Tykhiy.Pq Vt struct mbuf *
175238d9c59SYaroslav TykhiyA pointer to the next
1767b2fd183SYaroslav Tykhiy.Vt mbuf chain
177238d9c59SYaroslav Tykhiyin the queue.
178be82a0bcSYaroslav Tykhiy.It Va m_data
179238d9c59SYaroslav Tykhiy.Pq Vt caddr_t
180238d9c59SYaroslav TykhiyA pointer to data attached to this
181238d9c59SYaroslav Tykhiy.Vt mbuf .
182be82a0bcSYaroslav Tykhiy.It Va m_len
183238d9c59SYaroslav Tykhiy.Pq Vt int
184238d9c59SYaroslav TykhiyThe length of the data.
185be82a0bcSYaroslav Tykhiy.It Va m_type
186238d9c59SYaroslav Tykhiy.Pq Vt short
187238d9c59SYaroslav TykhiyThe type of the data.
188be82a0bcSYaroslav Tykhiy.It Va m_flags
189238d9c59SYaroslav Tykhiy.Pq Vt int
190238d9c59SYaroslav TykhiyThe
1917b2fd183SYaroslav Tykhiy.Vt mbuf
192238d9c59SYaroslav Tykhiyflags.
193de06f907SNik Clayton.El
194de06f907SNik Clayton.Pp
1957b2fd183SYaroslav TykhiyThe
1967b2fd183SYaroslav Tykhiy.Vt mbuf
1977b2fd183SYaroslav Tykhiyflag bits are defined as follows:
198de06f907SNik Clayton.Bd -literal
199de06f907SNik Clayton/* mbuf flags */
200de06f907SNik Clayton#define	M_EXT		0x0001	/* has associated external storage */
201de06f907SNik Clayton#define	M_PKTHDR	0x0002	/* start of record */
202de06f907SNik Clayton#define	M_EOR		0x0004	/* end of record */
2035d4050ccSBosko Milekic#define	M_RDONLY	0x0008	/* associated data marked read-only */
2045d4050ccSBosko Milekic#define	M_PROTO1	0x0010	/* protocol-specific */
2055d4050ccSBosko Milekic#define	M_PROTO2	0x0020 	/* protocol-specific */
2065d4050ccSBosko Milekic#define	M_PROTO3	0x0040	/* protocol-specific */
2075d4050ccSBosko Milekic#define	M_PROTO4	0x0080	/* protocol-specific */
2085d4050ccSBosko Milekic#define	M_PROTO5	0x0100	/* protocol-specific */
209f434362aSYaroslav Tykhiy#define	M_PROTO6	0x4000	/* protocol-specific (avoid M_BCAST conflict) */
210f434362aSYaroslav Tykhiy#define	M_FREELIST	0x8000	/* mbuf is on the free list */
211de06f907SNik Clayton
212f434362aSYaroslav Tykhiy/* mbuf pkthdr flags (also stored in m_flags) */
2135d4050ccSBosko Milekic#define	M_BCAST		0x0200	/* send/received as link-level broadcast */
2145d4050ccSBosko Milekic#define	M_MCAST		0x0400	/* send/received as link-level multicast */
2155d4050ccSBosko Milekic#define	M_FRAG		0x0800	/* packet is fragment of larger packet */
2165d4050ccSBosko Milekic#define	M_FIRSTFRAG	0x1000	/* packet is first fragment */
2175d4050ccSBosko Milekic#define	M_LASTFRAG	0x2000	/* packet is last fragment */
218de06f907SNik Clayton.Ed
219de06f907SNik Clayton.Pp
2207b2fd183SYaroslav TykhiyThe available
2217b2fd183SYaroslav Tykhiy.Vt mbuf
2227b2fd183SYaroslav Tykhiytypes are defined as follows:
223de06f907SNik Clayton.Bd -literal
224de06f907SNik Clayton/* mbuf types */
225de06f907SNik Clayton#define	MT_DATA		1	/* dynamic (data) allocation */
226de06f907SNik Clayton#define	MT_HEADER	2	/* packet header */
227de06f907SNik Clayton#define	MT_SONAME	8	/* socket name */
228de06f907SNik Clayton#define	MT_FTABLE	11	/* fragment reassembly header */
229d2b73059SYaroslav Tykhiy#define	MT_TAG		13	/* volatile metadata associated to pkts */
230de06f907SNik Clayton#define	MT_CONTROL	14	/* extra-data protocol message */
231de06f907SNik Clayton#define	MT_OOBDATA	15	/* expedited data */
232de06f907SNik Clayton.Ed
233de06f907SNik Clayton.Pp
234de06f907SNik ClaytonIf the
235de06f907SNik Clayton.Dv M_PKTHDR
236de06f907SNik Claytonflag is set, a
2377b2fd183SYaroslav Tykhiy.Vt struct pkthdr Va m_pkthdr
2387b2fd183SYaroslav Tykhiyis added to the
2397b2fd183SYaroslav Tykhiy.Vt mbuf
2407b2fd183SYaroslav Tykhiyheader.
241e73145f6SSheldon HearnIt contains a pointer to the interface
242de06f907SNik Claytonthe packet has been received from
2437b2fd183SYaroslav Tykhiy.Pq Vt struct ifnet Va *rcvif ,
244de06f907SNik Claytonand the total packet length
2457b2fd183SYaroslav Tykhiy.Pq Vt int Va len .
2460193eb5eSYaroslav TykhiyFields used in offloading checksum calculation to the hardware are kept in
2470193eb5eSYaroslav Tykhiy.Va m_pkthdr
2480193eb5eSYaroslav Tykhiyas well.
2490193eb5eSYaroslav TykhiySee
2500193eb5eSYaroslav Tykhiy.Sx HARDWARE-ASSISTED CHECKSUM CALCULATION
2510193eb5eSYaroslav Tykhiyfor details.
252de06f907SNik Clayton.Pp
2537b2fd183SYaroslav TykhiyIf small enough, data is stored in the internal data buffer of an
2547b2fd183SYaroslav Tykhiy.Vt mbuf .
2557b2fd183SYaroslav TykhiyIf the data is sufficiently large, another
2567b2fd183SYaroslav Tykhiy.Vt mbuf
2577b2fd183SYaroslav Tykhiymay be added to the
2587b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
2597b2fd183SYaroslav Tykhiyor external storage may be associated with the
2607b2fd183SYaroslav Tykhiy.Vt mbuf .
261de06f907SNik Clayton.Dv MHLEN
2627b2fd183SYaroslav Tykhiybytes of data can fit into an
2637b2fd183SYaroslav Tykhiy.Vt mbuf
2647b2fd183SYaroslav Tykhiywith the
265de06f907SNik Clayton.Dv M_PKTHDR
266de06f907SNik Claytonflag set,
267de06f907SNik Clayton.Dv MLEN
268de06f907SNik Claytonbytes can otherwise.
269de06f907SNik Clayton.Pp
2707b2fd183SYaroslav TykhiyIf external storage is being associated with an
2717b2fd183SYaroslav Tykhiy.Vt mbuf ,
2727b2fd183SYaroslav Tykhiythe
273be82a0bcSYaroslav Tykhiy.Va m_ext
27425f1a2e9SSheldon Hearnheader is added at the cost of losing the internal data buffer.
275e73145f6SSheldon HearnIt includes a pointer to external storage, the size of the storage,
276e73145f6SSheldon Hearna pointer to a function used for freeing the storage,
277e73145f6SSheldon Hearna pointer to an optional argument that can be passed to the function,
278e73145f6SSheldon Hearnand a pointer to a reference counter.
2797b2fd183SYaroslav TykhiyAn
2807b2fd183SYaroslav Tykhiy.Vt mbuf
2817b2fd183SYaroslav Tykhiyusing external storage has the
282de06f907SNik Clayton.Dv M_EXT
283de06f907SNik Claytonflag set.
284de06f907SNik Clayton.Pp
285bfd59721SBosko MilekicThe system supplies a macro for allocating the desired external storage
286bfd59721SBosko Milekicbuffer,
287bfd59721SBosko Milekic.Dv MEXTADD .
288bfd59721SBosko Milekic.Pp
289bfd59721SBosko MilekicThe allocation and management of the reference counter is handled by the
290e73145f6SSheldon Hearnsubsystem.
291e73145f6SSheldon HearnThe developer can check whether the reference count for the
2927b2fd183SYaroslav Tykhiyexternal storage of a given
2937b2fd183SYaroslav Tykhiy.Vt mbuf
2947b2fd183SYaroslav Tykhiyis greater than 1 with the
295bfd59721SBosko Milekic.Dv MEXT_IS_REF
296e73145f6SSheldon Hearnmacro.
297e73145f6SSheldon HearnSimilarly, the developer can directly add and remove references,
298e73145f6SSheldon Hearnif absolutely necessary, with the use of the
299bfd59721SBosko Milekic.Dv MEXT_ADD_REF
300bfd59721SBosko Milekicand
301bfd59721SBosko Milekic.Dv MEXT_REM_REF
302bfd59721SBosko Milekicmacros.
303bfd59721SBosko Milekic.Pp
304bfd59721SBosko MilekicThe system also supplies a default type of external storage buffer called an
3057b2fd183SYaroslav Tykhiy.Vt mbuf cluster .
3067b2fd183SYaroslav Tykhiy.Vt Mbuf clusters
3077b2fd183SYaroslav Tykhiycan be allocated and configured with the use of the
308bfd59721SBosko Milekic.Dv MCLGET
309e73145f6SSheldon Hearnmacro.
3107b2fd183SYaroslav TykhiyEach
3117b2fd183SYaroslav Tykhiy.Vt mbuf cluster
3127b2fd183SYaroslav Tykhiyis
313de06f907SNik Clayton.Dv MCLBYTES
314bfd59721SBosko Milekicin size, where MCLBYTES is a machine-dependent constant.
315bfd59721SBosko MilekicThe system defines an advisory macro
316de06f907SNik Clayton.Dv MINCLSIZE ,
3177b2fd183SYaroslav Tykhiywhich is the smallest amount of data to put into an
3187b2fd183SYaroslav Tykhiy.Vt mbuf cluster .
319de06f907SNik ClaytonIt's equal to the sum of
320de06f907SNik Clayton.Dv MLEN
321de06f907SNik Claytonand
322de06f907SNik Clayton.Dv MHLEN .
3237b2fd183SYaroslav TykhiyIt is typically preferable to store data into the data region of an
3247b2fd183SYaroslav Tykhiy.Vt mbuf ,
3257b2fd183SYaroslav Tykhiyif size permits, as opposed to allocating a separate
3267b2fd183SYaroslav Tykhiy.Vt mbuf cluster
3277b2fd183SYaroslav Tykhiyto hold the same data.
328de06f907SNik Clayton.\"
329de06f907SNik Clayton.Ss Macros and Functions
330bfd59721SBosko MilekicThere are numerous predefined macros and functions that provide the
331bfd59721SBosko Milekicdeveloper with common utilities.
332de06f907SNik Clayton.\"
333de06f907SNik Clayton.Bl -ohang -offset indent
334de06f907SNik Clayton.It Fn mtod mbuf type
3357b2fd183SYaroslav TykhiyConvert an
3367b2fd183SYaroslav Tykhiy.Fa mbuf
3377b2fd183SYaroslav Tykhiypointer to a data pointer.
3387b2fd183SYaroslav TykhiyThe macro expands to the data pointer cast to the pointer of the specified
3397b2fd183SYaroslav Tykhiy.Fa type .
340de06f907SNik Clayton.Sy Note :
3417b2fd183SYaroslav TykhiyIt is advisable to ensure that there is enough contiguous data in
3427b2fd183SYaroslav Tykhiy.Fa mbuf .
343de06f907SNik ClaytonSee
344de06f907SNik Clayton.Fn m_pullup
345de06f907SNik Claytonfor details.
346de06f907SNik Clayton.It Fn MGET mbuf how type
3477b2fd183SYaroslav TykhiyAllocate an
3487b2fd183SYaroslav Tykhiy.Vt mbuf
3497b2fd183SYaroslav Tykhiyand initialize it to contain internal data.
35025f1a2e9SSheldon Hearn.Fa mbuf
3517b2fd183SYaroslav Tykhiywill point to the allocated
3527b2fd183SYaroslav Tykhiy.Vt mbuf
3537b2fd183SYaroslav Tykhiyon success, or be set to
354de06f907SNik Clayton.Dv NULL
355e73145f6SSheldon Hearnon failure.
356e73145f6SSheldon HearnThe
357de06f907SNik Clayton.Fa how
358de06f907SNik Claytonargument is to be set to
359c65b53c5STom Rhodes.Dv M_TRYWAIT
360de06f907SNik Claytonor
361c65b53c5STom Rhodes.Dv M_DONTWAIT .
36225f1a2e9SSheldon HearnIt specifies whether the caller is willing to block if necessary.
363e73145f6SSheldon HearnIf
364de06f907SNik Clayton.Fa how
36525f1a2e9SSheldon Hearnis set to
366c65b53c5STom Rhodes.Dv M_TRYWAIT ,
36725f1a2e9SSheldon Hearna failed allocation will result in the caller being put
368bfd59721SBosko Milekicto sleep for a designated
36925f1a2e9SSheldon Hearnkern.ipc.mbuf_wait
370c4d9468eSRuslan Ermilov.Xr ( sysctl 8
371c4d9468eSRuslan Ermilovtunable)
37225f1a2e9SSheldon Hearnnumber of ticks.
3737b2fd183SYaroslav TykhiyA number of other functions and macros related to
3747b2fd183SYaroslav Tykhiy.Vt mbufs
3757b2fd183SYaroslav Tykhiyhave the same argument because they may
3767b2fd183SYaroslav Tykhiyat some point need to allocate new
3777b2fd183SYaroslav Tykhiy.Vt mbufs .
378c65b53c5STom Rhodes.Pp
3797b2fd183SYaroslav TykhiyProgrammers should be careful not to confuse the
3807b2fd183SYaroslav Tykhiy.Vt mbuf
3817b2fd183SYaroslav Tykhiyallocation flag
382c65b53c5STom Rhodes.Dv M_DONTWAIT
383c65b53c5STom Rhodeswith the
384c65b53c5STom Rhodes.Xr malloc 9
385c65b53c5STom Rhodesallocation flag,
386c65b53c5STom Rhodes.Dv M_NOWAIT .
387c65b53c5STom RhodesThey are not the same.
388de06f907SNik Clayton.It Fn MGETHDR mbuf how type
3897b2fd183SYaroslav TykhiyAllocate an
3907b2fd183SYaroslav Tykhiy.Vt mbuf
3917b2fd183SYaroslav Tykhiyand initialize it to contain a packet header
392e73145f6SSheldon Hearnand internal data.
393e73145f6SSheldon HearnSee
394de06f907SNik Clayton.Fn MGET
395de06f907SNik Claytonfor details.
396de06f907SNik Clayton.It Fn MCLGET mbuf how
3977b2fd183SYaroslav TykhiyAllocate and attach an
3987b2fd183SYaroslav Tykhiy.Vt mbuf cluster
3997b2fd183SYaroslav Tykhiyto
4007b2fd183SYaroslav Tykhiy.Fa mbuf .
401e73145f6SSheldon HearnIf the macro fails, the
402de06f907SNik Clayton.Dv M_EXT
4037b2fd183SYaroslav Tykhiyflag won't be set in
4047b2fd183SYaroslav Tykhiy.Fa mbuf .
405da72b998SYaroslav Tykhiy.It Fn M_ALIGN mbuf len
406da72b998SYaroslav TykhiySet the pointer
407da72b998SYaroslav Tykhiy.Fa mbuf->m_data
408da72b998SYaroslav Tykhiyto place an object of the size
409da72b998SYaroslav Tykhiy.Fa len
410da72b998SYaroslav Tykhiyat the end of the internal data area of
411da72b998SYaroslav Tykhiy.Fa mbuf ,
412da72b998SYaroslav Tykhiylong word aligned.
413da72b998SYaroslav TykhiyApplicable only if
414da72b998SYaroslav Tykhiy.Fa mbuf
415da72b998SYaroslav Tykhiyis newly allocated with
416da72b998SYaroslav Tykhiy.Fn MGET
417da72b998SYaroslav Tykhiyor
418da72b998SYaroslav Tykhiy.Fn m_get .
419da72b998SYaroslav Tykhiy.It Fn MH_ALIGN mbuf len
420da72b998SYaroslav TykhiyServes the same purpose as
421da72b998SYaroslav Tykhiy.Fn M_ALIGN
422da72b998SYaroslav Tykhiydoes, but only for
423da72b998SYaroslav Tykhiy.Fa mbuf
424da72b998SYaroslav Tykhiynewly allocated with
425da72b998SYaroslav Tykhiy.Fn MGETHDR
426da72b998SYaroslav Tykhiyor
427da72b998SYaroslav Tykhiy.Fn m_gethdr ,
428da72b998SYaroslav Tykhiyor initialized by
429da72b998SYaroslav Tykhiy.Fn m_dup_pkthdr
430da72b998SYaroslav Tykhiyor
431da72b998SYaroslav Tykhiy.Fn m_move_pkthdr .
432da72b998SYaroslav Tykhiy.It Fn M_LEADINGSPACE mbuf
433da72b998SYaroslav TykhiyReturns the number of bytes available before the beginning
434da72b998SYaroslav Tykhiyof data in
435da72b998SYaroslav Tykhiy.Fa mbuf .
436da72b998SYaroslav Tykhiy.It Fn M_TRAILINGSPACE mbuf
437da72b998SYaroslav TykhiyReturns the number of bytes available after the end of data in
438da72b998SYaroslav Tykhiy.Fa mbuf .
439de06f907SNik Clayton.It Fn M_PREPEND mbuf len how
4407b2fd183SYaroslav TykhiyThis macro operates on an
4417b2fd183SYaroslav Tykhiy.Vt mbuf chain .
442de06f907SNik ClaytonIt is an optimized wrapper for
443de06f907SNik Clayton.Fn m_prepend
444de06f907SNik Claytonthat can make use of possible empty space before data
445c4d9468eSRuslan Ermilov(e.g. left after trimming of a link-layer header).
4467b2fd183SYaroslav TykhiyThe new
4477b2fd183SYaroslav Tykhiy.Vt mbuf chain
4487b2fd183SYaroslav Tykhiypointer or
449de06f907SNik Clayton.Dv NULL
450de06f907SNik Claytonis in
451de06f907SNik Clayton.Fa mbuf
452de06f907SNik Claytonafter the call.
453da72b998SYaroslav Tykhiy.It Fn M_MOVE_PKTHDR to from
454da72b998SYaroslav TykhiyUsing this macro is equivalent to calling
455da72b998SYaroslav Tykhiy.Fn m_move_pkthdr to from .
4565d4050ccSBosko Milekic.It Fn M_WRITABLE mbuf
4577b2fd183SYaroslav TykhiyThis macro will evaluate true if
4587b2fd183SYaroslav Tykhiy.Fa mbuf
4597b2fd183SYaroslav Tykhiyis not marked
4605d4050ccSBosko Milekic.Dv M_RDONLY
4617b2fd183SYaroslav Tykhiyand if either
4627b2fd183SYaroslav Tykhiy.Fa mbuf
4637b2fd183SYaroslav Tykhiydoes not contain external storage or,
4645d4050ccSBosko Milekicif it does,
4655d4050ccSBosko Milekicthen if the reference count of the storage is not greater than 1.
4665d4050ccSBosko MilekicThe
4675d4050ccSBosko Milekic.Dv M_RDONLY
4687b2fd183SYaroslav Tykhiyflag can be set in
4697b2fd183SYaroslav Tykhiy.Fa mbuf->m_flags .
4705d4050ccSBosko MilekicThis can be achieved during setup of the external storage,
4715d4050ccSBosko Milekicby passing the
4725d4050ccSBosko Milekic.Dv M_RDONLY
4735d4050ccSBosko Milekicbit as a
4740b39b3ffSRuslan Ermilov.Fa flags
4755d4050ccSBosko Milekicargument to the
4765d4050ccSBosko Milekic.Fn MEXTADD
4777b2fd183SYaroslav Tykhiymacro, or can be directly set in individual
4787b2fd183SYaroslav Tykhiy.Vt mbufs .
479da72b998SYaroslav Tykhiy.It Fn MCHTYPE mbuf type
480da72b998SYaroslav TykhiyChange the type of
481da72b998SYaroslav Tykhiy.Fa mbuf
482da72b998SYaroslav Tykhiyto
483da72b998SYaroslav Tykhiy.Fa type .
484da72b998SYaroslav TykhiyThis is a relatively expensive operation and should be avoided.
485de06f907SNik Clayton.El
486de06f907SNik Clayton.Pp
487de06f907SNik ClaytonThe functions are:
488de06f907SNik Clayton.Bl -ohang -offset indent
489de06f907SNik Clayton.It Fn m_get how type
490de06f907SNik ClaytonA function version of
491bfd59721SBosko Milekic.Fn MGET
492bfd59721SBosko Milekicfor non-critical paths.
493328c0bbcSBosko Milekic.It Fn m_getm orig len how type
494328c0bbcSBosko MilekicAllocate
4950b39b3ffSRuslan Ermilov.Fa len
4967b2fd183SYaroslav Tykhiybytes worth of
4977b2fd183SYaroslav Tykhiy.Vt mbufs
4987b2fd183SYaroslav Tykhiyand
4997b2fd183SYaroslav Tykhiy.Vt mbuf clusters
5007b2fd183SYaroslav Tykhiyif necessary and append the resulting allocated
5017b2fd183SYaroslav Tykhiy.Vt mbuf chain
5027b2fd183SYaroslav Tykhiyto the
5037b2fd183SYaroslav Tykhiy.Vt mbuf chain
5047b2fd183SYaroslav Tykhiy.Fa orig ,
5057b2fd183SYaroslav Tykhiyif it is
5060b39b3ffSRuslan Ermilov.No non- Ns Dv NULL .
507328c0bbcSBosko MilekicIf the allocation fails at any point,
5080b39b3ffSRuslan Ermilovfree whatever was allocated and return
5090b39b3ffSRuslan Ermilov.Dv NULL .
510328c0bbcSBosko MilekicIf
5110b39b3ffSRuslan Ermilov.Fa orig
5120b39b3ffSRuslan Ermilovis
5130b39b3ffSRuslan Ermilov.No non- Ns Dv NULL ,
514328c0bbcSBosko Milekicit will not be freed.
515328c0bbcSBosko MilekicIt is possible to use
516328c0bbcSBosko Milekic.Fn m_getm
517328c0bbcSBosko Milekicto either append
5180b39b3ffSRuslan Ermilov.Fa len
5197b2fd183SYaroslav Tykhiybytes to an existing
5207b2fd183SYaroslav Tykhiy.Vt mbuf
5217b2fd183SYaroslav Tykhiyor
5227b2fd183SYaroslav Tykhiy.Vt mbuf chain
523328c0bbcSBosko Milekic(for example, one which may be sitting in a pre-allocated ring)
5247b2fd183SYaroslav Tykhiyor to simply perform an all-or-nothing
5257b2fd183SYaroslav Tykhiy.Vt mbuf
5267b2fd183SYaroslav Tykhiyand
5277b2fd183SYaroslav Tykhiy.Vt mbuf cluster
5287b2fd183SYaroslav Tykhiyallocation.
529de06f907SNik Clayton.It Fn m_gethdr how type
530de06f907SNik ClaytonA function version of
531bfd59721SBosko Milekic.Fn MGETHDR
532bfd59721SBosko Milekicfor non-critical paths.
533de06f907SNik Clayton.It Fn m_getclr how type
5347b2fd183SYaroslav TykhiyAllocate an
5357b2fd183SYaroslav Tykhiy.Vt mbuf
5367b2fd183SYaroslav Tykhiyand zero out the data region.
537de06f907SNik Clayton.El
538de06f907SNik Clayton.Pp
5397b2fd183SYaroslav TykhiyThe functions below operate on
5407b2fd183SYaroslav Tykhiy.Vt mbuf chains .
541de06f907SNik Clayton.Bl -ohang -offset indent
542de06f907SNik Clayton.It Fn m_freem mbuf
5437b2fd183SYaroslav TykhiyFree an entire
5447b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
5457b2fd183SYaroslav Tykhiyincluding any external storage.
546de06f907SNik Clayton.\"
547de06f907SNik Clayton.It Fn m_adj mbuf len
548de06f907SNik ClaytonTrim
549de06f907SNik Clayton.Fa len
5507b2fd183SYaroslav Tykhiybytes from the head of an
5517b2fd183SYaroslav Tykhiy.Vt mbuf chain
5527b2fd183SYaroslav Tykhiyif
553de06f907SNik Clayton.Fa len
554de06f907SNik Claytonis positive, from the tail otherwise.
555de06f907SNik Clayton.\"
556de06f907SNik Clayton.It Fn m_prepend mbuf len how
5577b2fd183SYaroslav TykhiyAllocate a new
5587b2fd183SYaroslav Tykhiy.Vt mbuf
5597b2fd183SYaroslav Tykhiyand prepend it to the
5607b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
5617b2fd183SYaroslav Tykhiyhandle
562de06f907SNik Clayton.Dv M_PKTHDR
563de06f907SNik Claytonproperly.
564de06f907SNik Clayton.Sy Note :
5657b2fd183SYaroslav TykhiyIt doesn't allocate any
5667b2fd183SYaroslav Tykhiy.Vt mbuf clusters ,
5677b2fd183SYaroslav Tykhiyso
568de06f907SNik Clayton.Fa len
569de06f907SNik Claytonmust be less than
570de06f907SNik Clayton.Dv MLEN
571de06f907SNik Claytonor
572de06f907SNik Clayton.Dv MHLEN ,
573de06f907SNik Claytondepending on the
574d0353b83SRuslan Ermilov.Dv M_PKTHDR
575d0353b83SRuslan Ermilovflag setting.
576de06f907SNik Clayton.\"
577de06f907SNik Clayton.It Fn m_pullup mbuf len
578de06f907SNik ClaytonArrange that the first
579de06f907SNik Clayton.Fa len
5807b2fd183SYaroslav Tykhiybytes of an
5817b2fd183SYaroslav Tykhiy.Vt mbuf chain
5827b2fd183SYaroslav Tykhiyare contiguous and lay in the data area of
583de06f907SNik Clayton.Fa mbuf ,
584de06f907SNik Claytonso they are accessible with
585de06f907SNik Clayton.Fn mtod mbuf type .
5867b2fd183SYaroslav TykhiyReturn the new
5877b2fd183SYaroslav Tykhiy.Vt mbuf chain
5887b2fd183SYaroslav Tykhiyon success,
589de06f907SNik Clayton.Dv NULL
590de06f907SNik Claytonon failure
5917b2fd183SYaroslav Tykhiy(the
5927b2fd183SYaroslav Tykhiy.Vt mbuf chain
5937b2fd183SYaroslav Tykhiyis freed in this case).
594de06f907SNik Clayton.Sy Note :
5957b2fd183SYaroslav TykhiyIt doesn't allocate any
5967b2fd183SYaroslav Tykhiy.Vt mbuf clusters ,
5977b2fd183SYaroslav Tykhiyso
598de06f907SNik Clayton.Fa len
599de06f907SNik Claytonmust be less than
600de06f907SNik Clayton.Dv MHLEN .
601de06f907SNik Clayton.\"
602de06f907SNik Clayton.It Fn m_copym mbuf offset len how
6037b2fd183SYaroslav TykhiyMake a copy of an
6047b2fd183SYaroslav Tykhiy.Vt mbuf chain
6057b2fd183SYaroslav Tykhiystarting
606de06f907SNik Clayton.Fa offset
607de06f907SNik Claytonbytes from the beginning, continuing for
608de06f907SNik Clayton.Fa len
609e73145f6SSheldon Hearnbytes.
610e73145f6SSheldon HearnIf
611de06f907SNik Clayton.Fa len
612de06f907SNik Claytonis
613de06f907SNik Clayton.Dv M_COPYALL ,
6147b2fd183SYaroslav Tykhiycopy to the end of the
6157b2fd183SYaroslav Tykhiy.Vt mbuf chain .
616de06f907SNik Clayton.Sy Note :
6177b2fd183SYaroslav TykhiyThe copy is read-only, because the
6187b2fd183SYaroslav Tykhiy.Vt mbuf clusters
6197b2fd183SYaroslav Tykhiyare not copied, only their reference counts are incremented.
620de06f907SNik Clayton.\"
621de06f907SNik Clayton.It Fn m_copypacket mbuf how
622de06f907SNik ClaytonCopy an entire packet including header, which must be present.
623de06f907SNik ClaytonThis is an optimized version of the common case
624de06f907SNik Clayton.Fn m_copym mbuf 0 M_COPYALL how .
625de06f907SNik Clayton.Sy Note :
6267b2fd183SYaroslav Tykhiythe copy is read-only, because the
6277b2fd183SYaroslav Tykhiy.Vt mbuf clusters
6287b2fd183SYaroslav Tykhiyare not copied, only their reference counts are incremented.
629de06f907SNik Clayton.\"
630de06f907SNik Clayton.It Fn m_dup mbuf how
6317b2fd183SYaroslav TykhiyCopy a packet header
6327b2fd183SYaroslav Tykhiy.Vt mbuf chain
6337b2fd183SYaroslav Tykhiyinto a completely new
6347b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
6357b2fd183SYaroslav Tykhiyincluding copying any
6367b2fd183SYaroslav Tykhiy.Vt mbuf clusters .
637e73145f6SSheldon HearnUse this instead of
638de06f907SNik Clayton.Fn m_copypacket
6397b2fd183SYaroslav Tykhiywhen you need a writable copy of an
6407b2fd183SYaroslav Tykhiy.Vt mbuf chain .
641de06f907SNik Clayton.\"
642de06f907SNik Clayton.It Fn m_copydata mbuf offset len buf
6437b2fd183SYaroslav TykhiyCopy data from an
6447b2fd183SYaroslav Tykhiy.Vt mbuf chain
6457b2fd183SYaroslav Tykhiystarting
646de06f907SNik Clayton.Fa off
647de06f907SNik Claytonbytes from the beginning, continuing for
648de06f907SNik Clayton.Fa len
649de06f907SNik Claytonbytes, into the indicated buffer
650de06f907SNik Clayton.Fa buf .
651de06f907SNik Clayton.\"
652de06f907SNik Clayton.It Fn m_copyback mbuf offset len buf
653de06f907SNik ClaytonCopy
654de06f907SNik Clayton.Fa len
655de06f907SNik Claytonbytes from the buffer
656de06f907SNik Clayton.Fa buf
6577b2fd183SYaroslav Tykhiyback into the indicated
6587b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
659de06f907SNik Claytonstarting at
660de06f907SNik Clayton.Fa offset
6617b2fd183SYaroslav Tykhiybytes from the beginning of the
6627b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
6637b2fd183SYaroslav Tykhiyextending the
6647b2fd183SYaroslav Tykhiy.Vt mbuf chain
6657b2fd183SYaroslav Tykhiyif necessary.
666de06f907SNik Clayton.Sy Note :
6677b2fd183SYaroslav TykhiyIt doesn't allocate any
6687b2fd183SYaroslav Tykhiy.Vt mbuf clusters ,
6697b2fd183SYaroslav Tykhiyjust adds
6707b2fd183SYaroslav Tykhiy.Vt mbufs
6717b2fd183SYaroslav Tykhiyto the
6727b2fd183SYaroslav Tykhiy.Vt mbuf chain .
673e73145f6SSheldon HearnIt's safe to set
674de06f907SNik Clayton.Fa offset
6757b2fd183SYaroslav Tykhiybeyond the current
6767b2fd183SYaroslav Tykhiy.Vt mbuf chain
6777b2fd183SYaroslav Tykhiyend: zeroed
6787b2fd183SYaroslav Tykhiy.Vt mbufs
6797b2fd183SYaroslav Tykhiywill be allocated to fill the space.
680de06f907SNik Clayton.\"
6815d96084fSYaroslav Tykhiy.It Fn m_length mbuf last
6827b2fd183SYaroslav TykhiyReturn the length of the
6837b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
6847b2fd183SYaroslav Tykhiyand optionally a pointer to the last
6857b2fd183SYaroslav Tykhiy.Vt mbuf .
6860b1c0994SPoul-Henning Kamp.\"
687da72b998SYaroslav Tykhiy.It Fn m_dup_pkthdr to from how
688da72b998SYaroslav TykhiyUpon the function's completion, the
689da72b998SYaroslav Tykhiy.Vt mbuf
690da72b998SYaroslav Tykhiy.Fa to
691da72b998SYaroslav Tykhiywill contain an identical copy of
692da72b998SYaroslav Tykhiy.Fa from->m_pkthdr
693da72b998SYaroslav Tykhiyand the per-packet attributes found in the
694da72b998SYaroslav Tykhiy.Vt mbuf chain
695da72b998SYaroslav Tykhiy.Fa from .
696da72b998SYaroslav TykhiyThe
697da72b998SYaroslav Tykhiy.Vt mbuf
698da72b998SYaroslav Tykhiy.Fa from
699da72b998SYaroslav Tykhiymust have the flag
700da72b998SYaroslav Tykhiy.Dv M_PKTHDR
701da72b998SYaroslav Tykhiyinitially set, and
702da72b998SYaroslav Tykhiy.Fa to
703da72b998SYaroslav Tykhiymust be empty on entry.
704da72b998SYaroslav Tykhiy.\"
705da72b998SYaroslav Tykhiy.It Fn m_move_pkthdr to from
706da72b998SYaroslav TykhiyMove
707da72b998SYaroslav Tykhiy.Va m_pkthdr
708da72b998SYaroslav Tykhiyand the per-packet attributes from the
709da72b998SYaroslav Tykhiy.Vt mbuf chain
710da72b998SYaroslav Tykhiy.Fa from
711da72b998SYaroslav Tykhiyto the
712da72b998SYaroslav Tykhiy.Vt mbuf
713da72b998SYaroslav Tykhiy.Fa to .
714da72b998SYaroslav TykhiyThe
715da72b998SYaroslav Tykhiy.Vt mbuf
716da72b998SYaroslav Tykhiy.Fa from
717da72b998SYaroslav Tykhiymust have the flag
718da72b998SYaroslav Tykhiy.Dv M_PKTHDR
719da72b998SYaroslav Tykhiyinitially set, and
720da72b998SYaroslav Tykhiy.Fa to
721da72b998SYaroslav Tykhiymust be empty on entry.
722da72b998SYaroslav TykhiyUpon the function's completion,
723da72b998SYaroslav Tykhiy.Fa from
724da72b998SYaroslav Tykhiywill have the flag
725da72b998SYaroslav Tykhiy.Dv M_PKTHDR
726da72b998SYaroslav Tykhiyand the per-packet attributes cleared.
727da72b998SYaroslav Tykhiy.\"
7285d96084fSYaroslav Tykhiy.It Fn m_fixhdr mbuf
7297b2fd183SYaroslav TykhiySet the packet-header length to the length of the
7307b2fd183SYaroslav Tykhiy.Vt mbuf chain .
7310b1c0994SPoul-Henning Kamp.\"
732de06f907SNik Clayton.It Fn m_devget buf len offset ifp copy
733de06f907SNik ClaytonCopy data from a device local memory pointed to by
734de06f907SNik Clayton.Fa buf
7357b2fd183SYaroslav Tykhiyto an
7367b2fd183SYaroslav Tykhiy.Vt mbuf chain .
737e73145f6SSheldon HearnThe copy is done using a specified copy routine
738de06f907SNik Clayton.Fa copy ,
739de06f907SNik Claytonor
740de06f907SNik Clayton.Fn bcopy
741de06f907SNik Claytonif
742de06f907SNik Clayton.Fa copy
743de06f907SNik Claytonis
744de06f907SNik Clayton.Dv NULL .
745de06f907SNik Clayton.\"
746de06f907SNik Clayton.It Fn m_cat m n
747de06f907SNik ClaytonConcatenate
748de06f907SNik Clayton.Fa n
749de06f907SNik Claytonto
750de06f907SNik Clayton.Fa m .
7517b2fd183SYaroslav TykhiyBoth
7527b2fd183SYaroslav Tykhiy.Vt mbuf chains
7537b2fd183SYaroslav Tykhiymust be of the same type.
754de06f907SNik Clayton.Fa N
755de06f907SNik Claytonis still valid after the function returned.
756de06f907SNik Clayton.Sy Note :
757de06f907SNik ClaytonIt does not handle
758de06f907SNik Clayton.Dv M_PKTHDR
759de06f907SNik Claytonand friends.
760de06f907SNik Clayton.\"
761de06f907SNik Clayton.It Fn m_split mbuf len how
7627b2fd183SYaroslav TykhiyPartition an
7637b2fd183SYaroslav Tykhiy.Vt mbuf chain
7647b2fd183SYaroslav Tykhiyin two pieces, returning the tail:
765de06f907SNik Claytonall but the first
766de06f907SNik Clayton.Fa len
767e73145f6SSheldon Hearnbytes.
768e73145f6SSheldon HearnIn case of failure, it returns
769de06f907SNik Clayton.Dv NULL
7707b2fd183SYaroslav Tykhiyand attempts to restore the
7717b2fd183SYaroslav Tykhiy.Vt mbuf chain
7727b2fd183SYaroslav Tykhiyto its original state.
77302312219SBruce M Simpson.\"
77402312219SBruce M Simpson.It Fn m_apply mbuf off len f arg
77502312219SBruce M SimpsonApply a function to an
77602312219SBruce M Simpson.Vt mbuf chain ,
77702312219SBruce M Simpsonat offset
77802312219SBruce M Simpson.Fa off ,
77902312219SBruce M Simpsonfor length
780f48cbcdeSRuslan Ermilov.Fa len
781f48cbcdeSRuslan Ermilovbytes.
78202312219SBruce M SimpsonTypically used to avoid calls to
78302312219SBruce M Simpson.Fn m_pullup
78402312219SBruce M Simpsonwhich would otherwise be unnecessary or undesirable.
78502312219SBruce M Simpson.Fa arg
78602312219SBruce M Simpsonis a convenience argument which is passed to the callback function
78702312219SBruce M Simpson.Fa f .
78802312219SBruce M Simpson.Pp
78902312219SBruce M SimpsonEach time
79002312219SBruce M Simpson.Fn f
79102312219SBruce M Simpsonis called, it will be passed
79202312219SBruce M Simpson.Fa arg ,
79302312219SBruce M Simpsona pointer to the
79402312219SBruce M Simpson.Fa data
79502312219SBruce M Simpsonin the current mbuf, and the length
79602312219SBruce M Simpson.Fa len
79702312219SBruce M Simpsonof the data in this mbuf to which the function should be applied.
79802312219SBruce M Simpson.Pp
79902312219SBruce M SimpsonThe function should return zero to indicate success;
80002312219SBruce M Simpsonotherwise, if an error is indicated, then
80102312219SBruce M Simpson.Fn m_apply
80202312219SBruce M Simpsonwill return the error and stop iterating through the
80302312219SBruce M Simpson.Vt mbuf chain .
80402312219SBruce M Simpson.\"
80502312219SBruce M Simpson.It Fn m_getptr mbuf loc off
80602312219SBruce M SimpsonReturn a pointer to the mbuf containing the data located at
80702312219SBruce M Simpson.Fa loc
80802312219SBruce M Simpsonbytes from the beginning of the
80902312219SBruce M Simpson.Vt mbuf chain .
81002312219SBruce M SimpsonThe corresponding offset into the mbuf will be stored in
81102312219SBruce M Simpson.Fa *off .
812a61bc764SMike Silbersack.It Fn m_defrag m0 how
813f48cbcdeSRuslan ErmilovDefragment an mbuf chain, returning the shortest possible
814bdf86185SMike Silbersackchain of mbufs and clusters.
815bdf86185SMike SilbersackIf allocation fails and this can not be completed,
816bdf86185SMike Silbersack.Dv NULL
817bdf86185SMike Silbersackwill be returned and the original chain will be unchanged.
818bdf86185SMike SilbersackUpon success, the original chain will be freed and the new
819bdf86185SMike Silbersackchain will be returned.
820a61bc764SMike Silbersack.Fa how
821a61bc764SMike Silbersackshould be either
822a61bc764SMike Silbersack.Dv M_TRYWAIT
823a61bc764SMike Silbersackor
824a61bc764SMike Silbersack.Dv M_DONTWAIT ,
825a61bc764SMike Silbersackdepending on the caller's preference.
826a61bc764SMike Silbersack.Pp
827a61bc764SMike SilbersackThis function is especially useful in network drivers, where
828a61bc764SMike Silbersackcertain long mbuf chains must be shortened before being added
829a61bc764SMike Silbersackto TX descriptor lists.
8303136363fSRuslan Ermilov.El
8310193eb5eSYaroslav Tykhiy.Sh HARDWARE-ASSISTED CHECKSUM CALCULATION
8320193eb5eSYaroslav TykhiyThis section currently applies to TCP/IP only.
8330193eb5eSYaroslav TykhiyIn order to save the host CPU resources, computing checksums is
8340193eb5eSYaroslav Tykhiyoffloaded to the network interface hardware if possible.
8350193eb5eSYaroslav TykhiyThe
8360193eb5eSYaroslav Tykhiy.Va m_pkthdr
8370193eb5eSYaroslav Tykhiymember of the leading
8380193eb5eSYaroslav Tykhiy.Vt mbuf
8390193eb5eSYaroslav Tykhiyof a packet contains two fields used for that purpose,
8400193eb5eSYaroslav Tykhiy.Vt int Va csum_flags
8410193eb5eSYaroslav Tykhiyand
8420193eb5eSYaroslav Tykhiy.Vt int Va csum_data .
8430193eb5eSYaroslav TykhiyThe meaning of those fields depends on the direction a packet flows in,
8440193eb5eSYaroslav Tykhiyand on whether the packet is fragmented.
8450193eb5eSYaroslav TykhiyHenceforth,
8460193eb5eSYaroslav Tykhiy.Va csum_flags
8470193eb5eSYaroslav Tykhiyor
8480193eb5eSYaroslav Tykhiy.Va csum_data
8490193eb5eSYaroslav Tykhiyof a packet
8500193eb5eSYaroslav Tykhiywill denote the corresponding field of the
8510193eb5eSYaroslav Tykhiy.Va m_pkthdr
8520193eb5eSYaroslav Tykhiymember of the leading
8530193eb5eSYaroslav Tykhiy.Vt mbuf
8540193eb5eSYaroslav Tykhiyin the
8550193eb5eSYaroslav Tykhiy.Vt mbuf chain
8560193eb5eSYaroslav Tykhiycontaining the packet.
8570193eb5eSYaroslav Tykhiy.Pp
8580193eb5eSYaroslav TykhiyOn output, checksum offloading is attempted after the outgoing
8590193eb5eSYaroslav Tykhiyinterface has been determined for a packet.
8600193eb5eSYaroslav TykhiyThe interface-specific field
8610193eb5eSYaroslav Tykhiy.Va ifnet.if_data.ifi_hwassist
8620193eb5eSYaroslav Tykhiy(see
8630193eb5eSYaroslav Tykhiy.Xr ifnet 9 )
8640193eb5eSYaroslav Tykhiyis consulted for the capabilities of the interface to assist in
8650193eb5eSYaroslav Tykhiycomputing checksums.
8660193eb5eSYaroslav TykhiyThe
8670193eb5eSYaroslav Tykhiy.Va csum_flags
868f48cbcdeSRuslan Ermilovfield of the packet header is set to indicate which actions the interface
8690193eb5eSYaroslav Tykhiyis supposed to perform on it.
8700193eb5eSYaroslav TykhiyThe actions unsupported by the network interface are done in the
8710193eb5eSYaroslav Tykhiysoftware prior to passing the packet down to the interface driver;
8720193eb5eSYaroslav Tykhiysuch actions will never be requested through
8730193eb5eSYaroslav Tykhiy.Va csum_flags .
8740193eb5eSYaroslav Tykhiy.Pp
8750193eb5eSYaroslav TykhiyThe flags demanding a particular action from an interface are as follows:
8760193eb5eSYaroslav Tykhiy.Bl -tag -width ".Dv CSUM_TCP" -offset indent
8770193eb5eSYaroslav Tykhiy.It Dv CSUM_IP
8780193eb5eSYaroslav TykhiyThe IP header checksum is to be computed and stored in the
8790193eb5eSYaroslav Tykhiycorresponding field of the packet.
8800193eb5eSYaroslav TykhiyThe hardware is expected to know the format of an IP header
8810193eb5eSYaroslav Tykhiyto determine the offset of the IP checksum field.
8820193eb5eSYaroslav Tykhiy.It Dv CSUM_TCP
883f48cbcdeSRuslan ErmilovThe TCP checksum is to be computed.
884f48cbcdeSRuslan Ermilov(See below.)
8850193eb5eSYaroslav Tykhiy.It Dv CSUM_UDP
886f48cbcdeSRuslan ErmilovThe UDP checksum is to be computed.
887f48cbcdeSRuslan Ermilov(See below.)
8880193eb5eSYaroslav Tykhiy.El
8890193eb5eSYaroslav Tykhiy.Pp
890fbde705aSYaroslav TykhiyShould a TCP or UDP checksum be offloaded to the hardware,
8910193eb5eSYaroslav Tykhiythe field
8920193eb5eSYaroslav Tykhiy.Va csum_data
8930193eb5eSYaroslav Tykhiywill contain the byte offset of the checksum field relative to the
8940193eb5eSYaroslav Tykhiyend of the IP header.
8950193eb5eSYaroslav TykhiyIn this case, the checksum field will be initially
8960193eb5eSYaroslav Tykhiyset by the TCP/IP module to the checksum of the pseudo header
897fbde705aSYaroslav Tykhiydefined by the TCP and UDP specifications.
8980193eb5eSYaroslav Tykhiy.Pp
8990193eb5eSYaroslav TykhiyFor outbound packets which have been fragmented
9000193eb5eSYaroslav Tykhiyby the host CPU, the following will also be true,
9010193eb5eSYaroslav Tykhiyregardless of the checksum flag settings:
9020193eb5eSYaroslav Tykhiy.Bl -bullet -offset indent
9030193eb5eSYaroslav Tykhiy.It
9040193eb5eSYaroslav Tykhiyall fragments will have the flag
9050193eb5eSYaroslav Tykhiy.Dv M_FRAG
9060193eb5eSYaroslav Tykhiyset in their
9070193eb5eSYaroslav Tykhiy.Va m_flags
9080193eb5eSYaroslav Tykhiyfield;
9090193eb5eSYaroslav Tykhiy.It
9100193eb5eSYaroslav Tykhiythe first and the last fragments in the chain will have
9110193eb5eSYaroslav Tykhiy.Dv M_FIRSTFRAG
9120193eb5eSYaroslav Tykhiyor
9130193eb5eSYaroslav Tykhiy.Dv M_LASTFRAG
9140193eb5eSYaroslav Tykhiyset in their
9150193eb5eSYaroslav Tykhiy.Va m_flags ,
9160193eb5eSYaroslav Tykhiycorrespondingly;
9170193eb5eSYaroslav Tykhiy.It
9180193eb5eSYaroslav Tykhiythe first fragment in the chain will have the total number
9190193eb5eSYaroslav Tykhiyof fragments contained in its
9200193eb5eSYaroslav Tykhiy.Va csum_data
9210193eb5eSYaroslav Tykhiyfield.
9220193eb5eSYaroslav Tykhiy.El
9230193eb5eSYaroslav Tykhiy.Pp
92476f80df6SYaroslav TykhiyThe last rule for fragmented packets takes precedence over the one
92576f80df6SYaroslav Tykhiyfor a TCP or UDP checksum.
92676f80df6SYaroslav TykhiyNevertheless, offloading a TCP or UDP checksum is possible for a
92776f80df6SYaroslav Tykhiyfragmented packet if the flag
92876f80df6SYaroslav Tykhiy.Dv CSUM_IP_FRAGS
92976f80df6SYaroslav Tykhiyis set in the field
93076f80df6SYaroslav Tykhiy.Va ifnet.if_data.ifi_hwassist
93176f80df6SYaroslav Tykhiyassociated with the network interface.
93276f80df6SYaroslav TykhiyHowever, in this case the interface is expected to figure out
93376f80df6SYaroslav Tykhiythe location of the checksum field within the sequence of fragments
93476f80df6SYaroslav Tykhiyby itself because
9350193eb5eSYaroslav Tykhiy.Va csum_data
93676f80df6SYaroslav Tykhiycontains a fragment count instead of a checksum offset value.
9370193eb5eSYaroslav Tykhiy.Pp
9380193eb5eSYaroslav TykhiyOn input, an interface indicates the actions it has performed
9390193eb5eSYaroslav Tykhiyon a packet by setting one or more of the following flags in
9400193eb5eSYaroslav Tykhiy.Va csum_flags
9410193eb5eSYaroslav Tykhiyassociated with the packet:
9420193eb5eSYaroslav Tykhiy.Bl -tag -width ".Dv CSUM_IP_CHECKED" -offset indent
9430193eb5eSYaroslav Tykhiy.It Dv CSUM_IP_CHECKED
9440193eb5eSYaroslav TykhiyThe IP header checksum has been computed.
9450193eb5eSYaroslav Tykhiy.It Dv CSUM_IP_VALID
9460193eb5eSYaroslav TykhiyThe IP header has a valid checksum.
9470193eb5eSYaroslav TykhiyThis flag can appear only in combination with
9480193eb5eSYaroslav Tykhiy.Dv CSUM_IP_CHECKED .
9490193eb5eSYaroslav Tykhiy.It Dv CSUM_DATA_VALID
9500193eb5eSYaroslav TykhiyThe checksum of the data portion of the IP packet has been computed
9510193eb5eSYaroslav Tykhiyand stored in the field
9520193eb5eSYaroslav Tykhiy.Va csum_data
9530193eb5eSYaroslav Tykhiyin network byte order.
9540193eb5eSYaroslav Tykhiy.It Dv CSUM_PSEUDO_HDR
9550193eb5eSYaroslav TykhiyCan be set only along with
9560193eb5eSYaroslav Tykhiy.Dv CSUM_DATA_VALID
9570193eb5eSYaroslav Tykhiyto indicate that the IP data checksum found in
9580193eb5eSYaroslav Tykhiy.Va csum_data
959fbde705aSYaroslav Tykhiyallows for the pseudo header defined by the TCP and UDP specifications.
9600193eb5eSYaroslav TykhiyOtherwise the checksum of the pseudo header must be calculated by
9610193eb5eSYaroslav Tykhiythe host CPU and added to
9620193eb5eSYaroslav Tykhiy.Va csum_data
963fbde705aSYaroslav Tykhiyto obtain the final checksum to be used for TCP or UDP validation purposes.
9640193eb5eSYaroslav Tykhiy.El
9650193eb5eSYaroslav Tykhiy.Pp
9660193eb5eSYaroslav TykhiyIf a particular network interface just indicates success or
9670193eb5eSYaroslav Tykhiyfailure of TCP or UDP checksum validation without returning
9680193eb5eSYaroslav Tykhiythe exact value of the checksum to the host CPU, its driver can mark
9690193eb5eSYaroslav Tykhiy.Dv CSUM_DATA_VALID
9700193eb5eSYaroslav Tykhiyand
9710193eb5eSYaroslav Tykhiy.Dv CSUM_PSEUDO_HDR
9720193eb5eSYaroslav Tykhiyin
9730193eb5eSYaroslav Tykhiy.Va csum_flags ,
9740193eb5eSYaroslav Tykhiyand set
9750193eb5eSYaroslav Tykhiy.Va csum_data
9760193eb5eSYaroslav Tykhiyto
9770193eb5eSYaroslav Tykhiy.Li 0xFFFF
9780193eb5eSYaroslav Tykhiyhexadecimal to indicate a valid checksum.
979f48cbcdeSRuslan ErmilovIt is a peculiarity of the algorithm used that the Internet checksum
9800193eb5eSYaroslav Tykhiycalculated over any valid packet will be
9810193eb5eSYaroslav Tykhiy.Li 0xFFFF
9820193eb5eSYaroslav Tykhiyas long as the original checksum field is included.
9830193eb5eSYaroslav Tykhiy.Pp
9840193eb5eSYaroslav TykhiyFor inbound packets which are IP fragments, all
9850193eb5eSYaroslav Tykhiy.Va csum_data
9860193eb5eSYaroslav Tykhiyfields will be summed during reassembly to obtain the final checksum
9870193eb5eSYaroslav Tykhiyvalue passed to an upper layer in the
9880193eb5eSYaroslav Tykhiy.Va csum_data
9890193eb5eSYaroslav Tykhiyfield of the reassembled packet.
9900193eb5eSYaroslav TykhiyThe
9910193eb5eSYaroslav Tykhiy.Va csum_flags
9920193eb5eSYaroslav Tykhiyfields of all fragments will be consolidated using logical AND
9930193eb5eSYaroslav Tykhiyto obtain the final value for
9940193eb5eSYaroslav Tykhiy.Va csum_flags .
9950193eb5eSYaroslav TykhiyThus, in order to successfully
9960193eb5eSYaroslav Tykhiyoffload checksum computation for fragmented data,
9970193eb5eSYaroslav Tykhiyall fragments should have the same value of
9980193eb5eSYaroslav Tykhiy.Va csum_flags .
9993cab047eSMike Silbersack.Sh STRESS TESTING
1000c8185672SRuslan ErmilovWhen running a kernel compiled with the option
1001c8185672SRuslan Ermilov.Dv MBUF_STRESS_TEST ,
1002c8185672SRuslan Ermilovthe following
1003c8185672SRuslan Ermilov.Xr sysctl 8 Ns
1004c8185672SRuslan Ermilov-controlled options may be used to create
10053cab047eSMike Silbersackvarious failure/extreme cases for testing of network drivers
10067b2fd183SYaroslav Tykhiyand other parts of the kernel that rely on
10077b2fd183SYaroslav Tykhiy.Vt mbufs .
1008c8185672SRuslan Ermilov.Bl -tag -width ident
1009c8185672SRuslan Ermilov.It Va net.inet.ip.mbuf_frag_size
1010c8185672SRuslan ErmilovCauses
1011c8185672SRuslan Ermilov.Fn ip_output
10127b2fd183SYaroslav Tykhiyto fragment outgoing
10137b2fd183SYaroslav Tykhiy.Vt mbuf chains
10147b2fd183SYaroslav Tykhiyinto fragments of the specified size.
1015c8185672SRuslan ErmilovSetting this variable to 1 is an excellent way to
10167b2fd183SYaroslav Tykhiytest the long
10177b2fd183SYaroslav Tykhiy.Vt mbuf chain
10187b2fd183SYaroslav Tykhiyhandling ability of network drivers.
1019c8185672SRuslan Ermilov.It Va kern.ipc.m_defragrandomfailures
1020c8185672SRuslan ErmilovCauses the function
1021c8185672SRuslan Ermilov.Fn m_defrag
1022c8185672SRuslan Ermilovto randomly fail, returning
1023c8185672SRuslan Ermilov.Dv NULL .
1024c8185672SRuslan ErmilovAny piece of code which uses
1025c8185672SRuslan Ermilov.Fn m_defrag
1026c8185672SRuslan Ermilovshould be tested with this feature.
1027c8185672SRuslan Ermilov.El
1028de06f907SNik Clayton.Sh RETURN VALUES
1029de06f907SNik ClaytonSee above.
10300193eb5eSYaroslav Tykhiy.Sh SEE ALSO
1031f48cbcdeSRuslan Ermilov.Xr ifnet 9
1032de06f907SNik Clayton.Sh HISTORY
1033de06f907SNik Clayton.\" Please correct me if I'm wrong
10347b2fd183SYaroslav Tykhiy.Vt Mbufs
10357b2fd183SYaroslav Tykhiyappeared in an early version of
1036753d686dSRuslan Ermilov.Bx .
103753b2520dSTom RhodesBesides being used for network packets, they were used
1038bfd59721SBosko Milekicto store various dynamic structures, such as routing table
1039bfd59721SBosko Milekicentries, interface addresses, protocol control blocks, etc.
1040de06f907SNik Clayton.Sh AUTHORS
10416575e6daSRuslan ErmilovThe original
10426575e6daSRuslan Ermilov.Nm
10436575e6daSRuslan Ermilovman page was written by Yar Tikhiy.
1044