]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
125638eb | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
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: | |
064af421 | 7 | * |
a14bc59f BP |
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. | |
064af421 BP |
15 | */ |
16 | ||
17 | #ifndef OFPBUF_H | |
18 | #define OFPBUF_H 1 | |
19 | ||
20 | #include <stddef.h> | |
31ac1e59 | 21 | #include <stdint.h> |
b3907fbc | 22 | #include "list.h" |
31ac1e59 | 23 | #include "util.h" |
064af421 | 24 | |
d45e9c65 BP |
25 | #ifdef __cplusplus |
26 | extern "C" { | |
27 | #endif | |
28 | ||
31ac1e59 BP |
29 | enum ofpbuf_source { |
30 | OFPBUF_MALLOC, /* Obtained via malloc(). */ | |
cca408da | 31 | OFPBUF_STACK, /* Un-movable stack space or static buffer. */ |
20ebd771 | 32 | OFPBUF_STUB, /* Starts on stack, may expand into heap. */ |
8a9562d2 PS |
33 | OFPBUF_DPDK, /* buffer data is from DPDK allocated memory. |
34 | ref to build_ofpbuf() in netdev-dpdk. */ | |
31ac1e59 BP |
35 | }; |
36 | ||
064af421 BP |
37 | /* Buffer for holding arbitrary data. An ofpbuf is automatically reallocated |
38 | * as necessary if it grows too large for the available memory. */ | |
39 | struct ofpbuf { | |
31ac1e59 | 40 | void *base; /* First byte of allocated space. */ |
064af421 | 41 | size_t allocated; /* Number of bytes allocated. */ |
31ac1e59 | 42 | enum ofpbuf_source source; /* Source of memory allocated as 'base'. */ |
064af421 BP |
43 | |
44 | void *data; /* First byte actually in use. */ | |
45 | size_t size; /* Number of bytes in use. */ | |
46 | ||
47 | void *l2; /* Link-level header. */ | |
b02475c5 | 48 | void *l2_5; /* MPLS label stack */ |
064af421 BP |
49 | void *l3; /* Network-level header. */ |
50 | void *l4; /* Transport-level header. */ | |
51 | void *l7; /* Application data. */ | |
52 | ||
b3907fbc | 53 | struct list list_node; /* Private list element for use by owner. */ |
d45e9c65 | 54 | void *private_p; /* Private pointer for use by owner. */ |
064af421 BP |
55 | }; |
56 | ||
57 | void ofpbuf_use(struct ofpbuf *, void *, size_t); | |
31ac1e59 | 58 | void ofpbuf_use_stack(struct ofpbuf *, void *, size_t); |
cca408da | 59 | void ofpbuf_use_stub(struct ofpbuf *, void *, size_t); |
0bc9407d | 60 | void ofpbuf_use_const(struct ofpbuf *, const void *, size_t); |
064af421 BP |
61 | |
62 | void ofpbuf_init(struct ofpbuf *, size_t); | |
63 | void ofpbuf_uninit(struct ofpbuf *); | |
cca408da | 64 | void *ofpbuf_get_uninit_pointer(struct ofpbuf *); |
064af421 BP |
65 | void ofpbuf_reinit(struct ofpbuf *, size_t); |
66 | ||
67 | struct ofpbuf *ofpbuf_new(size_t); | |
68efcbec | 68 | struct ofpbuf *ofpbuf_new_with_headroom(size_t, size_t headroom); |
064af421 | 69 | struct ofpbuf *ofpbuf_clone(const struct ofpbuf *); |
68efcbec BP |
70 | struct ofpbuf *ofpbuf_clone_with_headroom(const struct ofpbuf *, |
71 | size_t headroom); | |
064af421 | 72 | struct ofpbuf *ofpbuf_clone_data(const void *, size_t); |
a46c577a BP |
73 | struct ofpbuf *ofpbuf_clone_data_with_headroom(const void *, size_t, |
74 | size_t headroom); | |
064af421 BP |
75 | void ofpbuf_delete(struct ofpbuf *); |
76 | ||
77 | void *ofpbuf_at(const struct ofpbuf *, size_t offset, size_t size); | |
78 | void *ofpbuf_at_assert(const struct ofpbuf *, size_t offset, size_t size); | |
79 | void *ofpbuf_tail(const struct ofpbuf *); | |
80 | void *ofpbuf_end(const struct ofpbuf *); | |
81 | ||
82 | void *ofpbuf_put_uninit(struct ofpbuf *, size_t); | |
83 | void *ofpbuf_put_zeros(struct ofpbuf *, size_t); | |
84 | void *ofpbuf_put(struct ofpbuf *, const void *, size_t); | |
78090f63 | 85 | char *ofpbuf_put_hex(struct ofpbuf *, const char *s, size_t *n); |
064af421 | 86 | void ofpbuf_reserve(struct ofpbuf *, size_t); |
da546e07 JR |
87 | void ofpbuf_reserve_with_tailroom(struct ofpbuf *b, size_t headroom, |
88 | size_t tailroom); | |
064af421 | 89 | void *ofpbuf_push_uninit(struct ofpbuf *b, size_t); |
30f07f1a | 90 | void *ofpbuf_push_zeros(struct ofpbuf *, size_t); |
064af421 BP |
91 | void *ofpbuf_push(struct ofpbuf *b, const void *, size_t); |
92 | ||
5019f688 BP |
93 | size_t ofpbuf_headroom(const struct ofpbuf *); |
94 | size_t ofpbuf_tailroom(const struct ofpbuf *); | |
064af421 BP |
95 | void ofpbuf_prealloc_headroom(struct ofpbuf *, size_t); |
96 | void ofpbuf_prealloc_tailroom(struct ofpbuf *, size_t); | |
97 | void ofpbuf_trim(struct ofpbuf *); | |
63f2140a | 98 | void ofpbuf_padto(struct ofpbuf *, size_t); |
b2348f6d | 99 | void ofpbuf_shift(struct ofpbuf *, int); |
064af421 BP |
100 | |
101 | void ofpbuf_clear(struct ofpbuf *); | |
102 | void *ofpbuf_pull(struct ofpbuf *, size_t); | |
103 | void *ofpbuf_try_pull(struct ofpbuf *, size_t); | |
104 | ||
933369b1 BP |
105 | void *ofpbuf_steal_data(struct ofpbuf *); |
106 | ||
0ab8e15f BP |
107 | char *ofpbuf_to_string(const struct ofpbuf *, size_t maxbytes); |
108 | ||
b3907fbc BP |
109 | static inline struct ofpbuf *ofpbuf_from_list(const struct list *list) |
110 | { | |
111 | return CONTAINER_OF(list, struct ofpbuf, list_node); | |
112 | } | |
113 | void ofpbuf_list_delete(struct list *); | |
114 | ||
df35ec51 EJ |
115 | static inline bool |
116 | ofpbuf_equal(const struct ofpbuf *a, const struct ofpbuf *b) | |
117 | { | |
118 | return a->size == b->size && memcmp(a->data, b->data, a->size) == 0; | |
119 | } | |
120 | ||
d45e9c65 BP |
121 | #ifdef __cplusplus |
122 | } | |
123 | #endif | |
124 | ||
064af421 | 125 | #endif /* ofpbuf.h */ |