CNDP
22.08.0
|
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <bsd/string.h>
#include <cne_atomic.h>
#include <cne_common.h>
#include <cne_branch_prediction.h>
#include <cne_log.h>
#include <cne_mmap.h>
#include <cne_prefetch.h>
#include <mempool.h>
#include "pktmbuf_ops.h"
#include "pktmbuf_offload.h"
Go to the source code of this file.
Data Structures | |
struct | pktmbuf_info_s |
struct | pktmbuf_s |
struct | pktmbuf_pending |
Macros | |
#define | CNE_MBUF_DEFAULT_DATAROOM (2 * 1024) |
#define | pktmbuf_hash(m) ((m)->hash) |
#define | pktmbuf_pooldata(m) ((m)->pooldata) |
#define | pktmbuf_buf_addr(m) ((m)->buf_addr) |
#define | pktmbuf_port(m) ((m)->lport) |
#define | pktmbuf_buf_len(m) ((m)->buf_len) |
#define | pktmbuf_tx_offload(m) ((m)->tx_offload) |
#define | pktmbuf_userptr(m) ((m)->userptr) |
#define | pktmbuf_udata64(m) ((m)->udata64) |
#define | pktmbuf_data_len(m) ((m)->data_len) |
#define | pktmbuf_data_off(m) ((m)->data_off) |
#define | pktmbuf_refcnt(m) ((m)->refcnt) |
#define | pktmbuf_mtod_offset(m, t, o) ((t)((char *)pktmbuf_buf_addr(m) + pktmbuf_data_off(m) + (o))) |
#define | pktmbuf_mtod(m, t) pktmbuf_mtod_offset(m, t, 0) |
#define | pktmbuf_meta_index(m) ((m)->meta_index) |
#define | __pktmbuf_sanity_check(m, is_h) do { } while (0) |
#define | CNE_MBUF_PREFETCH_TO_FREE(m) |
#define | CNE_MBUF_INVALID_PORT UINT16_MAX |
Typedefs | |
typedef struct pktmbuf_info_s | pktmbuf_info_t |
typedef struct pktmbuf_pending | pktmbuf_pending_t |
typedef int(* | pktmbuf_cb_t) (pktmbuf_info_t *pi, pktmbuf_t *buf, uint32_t sz, uint32_t idx, void *ud) |
Enumerations | |
enum | { DEFAULT_BURST_SIZE = 64 , DEFAULT_MBUF_COUNT = (16 * 1024) , DEFAULT_MBUF_SIZE = 2048 , PKTMBUF_PENDING_SZ = 128 , PKTMBUF_INFO_NAME_SZ = 24 } |
Functions | |
CNDP_API void | pktmbuf_destroy (pktmbuf_info_t *pi) |
CNDP_API pktmbuf_info_t * | pktmbuf_pool_create (char *addr, uint32_t bufcnt, uint32_t bufsz, uint32_t cache_sz, mbuf_ops_t *ops) |
CNDP_API pktmbuf_info_t * | pktmbuf_pool_cfg_create (const pktmbuf_pool_cfg_t *cfg) |
CNDP_API int | pktmbuf_iterate (pktmbuf_info_t *pi, pktmbuf_cb_t cb, void *ud) |
static void | pktmbuf_prefetch (pktmbuf_t *m) |
static char * | pktmbuf_data_addr_default (pktmbuf_t *mb) |
static uint16_t | pktmbuf_refcnt_read (const pktmbuf_t *m) |
static void | pktmbuf_refcnt_set (pktmbuf_t *m, uint16_t new_value) |
static uint16_t | pktmbuf_refcnt_update (pktmbuf_t *m, int16_t value) |
CNDP_API void | pktmbuf_sanity_check (const pktmbuf_t *m, int is_header) |
CNDP_API int | pktmbuf_check (const pktmbuf_t *m, int is_header, const char **reason) |
static void | pktmbuf_info_name_set (pktmbuf_info_t *pi, const char *str) |
static const char * | pktmbuf_info_name_get (pktmbuf_info_t *pi) |
static void | pktmbuf_reset_headroom (pktmbuf_t *m) |
static void | pktmbuf_reset (pktmbuf_t *m) |
static int | pktmbuf_alloc_bulk (pktmbuf_info_t *pi, pktmbuf_t **mbufs, unsigned count) |
static pktmbuf_t * | pktmbuf_alloc (pktmbuf_info_t *pi) |
static __cne_always_inline pktmbuf_t * | pktmbuf_refcnt_free (pktmbuf_t *m) |
static __cne_always_inline void | pktmbuf_free (pktmbuf_t *m) |
static void | pktmbuf_free_bulk (pktmbuf_t **mbufs, unsigned int count) |
CNDP_API pktmbuf_t * | pktmbuf_copy (const pktmbuf_t *m, pktmbuf_info_t *pi, uint32_t offset, uint32_t length) |
static uint16_t | pktmbuf_headroom (const pktmbuf_t *m) |
static uint16_t | pktmbuf_tailroom (const pktmbuf_t *m) |
static char * | pktmbuf_mtod_last (const pktmbuf_t *m) |
static char * | pktmbuf_mtod_end (const pktmbuf_t *m) |
static char * | pktmbuf_prepend (pktmbuf_t *m, uint16_t len) |
static char * | pktmbuf_adj_offset (pktmbuf_t *m, int16_t len) |
static char * | pktmbuf_append (pktmbuf_t *m, uint16_t len) |
static int | pktmbuf_trim (pktmbuf_t *m, uint16_t len) |
static const void * | pktmbuf_read (const pktmbuf_t *m, uint32_t off, uint32_t len, void *buf) |
static const void * | pktmbuf_write (const void *buf, uint32_t len, pktmbuf_t *m, uint32_t off) |
static uint16_t | pktmbuf_data_room_size (mempool_t *mp) |
CNDP_API pktmbuf_t * | pktmbuf_clone (pktmbuf_t *md, pktmbuf_info_t *pi) |
CNDP_API void | pktmbuf_dump (const char *msg, const pktmbuf_t *m, unsigned dump_len) |
const char * | cne_get_rx_ol_flag_name (uint64_t mask) |
int | cne_get_rx_ol_flag_list (uint64_t mask, char *buf, size_t buflen) |
const char * | cne_get_tx_ol_flag_name (uint64_t mask) |
int | cne_get_tx_ol_flag_list (uint64_t mask, char *buf, size_t buflen) |
static void * | pktmbuf_metadata (const pktmbuf_t *m) |
static int32_t | pktmbuf_metadata_bufsz (const pktmbuf_t *m) |
CNDP_API int | pktmbuf_pool_cfg (pktmbuf_pool_cfg_t *c, void *addr, uint32_t bufcnt, uint32_t bufsz, uint32_t cache_sz, void *metadata, uint32_t metadata_bufsz, mbuf_ops_t *ops) |
CNDP_API void | pktmbuf_info_dump (void) |
Pktmbuf
The mbuf library provides the ability to create and destroy buffers that may be used by the CNE application to store message buffers. The message buffers are stored in a mempool, using the CNE mempool library.
The preferred way to create a mbuf pool is to use pktmbuf_pool_create(). However, in some situations, an application may want to have more control (ex: populate the pool with specific memory), in this case it is possible to use functions from mempool. See how pktmbuf_pool_create() is implemented for details.
This library provides an API to allocate/free packet mbufs, which are used to carry network packets.
To understand the concepts of packet buffers or mbufs, you should read "TCP/IP Illustrated, Volume 2: The Implementation, Addison-Wesley, 1995, ISBN 0-201-63354-X from Richard Stevens" http://www.kohala.com/start/tcpipiv2.html
Definition in file pktmbuf.h.
#define CNE_MBUF_DEFAULT_DATAROOM (2 * 1024) |
#define pktmbuf_hash | ( | m | ) | ((m)->hash) |
#define pktmbuf_pooldata | ( | m | ) | ((m)->pooldata) |
#define pktmbuf_buf_addr | ( | m | ) | ((m)->buf_addr) |
#define pktmbuf_port | ( | m | ) | ((m)->lport) |
#define pktmbuf_buf_len | ( | m | ) | ((m)->buf_len) |
#define pktmbuf_tx_offload | ( | m | ) | ((m)->tx_offload) |
#define pktmbuf_userptr | ( | m | ) | ((m)->userptr) |
#define pktmbuf_udata64 | ( | m | ) | ((m)->udata64) |
#define pktmbuf_data_len | ( | m | ) | ((m)->data_len) |
A macro that returns the length of the segment.
m | The packet mbuf. |
#define pktmbuf_data_off | ( | m | ) | ((m)->data_off) |
#define pktmbuf_refcnt | ( | m | ) | ((m)->refcnt) |
#define pktmbuf_mtod_offset | ( | m, | |
t, | |||
o | |||
) | ((t)((char *)pktmbuf_buf_addr(m) + pktmbuf_data_off(m) + (o))) |
A macro that points to an offset into the data in the mbuf.
The returned pointer is cast to type t. Before using this function, the user must ensure that the first segment is large enough to accommodate its data.
m | The packet mbuf. |
o | The offset into the mbuf data. |
t | The type to cast the result into. |
#define pktmbuf_mtod | ( | m, | |
t | |||
) | pktmbuf_mtod_offset(m, t, 0) |
A macro that points to the start of the data in the mbuf.
The returned pointer is cast to type t. Before using this function, the user must ensure that the segment is large enough to accommodate its data.
m | The packet mbuf. |
t | The type to cast the result into. |
#define pktmbuf_meta_index | ( | m | ) | ((m)->meta_index) |
#define __pktmbuf_sanity_check | ( | m, | |
is_h | |||
) | do { } while (0) |
#define CNE_MBUF_PREFETCH_TO_FREE | ( | m | ) |
#define CNE_MBUF_INVALID_PORT UINT16_MAX |
typedef struct pktmbuf_info_s pktmbuf_info_t |
Information structure for pktmbuf buffer and related information.
typedef struct pktmbuf_pending pktmbuf_pending_t |
Structure to help with bulk free of mbufs.
As the pktmbuf_free_bulk is called this structure holds all of the mbufs allocated from the same pool. When a different pool is found in the array of mbufs pointers they are freed and the set of mbufs to a given pool is created.
typedef int(* pktmbuf_cb_t) (pktmbuf_info_t *pi, pktmbuf_t *buf, uint32_t sz, uint32_t idx, void *ud) |
Object or pktmbuf callback routine to initialize the allocated pktmbuf_t structure.
pi | The pktmbuf_info_t pointer |
buf | The buffer pointer to pktmbuf_t structure to initialize |
sz | The size of the buffer to use in initialing the buffer, if needed. |
idx | The index of the pktmbuf structure |
ud | User data pointer supplied by the caller |
anonymous enum |
CNDP_API void pktmbuf_destroy | ( | pktmbuf_info_t * | pi | ) |
Destroy the pktmbuf_info_t structure
pi | Free the data and pktmbuf_info_t structure data. |
CNDP_API pktmbuf_info_t* pktmbuf_pool_create | ( | char * | addr, |
uint32_t | bufcnt, | ||
uint32_t | bufsz, | ||
uint32_t | cache_sz, | ||
mbuf_ops_t * | ops | ||
) |
Create the pktmbuf_info_t structure with no external metadata information.
addr | The starting address of the buffer space |
bufcnt | Number of buffers to create or allocate |
bufsz | The size of each buffer to be allocated |
cache_sz | Cache size for mempool or other user needs, can be zero for no cache. |
ops | Pointer to mbuf_ops_t structure for mbuf operator function pointers. This structure is copied into the pktmbuf_info_t.ops structure and can be NULL. |
CNDP_API pktmbuf_info_t* pktmbuf_pool_cfg_create | ( | const pktmbuf_pool_cfg_t * | cfg | ) |
Create the pktmbuf_info_t structure and setup some of the fields
cfg | Pointer to a configuration structure. addr - The starting address of the buffer space bufcnt - Number of buffers to create or allocate, using the mtype for mmap_alloc() bufsz - The size of each buffer to be allocated cache_sz - Cache size for mempool or other user needs ops - Pointer to mbuf_ops_t structure for mbuf operator function pointers. This structure is copied into the pktmbuf_info_t.ops structure and can be NULL. metadata_bufsz - is the size of the external metadata buffers. metadata - is a pointer to the start of the metadata, can be NULL for no external metadata. |
CNDP_API int pktmbuf_iterate | ( | pktmbuf_info_t * | pi, |
pktmbuf_cb_t | cb, | ||
void * | ud | ||
) |
Iterate over the set of buffers while calling the supplied callback function.
pi | Pointer to pktmbuf_info_t structure, returned from pktmbuf_pool_init(). |
cb | The callback routine to call to initialize the buffer. The callback uses the pktmbuf_cb_t prototype. |
ud | The user data pointer for the callback function |
|
inlinestatic |
Prefetch the first part of the mbuf
The first 64 bytes of the mbuf corresponds to fields that are used early in the receive path. If the cache line of the architecture is higher than 64B, the second part will also be prefetched.
m | The pointer to the mbuf. |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
CNDP_API void pktmbuf_sanity_check | ( | const pktmbuf_t * | m, |
int | is_header | ||
) |
Sanity checks on an mbuf.
Check the consistency of the given mbuf. The function will cause a panic if corruption is detected.
m | The mbuf to be checked. |
is_header | True if the mbuf is a packet header, false if it is a sub-segment of a packet (in this case, some fields like nb_segs are not checked) |
CNDP_API int pktmbuf_check | ( | const pktmbuf_t * | m, |
int | is_header, | ||
const char ** | reason | ||
) |
Sanity checks on a mbuf.
Almost like pktmbuf_sanity_check(), but this function gives the reason if corruption is detected rather than panic.
m | The mbuf to be checked. |
is_header | True if the mbuf is a packet header, false if it is a sub-segment of a packet (in this case, some fields like nb_segs are not checked) |
reason | A reference to a string pointer where to store the reason why a mbuf is considered invalid. |
|
inlinestatic |
Set the pktmbuf_info_t.name field with given string, does not need to be unique.
The string passed will be copied into the field. If pi
is NULL return without setting the name field.
pi | The pktmbuf_info_t pointer to set the name field. |
str | The string to copy into the name field, can be NULL and the string will cleared. |
|
inlinestatic |
Get the pktmbuf_info_t.name field.
pi | The pktmbuf_info_t pointer to set the name field. |
pi
is NULL or pointer to pktmbuf_info_t.name field which could be empty.
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
Allocate a bulk of mbufs, initialize refcnt and reset the fields to default values.
pi | The mempool from which mbufs are allocated. |
mbufs | Array of pointers to mbufs |
count | Array size |
|
inlinestatic |
Allocate a new mbuf from a mempool.
This new mbuf, which has a length of 0. The pointer to data is initialized to have some bytes of headroom in the buffer (if buffer size allows).
pi | The mempool from which the mbuf is allocated. |
|
static |
Decrease reference counter of an mbuf
This function does the same thing as a free. It decreases the reference counter, and if it reaches 0 it is freed.
m | The mbuf to be unlinked |
|
static |
Free a packet mbuf back into its original pool.
m | The packet mbuf to be freed. If NULL, the function does nothing. |
|
inlinestatic |
Free a bulk of packet mbufs back into their original pools.
mbufs | Array of pointers to packet mbufs. The array may contain NULL pointers. |
count | Array size. |
CNDP_API pktmbuf_t* pktmbuf_copy | ( | const pktmbuf_t * | m, |
pktmbuf_info_t * | pi, | ||
uint32_t | offset, | ||
uint32_t | length | ||
) |
Create a full copy of a given packet mbuf.
Copies all the data from a given packet mbuf to a newly allocated set of mbufs. The private data are is not copied.
m | The packet mbuf to be copiedd. |
pi | The pktmbuf_info_t from which the "clone" mbufs are allocated. |
offset | The number of bytes to skip before copying. If the mbuf does not have that many bytes, it is an error and NULL is returned. |
length | The upper limit on bytes to copy. Passing UINT32_MAX means all data (after offset). |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
Prepend len bytes to an mbuf data area.
Returns a pointer to the new data start address. If there is not enough headroom in the mbuf, the function will return NULL, without modifying the mbuf.
m | The pkt mbuf. |
len | The amount of data to prepend (in bytes). |
|
inlinestatic |
Remove len bytes of an mbuf, by increasing mbuf.data_off value
Returns a pointer to the new data start address. If the request length to move the data_off exceeds the mbuf len then return NULL or return the new start address.
m | The pkt mbuf. |
len | The amount of data to remove/added in bytes from the start of the mbuf. |
|
inlinestatic |
Append len bytes to an mbuf.
Append len bytes to an mbuf and return a pointer to the start address of the added data.
m | The packet mbuf. |
len | The amount of data to append (in bytes). |
|
inlinestatic |
Remove len bytes of data at the end of the mbuf.
If the length is greater than the length of the segment, the function will fail and return -1 without modifying the mbuf.
m | The packet mbuf. |
len | The amount of data to remove (in bytes). |
|
inlinestatic |
Read len data bytes in a mbuf at specified offset.
Copy the data in the buffer provided by the user and return its pointer.
m | The pointer to the mbuf. |
off | The offset of the data in the mbuf. |
len | The amount of bytes to read. |
buf | The buffer where data is copied in mbuf data. Its length should be at least equal to the len parameter. |
|
inlinestatic |
Write len data bytes into the mbuf at specified offset.
Copy the data in the buffer provided by the user into the mbuf.
buf | The buffer where data is copied to in mbuf buffer. Its length should be at least equal to the len parameter. |
len | The amount of bytes to write from buffer to mbuf. |
m | The pointer to the mbuf. |
off | The offset of the data in the mbuf. |
|
inlinestatic |
Amount of total data space available for packet data.
This value is the max space available for packet data in a pktmbuf_t, which means the value for a 2K buffer would be (2K - sizeof(pktmbuf_t)).
mp | The mempool pointer to get the element size. |
CNDP_API pktmbuf_t* pktmbuf_clone | ( | pktmbuf_t * | md, |
pktmbuf_info_t * | pi | ||
) |
Clone a pktmbuf from a given pool.
md | The mbuf to clone |
pi | The pktmbuf_info pointer to allocate the new mbuf from. |
CNDP_API void pktmbuf_dump | ( | const char * | msg, |
const pktmbuf_t * | m, | ||
unsigned | dump_len | ||
) |
Dump an mbuf structure to a file.
Dump all fields for the given packet mbuf and all its associated segments (in the case of a chained buffer).
msg | Title message, can be NULL |
m | The packet mbuf. |
dump_len | If dump_len != 0, also dump the "dump_len" first data bytes of the packet. |
const char* cne_get_rx_ol_flag_name | ( | uint64_t | mask | ) |
Get the name of a RX offload flag
mask | The mask describing the flag. |
int cne_get_rx_ol_flag_list | ( | uint64_t | mask, |
char * | buf, | ||
size_t | buflen | ||
) |
Dump the list of RX offload flags in a buffer
mask | The mask describing the RX flags. |
buf | The output buffer. |
buflen | The length of the buffer. |
const char* cne_get_tx_ol_flag_name | ( | uint64_t | mask | ) |
Get the name of a TX offload flag
mask | The mask describing the flag. Usually only one bit must be set. Several bits can be given if they belong to the same mask. Ex: PKT_TX_L4_MASK. |
int cne_get_tx_ol_flag_list | ( | uint64_t | mask, |
char * | buf, | ||
size_t | buflen | ||
) |
Dump the list of TX offload flags in a buffer
mask | The mask describing the TX flags. |
buf | The output buffer. |
buflen | The length of the buffer. |
|
inlinestatic |
Return the metadata buffer address
m | The pktmbuf_t structure pointer |
|
inlinestatic |
CNDP_API int pktmbuf_pool_cfg | ( | pktmbuf_pool_cfg_t * | c, |
void * | addr, | ||
uint32_t | bufcnt, | ||
uint32_t | bufsz, | ||
uint32_t | cache_sz, | ||
void * | metadata, | ||
uint32_t | metadata_bufsz, | ||
mbuf_ops_t * | ops | ||
) |
Construct a pktmbuf_pool_cfg_t structure with the given parameters.
c | Pointer to the pktmbuf_pool_cfg_t structure |
addr | Address of the pktmbuf buffers, must not be NULL |
bufcnt | Number of buffers in the pktmbuf pool, can be zero to use default |
bufsz | Size of each buffer in the pktmbuf pool, can be zero to use default |
cache_sz | Cache size of the mempool to use in allocate/free buffers, can be zero |
metadata | The pointer of the metadata buffer pool, can be NULL. |
metadata_bufsz | The size of each metadata buffer in the pool, if metadata is valid can not be zero and needs to be a multiple of a cacheline. |
ops | The pktmbuf ops structure pointer, can be NULL. |
CNDP_API void pktmbuf_info_dump | ( | void | ) |
Dump out the list of pktmbuf_info_t structures