1 .\" Copyright (c) 2000 FreeBSD Inc.
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED. IN NO EVENT SHALL [your name] OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 .\" $FreeBSD: src/share/man/man9/mbuf.9,v 1.27.2.1 2003/05/28 13:53:18 yar Exp $
26 .\" $DragonFly: src/share/man/man9/mbuf.9,v 1.8 2007/08/18 18:58:20 swildner Exp $
28 .Dd September 17, 2010
34 .Nd "memory management in the kernel IPC subsystem"
41 .Ss Mbuf allocation macros
42 .Fn MGET "struct mbuf *mbuf" "int how" "short type"
43 .Fn MGETHDR "struct mbuf *mbuf" "int how" "short type"
44 .Fn MCLGET "struct mbuf *mbuf" "int how"
46 .Ss Mbuf utility macros
48 .Fn mtod "struct mbuf *mbuf" "type"
49 .Fn M_ALIGN "struct mbuf *mbuf" "u_int len"
50 .Fn MH_ALIGN "struct mbuf *mbuf" "u_int len"
52 .Fn M_LEADINGSPACE "struct mbuf *mbuf"
54 .Fn M_TRAILINGSPACE "struct mbuf *mbuf"
55 .Fn M_PREPEND "struct mbuf *mbuf" "int len" "int how"
57 .Ss Mbuf allocation functions
59 .Fn m_get "int how" "int type"
61 .Fn m_getm "struct mbuf *orig" "int len" "int how" "int type"
63 .Fn m_getclr "int how" "int type"
65 .Fn m_gethdr "int how" "int type"
67 .Fn m_free "struct mbuf *mbuf"
69 .Fn m_freem "struct mbuf *mbuf"
71 .Ss Mbuf utility functions
73 .Fn m_adj "struct mbuf *mbuf" "int len"
75 .Fn m_prepend "struct mbuf *mbuf" "int len" "int how"
77 .Fn m_pullup "struct mbuf *mbuf" "int len"
79 .Fn m_copym "struct mbuf *mbuf" "int offset" "int len" "int how"
81 .Fn m_copypacket "struct mbuf *mbuf" "int how"
83 .Fn m_dup "struct mbuf *mbuf" "int how"
85 .Fn m_copydata "const struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
87 .Fn m_copyback "struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
93 .Fa "struct ifnet *ifp"
94 .Fa "void (*copy)(char *from, caddr_t to, u_int len)"
97 .Fn m_cat "struct mbuf *m" "struct mbuf *n"
99 .Fn m_split "struct mbuf *mbuf" "int len" "int how"
101 .Fn m_unshare "struct mbuf *mbuf" "int how"
104 An mbuf is a basic unit of memory management in the kernel IPC subsystem.
105 Network packets and socket buffers are stored in mbufs.
106 A network packet may span multiple mbufs arranged into a chain
108 which allows adding or trimming
109 network headers with little overhead.
111 While a developer should not bother with mbuf internals without serious
112 reason in order to avoid incompatibilities with future changes, it
113 is useful to understand the mbuf's general structure.
115 An mbuf consists of a variable-sized header and a small internal
117 The mbuf's total size,
119 is a machine-dependent constant defined in
120 .In machine/param.h .
121 The mbuf header includes:
123 .Bl -tag -width "m_nextpkt" -compact -offset indent
125 a pointer to the next buffer in the chain
127 a pointer to the next chain in the queue
129 a pointer to the data
131 the length of the data
138 The mbuf flag bits are defined as follows:
141 #define M_EXT 0x0001 /* has associated external storage */
142 #define M_PKTHDR 0x0002 /* start of record */
143 #define M_EOR 0x0004 /* end of record */
144 #define M_PROTO1 0x0010 /* protocol-specific */
145 #define M_PROTO2 0x0020 /* protocol-specific */
146 #define M_PROTO3 0x0040 /* protocol-specific */
147 #define M_PROTO4 0x0080 /* protocol-specific */
148 #define M_PROTO5 0x0100 /* protocol-specific */
150 /* mbuf pkthdr flags, also in m_flags */
151 #define M_BCAST 0x0200 /* send/received as link-level broadcast */
152 #define M_MCAST 0x0400 /* send/received as link-level multicast */
153 #define M_FRAG 0x0800 /* packet is fragment of larger packet */
154 #define M_FIRSTFRAG 0x1000 /* packet is first fragment */
155 #define M_LASTFRAG 0x2000 /* packet is last fragment */
158 The available mbuf types are defined as follows:
161 #define MT_FREE 0 /* should be on free list */
162 #define MT_DATA 1 /* dynamic (data) allocation */
163 #define MT_HEADER 2 /* packet header */
164 #define MT_SONAME 8 /* socket name */
165 #define MT_FTABLE 11 /* fragment reassembly header */
166 #define MT_CONTROL 14 /* extra-data protocol message */
167 #define MT_OOBDATA 15 /* expedited data */
173 .Li struct pkthdr m_pkthdr
174 is added to the mbuf header.
175 It contains a pointer to the interface
176 the packet has been received from
177 .Pq Fa struct ifnet *rcvif ,
178 and the total packet length
181 If small enough, data is stored in the mbuf's internal data buffer.
182 If the data is sufficiently large, another mbuf may be added to the chain,
183 or external storage may be associated with the mbuf.
185 bytes of data can fit into an mbuf with the
191 If external storage is being associated with an mbuf, the
193 header is added at the cost of losing the internal data buffer.
194 It includes a pointer to external storage, the size of the storage,
195 a pointer to a function used for freeing the storage,
196 a pointer to an optional argument that can be passed to the function,
197 and a pointer to a reference counter.
198 An mbuf using external storage has the
202 The system supplies a default type of external storage buffer called an
204 Mbuf clusters can be allocated and configured with the use of the
211 is a machine-dependent constant.
212 The system defines an advisory macro
214 which is the smallest amount of data to put into a cluster.
215 It's equal to the sum of
219 It is typically preferable to store data into an mbuf's data region, if size
220 permits, as opposed to allocating a separate mbuf cluster to hold the same
223 .Ss Macros and Functions
224 There are numerous predefined macros and functions that provide the
225 developer with common utilities.
227 .Bl -ohang -offset indent
228 .It Fn mtod mbuf type
229 Convert an mbuf pointer to a data pointer.
230 The macro expands to the data pointer cast to the pointer of the specified type.
232 It is advisable to ensure that there is enough contiguous data in the mbuf.
236 .It Fn MGET mbuf how type
237 Allocate an mbuf and initialize it to contain internal data.
239 will point to the allocated mbuf on success, or be set to
244 argument is to be set to
248 It specifies whether the caller is willing to block if necessary.
253 a failed allocation will result in the caller being put
254 to sleep for a designated
255 .Va kern.ipc.mbuf_wait
259 A number of other mbuf-related
260 functions and macros have the same argument because they may
261 at some point need to allocate new mbufs.
263 Programmers should be careful not to confuse the mbuf allocation flag
269 They are not the same.
270 .It Fn MGETHDR mbuf how type
271 Allocate an mbuf and initialize it to contain a packet header
276 .It Fn MCLGET mbuf how
277 Allocate and attach an mbuf cluster to an mbuf.
278 If the macro fails, the
280 flag won't be set in the mbuf.
281 .It Fn M_PREPEND mbuf len how
282 This macro operates on an mbuf chain.
283 It is an optimized wrapper for
285 that can make use of possible empty space before data
286 (e.g. left after trimming of a link-layer header).
287 The new chain pointer or
295 .Bl -ohang -offset indent
296 .It Fn m_get how type
297 A function version of
299 for non-critical paths.
300 .It Fn m_getm orig len how type
303 bytes worth of mbufs and mbuf clusters if necessary and append the resulting
304 allocated chain to the
307 .No non- Ns Dv NULL .
308 If the allocation fails at any point,
309 free whatever was allocated and return
314 .No non- Ns Dv NULL ,
315 it will not be freed.
316 It is possible to use
320 bytes to an existing mbuf or mbuf chain
321 (for example, one which may be sitting in a pre-allocated ring)
322 or to simply perform an all-or-nothing mbuf and mbuf cluster allocation.
323 .It Fn m_gethdr how type
324 A function version of
326 for non-critical paths.
327 .It Fn m_getclr how type
328 Allocate an mbuf and zero out the data region.
331 The functions below operate on mbuf chains.
332 .Bl -ohang -offset indent
334 Free an entire mbuf chain, including any external
337 .It Fn m_adj mbuf len
340 bytes from the head of an mbuf chain if
342 is positive, from the tail otherwise.
344 .It Fn m_prepend mbuf len how
345 Allocate a new mbuf and prepend it to the chain, handle
349 It doesn't allocate any clusters, so
359 .It Fn m_pullup mbuf len
360 Arrange that the first
362 bytes of an mbuf chain are contiguous and lay in the data area of
364 so they are accessible with
366 Return the new chain on success,
369 (the chain is freed in this case).
371 It doesn't allocate any clusters, so
376 .It Fn m_copym mbuf offset len how
377 Make a copy of an mbuf chain starting
379 bytes from the beginning, continuing for
386 copy to the end of the mbuf chain.
388 The copy is read-only, because clusters are not
389 copied, only their reference counts are incremented.
391 .It Fn m_copypacket mbuf how
392 Copy an entire packet including header, which must be present.
393 This is an optimized version of the common case
394 .Fn m_copym mbuf 0 M_COPYALL how .
396 the copy is read-only, because clusters are not
397 copied, only their reference counts are incremented.
399 .It Fn m_dup mbuf how
400 Copy a packet header mbuf chain into a completely new chain, including
401 copying any mbuf clusters.
404 when you need a writable copy of an mbuf chain.
406 .It Fn m_copydata mbuf offset len buf
407 Copy data from an mbuf chain starting
409 bytes from the beginning, continuing for
411 bytes, into the indicated buffer
414 .It Fn m_copyback mbuf offset len buf
417 bytes from the buffer
419 back into the indicated mbuf chain,
422 bytes from the beginning of the chain, extending the mbuf chain if necessary.
424 It doesn't allocate any clusters, just adds mbufs to the chain.
427 beyond the current chain end: zeroed mbufs will be allocated to fill the
430 .It Fn m_devget buf len offset ifp copy
431 Copy data from a device local memory pointed to by
434 The copy is done using a specified copy routine
448 Both chains must be of the same type.
450 is still valid after the function returned.
456 .It Fn m_split mbuf len how
457 Partition an mbuf chain in two pieces, returning the tail:
461 In case of failure, it returns
463 and attempts to restore the chain to its original state.
464 .It Fn m_unshare mbuf how
465 Create a version of the specified mbuf chain whose
466 contents can be safely modified without affecting other users.
467 If allocation fails and this operation can not be completed,
470 The original mbuf chain is always reclaimed and the reference
471 count of any shared mbuf clusters is decremented.
472 As a side-effect of this process the returned
473 mbuf chain may be compacted.
475 This function is especially useful in the transmit path of
476 network code, when data must be encrypted or otherwise
477 altered prior to transmission.
481 When running a kernel compiled with the option
482 .Dv MBUF_STRESS_TEST ,
485 -controlled options may be used to create
486 various failure/extreme cases for testing of network drivers
487 and other parts of the kernel that rely on
489 .Bl -tag -width ident
490 .It Va net.inet.ip.mbuf_frag_size
495 into fragments of the specified size.
496 Setting this variable to 1 is an excellent way to
499 handling ability of network drivers.
500 .It Va kern.ipc.m_defragrandomfailures
503 to randomly fail, returning
505 Any piece of code which uses
507 should be tested with this feature.
512 .\" Please correct me if I'm wrong
513 Mbufs appeared in an early version of
515 Besides for being used for network packets, they were used
516 to store various dynamic structures, such as routing table
517 entries, interface addresses, protocol control blocks, etc.
521 man page was written by Yar Tikhiy.