xref: /freebsd/share/man/man9/mbuf.9 (revision feccf50ce0e2ce93ab0080abbd508a3be3c5e449)
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_ALIGN "struct mbuf *mbuf" "u_int len"
64bfd59721SBosko Milekic.Fn MH_ALIGN "struct mbuf *mbuf" "u_int len"
65f16b3c0dSChad David.Ft int
66bfd59721SBosko Milekic.Fn M_LEADINGSPACE "struct mbuf *mbuf"
67f16b3c0dSChad David.Ft int
68bfd59721SBosko Milekic.Fn M_TRAILINGSPACE "struct mbuf *mbuf"
69da72b998SYaroslav Tykhiy.Fn M_MOVE_PKTHDR "struct mbuf *to" "struct mbuf *from"
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"
118da72b998SYaroslav Tykhiy.Ft void
119da72b998SYaroslav Tykhiy.Fn m_dup_pkthdr "struct mbuf *to" "struct mbuf *from"
120da72b998SYaroslav Tykhiy.Ft void
121da72b998SYaroslav Tykhiy.Fn m_move_pkthdr "struct mbuf *to" "struct mbuf *from"
122e60fa837SRuslan Ermilov.Ft u_int
1230b1c0994SPoul-Henning Kamp.Fn m_length "struct mbuf *mbuf" "struct mbuf **last"
124de06f907SNik Clayton.Ft struct mbuf *
125de06f907SNik Clayton.Fn m_split "struct mbuf *mbuf" "int len" "int how"
126de06f907SNik Clayton.\"
127de06f907SNik Clayton.Sh DESCRIPTION
1287b2fd183SYaroslav TykhiyAn
1297b2fd183SYaroslav Tykhiy.Vt mbuf
1307b2fd183SYaroslav Tykhiyis a basic unit of memory management in the kernel IPC subsystem.
1317b2fd183SYaroslav TykhiyNetwork packets and socket buffers are stored in
1327b2fd183SYaroslav Tykhiy.Vt mbufs .
1337b2fd183SYaroslav TykhiyA network packet may span multiple
1347b2fd183SYaroslav Tykhiy.Vt mbufs
1357b2fd183SYaroslav Tykhiyarranged into a
1367b2fd183SYaroslav Tykhiy.Vt mbuf chain
137c4d9468eSRuslan Ermilov(linked list),
138de06f907SNik Claytonwhich allows adding or trimming
139bfd59721SBosko Milekicnetwork headers with little overhead.
140de06f907SNik Clayton.Pp
1417b2fd183SYaroslav TykhiyWhile a developer should not bother with
1427b2fd183SYaroslav Tykhiy.Vt mbuf
1437b2fd183SYaroslav Tykhiyinternals without serious
144de06f907SNik Claytonreason in order to avoid incompatibilities with future changes, it
1457b2fd183SYaroslav Tykhiyis useful to understand the general structure of an
1467b2fd183SYaroslav Tykhiy.Vt mbuf .
147de06f907SNik Clayton.Pp
1487b2fd183SYaroslav TykhiyAn
1497b2fd183SYaroslav Tykhiy.Vt mbuf
1507b2fd183SYaroslav Tykhiyconsists of a variable-sized header and a small internal
151e73145f6SSheldon Hearnbuffer for data.
1527b2fd183SYaroslav TykhiyThe total size of an
1537b2fd183SYaroslav Tykhiy.Vt mbuf ,
154bfd59721SBosko Milekic.Dv MSIZE ,
155feccf50cSAlex Dupreis a constant defined in
156feccf50cSAlex Dupre.In sys/param.h .
1577b2fd183SYaroslav TykhiyThe
1587b2fd183SYaroslav Tykhiy.Vt mbuf
1597b2fd183SYaroslav Tykhiyheader includes:
160de06f907SNik Clayton.Pp
161238d9c59SYaroslav Tykhiy.Bl -tag -width "m_nextpkt" -offset indent
162be82a0bcSYaroslav Tykhiy.It Va m_next
163238d9c59SYaroslav Tykhiy.Pq Vt struct mbuf *
164238d9c59SYaroslav TykhiyA pointer to the next
1657b2fd183SYaroslav Tykhiy.Vt mbuf
1667b2fd183SYaroslav Tykhiyin the
167238d9c59SYaroslav Tykhiy.Vt mbuf chain .
168be82a0bcSYaroslav Tykhiy.It Va m_nextpkt
169238d9c59SYaroslav Tykhiy.Pq Vt struct mbuf *
170238d9c59SYaroslav TykhiyA pointer to the next
1717b2fd183SYaroslav Tykhiy.Vt mbuf chain
172238d9c59SYaroslav Tykhiyin the queue.
173be82a0bcSYaroslav Tykhiy.It Va m_data
174238d9c59SYaroslav Tykhiy.Pq Vt caddr_t
175238d9c59SYaroslav TykhiyA pointer to data attached to this
176238d9c59SYaroslav Tykhiy.Vt mbuf .
177be82a0bcSYaroslav Tykhiy.It Va m_len
178238d9c59SYaroslav Tykhiy.Pq Vt int
179238d9c59SYaroslav TykhiyThe length of the data.
180be82a0bcSYaroslav Tykhiy.It Va m_type
181238d9c59SYaroslav Tykhiy.Pq Vt short
182238d9c59SYaroslav TykhiyThe type of the data.
183be82a0bcSYaroslav Tykhiy.It Va m_flags
184238d9c59SYaroslav Tykhiy.Pq Vt int
185238d9c59SYaroslav TykhiyThe
1867b2fd183SYaroslav Tykhiy.Vt mbuf
187238d9c59SYaroslav Tykhiyflags.
188de06f907SNik Clayton.El
189de06f907SNik Clayton.Pp
1907b2fd183SYaroslav TykhiyThe
1917b2fd183SYaroslav Tykhiy.Vt mbuf
1927b2fd183SYaroslav Tykhiyflag bits are defined as follows:
193de06f907SNik Clayton.Bd -literal
194de06f907SNik Clayton/* mbuf flags */
195de06f907SNik Clayton#define	M_EXT		0x0001	/* has associated external storage */
196de06f907SNik Clayton#define	M_PKTHDR	0x0002	/* start of record */
197de06f907SNik Clayton#define	M_EOR		0x0004	/* end of record */
1985d4050ccSBosko Milekic#define	M_RDONLY	0x0008	/* associated data marked read-only */
1995d4050ccSBosko Milekic#define	M_PROTO1	0x0010	/* protocol-specific */
2005d4050ccSBosko Milekic#define	M_PROTO2	0x0020 	/* protocol-specific */
2015d4050ccSBosko Milekic#define	M_PROTO3	0x0040	/* protocol-specific */
2025d4050ccSBosko Milekic#define	M_PROTO4	0x0080	/* protocol-specific */
2035d4050ccSBosko Milekic#define	M_PROTO5	0x0100	/* protocol-specific */
204f434362aSYaroslav Tykhiy#define	M_PROTO6	0x4000	/* protocol-specific (avoid M_BCAST conflict) */
205f434362aSYaroslav Tykhiy#define	M_FREELIST	0x8000	/* mbuf is on the free list */
206de06f907SNik Clayton
207f434362aSYaroslav Tykhiy/* mbuf pkthdr flags (also stored in m_flags) */
2085d4050ccSBosko Milekic#define	M_BCAST		0x0200	/* send/received as link-level broadcast */
2095d4050ccSBosko Milekic#define	M_MCAST		0x0400	/* send/received as link-level multicast */
2105d4050ccSBosko Milekic#define	M_FRAG		0x0800	/* packet is fragment of larger packet */
2115d4050ccSBosko Milekic#define	M_FIRSTFRAG	0x1000	/* packet is first fragment */
2125d4050ccSBosko Milekic#define	M_LASTFRAG	0x2000	/* packet is last fragment */
213de06f907SNik Clayton.Ed
214de06f907SNik Clayton.Pp
2157b2fd183SYaroslav TykhiyThe available
2167b2fd183SYaroslav Tykhiy.Vt mbuf
2177b2fd183SYaroslav Tykhiytypes are defined as follows:
218de06f907SNik Clayton.Bd -literal
219de06f907SNik Clayton/* mbuf types */
220de06f907SNik Clayton#define	MT_DATA		1	/* dynamic (data) allocation */
221de06f907SNik Clayton#define	MT_HEADER	2	/* packet header */
222de06f907SNik Clayton#define	MT_SONAME	8	/* socket name */
223de06f907SNik Clayton#define	MT_FTABLE	11	/* fragment reassembly header */
224d2b73059SYaroslav Tykhiy#define	MT_TAG		13	/* volatile metadata associated to pkts */
225de06f907SNik Clayton#define	MT_CONTROL	14	/* extra-data protocol message */
226de06f907SNik Clayton#define	MT_OOBDATA	15	/* expedited data  */
227de06f907SNik Clayton.Ed
228de06f907SNik Clayton.Pp
229de06f907SNik ClaytonIf the
230de06f907SNik Clayton.Dv M_PKTHDR
231de06f907SNik Claytonflag is set, a
2327b2fd183SYaroslav Tykhiy.Vt struct pkthdr Va m_pkthdr
2337b2fd183SYaroslav Tykhiyis added to the
2347b2fd183SYaroslav Tykhiy.Vt mbuf
2357b2fd183SYaroslav Tykhiyheader.
236e73145f6SSheldon HearnIt contains a pointer to the interface
237de06f907SNik Claytonthe packet has been received from
2387b2fd183SYaroslav Tykhiy.Pq Vt struct ifnet Va *rcvif ,
239de06f907SNik Claytonand the total packet length
2407b2fd183SYaroslav Tykhiy.Pq Vt int Va len .
2410193eb5eSYaroslav TykhiyFields used in offloading checksum calculation to the hardware are kept in
2420193eb5eSYaroslav Tykhiy.Va m_pkthdr
2430193eb5eSYaroslav Tykhiyas well.
2440193eb5eSYaroslav TykhiySee
2450193eb5eSYaroslav Tykhiy.Sx HARDWARE-ASSISTED CHECKSUM CALCULATION
2460193eb5eSYaroslav Tykhiyfor details.
247de06f907SNik Clayton.Pp
2487b2fd183SYaroslav TykhiyIf small enough, data is stored in the internal data buffer of an
2497b2fd183SYaroslav Tykhiy.Vt mbuf .
2507b2fd183SYaroslav TykhiyIf the data is sufficiently large, another
2517b2fd183SYaroslav Tykhiy.Vt mbuf
2527b2fd183SYaroslav Tykhiymay be added to the
2537b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
2547b2fd183SYaroslav Tykhiyor external storage may be associated with the
2557b2fd183SYaroslav Tykhiy.Vt mbuf .
256de06f907SNik Clayton.Dv MHLEN
2577b2fd183SYaroslav Tykhiybytes of data can fit into an
2587b2fd183SYaroslav Tykhiy.Vt mbuf
2597b2fd183SYaroslav Tykhiywith the
260de06f907SNik Clayton.Dv M_PKTHDR
261de06f907SNik Claytonflag set,
262de06f907SNik Clayton.Dv MLEN
263de06f907SNik Claytonbytes can otherwise.
264de06f907SNik Clayton.Pp
2657b2fd183SYaroslav TykhiyIf external storage is being associated with an
2667b2fd183SYaroslav Tykhiy.Vt mbuf ,
2677b2fd183SYaroslav Tykhiythe
268be82a0bcSYaroslav Tykhiy.Va m_ext
26925f1a2e9SSheldon Hearnheader is added at the cost of losing the internal data buffer.
270e73145f6SSheldon HearnIt includes a pointer to external storage, the size of the storage,
271e73145f6SSheldon Hearna pointer to a function used for freeing the storage,
272e73145f6SSheldon Hearna pointer to an optional argument that can be passed to the function,
273e73145f6SSheldon Hearnand a pointer to a reference counter.
2747b2fd183SYaroslav TykhiyAn
2757b2fd183SYaroslav Tykhiy.Vt mbuf
2767b2fd183SYaroslav Tykhiyusing external storage has the
277de06f907SNik Clayton.Dv M_EXT
278de06f907SNik Claytonflag set.
279de06f907SNik Clayton.Pp
280bfd59721SBosko MilekicThe system supplies a macro for allocating the desired external storage
281bfd59721SBosko Milekicbuffer,
282bfd59721SBosko Milekic.Dv MEXTADD .
283bfd59721SBosko Milekic.Pp
284bfd59721SBosko MilekicThe allocation and management of the reference counter is handled by the
285e73145f6SSheldon Hearnsubsystem.
286e73145f6SSheldon HearnThe developer can check whether the reference count for the
2877b2fd183SYaroslav Tykhiyexternal storage of a given
2887b2fd183SYaroslav Tykhiy.Vt mbuf
2897b2fd183SYaroslav Tykhiyis greater than 1 with the
290bfd59721SBosko Milekic.Dv MEXT_IS_REF
291e73145f6SSheldon Hearnmacro.
292e73145f6SSheldon HearnSimilarly, the developer can directly add and remove references,
293e73145f6SSheldon Hearnif absolutely necessary, with the use of the
294bfd59721SBosko Milekic.Dv MEXT_ADD_REF
295bfd59721SBosko Milekicand
296bfd59721SBosko Milekic.Dv MEXT_REM_REF
297bfd59721SBosko Milekicmacros.
298bfd59721SBosko Milekic.Pp
299bfd59721SBosko MilekicThe system also supplies a default type of external storage buffer called an
3007b2fd183SYaroslav Tykhiy.Vt mbuf cluster .
3017b2fd183SYaroslav Tykhiy.Vt Mbuf clusters
3027b2fd183SYaroslav Tykhiycan be allocated and configured with the use of the
303bfd59721SBosko Milekic.Dv MCLGET
304e73145f6SSheldon Hearnmacro.
3057b2fd183SYaroslav TykhiyEach
3067b2fd183SYaroslav Tykhiy.Vt mbuf cluster
3077b2fd183SYaroslav Tykhiyis
308de06f907SNik Clayton.Dv MCLBYTES
309bfd59721SBosko Milekicin size, where MCLBYTES is a machine-dependent constant.
310bfd59721SBosko MilekicThe system defines an advisory macro
311de06f907SNik Clayton.Dv MINCLSIZE ,
3127b2fd183SYaroslav Tykhiywhich is the smallest amount of data to put into an
3137b2fd183SYaroslav Tykhiy.Vt mbuf cluster .
314de06f907SNik ClaytonIt's equal to the sum of
315de06f907SNik Clayton.Dv MLEN
316de06f907SNik Claytonand
317de06f907SNik Clayton.Dv MHLEN .
3187b2fd183SYaroslav TykhiyIt is typically preferable to store data into the data region of an
3197b2fd183SYaroslav Tykhiy.Vt mbuf ,
3207b2fd183SYaroslav Tykhiyif size permits, as opposed to allocating a separate
3217b2fd183SYaroslav Tykhiy.Vt mbuf cluster
3227b2fd183SYaroslav Tykhiyto hold the same data.
323de06f907SNik Clayton.\"
324de06f907SNik Clayton.Ss Macros and Functions
325bfd59721SBosko MilekicThere are numerous predefined macros and functions that provide the
326bfd59721SBosko Milekicdeveloper with common utilities.
327de06f907SNik Clayton.\"
328de06f907SNik Clayton.Bl -ohang -offset indent
329de06f907SNik Clayton.It Fn mtod mbuf type
3307b2fd183SYaroslav TykhiyConvert an
3317b2fd183SYaroslav Tykhiy.Fa mbuf
3327b2fd183SYaroslav Tykhiypointer to a data pointer.
3337b2fd183SYaroslav TykhiyThe macro expands to the data pointer cast to the pointer of the specified
3347b2fd183SYaroslav Tykhiy.Fa type .
335de06f907SNik Clayton.Sy Note :
3367b2fd183SYaroslav TykhiyIt is advisable to ensure that there is enough contiguous data in
3377b2fd183SYaroslav Tykhiy.Fa mbuf .
338de06f907SNik ClaytonSee
339de06f907SNik Clayton.Fn m_pullup
340de06f907SNik Claytonfor details.
341de06f907SNik Clayton.It Fn MGET mbuf how type
3427b2fd183SYaroslav TykhiyAllocate an
3437b2fd183SYaroslav Tykhiy.Vt mbuf
3447b2fd183SYaroslav Tykhiyand initialize it to contain internal data.
34525f1a2e9SSheldon Hearn.Fa mbuf
3467b2fd183SYaroslav Tykhiywill point to the allocated
3477b2fd183SYaroslav Tykhiy.Vt mbuf
3487b2fd183SYaroslav Tykhiyon success, or be set to
349de06f907SNik Clayton.Dv NULL
350e73145f6SSheldon Hearnon failure.
351e73145f6SSheldon HearnThe
352de06f907SNik Clayton.Fa how
353de06f907SNik Claytonargument is to be set to
354c65b53c5STom Rhodes.Dv M_TRYWAIT
355de06f907SNik Claytonor
356c65b53c5STom Rhodes.Dv M_DONTWAIT .
35725f1a2e9SSheldon HearnIt specifies whether the caller is willing to block if necessary.
358e73145f6SSheldon HearnIf
359de06f907SNik Clayton.Fa how
36025f1a2e9SSheldon Hearnis set to
361c65b53c5STom Rhodes.Dv M_TRYWAIT ,
36225f1a2e9SSheldon Hearna failed allocation will result in the caller being put
363bfd59721SBosko Milekicto sleep for a designated
36425f1a2e9SSheldon Hearnkern.ipc.mbuf_wait
365c4d9468eSRuslan Ermilov.Xr ( sysctl 8
366c4d9468eSRuslan Ermilovtunable)
36725f1a2e9SSheldon Hearnnumber of ticks.
3687b2fd183SYaroslav TykhiyA number of other functions and macros related to
3697b2fd183SYaroslav Tykhiy.Vt mbufs
3707b2fd183SYaroslav Tykhiyhave the same argument because they may
3717b2fd183SYaroslav Tykhiyat some point need to allocate new
3727b2fd183SYaroslav Tykhiy.Vt mbufs .
373c65b53c5STom Rhodes.Pp
3747b2fd183SYaroslav TykhiyProgrammers should be careful not to confuse the
3757b2fd183SYaroslav Tykhiy.Vt mbuf
3767b2fd183SYaroslav Tykhiyallocation flag
377c65b53c5STom Rhodes.Dv M_DONTWAIT
378c65b53c5STom Rhodeswith the
379c65b53c5STom Rhodes.Xr malloc 9
380c65b53c5STom Rhodesallocation flag,
381c65b53c5STom Rhodes.Dv M_NOWAIT .
382c65b53c5STom RhodesThey are not the same.
383de06f907SNik Clayton.It Fn MGETHDR mbuf how type
3847b2fd183SYaroslav TykhiyAllocate an
3857b2fd183SYaroslav Tykhiy.Vt mbuf
3867b2fd183SYaroslav Tykhiyand initialize it to contain a packet header
387e73145f6SSheldon Hearnand internal data.
388e73145f6SSheldon HearnSee
389de06f907SNik Clayton.Fn MGET
390de06f907SNik Claytonfor details.
391de06f907SNik Clayton.It Fn MCLGET mbuf how
3927b2fd183SYaroslav TykhiyAllocate and attach an
3937b2fd183SYaroslav Tykhiy.Vt mbuf cluster
3947b2fd183SYaroslav Tykhiyto
3957b2fd183SYaroslav Tykhiy.Fa mbuf .
396e73145f6SSheldon HearnIf the macro fails, the
397de06f907SNik Clayton.Dv M_EXT
3987b2fd183SYaroslav Tykhiyflag won't be set in
3997b2fd183SYaroslav Tykhiy.Fa mbuf .
400da72b998SYaroslav Tykhiy.It Fn M_ALIGN mbuf len
401da72b998SYaroslav TykhiySet the pointer
402da72b998SYaroslav Tykhiy.Fa mbuf->m_data
403da72b998SYaroslav Tykhiyto place an object of the size
404da72b998SYaroslav Tykhiy.Fa len
405da72b998SYaroslav Tykhiyat the end of the internal data area of
406da72b998SYaroslav Tykhiy.Fa mbuf ,
407da72b998SYaroslav Tykhiylong word aligned.
408da72b998SYaroslav TykhiyApplicable only if
409da72b998SYaroslav Tykhiy.Fa mbuf
410da72b998SYaroslav Tykhiyis newly allocated with
411da72b998SYaroslav Tykhiy.Fn MGET
412da72b998SYaroslav Tykhiyor
413da72b998SYaroslav Tykhiy.Fn m_get .
414da72b998SYaroslav Tykhiy.It Fn MH_ALIGN mbuf len
415da72b998SYaroslav TykhiyServes the same purpose as
416da72b998SYaroslav Tykhiy.Fn M_ALIGN
417da72b998SYaroslav Tykhiydoes, but only for
418da72b998SYaroslav Tykhiy.Fa mbuf
419da72b998SYaroslav Tykhiynewly allocated with
420da72b998SYaroslav Tykhiy.Fn MGETHDR
421da72b998SYaroslav Tykhiyor
422da72b998SYaroslav Tykhiy.Fn m_gethdr ,
423da72b998SYaroslav Tykhiyor initialized by
424da72b998SYaroslav Tykhiy.Fn m_dup_pkthdr
425da72b998SYaroslav Tykhiyor
426da72b998SYaroslav Tykhiy.Fn m_move_pkthdr .
427da72b998SYaroslav Tykhiy.It Fn M_LEADINGSPACE mbuf
428da72b998SYaroslav TykhiyReturns the number of bytes available before the beginning
429da72b998SYaroslav Tykhiyof data in
430da72b998SYaroslav Tykhiy.Fa mbuf .
431da72b998SYaroslav Tykhiy.It Fn M_TRAILINGSPACE mbuf
432da72b998SYaroslav TykhiyReturns the number of bytes available after the end of data in
433da72b998SYaroslav Tykhiy.Fa mbuf .
434de06f907SNik Clayton.It Fn M_PREPEND mbuf len how
4357b2fd183SYaroslav TykhiyThis macro operates on an
4367b2fd183SYaroslav Tykhiy.Vt mbuf chain .
437de06f907SNik ClaytonIt is an optimized wrapper for
438de06f907SNik Clayton.Fn m_prepend
439de06f907SNik Claytonthat can make use of possible empty space before data
440c4d9468eSRuslan Ermilov(e.g. left after trimming of a link-layer header).
4417b2fd183SYaroslav TykhiyThe new
4427b2fd183SYaroslav Tykhiy.Vt mbuf chain
4437b2fd183SYaroslav Tykhiypointer or
444de06f907SNik Clayton.Dv NULL
445de06f907SNik Claytonis in
446de06f907SNik Clayton.Fa mbuf
447de06f907SNik Claytonafter the call.
448da72b998SYaroslav Tykhiy.It Fn M_MOVE_PKTHDR to from
449da72b998SYaroslav TykhiyUsing this macro is equivalent to calling
450da72b998SYaroslav Tykhiy.Fn m_move_pkthdr to from .
4515d4050ccSBosko Milekic.It Fn M_WRITABLE mbuf
4527b2fd183SYaroslav TykhiyThis macro will evaluate true if
4537b2fd183SYaroslav Tykhiy.Fa mbuf
4547b2fd183SYaroslav Tykhiyis not marked
4555d4050ccSBosko Milekic.Dv M_RDONLY
4567b2fd183SYaroslav Tykhiyand if either
4577b2fd183SYaroslav Tykhiy.Fa mbuf
4587b2fd183SYaroslav Tykhiydoes not contain external storage or,
4595d4050ccSBosko Milekicif it does,
4605d4050ccSBosko Milekicthen if the reference count of the storage is not greater than 1.
4615d4050ccSBosko MilekicThe
4625d4050ccSBosko Milekic.Dv M_RDONLY
4637b2fd183SYaroslav Tykhiyflag can be set in
4647b2fd183SYaroslav Tykhiy.Fa mbuf->m_flags .
4655d4050ccSBosko MilekicThis can be achieved during setup of the external storage,
4665d4050ccSBosko Milekicby passing the
4675d4050ccSBosko Milekic.Dv M_RDONLY
4685d4050ccSBosko Milekicbit as a
4690b39b3ffSRuslan Ermilov.Fa flags
4705d4050ccSBosko Milekicargument to the
4715d4050ccSBosko Milekic.Fn MEXTADD
4727b2fd183SYaroslav Tykhiymacro, or can be directly set in individual
4737b2fd183SYaroslav Tykhiy.Vt mbufs .
474da72b998SYaroslav Tykhiy.It Fn MCHTYPE mbuf type
475da72b998SYaroslav TykhiyChange the type of
476da72b998SYaroslav Tykhiy.Fa mbuf
477da72b998SYaroslav Tykhiyto
478da72b998SYaroslav Tykhiy.Fa type .
479da72b998SYaroslav TykhiyThis is a relatively expensive operation and should be avoided.
480de06f907SNik Clayton.El
481de06f907SNik Clayton.Pp
482de06f907SNik ClaytonThe functions are:
483de06f907SNik Clayton.Bl -ohang -offset indent
484de06f907SNik Clayton.It Fn m_get how type
485de06f907SNik ClaytonA function version of
486bfd59721SBosko Milekic.Fn MGET
487bfd59721SBosko Milekicfor non-critical paths.
488328c0bbcSBosko Milekic.It Fn m_getm orig len how type
489328c0bbcSBosko MilekicAllocate
4900b39b3ffSRuslan Ermilov.Fa len
4917b2fd183SYaroslav Tykhiybytes worth of
4927b2fd183SYaroslav Tykhiy.Vt mbufs
4937b2fd183SYaroslav Tykhiyand
4947b2fd183SYaroslav Tykhiy.Vt mbuf clusters
4957b2fd183SYaroslav Tykhiyif necessary and append the resulting allocated
4967b2fd183SYaroslav Tykhiy.Vt mbuf chain
4977b2fd183SYaroslav Tykhiyto the
4987b2fd183SYaroslav Tykhiy.Vt mbuf chain
4997b2fd183SYaroslav Tykhiy.Fa orig ,
5007b2fd183SYaroslav Tykhiyif it is
5010b39b3ffSRuslan Ermilov.No non- Ns Dv NULL .
502328c0bbcSBosko MilekicIf the allocation fails at any point,
5030b39b3ffSRuslan Ermilovfree whatever was allocated and return
5040b39b3ffSRuslan Ermilov.Dv NULL .
505328c0bbcSBosko MilekicIf
5060b39b3ffSRuslan Ermilov.Fa orig
5070b39b3ffSRuslan Ermilovis
5080b39b3ffSRuslan Ermilov.No non- Ns Dv NULL ,
509328c0bbcSBosko Milekicit will not be freed.
510328c0bbcSBosko MilekicIt is possible to use
511328c0bbcSBosko Milekic.Fn m_getm
512328c0bbcSBosko Milekicto either append
5130b39b3ffSRuslan Ermilov.Fa len
5147b2fd183SYaroslav Tykhiybytes to an existing
5157b2fd183SYaroslav Tykhiy.Vt mbuf
5167b2fd183SYaroslav Tykhiyor
5177b2fd183SYaroslav Tykhiy.Vt mbuf chain
518328c0bbcSBosko Milekic(for example, one which may be sitting in a pre-allocated ring)
5197b2fd183SYaroslav Tykhiyor to simply perform an all-or-nothing
5207b2fd183SYaroslav Tykhiy.Vt mbuf
5217b2fd183SYaroslav Tykhiyand
5227b2fd183SYaroslav Tykhiy.Vt mbuf cluster
5237b2fd183SYaroslav Tykhiyallocation.
524de06f907SNik Clayton.It Fn m_gethdr how type
525de06f907SNik ClaytonA function version of
526bfd59721SBosko Milekic.Fn MGETHDR
527bfd59721SBosko Milekicfor non-critical paths.
528de06f907SNik Clayton.It Fn m_getclr how type
5297b2fd183SYaroslav TykhiyAllocate an
5307b2fd183SYaroslav Tykhiy.Vt mbuf
5317b2fd183SYaroslav Tykhiyand zero out the data region.
532de06f907SNik Clayton.El
533de06f907SNik Clayton.Pp
5347b2fd183SYaroslav TykhiyThe functions below operate on
5357b2fd183SYaroslav Tykhiy.Vt mbuf chains .
536de06f907SNik Clayton.Bl -ohang -offset indent
537de06f907SNik Clayton.It Fn m_freem mbuf
5387b2fd183SYaroslav TykhiyFree an entire
5397b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
5407b2fd183SYaroslav Tykhiyincluding any external storage.
541de06f907SNik Clayton.\"
542de06f907SNik Clayton.It Fn m_adj mbuf len
543de06f907SNik ClaytonTrim
544de06f907SNik Clayton.Fa len
5457b2fd183SYaroslav Tykhiybytes from the head of an
5467b2fd183SYaroslav Tykhiy.Vt mbuf chain
5477b2fd183SYaroslav Tykhiyif
548de06f907SNik Clayton.Fa len
549de06f907SNik Claytonis positive, from the tail otherwise.
550de06f907SNik Clayton.\"
551de06f907SNik Clayton.It Fn m_prepend mbuf len how
5527b2fd183SYaroslav TykhiyAllocate a new
5537b2fd183SYaroslav Tykhiy.Vt mbuf
5547b2fd183SYaroslav Tykhiyand prepend it to the
5557b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
5567b2fd183SYaroslav Tykhiyhandle
557de06f907SNik Clayton.Dv M_PKTHDR
558de06f907SNik Claytonproperly.
559de06f907SNik Clayton.Sy Note :
5607b2fd183SYaroslav TykhiyIt doesn't allocate any
5617b2fd183SYaroslav Tykhiy.Vt mbuf clusters ,
5627b2fd183SYaroslav Tykhiyso
563de06f907SNik Clayton.Fa len
564de06f907SNik Claytonmust be less than
565de06f907SNik Clayton.Dv MLEN
566de06f907SNik Claytonor
567de06f907SNik Clayton.Dv MHLEN ,
568de06f907SNik Claytondepending on the
569d0353b83SRuslan Ermilov.Dv M_PKTHDR
570d0353b83SRuslan Ermilovflag setting.
571de06f907SNik Clayton.\"
572de06f907SNik Clayton.It Fn m_pullup mbuf len
573de06f907SNik ClaytonArrange that the first
574de06f907SNik Clayton.Fa len
5757b2fd183SYaroslav Tykhiybytes of an
5767b2fd183SYaroslav Tykhiy.Vt mbuf chain
5777b2fd183SYaroslav Tykhiyare contiguous and lay in the data area of
578de06f907SNik Clayton.Fa mbuf ,
579de06f907SNik Claytonso they are accessible with
580de06f907SNik Clayton.Fn mtod mbuf type .
5817b2fd183SYaroslav TykhiyReturn the new
5827b2fd183SYaroslav Tykhiy.Vt mbuf chain
5837b2fd183SYaroslav Tykhiyon success,
584de06f907SNik Clayton.Dv NULL
585de06f907SNik Claytonon failure
5867b2fd183SYaroslav Tykhiy(the
5877b2fd183SYaroslav Tykhiy.Vt mbuf chain
5887b2fd183SYaroslav Tykhiyis freed in this case).
589de06f907SNik Clayton.Sy Note :
5907b2fd183SYaroslav TykhiyIt doesn't allocate any
5917b2fd183SYaroslav Tykhiy.Vt mbuf clusters ,
5927b2fd183SYaroslav Tykhiyso
593de06f907SNik Clayton.Fa len
594de06f907SNik Claytonmust be less than
595de06f907SNik Clayton.Dv MHLEN .
596de06f907SNik Clayton.\"
597de06f907SNik Clayton.It Fn m_copym mbuf offset len how
5987b2fd183SYaroslav TykhiyMake a copy of an
5997b2fd183SYaroslav Tykhiy.Vt mbuf chain
6007b2fd183SYaroslav Tykhiystarting
601de06f907SNik Clayton.Fa offset
602de06f907SNik Claytonbytes from the beginning, continuing for
603de06f907SNik Clayton.Fa len
604e73145f6SSheldon Hearnbytes.
605e73145f6SSheldon HearnIf
606de06f907SNik Clayton.Fa len
607de06f907SNik Claytonis
608de06f907SNik Clayton.Dv M_COPYALL ,
6097b2fd183SYaroslav Tykhiycopy to the end of the
6107b2fd183SYaroslav Tykhiy.Vt mbuf chain .
611de06f907SNik Clayton.Sy Note :
6127b2fd183SYaroslav TykhiyThe copy is read-only, because the
6137b2fd183SYaroslav Tykhiy.Vt mbuf clusters
6147b2fd183SYaroslav Tykhiyare not copied, only their reference counts are incremented.
615de06f907SNik Clayton.\"
616de06f907SNik Clayton.It Fn m_copypacket mbuf how
617de06f907SNik ClaytonCopy an entire packet including header, which must be present.
618de06f907SNik ClaytonThis is an optimized version of the common case
619de06f907SNik Clayton.Fn m_copym mbuf 0 M_COPYALL how .
620de06f907SNik Clayton.Sy Note :
6217b2fd183SYaroslav Tykhiythe copy is read-only, because the
6227b2fd183SYaroslav Tykhiy.Vt mbuf clusters
6237b2fd183SYaroslav Tykhiyare not copied, only their reference counts are incremented.
624de06f907SNik Clayton.\"
625de06f907SNik Clayton.It Fn m_dup mbuf how
6267b2fd183SYaroslav TykhiyCopy a packet header
6277b2fd183SYaroslav Tykhiy.Vt mbuf chain
6287b2fd183SYaroslav Tykhiyinto a completely new
6297b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
6307b2fd183SYaroslav Tykhiyincluding copying any
6317b2fd183SYaroslav Tykhiy.Vt mbuf clusters .
632e73145f6SSheldon HearnUse this instead of
633de06f907SNik Clayton.Fn m_copypacket
6347b2fd183SYaroslav Tykhiywhen you need a writable copy of an
6357b2fd183SYaroslav Tykhiy.Vt mbuf chain .
636de06f907SNik Clayton.\"
637de06f907SNik Clayton.It Fn m_copydata mbuf offset len buf
6387b2fd183SYaroslav TykhiyCopy data from an
6397b2fd183SYaroslav Tykhiy.Vt mbuf chain
6407b2fd183SYaroslav Tykhiystarting
641de06f907SNik Clayton.Fa off
642de06f907SNik Claytonbytes from the beginning, continuing for
643de06f907SNik Clayton.Fa len
644de06f907SNik Claytonbytes, into the indicated buffer
645de06f907SNik Clayton.Fa buf .
646de06f907SNik Clayton.\"
647de06f907SNik Clayton.It Fn m_copyback mbuf offset len buf
648de06f907SNik ClaytonCopy
649de06f907SNik Clayton.Fa len
650de06f907SNik Claytonbytes from the buffer
651de06f907SNik Clayton.Fa buf
6527b2fd183SYaroslav Tykhiyback into the indicated
6537b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
654de06f907SNik Claytonstarting at
655de06f907SNik Clayton.Fa offset
6567b2fd183SYaroslav Tykhiybytes from the beginning of the
6577b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
6587b2fd183SYaroslav Tykhiyextending the
6597b2fd183SYaroslav Tykhiy.Vt mbuf chain
6607b2fd183SYaroslav Tykhiyif necessary.
661de06f907SNik Clayton.Sy Note :
6627b2fd183SYaroslav TykhiyIt doesn't allocate any
6637b2fd183SYaroslav Tykhiy.Vt mbuf clusters ,
6647b2fd183SYaroslav Tykhiyjust adds
6657b2fd183SYaroslav Tykhiy.Vt mbufs
6667b2fd183SYaroslav Tykhiyto the
6677b2fd183SYaroslav Tykhiy.Vt mbuf chain .
668e73145f6SSheldon HearnIt's safe to set
669de06f907SNik Clayton.Fa offset
6707b2fd183SYaroslav Tykhiybeyond the current
6717b2fd183SYaroslav Tykhiy.Vt mbuf chain
6727b2fd183SYaroslav Tykhiyend: zeroed
6737b2fd183SYaroslav Tykhiy.Vt mbufs
6747b2fd183SYaroslav Tykhiywill be allocated to fill the space.
675de06f907SNik Clayton.\"
6765d96084fSYaroslav Tykhiy.It Fn m_length mbuf last
6777b2fd183SYaroslav TykhiyReturn the length of the
6787b2fd183SYaroslav Tykhiy.Vt mbuf chain ,
6797b2fd183SYaroslav Tykhiyand optionally a pointer to the last
6807b2fd183SYaroslav Tykhiy.Vt mbuf .
6810b1c0994SPoul-Henning Kamp.\"
682da72b998SYaroslav Tykhiy.It Fn m_dup_pkthdr to from how
683da72b998SYaroslav TykhiyUpon the function's completion, the
684da72b998SYaroslav Tykhiy.Vt mbuf
685da72b998SYaroslav Tykhiy.Fa to
686da72b998SYaroslav Tykhiywill contain an identical copy of
687da72b998SYaroslav Tykhiy.Fa from->m_pkthdr
688da72b998SYaroslav Tykhiyand the per-packet attributes found in the
689da72b998SYaroslav Tykhiy.Vt mbuf chain
690da72b998SYaroslav Tykhiy.Fa from .
691da72b998SYaroslav TykhiyThe
692da72b998SYaroslav Tykhiy.Vt mbuf
693da72b998SYaroslav Tykhiy.Fa from
694da72b998SYaroslav Tykhiymust have the flag
695da72b998SYaroslav Tykhiy.Dv M_PKTHDR
696da72b998SYaroslav Tykhiyinitially set, and
697da72b998SYaroslav Tykhiy.Fa to
698da72b998SYaroslav Tykhiymust be empty on entry.
699da72b998SYaroslav Tykhiy.\"
700da72b998SYaroslav Tykhiy.It Fn m_move_pkthdr to from
701da72b998SYaroslav TykhiyMove
702da72b998SYaroslav Tykhiy.Va m_pkthdr
703da72b998SYaroslav Tykhiyand the per-packet attributes from the
704da72b998SYaroslav Tykhiy.Vt mbuf chain
705da72b998SYaroslav Tykhiy.Fa from
706da72b998SYaroslav Tykhiyto the
707da72b998SYaroslav Tykhiy.Vt mbuf
708da72b998SYaroslav Tykhiy.Fa to .
709da72b998SYaroslav TykhiyThe
710da72b998SYaroslav Tykhiy.Vt mbuf
711da72b998SYaroslav Tykhiy.Fa from
712da72b998SYaroslav Tykhiymust have the flag
713da72b998SYaroslav Tykhiy.Dv M_PKTHDR
714da72b998SYaroslav Tykhiyinitially set, and
715da72b998SYaroslav Tykhiy.Fa to
716da72b998SYaroslav Tykhiymust be empty on entry.
717da72b998SYaroslav TykhiyUpon the function's completion,
718da72b998SYaroslav Tykhiy.Fa from
719da72b998SYaroslav Tykhiywill have the flag
720da72b998SYaroslav Tykhiy.Dv M_PKTHDR
721da72b998SYaroslav Tykhiyand the per-packet attributes cleared.
722da72b998SYaroslav Tykhiy.\"
7235d96084fSYaroslav Tykhiy.It Fn m_fixhdr mbuf
7247b2fd183SYaroslav TykhiySet the packet-header length to the length of the
7257b2fd183SYaroslav Tykhiy.Vt mbuf chain .
7260b1c0994SPoul-Henning Kamp.\"
727de06f907SNik Clayton.It Fn m_devget buf len offset ifp copy
728de06f907SNik ClaytonCopy data from a device local memory pointed to by
729de06f907SNik Clayton.Fa buf
7307b2fd183SYaroslav Tykhiyto an
7317b2fd183SYaroslav Tykhiy.Vt mbuf chain .
732e73145f6SSheldon HearnThe copy is done using a specified copy routine
733de06f907SNik Clayton.Fa copy ,
734de06f907SNik Claytonor
735de06f907SNik Clayton.Fn bcopy
736de06f907SNik Claytonif
737de06f907SNik Clayton.Fa copy
738de06f907SNik Claytonis
739de06f907SNik Clayton.Dv NULL .
740de06f907SNik Clayton.\"
741de06f907SNik Clayton.It Fn m_cat m n
742de06f907SNik ClaytonConcatenate
743de06f907SNik Clayton.Fa n
744de06f907SNik Claytonto
745de06f907SNik Clayton.Fa m .
7467b2fd183SYaroslav TykhiyBoth
7477b2fd183SYaroslav Tykhiy.Vt mbuf chains
7487b2fd183SYaroslav Tykhiymust be of the same type.
749de06f907SNik Clayton.Fa N
750de06f907SNik Claytonis still valid after the function returned.
751de06f907SNik Clayton.Sy Note :
752de06f907SNik ClaytonIt does not handle
753de06f907SNik Clayton.Dv M_PKTHDR
754de06f907SNik Claytonand friends.
755de06f907SNik Clayton.\"
756de06f907SNik Clayton.It Fn m_split mbuf len how
7577b2fd183SYaroslav TykhiyPartition an
7587b2fd183SYaroslav Tykhiy.Vt mbuf chain
7597b2fd183SYaroslav Tykhiyin two pieces, returning the tail:
760de06f907SNik Claytonall but the first
761de06f907SNik Clayton.Fa len
762e73145f6SSheldon Hearnbytes.
763e73145f6SSheldon HearnIn case of failure, it returns
764de06f907SNik Clayton.Dv NULL
7657b2fd183SYaroslav Tykhiyand attempts to restore the
7667b2fd183SYaroslav Tykhiy.Vt mbuf chain
7677b2fd183SYaroslav Tykhiyto its original state.
7683136363fSRuslan Ermilov.El
7690193eb5eSYaroslav Tykhiy.Sh HARDWARE-ASSISTED CHECKSUM CALCULATION
7700193eb5eSYaroslav TykhiyThis section currently applies to TCP/IP only.
7710193eb5eSYaroslav TykhiyIn order to save the host CPU resources, computing checksums is
7720193eb5eSYaroslav Tykhiyoffloaded to the network interface hardware if possible.
7730193eb5eSYaroslav TykhiyThe
7740193eb5eSYaroslav Tykhiy.Va m_pkthdr
7750193eb5eSYaroslav Tykhiymember of the leading
7760193eb5eSYaroslav Tykhiy.Vt mbuf
7770193eb5eSYaroslav Tykhiyof a packet contains two fields used for that purpose,
7780193eb5eSYaroslav Tykhiy.Vt int Va csum_flags
7790193eb5eSYaroslav Tykhiyand
7800193eb5eSYaroslav Tykhiy.Vt int Va csum_data .
7810193eb5eSYaroslav TykhiyThe meaning of those fields depends on the direction a packet flows in,
7820193eb5eSYaroslav Tykhiyand on whether the packet is fragmented.
7830193eb5eSYaroslav TykhiyHenceforth,
7840193eb5eSYaroslav Tykhiy.Va csum_flags
7850193eb5eSYaroslav Tykhiyor
7860193eb5eSYaroslav Tykhiy.Va csum_data
7870193eb5eSYaroslav Tykhiyof a packet
7880193eb5eSYaroslav Tykhiywill denote the corresponding field of the
7890193eb5eSYaroslav Tykhiy.Va m_pkthdr
7900193eb5eSYaroslav Tykhiymember of the leading
7910193eb5eSYaroslav Tykhiy.Vt mbuf
7920193eb5eSYaroslav Tykhiyin the
7930193eb5eSYaroslav Tykhiy.Vt mbuf chain
7940193eb5eSYaroslav Tykhiycontaining the packet.
7950193eb5eSYaroslav Tykhiy.Pp
7960193eb5eSYaroslav TykhiyOn output, checksum offloading is attempted after the outgoing
7970193eb5eSYaroslav Tykhiyinterface has been determined for a packet.
7980193eb5eSYaroslav TykhiyThe interface-specific field
7990193eb5eSYaroslav Tykhiy.Va ifnet.if_data.ifi_hwassist
8000193eb5eSYaroslav Tykhiy(see
8010193eb5eSYaroslav Tykhiy.Xr ifnet 9 )
8020193eb5eSYaroslav Tykhiyis consulted for the capabilities of the interface to assist in
8030193eb5eSYaroslav Tykhiycomputing checksums.
8040193eb5eSYaroslav TykhiyThe
8050193eb5eSYaroslav Tykhiy.Va csum_flags
8060193eb5eSYaroslav Tykhiyfield of the packet is set to indicate which actions the interface
8070193eb5eSYaroslav Tykhiyis supposed to perform on it.
8080193eb5eSYaroslav TykhiyThe actions unsupported by the network interface are done in the
8090193eb5eSYaroslav Tykhiysoftware prior to passing the packet down to the interface driver;
8100193eb5eSYaroslav Tykhiysuch actions will never be requested through
8110193eb5eSYaroslav Tykhiy.Va csum_flags .
8120193eb5eSYaroslav Tykhiy.Pp
8130193eb5eSYaroslav TykhiyThe flags demanding a particular action from an interface are as follows:
8140193eb5eSYaroslav Tykhiy.Bl -tag -width ".Dv CSUM_TCP" -offset indent
8150193eb5eSYaroslav Tykhiy.It Dv CSUM_IP
8160193eb5eSYaroslav TykhiyThe IP header checksum is to be computed and stored in the
8170193eb5eSYaroslav Tykhiycorresponding field of the packet.
8180193eb5eSYaroslav TykhiyThe hardware is expected to know the format of an IP header
8190193eb5eSYaroslav Tykhiyto determine the offset of the IP checksum field.
8200193eb5eSYaroslav Tykhiy.It Dv CSUM_TCP
8210193eb5eSYaroslav TykhiyThe TCP checksum is to be computed. (See below.)
8220193eb5eSYaroslav Tykhiy.It Dv CSUM_UDP
8230193eb5eSYaroslav TykhiyThe UDP checksum is to be computed. (See below.)
8240193eb5eSYaroslav Tykhiy.El
8250193eb5eSYaroslav Tykhiy.Pp
826fbde705aSYaroslav TykhiyShould a TCP or UDP checksum be offloaded to the hardware,
8270193eb5eSYaroslav Tykhiythe field
8280193eb5eSYaroslav Tykhiy.Va csum_data
8290193eb5eSYaroslav Tykhiywill contain the byte offset of the checksum field relative to the
8300193eb5eSYaroslav Tykhiyend of the IP header.
8310193eb5eSYaroslav TykhiyIn this case, the checksum field will be initially
8320193eb5eSYaroslav Tykhiyset by the TCP/IP module to the checksum of the pseudo header
833fbde705aSYaroslav Tykhiydefined by the TCP and UDP specifications.
8340193eb5eSYaroslav Tykhiy.Pp
8350193eb5eSYaroslav TykhiyFor outbound packets which have been fragmented
8360193eb5eSYaroslav Tykhiyby the host CPU, the following will also be true,
8370193eb5eSYaroslav Tykhiyregardless of the checksum flag settings:
8380193eb5eSYaroslav Tykhiy.Bl -bullet -offset indent
8390193eb5eSYaroslav Tykhiy.It
8400193eb5eSYaroslav Tykhiyall fragments will have the flag
8410193eb5eSYaroslav Tykhiy.Dv M_FRAG
8420193eb5eSYaroslav Tykhiyset in their
8430193eb5eSYaroslav Tykhiy.Va m_flags
8440193eb5eSYaroslav Tykhiyfield;
8450193eb5eSYaroslav Tykhiy.It
8460193eb5eSYaroslav Tykhiythe first and the last fragments in the chain will have
8470193eb5eSYaroslav Tykhiy.Dv M_FIRSTFRAG
8480193eb5eSYaroslav Tykhiyor
8490193eb5eSYaroslav Tykhiy.Dv M_LASTFRAG
8500193eb5eSYaroslav Tykhiyset in their
8510193eb5eSYaroslav Tykhiy.Va m_flags ,
8520193eb5eSYaroslav Tykhiycorrespondingly;
8530193eb5eSYaroslav Tykhiy.It
8540193eb5eSYaroslav Tykhiythe first fragment in the chain will have the total number
8550193eb5eSYaroslav Tykhiyof fragments contained in its
8560193eb5eSYaroslav Tykhiy.Va csum_data
8570193eb5eSYaroslav Tykhiyfield.
8580193eb5eSYaroslav Tykhiy.El
8590193eb5eSYaroslav Tykhiy.Pp
86076f80df6SYaroslav TykhiyThe last rule for fragmented packets takes precedence over the one
86176f80df6SYaroslav Tykhiyfor a TCP or UDP checksum.
86276f80df6SYaroslav TykhiyNevertheless, offloading a TCP or UDP checksum is possible for a
86376f80df6SYaroslav Tykhiyfragmented packet if the flag
86476f80df6SYaroslav Tykhiy.Dv CSUM_IP_FRAGS
86576f80df6SYaroslav Tykhiyis set in the field
86676f80df6SYaroslav Tykhiy.Va ifnet.if_data.ifi_hwassist
86776f80df6SYaroslav Tykhiyassociated with the network interface.
86876f80df6SYaroslav TykhiyHowever, in this case the interface is expected to figure out
86976f80df6SYaroslav Tykhiythe location of the checksum field within the sequence of fragments
87076f80df6SYaroslav Tykhiyby itself because
8710193eb5eSYaroslav Tykhiy.Va csum_data
87276f80df6SYaroslav Tykhiycontains a fragment count instead of a checksum offset value.
8730193eb5eSYaroslav Tykhiy.Pp
8740193eb5eSYaroslav TykhiyOn input, an interface indicates the actions it has performed
8750193eb5eSYaroslav Tykhiyon a packet by setting one or more of the following flags in
8760193eb5eSYaroslav Tykhiy.Va csum_flags
8770193eb5eSYaroslav Tykhiyassociated with the packet:
8780193eb5eSYaroslav Tykhiy.Bl -tag -width ".Dv CSUM_IP_CHECKED" -offset indent
8790193eb5eSYaroslav Tykhiy.It Dv CSUM_IP_CHECKED
8800193eb5eSYaroslav TykhiyThe IP header checksum has been computed.
8810193eb5eSYaroslav Tykhiy.It Dv CSUM_IP_VALID
8820193eb5eSYaroslav TykhiyThe IP header has a valid checksum.
8830193eb5eSYaroslav TykhiyThis flag can appear only in combination with
8840193eb5eSYaroslav Tykhiy.Dv CSUM_IP_CHECKED .
8850193eb5eSYaroslav Tykhiy.It Dv CSUM_DATA_VALID
8860193eb5eSYaroslav TykhiyThe checksum of the data portion of the IP packet has been computed
8870193eb5eSYaroslav Tykhiyand stored in the field
8880193eb5eSYaroslav Tykhiy.Va csum_data
8890193eb5eSYaroslav Tykhiyin network byte order.
8900193eb5eSYaroslav Tykhiy.It Dv CSUM_PSEUDO_HDR
8910193eb5eSYaroslav TykhiyCan be set only along with
8920193eb5eSYaroslav Tykhiy.Dv CSUM_DATA_VALID
8930193eb5eSYaroslav Tykhiyto indicate that the IP data checksum found in
8940193eb5eSYaroslav Tykhiy.Va csum_data
895fbde705aSYaroslav Tykhiyallows for the pseudo header defined by the TCP and UDP specifications.
8960193eb5eSYaroslav TykhiyOtherwise the checksum of the pseudo header must be calculated by
8970193eb5eSYaroslav Tykhiythe host CPU and added to
8980193eb5eSYaroslav Tykhiy.Va csum_data
899fbde705aSYaroslav Tykhiyto obtain the final checksum to be used for TCP or UDP validation purposes.
9000193eb5eSYaroslav Tykhiy.El
9010193eb5eSYaroslav Tykhiy.Pp
9020193eb5eSYaroslav TykhiyIf a particular network interface just indicates success or
9030193eb5eSYaroslav Tykhiyfailure of TCP or UDP checksum validation without returning
9040193eb5eSYaroslav Tykhiythe exact value of the checksum to the host CPU, its driver can mark
9050193eb5eSYaroslav Tykhiy.Dv CSUM_DATA_VALID
9060193eb5eSYaroslav Tykhiyand
9070193eb5eSYaroslav Tykhiy.Dv CSUM_PSEUDO_HDR
9080193eb5eSYaroslav Tykhiyin
9090193eb5eSYaroslav Tykhiy.Va csum_flags ,
9100193eb5eSYaroslav Tykhiyand set
9110193eb5eSYaroslav Tykhiy.Va csum_data
9120193eb5eSYaroslav Tykhiyto
9130193eb5eSYaroslav Tykhiy.Li 0xFFFF
9140193eb5eSYaroslav Tykhiyhexadecimal to indicate a valid checksum.
9150193eb5eSYaroslav TykhiyIt's a peculiarity of the algorithm used that the Internet checksum
9160193eb5eSYaroslav Tykhiycalculated over any valid packet will be
9170193eb5eSYaroslav Tykhiy.Li 0xFFFF
9180193eb5eSYaroslav Tykhiyas long as the original checksum field is included.
9190193eb5eSYaroslav Tykhiy.Pp
9200193eb5eSYaroslav TykhiyFor inbound packets which are IP fragments, all
9210193eb5eSYaroslav Tykhiy.Va csum_data
9220193eb5eSYaroslav Tykhiyfields will be summed during reassembly to obtain the final checksum
9230193eb5eSYaroslav Tykhiyvalue passed to an upper layer in the
9240193eb5eSYaroslav Tykhiy.Va csum_data
9250193eb5eSYaroslav Tykhiyfield of the reassembled packet.
9260193eb5eSYaroslav TykhiyThe
9270193eb5eSYaroslav Tykhiy.Va csum_flags
9280193eb5eSYaroslav Tykhiyfields of all fragments will be consolidated using logical AND
9290193eb5eSYaroslav Tykhiyto obtain the final value for
9300193eb5eSYaroslav Tykhiy.Va csum_flags .
9310193eb5eSYaroslav TykhiyThus, in order to successfully
9320193eb5eSYaroslav Tykhiyoffload checksum computation for fragmented data,
9330193eb5eSYaroslav Tykhiyall fragments should have the same value of
9340193eb5eSYaroslav Tykhiy.Va csum_flags .
9353cab047eSMike Silbersack.Sh STRESS TESTING
936c8185672SRuslan ErmilovWhen running a kernel compiled with the option
937c8185672SRuslan Ermilov.Dv MBUF_STRESS_TEST ,
938c8185672SRuslan Ermilovthe following
939c8185672SRuslan Ermilov.Xr sysctl 8 Ns
940c8185672SRuslan Ermilov-controlled options may be used to create
9413cab047eSMike Silbersackvarious failure/extreme cases for testing of network drivers
9427b2fd183SYaroslav Tykhiyand other parts of the kernel that rely on
9437b2fd183SYaroslav Tykhiy.Vt mbufs .
944c8185672SRuslan Ermilov.Bl -tag -width ident
945c8185672SRuslan Ermilov.It Va net.inet.ip.mbuf_frag_size
946c8185672SRuslan ErmilovCauses
947c8185672SRuslan Ermilov.Fn ip_output
9487b2fd183SYaroslav Tykhiyto fragment outgoing
9497b2fd183SYaroslav Tykhiy.Vt mbuf chains
9507b2fd183SYaroslav Tykhiyinto fragments of the specified size.
951c8185672SRuslan ErmilovSetting this variable to 1 is an excellent way to
9527b2fd183SYaroslav Tykhiytest the long
9537b2fd183SYaroslav Tykhiy.Vt mbuf chain
9547b2fd183SYaroslav Tykhiyhandling ability of network drivers.
955c8185672SRuslan Ermilov.It Va kern.ipc.m_defragrandomfailures
956c8185672SRuslan ErmilovCauses the function
957c8185672SRuslan Ermilov.Fn m_defrag
958c8185672SRuslan Ermilovto randomly fail, returning
959c8185672SRuslan Ermilov.Dv NULL .
960c8185672SRuslan ErmilovAny piece of code which uses
961c8185672SRuslan Ermilov.Fn m_defrag
962c8185672SRuslan Ermilovshould be tested with this feature.
963c8185672SRuslan Ermilov.El
964de06f907SNik Clayton.Sh RETURN VALUES
965de06f907SNik ClaytonSee above.
9660193eb5eSYaroslav Tykhiy.Sh SEE ALSO
9670193eb5eSYaroslav Tykhiy.Xr ifnet 9 .
968de06f907SNik Clayton.Sh HISTORY
969de06f907SNik Clayton.\" Please correct me if I'm wrong
9707b2fd183SYaroslav Tykhiy.Vt Mbufs
9717b2fd183SYaroslav Tykhiyappeared in an early version of
972753d686dSRuslan Ermilov.Bx .
97353b2520dSTom RhodesBesides being used for network packets, they were used
974bfd59721SBosko Milekicto store various dynamic structures, such as routing table
975bfd59721SBosko Milekicentries, interface addresses, protocol control blocks, etc.
976de06f907SNik Clayton.Sh AUTHORS
9776575e6daSRuslan ErmilovThe original
9786575e6daSRuslan Ermilov.Nm
9796575e6daSRuslan Ermilovman page was written by Yar Tikhiy.
980