2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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 OFPBUF_DPDK, /* buffer data is from DPDK allocated memory.
35 ref to build_ofpbuf() in netdev-dpdk. */
38 /* Buffer for holding arbitrary data. An ofpbuf is automatically reallocated
39 * as necessary if it grows too large for the available memory. */
41 void *base; /* First byte of allocated space. */
42 uint32_t allocated; /* Number of bytes allocated. */
43 uint32_t size; /* Number of bytes in use. */
44 void *data; /* First byte actually in use. */
46 void *l2; /* Link-level header. */
47 uint16_t l2_5_ofs; /* MPLS label stack offset from l2, or
49 uint16_t l3_ofs; /* Network-level header offset from l2, or
51 uint16_t l4_ofs; /* Transport-level header offset from l2, or
53 enum ofpbuf_source source; /* Source of memory allocated as 'base'. */
54 struct list list_node; /* Private list element for use by owner. */
57 void * ofpbuf_resize_l2(struct ofpbuf *, int increment);
58 void * ofpbuf_resize_l2_5(struct ofpbuf *, int increment);
59 static inline void * ofpbuf_get_l2_5(const struct ofpbuf *);
60 static inline void ofpbuf_set_l2_5(struct ofpbuf *, void *);
61 static inline void * ofpbuf_get_l3(const struct ofpbuf *);
62 static inline void ofpbuf_set_l3(struct ofpbuf *, void *);
63 static inline void * ofpbuf_get_l4(const struct ofpbuf *);
64 static inline void ofpbuf_set_l4(struct ofpbuf *, void *);
65 static inline size_t ofpbuf_get_l4_size(const struct ofpbuf *);
66 static inline const void *ofpbuf_get_tcp_payload(const struct ofpbuf *);
67 static inline const void *ofpbuf_get_udp_payload(const struct ofpbuf *);
68 static inline const void *ofpbuf_get_sctp_payload(const struct ofpbuf *);
69 static inline const void *ofpbuf_get_icmp_payload(const struct ofpbuf *);
71 void ofpbuf_use(struct ofpbuf *, void *, size_t);
72 void ofpbuf_use_stack(struct ofpbuf *, void *, size_t);
73 void ofpbuf_use_stub(struct ofpbuf *, void *, size_t);
74 void ofpbuf_use_const(struct ofpbuf *, const void *, size_t);
76 void ofpbuf_init(struct ofpbuf *, size_t);
77 void ofpbuf_uninit(struct ofpbuf *);
78 static inline void *ofpbuf_get_uninit_pointer(struct ofpbuf *);
79 void ofpbuf_reinit(struct ofpbuf *, size_t);
81 struct ofpbuf *ofpbuf_new(size_t);
82 struct ofpbuf *ofpbuf_new_with_headroom(size_t, size_t headroom);
83 struct ofpbuf *ofpbuf_clone(const struct ofpbuf *);
84 struct ofpbuf *ofpbuf_clone_with_headroom(const struct ofpbuf *,
86 struct ofpbuf *ofpbuf_clone_data(const void *, size_t);
87 struct ofpbuf *ofpbuf_clone_data_with_headroom(const void *, size_t,
89 static inline void ofpbuf_delete(struct ofpbuf *);
91 static inline void *ofpbuf_at(const struct ofpbuf *, size_t offset,
93 static inline void *ofpbuf_at_assert(const struct ofpbuf *, size_t offset,
95 static inline void *ofpbuf_tail(const struct ofpbuf *);
96 static inline void *ofpbuf_end(const struct ofpbuf *);
98 void *ofpbuf_put_uninit(struct ofpbuf *, size_t);
99 void *ofpbuf_put_zeros(struct ofpbuf *, size_t);
100 void *ofpbuf_put(struct ofpbuf *, const void *, size_t);
101 char *ofpbuf_put_hex(struct ofpbuf *, const char *s, size_t *n);
102 void ofpbuf_reserve(struct ofpbuf *, size_t);
103 void ofpbuf_reserve_with_tailroom(struct ofpbuf *b, size_t headroom,
105 void *ofpbuf_push_uninit(struct ofpbuf *b, size_t);
106 void *ofpbuf_push_zeros(struct ofpbuf *, size_t);
107 void *ofpbuf_push(struct ofpbuf *b, const void *, size_t);
109 static inline size_t ofpbuf_headroom(const struct ofpbuf *);
110 static inline size_t ofpbuf_tailroom(const struct ofpbuf *);
111 void ofpbuf_prealloc_headroom(struct ofpbuf *, size_t);
112 void ofpbuf_prealloc_tailroom(struct ofpbuf *, size_t);
113 void ofpbuf_trim(struct ofpbuf *);
114 void ofpbuf_padto(struct ofpbuf *, size_t);
115 void ofpbuf_shift(struct ofpbuf *, int);
117 static inline void ofpbuf_clear(struct ofpbuf *);
118 static inline void *ofpbuf_pull(struct ofpbuf *, size_t);
119 static inline void *ofpbuf_try_pull(struct ofpbuf *, size_t);
121 void *ofpbuf_steal_data(struct ofpbuf *);
123 char *ofpbuf_to_string(const struct ofpbuf *, size_t maxbytes);
124 static inline struct ofpbuf *ofpbuf_from_list(const struct list *);
125 void ofpbuf_list_delete(struct list *);
126 static inline bool ofpbuf_equal(const struct ofpbuf *, const struct ofpbuf *);
129 /* Returns a pointer that may be passed to free() to accomplish the same thing
130 * as ofpbuf_uninit(b). The return value is a null pointer if ofpbuf_uninit()
131 * would not free any memory. */
132 static inline void *ofpbuf_get_uninit_pointer(struct ofpbuf *b)
134 /* XXX: If 'source' is OFPBUF_DPDK memory gets leaked! */
135 return b && b->source == OFPBUF_MALLOC ? b->base : NULL;
138 /* Frees memory that 'b' points to, as well as 'b' itself. */
139 static inline void ofpbuf_delete(struct ofpbuf *b)
147 /* If 'b' contains at least 'offset + size' bytes of data, returns a pointer to
148 * byte 'offset'. Otherwise, returns a null pointer. */
149 static inline void *ofpbuf_at(const struct ofpbuf *b, size_t offset,
152 return offset + size <= b->size ? (char *) b->data + offset : NULL;
155 /* Returns a pointer to byte 'offset' in 'b', which must contain at least
156 * 'offset + size' bytes of data. */
157 static inline void *ofpbuf_at_assert(const struct ofpbuf *b, size_t offset,
160 ovs_assert(offset + size <= b->size);
161 return ((char *) b->data) + offset;
164 /* Returns the byte following the last byte of data in use in 'b'. */
165 static inline void *ofpbuf_tail(const struct ofpbuf *b)
167 return (char *) b->data + b->size;
170 /* Returns the byte following the last byte allocated for use (but not
171 * necessarily in use) by 'b'. */
172 static inline void *ofpbuf_end(const struct ofpbuf *b)
174 return (char *) b->base + b->allocated;
177 /* Returns the number of bytes of headroom in 'b', that is, the number of bytes
178 * of unused space in ofpbuf 'b' before the data that is in use. (Most
179 * commonly, the data in a ofpbuf is at its beginning, and thus the ofpbuf's
181 static inline size_t ofpbuf_headroom(const struct ofpbuf *b)
183 return (char*)b->data - (char*)b->base;
186 /* Returns the number of bytes that may be appended to the tail end of ofpbuf
187 * 'b' before the ofpbuf must be reallocated. */
188 static inline size_t ofpbuf_tailroom(const struct ofpbuf *b)
190 return (char*)ofpbuf_end(b) - (char*)ofpbuf_tail(b);
193 /* Clears any data from 'b'. */
194 static inline void ofpbuf_clear(struct ofpbuf *b)
200 /* Removes 'size' bytes from the head end of 'b', which must contain at least
201 * 'size' bytes of data. Returns the first byte of data removed. */
202 static inline void *ofpbuf_pull(struct ofpbuf *b, size_t size)
204 void *data = b->data;
205 ovs_assert(b->size >= size);
206 b->data = (char*)b->data + size;
211 /* If 'b' has at least 'size' bytes of data, removes that many bytes from the
212 * head end of 'b' and returns the first byte removed. Otherwise, returns a
213 * null pointer without modifying 'b'. */
214 static inline void *ofpbuf_try_pull(struct ofpbuf *b, size_t size)
216 return b->size >= size ? ofpbuf_pull(b, size) : NULL;
219 static inline struct ofpbuf *ofpbuf_from_list(const struct list *list)
221 return CONTAINER_OF(list, struct ofpbuf, list_node);
224 static inline bool ofpbuf_equal(const struct ofpbuf *a, const struct ofpbuf *b)
226 return a->size == b->size && memcmp(a->data, b->data, a->size) == 0;
229 static inline void * ofpbuf_get_l2_5(const struct ofpbuf *b)
231 return b->l2_5_ofs != UINT16_MAX ? (char *)b->l2 + b->l2_5_ofs : NULL;
234 static inline void ofpbuf_set_l2_5(struct ofpbuf *b, void *l2_5)
236 b->l2_5_ofs = l2_5 ? (char *)l2_5 - (char *)b->l2 : UINT16_MAX;
239 static inline void * ofpbuf_get_l3(const struct ofpbuf *b)
241 return b->l3_ofs != UINT16_MAX ? (char *)b->l2 + b->l3_ofs : NULL;
244 static inline void ofpbuf_set_l3(struct ofpbuf *b, void *l3)
246 b->l3_ofs = l3 ? (char *)l3 - (char *)b->l2 : UINT16_MAX;
249 static inline void * ofpbuf_get_l4(const struct ofpbuf *b)
251 return b->l4_ofs != UINT16_MAX ? (char *)b->l2 + b->l4_ofs : NULL;
254 static inline void ofpbuf_set_l4(struct ofpbuf *b, void *l4)
256 b->l4_ofs = l4 ? (char *)l4 - (char *)b->l2 : UINT16_MAX;
259 static inline size_t ofpbuf_get_l4_size(const struct ofpbuf *b)
261 return b->l4_ofs != UINT16_MAX
262 ? (const char *)ofpbuf_tail(b) - (const char *)ofpbuf_get_l4(b) : 0;
265 static inline const void *ofpbuf_get_tcp_payload(const struct ofpbuf *b)
267 size_t l4_size = ofpbuf_get_l4_size(b);
269 if (OVS_LIKELY(l4_size >= TCP_HEADER_LEN)) {
270 struct tcp_header *tcp = ofpbuf_get_l4(b);
271 int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
273 if (OVS_LIKELY(tcp_len >= TCP_HEADER_LEN && tcp_len <= l4_size)) {
274 return (const char *)tcp + tcp_len;
280 static inline const void *ofpbuf_get_udp_payload(const struct ofpbuf *b)
282 return OVS_LIKELY(ofpbuf_get_l4_size(b) >= UDP_HEADER_LEN)
283 ? (const char *)ofpbuf_get_l4(b) + UDP_HEADER_LEN : NULL;
286 static inline const void *ofpbuf_get_sctp_payload(const struct ofpbuf *b)
288 return OVS_LIKELY(ofpbuf_get_l4_size(b) >= SCTP_HEADER_LEN)
289 ? (const char *)ofpbuf_get_l4(b) + SCTP_HEADER_LEN : NULL;
292 static inline const void *ofpbuf_get_icmp_payload(const struct ofpbuf *b)
294 return OVS_LIKELY(ofpbuf_get_l4_size(b) >= ICMP_HEADER_LEN)
295 ? (const char *)ofpbuf_get_l4(b) + ICMP_HEADER_LEN : NULL;
302 #endif /* ofpbuf.h */