OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [lwIP_Demo_Rowley_ARM7/] [lwip-1.1.0/] [src/] [core/] [pbuf.c] - Blame information for rev 583

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.