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