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