7 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
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.
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
32 * This file is part of the lwIP TCP/IP stack.
34 * Author: Adam Dunkels <adam@sics.se>
38 #ifndef LWIP_HDR_PBUF_H
39 #define LWIP_HDR_PBUF_H
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))
58 /* @todo: We need a mechanism to prevent wasting memory in every pbuf
59 (TCP vs. UDP, IPv4 vs. IPv6: UDP/IPv4 packets may waste up to 28 bytes) */
61 #define PBUF_TRANSPORT_HLEN 20
63 #define PBUF_IP_HLEN 40
65 #define PBUF_IP_HLEN 20
70 * Enumeration of pbuf layers
73 /** Includes spare room for transport layer header, e.g. UDP header.
74 * Use this if you intend to pass the pbuf to functions like udp_send().
77 /** Includes spare room for IP header.
78 * Use this if you intend to pass the pbuf to functions like raw_send().
81 /** Includes spare room for link layer header (ethernet header).
82 * Use this if you intend to pass the pbuf to functions like ethernet_output().
86 /** Includes spare room for additional encapsulation header before ethernet
87 * headers (e.g. 802.11).
88 * Use this if you intend to pass the pbuf to functions like netif->linkoutput().
89 * @see PBUF_LINK_ENCAPSULATION_HLEN
92 /** Use this for input packets in a netif driver when calling netif->input()
93 * in the most common case - ethernet-layer netif driver. */
99 * Enumeration of pbuf types
102 /** pbuf data is stored in RAM, used for TX mostly, struct pbuf and its payload
103 are allocated in one piece of contiguous memory (so the first payload byte
104 can be calculated from struct pbuf).
105 pbuf_alloc() allocates PBUF_RAM pbufs as unchained pbufs (although that might
106 change in future versions).
107 This should be used for all OUTGOING packets (TX).*/
109 /** pbuf data is stored in ROM, i.e. struct pbuf and its payload are located in
110 totally different memory areas. Since it points to ROM, payload does not
111 have to be copied when queued for transmission. */
113 /** pbuf comes from the pbuf pool. Much like PBUF_ROM but payload might change
114 so it has to be duplicated when queued before transmitting, depending on
115 who has a 'ref' to it. */
117 /** pbuf payload refers to RAM. This one comes from a pool and should be used
118 for RX. Payload can be chained (scatter-gather RX) but like PBUF_RAM, struct
119 pbuf and its payload are allocated in one piece of contiguous memory (so
120 the first payload byte can be calculated from struct pbuf).
121 Don't use this for TX, if the pool becomes empty e.g. because of TCP queuing,
122 you are unable to receive TCP acks! */
127 /** indicates this packet's data should be immediately passed to the application */
128 #define PBUF_FLAG_PUSH 0x01U
129 /** indicates this is a custom pbuf: pbuf_free calls pbuf_custom->custom_free_function()
130 when the last reference is released (plus custom PBUF_RAM cannot be trimmed) */
131 #define PBUF_FLAG_IS_CUSTOM 0x02U
132 /** indicates this pbuf is UDP multicast to be looped back */
133 #define PBUF_FLAG_MCASTLOOP 0x04U
134 /** indicates this pbuf was received as link-level broadcast */
135 #define PBUF_FLAG_LLBCAST 0x08U
136 /** indicates this pbuf was received as link-level multicast */
137 #define PBUF_FLAG_LLMCAST 0x10U
138 /** indicates this pbuf includes a TCP FIN flag */
139 #define PBUF_FLAG_TCP_FIN 0x20U
141 /** Main packet buffer struct */
143 /** next pbuf in singly linked pbuf chain */
146 /** pointer to the actual data in the buffer */
150 * total length of this buffer and all next buffers in chain
151 * belonging to the same packet.
153 * For non-queue packet chains this is the invariant:
154 * p->tot_len == p->len + (p->next? p->next->tot_len: 0)
158 /** length of this buffer */
161 /** pbuf_type as u8_t instead of enum to save space */
162 u8_t /*pbuf_type*/ type;
168 * the reference count always equals the number of pointers
169 * that refer to this pbuf. This can be pointers from an application,
170 * the stack itself, or pbuf->next pointers from a chain.
174 #if LWIP_PBUF_FROM_CUSTOM_POOLS
175 /** Pool from which pbuf was allocated */
181 /** Helper struct for const-correctness only.
182 * The only meaning of this one is to provide a const payload pointer
186 /** next pbuf in singly linked pbuf chain */
189 /** pointer to the actual data in the buffer */
193 #if LWIP_SUPPORT_CUSTOM_PBUF
194 /** Prototype for a function to free a custom pbuf */
195 typedef void (*pbuf_free_custom_fn)(struct pbuf *p);
197 /** A custom pbuf: like a pbuf, but following a function pointer to free it. */
199 /** The actual pbuf */
201 /** This function is called when pbuf_free deallocates this pbuf(_custom) */
202 pbuf_free_custom_fn custom_free_function;
204 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
206 /** Define this to 0 to prevent freeing ooseq pbufs when the PBUF_POOL is empty */
207 #ifndef PBUF_POOL_FREE_OOSEQ
208 #define PBUF_POOL_FREE_OOSEQ 1
209 #endif /* PBUF_POOL_FREE_OOSEQ */
210 #if LWIP_TCP && TCP_QUEUE_OOSEQ && NO_SYS && PBUF_POOL_FREE_OOSEQ
211 extern volatile u8_t pbuf_free_ooseq_pending;
212 void pbuf_free_ooseq(void);
213 /** When not using sys_check_timeouts(), call PBUF_CHECK_FREE_OOSEQ()
214 at regular intervals from main level to check if ooseq pbufs need to be
216 #define PBUF_CHECK_FREE_OOSEQ() do { if(pbuf_free_ooseq_pending) { \
217 /* pbuf_alloc() reported PBUF_POOL to be empty -> try to free some \
218 ooseq queued pbufs now */ \
219 pbuf_free_ooseq(); }}while(0)
220 #else /* LWIP_TCP && TCP_QUEUE_OOSEQ && NO_SYS && PBUF_POOL_FREE_OOSEQ */
221 /* Otherwise declare an empty PBUF_CHECK_FREE_OOSEQ */
222 #define PBUF_CHECK_FREE_OOSEQ()
223 #endif /* LWIP_TCP && TCP_QUEUE_OOSEQ && NO_SYS && PBUF_POOL_FREE_OOSEQ*/
225 /* Initializes the pbuf module. This call is empty for now, but may not be in future. */
228 struct pbuf *pbuf_alloc(pbuf_layer l, u16_t length, pbuf_type type);
229 #if LWIP_SUPPORT_CUSTOM_PBUF
230 struct pbuf *pbuf_alloced_custom(pbuf_layer l, u16_t length, pbuf_type type,
231 struct pbuf_custom *p, void *payload_mem,
232 u16_t payload_mem_len);
233 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
234 struct pbuf *pbuf_rightsize(struct pbuf *p, s16_t offset);
235 void pbuf_realloc(struct pbuf *p, u16_t size);
236 u8_t pbuf_header(struct pbuf *p, s16_t header_size);
237 u8_t pbuf_header_force(struct pbuf *p, s16_t header_size);
238 void pbuf_ref(struct pbuf *p);
239 u8_t pbuf_free(struct pbuf *p);
240 u16_t pbuf_clen(const struct pbuf *p);
241 void pbuf_cat(struct pbuf *head, struct pbuf *tail);
242 void pbuf_chain(struct pbuf *head, struct pbuf *tail);
243 struct pbuf *pbuf_dechain(struct pbuf *p);
244 err_t pbuf_copy(struct pbuf *p_to, const struct pbuf *p_from);
245 u16_t pbuf_copy_partial(const struct pbuf *p, void *dataptr, u16_t len, u16_t offset);
246 err_t pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len);
247 err_t pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset);
248 struct pbuf *pbuf_skip(struct pbuf* in, u16_t in_offset, u16_t* out_offset);
249 struct pbuf *pbuf_coalesce(struct pbuf *p, pbuf_layer layer);
250 #if LWIP_CHECKSUM_ON_COPY
251 err_t pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr,
252 u16_t len, u16_t *chksum);
253 #endif /* LWIP_CHECKSUM_ON_COPY */
254 #if LWIP_TCP && TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
255 void pbuf_split_64k(struct pbuf *p, struct pbuf **rest);
256 #endif /* LWIP_TCP && TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
258 u8_t pbuf_get_at(const struct pbuf* p, u16_t offset);
259 int pbuf_try_get_at(const struct pbuf* p, u16_t offset);
260 void pbuf_put_at(struct pbuf* p, u16_t offset, u8_t data);
261 u16_t pbuf_memcmp(const struct pbuf* p, u16_t offset, const void* s2, u16_t n);
262 u16_t pbuf_memfind(const struct pbuf* p, const void* mem, u16_t mem_len, u16_t start_offset);
263 u16_t pbuf_strstr(const struct pbuf* p, const char* substr);
269 #endif /* LWIP_HDR_PBUF_H */