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, hmap_node, &table->rows) {
219 struct ovsdb_idl_arc *arc, *next_arc;
221 if (!ovsdb_idl_row_is_orphan(row)) {
222 ovsdb_idl_row_unparse(row);
224 LIST_FOR_EACH_SAFE (arc, next_arc, src_node, &row->src_arcs) {
227 /* No need to do anything with dst_arcs: some node has those arcs
228 * as forward arcs and will destroy them itself. */
230 ovsdb_idl_row_destroy(row);
239 /* Processes a batch of messages from the database server on 'idl'. Returns
240 * true if the database as seen through 'idl' changed, false if it did not
241 * change. The initial fetch of the entire contents of the remote database is
242 * considered to be one kind of change.
244 * When this function returns false, the client may continue to use any data
245 * structures it obtained from 'idl' in the past. But when it returns true,
246 * the client must not access any of these data structures again, because they
247 * could have freed or reused for other purposes.
249 * This function can return occasional false positives, that is, report that
250 * the database changed even though it didn't. This happens if the connection
251 * to the database drops and reconnects, which causes the database contents to
252 * be reloaded even if they didn't change. (It could also happen if the
253 * database server sends out a "change" that reflects what we already thought
254 * was in the database, but the database server is not supposed to do that.)
256 * As an alternative to checking the return value, the client may check for
257 * changes in the value returned by ovsdb_idl_get_seqno().
260 ovsdb_idl_run(struct ovsdb_idl *idl)
262 unsigned int initial_change_seqno = idl->change_seqno;
266 jsonrpc_session_run(idl->session);
267 for (i = 0; jsonrpc_session_is_connected(idl->session) && i < 50; i++) {
268 struct jsonrpc_msg *msg, *reply;
271 seqno = jsonrpc_session_get_seqno(idl->session);
272 if (idl->last_monitor_request_seqno != seqno) {
273 idl->last_monitor_request_seqno = seqno;
274 ovsdb_idl_txn_abort_all(idl);
275 ovsdb_idl_send_monitor_request(idl);
279 msg = jsonrpc_session_recv(idl->session);
285 if (msg->type == JSONRPC_NOTIFY
286 && !strcmp(msg->method, "update")
287 && msg->params->type == JSON_ARRAY
288 && msg->params->u.array.n == 2
289 && msg->params->u.array.elems[0]->type == JSON_NULL) {
290 ovsdb_idl_parse_update(idl, msg->params->u.array.elems[1]);
291 } else if (msg->type == JSONRPC_REPLY
292 && idl->monitor_request_id
293 && json_equal(idl->monitor_request_id, msg->id)) {
295 json_destroy(idl->monitor_request_id);
296 idl->monitor_request_id = NULL;
297 ovsdb_idl_clear(idl);
298 ovsdb_idl_parse_update(idl, msg->result);
299 } else if (msg->type == JSONRPC_REPLY
300 && msg->id && msg->id->type == JSON_STRING
301 && !strcmp(msg->id->u.string, "echo")) {
302 /* It's a reply to our echo request. Ignore it. */
303 } else if ((msg->type == JSONRPC_ERROR
304 || msg->type == JSONRPC_REPLY)
305 && ovsdb_idl_txn_process_reply(idl, msg)) {
306 /* ovsdb_idl_txn_process_reply() did everything needful. */
308 /* This can happen if ovsdb_idl_txn_destroy() is called to destroy
309 * a transaction before we receive the reply, so keep the log level
311 VLOG_DBG("%s: received unexpected %s message",
312 jsonrpc_session_get_name(idl->session),
313 jsonrpc_msg_type_to_string(msg->type));
316 jsonrpc_session_send(idl->session, reply);
318 jsonrpc_msg_destroy(msg);
321 return initial_change_seqno != idl->change_seqno;
324 /* Arranges for poll_block() to wake up when ovsdb_idl_run() has something to
325 * do or when activity occurs on a transaction on 'idl'. */
327 ovsdb_idl_wait(struct ovsdb_idl *idl)
329 jsonrpc_session_wait(idl->session);
330 jsonrpc_session_recv_wait(idl->session);
333 /* Returns a number that represents the state of 'idl'. When 'idl' is updated
334 * (by ovsdb_idl_run()), the return value changes. */
336 ovsdb_idl_get_seqno(const struct ovsdb_idl *idl)
338 return idl->change_seqno;
341 /* Returns true if 'idl' successfully connected to the remote database and
342 * retrieved its contents (even if the connection subsequently dropped and is
343 * in the process of reconnecting). If so, then 'idl' contains an atomic
344 * snapshot of the database's contents (but it might be arbitrarily old if the
345 * connection dropped).
347 * Returns false if 'idl' has never connected or retrieved the database's
348 * contents. If so, 'idl' is empty. */
350 ovsdb_idl_has_ever_connected(const struct ovsdb_idl *idl)
352 return ovsdb_idl_get_seqno(idl) != 0;
355 /* Forces 'idl' to drop its connection to the database and reconnect. In the
356 * meantime, the contents of 'idl' will not change. */
358 ovsdb_idl_force_reconnect(struct ovsdb_idl *idl)
360 jsonrpc_session_force_reconnect(idl->session);
364 ovsdb_idl_set_mode(struct ovsdb_idl *idl,
365 const struct ovsdb_idl_column *column,
366 enum ovsdb_idl_mode mode)
370 for (i = 0; i < idl->class->n_tables; i++) {
371 const struct ovsdb_idl_table *table = &idl->tables[i];
372 const struct ovsdb_idl_table_class *tc = table->class;
374 if (column >= tc->columns && column < &tc->columns[tc->n_columns]) {
375 unsigned char *modep = &table->modes[column - tc->columns];
376 assert(*modep == OVSDB_IDL_MODE_RW || *modep == mode);
385 /* By default, 'idl' replicates all of the columns in the remote database, and
386 * ovsdb_idl_run() returns true upon a change to any column in the database.
387 * Call this function to avoid alerting ovsdb_idl_run()'s caller upon changes
390 * This is useful for columns that a client treats as "write-only", that is, it
391 * updates them but doesn't want to get alerted about its own updates. It also
392 * won't be alerted about other clients' updates, so this is suitable only for
393 * use by a client that "owns" a particular column.
395 * The client must be careful not to retain pointers to data in 'column' across
396 * calls to ovsdb_idl_run(), even when that function returns false, because
397 * the client is not alerted to changes.
399 * This function should be called after ovsdb_idl_create(), but before the
400 * first call to ovsdb_idl_run(). For any given column, this function may be
401 * called or ovsdb_idl_omit() may be called, but not both. */
403 ovsdb_idl_set_write_only(struct ovsdb_idl *idl,
404 const struct ovsdb_idl_column *column)
406 ovsdb_idl_set_mode(idl, column, OVSDB_IDL_MODE_WO);
409 /* By default, 'idl' replicates all of the columns in the remote database.
410 * Call this function to omit replicating 'column'. This saves CPU time and
411 * bandwidth to the database.
413 * This function should be called after ovsdb_idl_create(), but before the
414 * first call to ovsdb_idl_run(). For any given column, this function may be
415 * called or ovsdb_idl_set_write_only() may be called, but not both. */
417 ovsdb_idl_omit(struct ovsdb_idl *idl, const struct ovsdb_idl_column *column)
419 ovsdb_idl_set_mode(idl, column, OVSDB_IDL_MODE_NONE);
423 ovsdb_idl_send_monitor_request(struct ovsdb_idl *idl)
425 struct json *monitor_requests;
426 struct jsonrpc_msg *msg;
429 monitor_requests = json_object_create();
430 for (i = 0; i < idl->class->n_tables; i++) {
431 const struct ovsdb_idl_table *table = &idl->tables[i];
432 const struct ovsdb_idl_table_class *tc = table->class;
433 struct json *monitor_request, *columns;
436 monitor_request = json_object_create();
437 columns = json_array_create_empty();
438 for (j = 0; j < tc->n_columns; j++) {
439 const struct ovsdb_idl_column *column = &tc->columns[j];
440 if (table->modes[j] != OVSDB_IDL_MODE_NONE) {
441 json_array_add(columns, json_string_create(column->name));
444 json_object_put(monitor_request, "columns", columns);
445 json_object_put(monitor_requests, tc->name, monitor_request);
448 json_destroy(idl->monitor_request_id);
449 msg = jsonrpc_create_request(
451 json_array_create_3(json_string_create(idl->class->database),
452 json_null_create(), monitor_requests),
453 &idl->monitor_request_id);
454 jsonrpc_session_send(idl->session, msg);
458 ovsdb_idl_parse_update(struct ovsdb_idl *idl, const struct json *table_updates)
460 struct ovsdb_error *error = ovsdb_idl_parse_update__(idl, table_updates);
462 if (!VLOG_DROP_WARN(&syntax_rl)) {
463 char *s = ovsdb_error_to_string(error);
464 VLOG_WARN_RL(&syntax_rl, "%s", s);
467 ovsdb_error_destroy(error);
471 static struct ovsdb_error *
472 ovsdb_idl_parse_update__(struct ovsdb_idl *idl,
473 const struct json *table_updates)
475 const struct shash_node *tables_node;
477 if (table_updates->type != JSON_OBJECT) {
478 return ovsdb_syntax_error(table_updates, NULL,
479 "<table-updates> is not an object");
481 SHASH_FOR_EACH (tables_node, json_object(table_updates)) {
482 const struct json *table_update = tables_node->data;
483 const struct shash_node *table_node;
484 struct ovsdb_idl_table *table;
486 table = shash_find_data(&idl->table_by_name, tables_node->name);
488 return ovsdb_syntax_error(
490 "<table-updates> includes unknown table \"%s\"",
494 if (table_update->type != JSON_OBJECT) {
495 return ovsdb_syntax_error(table_update, NULL,
496 "<table-update> for table \"%s\" is "
497 "not an object", table->class->name);
499 SHASH_FOR_EACH (table_node, json_object(table_update)) {
500 const struct json *row_update = table_node->data;
501 const struct json *old_json, *new_json;
504 if (!uuid_from_string(&uuid, table_node->name)) {
505 return ovsdb_syntax_error(table_update, NULL,
506 "<table-update> for table \"%s\" "
508 "\"%s\" as member name",
512 if (row_update->type != JSON_OBJECT) {
513 return ovsdb_syntax_error(row_update, NULL,
514 "<table-update> for table \"%s\" "
515 "contains <row-update> for %s that "
521 old_json = shash_find_data(json_object(row_update), "old");
522 new_json = shash_find_data(json_object(row_update), "new");
523 if (old_json && old_json->type != JSON_OBJECT) {
524 return ovsdb_syntax_error(old_json, NULL,
525 "\"old\" <row> is not object");
526 } else if (new_json && new_json->type != JSON_OBJECT) {
527 return ovsdb_syntax_error(new_json, NULL,
528 "\"new\" <row> is not object");
529 } else if ((old_json != NULL) + (new_json != NULL)
530 != shash_count(json_object(row_update))) {
531 return ovsdb_syntax_error(row_update, NULL,
532 "<row-update> contains unexpected "
534 } else if (!old_json && !new_json) {
535 return ovsdb_syntax_error(row_update, NULL,
536 "<row-update> missing \"old\" "
537 "and \"new\" members");
540 if (ovsdb_idl_process_update(table, &uuid, old_json, new_json)) {
549 static struct ovsdb_idl_row *
550 ovsdb_idl_get_row(struct ovsdb_idl_table *table, const struct uuid *uuid)
552 struct ovsdb_idl_row *row;
554 HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid), &table->rows) {
555 if (uuid_equals(&row->uuid, uuid)) {
562 /* Returns true if a column with mode OVSDB_IDL_MODE_RW changed, false
565 ovsdb_idl_process_update(struct ovsdb_idl_table *table,
566 const struct uuid *uuid, const struct json *old,
567 const struct json *new)
569 struct ovsdb_idl_row *row;
571 row = ovsdb_idl_get_row(table, uuid);
574 if (row && !ovsdb_idl_row_is_orphan(row)) {
575 /* XXX perhaps we should check the 'old' values? */
576 ovsdb_idl_delete_row(row);
578 VLOG_WARN_RL(&semantic_rl, "cannot delete missing row "UUID_FMT" "
580 UUID_ARGS(uuid), table->class->name);
586 ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
587 } else if (ovsdb_idl_row_is_orphan(row)) {
588 ovsdb_idl_insert_row(row, new);
590 VLOG_WARN_RL(&semantic_rl, "cannot add existing row "UUID_FMT" to "
591 "table %s", UUID_ARGS(uuid), table->class->name);
592 return ovsdb_idl_modify_row(row, new);
597 /* XXX perhaps we should check the 'old' values? */
598 if (!ovsdb_idl_row_is_orphan(row)) {
599 return ovsdb_idl_modify_row(row, new);
601 VLOG_WARN_RL(&semantic_rl, "cannot modify missing but "
602 "referenced row "UUID_FMT" in table %s",
603 UUID_ARGS(uuid), table->class->name);
604 ovsdb_idl_insert_row(row, new);
607 VLOG_WARN_RL(&semantic_rl, "cannot modify missing row "UUID_FMT" "
608 "in table %s", UUID_ARGS(uuid), table->class->name);
609 ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
616 /* Returns true if a column with mode OVSDB_IDL_MODE_RW changed, false
619 ovsdb_idl_row_update(struct ovsdb_idl_row *row, const struct json *row_json)
621 struct ovsdb_idl_table *table = row->table;
622 struct shash_node *node;
623 bool changed = false;
625 SHASH_FOR_EACH (node, json_object(row_json)) {
626 const char *column_name = node->name;
627 const struct ovsdb_idl_column *column;
628 struct ovsdb_datum datum;
629 struct ovsdb_error *error;
631 column = shash_find_data(&table->columns, column_name);
633 VLOG_WARN_RL(&syntax_rl, "unknown column %s updating row "UUID_FMT,
634 column_name, UUID_ARGS(&row->uuid));
638 error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
640 unsigned int column_idx = column - table->class->columns;
641 struct ovsdb_datum *old = &row->old[column_idx];
643 if (!ovsdb_datum_equals(old, &datum, &column->type)) {
644 ovsdb_datum_swap(old, &datum);
645 if (table->modes[column_idx] == OVSDB_IDL_MODE_RW) {
649 /* Didn't really change but the OVSDB monitor protocol always
650 * includes every value in a row. */
653 ovsdb_datum_destroy(&datum, &column->type);
655 char *s = ovsdb_error_to_string(error);
656 VLOG_WARN_RL(&syntax_rl, "error parsing column %s in row "UUID_FMT
657 " in table %s: %s", column_name,
658 UUID_ARGS(&row->uuid), table->class->name, s);
660 ovsdb_error_destroy(error);
666 /* When a row A refers to row B through a column with a "refTable" constraint,
667 * but row B does not exist, row B is called an "orphan row". Orphan rows
668 * should not persist, because the database enforces referential integrity, but
669 * they can appear transiently as changes from the database are received (the
670 * database doesn't try to topologically sort them and circular references mean
671 * it isn't always possible anyhow).
673 * This function returns true if 'row' is an orphan row, otherwise false.
676 ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *row)
678 return !row->old && !row->new;
681 /* Returns true if 'row' is conceptually part of the database as modified by
682 * the current transaction (if any), false otherwise.
684 * This function will return true if 'row' is not an orphan (see the comment on
685 * ovsdb_idl_row_is_orphan()) and:
687 * - 'row' exists in the database and has not been deleted within the
688 * current transaction (if any).
690 * - 'row' was inserted within the current transaction and has not been
691 * deleted. (In the latter case you should not have passed 'row' in at
692 * all, because ovsdb_idl_txn_delete() freed it.)
694 * This function will return false if 'row' is an orphan or if 'row' was
695 * deleted within the current transaction.
698 ovsdb_idl_row_exists(const struct ovsdb_idl_row *row)
700 return row->new != NULL;
704 ovsdb_idl_row_parse(struct ovsdb_idl_row *row)
706 const struct ovsdb_idl_table_class *class = row->table->class;
709 for (i = 0; i < class->n_columns; i++) {
710 const struct ovsdb_idl_column *c = &class->columns[i];
711 (c->parse)(row, &row->old[i]);
716 ovsdb_idl_row_unparse(struct ovsdb_idl_row *row)
718 const struct ovsdb_idl_table_class *class = row->table->class;
721 for (i = 0; i < class->n_columns; i++) {
722 const struct ovsdb_idl_column *c = &class->columns[i];
728 ovsdb_idl_row_clear_old(struct ovsdb_idl_row *row)
730 assert(row->old == row->new);
731 if (!ovsdb_idl_row_is_orphan(row)) {
732 const struct ovsdb_idl_table_class *class = row->table->class;
735 for (i = 0; i < class->n_columns; i++) {
736 ovsdb_datum_destroy(&row->old[i], &class->columns[i].type);
739 row->old = row->new = NULL;
744 ovsdb_idl_row_clear_new(struct ovsdb_idl_row *row)
746 if (row->old != row->new) {
748 const struct ovsdb_idl_table_class *class = row->table->class;
752 BITMAP_FOR_EACH_1 (i, class->n_columns, row->written) {
753 ovsdb_datum_destroy(&row->new[i], &class->columns[i].type);
765 ovsdb_idl_row_clear_arcs(struct ovsdb_idl_row *row, bool destroy_dsts)
767 struct ovsdb_idl_arc *arc, *next;
769 /* Delete all forward arcs. If 'destroy_dsts', destroy any orphaned rows
770 * that this causes to be unreferenced. */
771 LIST_FOR_EACH_SAFE (arc, next, src_node, &row->src_arcs) {
772 list_remove(&arc->dst_node);
774 && ovsdb_idl_row_is_orphan(arc->dst)
775 && list_is_empty(&arc->dst->dst_arcs)) {
776 ovsdb_idl_row_destroy(arc->dst);
780 list_init(&row->src_arcs);
783 /* Force nodes that reference 'row' to reparse. */
785 ovsdb_idl_row_reparse_backrefs(struct ovsdb_idl_row *row)
787 struct ovsdb_idl_arc *arc, *next;
789 /* This is trickier than it looks. ovsdb_idl_row_clear_arcs() will destroy
790 * 'arc', so we need to use the "safe" variant of list traversal. However,
791 * calling an ovsdb_idl_column's 'parse' function will add an arc
792 * equivalent to 'arc' to row->arcs. That could be a problem for
793 * traversal, but it adds it at the beginning of the list to prevent us
794 * from stumbling upon it again.
796 * (If duplicate arcs were possible then we would need to make sure that
797 * 'next' didn't also point into 'arc''s destination, but we forbid
798 * duplicate arcs.) */
799 LIST_FOR_EACH_SAFE (arc, next, dst_node, &row->dst_arcs) {
800 struct ovsdb_idl_row *ref = arc->src;
802 ovsdb_idl_row_unparse(ref);
803 ovsdb_idl_row_clear_arcs(ref, false);
804 ovsdb_idl_row_parse(ref);
808 static struct ovsdb_idl_row *
809 ovsdb_idl_row_create__(const struct ovsdb_idl_table_class *class)
811 struct ovsdb_idl_row *row = xzalloc(class->allocation_size);
812 list_init(&row->src_arcs);
813 list_init(&row->dst_arcs);
814 hmap_node_nullify(&row->txn_node);
818 static struct ovsdb_idl_row *
819 ovsdb_idl_row_create(struct ovsdb_idl_table *table, const struct uuid *uuid)
821 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(table->class);
822 hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
829 ovsdb_idl_row_destroy(struct ovsdb_idl_row *row)
832 ovsdb_idl_row_clear_old(row);
833 hmap_remove(&row->table->rows, &row->hmap_node);
839 ovsdb_idl_insert_row(struct ovsdb_idl_row *row, const struct json *row_json)
841 const struct ovsdb_idl_table_class *class = row->table->class;
844 assert(!row->old && !row->new);
845 row->old = row->new = xmalloc(class->n_columns * sizeof *row->old);
846 for (i = 0; i < class->n_columns; i++) {
847 ovsdb_datum_init_default(&row->old[i], &class->columns[i].type);
849 ovsdb_idl_row_update(row, row_json);
850 ovsdb_idl_row_parse(row);
852 ovsdb_idl_row_reparse_backrefs(row);
856 ovsdb_idl_delete_row(struct ovsdb_idl_row *row)
858 ovsdb_idl_row_unparse(row);
859 ovsdb_idl_row_clear_arcs(row, true);
860 ovsdb_idl_row_clear_old(row);
861 if (list_is_empty(&row->dst_arcs)) {
862 ovsdb_idl_row_destroy(row);
864 ovsdb_idl_row_reparse_backrefs(row);
868 /* Returns true if a column with mode OVSDB_IDL_MODE_RW changed, false
871 ovsdb_idl_modify_row(struct ovsdb_idl_row *row, const struct json *row_json)
875 ovsdb_idl_row_unparse(row);
876 ovsdb_idl_row_clear_arcs(row, true);
877 changed = ovsdb_idl_row_update(row, row_json);
878 ovsdb_idl_row_parse(row);
884 may_add_arc(const struct ovsdb_idl_row *src, const struct ovsdb_idl_row *dst)
886 const struct ovsdb_idl_arc *arc;
893 /* No duplicate arcs.
895 * We only need to test whether the first arc in dst->dst_arcs originates
896 * at 'src', since we add all of the arcs from a given source in a clump
897 * (in a single call to ovsdb_idl_row_parse()) and new arcs are always
898 * added at the front of the dst_arcs list. */
899 if (list_is_empty(&dst->dst_arcs)) {
902 arc = CONTAINER_OF(dst->dst_arcs.next, struct ovsdb_idl_arc, dst_node);
903 return arc->src != src;
906 static struct ovsdb_idl_table *
907 ovsdb_idl_table_from_class(const struct ovsdb_idl *idl,
908 const struct ovsdb_idl_table_class *table_class)
910 return &idl->tables[table_class - idl->class->tables];
913 struct ovsdb_idl_row *
914 ovsdb_idl_get_row_arc(struct ovsdb_idl_row *src,
915 struct ovsdb_idl_table_class *dst_table_class,
916 const struct uuid *dst_uuid)
918 struct ovsdb_idl *idl = src->table->idl;
919 struct ovsdb_idl_table *dst_table;
920 struct ovsdb_idl_arc *arc;
921 struct ovsdb_idl_row *dst;
923 dst_table = ovsdb_idl_table_from_class(idl, dst_table_class);
924 dst = ovsdb_idl_get_row(dst_table, dst_uuid);
926 /* We're being called from ovsdb_idl_txn_write(). We must not update
927 * any arcs, because the transaction will be backed out at commit or
928 * abort time and we don't want our graph screwed up.
930 * Just return the destination row, if there is one and it has not been
932 if (dst && (hmap_node_is_null(&dst->txn_node) || dst->new)) {
937 /* We're being called from some other context. Update the graph. */
939 dst = ovsdb_idl_row_create(dst_table, dst_uuid);
942 /* Add a new arc, if it wouldn't be a self-arc or a duplicate arc. */
943 if (may_add_arc(src, dst)) {
944 /* The arc *must* be added at the front of the dst_arcs list. See
945 * ovsdb_idl_row_reparse_backrefs() for details. */
946 arc = xmalloc(sizeof *arc);
947 list_push_front(&src->src_arcs, &arc->src_node);
948 list_push_front(&dst->dst_arcs, &arc->dst_node);
953 return !ovsdb_idl_row_is_orphan(dst) ? dst : NULL;
957 const struct ovsdb_idl_row *
958 ovsdb_idl_get_row_for_uuid(const struct ovsdb_idl *idl,
959 const struct ovsdb_idl_table_class *tc,
960 const struct uuid *uuid)
962 return ovsdb_idl_get_row(ovsdb_idl_table_from_class(idl, tc), uuid);
965 static struct ovsdb_idl_row *
966 next_real_row(struct ovsdb_idl_table *table, struct hmap_node *node)
968 for (; node; node = hmap_next(&table->rows, node)) {
969 struct ovsdb_idl_row *row;
971 row = CONTAINER_OF(node, struct ovsdb_idl_row, hmap_node);
972 if (ovsdb_idl_row_exists(row)) {
979 const struct ovsdb_idl_row *
980 ovsdb_idl_first_row(const struct ovsdb_idl *idl,
981 const struct ovsdb_idl_table_class *table_class)
983 struct ovsdb_idl_table *table
984 = ovsdb_idl_table_from_class(idl, table_class);
985 return next_real_row(table, hmap_first(&table->rows));
988 const struct ovsdb_idl_row *
989 ovsdb_idl_next_row(const struct ovsdb_idl_row *row)
991 struct ovsdb_idl_table *table = row->table;
993 return next_real_row(table, hmap_next(&table->rows, &row->hmap_node));
996 /* Reads and returns the value of 'column' within 'row'. If an ongoing
997 * transaction has changed 'column''s value, the modified value is returned.
999 * The caller must not modify or free the returned value.
1001 * Various kinds of changes can invalidate the returned value: writing to the
1002 * same 'column' in 'row' (e.g. with ovsdb_idl_txn_write()), deleting 'row'
1003 * (e.g. with ovsdb_idl_txn_delete()), or completing an ongoing transaction
1004 * (e.g. with ovsdb_idl_txn_commit() or ovsdb_idl_txn_abort()). If the
1005 * returned value is needed for a long time, it is best to make a copy of it
1006 * with ovsdb_datum_clone(). */
1007 const struct ovsdb_datum *
1008 ovsdb_idl_read(const struct ovsdb_idl_row *row,
1009 const struct ovsdb_idl_column *column)
1011 const struct ovsdb_idl_table_class *class = row->table->class;
1012 size_t column_idx = column - class->columns;
1014 assert(row->new != NULL);
1015 assert(column_idx < class->n_columns);
1017 if (row->written && bitmap_is_set(row->written, column_idx)) {
1018 return &row->new[column_idx];
1019 } else if (row->old) {
1020 return &row->old[column_idx];
1022 return ovsdb_datum_default(&column->type);
1026 /* Same as ovsdb_idl_read(), except that it also asserts that 'column' has key
1027 * type 'key_type' and value type 'value_type'. (Scalar and set types will
1028 * have a value type of OVSDB_TYPE_VOID.)
1030 * This is useful in code that "knows" that a particular column has a given
1031 * type, so that it will abort if someone changes the column's type without
1032 * updating the code that uses it. */
1033 const struct ovsdb_datum *
1034 ovsdb_idl_get(const struct ovsdb_idl_row *row,
1035 const struct ovsdb_idl_column *column,
1036 enum ovsdb_atomic_type key_type OVS_UNUSED,
1037 enum ovsdb_atomic_type value_type OVS_UNUSED)
1039 assert(column->type.key.type == key_type);
1040 assert(column->type.value.type == value_type);
1042 return ovsdb_idl_read(row, column);
1047 static void ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
1048 enum ovsdb_idl_txn_status);
1051 ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status status)
1056 case TXN_INCOMPLETE:
1057 return "incomplete";
1070 struct ovsdb_idl_txn *
1071 ovsdb_idl_txn_create(struct ovsdb_idl *idl)
1073 struct ovsdb_idl_txn *txn;
1076 idl->txn = txn = xmalloc(sizeof *txn);
1077 txn->request_id = NULL;
1079 hmap_init(&txn->txn_rows);
1080 txn->status = TXN_INCOMPLETE;
1082 txn->dry_run = false;
1083 ds_init(&txn->comment);
1085 txn->inc_table = NULL;
1086 txn->inc_column = NULL;
1087 txn->inc_where = NULL;
1089 hmap_init(&txn->inserted_rows);
1094 /* Appends 's', which is treated as a printf()-type format string, to the
1095 * comments that will be passed to the OVSDB server when 'txn' is committed.
1096 * (The comment will be committed to the OVSDB log, which "ovsdb-tool
1097 * show-log" can print in a relatively human-readable form.) */
1099 ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *txn, const char *s, ...)
1103 if (txn->comment.length) {
1104 ds_put_char(&txn->comment, '\n');
1108 ds_put_format_valist(&txn->comment, s, args);
1113 ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *txn)
1115 txn->dry_run = true;
1119 ovsdb_idl_txn_increment(struct ovsdb_idl_txn *txn, const char *table,
1120 const char *column, const struct json *where)
1122 assert(!txn->inc_table);
1123 txn->inc_table = xstrdup(table);
1124 txn->inc_column = xstrdup(column);
1125 txn->inc_where = where ? json_clone(where) : json_array_create_empty();
1129 ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *txn)
1131 struct ovsdb_idl_txn_insert *insert, *next;
1133 json_destroy(txn->request_id);
1134 if (txn->status == TXN_INCOMPLETE) {
1135 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
1137 ovsdb_idl_txn_abort(txn);
1138 ds_destroy(&txn->comment);
1140 free(txn->inc_table);
1141 free(txn->inc_column);
1142 json_destroy(txn->inc_where);
1143 HMAP_FOR_EACH_SAFE (insert, next, hmap_node, &txn->inserted_rows) {
1146 hmap_destroy(&txn->inserted_rows);
1151 ovsdb_idl_txn_wait(const struct ovsdb_idl_txn *txn)
1153 if (txn->status != TXN_INCOMPLETE) {
1154 poll_immediate_wake();
1158 static struct json *
1159 where_uuid_equals(const struct uuid *uuid)
1162 json_array_create_1(
1163 json_array_create_3(
1164 json_string_create("_uuid"),
1165 json_string_create("=="),
1166 json_array_create_2(
1167 json_string_create("uuid"),
1168 json_string_create_nocopy(
1169 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
1173 uuid_name_from_uuid(const struct uuid *uuid)
1178 name = xasprintf("row"UUID_FMT, UUID_ARGS(uuid));
1179 for (p = name; *p != '\0'; p++) {
1188 static const struct ovsdb_idl_row *
1189 ovsdb_idl_txn_get_row(const struct ovsdb_idl_txn *txn, const struct uuid *uuid)
1191 const struct ovsdb_idl_row *row;
1193 HMAP_FOR_EACH_WITH_HASH (row, txn_node, uuid_hash(uuid), &txn->txn_rows) {
1194 if (uuid_equals(&row->uuid, uuid)) {
1201 /* XXX there must be a cleaner way to do this */
1202 static struct json *
1203 substitute_uuids(struct json *json, const struct ovsdb_idl_txn *txn)
1205 if (json->type == JSON_ARRAY) {
1209 if (json->u.array.n == 2
1210 && json->u.array.elems[0]->type == JSON_STRING
1211 && json->u.array.elems[1]->type == JSON_STRING
1212 && !strcmp(json->u.array.elems[0]->u.string, "uuid")
1213 && uuid_from_string(&uuid, json->u.array.elems[1]->u.string)) {
1214 const struct ovsdb_idl_row *row;
1216 row = ovsdb_idl_txn_get_row(txn, &uuid);
1217 if (row && !row->old && row->new) {
1220 return json_array_create_2(
1221 json_string_create("named-uuid"),
1222 json_string_create_nocopy(uuid_name_from_uuid(&uuid)));
1226 for (i = 0; i < json->u.array.n; i++) {
1227 json->u.array.elems[i] = substitute_uuids(json->u.array.elems[i],
1230 } else if (json->type == JSON_OBJECT) {
1231 struct shash_node *node;
1233 SHASH_FOR_EACH (node, json_object(json)) {
1234 node->data = substitute_uuids(node->data, txn);
1241 ovsdb_idl_txn_disassemble(struct ovsdb_idl_txn *txn)
1243 struct ovsdb_idl_row *row, *next;
1245 /* This must happen early. Otherwise, ovsdb_idl_row_parse() will call an
1246 * ovsdb_idl_column's 'parse' function, which will call
1247 * ovsdb_idl_get_row_arc(), which will seen that the IDL is in a
1248 * transaction and fail to update the graph. */
1249 txn->idl->txn = NULL;
1251 HMAP_FOR_EACH_SAFE (row, next, txn_node, &txn->txn_rows) {
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, 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, 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, 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);
1542 /* Writes 'datum' to the specified 'column' in 'row_'. Updates both 'row_'
1543 * itself and the structs derived from it (e.g. the "struct ovsrec_*", for
1546 * 'datum' must have the correct type for its column. The IDL does not check
1547 * that it meets schema constraints, but ovsdb-server will do so at commit time
1548 * so it had better be correct.
1550 * A transaction must be in progress. Replication of 'column' must not have
1551 * been disabled (by calling ovsdb_idl_omit()).
1553 * Usually this function is used indirectly through one of the "set" functions
1554 * generated by ovsdb-idlc. */
1556 ovsdb_idl_txn_write(const struct ovsdb_idl_row *row_,
1557 const struct ovsdb_idl_column *column,
1558 struct ovsdb_datum *datum)
1560 struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1561 const struct ovsdb_idl_table_class *class = row->table->class;
1562 size_t column_idx = column - class->columns;
1564 assert(row->new != NULL);
1565 assert(column_idx < class->n_columns);
1566 assert(row->table->modes[column_idx] != OVSDB_IDL_MODE_NONE);
1568 if (hmap_node_is_null(&row->txn_node)) {
1569 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1570 uuid_hash(&row->uuid));
1572 if (row->old == row->new) {
1573 row->new = xmalloc(class->n_columns * sizeof *row->new);
1575 if (!row->written) {
1576 row->written = bitmap_allocate(class->n_columns);
1578 if (bitmap_is_set(row->written, column_idx)) {
1579 ovsdb_datum_destroy(&row->new[column_idx], &column->type);
1581 bitmap_set1(row->written, column_idx);
1583 row->new[column_idx] = *datum;
1584 (column->unparse)(row);
1585 (column->parse)(row, &row->new[column_idx]);
1588 /* Causes the original contents of 'column' in 'row_' to be verified as a
1589 * prerequisite to completing the transaction. That is, if 'column' in 'row_'
1590 * changed (or if 'row_' was deleted) between the time that the IDL originally
1591 * read its contents and the time that the transaction commits, then the
1592 * transaction aborts and ovsdb_idl_txn_commit() returns TXN_TRY_AGAIN.
1594 * The intention is that, to ensure that no transaction commits based on dirty
1595 * reads, an application should call ovsdb_idl_txn_verify() on each data item
1596 * read as part of a read-modify-write operation.
1598 * In some cases ovsdb_idl_txn_verify() reduces to a no-op, because the current
1599 * value of 'column' is already known:
1601 * - If 'row_' is a row created by the current transaction (returned by
1602 * ovsdb_idl_txn_insert()).
1604 * - If 'column' has already been modified (with ovsdb_idl_txn_write())
1605 * within the current transaction.
1607 * Because of the latter property, always call ovsdb_idl_txn_verify() *before*
1608 * ovsdb_idl_txn_write() for a given read-modify-write.
1610 * A transaction must be in progress.
1612 * Usually this function is used indirectly through one of the "verify"
1613 * functions generated by ovsdb-idlc. */
1615 ovsdb_idl_txn_verify(const struct ovsdb_idl_row *row_,
1616 const struct ovsdb_idl_column *column)
1618 struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1619 const struct ovsdb_idl_table_class *class = row->table->class;
1620 size_t column_idx = column - class->columns;
1622 assert(row->new != NULL);
1624 || (row->written && bitmap_is_set(row->written, column_idx))) {
1628 if (hmap_node_is_null(&row->txn_node)) {
1629 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1630 uuid_hash(&row->uuid));
1632 if (!row->prereqs) {
1633 row->prereqs = bitmap_allocate(class->n_columns);
1635 bitmap_set1(row->prereqs, column_idx);
1638 /* Deletes 'row_' from its table. May free 'row_', so it must not be
1639 * accessed afterward.
1641 * A transaction must be in progress.
1643 * Usually this function is used indirectly through one of the "delete"
1644 * functions generated by ovsdb-idlc. */
1646 ovsdb_idl_txn_delete(const struct ovsdb_idl_row *row_)
1648 struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1650 assert(row->new != NULL);
1652 ovsdb_idl_row_unparse(row);
1653 ovsdb_idl_row_clear_new(row);
1654 assert(!row->prereqs);
1655 hmap_remove(&row->table->rows, &row->hmap_node);
1656 hmap_remove(&row->table->idl->txn->txn_rows, &row->txn_node);
1660 if (hmap_node_is_null(&row->txn_node)) {
1661 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1662 uuid_hash(&row->uuid));
1664 ovsdb_idl_row_clear_new(row);
1668 /* Inserts and returns a new row in the table with the specified 'class' in the
1669 * database with open transaction 'txn'.
1671 * The new row is assigned a provisional UUID. If 'uuid' is null then one is
1672 * randomly generated; otherwise 'uuid' should specify a randomly generated
1673 * UUID not otherwise in use. ovsdb-server will assign a different UUID when
1674 * 'txn' is committed, but the IDL will replace any uses of the provisional
1675 * UUID in the data to be to be committed by the UUID assigned by
1678 * Usually this function is used indirectly through one of the "insert"
1679 * functions generated by ovsdb-idlc. */
1680 const struct ovsdb_idl_row *
1681 ovsdb_idl_txn_insert(struct ovsdb_idl_txn *txn,
1682 const struct ovsdb_idl_table_class *class,
1683 const struct uuid *uuid)
1685 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(class);
1688 assert(!ovsdb_idl_txn_get_row(txn, uuid));
1691 uuid_generate(&row->uuid);
1694 row->table = ovsdb_idl_table_from_class(txn->idl, class);
1695 row->new = xmalloc(class->n_columns * sizeof *row->new);
1696 hmap_insert(&row->table->rows, &row->hmap_node, uuid_hash(&row->uuid));
1697 hmap_insert(&txn->txn_rows, &row->txn_node, uuid_hash(&row->uuid));
1702 ovsdb_idl_txn_abort_all(struct ovsdb_idl *idl)
1704 struct ovsdb_idl_txn *txn;
1706 HMAP_FOR_EACH (txn, hmap_node, &idl->outstanding_txns) {
1707 ovsdb_idl_txn_complete(txn, TXN_TRY_AGAIN);
1711 static struct ovsdb_idl_txn *
1712 ovsdb_idl_txn_find(struct ovsdb_idl *idl, const struct json *id)
1714 struct ovsdb_idl_txn *txn;
1716 HMAP_FOR_EACH_WITH_HASH (txn, hmap_node,
1717 json_hash(id, 0), &idl->outstanding_txns) {
1718 if (json_equal(id, txn->request_id)) {
1726 check_json_type(const struct json *json, enum json_type type, const char *name)
1729 VLOG_WARN_RL(&syntax_rl, "%s is missing", name);
1731 } else if (json->type != type) {
1732 VLOG_WARN_RL(&syntax_rl, "%s is %s instead of %s",
1733 name, json_type_to_string(json->type),
1734 json_type_to_string(type));
1742 ovsdb_idl_txn_process_inc_reply(struct ovsdb_idl_txn *txn,
1743 const struct json_array *results)
1745 struct json *count, *rows, *row, *column;
1746 struct shash *mutate, *select;
1748 if (txn->inc_index + 2 > results->n) {
1749 VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
1750 "for increment (has %zu, needs %u)",
1751 results->n, txn->inc_index + 2);
1755 /* We know that this is a JSON object because the loop in
1756 * ovsdb_idl_txn_process_reply() checked. */
1757 mutate = json_object(results->elems[txn->inc_index]);
1758 count = shash_find_data(mutate, "count");
1759 if (!check_json_type(count, JSON_INTEGER, "\"mutate\" reply \"count\"")) {
1762 if (count->u.integer != 1) {
1763 VLOG_WARN_RL(&syntax_rl,
1764 "\"mutate\" reply \"count\" is %lld instead of 1",
1769 select = json_object(results->elems[txn->inc_index + 1]);
1770 rows = shash_find_data(select, "rows");
1771 if (!check_json_type(rows, JSON_ARRAY, "\"select\" reply \"rows\"")) {
1774 if (rows->u.array.n != 1) {
1775 VLOG_WARN_RL(&syntax_rl, "\"select\" reply \"rows\" has %zu elements "
1780 row = rows->u.array.elems[0];
1781 if (!check_json_type(row, JSON_OBJECT, "\"select\" reply row")) {
1784 column = shash_find_data(json_object(row), txn->inc_column);
1785 if (!check_json_type(column, JSON_INTEGER,
1786 "\"select\" reply inc column")) {
1789 txn->inc_new_value = column->u.integer;
1794 ovsdb_idl_txn_process_insert_reply(struct ovsdb_idl_txn_insert *insert,
1795 const struct json_array *results)
1797 static const struct ovsdb_base_type uuid_type = OVSDB_BASE_UUID_INIT;
1798 struct ovsdb_error *error;
1799 struct json *json_uuid;
1800 union ovsdb_atom uuid;
1801 struct shash *reply;
1803 if (insert->op_index >= results->n) {
1804 VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
1805 "for insert (has %zu, needs %u)",
1806 results->n, insert->op_index);
1810 /* We know that this is a JSON object because the loop in
1811 * ovsdb_idl_txn_process_reply() checked. */
1812 reply = json_object(results->elems[insert->op_index]);
1813 json_uuid = shash_find_data(reply, "uuid");
1814 if (!check_json_type(json_uuid, JSON_ARRAY, "\"insert\" reply \"uuid\"")) {
1818 error = ovsdb_atom_from_json(&uuid, &uuid_type, json_uuid, NULL);
1820 char *s = ovsdb_error_to_string(error);
1821 VLOG_WARN_RL(&syntax_rl, "\"insert\" reply \"uuid\" is not a JSON "
1827 insert->real = uuid.uuid;
1833 ovsdb_idl_txn_process_reply(struct ovsdb_idl *idl,
1834 const struct jsonrpc_msg *msg)
1836 struct ovsdb_idl_txn *txn;
1837 enum ovsdb_idl_txn_status status;
1839 txn = ovsdb_idl_txn_find(idl, msg->id);
1844 if (msg->type == JSONRPC_ERROR) {
1846 } else if (msg->result->type != JSON_ARRAY) {
1847 VLOG_WARN_RL(&syntax_rl, "reply to \"transact\" is not JSON array");
1850 struct json_array *ops = &msg->result->u.array;
1851 int hard_errors = 0;
1852 int soft_errors = 0;
1855 for (i = 0; i < ops->n; i++) {
1856 struct json *op = ops->elems[i];
1858 if (op->type == JSON_NULL) {
1859 /* This isn't an error in itself but indicates that some prior
1860 * operation failed, so make sure that we know about it. */
1862 } else if (op->type == JSON_OBJECT) {
1865 error = shash_find_data(json_object(op), "error");
1867 if (error->type == JSON_STRING) {
1868 if (!strcmp(error->u.string, "timed out")) {
1870 } else if (strcmp(error->u.string, "aborted")) {
1872 ovsdb_idl_txn_set_error_json(txn, op);
1876 ovsdb_idl_txn_set_error_json(txn, op);
1877 VLOG_WARN_RL(&syntax_rl,
1878 "\"error\" in reply is not JSON string");
1883 ovsdb_idl_txn_set_error_json(txn, op);
1884 VLOG_WARN_RL(&syntax_rl,
1885 "operation reply is not JSON null or object");
1889 if (!soft_errors && !hard_errors) {
1890 struct ovsdb_idl_txn_insert *insert;
1892 if (txn->inc_table && !ovsdb_idl_txn_process_inc_reply(txn, ops)) {
1896 HMAP_FOR_EACH (insert, hmap_node, &txn->inserted_rows) {
1897 if (!ovsdb_idl_txn_process_insert_reply(insert, ops)) {
1903 status = (hard_errors ? TXN_ERROR
1904 : soft_errors ? TXN_TRY_AGAIN
1908 ovsdb_idl_txn_complete(txn, status);
1912 struct ovsdb_idl_txn *
1913 ovsdb_idl_txn_get(const struct ovsdb_idl_row *row)
1915 struct ovsdb_idl_txn *txn = row->table->idl->txn;
1916 assert(txn != NULL);
1921 ovsdb_idl_txn_get_idl (struct ovsdb_idl_txn *txn)