]>
Commit | Line | Data |
---|---|---|
1ab39058 | 1 | /* Copyright (c) 2009, 2010, 2011, 2012, 2015, 2016, 2017 Nicira, Inc. |
f85f8ebb BP |
2 | * |
3 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | * you may not use this file except in compliance with the License. | |
5 | * You may obtain a copy of the License at: | |
6 | * | |
7 | * http://www.apache.org/licenses/LICENSE-2.0 | |
8 | * | |
9 | * Unless required by applicable law or agreed to in writing, software | |
10 | * distributed under the License is distributed on an "AS IS" BASIS, | |
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | * See the License for the specific language governing permissions and | |
13 | * limitations under the License. | |
14 | */ | |
15 | ||
16 | #ifndef OVSDB_DATA_H | |
17 | #define OVSDB_DATA_H 1 | |
18 | ||
19 | #include <stdlib.h> | |
20 | #include "compiler.h" | |
21 | #include "ovsdb-types.h" | |
ee89ea7b | 22 | #include "openvswitch/shash.h" |
f85f8ebb | 23 | |
1ab39058 LR |
24 | #define MAX_OVSDB_ATOM_RANGE_SIZE 4096 |
25 | ||
0194f33a | 26 | struct ds; |
f85f8ebb | 27 | struct ovsdb_symbol_table; |
57c8677b | 28 | struct smap; |
f85f8ebb BP |
29 | |
30 | /* One value of an atomic type (given by enum ovs_atomic_type). */ | |
31 | union ovsdb_atom { | |
32 | int64_t integer; | |
33 | double real; | |
34 | bool boolean; | |
35 | char *string; | |
36 | struct uuid uuid; | |
37 | }; | |
38 | ||
39 | void ovsdb_atom_init_default(union ovsdb_atom *, enum ovsdb_atomic_type); | |
958ac03a | 40 | const union ovsdb_atom *ovsdb_atom_default(enum ovsdb_atomic_type); |
c532bf9d | 41 | bool ovsdb_atom_is_default(const union ovsdb_atom *, enum ovsdb_atomic_type); |
f85f8ebb BP |
42 | void ovsdb_atom_clone(union ovsdb_atom *, const union ovsdb_atom *, |
43 | enum ovsdb_atomic_type); | |
44 | void ovsdb_atom_swap(union ovsdb_atom *, union ovsdb_atom *); | |
45 | ||
5413de95 BP |
46 | /* Returns false if ovsdb_atom_destroy() is a no-op when it is applied to an |
47 | * initialized atom of the given 'type', true if ovsdb_atom_destroy() actually | |
48 | * does something. | |
49 | * | |
50 | * This can be used to avoid calling ovsdb_atom_destroy() for each element in | |
51 | * an array of homogeneous atoms. (It's not worthwhile for a single atom.) */ | |
f85f8ebb BP |
52 | static inline bool |
53 | ovsdb_atom_needs_destruction(enum ovsdb_atomic_type type) | |
54 | { | |
55 | return type == OVSDB_TYPE_STRING; | |
56 | } | |
57 | ||
5413de95 BP |
58 | /* Frees the contents of 'atom', which must have the specified 'type'. |
59 | * | |
60 | * This does not actually call free(atom). If necessary, the caller must be | |
61 | * responsible for that. */ | |
f85f8ebb BP |
62 | static inline void |
63 | ovsdb_atom_destroy(union ovsdb_atom *atom, enum ovsdb_atomic_type type) | |
64 | { | |
65 | if (type == OVSDB_TYPE_STRING) { | |
66 | free(atom->string); | |
67 | } | |
68 | } | |
69 | ||
70 | uint32_t ovsdb_atom_hash(const union ovsdb_atom *, enum ovsdb_atomic_type, | |
71 | uint32_t basis); | |
72 | ||
73 | int ovsdb_atom_compare_3way(const union ovsdb_atom *, | |
74 | const union ovsdb_atom *, | |
75 | enum ovsdb_atomic_type); | |
76 | ||
5413de95 BP |
77 | /* Returns true if 'a' and 'b', which are both of type 'type', has the same |
78 | * contents, false if their contents differ. */ | |
f85f8ebb BP |
79 | static inline bool ovsdb_atom_equals(const union ovsdb_atom *a, |
80 | const union ovsdb_atom *b, | |
81 | enum ovsdb_atomic_type type) | |
82 | { | |
83 | return !ovsdb_atom_compare_3way(a, b, type); | |
84 | } | |
85 | ||
86 | struct ovsdb_error *ovsdb_atom_from_json(union ovsdb_atom *, | |
bd76d25d | 87 | const struct ovsdb_base_type *, |
f85f8ebb | 88 | const struct json *, |
fbf925e4 | 89 | struct ovsdb_symbol_table *) |
cab50449 | 90 | OVS_WARN_UNUSED_RESULT; |
f85f8ebb BP |
91 | struct json *ovsdb_atom_to_json(const union ovsdb_atom *, |
92 | enum ovsdb_atomic_type); | |
0194f33a | 93 | |
1ab39058 | 94 | char *ovsdb_atom_from_string(union ovsdb_atom *, union ovsdb_atom **, |
ce5a3e38 BP |
95 | const struct ovsdb_base_type *, const char *, |
96 | struct ovsdb_symbol_table *) | |
cab50449 | 97 | OVS_WARN_UNUSED_RESULT; |
0194f33a BP |
98 | void ovsdb_atom_to_string(const union ovsdb_atom *, enum ovsdb_atomic_type, |
99 | struct ds *); | |
c6a41252 BP |
100 | void ovsdb_atom_to_bare(const union ovsdb_atom *, enum ovsdb_atomic_type, |
101 | struct ds *); | |
bd76d25d BP |
102 | |
103 | struct ovsdb_error *ovsdb_atom_check_constraints( | |
104 | const union ovsdb_atom *, const struct ovsdb_base_type *) | |
cab50449 | 105 | OVS_WARN_UNUSED_RESULT; |
f85f8ebb | 106 | \f |
c7239c2f BP |
107 | /* An instance of an OVSDB type (given by struct ovsdb_type). |
108 | * | |
2f47998b BP |
109 | * - The 'keys' must be unique and in sorted order. Most functions that modify |
110 | * an ovsdb_datum maintain these invariants. Functions that don't maintain | |
111 | * the invariants have names that end in "_unsafe". Use ovsdb_datum_sort() | |
112 | * to check and restore these invariants. | |
c7239c2f | 113 | * |
2f47998b | 114 | * - 'n' is constrained by the ovsdb_type's 'n_min' and 'n_max'. |
c7239c2f | 115 | * |
2f47998b BP |
116 | * If 'n' is nonzero, then 'keys' points to an array of 'n' atoms of the type |
117 | * specified by the ovsdb_type's 'key_type'. (Otherwise, 'keys' should be | |
118 | * null.) | |
c7239c2f | 119 | * |
2f47998b BP |
120 | * If 'n' is nonzero and the ovsdb_type's 'value_type' is not |
121 | * OVSDB_TYPE_VOID, then 'values' points to an array of 'n' atoms of the type | |
122 | * specified by the 'value_type'. (Otherwise, 'values' should be null.) | |
123 | * | |
124 | * Thus, for 'n' > 0, 'keys' will always be nonnull and 'values' will be | |
125 | * nonnull only for "map" types. | |
c7239c2f | 126 | */ |
f85f8ebb BP |
127 | struct ovsdb_datum { |
128 | unsigned int n; /* Number of 'keys' and 'values'. */ | |
129 | union ovsdb_atom *keys; /* Each of the ovsdb_type's 'key_type'. */ | |
130 | union ovsdb_atom *values; /* Each of the ovsdb_type's 'value_type'. */ | |
131 | }; | |
0164e367 | 132 | #define OVSDB_DATUM_INITIALIZER { 0, NULL, NULL } |
f85f8ebb | 133 | |
2f47998b BP |
134 | /* Basics. */ |
135 | void ovsdb_datum_init_empty(struct ovsdb_datum *); | |
f85f8ebb | 136 | void ovsdb_datum_init_default(struct ovsdb_datum *, const struct ovsdb_type *); |
c532bf9d BP |
137 | bool ovsdb_datum_is_default(const struct ovsdb_datum *, |
138 | const struct ovsdb_type *); | |
958ac03a | 139 | const struct ovsdb_datum *ovsdb_datum_default(const struct ovsdb_type *); |
f85f8ebb BP |
140 | void ovsdb_datum_clone(struct ovsdb_datum *, const struct ovsdb_datum *, |
141 | const struct ovsdb_type *); | |
142 | void ovsdb_datum_destroy(struct ovsdb_datum *, const struct ovsdb_type *); | |
143 | void ovsdb_datum_swap(struct ovsdb_datum *, struct ovsdb_datum *); | |
2f47998b BP |
144 | |
145 | /* Checking and maintaining invariants. */ | |
e9f8f936 | 146 | struct ovsdb_error *ovsdb_datum_sort(struct ovsdb_datum *, |
bfc96d9b | 147 | enum ovsdb_atomic_type key_type) |
cab50449 | 148 | OVS_WARN_UNUSED_RESULT; |
bfc96d9b BP |
149 | |
150 | void ovsdb_datum_sort_assert(struct ovsdb_datum *, | |
151 | enum ovsdb_atomic_type key_type); | |
152 | ||
2b66469b BP |
153 | size_t ovsdb_datum_sort_unique(struct ovsdb_datum *, |
154 | enum ovsdb_atomic_type key_type, | |
155 | enum ovsdb_atomic_type value_type); | |
156 | ||
bd76d25d BP |
157 | struct ovsdb_error *ovsdb_datum_check_constraints( |
158 | const struct ovsdb_datum *, const struct ovsdb_type *) | |
cab50449 | 159 | OVS_WARN_UNUSED_RESULT; |
f85f8ebb | 160 | |
2f47998b | 161 | /* Type conversion. */ |
f85f8ebb BP |
162 | struct ovsdb_error *ovsdb_datum_from_json(struct ovsdb_datum *, |
163 | const struct ovsdb_type *, | |
164 | const struct json *, | |
fbf925e4 | 165 | struct ovsdb_symbol_table *) |
cab50449 | 166 | OVS_WARN_UNUSED_RESULT; |
aefd97b7 AZ |
167 | struct ovsdb_error *ovsdb_transient_datum_from_json( |
168 | struct ovsdb_datum *, | |
169 | const struct ovsdb_type *, | |
170 | const struct json *) | |
171 | OVS_WARN_UNUSED_RESULT; | |
f85f8ebb BP |
172 | struct json *ovsdb_datum_to_json(const struct ovsdb_datum *, |
173 | const struct ovsdb_type *); | |
174 | ||
1bc6ff29 | 175 | char *ovsdb_datum_from_string(struct ovsdb_datum *, |
ce5a3e38 BP |
176 | const struct ovsdb_type *, const char *, |
177 | struct ovsdb_symbol_table *) | |
cab50449 | 178 | OVS_WARN_UNUSED_RESULT; |
0194f33a BP |
179 | void ovsdb_datum_to_string(const struct ovsdb_datum *, |
180 | const struct ovsdb_type *, struct ds *); | |
c6a41252 BP |
181 | void ovsdb_datum_to_bare(const struct ovsdb_datum *, |
182 | const struct ovsdb_type *, struct ds *); | |
0194f33a | 183 | |
9b03e59d | 184 | void ovsdb_datum_from_smap(struct ovsdb_datum *, const struct smap *); |
e4af5615 | 185 | |
0194f33a | 186 | /* Comparison. */ |
f85f8ebb BP |
187 | uint32_t ovsdb_datum_hash(const struct ovsdb_datum *, |
188 | const struct ovsdb_type *, uint32_t basis); | |
189 | int ovsdb_datum_compare_3way(const struct ovsdb_datum *, | |
190 | const struct ovsdb_datum *, | |
191 | const struct ovsdb_type *); | |
192 | bool ovsdb_datum_equals(const struct ovsdb_datum *, | |
193 | const struct ovsdb_datum *, | |
194 | const struct ovsdb_type *); | |
2f47998b BP |
195 | |
196 | /* Search. */ | |
197 | unsigned int ovsdb_datum_find_key(const struct ovsdb_datum *, | |
198 | const union ovsdb_atom *key, | |
199 | enum ovsdb_atomic_type key_type); | |
200 | unsigned int ovsdb_datum_find_key_value(const struct ovsdb_datum *, | |
201 | const union ovsdb_atom *key, | |
202 | enum ovsdb_atomic_type key_type, | |
203 | const union ovsdb_atom *value, | |
204 | enum ovsdb_atomic_type value_type); | |
205 | ||
206 | /* Set operations. */ | |
f85f8ebb BP |
207 | bool ovsdb_datum_includes_all(const struct ovsdb_datum *, |
208 | const struct ovsdb_datum *, | |
209 | const struct ovsdb_type *); | |
210 | bool ovsdb_datum_excludes_all(const struct ovsdb_datum *, | |
211 | const struct ovsdb_datum *, | |
212 | const struct ovsdb_type *); | |
e9f8f936 BP |
213 | void ovsdb_datum_union(struct ovsdb_datum *, |
214 | const struct ovsdb_datum *, | |
2f47998b BP |
215 | const struct ovsdb_type *, |
216 | bool replace); | |
e9f8f936 BP |
217 | void ovsdb_datum_subtract(struct ovsdb_datum *a, |
218 | const struct ovsdb_type *a_type, | |
219 | const struct ovsdb_datum *b, | |
220 | const struct ovsdb_type *b_type); | |
221 | ||
1a6f1cef AZ |
222 | /* Generate and apply diffs */ |
223 | void ovsdb_datum_diff(struct ovsdb_datum *diff, | |
224 | const struct ovsdb_datum *old, | |
225 | const struct ovsdb_datum *new, | |
226 | const struct ovsdb_type *type); | |
227 | ||
228 | struct ovsdb_error *ovsdb_datum_apply_diff(struct ovsdb_datum *new, | |
229 | const struct ovsdb_datum *old, | |
230 | const struct ovsdb_datum *diff, | |
231 | const struct ovsdb_type *type) | |
232 | OVS_WARN_UNUSED_RESULT; | |
233 | ||
2f47998b BP |
234 | /* Raw operations that may not maintain the invariants. */ |
235 | void ovsdb_datum_remove_unsafe(struct ovsdb_datum *, size_t idx, | |
236 | const struct ovsdb_type *); | |
237 | void ovsdb_datum_add_unsafe(struct ovsdb_datum *, | |
238 | const union ovsdb_atom *key, | |
239 | const union ovsdb_atom *value, | |
1ab39058 LR |
240 | const struct ovsdb_type *, |
241 | const union ovsdb_atom *range_end_atom); | |
2f47998b BP |
242 | |
243 | /* Type checking. */ | |
f85f8ebb BP |
244 | static inline bool |
245 | ovsdb_datum_conforms_to_type(const struct ovsdb_datum *datum, | |
246 | const struct ovsdb_type *type) | |
247 | { | |
248 | return datum->n >= type->n_min && datum->n <= type->n_max; | |
249 | } | |
250 | \f | |
251 | /* A table mapping from names to data items. Currently the data items are | |
252 | * always UUIDs; perhaps this will be expanded in the future. */ | |
253 | ||
0dc66db9 BP |
254 | struct ovsdb_symbol_table { |
255 | struct shash sh; /* Maps from name to struct ovsdb_symbol *. */ | |
256 | }; | |
257 | ||
2d2d6d4a BP |
258 | struct ovsdb_symbol { |
259 | struct uuid uuid; /* The UUID that the symbol represents. */ | |
e9387de4 | 260 | bool created; /* Already used to create row? */ |
c5f341ab BP |
261 | bool strong_ref; /* Parsed a strong reference to this row? */ |
262 | bool weak_ref; /* Parsed a weak reference to this row? */ | |
2d2d6d4a BP |
263 | }; |
264 | ||
f85f8ebb BP |
265 | struct ovsdb_symbol_table *ovsdb_symbol_table_create(void); |
266 | void ovsdb_symbol_table_destroy(struct ovsdb_symbol_table *); | |
2d2d6d4a BP |
267 | struct ovsdb_symbol *ovsdb_symbol_table_get(const struct ovsdb_symbol_table *, |
268 | const char *name); | |
fbf925e4 BP |
269 | struct ovsdb_symbol *ovsdb_symbol_table_put(struct ovsdb_symbol_table *, |
270 | const char *name, | |
271 | const struct uuid *, bool used); | |
272 | struct ovsdb_symbol *ovsdb_symbol_table_insert(struct ovsdb_symbol_table *, | |
273 | const char *name); | |
0194f33a BP |
274 | \f |
275 | /* Tokenization | |
276 | * | |
277 | * Used by ovsdb_atom_from_string() and ovsdb_datum_from_string(). */ | |
278 | ||
cab50449 | 279 | char *ovsdb_token_parse(const char **, char **outp) OVS_WARN_UNUSED_RESULT; |
0194f33a | 280 | bool ovsdb_token_is_delim(unsigned char); |
f85f8ebb | 281 | |
1ab39058 LR |
282 | struct ovsdb_error *ovsdb_atom_range_check_size(int64_t range_start, |
283 | int64_t range_end); | |
284 | ||
f85f8ebb | 285 | #endif /* ovsdb-data.h */ |