Zephyr API Documentation 4.0.0
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
Network Packet Library

Network packet management library . More...

Data Structures

struct  net_pkt
 Network packet. More...
 

Macros

#define NET_PKT_SLAB_DEFINE(name, count)
 Create a net_pkt slab.
 
#define NET_PKT_DATA_POOL_DEFINE(name, count)
 Create a data fragment net_buf pool.
 
#define net_pkt_print_frags(pkt)
 

Functions

struct net_bufnet_pkt_get_reserve_data (struct net_buf_pool *pool, size_t min_len, k_timeout_t timeout)
 Get a data buffer from a given pool.
 
struct net_bufnet_pkt_get_reserve_rx_data (size_t min_len, k_timeout_t timeout)
 Get RX DATA buffer from pool.
 
struct net_bufnet_pkt_get_reserve_tx_data (size_t min_len, k_timeout_t timeout)
 Get TX DATA buffer from pool.
 
struct net_bufnet_pkt_get_frag (struct net_pkt *pkt, size_t min_len, k_timeout_t timeout)
 Get a data fragment that might be from user specific buffer pool or from global DATA pool.
 
void net_pkt_unref (struct net_pkt *pkt)
 Place packet back into the available packets slab.
 
struct net_pktnet_pkt_ref (struct net_pkt *pkt)
 Increase the packet ref count.
 
struct net_bufnet_pkt_frag_ref (struct net_buf *frag)
 Increase the packet fragment ref count.
 
void net_pkt_frag_unref (struct net_buf *frag)
 Decrease the packet fragment ref count.
 
struct net_bufnet_pkt_frag_del (struct net_pkt *pkt, struct net_buf *parent, struct net_buf *frag)
 Delete existing fragment from a packet.
 
void net_pkt_frag_add (struct net_pkt *pkt, struct net_buf *frag)
 Add a fragment to a packet at the end of its fragment list.
 
void net_pkt_frag_insert (struct net_pkt *pkt, struct net_buf *frag)
 Insert a fragment to a packet at the beginning of its fragment list.
 
void net_pkt_compact (struct net_pkt *pkt)
 Compact the fragment list of a packet.
 
void net_pkt_get_info (struct k_mem_slab **rx, struct k_mem_slab **tx, struct net_buf_pool **rx_data, struct net_buf_pool **tx_data)
 Get information about predefined RX, TX and DATA pools.
 
struct net_pktnet_pkt_alloc (k_timeout_t timeout)
 Allocate an initialized net_pkt.
 
struct net_pktnet_pkt_alloc_from_slab (struct k_mem_slab *slab, k_timeout_t timeout)
 Allocate an initialized net_pkt from a specific slab.
 
struct net_pktnet_pkt_rx_alloc (k_timeout_t timeout)
 Allocate an initialized net_pkt for RX.
 
struct net_pktnet_pkt_alloc_on_iface (struct net_if *iface, k_timeout_t timeout)
 Allocate a network packet for a specific network interface.
 
int net_pkt_alloc_buffer (struct net_pkt *pkt, size_t size, enum net_ip_protocol proto, k_timeout_t timeout)
 Allocate buffer for a net_pkt.
 
int net_pkt_alloc_buffer_raw (struct net_pkt *pkt, size_t size, k_timeout_t timeout)
 Allocate buffer for a net_pkt, of specified size, w/o any additional preconditions.
 
struct net_pktnet_pkt_alloc_with_buffer (struct net_if *iface, size_t size, sa_family_t family, enum net_ip_protocol proto, k_timeout_t timeout)
 Allocate a network packet and buffer at once.
 
void net_pkt_append_buffer (struct net_pkt *pkt, struct net_buf *buffer)
 Append a buffer in packet.
 
size_t net_pkt_available_buffer (struct net_pkt *pkt)
 Get available buffer space from a pkt.
 
size_t net_pkt_available_payload_buffer (struct net_pkt *pkt, enum net_ip_protocol proto)
 Get available buffer space for payload from a pkt.
 
void net_pkt_trim_buffer (struct net_pkt *pkt)
 Trim net_pkt buffer.
 
int net_pkt_remove_tail (struct net_pkt *pkt, size_t length)
 Remove length bytes from tail of packet.
 
void net_pkt_cursor_init (struct net_pkt *pkt)
 Initialize net_pkt cursor.
 
static void net_pkt_cursor_backup (struct net_pkt *pkt, struct net_pkt_cursor *backup)
 Backup net_pkt cursor.
 
