]> git.proxmox.com Git - ovs.git/blame - lib/ovsdb-idl.h
python: move Python idl to work with monitor_cond
[ovs.git] / lib / ovsdb-idl.h
CommitLineData
d322ffc8 1/* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
c3bb4bd7
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_IDL_H
17#define OVSDB_IDL_H 1
18
2ce42c88
BP
19/* Open vSwitch Database Interface Definition Language (OVSDB IDL).
20 *
21 * The OVSDB IDL maintains an in-memory replica of a database. It issues RPC
22 * requests to an OVSDB database server and parses the responses, converting
23 * raw JSON into data structures that are easier for clients to digest. Most
24 * notably, references to rows via UUID become C pointers.
25 *
d322ffc8
BP
26 * The IDL always presents a consistent snapshot of the database to its client,
27 * that is, it won't present the effects of some part of a transaction applied
28 * at the database server without presenting all of its effects.
29 *
2ce42c88
BP
30 * The IDL also assists with issuing database transactions. The client creates
31 * a transaction, manipulates the IDL data structures, and commits or aborts
32 * the transaction. The IDL then composes and issues the necessary JSON-RPC
33 * requests and reports to the client whether the transaction completed
34 * successfully.
35 */
36
f3d64521 37#include <stdbool.h>
b54e22e9 38#include <stdint.h>
e1c0e2d1 39#include "compiler.h"
8c3c2f30 40#include "ovsdb-types.h"
b54e22e9
BP
41
42struct json;
979821c0 43struct ovsdb_datum;
c3bb4bd7 44struct ovsdb_idl_class;
932104f4 45struct ovsdb_idl_row;
979821c0
BP
46struct ovsdb_idl_column;
47struct ovsdb_idl_table_class;
48struct uuid;
c3bb4bd7
BP
49
50struct ovsdb_idl *ovsdb_idl_create(const char *remote,
ef73f86c 51 const struct ovsdb_idl_class *,
fba6bd1d
BP
52 bool monitor_everything_by_default,
53 bool retry);
1b62572d 54void ovsdb_idl_set_remote(struct ovsdb_idl *, const char *, bool);
c3bb4bd7
BP
55void ovsdb_idl_destroy(struct ovsdb_idl *);
56
854a94d9 57void ovsdb_idl_run(struct ovsdb_idl *);
c3bb4bd7
BP
58void ovsdb_idl_wait(struct ovsdb_idl *);
59
06b6d651
BP
60void ovsdb_idl_set_lock(struct ovsdb_idl *, const char *lock_name);
61bool ovsdb_idl_has_lock(const struct ovsdb_idl *);
62bool ovsdb_idl_is_lock_contended(const struct ovsdb_idl *);
63
7152b6fa 64const struct uuid * ovsdb_idl_get_monitor_id(const struct ovsdb_idl *);
c3bb4bd7 65unsigned int ovsdb_idl_get_seqno(const struct ovsdb_idl *);
f3d64521 66bool ovsdb_idl_has_ever_connected(const struct ovsdb_idl *);
705d7a39 67void ovsdb_idl_enable_reconnect(struct ovsdb_idl *);
c3bb4bd7 68void ovsdb_idl_force_reconnect(struct ovsdb_idl *);
8cdec725 69void ovsdb_idl_verify_write_only(struct ovsdb_idl *);
fba6bd1d
BP
70
71bool ovsdb_idl_is_alive(const struct ovsdb_idl *);
72int ovsdb_idl_get_last_error(const struct ovsdb_idl *);
a4927e36
HL
73
74void ovsdb_idl_set_probe_interval(const struct ovsdb_idl *, int probe_interval);
ef73f86c
BP
75\f
76/* Choosing columns and tables to replicate. */
77
78/* Modes with which the IDL can monitor a column.
79 *
80 * If no bits are set, the column is not monitored at all. Its value will
81 * always appear to the client to be the default value for its type.
82 *
83 * If OVSDB_IDL_MONITOR is set, then the column is replicated. Its value will
854a94d9
BP
84 * reflect the value in the database. If OVSDB_IDL_ALERT is also set, then the
85 * value returned by ovsdb_idl_get_seqno() will change when the column's value
86 * changes.
ef73f86c
BP
87 *
88 * The possible mode combinations are:
89 *
90 * - 0, for a column that a client doesn't care about.
91 *
92 * - (OVSDB_IDL_MONITOR | OVSDB_IDL_ALERT), for a column that a client wants
93 * to track and possibly update.
94 *
95 * - OVSDB_IDL_MONITOR, for columns that a client treats as "write-only",
96 * that is, it updates them but doesn't want to get alerted about its own
97 * updates. It also won't be alerted about other clients' updates, so this
98 * is suitable only for use by a client that "owns" a particular column.
99 *
100 * - OVDSB_IDL_ALERT without OVSDB_IDL_MONITOR is not valid.
932104f4
SA
101 *
102 * - (OVSDB_IDL_MONITOR | OVSDB_IDL_ALERT | OVSDB_IDL_TRACK), for a column
103 * that a client wants to track using the change tracking
104 * ovsdb_idl_track_get_*() functions.
ef73f86c
BP
105 */
106#define OVSDB_IDL_MONITOR (1 << 0) /* Monitor this column? */
107#define OVSDB_IDL_ALERT (1 << 1) /* Alert client when column updated? */
932104f4 108#define OVSDB_IDL_TRACK (1 << 2)
ef73f86c
BP
109
110void ovsdb_idl_add_column(struct ovsdb_idl *, const struct ovsdb_idl_column *);
111void ovsdb_idl_add_table(struct ovsdb_idl *,
112 const struct ovsdb_idl_table_class *);
c3bb4bd7 113
c547535a 114void ovsdb_idl_omit(struct ovsdb_idl *, const struct ovsdb_idl_column *);
ef73f86c 115void ovsdb_idl_omit_alert(struct ovsdb_idl *, const struct ovsdb_idl_column *);
932104f4 116
104aec4e
RM
117/* Change tracking.
118 *
119 * In OVSDB, change tracking is applied at each client in the IDL layer. This
120 * means that when a client makes a request to track changes on a particular
121 * table, they are essentially requesting information about the incremental
122 * changes to that table from the point in time that the request is made. Once
123 * the client clears tracked changes, that information will no longer be
124 * available.
125 *
126 * The implication of the above is that if a client requires replaying
127 * untracked history, it faces the choice of either trying to remember changes
128 * itself (which translates into a memory leak) or of being structured with a
129 * path for processing the full untracked table as well as a path that
130 * processes incremental changes. */
932104f4
SA
131enum ovsdb_idl_change {
132 OVSDB_IDL_CHANGE_INSERT,
133 OVSDB_IDL_CHANGE_MODIFY,
134 OVSDB_IDL_CHANGE_DELETE,
135 OVSDB_IDL_CHANGE_MAX
136};
137
138/* Row, table sequence numbers */
139unsigned int ovsdb_idl_table_get_seqno(
140 const struct ovsdb_idl *idl,
141 const struct ovsdb_idl_table_class *table_class);
142unsigned int ovsdb_idl_row_get_seqno(
143 const struct ovsdb_idl_row *row,
144 enum ovsdb_idl_change change);
145
146void ovsdb_idl_track_add_column(struct ovsdb_idl *idl,
147 const struct ovsdb_idl_column *column);
148void ovsdb_idl_track_add_all(struct ovsdb_idl *idl);
149const struct ovsdb_idl_row *ovsdb_idl_track_get_first(
150 const struct ovsdb_idl *, const struct ovsdb_idl_table_class *);
151const struct ovsdb_idl_row *ovsdb_idl_track_get_next(const struct ovsdb_idl_row *);
32d37ce8
SA
152bool ovsdb_idl_track_is_updated(const struct ovsdb_idl_row *row,
153 const struct ovsdb_idl_column *column);
932104f4
SA
154void ovsdb_idl_track_clear(const struct ovsdb_idl *);
155
ef73f86c
BP
156\f
157/* Reading the database replica. */
c547535a 158
979821c0
BP
159const struct ovsdb_idl_row *ovsdb_idl_get_row_for_uuid(
160 const struct ovsdb_idl *, const struct ovsdb_idl_table_class *,
161 const struct uuid *);
162const struct ovsdb_idl_row *ovsdb_idl_first_row(
163 const struct ovsdb_idl *, const struct ovsdb_idl_table_class *);
164const struct ovsdb_idl_row *ovsdb_idl_next_row(const struct ovsdb_idl_row *);
165
8c3c2f30
BP
166const struct ovsdb_datum *ovsdb_idl_read(const struct ovsdb_idl_row *,
167 const struct ovsdb_idl_column *);
168const struct ovsdb_datum *ovsdb_idl_get(const struct ovsdb_idl_row *,
169 const struct ovsdb_idl_column *,
170 enum ovsdb_atomic_type key_type,
171 enum ovsdb_atomic_type value_type);
ff495b63
BP
172bool ovsdb_idl_is_mutable(const struct ovsdb_idl_row *,
173 const struct ovsdb_idl_column *);
cfea354b
BP
174
175bool ovsdb_idl_row_is_synthetic(const struct ovsdb_idl_row *);
ef73f86c 176\f
2f926787
BP
177/* Transactions.
178 *
179 * A transaction may modify the contents of a database by modifying the values
180 * of columns, deleting rows, inserting rows, or adding checks that columns in
181 * the database have not changed ("verify" operations), through
182 * ovsdb_idl_txn_*() functions. (The OVSDB IDL code generator produces helper
183 * functions that internally call the ovsdb_idl_txn_*() functions. These are
184 * likely to be more convenient.)
185 *
186 * Reading and writing columns and inserting and deleting rows are all
187 * straightforward. The reasons to verify columns are less obvious.
188 * Verification is the key to maintaining transactional integrity. Because
189 * OVSDB handles multiple clients, it can happen that between the time that
190 * OVSDB client A reads a column and writes a new value, OVSDB client B has
191 * written that column. Client A's write should not ordinarily overwrite
192 * client B's, especially if the column in question is a "map" column that
193 * contains several more or less independent data items. If client A adds a
194 * "verify" operation before it writes the column, then the transaction fails
195 * in case client B modifies it first. Client A will then see the new value of
196 * the column and compose a new transaction based on the new contents written
197 * by client B.
198 *
199 * When a transaction is complete, which must be before the next call to
200 * ovsdb_idl_run() on 'idl', call ovsdb_idl_txn_commit() or
201 * ovsdb_idl_txn_abort().
202 *
203 * The life-cycle of a transaction looks like this:
204 *
205 * 1. Create the transaction and record the initial sequence number:
206 *
207 * seqno = ovsdb_idl_get_seqno(idl);
208 * txn = ovsdb_idl_txn_create(idl);
209 *
210 * 2. Modify the database with ovsdb_idl_txn_*() functions directly or
211 * indirectly.
212 *
213 * 3. Commit the transaction by calling ovsdb_idl_txn_commit(). The first call
214 * to this function probably returns TXN_INCOMPLETE. The client must keep
215 * calling again along as this remains true, calling ovsdb_idl_run() in
216 * between to let the IDL do protocol processing. (If the client doesn't
217 * have anything else to do in the meantime, it can use
218 * ovsdb_idl_txn_commit_block() to avoid having to loop itself.)
219 *
220 * 4. If the final status is TXN_TRY_AGAIN, wait for ovsdb_idl_get_seqno() to
221 * change from the saved 'seqno' (it's possible that it's already changed,
222 * in which case the client should not wait at all), then start over from
223 * step 1. Only a call to ovsdb_idl_run() will change the return value of
224 * ovsdb_idl_get_seqno(). (ovsdb_idl_txn_commit_block() calls
225 * ovsdb_idl_run().)
226 */
8c3c2f30 227
475281c0 228enum ovsdb_idl_txn_status {
2096903b 229 TXN_UNCOMMITTED, /* Not yet committed or aborted. */
b54e22e9 230 TXN_UNCHANGED, /* Transaction didn't include any changes. */
475281c0
BP
231 TXN_INCOMPLETE, /* Commit in progress, please wait. */
232 TXN_ABORTED, /* ovsdb_idl_txn_abort() called. */
233 TXN_SUCCESS, /* Commit successful. */
854a94d9 234 TXN_TRY_AGAIN, /* Commit failed because a "verify" operation
475281c0 235 * reported an inconsistency, due to a network
4fdfe5cc
BP
236 * problem, or other transient failure. Wait
237 * for a change, then try again. */
06b6d651 238 TXN_NOT_LOCKED, /* Server hasn't given us the lock yet. */
475281c0
BP
239 TXN_ERROR /* Commit failed due to a hard error. */
240};
241
242const char *ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status);
243
244struct ovsdb_idl_txn *ovsdb_idl_txn_create(struct ovsdb_idl *);
e1c0e2d1 245void ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *, const char *, ...)
cab50449 246 OVS_PRINTF_FORMAT (2, 3);
577aebdf 247void ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *);
94fbe1aa
BP
248void ovsdb_idl_txn_increment(struct ovsdb_idl_txn *,
249 const struct ovsdb_idl_row *,
250 const struct ovsdb_idl_column *);
475281c0 251void ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *);
586bb84a 252void ovsdb_idl_txn_wait(const struct ovsdb_idl_txn *);
475281c0 253enum ovsdb_idl_txn_status ovsdb_idl_txn_commit(struct ovsdb_idl_txn *);
af96ccd2 254enum ovsdb_idl_txn_status ovsdb_idl_txn_commit_block(struct ovsdb_idl_txn *);
475281c0
BP
255void ovsdb_idl_txn_abort(struct ovsdb_idl_txn *);
256
91e310a5
BP
257const char *ovsdb_idl_txn_get_error(const struct ovsdb_idl_txn *);
258
69490970
BP
259int64_t ovsdb_idl_txn_get_increment_new_value(const struct ovsdb_idl_txn *);
260const struct uuid *ovsdb_idl_txn_get_insert_uuid(const struct ovsdb_idl_txn *,
261 const struct uuid *);
262
979821c0
BP
263void ovsdb_idl_txn_write(const struct ovsdb_idl_row *,
264 const struct ovsdb_idl_column *,
265 struct ovsdb_datum *);
fe19569a
BP
266void ovsdb_idl_txn_write_clone(const struct ovsdb_idl_row *,
267 const struct ovsdb_idl_column *,
268 const struct ovsdb_datum *);
f199df26
EA
269void ovsdb_idl_txn_write_partial_map(const struct ovsdb_idl_row *,
270 const struct ovsdb_idl_column *,
271 struct ovsdb_datum *);
272void ovsdb_idl_txn_delete_partial_map(const struct ovsdb_idl_row *,
273 const struct ovsdb_idl_column *,
274 struct ovsdb_datum *);
9e336f49
BP
275void ovsdb_idl_txn_delete(const struct ovsdb_idl_row *);
276const struct ovsdb_idl_row *ovsdb_idl_txn_insert(
ce5a3e38
BP
277 struct ovsdb_idl_txn *, const struct ovsdb_idl_table_class *,
278 const struct uuid *);
979821c0 279
1e86ae6f 280struct ovsdb_idl *ovsdb_idl_txn_get_idl (struct ovsdb_idl_txn *);
a660eac8 281void ovsdb_idl_get_initial_snapshot(struct ovsdb_idl *);
a548a764
AW
282\f
283
284/* ovsdb_idl_loop provides an easy way to manage the transactions related
285 * to 'idl' and to cope with different status during transaction. */
286struct ovsdb_idl_loop {
287 struct ovsdb_idl *idl;
288 unsigned int skip_seqno;
289
290 struct ovsdb_idl_txn *committing_txn;
291 unsigned int precommit_seqno;
292
293 struct ovsdb_idl_txn *open_txn;
294};
295
296#define OVSDB_IDL_LOOP_INITIALIZER(IDL) { .idl = (IDL) }
297
298void ovsdb_idl_loop_destroy(struct ovsdb_idl_loop *);
299struct ovsdb_idl_txn *ovsdb_idl_loop_run(struct ovsdb_idl_loop *);
300void ovsdb_idl_loop_commit_and_wait(struct ovsdb_idl_loop *);
1e86ae6f 301
c3bb4bd7 302#endif /* ovsdb-idl.h */