]> git.proxmox.com Git - mirror_ovs.git/blame - lib/ovsdb-data.h
openflow: Table maintenance commands for Geneve options.
[mirror_ovs.git] / lib / ovsdb-data.h
CommitLineData
7bd02255 1/* Copyright (c) 2009, 2010, 2011, 2012 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"
0dc66db9 22#include "shash.h"
f85f8ebb 23
0194f33a 24struct ds;
f85f8ebb 25struct ovsdb_symbol_table;
57c8677b 26struct smap;
f85f8ebb
BP
27
28/* One value of an atomic type (given by enum ovs_atomic_type). */
29union ovsdb_atom {
30 int64_t integer;
31 double real;
32 bool boolean;
33 char *string;
34 struct uuid uuid;
35};
36
37void ovsdb_atom_init_default(union ovsdb_atom *, enum ovsdb_atomic_type);
958ac03a 38const union ovsdb_atom *ovsdb_atom_default(enum ovsdb_atomic_type);
c532bf9d 39bool ovsdb_atom_is_default(const union ovsdb_atom *, enum ovsdb_atomic_type);
f85f8ebb
BP
40void ovsdb_atom_clone(union ovsdb_atom *, const union ovsdb_atom *,
41 enum ovsdb_atomic_type);
42void ovsdb_atom_swap(union ovsdb_atom *, union ovsdb_atom *);
43
5413de95
BP
44/* Returns false if ovsdb_atom_destroy() is a no-op when it is applied to an
45 * initialized atom of the given 'type', true if ovsdb_atom_destroy() actually
46 * does something.
47 *
48 * This can be used to avoid calling ovsdb_atom_destroy() for each element in
49 * an array of homogeneous atoms. (It's not worthwhile for a single atom.) */
f85f8ebb
BP
50static inline bool
51ovsdb_atom_needs_destruction(enum ovsdb_atomic_type type)
52{
53 return type == OVSDB_TYPE_STRING;
54}
55
5413de95
BP
56/* Frees the contents of 'atom', which must have the specified 'type'.
57 *
58 * This does not actually call free(atom). If necessary, the caller must be
59 * responsible for that. */
f85f8ebb
BP
60static inline void
61ovsdb_atom_destroy(union ovsdb_atom *atom, enum ovsdb_atomic_type type)
62{
63 if (type == OVSDB_TYPE_STRING) {
64 free(atom->string);
65 }
66}
67
68uint32_t ovsdb_atom_hash(const union ovsdb_atom *, enum ovsdb_atomic_type,
69 uint32_t basis);
70
71int ovsdb_atom_compare_3way(const union ovsdb_atom *,
72 const union ovsdb_atom *,
73 enum ovsdb_atomic_type);
74
5413de95
BP
75/* Returns true if 'a' and 'b', which are both of type 'type', has the same
76 * contents, false if their contents differ. */
f85f8ebb
BP
77static inline bool ovsdb_atom_equals(const union ovsdb_atom *a,
78 const union ovsdb_atom *b,
79 enum ovsdb_atomic_type type)
80{
81 return !ovsdb_atom_compare_3way(a, b, type);
82}
83
84struct ovsdb_error *ovsdb_atom_from_json(union ovsdb_atom *,
bd76d25d 85 const struct ovsdb_base_type *,
f85f8ebb 86 const struct json *,
fbf925e4 87 struct ovsdb_symbol_table *)
cab50449 88 OVS_WARN_UNUSED_RESULT;
f85f8ebb
BP
89struct json *ovsdb_atom_to_json(const union ovsdb_atom *,
90 enum ovsdb_atomic_type);
0194f33a 91
bd76d25d 92char *ovsdb_atom_from_string(union ovsdb_atom *,
ce5a3e38
BP
93 const struct ovsdb_base_type *, const char *,
94 struct ovsdb_symbol_table *)
cab50449 95 OVS_WARN_UNUSED_RESULT;
0194f33a
BP
96void ovsdb_atom_to_string(const union ovsdb_atom *, enum ovsdb_atomic_type,
97 struct ds *);
c6a41252
BP
98void ovsdb_atom_to_bare(const union ovsdb_atom *, enum ovsdb_atomic_type,
99 struct ds *);
bd76d25d
BP
100
101struct ovsdb_error *ovsdb_atom_check_constraints(
102 const union ovsdb_atom *, const struct ovsdb_base_type *)
cab50449 103 OVS_WARN_UNUSED_RESULT;
f85f8ebb 104\f
c7239c2f
BP
105/* An instance of an OVSDB type (given by struct ovsdb_type).
106 *
2f47998b
BP
107 * - The 'keys' must be unique and in sorted order. Most functions that modify
108 * an ovsdb_datum maintain these invariants. Functions that don't maintain
109 * the invariants have names that end in "_unsafe". Use ovsdb_datum_sort()
110 * to check and restore these invariants.
c7239c2f 111 *
2f47998b 112 * - 'n' is constrained by the ovsdb_type's 'n_min' and 'n_max'.
c7239c2f 113 *
2f47998b
BP
114 * If 'n' is nonzero, then 'keys' points to an array of 'n' atoms of the type
115 * specified by the ovsdb_type's 'key_type'. (Otherwise, 'keys' should be
116 * null.)
c7239c2f 117 *
2f47998b
BP
118 * If 'n' is nonzero and the ovsdb_type's 'value_type' is not
119 * OVSDB_TYPE_VOID, then 'values' points to an array of 'n' atoms of the type
120 * specified by the 'value_type'. (Otherwise, 'values' should be null.)
121 *
122 * Thus, for 'n' > 0, 'keys' will always be nonnull and 'values' will be
123 * nonnull only for "map" types.
c7239c2f 124 */
f85f8ebb
BP
125struct ovsdb_datum {
126 unsigned int n; /* Number of 'keys' and 'values'. */
127 union ovsdb_atom *keys; /* Each of the ovsdb_type's 'key_type'. */
128 union ovsdb_atom *values; /* Each of the ovsdb_type's 'value_type'. */
129};
130
2f47998b
BP
131/* Basics. */
132void ovsdb_datum_init_empty(struct ovsdb_datum *);
f85f8ebb 133void ovsdb_datum_init_default(struct ovsdb_datum *, const struct ovsdb_type *);
c532bf9d
BP
134bool ovsdb_datum_is_default(const struct ovsdb_datum *,
135 const struct ovsdb_type *);
958ac03a 136const struct ovsdb_datum *ovsdb_datum_default(const struct ovsdb_type *);
f85f8ebb
BP
137void ovsdb_datum_clone(struct ovsdb_datum *, const struct ovsdb_datum *,
138 const struct ovsdb_type *);
139void ovsdb_datum_destroy(struct ovsdb_datum *, const struct ovsdb_type *);
140void ovsdb_datum_swap(struct ovsdb_datum *, struct ovsdb_datum *);
2f47998b
BP
141
142/* Checking and maintaining invariants. */
e9f8f936 143struct ovsdb_error *ovsdb_datum_sort(struct ovsdb_datum *,
bfc96d9b 144 enum ovsdb_atomic_type key_type)
cab50449 145 OVS_WARN_UNUSED_RESULT;
bfc96d9b
BP
146
147void ovsdb_datum_sort_assert(struct ovsdb_datum *,
148 enum ovsdb_atomic_type key_type);
149
2b66469b
BP
150size_t ovsdb_datum_sort_unique(struct ovsdb_datum *,
151 enum ovsdb_atomic_type key_type,
152 enum ovsdb_atomic_type value_type);
153
bd76d25d
BP
154struct ovsdb_error *ovsdb_datum_check_constraints(
155 const struct ovsdb_datum *, const struct ovsdb_type *)
cab50449 156 OVS_WARN_UNUSED_RESULT;
f85f8ebb 157
2f47998b 158/* Type conversion. */
f85f8ebb
BP
159struct ovsdb_error *ovsdb_datum_from_json(struct ovsdb_datum *,
160 const struct ovsdb_type *,
161 const struct json *,
fbf925e4 162 struct ovsdb_symbol_table *)
cab50449 163 OVS_WARN_UNUSED_RESULT;
f85f8ebb
BP
164struct json *ovsdb_datum_to_json(const struct ovsdb_datum *,
165 const struct ovsdb_type *);
166
1bc6ff29 167char *ovsdb_datum_from_string(struct ovsdb_datum *,
ce5a3e38
BP
168 const struct ovsdb_type *, const char *,
169 struct ovsdb_symbol_table *)
cab50449 170 OVS_WARN_UNUSED_RESULT;
0194f33a
BP
171void ovsdb_datum_to_string(const struct ovsdb_datum *,
172 const struct ovsdb_type *, struct ds *);
c6a41252
BP
173void ovsdb_datum_to_bare(const struct ovsdb_datum *,
174 const struct ovsdb_type *, struct ds *);
0194f33a 175
57c8677b 176void ovsdb_datum_from_smap(struct ovsdb_datum *, struct smap *);
e4af5615 177
0194f33a 178/* Comparison. */
f85f8ebb
BP
179uint32_t ovsdb_datum_hash(const struct ovsdb_datum *,
180 const struct ovsdb_type *, uint32_t basis);
181int ovsdb_datum_compare_3way(const struct ovsdb_datum *,
182 const struct ovsdb_datum *,
183 const struct ovsdb_type *);
184bool ovsdb_datum_equals(const struct ovsdb_datum *,
185 const struct ovsdb_datum *,
186 const struct ovsdb_type *);
2f47998b
BP
187
188/* Search. */
189unsigned int ovsdb_datum_find_key(const struct ovsdb_datum *,
190 const union ovsdb_atom *key,
191 enum ovsdb_atomic_type key_type);
192unsigned int ovsdb_datum_find_key_value(const struct ovsdb_datum *,
193 const union ovsdb_atom *key,
194 enum ovsdb_atomic_type key_type,
195 const union ovsdb_atom *value,
196 enum ovsdb_atomic_type value_type);
197
198/* Set operations. */
f85f8ebb
BP
199bool ovsdb_datum_includes_all(const struct ovsdb_datum *,
200 const struct ovsdb_datum *,
201 const struct ovsdb_type *);
202bool ovsdb_datum_excludes_all(const struct ovsdb_datum *,
203 const struct ovsdb_datum *,
204 const struct ovsdb_type *);
e9f8f936
BP
205void ovsdb_datum_union(struct ovsdb_datum *,
206 const struct ovsdb_datum *,
2f47998b
BP
207 const struct ovsdb_type *,
208 bool replace);
e9f8f936
BP
209void ovsdb_datum_subtract(struct ovsdb_datum *a,
210 const struct ovsdb_type *a_type,
211 const struct ovsdb_datum *b,
212 const struct ovsdb_type *b_type);
213
2f47998b
BP
214/* Raw operations that may not maintain the invariants. */
215void ovsdb_datum_remove_unsafe(struct ovsdb_datum *, size_t idx,
216 const struct ovsdb_type *);
217void ovsdb_datum_add_unsafe(struct ovsdb_datum *,
218 const union ovsdb_atom *key,
219 const union ovsdb_atom *value,
220 const struct ovsdb_type *);
221
222/* Type checking. */
f85f8ebb
BP
223static inline bool
224ovsdb_datum_conforms_to_type(const struct ovsdb_datum *datum,
225 const struct ovsdb_type *type)
226{
227 return datum->n >= type->n_min && datum->n <= type->n_max;
228}
229\f
230/* A table mapping from names to data items. Currently the data items are
231 * always UUIDs; perhaps this will be expanded in the future. */
232
0dc66db9
BP
233struct ovsdb_symbol_table {
234 struct shash sh; /* Maps from name to struct ovsdb_symbol *. */
235};
236
2d2d6d4a
BP
237struct ovsdb_symbol {
238 struct uuid uuid; /* The UUID that the symbol represents. */
e9387de4 239 bool created; /* Already used to create row? */
c5f341ab
BP
240 bool strong_ref; /* Parsed a strong reference to this row? */
241 bool weak_ref; /* Parsed a weak reference to this row? */
2d2d6d4a
BP
242};
243
f85f8ebb
BP
244struct ovsdb_symbol_table *ovsdb_symbol_table_create(void);
245void ovsdb_symbol_table_destroy(struct ovsdb_symbol_table *);
2d2d6d4a
BP
246struct ovsdb_symbol *ovsdb_symbol_table_get(const struct ovsdb_symbol_table *,
247 const char *name);
fbf925e4
BP
248struct ovsdb_symbol *ovsdb_symbol_table_put(struct ovsdb_symbol_table *,
249 const char *name,
250 const struct uuid *, bool used);
251struct ovsdb_symbol *ovsdb_symbol_table_insert(struct ovsdb_symbol_table *,
252 const char *name);
0194f33a
BP
253\f
254/* Tokenization
255 *
256 * Used by ovsdb_atom_from_string() and ovsdb_datum_from_string(). */
257
cab50449 258char *ovsdb_token_parse(const char **, char **outp) OVS_WARN_UNUSED_RESULT;
0194f33a 259bool ovsdb_token_is_delim(unsigned char);
f85f8ebb
BP
260
261#endif /* ovsdb-data.h */