dpif_packet: Rename to dp_packet
[cascardo/ovs.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  *
42  * 'frame' and offset conventions:
43  *
44  * Network frames (aka "packets"): 'frame' MUST be set to the start of the
45  *    packet, layer offsets MAY be set as appropriate for the packet.
46  *    Additionally, we assume in many places that the 'frame' and 'data' are
47  *    the same for packets.
48  *
49  * OpenFlow messages: 'frame' points to the start of the OpenFlow
50  *    header, while 'l3_ofs' is the length of the OpenFlow header.
51  *    When parsing, the 'data' will move past these, as data is being
52  *    pulled from the OpenFlow message.
53  *
54  * Actions: When encoding OVS action lists, the 'frame' is used
55  *    as a pointer to the beginning of the current action (see ofpact_put()).
56  *
57  * rconn: Reuses 'frame' as a private pointer while queuing.
58  */
59 struct ofpbuf {
60 #ifdef DPDK_NETDEV
61     struct rte_mbuf mbuf;       /* DPDK mbuf */
62 #else
63     void *base_;                 /* First byte of allocated space. */
64     void *data_;                 /* First byte actually in use. */
65     uint32_t size_;              /* Number of bytes in use. */
66 #endif
67     uint32_t allocated;         /* Number of bytes allocated. */
68
69     void *frame;                /* Packet frame start, or NULL. */
70     enum ofpbuf_source source;  /* Source of memory allocated as 'base'. */
71     uint8_t l2_pad_size;        /* Detected l2 padding size.
72                                  * Padding is non-pullable. */
73     uint16_t l2_5_ofs;          /* MPLS label stack offset from 'frame', or
74                                  * UINT16_MAX */
75     uint16_t l3_ofs;            /* Network-level header offset from 'frame',
76                                    or UINT16_MAX. */
77     uint16_t l4_ofs;            /* Transport-level header offset from 'frame',
78                                    or UINT16_MAX. */
79     struct ovs_list list_node;  /* Private list element for use by owner. */
80 };
81
82 static inline void * ofpbuf_data(const struct ofpbuf *);
83 static inline void ofpbuf_set_data(struct ofpbuf *, void *);
84 static inline void * ofpbuf_base(const struct ofpbuf *);
85 static inline void ofpbuf_set_base(struct ofpbuf *, void *);
86
87 static inline uint32_t ofpbuf_size(const struct ofpbuf *);
88 static inline void ofpbuf_set_size(struct ofpbuf *, uint32_t);
89
90 void * ofpbuf_resize_l2(struct ofpbuf *, int increment);
91 void * ofpbuf_resize_l2_5(struct ofpbuf *, int increment);
92 static inline void * ofpbuf_l2(const struct ofpbuf *);
93 static inline void ofpbuf_set_frame(struct ofpbuf *, void *);
94 static inline uint8_t ofpbuf_l2_pad_size(const struct ofpbuf *);
95 static inline void ofpbuf_set_l2_pad_size(struct ofpbuf *, uint8_t);
96 static inline void * ofpbuf_l2_5(const struct ofpbuf *);
97 static inline void ofpbuf_set_l2_5(struct ofpbuf *, void *);
98 static inline void * ofpbuf_l3(const struct ofpbuf *);
99 static inline void ofpbuf_set_l3(struct ofpbuf *, void *);
100 static inline void * ofpbuf_l4(const struct ofpbuf *);
101 static inline void ofpbuf_set_l4(struct ofpbuf *, void *);
102 static inline size_t ofpbuf_l4_size(const struct ofpbuf *);
103 static inline const void *ofpbuf_get_tcp_payload(const struct ofpbuf *);
104 static inline const void *ofpbuf_get_udp_payload(const struct ofpbuf *);
105 static inline const void *ofpbuf_get_sctp_payload(const struct ofpbuf *);
106 static inline const void *ofpbuf_get_icmp_payload(const struct ofpbuf *);
107 static inline const void *ofpbuf_get_nd_payload(const struct ofpbuf *);
108
109 void ofpbuf_use(struct ofpbuf *, void *, size_t);
110 void ofpbuf_use_stack(struct ofpbuf *, void *, size_t);
111 void ofpbuf_use_stub(struct ofpbuf *, void *, size_t);
112 void ofpbuf_use_const(struct ofpbuf *, const void *, size_t);
113
114 void ofpbuf_init_dpdk(struct ofpbuf *b, size_t allocated);
115
116 void ofpbuf_init(struct ofpbuf *, size_t);
117 void ofpbuf_uninit(struct ofpbuf *);
118 static inline void *ofpbuf_get_uninit_pointer(struct ofpbuf *);
119 void ofpbuf_reinit(struct ofpbuf *, size_t);
120
121 struct ofpbuf *ofpbuf_new(size_t);
122 struct ofpbuf *ofpbuf_new_with_headroom(size_t, size_t headroom);
123 struct ofpbuf *ofpbuf_clone(const struct ofpbuf *);
124 struct ofpbuf *ofpbuf_clone_with_headroom(const struct ofpbuf *,
125                                           size_t headroom);
126 struct ofpbuf *ofpbuf_clone_data(const void *, size_t);
127 struct ofpbuf *ofpbuf_clone_data_with_headroom(const void *, size_t,
128                                                size_t headroom);
129 static inline void ofpbuf_delete(struct ofpbuf *);
130
131 static inline void *ofpbuf_at(const struct ofpbuf *, size_t offset,
132                               size_t size);
133 static inline void *ofpbuf_at_assert(const struct ofpbuf *, size_t offset,
134                                      size_t size);
135 static inline void *ofpbuf_tail(const struct ofpbuf *);
136 static inline void *ofpbuf_end(const struct ofpbuf *);
137
138 void *ofpbuf_put_uninit(struct ofpbuf *, size_t);
139 void *ofpbuf_put_zeros(struct ofpbuf *, size_t);
140 void *ofpbuf_put(struct ofpbuf *, const void *, size_t);
141 char *ofpbuf_put_hex(struct ofpbuf *, const char *s, size_t *n);
142 void ofpbuf_reserve(struct ofpbuf *, size_t);
143 void ofpbuf_reserve_with_tailroom(struct ofpbuf *b, size_t headroom,
144                                   size_t tailroom);
145 void *ofpbuf_push_uninit(struct ofpbuf *b, size_t);
146 void *ofpbuf_push_zeros(struct ofpbuf *, size_t);
147 void *ofpbuf_push(struct ofpbuf *b, const void *, size_t);
148
149 static inline size_t ofpbuf_headroom(const struct ofpbuf *);
150 static inline size_t ofpbuf_tailroom(const struct ofpbuf *);
151 void ofpbuf_prealloc_headroom(struct ofpbuf *, size_t);
152 void ofpbuf_prealloc_tailroom(struct ofpbuf *, size_t);
153 void ofpbuf_trim(struct ofpbuf *);
154 void ofpbuf_padto(struct ofpbuf *, size_t);
155 void ofpbuf_shift(struct ofpbuf *, int);
156
157 static inline void ofpbuf_clear(struct ofpbuf *);
158 static inline void *ofpbuf_pull(struct ofpbuf *, size_t);
159 static inline void *ofpbuf_try_pull(struct ofpbuf *, size_t);
160
161 void *ofpbuf_steal_data(struct ofpbuf *);
162
163 char *ofpbuf_to_string(const struct ofpbuf *, size_t maxbytes);
164 static inline struct ofpbuf *ofpbuf_from_list(const struct ovs_list *);
165 void ofpbuf_list_delete(struct ovs_list *);
166 static inline bool ofpbuf_equal(const struct ofpbuf *, const struct ofpbuf *);
167
168 \f
169 /* Returns a pointer that may be passed to free() to accomplish the same thing
170  * as ofpbuf_uninit(b).  The return value is a null pointer if ofpbuf_uninit()
171  * would not free any memory. */
172 static inline void *ofpbuf_get_uninit_pointer(struct ofpbuf *b)
173 {
174     /* XXX: If 'source' is OFPBUF_DPDK memory gets leaked! */
175     return b && b->source == OFPBUF_MALLOC ? ofpbuf_base(b) : NULL;
176 }
177
178 /* Frees memory that 'b' points to, as well as 'b' itself. */
179 static inline void ofpbuf_delete(struct ofpbuf *b)
180 {
181     if (b) {
182         if (b->source == OFPBUF_DPDK) {
183             /* If this ofpbuf was allocated by DPDK it must have been
184              * created as a dp_packet */
185             free_dpdk_buf((struct dp_packet*) b);
186             return;
187         }
188
189         ofpbuf_uninit(b);
190         free(b);
191     }
192 }
193
194 /* If 'b' contains at least 'offset + size' bytes of data, returns a pointer to
195  * byte 'offset'.  Otherwise, returns a null pointer. */
196 static inline void *ofpbuf_at(const struct ofpbuf *b, size_t offset,
197                               size_t size)
198 {
199     return offset + size <= ofpbuf_size(b) ? (char *) ofpbuf_data(b) + offset : NULL;
200 }
201
202 /* Returns a pointer to byte 'offset' in 'b', which must contain at least
203  * 'offset + size' bytes of data. */
204 static inline void *ofpbuf_at_assert(const struct ofpbuf *b, size_t offset,
205                                      size_t size)
206 {
207     ovs_assert(offset + size <= ofpbuf_size(b));
208     return ((char *) ofpbuf_data(b)) + offset;
209 }
210
211 /* Returns a pointer to byte following the last byte of data in use in 'b'. */
212 static inline void *ofpbuf_tail(const struct ofpbuf *b)
213 {
214     return (char *) ofpbuf_data(b) + ofpbuf_size(b);
215 }
216
217 /* Returns a pointer to byte following the last byte allocated for use (but
218  * not necessarily in use) in 'b'. */
219 static inline void *ofpbuf_end(const struct ofpbuf *b)
220 {
221     return (char *) ofpbuf_base(b) + b->allocated;
222 }
223
224 /* Returns the number of bytes of headroom in 'b', that is, the number of bytes
225  * of unused space in ofpbuf 'b' before the data that is in use.  (Most
226  * commonly, the data in a ofpbuf is at its beginning, and thus the ofpbuf's
227  * headroom is 0.) */
228 static inline size_t ofpbuf_headroom(const struct ofpbuf *b)
229 {
230     return (char*)ofpbuf_data(b) - (char*)ofpbuf_base(b);
231 }
232
233 /* Returns the number of bytes that may be appended to the tail end of ofpbuf
234  * 'b' before the ofpbuf must be reallocated. */
235 static inline size_t ofpbuf_tailroom(const struct ofpbuf *b)
236 {
237     return (char*)ofpbuf_end(b) - (char*)ofpbuf_tail(b);
238 }
239
240 /* Clears any data from 'b'. */
241 static inline void ofpbuf_clear(struct ofpbuf *b)
242 {
243     ofpbuf_set_data(b, ofpbuf_base(b));
244     ofpbuf_set_size(b, 0);
245 }
246
247 /* Removes 'size' bytes from the head end of 'b', which must contain at least
248  * 'size' bytes of data.  Returns the first byte of data removed. */
249 static inline void *ofpbuf_pull(struct ofpbuf *b, size_t size)
250 {
251     void *data = ofpbuf_data(b);
252     ovs_assert(ofpbuf_size(b) - ofpbuf_l2_pad_size(b) >= size);
253     ofpbuf_set_data(b, (char*)ofpbuf_data(b) + size);
254     ofpbuf_set_size(b, ofpbuf_size(b) - size);
255     return data;
256 }
257
258 /* If 'b' has at least 'size' bytes of data, removes that many bytes from the
259  * head end of 'b' and returns the first byte removed.  Otherwise, returns a
260  * null pointer without modifying 'b'. */
261 static inline void *ofpbuf_try_pull(struct ofpbuf *b, size_t size)
262 {
263     return ofpbuf_size(b) - ofpbuf_l2_pad_size(b) >= size
264         ? ofpbuf_pull(b, size) : NULL;
265 }
266
267 static inline struct ofpbuf *ofpbuf_from_list(const struct ovs_list *list)
268 {
269     return CONTAINER_OF(list, struct ofpbuf, list_node);
270 }
271
272 static inline bool ofpbuf_equal(const struct ofpbuf *a, const struct ofpbuf *b)
273 {
274     return ofpbuf_size(a) == ofpbuf_size(b) &&
275            memcmp(ofpbuf_data(a), ofpbuf_data(b), ofpbuf_size(a)) == 0;
276 }
277
278 /* Get the start if the Ethernet frame.  'l3_ofs' marks the end of the l2
279  * headers, so return NULL if it is not set. */
280 static inline void * ofpbuf_l2(const struct ofpbuf *b)
281 {
282     return (b->l3_ofs != UINT16_MAX) ? b->frame : NULL;
283 }
284
285 /* Sets the packet frame start pointer and resets all layer offsets.
286  * l3 offset must be set before 'l2' can be retrieved. */
287 static inline void ofpbuf_set_frame(struct ofpbuf *b, void *packet)
288 {
289     b->frame = packet;
290     b->l2_pad_size = 0;
291     b->l2_5_ofs = UINT16_MAX;
292     b->l3_ofs = UINT16_MAX;
293     b->l4_ofs = UINT16_MAX;
294 }
295
296 static inline uint8_t ofpbuf_l2_pad_size(const struct ofpbuf *b)
297 {
298     return b->l2_pad_size;
299 }
300
301 static inline void ofpbuf_set_l2_pad_size(struct ofpbuf *b, uint8_t pad_size)
302 {
303     ovs_assert(pad_size <= ofpbuf_size(b));
304     b->l2_pad_size = pad_size;
305 }
306
307 static inline void * ofpbuf_l2_5(const struct ofpbuf *b)
308 {
309     return b->l2_5_ofs != UINT16_MAX ? (char *)b->frame + b->l2_5_ofs : NULL;
310 }
311
312 static inline void ofpbuf_set_l2_5(struct ofpbuf *b, void *l2_5)
313 {
314     b->l2_5_ofs = l2_5 ? (char *)l2_5 - (char *)b->frame : UINT16_MAX;
315 }
316
317 static inline void * ofpbuf_l3(const struct ofpbuf *b)
318 {
319     return b->l3_ofs != UINT16_MAX ? (char *)b->frame + b->l3_ofs : NULL;
320 }
321
322 static inline void ofpbuf_set_l3(struct ofpbuf *b, void *l3)
323 {
324     b->l3_ofs = l3 ? (char *)l3 - (char *)b->frame : UINT16_MAX;
325 }
326
327 static inline void * ofpbuf_l4(const struct ofpbuf *b)
328 {
329     return b->l4_ofs != UINT16_MAX ? (char *)b->frame + b->l4_ofs : NULL;
330 }
331
332 static inline void ofpbuf_set_l4(struct ofpbuf *b, void *l4)
333 {
334     b->l4_ofs = l4 ? (char *)l4 - (char *)b->frame : UINT16_MAX;
335 }
336
337 static inline size_t ofpbuf_l4_size(const struct ofpbuf *b)
338 {
339     return b->l4_ofs != UINT16_MAX
340         ? (const char *)ofpbuf_tail(b) - (const char *)ofpbuf_l4(b)
341         - ofpbuf_l2_pad_size(b)
342         : 0;
343 }
344
345 static inline const void *ofpbuf_get_tcp_payload(const struct ofpbuf *b)
346 {
347     size_t l4_size = ofpbuf_l4_size(b);
348
349     if (OVS_LIKELY(l4_size >= TCP_HEADER_LEN)) {
350         struct tcp_header *tcp = ofpbuf_l4(b);
351         int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
352
353         if (OVS_LIKELY(tcp_len >= TCP_HEADER_LEN && tcp_len <= l4_size)) {
354             return (const char *)tcp + tcp_len;
355         }
356     }
357     return NULL;
358 }
359
360 static inline const void *ofpbuf_get_udp_payload(const struct ofpbuf *b)
361 {
362     return OVS_LIKELY(ofpbuf_l4_size(b) >= UDP_HEADER_LEN)
363         ? (const char *)ofpbuf_l4(b) + UDP_HEADER_LEN : NULL;
364 }
365
366 static inline const void *ofpbuf_get_sctp_payload(const struct ofpbuf *b)
367 {
368     return OVS_LIKELY(ofpbuf_l4_size(b) >= SCTP_HEADER_LEN)
369         ? (const char *)ofpbuf_l4(b) + SCTP_HEADER_LEN : NULL;
370 }
371
372 static inline const void *ofpbuf_get_icmp_payload(const struct ofpbuf *b)
373 {
374     return OVS_LIKELY(ofpbuf_l4_size(b) >= ICMP_HEADER_LEN)
375         ? (const char *)ofpbuf_l4(b) + ICMP_HEADER_LEN : NULL;
376 }
377
378 static inline const void *ofpbuf_get_nd_payload(const struct ofpbuf *b)
379 {
380     return OVS_LIKELY(ofpbuf_l4_size(b) >= ND_MSG_LEN)
381         ? (const char *)ofpbuf_l4(b) + ND_MSG_LEN : NULL;
382 }
383
384 #ifdef DPDK_NETDEV
385 BUILD_ASSERT_DECL(offsetof(struct ofpbuf, mbuf) == 0);
386
387 static inline void * ofpbuf_data(const struct ofpbuf *b)
388 {
389     return b->mbuf.pkt.data;
390 }
391
392 static inline void ofpbuf_set_data(struct ofpbuf *b, void *d)
393 {
394     b->mbuf.pkt.data = d;
395 }
396
397 static inline void * ofpbuf_base(const struct ofpbuf *b)
398 {
399     return b->mbuf.buf_addr;
400 }
401
402 static inline void ofpbuf_set_base(struct ofpbuf *b, void *d)
403 {
404     b->mbuf.buf_addr = d;
405 }
406
407 static inline uint32_t ofpbuf_size(const struct ofpbuf *b)
408 {
409     return b->mbuf.pkt.pkt_len;
410 }
411
412 static inline void ofpbuf_set_size(struct ofpbuf *b, uint32_t v)
413 {
414     b->mbuf.pkt.data_len = v;    /* Current seg length. */
415     b->mbuf.pkt.pkt_len = v;     /* Total length of all segments linked to
416                                   * this segment. */
417 }
418
419 #else
420 static inline void * ofpbuf_data(const struct ofpbuf *b)
421 {
422     return b->data_;
423 }
424
425 static inline void ofpbuf_set_data(struct ofpbuf *b, void *d)
426 {
427     b->data_ = d;
428 }
429
430 static inline void * ofpbuf_base(const struct ofpbuf *b)
431 {
432     return b->base_;
433 }
434
435 static inline void ofpbuf_set_base(struct ofpbuf *b, void *d)
436 {
437     b->base_ = d;
438 }
439
440 static inline uint32_t ofpbuf_size(const struct ofpbuf *b)
441 {
442     return b->size_;
443 }
444
445 static inline void ofpbuf_set_size(struct ofpbuf *b, uint32_t v)
446 {
447     b->size_ = v;
448 }
449 #endif
450
451 static inline void ofpbuf_reset_packet(struct ofpbuf *b, int off)
452 {
453     ofpbuf_set_size(b, ofpbuf_size(b) - off);
454     ofpbuf_set_data(b, (void *) ((unsigned char *) b->frame + off));
455     b->frame = NULL;
456     b->l2_5_ofs = b->l3_ofs = b->l4_ofs = UINT16_MAX;
457 }
458
459 #ifdef  __cplusplus
460 }
461 #endif
462
463 #endif /* ofpbuf.h */