MBUF(9)

πŸ“¦
version 15.1 Β· 4 versions
, in freebsd 14.5, freebsd 15.1

🌸 mbuf(9): memory management in the kernel IPC subsystem 🌸

okay bestie, welcome to the Kernel Developer's Manual page for mbuf β€” literally the memory-management main character of the kernel IPC subsystem. iykyk πŸ’…


✨ NAME ✨

mbuf β€” memory management in the kernel IPC subsystem. periodt. πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

First, the sacred includes:

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>

🌈 Mbuf allocation macros 🌈

These are the allocation macros, and yes, every single one is important: πŸ’–

MGET(struct mbuf *mbuf, int how, short type);

MGETHDR(struct mbuf *mbuf, int how, short type);

int
MCLGET(struct mbuf *mbuf, int how);

MEXTADD(struct mbuf *mbuf, char *buf, u_int size,
    void (*free)(struct mbuf *), void *opt_arg1, void *opt_arg2,
    int flags, int type);

🧚 Mbuf utility macros 🧚

Utility macro lineup, no skips, no cap: ✨

type
mtod(struct mbuf *mbuf, type);

void *
mtodo(struct mbuf *mbuf, offset);

M_ALIGN(struct mbuf *mbuf, u_int len);

MH_ALIGN(struct mbuf *mbuf, u_int len);

int
M_LEADINGSPACE(struct mbuf *mbuf);

int
M_TRAILINGSPACE(struct mbuf *mbuf);

M_MOVE_PKTHDR(struct mbuf *to, struct mbuf *from);

M_PREPEND(struct mbuf *mbuf, int len, int how);

MCHTYPE(struct mbuf *mbuf, short type);

int
M_WRITABLE(struct mbuf *mbuf);

πŸŽ€ Mbuf allocation functions πŸŽ€

Function forms, coming through: πŸ’–

struct mbuf *
m_get(int how, short type);

struct mbuf *
m_get2(int size, int how, short type, int flags);

struct mbuf *
m_get3(int size, int how, short type, int flags);

struct mbuf *
m_getm(struct mbuf *orig, int len, int how, short type);

struct mbuf *
m_getjcl(int how, short type, int flags, int size);

struct mbuf *
m_getcl(int how, short type, int flags);

struct mbuf *
m_gethdr(int how, short type);

struct mbuf *
m_free(struct mbuf *mbuf);

void
m_freem(struct mbuf *mbuf);

🌟 Mbuf utility functions 🌟

And here's the whole utility-function squad: 🫢

void
m_adj(struct mbuf *mbuf, int len);

void
m_align(struct mbuf *mbuf, int len);

int
m_append(struct mbuf *mbuf, int len, c_caddr_t cp);

struct mbuf *
m_prepend(struct mbuf *mbuf, int len, int how);

struct mbuf *
m_copyup(struct mbuf *mbuf, int len, int dstoff);

struct mbuf *
m_pullup(struct mbuf *mbuf, int len);

struct mbuf *
m_pulldown(struct mbuf *mbuf, int offset, int len, int *offsetp);

struct mbuf *
m_copym(struct mbuf *mbuf, int offset, int len, int how);

struct mbuf *
m_copypacket(struct mbuf *mbuf, int how);

struct mbuf *
m_dup(const struct mbuf *mbuf, int how);

void
m_copydata(const struct mbuf *mbuf, int offset, int len, caddr_t buf);

void
m_copyback(struct mbuf *mbuf, int offset, int len, caddr_t buf);

struct mbuf *
m_devget(char *buf, int len, int offset, struct ifnet *ifp,
    void (*copy)(char *from, caddr_t to, u_int len));

void
m_cat(struct mbuf *m, struct mbuf *n);

void
m_catpkt(struct mbuf *m, struct mbuf *n);

u_int
m_fixhdr(struct mbuf *mbuf);

int
m_dup_pkthdr(struct mbuf *to, const struct mbuf *from, int how);

void
m_move_pkthdr(struct mbuf *to, struct mbuf *from);

u_int
m_length(struct mbuf *mbuf, struct mbuf **last);

struct mbuf *
m_split(struct mbuf *mbuf, int len, int how);

int
m_apply(struct mbuf *mbuf, int off, int len,
    int (*f)(void *arg, void *data, u_int len), void *arg);

struct mbuf *
m_getptr(struct mbuf *mbuf, int loc, int *off);

