ConOpSys V2970  P004.07
ANVILEX control operating system
Macros | Enumerations | Functions
Packet buffers (PBUF)

Macros

#define PBUF_NEEDS_COPY(p)   ((p)->type_internal & PBUF_TYPE_FLAG_DATA_VOLATILE)
 

Enumerations

enum  pbuf_layer {
  PBUF_TRANSPORT = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN , PBUF_IP = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN , PBUF_LINK = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN , PBUF_RAW_TX = PBUF_LINK_ENCAPSULATION_HLEN ,
  PBUF_RAW = 0
}
 
enum  pbuf_type { PBUF_RAM = (PBUF_ALLOC_FLAG_DATA_CONTIGUOUS | PBUF_TYPE_FLAG_STRUCT_DATA_CONTIGUOUS | PBUF_TYPE_ALLOC_SRC_MASK_STD_HEAP) , PBUF_ROM = PBUF_TYPE_ALLOC_SRC_MASK_STD_MEMP_PBUF , PBUF_REF = (PBUF_TYPE_FLAG_DATA_VOLATILE | PBUF_TYPE_ALLOC_SRC_MASK_STD_MEMP_PBUF) , PBUF_POOL = (PBUF_ALLOC_FLAG_RX | PBUF_TYPE_FLAG_STRUCT_DATA_CONTIGUOUS | PBUF_TYPE_ALLOC_SRC_MASK_STD_MEMP_PBUF_POOL) }
 

Functions

struct pbufpbuf_alloc (pbuf_layer layer, u16_t length, pbuf_type type)
 
struct pbufpbuf_alloc_reference (void *payload, u16_t length, pbuf_type type)
 
void pbuf_realloc (struct pbuf *p, u16_t new_len)
 
u8_t pbuf_free (struct pbuf *p)
 
void pbuf_ref (struct pbuf *p)
 
void pbuf_cat (struct pbuf *h, struct pbuf *t)
 
void pbuf_chain (struct pbuf *h, struct pbuf *t)
 
err_t pbuf_copy (struct pbuf *p_to, const struct pbuf *p_from)
 
