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 *);
161 static void ovsdb_idl_row_parse(struct ovsdb_idl_row *);
162 static void ovsdb_idl_row_unparse(struct ovsdb_idl_row *);
163 static void ovsdb_idl_row_clear_old(struct ovsdb_idl_row *);
164 static void ovsdb_idl_row_clear_new(struct ovsdb_idl_row *);
166 static void ovsdb_idl_txn_abort_all(struct ovsdb_idl *);
167 static bool ovsdb_idl_txn_process_reply(struct ovsdb_idl *,
168 const struct jsonrpc_msg *msg);
170 static void ovsdb_idl_send_lock_request(struct ovsdb_idl *);
171 static void ovsdb_idl_send_unlock_request(struct ovsdb_idl *);
172 static void ovsdb_idl_parse_lock_reply(struct ovsdb_idl *,
173 const struct json *);
174 static void ovsdb_idl_parse_lock_notify(struct ovsdb_idl *,
175 const struct json *params,
178 /* Creates and returns a connection to database 'remote', which should be in a
179 * form acceptable to jsonrpc_session_open(). The connection will maintain an
180 * in-memory replica of the remote database whose schema is described by
181 * 'class'. (Ordinarily 'class' is compiled from an OVSDB schema automatically
184 * Passes 'retry' to jsonrpc_session_open(). See that function for
187 * If 'monitor_everything_by_default' is true, then everything in the remote
188 * database will be replicated by default. ovsdb_idl_omit() and
189 * ovsdb_idl_omit_alert() may be used to selectively drop some columns from
192 * If 'monitor_everything_by_default' is false, then no columns or tables will
193 * be replicated by default. ovsdb_idl_add_column() and ovsdb_idl_add_table()
194 * must be used to choose some columns or tables to replicate.
197 ovsdb_idl_create(const char *remote, const struct ovsdb_idl_class *class,
198 bool monitor_everything_by_default, bool retry)
200 struct ovsdb_idl *idl;
201 uint8_t default_mode;
204 default_mode = (monitor_everything_by_default
205 ? OVSDB_IDL_MONITOR | OVSDB_IDL_ALERT
208 idl = xzalloc(sizeof *idl);
210 idl->session = jsonrpc_session_open(remote, retry);
211 shash_init(&idl->table_by_name);
212 idl->tables = xmalloc(class->n_tables * sizeof *idl->tables);
213 for (i = 0; i < class->n_tables; i++) {
214 const struct ovsdb_idl_table_class *tc = &class->tables[i];
215 struct ovsdb_idl_table *table = &idl->tables[i];
218 shash_add_assert(&idl->table_by_name, tc->name, table);
220 table->modes = xmalloc(tc->n_columns);
221 memset(table->modes, default_mode, tc->n_columns);
222 table->need_table = false;
223 shash_init(&table->columns);
224 for (j = 0; j < tc->n_columns; j++) {
225 const struct ovsdb_idl_column *column = &tc->columns[j];
227 shash_add_assert(&table->columns, column->name, column);
229 hmap_init(&table->rows);
233 idl->state_seqno = UINT_MAX;
234 idl->request_id = NULL;
236 hmap_init(&idl->outstanding_txns);
241 /* Destroys 'idl' and all of the data structures that it manages. */
243 ovsdb_idl_destroy(struct ovsdb_idl *idl)
248 ovs_assert(!idl->txn);
249 ovsdb_idl_clear(idl);
250 jsonrpc_session_close(idl->session);
252 for (i = 0; i < idl->class->n_tables; i++) {
253 struct ovsdb_idl_table *table = &idl->tables[i];
254 shash_destroy(&table->columns);
255 hmap_destroy(&table->rows);
258 shash_destroy(&idl->table_by_name);
260 json_destroy(idl->request_id);
261 free(idl->lock_name);
262 json_destroy(idl->lock_request_id);
263 hmap_destroy(&idl->outstanding_txns);
269 ovsdb_idl_clear(struct ovsdb_idl *idl)
271 bool changed = false;
274 for (i = 0; i < idl->class->n_tables; i++) {
275 struct ovsdb_idl_table *table = &idl->tables[i];
276 struct ovsdb_idl_row *row, *next_row;
278 if (hmap_is_empty(&table->rows)) {
283 HMAP_FOR_EACH_SAFE (row, next_row, hmap_node, &table->rows) {
284 struct ovsdb_idl_arc *arc, *next_arc;
286 if (!ovsdb_idl_row_is_orphan(row)) {
287 ovsdb_idl_row_unparse(row);
289 LIST_FOR_EACH_SAFE (arc, next_arc, src_node, &row->src_arcs) {
292 /* No need to do anything with dst_arcs: some node has those arcs
293 * as forward arcs and will destroy them itself. */
295 ovsdb_idl_row_destroy(row);
304 /* Processes a batch of messages from the database server on 'idl'. This may
305 * cause the IDL's contents to change. The client may check for that with
306 * ovsdb_idl_get_seqno(). */
308 ovsdb_idl_run(struct ovsdb_idl *idl)
312 ovs_assert(!idl->txn);
313 jsonrpc_session_run(idl->session);
314 for (i = 0; jsonrpc_session_is_connected(idl->session) && i < 50; i++) {
315 struct jsonrpc_msg *msg;
318 seqno = jsonrpc_session_get_seqno(idl->session);
319 if (idl->state_seqno != seqno) {
320 idl->state_seqno = seqno;
321 json_destroy(idl->request_id);
322 idl->request_id = NULL;
323 ovsdb_idl_txn_abort_all(idl);
325 ovsdb_idl_send_schema_request(idl);
326 idl->state = IDL_S_SCHEMA_REQUESTED;
327 if (idl->lock_name) {
328 ovsdb_idl_send_lock_request(idl);
332 msg = jsonrpc_session_recv(idl->session);
337 if (msg->type == JSONRPC_NOTIFY
338 && !strcmp(msg->method, "update")
339 && msg->params->type == JSON_ARRAY
340 && msg->params->u.array.n == 2
341 && msg->params->u.array.elems[0]->type == JSON_NULL) {
342 /* Database contents changed. */
343 ovsdb_idl_parse_update(idl, msg->params->u.array.elems[1]);
344 } else if (msg->type == JSONRPC_REPLY
346 && json_equal(idl->request_id, msg->id)) {
347 switch (idl->state) {
348 case IDL_S_SCHEMA_REQUESTED:
349 /* Reply to our "get_schema" request. */
350 json_destroy(idl->request_id);
351 idl->request_id = NULL;
352 ovsdb_idl_send_monitor_request(idl, msg->result);
353 idl->state = IDL_S_MONITOR_REQUESTED;
356 case IDL_S_MONITOR_REQUESTED:
357 /* Reply to our "monitor" request. */
359 json_destroy(idl->request_id);
360 idl->request_id = NULL;
361 idl->state = IDL_S_MONITORING;
362 ovsdb_idl_clear(idl);
363 ovsdb_idl_parse_update(idl, msg->result);
366 case IDL_S_MONITORING:
370 } else if (msg->type == JSONRPC_REPLY
371 && idl->lock_request_id
372 && json_equal(idl->lock_request_id, msg->id)) {
373 /* Reply to our "lock" request. */
374 ovsdb_idl_parse_lock_reply(idl, msg->result);
375 } else if (msg->type == JSONRPC_NOTIFY
376 && !strcmp(msg->method, "locked")) {
377 /* We got our lock. */
378 ovsdb_idl_parse_lock_notify(idl, msg->params, true);
379 } else if (msg->type == JSONRPC_NOTIFY
380 && !strcmp(msg->method, "stolen")) {
381 /* Someone else stole our lock. */
382 ovsdb_idl_parse_lock_notify(idl, msg->params, false);
383 } else if ((msg->type == JSONRPC_ERROR
384 || msg->type == JSONRPC_REPLY)
385 && ovsdb_idl_txn_process_reply(idl, msg)) {
386 /* ovsdb_idl_txn_process_reply() did everything needful. */
388 /* This can happen if ovsdb_idl_txn_destroy() is called to destroy
389 * a transaction before we receive the reply, so keep the log level
391 VLOG_DBG("%s: received unexpected %s message",
392 jsonrpc_session_get_name(idl->session),
393 jsonrpc_msg_type_to_string(msg->type));
395 jsonrpc_msg_destroy(msg);
399 /* Arranges for poll_block() to wake up when ovsdb_idl_run() has something to
400 * do or when activity occurs on a transaction on 'idl'. */
402 ovsdb_idl_wait(struct ovsdb_idl *idl)
404 jsonrpc_session_wait(idl->session);
405 jsonrpc_session_recv_wait(idl->session);
408 /* Returns a "sequence number" that represents the state of 'idl'. When
409 * ovsdb_idl_run() changes the database, the sequence number changes. The
410 * initial fetch of the entire contents of the remote database is considered to
411 * be one kind of change. Successfully acquiring a lock, if one has been
412 * configured with ovsdb_idl_set_lock(), is also considered to be a change.
414 * As long as the sequence number does not change, the client may continue to
415 * use any data structures it obtains from 'idl'. But when it changes, the
416 * client must not access any of these data structures again, because they
417 * could have freed or reused for other purposes.
419 * The sequence number can occasionally change even if the database does not.
420 * This happens if the connection to the database drops and reconnects, which
421 * causes the database contents to be reloaded even if they didn't change. (It
422 * could also happen if the database server sends out a "change" that reflects
423 * what the IDL already thought was in the database. The database server is
424 * not supposed to do that, but bugs could in theory cause it to do so.) */
426 ovsdb_idl_get_seqno(const struct ovsdb_idl *idl)
428 return idl->change_seqno;
431 /* Returns true if 'idl' successfully connected to the remote database and
432 * retrieved its contents (even if the connection subsequently dropped and is
433 * in the process of reconnecting). If so, then 'idl' contains an atomic
434 * snapshot of the database's contents (but it might be arbitrarily old if the
435 * connection dropped).
437 * Returns false if 'idl' has never connected or retrieved the database's
438 * contents. If so, 'idl' is empty. */
440 ovsdb_idl_has_ever_connected(const struct ovsdb_idl *idl)
442 return ovsdb_idl_get_seqno(idl) != 0;
445 /* Reconfigures 'idl' so that it would reconnect to the database, if
446 * connection was dropped. */
448 ovsdb_idl_enable_reconnect(struct ovsdb_idl *idl)
450 jsonrpc_session_enable_reconnect(idl->session);
453 /* Forces 'idl' to drop its connection to the database and reconnect. In the
454 * meantime, the contents of 'idl' will not change. */
456 ovsdb_idl_force_reconnect(struct ovsdb_idl *idl)
458 jsonrpc_session_force_reconnect(idl->session);
461 /* Some IDL users should only write to write-only columns. Furthermore,
462 * writing to a column which is not write-only can cause serious performance
463 * degradations for these users. This function causes 'idl' to reject writes
464 * to columns which are not marked write only using ovsdb_idl_omit_alert(). */
466 ovsdb_idl_verify_write_only(struct ovsdb_idl *idl)
468 idl->verify_write_only = true;
471 /* Returns true if 'idl' is currently connected or trying to connect. */
473 ovsdb_idl_is_alive(const struct ovsdb_idl *idl)
475 return jsonrpc_session_is_alive(idl->session);
478 /* Returns the last error reported on a connection by 'idl'. The return value
479 * is 0 only if no connection made by 'idl' has ever encountered an error. See
480 * jsonrpc_get_status() for return value interpretation. */
482 ovsdb_idl_get_last_error(const struct ovsdb_idl *idl)
484 return jsonrpc_session_get_last_error(idl->session);
487 static unsigned char *
488 ovsdb_idl_get_mode(struct ovsdb_idl *idl,
489 const struct ovsdb_idl_column *column)
493 ovs_assert(!idl->change_seqno);
495 for (i = 0; i < idl->class->n_tables; i++) {
496 const struct ovsdb_idl_table *table = &idl->tables[i];
497 const struct ovsdb_idl_table_class *tc = table->class;
499 if (column >= tc->columns && column < &tc->columns[tc->n_columns]) {
500 return &table->modes[column - tc->columns];
508 add_ref_table(struct ovsdb_idl *idl, const struct ovsdb_base_type *base)
510 if (base->type == OVSDB_TYPE_UUID && base->u.uuid.refTableName) {
511 struct ovsdb_idl_table *table;
513 table = shash_find_data(&idl->table_by_name,
514 base->u.uuid.refTableName);
516 table->need_table = true;
518 VLOG_WARN("%s IDL class missing referenced table %s",
519 idl->class->database, base->u.uuid.refTableName);
524 /* Turns on OVSDB_IDL_MONITOR and OVSDB_IDL_ALERT for 'column' in 'idl'. Also
525 * ensures that any tables referenced by 'column' will be replicated, even if
526 * no columns in that table are selected for replication (see
527 * ovsdb_idl_add_table() for more information).
529 * This function is only useful if 'monitor_everything_by_default' was false in
530 * the call to ovsdb_idl_create(). This function should be called between
531 * ovsdb_idl_create() and the first call to ovsdb_idl_run().
534 ovsdb_idl_add_column(struct ovsdb_idl *idl,
535 const struct ovsdb_idl_column *column)
537 *ovsdb_idl_get_mode(idl, column) = OVSDB_IDL_MONITOR | OVSDB_IDL_ALERT;
538 add_ref_table(idl, &column->type.key);
539 add_ref_table(idl, &column->type.value);
542 /* Ensures that the table with class 'tc' will be replicated on 'idl' even if
543 * no columns are selected for replication. This can be useful because it
544 * allows 'idl' to keep track of what rows in the table actually exist, which
545 * in turn allows columns that reference the table to have accurate contents.
546 * (The IDL presents the database with references to rows that do not exist
549 * This function is only useful if 'monitor_everything_by_default' was false in
550 * the call to ovsdb_idl_create(). This function should be called between
551 * ovsdb_idl_create() and the first call to ovsdb_idl_run().
554 ovsdb_idl_add_table(struct ovsdb_idl *idl,
555 const struct ovsdb_idl_table_class *tc)
559 for (i = 0; i < idl->class->n_tables; i++) {
560 struct ovsdb_idl_table *table = &idl->tables[i];
562 if (table->class == tc) {
563 table->need_table = true;
571 /* Turns off OVSDB_IDL_ALERT for 'column' in 'idl'.
573 * This function should be called between ovsdb_idl_create() and the first call
574 * to ovsdb_idl_run().
577 ovsdb_idl_omit_alert(struct ovsdb_idl *idl,
578 const struct ovsdb_idl_column *column)
580 *ovsdb_idl_get_mode(idl, column) &= ~OVSDB_IDL_ALERT;
583 /* Sets the mode for 'column' in 'idl' to 0. See the big comment above
584 * OVSDB_IDL_MONITOR for details.
586 * This function should be called between ovsdb_idl_create() and the first call
587 * to ovsdb_idl_run().
590 ovsdb_idl_omit(struct ovsdb_idl *idl, const struct ovsdb_idl_column *column)
592 *ovsdb_idl_get_mode(idl, column) = 0;
596 ovsdb_idl_send_schema_request(struct ovsdb_idl *idl)
598 struct jsonrpc_msg *msg;
600 json_destroy(idl->request_id);
601 msg = jsonrpc_create_request(
603 json_array_create_1(json_string_create(idl->class->database)),
605 jsonrpc_session_send(idl->session, msg);
609 log_error(struct ovsdb_error *error)
611 char *s = ovsdb_error_to_string(error);
612 VLOG_WARN("error parsing database schema: %s", s);
614 ovsdb_error_destroy(error);
617 /* Frees 'schema', which is in the format returned by parse_schema(). */
619 free_schema(struct shash *schema)
622 struct shash_node *node, *next;
624 SHASH_FOR_EACH_SAFE (node, next, schema) {
625 struct sset *sset = node->data;
628 shash_delete(schema, node);
630 shash_destroy(schema);
635 /* Parses 'schema_json', an OVSDB schema in JSON format as described in RFC
636 * 7047, to obtain the names of its rows and columns. If successful, returns
637 * an shash whose keys are table names and whose values are ssets, where each
638 * sset contains the names of its table's columns. On failure (due to a parse
639 * error), returns NULL.
641 * It would also be possible to use the general-purpose OVSDB schema parser in
642 * ovsdb-server, but that's overkill, possibly too strict for the current use
643 * case, and would require restructuring ovsdb-server to separate the schema
644 * code from the rest. */
645 static struct shash *
646 parse_schema(const struct json *schema_json)
648 struct ovsdb_parser parser;
649 const struct json *tables_json;
650 struct ovsdb_error *error;
651 struct shash_node *node;
652 struct shash *schema;
654 ovsdb_parser_init(&parser, schema_json, "database schema");
655 tables_json = ovsdb_parser_member(&parser, "tables", OP_OBJECT);
656 error = ovsdb_parser_destroy(&parser);
662 schema = xmalloc(sizeof *schema);
664 SHASH_FOR_EACH (node, json_object(tables_json)) {
665 const char *table_name = node->name;
666 const struct json *json = node->data;
667 const struct json *columns_json;
669 ovsdb_parser_init(&parser, json, "table schema for table %s",
671 columns_json = ovsdb_parser_member(&parser, "columns", OP_OBJECT);
672 error = ovsdb_parser_destroy(&parser);
679 struct sset *columns = xmalloc(sizeof *columns);
682 struct shash_node *node2;
683 SHASH_FOR_EACH (node2, json_object(columns_json)) {
684 const char *column_name = node2->name;
685 sset_add(columns, column_name);
687 shash_add(schema, table_name, columns);
693 ovsdb_idl_send_monitor_request(struct ovsdb_idl *idl,
694 const struct json *schema_json)
696 struct shash *schema = parse_schema(schema_json);
697 struct json *monitor_requests;
698 struct jsonrpc_msg *msg;
701 monitor_requests = json_object_create();
702 for (i = 0; i < idl->class->n_tables; i++) {
703 const struct ovsdb_idl_table *table = &idl->tables[i];
704 const struct ovsdb_idl_table_class *tc = table->class;
705 struct json *monitor_request, *columns;
706 const struct sset *table_schema;
709 table_schema = (schema
710 ? shash_find_data(schema, table->class->name)
713 columns = table->need_table ? json_array_create_empty() : NULL;
714 for (j = 0; j < tc->n_columns; j++) {
715 const struct ovsdb_idl_column *column = &tc->columns[j];
716 if (table->modes[j] & OVSDB_IDL_MONITOR) {
718 && !sset_contains(table_schema, column->name)) {
719 VLOG_WARN("%s table in %s database lacks %s column "
720 "(database needs upgrade?)",
721 table->class->name, idl->class->database,
726 columns = json_array_create_empty();
728 json_array_add(columns, json_string_create(column->name));
733 if (schema && !table_schema) {
734 VLOG_WARN("%s database lacks %s table "
735 "(database needs upgrade?)",
736 idl->class->database, table->class->name);
737 json_destroy(columns);
741 monitor_request = json_object_create();
742 json_object_put(monitor_request, "columns", columns);
743 json_object_put(monitor_requests, tc->name, monitor_request);
748 json_destroy(idl->request_id);
749 msg = jsonrpc_create_request(
751 json_array_create_3(json_string_create(idl->class->database),
752 json_null_create(), monitor_requests),
754 jsonrpc_session_send(idl->session, msg);
758 ovsdb_idl_parse_update(struct ovsdb_idl *idl, const struct json *table_updates)
760 struct ovsdb_error *error = ovsdb_idl_parse_update__(idl, table_updates);
762 if (!VLOG_DROP_WARN(&syntax_rl)) {
763 char *s = ovsdb_error_to_string(error);
764 VLOG_WARN_RL(&syntax_rl, "%s", s);
767 ovsdb_error_destroy(error);
771 static struct ovsdb_error *
772 ovsdb_idl_parse_update__(struct ovsdb_idl *idl,
773 const struct json *table_updates)
775 const struct shash_node *tables_node;
777 if (table_updates->type != JSON_OBJECT) {
778 return ovsdb_syntax_error(table_updates, NULL,
779 "<table-updates> is not an object");
781 SHASH_FOR_EACH (tables_node, json_object(table_updates)) {
782 const struct json *table_update = tables_node->data;
783 const struct shash_node *table_node;
784 struct ovsdb_idl_table *table;
786 table = shash_find_data(&idl->table_by_name, tables_node->name);
788 return ovsdb_syntax_error(
790 "<table-updates> includes unknown table \"%s\"",
794 if (table_update->type != JSON_OBJECT) {
795 return ovsdb_syntax_error(table_update, NULL,
796 "<table-update> for table \"%s\" is "
797 "not an object", table->class->name);
799 SHASH_FOR_EACH (table_node, json_object(table_update)) {
800 const struct json *row_update = table_node->data;
801 const struct json *old_json, *new_json;
804 if (!uuid_from_string(&uuid, table_node->name)) {
805 return ovsdb_syntax_error(table_update, NULL,
806 "<table-update> for table \"%s\" "
808 "\"%s\" as member name",
812 if (row_update->type != JSON_OBJECT) {
813 return ovsdb_syntax_error(row_update, NULL,
814 "<table-update> for table \"%s\" "
815 "contains <row-update> for %s that "
821 old_json = shash_find_data(json_object(row_update), "old");
822 new_json = shash_find_data(json_object(row_update), "new");
823 if (old_json && old_json->type != JSON_OBJECT) {
824 return ovsdb_syntax_error(old_json, NULL,
825 "\"old\" <row> is not object");
826 } else if (new_json && new_json->type != JSON_OBJECT) {
827 return ovsdb_syntax_error(new_json, NULL,
828 "\"new\" <row> is not object");
829 } else if ((old_json != NULL) + (new_json != NULL)
830 != shash_count(json_object(row_update))) {
831 return ovsdb_syntax_error(row_update, NULL,
832 "<row-update> contains unexpected "
834 } else if (!old_json && !new_json) {
835 return ovsdb_syntax_error(row_update, NULL,
836 "<row-update> missing \"old\" "
837 "and \"new\" members");
840 if (ovsdb_idl_process_update(table, &uuid, old_json, new_json)) {
849 static struct ovsdb_idl_row *
850 ovsdb_idl_get_row(struct ovsdb_idl_table *table, const struct uuid *uuid)
852 struct ovsdb_idl_row *row;
854 HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid), &table->rows) {
855 if (uuid_equals(&row->uuid, uuid)) {
862 /* Returns true if a column with mode OVSDB_IDL_MODE_RW changed, false
865 ovsdb_idl_process_update(struct ovsdb_idl_table *table,
866 const struct uuid *uuid, const struct json *old,
867 const struct json *new)
869 struct ovsdb_idl_row *row;
871 row = ovsdb_idl_get_row(table, uuid);
874 if (row && !ovsdb_idl_row_is_orphan(row)) {
875 /* XXX perhaps we should check the 'old' values? */
876 ovsdb_idl_delete_row(row);
878 VLOG_WARN_RL(&semantic_rl, "cannot delete missing row "UUID_FMT" "
880 UUID_ARGS(uuid), table->class->name);
886 ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
887 } else if (ovsdb_idl_row_is_orphan(row)) {
888 ovsdb_idl_insert_row(row, new);
890 VLOG_WARN_RL(&semantic_rl, "cannot add existing row "UUID_FMT" to "
891 "table %s", UUID_ARGS(uuid), table->class->name);
892 return ovsdb_idl_modify_row(row, new);
897 /* XXX perhaps we should check the 'old' values? */
898 if (!ovsdb_idl_row_is_orphan(row)) {
899 return ovsdb_idl_modify_row(row, new);
901 VLOG_WARN_RL(&semantic_rl, "cannot modify missing but "
902 "referenced row "UUID_FMT" in table %s",
903 UUID_ARGS(uuid), table->class->name);
904 ovsdb_idl_insert_row(row, new);
907 VLOG_WARN_RL(&semantic_rl, "cannot modify missing row "UUID_FMT" "
908 "in table %s", UUID_ARGS(uuid), table->class->name);
909 ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
916 /* Returns true if a column with mode OVSDB_IDL_MODE_RW changed, false
919 ovsdb_idl_row_update(struct ovsdb_idl_row *row, const struct json *row_json)
921 struct ovsdb_idl_table *table = row->table;
922 struct shash_node *node;
923 bool changed = false;
925 SHASH_FOR_EACH (node, json_object(row_json)) {
926 const char *column_name = node->name;
927 const struct ovsdb_idl_column *column;
928 struct ovsdb_datum datum;
929 struct ovsdb_error *error;
931 column = shash_find_data(&table->columns, column_name);
933 VLOG_WARN_RL(&syntax_rl, "unknown column %s updating row "UUID_FMT,
934 column_name, UUID_ARGS(&row->uuid));
938 error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
940 unsigned int column_idx = column - table->class->columns;
941 struct ovsdb_datum *old = &row->old[column_idx];
943 if (!ovsdb_datum_equals(old, &datum, &column->type)) {
944 ovsdb_datum_swap(old, &datum);
945 if (table->modes[column_idx] & OVSDB_IDL_ALERT) {
949 /* Didn't really change but the OVSDB monitor protocol always
950 * includes every value in a row. */
953 ovsdb_datum_destroy(&datum, &column->type);
955 char *s = ovsdb_error_to_string(error);
956 VLOG_WARN_RL(&syntax_rl, "error parsing column %s in row "UUID_FMT
957 " in table %s: %s", column_name,
958 UUID_ARGS(&row->uuid), table->class->name, s);
960 ovsdb_error_destroy(error);
966 /* When a row A refers to row B through a column with a "refTable" constraint,
967 * but row B does not exist, row B is called an "orphan row". Orphan rows
968 * should not persist, because the database enforces referential integrity, but
969 * they can appear transiently as changes from the database are received (the
970 * database doesn't try to topologically sort them and circular references mean
971 * it isn't always possible anyhow).
973 * This function returns true if 'row' is an orphan row, otherwise false.
976 ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *row)
978 return !row->old && !row->new;
981 /* Returns true if 'row' is conceptually part of the database as modified by
982 * the current transaction (if any), false otherwise.
984 * This function will return true if 'row' is not an orphan (see the comment on
985 * ovsdb_idl_row_is_orphan()) and:
987 * - 'row' exists in the database and has not been deleted within the
988 * current transaction (if any).
990 * - 'row' was inserted within the current transaction and has not been
991 * deleted. (In the latter case you should not have passed 'row' in at
992 * all, because ovsdb_idl_txn_delete() freed it.)
994 * This function will return false if 'row' is an orphan or if 'row' was
995 * deleted within the current transaction.
998 ovsdb_idl_row_exists(const struct ovsdb_idl_row *row)
1000 return row->new != NULL;
1004 ovsdb_idl_row_parse(struct ovsdb_idl_row *row)
1006 const struct ovsdb_idl_table_class *class = row->table->class;
1009 for (i = 0; i < class->n_columns; i++) {
1010 const struct ovsdb_idl_column *c = &class->columns[i];
1011 (c->parse)(row, &row->old[i]);
1016 ovsdb_idl_row_unparse(struct ovsdb_idl_row *row)
1018 const struct ovsdb_idl_table_class *class = row->table->class;
1021 for (i = 0; i < class->n_columns; i++) {
1022 const struct ovsdb_idl_column *c = &class->columns[i];
1028 ovsdb_idl_row_clear_old(struct ovsdb_idl_row *row)
1030 ovs_assert(row->old == row->new);
1031 if (!ovsdb_idl_row_is_orphan(row)) {
1032 const struct ovsdb_idl_table_class *class = row->table->class;
1035 for (i = 0; i < class->n_columns; i++) {
1036 ovsdb_datum_destroy(&row->old[i], &class->columns[i].type);
1039 row->old = row->new = NULL;
1044 ovsdb_idl_row_clear_new(struct ovsdb_idl_row *row)
1046 if (row->old != row->new) {
1048 const struct ovsdb_idl_table_class *class = row->table->class;
1052 BITMAP_FOR_EACH_1 (i, class->n_columns, row->written) {
1053 ovsdb_datum_destroy(&row->new[i], &class->columns[i].type);
1058 row->written = NULL;
1060 row->new = row->old;
1065 ovsdb_idl_row_clear_arcs(struct ovsdb_idl_row *row, bool destroy_dsts)
1067 struct ovsdb_idl_arc *arc, *next;
1069 /* Delete all forward arcs. If 'destroy_dsts', destroy any orphaned rows
1070 * that this causes to be unreferenced. */
1071 LIST_FOR_EACH_SAFE (arc, next, src_node, &row->src_arcs) {
1072 list_remove(&arc->dst_node);
1074 && ovsdb_idl_row_is_orphan(arc->dst)
1075 && list_is_empty(&arc->dst->dst_arcs)) {
1076 ovsdb_idl_row_destroy(arc->dst);
1080 list_init(&row->src_arcs);
1083 /* Force nodes that reference 'row' to reparse. */
1085 ovsdb_idl_row_reparse_backrefs(struct ovsdb_idl_row *row)
1087 struct ovsdb_idl_arc *arc, *next;
1089 /* This is trickier than it looks. ovsdb_idl_row_clear_arcs() will destroy
1090 * 'arc', so we need to use the "safe" variant of list traversal. However,
1091 * calling an ovsdb_idl_column's 'parse' function will add an arc
1092 * equivalent to 'arc' to row->arcs. That could be a problem for
1093 * traversal, but it adds it at the beginning of the list to prevent us
1094 * from stumbling upon it again.
1096 * (If duplicate arcs were possible then we would need to make sure that
1097 * 'next' didn't also point into 'arc''s destination, but we forbid
1098 * duplicate arcs.) */
1099 LIST_FOR_EACH_SAFE (arc, next, dst_node, &row->dst_arcs) {
1100 struct ovsdb_idl_row *ref = arc->src;
1102 ovsdb_idl_row_unparse(ref);
1103 ovsdb_idl_row_clear_arcs(ref, false);
1104 ovsdb_idl_row_parse(ref);
1108 static struct ovsdb_idl_row *
1109 ovsdb_idl_row_create__(const struct ovsdb_idl_table_class *class)
1111 struct ovsdb_idl_row *row = xzalloc(class->allocation_size);
1112 class->row_init(row);
1113 list_init(&row->src_arcs);
1114 list_init(&row->dst_arcs);
1115 hmap_node_nullify(&row->txn_node);
1119 static struct ovsdb_idl_row *
1120 ovsdb_idl_row_create(struct ovsdb_idl_table *table, const struct uuid *uuid)
1122 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(table->class);
1123 hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
1130 ovsdb_idl_row_destroy(struct ovsdb_idl_row *row)
1133 ovsdb_idl_row_clear_old(row);
1134 hmap_remove(&row->table->rows, &row->hmap_node);
1140 ovsdb_idl_insert_row(struct ovsdb_idl_row *row, const struct json *row_json)
1142 const struct ovsdb_idl_table_class *class = row->table->class;
1145 ovs_assert(!row->old && !row->new);
1146 row->old = row->new = xmalloc(class->n_columns * sizeof *row->old);
1147 for (i = 0; i < class->n_columns; i++) {
1148 ovsdb_datum_init_default(&row->old[i], &class->columns[i].type);
1150 ovsdb_idl_row_update(row, row_json);
1151 ovsdb_idl_row_parse(row);
1153 ovsdb_idl_row_reparse_backrefs(row);
1157 ovsdb_idl_delete_row(struct ovsdb_idl_row *row)
1159 ovsdb_idl_row_unparse(row);
1160 ovsdb_idl_row_clear_arcs(row, true);
1161 ovsdb_idl_row_clear_old(row);
1162 if (list_is_empty(&row->dst_arcs)) {
1163 ovsdb_idl_row_destroy(row);
1165 ovsdb_idl_row_reparse_backrefs(row);
1169 /* Returns true if a column with mode OVSDB_IDL_MODE_RW changed, false
1172 ovsdb_idl_modify_row(struct ovsdb_idl_row *row, const struct json *row_json)
1176 ovsdb_idl_row_unparse(row);
1177 ovsdb_idl_row_clear_arcs(row, true);
1178 changed = ovsdb_idl_row_update(row, row_json);
1179 ovsdb_idl_row_parse(row);
1185 may_add_arc(const struct ovsdb_idl_row *src, const struct ovsdb_idl_row *dst)
1187 const struct ovsdb_idl_arc *arc;
1194 /* No duplicate arcs.
1196 * We only need to test whether the first arc in dst->dst_arcs originates
1197 * at 'src', since we add all of the arcs from a given source in a clump
1198 * (in a single call to ovsdb_idl_row_parse()) and new arcs are always
1199 * added at the front of the dst_arcs list. */
1200 if (list_is_empty(&dst->dst_arcs)) {
1203 arc = CONTAINER_OF(dst->dst_arcs.next, struct ovsdb_idl_arc, dst_node);
1204 return arc->src != src;
1207 static struct ovsdb_idl_table *
1208 ovsdb_idl_table_from_class(const struct ovsdb_idl *idl,
1209 const struct ovsdb_idl_table_class *table_class)
1211 return &idl->tables[table_class - idl->class->tables];
1214 /* Called by ovsdb-idlc generated code. */
1215 struct ovsdb_idl_row *
1216 ovsdb_idl_get_row_arc(struct ovsdb_idl_row *src,
1217 struct ovsdb_idl_table_class *dst_table_class,
1218 const struct uuid *dst_uuid)
1220 struct ovsdb_idl *idl = src->table->idl;
1221 struct ovsdb_idl_table *dst_table;
1222 struct ovsdb_idl_arc *arc;
1223 struct ovsdb_idl_row *dst;
1225 dst_table = ovsdb_idl_table_from_class(idl, dst_table_class);
1226 dst = ovsdb_idl_get_row(dst_table, dst_uuid);
1228 /* We're being called from ovsdb_idl_txn_write(). We must not update
1229 * any arcs, because the transaction will be backed out at commit or
1230 * abort time and we don't want our graph screwed up.
1232 * Just return the destination row, if there is one and it has not been
1234 if (dst && (hmap_node_is_null(&dst->txn_node) || dst->new)) {
1239 /* We're being called from some other context. Update the graph. */
1241 dst = ovsdb_idl_row_create(dst_table, dst_uuid);
1244 /* Add a new arc, if it wouldn't be a self-arc or a duplicate arc. */
1245 if (may_add_arc(src, dst)) {
1246 /* The arc *must* be added at the front of the dst_arcs list. See
1247 * ovsdb_idl_row_reparse_backrefs() for details. */
1248 arc = xmalloc(sizeof *arc);
1249 list_push_front(&src->src_arcs, &arc->src_node);
1250 list_push_front(&dst->dst_arcs, &arc->dst_node);
1255 return !ovsdb_idl_row_is_orphan(dst) ? dst : NULL;
1259 /* Searches 'tc''s table in 'idl' for a row with UUID 'uuid'. Returns a
1260 * pointer to the row if there is one, otherwise a null pointer. */
1261 const struct ovsdb_idl_row *
1262 ovsdb_idl_get_row_for_uuid(const struct ovsdb_idl *idl,
1263 const struct ovsdb_idl_table_class *tc,
1264 const struct uuid *uuid)
1266 return ovsdb_idl_get_row(ovsdb_idl_table_from_class(idl, tc), uuid);
1269 static struct ovsdb_idl_row *
1270 next_real_row(struct ovsdb_idl_table *table, struct hmap_node *node)
1272 for (; node; node = hmap_next(&table->rows, node)) {
1273 struct ovsdb_idl_row *row;
1275 row = CONTAINER_OF(node, struct ovsdb_idl_row, hmap_node);
1276 if (ovsdb_idl_row_exists(row)) {
1283 /* Returns a row in 'table_class''s table in 'idl', or a null pointer if that
1286 * Database tables are internally maintained as hash tables, so adding or
1287 * removing rows while traversing the same table can cause some rows to be
1288 * visited twice or not at apply. */
1289 const struct ovsdb_idl_row *
1290 ovsdb_idl_first_row(const struct ovsdb_idl *idl,
1291 const struct ovsdb_idl_table_class *table_class)
1293 struct ovsdb_idl_table *table
1294 = ovsdb_idl_table_from_class(idl, table_class);
1295 return next_real_row(table, hmap_first(&table->rows));
1298 /* Returns a row following 'row' within its table, or a null pointer if 'row'
1299 * is the last row in its table. */
1300 const struct ovsdb_idl_row *
1301 ovsdb_idl_next_row(const struct ovsdb_idl_row *row)
1303 struct ovsdb_idl_table *table = row->table;
1305 return next_real_row(table, hmap_next(&table->rows, &row->hmap_node));
1308 /* Reads and returns the value of 'column' within 'row'. If an ongoing
1309 * transaction has changed 'column''s value, the modified value is returned.
1311 * The caller must not modify or free the returned value.
1313 * Various kinds of changes can invalidate the returned value: writing to the
1314 * same 'column' in 'row' (e.g. with ovsdb_idl_txn_write()), deleting 'row'
1315 * (e.g. with ovsdb_idl_txn_delete()), or completing an ongoing transaction
1316 * (e.g. with ovsdb_idl_txn_commit() or ovsdb_idl_txn_abort()). If the
1317 * returned value is needed for a long time, it is best to make a copy of it
1318 * with ovsdb_datum_clone(). */
1319 const struct ovsdb_datum *
1320 ovsdb_idl_read(const struct ovsdb_idl_row *row,
1321 const struct ovsdb_idl_column *column)
1323 const struct ovsdb_idl_table_class *class;
1326 ovs_assert(!ovsdb_idl_row_is_synthetic(row));
1328 class = row->table->class;
1329 column_idx = column - class->columns;
1331 ovs_assert(row->new != NULL);
1332 ovs_assert(column_idx < class->n_columns);
1334 if (row->written && bitmap_is_set(row->written, column_idx)) {
1335 return &row->new[column_idx];
1336 } else if (row->old) {
1337 return &row->old[column_idx];
1339 return ovsdb_datum_default(&column->type);
1343 /* Same as ovsdb_idl_read(), except that it also asserts that 'column' has key
1344 * type 'key_type' and value type 'value_type'. (Scalar and set types will
1345 * have a value type of OVSDB_TYPE_VOID.)
1347 * This is useful in code that "knows" that a particular column has a given
1348 * type, so that it will abort if someone changes the column's type without
1349 * updating the code that uses it. */
1350 const struct ovsdb_datum *
1351 ovsdb_idl_get(const struct ovsdb_idl_row *row,
1352 const struct ovsdb_idl_column *column,
1353 enum ovsdb_atomic_type key_type OVS_UNUSED,
1354 enum ovsdb_atomic_type value_type OVS_UNUSED)
1356 ovs_assert(column->type.key.type == key_type);
1357 ovs_assert(column->type.value.type == value_type);
1359 return ovsdb_idl_read(row, column);
1362 /* Returns true if the field represented by 'column' in 'row' may be modified,
1363 * false if it is immutable.
1365 * Normally, whether a field is mutable is controlled by its column's schema.
1366 * However, an immutable column can be set to any initial value at the time of
1367 * insertion, so if 'row' is a new row (one that is being added as part of the
1368 * current transaction, supposing that a transaction is in progress) then even
1369 * its "immutable" fields are actually mutable. */
1371 ovsdb_idl_is_mutable(const struct ovsdb_idl_row *row,
1372 const struct ovsdb_idl_column *column)
1374 return column->mutable || (row->new && !row->old);
1377 /* Returns false if 'row' was obtained from the IDL, true if it was initialized
1378 * to all-zero-bits by some other entity. If 'row' was set up some other way
1379 * then the return value is indeterminate. */
1381 ovsdb_idl_row_is_synthetic(const struct ovsdb_idl_row *row)
1383 return row->table == NULL;
1388 static void ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
1389 enum ovsdb_idl_txn_status);
1391 /* Returns a string representation of 'status'. The caller must not modify or
1392 * free the returned string.
1394 * The return value is probably useful only for debug log messages and unit
1397 ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status status)
1400 case TXN_UNCOMMITTED:
1401 return "uncommitted";
1404 case TXN_INCOMPLETE:
1405 return "incomplete";
1412 case TXN_NOT_LOCKED:
1413 return "not locked";
1420 /* Starts a new transaction on 'idl'. A given ovsdb_idl may only have a single
1421 * active transaction at a time. See the large comment in ovsdb-idl.h for
1422 * general information on transactions. */
1423 struct ovsdb_idl_txn *
1424 ovsdb_idl_txn_create(struct ovsdb_idl *idl)
1426 struct ovsdb_idl_txn *txn;
1428 ovs_assert(!idl->txn);
1429 idl->txn = txn = xmalloc(sizeof *txn);
1430 txn->request_id = NULL;
1432 hmap_init(&txn->txn_rows);
1433 txn->status = TXN_UNCOMMITTED;
1435 txn->dry_run = false;
1436 ds_init(&txn->comment);
1438 txn->inc_table = NULL;
1439 txn->inc_column = NULL;
1441 hmap_init(&txn->inserted_rows);
1446 /* Appends 's', which is treated as a printf()-type format string, to the
1447 * comments that will be passed to the OVSDB server when 'txn' is committed.
1448 * (The comment will be committed to the OVSDB log, which "ovsdb-tool
1449 * show-log" can print in a relatively human-readable form.) */
1451 ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *txn, const char *s, ...)
1455 if (txn->comment.length) {
1456 ds_put_char(&txn->comment, '\n');
1460 ds_put_format_valist(&txn->comment, s, args);
1464 /* Marks 'txn' as a transaction that will not actually modify the database. In
1465 * almost every way, the transaction is treated like other transactions. It
1466 * must be committed or aborted like other transactions, it will be sent to the
1467 * database server like other transactions, and so on. The only difference is
1468 * that the operations sent to the database server will include, as the last
1469 * step, an "abort" operation, so that any changes made by the transaction will
1470 * not actually take effect. */
1472 ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *txn)
1474 txn->dry_run = true;
1477 /* Causes 'txn', when committed, to increment the value of 'column' within
1478 * 'row' by 1. 'column' must have an integer type. After 'txn' commits
1479 * successfully, the client may retrieve the final (incremented) value of
1480 * 'column' with ovsdb_idl_txn_get_increment_new_value().
1482 * The client could accomplish something similar with ovsdb_idl_read(),
1483 * ovsdb_idl_txn_verify() and ovsdb_idl_txn_write(), or with ovsdb-idlc
1484 * generated wrappers for these functions. However, ovsdb_idl_txn_increment()
1485 * will never (by itself) fail because of a verify error.
1487 * The intended use is for incrementing the "next_cfg" column in the
1488 * Open_vSwitch table. */
1490 ovsdb_idl_txn_increment(struct ovsdb_idl_txn *txn,
1491 const struct ovsdb_idl_row *row,
1492 const struct ovsdb_idl_column *column)
1494 ovs_assert(!txn->inc_table);
1495 ovs_assert(column->type.key.type == OVSDB_TYPE_INTEGER);
1496 ovs_assert(column->type.value.type == OVSDB_TYPE_VOID);
1498 txn->inc_table = row->table->class->name;
1499 txn->inc_column = column->name;
1500 txn->inc_row = row->uuid;
1503 /* Destroys 'txn' and frees all associated memory. If ovsdb_idl_txn_commit()
1504 * has been called for 'txn' but the commit is still incomplete (that is, the
1505 * last call returned TXN_INCOMPLETE) then the transaction may or may not still
1506 * end up committing at the database server, but the client will not be able to
1507 * get any further status information back. */
1509 ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *txn)
1511 struct ovsdb_idl_txn_insert *insert, *next;
1513 json_destroy(txn->request_id);
1514 if (txn->status == TXN_INCOMPLETE) {
1515 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
1517 ovsdb_idl_txn_abort(txn);
1518 ds_destroy(&txn->comment);
1520 HMAP_FOR_EACH_SAFE (insert, next, hmap_node, &txn->inserted_rows) {
1523 hmap_destroy(&txn->inserted_rows);
1527 /* Causes poll_block() to wake up if 'txn' has completed committing. */
1529 ovsdb_idl_txn_wait(const struct ovsdb_idl_txn *txn)
1531 if (txn->status != TXN_UNCOMMITTED && txn->status != TXN_INCOMPLETE) {
1532 poll_immediate_wake();
1536 static struct json *
1537 where_uuid_equals(const struct uuid *uuid)
1540 json_array_create_1(
1541 json_array_create_3(
1542 json_string_create("_uuid"),
1543 json_string_create("=="),
1544 json_array_create_2(
1545 json_string_create("uuid"),
1546 json_string_create_nocopy(
1547 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
1551 uuid_name_from_uuid(const struct uuid *uuid)
1556 name = xasprintf("row"UUID_FMT, UUID_ARGS(uuid));
1557 for (p = name; *p != '\0'; p++) {
1566 static const struct ovsdb_idl_row *
1567 ovsdb_idl_txn_get_row(const struct ovsdb_idl_txn *txn, const struct uuid *uuid)
1569 const struct ovsdb_idl_row *row;
1571 HMAP_FOR_EACH_WITH_HASH (row, txn_node, uuid_hash(uuid), &txn->txn_rows) {
1572 if (uuid_equals(&row->uuid, uuid)) {
1579 /* XXX there must be a cleaner way to do this */
1580 static struct json *
1581 substitute_uuids(struct json *json, const struct ovsdb_idl_txn *txn)
1583 if (json->type == JSON_ARRAY) {
1587 if (json->u.array.n == 2
1588 && json->u.array.elems[0]->type == JSON_STRING
1589 && json->u.array.elems[1]->type == JSON_STRING
1590 && !strcmp(json->u.array.elems[0]->u.string, "uuid")
1591 && uuid_from_string(&uuid, json->u.array.elems[1]->u.string)) {
1592 const struct ovsdb_idl_row *row;
1594 row = ovsdb_idl_txn_get_row(txn, &uuid);
1595 if (row && !row->old && row->new) {
1598 return json_array_create_2(
1599 json_string_create("named-uuid"),
1600 json_string_create_nocopy(uuid_name_from_uuid(&uuid)));
1604 for (i = 0; i < json->u.array.n; i++) {
1605 json->u.array.elems[i] = substitute_uuids(json->u.array.elems[i],
1608 } else if (json->type == JSON_OBJECT) {
1609 struct shash_node *node;
1611 SHASH_FOR_EACH (node, json_object(json)) {
1612 node->data = substitute_uuids(node->data, txn);
1619 ovsdb_idl_txn_disassemble(struct ovsdb_idl_txn *txn)
1621 struct ovsdb_idl_row *row, *next;
1623 /* This must happen early. Otherwise, ovsdb_idl_row_parse() will call an
1624 * ovsdb_idl_column's 'parse' function, which will call
1625 * ovsdb_idl_get_row_arc(), which will seen that the IDL is in a
1626 * transaction and fail to update the graph. */
1627 txn->idl->txn = NULL;
1629 HMAP_FOR_EACH_SAFE (row, next, txn_node, &txn->txn_rows) {
1632 ovsdb_idl_row_unparse(row);
1633 ovsdb_idl_row_clear_arcs(row, false);
1634 ovsdb_idl_row_parse(row);
1637 ovsdb_idl_row_unparse(row);
1639 ovsdb_idl_row_clear_new(row);
1642 row->prereqs = NULL;
1645 row->written = NULL;
1647 hmap_remove(&txn->txn_rows, &row->txn_node);
1648 hmap_node_nullify(&row->txn_node);
1650 hmap_remove(&row->table->rows, &row->hmap_node);
1654 hmap_destroy(&txn->txn_rows);
1655 hmap_init(&txn->txn_rows);
1658 /* Attempts to commit 'txn'. Returns the status of the commit operation, one
1659 * of the following TXN_* constants:
1663 * The transaction is in progress, but not yet complete. The caller
1664 * should call again later, after calling ovsdb_idl_run() to let the IDL
1665 * do OVSDB protocol processing.
1669 * The transaction is complete. (It didn't actually change the database,
1670 * so the IDL didn't send any request to the database server.)
1674 * The caller previously called ovsdb_idl_txn_abort().
1678 * The transaction was successful. The update made by the transaction
1679 * (and possibly other changes made by other database clients) should
1680 * already be visible in the IDL.
1684 * The transaction failed for some transient reason, e.g. because a
1685 * "verify" operation reported an inconsistency or due to a network
1686 * problem. The caller should wait for a change to the database, then
1687 * compose a new transaction, and commit the new transaction.
1689 * Use the return value of ovsdb_idl_get_seqno() to wait for a change in
1690 * the database. It is important to use its return value *before* the
1691 * initial call to ovsdb_idl_txn_commit() as the baseline for this
1692 * purpose, because the change that one should wait for can happen after
1693 * the initial call but before the call that returns TXN_TRY_AGAIN, and
1694 * using some other baseline value in that situation could cause an
1695 * indefinite wait if the database rarely changes.
1699 * The transaction failed because the IDL has been configured to require
1700 * a database lock (with ovsdb_idl_set_lock()) but didn't get it yet or
1701 * has already lost it.
1703 * Committing a transaction rolls back all of the changes that it made to the
1704 * IDL's copy of the database. If the transaction commits successfully, then
1705 * the database server will send an update and, thus, the IDL will be updated
1706 * with the committed changes. */
1707 enum ovsdb_idl_txn_status
1708 ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
1710 struct ovsdb_idl_row *row;
1711 struct json *operations;
1714 if (txn != txn->idl->txn) {
1718 /* If we need a lock but don't have it, give up quickly. */
1719 if (txn->idl->lock_name && !ovsdb_idl_has_lock(txn->idl)) {
1720 txn->status = TXN_NOT_LOCKED;
1721 goto disassemble_out;
1724 operations = json_array_create_1(
1725 json_string_create(txn->idl->class->database));
1727 /* Assert that we have the required lock (avoiding a race). */
1728 if (txn->idl->lock_name) {
1729 struct json *op = json_object_create();
1730 json_array_add(operations, op);
1731 json_object_put_string(op, "op", "assert");
1732 json_object_put_string(op, "lock", txn->idl->lock_name);
1735 /* Add prerequisites and declarations of new rows. */
1736 HMAP_FOR_EACH (row, txn_node, &txn->txn_rows) {
1737 /* XXX check that deleted rows exist even if no prereqs? */
1739 const struct ovsdb_idl_table_class *class = row->table->class;
1740 size_t n_columns = class->n_columns;
1741 struct json *op, *columns, *row_json;
1744 op = json_object_create();
1745 json_array_add(operations, op);
1746 json_object_put_string(op, "op", "wait");
1747 json_object_put_string(op, "table", class->name);
1748 json_object_put(op, "timeout", json_integer_create(0));
1749 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1750 json_object_put_string(op, "until", "==");
1751 columns = json_array_create_empty();
1752 json_object_put(op, "columns", columns);
1753 row_json = json_object_create();
1754 json_object_put(op, "rows", json_array_create_1(row_json));
1756 BITMAP_FOR_EACH_1 (idx, n_columns, row->prereqs) {
1757 const struct ovsdb_idl_column *column = &class->columns[idx];
1758 json_array_add(columns, json_string_create(column->name));
1759 json_object_put(row_json, column->name,
1760 ovsdb_datum_to_json(&row->old[idx],
1767 any_updates = false;
1768 HMAP_FOR_EACH (row, txn_node, &txn->txn_rows) {
1769 const struct ovsdb_idl_table_class *class = row->table->class;
1772 if (class->is_root) {
1773 struct json *op = json_object_create();
1774 json_object_put_string(op, "op", "delete");
1775 json_object_put_string(op, "table", class->name);
1776 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1777 json_array_add(operations, op);
1780 /* Let ovsdb-server decide whether to really delete it. */
1782 } else if (row->old != row->new) {
1783 struct json *row_json;
1787 op = json_object_create();
1788 json_object_put_string(op, "op", row->old ? "update" : "insert");
1789 json_object_put_string(op, "table", class->name);
1791 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1793 struct ovsdb_idl_txn_insert *insert;
1797 json_object_put(op, "uuid-name",
1798 json_string_create_nocopy(
1799 uuid_name_from_uuid(&row->uuid)));
1801 insert = xmalloc(sizeof *insert);
1802 insert->dummy = row->uuid;
1803 insert->op_index = operations->u.array.n - 1;
1804 uuid_zero(&insert->real);
1805 hmap_insert(&txn->inserted_rows, &insert->hmap_node,
1806 uuid_hash(&insert->dummy));
1808 row_json = json_object_create();
1809 json_object_put(op, "row", row_json);
1812 BITMAP_FOR_EACH_1 (idx, class->n_columns, row->written) {
1813 const struct ovsdb_idl_column *column =
1814 &class->columns[idx];
1817 || !ovsdb_datum_is_default(&row->new[idx],
1819 json_object_put(row_json, column->name,
1821 ovsdb_datum_to_json(&row->new[idx],
1825 /* If anything really changed, consider it an update.
1826 * We can't suppress not-really-changed values earlier
1827 * or transactions would become nonatomic (see the big
1828 * comment inside ovsdb_idl_txn_write()). */
1829 if (!any_updates && row->old &&
1830 !ovsdb_datum_equals(&row->old[idx], &row->new[idx],
1838 if (!row->old || !shash_is_empty(json_object(row_json))) {
1839 json_array_add(operations, op);
1846 /* Add increment. */
1847 if (txn->inc_table && any_updates) {
1850 txn->inc_index = operations->u.array.n - 1;
1852 op = json_object_create();
1853 json_object_put_string(op, "op", "mutate");
1854 json_object_put_string(op, "table", txn->inc_table);
1855 json_object_put(op, "where",
1856 substitute_uuids(where_uuid_equals(&txn->inc_row),
1858 json_object_put(op, "mutations",
1859 json_array_create_1(
1860 json_array_create_3(
1861 json_string_create(txn->inc_column),
1862 json_string_create("+="),
1863 json_integer_create(1))));
1864 json_array_add(operations, op);
1866 op = json_object_create();
1867 json_object_put_string(op, "op", "select");
1868 json_object_put_string(op, "table", txn->inc_table);
1869 json_object_put(op, "where",
1870 substitute_uuids(where_uuid_equals(&txn->inc_row),
1872 json_object_put(op, "columns",
1873 json_array_create_1(json_string_create(
1875 json_array_add(operations, op);
1878 if (txn->comment.length) {
1879 struct json *op = json_object_create();
1880 json_object_put_string(op, "op", "comment");
1881 json_object_put_string(op, "comment", ds_cstr(&txn->comment));
1882 json_array_add(operations, op);
1886 struct json *op = json_object_create();
1887 json_object_put_string(op, "op", "abort");
1888 json_array_add(operations, op);
1892 txn->status = TXN_UNCHANGED;
1893 json_destroy(operations);
1894 } else if (!jsonrpc_session_send(
1896 jsonrpc_create_request(
1897 "transact", operations, &txn->request_id))) {
1898 hmap_insert(&txn->idl->outstanding_txns, &txn->hmap_node,
1899 json_hash(txn->request_id, 0));
1900 txn->status = TXN_INCOMPLETE;
1902 txn->status = TXN_TRY_AGAIN;
1906 ovsdb_idl_txn_disassemble(txn);
1908 switch (txn->status) {
1909 case TXN_UNCOMMITTED: COVERAGE_INC(txn_uncommitted); break;
1910 case TXN_UNCHANGED: COVERAGE_INC(txn_unchanged); break;
1911 case TXN_INCOMPLETE: COVERAGE_INC(txn_incomplete); break;
1912 case TXN_ABORTED: COVERAGE_INC(txn_aborted); break;
1913 case TXN_SUCCESS: COVERAGE_INC(txn_success); break;
1914 case TXN_TRY_AGAIN: COVERAGE_INC(txn_try_again); break;
1915 case TXN_NOT_LOCKED: COVERAGE_INC(txn_not_locked); break;
1916 case TXN_ERROR: COVERAGE_INC(txn_error); break;
1922 /* Attempts to commit 'txn', blocking until the commit either succeeds or
1923 * fails. Returns the final commit status, which may be any TXN_* value other
1924 * than TXN_INCOMPLETE.
1926 * This function calls ovsdb_idl_run() on 'txn''s IDL, so it may cause the
1927 * return value of ovsdb_idl_get_seqno() to change. */
1928 enum ovsdb_idl_txn_status
1929 ovsdb_idl_txn_commit_block(struct ovsdb_idl_txn *txn)
1931 enum ovsdb_idl_txn_status status;
1934 while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
1935 ovsdb_idl_run(txn->idl);
1936 ovsdb_idl_wait(txn->idl);
1937 ovsdb_idl_txn_wait(txn);
1943 /* Returns the final (incremented) value of the column in 'txn' that was set to
1944 * be incremented by ovsdb_idl_txn_increment(). 'txn' must have committed
1947 ovsdb_idl_txn_get_increment_new_value(const struct ovsdb_idl_txn *txn)
1949 ovs_assert(txn->status == TXN_SUCCESS);
1950 return txn->inc_new_value;
1953 /* Aborts 'txn' without sending it to the database server. This is effective
1954 * only if ovsdb_idl_txn_commit() has not yet been called for 'txn'.
1955 * Otherwise, it has no effect.
1957 * Aborting a transaction doesn't free its memory. Use
1958 * ovsdb_idl_txn_destroy() to do that. */
1960 ovsdb_idl_txn_abort(struct ovsdb_idl_txn *txn)
1962 ovsdb_idl_txn_disassemble(txn);
1963 if (txn->status == TXN_UNCOMMITTED || txn->status == TXN_INCOMPLETE) {
1964 txn->status = TXN_ABORTED;
1968 /* Returns a string that reports the error status for 'txn'. The caller must
1969 * not modify or free the returned string. A call to ovsdb_idl_txn_destroy()
1970 * for 'txn' may free the returned string.
1972 * The return value is ordinarily one of the strings that
1973 * ovsdb_idl_txn_status_to_string() would return, but if the transaction failed
1974 * due to an error reported by the database server, the return value is that
1977 ovsdb_idl_txn_get_error(const struct ovsdb_idl_txn *txn)
1979 if (txn->status != TXN_ERROR) {
1980 return ovsdb_idl_txn_status_to_string(txn->status);
1981 } else if (txn->error) {
1984 return "no error details available";
1989 ovsdb_idl_txn_set_error_json(struct ovsdb_idl_txn *txn,
1990 const struct json *json)
1992 if (txn->error == NULL) {
1993 txn->error = json_to_string(json, JSSF_SORT);
1997 /* For transaction 'txn' that completed successfully, finds and returns the
1998 * permanent UUID that the database assigned to a newly inserted row, given the
1999 * 'uuid' that ovsdb_idl_txn_insert() assigned locally to that row.
2001 * Returns NULL if 'uuid' is not a UUID assigned by ovsdb_idl_txn_insert() or
2002 * if it was assigned by that function and then deleted by
2003 * ovsdb_idl_txn_delete() within the same transaction. (Rows that are inserted
2004 * and then deleted within a single transaction are never sent to the database
2005 * server, so it never assigns them a permanent UUID.) */
2007 ovsdb_idl_txn_get_insert_uuid(const struct ovsdb_idl_txn *txn,
2008 const struct uuid *uuid)
2010 const struct ovsdb_idl_txn_insert *insert;
2012 ovs_assert(txn->status == TXN_SUCCESS || txn->status == TXN_UNCHANGED);
2013 HMAP_FOR_EACH_IN_BUCKET (insert, hmap_node,
2014 uuid_hash(uuid), &txn->inserted_rows) {
2015 if (uuid_equals(uuid, &insert->dummy)) {
2016 return &insert->real;
2023 ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
2024 enum ovsdb_idl_txn_status status)
2026 txn->status = status;
2027 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
2030 /* Writes 'datum' to the specified 'column' in 'row_'. Updates both 'row_'
2031 * itself and the structs derived from it (e.g. the "struct ovsrec_*", for
2034 * 'datum' must have the correct type for its column. The IDL does not check
2035 * that it meets schema constraints, but ovsdb-server will do so at commit time
2036 * so it had better be correct.
2038 * A transaction must be in progress. Replication of 'column' must not have
2039 * been disabled (by calling ovsdb_idl_omit()).
2041 * Usually this function is used indirectly through one of the "set" functions
2042 * generated by ovsdb-idlc.
2044 * Takes ownership of what 'datum' points to (and in some cases destroys that
2045 * data before returning) but makes a copy of 'datum' itself. (Commonly
2046 * 'datum' is on the caller's stack.) */
2048 ovsdb_idl_txn_write__(const struct ovsdb_idl_row *row_,
2049 const struct ovsdb_idl_column *column,
2050 struct ovsdb_datum *datum, bool owns_datum)
2052 struct ovsdb_idl_row *row = CONST_CAST(struct ovsdb_idl_row *, row_);
2053 const struct ovsdb_idl_table_class *class;
2057 if (ovsdb_idl_row_is_synthetic(row)) {
2061 class = row->table->class;
2062 column_idx = column - class->columns;
2063 write_only = row->table->modes[column_idx] == OVSDB_IDL_MONITOR;
2065 ovs_assert(row->new != NULL);
2066 ovs_assert(column_idx < class->n_columns);
2067 ovs_assert(row->old == NULL ||
2068 row->table->modes[column_idx] & OVSDB_IDL_MONITOR);
2070 if (row->table->idl->verify_write_only && !write_only) {
2071 VLOG_ERR("Bug: Attempt to write to a read/write column (%s:%s) when"
2072 " explicitly configured not to.", class->name, column->name);
2076 /* If this is a write-only column and the datum being written is the same
2077 * as the one already there, just skip the update entirely. This is worth
2078 * optimizing because we have a lot of columns that get periodically
2079 * refreshed into the database but don't actually change that often.
2081 * We don't do this for read/write columns because that would break
2082 * atomicity of transactions--some other client might have written a
2083 * different value in that column since we read it. (But if a whole
2084 * transaction only does writes of existing values, without making any real
2085 * changes, we will drop the whole transaction later in
2086 * ovsdb_idl_txn_commit().) */
2087 if (write_only && ovsdb_datum_equals(ovsdb_idl_read(row, column),
2088 datum, &column->type)) {
2092 if (hmap_node_is_null(&row->txn_node)) {
2093 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
2094 uuid_hash(&row->uuid));
2096 if (row->old == row->new) {
2097 row->new = xmalloc(class->n_columns * sizeof *row->new);
2099 if (!row->written) {
2100 row->written = bitmap_allocate(class->n_columns);
2102 if (bitmap_is_set(row->written, column_idx)) {
2103 ovsdb_datum_destroy(&row->new[column_idx], &column->type);
2105 bitmap_set1(row->written, column_idx);
2108 row->new[column_idx] = *datum;
2110 ovsdb_datum_clone(&row->new[column_idx], datum, &column->type);
2112 (column->unparse)(row);
2113 (column->parse)(row, &row->new[column_idx]);
2118 ovsdb_datum_destroy(datum, &column->type);
2123 ovsdb_idl_txn_write(const struct ovsdb_idl_row *row,
2124 const struct ovsdb_idl_column *column,
2125 struct ovsdb_datum *datum)
2127 ovsdb_idl_txn_write__(row, column, datum, true);
2131 ovsdb_idl_txn_write_clone(const struct ovsdb_idl_row *row,
2132 const struct ovsdb_idl_column *column,
2133 const struct ovsdb_datum *datum)
2135 ovsdb_idl_txn_write__(row, column,
2136 CONST_CAST(struct ovsdb_datum *, datum), false);
2139 /* Causes the original contents of 'column' in 'row_' to be verified as a
2140 * prerequisite to completing the transaction. That is, if 'column' in 'row_'
2141 * changed (or if 'row_' was deleted) between the time that the IDL originally
2142 * read its contents and the time that the transaction commits, then the
2143 * transaction aborts and ovsdb_idl_txn_commit() returns TXN_AGAIN_WAIT or
2144 * TXN_AGAIN_NOW (depending on whether the database change has already been
2147 * The intention is that, to ensure that no transaction commits based on dirty
2148 * reads, an application should call ovsdb_idl_txn_verify() on each data item
2149 * read as part of a read-modify-write operation.
2151 * In some cases ovsdb_idl_txn_verify() reduces to a no-op, because the current
2152 * value of 'column' is already known:
2154 * - If 'row_' is a row created by the current transaction (returned by
2155 * ovsdb_idl_txn_insert()).
2157 * - If 'column' has already been modified (with ovsdb_idl_txn_write())
2158 * within the current transaction.
2160 * Because of the latter property, always call ovsdb_idl_txn_verify() *before*
2161 * ovsdb_idl_txn_write() for a given read-modify-write.
2163 * A transaction must be in progress.
2165 * Usually this function is used indirectly through one of the "verify"
2166 * functions generated by ovsdb-idlc. */
2168 ovsdb_idl_txn_verify(const struct ovsdb_idl_row *row_,
2169 const struct ovsdb_idl_column *column)
2171 struct ovsdb_idl_row *row = CONST_CAST(struct ovsdb_idl_row *, row_);
2172 const struct ovsdb_idl_table_class *class;
2175 if (ovsdb_idl_row_is_synthetic(row)) {
2179 class = row->table->class;
2180 column_idx = column - class->columns;
2182 ovs_assert(row->new != NULL);
2183 ovs_assert(row->old == NULL ||
2184 row->table->modes[column_idx] & OVSDB_IDL_MONITOR);
2186 || (row->written && bitmap_is_set(row->written, column_idx))) {
2190 if (hmap_node_is_null(&row->txn_node)) {
2191 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
2192 uuid_hash(&row->uuid));
2194 if (!row->prereqs) {
2195 row->prereqs = bitmap_allocate(class->n_columns);
2197 bitmap_set1(row->prereqs, column_idx);
2200 /* Deletes 'row_' from its table. May free 'row_', so it must not be
2201 * accessed afterward.
2203 * A transaction must be in progress.
2205 * Usually this function is used indirectly through one of the "delete"
2206 * functions generated by ovsdb-idlc. */
2208 ovsdb_idl_txn_delete(const struct ovsdb_idl_row *row_)
2210 struct ovsdb_idl_row *row = CONST_CAST(struct ovsdb_idl_row *, row_);
2212 if (ovsdb_idl_row_is_synthetic(row)) {
2216 ovs_assert(row->new != NULL);
2218 ovsdb_idl_row_unparse(row);
2219 ovsdb_idl_row_clear_new(row);
2220 ovs_assert(!row->prereqs);
2221 hmap_remove(&row->table->rows, &row->hmap_node);
2222 hmap_remove(&row->table->idl->txn->txn_rows, &row->txn_node);
2226 if (hmap_node_is_null(&row->txn_node)) {
2227 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
2228 uuid_hash(&row->uuid));
2230 ovsdb_idl_row_clear_new(row);
2234 /* Inserts and returns a new row in the table with the specified 'class' in the
2235 * database with open transaction 'txn'.
2237 * The new row is assigned a provisional UUID. If 'uuid' is null then one is
2238 * randomly generated; otherwise 'uuid' should specify a randomly generated
2239 * UUID not otherwise in use. ovsdb-server will assign a different UUID when
2240 * 'txn' is committed, but the IDL will replace any uses of the provisional
2241 * UUID in the data to be to be committed by the UUID assigned by
2244 * Usually this function is used indirectly through one of the "insert"
2245 * functions generated by ovsdb-idlc. */
2246 const struct ovsdb_idl_row *
2247 ovsdb_idl_txn_insert(struct ovsdb_idl_txn *txn,
2248 const struct ovsdb_idl_table_class *class,
2249 const struct uuid *uuid)
2251 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(class);
2254 ovs_assert(!ovsdb_idl_txn_get_row(txn, uuid));
2257 uuid_generate(&row->uuid);
2260 row->table = ovsdb_idl_table_from_class(txn->idl, class);
2261 row->new = xmalloc(class->n_columns * sizeof *row->new);
2262 hmap_insert(&row->table->rows, &row->hmap_node, uuid_hash(&row->uuid));
2263 hmap_insert(&txn->txn_rows, &row->txn_node, uuid_hash(&row->uuid));
2268 ovsdb_idl_txn_abort_all(struct ovsdb_idl *idl)
2270 struct ovsdb_idl_txn *txn;
2272 HMAP_FOR_EACH (txn, hmap_node, &idl->outstanding_txns) {
2273 ovsdb_idl_txn_complete(txn, TXN_TRY_AGAIN);
2277 static struct ovsdb_idl_txn *
2278 ovsdb_idl_txn_find(struct ovsdb_idl *idl, const struct json *id)
2280 struct ovsdb_idl_txn *txn;
2282 HMAP_FOR_EACH_WITH_HASH (txn, hmap_node,
2283 json_hash(id, 0), &idl->outstanding_txns) {
2284 if (json_equal(id, txn->request_id)) {
2292 check_json_type(const struct json *json, enum json_type type, const char *name)
2295 VLOG_WARN_RL(&syntax_rl, "%s is missing", name);
2297 } else if (json->type != type) {
2298 VLOG_WARN_RL(&syntax_rl, "%s is %s instead of %s",
2299 name, json_type_to_string(json->type),
2300 json_type_to_string(type));
2308 ovsdb_idl_txn_process_inc_reply(struct ovsdb_idl_txn *txn,
2309 const struct json_array *results)
2311 struct json *count, *rows, *row, *column;
2312 struct shash *mutate, *select;
2314 if (txn->inc_index + 2 > results->n) {
2315 VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
2316 "for increment (has %"PRIuSIZE", needs %u)",
2317 results->n, txn->inc_index + 2);
2321 /* We know that this is a JSON object because the loop in
2322 * ovsdb_idl_txn_process_reply() checked. */
2323 mutate = json_object(results->elems[txn->inc_index]);
2324 count = shash_find_data(mutate, "count");
2325 if (!check_json_type(count, JSON_INTEGER, "\"mutate\" reply \"count\"")) {
2328 if (count->u.integer != 1) {
2329 VLOG_WARN_RL(&syntax_rl,
2330 "\"mutate\" reply \"count\" is %lld instead of 1",
2335 select = json_object(results->elems[txn->inc_index + 1]);
2336 rows = shash_find_data(select, "rows");
2337 if (!check_json_type(rows, JSON_ARRAY, "\"select\" reply \"rows\"")) {
2340 if (rows->u.array.n != 1) {
2341 VLOG_WARN_RL(&syntax_rl, "\"select\" reply \"rows\" has %"PRIuSIZE" elements "
2346 row = rows->u.array.elems[0];
2347 if (!check_json_type(row, JSON_OBJECT, "\"select\" reply row")) {
2350 column = shash_find_data(json_object(row), txn->inc_column);
2351 if (!check_json_type(column, JSON_INTEGER,
2352 "\"select\" reply inc column")) {
2355 txn->inc_new_value = column->u.integer;
2360 ovsdb_idl_txn_process_insert_reply(struct ovsdb_idl_txn_insert *insert,
2361 const struct json_array *results)
2363 static const struct ovsdb_base_type uuid_type = OVSDB_BASE_UUID_INIT;
2364 struct ovsdb_error *error;
2365 struct json *json_uuid;
2366 union ovsdb_atom uuid;
2367 struct shash *reply;
2369 if (insert->op_index >= results->n) {
2370 VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
2371 "for insert (has %"PRIuSIZE", needs %u)",
2372 results->n, insert->op_index);
2376 /* We know that this is a JSON object because the loop in
2377 * ovsdb_idl_txn_process_reply() checked. */
2378 reply = json_object(results->elems[insert->op_index]);
2379 json_uuid = shash_find_data(reply, "uuid");
2380 if (!check_json_type(json_uuid, JSON_ARRAY, "\"insert\" reply \"uuid\"")) {
2384 error = ovsdb_atom_from_json(&uuid, &uuid_type, json_uuid, NULL);
2386 char *s = ovsdb_error_to_string(error);
2387 VLOG_WARN_RL(&syntax_rl, "\"insert\" reply \"uuid\" is not a JSON "
2390 ovsdb_error_destroy(error);
2394 insert->real = uuid.uuid;
2400 ovsdb_idl_txn_process_reply(struct ovsdb_idl *idl,
2401 const struct jsonrpc_msg *msg)
2403 struct ovsdb_idl_txn *txn;
2404 enum ovsdb_idl_txn_status status;
2406 txn = ovsdb_idl_txn_find(idl, msg->id);
2411 if (msg->type == JSONRPC_ERROR) {
2413 } else if (msg->result->type != JSON_ARRAY) {
2414 VLOG_WARN_RL(&syntax_rl, "reply to \"transact\" is not JSON array");
2417 struct json_array *ops = &msg->result->u.array;
2418 int hard_errors = 0;
2419 int soft_errors = 0;
2420 int lock_errors = 0;
2423 for (i = 0; i < ops->n; i++) {
2424 struct json *op = ops->elems[i];
2426 if (op->type == JSON_NULL) {
2427 /* This isn't an error in itself but indicates that some prior
2428 * operation failed, so make sure that we know about it. */
2430 } else if (op->type == JSON_OBJECT) {
2433 error = shash_find_data(json_object(op), "error");
2435 if (error->type == JSON_STRING) {
2436 if (!strcmp(error->u.string, "timed out")) {
2438 } else if (!strcmp(error->u.string, "not owner")) {
2440 } else if (strcmp(error->u.string, "aborted")) {
2442 ovsdb_idl_txn_set_error_json(txn, op);
2446 ovsdb_idl_txn_set_error_json(txn, op);
2447 VLOG_WARN_RL(&syntax_rl,
2448 "\"error\" in reply is not JSON string");
2453 ovsdb_idl_txn_set_error_json(txn, op);
2454 VLOG_WARN_RL(&syntax_rl,
2455 "operation reply is not JSON null or object");
2459 if (!soft_errors && !hard_errors && !lock_errors) {
2460 struct ovsdb_idl_txn_insert *insert;
2462 if (txn->inc_table && !ovsdb_idl_txn_process_inc_reply(txn, ops)) {
2466 HMAP_FOR_EACH (insert, hmap_node, &txn->inserted_rows) {
2467 if (!ovsdb_idl_txn_process_insert_reply(insert, ops)) {
2473 status = (hard_errors ? TXN_ERROR
2474 : lock_errors ? TXN_NOT_LOCKED
2475 : soft_errors ? TXN_TRY_AGAIN
2479 ovsdb_idl_txn_complete(txn, status);
2483 /* Returns the transaction currently active for 'row''s IDL. A transaction
2484 * must currently be active. */
2485 struct ovsdb_idl_txn *
2486 ovsdb_idl_txn_get(const struct ovsdb_idl_row *row)
2488 struct ovsdb_idl_txn *txn = row->table->idl->txn;
2489 ovs_assert(txn != NULL);
2493 /* Returns the IDL on which 'txn' acts. */
2495 ovsdb_idl_txn_get_idl (struct ovsdb_idl_txn *txn)
2500 /* If 'lock_name' is nonnull, configures 'idl' to obtain the named lock from
2501 * the database server and to avoid modifying the database when the lock cannot
2502 * be acquired (that is, when another client has the same lock).
2504 * If 'lock_name' is NULL, drops the locking requirement and releases the
2507 ovsdb_idl_set_lock(struct ovsdb_idl *idl, const char *lock_name)
2509 ovs_assert(!idl->txn);
2510 ovs_assert(hmap_is_empty(&idl->outstanding_txns));
2512 if (idl->lock_name && (!lock_name || strcmp(lock_name, idl->lock_name))) {
2513 /* Release previous lock. */
2514 ovsdb_idl_send_unlock_request(idl);
2515 free(idl->lock_name);
2516 idl->lock_name = NULL;
2517 idl->is_lock_contended = false;
2520 if (lock_name && !idl->lock_name) {
2521 /* Acquire new lock. */
2522 idl->lock_name = xstrdup(lock_name);
2523 ovsdb_idl_send_lock_request(idl);
2527 /* Returns true if 'idl' is configured to obtain a lock and owns that lock.
2529 * Locking and unlocking happens asynchronously from the database client's
2530 * point of view, so the information is only useful for optimization (e.g. if
2531 * the client doesn't have the lock then there's no point in trying to write to
2534 ovsdb_idl_has_lock(const struct ovsdb_idl *idl)
2536 return idl->has_lock;
2539 /* Returns true if 'idl' is configured to obtain a lock but the database server
2540 * has indicated that some other client already owns the requested lock. */
2542 ovsdb_idl_is_lock_contended(const struct ovsdb_idl *idl)
2544 return idl->is_lock_contended;
2548 ovsdb_idl_update_has_lock(struct ovsdb_idl *idl, bool new_has_lock)
2550 if (new_has_lock && !idl->has_lock) {
2551 if (idl->state == IDL_S_MONITORING) {
2552 idl->change_seqno++;
2554 /* We're setting up a session, so don't signal that the database
2555 * changed. Finalizing the session will increment change_seqno
2558 idl->is_lock_contended = false;
2560 idl->has_lock = new_has_lock;
2564 ovsdb_idl_send_lock_request__(struct ovsdb_idl *idl, const char *method,
2567 ovsdb_idl_update_has_lock(idl, false);
2569 json_destroy(idl->lock_request_id);
2570 idl->lock_request_id = NULL;
2572 if (jsonrpc_session_is_connected(idl->session)) {
2573 struct json *params;
2575 params = json_array_create_1(json_string_create(idl->lock_name));
2576 jsonrpc_session_send(idl->session,
2577 jsonrpc_create_request(method, params, idp));
2582 ovsdb_idl_send_lock_request(struct ovsdb_idl *idl)
2584 ovsdb_idl_send_lock_request__(idl, "lock", &idl->lock_request_id);
2588 ovsdb_idl_send_unlock_request(struct ovsdb_idl *idl)
2590 ovsdb_idl_send_lock_request__(idl, "unlock", NULL);
2594 ovsdb_idl_parse_lock_reply(struct ovsdb_idl *idl, const struct json *result)
2598 json_destroy(idl->lock_request_id);
2599 idl->lock_request_id = NULL;
2601 if (result->type == JSON_OBJECT) {
2602 const struct json *locked;
2604 locked = shash_find_data(json_object(result), "locked");
2605 got_lock = locked && locked->type == JSON_TRUE;
2610 ovsdb_idl_update_has_lock(idl, got_lock);
2612 idl->is_lock_contended = true;
2617 ovsdb_idl_parse_lock_notify(struct ovsdb_idl *idl,
2618 const struct json *params,
2622 && params->type == JSON_ARRAY
2623 && json_array(params)->n > 0
2624 && json_array(params)->elems[0]->type == JSON_STRING) {
2625 const char *lock_name = json_string(json_array(params)->elems[0]);
2627 if (!strcmp(idl->lock_name, lock_name)) {
2628 ovsdb_idl_update_has_lock(idl, new_has_lock);
2629 if (!new_has_lock) {
2630 idl->is_lock_contended = true;