struct mbuf *
m_defrag(struct mbuf *m0, int how);

struct mbuf *
m_collapse(struct mbuf *m0, int how, int maxfrags);

struct mbuf *
m_unshare(struct mbuf *m0, int how);

πŸ’œ DESCRIPTION πŸ’œ

So here's the tea: an mbuf is a basic unit of memory management in the kernel IPC subsystem. Network packets and socket buffers are stored in mbufs. A network packet may span multiple mbufs arranged into a mbuf chain (linked list), which allows adding or trimming network headers with little overhead. iconic behavior, honestly. ✨

Heads up, bestie: a developer should not bother with mbuf internals without serious reason, because that's how you avoid incompatibilities with future changes. But it is useful to understand the general structure of an mbuf. πŸ’…

An mbuf consists of a variable-sized header and a small internal buffer for data. The total size of an mbuf, MSIZE, is a constant defined in <sys/param.h>. The mbuf header includes:

  • m_next (struct mbuf *) β€” a pointer to the next mbuf in the mbuf chain.
  • m_nextpkt (struct mbuf *) β€” a pointer to the next mbuf chain in the queue.
  • m_data (caddr_t) β€” a pointer to data attached to this mbuf.
  • m_len (int) β€” the length of the data.
  • m_type (short) β€” the type of the data.
  • m_flags (int) β€” the mbuf flags.

The mbuf flag bits are defined as follows: πŸ¦‹

#define M_EXT           0x00000001 /* has associated external storage */
#define M_PKTHDR        0x00000002 /* start of record */
#define M_EOR           0x00000004 /* end of record */
#define M_RDONLY        0x00000008 /* associated data marked read-only */
#define M_BCAST         0x00000010 /* send/received as link-level broadcast */
#define M_MCAST         0x00000020 /* send/received as link-level multicast */
#define M_PROMISC       0x00000040 /* packet was not for us */
#define M_VLANTAG       0x00000080 /* ether_vtag is valid */
#define M_EXTPG         0x00000100 /* has array of unmapped pages and TLS */
#define M_NOFREE        0x00000200 /* do not free mbuf, embedded in cluster */
#define M_TSTMP         0x00000400 /* rcv_tstmp field is valid */
#define M_TSTMP_HPREC   0x00000800 /* rcv_tstmp is high-prec, typically
                                      hw-stamped on port (useful for IEEE 1588
                                      and 802.1AS) */

#define M_PROTO1        0x00001000 /* protocol-specific */
#define M_PROTO2        0x00002000 /* protocol-specific */
#define M_PROTO3        0x00004000 /* protocol-specific */
#define M_PROTO4        0x00008000 /* protocol-specific */
#define M_PROTO5        0x00010000 /* protocol-specific */
#define M_PROTO6        0x00020000 /* protocol-specific */
#define M_PROTO7        0x00040000 /* protocol-specific */
#define M_PROTO8        0x00080000 /* protocol-specific */
#define M_PROTO9        0x00100000 /* protocol-specific */
#define M_PROTO10       0x00200000 /* protocol-specific */
#define M_PROTO11       0x00400000 /* protocol-specific */
#define M_PROTO12       0x00800000 /* protocol-specific */

The available mbuf types are defined as follows: πŸŽ€

#define MT_DATA         1       /* dynamic (data) allocation */
#define MT_HEADER       MT_DATA /* packet header */

#define MT_VENDOR1      4       /* for vendor-internal use */
#define MT_VENDOR2      5       /* for vendor-internal use */
#define MT_VENDOR3      6       /* for vendor-internal use */
#define MT_VENDOR4      7       /* for vendor-internal use */

#define MT_SONAME       8       /* socket name */

#define MT_EXP1         9       /* for experimental use */
#define MT_EXP2         10      /* for experimental use */
#define MT_EXP3         11      /* for experimental use */
#define MT_EXP4         12      /* for experimental use */

#define MT_CONTROL      14      /* extra-data protocol message */
#define MT_EXTCONTROL   15      /* control message with externalized contents */
#define MT_OOBDATA      16      /* expedited data  */

The available external buffer types are defined as follows: 🌈

