---
name: "MEXT_IS_REF"
section: "9"
platform: "linux"
package: "freebsd-manpages"
version: "6.0-5"
release: "Debian 4.0 (Etch)"
platforms: ["linux"]
url: "https://new.carta.tech/MEXT_IS_REF"
---

# MEXT\_IS\_REF(9)

memory management in the kernel IPC subsystem

## NAME

**mbuf** – memory management in the kernel IPC subsystem

## SYNOPSIS

**#include \<sys/param.h\>**

**#include \<sys/systm.h\>**

**#include \<sys/mbuf.h\>**

### Mbuf allocation macros

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

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

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

**MEXTADD**(*struct mbuf \*mbuf*, *caddr\_t buf*, *u\_int size*, *void (\*free)(void \*opt\_args)*, *void \*opt\_args*, *short flags*, *int type*);

**MEXTFREE**(*struct mbuf \*mbuf*);

**MEXT\_ADD\_REF**(*struct mbuf \*mbuf*);

**MEXT\_REM\_REF**(*struct mbuf \*mbuf*);

**MFREE**(*struct mbuf \*mbuf*, *struct mbuf \*successor*);

### Mbuf utility macros

**mtod**(*struct mbuf \*mbuf*, *type*);

*int*\

**MEXT\_IS\_REF**(*struct mbuf \*mbuf*);

**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*, *u\_int type*);

*int*\

**M\_WRITABLE**(*struct mbuf \*mbuf*);

### Mbuf allocation functions

*struct* *mbuf* *\**\

**m\_get**(*int how*, *int type*);

*struct* *mbuf* *\**\

**m\_getm**(*struct mbuf \*orig*, *int len*, *int how*, *int type*);

*struct* *mbuf* *\**\

**m\_getcl**(*int how*, *short type*, *int flags*);

*struct* *mbuf* *\**\

**m\_getclr**(*int how*, *int type*);

*struct* *mbuf* *\**\

**m\_gethdr**(*int how*, *int type*);

*struct* *mbuf* *\**\

**m\_free**(*struct mbuf \*mbuf*);

*void*\

**m\_freem**(*struct mbuf \*mbuf*);

### Mbuf utility functions

*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\_copym**(*struct mbuf \*mbuf*, *int offset*, *int len*, *int how*);

*struct* *mbuf* *\**\

**m\_copypacket**(*struct mbuf \*mbuf*, *int how*);

*struct* *mbuf* *\**\

**m\_dup**(*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*);

*u\_int*\

**m\_fixhdr**(*struct mbuf \*mbuf*);

*void*\

**m\_dup\_pkthdr**(*struct mbuf \*to*, *struct mbuf \*from*);

*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*);

## DESCRIPTION

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.

While a developer should not bother with *mbuf* internals without serious reason in order to avoid incompatibilities with future changes, 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:

```
/* mbuf flags */
#define	M_EXT		0x0001	/* has associated external storage */
#define	M_PKTHDR	0x0002	/* start of record */
#define	M_EOR		0x0004	/* end of record */
#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 */
#define	M_PROTO6	0x4000	/* protocol-specific (avoid M_BCAST conflict) */
#define	M_FREELIST	0x8000	/* mbuf is on the free list */
/* mbuf pkthdr flags (also stored in m_flags) */
#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 */
```

The available *mbuf* types are defined as follows:

```
/* mbuf types */
#define	MT_DATA		1	/* dynamic (data) allocation */
#define	MT_HEADER	2	/* packet header */
#define	MT_SONAME	8	/* socket name */
#define	MT_FTABLE	11	/* fragment reassembly header */
#define	MT_CONTROL	14	/* extra-data protocol message */
#define	MT_OOBDATA	15	/* expedited data */
```

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)](/mbuf_tags/9.md) 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.

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. The developer can check whether the reference count for the external storage of a given *mbuf* is greater than 1 with the MEXT\_IS\_REF macro. Similarly, the developer can directly add and remove references, if absolutely necessary, with the use of the MEXT\_ADD\_REF and MEXT\_REM\_REF macros.

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 the sum of MLEN and MHLEN. 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.

