Some quick code to use UDP-only (no TCP) with mBed. Echos received packets and sends packets when a button is pressed

Dependencies:   mbed

Committer:
pehrhovey
Date:
Sun Mar 14 00:54:12 2010 +0000
Revision:
0:a548a085de55

        

Who changed what in which revision?

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