Lee Kai Xuan / mbed-os

Fork of mbed-os by erkin yucel

Embed: (wiki syntax)

« Back to documentation index

pbuf.c File Reference

pbuf.c File Reference

Packet buffer management. More...

Go to the source code of this file.

Functions

static void pbuf_free_ooseq (void *arg)
 Attempt to reclaim some memory from queued out-of-sequence TCP segments if we run out of pool pbufs.
static void pbuf_pool_is_empty (void)
 Queue a call to pbuf_free_ooseq if not already queued.
struct pbufpbuf_alloc (pbuf_layer layer, u16_t length, pbuf_type type)
 Allocates a pbuf of the given type (possibly a chain for PBUF_POOL type).
struct pbufpbuf_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).
void pbuf_realloc (struct pbuf *p, u16_t new_len)
 Shrink a pbuf chain to a desired length.
u8_t pbuf_header (struct pbuf *p, s16_t header_size_increment)
 Adjusts the payload pointer to hide or reveal headers in the payload.
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.
u8_t pbuf_clen (struct pbuf *p)
 Count number of pbufs in a chain.
void pbuf_ref (struct pbuf *p)
 Increment the reference count of the pbuf.
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.
void pbuf_chain (struct pbuf *h, struct pbuf *t)
 Chain two pbufs (or pbuf chains) together.
struct pbufpbuf_dechain (struct pbuf *p)
 Dechains the first pbuf from its succeeding pbufs in the chain.
err_t pbuf_copy (struct pbuf *p_to, struct pbuf *p_from)
 Create PBUF_RAM copies of pbufs.
u16_t pbuf_copy_partial (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.
err_t pbuf_take (struct pbuf *buf, const void *dataptr, u16_t len)
 Copy application supplied data into a pbuf.
struct pbufpbuf_coalesce (struct pbuf *p, pbuf_layer layer)
 Creates a single pbuf out of a queue of pbufs.
err_t pbuf_fill_chksum (struct pbuf *p, u16_t start_offset, const void *dataptr, u16_t len, u16_t *chksum)
 Copies data into a single pbuf (*not* into a pbuf queue!) and updates the checksum while copying.
u8_t pbuf_get_at (struct pbuf *p, u16_t offset)
 Get one byte from the specified position in a pbuf WARNING: returns zero for offset >= p->tot_len.
u16_t pbuf_memcmp (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.
u16_t pbuf_memfind (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.
u16_t pbuf_strstr (struct pbuf *p, const char *substr)
 Find occurrence of substr with length substr_len in pbuf p, start at offset start_offset WARNING: in contrast to strstr(), this one does not stop at the first \0 in the pbuf/source string!

Detailed Description

Packet buffer management.

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

Definition in file pbuf.c.


Function Documentation

u8_t pbuf_clen ( struct pbuf p )

Count number of pbufs in a chain.

Parameters:
pfirst pbuf of chain
Returns:
the number of pbufs in a chain

Definition at line 681 of file pbuf.c.

struct pbuf* pbuf_dechain ( struct pbuf p ) [read]

Dechains the first pbuf from its succeeding pbufs in the chain.

Makes p->tot_len field equal to p->len.

Parameters:
ppbuf to dechain
Returns:
remainder of the pbuf chain, or NULL if it was de-allocated.
Note:
May not be called on a packet queue.

Definition at line 780 of file pbuf.c.

err_t pbuf_fill_chksum ( struct pbuf p,
u16_t  start_offset,
const void *  dataptr,
u16_t  len,
u16_t *  chksum 
)

Copies data into a single pbuf (*not* into a pbuf queue!) and updates the checksum while copying.

Parameters:
pthe pbuf to copy data into
start_offsetoffset of p->payload where to copy the data to
dataptrdata to copy into the pbuf
lenlength of data to copy into the pbuf
chksumpointer to the checksum which is updated
Returns:
ERR_OK if successful, another error if the data does not fit within the (first) pbuf (no pbuf queues!)

Definition at line 1021 of file pbuf.c.

static void pbuf_free_ooseq ( void *  arg ) [static]

Attempt to reclaim some memory from queued out-of-sequence TCP segments if we run out of pool pbufs.

It's better to give priority to new packets if we're running out.

This must be done in the correct thread context therefore this function can only be used with NO_SYS=0 and through tcpip_callback.

Free the ooseq pbufs of one PCB only

Definition at line 108 of file pbuf.c.

u8_t pbuf_header ( struct pbuf p,
s16_t  header_size_increment 
)

Adjusts the payload pointer to hide or reveal headers in the payload.

Adjusts the ->payload pointer so that space for a header (dis)appears in the pbuf payload.

The ->payload, ->tot_len and ->len fields are adjusted.

Parameters:
ppbuf to change the header size.
header_size_incrementNumber of bytes to increment header size which increases the size of the pbuf. New space is on the front. (Using a negative value decreases the header size.) If hdr_size_inc is 0, this function does nothing and returns succesful.

PBUF_ROM and PBUF_REF type buffers cannot have their sizes increased, so the call will fail. A check is made that the increase in header size does not move the payload pointer in front of the start of the buffer.

Returns:
non-zero on failure, zero on success.

Definition at line 488 of file pbuf.c.

static void pbuf_pool_is_empty ( void   ) [static]

Queue a call to pbuf_free_ooseq if not already queued.

Definition at line 131 of file pbuf.c.

u16_t pbuf_strstr ( struct pbuf p,
const char *  substr 
)

Find occurrence of substr with length substr_len in pbuf p, start at offset start_offset WARNING: in contrast to strstr(), this one does not stop at the first \0 in the pbuf/source string!

Parameters:
ppbuf to search, maximum length is 0xFFFE since 0xFFFF is used as return value 'not found'
substrstring to search for in p, maximum length is 0xFFFE
Returns:
0xFFFF if substr was not found in p or the index where it was found

Definition at line 1147 of file pbuf.c.