A version of LWIP, provided for backwards compatibility.

Dependents:   AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... more

Committer:
root@mbed.org
Date:
Tue May 08 15:32:10 2012 +0100
Revision:
0:5e1631496985
initial commit

Who changed what in which revision?

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