static void net_pkt_cursor_restore (struct net_pkt *pkt, struct net_pkt_cursor *backup)
 Restore net_pkt cursor from a backup.
 
static void * net_pkt_cursor_get_pos (struct net_pkt *pkt)
 Returns current position of the cursor.
 
int net_pkt_skip (struct net_pkt *pkt, size_t length)
 Skip some data from a net_pkt.
 
int net_pkt_memset (struct net_pkt *pkt, int byte, size_t length)
 Memset some data in a net_pkt.
 
int net_pkt_copy (struct net_pkt *pkt_dst, struct net_pkt *pkt_src, size_t length)
 Copy data from a packet into another one.
 
struct net_pktnet_pkt_clone (struct net_pkt *pkt, k_timeout_t timeout)
 Clone pkt and its buffer.
 
struct net_pktnet_pkt_rx_clone (struct net_pkt *pkt, k_timeout_t timeout)
 Clone pkt and its buffer.
 
struct net_pktnet_pkt_shallow_clone (struct net_pkt *pkt, k_timeout_t timeout)
 Clone pkt and increase the refcount of its buffer.
 
int net_pkt_read (struct net_pkt *pkt, void *data, size_t length)
 Read some data from a net_pkt.
 
static int net_pkt_read_u8 (struct net_pkt *pkt, uint8_t *data)
 Read a byte (uint8_t) from a net_pkt.
 
int net_pkt_read_be16 (struct net_pkt *pkt, uint16_t *data)
 Read uint16_t big endian data from a net_pkt.
 
int net_pkt_read_le16 (struct net_pkt *pkt, uint16_t *data)
 Read uint16_t little endian data from a net_pkt.
 
int net_pkt_read_be32 (struct net_pkt *pkt, uint32_t *data)
 Read uint32_t big endian data from a net_pkt.
 
int net_pkt_write (struct net_pkt *pkt, const void *data, size_t length)
 Write data into a net_pkt.
 
static int net_pkt_write_u8 (struct net_pkt *pkt, uint8_t data)
 Write a byte (uint8_t) data to a net_pkt.
 
static int net_pkt_write_be16 (struct net_pkt *pkt, uint16_t data)
 Write a uint16_t big endian data to a net_pkt.
 
static int net_pkt_write_be32 (struct net_pkt *pkt, uint32_t data)
 Write a uint32_t big endian data to a net_pkt.
 
static int net_pkt_write_le32 (struct net_pkt *pkt, uint32_t data)
 Write a uint32_t little endian data to a net_pkt.
 
static int net_pkt_write_le16 (struct net_pkt *pkt, uint16_t data)
 Write a uint16_t little endian data to a net_pkt.
 
size_t net_pkt_remaining_data (struct net_pkt *pkt)
 Get the amount of data which can be read from current cursor position.
 
int net_pkt_update_length (struct net_pkt *pkt, size_t length)
 Update the overall length of a packet.
 
int net_pkt_pull (struct net_pkt *pkt, size_t length)
 Remove data from the packet at current location.
 
uint16_t net_pkt_get_current_offset (struct net_pkt *pkt)
 Get the actual offset in the packet from its cursor.
 
bool net_pkt_is_contiguous (struct net_pkt *pkt, size_t size)
 Check if a data size could fit contiguously.
 
size_t net_pkt_get_contiguous_len (struct net_pkt *pkt)
 Get the contiguous buffer space.
 
void * net_pkt_get_data (struct net_pkt *pkt, struct net_pkt_data_access *access)
 Get data from a network packet in a contiguous way.
 
int net_pkt_set_data (struct net_pkt *pkt, struct net_pkt_data_access *access)
 Set contiguous data into a network packet.
 
static int net_pkt_acknowledge_data (struct net_pkt *pkt, struct net_pkt_data_access *access)
 Acknowledge previously contiguous data taken from a network packet Packet needs to be set to overwrite mode.
 

Detailed Description

Network packet management library .

Since
1.5
Version
0.8.0

Macro Definition Documentation

◆ NET_PKT_DATA_POOL_DEFINE

#define NET_PKT_DATA_POOL_DEFINE ( name,
count )

#include <zephyr/net/net_pkt.h>

Value:
NET_BUF_POOL_DEFINE(name, count, CONFIG_NET_BUF_DATA_SIZE, \
0, NULL)
#define NET_BUF_POOL_DEFINE(_name, _count, _size, _ud_size, _destroy)
Define a new pool for buffers.
Definition net_buf.h:1299

