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 struct ovsdb_datum *old = &row->old[column_idx];
646 if (!ovsdb_datum_equals(old, &datum, &column->type)) {
647 ovsdb_datum_swap(old, &datum);
648 if (table->modes[column_idx] == OVSDB_IDL_MODE_RW) {
652 /* Didn't really change but the OVSDB monitor protocol always
653 * includes every value in a row. */
656 ovsdb_datum_destroy(&datum, &column->type);
658 char *s = ovsdb_error_to_string(error);
659 VLOG_WARN_RL(&syntax_rl, "error parsing column %s in row "UUID_FMT
660 " in table %s: %s", column_name,
661 UUID_ARGS(&row->uuid), table->class->name, s);
663 ovsdb_error_destroy(error);
669 /* When a row A refers to row B through a column with a "refTable" constraint,
670 * but row B does not exist, row B is called an "orphan row". Orphan rows
671 * should not persist, because the database enforces referential integrity, but
672 * they can appear transiently as changes from the database are received (the
673 * database doesn't try to topologically sort them and circular references mean
674 * it isn't always possible anyhow).
676 * This function returns true if 'row' is an orphan row, otherwise false.
679 ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *row)
681 return !row->old && !row->new;
684 /* Returns true if 'row' is conceptually part of the database as modified by
685 * the current transaction (if any), false otherwise.
687 * This function will return true if 'row' is not an orphan (see the comment on
688 * ovsdb_idl_row_is_orphan()) and:
690 * - 'row' exists in the database and has not been deleted within the
691 * current transaction (if any).
693 * - 'row' was inserted within the current transaction and has not been
694 * deleted. (In the latter case you should not have passed 'row' in at
695 * all, because ovsdb_idl_txn_delete() freed it.)
697 * This function will return false if 'row' is an orphan or if 'row' was
698 * deleted within the current transaction.
701 ovsdb_idl_row_exists(const struct ovsdb_idl_row *row)
703 return row->new != NULL;
707 ovsdb_idl_row_parse(struct ovsdb_idl_row *row)
709 const struct ovsdb_idl_table_class *class = row->table->class;
712 for (i = 0; i < class->n_columns; i++) {
713 const struct ovsdb_idl_column *c = &class->columns[i];
714 (c->parse)(row, &row->old[i]);
719 ovsdb_idl_row_unparse(struct ovsdb_idl_row *row)
721 const struct ovsdb_idl_table_class *class = row->table->class;
724 for (i = 0; i < class->n_columns; i++) {
725 const struct ovsdb_idl_column *c = &class->columns[i];
731 ovsdb_idl_row_clear_old(struct ovsdb_idl_row *row)
733 assert(row->old == row->new);
734 if (!ovsdb_idl_row_is_orphan(row)) {
735 const struct ovsdb_idl_table_class *class = row->table->class;
738 for (i = 0; i < class->n_columns; i++) {
739 ovsdb_datum_destroy(&row->old[i], &class->columns[i].type);
742 row->old = row->new = NULL;
747 ovsdb_idl_row_clear_new(struct ovsdb_idl_row *row)
749 if (row->old != row->new) {
751 const struct ovsdb_idl_table_class *class = row->table->class;
755 BITMAP_FOR_EACH_1 (i, class->n_columns, row->written) {
756 ovsdb_datum_destroy(&row->new[i], &class->columns[i].type);
768 ovsdb_idl_row_clear_arcs(struct ovsdb_idl_row *row, bool destroy_dsts)
770 struct ovsdb_idl_arc *arc, *next;
772 /* Delete all forward arcs. If 'destroy_dsts', destroy any orphaned rows
773 * that this causes to be unreferenced. */
774 LIST_FOR_EACH_SAFE (arc, next, struct ovsdb_idl_arc, src_node,
776 list_remove(&arc->dst_node);
778 && ovsdb_idl_row_is_orphan(arc->dst)
779 && list_is_empty(&arc->dst->dst_arcs)) {
780 ovsdb_idl_row_destroy(arc->dst);
784 list_init(&row->src_arcs);
787 /* Force nodes that reference 'row' to reparse. */
789 ovsdb_idl_row_reparse_backrefs(struct ovsdb_idl_row *row)
791 struct ovsdb_idl_arc *arc, *next;
793 /* This is trickier than it looks. ovsdb_idl_row_clear_arcs() will destroy
794 * 'arc', so we need to use the "safe" variant of list traversal. However,
795 * calling an ovsdb_idl_column's 'parse' function will add an arc
796 * equivalent to 'arc' to row->arcs. That could be a problem for
797 * traversal, but it adds it at the beginning of the list to prevent us
798 * from stumbling upon it again.
800 * (If duplicate arcs were possible then we would need to make sure that
801 * 'next' didn't also point into 'arc''s destination, but we forbid
802 * duplicate arcs.) */
803 LIST_FOR_EACH_SAFE (arc, next, struct ovsdb_idl_arc, dst_node,
805 struct ovsdb_idl_row *ref = arc->src;
807 ovsdb_idl_row_unparse(ref);
808 ovsdb_idl_row_clear_arcs(ref, false);
809 ovsdb_idl_row_parse(ref);
813 static struct ovsdb_idl_row *
814 ovsdb_idl_row_create__(const struct ovsdb_idl_table_class *class)
816 struct ovsdb_idl_row *row = xzalloc(class->allocation_size);
817 list_init(&row->src_arcs);
818 list_init(&row->dst_arcs);
819 hmap_node_nullify(&row->txn_node);
823 static struct ovsdb_idl_row *
824 ovsdb_idl_row_create(struct ovsdb_idl_table *table, const struct uuid *uuid)
826 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(table->class);
827 hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
834 ovsdb_idl_row_destroy(struct ovsdb_idl_row *row)
837 ovsdb_idl_row_clear_old(row);
838 hmap_remove(&row->table->rows, &row->hmap_node);
844 ovsdb_idl_insert_row(struct ovsdb_idl_row *row, const struct json *row_json)
846 const struct ovsdb_idl_table_class *class = row->table->class;
849 assert(!row->old && !row->new);
850 row->old = row->new = xmalloc(class->n_columns * sizeof *row->old);
851 for (i = 0; i < class->n_columns; i++) {
852 ovsdb_datum_init_default(&row->old[i], &class->columns[i].type);
854 ovsdb_idl_row_update(row, row_json);
855 ovsdb_idl_row_parse(row);
857 ovsdb_idl_row_reparse_backrefs(row);
861 ovsdb_idl_delete_row(struct ovsdb_idl_row *row)
863 ovsdb_idl_row_unparse(row);
864 ovsdb_idl_row_clear_arcs(row, true);
865 ovsdb_idl_row_clear_old(row);
866 if (list_is_empty(&row->dst_arcs)) {
867 ovsdb_idl_row_destroy(row);
869 ovsdb_idl_row_reparse_backrefs(row);
873 /* Returns true if a column with mode OVSDB_IDL_MODE_RW changed, false
876 ovsdb_idl_modify_row(struct ovsdb_idl_row *row, const struct json *row_json)
880 ovsdb_idl_row_unparse(row);
881 ovsdb_idl_row_clear_arcs(row, true);
882 changed = ovsdb_idl_row_update(row, row_json);
883 ovsdb_idl_row_parse(row);
889 may_add_arc(const struct ovsdb_idl_row *src, const struct ovsdb_idl_row *dst)
891 const struct ovsdb_idl_arc *arc;
898 /* No duplicate arcs.
900 * We only need to test whether the first arc in dst->dst_arcs originates
901 * at 'src', since we add all of the arcs from a given source in a clump
902 * (in a single call to ovsdb_idl_row_parse()) and new arcs are always
903 * added at the front of the dst_arcs list. */
904 if (list_is_empty(&dst->dst_arcs)) {
907 arc = CONTAINER_OF(dst->dst_arcs.next, struct ovsdb_idl_arc, dst_node);
908 return arc->src != src;
911 static struct ovsdb_idl_table *
912 ovsdb_idl_table_from_class(const struct ovsdb_idl *idl,
913 const struct ovsdb_idl_table_class *table_class)
915 return &idl->tables[table_class - idl->class->tables];
918 struct ovsdb_idl_row *
919 ovsdb_idl_get_row_arc(struct ovsdb_idl_row *src,
920 struct ovsdb_idl_table_class *dst_table_class,
921 const struct uuid *dst_uuid)
923 struct ovsdb_idl *idl = src->table->idl;
924 struct ovsdb_idl_table *dst_table;
925 struct ovsdb_idl_arc *arc;
926 struct ovsdb_idl_row *dst;
928 dst_table = ovsdb_idl_table_from_class(idl, dst_table_class);
929 dst = ovsdb_idl_get_row(dst_table, dst_uuid);
931 /* We're being called from ovsdb_idl_txn_write(). We must not update
932 * any arcs, because the transaction will be backed out at commit or
933 * abort time and we don't want our graph screwed up.
935 * Just return the destination row, if there is one and it has not been
937 if (dst && (hmap_node_is_null(&dst->txn_node) || dst->new)) {
942 /* We're being called from some other context. Update the graph. */
944 dst = ovsdb_idl_row_create(dst_table, dst_uuid);
947 /* Add a new arc, if it wouldn't be a self-arc or a duplicate arc. */
948 if (may_add_arc(src, dst)) {
949 /* The arc *must* be added at the front of the dst_arcs list. See
950 * ovsdb_idl_row_reparse_backrefs() for details. */
951 arc = xmalloc(sizeof *arc);
952 list_push_front(&src->src_arcs, &arc->src_node);
953 list_push_front(&dst->dst_arcs, &arc->dst_node);
958 return !ovsdb_idl_row_is_orphan(dst) ? dst : NULL;
962 const struct ovsdb_idl_row *
963 ovsdb_idl_get_row_for_uuid(const struct ovsdb_idl *idl,
964 const struct ovsdb_idl_table_class *tc,
965 const struct uuid *uuid)
967 return ovsdb_idl_get_row(ovsdb_idl_table_from_class(idl, tc), uuid);
970 static struct ovsdb_idl_row *
971 next_real_row(struct ovsdb_idl_table *table, struct hmap_node *node)
973 for (; node; node = hmap_next(&table->rows, node)) {
974 struct ovsdb_idl_row *row;
976 row = CONTAINER_OF(node, struct ovsdb_idl_row, hmap_node);
977 if (ovsdb_idl_row_exists(row)) {
984 const struct ovsdb_idl_row *
985 ovsdb_idl_first_row(const struct ovsdb_idl *idl,
986 const struct ovsdb_idl_table_class *table_class)
988 struct ovsdb_idl_table *table
989 = ovsdb_idl_table_from_class(idl, table_class);
990 return next_real_row(table, hmap_first(&table->rows));
993 const struct ovsdb_idl_row *
994 ovsdb_idl_next_row(const struct ovsdb_idl_row *row)
996 struct ovsdb_idl_table *table = row->table;
998 return next_real_row(table, hmap_next(&table->rows, &row->hmap_node));
1001 /* Reads and returns the value of 'column' within 'row'. If an ongoing
1002 * transaction has changed 'column''s value, the modified value is returned.
1004 * The caller must not modify or free the returned value.
1006 * Various kinds of changes can invalidate the returned value: writing to the
1007 * same 'column' in 'row' (e.g. with ovsdb_idl_txn_write()), deleting 'row'
1008 * (e.g. with ovsdb_idl_txn_delete()), or completing an ongoing transaction
1009 * (e.g. with ovsdb_idl_txn_commit() or ovsdb_idl_txn_abort()). If the
1010 * returned value is needed for a long time, it is best to make a copy of it
1011 * with ovsdb_datum_clone(). */
1012 const struct ovsdb_datum *
1013 ovsdb_idl_read(const struct ovsdb_idl_row *row,
1014 const struct ovsdb_idl_column *column)
1016 const struct ovsdb_idl_table_class *class = row->table->class;
1017 size_t column_idx = column - class->columns;
1019 assert(row->new != NULL);
1020 assert(column_idx < class->n_columns);
1022 if (row->written && bitmap_is_set(row->written, column_idx)) {
1023 return &row->new[column_idx];
1024 } else if (row->old) {
1025 return &row->old[column_idx];
1027 return ovsdb_datum_default(&column->type);
1031 /* Same as ovsdb_idl_read(), except that it also asserts that 'column' has key
1032 * type 'key_type' and value type 'value_type'. (Scalar and set types will
1033 * have a value type of OVSDB_TYPE_VOID.)
1035 * This is useful in code that "knows" that a particular column has a given
1036 * type, so that it will abort if someone changes the column's type without
1037 * updating the code that uses it. */
1038 const struct ovsdb_datum *
1039 ovsdb_idl_get(const struct ovsdb_idl_row *row,
1040 const struct ovsdb_idl_column *column,
1041 enum ovsdb_atomic_type key_type OVS_UNUSED,
1042 enum ovsdb_atomic_type value_type OVS_UNUSED)
1044 assert(column->type.key.type == key_type);
1045 assert(column->type.value.type == value_type);
1047 return ovsdb_idl_read(row, column);
1052 static void ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
1053 enum ovsdb_idl_txn_status);
1056 ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status status)
1061 case TXN_INCOMPLETE:
1062 return "incomplete";
1075 struct ovsdb_idl_txn *
1076 ovsdb_idl_txn_create(struct ovsdb_idl *idl)
1078 struct ovsdb_idl_txn *txn;
1081 idl->txn = txn = xmalloc(sizeof *txn);
1082 txn->request_id = NULL;
1084 hmap_init(&txn->txn_rows);
1085 txn->status = TXN_INCOMPLETE;
1087 txn->dry_run = false;
1088 ds_init(&txn->comment);
1090 txn->inc_table = NULL;
1091 txn->inc_column = NULL;
1092 txn->inc_where = NULL;
1094 hmap_init(&txn->inserted_rows);
1099 /* Appends 's', which is treated as a printf()-type format string, to the
1100 * comments that will be passed to the OVSDB server when 'txn' is committed.
1101 * (The comment will be committed to the OVSDB log, which "ovsdb-tool
1102 * show-log" can print in a relatively human-readable form.) */
1104 ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *txn, const char *s, ...)
1108 if (txn->comment.length) {
1109 ds_put_char(&txn->comment, '\n');
1113 ds_put_format_valist(&txn->comment, s, args);
1118 ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *txn)
1120 txn->dry_run = true;
1124 ovsdb_idl_txn_increment(struct ovsdb_idl_txn *txn, const char *table,
1125 const char *column, const struct json *where)
1127 assert(!txn->inc_table);
1128 txn->inc_table = xstrdup(table);
1129 txn->inc_column = xstrdup(column);
1130 txn->inc_where = where ? json_clone(where) : json_array_create_empty();
1134 ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *txn)
1136 struct ovsdb_idl_txn_insert *insert, *next;
1138 json_destroy(txn->request_id);
1139 if (txn->status == TXN_INCOMPLETE) {
1140 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
1142 ovsdb_idl_txn_abort(txn);
1143 ds_destroy(&txn->comment);
1145 free(txn->inc_table);
1146 free(txn->inc_column);
1147 json_destroy(txn->inc_where);
1148 HMAP_FOR_EACH_SAFE (insert, next, struct ovsdb_idl_txn_insert, hmap_node,
1149 &txn->inserted_rows) {
1152 hmap_destroy(&txn->inserted_rows);
1157 ovsdb_idl_txn_wait(const struct ovsdb_idl_txn *txn)
1159 if (txn->status != TXN_INCOMPLETE) {
1160 poll_immediate_wake();
1164 static struct json *
1165 where_uuid_equals(const struct uuid *uuid)
1168 json_array_create_1(
1169 json_array_create_3(
1170 json_string_create("_uuid"),
1171 json_string_create("=="),
1172 json_array_create_2(
1173 json_string_create("uuid"),
1174 json_string_create_nocopy(
1175 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
1179 uuid_name_from_uuid(const struct uuid *uuid)
1184 name = xasprintf("row"UUID_FMT, UUID_ARGS(uuid));
1185 for (p = name; *p != '\0'; p++) {
1194 static const struct ovsdb_idl_row *
1195 ovsdb_idl_txn_get_row(const struct ovsdb_idl_txn *txn, const struct uuid *uuid)
1197 const struct ovsdb_idl_row *row;
1199 HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_idl_row, txn_node,
1200 uuid_hash(uuid), &txn->txn_rows) {
1201 if (uuid_equals(&row->uuid, uuid)) {
1208 /* XXX there must be a cleaner way to do this */
1209 static struct json *
1210 substitute_uuids(struct json *json, const struct ovsdb_idl_txn *txn)
1212 if (json->type == JSON_ARRAY) {
1216 if (json->u.array.n == 2
1217 && json->u.array.elems[0]->type == JSON_STRING
1218 && json->u.array.elems[1]->type == JSON_STRING
1219 && !strcmp(json->u.array.elems[0]->u.string, "uuid")
1220 && uuid_from_string(&uuid, json->u.array.elems[1]->u.string)) {
1221 const struct ovsdb_idl_row *row;
1223 row = ovsdb_idl_txn_get_row(txn, &uuid);
1224 if (row && !row->old && row->new) {
1227 return json_array_create_2(
1228 json_string_create("named-uuid"),
1229 json_string_create_nocopy(uuid_name_from_uuid(&uuid)));
1233 for (i = 0; i < json->u.array.n; i++) {
1234 json->u.array.elems[i] = substitute_uuids(json->u.array.elems[i],
1237 } else if (json->type == JSON_OBJECT) {
1238 struct shash_node *node;
1240 SHASH_FOR_EACH (node, json_object(json)) {
1241 node->data = substitute_uuids(node->data, txn);
1248 ovsdb_idl_txn_disassemble(struct ovsdb_idl_txn *txn)
1250 struct ovsdb_idl_row *row, *next;
1252 /* This must happen early. Otherwise, ovsdb_idl_row_parse() will call an
1253 * ovsdb_idl_column's 'parse' function, which will call
1254 * ovsdb_idl_get_row_arc(), which will seen that the IDL is in a
1255 * transaction and fail to update the graph. */
1256 txn->idl->txn = NULL;
1258 HMAP_FOR_EACH_SAFE (row, next, struct ovsdb_idl_row, txn_node,
1262 ovsdb_idl_row_unparse(row);
1263 ovsdb_idl_row_clear_arcs(row, false);
1264 ovsdb_idl_row_parse(row);
1267 ovsdb_idl_row_unparse(row);
1269 ovsdb_idl_row_clear_new(row);
1272 row->prereqs = NULL;
1275 row->written = NULL;
1277 hmap_remove(&txn->txn_rows, &row->txn_node);
1278 hmap_node_nullify(&row->txn_node);
1280 hmap_remove(&row->table->rows, &row->hmap_node);
1284 hmap_destroy(&txn->txn_rows);
1285 hmap_init(&txn->txn_rows);
1288 enum ovsdb_idl_txn_status
1289 ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
1291 struct ovsdb_idl_row *row;
1292 struct json *operations;
1295 if (txn != txn->idl->txn) {
1299 operations = json_array_create_1(
1300 json_string_create(txn->idl->class->database));
1302 /* Add prerequisites and declarations of new rows. */
1303 HMAP_FOR_EACH (row, struct ovsdb_idl_row, txn_node, &txn->txn_rows) {
1304 /* XXX check that deleted rows exist even if no prereqs? */
1306 const struct ovsdb_idl_table_class *class = row->table->class;
1307 size_t n_columns = class->n_columns;
1308 struct json *op, *columns, *row_json;
1311 op = json_object_create();
1312 json_array_add(operations, op);
1313 json_object_put_string(op, "op", "wait");
1314 json_object_put_string(op, "table", class->name);
1315 json_object_put(op, "timeout", json_integer_create(0));
1316 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1317 json_object_put_string(op, "until", "==");
1318 columns = json_array_create_empty();
1319 json_object_put(op, "columns", columns);
1320 row_json = json_object_create();
1321 json_object_put(op, "rows", json_array_create_1(row_json));
1323 BITMAP_FOR_EACH_1 (idx, n_columns, row->prereqs) {
1324 const struct ovsdb_idl_column *column = &class->columns[idx];
1325 json_array_add(columns, json_string_create(column->name));
1326 json_object_put(row_json, column->name,
1327 ovsdb_datum_to_json(&row->old[idx],
1334 any_updates = false;
1335 HMAP_FOR_EACH (row, struct ovsdb_idl_row, txn_node, &txn->txn_rows) {
1336 const struct ovsdb_idl_table_class *class = row->table->class;
1338 if (row->old == row->new) {
1340 } else if (!row->new) {
1341 struct json *op = json_object_create();
1342 json_object_put_string(op, "op", "delete");
1343 json_object_put_string(op, "table", class->name);
1344 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1345 json_array_add(operations, op);
1348 struct json *row_json;
1352 op = json_object_create();
1353 json_object_put_string(op, "op", row->old ? "update" : "insert");
1354 json_object_put_string(op, "table", class->name);
1356 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1358 struct ovsdb_idl_txn_insert *insert;
1360 json_object_put(op, "uuid-name",
1361 json_string_create_nocopy(
1362 uuid_name_from_uuid(&row->uuid)));
1364 insert = xmalloc(sizeof *insert);
1365 insert->dummy = row->uuid;
1366 insert->op_index = operations->u.array.n - 1;
1367 uuid_zero(&insert->real);
1368 hmap_insert(&txn->inserted_rows, &insert->hmap_node,
1369 uuid_hash(&insert->dummy));
1371 row_json = json_object_create();
1372 json_object_put(op, "row", row_json);
1375 BITMAP_FOR_EACH_1 (idx, class->n_columns, row->written) {
1376 const struct ovsdb_idl_column *column =
1377 &class->columns[idx];
1380 ? !ovsdb_datum_equals(&row->old[idx], &row->new[idx],
1382 : !ovsdb_datum_is_default(&row->new[idx],
1384 json_object_put(row_json, column->name,
1386 ovsdb_datum_to_json(&row->new[idx],
1393 if (!row->old || !shash_is_empty(json_object(row_json))) {
1394 json_array_add(operations, op);
1402 /* Add increment. */
1403 if (txn->inc_table && any_updates) {
1406 txn->inc_index = operations->u.array.n - 1;
1408 op = json_object_create();
1409 json_object_put_string(op, "op", "mutate");
1410 json_object_put_string(op, "table", txn->inc_table);
1411 json_object_put(op, "where",
1412 substitute_uuids(json_clone(txn->inc_where), txn));
1413 json_object_put(op, "mutations",
1414 json_array_create_1(
1415 json_array_create_3(
1416 json_string_create(txn->inc_column),
1417 json_string_create("+="),
1418 json_integer_create(1))));
1419 json_array_add(operations, op);
1421 op = json_object_create();
1422 json_object_put_string(op, "op", "select");
1423 json_object_put_string(op, "table", txn->inc_table);
1424 json_object_put(op, "where",
1425 substitute_uuids(json_clone(txn->inc_where), txn));
1426 json_object_put(op, "columns",
1427 json_array_create_1(json_string_create(
1429 json_array_add(operations, op);
1432 if (txn->comment.length) {
1433 struct json *op = json_object_create();
1434 json_object_put_string(op, "op", "comment");
1435 json_object_put_string(op, "comment", ds_cstr(&txn->comment));
1436 json_array_add(operations, op);
1440 struct json *op = json_object_create();
1441 json_object_put_string(op, "op", "abort");
1442 json_array_add(operations, op);
1446 txn->status = TXN_UNCHANGED;
1447 json_destroy(operations);
1448 } else if (!jsonrpc_session_send(
1450 jsonrpc_create_request(
1451 "transact", operations, &txn->request_id))) {
1452 hmap_insert(&txn->idl->outstanding_txns, &txn->hmap_node,
1453 json_hash(txn->request_id, 0));
1455 txn->status = TXN_TRY_AGAIN;
1458 ovsdb_idl_txn_disassemble(txn);
1462 /* Attempts to commit 'txn', blocking until the commit either succeeds or
1463 * fails. Returns the final commit status, which may be any TXN_* value other
1464 * than TXN_INCOMPLETE. */
1465 enum ovsdb_idl_txn_status
1466 ovsdb_idl_txn_commit_block(struct ovsdb_idl_txn *txn)
1468 enum ovsdb_idl_txn_status status;
1471 while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
1472 ovsdb_idl_run(txn->idl);
1473 ovsdb_idl_wait(txn->idl);
1474 ovsdb_idl_txn_wait(txn);
1481 ovsdb_idl_txn_get_increment_new_value(const struct ovsdb_idl_txn *txn)
1483 assert(txn->status == TXN_SUCCESS);
1484 return txn->inc_new_value;
1488 ovsdb_idl_txn_abort(struct ovsdb_idl_txn *txn)
1490 ovsdb_idl_txn_disassemble(txn);
1491 if (txn->status == TXN_INCOMPLETE) {
1492 txn->status = TXN_ABORTED;
1497 ovsdb_idl_txn_get_error(const struct ovsdb_idl_txn *txn)
1499 if (txn->status != TXN_ERROR) {
1500 return ovsdb_idl_txn_status_to_string(txn->status);
1501 } else if (txn->error) {
1504 return "no error details available";
1509 ovsdb_idl_txn_set_error_json(struct ovsdb_idl_txn *txn,
1510 const struct json *json)
1512 if (txn->error == NULL) {
1513 txn->error = json_to_string(json, JSSF_SORT);
1517 /* For transaction 'txn' that completed successfully, finds and returns the
1518 * permanent UUID that the database assigned to a newly inserted row, given the
1519 * 'uuid' that ovsdb_idl_txn_insert() assigned locally to that row.
1521 * Returns NULL if 'uuid' is not a UUID assigned by ovsdb_idl_txn_insert() or
1522 * if it was assigned by that function and then deleted by
1523 * ovsdb_idl_txn_delete() within the same transaction. (Rows that are inserted
1524 * and then deleted within a single transaction are never sent to the database
1525 * server, so it never assigns them a permanent UUID.) */
1527 ovsdb_idl_txn_get_insert_uuid(const struct ovsdb_idl_txn *txn,
1528 const struct uuid *uuid)
1530 const struct ovsdb_idl_txn_insert *insert;
1532 assert(txn->status == TXN_SUCCESS || txn->status == TXN_UNCHANGED);
1533 HMAP_FOR_EACH_IN_BUCKET (insert, struct ovsdb_idl_txn_insert, hmap_node,
1534 uuid_hash(uuid), &txn->inserted_rows) {
1535 if (uuid_equals(uuid, &insert->dummy)) {
1536 return &insert->real;
1543 ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
1544 enum ovsdb_idl_txn_status status)
1546 txn->status = status;
1547 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
1551 ovsdb_idl_txn_write(const struct ovsdb_idl_row *row_,
1552 const struct ovsdb_idl_column *column,
1553 struct ovsdb_datum *datum)
1555 struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1556 const struct ovsdb_idl_table_class *class = row->table->class;
1557 size_t column_idx = column - class->columns;
1559 assert(row->new != NULL);
1560 assert(column_idx < class->n_columns);
1561 assert(row->table->modes[column_idx] != OVSDB_IDL_MODE_NONE);
1563 if (hmap_node_is_null(&row->txn_node)) {
1564 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1565 uuid_hash(&row->uuid));
1567 if (row->old == row->new) {
1568 row->new = xmalloc(class->n_columns * sizeof *row->new);
1570 if (!row->written) {
1571 row->written = bitmap_allocate(class->n_columns);
1573 if (bitmap_is_set(row->written, column_idx)) {
1574 ovsdb_datum_destroy(&row->new[column_idx], &column->type);
1576 bitmap_set1(row->written, column_idx);
1578 row->new[column_idx] = *datum;
1579 (column->unparse)(row);
1580 (column->parse)(row, &row->new[column_idx]);
1584 ovsdb_idl_txn_verify(const struct ovsdb_idl_row *row_,
1585 const struct ovsdb_idl_column *column)
1587 struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1588 const struct ovsdb_idl_table_class *class = row->table->class;
1589 size_t column_idx = column - class->columns;
1591 assert(row->new != NULL);
1593 || (row->written && bitmap_is_set(row->written, column_idx))) {
1597 if (hmap_node_is_null(&row->txn_node)) {
1598 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1599 uuid_hash(&row->uuid));
1601 if (!row->prereqs) {
1602 row->prereqs = bitmap_allocate(class->n_columns);
1604 bitmap_set1(row->prereqs, column_idx);
1608 ovsdb_idl_txn_delete(const struct ovsdb_idl_row *row_)
1610 struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1612 assert(row->new != NULL);
1614 ovsdb_idl_row_unparse(row);
1615 ovsdb_idl_row_clear_new(row);
1616 assert(!row->prereqs);
1617 hmap_remove(&row->table->rows, &row->hmap_node);
1618 hmap_remove(&row->table->idl->txn->txn_rows, &row->txn_node);
1622 if (hmap_node_is_null(&row->txn_node)) {
1623 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1624 uuid_hash(&row->uuid));
1626 ovsdb_idl_row_clear_new(row);
1630 const struct ovsdb_idl_row *
1631 ovsdb_idl_txn_insert(struct ovsdb_idl_txn *txn,
1632 const struct ovsdb_idl_table_class *class,
1633 const struct uuid *uuid)
1635 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(class);
1638 assert(!ovsdb_idl_txn_get_row(txn, uuid));
1641 uuid_generate(&row->uuid);
1644 row->table = ovsdb_idl_table_from_class(txn->idl, class);
1645 row->new = xmalloc(class->n_columns * sizeof *row->new);
1646 hmap_insert(&row->table->rows, &row->hmap_node, uuid_hash(&row->uuid));
1647 hmap_insert(&txn->txn_rows, &row->txn_node, uuid_hash(&row->uuid));
1652 ovsdb_idl_txn_abort_all(struct ovsdb_idl *idl)
1654 struct ovsdb_idl_txn *txn;
1656 HMAP_FOR_EACH (txn, struct ovsdb_idl_txn, hmap_node,
1657 &idl->outstanding_txns) {
1658 ovsdb_idl_txn_complete(txn, TXN_TRY_AGAIN);
1662 static struct ovsdb_idl_txn *
1663 ovsdb_idl_txn_find(struct ovsdb_idl *idl, const struct json *id)
1665 struct ovsdb_idl_txn *txn;
1667 HMAP_FOR_EACH_WITH_HASH (txn, struct ovsdb_idl_txn, hmap_node,
1668 json_hash(id, 0), &idl->outstanding_txns) {
1669 if (json_equal(id, txn->request_id)) {
1677 check_json_type(const struct json *json, enum json_type type, const char *name)
1680 VLOG_WARN_RL(&syntax_rl, "%s is missing", name);
1682 } else if (json->type != type) {
1683 VLOG_WARN_RL(&syntax_rl, "%s is %s instead of %s",
1684 name, json_type_to_string(json->type),
1685 json_type_to_string(type));
1693 ovsdb_idl_txn_process_inc_reply(struct ovsdb_idl_txn *txn,
1694 const struct json_array *results)
1696 struct json *count, *rows, *row, *column;
1697 struct shash *mutate, *select;
1699 if (txn->inc_index + 2 > results->n) {
1700 VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
1701 "for increment (has %zu, needs %u)",
1702 results->n, txn->inc_index + 2);
1706 /* We know that this is a JSON object because the loop in
1707 * ovsdb_idl_txn_process_reply() checked. */
1708 mutate = json_object(results->elems[txn->inc_index]);
1709 count = shash_find_data(mutate, "count");
1710 if (!check_json_type(count, JSON_INTEGER, "\"mutate\" reply \"count\"")) {
1713 if (count->u.integer != 1) {
1714 VLOG_WARN_RL(&syntax_rl,
1715 "\"mutate\" reply \"count\" is %lld instead of 1",
1720 select = json_object(results->elems[txn->inc_index + 1]);
1721 rows = shash_find_data(select, "rows");
1722 if (!check_json_type(rows, JSON_ARRAY, "\"select\" reply \"rows\"")) {
1725 if (rows->u.array.n != 1) {
1726 VLOG_WARN_RL(&syntax_rl, "\"select\" reply \"rows\" has %zu elements "
1731 row = rows->u.array.elems[0];
1732 if (!check_json_type(row, JSON_OBJECT, "\"select\" reply row")) {
1735 column = shash_find_data(json_object(row), txn->inc_column);
1736 if (!check_json_type(column, JSON_INTEGER,
1737 "\"select\" reply inc column")) {
1740 txn->inc_new_value = column->u.integer;
1745 ovsdb_idl_txn_process_insert_reply(struct ovsdb_idl_txn_insert *insert,
1746 const struct json_array *results)
1748 static const struct ovsdb_base_type uuid_type = OVSDB_BASE_UUID_INIT;
1749 struct ovsdb_error *error;
1750 struct json *json_uuid;
1751 union ovsdb_atom uuid;
1752 struct shash *reply;
1754 if (insert->op_index >= results->n) {
1755 VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
1756 "for insert (has %zu, needs %u)",
1757 results->n, insert->op_index);
1761 /* We know that this is a JSON object because the loop in
1762 * ovsdb_idl_txn_process_reply() checked. */
1763 reply = json_object(results->elems[insert->op_index]);
1764 json_uuid = shash_find_data(reply, "uuid");
1765 if (!check_json_type(json_uuid, JSON_ARRAY, "\"insert\" reply \"uuid\"")) {
1769 error = ovsdb_atom_from_json(&uuid, &uuid_type, json_uuid, NULL);
1771 char *s = ovsdb_error_to_string(error);
1772 VLOG_WARN_RL(&syntax_rl, "\"insert\" reply \"uuid\" is not a JSON "
1778 insert->real = uuid.uuid;
1784 ovsdb_idl_txn_process_reply(struct ovsdb_idl *idl,
1785 const struct jsonrpc_msg *msg)
1787 struct ovsdb_idl_txn *txn;
1788 enum ovsdb_idl_txn_status status;
1790 txn = ovsdb_idl_txn_find(idl, msg->id);
1795 if (msg->type == JSONRPC_ERROR) {
1797 } else if (msg->result->type != JSON_ARRAY) {
1798 VLOG_WARN_RL(&syntax_rl, "reply to \"transact\" is not JSON array");
1801 struct json_array *ops = &msg->result->u.array;
1802 int hard_errors = 0;
1803 int soft_errors = 0;
1806 for (i = 0; i < ops->n; i++) {
1807 struct json *op = ops->elems[i];
1809 if (op->type == JSON_NULL) {
1810 /* This isn't an error in itself but indicates that some prior
1811 * operation failed, so make sure that we know about it. */
1813 } else if (op->type == JSON_OBJECT) {
1816 error = shash_find_data(json_object(op), "error");
1818 if (error->type == JSON_STRING) {
1819 if (!strcmp(error->u.string, "timed out")) {
1821 } else if (strcmp(error->u.string, "aborted")) {
1823 ovsdb_idl_txn_set_error_json(txn, op);
1827 ovsdb_idl_txn_set_error_json(txn, op);
1828 VLOG_WARN_RL(&syntax_rl,
1829 "\"error\" in reply is not JSON string");
1834 ovsdb_idl_txn_set_error_json(txn, op);
1835 VLOG_WARN_RL(&syntax_rl,
1836 "operation reply is not JSON null or object");
1840 if (!soft_errors && !hard_errors) {
1841 struct ovsdb_idl_txn_insert *insert;
1843 if (txn->inc_table && !ovsdb_idl_txn_process_inc_reply(txn, ops)) {
1847 HMAP_FOR_EACH (insert, struct ovsdb_idl_txn_insert, hmap_node,
1848 &txn->inserted_rows) {
1849 if (!ovsdb_idl_txn_process_insert_reply(insert, ops)) {
1855 status = (hard_errors ? TXN_ERROR
1856 : soft_errors ? TXN_TRY_AGAIN
1860 ovsdb_idl_txn_complete(txn, status);
1864 struct ovsdb_idl_txn *
1865 ovsdb_idl_txn_get(const struct ovsdb_idl_row *row)
1867 struct ovsdb_idl_txn *txn = row->table->idl->txn;
1868 assert(txn != NULL);
1873 ovsdb_idl_txn_get_idl (struct ovsdb_idl_txn *txn)