Marco Zecchini
/
Example_RTOS
Rtos API example
Embed:
(wiki syntax)
Show/hide line numbers
lwip_pbuf.c
Go to the documentation of this file.
00001 /** 00002 * @file 00003 * Packet buffer management 00004 */ 00005 00006 /** 00007 * @defgroup pbuf Packet buffers (PBUF) 00008 * @ingroup infrastructure 00009 * 00010 * Packets are built from the pbuf data structure. It supports dynamic 00011 * memory allocation for packet contents or can reference externally 00012 * managed packet contents both in RAM and ROM. Quick allocation for 00013 * incoming packets is provided through pools with fixed sized pbufs. 00014 * 00015 * A packet may span over multiple pbufs, chained as a singly linked 00016 * list. This is called a "pbuf chain". 00017 * 00018 * Multiple packets may be queued, also using this singly linked list. 00019 * This is called a "packet queue". 00020 * 00021 * So, a packet queue consists of one or more pbuf chains, each of 00022 * which consist of one or more pbufs. CURRENTLY, PACKET QUEUES ARE 00023 * NOT SUPPORTED!!! Use helper structs to queue multiple packets. 00024 * 00025 * The differences between a pbuf chain and a packet queue are very 00026 * precise but subtle. 00027 * 00028 * The last pbuf of a packet has a ->tot_len field that equals the 00029 * ->len field. It can be found by traversing the list. If the last 00030 * pbuf of a packet has a ->next field other than NULL, more packets 00031 * are on the queue. 00032 * 00033 * Therefore, looping through a pbuf of a single packet, has an 00034 * loop end condition (tot_len == p->len), NOT (next == NULL). 00035 * 00036 * Example of custom pbuf usage for zero-copy RX: 00037 @code{.c} 00038 typedef struct my_custom_pbuf 00039 { 00040 struct pbuf_custom p; 00041 void* dma_descriptor; 00042 } my_custom_pbuf_t; 00043 00044 LWIP_MEMPOOL_DECLARE(RX_POOL, 10, sizeof(my_custom_pbuf_t), "Zero-copy RX PBUF pool"); 00045 00046 void my_pbuf_free_custom(void* p) 00047 { 00048 my_custom_pbuf_t* my_puf = (my_custom_pbuf_t*)p; 00049 00050 LOCK_INTERRUPTS(); 00051 free_rx_dma_descriptor(my_pbuf->dma_descriptor); 00052 LWIP_MEMPOOL_FREE(RX_POOL, my_pbuf); 00053 UNLOCK_INTERRUPTS(); 00054 } 00055 00056 void eth_rx_irq() 00057 { 00058 dma_descriptor* dma_desc = get_RX_DMA_descriptor_from_ethernet(); 00059 my_custom_pbuf_t* my_pbuf = (my_custom_pbuf_t*)LWIP_MEMPOOL_ALLOC(RX_POOL); 00060 00061 my_pbuf->p.custom_free_function = my_pbuf_free_custom; 00062 my_pbuf->dma_descriptor = dma_desc; 00063 00064 invalidate_cpu_cache(dma_desc->rx_data, dma_desc->rx_length); 00065 00066 struct pbuf* p = pbuf_alloced_custom(PBUF_RAW, 00067 dma_desc->rx_length, 00068 PBUF_REF, 00069 &my_pbuf->p, 00070 dma_desc->rx_data, 00071 dma_desc->max_buffer_size); 00072 00073 if(netif->input(p, netif) != ERR_OK) { 00074 pbuf_free(p); 00075 } 00076 } 00077 @endcode 00078 */ 00079 00080 /* 00081 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 00082 * All rights reserved. 00083 * 00084 * Redistribution and use in source and binary forms, with or without modification, 00085 * are permitted provided that the following conditions are met: 00086 * 00087 * 1. Redistributions of source code must retain the above copyright notice, 00088 * this list of conditions and the following disclaimer. 00089 * 2. Redistributions in binary form must reproduce the above copyright notice, 00090 * this list of conditions and the following disclaimer in the documentation 00091 * and/or other materials provided with the distribution. 00092 * 3. The name of the author may not be used to endorse or promote products 00093 * derived from this software without specific prior written permission. 00094 * 00095 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00096 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00097 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00098 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00099 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00100 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00101 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00102 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00103 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00104 * OF SUCH DAMAGE. 00105 * 00106 * This file is part of the lwIP TCP/IP stack. 00107 * 00108 * Author: Adam Dunkels <adam@sics.se> 00109 * 00110 */ 00111 00112 #include "lwip/opt.h" 00113 00114 #include "lwip/stats.h" 00115 #include "lwip/def.h" 00116 #include "lwip/mem.h" 00117 #include "lwip/memp.h" 00118 #include "lwip/pbuf.h" 00119 #include "lwip/sys.h" 00120 #if LWIP_TCP && TCP_QUEUE_OOSEQ 00121 #include "lwip/priv/tcp_priv.h" 00122 #endif 00123 #if LWIP_CHECKSUM_ON_COPY 00124 #include "lwip/inet_chksum.h" 00125 #endif 00126 00127 #include <string.h> 00128 00129 #define SIZEOF_STRUCT_PBUF LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf)) 00130 /* Since the pool is created in memp, PBUF_POOL_BUFSIZE will be automatically 00131 aligned there. Therefore, PBUF_POOL_BUFSIZE_ALIGNED can be used here. */ 00132 #define PBUF_POOL_BUFSIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE) 00133 00134 #if !LWIP_TCP || !TCP_QUEUE_OOSEQ || !PBUF_POOL_FREE_OOSEQ 00135 #define PBUF_POOL_IS_EMPTY() 00136 #else /* !LWIP_TCP || !TCP_QUEUE_OOSEQ || !PBUF_POOL_FREE_OOSEQ */ 00137 00138 #if !NO_SYS 00139 #ifndef PBUF_POOL_FREE_OOSEQ_QUEUE_CALL 00140 #include "lwip/tcpip.h" 00141 #define PBUF_POOL_FREE_OOSEQ_QUEUE_CALL() do { \ 00142 if (tcpip_callback_with_block(pbuf_free_ooseq_callback, NULL, 0) != ERR_OK) { \ 00143 SYS_ARCH_PROTECT(old_level); \ 00144 pbuf_free_ooseq_pending = 0; \ 00145 SYS_ARCH_UNPROTECT(old_level); \ 00146 } } while(0) 00147 #endif /* PBUF_POOL_FREE_OOSEQ_QUEUE_CALL */ 00148 #endif /* !NO_SYS */ 00149 00150 volatile u8_t pbuf_free_ooseq_pending; 00151 #define PBUF_POOL_IS_EMPTY() pbuf_pool_is_empty() 00152 00153 /** 00154 * Attempt to reclaim some memory from queued out-of-sequence TCP segments 00155 * if we run out of pool pbufs. It's better to give priority to new packets 00156 * if we're running out. 00157 * 00158 * This must be done in the correct thread context therefore this function 00159 * can only be used with NO_SYS=0 and through tcpip_callback. 00160 */ 00161 #if !NO_SYS 00162 static 00163 #endif /* !NO_SYS */ 00164 void 00165 pbuf_free_ooseq(void) 00166 { 00167 struct tcp_pcb* pcb; 00168 SYS_ARCH_SET(pbuf_free_ooseq_pending, 0); 00169 00170 for (pcb = tcp_active_pcbs; NULL != pcb; pcb = pcb->next) { 00171 if (NULL != pcb->ooseq) { 00172 /** Free the ooseq pbufs of one PCB only */ 00173 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free_ooseq: freeing out-of-sequence pbufs\n")); 00174 tcp_segs_free(pcb->ooseq); 00175 pcb->ooseq = NULL; 00176 return; 00177 } 00178 } 00179 } 00180 00181 #if !NO_SYS 00182 /** 00183 * Just a callback function for tcpip_callback() that calls pbuf_free_ooseq(). 00184 */ 00185 static void 00186 pbuf_free_ooseq_callback(void *arg) 00187 { 00188 LWIP_UNUSED_ARG(arg); 00189 pbuf_free_ooseq(); 00190 } 00191 #endif /* !NO_SYS */ 00192 00193 /** Queue a call to pbuf_free_ooseq if not already queued. */ 00194 static void 00195 pbuf_pool_is_empty(void) 00196 { 00197 #ifndef PBUF_POOL_FREE_OOSEQ_QUEUE_CALL 00198 SYS_ARCH_SET(pbuf_free_ooseq_pending, 1); 00199 #else /* PBUF_POOL_FREE_OOSEQ_QUEUE_CALL */ 00200 u8_t queued; 00201 SYS_ARCH_DECL_PROTECT(old_level); 00202 SYS_ARCH_PROTECT(old_level); 00203 queued = pbuf_free_ooseq_pending; 00204 pbuf_free_ooseq_pending = 1; 00205 SYS_ARCH_UNPROTECT(old_level); 00206 00207 if (!queued) { 00208 /* queue a call to pbuf_free_ooseq if not already queued */ 00209 PBUF_POOL_FREE_OOSEQ_QUEUE_CALL(); 00210 } 00211 #endif /* PBUF_POOL_FREE_OOSEQ_QUEUE_CALL */ 00212 } 00213 #endif /* !LWIP_TCP || !TCP_QUEUE_OOSEQ || !PBUF_POOL_FREE_OOSEQ */ 00214 00215 /** 00216 * @ingroup pbuf 00217 * Allocates a pbuf of the given type (possibly a chain for PBUF_POOL type). 00218 * 00219 * The actual memory allocated for the pbuf is determined by the 00220 * layer at which the pbuf is allocated and the requested size 00221 * (from the size parameter). 00222 * 00223 * @param layer flag to define header size 00224 * @param length size of the pbuf's payload 00225 * @param type this parameter decides how and where the pbuf 00226 * should be allocated as follows: 00227 * 00228 * - PBUF_RAM: buffer memory for pbuf is allocated as one large 00229 * chunk. This includes protocol headers as well. 00230 * - PBUF_ROM: no buffer memory is allocated for the pbuf, even for 00231 * protocol headers. Additional headers must be prepended 00232 * by allocating another pbuf and chain in to the front of 00233 * the ROM pbuf. It is assumed that the memory used is really 00234 * similar to ROM in that it is immutable and will not be 00235 * changed. Memory which is dynamic should generally not 00236 * be attached to PBUF_ROM pbufs. Use PBUF_REF instead. 00237 * - PBUF_REF: no buffer memory is allocated for the pbuf, even for 00238 * protocol headers. It is assumed that the pbuf is only 00239 * being used in a single thread. If the pbuf gets queued, 00240 * then pbuf_take should be called to copy the buffer. 00241 * - PBUF_POOL: the pbuf is allocated as a pbuf chain, with pbufs from 00242 * the pbuf pool that is allocated during pbuf_init(). 00243 * 00244 * @return the allocated pbuf. If multiple pbufs where allocated, this 00245 * is the first pbuf of a pbuf chain. 00246 */ 00247 struct pbuf * 00248 pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type) 00249 { 00250 struct pbuf *p, *q, *r; 00251 u16_t offset; 00252 s32_t rem_len; /* remaining length */ 00253 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F")\n", length)); 00254 00255 /* determine header offset */ 00256 switch (layer) { 00257 case PBUF_TRANSPORT: 00258 /* add room for transport (often TCP) layer header */ 00259 offset = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN; 00260 break; 00261 case PBUF_IP: 00262 /* add room for IP layer header */ 00263 offset = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN; 00264 break; 00265 case PBUF_LINK: 00266 /* add room for link layer header */ 00267 offset = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN; 00268 break; 00269 case PBUF_RAW_TX: 00270 /* add room for encapsulating link layer headers (e.g. 802.11) */ 00271 offset = PBUF_LINK_ENCAPSULATION_HLEN; 00272 break; 00273 case PBUF_RAW: 00274 /* no offset (e.g. RX buffers or chain successors) */ 00275 offset = 0; 00276 break; 00277 default: 00278 LWIP_ASSERT("pbuf_alloc: bad pbuf layer", 0); 00279 return NULL; 00280 } 00281 00282 switch (type) { 00283 case PBUF_POOL: 00284 /* allocate head of pbuf chain into p */ 00285 p = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL); 00286 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc: allocated pbuf %p\n", (void *)p)); 00287 if (p == NULL) { 00288 PBUF_POOL_IS_EMPTY(); 00289 return NULL; 00290 } 00291 p->type = type; 00292 p->next = NULL; 00293 00294 /* make the payload pointer point 'offset' bytes into pbuf data memory */ 00295 p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + (SIZEOF_STRUCT_PBUF + offset))); 00296 LWIP_ASSERT("pbuf_alloc: pbuf p->payload properly aligned", 00297 ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0); 00298 /* the total length of the pbuf chain is the requested size */ 00299 p->tot_len = length; 00300 /* set the length of the first pbuf in the chain */ 00301 p->len = LWIP_MIN(length, PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)); 00302 LWIP_ASSERT("check p->payload + p->len does not overflow pbuf", 00303 ((u8_t*)p->payload + p->len <= 00304 (u8_t*)p + SIZEOF_STRUCT_PBUF + PBUF_POOL_BUFSIZE_ALIGNED)); 00305 LWIP_ASSERT("PBUF_POOL_BUFSIZE must be bigger than MEM_ALIGNMENT", 00306 (PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)) > 0 ); 00307 /* set reference count (needed here in case we fail) */ 00308 p->ref = 1; 00309 00310 /* now allocate the tail of the pbuf chain */ 00311 00312 /* remember first pbuf for linkage in next iteration */ 00313 r = p; 00314 /* remaining length to be allocated */ 00315 rem_len = length - p->len; 00316 /* any remaining pbufs to be allocated? */ 00317 while (rem_len > 0) { 00318 q = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL); 00319 if (q == NULL) { 00320 PBUF_POOL_IS_EMPTY(); 00321 /* free chain so far allocated */ 00322 pbuf_free(p); 00323 /* bail out unsuccessfully */ 00324 return NULL; 00325 } 00326 q->type = type; 00327 q->flags = 0; 00328 q->next = NULL; 00329 /* make previous pbuf point to this pbuf */ 00330 r->next = q; 00331 /* set total length of this pbuf and next in chain */ 00332 LWIP_ASSERT("rem_len < max_u16_t", rem_len < 0xffff); 00333 q->tot_len = (u16_t)rem_len; 00334 /* this pbuf length is pool size, unless smaller sized tail */ 00335 q->len = LWIP_MIN((u16_t)rem_len, PBUF_POOL_BUFSIZE_ALIGNED); 00336 q->payload = (void *)((u8_t *)q + SIZEOF_STRUCT_PBUF); 00337 LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned", 00338 ((mem_ptr_t)q->payload % MEM_ALIGNMENT) == 0); 00339 LWIP_ASSERT("check p->payload + p->len does not overflow pbuf", 00340 ((u8_t*)p->payload + p->len <= 00341 (u8_t*)p + SIZEOF_STRUCT_PBUF + PBUF_POOL_BUFSIZE_ALIGNED)); 00342 q->ref = 1; 00343 /* calculate remaining length to be allocated */ 00344 rem_len -= q->len; 00345 /* remember this pbuf for linkage in next iteration */ 00346 r = q; 00347 } 00348 /* end of chain */ 00349 /*r->next = NULL;*/ 00350 00351 break; 00352 case PBUF_RAM: 00353 { 00354 mem_size_t alloc_len = LWIP_MEM_ALIGN_SIZE(SIZEOF_STRUCT_PBUF + offset) + LWIP_MEM_ALIGN_SIZE(length); 00355 00356 /* bug #50040: Check for integer overflow when calculating alloc_len */ 00357 if (alloc_len < LWIP_MEM_ALIGN_SIZE(length)) { 00358 return NULL; 00359 } 00360 00361 /* If pbuf is to be allocated in RAM, allocate memory for it. */ 00362 p = (struct pbuf*)mem_malloc(alloc_len); 00363 } 00364 00365 if (p == NULL) { 00366 return NULL; 00367 } 00368 /* Set up internal structure of the pbuf. */ 00369 p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + SIZEOF_STRUCT_PBUF + offset)); 00370 p->len = p->tot_len = length; 00371 p->next = NULL; 00372 p->type = type; 00373 00374 LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned", 00375 ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0); 00376 break; 00377 /* pbuf references existing (non-volatile static constant) ROM payload? */ 00378 case PBUF_ROM: 00379 /* pbuf references existing (externally allocated) RAM payload? */ 00380 case PBUF_REF: 00381 /* only allocate memory for the pbuf structure */ 00382 p = (struct pbuf *)memp_malloc(MEMP_PBUF); 00383 if (p == NULL) { 00384 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS, 00385 ("pbuf_alloc: Could not allocate MEMP_PBUF for PBUF_%s.\n", 00386 (type == PBUF_ROM) ? "ROM" : "REF")); 00387 return NULL; 00388 } 00389 /* caller must set this field properly, afterwards */ 00390 p->payload = NULL; 00391 p->len = p->tot_len = length; 00392 p->next = NULL; 00393 p->type = type; 00394 break; 00395 default: 00396 LWIP_ASSERT("pbuf_alloc: erroneous type", 0); 00397 return NULL; 00398 } 00399 /* set reference count */ 00400 p->ref = 1; 00401 /* set flags */ 00402 p->flags = 0; 00403 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F") == %p\n", length, (void *)p)); 00404 return p; 00405 } 00406 00407 #if LWIP_SUPPORT_CUSTOM_PBUF 00408 /** 00409 * @ingroup pbuf 00410 * Initialize a custom pbuf (already allocated). 00411 * 00412 * @param l flag to define header size 00413 * @param length size of the pbuf's payload 00414 * @param type type of the pbuf (only used to treat the pbuf accordingly, as 00415 * this function allocates no memory) 00416 * @param p pointer to the custom pbuf to initialize (already allocated) 00417 * @param payload_mem pointer to the buffer that is used for payload and headers, 00418 * must be at least big enough to hold 'length' plus the header size, 00419 * may be NULL if set later. 00420 * ATTENTION: The caller is responsible for correct alignment of this buffer!! 00421 * @param payload_mem_len the size of the 'payload_mem' buffer, must be at least 00422 * big enough to hold 'length' plus the header size 00423 */ 00424 struct pbuf* 00425 pbuf_alloced_custom(pbuf_layer l, u16_t length, pbuf_type type, struct pbuf_custom *p, 00426 void *payload_mem, u16_t payload_mem_len) 00427 { 00428 u16_t offset; 00429 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloced_custom(length=%"U16_F")\n", length)); 00430 00431 /* determine header offset */ 00432 switch (l) { 00433 case PBUF_TRANSPORT: 00434 /* add room for transport (often TCP) layer header */ 00435 offset = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN; 00436 break; 00437 case PBUF_IP: 00438 /* add room for IP layer header */ 00439 offset = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN; 00440 break; 00441 case PBUF_LINK: 00442 /* add room for link layer header */ 00443 offset = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN; 00444 break; 00445 case PBUF_RAW_TX: 00446 /* add room for encapsulating link layer headers (e.g. 802.11) */ 00447 offset = PBUF_LINK_ENCAPSULATION_HLEN; 00448 break; 00449 case PBUF_RAW: 00450 offset = 0; 00451 break; 00452 default: 00453 LWIP_ASSERT("pbuf_alloced_custom: bad pbuf layer", 0); 00454 return NULL; 00455 } 00456 00457 if (LWIP_MEM_ALIGN_SIZE(offset) + length > payload_mem_len) { 00458 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_WARNING, ("pbuf_alloced_custom(length=%"U16_F") buffer too short\n", length)); 00459 return NULL; 00460 } 00461 00462 p->pbuf.next = NULL; 00463 if (payload_mem != NULL) { 00464 p->pbuf.payload = (u8_t *)payload_mem + LWIP_MEM_ALIGN_SIZE(offset); 00465 } else { 00466 p->pbuf.payload = NULL; 00467 } 00468 p->pbuf.flags = PBUF_FLAG_IS_CUSTOM; 00469 p->pbuf.len = p->pbuf.tot_len = length; 00470 p->pbuf.type = type; 00471 p->pbuf.ref = 1; 00472 return &p->pbuf; 00473 } 00474 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */ 00475 00476 /** 00477 * @ingroup pbuf 00478 * Shrink a pbuf chain to a desired length. 00479 * 00480 * @param p pbuf to shrink. 00481 * @param new_len desired new length of pbuf chain 00482 * 00483 * Depending on the desired length, the first few pbufs in a chain might 00484 * be skipped and left unchanged. The new last pbuf in the chain will be 00485 * resized, and any remaining pbufs will be freed. 00486 * 00487 * @note If the pbuf is ROM/REF, only the ->tot_len and ->len fields are adjusted. 00488 * @note May not be called on a packet queue. 00489 * 00490 * @note Despite its name, pbuf_realloc cannot grow the size of a pbuf (chain). 00491 */ 00492 void 00493 pbuf_realloc(struct pbuf *p, u16_t new_len) 00494 { 00495 struct pbuf *q; 00496 u16_t rem_len; /* remaining length */ 00497 s32_t grow; 00498 00499 LWIP_ASSERT("pbuf_realloc: p != NULL", p != NULL); 00500 LWIP_ASSERT("pbuf_realloc: sane p->type", p->type == PBUF_POOL || 00501 p->type == PBUF_ROM || 00502 p->type == PBUF_RAM || 00503 p->type == PBUF_REF); 00504 00505 /* desired length larger than current length? */ 00506 if (new_len >= p->tot_len) { 00507 /* enlarging not yet supported */ 00508 return; 00509 } 00510 00511 /* the pbuf chain grows by (new_len - p->tot_len) bytes 00512 * (which may be negative in case of shrinking) */ 00513 grow = new_len - p->tot_len; 00514 00515 /* first, step over any pbufs that should remain in the chain */ 00516 rem_len = new_len; 00517 q = p; 00518 /* should this pbuf be kept? */ 00519 while (rem_len > q->len) { 00520 /* decrease remaining length by pbuf length */ 00521 rem_len -= q->len; 00522 /* decrease total length indicator */ 00523 LWIP_ASSERT("grow < max_u16_t", grow < 0xffff); 00524 q->tot_len += (u16_t)grow; 00525 /* proceed to next pbuf in chain */ 00526 q = q->next; 00527 LWIP_ASSERT("pbuf_realloc: q != NULL", q != NULL); 00528 } 00529 /* we have now reached the new last pbuf (in q) */ 00530 /* rem_len == desired length for pbuf q */ 00531 00532 /* shrink allocated memory for PBUF_RAM */ 00533 /* (other types merely adjust their length fields */ 00534 if ((q->type == PBUF_RAM) && (rem_len != q->len) 00535 #if LWIP_SUPPORT_CUSTOM_PBUF 00536 && ((q->flags & PBUF_FLAG_IS_CUSTOM) == 0) 00537 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */ 00538 ) { 00539 /* reallocate and adjust the length of the pbuf that will be split */ 00540 q = (struct pbuf *)mem_trim(q, (u16_t)((u8_t *)q->payload - (u8_t *)q) + rem_len); 00541 LWIP_ASSERT("mem_trim returned q == NULL", q != NULL); 00542 } 00543 /* adjust length fields for new last pbuf */ 00544 q->len = rem_len; 00545 q->tot_len = q->len; 00546 00547 /* any remaining pbufs in chain? */ 00548 if (q->next != NULL) { 00549 /* free remaining pbufs in chain */ 00550 pbuf_free(q->next); 00551 } 00552 /* q is last packet in chain */ 00553 q->next = NULL; 00554 00555 } 00556 00557 /** 00558 * Adjusts the payload pointer to hide or reveal headers in the payload. 00559 * @see pbuf_header. 00560 * 00561 * @param p pbuf to change the header size. 00562 * @param header_size_increment Number of bytes to increment header size. 00563 * @param force Allow 'header_size_increment > 0' for PBUF_REF/PBUF_ROM types 00564 * 00565 * @return non-zero on failure, zero on success. 00566 * 00567 */ 00568 static u8_t 00569 pbuf_header_impl(struct pbuf *p, s16_t header_size_increment, u8_t force) 00570 { 00571 u16_t type; 00572 void *payload; 00573 u16_t increment_magnitude; 00574 00575 LWIP_ASSERT("p != NULL", p != NULL); 00576 if ((header_size_increment == 0) || (p == NULL)) { 00577 return 0; 00578 } 00579 00580 if (header_size_increment < 0) { 00581 increment_magnitude = (u16_t)-header_size_increment; 00582 /* Check that we aren't going to move off the end of the pbuf */ 00583 LWIP_ERROR("increment_magnitude <= p->len", (increment_magnitude <= p->len), return 1;); 00584 } else { 00585 increment_magnitude = (u16_t)header_size_increment; 00586 #if 0 00587 /* Can't assert these as some callers speculatively call 00588 pbuf_header() to see if it's OK. Will return 1 below instead. */ 00589 /* Check that we've got the correct type of pbuf to work with */ 00590 LWIP_ASSERT("p->type == PBUF_RAM || p->type == PBUF_POOL", 00591 p->type == PBUF_RAM || p->type == PBUF_POOL); 00592 /* Check that we aren't going to move off the beginning of the pbuf */ 00593 LWIP_ASSERT("p->payload - increment_magnitude >= p + SIZEOF_STRUCT_PBUF", 00594 (u8_t *)p->payload - increment_magnitude >= (u8_t *)p + SIZEOF_STRUCT_PBUF); 00595 #endif 00596 } 00597 00598 type = p->type; 00599 /* remember current payload pointer */ 00600 payload = p->payload; 00601 00602 /* pbuf types containing payloads? */ 00603 if (type == PBUF_RAM || type == PBUF_POOL) { 00604 /* set new payload pointer */ 00605 p->payload = (u8_t *)p->payload - header_size_increment; 00606 /* boundary check fails? */ 00607 if ((u8_t *)p->payload < (u8_t *)p + SIZEOF_STRUCT_PBUF) { 00608 LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, 00609 ("pbuf_header: failed as %p < %p (not enough space for new header size)\n", 00610 (void *)p->payload, (void *)((u8_t *)p + SIZEOF_STRUCT_PBUF))); 00611 /* restore old payload pointer */ 00612 p->payload = payload; 00613 /* bail out unsuccessfully */ 00614 return 1; 00615 } 00616 /* pbuf types referring to external payloads? */ 00617 } else if (type == PBUF_REF || type == PBUF_ROM) { 00618 /* hide a header in the payload? */ 00619 if ((header_size_increment < 0) && (increment_magnitude <= p->len)) { 00620 /* increase payload pointer */ 00621 p->payload = (u8_t *)p->payload - header_size_increment; 00622 } else if ((header_size_increment > 0) && force) { 00623 p->payload = (u8_t *)p->payload - header_size_increment; 00624 } else { 00625 /* cannot expand payload to front (yet!) 00626 * bail out unsuccessfully */ 00627 return 1; 00628 } 00629 } else { 00630 /* Unknown type */ 00631 LWIP_ASSERT("bad pbuf type", 0); 00632 return 1; 00633 } 00634 /* modify pbuf length fields */ 00635 p->len += header_size_increment; 00636 p->tot_len += header_size_increment; 00637 00638 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_header: old %p new %p (%"S16_F")\n", 00639 (void *)payload, (void *)p->payload, header_size_increment)); 00640 00641 return 0; 00642 } 00643 00644 /** 00645 * Adjusts the payload pointer to hide or reveal headers in the payload. 00646 * 00647 * Adjusts the ->payload pointer so that space for a header 00648 * (dis)appears in the pbuf payload. 00649 * 00650 * The ->payload, ->tot_len and ->len fields are adjusted. 00651 * 00652 * @param p pbuf to change the header size. 00653 * @param header_size_increment Number of bytes to increment header size which 00654 * increases the size of the pbuf. New space is on the front. 00655 * (Using a negative value decreases the header size.) 00656 * If hdr_size_inc is 0, this function does nothing and returns successful. 00657 * 00658 * PBUF_ROM and PBUF_REF type buffers cannot have their sizes increased, so 00659 * the call will fail. A check is made that the increase in header size does 00660 * not move the payload pointer in front of the start of the buffer. 00661 * @return non-zero on failure, zero on success. 00662 * 00663 */ 00664 u8_t 00665 pbuf_header(struct pbuf *p, s16_t header_size_increment) 00666 { 00667 return pbuf_header_impl(p, header_size_increment, 0); 00668 } 00669 00670 /** 00671 * Same as pbuf_header but does not check if 'header_size > 0' is allowed. 00672 * This is used internally only, to allow PBUF_REF for RX. 00673 */ 00674 u8_t 00675 pbuf_header_force(struct pbuf *p, s16_t header_size_increment) 00676 { 00677 return pbuf_header_impl(p, header_size_increment, 1); 00678 } 00679 00680 /** 00681 * @ingroup pbuf 00682 * Dereference a pbuf chain or queue and deallocate any no-longer-used 00683 * pbufs at the head of this chain or queue. 00684 * 00685 * Decrements the pbuf reference count. If it reaches zero, the pbuf is 00686 * deallocated. 00687 * 00688 * For a pbuf chain, this is repeated for each pbuf in the chain, 00689 * up to the first pbuf which has a non-zero reference count after 00690 * decrementing. So, when all reference counts are one, the whole 00691 * chain is free'd. 00692 * 00693 * @param p The pbuf (chain) to be dereferenced. 00694 * 00695 * @return the number of pbufs that were de-allocated 00696 * from the head of the chain. 00697 * 00698 * @note MUST NOT be called on a packet queue (Not verified to work yet). 00699 * @note the reference counter of a pbuf equals the number of pointers 00700 * that refer to the pbuf (or into the pbuf). 00701 * 00702 * @internal examples: 00703 * 00704 * Assuming existing chains a->b->c with the following reference 00705 * counts, calling pbuf_free(a) results in: 00706 * 00707 * 1->2->3 becomes ...1->3 00708 * 3->3->3 becomes 2->3->3 00709 * 1->1->2 becomes ......1 00710 * 2->1->1 becomes 1->1->1 00711 * 1->1->1 becomes ....... 00712 * 00713 */ 00714 u8_t 00715 pbuf_free(struct pbuf *p) 00716 { 00717 u16_t type; 00718 struct pbuf *q; 00719 u8_t count; 00720 00721 if (p == NULL) { 00722 LWIP_ASSERT("p != NULL", p != NULL); 00723 /* if assertions are disabled, proceed with debug output */ 00724 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS, 00725 ("pbuf_free(p == NULL) was called.\n")); 00726 return 0; 00727 } 00728 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free(%p)\n", (void *)p)); 00729 00730 PERF_START; 00731 00732 LWIP_ASSERT("pbuf_free: sane type", 00733 p->type == PBUF_RAM || p->type == PBUF_ROM || 00734 p->type == PBUF_REF || p->type == PBUF_POOL); 00735 00736 count = 0; 00737 /* de-allocate all consecutive pbufs from the head of the chain that 00738 * obtain a zero reference count after decrementing*/ 00739 while (p != NULL) { 00740 u16_t ref; 00741 SYS_ARCH_DECL_PROTECT(old_level); 00742 /* Since decrementing ref cannot be guaranteed to be a single machine operation 00743 * we must protect it. We put the new ref into a local variable to prevent 00744 * further protection. */ 00745 SYS_ARCH_PROTECT(old_level); 00746 /* all pbufs in a chain are referenced at least once */ 00747 LWIP_ASSERT("pbuf_free: p->ref > 0", p->ref > 0); 00748 /* decrease reference count (number of pointers to pbuf) */ 00749 ref = --(p->ref); 00750 SYS_ARCH_UNPROTECT(old_level); 00751 /* this pbuf is no longer referenced to? */ 00752 if (ref == 0) { 00753 /* remember next pbuf in chain for next iteration */ 00754 q = p->next; 00755 LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: deallocating %p\n", (void *)p)); 00756 type = p->type; 00757 #if LWIP_SUPPORT_CUSTOM_PBUF 00758 /* is this a custom pbuf? */ 00759 if ((p->flags & PBUF_FLAG_IS_CUSTOM) != 0) { 00760 struct pbuf_custom *pc = (struct pbuf_custom*)p; 00761 LWIP_ASSERT("pc->custom_free_function != NULL", pc->custom_free_function != NULL); 00762 pc->custom_free_function(p); 00763 } else 00764 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */ 00765 { 00766 /* is this a pbuf from the pool? */ 00767 if (type == PBUF_POOL) { 00768 memp_free(MEMP_PBUF_POOL, p); 00769 /* is this a ROM or RAM referencing pbuf? */ 00770 } else if (type == PBUF_ROM || type == PBUF_REF) { 00771 memp_free(MEMP_PBUF, p); 00772 /* type == PBUF_RAM */ 00773 } else { 00774 mem_free(p); 00775 } 00776 } 00777 count++; 00778 /* proceed to next pbuf */ 00779 p = q; 00780 /* p->ref > 0, this pbuf is still referenced to */ 00781 /* (and so the remaining pbufs in chain as well) */ 00782 } else { 00783 LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: %p has ref %"U16_F", ending here.\n", (void *)p, ref)); 00784 /* stop walking through the chain */ 00785 p = NULL; 00786 } 00787 } 00788 PERF_STOP("pbuf_free"); 00789 /* return number of de-allocated pbufs */ 00790 return count; 00791 } 00792 00793 /** 00794 * Count number of pbufs in a chain 00795 * 00796 * @param p first pbuf of chain 00797 * @return the number of pbufs in a chain 00798 */ 00799 u16_t 00800 pbuf_clen(const struct pbuf *p) 00801 { 00802 u16_t len; 00803 00804 len = 0; 00805 while (p != NULL) { 00806 ++len; 00807 p = p->next; 00808 } 00809 return len; 00810 } 00811 00812 /** 00813 * @ingroup pbuf 00814 * Increment the reference count of the pbuf. 00815 * 00816 * @param p pbuf to increase reference counter of 00817 * 00818 */ 00819 void 00820 pbuf_ref(struct pbuf *p) 00821 { 00822 /* pbuf given? */ 00823 if (p != NULL) { 00824 SYS_ARCH_INC(p->ref, 1); 00825 LWIP_ASSERT("pbuf ref overflow", p->ref > 0); 00826 } 00827 } 00828 00829 /** 00830 * @ingroup pbuf 00831 * Concatenate two pbufs (each may be a pbuf chain) and take over 00832 * the caller's reference of the tail pbuf. 00833 * 00834 * @note The caller MAY NOT reference the tail pbuf afterwards. 00835 * Use pbuf_chain() for that purpose. 00836 * 00837 * @see pbuf_chain() 00838 */ 00839 void 00840 pbuf_cat(struct pbuf *h, struct pbuf *t) 00841 { 00842 struct pbuf *p; 00843 00844 LWIP_ERROR("(h != NULL) && (t != NULL) (programmer violates API)", 00845 ((h != NULL) && (t != NULL)), return;); 00846 00847 /* proceed to last pbuf of chain */ 00848 for (p = h; p->next != NULL; p = p->next) { 00849 /* add total length of second chain to all totals of first chain */ 00850 p->tot_len += t->tot_len; 00851 } 00852 /* { p is last pbuf of first h chain, p->next == NULL } */ 00853 LWIP_ASSERT("p->tot_len == p->len (of last pbuf in chain)", p->tot_len == p->len); 00854 LWIP_ASSERT("p->next == NULL", p->next == NULL); 00855 /* add total length of second chain to last pbuf total of first chain */ 00856 p->tot_len += t->tot_len; 00857 /* chain last pbuf of head (p) with first of tail (t) */ 00858 p->next = t; 00859 /* p->next now references t, but the caller will drop its reference to t, 00860 * so netto there is no change to the reference count of t. 00861 */ 00862 } 00863 00864 /** 00865 * @ingroup pbuf 00866 * Chain two pbufs (or pbuf chains) together. 00867 * 00868 * The caller MUST call pbuf_free(t) once it has stopped 00869 * using it. Use pbuf_cat() instead if you no longer use t. 00870 * 00871 * @param h head pbuf (chain) 00872 * @param t tail pbuf (chain) 00873 * @note The pbufs MUST belong to the same packet. 00874 * @note MAY NOT be called on a packet queue. 00875 * 00876 * The ->tot_len fields of all pbufs of the head chain are adjusted. 00877 * The ->next field of the last pbuf of the head chain is adjusted. 00878 * The ->ref field of the first pbuf of the tail chain is adjusted. 00879 * 00880 */ 00881 void 00882 pbuf_chain(struct pbuf *h, struct pbuf *t) 00883 { 00884 pbuf_cat(h, t); 00885 /* t is now referenced by h */ 00886 pbuf_ref(t); 00887 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_chain: %p references %p\n", (void *)h, (void *)t)); 00888 } 00889 00890 /** 00891 * Dechains the first pbuf from its succeeding pbufs in the chain. 00892 * 00893 * Makes p->tot_len field equal to p->len. 00894 * @param p pbuf to dechain 00895 * @return remainder of the pbuf chain, or NULL if it was de-allocated. 00896 * @note May not be called on a packet queue. 00897 */ 00898 struct pbuf * 00899 pbuf_dechain(struct pbuf *p) 00900 { 00901 struct pbuf *q; 00902 u8_t tail_gone = 1; 00903 /* tail */ 00904 q = p->next; 00905 /* pbuf has successor in chain? */ 00906 if (q != NULL) { 00907 /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */ 00908 LWIP_ASSERT("p->tot_len == p->len + q->tot_len", q->tot_len == p->tot_len - p->len); 00909 /* enforce invariant if assertion is disabled */ 00910 q->tot_len = p->tot_len - p->len; 00911 /* decouple pbuf from remainder */ 00912 p->next = NULL; 00913 /* total length of pbuf p is its own length only */ 00914 p->tot_len = p->len; 00915 /* q is no longer referenced by p, free it */ 00916 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_dechain: unreferencing %p\n", (void *)q)); 00917 tail_gone = pbuf_free(q); 00918 if (tail_gone > 0) { 00919 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, 00920 ("pbuf_dechain: deallocated %p (as it is no longer referenced)\n", (void *)q)); 00921 } 00922 /* return remaining tail or NULL if deallocated */ 00923 } 00924 /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */ 00925 LWIP_ASSERT("p->tot_len == p->len", p->tot_len == p->len); 00926 return ((tail_gone > 0) ? NULL : q); 00927 } 00928 00929 /** 00930 * @ingroup pbuf 00931 * Create PBUF_RAM copies of pbufs. 00932 * 00933 * Used to queue packets on behalf of the lwIP stack, such as 00934 * ARP based queueing. 00935 * 00936 * @note You MUST explicitly use p = pbuf_take(p); 00937 * 00938 * @note Only one packet is copied, no packet queue! 00939 * 00940 * @param p_to pbuf destination of the copy 00941 * @param p_from pbuf source of the copy 00942 * 00943 * @return ERR_OK if pbuf was copied 00944 * ERR_ARG if one of the pbufs is NULL or p_to is not big 00945 * enough to hold p_from 00946 */ 00947 err_t 00948 pbuf_copy(struct pbuf *p_to, const struct pbuf *p_from) 00949 { 00950 u16_t offset_to=0, offset_from=0, len; 00951 00952 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy(%p, %p)\n", 00953 (const void*)p_to, (const void*)p_from)); 00954 00955 /* is the target big enough to hold the source? */ 00956 LWIP_ERROR("pbuf_copy: target not big enough to hold source", ((p_to != NULL) && 00957 (p_from != NULL) && (p_to->tot_len >= p_from->tot_len)), return ERR_ARG;); 00958 00959 /* iterate through pbuf chain */ 00960 do 00961 { 00962 /* copy one part of the original chain */ 00963 if ((p_to->len - offset_to) >= (p_from->len - offset_from)) { 00964 /* complete current p_from fits into current p_to */ 00965 len = p_from->len - offset_from; 00966 } else { 00967 /* current p_from does not fit into current p_to */ 00968 len = p_to->len - offset_to; 00969 } 00970 MEMCPY((u8_t*)p_to->payload + offset_to, (u8_t*)p_from->payload + offset_from, len); 00971 offset_to += len; 00972 offset_from += len; 00973 LWIP_ASSERT("offset_to <= p_to->len", offset_to <= p_to->len); 00974 LWIP_ASSERT("offset_from <= p_from->len", offset_from <= p_from->len); 00975 if (offset_from >= p_from->len) { 00976 /* on to next p_from (if any) */ 00977 offset_from = 0; 00978 p_from = p_from->next; 00979 } 00980 if (offset_to == p_to->len) { 00981 /* on to next p_to (if any) */ 00982 offset_to = 0; 00983 p_to = p_to->next; 00984 LWIP_ERROR("p_to != NULL", (p_to != NULL) || (p_from == NULL) , return ERR_ARG;); 00985 } 00986 00987 if ((p_from != NULL) && (p_from->len == p_from->tot_len)) { 00988 /* don't copy more than one packet! */ 00989 LWIP_ERROR("pbuf_copy() does not allow packet queues!", 00990 (p_from->next == NULL), return ERR_VAL;); 00991 } 00992 if ((p_to != NULL) && (p_to->len == p_to->tot_len)) { 00993 /* don't copy more than one packet! */ 00994 LWIP_ERROR("pbuf_copy() does not allow packet queues!", 00995 (p_to->next == NULL), return ERR_VAL;); 00996 } 00997 } while (p_from); 00998 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy: end of chain reached.\n")); 00999 return ERR_OK; 01000 } 01001 01002 /** 01003 * @ingroup pbuf 01004 * Copy (part of) the contents of a packet buffer 01005 * to an application supplied buffer. 01006 * 01007 * @param buf the pbuf from which to copy data 01008 * @param dataptr the application supplied buffer 01009 * @param len length of data to copy (dataptr must be big enough). No more 01010 * than buf->tot_len will be copied, irrespective of len 01011 * @param offset offset into the packet buffer from where to begin copying len bytes 01012 * @return the number of bytes copied, or 0 on failure 01013 */ 01014 u16_t 01015 pbuf_copy_partial(const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset) 01016 { 01017 const struct pbuf *p; 01018 u16_t left; 01019 u16_t buf_copy_len; 01020 u16_t copied_total = 0; 01021 01022 LWIP_ERROR("pbuf_copy_partial: invalid buf", (buf != NULL), return 0;); 01023 LWIP_ERROR("pbuf_copy_partial: invalid dataptr", (dataptr != NULL), return 0;); 01024 01025 left = 0; 01026 01027 if ((buf == NULL) || (dataptr == NULL)) { 01028 return 0; 01029 } 01030 01031 /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */ 01032 for (p = buf; len != 0 && p != NULL; p = p->next) { 01033 if ((offset != 0) && (offset >= p->len)) { 01034 /* don't copy from this buffer -> on to the next */ 01035 offset -= p->len; 01036 } else { 01037 /* copy from this buffer. maybe only partially. */ 01038 buf_copy_len = p->len - offset; 01039 if (buf_copy_len > len) { 01040 buf_copy_len = len; 01041 } 01042 /* copy the necessary parts of the buffer */ 01043 MEMCPY(&((char*)dataptr)[left], &((char*)p->payload)[offset], buf_copy_len); 01044 copied_total += buf_copy_len; 01045 left += buf_copy_len; 01046 len -= buf_copy_len; 01047 offset = 0; 01048 } 01049 } 01050 return copied_total; 01051 } 01052 01053 #if LWIP_TCP && TCP_QUEUE_OOSEQ && LWIP_WND_SCALE 01054 /** 01055 * This method modifies a 'pbuf chain', so that its total length is 01056 * smaller than 64K. The remainder of the original pbuf chain is stored 01057 * in *rest. 01058 * This function never creates new pbufs, but splits an existing chain 01059 * in two parts. The tot_len of the modified packet queue will likely be 01060 * smaller than 64K. 01061 * 'packet queues' are not supported by this function. 01062 * 01063 * @param p the pbuf queue to be split 01064 * @param rest pointer to store the remainder (after the first 64K) 01065 */ 01066 void pbuf_split_64k(struct pbuf *p, struct pbuf **rest) 01067 { 01068 *rest = NULL; 01069 if ((p != NULL) && (p->next != NULL)) { 01070 u16_t tot_len_front = p->len; 01071 struct pbuf *i = p; 01072 struct pbuf *r = p->next; 01073 01074 /* continue until the total length (summed up as u16_t) overflows */ 01075 while ((r != NULL) && ((u16_t)(tot_len_front + r->len) > tot_len_front)) { 01076 tot_len_front += r->len; 01077 i = r; 01078 r = r->next; 01079 } 01080 /* i now points to last packet of the first segment. Set next 01081 pointer to NULL */ 01082 i->next = NULL; 01083 01084 if (r != NULL) { 01085 /* Update the tot_len field in the first part */ 01086 for (i = p; i != NULL; i = i->next) { 01087 i->tot_len -= r->tot_len; 01088 LWIP_ASSERT("tot_len/len mismatch in last pbuf", 01089 (i->next != NULL) || (i->tot_len == i->len)); 01090 } 01091 if (p->flags & PBUF_FLAG_TCP_FIN) { 01092 r->flags |= PBUF_FLAG_TCP_FIN; 01093 } 01094 01095 /* tot_len field in rest does not need modifications */ 01096 /* reference counters do not need modifications */ 01097 *rest = r; 01098 } 01099 } 01100 } 01101 #endif /* LWIP_TCP && TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */ 01102 01103 /* Actual implementation of pbuf_skip() but returning const pointer... */ 01104 static const struct pbuf* 01105 pbuf_skip_const(const struct pbuf* in, u16_t in_offset, u16_t* out_offset) 01106 { 01107 u16_t offset_left = in_offset; 01108 const struct pbuf* q = in; 01109 01110 /* get the correct pbuf */ 01111 while ((q != NULL) && (q->len <= offset_left)) { 01112 offset_left -= q->len; 01113 q = q->next; 01114 } 01115 if (out_offset != NULL) { 01116 *out_offset = offset_left; 01117 } 01118 return q; 01119 } 01120 01121 /** 01122 * @ingroup pbuf 01123 * Skip a number of bytes at the start of a pbuf 01124 * 01125 * @param in input pbuf 01126 * @param in_offset offset to skip 01127 * @param out_offset resulting offset in the returned pbuf 01128 * @return the pbuf in the queue where the offset is 01129 */ 01130 struct pbuf* 01131 pbuf_skip(struct pbuf* in, u16_t in_offset, u16_t* out_offset) 01132 { 01133 const struct pbuf* out = pbuf_skip_const(in, in_offset, out_offset); 01134 return LWIP_CONST_CAST(struct pbuf*, out); 01135 } 01136 01137 /** 01138 * @ingroup pbuf 01139 * Copy application supplied data into a pbuf. 01140 * This function can only be used to copy the equivalent of buf->tot_len data. 01141 * 01142 * @param buf pbuf to fill with data 01143 * @param dataptr application supplied data buffer 01144 * @param len length of the application supplied data buffer 01145 * 01146 * @return ERR_OK if successful, ERR_MEM if the pbuf is not big enough 01147 */ 01148 err_t 01149 pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len) 01150 { 01151 struct pbuf *p; 01152 u16_t buf_copy_len; 01153 u16_t total_copy_len = len; 01154 u16_t copied_total = 0; 01155 01156 LWIP_ERROR("pbuf_take: invalid buf", (buf != NULL), return ERR_ARG;); 01157 LWIP_ERROR("pbuf_take: invalid dataptr", (dataptr != NULL), return ERR_ARG;); 01158 LWIP_ERROR("pbuf_take: buf not large enough", (buf->tot_len >= len), return ERR_MEM;); 01159 01160 if ((buf == NULL) || (dataptr == NULL) || (buf->tot_len < len)) { 01161 return ERR_ARG; 01162 } 01163 01164 /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */ 01165 for (p = buf; total_copy_len != 0; p = p->next) { 01166 LWIP_ASSERT("pbuf_take: invalid pbuf", p != NULL); 01167 buf_copy_len = total_copy_len; 01168 if (buf_copy_len > p->len) { 01169 /* this pbuf cannot hold all remaining data */ 01170 buf_copy_len = p->len; 01171 } 01172 /* copy the necessary parts of the buffer */ 01173 MEMCPY(p->payload, &((const char*)dataptr)[copied_total], buf_copy_len); 01174 total_copy_len -= buf_copy_len; 01175 copied_total += buf_copy_len; 01176 } 01177 LWIP_ASSERT("did not copy all data", total_copy_len == 0 && copied_total == len); 01178 return ERR_OK; 01179 } 01180 01181 /** 01182 * @ingroup pbuf 01183 * Same as pbuf_take() but puts data at an offset 01184 * 01185 * @param buf pbuf to fill with data 01186 * @param dataptr application supplied data buffer 01187 * @param len length of the application supplied data buffer 01188 * @param offset offset in pbuf where to copy dataptr to 01189 * 01190 * @return ERR_OK if successful, ERR_MEM if the pbuf is not big enough 01191 */ 01192 err_t 01193 pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset) 01194 { 01195 u16_t target_offset; 01196 struct pbuf* q = pbuf_skip(buf, offset, &target_offset); 01197 01198 /* return requested data if pbuf is OK */ 01199 if ((q != NULL) && (q->tot_len >= target_offset + len)) { 01200 u16_t remaining_len = len; 01201 const u8_t* src_ptr = (const u8_t*)dataptr; 01202 /* copy the part that goes into the first pbuf */ 01203 u16_t first_copy_len = LWIP_MIN(q->len - target_offset, len); 01204 MEMCPY(((u8_t*)q->payload) + target_offset, dataptr, first_copy_len); 01205 remaining_len -= first_copy_len; 01206 src_ptr += first_copy_len; 01207 if (remaining_len > 0) { 01208 return pbuf_take(q->next, src_ptr, remaining_len); 01209 } 01210 return ERR_OK; 01211 } 01212 return ERR_MEM; 01213 } 01214 01215 /** 01216 * @ingroup pbuf 01217 * Creates a single pbuf out of a queue of pbufs. 01218 * 01219 * @remark: Either the source pbuf 'p' is freed by this function or the original 01220 * pbuf 'p' is returned, therefore the caller has to check the result! 01221 * 01222 * @param p the source pbuf 01223 * @param layer pbuf_layer of the new pbuf 01224 * 01225 * @return a new, single pbuf (p->next is NULL) 01226 * or the old pbuf if allocation fails 01227 */ 01228 struct pbuf* 01229 pbuf_coalesce(struct pbuf *p, pbuf_layer layer) 01230 { 01231 struct pbuf *q; 01232 err_t err; 01233 if (p->next == NULL) { 01234 return p; 01235 } 01236 q = pbuf_alloc(layer, p->tot_len, PBUF_RAM); 01237 if (q == NULL) { 01238 /* @todo: what do we do now? */ 01239 return p; 01240 } 01241 err = pbuf_copy(q, p); 01242 LWIP_UNUSED_ARG(err); /* in case of LWIP_NOASSERT */ 01243 LWIP_ASSERT("pbuf_copy failed", err == ERR_OK); 01244 pbuf_free(p); 01245 return q; 01246 } 01247 01248 #if LWIP_CHECKSUM_ON_COPY 01249 /** 01250 * Copies data into a single pbuf (*not* into a pbuf queue!) and updates 01251 * the checksum while copying 01252 * 01253 * @param p the pbuf to copy data into 01254 * @param start_offset offset of p->payload where to copy the data to 01255 * @param dataptr data to copy into the pbuf 01256 * @param len length of data to copy into the pbuf 01257 * @param chksum pointer to the checksum which is updated 01258 * @return ERR_OK if successful, another error if the data does not fit 01259 * within the (first) pbuf (no pbuf queues!) 01260 */ 01261 err_t 01262 pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr, 01263 u16_t len, u16_t *chksum) 01264 { 01265 u32_t acc; 01266 u16_t copy_chksum; 01267 char *dst_ptr; 01268 LWIP_ASSERT("p != NULL", p != NULL); 01269 LWIP_ASSERT("dataptr != NULL", dataptr != NULL); 01270 LWIP_ASSERT("chksum != NULL", chksum != NULL); 01271 LWIP_ASSERT("len != 0", len != 0); 01272 01273 if ((start_offset >= p->len) || (start_offset + len > p->len)) { 01274 return ERR_ARG; 01275 } 01276 01277 dst_ptr = ((char*)p->payload) + start_offset; 01278 copy_chksum = LWIP_CHKSUM_COPY(dst_ptr, dataptr, len); 01279 if ((start_offset & 1) != 0) { 01280 copy_chksum = SWAP_BYTES_IN_WORD(copy_chksum); 01281 } 01282 acc = *chksum; 01283 acc += copy_chksum; 01284 *chksum = FOLD_U32T(acc); 01285 return ERR_OK; 01286 } 01287 #endif /* LWIP_CHECKSUM_ON_COPY */ 01288 01289 /** 01290 * @ingroup pbuf 01291 * Get one byte from the specified position in a pbuf 01292 * WARNING: returns zero for offset >= p->tot_len 01293 * 01294 * @param p pbuf to parse 01295 * @param offset offset into p of the byte to return 01296 * @return byte at an offset into p OR ZERO IF 'offset' >= p->tot_len 01297 */ 01298 u8_t 01299 pbuf_get_at(const struct pbuf* p, u16_t offset) 01300 { 01301 int ret = pbuf_try_get_at(p, offset); 01302 if (ret >= 0) { 01303 return (u8_t)ret; 01304 } 01305 return 0; 01306 } 01307 01308 /** 01309 * @ingroup pbuf 01310 * Get one byte from the specified position in a pbuf 01311 * 01312 * @param p pbuf to parse 01313 * @param offset offset into p of the byte to return 01314 * @return byte at an offset into p [0..0xFF] OR negative if 'offset' >= p->tot_len 01315 */ 01316 int 01317 pbuf_try_get_at(const struct pbuf* p, u16_t offset) 01318 { 01319 u16_t q_idx; 01320 const struct pbuf* q = pbuf_skip_const(p, offset, &q_idx); 01321 01322 /* return requested data if pbuf is OK */ 01323 if ((q != NULL) && (q->len > q_idx)) { 01324 return ((u8_t*)q->payload)[q_idx]; 01325 } 01326 return -1; 01327 } 01328 01329 /** 01330 * @ingroup pbuf 01331 * Put one byte to the specified position in a pbuf 01332 * WARNING: silently ignores offset >= p->tot_len 01333 * 01334 * @param p pbuf to fill 01335 * @param offset offset into p of the byte to write 01336 * @param data byte to write at an offset into p 01337 */ 01338 void 01339 pbuf_put_at(struct pbuf* p, u16_t offset, u8_t data) 01340 { 01341 u16_t q_idx; 01342 struct pbuf* q = pbuf_skip(p, offset, &q_idx); 01343 01344 /* write requested data if pbuf is OK */ 01345 if ((q != NULL) && (q->len > q_idx)) { 01346 ((u8_t*)q->payload)[q_idx] = data; 01347 } 01348 } 01349 01350 /** 01351 * @ingroup pbuf 01352 * Compare pbuf contents at specified offset with memory s2, both of length n 01353 * 01354 * @param p pbuf to compare 01355 * @param offset offset into p at which to start comparing 01356 * @param s2 buffer to compare 01357 * @param n length of buffer to compare 01358 * @return zero if equal, nonzero otherwise 01359 * (0xffff if p is too short, diffoffset+1 otherwise) 01360 */ 01361 u16_t 01362 pbuf_memcmp(const struct pbuf* p, u16_t offset, const void* s2, u16_t n) 01363 { 01364 u16_t start = offset; 01365 const struct pbuf* q = p; 01366 u16_t i; 01367 01368 /* pbuf long enough to perform check? */ 01369 if(p->tot_len < (offset + n)) { 01370 return 0xffff; 01371 } 01372 01373 /* get the correct pbuf from chain. We know it succeeds because of p->tot_len check above. */ 01374 while ((q != NULL) && (q->len <= start)) { 01375 start -= q->len; 01376 q = q->next; 01377 } 01378 01379 /* return requested data if pbuf is OK */ 01380 for (i = 0; i < n; i++) { 01381 /* We know pbuf_get_at() succeeds because of p->tot_len check above. */ 01382 u8_t a = pbuf_get_at(q, start + i); 01383 u8_t b = ((const u8_t*)s2)[i]; 01384 if (a != b) { 01385 return i+1; 01386 } 01387 } 01388 return 0; 01389 } 01390 01391 /** 01392 * @ingroup pbuf 01393 * Find occurrence of mem (with length mem_len) in pbuf p, starting at offset 01394 * start_offset. 01395 * 01396 * @param p pbuf to search, maximum length is 0xFFFE since 0xFFFF is used as 01397 * return value 'not found' 01398 * @param mem search for the contents of this buffer 01399 * @param mem_len length of 'mem' 01400 * @param start_offset offset into p at which to start searching 01401 * @return 0xFFFF if substr was not found in p or the index where it was found 01402 */ 01403 u16_t 01404 pbuf_memfind(const struct pbuf* p, const void* mem, u16_t mem_len, u16_t start_offset) 01405 { 01406 u16_t i; 01407 u16_t max = p->tot_len - mem_len; 01408 if (p->tot_len >= mem_len + start_offset) { 01409 for (i = start_offset; i <= max; i++) { 01410 u16_t plus = pbuf_memcmp(p, i, mem, mem_len); 01411 if (plus == 0) { 01412 return i; 01413 } 01414 } 01415 } 01416 return 0xFFFF; 01417 } 01418 01419 /** 01420 * Find occurrence of substr with length substr_len in pbuf p, start at offset 01421 * start_offset 01422 * WARNING: in contrast to strstr(), this one does not stop at the first \0 in 01423 * the pbuf/source string! 01424 * 01425 * @param p pbuf to search, maximum length is 0xFFFE since 0xFFFF is used as 01426 * return value 'not found' 01427 * @param substr string to search for in p, maximum length is 0xFFFE 01428 * @return 0xFFFF if substr was not found in p or the index where it was found 01429 */ 01430 u16_t 01431 pbuf_strstr(const struct pbuf* p, const char* substr) 01432 { 01433 size_t substr_len; 01434 if ((substr == NULL) || (substr[0] == 0) || (p->tot_len == 0xFFFF)) { 01435 return 0xFFFF; 01436 } 01437 substr_len = strlen(substr); 01438 if (substr_len >= 0xFFFF) { 01439 return 0xFFFF; 01440 } 01441 return pbuf_memfind(p, substr, (u16_t)substr_len, 0); 01442 }
Generated on Sun Jul 17 2022 08:25:25 by 1.7.2