ovsdb-idl: Make rows inserted by transaction appear during table iteration.
[sliver-openvswitch.git] / lib / ovsdb-idl.c
1 /* Copyright (c) 2009, 2010 Nicira Networks.
2  *
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <config.h>
17
18 #include "ovsdb-idl.h"
19
20 #include <assert.h>
21 #include <errno.h>
22 #include <inttypes.h>
23 #include <limits.h>
24 #include <stdlib.h>
25
26 #include "bitmap.h"
27 #include "dynamic-string.h"
28 #include "json.h"
29 #include "jsonrpc.h"
30 #include "ovsdb-data.h"
31 #include "ovsdb-error.h"
32 #include "ovsdb-idl-provider.h"
33 #include "poll-loop.h"
34 #include "shash.h"
35 #include "util.h"
36
37 #define THIS_MODULE VLM_ovsdb_idl
38 #include "vlog.h"
39
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
42  * (the destination).
43  *
44  * Arcs from a row to itself are omitted, that is, src and dst are always
45  * different.
46  *
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.
49  *
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.
53  *
54  * The source and destination row may be in the same table or in different
55  * tables.
56  */
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. */
62 };
63
64 struct ovsdb_idl {
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;
72
73     /* Transaction support. */
74     struct ovsdb_idl_txn *txn;
75     struct hmap outstanding_txns;
76 };
77
78 struct ovsdb_idl_txn {
79     struct hmap_node hmap_node;
80     struct json *request_id;
81     struct ovsdb_idl *idl;
82     struct hmap txn_rows;
83     enum ovsdb_idl_txn_status status;
84     bool dry_run;
85     struct ds comment;
86
87     /* Increments. */
88     char *inc_table;
89     char *inc_column;
90     struct json *inc_where;
91     unsigned int inc_index;
92     int64_t inc_new_value;
93 };
94
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);
97
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 *,
104                                      const struct uuid *,
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 *);
110
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 *);
117
118 static void ovsdb_idl_row_parse(struct ovsdb_idl_row *);
119 static void ovsdb_idl_row_unparse(struct ovsdb_idl_row *);
120 static void ovsdb_idl_row_clear_old(struct ovsdb_idl_row *);
121 static void ovsdb_idl_row_clear_new(struct ovsdb_idl_row *);
122
123 static void ovsdb_idl_txn_abort_all(struct ovsdb_idl *);
124 static bool ovsdb_idl_txn_process_reply(struct ovsdb_idl *,
125                                         const struct jsonrpc_msg *msg);
126
127 struct ovsdb_idl *
128 ovsdb_idl_create(const char *remote, const struct ovsdb_idl_class *class)
129 {
130     struct ovsdb_idl *idl;
131     size_t i;
132
133     idl = xzalloc(sizeof *idl);
134     idl->class = class;
135     idl->session = jsonrpc_session_open(remote);
136     shash_init(&idl->table_by_name);
137     idl->tables = xmalloc(class->n_tables * sizeof *idl->tables);
138     for (i = 0; i < class->n_tables; i++) {
139         const struct ovsdb_idl_table_class *tc = &class->tables[i];
140         struct ovsdb_idl_table *table = &idl->tables[i];
141         size_t j;
142
143         assert(!shash_find(&idl->table_by_name, tc->name));
144         shash_add(&idl->table_by_name, tc->name, table);
145         table->class = tc;
146         shash_init(&table->columns);
147         for (j = 0; j < tc->n_columns; j++) {
148             const struct ovsdb_idl_column *column = &tc->columns[j];
149
150             assert(!shash_find(&table->columns, column->name));
151             shash_add(&table->columns, column->name, column);
152         }
153         hmap_init(&table->rows);
154         table->idl = idl;
155     }
156     idl->last_monitor_request_seqno = UINT_MAX;
157     hmap_init(&idl->outstanding_txns);
158
159     return idl;
160 }
161
162 void
163 ovsdb_idl_destroy(struct ovsdb_idl *idl)
164 {
165     if (idl) {
166         size_t i;
167
168         assert(!idl->txn);
169         ovsdb_idl_clear(idl);
170         jsonrpc_session_close(idl->session);
171
172         for (i = 0; i < idl->class->n_tables; i++) {
173             struct ovsdb_idl_table *table = &idl->tables[i];
174             shash_destroy(&table->columns);
175             hmap_destroy(&table->rows);
176         }
177         shash_destroy(&idl->table_by_name);
178         free(idl->tables);
179         json_destroy(idl->monitor_request_id);
180         free(idl);
181     }
182 }
183
184 static void
185 ovsdb_idl_clear(struct ovsdb_idl *idl)
186 {
187     bool changed = false;
188     size_t i;
189
190     for (i = 0; i < idl->class->n_tables; i++) {
191         struct ovsdb_idl_table *table = &idl->tables[i];
192         struct ovsdb_idl_row *row, *next_row;
193
194         if (hmap_is_empty(&table->rows)) {
195             continue;
196         }
197
198         changed = true;
199         HMAP_FOR_EACH_SAFE (row, next_row, struct ovsdb_idl_row, hmap_node,
200                             &table->rows) {
201             struct ovsdb_idl_arc *arc, *next_arc;
202
203             if (!ovsdb_idl_row_is_orphan(row)) {
204                 ovsdb_idl_row_unparse(row);
205                 ovsdb_idl_row_clear_old(row);
206             }
207             hmap_remove(&table->rows, &row->hmap_node);
208             LIST_FOR_EACH_SAFE (arc, next_arc, struct ovsdb_idl_arc, src_node,
209                                 &row->src_arcs) {
210                 free(arc);
211             }
212             /* No need to do anything with dst_arcs: some node has those arcs
213              * as forward arcs and will destroy them itself. */
214
215             free(row);
216         }
217     }
218
219     if (changed) {
220         idl->change_seqno++;
221     }
222 }
223
224 void
225 ovsdb_idl_run(struct ovsdb_idl *idl)
226 {
227     int i;
228
229     assert(!idl->txn);
230     jsonrpc_session_run(idl->session);
231     for (i = 0; jsonrpc_session_is_connected(idl->session) && i < 50; i++) {
232         struct jsonrpc_msg *msg, *reply;
233         unsigned int seqno;
234
235         seqno = jsonrpc_session_get_seqno(idl->session);
236         if (idl->last_monitor_request_seqno != seqno) {
237             idl->last_monitor_request_seqno = seqno;
238             ovsdb_idl_txn_abort_all(idl);
239             ovsdb_idl_send_monitor_request(idl);
240             break;
241         }
242
243         msg = jsonrpc_session_recv(idl->session);
244         if (!msg) {
245             break;
246         }
247
248         reply = NULL;
249         if (msg->type == JSONRPC_NOTIFY
250                    && !strcmp(msg->method, "update")
251                    && msg->params->type == JSON_ARRAY
252                    && msg->params->u.array.n == 2
253                    && msg->params->u.array.elems[0]->type == JSON_NULL) {
254             ovsdb_idl_parse_update(idl, msg->params->u.array.elems[1]);
255         } else if (msg->type == JSONRPC_REPLY
256                    && idl->monitor_request_id
257                    && json_equal(idl->monitor_request_id, msg->id)) {
258             json_destroy(idl->monitor_request_id);
259             idl->monitor_request_id = NULL;
260             ovsdb_idl_clear(idl);
261             ovsdb_idl_parse_update(idl, msg->result);
262         } else if (msg->type == JSONRPC_REPLY
263                    && msg->id && msg->id->type == JSON_STRING
264                    && !strcmp(msg->id->u.string, "echo")) {
265             /* It's a reply to our echo request.  Ignore it. */
266         } else if ((msg->type == JSONRPC_ERROR
267                     || msg->type == JSONRPC_REPLY)
268                    && ovsdb_idl_txn_process_reply(idl, msg)) {
269             /* ovsdb_idl_txn_process_reply() did everything needful. */
270         } else {
271             /* This can happen if ovsdb_idl_txn_destroy() is called to destroy
272              * a transaction before we receive the reply, so keep the log level
273              * low. */
274             VLOG_DBG("%s: received unexpected %s message",
275                      jsonrpc_session_get_name(idl->session),
276                      jsonrpc_msg_type_to_string(msg->type));
277         }
278         if (reply) {
279             jsonrpc_session_send(idl->session, reply);
280         }
281         jsonrpc_msg_destroy(msg);
282     }
283 }
284
285 void
286 ovsdb_idl_wait(struct ovsdb_idl *idl)
287 {
288     jsonrpc_session_wait(idl->session);
289     jsonrpc_session_recv_wait(idl->session);
290 }
291
292 unsigned int
293 ovsdb_idl_get_seqno(const struct ovsdb_idl *idl)
294 {
295     return idl->change_seqno;
296 }
297
298 bool
299 ovsdb_idl_has_ever_connected(const struct ovsdb_idl *idl)
300 {
301     return ovsdb_idl_get_seqno(idl) != 0;
302 }
303
304 void
305 ovsdb_idl_force_reconnect(struct ovsdb_idl *idl)
306 {
307     jsonrpc_session_force_reconnect(idl->session);
308 }
309 \f
310 static void
311 ovsdb_idl_send_monitor_request(struct ovsdb_idl *idl)
312 {
313     struct json *monitor_requests;
314     struct jsonrpc_msg *msg;
315     size_t i;
316
317     monitor_requests = json_object_create();
318     for (i = 0; i < idl->class->n_tables; i++) {
319         const struct ovsdb_idl_table *table = &idl->tables[i];
320         const struct ovsdb_idl_table_class *tc = table->class;
321         struct json *monitor_request, *columns;
322         size_t i;
323
324         monitor_request = json_object_create();
325         columns = json_array_create_empty();
326         for (i = 0; i < tc->n_columns; i++) {
327             const struct ovsdb_idl_column *column = &tc->columns[i];
328             json_array_add(columns, json_string_create(column->name));
329         }
330         json_object_put(monitor_request, "columns", columns);
331         json_object_put(monitor_requests, tc->name, monitor_request);
332     }
333
334     json_destroy(idl->monitor_request_id);
335     msg = jsonrpc_create_request(
336         "monitor", json_array_create_2(json_null_create(), monitor_requests),
337         &idl->monitor_request_id);
338     jsonrpc_session_send(idl->session, msg);
339 }
340
341 static void
342 ovsdb_idl_parse_update(struct ovsdb_idl *idl, const struct json *table_updates)
343 {
344     struct ovsdb_error *error;
345
346     idl->change_seqno++;
347
348     error = ovsdb_idl_parse_update__(idl, table_updates);
349     if (error) {
350         if (!VLOG_DROP_WARN(&syntax_rl)) {
351             char *s = ovsdb_error_to_string(error);
352             VLOG_WARN_RL(&syntax_rl, "%s", s);
353             free(s);
354         }
355         ovsdb_error_destroy(error);
356     }
357 }
358
359 static struct ovsdb_error *
360 ovsdb_idl_parse_update__(struct ovsdb_idl *idl,
361                          const struct json *table_updates)
362 {
363     const struct shash_node *tables_node;
364
365     if (table_updates->type != JSON_OBJECT) {
366         return ovsdb_syntax_error(table_updates, NULL,
367                                   "<table-updates> is not an object");
368     }
369     SHASH_FOR_EACH (tables_node, json_object(table_updates)) {
370         const struct json *table_update = tables_node->data;
371         const struct shash_node *table_node;
372         struct ovsdb_idl_table *table;
373
374         table = shash_find_data(&idl->table_by_name, tables_node->name);
375         if (!table) {
376             return ovsdb_syntax_error(
377                 table_updates, NULL,
378                 "<table-updates> includes unknown table \"%s\"",
379                 tables_node->name);
380         }
381
382         if (table_update->type != JSON_OBJECT) {
383             return ovsdb_syntax_error(table_update, NULL,
384                                       "<table-update> for table \"%s\" is "
385                                       "not an object", table->class->name);
386         }
387         SHASH_FOR_EACH (table_node, json_object(table_update)) {
388             const struct json *row_update = table_node->data;
389             const struct json *old_json, *new_json;
390             struct uuid uuid;
391
392             if (!uuid_from_string(&uuid, table_node->name)) {
393                 return ovsdb_syntax_error(table_update, NULL,
394                                           "<table-update> for table \"%s\" "
395                                           "contains bad UUID "
396                                           "\"%s\" as member name",
397                                           table->class->name,
398                                           table_node->name);
399             }
400             if (row_update->type != JSON_OBJECT) {
401                 return ovsdb_syntax_error(row_update, NULL,
402                                           "<table-update> for table \"%s\" "
403                                           "contains <row-update> for %s that "
404                                           "is not an object",
405                                           table->class->name,
406                                           table_node->name);
407             }
408
409             old_json = shash_find_data(json_object(row_update), "old");
410             new_json = shash_find_data(json_object(row_update), "new");
411             if (old_json && old_json->type != JSON_OBJECT) {
412                 return ovsdb_syntax_error(old_json, NULL,
413                                           "\"old\" <row> is not object");
414             } else if (new_json && new_json->type != JSON_OBJECT) {
415                 return ovsdb_syntax_error(new_json, NULL,
416                                           "\"new\" <row> is not object");
417             } else if ((old_json != NULL) + (new_json != NULL)
418                        != shash_count(json_object(row_update))) {
419                 return ovsdb_syntax_error(row_update, NULL,
420                                           "<row-update> contains unexpected "
421                                           "member");
422             } else if (!old_json && !new_json) {
423                 return ovsdb_syntax_error(row_update, NULL,
424                                           "<row-update> missing \"old\" "
425                                           "and \"new\" members");
426             }
427
428             ovsdb_idl_process_update(table, &uuid, old_json, new_json);
429         }
430     }
431
432     return NULL;
433 }
434
435 static struct ovsdb_idl_row *
436 ovsdb_idl_get_row(struct ovsdb_idl_table *table, const struct uuid *uuid)
437 {
438     struct ovsdb_idl_row *row;
439
440     HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_idl_row, hmap_node,
441                              uuid_hash(uuid), &table->rows) {
442         if (uuid_equals(&row->uuid, uuid)) {
443             return row;
444         }
445     }
446     return NULL;
447 }
448
449 static void
450 ovsdb_idl_process_update(struct ovsdb_idl_table *table,
451                          const struct uuid *uuid, const struct json *old,
452                          const struct json *new)
453 {
454     struct ovsdb_idl_row *row;
455
456     row = ovsdb_idl_get_row(table, uuid);
457     if (!new) {
458         /* Delete row. */
459         if (row && !ovsdb_idl_row_is_orphan(row)) {
460             /* XXX perhaps we should check the 'old' values? */
461             ovsdb_idl_delete_row(row);
462         } else {
463             VLOG_WARN_RL(&semantic_rl, "cannot delete missing row "UUID_FMT" "
464                          "from table %s",
465                          UUID_ARGS(uuid), table->class->name);
466         }
467     } else if (!old) {
468         /* Insert row. */
469         if (!row) {
470             ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
471         } else if (ovsdb_idl_row_is_orphan(row)) {
472             ovsdb_idl_insert_row(row, new);
473         } else {
474             VLOG_WARN_RL(&semantic_rl, "cannot add existing row "UUID_FMT" to "
475                          "table %s", UUID_ARGS(uuid), table->class->name);
476             ovsdb_idl_modify_row(row, new);
477         }
478     } else {
479         /* Modify row. */
480         if (row) {
481             /* XXX perhaps we should check the 'old' values? */
482             if (!ovsdb_idl_row_is_orphan(row)) {
483                 ovsdb_idl_modify_row(row, new);
484             } else {
485                 VLOG_WARN_RL(&semantic_rl, "cannot modify missing but "
486                              "referenced row "UUID_FMT" in table %s",
487                              UUID_ARGS(uuid), table->class->name);
488                 ovsdb_idl_insert_row(row, new);
489             }
490         } else {
491             VLOG_WARN_RL(&semantic_rl, "cannot modify missing row "UUID_FMT" "
492                          "in table %s", UUID_ARGS(uuid), table->class->name);
493             ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
494         }
495     }
496 }
497
498 static void
499 ovsdb_idl_row_update(struct ovsdb_idl_row *row, const struct json *row_json)
500 {
501     struct ovsdb_idl_table *table = row->table;
502     struct shash_node *node;
503
504     SHASH_FOR_EACH (node, json_object(row_json)) {
505         const char *column_name = node->name;
506         const struct ovsdb_idl_column *column;
507         struct ovsdb_datum datum;
508         struct ovsdb_error *error;
509
510         column = shash_find_data(&table->columns, column_name);
511         if (!column) {
512             VLOG_WARN_RL(&syntax_rl, "unknown column %s updating row "UUID_FMT,
513                          column_name, UUID_ARGS(&row->uuid));
514             continue;
515         }
516
517         error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
518         if (!error) {
519             ovsdb_datum_swap(&row->old[column - table->class->columns],
520                              &datum);
521             ovsdb_datum_destroy(&datum, &column->type);
522         } else {
523             char *s = ovsdb_error_to_string(error);
524             VLOG_WARN_RL(&syntax_rl, "error parsing column %s in row "UUID_FMT
525                          " in table %s: %s", column_name,
526                          UUID_ARGS(&row->uuid), table->class->name, s);
527             free(s);
528             ovsdb_error_destroy(error);
529         }
530     }
531 }
532
533 static bool
534 ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *row)
535 {
536     return !row->old;
537 }
538
539 static void
540 ovsdb_idl_row_parse(struct ovsdb_idl_row *row)
541 {
542     const struct ovsdb_idl_table_class *class = row->table->class;
543     size_t i;
544
545     for (i = 0; i < class->n_columns; i++) {
546         const struct ovsdb_idl_column *c = &class->columns[i];
547         (c->parse)(row, &row->old[i]);
548     }
549 }
550
551 static void
552 ovsdb_idl_row_unparse(struct ovsdb_idl_row *row)
553 {
554     const struct ovsdb_idl_table_class *class = row->table->class;
555     size_t i;
556
557     for (i = 0; i < class->n_columns; i++) {
558         const struct ovsdb_idl_column *c = &class->columns[i];
559         (c->unparse)(row);
560     }
561 }
562
563 static void
564 ovsdb_idl_row_clear_old(struct ovsdb_idl_row *row)
565 {
566     assert(row->old == row->new);
567     if (!ovsdb_idl_row_is_orphan(row)) {
568         const struct ovsdb_idl_table_class *class = row->table->class;
569         size_t i;
570
571         for (i = 0; i < class->n_columns; i++) {
572             ovsdb_datum_destroy(&row->old[i], &class->columns[i].type);
573         }
574         free(row->old);
575         row->old = row->new = NULL;
576     }
577 }
578
579 static void
580 ovsdb_idl_row_clear_new(struct ovsdb_idl_row *row)
581 {
582     if (row->old != row->new) {
583         if (row->new) {
584             const struct ovsdb_idl_table_class *class = row->table->class;
585             size_t i;
586
587             BITMAP_FOR_EACH_1 (i, class->n_columns, row->written) {
588                 ovsdb_datum_destroy(&row->new[i], &class->columns[i].type);
589             }
590             free(row->new);
591             free(row->written);
592             row->written = NULL;
593         }
594         row->new = row->old;
595     }
596 }
597
598 static void
599 ovsdb_idl_row_clear_arcs(struct ovsdb_idl_row *row, bool destroy_dsts)
600 {
601     struct ovsdb_idl_arc *arc, *next;
602
603     /* Delete all forward arcs.  If 'destroy_dsts', destroy any orphaned rows
604      * that this causes to be unreferenced. */
605     LIST_FOR_EACH_SAFE (arc, next, struct ovsdb_idl_arc, src_node,
606                         &row->src_arcs) {
607         list_remove(&arc->dst_node);
608         if (destroy_dsts
609             && ovsdb_idl_row_is_orphan(arc->dst)
610             && list_is_empty(&arc->dst->dst_arcs)) {
611             ovsdb_idl_row_destroy(arc->dst);
612         }
613         free(arc);
614     }
615     list_init(&row->src_arcs);
616 }
617
618 /* Force nodes that reference 'row' to reparse. */
619 static void
620 ovsdb_idl_row_reparse_backrefs(struct ovsdb_idl_row *row, bool destroy_dsts)
621 {
622     struct ovsdb_idl_arc *arc, *next;
623
624     /* This is trickier than it looks.  ovsdb_idl_row_clear_arcs() will destroy
625      * 'arc', so we need to use the "safe" variant of list traversal.  However,
626      * calling an ovsdb_idl_column's 'parse' function will add an arc
627      * equivalent to 'arc' to row->arcs.  That could be a problem for
628      * traversal, but it adds it at the beginning of the list to prevent us
629      * from stumbling upon it again.
630      *
631      * (If duplicate arcs were possible then we would need to make sure that
632      * 'next' didn't also point into 'arc''s destination, but we forbid
633      * duplicate arcs.) */
634     LIST_FOR_EACH_SAFE (arc, next, struct ovsdb_idl_arc, dst_node,
635                         &row->dst_arcs) {
636         struct ovsdb_idl_row *ref = arc->src;
637
638         ovsdb_idl_row_unparse(ref);
639         ovsdb_idl_row_clear_arcs(ref, destroy_dsts);
640         ovsdb_idl_row_parse(ref);
641     }
642 }
643
644 static struct ovsdb_idl_row *
645 ovsdb_idl_row_create__(const struct ovsdb_idl_table_class *class)
646 {
647     struct ovsdb_idl_row *row = xzalloc(class->allocation_size);
648     list_init(&row->src_arcs);
649     list_init(&row->dst_arcs);
650     hmap_node_nullify(&row->txn_node);
651     return row;
652 }
653
654 static struct ovsdb_idl_row *
655 ovsdb_idl_row_create(struct ovsdb_idl_table *table, const struct uuid *uuid)
656 {
657     struct ovsdb_idl_row *row = ovsdb_idl_row_create__(table->class);
658     hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
659     row->uuid = *uuid;
660     row->table = table;
661     return row;
662 }
663
664 static void
665 ovsdb_idl_row_destroy(struct ovsdb_idl_row *row)
666 {
667     if (row) {
668         ovsdb_idl_row_clear_old(row);
669         hmap_remove(&row->table->rows, &row->hmap_node);
670         free(row);
671     }
672 }
673
674 static void
675 ovsdb_idl_insert_row(struct ovsdb_idl_row *row, const struct json *row_json)
676 {
677     const struct ovsdb_idl_table_class *class = row->table->class;
678     size_t i;
679
680     assert(!row->old && !row->new);
681     row->old = row->new = xmalloc(class->n_columns * sizeof *row->old);
682     for (i = 0; i < class->n_columns; i++) {
683         ovsdb_datum_init_default(&row->old[i], &class->columns[i].type);
684     }
685     ovsdb_idl_row_update(row, row_json);
686     ovsdb_idl_row_parse(row);
687
688     ovsdb_idl_row_reparse_backrefs(row, false);
689 }
690
691 static void
692 ovsdb_idl_delete_row(struct ovsdb_idl_row *row)
693 {
694     ovsdb_idl_row_unparse(row);
695     ovsdb_idl_row_clear_arcs(row, true);
696     ovsdb_idl_row_clear_old(row);
697     if (list_is_empty(&row->dst_arcs)) {
698         ovsdb_idl_row_destroy(row);
699     } else {
700         ovsdb_idl_row_reparse_backrefs(row, true);
701     }
702 }
703
704 static void
705 ovsdb_idl_modify_row(struct ovsdb_idl_row *row, const struct json *row_json)
706 {
707     ovsdb_idl_row_unparse(row);
708     ovsdb_idl_row_clear_arcs(row, true);
709     ovsdb_idl_row_update(row, row_json);
710     ovsdb_idl_row_parse(row);
711 }
712
713 static bool
714 may_add_arc(const struct ovsdb_idl_row *src, const struct ovsdb_idl_row *dst)
715 {
716     const struct ovsdb_idl_arc *arc;
717
718     /* No self-arcs. */
719     if (src == dst) {
720         return false;
721     }
722
723     /* No duplicate arcs.
724      *
725      * We only need to test whether the first arc in dst->dst_arcs originates
726      * at 'src', since we add all of the arcs from a given source in a clump
727      * (in a single call to ovsdb_idl_row_parse()) and new arcs are always
728      * added at the front of the dst_arcs list. */
729     if (list_is_empty(&dst->dst_arcs)) {
730         return true;
731     }
732     arc = CONTAINER_OF(dst->dst_arcs.next, struct ovsdb_idl_arc, dst_node);
733     return arc->src != src;
734 }
735
736 static struct ovsdb_idl_table *
737 ovsdb_idl_table_from_class(const struct ovsdb_idl *idl,
738                            const struct ovsdb_idl_table_class *table_class)
739 {
740     return &idl->tables[table_class - idl->class->tables];
741 }
742
743 struct ovsdb_idl_row *
744 ovsdb_idl_get_row_arc(struct ovsdb_idl_row *src,
745                       struct ovsdb_idl_table_class *dst_table_class,
746                       const struct uuid *dst_uuid)
747 {
748     struct ovsdb_idl *idl = src->table->idl;
749     struct ovsdb_idl_table *dst_table;
750     struct ovsdb_idl_arc *arc;
751     struct ovsdb_idl_row *dst;
752
753     dst_table = ovsdb_idl_table_from_class(idl, dst_table_class);
754     dst = ovsdb_idl_get_row(dst_table, dst_uuid);
755     if (idl->txn) {
756         /* We're being called from ovsdb_idl_txn_write().  We must not update
757          * any arcs, because the transaction will be backed out at commit or
758          * abort time and we don't want our graph screwed up.
759          *
760          * Just return the destination row, if there is one and it has not been
761          * deleted. */
762         if (dst && (hmap_node_is_null(&dst->txn_node) || dst->new)) {
763             return dst;
764         }
765         return NULL;
766     } else {
767         /* We're being called from some other context.  Update the graph. */
768         if (!dst) {
769             dst = ovsdb_idl_row_create(dst_table, dst_uuid);
770         }
771
772         /* Add a new arc, if it wouldn't be a self-arc or a duplicate arc. */
773         if (may_add_arc(src, dst)) {
774             /* The arc *must* be added at the front of the dst_arcs list.  See
775              * ovsdb_idl_row_reparse_backrefs() for details. */
776             arc = xmalloc(sizeof *arc);
777             list_push_front(&src->src_arcs, &arc->src_node);
778             list_push_front(&dst->dst_arcs, &arc->dst_node);
779             arc->src = src;
780             arc->dst = dst;
781         }
782
783         return !ovsdb_idl_row_is_orphan(dst) ? dst : NULL;
784     }
785 }
786
787 const struct ovsdb_idl_row *
788 ovsdb_idl_get_row_for_uuid(const struct ovsdb_idl *idl,
789                            const struct ovsdb_idl_table_class *tc,
790                            const struct uuid *uuid)
791 {
792     return ovsdb_idl_get_row(ovsdb_idl_table_from_class(idl, tc), uuid);
793 }
794
795 static struct ovsdb_idl_row *
796 next_real_row(struct ovsdb_idl_table *table, struct hmap_node *node)
797 {
798     for (; node; node = hmap_next(&table->rows, node)) {
799         struct ovsdb_idl_row *row;
800
801         row = CONTAINER_OF(node, struct ovsdb_idl_row, hmap_node);
802         if (row->new || !ovsdb_idl_row_is_orphan(row)) {
803             return row;
804         }
805     }
806     return NULL;
807 }
808
809 const struct ovsdb_idl_row *
810 ovsdb_idl_first_row(const struct ovsdb_idl *idl,
811                     const struct ovsdb_idl_table_class *table_class)
812 {
813     struct ovsdb_idl_table *table
814         = ovsdb_idl_table_from_class(idl, table_class);
815     return next_real_row(table, hmap_first(&table->rows));
816 }
817
818 const struct ovsdb_idl_row *
819 ovsdb_idl_next_row(const struct ovsdb_idl_row *row)
820 {
821     struct ovsdb_idl_table *table = row->table;
822
823     return next_real_row(table, hmap_next(&table->rows, &row->hmap_node));
824 }
825 \f
826 /* Transactions. */
827
828 static void ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
829                                    enum ovsdb_idl_txn_status);
830
831 const char *
832 ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status status)
833 {
834     switch (status) {
835     case TXN_UNCHANGED:
836         return "unchanged";
837     case TXN_INCOMPLETE:
838         return "incomplete";
839     case TXN_ABORTED:
840         return "aborted";
841     case TXN_SUCCESS:
842         return "success";
843     case TXN_TRY_AGAIN:
844         return "try again";
845     case TXN_ERROR:
846         return "error";
847     }
848     return "<unknown>";
849 }
850
851 struct ovsdb_idl_txn *
852 ovsdb_idl_txn_create(struct ovsdb_idl *idl)
853 {
854     struct ovsdb_idl_txn *txn;
855
856     assert(!idl->txn);
857     idl->txn = txn = xmalloc(sizeof *txn);
858     txn->idl = idl;
859     txn->status = TXN_INCOMPLETE;
860     hmap_init(&txn->txn_rows);
861     txn->dry_run = false;
862     ds_init(&txn->comment);
863     txn->inc_table = NULL;
864     txn->inc_column = NULL;
865     txn->inc_where = NULL;
866     return txn;
867 }
868
869 void
870 ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *txn, const char *s)
871 {
872     if (txn->comment.length) {
873         ds_put_char(&txn->comment, '\n');
874     }
875     ds_put_cstr(&txn->comment, s);
876 }
877
878 void
879 ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *txn)
880 {
881     txn->dry_run = true;
882 }
883
884 void
885 ovsdb_idl_txn_increment(struct ovsdb_idl_txn *txn, const char *table,
886                         const char *column, const struct json *where)
887 {
888     assert(!txn->inc_table);
889     txn->inc_table = xstrdup(table);
890     txn->inc_column = xstrdup(column);
891     txn->inc_where = where ? json_clone(where) : json_array_create_empty();
892 }
893
894 void
895 ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *txn)
896 {
897     if (txn->status == TXN_INCOMPLETE) {
898         hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
899     }
900     ovsdb_idl_txn_abort(txn);
901     ds_destroy(&txn->comment);
902     free(txn->inc_table);
903     free(txn->inc_column);
904     json_destroy(txn->inc_where);
905     free(txn);
906 }
907
908 void
909 ovsdb_idl_txn_wait(const struct ovsdb_idl_txn *txn)
910 {
911     if (txn->status != TXN_INCOMPLETE) {
912         poll_immediate_wake();
913     }
914 }
915
916 static struct json *
917 where_uuid_equals(const struct uuid *uuid)
918 {
919     return
920         json_array_create_1(
921             json_array_create_3(
922                 json_string_create("_uuid"),
923                 json_string_create("=="),
924                 json_array_create_2(
925                     json_string_create("uuid"),
926                     json_string_create_nocopy(
927                         xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
928 }
929
930 static char *
931 uuid_name_from_uuid(const struct uuid *uuid)
932 {
933     char *name;
934     char *p;
935
936     name = xasprintf("row"UUID_FMT, UUID_ARGS(uuid));
937     for (p = name; *p != '\0'; p++) {
938         if (*p == '-') {
939             *p = '_';
940         }
941     }
942
943     return name;
944 }
945
946 static const struct ovsdb_idl_row *
947 ovsdb_idl_txn_get_row(const struct ovsdb_idl_txn *txn, const struct uuid *uuid)
948 {
949     const struct ovsdb_idl_row *row;
950
951     HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_idl_row, txn_node,
952                              uuid_hash(uuid), &txn->txn_rows) {
953         if (uuid_equals(&row->uuid, uuid)) {
954             return row;
955         }
956     }
957     return NULL;
958 }
959
960 /* XXX there must be a cleaner way to do this */
961 static struct json *
962 substitute_uuids(struct json *json, const struct ovsdb_idl_txn *txn)
963 {
964     if (json->type == JSON_ARRAY) {
965         struct uuid uuid;
966         size_t i;
967
968         if (json->u.array.n == 2
969             && json->u.array.elems[0]->type == JSON_STRING
970             && json->u.array.elems[1]->type == JSON_STRING
971             && !strcmp(json->u.array.elems[0]->u.string, "uuid")
972             && uuid_from_string(&uuid, json->u.array.elems[1]->u.string)) {
973             const struct ovsdb_idl_row *row;
974
975             row = ovsdb_idl_txn_get_row(txn, &uuid);
976             if (row && !row->old && row->new) {
977                 json_destroy(json);
978
979                 return json_array_create_2(
980                     json_string_create("named-uuid"),
981                     json_string_create_nocopy(uuid_name_from_uuid(&uuid)));
982             }
983         }
984
985         for (i = 0; i < json->u.array.n; i++) {
986             json->u.array.elems[i] = substitute_uuids(json->u.array.elems[i],
987                                                       txn);
988         }
989     } else if (json->type == JSON_OBJECT) {
990         struct shash_node *node;
991
992         SHASH_FOR_EACH (node, json_object(json)) {
993             node->data = substitute_uuids(node->data, txn);
994         }
995     }
996     return json;
997 }
998
999 static void
1000 ovsdb_idl_txn_disassemble(struct ovsdb_idl_txn *txn)
1001 {
1002     struct ovsdb_idl_row *row, *next;
1003
1004     /* This must happen early.  Otherwise, ovsdb_idl_row_parse() will call an
1005      * ovsdb_idl_column's 'parse' function, which will call
1006      * ovsdb_idl_get_row_arc(), which will seen that the IDL is in a
1007      * transaction and fail to update the graph.  */
1008     txn->idl->txn = NULL;
1009
1010     HMAP_FOR_EACH_SAFE (row, next, struct ovsdb_idl_row, txn_node,
1011                         &txn->txn_rows) {
1012         if (row->old) {
1013             if (row->written) {
1014                 ovsdb_idl_row_unparse(row);
1015                 ovsdb_idl_row_clear_arcs(row, false);
1016                 ovsdb_idl_row_parse(row);
1017             }
1018         } else {
1019             hmap_remove(&row->table->rows, &row->hmap_node);
1020         }
1021         ovsdb_idl_row_clear_new(row);
1022
1023         free(row->prereqs);
1024         row->prereqs = NULL;
1025
1026         free(row->written);
1027         row->written = NULL;
1028
1029         hmap_remove(&txn->txn_rows, &row->txn_node);
1030         hmap_node_nullify(&row->txn_node);
1031     }
1032     hmap_destroy(&txn->txn_rows);
1033     hmap_init(&txn->txn_rows);
1034 }
1035
1036 enum ovsdb_idl_txn_status
1037 ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
1038 {
1039     struct ovsdb_idl_row *row;
1040     struct json *operations;
1041     bool any_updates;
1042
1043     if (txn != txn->idl->txn) {
1044         return txn->status;
1045     }
1046
1047     operations = json_array_create_empty();
1048
1049     /* Add prerequisites and declarations of new rows. */
1050     HMAP_FOR_EACH (row, struct ovsdb_idl_row, txn_node, &txn->txn_rows) {
1051         /* XXX check that deleted rows exist even if no prereqs? */
1052         if (row->prereqs) {
1053             const struct ovsdb_idl_table_class *class = row->table->class;
1054             size_t n_columns = class->n_columns;
1055             struct json *op, *columns, *row_json;
1056             size_t idx;
1057
1058             op = json_object_create();
1059             json_array_add(operations, op);
1060             json_object_put_string(op, "op", "wait");
1061             json_object_put_string(op, "table", class->name);
1062             json_object_put(op, "timeout", json_integer_create(0));
1063             json_object_put(op, "where", where_uuid_equals(&row->uuid));
1064             json_object_put_string(op, "until", "==");
1065             columns = json_array_create_empty();
1066             json_object_put(op, "columns", columns);
1067             row_json = json_object_create();
1068             json_object_put(op, "rows", json_array_create_1(row_json));
1069
1070             BITMAP_FOR_EACH_1 (idx, n_columns, row->prereqs) {
1071                 const struct ovsdb_idl_column *column = &class->columns[idx];
1072                 json_array_add(columns, json_string_create(column->name));
1073                 json_object_put(row_json, column->name,
1074                                 ovsdb_datum_to_json(&row->old[idx],
1075                                                     &column->type));
1076             }
1077         }
1078         if (row->new && !row->old) {
1079             struct json *op;
1080
1081             op = json_object_create();
1082             json_array_add(operations, op);
1083             json_object_put_string(op, "op", "declare");
1084             json_object_put(op, "uuid-name",
1085                             json_string_create_nocopy(
1086                                 uuid_name_from_uuid(&row->uuid)));
1087         }
1088     }
1089
1090     /* Add updates. */
1091     any_updates = false;
1092     HMAP_FOR_EACH (row, struct ovsdb_idl_row, txn_node, &txn->txn_rows) {
1093         const struct ovsdb_idl_table_class *class = row->table->class;
1094
1095         if (row->old == row->new) {
1096             continue;
1097         } else if (!row->new) {
1098             struct json *op = json_object_create();
1099             json_object_put_string(op, "op", "delete");
1100             json_object_put_string(op, "table", class->name);
1101             json_object_put(op, "where", where_uuid_equals(&row->uuid));
1102             json_array_add(operations, op);
1103             any_updates = true;
1104         } else {
1105             struct json *row_json;
1106             struct json *op;
1107             size_t idx;
1108
1109             op = json_object_create();
1110             json_object_put_string(op, "op", row->old ? "update" : "insert");
1111             json_object_put_string(op, "table", class->name);
1112             if (row->old) {
1113                 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1114             } else {
1115                 json_object_put(op, "uuid-name",
1116                                 json_string_create_nocopy(
1117                                     uuid_name_from_uuid(&row->uuid)));
1118             }
1119             row_json = json_object_create();
1120             json_object_put(op, "row", row_json);
1121
1122             BITMAP_FOR_EACH_1 (idx, class->n_columns, row->written) {
1123                 const struct ovsdb_idl_column *column = &class->columns[idx];
1124
1125                 if (row->old
1126                     ? !ovsdb_datum_equals(&row->old[idx], &row->new[idx],
1127                                           &column->type)
1128                     : !ovsdb_datum_is_default(&row->new[idx], &column->type)) {
1129                     json_object_put(row_json, column->name,
1130                                     substitute_uuids(
1131                                         ovsdb_datum_to_json(&row->new[idx],
1132                                                             &column->type),
1133                                         txn));
1134                 }
1135             }
1136
1137             if (!row->old || !shash_is_empty(json_object(row_json))) {
1138                 json_array_add(operations, op);
1139                 any_updates = true;
1140             } else {
1141                 json_destroy(op);
1142             }
1143         }
1144     }
1145
1146     /* Add increment. */
1147     if (txn->inc_table && any_updates) {
1148         struct json *op;
1149
1150         txn->inc_index = operations->u.array.n;
1151
1152         op = json_object_create();
1153         json_object_put_string(op, "op", "mutate");
1154         json_object_put_string(op, "table", txn->inc_table);
1155         json_object_put(op, "where",
1156                         substitute_uuids(json_clone(txn->inc_where), txn));
1157         json_object_put(op, "mutations",
1158                         json_array_create_1(
1159                             json_array_create_3(
1160                                 json_string_create(txn->inc_column),
1161                                 json_string_create("+="),
1162                                 json_integer_create(1))));
1163         json_array_add(operations, op);
1164
1165         op = json_object_create();
1166         json_object_put_string(op, "op", "select");
1167         json_object_put_string(op, "table", txn->inc_table);
1168         json_object_put(op, "where",
1169                         substitute_uuids(json_clone(txn->inc_where), txn));
1170         json_object_put(op, "columns",
1171                         json_array_create_1(json_string_create(
1172                                                 txn->inc_column)));
1173         json_array_add(operations, op);
1174     }
1175
1176     if (txn->comment.length) {
1177         struct json *op = json_object_create();
1178         json_object_put_string(op, "op", "comment");
1179         json_object_put_string(op, "comment", ds_cstr(&txn->comment));
1180         json_array_add(operations, op);
1181     }
1182
1183     if (txn->dry_run) {
1184         struct json *op = json_object_create();
1185         json_object_put_string(op, "op", "abort");
1186         json_array_add(operations, op);
1187     }
1188
1189     if (!any_updates) {
1190         txn->status = TXN_UNCHANGED;
1191         json_destroy(operations);
1192     } else if (!jsonrpc_session_send(
1193                    txn->idl->session,
1194                    jsonrpc_create_request(
1195                        "transact", operations, &txn->request_id))) {
1196         hmap_insert(&txn->idl->outstanding_txns, &txn->hmap_node,
1197                     json_hash(txn->request_id, 0));
1198     } else {
1199         txn->status = TXN_INCOMPLETE;
1200     }
1201
1202     ovsdb_idl_txn_disassemble(txn);
1203     return txn->status;
1204 }
1205
1206 int64_t
1207 ovsdb_idl_txn_get_increment_new_value(const struct ovsdb_idl_txn *txn)
1208 {
1209     assert(txn->status == TXN_SUCCESS);
1210     return txn->inc_new_value;
1211 }
1212
1213 void
1214 ovsdb_idl_txn_abort(struct ovsdb_idl_txn *txn)
1215 {
1216     ovsdb_idl_txn_disassemble(txn);
1217     if (txn->status == TXN_INCOMPLETE) {
1218         txn->status = TXN_ABORTED;
1219     }
1220 }
1221
1222 static void
1223 ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
1224                        enum ovsdb_idl_txn_status status)
1225 {
1226     txn->status = status;
1227     hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
1228 }
1229
1230 void
1231 ovsdb_idl_txn_read(const struct ovsdb_idl_row *row,
1232                    const struct ovsdb_idl_column *column,
1233                    struct ovsdb_datum *datum)
1234 {
1235     const struct ovsdb_idl_table_class *class = row->table->class;
1236     size_t column_idx = column - class->columns;
1237
1238     assert(row->new != NULL);
1239     if (row->written && bitmap_is_set(row->written, column_idx)) {
1240         ovsdb_datum_clone(datum, &row->new[column_idx], &column->type);
1241     } else if (row->old) {
1242         ovsdb_datum_clone(datum, &row->old[column_idx], &column->type);
1243     } else {
1244         ovsdb_datum_init_default(datum, &column->type);
1245     }
1246 }
1247
1248 void
1249 ovsdb_idl_txn_write(const struct ovsdb_idl_row *row_,
1250                     const struct ovsdb_idl_column *column,
1251                     struct ovsdb_datum *datum)
1252 {
1253     struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1254     const struct ovsdb_idl_table_class *class = row->table->class;
1255     size_t column_idx = column - class->columns;
1256
1257     assert(row->new != NULL);
1258     if (hmap_node_is_null(&row->txn_node)) {
1259         hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1260                     uuid_hash(&row->uuid));
1261     }
1262     if (row->old == row->new) {
1263         row->new = xmalloc(class->n_columns * sizeof *row->new);
1264     }
1265     if (!row->written) {
1266         row->written = bitmap_allocate(class->n_columns);
1267     }
1268     if (bitmap_is_set(row->written, column_idx)) {
1269         ovsdb_datum_destroy(&row->new[column_idx], &column->type);
1270     } else {
1271         bitmap_set1(row->written, column_idx);
1272     }
1273     row->new[column_idx] = *datum;
1274     (column->unparse)(row);
1275     (column->parse)(row, &row->new[column_idx]);
1276 }
1277
1278 void
1279 ovsdb_idl_txn_verify(const struct ovsdb_idl_row *row_,
1280                      const struct ovsdb_idl_column *column)
1281 {
1282     struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1283     const struct ovsdb_idl_table_class *class = row->table->class;
1284     size_t column_idx = column - class->columns;
1285
1286     assert(row->new != NULL);
1287     if (!row->old
1288         || (row->written && bitmap_is_set(row->written, column_idx))) {
1289         return;
1290     }
1291
1292     if (hmap_node_is_null(&row->txn_node)) {
1293         hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1294                     uuid_hash(&row->uuid));
1295     }
1296     if (!row->prereqs) {
1297         row->prereqs = bitmap_allocate(class->n_columns);
1298     }
1299     bitmap_set1(row->prereqs, column_idx);
1300 }
1301
1302 void
1303 ovsdb_idl_txn_delete(const struct ovsdb_idl_row *row_)
1304 {
1305     struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1306
1307     assert(row->new != NULL);
1308     if (!row->old) {
1309         ovsdb_idl_row_clear_new(row);
1310         assert(!row->prereqs);
1311         hmap_remove(&row->table->rows, &row->hmap_node);
1312         hmap_remove(&row->table->idl->txn->txn_rows, &row->txn_node);
1313         free(row);
1314         return;
1315     }
1316     if (hmap_node_is_null(&row->txn_node)) {
1317         hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1318                     uuid_hash(&row->uuid));
1319     }
1320     ovsdb_idl_row_clear_new(row);
1321     row->new = NULL;
1322 }
1323
1324 const struct ovsdb_idl_row *
1325 ovsdb_idl_txn_insert(struct ovsdb_idl_txn *txn,
1326                      const struct ovsdb_idl_table_class *class)
1327 {
1328     struct ovsdb_idl_row *row = ovsdb_idl_row_create__(class);
1329     uuid_generate(&row->uuid);
1330     row->table = ovsdb_idl_table_from_class(txn->idl, class);
1331     row->new = xmalloc(class->n_columns * sizeof *row->new);
1332     row->written = bitmap_allocate(class->n_columns);
1333     hmap_insert(&row->table->rows, &row->hmap_node, uuid_hash(&row->uuid));
1334     hmap_insert(&txn->txn_rows, &row->txn_node, uuid_hash(&row->uuid));
1335     return row;
1336 }
1337
1338 static void
1339 ovsdb_idl_txn_abort_all(struct ovsdb_idl *idl)
1340 {
1341     struct ovsdb_idl_txn *txn;
1342
1343     HMAP_FOR_EACH (txn, struct ovsdb_idl_txn, hmap_node,
1344                    &idl->outstanding_txns) {
1345         ovsdb_idl_txn_complete(txn, TXN_TRY_AGAIN);
1346     }
1347 }
1348
1349 static struct ovsdb_idl_txn *
1350 ovsdb_idl_txn_find(struct ovsdb_idl *idl, const struct json *id)
1351 {
1352     struct ovsdb_idl_txn *txn;
1353
1354     HMAP_FOR_EACH_WITH_HASH (txn, struct ovsdb_idl_txn, hmap_node,
1355                              json_hash(id, 0), &idl->outstanding_txns) {
1356         if (json_equal(id, txn->request_id)) {
1357             return txn;
1358         }
1359     }
1360     return NULL;
1361 }
1362
1363 static bool
1364 check_json_type(const struct json *json, enum json_type type, const char *name)
1365 {
1366     if (!json) {
1367         VLOG_WARN_RL(&syntax_rl, "%s is missing", name);
1368         return false;
1369     } else if (json->type != type) {
1370         VLOG_WARN_RL(&syntax_rl, "%s is %s instead of %s",
1371                      name, json_type_to_string(json->type),
1372                      json_type_to_string(type));
1373         return false;
1374     } else {
1375         return true;
1376     }
1377 }
1378
1379 static bool
1380 ovsdb_idl_txn_process_inc_reply(struct ovsdb_idl_txn *txn,
1381                                 const struct json_array *results)
1382 {
1383     struct json *count, *rows, *row, *column;
1384     struct shash *mutate, *select;
1385
1386     if (txn->inc_index + 2 > results->n) {
1387         VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
1388                      "for increment (has %u, needs %u)",
1389                      results->n, txn->inc_index + 2);
1390         return false;
1391     }
1392
1393     /* We know that this is a JSON objects because the loop in
1394      * ovsdb_idl_txn_process_reply() checked. */
1395     mutate = json_object(results->elems[txn->inc_index]);
1396     count = shash_find_data(mutate, "count");
1397     if (!check_json_type(count, JSON_INTEGER, "\"mutate\" reply \"count\"")) {
1398         return false;
1399     }
1400     if (count->u.integer != 1) {
1401         VLOG_WARN_RL(&syntax_rl,
1402                      "\"mutate\" reply \"count\" is %"PRId64" instead of 1",
1403                      count->u.integer);
1404         return false;
1405     }
1406
1407     select = json_object(results->elems[txn->inc_index + 1]);
1408     rows = shash_find_data(select, "rows");
1409     if (!check_json_type(rows, JSON_ARRAY, "\"select\" reply \"rows\"")) {
1410         return false;
1411     }
1412     if (rows->u.array.n != 1) {
1413         VLOG_WARN_RL(&syntax_rl, "\"select\" reply \"rows\" has %u elements "
1414                      "instead of 1",
1415                      rows->u.array.n);
1416         return false;
1417     }
1418     row = rows->u.array.elems[0];
1419     if (!check_json_type(row, JSON_OBJECT, "\"select\" reply row")) {
1420         return false;
1421     }
1422     column = shash_find_data(json_object(row), txn->inc_column);
1423     if (!check_json_type(column, JSON_INTEGER,
1424                          "\"select\" reply inc column")) {
1425         return false;
1426     }
1427     txn->inc_new_value = column->u.integer;
1428     return true;
1429 }
1430
1431
1432 static bool
1433 ovsdb_idl_txn_process_reply(struct ovsdb_idl *idl,
1434                             const struct jsonrpc_msg *msg)
1435 {
1436     struct ovsdb_idl_txn *txn;
1437     enum ovsdb_idl_txn_status status;
1438
1439     txn = ovsdb_idl_txn_find(idl, msg->id);
1440     if (!txn) {
1441         return false;
1442     }
1443
1444     if (msg->type == JSONRPC_ERROR) {
1445         status = TXN_ERROR;
1446     } else if (msg->result->type != JSON_ARRAY) {
1447         VLOG_WARN_RL(&syntax_rl, "reply to \"transact\" is not JSON array");
1448         status = TXN_ERROR;
1449     } else {
1450         int hard_errors = 0;
1451         int soft_errors = 0;
1452         size_t i;
1453
1454         for (i = 0; i < msg->result->u.array.n; i++) {
1455             struct json *json = msg->result->u.array.elems[i];
1456
1457             if (json->type == JSON_NULL) {
1458                 /* This isn't an error in itself but indicates that some prior
1459                  * operation failed, so make sure that we know about it. */
1460                 soft_errors++;
1461             } else if (json->type == JSON_OBJECT) {
1462                 struct json *error;
1463
1464                 error = shash_find_data(json_object(json), "error");
1465                 if (error) {
1466                     if (error->type == JSON_STRING) {
1467                         if (!strcmp(error->u.string, "timed out")) {
1468                             soft_errors++;
1469                         } else if (strcmp(error->u.string, "aborted")) {
1470                             hard_errors++;
1471                         }
1472                     } else {
1473                         hard_errors++;
1474                         VLOG_WARN_RL(&syntax_rl,
1475                                      "\"error\" in reply is not JSON string");
1476                     }
1477                 }
1478             } else {
1479                 hard_errors++;
1480                 VLOG_WARN_RL(&syntax_rl,
1481                              "operation reply is not JSON null or object");
1482             }
1483         }
1484
1485         if (txn->inc_table
1486             && !soft_errors
1487             && !hard_errors
1488             && !ovsdb_idl_txn_process_inc_reply(txn,
1489                                                 json_array(msg->result))) {
1490             hard_errors++;
1491         }
1492
1493         status = (hard_errors ? TXN_ERROR
1494                   : soft_errors ? TXN_TRY_AGAIN
1495                   : TXN_SUCCESS);
1496     }
1497
1498     ovsdb_idl_txn_complete(txn, status);
1499     return true;
1500 }
1501
1502 struct ovsdb_idl_txn *
1503 ovsdb_idl_txn_get(const struct ovsdb_idl_row *row)
1504 {
1505     struct ovsdb_idl_txn *txn = row->table->idl->txn;
1506     assert(txn != NULL);
1507     return txn;
1508 }