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