ofpbuf: Add ofpbuf_init_dpdk()
[sliver-openvswitch.git] / lib / ofpbuf.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef OFPBUF_H
18 #define OFPBUF_H 1
19
20 #include <stddef.h>
21 #include <stdint.h>
22 #include "list.h"
23 #include "packets.h"
24 #include "util.h"
25 #include "netdev-dpdk.h"
26
27 #ifdef  __cplusplus
28 extern "C" {
29 #endif
30
31 enum OVS_PACKED_ENUM ofpbuf_source {
32     OFPBUF_MALLOC,              /* Obtained via malloc(). */
33     OFPBUF_STACK,               /* Un-movable stack space or static buffer. */
34     OFPBUF_STUB,                /* Starts on stack, may expand into heap. */
35     OFPBUF_DPDK,                /* buffer data is from DPDK allocated memory.
36                                    ref to build_ofpbuf() in netdev-dpdk. */
37 };
38
39 /* Buffer for holding arbitrary data.  An ofpbuf is automatically reallocated
40  * as necessary if it grows too large for the available memory. */
41 struct ofpbuf {
42     void *base;                 /* First byte of allocated space. */
43     uint32_t allocated;         /* Number of bytes allocated. */
44     uint32_t size;              /* Number of bytes in use. */
45     void *data;                 /* First byte actually in use. */
46
47     void *l2;                   /* Link-level header. */
48     uint16_t l2_5_ofs;          /* MPLS label stack offset from l2, or
49                                  * UINT16_MAX */
50     uint16_t l3_ofs;            /* Network-level header offset from l2, or
51                                  * UINT16_MAX. */
52     uint16_t l4_ofs;            /* Transport-level header offset from l2, or
53                                    UINT16_MAX. */
54     enum ofpbuf_source source;  /* Source of memory allocated as 'base'. */
55     struct list list_node;      /* Private list element for use by owner. */
56 #ifdef DPDK_NETDEV
57     void *private_p;            /* private pointer for use by dpdk */
58 #endif
59 };
60
61 static inline void * ofpbuf_data(const struct ofpbuf *);
62 static inline void ofpbuf_set_data(struct ofpbuf *, void *);
63 static inline void * ofpbuf_base(const struct ofpbuf *);
64 static inline void ofpbuf_set_base(struct ofpbuf *, void *);
65
66 static inline uint32_t ofpbuf_size(const struct ofpbuf *);
67 static inline void ofpbuf_set_size(struct ofpbuf *, uint32_t);
68
69 void * ofpbuf_resize_l2(struct ofpbuf *, int increment);
70 void * ofpbuf_resize_l2_5(struct ofpbuf *, int increment);
71 static inline void * ofpbuf_get_l2_5(const struct ofpbuf *);
72 static inline void ofpbuf_set_l2_5(struct ofpbuf *, void *);
73 static inline void * ofpbuf_get_l3(const struct ofpbuf *);
74 static inline void ofpbuf_set_l3(struct ofpbuf *, void *);
75 static inline void * ofpbuf_get_l4(const struct ofpbuf *);
76 static inline void ofpbuf_set_l4(struct ofpbuf *, void *);
77 static inline size_t ofpbuf_get_l4_size(const struct ofpbuf *);
78 static inline const void *ofpbuf_get_tcp_payload(const struct ofpbuf *);
79 static inline const void *ofpbuf_get_udp_payload(const struct ofpbuf *);
80 static inline const void *ofpbuf_get_sctp_payload(const struct ofpbuf *);
81 static inline const void *ofpbuf_get_icmp_payload(const struct ofpbuf *);
82
83 void ofpbuf_use(struct ofpbuf *, void *, size_t);
84 void ofpbuf_use_stack(struct ofpbuf *, void *, size_t);
85 void ofpbuf_use_stub(struct ofpbuf *, void *, size_t);
86 void ofpbuf_use_const(struct ofpbuf *, const void *, size_t);
87
88 void ofpbuf_init_dpdk(struct ofpbuf *b, size_t allocated);
89
90 void ofpbuf_init(struct ofpbuf *, size_t);
91 void ofpbuf_uninit(struct ofpbuf *);
92 static inline void *ofpbuf_get_uninit_pointer(struct ofpbuf *);
93 void ofpbuf_reinit(struct ofpbuf *, size_t);
94
95 struct ofpbuf *ofpbuf_new(size_t);
96 struct ofpbuf *ofpbuf_new_with_headroom(size_t, size_t headroom);
97 struct ofpbuf *ofpbuf_clone(const struct ofpbuf *);
98 struct ofpbuf *ofpbuf_clone_with_headroom(const struct ofpbuf *,
99                                           size_t headroom);
100 struct ofpbuf *ofpbuf_clone_data(const void *, size_t);
101 struct ofpbuf *ofpbuf_clone_data_with_headroom(const void *, size_t,
102                                                size_t headroom);
103 static inline void ofpbuf_delete(struct ofpbuf *);
104
105 static inline void *ofpbuf_at(const struct ofpbuf *, size_t offset,
106                               size_t size);
107 static inline void *ofpbuf_at_assert(const struct ofpbuf *, size_t offset,
108                                      size_t size);
109 static inline void *ofpbuf_tail(const struct ofpbuf *);
110 static inline void *ofpbuf_end(const struct ofpbuf *);
111
112 void *ofpbuf_put_uninit(struct ofpbuf *, size_t);
113 void *ofpbuf_put_zeros(struct ofpbuf *, size_t);
114 void *ofpbuf_put(struct ofpbuf *, const void *, size_t);
115 char *ofpbuf_put_hex(struct ofpbuf *, const char *s, size_t *n);
116 void ofpbuf_reserve(struct ofpbuf *, size_t);
117 void ofpbuf_reserve_with_tailroom(struct ofpbuf *b, size_t headroom,
118                                   size_t tailroom);
119 void *ofpbuf_push_uninit(struct ofpbuf *b, size_t);
120 void *ofpbuf_push_zeros(struct ofpbuf *, size_t);
121 void *ofpbuf_push(struct ofpbuf *b, const void *, size_t);
122
123 static inline size_t ofpbuf_headroom(const struct ofpbuf *);
124 static inline size_t ofpbuf_tailroom(const struct ofpbuf *);
125 void ofpbuf_prealloc_headroom(struct ofpbuf *, size_t);
126 void ofpbuf_prealloc_tailroom(struct ofpbuf *, size_t);
127 void ofpbuf_trim(struct ofpbuf *);
128 void ofpbuf_padto(struct ofpbuf *, size_t);
129 void ofpbuf_shift(struct ofpbuf *, int);
130
131 static inline void ofpbuf_clear(struct ofpbuf *);
132 static inline void *ofpbuf_pull(struct ofpbuf *, size_t);
133 static inline void *ofpbuf_try_pull(struct ofpbuf *, size_t);
134
135 void *ofpbuf_steal_data(struct ofpbuf *);
136
137 char *ofpbuf_to_string(const struct ofpbuf *, size_t maxbytes);
138 static inline struct ofpbuf *ofpbuf_from_list(const struct list *);
139 void ofpbuf_list_delete(struct list *);
140 static inline bool ofpbuf_equal(const struct ofpbuf *, const struct ofpbuf *);
141
142 \f
143 /* Returns a pointer that may be passed to free() to accomplish the same thing
144  * as ofpbuf_uninit(b).  The return value is a null pointer if ofpbuf_uninit()
145  * would not free any memory. */
146 static inline void *ofpbuf_get_uninit_pointer(struct ofpbuf *b)
147 {
148     /* XXX: If 'source' is OFPBUF_DPDK memory gets leaked! */
149     return b && b->source == OFPBUF_MALLOC ? ofpbuf_base(b) : NULL;
150 }
151
152 /* Frees memory that 'b' points to, as well as 'b' itself. */
153 static inline void ofpbuf_delete(struct ofpbuf *b)
154 {
155     if (b) {
156         ofpbuf_uninit(b);
157         free(b);
158     }
159 }
160
161 /* If 'b' contains at least 'offset + size' bytes of data, returns a pointer to
162  * byte 'offset'.  Otherwise, returns a null pointer. */
163 static inline void *ofpbuf_at(const struct ofpbuf *b, size_t offset,
164                               size_t size)
165 {
166     return offset + size <= ofpbuf_size(b) ? (char *) ofpbuf_data(b) + offset : NULL;
167 }
168
169 /* Returns a pointer to byte 'offset' in 'b', which must contain at least
170  * 'offset + size' bytes of data. */
171 static inline void *ofpbuf_at_assert(const struct ofpbuf *b, size_t offset,
172                                      size_t size)
173 {
174     ovs_assert(offset + size <= ofpbuf_size(b));
175     return ((char *) ofpbuf_data(b)) + offset;
176 }
177
178 /* Returns the byte following the last byte of data in use in 'b'. */
179 static inline void *ofpbuf_tail(const struct ofpbuf *b)
180 {
181     return (char *) ofpbuf_data(b) + ofpbuf_size(b);
182 }
183
184 /* Returns the byte following the last byte allocated for use (but not
185  * necessarily in use) by 'b'. */
186 static inline void *ofpbuf_end(const struct ofpbuf *b)
187 {
188     return (char *) ofpbuf_base(b) + b->allocated;
189 }
190
191 /* Returns the number of bytes of headroom in 'b', that is, the number of bytes
192  * of unused space in ofpbuf 'b' before the data that is in use.  (Most
193  * commonly, the data in a ofpbuf is at its beginning, and thus the ofpbuf's
194  * headroom is 0.) */
195 static inline size_t ofpbuf_headroom(const struct ofpbuf *b)
196 {
197     return (char*)ofpbuf_data(b) - (char*)ofpbuf_base(b);
198 }
199
200 /* Returns the number of bytes that may be appended to the tail end of ofpbuf
201  * 'b' before the ofpbuf must be reallocated. */
202 static inline size_t ofpbuf_tailroom(const struct ofpbuf *b)
203 {
204     return (char*)ofpbuf_end(b) - (char*)ofpbuf_tail(b);
205 }
206
207 /* Clears any data from 'b'. */
208 static inline void ofpbuf_clear(struct ofpbuf *b)
209 {
210     ofpbuf_set_data(b, ofpbuf_base(b));
211     ofpbuf_set_size(b, 0);
212 }
213
214 /* Removes 'size' bytes from the head end of 'b', which must contain at least
215  * 'size' bytes of data.  Returns the first byte of data removed. */
216 static inline void *ofpbuf_pull(struct ofpbuf *b, size_t size)
217 {
218     void *data = ofpbuf_data(b);
219     ovs_assert(ofpbuf_size(b) >= size);
220     ofpbuf_set_data(b, (char*)ofpbuf_data(b) + size);
221     ofpbuf_set_size(b, ofpbuf_size(b) - size);
222     return data;
223 }
224
225 /* If 'b' has at least 'size' bytes of data, removes that many bytes from the
226  * head end of 'b' and returns the first byte removed.  Otherwise, returns a
227  * null pointer without modifying 'b'. */
228 static inline void *ofpbuf_try_pull(struct ofpbuf *b, size_t size)
229 {
230     return ofpbuf_size(b) >= size ? ofpbuf_pull(b, size) : NULL;
231 }
232
233 static inline struct ofpbuf *ofpbuf_from_list(const struct list *list)
234 {
235     return CONTAINER_OF(list, struct ofpbuf, list_node);
236 }
237
238 static inline bool ofpbuf_equal(const struct ofpbuf *a, const struct ofpbuf *b)
239 {
240     return ofpbuf_size(a) == ofpbuf_size(b) &&
241            memcmp(ofpbuf_data(a), ofpbuf_data(b), ofpbuf_size(a)) == 0;
242 }
243
244 static inline void * ofpbuf_get_l2_5(const struct ofpbuf *b)
245 {
246     return b->l2_5_ofs != UINT16_MAX ? (char *)b->l2 + b->l2_5_ofs : NULL;
247 }
248
249 static inline void ofpbuf_set_l2_5(struct ofpbuf *b, void *l2_5)
250 {
251     b->l2_5_ofs = l2_5 ? (char *)l2_5 - (char *)b->l2 : UINT16_MAX;
252 }
253
254 static inline void * ofpbuf_get_l3(const struct ofpbuf *b)
255 {
256     return b->l3_ofs != UINT16_MAX ? (char *)b->l2 + b->l3_ofs : NULL;
257 }
258
259 static inline void ofpbuf_set_l3(struct ofpbuf *b, void *l3)
260 {
261     b->l3_ofs = l3 ? (char *)l3 - (char *)b->l2 : UINT16_MAX;
262 }
263
264 static inline void * ofpbuf_get_l4(const struct ofpbuf *b)
265 {
266     return b->l4_ofs != UINT16_MAX ? (char *)b->l2 + b->l4_ofs : NULL;
267 }
268
269 static inline void ofpbuf_set_l4(struct ofpbuf *b, void *l4)
270 {
271     b->l4_ofs = l4 ? (char *)l4 - (char *)b->l2 : UINT16_MAX;
272 }
273
274 static inline size_t ofpbuf_get_l4_size(const struct ofpbuf *b)
275 {
276     return b->l4_ofs != UINT16_MAX
277         ? (const char *)ofpbuf_tail(b) - (const char *)ofpbuf_get_l4(b) : 0;
278 }
279
280 static inline const void *ofpbuf_get_tcp_payload(const struct ofpbuf *b)
281 {
282     size_t l4_size = ofpbuf_get_l4_size(b);
283
284     if (OVS_LIKELY(l4_size >= TCP_HEADER_LEN)) {
285         struct tcp_header *tcp = ofpbuf_get_l4(b);
286         int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
287
288         if (OVS_LIKELY(tcp_len >= TCP_HEADER_LEN && tcp_len <= l4_size)) {
289             return (const char *)tcp + tcp_len;
290         }
291     }
292     return NULL;
293 }
294
295 static inline const void *ofpbuf_get_udp_payload(const struct ofpbuf *b)
296 {
297     return OVS_LIKELY(ofpbuf_get_l4_size(b) >= UDP_HEADER_LEN)
298         ? (const char *)ofpbuf_get_l4(b) + UDP_HEADER_LEN : NULL;
299 }
300
301 static inline const void *ofpbuf_get_sctp_payload(const struct ofpbuf *b)
302 {
303     return OVS_LIKELY(ofpbuf_get_l4_size(b) >= SCTP_HEADER_LEN)
304         ? (const char *)ofpbuf_get_l4(b) + SCTP_HEADER_LEN : NULL;
305 }
306
307 static inline const void *ofpbuf_get_icmp_payload(const struct ofpbuf *b)
308 {
309     return OVS_LIKELY(ofpbuf_get_l4_size(b) >= ICMP_HEADER_LEN)
310         ? (const char *)ofpbuf_get_l4(b) + ICMP_HEADER_LEN : NULL;
311 }
312
313 static inline void * ofpbuf_data(const struct ofpbuf *b)
314 {
315     return b->data;
316 }
317
318 static inline void ofpbuf_set_data(struct ofpbuf *b, void *d)
319 {
320     b->data = d;
321 }
322
323 static inline void * ofpbuf_base(const struct ofpbuf *b)
324 {
325     return b->base;
326 }
327
328 static inline void ofpbuf_set_base(struct ofpbuf *b, void *d)
329 {
330     b->base = d;
331 }
332
333 static inline uint32_t ofpbuf_size(const struct ofpbuf *b)
334 {
335     return b->size;
336 }
337
338 static inline void ofpbuf_set_size(struct ofpbuf *b, uint32_t v)
339 {
340     b->size = v;
341 }
342
343 #ifdef  __cplusplus
344 }
345 #endif
346
347 #endif /* ofpbuf.h */