text:  > LwIP_OSE_1, 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. * *). */ /* */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" static u8_t pbuf_pool_memory[MEM_ALIGNMENT - 1 + PBUF_POOL_SIZE * MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE + sizeof(struct pbuf))]; #if !SYS_LIGHTWEIGHT_PROT static volatile u8_t pbuf_pool_free_lock, pbuf_pool_alloc_lock; static sys_sem_t pbuf_pool_free_sem; #endif static struct pbuf *pbuf_pool = NULL; /** * Initializes the pbuf module. * * 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. * * 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. * */ void pbuf_init(void) { struct pbuf *p, *q = NULL; u16_t i; pbuf_pool = (struct pbuf *)MEM_ALIGN(pbuf_pool_memory); #if PBUF_STATS lwip_stats.pbuf.avail = PBUF_POOL_SIZE; #endif /* PBUF_STATS */ /* Set up ->next pointers to link the pbufs of the pool together */ p = pbuf_pool; 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))); p->flags = PBUF_FLAG_POOL; q = p; p = p->next; } /* The ->next pointer of last pbuf is NULL to indicate that there are no more pbufs in the pool */ q->next = NULL; #if !SYS_LIGHTWEIGHT_PROT pbuf_pool_alloc_lock = 0; pbuf_pool_free_lock = 0; pbuf_pool_free_sem = sys_sem_new(1); #endif } /** * @internal only called from pbuf_alloc() */ static struct pbuf * pbuf_pool_alloc(void) { struct pbuf *p = NULL; SYS_ARCH_DECL_PROTECT(old_level); SYS_ARCH_PROTECT(old_level); #if !SYS_LIGHTWEIGHT_PROT /* Next, check the actual pbuf pool, but if the pool is locked, we pretend to be out of buffers and return NULL. */ if (pbuf_pool_free_lock) { #if PBUF_STATS ++lwip_stats.pbuf.alloc_locked; #endif /* PBUF_STATS */ return NULL; } pbuf_pool_alloc_lock = 1; if (!pbuf_pool_free_lock) { #endif /* SYS_LIGHTWEIGHT_PROT */ p = pbuf_pool; if (p) { pbuf_pool = p->next; } #if !SYS_LIGHTWEIGHT_PROT #if PBUF_STATS } else { ++lwip_stats.pbuf.alloc_locked; #endif /* PBUF_STATS */ } pbuf_pool_alloc_lock = 0; #endif /* SYS_LIGHTWEIGHT_PROT */ #if PBUF_STATS if (p != NULL) { ++lwip_stats.pbuf.used; if (lwip_stats.pbuf.used > lwip_stats.pbuf.max) { lwip_stats.pbuf.max = lwip_stats.pbuf.used; } } #endif /* PBUF_STATS */ SYS_ARCH_UNPROTECT(old_level); return p; } /** * 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 flag l, u16_t length, pbuf_flag flag) { struct pbuf *p, *q, *r; u16_t offset; s32_t rem_len; /* remaining length */ LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 3, ("pbuf_alloc(length=%"U16_F")\n", length)); /* determine header offset */ offset = 0; switch (l) { (flag) { case PBUF_POOL: /* allocate head of pbuf chain into p */ p = pbuf_pool_alloc(); LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 3, ("pbuf_alloc: allocated pbuf %p\n", (void *)p)); if (p == NULL) { #if PBUF_STATS ++lwip_stats.pbuf.err; #endif /* PBUF_STATS */ return NULL; } p->next = NULL; /* make the payload pointer point 'offset' bytes into pbuf data memory */ p->payload = = length > PBUF_POOL_BUFSIZE - offset? PBUF_POOL_BUFSIZE - offset: length; /* = pbuf_pool_alloc(); if (q == NULL) { LWIP_DEBUGF(PBUF_DEBUG | 2, ("pbuf_alloc: Out of pbufs in pool.\n")); #if PBUF_STATS ++lwip_stats.pbuf.err; #endif /* PBUF_STATS */ /* free chain so far allocated */ pbuf_free(p); /* bail out unsuccesfully */ return NULL; } q->next = NULL; /* make previous pbuf point to this pbuf */ r->next = q; /* 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 */ q->len = rem_len > PBUF_POOL_BUFSIZE? PBUF_POOL_BUFSIZE: rem_len; 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); 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 = mem_malloc(MEM_ALIGN_SIZE(sizeof(struct pbuf) + offset) + MEM_ALIGN_SIZE(length)); if (p == NULL) { return NULL; } /* Set up internal structure of the pbuf. */ p->payload = MEM_ALIGN((void *)((u8_t *)p + sizeof(struct pbuf) + offset)); p->len = p->tot_len = length; p->next = NULL; p->flags = PBUF_FLAG_RAM; | DBG_TRACE | 2, ("pbuf_alloc: Could not allocate MEMP_PBUF for PBUF_%s.\n", flag == 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->flags = (flag == PBUF_ROM? PBUF_FLAG_ROM: PBUF_FLAG_REF); break; default: LWIP_ASSERT("pbuf_alloc: erroneous flag", 0); return NULL; } /* set reference count */ p->ref = 1; LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 3, ("pbuf_alloc(length=%"U16_F") == %p\n", length, (void *)p)); return p; } #if PBUF_STATS #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 { \ p->next = pbuf_pool; \ pbuf_pool = p; \ DEC_PBUF_STATS; \ } while (0) #if SYS_LIGHTWEIGHT_PROT #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); \ } while (0) #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); \ } while (0) #endif /* SYS_LIGHTWEIGHT_PROT */ /** *. * * @bug Cannot grow the size of a pbuf (chain) (yet). */ void pbuf_realloc(struct pbuf *p, u16_t new_len) { struct pbuf *q; u16_t rem_len; /* remaining length */ s16_t grow; 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 */ q->tot_len += grow; /* proceed to next pbuf in chain */ q = q->next; } /* we have now reached the new last pbuf (in q) */ /* rem_len == desired length for pbuf q */ /* shrink allocated memory for PBUF_RAM */ /* (other types merely adjust their length fields */ if ((q->flags == PBUF_FLAG_RAM) && (rem_len != q->len)) { /* reallocate and adjust the length of the pbuf that will be split */ mem_realloc(q, (u8_t *)q->payload - (u8_t *)q + rem_len); } /* hdr_size_inc flags; void *payload; LWIP_ASSERT("p != NULL", p != NULL); if ((header_size_increment == 0) || (p == NULL)) return 0; flags = p->flags; /* remember current payload pointer */ payload = p->payload; /* pbuf types containing payloads? */ if (flags == PBUF_FLAG_RAM || flags == PBUF_FLAG (flags == PBUF_FLAG_REF || flags == PBUF_FLAG_ROM) { /* hide a header in the payload? */ if ((header_size_increment < 0) && (header_size_increment - p->len <= 0)) { /* increase payload pointer */ p->payload = (u8_t *)p->payload - header_size_increment; } else { /* cannot expand payload to front (yet!) * bail out unsuccesfully */buf flags; struct pbuf *q; u8_t count; SYS_ARCH_DECL_PROTECT(old_level); LWIP_ASSERT("p != NULL", p != NULL); /* if assertions are disabled, proceed with debug output */ if (p == NULL) { LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 2, ("pbuf_free(p == NULL) was called.\n")); return 0; } LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 3, ("pbuf_free(%p)\n", (void *)p)); PERF_START; 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); count = 0; /* 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); /* de-allocate all consecutive pbufs from the head of the chain that * obtain a zero reference count after decrementing*/ while (p != NULL) { /* all pbufs in a chain are referenced at least once */ LWIP_ASSERT("pbuf_free: p->ref > 0", p->ref > 0); /* decrease reference count (number of pointers to pbuf) */ p->ref--; /* this pbuf is no longer referenced to? */ if (p->ref == 0) { /* remember next pbuf in chain for next iteration */ q = p->next; LWIP_DEBUGF( PBUF_DEBUG | 2, ("pbuf_free: deallocating %p\n", (void *)p)); flags = p->flags; /* is this a pbuf from the pool? */ if (flags == PBUF_FLAG_POOL) { p->len = p->tot_len = PBUF_POOL_BUFSIZE; p->payload = (void *)((u8_t *)p + sizeof(struct pbuf)); PBUF_POOL_FREE(p); /* is this a ROM or RAM referencing pbuf? */ } else if (flags == PBUF_FLAG_ROM || flags == PBUF_FLAG_REF) { memp_free(MEMP_PBUF, p); /* flags == PBUF_FLAG, (u16_t)p->ref)); /* stop walking through the chain */ p = NULL; } } SYS_ARCH_UNPROTECT(old_level);_ASSERT("h != NULL (programmer violates API)", h != NULL); LWIP_ASSERT("t != NULL (programmer violates API)", t != NULL); if ( | DBG_FRESH | 2, ("pbuf_chain: %p references %p\n", (void *)h, (void *)t)); } /* For packet queueing. Note that queued packets MUST be dequeued first * using pbuf_dequeue() before calling other pbuf_() functions. */ #if ARP_QUEUEING /** * Add a packet to the end of a queue. * * @param q pointer to first packet on the queue * @param n packet to be queued * * Both packets MUST be given, and must be different. */ void pbuf_queue(struct pbuf *p, struct pbuf *n) { #if PBUF_DEBUG /* remember head of queue */ struct pbuf *q = p; #endif /* programmer stupidity checks */ LWIP_ASSERT("p == NULL in pbuf_queue: this indicates a programmer error\n", p != NULL); LWIP_ASSERT("n == NULL in pbuf_queue: this indicates a programmer error\n", n != NULL); LWIP_ASSERT("p == n in pbuf_queue: this indicates a programmer error\n", p != n); if ((p == NULL) || (n == NULL) || (p == n)){ LWIP_DEBUGF(PBUF_DEBUG | DBG_HALT | 3, ("pbuf_queue: programmer argument error\n")); return; } /* iterate through all packets on queue */ while (p->next != NULL) { /* be very picky about pbuf chain correctness */ #if PBUF_DEBUG /* iterate through all pbufs in packet */ last pbuf of a packet } */ } /* { p is last pbuf of a packet } */ /* proceed to next packet on queue */ #endif /* proceed to next pbuf */ if (p->next != NULL) p = p->next; } /* { p->tot_len == p->len and p->next == NULL } ==> * { p is last pbuf of last packet on queue } */ /* chain last pbuf of queue with n */ p->next = n; /* n is now referenced to by the (packet p in the) queue */ pbuf_ref(n); #if PBUF_DEBUG LWIP_DEBUGF(PBUF_DEBUG | DBG_FRESH | 2, ("pbuf_queue: newly queued packet %p sits after packet %p in queue %p\n", (void *)n, (void *)p, (void *)q)); #endif } /** * Remove a packet from the head of a queue. * * The caller MUST reference the remainder of the queue (as returned). The * caller MUST NOT call pbuf_ref() as it implicitly takes over the reference * from p. * * @param p pointer to first packet on the queue which will be dequeued. * @return first packet on the remaining queue (NULL if no further packets). * */ struct pbuf * pbuf_dequeue(struct pbuf *p) { struct pbuf *q; LWIP_ASSERT("p != NULL", p != NULL); /* iterate through all pbufs in packet p */ the last pbuf of the first packet */ /* remember next packet on queue in q */ q = p->next; /* dequeue packet p from queue */ p->next = NULL; /* any next packet on queue? */ if (q != NULL) { /* although q is no longer referenced by p, it MUST be referenced by * the caller, who is maintaining this packet queue. So, we do not call * pbuf_free(q) here, resulting in an implicit pbuf_ref(q) for the caller. */ LWIP_DEBUGF(PBUF_DEBUG | DBG_FRESH | 2, ("pbuf_dequeue: first remaining packet on queue is %p\n", (void *)q)); } else { LWIP_DEBUGF(PBUF_DEBUG | DBG_FRESH | 2, ("pbuf_dequeue: no further packets on queue\n")); } return q; } #endif /** * * Create PBUF_POOL (or PBUF_RAM) copies of PBUF_REF pbufs. * * Used to queue packets on behalf of the lwIP stack, such as * ARP based queueing. * * Go through a pbuf chain and replace any PBUF_REF buffers * with PBUF_POOL (or PBUF_RAM) pbufs, each taking a copy of * the referenced data. * * @note You MUST explicitly use p = pbuf_take(p); * The pbuf you give as argument, may have been replaced * by a (differently located) copy through pbuf_take()! * * @note Any replaced pbufs will be freed through pbuf_free(). * This may deallocate them if they become no longer referenced. * * @param p Head of pbuf chain to process * * @return Pointer to head of pbuf chain */ struct pbuf * pbuf_take(struct pbuf *p) { struct pbuf *q , *prev, *head; LWIP_ASSERT("pbuf_take: p != NULL\n", p != NULL); LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 3, ("pbuf_take(%p)\n", (void*)p)); prev = NULL; head = p; /* iterate through pbuf chain */ do { /* pbuf is of type PBUF_REF? */ if (p->flags == PBUF_FLAG_REF) { LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE, ("pbuf_take: encountered PBUF_REF %p\n", (void *)p)); /* allocate a pbuf (w/ payload) fully in RAM */ /* PBUF_POOL buffers are faster if we can use them */ if (p->len <= PBUF_POOL_BUFSIZE) { q = pbuf_alloc(PBUF_RAW, p->len, PBUF_POOL); if (q == NULL) { LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 2, ("pbuf_take: Could not allocate PBUF_POOL\n")); } } else { /* no replacement pbuf yet */ q = NULL; LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 2, ("pbuf_take: PBUF_POOL too small to replace PBUF_REF\n")); } /* no (large enough) PBUF_POOL was available? retry with PBUF_RAM */ if (q == NULL) { q = pbuf_alloc(PBUF_RAW, p->len, PBUF_RAM); if (q == NULL) { LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 2, ("pbuf_take: Could not allocate PBUF_RAM\n")); } } /* replacement pbuf could be allocated? */ if (q != NULL) { /* copy p to q */ /* copy successor */ q->next = p->next; /* remove linkage from original pbuf */ p->next = NULL; /* remove linkage to original pbuf */ if (prev != NULL) { /* prev->next == p at this point */ LWIP_ASSERT("prev->next == p", prev->next == p); /* break chain and insert new pbuf instead */ prev->next = q; /* prev == NULL, so we replaced the head pbuf of the chain */ } else { head = q; } /* copy pbuf payload */ memcpy(q->payload, p->payload, p->len); q->tot_len = p->tot_len; q->len = p->len; /* in case p was the first pbuf, it is no longer refered to by * our caller, as the caller MUST do p = pbuf_take(p); * in case p was not the first pbuf, it is no longer refered to * by prev. we can safely free the pbuf here. * (note that we have set p->next to NULL already so that * we will not free the rest of the chain by accident.) */ pbuf_free(p); /* do not copy ref, since someone else might be using the old buffer */ LWIP_DEBUGF(PBUF_DEBUG, ("pbuf_take: replaced PBUF_REF %p with %p\n", (void *)p, (void *)q)); p = q; } else { /* deallocate chain */ pbuf_free(head); LWIP_DEBUGF(PBUF_DEBUG | 2, ("pbuf_take: failed to allocate replacement pbuf for %p\n", (void *)p)); return NULL; } /* p->flags != PBUF_FLAG_REF */ } else { LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 1, ("pbuf_take: skipping pbuf not of type PBUF_REF\n")); } /* remember this pbuf */ prev = p; /* proceed to next pbuf in original chain */ p = p->next; } while (p); LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 1, ("pbuf_take: end of chain reached.\n")); return head; } /** * | DBG_STATE, ("pbuf_dechain: unreferencing %p\n", (void *)q)); tail_gone = pbuf_free(q); if (tail_gone > 0) { LWIP_DEBUGF(PBUF_DEBUG |); }