• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file
3  * pbuf API
4  */
5 
6 /*
7  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without modification,
11  * are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30  * OF SUCH DAMAGE.
31  *
32  * This file is part of the lwIP TCP/IP stack.
33  *
34  * Author: Adam Dunkels <adam@sics.se>
35  *
36  */
37 
38 #ifndef LWIP_HDR_PBUF_H
39 #define LWIP_HDR_PBUF_H
40 
41 #include "lwip/opt.h"
42 #include "lwip/err.h"
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 /** LWIP_SUPPORT_CUSTOM_PBUF==1: Custom pbufs behave much like their pbuf type
49  * but they are allocated by external code (initialised by calling
50  * pbuf_alloced_custom()) and when pbuf_free gives up their last reference, they
51  * are freed by calling pbuf_custom->custom_free_function().
52  * Currently, the pbuf_custom code is only needed for one specific configuration
53  * of IP_FRAG, unless required by external driver/application code. */
54 #ifndef LWIP_SUPPORT_CUSTOM_PBUF
55 #define LWIP_SUPPORT_CUSTOM_PBUF ((IP_FRAG && !LWIP_NETIF_TX_SINGLE_PBUF) || (LWIP_IPV6 && LWIP_IPV6_FRAG))
56 #endif
57 
58 /** @ingroup pbuf
59  * PBUF_NEEDS_COPY(p): return a boolean value indicating whether the given
60  * pbuf needs to be copied in order to be kept around beyond the current call
61  * stack without risking being corrupted. The default setting provides safety:
62  * it will make a copy iof any pbuf chain that does not consist entirely of
63  * PBUF_ROM type pbufs. For setups with zero-copy support, it may be redefined
64  * to evaluate to true in all cases, for example. However, doing so also has an
65  * effect on the application side: any buffers that are *not* copied must also
66  * *not* be reused by the application after passing them to lwIP. For example,
67  * when setting PBUF_NEEDS_COPY to (0), after using udp_send() with a PBUF_RAM
68  * pbuf, the application must free the pbuf immediately, rather than reusing it
69  * for other purposes. For more background information on this, see tasks #6735
70  * and #7896, and bugs #11400 and #49914. */
71 #ifndef PBUF_NEEDS_COPY
72 #define PBUF_NEEDS_COPY(p)  ((p)->type_internal & PBUF_TYPE_FLAG_DATA_VOLATILE)
73 #endif /* PBUF_NEEDS_COPY */
74 
75 /* @todo: We need a mechanism to prevent wasting memory in every pbuf
76    (TCP vs. UDP, IPv4 vs. IPv6: UDP/IPv4 packets may waste up to 28 bytes) */
77 
78 #define PBUF_TRANSPORT_HLEN 20
79 #if LWIP_IPV6
80 #define PBUF_IP_HLEN        40
81 #else
82 #define PBUF_IP_HLEN        20
83 #endif
84 
85 /**
86  * @ingroup pbuf
87  * Enumeration of pbuf layers
88  */
89 typedef enum {
90   /** Includes spare room for transport layer header, e.g. UDP header.
91    * Use this if you intend to pass the pbuf to functions like udp_send().
92    */
93   PBUF_TRANSPORT = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN,
94   /** Includes spare room for IP header.
95    * Use this if you intend to pass the pbuf to functions like raw_send().
96    */
97   PBUF_IP = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN,
98   /** Includes spare room for link layer header (ethernet header).
99    * Use this if you intend to pass the pbuf to functions like ethernet_output().
100    * @see PBUF_LINK_HLEN
101    */
102   PBUF_LINK = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN,
103   /** Includes spare room for additional encapsulation header before ethernet
104    * headers (e.g. 802.11).
105    * Use this if you intend to pass the pbuf to functions like netif->linkoutput().
106    * @see PBUF_LINK_ENCAPSULATION_HLEN
107    */
108   PBUF_RAW_TX = PBUF_LINK_ENCAPSULATION_HLEN,
109   /** Use this for input packets in a netif driver when calling netif->input()
110    * in the most common case - ethernet-layer netif driver. */
111   PBUF_RAW = 0
112 } pbuf_layer;
113 
114 
115 /* Base flags for pbuf_type definitions: */
116 
117 /** Indicates that the payload directly follows the struct pbuf.
118  *  This makes @ref pbuf_header work in both directions. */
119 #define PBUF_TYPE_FLAG_STRUCT_DATA_CONTIGUOUS       0x80
120 /** Indicates the data stored in this pbuf can change. If this pbuf needs
121  * to be queued, it must be copied/duplicated. */
122 #define PBUF_TYPE_FLAG_DATA_VOLATILE                0x40
123 /** 4 bits are reserved for 16 allocation sources (e.g. heap, pool1, pool2, etc)
124  * Internally, we use: 0=heap, 1=MEMP_PBUF, 2=MEMP_PBUF_POOL -> 13 types free*/
125 #define PBUF_TYPE_ALLOC_SRC_MASK                    0x0F
126 /** Indicates this pbuf is used for RX (if not set, indicates use for TX).
127  * This information can be used to keep some spare RX buffers e.g. for
128  * receiving TCP ACKs to unblock a connection) */
129 #define PBUF_ALLOC_FLAG_RX                          0x0100
130 /** Indicates the application needs the pbuf payload to be in one piece */
131 #define PBUF_ALLOC_FLAG_DATA_CONTIGUOUS             0x0200
132 
133 #define PBUF_TYPE_ALLOC_SRC_MASK_STD_HEAP           0x00
134 #define PBUF_TYPE_ALLOC_SRC_MASK_STD_MEMP_PBUF      0x01
135 #define PBUF_TYPE_ALLOC_SRC_MASK_STD_MEMP_PBUF_POOL 0x02
136 /** First pbuf allocation type for applications */
137 #define PBUF_TYPE_ALLOC_SRC_MASK_APP_MIN            0x03
138 /** Last pbuf allocation type for applications */
139 #define PBUF_TYPE_ALLOC_SRC_MASK_APP_MAX            PBUF_TYPE_ALLOC_SRC_MASK
140 
141 /**
142  * @ingroup pbuf
143  * Enumeration of pbuf types
144  */
145 typedef enum {
146   /** pbuf data is stored in RAM, used for TX mostly, struct pbuf and its payload
147       are allocated in one piece of contiguous memory (so the first payload byte
148       can be calculated from struct pbuf).
149       pbuf_alloc() allocates PBUF_RAM pbufs as unchained pbufs (although that might
150       change in future versions).
151       This should be used for all OUTGOING packets (TX).*/
152   PBUF_RAM = (PBUF_ALLOC_FLAG_DATA_CONTIGUOUS | PBUF_TYPE_FLAG_STRUCT_DATA_CONTIGUOUS | PBUF_TYPE_ALLOC_SRC_MASK_STD_HEAP),
153   /** pbuf data is stored in ROM, i.e. struct pbuf and its payload are located in
154       totally different memory areas. Since it points to ROM, payload does not
155       have to be copied when queued for transmission. */
156   PBUF_ROM = PBUF_TYPE_ALLOC_SRC_MASK_STD_MEMP_PBUF,
157   /** pbuf comes from the pbuf pool. Much like PBUF_ROM but payload might change
158       so it has to be duplicated when queued before transmitting, depending on
159       who has a 'ref' to it. */
160   PBUF_REF = (PBUF_TYPE_FLAG_DATA_VOLATILE | PBUF_TYPE_ALLOC_SRC_MASK_STD_MEMP_PBUF),
161   /** pbuf payload refers to RAM. This one comes from a pool and should be used
162       for RX. Payload can be chained (scatter-gather RX) but like PBUF_RAM, struct
163       pbuf and its payload are allocated in one piece of contiguous memory (so
164       the first payload byte can be calculated from struct pbuf).
165       Don't use this for TX, if the pool becomes empty e.g. because of TCP queuing,
166       you are unable to receive TCP acks! */
167   PBUF_POOL = (PBUF_ALLOC_FLAG_RX | PBUF_TYPE_FLAG_STRUCT_DATA_CONTIGUOUS | PBUF_TYPE_ALLOC_SRC_MASK_STD_MEMP_PBUF_POOL)
168 } pbuf_type;
169 
170 
171 /** indicates this packet's data should be immediately passed to the application */
172 #define PBUF_FLAG_PUSH      0x01U
173 /** indicates this is a custom pbuf: pbuf_free calls pbuf_custom->custom_free_function()
174     when the last reference is released (plus custom PBUF_RAM cannot be trimmed) */
175 #define PBUF_FLAG_IS_CUSTOM 0x02U
176 /** indicates this pbuf is UDP multicast to be looped back */
177 #define PBUF_FLAG_MCASTLOOP 0x04U
178 /** indicates this pbuf was received as link-level broadcast */
179 #define PBUF_FLAG_LLBCAST   0x08U
180 /** indicates this pbuf was received as link-level multicast */
181 #define PBUF_FLAG_LLMCAST   0x10U
182 /** indicates this pbuf includes a TCP FIN flag */
183 #define PBUF_FLAG_TCP_FIN   0x20U
184 
185 /** Main packet buffer struct */
186 struct pbuf {
187   /** next pbuf in singly linked pbuf chain */
188   struct pbuf *next;
189 
190   /** pointer to the actual data in the buffer */
191   void *payload;
192 
193   /**
194    * total length of this buffer and all next buffers in chain
195    * belonging to the same packet.
196    *
197    * For non-queue packet chains this is the invariant:
198    * p->tot_len == p->len + (p->next? p->next->tot_len: 0)
199    */
200   u16_t tot_len;
201 
202   /** length of this buffer */
203   u16_t len;
204 
205   /** a bit field indicating pbuf type and allocation sources
206       (see PBUF_TYPE_FLAG_*, PBUF_ALLOC_FLAG_* and PBUF_TYPE_ALLOC_SRC_MASK)
207     */
208   u8_t type_internal;
209 
210   /** misc flags */
211   u8_t flags;
212 
213   /**
214    * the reference count always equals the number of pointers
215    * that refer to this pbuf. This can be pointers from an application,
216    * the stack itself, or pbuf->next pointers from a chain.
217    */
218   LWIP_PBUF_REF_T ref;
219 
220   /** For incoming packets, this contains the input netif's index */
221   u8_t if_idx;
222 
223   /** In case the user needs to store data custom data on a pbuf */
224   LWIP_PBUF_CUSTOM_DATA
225 };
226 
227 
228 /** Helper struct for const-correctness only.
229  * The only meaning of this one is to provide a const payload pointer
230  * for PBUF_ROM type.
231  */
232 struct pbuf_rom {
233   /** next pbuf in singly linked pbuf chain */
234   struct pbuf *next;
235 
236   /** pointer to the actual data in the buffer */
237   const void *payload;
238 };
239 
240 #if LWIP_SUPPORT_CUSTOM_PBUF
241 /** Prototype for a function to free a custom pbuf */
242 typedef void (*pbuf_free_custom_fn)(struct pbuf *p);
243 
244 /** A custom pbuf: like a pbuf, but following a function pointer to free it. */
245 struct pbuf_custom {
246   /** The actual pbuf */
247   struct pbuf pbuf;
248   /** This function is called when pbuf_free deallocates this pbuf(_custom) */
249   pbuf_free_custom_fn custom_free_function;
250 };
251 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
252 
253 /** Define this to 0 to prevent freeing ooseq pbufs when the PBUF_POOL is empty */
254 #ifndef PBUF_POOL_FREE_OOSEQ
255 #define PBUF_POOL_FREE_OOSEQ 1
256 #endif /* PBUF_POOL_FREE_OOSEQ */
257 #if LWIP_TCP && TCP_QUEUE_OOSEQ && NO_SYS && PBUF_POOL_FREE_OOSEQ
258 extern volatile u8_t pbuf_free_ooseq_pending;
259 void pbuf_free_ooseq(void);
260 /** When not using sys_check_timeouts(), call PBUF_CHECK_FREE_OOSEQ()
261     at regular intervals from main level to check if ooseq pbufs need to be
262     freed! */
263 #define PBUF_CHECK_FREE_OOSEQ() do { if(pbuf_free_ooseq_pending) { \
264   /* pbuf_alloc() reported PBUF_POOL to be empty -> try to free some \
265      ooseq queued pbufs now */ \
266   pbuf_free_ooseq(); }}while(0)
267 #else /* LWIP_TCP && TCP_QUEUE_OOSEQ && NO_SYS && PBUF_POOL_FREE_OOSEQ */
268   /* Otherwise declare an empty PBUF_CHECK_FREE_OOSEQ */
269   #define PBUF_CHECK_FREE_OOSEQ()
270 #endif /* LWIP_TCP && TCP_QUEUE_OOSEQ && NO_SYS && PBUF_POOL_FREE_OOSEQ*/
271 
272 /* Initializes the pbuf module. This call is empty for now, but may not be in future. */
273 #define pbuf_init()
274 
275 struct pbuf *pbuf_alloc(pbuf_layer l, u16_t length, pbuf_type type);
276 struct pbuf *pbuf_alloc_reference(void *payload, u16_t length, pbuf_type type);
277 #if LWIP_SUPPORT_CUSTOM_PBUF
278 struct pbuf *pbuf_alloced_custom(pbuf_layer l, u16_t length, pbuf_type type,
279                                  struct pbuf_custom *p, void *payload_mem,
280                                  u16_t payload_mem_len);
281 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
282 void pbuf_realloc(struct pbuf *p, u16_t size);
283 #define pbuf_get_allocsrc(p)          ((p)->type_internal & PBUF_TYPE_ALLOC_SRC_MASK)
284 #define pbuf_match_allocsrc(p, type)  (pbuf_get_allocsrc(p) == ((type) & PBUF_TYPE_ALLOC_SRC_MASK))
285 #define pbuf_match_type(p, type)      pbuf_match_allocsrc(p, type)
286 u8_t pbuf_header(struct pbuf *p, s16_t header_size);
287 u8_t pbuf_header_force(struct pbuf *p, s16_t header_size);
288 u8_t pbuf_add_header(struct pbuf *p, size_t header_size_increment);
289 u8_t pbuf_add_header_force(struct pbuf *p, size_t header_size_increment);
290 u8_t pbuf_remove_header(struct pbuf *p, size_t header_size);
291 struct pbuf *pbuf_free_header(struct pbuf *q, u16_t size);
292 void pbuf_ref(struct pbuf *p);
293 u8_t pbuf_free(struct pbuf *p);
294 u16_t pbuf_clen(const struct pbuf *p);
295 void pbuf_cat(struct pbuf *head, struct pbuf *tail);
296 void pbuf_chain(struct pbuf *head, struct pbuf *tail);
297 struct pbuf *pbuf_dechain(struct pbuf *p);
298 err_t pbuf_copy(struct pbuf *p_to, const struct pbuf *p_from);
299 err_t pbuf_copy_partial_pbuf(struct pbuf *p_to, const struct pbuf *p_from, u16_t copy_len, u16_t offset);
300 u16_t pbuf_copy_partial(const struct pbuf *p, void *dataptr, u16_t len, u16_t offset);
301 void *pbuf_get_contiguous(const struct pbuf *p, void *buffer, size_t bufsize, u16_t len, u16_t offset);
302 err_t pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len);
303 err_t pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset);
304 struct pbuf *pbuf_skip(struct pbuf* in, u16_t in_offset, u16_t* out_offset);
305 struct pbuf *pbuf_coalesce(struct pbuf *p, pbuf_layer layer);
306 struct pbuf *pbuf_clone(pbuf_layer l, pbuf_type type, struct pbuf *p);
307 #if LWIP_CHECKSUM_ON_COPY
308 err_t pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr,
309                        u16_t len, u16_t *chksum);
310 #endif /* LWIP_CHECKSUM_ON_COPY */
311 #if LWIP_TCP && TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
312 void pbuf_split_64k(struct pbuf *p, struct pbuf **rest);
313 #endif /* LWIP_TCP && TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
314 
315 u8_t pbuf_get_at(const struct pbuf* p, u16_t offset);
316 int pbuf_try_get_at(const struct pbuf* p, u16_t offset);
317 void pbuf_put_at(struct pbuf* p, u16_t offset, u8_t data);
318 u16_t pbuf_memcmp(const struct pbuf* p, u16_t offset, const void* s2, u16_t n);
319 u16_t pbuf_memfind(const struct pbuf* p, const void* mem, u16_t mem_len, u16_t start_offset);
320 u16_t pbuf_strstr(const struct pbuf* p, const char* substr);
321 
322 #ifdef __cplusplus
323 }
324 #endif
325 
326 #endif /* LWIP_HDR_PBUF_H */
327