1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "ovsdb-idl.h"
27 #include "dynamic-string.h"
28 #include "fatal-signal.h"
31 #include "ovsdb/ovsdb.h"
32 #include "ovsdb/table.h"
33 #include "ovsdb-data.h"
34 #include "ovsdb-error.h"
35 #include "ovsdb-idl-provider.h"
36 #include "ovsdb-parser.h"
37 #include "poll-loop.h"
41 #include "openvswitch/vlog.h"
43 VLOG_DEFINE_THIS_MODULE(ovsdb_idl);
45 COVERAGE_DEFINE(txn_uncommitted);
46 COVERAGE_DEFINE(txn_unchanged);
47 COVERAGE_DEFINE(txn_incomplete);
48 COVERAGE_DEFINE(txn_aborted);
49 COVERAGE_DEFINE(txn_success);
50 COVERAGE_DEFINE(txn_try_again);
51 COVERAGE_DEFINE(txn_not_locked);
52 COVERAGE_DEFINE(txn_error);
54 /* An arc from one idl_row to another. When row A contains a UUID that
55 * references row B, this is represented by an arc from A (the source) to B
58 * Arcs from a row to itself are omitted, that is, src and dst are always
61 * Arcs are never duplicated, that is, even if there are multiple references
62 * from A to B, there is only a single arc from A to B.
64 * Arcs are directed: an arc from A to B is the converse of an an arc from B to
65 * A. Both an arc and its converse may both be present, if each row refers
66 * to the other circularly.
68 * The source and destination row may be in the same table or in different
71 struct ovsdb_idl_arc {
72 struct ovs_list src_node; /* In src->src_arcs list. */
73 struct ovs_list dst_node; /* In dst->dst_arcs list. */
74 struct ovsdb_idl_row *src; /* Source row. */
75 struct ovsdb_idl_row *dst; /* Destination row. */
78 enum ovsdb_idl_state {
79 IDL_S_SCHEMA_REQUESTED,
80 IDL_S_MONITOR_REQUESTED,
85 const struct ovsdb_idl_class *class;
86 struct jsonrpc_session *session;
87 struct shash table_by_name;
88 struct ovsdb_idl_table *tables; /* Contains "struct ovsdb_idl_table *"s.*/
89 unsigned int change_seqno;
90 bool verify_write_only;
93 unsigned int state_seqno;
94 enum ovsdb_idl_state state;
95 struct json *request_id;
97 /* Database locking. */
98 char *lock_name; /* Name of lock we need, NULL if none. */
99 bool has_lock; /* Has db server told us we have the lock? */
100 bool is_lock_contended; /* Has db server told us we can't get lock? */
101 struct json *lock_request_id; /* JSON-RPC ID of in-flight lock request. */
103 /* Transaction support. */
104 struct ovsdb_idl_txn *txn;
105 struct hmap outstanding_txns;
108 struct ovsdb_idl_txn {
109 struct hmap_node hmap_node;
110 struct json *request_id;
111 struct ovsdb_idl *idl;
112 struct hmap txn_rows;
113 enum ovsdb_idl_txn_status status;
119 const char *inc_table;
120 const char *inc_column;
122 unsigned int inc_index;
123 int64_t inc_new_value;
126 struct hmap inserted_rows; /* Contains "struct ovsdb_idl_txn_insert"s. */
129 struct ovsdb_idl_txn_insert {
130 struct hmap_node hmap_node; /* In struct ovsdb_idl_txn's inserted_rows. */
131 struct uuid dummy; /* Dummy UUID used locally. */
132 int op_index; /* Index into transaction's operation array. */
133 struct uuid real; /* Real UUID used by database server. */
136 static struct vlog_rate_limit syntax_rl = VLOG_RATE_LIMIT_INIT(1, 5);
137 static struct vlog_rate_limit semantic_rl = VLOG_RATE_LIMIT_INIT(1, 5);
139 static void ovsdb_idl_clear(struct ovsdb_idl *);
140 static void ovsdb_idl_send_schema_request(struct ovsdb_idl *);
141 static void ovsdb_idl_send_monitor_request(struct ovsdb_idl *,
142 const struct json *schema_json);
143 static void ovsdb_idl_parse_update(struct ovsdb_idl *, const struct json *);
144 static struct ovsdb_error *ovsdb_idl_parse_update__(struct ovsdb_idl *,
145 const struct json *);
146 static bool ovsdb_idl_process_update(struct ovsdb_idl_table *,
148 const struct json *old,
149 const struct json *new);
150 static void ovsdb_idl_insert_row(struct ovsdb_idl_row *, const struct json *);
151 static void ovsdb_idl_delete_row(struct ovsdb_idl_row *);
152 static bool ovsdb_idl_modify_row(struct ovsdb_idl_row *, const struct json *);
154 static bool ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *);
155 static struct ovsdb_idl_row *ovsdb_idl_row_create__(
156 const struct ovsdb_idl_table_class *);
157 static struct ovsdb_idl_row *ovsdb_idl_row_create(struct ovsdb_idl_table *,
158 const struct uuid *);
159 static void ovsdb_idl_row_destroy(struct ovsdb_idl_row *);
160 static void ovsdb_idl_row_destroy_postprocess(struct ovsdb_idl *);
162 static void ovsdb_idl_row_parse(struct ovsdb_idl_row *);
163 static void ovsdb_idl_row_unparse(struct ovsdb_idl_row *);
164 static void ovsdb_idl_row_clear_old(struct ovsdb_idl_row *);
165 static void ovsdb_idl_row_clear_new(struct ovsdb_idl_row *);
166 static void ovsdb_idl_row_clear_arcs(struct ovsdb_idl_row *, bool destroy_dsts);
168 static void ovsdb_idl_txn_abort_all(struct ovsdb_idl *);
169 static bool ovsdb_idl_txn_process_reply(struct ovsdb_idl *,
170 const struct jsonrpc_msg *msg);
172 static void ovsdb_idl_send_lock_request(struct ovsdb_idl *);
173 static void ovsdb_idl_send_unlock_request(struct ovsdb_idl *);
174 static void ovsdb_idl_parse_lock_reply(struct ovsdb_idl *,
175 const struct json *);
176 static void ovsdb_idl_parse_lock_notify(struct ovsdb_idl *,
177 const struct json *params,
179 static struct ovsdb_idl_table *
180 ovsdb_idl_table_from_class(const struct ovsdb_idl *,
181 const struct ovsdb_idl_table_class *);
182 static bool ovsdb_idl_track_is_set(struct ovsdb_idl_table *table);
184 /* Creates and returns a connection to database 'remote', which should be in a
185 * form acceptable to jsonrpc_session_open(). The connection will maintain an
186 * in-memory replica of the remote database whose schema is described by
187 * 'class'. (Ordinarily 'class' is compiled from an OVSDB schema automatically
190 * Passes 'retry' to jsonrpc_session_open(). See that function for
193 * If 'monitor_everything_by_default' is true, then everything in the remote
194 * database will be replicated by default. ovsdb_idl_omit() and
195 * ovsdb_idl_omit_alert() may be used to selectively drop some columns from
198 * If 'monitor_everything_by_default' is false, then no columns or tables will
199 * be replicated by default. ovsdb_idl_add_column() and ovsdb_idl_add_table()
200 * must be used to choose some columns or tables to replicate.
203 ovsdb_idl_create(const char *remote, const struct ovsdb_idl_class *class,
204 bool monitor_everything_by_default, bool retry)
206 struct ovsdb_idl *idl;
207 uint8_t default_mode;
210 default_mode = (monitor_everything_by_default
211 ? OVSDB_IDL_MONITOR | OVSDB_IDL_ALERT
214 idl = xzalloc(sizeof *idl);
216 idl->session = jsonrpc_session_open(remote, retry);
217 shash_init(&idl->table_by_name);
218 idl->tables = xmalloc(class->n_tables * sizeof *idl->tables);
219 for (i = 0; i < class->n_tables; i++) {
220 const struct ovsdb_idl_table_class *tc = &class->tables[i];
221 struct ovsdb_idl_table *table = &idl->tables[i];
224 shash_add_assert(&idl->table_by_name, tc->name, table);
226 table->modes = xmalloc(tc->n_columns);
227 memset(table->modes, default_mode, tc->n_columns);
228 table->need_table = false;
229 shash_init(&table->columns);
230 for (j = 0; j < tc->n_columns; j++) {
231 const struct ovsdb_idl_column *column = &tc->columns[j];
233 shash_add_assert(&table->columns, column->name, column);
235 hmap_init(&table->rows);
236 list_init(&table->track_list);
237 table->change_seqno[OVSDB_IDL_CHANGE_INSERT]
238 = table->change_seqno[OVSDB_IDL_CHANGE_MODIFY]
239 = table->change_seqno[OVSDB_IDL_CHANGE_DELETE] = 0;
243 idl->state_seqno = UINT_MAX;
244 idl->request_id = NULL;
246 hmap_init(&idl->outstanding_txns);
251 /* Destroys 'idl' and all of the data structures that it manages. */
253 ovsdb_idl_destroy(struct ovsdb_idl *idl)
258 ovs_assert(!idl->txn);
259 ovsdb_idl_clear(idl);
260 jsonrpc_session_close(idl->session);
262 for (i = 0; i < idl->class->n_tables; i++) {
263 struct ovsdb_idl_table *table = &idl->tables[i];
264 shash_destroy(&table->columns);
265 hmap_destroy(&table->rows);
268 shash_destroy(&idl->table_by_name);
270 json_destroy(idl->request_id);
271 free(idl->lock_name);
272 json_destroy(idl->lock_request_id);
273 hmap_destroy(&idl->outstanding_txns);
279 ovsdb_idl_clear(struct ovsdb_idl *idl)
281 bool changed = false;
284 for (i = 0; i < idl->class->n_tables; i++) {
285 struct ovsdb_idl_table *table = &idl->tables[i];
286 struct ovsdb_idl_row *row, *next_row;
288 if (hmap_is_empty(&table->rows)) {
293 HMAP_FOR_EACH_SAFE (row, next_row, hmap_node, &table->rows) {
294 struct ovsdb_idl_arc *arc, *next_arc;
296 if (!ovsdb_idl_row_is_orphan(row)) {
297 ovsdb_idl_row_unparse(row);
299 LIST_FOR_EACH_SAFE (arc, next_arc, src_node, &row->src_arcs) {
302 /* No need to do anything with dst_arcs: some node has those arcs
303 * as forward arcs and will destroy them itself. */
305 if (!list_is_empty(&row->track_node)) {
306 list_remove(&row->track_node);
309 ovsdb_idl_row_destroy(row);
313 ovsdb_idl_track_clear(idl);
320 /* Processes a batch of messages from the database server on 'idl'. This may
321 * cause the IDL's contents to change. The client may check for that with
322 * ovsdb_idl_get_seqno(). */
324 ovsdb_idl_run(struct ovsdb_idl *idl)
328 ovs_assert(!idl->txn);
329 jsonrpc_session_run(idl->session);
330 for (i = 0; jsonrpc_session_is_connected(idl->session) && i < 50; i++) {
331 struct jsonrpc_msg *msg;
334 seqno = jsonrpc_session_get_seqno(idl->session);
335 if (idl->state_seqno != seqno) {
336 idl->state_seqno = seqno;
337 json_destroy(idl->request_id);
338 idl->request_id = NULL;
339 ovsdb_idl_txn_abort_all(idl);
341 ovsdb_idl_send_schema_request(idl);
342 idl->state = IDL_S_SCHEMA_REQUESTED;
343 if (idl->lock_name) {
344 ovsdb_idl_send_lock_request(idl);
348 msg = jsonrpc_session_recv(idl->session);
353 if (msg->type == JSONRPC_NOTIFY
354 && !strcmp(msg->method, "update")
355 && msg->params->type == JSON_ARRAY
356 && msg->params->u.array.n == 2
357 && msg->params->u.array.elems[0]->type == JSON_NULL) {
358 /* Database contents changed. */
359 ovsdb_idl_parse_update(idl, msg->params->u.array.elems[1]);
360 } else if (msg->type == JSONRPC_REPLY
362 && json_equal(idl->request_id, msg->id)) {
363 switch (idl->state) {
364 case IDL_S_SCHEMA_REQUESTED:
365 /* Reply to our "get_schema" request. */
366 json_destroy(idl->request_id);
367 idl->request_id = NULL;
368 ovsdb_idl_send_monitor_request(idl, msg->result);
369 idl->state = IDL_S_MONITOR_REQUESTED;
372 case IDL_S_MONITOR_REQUESTED:
373 /* Reply to our "monitor" request. */
375 json_destroy(idl->request_id);
376 idl->request_id = NULL;
377 idl->state = IDL_S_MONITORING;
378 ovsdb_idl_clear(idl);
379 ovsdb_idl_parse_update(idl, msg->result);
382 case IDL_S_MONITORING:
386 } else if (msg->type == JSONRPC_REPLY
387 && idl->lock_request_id
388 && json_equal(idl->lock_request_id, msg->id)) {
389 /* Reply to our "lock" request. */
390 ovsdb_idl_parse_lock_reply(idl, msg->result);
391 } else if (msg->type == JSONRPC_NOTIFY
392 && !strcmp(msg->method, "locked")) {
393 /* We got our lock. */
394 ovsdb_idl_parse_lock_notify(idl, msg->params, true);
395 } else if (msg->type == JSONRPC_NOTIFY
396 && !strcmp(msg->method, "stolen")) {
397 /* Someone else stole our lock. */
398 ovsdb_idl_parse_lock_notify(idl, msg->params, false);
399 } else if ((msg->type == JSONRPC_ERROR
400 || msg->type == JSONRPC_REPLY)
401 && ovsdb_idl_txn_process_reply(idl, msg)) {
402 /* ovsdb_idl_txn_process_reply() did everything needful. */
404 /* This can happen if ovsdb_idl_txn_destroy() is called to destroy
405 * a transaction before we receive the reply, so keep the log level
407 VLOG_DBG("%s: received unexpected %s message",
408 jsonrpc_session_get_name(idl->session),
409 jsonrpc_msg_type_to_string(msg->type));
411 jsonrpc_msg_destroy(msg);
413 ovsdb_idl_row_destroy_postprocess(idl);
416 /* Arranges for poll_block() to wake up when ovsdb_idl_run() has something to
417 * do or when activity occurs on a transaction on 'idl'. */
419 ovsdb_idl_wait(struct ovsdb_idl *idl)
421 jsonrpc_session_wait(idl->session);
422 jsonrpc_session_recv_wait(idl->session);
425 /* Returns a "sequence number" that represents the state of 'idl'. When
426 * ovsdb_idl_run() changes the database, the sequence number changes. The
427 * initial fetch of the entire contents of the remote database is considered to
428 * be one kind of change. Successfully acquiring a lock, if one has been
429 * configured with ovsdb_idl_set_lock(), is also considered to be a change.
431 * As long as the sequence number does not change, the client may continue to
432 * use any data structures it obtains from 'idl'. But when it changes, the
433 * client must not access any of these data structures again, because they
434 * could have freed or reused for other purposes.
436 * The sequence number can occasionally change even if the database does not.
437 * This happens if the connection to the database drops and reconnects, which
438 * causes the database contents to be reloaded even if they didn't change. (It
439 * could also happen if the database server sends out a "change" that reflects
440 * what the IDL already thought was in the database. The database server is
441 * not supposed to do that, but bugs could in theory cause it to do so.) */
443 ovsdb_idl_get_seqno(const struct ovsdb_idl *idl)
445 return idl->change_seqno;
448 /* Returns true if 'idl' successfully connected to the remote database and
449 * retrieved its contents (even if the connection subsequently dropped and is
450 * in the process of reconnecting). If so, then 'idl' contains an atomic
451 * snapshot of the database's contents (but it might be arbitrarily old if the
452 * connection dropped).
454 * Returns false if 'idl' has never connected or retrieved the database's
455 * contents. If so, 'idl' is empty. */
457 ovsdb_idl_has_ever_connected(const struct ovsdb_idl *idl)
459 return ovsdb_idl_get_seqno(idl) != 0;
462 /* Reconfigures 'idl' so that it would reconnect to the database, if
463 * connection was dropped. */
465 ovsdb_idl_enable_reconnect(struct ovsdb_idl *idl)
467 jsonrpc_session_enable_reconnect(idl->session);
470 /* Forces 'idl' to drop its connection to the database and reconnect. In the
471 * meantime, the contents of 'idl' will not change. */
473 ovsdb_idl_force_reconnect(struct ovsdb_idl *idl)
475 jsonrpc_session_force_reconnect(idl->session);
478 /* Some IDL users should only write to write-only columns. Furthermore,
479 * writing to a column which is not write-only can cause serious performance
480 * degradations for these users. This function causes 'idl' to reject writes
481 * to columns which are not marked write only using ovsdb_idl_omit_alert(). */
483 ovsdb_idl_verify_write_only(struct ovsdb_idl *idl)
485 idl->verify_write_only = true;
488 /* Returns true if 'idl' is currently connected or trying to connect. */
490 ovsdb_idl_is_alive(const struct ovsdb_idl *idl)
492 return jsonrpc_session_is_alive(idl->session);
495 /* Returns the last error reported on a connection by 'idl'. The return value
496 * is 0 only if no connection made by 'idl' has ever encountered an error. See
497 * jsonrpc_get_status() for return value interpretation. */
499 ovsdb_idl_get_last_error(const struct ovsdb_idl *idl)
501 return jsonrpc_session_get_last_error(idl->session);
504 static unsigned char *
505 ovsdb_idl_get_mode(struct ovsdb_idl *idl,
506 const struct ovsdb_idl_column *column)
510 ovs_assert(!idl->change_seqno);
512 for (i = 0; i < idl->class->n_tables; i++) {
513 const struct ovsdb_idl_table *table = &idl->tables[i];
514 const struct ovsdb_idl_table_class *tc = table->class;
516 if (column >= tc->columns && column < &tc->columns[tc->n_columns]) {
517 return &table->modes[column - tc->columns];
525 add_ref_table(struct ovsdb_idl *idl, const struct ovsdb_base_type *base)
527 if (base->type == OVSDB_TYPE_UUID && base->u.uuid.refTableName) {
528 struct ovsdb_idl_table *table;
530 table = shash_find_data(&idl->table_by_name,
531 base->u.uuid.refTableName);
533 table->need_table = true;
535 VLOG_WARN("%s IDL class missing referenced table %s",
536 idl->class->database, base->u.uuid.refTableName);
541 /* Turns on OVSDB_IDL_MONITOR and OVSDB_IDL_ALERT for 'column' in 'idl'. Also
542 * ensures that any tables referenced by 'column' will be replicated, even if
543 * no columns in that table are selected for replication (see
544 * ovsdb_idl_add_table() for more information).
546 * This function is only useful if 'monitor_everything_by_default' was false in
547 * the call to ovsdb_idl_create(). This function should be called between
548 * ovsdb_idl_create() and the first call to ovsdb_idl_run().
551 ovsdb_idl_add_column(struct ovsdb_idl *idl,
552 const struct ovsdb_idl_column *column)
554 *ovsdb_idl_get_mode(idl, column) = OVSDB_IDL_MONITOR | OVSDB_IDL_ALERT;
555 add_ref_table(idl, &column->type.key);
556 add_ref_table(idl, &column->type.value);
559 /* Ensures that the table with class 'tc' will be replicated on 'idl' even if
560 * no columns are selected for replication. This can be useful because it
561 * allows 'idl' to keep track of what rows in the table actually exist, which
562 * in turn allows columns that reference the table to have accurate contents.
563 * (The IDL presents the database with references to rows that do not exist
566 * This function is only useful if 'monitor_everything_by_default' was false in
567 * the call to ovsdb_idl_create(). This function should be called between
568 * ovsdb_idl_create() and the first call to ovsdb_idl_run().
571 ovsdb_idl_add_table(struct ovsdb_idl *idl,
572 const struct ovsdb_idl_table_class *tc)
576 for (i = 0; i < idl->class->n_tables; i++) {
577 struct ovsdb_idl_table *table = &idl->tables[i];
579 if (table->class == tc) {
580 table->need_table = true;
588 /* Turns off OVSDB_IDL_ALERT for 'column' in 'idl'.
590 * This function should be called between ovsdb_idl_create() and the first call
591 * to ovsdb_idl_run().
594 ovsdb_idl_omit_alert(struct ovsdb_idl *idl,
595 const struct ovsdb_idl_column *column)
597 *ovsdb_idl_get_mode(idl, column) &= ~OVSDB_IDL_ALERT;
600 /* Sets the mode for 'column' in 'idl' to 0. See the big comment above
601 * OVSDB_IDL_MONITOR for details.
603 * This function should be called between ovsdb_idl_create() and the first call
604 * to ovsdb_idl_run().
607 ovsdb_idl_omit(struct ovsdb_idl *idl, const struct ovsdb_idl_column *column)
609 *ovsdb_idl_get_mode(idl, column) = 0;
612 /* Returns the most recent IDL change sequence number that caused a
613 * insert, modify or delete update to the table with class 'table_class'.
616 ovsdb_idl_table_get_seqno(const struct ovsdb_idl *idl,
617 const struct ovsdb_idl_table_class *table_class)
619 struct ovsdb_idl_table *table
620 = ovsdb_idl_table_from_class(idl, table_class);
621 unsigned int max_seqno = table->change_seqno[OVSDB_IDL_CHANGE_INSERT];
623 if (max_seqno < table->change_seqno[OVSDB_IDL_CHANGE_MODIFY]) {
624 max_seqno = table->change_seqno[OVSDB_IDL_CHANGE_MODIFY];
626 if (max_seqno < table->change_seqno[OVSDB_IDL_CHANGE_DELETE]) {
627 max_seqno = table->change_seqno[OVSDB_IDL_CHANGE_DELETE];
632 /* For each row that contains tracked columns, IDL stores the most
633 * recent IDL change sequence numbers associateed with insert, modify
634 * and delete updates to the table.
637 ovsdb_idl_row_get_seqno(const struct ovsdb_idl_row *row,
638 enum ovsdb_idl_change change)
640 return row->change_seqno[change];
643 /* Turns on OVSDB_IDL_TRACK for 'column' in 'idl', ensuring that
644 * all rows whose 'column' is modified are traced. Similarly, insert
645 * or delete of rows having 'column' are tracked. Clients are able
646 * to retrive the tracked rows with the ovsdb_idl_track_get_*()
649 * This function should be called between ovsdb_idl_create() and
650 * the first call to ovsdb_idl_run(). The column to be tracked
651 * should have OVSDB_IDL_ALERT turned on.
654 ovsdb_idl_track_add_column(struct ovsdb_idl *idl,
655 const struct ovsdb_idl_column *column)
657 if (!(*ovsdb_idl_get_mode(idl, column) & OVSDB_IDL_ALERT)) {
658 ovsdb_idl_add_column(idl, column);
660 *ovsdb_idl_get_mode(idl, column) |= OVSDB_IDL_TRACK;
664 ovsdb_idl_track_add_all(struct ovsdb_idl *idl)
668 for (i = 0; i < idl->class->n_tables; i++) {
669 const struct ovsdb_idl_table_class *tc = &idl->class->tables[i];
671 for (j = 0; j < tc->n_columns; j++) {
672 const struct ovsdb_idl_column *column = &tc->columns[j];
673 ovsdb_idl_track_add_column(idl, column);
678 /* Returns true if 'table' has any tracked column. */
680 ovsdb_idl_track_is_set(struct ovsdb_idl_table *table)
684 for (i = 0; i < table->class->n_columns; i++) {
685 if (table->modes[i] & OVSDB_IDL_TRACK) {
692 /* Returns the first tracked row in table with class 'table_class'
693 * for the specified 'idl'. Returns NULL if there are no tracked rows */
694 const struct ovsdb_idl_row *
695 ovsdb_idl_track_get_first(const struct ovsdb_idl *idl,
696 const struct ovsdb_idl_table_class *table_class)
698 struct ovsdb_idl_table *table
699 = ovsdb_idl_table_from_class(idl, table_class);
701 if (!list_is_empty(&table->track_list)) {
702 return CONTAINER_OF(list_front(&table->track_list), struct ovsdb_idl_row, track_node);
707 /* Returns the next tracked row in table after the specified 'row'
708 * (in no particular order). Returns NULL if there are no tracked rows */
709 const struct ovsdb_idl_row *
710 ovsdb_idl_track_get_next(const struct ovsdb_idl_row *row)
712 if (row->track_node.next != &row->table->track_list) {
713 return CONTAINER_OF(row->track_node.next, struct ovsdb_idl_row, track_node);
719 /* Flushes the tracked rows. Client calls this function after calling
720 * ovsdb_idl_run() and read all tracked rows with the ovsdb_idl_track_get_*()
721 * functions. This is usually done at the end of the client's processing
722 * loop when it is ready to do ovsdb_idl_run() again.
725 ovsdb_idl_track_clear(const struct ovsdb_idl *idl)
729 for (i = 0; i < idl->class->n_tables; i++) {
730 struct ovsdb_idl_table *table = &idl->tables[i];
732 if (!list_is_empty(&table->track_list)) {
733 struct ovsdb_idl_row *row, *next;
735 LIST_FOR_EACH_SAFE(row, next, track_node, &table->track_list) {
736 list_remove(&row->track_node);
737 list_init(&row->track_node);
738 if (ovsdb_idl_row_is_orphan(row)) {
739 ovsdb_idl_row_clear_old(row);
749 ovsdb_idl_send_schema_request(struct ovsdb_idl *idl)
751 struct jsonrpc_msg *msg;
753 json_destroy(idl->request_id);
754 msg = jsonrpc_create_request(
756 json_array_create_1(json_string_create(idl->class->database)),
758 jsonrpc_session_send(idl->session, msg);
762 log_error(struct ovsdb_error *error)
764 char *s = ovsdb_error_to_string(error);
765 VLOG_WARN("error parsing database schema: %s", s);
767 ovsdb_error_destroy(error);
770 /* Frees 'schema', which is in the format returned by parse_schema(). */
772 free_schema(struct shash *schema)
775 struct shash_node *node, *next;
777 SHASH_FOR_EACH_SAFE (node, next, schema) {
778 struct sset *sset = node->data;
781 shash_delete(schema, node);
783 shash_destroy(schema);
788 /* Parses 'schema_json', an OVSDB schema in JSON format as described in RFC
789 * 7047, to obtain the names of its rows and columns. If successful, returns
790 * an shash whose keys are table names and whose values are ssets, where each
791 * sset contains the names of its table's columns. On failure (due to a parse
792 * error), returns NULL.
794 * It would also be possible to use the general-purpose OVSDB schema parser in
795 * ovsdb-server, but that's overkill, possibly too strict for the current use
796 * case, and would require restructuring ovsdb-server to separate the schema
797 * code from the rest. */
798 static struct shash *
799 parse_schema(const struct json *schema_json)
801 struct ovsdb_parser parser;
802 const struct json *tables_json;
803 struct ovsdb_error *error;
804 struct shash_node *node;
805 struct shash *schema;
807 ovsdb_parser_init(&parser, schema_json, "database schema");
808 tables_json = ovsdb_parser_member(&parser, "tables", OP_OBJECT);
809 error = ovsdb_parser_destroy(&parser);
815 schema = xmalloc(sizeof *schema);
817 SHASH_FOR_EACH (node, json_object(tables_json)) {
818 const char *table_name = node->name;
819 const struct json *json = node->data;
820 const struct json *columns_json;
822 ovsdb_parser_init(&parser, json, "table schema for table %s",
824 columns_json = ovsdb_parser_member(&parser, "columns", OP_OBJECT);
825 error = ovsdb_parser_destroy(&parser);
832 struct sset *columns = xmalloc(sizeof *columns);
835 struct shash_node *node2;
836 SHASH_FOR_EACH (node2, json_object(columns_json)) {
837 const char *column_name = node2->name;
838 sset_add(columns, column_name);
840 shash_add(schema, table_name, columns);
846 ovsdb_idl_send_monitor_request(struct ovsdb_idl *idl,
847 const struct json *schema_json)
849 struct shash *schema = parse_schema(schema_json);
850 struct json *monitor_requests;
851 struct jsonrpc_msg *msg;
854 monitor_requests = json_object_create();
855 for (i = 0; i < idl->class->n_tables; i++) {
856 const struct ovsdb_idl_table *table = &idl->tables[i];
857 const struct ovsdb_idl_table_class *tc = table->class;
858 struct json *monitor_request, *columns;
859 const struct sset *table_schema;
862 table_schema = (schema
863 ? shash_find_data(schema, table->class->name)
866 columns = table->need_table ? json_array_create_empty() : NULL;
867 for (j = 0; j < tc->n_columns; j++) {
868 const struct ovsdb_idl_column *column = &tc->columns[j];
869 if (table->modes[j] & OVSDB_IDL_MONITOR) {
871 && !sset_contains(table_schema, column->name)) {
872 VLOG_WARN("%s table in %s database lacks %s column "
873 "(database needs upgrade?)",
874 table->class->name, idl->class->database,
879 columns = json_array_create_empty();
881 json_array_add(columns, json_string_create(column->name));
886 if (schema && !table_schema) {
887 VLOG_WARN("%s database lacks %s table "
888 "(database needs upgrade?)",
889 idl->class->database, table->class->name);
890 json_destroy(columns);
894 monitor_request = json_object_create();
895 json_object_put(monitor_request, "columns", columns);
896 json_object_put(monitor_requests, tc->name, monitor_request);
901 json_destroy(idl->request_id);
902 msg = jsonrpc_create_request(
904 json_array_create_3(json_string_create(idl->class->database),
905 json_null_create(), monitor_requests),
907 jsonrpc_session_send(idl->session, msg);
911 ovsdb_idl_parse_update(struct ovsdb_idl *idl, const struct json *table_updates)
913 struct ovsdb_error *error = ovsdb_idl_parse_update__(idl, table_updates);
915 if (!VLOG_DROP_WARN(&syntax_rl)) {
916 char *s = ovsdb_error_to_string(error);
917 VLOG_WARN_RL(&syntax_rl, "%s", s);
920 ovsdb_error_destroy(error);
924 static struct ovsdb_error *
925 ovsdb_idl_parse_update__(struct ovsdb_idl *idl,
926 const struct json *table_updates)
928 const struct shash_node *tables_node;
930 if (table_updates->type != JSON_OBJECT) {
931 return ovsdb_syntax_error(table_updates, NULL,
932 "<table-updates> is not an object");
934 SHASH_FOR_EACH (tables_node, json_object(table_updates)) {
935 const struct json *table_update = tables_node->data;
936 const struct shash_node *table_node;
937 struct ovsdb_idl_table *table;
939 table = shash_find_data(&idl->table_by_name, tables_node->name);
941 return ovsdb_syntax_error(
943 "<table-updates> includes unknown table \"%s\"",
947 if (table_update->type != JSON_OBJECT) {
948 return ovsdb_syntax_error(table_update, NULL,
949 "<table-update> for table \"%s\" is "
950 "not an object", table->class->name);
952 SHASH_FOR_EACH (table_node, json_object(table_update)) {
953 const struct json *row_update = table_node->data;
954 const struct json *old_json, *new_json;
957 if (!uuid_from_string(&uuid, table_node->name)) {
958 return ovsdb_syntax_error(table_update, NULL,
959 "<table-update> for table \"%s\" "
961 "\"%s\" as member name",
965 if (row_update->type != JSON_OBJECT) {
966 return ovsdb_syntax_error(row_update, NULL,
967 "<table-update> for table \"%s\" "
968 "contains <row-update> for %s that "
974 old_json = shash_find_data(json_object(row_update), "old");
975 new_json = shash_find_data(json_object(row_update), "new");
976 if (old_json && old_json->type != JSON_OBJECT) {
977 return ovsdb_syntax_error(old_json, NULL,
978 "\"old\" <row> is not object");
979 } else if (new_json && new_json->type != JSON_OBJECT) {
980 return ovsdb_syntax_error(new_json, NULL,
981 "\"new\" <row> is not object");
982 } else if ((old_json != NULL) + (new_json != NULL)
983 != shash_count(json_object(row_update))) {
984 return ovsdb_syntax_error(row_update, NULL,
985 "<row-update> contains unexpected "
987 } else if (!old_json && !new_json) {
988 return ovsdb_syntax_error(row_update, NULL,
989 "<row-update> missing \"old\" "
990 "and \"new\" members");
993 if (ovsdb_idl_process_update(table, &uuid, old_json, new_json)) {
1002 static struct ovsdb_idl_row *
1003 ovsdb_idl_get_row(struct ovsdb_idl_table *table, const struct uuid *uuid)
1005 struct ovsdb_idl_row *row;
1007 HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid), &table->rows) {
1008 if (uuid_equals(&row->uuid, uuid)) {
1015 /* Returns true if a column with mode OVSDB_IDL_MODE_RW changed, false
1018 ovsdb_idl_process_update(struct ovsdb_idl_table *table,
1019 const struct uuid *uuid, const struct json *old,
1020 const struct json *new)
1022 struct ovsdb_idl_row *row;
1024 row = ovsdb_idl_get_row(table, uuid);
1027 if (row && !ovsdb_idl_row_is_orphan(row)) {
1028 /* XXX perhaps we should check the 'old' values? */
1029 ovsdb_idl_delete_row(row);
1031 VLOG_WARN_RL(&semantic_rl, "cannot delete missing row "UUID_FMT" "
1033 UUID_ARGS(uuid), table->class->name);
1039 ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
1040 } else if (ovsdb_idl_row_is_orphan(row)) {
1041 ovsdb_idl_insert_row(row, new);
1043 VLOG_WARN_RL(&semantic_rl, "cannot add existing row "UUID_FMT" to "
1044 "table %s", UUID_ARGS(uuid), table->class->name);
1045 return ovsdb_idl_modify_row(row, new);
1050 /* XXX perhaps we should check the 'old' values? */
1051 if (!ovsdb_idl_row_is_orphan(row)) {
1052 return ovsdb_idl_modify_row(row, new);
1054 VLOG_WARN_RL(&semantic_rl, "cannot modify missing but "
1055 "referenced row "UUID_FMT" in table %s",
1056 UUID_ARGS(uuid), table->class->name);
1057 ovsdb_idl_insert_row(row, new);
1060 VLOG_WARN_RL(&semantic_rl, "cannot modify missing row "UUID_FMT" "
1061 "in table %s", UUID_ARGS(uuid), table->class->name);
1062 ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
1069 /* Returns true if a column with mode OVSDB_IDL_MODE_RW changed, false
1072 ovsdb_idl_row_update(struct ovsdb_idl_row *row, const struct json *row_json,
1073 enum ovsdb_idl_change change)
1075 struct ovsdb_idl_table *table = row->table;
1076 struct shash_node *node;
1077 bool changed = false;
1079 SHASH_FOR_EACH (node, json_object(row_json)) {
1080 const char *column_name = node->name;
1081 const struct ovsdb_idl_column *column;
1082 struct ovsdb_datum datum;
1083 struct ovsdb_error *error;
1085 column = shash_find_data(&table->columns, column_name);
1087 VLOG_WARN_RL(&syntax_rl, "unknown column %s updating row "UUID_FMT,
1088 column_name, UUID_ARGS(&row->uuid));
1092 error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
1094 unsigned int column_idx = column - table->class->columns;
1095 struct ovsdb_datum *old = &row->old[column_idx];
1097 if (!ovsdb_datum_equals(old, &datum, &column->type)) {
1098 ovsdb_datum_swap(old, &datum);
1099 if (table->modes[column_idx] & OVSDB_IDL_ALERT) {
1101 row->change_seqno[change]
1102 = row->table->change_seqno[change]
1103 = row->table->idl->change_seqno + 1;
1104 if (table->modes[column_idx] & OVSDB_IDL_TRACK) {
1105 if (list_is_empty(&row->track_node)) {
1106 list_push_front(&row->table->track_list,
1112 /* Didn't really change but the OVSDB monitor protocol always
1113 * includes every value in a row. */
1116 ovsdb_datum_destroy(&datum, &column->type);
1118 char *s = ovsdb_error_to_string(error);
1119 VLOG_WARN_RL(&syntax_rl, "error parsing column %s in row "UUID_FMT
1120 " in table %s: %s", column_name,
1121 UUID_ARGS(&row->uuid), table->class->name, s);
1123 ovsdb_error_destroy(error);
1129 /* When a row A refers to row B through a column with a "refTable" constraint,
1130 * but row B does not exist, row B is called an "orphan row". Orphan rows
1131 * should not persist, because the database enforces referential integrity, but
1132 * they can appear transiently as changes from the database are received (the
1133 * database doesn't try to topologically sort them and circular references mean
1134 * it isn't always possible anyhow).
1136 * This function returns true if 'row' is an orphan row, otherwise false.
1139 ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *row)
1141 return !row->old && !row->new;
1144 /* Returns true if 'row' is conceptually part of the database as modified by
1145 * the current transaction (if any), false otherwise.
1147 * This function will return true if 'row' is not an orphan (see the comment on
1148 * ovsdb_idl_row_is_orphan()) and:
1150 * - 'row' exists in the database and has not been deleted within the
1151 * current transaction (if any).
1153 * - 'row' was inserted within the current transaction and has not been
1154 * deleted. (In the latter case you should not have passed 'row' in at
1155 * all, because ovsdb_idl_txn_delete() freed it.)
1157 * This function will return false if 'row' is an orphan or if 'row' was
1158 * deleted within the current transaction.
1161 ovsdb_idl_row_exists(const struct ovsdb_idl_row *row)
1163 return row->new != NULL;
1167 ovsdb_idl_row_parse(struct ovsdb_idl_row *row)
1169 const struct ovsdb_idl_table_class *class = row->table->class;
1172 for (i = 0; i < class->n_columns; i++) {
1173 const struct ovsdb_idl_column *c = &class->columns[i];
1174 (c->parse)(row, &row->old[i]);
1179 ovsdb_idl_row_unparse(struct ovsdb_idl_row *row)
1181 const struct ovsdb_idl_table_class *class = row->table->class;
1184 for (i = 0; i < class->n_columns; i++) {
1185 const struct ovsdb_idl_column *c = &class->columns[i];
1191 ovsdb_idl_row_clear_old(struct ovsdb_idl_row *row)
1193 ovs_assert(row->old == row->new);
1194 if (!ovsdb_idl_row_is_orphan(row)) {
1195 const struct ovsdb_idl_table_class *class = row->table->class;
1198 for (i = 0; i < class->n_columns; i++) {
1199 ovsdb_datum_destroy(&row->old[i], &class->columns[i].type);
1202 row->old = row->new = NULL;
1207 ovsdb_idl_row_clear_new(struct ovsdb_idl_row *row)
1209 if (row->old != row->new) {
1211 const struct ovsdb_idl_table_class *class = row->table->class;
1215 BITMAP_FOR_EACH_1 (i, class->n_columns, row->written) {
1216 ovsdb_datum_destroy(&row->new[i], &class->columns[i].type);
1221 row->written = NULL;
1223 row->new = row->old;
1228 ovsdb_idl_row_clear_arcs(struct ovsdb_idl_row *row, bool destroy_dsts)
1230 struct ovsdb_idl_arc *arc, *next;
1232 /* Delete all forward arcs. If 'destroy_dsts', destroy any orphaned rows
1233 * that this causes to be unreferenced, if tracking is not enabled.
1234 * If tracking is enabled, orphaned nodes are removed from hmap but not
1237 LIST_FOR_EACH_SAFE (arc, next, src_node, &row->src_arcs) {
1238 list_remove(&arc->dst_node);
1240 && ovsdb_idl_row_is_orphan(arc->dst)
1241 && list_is_empty(&arc->dst->dst_arcs)) {
1242 ovsdb_idl_row_destroy(arc->dst);
1246 list_init(&row->src_arcs);
1249 /* Force nodes that reference 'row' to reparse. */
1251 ovsdb_idl_row_reparse_backrefs(struct ovsdb_idl_row *row)
1253 struct ovsdb_idl_arc *arc, *next;
1255 /* This is trickier than it looks. ovsdb_idl_row_clear_arcs() will destroy
1256 * 'arc', so we need to use the "safe" variant of list traversal. However,
1257 * calling an ovsdb_idl_column's 'parse' function will add an arc
1258 * equivalent to 'arc' to row->arcs. That could be a problem for
1259 * traversal, but it adds it at the beginning of the list to prevent us
1260 * from stumbling upon it again.
1262 * (If duplicate arcs were possible then we would need to make sure that
1263 * 'next' didn't also point into 'arc''s destination, but we forbid
1264 * duplicate arcs.) */
1265 LIST_FOR_EACH_SAFE (arc, next, dst_node, &row->dst_arcs) {
1266 struct ovsdb_idl_row *ref = arc->src;
1268 ovsdb_idl_row_unparse(ref);
1269 ovsdb_idl_row_clear_arcs(ref, false);
1270 ovsdb_idl_row_parse(ref);
1274 static struct ovsdb_idl_row *
1275 ovsdb_idl_row_create__(const struct ovsdb_idl_table_class *class)
1277 struct ovsdb_idl_row *row = xzalloc(class->allocation_size);
1278 class->row_init(row);
1279 list_init(&row->src_arcs);
1280 list_init(&row->dst_arcs);
1281 hmap_node_nullify(&row->txn_node);
1282 list_init(&row->track_node);
1286 static struct ovsdb_idl_row *
1287 ovsdb_idl_row_create(struct ovsdb_idl_table *table, const struct uuid *uuid)
1289 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(table->class);
1290 hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
1297 ovsdb_idl_row_destroy(struct ovsdb_idl_row *row)
1300 ovsdb_idl_row_clear_old(row);
1301 hmap_remove(&row->table->rows, &row->hmap_node);
1302 if (ovsdb_idl_track_is_set(row->table)) {
1303 row->change_seqno[OVSDB_IDL_CHANGE_DELETE]
1304 = row->table->change_seqno[OVSDB_IDL_CHANGE_DELETE]
1305 = row->table->idl->change_seqno + 1;
1307 if (list_is_empty(&row->track_node)) {
1308 list_push_front(&row->table->track_list, &row->track_node);
1314 ovsdb_idl_row_destroy_postprocess(struct ovsdb_idl *idl)
1318 for (i = 0; i < idl->class->n_tables; i++) {
1319 struct ovsdb_idl_table *table = &idl->tables[i];
1321 if (!list_is_empty(&table->track_list)) {
1322 struct ovsdb_idl_row *row, *next;
1324 LIST_FOR_EACH_SAFE(row, next, track_node, &table->track_list) {
1325 if (!ovsdb_idl_track_is_set(row->table)) {
1326 list_remove(&row->track_node);
1335 ovsdb_idl_insert_row(struct ovsdb_idl_row *row, const struct json *row_json)
1337 const struct ovsdb_idl_table_class *class = row->table->class;
1340 ovs_assert(!row->old && !row->new);
1341 row->old = row->new = xmalloc(class->n_columns * sizeof *row->old);
1342 for (i = 0; i < class->n_columns; i++) {
1343 ovsdb_datum_init_default(&row->old[i], &class->columns[i].type);
1345 ovsdb_idl_row_update(row, row_json, OVSDB_IDL_CHANGE_INSERT);
1346 ovsdb_idl_row_parse(row);
1348 ovsdb_idl_row_reparse_backrefs(row);
1352 ovsdb_idl_delete_row(struct ovsdb_idl_row *row)
1354 ovsdb_idl_row_unparse(row);
1355 ovsdb_idl_row_clear_arcs(row, true);
1356 ovsdb_idl_row_clear_old(row);
1357 if (list_is_empty(&row->dst_arcs)) {
1358 ovsdb_idl_row_destroy(row);
1360 ovsdb_idl_row_reparse_backrefs(row);
1364 /* Returns true if a column with mode OVSDB_IDL_MODE_RW changed, false
1367 ovsdb_idl_modify_row(struct ovsdb_idl_row *row, const struct json *row_json)
1371 ovsdb_idl_row_unparse(row);
1372 ovsdb_idl_row_clear_arcs(row, true);
1373 changed = ovsdb_idl_row_update(row, row_json, OVSDB_IDL_CHANGE_MODIFY);
1374 ovsdb_idl_row_parse(row);
1380 may_add_arc(const struct ovsdb_idl_row *src, const struct ovsdb_idl_row *dst)
1382 const struct ovsdb_idl_arc *arc;
1389 /* No duplicate arcs.
1391 * We only need to test whether the first arc in dst->dst_arcs originates
1392 * at 'src', since we add all of the arcs from a given source in a clump
1393 * (in a single call to ovsdb_idl_row_parse()) and new arcs are always
1394 * added at the front of the dst_arcs list. */
1395 if (list_is_empty(&dst->dst_arcs)) {
1398 arc = CONTAINER_OF(dst->dst_arcs.next, struct ovsdb_idl_arc, dst_node);
1399 return arc->src != src;
1402 static struct ovsdb_idl_table *
1403 ovsdb_idl_table_from_class(const struct ovsdb_idl *idl,
1404 const struct ovsdb_idl_table_class *table_class)
1406 return &idl->tables[table_class - idl->class->tables];
1409 /* Called by ovsdb-idlc generated code. */
1410 struct ovsdb_idl_row *
1411 ovsdb_idl_get_row_arc(struct ovsdb_idl_row *src,
1412 struct ovsdb_idl_table_class *dst_table_class,
1413 const struct uuid *dst_uuid)
1415 struct ovsdb_idl *idl = src->table->idl;
1416 struct ovsdb_idl_table *dst_table;
1417 struct ovsdb_idl_arc *arc;
1418 struct ovsdb_idl_row *dst;
1420 dst_table = ovsdb_idl_table_from_class(idl, dst_table_class);
1421 dst = ovsdb_idl_get_row(dst_table, dst_uuid);
1423 /* We're being called from ovsdb_idl_txn_write(). We must not update
1424 * any arcs, because the transaction will be backed out at commit or
1425 * abort time and we don't want our graph screwed up.
1427 * Just return the destination row, if there is one and it has not been
1429 if (dst && (hmap_node_is_null(&dst->txn_node) || dst->new)) {
1434 /* We're being called from some other context. Update the graph. */
1436 dst = ovsdb_idl_row_create(dst_table, dst_uuid);
1439 /* Add a new arc, if it wouldn't be a self-arc or a duplicate arc. */
1440 if (may_add_arc(src, dst)) {
1441 /* The arc *must* be added at the front of the dst_arcs list. See
1442 * ovsdb_idl_row_reparse_backrefs() for details. */
1443 arc = xmalloc(sizeof *arc);
1444 list_push_front(&src->src_arcs, &arc->src_node);
1445 list_push_front(&dst->dst_arcs, &arc->dst_node);
1450 return !ovsdb_idl_row_is_orphan(dst) ? dst : NULL;
1454 /* Searches 'tc''s table in 'idl' for a row with UUID 'uuid'. Returns a
1455 * pointer to the row if there is one, otherwise a null pointer. */
1456 const struct ovsdb_idl_row *
1457 ovsdb_idl_get_row_for_uuid(const struct ovsdb_idl *idl,
1458 const struct ovsdb_idl_table_class *tc,
1459 const struct uuid *uuid)
1461 return ovsdb_idl_get_row(ovsdb_idl_table_from_class(idl, tc), uuid);
1464 static struct ovsdb_idl_row *
1465 next_real_row(struct ovsdb_idl_table *table, struct hmap_node *node)
1467 for (; node; node = hmap_next(&table->rows, node)) {
1468 struct ovsdb_idl_row *row;
1470 row = CONTAINER_OF(node, struct ovsdb_idl_row, hmap_node);
1471 if (ovsdb_idl_row_exists(row)) {
1478 /* Returns a row in 'table_class''s table in 'idl', or a null pointer if that
1481 * Database tables are internally maintained as hash tables, so adding or
1482 * removing rows while traversing the same table can cause some rows to be
1483 * visited twice or not at apply. */
1484 const struct ovsdb_idl_row *
1485 ovsdb_idl_first_row(const struct ovsdb_idl *idl,
1486 const struct ovsdb_idl_table_class *table_class)
1488 struct ovsdb_idl_table *table
1489 = ovsdb_idl_table_from_class(idl, table_class);
1490 return next_real_row(table, hmap_first(&table->rows));
1493 /* Returns a row following 'row' within its table, or a null pointer if 'row'
1494 * is the last row in its table. */
1495 const struct ovsdb_idl_row *
1496 ovsdb_idl_next_row(const struct ovsdb_idl_row *row)
1498 struct ovsdb_idl_table *table = row->table;
1500 return next_real_row(table, hmap_next(&table->rows, &row->hmap_node));
1503 /* Reads and returns the value of 'column' within 'row'. If an ongoing
1504 * transaction has changed 'column''s value, the modified value is returned.
1506 * The caller must not modify or free the returned value.
1508 * Various kinds of changes can invalidate the returned value: writing to the
1509 * same 'column' in 'row' (e.g. with ovsdb_idl_txn_write()), deleting 'row'
1510 * (e.g. with ovsdb_idl_txn_delete()), or completing an ongoing transaction
1511 * (e.g. with ovsdb_idl_txn_commit() or ovsdb_idl_txn_abort()). If the
1512 * returned value is needed for a long time, it is best to make a copy of it
1513 * with ovsdb_datum_clone(). */
1514 const struct ovsdb_datum *
1515 ovsdb_idl_read(const struct ovsdb_idl_row *row,
1516 const struct ovsdb_idl_column *column)
1518 const struct ovsdb_idl_table_class *class;
1521 ovs_assert(!ovsdb_idl_row_is_synthetic(row));
1523 class = row->table->class;
1524 column_idx = column - class->columns;
1526 ovs_assert(row->new != NULL);
1527 ovs_assert(column_idx < class->n_columns);
1529 if (row->written && bitmap_is_set(row->written, column_idx)) {
1530 return &row->new[column_idx];
1531 } else if (row->old) {
1532 return &row->old[column_idx];
1534 return ovsdb_datum_default(&column->type);
1538 /* Same as ovsdb_idl_read(), except that it also asserts that 'column' has key
1539 * type 'key_type' and value type 'value_type'. (Scalar and set types will
1540 * have a value type of OVSDB_TYPE_VOID.)
1542 * This is useful in code that "knows" that a particular column has a given
1543 * type, so that it will abort if someone changes the column's type without
1544 * updating the code that uses it. */
1545 const struct ovsdb_datum *
1546 ovsdb_idl_get(const struct ovsdb_idl_row *row,
1547 const struct ovsdb_idl_column *column,
1548 enum ovsdb_atomic_type key_type OVS_UNUSED,
1549 enum ovsdb_atomic_type value_type OVS_UNUSED)
1551 ovs_assert(column->type.key.type == key_type);
1552 ovs_assert(column->type.value.type == value_type);
1554 return ovsdb_idl_read(row, column);
1557 /* Returns true if the field represented by 'column' in 'row' may be modified,
1558 * false if it is immutable.
1560 * Normally, whether a field is mutable is controlled by its column's schema.
1561 * However, an immutable column can be set to any initial value at the time of
1562 * insertion, so if 'row' is a new row (one that is being added as part of the
1563 * current transaction, supposing that a transaction is in progress) then even
1564 * its "immutable" fields are actually mutable. */
1566 ovsdb_idl_is_mutable(const struct ovsdb_idl_row *row,
1567 const struct ovsdb_idl_column *column)
1569 return column->mutable || (row->new && !row->old);
1572 /* Returns false if 'row' was obtained from the IDL, true if it was initialized
1573 * to all-zero-bits by some other entity. If 'row' was set up some other way
1574 * then the return value is indeterminate. */
1576 ovsdb_idl_row_is_synthetic(const struct ovsdb_idl_row *row)
1578 return row->table == NULL;
1583 static void ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
1584 enum ovsdb_idl_txn_status);
1586 /* Returns a string representation of 'status'. The caller must not modify or
1587 * free the returned string.
1589 * The return value is probably useful only for debug log messages and unit
1592 ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status status)
1595 case TXN_UNCOMMITTED:
1596 return "uncommitted";
1599 case TXN_INCOMPLETE:
1600 return "incomplete";
1607 case TXN_NOT_LOCKED:
1608 return "not locked";
1615 /* Starts a new transaction on 'idl'. A given ovsdb_idl may only have a single
1616 * active transaction at a time. See the large comment in ovsdb-idl.h for
1617 * general information on transactions. */
1618 struct ovsdb_idl_txn *
1619 ovsdb_idl_txn_create(struct ovsdb_idl *idl)
1621 struct ovsdb_idl_txn *txn;
1623 ovs_assert(!idl->txn);
1624 idl->txn = txn = xmalloc(sizeof *txn);
1625 txn->request_id = NULL;
1627 hmap_init(&txn->txn_rows);
1628 txn->status = TXN_UNCOMMITTED;
1630 txn->dry_run = false;
1631 ds_init(&txn->comment);
1633 txn->inc_table = NULL;
1634 txn->inc_column = NULL;
1636 hmap_init(&txn->inserted_rows);
1641 /* Appends 's', which is treated as a printf()-type format string, to the
1642 * comments that will be passed to the OVSDB server when 'txn' is committed.
1643 * (The comment will be committed to the OVSDB log, which "ovsdb-tool
1644 * show-log" can print in a relatively human-readable form.) */
1646 ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *txn, const char *s, ...)
1650 if (txn->comment.length) {
1651 ds_put_char(&txn->comment, '\n');
1655 ds_put_format_valist(&txn->comment, s, args);
1659 /* Marks 'txn' as a transaction that will not actually modify the database. In
1660 * almost every way, the transaction is treated like other transactions. It
1661 * must be committed or aborted like other transactions, it will be sent to the
1662 * database server like other transactions, and so on. The only difference is
1663 * that the operations sent to the database server will include, as the last
1664 * step, an "abort" operation, so that any changes made by the transaction will
1665 * not actually take effect. */
1667 ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *txn)
1669 txn->dry_run = true;
1672 /* Causes 'txn', when committed, to increment the value of 'column' within
1673 * 'row' by 1. 'column' must have an integer type. After 'txn' commits
1674 * successfully, the client may retrieve the final (incremented) value of
1675 * 'column' with ovsdb_idl_txn_get_increment_new_value().
1677 * The client could accomplish something similar with ovsdb_idl_read(),
1678 * ovsdb_idl_txn_verify() and ovsdb_idl_txn_write(), or with ovsdb-idlc
1679 * generated wrappers for these functions. However, ovsdb_idl_txn_increment()
1680 * will never (by itself) fail because of a verify error.
1682 * The intended use is for incrementing the "next_cfg" column in the
1683 * Open_vSwitch table. */
1685 ovsdb_idl_txn_increment(struct ovsdb_idl_txn *txn,
1686 const struct ovsdb_idl_row *row,
1687 const struct ovsdb_idl_column *column)
1689 ovs_assert(!txn->inc_table);
1690 ovs_assert(column->type.key.type == OVSDB_TYPE_INTEGER);
1691 ovs_assert(column->type.value.type == OVSDB_TYPE_VOID);
1693 txn->inc_table = row->table->class->name;
1694 txn->inc_column = column->name;
1695 txn->inc_row = row->uuid;
1698 /* Destroys 'txn' and frees all associated memory. If ovsdb_idl_txn_commit()
1699 * has been called for 'txn' but the commit is still incomplete (that is, the
1700 * last call returned TXN_INCOMPLETE) then the transaction may or may not still
1701 * end up committing at the database server, but the client will not be able to
1702 * get any further status information back. */
1704 ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *txn)
1706 struct ovsdb_idl_txn_insert *insert, *next;
1708 json_destroy(txn->request_id);
1709 if (txn->status == TXN_INCOMPLETE) {
1710 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
1712 ovsdb_idl_txn_abort(txn);
1713 ds_destroy(&txn->comment);
1715 HMAP_FOR_EACH_SAFE (insert, next, hmap_node, &txn->inserted_rows) {
1718 hmap_destroy(&txn->inserted_rows);
1722 /* Causes poll_block() to wake up if 'txn' has completed committing. */
1724 ovsdb_idl_txn_wait(const struct ovsdb_idl_txn *txn)
1726 if (txn->status != TXN_UNCOMMITTED && txn->status != TXN_INCOMPLETE) {
1727 poll_immediate_wake();
1731 static struct json *
1732 where_uuid_equals(const struct uuid *uuid)
1735 json_array_create_1(
1736 json_array_create_3(
1737 json_string_create("_uuid"),
1738 json_string_create("=="),
1739 json_array_create_2(
1740 json_string_create("uuid"),
1741 json_string_create_nocopy(
1742 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
1746 uuid_name_from_uuid(const struct uuid *uuid)
1751 name = xasprintf("row"UUID_FMT, UUID_ARGS(uuid));
1752 for (p = name; *p != '\0'; p++) {
1761 static const struct ovsdb_idl_row *
1762 ovsdb_idl_txn_get_row(const struct ovsdb_idl_txn *txn, const struct uuid *uuid)
1764 const struct ovsdb_idl_row *row;
1766 HMAP_FOR_EACH_WITH_HASH (row, txn_node, uuid_hash(uuid), &txn->txn_rows) {
1767 if (uuid_equals(&row->uuid, uuid)) {
1774 /* XXX there must be a cleaner way to do this */
1775 static struct json *
1776 substitute_uuids(struct json *json, const struct ovsdb_idl_txn *txn)
1778 if (json->type == JSON_ARRAY) {
1782 if (json->u.array.n == 2
1783 && json->u.array.elems[0]->type == JSON_STRING
1784 && json->u.array.elems[1]->type == JSON_STRING
1785 && !strcmp(json->u.array.elems[0]->u.string, "uuid")
1786 && uuid_from_string(&uuid, json->u.array.elems[1]->u.string)) {
1787 const struct ovsdb_idl_row *row;
1789 row = ovsdb_idl_txn_get_row(txn, &uuid);
1790 if (row && !row->old && row->new) {
1793 return json_array_create_2(
1794 json_string_create("named-uuid"),
1795 json_string_create_nocopy(uuid_name_from_uuid(&uuid)));
1799 for (i = 0; i < json->u.array.n; i++) {
1800 json->u.array.elems[i] = substitute_uuids(json->u.array.elems[i],
1803 } else if (json->type == JSON_OBJECT) {
1804 struct shash_node *node;
1806 SHASH_FOR_EACH (node, json_object(json)) {
1807 node->data = substitute_uuids(node->data, txn);
1814 ovsdb_idl_txn_disassemble(struct ovsdb_idl_txn *txn)
1816 struct ovsdb_idl_row *row, *next;
1818 /* This must happen early. Otherwise, ovsdb_idl_row_parse() will call an
1819 * ovsdb_idl_column's 'parse' function, which will call
1820 * ovsdb_idl_get_row_arc(), which will seen that the IDL is in a
1821 * transaction and fail to update the graph. */
1822 txn->idl->txn = NULL;
1824 HMAP_FOR_EACH_SAFE (row, next, txn_node, &txn->txn_rows) {
1827 ovsdb_idl_row_unparse(row);
1828 ovsdb_idl_row_clear_arcs(row, false);
1829 ovsdb_idl_row_parse(row);
1832 ovsdb_idl_row_unparse(row);
1834 ovsdb_idl_row_clear_new(row);
1837 row->prereqs = NULL;
1840 row->written = NULL;
1842 hmap_remove(&txn->txn_rows, &row->txn_node);
1843 hmap_node_nullify(&row->txn_node);
1845 hmap_remove(&row->table->rows, &row->hmap_node);
1849 hmap_destroy(&txn->txn_rows);
1850 hmap_init(&txn->txn_rows);
1853 /* Attempts to commit 'txn'. Returns the status of the commit operation, one
1854 * of the following TXN_* constants:
1858 * The transaction is in progress, but not yet complete. The caller
1859 * should call again later, after calling ovsdb_idl_run() to let the IDL
1860 * do OVSDB protocol processing.
1864 * The transaction is complete. (It didn't actually change the database,
1865 * so the IDL didn't send any request to the database server.)
1869 * The caller previously called ovsdb_idl_txn_abort().
1873 * The transaction was successful. The update made by the transaction
1874 * (and possibly other changes made by other database clients) should
1875 * already be visible in the IDL.
1879 * The transaction failed for some transient reason, e.g. because a
1880 * "verify" operation reported an inconsistency or due to a network
1881 * problem. The caller should wait for a change to the database, then
1882 * compose a new transaction, and commit the new transaction.
1884 * Use the return value of ovsdb_idl_get_seqno() to wait for a change in
1885 * the database. It is important to use its return value *before* the
1886 * initial call to ovsdb_idl_txn_commit() as the baseline for this
1887 * purpose, because the change that one should wait for can happen after
1888 * the initial call but before the call that returns TXN_TRY_AGAIN, and
1889 * using some other baseline value in that situation could cause an
1890 * indefinite wait if the database rarely changes.
1894 * The transaction failed because the IDL has been configured to require
1895 * a database lock (with ovsdb_idl_set_lock()) but didn't get it yet or
1896 * has already lost it.
1898 * Committing a transaction rolls back all of the changes that it made to the
1899 * IDL's copy of the database. If the transaction commits successfully, then
1900 * the database server will send an update and, thus, the IDL will be updated
1901 * with the committed changes. */
1902 enum ovsdb_idl_txn_status
1903 ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
1905 struct ovsdb_idl_row *row;
1906 struct json *operations;
1909 if (txn != txn->idl->txn) {
1913 /* If we need a lock but don't have it, give up quickly. */
1914 if (txn->idl->lock_name && !ovsdb_idl_has_lock(txn->idl)) {
1915 txn->status = TXN_NOT_LOCKED;
1916 goto disassemble_out;
1919 operations = json_array_create_1(
1920 json_string_create(txn->idl->class->database));
1922 /* Assert that we have the required lock (avoiding a race). */
1923 if (txn->idl->lock_name) {
1924 struct json *op = json_object_create();
1925 json_array_add(operations, op);
1926 json_object_put_string(op, "op", "assert");
1927 json_object_put_string(op, "lock", txn->idl->lock_name);
1930 /* Add prerequisites and declarations of new rows. */
1931 HMAP_FOR_EACH (row, txn_node, &txn->txn_rows) {
1932 /* XXX check that deleted rows exist even if no prereqs? */
1934 const struct ovsdb_idl_table_class *class = row->table->class;
1935 size_t n_columns = class->n_columns;
1936 struct json *op, *columns, *row_json;
1939 op = json_object_create();
1940 json_array_add(operations, op);
1941 json_object_put_string(op, "op", "wait");
1942 json_object_put_string(op, "table", class->name);
1943 json_object_put(op, "timeout", json_integer_create(0));
1944 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1945 json_object_put_string(op, "until", "==");
1946 columns = json_array_create_empty();
1947 json_object_put(op, "columns", columns);
1948 row_json = json_object_create();
1949 json_object_put(op, "rows", json_array_create_1(row_json));
1951 BITMAP_FOR_EACH_1 (idx, n_columns, row->prereqs) {
1952 const struct ovsdb_idl_column *column = &class->columns[idx];
1953 json_array_add(columns, json_string_create(column->name));
1954 json_object_put(row_json, column->name,
1955 ovsdb_datum_to_json(&row->old[idx],
1962 any_updates = false;
1963 HMAP_FOR_EACH (row, txn_node, &txn->txn_rows) {
1964 const struct ovsdb_idl_table_class *class = row->table->class;
1967 if (class->is_root) {
1968 struct json *op = json_object_create();
1969 json_object_put_string(op, "op", "delete");
1970 json_object_put_string(op, "table", class->name);
1971 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1972 json_array_add(operations, op);
1975 /* Let ovsdb-server decide whether to really delete it. */
1977 } else if (row->old != row->new) {
1978 struct json *row_json;
1982 op = json_object_create();
1983 json_object_put_string(op, "op", row->old ? "update" : "insert");
1984 json_object_put_string(op, "table", class->name);
1986 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1988 struct ovsdb_idl_txn_insert *insert;
1992 json_object_put(op, "uuid-name",
1993 json_string_create_nocopy(
1994 uuid_name_from_uuid(&row->uuid)));
1996 insert = xmalloc(sizeof *insert);
1997 insert->dummy = row->uuid;
1998 insert->op_index = operations->u.array.n - 1;
1999 uuid_zero(&insert->real);
2000 hmap_insert(&txn->inserted_rows, &insert->hmap_node,
2001 uuid_hash(&insert->dummy));
2003 row_json = json_object_create();
2004 json_object_put(op, "row", row_json);
2007 BITMAP_FOR_EACH_1 (idx, class->n_columns, row->written) {
2008 const struct ovsdb_idl_column *column =
2009 &class->columns[idx];
2012 || !ovsdb_datum_is_default(&row->new[idx],
2014 json_object_put(row_json, column->name,
2016 ovsdb_datum_to_json(&row->new[idx],
2020 /* If anything really changed, consider it an update.
2021 * We can't suppress not-really-changed values earlier
2022 * or transactions would become nonatomic (see the big
2023 * comment inside ovsdb_idl_txn_write()). */
2024 if (!any_updates && row->old &&
2025 !ovsdb_datum_equals(&row->old[idx], &row->new[idx],
2033 if (!row->old || !shash_is_empty(json_object(row_json))) {
2034 json_array_add(operations, op);
2041 /* Add increment. */
2042 if (txn->inc_table && any_updates) {
2045 txn->inc_index = operations->u.array.n - 1;
2047 op = json_object_create();
2048 json_object_put_string(op, "op", "mutate");
2049 json_object_put_string(op, "table", txn->inc_table);
2050 json_object_put(op, "where",
2051 substitute_uuids(where_uuid_equals(&txn->inc_row),
2053 json_object_put(op, "mutations",
2054 json_array_create_1(
2055 json_array_create_3(
2056 json_string_create(txn->inc_column),
2057 json_string_create("+="),
2058 json_integer_create(1))));
2059 json_array_add(operations, op);
2061 op = json_object_create();
2062 json_object_put_string(op, "op", "select");
2063 json_object_put_string(op, "table", txn->inc_table);
2064 json_object_put(op, "where",
2065 substitute_uuids(where_uuid_equals(&txn->inc_row),
2067 json_object_put(op, "columns",
2068 json_array_create_1(json_string_create(
2070 json_array_add(operations, op);
2073 if (txn->comment.length) {
2074 struct json *op = json_object_create();
2075 json_object_put_string(op, "op", "comment");
2076 json_object_put_string(op, "comment", ds_cstr(&txn->comment));
2077 json_array_add(operations, op);
2081 struct json *op = json_object_create();
2082 json_object_put_string(op, "op", "abort");
2083 json_array_add(operations, op);
2087 txn->status = TXN_UNCHANGED;
2088 json_destroy(operations);
2089 } else if (!jsonrpc_session_send(
2091 jsonrpc_create_request(
2092 "transact", operations, &txn->request_id))) {
2093 hmap_insert(&txn->idl->outstanding_txns, &txn->hmap_node,
2094 json_hash(txn->request_id, 0));
2095 txn->status = TXN_INCOMPLETE;
2097 txn->status = TXN_TRY_AGAIN;
2101 ovsdb_idl_txn_disassemble(txn);
2103 switch (txn->status) {
2104 case TXN_UNCOMMITTED: COVERAGE_INC(txn_uncommitted); break;
2105 case TXN_UNCHANGED: COVERAGE_INC(txn_unchanged); break;
2106 case TXN_INCOMPLETE: COVERAGE_INC(txn_incomplete); break;
2107 case TXN_ABORTED: COVERAGE_INC(txn_aborted); break;
2108 case TXN_SUCCESS: COVERAGE_INC(txn_success); break;
2109 case TXN_TRY_AGAIN: COVERAGE_INC(txn_try_again); break;
2110 case TXN_NOT_LOCKED: COVERAGE_INC(txn_not_locked); break;
2111 case TXN_ERROR: COVERAGE_INC(txn_error); break;
2117 /* Attempts to commit 'txn', blocking until the commit either succeeds or
2118 * fails. Returns the final commit status, which may be any TXN_* value other
2119 * than TXN_INCOMPLETE.
2121 * This function calls ovsdb_idl_run() on 'txn''s IDL, so it may cause the
2122 * return value of ovsdb_idl_get_seqno() to change. */
2123 enum ovsdb_idl_txn_status
2124 ovsdb_idl_txn_commit_block(struct ovsdb_idl_txn *txn)
2126 enum ovsdb_idl_txn_status status;
2129 while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
2130 ovsdb_idl_run(txn->idl);
2131 ovsdb_idl_wait(txn->idl);
2132 ovsdb_idl_txn_wait(txn);
2138 /* Returns the final (incremented) value of the column in 'txn' that was set to
2139 * be incremented by ovsdb_idl_txn_increment(). 'txn' must have committed
2142 ovsdb_idl_txn_get_increment_new_value(const struct ovsdb_idl_txn *txn)
2144 ovs_assert(txn->status == TXN_SUCCESS);
2145 return txn->inc_new_value;
2148 /* Aborts 'txn' without sending it to the database server. This is effective
2149 * only if ovsdb_idl_txn_commit() has not yet been called for 'txn'.
2150 * Otherwise, it has no effect.
2152 * Aborting a transaction doesn't free its memory. Use
2153 * ovsdb_idl_txn_destroy() to do that. */
2155 ovsdb_idl_txn_abort(struct ovsdb_idl_txn *txn)
2157 ovsdb_idl_txn_disassemble(txn);
2158 if (txn->status == TXN_UNCOMMITTED || txn->status == TXN_INCOMPLETE) {
2159 txn->status = TXN_ABORTED;
2163 /* Returns a string that reports the error status for 'txn'. The caller must
2164 * not modify or free the returned string. A call to ovsdb_idl_txn_destroy()
2165 * for 'txn' may free the returned string.
2167 * The return value is ordinarily one of the strings that
2168 * ovsdb_idl_txn_status_to_string() would return, but if the transaction failed
2169 * due to an error reported by the database server, the return value is that
2172 ovsdb_idl_txn_get_error(const struct ovsdb_idl_txn *txn)
2174 if (txn->status != TXN_ERROR) {
2175 return ovsdb_idl_txn_status_to_string(txn->status);
2176 } else if (txn->error) {
2179 return "no error details available";
2184 ovsdb_idl_txn_set_error_json(struct ovsdb_idl_txn *txn,
2185 const struct json *json)
2187 if (txn->error == NULL) {
2188 txn->error = json_to_string(json, JSSF_SORT);
2192 /* For transaction 'txn' that completed successfully, finds and returns the
2193 * permanent UUID that the database assigned to a newly inserted row, given the
2194 * 'uuid' that ovsdb_idl_txn_insert() assigned locally to that row.
2196 * Returns NULL if 'uuid' is not a UUID assigned by ovsdb_idl_txn_insert() or
2197 * if it was assigned by that function and then deleted by
2198 * ovsdb_idl_txn_delete() within the same transaction. (Rows that are inserted
2199 * and then deleted within a single transaction are never sent to the database
2200 * server, so it never assigns them a permanent UUID.) */
2202 ovsdb_idl_txn_get_insert_uuid(const struct ovsdb_idl_txn *txn,
2203 const struct uuid *uuid)
2205 const struct ovsdb_idl_txn_insert *insert;
2207 ovs_assert(txn->status == TXN_SUCCESS || txn->status == TXN_UNCHANGED);
2208 HMAP_FOR_EACH_IN_BUCKET (insert, hmap_node,
2209 uuid_hash(uuid), &txn->inserted_rows) {
2210 if (uuid_equals(uuid, &insert->dummy)) {
2211 return &insert->real;
2218 ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
2219 enum ovsdb_idl_txn_status status)
2221 txn->status = status;
2222 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
2225 /* Writes 'datum' to the specified 'column' in 'row_'. Updates both 'row_'
2226 * itself and the structs derived from it (e.g. the "struct ovsrec_*", for
2229 * 'datum' must have the correct type for its column. The IDL does not check
2230 * that it meets schema constraints, but ovsdb-server will do so at commit time
2231 * so it had better be correct.
2233 * A transaction must be in progress. Replication of 'column' must not have
2234 * been disabled (by calling ovsdb_idl_omit()).
2236 * Usually this function is used indirectly through one of the "set" functions
2237 * generated by ovsdb-idlc.
2239 * Takes ownership of what 'datum' points to (and in some cases destroys that
2240 * data before returning) but makes a copy of 'datum' itself. (Commonly
2241 * 'datum' is on the caller's stack.) */
2243 ovsdb_idl_txn_write__(const struct ovsdb_idl_row *row_,
2244 const struct ovsdb_idl_column *column,
2245 struct ovsdb_datum *datum, bool owns_datum)
2247 struct ovsdb_idl_row *row = CONST_CAST(struct ovsdb_idl_row *, row_);
2248 const struct ovsdb_idl_table_class *class;
2252 if (ovsdb_idl_row_is_synthetic(row)) {
2256 class = row->table->class;
2257 column_idx = column - class->columns;
2258 write_only = row->table->modes[column_idx] == OVSDB_IDL_MONITOR;
2260 ovs_assert(row->new != NULL);
2261 ovs_assert(column_idx < class->n_columns);
2262 ovs_assert(row->old == NULL ||
2263 row->table->modes[column_idx] & OVSDB_IDL_MONITOR);
2265 if (row->table->idl->verify_write_only && !write_only) {
2266 VLOG_ERR("Bug: Attempt to write to a read/write column (%s:%s) when"
2267 " explicitly configured not to.", class->name, column->name);
2271 /* If this is a write-only column and the datum being written is the same
2272 * as the one already there, just skip the update entirely. This is worth
2273 * optimizing because we have a lot of columns that get periodically
2274 * refreshed into the database but don't actually change that often.
2276 * We don't do this for read/write columns because that would break
2277 * atomicity of transactions--some other client might have written a
2278 * different value in that column since we read it. (But if a whole
2279 * transaction only does writes of existing values, without making any real
2280 * changes, we will drop the whole transaction later in
2281 * ovsdb_idl_txn_commit().) */
2282 if (write_only && ovsdb_datum_equals(ovsdb_idl_read(row, column),
2283 datum, &column->type)) {
2287 if (hmap_node_is_null(&row->txn_node)) {
2288 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
2289 uuid_hash(&row->uuid));
2291 if (row->old == row->new) {
2292 row->new = xmalloc(class->n_columns * sizeof *row->new);
2294 if (!row->written) {
2295 row->written = bitmap_allocate(class->n_columns);
2297 if (bitmap_is_set(row->written, column_idx)) {
2298 ovsdb_datum_destroy(&row->new[column_idx], &column->type);
2300 bitmap_set1(row->written, column_idx);
2303 row->new[column_idx] = *datum;
2305 ovsdb_datum_clone(&row->new[column_idx], datum, &column->type);
2307 (column->unparse)(row);
2308 (column->parse)(row, &row->new[column_idx]);
2313 ovsdb_datum_destroy(datum, &column->type);
2318 ovsdb_idl_txn_write(const struct ovsdb_idl_row *row,
2319 const struct ovsdb_idl_column *column,
2320 struct ovsdb_datum *datum)
2322 ovsdb_idl_txn_write__(row, column, datum, true);
2326 ovsdb_idl_txn_write_clone(const struct ovsdb_idl_row *row,
2327 const struct ovsdb_idl_column *column,
2328 const struct ovsdb_datum *datum)
2330 ovsdb_idl_txn_write__(row, column,
2331 CONST_CAST(struct ovsdb_datum *, datum), false);
2334 /* Causes the original contents of 'column' in 'row_' to be verified as a
2335 * prerequisite to completing the transaction. That is, if 'column' in 'row_'
2336 * changed (or if 'row_' was deleted) between the time that the IDL originally
2337 * read its contents and the time that the transaction commits, then the
2338 * transaction aborts and ovsdb_idl_txn_commit() returns TXN_AGAIN_WAIT or
2339 * TXN_AGAIN_NOW (depending on whether the database change has already been
2342 * The intention is that, to ensure that no transaction commits based on dirty
2343 * reads, an application should call ovsdb_idl_txn_verify() on each data item
2344 * read as part of a read-modify-write operation.
2346 * In some cases ovsdb_idl_txn_verify() reduces to a no-op, because the current
2347 * value of 'column' is already known:
2349 * - If 'row_' is a row created by the current transaction (returned by
2350 * ovsdb_idl_txn_insert()).
2352 * - If 'column' has already been modified (with ovsdb_idl_txn_write())
2353 * within the current transaction.
2355 * Because of the latter property, always call ovsdb_idl_txn_verify() *before*
2356 * ovsdb_idl_txn_write() for a given read-modify-write.
2358 * A transaction must be in progress.
2360 * Usually this function is used indirectly through one of the "verify"
2361 * functions generated by ovsdb-idlc. */
2363 ovsdb_idl_txn_verify(const struct ovsdb_idl_row *row_,
2364 const struct ovsdb_idl_column *column)
2366 struct ovsdb_idl_row *row = CONST_CAST(struct ovsdb_idl_row *, row_);
2367 const struct ovsdb_idl_table_class *class;
2370 if (ovsdb_idl_row_is_synthetic(row)) {
2374 class = row->table->class;
2375 column_idx = column - class->columns;
2377 ovs_assert(row->new != NULL);
2378 ovs_assert(row->old == NULL ||
2379 row->table->modes[column_idx] & OVSDB_IDL_MONITOR);
2381 || (row->written && bitmap_is_set(row->written, column_idx))) {
2385 if (hmap_node_is_null(&row->txn_node)) {
2386 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
2387 uuid_hash(&row->uuid));
2389 if (!row->prereqs) {
2390 row->prereqs = bitmap_allocate(class->n_columns);
2392 bitmap_set1(row->prereqs, column_idx);
2395 /* Deletes 'row_' from its table. May free 'row_', so it must not be
2396 * accessed afterward.
2398 * A transaction must be in progress.
2400 * Usually this function is used indirectly through one of the "delete"
2401 * functions generated by ovsdb-idlc. */
2403 ovsdb_idl_txn_delete(const struct ovsdb_idl_row *row_)
2405 struct ovsdb_idl_row *row = CONST_CAST(struct ovsdb_idl_row *, row_);
2407 if (ovsdb_idl_row_is_synthetic(row)) {
2411 ovs_assert(row->new != NULL);
2413 ovsdb_idl_row_unparse(row);
2414 ovsdb_idl_row_clear_new(row);
2415 ovs_assert(!row->prereqs);
2416 hmap_remove(&row->table->rows, &row->hmap_node);
2417 hmap_remove(&row->table->idl->txn->txn_rows, &row->txn_node);
2421 if (hmap_node_is_null(&row->txn_node)) {
2422 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
2423 uuid_hash(&row->uuid));
2425 ovsdb_idl_row_clear_new(row);
2429 /* Inserts and returns a new row in the table with the specified 'class' in the
2430 * database with open transaction 'txn'.
2432 * The new row is assigned a provisional UUID. If 'uuid' is null then one is
2433 * randomly generated; otherwise 'uuid' should specify a randomly generated
2434 * UUID not otherwise in use. ovsdb-server will assign a different UUID when
2435 * 'txn' is committed, but the IDL will replace any uses of the provisional
2436 * UUID in the data to be to be committed by the UUID assigned by
2439 * Usually this function is used indirectly through one of the "insert"
2440 * functions generated by ovsdb-idlc. */
2441 const struct ovsdb_idl_row *
2442 ovsdb_idl_txn_insert(struct ovsdb_idl_txn *txn,
2443 const struct ovsdb_idl_table_class *class,
2444 const struct uuid *uuid)
2446 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(class);
2449 ovs_assert(!ovsdb_idl_txn_get_row(txn, uuid));
2452 uuid_generate(&row->uuid);
2455 row->table = ovsdb_idl_table_from_class(txn->idl, class);
2456 row->new = xmalloc(class->n_columns * sizeof *row->new);
2457 hmap_insert(&row->table->rows, &row->hmap_node, uuid_hash(&row->uuid));
2458 hmap_insert(&txn->txn_rows, &row->txn_node, uuid_hash(&row->uuid));
2463 ovsdb_idl_txn_abort_all(struct ovsdb_idl *idl)
2465 struct ovsdb_idl_txn *txn;
2467 HMAP_FOR_EACH (txn, hmap_node, &idl->outstanding_txns) {
2468 ovsdb_idl_txn_complete(txn, TXN_TRY_AGAIN);
2472 static struct ovsdb_idl_txn *
2473 ovsdb_idl_txn_find(struct ovsdb_idl *idl, const struct json *id)
2475 struct ovsdb_idl_txn *txn;
2477 HMAP_FOR_EACH_WITH_HASH (txn, hmap_node,
2478 json_hash(id, 0), &idl->outstanding_txns) {
2479 if (json_equal(id, txn->request_id)) {
2487 check_json_type(const struct json *json, enum json_type type, const char *name)
2490 VLOG_WARN_RL(&syntax_rl, "%s is missing", name);
2492 } else if (json->type != type) {
2493 VLOG_WARN_RL(&syntax_rl, "%s is %s instead of %s",
2494 name, json_type_to_string(json->type),
2495 json_type_to_string(type));
2503 ovsdb_idl_txn_process_inc_reply(struct ovsdb_idl_txn *txn,
2504 const struct json_array *results)
2506 struct json *count, *rows, *row, *column;
2507 struct shash *mutate, *select;
2509 if (txn->inc_index + 2 > results->n) {
2510 VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
2511 "for increment (has %"PRIuSIZE", needs %u)",
2512 results->n, txn->inc_index + 2);
2516 /* We know that this is a JSON object because the loop in
2517 * ovsdb_idl_txn_process_reply() checked. */
2518 mutate = json_object(results->elems[txn->inc_index]);
2519 count = shash_find_data(mutate, "count");
2520 if (!check_json_type(count, JSON_INTEGER, "\"mutate\" reply \"count\"")) {
2523 if (count->u.integer != 1) {
2524 VLOG_WARN_RL(&syntax_rl,
2525 "\"mutate\" reply \"count\" is %lld instead of 1",
2530 select = json_object(results->elems[txn->inc_index + 1]);
2531 rows = shash_find_data(select, "rows");
2532 if (!check_json_type(rows, JSON_ARRAY, "\"select\" reply \"rows\"")) {
2535 if (rows->u.array.n != 1) {
2536 VLOG_WARN_RL(&syntax_rl, "\"select\" reply \"rows\" has %"PRIuSIZE" elements "
2541 row = rows->u.array.elems[0];
2542 if (!check_json_type(row, JSON_OBJECT, "\"select\" reply row")) {
2545 column = shash_find_data(json_object(row), txn->inc_column);
2546 if (!check_json_type(column, JSON_INTEGER,
2547 "\"select\" reply inc column")) {
2550 txn->inc_new_value = column->u.integer;
2555 ovsdb_idl_txn_process_insert_reply(struct ovsdb_idl_txn_insert *insert,
2556 const struct json_array *results)
2558 static const struct ovsdb_base_type uuid_type = OVSDB_BASE_UUID_INIT;
2559 struct ovsdb_error *error;
2560 struct json *json_uuid;
2561 union ovsdb_atom uuid;
2562 struct shash *reply;
2564 if (insert->op_index >= results->n) {
2565 VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
2566 "for insert (has %"PRIuSIZE", needs %u)",
2567 results->n, insert->op_index);
2571 /* We know that this is a JSON object because the loop in
2572 * ovsdb_idl_txn_process_reply() checked. */
2573 reply = json_object(results->elems[insert->op_index]);
2574 json_uuid = shash_find_data(reply, "uuid");
2575 if (!check_json_type(json_uuid, JSON_ARRAY, "\"insert\" reply \"uuid\"")) {
2579 error = ovsdb_atom_from_json(&uuid, &uuid_type, json_uuid, NULL);
2581 char *s = ovsdb_error_to_string(error);
2582 VLOG_WARN_RL(&syntax_rl, "\"insert\" reply \"uuid\" is not a JSON "
2585 ovsdb_error_destroy(error);
2589 insert->real = uuid.uuid;
2595 ovsdb_idl_txn_process_reply(struct ovsdb_idl *idl,
2596 const struct jsonrpc_msg *msg)
2598 struct ovsdb_idl_txn *txn;
2599 enum ovsdb_idl_txn_status status;
2601 txn = ovsdb_idl_txn_find(idl, msg->id);
2606 if (msg->type == JSONRPC_ERROR) {
2608 } else if (msg->result->type != JSON_ARRAY) {
2609 VLOG_WARN_RL(&syntax_rl, "reply to \"transact\" is not JSON array");
2612 struct json_array *ops = &msg->result->u.array;
2613 int hard_errors = 0;
2614 int soft_errors = 0;
2615 int lock_errors = 0;
2618 for (i = 0; i < ops->n; i++) {
2619 struct json *op = ops->elems[i];
2621 if (op->type == JSON_NULL) {
2622 /* This isn't an error in itself but indicates that some prior
2623 * operation failed, so make sure that we know about it. */
2625 } else if (op->type == JSON_OBJECT) {
2628 error = shash_find_data(json_object(op), "error");
2630 if (error->type == JSON_STRING) {
2631 if (!strcmp(error->u.string, "timed out")) {
2633 } else if (!strcmp(error->u.string, "not owner")) {
2635 } else if (strcmp(error->u.string, "aborted")) {
2637 ovsdb_idl_txn_set_error_json(txn, op);
2641 ovsdb_idl_txn_set_error_json(txn, op);
2642 VLOG_WARN_RL(&syntax_rl,
2643 "\"error\" in reply is not JSON string");
2648 ovsdb_idl_txn_set_error_json(txn, op);
2649 VLOG_WARN_RL(&syntax_rl,
2650 "operation reply is not JSON null or object");
2654 if (!soft_errors && !hard_errors && !lock_errors) {
2655 struct ovsdb_idl_txn_insert *insert;
2657 if (txn->inc_table && !ovsdb_idl_txn_process_inc_reply(txn, ops)) {
2661 HMAP_FOR_EACH (insert, hmap_node, &txn->inserted_rows) {
2662 if (!ovsdb_idl_txn_process_insert_reply(insert, ops)) {
2668 status = (hard_errors ? TXN_ERROR
2669 : lock_errors ? TXN_NOT_LOCKED
2670 : soft_errors ? TXN_TRY_AGAIN
2674 ovsdb_idl_txn_complete(txn, status);
2678 /* Returns the transaction currently active for 'row''s IDL. A transaction
2679 * must currently be active. */
2680 struct ovsdb_idl_txn *
2681 ovsdb_idl_txn_get(const struct ovsdb_idl_row *row)
2683 struct ovsdb_idl_txn *txn = row->table->idl->txn;
2684 ovs_assert(txn != NULL);
2688 /* Returns the IDL on which 'txn' acts. */
2690 ovsdb_idl_txn_get_idl (struct ovsdb_idl_txn *txn)
2695 /* Blocks until 'idl' successfully connects to the remote database and
2696 * retrieves its contents. */
2698 ovsdb_idl_get_initial_snapshot(struct ovsdb_idl *idl)
2702 if (ovsdb_idl_has_ever_connected(idl)) {
2705 ovsdb_idl_wait(idl);
2710 /* If 'lock_name' is nonnull, configures 'idl' to obtain the named lock from
2711 * the database server and to avoid modifying the database when the lock cannot
2712 * be acquired (that is, when another client has the same lock).
2714 * If 'lock_name' is NULL, drops the locking requirement and releases the
2717 ovsdb_idl_set_lock(struct ovsdb_idl *idl, const char *lock_name)
2719 ovs_assert(!idl->txn);
2720 ovs_assert(hmap_is_empty(&idl->outstanding_txns));
2722 if (idl->lock_name && (!lock_name || strcmp(lock_name, idl->lock_name))) {
2723 /* Release previous lock. */
2724 ovsdb_idl_send_unlock_request(idl);
2725 free(idl->lock_name);
2726 idl->lock_name = NULL;
2727 idl->is_lock_contended = false;
2730 if (lock_name && !idl->lock_name) {
2731 /* Acquire new lock. */
2732 idl->lock_name = xstrdup(lock_name);
2733 ovsdb_idl_send_lock_request(idl);
2737 /* Returns true if 'idl' is configured to obtain a lock and owns that lock.
2739 * Locking and unlocking happens asynchronously from the database client's
2740 * point of view, so the information is only useful for optimization (e.g. if
2741 * the client doesn't have the lock then there's no point in trying to write to
2744 ovsdb_idl_has_lock(const struct ovsdb_idl *idl)
2746 return idl->has_lock;
2749 /* Returns true if 'idl' is configured to obtain a lock but the database server
2750 * has indicated that some other client already owns the requested lock. */
2752 ovsdb_idl_is_lock_contended(const struct ovsdb_idl *idl)
2754 return idl->is_lock_contended;
2758 ovsdb_idl_update_has_lock(struct ovsdb_idl *idl, bool new_has_lock)
2760 if (new_has_lock && !idl->has_lock) {
2761 if (idl->state == IDL_S_MONITORING) {
2762 idl->change_seqno++;
2764 /* We're setting up a session, so don't signal that the database
2765 * changed. Finalizing the session will increment change_seqno
2768 idl->is_lock_contended = false;
2770 idl->has_lock = new_has_lock;
2774 ovsdb_idl_send_lock_request__(struct ovsdb_idl *idl, const char *method,
2777 ovsdb_idl_update_has_lock(idl, false);
2779 json_destroy(idl->lock_request_id);
2780 idl->lock_request_id = NULL;
2782 if (jsonrpc_session_is_connected(idl->session)) {
2783 struct json *params;
2785 params = json_array_create_1(json_string_create(idl->lock_name));
2786 jsonrpc_session_send(idl->session,
2787 jsonrpc_create_request(method, params, idp));
2792 ovsdb_idl_send_lock_request(struct ovsdb_idl *idl)
2794 ovsdb_idl_send_lock_request__(idl, "lock", &idl->lock_request_id);
2798 ovsdb_idl_send_unlock_request(struct ovsdb_idl *idl)
2800 ovsdb_idl_send_lock_request__(idl, "unlock", NULL);
2804 ovsdb_idl_parse_lock_reply(struct ovsdb_idl *idl, const struct json *result)
2808 json_destroy(idl->lock_request_id);
2809 idl->lock_request_id = NULL;
2811 if (result->type == JSON_OBJECT) {
2812 const struct json *locked;
2814 locked = shash_find_data(json_object(result), "locked");
2815 got_lock = locked && locked->type == JSON_TRUE;
2820 ovsdb_idl_update_has_lock(idl, got_lock);
2822 idl->is_lock_contended = true;
2827 ovsdb_idl_parse_lock_notify(struct ovsdb_idl *idl,
2828 const struct json *params,
2832 && params->type == JSON_ARRAY
2833 && json_array(params)->n > 0
2834 && json_array(params)->elems[0]->type == JSON_STRING) {
2835 const char *lock_name = json_string(json_array(params)->elems[0]);
2837 if (!strcmp(idl->lock_name, lock_name)) {
2838 ovsdb_idl_update_has_lock(idl, new_has_lock);
2839 if (!new_has_lock) {
2840 idl->is_lock_contended = true;
2847 ovsdb_idl_loop_destroy(struct ovsdb_idl_loop *loop)
2850 ovsdb_idl_destroy(loop->idl);
2854 struct ovsdb_idl_txn *
2855 ovsdb_idl_loop_run(struct ovsdb_idl_loop *loop)
2857 ovsdb_idl_run(loop->idl);
2858 loop->open_txn = (loop->committing_txn
2859 || ovsdb_idl_get_seqno(loop->idl) == loop->skip_seqno
2861 : ovsdb_idl_txn_create(loop->idl));
2862 return loop->open_txn;
2866 ovsdb_idl_loop_commit_and_wait(struct ovsdb_idl_loop *loop)
2868 if (loop->open_txn) {
2869 loop->committing_txn = loop->open_txn;
2870 loop->open_txn = NULL;
2872 loop->precommit_seqno = ovsdb_idl_get_seqno(loop->idl);
2875 struct ovsdb_idl_txn *txn = loop->committing_txn;
2877 enum ovsdb_idl_txn_status status = ovsdb_idl_txn_commit(txn);
2878 if (status != TXN_INCOMPLETE) {
2881 /* We want to re-evaluate the database when it's changed from
2882 * the contents that it had when we started the commit. (That
2883 * might have already happened.) */
2884 loop->skip_seqno = loop->precommit_seqno;
2885 if (ovsdb_idl_get_seqno(loop->idl) != loop->skip_seqno) {
2886 poll_immediate_wake();
2891 /* If the database has already changed since we started the
2892 * commit, re-evaluate it immediately to avoid missing a change
2894 if (ovsdb_idl_get_seqno(loop->idl) != loop->precommit_seqno) {
2895 poll_immediate_wake();
2901 case TXN_NOT_LOCKED:
2905 case TXN_UNCOMMITTED:
2906 case TXN_INCOMPLETE:
2910 ovsdb_idl_txn_destroy(txn);
2911 loop->committing_txn = NULL;
2915 ovsdb_idl_wait(loop->idl);