Create a data fragment net_buf pool.

A net_buf pool is used to store actual data for network packets. It must be coupled with a net_pkt slab (NET_PKT_SLAB_DEFINE) used to store the packet meta-information. The macro can be used by an application to define additional custom per-context TX packet pools (see net_context_setup_pools()).

Parameters
nameName of the pool.
countNumber of net_buf in this pool.

◆ net_pkt_print_frags

#define net_pkt_print_frags ( pkt)

◆ NET_PKT_SLAB_DEFINE

#define NET_PKT_SLAB_DEFINE ( name,
count )

#include <zephyr/net/net_pkt.h>

Value:
K_MEM_SLAB_DEFINE(name, sizeof(struct net_pkt), count, 4); \
NET_PKT_ALLOC_STATS_DEFINE(pkt_alloc_stats_##name, name)
#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align)
Statically define and initialize a memory slab in a public (non-static) scope.
Definition kernel.h:5221
Network packet.
Definition net_pkt.h:91

Create a net_pkt slab.

A net_pkt slab is used to store meta-information about network packets. It must be coupled with a data fragment pool (NET_PKT_DATA_POOL_DEFINE) used to store the actual packet data. The macro can be used by an application to define additional custom per-context TX packet slabs (see net_context_setup_pools()).

Parameters
nameName of the slab.
countNumber of net_pkt in this slab.

Function Documentation

◆ net_pkt_acknowledge_data()

static int net_pkt_acknowledge_data ( struct net_pkt * pkt,
struct net_pkt_data_access * access )
inlinestatic

#include <zephyr/net/net_pkt.h>

Acknowledge previously contiguous data taken from a network packet Packet needs to be set to overwrite mode.

◆ net_pkt_alloc()

struct net_pkt * net_pkt_alloc ( k_timeout_t timeout)

#include <zephyr/net/net_pkt.h>

Allocate an initialized net_pkt.

for the time being, 2 pools are used. One for TX and one for RX. This allocator has to be used for TX.

Parameters
timeoutMaximum time to wait for an allocation.
Returns
a pointer to a newly allocated net_pkt on success, NULL otherwise.

◆ net_pkt_alloc_buffer()

int net_pkt_alloc_buffer ( struct net_pkt * pkt,
size_t size,
enum net_ip_protocol proto,
k_timeout_t timeout )

#include <zephyr/net/net_pkt.h>

Allocate buffer for a net_pkt.

: such allocator will take into account space necessary for headers, MTU, and existing buffer (if any). Beware that, due to all these criteria, the allocated size might be smaller/bigger than requested one.

Parameters
pktThe network packet requiring buffer to be allocated.
sizeThe size of buffer being requested.
protoThe IP protocol type (can be 0 for none).
timeoutMaximum time to wait for an allocation.
Returns
0 on success, negative errno code otherwise.

◆ net_pkt_alloc_buffer_raw()

int net_pkt_alloc_buffer_raw ( struct net_pkt * pkt,
size_t size,
k_timeout_t timeout )

#include <zephyr/net/net_pkt.h>

Allocate buffer for a net_pkt, of specified size, w/o any additional preconditions.

: The actual buffer size may be larger than requested one if fixed size buffers are in use.

Parameters
pktThe network packet requiring buffer to be allocated.
sizeThe size of buffer being requested.
timeoutMaximum time to wait for an allocation.
Returns
0 on success, negative errno code otherwise.

◆ net_pkt_alloc_from_slab()

struct net_pkt * net_pkt_alloc_from_slab ( struct k_mem_slab * slab,
k_timeout_t timeout )

#include <zephyr/net/net_pkt.h>

Allocate an initialized net_pkt from a specific slab.

unlike net_pkt_alloc() which uses core slabs, this one will use an external slab (see NET_PKT_SLAB_DEFINE()). Do not use it unless you know what you are doing. Basically, only net_context should be using this, in order to allocate packet and then buffer on its local slab/pool (if any).

Parameters
slabThe slab to use for allocating the packet
timeoutMaximum time to wait for an allocation.
Returns
a pointer to a newly allocated net_pkt on success, NULL otherwise.

◆ net_pkt_alloc_on_iface()

struct net_pkt * net_pkt_alloc_on_iface ( struct net_if * iface,
k_timeout_t timeout )

#include <zephyr/net/net_pkt.h>

Allocate a network packet for a specific network interface.

Parameters
ifaceThe network interface the packet is supposed to go through.
timeoutMaximum time to wait for an allocation.
Returns
a pointer to a newly allocated net_pkt on success, NULL otherwise.

◆ net_pkt_alloc_with_buffer()

struct net_pkt * net_pkt_alloc_with_buffer ( struct net_if * iface,
size_t size,
sa_family_t family,
enum net_ip_protocol proto,
k_timeout_t timeout )

#include <zephyr/net/net_pkt.h>

Allocate a network packet and buffer at once.

Parameters
ifaceThe network interface the packet is supposed to go through.
sizeThe size of buffer.
familyThe family to which the packet belongs.
protoThe IP protocol type (can be 0 for none).
timeoutMaximum time to wait for an allocation.
Returns
a pointer to a newly allocated net_pkt on success, NULL otherwise.

◆ net_pkt_append_buffer()

void net_pkt_append_buffer ( struct net_pkt * pkt,
struct net_buf * buffer )

#include <zephyr/net/net_pkt.h>

Append a buffer in packet.

Parameters
pktNetwork packet where to append the buffer
bufferBuffer to append

◆ net_pkt_available_buffer()

size_t net_pkt_available_buffer ( struct net_pkt * pkt)

#include <zephyr/net/net_pkt.h>

Get available buffer space from a pkt.

Note
Reserved bytes (headroom) in any of the fragments are not considered to be available.
Parameters
pktThe net_pkt which buffer availability should be evaluated
Returns
the amount of buffer available

◆ net_pkt_available_payload_buffer()

size_t net_pkt_available_payload_buffer ( struct net_pkt * pkt,
enum net_ip_protocol proto )

#include <zephyr/net/net_pkt.h>

Get available buffer space for payload from a pkt.

Note
Reserved bytes (headroom) in any of the fragments are not considered to be available.

Unlike net_pkt_available_buffer(), this will take into account the headers space.

Parameters
pktThe net_pkt which payload buffer availability should be evaluated
protoThe IP protocol type (can be 0 for none).
Returns
the amount of buffer available for payload

◆ net_pkt_clone()

struct net_pkt * net_pkt_clone ( struct net_pkt * pkt,
k_timeout_t timeout )

#include <zephyr/net/net_pkt.h>

Clone pkt and its buffer.

The cloned packet will be allocated on the same pool as the original one.

Parameters
pktOriginal pkt to be cloned
timeoutTimeout to wait for free buffer
Returns
NULL if error, cloned packet otherwise.

◆ net_pkt_compact()

void net_pkt_compact ( struct net_pkt * pkt)

#include <zephyr/net/net_pkt.h>

Compact the fragment list of a packet.

After this there is no more any free space in individual fragments.

Parameters
pktNetwork packet.

◆ net_pkt_copy()

int net_pkt_copy ( struct net_pkt * pkt_dst,
struct net_pkt * pkt_src,
size_t length )

#include <zephyr/net/net_pkt.h>

Copy data from a packet into another one.

Both net_pkt cursors should be properly initialized and, if needed, positioned using net_pkt_skip. The cursors will be updated after the operation.

Parameters
pkt_dstDestination network packet.
pkt_srcSource network packet.
lengthLength of data to be copied.
Returns
0 on success, negative errno code otherwise.

◆ net_pkt_cursor_backup()

static void net_pkt_cursor_backup ( struct net_pkt * pkt,
struct net_pkt_cursor * backup )
inlinestatic

#include <zephyr/net/net_pkt.h>

Backup net_pkt cursor.

Parameters
pktThe net_pkt whose cursor is going to be backed up
backupThe cursor where to backup net_pkt cursor

◆ net_pkt_cursor_get_pos()

static void * net_pkt_cursor_get_pos ( struct net_pkt * pkt)
inlinestatic

#include <zephyr/net/net_pkt.h>

Returns current position of the cursor.

Parameters
pktThe net_pkt whose cursor position is going to be returned
Returns
cursor's position

◆ net_pkt_cursor_init()

void net_pkt_cursor_init ( struct net_pkt * pkt)

#include <zephyr/net/net_pkt.h>

Initialize net_pkt cursor.

This will initialize the net_pkt cursor from its buffer.

Parameters
pktThe net_pkt whose cursor is going to be initialized

◆ net_pkt_cursor_restore()

static void net_pkt_cursor_restore ( struct net_pkt * pkt,
struct net_pkt_cursor * backup )
inlinestatic

#include <zephyr/net/net_pkt.h>

Restore net_pkt cursor from a backup.

Parameters
pktThe net_pkt whose cursor is going to be restored
backupThe cursor from where to restore net_pkt cursor

◆ net_pkt_frag_add()

void net_pkt_frag_add ( struct net_pkt * pkt,
struct net_buf * frag )

#include <zephyr/net/net_pkt.h>

Add a fragment to a packet at the end of its fragment list.

Parameters
pktpkt Network packet where to add the fragment
fragFragment to add

◆ net_pkt_frag_del()

struct net_buf * net_pkt_frag_del ( struct net_pkt * pkt,
struct net_buf * parent,
struct net_buf * frag )

#include <zephyr/net/net_pkt.h>

Delete existing fragment from a packet.

Parameters
pktNetwork packet from which frag belongs to.
parentparent fragment of frag, or NULL if none.
fragFragment to delete.
Returns
Pointer to the following fragment, or NULL if it had no further fragments.

◆ net_pkt_frag_insert()

void net_pkt_frag_insert ( struct net_pkt * pkt,
struct net_buf * frag )

#include <zephyr/net/net_pkt.h>

Insert a fragment to a packet at the beginning of its fragment list.

Parameters
pktpkt Network packet where to insert the fragment
fragFragment to insert

◆ net_pkt_frag_ref()

struct net_buf * net_pkt_frag_ref ( struct net_buf * frag)

#include <zephyr/net/net_pkt.h>

Increase the packet fragment ref count.

Mark the fragment to be used still.

Parameters
fragNetwork fragment to ref.
Returns
a pointer on the referenced Network fragment.

◆ net_pkt_frag_unref()

void net_pkt_frag_unref ( struct net_buf * frag)

#include <zephyr/net/net_pkt.h>

Decrease the packet fragment ref count.

Parameters
fragNetwork fragment to unref.

◆ net_pkt_get_contiguous_len()

size_t net_pkt_get_contiguous_len ( struct net_pkt * pkt)

#include <zephyr/net/net_pkt.h>

Get the contiguous buffer space.

Parameters
pktNetwork packet
Returns
The available contiguous buffer space in bytes starting from the current cursor position. 0 in case of an error.

◆ net_pkt_get_current_offset()

uint16_t net_pkt_get_current_offset ( struct net_pkt * pkt)

#include <zephyr/net/net_pkt.h>

Get the actual offset in the packet from its cursor.

Parameters
pktNetwork packet.
Returns
a valid offset on success, 0 otherwise as there is nothing that can be done to evaluate the offset.

◆ net_pkt_get_data()

void * net_pkt_get_data ( struct net_pkt * pkt,
struct net_pkt_data_access * access )

#include <zephyr/net/net_pkt.h>

Get data from a network packet in a contiguous way.

net_pkt's cursor should be properly initialized and, if needed, positioned using net_pkt_skip. Unlike other functions, cursor position will not be updated after the operation.

Parameters
pktThe network packet from where to get the data.
accessA pointer to a valid net_pkt_data_access describing the data to get in a contiguous way.
Returns
a pointer to the requested contiguous data, NULL otherwise.

◆ net_pkt_get_frag()

struct net_buf * net_pkt_get_frag ( struct net_pkt * pkt,
size_t min_len,
k_timeout_t timeout )

#include <zephyr/net/net_pkt.h>

Get a data fragment that might be from user specific buffer pool or from global DATA pool.

Parameters
pktNetwork packet.
min_lenMinimum length of the requested fragment.
timeoutAffects the action taken should the net buf pool be empty. If K_NO_WAIT, then return immediately. If K_FOREVER, then wait as long as necessary. Otherwise, wait up to the specified time.
Returns
Network buffer if successful, NULL otherwise.

◆ net_pkt_get_info()

void net_pkt_get_info ( struct k_mem_slab ** rx,
struct k_mem_slab ** tx,
struct net_buf_pool ** rx_data,
struct net_buf_pool ** tx_data )

#include <zephyr/net/net_pkt.h>

Get information about predefined RX, TX and DATA pools.

Parameters
rxPointer to RX pool is returned.
txPointer to TX pool is returned.
rx_dataPointer to RX DATA pool is returned.
tx_dataPointer to TX DATA pool is returned.

◆ net_pkt_get_reserve_data()

struct net_buf * net_pkt_get_reserve_data ( struct net_buf_pool * pool,
size_t min_len,
k_timeout_t timeout )

#include <zephyr/net/net_pkt.h>

Get a data buffer from a given pool.

Normally this version is not useful for applications but is mainly used by network fragmentation code.

Parameters
poolThe net_buf pool to use.
min_lenMinimum length of the requested fragment.
timeoutAffects the action taken should the net buf pool be empty. If K_NO_WAIT, then return immediately. If K_FOREVER, then wait as long as necessary. Otherwise, wait up to the specified time.
Returns
Network buffer if successful, NULL otherwise.

◆ net_pkt_get_reserve_rx_data()

struct net_buf * net_pkt_get_reserve_rx_data ( size_t min_len,
k_timeout_t timeout )

#include <zephyr/net/net_pkt.h>

Get RX DATA buffer from pool.

Normally you should use net_pkt_get_frag() instead.

Normally this version is not useful for applications but is mainly used by network fragmentation code.

Parameters
min_lenMinimum length of the requested fragment.
timeoutAffects the action taken should the net buf pool be empty. If K_NO_WAIT, then return immediately. If K_FOREVER, then wait as long as necessary. Otherwise, wait up to the specified time.
Returns
Network buffer if successful, NULL otherwise.

◆ net_pkt_get_reserve_tx_data()

struct net_buf * net_pkt_get_reserve_tx_data ( size_t min_len,
k_timeout_t timeout )

#include <zephyr/net/net_pkt.h>

Get TX DATA buffer from pool.

Normally you should use net_pkt_get_frag() instead.

Normally this version is not useful for applications but is mainly used by network fragmentation code.

Parameters
min_lenMinimum length of the requested fragment.
timeoutAffects the action taken should the net buf pool be empty. If K_NO_WAIT, then return immediately. If K_FOREVER, then wait as long as necessary. Otherwise, wait up to the specified time.
Returns
Network buffer if successful, NULL otherwise.

◆ net_pkt_is_contiguous()

bool net_pkt_is_contiguous ( struct net_pkt * pkt,
size_t size )

#include <zephyr/net/net_pkt.h>

Check if a data size could fit contiguously.

net_pkt's cursor should be properly initialized and, if needed, positioned using net_pkt_skip.

Parameters
pktNetwork packet.
sizeThe size to check for contiguity
Returns
true if that is the case, false otherwise.

◆ net_pkt_memset()

int net_pkt_memset ( struct net_pkt * pkt,
int byte,
size_t length )

#include <zephyr/net/net_pkt.h>

Memset some data in a net_pkt.

net_pkt's cursor should be properly initialized and, if needed, positioned using net_pkt_skip. Cursor position will be updated after the operation.

Parameters
pktThe net_pkt whose buffer to fill starting at the current cursor position.
byteThe byte to write in memory
lengthAmount of data to memset with given byte
Returns
0 in success, negative errno code otherwise.

◆ net_pkt_pull()

int net_pkt_pull ( struct net_pkt * pkt,
size_t length )

#include <zephyr/net/net_pkt.h>

Remove data from the packet at current location.

net_pkt's cursor should be properly initialized and, eventually, properly positioned using net_pkt_skip/read/write. Note that net_pkt's cursor is reset by this function.

Parameters
pktNetwork packet
lengthNumber of bytes to be removed
Returns
0 on success, negative errno code otherwise.

◆ net_pkt_read()

int net_pkt_read ( struct net_pkt * pkt,
void * data,
size_t length )

#include <zephyr/net/net_pkt.h>

Read some data from a net_pkt.

net_pkt's cursor should be properly initialized and, if needed, positioned using net_pkt_skip. Cursor position will be updated after the operation.

Parameters
pktThe network packet from where to read some data
dataThe destination buffer where to copy the data
lengthThe amount of data to copy
Returns
0 on success, negative errno code otherwise.

◆ net_pkt_read_be16()

int net_pkt_read_be16 ( struct net_pkt * pkt,
uint16_t * data )

#include <zephyr/net/net_pkt.h>

Read uint16_t big endian data from a net_pkt.

net_pkt's cursor should be properly initialized and, if needed, positioned using net_pkt_skip. Cursor position will be updated after the operation.

Parameters
pktThe network packet from where to read
dataThe destination uint16_t where to copy the data
Returns
0 on success, negative errno code otherwise.

◆ net_pkt_read_be32()

int net_pkt_read_be32 ( struct net_pkt * pkt,
uint32_t * data )

#include <zephyr/net/net_pkt.h>

Read uint32_t big endian data from a net_pkt.

net_pkt's cursor should be properly initialized and, if needed, positioned using net_pkt_skip. Cursor position will be updated after the operation.

Parameters
pktThe network packet from where to read
dataThe destination uint32_t where to copy the data
Returns
0 on success, negative errno code otherwise.

◆ net_pkt_read_le16()

int net_pkt_read_le16 ( struct net_pkt * pkt,
uint16_t * data )

#include <zephyr/net/net_pkt.h>

Read uint16_t little endian data from a net_pkt.

net_pkt's cursor should be properly initialized and, if needed, positioned using net_pkt_skip. Cursor position will be updated after the operation.

Parameters
pktThe network packet from where to read
dataThe destination uint16_t where to copy the data
Returns
0 on success, negative errno code otherwise.

◆ net_pkt_read_u8()

static int net_pkt_read_u8 ( struct net_pkt * pkt,
uint8_t * data )
inlinestatic

#include <zephyr/net/net_pkt.h>

Read a byte (uint8_t) from a net_pkt.

net_pkt's cursor should be properly initialized and, if needed, positioned using net_pkt_skip. Cursor position will be updated after the operation.

Parameters
pktThe network packet from where to read
dataThe destination uint8_t where to copy the data
Returns
0 on success, negative errno code otherwise.

◆ net_pkt_ref()

struct net_pkt * net_pkt_ref ( struct net_pkt * pkt)

#include <zephyr/net/net_pkt.h>

Increase the packet ref count.

Mark the packet to be used still.

Parameters
pktNetwork packet to ref.
Returns
Network packet if successful, NULL otherwise.

◆ net_pkt_remaining_data()

size_t net_pkt_remaining_data ( struct net_pkt * pkt)

#include <zephyr/net/net_pkt.h>

Get the amount of data which can be read from current cursor position.

Parameters
pktNetwork packet
Returns
Amount of data which can be read from current pkt cursor

◆ net_pkt_remove_tail()

int net_pkt_remove_tail ( struct net_pkt * pkt,
size_t length )

#include <zephyr/net/net_pkt.h>

Remove length bytes from tail of packet.

This function does not take packet cursor into account. It is a helper to remove unneeded bytes from tail of packet (like appended CRC). It takes care of buffer deallocation if removed bytes span whole buffer(s).

Parameters
pktNetwork packet
lengthNumber of bytes to be removed
Return values
0On success.
-EINVALIf packet length is shorter than length.

◆ net_pkt_rx_alloc()

struct net_pkt * net_pkt_rx_alloc ( k_timeout_t timeout)

#include <zephyr/net/net_pkt.h>

Allocate an initialized net_pkt for RX.

for the time being, 2 pools are used. One for TX and one for RX. This allocator has to be used for RX.

Parameters
timeoutMaximum time to wait for an allocation.
Returns
a pointer to a newly allocated net_pkt on success, NULL otherwise.

◆ net_pkt_rx_clone()

struct net_pkt * net_pkt_rx_clone ( struct net_pkt * pkt,
k_timeout_t timeout )

#include <zephyr/net/net_pkt.h>

Clone pkt and its buffer.

The cloned packet will be allocated on the RX packet poll.

Parameters
pktOriginal pkt to be cloned
timeoutTimeout to wait for free buffer
Returns
NULL if error, cloned packet otherwise.

◆ net_pkt_set_data()

int net_pkt_set_data ( struct net_pkt * pkt,
struct net_pkt_data_access * access )

#include <zephyr/net/net_pkt.h>

Set contiguous data into a network packet.

net_pkt's cursor should be properly initialized and, if needed, positioned using net_pkt_skip. Cursor position will be updated after the operation.

Parameters
pktThe network packet to where the data should be set.
accessA pointer to a valid net_pkt_data_access describing the data to set.
Returns
0 on success, a negative errno otherwise.

◆ net_pkt_shallow_clone()

struct net_pkt * net_pkt_shallow_clone ( struct net_pkt * pkt,
k_timeout_t timeout )

#include <zephyr/net/net_pkt.h>

Clone pkt and increase the refcount of its buffer.

Parameters
pktOriginal pkt to be shallow cloned
timeoutTimeout to wait for free packet
Returns
NULL if error, cloned packet otherwise.

◆ net_pkt_skip()

int net_pkt_skip ( struct net_pkt * pkt,
size_t length )

#include <zephyr/net/net_pkt.h>

Skip some data from a net_pkt.

net_pkt's cursor should be properly initialized Cursor position will be updated after the operation. Depending on the value of pkt->overwrite bit, this function will affect the buffer length or not. If it's true, it will advance the cursor to the requested length. If it's false, it will do the same but if the cursor was already also at the end of existing data, it will increment the buffer length. So in this case, its behavior is just like net_pkt_write or net_pkt_memset, difference being that it will not affect the buffer content itself (which may be just garbage then).

Parameters
pktThe net_pkt whose cursor will be updated to skip given amount of data from the buffer.
lengthAmount of data to skip in the buffer
Returns
0 in success, negative errno code otherwise.

◆ net_pkt_trim_buffer()

void net_pkt_trim_buffer ( struct net_pkt * pkt)

#include <zephyr/net/net_pkt.h>

Trim net_pkt buffer.

This will basically check for unused buffers and deallocate them relevantly

Parameters
pktThe net_pkt which buffer will be trimmed

◆ net_pkt_unref()

void net_pkt_unref ( struct net_pkt * pkt)

#include <zephyr/net/net_pkt.h>

Place packet back into the available packets slab.

Releases the packet to other use. This needs to be called by application after it has finished with the packet.

Parameters
pktNetwork packet to release.

◆ net_pkt_update_length()

int net_pkt_update_length ( struct net_pkt * pkt,
size_t length )

#include <zephyr/net/net_pkt.h>

Update the overall length of a packet.

Unlike net_pkt_pull() below, this does not take packet cursor into account. It's mainly a helper dedicated for ipv4 and ipv6 input functions. It shrinks the overall length by given parameter.

Parameters
pktNetwork packet
lengthThe new length of the packet
Returns
0 on success, negative errno code otherwise.

◆ net_pkt_write()

int net_pkt_write ( struct net_pkt * pkt,
const void * data,
size_t length )

#include <zephyr/net/net_pkt.h>

Write data into a net_pkt.

net_pkt's cursor should be properly initialized and, if needed, positioned using net_pkt_skip. Cursor position will be updated after the operation.

Parameters
pktThe network packet where to write
dataData to be written
lengthLength of the data to be written
Returns
0 on success, negative errno code otherwise.

◆ net_pkt_write_be16()

static int net_pkt_write_be16 ( struct net_pkt * pkt,
uint16_t data )
inlinestatic

#include <zephyr/net/net_pkt.h>

Write a uint16_t big endian data to a net_pkt.

net_pkt's cursor should be properly initialized and, if needed, positioned using net_pkt_skip. Cursor position will be updated after the operation.

Parameters
pktThe network packet from where to read
dataThe uint16_t value in host byte order to write
Returns
0 on success, negative errno code otherwise.

◆ net_pkt_write_be32()

static int net_pkt_write_be32 ( struct net_pkt * pkt,
uint32_t data )
inlinestatic

#include <zephyr/net/net_pkt.h>

Write a uint32_t big endian data to a net_pkt.

net_pkt's cursor should be properly initialized and, if needed, positioned using net_pkt_skip. Cursor position will be updated after the operation.

Parameters
pktThe network packet from where to read
dataThe uint32_t value in host byte order to write
Returns
0 on success, negative errno code otherwise.

◆ net_pkt_write_le16()

static int net_pkt_write_le16 ( struct net_pkt * pkt,
uint16_t data )
inlinestatic

#include <zephyr/net/net_pkt.h>

Write a uint16_t little endian data to a net_pkt.

net_pkt's cursor should be properly initialized and, if needed, positioned using net_pkt_skip. Cursor position will be updated after the operation.

Parameters
pktThe network packet from where to read
dataThe uint16_t value in host byte order to write
Returns
0 on success, negative errno code otherwise.

◆ net_pkt_write_le32()

static int net_pkt_write_le32 ( struct net_pkt * pkt,
uint32_t data )
inlinestatic

#include <zephyr/net/net_pkt.h>

Write a uint32_t little endian data to a net_pkt.

net_pkt's cursor should be properly initialized and, if needed, positioned using net_pkt_skip. Cursor position will be updated after the operation.

Parameters
pktThe network packet from where to read
dataThe uint32_t value in host byte order to write
Returns
0 on success, negative errno code otherwise.

◆ net_pkt_write_u8()

static int net_pkt_write_u8 ( struct net_pkt * pkt,
uint8_t data )
inlinestatic

#include <zephyr/net/net_pkt.h>

Write a byte (uint8_t) data to a net_pkt.

net_pkt's cursor should be properly initialized and, if needed, positioned using net_pkt_skip. Cursor position will be updated after the operation.

Parameters
pktThe network packet from where to read
dataThe uint8_t value to write
Returns
0 on success, negative errno code otherwise.