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"
30 #include "ovsdb-data.h"
31 #include "ovsdb-error.h"
32 #include "ovsdb-idl-provider.h"
33 #include "poll-loop.h"
37 #define THIS_MODULE VLM_ovsdb_idl
40 /* An arc from one idl_row to another. When row A contains a UUID that
41 * references row B, this is represented by an arc from A (the source) to B
44 * Arcs from a row to itself are omitted, that is, src and dst are always
47 * Arcs are never duplicated, that is, even if there are multiple references
48 * from A to B, there is only a single arc from A to B.
50 * Arcs are directed: an arc from A to B is the converse of an an arc from B to
51 * A. Both an arc and its converse may both be present, if each row refers
52 * to the other circularly.
54 * The source and destination row may be in the same table or in different
57 struct ovsdb_idl_arc {
58 struct list src_node; /* In src->src_arcs list. */
59 struct list dst_node; /* In dst->dst_arcs list. */
60 struct ovsdb_idl_row *src; /* Source row. */
61 struct ovsdb_idl_row *dst; /* Destination row. */
65 const struct ovsdb_idl_class *class;
66 struct jsonrpc_session *session;
67 struct shash table_by_name;
68 struct ovsdb_idl_table *tables;
69 struct json *monitor_request_id;
70 unsigned int last_monitor_request_seqno;
71 unsigned int change_seqno;
73 /* Transaction support. */
74 struct ovsdb_idl_txn *txn;
75 struct hmap outstanding_txns;
78 struct ovsdb_idl_txn {
79 struct hmap_node hmap_node;
80 struct json *request_id;
81 struct ovsdb_idl *idl;
83 enum ovsdb_idl_txn_status status;
90 struct json *inc_where;
91 unsigned int inc_index;
92 int64_t inc_new_value;
95 static struct vlog_rate_limit syntax_rl = VLOG_RATE_LIMIT_INIT(1, 5);
96 static struct vlog_rate_limit semantic_rl = VLOG_RATE_LIMIT_INIT(1, 5);
98 static void ovsdb_idl_clear(struct ovsdb_idl *);
99 static void ovsdb_idl_send_monitor_request(struct ovsdb_idl *);
100 static void ovsdb_idl_parse_update(struct ovsdb_idl *, const struct json *);
101 static struct ovsdb_error *ovsdb_idl_parse_update__(struct ovsdb_idl *,
102 const struct json *);
103 static void ovsdb_idl_process_update(struct ovsdb_idl_table *,
105 const struct json *old,
106 const struct json *new);
107 static void ovsdb_idl_insert_row(struct ovsdb_idl_row *, const struct json *);
108 static void ovsdb_idl_delete_row(struct ovsdb_idl_row *);
109 static void ovsdb_idl_modify_row(struct ovsdb_idl_row *, const struct json *);
111 static bool ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *);
112 static struct ovsdb_idl_row *ovsdb_idl_row_create__(
113 const struct ovsdb_idl_table_class *);
114 static struct ovsdb_idl_row *ovsdb_idl_row_create(struct ovsdb_idl_table *,
115 const struct uuid *);
116 static void ovsdb_idl_row_destroy(struct ovsdb_idl_row *);
118 static void ovsdb_idl_row_parse(struct ovsdb_idl_row *);
119 static void ovsdb_idl_row_unparse(struct ovsdb_idl_row *);
120 static void ovsdb_idl_row_clear_old(struct ovsdb_idl_row *);
121 static void ovsdb_idl_row_clear_new(struct ovsdb_idl_row *);
123 static void ovsdb_idl_txn_abort_all(struct ovsdb_idl *);
124 static bool ovsdb_idl_txn_process_reply(struct ovsdb_idl *,
125 const struct jsonrpc_msg *msg);
128 ovsdb_idl_create(const char *remote, const struct ovsdb_idl_class *class)
130 struct ovsdb_idl *idl;
133 idl = xzalloc(sizeof *idl);
135 idl->session = jsonrpc_session_open(remote);
136 shash_init(&idl->table_by_name);
137 idl->tables = xmalloc(class->n_tables * sizeof *idl->tables);
138 for (i = 0; i < class->n_tables; i++) {
139 const struct ovsdb_idl_table_class *tc = &class->tables[i];
140 struct ovsdb_idl_table *table = &idl->tables[i];
143 assert(!shash_find(&idl->table_by_name, tc->name));
144 shash_add(&idl->table_by_name, tc->name, table);
146 shash_init(&table->columns);
147 for (j = 0; j < tc->n_columns; j++) {
148 const struct ovsdb_idl_column *column = &tc->columns[j];
150 assert(!shash_find(&table->columns, column->name));
151 shash_add(&table->columns, column->name, column);
153 hmap_init(&table->rows);
156 idl->last_monitor_request_seqno = UINT_MAX;
157 hmap_init(&idl->outstanding_txns);
163 ovsdb_idl_destroy(struct ovsdb_idl *idl)
169 ovsdb_idl_clear(idl);
170 jsonrpc_session_close(idl->session);
172 for (i = 0; i < idl->class->n_tables; i++) {
173 struct ovsdb_idl_table *table = &idl->tables[i];
174 shash_destroy(&table->columns);
175 hmap_destroy(&table->rows);
177 shash_destroy(&idl->table_by_name);
179 json_destroy(idl->monitor_request_id);
185 ovsdb_idl_clear(struct ovsdb_idl *idl)
187 bool changed = false;
190 for (i = 0; i < idl->class->n_tables; i++) {
191 struct ovsdb_idl_table *table = &idl->tables[i];
192 struct ovsdb_idl_row *row, *next_row;
194 if (hmap_is_empty(&table->rows)) {
199 HMAP_FOR_EACH_SAFE (row, next_row, struct ovsdb_idl_row, hmap_node,
201 struct ovsdb_idl_arc *arc, *next_arc;
203 if (!ovsdb_idl_row_is_orphan(row)) {
204 ovsdb_idl_row_unparse(row);
205 ovsdb_idl_row_clear_old(row);
207 hmap_remove(&table->rows, &row->hmap_node);
208 LIST_FOR_EACH_SAFE (arc, next_arc, struct ovsdb_idl_arc, src_node,
212 /* No need to do anything with dst_arcs: some node has those arcs
213 * as forward arcs and will destroy them itself. */
225 ovsdb_idl_run(struct ovsdb_idl *idl)
230 jsonrpc_session_run(idl->session);
231 for (i = 0; jsonrpc_session_is_connected(idl->session) && i < 50; i++) {
232 struct jsonrpc_msg *msg, *reply;
235 seqno = jsonrpc_session_get_seqno(idl->session);
236 if (idl->last_monitor_request_seqno != seqno) {
237 idl->last_monitor_request_seqno = seqno;
238 ovsdb_idl_txn_abort_all(idl);
239 ovsdb_idl_send_monitor_request(idl);
243 msg = jsonrpc_session_recv(idl->session);
249 if (msg->type == JSONRPC_NOTIFY
250 && !strcmp(msg->method, "update")
251 && msg->params->type == JSON_ARRAY
252 && msg->params->u.array.n == 2
253 && msg->params->u.array.elems[0]->type == JSON_NULL) {
254 ovsdb_idl_parse_update(idl, msg->params->u.array.elems[1]);
255 } else if (msg->type == JSONRPC_REPLY
256 && idl->monitor_request_id
257 && json_equal(idl->monitor_request_id, msg->id)) {
258 json_destroy(idl->monitor_request_id);
259 idl->monitor_request_id = NULL;
260 ovsdb_idl_clear(idl);
261 ovsdb_idl_parse_update(idl, msg->result);
262 } else if (msg->type == JSONRPC_REPLY
263 && msg->id && msg->id->type == JSON_STRING
264 && !strcmp(msg->id->u.string, "echo")) {
265 /* It's a reply to our echo request. Ignore it. */
266 } else if ((msg->type == JSONRPC_ERROR
267 || msg->type == JSONRPC_REPLY)
268 && ovsdb_idl_txn_process_reply(idl, msg)) {
269 /* ovsdb_idl_txn_process_reply() did everything needful. */
271 /* This can happen if ovsdb_idl_txn_destroy() is called to destroy
272 * a transaction before we receive the reply, so keep the log level
274 VLOG_DBG("%s: received unexpected %s message",
275 jsonrpc_session_get_name(idl->session),
276 jsonrpc_msg_type_to_string(msg->type));
279 jsonrpc_session_send(idl->session, reply);
281 jsonrpc_msg_destroy(msg);
286 ovsdb_idl_wait(struct ovsdb_idl *idl)
288 jsonrpc_session_wait(idl->session);
289 jsonrpc_session_recv_wait(idl->session);
293 ovsdb_idl_get_seqno(const struct ovsdb_idl *idl)
295 return idl->change_seqno;
299 ovsdb_idl_has_ever_connected(const struct ovsdb_idl *idl)
301 return ovsdb_idl_get_seqno(idl) != 0;
305 ovsdb_idl_force_reconnect(struct ovsdb_idl *idl)
307 jsonrpc_session_force_reconnect(idl->session);
311 ovsdb_idl_send_monitor_request(struct ovsdb_idl *idl)
313 struct json *monitor_requests;
314 struct jsonrpc_msg *msg;
317 monitor_requests = json_object_create();
318 for (i = 0; i < idl->class->n_tables; i++) {
319 const struct ovsdb_idl_table *table = &idl->tables[i];
320 const struct ovsdb_idl_table_class *tc = table->class;
321 struct json *monitor_request, *columns;
324 monitor_request = json_object_create();
325 columns = json_array_create_empty();
326 for (i = 0; i < tc->n_columns; i++) {
327 const struct ovsdb_idl_column *column = &tc->columns[i];
328 json_array_add(columns, json_string_create(column->name));
330 json_object_put(monitor_request, "columns", columns);
331 json_object_put(monitor_requests, tc->name, monitor_request);
334 json_destroy(idl->monitor_request_id);
335 msg = jsonrpc_create_request(
336 "monitor", json_array_create_2(json_null_create(), monitor_requests),
337 &idl->monitor_request_id);
338 jsonrpc_session_send(idl->session, msg);
342 ovsdb_idl_parse_update(struct ovsdb_idl *idl, const struct json *table_updates)
344 struct ovsdb_error *error;
348 error = ovsdb_idl_parse_update__(idl, table_updates);
350 if (!VLOG_DROP_WARN(&syntax_rl)) {
351 char *s = ovsdb_error_to_string(error);
352 VLOG_WARN_RL(&syntax_rl, "%s", s);
355 ovsdb_error_destroy(error);
359 static struct ovsdb_error *
360 ovsdb_idl_parse_update__(struct ovsdb_idl *idl,
361 const struct json *table_updates)
363 const struct shash_node *tables_node;
365 if (table_updates->type != JSON_OBJECT) {
366 return ovsdb_syntax_error(table_updates, NULL,
367 "<table-updates> is not an object");
369 SHASH_FOR_EACH (tables_node, json_object(table_updates)) {
370 const struct json *table_update = tables_node->data;
371 const struct shash_node *table_node;
372 struct ovsdb_idl_table *table;
374 table = shash_find_data(&idl->table_by_name, tables_node->name);
376 return ovsdb_syntax_error(
378 "<table-updates> includes unknown table \"%s\"",
382 if (table_update->type != JSON_OBJECT) {
383 return ovsdb_syntax_error(table_update, NULL,
384 "<table-update> for table \"%s\" is "
385 "not an object", table->class->name);
387 SHASH_FOR_EACH (table_node, json_object(table_update)) {
388 const struct json *row_update = table_node->data;
389 const struct json *old_json, *new_json;
392 if (!uuid_from_string(&uuid, table_node->name)) {
393 return ovsdb_syntax_error(table_update, NULL,
394 "<table-update> for table \"%s\" "
396 "\"%s\" as member name",
400 if (row_update->type != JSON_OBJECT) {
401 return ovsdb_syntax_error(row_update, NULL,
402 "<table-update> for table \"%s\" "
403 "contains <row-update> for %s that "
409 old_json = shash_find_data(json_object(row_update), "old");
410 new_json = shash_find_data(json_object(row_update), "new");
411 if (old_json && old_json->type != JSON_OBJECT) {
412 return ovsdb_syntax_error(old_json, NULL,
413 "\"old\" <row> is not object");
414 } else if (new_json && new_json->type != JSON_OBJECT) {
415 return ovsdb_syntax_error(new_json, NULL,
416 "\"new\" <row> is not object");
417 } else if ((old_json != NULL) + (new_json != NULL)
418 != shash_count(json_object(row_update))) {
419 return ovsdb_syntax_error(row_update, NULL,
420 "<row-update> contains unexpected "
422 } else if (!old_json && !new_json) {
423 return ovsdb_syntax_error(row_update, NULL,
424 "<row-update> missing \"old\" "
425 "and \"new\" members");
428 ovsdb_idl_process_update(table, &uuid, old_json, new_json);
435 static struct ovsdb_idl_row *
436 ovsdb_idl_get_row(struct ovsdb_idl_table *table, const struct uuid *uuid)
438 struct ovsdb_idl_row *row;
440 HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_idl_row, hmap_node,
441 uuid_hash(uuid), &table->rows) {
442 if (uuid_equals(&row->uuid, uuid)) {
450 ovsdb_idl_process_update(struct ovsdb_idl_table *table,
451 const struct uuid *uuid, const struct json *old,
452 const struct json *new)
454 struct ovsdb_idl_row *row;
456 row = ovsdb_idl_get_row(table, uuid);
459 if (row && !ovsdb_idl_row_is_orphan(row)) {
460 /* XXX perhaps we should check the 'old' values? */
461 ovsdb_idl_delete_row(row);
463 VLOG_WARN_RL(&semantic_rl, "cannot delete missing row "UUID_FMT" "
465 UUID_ARGS(uuid), table->class->name);
470 ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
471 } else if (ovsdb_idl_row_is_orphan(row)) {
472 ovsdb_idl_insert_row(row, new);
474 VLOG_WARN_RL(&semantic_rl, "cannot add existing row "UUID_FMT" to "
475 "table %s", UUID_ARGS(uuid), table->class->name);
476 ovsdb_idl_modify_row(row, new);
481 /* XXX perhaps we should check the 'old' values? */
482 if (!ovsdb_idl_row_is_orphan(row)) {
483 ovsdb_idl_modify_row(row, new);
485 VLOG_WARN_RL(&semantic_rl, "cannot modify missing but "
486 "referenced row "UUID_FMT" in table %s",
487 UUID_ARGS(uuid), table->class->name);
488 ovsdb_idl_insert_row(row, new);
491 VLOG_WARN_RL(&semantic_rl, "cannot modify missing row "UUID_FMT" "
492 "in table %s", UUID_ARGS(uuid), table->class->name);
493 ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
499 ovsdb_idl_row_update(struct ovsdb_idl_row *row, const struct json *row_json)
501 struct ovsdb_idl_table *table = row->table;
502 struct shash_node *node;
504 SHASH_FOR_EACH (node, json_object(row_json)) {
505 const char *column_name = node->name;
506 const struct ovsdb_idl_column *column;
507 struct ovsdb_datum datum;
508 struct ovsdb_error *error;
510 column = shash_find_data(&table->columns, column_name);
512 VLOG_WARN_RL(&syntax_rl, "unknown column %s updating row "UUID_FMT,
513 column_name, UUID_ARGS(&row->uuid));
517 error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
519 ovsdb_datum_swap(&row->old[column - table->class->columns],
521 ovsdb_datum_destroy(&datum, &column->type);
523 char *s = ovsdb_error_to_string(error);
524 VLOG_WARN_RL(&syntax_rl, "error parsing column %s in row "UUID_FMT
525 " in table %s: %s", column_name,
526 UUID_ARGS(&row->uuid), table->class->name, s);
528 ovsdb_error_destroy(error);
534 ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *row)
540 ovsdb_idl_row_parse(struct ovsdb_idl_row *row)
542 const struct ovsdb_idl_table_class *class = row->table->class;
545 for (i = 0; i < class->n_columns; i++) {
546 const struct ovsdb_idl_column *c = &class->columns[i];
547 (c->parse)(row, &row->old[i]);
552 ovsdb_idl_row_unparse(struct ovsdb_idl_row *row)
554 const struct ovsdb_idl_table_class *class = row->table->class;
557 for (i = 0; i < class->n_columns; i++) {
558 const struct ovsdb_idl_column *c = &class->columns[i];
564 ovsdb_idl_row_clear_old(struct ovsdb_idl_row *row)
566 assert(row->old == row->new);
567 if (!ovsdb_idl_row_is_orphan(row)) {
568 const struct ovsdb_idl_table_class *class = row->table->class;
571 for (i = 0; i < class->n_columns; i++) {
572 ovsdb_datum_destroy(&row->old[i], &class->columns[i].type);
575 row->old = row->new = NULL;
580 ovsdb_idl_row_clear_new(struct ovsdb_idl_row *row)
582 if (row->old != row->new) {
584 const struct ovsdb_idl_table_class *class = row->table->class;
587 BITMAP_FOR_EACH_1 (i, class->n_columns, row->written) {
588 ovsdb_datum_destroy(&row->new[i], &class->columns[i].type);
599 ovsdb_idl_row_clear_arcs(struct ovsdb_idl_row *row, bool destroy_dsts)
601 struct ovsdb_idl_arc *arc, *next;
603 /* Delete all forward arcs. If 'destroy_dsts', destroy any orphaned rows
604 * that this causes to be unreferenced. */
605 LIST_FOR_EACH_SAFE (arc, next, struct ovsdb_idl_arc, src_node,
607 list_remove(&arc->dst_node);
609 && ovsdb_idl_row_is_orphan(arc->dst)
610 && list_is_empty(&arc->dst->dst_arcs)) {
611 ovsdb_idl_row_destroy(arc->dst);
615 list_init(&row->src_arcs);
618 /* Force nodes that reference 'row' to reparse. */
620 ovsdb_idl_row_reparse_backrefs(struct ovsdb_idl_row *row, bool destroy_dsts)
622 struct ovsdb_idl_arc *arc, *next;
624 /* This is trickier than it looks. ovsdb_idl_row_clear_arcs() will destroy
625 * 'arc', so we need to use the "safe" variant of list traversal. However,
626 * calling an ovsdb_idl_column's 'parse' function will add an arc
627 * equivalent to 'arc' to row->arcs. That could be a problem for
628 * traversal, but it adds it at the beginning of the list to prevent us
629 * from stumbling upon it again.
631 * (If duplicate arcs were possible then we would need to make sure that
632 * 'next' didn't also point into 'arc''s destination, but we forbid
633 * duplicate arcs.) */
634 LIST_FOR_EACH_SAFE (arc, next, struct ovsdb_idl_arc, dst_node,
636 struct ovsdb_idl_row *ref = arc->src;
638 ovsdb_idl_row_unparse(ref);
639 ovsdb_idl_row_clear_arcs(ref, destroy_dsts);
640 ovsdb_idl_row_parse(ref);
644 static struct ovsdb_idl_row *
645 ovsdb_idl_row_create__(const struct ovsdb_idl_table_class *class)
647 struct ovsdb_idl_row *row = xzalloc(class->allocation_size);
648 list_init(&row->src_arcs);
649 list_init(&row->dst_arcs);
650 hmap_node_nullify(&row->txn_node);
654 static struct ovsdb_idl_row *
655 ovsdb_idl_row_create(struct ovsdb_idl_table *table, const struct uuid *uuid)
657 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(table->class);
658 hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
665 ovsdb_idl_row_destroy(struct ovsdb_idl_row *row)
668 ovsdb_idl_row_clear_old(row);
669 hmap_remove(&row->table->rows, &row->hmap_node);
675 ovsdb_idl_insert_row(struct ovsdb_idl_row *row, const struct json *row_json)
677 const struct ovsdb_idl_table_class *class = row->table->class;
680 assert(!row->old && !row->new);
681 row->old = row->new = xmalloc(class->n_columns * sizeof *row->old);
682 for (i = 0; i < class->n_columns; i++) {
683 ovsdb_datum_init_default(&row->old[i], &class->columns[i].type);
685 ovsdb_idl_row_update(row, row_json);
686 ovsdb_idl_row_parse(row);
688 ovsdb_idl_row_reparse_backrefs(row, false);
692 ovsdb_idl_delete_row(struct ovsdb_idl_row *row)
694 ovsdb_idl_row_unparse(row);
695 ovsdb_idl_row_clear_arcs(row, true);
696 ovsdb_idl_row_clear_old(row);
697 if (list_is_empty(&row->dst_arcs)) {
698 ovsdb_idl_row_destroy(row);
700 ovsdb_idl_row_reparse_backrefs(row, true);
705 ovsdb_idl_modify_row(struct ovsdb_idl_row *row, const struct json *row_json)
707 ovsdb_idl_row_unparse(row);
708 ovsdb_idl_row_clear_arcs(row, true);
709 ovsdb_idl_row_update(row, row_json);
710 ovsdb_idl_row_parse(row);
714 may_add_arc(const struct ovsdb_idl_row *src, const struct ovsdb_idl_row *dst)
716 const struct ovsdb_idl_arc *arc;
723 /* No duplicate arcs.
725 * We only need to test whether the first arc in dst->dst_arcs originates
726 * at 'src', since we add all of the arcs from a given source in a clump
727 * (in a single call to ovsdb_idl_row_parse()) and new arcs are always
728 * added at the front of the dst_arcs list. */
729 if (list_is_empty(&dst->dst_arcs)) {
732 arc = CONTAINER_OF(dst->dst_arcs.next, struct ovsdb_idl_arc, dst_node);
733 return arc->src != src;
736 static struct ovsdb_idl_table *
737 ovsdb_idl_table_from_class(const struct ovsdb_idl *idl,
738 const struct ovsdb_idl_table_class *table_class)
740 return &idl->tables[table_class - idl->class->tables];
743 struct ovsdb_idl_row *
744 ovsdb_idl_get_row_arc(struct ovsdb_idl_row *src,
745 struct ovsdb_idl_table_class *dst_table_class,
746 const struct uuid *dst_uuid)
748 struct ovsdb_idl *idl = src->table->idl;
749 struct ovsdb_idl_table *dst_table;
750 struct ovsdb_idl_arc *arc;
751 struct ovsdb_idl_row *dst;
753 dst_table = ovsdb_idl_table_from_class(idl, dst_table_class);
754 dst = ovsdb_idl_get_row(dst_table, dst_uuid);
756 /* We're being called from ovsdb_idl_txn_write(). We must not update
757 * any arcs, because the transaction will be backed out at commit or
758 * abort time and we don't want our graph screwed up.
760 * Just return the destination row, if there is one and it has not been
762 if (dst && (hmap_node_is_null(&dst->txn_node) || dst->new)) {
767 /* We're being called from some other context. Update the graph. */
769 dst = ovsdb_idl_row_create(dst_table, dst_uuid);
772 /* Add a new arc, if it wouldn't be a self-arc or a duplicate arc. */
773 if (may_add_arc(src, dst)) {
774 /* The arc *must* be added at the front of the dst_arcs list. See
775 * ovsdb_idl_row_reparse_backrefs() for details. */
776 arc = xmalloc(sizeof *arc);
777 list_push_front(&src->src_arcs, &arc->src_node);
778 list_push_front(&dst->dst_arcs, &arc->dst_node);
783 return !ovsdb_idl_row_is_orphan(dst) ? dst : NULL;
787 const struct ovsdb_idl_row *
788 ovsdb_idl_get_row_for_uuid(const struct ovsdb_idl *idl,
789 const struct ovsdb_idl_table_class *tc,
790 const struct uuid *uuid)
792 return ovsdb_idl_get_row(ovsdb_idl_table_from_class(idl, tc), uuid);
795 static struct ovsdb_idl_row *
796 next_real_row(struct ovsdb_idl_table *table, struct hmap_node *node)
798 for (; node; node = hmap_next(&table->rows, node)) {
799 struct ovsdb_idl_row *row;
801 row = CONTAINER_OF(node, struct ovsdb_idl_row, hmap_node);
802 if (row->new || !ovsdb_idl_row_is_orphan(row)) {
809 const struct ovsdb_idl_row *
810 ovsdb_idl_first_row(const struct ovsdb_idl *idl,
811 const struct ovsdb_idl_table_class *table_class)
813 struct ovsdb_idl_table *table
814 = ovsdb_idl_table_from_class(idl, table_class);
815 return next_real_row(table, hmap_first(&table->rows));
818 const struct ovsdb_idl_row *
819 ovsdb_idl_next_row(const struct ovsdb_idl_row *row)
821 struct ovsdb_idl_table *table = row->table;
823 return next_real_row(table, hmap_next(&table->rows, &row->hmap_node));
828 static void ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
829 enum ovsdb_idl_txn_status);
832 ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status status)
851 struct ovsdb_idl_txn *
852 ovsdb_idl_txn_create(struct ovsdb_idl *idl)
854 struct ovsdb_idl_txn *txn;
857 idl->txn = txn = xmalloc(sizeof *txn);
859 txn->status = TXN_INCOMPLETE;
860 hmap_init(&txn->txn_rows);
861 txn->dry_run = false;
862 ds_init(&txn->comment);
863 txn->inc_table = NULL;
864 txn->inc_column = NULL;
865 txn->inc_where = NULL;
870 ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *txn, const char *s)
872 if (txn->comment.length) {
873 ds_put_char(&txn->comment, '\n');
875 ds_put_cstr(&txn->comment, s);
879 ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *txn)
885 ovsdb_idl_txn_increment(struct ovsdb_idl_txn *txn, const char *table,
886 const char *column, const struct json *where)
888 assert(!txn->inc_table);
889 txn->inc_table = xstrdup(table);
890 txn->inc_column = xstrdup(column);
891 txn->inc_where = where ? json_clone(where) : json_array_create_empty();
895 ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *txn)
897 if (txn->status == TXN_INCOMPLETE) {
898 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
900 ovsdb_idl_txn_abort(txn);
901 ds_destroy(&txn->comment);
902 free(txn->inc_table);
903 free(txn->inc_column);
904 json_destroy(txn->inc_where);
909 ovsdb_idl_txn_wait(const struct ovsdb_idl_txn *txn)
911 if (txn->status != TXN_INCOMPLETE) {
912 poll_immediate_wake();
917 where_uuid_equals(const struct uuid *uuid)
922 json_string_create("_uuid"),
923 json_string_create("=="),
925 json_string_create("uuid"),
926 json_string_create_nocopy(
927 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
931 uuid_name_from_uuid(const struct uuid *uuid)
936 name = xasprintf("row"UUID_FMT, UUID_ARGS(uuid));
937 for (p = name; *p != '\0'; p++) {
946 static const struct ovsdb_idl_row *
947 ovsdb_idl_txn_get_row(const struct ovsdb_idl_txn *txn, const struct uuid *uuid)
949 const struct ovsdb_idl_row *row;
951 HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_idl_row, txn_node,
952 uuid_hash(uuid), &txn->txn_rows) {
953 if (uuid_equals(&row->uuid, uuid)) {
960 /* XXX there must be a cleaner way to do this */
962 substitute_uuids(struct json *json, const struct ovsdb_idl_txn *txn)
964 if (json->type == JSON_ARRAY) {
968 if (json->u.array.n == 2
969 && json->u.array.elems[0]->type == JSON_STRING
970 && json->u.array.elems[1]->type == JSON_STRING
971 && !strcmp(json->u.array.elems[0]->u.string, "uuid")
972 && uuid_from_string(&uuid, json->u.array.elems[1]->u.string)) {
973 const struct ovsdb_idl_row *row;
975 row = ovsdb_idl_txn_get_row(txn, &uuid);
976 if (row && !row->old && row->new) {
979 return json_array_create_2(
980 json_string_create("named-uuid"),
981 json_string_create_nocopy(uuid_name_from_uuid(&uuid)));
985 for (i = 0; i < json->u.array.n; i++) {
986 json->u.array.elems[i] = substitute_uuids(json->u.array.elems[i],
989 } else if (json->type == JSON_OBJECT) {
990 struct shash_node *node;
992 SHASH_FOR_EACH (node, json_object(json)) {
993 node->data = substitute_uuids(node->data, txn);
1000 ovsdb_idl_txn_disassemble(struct ovsdb_idl_txn *txn)
1002 struct ovsdb_idl_row *row, *next;
1004 /* This must happen early. Otherwise, ovsdb_idl_row_parse() will call an
1005 * ovsdb_idl_column's 'parse' function, which will call
1006 * ovsdb_idl_get_row_arc(), which will seen that the IDL is in a
1007 * transaction and fail to update the graph. */
1008 txn->idl->txn = NULL;
1010 HMAP_FOR_EACH_SAFE (row, next, struct ovsdb_idl_row, txn_node,
1014 ovsdb_idl_row_unparse(row);
1015 ovsdb_idl_row_clear_arcs(row, false);
1016 ovsdb_idl_row_parse(row);
1019 hmap_remove(&row->table->rows, &row->hmap_node);
1021 ovsdb_idl_row_clear_new(row);
1024 row->prereqs = NULL;
1027 row->written = NULL;
1029 hmap_remove(&txn->txn_rows, &row->txn_node);
1030 hmap_node_nullify(&row->txn_node);
1032 hmap_destroy(&txn->txn_rows);
1033 hmap_init(&txn->txn_rows);
1036 enum ovsdb_idl_txn_status
1037 ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
1039 struct ovsdb_idl_row *row;
1040 struct json *operations;
1043 if (txn != txn->idl->txn) {
1047 operations = json_array_create_empty();
1049 /* Add prerequisites and declarations of new rows. */
1050 HMAP_FOR_EACH (row, struct ovsdb_idl_row, txn_node, &txn->txn_rows) {
1051 /* XXX check that deleted rows exist even if no prereqs? */
1053 const struct ovsdb_idl_table_class *class = row->table->class;
1054 size_t n_columns = class->n_columns;
1055 struct json *op, *columns, *row_json;
1058 op = json_object_create();
1059 json_array_add(operations, op);
1060 json_object_put_string(op, "op", "wait");
1061 json_object_put_string(op, "table", class->name);
1062 json_object_put(op, "timeout", json_integer_create(0));
1063 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1064 json_object_put_string(op, "until", "==");
1065 columns = json_array_create_empty();
1066 json_object_put(op, "columns", columns);
1067 row_json = json_object_create();
1068 json_object_put(op, "rows", json_array_create_1(row_json));
1070 BITMAP_FOR_EACH_1 (idx, n_columns, row->prereqs) {
1071 const struct ovsdb_idl_column *column = &class->columns[idx];
1072 json_array_add(columns, json_string_create(column->name));
1073 json_object_put(row_json, column->name,
1074 ovsdb_datum_to_json(&row->old[idx],
1078 if (row->new && !row->old) {
1081 op = json_object_create();
1082 json_array_add(operations, op);
1083 json_object_put_string(op, "op", "declare");
1084 json_object_put(op, "uuid-name",
1085 json_string_create_nocopy(
1086 uuid_name_from_uuid(&row->uuid)));
1091 any_updates = false;
1092 HMAP_FOR_EACH (row, struct ovsdb_idl_row, txn_node, &txn->txn_rows) {
1093 const struct ovsdb_idl_table_class *class = row->table->class;
1095 if (row->old == row->new) {
1097 } else if (!row->new) {
1098 struct json *op = json_object_create();
1099 json_object_put_string(op, "op", "delete");
1100 json_object_put_string(op, "table", class->name);
1101 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1102 json_array_add(operations, op);
1105 struct json *row_json;
1109 op = json_object_create();
1110 json_object_put_string(op, "op", row->old ? "update" : "insert");
1111 json_object_put_string(op, "table", class->name);
1113 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1115 json_object_put(op, "uuid-name",
1116 json_string_create_nocopy(
1117 uuid_name_from_uuid(&row->uuid)));
1119 row_json = json_object_create();
1120 json_object_put(op, "row", row_json);
1122 BITMAP_FOR_EACH_1 (idx, class->n_columns, row->written) {
1123 const struct ovsdb_idl_column *column = &class->columns[idx];
1126 ? !ovsdb_datum_equals(&row->old[idx], &row->new[idx],
1128 : !ovsdb_datum_is_default(&row->new[idx], &column->type)) {
1129 json_object_put(row_json, column->name,
1131 ovsdb_datum_to_json(&row->new[idx],
1137 if (!row->old || !shash_is_empty(json_object(row_json))) {
1138 json_array_add(operations, op);
1146 /* Add increment. */
1147 if (txn->inc_table && any_updates) {
1150 txn->inc_index = operations->u.array.n;
1152 op = json_object_create();
1153 json_object_put_string(op, "op", "mutate");
1154 json_object_put_string(op, "table", txn->inc_table);
1155 json_object_put(op, "where",
1156 substitute_uuids(json_clone(txn->inc_where), txn));
1157 json_object_put(op, "mutations",
1158 json_array_create_1(
1159 json_array_create_3(
1160 json_string_create(txn->inc_column),
1161 json_string_create("+="),
1162 json_integer_create(1))));
1163 json_array_add(operations, op);
1165 op = json_object_create();
1166 json_object_put_string(op, "op", "select");
1167 json_object_put_string(op, "table", txn->inc_table);
1168 json_object_put(op, "where",
1169 substitute_uuids(json_clone(txn->inc_where), txn));
1170 json_object_put(op, "columns",
1171 json_array_create_1(json_string_create(
1173 json_array_add(operations, op);
1176 if (txn->comment.length) {
1177 struct json *op = json_object_create();
1178 json_object_put_string(op, "op", "comment");
1179 json_object_put_string(op, "comment", ds_cstr(&txn->comment));
1180 json_array_add(operations, op);
1184 struct json *op = json_object_create();
1185 json_object_put_string(op, "op", "abort");
1186 json_array_add(operations, op);
1190 txn->status = TXN_UNCHANGED;
1191 json_destroy(operations);
1192 } else if (!jsonrpc_session_send(
1194 jsonrpc_create_request(
1195 "transact", operations, &txn->request_id))) {
1196 hmap_insert(&txn->idl->outstanding_txns, &txn->hmap_node,
1197 json_hash(txn->request_id, 0));
1199 txn->status = TXN_INCOMPLETE;
1202 ovsdb_idl_txn_disassemble(txn);
1207 ovsdb_idl_txn_get_increment_new_value(const struct ovsdb_idl_txn *txn)
1209 assert(txn->status == TXN_SUCCESS);
1210 return txn->inc_new_value;
1214 ovsdb_idl_txn_abort(struct ovsdb_idl_txn *txn)
1216 ovsdb_idl_txn_disassemble(txn);
1217 if (txn->status == TXN_INCOMPLETE) {
1218 txn->status = TXN_ABORTED;
1223 ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
1224 enum ovsdb_idl_txn_status status)
1226 txn->status = status;
1227 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
1231 ovsdb_idl_txn_read(const struct ovsdb_idl_row *row,
1232 const struct ovsdb_idl_column *column,
1233 struct ovsdb_datum *datum)
1235 const struct ovsdb_idl_table_class *class = row->table->class;
1236 size_t column_idx = column - class->columns;
1238 assert(row->new != NULL);
1239 if (row->written && bitmap_is_set(row->written, column_idx)) {
1240 ovsdb_datum_clone(datum, &row->new[column_idx], &column->type);
1241 } else if (row->old) {
1242 ovsdb_datum_clone(datum, &row->old[column_idx], &column->type);
1244 ovsdb_datum_init_default(datum, &column->type);
1249 ovsdb_idl_txn_write(const struct ovsdb_idl_row *row_,
1250 const struct ovsdb_idl_column *column,
1251 struct ovsdb_datum *datum)
1253 struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1254 const struct ovsdb_idl_table_class *class = row->table->class;
1255 size_t column_idx = column - class->columns;
1257 assert(row->new != NULL);
1258 if (hmap_node_is_null(&row->txn_node)) {
1259 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1260 uuid_hash(&row->uuid));
1262 if (row->old == row->new) {
1263 row->new = xmalloc(class->n_columns * sizeof *row->new);
1265 if (!row->written) {
1266 row->written = bitmap_allocate(class->n_columns);
1268 if (bitmap_is_set(row->written, column_idx)) {
1269 ovsdb_datum_destroy(&row->new[column_idx], &column->type);
1271 bitmap_set1(row->written, column_idx);
1273 row->new[column_idx] = *datum;
1274 (column->unparse)(row);
1275 (column->parse)(row, &row->new[column_idx]);
1279 ovsdb_idl_txn_verify(const struct ovsdb_idl_row *row_,
1280 const struct ovsdb_idl_column *column)
1282 struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1283 const struct ovsdb_idl_table_class *class = row->table->class;
1284 size_t column_idx = column - class->columns;
1286 assert(row->new != NULL);
1288 || (row->written && bitmap_is_set(row->written, column_idx))) {
1292 if (hmap_node_is_null(&row->txn_node)) {
1293 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1294 uuid_hash(&row->uuid));
1296 if (!row->prereqs) {
1297 row->prereqs = bitmap_allocate(class->n_columns);
1299 bitmap_set1(row->prereqs, column_idx);
1303 ovsdb_idl_txn_delete(const struct ovsdb_idl_row *row_)
1305 struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1307 assert(row->new != NULL);
1309 ovsdb_idl_row_clear_new(row);
1310 assert(!row->prereqs);
1311 hmap_remove(&row->table->rows, &row->hmap_node);
1312 hmap_remove(&row->table->idl->txn->txn_rows, &row->txn_node);
1316 if (hmap_node_is_null(&row->txn_node)) {
1317 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1318 uuid_hash(&row->uuid));
1320 ovsdb_idl_row_clear_new(row);
1324 const struct ovsdb_idl_row *
1325 ovsdb_idl_txn_insert(struct ovsdb_idl_txn *txn,
1326 const struct ovsdb_idl_table_class *class)
1328 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(class);
1329 uuid_generate(&row->uuid);
1330 row->table = ovsdb_idl_table_from_class(txn->idl, class);
1331 row->new = xmalloc(class->n_columns * sizeof *row->new);
1332 row->written = bitmap_allocate(class->n_columns);
1333 hmap_insert(&row->table->rows, &row->hmap_node, uuid_hash(&row->uuid));
1334 hmap_insert(&txn->txn_rows, &row->txn_node, uuid_hash(&row->uuid));
1339 ovsdb_idl_txn_abort_all(struct ovsdb_idl *idl)
1341 struct ovsdb_idl_txn *txn;
1343 HMAP_FOR_EACH (txn, struct ovsdb_idl_txn, hmap_node,
1344 &idl->outstanding_txns) {
1345 ovsdb_idl_txn_complete(txn, TXN_TRY_AGAIN);
1349 static struct ovsdb_idl_txn *
1350 ovsdb_idl_txn_find(struct ovsdb_idl *idl, const struct json *id)
1352 struct ovsdb_idl_txn *txn;
1354 HMAP_FOR_EACH_WITH_HASH (txn, struct ovsdb_idl_txn, hmap_node,
1355 json_hash(id, 0), &idl->outstanding_txns) {
1356 if (json_equal(id, txn->request_id)) {
1364 check_json_type(const struct json *json, enum json_type type, const char *name)
1367 VLOG_WARN_RL(&syntax_rl, "%s is missing", name);
1369 } else if (json->type != type) {
1370 VLOG_WARN_RL(&syntax_rl, "%s is %s instead of %s",
1371 name, json_type_to_string(json->type),
1372 json_type_to_string(type));
1380 ovsdb_idl_txn_process_inc_reply(struct ovsdb_idl_txn *txn,
1381 const struct json_array *results)
1383 struct json *count, *rows, *row, *column;
1384 struct shash *mutate, *select;
1386 if (txn->inc_index + 2 > results->n) {
1387 VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
1388 "for increment (has %u, needs %u)",
1389 results->n, txn->inc_index + 2);
1393 /* We know that this is a JSON objects because the loop in
1394 * ovsdb_idl_txn_process_reply() checked. */
1395 mutate = json_object(results->elems[txn->inc_index]);
1396 count = shash_find_data(mutate, "count");
1397 if (!check_json_type(count, JSON_INTEGER, "\"mutate\" reply \"count\"")) {
1400 if (count->u.integer != 1) {
1401 VLOG_WARN_RL(&syntax_rl,
1402 "\"mutate\" reply \"count\" is %"PRId64" instead of 1",
1407 select = json_object(results->elems[txn->inc_index + 1]);
1408 rows = shash_find_data(select, "rows");
1409 if (!check_json_type(rows, JSON_ARRAY, "\"select\" reply \"rows\"")) {
1412 if (rows->u.array.n != 1) {
1413 VLOG_WARN_RL(&syntax_rl, "\"select\" reply \"rows\" has %u elements "
1418 row = rows->u.array.elems[0];
1419 if (!check_json_type(row, JSON_OBJECT, "\"select\" reply row")) {
1422 column = shash_find_data(json_object(row), txn->inc_column);
1423 if (!check_json_type(column, JSON_INTEGER,
1424 "\"select\" reply inc column")) {
1427 txn->inc_new_value = column->u.integer;
1433 ovsdb_idl_txn_process_reply(struct ovsdb_idl *idl,
1434 const struct jsonrpc_msg *msg)
1436 struct ovsdb_idl_txn *txn;
1437 enum ovsdb_idl_txn_status status;
1439 txn = ovsdb_idl_txn_find(idl, msg->id);
1444 if (msg->type == JSONRPC_ERROR) {
1446 } else if (msg->result->type != JSON_ARRAY) {
1447 VLOG_WARN_RL(&syntax_rl, "reply to \"transact\" is not JSON array");
1450 int hard_errors = 0;
1451 int soft_errors = 0;
1454 for (i = 0; i < msg->result->u.array.n; i++) {
1455 struct json *json = msg->result->u.array.elems[i];
1457 if (json->type == JSON_NULL) {
1458 /* This isn't an error in itself but indicates that some prior
1459 * operation failed, so make sure that we know about it. */
1461 } else if (json->type == JSON_OBJECT) {
1464 error = shash_find_data(json_object(json), "error");
1466 if (error->type == JSON_STRING) {
1467 if (!strcmp(error->u.string, "timed out")) {
1469 } else if (strcmp(error->u.string, "aborted")) {
1474 VLOG_WARN_RL(&syntax_rl,
1475 "\"error\" in reply is not JSON string");
1480 VLOG_WARN_RL(&syntax_rl,
1481 "operation reply is not JSON null or object");
1488 && !ovsdb_idl_txn_process_inc_reply(txn,
1489 json_array(msg->result))) {
1493 status = (hard_errors ? TXN_ERROR
1494 : soft_errors ? TXN_TRY_AGAIN
1498 ovsdb_idl_txn_complete(txn, status);
1502 struct ovsdb_idl_txn *
1503 ovsdb_idl_txn_get(const struct ovsdb_idl_row *row)
1505 struct ovsdb_idl_txn *txn = row->table->idl->txn;
1506 assert(txn != NULL);