pbuf.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  1. /**
  2. * @file
  3. * Packet buffer management
  4. */
  5. /**
  6. * @defgroup pbuf Packet buffers (PBUF)
  7. * @ingroup infrastructure
  8. *
  9. * Packets are built from the pbuf data structure. It supports dynamic
  10. * memory allocation for packet contents or can reference externally
  11. * managed packet contents both in RAM and ROM. Quick allocation for
  12. * incoming packets is provided through pools with fixed sized pbufs.
  13. *
  14. * A packet may span over multiple pbufs, chained as a singly linked
  15. * list. This is called a "pbuf chain".
  16. *
  17. * Multiple packets may be queued, also using this singly linked list.
  18. * This is called a "packet queue".
  19. *
  20. * So, a packet queue consists of one or more pbuf chains, each of
  21. * which consist of one or more pbufs. CURRENTLY, PACKET QUEUES ARE
  22. * NOT SUPPORTED!!! Use helper structs to queue multiple packets.
  23. *
  24. * The differences between a pbuf chain and a packet queue are very
  25. * precise but subtle.
  26. *
  27. * The last pbuf of a packet has a ->tot_len field that equals the
  28. * ->len field. It can be found by traversing the list. If the last
  29. * pbuf of a packet has a ->next field other than NULL, more packets
  30. * are on the queue.
  31. *
  32. * Therefore, looping through a pbuf of a single packet, has an
  33. * loop end condition (tot_len == p->len), NOT (next == NULL).
  34. *
  35. * Example of custom pbuf usage for zero-copy RX:
  36. @code{.c}
  37. typedef struct my_custom_pbuf
  38. {
  39. struct pbuf_custom p;
  40. void* dma_descriptor;
  41. } my_custom_pbuf_t;
  42. LWIP_MEMPOOL_DECLARE(RX_POOL, 10, sizeof(my_custom_pbuf_t), "Zero-copy RX PBUF pool");
  43. void my_pbuf_free_custom(void* p)
  44. {
  45. my_custom_pbuf_t* my_puf = (my_custom_pbuf_t*)p;
  46. LOCK_INTERRUPTS();
  47. free_rx_dma_descriptor(my_pbuf->dma_descriptor);
  48. LWIP_MEMPOOL_FREE(RX_POOL, my_pbuf);
  49. UNLOCK_INTERRUPTS();
  50. }
  51. void eth_rx_irq()
  52. {
  53. dma_descriptor* dma_desc = get_RX_DMA_descriptor_from_ethernet();
  54. my_custom_pbuf_t* my_pbuf = (my_custom_pbuf_t*)LWIP_MEMPOOL_ALLOC(RX_POOL);
  55. my_pbuf->p.custom_free_function = my_pbuf_free_custom;
  56. my_pbuf->dma_descriptor = dma_desc;
  57. invalidate_cpu_cache(dma_desc->rx_data, dma_desc->rx_length);
  58. struct pbuf* p = pbuf_alloced_custom(PBUF_RAW,
  59. dma_desc->rx_length,
  60. PBUF_REF,
  61. &my_pbuf->p,
  62. dma_desc->rx_data,
  63. dma_desc->max_buffer_size);
  64. if(netif->input(p, netif) != ERR_OK) {
  65. pbuf_free(p);
  66. }
  67. }
  68. @endcode
  69. */
  70. /*
  71. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  72. * All rights reserved.
  73. *
  74. * Redistribution and use in source and binary forms, with or without modification,
  75. * are permitted provided that the following conditions are met:
  76. *
  77. * 1. Redistributions of source code must retain the above copyright notice,
  78. * this list of conditions and the following disclaimer.
  79. * 2. Redistributions in binary form must reproduce the above copyright notice,
  80. * this list of conditions and the following disclaimer in the documentation
  81. * and/or other materials provided with the distribution.
  82. * 3. The name of the author may not be used to endorse or promote products
  83. * derived from this software without specific prior written permission.
  84. *
  85. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  86. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  87. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  88. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  89. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  90. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  91. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  92. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  93. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  94. * OF SUCH DAMAGE.
  95. *
  96. * This file is part of the lwIP TCP/IP stack.
  97. *
  98. * Author: Adam Dunkels <adam@sics.se>
  99. *
  100. */
  101. #include "lwip/opt.h"
  102. #include "lwip/stats.h"
  103. #include "lwip/def.h"
  104. #include "lwip/mem.h"
  105. #include "lwip/memp.h"
  106. #include "lwip/pbuf.h"
  107. #include "lwip/sys.h"
  108. #if LWIP_TCP && TCP_QUEUE_OOSEQ
  109. #include "lwip/priv/tcp_priv.h"
  110. #endif
  111. #if LWIP_CHECKSUM_ON_COPY
  112. #include "lwip/inet_chksum.h"
  113. #endif
  114. #include <string.h>
  115. #define SIZEOF_STRUCT_PBUF LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf))
  116. /* Since the pool is created in memp, PBUF_POOL_BUFSIZE will be automatically
  117. aligned there. Therefore, PBUF_POOL_BUFSIZE_ALIGNED can be used here. */
  118. #define PBUF_POOL_BUFSIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE)
  119. #if !LWIP_TCP || !TCP_QUEUE_OOSEQ || !PBUF_POOL_FREE_OOSEQ
  120. #define PBUF_POOL_IS_EMPTY()
  121. #else /* !LWIP_TCP || !TCP_QUEUE_OOSEQ || !PBUF_POOL_FREE_OOSEQ */
  122. #if !NO_SYS
  123. #ifndef PBUF_POOL_FREE_OOSEQ_QUEUE_CALL
  124. #include "lwip/tcpip.h"
  125. #define PBUF_POOL_FREE_OOSEQ_QUEUE_CALL() do { \
  126. if (tcpip_callback_with_block(pbuf_free_ooseq_callback, NULL, 0) != ERR_OK) { \
  127. SYS_ARCH_PROTECT(old_level); \
  128. pbuf_free_ooseq_pending = 0; \
  129. SYS_ARCH_UNPROTECT(old_level); \
  130. } } while(0)
  131. #endif /* PBUF_POOL_FREE_OOSEQ_QUEUE_CALL */
  132. #endif /* !NO_SYS */
  133. volatile u8_t pbuf_free_ooseq_pending;
  134. #define PBUF_POOL_IS_EMPTY() pbuf_pool_is_empty()
  135. /**
  136. * Attempt to reclaim some memory from queued out-of-sequence TCP segments
  137. * if we run out of pool pbufs. It's better to give priority to new packets
  138. * if we're running out.
  139. *
  140. * This must be done in the correct thread context therefore this function
  141. * can only be used with NO_SYS=0 and through tcpip_callback.
  142. */
  143. #if !NO_SYS
  144. static
  145. #endif /* !NO_SYS */
  146. void
  147. pbuf_free_ooseq(void)
  148. {
  149. struct tcp_pcb* pcb;
  150. SYS_ARCH_SET(pbuf_free_ooseq_pending, 0);
  151. for (pcb = tcp_active_pcbs; NULL != pcb; pcb = pcb->next) {
  152. if (NULL != pcb->ooseq) {
  153. /** Free the ooseq pbufs of one PCB only */
  154. LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free_ooseq: freeing out-of-sequence pbufs\n"));
  155. tcp_segs_free(pcb->ooseq);
  156. pcb->ooseq = NULL;
  157. return;
  158. }
  159. }
  160. }
  161. #if !NO_SYS
  162. /**
  163. * Just a callback function for tcpip_callback() that calls pbuf_free_ooseq().
  164. */
  165. static void
  166. pbuf_free_ooseq_callback(void *arg)
  167. {
  168. LWIP_UNUSED_ARG(arg);
  169. pbuf_free_ooseq();
  170. }
  171. #endif /* !NO_SYS */
  172. /** Queue a call to pbuf_free_ooseq if not already queued. */
  173. static void
  174. pbuf_pool_is_empty(void)
  175. {
  176. #ifndef PBUF_POOL_FREE_OOSEQ_QUEUE_CALL
  177. SYS_ARCH_SET(pbuf_free_ooseq_pending, 1);
  178. #else /* PBUF_POOL_FREE_OOSEQ_QUEUE_CALL */
  179. u8_t queued;
  180. SYS_ARCH_DECL_PROTECT(old_level);
  181. SYS_ARCH_PROTECT(old_level);
  182. queued = pbuf_free_ooseq_pending;
  183. pbuf_free_ooseq_pending = 1;
  184. SYS_ARCH_UNPROTECT(old_level);
  185. if (!queued) {
  186. /* queue a call to pbuf_free_ooseq if not already queued */
  187. PBUF_POOL_FREE_OOSEQ_QUEUE_CALL();
  188. }
  189. #endif /* PBUF_POOL_FREE_OOSEQ_QUEUE_CALL */
  190. }
  191. #endif /* !LWIP_TCP || !TCP_QUEUE_OOSEQ || !PBUF_POOL_FREE_OOSEQ */
  192. /**
  193. * @ingroup pbuf
  194. * Allocates a pbuf of the given type (possibly a chain for PBUF_POOL type).
  195. *
  196. * The actual memory allocated for the pbuf is determined by the
  197. * layer at which the pbuf is allocated and the requested size
  198. * (from the size parameter).
  199. *
  200. * @param layer flag to define header size
  201. * @param length size of the pbuf's payload
  202. * @param type this parameter decides how and where the pbuf
  203. * should be allocated as follows:
  204. *
  205. * - PBUF_RAM: buffer memory for pbuf is allocated as one large
  206. * chunk. This includes protocol headers as well.
  207. * - PBUF_ROM: no buffer memory is allocated for the pbuf, even for
  208. * protocol headers. Additional headers must be prepended
  209. * by allocating another pbuf and chain in to the front of
  210. * the ROM pbuf. It is assumed that the memory used is really
  211. * similar to ROM in that it is immutable and will not be
  212. * changed. Memory which is dynamic should generally not
  213. * be attached to PBUF_ROM pbufs. Use PBUF_REF instead.
  214. * - PBUF_REF: no buffer memory is allocated for the pbuf, even for
  215. * protocol headers. It is assumed that the pbuf is only
  216. * being used in a single thread. If the pbuf gets queued,
  217. * then pbuf_take should be called to copy the buffer.
  218. * - PBUF_POOL: the pbuf is allocated as a pbuf chain, with pbufs from
  219. * the pbuf pool that is allocated during pbuf_init().
  220. *
  221. * @return the allocated pbuf. If multiple pbufs where allocated, this
  222. * is the first pbuf of a pbuf chain.
  223. */
  224. struct pbuf *
  225. pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
  226. {
  227. struct pbuf *p, *q, *r;
  228. u16_t offset;
  229. s32_t rem_len; /* remaining length */
  230. LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F")\n", length));
  231. /* determine header offset */
  232. switch (layer) {
  233. case PBUF_TRANSPORT:
  234. /* add room for transport (often TCP) layer header */
  235. offset = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN;
  236. break;
  237. case PBUF_IP:
  238. /* add room for IP layer header */
  239. offset = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN;
  240. break;
  241. case PBUF_LINK:
  242. /* add room for link layer header */
  243. offset = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN;
  244. break;
  245. case PBUF_RAW_TX:
  246. /* add room for encapsulating link layer headers (e.g. 802.11) */
  247. offset = PBUF_LINK_ENCAPSULATION_HLEN;
  248. break;
  249. case PBUF_RAW:
  250. /* no offset (e.g. RX buffers or chain successors) */
  251. offset = 0;
  252. break;
  253. default:
  254. LWIP_ASSERT("pbuf_alloc: bad pbuf layer", 0);
  255. return NULL;
  256. }
  257. switch (type) {
  258. case PBUF_POOL:
  259. /* allocate head of pbuf chain into p */
  260. p = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
  261. LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc: allocated pbuf %p\n", (void *)p));
  262. if (p == NULL) {
  263. PBUF_POOL_IS_EMPTY();
  264. return NULL;
  265. }
  266. p->type = type;
  267. p->next = NULL;
  268. /* make the payload pointer point 'offset' bytes into pbuf data memory */
  269. p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + (SIZEOF_STRUCT_PBUF + offset)));
  270. LWIP_ASSERT("pbuf_alloc: pbuf p->payload properly aligned",
  271. ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
  272. /* the total length of the pbuf chain is the requested size */
  273. p->tot_len = length;
  274. /* set the length of the first pbuf in the chain */
  275. p->len = LWIP_MIN(length, PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset));
  276. LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",
  277. ((u8_t*)p->payload + p->len <=
  278. (u8_t*)p + SIZEOF_STRUCT_PBUF + PBUF_POOL_BUFSIZE_ALIGNED));
  279. LWIP_ASSERT("PBUF_POOL_BUFSIZE must be bigger than MEM_ALIGNMENT",
  280. (PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)) > 0 );
  281. /* set reference count (needed here in case we fail) */
  282. p->ref = 1;
  283. /* now allocate the tail of the pbuf chain */
  284. /* remember first pbuf for linkage in next iteration */
  285. r = p;
  286. /* remaining length to be allocated */
  287. rem_len = length - p->len;
  288. /* any remaining pbufs to be allocated? */
  289. while (rem_len > 0) {
  290. q = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
  291. if (q == NULL) {
  292. PBUF_POOL_IS_EMPTY();
  293. /* free chain so far allocated */
  294. pbuf_free(p);
  295. /* bail out unsuccessfully */
  296. return NULL;
  297. }
  298. q->type = type;
  299. q->flags = 0;
  300. q->next = NULL;
  301. /* make previous pbuf point to this pbuf */
  302. r->next = q;
  303. /* set total length of this pbuf and next in chain */
  304. LWIP_ASSERT("rem_len < max_u16_t", rem_len < 0xffff);
  305. q->tot_len = (u16_t)rem_len;
  306. /* this pbuf length is pool size, unless smaller sized tail */
  307. q->len = LWIP_MIN((u16_t)rem_len, PBUF_POOL_BUFSIZE_ALIGNED);
  308. q->payload = (void *)((u8_t *)q + SIZEOF_STRUCT_PBUF);
  309. LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned",
  310. ((mem_ptr_t)q->payload % MEM_ALIGNMENT) == 0);
  311. LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",
  312. ((u8_t*)p->payload + p->len <=
  313. (u8_t*)p + SIZEOF_STRUCT_PBUF + PBUF_POOL_BUFSIZE_ALIGNED));
  314. q->ref = 1;
  315. /* calculate remaining length to be allocated */
  316. rem_len -= q->len;
  317. /* remember this pbuf for linkage in next iteration */
  318. r = q;
  319. }
  320. /* end of chain */
  321. /*r->next = NULL;*/
  322. break;
  323. case PBUF_RAM:
  324. {
  325. mem_size_t alloc_len = LWIP_MEM_ALIGN_SIZE(SIZEOF_STRUCT_PBUF + offset) + LWIP_MEM_ALIGN_SIZE(length);
  326. /* bug #50040: Check for integer overflow when calculating alloc_len */
  327. if (alloc_len < LWIP_MEM_ALIGN_SIZE(length)) {
  328. return NULL;
  329. }
  330. /* If pbuf is to be allocated in RAM, allocate memory for it. */
  331. p = (struct pbuf*)mem_malloc(alloc_len);
  332. }
  333. if (p == NULL) {
  334. return NULL;
  335. }
  336. /* Set up internal structure of the pbuf. */
  337. p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + SIZEOF_STRUCT_PBUF + offset));
  338. p->len = p->tot_len = length;
  339. p->next = NULL;
  340. p->type = type;
  341. LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned",
  342. ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
  343. break;
  344. /* pbuf references existing (non-volatile static constant) ROM payload? */
  345. case PBUF_ROM:
  346. /* pbuf references existing (externally allocated) RAM payload? */
  347. case PBUF_REF:
  348. /* only allocate memory for the pbuf structure */
  349. p = (struct pbuf *)memp_malloc(MEMP_PBUF);
  350. if (p == NULL) {
  351. LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
  352. ("pbuf_alloc: Could not allocate MEMP_PBUF for PBUF_%s.\n",
  353. (type == PBUF_ROM) ? "ROM" : "REF"));
  354. return NULL;
  355. }
  356. /* caller must set this field properly, afterwards */
  357. p->payload = NULL;
  358. p->len = p->tot_len = length;
  359. p->next = NULL;
  360. p->type = type;
  361. break;
  362. default:
  363. LWIP_ASSERT("pbuf_alloc: erroneous type", 0);
  364. return NULL;
  365. }
  366. /* set reference count */
  367. p->ref = 1;
  368. /* set flags */
  369. p->flags = 0;
  370. LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F") == %p\n", length, (void *)p));
  371. return p;
  372. }
  373. #if LWIP_SUPPORT_CUSTOM_PBUF
  374. /**
  375. * @ingroup pbuf
  376. * Initialize a custom pbuf (already allocated).
  377. *
  378. * @param l flag to define header size
  379. * @param length size of the pbuf's payload
  380. * @param type type of the pbuf (only used to treat the pbuf accordingly, as
  381. * this function allocates no memory)
  382. * @param p pointer to the custom pbuf to initialize (already allocated)
  383. * @param payload_mem pointer to the buffer that is used for payload and headers,
  384. * must be at least big enough to hold 'length' plus the header size,
  385. * may be NULL if set later.
  386. * ATTENTION: The caller is responsible for correct alignment of this buffer!!
  387. * @param payload_mem_len the size of the 'payload_mem' buffer, must be at least
  388. * big enough to hold 'length' plus the header size
  389. */
  390. struct pbuf*
  391. pbuf_alloced_custom(pbuf_layer l, u16_t length, pbuf_type type, struct pbuf_custom *p,
  392. void *payload_mem, u16_t payload_mem_len)
  393. {
  394. u16_t offset;
  395. LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloced_custom(length=%"U16_F")\n", length));
  396. /* determine header offset */
  397. switch (l) {
  398. case PBUF_TRANSPORT:
  399. /* add room for transport (often TCP) layer header */
  400. offset = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN;
  401. break;
  402. case PBUF_IP:
  403. /* add room for IP layer header */
  404. offset = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN;
  405. break;
  406. case PBUF_LINK:
  407. /* add room for link layer header */
  408. offset = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN;
  409. break;
  410. case PBUF_RAW_TX:
  411. /* add room for encapsulating link layer headers (e.g. 802.11) */
  412. offset = PBUF_LINK_ENCAPSULATION_HLEN;
  413. break;
  414. case PBUF_RAW:
  415. offset = 0;
  416. break;
  417. default:
  418. LWIP_ASSERT("pbuf_alloced_custom: bad pbuf layer", 0);
  419. return NULL;
  420. }
  421. if (LWIP_MEM_ALIGN_SIZE(offset) + length > payload_mem_len) {
  422. LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_WARNING, ("pbuf_alloced_custom(length=%"U16_F") buffer too short\n", length));
  423. return NULL;
  424. }
  425. p->pbuf.next = NULL;
  426. if (payload_mem != NULL) {
  427. p->pbuf.payload = (u8_t *)payload_mem + LWIP_MEM_ALIGN_SIZE(offset);
  428. } else {
  429. p->pbuf.payload = NULL;
  430. }
  431. p->pbuf.flags = PBUF_FLAG_IS_CUSTOM;
  432. p->pbuf.len = p->pbuf.tot_len = length;
  433. p->pbuf.type = type;
  434. p->pbuf.ref = 1;
  435. return &p->pbuf;
  436. }
  437. #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
  438. /**
  439. * @ingroup pbuf
  440. * Shrink a pbuf chain to a desired length.
  441. *
  442. * @param p pbuf to shrink.
  443. * @param new_len desired new length of pbuf chain
  444. *
  445. * Depending on the desired length, the first few pbufs in a chain might
  446. * be skipped and left unchanged. The new last pbuf in the chain will be
  447. * resized, and any remaining pbufs will be freed.
  448. *
  449. * @note If the pbuf is ROM/REF, only the ->tot_len and ->len fields are adjusted.
  450. * @note May not be called on a packet queue.
  451. *
  452. * @note Despite its name, pbuf_realloc cannot grow the size of a pbuf (chain).
  453. */
  454. void
  455. pbuf_realloc(struct pbuf *p, u16_t new_len)
  456. {
  457. struct pbuf *q;
  458. u16_t rem_len; /* remaining length */
  459. s32_t grow;
  460. LWIP_ASSERT("pbuf_realloc: p != NULL", p != NULL);
  461. LWIP_ASSERT("pbuf_realloc: sane p->type", p->type == PBUF_POOL ||
  462. p->type == PBUF_ROM ||
  463. p->type == PBUF_RAM ||
  464. p->type == PBUF_REF);
  465. /* desired length larger than current length? */
  466. if (new_len >= p->tot_len) {
  467. /* enlarging not yet supported */
  468. return;
  469. }
  470. /* the pbuf chain grows by (new_len - p->tot_len) bytes
  471. * (which may be negative in case of shrinking) */
  472. grow = new_len - p->tot_len;
  473. /* first, step over any pbufs that should remain in the chain */
  474. rem_len = new_len;
  475. q = p;
  476. /* should this pbuf be kept? */
  477. while (rem_len > q->len) {
  478. /* decrease remaining length by pbuf length */
  479. rem_len -= q->len;
  480. /* decrease total length indicator */
  481. LWIP_ASSERT("grow < max_u16_t", grow < 0xffff);
  482. q->tot_len += (u16_t)grow;
  483. /* proceed to next pbuf in chain */
  484. q = q->next;
  485. LWIP_ASSERT("pbuf_realloc: q != NULL", q != NULL);
  486. }
  487. /* we have now reached the new last pbuf (in q) */
  488. /* rem_len == desired length for pbuf q */
  489. /* shrink allocated memory for PBUF_RAM */
  490. /* (other types merely adjust their length fields */
  491. if ((q->type == PBUF_RAM) && (rem_len != q->len)
  492. #if LWIP_SUPPORT_CUSTOM_PBUF
  493. && ((q->flags & PBUF_FLAG_IS_CUSTOM) == 0)
  494. #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
  495. ) {
  496. /* reallocate and adjust the length of the pbuf that will be split */
  497. q = (struct pbuf *)mem_trim(q, (u16_t)((u8_t *)q->payload - (u8_t *)q) + rem_len);
  498. LWIP_ASSERT("mem_trim returned q == NULL", q != NULL);
  499. }
  500. /* adjust length fields for new last pbuf */
  501. q->len = rem_len;
  502. q->tot_len = q->len;
  503. /* any remaining pbufs in chain? */
  504. if (q->next != NULL) {
  505. /* free remaining pbufs in chain */
  506. pbuf_free(q->next);
  507. }
  508. /* q is last packet in chain */
  509. q->next = NULL;
  510. }
  511. /**
  512. * Adjusts the payload pointer to hide or reveal headers in the payload.
  513. * @see pbuf_header.
  514. *
  515. * @param p pbuf to change the header size.
  516. * @param header_size_increment Number of bytes to increment header size.
  517. * @param force Allow 'header_size_increment > 0' for PBUF_REF/PBUF_ROM types
  518. *
  519. * @return non-zero on failure, zero on success.
  520. *
  521. */
  522. static u8_t
  523. pbuf_header_impl(struct pbuf *p, s16_t header_size_increment, u8_t force)
  524. {
  525. u16_t type;
  526. void *payload;
  527. u16_t increment_magnitude;
  528. LWIP_ASSERT("p != NULL", p != NULL);
  529. if ((header_size_increment == 0) || (p == NULL)) {
  530. return 0;
  531. }
  532. if (header_size_increment < 0) {
  533. increment_magnitude = (u16_t)-header_size_increment;
  534. /* Check that we aren't going to move off the end of the pbuf */
  535. LWIP_ERROR("increment_magnitude <= p->len", (increment_magnitude <= p->len), return 1;);
  536. } else {
  537. increment_magnitude = (u16_t)header_size_increment;
  538. #if 0
  539. /* Can't assert these as some callers speculatively call
  540. pbuf_header() to see if it's OK. Will return 1 below instead. */
  541. /* Check that we've got the correct type of pbuf to work with */
  542. LWIP_ASSERT("p->type == PBUF_RAM || p->type == PBUF_POOL",
  543. p->type == PBUF_RAM || p->type == PBUF_POOL);
  544. /* Check that we aren't going to move off the beginning of the pbuf */
  545. LWIP_ASSERT("p->payload - increment_magnitude >= p + SIZEOF_STRUCT_PBUF",
  546. (u8_t *)p->payload - increment_magnitude >= (u8_t *)p + SIZEOF_STRUCT_PBUF);
  547. #endif
  548. }
  549. type = p->type;
  550. /* remember current payload pointer */
  551. payload = p->payload;
  552. /* pbuf types containing payloads? */
  553. if (type == PBUF_RAM || type == PBUF_POOL) {
  554. /* set new payload pointer */
  555. p->payload = (u8_t *)p->payload - header_size_increment;
  556. /* boundary check fails? */
  557. if ((u8_t *)p->payload < (u8_t *)p + SIZEOF_STRUCT_PBUF) {
  558. LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE,
  559. ("pbuf_header: failed as %p < %p (not enough space for new header size)\n",
  560. (void *)p->payload, (void *)((u8_t *)p + SIZEOF_STRUCT_PBUF)));
  561. /* restore old payload pointer */
  562. p->payload = payload;
  563. /* bail out unsuccessfully */
  564. return 1;
  565. }
  566. /* pbuf types referring to external payloads? */
  567. } else if (type == PBUF_REF || type == PBUF_ROM) {
  568. /* hide a header in the payload? */
  569. if ((header_size_increment < 0) && (increment_magnitude <= p->len)) {
  570. /* increase payload pointer */
  571. p->payload = (u8_t *)p->payload - header_size_increment;
  572. } else if ((header_size_increment > 0) && force) {
  573. p->payload = (u8_t *)p->payload - header_size_increment;
  574. } else {
  575. /* cannot expand payload to front (yet!)
  576. * bail out unsuccessfully */
  577. return 1;
  578. }
  579. } else {
  580. /* Unknown type */
  581. LWIP_ASSERT("bad pbuf type", 0);
  582. return 1;
  583. }
  584. /* modify pbuf length fields */
  585. p->len += header_size_increment;
  586. p->tot_len += header_size_increment;
  587. LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_header: old %p new %p (%"S16_F")\n",
  588. (void *)payload, (void *)p->payload, header_size_increment));
  589. return 0;
  590. }
  591. /**
  592. * Adjusts the payload pointer to hide or reveal headers in the payload.
  593. *
  594. * Adjusts the ->payload pointer so that space for a header
  595. * (dis)appears in the pbuf payload.
  596. *
  597. * The ->payload, ->tot_len and ->len fields are adjusted.
  598. *
  599. * @param p pbuf to change the header size.
  600. * @param header_size_increment Number of bytes to increment header size which
  601. * increases the size of the pbuf. New space is on the front.
  602. * (Using a negative value decreases the header size.)
  603. * If hdr_size_inc is 0, this function does nothing and returns successful.
  604. *
  605. * PBUF_ROM and PBUF_REF type buffers cannot have their sizes increased, so
  606. * the call will fail. A check is made that the increase in header size does
  607. * not move the payload pointer in front of the start of the buffer.
  608. * @return non-zero on failure, zero on success.
  609. *
  610. */
  611. u8_t
  612. pbuf_header(struct pbuf *p, s16_t header_size_increment)
  613. {
  614. return pbuf_header_impl(p, header_size_increment, 0);
  615. }
  616. /**
  617. * Same as pbuf_header but does not check if 'header_size > 0' is allowed.
  618. * This is used internally only, to allow PBUF_REF for RX.
  619. */
  620. u8_t
  621. pbuf_header_force(struct pbuf *p, s16_t header_size_increment)
  622. {
  623. return pbuf_header_impl(p, header_size_increment, 1);
  624. }
  625. /**
  626. * @ingroup pbuf
  627. * Dereference a pbuf chain or queue and deallocate any no-longer-used
  628. * pbufs at the head of this chain or queue.
  629. *
  630. * Decrements the pbuf reference count. If it reaches zero, the pbuf is
  631. * deallocated.
  632. *
  633. * For a pbuf chain, this is repeated for each pbuf in the chain,
  634. * up to the first pbuf which has a non-zero reference count after
  635. * decrementing. So, when all reference counts are one, the whole
  636. * chain is free'd.
  637. *
  638. * @param p The pbuf (chain) to be dereferenced.
  639. *
  640. * @return the number of pbufs that were de-allocated
  641. * from the head of the chain.
  642. *
  643. * @note MUST NOT be called on a packet queue (Not verified to work yet).
  644. * @note the reference counter of a pbuf equals the number of pointers
  645. * that refer to the pbuf (or into the pbuf).
  646. *
  647. * @internal examples:
  648. *
  649. * Assuming existing chains a->b->c with the following reference
  650. * counts, calling pbuf_free(a) results in:
  651. *
  652. * 1->2->3 becomes ...1->3
  653. * 3->3->3 becomes 2->3->3
  654. * 1->1->2 becomes ......1
  655. * 2->1->1 becomes 1->1->1
  656. * 1->1->1 becomes .......
  657. *
  658. */
  659. u8_t
  660. pbuf_free(struct pbuf *p)
  661. {
  662. u16_t type;
  663. struct pbuf *q;
  664. u8_t count;
  665. if (p == NULL) {
  666. LWIP_ASSERT("p != NULL", p != NULL);
  667. /* if assertions are disabled, proceed with debug output */
  668. LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
  669. ("pbuf_free(p == NULL) was called.\n"));
  670. return 0;
  671. }
  672. LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free(%p)\n", (void *)p));
  673. PERF_START;
  674. LWIP_ASSERT("pbuf_free: sane type",
  675. p->type == PBUF_RAM || p->type == PBUF_ROM ||
  676. p->type == PBUF_REF || p->type == PBUF_POOL);
  677. count = 0;
  678. /* de-allocate all consecutive pbufs from the head of the chain that
  679. * obtain a zero reference count after decrementing*/
  680. while (p != NULL) {
  681. u16_t ref;
  682. SYS_ARCH_DECL_PROTECT(old_level);
  683. /* Since decrementing ref cannot be guaranteed to be a single machine operation
  684. * we must protect it. We put the new ref into a local variable to prevent
  685. * further protection. */
  686. SYS_ARCH_PROTECT(old_level);
  687. /* all pbufs in a chain are referenced at least once */
  688. LWIP_ASSERT("pbuf_free: p->ref > 0", p->ref > 0);
  689. /* decrease reference count (number of pointers to pbuf) */
  690. ref = --(p->ref);
  691. SYS_ARCH_UNPROTECT(old_level);
  692. /* this pbuf is no longer referenced to? */
  693. if (ref == 0) {
  694. /* remember next pbuf in chain for next iteration */
  695. q = p->next;
  696. LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: deallocating %p\n", (void *)p));
  697. type = p->type;
  698. #if LWIP_SUPPORT_CUSTOM_PBUF
  699. /* is this a custom pbuf? */
  700. if ((p->flags & PBUF_FLAG_IS_CUSTOM) != 0) {
  701. struct pbuf_custom *pc = (struct pbuf_custom*)p;
  702. LWIP_ASSERT("pc->custom_free_function != NULL", pc->custom_free_function != NULL);
  703. pc->custom_free_function(p);
  704. } else
  705. #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
  706. {
  707. /* is this a pbuf from the pool? */
  708. if (type == PBUF_POOL) {
  709. memp_free(MEMP_PBUF_POOL, p);
  710. /* is this a ROM or RAM referencing pbuf? */
  711. } else if (type == PBUF_ROM || type == PBUF_REF) {
  712. memp_free(MEMP_PBUF, p);
  713. /* type == PBUF_RAM */
  714. } else {
  715. mem_free(p);
  716. }
  717. }
  718. count++;
  719. /* proceed to next pbuf */
  720. p = q;
  721. /* p->ref > 0, this pbuf is still referenced to */
  722. /* (and so the remaining pbufs in chain as well) */
  723. } else {
  724. LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: %p has ref %"U16_F", ending here.\n", (void *)p, ref));
  725. /* stop walking through the chain */
  726. p = NULL;
  727. }
  728. }
  729. PERF_STOP("pbuf_free");
  730. /* return number of de-allocated pbufs */
  731. return count;
  732. }
  733. /**
  734. * Count number of pbufs in a chain
  735. *
  736. * @param p first pbuf of chain
  737. * @return the number of pbufs in a chain
  738. */
  739. u16_t
  740. pbuf_clen(const struct pbuf *p)
  741. {
  742. u16_t len;
  743. len = 0;
  744. while (p != NULL) {
  745. ++len;
  746. p = p->next;
  747. }
  748. return len;
  749. }
  750. /**
  751. * @ingroup pbuf
  752. * Increment the reference count of the pbuf.
  753. *
  754. * @param p pbuf to increase reference counter of
  755. *
  756. */
  757. void
  758. pbuf_ref(struct pbuf *p)
  759. {
  760. /* pbuf given? */
  761. if (p != NULL) {
  762. SYS_ARCH_INC(p->ref, 1);
  763. LWIP_ASSERT("pbuf ref overflow", p->ref > 0);
  764. }
  765. }
  766. /**
  767. * @ingroup pbuf
  768. * Concatenate two pbufs (each may be a pbuf chain) and take over
  769. * the caller's reference of the tail pbuf.
  770. *
  771. * @note The caller MAY NOT reference the tail pbuf afterwards.
  772. * Use pbuf_chain() for that purpose.
  773. *
  774. * @see pbuf_chain()
  775. */
  776. void
  777. pbuf_cat(struct pbuf *h, struct pbuf *t)
  778. {
  779. struct pbuf *p;
  780. LWIP_ERROR("(h != NULL) && (t != NULL) (programmer violates API)",
  781. ((h != NULL) && (t != NULL)), return;);
  782. /* proceed to last pbuf of chain */
  783. for (p = h; p->next != NULL; p = p->next) {
  784. /* add total length of second chain to all totals of first chain */
  785. p->tot_len += t->tot_len;
  786. }
  787. /* { p is last pbuf of first h chain, p->next == NULL } */
  788. LWIP_ASSERT("p->tot_len == p->len (of last pbuf in chain)", p->tot_len == p->len);
  789. LWIP_ASSERT("p->next == NULL", p->next == NULL);
  790. /* add total length of second chain to last pbuf total of first chain */
  791. p->tot_len += t->tot_len;
  792. /* chain last pbuf of head (p) with first of tail (t) */
  793. p->next = t;
  794. /* p->next now references t, but the caller will drop its reference to t,
  795. * so netto there is no change to the reference count of t.
  796. */
  797. }
  798. /**
  799. * @ingroup pbuf
  800. * Chain two pbufs (or pbuf chains) together.
  801. *
  802. * The caller MUST call pbuf_free(t) once it has stopped
  803. * using it. Use pbuf_cat() instead if you no longer use t.
  804. *
  805. * @param h head pbuf (chain)
  806. * @param t tail pbuf (chain)
  807. * @note The pbufs MUST belong to the same packet.
  808. * @note MAY NOT be called on a packet queue.
  809. *
  810. * The ->tot_len fields of all pbufs of the head chain are adjusted.
  811. * The ->next field of the last pbuf of the head chain is adjusted.
  812. * The ->ref field of the first pbuf of the tail chain is adjusted.
  813. *
  814. */
  815. void
  816. pbuf_chain(struct pbuf *h, struct pbuf *t)
  817. {
  818. pbuf_cat(h, t);
  819. /* t is now referenced by h */
  820. pbuf_ref(t);
  821. LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_chain: %p references %p\n", (void *)h, (void *)t));
  822. }
  823. /**
  824. * Dechains the first pbuf from its succeeding pbufs in the chain.
  825. *
  826. * Makes p->tot_len field equal to p->len.
  827. * @param p pbuf to dechain
  828. * @return remainder of the pbuf chain, or NULL if it was de-allocated.
  829. * @note May not be called on a packet queue.
  830. */
  831. struct pbuf *
  832. pbuf_dechain(struct pbuf *p)
  833. {
  834. struct pbuf *q;
  835. u8_t tail_gone = 1;
  836. /* tail */
  837. q = p->next;
  838. /* pbuf has successor in chain? */
  839. if (q != NULL) {
  840. /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
  841. LWIP_ASSERT("p->tot_len == p->len + q->tot_len", q->tot_len == p->tot_len - p->len);
  842. /* enforce invariant if assertion is disabled */
  843. q->tot_len = p->tot_len - p->len;
  844. /* decouple pbuf from remainder */
  845. p->next = NULL;
  846. /* total length of pbuf p is its own length only */
  847. p->tot_len = p->len;
  848. /* q is no longer referenced by p, free it */
  849. LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_dechain: unreferencing %p\n", (void *)q));
  850. tail_gone = pbuf_free(q);
  851. if (tail_gone > 0) {
  852. LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE,
  853. ("pbuf_dechain: deallocated %p (as it is no longer referenced)\n", (void *)q));
  854. }
  855. /* return remaining tail or NULL if deallocated */
  856. }
  857. /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
  858. LWIP_ASSERT("p->tot_len == p->len", p->tot_len == p->len);
  859. return ((tail_gone > 0) ? NULL : q);
  860. }
  861. /**
  862. * @ingroup pbuf
  863. * Create PBUF_RAM copies of pbufs.
  864. *
  865. * Used to queue packets on behalf of the lwIP stack, such as
  866. * ARP based queueing.
  867. *
  868. * @note You MUST explicitly use p = pbuf_take(p);
  869. *
  870. * @note Only one packet is copied, no packet queue!
  871. *
  872. * @param p_to pbuf destination of the copy
  873. * @param p_from pbuf source of the copy
  874. *
  875. * @return ERR_OK if pbuf was copied
  876. * ERR_ARG if one of the pbufs is NULL or p_to is not big
  877. * enough to hold p_from
  878. */
  879. err_t
  880. pbuf_copy(struct pbuf *p_to, const struct pbuf *p_from)
  881. {
  882. u16_t offset_to=0, offset_from=0, len;
  883. LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy(%p, %p)\n",
  884. (const void*)p_to, (const void*)p_from));
  885. /* is the target big enough to hold the source? */
  886. LWIP_ERROR("pbuf_copy: target not big enough to hold source", ((p_to != NULL) &&
  887. (p_from != NULL) && (p_to->tot_len >= p_from->tot_len)), return ERR_ARG;);
  888. /* iterate through pbuf chain */
  889. do
  890. {
  891. /* copy one part of the original chain */
  892. if ((p_to->len - offset_to) >= (p_from->len - offset_from)) {
  893. /* complete current p_from fits into current p_to */
  894. len = p_from->len - offset_from;
  895. } else {
  896. /* current p_from does not fit into current p_to */
  897. len = p_to->len - offset_to;
  898. }
  899. MEMCPY((u8_t*)p_to->payload + offset_to, (u8_t*)p_from->payload + offset_from, len);
  900. offset_to += len;
  901. offset_from += len;
  902. LWIP_ASSERT("offset_to <= p_to->len", offset_to <= p_to->len);
  903. LWIP_ASSERT("offset_from <= p_from->len", offset_from <= p_from->len);
  904. if (offset_from >= p_from->len) {
  905. /* on to next p_from (if any) */
  906. offset_from = 0;
  907. p_from = p_from->next;
  908. }
  909. if (offset_to == p_to->len) {
  910. /* on to next p_to (if any) */
  911. offset_to = 0;
  912. p_to = p_to->next;
  913. LWIP_ERROR("p_to != NULL", (p_to != NULL) || (p_from == NULL) , return ERR_ARG;);
  914. }
  915. if ((p_from != NULL) && (p_from->len == p_from->tot_len)) {
  916. /* don't copy more than one packet! */
  917. LWIP_ERROR("pbuf_copy() does not allow packet queues!",
  918. (p_from->next == NULL), return ERR_VAL;);
  919. }
  920. if ((p_to != NULL) && (p_to->len == p_to->tot_len)) {
  921. /* don't copy more than one packet! */
  922. LWIP_ERROR("pbuf_copy() does not allow packet queues!",
  923. (p_to->next == NULL), return ERR_VAL;);
  924. }
  925. } while (p_from);
  926. LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy: end of chain reached.\n"));
  927. return ERR_OK;
  928. }
  929. /**
  930. * @ingroup pbuf
  931. * Copy (part of) the contents of a packet buffer
  932. * to an application supplied buffer.
  933. *
  934. * @param buf the pbuf from which to copy data
  935. * @param dataptr the application supplied buffer
  936. * @param len length of data to copy (dataptr must be big enough). No more
  937. * than buf->tot_len will be copied, irrespective of len
  938. * @param offset offset into the packet buffer from where to begin copying len bytes
  939. * @return the number of bytes copied, or 0 on failure
  940. */
  941. u16_t
  942. pbuf_copy_partial(const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
  943. {
  944. const struct pbuf *p;
  945. u16_t left;
  946. u16_t buf_copy_len;
  947. u16_t copied_total = 0;
  948. LWIP_ERROR("pbuf_copy_partial: invalid buf", (buf != NULL), return 0;);
  949. LWIP_ERROR("pbuf_copy_partial: invalid dataptr", (dataptr != NULL), return 0;);
  950. left = 0;
  951. if ((buf == NULL) || (dataptr == NULL)) {
  952. return 0;
  953. }
  954. /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
  955. for (p = buf; len != 0 && p != NULL; p = p->next) {
  956. if ((offset != 0) && (offset >= p->len)) {
  957. /* don't copy from this buffer -> on to the next */
  958. offset -= p->len;
  959. } else {
  960. /* copy from this buffer. maybe only partially. */
  961. buf_copy_len = p->len - offset;
  962. if (buf_copy_len > len) {
  963. buf_copy_len = len;
  964. }
  965. /* copy the necessary parts of the buffer */
  966. MEMCPY(&((char*)dataptr)[left], &((char*)p->payload)[offset], buf_copy_len);
  967. copied_total += buf_copy_len;
  968. left += buf_copy_len;
  969. len -= buf_copy_len;
  970. offset = 0;
  971. }
  972. }
  973. return copied_total;
  974. }
  975. #if LWIP_TCP && TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
  976. /**
  977. * This method modifies a 'pbuf chain', so that its total length is
  978. * smaller than 64K. The remainder of the original pbuf chain is stored
  979. * in *rest.
  980. * This function never creates new pbufs, but splits an existing chain
  981. * in two parts. The tot_len of the modified packet queue will likely be
  982. * smaller than 64K.
  983. * 'packet queues' are not supported by this function.
  984. *
  985. * @param p the pbuf queue to be split
  986. * @param rest pointer to store the remainder (after the first 64K)
  987. */
  988. void pbuf_split_64k(struct pbuf *p, struct pbuf **rest)
  989. {
  990. *rest = NULL;
  991. if ((p != NULL) && (p->next != NULL)) {
  992. u16_t tot_len_front = p->len;
  993. struct pbuf *i = p;
  994. struct pbuf *r = p->next;
  995. /* continue until the total length (summed up as u16_t) overflows */
  996. while ((r != NULL) && ((u16_t)(tot_len_front + r->len) > tot_len_front)) {
  997. tot_len_front += r->len;
  998. i = r;
  999. r = r->next;
  1000. }
  1001. /* i now points to last packet of the first segment. Set next
  1002. pointer to NULL */
  1003. i->next = NULL;
  1004. if (r != NULL) {
  1005. /* Update the tot_len field in the first part */
  1006. for (i = p; i != NULL; i = i->next) {
  1007. i->tot_len -= r->tot_len;
  1008. LWIP_ASSERT("tot_len/len mismatch in last pbuf",
  1009. (i->next != NULL) || (i->tot_len == i->len));
  1010. }
  1011. if (p->flags & PBUF_FLAG_TCP_FIN) {
  1012. r->flags |= PBUF_FLAG_TCP_FIN;
  1013. }
  1014. /* tot_len field in rest does not need modifications */
  1015. /* reference counters do not need modifications */
  1016. *rest = r;
  1017. }
  1018. }
  1019. }
  1020. #endif /* LWIP_TCP && TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
  1021. /* Actual implementation of pbuf_skip() but returning const pointer... */
  1022. static const struct pbuf*
  1023. pbuf_skip_const(const struct pbuf* in, u16_t in_offset, u16_t* out_offset)
  1024. {
  1025. u16_t offset_left = in_offset;
  1026. const struct pbuf* q = in;
  1027. /* get the correct pbuf */
  1028. while ((q != NULL) && (q->len <= offset_left)) {
  1029. offset_left -= q->len;
  1030. q = q->next;
  1031. }
  1032. if (out_offset != NULL) {
  1033. *out_offset = offset_left;
  1034. }
  1035. return q;
  1036. }
  1037. /**
  1038. * @ingroup pbuf
  1039. * Skip a number of bytes at the start of a pbuf
  1040. *
  1041. * @param in input pbuf
  1042. * @param in_offset offset to skip
  1043. * @param out_offset resulting offset in the returned pbuf
  1044. * @return the pbuf in the queue where the offset is
  1045. */
  1046. struct pbuf*
  1047. pbuf_skip(struct pbuf* in, u16_t in_offset, u16_t* out_offset)
  1048. {
  1049. const struct pbuf* out = pbuf_skip_const(in, in_offset, out_offset);
  1050. return LWIP_CONST_CAST(struct pbuf*, out);
  1051. }
  1052. /**
  1053. * @ingroup pbuf
  1054. * Copy application supplied data into a pbuf.
  1055. * This function can only be used to copy the equivalent of buf->tot_len data.
  1056. *
  1057. * @param buf pbuf to fill with data
  1058. * @param dataptr application supplied data buffer
  1059. * @param len length of the application supplied data buffer
  1060. *
  1061. * @return ERR_OK if successful, ERR_MEM if the pbuf is not big enough
  1062. */
  1063. err_t
  1064. pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)
  1065. {
  1066. struct pbuf *p;
  1067. u16_t buf_copy_len;
  1068. u16_t total_copy_len = len;
  1069. u16_t copied_total = 0;
  1070. LWIP_ERROR("pbuf_take: invalid buf", (buf != NULL), return ERR_ARG;);
  1071. LWIP_ERROR("pbuf_take: invalid dataptr", (dataptr != NULL), return ERR_ARG;);
  1072. LWIP_ERROR("pbuf_take: buf not large enough", (buf->tot_len >= len), return ERR_MEM;);
  1073. if ((buf == NULL) || (dataptr == NULL) || (buf->tot_len < len)) {
  1074. return ERR_ARG;
  1075. }
  1076. /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
  1077. for (p = buf; total_copy_len != 0; p = p->next) {
  1078. LWIP_ASSERT("pbuf_take: invalid pbuf", p != NULL);
  1079. buf_copy_len = total_copy_len;
  1080. if (buf_copy_len > p->len) {
  1081. /* this pbuf cannot hold all remaining data */
  1082. buf_copy_len = p->len;
  1083. }
  1084. /* copy the necessary parts of the buffer */
  1085. MEMCPY(p->payload, &((const char*)dataptr)[copied_total], buf_copy_len);
  1086. total_copy_len -= buf_copy_len;
  1087. copied_total += buf_copy_len;
  1088. }
  1089. LWIP_ASSERT("did not copy all data", total_copy_len == 0 && copied_total == len);
  1090. return ERR_OK;
  1091. }
  1092. /**
  1093. * @ingroup pbuf
  1094. * Same as pbuf_take() but puts data at an offset
  1095. *
  1096. * @param buf pbuf to fill with data
  1097. * @param dataptr application supplied data buffer
  1098. * @param len length of the application supplied data buffer
  1099. * @param offset offset in pbuf where to copy dataptr to
  1100. *
  1101. * @return ERR_OK if successful, ERR_MEM if the pbuf is not big enough
  1102. */
  1103. err_t
  1104. pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset)
  1105. {
  1106. u16_t target_offset;
  1107. struct pbuf* q = pbuf_skip(buf, offset, &target_offset);
  1108. /* return requested data if pbuf is OK */
  1109. if ((q != NULL) && (q->tot_len >= target_offset + len)) {
  1110. u16_t remaining_len = len;
  1111. const u8_t* src_ptr = (const u8_t*)dataptr;
  1112. /* copy the part that goes into the first pbuf */
  1113. u16_t first_copy_len = LWIP_MIN(q->len - target_offset, len);
  1114. MEMCPY(((u8_t*)q->payload) + target_offset, dataptr, first_copy_len);
  1115. remaining_len -= first_copy_len;
  1116. src_ptr += first_copy_len;
  1117. if (remaining_len > 0) {
  1118. return pbuf_take(q->next, src_ptr, remaining_len);
  1119. }
  1120. return ERR_OK;
  1121. }
  1122. return ERR_MEM;
  1123. }
  1124. /**
  1125. * @ingroup pbuf
  1126. * Creates a single pbuf out of a queue of pbufs.
  1127. *
  1128. * @remark: Either the source pbuf 'p' is freed by this function or the original
  1129. * pbuf 'p' is returned, therefore the caller has to check the result!
  1130. *
  1131. * @param p the source pbuf
  1132. * @param layer pbuf_layer of the new pbuf
  1133. *
  1134. * @return a new, single pbuf (p->next is NULL)
  1135. * or the old pbuf if allocation fails
  1136. */
  1137. struct pbuf*
  1138. pbuf_coalesce(struct pbuf *p, pbuf_layer layer)
  1139. {
  1140. struct pbuf *q;
  1141. err_t err;
  1142. if (p->next == NULL) {
  1143. return p;
  1144. }
  1145. q = pbuf_alloc(layer, p->tot_len, PBUF_RAM);
  1146. if (q == NULL) {
  1147. /* @todo: what do we do now? */
  1148. return p;
  1149. }
  1150. err = pbuf_copy(q, p);
  1151. LWIP_UNUSED_ARG(err); /* in case of LWIP_NOASSERT */
  1152. LWIP_ASSERT("pbuf_copy failed", err == ERR_OK);
  1153. pbuf_free(p);
  1154. return q;
  1155. }
  1156. #if LWIP_CHECKSUM_ON_COPY
  1157. /**
  1158. * Copies data into a single pbuf (*not* into a pbuf queue!) and updates
  1159. * the checksum while copying
  1160. *
  1161. * @param p the pbuf to copy data into
  1162. * @param start_offset offset of p->payload where to copy the data to
  1163. * @param dataptr data to copy into the pbuf
  1164. * @param len length of data to copy into the pbuf
  1165. * @param chksum pointer to the checksum which is updated
  1166. * @return ERR_OK if successful, another error if the data does not fit
  1167. * within the (first) pbuf (no pbuf queues!)
  1168. */
  1169. err_t
  1170. pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr,
  1171. u16_t len, u16_t *chksum)
  1172. {
  1173. u32_t acc;
  1174. u16_t copy_chksum;
  1175. char *dst_ptr;
  1176. LWIP_ASSERT("p != NULL", p != NULL);
  1177. LWIP_ASSERT("dataptr != NULL", dataptr != NULL);
  1178. LWIP_ASSERT("chksum != NULL", chksum != NULL);
  1179. LWIP_ASSERT("len != 0", len != 0);
  1180. if ((start_offset >= p->len) || (start_offset + len > p->len)) {
  1181. return ERR_ARG;
  1182. }
  1183. dst_ptr = ((char*)p->payload) + start_offset;
  1184. copy_chksum = LWIP_CHKSUM_COPY(dst_ptr, dataptr, len);
  1185. if ((start_offset & 1) != 0) {
  1186. copy_chksum = SWAP_BYTES_IN_WORD(copy_chksum);
  1187. }
  1188. acc = *chksum;
  1189. acc += copy_chksum;
  1190. *chksum = FOLD_U32T(acc);
  1191. return ERR_OK;
  1192. }
  1193. #endif /* LWIP_CHECKSUM_ON_COPY */
  1194. /**
  1195. * @ingroup pbuf
  1196. * Get one byte from the specified position in a pbuf
  1197. * WARNING: returns zero for offset >= p->tot_len
  1198. *
  1199. * @param p pbuf to parse
  1200. * @param offset offset into p of the byte to return
  1201. * @return byte at an offset into p OR ZERO IF 'offset' >= p->tot_len
  1202. */
  1203. u8_t
  1204. pbuf_get_at(const struct pbuf* p, u16_t offset)
  1205. {
  1206. int ret = pbuf_try_get_at(p, offset);
  1207. if (ret >= 0) {
  1208. return (u8_t)ret;
  1209. }
  1210. return 0;
  1211. }
  1212. /**
  1213. * @ingroup pbuf
  1214. * Get one byte from the specified position in a pbuf
  1215. *
  1216. * @param p pbuf to parse
  1217. * @param offset offset into p of the byte to return
  1218. * @return byte at an offset into p [0..0xFF] OR negative if 'offset' >= p->tot_len
  1219. */
  1220. int
  1221. pbuf_try_get_at(const struct pbuf* p, u16_t offset)
  1222. {
  1223. u16_t q_idx;
  1224. const struct pbuf* q = pbuf_skip_const(p, offset, &q_idx);
  1225. /* return requested data if pbuf is OK */
  1226. if ((q != NULL) && (q->len > q_idx)) {
  1227. return ((u8_t*)q->payload)[q_idx];
  1228. }
  1229. return -1;
  1230. }
  1231. /**
  1232. * @ingroup pbuf
  1233. * Put one byte to the specified position in a pbuf
  1234. * WARNING: silently ignores offset >= p->tot_len
  1235. *
  1236. * @param p pbuf to fill
  1237. * @param offset offset into p of the byte to write
  1238. * @param data byte to write at an offset into p
  1239. */
  1240. void
  1241. pbuf_put_at(struct pbuf* p, u16_t offset, u8_t data)
  1242. {
  1243. u16_t q_idx;
  1244. struct pbuf* q = pbuf_skip(p, offset, &q_idx);
  1245. /* write requested data if pbuf is OK */
  1246. if ((q != NULL) && (q->len > q_idx)) {
  1247. ((u8_t*)q->payload)[q_idx] = data;
  1248. }
  1249. }
  1250. /**
  1251. * @ingroup pbuf
  1252. * Compare pbuf contents at specified offset with memory s2, both of length n
  1253. *
  1254. * @param p pbuf to compare
  1255. * @param offset offset into p at which to start comparing
  1256. * @param s2 buffer to compare
  1257. * @param n length of buffer to compare
  1258. * @return zero if equal, nonzero otherwise
  1259. * (0xffff if p is too short, diffoffset+1 otherwise)
  1260. */
  1261. u16_t
  1262. pbuf_memcmp(const struct pbuf* p, u16_t offset, const void* s2, u16_t n)
  1263. {
  1264. u16_t start = offset;
  1265. const struct pbuf* q = p;
  1266. u16_t i;
  1267. /* pbuf long enough to perform check? */
  1268. if(p->tot_len < (offset + n)) {
  1269. return 0xffff;
  1270. }
  1271. /* get the correct pbuf from chain. We know it succeeds because of p->tot_len check above. */
  1272. while ((q != NULL) && (q->len <= start)) {
  1273. start -= q->len;
  1274. q = q->next;
  1275. }
  1276. /* return requested data if pbuf is OK */
  1277. for (i = 0; i < n; i++) {
  1278. /* We know pbuf_get_at() succeeds because of p->tot_len check above. */
  1279. u8_t a = pbuf_get_at(q, start + i);
  1280. u8_t b = ((const u8_t*)s2)[i];
  1281. if (a != b) {
  1282. return i+1;
  1283. }
  1284. }
  1285. return 0;
  1286. }
  1287. /**
  1288. * @ingroup pbuf
  1289. * Find occurrence of mem (with length mem_len) in pbuf p, starting at offset
  1290. * start_offset.
  1291. *
  1292. * @param p pbuf to search, maximum length is 0xFFFE since 0xFFFF is used as
  1293. * return value 'not found'
  1294. * @param mem search for the contents of this buffer
  1295. * @param mem_len length of 'mem'
  1296. * @param start_offset offset into p at which to start searching
  1297. * @return 0xFFFF if substr was not found in p or the index where it was found
  1298. */
  1299. u16_t
  1300. pbuf_memfind(const struct pbuf* p, const void* mem, u16_t mem_len, u16_t start_offset)
  1301. {
  1302. u16_t i;
  1303. u16_t max = p->tot_len - mem_len;
  1304. if (p->tot_len >= mem_len + start_offset) {
  1305. for (i = start_offset; i <= max; i++) {
  1306. u16_t plus = pbuf_memcmp(p, i, mem, mem_len);
  1307. if (plus == 0) {
  1308. return i;
  1309. }
  1310. }
  1311. }
  1312. return 0xFFFF;
  1313. }
  1314. /**
  1315. * Find occurrence of substr with length substr_len in pbuf p, start at offset
  1316. * start_offset
  1317. * WARNING: in contrast to strstr(), this one does not stop at the first \0 in
  1318. * the pbuf/source string!
  1319. *
  1320. * @param p pbuf to search, maximum length is 0xFFFE since 0xFFFF is used as
  1321. * return value 'not found'
  1322. * @param substr string to search for in p, maximum length is 0xFFFE
  1323. * @return 0xFFFF if substr was not found in p or the index where it was found
  1324. */
  1325. u16_t
  1326. pbuf_strstr(const struct pbuf* p, const char* substr)
  1327. {
  1328. size_t substr_len;
  1329. if ((substr == NULL) || (substr[0] == 0) || (p->tot_len == 0xFFFF)) {
  1330. return 0xFFFF;
  1331. }
  1332. substr_len = strlen(substr);
  1333. if (substr_len >= 0xFFFF) {
  1334. return 0xFFFF;
  1335. }
  1336. return pbuf_memfind(p, substr, (u16_t)substr_len, 0);
  1337. }