]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ovsdb-idl.h
ovsdb-idl: Break out database-specific stuff into new data structure.
[mirror_ovs.git] / lib / ovsdb-idl.h
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
2 * Copyright (C) 2016 Hewlett Packard Enterprise Development LP
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 OVSDB_IDL_H
18 #define OVSDB_IDL_H 1
19
20 /* Open vSwitch Database Interface Definition Language (OVSDB IDL).
21 *
22 * The OVSDB IDL maintains an in-memory replica of a database. It issues RPC
23 * requests to an OVSDB database server and parses the responses, converting
24 * raw JSON into data structures that are easier for clients to digest. Most
25 * notably, references to rows via UUID become C pointers.
26 *
27 * The IDL always presents a consistent snapshot of the database to its client,
28 * that is, it won't present the effects of some part of a transaction applied
29 * at the database server without presenting all of its effects.
30 *
31 * The IDL also assists with issuing database transactions. The client creates
32 * a transaction, manipulates the IDL data structures, and commits or aborts
33 * the transaction. The IDL then composes and issues the necessary JSON-RPC
34 * requests and reports to the client whether the transaction completed
35 * successfully.
36 */
37
38 #include <stdbool.h>
39 #include <stdint.h>
40 #include "compiler.h"
41 #include "ovsdb-types.h"
42 #include "ovsdb-data.h"
43 #include "openvswitch/list.h"
44 #include "ovsdb-condition.h"
45 #include "skiplist.h"
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 struct json;
52 struct ovsdb_datum;
53 struct ovsdb_idl_class;
54 struct ovsdb_idl_row;
55 struct ovsdb_idl_column;
56 struct ovsdb_idl_table_class;
57 struct uuid;
58
59 struct ovsdb_idl *ovsdb_idl_create(const char *remote,
60 const struct ovsdb_idl_class *,
61 bool monitor_everything_by_default,
62 bool retry);
63 void ovsdb_idl_set_remote(struct ovsdb_idl *, const char *, bool);
64 void ovsdb_idl_destroy(struct ovsdb_idl *);
65
66 void ovsdb_idl_run(struct ovsdb_idl *);
67 void ovsdb_idl_wait(struct ovsdb_idl *);
68
69 void ovsdb_idl_set_lock(struct ovsdb_idl *, const char *lock_name);
70 bool ovsdb_idl_has_lock(const struct ovsdb_idl *);
71 bool ovsdb_idl_is_lock_contended(const struct ovsdb_idl *);
72
73 const struct uuid * ovsdb_idl_get_monitor_id(const struct ovsdb_idl *);
74 unsigned int ovsdb_idl_get_seqno(const struct ovsdb_idl *);
75 bool ovsdb_idl_has_ever_connected(const struct ovsdb_idl *);
76 void ovsdb_idl_enable_reconnect(struct ovsdb_idl *);
77 void ovsdb_idl_force_reconnect(struct ovsdb_idl *);
78 void ovsdb_idl_verify_write_only(struct ovsdb_idl *);
79
80 bool ovsdb_idl_is_alive(const struct ovsdb_idl *);
81 int ovsdb_idl_get_last_error(const struct ovsdb_idl *);
82
83 void ovsdb_idl_set_probe_interval(const struct ovsdb_idl *, int probe_interval);
84
85 void ovsdb_idl_check_consistency(const struct ovsdb_idl *);
86
87 const struct ovsdb_idl_class *ovsdb_idl_get_class(const struct ovsdb_idl *);
88 const struct ovsdb_idl_table_class *ovsdb_idl_table_class_from_column(
89 const struct ovsdb_idl_class *, const struct ovsdb_idl_column *);
90 \f
91 /* Choosing columns and tables to replicate. */
92
93 /* Modes with which the IDL can monitor a column.
94 *
95 * If no bits are set, the column is not monitored at all. Its value will
96 * always appear to the client to be the default value for its type.
97 *
98 * If OVSDB_IDL_MONITOR is set, then the column is replicated. Its value will
99 * reflect the value in the database. If OVSDB_IDL_ALERT is also set, then the
100 * value returned by ovsdb_idl_get_seqno() will change when the column's value
101 * changes.
102 *
103 * The possible mode combinations are:
104 *
105 * - 0, for a column that a client doesn't care about.
106 *
107 * - (OVSDB_IDL_MONITOR | OVSDB_IDL_ALERT), for a column that a client wants
108 * to track and possibly update.
109 *
110 * - OVSDB_IDL_MONITOR, for columns that a client treats as "write-only",
111 * that is, it updates them but doesn't want to get alerted about its own
112 * updates. It also won't be alerted about other clients' updates, so this
113 * is suitable only for use by a client that "owns" a particular column.
114 *
115 * - OVDSB_IDL_ALERT without OVSDB_IDL_MONITOR is not valid.
116 *
117 * - (OVSDB_IDL_MONITOR | OVSDB_IDL_ALERT | OVSDB_IDL_TRACK), for a column
118 * that a client wants to track using the change tracking
119 * ovsdb_idl_track_get_*() functions.
120 */
121 #define OVSDB_IDL_MONITOR (1 << 0) /* Monitor this column? */
122 #define OVSDB_IDL_ALERT (1 << 1) /* Alert client when column updated? */
123 #define OVSDB_IDL_TRACK (1 << 2)
124
125 void ovsdb_idl_add_column(struct ovsdb_idl *, const struct ovsdb_idl_column *);
126 void ovsdb_idl_add_table(struct ovsdb_idl *,
127 const struct ovsdb_idl_table_class *);
128
129 void ovsdb_idl_omit(struct ovsdb_idl *, const struct ovsdb_idl_column *);
130 void ovsdb_idl_omit_alert(struct ovsdb_idl *, const struct ovsdb_idl_column *);
131
132 /* Change tracking.
133 *
134 * In OVSDB, change tracking is applied at each client in the IDL layer. This
135 * means that when a client makes a request to track changes on a particular
136 * table, they are essentially requesting information about the incremental
137 * changes to that table from the point in time that the request is made. Once
138 * the client clears tracked changes, that information will no longer be
139 * available.
140 *
141 * The implication of the above is that if a client requires replaying
142 * untracked history, it faces the choice of either trying to remember changes
143 * itself (which translates into a memory leak) or of being structured with a
144 * path for processing the full untracked table as well as a path that
145 * processes incremental changes. */
146 enum ovsdb_idl_change {
147 OVSDB_IDL_CHANGE_INSERT,
148 OVSDB_IDL_CHANGE_MODIFY,
149 OVSDB_IDL_CHANGE_DELETE,
150 OVSDB_IDL_CHANGE_MAX
151 };
152
153 /* Row, table sequence numbers */
154 unsigned int ovsdb_idl_table_get_seqno(
155 const struct ovsdb_idl *idl,
156 const struct ovsdb_idl_table_class *table_class);
157 unsigned int ovsdb_idl_row_get_seqno(
158 const struct ovsdb_idl_row *row,
159 enum ovsdb_idl_change change);
160
161 void ovsdb_idl_track_add_column(struct ovsdb_idl *idl,
162 const struct ovsdb_idl_column *column);
163 void ovsdb_idl_track_add_all(struct ovsdb_idl *idl);
164 const struct ovsdb_idl_row *ovsdb_idl_track_get_first(
165 const struct ovsdb_idl *, const struct ovsdb_idl_table_class *);
166 const struct ovsdb_idl_row *ovsdb_idl_track_get_next(const struct ovsdb_idl_row *);
167 bool ovsdb_idl_track_is_updated(const struct ovsdb_idl_row *row,
168 const struct ovsdb_idl_column *column);
169 void ovsdb_idl_track_clear(struct ovsdb_idl *);
170
171 \f
172 /* Reading the database replica. */
173
174 const struct ovsdb_idl_row *ovsdb_idl_get_row_for_uuid(
175 const struct ovsdb_idl *, const struct ovsdb_idl_table_class *,
176 const struct uuid *);
177 const struct ovsdb_idl_row *ovsdb_idl_first_row(
178 const struct ovsdb_idl *, const struct ovsdb_idl_table_class *);
179 const struct ovsdb_idl_row *ovsdb_idl_next_row(const struct ovsdb_idl_row *);
180
181 const struct ovsdb_datum *ovsdb_idl_read(const struct ovsdb_idl_row *,
182 const struct ovsdb_idl_column *);
183 const struct ovsdb_datum *ovsdb_idl_get(const struct ovsdb_idl_row *,
184 const struct ovsdb_idl_column *,
185 enum ovsdb_atomic_type key_type,
186 enum ovsdb_atomic_type value_type);
187 bool ovsdb_idl_is_mutable(const struct ovsdb_idl_row *,
188 const struct ovsdb_idl_column *);
189
190 bool ovsdb_idl_row_is_synthetic(const struct ovsdb_idl_row *);
191 \f
192 /* Transactions.
193 *
194 * A transaction may modify the contents of a database by modifying the values
195 * of columns, deleting rows, inserting rows, or adding checks that columns in
196 * the database have not changed ("verify" operations), through
197 * ovsdb_idl_txn_*() functions. (The OVSDB IDL code generator produces helper
198 * functions that internally call the ovsdb_idl_txn_*() functions. These are
199 * likely to be more convenient.)
200 *
201 * Reading and writing columns and inserting and deleting rows are all
202 * straightforward. The reasons to verify columns are less obvious.
203 * Verification is the key to maintaining transactional integrity. Because
204 * OVSDB handles multiple clients, it can happen that between the time that
205 * OVSDB client A reads a column and writes a new value, OVSDB client B has
206 * written that column. Client A's write should not ordinarily overwrite
207 * client B's, especially if the column in question is a "map" column that
208 * contains several more or less independent data items. If client A adds a
209 * "verify" operation before it writes the column, then the transaction fails
210 * in case client B modifies it first. Client A will then see the new value of
211 * the column and compose a new transaction based on the new contents written
212 * by client B.
213 *
214 * When a transaction is complete, which must be before the next call to
215 * ovsdb_idl_run() on 'idl', call ovsdb_idl_txn_commit() or
216 * ovsdb_idl_txn_abort().
217 *
218 * The life-cycle of a transaction looks like this:
219 *
220 * 1. Create the transaction and record the initial sequence number:
221 *
222 * seqno = ovsdb_idl_get_seqno(idl);
223 * txn = ovsdb_idl_txn_create(idl);
224 *
225 * 2. Modify the database with ovsdb_idl_txn_*() functions directly or
226 * indirectly.
227 *
228 * 3. Commit the transaction by calling ovsdb_idl_txn_commit(). The first call
229 * to this function probably returns TXN_INCOMPLETE. The client must keep
230 * calling again along as this remains true, calling ovsdb_idl_run() in
231 * between to let the IDL do protocol processing. (If the client doesn't
232 * have anything else to do in the meantime, it can use
233 * ovsdb_idl_txn_commit_block() to avoid having to loop itself.)
234 *
235 * 4. If the final status is TXN_TRY_AGAIN, wait for ovsdb_idl_get_seqno() to
236 * change from the saved 'seqno' (it's possible that it's already changed,
237 * in which case the client should not wait at all), then start over from
238 * step 1. Only a call to ovsdb_idl_run() will change the return value of
239 * ovsdb_idl_get_seqno(). (ovsdb_idl_txn_commit_block() calls
240 * ovsdb_idl_run().)
241 */
242
243 enum ovsdb_idl_txn_status {
244 TXN_UNCOMMITTED, /* Not yet committed or aborted. */
245 TXN_UNCHANGED, /* Transaction didn't include any changes. */
246 TXN_INCOMPLETE, /* Commit in progress, please wait. */
247 TXN_ABORTED, /* ovsdb_idl_txn_abort() called. */
248 TXN_SUCCESS, /* Commit successful. */
249 TXN_TRY_AGAIN, /* Commit failed because a "verify" operation
250 * reported an inconsistency, due to a network
251 * problem, or other transient failure. Wait
252 * for a change, then try again. */
253 TXN_NOT_LOCKED, /* Server hasn't given us the lock yet. */
254 TXN_ERROR /* Commit failed due to a hard error. */
255 };
256
257 const char *ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status);
258
259 struct ovsdb_idl_txn *ovsdb_idl_txn_create(struct ovsdb_idl *);
260 void ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *, const char *, ...)
261 OVS_PRINTF_FORMAT (2, 3);
262 void ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *);
263 void ovsdb_idl_txn_increment(struct ovsdb_idl_txn *,
264 const struct ovsdb_idl_row *,
265 const struct ovsdb_idl_column *,
266 bool force);
267 void ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *);
268 void ovsdb_idl_txn_wait(const struct ovsdb_idl_txn *);
269 enum ovsdb_idl_txn_status ovsdb_idl_txn_commit(struct ovsdb_idl_txn *);
270 enum ovsdb_idl_txn_status ovsdb_idl_txn_commit_block(struct ovsdb_idl_txn *);
271 void ovsdb_idl_txn_abort(struct ovsdb_idl_txn *);
272
273 const char *ovsdb_idl_txn_get_error(const struct ovsdb_idl_txn *);
274
275 int64_t ovsdb_idl_txn_get_increment_new_value(const struct ovsdb_idl_txn *);
276 const struct uuid *ovsdb_idl_txn_get_insert_uuid(const struct ovsdb_idl_txn *,
277 const struct uuid *);
278
279 void ovsdb_idl_txn_write(const struct ovsdb_idl_row *,
280 const struct ovsdb_idl_column *,
281 struct ovsdb_datum *);
282 void ovsdb_idl_txn_write_clone(const struct ovsdb_idl_row *,
283 const struct ovsdb_idl_column *,
284 const struct ovsdb_datum *);
285 void ovsdb_idl_txn_write_partial_map(const struct ovsdb_idl_row *,
286 const struct ovsdb_idl_column *,
287 struct ovsdb_datum *);
288 void ovsdb_idl_txn_delete_partial_map(const struct ovsdb_idl_row *,
289 const struct ovsdb_idl_column *,
290 struct ovsdb_datum *);
291 void ovsdb_idl_txn_write_partial_set(const struct ovsdb_idl_row *,
292 const struct ovsdb_idl_column *,
293 struct ovsdb_datum *);
294 void ovsdb_idl_txn_delete_partial_set(const struct ovsdb_idl_row *,
295 const struct ovsdb_idl_column *,
296 struct ovsdb_datum *);
297 void ovsdb_idl_txn_delete(const struct ovsdb_idl_row *);
298 const struct ovsdb_idl_row *ovsdb_idl_txn_insert(
299 struct ovsdb_idl_txn *, const struct ovsdb_idl_table_class *,
300 const struct uuid *);
301
302 struct ovsdb_idl *ovsdb_idl_txn_get_idl (struct ovsdb_idl_txn *);
303 void ovsdb_idl_get_initial_snapshot(struct ovsdb_idl *);
304 \f
305
306 /* ovsdb_idl_loop provides an easy way to manage the transactions related
307 * to 'idl' and to cope with different status during transaction. */
308 struct ovsdb_idl_loop {
309 struct ovsdb_idl *idl;
310 unsigned int skip_seqno;
311
312 struct ovsdb_idl_txn *committing_txn;
313 unsigned int precommit_seqno;
314
315 struct ovsdb_idl_txn *open_txn;
316
317 /* These members allow a client a simple, stateless way to keep track of
318 * transactions that commit: when a transaction commits successfully,
319 * ovsdb_idl_loop_commit_and_wait() copies 'next_cfg' to 'cur_cfg'. Thus,
320 * the client can set 'next_cfg' to a value that indicates a successful
321 * commit and check 'cur_cfg' on each iteration. */
322 int64_t cur_cfg;
323 int64_t next_cfg;
324 };
325
326 #define OVSDB_IDL_LOOP_INITIALIZER(IDL) { .idl = (IDL) }
327
328 void ovsdb_idl_loop_destroy(struct ovsdb_idl_loop *);
329 struct ovsdb_idl_txn *ovsdb_idl_loop_run(struct ovsdb_idl_loop *);
330 int ovsdb_idl_loop_commit_and_wait(struct ovsdb_idl_loop *);
331 \f
332 /* Conditional Replication
333 * =======================
334 *
335 * By default, when the IDL replicates a particular table in the database, it
336 * replicates every row in the table. These functions allow the client to
337 * specify that only selected rows should be replicated, by constructing a
338 * per-table condition that specifies the rows to replicate.
339 *
340 * A condition is a disjunction of clauses. The condition is true, and thus a
341 * row is replicated, if any of the clauses evaluates to true for a given row.
342 * (Thus, a condition with no clauses is always false.)
343 */
344
345 struct ovsdb_idl_condition {
346 struct hmap clauses; /* Contains "struct ovsdb_idl_clause"s. */
347 bool is_true; /* Is the condition unconditionally true? */
348 };
349 #define OVSDB_IDL_CONDITION_INIT(CONDITION) \
350 { HMAP_INITIALIZER(&(CONDITION)->clauses), false }
351
352 void ovsdb_idl_condition_init(struct ovsdb_idl_condition *);
353 void ovsdb_idl_condition_clear(struct ovsdb_idl_condition *);
354 void ovsdb_idl_condition_destroy(struct ovsdb_idl_condition *);
355 void ovsdb_idl_condition_add_clause(struct ovsdb_idl_condition *,
356 enum ovsdb_function function,
357 const struct ovsdb_idl_column *column,
358 const struct ovsdb_datum *arg);
359 void ovsdb_idl_condition_add_clause_true(struct ovsdb_idl_condition *);
360 bool ovsdb_idl_condition_is_true(const struct ovsdb_idl_condition *);
361
362 unsigned int ovsdb_idl_set_condition(struct ovsdb_idl *,
363 const struct ovsdb_idl_table_class *,
364 const struct ovsdb_idl_condition *);
365
366 unsigned int ovsdb_idl_get_condition_seqno(const struct ovsdb_idl *);
367
368 /* The OVSDB-IDL Compound Indexes feature allows for the creation of custom
369 * table indexes over one or more columns in the IDL. These indexes provide
370 * the ability to retrieve rows matching a particular search criteria and to
371 * iterate over a subset of rows in a defined order.
372 */
373
374 #define OVSDB_INDEX_DESC -1
375 #define OVSDB_INDEX_ASC 1
376
377 /*
378 * Skiplist comparison function. Allows to store sorted data.
379 */
380 typedef int (column_comparator)(const void *a, const void *b);
381
382 struct ovsdb_idl_index_cursor {
383 struct ovsdb_idl_index *index; /* Index used by this cursor */
384 struct skiplist_node *position; /* Current position in the index */
385 };
386
387 struct ovsdb_idl_index *ovsdb_idl_create_index(struct ovsdb_idl *idl,
388 const struct ovsdb_idl_table_class *tc,
389 const char *index_name);
390 void ovsdb_idl_index_add_column(struct ovsdb_idl_index *,
391 const struct ovsdb_idl_column *,
392 int order,
393 column_comparator *custom_comparer);
394 bool ovsdb_idl_initialize_cursor(struct ovsdb_idl *,
395 const struct ovsdb_idl_table_class *tc,
396 const char *index_name,
397 struct ovsdb_idl_index_cursor *cursor);
398 void ovsdb_idl_index_write_(struct ovsdb_idl_row *,
399 const struct ovsdb_idl_column *,
400 struct ovsdb_datum *,
401 const struct ovsdb_idl_table_class *);
402 struct ovsdb_idl_row *ovsdb_idl_index_init_row(struct ovsdb_idl *,
403 const struct ovsdb_idl_table_class *);
404 void ovsdb_idl_index_destroy_row__(const struct ovsdb_idl_row *);
405 struct ovsdb_idl_row *ovsdb_idl_index_first(struct ovsdb_idl_index_cursor *);
406 struct ovsdb_idl_row *ovsdb_idl_index_next(struct ovsdb_idl_index_cursor *);
407 struct ovsdb_idl_row *ovsdb_idl_index_data(struct ovsdb_idl_index_cursor *);
408 struct ovsdb_idl_row *ovsdb_idl_index_find(struct ovsdb_idl_index_cursor *,
409 struct ovsdb_idl_row *);
410 struct ovsdb_idl_row *ovsdb_idl_index_forward_to(
411 struct ovsdb_idl_index_cursor *,
412 struct ovsdb_idl_row *);
413 int ovsdb_idl_index_compare(struct ovsdb_idl_index_cursor *,
414 struct ovsdb_idl_row *a,
415 struct ovsdb_idl_row *b);
416
417 #ifdef __cplusplus
418 }
419 #endif
420
421 #endif /* ovsdb-idl.h */