- **mtod**(*mbuf*, *type*)

  Convert an *mbuf* pointer to a data pointer. The macro expands to the data pointer cast to the pointer of the specified *type*. **Note**: It is advisable to ensure that there is enough contiguous data in *mbuf*. See **m\_pullup**() for details.

- **MGET**(*mbuf*, *how*, *type*)

  Allocate an *mbuf* and initialize it to contain internal data. *mbuf* will point to the allocated *mbuf* on success, or be set to NULL on failure. The *how* argument is to be set to M\_TRYWAIT or M\_DONTWAIT. It specifies whether the caller is willing to block if necessary. If *how* is set to M\_TRYWAIT, a failed allocation will result in the caller being put to sleep for a designated kern.ipc.mbuf\_wait ((sysctl) 8 tunable) number of ticks. A number of other functions and macros related to *mbufs* have the same argument because they may at some point need to allocate new *mbufs*.

  Programmers should be careful not to confuse the *mbuf* allocation flag M\_DONTWAIT with the [malloc(9)](/malloc/9.md) allocation flag, M\_NOWAIT. They are not the same.

- **MGETHDR**(*mbuf*, *how*, *type*)

  Allocate an *mbuf* and initialize it to contain a packet header and internal data. See **MGET**() for details.

- **MCLGET**(*mbuf*, *how*)

  Allocate and attach an *mbuf* *cluster* to *mbuf*. If the macro fails, the M\_EXT flag will not be set in *mbuf*.

- **M\_ALIGN**(*mbuf*, *len*)

  Set the pointer *mbuf-\>m\_data* to place an object of the size *len* at the end of the internal data area of *mbuf*, long word aligned. Applicable only if *mbuf* is newly allocated with **MGET**() or **m\_get**().

- **MH\_ALIGN**(*mbuf*, *len*)

  Serves the same purpose as **M\_ALIGN**() does, but only for *mbuf* newly allocated with **MGETHDR**() or **m\_gethdr**(), or initialized by **m\_dup\_pkthdr**() or **m\_move\_pkthdr**().

- **m\_align**(*mbuf*, *len*)

  Services the same purpose as **M\_ALIGN**() but handles any type of mbuf.

- **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 is 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). The new *mbuf* *chain* pointer or NULL is in *mbuf* after the call.

- **M\_MOVE\_PKTHDR**(*to*, *from*)

  Using this macro is equivalent to calling **m\_move\_pkthdr**(*to*, *from*).

- **M\_WRITABLE**(*mbuf*)

  This macro will evaluate 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 can be directly set in individual *mbufs*.

- **MCHTYPE**(*mbuf*, *type*)

  Change the type of *mbuf* to *type*. This is a relatively expensive operation and should be avoided.

The functions are:

- **m\_get**(*how*, *type*)

  A function version of **MGET**() for non-critical paths.

- **m\_getm**(*orig*, *len*, *how*, *type*)

  Allocate *len* bytes worth of *mbufs* and *mbuf* *clusters* if necessary and append the resulting allocated *mbuf* *chain* to the *mbuf* *chain* *orig*, if it is non-NULL. If the allocation fails at any point, free whatever was allocated and return NULL. If *orig* is non-NULL, it will not be freed. It is 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*)

  Fetch an *mbuf* with a *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\_getclr**(*how*, *type*)

  Allocate an *mbuf* and zero out the data region.

- **m\_free**(*mbuf*)

  Frees *mbuf*. Returns *m\_next* of the freed *mbuf*.

The functions below operate on *mbuf* *chains*.

- **m\_freem**(*mbuf*)

  Free an entire *mbuf* *chain*, including any external storage.

- **m\_adj**(*mbuf*, *len*)

  Trim *len* bytes from the head of an *mbuf* *chain* if *len* is positive, from the tail otherwise.

- **m\_append**(*mbuf*, *len*, *cp*)

  Append *len* bytes of data *cp* to the *mbuf* *chain*. Extend the mbuf chain if the new data does not fit in existing space.

- **m\_prepend**(*mbuf*, *len*, *how*)

  Allocate a new *mbuf* and prepend it to the *mbuf* *chain*, handle 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*)

  Arrange that the first *len* bytes of an *mbuf* *chain* are contiguous and lay in the data area of *mbuf*, so they are accessible with **mtod**(*mbuf*, *type*). Return 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 MHLEN.

