]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ovsdb-idl.h
idl-loop: Move idl-loop into ovsdb-idl library.
[mirror_ovs.git] / lib / ovsdb-idl.h
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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
42 struct json;
43 struct ovsdb_datum;
44 struct ovsdb_idl_class;
45 struct ovsdb_idl_column;
46 struct ovsdb_idl_table_class;
47 struct uuid;
48
49 struct ovsdb_idl *ovsdb_idl_create(const char *remote,
50 const struct ovsdb_idl_class *,
51 bool monitor_everything_by_default,
52 bool retry);
53 void ovsdb_idl_destroy(struct ovsdb_idl *);
54
55 void ovsdb_idl_run(struct ovsdb_idl *);
56 void ovsdb_idl_wait(struct ovsdb_idl *);
57
58 void ovsdb_idl_set_lock(struct ovsdb_idl *, const char *lock_name);
59 bool ovsdb_idl_has_lock(const struct ovsdb_idl *);
60 bool ovsdb_idl_is_lock_contended(const struct ovsdb_idl *);
61
62 unsigned int ovsdb_idl_get_seqno(const struct ovsdb_idl *);
63 bool ovsdb_idl_has_ever_connected(const struct ovsdb_idl *);
64 void ovsdb_idl_enable_reconnect(struct ovsdb_idl *);
65 void ovsdb_idl_force_reconnect(struct ovsdb_idl *);
66 void ovsdb_idl_verify_write_only(struct ovsdb_idl *);
67
68 bool ovsdb_idl_is_alive(const struct ovsdb_idl *);
69 int ovsdb_idl_get_last_error(const struct ovsdb_idl *);
70 \f
71 /* Choosing columns and tables to replicate. */
72
73 /* Modes with which the IDL can monitor a column.
74 *
75 * If no bits are set, the column is not monitored at all. Its value will
76 * always appear to the client to be the default value for its type.
77 *
78 * If OVSDB_IDL_MONITOR is set, then the column is replicated. Its value will
79 * reflect the value in the database. If OVSDB_IDL_ALERT is also set, then the
80 * value returned by ovsdb_idl_get_seqno() will change when the column's value
81 * changes.
82 *
83 * The possible mode combinations are:
84 *
85 * - 0, for a column that a client doesn't care about.
86 *
87 * - (OVSDB_IDL_MONITOR | OVSDB_IDL_ALERT), for a column that a client wants
88 * to track and possibly update.
89 *
90 * - OVSDB_IDL_MONITOR, for columns that a client treats as "write-only",
91 * that is, it updates them but doesn't want to get alerted about its own
92 * updates. It also won't be alerted about other clients' updates, so this
93 * is suitable only for use by a client that "owns" a particular column.
94 *
95 * - OVDSB_IDL_ALERT without OVSDB_IDL_MONITOR is not valid.
96 */
97 #define OVSDB_IDL_MONITOR (1 << 0) /* Monitor this column? */
98 #define OVSDB_IDL_ALERT (1 << 1) /* Alert client when column updated? */
99
100 void ovsdb_idl_add_column(struct ovsdb_idl *, const struct ovsdb_idl_column *);
101 void ovsdb_idl_add_table(struct ovsdb_idl *,
102 const struct ovsdb_idl_table_class *);
103
104 void ovsdb_idl_omit(struct ovsdb_idl *, const struct ovsdb_idl_column *);
105 void ovsdb_idl_omit_alert(struct ovsdb_idl *, const struct ovsdb_idl_column *);
106 \f
107 /* Reading the database replica. */
108
109 const struct ovsdb_idl_row *ovsdb_idl_get_row_for_uuid(
110 const struct ovsdb_idl *, const struct ovsdb_idl_table_class *,
111 const struct uuid *);
112 const struct ovsdb_idl_row *ovsdb_idl_first_row(
113 const struct ovsdb_idl *, const struct ovsdb_idl_table_class *);
114 const struct ovsdb_idl_row *ovsdb_idl_next_row(const struct ovsdb_idl_row *);
115
116 const struct ovsdb_datum *ovsdb_idl_read(const struct ovsdb_idl_row *,
117 const struct ovsdb_idl_column *);
118 const struct ovsdb_datum *ovsdb_idl_get(const struct ovsdb_idl_row *,
119 const struct ovsdb_idl_column *,
120 enum ovsdb_atomic_type key_type,
121 enum ovsdb_atomic_type value_type);
122 bool ovsdb_idl_is_mutable(const struct ovsdb_idl_row *,
123 const struct ovsdb_idl_column *);
124
125 bool ovsdb_idl_row_is_synthetic(const struct ovsdb_idl_row *);
126 \f
127 /* Transactions.
128 *
129 * A transaction may modify the contents of a database by modifying the values
130 * of columns, deleting rows, inserting rows, or adding checks that columns in
131 * the database have not changed ("verify" operations), through
132 * ovsdb_idl_txn_*() functions. (The OVSDB IDL code generator produces helper
133 * functions that internally call the ovsdb_idl_txn_*() functions. These are
134 * likely to be more convenient.)
135 *
136 * Reading and writing columns and inserting and deleting rows are all
137 * straightforward. The reasons to verify columns are less obvious.
138 * Verification is the key to maintaining transactional integrity. Because
139 * OVSDB handles multiple clients, it can happen that between the time that
140 * OVSDB client A reads a column and writes a new value, OVSDB client B has
141 * written that column. Client A's write should not ordinarily overwrite
142 * client B's, especially if the column in question is a "map" column that
143 * contains several more or less independent data items. If client A adds a
144 * "verify" operation before it writes the column, then the transaction fails
145 * in case client B modifies it first. Client A will then see the new value of
146 * the column and compose a new transaction based on the new contents written
147 * by client B.
148 *
149 * When a transaction is complete, which must be before the next call to
150 * ovsdb_idl_run() on 'idl', call ovsdb_idl_txn_commit() or
151 * ovsdb_idl_txn_abort().
152 *
153 * The life-cycle of a transaction looks like this:
154 *
155 * 1. Create the transaction and record the initial sequence number:
156 *
157 * seqno = ovsdb_idl_get_seqno(idl);
158 * txn = ovsdb_idl_txn_create(idl);
159 *
160 * 2. Modify the database with ovsdb_idl_txn_*() functions directly or
161 * indirectly.
162 *
163 * 3. Commit the transaction by calling ovsdb_idl_txn_commit(). The first call
164 * to this function probably returns TXN_INCOMPLETE. The client must keep
165 * calling again along as this remains true, calling ovsdb_idl_run() in
166 * between to let the IDL do protocol processing. (If the client doesn't
167 * have anything else to do in the meantime, it can use
168 * ovsdb_idl_txn_commit_block() to avoid having to loop itself.)
169 *
170 * 4. If the final status is TXN_TRY_AGAIN, wait for ovsdb_idl_get_seqno() to
171 * change from the saved 'seqno' (it's possible that it's already changed,
172 * in which case the client should not wait at all), then start over from
173 * step 1. Only a call to ovsdb_idl_run() will change the return value of
174 * ovsdb_idl_get_seqno(). (ovsdb_idl_txn_commit_block() calls
175 * ovsdb_idl_run().)
176 */
177
178 enum ovsdb_idl_txn_status {
179 TXN_UNCOMMITTED, /* Not yet committed or aborted. */
180 TXN_UNCHANGED, /* Transaction didn't include any changes. */
181 TXN_INCOMPLETE, /* Commit in progress, please wait. */
182 TXN_ABORTED, /* ovsdb_idl_txn_abort() called. */
183 TXN_SUCCESS, /* Commit successful. */
184 TXN_TRY_AGAIN, /* Commit failed because a "verify" operation
185 * reported an inconsistency, due to a network
186 * problem, or other transient failure. Wait
187 * for a change, then try again. */
188 TXN_NOT_LOCKED, /* Server hasn't given us the lock yet. */
189 TXN_ERROR /* Commit failed due to a hard error. */
190 };
191
192 const char *ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status);
193
194 struct ovsdb_idl_txn *ovsdb_idl_txn_create(struct ovsdb_idl *);
195 void ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *, const char *, ...)
196 OVS_PRINTF_FORMAT (2, 3);
197 void ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *);
198 void ovsdb_idl_txn_increment(struct ovsdb_idl_txn *,
199 const struct ovsdb_idl_row *,
200 const struct ovsdb_idl_column *);
201 void ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *);
202 void ovsdb_idl_txn_wait(const struct ovsdb_idl_txn *);
203 enum ovsdb_idl_txn_status ovsdb_idl_txn_commit(struct ovsdb_idl_txn *);
204 enum ovsdb_idl_txn_status ovsdb_idl_txn_commit_block(struct ovsdb_idl_txn *);
205 void ovsdb_idl_txn_abort(struct ovsdb_idl_txn *);
206
207 const char *ovsdb_idl_txn_get_error(const struct ovsdb_idl_txn *);
208
209 int64_t ovsdb_idl_txn_get_increment_new_value(const struct ovsdb_idl_txn *);
210 const struct uuid *ovsdb_idl_txn_get_insert_uuid(const struct ovsdb_idl_txn *,
211 const struct uuid *);
212
213 void ovsdb_idl_txn_write(const struct ovsdb_idl_row *,
214 const struct ovsdb_idl_column *,
215 struct ovsdb_datum *);
216 void ovsdb_idl_txn_write_clone(const struct ovsdb_idl_row *,
217 const struct ovsdb_idl_column *,
218 const struct ovsdb_datum *);
219 void ovsdb_idl_txn_delete(const struct ovsdb_idl_row *);
220 const struct ovsdb_idl_row *ovsdb_idl_txn_insert(
221 struct ovsdb_idl_txn *, const struct ovsdb_idl_table_class *,
222 const struct uuid *);
223
224 struct ovsdb_idl *ovsdb_idl_txn_get_idl (struct ovsdb_idl_txn *);
225 \f
226
227 /* ovsdb_idl_loop provides an easy way to manage the transactions related
228 * to 'idl' and to cope with different status during transaction. */
229 struct ovsdb_idl_loop {
230 struct ovsdb_idl *idl;
231 unsigned int skip_seqno;
232
233 struct ovsdb_idl_txn *committing_txn;
234 unsigned int precommit_seqno;
235
236 struct ovsdb_idl_txn *open_txn;
237 };
238
239 #define OVSDB_IDL_LOOP_INITIALIZER(IDL) { .idl = (IDL) }
240
241 void ovsdb_idl_loop_destroy(struct ovsdb_idl_loop *);
242 struct ovsdb_idl_txn *ovsdb_idl_loop_run(struct ovsdb_idl_loop *);
243 void ovsdb_idl_loop_commit_and_wait(struct ovsdb_idl_loop *);
244
245 #endif /* ovsdb-idl.h */