ovn: Make it possible for CMS to detect when the OVN system is up-to-date.
[cascardo/ovs.git] / lib / ovsdb-idl.h
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
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
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  *
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  *
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
37 #include <stdbool.h>
38 #include <stdint.h>
39 #include "compiler.h"
40 #include "ovsdb-types.h"
41 #include "ovsdb-data.h"
42 #include "openvswitch/list.h"
43 #include "ovsdb-condition.h"
44
45 struct json;
46 struct ovsdb_datum;
47 struct ovsdb_idl_class;
48 struct ovsdb_idl_row;
49 struct ovsdb_idl_column;
50 struct ovsdb_idl_table_class;
51 struct ovsdb_idl_condition;
52 struct uuid;
53
54 struct ovsdb_idl *ovsdb_idl_create(const char *remote,
55                                    const struct ovsdb_idl_class *,
56                                    bool monitor_everything_by_default,
57                                    bool retry);
58 void ovsdb_idl_set_remote(struct ovsdb_idl *, const char *, bool);
59 void ovsdb_idl_destroy(struct ovsdb_idl *);
60
61 void ovsdb_idl_run(struct ovsdb_idl *);
62 void ovsdb_idl_wait(struct ovsdb_idl *);
63
64 void ovsdb_idl_set_lock(struct ovsdb_idl *, const char *lock_name);
65 bool ovsdb_idl_has_lock(const struct ovsdb_idl *);
66 bool ovsdb_idl_is_lock_contended(const struct ovsdb_idl *);
67
68 const struct uuid  * ovsdb_idl_get_monitor_id(const struct ovsdb_idl *);
69 unsigned int ovsdb_idl_get_seqno(const struct ovsdb_idl *);
70 bool ovsdb_idl_has_ever_connected(const struct ovsdb_idl *);
71 void ovsdb_idl_enable_reconnect(struct ovsdb_idl *);
72 void ovsdb_idl_force_reconnect(struct ovsdb_idl *);
73 void ovsdb_idl_verify_write_only(struct ovsdb_idl *);
74
75 bool ovsdb_idl_is_alive(const struct ovsdb_idl *);
76 int ovsdb_idl_get_last_error(const struct ovsdb_idl *);
77
78 void ovsdb_idl_set_probe_interval(const struct ovsdb_idl *, int probe_interval);
79 \f
80 /* Choosing columns and tables to replicate. */
81
82 /* Modes with which the IDL can monitor a column.
83  *
84  * If no bits are set, the column is not monitored at all.  Its value will
85  * always appear to the client to be the default value for its type.
86  *
87  * If OVSDB_IDL_MONITOR is set, then the column is replicated.  Its value will
88  * reflect the value in the database.  If OVSDB_IDL_ALERT is also set, then the
89  * value returned by ovsdb_idl_get_seqno() will change when the column's value
90  * changes.
91  *
92  * The possible mode combinations are:
93  *
94  *   - 0, for a column that a client doesn't care about.
95  *
96  *   - (OVSDB_IDL_MONITOR | OVSDB_IDL_ALERT), for a column that a client wants
97  *     to track and possibly update.
98  *
99  *   - OVSDB_IDL_MONITOR, for columns that a client treats as "write-only",
100  *     that is, it updates them but doesn't want to get alerted about its own
101  *     updates.  It also won't be alerted about other clients' updates, so this
102  *     is suitable only for use by a client that "owns" a particular column.
103  *
104  *   - OVDSB_IDL_ALERT without OVSDB_IDL_MONITOR is not valid.
105  *
106  *   - (OVSDB_IDL_MONITOR | OVSDB_IDL_ALERT | OVSDB_IDL_TRACK), for a column
107  *     that a client wants to track using the change tracking
108  *     ovsdb_idl_track_get_*() functions.
109  */
110 #define OVSDB_IDL_MONITOR (1 << 0) /* Monitor this column? */
111 #define OVSDB_IDL_ALERT   (1 << 1) /* Alert client when column updated? */
112 #define OVSDB_IDL_TRACK   (1 << 2)
113
114 void ovsdb_idl_add_column(struct ovsdb_idl *, const struct ovsdb_idl_column *);
115 void ovsdb_idl_add_table(struct ovsdb_idl *,
116                          const struct ovsdb_idl_table_class *);
117
118 void ovsdb_idl_omit(struct ovsdb_idl *, const struct ovsdb_idl_column *);
119 void ovsdb_idl_omit_alert(struct ovsdb_idl *, const struct ovsdb_idl_column *);
120
121 /* Change tracking.
122  *
123  * In OVSDB, change tracking is applied at each client in the IDL layer.  This
124  * means that when a client makes a request to track changes on a particular
125  * table, they are essentially requesting information about the incremental
126  * changes to that table from the point in time that the request is made.  Once
127  * the client clears tracked changes, that information will no longer be
128  * available.
129  *
130  * The implication of the above is that if a client requires replaying
131  * untracked history, it faces the choice of either trying to remember changes
132  * itself (which translates into a memory leak) or of being structured with a
133  * path for processing the full untracked table as well as a path that
134  * processes incremental changes. */
135 enum ovsdb_idl_change {
136     OVSDB_IDL_CHANGE_INSERT,
137     OVSDB_IDL_CHANGE_MODIFY,
138     OVSDB_IDL_CHANGE_DELETE,
139     OVSDB_IDL_CHANGE_MAX
140 };
141
142 /* Row, table sequence numbers */
143 unsigned int ovsdb_idl_table_get_seqno(
144     const struct ovsdb_idl *idl,
145     const struct ovsdb_idl_table_class *table_class);
146 unsigned int ovsdb_idl_row_get_seqno(
147     const struct ovsdb_idl_row *row,
148     enum ovsdb_idl_change change);
149
150 void ovsdb_idl_track_add_column(struct ovsdb_idl *idl,
151                                 const struct ovsdb_idl_column *column);
152 void ovsdb_idl_track_add_all(struct ovsdb_idl *idl);
153 const struct ovsdb_idl_row *ovsdb_idl_track_get_first(
154     const struct ovsdb_idl *, const struct ovsdb_idl_table_class *);
155 const struct ovsdb_idl_row *ovsdb_idl_track_get_next(const struct ovsdb_idl_row *);
156 bool ovsdb_idl_track_is_updated(const struct ovsdb_idl_row *row,
157                                 const struct ovsdb_idl_column *column);
158 void ovsdb_idl_track_clear(const struct ovsdb_idl *);
159
160 \f
161 /* Reading the database replica. */
162
163 const struct ovsdb_idl_row *ovsdb_idl_get_row_for_uuid(
164     const struct ovsdb_idl *, const struct ovsdb_idl_table_class *,
165     const struct uuid *);
166 const struct ovsdb_idl_row *ovsdb_idl_first_row(
167     const struct ovsdb_idl *, const struct ovsdb_idl_table_class *);
168 const struct ovsdb_idl_row *ovsdb_idl_next_row(const struct ovsdb_idl_row *);
169
170 const struct ovsdb_datum *ovsdb_idl_read(const struct ovsdb_idl_row *,
171                                          const struct ovsdb_idl_column *);
172 const struct ovsdb_datum *ovsdb_idl_get(const struct ovsdb_idl_row *,
173                                         const struct ovsdb_idl_column *,
174                                         enum ovsdb_atomic_type key_type,
175                                         enum ovsdb_atomic_type value_type);
176 bool ovsdb_idl_is_mutable(const struct ovsdb_idl_row *,
177                           const struct ovsdb_idl_column *);
178
179 bool ovsdb_idl_row_is_synthetic(const struct ovsdb_idl_row *);
180 \f
181 /* Transactions.
182  *
183  * A transaction may modify the contents of a database by modifying the values
184  * of columns, deleting rows, inserting rows, or adding checks that columns in
185  * the database have not changed ("verify" operations), through
186  * ovsdb_idl_txn_*() functions.  (The OVSDB IDL code generator produces helper
187  * functions that internally call the ovsdb_idl_txn_*() functions.  These are
188  * likely to be more convenient.)
189  *
190  * Reading and writing columns and inserting and deleting rows are all
191  * straightforward.  The reasons to verify columns are less obvious.
192  * Verification is the key to maintaining transactional integrity.  Because
193  * OVSDB handles multiple clients, it can happen that between the time that
194  * OVSDB client A reads a column and writes a new value, OVSDB client B has
195  * written that column.  Client A's write should not ordinarily overwrite
196  * client B's, especially if the column in question is a "map" column that
197  * contains several more or less independent data items.  If client A adds a
198  * "verify" operation before it writes the column, then the transaction fails
199  * in case client B modifies it first.  Client A will then see the new value of
200  * the column and compose a new transaction based on the new contents written
201  * by client B.
202  *
203  * When a transaction is complete, which must be before the next call to
204  * ovsdb_idl_run() on 'idl', call ovsdb_idl_txn_commit() or
205  * ovsdb_idl_txn_abort().
206  *
207  * The life-cycle of a transaction looks like this:
208  *
209  * 1. Create the transaction and record the initial sequence number:
210  *
211  *     seqno = ovsdb_idl_get_seqno(idl);
212  *     txn = ovsdb_idl_txn_create(idl);
213  *
214  * 2. Modify the database with ovsdb_idl_txn_*() functions directly or
215  *    indirectly.
216  *
217  * 3. Commit the transaction by calling ovsdb_idl_txn_commit().  The first call
218  *    to this function probably returns TXN_INCOMPLETE.  The client must keep
219  *    calling again along as this remains true, calling ovsdb_idl_run() in
220  *    between to let the IDL do protocol processing.  (If the client doesn't
221  *    have anything else to do in the meantime, it can use
222  *    ovsdb_idl_txn_commit_block() to avoid having to loop itself.)
223  *
224  * 4. If the final status is TXN_TRY_AGAIN, wait for ovsdb_idl_get_seqno() to
225  *    change from the saved 'seqno' (it's possible that it's already changed,
226  *    in which case the client should not wait at all), then start over from
227  *    step 1.  Only a call to ovsdb_idl_run() will change the return value of
228  *    ovsdb_idl_get_seqno().  (ovsdb_idl_txn_commit_block() calls
229  *    ovsdb_idl_run().)
230  */
231
232 enum ovsdb_idl_txn_status {
233     TXN_UNCOMMITTED,            /* Not yet committed or aborted. */
234     TXN_UNCHANGED,              /* Transaction didn't include any changes. */
235     TXN_INCOMPLETE,             /* Commit in progress, please wait. */
236     TXN_ABORTED,                /* ovsdb_idl_txn_abort() called. */
237     TXN_SUCCESS,                /* Commit successful. */
238     TXN_TRY_AGAIN,              /* Commit failed because a "verify" operation
239                                  * reported an inconsistency, due to a network
240                                  * problem, or other transient failure.  Wait
241                                  * for a change, then try again. */
242     TXN_NOT_LOCKED,             /* Server hasn't given us the lock yet. */
243     TXN_ERROR                   /* Commit failed due to a hard error. */
244 };
245
246 const char *ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status);
247
248 struct ovsdb_idl_txn *ovsdb_idl_txn_create(struct ovsdb_idl *);
249 void ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *, const char *, ...)
250     OVS_PRINTF_FORMAT (2, 3);
251 void ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *);
252 void ovsdb_idl_txn_increment(struct ovsdb_idl_txn *,
253                              const struct ovsdb_idl_row *,
254                              const struct ovsdb_idl_column *);
255 void ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *);
256 void ovsdb_idl_txn_wait(const struct ovsdb_idl_txn *);
257 enum ovsdb_idl_txn_status ovsdb_idl_txn_commit(struct ovsdb_idl_txn *);
258 enum ovsdb_idl_txn_status ovsdb_idl_txn_commit_block(struct ovsdb_idl_txn *);
259 void ovsdb_idl_txn_abort(struct ovsdb_idl_txn *);
260
261 const char *ovsdb_idl_txn_get_error(const struct ovsdb_idl_txn *);
262
263 int64_t ovsdb_idl_txn_get_increment_new_value(const struct ovsdb_idl_txn *);
264 const struct uuid *ovsdb_idl_txn_get_insert_uuid(const struct ovsdb_idl_txn *,
265                                                  const struct uuid *);
266
267 void ovsdb_idl_txn_write(const struct ovsdb_idl_row *,
268                          const struct ovsdb_idl_column *,
269                          struct ovsdb_datum *);
270 void ovsdb_idl_txn_write_clone(const struct ovsdb_idl_row *,
271                                const struct ovsdb_idl_column *,
272                                const struct ovsdb_datum *);
273 void ovsdb_idl_txn_write_partial_map(const struct ovsdb_idl_row *,
274                                      const struct ovsdb_idl_column *,
275                                      struct ovsdb_datum *);
276 void ovsdb_idl_txn_delete_partial_map(const struct ovsdb_idl_row *,
277                                       const struct ovsdb_idl_column *,
278                                       struct ovsdb_datum *);
279 void ovsdb_idl_txn_delete(const struct ovsdb_idl_row *);
280 const struct ovsdb_idl_row *ovsdb_idl_txn_insert(
281     struct ovsdb_idl_txn *, const struct ovsdb_idl_table_class *,
282     const struct uuid *);
283
284 struct ovsdb_idl *ovsdb_idl_txn_get_idl (struct ovsdb_idl_txn *);
285 void ovsdb_idl_get_initial_snapshot(struct ovsdb_idl *);
286 \f
287
288 /* ovsdb_idl_loop provides an easy way to manage the transactions related
289  * to 'idl' and to cope with different status during transaction. */
290 struct ovsdb_idl_loop {
291     struct ovsdb_idl *idl;
292     unsigned int skip_seqno;
293
294     struct ovsdb_idl_txn *committing_txn;
295     unsigned int precommit_seqno;
296
297     struct ovsdb_idl_txn *open_txn;
298
299     /* These members allow a client a simple, stateless way to keep track of
300      * transactions that commit: when a transaction commits successfully,
301      * ovsdb_idl_loop_commit_and_wait() copies 'next_cfg' to 'cur_cfg'.  Thus,
302      * the client can set 'next_cfg' to a value that indicates a successful
303      * commit and check 'cur_cfg' on each iteration. */
304     int64_t cur_cfg;
305     int64_t next_cfg;
306 };
307
308 #define OVSDB_IDL_LOOP_INITIALIZER(IDL) { .idl = (IDL) }
309
310 void ovsdb_idl_loop_destroy(struct ovsdb_idl_loop *);
311 struct ovsdb_idl_txn *ovsdb_idl_loop_run(struct ovsdb_idl_loop *);
312 void ovsdb_idl_loop_commit_and_wait(struct ovsdb_idl_loop *);
313
314 struct ovsdb_idl_condition {
315     const struct ovsdb_idl_table_class *tc;
316     struct ovs_list clauses;
317 };
318
319 struct ovsdb_idl_clause {
320     struct ovs_list node;
321     enum ovsdb_function function;
322     const struct ovsdb_idl_column *column;
323     struct ovsdb_datum arg;
324 };
325
326 void ovsdb_idl_condition_reset(struct ovsdb_idl *idl,
327                                const struct ovsdb_idl_table_class *tc);
328 void ovsdb_idl_condition_add_clause(struct ovsdb_idl *idl,
329                                     const struct ovsdb_idl_table_class *tc,
330                                     enum ovsdb_function function,
331                                     const struct ovsdb_idl_column *column,
332                                     struct ovsdb_datum *arg);
333 void ovsdb_idl_condition_remove_clause(struct ovsdb_idl *idl,
334                                        const struct ovsdb_idl_table_class *tc,
335                                        enum ovsdb_function function,
336                                        const struct ovsdb_idl_column *column,
337                                        struct ovsdb_datum *arg);
338
339 #endif /* ovsdb-idl.h */