ovsdb-client: Add optional table and columns to dump command
[cascardo/ovs.git] / ovsdb / ovsdb-server.c
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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 <errno.h>
19 #include <getopt.h>
20 #include <inttypes.h>
21 #include <signal.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25 #include "column.h"
26 #include "command-line.h"
27 #include "daemon.h"
28 #include "dirs.h"
29 #include "dynamic-string.h"
30 #include "fatal-signal.h"
31 #include "file.h"
32 #include "hash.h"
33 #include "json.h"
34 #include "jsonrpc.h"
35 #include "jsonrpc-server.h"
36 #include "list.h"
37 #include "memory.h"
38 #include "ovsdb.h"
39 #include "ovsdb-data.h"
40 #include "ovsdb-types.h"
41 #include "ovsdb-error.h"
42 #include "poll-loop.h"
43 #include "process.h"
44 #include "row.h"
45 #include "simap.h"
46 #include "shash.h"
47 #include "stream-ssl.h"
48 #include "stream.h"
49 #include "sset.h"
50 #include "table.h"
51 #include "timeval.h"
52 #include "transaction.h"
53 #include "trigger.h"
54 #include "util.h"
55 #include "unixctl.h"
56 #include "perf-counter.h"
57 #include "openvswitch/vlog.h"
58
59 VLOG_DEFINE_THIS_MODULE(ovsdb_server);
60
61 struct db {
62     /* Initialized in main(). */
63     char *filename;
64     struct ovsdb_file *file;
65     struct ovsdb *db;
66
67     /* Only used by update_remote_status(). */
68     struct ovsdb_txn *txn;
69 };
70
71 /* SSL configuration. */
72 static char *private_key_file;
73 static char *certificate_file;
74 static char *ca_cert_file;
75 static bool bootstrap_ca_cert;
76
77 static unixctl_cb_func ovsdb_server_exit;
78 static unixctl_cb_func ovsdb_server_compact;
79 static unixctl_cb_func ovsdb_server_reconnect;
80 static unixctl_cb_func ovsdb_server_perf_counters_clear;
81 static unixctl_cb_func ovsdb_server_perf_counters_show;
82
83 struct server_config {
84     struct sset *remotes;
85     struct shash *all_dbs;
86     FILE *config_tmpfile;
87     struct ovsdb_jsonrpc_server *jsonrpc;
88 };
89 static unixctl_cb_func ovsdb_server_add_remote;
90 static unixctl_cb_func ovsdb_server_remove_remote;
91 static unixctl_cb_func ovsdb_server_list_remotes;
92
93 static unixctl_cb_func ovsdb_server_add_database;
94 static unixctl_cb_func ovsdb_server_remove_database;
95 static unixctl_cb_func ovsdb_server_list_databases;
96
97 static char *open_db(struct server_config *config, const char *filename);
98 static void close_db(struct db *db);
99
100 static void parse_options(int *argc, char **argvp[],
101                           struct sset *remotes, char **unixctl_pathp,
102                           char **run_command);
103 OVS_NO_RETURN static void usage(void);
104
105 static char *reconfigure_remotes(struct ovsdb_jsonrpc_server *,
106                                  const struct shash *all_dbs,
107                                  struct sset *remotes);
108 static char *reconfigure_ssl(const struct shash *all_dbs);
109 static void report_error_if_changed(char *error, char **last_errorp);
110
111 static void update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
112                                  const struct sset *remotes,
113                                  struct shash *all_dbs);
114
115 static void save_config__(FILE *config_file, const struct sset *remotes,
116                           const struct sset *db_filenames);
117 static void save_config(struct server_config *);
118 static void load_config(FILE *config_file, struct sset *remotes,
119                         struct sset *db_filenames);
120
121 static void
122 main_loop(struct ovsdb_jsonrpc_server *jsonrpc, struct shash *all_dbs,
123           struct unixctl_server *unixctl, struct sset *remotes,
124           struct process *run_process, bool *exiting)
125 {
126     char *remotes_error, *ssl_error;
127     struct shash_node *node;
128     long long int status_timer = LLONG_MIN;
129
130     *exiting = false;
131     ssl_error = NULL;
132     remotes_error = NULL;
133     while (!*exiting) {
134         memory_run();
135         if (memory_should_report()) {
136             struct simap usage;
137
138             simap_init(&usage);
139             ovsdb_jsonrpc_server_get_memory_usage(jsonrpc, &usage);
140             SHASH_FOR_EACH(node, all_dbs) {
141                 struct db *db = node->data;
142                 ovsdb_get_memory_usage(db->db, &usage);
143             }
144             memory_report(&usage);
145             simap_destroy(&usage);
146         }
147
148         /* Run unixctl_server_run() before reconfigure_remotes() because
149          * ovsdb-server/add-remote and ovsdb-server/remove-remote can change
150          * the set of remotes that reconfigure_remotes() uses. */
151         unixctl_server_run(unixctl);
152
153         report_error_if_changed(
154             reconfigure_remotes(jsonrpc, all_dbs, remotes),
155             &remotes_error);
156         report_error_if_changed(reconfigure_ssl(all_dbs), &ssl_error);
157         ovsdb_jsonrpc_server_run(jsonrpc);
158
159         SHASH_FOR_EACH(node, all_dbs) {
160             struct db *db = node->data;
161             ovsdb_trigger_run(db->db, time_msec());
162         }
163         if (run_process) {
164             process_run();
165             if (process_exited(run_process)) {
166                 *exiting = true;
167             }
168         }
169
170         /* update Manager status(es) every 5 seconds */
171         if (time_msec() >= status_timer) {
172             status_timer = time_msec() + 5000;
173             update_remote_status(jsonrpc, remotes, all_dbs);
174         }
175
176         memory_wait();
177         ovsdb_jsonrpc_server_wait(jsonrpc);
178         unixctl_server_wait(unixctl);
179         SHASH_FOR_EACH(node, all_dbs) {
180             struct db *db = node->data;
181             ovsdb_trigger_wait(db->db, time_msec());
182         }
183         if (run_process) {
184             process_wait(run_process);
185         }
186         if (*exiting) {
187             poll_immediate_wake();
188         }
189         poll_timer_wait_until(status_timer);
190         poll_block();
191         if (should_service_stop()) {
192             *exiting = true;
193         }
194     }
195
196 }
197
198 int
199 main(int argc, char *argv[])
200 {
201     char *unixctl_path = NULL;
202     char *run_command = NULL;
203     struct unixctl_server *unixctl;
204     struct ovsdb_jsonrpc_server *jsonrpc;
205     struct sset remotes, db_filenames;
206     const char *db_filename;
207     struct process *run_process;
208     bool exiting;
209     int retval;
210     FILE *config_tmpfile;
211     struct server_config server_config;
212     struct shash all_dbs;
213     struct shash_node *node, *next;
214     char *error;
215     int i;
216
217     ovs_cmdl_proctitle_init(argc, argv);
218     set_program_name(argv[0]);
219     service_start(&argc, &argv);
220     fatal_ignore_sigpipe();
221     process_init();
222
223     parse_options(&argc, &argv, &remotes, &unixctl_path, &run_command);
224     daemon_become_new_user(false);
225
226     /* Create and initialize 'config_tmpfile' as a temporary file to hold
227      * ovsdb-server's most basic configuration, and then save our initial
228      * configuration to it.  When --monitor is used, this preserves the effects
229      * of ovs-appctl commands such as ovsdb-server/add-remote (which saves the
230      * new configuration) across crashes. */
231     config_tmpfile = tmpfile();
232     if (!config_tmpfile) {
233         ovs_fatal(errno, "failed to create temporary file");
234     }
235
236     sset_init(&db_filenames);
237     if (argc > 0) {
238         for (i = 0; i < argc; i++) {
239             sset_add(&db_filenames, argv[i]);
240          }
241     } else {
242         char *default_db = xasprintf("%s/conf.db", ovs_dbdir());
243         sset_add(&db_filenames, default_db);
244         free(default_db);
245     }
246
247     server_config.remotes = &remotes;
248     server_config.config_tmpfile = config_tmpfile;
249
250     save_config__(config_tmpfile, &remotes, &db_filenames);
251
252     daemonize_start(false);
253
254     /* Load the saved config. */
255     load_config(config_tmpfile, &remotes, &db_filenames);
256     jsonrpc = ovsdb_jsonrpc_server_create();
257
258     shash_init(&all_dbs);
259     server_config.all_dbs = &all_dbs;
260     server_config.jsonrpc = jsonrpc;
261     SSET_FOR_EACH (db_filename, &db_filenames) {
262         error = open_db(&server_config, db_filename);
263         if (error) {
264             ovs_fatal(0, "%s", error);
265         }
266     }
267
268     error = reconfigure_remotes(jsonrpc, &all_dbs, &remotes);
269     if (!error) {
270         error = reconfigure_ssl(&all_dbs);
271     }
272     if (error) {
273         ovs_fatal(0, "%s", error);
274     }
275
276     retval = unixctl_server_create(unixctl_path, &unixctl);
277     if (retval) {
278         exit(EXIT_FAILURE);
279     }
280
281     if (run_command) {
282         char *run_argv[4];
283
284         run_argv[0] = "/bin/sh";
285         run_argv[1] = "-c";
286         run_argv[2] = run_command;
287         run_argv[3] = NULL;
288
289         retval = process_start(run_argv, &run_process);
290         if (retval) {
291             ovs_fatal(retval, "%s: process failed to start", run_command);
292         }
293     } else {
294         run_process = NULL;
295     }
296
297     daemonize_complete();
298
299     perf_counters_init();
300
301     if (!run_command) {
302         /* ovsdb-server is usually a long-running process, in which case it
303          * makes plenty of sense to log the version, but --run makes
304          * ovsdb-server more like a command-line tool, so skip it.  */
305         VLOG_INFO("%s (Open vSwitch) %s", program_name, VERSION);
306     }
307
308     unixctl_command_register("exit", "", 0, 0, ovsdb_server_exit, &exiting);
309     unixctl_command_register("ovsdb-server/compact", "", 0, 1,
310                              ovsdb_server_compact, &all_dbs);
311     unixctl_command_register("ovsdb-server/reconnect", "", 0, 0,
312                              ovsdb_server_reconnect, jsonrpc);
313
314     unixctl_command_register("ovsdb-server/add-remote", "REMOTE", 1, 1,
315                              ovsdb_server_add_remote, &server_config);
316     unixctl_command_register("ovsdb-server/remove-remote", "REMOTE", 1, 1,
317                              ovsdb_server_remove_remote, &server_config);
318     unixctl_command_register("ovsdb-server/list-remotes", "", 0, 0,
319                              ovsdb_server_list_remotes, &remotes);
320
321     unixctl_command_register("ovsdb-server/add-db", "DB", 1, 1,
322                              ovsdb_server_add_database, &server_config);
323     unixctl_command_register("ovsdb-server/remove-db", "DB", 1, 1,
324                              ovsdb_server_remove_database, &server_config);
325     unixctl_command_register("ovsdb-server/list-dbs", "", 0, 0,
326                              ovsdb_server_list_databases, &all_dbs);
327     unixctl_command_register("ovsdb-server/perf-counters-show", "", 0, 0,
328                              ovsdb_server_perf_counters_show, NULL);
329     unixctl_command_register("ovsdb-server/perf-counters-clear", "", 0, 0,
330                              ovsdb_server_perf_counters_clear, NULL);
331
332     main_loop(jsonrpc, &all_dbs, unixctl, &remotes, run_process, &exiting);
333
334     ovsdb_jsonrpc_server_destroy(jsonrpc);
335     SHASH_FOR_EACH_SAFE(node, next, &all_dbs) {
336         struct db *db = node->data;
337         close_db(db);
338         shash_delete(&all_dbs, node);
339     }
340     shash_destroy(&all_dbs);
341     sset_destroy(&remotes);
342     sset_destroy(&db_filenames);
343     unixctl_server_destroy(unixctl);
344
345     if (run_process && process_exited(run_process)) {
346         int status = process_status(run_process);
347         if (status) {
348             ovs_fatal(0, "%s: child exited, %s",
349                       run_command, process_status_msg(status));
350         }
351     }
352     perf_counters_destroy();
353     service_stop();
354     return 0;
355 }
356
357 /* Returns true if 'filename' is known to be already open as a database,
358  * false if not.
359  *
360  * "False negatives" are possible. */
361 static bool
362 is_already_open(struct server_config *config OVS_UNUSED,
363                 const char *filename OVS_UNUSED)
364 {
365 #ifndef _WIN32
366     struct stat s;
367
368     if (!stat(filename, &s)) {
369         struct shash_node *node;
370
371         SHASH_FOR_EACH (node, config->all_dbs) {
372             struct db *db = node->data;
373             struct stat s2;
374
375             if (!stat(db->filename, &s2)
376                 && s.st_dev == s2.st_dev
377                 && s.st_ino == s2.st_ino) {
378                 return true;
379             }
380         }
381     }
382 #endif  /* !_WIN32 */
383
384     return false;
385 }
386
387 static void
388 close_db(struct db *db)
389 {
390     ovsdb_destroy(db->db);
391     free(db->filename);
392     free(db);
393 }
394
395 static char *
396 open_db(struct server_config *config, const char *filename)
397 {
398     struct ovsdb_error *db_error;
399     struct db *db;
400     char *error;
401
402     /* If we know that the file is already open, return a good error message.
403      * Otherwise, if the file is open, we'll fail later on with a harder to
404      * interpret file locking error. */
405     if (is_already_open(config, filename)) {
406         return xasprintf("%s: already open", filename);
407     }
408
409     db = xzalloc(sizeof *db);
410     db->filename = xstrdup(filename);
411
412     db_error = ovsdb_file_open(db->filename, false, &db->db, &db->file);
413     if (db_error) {
414         error = ovsdb_error_to_string(db_error);
415     } else if (!ovsdb_jsonrpc_server_add_db(config->jsonrpc, db->db)) {
416         error = xasprintf("%s: duplicate database name", db->db->schema->name);
417     } else {
418         shash_add_assert(config->all_dbs, db->db->schema->name, db);
419         return NULL;
420     }
421
422     ovsdb_error_destroy(db_error);
423     close_db(db);
424     return error;
425 }
426
427 static const struct db *
428 find_db(const struct shash *all_dbs, const char *db_name)
429 {
430     struct shash_node *node;
431
432     SHASH_FOR_EACH(node, all_dbs) {
433         struct db *db = node->data;
434         if (!strcmp(db->db->schema->name, db_name)) {
435             return db;
436         }
437     }
438
439     return NULL;
440 }
441
442 static char * OVS_WARN_UNUSED_RESULT
443 parse_db_column__(const struct shash *all_dbs,
444                   const char *name_, char *name,
445                   const struct db **dbp,
446                   const struct ovsdb_table **tablep,
447                   const struct ovsdb_column **columnp)
448 {
449     const char *db_name, *table_name, *column_name;
450     const struct ovsdb_column *column;
451     const struct ovsdb_table *table;
452     const char *tokens[3];
453     char *save_ptr = NULL;
454     const struct db *db;
455
456     *dbp = NULL;
457     *tablep = NULL;
458     *columnp = NULL;
459
460     strtok_r(name, ":", &save_ptr); /* "db:" */
461     tokens[0] = strtok_r(NULL, ",", &save_ptr);
462     tokens[1] = strtok_r(NULL, ",", &save_ptr);
463     tokens[2] = strtok_r(NULL, ",", &save_ptr);
464     if (!tokens[0] || !tokens[1] || !tokens[2]) {
465         return xasprintf("\"%s\": invalid syntax", name_);
466     }
467
468     db_name = tokens[0];
469     table_name = tokens[1];
470     column_name = tokens[2];
471
472     db = find_db(all_dbs, tokens[0]);
473     if (!db) {
474         return xasprintf("\"%s\": no database named %s", name_, db_name);
475     }
476
477     table = ovsdb_get_table(db->db, table_name);
478     if (!table) {
479         return xasprintf("\"%s\": no table named %s", name_, table_name);
480     }
481
482     column = ovsdb_table_schema_get_column(table->schema, column_name);
483     if (!column) {
484         return xasprintf("\"%s\": table \"%s\" has no column \"%s\"",
485                          name_, table_name, column_name);
486     }
487
488     *dbp = db;
489     *columnp = column;
490     *tablep = table;
491     return NULL;
492 }
493
494 /* Returns NULL if successful, otherwise a malloc()'d string describing the
495  * error. */
496 static char * OVS_WARN_UNUSED_RESULT
497 parse_db_column(const struct shash *all_dbs,
498                 const char *name_,
499                 const struct db **dbp,
500                 const struct ovsdb_table **tablep,
501                 const struct ovsdb_column **columnp)
502 {
503     char *name = xstrdup(name_);
504     char *retval = parse_db_column__(all_dbs, name_, name,
505                                      dbp, tablep, columnp);
506     free(name);
507     return retval;
508 }
509
510 /* Returns NULL if successful, otherwise a malloc()'d string describing the
511  * error. */
512 static char * OVS_WARN_UNUSED_RESULT
513 parse_db_string_column(const struct shash *all_dbs,
514                        const char *name,
515                        const struct db **dbp,
516                        const struct ovsdb_table **tablep,
517                        const struct ovsdb_column **columnp)
518 {
519     char *retval;
520
521     retval = parse_db_column(all_dbs, name, dbp, tablep, columnp);
522     if (retval) {
523         return retval;
524     }
525
526     if ((*columnp)->type.key.type != OVSDB_TYPE_STRING
527         || (*columnp)->type.value.type != OVSDB_TYPE_VOID) {
528         return xasprintf("\"%s\": table \"%s\" column \"%s\" is "
529                          "not string or set of strings",
530                          name, (*tablep)->schema->name, (*columnp)->name);
531     }
532
533     return NULL;
534 }
535
536 static const char *
537 query_db_string(const struct shash *all_dbs, const char *name,
538                 struct ds *errors)
539 {
540     if (!name || strncmp(name, "db:", 3)) {
541         return name;
542     } else {
543         const struct ovsdb_column *column;
544         const struct ovsdb_table *table;
545         const struct ovsdb_row *row;
546         const struct db *db;
547         char *retval;
548
549         retval = parse_db_string_column(all_dbs, name,
550                                         &db, &table, &column);
551         if (retval) {
552             ds_put_format(errors, "%s\n", retval);
553             free(retval);
554             return NULL;
555         }
556
557         HMAP_FOR_EACH (row, hmap_node, &table->rows) {
558             const struct ovsdb_datum *datum;
559             size_t i;
560
561             datum = &row->fields[column->index];
562             for (i = 0; i < datum->n; i++) {
563                 if (datum->keys[i].string[0]) {
564                     return datum->keys[i].string;
565                 }
566             }
567         }
568         return NULL;
569     }
570 }
571
572 static struct ovsdb_jsonrpc_options *
573 add_remote(struct shash *remotes, const char *target)
574 {
575     struct ovsdb_jsonrpc_options *options;
576
577     options = shash_find_data(remotes, target);
578     if (!options) {
579         options = ovsdb_jsonrpc_default_options(target);
580         shash_add(remotes, target, options);
581     }
582
583     return options;
584 }
585
586 static struct ovsdb_datum *
587 get_datum(struct ovsdb_row *row, const char *column_name,
588           const enum ovsdb_atomic_type key_type,
589           const enum ovsdb_atomic_type value_type,
590           const size_t n_max)
591 {
592     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
593     const struct ovsdb_table_schema *schema = row->table->schema;
594     const struct ovsdb_column *column;
595
596     column = ovsdb_table_schema_get_column(schema, column_name);
597     if (!column) {
598         VLOG_DBG_RL(&rl, "Table `%s' has no `%s' column",
599                     schema->name, column_name);
600         return NULL;
601     }
602
603     if (column->type.key.type != key_type
604         || column->type.value.type != value_type
605         || column->type.n_max != n_max) {
606         if (!VLOG_DROP_DBG(&rl)) {
607             char *type_name = ovsdb_type_to_english(&column->type);
608             VLOG_DBG("Table `%s' column `%s' has type %s, not expected "
609                      "key type %s, value type %s, max elements %"PRIuSIZE".",
610                      schema->name, column_name, type_name,
611                      ovsdb_atomic_type_to_string(key_type),
612                      ovsdb_atomic_type_to_string(value_type),
613                      n_max);
614             free(type_name);
615         }
616         return NULL;
617     }
618
619     return &row->fields[column->index];
620 }
621
622 /* Read string-string key-values from a map.  Returns the value associated with
623  * 'key', if found, or NULL */
624 static const char *
625 read_map_string_column(const struct ovsdb_row *row, const char *column_name,
626                        const char *key)
627 {
628     const struct ovsdb_datum *datum;
629     union ovsdb_atom *atom_key = NULL, *atom_value = NULL;
630     size_t i;
631
632     datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name,
633                       OVSDB_TYPE_STRING, OVSDB_TYPE_STRING, UINT_MAX);
634
635     if (!datum) {
636         return NULL;
637     }
638
639     for (i = 0; i < datum->n; i++) {
640         atom_key = &datum->keys[i];
641         if (!strcmp(atom_key->string, key)){
642             atom_value = &datum->values[i];
643             break;
644         }
645     }
646
647     return atom_value ? atom_value->string : NULL;
648 }
649
650 static const union ovsdb_atom *
651 read_column(const struct ovsdb_row *row, const char *column_name,
652             enum ovsdb_atomic_type type)
653 {
654     const struct ovsdb_datum *datum;
655
656     datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name, type,
657                       OVSDB_TYPE_VOID, 1);
658     return datum && datum->n ? datum->keys : NULL;
659 }
660
661 static bool
662 read_integer_column(const struct ovsdb_row *row, const char *column_name,
663                     long long int *integerp)
664 {
665     const union ovsdb_atom *atom;
666
667     atom = read_column(row, column_name, OVSDB_TYPE_INTEGER);
668     *integerp = atom ? atom->integer : 0;
669     return atom != NULL;
670 }
671
672 static bool
673 read_string_column(const struct ovsdb_row *row, const char *column_name,
674                    const char **stringp)
675 {
676     const union ovsdb_atom *atom;
677
678     atom = read_column(row, column_name, OVSDB_TYPE_STRING);
679     *stringp = atom ? atom->string : NULL;
680     return atom != NULL;
681 }
682
683 static void
684 write_bool_column(struct ovsdb_row *row, const char *column_name, bool value)
685 {
686     const struct ovsdb_column *column;
687     struct ovsdb_datum *datum;
688
689     column = ovsdb_table_schema_get_column(row->table->schema, column_name);
690     datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
691                       OVSDB_TYPE_VOID, 1);
692     if (!datum) {
693         return;
694     }
695
696     if (datum->n != 1) {
697         ovsdb_datum_destroy(datum, &column->type);
698
699         datum->n = 1;
700         datum->keys = xmalloc(sizeof *datum->keys);
701         datum->values = NULL;
702     }
703
704     datum->keys[0].boolean = value;
705 }
706
707 static void
708 write_string_string_column(struct ovsdb_row *row, const char *column_name,
709                            char **keys, char **values, size_t n)
710 {
711     const struct ovsdb_column *column;
712     struct ovsdb_datum *datum;
713     size_t i;
714
715     column = ovsdb_table_schema_get_column(row->table->schema, column_name);
716     datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
717                       UINT_MAX);
718     if (!datum) {
719         for (i = 0; i < n; i++) {
720             free(keys[i]);
721             free(values[i]);
722         }
723         return;
724     }
725
726     /* Free existing data. */
727     ovsdb_datum_destroy(datum, &column->type);
728
729     /* Allocate space for new values. */
730     datum->n = n;
731     datum->keys = xmalloc(n * sizeof *datum->keys);
732     datum->values = xmalloc(n * sizeof *datum->values);
733
734     for (i = 0; i < n; ++i) {
735         datum->keys[i].string = keys[i];
736         datum->values[i].string = values[i];
737     }
738
739     /* Sort and check constraints. */
740     ovsdb_datum_sort_assert(datum, column->type.key.type);
741 }
742
743 /* Adds a remote and options to 'remotes', based on the Manager table row in
744  * 'row'. */
745 static void
746 add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
747 {
748     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
749     struct ovsdb_jsonrpc_options *options;
750     long long int max_backoff, probe_interval;
751     const char *target, *dscp_string;
752
753     if (!read_string_column(row, "target", &target) || !target) {
754         VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
755                      row->table->schema->name);
756         return;
757     }
758
759     options = add_remote(remotes, target);
760     if (read_integer_column(row, "max_backoff", &max_backoff)) {
761         options->max_backoff = max_backoff;
762     }
763     if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
764         options->probe_interval = probe_interval;
765     }
766
767     options->dscp = DSCP_DEFAULT;
768     dscp_string = read_map_string_column(row, "other_config", "dscp");
769     if (dscp_string) {
770         int dscp = atoi(dscp_string);
771         if (dscp >= 0 && dscp <= 63) {
772             options->dscp = dscp;
773         }
774     }
775 }
776
777 static void
778 query_db_remotes(const char *name, const struct shash *all_dbs,
779                  struct shash *remotes, struct ds *errors)
780 {
781     const struct ovsdb_column *column;
782     const struct ovsdb_table *table;
783     const struct ovsdb_row *row;
784     const struct db *db;
785     char *retval;
786
787     retval = parse_db_column(all_dbs, name, &db, &table, &column);
788     if (retval) {
789         ds_put_format(errors, "%s\n", retval);
790         free(retval);
791         return;
792     }
793
794     if (column->type.key.type == OVSDB_TYPE_STRING
795         && column->type.value.type == OVSDB_TYPE_VOID) {
796         HMAP_FOR_EACH (row, hmap_node, &table->rows) {
797             const struct ovsdb_datum *datum;
798             size_t i;
799
800             datum = &row->fields[column->index];
801             for (i = 0; i < datum->n; i++) {
802                 add_remote(remotes, datum->keys[i].string);
803             }
804         }
805     } else if (column->type.key.type == OVSDB_TYPE_UUID
806                && column->type.key.u.uuid.refTable
807                && column->type.value.type == OVSDB_TYPE_VOID) {
808         const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
809         HMAP_FOR_EACH (row, hmap_node, &table->rows) {
810             const struct ovsdb_datum *datum;
811             size_t i;
812
813             datum = &row->fields[column->index];
814             for (i = 0; i < datum->n; i++) {
815                 const struct ovsdb_row *ref_row;
816
817                 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
818                 if (ref_row) {
819                     add_manager_options(remotes, ref_row);
820                 }
821             }
822         }
823     }
824 }
825
826 static void
827 update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
828                   const struct ovsdb_jsonrpc_server *jsonrpc)
829 {
830     struct ovsdb_jsonrpc_remote_status status;
831     struct ovsdb_row *rw_row;
832     const char *target;
833     char *keys[9], *values[9];
834     size_t n = 0;
835
836     /* Get the "target" (protocol/host/port) spec. */
837     if (!read_string_column(row, "target", &target)) {
838         /* Bad remote spec or incorrect schema. */
839         return;
840     }
841     rw_row = ovsdb_txn_row_modify(txn, row);
842     ovsdb_jsonrpc_server_get_remote_status(jsonrpc, target, &status);
843
844     /* Update status information columns. */
845     write_bool_column(rw_row, "is_connected", status.is_connected);
846
847     if (status.state) {
848         keys[n] = xstrdup("state");
849         values[n++] = xstrdup(status.state);
850     }
851     if (status.sec_since_connect != UINT_MAX) {
852         keys[n] = xstrdup("sec_since_connect");
853         values[n++] = xasprintf("%u", status.sec_since_connect);
854     }
855     if (status.sec_since_disconnect != UINT_MAX) {
856         keys[n] = xstrdup("sec_since_disconnect");
857         values[n++] = xasprintf("%u", status.sec_since_disconnect);
858     }
859     if (status.last_error) {
860         keys[n] = xstrdup("last_error");
861         values[n++] =
862             xstrdup(ovs_retval_to_string(status.last_error));
863     }
864     if (status.locks_held && status.locks_held[0]) {
865         keys[n] = xstrdup("locks_held");
866         values[n++] = xstrdup(status.locks_held);
867     }
868     if (status.locks_waiting && status.locks_waiting[0]) {
869         keys[n] = xstrdup("locks_waiting");
870         values[n++] = xstrdup(status.locks_waiting);
871     }
872     if (status.locks_lost && status.locks_lost[0]) {
873         keys[n] = xstrdup("locks_lost");
874         values[n++] = xstrdup(status.locks_lost);
875     }
876     if (status.n_connections > 1) {
877         keys[n] = xstrdup("n_connections");
878         values[n++] = xasprintf("%d", status.n_connections);
879     }
880     if (status.bound_port != htons(0)) {
881         keys[n] = xstrdup("bound_port");
882         values[n++] = xasprintf("%"PRIu16, ntohs(status.bound_port));
883     }
884     write_string_string_column(rw_row, "status", keys, values, n);
885
886     ovsdb_jsonrpc_server_free_remote_status(&status);
887 }
888
889 static void
890 update_remote_rows(const struct shash *all_dbs,
891                    const char *remote_name,
892                    const struct ovsdb_jsonrpc_server *jsonrpc)
893 {
894     const struct ovsdb_table *table, *ref_table;
895     const struct ovsdb_column *column;
896     const struct ovsdb_row *row;
897     const struct db *db;
898     char *retval;
899
900     if (strncmp("db:", remote_name, 3)) {
901         return;
902     }
903
904     retval = parse_db_column(all_dbs, remote_name, &db, &table, &column);
905     if (retval) {
906         free(retval);
907         return;
908     }
909
910     if (column->type.key.type != OVSDB_TYPE_UUID
911         || !column->type.key.u.uuid.refTable
912         || column->type.value.type != OVSDB_TYPE_VOID) {
913         return;
914     }
915
916     ref_table = column->type.key.u.uuid.refTable;
917
918     HMAP_FOR_EACH (row, hmap_node, &table->rows) {
919         const struct ovsdb_datum *datum;
920         size_t i;
921
922         datum = &row->fields[column->index];
923         for (i = 0; i < datum->n; i++) {
924             const struct ovsdb_row *ref_row;
925
926             ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
927             if (ref_row) {
928                 update_remote_row(ref_row, db->txn, jsonrpc);
929             }
930         }
931     }
932 }
933
934 static void
935 update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
936                      const struct sset *remotes,
937                      struct shash *all_dbs)
938 {
939     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
940     const char *remote;
941     struct db *db;
942     struct shash_node *node;
943
944     SHASH_FOR_EACH(node, all_dbs) {
945         db = node->data;
946         db->txn = ovsdb_txn_create(db->db);
947     }
948
949     /* Iterate over --remote arguments given on command line. */
950     SSET_FOR_EACH (remote, remotes) {
951         update_remote_rows(all_dbs, remote, jsonrpc);
952     }
953
954     SHASH_FOR_EACH(node, all_dbs) {
955         struct ovsdb_error *error;
956         db = node->data;
957         error = ovsdb_txn_commit(db->txn, false);
958         if (error) {
959             VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
960                         ovsdb_error_to_string(error));
961             ovsdb_error_destroy(error);
962         }
963     }
964 }
965
966 /* Reconfigures ovsdb-server's remotes based on information in the database. */
967 static char *
968 reconfigure_remotes(struct ovsdb_jsonrpc_server *jsonrpc,
969                     const struct shash *all_dbs, struct sset *remotes)
970 {
971     struct ds errors = DS_EMPTY_INITIALIZER;
972     struct shash resolved_remotes;
973     const char *name;
974
975     /* Configure remotes. */
976     shash_init(&resolved_remotes);
977     SSET_FOR_EACH (name, remotes) {
978         if (!strncmp(name, "db:", 3)) {
979             query_db_remotes(name, all_dbs, &resolved_remotes, &errors);
980         } else {
981             add_remote(&resolved_remotes, name);
982         }
983     }
984     ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
985     shash_destroy_free_data(&resolved_remotes);
986
987     return errors.string;
988 }
989
990 static char *
991 reconfigure_ssl(const struct shash *all_dbs)
992 {
993     struct ds errors = DS_EMPTY_INITIALIZER;
994     const char *resolved_private_key;
995     const char *resolved_certificate;
996     const char *resolved_ca_cert;
997
998     resolved_private_key = query_db_string(all_dbs, private_key_file, &errors);
999     resolved_certificate = query_db_string(all_dbs, certificate_file, &errors);
1000     resolved_ca_cert = query_db_string(all_dbs, ca_cert_file, &errors);
1001
1002     stream_ssl_set_key_and_cert(resolved_private_key, resolved_certificate);
1003     stream_ssl_set_ca_cert_file(resolved_ca_cert, bootstrap_ca_cert);
1004
1005     return errors.string;
1006 }
1007
1008 static void
1009 report_error_if_changed(char *error, char **last_errorp)
1010 {
1011     if (error) {
1012         if (!*last_errorp || strcmp(error, *last_errorp)) {
1013             VLOG_WARN("%s", error);
1014             free(*last_errorp);
1015             *last_errorp = error;
1016             return;
1017         }
1018         free(error);
1019     } else {
1020         free(*last_errorp);
1021         *last_errorp = NULL;
1022     }
1023 }
1024
1025 static void
1026 ovsdb_server_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
1027                   const char *argv[] OVS_UNUSED,
1028                   void *exiting_)
1029 {
1030     bool *exiting = exiting_;
1031     *exiting = true;
1032     unixctl_command_reply(conn, NULL);
1033 }
1034
1035 static void
1036 ovsdb_server_perf_counters_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
1037                                 const char *argv[] OVS_UNUSED,
1038                                 void *arg_ OVS_UNUSED)
1039 {
1040     char *s = perf_counters_to_string();
1041
1042     unixctl_command_reply(conn, s);
1043     free(s);
1044 }
1045
1046 static void
1047 ovsdb_server_perf_counters_clear(struct unixctl_conn *conn, int argc OVS_UNUSED,
1048                                  const char *argv[] OVS_UNUSED,
1049                                  void *arg_ OVS_UNUSED)
1050 {
1051     perf_counters_clear();
1052     unixctl_command_reply(conn, NULL);
1053 }
1054
1055 static void
1056 ovsdb_server_compact(struct unixctl_conn *conn, int argc,
1057                      const char *argv[], void *dbs_)
1058 {
1059     struct shash *all_dbs = dbs_;
1060     struct ds reply;
1061     struct db *db;
1062     struct shash_node *node;
1063     int n = 0;
1064
1065     ds_init(&reply);
1066     SHASH_FOR_EACH(node, all_dbs) {
1067         const char *name;
1068
1069         db = node->data;
1070         name = db->db->schema->name;
1071
1072         if (argc < 2 || !strcmp(argv[1], name)) {
1073             struct ovsdb_error *error;
1074
1075             VLOG_INFO("compacting %s database by user request", name);
1076
1077             error = ovsdb_file_compact(db->file);
1078             if (error) {
1079                 char *s = ovsdb_error_to_string(error);
1080                 ds_put_format(&reply, "%s\n", s);
1081                 free(s);
1082                 ovsdb_error_destroy(error);
1083             }
1084
1085             n++;
1086         }
1087     }
1088
1089     if (!n) {
1090         unixctl_command_reply_error(conn, "no database by that name");
1091     } else if (reply.length) {
1092         unixctl_command_reply_error(conn, ds_cstr(&reply));
1093     } else {
1094         unixctl_command_reply(conn, NULL);
1095     }
1096     ds_destroy(&reply);
1097 }
1098
1099 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
1100  * connections and reconnect. */
1101 static void
1102 ovsdb_server_reconnect(struct unixctl_conn *conn, int argc OVS_UNUSED,
1103                        const char *argv[] OVS_UNUSED, void *jsonrpc_)
1104 {
1105     struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
1106
1107     ovsdb_jsonrpc_server_reconnect(jsonrpc);
1108     unixctl_command_reply(conn, NULL);
1109 }
1110
1111 /* "ovsdb-server/add-remote REMOTE": adds REMOTE to the set of remotes that
1112  * ovsdb-server services. */
1113 static void
1114 ovsdb_server_add_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
1115                         const char *argv[], void *config_)
1116 {
1117     struct server_config *config = config_;
1118     const char *remote = argv[1];
1119
1120     const struct ovsdb_column *column;
1121     const struct ovsdb_table *table;
1122     const struct db *db;
1123     char *retval;
1124
1125     retval = (strncmp("db:", remote, 3)
1126               ? NULL
1127               : parse_db_column(config->all_dbs, remote,
1128                                 &db, &table, &column));
1129     if (!retval) {
1130         if (sset_add(config->remotes, remote)) {
1131             save_config(config);
1132         }
1133         unixctl_command_reply(conn, NULL);
1134     } else {
1135         unixctl_command_reply_error(conn, retval);
1136         free(retval);
1137     }
1138 }
1139
1140 /* "ovsdb-server/remove-remote REMOTE": removes REMOTE frmo the set of remotes
1141  * that ovsdb-server services. */
1142 static void
1143 ovsdb_server_remove_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
1144                            const char *argv[], void *config_)
1145 {
1146     struct server_config *config = config_;
1147     struct sset_node *node;
1148
1149     node = sset_find(config->remotes, argv[1]);
1150     if (node) {
1151         sset_delete(config->remotes, node);
1152         save_config(config);
1153         unixctl_command_reply(conn, NULL);
1154     } else {
1155         unixctl_command_reply_error(conn, "no such remote");
1156     }
1157 }
1158
1159 /* "ovsdb-server/list-remotes": outputs a list of configured rmeotes. */
1160 static void
1161 ovsdb_server_list_remotes(struct unixctl_conn *conn, int argc OVS_UNUSED,
1162                           const char *argv[] OVS_UNUSED, void *remotes_)
1163 {
1164     struct sset *remotes = remotes_;
1165     const char **list, **p;
1166     struct ds s;
1167
1168     ds_init(&s);
1169
1170     list = sset_sort(remotes);
1171     for (p = list; *p; p++) {
1172         ds_put_format(&s, "%s\n", *p);
1173     }
1174     free(list);
1175
1176     unixctl_command_reply(conn, ds_cstr(&s));
1177     ds_destroy(&s);
1178 }
1179
1180
1181 /* "ovsdb-server/add-db DB": adds the DB to ovsdb-server. */
1182 static void
1183 ovsdb_server_add_database(struct unixctl_conn *conn, int argc OVS_UNUSED,
1184                           const char *argv[], void *config_)
1185 {
1186     struct server_config *config = config_;
1187     const char *filename = argv[1];
1188     char *error;
1189
1190     error = open_db(config, filename);
1191     if (!error) {
1192         save_config(config);
1193         unixctl_command_reply(conn, NULL);
1194     } else {
1195         unixctl_command_reply_error(conn, error);
1196         free(error);
1197     }
1198 }
1199
1200 static void
1201 ovsdb_server_remove_database(struct unixctl_conn *conn, int argc OVS_UNUSED,
1202                              const char *argv[], void *config_)
1203 {
1204     struct server_config *config = config_;
1205     struct shash_node *node;
1206     struct db *db;
1207     bool ok;
1208
1209     node = shash_find(config->all_dbs, argv[1]);
1210     if (!node)  {
1211         unixctl_command_reply_error(conn, "Failed to find the database.");
1212         return;
1213     }
1214     db = node->data;
1215
1216     ok = ovsdb_jsonrpc_server_remove_db(config->jsonrpc, db->db);
1217     ovs_assert(ok);
1218
1219     close_db(db);
1220     shash_delete(config->all_dbs, node);
1221
1222     save_config(config);
1223     unixctl_command_reply(conn, NULL);
1224 }
1225
1226 static void
1227 ovsdb_server_list_databases(struct unixctl_conn *conn, int argc OVS_UNUSED,
1228                             const char *argv[] OVS_UNUSED, void *all_dbs_)
1229 {
1230     struct shash *all_dbs = all_dbs_;
1231     const struct shash_node **nodes;
1232     struct ds s;
1233     size_t i;
1234
1235     ds_init(&s);
1236
1237     nodes = shash_sort(all_dbs);
1238     for (i = 0; i < shash_count(all_dbs); i++) {
1239         struct db *db = nodes[i]->data;
1240         ds_put_format(&s, "%s\n", db->db->schema->name);
1241     }
1242     free(nodes);
1243
1244     unixctl_command_reply(conn, ds_cstr(&s));
1245     ds_destroy(&s);
1246 }
1247
1248 static void
1249 parse_options(int *argcp, char **argvp[],
1250               struct sset *remotes, char **unixctl_pathp, char **run_command)
1251 {
1252     enum {
1253         OPT_REMOTE = UCHAR_MAX + 1,
1254         OPT_UNIXCTL,
1255         OPT_RUN,
1256         OPT_BOOTSTRAP_CA_CERT,
1257         OPT_PEER_CA_CERT,
1258         VLOG_OPTION_ENUMS,
1259         DAEMON_OPTION_ENUMS
1260     };
1261     static const struct option long_options[] = {
1262         {"remote",      required_argument, NULL, OPT_REMOTE},
1263         {"unixctl",     required_argument, NULL, OPT_UNIXCTL},
1264 #ifndef _WIN32
1265         {"run",         required_argument, NULL, OPT_RUN},
1266 #endif
1267         {"help",        no_argument, NULL, 'h'},
1268         {"version",     no_argument, NULL, 'V'},
1269         DAEMON_LONG_OPTIONS,
1270         VLOG_LONG_OPTIONS,
1271         {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
1272         {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
1273         {"private-key", required_argument, NULL, 'p'},
1274         {"certificate", required_argument, NULL, 'c'},
1275         {"ca-cert",     required_argument, NULL, 'C'},
1276         {NULL, 0, NULL, 0},
1277     };
1278     char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
1279     int argc = *argcp;
1280     char **argv = *argvp;
1281
1282     sset_init(remotes);
1283     for (;;) {
1284         int c;
1285
1286         c = getopt_long(argc, argv, short_options, long_options, NULL);
1287         if (c == -1) {
1288             break;
1289         }
1290
1291         switch (c) {
1292         case OPT_REMOTE:
1293             sset_add(remotes, optarg);
1294             break;
1295
1296         case OPT_UNIXCTL:
1297             *unixctl_pathp = optarg;
1298             break;
1299
1300         case OPT_RUN:
1301             *run_command = optarg;
1302             break;
1303
1304         case 'h':
1305             usage();
1306
1307         case 'V':
1308             ovs_print_version(0, 0);
1309             exit(EXIT_SUCCESS);
1310
1311         VLOG_OPTION_HANDLERS
1312         DAEMON_OPTION_HANDLERS
1313
1314         case 'p':
1315             private_key_file = optarg;
1316             break;
1317
1318         case 'c':
1319             certificate_file = optarg;
1320             break;
1321
1322         case 'C':
1323             ca_cert_file = optarg;
1324             bootstrap_ca_cert = false;
1325             break;
1326
1327         case OPT_BOOTSTRAP_CA_CERT:
1328             ca_cert_file = optarg;
1329             bootstrap_ca_cert = true;
1330             break;
1331
1332         case OPT_PEER_CA_CERT:
1333             stream_ssl_set_peer_ca_cert_file(optarg);
1334             break;
1335
1336         case '?':
1337             exit(EXIT_FAILURE);
1338
1339         default:
1340             abort();
1341         }
1342     }
1343     free(short_options);
1344
1345     *argcp -= optind;
1346     *argvp += optind;
1347 }
1348
1349 static void
1350 usage(void)
1351 {
1352     printf("%s: Open vSwitch database server\n"
1353            "usage: %s [OPTIONS] [DATABASE...]\n"
1354            "where each DATABASE is a database file in ovsdb format.\n"
1355            "The default DATABASE, if none is given, is\n%s/conf.db.\n",
1356            program_name, program_name, ovs_dbdir());
1357     printf("\nJSON-RPC options (may be specified any number of times):\n"
1358            "  --remote=REMOTE         connect or listen to REMOTE\n");
1359     stream_usage("JSON-RPC", true, true, true);
1360     daemon_usage();
1361     vlog_usage();
1362     printf("\nOther options:\n"
1363            "  --run COMMAND           run COMMAND as subprocess then exit\n"
1364            "  --unixctl=SOCKET        override default control socket name\n"
1365            "  -h, --help              display this help message\n"
1366            "  -V, --version           display version information\n");
1367     exit(EXIT_SUCCESS);
1368 }
1369 \f
1370 static struct json *
1371 sset_to_json(const struct sset *sset)
1372 {
1373     struct json *array;
1374     const char *s;
1375
1376     array = json_array_create_empty();
1377     SSET_FOR_EACH (s, sset) {
1378         json_array_add(array, json_string_create(s));
1379     }
1380     return array;
1381 }
1382
1383 /* Truncates and replaces the contents of 'config_file' by a representation of
1384  * 'remotes' and 'db_filenames'. */
1385 static void
1386 save_config__(FILE *config_file, const struct sset *remotes,
1387               const struct sset *db_filenames)
1388 {
1389     struct json *obj;
1390     char *s;
1391
1392     if (ftruncate(fileno(config_file), 0) == -1) {
1393         VLOG_FATAL("failed to truncate temporary file (%s)",
1394                    ovs_strerror(errno));
1395     }
1396
1397     obj = json_object_create();
1398     json_object_put(obj, "remotes", sset_to_json(remotes));
1399     json_object_put(obj, "db_filenames", sset_to_json(db_filenames));
1400     s = json_to_string(obj, 0);
1401     json_destroy(obj);
1402
1403     if (fseek(config_file, 0, SEEK_SET) != 0
1404         || fputs(s, config_file) == EOF
1405         || fflush(config_file) == EOF) {
1406         VLOG_FATAL("failed to write temporary file (%s)", ovs_strerror(errno));
1407     }
1408     free(s);
1409 }
1410
1411 /* Truncates and replaces the contents of 'config_file' by a representation of
1412  * 'config'. */
1413 static void
1414 save_config(struct server_config *config)
1415 {
1416     struct sset db_filenames;
1417     struct shash_node *node;
1418
1419     sset_init(&db_filenames);
1420     SHASH_FOR_EACH (node, config->all_dbs) {
1421         struct db *db = node->data;
1422         sset_add(&db_filenames, db->filename);
1423     }
1424
1425     save_config__(config->config_tmpfile, config->remotes, &db_filenames);
1426
1427     sset_destroy(&db_filenames);
1428 }
1429
1430 static void
1431 sset_from_json(struct sset *sset, const struct json *array)
1432 {
1433     size_t i;
1434
1435     sset_clear(sset);
1436
1437     ovs_assert(array->type == JSON_ARRAY);
1438     for (i = 0; i < array->u.array.n; i++) {
1439         const struct json *elem = array->u.array.elems[i];
1440         sset_add(sset, json_string(elem));
1441     }
1442 }
1443
1444 /* Clears and replaces 'remotes' and 'dbnames' by a configuration read from
1445  * 'config_file', which must have been previously written by save_config(). */
1446 static void
1447 load_config(FILE *config_file, struct sset *remotes, struct sset *db_filenames)
1448 {
1449     struct json *json;
1450
1451     if (fseek(config_file, 0, SEEK_SET) != 0) {
1452         VLOG_FATAL("seek failed in temporary file (%s)", ovs_strerror(errno));
1453     }
1454     json = json_from_stream(config_file);
1455     if (json->type == JSON_STRING) {
1456         VLOG_FATAL("reading json failed (%s)", json_string(json));
1457     }
1458     ovs_assert(json->type == JSON_OBJECT);
1459
1460     sset_from_json(remotes, shash_find_data(json_object(json), "remotes"));
1461     sset_from_json(db_filenames,
1462                    shash_find_data(json_object(json), "db_filenames"));
1463     json_destroy(json);
1464 }