pbuf.c 30 KB

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