Bonjour/Zerconf library

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Aug 14 15:54:31 2010 +0000
Revision:
5:8e53abda9900
Parent:
0:355018f44c9f

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dirkx 0:355018f44c9f 1 /**
dirkx 0:355018f44c9f 2 * @file
dirkx 0:355018f44c9f 3 * Packet buffer management
dirkx 0:355018f44c9f 4 *
dirkx 0:355018f44c9f 5 * Packets are built from the pbuf data structure. It supports dynamic
dirkx 0:355018f44c9f 6 * memory allocation for packet contents or can reference externally
dirkx 0:355018f44c9f 7 * managed packet contents both in RAM and ROM. Quick allocation for
dirkx 0:355018f44c9f 8 * incoming packets is provided through pools with fixed sized pbufs.
dirkx 0:355018f44c9f 9 *
dirkx 0:355018f44c9f 10 * A packet may span over multiple pbufs, chained as a singly linked
dirkx 0:355018f44c9f 11 * list. This is called a "pbuf chain".
dirkx 0:355018f44c9f 12 *
dirkx 0:355018f44c9f 13 * Multiple packets may be queued, also using this singly linked list.
dirkx 0:355018f44c9f 14 * This is called a "packet queue".
dirkx 0:355018f44c9f 15 *
dirkx 0:355018f44c9f 16 * So, a packet queue consists of one or more pbuf chains, each of
dirkx 0:355018f44c9f 17 * which consist of one or more pbufs. CURRENTLY, PACKET QUEUES ARE
dirkx 0:355018f44c9f 18 * NOT SUPPORTED!!! Use helper structs to queue multiple packets.
dirkx 0:355018f44c9f 19 *
dirkx 0:355018f44c9f 20 * The differences between a pbuf chain and a packet queue are very
dirkx 0:355018f44c9f 21 * precise but subtle.
dirkx 0:355018f44c9f 22 *
dirkx 0:355018f44c9f 23 * The last pbuf of a packet has a ->tot_len field that equals the
dirkx 0:355018f44c9f 24 * ->len field. It can be found by traversing the list. If the last
dirkx 0:355018f44c9f 25 * pbuf of a packet has a ->next field other than NULL, more packets
dirkx 0:355018f44c9f 26 * are on the queue.
dirkx 0:355018f44c9f 27 *
dirkx 0:355018f44c9f 28 * Therefore, looping through a pbuf of a single packet, has an
dirkx 0:355018f44c9f 29 * loop end condition (tot_len == p->len), NOT (next == NULL).
dirkx 0:355018f44c9f 30 */
dirkx 0:355018f44c9f 31
dirkx 0:355018f44c9f 32 /*
dirkx 0:355018f44c9f 33 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
dirkx 0:355018f44c9f 34 * All rights reserved.
dirkx 0:355018f44c9f 35 *
dirkx 0:355018f44c9f 36 * Redistribution and use in source and binary forms, with or without modification,
dirkx 0:355018f44c9f 37 * are permitted provided that the following conditions are met:
dirkx 0:355018f44c9f 38 *
dirkx 0:355018f44c9f 39 * 1. Redistributions of source code must retain the above copyright notice,
dirkx 0:355018f44c9f 40 * this list of conditions and the following disclaimer.
dirkx 0:355018f44c9f 41 * 2. Redistributions in binary form must reproduce the above copyright notice,
dirkx 0:355018f44c9f 42 * this list of conditions and the following disclaimer in the documentation
dirkx 0:355018f44c9f 43 * and/or other materials provided with the distribution.
dirkx 0:355018f44c9f 44 * 3. The name of the author may not be used to endorse or promote products
dirkx 0:355018f44c9f 45 * derived from this software without specific prior written permission.
dirkx 0:355018f44c9f 46 *
dirkx 0:355018f44c9f 47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
dirkx 0:355018f44c9f 48 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
dirkx 0:355018f44c9f 49 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
dirkx 0:355018f44c9f 50 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
dirkx 0:355018f44c9f 51 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
dirkx 0:355018f44c9f 52 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
dirkx 0:355018f44c9f 53 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
dirkx 0:355018f44c9f 54 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
dirkx 0:355018f44c9f 55 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
dirkx 0:355018f44c9f 56 * OF SUCH DAMAGE.
dirkx 0:355018f44c9f 57 *
dirkx 0:355018f44c9f 58 * This file is part of the lwIP TCP/IP stack.
dirkx 0:355018f44c9f 59 *
dirkx 0:355018f44c9f 60 * Author: Adam Dunkels <adam@sics.se>
dirkx 0:355018f44c9f 61 *
dirkx 0:355018f44c9f 62 */
dirkx 0:355018f44c9f 63
dirkx 0:355018f44c9f 64 #include "lwip/opt.h"
dirkx 0:355018f44c9f 65
dirkx 0:355018f44c9f 66 #include "lwip/stats.h"
dirkx 0:355018f44c9f 67 #include "lwip/def.h"
dirkx 0:355018f44c9f 68 #include "lwip/mem.h"
dirkx 0:355018f44c9f 69 #include "lwip/memp.h"
dirkx 0:355018f44c9f 70 #include "lwip/pbuf.h"
dirkx 0:355018f44c9f 71 #include "lwip/sys.h"
dirkx 0:355018f44c9f 72 #include "arch/perf.h"
dirkx 0:355018f44c9f 73 #if TCP_QUEUE_OOSEQ
dirkx 0:355018f44c9f 74 #include "lwip/tcp_impl.h"
dirkx 0:355018f44c9f 75 #endif
dirkx 0:355018f44c9f 76
dirkx 0:355018f44c9f 77 #include <string.h>
dirkx 0:355018f44c9f 78
dirkx 0:355018f44c9f 79 #define SIZEOF_STRUCT_PBUF LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf))
dirkx 0:355018f44c9f 80 /* Since the pool is created in memp, PBUF_POOL_BUFSIZE will be automatically
dirkx 0:355018f44c9f 81 aligned there. Therefore, PBUF_POOL_BUFSIZE_ALIGNED can be used here. */
dirkx 0:355018f44c9f 82 #define PBUF_POOL_BUFSIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE)
dirkx 0:355018f44c9f 83
dirkx 0:355018f44c9f 84 #if !TCP_QUEUE_OOSEQ || NO_SYS
dirkx 0:355018f44c9f 85 #define PBUF_POOL_IS_EMPTY()
dirkx 0:355018f44c9f 86 #else /* !TCP_QUEUE_OOSEQ || NO_SYS */
dirkx 0:355018f44c9f 87 /** Define this to 0 to prevent freeing ooseq pbufs when the PBUF_POOL is empty */
dirkx 0:355018f44c9f 88 #ifndef PBUF_POOL_FREE_OOSEQ
dirkx 0:355018f44c9f 89 #define PBUF_POOL_FREE_OOSEQ 1
dirkx 0:355018f44c9f 90 #endif /* PBUF_POOL_FREE_OOSEQ */
dirkx 0:355018f44c9f 91
dirkx 0:355018f44c9f 92 #if PBUF_POOL_FREE_OOSEQ
dirkx 0:355018f44c9f 93 #include "lwip/tcpip.h"
dirkx 0:355018f44c9f 94 #define PBUF_POOL_IS_EMPTY() pbuf_pool_is_empty()
dirkx 0:355018f44c9f 95 static u8_t pbuf_free_ooseq_queued;
dirkx 0:355018f44c9f 96 /**
dirkx 0:355018f44c9f 97 * Attempt to reclaim some memory from queued out-of-sequence TCP segments
dirkx 0:355018f44c9f 98 * if we run out of pool pbufs. It's better to give priority to new packets
dirkx 0:355018f44c9f 99 * if we're running out.
dirkx 0:355018f44c9f 100 *
dirkx 0:355018f44c9f 101 * This must be done in the correct thread context therefore this function
dirkx 0:355018f44c9f 102 * can only be used with NO_SYS=0 and through tcpip_callback.
dirkx 0:355018f44c9f 103 */
dirkx 0:355018f44c9f 104 static void
dirkx 0:355018f44c9f 105 pbuf_free_ooseq(void* arg)
dirkx 0:355018f44c9f 106 {
dirkx 0:355018f44c9f 107 struct tcp_pcb* pcb;
dirkx 0:355018f44c9f 108 SYS_ARCH_DECL_PROTECT(old_level);
dirkx 0:355018f44c9f 109 LWIP_UNUSED_ARG(arg);
dirkx 0:355018f44c9f 110
dirkx 0:355018f44c9f 111 SYS_ARCH_PROTECT(old_level);
dirkx 0:355018f44c9f 112 pbuf_free_ooseq_queued = 0;
dirkx 0:355018f44c9f 113 SYS_ARCH_UNPROTECT(old_level);
dirkx 0:355018f44c9f 114
dirkx 0:355018f44c9f 115 for (pcb = tcp_active_pcbs; NULL != pcb; pcb = pcb->next) {
dirkx 0:355018f44c9f 116 if (NULL != pcb->ooseq) {
dirkx 0:355018f44c9f 117 /** Free the ooseq pbufs of one PCB only */
dirkx 0:355018f44c9f 118 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free_ooseq: freeing out-of-sequence pbufs\n"));
dirkx 0:355018f44c9f 119 tcp_segs_free(pcb->ooseq);
dirkx 0:355018f44c9f 120 pcb->ooseq = NULL;
dirkx 0:355018f44c9f 121 return;
dirkx 0:355018f44c9f 122 }
dirkx 0:355018f44c9f 123 }
dirkx 0:355018f44c9f 124 }
dirkx 0:355018f44c9f 125
dirkx 0:355018f44c9f 126 /** Queue a call to pbuf_free_ooseq if not already queued. */
dirkx 0:355018f44c9f 127 static void
dirkx 0:355018f44c9f 128 pbuf_pool_is_empty(void)
dirkx 0:355018f44c9f 129 {
dirkx 0:355018f44c9f 130 u8_t queued;
dirkx 0:355018f44c9f 131 SYS_ARCH_DECL_PROTECT(old_level);
dirkx 0:355018f44c9f 132
dirkx 0:355018f44c9f 133 SYS_ARCH_PROTECT(old_level);
dirkx 0:355018f44c9f 134 queued = pbuf_free_ooseq_queued;
dirkx 0:355018f44c9f 135 pbuf_free_ooseq_queued = 1;
dirkx 0:355018f44c9f 136 SYS_ARCH_UNPROTECT(old_level);
dirkx 0:355018f44c9f 137
dirkx 0:355018f44c9f 138 if(!queued) {
dirkx 0:355018f44c9f 139 /* queue a call to pbuf_free_ooseq if not already queued */
dirkx 0:355018f44c9f 140 if(tcpip_callback_with_block(pbuf_free_ooseq, NULL, 0) != ERR_OK) {
dirkx 0:355018f44c9f 141 SYS_ARCH_PROTECT(old_level);
dirkx 0:355018f44c9f 142 pbuf_free_ooseq_queued = 0;
dirkx 0:355018f44c9f 143 SYS_ARCH_UNPROTECT(old_level);
dirkx 0:355018f44c9f 144 }
dirkx 0:355018f44c9f 145 }
dirkx 0:355018f44c9f 146 }
dirkx 0:355018f44c9f 147 #endif /* PBUF_POOL_FREE_OOSEQ */
dirkx 0:355018f44c9f 148 #endif /* !TCP_QUEUE_OOSEQ || NO_SYS */
dirkx 0:355018f44c9f 149
dirkx 0:355018f44c9f 150 /**
dirkx 0:355018f44c9f 151 * Allocates a pbuf of the given type (possibly a chain for PBUF_POOL type).
dirkx 0:355018f44c9f 152 *
dirkx 0:355018f44c9f 153 * The actual memory allocated for the pbuf is determined by the
dirkx 0:355018f44c9f 154 * layer at which the pbuf is allocated and the requested size
dirkx 0:355018f44c9f 155 * (from the size parameter).
dirkx 0:355018f44c9f 156 *
dirkx 0:355018f44c9f 157 * @param layer flag to define header size
dirkx 0:355018f44c9f 158 * @param length size of the pbuf's payload
dirkx 0:355018f44c9f 159 * @param type this parameter decides how and where the pbuf
dirkx 0:355018f44c9f 160 * should be allocated as follows:
dirkx 0:355018f44c9f 161 *
dirkx 0:355018f44c9f 162 * - PBUF_RAM: buffer memory for pbuf is allocated as one large
dirkx 0:355018f44c9f 163 * chunk. This includes protocol headers as well.
dirkx 0:355018f44c9f 164 * - PBUF_ROM: no buffer memory is allocated for the pbuf, even for
dirkx 0:355018f44c9f 165 * protocol headers. Additional headers must be prepended
dirkx 0:355018f44c9f 166 * by allocating another pbuf and chain in to the front of
dirkx 0:355018f44c9f 167 * the ROM pbuf. It is assumed that the memory used is really
dirkx 0:355018f44c9f 168 * similar to ROM in that it is immutable and will not be
dirkx 0:355018f44c9f 169 * changed. Memory which is dynamic should generally not
dirkx 0:355018f44c9f 170 * be attached to PBUF_ROM pbufs. Use PBUF_REF instead.
dirkx 0:355018f44c9f 171 * - PBUF_REF: no buffer memory is allocated for the pbuf, even for
dirkx 0:355018f44c9f 172 * protocol headers. It is assumed that the pbuf is only
dirkx 0:355018f44c9f 173 * being used in a single thread. If the pbuf gets queued,
dirkx 0:355018f44c9f 174 * then pbuf_take should be called to copy the buffer.
dirkx 0:355018f44c9f 175 * - PBUF_POOL: the pbuf is allocated as a pbuf chain, with pbufs from
dirkx 0:355018f44c9f 176 * the pbuf pool that is allocated during pbuf_init().
dirkx 0:355018f44c9f 177 *
dirkx 0:355018f44c9f 178 * @return the allocated pbuf. If multiple pbufs where allocated, this
dirkx 0:355018f44c9f 179 * is the first pbuf of a pbuf chain.
dirkx 0:355018f44c9f 180 */
dirkx 0:355018f44c9f 181 struct pbuf *
dirkx 0:355018f44c9f 182 pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
dirkx 0:355018f44c9f 183 {
dirkx 0:355018f44c9f 184 struct pbuf *p, *q, *r;
dirkx 0:355018f44c9f 185 u16_t offset;
dirkx 0:355018f44c9f 186 s32_t rem_len; /* remaining length */
dirkx 0:355018f44c9f 187 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F")\n", length));
dirkx 0:355018f44c9f 188
dirkx 0:355018f44c9f 189 /* determine header offset */
dirkx 0:355018f44c9f 190 offset = 0;
dirkx 0:355018f44c9f 191 switch (layer) {
dirkx 0:355018f44c9f 192 case PBUF_TRANSPORT:
dirkx 0:355018f44c9f 193 /* add room for transport (often TCP) layer header */
dirkx 0:355018f44c9f 194 offset += PBUF_TRANSPORT_HLEN;
dirkx 0:355018f44c9f 195 /* FALLTHROUGH */
dirkx 0:355018f44c9f 196 case PBUF_IP:
dirkx 0:355018f44c9f 197 /* add room for IP layer header */
dirkx 0:355018f44c9f 198 offset += PBUF_IP_HLEN;
dirkx 0:355018f44c9f 199 /* FALLTHROUGH */
dirkx 0:355018f44c9f 200 case PBUF_LINK:
dirkx 0:355018f44c9f 201 /* add room for link layer header */
dirkx 0:355018f44c9f 202 offset += PBUF_LINK_HLEN;
dirkx 0:355018f44c9f 203 break;
dirkx 0:355018f44c9f 204 case PBUF_RAW:
dirkx 0:355018f44c9f 205 break;
dirkx 0:355018f44c9f 206 default:
dirkx 0:355018f44c9f 207 LWIP_ASSERT("pbuf_alloc: bad pbuf layer", 0);
dirkx 0:355018f44c9f 208 return NULL;
dirkx 0:355018f44c9f 209 }
dirkx 0:355018f44c9f 210
dirkx 0:355018f44c9f 211 switch (type) {
dirkx 0:355018f44c9f 212 case PBUF_POOL:
dirkx 0:355018f44c9f 213 /* allocate head of pbuf chain into p */
dirkx 0:355018f44c9f 214 p = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
dirkx 0:355018f44c9f 215 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc: allocated pbuf %p\n", (void *)p));
dirkx 0:355018f44c9f 216 if (p == NULL) {
dirkx 0:355018f44c9f 217 PBUF_POOL_IS_EMPTY();
dirkx 0:355018f44c9f 218 return NULL;
dirkx 0:355018f44c9f 219 }
dirkx 0:355018f44c9f 220 p->type = type;
dirkx 0:355018f44c9f 221 p->next = NULL;
dirkx 0:355018f44c9f 222
dirkx 0:355018f44c9f 223 /* make the payload pointer point 'offset' bytes into pbuf data memory */
dirkx 0:355018f44c9f 224 p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + (SIZEOF_STRUCT_PBUF + offset)));
dirkx 0:355018f44c9f 225 LWIP_ASSERT("pbuf_alloc: pbuf p->payload properly aligned",
dirkx 0:355018f44c9f 226 ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
dirkx 0:355018f44c9f 227 /* the total length of the pbuf chain is the requested size */
dirkx 0:355018f44c9f 228 p->tot_len = length;
dirkx 0:355018f44c9f 229 /* set the length of the first pbuf in the chain */
dirkx 0:355018f44c9f 230 p->len = LWIP_MIN(length, PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset));
dirkx 0:355018f44c9f 231 LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",
dirkx 0:355018f44c9f 232 ((u8_t*)p->payload + p->len <=
dirkx 0:355018f44c9f 233 (u8_t*)p + SIZEOF_STRUCT_PBUF + PBUF_POOL_BUFSIZE_ALIGNED));
dirkx 0:355018f44c9f 234 LWIP_ASSERT("PBUF_POOL_BUFSIZE must be bigger than MEM_ALIGNMENT",
dirkx 0:355018f44c9f 235 (PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)) > 0 );
dirkx 0:355018f44c9f 236 /* set reference count (needed here in case we fail) */
dirkx 0:355018f44c9f 237 p->ref = 1;
dirkx 0:355018f44c9f 238
dirkx 0:355018f44c9f 239 /* now allocate the tail of the pbuf chain */
dirkx 0:355018f44c9f 240
dirkx 0:355018f44c9f 241 /* remember first pbuf for linkage in next iteration */
dirkx 0:355018f44c9f 242 r = p;
dirkx 0:355018f44c9f 243 /* remaining length to be allocated */
dirkx 0:355018f44c9f 244 rem_len = length - p->len;
dirkx 0:355018f44c9f 245 /* any remaining pbufs to be allocated? */
dirkx 0:355018f44c9f 246 while (rem_len > 0) {
dirkx 0:355018f44c9f 247 q = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
dirkx 0:355018f44c9f 248 if (q == NULL) {
dirkx 0:355018f44c9f 249 PBUF_POOL_IS_EMPTY();
dirkx 0:355018f44c9f 250 /* free chain so far allocated */
dirkx 0:355018f44c9f 251 pbuf_free(p);
dirkx 0:355018f44c9f 252 /* bail out unsuccesfully */
dirkx 0:355018f44c9f 253 return NULL;
dirkx 0:355018f44c9f 254 }
dirkx 0:355018f44c9f 255 q->type = type;
dirkx 0:355018f44c9f 256 q->flags = 0;
dirkx 0:355018f44c9f 257 q->next = NULL;
dirkx 0:355018f44c9f 258 /* make previous pbuf point to this pbuf */
dirkx 0:355018f44c9f 259 r->next = q;
dirkx 0:355018f44c9f 260 /* set total length of this pbuf and next in chain */
dirkx 0:355018f44c9f 261 LWIP_ASSERT("rem_len < max_u16_t", rem_len < 0xffff);
dirkx 0:355018f44c9f 262 q->tot_len = (u16_t)rem_len;
dirkx 0:355018f44c9f 263 /* this pbuf length is pool size, unless smaller sized tail */
dirkx 0:355018f44c9f 264 q->len = LWIP_MIN((u16_t)rem_len, PBUF_POOL_BUFSIZE_ALIGNED);
dirkx 0:355018f44c9f 265 q->payload = (void *)((u8_t *)q + SIZEOF_STRUCT_PBUF);
dirkx 0:355018f44c9f 266 LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned",
dirkx 0:355018f44c9f 267 ((mem_ptr_t)q->payload % MEM_ALIGNMENT) == 0);
dirkx 0:355018f44c9f 268 LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",
dirkx 0:355018f44c9f 269 ((u8_t*)p->payload + p->len <=
dirkx 0:355018f44c9f 270 (u8_t*)p + SIZEOF_STRUCT_PBUF + PBUF_POOL_BUFSIZE_ALIGNED));
dirkx 0:355018f44c9f 271 q->ref = 1;
dirkx 0:355018f44c9f 272 /* calculate remaining length to be allocated */
dirkx 0:355018f44c9f 273 rem_len -= q->len;
dirkx 0:355018f44c9f 274 /* remember this pbuf for linkage in next iteration */
dirkx 0:355018f44c9f 275 r = q;
dirkx 0:355018f44c9f 276 }
dirkx 0:355018f44c9f 277 /* end of chain */
dirkx 0:355018f44c9f 278 /*r->next = NULL;*/
dirkx 0:355018f44c9f 279
dirkx 0:355018f44c9f 280 break;
dirkx 0:355018f44c9f 281 case PBUF_RAM:
dirkx 0:355018f44c9f 282 /* If pbuf is to be allocated in RAM, allocate memory for it. */
dirkx 0:355018f44c9f 283 p = (struct pbuf*)mem_malloc(LWIP_MEM_ALIGN_SIZE(SIZEOF_STRUCT_PBUF + offset) + LWIP_MEM_ALIGN_SIZE(length));
dirkx 0:355018f44c9f 284 if (p == NULL) {
dirkx 0:355018f44c9f 285 return NULL;
dirkx 0:355018f44c9f 286 }
dirkx 0:355018f44c9f 287 /* Set up internal structure of the pbuf. */
dirkx 0:355018f44c9f 288 p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + SIZEOF_STRUCT_PBUF + offset));
dirkx 0:355018f44c9f 289 p->len = p->tot_len = length;
dirkx 0:355018f44c9f 290 p->next = NULL;
dirkx 0:355018f44c9f 291 p->type = type;
dirkx 0:355018f44c9f 292
dirkx 0:355018f44c9f 293 LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned",
dirkx 0:355018f44c9f 294 ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
dirkx 0:355018f44c9f 295 break;
dirkx 0:355018f44c9f 296 /* pbuf references existing (non-volatile static constant) ROM payload? */
dirkx 0:355018f44c9f 297 case PBUF_ROM:
dirkx 0:355018f44c9f 298 /* pbuf references existing (externally allocated) RAM payload? */
dirkx 0:355018f44c9f 299 case PBUF_REF:
dirkx 0:355018f44c9f 300 /* only allocate memory for the pbuf structure */
dirkx 0:355018f44c9f 301 p = (struct pbuf *)memp_malloc(MEMP_PBUF);
dirkx 0:355018f44c9f 302 if (p == NULL) {
dirkx 0:355018f44c9f 303 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
dirkx 0:355018f44c9f 304 ("pbuf_alloc: Could not allocate MEMP_PBUF for PBUF_%s.\n",
dirkx 0:355018f44c9f 305 (type == PBUF_ROM) ? "ROM" : "REF"));
dirkx 0:355018f44c9f 306 return NULL;
dirkx 0:355018f44c9f 307 }
dirkx 0:355018f44c9f 308 /* caller must set this field properly, afterwards */
dirkx 0:355018f44c9f 309 p->payload = NULL;
dirkx 0:355018f44c9f 310 p->len = p->tot_len = length;
dirkx 0:355018f44c9f 311 p->next = NULL;
dirkx 0:355018f44c9f 312 p->type = type;
dirkx 0:355018f44c9f 313 break;
dirkx 0:355018f44c9f 314 default:
dirkx 0:355018f44c9f 315 LWIP_ASSERT("pbuf_alloc: erroneous type", 0);
dirkx 0:355018f44c9f 316 return NULL;
dirkx 0:355018f44c9f 317 }
dirkx 0:355018f44c9f 318 /* set reference count */
dirkx 0:355018f44c9f 319 p->ref = 1;
dirkx 0:355018f44c9f 320 /* set flags */
dirkx 0:355018f44c9f 321 p->flags = 0;
dirkx 0:355018f44c9f 322 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F") == %p\n", length, (void *)p));
dirkx 0:355018f44c9f 323 return p;
dirkx 0:355018f44c9f 324 }
dirkx 0:355018f44c9f 325
dirkx 0:355018f44c9f 326
dirkx 0:355018f44c9f 327 /**
dirkx 0:355018f44c9f 328 * Shrink a pbuf chain to a desired length.
dirkx 0:355018f44c9f 329 *
dirkx 0:355018f44c9f 330 * @param p pbuf to shrink.
dirkx 0:355018f44c9f 331 * @param new_len desired new length of pbuf chain
dirkx 0:355018f44c9f 332 *
dirkx 0:355018f44c9f 333 * Depending on the desired length, the first few pbufs in a chain might
dirkx 0:355018f44c9f 334 * be skipped and left unchanged. The new last pbuf in the chain will be
dirkx 0:355018f44c9f 335 * resized, and any remaining pbufs will be freed.
dirkx 0:355018f44c9f 336 *
dirkx 0:355018f44c9f 337 * @note If the pbuf is ROM/REF, only the ->tot_len and ->len fields are adjusted.
dirkx 0:355018f44c9f 338 * @note May not be called on a packet queue.
dirkx 0:355018f44c9f 339 *
dirkx 0:355018f44c9f 340 * @note Despite its name, pbuf_realloc cannot grow the size of a pbuf (chain).
dirkx 0:355018f44c9f 341 */
dirkx 0:355018f44c9f 342 void
dirkx 0:355018f44c9f 343 pbuf_realloc(struct pbuf *p, u16_t new_len)
dirkx 0:355018f44c9f 344 {
dirkx 0:355018f44c9f 345 struct pbuf *q;
dirkx 0:355018f44c9f 346 u16_t rem_len; /* remaining length */
dirkx 0:355018f44c9f 347 s32_t grow;
dirkx 0:355018f44c9f 348
dirkx 0:355018f44c9f 349 LWIP_ASSERT("pbuf_realloc: p != NULL", p != NULL);
dirkx 0:355018f44c9f 350 LWIP_ASSERT("pbuf_realloc: sane p->type", p->type == PBUF_POOL ||
dirkx 0:355018f44c9f 351 p->type == PBUF_ROM ||
dirkx 0:355018f44c9f 352 p->type == PBUF_RAM ||
dirkx 0:355018f44c9f 353 p->type == PBUF_REF);
dirkx 0:355018f44c9f 354
dirkx 0:355018f44c9f 355 /* desired length larger than current length? */
dirkx 0:355018f44c9f 356 if (new_len >= p->tot_len) {
dirkx 0:355018f44c9f 357 /* enlarging not yet supported */
dirkx 0:355018f44c9f 358 return;
dirkx 0:355018f44c9f 359 }
dirkx 0:355018f44c9f 360
dirkx 0:355018f44c9f 361 /* the pbuf chain grows by (new_len - p->tot_len) bytes
dirkx 0:355018f44c9f 362 * (which may be negative in case of shrinking) */
dirkx 0:355018f44c9f 363 grow = new_len - p->tot_len;
dirkx 0:355018f44c9f 364
dirkx 0:355018f44c9f 365 /* first, step over any pbufs that should remain in the chain */
dirkx 0:355018f44c9f 366 rem_len = new_len;
dirkx 0:355018f44c9f 367 q = p;
dirkx 0:355018f44c9f 368 /* should this pbuf be kept? */
dirkx 0:355018f44c9f 369 while (rem_len > q->len) {
dirkx 0:355018f44c9f 370 /* decrease remaining length by pbuf length */
dirkx 0:355018f44c9f 371 rem_len -= q->len;
dirkx 0:355018f44c9f 372 /* decrease total length indicator */
dirkx 0:355018f44c9f 373 LWIP_ASSERT("grow < max_u16_t", grow < 0xffff);
dirkx 0:355018f44c9f 374 q->tot_len += (u16_t)grow;
dirkx 0:355018f44c9f 375 /* proceed to next pbuf in chain */
dirkx 0:355018f44c9f 376 q = q->next;
dirkx 0:355018f44c9f 377 LWIP_ASSERT("pbuf_realloc: q != NULL", q != NULL);
dirkx 0:355018f44c9f 378 }
dirkx 0:355018f44c9f 379 /* we have now reached the new last pbuf (in q) */
dirkx 0:355018f44c9f 380 /* rem_len == desired length for pbuf q */
dirkx 0:355018f44c9f 381
dirkx 0:355018f44c9f 382 /* shrink allocated memory for PBUF_RAM */
dirkx 0:355018f44c9f 383 /* (other types merely adjust their length fields */
dirkx 0:355018f44c9f 384 if ((q->type == PBUF_RAM) && (rem_len != q->len)) {
dirkx 0:355018f44c9f 385 /* reallocate and adjust the length of the pbuf that will be split */
dirkx 0:355018f44c9f 386 q = (struct pbuf *)mem_trim(q, (u16_t)((u8_t *)q->payload - (u8_t *)q) + rem_len);
dirkx 0:355018f44c9f 387 LWIP_ASSERT("mem_trim returned q == NULL", q != NULL);
dirkx 0:355018f44c9f 388 }
dirkx 0:355018f44c9f 389 /* adjust length fields for new last pbuf */
dirkx 0:355018f44c9f 390 q->len = rem_len;
dirkx 0:355018f44c9f 391 q->tot_len = q->len;
dirkx 0:355018f44c9f 392
dirkx 0:355018f44c9f 393 /* any remaining pbufs in chain? */
dirkx 0:355018f44c9f 394 if (q->next != NULL) {
dirkx 0:355018f44c9f 395 /* free remaining pbufs in chain */
dirkx 0:355018f44c9f 396 pbuf_free(q->next);
dirkx 0:355018f44c9f 397 }
dirkx 0:355018f44c9f 398 /* q is last packet in chain */
dirkx 0:355018f44c9f 399 q->next = NULL;
dirkx 0:355018f44c9f 400
dirkx 0:355018f44c9f 401 }
dirkx 0:355018f44c9f 402
dirkx 0:355018f44c9f 403 /**
dirkx 0:355018f44c9f 404 * Adjusts the payload pointer to hide or reveal headers in the payload.
dirkx 0:355018f44c9f 405 *
dirkx 0:355018f44c9f 406 * Adjusts the ->payload pointer so that space for a header
dirkx 0:355018f44c9f 407 * (dis)appears in the pbuf payload.
dirkx 0:355018f44c9f 408 *
dirkx 0:355018f44c9f 409 * The ->payload, ->tot_len and ->len fields are adjusted.
dirkx 0:355018f44c9f 410 *
dirkx 0:355018f44c9f 411 * @param p pbuf to change the header size.
dirkx 0:355018f44c9f 412 * @param header_size_increment Number of bytes to increment header size which
dirkx 0:355018f44c9f 413 * increases the size of the pbuf. New space is on the front.
dirkx 0:355018f44c9f 414 * (Using a negative value decreases the header size.)
dirkx 0:355018f44c9f 415 * If hdr_size_inc is 0, this function does nothing and returns succesful.
dirkx 0:355018f44c9f 416 *
dirkx 0:355018f44c9f 417 * PBUF_ROM and PBUF_REF type buffers cannot have their sizes increased, so
dirkx 0:355018f44c9f 418 * the call will fail. A check is made that the increase in header size does
dirkx 0:355018f44c9f 419 * not move the payload pointer in front of the start of the buffer.
dirkx 0:355018f44c9f 420 * @return non-zero on failure, zero on success.
dirkx 0:355018f44c9f 421 *
dirkx 0:355018f44c9f 422 */
dirkx 0:355018f44c9f 423 u8_t
dirkx 0:355018f44c9f 424 pbuf_header(struct pbuf *p, s16_t header_size_increment)
dirkx 0:355018f44c9f 425 {
dirkx 0:355018f44c9f 426 u16_t type;
dirkx 0:355018f44c9f 427 void *payload;
dirkx 0:355018f44c9f 428 u16_t increment_magnitude;
dirkx 0:355018f44c9f 429
dirkx 0:355018f44c9f 430 LWIP_ASSERT("p != NULL", p != NULL);
dirkx 0:355018f44c9f 431 if ((header_size_increment == 0) || (p == NULL))
dirkx 0:355018f44c9f 432 return 0;
dirkx 0:355018f44c9f 433
dirkx 0:355018f44c9f 434 if (header_size_increment < 0){
dirkx 0:355018f44c9f 435 increment_magnitude = -header_size_increment;
dirkx 0:355018f44c9f 436 /* Check that we aren't going to move off the end of the pbuf */
dirkx 0:355018f44c9f 437 LWIP_ERROR("increment_magnitude <= p->len", (increment_magnitude <= p->len), return 1;);
dirkx 0:355018f44c9f 438 } else {
dirkx 0:355018f44c9f 439 increment_magnitude = header_size_increment;
dirkx 0:355018f44c9f 440 #if 0
dirkx 0:355018f44c9f 441 /* Can't assert these as some callers speculatively call
dirkx 0:355018f44c9f 442 pbuf_header() to see if it's OK. Will return 1 below instead. */
dirkx 0:355018f44c9f 443 /* Check that we've got the correct type of pbuf to work with */
dirkx 0:355018f44c9f 444 LWIP_ASSERT("p->type == PBUF_RAM || p->type == PBUF_POOL",
dirkx 0:355018f44c9f 445 p->type == PBUF_RAM || p->type == PBUF_POOL);
dirkx 0:355018f44c9f 446 /* Check that we aren't going to move off the beginning of the pbuf */
dirkx 0:355018f44c9f 447 LWIP_ASSERT("p->payload - increment_magnitude >= p + SIZEOF_STRUCT_PBUF",
dirkx 0:355018f44c9f 448 (u8_t *)p->payload - increment_magnitude >= (u8_t *)p + SIZEOF_STRUCT_PBUF);
dirkx 0:355018f44c9f 449 #endif
dirkx 0:355018f44c9f 450 }
dirkx 0:355018f44c9f 451
dirkx 0:355018f44c9f 452 type = p->type;
dirkx 0:355018f44c9f 453 /* remember current payload pointer */
dirkx 0:355018f44c9f 454 payload = p->payload;
dirkx 0:355018f44c9f 455
dirkx 0:355018f44c9f 456 /* pbuf types containing payloads? */
dirkx 0:355018f44c9f 457 if (type == PBUF_RAM || type == PBUF_POOL) {
dirkx 0:355018f44c9f 458 /* set new payload pointer */
dirkx 0:355018f44c9f 459 p->payload = (u8_t *)p->payload - header_size_increment;
dirkx 0:355018f44c9f 460 /* boundary check fails? */
dirkx 0:355018f44c9f 461 if ((u8_t *)p->payload < (u8_t *)p + SIZEOF_STRUCT_PBUF) {
dirkx 0:355018f44c9f 462 LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
dirkx 0:355018f44c9f 463 ("pbuf_header: failed as %p < %p (not enough space for new header size)\n",
dirkx 0:355018f44c9f 464 (void *)p->payload, (void *)(p + 1)));
dirkx 0:355018f44c9f 465 /* restore old payload pointer */
dirkx 0:355018f44c9f 466 p->payload = payload;
dirkx 0:355018f44c9f 467 /* bail out unsuccesfully */
dirkx 0:355018f44c9f 468 return 1;
dirkx 0:355018f44c9f 469 }
dirkx 0:355018f44c9f 470 /* pbuf types refering to external payloads? */
dirkx 0:355018f44c9f 471 } else if (type == PBUF_REF || type == PBUF_ROM) {
dirkx 0:355018f44c9f 472 /* hide a header in the payload? */
dirkx 0:355018f44c9f 473 if ((header_size_increment < 0) && (increment_magnitude <= p->len)) {
dirkx 0:355018f44c9f 474 /* increase payload pointer */
dirkx 0:355018f44c9f 475 p->payload = (u8_t *)p->payload - header_size_increment;
dirkx 0:355018f44c9f 476 } else {
dirkx 0:355018f44c9f 477 /* cannot expand payload to front (yet!)
dirkx 0:355018f44c9f 478 * bail out unsuccesfully */
dirkx 0:355018f44c9f 479 return 1;
dirkx 0:355018f44c9f 480 }
dirkx 0:355018f44c9f 481 }
dirkx 0:355018f44c9f 482 else {
dirkx 0:355018f44c9f 483 /* Unknown type */
dirkx 0:355018f44c9f 484 LWIP_ASSERT("bad pbuf type", 0);
dirkx 0:355018f44c9f 485 return 1;
dirkx 0:355018f44c9f 486 }
dirkx 0:355018f44c9f 487 /* modify pbuf length fields */
dirkx 0:355018f44c9f 488 p->len += header_size_increment;
dirkx 0:355018f44c9f 489 p->tot_len += header_size_increment;
dirkx 0:355018f44c9f 490
dirkx 0:355018f44c9f 491 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_header: old %p new %p (%"S16_F")\n",
dirkx 0:355018f44c9f 492 (void *)payload, (void *)p->payload, header_size_increment));
dirkx 0:355018f44c9f 493
dirkx 0:355018f44c9f 494 return 0;
dirkx 0:355018f44c9f 495 }
dirkx 0:355018f44c9f 496
dirkx 0:355018f44c9f 497 /**
dirkx 0:355018f44c9f 498 * Dereference a pbuf chain or queue and deallocate any no-longer-used
dirkx 0:355018f44c9f 499 * pbufs at the head of this chain or queue.
dirkx 0:355018f44c9f 500 *
dirkx 0:355018f44c9f 501 * Decrements the pbuf reference count. If it reaches zero, the pbuf is
dirkx 0:355018f44c9f 502 * deallocated.
dirkx 0:355018f44c9f 503 *
dirkx 0:355018f44c9f 504 * For a pbuf chain, this is repeated for each pbuf in the chain,
dirkx 0:355018f44c9f 505 * up to the first pbuf which has a non-zero reference count after
dirkx 0:355018f44c9f 506 * decrementing. So, when all reference counts are one, the whole
dirkx 0:355018f44c9f 507 * chain is free'd.
dirkx 0:355018f44c9f 508 *
dirkx 0:355018f44c9f 509 * @param p The pbuf (chain) to be dereferenced.
dirkx 0:355018f44c9f 510 *
dirkx 0:355018f44c9f 511 * @return the number of pbufs that were de-allocated
dirkx 0:355018f44c9f 512 * from the head of the chain.
dirkx 0:355018f44c9f 513 *
dirkx 0:355018f44c9f 514 * @note MUST NOT be called on a packet queue (Not verified to work yet).
dirkx 0:355018f44c9f 515 * @note the reference counter of a pbuf equals the number of pointers
dirkx 0:355018f44c9f 516 * that refer to the pbuf (or into the pbuf).
dirkx 0:355018f44c9f 517 *
dirkx 0:355018f44c9f 518 * @internal examples:
dirkx 0:355018f44c9f 519 *
dirkx 0:355018f44c9f 520 * Assuming existing chains a->b->c with the following reference
dirkx 0:355018f44c9f 521 * counts, calling pbuf_free(a) results in:
dirkx 0:355018f44c9f 522 *
dirkx 0:355018f44c9f 523 * 1->2->3 becomes ...1->3
dirkx 0:355018f44c9f 524 * 3->3->3 becomes 2->3->3
dirkx 0:355018f44c9f 525 * 1->1->2 becomes ......1
dirkx 0:355018f44c9f 526 * 2->1->1 becomes 1->1->1
dirkx 0:355018f44c9f 527 * 1->1->1 becomes .......
dirkx 0:355018f44c9f 528 *
dirkx 0:355018f44c9f 529 */
dirkx 0:355018f44c9f 530 u8_t
dirkx 0:355018f44c9f 531 pbuf_free(struct pbuf *p)
dirkx 0:355018f44c9f 532 {
dirkx 0:355018f44c9f 533 u16_t type;
dirkx 0:355018f44c9f 534 struct pbuf *q;
dirkx 0:355018f44c9f 535 u8_t count;
dirkx 0:355018f44c9f 536
dirkx 0:355018f44c9f 537 if (p == NULL) {
dirkx 0:355018f44c9f 538 LWIP_ASSERT("p != NULL", p != NULL);
dirkx 0:355018f44c9f 539 /* if assertions are disabled, proceed with debug output */
dirkx 0:355018f44c9f 540 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
dirkx 0:355018f44c9f 541 ("pbuf_free(p == NULL) was called.\n"));
dirkx 0:355018f44c9f 542 return 0;
dirkx 0:355018f44c9f 543 }
dirkx 0:355018f44c9f 544 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free(%p)\n", (void *)p));
dirkx 0:355018f44c9f 545
dirkx 0:355018f44c9f 546 PERF_START;
dirkx 0:355018f44c9f 547
dirkx 0:355018f44c9f 548 LWIP_ASSERT("pbuf_free: sane type",
dirkx 0:355018f44c9f 549 p->type == PBUF_RAM || p->type == PBUF_ROM ||
dirkx 0:355018f44c9f 550 p->type == PBUF_REF || p->type == PBUF_POOL);
dirkx 0:355018f44c9f 551
dirkx 0:355018f44c9f 552 count = 0;
dirkx 0:355018f44c9f 553 /* de-allocate all consecutive pbufs from the head of the chain that
dirkx 0:355018f44c9f 554 * obtain a zero reference count after decrementing*/
dirkx 0:355018f44c9f 555 while (p != NULL) {
dirkx 0:355018f44c9f 556 u16_t ref;
dirkx 0:355018f44c9f 557 SYS_ARCH_DECL_PROTECT(old_level);
dirkx 0:355018f44c9f 558 /* Since decrementing ref cannot be guaranteed to be a single machine operation
dirkx 0:355018f44c9f 559 * we must protect it. We put the new ref into a local variable to prevent
dirkx 0:355018f44c9f 560 * further protection. */
dirkx 0:355018f44c9f 561 SYS_ARCH_PROTECT(old_level);
dirkx 0:355018f44c9f 562 /* all pbufs in a chain are referenced at least once */
dirkx 0:355018f44c9f 563 LWIP_ASSERT("pbuf_free: p->ref > 0", p->ref > 0);
dirkx 0:355018f44c9f 564 /* decrease reference count (number of pointers to pbuf) */
dirkx 0:355018f44c9f 565 ref = --(p->ref);
dirkx 0:355018f44c9f 566 SYS_ARCH_UNPROTECT(old_level);
dirkx 0:355018f44c9f 567 /* this pbuf is no longer referenced to? */
dirkx 0:355018f44c9f 568 if (ref == 0) {
dirkx 0:355018f44c9f 569 /* remember next pbuf in chain for next iteration */
dirkx 0:355018f44c9f 570 q = p->next;
dirkx 0:355018f44c9f 571 LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: deallocating %p\n", (void *)p));
dirkx 0:355018f44c9f 572 type = p->type;
dirkx 0:355018f44c9f 573 /* is this a pbuf from the pool? */
dirkx 0:355018f44c9f 574 if (type == PBUF_POOL) {
dirkx 0:355018f44c9f 575 memp_free(MEMP_PBUF_POOL, p);
dirkx 0:355018f44c9f 576 /* is this a ROM or RAM referencing pbuf? */
dirkx 0:355018f44c9f 577 } else if (type == PBUF_ROM || type == PBUF_REF) {
dirkx 0:355018f44c9f 578 memp_free(MEMP_PBUF, p);
dirkx 0:355018f44c9f 579 /* type == PBUF_RAM */
dirkx 0:355018f44c9f 580 } else {
dirkx 0:355018f44c9f 581 mem_free(p);
dirkx 0:355018f44c9f 582 }
dirkx 0:355018f44c9f 583 count++;
dirkx 0:355018f44c9f 584 /* proceed to next pbuf */
dirkx 0:355018f44c9f 585 p = q;
dirkx 0:355018f44c9f 586 /* p->ref > 0, this pbuf is still referenced to */
dirkx 0:355018f44c9f 587 /* (and so the remaining pbufs in chain as well) */
dirkx 0:355018f44c9f 588 } else {
dirkx 0:355018f44c9f 589 LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: %p has ref %"U16_F", ending here.\n", (void *)p, ref));
dirkx 0:355018f44c9f 590 /* stop walking through the chain */
dirkx 0:355018f44c9f 591 p = NULL;
dirkx 0:355018f44c9f 592 }
dirkx 0:355018f44c9f 593 }
dirkx 0:355018f44c9f 594 PERF_STOP("pbuf_free");
dirkx 0:355018f44c9f 595 /* return number of de-allocated pbufs */
dirkx 0:355018f44c9f 596 return count;
dirkx 0:355018f44c9f 597 }
dirkx 0:355018f44c9f 598
dirkx 0:355018f44c9f 599 /**
dirkx 0:355018f44c9f 600 * Count number of pbufs in a chain
dirkx 0:355018f44c9f 601 *
dirkx 0:355018f44c9f 602 * @param p first pbuf of chain
dirkx 0:355018f44c9f 603 * @return the number of pbufs in a chain
dirkx 0:355018f44c9f 604 */
dirkx 0:355018f44c9f 605
dirkx 0:355018f44c9f 606 u8_t
dirkx 0:355018f44c9f 607 pbuf_clen(struct pbuf *p)
dirkx 0:355018f44c9f 608 {
dirkx 0:355018f44c9f 609 u8_t len;
dirkx 0:355018f44c9f 610
dirkx 0:355018f44c9f 611 len = 0;
dirkx 0:355018f44c9f 612 while (p != NULL) {
dirkx 0:355018f44c9f 613 ++len;
dirkx 0:355018f44c9f 614 p = p->next;
dirkx 0:355018f44c9f 615 }
dirkx 0:355018f44c9f 616 return len;
dirkx 0:355018f44c9f 617 }
dirkx 0:355018f44c9f 618
dirkx 0:355018f44c9f 619 /**
dirkx 0:355018f44c9f 620 * Increment the reference count of the pbuf.
dirkx 0:355018f44c9f 621 *
dirkx 0:355018f44c9f 622 * @param p pbuf to increase reference counter of
dirkx 0:355018f44c9f 623 *
dirkx 0:355018f44c9f 624 */
dirkx 0:355018f44c9f 625 void
dirkx 0:355018f44c9f 626 pbuf_ref(struct pbuf *p)
dirkx 0:355018f44c9f 627 {
dirkx 0:355018f44c9f 628 SYS_ARCH_DECL_PROTECT(old_level);
dirkx 0:355018f44c9f 629 /* pbuf given? */
dirkx 0:355018f44c9f 630 if (p != NULL) {
dirkx 0:355018f44c9f 631 SYS_ARCH_PROTECT(old_level);
dirkx 0:355018f44c9f 632 ++(p->ref);
dirkx 0:355018f44c9f 633 SYS_ARCH_UNPROTECT(old_level);
dirkx 0:355018f44c9f 634 }
dirkx 0:355018f44c9f 635 }
dirkx 0:355018f44c9f 636
dirkx 0:355018f44c9f 637 /**
dirkx 0:355018f44c9f 638 * Concatenate two pbufs (each may be a pbuf chain) and take over
dirkx 0:355018f44c9f 639 * the caller's reference of the tail pbuf.
dirkx 0:355018f44c9f 640 *
dirkx 0:355018f44c9f 641 * @note The caller MAY NOT reference the tail pbuf afterwards.
dirkx 0:355018f44c9f 642 * Use pbuf_chain() for that purpose.
dirkx 0:355018f44c9f 643 *
dirkx 0:355018f44c9f 644 * @see pbuf_chain()
dirkx 0:355018f44c9f 645 */
dirkx 0:355018f44c9f 646
dirkx 0:355018f44c9f 647 void
dirkx 0:355018f44c9f 648 pbuf_cat(struct pbuf *h, struct pbuf *t)
dirkx 0:355018f44c9f 649 {
dirkx 0:355018f44c9f 650 struct pbuf *p;
dirkx 0:355018f44c9f 651
dirkx 0:355018f44c9f 652 LWIP_ERROR("(h != NULL) && (t != NULL) (programmer violates API)",
dirkx 0:355018f44c9f 653 ((h != NULL) && (t != NULL)), return;);
dirkx 0:355018f44c9f 654
dirkx 0:355018f44c9f 655 /* proceed to last pbuf of chain */
dirkx 0:355018f44c9f 656 for (p = h; p->next != NULL; p = p->next) {
dirkx 0:355018f44c9f 657 /* add total length of second chain to all totals of first chain */
dirkx 0:355018f44c9f 658 p->tot_len += t->tot_len;
dirkx 0:355018f44c9f 659 }
dirkx 0:355018f44c9f 660 /* { p is last pbuf of first h chain, p->next == NULL } */
dirkx 0:355018f44c9f 661 LWIP_ASSERT("p->tot_len == p->len (of last pbuf in chain)", p->tot_len == p->len);
dirkx 0:355018f44c9f 662 LWIP_ASSERT("p->next == NULL", p->next == NULL);
dirkx 0:355018f44c9f 663 /* add total length of second chain to last pbuf total of first chain */
dirkx 0:355018f44c9f 664 p->tot_len += t->tot_len;
dirkx 0:355018f44c9f 665 /* chain last pbuf of head (p) with first of tail (t) */
dirkx 0:355018f44c9f 666 p->next = t;
dirkx 0:355018f44c9f 667 /* p->next now references t, but the caller will drop its reference to t,
dirkx 0:355018f44c9f 668 * so netto there is no change to the reference count of t.
dirkx 0:355018f44c9f 669 */
dirkx 0:355018f44c9f 670 }
dirkx 0:355018f44c9f 671
dirkx 0:355018f44c9f 672 /**
dirkx 0:355018f44c9f 673 * Chain two pbufs (or pbuf chains) together.
dirkx 0:355018f44c9f 674 *
dirkx 0:355018f44c9f 675 * The caller MUST call pbuf_free(t) once it has stopped
dirkx 0:355018f44c9f 676 * using it. Use pbuf_cat() instead if you no longer use t.
dirkx 0:355018f44c9f 677 *
dirkx 0:355018f44c9f 678 * @param h head pbuf (chain)
dirkx 0:355018f44c9f 679 * @param t tail pbuf (chain)
dirkx 0:355018f44c9f 680 * @note The pbufs MUST belong to the same packet.
dirkx 0:355018f44c9f 681 * @note MAY NOT be called on a packet queue.
dirkx 0:355018f44c9f 682 *
dirkx 0:355018f44c9f 683 * The ->tot_len fields of all pbufs of the head chain are adjusted.
dirkx 0:355018f44c9f 684 * The ->next field of the last pbuf of the head chain is adjusted.
dirkx 0:355018f44c9f 685 * The ->ref field of the first pbuf of the tail chain is adjusted.
dirkx 0:355018f44c9f 686 *
dirkx 0:355018f44c9f 687 */
dirkx 0:355018f44c9f 688 void
dirkx 0:355018f44c9f 689 pbuf_chain(struct pbuf *h, struct pbuf *t)
dirkx 0:355018f44c9f 690 {
dirkx 0:355018f44c9f 691 pbuf_cat(h, t);
dirkx 0:355018f44c9f 692 /* t is now referenced by h */
dirkx 0:355018f44c9f 693 pbuf_ref(t);
dirkx 0:355018f44c9f 694 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_chain: %p references %p\n", (void *)h, (void *)t));
dirkx 0:355018f44c9f 695 }
dirkx 0:355018f44c9f 696
dirkx 0:355018f44c9f 697 /**
dirkx 0:355018f44c9f 698 * Dechains the first pbuf from its succeeding pbufs in the chain.
dirkx 0:355018f44c9f 699 *
dirkx 0:355018f44c9f 700 * Makes p->tot_len field equal to p->len.
dirkx 0:355018f44c9f 701 * @param p pbuf to dechain
dirkx 0:355018f44c9f 702 * @return remainder of the pbuf chain, or NULL if it was de-allocated.
dirkx 0:355018f44c9f 703 * @note May not be called on a packet queue.
dirkx 0:355018f44c9f 704 */
dirkx 0:355018f44c9f 705 struct pbuf *
dirkx 0:355018f44c9f 706 pbuf_dechain(struct pbuf *p)
dirkx 0:355018f44c9f 707 {
dirkx 0:355018f44c9f 708 struct pbuf *q;
dirkx 0:355018f44c9f 709 u8_t tail_gone = 1;
dirkx 0:355018f44c9f 710 /* tail */
dirkx 0:355018f44c9f 711 q = p->next;
dirkx 0:355018f44c9f 712 /* pbuf has successor in chain? */
dirkx 0:355018f44c9f 713 if (q != NULL) {
dirkx 0:355018f44c9f 714 /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
dirkx 0:355018f44c9f 715 LWIP_ASSERT("p->tot_len == p->len + q->tot_len", q->tot_len == p->tot_len - p->len);
dirkx 0:355018f44c9f 716 /* enforce invariant if assertion is disabled */
dirkx 0:355018f44c9f 717 q->tot_len = p->tot_len - p->len;
dirkx 0:355018f44c9f 718 /* decouple pbuf from remainder */
dirkx 0:355018f44c9f 719 p->next = NULL;
dirkx 0:355018f44c9f 720 /* total length of pbuf p is its own length only */
dirkx 0:355018f44c9f 721 p->tot_len = p->len;
dirkx 0:355018f44c9f 722 /* q is no longer referenced by p, free it */
dirkx 0:355018f44c9f 723 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_dechain: unreferencing %p\n", (void *)q));
dirkx 0:355018f44c9f 724 tail_gone = pbuf_free(q);
dirkx 0:355018f44c9f 725 if (tail_gone > 0) {
dirkx 0:355018f44c9f 726 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE,
dirkx 0:355018f44c9f 727 ("pbuf_dechain: deallocated %p (as it is no longer referenced)\n", (void *)q));
dirkx 0:355018f44c9f 728 }
dirkx 0:355018f44c9f 729 /* return remaining tail or NULL if deallocated */
dirkx 0:355018f44c9f 730 }
dirkx 0:355018f44c9f 731 /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
dirkx 0:355018f44c9f 732 LWIP_ASSERT("p->tot_len == p->len", p->tot_len == p->len);
dirkx 0:355018f44c9f 733 return ((tail_gone > 0) ? NULL : q);
dirkx 0:355018f44c9f 734 }
dirkx 0:355018f44c9f 735
dirkx 0:355018f44c9f 736 /**
dirkx 0:355018f44c9f 737 *
dirkx 0:355018f44c9f 738 * Create PBUF_RAM copies of pbufs.
dirkx 0:355018f44c9f 739 *
dirkx 0:355018f44c9f 740 * Used to queue packets on behalf of the lwIP stack, such as
dirkx 0:355018f44c9f 741 * ARP based queueing.
dirkx 0:355018f44c9f 742 *
dirkx 0:355018f44c9f 743 * @note You MUST explicitly use p = pbuf_take(p);
dirkx 0:355018f44c9f 744 *
dirkx 0:355018f44c9f 745 * @note Only one packet is copied, no packet queue!
dirkx 0:355018f44c9f 746 *
dirkx 0:355018f44c9f 747 * @param p_to pbuf destination of the copy
dirkx 0:355018f44c9f 748 * @param p_from pbuf source of the copy
dirkx 0:355018f44c9f 749 *
dirkx 0:355018f44c9f 750 * @return ERR_OK if pbuf was copied
dirkx 0:355018f44c9f 751 * ERR_ARG if one of the pbufs is NULL or p_to is not big
dirkx 0:355018f44c9f 752 * enough to hold p_from
dirkx 0:355018f44c9f 753 */
dirkx 0:355018f44c9f 754 err_t
dirkx 0:355018f44c9f 755 pbuf_copy(struct pbuf *p_to, struct pbuf *p_from)
dirkx 0:355018f44c9f 756 {
dirkx 0:355018f44c9f 757 u16_t offset_to=0, offset_from=0, len;
dirkx 0:355018f44c9f 758
dirkx 0:355018f44c9f 759 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy(%p, %p)\n",
dirkx 0:355018f44c9f 760 (void*)p_to, (void*)p_from));
dirkx 0:355018f44c9f 761
dirkx 0:355018f44c9f 762 /* is the target big enough to hold the source? */
dirkx 0:355018f44c9f 763 LWIP_ERROR("pbuf_copy: target not big enough to hold source", ((p_to != NULL) &&
dirkx 0:355018f44c9f 764 (p_from != NULL) && (p_to->tot_len >= p_from->tot_len)), return ERR_ARG;);
dirkx 0:355018f44c9f 765
dirkx 0:355018f44c9f 766 /* iterate through pbuf chain */
dirkx 0:355018f44c9f 767 do
dirkx 0:355018f44c9f 768 {
dirkx 0:355018f44c9f 769 LWIP_ASSERT("p_to != NULL", p_to != NULL);
dirkx 0:355018f44c9f 770 /* copy one part of the original chain */
dirkx 0:355018f44c9f 771 if ((p_to->len - offset_to) >= (p_from->len - offset_from)) {
dirkx 0:355018f44c9f 772 /* complete current p_from fits into current p_to */
dirkx 0:355018f44c9f 773 len = p_from->len - offset_from;
dirkx 0:355018f44c9f 774 } else {
dirkx 0:355018f44c9f 775 /* current p_from does not fit into current p_to */
dirkx 0:355018f44c9f 776 len = p_to->len - offset_to;
dirkx 0:355018f44c9f 777 }
dirkx 0:355018f44c9f 778 MEMCPY((u8_t*)p_to->payload + offset_to, (u8_t*)p_from->payload + offset_from, len);
dirkx 0:355018f44c9f 779 offset_to += len;
dirkx 0:355018f44c9f 780 offset_from += len;
dirkx 0:355018f44c9f 781 LWIP_ASSERT("offset_to <= p_to->len", offset_to <= p_to->len);
dirkx 0:355018f44c9f 782 if (offset_to == p_to->len) {
dirkx 0:355018f44c9f 783 /* on to next p_to (if any) */
dirkx 0:355018f44c9f 784 offset_to = 0;
dirkx 0:355018f44c9f 785 p_to = p_to->next;
dirkx 0:355018f44c9f 786 }
dirkx 0:355018f44c9f 787 LWIP_ASSERT("offset_from <= p_from->len", offset_from <= p_from->len);
dirkx 0:355018f44c9f 788 if (offset_from >= p_from->len) {
dirkx 0:355018f44c9f 789 /* on to next p_from (if any) */
dirkx 0:355018f44c9f 790 offset_from = 0;
dirkx 0:355018f44c9f 791 p_from = p_from->next;
dirkx 0:355018f44c9f 792 }
dirkx 0:355018f44c9f 793
dirkx 0:355018f44c9f 794 if((p_from != NULL) && (p_from->len == p_from->tot_len)) {
dirkx 0:355018f44c9f 795 /* don't copy more than one packet! */
dirkx 0:355018f44c9f 796 LWIP_ERROR("pbuf_copy() does not allow packet queues!\n",
dirkx 0:355018f44c9f 797 (p_from->next == NULL), return ERR_VAL;);
dirkx 0:355018f44c9f 798 }
dirkx 0:355018f44c9f 799 if((p_to != NULL) && (p_to->len == p_to->tot_len)) {
dirkx 0:355018f44c9f 800 /* don't copy more than one packet! */
dirkx 0:355018f44c9f 801 LWIP_ERROR("pbuf_copy() does not allow packet queues!\n",
dirkx 0:355018f44c9f 802 (p_to->next == NULL), return ERR_VAL;);
dirkx 0:355018f44c9f 803 }
dirkx 0:355018f44c9f 804 } while (p_from);
dirkx 0:355018f44c9f 805 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy: end of chain reached.\n"));
dirkx 0:355018f44c9f 806 return ERR_OK;
dirkx 0:355018f44c9f 807 }
dirkx 0:355018f44c9f 808
dirkx 0:355018f44c9f 809 /**
dirkx 0:355018f44c9f 810 * Copy (part of) the contents of a packet buffer
dirkx 0:355018f44c9f 811 * to an application supplied buffer.
dirkx 0:355018f44c9f 812 *
dirkx 0:355018f44c9f 813 * @param buf the pbuf from which to copy data
dirkx 0:355018f44c9f 814 * @param dataptr the application supplied buffer
dirkx 0:355018f44c9f 815 * @param len length of data to copy (dataptr must be big enough). No more
dirkx 0:355018f44c9f 816 * than buf->tot_len will be copied, irrespective of len
dirkx 0:355018f44c9f 817 * @param offset offset into the packet buffer from where to begin copying len bytes
dirkx 0:355018f44c9f 818 * @return the number of bytes copied, or 0 on failure
dirkx 0:355018f44c9f 819 */
dirkx 0:355018f44c9f 820 u16_t
dirkx 0:355018f44c9f 821 pbuf_copy_partial(struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
dirkx 0:355018f44c9f 822 {
dirkx 0:355018f44c9f 823 struct pbuf *p;
dirkx 0:355018f44c9f 824 u16_t left;
dirkx 0:355018f44c9f 825 u16_t buf_copy_len;
dirkx 0:355018f44c9f 826 u16_t copied_total = 0;
dirkx 0:355018f44c9f 827
dirkx 0:355018f44c9f 828 LWIP_ERROR("pbuf_copy_partial: invalid buf", (buf != NULL), return 0;);
dirkx 0:355018f44c9f 829 LWIP_ERROR("pbuf_copy_partial: invalid dataptr", (dataptr != NULL), return 0;);
dirkx 0:355018f44c9f 830
dirkx 0:355018f44c9f 831 left = 0;
dirkx 0:355018f44c9f 832
dirkx 0:355018f44c9f 833 if((buf == NULL) || (dataptr == NULL)) {
dirkx 0:355018f44c9f 834 return 0;
dirkx 0:355018f44c9f 835 }
dirkx 0:355018f44c9f 836
dirkx 0:355018f44c9f 837 /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
dirkx 0:355018f44c9f 838 for(p = buf; len != 0 && p != NULL; p = p->next) {
dirkx 0:355018f44c9f 839 if ((offset != 0) && (offset >= p->len)) {
dirkx 0:355018f44c9f 840 /* don't copy from this buffer -> on to the next */
dirkx 0:355018f44c9f 841 offset -= p->len;
dirkx 0:355018f44c9f 842 } else {
dirkx 0:355018f44c9f 843 /* copy from this buffer. maybe only partially. */
dirkx 0:355018f44c9f 844 buf_copy_len = p->len - offset;
dirkx 0:355018f44c9f 845 if (buf_copy_len > len)
dirkx 0:355018f44c9f 846 buf_copy_len = len;
dirkx 0:355018f44c9f 847 /* copy the necessary parts of the buffer */
dirkx 0:355018f44c9f 848 MEMCPY(&((char*)dataptr)[left], &((char*)p->payload)[offset], buf_copy_len);
dirkx 0:355018f44c9f 849 copied_total += buf_copy_len;
dirkx 0:355018f44c9f 850 left += buf_copy_len;
dirkx 0:355018f44c9f 851 len -= buf_copy_len;
dirkx 0:355018f44c9f 852 offset = 0;
dirkx 0:355018f44c9f 853 }
dirkx 0:355018f44c9f 854 }
dirkx 0:355018f44c9f 855 return copied_total;
dirkx 0:355018f44c9f 856 }
dirkx 0:355018f44c9f 857
dirkx 0:355018f44c9f 858 /**
dirkx 0:355018f44c9f 859 * Copy application supplied data into a pbuf.
dirkx 0:355018f44c9f 860 * This function can only be used to copy the equivalent of buf->tot_len data.
dirkx 0:355018f44c9f 861 *
dirkx 0:355018f44c9f 862 * @param buf pbuf to fill with data
dirkx 0:355018f44c9f 863 * @param dataptr application supplied data buffer
dirkx 0:355018f44c9f 864 * @param len length of the application supplied data buffer
dirkx 0:355018f44c9f 865 *
dirkx 0:355018f44c9f 866 * @return ERR_OK if successful, ERR_MEM if the pbuf is not big enough
dirkx 0:355018f44c9f 867 */
dirkx 0:355018f44c9f 868 err_t
dirkx 0:355018f44c9f 869 pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)
dirkx 0:355018f44c9f 870 {
dirkx 0:355018f44c9f 871 struct pbuf *p;
dirkx 0:355018f44c9f 872 u16_t buf_copy_len;
dirkx 0:355018f44c9f 873 u16_t total_copy_len = len;
dirkx 0:355018f44c9f 874 u16_t copied_total = 0;
dirkx 0:355018f44c9f 875
dirkx 0:355018f44c9f 876 LWIP_ERROR("pbuf_take: invalid buf", (buf != NULL), return 0;);
dirkx 0:355018f44c9f 877 LWIP_ERROR("pbuf_take: invalid dataptr", (dataptr != NULL), return 0;);
dirkx 0:355018f44c9f 878
dirkx 0:355018f44c9f 879 if ((buf == NULL) || (dataptr == NULL) || (buf->tot_len < len)) {
dirkx 0:355018f44c9f 880 return ERR_ARG;
dirkx 0:355018f44c9f 881 }
dirkx 0:355018f44c9f 882
dirkx 0:355018f44c9f 883 /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
dirkx 0:355018f44c9f 884 for(p = buf; total_copy_len != 0; p = p->next) {
dirkx 0:355018f44c9f 885 LWIP_ASSERT("pbuf_take: invalid pbuf", p != NULL);
dirkx 0:355018f44c9f 886 buf_copy_len = total_copy_len;
dirkx 0:355018f44c9f 887 if (buf_copy_len > p->len) {
dirkx 0:355018f44c9f 888 /* this pbuf cannot hold all remaining data */
dirkx 0:355018f44c9f 889 buf_copy_len = p->len;
dirkx 0:355018f44c9f 890 }
dirkx 0:355018f44c9f 891 /* copy the necessary parts of the buffer */
dirkx 0:355018f44c9f 892 MEMCPY(p->payload, &((char*)dataptr)[copied_total], buf_copy_len);
dirkx 0:355018f44c9f 893 total_copy_len -= buf_copy_len;
dirkx 0:355018f44c9f 894 copied_total += buf_copy_len;
dirkx 0:355018f44c9f 895 }
dirkx 0:355018f44c9f 896 LWIP_ASSERT("did not copy all data", total_copy_len == 0 && copied_total == len);
dirkx 0:355018f44c9f 897 return ERR_OK;
dirkx 0:355018f44c9f 898 }
dirkx 0:355018f44c9f 899
dirkx 0:355018f44c9f 900 /**
dirkx 0:355018f44c9f 901 * Creates a single pbuf out of a queue of pbufs.
dirkx 0:355018f44c9f 902 *
dirkx 0:355018f44c9f 903 * @remark: The source pbuf 'p' is not freed by this function because that can
dirkx 0:355018f44c9f 904 * be illegal in some places!
dirkx 0:355018f44c9f 905 *
dirkx 0:355018f44c9f 906 * @param p the source pbuf
dirkx 0:355018f44c9f 907 * @param layer pbuf_layer of the new pbuf
dirkx 0:355018f44c9f 908 *
dirkx 0:355018f44c9f 909 * @return a new, single pbuf (p->next is NULL)
dirkx 0:355018f44c9f 910 * or the old pbuf if allocation fails
dirkx 0:355018f44c9f 911 */
dirkx 0:355018f44c9f 912 struct pbuf*
dirkx 0:355018f44c9f 913 pbuf_coalesce(struct pbuf *p, pbuf_layer layer)
dirkx 0:355018f44c9f 914 {
dirkx 0:355018f44c9f 915 struct pbuf *q;
dirkx 0:355018f44c9f 916 err_t err;
dirkx 0:355018f44c9f 917 if (p->next == NULL) {
dirkx 0:355018f44c9f 918 return p;
dirkx 0:355018f44c9f 919 }
dirkx 0:355018f44c9f 920 q = pbuf_alloc(layer, p->tot_len, PBUF_RAM);
dirkx 0:355018f44c9f 921 if (q == NULL) {
dirkx 0:355018f44c9f 922 /* @todo: what do we do now? */
dirkx 0:355018f44c9f 923 return p;
dirkx 0:355018f44c9f 924 }
dirkx 0:355018f44c9f 925 err = pbuf_copy(q, p);
dirkx 0:355018f44c9f 926 LWIP_ASSERT("pbuf_copy failed", err == ERR_OK);
dirkx 0:355018f44c9f 927 pbuf_free(p);
dirkx 0:355018f44c9f 928 return q;
dirkx 0:355018f44c9f 929 }