2003-03-25 12:59:42 +00:00
|
|
|
/**
|
|
|
|
* @file
|
2003-03-31 10:32:35 +00:00
|
|
|
* Packet buffer management
|
|
|
|
*
|
2003-05-01 07:48:01 +00:00
|
|
|
* Packets are built from the pbuf data structure. It supports dynamic
|
2003-03-31 10:32:35 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2003-05-01 07:48:01 +00:00
|
|
|
* A packet may span over multiple pbufs, chained as a singly linked
|
|
|
|
* list. This is called a "pbuf chain".
|
2003-03-31 10:32:35 +00:00
|
|
|
*
|
2003-05-18 22:29:10 +00:00
|
|
|
* Multiple packets may be queued, also using this singly linked list.
|
2004-04-30 00:25:03 +00:00
|
|
|
* 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, queues are only
|
|
|
|
* supported in a limited section of lwIP, this is the etharp queueing
|
|
|
|
* code. Outside of this section no packet queues are supported yet.
|
|
|
|
*
|
2003-06-06 08:34:37 +00:00
|
|
|
* The differences between a pbuf chain and a packet queue are very
|
2004-04-30 00:25:03 +00:00
|
|
|
* precise but subtle.
|
2003-05-18 22:29:10 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2003-06-06 08:34:37 +00:00
|
|
|
*
|
|
|
|
* Therefore, looping through a pbuf of a single packet, has an
|
|
|
|
* loop end condition (tot_len == p->len), NOT (next == NULL).
|
2003-03-25 12:59:42 +00:00
|
|
|
*/
|
2003-03-30 22:24:10 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
/*
|
2004-02-07 00:30:03 +00:00
|
|
|
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
2003-06-11 22:34:51 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
2002-10-19 12:59:30 +00:00
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
2003-06-11 22:34:51 +00:00
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
|
|
|
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
|
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
2002-10-19 12:59:30 +00:00
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the lwIP TCP/IP stack.
|
2003-06-11 22:34:51 +00:00
|
|
|
*
|
2002-10-19 12:59:30 +00:00
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2005-01-24 21:05:47 +00:00
|
|
|
#include <string.h>
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2005-01-24 21:05:47 +00:00
|
|
|
#include "lwip/opt.h"
|
2002-10-19 12:59:30 +00:00
|
|
|
#include "lwip/stats.h"
|
|
|
|
#include "lwip/def.h"
|
|
|
|
#include "lwip/mem.h"
|
|
|
|
#include "lwip/memp.h"
|
|
|
|
#include "lwip/pbuf.h"
|
|
|
|
#include "lwip/sys.h"
|
|
|
|
#include "arch/perf.h"
|
|
|
|
|
2006-02-27 14:11:19 +00:00
|
|
|
static u8_t pbuf_pool_memory[MEM_ALIGNMENT - 1 + PBUF_POOL_SIZE * MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE + sizeof(struct pbuf))];
|
2003-02-04 22:52:22 +00:00
|
|
|
|
2003-02-24 10:49:05 +00:00
|
|
|
#if !SYS_LIGHTWEIGHT_PROT
|
2002-10-19 12:59:30 +00:00
|
|
|
static volatile u8_t pbuf_pool_free_lock, pbuf_pool_alloc_lock;
|
|
|
|
static sys_sem_t pbuf_pool_free_sem;
|
2003-02-04 22:52:22 +00:00
|
|
|
#endif
|
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
static struct pbuf *pbuf_pool = NULL;
|
|
|
|
|
2003-03-30 22:24:10 +00:00
|
|
|
/**
|
|
|
|
* Initializes the pbuf module.
|
2002-10-19 12:59:30 +00:00
|
|
|
*
|
2003-03-30 22:24:10 +00:00
|
|
|
* A large part of memory is allocated for holding the pool of pbufs.
|
|
|
|
* The size of the individual pbufs in the pool is given by the size
|
|
|
|
* parameter, and the number of pbufs in the pool by the num parameter.
|
2002-10-19 12:59:30 +00:00
|
|
|
*
|
|
|
|
* After the memory has been allocated, the pbufs are set up. The
|
|
|
|
* ->next pointer in each pbuf is set up to point to the next pbuf in
|
|
|
|
* the pool.
|
2003-03-30 22:24:10 +00:00
|
|
|
*
|
2002-10-19 12:59:30 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
pbuf_init(void)
|
|
|
|
{
|
2003-03-30 22:24:10 +00:00
|
|
|
struct pbuf *p, *q = NULL;
|
2002-10-19 12:59:30 +00:00
|
|
|
u16_t i;
|
|
|
|
|
2006-02-27 14:11:19 +00:00
|
|
|
pbuf_pool = (struct pbuf *)MEM_ALIGN(pbuf_pool_memory);
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2007-03-11 20:07:37 +00:00
|
|
|
LWIP_ASSERT("pbuf_init: sizeof(struct pbuf) must be a multiple of MEM_ALIGNMENT",
|
|
|
|
(sizeof(struct pbuf) % MEM_ALIGNMENT) == 0);
|
2007-02-27 16:50:05 +00:00
|
|
|
LWIP_ASSERT("pbuf_init: PBUF_POOL_BUFSIZE not aligned",
|
|
|
|
(PBUF_POOL_BUFSIZE % MEM_ALIGNMENT) == 0);
|
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
#if PBUF_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
lwip_stats.pbuf.avail = PBUF_POOL_SIZE;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* PBUF_STATS */
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2003-03-25 12:59:42 +00:00
|
|
|
/* Set up ->next pointers to link the pbufs of the pool together */
|
2002-10-19 12:59:30 +00:00
|
|
|
p = pbuf_pool;
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
for(i = 0; i < PBUF_POOL_SIZE; ++i) {
|
|
|
|
p->next = (struct pbuf *)((u8_t *)p + PBUF_POOL_BUFSIZE + sizeof(struct pbuf));
|
|
|
|
p->len = p->tot_len = PBUF_POOL_BUFSIZE;
|
|
|
|
p->payload = MEM_ALIGN((void *)((u8_t *)p + sizeof(struct pbuf)));
|
2003-11-14 13:17:23 +00:00
|
|
|
p->flags = PBUF_FLAG_POOL;
|
2002-10-19 12:59:30 +00:00
|
|
|
q = p;
|
|
|
|
p = p->next;
|
|
|
|
}
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
/* The ->next pointer of last pbuf is NULL to indicate that there
|
2003-03-25 12:59:42 +00:00
|
|
|
are no more pbufs in the pool */
|
2002-10-19 12:59:30 +00:00
|
|
|
q->next = NULL;
|
|
|
|
|
2003-06-11 22:34:51 +00:00
|
|
|
#if !SYS_LIGHTWEIGHT_PROT
|
2002-10-19 12:59:30 +00:00
|
|
|
pbuf_pool_alloc_lock = 0;
|
|
|
|
pbuf_pool_free_lock = 0;
|
|
|
|
pbuf_pool_free_sem = sys_sem_new(1);
|
2003-06-11 22:34:51 +00:00
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-03-30 22:24:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal only called from pbuf_alloc()
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
static struct pbuf *
|
|
|
|
pbuf_pool_alloc(void)
|
|
|
|
{
|
|
|
|
struct pbuf *p = NULL;
|
|
|
|
|
2003-02-12 22:00:18 +00:00
|
|
|
SYS_ARCH_DECL_PROTECT(old_level);
|
|
|
|
SYS_ARCH_PROTECT(old_level);
|
2003-06-11 22:34:51 +00:00
|
|
|
|
|
|
|
#if !SYS_LIGHTWEIGHT_PROT
|
2003-04-11 14:39:15 +00:00
|
|
|
/* Next, check the actual pbuf pool, but if the pool is locked, we
|
|
|
|
pretend to be out of buffers and return NULL. */
|
2003-05-01 13:24:01 +00:00
|
|
|
if (pbuf_pool_free_lock) {
|
2003-11-14 13:17:23 +00:00
|
|
|
#if PBUF_STATS
|
2003-04-11 14:39:15 +00:00
|
|
|
++lwip_stats.pbuf.alloc_locked;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* PBUF_STATS */
|
2003-04-11 14:39:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
pbuf_pool_alloc_lock = 1;
|
2003-05-01 13:24:01 +00:00
|
|
|
if (!pbuf_pool_free_lock) {
|
2003-06-11 22:34:51 +00:00
|
|
|
#endif /* SYS_LIGHTWEIGHT_PROT */
|
2003-04-11 14:39:15 +00:00
|
|
|
p = pbuf_pool;
|
2003-05-01 13:24:01 +00:00
|
|
|
if (p) {
|
2003-06-11 22:34:51 +00:00
|
|
|
pbuf_pool = p->next;
|
2003-04-11 14:39:15 +00:00
|
|
|
}
|
2003-06-11 22:34:51 +00:00
|
|
|
#if !SYS_LIGHTWEIGHT_PROT
|
2003-11-14 13:17:23 +00:00
|
|
|
#if PBUF_STATS
|
2003-04-11 14:39:15 +00:00
|
|
|
} else {
|
|
|
|
++lwip_stats.pbuf.alloc_locked;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* PBUF_STATS */
|
|
|
|
}
|
2003-04-11 14:39:15 +00:00
|
|
|
pbuf_pool_alloc_lock = 0;
|
2003-06-11 22:34:51 +00:00
|
|
|
#endif /* SYS_LIGHTWEIGHT_PROT */
|
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
#if PBUF_STATS
|
2003-06-11 22:34:51 +00:00
|
|
|
if (p != NULL) {
|
2002-12-18 12:49:01 +00:00
|
|
|
++lwip_stats.pbuf.used;
|
2003-05-01 13:24:01 +00:00
|
|
|
if (lwip_stats.pbuf.used > lwip_stats.pbuf.max) {
|
2002-12-18 12:49:01 +00:00
|
|
|
lwip_stats.pbuf.max = lwip_stats.pbuf.used;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* PBUF_STATS */
|
|
|
|
|
2003-02-12 22:00:18 +00:00
|
|
|
SYS_ARCH_UNPROTECT(old_level);
|
2003-06-11 22:34:51 +00:00
|
|
|
return p;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-03-30 22:24:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2004-12-27 14:42:02 +00:00
|
|
|
* Allocates a pbuf of the given type (possibly a chain for PBUF_POOL type).
|
2003-03-30 22:24:10 +00:00
|
|
|
*
|
|
|
|
* The actual memory allocated for the pbuf is determined by the
|
|
|
|
* layer at which the pbuf is allocated and the requested size
|
2003-03-31 10:32:35 +00:00
|
|
|
* (from the size parameter).
|
|
|
|
*
|
|
|
|
* @param flag this parameter decides how and where the pbuf
|
|
|
|
* should be allocated as follows:
|
2003-06-11 22:34:51 +00:00
|
|
|
*
|
2003-03-30 22:24:10 +00:00
|
|
|
* - PBUF_RAM: buffer memory for pbuf is allocated as one large
|
2003-06-11 22:34:51 +00:00
|
|
|
* chunk. This includes protocol headers as well.
|
2003-03-30 22:24:10 +00:00
|
|
|
* - PBUF_ROM: no buffer memory is allocated for the pbuf, even for
|
2002-10-19 12:59:30 +00:00
|
|
|
* protocol headers. Additional headers must be prepended
|
|
|
|
* by allocating another pbuf and chain in to the front of
|
2003-03-19 22:14:49 +00:00
|
|
|
* 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.
|
2003-03-30 22:24:10 +00:00
|
|
|
* - PBUF_REF: no buffer memory is allocated for the pbuf, even for
|
2003-03-19 22:14:49 +00:00
|
|
|
* protocol headers. It is assumed that the pbuf is only
|
|
|
|
* being used in a single thread. If the pbuf gets queued,
|
2003-03-28 08:49:05 +00:00
|
|
|
* then pbuf_take should be called to copy the buffer.
|
2003-03-30 22:24:10 +00:00
|
|
|
* - PBUF_POOL: the pbuf is allocated as a pbuf chain, with pbufs from
|
2002-10-19 12:59:30 +00:00
|
|
|
* the pbuf pool that is allocated during pbuf_init().
|
2003-03-31 10:32:35 +00:00
|
|
|
*
|
|
|
|
* @return the allocated pbuf. If multiple pbufs where allocated, this
|
2003-06-11 22:34:51 +00:00
|
|
|
* is the first pbuf of a pbuf chain.
|
2002-10-19 12:59:30 +00:00
|
|
|
*/
|
|
|
|
struct pbuf *
|
2003-03-31 10:32:35 +00:00
|
|
|
pbuf_alloc(pbuf_layer l, u16_t length, pbuf_flag flag)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
|
|
|
struct pbuf *p, *q, *r;
|
|
|
|
u16_t offset;
|
2007-03-07 15:47:54 +00:00
|
|
|
s32_t rem_len; /* remaining length */
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE | 3, ("pbuf_alloc(length=%"U16_F")\n", length));
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2003-03-25 12:59:42 +00:00
|
|
|
/* determine header offset */
|
2002-10-19 12:59:30 +00:00
|
|
|
offset = 0;
|
2003-03-30 22:24:10 +00:00
|
|
|
switch (l) {
|
2002-10-19 12:59:30 +00:00
|
|
|
case PBUF_TRANSPORT:
|
2003-04-01 12:17:55 +00:00
|
|
|
/* add room for transport (often TCP) layer header */
|
2002-10-19 12:59:30 +00:00
|
|
|
offset += PBUF_TRANSPORT_HLEN;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case PBUF_IP:
|
2003-04-01 12:17:55 +00:00
|
|
|
/* add room for IP layer header */
|
2002-10-19 12:59:30 +00:00
|
|
|
offset += PBUF_IP_HLEN;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case PBUF_LINK:
|
2003-04-01 12:17:55 +00:00
|
|
|
/* add room for link layer header */
|
2002-11-22 08:32:31 +00:00
|
|
|
offset += PBUF_LINK_HLEN;
|
2002-10-19 12:59:30 +00:00
|
|
|
break;
|
|
|
|
case PBUF_RAW:
|
|
|
|
break;
|
|
|
|
default:
|
2003-02-06 22:18:56 +00:00
|
|
|
LWIP_ASSERT("pbuf_alloc: bad pbuf layer", 0);
|
2002-10-19 12:59:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-03-30 22:24:10 +00:00
|
|
|
switch (flag) {
|
2002-10-19 12:59:30 +00:00
|
|
|
case PBUF_POOL:
|
2003-03-25 12:59:42 +00:00
|
|
|
/* allocate head of pbuf chain into p */
|
2002-10-19 12:59:30 +00:00
|
|
|
p = pbuf_pool_alloc();
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE | 3, ("pbuf_alloc: allocated pbuf %p\n", (void *)p));
|
2003-03-30 22:24:10 +00:00
|
|
|
if (p == NULL) {
|
2003-11-14 13:17:23 +00:00
|
|
|
#if PBUF_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
++lwip_stats.pbuf.err;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* PBUF_STATS */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p->next = NULL;
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2003-04-01 12:17:55 +00:00
|
|
|
/* make the payload pointer point 'offset' bytes into pbuf data memory */
|
2002-10-19 12:59:30 +00:00
|
|
|
p->payload = MEM_ALIGN((void *)((u8_t *)p + (sizeof(struct pbuf) + offset)));
|
2003-04-01 12:17:55 +00:00
|
|
|
LWIP_ASSERT("pbuf_alloc: pbuf p->payload properly aligned",
|
2004-02-16 21:33:42 +00:00
|
|
|
((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
|
2003-04-01 12:17:55 +00:00
|
|
|
/* the total length of the pbuf chain is the requested size */
|
2003-03-31 10:32:35 +00:00
|
|
|
p->tot_len = length;
|
|
|
|
/* set the length of the first pbuf in the chain */
|
2007-03-11 20:07:37 +00:00
|
|
|
p->len = length > PBUF_POOL_BUFSIZE - MEM_ALIGN_SIZE(offset)? PBUF_POOL_BUFSIZE - MEM_ALIGN_SIZE(offset): length;
|
2003-06-04 19:09:11 +00:00
|
|
|
/* set reference count (needed here in case we fail) */
|
|
|
|
p->ref = 1;
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2003-04-01 12:17:55 +00:00
|
|
|
/* now allocate the tail of the pbuf chain */
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2003-04-01 12:17:55 +00:00
|
|
|
/* remember first pbuf for linkage in next iteration */
|
2002-10-19 12:59:30 +00:00
|
|
|
r = p;
|
2003-03-31 10:32:35 +00:00
|
|
|
/* remaining length to be allocated */
|
|
|
|
rem_len = length - p->len;
|
|
|
|
/* any remaining pbufs to be allocated? */
|
2003-06-11 22:34:51 +00:00
|
|
|
while (rem_len > 0) {
|
2002-10-19 12:59:30 +00:00
|
|
|
q = pbuf_pool_alloc();
|
2003-03-24 15:15:18 +00:00
|
|
|
if (q == NULL) {
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(PBUF_DEBUG | 2, ("pbuf_alloc: Out of pbufs in pool.\n"));
|
2003-11-14 13:17:23 +00:00
|
|
|
#if PBUF_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
++lwip_stats.pbuf.err;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* PBUF_STATS */
|
2003-06-04 19:09:11 +00:00
|
|
|
/* free chain so far allocated */
|
2003-04-11 14:39:15 +00:00
|
|
|
pbuf_free(p);
|
2003-06-04 19:09:11 +00:00
|
|
|
/* bail out unsuccesfully */
|
2002-10-19 12:59:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-06-04 19:09:11 +00:00
|
|
|
q->next = NULL;
|
2003-04-01 12:17:55 +00:00
|
|
|
/* make previous pbuf point to this pbuf */
|
2002-10-19 12:59:30 +00:00
|
|
|
r->next = q;
|
2003-06-04 09:06:22 +00:00
|
|
|
/* set total length of this pbuf and next in chain */
|
|
|
|
q->tot_len = rem_len;
|
|
|
|
/* this pbuf length is pool size, unless smaller sized tail */
|
2003-03-30 22:24:10 +00:00
|
|
|
q->len = rem_len > PBUF_POOL_BUFSIZE? PBUF_POOL_BUFSIZE: rem_len;
|
2002-10-19 12:59:30 +00:00
|
|
|
q->payload = (void *)((u8_t *)q + sizeof(struct pbuf));
|
2003-04-01 12:17:55 +00:00
|
|
|
LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned",
|
2004-02-16 21:33:42 +00:00
|
|
|
((mem_ptr_t)q->payload % MEM_ALIGNMENT) == 0);
|
2002-10-19 12:59:30 +00:00
|
|
|
q->ref = 1;
|
2003-04-01 12:17:55 +00:00
|
|
|
/* calculate remaining length to be allocated */
|
2007-03-07 15:47:54 +00:00
|
|
|
rem_len -= q->len;
|
2003-04-01 12:17:55 +00:00
|
|
|
/* remember this pbuf for linkage in next iteration */
|
|
|
|
r = q;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-03-24 15:15:18 +00:00
|
|
|
/* end of chain */
|
2003-06-27 20:46:11 +00:00
|
|
|
/*r->next = NULL;*/
|
2002-10-19 12:59:30 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
case PBUF_RAM:
|
|
|
|
/* If pbuf is to be allocated in RAM, allocate memory for it. */
|
2004-05-30 14:04:30 +00:00
|
|
|
p = mem_malloc(MEM_ALIGN_SIZE(sizeof(struct pbuf) + offset) + MEM_ALIGN_SIZE(length));
|
2003-03-30 22:24:10 +00:00
|
|
|
if (p == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* Set up internal structure of the pbuf. */
|
|
|
|
p->payload = MEM_ALIGN((void *)((u8_t *)p + sizeof(struct pbuf) + offset));
|
2003-03-31 10:32:35 +00:00
|
|
|
p->len = p->tot_len = length;
|
2002-10-19 12:59:30 +00:00
|
|
|
p->next = NULL;
|
|
|
|
p->flags = PBUF_FLAG_RAM;
|
|
|
|
|
2003-02-06 22:18:56 +00:00
|
|
|
LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned",
|
2004-02-16 21:33:42 +00:00
|
|
|
((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
|
2002-10-19 12:59:30 +00:00
|
|
|
break;
|
2004-12-27 14:42:02 +00:00
|
|
|
/* pbuf references existing (non-volatile static constant) ROM payload? */
|
2003-03-24 15:15:18 +00:00
|
|
|
case PBUF_ROM:
|
|
|
|
/* pbuf references existing (externally allocated) RAM payload? */
|
|
|
|
case PBUF_REF:
|
|
|
|
/* only allocate memory for the pbuf structure */
|
2003-06-27 20:46:11 +00:00
|
|
|
p = memp_malloc(MEMP_PBUF);
|
2003-03-30 22:24:10 +00:00
|
|
|
if (p == NULL) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE | 2, ("pbuf_alloc: Could not allocate MEMP_PBUF for PBUF_%s.\n", flag == PBUF_ROM?"ROM":"REF"));
|
2002-10-19 12:59:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-03-30 22:24:10 +00:00
|
|
|
/* caller must set this field properly, afterwards */
|
2002-10-19 12:59:30 +00:00
|
|
|
p->payload = NULL;
|
2003-03-31 10:32:35 +00:00
|
|
|
p->len = p->tot_len = length;
|
2002-10-19 12:59:30 +00:00
|
|
|
p->next = NULL;
|
2003-03-25 12:59:42 +00:00
|
|
|
p->flags = (flag == PBUF_ROM? PBUF_FLAG_ROM: PBUF_FLAG_REF);
|
2002-10-19 12:59:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
2003-02-06 22:18:56 +00:00
|
|
|
LWIP_ASSERT("pbuf_alloc: erroneous flag", 0);
|
2002-10-19 12:59:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-06-04 19:09:11 +00:00
|
|
|
/* set reference count */
|
2002-10-19 12:59:30 +00:00
|
|
|
p->ref = 1;
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE | 3, ("pbuf_alloc(length=%"U16_F") == %p\n", length, (void *)p));
|
2002-10-19 12:59:30 +00:00
|
|
|
return p;
|
|
|
|
}
|
2003-03-30 22:24:10 +00:00
|
|
|
|
2003-02-04 22:52:22 +00:00
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
#if PBUF_STATS
|
2003-02-04 22:52:22 +00:00
|
|
|
#define DEC_PBUF_STATS do { --lwip_stats.pbuf.used; } while (0)
|
|
|
|
#else /* PBUF_STATS */
|
|
|
|
#define DEC_PBUF_STATS
|
|
|
|
#endif /* PBUF_STATS */
|
|
|
|
|
|
|
|
#define PBUF_POOL_FAST_FREE(p) do { \
|
2003-04-11 14:39:15 +00:00
|
|
|
p->next = pbuf_pool; \
|
|
|
|
pbuf_pool = p; \
|
2003-02-04 22:52:22 +00:00
|
|
|
DEC_PBUF_STATS; \
|
|
|
|
} while (0)
|
|
|
|
|
2003-02-24 10:49:05 +00:00
|
|
|
#if SYS_LIGHTWEIGHT_PROT
|
2003-02-12 22:00:18 +00:00
|
|
|
#define PBUF_POOL_FREE(p) do { \
|
|
|
|
SYS_ARCH_DECL_PROTECT(old_level); \
|
|
|
|
SYS_ARCH_PROTECT(old_level); \
|
|
|
|
PBUF_POOL_FAST_FREE(p); \
|
|
|
|
SYS_ARCH_UNPROTECT(old_level); \
|
2003-05-01 13:24:01 +00:00
|
|
|
} while (0)
|
2003-02-04 22:52:22 +00:00
|
|
|
#else /* SYS_LIGHTWEIGHT_PROT */
|
|
|
|
#define PBUF_POOL_FREE(p) do { \
|
|
|
|
sys_sem_wait(pbuf_pool_free_sem); \
|
|
|
|
PBUF_POOL_FAST_FREE(p); \
|
|
|
|
sys_sem_signal(pbuf_pool_free_sem); \
|
2003-05-01 13:24:01 +00:00
|
|
|
} while (0)
|
2003-02-04 22:52:22 +00:00
|
|
|
#endif /* SYS_LIGHTWEIGHT_PROT */
|
2003-03-26 13:30:38 +00:00
|
|
|
|
|
|
|
/**
|
2003-03-30 22:24:10 +00:00
|
|
|
* Shrink a pbuf chain to a desired length.
|
2002-10-19 12:59:30 +00:00
|
|
|
*
|
2003-03-26 13:30:38 +00:00
|
|
|
* @param p pbuf to shrink.
|
2003-03-30 22:24:10 +00:00
|
|
|
* @param new_len desired new length of pbuf chain
|
2003-03-26 13:50:03 +00:00
|
|
|
*
|
2003-03-30 22:24:10 +00:00
|
|
|
* 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.
|
2003-06-11 22:34:51 +00:00
|
|
|
*
|
2003-03-26 13:50:03 +00:00
|
|
|
* @note If the pbuf is ROM/REF, only the ->tot_len and ->len fields are adjusted.
|
2003-05-18 22:29:10 +00:00
|
|
|
* @note May not be called on a packet queue.
|
2003-03-30 22:24:10 +00:00
|
|
|
*
|
|
|
|
* @bug Cannot grow the size of a pbuf (chain) (yet).
|
2002-10-19 12:59:30 +00:00
|
|
|
*/
|
2003-03-26 13:30:38 +00:00
|
|
|
void
|
|
|
|
pbuf_realloc(struct pbuf *p, u16_t new_len)
|
|
|
|
{
|
2003-03-31 08:34:02 +00:00
|
|
|
struct pbuf *q;
|
2003-03-26 13:30:38 +00:00
|
|
|
u16_t rem_len; /* remaining length */
|
2007-04-17 14:44:41 +00:00
|
|
|
s32_t grow;
|
2003-03-26 13:30:38 +00:00
|
|
|
|
|
|
|
LWIP_ASSERT("pbuf_realloc: sane p->flags", p->flags == PBUF_FLAG_POOL ||
|
|
|
|
p->flags == PBUF_FLAG_ROM ||
|
|
|
|
p->flags == PBUF_FLAG_RAM ||
|
|
|
|
p->flags == PBUF_FLAG_REF);
|
|
|
|
|
2003-03-30 22:24:10 +00:00
|
|
|
/* desired length larger than current length? */
|
2003-03-26 13:30:38 +00:00
|
|
|
if (new_len >= p->tot_len) {
|
2003-03-30 22:24:10 +00:00
|
|
|
/* enlarging not yet supported */
|
2003-03-26 13:30:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2003-03-30 22:24:10 +00:00
|
|
|
/* the pbuf chain grows by (new_len - p->tot_len) bytes
|
|
|
|
* (which may be negative in case of shrinking) */
|
2003-03-26 13:50:03 +00:00
|
|
|
grow = new_len - p->tot_len;
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2003-03-26 13:30:38 +00:00
|
|
|
/* first, step over any pbufs that should remain in the chain */
|
|
|
|
rem_len = new_len;
|
2003-06-11 22:34:51 +00:00
|
|
|
q = p;
|
2003-11-14 13:17:23 +00:00
|
|
|
/* should this pbuf be kept? */
|
2003-03-26 13:30:38 +00:00
|
|
|
while (rem_len > q->len) {
|
|
|
|
/* decrease remaining length by pbuf length */
|
2003-03-26 13:50:03 +00:00
|
|
|
rem_len -= q->len;
|
2003-03-30 22:24:10 +00:00
|
|
|
/* decrease total length indicator */
|
2003-03-26 13:50:03 +00:00
|
|
|
q->tot_len += grow;
|
2003-03-30 22:24:10 +00:00
|
|
|
/* proceed to next pbuf in chain */
|
2003-03-26 13:30:38 +00:00
|
|
|
q = q->next;
|
|
|
|
}
|
2003-03-30 22:24:10 +00:00
|
|
|
/* we have now reached the new last pbuf (in q) */
|
2003-06-11 22:34:51 +00:00
|
|
|
/* rem_len == desired length for pbuf q */
|
2003-03-26 13:30:38 +00:00
|
|
|
|
|
|
|
/* shrink allocated memory for PBUF_RAM */
|
|
|
|
/* (other types merely adjust their length fields */
|
2003-03-30 22:24:10 +00:00
|
|
|
if ((q->flags == PBUF_FLAG_RAM) && (rem_len != q->len)) {
|
2003-03-26 13:30:38 +00:00
|
|
|
/* reallocate and adjust the length of the pbuf that will be split */
|
|
|
|
mem_realloc(q, (u8_t *)q->payload - (u8_t *)q + rem_len);
|
|
|
|
}
|
2003-03-30 22:24:10 +00:00
|
|
|
/* adjust length fields for new last pbuf */
|
2003-03-26 13:30:38 +00:00
|
|
|
q->len = rem_len;
|
|
|
|
q->tot_len = q->len;
|
|
|
|
|
2003-03-30 22:24:10 +00:00
|
|
|
/* any remaining pbufs in chain? */
|
|
|
|
if (q->next != NULL) {
|
|
|
|
/* free remaining pbufs in chain */
|
|
|
|
pbuf_free(q->next);
|
|
|
|
}
|
2003-03-26 13:30:38 +00:00
|
|
|
/* q is last packet in chain */
|
|
|
|
q->next = NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2003-03-25 12:59:42 +00:00
|
|
|
/**
|
2003-04-01 12:17:55 +00:00
|
|
|
* Adjusts the payload pointer to hide or reveal headers in the payload.
|
2003-06-11 22:34:51 +00:00
|
|
|
*
|
2003-04-01 12:17:55 +00:00
|
|
|
* 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.
|
2003-01-30 12:38:03 +00:00
|
|
|
*
|
2004-05-05 15:09:13 +00:00
|
|
|
* @param hdr_size_inc Number of bytes to increment header size which
|
2003-03-31 20:45:08 +00:00
|
|
|
* increases the size of the pbuf. New space is on the front.
|
|
|
|
* (Using a negative value decreases the header size.)
|
2004-05-05 15:09:13 +00:00
|
|
|
* If hdr_size_inc is 0, this function does nothing and returns succesful.
|
2003-03-25 12:59:42 +00:00
|
|
|
*
|
2003-03-31 20:45:08 +00:00
|
|
|
* 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
|
2003-06-11 22:34:51 +00:00
|
|
|
* not move the payload pointer in front of the start of the buffer.
|
2004-05-05 14:12:28 +00:00
|
|
|
* @return non-zero on failure, zero on success.
|
2003-05-18 22:29:10 +00:00
|
|
|
*
|
2002-10-19 12:59:30 +00:00
|
|
|
*/
|
|
|
|
u8_t
|
2004-05-05 14:12:28 +00:00
|
|
|
pbuf_header(struct pbuf *p, s16_t header_size_increment)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2006-03-28 15:06:33 +00:00
|
|
|
u16_t flags;
|
2002-10-19 12:59:30 +00:00
|
|
|
void *payload;
|
2007-02-28 13:45:35 +00:00
|
|
|
u16_t increment_magnitude;
|
2003-03-31 10:32:35 +00:00
|
|
|
|
2004-05-05 14:12:28 +00:00
|
|
|
LWIP_ASSERT("p != NULL", p != NULL);
|
|
|
|
if ((header_size_increment == 0) || (p == NULL)) return 0;
|
|
|
|
|
2007-02-28 13:45:35 +00:00
|
|
|
if (header_size_increment < 0){
|
|
|
|
increment_magnitude = -header_size_increment;
|
|
|
|
/* Check that we aren't going to move off the end of the pbuf */
|
|
|
|
LWIP_ASSERT("increment_magnitude <= p->len", increment_magnitude <= p->len);
|
|
|
|
} else {
|
|
|
|
increment_magnitude = header_size_increment;
|
2007-03-21 12:55:00 +00:00
|
|
|
#if 0 /* Can't assert these as some callers speculatively call
|
|
|
|
pbuf_header() to see if it's OK. Will return 1 below instead. */
|
2007-02-28 13:45:35 +00:00
|
|
|
/* Check that we've got the correct type of pbuf to work with */
|
|
|
|
LWIP_ASSERT("p->flags == PBUF_FLAG_RAM || p->flags == PBUF_FLAG_POOL",
|
|
|
|
p->flags == PBUF_FLAG_RAM || p->flags == PBUF_FLAG_POOL);
|
|
|
|
/* Check that we aren't going to move off the beginning of the pbuf */
|
|
|
|
LWIP_ASSERT("p->payload - increment_magnitude >= p + sizeof(struct pbuf)",
|
|
|
|
(u8_t *)p->payload - increment_magnitude >= (u8_t *)p + sizeof(struct pbuf));
|
2007-03-21 12:55:00 +00:00
|
|
|
#endif
|
2007-02-28 13:45:35 +00:00
|
|
|
}
|
|
|
|
|
2006-03-28 15:06:33 +00:00
|
|
|
flags = p->flags;
|
2003-03-31 10:32:35 +00:00
|
|
|
/* remember current payload pointer */
|
2002-10-19 12:59:30 +00:00
|
|
|
payload = p->payload;
|
|
|
|
|
2003-03-31 10:32:35 +00:00
|
|
|
/* pbuf types containing payloads? */
|
2006-03-28 15:06:33 +00:00
|
|
|
if (flags == PBUF_FLAG_RAM || flags == PBUF_FLAG_POOL) {
|
2003-03-31 18:48:52 +00:00
|
|
|
/* set new payload pointer */
|
2004-05-05 14:12:28 +00:00
|
|
|
p->payload = (u8_t *)p->payload - header_size_increment;
|
2003-03-31 10:32:35 +00:00
|
|
|
/* boundary check fails? */
|
|
|
|
if ((u8_t *)p->payload < (u8_t *)p + sizeof(struct pbuf)) {
|
2004-05-05 14:12:28 +00:00
|
|
|
LWIP_DEBUGF( PBUF_DEBUG | 2, ("pbuf_header: failed as %p < %p (not enough space for new header size)\n",
|
2004-07-22 08:06:00 +00:00
|
|
|
(void *)p->payload,
|
|
|
|
(void *)(p + 1)));\
|
2003-03-31 10:32:35 +00:00
|
|
|
/* restore old payload pointer */
|
2003-03-31 11:39:48 +00:00
|
|
|
p->payload = payload;
|
2003-03-31 10:32:35 +00:00
|
|
|
/* bail out unsuccesfully */
|
|
|
|
return 1;
|
|
|
|
}
|
2004-05-05 14:12:28 +00:00
|
|
|
/* pbuf types refering to external payloads? */
|
2006-03-28 15:06:33 +00:00
|
|
|
} else if (flags == PBUF_FLAG_REF || flags == PBUF_FLAG_ROM) {
|
2003-03-31 18:48:52 +00:00
|
|
|
/* hide a header in the payload? */
|
2007-02-28 13:45:35 +00:00
|
|
|
if ((header_size_increment < 0) && (increment_magnitude <= p->len)) {
|
2003-03-31 18:48:52 +00:00
|
|
|
/* increase payload pointer */
|
2004-05-05 14:12:28 +00:00
|
|
|
p->payload = (u8_t *)p->payload - header_size_increment;
|
2003-03-31 18:48:52 +00:00
|
|
|
} else {
|
|
|
|
/* cannot expand payload to front (yet!)
|
|
|
|
* bail out unsuccesfully */
|
|
|
|
return 1;
|
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2007-03-21 12:55:00 +00:00
|
|
|
else {
|
|
|
|
/* Unknown type */
|
|
|
|
LWIP_ASSERT("bad pbuf flag type", 0);
|
|
|
|
return 1;
|
|
|
|
}
|
2003-03-31 10:32:35 +00:00
|
|
|
/* modify pbuf length fields */
|
2004-05-05 14:12:28 +00:00
|
|
|
p->len += header_size_increment;
|
|
|
|
p->tot_len += header_size_increment;
|
|
|
|
|
2005-11-25 12:03:38 +00:00
|
|
|
LWIP_DEBUGF( PBUF_DEBUG, ("pbuf_header: old %p new %p (%"S16_F")\n",
|
2004-05-05 14:12:28 +00:00
|
|
|
(void *)payload, (void *)p->payload, header_size_increment));
|
2003-03-31 10:32:35 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2003-03-28 08:49:05 +00:00
|
|
|
|
|
|
|
/**
|
2004-04-30 00:25:03 +00:00
|
|
|
* Dereference a pbuf chain or queue and deallocate any no-longer-used
|
|
|
|
* pbufs at the head of this chain or queue.
|
2003-03-28 08:49:05 +00:00
|
|
|
*
|
2004-05-05 14:12:28 +00:00
|
|
|
* Decrements the pbuf reference count. If it reaches zero, the pbuf is
|
|
|
|
* deallocated.
|
2003-03-28 08:49:05 +00:00
|
|
|
*
|
2003-12-28 02:38:51 +00:00
|
|
|
* For a pbuf chain, this is repeated for each pbuf in the chain,
|
2004-04-30 00:25:03 +00:00
|
|
|
* up to the first pbuf which has a non-zero reference count after
|
2004-07-25 23:41:01 +00:00
|
|
|
* decrementing. So, when all reference counts are one, the whole
|
|
|
|
* chain is free'd.
|
2002-10-19 12:59:30 +00:00
|
|
|
*
|
2003-12-28 02:38:51 +00:00
|
|
|
* @param pbuf The pbuf (chain) to be dereferenced.
|
2003-03-28 08:49:05 +00:00
|
|
|
*
|
2003-12-28 02:38:51 +00:00
|
|
|
* @return the number of pbufs that were de-allocated
|
2003-03-30 22:24:10 +00:00
|
|
|
* from the head of the chain.
|
|
|
|
*
|
2004-05-05 14:12:28 +00:00
|
|
|
* @note MUST NOT be called on a packet queue (Not verified to work yet).
|
2003-03-30 22:24:10 +00:00
|
|
|
* @note the reference counter of a pbuf equals the number of pointers
|
|
|
|
* that refer to the pbuf (or into the pbuf).
|
|
|
|
*
|
|
|
|
* @internal examples:
|
2003-03-28 08:49:05 +00:00
|
|
|
*
|
2003-12-28 02:38:51 +00:00
|
|
|
* Assuming existing chains a->b->c with the following reference
|
|
|
|
* counts, calling pbuf_free(a) results in:
|
|
|
|
*
|
2003-03-30 22:24:10 +00:00
|
|
|
* 1->2->3 becomes ...1->3
|
|
|
|
* 3->3->3 becomes 2->3->3
|
2003-12-28 02:38:51 +00:00
|
|
|
* 1->1->2 becomes ......1
|
2003-03-30 22:24:10 +00:00
|
|
|
* 2->1->1 becomes 1->1->1
|
|
|
|
* 1->1->1 becomes .......
|
2003-06-11 22:34:51 +00:00
|
|
|
*
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
u8_t
|
|
|
|
pbuf_free(struct pbuf *p)
|
|
|
|
{
|
2006-03-28 15:06:33 +00:00
|
|
|
u16_t flags;
|
2002-10-19 12:59:30 +00:00
|
|
|
struct pbuf *q;
|
2003-03-30 22:24:10 +00:00
|
|
|
u8_t count;
|
2003-03-24 15:15:18 +00:00
|
|
|
|
2004-04-30 00:25:03 +00:00
|
|
|
LWIP_ASSERT("p != NULL", p != NULL);
|
|
|
|
/* if assertions are disabled, proceed with debug output */
|
2003-03-25 12:59:42 +00:00
|
|
|
if (p == NULL) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE | 2, ("pbuf_free(p == NULL) was called.\n"));
|
2002-10-19 12:59:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE | 3, ("pbuf_free(%p)\n", (void *)p));
|
2002-10-19 12:59:30 +00:00
|
|
|
|
|
|
|
PERF_START;
|
2003-03-24 15:15:18 +00:00
|
|
|
|
2003-04-01 12:28:45 +00:00
|
|
|
LWIP_ASSERT("pbuf_free: sane flags",
|
|
|
|
p->flags == PBUF_FLAG_RAM || p->flags == PBUF_FLAG_ROM ||
|
|
|
|
p->flags == PBUF_FLAG_REF || p->flags == PBUF_FLAG_POOL);
|
2003-02-04 22:52:22 +00:00
|
|
|
|
2003-03-30 22:24:10 +00:00
|
|
|
count = 0;
|
|
|
|
/* de-allocate all consecutive pbufs from the head of the chain that
|
2003-04-16 07:39:48 +00:00
|
|
|
* obtain a zero reference count after decrementing*/
|
2003-03-30 22:24:10 +00:00
|
|
|
while (p != NULL) {
|
2007-03-04 12:45:58 +00:00
|
|
|
u16_t ref;
|
|
|
|
SYS_ARCH_DECL_PROTECT(old_level);
|
|
|
|
/* Since decrementing ref cannot be guaranteed to be a single machine operation
|
|
|
|
* we must protect it. Also, the later test of ref must be protected.
|
|
|
|
*/
|
|
|
|
SYS_ARCH_PROTECT(old_level);
|
2003-03-30 22:24:10 +00:00
|
|
|
/* all pbufs in a chain are referenced at least once */
|
2003-04-01 12:28:45 +00:00
|
|
|
LWIP_ASSERT("pbuf_free: p->ref > 0", p->ref > 0);
|
2003-04-16 07:39:48 +00:00
|
|
|
/* decrease reference count (number of pointers to pbuf) */
|
2007-03-04 17:21:43 +00:00
|
|
|
ref = --(p->ref);
|
2007-03-04 12:45:58 +00:00
|
|
|
SYS_ARCH_UNPROTECT(old_level);
|
2003-03-30 22:24:10 +00:00
|
|
|
/* this pbuf is no longer referenced to? */
|
2007-03-04 12:45:58 +00:00
|
|
|
if (ref == 0) {
|
2003-03-30 22:24:10 +00:00
|
|
|
/* remember next pbuf in chain for next iteration */
|
2003-03-25 12:59:42 +00:00
|
|
|
q = p->next;
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF( PBUF_DEBUG | 2, ("pbuf_free: deallocating %p\n", (void *)p));
|
2006-03-28 15:06:33 +00:00
|
|
|
flags = p->flags;
|
2003-03-30 22:24:10 +00:00
|
|
|
/* is this a pbuf from the pool? */
|
2006-03-28 15:06:33 +00:00
|
|
|
if (flags == PBUF_FLAG_POOL) {
|
2003-03-24 15:15:18 +00:00
|
|
|
p->len = p->tot_len = PBUF_POOL_BUFSIZE;
|
|
|
|
p->payload = (void *)((u8_t *)p + sizeof(struct pbuf));
|
|
|
|
PBUF_POOL_FREE(p);
|
2004-07-25 23:41:01 +00:00
|
|
|
/* is this a ROM or RAM referencing pbuf? */
|
2006-03-28 15:06:33 +00:00
|
|
|
} else if (flags == PBUF_FLAG_ROM || flags == PBUF_FLAG_REF) {
|
2003-06-27 20:46:11 +00:00
|
|
|
memp_free(MEMP_PBUF, p);
|
2006-03-28 15:06:33 +00:00
|
|
|
/* flags == PBUF_FLAG_RAM */
|
2003-03-25 12:59:42 +00:00
|
|
|
} else {
|
|
|
|
mem_free(p);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-03-30 22:24:10 +00:00
|
|
|
count++;
|
|
|
|
/* proceed to next pbuf */
|
2003-03-24 15:15:18 +00:00
|
|
|
p = q;
|
2003-03-30 22:24:10 +00:00
|
|
|
/* p->ref > 0, this pbuf is still referenced to */
|
2003-04-16 07:39:48 +00:00
|
|
|
/* (and so the remaining pbufs in chain as well) */
|
2003-03-30 22:24:10 +00:00
|
|
|
} else {
|
2007-03-04 12:45:58 +00:00
|
|
|
LWIP_DEBUGF( PBUF_DEBUG | 2, ("pbuf_free: %p has ref %"U16_F", ending here.\n", (void *)p, ref));
|
2004-07-25 23:41:01 +00:00
|
|
|
/* stop walking through the chain */
|
2003-03-30 22:24:10 +00:00
|
|
|
p = NULL;
|
2003-03-24 15:15:18 +00:00
|
|
|
}
|
2003-03-30 22:24:10 +00:00
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
PERF_STOP("pbuf_free");
|
2003-03-30 22:24:10 +00:00
|
|
|
/* return number of de-allocated pbufs */
|
2002-10-19 12:59:30 +00:00
|
|
|
return count;
|
|
|
|
}
|
2003-03-30 22:24:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Count number of pbufs in a chain
|
2002-10-19 12:59:30 +00:00
|
|
|
*
|
2003-03-30 22:24:10 +00:00
|
|
|
* @param p first pbuf of chain
|
|
|
|
* @return the number of pbufs in a chain
|
2002-10-19 12:59:30 +00:00
|
|
|
*/
|
2003-03-30 22:24:10 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
u8_t
|
|
|
|
pbuf_clen(struct pbuf *p)
|
|
|
|
{
|
|
|
|
u8_t len;
|
|
|
|
|
2003-06-11 22:34:51 +00:00
|
|
|
len = 0;
|
2003-03-30 22:24:10 +00:00
|
|
|
while (p != NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
++len;
|
2003-03-30 22:24:10 +00:00
|
|
|
p = p->next;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
2003-05-18 22:29:10 +00:00
|
|
|
|
2003-03-30 22:24:10 +00:00
|
|
|
/**
|
|
|
|
* Increment the reference count of the pbuf.
|
|
|
|
*
|
|
|
|
* @param p pbuf to increase reference counter of
|
2002-10-19 12:59:30 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pbuf_ref(struct pbuf *p)
|
|
|
|
{
|
2003-03-30 22:24:10 +00:00
|
|
|
SYS_ARCH_DECL_PROTECT(old_level);
|
2003-06-11 22:34:51 +00:00
|
|
|
/* pbuf given? */
|
2003-04-01 12:28:45 +00:00
|
|
|
if (p != NULL) {
|
2003-03-30 22:24:10 +00:00
|
|
|
SYS_ARCH_PROTECT(old_level);
|
|
|
|
++(p->ref);
|
|
|
|
SYS_ARCH_UNPROTECT(old_level);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
2002-11-11 11:22:49 +00:00
|
|
|
|
2003-03-30 22:24:10 +00:00
|
|
|
/**
|
2003-11-14 13:17:23 +00:00
|
|
|
* Concatenate two pbufs (each may be a pbuf chain) and take over
|
2003-12-28 02:38:51 +00:00
|
|
|
* the caller's reference of the tail pbuf.
|
2003-11-14 13:17:23 +00:00
|
|
|
*
|
2003-12-28 02:38:51 +00:00
|
|
|
* @note The caller MAY NOT reference the tail pbuf afterwards.
|
|
|
|
* Use pbuf_chain() for that purpose.
|
2003-11-14 13:17:23 +00:00
|
|
|
*
|
2003-12-28 02:38:51 +00:00
|
|
|
* @see pbuf_chain()
|
2002-10-19 12:59:30 +00:00
|
|
|
*/
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
void
|
2003-11-14 13:17:23 +00:00
|
|
|
pbuf_cat(struct pbuf *h, struct pbuf *t)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
|
|
|
struct pbuf *p;
|
|
|
|
|
2004-12-27 14:42:02 +00:00
|
|
|
LWIP_ASSERT("h != NULL (programmer violates API)", h != NULL);
|
|
|
|
LWIP_ASSERT("t != NULL (programmer violates API)", t != NULL);
|
2003-12-28 02:38:51 +00:00
|
|
|
if ((h == NULL) || (t == NULL)) return;
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2003-03-28 08:49:05 +00:00
|
|
|
/* proceed to last pbuf of chain */
|
2003-04-01 09:35:20 +00:00
|
|
|
for (p = h; p->next != NULL; p = p->next) {
|
2003-03-31 11:39:48 +00:00
|
|
|
/* add total length of second chain to all totals of first chain */
|
2003-03-28 08:49:05 +00:00
|
|
|
p->tot_len += t->tot_len;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-12-28 02:38:51 +00:00
|
|
|
/* { p is last pbuf of first h chain, p->next == NULL } */
|
2003-05-01 07:48:01 +00:00
|
|
|
LWIP_ASSERT("p->tot_len == p->len (of last pbuf in chain)", p->tot_len == p->len);
|
2004-12-27 14:42:02 +00:00
|
|
|
LWIP_ASSERT("p->next == NULL", p->next == NULL);
|
2003-04-01 09:35:20 +00:00
|
|
|
/* add total length of second chain to last pbuf total of first chain */
|
|
|
|
p->tot_len += t->tot_len;
|
2003-11-14 13:17:23 +00:00
|
|
|
/* chain last pbuf of head (p) with first of tail (t) */
|
2002-10-19 12:59:30 +00:00
|
|
|
p->next = t;
|
2004-12-27 14:42:02 +00:00
|
|
|
/* p->next now references t, but the caller will drop its reference to t,
|
|
|
|
* so netto there is no change to the reference count of t.
|
|
|
|
*/
|
2003-11-14 13:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-12-28 02:38:51 +00:00
|
|
|
* 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.
|
2003-11-14 13:17:23 +00:00
|
|
|
*
|
|
|
|
* @param h head pbuf (chain)
|
|
|
|
* @param t tail pbuf (chain)
|
2003-12-28 02:38:51 +00:00
|
|
|
* @note The pbufs MUST belong to the same packet.
|
|
|
|
* @note MAY NOT be called on a packet queue.
|
2003-11-14 13:17:23 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pbuf_chain(struct pbuf *h, struct pbuf *t)
|
|
|
|
{
|
|
|
|
pbuf_cat(h, t);
|
2003-12-28 02:38:51 +00:00
|
|
|
/* t is now referenced by h */
|
2003-03-28 08:49:05 +00:00
|
|
|
pbuf_ref(t);
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_FRESH | 2, ("pbuf_chain: %p references %p\n", (void *)h, (void *)t));
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-03-25 12:59:42 +00:00
|
|
|
|
2003-04-14 11:37:12 +00:00
|
|
|
/**
|
|
|
|
* Dechains the first pbuf from its succeeding pbufs in the chain.
|
|
|
|
*
|
|
|
|
* Makes p->tot_len field equal to p->len.
|
|
|
|
* @param p pbuf to dechain
|
|
|
|
* @return remainder of the pbuf chain, or NULL if it was de-allocated.
|
2003-05-18 22:29:10 +00:00
|
|
|
* @note May not be called on a packet queue.
|
2003-04-14 11:37:12 +00:00
|
|
|
*/
|
|
|
|
struct pbuf *
|
|
|
|
pbuf_dechain(struct pbuf *p)
|
|
|
|
{
|
|
|
|
struct pbuf *q;
|
|
|
|
u8_t tail_gone = 1;
|
2003-06-11 22:34:51 +00:00
|
|
|
/* tail */
|
2003-04-14 11:37:12 +00:00
|
|
|
q = p->next;
|
|
|
|
/* pbuf has successor in chain? */
|
|
|
|
if (q != NULL) {
|
|
|
|
/* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
|
|
|
|
LWIP_ASSERT("p->tot_len == p->len + q->tot_len", q->tot_len == p->tot_len - p->len);
|
|
|
|
/* enforce invariant if assertion is disabled */
|
|
|
|
q->tot_len = p->tot_len - p->len;
|
|
|
|
/* decouple pbuf from remainder */
|
|
|
|
p->next = NULL;
|
|
|
|
/* total length of pbuf p is its own length only */
|
|
|
|
p->tot_len = p->len;
|
|
|
|
/* q is no longer referenced by p, free it */
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_STATE, ("pbuf_dechain: unreferencing %p\n", (void *)q));
|
2003-04-14 11:37:12 +00:00
|
|
|
tail_gone = pbuf_free(q);
|
2004-07-27 16:08:05 +00:00
|
|
|
if (tail_gone > 0) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_STATE,
|
2004-07-27 16:08:05 +00:00
|
|
|
("pbuf_dechain: deallocated %p (as it is no longer referenced)\n", (void *)q));
|
|
|
|
}
|
2003-04-14 11:37:12 +00:00
|
|
|
/* return remaining tail or NULL if deallocated */
|
|
|
|
}
|
|
|
|
/* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
|
|
|
|
LWIP_ASSERT("p->tot_len == p->len", p->tot_len == p->len);
|
|
|
|
return (tail_gone > 0? NULL: q);
|
|
|
|
}
|
2007-04-11 18:50:45 +00:00
|
|
|
|
|
|
|
#if ARP_QUEUEING
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* 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);
|
|
|
|
*
|
|
|
|
* @note Only one packet is copied, no packet queue!
|
|
|
|
*
|
|
|
|
* @param p Head of pbuf chain to process
|
|
|
|
*
|
|
|
|
* @return Pointer to head of pbuf chain
|
|
|
|
*/
|
|
|
|
struct pbuf *
|
|
|
|
pbuf_copy(struct pbuf *p)
|
|
|
|
{
|
|
|
|
u16_t copied=0;
|
|
|
|
struct pbuf *p_copy;
|
|
|
|
LWIP_ASSERT("pbuf_copy: p != NULL\n", p != NULL);
|
|
|
|
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE | 3, ("pbuf_copy(%p)\n", (void*)p));
|
|
|
|
|
|
|
|
/* allocate one pbuf to hold the complete packet */
|
|
|
|
p_copy = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
|
|
|
|
if(p_copy == NULL) {
|
|
|
|
/* out of memory */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* iterate through pbuf chain */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* copy one part of the original chain */
|
|
|
|
memcpy((char*)p_copy->payload + copied, p->payload, p->len);
|
|
|
|
copied += p->len;
|
|
|
|
LWIP_DEBUGF(PBUF_DEBUG, ("pbuf_copy: copied pbuf %p\n", (void *)p));
|
|
|
|
|
|
|
|
if(p->len == p->tot_len) {
|
|
|
|
/* don't copy more than one packet! */
|
|
|
|
LWIP_ASSERT("pbuf_copy() does not allow packet queues!\n",
|
|
|
|
p->next == NULL);
|
|
|
|
}
|
|
|
|
/* proceed to next pbuf in original chain */
|
|
|
|
p = p->next;
|
|
|
|
} while (p);
|
|
|
|
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE | 1, ("pbuf_copy: end of chain reached.\n"));
|
|
|
|
|
|
|
|
return p_copy;
|
|
|
|
}
|
|
|
|
#endif /* ARP_QUEUEING */
|