STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
pbuf.c
Go to the documentation of this file.
1 
32 /*
33  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without modification,
37  * are permitted provided that the following conditions are met:
38  *
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.
46  *
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
56  * OF SUCH DAMAGE.
57  *
58  * This file is part of the lwIP TCP/IP stack.
59  *
60  * Author: Adam Dunkels <adam@sics.se>
61  *
62  */
63 
64 #include "lwip/opt.h"
65 
66 #include "lwip/stats.h"
67 #include "lwip/def.h"
68 #include "lwip/mem.h"
69 #include "lwip/memp.h"
70 #include "lwip/pbuf.h"
71 #include "lwip/sys.h"
72 #if LWIP_TCP && TCP_QUEUE_OOSEQ
73 #include "lwip/priv/tcp_priv.h"
74 #endif
75 #if LWIP_CHECKSUM_ON_COPY
76 #include "lwip/inet_chksum.h"
77 #endif
78 
79 #include <string.h>
80 
81 #define SIZEOF_STRUCT_PBUF LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf))
82 /* Since the pool is created in memp, PBUF_POOL_BUFSIZE will be automatically
83  aligned there. Therefore, PBUF_POOL_BUFSIZE_ALIGNED can be used here. */
84 #define PBUF_POOL_BUFSIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE)
85 
86 #if !LWIP_TCP || !TCP_QUEUE_OOSEQ || !PBUF_POOL_FREE_OOSEQ
87 #define PBUF_POOL_IS_EMPTY()
88 #else /* !LWIP_TCP || !TCP_QUEUE_OOSEQ || !PBUF_POOL_FREE_OOSEQ */
89 
90 #if !NO_SYS
91 #ifndef PBUF_POOL_FREE_OOSEQ_QUEUE_CALL
92 #include "lwip/tcpip.h"
93 #define PBUF_POOL_FREE_OOSEQ_QUEUE_CALL() do { \
94  if (tcpip_callback_with_block(pbuf_free_ooseq_callback, NULL, 0) != ERR_OK) { \
95  SYS_ARCH_PROTECT(old_level); \
96  pbuf_free_ooseq_pending = 0; \
97  SYS_ARCH_UNPROTECT(old_level); \
98  } } while(0)
99 #endif /* PBUF_POOL_FREE_OOSEQ_QUEUE_CALL */
100 #endif /* !NO_SYS */
101 
102 volatile u8_t pbuf_free_ooseq_pending;
103 #define PBUF_POOL_IS_EMPTY() pbuf_pool_is_empty()
104 
113 #if !NO_SYS
114 static
115 #endif /* !NO_SYS */
116 void
117 pbuf_free_ooseq(void)
118 {
119  struct tcp_pcb* pcb;
120  SYS_ARCH_SET(pbuf_free_ooseq_pending, 0);
121 
122  for (pcb = tcp_active_pcbs; NULL != pcb; pcb = pcb->next) {
123  if (NULL != pcb->ooseq) {
125  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free_ooseq: freeing out-of-sequence pbufs\n"));
126  tcp_segs_free(pcb->ooseq);
127  pcb->ooseq = NULL;
128  return;
129  }
130  }
131 }
132 
133 #if !NO_SYS
134 
137 static void
138 pbuf_free_ooseq_callback(void *arg)
139 {
140  LWIP_UNUSED_ARG(arg);
141  pbuf_free_ooseq();
142 }
143 #endif /* !NO_SYS */
144 
146 static void
147 pbuf_pool_is_empty(void)
148 {
149 #ifndef PBUF_POOL_FREE_OOSEQ_QUEUE_CALL
150  SYS_ARCH_SET(pbuf_free_ooseq_pending, 1);
151 #else /* PBUF_POOL_FREE_OOSEQ_QUEUE_CALL */
152  u8_t queued;
153  SYS_ARCH_DECL_PROTECT(old_level);
154  SYS_ARCH_PROTECT(old_level);
155  queued = pbuf_free_ooseq_pending;
156  pbuf_free_ooseq_pending = 1;
157  SYS_ARCH_UNPROTECT(old_level);
158 
159  if (!queued) {
160  /* queue a call to pbuf_free_ooseq if not already queued */
161  PBUF_POOL_FREE_OOSEQ_QUEUE_CALL();
162  }
163 #endif /* PBUF_POOL_FREE_OOSEQ_QUEUE_CALL */
164 }
165 #endif /* !LWIP_TCP || !TCP_QUEUE_OOSEQ || !PBUF_POOL_FREE_OOSEQ */
166 
198 struct pbuf *
200 {
201  struct pbuf *p, *q, *r;
202  u16_t offset;
203  s32_t rem_len; /* remaining length */
204  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F")\n", length));
205 
206  /* determine header offset */
207  switch (layer) {
208  case PBUF_TRANSPORT:
209  /* add room for transport (often TCP) layer header */
211  break;
212  case PBUF_IP:
213  /* add room for IP layer header */
215  break;
216  case PBUF_LINK:
217  /* add room for link layer header */
219  break;
220  case PBUF_RAW_TX:
221  /* add room for encapsulating link layer headers (e.g. 802.11) */
223  break;
224  case PBUF_RAW:
225  /* no offset (e.g. RX buffers or chain successors) */
226  offset = 0;
227  break;
228  default:
229  LWIP_ASSERT("pbuf_alloc: bad pbuf layer", 0);
230  return NULL;
231  }
232 
233  switch (type) {
234  case PBUF_POOL:
235  /* allocate head of pbuf chain into p */
236  p = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
237  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc: allocated pbuf %p\n", (void *)p));
238  if (p == NULL) {
240  return NULL;
241  }
242  p->type = type;
243  p->next = NULL;
244 
245  /* make the payload pointer point 'offset' bytes into pbuf data memory */
246  p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + (SIZEOF_STRUCT_PBUF + offset)));
247  LWIP_ASSERT("pbuf_alloc: pbuf p->payload properly aligned",
248  ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
249  /* the total length of the pbuf chain is the requested size */
250  p->tot_len = length;
251  /* set the length of the first pbuf in the chain */
253  LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",
254  ((u8_t*)p->payload + p->len <=
256  LWIP_ASSERT("PBUF_POOL_BUFSIZE must be bigger than MEM_ALIGNMENT",
258  /* set reference count (needed here in case we fail) */
259  p->ref = 1;
260 
261  /* now allocate the tail of the pbuf chain */
262 
263  /* remember first pbuf for linkage in next iteration */
264  r = p;
265  /* remaining length to be allocated */
266  rem_len = length - p->len;
267  /* any remaining pbufs to be allocated? */
268  while (rem_len > 0) {
269  q = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
270  if (q == NULL) {
272  /* free chain so far allocated */
273  pbuf_free(p);
274  /* bail out unsuccessfully */
275  return NULL;
276  }
277  q->type = type;
278  q->flags = 0;
279  q->next = NULL;
280  /* make previous pbuf point to this pbuf */
281  r->next = q;
282  /* set total length of this pbuf and next in chain */
283  LWIP_ASSERT("rem_len < max_u16_t", rem_len < 0xffff);
284  q->tot_len = (u16_t)rem_len;
285  /* this pbuf length is pool size, unless smaller sized tail */
287  q->payload = (void *)((u8_t *)q + SIZEOF_STRUCT_PBUF);
288  LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned",
289  ((mem_ptr_t)q->payload % MEM_ALIGNMENT) == 0);
290  LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",
291  ((u8_t*)p->payload + p->len <=
293  q->ref = 1;
294  /* calculate remaining length to be allocated */
295  rem_len -= q->len;
296  /* remember this pbuf for linkage in next iteration */
297  r = q;
298  }
299  /* end of chain */
300  /*r->next = NULL;*/
301 
302  break;
303  case PBUF_RAM:
304  /* If pbuf is to be allocated in RAM, allocate memory for it. */
306  if (p == NULL) {
307  return NULL;
308  }
309  /* Set up internal structure of the pbuf. */
310  p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + SIZEOF_STRUCT_PBUF + offset));
311  p->len = p->tot_len = length;
312  p->next = NULL;
313  p->type = type;
314 
315  LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned",
316  ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
317  break;
318  /* pbuf references existing (non-volatile static constant) ROM payload? */
319  case PBUF_ROM:
320  /* pbuf references existing (externally allocated) RAM payload? */
321  case PBUF_REF:
322  /* only allocate memory for the pbuf structure */
323  p = (struct pbuf *)memp_malloc(MEMP_PBUF);
324  if (p == NULL) {
326  ("pbuf_alloc: Could not allocate MEMP_PBUF for PBUF_%s.\n",
327  (type == PBUF_ROM) ? "ROM" : "REF"));
328  return NULL;
329  }
330  /* caller must set this field properly, afterwards */
331  p->payload = NULL;
332  p->len = p->tot_len = length;
333  p->next = NULL;
334  p->type = type;
335  break;
336  default:
337  LWIP_ASSERT("pbuf_alloc: erroneous type", 0);
338  return NULL;
339  }
340  /* set reference count */
341  p->ref = 1;
342  /* set flags */
343  p->flags = 0;
344  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F") == %p\n", length, (void *)p));
345  return p;
346 }
347 
348 #if LWIP_SUPPORT_CUSTOM_PBUF
349 
363 struct pbuf*
364 pbuf_alloced_custom(pbuf_layer l, u16_t length, pbuf_type type, struct pbuf_custom *p,
365  void *payload_mem, u16_t payload_mem_len)
366 {
367  u16_t offset;
368  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloced_custom(length=%"U16_F")\n", length));
369 
370  /* determine header offset */
371  switch (l) {
372  case PBUF_TRANSPORT:
373  /* add room for transport (often TCP) layer header */
375  break;
376  case PBUF_IP:
377  /* add room for IP layer header */
379  break;
380  case PBUF_LINK:
381  /* add room for link layer header */
383  break;
384  case PBUF_RAW_TX:
385  /* add room for encapsulating link layer headers (e.g. 802.11) */
387  break;
388  case PBUF_RAW:
389  offset = 0;
390  break;
391  default:
392  LWIP_ASSERT("pbuf_alloced_custom: bad pbuf layer", 0);
393  return NULL;
394  }
395 
396  if (LWIP_MEM_ALIGN_SIZE(offset) + length > payload_mem_len) {
397  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_WARNING, ("pbuf_alloced_custom(length=%"U16_F") buffer too short\n", length));
398  return NULL;
399  }
400 
401  p->pbuf.next = NULL;
402  if (payload_mem != NULL) {
403  p->pbuf.payload = (u8_t *)payload_mem + LWIP_MEM_ALIGN_SIZE(offset);
404  } else {
405  p->pbuf.payload = NULL;
406  }
407  p->pbuf.flags = PBUF_FLAG_IS_CUSTOM;
408  p->pbuf.len = p->pbuf.tot_len = length;
409  p->pbuf.type = type;
410  p->pbuf.ref = 1;
411  return &p->pbuf;
412 }
413 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
414 
430 void
431 pbuf_realloc(struct pbuf *p, u16_t new_len)
432 {
433  struct pbuf *q;
434  u16_t rem_len; /* remaining length */
435  s32_t grow;
436 
437  LWIP_ASSERT("pbuf_realloc: p != NULL", p != NULL);
438  LWIP_ASSERT("pbuf_realloc: sane p->type", p->type == PBUF_POOL ||
439  p->type == PBUF_ROM ||
440  p->type == PBUF_RAM ||
441  p->type == PBUF_REF);
442 
443  /* desired length larger than current length? */
444  if (new_len >= p->tot_len) {
445  /* enlarging not yet supported */
446  return;
447  }
448 
449  /* the pbuf chain grows by (new_len - p->tot_len) bytes
450  * (which may be negative in case of shrinking) */
451  grow = new_len - p->tot_len;
452 
453  /* first, step over any pbufs that should remain in the chain */
454  rem_len = new_len;
455  q = p;
456  /* should this pbuf be kept? */
457  while (rem_len > q->len) {
458  /* decrease remaining length by pbuf length */
459  rem_len -= q->len;
460  /* decrease total length indicator */
461  LWIP_ASSERT("grow < max_u16_t", grow < 0xffff);
462  q->tot_len += (u16_t)grow;
463  /* proceed to next pbuf in chain */
464  q = q->next;
465  LWIP_ASSERT("pbuf_realloc: q != NULL", q != NULL);
466  }
467  /* we have now reached the new last pbuf (in q) */
468  /* rem_len == desired length for pbuf q */
469 
470  /* shrink allocated memory for PBUF_RAM */
471  /* (other types merely adjust their length fields */
472  if ((q->type == PBUF_RAM) && (rem_len != q->len)
474  && ((q->flags & PBUF_FLAG_IS_CUSTOM) == 0)
475 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
476  ) {
477  /* reallocate and adjust the length of the pbuf that will be split */
478  q = (struct pbuf *)mem_trim(q, (u16_t)((u8_t *)q->payload - (u8_t *)q) + rem_len);
479  LWIP_ASSERT("mem_trim returned q == NULL", q != NULL);
480  }
481  /* adjust length fields for new last pbuf */
482  q->len = rem_len;
483  q->tot_len = q->len;
484 
485  /* any remaining pbufs in chain? */
486  if (q->next != NULL) {
487  /* free remaining pbufs in chain */
488  pbuf_free(q->next);
489  }
490  /* q is last packet in chain */
491  q->next = NULL;
492 
493 }
494 
506 static u8_t
507 pbuf_header_impl(struct pbuf *p, s16_t header_size_increment, u8_t force)
508 {
509  u16_t type;
510  void *payload;
511  u16_t increment_magnitude;
512 
513  LWIP_ASSERT("p != NULL", p != NULL);
514  if ((header_size_increment == 0) || (p == NULL)) {
515  return 0;
516  }
517 
518  if (header_size_increment < 0) {
519  increment_magnitude = -header_size_increment;
520  /* Check that we aren't going to move off the end of the pbuf */
521  LWIP_ERROR("increment_magnitude <= p->len", (increment_magnitude <= p->len), return 1;);
522  } else {
523  increment_magnitude = header_size_increment;
524 #if 0
525  /* Can't assert these as some callers speculatively call
526  pbuf_header() to see if it's OK. Will return 1 below instead. */
527  /* Check that we've got the correct type of pbuf to work with */
528  LWIP_ASSERT("p->type == PBUF_RAM || p->type == PBUF_POOL",
529  p->type == PBUF_RAM || p->type == PBUF_POOL);
530  /* Check that we aren't going to move off the beginning of the pbuf */
531  LWIP_ASSERT("p->payload - increment_magnitude >= p + SIZEOF_STRUCT_PBUF",
532  (u8_t *)p->payload - increment_magnitude >= (u8_t *)p + SIZEOF_STRUCT_PBUF);
533 #endif
534  }
535 
536  type = p->type;
537  /* remember current payload pointer */
538  payload = p->payload;
539 
540  /* pbuf types containing payloads? */
541  if (type == PBUF_RAM || type == PBUF_POOL) {
542  /* set new payload pointer */
543  p->payload = (u8_t *)p->payload - header_size_increment;
544  /* boundary check fails? */
545  if ((u8_t *)p->payload < (u8_t *)p + SIZEOF_STRUCT_PBUF) {
547  ("pbuf_header: failed as %p < %p (not enough space for new header size)\n",
548  (void *)p->payload, (void *)(p + 1)));
549  /* restore old payload pointer */
550  p->payload = payload;
551  /* bail out unsuccessfully */
552  return 1;
553  }
554  /* pbuf types referring to external payloads? */
555  } else if (type == PBUF_REF || type == PBUF_ROM) {
556  /* hide a header in the payload? */
557  if ((header_size_increment < 0) && (increment_magnitude <= p->len)) {
558  /* increase payload pointer */
559  p->payload = (u8_t *)p->payload - header_size_increment;
560  } else if ((header_size_increment > 0) && force) {
561  p->payload = (u8_t *)p->payload - header_size_increment;
562  } else {
563  /* cannot expand payload to front (yet!)
564  * bail out unsuccessfully */
565  return 1;
566  }
567  } else {
568  /* Unknown type */
569  LWIP_ASSERT("bad pbuf type", 0);
570  return 1;
571  }
572  /* modify pbuf length fields */
573  p->len += header_size_increment;
574  p->tot_len += header_size_increment;
575 
576  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_header: old %p new %p (%"S16_F")\n",
577  (void *)payload, (void *)p->payload, header_size_increment));
578 
579  return 0;
580 }
581 
602 u8_t
603 pbuf_header(struct pbuf *p, s16_t header_size_increment)
604 {
605  return pbuf_header_impl(p, header_size_increment, 0);
606 }
607 
612 u8_t
613 pbuf_header_force(struct pbuf *p, s16_t header_size_increment)
614 {
615  return pbuf_header_impl(p, header_size_increment, 1);
616 }
617 
651 u8_t
652 pbuf_free(struct pbuf *p)
653 {
654  u16_t type;
655  struct pbuf *q;
656  u8_t count;
657 
658  if (p == NULL) {
659  LWIP_ASSERT("p != NULL", p != NULL);
660  /* if assertions are disabled, proceed with debug output */
662  ("pbuf_free(p == NULL) was called.\n"));
663  return 0;
664  }
665  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free(%p)\n", (void *)p));
666 
667  PERF_START;
668 
669  LWIP_ASSERT("pbuf_free: sane type",
670  p->type == PBUF_RAM || p->type == PBUF_ROM ||
671  p->type == PBUF_REF || p->type == PBUF_POOL);
672 
673  count = 0;
674  /* de-allocate all consecutive pbufs from the head of the chain that
675  * obtain a zero reference count after decrementing*/
676  while (p != NULL) {
677  u16_t ref;
678  SYS_ARCH_DECL_PROTECT(old_level);
679  /* Since decrementing ref cannot be guaranteed to be a single machine operation
680  * we must protect it. We put the new ref into a local variable to prevent
681  * further protection. */
682  SYS_ARCH_PROTECT(old_level);
683  /* all pbufs in a chain are referenced at least once */
684  LWIP_ASSERT("pbuf_free: p->ref > 0", p->ref > 0);
685  /* decrease reference count (number of pointers to pbuf) */
686  ref = --(p->ref);
687  SYS_ARCH_UNPROTECT(old_level);
688  /* this pbuf is no longer referenced to? */
689  if (ref == 0) {
690  /* remember next pbuf in chain for next iteration */
691  q = p->next;
692  LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: deallocating %p\n", (void *)p));
693  type = p->type;
694 #if LWIP_SUPPORT_CUSTOM_PBUF
695  /* is this a custom pbuf? */
696  if ((p->flags & PBUF_FLAG_IS_CUSTOM) != 0) {
697  struct pbuf_custom *pc = (struct pbuf_custom*)p;
698  LWIP_ASSERT("pc->custom_free_function != NULL", pc->custom_free_function != NULL);
699  pc->custom_free_function(p);
700  } else
701 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
702  {
703  /* is this a pbuf from the pool? */
704  if (type == PBUF_POOL) {
705  memp_free(MEMP_PBUF_POOL, p);
706  /* is this a ROM or RAM referencing pbuf? */
707  } else if (type == PBUF_ROM || type == PBUF_REF) {
708  memp_free(MEMP_PBUF, p);
709  /* type == PBUF_RAM */
710  } else {
711  mem_free(p);
712  }
713  }
714  count++;
715  /* proceed to next pbuf */
716  p = q;
717  /* p->ref > 0, this pbuf is still referenced to */
718  /* (and so the remaining pbufs in chain as well) */
719  } else {
720  LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: %p has ref %"U16_F", ending here.\n", (void *)p, ref));
721  /* stop walking through the chain */
722  p = NULL;
723  }
724  }
725  PERF_STOP("pbuf_free");
726  /* return number of de-allocated pbufs */
727  return count;
728 }
729 
737 u8_t
738 pbuf_clen(struct pbuf *p)
739 {
740  u8_t len;
741 
742  len = 0;
743  while (p != NULL) {
744  ++len;
745  p = p->next;
746  }
747  return len;
748 }
749 
756 void
757 pbuf_ref(struct pbuf *p)
758 {
759  SYS_ARCH_DECL_PROTECT(old_level);
760  /* pbuf given? */
761  if (p != NULL) {
762  SYS_ARCH_PROTECT(old_level);
763  ++(p->ref);
764  SYS_ARCH_UNPROTECT(old_level);
765  }
766 }
767 
778 void
779 pbuf_cat(struct pbuf *h, struct pbuf *t)
780 {
781  struct pbuf *p;
782 
783  LWIP_ERROR("(h != NULL) && (t != NULL) (programmer violates API)",
784  ((h != NULL) && (t != NULL)), return;);
785 
786  /* proceed to last pbuf of chain */
787  for (p = h; p->next != NULL; p = p->next) {
788  /* add total length of second chain to all totals of first chain */
789  p->tot_len += t->tot_len;
790  }
791  /* { p is last pbuf of first h chain, p->next == NULL } */
792  LWIP_ASSERT("p->tot_len == p->len (of last pbuf in chain)", p->tot_len == p->len);
793  LWIP_ASSERT("p->next == NULL", p->next == NULL);
794  /* add total length of second chain to last pbuf total of first chain */
795  p->tot_len += t->tot_len;
796  /* chain last pbuf of head (p) with first of tail (t) */
797  p->next = t;
798  /* p->next now references t, but the caller will drop its reference to t,
799  * so netto there is no change to the reference count of t.
800  */
801 }
802 
819 void
820 pbuf_chain(struct pbuf *h, struct pbuf *t)
821 {
822  pbuf_cat(h, t);
823  /* t is now referenced by h */
824  pbuf_ref(t);
825  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_chain: %p references %p\n", (void *)h, (void *)t));
826 }
827 
836 struct pbuf *
837 pbuf_dechain(struct pbuf *p)
838 {
839  struct pbuf *q;
840  u8_t tail_gone = 1;
841  /* tail */
842  q = p->next;
843  /* pbuf has successor in chain? */
844  if (q != NULL) {
845  /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
846  LWIP_ASSERT("p->tot_len == p->len + q->tot_len", q->tot_len == p->tot_len - p->len);
847  /* enforce invariant if assertion is disabled */
848  q->tot_len = p->tot_len - p->len;
849  /* decouple pbuf from remainder */
850  p->next = NULL;
851  /* total length of pbuf p is its own length only */
852  p->tot_len = p->len;
853  /* q is no longer referenced by p, free it */
854  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_dechain: unreferencing %p\n", (void *)q));
855  tail_gone = pbuf_free(q);
856  if (tail_gone > 0) {
858  ("pbuf_dechain: deallocated %p (as it is no longer referenced)\n", (void *)q));
859  }
860  /* return remaining tail or NULL if deallocated */
861  }
862  /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
863  LWIP_ASSERT("p->tot_len == p->len", p->tot_len == p->len);
864  return ((tail_gone > 0) ? NULL : q);
865 }
866 
885 err_t
886 pbuf_copy(struct pbuf *p_to, struct pbuf *p_from)
887 {
888  u16_t offset_to=0, offset_from=0, len;
889 
890  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy(%p, %p)\n",
891  (void*)p_to, (void*)p_from));
892 
893  /* is the target big enough to hold the source? */
894  LWIP_ERROR("pbuf_copy: target not big enough to hold source", ((p_to != NULL) &&
895  (p_from != NULL) && (p_to->tot_len >= p_from->tot_len)), return ERR_ARG;);
896 
897  /* iterate through pbuf chain */
898  do
899  {
900  /* copy one part of the original chain */
901  if ((p_to->len - offset_to) >= (p_from->len - offset_from)) {
902  /* complete current p_from fits into current p_to */
903  len = p_from->len - offset_from;
904  } else {
905  /* current p_from does not fit into current p_to */
906  len = p_to->len - offset_to;
907  }
908  MEMCPY((u8_t*)p_to->payload + offset_to, (u8_t*)p_from->payload + offset_from, len);
909  offset_to += len;
910  offset_from += len;
911  LWIP_ASSERT("offset_to <= p_to->len", offset_to <= p_to->len);
912  LWIP_ASSERT("offset_from <= p_from->len", offset_from <= p_from->len);
913  if (offset_from >= p_from->len) {
914  /* on to next p_from (if any) */
915  offset_from = 0;
916  p_from = p_from->next;
917  }
918  if (offset_to == p_to->len) {
919  /* on to next p_to (if any) */
920  offset_to = 0;
921  p_to = p_to->next;
922  LWIP_ERROR("p_to != NULL", (p_to != NULL) || (p_from == NULL) , return ERR_ARG;);
923  }
924 
925  if ((p_from != NULL) && (p_from->len == p_from->tot_len)) {
926  /* don't copy more than one packet! */
927  LWIP_ERROR("pbuf_copy() does not allow packet queues!",
928  (p_from->next == NULL), return ERR_VAL;);
929  }
930  if ((p_to != NULL) && (p_to->len == p_to->tot_len)) {
931  /* don't copy more than one packet! */
932  LWIP_ERROR("pbuf_copy() does not allow packet queues!",
933  (p_to->next == NULL), return ERR_VAL;);
934  }
935  } while (p_from);
936  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy: end of chain reached.\n"));
937  return ERR_OK;
938 }
939 
951 u16_t
952 pbuf_copy_partial(struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
953 {
954  struct pbuf *p;
955  u16_t left;
956  u16_t buf_copy_len;
957  u16_t copied_total = 0;
958 
959  LWIP_ERROR("pbuf_copy_partial: invalid buf", (buf != NULL), return 0;);
960  LWIP_ERROR("pbuf_copy_partial: invalid dataptr", (dataptr != NULL), return 0;);
961 
962  left = 0;
963 
964  if ((buf == NULL) || (dataptr == NULL)) {
965  return 0;
966  }
967 
968  /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
969  for (p = buf; len != 0 && p != NULL; p = p->next) {
970  if ((offset != 0) && (offset >= p->len)) {
971  /* don't copy from this buffer -> on to the next */
972  offset -= p->len;
973  } else {
974  /* copy from this buffer. maybe only partially. */
975  buf_copy_len = p->len - offset;
976  if (buf_copy_len > len)
977  buf_copy_len = len;
978  /* copy the necessary parts of the buffer */
979  MEMCPY(&((char*)dataptr)[left], &((char*)p->payload)[offset], buf_copy_len);
980  copied_total += buf_copy_len;
981  left += buf_copy_len;
982  len -= buf_copy_len;
983  offset = 0;
984  }
985  }
986  return copied_total;
987 }
988 
989 #if LWIP_TCP && TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
990 
1002 void pbuf_split_64k(struct pbuf *p, struct pbuf **rest)
1003 {
1004  *rest = NULL;
1005  if ((p != NULL) && (p->next != NULL)) {
1006  u16_t tot_len_front = p->len;
1007  struct pbuf *i = p;
1008  struct pbuf *r = p->next;
1009 
1010  /* continue until the total length (summed up as u16_t) overflows */
1011  while ((r != NULL) && ((u16_t)(tot_len_front + r->len) > tot_len_front)) {
1012  tot_len_front += r->len;
1013  i = r;
1014  r = r->next;
1015  }
1016  /* i now points to last packet of the first segment. Set next
1017  pointer to NULL */
1018  i->next = NULL;
1019 
1020  if (r != NULL) {
1021  /* Update the tot_len field in the first part */
1022  for (i = p; i != NULL; i = i->next) {
1023  i->tot_len -= r->tot_len;
1024  LWIP_ASSERT("tot_len/len mismatch in last pbuf",
1025  (i->next != NULL) || (i->tot_len == i->len));
1026  }
1027  if (p->flags & PBUF_FLAG_TCP_FIN) {
1028  r->flags |= PBUF_FLAG_TCP_FIN;
1029  }
1030 
1031  /* tot_len field in rest does not need modifications */
1032  /* reference counters do not need modifications */
1033  *rest = r;
1034  }
1035  }
1036 }
1037 #endif /* LWIP_TCP && TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
1038 
1047 struct pbuf*
1048 pbuf_skip(struct pbuf* in, u16_t in_offset, u16_t* out_offset)
1049 {
1050  u16_t offset_left = in_offset;
1051  struct pbuf* q = in;
1052 
1053  /* get the correct pbuf */
1054  while ((q != NULL) && (q->len <= offset_left)) {
1055  offset_left -= q->len;
1056  q = q->next;
1057  }
1058  if (out_offset != NULL) {
1059  *out_offset = offset_left;
1060  }
1061  return q;
1062 }
1063 
1074 err_t
1075 pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)
1076 {
1077  struct pbuf *p;
1078  u16_t buf_copy_len;
1079  u16_t total_copy_len = len;
1080  u16_t copied_total = 0;
1081 
1082  LWIP_ERROR("pbuf_take: invalid buf", (buf != NULL), return ERR_ARG;);
1083  LWIP_ERROR("pbuf_take: invalid dataptr", (dataptr != NULL), return ERR_ARG;);
1084  LWIP_ERROR("pbuf_take: buf not large enough", (buf->tot_len >= len), return ERR_MEM;);
1085 
1086  if ((buf == NULL) || (dataptr == NULL) || (buf->tot_len < len)) {
1087  return ERR_ARG;
1088  }
1089 
1090  /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
1091  for (p = buf; total_copy_len != 0; p = p->next) {
1092  LWIP_ASSERT("pbuf_take: invalid pbuf", p != NULL);
1093  buf_copy_len = total_copy_len;
1094  if (buf_copy_len > p->len) {
1095  /* this pbuf cannot hold all remaining data */
1096  buf_copy_len = p->len;
1097  }
1098  /* copy the necessary parts of the buffer */
1099  MEMCPY(p->payload, &((const char*)dataptr)[copied_total], buf_copy_len);
1100  total_copy_len -= buf_copy_len;
1101  copied_total += buf_copy_len;
1102  }
1103  LWIP_ASSERT("did not copy all data", total_copy_len == 0 && copied_total == len);
1104  return ERR_OK;
1105 }
1106 
1116 err_t
1117 pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset)
1118 {
1119  u16_t target_offset;
1120  struct pbuf* q = pbuf_skip(buf, offset, &target_offset);
1121 
1122  /* return requested data if pbuf is OK */
1123  if ((q != NULL) && (q->tot_len >= target_offset + len)) {
1124  u16_t remaining_len = len;
1125  const u8_t* src_ptr = (const u8_t*)dataptr;
1126  /* copy the part that goes into the first pbuf */
1127  u16_t first_copy_len = LWIP_MIN(q->len - target_offset, len);
1128  MEMCPY(((u8_t*)q->payload) + target_offset, dataptr, first_copy_len);
1129  remaining_len -= first_copy_len;
1130  src_ptr += first_copy_len;
1131  if (remaining_len > 0) {
1132  return pbuf_take(q->next, src_ptr, remaining_len);
1133  }
1134  return ERR_OK;
1135  }
1136  return ERR_MEM;
1137 }
1138 
1151 struct pbuf*
1152 pbuf_coalesce(struct pbuf *p, pbuf_layer layer)
1153 {
1154  struct pbuf *q;
1155  err_t err;
1156  if (p->next == NULL) {
1157  return p;
1158  }
1159  q = pbuf_alloc(layer, p->tot_len, PBUF_RAM);
1160  if (q == NULL) {
1161  /* @todo: what do we do now? */
1162  return p;
1163  }
1164  err = pbuf_copy(q, p);
1165  LWIP_ASSERT("pbuf_copy failed", err == ERR_OK);
1166  pbuf_free(p);
1167  return q;
1168 }
1169 
1170 #if LWIP_CHECKSUM_ON_COPY
1171 
1183 err_t
1184 pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr,
1185  u16_t len, u16_t *chksum)
1186 {
1187  u32_t acc;
1188  u16_t copy_chksum;
1189  char *dst_ptr;
1190  LWIP_ASSERT("p != NULL", p != NULL);
1191  LWIP_ASSERT("dataptr != NULL", dataptr != NULL);
1192  LWIP_ASSERT("chksum != NULL", chksum != NULL);
1193  LWIP_ASSERT("len != 0", len != 0);
1194 
1195  if ((start_offset >= p->len) || (start_offset + len > p->len)) {
1196  return ERR_ARG;
1197  }
1198 
1199  dst_ptr = ((char*)p->payload) + start_offset;
1200  copy_chksum = LWIP_CHKSUM_COPY(dst_ptr, dataptr, len);
1201  if ((start_offset & 1) != 0) {
1202  copy_chksum = SWAP_BYTES_IN_WORD(copy_chksum);
1203  }
1204  acc = *chksum;
1205  acc += copy_chksum;
1206  *chksum = FOLD_U32T(acc);
1207  return ERR_OK;
1208 }
1209 #endif /* LWIP_CHECKSUM_ON_COPY */
1210 
1218 u8_t
1219 pbuf_get_at(struct pbuf* p, u16_t offset)
1220 {
1221  u16_t q_idx;
1222  struct pbuf* q = pbuf_skip(p, offset, &q_idx);
1223 
1224  /* return requested data if pbuf is OK */
1225  if ((q != NULL) && (q->len > q_idx)) {
1226  return ((u8_t*)q->payload)[q_idx];
1227  }
1228  return 0;
1229 }
1230 
1238 void
1239 pbuf_put_at(struct pbuf* p, u16_t offset, u8_t data)
1240 {
1241  u16_t q_idx;
1242  struct pbuf* q = pbuf_skip(p, offset, &q_idx);
1243 
1244  /* write requested data if pbuf is OK */
1245  if ((q != NULL) && (q->len > q_idx)) {
1246  ((u8_t*)q->payload)[q_idx] = data;
1247  }
1248 }
1249 
1259 u16_t
1260 pbuf_memcmp(struct pbuf* p, u16_t offset, const void* s2, u16_t n)
1261 {
1262  u16_t start = offset;
1263  struct pbuf* q = p;
1264 
1265  /* get the correct pbuf */
1266  while ((q != NULL) && (q->len <= start)) {
1267  start -= q->len;
1268  q = q->next;
1269  }
1270  /* return requested data if pbuf is OK */
1271  if ((q != NULL) && (q->len > start)) {
1272  u16_t i;
1273  for (i = 0; i < n; i++) {
1274  u8_t a = pbuf_get_at(q, start + i);
1275  u8_t b = ((const u8_t*)s2)[i];
1276  if (a != b) {
1277  return i+1;
1278  }
1279  }
1280  return 0;
1281  }
1282  return 0xffff;
1283 }
1284 
1295 u16_t
1296 pbuf_memfind(struct pbuf* p, const void* mem, u16_t mem_len, u16_t start_offset)
1297 {
1298  u16_t i;
1299  u16_t max = p->tot_len - mem_len;
1300  if (p->tot_len >= mem_len + start_offset) {
1301  for (i = start_offset; i <= max; i++) {
1302  u16_t plus = pbuf_memcmp(p, i, mem, mem_len);
1303  if (plus == 0) {
1304  return i;
1305  }
1306  }
1307  }
1308  return 0xFFFF;
1309 }
1310 
1321 u16_t
1322 pbuf_strstr(struct pbuf* p, const char* substr)
1323 {
1324  size_t substr_len;
1325  if ((substr == NULL) || (substr[0] == 0) || (p->tot_len == 0xFFFF)) {
1326  return 0xFFFF;
1327  }
1328  substr_len = strlen(substr);
1329  if (substr_len >= 0xFFFF) {
1330  return 0xFFFF;
1331  }
1332  return pbuf_memfind(p, substr, (u16_t)substr_len, 0);
1333 }
1334 
struct pbuf * pbuf_skip(struct pbuf *in, u16_t in_offset, u16_t *out_offset)
Definition: pbuf.c:1048
#define SYS_ARCH_SET(var, val)
Definition: sys.h:345
#define LWIP_DBG_LEVEL_SERIOUS
Definition: debug.h:47
void mem_free(void *rmem)
Definition: mem.c:334
#define LWIP_DBG_LEVEL_WARNING
Definition: debug.h:46
#define U16_F
Definition: cc.h:48
signed short s16_t
Definition: cc.h:41
#define FOLD_U32T(u)
Definition: inet_chksum.h:53
void pbuf_put_at(struct pbuf *p, u16_t offset, u8_t data)
Definition: pbuf.c:1239
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
Definition: pbuf.c:603
void pbuf_realloc(struct pbuf *p, u16_t new_len)
Definition: pbuf.c:431
#define ERR_VAL
Definition: err.h:58
u16_t ref
Definition: pbuf.h:138
#define PERF_STOP(x)
Definition: def.h:42
#define LWIP_SUPPORT_CUSTOM_PBUF
Definition: pbuf.h:50
#define PBUF_IP_HLEN
Definition: pbuf.h:60
u16_t pbuf_memcmp(struct pbuf *p, u16_t offset, const void *s2, u16_t n)
Definition: pbuf.c:1260
#define MEMCPY(dst, src, len)
Definition: opt.h:84
if(LCD_Lock==DISABLE)
Definition: lcd_log.c:249
void memp_free(memp_t type, void *mem)
Definition: memp.c:399
u16_t len
Definition: pbuf.h:125
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:199
Definition: pbuf.h:77
u32_t mem_ptr_t
Definition: cc.h:44
#define MEM_ALIGNMENT
Definition: lwipopts.h:73
#define SYS_ARCH_DECL_PROTECT(lev)
Definition: sys.h:304
Definition: pbuf.h:66
u8_t flags
Definition: pbuf.h:131
#define PBUF_DEBUG
Definition: opt.h:2788
#define LWIP_MIN(x, y)
Definition: def.h:50
void * mem_trim(void *rmem, mem_size_t newsize)
Definition: mem.c:392
#define PERF_START
Definition: def.h:41
#define NULL
Definition: usbd_def.h:53
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:652
Definition: pbuf.h:68
err_t pbuf_copy(struct pbuf *p_to, struct pbuf *p_from)
Definition: pbuf.c:886
#define SIZEOF_STRUCT_PBUF
Definition: pbuf.c:81
#define PBUF_TRANSPORT_HLEN
Definition: pbuf.h:56
u16_t pbuf_memfind(struct pbuf *p, const void *mem, u16_t mem_len, u16_t start_offset)
Definition: pbuf.c:1296
unsigned long u32_t
Definition: cc.h:42
err_t pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)
Definition: pbuf.c:1075
#define PBUF_POOL_BUFSIZE_ALIGNED
Definition: pbuf.c:84
void pbuf_chain(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:820
#define PBUF_LINK_ENCAPSULATION_HLEN
Definition: opt.h:1193
#define ERR_OK
Definition: err.h:52
u16_t tot_len
Definition: pbuf.h:122
#define LWIP_DBG_TRACE
Definition: debug.h:57
Definition: pbuf.h:108
s8_t err_t
Definition: err.h:47
Definition: pbuf.h:90
#define SWAP_BYTES_IN_WORD(w)
Definition: inet_chksum.h:47
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:70
Definition: pbuf.h:81
struct pbuf * next
Definition: pbuf.h:110
struct pbuf * pbuf_dechain(struct pbuf *p)
Definition: pbuf.c:837
void pbuf_ref(struct pbuf *p)
Definition: pbuf.c:757
pbuf_type
Definition: pbuf.h:71
#define S16_F
Definition: cc.h:49
#define ERR_ARG
Definition: err.h:71
u8_t type
Definition: pbuf.h:128
#define PBUF_FLAG_IS_CUSTOM
Definition: pbuf.h:98
u16_t pbuf_copy_partial(struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
Definition: pbuf.c:952
#define PBUF_FLAG_TCP_FIN
Definition: pbuf.h:106
u8_t pbuf_clen(struct pbuf *p)
Definition: pbuf.c:738
u8_t pbuf_header_force(struct pbuf *p, s16_t header_size_increment)
Definition: pbuf.c:613
void pbuf_cat(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:779
Definition: pbuf.h:85
u8_t pbuf_get_at(struct pbuf *p, u16_t offset)
Definition: pbuf.c:1219
unsigned char u8_t
Definition: cc.h:38
#define PBUF_POOL_IS_EMPTY()
Definition: pbuf.c:87
#define SYS_ARCH_PROTECT(lev)
Definition: sys.h:305
#define LWIP_MEM_ALIGN(addr)
Definition: mem.h:116
#define SYS_ARCH_UNPROTECT(lev)
Definition: sys.h:306
Definition: mem.c:179
void * mem_malloc(mem_size_t size)
Definition: mem.c:517
Definition: pbuf.h:65
#define LWIP_ERROR(message, expression, handler)
Definition: debug.h:89
#define LWIP_MEM_ALIGN_SIZE(size)
Definition: mem.h:101
signed long s32_t
Definition: cc.h:43
pbuf_layer
Definition: pbuf.h:63
void * memp_malloc(memp_t type)
Definition: memp.c:303
u16_t pbuf_strstr(struct pbuf *p, const char *substr)
Definition: pbuf.c:1322
#define PBUF_LINK_HLEN
Definition: opt.h:1184
err_t pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset)
Definition: pbuf.c:1117
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:113
#define ERR_MEM
Definition: err.h:53
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:89
struct pbuf * pbuf_coalesce(struct pbuf *p, pbuf_layer layer)
Definition: pbuf.c:1152
unsigned short u16_t
Definition: cc.h:40
void * payload
Definition: pbuf.h:113