#define EXT_CLUSTER     1       /* mbuf cluster */
#define EXT_SFBUF       2       /* sendfile(2)'s sf_bufs */
#define EXT_JUMBOP      3       /* jumbo cluster 4096 bytes */
#define EXT_JUMBO9      4       /* jumbo cluster 9216 bytes */
#define EXT_JUMBO16     5       /* jumbo cluster 16184 bytes */
#define EXT_PACKET      6       /* mbuf+cluster from packet zone */
#define EXT_MBUF        7       /* external mbuf reference */
#define EXT_RXRING      8       /* data in NIC receive ring */
#define EXT_PGS         9       /* array of unmapped pages */

#define EXT_VENDOR1     224     /* for vendor-internal use */
#define EXT_VENDOR2     225     /* for vendor-internal use */
#define EXT_VENDOR3     226     /* for vendor-internal use */
#define EXT_VENDOR4     227     /* for vendor-internal use */

#define EXT_EXP1        244     /* for experimental use */
#define EXT_EXP2        245     /* for experimental use */
#define EXT_EXP3        246     /* for experimental use */
#define EXT_EXP4        247     /* for experimental use */

#define EXT_NET_DRV     252     /* custom ext_buf provided by net driver(s) */
#define EXT_MOD_TYPE    253     /* custom module's ext_buf type */
#define EXT_DISPOSABLE  254     /* can throw this buffer away w/page flipping */
#define EXT_EXTREF      255     /* has externally maintained ref_cnt ptr */

If the M_PKTHDR flag is set, a struct pkthdr m_pkthdr is added to the mbuf header. It contains a pointer to the interface the packet has been received from (struct ifnet *rcvif), and the total packet length (int len). Optionally, it may also contain an attached list of packet tags (struct m_tag). See mbuf_tags(9) for details. Fields used in offloading checksum calculation to the hardware are kept in m_pkthdr as well. See β€œHARDWARE-ASSISTED CHECKSUM CALCULATION” for details. iykyk. πŸ”₯

If small enough, data is stored in the internal data buffer of an mbuf. If the data is sufficiently large, another mbuf may be added to the mbuf chain, or external storage may be associated with the mbuf. MHLEN bytes of data can fit into an mbuf with the M_PKTHDR flag set, MLEN bytes can otherwise. ✨

If external storage is being associated with an mbuf, the m_ext header is added at the cost of losing the internal data buffer. 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. An mbuf using external storage has the M_EXT flag set. πŸ’…

The system supplies a macro for allocating the desired external storage buffer, MEXTADD. 🫢

The allocation and management of the reference counter is handled by the subsystem. no cap.

The system also supplies a default type of external storage buffer called an mbuf cluster. Mbuf clusters can be allocated and configured with the use of the MCLGET macro. Each mbuf cluster is MCLBYTES in size, where MCLBYTES is a machine-dependent constant. The system defines an advisory macro MINCLSIZE, which is the smallest amount of data to put into an mbuf cluster. It is equal to MHLEN plus one. It is typically preferable to store data into the data region of an mbuf, if size permits, as opposed to allocating a separate mbuf cluster to hold the same data. 🌸


πŸ’– Macros and Functions πŸ’–

There are numerous predefined macros and functions that provide the developer with common utilities. and bestie, the utility lineup is genuinely giving main character energy. πŸ‘‘

