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