xref: /freebsd/share/man/man9/mbuf.9 (revision 328c0bbcf7a9e303ed0ba1056ccc28a3c5747cee)
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
362ba5bb1fSBruce Evans.Fd #include <sys/param.h>
37de06f907SNik Clayton.Fd #include <sys/mbuf.h>
38de06f907SNik Clayton.\"
39bfd59721SBosko Milekic.Ss Mbuf allocation macros
40de06f907SNik Clayton.Fn MGET "struct mbuf *mbuf" "int how" "short type"
41de06f907SNik Clayton.Fn MGETHDR "struct mbuf *mbuf" "int how" "short type"
42de06f907SNik Clayton.Fn MCLGET "struct mbuf *mbuf" "int how"
43bfd59721SBosko Milekic.Fo MEXTADD
44bfd59721SBosko Milekic.Fa "struct mbuf *mbuf"
45bfd59721SBosko Milekic.Fa "caddr_t buf"
46bfd59721SBosko Milekic.Fa "u_int size"
47bfd59721SBosko Milekic.Fa "void (*free)(void *opt_args)"
48bfd59721SBosko Milekic.Fa "void *opt_args"
495d4050ccSBosko Milekic.Fa "short flags"
505d4050ccSBosko Milekic.Fa "int type"
51bfd59721SBosko Milekic.Fc
52bfd59721SBosko Milekic.Fn MEXTFREE "struct mbuf *mbuf"
53bfd59721SBosko Milekic.Fn MEXT_ADD_REF "struct mbuf *mbuf"
54bfd59721SBosko Milekic.Fn MEXT_REM_REF "struct mbuf *mbuf"
55bfd59721SBosko Milekic.Fn MFREE "struct mbuf *mbuf" "struct mbuf *successor"
56de06f907SNik Clayton.\"
57bfd59721SBosko Milekic.Ss Mbuf utility macros
58bfd59721SBosko Milekic.Fn mtod "struct mbuf *mbuf" "any type"
59bfd59721SBosko Milekic.Fn MEXT_IS_REF "struct mbuf *mbuf"
60bfd59721SBosko Milekic.Fn M_COPY_PKTHDR "struct mbuf *to" "struct mbuf *from"
61bfd59721SBosko Milekic.Fn M_ALIGN "struct mbuf *mbuf" "u_int len"
62bfd59721SBosko Milekic.Fn MH_ALIGN "struct mbuf *mbuf" "u_int len"
63bfd59721SBosko Milekic.Fn M_LEADINGSPACE "struct mbuf *mbuf"
64bfd59721SBosko Milekic.Fn M_TRAILINGSPACE "struct mbuf *mbuf"
65bfd59721SBosko Milekic.Fn M_PREPEND "struct mbuf *mbuf" "int len" "int how"
66bfd59721SBosko Milekic.Fn MCHTYPE "struct mbuf *mbuf" "u_int type"
675d4050ccSBosko Milekic.Fn M_WRITABLE "struct mbuf *mbuf"
68bfd59721SBosko Milekic.\"
69bfd59721SBosko Milekic.Ss Mbuf allocation functions
70de06f907SNik Clayton.Ft struct mbuf *
71de06f907SNik Clayton.Fn m_get "int how" "int type"
72de06f907SNik Clayton.Ft struct mbuf *
73328c0bbcSBosko Milekic.Fn m_getm "struct mbuf *orig" "int len" "int how" "int type"
74328c0bbcSBosko Milekic.Ft struct mbuf *
75de06f907SNik Clayton.Fn m_getclr "int how" "int type"
76de06f907SNik Clayton.Ft struct mbuf *
77de06f907SNik Clayton.Fn m_gethdr "int how" "int type"
78bfd59721SBosko Milekic.Ft struct mbuf *
79bfd59721SBosko Milekic.Fn m_free "struct mbuf *mbuf"
80de06f907SNik Clayton.Ft void
81de06f907SNik Clayton.Fn m_freem "struct mbuf *mbuf"
82bfd59721SBosko Milekic.\"
83bfd59721SBosko Milekic.Ss Mbuf utility functions
84de06f907SNik Clayton.Ft void
85de06f907SNik Clayton.Fn m_adj "struct mbuf *mbuf" "int len"
86de06f907SNik Clayton.Ft struct mbuf *
87de06f907SNik Clayton.Fn m_prepend "struct mbuf *mbuf" "int len" "int how"
88de06f907SNik Clayton.Ft struct mbuf *
89de06f907SNik Clayton.Fn m_pullup "struct mbuf *mbuf" "int len"
90de06f907SNik Clayton.Ft struct mbuf *
91de06f907SNik Clayton.Fn m_copym "struct mbuf *mbuf" "int offset" "int len" "int how"
92de06f907SNik Clayton.Ft struct mbuf *
93de06f907SNik Clayton.Fn m_copypacket "struct mbuf *mbuf" "int how"
94de06f907SNik Clayton.Ft struct mbuf *
95de06f907SNik Clayton.Fn m_dup "struct mbuf *mbuf" "int how"
96de06f907SNik Clayton.Ft void
97de06f907SNik Clayton.Fn m_copydata "struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
98de06f907SNik Clayton.Ft void
99de06f907SNik Clayton.Fn m_copyback "struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
100de06f907SNik Clayton.Ft struct mbuf *
101de06f907SNik Clayton.Fo m_devget
102de06f907SNik Clayton.Fa "char *buf"
103de06f907SNik Clayton.Fa "int len"
104de06f907SNik Clayton.Fa "int offset"
105de06f907SNik Clayton.Fa "struct ifnet *ifp"
106de06f907SNik Clayton.Fa "void (*copy)(char *from, caddr_t to, u_int len)"
107de06f907SNik Clayton.Fc
108de06f907SNik Clayton.Ft void
109de06f907SNik Clayton.Fn m_cat "struct mbuf *m" "struct mbuf *n"
110de06f907SNik Clayton.Ft struct mbuf *
111de06f907SNik Clayton.Fn m_split "struct mbuf *mbuf" "int len" "int how"
112de06f907SNik Clayton.\"
113de06f907SNik Clayton.Sh DESCRIPTION
114bfd59721SBosko MilekicAn mbuf is a basic unit of memory management in the kernel IPC subsystem.
115e73145f6SSheldon HearnNetwork packets and socket buffers are stored in mbufs.
116e73145f6SSheldon HearnA network packet may span multiple mbufs arranged into a chain
117de06f907SNik Clayton.Pq linked list ,
118de06f907SNik Claytonwhich allows adding or trimming
119bfd59721SBosko Milekicnetwork headers with little overhead.
120de06f907SNik Clayton.Pp
121bfd59721SBosko MilekicWhile a developer should not bother with mbuf internals without serious
122de06f907SNik Claytonreason in order to avoid incompatibilities with future changes, it
123bfd59721SBosko Milekicis useful to understand the mbuf's general structure.
124de06f907SNik Clayton.Pp
125bfd59721SBosko MilekicAn mbuf consists of a variable-sized header and a small internal
126e73145f6SSheldon Hearnbuffer for data.
127e73145f6SSheldon HearnThe mbuf's total size,
128bfd59721SBosko Milekic.Dv MSIZE ,
129de06f907SNik Claytonis a machine-dependent constant defined in
130de06f907SNik Clayton.Pa machine/param.h .
131de06f907SNik ClaytonThe mbuf header includes:
132de06f907SNik Clayton.Pp
133de06f907SNik Clayton.Bl -tag -width "m_nextpkt" -compact -offset indent
134de06f907SNik Clayton.It Fa m_next
135de06f907SNik Claytona pointer to the next buffer in the chain
136de06f907SNik Clayton.It Fa m_nextpkt
137de06f907SNik Claytona pointer to the next chain in the queue
138de06f907SNik Clayton.It Fa m_data
139de06f907SNik Claytona pointer to the data
140de06f907SNik Clayton.It Fa m_len
141de06f907SNik Claytonthe length of the data
142de06f907SNik Clayton.It Fa m_type
143bfd59721SBosko Milekicthe type of data
144de06f907SNik Clayton.It Fa m_flags
145de06f907SNik Claytonthe mbuf flags
146de06f907SNik Clayton.El
147de06f907SNik Clayton.Pp
148de06f907SNik ClaytonThe mbuf flag bits are defined as follows:
149de06f907SNik Clayton.Bd -literal
150de06f907SNik Clayton/* mbuf flags */
151de06f907SNik Clayton#define	M_EXT		0x0001	/* has associated external storage */
152de06f907SNik Clayton#define	M_PKTHDR	0x0002	/* start of record */
153de06f907SNik Clayton#define	M_EOR		0x0004	/* end of record */
1545d4050ccSBosko Milekic#define	M_RDONLY	0x0008	/* associated data marked read-only */
1555d4050ccSBosko Milekic#define	M_PROTO1	0x0010	/* protocol-specific */
1565d4050ccSBosko Milekic#define	M_PROTO2	0x0020 	/* protocol-specific */
1575d4050ccSBosko Milekic#define	M_PROTO3	0x0040	/* protocol-specific */
1585d4050ccSBosko Milekic#define	M_PROTO4	0x0080	/* protocol-specific */
1595d4050ccSBosko Milekic#define	M_PROTO5	0x0100	/* protocol-specific */
160de06f907SNik Clayton
161de06f907SNik Clayton/* mbuf pkthdr flags, also in m_flags */
1625d4050ccSBosko Milekic#define	M_BCAST		0x0200	/* send/received as link-level broadcast */
1635d4050ccSBosko Milekic#define	M_MCAST		0x0400	/* send/received as link-level multicast */
1645d4050ccSBosko Milekic#define	M_FRAG		0x0800	/* packet is fragment of larger packet */
1655d4050ccSBosko Milekic#define	M_FIRSTFRAG	0x1000	/* packet is first fragment */
1665d4050ccSBosko Milekic#define	M_LASTFRAG	0x2000	/* packet is last fragment */
167de06f907SNik Clayton.Ed
168de06f907SNik Clayton.Pp
169bfd59721SBosko MilekicThe available mbuf types are defined as follows:
170de06f907SNik Clayton.Bd -literal
171de06f907SNik Clayton/* mbuf types */
172de06f907SNik Clayton#define	MT_FREE		0	/* should be on free list */
173de06f907SNik Clayton#define	MT_DATA		1	/* dynamic (data) allocation */
174de06f907SNik Clayton#define	MT_HEADER	2	/* packet header */
175de06f907SNik Clayton#define	MT_SONAME	8	/* socket name */
176de06f907SNik Clayton#define	MT_FTABLE	11	/* fragment reassembly header */
177de06f907SNik Clayton#define	MT_CONTROL	14	/* extra-data protocol message */
178de06f907SNik Clayton#define	MT_OOBDATA	15	/* expedited data  */
179de06f907SNik Clayton.Ed
180de06f907SNik Clayton.Pp
181de06f907SNik ClaytonIf the
182de06f907SNik Clayton.Dv M_PKTHDR
183de06f907SNik Claytonflag is set, a
18425f1a2e9SSheldon Hearn.Li struct pkthdr m_pkthdr
185e73145f6SSheldon Hearnis added to the mbuf header.
186e73145f6SSheldon HearnIt contains a pointer to the interface
187de06f907SNik Claytonthe packet has been received from
188de06f907SNik Clayton.Pq Fa struct ifnet *rcvif ,
189de06f907SNik Claytonand the total packet length
190de06f907SNik Clayton.Pq Fa int len .
191de06f907SNik Clayton.Pp
192e73145f6SSheldon HearnIf small enough, data is stored in the mbuf's internal data buffer.
193e73145f6SSheldon HearnIf the data is sufficiently large, another mbuf may be added to the chain,
194e73145f6SSheldon Hearnor external storage may be associated with the mbuf.
195de06f907SNik Clayton.Dv MHLEN
19625f1a2e9SSheldon Hearnbytes of data can fit into an mbuf with the
197de06f907SNik Clayton.Dv M_PKTHDR
198de06f907SNik Claytonflag set,
199de06f907SNik Clayton.Dv MLEN
200de06f907SNik Claytonbytes can otherwise.
201de06f907SNik Clayton.Pp
202bfd59721SBosko MilekicIf external storage is being associated with an mbuf, the
203bfd59721SBosko Milekic.Dv m_ext
20425f1a2e9SSheldon Hearnheader is added at the cost of losing the internal data buffer.
205e73145f6SSheldon HearnIt includes a pointer to external storage, the size of the storage,
206e73145f6SSheldon Hearna pointer to a function used for freeing the storage,
207e73145f6SSheldon Hearna pointer to an optional argument that can be passed to the function,
208e73145f6SSheldon Hearnand a pointer to a reference counter.
209e73145f6SSheldon HearnAn mbuf using external storage has the
210de06f907SNik Clayton.Dv M_EXT
211de06f907SNik Claytonflag set.
212de06f907SNik Clayton.Pp
213bfd59721SBosko MilekicThe system supplies a macro for allocating the desired external storage
214bfd59721SBosko Milekicbuffer,
215bfd59721SBosko Milekic.Dv MEXTADD .
216bfd59721SBosko Milekic.Pp
217bfd59721SBosko MilekicThe allocation and management of the reference counter is handled by the
218e73145f6SSheldon Hearnsubsystem.
219e73145f6SSheldon HearnThe developer can check whether the reference count for the
220bfd59721SBosko Milekicgiven mbuf's external storage is greater than 1 with the
221bfd59721SBosko Milekic.Dv MEXT_IS_REF
222e73145f6SSheldon Hearnmacro.
223e73145f6SSheldon HearnSimilarly, the developer can directly add and remove references,
224e73145f6SSheldon Hearnif absolutely necessary, with the use of the
225bfd59721SBosko Milekic.Dv MEXT_ADD_REF
226bfd59721SBosko Milekicand
227bfd59721SBosko Milekic.Dv MEXT_REM_REF
228bfd59721SBosko Milekicmacros.
229bfd59721SBosko Milekic.Pp
230bfd59721SBosko MilekicThe system also supplies a default type of external storage buffer called an
231bfd59721SBosko Milekic.Dq mbuf cluster .
232bfd59721SBosko MilekicMbuf clusters can be allocated and configured with the use of the
233bfd59721SBosko Milekic.Dv MCLGET
234e73145f6SSheldon Hearnmacro.
235e73145f6SSheldon HearnEach cluster is
236de06f907SNik Clayton.Dv MCLBYTES
237bfd59721SBosko Milekicin size, where MCLBYTES is a machine-dependent constant.
238bfd59721SBosko MilekicThe system defines an advisory macro
239de06f907SNik Clayton.Dv MINCLSIZE ,
240de06f907SNik Claytonwhich is the smallest amount of data to put into a cluster.
241de06f907SNik ClaytonIt's equal to the sum of
242de06f907SNik Clayton.Dv MLEN
243de06f907SNik Claytonand
244de06f907SNik Clayton.Dv MHLEN .
245bfd59721SBosko MilekicIt is typically preferable to store data into an mbuf's data region, if size
246bfd59721SBosko Milekicpermits, as opposed to allocating a separate mbuf cluster to hold the same
247bfd59721SBosko Milekicdata.
248de06f907SNik Clayton.\"
249de06f907SNik Clayton.Ss Macros and Functions
250bfd59721SBosko MilekicThere are numerous predefined macros and functions that provide the
251bfd59721SBosko Milekicdeveloper with common utilities.
252de06f907SNik Clayton.\"
253de06f907SNik Clayton.Bl -ohang -offset indent
254de06f907SNik Clayton.It Fn mtod mbuf type
255bfd59721SBosko MilekicConvert an mbuf pointer to a data pointer.
256bfd59721SBosko MilekicThe macro expands to the data pointer cast to the pointer of the specified type.
257de06f907SNik Clayton.Sy Note :
258bfd59721SBosko MilekicIt is advisable to ensure that there is enough contiguous data in the mbuf.
259de06f907SNik ClaytonSee
260de06f907SNik Clayton.Fn m_pullup
261de06f907SNik Claytonfor details.
262de06f907SNik Clayton.It Fn MGET mbuf how type
263bfd59721SBosko MilekicAllocate an mbuf and initialize it to contain internal data.
26425f1a2e9SSheldon Hearn.Fa mbuf
265bfd59721SBosko Milekicwill point to the allocated mbuf on success, or be set to
266de06f907SNik Clayton.Dv NULL
267e73145f6SSheldon Hearnon failure.
268e73145f6SSheldon HearnThe
269de06f907SNik Clayton.Fa how
270de06f907SNik Claytonargument is to be set to
271f73f120bSBosko Milekic.Dv M_TRYWAIT
272de06f907SNik Claytonor
273de06f907SNik Clayton.Dv M_DONTWAIT .
27425f1a2e9SSheldon HearnIt specifies whether the caller is willing to block if necessary.
275e73145f6SSheldon HearnIf
276de06f907SNik Clayton.Fa how
27725f1a2e9SSheldon Hearnis set to
278f73f120bSBosko Milekic.Dv M_TRYWAIT ,
27925f1a2e9SSheldon Hearna failed allocation will result in the caller being put
280bfd59721SBosko Milekicto sleep for a designated
28125f1a2e9SSheldon Hearnkern.ipc.mbuf_wait
28225f1a2e9SSheldon Hearn.Po
28325f1a2e9SSheldon Hearn.Xr sysctl 8
28425f1a2e9SSheldon Hearntunable
28525f1a2e9SSheldon Hearn.Pc
28625f1a2e9SSheldon Hearnnumber of ticks.
287bfd59721SBosko MilekicA number of other mbuf-related
288de06f907SNik Claytonfunctions and macros have the same argument because they may
289bfd59721SBosko Milekicat some point need to allocate new mbufs.
290de06f907SNik Clayton.It Fn MGETHDR mbuf how type
291bfd59721SBosko MilekicAllocate an mbuf and initialize it to contain a packet header
292e73145f6SSheldon Hearnand internal data.
293e73145f6SSheldon HearnSee
294de06f907SNik Clayton.Fn MGET
295de06f907SNik Claytonfor details.
296de06f907SNik Clayton.It Fn MCLGET mbuf how
297e73145f6SSheldon HearnAllocate and attach an mbuf cluster to an mbuf.
298e73145f6SSheldon HearnIf the macro fails, the
299de06f907SNik Clayton.Dv M_EXT
300de06f907SNik Claytonflag won't be set in the mbuf.
301de06f907SNik Clayton.It Fn M_PREPEND mbuf len how
302bfd59721SBosko MilekicThis macro operates on an mbuf chain.
303de06f907SNik ClaytonIt is an optimized wrapper for
304de06f907SNik Clayton.Fn m_prepend
305de06f907SNik Claytonthat can make use of possible empty space before data
306de06f907SNik Clayton.Pq "e.g. left after trimming of a link-layer header" .
307de06f907SNik ClaytonThe new chain pointer or
308de06f907SNik Clayton.Dv NULL
309de06f907SNik Claytonis in
310de06f907SNik Clayton.Fa mbuf
311de06f907SNik Claytonafter the call.
3125d4050ccSBosko Milekic.It Fn M_WRITABLE mbuf
3135d4050ccSBosko MilekicThis macro will evaluate true if the mbuf is not marked
3145d4050ccSBosko Milekic.Dv M_RDONLY
3155d4050ccSBosko Milekicand if either the mbuf does not contain external storage or,
3165d4050ccSBosko Milekicif it does,
3175d4050ccSBosko Milekicthen if the reference count of the storage is not greater than 1.
3185d4050ccSBosko MilekicThe
3195d4050ccSBosko Milekic.Dv M_RDONLY
3205d4050ccSBosko Milekicflag can be set in the mbuf's
3215d4050ccSBosko Milekic.Dv m_flags .
3225d4050ccSBosko MilekicThis can be achieved during setup of the external storage,
3235d4050ccSBosko Milekicby passing the
3245d4050ccSBosko Milekic.Dv M_RDONLY
3255d4050ccSBosko Milekicbit as a
3265d4050ccSBosko Milekic.Ar flags
3275d4050ccSBosko Milekicargument to the
3285d4050ccSBosko Milekic.Fn MEXTADD
3295d4050ccSBosko Milekicmacro, or can be directly set in individual mbufs.
330de06f907SNik Clayton.El
331de06f907SNik Clayton.Pp
332de06f907SNik ClaytonThe functions are:
333de06f907SNik Clayton.Bl -ohang -offset indent
334de06f907SNik Clayton.It Fn m_get how type
335de06f907SNik ClaytonA function version of
336bfd59721SBosko Milekic.Fn MGET
337bfd59721SBosko Milekicfor non-critical paths.
338328c0bbcSBosko Milekic.It Fn m_getm orig len how type
339328c0bbcSBosko MilekicAllocate
340328c0bbcSBosko Milekic.Ar len
341328c0bbcSBosko Milekicbytes worth of mbufs and mbuf clusters if necessary and append the resulting
342328c0bbcSBosko Milekicallocated chain to the
343328c0bbcSBosko Milekic.Ar orig
344328c0bbcSBosko Milekicmbuf chain, if it is non-NULL.
345328c0bbcSBosko MilekicIf the allocation fails at any point,
346328c0bbcSBosko Milekicfree whatever was allocated and return NULL.
347328c0bbcSBosko MilekicIf
348328c0bbcSBosko Milekic.Ar orig
349328c0bbcSBosko Milekicis non-NULL,
350328c0bbcSBosko Milekicit will not be freed.
351328c0bbcSBosko MilekicIt is possible to use
352328c0bbcSBosko Milekic.Fn m_getm
353328c0bbcSBosko Milekicto either append
354328c0bbcSBosko Milekic.Ar len
355328c0bbcSBosko Milekicbytes to an existing mbuf or mbuf chain
356328c0bbcSBosko Milekic(for example, one which may be sitting in a pre-allocated ring)
357328c0bbcSBosko Milekicor to simply perform an all-or-nothing mbuf and mbuf cluster allocation.
358de06f907SNik Clayton.It Fn m_gethdr how type
359de06f907SNik ClaytonA function version of
360bfd59721SBosko Milekic.Fn MGETHDR
361bfd59721SBosko Milekicfor non-critical paths.
362de06f907SNik Clayton.It Fn m_getclr how type
363bfd59721SBosko MilekicAllocate an mbuf and zero out the data region.
364de06f907SNik Clayton.El
365de06f907SNik Clayton.Pp
366de06f907SNik ClaytonThe functions below operate on mbuf chains.
367de06f907SNik Clayton.Bl -ohang -offset indent
368de06f907SNik Clayton.It Fn m_freem mbuf
369de06f907SNik ClaytonFree an entire mbuf chain, including any external
370de06f907SNik Claytonstorage.
371de06f907SNik Clayton.\"
372de06f907SNik Clayton.It Fn m_adj mbuf len
373de06f907SNik ClaytonTrim
374de06f907SNik Clayton.Fa len
37525f1a2e9SSheldon Hearnbytes from the head of an mbuf chain if
376de06f907SNik Clayton.Fa len
377de06f907SNik Claytonis positive, from the tail otherwise.
378de06f907SNik Clayton.\"
379de06f907SNik Clayton.It Fn m_prepend mbuf len how
380de06f907SNik ClaytonAllocate a new mbuf and prepend it to the chain, handle
381de06f907SNik Clayton.Dv M_PKTHDR
382de06f907SNik Claytonproperly.
383de06f907SNik Clayton.Sy Note :
384de06f907SNik ClaytonIt doesn't allocate any clusters, so
385de06f907SNik Clayton.Fa len
386de06f907SNik Claytonmust be less than
387de06f907SNik Clayton.Dv MLEN
388de06f907SNik Claytonor
389de06f907SNik Clayton.Dv MHLEN ,
390de06f907SNik Claytondepending on the
391d0353b83SRuslan Ermilov.Dv M_PKTHDR
392d0353b83SRuslan Ermilovflag setting.
393de06f907SNik Clayton.\"
394de06f907SNik Clayton.It Fn m_pullup mbuf len
395de06f907SNik ClaytonArrange that the first
396de06f907SNik Clayton.Fa len
39725f1a2e9SSheldon Hearnbytes of an mbuf chain are contiguous and lay in the data area of
398de06f907SNik Clayton.Fa mbuf ,
399de06f907SNik Claytonso they are accessible with
400de06f907SNik Clayton.Fn mtod mbuf type .
401de06f907SNik ClaytonReturn the new chain on success,
402de06f907SNik Clayton.Dv NULL
403de06f907SNik Claytonon failure
404de06f907SNik Clayton.Pq the chain is freed in this case .
405de06f907SNik Clayton.Sy Note :
406de06f907SNik ClaytonIt doesn't allocate any clusters, so
407de06f907SNik Clayton.Fa len
408de06f907SNik Claytonmust be less than
409de06f907SNik Clayton.Dv MHLEN .
410de06f907SNik Clayton.\"
411de06f907SNik Clayton.It Fn m_copym mbuf offset len how
412de06f907SNik ClaytonMake a copy of an mbuf chain starting
413de06f907SNik Clayton.Fa offset
414de06f907SNik Claytonbytes from the beginning, continuing for
415de06f907SNik Clayton.Fa len
416e73145f6SSheldon Hearnbytes.
417e73145f6SSheldon HearnIf
418de06f907SNik Clayton.Fa len
419de06f907SNik Claytonis
420de06f907SNik Clayton.Dv M_COPYALL ,
421de06f907SNik Claytoncopy to the end of the mbuf chain.
422de06f907SNik Clayton.Sy Note :
423de06f907SNik ClaytonThe copy is read-only, because clusters are not
424de06f907SNik Claytoncopied, only their reference counts are incremented.
425de06f907SNik Clayton.\"
426de06f907SNik Clayton.It Fn m_copypacket mbuf how
427de06f907SNik ClaytonCopy an entire packet including header, which must be present.
428de06f907SNik ClaytonThis is an optimized version of the common case
429de06f907SNik Clayton.Fn m_copym mbuf 0 M_COPYALL how .
430de06f907SNik Clayton.Sy Note :
431de06f907SNik Claytonthe copy is read-only, because clusters are not
432de06f907SNik Claytoncopied, only their reference counts are incremented.
433de06f907SNik Clayton.\"
434de06f907SNik Clayton.It Fn m_dup mbuf how
435de06f907SNik ClaytonCopy a packet header mbuf chain into a completely new chain, including
436e73145f6SSheldon Hearncopying any mbuf clusters.
437e73145f6SSheldon HearnUse this instead of
438de06f907SNik Clayton.Fn m_copypacket
439de06f907SNik Claytonwhen you need a writable copy of an mbuf chain.
440de06f907SNik Clayton.\"
441de06f907SNik Clayton.It Fn m_copydata mbuf offset len buf
44225f1a2e9SSheldon HearnCopy data from an mbuf chain starting
443de06f907SNik Clayton.Fa off
444de06f907SNik Claytonbytes from the beginning, continuing for
445de06f907SNik Clayton.Fa len
446de06f907SNik Claytonbytes, into the indicated buffer
447de06f907SNik Clayton.Fa buf .
448de06f907SNik Clayton.\"
449de06f907SNik Clayton.It Fn m_copyback mbuf offset len buf
450de06f907SNik ClaytonCopy
451de06f907SNik Clayton.Fa len
452de06f907SNik Claytonbytes from the buffer
453de06f907SNik Clayton.Fa buf
454de06f907SNik Claytonback into the indicated mbuf chain,
455de06f907SNik Claytonstarting at
456de06f907SNik Clayton.Fa offset
457de06f907SNik Claytonbytes from the beginning of the chain, extending the mbuf chain if necessary.
458de06f907SNik Clayton.Sy Note :
459e73145f6SSheldon HearnIt doesn't allocate any clusters, just adds mbufs to the chain.
460e73145f6SSheldon HearnIt's safe to set
461de06f907SNik Clayton.Fa offset
462de06f907SNik Claytonbeyond the current chain end: zeroed mbufs will be allocated to fill the
463de06f907SNik Claytonspace.
464de06f907SNik Clayton.\"
465de06f907SNik Clayton.It Fn m_devget buf len offset ifp copy
466de06f907SNik ClaytonCopy data from a device local memory pointed to by
467de06f907SNik Clayton.Fa buf
46825f1a2e9SSheldon Hearnto an mbuf chain.
469e73145f6SSheldon HearnThe copy is done using a specified copy routine
470de06f907SNik Clayton.Fa copy ,
471de06f907SNik Claytonor
472de06f907SNik Clayton.Fn bcopy
473de06f907SNik Claytonif
474de06f907SNik Clayton.Fa copy
475de06f907SNik Claytonis
476de06f907SNik Clayton.Dv NULL .
477de06f907SNik Clayton.\"
478de06f907SNik Clayton.It Fn m_cat m n
479de06f907SNik ClaytonConcatenate
480de06f907SNik Clayton.Fa n
481de06f907SNik Claytonto
482de06f907SNik Clayton.Fa m .
483de06f907SNik ClaytonBoth chains must be of the same type.
484de06f907SNik Clayton.Fa N
485de06f907SNik Claytonis still valid after the function returned.
486de06f907SNik Clayton.Sy Note :
487de06f907SNik ClaytonIt does not handle
488de06f907SNik Clayton.Dv M_PKTHDR
489de06f907SNik Claytonand friends.
490de06f907SNik Clayton.\"
491de06f907SNik Clayton.It Fn m_split mbuf len how
492de06f907SNik ClaytonPartition an mbuf chain in two pieces, returning the tail:
493de06f907SNik Claytonall but the first
494de06f907SNik Clayton.Fa len
495e73145f6SSheldon Hearnbytes.
496e73145f6SSheldon HearnIn case of failure, it returns
497de06f907SNik Clayton.Dv NULL
498de06f907SNik Claytonand attempts to restore the chain to its original state.
4993136363fSRuslan Ermilov.El
500de06f907SNik Clayton.Sh RETURN VALUES
501de06f907SNik ClaytonSee above.
502de06f907SNik Clayton.Sh HISTORY
503de06f907SNik Clayton.\" Please correct me if I'm wrong
504e73145f6SSheldon HearnMbufs appeared in an early version of BSD.
505e73145f6SSheldon HearnBesides for being used for network packets, they were used
506bfd59721SBosko Milekicto store various dynamic structures, such as routing table
507bfd59721SBosko Milekicentries, interface addresses, protocol control blocks, etc.
508de06f907SNik Clayton.Sh AUTHORS
509bfd59721SBosko MilekicThe original mbuf(9) man page was written by Yar Tikhiy.
510