okay so picking up right where we left off β€” the macros are still very much the main character, and they are giving β€œI know exactly what I’m doing with your memory.” πŸ’…

  • mtod(mbuf, type) β€” turns an mbuf pointer into a data pointer, bestie. The macro expands to the data pointer cast to the specified type. note: it’s advisable to make sure there’s enough contiguous data in mbuf first β€” see m_pullup() for the details. ✨
  • mtodo(mbuf, offset) β€” gives you a data pointer at an offset (in bytes) into the data attached to mbuf. Returns a void * pointer. note: the caller has to make sure the offset is in bounds of the attached data. no sneaky out-of-bounds vibes, okay? 🫢
  • MGET(mbuf, how, type) β€” allocates an mbuf and initializes it to contain internal data. On success, mbuf points to the allocated mbuf; on failure, it’s set to NULL. The how argument is set to M_WAITOK or M_NOWAIT, and it says whether the caller is willing to block if necessary. A bunch of other mbuf-related functions and macros take the same argument because they may at some point need to allocate new mbufs. πŸŽ€
  • MGETHDR(mbuf, how, type) β€” allocates an mbuf and initializes it to contain a packet header and internal data. See MGET() for the details. πŸ‘‘
  • MEXTADD(mbuf, buf, size, free, opt_arg1, opt_arg2, flags, type) β€” associates externally managed data with mbuf. Any internal data already in the mbuf gets discarded, and the M_EXT flag gets set. The buf and size arguments are the address and length, respectively, of the data. The free argument points to a function that will be called to free the data when the mbuf is freed; it’s only used if type is EXT_EXTREF. The opt_arg1 and opt_arg2 arguments get saved in the ext_arg1 and ext_arg2 fields of the struct m_ext of the mbuf. The flags argument specifies additional mbuf flags; you don’t need to specify M_EXT. Finally, the type argument specifies the type of external data, which controls how it will be disposed of when the mbuf is freed. In most cases, the correct value is EXT_EXTREF. πŸ”₯
  • MCLGET(mbuf, how) β€” allocates and attaches an mbuf cluster to mbuf. On success, you get a non-zero value back; otherwise it’s 0. Historically, consumers would check for success by testing the M_EXT flag on the mbuf, but that’s now discouraged so we can avoid unnecessary awareness of the implementation of external storage in protocol stacks and device drivers. πŸ’€
  • M_ALIGN(mbuf, len) β€” sets the pointer mbuf->m_data to place an object of size len at the end of the internal data area of mbuf, long word aligned. Applicable only if mbuf was newly allocated with MGET() or m_get(). 🌸
  • MH_ALIGN(mbuf, len) β€” serves the same purpose as M_ALIGN(), but only for an mbuf newly allocated with MGETHDR() or m_gethdr(), or initialized by m_dup_pkthdr() or m_move_pkthdr(). πŸ¦‹
  • m_align(mbuf, len) β€” serves the same purpose as M_ALIGN() but handles any type of mbuf. versatile icon behavior. ✨
  • M_LEADINGSPACE(mbuf) β€” returns the number of bytes available before the beginning of data in mbuf. 🌈
  • M_TRAILINGSPACE(mbuf) β€” returns the number of bytes available after the end of data in mbuf. ⭐
  • M_PREPEND(mbuf, len, how) β€” this macro operates on an mbuf chain. It’s an optimized wrapper for m_prepend() that can make use of possible empty space before data (e.g. left after trimming of a link-layer header). After the call, the new mbuf chain pointer or NULL is in mbuf. πŸŽ€
  • M_MOVE_PKTHDR(to, from) β€” using this macro is equivalent to calling m_move_pkthdr(to, from). periodt. πŸ’…
  • M_WRITABLE(mbuf) β€” this macro evaluates true if mbuf is not marked M_RDONLY and if either mbuf does not contain external storage or, if it does, then if the reference count of the storage is not greater than 1. The M_RDONLY flag can be set in mbuf->m_flags. This can be achieved during setup of the external storage by passing the M_RDONLY bit as a flags argument to the MEXTADD() macro, or it can be directly set in individual mbufs. πŸ‘‘
  • MCHTYPE(mbuf, type) β€” changes the type of mbuf to type. This is a relatively expensive operation and should be avoided. not me recommending the expensive option β€” literally don’t. πŸ’€

okay so the functions are:

  • m_get(how, type) β€” a function version of MGET() for non-critical paths. 🌸
  • m_get2(size, how, type, flags) β€” allocates an mbuf with enough space to hold the specified amount of data. If size is larger than MJUMPAGESIZE, NULL will be returned. 🫢
  • m_get3(size, how, type, flags) β€” allocates an mbuf with enough space to hold the specified amount of data. If size is larger than MJUM16BYTES, NULL will be returned. πŸ¦‹
  • m_getm(orig, len, how, type) β€” allocates len bytes worth of mbufs and mbuf clusters if necessary, and appends the resulting allocated mbuf chain to the mbuf chain orig, if it is non-NULL. If the allocation fails at any point, it frees whatever was allocated and returns NULL. If orig is non-NULL, it will not be freed. It’s possible to use m_getm() to either append len bytes to an existing mbuf or mbuf chain (for example, one which may be sitting in a pre-allocated ring) or to simply perform an all-or-nothing mbuf and mbuf cluster allocation. ✨
  • m_gethdr(how, type) β€” a function version of MGETHDR() for non-critical paths. πŸŽ€
  • m_getcl(how, type, flags) β€” fetches an mbuf with an 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 mbuf and mbuf cluster together, as it avoids having to unlock/relock between allocations. Returns NULL on failure. πŸ’œ
  • m_getjcl(how, type, flags, size) β€” this is like m_getcl() but the specified size of the cluster to be allocated must be one of MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, or MJUM16BYTES. πŸ”₯
  • m_free(mbuf) β€” frees mbuf. Returns m_next of the freed mbuf. okay bye bestie, and here’s your next pointer. πŸ’…

