1 /* Copyright (c) 2009 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_clear_old(struct ovsdb_idl_row *);
119 static void ovsdb_idl_row_clear_new(struct ovsdb_idl_row *);
121 static void ovsdb_idl_txn_abort_all(struct ovsdb_idl *);
122 static bool ovsdb_idl_txn_process_reply(struct ovsdb_idl *,
123 const struct jsonrpc_msg *msg);
126 ovsdb_idl_create(const char *remote, const struct ovsdb_idl_class *class)
128 struct ovsdb_idl *idl;
131 idl = xzalloc(sizeof *idl);
133 idl->session = jsonrpc_session_open(remote);
134 shash_init(&idl->table_by_name);
135 idl->tables = xmalloc(class->n_tables * sizeof *idl->tables);
136 for (i = 0; i < class->n_tables; i++) {
137 const struct ovsdb_idl_table_class *tc = &class->tables[i];
138 struct ovsdb_idl_table *table = &idl->tables[i];
141 assert(!shash_find(&idl->table_by_name, tc->name));
142 shash_add(&idl->table_by_name, tc->name, table);
144 shash_init(&table->columns);
145 for (j = 0; j < tc->n_columns; j++) {
146 const struct ovsdb_idl_column *column = &tc->columns[j];
148 assert(!shash_find(&table->columns, column->name));
149 shash_add(&table->columns, column->name, column);
151 hmap_init(&table->rows);
154 idl->last_monitor_request_seqno = UINT_MAX;
155 hmap_init(&idl->outstanding_txns);
161 ovsdb_idl_destroy(struct ovsdb_idl *idl)
167 ovsdb_idl_clear(idl);
168 jsonrpc_session_close(idl->session);
170 for (i = 0; i < idl->class->n_tables; i++) {
171 struct ovsdb_idl_table *table = &idl->tables[i];
172 shash_destroy(&table->columns);
173 hmap_destroy(&table->rows);
175 shash_destroy(&idl->table_by_name);
177 json_destroy(idl->monitor_request_id);
183 ovsdb_idl_clear(struct ovsdb_idl *idl)
185 bool changed = false;
188 for (i = 0; i < idl->class->n_tables; i++) {
189 struct ovsdb_idl_table *table = &idl->tables[i];
190 struct ovsdb_idl_row *row, *next_row;
192 if (hmap_is_empty(&table->rows)) {
197 HMAP_FOR_EACH_SAFE (row, next_row, struct ovsdb_idl_row, hmap_node,
199 struct ovsdb_idl_arc *arc, *next_arc;
201 if (!ovsdb_idl_row_is_orphan(row)) {
202 (row->table->class->unparse)(row);
203 ovsdb_idl_row_clear_old(row);
205 hmap_remove(&table->rows, &row->hmap_node);
206 LIST_FOR_EACH_SAFE (arc, next_arc, struct ovsdb_idl_arc, src_node,
210 /* No need to do anything with dst_arcs: some node has those arcs
211 * as forward arcs and will destroy them itself. */
223 ovsdb_idl_run(struct ovsdb_idl *idl)
228 jsonrpc_session_run(idl->session);
229 for (i = 0; jsonrpc_session_is_connected(idl->session) && i < 50; i++) {
230 struct jsonrpc_msg *msg, *reply;
233 seqno = jsonrpc_session_get_seqno(idl->session);
234 if (idl->last_monitor_request_seqno != seqno) {
235 idl->last_monitor_request_seqno = seqno;
236 ovsdb_idl_txn_abort_all(idl);
237 ovsdb_idl_send_monitor_request(idl);
241 msg = jsonrpc_session_recv(idl->session);
247 if (msg->type == JSONRPC_NOTIFY
248 && !strcmp(msg->method, "update")
249 && msg->params->type == JSON_ARRAY
250 && msg->params->u.array.n == 2
251 && msg->params->u.array.elems[0]->type == JSON_NULL) {
252 ovsdb_idl_parse_update(idl, msg->params->u.array.elems[1]);
253 } else if (msg->type == JSONRPC_REPLY
254 && idl->monitor_request_id
255 && json_equal(idl->monitor_request_id, msg->id)) {
256 json_destroy(idl->monitor_request_id);
257 idl->monitor_request_id = NULL;
258 ovsdb_idl_clear(idl);
259 ovsdb_idl_parse_update(idl, msg->result);
260 } else if (msg->type == JSONRPC_REPLY
261 && msg->id && msg->id->type == JSON_STRING
262 && !strcmp(msg->id->u.string, "echo")) {
263 /* It's a reply to our echo request. Ignore it. */
264 } else if ((msg->type == JSONRPC_ERROR
265 || msg->type == JSONRPC_REPLY)
266 && ovsdb_idl_txn_process_reply(idl, msg)) {
267 /* ovsdb_idl_txn_process_reply() did everything needful. */
269 VLOG_WARN("%s: received unexpected %s message",
270 jsonrpc_session_get_name(idl->session),
271 jsonrpc_msg_type_to_string(msg->type));
272 jsonrpc_session_force_reconnect(idl->session);
275 jsonrpc_session_send(idl->session, reply);
277 jsonrpc_msg_destroy(msg);
282 ovsdb_idl_wait(struct ovsdb_idl *idl)
284 jsonrpc_session_wait(idl->session);
285 jsonrpc_session_recv_wait(idl->session);
289 ovsdb_idl_get_seqno(const struct ovsdb_idl *idl)
291 return idl->change_seqno;
295 ovsdb_idl_force_reconnect(struct ovsdb_idl *idl)
297 jsonrpc_session_force_reconnect(idl->session);
301 ovsdb_idl_send_monitor_request(struct ovsdb_idl *idl)
303 struct json *monitor_requests;
304 struct jsonrpc_msg *msg;
307 monitor_requests = json_object_create();
308 for (i = 0; i < idl->class->n_tables; i++) {
309 const struct ovsdb_idl_table *table = &idl->tables[i];
310 const struct ovsdb_idl_table_class *tc = table->class;
311 struct json *monitor_request, *columns;
314 monitor_request = json_object_create();
315 columns = json_array_create_empty();
316 for (i = 0; i < tc->n_columns; i++) {
317 const struct ovsdb_idl_column *column = &tc->columns[i];
318 json_array_add(columns, json_string_create(column->name));
320 json_object_put(monitor_request, "columns", columns);
321 json_object_put(monitor_requests, tc->name, monitor_request);
324 json_destroy(idl->monitor_request_id);
325 msg = jsonrpc_create_request(
326 "monitor", json_array_create_2(json_null_create(), monitor_requests),
327 &idl->monitor_request_id);
328 jsonrpc_session_send(idl->session, msg);
332 ovsdb_idl_parse_update(struct ovsdb_idl *idl, const struct json *table_updates)
334 struct ovsdb_error *error;
338 error = ovsdb_idl_parse_update__(idl, table_updates);
340 if (!VLOG_DROP_WARN(&syntax_rl)) {
341 char *s = ovsdb_error_to_string(error);
342 VLOG_WARN_RL(&syntax_rl, "%s", s);
345 ovsdb_error_destroy(error);
349 static struct ovsdb_error *
350 ovsdb_idl_parse_update__(struct ovsdb_idl *idl,
351 const struct json *table_updates)
353 const struct shash_node *tables_node;
355 if (table_updates->type != JSON_OBJECT) {
356 return ovsdb_syntax_error(table_updates, NULL,
357 "<table-updates> is not an object");
359 SHASH_FOR_EACH (tables_node, json_object(table_updates)) {
360 const struct json *table_update = tables_node->data;
361 const struct shash_node *table_node;
362 struct ovsdb_idl_table *table;
364 table = shash_find_data(&idl->table_by_name, tables_node->name);
366 return ovsdb_syntax_error(
368 "<table-updates> includes unknown table \"%s\"",
372 if (table_update->type != JSON_OBJECT) {
373 return ovsdb_syntax_error(table_update, NULL,
374 "<table-update> for table \"%s\" is "
375 "not an object", table->class->name);
377 SHASH_FOR_EACH (table_node, json_object(table_update)) {
378 const struct json *row_update = table_node->data;
379 const struct json *old_json, *new_json;
382 if (!uuid_from_string(&uuid, table_node->name)) {
383 return ovsdb_syntax_error(table_update, NULL,
384 "<table-update> for table \"%s\" "
386 "\"%s\" as member name",
390 if (row_update->type != JSON_OBJECT) {
391 return ovsdb_syntax_error(row_update, NULL,
392 "<table-update> for table \"%s\" "
393 "contains <row-update> for %s that "
399 old_json = shash_find_data(json_object(row_update), "old");
400 new_json = shash_find_data(json_object(row_update), "new");
401 if (old_json && old_json->type != JSON_OBJECT) {
402 return ovsdb_syntax_error(old_json, NULL,
403 "\"old\" <row> is not object");
404 } else if (new_json && new_json->type != JSON_OBJECT) {
405 return ovsdb_syntax_error(new_json, NULL,
406 "\"new\" <row> is not object");
407 } else if ((old_json != NULL) + (new_json != NULL)
408 != shash_count(json_object(row_update))) {
409 return ovsdb_syntax_error(row_update, NULL,
410 "<row-update> contains unexpected "
412 } else if (!old_json && !new_json) {
413 return ovsdb_syntax_error(row_update, NULL,
414 "<row-update> missing \"old\" "
415 "and \"new\" members");
418 ovsdb_idl_process_update(table, &uuid, old_json, new_json);
425 static struct ovsdb_idl_row *
426 ovsdb_idl_get_row(struct ovsdb_idl_table *table, const struct uuid *uuid)
428 struct ovsdb_idl_row *row;
430 HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_idl_row, hmap_node,
431 uuid_hash(uuid), &table->rows) {
432 if (uuid_equals(&row->uuid, uuid)) {
440 ovsdb_idl_process_update(struct ovsdb_idl_table *table,
441 const struct uuid *uuid, const struct json *old,
442 const struct json *new)
444 struct ovsdb_idl_row *row;
446 row = ovsdb_idl_get_row(table, uuid);
449 if (row && !ovsdb_idl_row_is_orphan(row)) {
450 /* XXX perhaps we should check the 'old' values? */
451 ovsdb_idl_delete_row(row);
453 VLOG_WARN_RL(&semantic_rl, "cannot delete missing row "UUID_FMT" "
455 UUID_ARGS(uuid), table->class->name);
460 ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
461 } else if (ovsdb_idl_row_is_orphan(row)) {
462 ovsdb_idl_insert_row(row, new);
464 VLOG_WARN_RL(&semantic_rl, "cannot add existing row "UUID_FMT" to "
465 "table %s", UUID_ARGS(uuid), table->class->name);
466 ovsdb_idl_modify_row(row, new);
471 /* XXX perhaps we should check the 'old' values? */
472 if (!ovsdb_idl_row_is_orphan(row)) {
473 ovsdb_idl_modify_row(row, new);
475 VLOG_WARN_RL(&semantic_rl, "cannot modify missing but "
476 "referenced row "UUID_FMT" in table %s",
477 UUID_ARGS(uuid), table->class->name);
478 ovsdb_idl_insert_row(row, new);
481 VLOG_WARN_RL(&semantic_rl, "cannot modify missing row "UUID_FMT" "
482 "in table %s", UUID_ARGS(uuid), table->class->name);
483 ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
489 ovsdb_idl_row_update(struct ovsdb_idl_row *row, const struct json *row_json)
491 struct ovsdb_idl_table *table = row->table;
492 struct shash_node *node;
494 SHASH_FOR_EACH (node, json_object(row_json)) {
495 const char *column_name = node->name;
496 const struct ovsdb_idl_column *column;
497 struct ovsdb_datum datum;
498 struct ovsdb_error *error;
500 column = shash_find_data(&table->columns, column_name);
502 VLOG_WARN_RL(&syntax_rl, "unknown column %s updating row "UUID_FMT,
503 column_name, UUID_ARGS(&row->uuid));
507 error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
509 ovsdb_datum_swap(&row->old[column - table->class->columns],
511 ovsdb_datum_destroy(&datum, &column->type);
513 char *s = ovsdb_error_to_string(error);
514 VLOG_WARN_RL(&syntax_rl, "error parsing column %s in row "UUID_FMT
515 " in table %s: %s", column_name,
516 UUID_ARGS(&row->uuid), table->class->name, s);
518 ovsdb_error_destroy(error);
524 ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *row)
530 ovsdb_idl_row_clear_old(struct ovsdb_idl_row *row)
532 assert(row->old == row->new);
533 if (!ovsdb_idl_row_is_orphan(row)) {
534 const struct ovsdb_idl_table_class *class = row->table->class;
537 for (i = 0; i < class->n_columns; i++) {
538 ovsdb_datum_destroy(&row->old[i], &class->columns[i].type);
541 row->old = row->new = NULL;
546 ovsdb_idl_row_clear_new(struct ovsdb_idl_row *row)
548 if (row->old != row->new) {
550 const struct ovsdb_idl_table_class *class = row->table->class;
553 BITMAP_FOR_EACH_1 (i, class->n_columns, row->written) {
554 ovsdb_datum_destroy(&row->new[i], &class->columns[i].type);
565 ovsdb_idl_row_clear_arcs(struct ovsdb_idl_row *row, bool destroy_dsts)
567 struct ovsdb_idl_arc *arc, *next;
569 /* Delete all forward arcs. If 'destroy_dsts', destroy any orphaned rows
570 * that this causes to be unreferenced. */
571 LIST_FOR_EACH_SAFE (arc, next, struct ovsdb_idl_arc, src_node,
573 list_remove(&arc->dst_node);
575 && ovsdb_idl_row_is_orphan(arc->dst)
576 && list_is_empty(&arc->dst->dst_arcs)) {
577 ovsdb_idl_row_destroy(arc->dst);
581 list_init(&row->src_arcs);
584 /* Force nodes that reference 'row' to reparse. */
586 ovsdb_idl_row_reparse_backrefs(struct ovsdb_idl_row *row, bool destroy_dsts)
588 struct ovsdb_idl_arc *arc, *next;
590 /* This is trickier than it looks. ovsdb_idl_row_clear_arcs() will destroy
591 * 'arc', so we need to use the "safe" variant of list traversal. However,
592 * calling ref->table->class->parse will add an arc equivalent to 'arc' to
593 * row->arcs. That could be a problem for traversal, but it adds it at the
594 * beginning of the list to prevent us from stumbling upon it again.
596 * (If duplicate arcs were possible then we would need to make sure that
597 * 'next' didn't also point into 'arc''s destination, but we forbid
598 * duplicate arcs.) */
599 LIST_FOR_EACH_SAFE (arc, next, struct ovsdb_idl_arc, dst_node,
601 struct ovsdb_idl_row *ref = arc->src;
603 (ref->table->class->unparse)(ref);
604 ovsdb_idl_row_clear_arcs(ref, destroy_dsts);
605 (ref->table->class->parse)(ref);
609 static struct ovsdb_idl_row *
610 ovsdb_idl_row_create__(const struct ovsdb_idl_table_class *class)
612 struct ovsdb_idl_row *row = xzalloc(class->allocation_size);
613 memset(row, 0, sizeof *row);
614 list_init(&row->src_arcs);
615 list_init(&row->dst_arcs);
616 hmap_node_nullify(&row->txn_node);
620 static struct ovsdb_idl_row *
621 ovsdb_idl_row_create(struct ovsdb_idl_table *table, const struct uuid *uuid)
623 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(table->class);
624 hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
631 ovsdb_idl_row_destroy(struct ovsdb_idl_row *row)
634 ovsdb_idl_row_clear_old(row);
635 hmap_remove(&row->table->rows, &row->hmap_node);
641 ovsdb_idl_insert_row(struct ovsdb_idl_row *row, const struct json *row_json)
643 const struct ovsdb_idl_table_class *class = row->table->class;
646 assert(!row->old && !row->new);
647 row->old = row->new = xmalloc(class->n_columns * sizeof *row->old);
648 for (i = 0; i < class->n_columns; i++) {
649 ovsdb_datum_init_default(&row->old[i], &class->columns[i].type);
651 ovsdb_idl_row_update(row, row_json);
654 ovsdb_idl_row_reparse_backrefs(row, false);
658 ovsdb_idl_delete_row(struct ovsdb_idl_row *row)
660 (row->table->class->unparse)(row);
661 ovsdb_idl_row_clear_arcs(row, true);
662 ovsdb_idl_row_clear_old(row);
663 if (list_is_empty(&row->dst_arcs)) {
664 ovsdb_idl_row_destroy(row);
666 ovsdb_idl_row_reparse_backrefs(row, true);
671 ovsdb_idl_modify_row(struct ovsdb_idl_row *row, const struct json *row_json)
673 (row->table->class->unparse)(row);
674 ovsdb_idl_row_clear_arcs(row, true);
675 ovsdb_idl_row_update(row, row_json);
676 (row->table->class->parse)(row);
680 may_add_arc(const struct ovsdb_idl_row *src, const struct ovsdb_idl_row *dst)
682 const struct ovsdb_idl_arc *arc;
689 /* No duplicate arcs.
691 * We only need to test whether the first arc in dst->dst_arcs originates
692 * at 'src', since we add all of the arcs from a given source in a clump
693 * (in a single call to a row's ->parse function) and new arcs are always
694 * added at the front of the dst_arcs list. */
695 if (list_is_empty(&dst->dst_arcs)) {
698 arc = CONTAINER_OF(dst->dst_arcs.next, struct ovsdb_idl_arc, dst_node);
699 return arc->src != src;
702 static struct ovsdb_idl_table *
703 ovsdb_idl_table_from_class(const struct ovsdb_idl *idl,
704 const struct ovsdb_idl_table_class *table_class)
706 return &idl->tables[table_class - idl->class->tables];
709 struct ovsdb_idl_row *
710 ovsdb_idl_get_row_arc(struct ovsdb_idl_row *src,
711 struct ovsdb_idl_table_class *dst_table_class,
712 const struct uuid *dst_uuid)
714 struct ovsdb_idl *idl = src->table->idl;
715 struct ovsdb_idl_table *dst_table;
716 struct ovsdb_idl_arc *arc;
717 struct ovsdb_idl_row *dst;
719 dst_table = ovsdb_idl_table_from_class(idl, dst_table_class);
720 dst = ovsdb_idl_get_row(dst_table, dst_uuid);
722 dst = ovsdb_idl_row_create(dst_table, dst_uuid);
725 /* Add a new arc, if it wouldn't be a self-arc or a duplicate arc. */
726 if (may_add_arc(src, dst)) {
727 /* The arc *must* be added at the front of the dst_arcs list. See
728 * ovsdb_idl_row_reparse_backrefs() for details. */
729 arc = xmalloc(sizeof *arc);
730 list_push_front(&src->src_arcs, &arc->src_node);
731 list_push_front(&dst->dst_arcs, &arc->dst_node);
736 return !ovsdb_idl_row_is_orphan(dst) ? dst : NULL;
739 static struct ovsdb_idl_row *
740 next_real_row(struct ovsdb_idl_table *table, struct hmap_node *node)
742 for (; node; node = hmap_next(&table->rows, node)) {
743 struct ovsdb_idl_row *row;
745 row = CONTAINER_OF(node, struct ovsdb_idl_row, hmap_node);
746 if (!ovsdb_idl_row_is_orphan(row)) {
753 struct ovsdb_idl_row *
754 ovsdb_idl_first_row(const struct ovsdb_idl *idl,
755 const struct ovsdb_idl_table_class *table_class)
757 struct ovsdb_idl_table *table
758 = ovsdb_idl_table_from_class(idl, table_class);
759 return next_real_row(table, hmap_first(&table->rows));
762 struct ovsdb_idl_row *
763 ovsdb_idl_next_row(const struct ovsdb_idl_row *row)
765 struct ovsdb_idl_table *table = row->table;
767 return next_real_row(table, hmap_next(&table->rows, &row->hmap_node));
772 static void ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
773 enum ovsdb_idl_txn_status);
776 ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status status)
795 struct ovsdb_idl_txn *
796 ovsdb_idl_txn_create(struct ovsdb_idl *idl)
798 struct ovsdb_idl_txn *txn;
801 idl->txn = txn = xmalloc(sizeof *txn);
803 txn->status = TXN_INCOMPLETE;
804 hmap_init(&txn->txn_rows);
805 txn->dry_run = false;
806 ds_init(&txn->comment);
807 txn->inc_table = NULL;
808 txn->inc_column = NULL;
809 txn->inc_where = NULL;
814 ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *txn, const char *s)
816 if (txn->comment.length) {
817 ds_put_char(&txn->comment, '\n');
819 ds_put_cstr(&txn->comment, s);
823 ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *txn)
829 ovsdb_idl_txn_increment(struct ovsdb_idl_txn *txn, const char *table,
830 const char *column, const struct json *where)
832 assert(!txn->inc_table);
833 txn->inc_table = xstrdup(table);
834 txn->inc_column = xstrdup(column);
835 txn->inc_where = where ? json_clone(where) : json_array_create_empty();
839 ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *txn)
841 if (txn->status == TXN_INCOMPLETE) {
842 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
844 ovsdb_idl_txn_abort(txn);
845 ds_destroy(&txn->comment);
846 free(txn->inc_table);
847 free(txn->inc_column);
848 json_destroy(txn->inc_where);
853 ovsdb_idl_txn_wait(const struct ovsdb_idl_txn *txn)
855 if (txn->status != TXN_INCOMPLETE) {
856 poll_immediate_wake();
861 where_uuid_equals(const struct uuid *uuid)
866 json_string_create("_uuid"),
867 json_string_create("=="),
869 json_string_create("uuid"),
870 json_string_create_nocopy(
871 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
875 uuid_name_from_uuid(const struct uuid *uuid)
880 name = xasprintf("row"UUID_FMT, UUID_ARGS(uuid));
881 for (p = name; *p != '\0'; p++) {
890 static const struct ovsdb_idl_row *
891 ovsdb_idl_txn_get_row(const struct ovsdb_idl_txn *txn, const struct uuid *uuid)
893 const struct ovsdb_idl_row *row;
895 HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_idl_row, txn_node,
896 uuid_hash(uuid), &txn->txn_rows) {
897 if (uuid_equals(&row->uuid, uuid)) {
904 /* XXX there must be a cleaner way to do this */
906 substitute_uuids(struct json *json, const struct ovsdb_idl_txn *txn)
908 if (json->type == JSON_ARRAY) {
912 if (json->u.array.n == 2
913 && json->u.array.elems[0]->type == JSON_STRING
914 && json->u.array.elems[1]->type == JSON_STRING
915 && !strcmp(json->u.array.elems[0]->u.string, "uuid")
916 && uuid_from_string(&uuid, json->u.array.elems[1]->u.string)) {
917 const struct ovsdb_idl_row *row;
919 row = ovsdb_idl_txn_get_row(txn, &uuid);
920 if (row && !row->old && row->new) {
923 return json_array_create_2(
924 json_string_create("named-uuid"),
925 json_string_create_nocopy(uuid_name_from_uuid(&uuid)));
929 for (i = 0; i < json->u.array.n; i++) {
930 json->u.array.elems[i] = substitute_uuids(json->u.array.elems[i],
933 } else if (json->type == JSON_OBJECT) {
934 struct shash_node *node;
936 SHASH_FOR_EACH (node, json_object(json)) {
937 node->data = substitute_uuids(node->data, txn);
944 ovsdb_idl_txn_disassemble(struct ovsdb_idl_txn *txn)
946 struct ovsdb_idl_row *row, *next;
948 HMAP_FOR_EACH_SAFE (row, next, struct ovsdb_idl_row, txn_node,
950 if (row->old && row->written) {
951 (row->table->class->unparse)(row);
952 ovsdb_idl_row_clear_arcs(row, false);
953 (row->table->class->parse)(row);
955 ovsdb_idl_row_clear_new(row);
963 hmap_remove(&txn->txn_rows, &row->txn_node);
964 hmap_node_nullify(&row->txn_node);
966 hmap_destroy(&txn->txn_rows);
967 hmap_init(&txn->txn_rows);
970 enum ovsdb_idl_txn_status
971 ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
973 struct ovsdb_idl_row *row;
974 struct json *operations;
977 if (txn != txn->idl->txn) {
981 operations = json_array_create_empty();
983 /* Add prerequisites and declarations of new rows. */
984 HMAP_FOR_EACH (row, struct ovsdb_idl_row, txn_node, &txn->txn_rows) {
985 /* XXX check that deleted rows exist even if no prereqs? */
987 const struct ovsdb_idl_table_class *class = row->table->class;
988 size_t n_columns = class->n_columns;
989 struct json *op, *columns, *row_json;
992 op = json_object_create();
993 json_array_add(operations, op);
994 json_object_put_string(op, "op", "wait");
995 json_object_put_string(op, "table", class->name);
996 json_object_put(op, "timeout", json_integer_create(0));
997 json_object_put(op, "where", where_uuid_equals(&row->uuid));
998 json_object_put_string(op, "until", "==");
999 columns = json_array_create_empty();
1000 json_object_put(op, "columns", columns);
1001 row_json = json_object_create();
1002 json_object_put(op, "rows", json_array_create_1(row_json));
1004 BITMAP_FOR_EACH_1 (idx, n_columns, row->prereqs) {
1005 const struct ovsdb_idl_column *column = &class->columns[idx];
1006 json_array_add(columns, json_string_create(column->name));
1007 json_object_put(row_json, column->name,
1008 ovsdb_datum_to_json(&row->old[idx],
1012 if (row->new && !row->old) {
1015 op = json_object_create();
1016 json_array_add(operations, op);
1017 json_object_put_string(op, "op", "declare");
1018 json_object_put(op, "uuid-name",
1019 json_string_create_nocopy(
1020 uuid_name_from_uuid(&row->uuid)));
1025 any_updates = false;
1026 HMAP_FOR_EACH (row, struct ovsdb_idl_row, txn_node, &txn->txn_rows) {
1027 const struct ovsdb_idl_table_class *class = row->table->class;
1029 if (row->old == row->new) {
1031 } else if (!row->new) {
1032 struct json *op = json_object_create();
1033 json_object_put_string(op, "op", "delete");
1034 json_object_put_string(op, "table", class->name);
1035 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1036 json_array_add(operations, op);
1039 struct json *row_json;
1043 op = json_object_create();
1044 json_object_put_string(op, "op", row->old ? "update" : "insert");
1045 json_object_put_string(op, "table", class->name);
1047 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1049 json_object_put(op, "uuid-name",
1050 json_string_create_nocopy(
1051 uuid_name_from_uuid(&row->uuid)));
1053 row_json = json_object_create();
1054 json_object_put(op, "row", row_json);
1056 BITMAP_FOR_EACH_1 (idx, class->n_columns, row->written) {
1057 const struct ovsdb_idl_column *column = &class->columns[idx];
1059 if (!row->old || !ovsdb_datum_equals(&row->old[idx],
1062 json_object_put(row_json, column->name,
1064 ovsdb_datum_to_json(&row->new[idx],
1070 if (!row->old || !shash_is_empty(json_object(row_json))) {
1071 json_array_add(operations, op);
1079 /* Add increment. */
1080 if (txn->inc_table && any_updates) {
1083 txn->inc_index = operations->u.array.n;
1085 op = json_object_create();
1086 json_object_put_string(op, "op", "mutate");
1087 json_object_put_string(op, "table", txn->inc_table);
1088 json_object_put(op, "where",
1089 substitute_uuids(json_clone(txn->inc_where), txn));
1090 json_object_put(op, "mutations",
1091 json_array_create_1(
1092 json_array_create_3(
1093 json_string_create(txn->inc_column),
1094 json_string_create("+="),
1095 json_integer_create(1))));
1096 json_array_add(operations, op);
1098 op = json_object_create();
1099 json_object_put_string(op, "op", "select");
1100 json_object_put_string(op, "table", txn->inc_table);
1101 json_object_put(op, "where",
1102 substitute_uuids(json_clone(txn->inc_where), txn));
1103 json_object_put(op, "columns",
1104 json_array_create_1(json_string_create(
1106 json_array_add(operations, op);
1109 if (txn->comment.length) {
1110 struct json *op = json_object_create();
1111 json_object_put_string(op, "op", "comment");
1112 json_object_put_string(op, "comment", ds_cstr(&txn->comment));
1113 json_array_add(operations, op);
1117 struct json *op = json_object_create();
1118 json_object_put_string(op, "op", "abort");
1119 json_array_add(operations, op);
1123 txn->status = TXN_UNCHANGED;
1124 } else if (!jsonrpc_session_send(
1126 jsonrpc_create_request(
1127 "transact", operations, &txn->request_id))) {
1128 hmap_insert(&txn->idl->outstanding_txns, &txn->hmap_node,
1129 json_hash(txn->request_id, 0));
1131 txn->status = TXN_INCOMPLETE;
1134 txn->idl->txn = NULL;
1135 ovsdb_idl_txn_disassemble(txn);
1140 ovsdb_idl_txn_get_increment_new_value(const struct ovsdb_idl_txn *txn)
1142 assert(txn->status == TXN_SUCCESS);
1143 return txn->inc_new_value;
1147 ovsdb_idl_txn_abort(struct ovsdb_idl_txn *txn)
1149 ovsdb_idl_txn_disassemble(txn);
1150 if (txn->status == TXN_INCOMPLETE) {
1151 txn->status = TXN_ABORTED;
1156 ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
1157 enum ovsdb_idl_txn_status status)
1159 txn->status = status;
1160 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
1164 ovsdb_idl_txn_write(struct ovsdb_idl_row *row,
1165 const struct ovsdb_idl_column *column,
1166 struct ovsdb_datum *datum)
1168 const struct ovsdb_idl_table_class *class = row->table->class;
1169 size_t column_idx = column - class->columns;
1172 if (hmap_node_is_null(&row->txn_node)) {
1173 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1174 uuid_hash(&row->uuid));
1176 if (row->old == row->new) {
1177 row->new = xmalloc(class->n_columns * sizeof *row->new);
1179 if (!row->written) {
1180 row->written = bitmap_allocate(class->n_columns);
1182 if (bitmap_is_set(row->written, column_idx)) {
1183 ovsdb_datum_destroy(&row->new[column_idx], &column->type);
1185 bitmap_set1(row->written, column_idx);
1187 row->new[column_idx] = *datum;
1191 ovsdb_idl_txn_verify(const struct ovsdb_idl_row *row_,
1192 const struct ovsdb_idl_column *column)
1194 struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1195 const struct ovsdb_idl_table_class *class = row->table->class;
1196 size_t column_idx = column - class->columns;
1200 || (row->written && bitmap_is_set(row->written, column_idx))) {
1204 if (hmap_node_is_null(&row->txn_node)) {
1205 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1206 uuid_hash(&row->uuid));
1208 if (!row->prereqs) {
1209 row->prereqs = bitmap_allocate(class->n_columns);
1211 bitmap_set1(row->prereqs, column_idx);
1215 ovsdb_idl_txn_delete(struct ovsdb_idl_row *row)
1219 ovsdb_idl_row_clear_new(row);
1220 assert(!row->prereqs);
1221 hmap_remove(&row->table->idl->txn->txn_rows, &row->txn_node);
1224 if (hmap_node_is_null(&row->txn_node)) {
1225 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1226 uuid_hash(&row->uuid));
1228 ovsdb_idl_row_clear_new(row);
1232 struct ovsdb_idl_row *
1233 ovsdb_idl_txn_insert(struct ovsdb_idl_txn *txn,
1234 const struct ovsdb_idl_table_class *class)
1236 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(class);
1237 uuid_generate(&row->uuid);
1238 row->table = ovsdb_idl_table_from_class(txn->idl, class);
1239 row->new = xmalloc(class->n_columns * sizeof *row->new);
1240 row->written = bitmap_allocate(class->n_columns);
1241 hmap_insert(&txn->txn_rows, &row->txn_node, uuid_hash(&row->uuid));
1246 ovsdb_idl_txn_abort_all(struct ovsdb_idl *idl)
1248 struct ovsdb_idl_txn *txn;
1250 HMAP_FOR_EACH (txn, struct ovsdb_idl_txn, hmap_node,
1251 &idl->outstanding_txns) {
1252 ovsdb_idl_txn_complete(txn, TXN_TRY_AGAIN);
1256 static struct ovsdb_idl_txn *
1257 ovsdb_idl_txn_find(struct ovsdb_idl *idl, const struct json *id)
1259 struct ovsdb_idl_txn *txn;
1261 HMAP_FOR_EACH_WITH_HASH (txn, struct ovsdb_idl_txn, hmap_node,
1262 json_hash(id, 0), &idl->outstanding_txns) {
1263 if (json_equal(id, txn->request_id)) {
1271 check_json_type(const struct json *json, enum json_type type, const char *name)
1274 VLOG_WARN_RL(&syntax_rl, "%s is missing", name);
1276 } else if (json->type != type) {
1277 VLOG_WARN_RL(&syntax_rl, "%s is %s instead of %s",
1278 name, json_type_to_string(json->type),
1279 json_type_to_string(type));
1287 ovsdb_idl_txn_process_inc_reply(struct ovsdb_idl_txn *txn,
1288 const struct json_array *results)
1290 struct json *count, *rows, *row, *column;
1291 struct shash *mutate, *select;
1293 if (txn->inc_index + 2 > results->n) {
1294 VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
1295 "for increment (has %u, needs %u)",
1296 results->n, txn->inc_index + 2);
1300 /* We know that this is a JSON objects because the loop in
1301 * ovsdb_idl_txn_process_reply() checked. */
1302 mutate = json_object(results->elems[txn->inc_index]);
1303 count = shash_find_data(mutate, "count");
1304 if (!check_json_type(count, JSON_INTEGER, "\"mutate\" reply \"count\"")) {
1307 if (count->u.integer != 1) {
1308 VLOG_WARN_RL(&syntax_rl,
1309 "\"mutate\" reply \"count\" is %"PRId64" instead of 1",
1314 select = json_object(results->elems[txn->inc_index + 1]);
1315 rows = shash_find_data(select, "rows");
1316 if (!check_json_type(rows, JSON_ARRAY, "\"select\" reply \"rows\"")) {
1319 if (rows->u.array.n != 1) {
1320 VLOG_WARN_RL(&syntax_rl, "\"select\" reply \"rows\" has %u elements "
1325 row = rows->u.array.elems[0];
1326 if (!check_json_type(row, JSON_OBJECT, "\"select\" reply row")) {
1329 column = shash_find_data(json_object(row), txn->inc_column);
1330 if (!check_json_type(column, JSON_INTEGER,
1331 "\"select\" reply inc column")) {
1334 txn->inc_new_value = column->u.integer;
1340 ovsdb_idl_txn_process_reply(struct ovsdb_idl *idl,
1341 const struct jsonrpc_msg *msg)
1343 struct ovsdb_idl_txn *txn;
1344 enum ovsdb_idl_txn_status status;
1346 txn = ovsdb_idl_txn_find(idl, msg->id);
1351 if (msg->type == JSONRPC_ERROR) {
1353 } else if (msg->result->type != JSON_ARRAY) {
1354 VLOG_WARN_RL(&syntax_rl, "reply to \"transact\" is not JSON array");
1357 int hard_errors = 0;
1358 int soft_errors = 0;
1361 for (i = 0; i < msg->result->u.array.n; i++) {
1362 struct json *json = msg->result->u.array.elems[i];
1364 if (json->type == JSON_NULL) {
1365 /* This isn't an error in itself but indicates that some prior
1366 * operation failed, so make sure that we know about it. */
1368 } else if (json->type == JSON_OBJECT) {
1371 error = shash_find_data(json_object(json), "error");
1373 if (error->type == JSON_STRING) {
1374 if (!strcmp(error->u.string, "timed out")) {
1376 } else if (strcmp(error->u.string, "aborted")) {
1381 VLOG_WARN_RL(&syntax_rl,
1382 "\"error\" in reply is not JSON string");
1387 VLOG_WARN_RL(&syntax_rl,
1388 "operation reply is not JSON null or object");
1395 && !ovsdb_idl_txn_process_inc_reply(txn,
1396 json_array(msg->result))) {
1400 status = (hard_errors ? TXN_ERROR
1401 : soft_errors ? TXN_TRY_AGAIN
1405 ovsdb_idl_txn_complete(txn, status);
1409 struct ovsdb_idl_txn *
1410 ovsdb_idl_txn_get(const struct ovsdb_idl_row *row)
1412 struct ovsdb_idl_txn *txn = row->table->idl->txn;
1413 assert(txn != NULL);