1 /* Copyright (c) 2009, 2010 Nicira Networks.
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-data.h"
32 #include "ovsdb-error.h"
33 #include "ovsdb-idl-provider.h"
34 #include "poll-loop.h"
39 VLOG_DEFINE_THIS_MODULE(ovsdb_idl)
41 /* An arc from one idl_row to another. When row A contains a UUID that
42 * references row B, this is represented by an arc from A (the source) to B
45 * Arcs from a row to itself are omitted, that is, src and dst are always
48 * Arcs are never duplicated, that is, even if there are multiple references
49 * from A to B, there is only a single arc from A to B.
51 * Arcs are directed: an arc from A to B is the converse of an an arc from B to
52 * A. Both an arc and its converse may both be present, if each row refers
53 * to the other circularly.
55 * The source and destination row may be in the same table or in different
58 struct ovsdb_idl_arc {
59 struct list src_node; /* In src->src_arcs list. */
60 struct list dst_node; /* In dst->dst_arcs list. */
61 struct ovsdb_idl_row *src; /* Source row. */
62 struct ovsdb_idl_row *dst; /* Destination row. */
66 const struct ovsdb_idl_class *class;
67 struct jsonrpc_session *session;
68 struct shash table_by_name;
69 struct ovsdb_idl_table *tables;
70 struct json *monitor_request_id;
71 unsigned int last_monitor_request_seqno;
72 unsigned int change_seqno;
74 /* Transaction support. */
75 struct ovsdb_idl_txn *txn;
76 struct hmap outstanding_txns;
79 struct ovsdb_idl_txn {
80 struct hmap_node hmap_node;
81 struct json *request_id;
82 struct ovsdb_idl *idl;
84 enum ovsdb_idl_txn_status status;
92 struct json *inc_where;
93 unsigned int inc_index;
94 int64_t inc_new_value;
97 struct hmap inserted_rows;
100 struct ovsdb_idl_txn_insert {
101 struct hmap_node hmap_node; /* In struct ovsdb_idl_txn's inserted_rows. */
102 struct uuid dummy; /* Dummy UUID used locally. */
103 int op_index; /* Index into transaction's operation array. */
104 struct uuid real; /* Real UUID used by database server. */
107 static struct vlog_rate_limit syntax_rl = VLOG_RATE_LIMIT_INIT(1, 5);
108 static struct vlog_rate_limit semantic_rl = VLOG_RATE_LIMIT_INIT(1, 5);
110 static void ovsdb_idl_clear(struct ovsdb_idl *);
111 static void ovsdb_idl_send_monitor_request(struct ovsdb_idl *);
112 static void ovsdb_idl_parse_update(struct ovsdb_idl *, const struct json *);
113 static struct ovsdb_error *ovsdb_idl_parse_update__(struct ovsdb_idl *,
114 const struct json *);
115 static bool ovsdb_idl_process_update(struct ovsdb_idl_table *,
117 const struct json *old,
118 const struct json *new);
119 static void ovsdb_idl_insert_row(struct ovsdb_idl_row *, const struct json *);
120 static void ovsdb_idl_delete_row(struct ovsdb_idl_row *);
121 static bool ovsdb_idl_modify_row(struct ovsdb_idl_row *, const struct json *);
123 static bool ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *);
124 static struct ovsdb_idl_row *ovsdb_idl_row_create__(
125 const struct ovsdb_idl_table_class *);
126 static struct ovsdb_idl_row *ovsdb_idl_row_create(struct ovsdb_idl_table *,
127 const struct uuid *);
128 static void ovsdb_idl_row_destroy(struct ovsdb_idl_row *);
130 static void ovsdb_idl_row_parse(struct ovsdb_idl_row *);
131 static void ovsdb_idl_row_unparse(struct ovsdb_idl_row *);
132 static void ovsdb_idl_row_clear_old(struct ovsdb_idl_row *);
133 static void ovsdb_idl_row_clear_new(struct ovsdb_idl_row *);
135 static void ovsdb_idl_txn_abort_all(struct ovsdb_idl *);
136 static bool ovsdb_idl_txn_process_reply(struct ovsdb_idl *,
137 const struct jsonrpc_msg *msg);
139 /* Creates and returns a connection to database 'remote', which should be in a
140 * form acceptable to jsonrpc_session_open(). The connection will maintain an
141 * in-memory replica of the remote database whose schema is described by
142 * 'class'. (Ordinarily 'class' is compiled from an OVSDB schema automatically
145 ovsdb_idl_create(const char *remote, const struct ovsdb_idl_class *class)
147 struct ovsdb_idl *idl;
150 idl = xzalloc(sizeof *idl);
152 idl->session = jsonrpc_session_open(remote);
153 shash_init(&idl->table_by_name);
154 idl->tables = xmalloc(class->n_tables * sizeof *idl->tables);
155 for (i = 0; i < class->n_tables; i++) {
156 const struct ovsdb_idl_table_class *tc = &class->tables[i];
157 struct ovsdb_idl_table *table = &idl->tables[i];
160 shash_add_assert(&idl->table_by_name, tc->name, table);
162 table->modes = xmalloc(tc->n_columns);
163 memset(table->modes, OVSDB_IDL_MODE_RW, tc->n_columns);
164 shash_init(&table->columns);
165 for (j = 0; j < tc->n_columns; j++) {
166 const struct ovsdb_idl_column *column = &tc->columns[j];
168 shash_add_assert(&table->columns, column->name, column);
170 hmap_init(&table->rows);
173 idl->last_monitor_request_seqno = UINT_MAX;
174 hmap_init(&idl->outstanding_txns);
179 /* Destroys 'idl' and all of the data structures that it manages. */
181 ovsdb_idl_destroy(struct ovsdb_idl *idl)
187 ovsdb_idl_clear(idl);
188 jsonrpc_session_close(idl->session);
190 for (i = 0; i < idl->class->n_tables; i++) {
191 struct ovsdb_idl_table *table = &idl->tables[i];
192 shash_destroy(&table->columns);
193 hmap_destroy(&table->rows);
196 shash_destroy(&idl->table_by_name);
198 json_destroy(idl->monitor_request_id);
204 ovsdb_idl_clear(struct ovsdb_idl *idl)
206 bool changed = false;
209 for (i = 0; i < idl->class->n_tables; i++) {
210 struct ovsdb_idl_table *table = &idl->tables[i];
211 struct ovsdb_idl_row *row, *next_row;
213 if (hmap_is_empty(&table->rows)) {
218 HMAP_FOR_EACH_SAFE (row, next_row, struct ovsdb_idl_row, hmap_node,
220 struct ovsdb_idl_arc *arc, *next_arc;
222 if (!ovsdb_idl_row_is_orphan(row)) {
223 ovsdb_idl_row_unparse(row);
225 LIST_FOR_EACH_SAFE (arc, next_arc, struct ovsdb_idl_arc, src_node,
229 /* No need to do anything with dst_arcs: some node has those arcs
230 * as forward arcs and will destroy them itself. */
232 ovsdb_idl_row_destroy(row);
241 /* Processes a batch of messages from the database server on 'idl'. Returns
242 * true if the database as seen through 'idl' changed, false if it did not
243 * change. The initial fetch of the entire contents of the remote database is
244 * considered to be one kind of change.
246 * When this function returns false, the client may continue to use any data
247 * structures it obtained from 'idl' in the past. But when it returns true,
248 * the client must not access any of these data structures again, because they
249 * could have freed or reused for other purposes.
251 * This function can return occasional false positives, that is, report that
252 * the database changed even though it didn't. This happens if the connection
253 * to the database drops and reconnects, which causes the database contents to
254 * be reloaded even if they didn't change. (It could also happen if the
255 * database server sends out a "change" that reflects what we already thought
256 * was in the database, but the database server is not supposed to do that.)
258 * As an alternative to checking the return value, the client may check for
259 * changes in the value returned by ovsdb_idl_get_seqno().
262 ovsdb_idl_run(struct ovsdb_idl *idl)
264 unsigned int initial_change_seqno = idl->change_seqno;
268 jsonrpc_session_run(idl->session);
269 for (i = 0; jsonrpc_session_is_connected(idl->session) && i < 50; i++) {
270 struct jsonrpc_msg *msg, *reply;
273 seqno = jsonrpc_session_get_seqno(idl->session);
274 if (idl->last_monitor_request_seqno != seqno) {
275 idl->last_monitor_request_seqno = seqno;
276 ovsdb_idl_txn_abort_all(idl);
277 ovsdb_idl_send_monitor_request(idl);
281 msg = jsonrpc_session_recv(idl->session);
287 if (msg->type == JSONRPC_NOTIFY
288 && !strcmp(msg->method, "update")
289 && msg->params->type == JSON_ARRAY
290 && msg->params->u.array.n == 2
291 && msg->params->u.array.elems[0]->type == JSON_NULL) {
292 ovsdb_idl_parse_update(idl, msg->params->u.array.elems[1]);
293 } else if (msg->type == JSONRPC_REPLY
294 && idl->monitor_request_id
295 && json_equal(idl->monitor_request_id, msg->id)) {
297 json_destroy(idl->monitor_request_id);
298 idl->monitor_request_id = NULL;
299 ovsdb_idl_clear(idl);
300 ovsdb_idl_parse_update(idl, msg->result);
301 } else if (msg->type == JSONRPC_REPLY
302 && msg->id && msg->id->type == JSON_STRING
303 && !strcmp(msg->id->u.string, "echo")) {
304 /* It's a reply to our echo request. Ignore it. */
305 } else if ((msg->type == JSONRPC_ERROR
306 || msg->type == JSONRPC_REPLY)
307 && ovsdb_idl_txn_process_reply(idl, msg)) {
308 /* ovsdb_idl_txn_process_reply() did everything needful. */
310 /* This can happen if ovsdb_idl_txn_destroy() is called to destroy
311 * a transaction before we receive the reply, so keep the log level
313 VLOG_DBG("%s: received unexpected %s message",
314 jsonrpc_session_get_name(idl->session),
315 jsonrpc_msg_type_to_string(msg->type));
318 jsonrpc_session_send(idl->session, reply);
320 jsonrpc_msg_destroy(msg);
323 return initial_change_seqno != idl->change_seqno;
326 /* Arranges for poll_block() to wake up when ovsdb_idl_run() has something to
327 * do or when activity occurs on a transaction on 'idl'. */
329 ovsdb_idl_wait(struct ovsdb_idl *idl)
331 jsonrpc_session_wait(idl->session);
332 jsonrpc_session_recv_wait(idl->session);
335 /* Returns a number that represents the state of 'idl'. When 'idl' is updated
336 * (by ovsdb_idl_run()), the return value changes. */
338 ovsdb_idl_get_seqno(const struct ovsdb_idl *idl)
340 return idl->change_seqno;
343 /* Returns true if 'idl' successfully connected to the remote database and
344 * retrieved its contents (even if the connection subsequently dropped and is
345 * in the process of reconnecting). If so, then 'idl' contains an atomic
346 * snapshot of the database's contents (but it might be arbitrarily old if the
347 * connection dropped).
349 * Returns false if 'idl' has never connected or retrieved the database's
350 * contents. If so, 'idl' is empty. */
352 ovsdb_idl_has_ever_connected(const struct ovsdb_idl *idl)
354 return ovsdb_idl_get_seqno(idl) != 0;
357 /* Forces 'idl' to drop its connection to the database and reconnect. In the
358 * meantime, the contents of 'idl' will not change. */
360 ovsdb_idl_force_reconnect(struct ovsdb_idl *idl)
362 jsonrpc_session_force_reconnect(idl->session);
366 ovsdb_idl_set_mode(struct ovsdb_idl *idl,
367 const struct ovsdb_idl_column *column,
368 enum ovsdb_idl_mode mode)
372 for (i = 0; i < idl->class->n_tables; i++) {
373 const struct ovsdb_idl_table *table = &idl->tables[i];
374 const struct ovsdb_idl_table_class *tc = table->class;
376 if (column >= tc->columns && column < &tc->columns[tc->n_columns]) {
377 unsigned char *modep = &table->modes[column - tc->columns];
378 assert(*modep == OVSDB_IDL_MODE_RW || *modep == mode);
387 /* By default, 'idl' replicates all of the columns in the remote database, and
388 * ovsdb_idl_run() returns true upon a change to any column in the database.
389 * Call this function to avoid alerting ovsdb_idl_run()'s caller upon changes
392 * This is useful for columns that a client treats as "write-only", that is, it
393 * updates them but doesn't want to get alerted about its own updates. It also
394 * won't be alerted about other clients' updates, so this is suitable only for
395 * use by a client that "owns" a particular column.
397 * The client must be careful not to retain pointers to data in 'column' across
398 * calls to ovsdb_idl_run(), even when that function returns false, because
399 * the client is not alerted to changes.
401 * This function should be called after ovsdb_idl_create(), but before the
402 * first call to ovsdb_idl_run(). For any given column, this function may be
403 * called or ovsdb_idl_omit() may be called, but not both. */
405 ovsdb_idl_set_write_only(struct ovsdb_idl *idl,
406 const struct ovsdb_idl_column *column)
408 ovsdb_idl_set_mode(idl, column, OVSDB_IDL_MODE_WO);
411 /* By default, 'idl' replicates all of the columns in the remote database.
412 * Call this function to omit replicating 'column'. This saves CPU time and
413 * bandwidth to the database.
415 * This function should be called after ovsdb_idl_create(), but before the
416 * first call to ovsdb_idl_run(). For any given column, this function may be
417 * called or ovsdb_idl_set_write_only() may be called, but not both. */
419 ovsdb_idl_omit(struct ovsdb_idl *idl, const struct ovsdb_idl_column *column)
421 ovsdb_idl_set_mode(idl, column, OVSDB_IDL_MODE_NONE);
425 ovsdb_idl_send_monitor_request(struct ovsdb_idl *idl)
427 struct json *monitor_requests;
428 struct jsonrpc_msg *msg;
431 monitor_requests = json_object_create();
432 for (i = 0; i < idl->class->n_tables; i++) {
433 const struct ovsdb_idl_table *table = &idl->tables[i];
434 const struct ovsdb_idl_table_class *tc = table->class;
435 struct json *monitor_request, *columns;
438 monitor_request = json_object_create();
439 columns = json_array_create_empty();
440 for (i = 0; i < tc->n_columns; i++) {
441 const struct ovsdb_idl_column *column = &tc->columns[i];
442 if (table->modes[i] != OVSDB_IDL_MODE_NONE) {
443 json_array_add(columns, json_string_create(column->name));
446 json_object_put(monitor_request, "columns", columns);
447 json_object_put(monitor_requests, tc->name, monitor_request);
450 json_destroy(idl->monitor_request_id);
451 msg = jsonrpc_create_request(
453 json_array_create_3(json_string_create(idl->class->database),
454 json_null_create(), monitor_requests),
455 &idl->monitor_request_id);
456 jsonrpc_session_send(idl->session, msg);
460 ovsdb_idl_parse_update(struct ovsdb_idl *idl, const struct json *table_updates)
462 struct ovsdb_error *error = ovsdb_idl_parse_update__(idl, table_updates);
464 if (!VLOG_DROP_WARN(&syntax_rl)) {
465 char *s = ovsdb_error_to_string(error);
466 VLOG_WARN_RL(&syntax_rl, "%s", s);
469 ovsdb_error_destroy(error);
473 static struct ovsdb_error *
474 ovsdb_idl_parse_update__(struct ovsdb_idl *idl,
475 const struct json *table_updates)
477 const struct shash_node *tables_node;
479 if (table_updates->type != JSON_OBJECT) {
480 return ovsdb_syntax_error(table_updates, NULL,
481 "<table-updates> is not an object");
483 SHASH_FOR_EACH (tables_node, json_object(table_updates)) {
484 const struct json *table_update = tables_node->data;
485 const struct shash_node *table_node;
486 struct ovsdb_idl_table *table;
488 table = shash_find_data(&idl->table_by_name, tables_node->name);
490 return ovsdb_syntax_error(
492 "<table-updates> includes unknown table \"%s\"",
496 if (table_update->type != JSON_OBJECT) {
497 return ovsdb_syntax_error(table_update, NULL,
498 "<table-update> for table \"%s\" is "
499 "not an object", table->class->name);
501 SHASH_FOR_EACH (table_node, json_object(table_update)) {
502 const struct json *row_update = table_node->data;
503 const struct json *old_json, *new_json;
506 if (!uuid_from_string(&uuid, table_node->name)) {
507 return ovsdb_syntax_error(table_update, NULL,
508 "<table-update> for table \"%s\" "
510 "\"%s\" as member name",
514 if (row_update->type != JSON_OBJECT) {
515 return ovsdb_syntax_error(row_update, NULL,
516 "<table-update> for table \"%s\" "
517 "contains <row-update> for %s that "
523 old_json = shash_find_data(json_object(row_update), "old");
524 new_json = shash_find_data(json_object(row_update), "new");
525 if (old_json && old_json->type != JSON_OBJECT) {
526 return ovsdb_syntax_error(old_json, NULL,
527 "\"old\" <row> is not object");
528 } else if (new_json && new_json->type != JSON_OBJECT) {
529 return ovsdb_syntax_error(new_json, NULL,
530 "\"new\" <row> is not object");
531 } else if ((old_json != NULL) + (new_json != NULL)
532 != shash_count(json_object(row_update))) {
533 return ovsdb_syntax_error(row_update, NULL,
534 "<row-update> contains unexpected "
536 } else if (!old_json && !new_json) {
537 return ovsdb_syntax_error(row_update, NULL,
538 "<row-update> missing \"old\" "
539 "and \"new\" members");
542 if (ovsdb_idl_process_update(table, &uuid, old_json, new_json)) {
551 static struct ovsdb_idl_row *
552 ovsdb_idl_get_row(struct ovsdb_idl_table *table, const struct uuid *uuid)
554 struct ovsdb_idl_row *row;
556 HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_idl_row, hmap_node,
557 uuid_hash(uuid), &table->rows) {
558 if (uuid_equals(&row->uuid, uuid)) {
565 /* Returns true if a column with mode OVSDB_IDL_MODE_RW changed, false
568 ovsdb_idl_process_update(struct ovsdb_idl_table *table,
569 const struct uuid *uuid, const struct json *old,
570 const struct json *new)
572 struct ovsdb_idl_row *row;
574 row = ovsdb_idl_get_row(table, uuid);
577 if (row && !ovsdb_idl_row_is_orphan(row)) {
578 /* XXX perhaps we should check the 'old' values? */
579 ovsdb_idl_delete_row(row);
581 VLOG_WARN_RL(&semantic_rl, "cannot delete missing row "UUID_FMT" "
583 UUID_ARGS(uuid), table->class->name);
589 ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
590 } else if (ovsdb_idl_row_is_orphan(row)) {
591 ovsdb_idl_insert_row(row, new);
593 VLOG_WARN_RL(&semantic_rl, "cannot add existing row "UUID_FMT" to "
594 "table %s", UUID_ARGS(uuid), table->class->name);
595 return ovsdb_idl_modify_row(row, new);
600 /* XXX perhaps we should check the 'old' values? */
601 if (!ovsdb_idl_row_is_orphan(row)) {
602 return ovsdb_idl_modify_row(row, new);
604 VLOG_WARN_RL(&semantic_rl, "cannot modify missing but "
605 "referenced row "UUID_FMT" in table %s",
606 UUID_ARGS(uuid), table->class->name);
607 ovsdb_idl_insert_row(row, new);
610 VLOG_WARN_RL(&semantic_rl, "cannot modify missing row "UUID_FMT" "
611 "in table %s", UUID_ARGS(uuid), table->class->name);
612 ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
619 /* Returns true if a column with mode OVSDB_IDL_MODE_RW changed, false
622 ovsdb_idl_row_update(struct ovsdb_idl_row *row, const struct json *row_json)
624 struct ovsdb_idl_table *table = row->table;
625 struct shash_node *node;
626 bool changed = false;
628 SHASH_FOR_EACH (node, json_object(row_json)) {
629 const char *column_name = node->name;
630 const struct ovsdb_idl_column *column;
631 struct ovsdb_datum datum;
632 struct ovsdb_error *error;
634 column = shash_find_data(&table->columns, column_name);
636 VLOG_WARN_RL(&syntax_rl, "unknown column %s updating row "UUID_FMT,
637 column_name, UUID_ARGS(&row->uuid));
641 error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
643 unsigned int column_idx = column - table->class->columns;
644 ovsdb_datum_swap(&row->old[column_idx], &datum);
645 ovsdb_datum_destroy(&datum, &column->type);
646 if (table->modes[column_idx] == OVSDB_IDL_MODE_RW) {
650 char *s = ovsdb_error_to_string(error);
651 VLOG_WARN_RL(&syntax_rl, "error parsing column %s in row "UUID_FMT
652 " in table %s: %s", column_name,
653 UUID_ARGS(&row->uuid), table->class->name, s);
655 ovsdb_error_destroy(error);
661 /* When a row A refers to row B through a column with a "refTable" constraint,
662 * but row B does not exist, row B is called an "orphan row". Orphan rows
663 * should not persist, because the database enforces referential integrity, but
664 * they can appear transiently as changes from the database are received (the
665 * database doesn't try to topologically sort them and circular references mean
666 * it isn't always possible anyhow).
668 * This function returns true if 'row' is an orphan row, otherwise false.
671 ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *row)
673 return !row->old && !row->new;
676 /* Returns true if 'row' is conceptually part of the database as modified by
677 * the current transaction (if any), false otherwise.
679 * This function will return true if 'row' is not an orphan (see the comment on
680 * ovsdb_idl_row_is_orphan()) and:
682 * - 'row' exists in the database and has not been deleted within the
683 * current transaction (if any).
685 * - 'row' was inserted within the current transaction and has not been
686 * deleted. (In the latter case you should not have passed 'row' in at
687 * all, because ovsdb_idl_txn_delete() freed it.)
689 * This function will return false if 'row' is an orphan or if 'row' was
690 * deleted within the current transaction.
693 ovsdb_idl_row_exists(const struct ovsdb_idl_row *row)
695 return row->new != NULL;
699 ovsdb_idl_row_parse(struct ovsdb_idl_row *row)
701 const struct ovsdb_idl_table_class *class = row->table->class;
704 for (i = 0; i < class->n_columns; i++) {
705 const struct ovsdb_idl_column *c = &class->columns[i];
706 (c->parse)(row, &row->old[i]);
711 ovsdb_idl_row_unparse(struct ovsdb_idl_row *row)
713 const struct ovsdb_idl_table_class *class = row->table->class;
716 for (i = 0; i < class->n_columns; i++) {
717 const struct ovsdb_idl_column *c = &class->columns[i];
723 ovsdb_idl_row_clear_old(struct ovsdb_idl_row *row)
725 assert(row->old == row->new);
726 if (!ovsdb_idl_row_is_orphan(row)) {
727 const struct ovsdb_idl_table_class *class = row->table->class;
730 for (i = 0; i < class->n_columns; i++) {
731 ovsdb_datum_destroy(&row->old[i], &class->columns[i].type);
734 row->old = row->new = NULL;
739 ovsdb_idl_row_clear_new(struct ovsdb_idl_row *row)
741 if (row->old != row->new) {
743 const struct ovsdb_idl_table_class *class = row->table->class;
747 BITMAP_FOR_EACH_1 (i, class->n_columns, row->written) {
748 ovsdb_datum_destroy(&row->new[i], &class->columns[i].type);
760 ovsdb_idl_row_clear_arcs(struct ovsdb_idl_row *row, bool destroy_dsts)
762 struct ovsdb_idl_arc *arc, *next;
764 /* Delete all forward arcs. If 'destroy_dsts', destroy any orphaned rows
765 * that this causes to be unreferenced. */
766 LIST_FOR_EACH_SAFE (arc, next, struct ovsdb_idl_arc, src_node,
768 list_remove(&arc->dst_node);
770 && ovsdb_idl_row_is_orphan(arc->dst)
771 && list_is_empty(&arc->dst->dst_arcs)) {
772 ovsdb_idl_row_destroy(arc->dst);
776 list_init(&row->src_arcs);
779 /* Force nodes that reference 'row' to reparse. */
781 ovsdb_idl_row_reparse_backrefs(struct ovsdb_idl_row *row)
783 struct ovsdb_idl_arc *arc, *next;
785 /* This is trickier than it looks. ovsdb_idl_row_clear_arcs() will destroy
786 * 'arc', so we need to use the "safe" variant of list traversal. However,
787 * calling an ovsdb_idl_column's 'parse' function will add an arc
788 * equivalent to 'arc' to row->arcs. That could be a problem for
789 * traversal, but it adds it at the beginning of the list to prevent us
790 * from stumbling upon it again.
792 * (If duplicate arcs were possible then we would need to make sure that
793 * 'next' didn't also point into 'arc''s destination, but we forbid
794 * duplicate arcs.) */
795 LIST_FOR_EACH_SAFE (arc, next, struct ovsdb_idl_arc, dst_node,
797 struct ovsdb_idl_row *ref = arc->src;
799 ovsdb_idl_row_unparse(ref);
800 ovsdb_idl_row_clear_arcs(ref, false);
801 ovsdb_idl_row_parse(ref);
805 static struct ovsdb_idl_row *
806 ovsdb_idl_row_create__(const struct ovsdb_idl_table_class *class)
808 struct ovsdb_idl_row *row = xzalloc(class->allocation_size);
809 list_init(&row->src_arcs);
810 list_init(&row->dst_arcs);
811 hmap_node_nullify(&row->txn_node);
815 static struct ovsdb_idl_row *
816 ovsdb_idl_row_create(struct ovsdb_idl_table *table, const struct uuid *uuid)
818 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(table->class);
819 hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
826 ovsdb_idl_row_destroy(struct ovsdb_idl_row *row)
829 ovsdb_idl_row_clear_old(row);
830 hmap_remove(&row->table->rows, &row->hmap_node);
836 ovsdb_idl_insert_row(struct ovsdb_idl_row *row, const struct json *row_json)
838 const struct ovsdb_idl_table_class *class = row->table->class;
841 assert(!row->old && !row->new);
842 row->old = row->new = xmalloc(class->n_columns * sizeof *row->old);
843 for (i = 0; i < class->n_columns; i++) {
844 ovsdb_datum_init_default(&row->old[i], &class->columns[i].type);
846 ovsdb_idl_row_update(row, row_json);
847 ovsdb_idl_row_parse(row);
849 ovsdb_idl_row_reparse_backrefs(row);
853 ovsdb_idl_delete_row(struct ovsdb_idl_row *row)
855 ovsdb_idl_row_unparse(row);
856 ovsdb_idl_row_clear_arcs(row, true);
857 ovsdb_idl_row_clear_old(row);
858 if (list_is_empty(&row->dst_arcs)) {
859 ovsdb_idl_row_destroy(row);
861 ovsdb_idl_row_reparse_backrefs(row);
865 /* Returns true if a column with mode OVSDB_IDL_MODE_RW changed, false
868 ovsdb_idl_modify_row(struct ovsdb_idl_row *row, const struct json *row_json)
872 ovsdb_idl_row_unparse(row);
873 ovsdb_idl_row_clear_arcs(row, true);
874 changed = ovsdb_idl_row_update(row, row_json);
875 ovsdb_idl_row_parse(row);
881 may_add_arc(const struct ovsdb_idl_row *src, const struct ovsdb_idl_row *dst)
883 const struct ovsdb_idl_arc *arc;
890 /* No duplicate arcs.
892 * We only need to test whether the first arc in dst->dst_arcs originates
893 * at 'src', since we add all of the arcs from a given source in a clump
894 * (in a single call to ovsdb_idl_row_parse()) and new arcs are always
895 * added at the front of the dst_arcs list. */
896 if (list_is_empty(&dst->dst_arcs)) {
899 arc = CONTAINER_OF(dst->dst_arcs.next, struct ovsdb_idl_arc, dst_node);
900 return arc->src != src;
903 static struct ovsdb_idl_table *
904 ovsdb_idl_table_from_class(const struct ovsdb_idl *idl,
905 const struct ovsdb_idl_table_class *table_class)
907 return &idl->tables[table_class - idl->class->tables];
910 struct ovsdb_idl_row *
911 ovsdb_idl_get_row_arc(struct ovsdb_idl_row *src,
912 struct ovsdb_idl_table_class *dst_table_class,
913 const struct uuid *dst_uuid)
915 struct ovsdb_idl *idl = src->table->idl;
916 struct ovsdb_idl_table *dst_table;
917 struct ovsdb_idl_arc *arc;
918 struct ovsdb_idl_row *dst;
920 dst_table = ovsdb_idl_table_from_class(idl, dst_table_class);
921 dst = ovsdb_idl_get_row(dst_table, dst_uuid);
923 /* We're being called from ovsdb_idl_txn_write(). We must not update
924 * any arcs, because the transaction will be backed out at commit or
925 * abort time and we don't want our graph screwed up.
927 * Just return the destination row, if there is one and it has not been
929 if (dst && (hmap_node_is_null(&dst->txn_node) || dst->new)) {
934 /* We're being called from some other context. Update the graph. */
936 dst = ovsdb_idl_row_create(dst_table, dst_uuid);
939 /* Add a new arc, if it wouldn't be a self-arc or a duplicate arc. */
940 if (may_add_arc(src, dst)) {
941 /* The arc *must* be added at the front of the dst_arcs list. See
942 * ovsdb_idl_row_reparse_backrefs() for details. */
943 arc = xmalloc(sizeof *arc);
944 list_push_front(&src->src_arcs, &arc->src_node);
945 list_push_front(&dst->dst_arcs, &arc->dst_node);
950 return !ovsdb_idl_row_is_orphan(dst) ? dst : NULL;
954 const struct ovsdb_idl_row *
955 ovsdb_idl_get_row_for_uuid(const struct ovsdb_idl *idl,
956 const struct ovsdb_idl_table_class *tc,
957 const struct uuid *uuid)
959 return ovsdb_idl_get_row(ovsdb_idl_table_from_class(idl, tc), uuid);
962 static struct ovsdb_idl_row *
963 next_real_row(struct ovsdb_idl_table *table, struct hmap_node *node)
965 for (; node; node = hmap_next(&table->rows, node)) {
966 struct ovsdb_idl_row *row;
968 row = CONTAINER_OF(node, struct ovsdb_idl_row, hmap_node);
969 if (ovsdb_idl_row_exists(row)) {
976 const struct ovsdb_idl_row *
977 ovsdb_idl_first_row(const struct ovsdb_idl *idl,
978 const struct ovsdb_idl_table_class *table_class)
980 struct ovsdb_idl_table *table
981 = ovsdb_idl_table_from_class(idl, table_class);
982 return next_real_row(table, hmap_first(&table->rows));
985 const struct ovsdb_idl_row *
986 ovsdb_idl_next_row(const struct ovsdb_idl_row *row)
988 struct ovsdb_idl_table *table = row->table;
990 return next_real_row(table, hmap_next(&table->rows, &row->hmap_node));
993 /* Reads and returns the value of 'column' within 'row'. If an ongoing
994 * transaction has changed 'column''s value, the modified value is returned.
996 * The caller must not modify or free the returned value.
998 * Various kinds of changes can invalidate the returned value: writing to the
999 * same 'column' in 'row' (e.g. with ovsdb_idl_txn_write()), deleting 'row'
1000 * (e.g. with ovsdb_idl_txn_delete()), or completing an ongoing transaction
1001 * (e.g. with ovsdb_idl_txn_commit() or ovsdb_idl_txn_abort()). If the
1002 * returned value is needed for a long time, it is best to make a copy of it
1003 * with ovsdb_datum_clone(). */
1004 const struct ovsdb_datum *
1005 ovsdb_idl_read(const struct ovsdb_idl_row *row,
1006 const struct ovsdb_idl_column *column)
1008 const struct ovsdb_idl_table_class *class = row->table->class;
1009 size_t column_idx = column - class->columns;
1011 assert(row->new != NULL);
1012 assert(column_idx < class->n_columns);
1014 if (row->written && bitmap_is_set(row->written, column_idx)) {
1015 return &row->new[column_idx];
1016 } else if (row->old) {
1017 return &row->old[column_idx];
1019 return ovsdb_datum_default(&column->type);
1023 /* Same as ovsdb_idl_read(), except that it also asserts that 'column' has key
1024 * type 'key_type' and value type 'value_type'. (Scalar and set types will
1025 * have a value type of OVSDB_TYPE_VOID.)
1027 * This is useful in code that "knows" that a particular column has a given
1028 * type, so that it will abort if someone changes the column's type without
1029 * updating the code that uses it. */
1030 const struct ovsdb_datum *
1031 ovsdb_idl_get(const struct ovsdb_idl_row *row,
1032 const struct ovsdb_idl_column *column,
1033 enum ovsdb_atomic_type key_type OVS_UNUSED,
1034 enum ovsdb_atomic_type value_type OVS_UNUSED)
1036 assert(column->type.key.type == key_type);
1037 assert(column->type.value.type == value_type);
1039 return ovsdb_idl_read(row, column);
1044 static void ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
1045 enum ovsdb_idl_txn_status);
1048 ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status status)
1053 case TXN_INCOMPLETE:
1054 return "incomplete";
1067 struct ovsdb_idl_txn *
1068 ovsdb_idl_txn_create(struct ovsdb_idl *idl)
1070 struct ovsdb_idl_txn *txn;
1073 idl->txn = txn = xmalloc(sizeof *txn);
1074 txn->request_id = NULL;
1076 hmap_init(&txn->txn_rows);
1077 txn->status = TXN_INCOMPLETE;
1079 txn->dry_run = false;
1080 ds_init(&txn->comment);
1082 txn->inc_table = NULL;
1083 txn->inc_column = NULL;
1084 txn->inc_where = NULL;
1086 hmap_init(&txn->inserted_rows);
1091 /* Appends 's', which is treated as a printf()-type format string, to the
1092 * comments that will be passed to the OVSDB server when 'txn' is committed.
1093 * (The comment will be committed to the OVSDB log, which "ovsdb-tool
1094 * show-log" can print in a relatively human-readable form.) */
1096 ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *txn, const char *s, ...)
1100 if (txn->comment.length) {
1101 ds_put_char(&txn->comment, '\n');
1105 ds_put_format_valist(&txn->comment, s, args);
1110 ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *txn)
1112 txn->dry_run = true;
1116 ovsdb_idl_txn_increment(struct ovsdb_idl_txn *txn, const char *table,
1117 const char *column, const struct json *where)
1119 assert(!txn->inc_table);
1120 txn->inc_table = xstrdup(table);
1121 txn->inc_column = xstrdup(column);
1122 txn->inc_where = where ? json_clone(where) : json_array_create_empty();
1126 ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *txn)
1128 struct ovsdb_idl_txn_insert *insert, *next;
1130 json_destroy(txn->request_id);
1131 if (txn->status == TXN_INCOMPLETE) {
1132 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
1134 ovsdb_idl_txn_abort(txn);
1135 ds_destroy(&txn->comment);
1137 free(txn->inc_table);
1138 free(txn->inc_column);
1139 json_destroy(txn->inc_where);
1140 HMAP_FOR_EACH_SAFE (insert, next, struct ovsdb_idl_txn_insert, hmap_node,
1141 &txn->inserted_rows) {
1144 hmap_destroy(&txn->inserted_rows);
1149 ovsdb_idl_txn_wait(const struct ovsdb_idl_txn *txn)
1151 if (txn->status != TXN_INCOMPLETE) {
1152 poll_immediate_wake();
1156 static struct json *
1157 where_uuid_equals(const struct uuid *uuid)
1160 json_array_create_1(
1161 json_array_create_3(
1162 json_string_create("_uuid"),
1163 json_string_create("=="),
1164 json_array_create_2(
1165 json_string_create("uuid"),
1166 json_string_create_nocopy(
1167 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
1171 uuid_name_from_uuid(const struct uuid *uuid)
1176 name = xasprintf("row"UUID_FMT, UUID_ARGS(uuid));
1177 for (p = name; *p != '\0'; p++) {
1186 static const struct ovsdb_idl_row *
1187 ovsdb_idl_txn_get_row(const struct ovsdb_idl_txn *txn, const struct uuid *uuid)
1189 const struct ovsdb_idl_row *row;
1191 HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_idl_row, txn_node,
1192 uuid_hash(uuid), &txn->txn_rows) {
1193 if (uuid_equals(&row->uuid, uuid)) {
1200 /* XXX there must be a cleaner way to do this */
1201 static struct json *
1202 substitute_uuids(struct json *json, const struct ovsdb_idl_txn *txn)
1204 if (json->type == JSON_ARRAY) {
1208 if (json->u.array.n == 2
1209 && json->u.array.elems[0]->type == JSON_STRING
1210 && json->u.array.elems[1]->type == JSON_STRING
1211 && !strcmp(json->u.array.elems[0]->u.string, "uuid")
1212 && uuid_from_string(&uuid, json->u.array.elems[1]->u.string)) {
1213 const struct ovsdb_idl_row *row;
1215 row = ovsdb_idl_txn_get_row(txn, &uuid);
1216 if (row && !row->old && row->new) {
1219 return json_array_create_2(
1220 json_string_create("named-uuid"),
1221 json_string_create_nocopy(uuid_name_from_uuid(&uuid)));
1225 for (i = 0; i < json->u.array.n; i++) {
1226 json->u.array.elems[i] = substitute_uuids(json->u.array.elems[i],
1229 } else if (json->type == JSON_OBJECT) {
1230 struct shash_node *node;
1232 SHASH_FOR_EACH (node, json_object(json)) {
1233 node->data = substitute_uuids(node->data, txn);
1240 ovsdb_idl_txn_disassemble(struct ovsdb_idl_txn *txn)
1242 struct ovsdb_idl_row *row, *next;
1244 /* This must happen early. Otherwise, ovsdb_idl_row_parse() will call an
1245 * ovsdb_idl_column's 'parse' function, which will call
1246 * ovsdb_idl_get_row_arc(), which will seen that the IDL is in a
1247 * transaction and fail to update the graph. */
1248 txn->idl->txn = NULL;
1250 HMAP_FOR_EACH_SAFE (row, next, struct ovsdb_idl_row, txn_node,
1254 ovsdb_idl_row_unparse(row);
1255 ovsdb_idl_row_clear_arcs(row, false);
1256 ovsdb_idl_row_parse(row);
1259 ovsdb_idl_row_unparse(row);
1261 ovsdb_idl_row_clear_new(row);
1264 row->prereqs = NULL;
1267 row->written = NULL;
1269 hmap_remove(&txn->txn_rows, &row->txn_node);
1270 hmap_node_nullify(&row->txn_node);
1272 hmap_remove(&row->table->rows, &row->hmap_node);
1276 hmap_destroy(&txn->txn_rows);
1277 hmap_init(&txn->txn_rows);
1280 enum ovsdb_idl_txn_status
1281 ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
1283 struct ovsdb_idl_row *row;
1284 struct json *operations;
1287 if (txn != txn->idl->txn) {
1291 operations = json_array_create_1(
1292 json_string_create(txn->idl->class->database));
1294 /* Add prerequisites and declarations of new rows. */
1295 HMAP_FOR_EACH (row, struct ovsdb_idl_row, txn_node, &txn->txn_rows) {
1296 /* XXX check that deleted rows exist even if no prereqs? */
1298 const struct ovsdb_idl_table_class *class = row->table->class;
1299 size_t n_columns = class->n_columns;
1300 struct json *op, *columns, *row_json;
1303 op = json_object_create();
1304 json_array_add(operations, op);
1305 json_object_put_string(op, "op", "wait");
1306 json_object_put_string(op, "table", class->name);
1307 json_object_put(op, "timeout", json_integer_create(0));
1308 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1309 json_object_put_string(op, "until", "==");
1310 columns = json_array_create_empty();
1311 json_object_put(op, "columns", columns);
1312 row_json = json_object_create();
1313 json_object_put(op, "rows", json_array_create_1(row_json));
1315 BITMAP_FOR_EACH_1 (idx, n_columns, row->prereqs) {
1316 const struct ovsdb_idl_column *column = &class->columns[idx];
1317 json_array_add(columns, json_string_create(column->name));
1318 json_object_put(row_json, column->name,
1319 ovsdb_datum_to_json(&row->old[idx],
1326 any_updates = false;
1327 HMAP_FOR_EACH (row, struct ovsdb_idl_row, txn_node, &txn->txn_rows) {
1328 const struct ovsdb_idl_table_class *class = row->table->class;
1330 if (row->old == row->new) {
1332 } else if (!row->new) {
1333 struct json *op = json_object_create();
1334 json_object_put_string(op, "op", "delete");
1335 json_object_put_string(op, "table", class->name);
1336 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1337 json_array_add(operations, op);
1340 struct json *row_json;
1344 op = json_object_create();
1345 json_object_put_string(op, "op", row->old ? "update" : "insert");
1346 json_object_put_string(op, "table", class->name);
1348 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1350 struct ovsdb_idl_txn_insert *insert;
1352 json_object_put(op, "uuid-name",
1353 json_string_create_nocopy(
1354 uuid_name_from_uuid(&row->uuid)));
1356 insert = xmalloc(sizeof *insert);
1357 insert->dummy = row->uuid;
1358 insert->op_index = operations->u.array.n - 1;
1359 uuid_zero(&insert->real);
1360 hmap_insert(&txn->inserted_rows, &insert->hmap_node,
1361 uuid_hash(&insert->dummy));
1363 row_json = json_object_create();
1364 json_object_put(op, "row", row_json);
1367 BITMAP_FOR_EACH_1 (idx, class->n_columns, row->written) {
1368 const struct ovsdb_idl_column *column =
1369 &class->columns[idx];
1372 ? !ovsdb_datum_equals(&row->old[idx], &row->new[idx],
1374 : !ovsdb_datum_is_default(&row->new[idx],
1376 json_object_put(row_json, column->name,
1378 ovsdb_datum_to_json(&row->new[idx],
1385 if (!row->old || !shash_is_empty(json_object(row_json))) {
1386 json_array_add(operations, op);
1394 /* Add increment. */
1395 if (txn->inc_table && any_updates) {
1398 txn->inc_index = operations->u.array.n - 1;
1400 op = json_object_create();
1401 json_object_put_string(op, "op", "mutate");
1402 json_object_put_string(op, "table", txn->inc_table);
1403 json_object_put(op, "where",
1404 substitute_uuids(json_clone(txn->inc_where), txn));
1405 json_object_put(op, "mutations",
1406 json_array_create_1(
1407 json_array_create_3(
1408 json_string_create(txn->inc_column),
1409 json_string_create("+="),
1410 json_integer_create(1))));
1411 json_array_add(operations, op);
1413 op = json_object_create();
1414 json_object_put_string(op, "op", "select");
1415 json_object_put_string(op, "table", txn->inc_table);
1416 json_object_put(op, "where",
1417 substitute_uuids(json_clone(txn->inc_where), txn));
1418 json_object_put(op, "columns",
1419 json_array_create_1(json_string_create(
1421 json_array_add(operations, op);
1424 if (txn->comment.length) {
1425 struct json *op = json_object_create();
1426 json_object_put_string(op, "op", "comment");
1427 json_object_put_string(op, "comment", ds_cstr(&txn->comment));
1428 json_array_add(operations, op);
1432 struct json *op = json_object_create();
1433 json_object_put_string(op, "op", "abort");
1434 json_array_add(operations, op);
1438 txn->status = TXN_UNCHANGED;
1439 json_destroy(operations);
1440 } else if (!jsonrpc_session_send(
1442 jsonrpc_create_request(
1443 "transact", operations, &txn->request_id))) {
1444 hmap_insert(&txn->idl->outstanding_txns, &txn->hmap_node,
1445 json_hash(txn->request_id, 0));
1447 txn->status = TXN_TRY_AGAIN;
1450 ovsdb_idl_txn_disassemble(txn);
1454 /* Attempts to commit 'txn', blocking until the commit either succeeds or
1455 * fails. Returns the final commit status, which may be any TXN_* value other
1456 * than TXN_INCOMPLETE. */
1457 enum ovsdb_idl_txn_status
1458 ovsdb_idl_txn_commit_block(struct ovsdb_idl_txn *txn)
1460 enum ovsdb_idl_txn_status status;
1463 while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
1464 ovsdb_idl_run(txn->idl);
1465 ovsdb_idl_wait(txn->idl);
1466 ovsdb_idl_txn_wait(txn);
1473 ovsdb_idl_txn_get_increment_new_value(const struct ovsdb_idl_txn *txn)
1475 assert(txn->status == TXN_SUCCESS);
1476 return txn->inc_new_value;
1480 ovsdb_idl_txn_abort(struct ovsdb_idl_txn *txn)
1482 ovsdb_idl_txn_disassemble(txn);
1483 if (txn->status == TXN_INCOMPLETE) {
1484 txn->status = TXN_ABORTED;
1489 ovsdb_idl_txn_get_error(const struct ovsdb_idl_txn *txn)
1491 if (txn->status != TXN_ERROR) {
1492 return ovsdb_idl_txn_status_to_string(txn->status);
1493 } else if (txn->error) {
1496 return "no error details available";
1501 ovsdb_idl_txn_set_error_json(struct ovsdb_idl_txn *txn,
1502 const struct json *json)
1504 if (txn->error == NULL) {
1505 txn->error = json_to_string(json, JSSF_SORT);
1509 /* For transaction 'txn' that completed successfully, finds and returns the
1510 * permanent UUID that the database assigned to a newly inserted row, given the
1511 * 'uuid' that ovsdb_idl_txn_insert() assigned locally to that row.
1513 * Returns NULL if 'uuid' is not a UUID assigned by ovsdb_idl_txn_insert() or
1514 * if it was assigned by that function and then deleted by
1515 * ovsdb_idl_txn_delete() within the same transaction. (Rows that are inserted
1516 * and then deleted within a single transaction are never sent to the database
1517 * server, so it never assigns them a permanent UUID.) */
1519 ovsdb_idl_txn_get_insert_uuid(const struct ovsdb_idl_txn *txn,
1520 const struct uuid *uuid)
1522 const struct ovsdb_idl_txn_insert *insert;
1524 assert(txn->status == TXN_SUCCESS || txn->status == TXN_UNCHANGED);
1525 HMAP_FOR_EACH_IN_BUCKET (insert, struct ovsdb_idl_txn_insert, hmap_node,
1526 uuid_hash(uuid), &txn->inserted_rows) {
1527 if (uuid_equals(uuid, &insert->dummy)) {
1528 return &insert->real;
1535 ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
1536 enum ovsdb_idl_txn_status status)
1538 txn->status = status;
1539 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
1543 ovsdb_idl_txn_write(const struct ovsdb_idl_row *row_,
1544 const struct ovsdb_idl_column *column,
1545 struct ovsdb_datum *datum)
1547 struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1548 const struct ovsdb_idl_table_class *class = row->table->class;
1549 size_t column_idx = column - class->columns;
1551 assert(row->new != NULL);
1552 assert(column_idx < class->n_columns);
1553 assert(row->table->modes[column_idx] != OVSDB_IDL_MODE_NONE);
1555 if (hmap_node_is_null(&row->txn_node)) {
1556 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1557 uuid_hash(&row->uuid));
1559 if (row->old == row->new) {
1560 row->new = xmalloc(class->n_columns * sizeof *row->new);
1562 if (!row->written) {
1563 row->written = bitmap_allocate(class->n_columns);
1565 if (bitmap_is_set(row->written, column_idx)) {
1566 ovsdb_datum_destroy(&row->new[column_idx], &column->type);
1568 bitmap_set1(row->written, column_idx);
1570 row->new[column_idx] = *datum;
1571 (column->unparse)(row);
1572 (column->parse)(row, &row->new[column_idx]);
1576 ovsdb_idl_txn_verify(const struct ovsdb_idl_row *row_,
1577 const struct ovsdb_idl_column *column)
1579 struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1580 const struct ovsdb_idl_table_class *class = row->table->class;
1581 size_t column_idx = column - class->columns;
1583 assert(row->new != NULL);
1585 || (row->written && bitmap_is_set(row->written, column_idx))) {
1589 if (hmap_node_is_null(&row->txn_node)) {
1590 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1591 uuid_hash(&row->uuid));
1593 if (!row->prereqs) {
1594 row->prereqs = bitmap_allocate(class->n_columns);
1596 bitmap_set1(row->prereqs, column_idx);
1600 ovsdb_idl_txn_delete(const struct ovsdb_idl_row *row_)
1602 struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1604 assert(row->new != NULL);
1606 ovsdb_idl_row_unparse(row);
1607 ovsdb_idl_row_clear_new(row);
1608 assert(!row->prereqs);
1609 hmap_remove(&row->table->rows, &row->hmap_node);
1610 hmap_remove(&row->table->idl->txn->txn_rows, &row->txn_node);
1614 if (hmap_node_is_null(&row->txn_node)) {
1615 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1616 uuid_hash(&row->uuid));
1618 ovsdb_idl_row_clear_new(row);
1622 const struct ovsdb_idl_row *
1623 ovsdb_idl_txn_insert(struct ovsdb_idl_txn *txn,
1624 const struct ovsdb_idl_table_class *class,
1625 const struct uuid *uuid)
1627 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(class);
1630 assert(!ovsdb_idl_txn_get_row(txn, uuid));
1633 uuid_generate(&row->uuid);
1636 row->table = ovsdb_idl_table_from_class(txn->idl, class);
1637 row->new = xmalloc(class->n_columns * sizeof *row->new);
1638 hmap_insert(&row->table->rows, &row->hmap_node, uuid_hash(&row->uuid));
1639 hmap_insert(&txn->txn_rows, &row->txn_node, uuid_hash(&row->uuid));
1644 ovsdb_idl_txn_abort_all(struct ovsdb_idl *idl)
1646 struct ovsdb_idl_txn *txn;
1648 HMAP_FOR_EACH (txn, struct ovsdb_idl_txn, hmap_node,
1649 &idl->outstanding_txns) {
1650 ovsdb_idl_txn_complete(txn, TXN_TRY_AGAIN);
1654 static struct ovsdb_idl_txn *
1655 ovsdb_idl_txn_find(struct ovsdb_idl *idl, const struct json *id)
1657 struct ovsdb_idl_txn *txn;
1659 HMAP_FOR_EACH_WITH_HASH (txn, struct ovsdb_idl_txn, hmap_node,
1660 json_hash(id, 0), &idl->outstanding_txns) {
1661 if (json_equal(id, txn->request_id)) {
1669 check_json_type(const struct json *json, enum json_type type, const char *name)
1672 VLOG_WARN_RL(&syntax_rl, "%s is missing", name);
1674 } else if (json->type != type) {
1675 VLOG_WARN_RL(&syntax_rl, "%s is %s instead of %s",
1676 name, json_type_to_string(json->type),
1677 json_type_to_string(type));
1685 ovsdb_idl_txn_process_inc_reply(struct ovsdb_idl_txn *txn,
1686 const struct json_array *results)
1688 struct json *count, *rows, *row, *column;
1689 struct shash *mutate, *select;
1691 if (txn->inc_index + 2 > results->n) {
1692 VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
1693 "for increment (has %zu, needs %u)",
1694 results->n, txn->inc_index + 2);
1698 /* We know that this is a JSON object because the loop in
1699 * ovsdb_idl_txn_process_reply() checked. */
1700 mutate = json_object(results->elems[txn->inc_index]);
1701 count = shash_find_data(mutate, "count");
1702 if (!check_json_type(count, JSON_INTEGER, "\"mutate\" reply \"count\"")) {
1705 if (count->u.integer != 1) {
1706 VLOG_WARN_RL(&syntax_rl,
1707 "\"mutate\" reply \"count\" is %lld instead of 1",
1712 select = json_object(results->elems[txn->inc_index + 1]);
1713 rows = shash_find_data(select, "rows");
1714 if (!check_json_type(rows, JSON_ARRAY, "\"select\" reply \"rows\"")) {
1717 if (rows->u.array.n != 1) {
1718 VLOG_WARN_RL(&syntax_rl, "\"select\" reply \"rows\" has %zu elements "
1723 row = rows->u.array.elems[0];
1724 if (!check_json_type(row, JSON_OBJECT, "\"select\" reply row")) {
1727 column = shash_find_data(json_object(row), txn->inc_column);
1728 if (!check_json_type(column, JSON_INTEGER,
1729 "\"select\" reply inc column")) {
1732 txn->inc_new_value = column->u.integer;
1737 ovsdb_idl_txn_process_insert_reply(struct ovsdb_idl_txn_insert *insert,
1738 const struct json_array *results)
1740 static const struct ovsdb_base_type uuid_type = OVSDB_BASE_UUID_INIT;
1741 struct ovsdb_error *error;
1742 struct json *json_uuid;
1743 union ovsdb_atom uuid;
1744 struct shash *reply;
1746 if (insert->op_index >= results->n) {
1747 VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
1748 "for insert (has %zu, needs %u)",
1749 results->n, insert->op_index);
1753 /* We know that this is a JSON object because the loop in
1754 * ovsdb_idl_txn_process_reply() checked. */
1755 reply = json_object(results->elems[insert->op_index]);
1756 json_uuid = shash_find_data(reply, "uuid");
1757 if (!check_json_type(json_uuid, JSON_ARRAY, "\"insert\" reply \"uuid\"")) {
1761 error = ovsdb_atom_from_json(&uuid, &uuid_type, json_uuid, NULL);
1763 char *s = ovsdb_error_to_string(error);
1764 VLOG_WARN_RL(&syntax_rl, "\"insert\" reply \"uuid\" is not a JSON "
1770 insert->real = uuid.uuid;
1776 ovsdb_idl_txn_process_reply(struct ovsdb_idl *idl,
1777 const struct jsonrpc_msg *msg)
1779 struct ovsdb_idl_txn *txn;
1780 enum ovsdb_idl_txn_status status;
1782 txn = ovsdb_idl_txn_find(idl, msg->id);
1787 if (msg->type == JSONRPC_ERROR) {
1789 } else if (msg->result->type != JSON_ARRAY) {
1790 VLOG_WARN_RL(&syntax_rl, "reply to \"transact\" is not JSON array");
1793 struct json_array *ops = &msg->result->u.array;
1794 int hard_errors = 0;
1795 int soft_errors = 0;
1798 for (i = 0; i < ops->n; i++) {
1799 struct json *op = ops->elems[i];
1801 if (op->type == JSON_NULL) {
1802 /* This isn't an error in itself but indicates that some prior
1803 * operation failed, so make sure that we know about it. */
1805 } else if (op->type == JSON_OBJECT) {
1808 error = shash_find_data(json_object(op), "error");
1810 if (error->type == JSON_STRING) {
1811 if (!strcmp(error->u.string, "timed out")) {
1813 } else if (strcmp(error->u.string, "aborted")) {
1815 ovsdb_idl_txn_set_error_json(txn, op);
1819 ovsdb_idl_txn_set_error_json(txn, op);
1820 VLOG_WARN_RL(&syntax_rl,
1821 "\"error\" in reply is not JSON string");
1826 ovsdb_idl_txn_set_error_json(txn, op);
1827 VLOG_WARN_RL(&syntax_rl,
1828 "operation reply is not JSON null or object");
1832 if (!soft_errors && !hard_errors) {
1833 struct ovsdb_idl_txn_insert *insert;
1835 if (txn->inc_table && !ovsdb_idl_txn_process_inc_reply(txn, ops)) {
1839 HMAP_FOR_EACH (insert, struct ovsdb_idl_txn_insert, hmap_node,
1840 &txn->inserted_rows) {
1841 if (!ovsdb_idl_txn_process_insert_reply(insert, ops)) {
1847 status = (hard_errors ? TXN_ERROR
1848 : soft_errors ? TXN_TRY_AGAIN
1852 ovsdb_idl_txn_complete(txn, status);
1856 struct ovsdb_idl_txn *
1857 ovsdb_idl_txn_get(const struct ovsdb_idl_row *row)
1859 struct ovsdb_idl_txn *txn = row->table->idl->txn;
1860 assert(txn != NULL);
1865 ovsdb_idl_txn_get_idl (struct ovsdb_idl_txn *txn)