the functions below operate on mbuf chains:

  • m_freem(mbuf) β€” frees an entire mbuf chain, including any external storage. 🌈
  • m_adj(mbuf, len) β€” trims len bytes from the head of an mbuf chain if len is positive, from the tail otherwise. ⭐
  • m_append(mbuf, len, cp) β€” appends len bytes of data cp to the mbuf chain. Extends the mbuf chain if the new data does not fit in existing space. πŸ“
  • m_prepend(mbuf, len, how) β€” allocates a new mbuf and prepends it to the mbuf chain, handling M_PKTHDR properly. note: it does not allocate any mbuf clusters, so len must be less than MLEN or MHLEN, depending on the M_PKTHDR flag setting. 😭
  • m_copyup(mbuf, len, dstoff) β€” similar to m_pullup() but copies len bytes of data into a new mbuf at dstoff bytes into the mbuf. The dstoff argument aligns the data and leaves room for a link-layer header. Returns the new mbuf chain on success, and frees the mbuf chain and returns NULL on failure. note: the function does not allocate mbuf clusters, so len + dstoff must be less than MHLEN. 🫢
  • m_pullup(mbuf, len) β€” arranges that the first len bytes of an mbuf chain are contiguous and lay in the data area of mbuf, so they’re accessible with mtod(mbuf, type). 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. Returns the new mbuf chain on success, NULL on failure (the mbuf chain is freed in this case). note: it does not allocate any mbuf clusters, so len must be less than or equal to MHLEN. πŸ’€
  • m_pulldown(mbuf, offset, len, offsetp) β€” arranges that len bytes between offset and offset + len in the mbuf chain are contiguous and lay in the data area of mbuf, so they’re accessible with mtod() or mtodo(). len must be smaller than, or equal to, the size of an mbuf cluster. Returns a pointer to an intermediate mbuf in the chain containing the requested region; the offset in the data region of the mbuf chain to the data contained in the returned mbuf is stored in *offsetp. If offsetp is NULL, the region may be accessed using mtod(mbuf, type) or mtodo(mbuf, 0). If offsetp is non-NULL, the region may be accessed using mtodo(mbuf, *offsetp). The region of the mbuf chain between its beginning and offset is not modified, so it’s safe to hold pointers to data within this region before calling m_pulldown(). πŸ‘‘
  • m_copym(mbuf, offset, len, how) β€” makes a copy of an mbuf chain starting offset bytes from the beginning, continuing for len bytes. If len is M_COPYALL, it copies to the end of the mbuf chain. note: the copy is read-only, because the mbuf clusters are not copied β€” only their reference counts are incremented. ✨
  • m_copypacket(mbuf, how) β€” copies an entire packet including the header, which must be present. This is an optimized version of the common case m_copym(mbuf, 0, M_COPYALL, how). note: the copy is read-only, because the mbuf clusters are not copied β€” only their reference counts are incremented. πŸ’œ
  • m_dup(mbuf, how) β€” copies a packet header mbuf chain into a completely new mbuf chain, including copying any mbuf clusters. Use this instead of m_copypacket() when you need a writable copy of an mbuf chain. πŸ”₯
  • m_copydata(mbuf, offset, len, buf) β€” copies data from an mbuf chain starting offset bytes from the beginning, continuing for len bytes, into the indicated buffer buf. 🌸
  • m_copyback(mbuf, offset, len, buf) β€” copies len bytes from the buffer buf back into the indicated mbuf chain, starting at offset bytes from the beginning of the mbuf chain, extending the mbuf chain if necessary. note: it does not allocate any mbuf clusters, just adds mbufs to the mbuf chain. It is safe to set offset beyond the current mbuf chain end: zeroed mbufs will be allocated to fill the space. πŸ¦‹
  • m_length(mbuf, last) β€” returns the length of the mbuf chain, and optionally a pointer to the last mbuf. ⭐
  • m_dup_pkthdr(to, from, how) β€” when the function completes, the mbuf to will contain an identical copy of from->m_pkthdr and the per-packet attributes found in the mbuf chain from. The mbuf from must have the flag M_PKTHDR initially set, and to must be empty on entry. πŸ‘‘
  • m_move_pkthdr(to, from) β€” okay so this one moves the m_pkthdr and the per-packet attributes from the mbuf chain from over to the mbuf to. from has to come in with the M_PKTHDR flag initially set, and to must be empty on entry. When the function finishes, from will have the M_PKTHDR flag and the per-packet attributes cleared.
  • m_fixhdr(mbuf) β€” sets the packet-header length to the length of the mbuf chain. cute and simple πŸ’…
  • m_devget(buf, len, offset, ifp, copy) β€” copies data from device local memory pointed to by buf into an mbuf chain. The copy is done using the specified copy routine copy, or bcopy() if copy is NULL.
  • m_cat(m, n) β€” concatenates n to m. Both mbuf chains must be of the same type. Heads up: n is not guaranteed to be valid after m_cat() returns. Also, m_cat() does not update any packet header fields or free mbuf tags, no cap.
  • m_catpkt(m, n) β€” a variant of m_cat() that operates on packets. Both m and n must contain packet headers. n is not guaranteed to be valid after m_catpkt() returns.
  • m_split(mbuf, len, how) β€” partitions an mbuf chain into two pieces and returns the tail: all but the first len bytes. If it fails, it returns NULL and attempts to restore the mbuf chain to its original state.
  • m_apply(mbuf, off, len, f, arg) β€” applies a function to an mbuf chain, at offset off, for length len bytes. Typically used to avoid calls to m_pullup() which would otherwise be unnecessary or undesirable. arg is a convenience argument which is passed to the callback function f.

