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