1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2016, 2017 Nicira, Inc.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include "command-line.h"
29 #include "openvswitch/dynamic-string.h"
30 #include "fatal-signal.h"
33 #include "openvswitch/json.h"
35 #include "jsonrpc-server.h"
36 #include "openvswitch/list.h"
40 #include "ovsdb-data.h"
41 #include "ovsdb-types.h"
42 #include "ovsdb-error.h"
43 #include "openvswitch/poll-loop.h"
45 #include "replication.h"
48 #include "openvswitch/shash.h"
49 #include "stream-ssl.h"
55 #include "transaction.h"
59 #include "perf-counter.h"
60 #include "ovsdb-util.h"
61 #include "openvswitch/vlog.h"
63 VLOG_DEFINE_THIS_MODULE(ovsdb_server
);
71 /* SSL configuration. */
72 static char *private_key_file
;
73 static char *certificate_file
;
74 static char *ca_cert_file
;
75 static char *ssl_protocols
;
76 static char *ssl_ciphers
;
77 static bool bootstrap_ca_cert
;
79 static unixctl_cb_func ovsdb_server_exit
;
80 static unixctl_cb_func ovsdb_server_compact
;
81 static unixctl_cb_func ovsdb_server_reconnect
;
82 static unixctl_cb_func ovsdb_server_perf_counters_clear
;
83 static unixctl_cb_func ovsdb_server_perf_counters_show
;
84 static unixctl_cb_func ovsdb_server_disable_monitor_cond
;
85 static unixctl_cb_func ovsdb_server_set_active_ovsdb_server
;
86 static unixctl_cb_func ovsdb_server_get_active_ovsdb_server
;
87 static unixctl_cb_func ovsdb_server_connect_active_ovsdb_server
;
88 static unixctl_cb_func ovsdb_server_disconnect_active_ovsdb_server
;
89 static unixctl_cb_func ovsdb_server_set_active_ovsdb_server_probe_interval
;
90 static unixctl_cb_func ovsdb_server_set_sync_exclude_tables
;
91 static unixctl_cb_func ovsdb_server_get_sync_exclude_tables
;
92 static unixctl_cb_func ovsdb_server_get_sync_status
;
93 static unixctl_cb_func ovsdb_server_get_db_storage_status
;
95 struct server_config
{
97 struct shash
*all_dbs
;
102 int *replication_probe_interval
;
103 struct ovsdb_jsonrpc_server
*jsonrpc
;
105 static unixctl_cb_func ovsdb_server_add_remote
;
106 static unixctl_cb_func ovsdb_server_remove_remote
;
107 static unixctl_cb_func ovsdb_server_list_remotes
;
109 static unixctl_cb_func ovsdb_server_add_database
;
110 static unixctl_cb_func ovsdb_server_remove_database
;
111 static unixctl_cb_func ovsdb_server_list_databases
;
113 static void read_db(struct server_config
*, struct db
*);
114 static struct ovsdb_error
*open_db(struct server_config
*,
115 const char *filename
)
116 OVS_WARN_UNUSED_RESULT
;
117 static void add_server_db(struct server_config
*);
118 static void remove_db(struct server_config
*, struct shash_node
*db
, char *);
119 static void close_db(struct server_config
*, struct db
*, char *);
121 static void parse_options(int argc
, char *argvp
[],
122 struct sset
*db_filenames
, struct sset
*remotes
,
123 char **unixctl_pathp
, char **run_command
,
124 char **sync_from
, char **sync_exclude
,
126 OVS_NO_RETURN
static void usage(void);
128 static char *reconfigure_remotes(struct ovsdb_jsonrpc_server
*,
129 const struct shash
*all_dbs
,
130 struct sset
*remotes
);
131 static char *reconfigure_ssl(const struct shash
*all_dbs
);
132 static void report_error_if_changed(char *error
, char **last_errorp
);
134 static void update_remote_status(const struct ovsdb_jsonrpc_server
*jsonrpc
,
135 const struct sset
*remotes
,
136 struct shash
*all_dbs
);
137 static void update_server_status(struct shash
*all_dbs
);
139 static void save_config__(FILE *config_file
, const struct sset
*remotes
,
140 const struct sset
*db_filenames
,
141 const char *sync_from
, const char *sync_exclude
,
143 static void save_config(struct server_config
*);
144 static void load_config(FILE *config_file
, struct sset
*remotes
,
145 struct sset
*db_filenames
, char **sync_from
,
146 char **sync_exclude
, bool *is_backup
);
149 ovsdb_replication_init(const char *sync_from
, const char *exclude
,
150 struct shash
*all_dbs
, const struct uuid
*server_uuid
,
153 replication_init(sync_from
, exclude
, server_uuid
, probe_interval
);
154 struct shash_node
*node
;
155 SHASH_FOR_EACH (node
, all_dbs
) {
156 struct db
*db
= node
->data
;
157 if (node
->name
[0] != '_' && db
->db
) {
158 replication_add_local_db(node
->name
, db
->db
);
164 log_and_free_error(struct ovsdb_error
*error
)
167 char *s
= ovsdb_error_to_string_free(error
);
174 main_loop(struct server_config
*config
,
175 struct ovsdb_jsonrpc_server
*jsonrpc
, struct shash
*all_dbs
,
176 struct unixctl_server
*unixctl
, struct sset
*remotes
,
177 struct process
*run_process
, bool *exiting
, bool *is_backup
)
179 char *remotes_error
, *ssl_error
;
180 struct shash_node
*node
;
181 long long int status_timer
= LLONG_MIN
;
185 remotes_error
= NULL
;
188 if (memory_should_report()) {
192 ovsdb_jsonrpc_server_get_memory_usage(jsonrpc
, &usage
);
193 ovsdb_monitor_get_memory_usage(&usage
);
194 SHASH_FOR_EACH(node
, all_dbs
) {
195 struct db
*db
= node
->data
;
196 ovsdb_get_memory_usage(db
->db
, &usage
);
198 memory_report(&usage
);
199 simap_destroy(&usage
);
202 /* Run unixctl_server_run() before reconfigure_remotes() because
203 * ovsdb-server/add-remote and ovsdb-server/remove-remote can change
204 * the set of remotes that reconfigure_remotes() uses. */
205 unixctl_server_run(unixctl
);
207 ovsdb_jsonrpc_server_set_read_only(jsonrpc
, *is_backup
);
209 report_error_if_changed(
210 reconfigure_remotes(jsonrpc
, all_dbs
, remotes
),
212 report_error_if_changed(reconfigure_ssl(all_dbs
), &ssl_error
);
213 ovsdb_jsonrpc_server_run(jsonrpc
);
217 if (!replication_is_alive()) {
218 disconnect_active_server();
223 struct shash_node
*next
;
224 SHASH_FOR_EACH_SAFE (node
, next
, all_dbs
) {
225 struct db
*db
= node
->data
;
226 ovsdb_txn_history_run(db
->db
);
227 ovsdb_storage_run(db
->db
->storage
);
229 /* Run triggers after storage_run and read_db to make sure new raft
230 * updates are utilized in current iteration. */
231 if (ovsdb_trigger_run(db
->db
, time_msec())) {
232 /* The message below is currently the only reason to disconnect
234 ovsdb_jsonrpc_server_reconnect(
236 xasprintf("committed %s database schema conversion",
239 if (ovsdb_storage_is_dead(db
->db
->storage
)) {
240 VLOG_INFO("%s: removing database because storage disconnected "
241 "permanently", node
->name
);
242 remove_db(config
, node
,
243 xasprintf("removing database %s because storage "
244 "disconnected permanently", node
->name
));
245 } else if (ovsdb_storage_should_snapshot(db
->db
->storage
)) {
246 log_and_free_error(ovsdb_snapshot(db
->db
));
251 if (process_exited(run_process
)) {
256 /* update Manager status(es) every 2.5 seconds */
257 if (time_msec() >= status_timer
) {
258 status_timer
= time_msec() + 2500;
259 update_remote_status(jsonrpc
, remotes
, all_dbs
);
262 update_server_status(all_dbs
);
269 ovsdb_jsonrpc_server_wait(jsonrpc
);
270 unixctl_server_wait(unixctl
);
271 SHASH_FOR_EACH(node
, all_dbs
) {
272 struct db
*db
= node
->data
;
273 ovsdb_trigger_wait(db
->db
, time_msec());
274 ovsdb_storage_wait(db
->db
->storage
);
275 ovsdb_storage_read_wait(db
->db
->storage
);
278 process_wait(run_process
);
281 poll_immediate_wake();
283 poll_timer_wait_until(status_timer
);
285 if (should_service_stop()) {
294 main(int argc
, char *argv
[])
296 char *unixctl_path
= NULL
;
297 char *run_command
= NULL
;
298 struct unixctl_server
*unixctl
;
299 struct ovsdb_jsonrpc_server
*jsonrpc
;
300 struct sset remotes
, db_filenames
;
301 char *sync_from
, *sync_exclude
;
303 const char *db_filename
;
304 struct process
*run_process
;
307 FILE *config_tmpfile
;
308 struct server_config server_config
;
309 struct shash all_dbs
;
310 struct shash_node
*node
, *next
;
311 int replication_probe_interval
= REPLICATION_DEFAULT_PROBE_INTERVAL
;
313 ovs_cmdl_proctitle_init(argc
, argv
);
314 set_program_name(argv
[0]);
315 service_start(&argc
, &argv
);
316 fatal_ignore_sigpipe();
320 parse_options(argc
, argv
, &db_filenames
, &remotes
, &unixctl_path
,
321 &run_command
, &sync_from
, &sync_exclude
, &active
);
322 is_backup
= sync_from
&& !active
;
324 daemon_become_new_user(false);
326 /* Create and initialize 'config_tmpfile' as a temporary file to hold
327 * ovsdb-server's most basic configuration, and then save our initial
328 * configuration to it. When --monitor is used, this preserves the effects
329 * of ovs-appctl commands such as ovsdb-server/add-remote (which saves the
330 * new configuration) across crashes. */
331 config_tmpfile
= tmpfile();
332 if (!config_tmpfile
) {
333 ovs_fatal(errno
, "failed to create temporary file");
336 server_config
.remotes
= &remotes
;
337 server_config
.config_tmpfile
= config_tmpfile
;
339 save_config__(config_tmpfile
, &remotes
, &db_filenames
, sync_from
,
340 sync_exclude
, is_backup
);
342 daemonize_start(false);
344 /* Load the saved config. */
345 load_config(config_tmpfile
, &remotes
, &db_filenames
, &sync_from
,
346 &sync_exclude
, &is_backup
);
348 /* Start ovsdb jsonrpc server. When running as a backup server,
349 * jsonrpc connections are read only. Otherwise, both read
350 * and write transactions are allowed. */
351 jsonrpc
= ovsdb_jsonrpc_server_create(is_backup
);
353 shash_init(&all_dbs
);
354 server_config
.all_dbs
= &all_dbs
;
355 server_config
.jsonrpc
= jsonrpc
;
356 server_config
.sync_from
= &sync_from
;
357 server_config
.sync_exclude
= &sync_exclude
;
358 server_config
.is_backup
= &is_backup
;
359 server_config
.replication_probe_interval
= &replication_probe_interval
;
361 perf_counters_init();
363 SSET_FOR_EACH (db_filename
, &db_filenames
) {
364 struct ovsdb_error
*error
= open_db(&server_config
, db_filename
);
366 char *s
= ovsdb_error_to_string_free(error
);
367 ovs_fatal(0, "%s", s
);
370 add_server_db(&server_config
);
372 char *error
= reconfigure_remotes(jsonrpc
, &all_dbs
, &remotes
);
374 error
= reconfigure_ssl(&all_dbs
);
377 ovs_fatal(0, "%s", error
);
380 retval
= unixctl_server_create(unixctl_path
, &unixctl
);
388 run_argv
[0] = "/bin/sh";
390 run_argv
[2] = run_command
;
393 retval
= process_start(run_argv
, &run_process
);
395 ovs_fatal(retval
, "%s: process failed to start", run_command
);
401 daemonize_complete();
404 /* ovsdb-server is usually a long-running process, in which case it
405 * makes plenty of sense to log the version, but --run makes
406 * ovsdb-server more like a command-line tool, so skip it. */
407 VLOG_INFO("%s (Open vSwitch) %s", program_name
, VERSION
);
410 unixctl_command_register("exit", "", 0, 0, ovsdb_server_exit
, &exiting
);
411 unixctl_command_register("ovsdb-server/compact", "", 0, 1,
412 ovsdb_server_compact
, &all_dbs
);
413 unixctl_command_register("ovsdb-server/reconnect", "", 0, 0,
414 ovsdb_server_reconnect
, jsonrpc
);
416 unixctl_command_register("ovsdb-server/add-remote", "REMOTE", 1, 1,
417 ovsdb_server_add_remote
, &server_config
);
418 unixctl_command_register("ovsdb-server/remove-remote", "REMOTE", 1, 1,
419 ovsdb_server_remove_remote
, &server_config
);
420 unixctl_command_register("ovsdb-server/list-remotes", "", 0, 0,
421 ovsdb_server_list_remotes
, &remotes
);
423 unixctl_command_register("ovsdb-server/add-db", "DB", 1, 1,
424 ovsdb_server_add_database
, &server_config
);
425 unixctl_command_register("ovsdb-server/remove-db", "DB", 1, 1,
426 ovsdb_server_remove_database
, &server_config
);
427 unixctl_command_register("ovsdb-server/list-dbs", "", 0, 0,
428 ovsdb_server_list_databases
, &all_dbs
);
429 unixctl_command_register("ovsdb-server/perf-counters-show", "", 0, 0,
430 ovsdb_server_perf_counters_show
, NULL
);
431 unixctl_command_register("ovsdb-server/perf-counters-clear", "", 0, 0,
432 ovsdb_server_perf_counters_clear
, NULL
);
433 unixctl_command_register("ovsdb-server/set-active-ovsdb-server", "", 1, 1,
434 ovsdb_server_set_active_ovsdb_server
,
436 unixctl_command_register("ovsdb-server/get-active-ovsdb-server", "", 0, 0,
437 ovsdb_server_get_active_ovsdb_server
,
439 unixctl_command_register("ovsdb-server/connect-active-ovsdb-server", "",
440 0, 0, ovsdb_server_connect_active_ovsdb_server
,
442 unixctl_command_register("ovsdb-server/disconnect-active-ovsdb-server", "",
443 0, 0, ovsdb_server_disconnect_active_ovsdb_server
,
445 unixctl_command_register(
446 "ovsdb-server/set-active-ovsdb-server-probe-interval", "", 1, 1,
447 ovsdb_server_set_active_ovsdb_server_probe_interval
, &server_config
);
448 unixctl_command_register("ovsdb-server/set-sync-exclude-tables", "",
449 0, 1, ovsdb_server_set_sync_exclude_tables
,
451 unixctl_command_register("ovsdb-server/get-sync-exclude-tables", "",
452 0, 0, ovsdb_server_get_sync_exclude_tables
,
454 unixctl_command_register("ovsdb-server/sync-status", "",
455 0, 0, ovsdb_server_get_sync_status
,
457 unixctl_command_register("ovsdb-server/get-db-storage-status", "DB", 1, 1,
458 ovsdb_server_get_db_storage_status
,
461 /* Simulate the behavior of OVS release prior to version 2.5 that
462 * does not support the monitor_cond method. */
463 unixctl_command_register("ovsdb-server/disable-monitor-cond", "", 0, 0,
464 ovsdb_server_disable_monitor_cond
, jsonrpc
);
467 const struct uuid
*server_uuid
;
468 server_uuid
= ovsdb_jsonrpc_server_get_uuid(jsonrpc
);
469 ovsdb_replication_init(sync_from
, sync_exclude
, &all_dbs
, server_uuid
,
470 replication_probe_interval
);
473 main_loop(&server_config
, jsonrpc
, &all_dbs
, unixctl
, &remotes
,
474 run_process
, &exiting
, &is_backup
);
476 SHASH_FOR_EACH_SAFE(node
, next
, &all_dbs
) {
477 struct db
*db
= node
->data
;
478 close_db(&server_config
, db
, NULL
);
479 shash_delete(&all_dbs
, node
);
481 ovsdb_jsonrpc_server_destroy(jsonrpc
);
482 shash_destroy(&all_dbs
);
483 sset_destroy(&remotes
);
484 sset_destroy(&db_filenames
);
487 unixctl_server_destroy(unixctl
);
488 replication_destroy();
490 if (run_process
&& process_exited(run_process
)) {
491 int status
= process_status(run_process
);
493 ovs_fatal(0, "%s: child exited, %s",
494 run_command
, process_status_msg(status
));
497 perf_counters_destroy();
502 /* Returns true if 'filename' is known to be already open as a database,
505 * "False negatives" are possible. */
507 is_already_open(struct server_config
*config OVS_UNUSED
,
508 const char *filename OVS_UNUSED
)
513 if (!stat(filename
, &s
)) {
514 struct shash_node
*node
;
516 SHASH_FOR_EACH (node
, config
->all_dbs
) {
517 struct db
*db
= node
->data
;
520 if (!stat(db
->filename
, &s2
)
521 && s
.st_dev
== s2
.st_dev
522 && s
.st_ino
== s2
.st_ino
) {
533 close_db(struct server_config
*config
, struct db
*db
, char *comment
)
536 ovsdb_jsonrpc_server_remove_db(config
->jsonrpc
, db
->db
, comment
);
537 ovsdb_destroy(db
->db
);
545 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
546 parse_txn(struct server_config
*config
, struct db
*db
,
547 const struct ovsdb_schema
*schema
, const struct json
*txn_json
,
548 const struct uuid
*txnid
)
551 /* We're replacing the schema (and the data). Destroy the database
552 * (first grabbing its storage), then replace it with the new schema.
553 * The transaction must also include the replacement data.
555 * Only clustered database schema changes and snapshot installs
556 * go through this path.
558 ovs_assert(txn_json
);
559 ovs_assert(ovsdb_storage_is_clustered(db
->db
->storage
));
561 struct ovsdb_error
*error
= ovsdb_schema_check_for_ephemeral_columns(
567 if (!db
->db
->schema
||
568 strcmp(schema
->version
, db
->db
->schema
->version
)) {
569 ovsdb_jsonrpc_server_reconnect(
570 config
->jsonrpc
, false,
572 ? xasprintf("database %s schema changed", db
->db
->name
)
573 : xasprintf("database %s connected to storage",
577 ovsdb_replace(db
->db
, ovsdb_create(ovsdb_schema_clone(schema
), NULL
));
579 /* Force update to schema in _Server database. */
580 db
->row_uuid
= UUID_ZERO
;
584 if (!db
->db
->schema
) {
585 return ovsdb_error(NULL
, "%s: data without schema", db
->filename
);
588 struct ovsdb_txn
*txn
;
589 struct ovsdb_error
*error
;
591 error
= ovsdb_file_txn_from_json(db
->db
, txn_json
, false, &txn
);
593 ovsdb_txn_set_txnid(txnid
, txn
);
594 log_and_free_error(ovsdb_txn_replay_commit(txn
));
596 if (!error
&& !uuid_is_zero(txnid
)) {
597 db
->db
->prereq
= *txnid
;
600 ovsdb_storage_unread(db
->db
->storage
);
609 read_db(struct server_config
*config
, struct db
*db
)
611 struct ovsdb_error
*error
;
613 struct ovsdb_schema
*schema
;
614 struct json
*txn_json
;
616 error
= ovsdb_storage_read(db
->db
->storage
, &schema
, &txn_json
,
620 } else if (!schema
&& !txn_json
) {
624 error
= parse_txn(config
, db
, schema
, txn_json
, &txnid
);
625 json_destroy(txn_json
);
626 ovsdb_schema_destroy(schema
);
633 /* Log error but otherwise ignore it. Probably the database just
634 * got truncated due to power failure etc. and we should use its
635 * current contents. */
636 char *msg
= ovsdb_error_to_string_free(error
);
642 add_db(struct server_config
*config
, struct db
*db
)
644 db
->row_uuid
= UUID_ZERO
;
645 shash_add_assert(config
->all_dbs
, db
->db
->name
, db
);
648 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
649 open_db(struct server_config
*config
, const char *filename
)
653 /* If we know that the file is already open, return a good error message.
654 * Otherwise, if the file is open, we'll fail later on with a harder to
655 * interpret file locking error. */
656 if (is_already_open(config
, filename
)) {
657 return ovsdb_error(NULL
, "%s: already open", filename
);
660 struct ovsdb_storage
*storage
;
661 struct ovsdb_error
*error
;
662 error
= ovsdb_storage_open(filename
, true, &storage
);
667 db
= xzalloc(sizeof *db
);
668 db
->filename
= xstrdup(filename
);
670 struct ovsdb_schema
*schema
;
671 if (ovsdb_storage_is_clustered(storage
)) {
674 struct json
*txn_json
;
675 error
= ovsdb_storage_read(storage
, &schema
, &txn_json
, NULL
);
677 ovsdb_storage_close(storage
);
680 ovs_assert(schema
&& !txn_json
);
682 db
->db
= ovsdb_create(schema
, storage
);
683 ovsdb_jsonrpc_server_add_db(config
->jsonrpc
, db
->db
);
685 /* Enable txn history for clustered mode. It is not enabled for other mode
686 * for now, since txn id is available for clustered mode only. */
687 ovsdb_txn_history_init(db
->db
, ovsdb_storage_is_clustered(storage
));
691 error
= (db
->db
->name
[0] == '_'
692 ? ovsdb_error(NULL
, "%s: names beginning with \"_\" are reserved",
694 : shash_find(config
->all_dbs
, db
->db
->name
)
695 ? ovsdb_error(NULL
, "%s: duplicate database name", db
->db
->name
)
698 char *error_s
= ovsdb_error_to_string(error
);
700 xasprintf("cannot complete opening %s database (%s)",
701 db
->db
->name
, error_s
));
710 /* Add the internal _Server database to the server configuration. */
712 add_server_db(struct server_config
*config
)
714 struct json
*schema_json
= json_from_string(
715 #include "ovsdb/_server.ovsschema.inc"
717 ovs_assert(schema_json
->type
== JSON_OBJECT
);
719 struct ovsdb_schema
*schema
;
720 struct ovsdb_error
*error OVS_UNUSED
= ovsdb_schema_from_json(schema_json
,
723 json_destroy(schema_json
);
725 struct db
*db
= xzalloc(sizeof *db
);
726 /* We don't need txn_history for server_db. */
728 db
->filename
= xstrdup("<internal>");
729 db
->db
= ovsdb_create(schema
, ovsdb_storage_create_unbacked());
730 bool ok OVS_UNUSED
= ovsdb_jsonrpc_server_add_db(config
->jsonrpc
, db
->db
);
735 static char * OVS_WARN_UNUSED_RESULT
736 parse_db_column__(const struct shash
*all_dbs
,
737 const char *name_
, char *name
,
738 const struct db
**dbp
,
739 const struct ovsdb_table
**tablep
,
740 const struct ovsdb_column
**columnp
)
742 const char *db_name
, *table_name
, *column_name
;
743 const char *tokens
[3];
744 char *save_ptr
= NULL
;
750 strtok_r(name
, ":", &save_ptr
); /* "db:" */
751 tokens
[0] = strtok_r(NULL
, ",", &save_ptr
);
752 tokens
[1] = strtok_r(NULL
, ",", &save_ptr
);
753 tokens
[2] = strtok_r(NULL
, ",", &save_ptr
);
754 if (!tokens
[0] || !tokens
[1] || !tokens
[2]) {
755 return xasprintf("\"%s\": invalid syntax", name_
);
759 table_name
= tokens
[1];
760 column_name
= tokens
[2];
762 *dbp
= shash_find_data(all_dbs
, tokens
[0]);
764 return xasprintf("\"%s\": no database named %s", name_
, db_name
);
767 *tablep
= ovsdb_get_table((*dbp
)->db
, table_name
);
769 return xasprintf("\"%s\": no table named %s", name_
, table_name
);
772 *columnp
= ovsdb_table_schema_get_column((*tablep
)->schema
, column_name
);
774 return xasprintf("\"%s\": table \"%s\" has no column \"%s\"",
775 name_
, table_name
, column_name
);
781 /* Returns NULL if successful, otherwise a malloc()'d string describing the
783 static char * OVS_WARN_UNUSED_RESULT
784 parse_db_column(const struct shash
*all_dbs
,
786 const struct db
**dbp
,
787 const struct ovsdb_table
**tablep
,
788 const struct ovsdb_column
**columnp
)
790 char *name
= xstrdup(name_
);
791 char *retval
= parse_db_column__(all_dbs
, name_
, name
,
792 dbp
, tablep
, columnp
);
797 /* Returns NULL if successful, otherwise a malloc()'d string describing the
799 static char * OVS_WARN_UNUSED_RESULT
800 parse_db_string_column(const struct shash
*all_dbs
,
802 const struct db
**dbp
,
803 const struct ovsdb_table
**tablep
,
804 const struct ovsdb_column
**columnp
)
808 retval
= parse_db_column(all_dbs
, name
, dbp
, tablep
, columnp
);
813 if ((*columnp
)->type
.key
.type
!= OVSDB_TYPE_STRING
814 || (*columnp
)->type
.value
.type
!= OVSDB_TYPE_VOID
) {
815 return xasprintf("\"%s\": table \"%s\" column \"%s\" is "
816 "not string or set of strings",
817 name
, (*tablep
)->schema
->name
, (*columnp
)->name
);
824 query_db_string(const struct shash
*all_dbs
, const char *name
,
827 if (!name
|| strncmp(name
, "db:", 3)) {
830 const struct ovsdb_column
*column
;
831 const struct ovsdb_table
*table
;
832 const struct ovsdb_row
*row
;
836 retval
= parse_db_string_column(all_dbs
, name
,
837 &db
, &table
, &column
);
839 if (db
&& !db
->db
->schema
) {
840 /* 'db' is a clustered database but it hasn't connected to the
841 * cluster yet, so we can't get anything out of it, not even a
842 * schema. Not really an error. */
844 ds_put_format(errors
, "%s\n", retval
);
850 HMAP_FOR_EACH (row
, hmap_node
, &table
->rows
) {
851 const struct ovsdb_datum
*datum
;
854 datum
= &row
->fields
[column
->index
];
855 for (i
= 0; i
< datum
->n
; i
++) {
856 if (datum
->keys
[i
].string
[0]) {
857 return datum
->keys
[i
].string
;
865 static struct ovsdb_jsonrpc_options
*
866 add_remote(struct shash
*remotes
, const char *target
)
868 struct ovsdb_jsonrpc_options
*options
;
870 options
= shash_find_data(remotes
, target
);
872 options
= ovsdb_jsonrpc_default_options(target
);
873 shash_add(remotes
, target
, options
);
880 free_remotes(struct shash
*remotes
)
883 struct shash_node
*node
;
885 SHASH_FOR_EACH (node
, remotes
) {
886 struct ovsdb_jsonrpc_options
*options
= node
->data
;
889 shash_destroy_free_data(remotes
);
893 /* Adds a remote and options to 'remotes', based on the Manager table row in
896 add_manager_options(struct shash
*remotes
, const struct ovsdb_row
*row
)
898 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
899 struct ovsdb_jsonrpc_options
*options
;
900 long long int max_backoff
, probe_interval
;
902 const char *target
, *dscp_string
, *role
;
904 if (!ovsdb_util_read_string_column(row
, "target", &target
) || !target
) {
905 VLOG_INFO_RL(&rl
, "Table `%s' has missing or invalid `target' column",
906 row
->table
->schema
->name
);
910 options
= add_remote(remotes
, target
);
911 if (ovsdb_util_read_integer_column(row
, "max_backoff", &max_backoff
)) {
912 options
->max_backoff
= max_backoff
;
914 if (ovsdb_util_read_integer_column(row
, "inactivity_probe",
916 options
->probe_interval
= probe_interval
;
918 if (ovsdb_util_read_bool_column(row
, "read_only", &read_only
)) {
919 options
->read_only
= read_only
;
923 options
->role
= NULL
;
924 if (ovsdb_util_read_string_column(row
, "role", &role
) && role
) {
925 options
->role
= xstrdup(role
);
928 options
->dscp
= DSCP_DEFAULT
;
929 dscp_string
= ovsdb_util_read_map_string_column(row
, "other_config",
932 int dscp
= atoi(dscp_string
);
933 if (dscp
>= 0 && dscp
<= 63) {
934 options
->dscp
= dscp
;
940 query_db_remotes(const char *name
, const struct shash
*all_dbs
,
941 struct shash
*remotes
, struct ds
*errors
)
943 const struct ovsdb_column
*column
;
944 const struct ovsdb_table
*table
;
945 const struct ovsdb_row
*row
;
949 retval
= parse_db_column(all_dbs
, name
, &db
, &table
, &column
);
951 if (db
&& !db
->db
->schema
) {
952 /* 'db' is a clustered database but it hasn't connected to the
953 * cluster yet, so we can't get anything out of it, not even a
954 * schema. Not really an error. */
956 ds_put_format(errors
, "%s\n", retval
);
962 if (column
->type
.key
.type
== OVSDB_TYPE_STRING
963 && column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
964 HMAP_FOR_EACH (row
, hmap_node
, &table
->rows
) {
965 const struct ovsdb_datum
*datum
;
968 datum
= &row
->fields
[column
->index
];
969 for (i
= 0; i
< datum
->n
; i
++) {
970 add_remote(remotes
, datum
->keys
[i
].string
);
973 } else if (column
->type
.key
.type
== OVSDB_TYPE_UUID
974 && column
->type
.key
.uuid
.refTable
975 && column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
976 const struct ovsdb_table
*ref_table
= column
->type
.key
.uuid
.refTable
;
977 HMAP_FOR_EACH (row
, hmap_node
, &table
->rows
) {
978 const struct ovsdb_datum
*datum
;
981 datum
= &row
->fields
[column
->index
];
982 for (i
= 0; i
< datum
->n
; i
++) {
983 const struct ovsdb_row
*ref_row
;
985 ref_row
= ovsdb_table_get_row(ref_table
, &datum
->keys
[i
].uuid
);
987 add_manager_options(remotes
, ref_row
);
995 update_remote_row(const struct ovsdb_row
*row
, struct ovsdb_txn
*txn
,
996 const struct ovsdb_jsonrpc_server
*jsonrpc
)
998 struct ovsdb_jsonrpc_remote_status status
;
999 struct ovsdb_row
*rw_row
;
1001 char *keys
[9], *values
[9];
1004 /* Get the "target" (protocol/host/port) spec. */
1005 if (!ovsdb_util_read_string_column(row
, "target", &target
)) {
1006 /* Bad remote spec or incorrect schema. */
1009 rw_row
= ovsdb_txn_row_modify(txn
, row
);
1010 ovsdb_jsonrpc_server_get_remote_status(jsonrpc
, target
, &status
);
1012 /* Update status information columns. */
1013 ovsdb_util_write_bool_column(rw_row
, "is_connected", status
.is_connected
);
1016 keys
[n
] = xstrdup("state");
1017 values
[n
++] = xstrdup(status
.state
);
1019 if (status
.sec_since_connect
!= UINT_MAX
) {
1020 keys
[n
] = xstrdup("sec_since_connect");
1021 values
[n
++] = xasprintf("%u", status
.sec_since_connect
);
1023 if (status
.sec_since_disconnect
!= UINT_MAX
) {
1024 keys
[n
] = xstrdup("sec_since_disconnect");
1025 values
[n
++] = xasprintf("%u", status
.sec_since_disconnect
);
1027 if (status
.last_error
) {
1028 keys
[n
] = xstrdup("last_error");
1030 xstrdup(ovs_retval_to_string(status
.last_error
));
1032 if (status
.locks_held
&& status
.locks_held
[0]) {
1033 keys
[n
] = xstrdup("locks_held");
1034 values
[n
++] = xstrdup(status
.locks_held
);
1036 if (status
.locks_waiting
&& status
.locks_waiting
[0]) {
1037 keys
[n
] = xstrdup("locks_waiting");
1038 values
[n
++] = xstrdup(status
.locks_waiting
);
1040 if (status
.locks_lost
&& status
.locks_lost
[0]) {
1041 keys
[n
] = xstrdup("locks_lost");
1042 values
[n
++] = xstrdup(status
.locks_lost
);
1044 if (status
.n_connections
> 1) {
1045 keys
[n
] = xstrdup("n_connections");
1046 values
[n
++] = xasprintf("%d", status
.n_connections
);
1048 if (status
.bound_port
!= htons(0)) {
1049 keys
[n
] = xstrdup("bound_port");
1050 values
[n
++] = xasprintf("%"PRIu16
, ntohs(status
.bound_port
));
1052 ovsdb_util_write_string_string_column(rw_row
, "status", keys
, values
, n
);
1054 ovsdb_jsonrpc_server_free_remote_status(&status
);
1058 update_remote_rows(const struct shash
*all_dbs
, const struct db
*db_
,
1059 const char *remote_name
,
1060 const struct ovsdb_jsonrpc_server
*jsonrpc
,
1061 struct ovsdb_txn
*txn
)
1063 const struct ovsdb_table
*table
, *ref_table
;
1064 const struct ovsdb_column
*column
;
1065 const struct ovsdb_row
*row
;
1066 const struct db
*db
;
1069 if (strncmp("db:", remote_name
, 3)) {
1073 retval
= parse_db_column(all_dbs
, remote_name
, &db
, &table
, &column
);
1080 || column
->type
.key
.type
!= OVSDB_TYPE_UUID
1081 || !column
->type
.key
.uuid
.refTable
1082 || column
->type
.value
.type
!= OVSDB_TYPE_VOID
) {
1086 ref_table
= column
->type
.key
.uuid
.refTable
;
1088 HMAP_FOR_EACH (row
, hmap_node
, &table
->rows
) {
1089 const struct ovsdb_datum
*datum
;
1092 datum
= &row
->fields
[column
->index
];
1093 for (i
= 0; i
< datum
->n
; i
++) {
1094 const struct ovsdb_row
*ref_row
;
1096 ref_row
= ovsdb_table_get_row(ref_table
, &datum
->keys
[i
].uuid
);
1098 update_remote_row(ref_row
, txn
, jsonrpc
);
1105 commit_txn(struct ovsdb_txn
*txn
, const char *name
)
1107 struct ovsdb_error
*error
= ovsdb_txn_propose_commit_block(txn
, false);
1109 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
1110 char *msg
= ovsdb_error_to_string_free(error
);
1111 VLOG_ERR_RL(&rl
, "Failed to update %s: %s", name
, msg
);
1117 update_remote_status(const struct ovsdb_jsonrpc_server
*jsonrpc
,
1118 const struct sset
*remotes
,
1119 struct shash
*all_dbs
)
1121 struct shash_node
*node
;
1122 SHASH_FOR_EACH (node
, all_dbs
) {
1123 struct db
*db
= node
->data
;
1124 if (!db
->db
|| ovsdb_storage_is_clustered(db
->db
->storage
)) {
1128 struct ovsdb_txn
*txn
= ovsdb_txn_create(db
->db
);
1130 SSET_FOR_EACH (remote
, remotes
) {
1131 update_remote_rows(all_dbs
, db
, remote
, jsonrpc
, txn
);
1133 commit_txn(txn
, "remote status");
1137 /* Updates 'row', a row in the _Server database's Database table, to match
1140 update_database_status(struct ovsdb_row
*row
, struct db
*db
)
1142 ovsdb_util_write_string_column(row
, "name", db
->db
->name
);
1143 ovsdb_util_write_string_column(row
, "model",
1144 ovsdb_storage_get_model(db
->db
->storage
));
1145 ovsdb_util_write_bool_column(row
, "connected",
1146 ovsdb_storage_is_connected(db
->db
->storage
));
1147 ovsdb_util_write_bool_column(row
, "leader",
1148 ovsdb_storage_is_leader(db
->db
->storage
));
1149 ovsdb_util_write_uuid_column(row
, "cid",
1150 ovsdb_storage_get_cid(db
->db
->storage
));
1151 ovsdb_util_write_uuid_column(row
, "sid",
1152 ovsdb_storage_get_sid(db
->db
->storage
));
1154 uint64_t index
= ovsdb_storage_get_applied_index(db
->db
->storage
);
1156 ovsdb_util_write_integer_column(row
, "index", index
);
1158 ovsdb_util_clear_column(row
, "index");
1161 const struct uuid
*row_uuid
= ovsdb_row_get_uuid(row
);
1162 if (!uuid_equals(row_uuid
, &db
->row_uuid
)) {
1163 db
->row_uuid
= *row_uuid
;
1165 /* The schema can only change if the row UUID changes, so only update
1166 * it in that case. Presumably, this is worth optimizing because
1167 * schemas are often kilobytes in size and nontrivial to serialize. */
1168 char *schema
= NULL
;
1169 if (db
->db
->schema
) {
1170 struct json
*json_schema
= ovsdb_schema_to_json(db
->db
->schema
);
1171 schema
= json_to_string(json_schema
, JSSF_SORT
);
1172 json_destroy(json_schema
);
1174 ovsdb_util_write_string_column(row
, "schema", schema
);
1179 /* Updates the Database table in the _Server database. */
1181 update_server_status(struct shash
*all_dbs
)
1183 struct db
*server_db
= shash_find_data(all_dbs
, "_Server");
1184 struct ovsdb_table
*database_table
= shash_find_data(
1185 &server_db
->db
->tables
, "Database");
1186 struct ovsdb_txn
*txn
= ovsdb_txn_create(server_db
->db
);
1188 /* Update rows for databases that still exist.
1189 * Delete rows for databases that no longer exist. */
1190 const struct ovsdb_row
*row
, *next_row
;
1191 HMAP_FOR_EACH_SAFE (row
, next_row
, hmap_node
, &database_table
->rows
) {
1193 ovsdb_util_read_string_column(row
, "name", &name
);
1194 struct db
*db
= shash_find_data(all_dbs
, name
);
1195 if (!db
|| !db
->db
) {
1196 ovsdb_txn_row_delete(txn
, row
);
1198 update_database_status(ovsdb_txn_row_modify(txn
, row
), db
);
1202 /* Add rows for new databases.
1204 * This is O(n**2) but usually there are only 2 or 3 databases. */
1205 struct shash_node
*node
;
1206 SHASH_FOR_EACH (node
, all_dbs
) {
1207 struct db
*db
= node
->data
;
1213 HMAP_FOR_EACH (row
, hmap_node
, &database_table
->rows
) {
1215 ovsdb_util_read_string_column(row
, "name", &name
);
1216 if (!strcmp(name
, node
->name
)) {
1222 struct ovsdb_row
*new_row
= ovsdb_row_create(database_table
);
1223 uuid_generate(ovsdb_row_get_uuid_rw(new_row
));
1224 update_database_status(new_row
, db
);
1225 ovsdb_txn_row_insert(txn
, new_row
);
1230 commit_txn(txn
, "_Server");
1233 /* Reconfigures ovsdb-server's remotes based on information in the database. */
1235 reconfigure_remotes(struct ovsdb_jsonrpc_server
*jsonrpc
,
1236 const struct shash
*all_dbs
, struct sset
*remotes
)
1238 struct ds errors
= DS_EMPTY_INITIALIZER
;
1239 struct shash resolved_remotes
;
1242 /* Configure remotes. */
1243 shash_init(&resolved_remotes
);
1244 SSET_FOR_EACH (name
, remotes
) {
1245 if (!strncmp(name
, "db:", 3)) {
1246 query_db_remotes(name
, all_dbs
, &resolved_remotes
, &errors
);
1248 add_remote(&resolved_remotes
, name
);
1251 ovsdb_jsonrpc_server_set_remotes(jsonrpc
, &resolved_remotes
);
1252 free_remotes(&resolved_remotes
);
1254 return errors
.string
;
1258 reconfigure_ssl(const struct shash
*all_dbs
)
1260 struct ds errors
= DS_EMPTY_INITIALIZER
;
1261 const char *resolved_private_key
;
1262 const char *resolved_certificate
;
1263 const char *resolved_ca_cert
;
1264 const char *resolved_ssl_protocols
;
1265 const char *resolved_ssl_ciphers
;
1267 resolved_private_key
= query_db_string(all_dbs
, private_key_file
, &errors
);
1268 resolved_certificate
= query_db_string(all_dbs
, certificate_file
, &errors
);
1269 resolved_ca_cert
= query_db_string(all_dbs
, ca_cert_file
, &errors
);
1270 resolved_ssl_protocols
= query_db_string(all_dbs
, ssl_protocols
, &errors
);
1271 resolved_ssl_ciphers
= query_db_string(all_dbs
, ssl_ciphers
, &errors
);
1273 stream_ssl_set_key_and_cert(resolved_private_key
, resolved_certificate
);
1274 stream_ssl_set_ca_cert_file(resolved_ca_cert
, bootstrap_ca_cert
);
1275 stream_ssl_set_protocols(resolved_ssl_protocols
);
1276 stream_ssl_set_ciphers(resolved_ssl_ciphers
);
1278 return errors
.string
;
1282 report_error_if_changed(char *error
, char **last_errorp
)
1285 if (!*last_errorp
|| strcmp(error
, *last_errorp
)) {
1286 VLOG_WARN("%s", error
);
1288 *last_errorp
= error
;
1294 *last_errorp
= NULL
;
1299 ovsdb_server_set_active_ovsdb_server(struct unixctl_conn
*conn
,
1300 int argc OVS_UNUSED
, const char *argv
[],
1303 struct server_config
*config
= config_
;
1305 if (*config
->sync_from
) {
1306 free(*config
->sync_from
);
1308 *config
->sync_from
= xstrdup(argv
[1]);
1309 save_config(config
);
1311 unixctl_command_reply(conn
, NULL
);
1315 ovsdb_server_get_active_ovsdb_server(struct unixctl_conn
*conn
,
1316 int argc OVS_UNUSED
,
1317 const char *argv
[] OVS_UNUSED
,
1320 struct server_config
*config
= config_
;
1322 unixctl_command_reply(conn
, *config
->sync_from
);
1326 ovsdb_server_connect_active_ovsdb_server(struct unixctl_conn
*conn
,
1327 int argc OVS_UNUSED
,
1328 const char *argv
[] OVS_UNUSED
,
1331 struct server_config
*config
= config_
;
1334 if ( !*config
->sync_from
) {
1335 msg
= "Unable to connect: active server is not specified.\n";
1337 const struct uuid
*server_uuid
;
1338 server_uuid
= ovsdb_jsonrpc_server_get_uuid(config
->jsonrpc
);
1339 ovsdb_replication_init(*config
->sync_from
, *config
->sync_exclude
,
1340 config
->all_dbs
, server_uuid
,
1341 *config
->replication_probe_interval
);
1342 if (!*config
->is_backup
) {
1343 *config
->is_backup
= true;
1344 save_config(config
);
1347 unixctl_command_reply(conn
, msg
);
1351 ovsdb_server_disconnect_active_ovsdb_server(struct unixctl_conn
*conn
,
1352 int argc OVS_UNUSED
,
1353 const char *argv
[] OVS_UNUSED
,
1356 struct server_config
*config
= config_
;
1358 disconnect_active_server();
1359 *config
->is_backup
= false;
1360 save_config(config
);
1361 unixctl_command_reply(conn
, NULL
);
1365 ovsdb_server_set_active_ovsdb_server_probe_interval(struct unixctl_conn
*conn
,
1366 int argc OVS_UNUSED
,
1370 struct server_config
*config
= config_
;
1373 if (str_to_int(argv
[1], 10, &probe_interval
)) {
1374 *config
->replication_probe_interval
= probe_interval
;
1375 save_config(config
);
1376 if (*config
->is_backup
) {
1377 replication_set_probe_interval(probe_interval
);
1379 unixctl_command_reply(conn
, NULL
);
1381 unixctl_command_reply(
1382 conn
, "Invalid probe interval, integer value expected");
1387 ovsdb_server_set_sync_exclude_tables(struct unixctl_conn
*conn
,
1388 int argc OVS_UNUSED
,
1392 struct server_config
*config
= config_
;
1394 char *err
= set_excluded_tables(argv
[1], true);
1396 free(*config
->sync_exclude
);
1397 *config
->sync_exclude
= xstrdup(argv
[1]);
1398 save_config(config
);
1399 if (*config
->is_backup
) {
1400 const struct uuid
*server_uuid
;
1401 server_uuid
= ovsdb_jsonrpc_server_get_uuid(config
->jsonrpc
);
1402 ovsdb_replication_init(*config
->sync_from
, *config
->sync_exclude
,
1403 config
->all_dbs
, server_uuid
,
1404 *config
->replication_probe_interval
);
1406 err
= set_excluded_tables(argv
[1], false);
1408 unixctl_command_reply(conn
, err
);
1413 ovsdb_server_get_sync_exclude_tables(struct unixctl_conn
*conn
,
1414 int argc OVS_UNUSED
,
1415 const char *argv
[] OVS_UNUSED
,
1416 void *arg_ OVS_UNUSED
)
1418 char *reply
= get_excluded_tables();
1419 unixctl_command_reply(conn
, reply
);
1424 ovsdb_server_exit(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1425 const char *argv
[] OVS_UNUSED
,
1428 bool *exiting
= exiting_
;
1430 unixctl_command_reply(conn
, NULL
);
1434 ovsdb_server_perf_counters_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1435 const char *argv
[] OVS_UNUSED
,
1436 void *arg_ OVS_UNUSED
)
1438 char *s
= perf_counters_to_string();
1440 unixctl_command_reply(conn
, s
);
1445 ovsdb_server_perf_counters_clear(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1446 const char *argv
[] OVS_UNUSED
,
1447 void *arg_ OVS_UNUSED
)
1449 perf_counters_clear();
1450 unixctl_command_reply(conn
, NULL
);
1453 /* "ovsdb-server/disable-monitor-cond": makes ovsdb-server drop all of its
1454 * JSON-RPC connections and reconnect. New sessions will not recognize
1455 * the 'monitor_cond' method. */
1457 ovsdb_server_disable_monitor_cond(struct unixctl_conn
*conn
,
1458 int argc OVS_UNUSED
,
1459 const char *argv
[] OVS_UNUSED
,
1462 struct ovsdb_jsonrpc_server
*jsonrpc
= jsonrpc_
;
1464 ovsdb_jsonrpc_disable_monitor_cond();
1465 ovsdb_jsonrpc_server_reconnect(
1466 jsonrpc
, true, xstrdup("user ran ovsdb-server/disable-monitor-cond"));
1467 unixctl_command_reply(conn
, NULL
);
1471 ovsdb_server_compact(struct unixctl_conn
*conn
, int argc
,
1472 const char *argv
[], void *dbs_
)
1474 const char *db_name
= argc
< 2 ? NULL
: argv
[1];
1475 struct shash
*all_dbs
= dbs_
;
1477 struct shash_node
*node
;
1480 if (db_name
&& db_name
[0] == '_') {
1481 unixctl_command_reply_error(conn
, "cannot compact built-in databases");
1486 SHASH_FOR_EACH(node
, all_dbs
) {
1487 struct db
*db
= node
->data
;
1489 ? !strcmp(node
->name
, db_name
)
1490 : node
->name
[0] != '_') {
1492 VLOG_INFO("compacting %s database by user request",
1495 struct ovsdb_error
*error
= ovsdb_snapshot(db
->db
);
1497 char *s
= ovsdb_error_to_string(error
);
1498 ds_put_format(&reply
, "%s\n", s
);
1500 ovsdb_error_destroy(error
);
1509 unixctl_command_reply_error(conn
, "no database by that name");
1510 } else if (reply
.length
) {
1511 unixctl_command_reply_error(conn
, ds_cstr(&reply
));
1513 unixctl_command_reply(conn
, NULL
);
1518 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
1519 * connections and reconnect. */
1521 ovsdb_server_reconnect(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1522 const char *argv
[] OVS_UNUSED
, void *jsonrpc_
)
1524 struct ovsdb_jsonrpc_server
*jsonrpc
= jsonrpc_
;
1525 ovsdb_jsonrpc_server_reconnect(
1526 jsonrpc
, true, xstrdup("user ran ovsdb-server/reconnect"));
1527 unixctl_command_reply(conn
, NULL
);
1530 /* "ovsdb-server/add-remote REMOTE": adds REMOTE to the set of remotes that
1531 * ovsdb-server services. */
1533 ovsdb_server_add_remote(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1534 const char *argv
[], void *config_
)
1536 struct server_config
*config
= config_
;
1537 const char *remote
= argv
[1];
1539 const struct ovsdb_column
*column
;
1540 const struct ovsdb_table
*table
;
1541 const struct db
*db
;
1544 retval
= (strncmp("db:", remote
, 3)
1546 : parse_db_column(config
->all_dbs
, remote
,
1547 &db
, &table
, &column
));
1549 if (sset_add(config
->remotes
, remote
)) {
1550 save_config(config
);
1552 unixctl_command_reply(conn
, NULL
);
1554 unixctl_command_reply_error(conn
, retval
);
1559 /* "ovsdb-server/remove-remote REMOTE": removes REMOTE frmo the set of remotes
1560 * that ovsdb-server services. */
1562 ovsdb_server_remove_remote(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1563 const char *argv
[], void *config_
)
1565 struct server_config
*config
= config_
;
1566 struct sset_node
*node
;
1568 node
= sset_find(config
->remotes
, argv
[1]);
1570 sset_delete(config
->remotes
, node
);
1571 save_config(config
);
1572 unixctl_command_reply(conn
, NULL
);
1574 unixctl_command_reply_error(conn
, "no such remote");
1578 /* "ovsdb-server/list-remotes": outputs a list of configured rmeotes. */
1580 ovsdb_server_list_remotes(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1581 const char *argv
[] OVS_UNUSED
, void *remotes_
)
1583 struct sset
*remotes
= remotes_
;
1584 const char **list
, **p
;
1589 list
= sset_sort(remotes
);
1590 for (p
= list
; *p
; p
++) {
1591 ds_put_format(&s
, "%s\n", *p
);
1595 unixctl_command_reply(conn
, ds_cstr(&s
));
1600 /* "ovsdb-server/add-db DB": adds the DB to ovsdb-server. */
1602 ovsdb_server_add_database(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1603 const char *argv
[], void *config_
)
1605 struct server_config
*config
= config_
;
1606 const char *filename
= argv
[1];
1608 char *error
= ovsdb_error_to_string_free(open_db(config
, filename
));
1610 save_config(config
);
1611 if (*config
->is_backup
) {
1612 const struct uuid
*server_uuid
;
1613 server_uuid
= ovsdb_jsonrpc_server_get_uuid(config
->jsonrpc
);
1614 ovsdb_replication_init(*config
->sync_from
, *config
->sync_exclude
,
1615 config
->all_dbs
, server_uuid
,
1616 *config
->replication_probe_interval
);
1618 unixctl_command_reply(conn
, NULL
);
1620 unixctl_command_reply_error(conn
, error
);
1626 remove_db(struct server_config
*config
, struct shash_node
*node
, char *comment
)
1628 struct db
*db
= node
->data
;
1630 close_db(config
, db
, comment
);
1631 shash_delete(config
->all_dbs
, node
);
1633 save_config(config
);
1634 if (*config
->is_backup
) {
1635 const struct uuid
*server_uuid
;
1636 server_uuid
= ovsdb_jsonrpc_server_get_uuid(config
->jsonrpc
);
1637 ovsdb_replication_init(*config
->sync_from
, *config
->sync_exclude
,
1638 config
->all_dbs
, server_uuid
,
1639 *config
->replication_probe_interval
);
1644 ovsdb_server_remove_database(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1645 const char *argv
[], void *config_
)
1647 struct server_config
*config
= config_
;
1648 struct shash_node
*node
;
1650 node
= shash_find(config
->all_dbs
, argv
[1]);
1652 unixctl_command_reply_error(conn
, "Failed to find the database.");
1655 if (node
->name
[0] == '_') {
1656 unixctl_command_reply_error(conn
, "Cannot remove reserved database.");
1660 remove_db(config
, node
, xasprintf("removing %s database by user request",
1662 unixctl_command_reply(conn
, NULL
);
1666 ovsdb_server_list_databases(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1667 const char *argv
[] OVS_UNUSED
, void *all_dbs_
)
1669 struct shash
*all_dbs
= all_dbs_
;
1670 const struct shash_node
**nodes
;
1676 nodes
= shash_sort(all_dbs
);
1677 for (i
= 0; i
< shash_count(all_dbs
); i
++) {
1678 const struct shash_node
*node
= nodes
[i
];
1679 struct db
*db
= node
->data
;
1681 ds_put_format(&s
, "%s\n", node
->name
);
1686 unixctl_command_reply(conn
, ds_cstr(&s
));
1691 ovsdb_server_get_sync_status(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1692 const char *argv
[] OVS_UNUSED
, void *config_
)
1694 struct server_config
*config
= config_
;
1695 bool is_backup
= *config
->is_backup
;
1696 struct ds ds
= DS_EMPTY_INITIALIZER
;
1698 ds_put_format(&ds
, "state: %s\n", is_backup
? "backup" : "active");
1701 ds_put_and_free_cstr(&ds
, replication_status());
1704 unixctl_command_reply(conn
, ds_cstr(&ds
));
1709 ovsdb_server_get_db_storage_status(struct unixctl_conn
*conn
,
1710 int argc OVS_UNUSED
,
1714 struct server_config
*config
= config_
;
1715 struct shash_node
*node
;
1717 node
= shash_find(config
->all_dbs
, argv
[1]);
1719 unixctl_command_reply_error(conn
, "Failed to find the database.");
1723 struct db
*db
= node
->data
;
1726 unixctl_command_reply_error(conn
, "Failed to find the database.");
1730 struct ds ds
= DS_EMPTY_INITIALIZER
;
1731 char *error
= ovsdb_storage_get_error(db
->db
->storage
);
1734 ds_put_cstr(&ds
, "status: ok");
1736 ds_put_format(&ds
, "status: %s", error
);
1739 unixctl_command_reply(conn
, ds_cstr(&ds
));
1744 parse_options(int argc
, char *argv
[],
1745 struct sset
*db_filenames
, struct sset
*remotes
,
1746 char **unixctl_pathp
, char **run_command
,
1747 char **sync_from
, char **sync_exclude
, bool *active
)
1750 OPT_REMOTE
= UCHAR_MAX
+ 1,
1753 OPT_BOOTSTRAP_CA_CERT
,
1760 DAEMON_OPTION_ENUMS
,
1764 static const struct option long_options
[] = {
1765 {"remote", required_argument
, NULL
, OPT_REMOTE
},
1766 {"unixctl", required_argument
, NULL
, OPT_UNIXCTL
},
1768 {"run", required_argument
, NULL
, OPT_RUN
},
1770 {"help", no_argument
, NULL
, 'h'},
1771 {"version", no_argument
, NULL
, 'V'},
1772 DAEMON_LONG_OPTIONS
,
1774 {"bootstrap-ca-cert", required_argument
, NULL
, OPT_BOOTSTRAP_CA_CERT
},
1775 {"peer-ca-cert", required_argument
, NULL
, OPT_PEER_CA_CERT
},
1776 STREAM_SSL_LONG_OPTIONS
,
1777 {"sync-from", required_argument
, NULL
, OPT_SYNC_FROM
},
1778 {"sync-exclude-tables", required_argument
, NULL
, OPT_SYNC_EXCLUDE
},
1779 {"active", no_argument
, NULL
, OPT_ACTIVE
},
1780 {"no-dbs", no_argument
, NULL
, OPT_NO_DBS
},
1783 char *short_options
= ovs_cmdl_long_options_to_short_options(long_options
);
1784 bool add_default_db
= true;
1787 *sync_exclude
= NULL
;
1788 sset_init(db_filenames
);
1793 c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
1800 sset_add(remotes
, optarg
);
1804 *unixctl_pathp
= optarg
;
1808 *run_command
= optarg
;
1815 ovs_print_version(0, 0);
1818 VLOG_OPTION_HANDLERS
1819 DAEMON_OPTION_HANDLERS
1822 private_key_file
= optarg
;
1826 certificate_file
= optarg
;
1830 ca_cert_file
= optarg
;
1831 bootstrap_ca_cert
= false;
1834 case OPT_SSL_PROTOCOLS
:
1835 ssl_protocols
= optarg
;
1838 case OPT_SSL_CIPHERS
:
1839 ssl_ciphers
= optarg
;
1842 case OPT_BOOTSTRAP_CA_CERT
:
1843 ca_cert_file
= optarg
;
1844 bootstrap_ca_cert
= true;
1847 case OPT_PEER_CA_CERT
:
1848 stream_ssl_set_peer_ca_cert_file(optarg
);
1852 *sync_from
= xstrdup(optarg
);
1855 case OPT_SYNC_EXCLUDE
: {
1856 char *err
= set_excluded_tables(optarg
, false);
1858 ovs_fatal(0, "%s", err
);
1860 *sync_exclude
= xstrdup(optarg
);
1868 add_default_db
= false;
1878 free(short_options
);
1883 for (int i
= 0; i
< argc
; i
++) {
1884 sset_add(db_filenames
, argv
[i
]);
1886 } else if (add_default_db
) {
1887 sset_add_and_free(db_filenames
, xasprintf("%s/conf.db", ovs_dbdir()));
1894 printf("%s: Open vSwitch database server\n"
1895 "usage: %s [OPTIONS] [DATABASE...]\n"
1896 "where each DATABASE is a database file in ovsdb format.\n"
1897 "The default DATABASE, if none is given, is\n%s/conf.db.\n",
1898 program_name
, program_name
, ovs_dbdir());
1899 printf("\nJSON-RPC options (may be specified any number of times):\n"
1900 " --remote=REMOTE connect or listen to REMOTE\n");
1901 stream_usage("JSON-RPC", true, true, true);
1904 replication_usage();
1905 printf("\nOther options:\n"
1906 " --run COMMAND run COMMAND as subprocess then exit\n"
1907 " --unixctl=SOCKET override default control socket name\n"
1908 " -h, --help display this help message\n"
1909 " -V, --version display version information\n");
1913 static struct json
*
1914 sset_to_json(const struct sset
*sset
)
1919 array
= json_array_create_empty();
1920 SSET_FOR_EACH (s
, sset
) {
1921 json_array_add(array
, json_string_create(s
));
1926 /* Truncates and replaces the contents of 'config_file' by a representation of
1927 * 'remotes' and 'db_filenames'. */
1929 save_config__(FILE *config_file
, const struct sset
*remotes
,
1930 const struct sset
*db_filenames
, const char *sync_from
,
1931 const char *sync_exclude
, bool is_backup
)
1936 if (ftruncate(fileno(config_file
), 0) == -1) {
1937 VLOG_FATAL("failed to truncate temporary file (%s)",
1938 ovs_strerror(errno
));
1941 obj
= json_object_create();
1942 json_object_put(obj
, "remotes", sset_to_json(remotes
));
1943 json_object_put(obj
, "db_filenames", sset_to_json(db_filenames
));
1945 json_object_put(obj
, "sync_from", json_string_create(sync_from
));
1948 json_object_put(obj
, "sync_exclude",
1949 json_string_create(sync_exclude
));
1951 json_object_put(obj
, "is_backup", json_boolean_create(is_backup
));
1953 s
= json_to_string(obj
, 0);
1956 if (fseek(config_file
, 0, SEEK_SET
) != 0
1957 || fputs(s
, config_file
) == EOF
1958 || fflush(config_file
) == EOF
) {
1959 VLOG_FATAL("failed to write temporary file (%s)", ovs_strerror(errno
));
1964 /* Truncates and replaces the contents of 'config_file' by a representation of
1967 save_config(struct server_config
*config
)
1969 struct sset db_filenames
;
1970 struct shash_node
*node
;
1972 sset_init(&db_filenames
);
1973 SHASH_FOR_EACH (node
, config
->all_dbs
) {
1974 struct db
*db
= node
->data
;
1975 if (node
->name
[0] != '_') {
1976 sset_add(&db_filenames
, db
->filename
);
1980 save_config__(config
->config_tmpfile
, config
->remotes
, &db_filenames
,
1981 *config
->sync_from
, *config
->sync_exclude
,
1982 *config
->is_backup
);
1984 sset_destroy(&db_filenames
);
1988 sset_from_json(struct sset
*sset
, const struct json
*array
)
1994 ovs_assert(array
->type
== JSON_ARRAY
);
1995 for (i
= 0; i
< array
->array
.n
; i
++) {
1996 const struct json
*elem
= array
->array
.elems
[i
];
1997 sset_add(sset
, json_string(elem
));
2001 /* Clears and replaces 'remotes' and 'dbnames' by a configuration read from
2002 * 'config_file', which must have been previously written by save_config(). */
2004 load_config(FILE *config_file
, struct sset
*remotes
, struct sset
*db_filenames
,
2005 char **sync_from
, char **sync_exclude
, bool *is_backup
)
2009 if (fseek(config_file
, 0, SEEK_SET
) != 0) {
2010 VLOG_FATAL("seek failed in temporary file (%s)", ovs_strerror(errno
));
2012 json
= json_from_stream(config_file
);
2013 if (json
->type
== JSON_STRING
) {
2014 VLOG_FATAL("reading json failed (%s)", json_string(json
));
2016 ovs_assert(json
->type
== JSON_OBJECT
);
2018 sset_from_json(remotes
, shash_find_data(json_object(json
), "remotes"));
2019 sset_from_json(db_filenames
,
2020 shash_find_data(json_object(json
), "db_filenames"));
2022 struct json
*string
;
2023 string
= shash_find_data(json_object(json
), "sync_from");
2025 *sync_from
= string
? xstrdup(json_string(string
)) : NULL
;
2027 string
= shash_find_data(json_object(json
), "sync_exclude");
2028 free(*sync_exclude
);
2029 *sync_exclude
= string
? xstrdup(json_string(string
)) : NULL
;
2031 *is_backup
= json_boolean(shash_find_data(json_object(json
), "is_backup"));