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"
28 #include "ovsdb-data.h"
29 #include "ovsdb-error.h"
30 #include "ovsdb-idl-provider.h"
31 #include "poll-loop.h"
35 #define THIS_MODULE VLM_ovsdb_idl
38 /* An arc from one idl_row to another. When row A contains a UUID that
39 * references row B, this is represented by an arc from A (the source) to B
42 * Arcs from a row to itself are omitted, that is, src and dst are always
45 * Arcs are never duplicated, that is, even if there are multiple references
46 * from A to B, there is only a single arc from A to B.
48 * Arcs are directed: an arc from A to B is the converse of an an arc from B to
49 * A. Both an arc and its converse may both be present, if each row refers
50 * to the other circularly.
52 * The source and destination row may be in the same table or in different
55 struct ovsdb_idl_arc {
56 struct list src_node; /* In src->src_arcs list. */
57 struct list dst_node; /* In dst->dst_arcs list. */
58 struct ovsdb_idl_row *src; /* Source row. */
59 struct ovsdb_idl_row *dst; /* Destination row. */
63 const struct ovsdb_idl_class *class;
64 struct jsonrpc_session *session;
65 struct shash table_by_name;
66 struct ovsdb_idl_table *tables;
67 struct json *monitor_request_id;
68 unsigned int last_monitor_request_seqno;
69 unsigned int change_seqno;
71 /* Transaction support. */
72 struct ovsdb_idl_txn *txn;
73 struct hmap outstanding_txns;
76 struct ovsdb_idl_txn {
77 struct hmap_node hmap_node;
78 struct json *request_id;
79 struct ovsdb_idl *idl;
81 enum ovsdb_idl_txn_status status;
85 static struct vlog_rate_limit syntax_rl = VLOG_RATE_LIMIT_INIT(1, 5);
86 static struct vlog_rate_limit semantic_rl = VLOG_RATE_LIMIT_INIT(1, 5);
88 static void ovsdb_idl_clear(struct ovsdb_idl *);
89 static void ovsdb_idl_send_monitor_request(struct ovsdb_idl *);
90 static void ovsdb_idl_parse_update(struct ovsdb_idl *, const struct json *);
91 static struct ovsdb_error *ovsdb_idl_parse_update__(struct ovsdb_idl *,
93 static void ovsdb_idl_process_update(struct ovsdb_idl_table *,
95 const struct json *old,
96 const struct json *new);
97 static void ovsdb_idl_insert_row(struct ovsdb_idl_row *, const struct json *);
98 static void ovsdb_idl_delete_row(struct ovsdb_idl_row *);
99 static void ovsdb_idl_modify_row(struct ovsdb_idl_row *, const struct json *);
101 static bool ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *);
102 static struct ovsdb_idl_row *ovsdb_idl_row_create__(
103 const struct ovsdb_idl_table_class *);
104 static struct ovsdb_idl_row *ovsdb_idl_row_create(struct ovsdb_idl_table *,
105 const struct uuid *);
106 static void ovsdb_idl_row_destroy(struct ovsdb_idl_row *);
108 static void ovsdb_idl_row_clear_old(struct ovsdb_idl_row *);
109 static void ovsdb_idl_row_clear_new(struct ovsdb_idl_row *);
111 static void ovsdb_idl_txn_abort_all(struct ovsdb_idl *);
112 static bool ovsdb_idl_txn_process_reply(struct ovsdb_idl *,
113 const struct jsonrpc_msg *msg);
116 ovsdb_idl_create(const char *remote, const struct ovsdb_idl_class *class)
118 struct ovsdb_idl *idl;
121 idl = xzalloc(sizeof *idl);
123 idl->session = jsonrpc_session_open(remote);
124 shash_init(&idl->table_by_name);
125 idl->tables = xmalloc(class->n_tables * sizeof *idl->tables);
126 for (i = 0; i < class->n_tables; i++) {
127 const struct ovsdb_idl_table_class *tc = &class->tables[i];
128 struct ovsdb_idl_table *table = &idl->tables[i];
131 assert(!shash_find(&idl->table_by_name, tc->name));
132 shash_add(&idl->table_by_name, tc->name, table);
134 shash_init(&table->columns);
135 for (j = 0; j < tc->n_columns; j++) {
136 const struct ovsdb_idl_column *column = &tc->columns[j];
138 assert(!shash_find(&table->columns, column->name));
139 shash_add(&table->columns, column->name, column);
141 hmap_init(&table->rows);
144 idl->last_monitor_request_seqno = UINT_MAX;
145 hmap_init(&idl->outstanding_txns);
151 ovsdb_idl_destroy(struct ovsdb_idl *idl)
157 ovsdb_idl_clear(idl);
158 jsonrpc_session_close(idl->session);
160 for (i = 0; i < idl->class->n_tables; i++) {
161 struct ovsdb_idl_table *table = &idl->tables[i];
162 shash_destroy(&table->columns);
163 hmap_destroy(&table->rows);
165 shash_destroy(&idl->table_by_name);
167 json_destroy(idl->monitor_request_id);
173 ovsdb_idl_clear(struct ovsdb_idl *idl)
175 bool changed = false;
178 for (i = 0; i < idl->class->n_tables; i++) {
179 struct ovsdb_idl_table *table = &idl->tables[i];
180 struct ovsdb_idl_row *row, *next_row;
182 if (hmap_is_empty(&table->rows)) {
187 HMAP_FOR_EACH_SAFE (row, next_row, struct ovsdb_idl_row, hmap_node,
189 struct ovsdb_idl_arc *arc, *next_arc;
191 if (!ovsdb_idl_row_is_orphan(row)) {
192 (row->table->class->unparse)(row);
193 ovsdb_idl_row_clear_old(row);
195 hmap_remove(&table->rows, &row->hmap_node);
196 LIST_FOR_EACH_SAFE (arc, next_arc, struct ovsdb_idl_arc, src_node,
200 /* No need to do anything with dst_arcs: some node has those arcs
201 * as forward arcs and will destroy them itself. */
213 ovsdb_idl_run(struct ovsdb_idl *idl)
218 jsonrpc_session_run(idl->session);
219 for (i = 0; jsonrpc_session_is_connected(idl->session) && i < 50; i++) {
220 struct jsonrpc_msg *msg, *reply;
223 seqno = jsonrpc_session_get_seqno(idl->session);
224 if (idl->last_monitor_request_seqno != seqno) {
225 idl->last_monitor_request_seqno = seqno;
226 ovsdb_idl_txn_abort_all(idl);
227 ovsdb_idl_send_monitor_request(idl);
231 msg = jsonrpc_session_recv(idl->session);
237 if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) {
238 reply = jsonrpc_create_reply(json_clone(msg->params), msg->id);
239 } else if (msg->type == JSONRPC_NOTIFY
240 && !strcmp(msg->method, "update")
241 && msg->params->type == JSON_ARRAY
242 && msg->params->u.array.n == 2
243 && msg->params->u.array.elems[0]->type == JSON_NULL) {
244 ovsdb_idl_parse_update(idl, msg->params->u.array.elems[1]);
245 } else if (msg->type == JSONRPC_REPLY
246 && idl->monitor_request_id
247 && json_equal(idl->monitor_request_id, msg->id)) {
248 json_destroy(idl->monitor_request_id);
249 idl->monitor_request_id = NULL;
250 ovsdb_idl_clear(idl);
251 ovsdb_idl_parse_update(idl, msg->result);
252 } else if (msg->type == JSONRPC_REPLY
253 && msg->id && msg->id->type == JSON_STRING
254 && !strcmp(msg->id->u.string, "echo")) {
255 /* It's a reply to our echo request. Ignore it. */
256 } else if ((msg->type == JSONRPC_ERROR
257 || msg->type == JSONRPC_REPLY)
258 && ovsdb_idl_txn_process_reply(idl, msg)) {
259 /* ovsdb_idl_txn_process_reply() did everything needful. */
261 VLOG_WARN("%s: received unexpected %s message",
262 jsonrpc_session_get_name(idl->session),
263 jsonrpc_msg_type_to_string(msg->type));
264 jsonrpc_session_force_reconnect(idl->session);
267 jsonrpc_session_send(idl->session, reply);
269 jsonrpc_msg_destroy(msg);
274 ovsdb_idl_wait(struct ovsdb_idl *idl)
276 jsonrpc_session_wait(idl->session);
277 jsonrpc_session_recv_wait(idl->session);
281 ovsdb_idl_get_seqno(const struct ovsdb_idl *idl)
283 return idl->change_seqno;
287 ovsdb_idl_force_reconnect(struct ovsdb_idl *idl)
289 jsonrpc_session_force_reconnect(idl->session);
293 ovsdb_idl_send_monitor_request(struct ovsdb_idl *idl)
295 struct json *monitor_requests;
296 struct jsonrpc_msg *msg;
299 monitor_requests = json_object_create();
300 for (i = 0; i < idl->class->n_tables; i++) {
301 const struct ovsdb_idl_table *table = &idl->tables[i];
302 const struct ovsdb_idl_table_class *tc = table->class;
303 struct json *monitor_request, *columns;
306 monitor_request = json_object_create();
307 columns = json_array_create_empty();
308 for (i = 0; i < tc->n_columns; i++) {
309 const struct ovsdb_idl_column *column = &tc->columns[i];
310 json_array_add(columns, json_string_create(column->name));
312 json_object_put(monitor_request, "columns", columns);
313 json_object_put(monitor_requests, tc->name, monitor_request);
316 json_destroy(idl->monitor_request_id);
317 msg = jsonrpc_create_request(
318 "monitor", json_array_create_2(json_null_create(), monitor_requests),
319 &idl->monitor_request_id);
320 jsonrpc_session_send(idl->session, msg);
324 ovsdb_idl_parse_update(struct ovsdb_idl *idl, const struct json *table_updates)
326 struct ovsdb_error *error;
330 error = ovsdb_idl_parse_update__(idl, table_updates);
332 if (!VLOG_DROP_WARN(&syntax_rl)) {
333 char *s = ovsdb_error_to_string(error);
334 VLOG_WARN_RL(&syntax_rl, "%s", s);
337 ovsdb_error_destroy(error);
341 static struct ovsdb_error *
342 ovsdb_idl_parse_update__(struct ovsdb_idl *idl,
343 const struct json *table_updates)
345 const struct shash_node *tables_node;
347 if (table_updates->type != JSON_OBJECT) {
348 return ovsdb_syntax_error(table_updates, NULL,
349 "<table-updates> is not an object");
351 SHASH_FOR_EACH (tables_node, json_object(table_updates)) {
352 const struct json *table_update = tables_node->data;
353 const struct shash_node *table_node;
354 struct ovsdb_idl_table *table;
356 table = shash_find_data(&idl->table_by_name, tables_node->name);
358 return ovsdb_syntax_error(
360 "<table-updates> includes unknown table \"%s\"",
364 if (table_update->type != JSON_OBJECT) {
365 return ovsdb_syntax_error(table_update, NULL,
366 "<table-update> for table \"%s\" is "
367 "not an object", table->class->name);
369 SHASH_FOR_EACH (table_node, json_object(table_update)) {
370 const struct json *row_update = table_node->data;
371 const struct json *old_json, *new_json;
374 if (!uuid_from_string(&uuid, table_node->name)) {
375 return ovsdb_syntax_error(table_update, NULL,
376 "<table-update> for table \"%s\" "
378 "\"%s\" as member name",
382 if (row_update->type != JSON_OBJECT) {
383 return ovsdb_syntax_error(row_update, NULL,
384 "<table-update> for table \"%s\" "
385 "contains <row-update> for %s that "
391 old_json = shash_find_data(json_object(row_update), "old");
392 new_json = shash_find_data(json_object(row_update), "new");
393 if (old_json && old_json->type != JSON_OBJECT) {
394 return ovsdb_syntax_error(old_json, NULL,
395 "\"old\" <row> is not object");
396 } else if (new_json && new_json->type != JSON_OBJECT) {
397 return ovsdb_syntax_error(new_json, NULL,
398 "\"new\" <row> is not object");
399 } else if ((old_json != NULL) + (new_json != NULL)
400 != shash_count(json_object(row_update))) {
401 return ovsdb_syntax_error(row_update, NULL,
402 "<row-update> contains unexpected "
404 } else if (!old_json && !new_json) {
405 return ovsdb_syntax_error(row_update, NULL,
406 "<row-update> missing \"old\" "
407 "and \"new\" members");
410 ovsdb_idl_process_update(table, &uuid, old_json, new_json);
417 static struct ovsdb_idl_row *
418 ovsdb_idl_get_row(struct ovsdb_idl_table *table, const struct uuid *uuid)
420 struct ovsdb_idl_row *row;
422 HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_idl_row, hmap_node,
423 uuid_hash(uuid), &table->rows) {
424 if (uuid_equals(&row->uuid, uuid)) {
432 ovsdb_idl_process_update(struct ovsdb_idl_table *table,
433 const struct uuid *uuid, const struct json *old,
434 const struct json *new)
436 struct ovsdb_idl_row *row;
438 row = ovsdb_idl_get_row(table, uuid);
441 if (row && !ovsdb_idl_row_is_orphan(row)) {
442 /* XXX perhaps we should check the 'old' values? */
443 ovsdb_idl_delete_row(row);
445 VLOG_WARN_RL(&semantic_rl, "cannot delete missing row "UUID_FMT" "
447 UUID_ARGS(uuid), table->class->name);
452 ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
453 } else if (ovsdb_idl_row_is_orphan(row)) {
454 ovsdb_idl_insert_row(row, new);
456 VLOG_WARN_RL(&semantic_rl, "cannot add existing row "UUID_FMT" to "
457 "table %s", UUID_ARGS(uuid), table->class->name);
458 ovsdb_idl_modify_row(row, new);
463 /* XXX perhaps we should check the 'old' values? */
464 if (!ovsdb_idl_row_is_orphan(row)) {
465 ovsdb_idl_modify_row(row, new);
467 VLOG_WARN_RL(&semantic_rl, "cannot modify missing but "
468 "referenced row "UUID_FMT" in table %s",
469 UUID_ARGS(uuid), table->class->name);
470 ovsdb_idl_insert_row(row, new);
473 VLOG_WARN_RL(&semantic_rl, "cannot modify missing row "UUID_FMT" "
474 "in table %s", UUID_ARGS(uuid), table->class->name);
475 ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
481 ovsdb_idl_row_update(struct ovsdb_idl_row *row, const struct json *row_json)
483 struct ovsdb_idl_table *table = row->table;
484 struct shash_node *node;
486 SHASH_FOR_EACH (node, json_object(row_json)) {
487 const char *column_name = node->name;
488 const struct ovsdb_idl_column *column;
489 struct ovsdb_datum datum;
490 struct ovsdb_error *error;
492 column = shash_find_data(&table->columns, column_name);
494 VLOG_WARN_RL(&syntax_rl, "unknown column %s updating row "UUID_FMT,
495 column_name, UUID_ARGS(&row->uuid));
499 error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
501 ovsdb_datum_swap(&row->old[column - table->class->columns],
503 ovsdb_datum_destroy(&datum, &column->type);
505 char *s = ovsdb_error_to_string(error);
506 VLOG_WARN_RL(&syntax_rl, "error parsing column %s in row "UUID_FMT
507 " in table %s: %s", column_name,
508 UUID_ARGS(&row->uuid), table->class->name, s);
510 ovsdb_error_destroy(error);
516 ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *row)
522 ovsdb_idl_row_clear_old(struct ovsdb_idl_row *row)
524 assert(row->old == row->new);
525 if (!ovsdb_idl_row_is_orphan(row)) {
526 const struct ovsdb_idl_table_class *class = row->table->class;
529 for (i = 0; i < class->n_columns; i++) {
530 ovsdb_datum_destroy(&row->old[i], &class->columns[i].type);
533 row->old = row->new = NULL;
538 ovsdb_idl_row_clear_new(struct ovsdb_idl_row *row)
540 if (row->old != row->new) {
542 const struct ovsdb_idl_table_class *class = row->table->class;
545 BITMAP_FOR_EACH_1 (i, class->n_columns, row->written) {
546 ovsdb_datum_destroy(&row->new[i], &class->columns[i].type);
557 ovsdb_idl_row_clear_arcs(struct ovsdb_idl_row *row, bool destroy_dsts)
559 struct ovsdb_idl_arc *arc, *next;
561 /* Delete all forward arcs. If 'destroy_dsts', destroy any orphaned rows
562 * that this causes to be unreferenced. */
563 LIST_FOR_EACH_SAFE (arc, next, struct ovsdb_idl_arc, src_node,
565 list_remove(&arc->dst_node);
567 && ovsdb_idl_row_is_orphan(arc->dst)
568 && list_is_empty(&arc->dst->dst_arcs)) {
569 ovsdb_idl_row_destroy(arc->dst);
573 list_init(&row->src_arcs);
576 /* Force nodes that reference 'row' to reparse. */
578 ovsdb_idl_row_reparse_backrefs(struct ovsdb_idl_row *row, bool destroy_dsts)
580 struct ovsdb_idl_arc *arc, *next;
582 /* This is trickier than it looks. ovsdb_idl_row_clear_arcs() will destroy
583 * 'arc', so we need to use the "safe" variant of list traversal. However,
584 * calling ref->table->class->parse will add an arc equivalent to 'arc' to
585 * row->arcs. That could be a problem for traversal, but it adds it at the
586 * beginning of the list to prevent us from stumbling upon it again.
588 * (If duplicate arcs were possible then we would need to make sure that
589 * 'next' didn't also point into 'arc''s destination, but we forbid
590 * duplicate arcs.) */
591 LIST_FOR_EACH_SAFE (arc, next, struct ovsdb_idl_arc, dst_node,
593 struct ovsdb_idl_row *ref = arc->src;
595 (ref->table->class->unparse)(ref);
596 ovsdb_idl_row_clear_arcs(ref, destroy_dsts);
597 (ref->table->class->parse)(ref);
601 static struct ovsdb_idl_row *
602 ovsdb_idl_row_create__(const struct ovsdb_idl_table_class *class)
604 struct ovsdb_idl_row *row = xzalloc(class->allocation_size);
605 memset(row, 0, sizeof *row);
606 list_init(&row->src_arcs);
607 list_init(&row->dst_arcs);
608 hmap_node_nullify(&row->txn_node);
612 static struct ovsdb_idl_row *
613 ovsdb_idl_row_create(struct ovsdb_idl_table *table, const struct uuid *uuid)
615 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(table->class);
616 hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
623 ovsdb_idl_row_destroy(struct ovsdb_idl_row *row)
626 ovsdb_idl_row_clear_old(row);
627 hmap_remove(&row->table->rows, &row->hmap_node);
633 ovsdb_idl_insert_row(struct ovsdb_idl_row *row, const struct json *row_json)
635 const struct ovsdb_idl_table_class *class = row->table->class;
638 assert(!row->old && !row->new);
639 row->old = row->new = xmalloc(class->n_columns * sizeof *row->old);
640 for (i = 0; i < class->n_columns; i++) {
641 ovsdb_datum_init_default(&row->old[i], &class->columns[i].type);
643 ovsdb_idl_row_update(row, row_json);
646 ovsdb_idl_row_reparse_backrefs(row, false);
650 ovsdb_idl_delete_row(struct ovsdb_idl_row *row)
652 (row->table->class->unparse)(row);
653 ovsdb_idl_row_clear_arcs(row, true);
654 ovsdb_idl_row_clear_old(row);
655 if (list_is_empty(&row->dst_arcs)) {
656 ovsdb_idl_row_destroy(row);
658 ovsdb_idl_row_reparse_backrefs(row, true);
663 ovsdb_idl_modify_row(struct ovsdb_idl_row *row, const struct json *row_json)
665 (row->table->class->unparse)(row);
666 ovsdb_idl_row_clear_arcs(row, true);
667 ovsdb_idl_row_update(row, row_json);
668 (row->table->class->parse)(row);
672 may_add_arc(const struct ovsdb_idl_row *src, const struct ovsdb_idl_row *dst)
674 const struct ovsdb_idl_arc *arc;
681 /* No duplicate arcs.
683 * We only need to test whether the first arc in dst->dst_arcs originates
684 * at 'src', since we add all of the arcs from a given source in a clump
685 * (in a single call to a row's ->parse function) and new arcs are always
686 * added at the front of the dst_arcs list. */
687 if (list_is_empty(&dst->dst_arcs)) {
690 arc = CONTAINER_OF(dst->dst_arcs.next, struct ovsdb_idl_arc, dst_node);
691 return arc->src != src;
694 static struct ovsdb_idl_table *
695 ovsdb_idl_table_from_class(const struct ovsdb_idl *idl,
696 const struct ovsdb_idl_table_class *table_class)
698 return &idl->tables[table_class - idl->class->tables];
701 struct ovsdb_idl_row *
702 ovsdb_idl_get_row_arc(struct ovsdb_idl_row *src,
703 struct ovsdb_idl_table_class *dst_table_class,
704 const struct uuid *dst_uuid)
706 struct ovsdb_idl *idl = src->table->idl;
707 struct ovsdb_idl_table *dst_table;
708 struct ovsdb_idl_arc *arc;
709 struct ovsdb_idl_row *dst;
711 dst_table = ovsdb_idl_table_from_class(idl, dst_table_class);
712 dst = ovsdb_idl_get_row(dst_table, dst_uuid);
714 dst = ovsdb_idl_row_create(dst_table, dst_uuid);
717 /* Add a new arc, if it wouldn't be a self-arc or a duplicate arc. */
718 if (may_add_arc(src, dst)) {
719 /* The arc *must* be added at the front of the dst_arcs list. See
720 * ovsdb_idl_row_reparse_backrefs() for details. */
721 arc = xmalloc(sizeof *arc);
722 list_push_front(&src->src_arcs, &arc->src_node);
723 list_push_front(&dst->dst_arcs, &arc->dst_node);
728 return !ovsdb_idl_row_is_orphan(dst) ? dst : NULL;
731 static struct ovsdb_idl_row *
732 next_real_row(struct ovsdb_idl_table *table, struct hmap_node *node)
734 for (; node; node = hmap_next(&table->rows, node)) {
735 struct ovsdb_idl_row *row;
737 row = CONTAINER_OF(node, struct ovsdb_idl_row, hmap_node);
738 if (!ovsdb_idl_row_is_orphan(row)) {
745 struct ovsdb_idl_row *
746 ovsdb_idl_first_row(const struct ovsdb_idl *idl,
747 const struct ovsdb_idl_table_class *table_class)
749 struct ovsdb_idl_table *table
750 = ovsdb_idl_table_from_class(idl, table_class);
751 return next_real_row(table, hmap_first(&table->rows));
754 struct ovsdb_idl_row *
755 ovsdb_idl_next_row(const struct ovsdb_idl_row *row)
757 struct ovsdb_idl_table *table = row->table;
759 return next_real_row(table, hmap_next(&table->rows, &row->hmap_node));
764 static void ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
765 enum ovsdb_idl_txn_status);
768 ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status status)
785 struct ovsdb_idl_txn *
786 ovsdb_idl_txn_create(struct ovsdb_idl *idl)
788 struct ovsdb_idl_txn *txn;
791 idl->txn = txn = xmalloc(sizeof *txn);
793 txn->status = TXN_INCOMPLETE;
794 hmap_init(&txn->txn_rows);
795 txn->dry_run = false;
800 ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *txn)
806 ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *txn)
808 if (txn->status == TXN_INCOMPLETE) {
809 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
811 ovsdb_idl_txn_abort(txn);
816 ovsdb_idl_txn_wait(const struct ovsdb_idl_txn *txn)
818 if (txn->status != TXN_INCOMPLETE) {
819 poll_immediate_wake();
824 where_uuid_equals(const struct uuid *uuid)
829 json_string_create("_uuid"),
830 json_string_create("=="),
832 json_string_create("uuid"),
833 json_string_create_nocopy(
834 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
838 uuid_name_from_uuid(const struct uuid *uuid)
843 name = xasprintf("row"UUID_FMT, UUID_ARGS(uuid));
844 for (p = name; *p != '\0'; p++) {
853 static const struct ovsdb_idl_row *
854 ovsdb_idl_txn_get_row(const struct ovsdb_idl_txn *txn, const struct uuid *uuid)
856 const struct ovsdb_idl_row *row;
858 HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_idl_row, txn_node,
859 uuid_hash(uuid), &txn->txn_rows) {
860 if (uuid_equals(&row->uuid, uuid)) {
867 /* XXX there must be a cleaner way to do this */
869 substitute_uuids(struct json *json, const struct ovsdb_idl_txn *txn)
871 if (json->type == JSON_ARRAY) {
875 if (json->u.array.n == 2
876 && json->u.array.elems[0]->type == JSON_STRING
877 && json->u.array.elems[1]->type == JSON_STRING
878 && !strcmp(json->u.array.elems[0]->u.string, "uuid")
879 && uuid_from_string(&uuid, json->u.array.elems[1]->u.string)) {
880 const struct ovsdb_idl_row *row;
882 row = ovsdb_idl_txn_get_row(txn, &uuid);
883 if (row && !row->old && row->new) {
886 return json_array_create_2(
887 json_string_create("named-uuid"),
888 json_string_create_nocopy(uuid_name_from_uuid(&uuid)));
892 for (i = 0; i < json->u.array.n; i++) {
893 json->u.array.elems[i] = substitute_uuids(json->u.array.elems[i],
896 } else if (json->type == JSON_OBJECT) {
897 struct shash_node *node;
899 SHASH_FOR_EACH (node, json_object(json)) {
900 node->data = substitute_uuids(node->data, txn);
907 ovsdb_idl_txn_disassemble(struct ovsdb_idl_txn *txn)
909 struct ovsdb_idl_row *row, *next;
911 HMAP_FOR_EACH_SAFE (row, next, struct ovsdb_idl_row, txn_node,
913 if (row->old && row->written) {
914 (row->table->class->unparse)(row);
915 ovsdb_idl_row_clear_arcs(row, false);
916 (row->table->class->parse)(row);
918 ovsdb_idl_row_clear_new(row);
926 hmap_remove(&txn->txn_rows, &row->txn_node);
927 hmap_node_nullify(&row->txn_node);
929 hmap_destroy(&txn->txn_rows);
930 hmap_init(&txn->txn_rows);
933 enum ovsdb_idl_txn_status
934 ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
936 struct ovsdb_idl_row *row;
937 struct json *operations;
940 if (txn != txn->idl->txn) {
944 operations = json_array_create_empty();
946 /* Add prerequisites and declarations of new rows. */
947 HMAP_FOR_EACH (row, struct ovsdb_idl_row, txn_node, &txn->txn_rows) {
948 /* XXX check that deleted rows exist even if no prereqs? */
950 const struct ovsdb_idl_table_class *class = row->table->class;
951 size_t n_columns = class->n_columns;
952 struct json *op, *columns, *row_json;
955 op = json_object_create();
956 json_array_add(operations, op);
957 json_object_put_string(op, "op", "wait");
958 json_object_put_string(op, "table", class->name);
959 json_object_put(op, "timeout", json_integer_create(0));
960 json_object_put(op, "where", where_uuid_equals(&row->uuid));
961 json_object_put_string(op, "until", "==");
962 columns = json_array_create_empty();
963 json_object_put(op, "columns", columns);
964 row_json = json_object_create();
965 json_object_put(op, "rows", json_array_create_1(row_json));
967 BITMAP_FOR_EACH_1 (idx, n_columns, row->prereqs) {
968 const struct ovsdb_idl_column *column = &class->columns[idx];
969 json_array_add(columns, json_string_create(column->name));
970 json_object_put(row_json, column->name,
971 ovsdb_datum_to_json(&row->old[idx],
975 if (row->new && !row->old) {
978 op = json_object_create();
979 json_array_add(operations, op);
980 json_object_put_string(op, "op", "declare");
981 json_object_put(op, "uuid-name",
982 json_string_create_nocopy(
983 uuid_name_from_uuid(&row->uuid)));
989 HMAP_FOR_EACH (row, struct ovsdb_idl_row, txn_node, &txn->txn_rows) {
990 const struct ovsdb_idl_table_class *class = row->table->class;
992 if (row->old == row->new) {
994 } else if (!row->new) {
995 struct json *op = json_object_create();
996 json_object_put_string(op, "op", "delete");
997 json_object_put_string(op, "table", class->name);
998 json_object_put(op, "where", where_uuid_equals(&row->uuid));
999 json_array_add(operations, op);
1002 struct json *row_json;
1006 op = json_object_create();
1007 json_object_put_string(op, "op", row->old ? "update" : "insert");
1008 json_object_put_string(op, "table", class->name);
1010 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1012 json_object_put(op, "uuid-name",
1013 json_string_create_nocopy(
1014 uuid_name_from_uuid(&row->uuid)));
1016 row_json = json_object_create();
1017 json_object_put(op, "row", row_json);
1019 BITMAP_FOR_EACH_1 (idx, class->n_columns, row->written) {
1020 const struct ovsdb_idl_column *column = &class->columns[idx];
1022 if (!row->old || !ovsdb_datum_equals(&row->old[idx],
1025 json_object_put(row_json, column->name,
1027 ovsdb_datum_to_json(&row->new[idx],
1033 if (!row->old || !shash_is_empty(json_object(row_json))) {
1034 json_array_add(operations, op);
1043 struct json *op = json_object_create();
1044 json_object_put_string(op, "op", "abort");
1045 json_array_add(operations, op);
1049 txn->status = TXN_SUCCESS;
1050 } else if (!jsonrpc_session_send(
1052 jsonrpc_create_request(
1053 "transact", operations, &txn->request_id))) {
1054 hmap_insert(&txn->idl->outstanding_txns, &txn->hmap_node,
1055 json_hash(txn->request_id, 0));
1057 txn->status = TXN_INCOMPLETE;
1060 txn->idl->txn = NULL;
1061 ovsdb_idl_txn_disassemble(txn);
1066 ovsdb_idl_txn_abort(struct ovsdb_idl_txn *txn)
1068 ovsdb_idl_txn_disassemble(txn);
1069 if (txn->status == TXN_INCOMPLETE) {
1070 txn->status = TXN_ABORTED;
1075 ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
1076 enum ovsdb_idl_txn_status status)
1078 txn->status = status;
1079 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
1083 ovsdb_idl_txn_write(struct ovsdb_idl_row *row,
1084 const struct ovsdb_idl_column *column,
1085 struct ovsdb_datum *datum)
1087 const struct ovsdb_idl_table_class *class = row->table->class;
1088 size_t column_idx = column - class->columns;
1091 if (hmap_node_is_null(&row->txn_node)) {
1092 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1093 uuid_hash(&row->uuid));
1095 if (row->old == row->new) {
1096 row->new = xmalloc(class->n_columns * sizeof *row->new);
1098 if (!row->written) {
1099 row->written = bitmap_allocate(class->n_columns);
1101 if (bitmap_is_set(row->written, column_idx)) {
1102 ovsdb_datum_destroy(&row->new[column_idx], &column->type);
1104 bitmap_set1(row->written, column_idx);
1106 row->new[column_idx] = *datum;
1110 ovsdb_idl_txn_verify(const struct ovsdb_idl_row *row_,
1111 const struct ovsdb_idl_column *column)
1113 struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1114 const struct ovsdb_idl_table_class *class = row->table->class;
1115 size_t column_idx = column - class->columns;
1119 || (row->written && bitmap_is_set(row->written, column_idx))) {
1123 if (hmap_node_is_null(&row->txn_node)) {
1124 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1125 uuid_hash(&row->uuid));
1127 if (!row->prereqs) {
1128 row->prereqs = bitmap_allocate(class->n_columns);
1130 bitmap_set1(row->prereqs, column_idx);
1134 ovsdb_idl_txn_delete(struct ovsdb_idl_row *row)
1138 ovsdb_idl_row_clear_new(row);
1139 assert(!row->prereqs);
1140 hmap_remove(&row->table->idl->txn->txn_rows, &row->txn_node);
1143 if (hmap_node_is_null(&row->txn_node)) {
1144 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1145 uuid_hash(&row->uuid));
1147 ovsdb_idl_row_clear_new(row);
1151 struct ovsdb_idl_row *
1152 ovsdb_idl_txn_insert(struct ovsdb_idl_txn *txn,
1153 const struct ovsdb_idl_table_class *class)
1155 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(class);
1156 uuid_generate(&row->uuid);
1157 row->table = ovsdb_idl_table_from_class(txn->idl, class);
1158 row->new = xmalloc(class->n_columns * sizeof *row->new);
1159 row->written = bitmap_allocate(class->n_columns);
1160 hmap_insert(&txn->txn_rows, &row->txn_node, uuid_hash(&row->uuid));
1165 ovsdb_idl_txn_abort_all(struct ovsdb_idl *idl)
1167 struct ovsdb_idl_txn *txn;
1169 HMAP_FOR_EACH (txn, struct ovsdb_idl_txn, hmap_node,
1170 &idl->outstanding_txns) {
1171 ovsdb_idl_txn_complete(txn, TXN_TRY_AGAIN);
1175 static struct ovsdb_idl_txn *
1176 ovsdb_idl_txn_find(struct ovsdb_idl *idl, const struct json *id)
1178 struct ovsdb_idl_txn *txn;
1180 HMAP_FOR_EACH_WITH_HASH (txn, struct ovsdb_idl_txn, hmap_node,
1181 json_hash(id, 0), &idl->outstanding_txns) {
1182 if (json_equal(id, txn->request_id)) {
1190 ovsdb_idl_txn_process_reply(struct ovsdb_idl *idl,
1191 const struct jsonrpc_msg *msg)
1193 struct ovsdb_idl_txn *txn;
1194 enum ovsdb_idl_txn_status status;
1196 txn = ovsdb_idl_txn_find(idl, msg->id);
1201 if (msg->type == JSONRPC_ERROR) {
1203 } else if (msg->result->type != JSON_ARRAY) {
1204 VLOG_WARN_RL(&syntax_rl, "reply to \"transact\" is not JSON array");
1207 int hard_errors = 0;
1208 int soft_errors = 0;
1211 for (i = 0; i < msg->result->u.array.n; i++) {
1212 struct json *json = msg->result->u.array.elems[i];
1214 if (json->type == JSON_NULL) {
1215 /* This isn't an error in itself but indicates that some prior
1216 * operation failed, so make sure that we know about it. */
1218 } else if (json->type == JSON_OBJECT) {
1221 error = shash_find_data(json_object(json), "error");
1223 if (error->type == JSON_STRING) {
1224 if (!strcmp(error->u.string, "timed out")) {
1226 } else if (strcmp(error->u.string, "aborted")) {
1231 VLOG_WARN_RL(&syntax_rl,
1232 "\"error\" in reply is not JSON string");
1237 VLOG_WARN_RL(&syntax_rl,
1238 "operation reply is not JSON null or object");
1242 status = (hard_errors ? TXN_ERROR
1243 : soft_errors ? TXN_TRY_AGAIN
1247 ovsdb_idl_txn_complete(txn, status);
1251 struct ovsdb_idl_txn *
1252 ovsdb_idl_txn_get(const struct ovsdb_idl_row *row)
1254 struct ovsdb_idl_txn *txn = row->table->idl->txn;
1255 assert(txn != NULL);