compat: Backport nf_connlabels_{get, put}().
[cascardo/ovs.git] / lib / ovsdb-idl.c
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
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 <errno.h>
21 #include <inttypes.h>
22 #include <limits.h>
23 #include <stdlib.h>
24
25 #include "bitmap.h"
26 #include "coverage.h"
27 #include "dynamic-string.h"
28 #include "fatal-signal.h"
29 #include "json.h"
30 #include "jsonrpc.h"
31 #include "ovsdb/ovsdb.h"
32 #include "ovsdb/table.h"
33 #include "ovsdb-data.h"
34 #include "ovsdb-error.h"
35 #include "ovsdb-idl-provider.h"
36 #include "ovsdb-parser.h"
37 #include "poll-loop.h"
38 #include "shash.h"
39 #include "sset.h"
40 #include "util.h"
41 #include "openvswitch/vlog.h"
42
43 VLOG_DEFINE_THIS_MODULE(ovsdb_idl);
44
45 COVERAGE_DEFINE(txn_uncommitted);
46 COVERAGE_DEFINE(txn_unchanged);
47 COVERAGE_DEFINE(txn_incomplete);
48 COVERAGE_DEFINE(txn_aborted);
49 COVERAGE_DEFINE(txn_success);
50 COVERAGE_DEFINE(txn_try_again);
51 COVERAGE_DEFINE(txn_not_locked);
52 COVERAGE_DEFINE(txn_error);
53
54 /* An arc from one idl_row to another.  When row A contains a UUID that
55  * references row B, this is represented by an arc from A (the source) to B
56  * (the destination).
57  *
58  * Arcs from a row to itself are omitted, that is, src and dst are always
59  * different.
60  *
61  * Arcs are never duplicated, that is, even if there are multiple references
62  * from A to B, there is only a single arc from A to B.
63  *
64  * Arcs are directed: an arc from A to B is the converse of an an arc from B to
65  * A.  Both an arc and its converse may both be present, if each row refers
66  * to the other circularly.
67  *
68  * The source and destination row may be in the same table or in different
69  * tables.
70  */
71 struct ovsdb_idl_arc {
72     struct ovs_list src_node;   /* In src->src_arcs list. */
73     struct ovs_list dst_node;   /* In dst->dst_arcs list. */
74     struct ovsdb_idl_row *src;  /* Source row. */
75     struct ovsdb_idl_row *dst;  /* Destination row. */
76 };
77
78 enum ovsdb_idl_state {
79     IDL_S_SCHEMA_REQUESTED,
80     IDL_S_MONITOR_REQUESTED,
81     IDL_S_MONITORING
82 };
83
84 struct ovsdb_idl {
85     const struct ovsdb_idl_class *class;
86     struct jsonrpc_session *session;
87     struct shash table_by_name;
88     struct ovsdb_idl_table *tables; /* Contains "struct ovsdb_idl_table *"s.*/
89     unsigned int change_seqno;
90     bool verify_write_only;
91
92     /* Session state. */
93     unsigned int state_seqno;
94     enum ovsdb_idl_state state;
95     struct json *request_id;
96
97     /* Database locking. */
98     char *lock_name;            /* Name of lock we need, NULL if none. */
99     bool has_lock;              /* Has db server told us we have the lock? */
100     bool is_lock_contended;     /* Has db server told us we can't get lock? */
101     struct json *lock_request_id; /* JSON-RPC ID of in-flight lock request. */
102
103     /* Transaction support. */
104     struct ovsdb_idl_txn *txn;
105     struct hmap outstanding_txns;
106 };
107
108 struct ovsdb_idl_txn {
109     struct hmap_node hmap_node;
110     struct json *request_id;
111     struct ovsdb_idl *idl;
112     struct hmap txn_rows;
113     enum ovsdb_idl_txn_status status;
114     char *error;
115     bool dry_run;
116     struct ds comment;
117
118     /* Increments. */
119     const char *inc_table;
120     const char *inc_column;
121     struct uuid inc_row;
122     unsigned int inc_index;
123     int64_t inc_new_value;
124
125     /* Inserted rows. */
126     struct hmap inserted_rows;  /* Contains "struct ovsdb_idl_txn_insert"s. */
127 };
128
129 struct ovsdb_idl_txn_insert {
130     struct hmap_node hmap_node; /* In struct ovsdb_idl_txn's inserted_rows. */
131     struct uuid dummy;          /* Dummy UUID used locally. */
132     int op_index;               /* Index into transaction's operation array. */
133     struct uuid real;           /* Real UUID used by database server. */
134 };
135
136 static struct vlog_rate_limit syntax_rl = VLOG_RATE_LIMIT_INIT(1, 5);
137 static struct vlog_rate_limit semantic_rl = VLOG_RATE_LIMIT_INIT(1, 5);
138
139 static void ovsdb_idl_clear(struct ovsdb_idl *);
140 static void ovsdb_idl_send_schema_request(struct ovsdb_idl *);
141 static void ovsdb_idl_send_monitor_request(struct ovsdb_idl *,
142                                            const struct json *schema_json);
143 static void ovsdb_idl_parse_update(struct ovsdb_idl *, const struct json *);
144 static struct ovsdb_error *ovsdb_idl_parse_update__(struct ovsdb_idl *,
145                                                     const struct json *);
146 static bool ovsdb_idl_process_update(struct ovsdb_idl_table *,
147                                      const struct uuid *,
148                                      const struct json *old,
149                                      const struct json *new);
150 static void ovsdb_idl_insert_row(struct ovsdb_idl_row *, const struct json *);
151 static void ovsdb_idl_delete_row(struct ovsdb_idl_row *);
152 static bool ovsdb_idl_modify_row(struct ovsdb_idl_row *, const struct json *);
153
154 static bool ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *);
155 static struct ovsdb_idl_row *ovsdb_idl_row_create__(
156     const struct ovsdb_idl_table_class *);
157 static struct ovsdb_idl_row *ovsdb_idl_row_create(struct ovsdb_idl_table *,
158                                                   const struct uuid *);
159 static void ovsdb_idl_row_destroy(struct ovsdb_idl_row *);
160 static void ovsdb_idl_row_destroy_postprocess(struct ovsdb_idl *);
161
162 static void ovsdb_idl_row_parse(struct ovsdb_idl_row *);
163 static void ovsdb_idl_row_unparse(struct ovsdb_idl_row *);
164 static void ovsdb_idl_row_clear_old(struct ovsdb_idl_row *);
165 static void ovsdb_idl_row_clear_new(struct ovsdb_idl_row *);
166 static void ovsdb_idl_row_clear_arcs(struct ovsdb_idl_row *, bool destroy_dsts);
167
168 static void ovsdb_idl_txn_abort_all(struct ovsdb_idl *);
169 static bool ovsdb_idl_txn_process_reply(struct ovsdb_idl *,
170                                         const struct jsonrpc_msg *msg);
171
172 static void ovsdb_idl_send_lock_request(struct ovsdb_idl *);
173 static void ovsdb_idl_send_unlock_request(struct ovsdb_idl *);
174 static void ovsdb_idl_parse_lock_reply(struct ovsdb_idl *,
175                                        const struct json *);
176 static void ovsdb_idl_parse_lock_notify(struct ovsdb_idl *,
177                                         const struct json *params,
178                                         bool new_has_lock);
179 static struct ovsdb_idl_table *
180 ovsdb_idl_table_from_class(const struct ovsdb_idl *,
181                            const struct ovsdb_idl_table_class *);
182 static bool ovsdb_idl_track_is_set(struct ovsdb_idl_table *table);
183
184 /* Creates and returns a connection to database 'remote', which should be in a
185  * form acceptable to jsonrpc_session_open().  The connection will maintain an
186  * in-memory replica of the remote database whose schema is described by
187  * 'class'.  (Ordinarily 'class' is compiled from an OVSDB schema automatically
188  * by ovsdb-idlc.)
189  *
190  * Passes 'retry' to jsonrpc_session_open().  See that function for
191  * documentation.
192  *
193  * If 'monitor_everything_by_default' is true, then everything in the remote
194  * database will be replicated by default.  ovsdb_idl_omit() and
195  * ovsdb_idl_omit_alert() may be used to selectively drop some columns from
196  * monitoring.
197  *
198  * If 'monitor_everything_by_default' is false, then no columns or tables will
199  * be replicated by default.  ovsdb_idl_add_column() and ovsdb_idl_add_table()
200  * must be used to choose some columns or tables to replicate.
201  */
202 struct ovsdb_idl *
203 ovsdb_idl_create(const char *remote, const struct ovsdb_idl_class *class,
204                  bool monitor_everything_by_default, bool retry)
205 {
206     struct ovsdb_idl *idl;
207     uint8_t default_mode;
208     size_t i;
209
210     default_mode = (monitor_everything_by_default
211                     ? OVSDB_IDL_MONITOR | OVSDB_IDL_ALERT
212                     : 0);
213
214     idl = xzalloc(sizeof *idl);
215     idl->class = class;
216     idl->session = jsonrpc_session_open(remote, retry);
217     shash_init(&idl->table_by_name);
218     idl->tables = xmalloc(class->n_tables * sizeof *idl->tables);
219     for (i = 0; i < class->n_tables; i++) {
220         const struct ovsdb_idl_table_class *tc = &class->tables[i];
221         struct ovsdb_idl_table *table = &idl->tables[i];
222         size_t j;
223
224         shash_add_assert(&idl->table_by_name, tc->name, table);
225         table->class = tc;
226         table->modes = xmalloc(tc->n_columns);
227         memset(table->modes, default_mode, tc->n_columns);
228         table->need_table = false;
229         shash_init(&table->columns);
230         for (j = 0; j < tc->n_columns; j++) {
231             const struct ovsdb_idl_column *column = &tc->columns[j];
232
233             shash_add_assert(&table->columns, column->name, column);
234         }
235         hmap_init(&table->rows);
236         list_init(&table->track_list);
237         table->change_seqno[OVSDB_IDL_CHANGE_INSERT]
238             = table->change_seqno[OVSDB_IDL_CHANGE_MODIFY]
239             = table->change_seqno[OVSDB_IDL_CHANGE_DELETE] = 0;
240         table->idl = idl;
241     }
242
243     idl->state_seqno = UINT_MAX;
244     idl->request_id = NULL;
245
246     hmap_init(&idl->outstanding_txns);
247
248     return idl;
249 }
250
251 /* Destroys 'idl' and all of the data structures that it manages. */
252 void
253 ovsdb_idl_destroy(struct ovsdb_idl *idl)
254 {
255     if (idl) {
256         size_t i;
257
258         ovs_assert(!idl->txn);
259         ovsdb_idl_clear(idl);
260         jsonrpc_session_close(idl->session);
261
262         for (i = 0; i < idl->class->n_tables; i++) {
263             struct ovsdb_idl_table *table = &idl->tables[i];
264             shash_destroy(&table->columns);
265             hmap_destroy(&table->rows);
266             free(table->modes);
267         }
268         shash_destroy(&idl->table_by_name);
269         free(idl->tables);
270         json_destroy(idl->request_id);
271         free(idl->lock_name);
272         json_destroy(idl->lock_request_id);
273         hmap_destroy(&idl->outstanding_txns);
274         free(idl);
275     }
276 }
277
278 static void
279 ovsdb_idl_clear(struct ovsdb_idl *idl)
280 {
281     bool changed = false;
282     size_t i;
283
284     for (i = 0; i < idl->class->n_tables; i++) {
285         struct ovsdb_idl_table *table = &idl->tables[i];
286         struct ovsdb_idl_row *row, *next_row;
287
288         if (hmap_is_empty(&table->rows)) {
289             continue;
290         }
291
292         changed = true;
293         HMAP_FOR_EACH_SAFE (row, next_row, hmap_node, &table->rows) {
294             struct ovsdb_idl_arc *arc, *next_arc;
295
296             if (!ovsdb_idl_row_is_orphan(row)) {
297                 ovsdb_idl_row_unparse(row);
298             }
299             LIST_FOR_EACH_SAFE (arc, next_arc, src_node, &row->src_arcs) {
300                 free(arc);
301             }
302             /* No need to do anything with dst_arcs: some node has those arcs
303              * as forward arcs and will destroy them itself. */
304
305             if (!list_is_empty(&row->track_node)) {
306                 list_remove(&row->track_node);
307             }
308
309             ovsdb_idl_row_destroy(row);
310         }
311     }
312
313     ovsdb_idl_track_clear(idl);
314
315     if (changed) {
316         idl->change_seqno++;
317     }
318 }
319
320 /* Processes a batch of messages from the database server on 'idl'.  This may
321  * cause the IDL's contents to change.  The client may check for that with
322  * ovsdb_idl_get_seqno(). */
323 void
324 ovsdb_idl_run(struct ovsdb_idl *idl)
325 {
326     int i;
327
328     ovs_assert(!idl->txn);
329     jsonrpc_session_run(idl->session);
330     for (i = 0; jsonrpc_session_is_connected(idl->session) && i < 50; i++) {
331         struct jsonrpc_msg *msg;
332         unsigned int seqno;
333
334         seqno = jsonrpc_session_get_seqno(idl->session);
335         if (idl->state_seqno != seqno) {
336             idl->state_seqno = seqno;
337             json_destroy(idl->request_id);
338             idl->request_id = NULL;
339             ovsdb_idl_txn_abort_all(idl);
340
341             ovsdb_idl_send_schema_request(idl);
342             idl->state = IDL_S_SCHEMA_REQUESTED;
343             if (idl->lock_name) {
344                 ovsdb_idl_send_lock_request(idl);
345             }
346         }
347
348         msg = jsonrpc_session_recv(idl->session);
349         if (!msg) {
350             break;
351         }
352
353         if (msg->type == JSONRPC_NOTIFY
354             && !strcmp(msg->method, "update")
355             && msg->params->type == JSON_ARRAY
356             && msg->params->u.array.n == 2
357             && msg->params->u.array.elems[0]->type == JSON_NULL) {
358             /* Database contents changed. */
359             ovsdb_idl_parse_update(idl, msg->params->u.array.elems[1]);
360         } else if (msg->type == JSONRPC_REPLY
361                    && idl->request_id
362                    && json_equal(idl->request_id, msg->id)) {
363             switch (idl->state) {
364             case IDL_S_SCHEMA_REQUESTED:
365                 /* Reply to our "get_schema" request. */
366                 json_destroy(idl->request_id);
367                 idl->request_id = NULL;
368                 ovsdb_idl_send_monitor_request(idl, msg->result);
369                 idl->state = IDL_S_MONITOR_REQUESTED;
370                 break;
371
372             case IDL_S_MONITOR_REQUESTED:
373                 /* Reply to our "monitor" request. */
374                 idl->change_seqno++;
375                 json_destroy(idl->request_id);
376                 idl->request_id = NULL;
377                 idl->state = IDL_S_MONITORING;
378                 ovsdb_idl_clear(idl);
379                 ovsdb_idl_parse_update(idl, msg->result);
380                 break;
381
382             case IDL_S_MONITORING:
383             default:
384                 OVS_NOT_REACHED();
385             }
386         } else if (msg->type == JSONRPC_REPLY
387                    && idl->lock_request_id
388                    && json_equal(idl->lock_request_id, msg->id)) {
389             /* Reply to our "lock" request. */
390             ovsdb_idl_parse_lock_reply(idl, msg->result);
391         } else if (msg->type == JSONRPC_NOTIFY
392                    && !strcmp(msg->method, "locked")) {
393             /* We got our lock. */
394             ovsdb_idl_parse_lock_notify(idl, msg->params, true);
395         } else if (msg->type == JSONRPC_NOTIFY
396                    && !strcmp(msg->method, "stolen")) {
397             /* Someone else stole our lock. */
398             ovsdb_idl_parse_lock_notify(idl, msg->params, false);
399         } else if ((msg->type == JSONRPC_ERROR
400                     || msg->type == JSONRPC_REPLY)
401                    && ovsdb_idl_txn_process_reply(idl, msg)) {
402             /* ovsdb_idl_txn_process_reply() did everything needful. */
403         } else {
404             /* This can happen if ovsdb_idl_txn_destroy() is called to destroy
405              * a transaction before we receive the reply, so keep the log level
406              * low. */
407             VLOG_DBG("%s: received unexpected %s message",
408                      jsonrpc_session_get_name(idl->session),
409                      jsonrpc_msg_type_to_string(msg->type));
410         }
411         jsonrpc_msg_destroy(msg);
412     }
413     ovsdb_idl_row_destroy_postprocess(idl);
414 }
415
416 /* Arranges for poll_block() to wake up when ovsdb_idl_run() has something to
417  * do or when activity occurs on a transaction on 'idl'. */
418 void
419 ovsdb_idl_wait(struct ovsdb_idl *idl)
420 {
421     jsonrpc_session_wait(idl->session);
422     jsonrpc_session_recv_wait(idl->session);
423 }
424
425 /* Returns a "sequence number" that represents the state of 'idl'.  When
426  * ovsdb_idl_run() changes the database, the sequence number changes.  The
427  * initial fetch of the entire contents of the remote database is considered to
428  * be one kind of change.  Successfully acquiring a lock, if one has been
429  * configured with ovsdb_idl_set_lock(), is also considered to be a change.
430  *
431  * As long as the sequence number does not change, the client may continue to
432  * use any data structures it obtains from 'idl'.  But when it changes, the
433  * client must not access any of these data structures again, because they
434  * could have freed or reused for other purposes.
435  *
436  * The sequence number can occasionally change even if the database does not.
437  * This happens if the connection to the database drops and reconnects, which
438  * causes the database contents to be reloaded even if they didn't change.  (It
439  * could also happen if the database server sends out a "change" that reflects
440  * what the IDL already thought was in the database.  The database server is
441  * not supposed to do that, but bugs could in theory cause it to do so.) */
442 unsigned int
443 ovsdb_idl_get_seqno(const struct ovsdb_idl *idl)
444 {
445     return idl->change_seqno;
446 }
447
448 /* Returns true if 'idl' successfully connected to the remote database and
449  * retrieved its contents (even if the connection subsequently dropped and is
450  * in the process of reconnecting).  If so, then 'idl' contains an atomic
451  * snapshot of the database's contents (but it might be arbitrarily old if the
452  * connection dropped).
453  *
454  * Returns false if 'idl' has never connected or retrieved the database's
455  * contents.  If so, 'idl' is empty. */
456 bool
457 ovsdb_idl_has_ever_connected(const struct ovsdb_idl *idl)
458 {
459     return ovsdb_idl_get_seqno(idl) != 0;
460 }
461
462 /* Reconfigures 'idl' so that it would reconnect to the database, if
463  * connection was dropped. */
464 void
465 ovsdb_idl_enable_reconnect(struct ovsdb_idl *idl)
466 {
467     jsonrpc_session_enable_reconnect(idl->session);
468 }
469
470 /* Forces 'idl' to drop its connection to the database and reconnect.  In the
471  * meantime, the contents of 'idl' will not change. */
472 void
473 ovsdb_idl_force_reconnect(struct ovsdb_idl *idl)
474 {
475     jsonrpc_session_force_reconnect(idl->session);
476 }
477
478 /* Some IDL users should only write to write-only columns.  Furthermore,
479  * writing to a column which is not write-only can cause serious performance
480  * degradations for these users.  This function causes 'idl' to reject writes
481  * to columns which are not marked write only using ovsdb_idl_omit_alert(). */
482 void
483 ovsdb_idl_verify_write_only(struct ovsdb_idl *idl)
484 {
485     idl->verify_write_only = true;
486 }
487
488 /* Returns true if 'idl' is currently connected or trying to connect. */
489 bool
490 ovsdb_idl_is_alive(const struct ovsdb_idl *idl)
491 {
492     return jsonrpc_session_is_alive(idl->session);
493 }
494
495 /* Returns the last error reported on a connection by 'idl'.  The return value
496  * is 0 only if no connection made by 'idl' has ever encountered an error.  See
497  * jsonrpc_get_status() for return value interpretation. */
498 int
499 ovsdb_idl_get_last_error(const struct ovsdb_idl *idl)
500 {
501     return jsonrpc_session_get_last_error(idl->session);
502 }
503 \f
504 static unsigned char *
505 ovsdb_idl_get_mode(struct ovsdb_idl *idl,
506                    const struct ovsdb_idl_column *column)
507 {
508     size_t i;
509
510     ovs_assert(!idl->change_seqno);
511
512     for (i = 0; i < idl->class->n_tables; i++) {
513         const struct ovsdb_idl_table *table = &idl->tables[i];
514         const struct ovsdb_idl_table_class *tc = table->class;
515
516         if (column >= tc->columns && column < &tc->columns[tc->n_columns]) {
517             return &table->modes[column - tc->columns];
518         }
519     }
520
521     OVS_NOT_REACHED();
522 }
523
524 static void
525 add_ref_table(struct ovsdb_idl *idl, const struct ovsdb_base_type *base)
526 {
527     if (base->type == OVSDB_TYPE_UUID && base->u.uuid.refTableName) {
528         struct ovsdb_idl_table *table;
529
530         table = shash_find_data(&idl->table_by_name,
531                                 base->u.uuid.refTableName);
532         if (table) {
533             table->need_table = true;
534         } else {
535             VLOG_WARN("%s IDL class missing referenced table %s",
536                       idl->class->database, base->u.uuid.refTableName);
537         }
538     }
539 }
540
541 /* Turns on OVSDB_IDL_MONITOR and OVSDB_IDL_ALERT for 'column' in 'idl'.  Also
542  * ensures that any tables referenced by 'column' will be replicated, even if
543  * no columns in that table are selected for replication (see
544  * ovsdb_idl_add_table() for more information).
545  *
546  * This function is only useful if 'monitor_everything_by_default' was false in
547  * the call to ovsdb_idl_create().  This function should be called between
548  * ovsdb_idl_create() and the first call to ovsdb_idl_run().
549  */
550 void
551 ovsdb_idl_add_column(struct ovsdb_idl *idl,
552                      const struct ovsdb_idl_column *column)
553 {
554     *ovsdb_idl_get_mode(idl, column) = OVSDB_IDL_MONITOR | OVSDB_IDL_ALERT;
555     add_ref_table(idl, &column->type.key);
556     add_ref_table(idl, &column->type.value);
557 }
558
559 /* Ensures that the table with class 'tc' will be replicated on 'idl' even if
560  * no columns are selected for replication. Just the necessary data for table
561  * references will be replicated (the UUID of the rows, for instance), any
562  * columns not selected for replication will remain unreplicated.
563  * This can be useful because it allows 'idl' to keep track of what rows in the
564  * table actually exist, which in turn allows columns that reference the table
565  * to have accurate contents. (The IDL presents the database with references to
566  * rows that do not exist removed.)
567  *
568  * This function is only useful if 'monitor_everything_by_default' was false in
569  * the call to ovsdb_idl_create().  This function should be called between
570  * ovsdb_idl_create() and the first call to ovsdb_idl_run().
571  */
572 void
573 ovsdb_idl_add_table(struct ovsdb_idl *idl,
574                     const struct ovsdb_idl_table_class *tc)
575 {
576     size_t i;
577
578     for (i = 0; i < idl->class->n_tables; i++) {
579         struct ovsdb_idl_table *table = &idl->tables[i];
580
581         if (table->class == tc) {
582             table->need_table = true;
583             return;
584         }
585     }
586
587     OVS_NOT_REACHED();
588 }
589
590 /* Turns off OVSDB_IDL_ALERT for 'column' in 'idl'.
591  *
592  * This function should be called between ovsdb_idl_create() and the first call
593  * to ovsdb_idl_run().
594  */
595 void
596 ovsdb_idl_omit_alert(struct ovsdb_idl *idl,
597                      const struct ovsdb_idl_column *column)
598 {
599     *ovsdb_idl_get_mode(idl, column) &= ~OVSDB_IDL_ALERT;
600 }
601
602 /* Sets the mode for 'column' in 'idl' to 0.  See the big comment above
603  * OVSDB_IDL_MONITOR for details.
604  *
605  * This function should be called between ovsdb_idl_create() and the first call
606  * to ovsdb_idl_run().
607  */
608 void
609 ovsdb_idl_omit(struct ovsdb_idl *idl, const struct ovsdb_idl_column *column)
610 {
611     *ovsdb_idl_get_mode(idl, column) = 0;
612 }
613
614 /* Returns the most recent IDL change sequence number that caused a
615  * insert, modify or delete update to the table with class 'table_class'.
616  */
617 unsigned int
618 ovsdb_idl_table_get_seqno(const struct ovsdb_idl *idl,
619                           const struct ovsdb_idl_table_class *table_class)
620 {
621     struct ovsdb_idl_table *table
622         = ovsdb_idl_table_from_class(idl, table_class);
623     unsigned int max_seqno = table->change_seqno[OVSDB_IDL_CHANGE_INSERT];
624
625     if (max_seqno < table->change_seqno[OVSDB_IDL_CHANGE_MODIFY]) {
626         max_seqno = table->change_seqno[OVSDB_IDL_CHANGE_MODIFY];
627     }
628     if (max_seqno < table->change_seqno[OVSDB_IDL_CHANGE_DELETE]) {
629         max_seqno = table->change_seqno[OVSDB_IDL_CHANGE_DELETE];
630     }
631     return max_seqno;
632 }
633
634 /* For each row that contains tracked columns, IDL stores the most
635  * recent IDL change sequence numbers associateed with insert, modify
636  * and delete updates to the table.
637  */
638 unsigned int
639 ovsdb_idl_row_get_seqno(const struct ovsdb_idl_row *row,
640                         enum ovsdb_idl_change change)
641 {
642     return row->change_seqno[change];
643 }
644
645 /* Turns on OVSDB_IDL_TRACK for 'column' in 'idl', ensuring that
646  * all rows whose 'column' is modified are traced. Similarly, insert
647  * or delete of rows having 'column' are tracked. Clients are able
648  * to retrive the tracked rows with the ovsdb_idl_track_get_*()
649  * functions.
650  *
651  * This function should be called between ovsdb_idl_create() and
652  * the first call to ovsdb_idl_run(). The column to be tracked
653  * should have OVSDB_IDL_ALERT turned on.
654  */
655 void
656 ovsdb_idl_track_add_column(struct ovsdb_idl *idl,
657                            const struct ovsdb_idl_column *column)
658 {
659     if (!(*ovsdb_idl_get_mode(idl, column) & OVSDB_IDL_ALERT)) {
660         ovsdb_idl_add_column(idl, column);
661     }
662     *ovsdb_idl_get_mode(idl, column) |= OVSDB_IDL_TRACK;
663 }
664
665 void
666 ovsdb_idl_track_add_all(struct ovsdb_idl *idl)
667 {
668     size_t i, j;
669
670     for (i = 0; i < idl->class->n_tables; i++) {
671         const struct ovsdb_idl_table_class *tc = &idl->class->tables[i];
672
673         for (j = 0; j < tc->n_columns; j++) {
674             const struct ovsdb_idl_column *column = &tc->columns[j];
675             ovsdb_idl_track_add_column(idl, column);
676         }
677     }
678 }
679
680 /* Returns true if 'table' has any tracked column. */
681 static bool
682 ovsdb_idl_track_is_set(struct ovsdb_idl_table *table)
683 {
684     size_t i;
685
686     for (i = 0; i < table->class->n_columns; i++) {
687         if (table->modes[i] & OVSDB_IDL_TRACK) {
688             return true;
689         }
690     }
691    return false;
692 }
693
694 /* Returns the first tracked row in table with class 'table_class'
695  * for the specified 'idl'. Returns NULL if there are no tracked rows */
696 const struct ovsdb_idl_row *
697 ovsdb_idl_track_get_first(const struct ovsdb_idl *idl,
698                           const struct ovsdb_idl_table_class *table_class)
699 {
700     struct ovsdb_idl_table *table
701         = ovsdb_idl_table_from_class(idl, table_class);
702
703     if (!list_is_empty(&table->track_list)) {
704         return CONTAINER_OF(list_front(&table->track_list), struct ovsdb_idl_row, track_node);
705     }
706     return NULL;
707 }
708
709 /* Returns the next tracked row in table after the specified 'row'
710  * (in no particular order). Returns NULL if there are no tracked rows */
711 const struct ovsdb_idl_row *
712 ovsdb_idl_track_get_next(const struct ovsdb_idl_row *row)
713 {
714     if (row->track_node.next != &row->table->track_list) {
715         return CONTAINER_OF(row->track_node.next, struct ovsdb_idl_row, track_node);
716     }
717
718     return NULL;
719 }
720
721 /* Flushes the tracked rows. Client calls this function after calling
722  * ovsdb_idl_run() and read all tracked rows with the ovsdb_idl_track_get_*()
723  * functions. This is usually done at the end of the client's processing
724  * loop when it is ready to do ovsdb_idl_run() again.
725  */
726 void
727 ovsdb_idl_track_clear(const struct ovsdb_idl *idl)
728 {
729     size_t i;
730
731     for (i = 0; i < idl->class->n_tables; i++) {
732         struct ovsdb_idl_table *table = &idl->tables[i];
733
734         if (!list_is_empty(&table->track_list)) {
735             struct ovsdb_idl_row *row, *next;
736
737             LIST_FOR_EACH_SAFE(row, next, track_node, &table->track_list) {
738                 list_remove(&row->track_node);
739                 list_init(&row->track_node);
740                 if (ovsdb_idl_row_is_orphan(row)) {
741                     ovsdb_idl_row_clear_old(row);
742                     free(row);
743                 }
744             }
745         }
746     }
747 }
748
749 \f
750 static void
751 ovsdb_idl_send_schema_request(struct ovsdb_idl *idl)
752 {
753     struct jsonrpc_msg *msg;
754
755     json_destroy(idl->request_id);
756     msg = jsonrpc_create_request(
757         "get_schema",
758         json_array_create_1(json_string_create(idl->class->database)),
759         &idl->request_id);
760     jsonrpc_session_send(idl->session, msg);
761 }
762
763 static void
764 log_error(struct ovsdb_error *error)
765 {
766     char *s = ovsdb_error_to_string(error);
767     VLOG_WARN("error parsing database schema: %s", s);
768     free(s);
769     ovsdb_error_destroy(error);
770 }
771
772 /* Frees 'schema', which is in the format returned by parse_schema(). */
773 static void
774 free_schema(struct shash *schema)
775 {
776     if (schema) {
777         struct shash_node *node, *next;
778
779         SHASH_FOR_EACH_SAFE (node, next, schema) {
780             struct sset *sset = node->data;
781             sset_destroy(sset);
782             free(sset);
783             shash_delete(schema, node);
784         }
785         shash_destroy(schema);
786         free(schema);
787     }
788 }
789
790 /* Parses 'schema_json', an OVSDB schema in JSON format as described in RFC
791  * 7047, to obtain the names of its rows and columns.  If successful, returns
792  * an shash whose keys are table names and whose values are ssets, where each
793  * sset contains the names of its table's columns.  On failure (due to a parse
794  * error), returns NULL.
795  *
796  * It would also be possible to use the general-purpose OVSDB schema parser in
797  * ovsdb-server, but that's overkill, possibly too strict for the current use
798  * case, and would require restructuring ovsdb-server to separate the schema
799  * code from the rest. */
800 static struct shash *
801 parse_schema(const struct json *schema_json)
802 {
803     struct ovsdb_parser parser;
804     const struct json *tables_json;
805     struct ovsdb_error *error;
806     struct shash_node *node;
807     struct shash *schema;
808
809     ovsdb_parser_init(&parser, schema_json, "database schema");
810     tables_json = ovsdb_parser_member(&parser, "tables", OP_OBJECT);
811     error = ovsdb_parser_destroy(&parser);
812     if (error) {
813         log_error(error);
814         return NULL;
815     }
816
817     schema = xmalloc(sizeof *schema);
818     shash_init(schema);
819     SHASH_FOR_EACH (node, json_object(tables_json)) {
820         const char *table_name = node->name;
821         const struct json *json = node->data;
822         const struct json *columns_json;
823
824         ovsdb_parser_init(&parser, json, "table schema for table %s",
825                           table_name);
826         columns_json = ovsdb_parser_member(&parser, "columns", OP_OBJECT);
827         error = ovsdb_parser_destroy(&parser);
828         if (error) {
829             log_error(error);
830             free_schema(schema);
831             return NULL;
832         }
833
834         struct sset *columns = xmalloc(sizeof *columns);
835         sset_init(columns);
836
837         struct shash_node *node2;
838         SHASH_FOR_EACH (node2, json_object(columns_json)) {
839             const char *column_name = node2->name;
840             sset_add(columns, column_name);
841         }
842         shash_add(schema, table_name, columns);
843     }
844     return schema;
845 }
846
847 static void
848 ovsdb_idl_send_monitor_request(struct ovsdb_idl *idl,
849                                const struct json *schema_json)
850 {
851     struct shash *schema = parse_schema(schema_json);
852     struct json *monitor_requests;
853     struct jsonrpc_msg *msg;
854     size_t i;
855
856     monitor_requests = json_object_create();
857     for (i = 0; i < idl->class->n_tables; i++) {
858         const struct ovsdb_idl_table *table = &idl->tables[i];
859         const struct ovsdb_idl_table_class *tc = table->class;
860         struct json *monitor_request, *columns;
861         const struct sset *table_schema;
862         size_t j;
863
864         table_schema = (schema
865                         ? shash_find_data(schema, table->class->name)
866                         : NULL);
867
868         columns = table->need_table ? json_array_create_empty() : NULL;
869         for (j = 0; j < tc->n_columns; j++) {
870             const struct ovsdb_idl_column *column = &tc->columns[j];
871             if (table->modes[j] & OVSDB_IDL_MONITOR) {
872                 if (table_schema
873                     && !sset_contains(table_schema, column->name)) {
874                     VLOG_WARN("%s table in %s database lacks %s column "
875                               "(database needs upgrade?)",
876                               table->class->name, idl->class->database,
877                               column->name);
878                     continue;
879                 }
880                 if (!columns) {
881                     columns = json_array_create_empty();
882                 }
883                 json_array_add(columns, json_string_create(column->name));
884             }
885         }
886
887         if (columns) {
888             if (schema && !table_schema) {
889                 VLOG_WARN("%s database lacks %s table "
890                           "(database needs upgrade?)",
891                           idl->class->database, table->class->name);
892                 json_destroy(columns);
893                 continue;
894             }
895
896             monitor_request = json_object_create();
897             json_object_put(monitor_request, "columns", columns);
898             json_object_put(monitor_requests, tc->name, monitor_request);
899         }
900     }
901     free_schema(schema);
902
903     json_destroy(idl->request_id);
904     msg = jsonrpc_create_request(
905         "monitor",
906         json_array_create_3(json_string_create(idl->class->database),
907                             json_null_create(), monitor_requests),
908         &idl->request_id);
909     jsonrpc_session_send(idl->session, msg);
910 }
911
912 static void
913 ovsdb_idl_parse_update(struct ovsdb_idl *idl, const struct json *table_updates)
914 {
915     struct ovsdb_error *error = ovsdb_idl_parse_update__(idl, table_updates);
916     if (error) {
917         if (!VLOG_DROP_WARN(&syntax_rl)) {
918             char *s = ovsdb_error_to_string(error);
919             VLOG_WARN_RL(&syntax_rl, "%s", s);
920             free(s);
921         }
922         ovsdb_error_destroy(error);
923     }
924 }
925
926 static struct ovsdb_error *
927 ovsdb_idl_parse_update__(struct ovsdb_idl *idl,
928                          const struct json *table_updates)
929 {
930     const struct shash_node *tables_node;
931
932     if (table_updates->type != JSON_OBJECT) {
933         return ovsdb_syntax_error(table_updates, NULL,
934                                   "<table-updates> is not an object");
935     }
936     SHASH_FOR_EACH (tables_node, json_object(table_updates)) {
937         const struct json *table_update = tables_node->data;
938         const struct shash_node *table_node;
939         struct ovsdb_idl_table *table;
940
941         table = shash_find_data(&idl->table_by_name, tables_node->name);
942         if (!table) {
943             return ovsdb_syntax_error(
944                 table_updates, NULL,
945                 "<table-updates> includes unknown table \"%s\"",
946                 tables_node->name);
947         }
948
949         if (table_update->type != JSON_OBJECT) {
950             return ovsdb_syntax_error(table_update, NULL,
951                                       "<table-update> for table \"%s\" is "
952                                       "not an object", table->class->name);
953         }
954         SHASH_FOR_EACH (table_node, json_object(table_update)) {
955             const struct json *row_update = table_node->data;
956             const struct json *old_json, *new_json;
957             struct uuid uuid;
958
959             if (!uuid_from_string(&uuid, table_node->name)) {
960                 return ovsdb_syntax_error(table_update, NULL,
961                                           "<table-update> for table \"%s\" "
962                                           "contains bad UUID "
963                                           "\"%s\" as member name",
964                                           table->class->name,
965                                           table_node->name);
966             }
967             if (row_update->type != JSON_OBJECT) {
968                 return ovsdb_syntax_error(row_update, NULL,
969                                           "<table-update> for table \"%s\" "
970                                           "contains <row-update> for %s that "
971                                           "is not an object",
972                                           table->class->name,
973                                           table_node->name);
974             }
975
976             old_json = shash_find_data(json_object(row_update), "old");
977             new_json = shash_find_data(json_object(row_update), "new");
978             if (old_json && old_json->type != JSON_OBJECT) {
979                 return ovsdb_syntax_error(old_json, NULL,
980                                           "\"old\" <row> is not object");
981             } else if (new_json && new_json->type != JSON_OBJECT) {
982                 return ovsdb_syntax_error(new_json, NULL,
983                                           "\"new\" <row> is not object");
984             } else if ((old_json != NULL) + (new_json != NULL)
985                        != shash_count(json_object(row_update))) {
986                 return ovsdb_syntax_error(row_update, NULL,
987                                           "<row-update> contains unexpected "
988                                           "member");
989             } else if (!old_json && !new_json) {
990                 return ovsdb_syntax_error(row_update, NULL,
991                                           "<row-update> missing \"old\" "
992                                           "and \"new\" members");
993             }
994
995             if (ovsdb_idl_process_update(table, &uuid, old_json, new_json)) {
996                 idl->change_seqno++;
997             }
998         }
999     }
1000
1001     return NULL;
1002 }
1003
1004 static struct ovsdb_idl_row *
1005 ovsdb_idl_get_row(struct ovsdb_idl_table *table, const struct uuid *uuid)
1006 {
1007     struct ovsdb_idl_row *row;
1008
1009     HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid), &table->rows) {
1010         if (uuid_equals(&row->uuid, uuid)) {
1011             return row;
1012         }
1013     }
1014     return NULL;
1015 }
1016
1017 /* Returns true if a column with mode OVSDB_IDL_MODE_RW changed, false
1018  * otherwise. */
1019 static bool
1020 ovsdb_idl_process_update(struct ovsdb_idl_table *table,
1021                          const struct uuid *uuid, const struct json *old,
1022                          const struct json *new)
1023 {
1024     struct ovsdb_idl_row *row;
1025
1026     row = ovsdb_idl_get_row(table, uuid);
1027     if (!new) {
1028         /* Delete row. */
1029         if (row && !ovsdb_idl_row_is_orphan(row)) {
1030             /* XXX perhaps we should check the 'old' values? */
1031             ovsdb_idl_delete_row(row);
1032         } else {
1033             VLOG_WARN_RL(&semantic_rl, "cannot delete missing row "UUID_FMT" "
1034                          "from table %s",
1035                          UUID_ARGS(uuid), table->class->name);
1036             return false;
1037         }
1038     } else if (!old) {
1039         /* Insert row. */
1040         if (!row) {
1041             ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
1042         } else if (ovsdb_idl_row_is_orphan(row)) {
1043             ovsdb_idl_insert_row(row, new);
1044         } else {
1045             VLOG_WARN_RL(&semantic_rl, "cannot add existing row "UUID_FMT" to "
1046                          "table %s", UUID_ARGS(uuid), table->class->name);
1047             return ovsdb_idl_modify_row(row, new);
1048         }
1049     } else {
1050         /* Modify row. */
1051         if (row) {
1052             /* XXX perhaps we should check the 'old' values? */
1053             if (!ovsdb_idl_row_is_orphan(row)) {
1054                 return ovsdb_idl_modify_row(row, new);
1055             } else {
1056                 VLOG_WARN_RL(&semantic_rl, "cannot modify missing but "
1057                              "referenced row "UUID_FMT" in table %s",
1058                              UUID_ARGS(uuid), table->class->name);
1059                 ovsdb_idl_insert_row(row, new);
1060             }
1061         } else {
1062             VLOG_WARN_RL(&semantic_rl, "cannot modify missing row "UUID_FMT" "
1063                          "in table %s", UUID_ARGS(uuid), table->class->name);
1064             ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
1065         }
1066     }
1067
1068     return true;
1069 }
1070
1071 /* Returns true if a column with mode OVSDB_IDL_MODE_RW changed, false
1072  * otherwise. */
1073 static bool
1074 ovsdb_idl_row_update(struct ovsdb_idl_row *row, const struct json *row_json,
1075                      enum ovsdb_idl_change change)
1076 {
1077     struct ovsdb_idl_table *table = row->table;
1078     struct shash_node *node;
1079     bool changed = false;
1080
1081     SHASH_FOR_EACH (node, json_object(row_json)) {
1082         const char *column_name = node->name;
1083         const struct ovsdb_idl_column *column;
1084         struct ovsdb_datum datum;
1085         struct ovsdb_error *error;
1086
1087         column = shash_find_data(&table->columns, column_name);
1088         if (!column) {
1089             VLOG_WARN_RL(&syntax_rl, "unknown column %s updating row "UUID_FMT,
1090                          column_name, UUID_ARGS(&row->uuid));
1091             continue;
1092         }
1093
1094         error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
1095         if (!error) {
1096             unsigned int column_idx = column - table->class->columns;
1097             struct ovsdb_datum *old = &row->old[column_idx];
1098
1099             if (!ovsdb_datum_equals(old, &datum, &column->type)) {
1100                 ovsdb_datum_swap(old, &datum);
1101                 if (table->modes[column_idx] & OVSDB_IDL_ALERT) {
1102                     changed = true;
1103                     row->change_seqno[change]
1104                         = row->table->change_seqno[change]
1105                         = row->table->idl->change_seqno + 1;
1106                     if (table->modes[column_idx] & OVSDB_IDL_TRACK) {
1107                         if (list_is_empty(&row->track_node)) {
1108                             list_push_front(&row->table->track_list,
1109                                             &row->track_node);
1110                         }
1111                     }
1112                 }
1113             } else {
1114                 /* Didn't really change but the OVSDB monitor protocol always
1115                  * includes every value in a row. */
1116             }
1117
1118             ovsdb_datum_destroy(&datum, &column->type);
1119         } else {
1120             char *s = ovsdb_error_to_string(error);
1121             VLOG_WARN_RL(&syntax_rl, "error parsing column %s in row "UUID_FMT
1122                          " in table %s: %s", column_name,
1123                          UUID_ARGS(&row->uuid), table->class->name, s);
1124             free(s);
1125             ovsdb_error_destroy(error);
1126         }
1127     }
1128     return changed;
1129 }
1130
1131 /* When a row A refers to row B through a column with a "refTable" constraint,
1132  * but row B does not exist, row B is called an "orphan row".  Orphan rows
1133  * should not persist, because the database enforces referential integrity, but
1134  * they can appear transiently as changes from the database are received (the
1135  * database doesn't try to topologically sort them and circular references mean
1136  * it isn't always possible anyhow).
1137  *
1138  * This function returns true if 'row' is an orphan row, otherwise false.
1139  */
1140 static bool
1141 ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *row)
1142 {
1143     return !row->old && !row->new;
1144 }
1145
1146 /* Returns true if 'row' is conceptually part of the database as modified by
1147  * the current transaction (if any), false otherwise.
1148  *
1149  * This function will return true if 'row' is not an orphan (see the comment on
1150  * ovsdb_idl_row_is_orphan()) and:
1151  *
1152  *   - 'row' exists in the database and has not been deleted within the
1153  *     current transaction (if any).
1154  *
1155  *   - 'row' was inserted within the current transaction and has not been
1156  *     deleted.  (In the latter case you should not have passed 'row' in at
1157  *     all, because ovsdb_idl_txn_delete() freed it.)
1158  *
1159  * This function will return false if 'row' is an orphan or if 'row' was
1160  * deleted within the current transaction.
1161  */
1162 static bool
1163 ovsdb_idl_row_exists(const struct ovsdb_idl_row *row)
1164 {
1165     return row->new != NULL;
1166 }
1167
1168 static void
1169 ovsdb_idl_row_parse(struct ovsdb_idl_row *row)
1170 {
1171     const struct ovsdb_idl_table_class *class = row->table->class;
1172     size_t i;
1173
1174     for (i = 0; i < class->n_columns; i++) {
1175         const struct ovsdb_idl_column *c = &class->columns[i];
1176         (c->parse)(row, &row->old[i]);
1177     }
1178 }
1179
1180 static void
1181 ovsdb_idl_row_unparse(struct ovsdb_idl_row *row)
1182 {
1183     const struct ovsdb_idl_table_class *class = row->table->class;
1184     size_t i;
1185
1186     for (i = 0; i < class->n_columns; i++) {
1187         const struct ovsdb_idl_column *c = &class->columns[i];
1188         (c->unparse)(row);
1189     }
1190 }
1191
1192 static void
1193 ovsdb_idl_row_clear_old(struct ovsdb_idl_row *row)
1194 {
1195     ovs_assert(row->old == row->new);
1196     if (!ovsdb_idl_row_is_orphan(row)) {
1197         const struct ovsdb_idl_table_class *class = row->table->class;
1198         size_t i;
1199
1200         for (i = 0; i < class->n_columns; i++) {
1201             ovsdb_datum_destroy(&row->old[i], &class->columns[i].type);
1202         }
1203         free(row->old);
1204         row->old = row->new = NULL;
1205     }
1206 }
1207
1208 static void
1209 ovsdb_idl_row_clear_new(struct ovsdb_idl_row *row)
1210 {
1211     if (row->old != row->new) {
1212         if (row->new) {
1213             const struct ovsdb_idl_table_class *class = row->table->class;
1214             size_t i;
1215
1216             if (row->written) {
1217                 BITMAP_FOR_EACH_1 (i, class->n_columns, row->written) {
1218                     ovsdb_datum_destroy(&row->new[i], &class->columns[i].type);
1219                 }
1220             }
1221             free(row->new);
1222             free(row->written);
1223             row->written = NULL;
1224         }
1225         row->new = row->old;
1226     }
1227 }
1228
1229 static void
1230 ovsdb_idl_row_clear_arcs(struct ovsdb_idl_row *row, bool destroy_dsts)
1231 {
1232     struct ovsdb_idl_arc *arc, *next;
1233
1234     /* Delete all forward arcs.  If 'destroy_dsts', destroy any orphaned rows
1235      * that this causes to be unreferenced, if tracking is not enabled.
1236      * If tracking is enabled, orphaned nodes are removed from hmap but not
1237      * freed.
1238      */
1239     LIST_FOR_EACH_SAFE (arc, next, src_node, &row->src_arcs) {
1240         list_remove(&arc->dst_node);
1241         if (destroy_dsts
1242             && ovsdb_idl_row_is_orphan(arc->dst)
1243             && list_is_empty(&arc->dst->dst_arcs)) {
1244             ovsdb_idl_row_destroy(arc->dst);
1245         }
1246         free(arc);
1247     }
1248     list_init(&row->src_arcs);
1249 }
1250
1251 /* Force nodes that reference 'row' to reparse. */
1252 static void
1253 ovsdb_idl_row_reparse_backrefs(struct ovsdb_idl_row *row)
1254 {
1255     struct ovsdb_idl_arc *arc, *next;
1256
1257     /* This is trickier than it looks.  ovsdb_idl_row_clear_arcs() will destroy
1258      * 'arc', so we need to use the "safe" variant of list traversal.  However,
1259      * calling an ovsdb_idl_column's 'parse' function will add an arc
1260      * equivalent to 'arc' to row->arcs.  That could be a problem for
1261      * traversal, but it adds it at the beginning of the list to prevent us
1262      * from stumbling upon it again.
1263      *
1264      * (If duplicate arcs were possible then we would need to make sure that
1265      * 'next' didn't also point into 'arc''s destination, but we forbid
1266      * duplicate arcs.) */
1267     LIST_FOR_EACH_SAFE (arc, next, dst_node, &row->dst_arcs) {
1268         struct ovsdb_idl_row *ref = arc->src;
1269
1270         ovsdb_idl_row_unparse(ref);
1271         ovsdb_idl_row_clear_arcs(ref, false);
1272         ovsdb_idl_row_parse(ref);
1273     }
1274 }
1275
1276 static struct ovsdb_idl_row *
1277 ovsdb_idl_row_create__(const struct ovsdb_idl_table_class *class)
1278 {
1279     struct ovsdb_idl_row *row = xzalloc(class->allocation_size);
1280     class->row_init(row);
1281     list_init(&row->src_arcs);
1282     list_init(&row->dst_arcs);
1283     hmap_node_nullify(&row->txn_node);
1284     list_init(&row->track_node);
1285     return row;
1286 }
1287
1288 static struct ovsdb_idl_row *
1289 ovsdb_idl_row_create(struct ovsdb_idl_table *table, const struct uuid *uuid)
1290 {
1291     struct ovsdb_idl_row *row = ovsdb_idl_row_create__(table->class);
1292     hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
1293     row->uuid = *uuid;
1294     row->table = table;
1295     return row;
1296 }
1297
1298 static void
1299 ovsdb_idl_row_destroy(struct ovsdb_idl_row *row)
1300 {
1301     if (row) {
1302         ovsdb_idl_row_clear_old(row);
1303         hmap_remove(&row->table->rows, &row->hmap_node);
1304         if (ovsdb_idl_track_is_set(row->table)) {
1305             row->change_seqno[OVSDB_IDL_CHANGE_DELETE]
1306                 = row->table->change_seqno[OVSDB_IDL_CHANGE_DELETE]
1307                 = row->table->idl->change_seqno + 1;
1308         }
1309         if (list_is_empty(&row->track_node)) {
1310             list_push_front(&row->table->track_list, &row->track_node);
1311         }
1312     }
1313 }
1314
1315 static void
1316 ovsdb_idl_row_destroy_postprocess(struct ovsdb_idl *idl)
1317 {
1318     size_t i;
1319
1320     for (i = 0; i < idl->class->n_tables; i++) {
1321         struct ovsdb_idl_table *table = &idl->tables[i];
1322
1323         if (!list_is_empty(&table->track_list)) {
1324             struct ovsdb_idl_row *row, *next;
1325
1326             LIST_FOR_EACH_SAFE(row, next, track_node, &table->track_list) {
1327                 if (!ovsdb_idl_track_is_set(row->table)) {
1328                     list_remove(&row->track_node);
1329                     free(row);
1330                 }
1331             }
1332         }
1333     }
1334 }
1335
1336 static void
1337 ovsdb_idl_insert_row(struct ovsdb_idl_row *row, const struct json *row_json)
1338 {
1339     const struct ovsdb_idl_table_class *class = row->table->class;
1340     size_t i;
1341
1342     ovs_assert(!row->old && !row->new);
1343     row->old = row->new = xmalloc(class->n_columns * sizeof *row->old);
1344     for (i = 0; i < class->n_columns; i++) {
1345         ovsdb_datum_init_default(&row->old[i], &class->columns[i].type);
1346     }
1347     ovsdb_idl_row_update(row, row_json, OVSDB_IDL_CHANGE_INSERT);
1348     ovsdb_idl_row_parse(row);
1349
1350     ovsdb_idl_row_reparse_backrefs(row);
1351 }
1352
1353 static void
1354 ovsdb_idl_delete_row(struct ovsdb_idl_row *row)
1355 {
1356     ovsdb_idl_row_unparse(row);
1357     ovsdb_idl_row_clear_arcs(row, true);
1358     ovsdb_idl_row_clear_old(row);
1359     if (list_is_empty(&row->dst_arcs)) {
1360         ovsdb_idl_row_destroy(row);
1361     } else {
1362         ovsdb_idl_row_reparse_backrefs(row);
1363     }
1364 }
1365
1366 /* Returns true if a column with mode OVSDB_IDL_MODE_RW changed, false
1367  * otherwise. */
1368 static bool
1369 ovsdb_idl_modify_row(struct ovsdb_idl_row *row, const struct json *row_json)
1370 {
1371     bool changed;
1372
1373     ovsdb_idl_row_unparse(row);
1374     ovsdb_idl_row_clear_arcs(row, true);
1375     changed = ovsdb_idl_row_update(row, row_json, OVSDB_IDL_CHANGE_MODIFY);
1376     ovsdb_idl_row_parse(row);
1377
1378     return changed;
1379 }
1380
1381 static bool
1382 may_add_arc(const struct ovsdb_idl_row *src, const struct ovsdb_idl_row *dst)
1383 {
1384     const struct ovsdb_idl_arc *arc;
1385
1386     /* No self-arcs. */
1387     if (src == dst) {
1388         return false;
1389     }
1390
1391     /* No duplicate arcs.
1392      *
1393      * We only need to test whether the first arc in dst->dst_arcs originates
1394      * at 'src', since we add all of the arcs from a given source in a clump
1395      * (in a single call to ovsdb_idl_row_parse()) and new arcs are always
1396      * added at the front of the dst_arcs list. */
1397     if (list_is_empty(&dst->dst_arcs)) {
1398         return true;
1399     }
1400     arc = CONTAINER_OF(dst->dst_arcs.next, struct ovsdb_idl_arc, dst_node);
1401     return arc->src != src;
1402 }
1403
1404 static struct ovsdb_idl_table *
1405 ovsdb_idl_table_from_class(const struct ovsdb_idl *idl,
1406                            const struct ovsdb_idl_table_class *table_class)
1407 {
1408     return &idl->tables[table_class - idl->class->tables];
1409 }
1410
1411 /* Called by ovsdb-idlc generated code. */
1412 struct ovsdb_idl_row *
1413 ovsdb_idl_get_row_arc(struct ovsdb_idl_row *src,
1414                       struct ovsdb_idl_table_class *dst_table_class,
1415                       const struct uuid *dst_uuid)
1416 {
1417     struct ovsdb_idl *idl = src->table->idl;
1418     struct ovsdb_idl_table *dst_table;
1419     struct ovsdb_idl_arc *arc;
1420     struct ovsdb_idl_row *dst;
1421
1422     dst_table = ovsdb_idl_table_from_class(idl, dst_table_class);
1423     dst = ovsdb_idl_get_row(dst_table, dst_uuid);
1424     if (idl->txn) {
1425         /* We're being called from ovsdb_idl_txn_write().  We must not update
1426          * any arcs, because the transaction will be backed out at commit or
1427          * abort time and we don't want our graph screwed up.
1428          *
1429          * Just return the destination row, if there is one and it has not been
1430          * deleted. */
1431         if (dst && (hmap_node_is_null(&dst->txn_node) || dst->new)) {
1432             return dst;
1433         }
1434         return NULL;
1435     } else {
1436         /* We're being called from some other context.  Update the graph. */
1437         if (!dst) {
1438             dst = ovsdb_idl_row_create(dst_table, dst_uuid);
1439         }
1440
1441         /* Add a new arc, if it wouldn't be a self-arc or a duplicate arc. */
1442         if (may_add_arc(src, dst)) {
1443             /* The arc *must* be added at the front of the dst_arcs list.  See
1444              * ovsdb_idl_row_reparse_backrefs() for details. */
1445             arc = xmalloc(sizeof *arc);
1446             list_push_front(&src->src_arcs, &arc->src_node);
1447             list_push_front(&dst->dst_arcs, &arc->dst_node);
1448             arc->src = src;
1449             arc->dst = dst;
1450         }
1451
1452         return !ovsdb_idl_row_is_orphan(dst) ? dst : NULL;
1453     }
1454 }
1455
1456 /* Searches 'tc''s table in 'idl' for a row with UUID 'uuid'.  Returns a
1457  * pointer to the row if there is one, otherwise a null pointer.  */
1458 const struct ovsdb_idl_row *
1459 ovsdb_idl_get_row_for_uuid(const struct ovsdb_idl *idl,
1460                            const struct ovsdb_idl_table_class *tc,
1461                            const struct uuid *uuid)
1462 {
1463     return ovsdb_idl_get_row(ovsdb_idl_table_from_class(idl, tc), uuid);
1464 }
1465
1466 static struct ovsdb_idl_row *
1467 next_real_row(struct ovsdb_idl_table *table, struct hmap_node *node)
1468 {
1469     for (; node; node = hmap_next(&table->rows, node)) {
1470         struct ovsdb_idl_row *row;
1471
1472         row = CONTAINER_OF(node, struct ovsdb_idl_row, hmap_node);
1473         if (ovsdb_idl_row_exists(row)) {
1474             return row;
1475         }
1476     }
1477     return NULL;
1478 }
1479
1480 /* Returns a row in 'table_class''s table in 'idl', or a null pointer if that
1481  * table is empty.
1482  *
1483  * Database tables are internally maintained as hash tables, so adding or
1484  * removing rows while traversing the same table can cause some rows to be
1485  * visited twice or not at apply. */
1486 const struct ovsdb_idl_row *
1487 ovsdb_idl_first_row(const struct ovsdb_idl *idl,
1488                     const struct ovsdb_idl_table_class *table_class)
1489 {
1490     struct ovsdb_idl_table *table
1491         = ovsdb_idl_table_from_class(idl, table_class);
1492     return next_real_row(table, hmap_first(&table->rows));
1493 }
1494
1495 /* Returns a row following 'row' within its table, or a null pointer if 'row'
1496  * is the last row in its table. */
1497 const struct ovsdb_idl_row *
1498 ovsdb_idl_next_row(const struct ovsdb_idl_row *row)
1499 {
1500     struct ovsdb_idl_table *table = row->table;
1501
1502     return next_real_row(table, hmap_next(&table->rows, &row->hmap_node));
1503 }
1504
1505 /* Reads and returns the value of 'column' within 'row'.  If an ongoing
1506  * transaction has changed 'column''s value, the modified value is returned.
1507  *
1508  * The caller must not modify or free the returned value.
1509  *
1510  * Various kinds of changes can invalidate the returned value: writing to the
1511  * same 'column' in 'row' (e.g. with ovsdb_idl_txn_write()), deleting 'row'
1512  * (e.g. with ovsdb_idl_txn_delete()), or completing an ongoing transaction
1513  * (e.g. with ovsdb_idl_txn_commit() or ovsdb_idl_txn_abort()).  If the
1514  * returned value is needed for a long time, it is best to make a copy of it
1515  * with ovsdb_datum_clone(). */
1516 const struct ovsdb_datum *
1517 ovsdb_idl_read(const struct ovsdb_idl_row *row,
1518                const struct ovsdb_idl_column *column)
1519 {
1520     const struct ovsdb_idl_table_class *class;
1521     size_t column_idx;
1522
1523     ovs_assert(!ovsdb_idl_row_is_synthetic(row));
1524
1525     class = row->table->class;
1526     column_idx = column - class->columns;
1527
1528     ovs_assert(row->new != NULL);
1529     ovs_assert(column_idx < class->n_columns);
1530
1531     if (row->written && bitmap_is_set(row->written, column_idx)) {
1532         return &row->new[column_idx];
1533     } else if (row->old) {
1534         return &row->old[column_idx];
1535     } else {
1536         return ovsdb_datum_default(&column->type);
1537     }
1538 }
1539
1540 /* Same as ovsdb_idl_read(), except that it also asserts that 'column' has key
1541  * type 'key_type' and value type 'value_type'.  (Scalar and set types will
1542  * have a value type of OVSDB_TYPE_VOID.)
1543  *
1544  * This is useful in code that "knows" that a particular column has a given
1545  * type, so that it will abort if someone changes the column's type without
1546  * updating the code that uses it. */
1547 const struct ovsdb_datum *
1548 ovsdb_idl_get(const struct ovsdb_idl_row *row,
1549               const struct ovsdb_idl_column *column,
1550               enum ovsdb_atomic_type key_type OVS_UNUSED,
1551               enum ovsdb_atomic_type value_type OVS_UNUSED)
1552 {
1553     ovs_assert(column->type.key.type == key_type);
1554     ovs_assert(column->type.value.type == value_type);
1555
1556     return ovsdb_idl_read(row, column);
1557 }
1558
1559 /* Returns true if the field represented by 'column' in 'row' may be modified,
1560  * false if it is immutable.
1561  *
1562  * Normally, whether a field is mutable is controlled by its column's schema.
1563  * However, an immutable column can be set to any initial value at the time of
1564  * insertion, so if 'row' is a new row (one that is being added as part of the
1565  * current transaction, supposing that a transaction is in progress) then even
1566  * its "immutable" fields are actually mutable. */
1567 bool
1568 ovsdb_idl_is_mutable(const struct ovsdb_idl_row *row,
1569                      const struct ovsdb_idl_column *column)
1570 {
1571     return column->mutable || (row->new && !row->old);
1572 }
1573
1574 /* Returns false if 'row' was obtained from the IDL, true if it was initialized
1575  * to all-zero-bits by some other entity.  If 'row' was set up some other way
1576  * then the return value is indeterminate. */
1577 bool
1578 ovsdb_idl_row_is_synthetic(const struct ovsdb_idl_row *row)
1579 {
1580     return row->table == NULL;
1581 }
1582 \f
1583 /* Transactions. */
1584
1585 static void ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
1586                                    enum ovsdb_idl_txn_status);
1587
1588 /* Returns a string representation of 'status'.  The caller must not modify or
1589  * free the returned string.
1590  *
1591  * The return value is probably useful only for debug log messages and unit
1592  * tests. */
1593 const char *
1594 ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status status)
1595 {
1596     switch (status) {
1597     case TXN_UNCOMMITTED:
1598         return "uncommitted";
1599     case TXN_UNCHANGED:
1600         return "unchanged";
1601     case TXN_INCOMPLETE:
1602         return "incomplete";
1603     case TXN_ABORTED:
1604         return "aborted";
1605     case TXN_SUCCESS:
1606         return "success";
1607     case TXN_TRY_AGAIN:
1608         return "try again";
1609     case TXN_NOT_LOCKED:
1610         return "not locked";
1611     case TXN_ERROR:
1612         return "error";
1613     }
1614     return "<unknown>";
1615 }
1616
1617 /* Starts a new transaction on 'idl'.  A given ovsdb_idl may only have a single
1618  * active transaction at a time.  See the large comment in ovsdb-idl.h for
1619  * general information on transactions. */
1620 struct ovsdb_idl_txn *
1621 ovsdb_idl_txn_create(struct ovsdb_idl *idl)
1622 {
1623     struct ovsdb_idl_txn *txn;
1624
1625     ovs_assert(!idl->txn);
1626     idl->txn = txn = xmalloc(sizeof *txn);
1627     txn->request_id = NULL;
1628     txn->idl = idl;
1629     hmap_init(&txn->txn_rows);
1630     txn->status = TXN_UNCOMMITTED;
1631     txn->error = NULL;
1632     txn->dry_run = false;
1633     ds_init(&txn->comment);
1634
1635     txn->inc_table = NULL;
1636     txn->inc_column = NULL;
1637
1638     hmap_init(&txn->inserted_rows);
1639
1640     return txn;
1641 }
1642
1643 /* Appends 's', which is treated as a printf()-type format string, to the
1644  * comments that will be passed to the OVSDB server when 'txn' is committed.
1645  * (The comment will be committed to the OVSDB log, which "ovsdb-tool
1646  * show-log" can print in a relatively human-readable form.) */
1647 void
1648 ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *txn, const char *s, ...)
1649 {
1650     va_list args;
1651
1652     if (txn->comment.length) {
1653         ds_put_char(&txn->comment, '\n');
1654     }
1655
1656     va_start(args, s);
1657     ds_put_format_valist(&txn->comment, s, args);
1658     va_end(args);
1659 }
1660
1661 /* Marks 'txn' as a transaction that will not actually modify the database.  In
1662  * almost every way, the transaction is treated like other transactions.  It
1663  * must be committed or aborted like other transactions, it will be sent to the
1664  * database server like other transactions, and so on.  The only difference is
1665  * that the operations sent to the database server will include, as the last
1666  * step, an "abort" operation, so that any changes made by the transaction will
1667  * not actually take effect. */
1668 void
1669 ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *txn)
1670 {
1671     txn->dry_run = true;
1672 }
1673
1674 /* Causes 'txn', when committed, to increment the value of 'column' within
1675  * 'row' by 1.  'column' must have an integer type.  After 'txn' commits
1676  * successfully, the client may retrieve the final (incremented) value of
1677  * 'column' with ovsdb_idl_txn_get_increment_new_value().
1678  *
1679  * The client could accomplish something similar with ovsdb_idl_read(),
1680  * ovsdb_idl_txn_verify() and ovsdb_idl_txn_write(), or with ovsdb-idlc
1681  * generated wrappers for these functions.  However, ovsdb_idl_txn_increment()
1682  * will never (by itself) fail because of a verify error.
1683  *
1684  * The intended use is for incrementing the "next_cfg" column in the
1685  * Open_vSwitch table. */
1686 void
1687 ovsdb_idl_txn_increment(struct ovsdb_idl_txn *txn,
1688                         const struct ovsdb_idl_row *row,
1689                         const struct ovsdb_idl_column *column)
1690 {
1691     ovs_assert(!txn->inc_table);
1692     ovs_assert(column->type.key.type == OVSDB_TYPE_INTEGER);
1693     ovs_assert(column->type.value.type == OVSDB_TYPE_VOID);
1694
1695     txn->inc_table = row->table->class->name;
1696     txn->inc_column = column->name;
1697     txn->inc_row = row->uuid;
1698 }
1699
1700 /* Destroys 'txn' and frees all associated memory.  If ovsdb_idl_txn_commit()
1701  * has been called for 'txn' but the commit is still incomplete (that is, the
1702  * last call returned TXN_INCOMPLETE) then the transaction may or may not still
1703  * end up committing at the database server, but the client will not be able to
1704  * get any further status information back. */
1705 void
1706 ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *txn)
1707 {
1708     struct ovsdb_idl_txn_insert *insert, *next;
1709
1710     json_destroy(txn->request_id);
1711     if (txn->status == TXN_INCOMPLETE) {
1712         hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
1713     }
1714     ovsdb_idl_txn_abort(txn);
1715     ds_destroy(&txn->comment);
1716     free(txn->error);
1717     HMAP_FOR_EACH_SAFE (insert, next, hmap_node, &txn->inserted_rows) {
1718         free(insert);
1719     }
1720     hmap_destroy(&txn->inserted_rows);
1721     free(txn);
1722 }
1723
1724 /* Causes poll_block() to wake up if 'txn' has completed committing. */
1725 void
1726 ovsdb_idl_txn_wait(const struct ovsdb_idl_txn *txn)
1727 {
1728     if (txn->status != TXN_UNCOMMITTED && txn->status != TXN_INCOMPLETE) {
1729         poll_immediate_wake();
1730     }
1731 }
1732
1733 static struct json *
1734 where_uuid_equals(const struct uuid *uuid)
1735 {
1736     return
1737         json_array_create_1(
1738             json_array_create_3(
1739                 json_string_create("_uuid"),
1740                 json_string_create("=="),
1741                 json_array_create_2(
1742                     json_string_create("uuid"),
1743                     json_string_create_nocopy(
1744                         xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
1745 }
1746
1747 static char *
1748 uuid_name_from_uuid(const struct uuid *uuid)
1749 {
1750     char *name;
1751     char *p;
1752
1753     name = xasprintf("row"UUID_FMT, UUID_ARGS(uuid));
1754     for (p = name; *p != '\0'; p++) {
1755         if (*p == '-') {
1756             *p = '_';
1757         }
1758     }
1759
1760     return name;
1761 }
1762
1763 static const struct ovsdb_idl_row *
1764 ovsdb_idl_txn_get_row(const struct ovsdb_idl_txn *txn, const struct uuid *uuid)
1765 {
1766     const struct ovsdb_idl_row *row;
1767
1768     HMAP_FOR_EACH_WITH_HASH (row, txn_node, uuid_hash(uuid), &txn->txn_rows) {
1769         if (uuid_equals(&row->uuid, uuid)) {
1770             return row;
1771         }
1772     }
1773     return NULL;
1774 }
1775
1776 /* XXX there must be a cleaner way to do this */
1777 static struct json *
1778 substitute_uuids(struct json *json, const struct ovsdb_idl_txn *txn)
1779 {
1780     if (json->type == JSON_ARRAY) {
1781         struct uuid uuid;
1782         size_t i;
1783
1784         if (json->u.array.n == 2
1785             && json->u.array.elems[0]->type == JSON_STRING
1786             && json->u.array.elems[1]->type == JSON_STRING
1787             && !strcmp(json->u.array.elems[0]->u.string, "uuid")
1788             && uuid_from_string(&uuid, json->u.array.elems[1]->u.string)) {
1789             const struct ovsdb_idl_row *row;
1790
1791             row = ovsdb_idl_txn_get_row(txn, &uuid);
1792             if (row && !row->old && row->new) {
1793                 json_destroy(json);
1794
1795                 return json_array_create_2(
1796                     json_string_create("named-uuid"),
1797                     json_string_create_nocopy(uuid_name_from_uuid(&uuid)));
1798             }
1799         }
1800
1801         for (i = 0; i < json->u.array.n; i++) {
1802             json->u.array.elems[i] = substitute_uuids(json->u.array.elems[i],
1803                                                       txn);
1804         }
1805     } else if (json->type == JSON_OBJECT) {
1806         struct shash_node *node;
1807
1808         SHASH_FOR_EACH (node, json_object(json)) {
1809             node->data = substitute_uuids(node->data, txn);
1810         }
1811     }
1812     return json;
1813 }
1814
1815 static void
1816 ovsdb_idl_txn_disassemble(struct ovsdb_idl_txn *txn)
1817 {
1818     struct ovsdb_idl_row *row, *next;
1819
1820     /* This must happen early.  Otherwise, ovsdb_idl_row_parse() will call an
1821      * ovsdb_idl_column's 'parse' function, which will call
1822      * ovsdb_idl_get_row_arc(), which will seen that the IDL is in a
1823      * transaction and fail to update the graph.  */
1824     txn->idl->txn = NULL;
1825
1826     HMAP_FOR_EACH_SAFE (row, next, txn_node, &txn->txn_rows) {
1827         if (row->old) {
1828             if (row->written) {
1829                 ovsdb_idl_row_unparse(row);
1830                 ovsdb_idl_row_clear_arcs(row, false);
1831                 ovsdb_idl_row_parse(row);
1832             }
1833         } else {
1834             ovsdb_idl_row_unparse(row);
1835         }
1836         ovsdb_idl_row_clear_new(row);
1837
1838         free(row->prereqs);
1839         row->prereqs = NULL;
1840
1841         free(row->written);
1842         row->written = NULL;
1843
1844         hmap_remove(&txn->txn_rows, &row->txn_node);
1845         hmap_node_nullify(&row->txn_node);
1846         if (!row->old) {
1847             hmap_remove(&row->table->rows, &row->hmap_node);
1848             free(row);
1849         }
1850     }
1851     hmap_destroy(&txn->txn_rows);
1852     hmap_init(&txn->txn_rows);
1853 }
1854
1855 /* Attempts to commit 'txn'.  Returns the status of the commit operation, one
1856  * of the following TXN_* constants:
1857  *
1858  *   TXN_INCOMPLETE:
1859  *
1860  *       The transaction is in progress, but not yet complete.  The caller
1861  *       should call again later, after calling ovsdb_idl_run() to let the IDL
1862  *       do OVSDB protocol processing.
1863  *
1864  *   TXN_UNCHANGED:
1865  *
1866  *       The transaction is complete.  (It didn't actually change the database,
1867  *       so the IDL didn't send any request to the database server.)
1868  *
1869  *   TXN_ABORTED:
1870  *
1871  *       The caller previously called ovsdb_idl_txn_abort().
1872  *
1873  *   TXN_SUCCESS:
1874  *
1875  *       The transaction was successful.  The update made by the transaction
1876  *       (and possibly other changes made by other database clients) should
1877  *       already be visible in the IDL.
1878  *
1879  *   TXN_TRY_AGAIN:
1880  *
1881  *       The transaction failed for some transient reason, e.g. because a
1882  *       "verify" operation reported an inconsistency or due to a network
1883  *       problem.  The caller should wait for a change to the database, then
1884  *       compose a new transaction, and commit the new transaction.
1885  *
1886  *       Use the return value of ovsdb_idl_get_seqno() to wait for a change in
1887  *       the database.  It is important to use its return value *before* the
1888  *       initial call to ovsdb_idl_txn_commit() as the baseline for this
1889  *       purpose, because the change that one should wait for can happen after
1890  *       the initial call but before the call that returns TXN_TRY_AGAIN, and
1891  *       using some other baseline value in that situation could cause an
1892  *       indefinite wait if the database rarely changes.
1893  *
1894  *   TXN_NOT_LOCKED:
1895  *
1896  *       The transaction failed because the IDL has been configured to require
1897  *       a database lock (with ovsdb_idl_set_lock()) but didn't get it yet or
1898  *       has already lost it.
1899  *
1900  * Committing a transaction rolls back all of the changes that it made to the
1901  * IDL's copy of the database.  If the transaction commits successfully, then
1902  * the database server will send an update and, thus, the IDL will be updated
1903  * with the committed changes. */
1904 enum ovsdb_idl_txn_status
1905 ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
1906 {
1907     struct ovsdb_idl_row *row;
1908     struct json *operations;
1909     bool any_updates;
1910
1911     if (txn != txn->idl->txn) {
1912         goto coverage_out;
1913     }
1914
1915     /* If we need a lock but don't have it, give up quickly. */
1916     if (txn->idl->lock_name && !ovsdb_idl_has_lock(txn->idl)) {
1917         txn->status = TXN_NOT_LOCKED;
1918         goto disassemble_out;
1919     }
1920
1921     operations = json_array_create_1(
1922         json_string_create(txn->idl->class->database));
1923
1924     /* Assert that we have the required lock (avoiding a race). */
1925     if (txn->idl->lock_name) {
1926         struct json *op = json_object_create();
1927         json_array_add(operations, op);
1928         json_object_put_string(op, "op", "assert");
1929         json_object_put_string(op, "lock", txn->idl->lock_name);
1930     }
1931
1932     /* Add prerequisites and declarations of new rows. */
1933     HMAP_FOR_EACH (row, txn_node, &txn->txn_rows) {
1934         /* XXX check that deleted rows exist even if no prereqs? */
1935         if (row->prereqs) {
1936             const struct ovsdb_idl_table_class *class = row->table->class;
1937             size_t n_columns = class->n_columns;
1938             struct json *op, *columns, *row_json;
1939             size_t idx;
1940
1941             op = json_object_create();
1942             json_array_add(operations, op);
1943             json_object_put_string(op, "op", "wait");
1944             json_object_put_string(op, "table", class->name);
1945             json_object_put(op, "timeout", json_integer_create(0));
1946             json_object_put(op, "where", where_uuid_equals(&row->uuid));
1947             json_object_put_string(op, "until", "==");
1948             columns = json_array_create_empty();
1949             json_object_put(op, "columns", columns);
1950             row_json = json_object_create();
1951             json_object_put(op, "rows", json_array_create_1(row_json));
1952
1953             BITMAP_FOR_EACH_1 (idx, n_columns, row->prereqs) {
1954                 const struct ovsdb_idl_column *column = &class->columns[idx];
1955                 json_array_add(columns, json_string_create(column->name));
1956                 json_object_put(row_json, column->name,
1957                                 ovsdb_datum_to_json(&row->old[idx],
1958                                                     &column->type));
1959             }
1960         }
1961     }
1962
1963     /* Add updates. */
1964     any_updates = false;
1965     HMAP_FOR_EACH (row, txn_node, &txn->txn_rows) {
1966         const struct ovsdb_idl_table_class *class = row->table->class;
1967
1968         if (!row->new) {
1969             if (class->is_root) {
1970                 struct json *op = json_object_create();
1971                 json_object_put_string(op, "op", "delete");
1972                 json_object_put_string(op, "table", class->name);
1973                 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1974                 json_array_add(operations, op);
1975                 any_updates = true;
1976             } else {
1977                 /* Let ovsdb-server decide whether to really delete it. */
1978             }
1979         } else if (row->old != row->new) {
1980             struct json *row_json;
1981             struct json *op;
1982             size_t idx;
1983
1984             op = json_object_create();
1985             json_object_put_string(op, "op", row->old ? "update" : "insert");
1986             json_object_put_string(op, "table", class->name);
1987             if (row->old) {
1988                 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1989             } else {
1990                 struct ovsdb_idl_txn_insert *insert;
1991
1992                 any_updates = true;
1993
1994                 json_object_put(op, "uuid-name",
1995                                 json_string_create_nocopy(
1996                                     uuid_name_from_uuid(&row->uuid)));
1997
1998                 insert = xmalloc(sizeof *insert);
1999                 insert->dummy = row->uuid;
2000                 insert->op_index = operations->u.array.n - 1;
2001                 uuid_zero(&insert->real);
2002                 hmap_insert(&txn->inserted_rows, &insert->hmap_node,
2003                             uuid_hash(&insert->dummy));
2004             }
2005             row_json = json_object_create();
2006             json_object_put(op, "row", row_json);
2007
2008             if (row->written) {
2009                 BITMAP_FOR_EACH_1 (idx, class->n_columns, row->written) {
2010                     const struct ovsdb_idl_column *column =
2011                                                         &class->columns[idx];
2012
2013                     if (row->old
2014                         || !ovsdb_datum_is_default(&row->new[idx],
2015                                                   &column->type)) {
2016                         json_object_put(row_json, column->name,
2017                                         substitute_uuids(
2018                                             ovsdb_datum_to_json(&row->new[idx],
2019                                                                 &column->type),
2020                                             txn));
2021
2022                         /* If anything really changed, consider it an update.
2023                          * We can't suppress not-really-changed values earlier
2024                          * or transactions would become nonatomic (see the big
2025                          * comment inside ovsdb_idl_txn_write()). */
2026                         if (!any_updates && row->old &&
2027                             !ovsdb_datum_equals(&row->old[idx], &row->new[idx],
2028                                                 &column->type)) {
2029                             any_updates = true;
2030                         }
2031                     }
2032                 }
2033             }
2034
2035             if (!row->old || !shash_is_empty(json_object(row_json))) {
2036                 json_array_add(operations, op);
2037             } else {
2038                 json_destroy(op);
2039             }
2040         }
2041     }
2042
2043     /* Add increment. */
2044     if (txn->inc_table && any_updates) {
2045         struct json *op;
2046
2047         txn->inc_index = operations->u.array.n - 1;
2048
2049         op = json_object_create();
2050         json_object_put_string(op, "op", "mutate");
2051         json_object_put_string(op, "table", txn->inc_table);
2052         json_object_put(op, "where",
2053                         substitute_uuids(where_uuid_equals(&txn->inc_row),
2054                                          txn));
2055         json_object_put(op, "mutations",
2056                         json_array_create_1(
2057                             json_array_create_3(
2058                                 json_string_create(txn->inc_column),
2059                                 json_string_create("+="),
2060                                 json_integer_create(1))));
2061         json_array_add(operations, op);
2062
2063         op = json_object_create();
2064         json_object_put_string(op, "op", "select");
2065         json_object_put_string(op, "table", txn->inc_table);
2066         json_object_put(op, "where",
2067                         substitute_uuids(where_uuid_equals(&txn->inc_row),
2068                                          txn));
2069         json_object_put(op, "columns",
2070                         json_array_create_1(json_string_create(
2071                                                 txn->inc_column)));
2072         json_array_add(operations, op);
2073     }
2074
2075     if (txn->comment.length) {
2076         struct json *op = json_object_create();
2077         json_object_put_string(op, "op", "comment");
2078         json_object_put_string(op, "comment", ds_cstr(&txn->comment));
2079         json_array_add(operations, op);
2080     }
2081
2082     if (txn->dry_run) {
2083         struct json *op = json_object_create();
2084         json_object_put_string(op, "op", "abort");
2085         json_array_add(operations, op);
2086     }
2087
2088     if (!any_updates) {
2089         txn->status = TXN_UNCHANGED;
2090         json_destroy(operations);
2091     } else if (!jsonrpc_session_send(
2092                    txn->idl->session,
2093                    jsonrpc_create_request(
2094                        "transact", operations, &txn->request_id))) {
2095         hmap_insert(&txn->idl->outstanding_txns, &txn->hmap_node,
2096                     json_hash(txn->request_id, 0));
2097         txn->status = TXN_INCOMPLETE;
2098     } else {
2099         txn->status = TXN_TRY_AGAIN;
2100     }
2101
2102 disassemble_out:
2103     ovsdb_idl_txn_disassemble(txn);
2104 coverage_out:
2105     switch (txn->status) {
2106     case TXN_UNCOMMITTED:   COVERAGE_INC(txn_uncommitted);    break;
2107     case TXN_UNCHANGED:     COVERAGE_INC(txn_unchanged);      break;
2108     case TXN_INCOMPLETE:    COVERAGE_INC(txn_incomplete);     break;
2109     case TXN_ABORTED:       COVERAGE_INC(txn_aborted);        break;
2110     case TXN_SUCCESS:       COVERAGE_INC(txn_success);        break;
2111     case TXN_TRY_AGAIN:     COVERAGE_INC(txn_try_again);      break;
2112     case TXN_NOT_LOCKED:    COVERAGE_INC(txn_not_locked);     break;
2113     case TXN_ERROR:         COVERAGE_INC(txn_error);          break;
2114     }
2115
2116     return txn->status;
2117 }
2118
2119 /* Attempts to commit 'txn', blocking until the commit either succeeds or
2120  * fails.  Returns the final commit status, which may be any TXN_* value other
2121  * than TXN_INCOMPLETE.
2122  *
2123  * This function calls ovsdb_idl_run() on 'txn''s IDL, so it may cause the
2124  * return value of ovsdb_idl_get_seqno() to change. */
2125 enum ovsdb_idl_txn_status
2126 ovsdb_idl_txn_commit_block(struct ovsdb_idl_txn *txn)
2127 {
2128     enum ovsdb_idl_txn_status status;
2129
2130     fatal_signal_run();
2131     while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
2132         ovsdb_idl_run(txn->idl);
2133         ovsdb_idl_wait(txn->idl);
2134         ovsdb_idl_txn_wait(txn);
2135         poll_block();
2136     }
2137     return status;
2138 }
2139
2140 /* Returns the final (incremented) value of the column in 'txn' that was set to
2141  * be incremented by ovsdb_idl_txn_increment().  'txn' must have committed
2142  * successfully. */
2143 int64_t
2144 ovsdb_idl_txn_get_increment_new_value(const struct ovsdb_idl_txn *txn)
2145 {
2146     ovs_assert(txn->status == TXN_SUCCESS);
2147     return txn->inc_new_value;
2148 }
2149
2150 /* Aborts 'txn' without sending it to the database server.  This is effective
2151  * only if ovsdb_idl_txn_commit() has not yet been called for 'txn'.
2152  * Otherwise, it has no effect.
2153  *
2154  * Aborting a transaction doesn't free its memory.  Use
2155  * ovsdb_idl_txn_destroy() to do that. */
2156 void
2157 ovsdb_idl_txn_abort(struct ovsdb_idl_txn *txn)
2158 {
2159     ovsdb_idl_txn_disassemble(txn);
2160     if (txn->status == TXN_UNCOMMITTED || txn->status == TXN_INCOMPLETE) {
2161         txn->status = TXN_ABORTED;
2162     }
2163 }
2164
2165 /* Returns a string that reports the error status for 'txn'.  The caller must
2166  * not modify or free the returned string.  A call to ovsdb_idl_txn_destroy()
2167  * for 'txn' may free the returned string.
2168  *
2169  * The return value is ordinarily one of the strings that
2170  * ovsdb_idl_txn_status_to_string() would return, but if the transaction failed
2171  * due to an error reported by the database server, the return value is that
2172  * error. */
2173 const char *
2174 ovsdb_idl_txn_get_error(const struct ovsdb_idl_txn *txn)
2175 {
2176     if (txn->status != TXN_ERROR) {
2177         return ovsdb_idl_txn_status_to_string(txn->status);
2178     } else if (txn->error) {
2179         return txn->error;
2180     } else {
2181         return "no error details available";
2182     }
2183 }
2184
2185 static void
2186 ovsdb_idl_txn_set_error_json(struct ovsdb_idl_txn *txn,
2187                              const struct json *json)
2188 {
2189     if (txn->error == NULL) {
2190         txn->error = json_to_string(json, JSSF_SORT);
2191     }
2192 }
2193
2194 /* For transaction 'txn' that completed successfully, finds and returns the
2195  * permanent UUID that the database assigned to a newly inserted row, given the
2196  * 'uuid' that ovsdb_idl_txn_insert() assigned locally to that row.
2197  *
2198  * Returns NULL if 'uuid' is not a UUID assigned by ovsdb_idl_txn_insert() or
2199  * if it was assigned by that function and then deleted by
2200  * ovsdb_idl_txn_delete() within the same transaction.  (Rows that are inserted
2201  * and then deleted within a single transaction are never sent to the database
2202  * server, so it never assigns them a permanent UUID.) */
2203 const struct uuid *
2204 ovsdb_idl_txn_get_insert_uuid(const struct ovsdb_idl_txn *txn,
2205                               const struct uuid *uuid)
2206 {
2207     const struct ovsdb_idl_txn_insert *insert;
2208
2209     ovs_assert(txn->status == TXN_SUCCESS || txn->status == TXN_UNCHANGED);
2210     HMAP_FOR_EACH_IN_BUCKET (insert, hmap_node,
2211                              uuid_hash(uuid), &txn->inserted_rows) {
2212         if (uuid_equals(uuid, &insert->dummy)) {
2213             return &insert->real;
2214         }
2215     }
2216     return NULL;
2217 }
2218
2219 static void
2220 ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
2221                        enum ovsdb_idl_txn_status status)
2222 {
2223     txn->status = status;
2224     hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
2225 }
2226
2227 /* Writes 'datum' to the specified 'column' in 'row_'.  Updates both 'row_'
2228  * itself and the structs derived from it (e.g. the "struct ovsrec_*", for
2229  * ovs-vswitchd).
2230  *
2231  * 'datum' must have the correct type for its column.  The IDL does not check
2232  * that it meets schema constraints, but ovsdb-server will do so at commit time
2233  * so it had better be correct.
2234  *
2235  * A transaction must be in progress.  Replication of 'column' must not have
2236  * been disabled (by calling ovsdb_idl_omit()).
2237  *
2238  * Usually this function is used indirectly through one of the "set" functions
2239  * generated by ovsdb-idlc.
2240  *
2241  * Takes ownership of what 'datum' points to (and in some cases destroys that
2242  * data before returning) but makes a copy of 'datum' itself.  (Commonly
2243  * 'datum' is on the caller's stack.) */
2244 static void
2245 ovsdb_idl_txn_write__(const struct ovsdb_idl_row *row_,
2246                       const struct ovsdb_idl_column *column,
2247                       struct ovsdb_datum *datum, bool owns_datum)
2248 {
2249     struct ovsdb_idl_row *row = CONST_CAST(struct ovsdb_idl_row *, row_);
2250     const struct ovsdb_idl_table_class *class;
2251     size_t column_idx;
2252     bool write_only;
2253
2254     if (ovsdb_idl_row_is_synthetic(row)) {
2255         goto discard_datum;
2256     }
2257
2258     class = row->table->class;
2259     column_idx = column - class->columns;
2260     write_only = row->table->modes[column_idx] == OVSDB_IDL_MONITOR;
2261
2262     ovs_assert(row->new != NULL);
2263     ovs_assert(column_idx < class->n_columns);
2264     ovs_assert(row->old == NULL ||
2265                row->table->modes[column_idx] & OVSDB_IDL_MONITOR);
2266
2267     if (row->table->idl->verify_write_only && !write_only) {
2268         VLOG_ERR("Bug: Attempt to write to a read/write column (%s:%s) when"
2269                  " explicitly configured not to.", class->name, column->name);
2270         goto discard_datum;
2271     }
2272
2273     /* If this is a write-only column and the datum being written is the same
2274      * as the one already there, just skip the update entirely.  This is worth
2275      * optimizing because we have a lot of columns that get periodically
2276      * refreshed into the database but don't actually change that often.
2277      *
2278      * We don't do this for read/write columns because that would break
2279      * atomicity of transactions--some other client might have written a
2280      * different value in that column since we read it.  (But if a whole
2281      * transaction only does writes of existing values, without making any real
2282      * changes, we will drop the whole transaction later in
2283      * ovsdb_idl_txn_commit().) */
2284     if (write_only && ovsdb_datum_equals(ovsdb_idl_read(row, column),
2285                                          datum, &column->type)) {
2286         goto discard_datum;
2287     }
2288
2289     if (hmap_node_is_null(&row->txn_node)) {
2290         hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
2291                     uuid_hash(&row->uuid));
2292     }
2293     if (row->old == row->new) {
2294         row->new = xmalloc(class->n_columns * sizeof *row->new);
2295     }
2296     if (!row->written) {
2297         row->written = bitmap_allocate(class->n_columns);
2298     }
2299     if (bitmap_is_set(row->written, column_idx)) {
2300         ovsdb_datum_destroy(&row->new[column_idx], &column->type);
2301     } else {
2302         bitmap_set1(row->written, column_idx);
2303     }
2304     if (owns_datum) {
2305         row->new[column_idx] = *datum;
2306     } else {
2307         ovsdb_datum_clone(&row->new[column_idx], datum, &column->type);
2308     }
2309     (column->unparse)(row);
2310     (column->parse)(row, &row->new[column_idx]);
2311     return;
2312
2313 discard_datum:
2314     if (owns_datum) {
2315         ovsdb_datum_destroy(datum, &column->type);
2316     }
2317 }
2318
2319 void
2320 ovsdb_idl_txn_write(const struct ovsdb_idl_row *row,
2321                     const struct ovsdb_idl_column *column,
2322                     struct ovsdb_datum *datum)
2323 {
2324     ovsdb_idl_txn_write__(row, column, datum, true);
2325 }
2326
2327 void
2328 ovsdb_idl_txn_write_clone(const struct ovsdb_idl_row *row,
2329                           const struct ovsdb_idl_column *column,
2330                           const struct ovsdb_datum *datum)
2331 {
2332     ovsdb_idl_txn_write__(row, column,
2333                           CONST_CAST(struct ovsdb_datum *, datum), false);
2334 }
2335
2336 /* Causes the original contents of 'column' in 'row_' to be verified as a
2337  * prerequisite to completing the transaction.  That is, if 'column' in 'row_'
2338  * changed (or if 'row_' was deleted) between the time that the IDL originally
2339  * read its contents and the time that the transaction commits, then the
2340  * transaction aborts and ovsdb_idl_txn_commit() returns TXN_AGAIN_WAIT or
2341  * TXN_AGAIN_NOW (depending on whether the database change has already been
2342  * received).
2343  *
2344  * The intention is that, to ensure that no transaction commits based on dirty
2345  * reads, an application should call ovsdb_idl_txn_verify() on each data item
2346  * read as part of a read-modify-write operation.
2347  *
2348  * In some cases ovsdb_idl_txn_verify() reduces to a no-op, because the current
2349  * value of 'column' is already known:
2350  *
2351  *   - If 'row_' is a row created by the current transaction (returned by
2352  *     ovsdb_idl_txn_insert()).
2353  *
2354  *   - If 'column' has already been modified (with ovsdb_idl_txn_write())
2355  *     within the current transaction.
2356  *
2357  * Because of the latter property, always call ovsdb_idl_txn_verify() *before*
2358  * ovsdb_idl_txn_write() for a given read-modify-write.
2359  *
2360  * A transaction must be in progress.
2361  *
2362  * Usually this function is used indirectly through one of the "verify"
2363  * functions generated by ovsdb-idlc. */
2364 void
2365 ovsdb_idl_txn_verify(const struct ovsdb_idl_row *row_,
2366                      const struct ovsdb_idl_column *column)
2367 {
2368     struct ovsdb_idl_row *row = CONST_CAST(struct ovsdb_idl_row *, row_);
2369     const struct ovsdb_idl_table_class *class;
2370     size_t column_idx;
2371
2372     if (ovsdb_idl_row_is_synthetic(row)) {
2373         return;
2374     }
2375
2376     class = row->table->class;
2377     column_idx = column - class->columns;
2378
2379     ovs_assert(row->new != NULL);
2380     ovs_assert(row->old == NULL ||
2381                row->table->modes[column_idx] & OVSDB_IDL_MONITOR);
2382     if (!row->old
2383         || (row->written && bitmap_is_set(row->written, column_idx))) {
2384         return;
2385     }
2386
2387     if (hmap_node_is_null(&row->txn_node)) {
2388         hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
2389                     uuid_hash(&row->uuid));
2390     }
2391     if (!row->prereqs) {
2392         row->prereqs = bitmap_allocate(class->n_columns);
2393     }
2394     bitmap_set1(row->prereqs, column_idx);
2395 }
2396
2397 /* Deletes 'row_' from its table.  May free 'row_', so it must not be
2398  * accessed afterward.
2399  *
2400  * A transaction must be in progress.
2401  *
2402  * Usually this function is used indirectly through one of the "delete"
2403  * functions generated by ovsdb-idlc. */
2404 void
2405 ovsdb_idl_txn_delete(const struct ovsdb_idl_row *row_)
2406 {
2407     struct ovsdb_idl_row *row = CONST_CAST(struct ovsdb_idl_row *, row_);
2408
2409     if (ovsdb_idl_row_is_synthetic(row)) {
2410         return;
2411     }
2412
2413     ovs_assert(row->new != NULL);
2414     if (!row->old) {
2415         ovsdb_idl_row_unparse(row);
2416         ovsdb_idl_row_clear_new(row);
2417         ovs_assert(!row->prereqs);
2418         hmap_remove(&row->table->rows, &row->hmap_node);
2419         hmap_remove(&row->table->idl->txn->txn_rows, &row->txn_node);
2420         free(row);
2421         return;
2422     }
2423     if (hmap_node_is_null(&row->txn_node)) {
2424         hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
2425                     uuid_hash(&row->uuid));
2426     }
2427     ovsdb_idl_row_clear_new(row);
2428     row->new = NULL;
2429 }
2430
2431 /* Inserts and returns a new row in the table with the specified 'class' in the
2432  * database with open transaction 'txn'.
2433  *
2434  * The new row is assigned a provisional UUID.  If 'uuid' is null then one is
2435  * randomly generated; otherwise 'uuid' should specify a randomly generated
2436  * UUID not otherwise in use.  ovsdb-server will assign a different UUID when
2437  * 'txn' is committed, but the IDL will replace any uses of the provisional
2438  * UUID in the data to be to be committed by the UUID assigned by
2439  * ovsdb-server.
2440  *
2441  * Usually this function is used indirectly through one of the "insert"
2442  * functions generated by ovsdb-idlc. */
2443 const struct ovsdb_idl_row *
2444 ovsdb_idl_txn_insert(struct ovsdb_idl_txn *txn,
2445                      const struct ovsdb_idl_table_class *class,
2446                      const struct uuid *uuid)
2447 {
2448     struct ovsdb_idl_row *row = ovsdb_idl_row_create__(class);
2449
2450     if (uuid) {
2451         ovs_assert(!ovsdb_idl_txn_get_row(txn, uuid));
2452         row->uuid = *uuid;
2453     } else {
2454         uuid_generate(&row->uuid);
2455     }
2456
2457     row->table = ovsdb_idl_table_from_class(txn->idl, class);
2458     row->new = xmalloc(class->n_columns * sizeof *row->new);
2459     hmap_insert(&row->table->rows, &row->hmap_node, uuid_hash(&row->uuid));
2460     hmap_insert(&txn->txn_rows, &row->txn_node, uuid_hash(&row->uuid));
2461     return row;
2462 }
2463
2464 static void
2465 ovsdb_idl_txn_abort_all(struct ovsdb_idl *idl)
2466 {
2467     struct ovsdb_idl_txn *txn;
2468
2469     HMAP_FOR_EACH (txn, hmap_node, &idl->outstanding_txns) {
2470         ovsdb_idl_txn_complete(txn, TXN_TRY_AGAIN);
2471     }
2472 }
2473
2474 static struct ovsdb_idl_txn *
2475 ovsdb_idl_txn_find(struct ovsdb_idl *idl, const struct json *id)
2476 {
2477     struct ovsdb_idl_txn *txn;
2478
2479     HMAP_FOR_EACH_WITH_HASH (txn, hmap_node,
2480                              json_hash(id, 0), &idl->outstanding_txns) {
2481         if (json_equal(id, txn->request_id)) {
2482             return txn;
2483         }
2484     }
2485     return NULL;
2486 }
2487
2488 static bool
2489 check_json_type(const struct json *json, enum json_type type, const char *name)
2490 {
2491     if (!json) {
2492         VLOG_WARN_RL(&syntax_rl, "%s is missing", name);
2493         return false;
2494     } else if (json->type != type) {
2495         VLOG_WARN_RL(&syntax_rl, "%s is %s instead of %s",
2496                      name, json_type_to_string(json->type),
2497                      json_type_to_string(type));
2498         return false;
2499     } else {
2500         return true;
2501     }
2502 }
2503
2504 static bool
2505 ovsdb_idl_txn_process_inc_reply(struct ovsdb_idl_txn *txn,
2506                                 const struct json_array *results)
2507 {
2508     struct json *count, *rows, *row, *column;
2509     struct shash *mutate, *select;
2510
2511     if (txn->inc_index + 2 > results->n) {
2512         VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
2513                      "for increment (has %"PRIuSIZE", needs %u)",
2514                      results->n, txn->inc_index + 2);
2515         return false;
2516     }
2517
2518     /* We know that this is a JSON object because the loop in
2519      * ovsdb_idl_txn_process_reply() checked. */
2520     mutate = json_object(results->elems[txn->inc_index]);
2521     count = shash_find_data(mutate, "count");
2522     if (!check_json_type(count, JSON_INTEGER, "\"mutate\" reply \"count\"")) {
2523         return false;
2524     }
2525     if (count->u.integer != 1) {
2526         VLOG_WARN_RL(&syntax_rl,
2527                      "\"mutate\" reply \"count\" is %lld instead of 1",
2528                      count->u.integer);
2529         return false;
2530     }
2531
2532     select = json_object(results->elems[txn->inc_index + 1]);
2533     rows = shash_find_data(select, "rows");
2534     if (!check_json_type(rows, JSON_ARRAY, "\"select\" reply \"rows\"")) {
2535         return false;
2536     }
2537     if (rows->u.array.n != 1) {
2538         VLOG_WARN_RL(&syntax_rl, "\"select\" reply \"rows\" has %"PRIuSIZE" elements "
2539                      "instead of 1",
2540                      rows->u.array.n);
2541         return false;
2542     }
2543     row = rows->u.array.elems[0];
2544     if (!check_json_type(row, JSON_OBJECT, "\"select\" reply row")) {
2545         return false;
2546     }
2547     column = shash_find_data(json_object(row), txn->inc_column);
2548     if (!check_json_type(column, JSON_INTEGER,
2549                          "\"select\" reply inc column")) {
2550         return false;
2551     }
2552     txn->inc_new_value = column->u.integer;
2553     return true;
2554 }
2555
2556 static bool
2557 ovsdb_idl_txn_process_insert_reply(struct ovsdb_idl_txn_insert *insert,
2558                                    const struct json_array *results)
2559 {
2560     static const struct ovsdb_base_type uuid_type = OVSDB_BASE_UUID_INIT;
2561     struct ovsdb_error *error;
2562     struct json *json_uuid;
2563     union ovsdb_atom uuid;
2564     struct shash *reply;
2565
2566     if (insert->op_index >= results->n) {
2567         VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
2568                      "for insert (has %"PRIuSIZE", needs %u)",
2569                      results->n, insert->op_index);
2570         return false;
2571     }
2572
2573     /* We know that this is a JSON object because the loop in
2574      * ovsdb_idl_txn_process_reply() checked. */
2575     reply = json_object(results->elems[insert->op_index]);
2576     json_uuid = shash_find_data(reply, "uuid");
2577     if (!check_json_type(json_uuid, JSON_ARRAY, "\"insert\" reply \"uuid\"")) {
2578         return false;
2579     }
2580
2581     error = ovsdb_atom_from_json(&uuid, &uuid_type, json_uuid, NULL);
2582     if (error) {
2583         char *s = ovsdb_error_to_string(error);
2584         VLOG_WARN_RL(&syntax_rl, "\"insert\" reply \"uuid\" is not a JSON "
2585                      "UUID: %s", s);
2586         free(s);
2587         ovsdb_error_destroy(error);
2588         return false;
2589     }
2590
2591     insert->real = uuid.uuid;
2592
2593     return true;
2594 }
2595
2596 static bool
2597 ovsdb_idl_txn_process_reply(struct ovsdb_idl *idl,
2598                             const struct jsonrpc_msg *msg)
2599 {
2600     struct ovsdb_idl_txn *txn;
2601     enum ovsdb_idl_txn_status status;
2602
2603     txn = ovsdb_idl_txn_find(idl, msg->id);
2604     if (!txn) {
2605         return false;
2606     }
2607
2608     if (msg->type == JSONRPC_ERROR) {
2609         status = TXN_ERROR;
2610     } else if (msg->result->type != JSON_ARRAY) {
2611         VLOG_WARN_RL(&syntax_rl, "reply to \"transact\" is not JSON array");
2612         status = TXN_ERROR;
2613     } else {
2614         struct json_array *ops = &msg->result->u.array;
2615         int hard_errors = 0;
2616         int soft_errors = 0;
2617         int lock_errors = 0;
2618         size_t i;
2619
2620         for (i = 0; i < ops->n; i++) {
2621             struct json *op = ops->elems[i];
2622
2623             if (op->type == JSON_NULL) {
2624                 /* This isn't an error in itself but indicates that some prior
2625                  * operation failed, so make sure that we know about it. */
2626                 soft_errors++;
2627             } else if (op->type == JSON_OBJECT) {
2628                 struct json *error;
2629
2630                 error = shash_find_data(json_object(op), "error");
2631                 if (error) {
2632                     if (error->type == JSON_STRING) {
2633                         if (!strcmp(error->u.string, "timed out")) {
2634                             soft_errors++;
2635                         } else if (!strcmp(error->u.string, "not owner")) {
2636                             lock_errors++;
2637                         } else if (strcmp(error->u.string, "aborted")) {
2638                             hard_errors++;
2639                             ovsdb_idl_txn_set_error_json(txn, op);
2640                         }
2641                     } else {
2642                         hard_errors++;
2643                         ovsdb_idl_txn_set_error_json(txn, op);
2644                         VLOG_WARN_RL(&syntax_rl,
2645                                      "\"error\" in reply is not JSON string");
2646                     }
2647                 }
2648             } else {
2649                 hard_errors++;
2650                 ovsdb_idl_txn_set_error_json(txn, op);
2651                 VLOG_WARN_RL(&syntax_rl,
2652                              "operation reply is not JSON null or object");
2653             }
2654         }
2655
2656         if (!soft_errors && !hard_errors && !lock_errors) {
2657             struct ovsdb_idl_txn_insert *insert;
2658
2659             if (txn->inc_table && !ovsdb_idl_txn_process_inc_reply(txn, ops)) {
2660                 hard_errors++;
2661             }
2662
2663             HMAP_FOR_EACH (insert, hmap_node, &txn->inserted_rows) {
2664                 if (!ovsdb_idl_txn_process_insert_reply(insert, ops)) {
2665                     hard_errors++;
2666                 }
2667             }
2668         }
2669
2670         status = (hard_errors ? TXN_ERROR
2671                   : lock_errors ? TXN_NOT_LOCKED
2672                   : soft_errors ? TXN_TRY_AGAIN
2673                   : TXN_SUCCESS);
2674     }
2675
2676     ovsdb_idl_txn_complete(txn, status);
2677     return true;
2678 }
2679
2680 /* Returns the transaction currently active for 'row''s IDL.  A transaction
2681  * must currently be active. */
2682 struct ovsdb_idl_txn *
2683 ovsdb_idl_txn_get(const struct ovsdb_idl_row *row)
2684 {
2685     struct ovsdb_idl_txn *txn = row->table->idl->txn;
2686     ovs_assert(txn != NULL);
2687     return txn;
2688 }
2689
2690 /* Returns the IDL on which 'txn' acts. */
2691 struct ovsdb_idl *
2692 ovsdb_idl_txn_get_idl (struct ovsdb_idl_txn *txn)
2693 {
2694     return txn->idl;
2695 }
2696
2697 /* Blocks until 'idl' successfully connects to the remote database and
2698  * retrieves its contents. */
2699 void
2700 ovsdb_idl_get_initial_snapshot(struct ovsdb_idl *idl)
2701 {
2702     while (1) {
2703         ovsdb_idl_run(idl);
2704         if (ovsdb_idl_has_ever_connected(idl)) {
2705             return;
2706         }
2707         ovsdb_idl_wait(idl);
2708         poll_block();
2709     }
2710 }
2711 \f
2712 /* If 'lock_name' is nonnull, configures 'idl' to obtain the named lock from
2713  * the database server and to avoid modifying the database when the lock cannot
2714  * be acquired (that is, when another client has the same lock).
2715  *
2716  * If 'lock_name' is NULL, drops the locking requirement and releases the
2717  * lock. */
2718 void
2719 ovsdb_idl_set_lock(struct ovsdb_idl *idl, const char *lock_name)
2720 {
2721     ovs_assert(!idl->txn);
2722     ovs_assert(hmap_is_empty(&idl->outstanding_txns));
2723
2724     if (idl->lock_name && (!lock_name || strcmp(lock_name, idl->lock_name))) {
2725         /* Release previous lock. */
2726         ovsdb_idl_send_unlock_request(idl);
2727         free(idl->lock_name);
2728         idl->lock_name = NULL;
2729         idl->is_lock_contended = false;
2730     }
2731
2732     if (lock_name && !idl->lock_name) {
2733         /* Acquire new lock. */
2734         idl->lock_name = xstrdup(lock_name);
2735         ovsdb_idl_send_lock_request(idl);
2736     }
2737 }
2738
2739 /* Returns true if 'idl' is configured to obtain a lock and owns that lock.
2740  *
2741  * Locking and unlocking happens asynchronously from the database client's
2742  * point of view, so the information is only useful for optimization (e.g. if
2743  * the client doesn't have the lock then there's no point in trying to write to
2744  * the database). */
2745 bool
2746 ovsdb_idl_has_lock(const struct ovsdb_idl *idl)
2747 {
2748     return idl->has_lock;
2749 }
2750
2751 /* Returns true if 'idl' is configured to obtain a lock but the database server
2752  * has indicated that some other client already owns the requested lock. */
2753 bool
2754 ovsdb_idl_is_lock_contended(const struct ovsdb_idl *idl)
2755 {
2756     return idl->is_lock_contended;
2757 }
2758
2759 static void
2760 ovsdb_idl_update_has_lock(struct ovsdb_idl *idl, bool new_has_lock)
2761 {
2762     if (new_has_lock && !idl->has_lock) {
2763         if (idl->state == IDL_S_MONITORING) {
2764             idl->change_seqno++;
2765         } else {
2766             /* We're setting up a session, so don't signal that the database
2767              * changed.  Finalizing the session will increment change_seqno
2768              * anyhow. */
2769         }
2770         idl->is_lock_contended = false;
2771     }
2772     idl->has_lock = new_has_lock;
2773 }
2774
2775 static void
2776 ovsdb_idl_send_lock_request__(struct ovsdb_idl *idl, const char *method,
2777                               struct json **idp)
2778 {
2779     ovsdb_idl_update_has_lock(idl, false);
2780
2781     json_destroy(idl->lock_request_id);
2782     idl->lock_request_id = NULL;
2783
2784     if (jsonrpc_session_is_connected(idl->session)) {
2785         struct json *params;
2786
2787         params = json_array_create_1(json_string_create(idl->lock_name));
2788         jsonrpc_session_send(idl->session,
2789                              jsonrpc_create_request(method, params, idp));
2790     }
2791 }
2792
2793 static void
2794 ovsdb_idl_send_lock_request(struct ovsdb_idl *idl)
2795 {
2796     ovsdb_idl_send_lock_request__(idl, "lock", &idl->lock_request_id);
2797 }
2798
2799 static void
2800 ovsdb_idl_send_unlock_request(struct ovsdb_idl *idl)
2801 {
2802     ovsdb_idl_send_lock_request__(idl, "unlock", NULL);
2803 }
2804
2805 static void
2806 ovsdb_idl_parse_lock_reply(struct ovsdb_idl *idl, const struct json *result)
2807 {
2808     bool got_lock;
2809
2810     json_destroy(idl->lock_request_id);
2811     idl->lock_request_id = NULL;
2812
2813     if (result->type == JSON_OBJECT) {
2814         const struct json *locked;
2815
2816         locked = shash_find_data(json_object(result), "locked");
2817         got_lock = locked && locked->type == JSON_TRUE;
2818     } else {
2819         got_lock = false;
2820     }
2821
2822     ovsdb_idl_update_has_lock(idl, got_lock);
2823     if (!got_lock) {
2824         idl->is_lock_contended = true;
2825     }
2826 }
2827
2828 static void
2829 ovsdb_idl_parse_lock_notify(struct ovsdb_idl *idl,
2830                             const struct json *params,
2831                             bool new_has_lock)
2832 {
2833     if (idl->lock_name
2834         && params->type == JSON_ARRAY
2835         && json_array(params)->n > 0
2836         && json_array(params)->elems[0]->type == JSON_STRING) {
2837         const char *lock_name = json_string(json_array(params)->elems[0]);
2838
2839         if (!strcmp(idl->lock_name, lock_name)) {
2840             ovsdb_idl_update_has_lock(idl, new_has_lock);
2841             if (!new_has_lock) {
2842                 idl->is_lock_contended = true;
2843             }
2844         }
2845     }
2846 }
2847
2848 void
2849 ovsdb_idl_loop_destroy(struct ovsdb_idl_loop *loop)
2850 {
2851     if (loop) {
2852         ovsdb_idl_destroy(loop->idl);
2853     }
2854 }
2855
2856 struct ovsdb_idl_txn *
2857 ovsdb_idl_loop_run(struct ovsdb_idl_loop *loop)
2858 {
2859     ovsdb_idl_run(loop->idl);
2860     loop->open_txn = (loop->committing_txn
2861                       || ovsdb_idl_get_seqno(loop->idl) == loop->skip_seqno
2862                       ? NULL
2863                       : ovsdb_idl_txn_create(loop->idl));
2864     return loop->open_txn;
2865 }
2866
2867 void
2868 ovsdb_idl_loop_commit_and_wait(struct ovsdb_idl_loop *loop)
2869 {
2870     if (loop->open_txn) {
2871         loop->committing_txn = loop->open_txn;
2872         loop->open_txn = NULL;
2873
2874         loop->precommit_seqno = ovsdb_idl_get_seqno(loop->idl);
2875     }
2876
2877     struct ovsdb_idl_txn *txn = loop->committing_txn;
2878     if (txn) {
2879         enum ovsdb_idl_txn_status status = ovsdb_idl_txn_commit(txn);
2880         if (status != TXN_INCOMPLETE) {
2881             switch (status) {
2882             case TXN_TRY_AGAIN:
2883                 /* We want to re-evaluate the database when it's changed from
2884                  * the contents that it had when we started the commit.  (That
2885                  * might have already happened.) */
2886                 loop->skip_seqno = loop->precommit_seqno;
2887                 if (ovsdb_idl_get_seqno(loop->idl) != loop->skip_seqno) {
2888                     poll_immediate_wake();
2889                 }
2890                 break;
2891
2892             case TXN_SUCCESS:
2893                 /* If the database has already changed since we started the
2894                  * commit, re-evaluate it immediately to avoid missing a change
2895                  * for a while. */
2896                 if (ovsdb_idl_get_seqno(loop->idl) != loop->precommit_seqno) {
2897                     poll_immediate_wake();
2898                 }
2899                 break;
2900
2901             case TXN_UNCHANGED:
2902             case TXN_ABORTED:
2903             case TXN_NOT_LOCKED:
2904             case TXN_ERROR:
2905                 break;
2906
2907             case TXN_UNCOMMITTED:
2908             case TXN_INCOMPLETE:
2909                 OVS_NOT_REACHED();
2910
2911             }
2912             ovsdb_idl_txn_destroy(txn);
2913             loop->committing_txn = NULL;
2914         }
2915     }
2916
2917     ovsdb_idl_wait(loop->idl);
2918 }