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_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 /* This can happen if ovsdb_idl_txn_destroy() is called to destroy
270 * a transaction before we receive the reply, so keep the log level
272 VLOG_DBG("%s: received unexpected %s message",
273 jsonrpc_session_get_name(idl->session),
274 jsonrpc_msg_type_to_string(msg->type));
277 jsonrpc_session_send(idl->session, reply);
279 jsonrpc_msg_destroy(msg);
284 ovsdb_idl_wait(struct ovsdb_idl *idl)
286 jsonrpc_session_wait(idl->session);
287 jsonrpc_session_recv_wait(idl->session);
291 ovsdb_idl_get_seqno(const struct ovsdb_idl *idl)
293 return idl->change_seqno;
297 ovsdb_idl_has_ever_connected(const struct ovsdb_idl *idl)
299 return ovsdb_idl_get_seqno(idl) != 0;
303 ovsdb_idl_force_reconnect(struct ovsdb_idl *idl)
305 jsonrpc_session_force_reconnect(idl->session);
309 ovsdb_idl_send_monitor_request(struct ovsdb_idl *idl)
311 struct json *monitor_requests;
312 struct jsonrpc_msg *msg;
315 monitor_requests = json_object_create();
316 for (i = 0; i < idl->class->n_tables; i++) {
317 const struct ovsdb_idl_table *table = &idl->tables[i];
318 const struct ovsdb_idl_table_class *tc = table->class;
319 struct json *monitor_request, *columns;
322 monitor_request = json_object_create();
323 columns = json_array_create_empty();
324 for (i = 0; i < tc->n_columns; i++) {
325 const struct ovsdb_idl_column *column = &tc->columns[i];
326 json_array_add(columns, json_string_create(column->name));
328 json_object_put(monitor_request, "columns", columns);
329 json_object_put(monitor_requests, tc->name, monitor_request);
332 json_destroy(idl->monitor_request_id);
333 msg = jsonrpc_create_request(
334 "monitor", json_array_create_2(json_null_create(), monitor_requests),
335 &idl->monitor_request_id);
336 jsonrpc_session_send(idl->session, msg);
340 ovsdb_idl_parse_update(struct ovsdb_idl *idl, const struct json *table_updates)
342 struct ovsdb_error *error;
346 error = ovsdb_idl_parse_update__(idl, table_updates);
348 if (!VLOG_DROP_WARN(&syntax_rl)) {
349 char *s = ovsdb_error_to_string(error);
350 VLOG_WARN_RL(&syntax_rl, "%s", s);
353 ovsdb_error_destroy(error);
357 static struct ovsdb_error *
358 ovsdb_idl_parse_update__(struct ovsdb_idl *idl,
359 const struct json *table_updates)
361 const struct shash_node *tables_node;
363 if (table_updates->type != JSON_OBJECT) {
364 return ovsdb_syntax_error(table_updates, NULL,
365 "<table-updates> is not an object");
367 SHASH_FOR_EACH (tables_node, json_object(table_updates)) {
368 const struct json *table_update = tables_node->data;
369 const struct shash_node *table_node;
370 struct ovsdb_idl_table *table;
372 table = shash_find_data(&idl->table_by_name, tables_node->name);
374 return ovsdb_syntax_error(
376 "<table-updates> includes unknown table \"%s\"",
380 if (table_update->type != JSON_OBJECT) {
381 return ovsdb_syntax_error(table_update, NULL,
382 "<table-update> for table \"%s\" is "
383 "not an object", table->class->name);
385 SHASH_FOR_EACH (table_node, json_object(table_update)) {
386 const struct json *row_update = table_node->data;
387 const struct json *old_json, *new_json;
390 if (!uuid_from_string(&uuid, table_node->name)) {
391 return ovsdb_syntax_error(table_update, NULL,
392 "<table-update> for table \"%s\" "
394 "\"%s\" as member name",
398 if (row_update->type != JSON_OBJECT) {
399 return ovsdb_syntax_error(row_update, NULL,
400 "<table-update> for table \"%s\" "
401 "contains <row-update> for %s that "
407 old_json = shash_find_data(json_object(row_update), "old");
408 new_json = shash_find_data(json_object(row_update), "new");
409 if (old_json && old_json->type != JSON_OBJECT) {
410 return ovsdb_syntax_error(old_json, NULL,
411 "\"old\" <row> is not object");
412 } else if (new_json && new_json->type != JSON_OBJECT) {
413 return ovsdb_syntax_error(new_json, NULL,
414 "\"new\" <row> is not object");
415 } else if ((old_json != NULL) + (new_json != NULL)
416 != shash_count(json_object(row_update))) {
417 return ovsdb_syntax_error(row_update, NULL,
418 "<row-update> contains unexpected "
420 } else if (!old_json && !new_json) {
421 return ovsdb_syntax_error(row_update, NULL,
422 "<row-update> missing \"old\" "
423 "and \"new\" members");
426 ovsdb_idl_process_update(table, &uuid, old_json, new_json);
433 static struct ovsdb_idl_row *
434 ovsdb_idl_get_row(struct ovsdb_idl_table *table, const struct uuid *uuid)
436 struct ovsdb_idl_row *row;
438 HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_idl_row, hmap_node,
439 uuid_hash(uuid), &table->rows) {
440 if (uuid_equals(&row->uuid, uuid)) {
448 ovsdb_idl_process_update(struct ovsdb_idl_table *table,
449 const struct uuid *uuid, const struct json *old,
450 const struct json *new)
452 struct ovsdb_idl_row *row;
454 row = ovsdb_idl_get_row(table, uuid);
457 if (row && !ovsdb_idl_row_is_orphan(row)) {
458 /* XXX perhaps we should check the 'old' values? */
459 ovsdb_idl_delete_row(row);
461 VLOG_WARN_RL(&semantic_rl, "cannot delete missing row "UUID_FMT" "
463 UUID_ARGS(uuid), table->class->name);
468 ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
469 } else if (ovsdb_idl_row_is_orphan(row)) {
470 ovsdb_idl_insert_row(row, new);
472 VLOG_WARN_RL(&semantic_rl, "cannot add existing row "UUID_FMT" to "
473 "table %s", UUID_ARGS(uuid), table->class->name);
474 ovsdb_idl_modify_row(row, new);
479 /* XXX perhaps we should check the 'old' values? */
480 if (!ovsdb_idl_row_is_orphan(row)) {
481 ovsdb_idl_modify_row(row, new);
483 VLOG_WARN_RL(&semantic_rl, "cannot modify missing but "
484 "referenced row "UUID_FMT" in table %s",
485 UUID_ARGS(uuid), table->class->name);
486 ovsdb_idl_insert_row(row, new);
489 VLOG_WARN_RL(&semantic_rl, "cannot modify missing row "UUID_FMT" "
490 "in table %s", UUID_ARGS(uuid), table->class->name);
491 ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
497 ovsdb_idl_row_update(struct ovsdb_idl_row *row, const struct json *row_json)
499 struct ovsdb_idl_table *table = row->table;
500 struct shash_node *node;
502 SHASH_FOR_EACH (node, json_object(row_json)) {
503 const char *column_name = node->name;
504 const struct ovsdb_idl_column *column;
505 struct ovsdb_datum datum;
506 struct ovsdb_error *error;
508 column = shash_find_data(&table->columns, column_name);
510 VLOG_WARN_RL(&syntax_rl, "unknown column %s updating row "UUID_FMT,
511 column_name, UUID_ARGS(&row->uuid));
515 error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
517 ovsdb_datum_swap(&row->old[column - table->class->columns],
519 ovsdb_datum_destroy(&datum, &column->type);
521 char *s = ovsdb_error_to_string(error);
522 VLOG_WARN_RL(&syntax_rl, "error parsing column %s in row "UUID_FMT
523 " in table %s: %s", column_name,
524 UUID_ARGS(&row->uuid), table->class->name, s);
526 ovsdb_error_destroy(error);
532 ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *row)
538 ovsdb_idl_row_clear_old(struct ovsdb_idl_row *row)
540 assert(row->old == row->new);
541 if (!ovsdb_idl_row_is_orphan(row)) {
542 const struct ovsdb_idl_table_class *class = row->table->class;
545 for (i = 0; i < class->n_columns; i++) {
546 ovsdb_datum_destroy(&row->old[i], &class->columns[i].type);
549 row->old = row->new = NULL;
554 ovsdb_idl_row_clear_new(struct ovsdb_idl_row *row)
556 if (row->old != row->new) {
558 const struct ovsdb_idl_table_class *class = row->table->class;
561 BITMAP_FOR_EACH_1 (i, class->n_columns, row->written) {
562 ovsdb_datum_destroy(&row->new[i], &class->columns[i].type);
573 ovsdb_idl_row_clear_arcs(struct ovsdb_idl_row *row, bool destroy_dsts)
575 struct ovsdb_idl_arc *arc, *next;
577 /* Delete all forward arcs. If 'destroy_dsts', destroy any orphaned rows
578 * that this causes to be unreferenced. */
579 LIST_FOR_EACH_SAFE (arc, next, struct ovsdb_idl_arc, src_node,
581 list_remove(&arc->dst_node);
583 && ovsdb_idl_row_is_orphan(arc->dst)
584 && list_is_empty(&arc->dst->dst_arcs)) {
585 ovsdb_idl_row_destroy(arc->dst);
589 list_init(&row->src_arcs);
592 /* Force nodes that reference 'row' to reparse. */
594 ovsdb_idl_row_reparse_backrefs(struct ovsdb_idl_row *row, bool destroy_dsts)
596 struct ovsdb_idl_arc *arc, *next;
598 /* This is trickier than it looks. ovsdb_idl_row_clear_arcs() will destroy
599 * 'arc', so we need to use the "safe" variant of list traversal. However,
600 * calling ref->table->class->parse will add an arc equivalent to 'arc' to
601 * row->arcs. That could be a problem for traversal, but it adds it at the
602 * beginning of the list to prevent us from stumbling upon it again.
604 * (If duplicate arcs were possible then we would need to make sure that
605 * 'next' didn't also point into 'arc''s destination, but we forbid
606 * duplicate arcs.) */
607 LIST_FOR_EACH_SAFE (arc, next, struct ovsdb_idl_arc, dst_node,
609 struct ovsdb_idl_row *ref = arc->src;
611 (ref->table->class->unparse)(ref);
612 ovsdb_idl_row_clear_arcs(ref, destroy_dsts);
613 (ref->table->class->parse)(ref);
617 static struct ovsdb_idl_row *
618 ovsdb_idl_row_create__(const struct ovsdb_idl_table_class *class)
620 struct ovsdb_idl_row *row = xzalloc(class->allocation_size);
621 memset(row, 0, sizeof *row);
622 list_init(&row->src_arcs);
623 list_init(&row->dst_arcs);
624 hmap_node_nullify(&row->txn_node);
628 static struct ovsdb_idl_row *
629 ovsdb_idl_row_create(struct ovsdb_idl_table *table, const struct uuid *uuid)
631 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(table->class);
632 hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
639 ovsdb_idl_row_destroy(struct ovsdb_idl_row *row)
642 ovsdb_idl_row_clear_old(row);
643 hmap_remove(&row->table->rows, &row->hmap_node);
649 ovsdb_idl_insert_row(struct ovsdb_idl_row *row, const struct json *row_json)
651 const struct ovsdb_idl_table_class *class = row->table->class;
654 assert(!row->old && !row->new);
655 row->old = row->new = xmalloc(class->n_columns * sizeof *row->old);
656 for (i = 0; i < class->n_columns; i++) {
657 ovsdb_datum_init_default(&row->old[i], &class->columns[i].type);
659 ovsdb_idl_row_update(row, row_json);
662 ovsdb_idl_row_reparse_backrefs(row, false);
666 ovsdb_idl_delete_row(struct ovsdb_idl_row *row)
668 (row->table->class->unparse)(row);
669 ovsdb_idl_row_clear_arcs(row, true);
670 ovsdb_idl_row_clear_old(row);
671 if (list_is_empty(&row->dst_arcs)) {
672 ovsdb_idl_row_destroy(row);
674 ovsdb_idl_row_reparse_backrefs(row, true);
679 ovsdb_idl_modify_row(struct ovsdb_idl_row *row, const struct json *row_json)
681 (row->table->class->unparse)(row);
682 ovsdb_idl_row_clear_arcs(row, true);
683 ovsdb_idl_row_update(row, row_json);
684 (row->table->class->parse)(row);
688 may_add_arc(const struct ovsdb_idl_row *src, const struct ovsdb_idl_row *dst)
690 const struct ovsdb_idl_arc *arc;
697 /* No duplicate arcs.
699 * We only need to test whether the first arc in dst->dst_arcs originates
700 * at 'src', since we add all of the arcs from a given source in a clump
701 * (in a single call to a row's ->parse function) and new arcs are always
702 * added at the front of the dst_arcs list. */
703 if (list_is_empty(&dst->dst_arcs)) {
706 arc = CONTAINER_OF(dst->dst_arcs.next, struct ovsdb_idl_arc, dst_node);
707 return arc->src != src;
710 static struct ovsdb_idl_table *
711 ovsdb_idl_table_from_class(const struct ovsdb_idl *idl,
712 const struct ovsdb_idl_table_class *table_class)
714 return &idl->tables[table_class - idl->class->tables];
717 struct ovsdb_idl_row *
718 ovsdb_idl_get_row_arc(struct ovsdb_idl_row *src,
719 struct ovsdb_idl_table_class *dst_table_class,
720 const struct uuid *dst_uuid)
722 struct ovsdb_idl *idl = src->table->idl;
723 struct ovsdb_idl_table *dst_table;
724 struct ovsdb_idl_arc *arc;
725 struct ovsdb_idl_row *dst;
727 dst_table = ovsdb_idl_table_from_class(idl, dst_table_class);
728 dst = ovsdb_idl_get_row(dst_table, dst_uuid);
730 dst = ovsdb_idl_row_create(dst_table, dst_uuid);
733 /* Add a new arc, if it wouldn't be a self-arc or a duplicate arc. */
734 if (may_add_arc(src, dst)) {
735 /* The arc *must* be added at the front of the dst_arcs list. See
736 * ovsdb_idl_row_reparse_backrefs() for details. */
737 arc = xmalloc(sizeof *arc);
738 list_push_front(&src->src_arcs, &arc->src_node);
739 list_push_front(&dst->dst_arcs, &arc->dst_node);
744 return !ovsdb_idl_row_is_orphan(dst) ? dst : NULL;
747 static struct ovsdb_idl_row *
748 next_real_row(struct ovsdb_idl_table *table, struct hmap_node *node)
750 for (; node; node = hmap_next(&table->rows, node)) {
751 struct ovsdb_idl_row *row;
753 row = CONTAINER_OF(node, struct ovsdb_idl_row, hmap_node);
754 if (!ovsdb_idl_row_is_orphan(row)) {
761 struct ovsdb_idl_row *
762 ovsdb_idl_first_row(const struct ovsdb_idl *idl,
763 const struct ovsdb_idl_table_class *table_class)
765 struct ovsdb_idl_table *table
766 = ovsdb_idl_table_from_class(idl, table_class);
767 return next_real_row(table, hmap_first(&table->rows));
770 struct ovsdb_idl_row *
771 ovsdb_idl_next_row(const struct ovsdb_idl_row *row)
773 struct ovsdb_idl_table *table = row->table;
775 return next_real_row(table, hmap_next(&table->rows, &row->hmap_node));
780 static void ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
781 enum ovsdb_idl_txn_status);
784 ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status status)
803 struct ovsdb_idl_txn *
804 ovsdb_idl_txn_create(struct ovsdb_idl *idl)
806 struct ovsdb_idl_txn *txn;
809 idl->txn = txn = xmalloc(sizeof *txn);
811 txn->status = TXN_INCOMPLETE;
812 hmap_init(&txn->txn_rows);
813 txn->dry_run = false;
814 ds_init(&txn->comment);
815 txn->inc_table = NULL;
816 txn->inc_column = NULL;
817 txn->inc_where = NULL;
822 ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *txn, const char *s)
824 if (txn->comment.length) {
825 ds_put_char(&txn->comment, '\n');
827 ds_put_cstr(&txn->comment, s);
831 ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *txn)
837 ovsdb_idl_txn_increment(struct ovsdb_idl_txn *txn, const char *table,
838 const char *column, const struct json *where)
840 assert(!txn->inc_table);
841 txn->inc_table = xstrdup(table);
842 txn->inc_column = xstrdup(column);
843 txn->inc_where = where ? json_clone(where) : json_array_create_empty();
847 ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *txn)
849 if (txn->status == TXN_INCOMPLETE) {
850 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
852 ovsdb_idl_txn_abort(txn);
853 ds_destroy(&txn->comment);
854 free(txn->inc_table);
855 free(txn->inc_column);
856 json_destroy(txn->inc_where);
861 ovsdb_idl_txn_wait(const struct ovsdb_idl_txn *txn)
863 if (txn->status != TXN_INCOMPLETE) {
864 poll_immediate_wake();
869 where_uuid_equals(const struct uuid *uuid)
874 json_string_create("_uuid"),
875 json_string_create("=="),
877 json_string_create("uuid"),
878 json_string_create_nocopy(
879 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
883 uuid_name_from_uuid(const struct uuid *uuid)
888 name = xasprintf("row"UUID_FMT, UUID_ARGS(uuid));
889 for (p = name; *p != '\0'; p++) {
898 static const struct ovsdb_idl_row *
899 ovsdb_idl_txn_get_row(const struct ovsdb_idl_txn *txn, const struct uuid *uuid)
901 const struct ovsdb_idl_row *row;
903 HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_idl_row, txn_node,
904 uuid_hash(uuid), &txn->txn_rows) {
905 if (uuid_equals(&row->uuid, uuid)) {
912 /* XXX there must be a cleaner way to do this */
914 substitute_uuids(struct json *json, const struct ovsdb_idl_txn *txn)
916 if (json->type == JSON_ARRAY) {
920 if (json->u.array.n == 2
921 && json->u.array.elems[0]->type == JSON_STRING
922 && json->u.array.elems[1]->type == JSON_STRING
923 && !strcmp(json->u.array.elems[0]->u.string, "uuid")
924 && uuid_from_string(&uuid, json->u.array.elems[1]->u.string)) {
925 const struct ovsdb_idl_row *row;
927 row = ovsdb_idl_txn_get_row(txn, &uuid);
928 if (row && !row->old && row->new) {
931 return json_array_create_2(
932 json_string_create("named-uuid"),
933 json_string_create_nocopy(uuid_name_from_uuid(&uuid)));
937 for (i = 0; i < json->u.array.n; i++) {
938 json->u.array.elems[i] = substitute_uuids(json->u.array.elems[i],
941 } else if (json->type == JSON_OBJECT) {
942 struct shash_node *node;
944 SHASH_FOR_EACH (node, json_object(json)) {
945 node->data = substitute_uuids(node->data, txn);
952 ovsdb_idl_txn_disassemble(struct ovsdb_idl_txn *txn)
954 struct ovsdb_idl_row *row, *next;
956 HMAP_FOR_EACH_SAFE (row, next, struct ovsdb_idl_row, txn_node,
958 if (row->old && row->written) {
959 (row->table->class->unparse)(row);
960 ovsdb_idl_row_clear_arcs(row, false);
961 (row->table->class->parse)(row);
963 ovsdb_idl_row_clear_new(row);
971 hmap_remove(&txn->txn_rows, &row->txn_node);
972 hmap_node_nullify(&row->txn_node);
974 hmap_destroy(&txn->txn_rows);
975 hmap_init(&txn->txn_rows);
978 enum ovsdb_idl_txn_status
979 ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
981 struct ovsdb_idl_row *row;
982 struct json *operations;
985 if (txn != txn->idl->txn) {
989 operations = json_array_create_empty();
991 /* Add prerequisites and declarations of new rows. */
992 HMAP_FOR_EACH (row, struct ovsdb_idl_row, txn_node, &txn->txn_rows) {
993 /* XXX check that deleted rows exist even if no prereqs? */
995 const struct ovsdb_idl_table_class *class = row->table->class;
996 size_t n_columns = class->n_columns;
997 struct json *op, *columns, *row_json;
1000 op = json_object_create();
1001 json_array_add(operations, op);
1002 json_object_put_string(op, "op", "wait");
1003 json_object_put_string(op, "table", class->name);
1004 json_object_put(op, "timeout", json_integer_create(0));
1005 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1006 json_object_put_string(op, "until", "==");
1007 columns = json_array_create_empty();
1008 json_object_put(op, "columns", columns);
1009 row_json = json_object_create();
1010 json_object_put(op, "rows", json_array_create_1(row_json));
1012 BITMAP_FOR_EACH_1 (idx, n_columns, row->prereqs) {
1013 const struct ovsdb_idl_column *column = &class->columns[idx];
1014 json_array_add(columns, json_string_create(column->name));
1015 json_object_put(row_json, column->name,
1016 ovsdb_datum_to_json(&row->old[idx],
1020 if (row->new && !row->old) {
1023 op = json_object_create();
1024 json_array_add(operations, op);
1025 json_object_put_string(op, "op", "declare");
1026 json_object_put(op, "uuid-name",
1027 json_string_create_nocopy(
1028 uuid_name_from_uuid(&row->uuid)));
1033 any_updates = false;
1034 HMAP_FOR_EACH (row, struct ovsdb_idl_row, txn_node, &txn->txn_rows) {
1035 const struct ovsdb_idl_table_class *class = row->table->class;
1037 if (row->old == row->new) {
1039 } else if (!row->new) {
1040 struct json *op = json_object_create();
1041 json_object_put_string(op, "op", "delete");
1042 json_object_put_string(op, "table", class->name);
1043 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1044 json_array_add(operations, op);
1047 struct json *row_json;
1051 op = json_object_create();
1052 json_object_put_string(op, "op", row->old ? "update" : "insert");
1053 json_object_put_string(op, "table", class->name);
1055 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1057 json_object_put(op, "uuid-name",
1058 json_string_create_nocopy(
1059 uuid_name_from_uuid(&row->uuid)));
1061 row_json = json_object_create();
1062 json_object_put(op, "row", row_json);
1064 BITMAP_FOR_EACH_1 (idx, class->n_columns, row->written) {
1065 const struct ovsdb_idl_column *column = &class->columns[idx];
1067 if (!row->old || !ovsdb_datum_equals(&row->old[idx],
1070 json_object_put(row_json, column->name,
1072 ovsdb_datum_to_json(&row->new[idx],
1078 if (!row->old || !shash_is_empty(json_object(row_json))) {
1079 json_array_add(operations, op);
1087 /* Add increment. */
1088 if (txn->inc_table && any_updates) {
1091 txn->inc_index = operations->u.array.n;
1093 op = json_object_create();
1094 json_object_put_string(op, "op", "mutate");
1095 json_object_put_string(op, "table", txn->inc_table);
1096 json_object_put(op, "where",
1097 substitute_uuids(json_clone(txn->inc_where), txn));
1098 json_object_put(op, "mutations",
1099 json_array_create_1(
1100 json_array_create_3(
1101 json_string_create(txn->inc_column),
1102 json_string_create("+="),
1103 json_integer_create(1))));
1104 json_array_add(operations, op);
1106 op = json_object_create();
1107 json_object_put_string(op, "op", "select");
1108 json_object_put_string(op, "table", txn->inc_table);
1109 json_object_put(op, "where",
1110 substitute_uuids(json_clone(txn->inc_where), txn));
1111 json_object_put(op, "columns",
1112 json_array_create_1(json_string_create(
1114 json_array_add(operations, op);
1117 if (txn->comment.length) {
1118 struct json *op = json_object_create();
1119 json_object_put_string(op, "op", "comment");
1120 json_object_put_string(op, "comment", ds_cstr(&txn->comment));
1121 json_array_add(operations, op);
1125 struct json *op = json_object_create();
1126 json_object_put_string(op, "op", "abort");
1127 json_array_add(operations, op);
1131 txn->status = TXN_UNCHANGED;
1132 json_destroy(operations);
1133 } else if (!jsonrpc_session_send(
1135 jsonrpc_create_request(
1136 "transact", operations, &txn->request_id))) {
1137 hmap_insert(&txn->idl->outstanding_txns, &txn->hmap_node,
1138 json_hash(txn->request_id, 0));
1140 txn->status = TXN_INCOMPLETE;
1143 txn->idl->txn = NULL;
1144 ovsdb_idl_txn_disassemble(txn);
1149 ovsdb_idl_txn_get_increment_new_value(const struct ovsdb_idl_txn *txn)
1151 assert(txn->status == TXN_SUCCESS);
1152 return txn->inc_new_value;
1156 ovsdb_idl_txn_abort(struct ovsdb_idl_txn *txn)
1158 ovsdb_idl_txn_disassemble(txn);
1159 if (txn->status == TXN_INCOMPLETE) {
1160 txn->status = TXN_ABORTED;
1165 ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
1166 enum ovsdb_idl_txn_status status)
1168 txn->status = status;
1169 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
1173 ovsdb_idl_txn_write(struct ovsdb_idl_row *row,
1174 const struct ovsdb_idl_column *column,
1175 struct ovsdb_datum *datum)
1177 const struct ovsdb_idl_table_class *class = row->table->class;
1178 size_t column_idx = column - class->columns;
1181 if (hmap_node_is_null(&row->txn_node)) {
1182 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1183 uuid_hash(&row->uuid));
1185 if (row->old == row->new) {
1186 row->new = xmalloc(class->n_columns * sizeof *row->new);
1188 if (!row->written) {
1189 row->written = bitmap_allocate(class->n_columns);
1191 if (bitmap_is_set(row->written, column_idx)) {
1192 ovsdb_datum_destroy(&row->new[column_idx], &column->type);
1194 bitmap_set1(row->written, column_idx);
1196 row->new[column_idx] = *datum;
1200 ovsdb_idl_txn_verify(const struct ovsdb_idl_row *row_,
1201 const struct ovsdb_idl_column *column)
1203 struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1204 const struct ovsdb_idl_table_class *class = row->table->class;
1205 size_t column_idx = column - class->columns;
1209 || (row->written && bitmap_is_set(row->written, column_idx))) {
1213 if (hmap_node_is_null(&row->txn_node)) {
1214 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1215 uuid_hash(&row->uuid));
1217 if (!row->prereqs) {
1218 row->prereqs = bitmap_allocate(class->n_columns);
1220 bitmap_set1(row->prereqs, column_idx);
1224 ovsdb_idl_txn_delete(struct ovsdb_idl_row *row)
1228 ovsdb_idl_row_clear_new(row);
1229 assert(!row->prereqs);
1230 hmap_remove(&row->table->idl->txn->txn_rows, &row->txn_node);
1234 if (hmap_node_is_null(&row->txn_node)) {
1235 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1236 uuid_hash(&row->uuid));
1238 ovsdb_idl_row_clear_new(row);
1242 struct ovsdb_idl_row *
1243 ovsdb_idl_txn_insert(struct ovsdb_idl_txn *txn,
1244 const struct ovsdb_idl_table_class *class)
1246 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(class);
1247 uuid_generate(&row->uuid);
1248 row->table = ovsdb_idl_table_from_class(txn->idl, class);
1249 row->new = xmalloc(class->n_columns * sizeof *row->new);
1250 row->written = bitmap_allocate(class->n_columns);
1251 hmap_insert(&txn->txn_rows, &row->txn_node, uuid_hash(&row->uuid));
1256 ovsdb_idl_txn_abort_all(struct ovsdb_idl *idl)
1258 struct ovsdb_idl_txn *txn;
1260 HMAP_FOR_EACH (txn, struct ovsdb_idl_txn, hmap_node,
1261 &idl->outstanding_txns) {
1262 ovsdb_idl_txn_complete(txn, TXN_TRY_AGAIN);
1266 static struct ovsdb_idl_txn *
1267 ovsdb_idl_txn_find(struct ovsdb_idl *idl, const struct json *id)
1269 struct ovsdb_idl_txn *txn;
1271 HMAP_FOR_EACH_WITH_HASH (txn, struct ovsdb_idl_txn, hmap_node,
1272 json_hash(id, 0), &idl->outstanding_txns) {
1273 if (json_equal(id, txn->request_id)) {
1281 check_json_type(const struct json *json, enum json_type type, const char *name)
1284 VLOG_WARN_RL(&syntax_rl, "%s is missing", name);
1286 } else if (json->type != type) {
1287 VLOG_WARN_RL(&syntax_rl, "%s is %s instead of %s",
1288 name, json_type_to_string(json->type),
1289 json_type_to_string(type));
1297 ovsdb_idl_txn_process_inc_reply(struct ovsdb_idl_txn *txn,
1298 const struct json_array *results)
1300 struct json *count, *rows, *row, *column;
1301 struct shash *mutate, *select;
1303 if (txn->inc_index + 2 > results->n) {
1304 VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
1305 "for increment (has %u, needs %u)",
1306 results->n, txn->inc_index + 2);
1310 /* We know that this is a JSON objects because the loop in
1311 * ovsdb_idl_txn_process_reply() checked. */
1312 mutate = json_object(results->elems[txn->inc_index]);
1313 count = shash_find_data(mutate, "count");
1314 if (!check_json_type(count, JSON_INTEGER, "\"mutate\" reply \"count\"")) {
1317 if (count->u.integer != 1) {
1318 VLOG_WARN_RL(&syntax_rl,
1319 "\"mutate\" reply \"count\" is %"PRId64" instead of 1",
1324 select = json_object(results->elems[txn->inc_index + 1]);
1325 rows = shash_find_data(select, "rows");
1326 if (!check_json_type(rows, JSON_ARRAY, "\"select\" reply \"rows\"")) {
1329 if (rows->u.array.n != 1) {
1330 VLOG_WARN_RL(&syntax_rl, "\"select\" reply \"rows\" has %u elements "
1335 row = rows->u.array.elems[0];
1336 if (!check_json_type(row, JSON_OBJECT, "\"select\" reply row")) {
1339 column = shash_find_data(json_object(row), txn->inc_column);
1340 if (!check_json_type(column, JSON_INTEGER,
1341 "\"select\" reply inc column")) {
1344 txn->inc_new_value = column->u.integer;
1350 ovsdb_idl_txn_process_reply(struct ovsdb_idl *idl,
1351 const struct jsonrpc_msg *msg)
1353 struct ovsdb_idl_txn *txn;
1354 enum ovsdb_idl_txn_status status;
1356 txn = ovsdb_idl_txn_find(idl, msg->id);
1361 if (msg->type == JSONRPC_ERROR) {
1363 } else if (msg->result->type != JSON_ARRAY) {
1364 VLOG_WARN_RL(&syntax_rl, "reply to \"transact\" is not JSON array");
1367 int hard_errors = 0;
1368 int soft_errors = 0;
1371 for (i = 0; i < msg->result->u.array.n; i++) {
1372 struct json *json = msg->result->u.array.elems[i];
1374 if (json->type == JSON_NULL) {
1375 /* This isn't an error in itself but indicates that some prior
1376 * operation failed, so make sure that we know about it. */
1378 } else if (json->type == JSON_OBJECT) {
1381 error = shash_find_data(json_object(json), "error");
1383 if (error->type == JSON_STRING) {
1384 if (!strcmp(error->u.string, "timed out")) {
1386 } else if (strcmp(error->u.string, "aborted")) {
1391 VLOG_WARN_RL(&syntax_rl,
1392 "\"error\" in reply is not JSON string");
1397 VLOG_WARN_RL(&syntax_rl,
1398 "operation reply is not JSON null or object");
1405 && !ovsdb_idl_txn_process_inc_reply(txn,
1406 json_array(msg->result))) {
1410 status = (hard_errors ? TXN_ERROR
1411 : soft_errors ? TXN_TRY_AGAIN
1415 ovsdb_idl_txn_complete(txn, status);
1419 struct ovsdb_idl_txn *
1420 ovsdb_idl_txn_get(const struct ovsdb_idl_row *row)
1422 struct ovsdb_idl_txn *txn = row->table->idl->txn;
1423 assert(txn != NULL);