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