u16_t pbuf_copy_partial (const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
 
void * pbuf_get_contiguous (const struct pbuf *p, void *buffer, size_t bufsize, u16_t len, u16_t offset)
 
struct pbufpbuf_skip (struct pbuf *in, u16_t in_offset, u16_t *out_offset)
 
err_t pbuf_take (struct pbuf *buf, const void *dataptr, u16_t len)
 
err_t pbuf_take_at (struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset)
 
struct pbufpbuf_coalesce (struct pbuf *p, pbuf_layer layer)
 
struct pbufpbuf_clone (pbuf_layer layer, pbuf_type type, struct pbuf *p)
 
u8_t pbuf_get_at (const struct pbuf *p, u16_t offset)
 
int pbuf_try_get_at (const struct pbuf *p, u16_t offset)
 
void pbuf_put_at (struct pbuf *p, u16_t offset, u8_t data)
 
u16_t pbuf_memcmp (const struct pbuf *p, u16_t offset, const void *s2, u16_t n)
 
u16_t pbuf_memfind (const struct pbuf *p, const void *mem, u16_t mem_len, u16_t start_offset)
 

Detailed Description

Packets are built from the pbuf data structure. It supports dynamic memory allocation for packet contents or can reference externally managed packet contents both in RAM and ROM. Quick allocation for incoming packets is provided through pools with fixed sized pbufs.

A packet may span over multiple pbufs, chained as a singly linked list. This is called a "pbuf chain".

Multiple packets may be queued, also using this singly linked list. This is called a "packet queue".

So, a packet queue consists of one or more pbuf chains, each of which consist of one or more pbufs. CURRENTLY, PACKET QUEUES ARE NOT SUPPORTED!!! Use helper structs to queue multiple packets.

The differences between a pbuf chain and a packet queue are very precise but subtle.

The last pbuf of a packet has a ->tot_len field that equals the ->len field. It can be found by traversing the list. If the last pbuf of a packet has a ->next field other than NULL, more packets are on the queue.

Therefore, looping through a pbuf of a single packet, has an loop end condition (tot_len == p->len), NOT (next == NULL).

Example of custom pbuf usage: zerocopyrx

Macro Definition Documentation

◆ PBUF_NEEDS_COPY

#define PBUF_NEEDS_COPY (   p)    ((p)->type_internal & PBUF_TYPE_FLAG_DATA_VOLATILE)

PBUF_NEEDS_COPY(p): return a boolean value indicating whether the given pbuf needs to be copied in order to be kept around beyond the current call stack without risking being corrupted. The default setting provides safety: it will make a copy iof any pbuf chain that does not consist entirely of PBUF_ROM type pbufs. For setups with zero-copy support, it may be redefined to evaluate to true in all cases, for example. However, doing so also has an effect on the application side: any buffers that are not copied must also not be reused by the application after passing them to lwIP. For example, when setting PBUF_NEEDS_COPY to (0), after using udp_send() with a PBUF_RAM pbuf, the application must free the pbuf immediately, rather than reusing it for other purposes. For more background information on this, see tasks #6735 and #7896, and bugs #11400 and #49914.

Enumeration Type Documentation

◆ pbuf_layer

enum pbuf_layer

Enumeration of pbuf layers

Enumerator
PBUF_TRANSPORT 

Includes spare room for transport layer header, e.g. UDP header. Use this if you intend to pass the pbuf to functions like udp_send().

PBUF_IP 

Includes spare room for IP header. Use this if you intend to pass the pbuf to functions like raw_send().

PBUF_LINK 

Includes spare room for link layer header (ethernet header). Use this if you intend to pass the pbuf to functions like ethernet_output().

See also
PBUF_LINK_HLEN
PBUF_RAW_TX 

Includes spare room for additional encapsulation header before ethernet headers (e.g. 802.11). Use this if you intend to pass the pbuf to functions like netif->linkoutput().

See also
PBUF_LINK_ENCAPSULATION_HLEN
PBUF_RAW 

Use this for input packets in a netif driver when calling netif->input() in the most common case - ethernet-layer netif driver.

◆ pbuf_type

enum pbuf_type

Enumeration of pbuf types

Enumerator
PBUF_RAM 

pbuf data is stored in RAM, used for TX mostly, struct pbuf and its payload are allocated in one piece of contiguous memory (so the first payload byte can be calculated from struct pbuf). pbuf_alloc() allocates PBUF_RAM pbufs as unchained pbufs (although that might change in future versions). This should be used for all OUTGOING packets (TX).

PBUF_ROM 

pbuf data is stored in ROM, i.e. struct pbuf and its payload are located in totally different memory areas. Since it points to ROM, payload does not have to be copied when queued for transmission.

PBUF_REF 

pbuf comes from the pbuf pool. Much like PBUF_ROM but payload might change so it has to be duplicated when queued before transmitting, depending on who has a 'ref' to it.

PBUF_POOL 

pbuf payload refers to RAM. This one comes from a pool and should be used for RX. Payload can be chained (scatter-gather RX) but like PBUF_RAM, struct pbuf and its payload are allocated in one piece of contiguous memory (so the first payload byte can be calculated from struct pbuf). Don't use this for TX, if the pool becomes empty e.g. because of TCP queuing, you are unable to receive TCP acks!

Function Documentation

◆ pbuf_alloc()

struct pbuf* pbuf_alloc ( pbuf_layer  layer,
u16_t  length,
pbuf_type  type 
)

Allocates a pbuf of the given type (possibly a chain for PBUF_POOL type).

The actual memory allocated for the pbuf is determined by the layer at which the pbuf is allocated and the requested size (from the size parameter).

Parameters
layerheader size
lengthsize of the pbuf's payload
typethis parameter decides how and where the pbuf should be allocated as follows:
  • PBUF_RAM: buffer memory for pbuf is allocated as one large chunk. This includes protocol headers as well.
  • PBUF_ROM: no buffer memory is allocated for the pbuf, even for protocol headers. Additional headers must be prepended by allocating another pbuf and chain in to the front of the ROM pbuf. It is assumed that the memory used is really similar to ROM in that it is immutable and will not be changed. Memory which is dynamic should generally not be attached to PBUF_ROM pbufs. Use PBUF_REF instead.
  • PBUF_REF: no buffer memory is allocated for the pbuf, even for protocol headers. It is assumed that the pbuf is only being used in a single thread. If the pbuf gets queued, then pbuf_take should be called to copy the buffer.
  • PBUF_POOL: the pbuf is allocated as a pbuf chain, with pbufs from the pbuf pool that is allocated during pbuf_init().
Returns
the allocated pbuf. If multiple pbufs where allocated, this is the first pbuf of a pbuf chain.

References LWIP_ASSERT, LWIP_DBG_TRACE, LWIP_DEBUGF, LWIP_MEM_ALIGN, LWIP_MEM_ALIGN_SIZE, LWIP_MIN, MEM_ALIGNMENT, mem_malloc(), memp_malloc(), next, NULL, payload, pbuf_alloc_reference(), PBUF_DEBUG, pbuf_free(), pbuf_init_alloced_pbuf(), PBUF_POOL, PBUF_POOL_BUFSIZE_ALIGNED, PBUF_POOL_IS_EMPTY, PBUF_RAM, PBUF_REF, PBUF_ROM, SIZEOF_STRUCT_PBUF, and U16_F.

Referenced by low_level_input(), pbuf_clone(), and slipif_rxbyte().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pbuf_alloc_reference()

struct pbuf* pbuf_alloc_reference ( void *  payload,
u16_t  length,
pbuf_type  type 
)

Allocates a pbuf for referenced data. Referenced data can be volatile (PBUF_REF) or long-lived (PBUF_ROM).

The actual memory allocated for the pbuf is determined by the layer at which the pbuf is allocated and the requested size (from the size parameter).

Parameters
payloadreferenced payload
lengthsize of the pbuf's payload
typethis parameter decides how and where the pbuf should be allocated as follows:
  • PBUF_ROM: It is assumed that the memory used is really similar to ROM in that it is immutable and will not be changed. Memory which is dynamic should generally not be attached to PBUF_ROM pbufs. Use PBUF_REF instead.
  • PBUF_REF: It is assumed that the pbuf is only being used in a single thread. If the pbuf gets queued, then pbuf_take should be called to copy the buffer.
Returns
the allocated pbuf.

References LWIP_ASSERT, LWIP_DBG_LEVEL_SERIOUS, LWIP_DEBUGF, memp_malloc(), NULL, payload, PBUF_DEBUG, pbuf_init_alloced_pbuf(), PBUF_REF, and PBUF_ROM.

Referenced by pbuf_alloc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pbuf_cat()

void pbuf_cat ( struct pbuf h,
struct pbuf t 
)

Concatenate two pbufs (each may be a pbuf chain) and take over the caller's reference of the tail pbuf.

Note
The caller MAY NOT reference the tail pbuf afterwards. Use pbuf_chain() for that purpose.

This function explicitly does not check for tot_len overflow to prevent failing to queue too long pbufs. This can produce invalid pbufs, so handle with care!

See also
pbuf_chain()

References len, LWIP_ASSERT, LWIP_ERROR, next, NULL, and tot_len.

Referenced by pbuf_chain(), and slipif_rxbyte().

Here is the caller graph for this function:

◆ pbuf_chain()

void pbuf_chain ( struct pbuf h,
struct pbuf t 
)

Chain two pbufs (or pbuf chains) together.

The caller MUST call pbuf_free(t) once it has stopped using it. Use pbuf_cat() instead if you no longer use t.

Parameters
hhead pbuf (chain)
ttail pbuf (chain)
Note
The pbufs MUST belong to the same packet.
MAY NOT be called on a packet queue.

The ->tot_len fields of all pbufs of the head chain are adjusted. The ->next field of the last pbuf of the head chain is adjusted. The ->ref field of the first pbuf of the tail chain is adjusted.

References LWIP_DBG_TRACE, LWIP_DEBUGF, pbuf_cat(), PBUF_DEBUG, and pbuf_ref().

Here is the call graph for this function:

◆ pbuf_clone()

struct pbuf* pbuf_clone ( pbuf_layer  layer,
pbuf_type  type,
struct pbuf p 
)

Allocates a new pbuf of same length (via pbuf_alloc()) and copies the source pbuf into this new pbuf (using pbuf_copy()).

Parameters
layerpbuf_layer of the new pbuf
typethis parameter decides how and where the pbuf should be allocated (
See also
pbuf_alloc())
Parameters
pthe source pbuf
Returns
a new pbuf or NULL if allocation fails

References ERR_OK, LWIP_ASSERT, LWIP_UNUSED_ARG, NULL, pbuf_alloc(), pbuf_copy(), and tot_len.

Referenced by pbuf_coalesce().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pbuf_coalesce()

struct pbuf* pbuf_coalesce ( struct pbuf p,
pbuf_layer  layer 
)

Creates a single pbuf out of a queue of pbufs.

Remarks
: Either the source pbuf 'p' is freed by this function or the original pbuf 'p' is returned, therefore the caller has to check the result!
Parameters
pthe source pbuf
layerpbuf_layer of the new pbuf
Returns
a new, single pbuf (p->next is NULL) or the old pbuf if allocation fails

References next, NULL, pbuf_clone(), pbuf_free(), and PBUF_RAM.

Here is the call graph for this function:

◆ pbuf_copy()

err_t pbuf_copy ( struct pbuf p_to,
const struct pbuf p_from 
)

Create PBUF_RAM copies of pbufs.

Used to queue packets on behalf of the lwIP stack, such as ARP based queueing.

Note
You MUST explicitly use p = pbuf_take(p);
Only one packet is copied, no packet queue!
Parameters
p_topbuf destination of the copy
p_frompbuf source of the copy
Returns
ERR_OK if pbuf was copied ERR_ARG if one of the pbufs is NULL or p_to is not big enough to hold p_from

References ERR_ARG, ERR_OK, ERR_VAL, len, LWIP_ASSERT, LWIP_DBG_TRACE, LWIP_DEBUGF, LWIP_ERROR, MEMCPY, next, NULL, payload, PBUF_DEBUG, and tot_len.

Referenced by pbuf_clone().

Here is the caller graph for this function:

◆ pbuf_copy_partial()

u16_t pbuf_copy_partial ( const struct pbuf buf,
void *  dataptr,
u16_t  len,
u16_t  offset 
)

Copy (part of) the contents of a packet buffer to an application supplied buffer.

Parameters
bufthe pbuf from which to copy data
dataptrthe application supplied buffer
lenlength of data to copy (dataptr must be big enough). No more than buf->tot_len will be copied, irrespective of len
offsetoffset into the packet buffer from where to begin copying len bytes
Returns
the number of bytes copied, or 0 on failure

References len, LWIP_ERROR, MEMCPY, next, NULL, and payload.

Referenced by pbuf_get_contiguous().

Here is the caller graph for this function:

◆ pbuf_free()

u8_t pbuf_free ( struct pbuf p)

Dereference a pbuf chain or queue and deallocate any no-longer-used pbufs at the head of this chain or queue.

Decrements the pbuf reference count. If it reaches zero, the pbuf is deallocated.

For a pbuf chain, this is repeated for each pbuf in the chain, up to the first pbuf which has a non-zero reference count after decrementing. So, when all reference counts are one, the whole chain is free'd.

Parameters
pThe pbuf (chain) to be dereferenced.
Returns
the number of pbufs that were de-allocated from the head of the chain.
Note
MUST NOT be called on a packet queue (Not verified to work yet).
the reference counter of a pbuf equals the number of pointers that refer to the pbuf (or into the pbuf).

References LWIP_ASSERT, LWIP_DBG_LEVEL_SERIOUS, LWIP_DBG_TRACE, LWIP_DEBUGF, LWIP_PBUF_REF_T, mem_free(), memp_free(), next, NULL, PBUF_DEBUG, PBUF_FLAG_IS_CUSTOM, pbuf_get_allocsrc, PBUF_TYPE_ALLOC_SRC_MASK_STD_HEAP, PBUF_TYPE_ALLOC_SRC_MASK_STD_MEMP_PBUF, PBUF_TYPE_ALLOC_SRC_MASK_STD_MEMP_PBUF_POOL, PERF_START, PERF_STOP, ref, SYS_ARCH_DECL_PROTECT, SYS_ARCH_PROTECT, SYS_ARCH_UNPROTECT, and U16_F.

Referenced by ethernetif_input(), pbuf_alloc(), pbuf_coalesce(), pbuf_dechain(), pbuf_free_header(), pbuf_free_int(), pbuf_realloc(), slipif_rxbyte_input(), and tcpip_thread_handle_msg().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pbuf_get_at()

u8_t pbuf_get_at ( const struct pbuf p,
u16_t  offset 
)

Get one byte from the specified position in a pbuf WARNING: returns zero for offset >= p->tot_len

Parameters
ppbuf to parse
offsetoffset into p of the byte to return
Returns
byte at an offset into p OR ZERO IF 'offset' >= p->tot_len

References pbuf_try_get_at().

Referenced by pbuf_memcmp().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pbuf_get_contiguous()

void* pbuf_get_contiguous ( const struct pbuf p,
void *  buffer,
size_t  bufsize,
u16_t  len,
u16_t  offset 
)

Get part of a pbuf's payload as contiguous memory. The returned memory is either a pointer into the pbuf's payload or, if split over multiple pbufs, a copy into the user-supplied buffer.

Parameters
pthe pbuf from which to copy data
bufferthe application supplied buffer
bufsizesize of the application supplied buffer
lenlength of data to copy (dataptr must be big enough). No more than buf->tot_len will be copied, irrespective of len
offsetoffset into the packet buffer from where to begin copying len bytes
Returns
the number of bytes copied, or 0 on failure

References len, LWIP_ERROR, NULL, payload, pbuf_copy_partial(), and pbuf_skip_const().

Here is the call graph for this function:

◆ pbuf_memcmp()

u16_t pbuf_memcmp ( const struct pbuf p,
u16_t  offset,
const void *  s2,
u16_t  n 
)

Compare pbuf contents at specified offset with memory s2, both of length n

Parameters
ppbuf to compare
offsetoffset into p at which to start comparing
s2buffer to compare
nlength of buffer to compare
Returns
zero if equal, nonzero otherwise (0xffff if p is too short, diffoffset+1 otherwise)

References len, LWIP_MIN, next, NULL, pbuf_get_at(), and tot_len.

Referenced by pbuf_memfind().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pbuf_memfind()

u16_t pbuf_memfind ( const struct pbuf p,
const void *  mem,
u16_t  mem_len,
u16_t  start_offset 
)

Find occurrence of mem (with length mem_len) in pbuf p, starting at offset start_offset.

Parameters
ppbuf to search, maximum length is 0xFFFE since 0xFFFF is used as return value 'not found'
memsearch for the contents of this buffer
mem_lenlength of 'mem'
start_offsetoffset into p at which to start searching
Returns
0xFFFF if substr was not found in p or the index where it was found

References pbuf_memcmp(), and tot_len.

Referenced by pbuf_strstr().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pbuf_put_at()

void pbuf_put_at ( struct pbuf p,
u16_t  offset,
u8_t  data 
)

Put one byte to the specified position in a pbuf WARNING: silently ignores offset >= p->tot_len

Parameters
ppbuf to fill
offsetoffset into p of the byte to write
databyte to write at an offset into p

References len, NULL, payload, and pbuf_skip().

Here is the call graph for this function:

◆ pbuf_realloc()

void pbuf_realloc ( struct pbuf p,
u16_t  new_len 
)

Shrink a pbuf chain to a desired length.

Parameters
ppbuf to shrink.
new_lendesired new length of pbuf chain

Depending on the desired length, the first few pbufs in a chain might be skipped and left unchanged. The new last pbuf in the chain will be resized, and any remaining pbufs will be freed.

Note
If the pbuf is ROM/REF, only the ->tot_len and ->len fields are adjusted.
May not be called on a packet queue.
Despite its name, pbuf_realloc cannot grow the size of a pbuf (chain).

References flags, len, LWIP_ASSERT, LWIP_SUPPORT_CUSTOM_PBUF, mem_trim(), next, NULL, payload, PBUF_FLAG_IS_CUSTOM, pbuf_free(), pbuf_match_allocsrc, PBUF_TYPE_ALLOC_SRC_MASK_STD_HEAP, and tot_len.

Referenced by slipif_rxbyte().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pbuf_ref()

void pbuf_ref ( struct pbuf p)

Increment the reference count of the pbuf.

Parameters
ppbuf to increase reference counter of

References LWIP_ASSERT, LWIP_PBUF_REF_T, NULL, ref, and SYS_ARCH_SET.

Referenced by pbuf_chain().

Here is the caller graph for this function:

◆ pbuf_skip()

struct pbuf* pbuf_skip ( struct pbuf in,
u16_t  in_offset,
u16_t out_offset 
)

Skip a number of bytes at the start of a pbuf

Parameters
ininput pbuf
in_offsetoffset to skip
out_offsetresulting offset in the returned pbuf
Returns
the pbuf in the queue where the offset is

References LWIP_CONST_CAST, and pbuf_skip_const().

Referenced by pbuf_put_at(), and pbuf_take_at().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pbuf_take()

err_t pbuf_take ( struct pbuf buf,
const void *  dataptr,
u16_t  len 
)

Copy application supplied data into a pbuf. This function can only be used to copy the equivalent of buf->tot_len data.

Parameters
bufpbuf to fill with data
dataptrapplication supplied data buffer
lenlength of the application supplied data buffer
Returns
ERR_OK if successful, ERR_MEM if the pbuf is not big enough

References ERR_ARG, ERR_MEM, ERR_OK, len, LWIP_ASSERT, LWIP_ERROR, MEMCPY, next, NULL, payload, and tot_len.

Referenced by pbuf_take_at().

Here is the caller graph for this function:

◆ pbuf_take_at()

err_t pbuf_take_at ( struct pbuf buf,
const void *  dataptr,
u16_t  len,
u16_t  offset 
)

Same as pbuf_take() but puts data at an offset

Parameters
bufpbuf to fill with data
dataptrapplication supplied data buffer
lenlength of the application supplied data buffer
offsetoffset in pbuf where to copy dataptr to
Returns
ERR_OK if successful, ERR_MEM if the pbuf is not big enough

References ERR_MEM, ERR_OK, len, LWIP_ASSERT, LWIP_MIN, MEMCPY, next, NULL, payload, pbuf_skip(), pbuf_take(), and tot_len.

Here is the call graph for this function:

◆ pbuf_try_get_at()

int pbuf_try_get_at ( const struct pbuf p,
u16_t  offset 
)

Get one byte from the specified position in a pbuf

Parameters
ppbuf to parse
offsetoffset into p of the byte to return
Returns
byte at an offset into p [0..0xFF] OR negative if 'offset' >= p->tot_len

References len, NULL, payload, and pbuf_skip_const().

Referenced by pbuf_get_at().

Here is the call graph for this function:
Here is the caller graph for this function: