3 * Packet buffer management
5 * Packets are built from the pbuf data structure. It supports dynamic
6 * memory allocation for packet contents or can reference externally
7 * managed packet contents both in RAM and ROM. Quick allocation for
8 * incoming packets is provided through pools with fixed sized pbufs.
10 * A packet may span over multiple pbufs, chained as a singly linked
11 * list. This is called a "pbuf chain".
13 * Multiple packets may be queued, also using this singly linked list.
14 * This is called a "packet queue".
16 * So, a packet queue consists of one or more pbuf chains, each of
17 * which consist of one or more pbufs. CURRENTLY, PACKET QUEUES ARE
18 * NOT SUPPORTED!!! Use helper structs to queue multiple packets.
20 * The differences between a pbuf chain and a packet queue are very
23 * The last pbuf of a packet has a ->tot_len field that equals the
24 * ->len field. It can be found by traversing the list. If the last
25 * pbuf of a packet has a ->next field other than NULL, more packets
28 * Therefore, looping through a pbuf of a single packet, has an
29 * loop end condition (tot_len == p->len), NOT (next == NULL).
33 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without modification,
37 * are permitted provided that the following conditions are met:
39 * 1. Redistributions of source code must retain the above copyright notice,
40 * this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright notice,
42 * this list of conditions and the following disclaimer in the documentation
43 * and/or other materials provided with the distribution.
44 * 3. The name of the author may not be used to endorse or promote products
45 * derived from this software without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
48 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
49 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
50 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
51 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
52 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
55 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
58 * This file is part of the lwIP TCP/IP stack.
60 * Author: Adam Dunkels <adam@sics.se>
66 #include "lwip/stats.h"
69 #include "lwip/memp.h"
70 #include "lwip/pbuf.h"
72 #include "arch/perf.h"
74 #include "lwip/tcp_impl.h"
76 #if LWIP_CHECKSUM_ON_COPY
77 #include "lwip/inet_chksum.h"
82 #define SIZEOF_STRUCT_PBUF LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf))
83 /* Since the pool is created in memp, PBUF_POOL_BUFSIZE will be automatically
84 aligned there. Therefore, PBUF_POOL_BUFSIZE_ALIGNED can be used here. */
85 #define PBUF_POOL_BUFSIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE)
87 #if !LWIP_TCP || !TCP_QUEUE_OOSEQ || NO_SYS
88 #define PBUF_POOL_IS_EMPTY()
89 #else /* !LWIP_TCP || !TCP_QUEUE_OOSEQ || NO_SYS */
90 /** Define this to 0 to prevent freeing ooseq pbufs when the PBUF_POOL is empty */
91 #ifndef PBUF_POOL_FREE_OOSEQ
92 #define PBUF_POOL_FREE_OOSEQ 1
93 #endif /* PBUF_POOL_FREE_OOSEQ */
95 #if PBUF_POOL_FREE_OOSEQ
96 #include "lwip/tcpip.h"
97 #define PBUF_POOL_IS_EMPTY() pbuf_pool_is_empty()
98 static u8_t pbuf_free_ooseq_queued
;
100 * Attempt to reclaim some memory from queued out-of-sequence TCP segments
101 * if we run out of pool pbufs. It's better to give priority to new packets
102 * if we're running out.
104 * This must be done in the correct thread context therefore this function
105 * can only be used with NO_SYS=0 and through tcpip_callback.
108 pbuf_free_ooseq(void* arg
)
111 SYS_ARCH_DECL_PROTECT(old_level
);
112 LWIP_UNUSED_ARG(arg
);
114 SYS_ARCH_PROTECT(old_level
);
115 pbuf_free_ooseq_queued
= 0;
116 SYS_ARCH_UNPROTECT(old_level
);
118 for (pcb
= tcp_active_pcbs
; NULL
!= pcb
; pcb
= pcb
->next
) {
119 if (NULL
!= pcb
->ooseq
) {
120 /** Free the ooseq pbufs of one PCB only */
121 LWIP_DEBUGF(PBUF_DEBUG
| LWIP_DBG_TRACE
, ("pbuf_free_ooseq: freeing out-of-sequence pbufs\n"));
122 tcp_segs_free(pcb
->ooseq
);
129 /** Queue a call to pbuf_free_ooseq if not already queued. */
131 pbuf_pool_is_empty(void)
134 SYS_ARCH_DECL_PROTECT(old_level
);
136 SYS_ARCH_PROTECT(old_level
);
137 queued
= pbuf_free_ooseq_queued
;
138 pbuf_free_ooseq_queued
= 1;
139 SYS_ARCH_UNPROTECT(old_level
);
142 /* queue a call to pbuf_free_ooseq if not already queued */
143 if(tcpip_callback_with_block(pbuf_free_ooseq
, NULL
, 0) != ERR_OK
) {
144 SYS_ARCH_PROTECT(old_level
);
145 pbuf_free_ooseq_queued
= 0;
146 SYS_ARCH_UNPROTECT(old_level
);
150 #endif /* PBUF_POOL_FREE_OOSEQ */
151 #endif /* !LWIP_TCP || !TCP_QUEUE_OOSEQ || NO_SYS */
154 * Allocates a pbuf of the given type (possibly a chain for PBUF_POOL type).
156 * The actual memory allocated for the pbuf is determined by the
157 * layer at which the pbuf is allocated and the requested size
158 * (from the size parameter).
160 * @param layer flag to define header size
161 * @param length size of the pbuf's payload
162 * @param type this parameter decides how and where the pbuf
163 * should be allocated as follows:
165 * - PBUF_RAM: buffer memory for pbuf is allocated as one large
166 * chunk. This includes protocol headers as well.
167 * - PBUF_ROM: no buffer memory is allocated for the pbuf, even for
168 * protocol headers. Additional headers must be prepended
169 * by allocating another pbuf and chain in to the front of
170 * the ROM pbuf. It is assumed that the memory used is really
171 * similar to ROM in that it is immutable and will not be
172 * changed. Memory which is dynamic should generally not
173 * be attached to PBUF_ROM pbufs. Use PBUF_REF instead.
174 * - PBUF_REF: no buffer memory is allocated for the pbuf, even for
175 * protocol headers. It is assumed that the pbuf is only
176 * being used in a single thread. If the pbuf gets queued,
177 * then pbuf_take should be called to copy the buffer.
178 * - PBUF_POOL: the pbuf is allocated as a pbuf chain, with pbufs from
179 * the pbuf pool that is allocated during pbuf_init().
181 * @return the allocated pbuf. If multiple pbufs where allocated, this
182 * is the first pbuf of a pbuf chain.
185 pbuf_alloc(pbuf_layer layer
, u16_t length
, pbuf_type type
)
187 struct pbuf
*p
, *q
, *r
;
189 s32_t rem_len
; /* remaining length */
190 LWIP_DEBUGF(PBUF_DEBUG
| LWIP_DBG_TRACE
, ("pbuf_alloc(length=%"U16_F
")\n", length
));
192 /* determine header offset */
196 /* add room for transport (often TCP) layer header */
197 offset
+= PBUF_TRANSPORT_HLEN
;
200 /* add room for IP layer header */
201 offset
+= PBUF_IP_HLEN
;
204 /* add room for link layer header */
205 offset
+= PBUF_LINK_HLEN
;
210 LWIP_ASSERT("pbuf_alloc: bad pbuf layer", 0);
216 /* allocate head of pbuf chain into p */
217 p
= (struct pbuf
*)memp_malloc(MEMP_PBUF_POOL
);
218 LWIP_DEBUGF(PBUF_DEBUG
| LWIP_DBG_TRACE
, ("pbuf_alloc: allocated pbuf %p\n", (void *)p
));
220 PBUF_POOL_IS_EMPTY();
226 /* make the payload pointer point 'offset' bytes into pbuf data memory */
227 p
->payload
= LWIP_MEM_ALIGN((void *)((u8_t
*)p
+ (SIZEOF_STRUCT_PBUF
+ offset
)));
228 LWIP_ASSERT("pbuf_alloc: pbuf p->payload properly aligned",
229 ((mem_ptr_t
)p
->payload
% MEM_ALIGNMENT
) == 0);
230 /* the total length of the pbuf chain is the requested size */
232 /* set the length of the first pbuf in the chain */
233 p
->len
= LWIP_MIN(length
, PBUF_POOL_BUFSIZE_ALIGNED
- LWIP_MEM_ALIGN_SIZE(offset
));
234 LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",
235 ((u8_t
*)p
->payload
+ p
->len
<=
236 (u8_t
*)p
+ SIZEOF_STRUCT_PBUF
+ PBUF_POOL_BUFSIZE_ALIGNED
));
237 LWIP_ASSERT("PBUF_POOL_BUFSIZE must be bigger than MEM_ALIGNMENT",
238 (PBUF_POOL_BUFSIZE_ALIGNED
- LWIP_MEM_ALIGN_SIZE(offset
)) > 0 );
239 /* set reference count (needed here in case we fail) */
242 /* now allocate the tail of the pbuf chain */
244 /* remember first pbuf for linkage in next iteration */
246 /* remaining length to be allocated */
247 rem_len
= length
- p
->len
;
248 /* any remaining pbufs to be allocated? */
249 while (rem_len
> 0) {
250 q
= (struct pbuf
*)memp_malloc(MEMP_PBUF_POOL
);
252 PBUF_POOL_IS_EMPTY();
253 /* free chain so far allocated */
255 /* bail out unsuccesfully */
261 /* make previous pbuf point to this pbuf */
263 /* set total length of this pbuf and next in chain */
264 LWIP_ASSERT("rem_len < max_u16_t", rem_len
< 0xffff);
265 q
->tot_len
= (u16_t
)rem_len
;
266 /* this pbuf length is pool size, unless smaller sized tail */
267 q
->len
= LWIP_MIN((u16_t
)rem_len
, PBUF_POOL_BUFSIZE_ALIGNED
);
268 q
->payload
= (void *)((u8_t
*)q
+ SIZEOF_STRUCT_PBUF
);
269 LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned",
270 ((mem_ptr_t
)q
->payload
% MEM_ALIGNMENT
) == 0);
271 LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",
272 ((u8_t
*)p
->payload
+ p
->len
<=
273 (u8_t
*)p
+ SIZEOF_STRUCT_PBUF
+ PBUF_POOL_BUFSIZE_ALIGNED
));
275 /* calculate remaining length to be allocated */
277 /* remember this pbuf for linkage in next iteration */
285 /* If pbuf is to be allocated in RAM, allocate memory for it. */
286 p
= (struct pbuf
*)mem_malloc(LWIP_MEM_ALIGN_SIZE(SIZEOF_STRUCT_PBUF
+ offset
) + LWIP_MEM_ALIGN_SIZE(length
));
290 /* Set up internal structure of the pbuf. */
291 p
->payload
= LWIP_MEM_ALIGN((void *)((u8_t
*)p
+ SIZEOF_STRUCT_PBUF
+ offset
));
292 p
->len
= p
->tot_len
= length
;
296 LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned",
297 ((mem_ptr_t
)p
->payload
% MEM_ALIGNMENT
) == 0);
299 /* pbuf references existing (non-volatile static constant) ROM payload? */
301 /* pbuf references existing (externally allocated) RAM payload? */
303 /* only allocate memory for the pbuf structure */
304 p
= (struct pbuf
*)memp_malloc(MEMP_PBUF
);
306 LWIP_DEBUGF(PBUF_DEBUG
| LWIP_DBG_LEVEL_SERIOUS
,
307 ("pbuf_alloc: Could not allocate MEMP_PBUF for PBUF_%s.\n",
308 (type
== PBUF_ROM
) ? "ROM" : "REF"));
311 /* caller must set this field properly, afterwards */
313 p
->len
= p
->tot_len
= length
;
318 LWIP_ASSERT("pbuf_alloc: erroneous type", 0);
321 /* set reference count */
325 LWIP_DEBUGF(PBUF_DEBUG
| LWIP_DBG_TRACE
, ("pbuf_alloc(length=%"U16_F
") == %p\n", length
, (void *)p
));
329 #if LWIP_SUPPORT_CUSTOM_PBUF
330 /** Initialize a custom pbuf (already allocated).
332 * @param layer flag to define header size
333 * @param length size of the pbuf's payload
334 * @param type type of the pbuf (only used to treat the pbuf accordingly, as
335 * this function allocates no memory)
336 * @param p pointer to the custom pbuf to initialize (already allocated)
337 * @param payload_mem pointer to the buffer that is used for payload and headers,
338 * must be at least big enough to hold 'length' plus the header size,
339 * may be NULL if set later
340 * @param payload_mem_len the size of the 'payload_mem' buffer, must be at least
341 * big enough to hold 'length' plus the header size
344 pbuf_alloced_custom(pbuf_layer l
, u16_t length
, pbuf_type type
, struct pbuf_custom
*p
,
345 void *payload_mem
, u16_t payload_mem_len
)
348 LWIP_DEBUGF(PBUF_DEBUG
| LWIP_DBG_TRACE
, ("pbuf_alloced_custom(length=%"U16_F
")\n", length
));
350 /* determine header offset */
354 /* add room for transport (often TCP) layer header */
355 offset
+= PBUF_TRANSPORT_HLEN
;
358 /* add room for IP layer header */
359 offset
+= PBUF_IP_HLEN
;
362 /* add room for link layer header */
363 offset
+= PBUF_LINK_HLEN
;
368 LWIP_ASSERT("pbuf_alloced_custom: bad pbuf layer", 0);
372 if (LWIP_MEM_ALIGN_SIZE(offset
) + length
< payload_mem_len
) {
373 LWIP_DEBUGF(PBUF_DEBUG
| LWIP_DBG_LEVEL_WARNING
, ("pbuf_alloced_custom(length=%"U16_F
") buffer too short\n", length
));
378 if (payload_mem
!= NULL
) {
379 p
->pbuf
.payload
= LWIP_MEM_ALIGN((void *)((u8_t
*)payload_mem
+ offset
));
381 p
->pbuf
.payload
= NULL
;
383 p
->pbuf
.flags
= PBUF_FLAG_IS_CUSTOM
;
384 p
->pbuf
.len
= p
->pbuf
.tot_len
= length
;
389 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
392 * Shrink a pbuf chain to a desired length.
394 * @param p pbuf to shrink.
395 * @param new_len desired new length of pbuf chain
397 * Depending on the desired length, the first few pbufs in a chain might
398 * be skipped and left unchanged. The new last pbuf in the chain will be
399 * resized, and any remaining pbufs will be freed.
401 * @note If the pbuf is ROM/REF, only the ->tot_len and ->len fields are adjusted.
402 * @note May not be called on a packet queue.
404 * @note Despite its name, pbuf_realloc cannot grow the size of a pbuf (chain).
407 pbuf_realloc(struct pbuf
*p
, u16_t new_len
)
410 u16_t rem_len
; /* remaining length */
413 LWIP_ASSERT("pbuf_realloc: p != NULL", p
!= NULL
);
414 LWIP_ASSERT("pbuf_realloc: sane p->type", p
->type
== PBUF_POOL
||
415 p
->type
== PBUF_ROM
||
416 p
->type
== PBUF_RAM
||
417 p
->type
== PBUF_REF
);
419 /* desired length larger than current length? */
420 if (new_len
>= p
->tot_len
) {
421 /* enlarging not yet supported */
425 /* the pbuf chain grows by (new_len - p->tot_len) bytes
426 * (which may be negative in case of shrinking) */
427 grow
= new_len
- p
->tot_len
;
429 /* first, step over any pbufs that should remain in the chain */
432 /* should this pbuf be kept? */
433 while (rem_len
> q
->len
) {
434 /* decrease remaining length by pbuf length */
436 /* decrease total length indicator */
437 LWIP_ASSERT("grow < max_u16_t", grow
< 0xffff);
438 q
->tot_len
+= (u16_t
)grow
;
439 /* proceed to next pbuf in chain */
441 LWIP_ASSERT("pbuf_realloc: q != NULL", q
!= NULL
);
443 /* we have now reached the new last pbuf (in q) */
444 /* rem_len == desired length for pbuf q */
446 /* shrink allocated memory for PBUF_RAM */
447 /* (other types merely adjust their length fields */
448 if ((q
->type
== PBUF_RAM
) && (rem_len
!= q
->len
)) {
449 /* reallocate and adjust the length of the pbuf that will be split */
450 q
= (struct pbuf
*)mem_trim(q
, (u16_t
)((u8_t
*)q
->payload
- (u8_t
*)q
) + rem_len
);
451 LWIP_ASSERT("mem_trim returned q == NULL", q
!= NULL
);
453 /* adjust length fields for new last pbuf */
457 /* any remaining pbufs in chain? */
458 if (q
->next
!= NULL
) {
459 /* free remaining pbufs in chain */
462 /* q is last packet in chain */
468 * Adjusts the payload pointer to hide or reveal headers in the payload.
470 * Adjusts the ->payload pointer so that space for a header
471 * (dis)appears in the pbuf payload.
473 * The ->payload, ->tot_len and ->len fields are adjusted.
475 * @param p pbuf to change the header size.
476 * @param header_size_increment Number of bytes to increment header size which
477 * increases the size of the pbuf. New space is on the front.
478 * (Using a negative value decreases the header size.)
479 * If hdr_size_inc is 0, this function does nothing and returns succesful.
481 * PBUF_ROM and PBUF_REF type buffers cannot have their sizes increased, so
482 * the call will fail. A check is made that the increase in header size does
483 * not move the payload pointer in front of the start of the buffer.
484 * @return non-zero on failure, zero on success.
488 pbuf_header(struct pbuf
*p
, s16_t header_size_increment
)
492 u16_t increment_magnitude
;
494 LWIP_ASSERT("p != NULL", p
!= NULL
);
495 if ((header_size_increment
== 0) || (p
== NULL
)) {
499 if (header_size_increment
< 0){
500 increment_magnitude
= -header_size_increment
;
501 /* Check that we aren't going to move off the end of the pbuf */
502 LWIP_ERROR("increment_magnitude <= p->len", (increment_magnitude
<= p
->len
), return 1;);
504 increment_magnitude
= header_size_increment
;
506 /* Can't assert these as some callers speculatively call
507 pbuf_header() to see if it's OK. Will return 1 below instead. */
508 /* Check that we've got the correct type of pbuf to work with */
509 LWIP_ASSERT("p->type == PBUF_RAM || p->type == PBUF_POOL",
510 p
->type
== PBUF_RAM
|| p
->type
== PBUF_POOL
);
511 /* Check that we aren't going to move off the beginning of the pbuf */
512 LWIP_ASSERT("p->payload - increment_magnitude >= p + SIZEOF_STRUCT_PBUF",
513 (u8_t
*)p
->payload
- increment_magnitude
>= (u8_t
*)p
+ SIZEOF_STRUCT_PBUF
);
518 /* remember current payload pointer */
519 payload
= p
->payload
;
521 /* pbuf types containing payloads? */
522 if (type
== PBUF_RAM
|| type
== PBUF_POOL
) {
523 /* set new payload pointer */
524 p
->payload
= (u8_t
*)p
->payload
- header_size_increment
;
525 /* boundary check fails? */
526 if ((u8_t
*)p
->payload
< (u8_t
*)p
+ SIZEOF_STRUCT_PBUF
) {
527 LWIP_DEBUGF( PBUF_DEBUG
| LWIP_DBG_LEVEL_SERIOUS
,
528 ("pbuf_header: failed as %p < %p (not enough space for new header size)\n",
529 (void *)p
->payload
, (void *)(p
+ 1)));
530 /* restore old payload pointer */
531 p
->payload
= payload
;
532 /* bail out unsuccesfully */
535 /* pbuf types refering to external payloads? */
536 } else if (type
== PBUF_REF
|| type
== PBUF_ROM
) {
537 /* hide a header in the payload? */
538 if ((header_size_increment
< 0) && (increment_magnitude
<= p
->len
)) {
539 /* increase payload pointer */
540 p
->payload
= (u8_t
*)p
->payload
- header_size_increment
;
542 /* cannot expand payload to front (yet!)
543 * bail out unsuccesfully */
548 LWIP_ASSERT("bad pbuf type", 0);
551 /* modify pbuf length fields */
552 p
->len
+= header_size_increment
;
553 p
->tot_len
+= header_size_increment
;
555 LWIP_DEBUGF(PBUF_DEBUG
| LWIP_DBG_TRACE
, ("pbuf_header: old %p new %p (%"S16_F
")\n",
556 (void *)payload
, (void *)p
->payload
, header_size_increment
));
562 * Dereference a pbuf chain or queue and deallocate any no-longer-used
563 * pbufs at the head of this chain or queue.
565 * Decrements the pbuf reference count. If it reaches zero, the pbuf is
568 * For a pbuf chain, this is repeated for each pbuf in the chain,
569 * up to the first pbuf which has a non-zero reference count after
570 * decrementing. So, when all reference counts are one, the whole
573 * @param p The pbuf (chain) to be dereferenced.
575 * @return the number of pbufs that were de-allocated
576 * from the head of the chain.
578 * @note MUST NOT be called on a packet queue (Not verified to work yet).
579 * @note the reference counter of a pbuf equals the number of pointers
580 * that refer to the pbuf (or into the pbuf).
582 * @internal examples:
584 * Assuming existing chains a->b->c with the following reference
585 * counts, calling pbuf_free(a) results in:
587 * 1->2->3 becomes ...1->3
588 * 3->3->3 becomes 2->3->3
589 * 1->1->2 becomes ......1
590 * 2->1->1 becomes 1->1->1
591 * 1->1->1 becomes .......
595 pbuf_free(struct pbuf
*p
)
602 LWIP_ASSERT("p != NULL", p
!= NULL
);
603 /* if assertions are disabled, proceed with debug output */
604 LWIP_DEBUGF(PBUF_DEBUG
| LWIP_DBG_LEVEL_SERIOUS
,
605 ("pbuf_free(p == NULL) was called.\n"));
608 LWIP_DEBUGF(PBUF_DEBUG
| LWIP_DBG_TRACE
, ("pbuf_free(%p)\n", (void *)p
));
612 LWIP_ASSERT("pbuf_free: sane type",
613 p
->type
== PBUF_RAM
|| p
->type
== PBUF_ROM
||
614 p
->type
== PBUF_REF
|| p
->type
== PBUF_POOL
);
617 /* de-allocate all consecutive pbufs from the head of the chain that
618 * obtain a zero reference count after decrementing*/
621 SYS_ARCH_DECL_PROTECT(old_level
);
622 /* Since decrementing ref cannot be guaranteed to be a single machine operation
623 * we must protect it. We put the new ref into a local variable to prevent
624 * further protection. */
625 SYS_ARCH_PROTECT(old_level
);
626 /* all pbufs in a chain are referenced at least once */
627 LWIP_ASSERT("pbuf_free: p->ref > 0", p
->ref
> 0);
628 /* decrease reference count (number of pointers to pbuf) */
630 SYS_ARCH_UNPROTECT(old_level
);
631 /* this pbuf is no longer referenced to? */
633 /* remember next pbuf in chain for next iteration */
635 LWIP_DEBUGF( PBUF_DEBUG
| LWIP_DBG_TRACE
, ("pbuf_free: deallocating %p\n", (void *)p
));
637 #if LWIP_SUPPORT_CUSTOM_PBUF
638 /* is this a custom pbuf? */
639 if ((p
->flags
& PBUF_FLAG_IS_CUSTOM
) != 0) {
640 struct pbuf_custom
*pc
= (struct pbuf_custom
*)p
;
641 LWIP_ASSERT("pc->custom_free_function != NULL", pc
->custom_free_function
!= NULL
);
642 pc
->custom_free_function(p
);
644 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
646 /* is this a pbuf from the pool? */
647 if (type
== PBUF_POOL
) {
648 memp_free(MEMP_PBUF_POOL
, p
);
649 /* is this a ROM or RAM referencing pbuf? */
650 } else if (type
== PBUF_ROM
|| type
== PBUF_REF
) {
651 memp_free(MEMP_PBUF
, p
);
652 /* type == PBUF_RAM */
658 /* proceed to next pbuf */
660 /* p->ref > 0, this pbuf is still referenced to */
661 /* (and so the remaining pbufs in chain as well) */
663 LWIP_DEBUGF( PBUF_DEBUG
| LWIP_DBG_TRACE
, ("pbuf_free: %p has ref %"U16_F
", ending here.\n", (void *)p
, ref
));
664 /* stop walking through the chain */
668 PERF_STOP("pbuf_free");
669 /* return number of de-allocated pbufs */
674 * Count number of pbufs in a chain
676 * @param p first pbuf of chain
677 * @return the number of pbufs in a chain
681 pbuf_clen(struct pbuf
*p
)
694 * Increment the reference count of the pbuf.
696 * @param p pbuf to increase reference counter of
700 pbuf_ref(struct pbuf
*p
)
702 SYS_ARCH_DECL_PROTECT(old_level
);
705 SYS_ARCH_PROTECT(old_level
);
707 SYS_ARCH_UNPROTECT(old_level
);
712 * Concatenate two pbufs (each may be a pbuf chain) and take over
713 * the caller's reference of the tail pbuf.
715 * @note The caller MAY NOT reference the tail pbuf afterwards.
716 * Use pbuf_chain() for that purpose.
722 pbuf_cat(struct pbuf
*h
, struct pbuf
*t
)
726 LWIP_ERROR("(h != NULL) && (t != NULL) (programmer violates API)",
727 ((h
!= NULL
) && (t
!= NULL
)), return;);
729 /* proceed to last pbuf of chain */
730 for (p
= h
; p
->next
!= NULL
; p
= p
->next
) {
731 /* add total length of second chain to all totals of first chain */
732 p
->tot_len
+= t
->tot_len
;
734 /* { p is last pbuf of first h chain, p->next == NULL } */
735 LWIP_ASSERT("p->tot_len == p->len (of last pbuf in chain)", p
->tot_len
== p
->len
);
736 LWIP_ASSERT("p->next == NULL", p
->next
== NULL
);
737 /* add total length of second chain to last pbuf total of first chain */
738 p
->tot_len
+= t
->tot_len
;
739 /* chain last pbuf of head (p) with first of tail (t) */
741 /* p->next now references t, but the caller will drop its reference to t,
742 * so netto there is no change to the reference count of t.
747 * Chain two pbufs (or pbuf chains) together.
749 * The caller MUST call pbuf_free(t) once it has stopped
750 * using it. Use pbuf_cat() instead if you no longer use t.
752 * @param h head pbuf (chain)
753 * @param t tail pbuf (chain)
754 * @note The pbufs MUST belong to the same packet.
755 * @note MAY NOT be called on a packet queue.
757 * The ->tot_len fields of all pbufs of the head chain are adjusted.
758 * The ->next field of the last pbuf of the head chain is adjusted.
759 * The ->ref field of the first pbuf of the tail chain is adjusted.
763 pbuf_chain(struct pbuf
*h
, struct pbuf
*t
)
766 /* t is now referenced by h */
768 LWIP_DEBUGF(PBUF_DEBUG
| LWIP_DBG_TRACE
, ("pbuf_chain: %p references %p\n", (void *)h
, (void *)t
));
772 * Dechains the first pbuf from its succeeding pbufs in the chain.
774 * Makes p->tot_len field equal to p->len.
775 * @param p pbuf to dechain
776 * @return remainder of the pbuf chain, or NULL if it was de-allocated.
777 * @note May not be called on a packet queue.
780 pbuf_dechain(struct pbuf
*p
)
786 /* pbuf has successor in chain? */
788 /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
789 LWIP_ASSERT("p->tot_len == p->len + q->tot_len", q
->tot_len
== p
->tot_len
- p
->len
);
790 /* enforce invariant if assertion is disabled */
791 q
->tot_len
= p
->tot_len
- p
->len
;
792 /* decouple pbuf from remainder */
794 /* total length of pbuf p is its own length only */
796 /* q is no longer referenced by p, free it */
797 LWIP_DEBUGF(PBUF_DEBUG
| LWIP_DBG_TRACE
, ("pbuf_dechain: unreferencing %p\n", (void *)q
));
798 tail_gone
= pbuf_free(q
);
800 LWIP_DEBUGF(PBUF_DEBUG
| LWIP_DBG_TRACE
,
801 ("pbuf_dechain: deallocated %p (as it is no longer referenced)\n", (void *)q
));
803 /* return remaining tail or NULL if deallocated */
805 /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
806 LWIP_ASSERT("p->tot_len == p->len", p
->tot_len
== p
->len
);
807 return ((tail_gone
> 0) ? NULL
: q
);
812 * Create PBUF_RAM copies of pbufs.
814 * Used to queue packets on behalf of the lwIP stack, such as
815 * ARP based queueing.
817 * @note You MUST explicitly use p = pbuf_take(p);
819 * @note Only one packet is copied, no packet queue!
821 * @param p_to pbuf destination of the copy
822 * @param p_from pbuf source of the copy
824 * @return ERR_OK if pbuf was copied
825 * ERR_ARG if one of the pbufs is NULL or p_to is not big
826 * enough to hold p_from
829 pbuf_copy(struct pbuf
*p_to
, struct pbuf
*p_from
)
831 u16_t offset_to
=0, offset_from
=0, len
;
833 LWIP_DEBUGF(PBUF_DEBUG
| LWIP_DBG_TRACE
, ("pbuf_copy(%p, %p)\n",
834 (void*)p_to
, (void*)p_from
));
836 /* is the target big enough to hold the source? */
837 LWIP_ERROR("pbuf_copy: target not big enough to hold source", ((p_to
!= NULL
) &&
838 (p_from
!= NULL
) && (p_to
->tot_len
>= p_from
->tot_len
)), return ERR_ARG
;);
840 /* iterate through pbuf chain */
843 LWIP_ASSERT("p_to != NULL", p_to
!= NULL
);
844 /* copy one part of the original chain */
845 if ((p_to
->len
- offset_to
) >= (p_from
->len
- offset_from
)) {
846 /* complete current p_from fits into current p_to */
847 len
= p_from
->len
- offset_from
;
849 /* current p_from does not fit into current p_to */
850 len
= p_to
->len
- offset_to
;
852 MEMCPY((u8_t
*)p_to
->payload
+ offset_to
, (u8_t
*)p_from
->payload
+ offset_from
, len
);
855 LWIP_ASSERT("offset_to <= p_to->len", offset_to
<= p_to
->len
);
856 if (offset_to
== p_to
->len
) {
857 /* on to next p_to (if any) */
861 LWIP_ASSERT("offset_from <= p_from->len", offset_from
<= p_from
->len
);
862 if (offset_from
>= p_from
->len
) {
863 /* on to next p_from (if any) */
865 p_from
= p_from
->next
;
868 if((p_from
!= NULL
) && (p_from
->len
== p_from
->tot_len
)) {
869 /* don't copy more than one packet! */
870 LWIP_ERROR("pbuf_copy() does not allow packet queues!\n",
871 (p_from
->next
== NULL
), return ERR_VAL
;);
873 if((p_to
!= NULL
) && (p_to
->len
== p_to
->tot_len
)) {
874 /* don't copy more than one packet! */
875 LWIP_ERROR("pbuf_copy() does not allow packet queues!\n",
876 (p_to
->next
== NULL
), return ERR_VAL
;);
879 LWIP_DEBUGF(PBUF_DEBUG
| LWIP_DBG_TRACE
, ("pbuf_copy: end of chain reached.\n"));
884 * Copy (part of) the contents of a packet buffer
885 * to an application supplied buffer.
887 * @param buf the pbuf from which to copy data
888 * @param dataptr the application supplied buffer
889 * @param len length of data to copy (dataptr must be big enough). No more
890 * than buf->tot_len will be copied, irrespective of len
891 * @param offset offset into the packet buffer from where to begin copying len bytes
892 * @return the number of bytes copied, or 0 on failure
895 pbuf_copy_partial(struct pbuf
*buf
, void *dataptr
, u16_t len
, u16_t offset
)
900 u16_t copied_total
= 0;
902 LWIP_ERROR("pbuf_copy_partial: invalid buf", (buf
!= NULL
), return 0;);
903 LWIP_ERROR("pbuf_copy_partial: invalid dataptr", (dataptr
!= NULL
), return 0;);
907 if((buf
== NULL
) || (dataptr
== NULL
)) {
911 /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
912 for(p
= buf
; len
!= 0 && p
!= NULL
; p
= p
->next
) {
913 if ((offset
!= 0) && (offset
>= p
->len
)) {
914 /* don't copy from this buffer -> on to the next */
917 /* copy from this buffer. maybe only partially. */
918 buf_copy_len
= p
->len
- offset
;
919 if (buf_copy_len
> len
)
921 /* copy the necessary parts of the buffer */
922 MEMCPY(&((char*)dataptr
)[left
], &((char*)p
->payload
)[offset
], buf_copy_len
);
923 copied_total
+= buf_copy_len
;
924 left
+= buf_copy_len
;
933 * Copy application supplied data into a pbuf.
934 * This function can only be used to copy the equivalent of buf->tot_len data.
936 * @param buf pbuf to fill with data
937 * @param dataptr application supplied data buffer
938 * @param len length of the application supplied data buffer
940 * @return ERR_OK if successful, ERR_MEM if the pbuf is not big enough
943 pbuf_take(struct pbuf
*buf
, const void *dataptr
, u16_t len
)
947 u16_t total_copy_len
= len
;
948 u16_t copied_total
= 0;
950 LWIP_ERROR("pbuf_take: invalid buf", (buf
!= NULL
), return 0;);
951 LWIP_ERROR("pbuf_take: invalid dataptr", (dataptr
!= NULL
), return 0;);
953 if ((buf
== NULL
) || (dataptr
== NULL
) || (buf
->tot_len
< len
)) {
957 /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
958 for(p
= buf
; total_copy_len
!= 0; p
= p
->next
) {
959 LWIP_ASSERT("pbuf_take: invalid pbuf", p
!= NULL
);
960 buf_copy_len
= total_copy_len
;
961 if (buf_copy_len
> p
->len
) {
962 /* this pbuf cannot hold all remaining data */
963 buf_copy_len
= p
->len
;
965 /* copy the necessary parts of the buffer */
966 MEMCPY(p
->payload
, &((char*)dataptr
)[copied_total
], buf_copy_len
);
967 total_copy_len
-= buf_copy_len
;
968 copied_total
+= buf_copy_len
;
970 LWIP_ASSERT("did not copy all data", total_copy_len
== 0 && copied_total
== len
);
975 * Creates a single pbuf out of a queue of pbufs.
977 * @remark: Either the source pbuf 'p' is freed by this function or the original
978 * pbuf 'p' is returned, therefore the caller has to check the result!
980 * @param p the source pbuf
981 * @param layer pbuf_layer of the new pbuf
983 * @return a new, single pbuf (p->next is NULL)
984 * or the old pbuf if allocation fails
987 pbuf_coalesce(struct pbuf
*p
, pbuf_layer layer
)
991 if (p
->next
== NULL
) {
994 q
= pbuf_alloc(layer
, p
->tot_len
, PBUF_RAM
);
996 /* @todo: what do we do now? */
999 err
= pbuf_copy(q
, p
);
1000 LWIP_ASSERT("pbuf_copy failed", err
== ERR_OK
);
1005 #if LWIP_CHECKSUM_ON_COPY
1007 * Copies data into a single pbuf (*not* into a pbuf queue!) and updates
1008 * the checksum while copying
1010 * @param p the pbuf to copy data into
1011 * @param start_offset offset of p->payload where to copy the data to
1012 * @param dataptr data to copy into the pbuf
1013 * @param len length of data to copy into the pbuf
1014 * @param chksum pointer to the checksum which is updated
1015 * @return ERR_OK if successful, another error if the data does not fit
1016 * within the (first) pbuf (no pbuf queues!)
1019 pbuf_fill_chksum(struct pbuf
*p
, u16_t start_offset
, const void *dataptr
,
1020 u16_t len
, u16_t
*chksum
)
1025 LWIP_ASSERT("p != NULL", p
!= NULL
);
1026 LWIP_ASSERT("dataptr != NULL", dataptr
!= NULL
);
1027 LWIP_ASSERT("chksum != NULL", chksum
!= NULL
);
1028 LWIP_ASSERT("len != 0", len
!= 0);
1030 if ((start_offset
>= p
->len
) || (start_offset
+ len
> p
->len
)) {
1034 dst_ptr
= ((char*)p
->payload
) + start_offset
;
1035 copy_chksum
= LWIP_CHKSUM_COPY(dst_ptr
, dataptr
, len
);
1036 if ((start_offset
& 1) != 0) {
1037 copy_chksum
= SWAP_BYTES_IN_WORD(copy_chksum
);
1041 *chksum
= FOLD_U32T(acc
);
1044 #endif /* LWIP_CHECKSUM_ON_COPY */
1046 /** Get one byte from the specified position in a pbuf
1047 * WARNING: returns zero for offset >= p->tot_len
1049 * @param p pbuf to parse
1050 * @param offset offset into p of the byte to return
1051 * @return byte at an offset into p OR ZERO IF 'offset' >= p->tot_len
1054 pbuf_get_at(struct pbuf
* p
, u16_t offset
)
1056 u16_t copy_from
= offset
;
1059 /* get the correct pbuf */
1060 while ((q
!= NULL
) && (q
->len
<= copy_from
)) {
1061 copy_from
-= q
->len
;
1064 /* return requested data if pbuf is OK */
1065 if ((q
!= NULL
) && (q
->len
> copy_from
)) {
1066 return ((u8_t
*)q
->payload
)[copy_from
];
1071 /** Compare pbuf contents at specified offset with memory s2, both of length n
1073 * @param p pbuf to compare
1074 * @param offset offset into p at wich to start comparing
1075 * @param s2 buffer to compare
1076 * @param n length of buffer to compare
1077 * @return zero if equal, nonzero otherwise
1078 * (0xffff if p is too short, diffoffset+1 otherwise)
1081 pbuf_memcmp(struct pbuf
* p
, u16_t offset
, const void* s2
, u16_t n
)
1083 u16_t start
= offset
;
1086 /* get the correct pbuf */
1087 while ((q
!= NULL
) && (q
->len
<= start
)) {
1091 /* return requested data if pbuf is OK */
1092 if ((q
!= NULL
) && (q
->len
> start
)) {
1094 for(i
= 0; i
< n
; i
++) {
1095 u8_t a
= pbuf_get_at(q
, start
+ i
);
1096 u8_t b
= ((u8_t
*)s2
)[i
];
1106 /** Find occurrence of mem (with length mem_len) in pbuf p, starting at offset
1109 * @param p pbuf to search, maximum length is 0xFFFE since 0xFFFF is used as
1110 * return value 'not found'
1111 * @param mem search for the contents of this buffer
1112 * @param mem_len length of 'mem'
1113 * @param start_offset offset into p at which to start searching
1114 * @return 0xFFFF if substr was not found in p or the index where it was found
1117 pbuf_memfind(struct pbuf
* p
, const void* mem
, u16_t mem_len
, u16_t start_offset
)
1120 u16_t max
= p
->tot_len
- mem_len
;
1121 if (p
->tot_len
>= mem_len
+ start_offset
) {
1122 for(i
= start_offset
; i
<= max
; ) {
1123 u16_t plus
= pbuf_memcmp(p
, i
, mem
, mem_len
);
1134 /** Find occurrence of substr with length substr_len in pbuf p, start at offset
1136 * WARNING: in contrast to strstr(), this one does not stop at the first \0 in
1137 * the pbuf/source string!
1139 * @param p pbuf to search, maximum length is 0xFFFE since 0xFFFF is used as
1140 * return value 'not found'
1141 * @param substr string to search for in p, maximum length is 0xFFFE
1142 * @return 0xFFFF if substr was not found in p or the index where it was found
1145 pbuf_strstr(struct pbuf
* p
, const char* substr
)
1148 if ((substr
== NULL
) || (substr
[0] == 0) || (p
->tot_len
== 0xFFFF)) {
1151 substr_len
= strlen(substr
);
1152 if (substr_len
>= 0xFFFF) {
1155 return pbuf_memfind(p
, substr
, (u16_t
)substr_len
, 0);