- **m\_copym**(*mbuf*, *offset*, *len*, *how*)

  Make a copy of an *mbuf* *chain* starting *offset* bytes from the beginning, continuing for *len* bytes. If *len* is M\_COPYALL, copy 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*)

  Copy an entire packet including 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*)

  Copy 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*)

  Copy data from an *mbuf* *chain* starting *off* bytes from the beginning, continuing for *len* bytes, into the indicated buffer *buf*.

- **m\_copyback**(*mbuf*, *offset*, *len*, *buf*)

  Copy *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*)

  Return the length of the *mbuf* *chain*, and optionally a pointer to the last *mbuf*.

- **m\_dup\_pkthdr**(*to*, *from*, *how*)

  Upon the function's completion, 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*)

  Move *m\_pkthdr* and the per-packet attributes from the *mbuf* *chain* *from* to the *mbuf* *to*. The *mbuf* *from* must have the flag M\_PKTHDR initially set, and *to* must be empty on entry. Upon the function's completion, *from* will have the flag M\_PKTHDR and the per-packet attributes cleared.

- **m\_fixhdr**(*mbuf*)

  Set the packet-header length to the length of the *mbuf* *chain*.

- **m\_devget**(*buf*, *len*, *offset*, *ifp*, *copy*)

  Copy data from a device local memory pointed to by *buf* to an *mbuf* *chain*. The copy is done using a specified copy routine *copy*, or **bcopy**() if *copy* is NULL.

- **m\_cat**(*m*, *n*)

  Concatenate *n* to *m*. Both *mbuf* *chains* must be of the same type. *N* is still valid after the function returned. **Note**: It does not handle M\_PKTHDR and friends.

- **m\_split**(*mbuf*, *len*, *how*)

  Partition an *mbuf* *chain* in two pieces, returning the tail: all but the first *len* bytes. In case of failure, it returns NULL and attempts to restore the *mbuf* *chain* to its original state.

- **m\_apply**(*mbuf*, *off*, *len*, *f*, *arg*)

  Apply 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*)

  Return 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*)

  Defragment 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\_TRYWAIT or M\_DONTWAIT, 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.

## 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 *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 the direction a packet flows in, and 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.

On output, checksum offloading is attempted after the outgoing interface has been determined for a packet. The interface-specific field *ifnet.if\_data.ifi\_hwassist* (see [ifnet(9)](/ifnet/9.md)) is consulted for the capabilities of the interface 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\_TCP

  The TCP checksum is to be computed. (See below.)

- CSUM\_UDP

  The UDP checksum is to be computed. (See below.)

Should a 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 this case, the checksum field will be initially set by the TCP/IP module to the checksum of the pseudo header defined by the TCP and UDP specifications.

For outbound packets which have been fragmented by the host CPU, the following will also be true, regardless of the checksum flag settings:

- all fragments will have the flag M\_FRAG set in their *m\_flags* field;

- the first and the last fragments in the chain will have M\_FIRSTFRAG or M\_LASTFRAG set in their *m\_flags*, correspondingly;

- the first fragment in the chain will have the total number of fragments contained in its *csum\_data* field.

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 CSUM\_IP\_FRAGS is set in the field *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 *csum\_data* contains a fragment count instead of a checksum offset value.

On input, an interface 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 TCP or UDP checksum validation without returning the exact value of the checksum to the host CPU, its driver can mark CSUM\_DATA\_VALID and CSUM\_PSEUDO\_HDR in *csum\_flags*, 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.

For inbound packets which are IP fragments, all *csum\_data* fields will be summed during reassembly to obtain the final checksum value passed to an upper layer in the *csum\_data* field of the reassembled packet. The *csum\_flags* fields of all fragments will be consolidated using logical AND to obtain the final value for *csum\_flags*. Thus, in order to successfully offload checksum computation for fragmented data, all fragments should have the same value of *csum\_flags*.

## STRESS TESTING

When running a kernel compiled with the option MBUF\_STRESS\_TEST, the following[sysctl(8)](/sysctl/8.md) -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

See above.

## SEE ALSO

[ifnet(9)](/ifnet/9.md), [mbuf\_tags(9)](/mbuf_tags/9.md)

## 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.

## AUTHORS

The original **mbuf** manual page was written by Yar Tikhiy.
