1 .\" $NetBSD: mbuf.9,v 1.48 2008/05/31 16:24:59 enami Exp $
3 .\" Copyright (c) 1997 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This documentation is derived from text contributed to The NetBSD Foundation
7 .\" by S.P.Zeidler (aka stargazer).
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
70 .Nd "functions and macros for managing memory used by networking code"
74 .Fn m_get "int nowait" "int type"
76 .Fn m_getclr "int nowait" "int type"
78 .Fn m_gethdr "int nowait" "int type"
80 .Fn m_devget "char *buf" "int totlen" "int off0" "struct ifnet *ifp" "void (*copy)(const void *, void *, size_t)"
82 .Fn m_copym "struct mbuf *m" "int off0" "int len" "int wait"
84 .Fn m_copypacket "struct mbuf *m" "int how"
86 .Fn m_copydata "struct mbuf *m" "int off" "int len" "void *cp"
88 .Fn m_copyback "struct mbuf *m0" "int off" "int len" "void *cp"
90 .Fn m_copyback_cow "struct mbuf *m0" "int off" "int len" "void *cp" "int how"
92 .Fn m_makewritable "struct mbuf **mp" "int off" "int len" "int how"
94 .Fn m_cat "struct mbuf *m" "struct mbuf *n"
96 .Fn m_dup "struct mbuf *m" "int off0" "int len" "int wait"
98 .Fn m_prepend "struct mbuf *m" "int len" "int how"
100 .Fn m_pulldown "struct mbuf *m" "int off" "int len" "int *offp"
102 .Fn m_pullup "struct mbuf *n" "int len"
104 .Fn m_split "struct mbuf *m0" "int len0" "int wait"
106 .Fn m_adj "struct mbuf *mp" "int req_len"
108 .Fn m_apply "struct mbuf *m" "int off" "int len" "int *f(void *, void *, unsigned int)" "void *arg"
110 .Fn m_free "struct mbuf *m"
112 .Fn m_freem "struct mbuf *m"
114 .Fn mtod "struct mbuf *m" "datatype"
116 .Fn MGET "struct mbuf *m" "int how" "int type"
118 .Fn MGETHDR "struct mbuf *m" "int how" "int type"
120 .Fn MEXTMALLOC "struct mbuf *m" "int len" "int how"
122 .Fn MEXTADD "struct mbuf *m" "void *buf" "int size" "int type" "void (*free)(struct mbuf *, void *, size_t, void *)" "void *arg"
124 .Fn MCLGET "struct mbuf *m" "int how"
126 .Fn M_COPY_PKTHDR "struct mbuf *to" "struct mbuf *from"
128 .Fn M_MOVE_PKTHDR "struct mbuf *to" "struct mbuf *from"
130 .Fn M_ALIGN "struct mbuf *m" "int len"
132 .Fn MH_ALIGN "struct mbuf *m" "int len"
134 .Fn M_LEADINGSPACE "struct mbuf *m"
136 .Fn M_TRAILINGSPACE "struct mbuf *m"
138 .Fn M_PREPEND "struct mbuf *m" "int plen" "int how"
140 .Fn MCHTYPE "struct mbuf *m" "int type"
142 .Fn MFREE "struct mbuf *m" "struct mbuf *n"
146 functions and macros provide an easy and consistent way to handle
147 a networking stack's memory management needs.
150 consists of a header and a data area.
151 It is of a fixed size,
153 .Pq defined in Aq Pa machine/param.h ,
154 which includes overhead.
155 The header contains a pointer to the next
159 a pointer to the next
161 a pointer to the data area, the amount of data in this mbuf, its type
168 variable can signify:
169 .Bl -tag -compact -offset indent -width "XXXXXXXXXXX"
171 the mbuf should be on the ``free'' list
173 data was dynamically allocated
175 data is a packet header
177 data is a socket name
179 data is socket options
181 data is the fragment reassembly header
183 mbuf contains ancillary \&(protocol control\&) data
185 mbuf contains out-of-band data.
190 variable contains information describing the
193 .Bl -tag -compact -offset indent -width "XXXXXXXXXXX"
201 external storage is a cluster.
206 designates the start of a record
210 field may contain additional information describing the content of
212 .Bl -tag -compact -offset indent -width "XXXXXXXXXXX"
214 sent/received as link-level broadcast
216 sent/received as link-level multicast
220 three link-level specific flags.
230 .Pq also defined in Aq Pa machine/param.h ,
231 which has no additional overhead
232 and is used instead of the internal data area; this is done when at least
234 bytes of data must be stored.
238 flag is raised for an mbuf,
239 the external storage area could be shared among multiple mbufs.
240 Be careful when you attempt to overwrite the data content of the mbuf.
241 .Bl -tag -width compact
242 .It Fn m_get "int nowait" "int type"
243 Allocates an mbuf and initializes it to contain internal data.
246 parameter is a choice of
247 .Dv M_WAIT / M_DONTWAIT
250 means the call cannot fail, but may take forever.
253 parameter is an mbuf type.
254 .It Fn m_getclr "int nowait" "int type"
255 Allocates an mbuf and initializes it to contain internal data, then
259 parameter is a choice of
260 .Dv M_WAIT / M_DONTWAIT
264 parameter is an mbuf type.
265 .It Fn m_gethdr "int nowait" "int type"
266 Allocates an mbuf and initializes it to contain a packet header and internal
270 parameter is a choice of
271 .Dv M_WAIT / M_DONTWAIT
275 parameter is an mbuf type.
276 .It Fn m_devget "char *buf" "int totlen" "int off0" "struct ifnet *ifp" "void (*copy)(const void *, void *, size_t)"
279 bytes from device local memory into mbufs using copy routine
283 is non-zero, the packet is supposed to be trailer-encapsulated and
285 bytes plus the type and length fields will be skipped before copying.
286 Returns the top of the mbuf chain it created.
287 .It Fn m_copym "struct mbuf *m" "int off0" "int len" "int wait"
288 Creates a copy of an mbuf chain starting
290 bytes from the beginning, continuing for
297 the complete mbuf chain will be copied.
300 parameter is a choice of
301 .Dv M_WAIT / M_DONTWAIT
303 .It Fn m_copypacket "struct mbuf *m" "int how"
304 Copies an entire packet, including header (which must be present).
305 This function is an optimization of the common case
306 .Li m_copym(m, 0, Dv M_COPYALL, Fa how ) .
307 .It Fn m_copydata "struct mbuf *m" "int off" "int len" "void *cp"
310 bytes data from mbuf chain
316 bytes from the beginning.
317 .It Fn m_copyback "struct mbuf *m0" "int off" "int len" "void *cp"
320 bytes data from buffer
322 back into the mbuf chain
326 bytes from the beginning of the chain, extending the mbuf chain if necessary.
328 can only fail when extending the chain.
329 The caller should check for this kind of failure
330 by checking the resulting length of the chain in that case.
331 It is an error to use
334 .It Fn m_copyback_cow "struct mbuf *m0" "int off" "int len" "void *cp" \
338 bytes data from buffer
340 back into the mbuf chain
352 automatically allocates new mbufs and adjusts the chain.
353 On success, it returns a pointer to the resulting mbuf chain,
354 and frees the original mbuf
356 Otherwise, it returns
360 parameter is a choice of
361 .Dv M_WAIT / M_DONTWAIT
365 extending the mbuf chain isn't supported.
366 It is an error to attempt to extend the mbuf chain using
368 .It Fn m_makewritable "struct mbuf **mp" "int off" "int len" "int how"
369 Rearranges an mbuf chain so that
374 When it meets read-only mbufs, it allocates new mbufs, adjusts the chain as
376 does, and copies the original content into them.
387 parameter is a choice of
388 .Dv M_WAIT / M_DONTWAIT
391 preserves the contents of the mbuf chain even in the case of failure.
392 It updates a pointer to the mbuf chain pointed to by
394 It returns 0 on success.
395 Otherwise, it returns an error code, typically
397 .It Fn m_cat "struct mbuf *m" "struct mbuf *n"
398 Concatenates mbuf chain
402 Both chains must be of the same type; packet headers will
404 be updated if present.
405 .It Fn m_dup "struct mbuf *m" "int off0" "int len" "int wait"
408 the function creates a copy of an mbuf chain starting
410 bytes from the beginning, continuing for
415 tries to share external storage for mbufs with
419 will deep-copy the whole data content into new mbuf chain
420 and avoids shared external storage.
421 .It Fn m_prepend "struct mbuf *m" "int len" "int how"
428 to prepend to the chain, copying junk along.
431 parameter is a choice of
432 .Dv M_WAIT / M_DONTWAIT
434 .It Fn m_pulldown "struct mbuf *m" "int off" "int len" "int *offp"
435 Rearranges an mbuf chain so that
439 are contiguous and in the data area of an mbuf.
440 The return value points to an mbuf in the middle of the mbuf chain
442 If we call the return value
444 the contiguous data region is available at
445 .Li "mtod(n, void *) + *offp" ,
447 .Li "mtod(n, void *)"
452 The top of the mbuf chain
456 will not be modified.
457 On successful return, it is guaranteed that the mbuf pointed to by
459 does not have a shared external storage,
460 therefore it is safe to update the contiguous region.
463 and frees the mbuf chain on failure.
465 must be smaller or equal than
467 .It Fn m_pullup "struct mbuf *m" "int len"
468 Rearranges an mbuf chain so that
471 and in the data area of an mbuf (so that
473 will work for a structure of size
475 Returns the resulting
476 mbuf chain on success, frees it and returns
479 If there is room, it will add up to
484 contiguous region to possibly avoid being called again.
486 must be smaller or equal than
488 .It Fn m_split "struct mbuf *m0" "int len0" "int wait"
489 Partitions an mbuf chain in two pieces, returning the tail,
490 which is all but the first
493 In case of failure, it returns
496 restore the chain to its original state.
497 .It Fn m_adj "struct mbuf *mp" "int req_len"
500 bytes from head or tail of the (valid) data area.
503 is greater than zero, front bytes are being shaved off, if it's smaller,
504 from the back (and if it is zero, the mbuf will stay bearded).
505 This function does not move data in any way, but is used to manipulate the
506 data area pointer and data length variable of the mbuf in a non-clobbering
508 .It Fn m_apply "struct mbuf *m" "int off" "int len" "int (*f)(void *, void *, unsigned int)" "void *arg"
511 to the data in an mbuf chain starting
513 bytes from the beginning, continuing for
522 will be supplied as first argument for
524 the second argument will be the pointer to the data buffer of a
525 packet (starting after
527 bytes in the stream), and the third argument is the amount
528 of data in bytes in this call.
531 returns something not equal to zero
533 will bail out, returning the return code of
535 Upon successful completion it will return zero.
536 .It Fn m_free "struct mbuf *m"
539 .It Fn m_freem "struct mbuf *m"
540 Frees the mbuf chain beginning with
542 This function contains the elementary sanity check for a
545 .It Fn mtod "struct mbuf *m" "datatype"
546 Returns a pointer to the data contained in the specified mbuf
548 type-casted to the specified data type
550 Implemented as a macro.
551 .It Fn MGET "struct mbuf *m" "int how" "int type"
554 and initializes it to contain internal data.
557 Implemented as a macro.
558 .It Fn MGETHDR "struct mbuf *m" "int how" "int type"
561 and initializes it to contain a packet header.
564 Implemented as a macro.
565 .It Fn MEXTMALLOC "struct mbuf *m" "int len" "int how"
566 Allocates external storage of size
572 parameter is a choice of
573 .Dv M_WAIT / M_DONTWAIT
578 Implemented as a macro.
579 .It Fn MEXTADD "struct mbuf *m" "void *buf" "int size" "int type" "void (*free)(struct mbuf *, void *, size_t, void *)" "void *arg"
580 Adds pre-allocated external storage
590 describe the external storage.
592 is the size of the storage,
598 is a free routine (if not the usual one), and
600 is a possible argument to the free routine.
604 Implemented as a macro.
605 If a free routine is specified, it will be called when the mbuf is freed.
606 In the case of former, the first argument for a free routine is the mbuf
608 and the routine is expected to free it in addition to the external storage
609 pointed by second argument.
610 In the case of latter, the first argument for the routine is NULL.
611 .It Fn MCLGET "struct mbuf *m" "int how"
612 Allocates and adds an mbuf cluster to a normal mbuf
616 parameter is a choice of
617 .Dv M_WAIT / M_DONTWAIT
622 Implemented as a macro.
623 .It Fn M_COPY_PKTHDR "struct mbuf *to" "struct mbuf *from"
624 Copies the mbuf pkthdr from mbuf
629 must have the type flag
634 Implemented as a macro.
635 .It Fn M_MOVE_PKTHDR "struct mbuf *to" "struct mbuf *from"
636 Moves the mbuf pkthdr from mbuf
641 must have the type flag
651 .It Fn M_ALIGN "struct mbuf *m" "int len"
652 Sets the data pointer of a newly allocated mbuf
656 bytes from the end of the mbuf data area, so that
658 bytes of data written to the mbuf
660 starting at the data pointer, will be aligned to the end of the data area.
661 Implemented as a macro.
662 .It Fn MH_ALIGN "struct mbuf *m" "int len"
663 Sets the data pointer of a newly allocated packetheader mbuf
667 bytes from the end of the mbuf data area, so that
669 bytes of data written to the mbuf
671 starting at the data pointer, will be aligned to the end of the data area.
672 Implemented as a macro.
673 .It Fn M_LEADINGSPACE "struct mbuf *m"
674 Returns the amount of space available before the current start of valid
677 Returns 0 if the mbuf data part is shared across multiple mbufs
678 .Pq i.e. not writable .
679 Implemented as a macro.
680 .It Fn M_TRAILINGSPACE "struct mbuf *m"
681 Returns the amount of space available after the current end of valid
684 Returns 0 if the mbuf data part is shared across multiple mbufs
685 .Pq i.e. not writable .
686 Implemented as a macro.
687 .It Fn M_PREPEND "struct mbuf *m" "int plen" "int how"
688 Prepends space of size
692 If a new mbuf must be allocated,
694 specifies whether to wait.
699 and allocation fails, the original mbuf chain is freed and
703 Implemented as a macro.
704 .It Fn MCHTYPE "struct mbuf *m" "int type"
709 Implemented as a macro.
710 .It Fn MFREE "struct mbuf *m" "struct mbuf *n"
713 and places the successor, if any, in mbuf
715 Implemented as a macro.
720 management functions are implemented within the file
721 .Pa sys/kern/uipc_mbuf.c .
722 Function prototypes, and the functions implemented as macros
725 Both pathnames are relative to the root of the
730 .Pa /usr/share/doc/smm/18.net ,
735 .%A Jun-ichiro Hagino
736 .%T "Mbuf issues in 4.4BSD IPv6/IPsec support (experiences from KAME IPv6/IPsec implementation)"
737 .%B "Proceedings of the freenix track: 2000 USENIX annual technical conference"
742 The original mbuf data structures were designed by Rob Gurwitz
743 when he did the initial TCP/IP implementation at BBN.
745 Further extensions and enhancements were made by Bill Joy, Sam Leffler,
746 and Mike Karels at CSRG.
748 Current implementation of external storage by
750 .Aq matt@3am-software.com
753 .Aq thorpej@NetBSD.org .