ofp-prop: Add helpers for u8, be64/u64, flag, and UUID properties.
[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 "list.h"
23 #include "packets.h"
24 #include "util.h"
25
26 #ifdef  __cplusplus
27 extern "C" {
28 #endif
29
30 enum OVS_PACKED_ENUM ofpbuf_source {
31     OFPBUF_MALLOC,              /* Obtained via malloc(). */
32     OFPBUF_STACK,               /* Un-movable stack space or static buffer. */
33     OFPBUF_STUB,                /* Starts on stack, may expand into heap. */
34 };
35
36 /* Buffer for holding arbitrary data.  An ofpbuf is automatically reallocated
37  * as necessary if it grows too large for the available memory.
38  *
39  * 'header' and 'msg' conventions:
40  *
41  * OpenFlow messages: 'header' points to the start of the OpenFlow
42  *    header, while 'msg' is the OpenFlow msg bofy.
43  *    When parsing, the 'data' will move past these, as data is being
44  *    pulled from the OpenFlow message.
45  *
46  *    Caution: buffer manipulation of 'struct ofpbuf' must always update
47  *             the 'header' and 'msg' pointers.
48  *
49  *
50  * Actions: When encoding OVS action lists, the 'header' is used
51  *    as a pointer to the beginning of the current action (see ofpact_put()).
52  *
53  * rconn: Reuses 'header' as a private pointer while queuing.
54  */
55 struct ofpbuf {
56     void *base;                 /* First byte of allocated space. */
57     void *data;                 /* First byte actually in use. */
58     uint32_t size;              /* Number of bytes in use. */
59     uint32_t allocated;         /* Number of bytes allocated. */
60
61     void *header;               /* OpenFlow header. */
62     void *msg;                  /* message's body */
63     struct ovs_list list_node;  /* Private list element for use by owner. */
64     enum ofpbuf_source source;  /* Source of memory allocated as 'base'. */
65 };
66
67 /* An initializer for a struct ofpbuf that will be initially empty and
68  * uses the space in STUB (which should be an array) as a stub.
69  *
70  * Usage example:
71  *
72  *     uint64_t stub[1024 / 8]; // 1 kB stub properly aligned for 64-bit data.
73  *     struct ofpbuf ofpbuf = OFPBUF_STUB_INITIALIZER(stub);
74  */
75 #define OFPBUF_STUB_INITIALIZER(STUB) {         \
76         .base = (STUB),                         \
77         .data = (STUB),                         \
78         .size = 0,                              \
79         .allocated = sizeof (STUB),             \
80         .header = NULL,                         \
81         .msg = NULL,                            \
82         .list_node = OVS_LIST_POISON,           \
83         .source = OFPBUF_STUB,                  \
84     }
85
86 void ofpbuf_use_ds(struct ofpbuf *, const struct ds *);
87 void ofpbuf_use_stack(struct ofpbuf *, void *, size_t);
88 void ofpbuf_use_stub(struct ofpbuf *, void *, size_t);
89 void ofpbuf_use_const(struct ofpbuf *, const void *, size_t);
90
91 void ofpbuf_init(struct ofpbuf *, size_t);
92 void ofpbuf_uninit(struct ofpbuf *);
93 void ofpbuf_reinit(struct ofpbuf *, size_t);
94
95 struct ofpbuf *ofpbuf_new(size_t);
96 struct ofpbuf *ofpbuf_new_with_headroom(size_t, size_t headroom);
97 struct ofpbuf *ofpbuf_clone(const struct ofpbuf *);
98 struct ofpbuf *ofpbuf_clone_with_headroom(const struct ofpbuf *,
99                                           size_t headroom);
100 struct ofpbuf *ofpbuf_clone_data(const void *, size_t);
101 struct ofpbuf *ofpbuf_clone_data_with_headroom(const void *, size_t,
102                                                size_t headroom);
103 static inline void ofpbuf_delete(struct ofpbuf *);
104
105 static inline void *ofpbuf_at(const struct ofpbuf *, size_t offset,
106                               size_t size);
107 static inline void *ofpbuf_at_assert(const struct ofpbuf *, size_t offset,
108                                      size_t size);
109 static inline void *ofpbuf_tail(const struct ofpbuf *);
110 static inline void *ofpbuf_end(const struct ofpbuf *);
111
112 void *ofpbuf_put_uninit(struct ofpbuf *, size_t);
113 void *ofpbuf_put_zeros(struct ofpbuf *, size_t);
114 void *ofpbuf_put(struct ofpbuf *, const void *, size_t);
115 char *ofpbuf_put_hex(struct ofpbuf *, const char *s, size_t *n);
116 void ofpbuf_reserve(struct ofpbuf *, size_t);
117 void *ofpbuf_push_uninit(struct ofpbuf *b, size_t);
118 void *ofpbuf_push_zeros(struct ofpbuf *, size_t);
119 void *ofpbuf_push(struct ofpbuf *b, const void *, size_t);
120
121 static inline size_t ofpbuf_headroom(const struct ofpbuf *);
122 static inline size_t ofpbuf_tailroom(const struct ofpbuf *);
123 static inline size_t ofpbuf_msgsize(const struct ofpbuf *);
124 void ofpbuf_prealloc_headroom(struct ofpbuf *, size_t);
125 void ofpbuf_prealloc_tailroom(struct ofpbuf *, size_t);
126 void ofpbuf_trim(struct ofpbuf *);
127 void ofpbuf_padto(struct ofpbuf *, size_t);
128 void ofpbuf_shift(struct ofpbuf *, int);
129
130 static inline void ofpbuf_clear(struct ofpbuf *);
131 static inline void *ofpbuf_pull(struct ofpbuf *, size_t);
132 static inline void *ofpbuf_try_pull(struct ofpbuf *, size_t);
133
134 void *ofpbuf_steal_data(struct ofpbuf *);
135
136 char *ofpbuf_to_string(const struct ofpbuf *, size_t maxbytes);
137 static inline struct ofpbuf *ofpbuf_from_list(const struct ovs_list *);
138 void ofpbuf_list_delete(struct ovs_list *);
139 static inline bool ofpbuf_equal(const struct ofpbuf *, const struct ofpbuf *);
140
141 \f
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 a pointer to 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 a pointer to byte following the last byte allocated for use (but
175  * not necessarily in use) in '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 /* Returns the number of bytes from 'b->header' to 'b->msg', that is, the
198  * length of 'b''s header. */
199 static inline size_t
200 ofpbuf_headersize(const struct ofpbuf *b)
201 {
202     return (char *)b->msg - (char *)b->header;
203 }
204
205 /* Returns the number of bytes from 'b->msg' to 'b->data + b->size', that is,
206  * the length of the used space in 'b' starting from 'msg'. */
207 static inline size_t
208 ofpbuf_msgsize(const struct ofpbuf *b)
209 {
210     return (char *)ofpbuf_tail(b) - (char *)b->msg;
211 }
212
213 /* Clears any data from 'b'. */
214 static inline void ofpbuf_clear(struct ofpbuf *b)
215 {
216     b->data = b->base;
217     b->size = 0;
218 }
219
220 /* Removes 'size' bytes from the head end of 'b', which must contain at least
221  * 'size' bytes of data.  Returns the first byte of data removed. */
222 static inline void *ofpbuf_pull(struct ofpbuf *b, size_t size)
223 {
224     void *data = b->data;
225     b->data = (char*)b->data + size;
226     b->size = b->size - size;
227     return data;
228 }
229
230 /* If 'b' has at least 'size' bytes of data, removes that many bytes from the
231  * head end of 'b' and returns the first byte removed.  Otherwise, returns a
232  * null pointer without modifying 'b'. */
233 static inline void *ofpbuf_try_pull(struct ofpbuf *b, size_t size)
234 {
235     return b->size >= size ? ofpbuf_pull(b, size) : NULL;
236 }
237
238 static inline struct ofpbuf *ofpbuf_from_list(const struct ovs_list *list)
239 {
240     return CONTAINER_OF(list, struct ofpbuf, list_node);
241 }
242
243 static inline bool ofpbuf_equal(const struct ofpbuf *a, const struct ofpbuf *b)
244 {
245     return a->size == b->size &&
246            memcmp(a->data, b->data, a->size) == 0;
247 }
248
249 #ifdef  __cplusplus
250 }
251 #endif
252
253 #endif /* ofpbuf.h */