xref: /freebsd/share/man/man9/mbuf.9 (revision c8185672b28957105341b2ce95fd497e209ad64c)
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.Ft void *
60f16b3c0dSChad David.Fn mtod "struct mbuf *mbuf" "type"
61f16b3c0dSChad David.Ft int
62bfd59721SBosko Milekic.Fn MEXT_IS_REF "struct mbuf *mbuf"
63bfd59721SBosko Milekic.Fn M_COPY_PKTHDR "struct mbuf *to" "struct mbuf *from"
64bfd59721SBosko Milekic.Fn M_ALIGN "struct mbuf *mbuf" "u_int len"
65bfd59721SBosko Milekic.Fn MH_ALIGN "struct mbuf *mbuf" "u_int len"
66f16b3c0dSChad David.Ft int
67bfd59721SBosko Milekic.Fn M_LEADINGSPACE "struct mbuf *mbuf"
68f16b3c0dSChad David.Ft int
69bfd59721SBosko Milekic.Fn M_TRAILINGSPACE "struct mbuf *mbuf"
70bfd59721SBosko Milekic.Fn M_PREPEND "struct mbuf *mbuf" "int len" "int how"
71bfd59721SBosko Milekic.Fn MCHTYPE "struct mbuf *mbuf" "u_int type"
72f16b3c0dSChad David.Ft int
735d4050ccSBosko Milekic.Fn M_WRITABLE "struct mbuf *mbuf"
74bfd59721SBosko Milekic.\"
75bfd59721SBosko Milekic.Ss Mbuf allocation functions
76de06f907SNik Clayton.Ft struct mbuf *
77de06f907SNik Clayton.Fn m_get "int how" "int type"
78de06f907SNik Clayton.Ft struct mbuf *
79328c0bbcSBosko Milekic.Fn m_getm "struct mbuf *orig" "int len" "int how" "int type"
80328c0bbcSBosko Milekic.Ft struct mbuf *
81de06f907SNik Clayton.Fn m_getclr "int how" "int type"
82de06f907SNik Clayton.Ft struct mbuf *
83de06f907SNik Clayton.Fn m_gethdr "int how" "int type"
84bfd59721SBosko Milekic.Ft struct mbuf *
85bfd59721SBosko Milekic.Fn m_free "struct mbuf *mbuf"
86de06f907SNik Clayton.Ft void
87de06f907SNik Clayton.Fn m_freem "struct mbuf *mbuf"
88bfd59721SBosko Milekic.\"
89bfd59721SBosko Milekic.Ss Mbuf utility functions
90de06f907SNik Clayton.Ft void
91de06f907SNik Clayton.Fn m_adj "struct mbuf *mbuf" "int len"
92de06f907SNik Clayton.Ft struct mbuf *
93de06f907SNik Clayton.Fn m_prepend "struct mbuf *mbuf" "int len" "int how"
94de06f907SNik Clayton.Ft struct mbuf *
95de06f907SNik Clayton.Fn m_pullup "struct mbuf *mbuf" "int len"
96de06f907SNik Clayton.Ft struct mbuf *
97de06f907SNik Clayton.Fn m_copym "struct mbuf *mbuf" "int offset" "int len" "int how"
98de06f907SNik Clayton.Ft struct mbuf *
99de06f907SNik Clayton.Fn m_copypacket "struct mbuf *mbuf" "int how"
100de06f907SNik Clayton.Ft struct mbuf *
101de06f907SNik Clayton.Fn m_dup "struct mbuf *mbuf" "int how"
102de06f907SNik Clayton.Ft void
1030d8e4c57SBruce Evans.Fn m_copydata "const struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
104de06f907SNik Clayton.Ft void
105de06f907SNik Clayton.Fn m_copyback "struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
106de06f907SNik Clayton.Ft struct mbuf *
107de06f907SNik Clayton.Fo m_devget
108de06f907SNik Clayton.Fa "char *buf"
109de06f907SNik Clayton.Fa "int len"
110de06f907SNik Clayton.Fa "int offset"
111de06f907SNik Clayton.Fa "struct ifnet *ifp"
112de06f907SNik Clayton.Fa "void (*copy)(char *from, caddr_t to, u_int len)"
113de06f907SNik Clayton.Fc
114de06f907SNik Clayton.Ft void
115de06f907SNik Clayton.Fn m_cat "struct mbuf *m" "struct mbuf *n"
116e60fa837SRuslan Ermilov.Ft u_int
1170b1c0994SPoul-Henning Kamp.Fn m_fixhdr "struct mbuf *mbuf"
118e60fa837SRuslan Ermilov.Ft u_int
1190b1c0994SPoul-Henning Kamp.Fn m_length "struct mbuf *mbuf" "struct mbuf **last"
120de06f907SNik Clayton.Ft struct mbuf *
121de06f907SNik Clayton.Fn m_split "struct mbuf *mbuf" "int len" "int how"
122de06f907SNik Clayton.\"
123de06f907SNik Clayton.Sh DESCRIPTION
124bfd59721SBosko MilekicAn mbuf is a basic unit of memory management in the kernel IPC subsystem.
125e73145f6SSheldon HearnNetwork packets and socket buffers are stored in mbufs.
126e73145f6SSheldon HearnA network packet may span multiple mbufs arranged into a chain
127c4d9468eSRuslan Ermilov(linked list),
128de06f907SNik Claytonwhich allows adding or trimming
129bfd59721SBosko Milekicnetwork headers with little overhead.
130de06f907SNik Clayton.Pp
131bfd59721SBosko MilekicWhile a developer should not bother with mbuf internals without serious
132de06f907SNik Claytonreason in order to avoid incompatibilities with future changes, it
133bfd59721SBosko Milekicis useful to understand the mbuf's general structure.
134de06f907SNik Clayton.Pp
135bfd59721SBosko MilekicAn mbuf consists of a variable-sized header and a small internal
136e73145f6SSheldon Hearnbuffer for data.
137e73145f6SSheldon HearnThe mbuf's total size,
138bfd59721SBosko Milekic.Dv MSIZE ,
139de06f907SNik Claytonis a machine-dependent constant defined in
140de06f907SNik Clayton.Pa machine/param.h .
141de06f907SNik ClaytonThe mbuf header includes:
142de06f907SNik Clayton.Pp
143de06f907SNik Clayton.Bl -tag -width "m_nextpkt" -compact -offset indent
144de06f907SNik Clayton.It Fa m_next
145de06f907SNik Claytona pointer to the next buffer in the chain
146de06f907SNik Clayton.It Fa m_nextpkt
147de06f907SNik Claytona pointer to the next chain in the queue
148de06f907SNik Clayton.It Fa m_data
149de06f907SNik Claytona pointer to the data
150de06f907SNik Clayton.It Fa m_len
151de06f907SNik Claytonthe length of the data
152de06f907SNik Clayton.It Fa m_type
153bfd59721SBosko Milekicthe type of data
154de06f907SNik Clayton.It Fa m_flags
155de06f907SNik Claytonthe mbuf flags
156de06f907SNik Clayton.El
157de06f907SNik Clayton.Pp
158de06f907SNik ClaytonThe mbuf flag bits are defined as follows:
159de06f907SNik Clayton.Bd -literal
160de06f907SNik Clayton/* mbuf flags */
161de06f907SNik Clayton#define	M_EXT		0x0001	/* has associated external storage */
162de06f907SNik Clayton#define	M_PKTHDR	0x0002	/* start of record */
163de06f907SNik Clayton#define	M_EOR		0x0004	/* end of record */
1645d4050ccSBosko Milekic#define	M_RDONLY	0x0008	/* associated data marked read-only */
1655d4050ccSBosko Milekic#define	M_PROTO1	0x0010	/* protocol-specific */
1665d4050ccSBosko Milekic#define	M_PROTO2	0x0020 	/* protocol-specific */
1675d4050ccSBosko Milekic#define	M_PROTO3	0x0040	/* protocol-specific */
1685d4050ccSBosko Milekic#define	M_PROTO4	0x0080	/* protocol-specific */
1695d4050ccSBosko Milekic#define	M_PROTO5	0x0100	/* protocol-specific */
170de06f907SNik Clayton
171de06f907SNik Clayton/* mbuf pkthdr flags, also in m_flags */
1725d4050ccSBosko Milekic#define	M_BCAST		0x0200	/* send/received as link-level broadcast */
1735d4050ccSBosko Milekic#define	M_MCAST		0x0400	/* send/received as link-level multicast */
1745d4050ccSBosko Milekic#define	M_FRAG		0x0800	/* packet is fragment of larger packet */
1755d4050ccSBosko Milekic#define	M_FIRSTFRAG	0x1000	/* packet is first fragment */
1765d4050ccSBosko Milekic#define	M_LASTFRAG	0x2000	/* packet is last fragment */
177de06f907SNik Clayton.Ed
178de06f907SNik Clayton.Pp
179bfd59721SBosko MilekicThe available mbuf types are defined as follows:
180de06f907SNik Clayton.Bd -literal
181de06f907SNik Clayton/* mbuf types */
182de06f907SNik Clayton#define	MT_FREE		0	/* should be on free list */
183de06f907SNik Clayton#define	MT_DATA		1	/* dynamic (data) allocation */
184de06f907SNik Clayton#define	MT_HEADER	2	/* packet header */
185de06f907SNik Clayton#define	MT_SONAME	8	/* socket name */
186de06f907SNik Clayton#define	MT_FTABLE	11	/* fragment reassembly header */
187de06f907SNik Clayton#define	MT_CONTROL	14	/* extra-data protocol message */
188de06f907SNik Clayton#define	MT_OOBDATA	15	/* expedited data  */
189de06f907SNik Clayton.Ed
190de06f907SNik Clayton.Pp
191de06f907SNik ClaytonIf the
192de06f907SNik Clayton.Dv M_PKTHDR
193de06f907SNik Claytonflag is set, a
19425f1a2e9SSheldon Hearn.Li struct pkthdr m_pkthdr
195e73145f6SSheldon Hearnis added to the mbuf header.
196e73145f6SSheldon HearnIt contains a pointer to the interface
197de06f907SNik Claytonthe packet has been received from
198de06f907SNik Clayton.Pq Fa struct ifnet *rcvif ,
199de06f907SNik Claytonand the total packet length
200de06f907SNik Clayton.Pq Fa int len .
201de06f907SNik Clayton.Pp
202e73145f6SSheldon HearnIf small enough, data is stored in the mbuf's internal data buffer.
203e73145f6SSheldon HearnIf the data is sufficiently large, another mbuf may be added to the chain,
204e73145f6SSheldon Hearnor external storage may be associated with the mbuf.
205de06f907SNik Clayton.Dv MHLEN
20625f1a2e9SSheldon Hearnbytes of data can fit into an mbuf with the
207de06f907SNik Clayton.Dv M_PKTHDR
208de06f907SNik Claytonflag set,
209de06f907SNik Clayton.Dv MLEN
210de06f907SNik Claytonbytes can otherwise.
211de06f907SNik Clayton.Pp
212bfd59721SBosko MilekicIf external storage is being associated with an mbuf, the
213bfd59721SBosko Milekic.Dv m_ext
21425f1a2e9SSheldon Hearnheader is added at the cost of losing the internal data buffer.
215e73145f6SSheldon HearnIt includes a pointer to external storage, the size of the storage,
216e73145f6SSheldon Hearna pointer to a function used for freeing the storage,
217e73145f6SSheldon Hearna pointer to an optional argument that can be passed to the function,
218e73145f6SSheldon Hearnand a pointer to a reference counter.
219e73145f6SSheldon HearnAn mbuf using external storage has the
220de06f907SNik Clayton.Dv M_EXT
221de06f907SNik Claytonflag set.
222de06f907SNik Clayton.Pp
223bfd59721SBosko MilekicThe system supplies a macro for allocating the desired external storage
224bfd59721SBosko Milekicbuffer,
225bfd59721SBosko Milekic.Dv MEXTADD .
226bfd59721SBosko Milekic.Pp
227bfd59721SBosko MilekicThe allocation and management of the reference counter is handled by the
228e73145f6SSheldon Hearnsubsystem.
229e73145f6SSheldon HearnThe developer can check whether the reference count for the
230bfd59721SBosko Milekicgiven mbuf's external storage is greater than 1 with the
231bfd59721SBosko Milekic.Dv MEXT_IS_REF
232e73145f6SSheldon Hearnmacro.
233e73145f6SSheldon HearnSimilarly, the developer can directly add and remove references,
234e73145f6SSheldon Hearnif absolutely necessary, with the use of the
235bfd59721SBosko Milekic.Dv MEXT_ADD_REF
236bfd59721SBosko Milekicand
237bfd59721SBosko Milekic.Dv MEXT_REM_REF
238bfd59721SBosko Milekicmacros.
239bfd59721SBosko Milekic.Pp
240bfd59721SBosko MilekicThe system also supplies a default type of external storage buffer called an
241bfd59721SBosko Milekic.Dq mbuf cluster .
242bfd59721SBosko MilekicMbuf clusters can be allocated and configured with the use of the
243bfd59721SBosko Milekic.Dv MCLGET
244e73145f6SSheldon Hearnmacro.
245e73145f6SSheldon HearnEach cluster is
246de06f907SNik Clayton.Dv MCLBYTES
247bfd59721SBosko Milekicin size, where MCLBYTES is a machine-dependent constant.
248bfd59721SBosko MilekicThe system defines an advisory macro
249de06f907SNik Clayton.Dv MINCLSIZE ,
250de06f907SNik Claytonwhich is the smallest amount of data to put into a cluster.
251de06f907SNik ClaytonIt's equal to the sum of
252de06f907SNik Clayton.Dv MLEN
253de06f907SNik Claytonand
254de06f907SNik Clayton.Dv MHLEN .
255bfd59721SBosko MilekicIt is typically preferable to store data into an mbuf's data region, if size
256bfd59721SBosko Milekicpermits, as opposed to allocating a separate mbuf cluster to hold the same
257bfd59721SBosko Milekicdata.
258de06f907SNik Clayton.\"
259de06f907SNik Clayton.Ss Macros and Functions
260bfd59721SBosko MilekicThere are numerous predefined macros and functions that provide the
261bfd59721SBosko Milekicdeveloper with common utilities.
262de06f907SNik Clayton.\"
263de06f907SNik Clayton.Bl -ohang -offset indent
264de06f907SNik Clayton.It Fn mtod mbuf type
265bfd59721SBosko MilekicConvert an mbuf pointer to a data pointer.
266bfd59721SBosko MilekicThe macro expands to the data pointer cast to the pointer of the specified type.
267de06f907SNik Clayton.Sy Note :
268bfd59721SBosko MilekicIt is advisable to ensure that there is enough contiguous data in the mbuf.
269de06f907SNik ClaytonSee
270de06f907SNik Clayton.Fn m_pullup
271de06f907SNik Claytonfor details.
272de06f907SNik Clayton.It Fn MGET mbuf how type
273bfd59721SBosko MilekicAllocate an mbuf and initialize it to contain internal data.
27425f1a2e9SSheldon Hearn.Fa mbuf
275bfd59721SBosko Milekicwill point to the allocated mbuf on success, or be set to
276de06f907SNik Clayton.Dv NULL
277e73145f6SSheldon Hearnon failure.
278e73145f6SSheldon HearnThe
279de06f907SNik Clayton.Fa how
280de06f907SNik Claytonargument is to be set to
281c65b53c5STom Rhodes.Dv M_TRYWAIT
282de06f907SNik Claytonor
283c65b53c5STom Rhodes.Dv M_DONTWAIT .
28425f1a2e9SSheldon HearnIt specifies whether the caller is willing to block if necessary.
285e73145f6SSheldon HearnIf
286de06f907SNik Clayton.Fa how
28725f1a2e9SSheldon Hearnis set to
288c65b53c5STom Rhodes.Dv M_TRYWAIT ,
28925f1a2e9SSheldon Hearna failed allocation will result in the caller being put
290bfd59721SBosko Milekicto sleep for a designated
29125f1a2e9SSheldon Hearnkern.ipc.mbuf_wait
292c4d9468eSRuslan Ermilov.Xr ( sysctl 8
293c4d9468eSRuslan Ermilovtunable)
29425f1a2e9SSheldon Hearnnumber of ticks.
295bfd59721SBosko MilekicA number of other mbuf-related
296de06f907SNik Claytonfunctions and macros have the same argument because they may
297bfd59721SBosko Milekicat some point need to allocate new mbufs.
298c65b53c5STom Rhodes.Pp
299c65b53c5STom RhodesProgrammers should be careful not to confuse the mbuf allocation flag
300c65b53c5STom Rhodes.Dv M_DONTWAIT
301c65b53c5STom Rhodeswith the
302c65b53c5STom Rhodes.Xr malloc 9
303c65b53c5STom Rhodesallocation flag,
304c65b53c5STom Rhodes.Dv M_NOWAIT .
305c65b53c5STom RhodesThey are not the same.
306de06f907SNik Clayton.It Fn MGETHDR mbuf how type
307bfd59721SBosko MilekicAllocate an mbuf and initialize it to contain a packet header
308e73145f6SSheldon Hearnand internal data.
309e73145f6SSheldon HearnSee
310de06f907SNik Clayton.Fn MGET
311de06f907SNik Claytonfor details.
312de06f907SNik Clayton.It Fn MCLGET mbuf how
313e73145f6SSheldon HearnAllocate and attach an mbuf cluster to an mbuf.
314e73145f6SSheldon HearnIf the macro fails, the
315de06f907SNik Clayton.Dv M_EXT
316de06f907SNik Claytonflag won't be set in the mbuf.
317de06f907SNik Clayton.It Fn M_PREPEND mbuf len how
318bfd59721SBosko MilekicThis macro operates on an mbuf chain.
319de06f907SNik ClaytonIt is an optimized wrapper for
320de06f907SNik Clayton.Fn m_prepend
321de06f907SNik Claytonthat can make use of possible empty space before data
322c4d9468eSRuslan Ermilov(e.g. left after trimming of a link-layer header).
323de06f907SNik ClaytonThe new chain pointer or
324de06f907SNik Clayton.Dv NULL
325de06f907SNik Claytonis in
326de06f907SNik Clayton.Fa mbuf
327de06f907SNik Claytonafter the call.
3285d4050ccSBosko Milekic.It Fn M_WRITABLE mbuf
3295d4050ccSBosko MilekicThis macro will evaluate true if the mbuf is not marked
3305d4050ccSBosko Milekic.Dv M_RDONLY
3315d4050ccSBosko Milekicand if either the mbuf does not contain external storage or,
3325d4050ccSBosko Milekicif it does,
3335d4050ccSBosko Milekicthen if the reference count of the storage is not greater than 1.
3345d4050ccSBosko MilekicThe
3355d4050ccSBosko Milekic.Dv M_RDONLY
3365d4050ccSBosko Milekicflag can be set in the mbuf's
3375d4050ccSBosko Milekic.Dv m_flags .
3385d4050ccSBosko MilekicThis can be achieved during setup of the external storage,
3395d4050ccSBosko Milekicby passing the
3405d4050ccSBosko Milekic.Dv M_RDONLY
3415d4050ccSBosko Milekicbit as a
3420b39b3ffSRuslan Ermilov.Fa flags
3435d4050ccSBosko Milekicargument to the
3445d4050ccSBosko Milekic.Fn MEXTADD
3455d4050ccSBosko Milekicmacro, or can be directly set in individual mbufs.
346de06f907SNik Clayton.El
347de06f907SNik Clayton.Pp
348de06f907SNik ClaytonThe functions are:
349de06f907SNik Clayton.Bl -ohang -offset indent
350de06f907SNik Clayton.It Fn m_get how type
351de06f907SNik ClaytonA function version of
352bfd59721SBosko Milekic.Fn MGET
353bfd59721SBosko Milekicfor non-critical paths.
354328c0bbcSBosko Milekic.It Fn m_getm orig len how type
355328c0bbcSBosko MilekicAllocate
3560b39b3ffSRuslan Ermilov.Fa len
357328c0bbcSBosko Milekicbytes worth of mbufs and mbuf clusters if necessary and append the resulting
358328c0bbcSBosko Milekicallocated chain to the
3590b39b3ffSRuslan Ermilov.Fa orig
3600b39b3ffSRuslan Ermilovmbuf chain, if it is
3610b39b3ffSRuslan Ermilov.No non- Ns Dv NULL .
362328c0bbcSBosko MilekicIf the allocation fails at any point,
3630b39b3ffSRuslan Ermilovfree whatever was allocated and return
3640b39b3ffSRuslan Ermilov.Dv NULL .
365328c0bbcSBosko MilekicIf
3660b39b3ffSRuslan Ermilov.Fa orig
3670b39b3ffSRuslan Ermilovis
3680b39b3ffSRuslan Ermilov.No non- Ns Dv NULL ,
369328c0bbcSBosko Milekicit will not be freed.
370328c0bbcSBosko MilekicIt is possible to use
371328c0bbcSBosko Milekic.Fn m_getm
372328c0bbcSBosko Milekicto either append
3730b39b3ffSRuslan Ermilov.Fa len
374328c0bbcSBosko Milekicbytes to an existing mbuf or mbuf chain
375328c0bbcSBosko Milekic(for example, one which may be sitting in a pre-allocated ring)
376328c0bbcSBosko Milekicor to simply perform an all-or-nothing mbuf and mbuf cluster allocation.
377de06f907SNik Clayton.It Fn m_gethdr how type
378de06f907SNik ClaytonA function version of
379bfd59721SBosko Milekic.Fn MGETHDR
380bfd59721SBosko Milekicfor non-critical paths.
381de06f907SNik Clayton.It Fn m_getclr how type
382bfd59721SBosko MilekicAllocate an mbuf and zero out the data region.
383de06f907SNik Clayton.El
384de06f907SNik Clayton.Pp
385de06f907SNik ClaytonThe functions below operate on mbuf chains.
386de06f907SNik Clayton.Bl -ohang -offset indent
387de06f907SNik Clayton.It Fn m_freem mbuf
388de06f907SNik ClaytonFree an entire mbuf chain, including any external
389de06f907SNik Claytonstorage.
390de06f907SNik Clayton.\"
391de06f907SNik Clayton.It Fn m_adj mbuf len
392de06f907SNik ClaytonTrim
393de06f907SNik Clayton.Fa len
39425f1a2e9SSheldon Hearnbytes from the head of an mbuf chain if
395de06f907SNik Clayton.Fa len
396de06f907SNik Claytonis positive, from the tail otherwise.
397de06f907SNik Clayton.\"
398de06f907SNik Clayton.It Fn m_prepend mbuf len how
399de06f907SNik ClaytonAllocate a new mbuf and prepend it to the chain, handle
400de06f907SNik Clayton.Dv M_PKTHDR
401de06f907SNik Claytonproperly.
402de06f907SNik Clayton.Sy Note :
403de06f907SNik ClaytonIt doesn't allocate any clusters, so
404de06f907SNik Clayton.Fa len
405de06f907SNik Claytonmust be less than
406de06f907SNik Clayton.Dv MLEN
407de06f907SNik Claytonor
408de06f907SNik Clayton.Dv MHLEN ,
409de06f907SNik Claytondepending on the
410d0353b83SRuslan Ermilov.Dv M_PKTHDR
411d0353b83SRuslan Ermilovflag setting.
412de06f907SNik Clayton.\"
413de06f907SNik Clayton.It Fn m_pullup mbuf len
414de06f907SNik ClaytonArrange that the first
415de06f907SNik Clayton.Fa len
41625f1a2e9SSheldon Hearnbytes of an mbuf chain are contiguous and lay in the data area of
417de06f907SNik Clayton.Fa mbuf ,
418de06f907SNik Claytonso they are accessible with
419de06f907SNik Clayton.Fn mtod mbuf type .
420de06f907SNik ClaytonReturn the new chain on success,
421de06f907SNik Clayton.Dv NULL
422de06f907SNik Claytonon failure
423c4d9468eSRuslan Ermilov(the chain is freed in this case).
424de06f907SNik Clayton.Sy Note :
425de06f907SNik ClaytonIt doesn't allocate any clusters, so
426de06f907SNik Clayton.Fa len
427de06f907SNik Claytonmust be less than
428de06f907SNik Clayton.Dv MHLEN .
429de06f907SNik Clayton.\"
430de06f907SNik Clayton.It Fn m_copym mbuf offset len how
431de06f907SNik ClaytonMake a copy of an mbuf chain starting
432de06f907SNik Clayton.Fa offset
433de06f907SNik Claytonbytes from the beginning, continuing for
434de06f907SNik Clayton.Fa len
435e73145f6SSheldon Hearnbytes.
436e73145f6SSheldon HearnIf
437de06f907SNik Clayton.Fa len
438de06f907SNik Claytonis
439de06f907SNik Clayton.Dv M_COPYALL ,
440de06f907SNik Claytoncopy to the end of the mbuf chain.
441de06f907SNik Clayton.Sy Note :
442de06f907SNik ClaytonThe copy is read-only, because clusters are not
443de06f907SNik Claytoncopied, only their reference counts are incremented.
444de06f907SNik Clayton.\"
445de06f907SNik Clayton.It Fn m_copypacket mbuf how
446de06f907SNik ClaytonCopy an entire packet including header, which must be present.
447de06f907SNik ClaytonThis is an optimized version of the common case
448de06f907SNik Clayton.Fn m_copym mbuf 0 M_COPYALL how .
449de06f907SNik Clayton.Sy Note :
450de06f907SNik Claytonthe copy is read-only, because clusters are not
451de06f907SNik Claytoncopied, only their reference counts are incremented.
452de06f907SNik Clayton.\"
453de06f907SNik Clayton.It Fn m_dup mbuf how
454de06f907SNik ClaytonCopy a packet header mbuf chain into a completely new chain, including
455e73145f6SSheldon Hearncopying any mbuf clusters.
456e73145f6SSheldon HearnUse this instead of
457de06f907SNik Clayton.Fn m_copypacket
458de06f907SNik Claytonwhen you need a writable copy of an mbuf chain.
459de06f907SNik Clayton.\"
460de06f907SNik Clayton.It Fn m_copydata mbuf offset len buf
46125f1a2e9SSheldon HearnCopy data from an mbuf chain starting
462de06f907SNik Clayton.Fa off
463de06f907SNik Claytonbytes from the beginning, continuing for
464de06f907SNik Clayton.Fa len
465de06f907SNik Claytonbytes, into the indicated buffer
466de06f907SNik Clayton.Fa buf .
467de06f907SNik Clayton.\"
468de06f907SNik Clayton.It Fn m_copyback mbuf offset len buf
469de06f907SNik ClaytonCopy
470de06f907SNik Clayton.Fa len
471de06f907SNik Claytonbytes from the buffer
472de06f907SNik Clayton.Fa buf
473de06f907SNik Claytonback into the indicated mbuf chain,
474de06f907SNik Claytonstarting at
475de06f907SNik Clayton.Fa offset
476de06f907SNik Claytonbytes from the beginning of the chain, extending the mbuf chain if necessary.
477de06f907SNik Clayton.Sy Note :
478e73145f6SSheldon HearnIt doesn't allocate any clusters, just adds mbufs to the chain.
479e73145f6SSheldon HearnIt's safe to set
480de06f907SNik Clayton.Fa offset
481de06f907SNik Claytonbeyond the current chain end: zeroed mbufs will be allocated to fill the
482de06f907SNik Claytonspace.
483de06f907SNik Clayton.\"
4840b1c0994SPoul-Henning Kamp.It Fn m_length buf last
4850b1c0994SPoul-Henning KampReturn the length of the mbuf chain, and optionally a pointer to the last mbuf.
4860b1c0994SPoul-Henning Kamp.\"
4870b1c0994SPoul-Henning Kamp.It Fn m_fixhdr buf
4880b1c0994SPoul-Henning KampSet the packet-header length to the length of the mbuf chain.
4890b1c0994SPoul-Henning Kamp.\"
490de06f907SNik Clayton.It Fn m_devget buf len offset ifp copy
491de06f907SNik ClaytonCopy data from a device local memory pointed to by
492de06f907SNik Clayton.Fa buf
49325f1a2e9SSheldon Hearnto an mbuf chain.
494e73145f6SSheldon HearnThe copy is done using a specified copy routine
495de06f907SNik Clayton.Fa copy ,
496de06f907SNik Claytonor
497de06f907SNik Clayton.Fn bcopy
498de06f907SNik Claytonif
499de06f907SNik Clayton.Fa copy
500de06f907SNik Claytonis
501de06f907SNik Clayton.Dv NULL .
502de06f907SNik Clayton.\"
503de06f907SNik Clayton.It Fn m_cat m n
504de06f907SNik ClaytonConcatenate
505de06f907SNik Clayton.Fa n
506de06f907SNik Claytonto
507de06f907SNik Clayton.Fa m .
508de06f907SNik ClaytonBoth chains must be of the same type.
509de06f907SNik Clayton.Fa N
510de06f907SNik Claytonis still valid after the function returned.
511de06f907SNik Clayton.Sy Note :
512de06f907SNik ClaytonIt does not handle
513de06f907SNik Clayton.Dv M_PKTHDR
514de06f907SNik Claytonand friends.
515de06f907SNik Clayton.\"
516de06f907SNik Clayton.It Fn m_split mbuf len how
517de06f907SNik ClaytonPartition an mbuf chain in two pieces, returning the tail:
518de06f907SNik Claytonall but the first
519de06f907SNik Clayton.Fa len
520e73145f6SSheldon Hearnbytes.
521e73145f6SSheldon HearnIn case of failure, it returns
522de06f907SNik Clayton.Dv NULL
523de06f907SNik Claytonand attempts to restore the chain to its original state.
5243136363fSRuslan Ermilov.El
5253cab047eSMike Silbersack.Sh STRESS TESTING
526c8185672SRuslan ErmilovWhen running a kernel compiled with the option
527c8185672SRuslan Ermilov.Dv MBUF_STRESS_TEST ,
528c8185672SRuslan Ermilovthe following
529c8185672SRuslan Ermilov.Xr sysctl 8 Ns
530c8185672SRuslan Ermilov-controlled options may be used to create
5313cab047eSMike Silbersackvarious failure/extreme cases for testing of network drivers
5323cab047eSMike Silbersackand other mbuf-reliant parts of the kernel.
533c8185672SRuslan Ermilov.Bl -tag -width ident
534c8185672SRuslan Ermilov.It Va net.inet.ip.mbuf_frag_size
535c8185672SRuslan ErmilovCauses
536c8185672SRuslan Ermilov.Fn ip_output
537c8185672SRuslan Ermilovto fragment outgoing mbuf chains into fragments of the specified size.
538c8185672SRuslan ErmilovSetting this variable to 1 is an excellent way to
5393cab047eSMike Silbersacktest the long mbuf chain handling ability of network drivers.
540c8185672SRuslan Ermilov.It Va kern.ipc.m_defragrandomfailures
541c8185672SRuslan ErmilovCauses the function
542c8185672SRuslan Ermilov.Fn m_defrag
543c8185672SRuslan Ermilovto randomly fail, returning
544c8185672SRuslan Ermilov.Dv NULL .
545c8185672SRuslan ErmilovAny piece of code which uses
546c8185672SRuslan Ermilov.Fn m_defrag
547c8185672SRuslan Ermilovshould be tested with this feature.
548c8185672SRuslan Ermilov.El
549de06f907SNik Clayton.Sh RETURN VALUES
550de06f907SNik ClaytonSee above.
551de06f907SNik Clayton.Sh HISTORY
552de06f907SNik Clayton.\" Please correct me if I'm wrong
553753d686dSRuslan ErmilovMbufs appeared in an early version of
554753d686dSRuslan Ermilov.Bx .
555e73145f6SSheldon HearnBesides for being used for network packets, they were used
556bfd59721SBosko Milekicto store various dynamic structures, such as routing table
557bfd59721SBosko Milekicentries, interface addresses, protocol control blocks, etc.
558de06f907SNik Clayton.Sh AUTHORS
5596575e6daSRuslan ErmilovThe original
5606575e6daSRuslan Ermilov.Nm
5616575e6daSRuslan Ermilovman page was written by Yar Tikhiy.
562