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