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 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_has_ever_connected(const struct ovsdb_idl *idl)
297 return ovsdb_idl_get_seqno(idl) != 0;
301 ovsdb_idl_force_reconnect(struct ovsdb_idl *idl)
303 jsonrpc_session_force_reconnect(idl->session);
307 ovsdb_idl_send_monitor_request(struct ovsdb_idl *idl)
309 struct json *monitor_requests;
310 struct jsonrpc_msg *msg;
313 monitor_requests = json_object_create();
314 for (i = 0; i < idl->class->n_tables; i++) {
315 const struct ovsdb_idl_table *table = &idl->tables[i];
316 const struct ovsdb_idl_table_class *tc = table->class;
317 struct json *monitor_request, *columns;
320 monitor_request = json_object_create();
321 columns = json_array_create_empty();
322 for (i = 0; i < tc->n_columns; i++) {
323 const struct ovsdb_idl_column *column = &tc->columns[i];
324 json_array_add(columns, json_string_create(column->name));
326 json_object_put(monitor_request, "columns", columns);
327 json_object_put(monitor_requests, tc->name, monitor_request);
330 json_destroy(idl->monitor_request_id);
331 msg = jsonrpc_create_request(
332 "monitor", json_array_create_2(json_null_create(), monitor_requests),
333 &idl->monitor_request_id);
334 jsonrpc_session_send(idl->session, msg);
338 ovsdb_idl_parse_update(struct ovsdb_idl *idl, const struct json *table_updates)
340 struct ovsdb_error *error;
344 error = ovsdb_idl_parse_update__(idl, table_updates);
346 if (!VLOG_DROP_WARN(&syntax_rl)) {
347 char *s = ovsdb_error_to_string(error);
348 VLOG_WARN_RL(&syntax_rl, "%s", s);
351 ovsdb_error_destroy(error);
355 static struct ovsdb_error *
356 ovsdb_idl_parse_update__(struct ovsdb_idl *idl,
357 const struct json *table_updates)
359 const struct shash_node *tables_node;
361 if (table_updates->type != JSON_OBJECT) {
362 return ovsdb_syntax_error(table_updates, NULL,
363 "<table-updates> is not an object");
365 SHASH_FOR_EACH (tables_node, json_object(table_updates)) {
366 const struct json *table_update = tables_node->data;
367 const struct shash_node *table_node;
368 struct ovsdb_idl_table *table;
370 table = shash_find_data(&idl->table_by_name, tables_node->name);
372 return ovsdb_syntax_error(
374 "<table-updates> includes unknown table \"%s\"",
378 if (table_update->type != JSON_OBJECT) {
379 return ovsdb_syntax_error(table_update, NULL,
380 "<table-update> for table \"%s\" is "
381 "not an object", table->class->name);
383 SHASH_FOR_EACH (table_node, json_object(table_update)) {
384 const struct json *row_update = table_node->data;
385 const struct json *old_json, *new_json;
388 if (!uuid_from_string(&uuid, table_node->name)) {
389 return ovsdb_syntax_error(table_update, NULL,
390 "<table-update> for table \"%s\" "
392 "\"%s\" as member name",
396 if (row_update->type != JSON_OBJECT) {
397 return ovsdb_syntax_error(row_update, NULL,
398 "<table-update> for table \"%s\" "
399 "contains <row-update> for %s that "
405 old_json = shash_find_data(json_object(row_update), "old");
406 new_json = shash_find_data(json_object(row_update), "new");
407 if (old_json && old_json->type != JSON_OBJECT) {
408 return ovsdb_syntax_error(old_json, NULL,
409 "\"old\" <row> is not object");
410 } else if (new_json && new_json->type != JSON_OBJECT) {
411 return ovsdb_syntax_error(new_json, NULL,
412 "\"new\" <row> is not object");
413 } else if ((old_json != NULL) + (new_json != NULL)
414 != shash_count(json_object(row_update))) {
415 return ovsdb_syntax_error(row_update, NULL,
416 "<row-update> contains unexpected "
418 } else if (!old_json && !new_json) {
419 return ovsdb_syntax_error(row_update, NULL,
420 "<row-update> missing \"old\" "
421 "and \"new\" members");
424 ovsdb_idl_process_update(table, &uuid, old_json, new_json);
431 static struct ovsdb_idl_row *
432 ovsdb_idl_get_row(struct ovsdb_idl_table *table, const struct uuid *uuid)
434 struct ovsdb_idl_row *row;
436 HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_idl_row, hmap_node,
437 uuid_hash(uuid), &table->rows) {
438 if (uuid_equals(&row->uuid, uuid)) {
446 ovsdb_idl_process_update(struct ovsdb_idl_table *table,
447 const struct uuid *uuid, const struct json *old,
448 const struct json *new)
450 struct ovsdb_idl_row *row;
452 row = ovsdb_idl_get_row(table, uuid);
455 if (row && !ovsdb_idl_row_is_orphan(row)) {
456 /* XXX perhaps we should check the 'old' values? */
457 ovsdb_idl_delete_row(row);
459 VLOG_WARN_RL(&semantic_rl, "cannot delete missing row "UUID_FMT" "
461 UUID_ARGS(uuid), table->class->name);
466 ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
467 } else if (ovsdb_idl_row_is_orphan(row)) {
468 ovsdb_idl_insert_row(row, new);
470 VLOG_WARN_RL(&semantic_rl, "cannot add existing row "UUID_FMT" to "
471 "table %s", UUID_ARGS(uuid), table->class->name);
472 ovsdb_idl_modify_row(row, new);
477 /* XXX perhaps we should check the 'old' values? */
478 if (!ovsdb_idl_row_is_orphan(row)) {
479 ovsdb_idl_modify_row(row, new);
481 VLOG_WARN_RL(&semantic_rl, "cannot modify missing but "
482 "referenced row "UUID_FMT" in table %s",
483 UUID_ARGS(uuid), table->class->name);
484 ovsdb_idl_insert_row(row, new);
487 VLOG_WARN_RL(&semantic_rl, "cannot modify missing row "UUID_FMT" "
488 "in table %s", UUID_ARGS(uuid), table->class->name);
489 ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
495 ovsdb_idl_row_update(struct ovsdb_idl_row *row, const struct json *row_json)
497 struct ovsdb_idl_table *table = row->table;
498 struct shash_node *node;
500 SHASH_FOR_EACH (node, json_object(row_json)) {
501 const char *column_name = node->name;
502 const struct ovsdb_idl_column *column;
503 struct ovsdb_datum datum;
504 struct ovsdb_error *error;
506 column = shash_find_data(&table->columns, column_name);
508 VLOG_WARN_RL(&syntax_rl, "unknown column %s updating row "UUID_FMT,
509 column_name, UUID_ARGS(&row->uuid));
513 error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
515 ovsdb_datum_swap(&row->old[column - table->class->columns],
517 ovsdb_datum_destroy(&datum, &column->type);
519 char *s = ovsdb_error_to_string(error);
520 VLOG_WARN_RL(&syntax_rl, "error parsing column %s in row "UUID_FMT
521 " in table %s: %s", column_name,
522 UUID_ARGS(&row->uuid), table->class->name, s);
524 ovsdb_error_destroy(error);
530 ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *row)
536 ovsdb_idl_row_clear_old(struct ovsdb_idl_row *row)
538 assert(row->old == row->new);
539 if (!ovsdb_idl_row_is_orphan(row)) {
540 const struct ovsdb_idl_table_class *class = row->table->class;
543 for (i = 0; i < class->n_columns; i++) {
544 ovsdb_datum_destroy(&row->old[i], &class->columns[i].type);
547 row->old = row->new = NULL;
552 ovsdb_idl_row_clear_new(struct ovsdb_idl_row *row)
554 if (row->old != row->new) {
556 const struct ovsdb_idl_table_class *class = row->table->class;
559 BITMAP_FOR_EACH_1 (i, class->n_columns, row->written) {
560 ovsdb_datum_destroy(&row->new[i], &class->columns[i].type);
571 ovsdb_idl_row_clear_arcs(struct ovsdb_idl_row *row, bool destroy_dsts)
573 struct ovsdb_idl_arc *arc, *next;
575 /* Delete all forward arcs. If 'destroy_dsts', destroy any orphaned rows
576 * that this causes to be unreferenced. */
577 LIST_FOR_EACH_SAFE (arc, next, struct ovsdb_idl_arc, src_node,
579 list_remove(&arc->dst_node);
581 && ovsdb_idl_row_is_orphan(arc->dst)
582 && list_is_empty(&arc->dst->dst_arcs)) {
583 ovsdb_idl_row_destroy(arc->dst);
587 list_init(&row->src_arcs);
590 /* Force nodes that reference 'row' to reparse. */
592 ovsdb_idl_row_reparse_backrefs(struct ovsdb_idl_row *row, bool destroy_dsts)
594 struct ovsdb_idl_arc *arc, *next;
596 /* This is trickier than it looks. ovsdb_idl_row_clear_arcs() will destroy
597 * 'arc', so we need to use the "safe" variant of list traversal. However,
598 * calling ref->table->class->parse will add an arc equivalent to 'arc' to
599 * row->arcs. That could be a problem for traversal, but it adds it at the
600 * beginning of the list to prevent us from stumbling upon it again.
602 * (If duplicate arcs were possible then we would need to make sure that
603 * 'next' didn't also point into 'arc''s destination, but we forbid
604 * duplicate arcs.) */
605 LIST_FOR_EACH_SAFE (arc, next, struct ovsdb_idl_arc, dst_node,
607 struct ovsdb_idl_row *ref = arc->src;
609 (ref->table->class->unparse)(ref);
610 ovsdb_idl_row_clear_arcs(ref, destroy_dsts);
611 (ref->table->class->parse)(ref);
615 static struct ovsdb_idl_row *
616 ovsdb_idl_row_create__(const struct ovsdb_idl_table_class *class)
618 struct ovsdb_idl_row *row = xzalloc(class->allocation_size);
619 memset(row, 0, sizeof *row);
620 list_init(&row->src_arcs);
621 list_init(&row->dst_arcs);
622 hmap_node_nullify(&row->txn_node);
626 static struct ovsdb_idl_row *
627 ovsdb_idl_row_create(struct ovsdb_idl_table *table, const struct uuid *uuid)
629 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(table->class);
630 hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
637 ovsdb_idl_row_destroy(struct ovsdb_idl_row *row)
640 ovsdb_idl_row_clear_old(row);
641 hmap_remove(&row->table->rows, &row->hmap_node);
647 ovsdb_idl_insert_row(struct ovsdb_idl_row *row, const struct json *row_json)
649 const struct ovsdb_idl_table_class *class = row->table->class;
652 assert(!row->old && !row->new);
653 row->old = row->new = xmalloc(class->n_columns * sizeof *row->old);
654 for (i = 0; i < class->n_columns; i++) {
655 ovsdb_datum_init_default(&row->old[i], &class->columns[i].type);
657 ovsdb_idl_row_update(row, row_json);
660 ovsdb_idl_row_reparse_backrefs(row, false);
664 ovsdb_idl_delete_row(struct ovsdb_idl_row *row)
666 (row->table->class->unparse)(row);
667 ovsdb_idl_row_clear_arcs(row, true);
668 ovsdb_idl_row_clear_old(row);
669 if (list_is_empty(&row->dst_arcs)) {
670 ovsdb_idl_row_destroy(row);
672 ovsdb_idl_row_reparse_backrefs(row, true);
677 ovsdb_idl_modify_row(struct ovsdb_idl_row *row, const struct json *row_json)
679 (row->table->class->unparse)(row);
680 ovsdb_idl_row_clear_arcs(row, true);
681 ovsdb_idl_row_update(row, row_json);
682 (row->table->class->parse)(row);
686 may_add_arc(const struct ovsdb_idl_row *src, const struct ovsdb_idl_row *dst)
688 const struct ovsdb_idl_arc *arc;
695 /* No duplicate arcs.
697 * We only need to test whether the first arc in dst->dst_arcs originates
698 * at 'src', since we add all of the arcs from a given source in a clump
699 * (in a single call to a row's ->parse function) and new arcs are always
700 * added at the front of the dst_arcs list. */
701 if (list_is_empty(&dst->dst_arcs)) {
704 arc = CONTAINER_OF(dst->dst_arcs.next, struct ovsdb_idl_arc, dst_node);
705 return arc->src != src;
708 static struct ovsdb_idl_table *
709 ovsdb_idl_table_from_class(const struct ovsdb_idl *idl,
710 const struct ovsdb_idl_table_class *table_class)
712 return &idl->tables[table_class - idl->class->tables];
715 struct ovsdb_idl_row *
716 ovsdb_idl_get_row_arc(struct ovsdb_idl_row *src,
717 struct ovsdb_idl_table_class *dst_table_class,
718 const struct uuid *dst_uuid)
720 struct ovsdb_idl *idl = src->table->idl;
721 struct ovsdb_idl_table *dst_table;
722 struct ovsdb_idl_arc *arc;
723 struct ovsdb_idl_row *dst;
725 dst_table = ovsdb_idl_table_from_class(idl, dst_table_class);
726 dst = ovsdb_idl_get_row(dst_table, dst_uuid);
728 dst = ovsdb_idl_row_create(dst_table, dst_uuid);
731 /* Add a new arc, if it wouldn't be a self-arc or a duplicate arc. */
732 if (may_add_arc(src, dst)) {
733 /* The arc *must* be added at the front of the dst_arcs list. See
734 * ovsdb_idl_row_reparse_backrefs() for details. */
735 arc = xmalloc(sizeof *arc);
736 list_push_front(&src->src_arcs, &arc->src_node);
737 list_push_front(&dst->dst_arcs, &arc->dst_node);
742 return !ovsdb_idl_row_is_orphan(dst) ? dst : NULL;
745 static struct ovsdb_idl_row *
746 next_real_row(struct ovsdb_idl_table *table, struct hmap_node *node)
748 for (; node; node = hmap_next(&table->rows, node)) {
749 struct ovsdb_idl_row *row;
751 row = CONTAINER_OF(node, struct ovsdb_idl_row, hmap_node);
752 if (!ovsdb_idl_row_is_orphan(row)) {
759 struct ovsdb_idl_row *
760 ovsdb_idl_first_row(const struct ovsdb_idl *idl,
761 const struct ovsdb_idl_table_class *table_class)
763 struct ovsdb_idl_table *table
764 = ovsdb_idl_table_from_class(idl, table_class);
765 return next_real_row(table, hmap_first(&table->rows));
768 struct ovsdb_idl_row *
769 ovsdb_idl_next_row(const struct ovsdb_idl_row *row)
771 struct ovsdb_idl_table *table = row->table;
773 return next_real_row(table, hmap_next(&table->rows, &row->hmap_node));
778 static void ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
779 enum ovsdb_idl_txn_status);
782 ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status status)
801 struct ovsdb_idl_txn *
802 ovsdb_idl_txn_create(struct ovsdb_idl *idl)
804 struct ovsdb_idl_txn *txn;
807 idl->txn = txn = xmalloc(sizeof *txn);
809 txn->status = TXN_INCOMPLETE;
810 hmap_init(&txn->txn_rows);
811 txn->dry_run = false;
812 ds_init(&txn->comment);
813 txn->inc_table = NULL;
814 txn->inc_column = NULL;
815 txn->inc_where = NULL;
820 ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *txn, const char *s)
822 if (txn->comment.length) {
823 ds_put_char(&txn->comment, '\n');
825 ds_put_cstr(&txn->comment, s);
829 ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *txn)
835 ovsdb_idl_txn_increment(struct ovsdb_idl_txn *txn, const char *table,
836 const char *column, const struct json *where)
838 assert(!txn->inc_table);
839 txn->inc_table = xstrdup(table);
840 txn->inc_column = xstrdup(column);
841 txn->inc_where = where ? json_clone(where) : json_array_create_empty();
845 ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *txn)
847 if (txn->status == TXN_INCOMPLETE) {
848 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
850 ovsdb_idl_txn_abort(txn);
851 ds_destroy(&txn->comment);
852 free(txn->inc_table);
853 free(txn->inc_column);
854 json_destroy(txn->inc_where);
859 ovsdb_idl_txn_wait(const struct ovsdb_idl_txn *txn)
861 if (txn->status != TXN_INCOMPLETE) {
862 poll_immediate_wake();
867 where_uuid_equals(const struct uuid *uuid)
872 json_string_create("_uuid"),
873 json_string_create("=="),
875 json_string_create("uuid"),
876 json_string_create_nocopy(
877 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
881 uuid_name_from_uuid(const struct uuid *uuid)
886 name = xasprintf("row"UUID_FMT, UUID_ARGS(uuid));
887 for (p = name; *p != '\0'; p++) {
896 static const struct ovsdb_idl_row *
897 ovsdb_idl_txn_get_row(const struct ovsdb_idl_txn *txn, const struct uuid *uuid)
899 const struct ovsdb_idl_row *row;
901 HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_idl_row, txn_node,
902 uuid_hash(uuid), &txn->txn_rows) {
903 if (uuid_equals(&row->uuid, uuid)) {
910 /* XXX there must be a cleaner way to do this */
912 substitute_uuids(struct json *json, const struct ovsdb_idl_txn *txn)
914 if (json->type == JSON_ARRAY) {
918 if (json->u.array.n == 2
919 && json->u.array.elems[0]->type == JSON_STRING
920 && json->u.array.elems[1]->type == JSON_STRING
921 && !strcmp(json->u.array.elems[0]->u.string, "uuid")
922 && uuid_from_string(&uuid, json->u.array.elems[1]->u.string)) {
923 const struct ovsdb_idl_row *row;
925 row = ovsdb_idl_txn_get_row(txn, &uuid);
926 if (row && !row->old && row->new) {
929 return json_array_create_2(
930 json_string_create("named-uuid"),
931 json_string_create_nocopy(uuid_name_from_uuid(&uuid)));
935 for (i = 0; i < json->u.array.n; i++) {
936 json->u.array.elems[i] = substitute_uuids(json->u.array.elems[i],
939 } else if (json->type == JSON_OBJECT) {
940 struct shash_node *node;
942 SHASH_FOR_EACH (node, json_object(json)) {
943 node->data = substitute_uuids(node->data, txn);
950 ovsdb_idl_txn_disassemble(struct ovsdb_idl_txn *txn)
952 struct ovsdb_idl_row *row, *next;
954 HMAP_FOR_EACH_SAFE (row, next, struct ovsdb_idl_row, txn_node,
956 if (row->old && row->written) {
957 (row->table->class->unparse)(row);
958 ovsdb_idl_row_clear_arcs(row, false);
959 (row->table->class->parse)(row);
961 ovsdb_idl_row_clear_new(row);
969 hmap_remove(&txn->txn_rows, &row->txn_node);
970 hmap_node_nullify(&row->txn_node);
972 hmap_destroy(&txn->txn_rows);
973 hmap_init(&txn->txn_rows);
976 enum ovsdb_idl_txn_status
977 ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
979 struct ovsdb_idl_row *row;
980 struct json *operations;
983 if (txn != txn->idl->txn) {
987 operations = json_array_create_empty();
989 /* Add prerequisites and declarations of new rows. */
990 HMAP_FOR_EACH (row, struct ovsdb_idl_row, txn_node, &txn->txn_rows) {
991 /* XXX check that deleted rows exist even if no prereqs? */
993 const struct ovsdb_idl_table_class *class = row->table->class;
994 size_t n_columns = class->n_columns;
995 struct json *op, *columns, *row_json;
998 op = json_object_create();
999 json_array_add(operations, op);
1000 json_object_put_string(op, "op", "wait");
1001 json_object_put_string(op, "table", class->name);
1002 json_object_put(op, "timeout", json_integer_create(0));
1003 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1004 json_object_put_string(op, "until", "==");
1005 columns = json_array_create_empty();
1006 json_object_put(op, "columns", columns);
1007 row_json = json_object_create();
1008 json_object_put(op, "rows", json_array_create_1(row_json));
1010 BITMAP_FOR_EACH_1 (idx, n_columns, row->prereqs) {
1011 const struct ovsdb_idl_column *column = &class->columns[idx];
1012 json_array_add(columns, json_string_create(column->name));
1013 json_object_put(row_json, column->name,
1014 ovsdb_datum_to_json(&row->old[idx],
1018 if (row->new && !row->old) {
1021 op = json_object_create();
1022 json_array_add(operations, op);
1023 json_object_put_string(op, "op", "declare");
1024 json_object_put(op, "uuid-name",
1025 json_string_create_nocopy(
1026 uuid_name_from_uuid(&row->uuid)));
1031 any_updates = false;
1032 HMAP_FOR_EACH (row, struct ovsdb_idl_row, txn_node, &txn->txn_rows) {
1033 const struct ovsdb_idl_table_class *class = row->table->class;
1035 if (row->old == row->new) {
1037 } else if (!row->new) {
1038 struct json *op = json_object_create();
1039 json_object_put_string(op, "op", "delete");
1040 json_object_put_string(op, "table", class->name);
1041 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1042 json_array_add(operations, op);
1045 struct json *row_json;
1049 op = json_object_create();
1050 json_object_put_string(op, "op", row->old ? "update" : "insert");
1051 json_object_put_string(op, "table", class->name);
1053 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1055 json_object_put(op, "uuid-name",
1056 json_string_create_nocopy(
1057 uuid_name_from_uuid(&row->uuid)));
1059 row_json = json_object_create();
1060 json_object_put(op, "row", row_json);
1062 BITMAP_FOR_EACH_1 (idx, class->n_columns, row->written) {
1063 const struct ovsdb_idl_column *column = &class->columns[idx];
1065 if (!row->old || !ovsdb_datum_equals(&row->old[idx],
1068 json_object_put(row_json, column->name,
1070 ovsdb_datum_to_json(&row->new[idx],
1076 if (!row->old || !shash_is_empty(json_object(row_json))) {
1077 json_array_add(operations, op);
1085 /* Add increment. */
1086 if (txn->inc_table && any_updates) {
1089 txn->inc_index = operations->u.array.n;
1091 op = json_object_create();
1092 json_object_put_string(op, "op", "mutate");
1093 json_object_put_string(op, "table", txn->inc_table);
1094 json_object_put(op, "where",
1095 substitute_uuids(json_clone(txn->inc_where), txn));
1096 json_object_put(op, "mutations",
1097 json_array_create_1(
1098 json_array_create_3(
1099 json_string_create(txn->inc_column),
1100 json_string_create("+="),
1101 json_integer_create(1))));
1102 json_array_add(operations, op);
1104 op = json_object_create();
1105 json_object_put_string(op, "op", "select");
1106 json_object_put_string(op, "table", txn->inc_table);
1107 json_object_put(op, "where",
1108 substitute_uuids(json_clone(txn->inc_where), txn));
1109 json_object_put(op, "columns",
1110 json_array_create_1(json_string_create(
1112 json_array_add(operations, op);
1115 if (txn->comment.length) {
1116 struct json *op = json_object_create();
1117 json_object_put_string(op, "op", "comment");
1118 json_object_put_string(op, "comment", ds_cstr(&txn->comment));
1119 json_array_add(operations, op);
1123 struct json *op = json_object_create();
1124 json_object_put_string(op, "op", "abort");
1125 json_array_add(operations, op);
1129 txn->status = TXN_UNCHANGED;
1130 json_destroy(operations);
1131 } else if (!jsonrpc_session_send(
1133 jsonrpc_create_request(
1134 "transact", operations, &txn->request_id))) {
1135 hmap_insert(&txn->idl->outstanding_txns, &txn->hmap_node,
1136 json_hash(txn->request_id, 0));
1138 txn->status = TXN_INCOMPLETE;
1141 txn->idl->txn = NULL;
1142 ovsdb_idl_txn_disassemble(txn);
1147 ovsdb_idl_txn_get_increment_new_value(const struct ovsdb_idl_txn *txn)
1149 assert(txn->status == TXN_SUCCESS);
1150 return txn->inc_new_value;
1154 ovsdb_idl_txn_abort(struct ovsdb_idl_txn *txn)
1156 ovsdb_idl_txn_disassemble(txn);
1157 if (txn->status == TXN_INCOMPLETE) {
1158 txn->status = TXN_ABORTED;
1163 ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
1164 enum ovsdb_idl_txn_status status)
1166 txn->status = status;
1167 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
1171 ovsdb_idl_txn_write(struct ovsdb_idl_row *row,
1172 const struct ovsdb_idl_column *column,
1173 struct ovsdb_datum *datum)
1175 const struct ovsdb_idl_table_class *class = row->table->class;
1176 size_t column_idx = column - class->columns;
1179 if (hmap_node_is_null(&row->txn_node)) {
1180 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1181 uuid_hash(&row->uuid));
1183 if (row->old == row->new) {
1184 row->new = xmalloc(class->n_columns * sizeof *row->new);
1186 if (!row->written) {
1187 row->written = bitmap_allocate(class->n_columns);
1189 if (bitmap_is_set(row->written, column_idx)) {
1190 ovsdb_datum_destroy(&row->new[column_idx], &column->type);
1192 bitmap_set1(row->written, column_idx);
1194 row->new[column_idx] = *datum;
1198 ovsdb_idl_txn_verify(const struct ovsdb_idl_row *row_,
1199 const struct ovsdb_idl_column *column)
1201 struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1202 const struct ovsdb_idl_table_class *class = row->table->class;
1203 size_t column_idx = column - class->columns;
1207 || (row->written && bitmap_is_set(row->written, column_idx))) {
1211 if (hmap_node_is_null(&row->txn_node)) {
1212 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1213 uuid_hash(&row->uuid));
1215 if (!row->prereqs) {
1216 row->prereqs = bitmap_allocate(class->n_columns);
1218 bitmap_set1(row->prereqs, column_idx);
1222 ovsdb_idl_txn_delete(struct ovsdb_idl_row *row)
1226 ovsdb_idl_row_clear_new(row);
1227 assert(!row->prereqs);
1228 hmap_remove(&row->table->idl->txn->txn_rows, &row->txn_node);
1231 if (hmap_node_is_null(&row->txn_node)) {
1232 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1233 uuid_hash(&row->uuid));
1235 ovsdb_idl_row_clear_new(row);
1239 struct ovsdb_idl_row *
1240 ovsdb_idl_txn_insert(struct ovsdb_idl_txn *txn,
1241 const struct ovsdb_idl_table_class *class)
1243 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(class);
1244 uuid_generate(&row->uuid);
1245 row->table = ovsdb_idl_table_from_class(txn->idl, class);
1246 row->new = xmalloc(class->n_columns * sizeof *row->new);
1247 row->written = bitmap_allocate(class->n_columns);
1248 hmap_insert(&txn->txn_rows, &row->txn_node, uuid_hash(&row->uuid));
1253 ovsdb_idl_txn_abort_all(struct ovsdb_idl *idl)
1255 struct ovsdb_idl_txn *txn;
1257 HMAP_FOR_EACH (txn, struct ovsdb_idl_txn, hmap_node,
1258 &idl->outstanding_txns) {
1259 ovsdb_idl_txn_complete(txn, TXN_TRY_AGAIN);
1263 static struct ovsdb_idl_txn *
1264 ovsdb_idl_txn_find(struct ovsdb_idl *idl, const struct json *id)
1266 struct ovsdb_idl_txn *txn;
1268 HMAP_FOR_EACH_WITH_HASH (txn, struct ovsdb_idl_txn, hmap_node,
1269 json_hash(id, 0), &idl->outstanding_txns) {
1270 if (json_equal(id, txn->request_id)) {
1278 check_json_type(const struct json *json, enum json_type type, const char *name)
1281 VLOG_WARN_RL(&syntax_rl, "%s is missing", name);
1283 } else if (json->type != type) {
1284 VLOG_WARN_RL(&syntax_rl, "%s is %s instead of %s",
1285 name, json_type_to_string(json->type),
1286 json_type_to_string(type));
1294 ovsdb_idl_txn_process_inc_reply(struct ovsdb_idl_txn *txn,
1295 const struct json_array *results)
1297 struct json *count, *rows, *row, *column;
1298 struct shash *mutate, *select;
1300 if (txn->inc_index + 2 > results->n) {
1301 VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
1302 "for increment (has %u, needs %u)",
1303 results->n, txn->inc_index + 2);
1307 /* We know that this is a JSON objects because the loop in
1308 * ovsdb_idl_txn_process_reply() checked. */
1309 mutate = json_object(results->elems[txn->inc_index]);
1310 count = shash_find_data(mutate, "count");
1311 if (!check_json_type(count, JSON_INTEGER, "\"mutate\" reply \"count\"")) {
1314 if (count->u.integer != 1) {
1315 VLOG_WARN_RL(&syntax_rl,
1316 "\"mutate\" reply \"count\" is %"PRId64" instead of 1",
1321 select = json_object(results->elems[txn->inc_index + 1]);
1322 rows = shash_find_data(select, "rows");
1323 if (!check_json_type(rows, JSON_ARRAY, "\"select\" reply \"rows\"")) {
1326 if (rows->u.array.n != 1) {
1327 VLOG_WARN_RL(&syntax_rl, "\"select\" reply \"rows\" has %u elements "
1332 row = rows->u.array.elems[0];
1333 if (!check_json_type(row, JSON_OBJECT, "\"select\" reply row")) {
1336 column = shash_find_data(json_object(row), txn->inc_column);
1337 if (!check_json_type(column, JSON_INTEGER,
1338 "\"select\" reply inc column")) {
1341 txn->inc_new_value = column->u.integer;
1347 ovsdb_idl_txn_process_reply(struct ovsdb_idl *idl,
1348 const struct jsonrpc_msg *msg)
1350 struct ovsdb_idl_txn *txn;
1351 enum ovsdb_idl_txn_status status;
1353 txn = ovsdb_idl_txn_find(idl, msg->id);
1358 if (msg->type == JSONRPC_ERROR) {
1360 } else if (msg->result->type != JSON_ARRAY) {
1361 VLOG_WARN_RL(&syntax_rl, "reply to \"transact\" is not JSON array");
1364 int hard_errors = 0;
1365 int soft_errors = 0;
1368 for (i = 0; i < msg->result->u.array.n; i++) {
1369 struct json *json = msg->result->u.array.elems[i];
1371 if (json->type == JSON_NULL) {
1372 /* This isn't an error in itself but indicates that some prior
1373 * operation failed, so make sure that we know about it. */
1375 } else if (json->type == JSON_OBJECT) {
1378 error = shash_find_data(json_object(json), "error");
1380 if (error->type == JSON_STRING) {
1381 if (!strcmp(error->u.string, "timed out")) {
1383 } else if (strcmp(error->u.string, "aborted")) {
1388 VLOG_WARN_RL(&syntax_rl,
1389 "\"error\" in reply is not JSON string");
1394 VLOG_WARN_RL(&syntax_rl,
1395 "operation reply is not JSON null or object");
1402 && !ovsdb_idl_txn_process_inc_reply(txn,
1403 json_array(msg->result))) {
1407 status = (hard_errors ? TXN_ERROR
1408 : soft_errors ? TXN_TRY_AGAIN
1412 ovsdb_idl_txn_complete(txn, status);
1416 struct ovsdb_idl_txn *
1417 ovsdb_idl_txn_get(const struct ovsdb_idl_row *row)
1419 struct ovsdb_idl_txn *txn = row->table->idl->txn;
1420 assert(txn != NULL);