⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pbuf.c

📁 lwip-1.4.0
💻 C
📖 第 1 页 / 共 3 页
字号:
/** * @file * 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). *//* * Copyright (c) 2001-2004 Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * 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 *    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 * OF SUCH DAMAGE. * * This file is part of the lwIP TCP/IP stack. * * Author: Adam Dunkels <adam@sics.se> * */#include "lwip/opt.h"#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"#if TCP_QUEUE_OOSEQ#include "lwip/tcp_impl.h"#endif#if LWIP_CHECKSUM_ON_COPY#include "lwip/inet_chksum.h"#endif#include <string.h>#define SIZEOF_STRUCT_PBUF        LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf))/* Since the pool is created in memp, PBUF_POOL_BUFSIZE will be automatically   aligned there. Therefore, PBUF_POOL_BUFSIZE_ALIGNED can be used here. */#define PBUF_POOL_BUFSIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE)#if !LWIP_TCP || !TCP_QUEUE_OOSEQ || NO_SYS#define PBUF_POOL_IS_EMPTY()#else /* !LWIP_TCP || !TCP_QUEUE_OOSEQ || NO_SYS *//** Define this to 0 to prevent freeing ooseq pbufs when the PBUF_POOL is empty */#ifndef PBUF_POOL_FREE_OOSEQ#define PBUF_POOL_FREE_OOSEQ 1#endif /* PBUF_POOL_FREE_OOSEQ */#if PBUF_POOL_FREE_OOSEQ#include "lwip/tcpip.h"#define PBUF_POOL_IS_EMPTY() pbuf_pool_is_empty()static u8_t pbuf_free_ooseq_queued;/** * 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. */static voidpbuf_free_ooseq(void* arg){  struct tcp_pcb* pcb;  SYS_ARCH_DECL_PROTECT(old_level);  LWIP_UNUSED_ARG(arg);  SYS_ARCH_PROTECT(old_level);  pbuf_free_ooseq_queued = 0;  SYS_ARCH_UNPROTECT(old_level);  for (pcb = tcp_active_pcbs; NULL != pcb; pcb = pcb->next) {    if (NULL != pcb->ooseq) {      /** Free the ooseq pbufs of one PCB only */      LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free_ooseq: freeing out-of-sequence pbufs\n"));      tcp_segs_free(pcb->ooseq);      pcb->ooseq = NULL;      return;    }  }}/** Queue a call to pbuf_free_ooseq if not already queued. */static voidpbuf_pool_is_empty(void){  u8_t queued;  SYS_ARCH_DECL_PROTECT(old_level);  SYS_ARCH_PROTECT(old_level);  queued = pbuf_free_ooseq_queued;  pbuf_free_ooseq_queued = 1;  SYS_ARCH_UNPROTECT(old_level);  if(!queued) {    /* queue a call to pbuf_free_ooseq if not already queued */    if(tcpip_callback_with_block(pbuf_free_ooseq, NULL, 0) != ERR_OK) {      SYS_ARCH_PROTECT(old_level);      pbuf_free_ooseq_queued = 0;      SYS_ARCH_UNPROTECT(old_level);    }  }}#endif /* PBUF_POOL_FREE_OOSEQ */#endif /* !LWIP_TCP || !TCP_QUEUE_OOSEQ || NO_SYS *//** * 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). * * @param layer flag to define header size * @param length size of the pbuf's payload * @param type this 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(). * * @return the allocated pbuf. If multiple pbufs where allocated, this * is the first pbuf of a pbuf chain. */struct pbuf *pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type){  struct pbuf *p, *q, *r;  u16_t offset;  s32_t rem_len; /* remaining length */  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F")\n", length));  /* determine header offset */  offset = 0;  switch (layer) {  case PBUF_TRANSPORT:    /* add room for transport (often TCP) layer header */    offset += PBUF_TRANSPORT_HLEN;    /* FALLTHROUGH */  case PBUF_IP:    /* add room for IP layer header */    offset += PBUF_IP_HLEN;    /* FALLTHROUGH */  case PBUF_LINK:    /* add room for link layer header */    offset += PBUF_LINK_HLEN;    break;  case PBUF_RAW:    break;  default:    LWIP_ASSERT("pbuf_alloc: bad pbuf layer", 0);    return NULL;  }  switch (type) {  case PBUF_POOL:    /* allocate head of pbuf chain into p */    p = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);    LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc: allocated pbuf %p\n", (void *)p));    if (p == NULL) {      PBUF_POOL_IS_EMPTY();      return NULL;    }    p->type = type;    p->next = NULL;    /* make the payload pointer point 'offset' bytes into pbuf data memory */    p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + (SIZEOF_STRUCT_PBUF + offset)));    LWIP_ASSERT("pbuf_alloc: pbuf p->payload properly aligned",            ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);    /* the total length of the pbuf chain is the requested size */    p->tot_len = length;    /* set the length of the first pbuf in the chain */    p->len = LWIP_MIN(length, PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset));    LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",                ((u8_t*)p->payload + p->len <=                 (u8_t*)p + SIZEOF_STRUCT_PBUF + PBUF_POOL_BUFSIZE_ALIGNED));    LWIP_ASSERT("PBUF_POOL_BUFSIZE must be bigger than MEM_ALIGNMENT",      (PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)) > 0 );    /* set reference count (needed here in case we fail) */    p->ref = 1;    /* now allocate the tail of the pbuf chain */    /* remember first pbuf for linkage in next iteration */    r = p;    /* remaining length to be allocated */    rem_len = length - p->len;    /* any remaining pbufs to be allocated? */    while (rem_len > 0) {      q = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);      if (q == NULL) {        PBUF_POOL_IS_EMPTY();        /* free chain so far allocated */        pbuf_free(p);        /* bail out unsuccesfully */        return NULL;      }      q->type = type;      q->flags = 0;      q->next = NULL;      /* make previous pbuf point to this pbuf */      r->next = q;      /* set total length of this pbuf and next in chain */      LWIP_ASSERT("rem_len < max_u16_t", rem_len < 0xffff);      q->tot_len = (u16_t)rem_len;      /* this pbuf length is pool size, unless smaller sized tail */      q->len = LWIP_MIN((u16_t)rem_len, PBUF_POOL_BUFSIZE_ALIGNED);      q->payload = (void *)((u8_t *)q + SIZEOF_STRUCT_PBUF);      LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned",              ((mem_ptr_t)q->payload % MEM_ALIGNMENT) == 0);      LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",                  ((u8_t*)p->payload + p->len <=                   (u8_t*)p + SIZEOF_STRUCT_PBUF + PBUF_POOL_BUFSIZE_ALIGNED));      q->ref = 1;      /* calculate remaining length to be allocated */      rem_len -= q->len;      /* remember this pbuf for linkage in next iteration */      r = q;    }    /* end of chain */    /*r->next = NULL;*/    break;  case PBUF_RAM:    /* If pbuf is to be allocated in RAM, allocate memory for it. */    p = (struct pbuf*)mem_malloc(LWIP_MEM_ALIGN_SIZE(SIZEOF_STRUCT_PBUF + offset) + LWIP_MEM_ALIGN_SIZE(length));    if (p == NULL) {      return NULL;    }    /* Set up internal structure of the pbuf. */    p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + SIZEOF_STRUCT_PBUF + offset));    p->len = p->tot_len = length;    p->next = NULL;    p->type = type;    LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned",           ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);    break;  /* pbuf references existing (non-volatile static constant) ROM payload? */  case PBUF_ROM:  /* pbuf references existing (externally allocated) RAM payload? */  case PBUF_REF:    /* only allocate memory for the pbuf structure */    p = (struct pbuf *)memp_malloc(MEMP_PBUF);    if (p == NULL) {      LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS,                  ("pbuf_alloc: Could not allocate MEMP_PBUF for PBUF_%s.\n",                  (type == PBUF_ROM) ? "ROM" : "REF"));      return NULL;    }    /* caller must set this field properly, afterwards */    p->payload = NULL;    p->len = p->tot_len = length;    p->next = NULL;    p->type = type;    break;  default:    LWIP_ASSERT("pbuf_alloc: erroneous type", 0);    return NULL;  }  /* set reference count */  p->ref = 1;  /* set flags */  p->flags = 0;  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F") == %p\n", length, (void *)p));  return p;}#if LWIP_SUPPORT_CUSTOM_PBUF/** Initialize a custom pbuf (already allocated). * * @param layer flag to define header size * @param length size of the pbuf's payload * @param type type of the pbuf (only used to treat the pbuf accordingly, as *        this function allocates no memory) * @param p pointer to the custom pbuf to initialize (already allocated) * @param 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 * @param payload_mem_len the size of the 'payload_mem' buffer, must be at least *        big enough to hold 'length' plus the header size */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){  u16_t offset;  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloced_custom(length=%"U16_F")\n", length));  /* determine header offset */  offset = 0;  switch (l) {  case PBUF_TRANSPORT:    /* add room for transport (often TCP) layer header */    offset += PBUF_TRANSPORT_HLEN;    /* FALLTHROUGH */  case PBUF_IP:    /* add room for IP layer header */    offset += PBUF_IP_HLEN;    /* FALLTHROUGH */  case PBUF_LINK:    /* add room for link layer header */    offset += PBUF_LINK_HLEN;    break;  case PBUF_RAW:    break;  default:    LWIP_ASSERT("pbuf_alloced_custom: bad pbuf layer", 0);    return NULL;  }  if (LWIP_MEM_ALIGN_SIZE(offset) + length < payload_mem_len) {    LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_WARNING, ("pbuf_alloced_custom(length=%"U16_F") buffer too short\n", length));    return NULL;  }  p->pbuf.next = NULL;  if (payload_mem != NULL) {    p->pbuf.payload = LWIP_MEM_ALIGN((void *)((u8_t *)payload_mem + offset));  } else {    p->pbuf.payload = NULL;  }  p->pbuf.flags = PBUF_FLAG_IS_CUSTOM;  p->pbuf.len = p->pbuf.tot_len = length;  p->pbuf.type = type;  p->pbuf.ref = 1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -