MBUF(9)
πΈ 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 specifiedtype. note: itβs advisable to make sure thereβs enough contiguous data inmbuffirst β seem_pullup()for the details. β¨mtodo(mbuf, offset)β gives you a data pointer at anoffset(in bytes) into the data attached tombuf. Returns avoid *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,mbufpoints to the allocated mbuf; on failure, itβs set toNULL. Thehowargument is set toM_WAITOKorM_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. SeeMGET()for the details. πMEXTADD(mbuf, buf, size, free, opt_arg1, opt_arg2, flags, type)β associates externally managed data withmbuf. Any internal data already in the mbuf gets discarded, and theM_EXTflag gets set. Thebufandsizearguments are the address and length, respectively, of the data. Thefreeargument points to a function that will be called to free the data when the mbuf is freed; itβs only used iftypeisEXT_EXTREF. Theopt_arg1andopt_arg2arguments get saved in theext_arg1andext_arg2fields of thestruct m_extof the mbuf. Theflagsargument specifies additional mbuf flags; you donβt need to specifyM_EXT. Finally, thetypeargument 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 isEXT_EXTREF. π₯MCLGET(mbuf, how)β allocates and attaches an mbuf cluster tombuf. On success, you get a non-zero value back; otherwise itβs0. Historically, consumers would check for success by testing theM_EXTflag 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 pointermbuf->m_datato place an object of sizelenat the end of the internal data area ofmbuf, long word aligned. Applicable only ifmbufwas newly allocated withMGET()orm_get(). πΈMH_ALIGN(mbuf, len)β serves the same purpose asM_ALIGN(), but only for an mbuf newly allocated withMGETHDR()orm_gethdr(), or initialized bym_dup_pkthdr()orm_move_pkthdr(). π¦m_align(mbuf, len)β serves the same purpose asM_ALIGN()but handles any type of mbuf. versatile icon behavior. β¨M_LEADINGSPACE(mbuf)β returns the number of bytes available before the beginning of data inmbuf. πM_TRAILINGSPACE(mbuf)β returns the number of bytes available after the end of data inmbuf. βM_PREPEND(mbuf, len, how)β this macro operates on an mbuf chain. Itβs an optimized wrapper form_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 orNULLis inmbuf. πM_MOVE_PKTHDR(to, from)β using this macro is equivalent to callingm_move_pkthdr(to, from). periodt. πM_WRITABLE(mbuf)β this macro evaluates true ifmbufis not markedM_RDONLYand if eithermbufdoes not contain external storage or, if it does, then if the reference count of the storage is not greater than1. TheM_RDONLYflag can be set inmbuf->m_flags. This can be achieved during setup of the external storage by passing theM_RDONLYbit as aflagsargument to theMEXTADD()macro, or it can be directly set in individual mbufs. πMCHTYPE(mbuf, type)β changes the type ofmbuftotype. 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 ofMGET()for non-critical paths. πΈm_get2(size, how, type, flags)β allocates an mbuf with enough space to hold the specified amount of data. Ifsizeis larger thanMJUMPAGESIZE,NULLwill be returned. π«Άm_get3(size, how, type, flags)β allocates an mbuf with enough space to hold the specified amount of data. Ifsizeis larger thanMJUM16BYTES,NULLwill be returned. π¦m_getm(orig, len, how, type)β allocateslenbytes worth of mbufs and mbuf clusters if necessary, and appends the resulting allocated mbuf chain to the mbuf chainorig, if it is non-NULL. If the allocation fails at any point, it frees whatever was allocated and returnsNULL. Iforigis non-NULL, it will not be freed. Itβs possible to usem_getm()to either appendlenbytes 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 ofMGETHDR()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. ReturnsNULLon failure. πm_getjcl(how, type, flags, size)β this is likem_getcl()but the specified size of the cluster to be allocated must be one ofMCLBYTES,MJUMPAGESIZE,MJUM9BYTES, orMJUM16BYTES. π₯m_free(mbuf)β freesmbuf. Returnsm_nextof 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)β trimslenbytes from the head of an mbuf chain iflenis positive, from the tail otherwise. βm_append(mbuf, len, cp)β appendslenbytes of datacpto 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, handlingM_PKTHDRproperly. note: it does not allocate any mbuf clusters, solenmust be less thanMLENorMHLEN, depending on theM_PKTHDRflag setting. πm_copyup(mbuf, len, dstoff)β similar tom_pullup()but copieslenbytes of data into a new mbuf atdstoffbytes into the mbuf. Thedstoffargument aligns the data and leaves room for a link-layer header. Returns the new mbuf chain on success, and frees the mbuf chain and returnsNULLon failure. note: the function does not allocate mbuf clusters, solen + dstoffmust be less thanMHLEN. π«Άm_pullup(mbuf, len)β arranges that the firstlenbytes of an mbuf chain are contiguous and lay in the data area ofmbuf, so theyβre accessible withmtod(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,NULLon failure (the mbuf chain is freed in this case). note: it does not allocate any mbuf clusters, solenmust be less than or equal toMHLEN. πm_pulldown(mbuf, offset, len, offsetp)β arranges thatlenbytes betweenoffsetandoffset + lenin the mbuf chain are contiguous and lay in the data area ofmbuf, so theyβre accessible withmtod()ormtodo().lenmust 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. IfoffsetpisNULL, the region may be accessed usingmtod(mbuf, type)ormtodo(mbuf, 0). Ifoffsetpis non-NULL, the region may be accessed usingmtodo(mbuf, *offsetp). The region of the mbuf chain between its beginning andoffsetis not modified, so itβs safe to hold pointers to data within this region before callingm_pulldown(). πm_copym(mbuf, offset, len, how)β makes a copy of an mbuf chain startingoffsetbytes from the beginning, continuing forlenbytes. IflenisM_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 casem_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 ofm_copypacket()when you need a writable copy of an mbuf chain. π₯m_copydata(mbuf, offset, len, buf)β copies data from an mbuf chain startingoffsetbytes from the beginning, continuing forlenbytes, into the indicated bufferbuf. πΈm_copyback(mbuf, offset, len, buf)β copieslenbytes from the bufferbufback into the indicated mbuf chain, starting atoffsetbytes 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 setoffsetbeyond 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 mbuftowill contain an identical copy offrom->m_pkthdrand the per-packet attributes found in the mbuf chainfrom. The mbuffrommust have the flagM_PKTHDRinitially set, andtomust be empty on entry. πm_move_pkthdr(to, from)β okay so this one moves them_pkthdrand the per-packet attributes from the mbuf chainfromover to the mbufto.fromhas to come in with theM_PKTHDRflag initially set, andtomust be empty on entry. When the function finishes,fromwill have theM_PKTHDRflag 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 bybufinto an mbuf chain. The copy is done using the specified copy routinecopy, orbcopy()ifcopyisNULL.m_cat(m, n)β concatenatesntom. Both mbuf chains must be of the same type. Heads up:nis not guaranteed to be valid afterm_cat()returns. Also,m_cat()does not update any packet header fields or free mbuf tags, no cap.m_catpkt(m, n)β a variant ofm_cat()that operates on packets. Bothmandnmust contain packet headers.nis not guaranteed to be valid afterm_catpkt()returns.m_split(mbuf, len, how)β partitions an mbuf chain into two pieces and returns the tail: all but the firstlenbytes. If it fails, it returnsNULLand 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 offsetoff, for lengthlenbytes. Typically used to avoid calls tom_pullup()which would otherwise be unnecessary or undesirable.argis a convenience argument which is passed to the callback functionf.
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 atlocbytes 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,NULLwill be returned and the original chain will be unchanged. Upon success, the original chain will be freed and the new chain will be returned.howshould be eitherM_WAITOKorM_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 mostmaxfragsmbufs and clusters. If allocation fails or the chain cannot be collapsed as requested,NULLwill be returned, with the original chain possibly modified. As withm_defrag(),howshould be one ofM_WAITOKorM_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,NULLwill be returned. The original mbuf chain is always reclaimed and the reference count of any shared mbuf clusters is decremented.howshould be eitherM_WAITOKorM_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 withCSUM_IP_CHECKED.CSUM_DATA_VALIDβ the checksum of the data portion of the IP packet has been computed and stored in the fieldcsum_datain network byte order.CSUM_PSEUDO_HDRβ can be set only along withCSUM_DATA_VALIDto indicate that the IP data checksum found incsum_dataallows 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 tocsum_datato 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β causesip_output()to fragment outgoing mbuf chains into fragments of the specified size. Setting this variable to1is an excellent way to test the long mbuf chain handling ability of network drivers π«Άkern.ipc.m_defragrandomfailuresβ causes the functionm_defrag()to randomly fail, returningNULL. Any piece of code which usesm_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
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 2000 FreeBSD Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms