ofpbuf: Simplify ofpbuf API.
[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
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  * Actions: When encoding OVS action lists, the 'header' is used
47  *    as a pointer to the beginning of the current action (see ofpact_put()).
48  *
49  * rconn: Reuses 'header' as a private pointer while queuing.
50  */
51 struct ofpbuf {
52     void *base;                 /* First byte of allocated space. */
53     void *data;                 /* First byte actually in use. */
54     uint32_t size;              /* Number of bytes in use. */
55     uint32_t allocated;         /* Number of bytes allocated. */
56
57     void *header;               /* OpenFlow header. */
58     void *msg;                  /* message's body */
59     struct ovs_list list_node;  /* Private list element for use by owner. */
60     enum ofpbuf_source source;  /* Source of memory allocated as 'base'. */
61 };
62
63 void ofpbuf_use(struct ofpbuf *, void *, size_t);
64 void ofpbuf_use_stack(struct ofpbuf *, void *, size_t);
65 void ofpbuf_use_stub(struct ofpbuf *, void *, size_t);
66 void ofpbuf_use_const(struct ofpbuf *, const void *, size_t);
67
68 void ofpbuf_init(struct ofpbuf *, size_t);
69 void ofpbuf_uninit(struct ofpbuf *);
70 void ofpbuf_reinit(struct ofpbuf *, size_t);
71
72 struct ofpbuf *ofpbuf_new(size_t);
73 struct ofpbuf *ofpbuf_new_with_headroom(size_t, size_t headroom);
74 struct ofpbuf *ofpbuf_clone(const struct ofpbuf *);
75 struct ofpbuf *ofpbuf_clone_with_headroom(const struct ofpbuf *,
76                                           size_t headroom);
77 struct ofpbuf *ofpbuf_clone_data(const void *, size_t);
78 struct ofpbuf *ofpbuf_clone_data_with_headroom(const void *, size_t,
79                                                size_t headroom);
80 static inline void ofpbuf_delete(struct ofpbuf *);
81
82 static inline void *ofpbuf_at(const struct ofpbuf *, size_t offset,
83                               size_t size);
84 static inline void *ofpbuf_at_assert(const struct ofpbuf *, size_t offset,
85                                      size_t size);
86 static inline void *ofpbuf_tail(const struct ofpbuf *);
87 static inline void *ofpbuf_end(const struct ofpbuf *);
88
89 void *ofpbuf_put_uninit(struct ofpbuf *, size_t);
90 void *ofpbuf_put_zeros(struct ofpbuf *, size_t);
91 void *ofpbuf_put(struct ofpbuf *, const void *, size_t);
92 char *ofpbuf_put_hex(struct ofpbuf *, const char *s, size_t *n);
93 void ofpbuf_reserve(struct ofpbuf *, size_t);
94 void *ofpbuf_push_uninit(struct ofpbuf *b, size_t);
95 void *ofpbuf_push_zeros(struct ofpbuf *, size_t);
96 void *ofpbuf_push(struct ofpbuf *b, const void *, size_t);
97
98 static inline size_t ofpbuf_headroom(const struct ofpbuf *);
99 static inline size_t ofpbuf_tailroom(const struct ofpbuf *);
100 void ofpbuf_prealloc_headroom(struct ofpbuf *, size_t);
101 void ofpbuf_prealloc_tailroom(struct ofpbuf *, size_t);
102 void ofpbuf_trim(struct ofpbuf *);
103 void ofpbuf_padto(struct ofpbuf *, size_t);
104 void ofpbuf_shift(struct ofpbuf *, int);
105
106 static inline void ofpbuf_clear(struct ofpbuf *);
107 static inline void *ofpbuf_pull(struct ofpbuf *, size_t);
108 static inline void *ofpbuf_try_pull(struct ofpbuf *, size_t);
109
110 void *ofpbuf_steal_data(struct ofpbuf *);
111
112 char *ofpbuf_to_string(const struct ofpbuf *, size_t maxbytes);
113 static inline struct ofpbuf *ofpbuf_from_list(const struct ovs_list *);
114 void ofpbuf_list_delete(struct ovs_list *);
115 static inline bool ofpbuf_equal(const struct ofpbuf *, const struct ofpbuf *);
116
117 \f
118 /* Frees memory that 'b' points to, as well as 'b' itself. */
119 static inline void ofpbuf_delete(struct ofpbuf *b)
120 {
121     if (b) {
122         ofpbuf_uninit(b);
123         free(b);
124     }
125 }
126
127 /* If 'b' contains at least 'offset + size' bytes of data, returns a pointer to
128  * byte 'offset'.  Otherwise, returns a null pointer. */
129 static inline void *ofpbuf_at(const struct ofpbuf *b, size_t offset,
130                               size_t size)
131 {
132     return offset + size <= b->size ? (char *) b->data + offset : NULL;
133 }
134
135 /* Returns a pointer to byte 'offset' in 'b', which must contain at least
136  * 'offset + size' bytes of data. */
137 static inline void *ofpbuf_at_assert(const struct ofpbuf *b, size_t offset,
138                                      size_t size)
139 {
140     ovs_assert(offset + size <= b->size);
141     return ((char *) b->data) + offset;
142 }
143
144 /* Returns a pointer to byte following the last byte of data in use in 'b'. */
145 static inline void *ofpbuf_tail(const struct ofpbuf *b)
146 {
147     return (char *) b->data + b->size;
148 }
149
150 /* Returns a pointer to byte following the last byte allocated for use (but
151  * not necessarily in use) in 'b'. */
152 static inline void *ofpbuf_end(const struct ofpbuf *b)
153 {
154     return (char *) b->base + b->allocated;
155 }
156
157 /* Returns the number of bytes of headroom in 'b', that is, the number of bytes
158  * of unused space in ofpbuf 'b' before the data that is in use.  (Most
159  * commonly, the data in a ofpbuf is at its beginning, and thus the ofpbuf's
160  * headroom is 0.) */
161 static inline size_t ofpbuf_headroom(const struct ofpbuf *b)
162 {
163     return (char*)b->data - (char*)b->base;
164 }
165
166 /* Returns the number of bytes that may be appended to the tail end of ofpbuf
167  * 'b' before the ofpbuf must be reallocated. */
168 static inline size_t ofpbuf_tailroom(const struct ofpbuf *b)
169 {
170     return (char*)ofpbuf_end(b) - (char*)ofpbuf_tail(b);
171 }
172
173 /* Clears any data from 'b'. */
174 static inline void ofpbuf_clear(struct ofpbuf *b)
175 {
176     b->data = b->base;
177     b->size = 0;
178 }
179
180 /* Removes 'size' bytes from the head end of 'b', which must contain at least
181  * 'size' bytes of data.  Returns the first byte of data removed. */
182 static inline void *ofpbuf_pull(struct ofpbuf *b, size_t size)
183 {
184     void *data = b->data;
185     b->data = (char*)b->data + size;
186     b->size = b->size - size;
187     return data;
188 }
189
190 /* If 'b' has at least 'size' bytes of data, removes that many bytes from the
191  * head end of 'b' and returns the first byte removed.  Otherwise, returns a
192  * null pointer without modifying 'b'. */
193 static inline void *ofpbuf_try_pull(struct ofpbuf *b, size_t size)
194 {
195     return b->size >= size ? ofpbuf_pull(b, size) : NULL;
196 }
197
198 static inline struct ofpbuf *ofpbuf_from_list(const struct ovs_list *list)
199 {
200     return CONTAINER_OF(list, struct ofpbuf, list_node);
201 }
202
203 static inline bool ofpbuf_equal(const struct ofpbuf *a, const struct ofpbuf *b)
204 {
205     return a->size == b->size &&
206            memcmp(a->data, b->data, a->size) == 0;
207 }
208
209 #ifdef  __cplusplus
210 }
211 #endif
212
213 #endif /* ofpbuf.h */