Each time f() is called, it will be passed arg, a pointer to the data in the current mbuf, and the length len of the data in this mbuf to which the function should be applied.

The function should return zero to indicate success; otherwise, if an error is indicated, then m_apply() will return the error and stop iterating through the mbuf chain.

  • m_getptr(mbuf, loc, off) β€” returns a pointer to the mbuf containing the data located at loc bytes from the beginning of the mbuf chain. The corresponding offset into the mbuf will be stored in *off.
  • m_defrag(m0, how) β€” defragments an mbuf chain, returning the shortest possible chain of mbufs and clusters. If allocation fails and this can not be completed, 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. how should be either M_WAITOK or M_NOWAIT, depending on the caller's preference.

This function is especially useful in network drivers, where certain long mbuf chains must be shortened before being added to TX descriptor lists ✨

  • m_collapse(m0, how, maxfrags) β€” defragments an mbuf chain, returning a chain of at most maxfrags mbufs and clusters. If allocation fails or the chain cannot be collapsed as requested, NULL will be returned, with the original chain possibly modified. As with m_defrag(), how should be one of M_WAITOK or M_NOWAIT.
⚠️ bestie, note the β€œpossibly modified” part β€” the original chain is not guaranteed to be untouched here.
  • m_unshare(m0, how) β€” creates 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, NULL will be returned. The original mbuf chain is always reclaimed and the reference count of any shared mbuf clusters is decremented. how should be either M_WAITOK or M_NOWAIT, depending on the caller's preference. As a side-effect of this process the returned mbuf chain may be compacted.

This function is especially useful in the transmit path of network code, when data must be encrypted or otherwise altered prior to transmission πŸ”₯

πŸ”₯ HARDWARE-ASSISTED CHECKSUM CALCULATION πŸ”₯

This section currently applies to SCTP, TCP, and UDP over IP only. In order to save the host CPU resources, computing checksums is offloaded to the network interface hardware if possible. The m_pkthdr member of the leading mbuf of a packet contains two fields used for that purpose, int csum_flags and int csum_data. The meaning of those fields depends on whether the packet is fragmented. Henceforth, csum_flags or csum_data of a packet will denote the corresponding field of the m_pkthdr member of the leading mbuf in the mbuf chain containing the packet.

When a packet is sent by SCTP, TCP, or UDP, the computation of the checksum is delayed until the outgoing interface has been determined for a packet. The interface-specific field ifnet.if_data.ifi_hwassist (see ifnet(9)) is consulted by IP for the capabilities of the network interface selected for output to assist in computing checksums. The csum_flags field of the packet header is set to indicate which actions the interface 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 csum_flags.

The flags demanding a particular action from an interface are as follows:

  • 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.
  • CSUM_SCTP β€” the SCTP checksum is to be computed. (See below.)
  • CSUM_TCP β€” the TCP checksum is to be computed. (See below.)
  • CSUM_UDP β€” the UDP checksum is to be computed. (See below.)

