Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pbuf.h Source File

pbuf.h

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * pbuf API
00004  */
00005 
00006 /*
00007  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without modification,
00011  * are permitted provided that the following conditions are met:
00012  *
00013  * 1. Redistributions of source code must retain the above copyright notice,
00014  *    this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright notice,
00016  *    this list of conditions and the following disclaimer in the documentation
00017  *    and/or other materials provided with the distribution.
00018  * 3. The name of the author may not be used to endorse or promote products
00019  *    derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00023  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00024  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00026  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00029  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00030  * OF SUCH DAMAGE.
00031  *
00032  * This file is part of the lwIP TCP/IP stack.
00033  *
00034  * Author: Adam Dunkels <adam@sics.se>
00035  *
00036  */
00037 
00038 #ifndef LWIP_HDR_PBUF_H
00039 #define LWIP_HDR_PBUF_H
00040 
00041 #include "lwip/opt.h"
00042 #include "lwip/err.h"
00043 
00044 #ifdef __cplusplus
00045 extern "C" {
00046 #endif
00047 
00048 /** LWIP_SUPPORT_CUSTOM_PBUF==1: Custom pbufs behave much like their pbuf type
00049  * but they are allocated by external code (initialised by calling
00050  * pbuf_alloced_custom()) and when pbuf_free gives up their last reference, they
00051  * are freed by calling pbuf_custom->custom_free_function().
00052  * Currently, the pbuf_custom code is only needed for one specific configuration
00053  * of IP_FRAG, unless required by external driver/application code. */
00054 #ifndef LWIP_SUPPORT_CUSTOM_PBUF
00055 #define LWIP_SUPPORT_CUSTOM_PBUF ((IP_FRAG && !LWIP_NETIF_TX_SINGLE_PBUF) || (LWIP_IPV6 && LWIP_IPV6_FRAG))
00056 #endif
00057 
00058 /* @todo: We need a mechanism to prevent wasting memory in every pbuf
00059    (TCP vs. UDP, IPv4 vs. IPv6: UDP/IPv4 packets may waste up to 28 bytes) */
00060 
00061 #define PBUF_TRANSPORT_HLEN 20
00062 #if LWIP_IPV6
00063 #define PBUF_IP_HLEN        40
00064 #else
00065 #define PBUF_IP_HLEN        20
00066 #endif
00067 
00068 /**
00069  * @ingroup pbuf
00070  * Enumeration of pbuf layers
00071  */
00072 typedef enum {
00073   /** Includes spare room for transport layer header, e.g. UDP header.
00074    * Use this if you intend to pass the pbuf to functions like udp_send().
00075    */
00076   PBUF_TRANSPORT,
00077   /** Includes spare room for IP header.
00078    * Use this if you intend to pass the pbuf to functions like raw_send().
00079    */
00080   PBUF_IP,
00081   /** Includes spare room for link layer header (ethernet header).
00082    * Use this if you intend to pass the pbuf to functions like ethernet_output().
00083    * @see PBUF_LINK_HLEN
00084    */
00085   PBUF_LINK,
00086   /** Includes spare room for additional encapsulation header before ethernet
00087    * headers (e.g. 802.11).
00088    * Use this if you intend to pass the pbuf to functions like netif->linkoutput().
00089    * @see PBUF_LINK_ENCAPSULATION_HLEN
00090    */
00091   PBUF_RAW_TX,
00092   /** Use this for input packets in a netif driver when calling netif->input()
00093    * in the most common case - ethernet-layer netif driver. */
00094   PBUF_RAW
00095 } pbuf_layer;
00096 
00097 /**
00098  * @ingroup pbuf
00099  * Enumeration of pbuf types
00100  */
00101 typedef enum {
00102   /** pbuf data is stored in RAM, used for TX mostly, struct pbuf and its payload
00103       are allocated in one piece of contiguous memory (so the first payload byte
00104       can be calculated from struct pbuf).
00105       pbuf_alloc() allocates PBUF_RAM pbufs as unchained pbufs (although that might
00106       change in future versions).
00107       This should be used for all OUTGOING packets (TX).*/
00108   PBUF_RAM,
00109   /** pbuf data is stored in ROM, i.e. struct pbuf and its payload are located in
00110       totally different memory areas. Since it points to ROM, payload does not
00111       have to be copied when queued for transmission. */
00112   PBUF_ROM,
00113   /** pbuf comes from the pbuf pool. Much like PBUF_ROM but payload might change
00114       so it has to be duplicated when queued before transmitting, depending on
00115       who has a 'ref' to it. */
00116   PBUF_REF,
00117   /** pbuf payload refers to RAM. This one comes from a pool and should be used
00118       for RX. Payload can be chained (scatter-gather RX) but like PBUF_RAM, struct
00119       pbuf and its payload are allocated in one piece of contiguous memory (so
00120       the first payload byte can be calculated from struct pbuf).
00121       Don't use this for TX, if the pool becomes empty e.g. because of TCP queuing,
00122       you are unable to receive TCP acks! */
00123   PBUF_POOL
00124 } pbuf_type;
00125 
00126 
00127 /** indicates this packet's data should be immediately passed to the application */
00128 #define PBUF_FLAG_PUSH      0x01U
00129 /** indicates this is a custom pbuf: pbuf_free calls pbuf_custom->custom_free_function()
00130     when the last reference is released (plus custom PBUF_RAM cannot be trimmed) */
00131 #define PBUF_FLAG_IS_CUSTOM 0x02U
00132 /** indicates this pbuf is UDP multicast to be looped back */
00133 #define PBUF_FLAG_MCASTLOOP 0x04U
00134 /** indicates this pbuf was received as link-level broadcast */
00135 #define PBUF_FLAG_LLBCAST   0x08U
00136 /** indicates this pbuf was received as link-level multicast */
00137 #define PBUF_FLAG_LLMCAST   0x10U
00138 /** indicates this pbuf includes a TCP FIN flag */
00139 #define PBUF_FLAG_TCP_FIN   0x20U
00140 
00141 /** Main packet buffer struct */
00142 struct pbuf {
00143   /** next pbuf in singly linked pbuf chain */
00144   struct pbuf *next;
00145 
00146   /** pointer to the actual data in the buffer */
00147   void *payload;
00148 
00149   /**
00150    * total length of this buffer and all next buffers in chain
00151    * belonging to the same packet.
00152    *
00153    * For non-queue packet chains this is the invariant:
00154    * p->tot_len == p->len + (p->next? p->next->tot_len: 0)
00155    */
00156   u16_t tot_len;
00157 
00158   /** length of this buffer */
00159   u16_t len;
00160 
00161   /** pbuf_type as u8_t instead of enum to save space */
00162   u8_t /*pbuf_type*/ type;
00163 
00164   /** misc flags */
00165   u8_t flags;
00166 
00167   /**
00168    * the reference count always equals the number of pointers
00169    * that refer to this pbuf. This can be pointers from an application,
00170    * the stack itself, or pbuf->next pointers from a chain.
00171    */
00172   u16_t ref;
00173 };
00174 
00175 
00176 /** Helper struct for const-correctness only.
00177  * The only meaning of this one is to provide a const payload pointer
00178  * for PBUF_ROM type.
00179  */
00180 struct pbuf_rom {
00181   /** next pbuf in singly linked pbuf chain */
00182   struct pbuf *next;
00183 
00184   /** pointer to the actual data in the buffer */
00185   const void *payload;
00186 };
00187 
00188 #if LWIP_SUPPORT_CUSTOM_PBUF
00189 /** Prototype for a function to free a custom pbuf */
00190 typedef void (*pbuf_free_custom_fn)(struct pbuf *p);
00191 
00192 /** A custom pbuf: like a pbuf, but following a function pointer to free it. */
00193 struct pbuf_custom {
00194   /** The actual pbuf */
00195   struct pbuf pbuf;
00196   /** This function is called when pbuf_free deallocates this pbuf(_custom) */
00197   pbuf_free_custom_fn custom_free_function;
00198 };
00199 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
00200 
00201 /** Define this to 0 to prevent freeing ooseq pbufs when the PBUF_POOL is empty */
00202 #ifndef PBUF_POOL_FREE_OOSEQ
00203 #define PBUF_POOL_FREE_OOSEQ 1
00204 #endif /* PBUF_POOL_FREE_OOSEQ */
00205 #if LWIP_TCP && TCP_QUEUE_OOSEQ && NO_SYS && PBUF_POOL_FREE_OOSEQ
00206 extern volatile u8_t pbuf_free_ooseq_pending;
00207 void pbuf_free_ooseq(void);
00208 /** When not using sys_check_timeouts(), call PBUF_CHECK_FREE_OOSEQ()
00209     at regular intervals from main level to check if ooseq pbufs need to be
00210     freed! */
00211 #define PBUF_CHECK_FREE_OOSEQ() do { if(pbuf_free_ooseq_pending) { \
00212   /* pbuf_alloc() reported PBUF_POOL to be empty -> try to free some \
00213      ooseq queued pbufs now */ \
00214   pbuf_free_ooseq(); }}while(0)
00215 #else /* LWIP_TCP && TCP_QUEUE_OOSEQ && NO_SYS && PBUF_POOL_FREE_OOSEQ */
00216   /* Otherwise declare an empty PBUF_CHECK_FREE_OOSEQ */
00217   #define PBUF_CHECK_FREE_OOSEQ()
00218 #endif /* LWIP_TCP && TCP_QUEUE_OOSEQ && NO_SYS && PBUF_POOL_FREE_OOSEQ*/
00219 
00220 /* Initializes the pbuf module. This call is empty for now, but may not be in future. */
00221 #define pbuf_init()
00222 
00223 struct pbuf *pbuf_alloc(pbuf_layer l, u16_t length, pbuf_type type);
00224 #if LWIP_SUPPORT_CUSTOM_PBUF
00225 struct pbuf *pbuf_alloced_custom(pbuf_layer l, u16_t length, pbuf_type type,
00226                                  struct pbuf_custom *p, void *payload_mem,
00227                                  u16_t payload_mem_len);
00228 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
00229 void pbuf_realloc(struct pbuf *p, u16_t size);
00230 u8_t pbuf_header(struct pbuf *p, s16_t header_size);
00231 u8_t pbuf_header_force(struct pbuf *p, s16_t header_size);
00232 void pbuf_ref(struct pbuf *p);
00233 u8_t pbuf_free(struct pbuf *p);
00234 u16_t pbuf_clen(const struct pbuf *p);
00235 void pbuf_cat(struct pbuf *head, struct pbuf *tail);
00236 void pbuf_chain(struct pbuf *head, struct pbuf *tail);
00237 struct pbuf *pbuf_dechain(struct pbuf *p);
00238 err_t pbuf_copy(struct pbuf *p_to, const struct pbuf *p_from);
00239 u16_t pbuf_copy_partial(const struct pbuf *p, void *dataptr, u16_t len, u16_t offset);
00240 err_t pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len);
00241 err_t pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset);
00242 struct pbuf *pbuf_skip(struct pbuf* in, u16_t in_offset, u16_t* out_offset);
00243 struct pbuf *pbuf_coalesce(struct pbuf *p, pbuf_layer layer);
00244 #if LWIP_CHECKSUM_ON_COPY
00245 err_t pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr,
00246                        u16_t len, u16_t *chksum);
00247 #endif /* LWIP_CHECKSUM_ON_COPY */
00248 #if LWIP_TCP && TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
00249 void pbuf_split_64k(struct pbuf *p, struct pbuf **rest);
00250 #endif /* LWIP_TCP && TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
00251 
00252 u8_t pbuf_get_at(const struct pbuf* p, u16_t offset);
00253 int pbuf_try_get_at(const struct pbuf* p, u16_t offset);
00254 void pbuf_put_at(struct pbuf* p, u16_t offset, u8_t data);
00255 u16_t pbuf_memcmp(const struct pbuf* p, u16_t offset, const void* s2, u16_t n);
00256 u16_t pbuf_memfind(const struct pbuf* p, const void* mem, u16_t mem_len, u16_t start_offset);
00257 u16_t pbuf_strstr(const struct pbuf* p, const char* substr);
00258 
00259 #ifdef __cplusplus
00260 }
00261 #endif
00262 
00263 #endif /* LWIP_HDR_PBUF_H */