]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ovsdb-idl.h
ovsdb-idl: Improve prototypes.
[mirror_ovs.git] / lib / ovsdb-idl.h
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2019 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 struct ovsdb_idl *ovsdb_idl_create_unconnected(
64 const struct ovsdb_idl_class *, bool monitor_everything_by_default);
65 void ovsdb_idl_set_remote(struct ovsdb_idl *, const char *remote, bool retry);
66 void ovsdb_idl_set_shuffle_remotes(struct ovsdb_idl *, bool shuffle);
67 void ovsdb_idl_reset_min_index(struct ovsdb_idl *);
68 void ovsdb_idl_destroy(struct ovsdb_idl *);
69
70 void ovsdb_idl_set_leader_only(struct ovsdb_idl *, bool leader_only);
71
72 void ovsdb_idl_run(struct ovsdb_idl *);
73 void ovsdb_idl_wait(struct ovsdb_idl *);
74
75 void ovsdb_idl_set_lock(struct ovsdb_idl *, const char *lock_name);
76 bool ovsdb_idl_has_lock(const struct ovsdb_idl *);
77 bool ovsdb_idl_is_lock_contended(const struct ovsdb_idl *);
78
79 unsigned int ovsdb_idl_get_seqno(const struct ovsdb_idl *);
80 bool ovsdb_idl_has_ever_connected(const struct ovsdb_idl *);
81 void ovsdb_idl_enable_reconnect(struct ovsdb_idl *);
82 void ovsdb_idl_force_reconnect(struct ovsdb_idl *);
83 void ovsdb_idl_verify_write_only(struct ovsdb_idl *);
84
85 bool ovsdb_idl_is_alive(const struct ovsdb_idl *);
86 bool ovsdb_idl_is_connected(const struct ovsdb_idl *idl);
87 int ovsdb_idl_get_last_error(const struct ovsdb_idl *);
88
89 void ovsdb_idl_set_probe_interval(const struct ovsdb_idl *, int probe_interval);
90
91 void ovsdb_idl_check_consistency(const struct ovsdb_idl *);
92
93 const struct ovsdb_idl_class *ovsdb_idl_get_class(const struct ovsdb_idl *);
94 const struct ovsdb_idl_table_class *ovsdb_idl_table_class_from_column(
95 const struct ovsdb_idl_class *, const struct ovsdb_idl_column *);
96 \f
97 /* Choosing columns and tables to replicate.
98 *
99 * The client may choose any subset of the columns and tables to replicate,
100 * specifying it one of two ways:
101 *
102 * - As a deny list (adding the columns or tables to replicate). To do so,
103 * the client passes false as 'monitor_everything_by_default' to
104 * ovsdb_idl_create() and then calls ovsdb_idl_add_column() and
105 * ovsdb_idl_add_table() for the desired columns and, if necessary, tables.
106 *
107 * - As an allow list (replicating all columns and tables except those
108 * explicitly removed). To do so, the client passes true as
109 * 'monitor_everything_by_default' to ovsdb_idl_create() and then calls
110 * ovsdb_idl_omit() to remove columns.
111 *
112 * There are multiple modes a column may be replicated:
113 *
114 * - Read-only. This is the default. Whenever the column changes in any
115 * replicated row, the value returned by ovsdb_idl_get_seqno() will change,
116 * letting the client know to look at the replicated data again.
117 *
118 * - Write-only. This is for columns that the client sets and updates but
119 * does not want to be alerted about its own updates (which, at the OVSDB
120 * level, cannot be distinguished from updates made by any other client).
121 * The column will be replicated in the same way as for read-only columns,
122 * but the value returned by ovsdb_idl_get_seqno() will not change when the
123 * column changes, saving wasted CPU time.
124 *
125 * (A "write-only" client probably does read the column so that it can know
126 * whether it needs to update it, but it doesn't expect to react to changes
127 * by other clients.)
128 *
129 * To mark a replicated column as write-only, a client calls
130 * ovsdb_idl_omit_alert(). (The column must already be replicated one of
131 * the ways described in the previous list.)
132 *
133 * This is an optimization only and does not affect behavioral correctness
134 * of an otherwise well-written client.
135 *
136 * - Read/write. In theory, an OVSDB client might both read and write a
137 * column, although OVSDB schemas are usually designed so that any given
138 * client only does one or the other. This is actually the same as
139 * read/write columns; that is, the client need take no special action.
140 */
141
142 /* Modes with which the IDL can replicate a column. See above comment for
143 * overview.
144 *
145 * If no bits are set, the IDL does not replicate the column at all. The
146 * client will always see it with the default value for its type.
147 *
148 * If OVSDB_IDL_MONITOR is set, then the IDL replicates the column and sets it
149 * to to the value in the database. If OVSDB_IDL_ALERT is also set, then the
150 * IDL will change the value returned by ovsdb_idl_get_seqno() when the
151 * column's value changes in any row.
152 *
153 * The possible mode combinations are:
154 *
155 * - 0, for a column that a client doesn't care about. This is the default
156 * for every column in every table, if the client passes false for
157 * 'monitor_everything_by_default' to ovsdb_idl_create().
158 *
159 * - (OVSDB_IDL_MONITOR | OVSDB_IDL_ALERT), for a column that a client wants
160 * to track and possibly update. This is the default for every column in
161 * every table, if the client passes true for
162 * 'monitor_everything_by_default' to ovsdb_idl_create().
163 *
164 * - OVSDB_IDL_MONITOR, for columns that a client treats as "write-only",
165 * that is, it updates them but doesn't want to get alerted about its own
166 * updates. It also won't be alerted about other clients' updates, so this
167 * is suitable only for use by a client that "owns" a particular column.
168 * Use ovsdb_idl_omit_alert() to set a column that is already replicated to
169 * this mode.
170 *
171 * - OVDSB_IDL_ALERT without OVSDB_IDL_MONITOR is not valid.
172 *
173 * - (OVSDB_IDL_MONITOR | OVSDB_IDL_ALERT | OVSDB_IDL_TRACK), for a column
174 * that a client wants to track using the change tracking
175 * ovsdb_idl_track_get_*() functions.
176 */
177 #define OVSDB_IDL_MONITOR (1 << 0) /* Replicate this column? */
178 #define OVSDB_IDL_ALERT (1 << 1) /* Alert client when column changes? */
179 #define OVSDB_IDL_TRACK (1 << 2)
180
181 void ovsdb_idl_add_column(struct ovsdb_idl *, const struct ovsdb_idl_column *);
182 void ovsdb_idl_add_table(struct ovsdb_idl *,
183 const struct ovsdb_idl_table_class *);
184
185 void ovsdb_idl_omit(struct ovsdb_idl *, const struct ovsdb_idl_column *);
186 void ovsdb_idl_omit_alert(struct ovsdb_idl *, const struct ovsdb_idl_column *);
187
188 /* Change tracking.
189 *
190 * In OVSDB, change tracking is applied at each client in the IDL layer. This
191 * means that when a client makes a request to track changes on a particular
192 * table, they are essentially requesting information about the incremental
193 * changes to that table from the point in time that the request is made. Once
194 * the client clears tracked changes, that information will no longer be
195 * available.
196 *
197 * The implication of the above is that if a client requires replaying
198 * untracked history, it faces the choice of either trying to remember changes
199 * itself (which translates into a memory leak) or of being structured with a
200 * path for processing the full untracked table as well as a path that
201 * processes incremental changes. */
202 enum ovsdb_idl_change {
203 OVSDB_IDL_CHANGE_INSERT,
204 OVSDB_IDL_CHANGE_MODIFY,
205 OVSDB_IDL_CHANGE_DELETE,
206 OVSDB_IDL_CHANGE_MAX
207 };
208
209 /* Row, table sequence numbers */
210 unsigned int ovsdb_idl_table_get_seqno(
211 const struct ovsdb_idl *idl,
212 const struct ovsdb_idl_table_class *table_class);
213 unsigned int ovsdb_idl_row_get_seqno(
214 const struct ovsdb_idl_row *row,
215 enum ovsdb_idl_change change);
216
217 void ovsdb_idl_track_add_column(struct ovsdb_idl *idl,
218 const struct ovsdb_idl_column *column);
219 void ovsdb_idl_track_add_all(struct ovsdb_idl *idl);
220 const struct ovsdb_idl_row *ovsdb_idl_track_get_first(
221 const struct ovsdb_idl *, const struct ovsdb_idl_table_class *);
222 const struct ovsdb_idl_row *ovsdb_idl_track_get_next(const struct ovsdb_idl_row *);
223 bool ovsdb_idl_track_is_updated(const struct ovsdb_idl_row *row,
224 const struct ovsdb_idl_column *column);
225 void ovsdb_idl_track_clear(struct ovsdb_idl *);
226
227 \f
228 /* Reading the database replica. */
229
230 const struct ovsdb_idl_row *ovsdb_idl_get_row_for_uuid(
231 const struct ovsdb_idl *, const struct ovsdb_idl_table_class *,
232 const struct uuid *);
233 const struct ovsdb_idl_row *ovsdb_idl_first_row(
234 const struct ovsdb_idl *, const struct ovsdb_idl_table_class *);
235 const struct ovsdb_idl_row *ovsdb_idl_next_row(const struct ovsdb_idl_row *);
236
237 const struct ovsdb_datum *ovsdb_idl_read(const struct ovsdb_idl_row *,
238 const struct ovsdb_idl_column *);
239 const struct ovsdb_datum *ovsdb_idl_get(const struct ovsdb_idl_row *,
240 const struct ovsdb_idl_column *,
241 enum ovsdb_atomic_type key_type,
242 enum ovsdb_atomic_type value_type);
243 bool ovsdb_idl_is_mutable(const struct ovsdb_idl_row *,
244 const struct ovsdb_idl_column *);
245
246 bool ovsdb_idl_row_is_synthetic(const struct ovsdb_idl_row *);
247 \f
248 /* Transactions.
249 *
250 * A transaction may modify the contents of a database by modifying the values
251 * of columns, deleting rows, inserting rows, or adding checks that columns in
252 * the database have not changed ("verify" operations), through
253 * ovsdb_idl_txn_*() functions. (The OVSDB IDL code generator produces helper
254 * functions that internally call the ovsdb_idl_txn_*() functions. These are
255 * likely to be more convenient.)
256 *
257 * Reading and writing columns and inserting and deleting rows are all
258 * straightforward. The reasons to verify columns are less obvious.
259 * Verification is the key to maintaining transactional integrity. Because
260 * OVSDB handles multiple clients, it can happen that between the time that
261 * OVSDB client A reads a column and writes a new value, OVSDB client B has
262 * written that column. Client A's write should not ordinarily overwrite
263 * client B's, especially if the column in question is a "map" column that
264 * contains several more or less independent data items. If client A adds a
265 * "verify" operation before it writes the column, then the transaction fails
266 * in case client B modifies it first. Client A will then see the new value of
267 * the column and compose a new transaction based on the new contents written
268 * by client B.
269 *
270 * When a transaction is complete, which must be before the next call to
271 * ovsdb_idl_run() on 'idl', call ovsdb_idl_txn_commit() or
272 * ovsdb_idl_txn_abort().
273 *
274 * The life-cycle of a transaction looks like this:
275 *
276 * 1. Create the transaction and record the initial sequence number:
277 *
278 * seqno = ovsdb_idl_get_seqno(idl);
279 * txn = ovsdb_idl_txn_create(idl);
280 *
281 * 2. Modify the database with ovsdb_idl_txn_*() functions directly or
282 * indirectly.
283 *
284 * 3. Commit the transaction by calling ovsdb_idl_txn_commit(). The first call
285 * to this function probably returns TXN_INCOMPLETE. The client must keep
286 * calling again along as this remains true, calling ovsdb_idl_run() in
287 * between to let the IDL do protocol processing. (If the client doesn't
288 * have anything else to do in the meantime, it can use
289 * ovsdb_idl_txn_commit_block() to avoid having to loop itself.)
290 *
291 * 4. If the final status is TXN_TRY_AGAIN, wait for ovsdb_idl_get_seqno() to
292 * change from the saved 'seqno' (it's possible that it's already changed,
293 * in which case the client should not wait at all), then start over from
294 * step 1. Only a call to ovsdb_idl_run() will change the return value of
295 * ovsdb_idl_get_seqno(). (ovsdb_idl_txn_commit_block() calls
296 * ovsdb_idl_run().)
297 */
298
299 enum ovsdb_idl_txn_status {
300 TXN_UNCOMMITTED, /* Not yet committed or aborted. */
301 TXN_UNCHANGED, /* Transaction didn't include any changes. */
302 TXN_INCOMPLETE, /* Commit in progress, please wait. */
303 TXN_ABORTED, /* ovsdb_idl_txn_abort() called. */
304 TXN_SUCCESS, /* Commit successful. */
305 TXN_TRY_AGAIN, /* Commit failed because a "verify" operation
306 * reported an inconsistency, due to a network
307 * problem, or other transient failure. Wait
308 * for a change, then try again. */
309 TXN_NOT_LOCKED, /* Server hasn't given us the lock yet. */
310 TXN_ERROR /* Commit failed due to a hard error. */
311 };
312
313 const char *ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status);
314
315 struct ovsdb_idl_txn *ovsdb_idl_txn_create(struct ovsdb_idl *);
316 void ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *, const char *, ...)
317 OVS_PRINTF_FORMAT (2, 3);
318 void ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *);
319 void ovsdb_idl_txn_increment(struct ovsdb_idl_txn *,
320 const struct ovsdb_idl_row *,
321 const struct ovsdb_idl_column *,
322 bool force);
323 void ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *);
324 void ovsdb_idl_txn_wait(const struct ovsdb_idl_txn *);
325 enum ovsdb_idl_txn_status ovsdb_idl_txn_commit(struct ovsdb_idl_txn *);
326 enum ovsdb_idl_txn_status ovsdb_idl_txn_commit_block(struct ovsdb_idl_txn *);
327 void ovsdb_idl_txn_abort(struct ovsdb_idl_txn *);
328
329 const char *ovsdb_idl_txn_get_error(const struct ovsdb_idl_txn *);
330
331 int64_t ovsdb_idl_txn_get_increment_new_value(const struct ovsdb_idl_txn *);
332 const struct uuid *ovsdb_idl_txn_get_insert_uuid(const struct ovsdb_idl_txn *,
333 const struct uuid *);
334
335 void ovsdb_idl_txn_write(const struct ovsdb_idl_row *,
336 const struct ovsdb_idl_column *,
337 struct ovsdb_datum *);
338 void ovsdb_idl_txn_write_clone(const struct ovsdb_idl_row *,
339 const struct ovsdb_idl_column *,
340 const struct ovsdb_datum *);
341 void ovsdb_idl_txn_write_partial_map(const struct ovsdb_idl_row *,
342 const struct ovsdb_idl_column *,
343 struct ovsdb_datum *);
344 void ovsdb_idl_txn_delete_partial_map(const struct ovsdb_idl_row *,
345 const struct ovsdb_idl_column *,
346 struct ovsdb_datum *);
347 void ovsdb_idl_txn_write_partial_set(const struct ovsdb_idl_row *,
348 const struct ovsdb_idl_column *,
349 struct ovsdb_datum *);
350 void ovsdb_idl_txn_delete_partial_set(const struct ovsdb_idl_row *,
351 const struct ovsdb_idl_column *,
352 struct ovsdb_datum *);
353 void ovsdb_idl_txn_delete(const struct ovsdb_idl_row *);
354 const struct ovsdb_idl_row *ovsdb_idl_txn_insert(
355 struct ovsdb_idl_txn *, const struct ovsdb_idl_table_class *,
356 const struct uuid *);
357
358 struct ovsdb_idl *ovsdb_idl_txn_get_idl (struct ovsdb_idl_txn *);
359 void ovsdb_idl_get_initial_snapshot(struct ovsdb_idl *);
360 \f
361
362 /* ovsdb_idl_loop provides an easy way to manage the transactions related
363 * to 'idl' and to cope with different status during transaction. */
364 struct ovsdb_idl_loop {
365 struct ovsdb_idl *idl;
366 unsigned int skip_seqno;
367
368 struct ovsdb_idl_txn *committing_txn;
369 unsigned int precommit_seqno;
370
371 struct ovsdb_idl_txn *open_txn;
372
373 /* These members allow a client a simple, stateless way to keep track of
374 * transactions that commit: when a transaction commits successfully,
375 * ovsdb_idl_loop_commit_and_wait() copies 'next_cfg' to 'cur_cfg'. Thus,
376 * the client can set 'next_cfg' to a value that indicates a successful
377 * commit and check 'cur_cfg' on each iteration. */
378 int64_t cur_cfg;
379 int64_t next_cfg;
380 };
381
382 #define OVSDB_IDL_LOOP_INITIALIZER(IDL) { .idl = (IDL) }
383
384 void ovsdb_idl_loop_destroy(struct ovsdb_idl_loop *);
385 struct ovsdb_idl_txn *ovsdb_idl_loop_run(struct ovsdb_idl_loop *);
386 int ovsdb_idl_loop_commit_and_wait(struct ovsdb_idl_loop *);
387 \f
388 /* Conditional Replication
389 * =======================
390 *
391 * By default, when the IDL replicates a particular table in the database, it
392 * replicates every row in the table. These functions allow the client to
393 * specify that only selected rows should be replicated, by constructing a
394 * per-table condition that specifies the rows to replicate.
395 *
396 * A condition is a disjunction of clauses. The condition is true, and thus a
397 * row is replicated, if any of the clauses evaluates to true for a given row.
398 * (Thus, a condition with no clauses is always false.)
399 */
400
401 struct ovsdb_idl_condition {
402 struct hmap clauses; /* Contains "struct ovsdb_idl_clause"s. */
403 bool is_true; /* Is the condition unconditionally true? */
404 };
405 #define OVSDB_IDL_CONDITION_INIT(CONDITION) \
406 { HMAP_INITIALIZER(&(CONDITION)->clauses), false }
407
408 void ovsdb_idl_condition_init(struct ovsdb_idl_condition *);
409 void ovsdb_idl_condition_clear(struct ovsdb_idl_condition *);
410 void ovsdb_idl_condition_destroy(struct ovsdb_idl_condition *);
411 void ovsdb_idl_condition_add_clause(struct ovsdb_idl_condition *,
412 enum ovsdb_function function,
413 const struct ovsdb_idl_column *column,
414 const struct ovsdb_datum *arg);
415 void ovsdb_idl_condition_add_clause_true(struct ovsdb_idl_condition *);
416 bool ovsdb_idl_condition_is_true(const struct ovsdb_idl_condition *);
417
418 unsigned int ovsdb_idl_set_condition(struct ovsdb_idl *,
419 const struct ovsdb_idl_table_class *,
420 const struct ovsdb_idl_condition *);
421
422 unsigned int ovsdb_idl_get_condition_seqno(const struct ovsdb_idl *);
423 \f
424 /* Indexes over one or more columns in the IDL, to retrieve rows matching
425 * particular search criteria and to iterate over a subset of rows in a defined
426 * order. */
427
428 enum ovsdb_index_order {
429 OVSDB_INDEX_ASC, /* 0, 1, 2, ... */
430 OVSDB_INDEX_DESC /* 2, 1, 0, ... */
431 };
432
433 typedef int column_comparator_func(const void *a, const void *b);
434
435 struct ovsdb_idl_index_column {
436 const struct ovsdb_idl_column *column;
437 column_comparator_func *comparer;
438 enum ovsdb_index_order order;
439 };
440
441 /* Creating an index. */
442 struct ovsdb_idl_index *ovsdb_idl_index_create(
443 struct ovsdb_idl *, const struct ovsdb_idl_index_column *, size_t n);
444 struct ovsdb_idl_index *ovsdb_idl_index_create1(
445 struct ovsdb_idl *, const struct ovsdb_idl_column *);
446 struct ovsdb_idl_index *ovsdb_idl_index_create2(
447 struct ovsdb_idl *, const struct ovsdb_idl_column *,
448 const struct ovsdb_idl_column *);
449
450 /* Searching an index. */
451 struct ovsdb_idl_row *ovsdb_idl_index_find(struct ovsdb_idl_index *,
452 const struct ovsdb_idl_row *);
453 \f
454 /* Iteration over an index.
455 *
456 * Usually these would be invoked through table-specific wrappers generated
457 * by the IDL. */
458
459 struct ovsdb_idl_cursor {
460 struct ovsdb_idl_index *index; /* Index being iterated. */
461 struct skiplist_node *position; /* Current position in 'index'. */
462 };
463
464 struct ovsdb_idl_cursor ovsdb_idl_cursor_first(struct ovsdb_idl_index *);
465 struct ovsdb_idl_cursor ovsdb_idl_cursor_first_eq(
466 struct ovsdb_idl_index *, const struct ovsdb_idl_row *);
467 struct ovsdb_idl_cursor ovsdb_idl_cursor_first_ge(
468 struct ovsdb_idl_index *, const struct ovsdb_idl_row *);
469
470 void ovsdb_idl_cursor_next(struct ovsdb_idl_cursor *);
471 void ovsdb_idl_cursor_next_eq(struct ovsdb_idl_cursor *);
472
473 struct ovsdb_idl_row *ovsdb_idl_cursor_data(struct ovsdb_idl_cursor *);
474
475 #ifdef __cplusplus
476 }
477 #endif
478
479 #endif /* ovsdb-idl.h */