2000-10-26 23:15:47 +00:00
|
|
|
.\" Copyright (c) 2000 FreeBSD Inc.
|
|
|
|
.\" All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL [your name] OR CONTRIBUTORS BE LIABLE
|
|
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
.\" SUCH DAMAGE.
|
|
|
|
.\"
|
|
|
|
.\" $FreeBSD$
|
|
|
|
.\"
|
2006-07-24 07:32:23 +00:00
|
|
|
.Dd July 24, 2006
|
2000-10-26 23:15:47 +00:00
|
|
|
.Dt MBUF 9
|
|
|
|
.Os
|
|
|
|
.\"
|
|
|
|
.Sh NAME
|
|
|
|
.Nm mbuf
|
|
|
|
.Nd "memory management in the kernel IPC subsystem"
|
|
|
|
.\"
|
|
|
|
.Sh SYNOPSIS
|
2001-10-01 16:09:29 +00:00
|
|
|
.In sys/param.h
|
2001-12-26 23:14:04 +00:00
|
|
|
.In sys/systm.h
|
2001-10-01 16:09:29 +00:00
|
|
|
.In sys/mbuf.h
|
2000-10-26 23:15:47 +00:00
|
|
|
.\"
|
2000-11-01 23:02:19 +00:00
|
|
|
.Ss Mbuf allocation macros
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fn MGET "struct mbuf *mbuf" "int how" "short type"
|
2001-07-14 19:41:16 +00:00
|
|
|
.Fn MGETHDR "struct mbuf *mbuf" "int how" "short type"
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fn MCLGET "struct mbuf *mbuf" "int how"
|
2000-11-01 23:02:19 +00:00
|
|
|
.Fo MEXTADD
|
|
|
|
.Fa "struct mbuf *mbuf"
|
|
|
|
.Fa "caddr_t buf"
|
|
|
|
.Fa "u_int size"
|
|
|
|
.Fa "void (*free)(void *opt_args)"
|
|
|
|
.Fa "void *opt_args"
|
2000-11-21 22:29:31 +00:00
|
|
|
.Fa "short flags"
|
|
|
|
.Fa "int type"
|
2000-11-01 23:02:19 +00:00
|
|
|
.Fc
|
|
|
|
.Fn MEXTFREE "struct mbuf *mbuf"
|
|
|
|
.Fn MFREE "struct mbuf *mbuf" "struct mbuf *successor"
|
|
|
|
.\"
|
|
|
|
.Ss Mbuf utility macros
|
2001-12-26 23:14:04 +00:00
|
|
|
.Fn mtod "struct mbuf *mbuf" "type"
|
2000-11-01 23:02:19 +00:00
|
|
|
.Fn M_ALIGN "struct mbuf *mbuf" "u_int len"
|
|
|
|
.Fn MH_ALIGN "struct mbuf *mbuf" "u_int len"
|
2001-12-26 23:14:04 +00:00
|
|
|
.Ft int
|
2000-11-01 23:02:19 +00:00
|
|
|
.Fn M_LEADINGSPACE "struct mbuf *mbuf"
|
2001-12-26 23:14:04 +00:00
|
|
|
.Ft int
|
2000-11-01 23:02:19 +00:00
|
|
|
.Fn M_TRAILINGSPACE "struct mbuf *mbuf"
|
2003-06-15 14:40:03 +00:00
|
|
|
.Fn M_MOVE_PKTHDR "struct mbuf *to" "struct mbuf *from"
|
2000-11-01 23:02:19 +00:00
|
|
|
.Fn M_PREPEND "struct mbuf *mbuf" "int len" "int how"
|
|
|
|
.Fn MCHTYPE "struct mbuf *mbuf" "u_int type"
|
2001-12-26 23:14:04 +00:00
|
|
|
.Ft int
|
2000-11-21 22:29:31 +00:00
|
|
|
.Fn M_WRITABLE "struct mbuf *mbuf"
|
2000-10-26 23:15:47 +00:00
|
|
|
.\"
|
2000-11-01 23:02:19 +00:00
|
|
|
.Ss Mbuf allocation functions
|
2000-10-26 23:15:47 +00:00
|
|
|
.Ft struct mbuf *
|
|
|
|
.Fn m_get "int how" "int type"
|
|
|
|
.Ft struct mbuf *
|
2001-02-16 03:23:04 +00:00
|
|
|
.Fn m_getm "struct mbuf *orig" "int len" "int how" "int type"
|
|
|
|
.Ft struct mbuf *
|
2004-05-04 14:53:34 +00:00
|
|
|
.Fn m_getcl "int how" "short type" "int flags"
|
|
|
|
.Ft struct mbuf *
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fn m_getclr "int how" "int type"
|
|
|
|
.Ft struct mbuf *
|
|
|
|
.Fn m_gethdr "int how" "int type"
|
2000-11-01 23:02:19 +00:00
|
|
|
.Ft struct mbuf *
|
|
|
|
.Fn m_free "struct mbuf *mbuf"
|
2000-10-26 23:15:47 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn m_freem "struct mbuf *mbuf"
|
2000-11-01 23:02:19 +00:00
|
|
|
.\"
|
|
|
|
.Ss Mbuf utility functions
|
2000-10-26 23:15:47 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn m_adj "struct mbuf *mbuf" "int len"
|
2005-08-08 00:50:25 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn m_align "struct mbuf *mbuf" "int len"
|
2004-12-08 17:47:49 +00:00
|
|
|
.Ft int
|
|
|
|
.Fn m_append "struct mbuf *mbuf" "int len" "c_caddr_t cp"
|
2000-10-26 23:15:47 +00:00
|
|
|
.Ft struct mbuf *
|
|
|
|
.Fn m_prepend "struct mbuf *mbuf" "int len" "int how"
|
|
|
|
.Ft struct mbuf *
|
2005-03-17 19:34:57 +00:00
|
|
|
.Fn m_copyup "struct mbuf *mbuf" "int len" "int dstoff"
|
|
|
|
.Ft struct mbuf *
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fn m_pullup "struct mbuf *mbuf" "int len"
|
|
|
|
.Ft struct mbuf *
|
|
|
|
.Fn m_copym "struct mbuf *mbuf" "int offset" "int len" "int how"
|
|
|
|
.Ft struct mbuf *
|
|
|
|
.Fn m_copypacket "struct mbuf *mbuf" "int how"
|
|
|
|
.Ft struct mbuf *
|
|
|
|
.Fn m_dup "struct mbuf *mbuf" "int how"
|
|
|
|
.Ft void
|
2001-10-04 09:00:38 +00:00
|
|
|
.Fn m_copydata "const struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
|
2000-10-26 23:15:47 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn m_copyback "struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
|
|
|
|
.Ft struct mbuf *
|
|
|
|
.Fo m_devget
|
|
|
|
.Fa "char *buf"
|
|
|
|
.Fa "int len"
|
|
|
|
.Fa "int offset"
|
|
|
|
.Fa "struct ifnet *ifp"
|
|
|
|
.Fa "void (*copy)(char *from, caddr_t to, u_int len)"
|
|
|
|
.Fc
|
|
|
|
.Ft void
|
|
|
|
.Fn m_cat "struct mbuf *m" "struct mbuf *n"
|
2002-11-29 11:36:32 +00:00
|
|
|
.Ft u_int
|
2002-09-18 20:04:30 +00:00
|
|
|
.Fn m_fixhdr "struct mbuf *mbuf"
|
2003-06-15 14:40:03 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn m_dup_pkthdr "struct mbuf *to" "struct mbuf *from"
|
|
|
|
.Ft void
|
|
|
|
.Fn m_move_pkthdr "struct mbuf *to" "struct mbuf *from"
|
2002-11-29 11:36:32 +00:00
|
|
|
.Ft u_int
|
2002-09-18 20:04:30 +00:00
|
|
|
.Fn m_length "struct mbuf *mbuf" "struct mbuf **last"
|
2000-10-26 23:15:47 +00:00
|
|
|
.Ft struct mbuf *
|
|
|
|
.Fn m_split "struct mbuf *mbuf" "int len" "int how"
|
2004-02-11 02:39:24 +00:00
|
|
|
.Ft int
|
|
|
|
.Fn m_apply "struct mbuf *mbuf" "int off" "int len" "int (*f)(void *arg, void *data, u_int len)" "void *arg"
|
|
|
|
.Ft struct mbuf *
|
|
|
|
.Fn m_getptr "struct mbuf *mbuf" "int loc" "int *off"
|
2004-04-01 06:50:21 +00:00
|
|
|
.Ft struct mbuf *
|
|
|
|
.Fn m_defrag "struct mbuf *m0" "int how"
|
2006-03-15 21:11:11 +00:00
|
|
|
.Ft struct mbuf *
|
|
|
|
.Fn m_unshare "struct mbuf *m0" "int how"
|
2000-10-26 23:15:47 +00:00
|
|
|
.\"
|
|
|
|
.Sh DESCRIPTION
|
2003-06-15 14:14:11 +00:00
|
|
|
An
|
|
|
|
.Vt mbuf
|
|
|
|
is a basic unit of memory management in the kernel IPC subsystem.
|
|
|
|
Network packets and socket buffers are stored in
|
|
|
|
.Vt mbufs .
|
|
|
|
A network packet may span multiple
|
|
|
|
.Vt mbufs
|
|
|
|
arranged into a
|
|
|
|
.Vt mbuf chain
|
2001-08-07 15:48:51 +00:00
|
|
|
(linked list),
|
2000-10-26 23:15:47 +00:00
|
|
|
which allows adding or trimming
|
2000-11-01 23:02:19 +00:00
|
|
|
network headers with little overhead.
|
2000-10-26 23:15:47 +00:00
|
|
|
.Pp
|
2003-06-15 14:14:11 +00:00
|
|
|
While a developer should not bother with
|
|
|
|
.Vt mbuf
|
|
|
|
internals without serious
|
2000-10-26 23:15:47 +00:00
|
|
|
reason in order to avoid incompatibilities with future changes, it
|
2003-06-15 14:14:11 +00:00
|
|
|
is useful to understand the general structure of an
|
|
|
|
.Vt mbuf .
|
2000-10-26 23:15:47 +00:00
|
|
|
.Pp
|
2003-06-15 14:14:11 +00:00
|
|
|
An
|
|
|
|
.Vt mbuf
|
|
|
|
consists of a variable-sized header and a small internal
|
2000-11-10 10:08:34 +00:00
|
|
|
buffer for data.
|
2003-06-15 14:14:11 +00:00
|
|
|
The total size of an
|
|
|
|
.Vt mbuf ,
|
2000-11-01 23:02:19 +00:00
|
|
|
.Dv MSIZE ,
|
2004-02-01 22:33:59 +00:00
|
|
|
is a constant defined in
|
|
|
|
.In sys/param.h .
|
2003-06-15 14:14:11 +00:00
|
|
|
The
|
|
|
|
.Vt mbuf
|
|
|
|
header includes:
|
2000-10-26 23:15:47 +00:00
|
|
|
.Pp
|
2004-01-18 23:42:28 +00:00
|
|
|
.Bl -tag -width "m_nextpkt" -offset indent
|
2003-06-15 12:42:41 +00:00
|
|
|
.It Va m_next
|
2004-01-18 23:42:28 +00:00
|
|
|
.Pq Vt struct mbuf *
|
|
|
|
A pointer to the next
|
2003-06-15 14:14:11 +00:00
|
|
|
.Vt mbuf
|
|
|
|
in the
|
2004-01-18 23:42:28 +00:00
|
|
|
.Vt mbuf chain .
|
2003-06-15 12:42:41 +00:00
|
|
|
.It Va m_nextpkt
|
2004-01-18 23:42:28 +00:00
|
|
|
.Pq Vt struct mbuf *
|
|
|
|
A pointer to the next
|
2003-06-15 14:14:11 +00:00
|
|
|
.Vt mbuf chain
|
2004-01-18 23:42:28 +00:00
|
|
|
in the queue.
|
2003-06-15 12:42:41 +00:00
|
|
|
.It Va m_data
|
2004-01-18 23:42:28 +00:00
|
|
|
.Pq Vt caddr_t
|
|
|
|
A pointer to data attached to this
|
|
|
|
.Vt mbuf .
|
2003-06-15 12:42:41 +00:00
|
|
|
.It Va m_len
|
2004-01-18 23:42:28 +00:00
|
|
|
.Pq Vt int
|
|
|
|
The length of the data.
|
2003-06-15 12:42:41 +00:00
|
|
|
.It Va m_type
|
2004-01-18 23:42:28 +00:00
|
|
|
.Pq Vt short
|
|
|
|
The type of the data.
|
2003-06-15 12:42:41 +00:00
|
|
|
.It Va m_flags
|
2004-01-18 23:42:28 +00:00
|
|
|
.Pq Vt int
|
|
|
|
The
|
2003-06-15 14:14:11 +00:00
|
|
|
.Vt mbuf
|
2004-01-18 23:42:28 +00:00
|
|
|
flags.
|
2000-10-26 23:15:47 +00:00
|
|
|
.El
|
|
|
|
.Pp
|
2003-06-15 14:14:11 +00:00
|
|
|
The
|
|
|
|
.Vt mbuf
|
|
|
|
flag bits are defined as follows:
|
2000-10-26 23:15:47 +00:00
|
|
|
.Bd -literal
|
|
|
|
/* mbuf flags */
|
|
|
|
#define M_EXT 0x0001 /* has associated external storage */
|
|
|
|
#define M_PKTHDR 0x0002 /* start of record */
|
|
|
|
#define M_EOR 0x0004 /* end of record */
|
2000-11-21 22:29:31 +00:00
|
|
|
#define M_RDONLY 0x0008 /* associated data marked read-only */
|
|
|
|
#define M_PROTO1 0x0010 /* protocol-specific */
|
|
|
|
#define M_PROTO2 0x0020 /* protocol-specific */
|
|
|
|
#define M_PROTO3 0x0040 /* protocol-specific */
|
|
|
|
#define M_PROTO4 0x0080 /* protocol-specific */
|
|
|
|
#define M_PROTO5 0x0100 /* protocol-specific */
|
2004-01-18 21:23:30 +00:00
|
|
|
#define M_PROTO6 0x4000 /* protocol-specific (avoid M_BCAST conflict) */
|
|
|
|
#define M_FREELIST 0x8000 /* mbuf is on the free list */
|
2000-10-26 23:15:47 +00:00
|
|
|
|
2004-01-18 21:23:30 +00:00
|
|
|
/* mbuf pkthdr flags (also stored in m_flags) */
|
2000-11-21 22:29:31 +00:00
|
|
|
#define M_BCAST 0x0200 /* send/received as link-level broadcast */
|
|
|
|
#define M_MCAST 0x0400 /* send/received as link-level multicast */
|
|
|
|
#define M_FRAG 0x0800 /* packet is fragment of larger packet */
|
|
|
|
#define M_FIRSTFRAG 0x1000 /* packet is first fragment */
|
|
|
|
#define M_LASTFRAG 0x2000 /* packet is last fragment */
|
2000-10-26 23:15:47 +00:00
|
|
|
.Ed
|
|
|
|
.Pp
|
2003-06-15 14:14:11 +00:00
|
|
|
The available
|
|
|
|
.Vt mbuf
|
|
|
|
types are defined as follows:
|
2000-10-26 23:15:47 +00:00
|
|
|
.Bd -literal
|
|
|
|
/* mbuf types */
|
|
|
|
#define MT_DATA 1 /* dynamic (data) allocation */
|
2006-07-24 07:32:23 +00:00
|
|
|
#define MT_HEADER MT_DATA /* packet header */
|
2000-10-26 23:15:47 +00:00
|
|
|
#define MT_SONAME 8 /* socket name */
|
|
|
|
#define MT_CONTROL 14 /* extra-data protocol message */
|
2004-04-01 07:54:27 +00:00
|
|
|
#define MT_OOBDATA 15 /* expedited data */
|
2000-10-26 23:15:47 +00:00
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
If the
|
|
|
|
.Dv M_PKTHDR
|
|
|
|
flag is set, a
|
2003-06-15 14:14:11 +00:00
|
|
|
.Vt struct pkthdr Va m_pkthdr
|
|
|
|
is added to the
|
|
|
|
.Vt mbuf
|
|
|
|
header.
|
2000-11-10 10:08:34 +00:00
|
|
|
It contains a pointer to the interface
|
2000-10-26 23:15:47 +00:00
|
|
|
the packet has been received from
|
2003-06-15 14:14:11 +00:00
|
|
|
.Pq Vt struct ifnet Va *rcvif ,
|
2000-10-26 23:15:47 +00:00
|
|
|
and the total packet length
|
2003-06-15 14:14:11 +00:00
|
|
|
.Pq Vt int Va len .
|
2004-05-20 09:34:19 +00:00
|
|
|
Optionally, it may also contain an attached list of packet tags
|
|
|
|
.Pq Vt "struct m_tag" .
|
|
|
|
See
|
|
|
|
.Xr mbuf_tags 9
|
|
|
|
for details.
|
2004-01-21 12:45:37 +00:00
|
|
|
Fields used in offloading checksum calculation to the hardware are kept in
|
|
|
|
.Va m_pkthdr
|
|
|
|
as well.
|
|
|
|
See
|
|
|
|
.Sx HARDWARE-ASSISTED CHECKSUM CALCULATION
|
|
|
|
for details.
|
2000-10-26 23:15:47 +00:00
|
|
|
.Pp
|
2003-06-15 14:14:11 +00:00
|
|
|
If small enough, data is stored in the internal data buffer of an
|
|
|
|
.Vt mbuf .
|
|
|
|
If the data is sufficiently large, another
|
|
|
|
.Vt mbuf
|
|
|
|
may be added to the
|
|
|
|
.Vt mbuf chain ,
|
|
|
|
or external storage may be associated with the
|
|
|
|
.Vt mbuf .
|
2000-10-26 23:15:47 +00:00
|
|
|
.Dv MHLEN
|
2003-06-15 14:14:11 +00:00
|
|
|
bytes of data can fit into an
|
|
|
|
.Vt mbuf
|
|
|
|
with the
|
2000-10-26 23:15:47 +00:00
|
|
|
.Dv M_PKTHDR
|
|
|
|
flag set,
|
|
|
|
.Dv MLEN
|
|
|
|
bytes can otherwise.
|
|
|
|
.Pp
|
2003-06-15 14:14:11 +00:00
|
|
|
If external storage is being associated with an
|
|
|
|
.Vt mbuf ,
|
|
|
|
the
|
2003-06-15 12:42:41 +00:00
|
|
|
.Va m_ext
|
2000-11-10 10:22:49 +00:00
|
|
|
header is added at the cost of losing the internal data buffer.
|
2000-11-10 10:08:34 +00:00
|
|
|
It includes a pointer to external storage, the size of the storage,
|
|
|
|
a pointer to a function used for freeing the storage,
|
|
|
|
a pointer to an optional argument that can be passed to the function,
|
|
|
|
and a pointer to a reference counter.
|
2003-06-15 14:14:11 +00:00
|
|
|
An
|
|
|
|
.Vt mbuf
|
|
|
|
using external storage has the
|
2000-10-26 23:15:47 +00:00
|
|
|
.Dv M_EXT
|
|
|
|
flag set.
|
|
|
|
.Pp
|
2000-11-01 23:02:19 +00:00
|
|
|
The system supplies a macro for allocating the desired external storage
|
|
|
|
buffer,
|
|
|
|
.Dv MEXTADD .
|
|
|
|
.Pp
|
|
|
|
The allocation and management of the reference counter is handled by the
|
2000-11-10 10:08:34 +00:00
|
|
|
subsystem.
|
2000-11-01 23:02:19 +00:00
|
|
|
.Pp
|
|
|
|
The system also supplies a default type of external storage buffer called an
|
2003-06-15 14:14:11 +00:00
|
|
|
.Vt mbuf cluster .
|
|
|
|
.Vt Mbuf clusters
|
|
|
|
can be allocated and configured with the use of the
|
2000-11-01 23:02:19 +00:00
|
|
|
.Dv MCLGET
|
2000-11-10 10:08:34 +00:00
|
|
|
macro.
|
2003-06-15 14:14:11 +00:00
|
|
|
Each
|
|
|
|
.Vt mbuf cluster
|
|
|
|
is
|
2000-10-26 23:15:47 +00:00
|
|
|
.Dv MCLBYTES
|
2000-11-01 23:02:19 +00:00
|
|
|
in size, where MCLBYTES is a machine-dependent constant.
|
|
|
|
The system defines an advisory macro
|
2000-10-26 23:15:47 +00:00
|
|
|
.Dv MINCLSIZE ,
|
2003-06-15 14:14:11 +00:00
|
|
|
which is the smallest amount of data to put into an
|
|
|
|
.Vt mbuf cluster .
|
2005-02-13 23:45:54 +00:00
|
|
|
It is equal to the sum of
|
2000-10-26 23:15:47 +00:00
|
|
|
.Dv MLEN
|
|
|
|
and
|
|
|
|
.Dv MHLEN .
|
2003-06-15 14:14:11 +00:00
|
|
|
It is typically preferable to store data into the data region of an
|
|
|
|
.Vt mbuf ,
|
|
|
|
if size permits, as opposed to allocating a separate
|
|
|
|
.Vt mbuf cluster
|
|
|
|
to hold the same data.
|
2000-10-26 23:15:47 +00:00
|
|
|
.\"
|
|
|
|
.Ss Macros and Functions
|
2001-07-14 19:41:16 +00:00
|
|
|
There are numerous predefined macros and functions that provide the
|
2000-11-01 23:02:19 +00:00
|
|
|
developer with common utilities.
|
2000-10-26 23:15:47 +00:00
|
|
|
.\"
|
|
|
|
.Bl -ohang -offset indent
|
|
|
|
.It Fn mtod mbuf type
|
2003-06-15 14:14:11 +00:00
|
|
|
Convert an
|
|
|
|
.Fa mbuf
|
|
|
|
pointer to a data pointer.
|
|
|
|
The macro expands to the data pointer cast to the pointer of the specified
|
|
|
|
.Fa type .
|
2000-10-26 23:15:47 +00:00
|
|
|
.Sy Note :
|
2003-06-15 14:14:11 +00:00
|
|
|
It is advisable to ensure that there is enough contiguous data in
|
|
|
|
.Fa mbuf .
|
2000-10-26 23:15:47 +00:00
|
|
|
See
|
|
|
|
.Fn m_pullup
|
|
|
|
for details.
|
|
|
|
.It Fn MGET mbuf how type
|
2003-06-15 14:14:11 +00:00
|
|
|
Allocate an
|
|
|
|
.Vt mbuf
|
|
|
|
and initialize it to contain internal data.
|
2000-11-10 10:22:49 +00:00
|
|
|
.Fa mbuf
|
2003-06-15 14:14:11 +00:00
|
|
|
will point to the allocated
|
|
|
|
.Vt mbuf
|
|
|
|
on success, or be set to
|
2000-10-26 23:15:47 +00:00
|
|
|
.Dv NULL
|
2000-11-10 10:08:34 +00:00
|
|
|
on failure.
|
|
|
|
The
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fa how
|
|
|
|
argument is to be set to
|
2003-02-24 05:59:12 +00:00
|
|
|
.Dv M_TRYWAIT
|
2000-10-26 23:15:47 +00:00
|
|
|
or
|
2003-02-24 05:59:12 +00:00
|
|
|
.Dv M_DONTWAIT .
|
2000-11-10 10:22:49 +00:00
|
|
|
It specifies whether the caller is willing to block if necessary.
|
2001-07-14 19:41:16 +00:00
|
|
|
If
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fa how
|
2000-11-10 10:22:49 +00:00
|
|
|
is set to
|
2003-02-24 05:59:12 +00:00
|
|
|
.Dv M_TRYWAIT ,
|
2000-11-10 10:22:49 +00:00
|
|
|
a failed allocation will result in the caller being put
|
2000-11-01 23:02:19 +00:00
|
|
|
to sleep for a designated
|
2000-11-10 10:22:49 +00:00
|
|
|
kern.ipc.mbuf_wait
|
2001-08-07 15:48:51 +00:00
|
|
|
.Xr ( sysctl 8
|
|
|
|
tunable)
|
2000-11-10 10:22:49 +00:00
|
|
|
number of ticks.
|
2003-06-15 14:14:11 +00:00
|
|
|
A number of other functions and macros related to
|
|
|
|
.Vt mbufs
|
|
|
|
have the same argument because they may
|
|
|
|
at some point need to allocate new
|
|
|
|
.Vt mbufs .
|
2003-02-24 05:59:12 +00:00
|
|
|
.Pp
|
2003-06-15 14:14:11 +00:00
|
|
|
Programmers should be careful not to confuse the
|
|
|
|
.Vt mbuf
|
|
|
|
allocation flag
|
2003-02-24 05:59:12 +00:00
|
|
|
.Dv M_DONTWAIT
|
|
|
|
with the
|
|
|
|
.Xr malloc 9
|
|
|
|
allocation flag,
|
|
|
|
.Dv M_NOWAIT .
|
|
|
|
They are not the same.
|
2000-10-26 23:15:47 +00:00
|
|
|
.It Fn MGETHDR mbuf how type
|
2003-06-15 14:14:11 +00:00
|
|
|
Allocate an
|
|
|
|
.Vt mbuf
|
|
|
|
and initialize it to contain a packet header
|
2000-11-10 10:08:34 +00:00
|
|
|
and internal data.
|
|
|
|
See
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fn MGET
|
|
|
|
for details.
|
|
|
|
.It Fn MCLGET mbuf how
|
2003-06-15 14:14:11 +00:00
|
|
|
Allocate and attach an
|
|
|
|
.Vt mbuf cluster
|
|
|
|
to
|
|
|
|
.Fa mbuf .
|
2000-11-10 10:08:34 +00:00
|
|
|
If the macro fails, the
|
2000-10-26 23:15:47 +00:00
|
|
|
.Dv M_EXT
|
2005-02-13 22:25:33 +00:00
|
|
|
flag will not be set in
|
2003-06-15 14:14:11 +00:00
|
|
|
.Fa mbuf .
|
2003-06-15 14:40:03 +00:00
|
|
|
.It Fn M_ALIGN mbuf len
|
|
|
|
Set the pointer
|
|
|
|
.Fa mbuf->m_data
|
|
|
|
to place an object of the size
|
|
|
|
.Fa len
|
|
|
|
at the end of the internal data area of
|
|
|
|
.Fa mbuf ,
|
|
|
|
long word aligned.
|
|
|
|
Applicable only if
|
|
|
|
.Fa mbuf
|
|
|
|
is newly allocated with
|
|
|
|
.Fn MGET
|
|
|
|
or
|
|
|
|
.Fn m_get .
|
|
|
|
.It Fn MH_ALIGN mbuf len
|
|
|
|
Serves the same purpose as
|
|
|
|
.Fn M_ALIGN
|
|
|
|
does, but only for
|
|
|
|
.Fa mbuf
|
|
|
|
newly allocated with
|
|
|
|
.Fn MGETHDR
|
|
|
|
or
|
|
|
|
.Fn m_gethdr ,
|
|
|
|
or initialized by
|
|
|
|
.Fn m_dup_pkthdr
|
|
|
|
or
|
|
|
|
.Fn m_move_pkthdr .
|
2005-08-08 00:50:25 +00:00
|
|
|
.It Fn m_align mbuf len
|
|
|
|
Services the same purpose as
|
|
|
|
.Fn M_ALIGN
|
|
|
|
but handles any type of mbuf.
|
2003-06-15 14:40:03 +00:00
|
|
|
.It Fn M_LEADINGSPACE mbuf
|
|
|
|
Returns the number of bytes available before the beginning
|
|
|
|
of data in
|
|
|
|
.Fa mbuf .
|
|
|
|
.It Fn M_TRAILINGSPACE mbuf
|
|
|
|
Returns the number of bytes available after the end of data in
|
|
|
|
.Fa mbuf .
|
2000-10-26 23:15:47 +00:00
|
|
|
.It Fn M_PREPEND mbuf len how
|
2003-06-15 14:14:11 +00:00
|
|
|
This macro operates on an
|
|
|
|
.Vt mbuf chain .
|
2000-10-26 23:15:47 +00:00
|
|
|
It is an optimized wrapper for
|
|
|
|
.Fn m_prepend
|
|
|
|
that can make use of possible empty space before data
|
2004-07-03 18:29:24 +00:00
|
|
|
(e.g.\& left after trimming of a link-layer header).
|
2003-06-15 14:14:11 +00:00
|
|
|
The new
|
|
|
|
.Vt mbuf chain
|
|
|
|
pointer or
|
2000-10-26 23:15:47 +00:00
|
|
|
.Dv NULL
|
2001-07-14 19:41:16 +00:00
|
|
|
is in
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fa mbuf
|
|
|
|
after the call.
|
2003-06-15 14:40:03 +00:00
|
|
|
.It Fn M_MOVE_PKTHDR to from
|
|
|
|
Using this macro is equivalent to calling
|
|
|
|
.Fn m_move_pkthdr to from .
|
2000-11-21 22:29:31 +00:00
|
|
|
.It Fn M_WRITABLE mbuf
|
2003-06-15 14:14:11 +00:00
|
|
|
This macro will evaluate true if
|
|
|
|
.Fa mbuf
|
|
|
|
is not marked
|
2000-11-21 22:29:31 +00:00
|
|
|
.Dv M_RDONLY
|
2003-06-15 14:14:11 +00:00
|
|
|
and if either
|
|
|
|
.Fa mbuf
|
|
|
|
does not contain external storage or,
|
2000-11-21 22:29:31 +00:00
|
|
|
if it does,
|
2001-07-14 19:41:16 +00:00
|
|
|
then if the reference count of the storage is not greater than 1.
|
2000-11-21 22:29:31 +00:00
|
|
|
The
|
|
|
|
.Dv M_RDONLY
|
2003-06-15 14:14:11 +00:00
|
|
|
flag can be set in
|
|
|
|
.Fa mbuf->m_flags .
|
2000-11-21 22:29:31 +00:00
|
|
|
This can be achieved during setup of the external storage,
|
|
|
|
by passing the
|
|
|
|
.Dv M_RDONLY
|
|
|
|
bit as a
|
2001-02-16 09:05:48 +00:00
|
|
|
.Fa flags
|
2000-11-21 22:29:31 +00:00
|
|
|
argument to the
|
|
|
|
.Fn MEXTADD
|
2003-06-15 14:14:11 +00:00
|
|
|
macro, or can be directly set in individual
|
|
|
|
.Vt mbufs .
|
2003-06-15 14:40:03 +00:00
|
|
|
.It Fn MCHTYPE mbuf type
|
|
|
|
Change the type of
|
|
|
|
.Fa mbuf
|
|
|
|
to
|
|
|
|
.Fa type .
|
|
|
|
This is a relatively expensive operation and should be avoided.
|
2000-10-26 23:15:47 +00:00
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
The functions are:
|
2001-07-14 19:41:16 +00:00
|
|
|
.Bl -ohang -offset indent
|
2000-10-26 23:15:47 +00:00
|
|
|
.It Fn m_get how type
|
|
|
|
A function version of
|
2000-11-01 23:02:19 +00:00
|
|
|
.Fn MGET
|
|
|
|
for non-critical paths.
|
2001-02-16 03:23:04 +00:00
|
|
|
.It Fn m_getm orig len how type
|
|
|
|
Allocate
|
2001-02-16 09:05:48 +00:00
|
|
|
.Fa len
|
2003-06-15 14:14:11 +00:00
|
|
|
bytes worth of
|
|
|
|
.Vt mbufs
|
|
|
|
and
|
|
|
|
.Vt mbuf clusters
|
|
|
|
if necessary and append the resulting allocated
|
|
|
|
.Vt mbuf chain
|
|
|
|
to the
|
|
|
|
.Vt mbuf chain
|
|
|
|
.Fa orig ,
|
|
|
|
if it is
|
2001-02-16 09:05:48 +00:00
|
|
|
.No non- Ns Dv NULL .
|
2001-02-16 03:23:04 +00:00
|
|
|
If the allocation fails at any point,
|
2001-02-16 09:05:48 +00:00
|
|
|
free whatever was allocated and return
|
|
|
|
.Dv NULL .
|
2001-02-16 03:23:04 +00:00
|
|
|
If
|
2001-02-16 09:05:48 +00:00
|
|
|
.Fa orig
|
|
|
|
is
|
|
|
|
.No non- Ns Dv NULL ,
|
2001-02-16 03:23:04 +00:00
|
|
|
it will not be freed.
|
|
|
|
It is possible to use
|
|
|
|
.Fn m_getm
|
|
|
|
to either append
|
2001-02-16 09:05:48 +00:00
|
|
|
.Fa len
|
2003-06-15 14:14:11 +00:00
|
|
|
bytes to an existing
|
|
|
|
.Vt mbuf
|
|
|
|
or
|
|
|
|
.Vt mbuf chain
|
2001-02-16 03:23:04 +00:00
|
|
|
(for example, one which may be sitting in a pre-allocated ring)
|
2003-06-15 14:14:11 +00:00
|
|
|
or to simply perform an all-or-nothing
|
|
|
|
.Vt mbuf
|
|
|
|
and
|
|
|
|
.Vt mbuf cluster
|
|
|
|
allocation.
|
2000-10-26 23:15:47 +00:00
|
|
|
.It Fn m_gethdr how type
|
|
|
|
A function version of
|
2000-11-01 23:02:19 +00:00
|
|
|
.Fn MGETHDR
|
|
|
|
for non-critical paths.
|
2004-05-04 14:53:34 +00:00
|
|
|
.It Fn m_getcl how type flags
|
|
|
|
Fetch an
|
|
|
|
.Vt mbuf
|
|
|
|
with a
|
|
|
|
.Vt mbuf cluster
|
|
|
|
attached to it.
|
|
|
|
If one of the allocations fails, the entire allocation fails.
|
|
|
|
This routine is the preferred way of fetching both the
|
|
|
|
.Vt mbuf
|
|
|
|
and
|
|
|
|
.Vt mbuf cluster
|
|
|
|
together, as it avoids having to unlock/relock between allocations.
|
|
|
|
Returns
|
|
|
|
.Dv NULL
|
|
|
|
on failure.
|
2000-10-26 23:15:47 +00:00
|
|
|
.It Fn m_getclr how type
|
2003-06-15 14:14:11 +00:00
|
|
|
Allocate an
|
|
|
|
.Vt mbuf
|
|
|
|
and zero out the data region.
|
2004-04-28 14:53:49 +00:00
|
|
|
.It Fn m_free mbuf
|
|
|
|
Frees
|
|
|
|
.Vt mbuf .
|
2004-08-28 03:46:18 +00:00
|
|
|
Returns
|
|
|
|
.Va m_next
|
|
|
|
of the freed
|
|
|
|
.Vt mbuf .
|
2000-10-26 23:15:47 +00:00
|
|
|
.El
|
|
|
|
.Pp
|
2003-06-15 14:14:11 +00:00
|
|
|
The functions below operate on
|
|
|
|
.Vt mbuf chains .
|
2000-10-26 23:15:47 +00:00
|
|
|
.Bl -ohang -offset indent
|
|
|
|
.It Fn m_freem mbuf
|
2003-06-15 14:14:11 +00:00
|
|
|
Free an entire
|
|
|
|
.Vt mbuf chain ,
|
|
|
|
including any external storage.
|
2000-10-26 23:15:47 +00:00
|
|
|
.\"
|
|
|
|
.It Fn m_adj mbuf len
|
|
|
|
Trim
|
|
|
|
.Fa len
|
2003-06-15 14:14:11 +00:00
|
|
|
bytes from the head of an
|
|
|
|
.Vt mbuf chain
|
|
|
|
if
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fa len
|
|
|
|
is positive, from the tail otherwise.
|
|
|
|
.\"
|
2004-12-08 17:47:49 +00:00
|
|
|
.It Fn m_append mbuf len cp
|
|
|
|
Append
|
|
|
|
.Vt len
|
|
|
|
bytes of data
|
|
|
|
.Vt cp
|
|
|
|
to the
|
|
|
|
.Vt mbuf chain .
|
|
|
|
Extend the mbuf chain if the new data does not fit in
|
|
|
|
existing space.
|
|
|
|
.\"
|
2000-10-26 23:15:47 +00:00
|
|
|
.It Fn m_prepend mbuf len how
|
2003-06-15 14:14:11 +00:00
|
|
|
Allocate a new
|
|
|
|
.Vt mbuf
|
|
|
|
and prepend it to the
|
|
|
|
.Vt mbuf chain ,
|
|
|
|
handle
|
2000-10-26 23:15:47 +00:00
|
|
|
.Dv M_PKTHDR
|
|
|
|
properly.
|
|
|
|
.Sy Note :
|
2005-02-13 22:25:33 +00:00
|
|
|
It does not allocate any
|
2003-06-15 14:14:11 +00:00
|
|
|
.Vt mbuf clusters ,
|
|
|
|
so
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fa len
|
|
|
|
must be less than
|
|
|
|
.Dv MLEN
|
|
|
|
or
|
|
|
|
.Dv MHLEN ,
|
|
|
|
depending on the
|
2001-02-01 16:38:02 +00:00
|
|
|
.Dv M_PKTHDR
|
|
|
|
flag setting.
|
2000-10-26 23:15:47 +00:00
|
|
|
.\"
|
2005-03-17 19:34:57 +00:00
|
|
|
.It Fn m_copyup mbuf len dstoff
|
|
|
|
Similar to
|
|
|
|
.Fn m_pullup
|
|
|
|
but copies
|
|
|
|
.Fa len
|
|
|
|
bytes of data into a new mbuf at
|
|
|
|
.Fa dstoff
|
|
|
|
bytes into the mbuf.
|
|
|
|
The
|
|
|
|
.Fa dstoff
|
|
|
|
argument aligns the data and leaves room for a link layer header.
|
2005-06-15 13:31:23 +00:00
|
|
|
Returns the new
|
2005-03-17 19:34:57 +00:00
|
|
|
.Vt mbuf chain
|
|
|
|
on success,
|
|
|
|
and frees the
|
|
|
|
.Vt mbuf chain
|
|
|
|
and returns
|
|
|
|
.Dv NULL
|
|
|
|
on failure.
|
|
|
|
.Sy Note :
|
|
|
|
The function does not allocate
|
|
|
|
.Vt mbuf clusters ,
|
|
|
|
so
|
|
|
|
.Fa len + dstoff
|
|
|
|
must be less than
|
|
|
|
.Dv MHLEN .
|
|
|
|
.\"
|
2000-10-26 23:15:47 +00:00
|
|
|
.It Fn m_pullup mbuf len
|
|
|
|
Arrange that the first
|
|
|
|
.Fa len
|
2003-06-15 14:14:11 +00:00
|
|
|
bytes of an
|
|
|
|
.Vt mbuf chain
|
|
|
|
are contiguous and lay in the data area of
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fa mbuf ,
|
|
|
|
so they are accessible with
|
|
|
|
.Fn mtod mbuf type .
|
2006-12-08 23:44:30 +00:00
|
|
|
It is important to remember that this may involve
|
|
|
|
reallocating some mbufs and moving data so all pointers
|
|
|
|
referencing data within the old mbuf chain
|
|
|
|
must be recalculated or made invalid.
|
2003-06-15 14:14:11 +00:00
|
|
|
Return the new
|
|
|
|
.Vt mbuf chain
|
|
|
|
on success,
|
2000-10-26 23:15:47 +00:00
|
|
|
.Dv NULL
|
|
|
|
on failure
|
2003-06-15 14:14:11 +00:00
|
|
|
(the
|
|
|
|
.Vt mbuf chain
|
|
|
|
is freed in this case).
|
2000-10-26 23:15:47 +00:00
|
|
|
.Sy Note :
|
2005-02-13 22:25:33 +00:00
|
|
|
It does not allocate any
|
2003-06-15 14:14:11 +00:00
|
|
|
.Vt mbuf clusters ,
|
|
|
|
so
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fa len
|
|
|
|
must be less than
|
|
|
|
.Dv MHLEN .
|
|
|
|
.\"
|
|
|
|
.It Fn m_copym mbuf offset len how
|
2003-06-15 14:14:11 +00:00
|
|
|
Make a copy of an
|
|
|
|
.Vt mbuf chain
|
|
|
|
starting
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fa offset
|
2001-07-14 19:41:16 +00:00
|
|
|
bytes from the beginning, continuing for
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fa len
|
2000-11-10 10:08:34 +00:00
|
|
|
bytes.
|
|
|
|
If
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fa len
|
|
|
|
is
|
|
|
|
.Dv M_COPYALL ,
|
2003-06-15 14:14:11 +00:00
|
|
|
copy to the end of the
|
|
|
|
.Vt mbuf chain .
|
2000-10-26 23:15:47 +00:00
|
|
|
.Sy Note :
|
2003-06-15 14:14:11 +00:00
|
|
|
The copy is read-only, because the
|
|
|
|
.Vt mbuf clusters
|
|
|
|
are not copied, only their reference counts are incremented.
|
2000-10-26 23:15:47 +00:00
|
|
|
.\"
|
|
|
|
.It Fn m_copypacket mbuf how
|
|
|
|
Copy an entire packet including header, which must be present.
|
|
|
|
This is an optimized version of the common case
|
|
|
|
.Fn m_copym mbuf 0 M_COPYALL how .
|
|
|
|
.Sy Note :
|
2003-06-15 14:14:11 +00:00
|
|
|
the copy is read-only, because the
|
|
|
|
.Vt mbuf clusters
|
|
|
|
are not copied, only their reference counts are incremented.
|
2000-10-26 23:15:47 +00:00
|
|
|
.\"
|
|
|
|
.It Fn m_dup mbuf how
|
2003-06-15 14:14:11 +00:00
|
|
|
Copy a packet header
|
|
|
|
.Vt mbuf chain
|
|
|
|
into a completely new
|
|
|
|
.Vt mbuf chain ,
|
|
|
|
including copying any
|
|
|
|
.Vt mbuf clusters .
|
2000-11-10 10:08:34 +00:00
|
|
|
Use this instead of
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fn m_copypacket
|
2003-06-15 14:14:11 +00:00
|
|
|
when you need a writable copy of an
|
|
|
|
.Vt mbuf chain .
|
2000-10-26 23:15:47 +00:00
|
|
|
.\"
|
|
|
|
.It Fn m_copydata mbuf offset len buf
|
2003-06-15 14:14:11 +00:00
|
|
|
Copy data from an
|
|
|
|
.Vt mbuf chain
|
|
|
|
starting
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fa off
|
|
|
|
bytes from the beginning, continuing for
|
|
|
|
.Fa len
|
2001-07-14 19:41:16 +00:00
|
|
|
bytes, into the indicated buffer
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fa buf .
|
|
|
|
.\"
|
|
|
|
.It Fn m_copyback mbuf offset len buf
|
|
|
|
Copy
|
|
|
|
.Fa len
|
|
|
|
bytes from the buffer
|
|
|
|
.Fa buf
|
2003-06-15 14:14:11 +00:00
|
|
|
back into the indicated
|
|
|
|
.Vt mbuf chain ,
|
2000-10-26 23:15:47 +00:00
|
|
|
starting at
|
|
|
|
.Fa offset
|
2003-06-15 14:14:11 +00:00
|
|
|
bytes from the beginning of the
|
|
|
|
.Vt mbuf chain ,
|
|
|
|
extending the
|
|
|
|
.Vt mbuf chain
|
|
|
|
if necessary.
|
2000-10-26 23:15:47 +00:00
|
|
|
.Sy Note :
|
2005-02-13 22:25:33 +00:00
|
|
|
It does not allocate any
|
2003-06-15 14:14:11 +00:00
|
|
|
.Vt mbuf clusters ,
|
|
|
|
just adds
|
|
|
|
.Vt mbufs
|
|
|
|
to the
|
|
|
|
.Vt mbuf chain .
|
2005-02-13 23:45:54 +00:00
|
|
|
It is safe to set
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fa offset
|
2003-06-15 14:14:11 +00:00
|
|
|
beyond the current
|
|
|
|
.Vt mbuf chain
|
|
|
|
end: zeroed
|
|
|
|
.Vt mbufs
|
|
|
|
will be allocated to fill the space.
|
2000-10-26 23:15:47 +00:00
|
|
|
.\"
|
2003-06-15 14:47:12 +00:00
|
|
|
.It Fn m_length mbuf last
|
2003-06-15 14:14:11 +00:00
|
|
|
Return the length of the
|
|
|
|
.Vt mbuf chain ,
|
|
|
|
and optionally a pointer to the last
|
|
|
|
.Vt mbuf .
|
2002-09-18 20:04:30 +00:00
|
|
|
.\"
|
2003-06-15 14:40:03 +00:00
|
|
|
.It Fn m_dup_pkthdr to from how
|
|
|
|
Upon the function's completion, the
|
|
|
|
.Vt mbuf
|
|
|
|
.Fa to
|
|
|
|
will contain an identical copy of
|
|
|
|
.Fa from->m_pkthdr
|
|
|
|
and the per-packet attributes found in the
|
|
|
|
.Vt mbuf chain
|
|
|
|
.Fa from .
|
|
|
|
The
|
|
|
|
.Vt mbuf
|
|
|
|
.Fa from
|
|
|
|
must have the flag
|
|
|
|
.Dv M_PKTHDR
|
|
|
|
initially set, and
|
|
|
|
.Fa to
|
|
|
|
must be empty on entry.
|
|
|
|
.\"
|
|
|
|
.It Fn m_move_pkthdr to from
|
|
|
|
Move
|
|
|
|
.Va m_pkthdr
|
|
|
|
and the per-packet attributes from the
|
|
|
|
.Vt mbuf chain
|
|
|
|
.Fa from
|
|
|
|
to the
|
|
|
|
.Vt mbuf
|
|
|
|
.Fa to .
|
|
|
|
The
|
|
|
|
.Vt mbuf
|
|
|
|
.Fa from
|
|
|
|
must have the flag
|
|
|
|
.Dv M_PKTHDR
|
|
|
|
initially set, and
|
|
|
|
.Fa to
|
|
|
|
must be empty on entry.
|
|
|
|
Upon the function's completion,
|
|
|
|
.Fa from
|
|
|
|
will have the flag
|
|
|
|
.Dv M_PKTHDR
|
|
|
|
and the per-packet attributes cleared.
|
|
|
|
.\"
|
2003-06-15 14:47:12 +00:00
|
|
|
.It Fn m_fixhdr mbuf
|
2003-06-15 14:14:11 +00:00
|
|
|
Set the packet-header length to the length of the
|
|
|
|
.Vt mbuf chain .
|
2002-09-18 20:04:30 +00:00
|
|
|
.\"
|
2000-10-26 23:15:47 +00:00
|
|
|
.It Fn m_devget buf len offset ifp copy
|
|
|
|
Copy data from a device local memory pointed to by
|
|
|
|
.Fa buf
|
2003-06-15 14:14:11 +00:00
|
|
|
to an
|
|
|
|
.Vt mbuf chain .
|
2000-11-10 10:08:34 +00:00
|
|
|
The copy is done using a specified copy routine
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fa copy ,
|
|
|
|
or
|
|
|
|
.Fn bcopy
|
|
|
|
if
|
|
|
|
.Fa copy
|
|
|
|
is
|
|
|
|
.Dv NULL .
|
|
|
|
.\"
|
|
|
|
.It Fn m_cat m n
|
|
|
|
Concatenate
|
|
|
|
.Fa n
|
|
|
|
to
|
|
|
|
.Fa m .
|
2003-06-15 14:14:11 +00:00
|
|
|
Both
|
|
|
|
.Vt mbuf chains
|
|
|
|
must be of the same type.
|
2000-10-26 23:15:47 +00:00
|
|
|
.Fa N
|
|
|
|
is still valid after the function returned.
|
|
|
|
.Sy Note :
|
|
|
|
It does not handle
|
|
|
|
.Dv M_PKTHDR
|
|
|
|
and friends.
|
|
|
|
.\"
|
|
|
|
.It Fn m_split mbuf len how
|
2003-06-15 14:14:11 +00:00
|
|
|
Partition an
|
|
|
|
.Vt mbuf chain
|
|
|
|
in two pieces, returning the tail:
|
2000-10-26 23:15:47 +00:00
|
|
|
all but the first
|
|
|
|
.Fa len
|
2000-11-10 10:08:34 +00:00
|
|
|
bytes.
|
|
|
|
In case of failure, it returns
|
2000-10-26 23:15:47 +00:00
|
|
|
.Dv NULL
|
2003-06-15 14:14:11 +00:00
|
|
|
and attempts to restore the
|
|
|
|
.Vt mbuf chain
|
|
|
|
to its original state.
|
2004-02-11 02:39:24 +00:00
|
|
|
.\"
|
|
|
|
.It Fn m_apply mbuf off len f arg
|
|
|
|
Apply a function to an
|
|
|
|
.Vt mbuf chain ,
|
|
|
|
at offset
|
|
|
|
.Fa off ,
|
|
|
|
for length
|
2004-04-01 07:54:27 +00:00
|
|
|
.Fa len
|
|
|
|
bytes.
|
2004-02-11 02:39:24 +00:00
|
|
|
Typically used to avoid calls to
|
|
|
|
.Fn m_pullup
|
|
|
|
which would otherwise be unnecessary or undesirable.
|
|
|
|
.Fa arg
|
|
|
|
is a convenience argument which is passed to the callback function
|
|
|
|
.Fa f .
|
|
|
|
.Pp
|
|
|
|
Each time
|
|
|
|
.Fn f
|
|
|
|
is called, it will be passed
|
|
|
|
.Fa arg ,
|
|
|
|
a pointer to the
|
|
|
|
.Fa data
|
|
|
|
in the current mbuf, and the length
|
|
|
|
.Fa len
|
|
|
|
of the data in this mbuf to which the function should be applied.
|
|
|
|
.Pp
|
|
|
|
The function should return zero to indicate success;
|
|
|
|
otherwise, if an error is indicated, then
|
|
|
|
.Fn m_apply
|
|
|
|
will return the error and stop iterating through the
|
|
|
|
.Vt mbuf chain .
|
|
|
|
.\"
|
|
|
|
.It Fn m_getptr mbuf loc off
|
|
|
|
Return a pointer to the mbuf containing the data located at
|
|
|
|
.Fa loc
|
|
|
|
bytes from the beginning of the
|
|
|
|
.Vt mbuf chain .
|
|
|
|
The corresponding offset into the mbuf will be stored in
|
|
|
|
.Fa *off .
|
2004-04-01 06:50:21 +00:00
|
|
|
.It Fn m_defrag m0 how
|
2004-04-01 07:54:27 +00:00
|
|
|
Defragment an mbuf chain, returning the shortest possible
|
2004-04-01 07:36:56 +00:00
|
|
|
chain of mbufs and clusters.
|
|
|
|
If allocation fails and this can not be completed,
|
|
|
|
.Dv NULL
|
|
|
|
will be returned and the original chain will be unchanged.
|
|
|
|
Upon success, the original chain will be freed and the new
|
|
|
|
chain will be returned.
|
2004-04-01 06:50:21 +00:00
|
|
|
.Fa how
|
|
|
|
should be either
|
|
|
|
.Dv M_TRYWAIT
|
|
|
|
or
|
|
|
|
.Dv M_DONTWAIT ,
|
|
|
|
depending on the caller's preference.
|
|
|
|
.Pp
|
|
|
|
This function is especially useful in network drivers, where
|
|
|
|
certain long mbuf chains must be shortened before being added
|
|
|
|
to TX descriptor lists.
|
2006-03-15 21:11:11 +00:00
|
|
|
.It Fn m_unshare m0 how
|
|
|
|
Create a version of the specified mbuf chain whose
|
|
|
|
contents can be safely modified without affecting other users.
|
|
|
|
If allocation fails and this operation can not be completed,
|
|
|
|
.Dv NULL
|
|
|
|
will be returned.
|
|
|
|
The original mbuf chain is always reclaimed and the reference
|
|
|
|
count of any shared mbuf clusters is decremented.
|
|
|
|
.Fa how
|
|
|
|
should be either
|
|
|
|
.Dv M_TRYWAIT
|
|
|
|
or
|
|
|
|
.Dv M_DONTWAIT ,
|
|
|
|
depending on the caller's preference.
|
|
|
|
As a side-effect of this process the returned
|
|
|
|
mbuf chain may be compacted.
|
|
|
|
.Pp
|
|
|
|
This function is especially useful in the transmit path of
|
|
|
|
network code, when data must be encrypted or otherwise
|
|
|
|
altered prior to transmission.
|
2000-12-29 09:18:45 +00:00
|
|
|
.El
|
2004-01-21 12:45:37 +00:00
|
|
|
.Sh HARDWARE-ASSISTED CHECKSUM CALCULATION
|
|
|
|
This section currently applies to TCP/IP only.
|
|
|
|
In order to save the host CPU resources, computing checksums is
|
|
|
|
offloaded to the network interface hardware if possible.
|
|
|
|
The
|
|
|
|
.Va m_pkthdr
|
|
|
|
member of the leading
|
|
|
|
.Vt mbuf
|
|
|
|
of a packet contains two fields used for that purpose,
|
|
|
|
.Vt int Va csum_flags
|
|
|
|
and
|
|
|
|
.Vt int Va csum_data .
|
|
|
|
The meaning of those fields depends on the direction a packet flows in,
|
|
|
|
and on whether the packet is fragmented.
|
|
|
|
Henceforth,
|
|
|
|
.Va csum_flags
|
|
|
|
or
|
|
|
|
.Va csum_data
|
|
|
|
of a packet
|
|
|
|
will denote the corresponding field of the
|
|
|
|
.Va m_pkthdr
|
|
|
|
member of the leading
|
|
|
|
.Vt mbuf
|
|
|
|
in the
|
|
|
|
.Vt mbuf chain
|
|
|
|
containing the packet.
|
|
|
|
.Pp
|
|
|
|
On output, checksum offloading is attempted after the outgoing
|
|
|
|
interface has been determined for a packet.
|
|
|
|
The interface-specific field
|
|
|
|
.Va ifnet.if_data.ifi_hwassist
|
|
|
|
(see
|
|
|
|
.Xr ifnet 9 )
|
|
|
|
is consulted for the capabilities of the interface to assist in
|
|
|
|
computing checksums.
|
|
|
|
The
|
|
|
|
.Va csum_flags
|
2004-04-01 07:54:27 +00:00
|
|
|
field of the packet header is set to indicate which actions the interface
|
2004-01-21 12:45:37 +00:00
|
|
|
is supposed to perform on it.
|
|
|
|
The actions unsupported by the network interface are done in the
|
|
|
|
software prior to passing the packet down to the interface driver;
|
|
|
|
such actions will never be requested through
|
|
|
|
.Va csum_flags .
|
|
|
|
.Pp
|
|
|
|
The flags demanding a particular action from an interface are as follows:
|
|
|
|
.Bl -tag -width ".Dv CSUM_TCP" -offset indent
|
|
|
|
.It Dv CSUM_IP
|
|
|
|
The IP header checksum is to be computed and stored in the
|
|
|
|
corresponding field of the packet.
|
|
|
|
The hardware is expected to know the format of an IP header
|
|
|
|
to determine the offset of the IP checksum field.
|
|
|
|
.It Dv CSUM_TCP
|
2004-04-01 07:54:27 +00:00
|
|
|
The TCP checksum is to be computed.
|
|
|
|
(See below.)
|
2004-01-21 12:45:37 +00:00
|
|
|
.It Dv CSUM_UDP
|
2004-04-01 07:54:27 +00:00
|
|
|
The UDP checksum is to be computed.
|
|
|
|
(See below.)
|
2004-01-21 12:45:37 +00:00
|
|
|
.El
|
|
|
|
.Pp
|
2004-01-21 13:17:24 +00:00
|
|
|
Should a TCP or UDP checksum be offloaded to the hardware,
|
2004-01-21 12:45:37 +00:00
|
|
|
the field
|
|
|
|
.Va csum_data
|
|
|
|
will contain the byte offset of the checksum field relative to the
|
|
|
|
end of the IP header.
|
|
|
|
In this case, the checksum field will be initially
|
|
|
|
set by the TCP/IP module to the checksum of the pseudo header
|
2004-01-21 13:17:24 +00:00
|
|
|
defined by the TCP and UDP specifications.
|
2004-01-21 12:45:37 +00:00
|
|
|
.Pp
|
|
|
|
For outbound packets which have been fragmented
|
|
|
|
by the host CPU, the following will also be true,
|
|
|
|
regardless of the checksum flag settings:
|
|
|
|
.Bl -bullet -offset indent
|
|
|
|
.It
|
|
|
|
all fragments will have the flag
|
|
|
|
.Dv M_FRAG
|
|
|
|
set in their
|
|
|
|
.Va m_flags
|
|
|
|
field;
|
|
|
|
.It
|
|
|
|
the first and the last fragments in the chain will have
|
|
|
|
.Dv M_FIRSTFRAG
|
|
|
|
or
|
|
|
|
.Dv M_LASTFRAG
|
|
|
|
set in their
|
|
|
|
.Va m_flags ,
|
|
|
|
correspondingly;
|
|
|
|
.It
|
|
|
|
the first fragment in the chain will have the total number
|
|
|
|
of fragments contained in its
|
|
|
|
.Va csum_data
|
|
|
|
field.
|
|
|
|
.El
|
|
|
|
.Pp
|
2004-01-21 23:32:50 +00:00
|
|
|
The last rule for fragmented packets takes precedence over the one
|
|
|
|
for a TCP or UDP checksum.
|
|
|
|
Nevertheless, offloading a TCP or UDP checksum is possible for a
|
|
|
|
fragmented packet if the flag
|
|
|
|
.Dv CSUM_IP_FRAGS
|
|
|
|
is set in the field
|
|
|
|
.Va ifnet.if_data.ifi_hwassist
|
|
|
|
associated with the network interface.
|
|
|
|
However, in this case the interface is expected to figure out
|
|
|
|
the location of the checksum field within the sequence of fragments
|
|
|
|
by itself because
|
2004-01-21 12:45:37 +00:00
|
|
|
.Va csum_data
|
2004-01-21 23:32:50 +00:00
|
|
|
contains a fragment count instead of a checksum offset value.
|
2004-01-21 12:45:37 +00:00
|
|
|
.Pp
|
|
|
|
On input, an interface indicates the actions it has performed
|
|
|
|
on a packet by setting one or more of the following flags in
|
|
|
|
.Va csum_flags
|
|
|
|
associated with the packet:
|
|
|
|
.Bl -tag -width ".Dv CSUM_IP_CHECKED" -offset indent
|
|
|
|
.It Dv CSUM_IP_CHECKED
|
|
|
|
The IP header checksum has been computed.
|
|
|
|
.It Dv CSUM_IP_VALID
|
|
|
|
The IP header has a valid checksum.
|
|
|
|
This flag can appear only in combination with
|
|
|
|
.Dv CSUM_IP_CHECKED .
|
|
|
|
.It Dv CSUM_DATA_VALID
|
|
|
|
The checksum of the data portion of the IP packet has been computed
|
|
|
|
and stored in the field
|
|
|
|
.Va csum_data
|
|
|
|
in network byte order.
|
|
|
|
.It Dv CSUM_PSEUDO_HDR
|
|
|
|
Can be set only along with
|
|
|
|
.Dv CSUM_DATA_VALID
|
|
|
|
to indicate that the IP data checksum found in
|
|
|
|
.Va csum_data
|
2004-01-21 13:17:24 +00:00
|
|
|
allows for the pseudo header defined by the TCP and UDP specifications.
|
2004-01-21 12:45:37 +00:00
|
|
|
Otherwise the checksum of the pseudo header must be calculated by
|
|
|
|
the host CPU and added to
|
|
|
|
.Va csum_data
|
2004-01-21 13:17:24 +00:00
|
|
|
to obtain the final checksum to be used for TCP or UDP validation purposes.
|
2004-01-21 12:45:37 +00:00
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
If a particular network interface just indicates success or
|
|
|
|
failure of TCP or UDP checksum validation without returning
|
|
|
|
the exact value of the checksum to the host CPU, its driver can mark
|
|
|
|
.Dv CSUM_DATA_VALID
|
|
|
|
and
|
|
|
|
.Dv CSUM_PSEUDO_HDR
|
|
|
|
in
|
|
|
|
.Va csum_flags ,
|
|
|
|
and set
|
|
|
|
.Va csum_data
|
|
|
|
to
|
|
|
|
.Li 0xFFFF
|
|
|
|
hexadecimal to indicate a valid checksum.
|
2004-04-01 07:54:27 +00:00
|
|
|
It is a peculiarity of the algorithm used that the Internet checksum
|
2004-01-21 12:45:37 +00:00
|
|
|
calculated over any valid packet will be
|
|
|
|
.Li 0xFFFF
|
|
|
|
as long as the original checksum field is included.
|
|
|
|
.Pp
|
|
|
|
For inbound packets which are IP fragments, all
|
|
|
|
.Va csum_data
|
|
|
|
fields will be summed during reassembly to obtain the final checksum
|
|
|
|
value passed to an upper layer in the
|
|
|
|
.Va csum_data
|
|
|
|
field of the reassembled packet.
|
|
|
|
The
|
|
|
|
.Va csum_flags
|
|
|
|
fields of all fragments will be consolidated using logical AND
|
|
|
|
to obtain the final value for
|
|
|
|
.Va csum_flags .
|
|
|
|
Thus, in order to successfully
|
|
|
|
offload checksum computation for fragmented data,
|
|
|
|
all fragments should have the same value of
|
|
|
|
.Va csum_flags .
|
2003-04-13 23:31:06 +00:00
|
|
|
.Sh STRESS TESTING
|
2003-05-21 15:49:01 +00:00
|
|
|
When running a kernel compiled with the option
|
|
|
|
.Dv MBUF_STRESS_TEST ,
|
|
|
|
the following
|
|
|
|
.Xr sysctl 8 Ns
|
|
|
|
-controlled options may be used to create
|
|
|
|
various failure/extreme cases for testing of network drivers
|
2003-06-15 14:14:11 +00:00
|
|
|
and other parts of the kernel that rely on
|
|
|
|
.Vt mbufs .
|
2003-05-21 15:49:01 +00:00
|
|
|
.Bl -tag -width ident
|
|
|
|
.It Va net.inet.ip.mbuf_frag_size
|
|
|
|
Causes
|
|
|
|
.Fn ip_output
|
2003-06-15 14:14:11 +00:00
|
|
|
to fragment outgoing
|
|
|
|
.Vt mbuf chains
|
|
|
|
into fragments of the specified size.
|
2003-05-21 15:49:01 +00:00
|
|
|
Setting this variable to 1 is an excellent way to
|
2003-06-15 14:14:11 +00:00
|
|
|
test the long
|
|
|
|
.Vt mbuf chain
|
|
|
|
handling ability of network drivers.
|
2003-05-21 15:49:01 +00:00
|
|
|
.It Va kern.ipc.m_defragrandomfailures
|
|
|
|
Causes the function
|
|
|
|
.Fn m_defrag
|
|
|
|
to randomly fail, returning
|
|
|
|
.Dv NULL .
|
|
|
|
Any piece of code which uses
|
|
|
|
.Fn m_defrag
|
|
|
|
should be tested with this feature.
|
|
|
|
.El
|
2000-10-26 23:15:47 +00:00
|
|
|
.Sh RETURN VALUES
|
|
|
|
See above.
|
2004-01-21 12:45:37 +00:00
|
|
|
.Sh SEE ALSO
|
2004-05-20 09:34:19 +00:00
|
|
|
.Xr ifnet 9 ,
|
|
|
|
.Xr mbuf_tags 9
|
2000-10-26 23:15:47 +00:00
|
|
|
.Sh HISTORY
|
|
|
|
.\" Please correct me if I'm wrong
|
2003-06-15 14:14:11 +00:00
|
|
|
.Vt Mbufs
|
|
|
|
appeared in an early version of
|
2001-08-14 10:01:54 +00:00
|
|
|
.Bx .
|
2003-11-15 14:23:48 +00:00
|
|
|
Besides being used for network packets, they were used
|
2000-11-01 23:02:19 +00:00
|
|
|
to store various dynamic structures, such as routing table
|
|
|
|
entries, interface addresses, protocol control blocks, etc.
|
2000-10-26 23:15:47 +00:00
|
|
|
.Sh AUTHORS
|
2001-08-08 11:48:28 +00:00
|
|
|
The original
|
|
|
|
.Nm
|
2005-06-28 20:15:19 +00:00
|
|
|
manual page was written by Yar Tikhiy.
|