9beceddd63ec33ac5b61a7b116b356e8aa3d8618
[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 void * ofpbuf_resize_l2(struct ofpbuf *, int increment);
62 void * ofpbuf_resize_l2_5(struct ofpbuf *, int increment);
63 static inline void * ofpbuf_get_l2_5(const struct ofpbuf *);
64 static inline void ofpbuf_set_l2_5(struct ofpbuf *, void *);
65 static inline void * ofpbuf_get_l3(const struct ofpbuf *);
66 static inline void ofpbuf_set_l3(struct ofpbuf *, void *);
67 static inline void * ofpbuf_get_l4(const struct ofpbuf *);
68 static inline void ofpbuf_set_l4(struct ofpbuf *, void *);
69 static inline size_t ofpbuf_get_l4_size(const struct ofpbuf *);
70 static inline const void *ofpbuf_get_tcp_payload(const struct ofpbuf *);
71 static inline const void *ofpbuf_get_udp_payload(const struct ofpbuf *);
72 static inline const void *ofpbuf_get_sctp_payload(const struct ofpbuf *);
73 static inline const void *ofpbuf_get_icmp_payload(const struct ofpbuf *);
74
75 void ofpbuf_use(struct ofpbuf *, void *, size_t);
76 void ofpbuf_use_stack(struct ofpbuf *, void *, size_t);
77 void ofpbuf_use_stub(struct ofpbuf *, void *, size_t);
78 void ofpbuf_use_const(struct ofpbuf *, const void *, size_t);
79
80 void ofpbuf_init(struct ofpbuf *, size_t);
81 void ofpbuf_uninit(struct ofpbuf *);
82 static inline void *ofpbuf_get_uninit_pointer(struct ofpbuf *);
83 void ofpbuf_reinit(struct ofpbuf *, size_t);
84
85 struct ofpbuf *ofpbuf_new(size_t);
86 struct ofpbuf *ofpbuf_new_with_headroom(size_t, size_t headroom);
87 struct ofpbuf *ofpbuf_clone(const struct ofpbuf *);
88 struct ofpbuf *ofpbuf_clone_with_headroom(const struct ofpbuf *,
89                                           size_t headroom);
90 struct ofpbuf *ofpbuf_clone_data(const void *, size_t);
91 struct ofpbuf *ofpbuf_clone_data_with_headroom(const void *, size_t,
92                                                size_t headroom);
93 static inline void ofpbuf_delete(struct ofpbuf *);
94
95 static inline void *ofpbuf_at(const struct ofpbuf *, size_t offset,
96                               size_t size);
97 static inline void *ofpbuf_at_assert(const struct ofpbuf *, size_t offset,
98                                      size_t size);
99 static inline void *ofpbuf_tail(const struct ofpbuf *);
100 static inline void *ofpbuf_end(const struct ofpbuf *);
101
102 void *ofpbuf_put_uninit(struct ofpbuf *, size_t);
103 void *ofpbuf_put_zeros(struct ofpbuf *, size_t);
104 void *ofpbuf_put(struct ofpbuf *, const void *, size_t);
105 char *ofpbuf_put_hex(struct ofpbuf *, const char *s, size_t *n);
106 void ofpbuf_reserve(struct ofpbuf *, size_t);
107 void ofpbuf_reserve_with_tailroom(struct ofpbuf *b, size_t headroom,
108                                   size_t tailroom);
109 void *ofpbuf_push_uninit(struct ofpbuf *b, size_t);
110 void *ofpbuf_push_zeros(struct ofpbuf *, size_t);
111 void *ofpbuf_push(struct ofpbuf *b, const void *, size_t);
112
113 static inline size_t ofpbuf_headroom(const struct ofpbuf *);
114 static inline size_t ofpbuf_tailroom(const struct ofpbuf *);
115 void ofpbuf_prealloc_headroom(struct ofpbuf *, size_t);
116 void ofpbuf_prealloc_tailroom(struct ofpbuf *, size_t);
117 void ofpbuf_trim(struct ofpbuf *);
118 void ofpbuf_padto(struct ofpbuf *, size_t);
119 void ofpbuf_shift(struct ofpbuf *, int);
120
121 static inline void ofpbuf_clear(struct ofpbuf *);
122 static inline void *ofpbuf_pull(struct ofpbuf *, size_t);
123 static inline void *ofpbuf_try_pull(struct ofpbuf *, size_t);
124
125 void *ofpbuf_steal_data(struct ofpbuf *);
126
127 char *ofpbuf_to_string(const struct ofpbuf *, size_t maxbytes);
128 static inline struct ofpbuf *ofpbuf_from_list(const struct list *);
129 void ofpbuf_list_delete(struct list *);
130 static inline bool ofpbuf_equal(const struct ofpbuf *, const struct ofpbuf *);
131
132 \f
133 /* Returns a pointer that may be passed to free() to accomplish the same thing
134  * as ofpbuf_uninit(b).  The return value is a null pointer if ofpbuf_uninit()
135  * would not free any memory. */
136 static inline void *ofpbuf_get_uninit_pointer(struct ofpbuf *b)
137 {
138     /* XXX: If 'source' is OFPBUF_DPDK memory gets leaked! */
139     return b && b->source == OFPBUF_MALLOC ? b->base : NULL;
140 }
141
142 /* Frees memory that 'b' points to, as well as 'b' itself. */
143 static inline void ofpbuf_delete(struct ofpbuf *b)
144 {
145     if (b) {
146         ofpbuf_uninit(b);
147         free(b);
148     }
149 }
150
151 /* If 'b' contains at least 'offset + size' bytes of data, returns a pointer to
152  * byte 'offset'.  Otherwise, returns a null pointer. */
153 static inline void *ofpbuf_at(const struct ofpbuf *b, size_t offset,
154                               size_t size)
155 {
156     return offset + size <= b->size ? (char *) b->data + offset : NULL;
157 }
158
159 /* Returns a pointer to byte 'offset' in 'b', which must contain at least
160  * 'offset + size' bytes of data. */
161 static inline void *ofpbuf_at_assert(const struct ofpbuf *b, size_t offset,
162                                      size_t size)
163 {
164     ovs_assert(offset + size <= b->size);
165     return ((char *) b->data) + offset;
166 }
167
168 /* Returns the byte following the last byte of data in use in 'b'. */
169 static inline void *ofpbuf_tail(const struct ofpbuf *b)
170 {
171     return (char *) b->data + b->size;
172 }
173
174 /* Returns the byte following the last byte allocated for use (but not
175  * necessarily in use) by 'b'. */
176 static inline void *ofpbuf_end(const struct ofpbuf *b)
177 {
178     return (char *) b->base + b->allocated;
179 }
180
181 /* Returns the number of bytes of headroom in 'b', that is, the number of bytes
182  * of unused space in ofpbuf 'b' before the data that is in use.  (Most
183  * commonly, the data in a ofpbuf is at its beginning, and thus the ofpbuf's
184  * headroom is 0.) */
185 static inline size_t ofpbuf_headroom(const struct ofpbuf *b)
186 {
187     return (char*)b->data - (char*)b->base;
188 }
189
190 /* Returns the number of bytes that may be appended to the tail end of ofpbuf
191  * 'b' before the ofpbuf must be reallocated. */
192 static inline size_t ofpbuf_tailroom(const struct ofpbuf *b)
193 {
194     return (char*)ofpbuf_end(b) - (char*)ofpbuf_tail(b);
195 }
196
197 /* Clears any data from 'b'. */
198 static inline void ofpbuf_clear(struct ofpbuf *b)
199 {
200     b->data = b->base;
201     b->size = 0;
202 }
203
204 /* Removes 'size' bytes from the head end of 'b', which must contain at least
205  * 'size' bytes of data.  Returns the first byte of data removed. */
206 static inline void *ofpbuf_pull(struct ofpbuf *b, size_t size)
207 {
208     void *data = b->data;
209     ovs_assert(b->size >= size);
210     b->data = (char*)b->data + size;
211     b->size -= size;
212     return data;
213 }
214
215 /* If 'b' has at least 'size' bytes of data, removes that many bytes from the
216  * head end of 'b' and returns the first byte removed.  Otherwise, returns a
217  * null pointer without modifying 'b'. */
218 static inline void *ofpbuf_try_pull(struct ofpbuf *b, size_t size)
219 {
220     return b->size >= size ? ofpbuf_pull(b, size) : NULL;
221 }
222
223 static inline struct ofpbuf *ofpbuf_from_list(const struct list *list)
224 {
225     return CONTAINER_OF(list, struct ofpbuf, list_node);
226 }
227
228 static inline bool ofpbuf_equal(const struct ofpbuf *a, const struct ofpbuf *b)
229 {
230     return a->size == b->size && memcmp(a->data, b->data, a->size) == 0;
231 }
232
233 static inline void * ofpbuf_get_l2_5(const struct ofpbuf *b)
234 {
235     return b->l2_5_ofs != UINT16_MAX ? (char *)b->l2 + b->l2_5_ofs : NULL;
236 }
237
238 static inline void ofpbuf_set_l2_5(struct ofpbuf *b, void *l2_5)
239 {
240     b->l2_5_ofs = l2_5 ? (char *)l2_5 - (char *)b->l2 : UINT16_MAX;
241 }
242
243 static inline void * ofpbuf_get_l3(const struct ofpbuf *b)
244 {
245     return b->l3_ofs != UINT16_MAX ? (char *)b->l2 + b->l3_ofs : NULL;
246 }
247
248 static inline void ofpbuf_set_l3(struct ofpbuf *b, void *l3)
249 {
250     b->l3_ofs = l3 ? (char *)l3 - (char *)b->l2 : UINT16_MAX;
251 }
252
253 static inline void * ofpbuf_get_l4(const struct ofpbuf *b)
254 {
255     return b->l4_ofs != UINT16_MAX ? (char *)b->l2 + b->l4_ofs : NULL;
256 }
257
258 static inline void ofpbuf_set_l4(struct ofpbuf *b, void *l4)
259 {
260     b->l4_ofs = l4 ? (char *)l4 - (char *)b->l2 : UINT16_MAX;
261 }
262
263 static inline size_t ofpbuf_get_l4_size(const struct ofpbuf *b)
264 {
265     return b->l4_ofs != UINT16_MAX
266         ? (const char *)ofpbuf_tail(b) - (const char *)ofpbuf_get_l4(b) : 0;
267 }
268
269 static inline const void *ofpbuf_get_tcp_payload(const struct ofpbuf *b)
270 {
271     size_t l4_size = ofpbuf_get_l4_size(b);
272
273     if (OVS_LIKELY(l4_size >= TCP_HEADER_LEN)) {
274         struct tcp_header *tcp = ofpbuf_get_l4(b);
275         int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
276
277         if (OVS_LIKELY(tcp_len >= TCP_HEADER_LEN && tcp_len <= l4_size)) {
278             return (const char *)tcp + tcp_len;
279         }
280     }
281     return NULL;
282 }
283
284 static inline const void *ofpbuf_get_udp_payload(const struct ofpbuf *b)
285 {
286     return OVS_LIKELY(ofpbuf_get_l4_size(b) >= UDP_HEADER_LEN)
287         ? (const char *)ofpbuf_get_l4(b) + UDP_HEADER_LEN : NULL;
288 }
289
290 static inline const void *ofpbuf_get_sctp_payload(const struct ofpbuf *b)
291 {
292     return OVS_LIKELY(ofpbuf_get_l4_size(b) >= SCTP_HEADER_LEN)
293         ? (const char *)ofpbuf_get_l4(b) + SCTP_HEADER_LEN : NULL;
294 }
295
296 static inline const void *ofpbuf_get_icmp_payload(const struct ofpbuf *b)
297 {
298     return OVS_LIKELY(ofpbuf_get_l4_size(b) >= ICMP_HEADER_LEN)
299         ? (const char *)ofpbuf_get_l4(b) + ICMP_HEADER_LEN : NULL;
300 }
301
302 #ifdef  __cplusplus
303 }
304 #endif
305
306 #endif /* ofpbuf.h */