Should a SCTP, TCP, or UDP checksum be offloaded to the hardware, the field csum_data will contain the byte offset of the checksum field relative to the end of the IP header. In the case of TCP or UDP, the checksum field will be initially set by the TCP or UDP implementation to the checksum of the pseudo header defined by the TCP and UDP specifications. In the case of SCTP, the checksum field will be initially set by the SCTP implementation to 0.

When a packet is received by an interface, it indicates the actions it has performed on a packet by setting one or more of the following flags in csum_flags associated with the packet:

  • CSUM_IP_CHECKED β€” the IP header checksum has been computed.
  • CSUM_IP_VALID β€” the IP header has a valid checksum. This flag can appear only in combination with CSUM_IP_CHECKED.
  • CSUM_DATA_VALID β€” the checksum of the data portion of the IP packet has been computed and stored in the field csum_data in network byte order.
  • CSUM_PSEUDO_HDR β€” can be set only along with CSUM_DATA_VALID to indicate that the IP data checksum found in csum_data allows for the pseudo header defined by the TCP and UDP specifications. Otherwise the checksum of the pseudo header must be calculated by the host CPU and added to csum_data to obtain the final checksum to be used for TCP or UDP validation purposes.

If a particular network interface just indicates success or failure of SCTP, TCP, or UDP checksum validation without returning the exact value of the checksum to the host CPU, its driver can mark CSUM_DATA_VALID in csum_flags as well as, for TCP and UDP, CSUM_PSEUDO_HDR and set csum_data to 0xFFFF hexadecimal to indicate a valid checksum. It is a peculiarity of the algorithm used that the Internet checksum calculated over any valid packet will be 0xFFFF as long as the original checksum field is included. Note that for SCTP the value of csum_data is not relevant and CSUM_PSEUDO_HDR in csum_flags is not set, since SCTP does not use a pseudo header checksum.

If IP delivers a packet with the flags CSUM_IP, CSUM_SCTP, CSUM_TCP, or CSUM_UDP set in csum_flags to a local IP, SCTP, TCP, or UDP stack, the packet will be processed without computing or validating the checksum, since the packet has not been on the wire. This can happen if the packet was handled by a virtual interface such as tap(4) or epair(4).

πŸ’€ STRESS TESTING πŸ’€

When running a kernel compiled with the option MBUF_STRESS_TEST, the following sysctl(8)-controlled options may be used to create various failure/extreme cases for testing of network drivers and other parts of the kernel that rely on mbufs.

  • net.inet.ip.mbuf_frag_size β€” causes ip_output() to fragment outgoing mbuf chains into fragments of the specified size. Setting this variable to 1 is an excellent way to test the long mbuf chain handling ability of network drivers 🫢
  • kern.ipc.m_defragrandomfailures β€” causes the function m_defrag() to randomly fail, returning NULL. Any piece of code which uses m_defrag() should be tested with this feature.

🌈 RETURN VALUES 🌈

Bestie, this one is literally: See above. πŸ’€

πŸ¦‹ SEE ALSO πŸ¦‹

  • ifnet(9)
  • mbuf_tags(9)
  • S. J. Leffler, W. N. Joy, R. S. Fabry, and M. J. Karels, β€œNetworking Implementation Notes”, 4.4BSD System Manager's Manual (SMM).

πŸŽ€ HISTORY πŸŽ€

Mbufs appeared in an early version of BSD. Besides being used for network packets, they were used to store various dynamic structures, such as routing table entries, interface addresses, protocol control blocks, etc. In more recent FreeBSD use of mbufs is almost entirely limited to packet storage, with uma(9) zones being used directly to store other network-related memory.

Historically, the mbuf allocator has been a special-purpose memory allocator able to run in interrupt contexts and allocating from a special kernel address space map. As of FreeBSD 5.3, the mbuf allocator is a wrapper around uma(9), allowing caching of mbufs, clusters, and mbuf + cluster pairs in per-CPU caches, as well as bringing other benefits of slab allocation.

πŸ‘‘ AUTHORS πŸ‘‘

The original mbuf manual page was written by Yar Tikhiy. The uma(9) mbuf allocator was written by Bosko Milekic.


πŸ’– That’s the whole vibe, bestie! Shoutout to Debian, dated January 20, 2026, and the page is MBUF(9). Stay iconic ✨

πŸ“¦ where this page comes from