xref: /freebsd/share/man/man9/mbuf.9 (revision 01e7fdafa77dc2280bfdb4e36b363bda98f3f439)
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.
53701e7fdafSMaxim Konovalov.It Fn m_free mbuf
53801e7fdafSMaxim KonovalovFrees
53901e7fdafSMaxim Konovalov.Vt mbuf .
540de06f907SNik Clayton.El
541de06f907SNik Clayton.Pp
5427b2fd183SYaroslav TykhiyThe functions below operate on
5437b2fd183SYaroslav Tykhiy.Vt mbuf chains .
544de06f907SNik Clayton.Bl -ohang -offset indent
545de06f907SNik Clayton.It Fn m_freem mbuf
5467b2fd183SYaroslav TykhiyFree an entire
5477b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
5487b2fd183SYaroslav Tykhiyincluding any external storage.
549de06f907SNik Clayton.\"
550de06f907SNik Clayton.It Fn m_adj mbuf len
551de06f907SNik ClaytonTrim
552de06f907SNik Clayton.Fa len
5537b2fd183SYaroslav Tykhiybytes from the head of an
5547b2fd183SYaroslav Tykhiy.Vt mbuf chain
5557b2fd183SYaroslav Tykhiyif
556de06f907SNik Clayton.Fa len
557de06f907SNik Claytonis positive, from the tail otherwise.
558de06f907SNik Clayton.\"
559de06f907SNik Clayton.It Fn m_prepend mbuf len how
5607b2fd183SYaroslav TykhiyAllocate a new
5617b2fd183SYaroslav Tykhiy.Vt mbuf
5627b2fd183SYaroslav Tykhiyand prepend it to the
5637b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
5647b2fd183SYaroslav Tykhiyhandle
565de06f907SNik Clayton.Dv M_PKTHDR
566de06f907SNik Claytonproperly.
567de06f907SNik Clayton.Sy Note :
5687b2fd183SYaroslav TykhiyIt doesn't allocate any
5697b2fd183SYaroslav Tykhiy.Vt mbuf clusters ,
5707b2fd183SYaroslav Tykhiyso
571de06f907SNik Clayton.Fa len
572de06f907SNik Claytonmust be less than
573de06f907SNik Clayton.Dv MLEN
574de06f907SNik Claytonor
575de06f907SNik Clayton.Dv MHLEN ,
576de06f907SNik Claytondepending on the
577d0353b83SRuslan Ermilov.Dv M_PKTHDR
578d0353b83SRuslan Ermilovflag setting.
579de06f907SNik Clayton.\"
580de06f907SNik Clayton.It Fn m_pullup mbuf len
581de06f907SNik ClaytonArrange that the first
582de06f907SNik Clayton.Fa len
5837b2fd183SYaroslav Tykhiybytes of an
5847b2fd183SYaroslav Tykhiy.Vt mbuf chain
5857b2fd183SYaroslav Tykhiyare contiguous and lay in the data area of
586de06f907SNik Clayton.Fa mbuf ,
587de06f907SNik Claytonso they are accessible with
588de06f907SNik Clayton.Fn mtod mbuf type .
5897b2fd183SYaroslav TykhiyReturn the new
5907b2fd183SYaroslav Tykhiy.Vt mbuf chain
5917b2fd183SYaroslav Tykhiyon success,
592de06f907SNik Clayton.Dv NULL
593de06f907SNik Claytonon failure
5947b2fd183SYaroslav Tykhiy(the
5957b2fd183SYaroslav Tykhiy.Vt mbuf chain
5967b2fd183SYaroslav Tykhiyis freed in this case).
597de06f907SNik Clayton.Sy Note :
5987b2fd183SYaroslav TykhiyIt doesn't allocate any
5997b2fd183SYaroslav Tykhiy.Vt mbuf clusters ,
6007b2fd183SYaroslav Tykhiyso
601de06f907SNik Clayton.Fa len
602de06f907SNik Claytonmust be less than
603de06f907SNik Clayton.Dv MHLEN .
604de06f907SNik Clayton.\"
605de06f907SNik Clayton.It Fn m_copym mbuf offset len how
6067b2fd183SYaroslav TykhiyMake a copy of an
6077b2fd183SYaroslav Tykhiy.Vt mbuf chain
6087b2fd183SYaroslav Tykhiystarting
609de06f907SNik Clayton.Fa offset
610de06f907SNik Claytonbytes from the beginning, continuing for
611de06f907SNik Clayton.Fa len
612e73145f6SSheldon Hearnbytes.
613e73145f6SSheldon HearnIf
614de06f907SNik Clayton.Fa len
615de06f907SNik Claytonis
616de06f907SNik Clayton.Dv M_COPYALL ,
6177b2fd183SYaroslav Tykhiycopy to the end of the
6187b2fd183SYaroslav Tykhiy.Vt mbuf chain .
619de06f907SNik Clayton.Sy Note :
6207b2fd183SYaroslav TykhiyThe copy is read-only, because the
6217b2fd183SYaroslav Tykhiy.Vt mbuf clusters
6227b2fd183SYaroslav Tykhiyare not copied, only their reference counts are incremented.
623de06f907SNik Clayton.\"
624de06f907SNik Clayton.It Fn m_copypacket mbuf how
625de06f907SNik ClaytonCopy an entire packet including header, which must be present.
626de06f907SNik ClaytonThis is an optimized version of the common case
627de06f907SNik Clayton.Fn m_copym mbuf 0 M_COPYALL how .
628de06f907SNik Clayton.Sy Note :
6297b2fd183SYaroslav Tykhiythe copy is read-only, because the
6307b2fd183SYaroslav Tykhiy.Vt mbuf clusters
6317b2fd183SYaroslav Tykhiyare not copied, only their reference counts are incremented.
632de06f907SNik Clayton.\"
633de06f907SNik Clayton.It Fn m_dup mbuf how
6347b2fd183SYaroslav TykhiyCopy a packet header
6357b2fd183SYaroslav Tykhiy.Vt mbuf chain
6367b2fd183SYaroslav Tykhiyinto a completely new
6377b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
6387b2fd183SYaroslav Tykhiyincluding copying any
6397b2fd183SYaroslav Tykhiy.Vt mbuf clusters .
640e73145f6SSheldon HearnUse this instead of
641de06f907SNik Clayton.Fn m_copypacket
6427b2fd183SYaroslav Tykhiywhen you need a writable copy of an
6437b2fd183SYaroslav Tykhiy.Vt mbuf chain .
644de06f907SNik Clayton.\"
645de06f907SNik Clayton.It Fn m_copydata mbuf offset len buf
6467b2fd183SYaroslav TykhiyCopy data from an
6477b2fd183SYaroslav Tykhiy.Vt mbuf chain
6487b2fd183SYaroslav Tykhiystarting
649de06f907SNik Clayton.Fa off
650de06f907SNik Claytonbytes from the beginning, continuing for
651de06f907SNik Clayton.Fa len
652de06f907SNik Claytonbytes, into the indicated buffer
653de06f907SNik Clayton.Fa buf .
654de06f907SNik Clayton.\"
655de06f907SNik Clayton.It Fn m_copyback mbuf offset len buf
656de06f907SNik ClaytonCopy
657de06f907SNik Clayton.Fa len
658de06f907SNik Claytonbytes from the buffer
659de06f907SNik Clayton.Fa buf
6607b2fd183SYaroslav Tykhiyback into the indicated
6617b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
662de06f907SNik Claytonstarting at
663de06f907SNik Clayton.Fa offset
6647b2fd183SYaroslav Tykhiybytes from the beginning of the
6657b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
6667b2fd183SYaroslav Tykhiyextending the
6677b2fd183SYaroslav Tykhiy.Vt mbuf chain
6687b2fd183SYaroslav Tykhiyif necessary.
669de06f907SNik Clayton.Sy Note :
6707b2fd183SYaroslav TykhiyIt doesn't allocate any
6717b2fd183SYaroslav Tykhiy.Vt mbuf clusters ,
6727b2fd183SYaroslav Tykhiyjust adds
6737b2fd183SYaroslav Tykhiy.Vt mbufs
6747b2fd183SYaroslav Tykhiyto the
6757b2fd183SYaroslav Tykhiy.Vt mbuf chain .
676e73145f6SSheldon HearnIt's safe to set
677de06f907SNik Clayton.Fa offset
6787b2fd183SYaroslav Tykhiybeyond the current
6797b2fd183SYaroslav Tykhiy.Vt mbuf chain
6807b2fd183SYaroslav Tykhiyend: zeroed
6817b2fd183SYaroslav Tykhiy.Vt mbufs
6827b2fd183SYaroslav Tykhiywill be allocated to fill the space.
683de06f907SNik Clayton.\"
6845d96084fSYaroslav Tykhiy.It Fn m_length mbuf last
6857b2fd183SYaroslav TykhiyReturn the length of the
6867b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
6877b2fd183SYaroslav Tykhiyand optionally a pointer to the last
6887b2fd183SYaroslav Tykhiy.Vt mbuf .
6890b1c0994SPoul-Henning Kamp.\"
690da72b998SYaroslav Tykhiy.It Fn m_dup_pkthdr to from how
691da72b998SYaroslav TykhiyUpon the function's completion, the
692da72b998SYaroslav Tykhiy.Vt mbuf
693da72b998SYaroslav Tykhiy.Fa to
694da72b998SYaroslav Tykhiywill contain an identical copy of
695da72b998SYaroslav Tykhiy.Fa from->m_pkthdr
696da72b998SYaroslav Tykhiyand the per-packet attributes found in the
697da72b998SYaroslav Tykhiy.Vt mbuf chain
698da72b998SYaroslav Tykhiy.Fa from .
699da72b998SYaroslav TykhiyThe
700da72b998SYaroslav Tykhiy.Vt mbuf
701da72b998SYaroslav Tykhiy.Fa from
702da72b998SYaroslav Tykhiymust have the flag
703da72b998SYaroslav Tykhiy.Dv M_PKTHDR
704da72b998SYaroslav Tykhiyinitially set, and
705da72b998SYaroslav Tykhiy.Fa to
706da72b998SYaroslav Tykhiymust be empty on entry.
707da72b998SYaroslav Tykhiy.\"
708da72b998SYaroslav Tykhiy.It Fn m_move_pkthdr to from
709da72b998SYaroslav TykhiyMove
710da72b998SYaroslav Tykhiy.Va m_pkthdr
711da72b998SYaroslav Tykhiyand the per-packet attributes from the
712da72b998SYaroslav Tykhiy.Vt mbuf chain
713da72b998SYaroslav Tykhiy.Fa from
714da72b998SYaroslav Tykhiyto the
715da72b998SYaroslav Tykhiy.Vt mbuf
716da72b998SYaroslav Tykhiy.Fa to .
717da72b998SYaroslav TykhiyThe
718da72b998SYaroslav Tykhiy.Vt mbuf
719da72b998SYaroslav Tykhiy.Fa from
720da72b998SYaroslav Tykhiymust have the flag
721da72b998SYaroslav Tykhiy.Dv M_PKTHDR
722da72b998SYaroslav Tykhiyinitially set, and
723da72b998SYaroslav Tykhiy.Fa to
724da72b998SYaroslav Tykhiymust be empty on entry.
725da72b998SYaroslav TykhiyUpon the function's completion,
726da72b998SYaroslav Tykhiy.Fa from
727da72b998SYaroslav Tykhiywill have the flag
728da72b998SYaroslav Tykhiy.Dv M_PKTHDR
729da72b998SYaroslav Tykhiyand the per-packet attributes cleared.
730da72b998SYaroslav Tykhiy.\"
7315d96084fSYaroslav Tykhiy.It Fn m_fixhdr mbuf
7327b2fd183SYaroslav TykhiySet the packet-header length to the length of the
7337b2fd183SYaroslav Tykhiy.Vt mbuf chain .
7340b1c0994SPoul-Henning Kamp.\"
735de06f907SNik Clayton.It Fn m_devget buf len offset ifp copy
736de06f907SNik ClaytonCopy data from a device local memory pointed to by
737de06f907SNik Clayton.Fa buf
7387b2fd183SYaroslav Tykhiyto an
7397b2fd183SYaroslav Tykhiy.Vt mbuf chain .
740e73145f6SSheldon HearnThe copy is done using a specified copy routine
741de06f907SNik Clayton.Fa copy ,
742de06f907SNik Claytonor
743de06f907SNik Clayton.Fn bcopy
744de06f907SNik Claytonif
745de06f907SNik Clayton.Fa copy
746de06f907SNik Claytonis
747de06f907SNik Clayton.Dv NULL .
748de06f907SNik Clayton.\"
749de06f907SNik Clayton.It Fn m_cat m n
750de06f907SNik ClaytonConcatenate
751de06f907SNik Clayton.Fa n
752de06f907SNik Claytonto
753de06f907SNik Clayton.Fa m .
7547b2fd183SYaroslav TykhiyBoth
7557b2fd183SYaroslav Tykhiy.Vt mbuf chains
7567b2fd183SYaroslav Tykhiymust be of the same type.
757de06f907SNik Clayton.Fa N
758de06f907SNik Claytonis still valid after the function returned.
759de06f907SNik Clayton.Sy Note :
760de06f907SNik ClaytonIt does not handle
761de06f907SNik Clayton.Dv M_PKTHDR
762de06f907SNik Claytonand friends.
763de06f907SNik Clayton.\"
764de06f907SNik Clayton.It Fn m_split mbuf len how
7657b2fd183SYaroslav TykhiyPartition an
7667b2fd183SYaroslav Tykhiy.Vt mbuf chain
7677b2fd183SYaroslav Tykhiyin two pieces, returning the tail:
768de06f907SNik Claytonall but the first
769de06f907SNik Clayton.Fa len
770e73145f6SSheldon Hearnbytes.
771e73145f6SSheldon HearnIn case of failure, it returns
772de06f907SNik Clayton.Dv NULL
7737b2fd183SYaroslav Tykhiyand attempts to restore the
7747b2fd183SYaroslav Tykhiy.Vt mbuf chain
7757b2fd183SYaroslav Tykhiyto its original state.
77602312219SBruce M Simpson.\"
77702312219SBruce M Simpson.It Fn m_apply mbuf off len f arg
77802312219SBruce M SimpsonApply a function to an
77902312219SBruce M Simpson.Vt mbuf chain ,
78002312219SBruce M Simpsonat offset
78102312219SBruce M Simpson.Fa off ,
78202312219SBruce M Simpsonfor length
783f48cbcdeSRuslan Ermilov.Fa len
784f48cbcdeSRuslan Ermilovbytes.
78502312219SBruce M SimpsonTypically used to avoid calls to
78602312219SBruce M Simpson.Fn m_pullup
78702312219SBruce M Simpsonwhich would otherwise be unnecessary or undesirable.
78802312219SBruce M Simpson.Fa arg
78902312219SBruce M Simpsonis a convenience argument which is passed to the callback function
79002312219SBruce M Simpson.Fa f .
79102312219SBruce M Simpson.Pp
79202312219SBruce M SimpsonEach time
79302312219SBruce M Simpson.Fn f
79402312219SBruce M Simpsonis called, it will be passed
79502312219SBruce M Simpson.Fa arg ,
79602312219SBruce M Simpsona pointer to the
79702312219SBruce M Simpson.Fa data
79802312219SBruce M Simpsonin the current mbuf, and the length
79902312219SBruce M Simpson.Fa len
80002312219SBruce M Simpsonof the data in this mbuf to which the function should be applied.
80102312219SBruce M Simpson.Pp
80202312219SBruce M SimpsonThe function should return zero to indicate success;
80302312219SBruce M Simpsonotherwise, if an error is indicated, then
80402312219SBruce M Simpson.Fn m_apply
80502312219SBruce M Simpsonwill return the error and stop iterating through the
80602312219SBruce M Simpson.Vt mbuf chain .
80702312219SBruce M Simpson.\"
80802312219SBruce M Simpson.It Fn m_getptr mbuf loc off
80902312219SBruce M SimpsonReturn a pointer to the mbuf containing the data located at
81002312219SBruce M Simpson.Fa loc
81102312219SBruce M Simpsonbytes from the beginning of the
81202312219SBruce M Simpson.Vt mbuf chain .
81302312219SBruce M SimpsonThe corresponding offset into the mbuf will be stored in
81402312219SBruce M Simpson.Fa *off .
815a61bc764SMike Silbersack.It Fn m_defrag m0 how
816f48cbcdeSRuslan ErmilovDefragment an mbuf chain, returning the shortest possible
817bdf86185SMike Silbersackchain of mbufs and clusters.
818bdf86185SMike SilbersackIf allocation fails and this can not be completed,
819bdf86185SMike Silbersack.Dv NULL
820bdf86185SMike Silbersackwill be returned and the original chain will be unchanged.
821bdf86185SMike SilbersackUpon success, the original chain will be freed and the new
822bdf86185SMike Silbersackchain will be returned.
823a61bc764SMike Silbersack.Fa how
824a61bc764SMike Silbersackshould be either
825a61bc764SMike Silbersack.Dv M_TRYWAIT
826a61bc764SMike Silbersackor
827a61bc764SMike Silbersack.Dv M_DONTWAIT ,
828a61bc764SMike Silbersackdepending on the caller's preference.
829a61bc764SMike Silbersack.Pp
830a61bc764SMike SilbersackThis function is especially useful in network drivers, where
831a61bc764SMike Silbersackcertain long mbuf chains must be shortened before being added
832a61bc764SMike Silbersackto TX descriptor lists.
8333136363fSRuslan Ermilov.El
8340193eb5eSYaroslav Tykhiy.Sh HARDWARE-ASSISTED CHECKSUM CALCULATION
8350193eb5eSYaroslav TykhiyThis section currently applies to TCP/IP only.
8360193eb5eSYaroslav TykhiyIn order to save the host CPU resources, computing checksums is
8370193eb5eSYaroslav Tykhiyoffloaded to the network interface hardware if possible.
8380193eb5eSYaroslav TykhiyThe
8390193eb5eSYaroslav Tykhiy.Va m_pkthdr
8400193eb5eSYaroslav Tykhiymember of the leading
8410193eb5eSYaroslav Tykhiy.Vt mbuf
8420193eb5eSYaroslav Tykhiyof a packet contains two fields used for that purpose,
8430193eb5eSYaroslav Tykhiy.Vt int Va csum_flags
8440193eb5eSYaroslav Tykhiyand
8450193eb5eSYaroslav Tykhiy.Vt int Va csum_data .
8460193eb5eSYaroslav TykhiyThe meaning of those fields depends on the direction a packet flows in,
8470193eb5eSYaroslav Tykhiyand on whether the packet is fragmented.
8480193eb5eSYaroslav TykhiyHenceforth,
8490193eb5eSYaroslav Tykhiy.Va csum_flags
8500193eb5eSYaroslav Tykhiyor
8510193eb5eSYaroslav Tykhiy.Va csum_data
8520193eb5eSYaroslav Tykhiyof a packet
8530193eb5eSYaroslav Tykhiywill denote the corresponding field of the
8540193eb5eSYaroslav Tykhiy.Va m_pkthdr
8550193eb5eSYaroslav Tykhiymember of the leading
8560193eb5eSYaroslav Tykhiy.Vt mbuf
8570193eb5eSYaroslav Tykhiyin the
8580193eb5eSYaroslav Tykhiy.Vt mbuf chain
8590193eb5eSYaroslav Tykhiycontaining the packet.
8600193eb5eSYaroslav Tykhiy.Pp
8610193eb5eSYaroslav TykhiyOn output, checksum offloading is attempted after the outgoing
8620193eb5eSYaroslav Tykhiyinterface has been determined for a packet.
8630193eb5eSYaroslav TykhiyThe interface-specific field
8640193eb5eSYaroslav Tykhiy.Va ifnet.if_data.ifi_hwassist
8650193eb5eSYaroslav Tykhiy(see
8660193eb5eSYaroslav Tykhiy.Xr ifnet 9 )
8670193eb5eSYaroslav Tykhiyis consulted for the capabilities of the interface to assist in
8680193eb5eSYaroslav Tykhiycomputing checksums.
8690193eb5eSYaroslav TykhiyThe
8700193eb5eSYaroslav Tykhiy.Va csum_flags
871f48cbcdeSRuslan Ermilovfield of the packet header is set to indicate which actions the interface
8720193eb5eSYaroslav Tykhiyis supposed to perform on it.
8730193eb5eSYaroslav TykhiyThe actions unsupported by the network interface are done in the
8740193eb5eSYaroslav Tykhiysoftware prior to passing the packet down to the interface driver;
8750193eb5eSYaroslav Tykhiysuch actions will never be requested through
8760193eb5eSYaroslav Tykhiy.Va csum_flags .
8770193eb5eSYaroslav Tykhiy.Pp
8780193eb5eSYaroslav TykhiyThe flags demanding a particular action from an interface are as follows:
8790193eb5eSYaroslav Tykhiy.Bl -tag -width ".Dv CSUM_TCP" -offset indent
8800193eb5eSYaroslav Tykhiy.It Dv CSUM_IP
8810193eb5eSYaroslav TykhiyThe IP header checksum is to be computed and stored in the
8820193eb5eSYaroslav Tykhiycorresponding field of the packet.
8830193eb5eSYaroslav TykhiyThe hardware is expected to know the format of an IP header
8840193eb5eSYaroslav Tykhiyto determine the offset of the IP checksum field.
8850193eb5eSYaroslav Tykhiy.It Dv CSUM_TCP
886f48cbcdeSRuslan ErmilovThe TCP checksum is to be computed.
887f48cbcdeSRuslan Ermilov(See below.)
8880193eb5eSYaroslav Tykhiy.It Dv CSUM_UDP
889f48cbcdeSRuslan ErmilovThe UDP checksum is to be computed.
890f48cbcdeSRuslan Ermilov(See below.)
8910193eb5eSYaroslav Tykhiy.El
8920193eb5eSYaroslav Tykhiy.Pp
893fbde705aSYaroslav TykhiyShould a TCP or UDP checksum be offloaded to the hardware,
8940193eb5eSYaroslav Tykhiythe field
8950193eb5eSYaroslav Tykhiy.Va csum_data
8960193eb5eSYaroslav Tykhiywill contain the byte offset of the checksum field relative to the
8970193eb5eSYaroslav Tykhiyend of the IP header.
8980193eb5eSYaroslav TykhiyIn this case, the checksum field will be initially
8990193eb5eSYaroslav Tykhiyset by the TCP/IP module to the checksum of the pseudo header
900fbde705aSYaroslav Tykhiydefined by the TCP and UDP specifications.
9010193eb5eSYaroslav Tykhiy.Pp
9020193eb5eSYaroslav TykhiyFor outbound packets which have been fragmented
9030193eb5eSYaroslav Tykhiyby the host CPU, the following will also be true,
9040193eb5eSYaroslav Tykhiyregardless of the checksum flag settings:
9050193eb5eSYaroslav Tykhiy.Bl -bullet -offset indent
9060193eb5eSYaroslav Tykhiy.It
9070193eb5eSYaroslav Tykhiyall fragments will have the flag
9080193eb5eSYaroslav Tykhiy.Dv M_FRAG
9090193eb5eSYaroslav Tykhiyset in their
9100193eb5eSYaroslav Tykhiy.Va m_flags
9110193eb5eSYaroslav Tykhiyfield;
9120193eb5eSYaroslav Tykhiy.It
9130193eb5eSYaroslav Tykhiythe first and the last fragments in the chain will have
9140193eb5eSYaroslav Tykhiy.Dv M_FIRSTFRAG
9150193eb5eSYaroslav Tykhiyor
9160193eb5eSYaroslav Tykhiy.Dv M_LASTFRAG
9170193eb5eSYaroslav Tykhiyset in their
9180193eb5eSYaroslav Tykhiy.Va m_flags ,
9190193eb5eSYaroslav Tykhiycorrespondingly;
9200193eb5eSYaroslav Tykhiy.It
9210193eb5eSYaroslav Tykhiythe first fragment in the chain will have the total number
9220193eb5eSYaroslav Tykhiyof fragments contained in its
9230193eb5eSYaroslav Tykhiy.Va csum_data
9240193eb5eSYaroslav Tykhiyfield.
9250193eb5eSYaroslav Tykhiy.El
9260193eb5eSYaroslav Tykhiy.Pp
92776f80df6SYaroslav TykhiyThe last rule for fragmented packets takes precedence over the one
92876f80df6SYaroslav Tykhiyfor a TCP or UDP checksum.
92976f80df6SYaroslav TykhiyNevertheless, offloading a TCP or UDP checksum is possible for a
93076f80df6SYaroslav Tykhiyfragmented packet if the flag
93176f80df6SYaroslav Tykhiy.Dv CSUM_IP_FRAGS
93276f80df6SYaroslav Tykhiyis set in the field
93376f80df6SYaroslav Tykhiy.Va ifnet.if_data.ifi_hwassist
93476f80df6SYaroslav Tykhiyassociated with the network interface.
93576f80df6SYaroslav TykhiyHowever, in this case the interface is expected to figure out
93676f80df6SYaroslav Tykhiythe location of the checksum field within the sequence of fragments
93776f80df6SYaroslav Tykhiyby itself because
9380193eb5eSYaroslav Tykhiy.Va csum_data
93976f80df6SYaroslav Tykhiycontains a fragment count instead of a checksum offset value.
9400193eb5eSYaroslav Tykhiy.Pp
9410193eb5eSYaroslav TykhiyOn input, an interface indicates the actions it has performed
9420193eb5eSYaroslav Tykhiyon a packet by setting one or more of the following flags in
9430193eb5eSYaroslav Tykhiy.Va csum_flags
9440193eb5eSYaroslav Tykhiyassociated with the packet:
9450193eb5eSYaroslav Tykhiy.Bl -tag -width ".Dv CSUM_IP_CHECKED" -offset indent
9460193eb5eSYaroslav Tykhiy.It Dv CSUM_IP_CHECKED
9470193eb5eSYaroslav TykhiyThe IP header checksum has been computed.
9480193eb5eSYaroslav Tykhiy.It Dv CSUM_IP_VALID
9490193eb5eSYaroslav TykhiyThe IP header has a valid checksum.
9500193eb5eSYaroslav TykhiyThis flag can appear only in combination with
9510193eb5eSYaroslav Tykhiy.Dv CSUM_IP_CHECKED .
9520193eb5eSYaroslav Tykhiy.It Dv CSUM_DATA_VALID
9530193eb5eSYaroslav TykhiyThe checksum of the data portion of the IP packet has been computed
9540193eb5eSYaroslav Tykhiyand stored in the field
9550193eb5eSYaroslav Tykhiy.Va csum_data
9560193eb5eSYaroslav Tykhiyin network byte order.
9570193eb5eSYaroslav Tykhiy.It Dv CSUM_PSEUDO_HDR
9580193eb5eSYaroslav TykhiyCan be set only along with
9590193eb5eSYaroslav Tykhiy.Dv CSUM_DATA_VALID
9600193eb5eSYaroslav Tykhiyto indicate that the IP data checksum found in
9610193eb5eSYaroslav Tykhiy.Va csum_data
962fbde705aSYaroslav Tykhiyallows for the pseudo header defined by the TCP and UDP specifications.
9630193eb5eSYaroslav TykhiyOtherwise the checksum of the pseudo header must be calculated by
9640193eb5eSYaroslav Tykhiythe host CPU and added to
9650193eb5eSYaroslav Tykhiy.Va csum_data
966fbde705aSYaroslav Tykhiyto obtain the final checksum to be used for TCP or UDP validation purposes.
9670193eb5eSYaroslav Tykhiy.El
9680193eb5eSYaroslav Tykhiy.Pp
9690193eb5eSYaroslav TykhiyIf a particular network interface just indicates success or
9700193eb5eSYaroslav Tykhiyfailure of TCP or UDP checksum validation without returning
9710193eb5eSYaroslav Tykhiythe exact value of the checksum to the host CPU, its driver can mark
9720193eb5eSYaroslav Tykhiy.Dv CSUM_DATA_VALID
9730193eb5eSYaroslav Tykhiyand
9740193eb5eSYaroslav Tykhiy.Dv CSUM_PSEUDO_HDR
9750193eb5eSYaroslav Tykhiyin
9760193eb5eSYaroslav Tykhiy.Va csum_flags ,
9770193eb5eSYaroslav Tykhiyand set
9780193eb5eSYaroslav Tykhiy.Va csum_data
9790193eb5eSYaroslav Tykhiyto
9800193eb5eSYaroslav Tykhiy.Li 0xFFFF
9810193eb5eSYaroslav Tykhiyhexadecimal to indicate a valid checksum.
982f48cbcdeSRuslan ErmilovIt is a peculiarity of the algorithm used that the Internet checksum
9830193eb5eSYaroslav Tykhiycalculated over any valid packet will be
9840193eb5eSYaroslav Tykhiy.Li 0xFFFF
9850193eb5eSYaroslav Tykhiyas long as the original checksum field is included.
9860193eb5eSYaroslav Tykhiy.Pp
9870193eb5eSYaroslav TykhiyFor inbound packets which are IP fragments, all
9880193eb5eSYaroslav Tykhiy.Va csum_data
9890193eb5eSYaroslav Tykhiyfields will be summed during reassembly to obtain the final checksum
9900193eb5eSYaroslav Tykhiyvalue passed to an upper layer in the
9910193eb5eSYaroslav Tykhiy.Va csum_data
9920193eb5eSYaroslav Tykhiyfield of the reassembled packet.
9930193eb5eSYaroslav TykhiyThe
9940193eb5eSYaroslav Tykhiy.Va csum_flags
9950193eb5eSYaroslav Tykhiyfields of all fragments will be consolidated using logical AND
9960193eb5eSYaroslav Tykhiyto obtain the final value for
9970193eb5eSYaroslav Tykhiy.Va csum_flags .
9980193eb5eSYaroslav TykhiyThus, in order to successfully
9990193eb5eSYaroslav Tykhiyoffload checksum computation for fragmented data,
10000193eb5eSYaroslav Tykhiyall fragments should have the same value of
10010193eb5eSYaroslav Tykhiy.Va csum_flags .
10023cab047eSMike Silbersack.Sh STRESS TESTING
1003c8185672SRuslan ErmilovWhen running a kernel compiled with the option
1004c8185672SRuslan Ermilov.Dv MBUF_STRESS_TEST ,
1005c8185672SRuslan Ermilovthe following
1006c8185672SRuslan Ermilov.Xr sysctl 8 Ns
1007c8185672SRuslan Ermilov-controlled options may be used to create
10083cab047eSMike Silbersackvarious failure/extreme cases for testing of network drivers
10097b2fd183SYaroslav Tykhiyand other parts of the kernel that rely on
10107b2fd183SYaroslav Tykhiy.Vt mbufs .
1011c8185672SRuslan Ermilov.Bl -tag -width ident
1012c8185672SRuslan Ermilov.It Va net.inet.ip.mbuf_frag_size
1013c8185672SRuslan ErmilovCauses
1014c8185672SRuslan Ermilov.Fn ip_output
10157b2fd183SYaroslav Tykhiyto fragment outgoing
10167b2fd183SYaroslav Tykhiy.Vt mbuf chains
10177b2fd183SYaroslav Tykhiyinto fragments of the specified size.
1018c8185672SRuslan ErmilovSetting this variable to 1 is an excellent way to
10197b2fd183SYaroslav Tykhiytest the long
10207b2fd183SYaroslav Tykhiy.Vt mbuf chain
10217b2fd183SYaroslav Tykhiyhandling ability of network drivers.
1022c8185672SRuslan Ermilov.It Va kern.ipc.m_defragrandomfailures
1023c8185672SRuslan ErmilovCauses the function
1024c8185672SRuslan Ermilov.Fn m_defrag
1025c8185672SRuslan Ermilovto randomly fail, returning
1026c8185672SRuslan Ermilov.Dv NULL .
1027c8185672SRuslan ErmilovAny piece of code which uses
1028c8185672SRuslan Ermilov.Fn m_defrag
1029c8185672SRuslan Ermilovshould be tested with this feature.
1030c8185672SRuslan Ermilov.El
1031de06f907SNik Clayton.Sh RETURN VALUES
1032de06f907SNik ClaytonSee above.
10330193eb5eSYaroslav Tykhiy.Sh SEE ALSO
1034f48cbcdeSRuslan Ermilov.Xr ifnet 9
1035de06f907SNik Clayton.Sh HISTORY
1036de06f907SNik Clayton.\" Please correct me if I'm wrong
10377b2fd183SYaroslav Tykhiy.Vt Mbufs
10387b2fd183SYaroslav Tykhiyappeared in an early version of
1039753d686dSRuslan Ermilov.Bx .
104053b2520dSTom RhodesBesides being used for network packets, they were used
1041bfd59721SBosko Milekicto store various dynamic structures, such as routing table
1042bfd59721SBosko Milekicentries, interface addresses, protocol control blocks, etc.
1043de06f907SNik Clayton.Sh AUTHORS
10446575e6daSRuslan ErmilovThe original
10456575e6daSRuslan Ermilov.Nm
10466575e6daSRuslan Ermilovman page was written by Yar Tikhiy.
1047