]> git.proxmox.com Git - ovs.git/blame - ovsdb/ovsdb-server.c
Fix broken link in installation instructions.
[ovs.git] / ovsdb / ovsdb-server.c
CommitLineData
3109b4e1 1/* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2016 Nicira, Inc.
f85f8ebb
BP
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
f85f8ebb
BP
18#include <errno.h>
19#include <getopt.h>
798e1352 20#include <inttypes.h>
f85f8ebb 21#include <signal.h>
ebed9f78 22#include <sys/stat.h>
eb077b26 23#include <unistd.h>
f85f8ebb 24
0b1fae1b 25#include "column.h"
f85f8ebb
BP
26#include "command-line.h"
27#include "daemon.h"
29701194 28#include "dirs.h"
3e8a2ad1 29#include "openvswitch/dynamic-string.h"
8a777cf6 30#include "fatal-signal.h"
bd06962a 31#include "file.h"
da897f41 32#include "hash.h"
ee89ea7b 33#include "openvswitch/json.h"
f85f8ebb
BP
34#include "jsonrpc.h"
35#include "jsonrpc-server.h"
b19bab5b 36#include "openvswitch/list.h"
0d085684 37#include "memory.h"
e0c73ed1 38#include "monitor.h"
da897f41 39#include "ovsdb.h"
0b1fae1b
BP
40#include "ovsdb-data.h"
41#include "ovsdb-types.h"
f85f8ebb
BP
42#include "ovsdb-error.h"
43#include "poll-loop.h"
44#include "process.h"
ae671c5f 45#include "replication.h"
0b1fae1b 46#include "row.h"
0d085684 47#include "simap.h"
ee89ea7b 48#include "openvswitch/shash.h"
9467fe62 49#include "stream-ssl.h"
f85f8ebb 50#include "stream.h"
b3c01ed3 51#include "sset.h"
0b1fae1b 52#include "table.h"
f85f8ebb 53#include "timeval.h"
0b3e7a8b 54#include "transaction.h"
f85f8ebb
BP
55#include "trigger.h"
56#include "util.h"
57#include "unixctl.h"
97a3c435 58#include "perf-counter.h"
e6211adc 59#include "openvswitch/vlog.h"
5136ce49 60
d98e6007 61VLOG_DEFINE_THIS_MODULE(ovsdb_server);
f85f8ebb 62
6ab3dd96
AZ
63struct db {
64 /* Initialized in main(). */
65 char *filename;
66 struct ovsdb_file *file;
67 struct ovsdb *db;
68
69 /* Only used by update_remote_status(). */
70 struct ovsdb_txn *txn;
71};
b4e8d170 72
78876719
BP
73/* SSL configuration. */
74static char *private_key_file;
75static char *certificate_file;
76static char *ca_cert_file;
e18a1d08
ER
77static char *ssl_protocols;
78static char *ssl_ciphers;
78876719
BP
79static bool bootstrap_ca_cert;
80
aa78de9d 81static unixctl_cb_func ovsdb_server_exit;
ada496b5 82static unixctl_cb_func ovsdb_server_compact;
31d0b6c9 83static unixctl_cb_func ovsdb_server_reconnect;
97a3c435
AZ
84static unixctl_cb_func ovsdb_server_perf_counters_clear;
85static unixctl_cb_func ovsdb_server_perf_counters_show;
c383f3bf 86static unixctl_cb_func ovsdb_server_disable_monitor_cond;
f53d7518
AZ
87static unixctl_cb_func ovsdb_server_set_active_ovsdb_server;
88static unixctl_cb_func ovsdb_server_get_active_ovsdb_server;
89static unixctl_cb_func ovsdb_server_connect_active_ovsdb_server;
90static unixctl_cb_func ovsdb_server_disconnect_active_ovsdb_server;
60e0cd04
AZ
91static unixctl_cb_func ovsdb_server_set_sync_exclude_tables;
92static unixctl_cb_func ovsdb_server_get_sync_exclude_tables;
93static unixctl_cb_func ovsdb_server_get_sync_status;
aa78de9d 94
0a3b723b 95struct server_config {
b421d2af 96 struct sset *remotes;
eeb36a52 97 struct shash *all_dbs;
5f36127e 98 FILE *config_tmpfile;
60e0cd04
AZ
99 char **sync_from;
100 char **sync_exclude;
101 bool *is_backup;
0a3b723b 102 struct ovsdb_jsonrpc_server *jsonrpc;
b421d2af
BP
103};
104static unixctl_cb_func ovsdb_server_add_remote;
105static unixctl_cb_func ovsdb_server_remove_remote;
106static unixctl_cb_func ovsdb_server_list_remotes;
107
0a3b723b
BP
108static unixctl_cb_func ovsdb_server_add_database;
109static unixctl_cb_func ovsdb_server_remove_database;
110static unixctl_cb_func ovsdb_server_list_databases;
111
112static char *open_db(struct server_config *config, const char *filename);
03093a4f 113static void close_db(struct db *db);
eeb36a52 114
b4e8d170 115static void parse_options(int *argc, char **argvp[],
b3c01ed3 116 struct sset *remotes, char **unixctl_pathp,
60e0cd04
AZ
117 char **run_command, char **sync_from,
118 char **sync_exclude, bool *is_backup);
cab50449 119OVS_NO_RETURN static void usage(void);
f85f8ebb 120
0a3b723b
BP
121static char *reconfigure_remotes(struct ovsdb_jsonrpc_server *,
122 const struct shash *all_dbs,
123 struct sset *remotes);
124static char *reconfigure_ssl(const struct shash *all_dbs);
125static void report_error_if_changed(char *error, char **last_errorp);
0b1fae1b 126
0b3e7a8b 127static void update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
b3c01ed3 128 const struct sset *remotes,
eeb36a52 129 struct shash *all_dbs);
0b3e7a8b 130
0a3b723b 131static void save_config__(FILE *config_file, const struct sset *remotes,
60e0cd04
AZ
132 const struct sset *db_filenames,
133 const char *sync_from, const char *sync_exclude,
134 bool is_backup);
0a3b723b
BP
135static void save_config(struct server_config *);
136static void load_config(FILE *config_file, struct sset *remotes,
60e0cd04
AZ
137 struct sset *db_filenames, char **sync_from,
138 char **sync_exclude, bool *is_backup);
5f36127e 139
6ab3dd96 140static void
60e0cd04 141ovsdb_replication_init(const char *sync_from, const char *exclude,
05ac209a 142 struct shash *all_dbs, const struct uuid *server_uuid)
6ab3dd96 143{
05ac209a 144 replication_init(sync_from, exclude, server_uuid);
6ab3dd96
AZ
145 struct shash_node *node;
146 SHASH_FOR_EACH (node, all_dbs) {
147 struct db *db = node->data;
23c16b51 148 replication_add_local_db(db->db->schema->name, db->db);
6ab3dd96
AZ
149 }
150}
151
513a3f64
ES
152static void
153main_loop(struct ovsdb_jsonrpc_server *jsonrpc, struct shash *all_dbs,
154 struct unixctl_server *unixctl, struct sset *remotes,
60e0cd04 155 struct process *run_process, bool *exiting, bool *is_backup)
513a3f64
ES
156{
157 char *remotes_error, *ssl_error;
158 struct shash_node *node;
159 long long int status_timer = LLONG_MIN;
60e0cd04 160 bool last_role = *is_backup;
513a3f64
ES
161
162 *exiting = false;
163 ssl_error = NULL;
164 remotes_error = NULL;
165 while (!*exiting) {
166 memory_run();
167 if (memory_should_report()) {
168 struct simap usage;
169
170 simap_init(&usage);
171 ovsdb_jsonrpc_server_get_memory_usage(jsonrpc, &usage);
e0c73ed1 172 ovsdb_monitor_get_memory_usage(&usage);
513a3f64
ES
173 SHASH_FOR_EACH(node, all_dbs) {
174 struct db *db = node->data;
175 ovsdb_get_memory_usage(db->db, &usage);
176 }
177 memory_report(&usage);
178 simap_destroy(&usage);
179 }
180
181 /* Run unixctl_server_run() before reconfigure_remotes() because
182 * ovsdb-server/add-remote and ovsdb-server/remove-remote can change
183 * the set of remotes that reconfigure_remotes() uses. */
184 unixctl_server_run(unixctl);
185
60e0cd04
AZ
186 /* In ovsdb-server's role (active or backup) has changed, restart
187 * the ovsdb jsonrpc server. */
188 if (last_role != *is_backup) {
189 bool read_only = last_role = *is_backup;
190 ovsdb_jsonrpc_server_reconnect(jsonrpc, read_only);
e51879e9
AZ
191 }
192
513a3f64
ES
193 report_error_if_changed(
194 reconfigure_remotes(jsonrpc, all_dbs, remotes),
195 &remotes_error);
196 report_error_if_changed(reconfigure_ssl(all_dbs), &ssl_error);
197 ovsdb_jsonrpc_server_run(jsonrpc);
198
60e0cd04 199 if (*is_backup) {
23c16b51
AZ
200 replication_run();
201 if (!replication_is_alive()) {
ac5d315c
AZ
202 disconnect_active_server();
203 *is_backup = false;
23c16b51 204 }
ae671c5f
MC
205 }
206
513a3f64
ES
207 SHASH_FOR_EACH(node, all_dbs) {
208 struct db *db = node->data;
209 ovsdb_trigger_run(db->db, time_msec());
210 }
211 if (run_process) {
212 process_run();
213 if (process_exited(run_process)) {
214 *exiting = true;
215 }
216 }
217
ae671c5f 218 /* update Manager status(es) every 2.5 seconds */
513a3f64 219 if (time_msec() >= status_timer) {
ae671c5f 220 status_timer = time_msec() + 2500;
513a3f64
ES
221 update_remote_status(jsonrpc, remotes, all_dbs);
222 }
223
224 memory_wait();
60e0cd04 225 if (*is_backup) {
8c945cec
AZ
226 replication_wait();
227 }
23c16b51 228
513a3f64
ES
229 ovsdb_jsonrpc_server_wait(jsonrpc);
230 unixctl_server_wait(unixctl);
231 SHASH_FOR_EACH(node, all_dbs) {
232 struct db *db = node->data;
233 ovsdb_trigger_wait(db->db, time_msec());
234 }
235 if (run_process) {
236 process_wait(run_process);
237 }
238 if (*exiting) {
239 poll_immediate_wake();
240 }
241 poll_timer_wait_until(status_timer);
242 poll_block();
243 if (should_service_stop()) {
244 *exiting = true;
245 }
246 }
247
b396293a 248 free(remotes_error);
513a3f64
ES
249}
250
f85f8ebb
BP
251int
252main(int argc, char *argv[])
253{
aa78de9d 254 char *unixctl_path = NULL;
475afa1b 255 char *run_command = NULL;
f85f8ebb
BP
256 struct unixctl_server *unixctl;
257 struct ovsdb_jsonrpc_server *jsonrpc;
0a3b723b 258 struct sset remotes, db_filenames;
60e0cd04
AZ
259 char *sync_from, *sync_exclude;
260 bool is_backup;
0a3b723b 261 const char *db_filename;
475afa1b 262 struct process *run_process;
aa78de9d 263 bool exiting;
f85f8ebb 264 int retval;
5f36127e 265 FILE *config_tmpfile;
0a3b723b 266 struct server_config server_config;
eeb36a52 267 struct shash all_dbs;
03093a4f 268 struct shash_node *node, *next;
0a3b723b 269 char *error;
b4e8d170
BP
270 int i;
271
5f383751 272 ovs_cmdl_proctitle_init(argc, argv);
f85f8ebb 273 set_program_name(argv[0]);
42dd41ef 274 service_start(&argc, &argv);
8a777cf6 275 fatal_ignore_sigpipe();
f85f8ebb
BP
276 process_init();
277
60e0cd04
AZ
278 bool active = false;
279 parse_options(&argc, &argv, &remotes, &unixctl_path, &run_command,
280 &sync_from, &sync_exclude, &active);
281 is_backup = sync_from && !active;
282
e91b927d 283 daemon_become_new_user(false);
f85f8ebb 284
5f36127e
BP
285 /* Create and initialize 'config_tmpfile' as a temporary file to hold
286 * ovsdb-server's most basic configuration, and then save our initial
287 * configuration to it. When --monitor is used, this preserves the effects
288 * of ovs-appctl commands such as ovsdb-server/add-remote (which saves the
289 * new configuration) across crashes. */
290 config_tmpfile = tmpfile();
291 if (!config_tmpfile) {
292 ovs_fatal(errno, "failed to create temporary file");
293 }
0a3b723b
BP
294
295 sset_init(&db_filenames);
296 if (argc > 0) {
297 for (i = 0; i < argc; i++) {
298 sset_add(&db_filenames, argv[i]);
299 }
300 } else {
301 char *default_db = xasprintf("%s/conf.db", ovs_dbdir());
302 sset_add(&db_filenames, default_db);
303 free(default_db);
304 }
305
306 server_config.remotes = &remotes;
307 server_config.config_tmpfile = config_tmpfile;
308
60e0cd04
AZ
309 save_config__(config_tmpfile, &remotes, &db_filenames, sync_from,
310 sync_exclude, is_backup);
5f36127e 311
e91b927d 312 daemonize_start(false);
eb077b26 313
5f36127e 314 /* Load the saved config. */
60e0cd04
AZ
315 load_config(config_tmpfile, &remotes, &db_filenames, &sync_from,
316 &sync_exclude, &is_backup);
317
318 /* Start ovsdb jsonrpc server. When running as a backup server,
319 * jsonrpc connections are read only. Otherwise, both read
320 * and write transactions are allowed. */
321 jsonrpc = ovsdb_jsonrpc_server_create(is_backup);
eeb36a52 322
0a3b723b
BP
323 shash_init(&all_dbs);
324 server_config.all_dbs = &all_dbs;
325 server_config.jsonrpc = jsonrpc;
60e0cd04
AZ
326 server_config.sync_from = &sync_from;
327 server_config.sync_exclude = &sync_exclude;
328 server_config.is_backup = &is_backup;
7ef28119
WT
329
330 perf_counters_init();
331
0a3b723b
BP
332 SSET_FOR_EACH (db_filename, &db_filenames) {
333 error = open_db(&server_config, db_filename);
334 if (error) {
335 ovs_fatal(0, "%s", error);
336 }
b4e8d170
BP
337 }
338
0a3b723b
BP
339 error = reconfigure_remotes(jsonrpc, &all_dbs, &remotes);
340 if (!error) {
341 error = reconfigure_ssl(&all_dbs);
342 }
343 if (error) {
344 ovs_fatal(0, "%s", error);
345 }
f85f8ebb 346
aa78de9d 347 retval = unixctl_server_create(unixctl_path, &unixctl);
f85f8ebb 348 if (retval) {
4d12270a 349 exit(EXIT_FAILURE);
f85f8ebb
BP
350 }
351
475afa1b
BP
352 if (run_command) {
353 char *run_argv[4];
354
355 run_argv[0] = "/bin/sh";
356 run_argv[1] = "-c";
357 run_argv[2] = run_command;
358 run_argv[3] = NULL;
359
e1208bc4 360 retval = process_start(run_argv, &run_process);
475afa1b
BP
361 if (retval) {
362 ovs_fatal(retval, "%s: process failed to start", run_command);
363 }
364 } else {
365 run_process = NULL;
366 }
367
95440284 368 daemonize_complete();
aa78de9d 369
08b9b190
EJ
370 if (!run_command) {
371 /* ovsdb-server is usually a long-running process, in which case it
372 * makes plenty of sense to log the version, but --run makes
373 * ovsdb-server more like a command-line tool, so skip it. */
374 VLOG_INFO("%s (Open vSwitch) %s", program_name, VERSION);
375 }
9dbc190c 376
0e15264f 377 unixctl_command_register("exit", "", 0, 0, ovsdb_server_exit, &exiting);
b4e8d170 378 unixctl_command_register("ovsdb-server/compact", "", 0, 1,
eeb36a52 379 ovsdb_server_compact, &all_dbs);
0e15264f 380 unixctl_command_register("ovsdb-server/reconnect", "", 0, 0,
7ff2009a 381 ovsdb_server_reconnect, jsonrpc);
aa78de9d 382
b421d2af 383 unixctl_command_register("ovsdb-server/add-remote", "REMOTE", 1, 1,
0a3b723b 384 ovsdb_server_add_remote, &server_config);
b421d2af 385 unixctl_command_register("ovsdb-server/remove-remote", "REMOTE", 1, 1,
0a3b723b 386 ovsdb_server_remove_remote, &server_config);
b421d2af
BP
387 unixctl_command_register("ovsdb-server/list-remotes", "", 0, 0,
388 ovsdb_server_list_remotes, &remotes);
389
0a3b723b
BP
390 unixctl_command_register("ovsdb-server/add-db", "DB", 1, 1,
391 ovsdb_server_add_database, &server_config);
392 unixctl_command_register("ovsdb-server/remove-db", "DB", 1, 1,
393 ovsdb_server_remove_database, &server_config);
394 unixctl_command_register("ovsdb-server/list-dbs", "", 0, 0,
395 ovsdb_server_list_databases, &all_dbs);
97a3c435
AZ
396 unixctl_command_register("ovsdb-server/perf-counters-show", "", 0, 0,
397 ovsdb_server_perf_counters_show, NULL);
398 unixctl_command_register("ovsdb-server/perf-counters-clear", "", 0, 0,
399 ovsdb_server_perf_counters_clear, NULL);
60e0cd04
AZ
400 unixctl_command_register("ovsdb-server/set-active-ovsdb-server", "", 1, 1,
401 ovsdb_server_set_active_ovsdb_server,
402 &server_config);
f53d7518 403 unixctl_command_register("ovsdb-server/get-active-ovsdb-server", "", 0, 0,
60e0cd04
AZ
404 ovsdb_server_get_active_ovsdb_server,
405 &server_config);
e51879e9
AZ
406 unixctl_command_register("ovsdb-server/connect-active-ovsdb-server", "",
407 0, 0, ovsdb_server_connect_active_ovsdb_server,
60e0cd04 408 &server_config);
e51879e9
AZ
409 unixctl_command_register("ovsdb-server/disconnect-active-ovsdb-server", "",
410 0, 0, ovsdb_server_disconnect_active_ovsdb_server,
60e0cd04
AZ
411 &server_config);
412 unixctl_command_register("ovsdb-server/set-sync-exclude-tables", "",
413 0, 1, ovsdb_server_set_sync_exclude_tables,
414 &server_config);
415 unixctl_command_register("ovsdb-server/get-sync-exclude-tables", "",
416 0, 0, ovsdb_server_get_sync_exclude_tables,
e51879e9 417 NULL);
60e0cd04
AZ
418 unixctl_command_register("ovsdb-server/sync-status", "",
419 0, 0, ovsdb_server_get_sync_status,
420 &server_config);
9dc05cdc 421
e47cb14c 422 /* Simulate the behavior of OVS release prior to version 2.5 that
c383f3bf
LS
423 * does not support the monitor_cond method. */
424 unixctl_command_register("ovsdb-server/disable-monitor-cond", "", 0, 0,
425 ovsdb_server_disable_monitor_cond, jsonrpc);
e47cb14c 426
60e0cd04 427 if (is_backup) {
05ac209a
AZ
428 const struct uuid *server_uuid;
429 server_uuid = ovsdb_jsonrpc_server_get_uuid(jsonrpc);
430 ovsdb_replication_init(sync_from, sync_exclude, &all_dbs, server_uuid);
6ab3dd96
AZ
431 }
432
60e0cd04
AZ
433 main_loop(jsonrpc, &all_dbs, unixctl, &remotes, run_process, &exiting,
434 &is_backup);
0d085684 435
23935e8b 436 ovsdb_jsonrpc_server_destroy(jsonrpc);
03093a4f 437 SHASH_FOR_EACH_SAFE(node, next, &all_dbs) {
eeb36a52 438 struct db *db = node->data;
03093a4f
RW
439 close_db(db);
440 shash_delete(&all_dbs, node);
b4e8d170 441 }
d557df96 442 shash_destroy(&all_dbs);
b3c01ed3 443 sset_destroy(&remotes);
03093a4f 444 sset_destroy(&db_filenames);
60e0cd04
AZ
445 free(sync_from);
446 free(sync_exclude);
23935e8b 447 unixctl_server_destroy(unixctl);
3109b4e1 448 replication_destroy();
f85f8ebb 449
475afa1b
BP
450 if (run_process && process_exited(run_process)) {
451 int status = process_status(run_process);
452 if (status) {
453 ovs_fatal(0, "%s: child exited, %s",
454 run_command, process_status_msg(status));
455 }
456 }
97a3c435 457 perf_counters_destroy();
42dd41ef 458 service_stop();
f85f8ebb
BP
459 return 0;
460}
461
ebed9f78
BP
462/* Returns true if 'filename' is known to be already open as a database,
463 * false if not.
464 *
465 * "False negatives" are possible. */
466static bool
467is_already_open(struct server_config *config OVS_UNUSED,
468 const char *filename OVS_UNUSED)
469{
470#ifndef _WIN32
471 struct stat s;
472
473 if (!stat(filename, &s)) {
474 struct shash_node *node;
475
476 SHASH_FOR_EACH (node, config->all_dbs) {
477 struct db *db = node->data;
478 struct stat s2;
479
480 if (!stat(db->filename, &s2)
481 && s.st_dev == s2.st_dev
482 && s.st_ino == s2.st_ino) {
483 return true;
484 }
485 }
486 }
487#endif /* !_WIN32 */
488
489 return false;
490}
491
03093a4f
RW
492static void
493close_db(struct db *db)
494{
495 ovsdb_destroy(db->db);
496 free(db->filename);
497 free(db);
498}
499
0a3b723b
BP
500static char *
501open_db(struct server_config *config, const char *filename)
eeb36a52 502{
0a3b723b
BP
503 struct ovsdb_error *db_error;
504 struct db *db;
505 char *error;
eeb36a52 506
ebed9f78
BP
507 /* If we know that the file is already open, return a good error message.
508 * Otherwise, if the file is open, we'll fail later on with a harder to
509 * interpret file locking error. */
510 if (is_already_open(config, filename)) {
511 return xasprintf("%s: already open", filename);
512 }
513
0a3b723b
BP
514 db = xzalloc(sizeof *db);
515 db->filename = xstrdup(filename);
eeb36a52 516
0a3b723b
BP
517 db_error = ovsdb_file_open(db->filename, false, &db->db, &db->file);
518 if (db_error) {
519 error = ovsdb_error_to_string(db_error);
520 } else if (!ovsdb_jsonrpc_server_add_db(config->jsonrpc, db->db)) {
521 error = xasprintf("%s: duplicate database name", db->db->schema->name);
522 } else {
523 shash_add_assert(config->all_dbs, db->db->schema->name, db);
524 return NULL;
eeb36a52
GS
525 }
526
0a3b723b 527 ovsdb_error_destroy(db_error);
03093a4f 528 close_db(db);
0a3b723b 529 return error;
eeb36a52
GS
530}
531
6ab3dd96
AZ
532static const struct db *
533find_db(const struct shash *all_dbs, const char *db_name)
534{
535 struct shash_node *node;
536
537 SHASH_FOR_EACH (node, all_dbs) {
538 struct db *db = node->data;
539 if (!strcmp(db->db->schema->name, db_name)) {
540 return db;
541 }
542 }
543
544 return NULL;
545}
546
cab50449 547static char * OVS_WARN_UNUSED_RESULT
eeb36a52 548parse_db_column__(const struct shash *all_dbs,
c02cf07b
BP
549 const char *name_, char *name,
550 const struct db **dbp,
551 const struct ovsdb_table **tablep,
552 const struct ovsdb_column **columnp)
0b1fae1b 553{
fb6de52c 554 const char *db_name, *table_name, *column_name;
0b1fae1b
BP
555 const struct ovsdb_column *column;
556 const struct ovsdb_table *table;
b4e8d170 557 const char *tokens[3];
0b1fae1b 558 char *save_ptr = NULL;
b4e8d170 559 const struct db *db;
0b1fae1b 560
c02cf07b
BP
561 *dbp = NULL;
562 *tablep = NULL;
563 *columnp = NULL;
564
b0ef0551 565 strtok_r(name, ":", &save_ptr); /* "db:" */
b4e8d170
BP
566 tokens[0] = strtok_r(NULL, ",", &save_ptr);
567 tokens[1] = strtok_r(NULL, ",", &save_ptr);
568 tokens[2] = strtok_r(NULL, ",", &save_ptr);
fb6de52c 569 if (!tokens[0] || !tokens[1] || !tokens[2]) {
c02cf07b 570 return xasprintf("\"%s\": invalid syntax", name_);
0b1fae1b 571 }
b4e8d170 572
fb6de52c
GS
573 db_name = tokens[0];
574 table_name = tokens[1];
575 column_name = tokens[2];
576
577 db = find_db(all_dbs, tokens[0]);
578 if (!db) {
579 return xasprintf("\"%s\": no database named %s", name_, db_name);
b4e8d170 580 }
0b1fae1b 581
b4e8d170 582 table = ovsdb_get_table(db->db, table_name);
0b1fae1b 583 if (!table) {
c02cf07b 584 return xasprintf("\"%s\": no table named %s", name_, table_name);
0b1fae1b
BP
585 }
586
587 column = ovsdb_table_schema_get_column(table->schema, column_name);
588 if (!column) {
c02cf07b
BP
589 return xasprintf("\"%s\": table \"%s\" has no column \"%s\"",
590 name_, table_name, column_name);
0b1fae1b
BP
591 }
592
b4e8d170 593 *dbp = db;
94db5407
BP
594 *columnp = column;
595 *tablep = table;
c02cf07b 596 return NULL;
94db5407
BP
597}
598
c02cf07b
BP
599/* Returns NULL if successful, otherwise a malloc()'d string describing the
600 * error. */
cab50449 601static char * OVS_WARN_UNUSED_RESULT
eeb36a52 602parse_db_column(const struct shash *all_dbs,
c02cf07b
BP
603 const char *name_,
604 const struct db **dbp,
605 const struct ovsdb_table **tablep,
606 const struct ovsdb_column **columnp)
607{
608 char *name = xstrdup(name_);
eeb36a52 609 char *retval = parse_db_column__(all_dbs, name_, name,
c02cf07b
BP
610 dbp, tablep, columnp);
611 free(name);
612 return retval;
613}
614
615/* Returns NULL if successful, otherwise a malloc()'d string describing the
616 * error. */
cab50449 617static char * OVS_WARN_UNUSED_RESULT
eeb36a52 618parse_db_string_column(const struct shash *all_dbs,
94db5407 619 const char *name,
b4e8d170 620 const struct db **dbp,
94db5407
BP
621 const struct ovsdb_table **tablep,
622 const struct ovsdb_column **columnp)
623{
c02cf07b 624 char *retval;
94db5407 625
eeb36a52 626 retval = parse_db_column(all_dbs, name, dbp, tablep, columnp);
c02cf07b
BP
627 if (retval) {
628 return retval;
629 }
94db5407 630
c02cf07b
BP
631 if ((*columnp)->type.key.type != OVSDB_TYPE_STRING
632 || (*columnp)->type.value.type != OVSDB_TYPE_VOID) {
633 return xasprintf("\"%s\": table \"%s\" column \"%s\" is "
634 "not string or set of strings",
635 name, (*tablep)->schema->name, (*columnp)->name);
78876719
BP
636 }
637
c02cf07b 638 return NULL;
78876719
BP
639}
640
0a3b723b
BP
641static const char *
642query_db_string(const struct shash *all_dbs, const char *name,
643 struct ds *errors)
78876719
BP
644{
645 if (!name || strncmp(name, "db:", 3)) {
646 return name;
647 } else {
648 const struct ovsdb_column *column;
649 const struct ovsdb_table *table;
650 const struct ovsdb_row *row;
b4e8d170 651 const struct db *db;
c02cf07b 652 char *retval;
78876719 653
eeb36a52 654 retval = parse_db_string_column(all_dbs, name,
c02cf07b
BP
655 &db, &table, &column);
656 if (retval) {
0a3b723b 657 ds_put_format(errors, "%s\n", retval);
0ded15d4 658 free(retval);
0a3b723b 659 return NULL;
c02cf07b 660 }
78876719 661
4e8e4213 662 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
78876719
BP
663 const struct ovsdb_datum *datum;
664 size_t i;
665
666 datum = &row->fields[column->index];
667 for (i = 0; i < datum->n; i++) {
668 if (datum->keys[i].string[0]) {
669 return datum->keys[i].string;
670 }
671 }
672 }
673 return NULL;
0b1fae1b 674 }
78876719
BP
675}
676
94db5407
BP
677static struct ovsdb_jsonrpc_options *
678add_remote(struct shash *remotes, const char *target)
679{
680 struct ovsdb_jsonrpc_options *options;
681
682 options = shash_find_data(remotes, target);
683 if (!options) {
f1936eb6 684 options = ovsdb_jsonrpc_default_options(target);
94db5407
BP
685 shash_add(remotes, target, options);
686 }
687
688 return options;
689}
690
0b3e7a8b
AE
691static struct ovsdb_datum *
692get_datum(struct ovsdb_row *row, const char *column_name,
693 const enum ovsdb_atomic_type key_type,
694 const enum ovsdb_atomic_type value_type,
695 const size_t n_max)
94db5407
BP
696{
697 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
698 const struct ovsdb_table_schema *schema = row->table->schema;
699 const struct ovsdb_column *column;
94db5407
BP
700
701 column = ovsdb_table_schema_get_column(schema, column_name);
702 if (!column) {
703 VLOG_DBG_RL(&rl, "Table `%s' has no `%s' column",
704 schema->name, column_name);
0b3e7a8b 705 return NULL;
94db5407
BP
706 }
707
0b3e7a8b
AE
708 if (column->type.key.type != key_type
709 || column->type.value.type != value_type
710 || column->type.n_max != n_max) {
94db5407
BP
711 if (!VLOG_DROP_DBG(&rl)) {
712 char *type_name = ovsdb_type_to_english(&column->type);
713 VLOG_DBG("Table `%s' column `%s' has type %s, not expected "
34582733 714 "key type %s, value type %s, max elements %"PRIuSIZE".",
0b3e7a8b
AE
715 schema->name, column_name, type_name,
716 ovsdb_atomic_type_to_string(key_type),
717 ovsdb_atomic_type_to_string(value_type),
718 n_max);
719 free(type_name);
94db5407 720 }
0b3e7a8b 721 return NULL;
94db5407
BP
722 }
723
0b3e7a8b
AE
724 return &row->fields[column->index];
725}
726
cea15768
EJ
727/* Read string-string key-values from a map. Returns the value associated with
728 * 'key', if found, or NULL */
729static const char *
f125905c 730read_map_string_column(const struct ovsdb_row *row, const char *column_name,
cea15768 731 const char *key)
f125905c
MM
732{
733 const struct ovsdb_datum *datum;
734 union ovsdb_atom *atom_key = NULL, *atom_value = NULL;
735 size_t i;
736
ebc56baa
BP
737 datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name,
738 OVSDB_TYPE_STRING, OVSDB_TYPE_STRING, UINT_MAX);
f125905c
MM
739
740 if (!datum) {
cea15768 741 return NULL;
f125905c
MM
742 }
743
744 for (i = 0; i < datum->n; i++) {
745 atom_key = &datum->keys[i];
746 if (!strcmp(atom_key->string, key)){
747 atom_value = &datum->values[i];
748 break;
749 }
750 }
751
cea15768 752 return atom_value ? atom_value->string : NULL;
f125905c
MM
753}
754
0b3e7a8b
AE
755static const union ovsdb_atom *
756read_column(const struct ovsdb_row *row, const char *column_name,
757 enum ovsdb_atomic_type type)
758{
759 const struct ovsdb_datum *datum;
760
ebc56baa
BP
761 datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name, type,
762 OVSDB_TYPE_VOID, 1);
0b3e7a8b 763 return datum && datum->n ? datum->keys : NULL;
94db5407
BP
764}
765
766static bool
767read_integer_column(const struct ovsdb_row *row, const char *column_name,
768 long long int *integerp)
769{
770 const union ovsdb_atom *atom;
771
772 atom = read_column(row, column_name, OVSDB_TYPE_INTEGER);
773 *integerp = atom ? atom->integer : 0;
774 return atom != NULL;
775}
776
777static bool
778read_string_column(const struct ovsdb_row *row, const char *column_name,
779 const char **stringp)
780{
781 const union ovsdb_atom *atom;
782
783 atom = read_column(row, column_name, OVSDB_TYPE_STRING);
e3c17733 784 *stringp = atom ? atom->string : NULL;
94db5407
BP
785 return atom != NULL;
786}
787
9c1a1182
LR
788static bool
789read_bool_column(const struct ovsdb_row *row, const char *column_name,
790 bool *boolp)
791{
792 const union ovsdb_atom *atom;
793
794 atom = read_column(row, column_name, OVSDB_TYPE_BOOLEAN);
795 *boolp = atom ? atom->boolean : false;
796 return atom != NULL;
797}
798
0b3e7a8b
AE
799static void
800write_bool_column(struct ovsdb_row *row, const char *column_name, bool value)
801{
c824c8a3
BP
802 const struct ovsdb_column *column;
803 struct ovsdb_datum *datum;
0b3e7a8b 804
c824c8a3
BP
805 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
806 datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
807 OVSDB_TYPE_VOID, 1);
0b3e7a8b
AE
808 if (!datum) {
809 return;
810 }
c824c8a3
BP
811
812 if (datum->n != 1) {
813 ovsdb_datum_destroy(datum, &column->type);
814
815 datum->n = 1;
816 datum->keys = xmalloc(sizeof *datum->keys);
817 datum->values = NULL;
818 }
819
0b3e7a8b
AE
820 datum->keys[0].boolean = value;
821}
822
823static void
824write_string_string_column(struct ovsdb_row *row, const char *column_name,
825 char **keys, char **values, size_t n)
826{
827 const struct ovsdb_column *column;
828 struct ovsdb_datum *datum;
829 size_t i;
830
831 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
832 datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
833 UINT_MAX);
834 if (!datum) {
bfe834eb
BP
835 for (i = 0; i < n; i++) {
836 free(keys[i]);
837 free(values[i]);
838 }
0b3e7a8b
AE
839 return;
840 }
841
842 /* Free existing data. */
843 ovsdb_datum_destroy(datum, &column->type);
844
845 /* Allocate space for new values. */
846 datum->n = n;
847 datum->keys = xmalloc(n * sizeof *datum->keys);
848 datum->values = xmalloc(n * sizeof *datum->values);
849
850 for (i = 0; i < n; ++i) {
851 datum->keys[i].string = keys[i];
852 datum->values[i].string = values[i];
853 }
854
855 /* Sort and check constraints. */
856 ovsdb_datum_sort_assert(datum, column->type.key.type);
857}
858
94db5407
BP
859/* Adds a remote and options to 'remotes', based on the Manager table row in
860 * 'row'. */
861static void
862add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
863{
864 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
865 struct ovsdb_jsonrpc_options *options;
866 long long int max_backoff, probe_interval;
9c1a1182 867 bool read_only;
cea15768 868 const char *target, *dscp_string;
94db5407
BP
869
870 if (!read_string_column(row, "target", &target) || !target) {
871 VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
872 row->table->schema->name);
873 return;
874 }
875
876 options = add_remote(remotes, target);
877 if (read_integer_column(row, "max_backoff", &max_backoff)) {
878 options->max_backoff = max_backoff;
879 }
f441c1a8 880 if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
94db5407
BP
881 options->probe_interval = probe_interval;
882 }
9c1a1182
LR
883 if (read_bool_column(row, "read_only", &read_only)) {
884 options->read_only = read_only;
885 }
f125905c 886
cea15768
EJ
887 options->dscp = DSCP_DEFAULT;
888 dscp_string = read_map_string_column(row, "other_config", "dscp");
889 if (dscp_string) {
890 int dscp = atoi(dscp_string);
891 if (dscp >= 0 && dscp <= 63) {
892 options->dscp = dscp;
893 }
894 }
94db5407
BP
895}
896
78876719 897static void
eeb36a52 898query_db_remotes(const char *name, const struct shash *all_dbs,
0a3b723b 899 struct shash *remotes, struct ds *errors)
78876719
BP
900{
901 const struct ovsdb_column *column;
902 const struct ovsdb_table *table;
903 const struct ovsdb_row *row;
b4e8d170 904 const struct db *db;
c02cf07b 905 char *retval;
78876719 906
eeb36a52 907 retval = parse_db_column(all_dbs, name, &db, &table, &column);
c02cf07b 908 if (retval) {
0a3b723b
BP
909 ds_put_format(errors, "%s\n", retval);
910 free(retval);
911 return;
c02cf07b 912 }
0b1fae1b 913
94db5407
BP
914 if (column->type.key.type == OVSDB_TYPE_STRING
915 && column->type.value.type == OVSDB_TYPE_VOID) {
916 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
917 const struct ovsdb_datum *datum;
918 size_t i;
919
920 datum = &row->fields[column->index];
921 for (i = 0; i < datum->n; i++) {
922 add_remote(remotes, datum->keys[i].string);
923 }
924 }
925 } else if (column->type.key.type == OVSDB_TYPE_UUID
926 && column->type.key.u.uuid.refTable
927 && column->type.value.type == OVSDB_TYPE_VOID) {
928 const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
929 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
930 const struct ovsdb_datum *datum;
931 size_t i;
932
933 datum = &row->fields[column->index];
934 for (i = 0; i < datum->n; i++) {
935 const struct ovsdb_row *ref_row;
0b1fae1b 936
94db5407
BP
937 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
938 if (ref_row) {
939 add_manager_options(remotes, ref_row);
940 }
941 }
0b1fae1b
BP
942 }
943 }
944}
945
0b3e7a8b
AE
946static void
947update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
87fcbc60 948 const struct ovsdb_jsonrpc_server *jsonrpc)
0b3e7a8b 949{
87fcbc60 950 struct ovsdb_jsonrpc_remote_status status;
0b3e7a8b
AE
951 struct ovsdb_row *rw_row;
952 const char *target;
798e1352 953 char *keys[9], *values[9];
0b3e7a8b
AE
954 size_t n = 0;
955
956 /* Get the "target" (protocol/host/port) spec. */
957 if (!read_string_column(row, "target", &target)) {
958 /* Bad remote spec or incorrect schema. */
959 return;
960 }
0b3e7a8b 961 rw_row = ovsdb_txn_row_modify(txn, row);
87fcbc60 962 ovsdb_jsonrpc_server_get_remote_status(jsonrpc, target, &status);
0b3e7a8b
AE
963
964 /* Update status information columns. */
87fcbc60 965 write_bool_column(rw_row, "is_connected", status.is_connected);
0b3e7a8b 966
87fcbc60
BP
967 if (status.state) {
968 keys[n] = xstrdup("state");
969 values[n++] = xstrdup(status.state);
970 }
971 if (status.sec_since_connect != UINT_MAX) {
5eda645e 972 keys[n] = xstrdup("sec_since_connect");
87fcbc60 973 values[n++] = xasprintf("%u", status.sec_since_connect);
5eda645e 974 }
87fcbc60 975 if (status.sec_since_disconnect != UINT_MAX) {
5eda645e 976 keys[n] = xstrdup("sec_since_disconnect");
87fcbc60 977 values[n++] = xasprintf("%u", status.sec_since_disconnect);
5eda645e 978 }
87fcbc60 979 if (status.last_error) {
0b3e7a8b
AE
980 keys[n] = xstrdup("last_error");
981 values[n++] =
87fcbc60 982 xstrdup(ovs_retval_to_string(status.last_error));
0b3e7a8b 983 }
da897f41
BP
984 if (status.locks_held && status.locks_held[0]) {
985 keys[n] = xstrdup("locks_held");
986 values[n++] = xstrdup(status.locks_held);
987 }
988 if (status.locks_waiting && status.locks_waiting[0]) {
989 keys[n] = xstrdup("locks_waiting");
990 values[n++] = xstrdup(status.locks_waiting);
991 }
992 if (status.locks_lost && status.locks_lost[0]) {
993 keys[n] = xstrdup("locks_lost");
994 values[n++] = xstrdup(status.locks_lost);
995 }
a11f6164
BP
996 if (status.n_connections > 1) {
997 keys[n] = xstrdup("n_connections");
998 values[n++] = xasprintf("%d", status.n_connections);
999 }
798e1352
BP
1000 if (status.bound_port != htons(0)) {
1001 keys[n] = xstrdup("bound_port");
1002 values[n++] = xasprintf("%"PRIu16, ntohs(status.bound_port));
1003 }
0b3e7a8b 1004 write_string_string_column(rw_row, "status", keys, values, n);
da897f41
BP
1005
1006 ovsdb_jsonrpc_server_free_remote_status(&status);
0b3e7a8b
AE
1007}
1008
1009static void
eeb36a52 1010update_remote_rows(const struct shash *all_dbs,
87fcbc60
BP
1011 const char *remote_name,
1012 const struct ovsdb_jsonrpc_server *jsonrpc)
0b3e7a8b
AE
1013{
1014 const struct ovsdb_table *table, *ref_table;
1015 const struct ovsdb_column *column;
1016 const struct ovsdb_row *row;
b4e8d170 1017 const struct db *db;
c02cf07b 1018 char *retval;
0b3e7a8b
AE
1019
1020 if (strncmp("db:", remote_name, 3)) {
1021 return;
1022 }
1023
eeb36a52 1024 retval = parse_db_column(all_dbs, remote_name, &db, &table, &column);
c02cf07b 1025 if (retval) {
0a3b723b
BP
1026 free(retval);
1027 return;
c02cf07b 1028 }
0b3e7a8b
AE
1029
1030 if (column->type.key.type != OVSDB_TYPE_UUID
1031 || !column->type.key.u.uuid.refTable
1032 || column->type.value.type != OVSDB_TYPE_VOID) {
1033 return;
1034 }
1035
1036 ref_table = column->type.key.u.uuid.refTable;
1037
1038 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
1039 const struct ovsdb_datum *datum;
1040 size_t i;
1041
1042 datum = &row->fields[column->index];
1043 for (i = 0; i < datum->n; i++) {
1044 const struct ovsdb_row *ref_row;
1045
1046 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
1047 if (ref_row) {
b4e8d170 1048 update_remote_row(ref_row, db->txn, jsonrpc);
0b3e7a8b
AE
1049 }
1050 }
1051 }
1052}
1053
1054static void
1055update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
b4e8d170 1056 const struct sset *remotes,
eeb36a52 1057 struct shash *all_dbs)
0b3e7a8b
AE
1058{
1059 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
b3c01ed3 1060 const char *remote;
eeb36a52
GS
1061 struct db *db;
1062 struct shash_node *node;
0b3e7a8b 1063
eeb36a52
GS
1064 SHASH_FOR_EACH(node, all_dbs) {
1065 db = node->data;
1066 db->txn = ovsdb_txn_create(db->db);
b4e8d170 1067 }
0b3e7a8b
AE
1068
1069 /* Iterate over --remote arguments given on command line. */
b3c01ed3 1070 SSET_FOR_EACH (remote, remotes) {
eeb36a52 1071 update_remote_rows(all_dbs, remote, jsonrpc);
0b3e7a8b
AE
1072 }
1073
eeb36a52
GS
1074 SHASH_FOR_EACH(node, all_dbs) {
1075 struct ovsdb_error *error;
1076 db = node->data;
1077 error = ovsdb_txn_commit(db->txn, false);
b4e8d170
BP
1078 if (error) {
1079 VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
1080 ovsdb_error_to_string(error));
1081 ovsdb_error_destroy(error);
1082 }
0b3e7a8b 1083 }
0b3e7a8b
AE
1084}
1085
0a3b723b
BP
1086/* Reconfigures ovsdb-server's remotes based on information in the database. */
1087static char *
1088reconfigure_remotes(struct ovsdb_jsonrpc_server *jsonrpc,
eeb36a52 1089 const struct shash *all_dbs, struct sset *remotes)
0b1fae1b 1090{
0a3b723b 1091 struct ds errors = DS_EMPTY_INITIALIZER;
0b1fae1b 1092 struct shash resolved_remotes;
b3c01ed3 1093 const char *name;
0b1fae1b 1094
78876719 1095 /* Configure remotes. */
0b1fae1b 1096 shash_init(&resolved_remotes);
b3c01ed3 1097 SSET_FOR_EACH (name, remotes) {
0b1fae1b 1098 if (!strncmp(name, "db:", 3)) {
0a3b723b 1099 query_db_remotes(name, all_dbs, &resolved_remotes, &errors);
0b1fae1b 1100 } else {
94db5407 1101 add_remote(&resolved_remotes, name);
0b1fae1b
BP
1102 }
1103 }
1104 ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
88b64974 1105 shash_destroy_free_data(&resolved_remotes);
0b1fae1b 1106
0a3b723b
BP
1107 return errors.string;
1108}
1109
1110static char *
1111reconfigure_ssl(const struct shash *all_dbs)
1112{
1113 struct ds errors = DS_EMPTY_INITIALIZER;
1114 const char *resolved_private_key;
1115 const char *resolved_certificate;
1116 const char *resolved_ca_cert;
e18a1d08
ER
1117 const char *resolved_ssl_protocols;
1118 const char *resolved_ssl_ciphers;
0a3b723b
BP
1119
1120 resolved_private_key = query_db_string(all_dbs, private_key_file, &errors);
1121 resolved_certificate = query_db_string(all_dbs, certificate_file, &errors);
1122 resolved_ca_cert = query_db_string(all_dbs, ca_cert_file, &errors);
e18a1d08
ER
1123 resolved_ssl_protocols = query_db_string(all_dbs, ssl_protocols, &errors);
1124 resolved_ssl_ciphers = query_db_string(all_dbs, ssl_ciphers, &errors);
0a3b723b
BP
1125
1126 stream_ssl_set_key_and_cert(resolved_private_key, resolved_certificate);
1127 stream_ssl_set_ca_cert_file(resolved_ca_cert, bootstrap_ca_cert);
e18a1d08
ER
1128 stream_ssl_set_protocols(resolved_ssl_protocols);
1129 stream_ssl_set_ciphers(resolved_ssl_ciphers);
0a3b723b
BP
1130
1131 return errors.string;
1132}
1133
1134static void
1135report_error_if_changed(char *error, char **last_errorp)
1136{
1137 if (error) {
1138 if (!*last_errorp || strcmp(error, *last_errorp)) {
1139 VLOG_WARN("%s", error);
1140 free(*last_errorp);
1141 *last_errorp = error;
1142 return;
1143 }
1144 free(error);
1145 } else {
1146 free(*last_errorp);
1147 *last_errorp = NULL;
1148 }
78876719 1149}
0b1fae1b 1150
9dc05cdc 1151static void
f53d7518 1152ovsdb_server_set_active_ovsdb_server(struct unixctl_conn *conn,
9dc05cdc 1153 int argc OVS_UNUSED, const char *argv[],
60e0cd04 1154 void *config_)
9dc05cdc 1155{
60e0cd04
AZ
1156 struct server_config *config = config_;
1157
1158 if (*config->sync_from) {
1159 free(*config->sync_from);
1160 }
1161 *config->sync_from = xstrdup(argv[1]);
1162 save_config(config);
1163
9dc05cdc
MC
1164 unixctl_command_reply(conn, NULL);
1165}
1166
1167static void
f53d7518 1168ovsdb_server_get_active_ovsdb_server(struct unixctl_conn *conn,
9dc05cdc
MC
1169 int argc OVS_UNUSED,
1170 const char *argv[] OVS_UNUSED,
60e0cd04 1171 void *config_ )
9dc05cdc 1172{
60e0cd04
AZ
1173 struct server_config *config = config_;
1174
1175 unixctl_command_reply(conn, *config->sync_from);
9dc05cdc
MC
1176}
1177
1178static void
f53d7518 1179ovsdb_server_connect_active_ovsdb_server(struct unixctl_conn *conn,
9dc05cdc
MC
1180 int argc OVS_UNUSED,
1181 const char *argv[] OVS_UNUSED,
60e0cd04 1182 void *config_)
9dc05cdc 1183{
60e0cd04
AZ
1184 struct server_config *config = config_;
1185 char *msg = NULL;
6ab3dd96 1186
60e0cd04
AZ
1187 if ( !*config->sync_from) {
1188 msg = "Unable to connect: active server is not specified.\n";
1189 } else {
05ac209a
AZ
1190 const struct uuid *server_uuid;
1191 server_uuid = ovsdb_jsonrpc_server_get_uuid(config->jsonrpc);
60e0cd04 1192 ovsdb_replication_init(*config->sync_from, *config->sync_exclude,
05ac209a 1193 config->all_dbs, server_uuid);
60e0cd04
AZ
1194 if (!*config->is_backup) {
1195 *config->is_backup = true;
1196 save_config(config);
1197 }
9dc05cdc 1198 }
60e0cd04 1199 unixctl_command_reply(conn, msg);
9dc05cdc
MC
1200}
1201
1202static void
f53d7518 1203ovsdb_server_disconnect_active_ovsdb_server(struct unixctl_conn *conn,
9dc05cdc
MC
1204 int argc OVS_UNUSED,
1205 const char *argv[] OVS_UNUSED,
60e0cd04 1206 void *config_)
9dc05cdc 1207{
60e0cd04
AZ
1208 struct server_config *config = config_;
1209
f53d7518 1210 disconnect_active_server();
60e0cd04
AZ
1211 *config->is_backup = false;
1212 save_config(config);
9dc05cdc
MC
1213 unixctl_command_reply(conn, NULL);
1214}
1215
1216static void
60e0cd04
AZ
1217ovsdb_server_set_sync_exclude_tables(struct unixctl_conn *conn,
1218 int argc OVS_UNUSED,
1219 const char *argv[],
1220 void *config_)
9dc05cdc 1221{
60e0cd04 1222 struct server_config *config = config_;
3109b4e1 1223
60e0cd04 1224 char *err = set_blacklist_tables(argv[1], true);
3109b4e1 1225 if (!err) {
60e0cd04
AZ
1226 free(*config->sync_exclude);
1227 *config->sync_exclude = xstrdup(argv[1]);
1228 save_config(config);
1229 if (*config->is_backup) {
05ac209a
AZ
1230 const struct uuid *server_uuid;
1231 server_uuid = ovsdb_jsonrpc_server_get_uuid(config->jsonrpc);
60e0cd04 1232 ovsdb_replication_init(*config->sync_from, *config->sync_exclude,
05ac209a 1233 config->all_dbs, server_uuid);
3109b4e1
AZ
1234 }
1235 err = set_blacklist_tables(argv[1], false);
1236 }
1237 unixctl_command_reply(conn, err);
1238 free(err);
9dc05cdc
MC
1239}
1240
1241static void
60e0cd04
AZ
1242ovsdb_server_get_sync_exclude_tables(struct unixctl_conn *conn,
1243 int argc OVS_UNUSED,
1244 const char *argv[] OVS_UNUSED,
1245 void *arg_ OVS_UNUSED)
9dc05cdc 1246{
5975d1fc
BP
1247 char *reply = get_blacklist_tables();
1248 unixctl_command_reply(conn, reply);
1249 free(reply);
9dc05cdc
MC
1250}
1251
aa78de9d 1252static void
0e15264f
BP
1253ovsdb_server_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
1254 const char *argv[] OVS_UNUSED,
aa78de9d
BP
1255 void *exiting_)
1256{
1257 bool *exiting = exiting_;
1258 *exiting = true;
bde9f75d 1259 unixctl_command_reply(conn, NULL);
97a3c435
AZ
1260}
1261
1262static void
1263ovsdb_server_perf_counters_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
1264 const char *argv[] OVS_UNUSED,
1265 void *arg_ OVS_UNUSED)
1266{
1267 char *s = perf_counters_to_string();
1268
1269 unixctl_command_reply(conn, s);
1270 free(s);
1271}
1272
1273static void
1274ovsdb_server_perf_counters_clear(struct unixctl_conn *conn, int argc OVS_UNUSED,
1275 const char *argv[] OVS_UNUSED,
1276 void *arg_ OVS_UNUSED)
1277{
1278 perf_counters_clear();
1279 unixctl_command_reply(conn, NULL);
aa78de9d
BP
1280}
1281
c383f3bf 1282/* "ovsdb-server/disable-monitor-cond": makes ovsdb-server drop all of its
e47cb14c 1283 * JSON-RPC connections and reconnect. New sessions will not recognize
c383f3bf 1284 * the 'monitor_cond' method. */
e47cb14c 1285static void
c383f3bf
LS
1286ovsdb_server_disable_monitor_cond(struct unixctl_conn *conn,
1287 int argc OVS_UNUSED,
1288 const char *argv[] OVS_UNUSED,
1289 void *jsonrpc_)
e47cb14c
AZ
1290{
1291 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
60e0cd04 1292 bool read_only = ovsdb_jsonrpc_server_is_read_only(jsonrpc);
e47cb14c 1293
c383f3bf 1294 ovsdb_jsonrpc_disable_monitor_cond();
60e0cd04 1295 ovsdb_jsonrpc_server_reconnect(jsonrpc, read_only);
e47cb14c
AZ
1296 unixctl_command_reply(conn, NULL);
1297}
1298
ada496b5 1299static void
b4e8d170
BP
1300ovsdb_server_compact(struct unixctl_conn *conn, int argc,
1301 const char *argv[], void *dbs_)
ada496b5 1302{
eeb36a52 1303 struct shash *all_dbs = dbs_;
b4e8d170
BP
1304 struct ds reply;
1305 struct db *db;
eeb36a52 1306 struct shash_node *node;
b4e8d170 1307 int n = 0;
ada496b5 1308
b4e8d170 1309 ds_init(&reply);
eeb36a52
GS
1310 SHASH_FOR_EACH(node, all_dbs) {
1311 const char *name;
1312
1313 db = node->data;
1314 name = db->db->schema->name;
b4e8d170
BP
1315
1316 if (argc < 2 || !strcmp(argv[1], name)) {
1317 struct ovsdb_error *error;
1318
1319 VLOG_INFO("compacting %s database by user request", name);
1320
1321 error = ovsdb_file_compact(db->file);
1322 if (error) {
1323 char *s = ovsdb_error_to_string(error);
1324 ds_put_format(&reply, "%s\n", s);
1325 free(s);
9c0639a4 1326 ovsdb_error_destroy(error);
b4e8d170
BP
1327 }
1328
1329 n++;
1330 }
1331 }
1332
1333 if (!n) {
1334 unixctl_command_reply_error(conn, "no database by that name");
1335 } else if (reply.length) {
1336 unixctl_command_reply_error(conn, ds_cstr(&reply));
ada496b5 1337 } else {
b4e8d170 1338 unixctl_command_reply(conn, NULL);
ada496b5 1339 }
b4e8d170 1340 ds_destroy(&reply);
ada496b5
BP
1341}
1342
31d0b6c9
BP
1343/* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
1344 * connections and reconnect. */
1345static void
0e15264f
BP
1346ovsdb_server_reconnect(struct unixctl_conn *conn, int argc OVS_UNUSED,
1347 const char *argv[] OVS_UNUSED, void *jsonrpc_)
31d0b6c9
BP
1348{
1349 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
60e0cd04 1350 bool read_only = ovsdb_jsonrpc_server_is_read_only(jsonrpc);
31d0b6c9 1351
60e0cd04 1352 ovsdb_jsonrpc_server_reconnect(jsonrpc, read_only);
bde9f75d 1353 unixctl_command_reply(conn, NULL);
31d0b6c9
BP
1354}
1355
b421d2af
BP
1356/* "ovsdb-server/add-remote REMOTE": adds REMOTE to the set of remotes that
1357 * ovsdb-server services. */
1358static void
1359ovsdb_server_add_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
0a3b723b 1360 const char *argv[], void *config_)
b421d2af 1361{
0a3b723b 1362 struct server_config *config = config_;
b421d2af
BP
1363 const char *remote = argv[1];
1364
1365 const struct ovsdb_column *column;
1366 const struct ovsdb_table *table;
1367 const struct db *db;
1368 char *retval;
1369
1370 retval = (strncmp("db:", remote, 3)
1371 ? NULL
0a3b723b 1372 : parse_db_column(config->all_dbs, remote,
b421d2af
BP
1373 &db, &table, &column));
1374 if (!retval) {
0a3b723b
BP
1375 if (sset_add(config->remotes, remote)) {
1376 save_config(config);
5f36127e 1377 }
b421d2af
BP
1378 unixctl_command_reply(conn, NULL);
1379 } else {
1380 unixctl_command_reply_error(conn, retval);
1381 free(retval);
1382 }
1383}
1384
1385/* "ovsdb-server/remove-remote REMOTE": removes REMOTE frmo the set of remotes
1386 * that ovsdb-server services. */
1387static void
1388ovsdb_server_remove_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
0a3b723b 1389 const char *argv[], void *config_)
b421d2af 1390{
0a3b723b 1391 struct server_config *config = config_;
b421d2af
BP
1392 struct sset_node *node;
1393
0a3b723b 1394 node = sset_find(config->remotes, argv[1]);
b421d2af 1395 if (node) {
0a3b723b
BP
1396 sset_delete(config->remotes, node);
1397 save_config(config);
b421d2af
BP
1398 unixctl_command_reply(conn, NULL);
1399 } else {
1400 unixctl_command_reply_error(conn, "no such remote");
1401 }
1402}
1403
1404/* "ovsdb-server/list-remotes": outputs a list of configured rmeotes. */
1405static void
1406ovsdb_server_list_remotes(struct unixctl_conn *conn, int argc OVS_UNUSED,
1407 const char *argv[] OVS_UNUSED, void *remotes_)
1408{
1409 struct sset *remotes = remotes_;
1410 const char **list, **p;
1411 struct ds s;
1412
1413 ds_init(&s);
1414
1415 list = sset_sort(remotes);
1416 for (p = list; *p; p++) {
1417 ds_put_format(&s, "%s\n", *p);
1418 }
1419 free(list);
1420
1421 unixctl_command_reply(conn, ds_cstr(&s));
1422 ds_destroy(&s);
1423}
1424
0a3b723b
BP
1425
1426/* "ovsdb-server/add-db DB": adds the DB to ovsdb-server. */
1427static void
1428ovsdb_server_add_database(struct unixctl_conn *conn, int argc OVS_UNUSED,
1429 const char *argv[], void *config_)
1430{
1431 struct server_config *config = config_;
1432 const char *filename = argv[1];
1433 char *error;
1434
1435 error = open_db(config, filename);
1436 if (!error) {
1437 save_config(config);
60e0cd04 1438 if (*config->is_backup) {
05ac209a
AZ
1439 const struct uuid *server_uuid;
1440 server_uuid = ovsdb_jsonrpc_server_get_uuid(config->jsonrpc);
60e0cd04 1441 ovsdb_replication_init(*config->sync_from, *config->sync_exclude,
05ac209a 1442 config->all_dbs, server_uuid);
23c16b51 1443 }
0a3b723b
BP
1444 unixctl_command_reply(conn, NULL);
1445 } else {
1446 unixctl_command_reply_error(conn, error);
1447 free(error);
1448 }
1449}
1450
1451static void
1452ovsdb_server_remove_database(struct unixctl_conn *conn, int argc OVS_UNUSED,
1453 const char *argv[], void *config_)
1454{
1455 struct server_config *config = config_;
1456 struct shash_node *node;
1457 struct db *db;
1458 bool ok;
1459
1460 node = shash_find(config->all_dbs, argv[1]);
1461 if (!node) {
1462 unixctl_command_reply_error(conn, "Failed to find the database.");
1463 return;
1464 }
1465 db = node->data;
1466
1467 ok = ovsdb_jsonrpc_server_remove_db(config->jsonrpc, db->db);
1468 ovs_assert(ok);
1469
03093a4f 1470 close_db(db);
0a3b723b 1471 shash_delete(config->all_dbs, node);
0a3b723b
BP
1472
1473 save_config(config);
60e0cd04 1474 if (*config->is_backup) {
05ac209a
AZ
1475 const struct uuid *server_uuid;
1476 server_uuid = ovsdb_jsonrpc_server_get_uuid(config->jsonrpc);
60e0cd04 1477 ovsdb_replication_init(*config->sync_from, *config->sync_exclude,
05ac209a 1478 config->all_dbs, server_uuid);
23c16b51 1479 }
0a3b723b
BP
1480 unixctl_command_reply(conn, NULL);
1481}
1482
1483static void
1484ovsdb_server_list_databases(struct unixctl_conn *conn, int argc OVS_UNUSED,
1485 const char *argv[] OVS_UNUSED, void *all_dbs_)
1486{
1487 struct shash *all_dbs = all_dbs_;
1488 const struct shash_node **nodes;
1489 struct ds s;
1490 size_t i;
1491
1492 ds_init(&s);
1493
1494 nodes = shash_sort(all_dbs);
1495 for (i = 0; i < shash_count(all_dbs); i++) {
1496 struct db *db = nodes[i]->data;
1497 ds_put_format(&s, "%s\n", db->db->schema->name);
1498 }
1499 free(nodes);
1500
1501 unixctl_command_reply(conn, ds_cstr(&s));
1502 ds_destroy(&s);
1503}
1504
60e0cd04
AZ
1505static void
1506ovsdb_server_get_sync_status(struct unixctl_conn *conn, int argc OVS_UNUSED,
1507 const char *argv[] OVS_UNUSED, void *config_)
1508{
1509 struct server_config *config = config_;
1510 bool is_backup = *config->is_backup;
1511 struct ds ds = DS_EMPTY_INITIALIZER;
1512
1513 ds_put_format(&ds, "state: %s\n", is_backup ? "backup" : "active");
1514
1515 if (is_backup) {
1516 ds_put_and_free_cstr(&ds, replication_status());
1517 }
1518
1519 unixctl_command_reply(conn, ds_cstr(&ds));
1520 ds_destroy(&ds);
1521}
1522
f85f8ebb 1523static void
b4e8d170 1524parse_options(int *argcp, char **argvp[],
60e0cd04
AZ
1525 struct sset *remotes, char **unixctl_pathp, char **run_command,
1526 char **sync_from, char **sync_exclude, bool *active)
f85f8ebb
BP
1527{
1528 enum {
06834871 1529 OPT_REMOTE = UCHAR_MAX + 1,
aa78de9d 1530 OPT_UNIXCTL,
475afa1b 1531 OPT_RUN,
9467fe62 1532 OPT_BOOTSTRAP_CA_CERT,
5bf6cbd6 1533 OPT_PEER_CA_CERT,
ae671c5f 1534 OPT_SYNC_FROM,
7a9d65d2 1535 OPT_SYNC_EXCLUDE,
60e0cd04 1536 OPT_ACTIVE,
f85f8ebb 1537 VLOG_OPTION_ENUMS,
e18a1d08
ER
1538 DAEMON_OPTION_ENUMS,
1539 SSL_OPTION_ENUMS,
f85f8ebb 1540 };
07fc4ed3 1541 static const struct option long_options[] = {
e3c17733
BP
1542 {"remote", required_argument, NULL, OPT_REMOTE},
1543 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
41064650 1544#ifndef _WIN32
e3c17733 1545 {"run", required_argument, NULL, OPT_RUN},
41064650 1546#endif
e3c17733
BP
1547 {"help", no_argument, NULL, 'h'},
1548 {"version", no_argument, NULL, 'V'},
f85f8ebb
BP
1549 DAEMON_LONG_OPTIONS,
1550 VLOG_LONG_OPTIONS,
e3c17733 1551 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
5bf6cbd6 1552 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
e18a1d08 1553 STREAM_SSL_LONG_OPTIONS,
ae671c5f 1554 {"sync-from", required_argument, NULL, OPT_SYNC_FROM},
7a9d65d2 1555 {"sync-exclude-tables", required_argument, NULL, OPT_SYNC_EXCLUDE},
60e0cd04 1556 {"active", no_argument, NULL, OPT_ACTIVE},
e3c17733 1557 {NULL, 0, NULL, 0},
f85f8ebb 1558 };
5f383751 1559 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
b4e8d170
BP
1560 int argc = *argcp;
1561 char **argv = *argvp;
f85f8ebb 1562
60e0cd04
AZ
1563 *sync_from = NULL;
1564 *sync_exclude = NULL;
b3c01ed3 1565 sset_init(remotes);
f85f8ebb
BP
1566 for (;;) {
1567 int c;
1568
1569 c = getopt_long(argc, argv, short_options, long_options, NULL);
1570 if (c == -1) {
1571 break;
1572 }
1573
1574 switch (c) {
0b1fae1b 1575 case OPT_REMOTE:
b3c01ed3 1576 sset_add(remotes, optarg);
f85f8ebb
BP
1577 break;
1578
aa78de9d
BP
1579 case OPT_UNIXCTL:
1580 *unixctl_pathp = optarg;
1581 break;
1582
475afa1b
BP
1583 case OPT_RUN:
1584 *run_command = optarg;
1585 break;
1586
f85f8ebb
BP
1587 case 'h':
1588 usage();
1589
1590 case 'V':
55d5bb44 1591 ovs_print_version(0, 0);
f85f8ebb
BP
1592 exit(EXIT_SUCCESS);
1593
1594 VLOG_OPTION_HANDLERS
1595 DAEMON_OPTION_HANDLERS
f85f8ebb 1596
78876719
BP
1597 case 'p':
1598 private_key_file = optarg;
1599 break;
1600
1601 case 'c':
1602 certificate_file = optarg;
1603 break;
1604
1605 case 'C':
1606 ca_cert_file = optarg;
1607 bootstrap_ca_cert = false;
1608 break;
9467fe62 1609
e18a1d08
ER
1610 case OPT_SSL_PROTOCOLS:
1611 ssl_protocols = optarg;
1612 break;
1613
1614 case OPT_SSL_CIPHERS:
1615 ssl_ciphers = optarg;
1616 break;
1617
9467fe62 1618 case OPT_BOOTSTRAP_CA_CERT:
78876719
BP
1619 ca_cert_file = optarg;
1620 bootstrap_ca_cert = true;
9467fe62 1621 break;
9467fe62 1622
5bf6cbd6
GS
1623 case OPT_PEER_CA_CERT:
1624 stream_ssl_set_peer_ca_cert_file(optarg);
1625 break;
1626
ae671c5f 1627 case OPT_SYNC_FROM:
60e0cd04 1628 *sync_from = xstrdup(optarg);
ae671c5f
MC
1629 break;
1630
3109b4e1
AZ
1631 case OPT_SYNC_EXCLUDE: {
1632 char *err = set_blacklist_tables(optarg, false);
1633 if (err) {
1634 ovs_fatal(0, "%s", err);
1635 }
60e0cd04 1636 *sync_exclude = xstrdup(optarg);
7a9d65d2 1637 break;
3109b4e1 1638 }
60e0cd04
AZ
1639 case OPT_ACTIVE:
1640 *active = true;
1641 break;
7a9d65d2 1642
f85f8ebb
BP
1643 case '?':
1644 exit(EXIT_FAILURE);
1645
1646 default:
1647 abort();
1648 }
1649 }
1650 free(short_options);
1651
b4e8d170
BP
1652 *argcp -= optind;
1653 *argvp += optind;
f85f8ebb
BP
1654}
1655
1656static void
1657usage(void)
1658{
1659 printf("%s: Open vSwitch database server\n"
b4e8d170
BP
1660 "usage: %s [OPTIONS] [DATABASE...]\n"
1661 "where each DATABASE is a database file in ovsdb format.\n"
1662 "The default DATABASE, if none is given, is\n%s/conf.db.\n",
1663 program_name, program_name, ovs_dbdir());
f85f8ebb 1664 printf("\nJSON-RPC options (may be specified any number of times):\n"
0b1fae1b 1665 " --remote=REMOTE connect or listen to REMOTE\n");
9467fe62 1666 stream_usage("JSON-RPC", true, true, true);
f85f8ebb
BP
1667 daemon_usage();
1668 vlog_usage();
ae671c5f 1669 replication_usage();
f85f8ebb 1670 printf("\nOther options:\n"
475afa1b 1671 " --run COMMAND run COMMAND as subprocess then exit\n"
7b38bdc8 1672 " --unixctl=SOCKET override default control socket name\n"
f85f8ebb
BP
1673 " -h, --help display this help message\n"
1674 " -V, --version display version information\n");
f85f8ebb
BP
1675 exit(EXIT_SUCCESS);
1676}
5f36127e 1677\f
0a3b723b
BP
1678static struct json *
1679sset_to_json(const struct sset *sset)
1680{
1681 struct json *array;
1682 const char *s;
1683
1684 array = json_array_create_empty();
1685 SSET_FOR_EACH (s, sset) {
1686 json_array_add(array, json_string_create(s));
1687 }
1688 return array;
1689}
1690
1691/* Truncates and replaces the contents of 'config_file' by a representation of
1692 * 'remotes' and 'db_filenames'. */
5f36127e 1693static void
0a3b723b 1694save_config__(FILE *config_file, const struct sset *remotes,
60e0cd04
AZ
1695 const struct sset *db_filenames, const char *sync_from,
1696 const char *sync_exclude, bool is_backup)
5f36127e 1697{
0a3b723b 1698 struct json *obj;
5f36127e
BP
1699 char *s;
1700
1701 if (ftruncate(fileno(config_file), 0) == -1) {
10a89ef0
BP
1702 VLOG_FATAL("failed to truncate temporary file (%s)",
1703 ovs_strerror(errno));
5f36127e
BP
1704 }
1705
0a3b723b
BP
1706 obj = json_object_create();
1707 json_object_put(obj, "remotes", sset_to_json(remotes));
1708 json_object_put(obj, "db_filenames", sset_to_json(db_filenames));
60e0cd04
AZ
1709 if (sync_from) {
1710 json_object_put(obj, "sync_from", json_string_create(sync_from));
1711 }
1712 if (sync_exclude) {
1713 json_object_put(obj, "sync_exclude",
1714 json_string_create(sync_exclude));
1715 }
1716 json_object_put(obj, "is_backup", json_boolean_create(is_backup));
1717
0a3b723b
BP
1718 s = json_to_string(obj, 0);
1719 json_destroy(obj);
5f36127e
BP
1720
1721 if (fseek(config_file, 0, SEEK_SET) != 0
1722 || fputs(s, config_file) == EOF
1723 || fflush(config_file) == EOF) {
10a89ef0 1724 VLOG_FATAL("failed to write temporary file (%s)", ovs_strerror(errno));
5f36127e
BP
1725 }
1726 free(s);
1727}
1728
0a3b723b
BP
1729/* Truncates and replaces the contents of 'config_file' by a representation of
1730 * 'config'. */
5f36127e 1731static void
0a3b723b
BP
1732save_config(struct server_config *config)
1733{
1734 struct sset db_filenames;
1735 struct shash_node *node;
1736
1737 sset_init(&db_filenames);
1738 SHASH_FOR_EACH (node, config->all_dbs) {
1739 struct db *db = node->data;
1740 sset_add(&db_filenames, db->filename);
1741 }
1742
60e0cd04
AZ
1743 save_config__(config->config_tmpfile, config->remotes, &db_filenames,
1744 *config->sync_from, *config->sync_exclude,
1745 *config->is_backup);
0a3b723b
BP
1746
1747 sset_destroy(&db_filenames);
1748}
1749
1750static void
1751sset_from_json(struct sset *sset, const struct json *array)
5f36127e 1752{
5f36127e
BP
1753 size_t i;
1754
0a3b723b
BP
1755 sset_clear(sset);
1756
1757 ovs_assert(array->type == JSON_ARRAY);
1758 for (i = 0; i < array->u.array.n; i++) {
1759 const struct json *elem = array->u.array.elems[i];
1760 sset_add(sset, json_string(elem));
1761 }
1762}
1763
1764/* Clears and replaces 'remotes' and 'dbnames' by a configuration read from
1765 * 'config_file', which must have been previously written by save_config(). */
1766static void
60e0cd04
AZ
1767load_config(FILE *config_file, struct sset *remotes, struct sset *db_filenames,
1768 char **sync_from, char **sync_exclude, bool *is_backup)
0a3b723b
BP
1769{
1770 struct json *json;
5f36127e
BP
1771
1772 if (fseek(config_file, 0, SEEK_SET) != 0) {
10a89ef0 1773 VLOG_FATAL("seek failed in temporary file (%s)", ovs_strerror(errno));
5f36127e
BP
1774 }
1775 json = json_from_stream(config_file);
1776 if (json->type == JSON_STRING) {
1777 VLOG_FATAL("reading json failed (%s)", json_string(json));
1778 }
0a3b723b
BP
1779 ovs_assert(json->type == JSON_OBJECT);
1780
1781 sset_from_json(remotes, shash_find_data(json_object(json), "remotes"));
1782 sset_from_json(db_filenames,
1783 shash_find_data(json_object(json), "db_filenames"));
60e0cd04
AZ
1784
1785 struct json *string;
1786 string = shash_find_data(json_object(json), "sync_from");
1787 free(*sync_from);
1788 *sync_from = string ? xstrdup(json_string(string)) : NULL;
1789
1790 string = shash_find_data(json_object(json), "sync_exclude");
1791 free(*sync_exclude);
1792 *sync_exclude = string ? xstrdup(json_string(string)) : NULL;
1793
1794 *is_backup = json_boolean(shash_find_data(json_object(json), "is_backup"));
1795
5f36127e
BP
1796 json_destroy(json);
1797}