lwIP
2.1.0
Lightweight IP stack
|
Macros | |
#define | PBUF_NEEDS_COPY(p) ((p)->type_internal & PBUF_TYPE_FLAG_DATA_VOLATILE) |
Enumerations | |
enum | pbuf_layer { PBUF_TRANSPORT = 0 + (14 + 0 ) + 40 + 20, PBUF_IP = 0 + (14 + 0 ) + 40, PBUF_LINK = 0 + (14 + 0 ), PBUF_RAW_TX = 0, PBUF_RAW = 0 } |
enum | pbuf_type { PBUF_RAM = ( 0x0200 | 0x80 | 0x00 ), PBUF_ROM = 0x01, PBUF_REF = ( 0x40 | 0x01 ), PBUF_POOL = ( 0x0100 | 0x80 | 0x02 ) } |
Functions | |
struct pbuf * | pbuf_alloc (pbuf_layer layer, u16_t length, pbuf_type type) |
struct pbuf * | pbuf_alloc_reference (void *payload, u16_t length, pbuf_type type) |
struct pbuf * | pbuf_alloced_custom (pbuf_layer l, u16_t length, pbuf_type type, struct pbuf_custom *p, void *payload_mem, u16_t payload_mem_len) |
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 pbuf * | pbuf_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 pbuf * | pbuf_coalesce (struct pbuf *p, pbuf_layer layer) |
struct pbuf * | pbuf_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) |
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: Zero-copy RX
#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.
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().
|
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().
|
PBUF_RAW | Use this for input packets in a netif driver when calling netif->input() in the most common case - ethernet-layer netif driver. |
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! |
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).
layer | header size |
length | size of the pbuf's payload |
type | this parameter decides how and where the pbuf should be allocated as follows: |
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).
payload | referenced payload |
length | size of the pbuf's payload |
type | this parameter decides how and where the pbuf should be allocated as follows: |
struct pbuf* pbuf_alloced_custom | ( | pbuf_layer | l, |
u16_t | length, | ||
pbuf_type | type, | ||
struct pbuf_custom * | p, | ||
void * | payload_mem, | ||
u16_t | payload_mem_len | ||
) |
Initialize a custom pbuf (already allocated). Example of custom pbuf usage: Zero-copy RX
l | header size |
length | size of the pbuf's payload |
type | type of the pbuf (only used to treat the pbuf accordingly, as this function allocates no memory) |
p | pointer to the custom pbuf to initialize (already allocated) |
payload_mem | pointer to the buffer that is used for payload and headers, must be at least big enough to hold 'length' plus the header size, may be NULL if set later. ATTENTION: The caller is responsible for correct alignment of this buffer!! |
payload_mem_len | the size of the 'payload_mem' buffer, must be at least big enough to hold 'length' plus the header size |
Concatenate two pbufs (each may be a pbuf chain) and take over the caller's reference of the tail pbuf.
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!
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.
h | head pbuf (chain) |
t | tail pbuf (chain) |
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.
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()).
layer | pbuf_layer of the new pbuf |
type | this parameter decides how and where the pbuf should be allocated ( |
p | the source pbuf |
struct pbuf* pbuf_coalesce | ( | struct pbuf * | p, |
pbuf_layer | layer | ||
) |
Creates a single pbuf out of a queue of pbufs.
p | the source pbuf |
layer | pbuf_layer of the new pbuf |
Create PBUF_RAM copies of pbufs.
Used to queue packets on behalf of the lwIP stack, such as ARP based queueing.
p_to | pbuf destination of the copy |
p_from | pbuf source of the copy |
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.
buf | the pbuf from which to copy data |
dataptr | the application supplied buffer |
len | length of data to copy (dataptr must be big enough). No more than buf->tot_len will be copied, irrespective of len |
offset | offset into the packet buffer from where to begin copying len bytes |
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.
p | The pbuf (chain) to be dereferenced. |
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
p | pbuf to parse |
offset | offset into p of the byte to return |
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.
p | the pbuf from which to copy data |
buffer | the application supplied buffer |
bufsize | size of the application supplied buffer |
len | length of data to copy (dataptr must be big enough). No more than buf->tot_len will be copied, irrespective of len |
offset | offset into the packet buffer from where to begin copying len bytes |
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
p | pbuf to compare |
offset | offset into p at which to start comparing |
s2 | buffer to compare |
n | length of buffer to compare |
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.
p | pbuf to search, maximum length is 0xFFFE since 0xFFFF is used as return value 'not found' |
mem | search for the contents of this buffer |
mem_len | length of 'mem' |
start_offset | offset into p at which to start searching |
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
p | pbuf to fill |
offset | offset into p of the byte to write |
data | byte to write at an offset into p |
void pbuf_realloc | ( | struct pbuf * | p, |
u16_t | new_len | ||
) |
Shrink a pbuf chain to a desired length.
p | pbuf to shrink. |
new_len | desired 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.
void pbuf_ref | ( | struct pbuf * | p | ) |
Increment the reference count of the pbuf.
p | pbuf to increase reference counter of |
Skip a number of bytes at the start of a pbuf
in | input pbuf |
in_offset | offset to skip |
out_offset | resulting offset in the returned pbuf |
Copy application supplied data into a pbuf. This function can only be used to copy the equivalent of buf->tot_len data.
buf | pbuf to fill with data |
dataptr | application supplied data buffer |
len | length of the application supplied data buffer |
Same as pbuf_take() but puts data at an offset
buf | pbuf to fill with data |
dataptr | application supplied data buffer |
len | length of the application supplied data buffer |
offset | offset in pbuf where to copy dataptr to |
int pbuf_try_get_at | ( | const struct pbuf * | p, |
u16_t | offset | ||
) |
Get one byte from the specified position in a pbuf
p | pbuf to parse |
offset | offset into p of the byte to return |