Move ofp-parse.h to include/openvswitch directory
[cascardo/ovs.git] / lib / ofpbuf.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2015, 2016 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 "openvswitch/list.h"
23 #include "util.h"
24
25 struct ds;
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 };
36
37 /* Buffer for holding arbitrary data.  An ofpbuf is automatically reallocated
38  * as necessary if it grows too large for the available memory.
39  *
40  * 'header' and 'msg' conventions:
41  *
42  * OpenFlow messages: 'header' points to the start of the OpenFlow
43  *    header, while 'msg' is the OpenFlow msg body.
44  *    When parsing, the 'data' will move past these, as data is being
45  *    pulled from the OpenFlow message.
46  *
47  *    Caution: buffer manipulation of 'struct ofpbuf' must always update
48  *             the 'header' and 'msg' pointers.
49  *
50  *
51  * Actions: When encoding OVS action lists, the 'header' is used
52  *    as a pointer to the beginning of the current action (see ofpact_put()).
53  *
54  * rconn: Reuses 'header' as a private pointer while queuing.
55  */
56 struct ofpbuf {
57     void *base;                 /* First byte of allocated space. */
58     void *data;                 /* First byte actually in use. */
59     uint32_t size;              /* Number of bytes in use. */
60     uint32_t allocated;         /* Number of bytes allocated. */
61
62     void *header;               /* OpenFlow header. */
63     void *msg;                  /* message's body */
64     struct ovs_list list_node;  /* Private list element for use by owner. */
65     enum ofpbuf_source source;  /* Source of memory allocated as 'base'. */
66 };
67
68 /* An initializer for a struct ofpbuf that will be initially empty and uses the
69  * space in STUB (which should be an array) as a stub.  This is the initializer
70  * form of ofpbuf_use_stub().
71  *
72  * Usage example:
73  *
74  *     uint64_t stub[1024 / 8]; // 1 kB stub properly aligned for 64-bit data.
75  *     struct ofpbuf ofpbuf = OFPBUF_STUB_INITIALIZER(stub);
76  */
77 #define OFPBUF_STUB_INITIALIZER(STUB) {         \
78         .base = (STUB),                         \
79         .data = (STUB),                         \
80         .size = 0,                              \
81         .allocated = sizeof (STUB),             \
82         .header = NULL,                         \
83         .msg = NULL,                            \
84         .list_node = OVS_LIST_POISON,           \
85         .source = OFPBUF_STUB,                  \
86     }
87
88 /* An initializer for a struct ofpbuf whose data starts at DATA and continues
89  * for SIZE bytes.  This is appropriate for an ofpbuf that will be used to
90  * inspect existing data, without moving it around or reallocating it, and
91  * generally without modifying it at all.  This is the initializer form of
92  * ofpbuf_use_const().
93  */
94 static inline struct ofpbuf
95 ofpbuf_const_initializer(const void *data, size_t size)
96 {
97     return (struct ofpbuf) {
98         .base = CONST_CAST(void *, data),
99         .data = CONST_CAST(void *, data),
100         .size = size,
101         .allocated = size,
102         .header = NULL,
103         .msg = NULL,
104         .list_node = OVS_LIST_POISON,
105         .source = OFPBUF_STACK,
106     };
107 }
108
109 void ofpbuf_use_ds(struct ofpbuf *, const struct ds *);
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(struct ofpbuf *, size_t);
115 void ofpbuf_uninit(struct ofpbuf *);
116 void ofpbuf_reinit(struct ofpbuf *, size_t);
117
118 struct ofpbuf *ofpbuf_new(size_t);
119 struct ofpbuf *ofpbuf_new_with_headroom(size_t, size_t headroom);
120 struct ofpbuf *ofpbuf_clone(const struct ofpbuf *);
121 struct ofpbuf *ofpbuf_clone_with_headroom(const struct ofpbuf *,
122                                           size_t headroom);
123 struct ofpbuf *ofpbuf_clone_data(const void *, size_t);
124 struct ofpbuf *ofpbuf_clone_data_with_headroom(const void *, size_t,
125                                                size_t headroom);
126 static inline void ofpbuf_delete(struct ofpbuf *);
127
128 static inline void *ofpbuf_at(const struct ofpbuf *, size_t offset,
129                               size_t size);
130 static inline void *ofpbuf_at_assert(const struct ofpbuf *, size_t offset,
131                                      size_t size);
132 static inline void *ofpbuf_tail(const struct ofpbuf *);
133 static inline void *ofpbuf_end(const struct ofpbuf *);
134
135 void *ofpbuf_put_uninit(struct ofpbuf *, size_t);
136 void *ofpbuf_put_zeros(struct ofpbuf *, size_t);
137 void *ofpbuf_put(struct ofpbuf *, const void *, size_t);
138 char *ofpbuf_put_hex(struct ofpbuf *, const char *s, size_t *n);
139 void ofpbuf_reserve(struct ofpbuf *, size_t);
140 void *ofpbuf_push_uninit(struct ofpbuf *b, size_t);
141 void *ofpbuf_push_zeros(struct ofpbuf *, size_t);
142 void *ofpbuf_push(struct ofpbuf *b, const void *, size_t);
143
144 static inline size_t ofpbuf_headroom(const struct ofpbuf *);
145 static inline size_t ofpbuf_tailroom(const struct ofpbuf *);
146 static inline size_t ofpbuf_msgsize(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 ovs_list *);
161 void ofpbuf_list_delete(struct ovs_list *);
162 static inline bool ofpbuf_equal(const struct ofpbuf *, const struct ofpbuf *);
163
164 \f
165 /* Frees memory that 'b' points to, as well as 'b' itself. */
166 static inline void ofpbuf_delete(struct ofpbuf *b)
167 {
168     if (b) {
169         ofpbuf_uninit(b);
170         free(b);
171     }
172 }
173
174 /* If 'b' contains at least 'offset + size' bytes of data, returns a pointer to
175  * byte 'offset'.  Otherwise, returns a null pointer. */
176 static inline void *ofpbuf_at(const struct ofpbuf *b, size_t offset,
177                               size_t size)
178 {
179     return offset + size <= b->size ? (char *) b->data + offset : NULL;
180 }
181
182 /* Returns a pointer to byte 'offset' in 'b', which must contain at least
183  * 'offset + size' bytes of data. */
184 static inline void *ofpbuf_at_assert(const struct ofpbuf *b, size_t offset,
185                                      size_t size)
186 {
187     ovs_assert(offset + size <= b->size);
188     return ((char *) b->data) + offset;
189 }
190
191 /* Returns a pointer to byte following the last byte of data in use in 'b'. */
192 static inline void *ofpbuf_tail(const struct ofpbuf *b)
193 {
194     return (char *) b->data + b->size;
195 }
196
197 /* Returns a pointer to byte following the last byte allocated for use (but
198  * not necessarily in use) in 'b'. */
199 static inline void *ofpbuf_end(const struct ofpbuf *b)
200 {
201     return (char *) b->base + b->allocated;
202 }
203
204 /* Returns the number of bytes of headroom in 'b', that is, the number of bytes
205  * of unused space in ofpbuf 'b' before the data that is in use.  (Most
206  * commonly, the data in a ofpbuf is at its beginning, and thus the ofpbuf's
207  * headroom is 0.) */
208 static inline size_t ofpbuf_headroom(const struct ofpbuf *b)
209 {
210     return (char*)b->data - (char*)b->base;
211 }
212
213 /* Returns the number of bytes that may be appended to the tail end of ofpbuf
214  * 'b' before the ofpbuf must be reallocated. */
215 static inline size_t ofpbuf_tailroom(const struct ofpbuf *b)
216 {
217     return (char*)ofpbuf_end(b) - (char*)ofpbuf_tail(b);
218 }
219
220 /* Returns the number of bytes from 'b->header' to 'b->msg', that is, the
221  * length of 'b''s header. */
222 static inline size_t
223 ofpbuf_headersize(const struct ofpbuf *b)
224 {
225     return (char *)b->msg - (char *)b->header;
226 }
227
228 /* Returns the number of bytes from 'b->msg' to 'b->data + b->size', that is,
229  * the length of the used space in 'b' starting from 'msg'. */
230 static inline size_t
231 ofpbuf_msgsize(const struct ofpbuf *b)
232 {
233     return (char *)ofpbuf_tail(b) - (char *)b->msg;
234 }
235
236 /* Clears any data from 'b'. */
237 static inline void ofpbuf_clear(struct ofpbuf *b)
238 {
239     b->data = b->base;
240     b->size = 0;
241 }
242
243 /* Removes 'size' bytes from the head end of 'b', which must contain at least
244  * 'size' bytes of data.  Returns the first byte of data removed. */
245 static inline void *ofpbuf_pull(struct ofpbuf *b, size_t size)
246 {
247     void *data = b->data;
248     b->data = (char*)b->data + size;
249     b->size = b->size - size;
250     return data;
251 }
252
253 /* If 'b' has at least 'size' bytes of data, removes that many bytes from the
254  * head end of 'b' and returns the first byte removed.  Otherwise, returns a
255  * null pointer without modifying 'b'. */
256 static inline void *ofpbuf_try_pull(struct ofpbuf *b, size_t size)
257 {
258     return b->size >= size ? ofpbuf_pull(b, size) : NULL;
259 }
260
261 static inline struct ofpbuf *ofpbuf_from_list(const struct ovs_list *list)
262 {
263     return CONTAINER_OF(list, struct ofpbuf, list_node);
264 }
265
266 static inline bool ofpbuf_equal(const struct ofpbuf *a, const struct ofpbuf *b)
267 {
268     return a->size == b->size &&
269            memcmp(a->data, b->data, a->size) == 0;
270 }
271
272 #ifdef  __cplusplus
273 }
274 #endif
275
276 #endif /* ofpbuf.h */