1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013 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.
25 #include "command-line.h"
29 #include "dynamic-string.h"
34 #include "jsonrpc-server.h"
38 #include "ovsdb-data.h"
39 #include "ovsdb-types.h"
40 #include "ovsdb-error.h"
41 #include "poll-loop.h"
46 #include "stream-ssl.h"
52 #include "transaction.h"
58 VLOG_DEFINE_THIS_MODULE(ovsdb_server);
61 /* Initialized in main(). */
63 struct ovsdb_file *file;
66 /* Only used by update_remote_status(). */
67 struct ovsdb_txn *txn;
70 /* SSL configuration. */
71 static char *private_key_file;
72 static char *certificate_file;
73 static char *ca_cert_file;
74 static bool bootstrap_ca_cert;
76 static unixctl_cb_func ovsdb_server_exit;
77 static unixctl_cb_func ovsdb_server_compact;
78 static unixctl_cb_func ovsdb_server_reconnect;
80 struct server_config {
82 struct shash *all_dbs;
84 struct ovsdb_jsonrpc_server *jsonrpc;
86 static unixctl_cb_func ovsdb_server_add_remote;
87 static unixctl_cb_func ovsdb_server_remove_remote;
88 static unixctl_cb_func ovsdb_server_list_remotes;
90 static unixctl_cb_func ovsdb_server_add_database;
91 static unixctl_cb_func ovsdb_server_remove_database;
92 static unixctl_cb_func ovsdb_server_list_databases;
94 static char *open_db(struct server_config *config, const char *filename);
96 static void parse_options(int *argc, char **argvp[],
97 struct sset *remotes, char **unixctl_pathp,
99 static void usage(void) NO_RETURN;
101 static char *reconfigure_remotes(struct ovsdb_jsonrpc_server *,
102 const struct shash *all_dbs,
103 struct sset *remotes);
104 static char *reconfigure_ssl(const struct shash *all_dbs);
105 static void report_error_if_changed(char *error, char **last_errorp);
107 static void update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
108 const struct sset *remotes,
109 struct shash *all_dbs);
111 static void save_config__(FILE *config_file, const struct sset *remotes,
112 const struct sset *db_filenames);
113 static void save_config(struct server_config *);
114 static void load_config(FILE *config_file, struct sset *remotes,
115 struct sset *db_filenames);
118 main(int argc, char *argv[])
120 char *unixctl_path = NULL;
121 char *run_command = NULL;
122 struct unixctl_server *unixctl;
123 struct ovsdb_jsonrpc_server *jsonrpc;
124 struct sset remotes, db_filenames;
125 const char *db_filename;
126 struct process *run_process;
129 long long int status_timer = LLONG_MIN;
130 FILE *config_tmpfile;
131 struct server_config server_config;
132 struct shash all_dbs;
133 struct shash_node *node;
134 char *remotes_error, *ssl_error;
138 proctitle_init(argc, argv);
139 set_program_name(argv[0]);
140 stress_init_command();
141 signal(SIGPIPE, SIG_IGN);
144 parse_options(&argc, &argv, &remotes, &unixctl_path, &run_command);
146 /* Create and initialize 'config_tmpfile' as a temporary file to hold
147 * ovsdb-server's most basic configuration, and then save our initial
148 * configuration to it. When --monitor is used, this preserves the effects
149 * of ovs-appctl commands such as ovsdb-server/add-remote (which saves the
150 * new configuration) across crashes. */
151 config_tmpfile = tmpfile();
152 if (!config_tmpfile) {
153 ovs_fatal(errno, "failed to create temporary file");
156 sset_init(&db_filenames);
158 for (i = 0; i < argc; i++) {
159 sset_add(&db_filenames, argv[i]);
162 char *default_db = xasprintf("%s/conf.db", ovs_dbdir());
163 sset_add(&db_filenames, default_db);
167 server_config.remotes = &remotes;
168 server_config.config_tmpfile = config_tmpfile;
170 save_config__(config_tmpfile, &remotes, &db_filenames);
174 /* Load the saved config. */
175 load_config(config_tmpfile, &remotes, &db_filenames);
176 jsonrpc = ovsdb_jsonrpc_server_create();
178 shash_init(&all_dbs);
179 server_config.all_dbs = &all_dbs;
180 server_config.jsonrpc = jsonrpc;
181 SSET_FOR_EACH (db_filename, &db_filenames) {
182 error = open_db(&server_config, db_filename);
184 ovs_fatal(0, "%s", error);
188 error = reconfigure_remotes(jsonrpc, &all_dbs, &remotes);
190 error = reconfigure_ssl(&all_dbs);
193 ovs_fatal(0, "%s", error);
196 retval = unixctl_server_create(unixctl_path, &unixctl);
204 run_argv[0] = "/bin/sh";
206 run_argv[2] = run_command;
209 retval = process_start(run_argv, &run_process);
211 ovs_fatal(retval, "%s: process failed to start", run_command);
217 daemonize_complete();
220 /* ovsdb-server is usually a long-running process, in which case it
221 * makes plenty of sense to log the version, but --run makes
222 * ovsdb-server more like a command-line tool, so skip it. */
223 VLOG_INFO("%s (Open vSwitch) %s", program_name, VERSION);
226 unixctl_command_register("exit", "", 0, 0, ovsdb_server_exit, &exiting);
227 unixctl_command_register("ovsdb-server/compact", "", 0, 1,
228 ovsdb_server_compact, &all_dbs);
229 unixctl_command_register("ovsdb-server/reconnect", "", 0, 0,
230 ovsdb_server_reconnect, jsonrpc);
232 unixctl_command_register("ovsdb-server/add-remote", "REMOTE", 1, 1,
233 ovsdb_server_add_remote, &server_config);
234 unixctl_command_register("ovsdb-server/remove-remote", "REMOTE", 1, 1,
235 ovsdb_server_remove_remote, &server_config);
236 unixctl_command_register("ovsdb-server/list-remotes", "", 0, 0,
237 ovsdb_server_list_remotes, &remotes);
239 unixctl_command_register("ovsdb-server/add-db", "DB", 1, 1,
240 ovsdb_server_add_database, &server_config);
241 unixctl_command_register("ovsdb-server/remove-db", "DB", 1, 1,
242 ovsdb_server_remove_database, &server_config);
243 unixctl_command_register("ovsdb-server/list-dbs", "", 0, 0,
244 ovsdb_server_list_databases, &all_dbs);
248 remotes_error = NULL;
251 if (memory_should_report()) {
255 ovsdb_jsonrpc_server_get_memory_usage(jsonrpc, &usage);
256 SHASH_FOR_EACH(node, &all_dbs) {
257 struct db *db = node->data;
258 ovsdb_get_memory_usage(db->db, &usage);
260 memory_report(&usage);
261 simap_destroy(&usage);
264 /* Run unixctl_server_run() before reconfigure_remotes() because
265 * ovsdb-server/add-remote and ovsdb-server/remove-remote can change
266 * the set of remotes that reconfigure_remotes() uses. */
267 unixctl_server_run(unixctl);
269 report_error_if_changed(
270 reconfigure_remotes(jsonrpc, &all_dbs, &remotes),
272 report_error_if_changed(reconfigure_ssl(&all_dbs), &ssl_error);
273 ovsdb_jsonrpc_server_run(jsonrpc);
275 SHASH_FOR_EACH(node, &all_dbs) {
276 struct db *db = node->data;
277 ovsdb_trigger_run(db->db, time_msec());
281 if (process_exited(run_process)) {
286 /* update Manager status(es) every 5 seconds */
287 if (time_msec() >= status_timer) {
288 status_timer = time_msec() + 5000;
289 update_remote_status(jsonrpc, &remotes, &all_dbs);
293 ovsdb_jsonrpc_server_wait(jsonrpc);
294 unixctl_server_wait(unixctl);
295 SHASH_FOR_EACH(node, &all_dbs) {
296 struct db *db = node->data;
297 ovsdb_trigger_wait(db->db, time_msec());
300 process_wait(run_process);
303 poll_immediate_wake();
305 poll_timer_wait_until(status_timer);
308 ovsdb_jsonrpc_server_destroy(jsonrpc);
309 SHASH_FOR_EACH(node, &all_dbs) {
310 struct db *db = node->data;
311 ovsdb_destroy(db->db);
313 sset_destroy(&remotes);
314 unixctl_server_destroy(unixctl);
316 if (run_process && process_exited(run_process)) {
317 int status = process_status(run_process);
319 ovs_fatal(0, "%s: child exited, %s",
320 run_command, process_status_msg(status));
328 open_db(struct server_config *config, const char *filename)
330 struct ovsdb_error *db_error;
334 db = xzalloc(sizeof *db);
335 db->filename = xstrdup(filename);
337 db_error = ovsdb_file_open(db->filename, false, &db->db, &db->file);
339 error = ovsdb_error_to_string(db_error);
340 } else if (!ovsdb_jsonrpc_server_add_db(config->jsonrpc, db->db)) {
341 error = xasprintf("%s: duplicate database name", db->db->schema->name);
343 shash_add_assert(config->all_dbs, db->db->schema->name, db);
347 ovsdb_error_destroy(db_error);
348 ovsdb_destroy(db->db);
354 static const struct db *
355 find_db(const struct shash *all_dbs, const char *db_name)
357 struct shash_node *node;
359 SHASH_FOR_EACH(node, all_dbs) {
360 struct db *db = node->data;
361 if (!strcmp(db->db->schema->name, db_name)) {
369 static char * WARN_UNUSED_RESULT
370 parse_db_column__(const struct shash *all_dbs,
371 const char *name_, char *name,
372 const struct db **dbp,
373 const struct ovsdb_table **tablep,
374 const struct ovsdb_column **columnp)
376 const char *db_name, *table_name, *column_name;
377 const struct ovsdb_column *column;
378 const struct ovsdb_table *table;
379 const char *tokens[3];
380 char *save_ptr = NULL;
387 strtok_r(name, ":", &save_ptr); /* "db:" */
388 tokens[0] = strtok_r(NULL, ",", &save_ptr);
389 tokens[1] = strtok_r(NULL, ",", &save_ptr);
390 tokens[2] = strtok_r(NULL, ",", &save_ptr);
391 if (!tokens[0] || !tokens[1] || !tokens[2]) {
392 return xasprintf("\"%s\": invalid syntax", name_);
396 table_name = tokens[1];
397 column_name = tokens[2];
399 db = find_db(all_dbs, tokens[0]);
401 return xasprintf("\"%s\": no database named %s", name_, db_name);
404 table = ovsdb_get_table(db->db, table_name);
406 return xasprintf("\"%s\": no table named %s", name_, table_name);
409 column = ovsdb_table_schema_get_column(table->schema, column_name);
411 return xasprintf("\"%s\": table \"%s\" has no column \"%s\"",
412 name_, table_name, column_name);
421 /* Returns NULL if successful, otherwise a malloc()'d string describing the
423 static char * WARN_UNUSED_RESULT
424 parse_db_column(const struct shash *all_dbs,
426 const struct db **dbp,
427 const struct ovsdb_table **tablep,
428 const struct ovsdb_column **columnp)
430 char *name = xstrdup(name_);
431 char *retval = parse_db_column__(all_dbs, name_, name,
432 dbp, tablep, columnp);
437 /* Returns NULL if successful, otherwise a malloc()'d string describing the
439 static char * WARN_UNUSED_RESULT
440 parse_db_string_column(const struct shash *all_dbs,
442 const struct db **dbp,
443 const struct ovsdb_table **tablep,
444 const struct ovsdb_column **columnp)
448 retval = parse_db_column(all_dbs, name, dbp, tablep, columnp);
453 if ((*columnp)->type.key.type != OVSDB_TYPE_STRING
454 || (*columnp)->type.value.type != OVSDB_TYPE_VOID) {
455 return xasprintf("\"%s\": table \"%s\" column \"%s\" is "
456 "not string or set of strings",
457 name, (*tablep)->schema->name, (*columnp)->name);
464 query_db_string(const struct shash *all_dbs, const char *name,
467 if (!name || strncmp(name, "db:", 3)) {
470 const struct ovsdb_column *column;
471 const struct ovsdb_table *table;
472 const struct ovsdb_row *row;
476 retval = parse_db_string_column(all_dbs, name,
477 &db, &table, &column);
479 ds_put_format(errors, "%s\n", retval);
483 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
484 const struct ovsdb_datum *datum;
487 datum = &row->fields[column->index];
488 for (i = 0; i < datum->n; i++) {
489 if (datum->keys[i].string[0]) {
490 return datum->keys[i].string;
498 static struct ovsdb_jsonrpc_options *
499 add_remote(struct shash *remotes, const char *target)
501 struct ovsdb_jsonrpc_options *options;
503 options = shash_find_data(remotes, target);
505 options = ovsdb_jsonrpc_default_options(target);
506 shash_add(remotes, target, options);
512 static struct ovsdb_datum *
513 get_datum(struct ovsdb_row *row, const char *column_name,
514 const enum ovsdb_atomic_type key_type,
515 const enum ovsdb_atomic_type value_type,
518 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
519 const struct ovsdb_table_schema *schema = row->table->schema;
520 const struct ovsdb_column *column;
522 column = ovsdb_table_schema_get_column(schema, column_name);
524 VLOG_DBG_RL(&rl, "Table `%s' has no `%s' column",
525 schema->name, column_name);
529 if (column->type.key.type != key_type
530 || column->type.value.type != value_type
531 || column->type.n_max != n_max) {
532 if (!VLOG_DROP_DBG(&rl)) {
533 char *type_name = ovsdb_type_to_english(&column->type);
534 VLOG_DBG("Table `%s' column `%s' has type %s, not expected "
535 "key type %s, value type %s, max elements %zd.",
536 schema->name, column_name, type_name,
537 ovsdb_atomic_type_to_string(key_type),
538 ovsdb_atomic_type_to_string(value_type),
545 return &row->fields[column->index];
548 /* Read string-string key-values from a map. Returns the value associated with
549 * 'key', if found, or NULL */
551 read_map_string_column(const struct ovsdb_row *row, const char *column_name,
554 const struct ovsdb_datum *datum;
555 union ovsdb_atom *atom_key = NULL, *atom_value = NULL;
558 datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name,
559 OVSDB_TYPE_STRING, OVSDB_TYPE_STRING, UINT_MAX);
565 for (i = 0; i < datum->n; i++) {
566 atom_key = &datum->keys[i];
567 if (!strcmp(atom_key->string, key)){
568 atom_value = &datum->values[i];
573 return atom_value ? atom_value->string : NULL;
576 static const union ovsdb_atom *
577 read_column(const struct ovsdb_row *row, const char *column_name,
578 enum ovsdb_atomic_type type)
580 const struct ovsdb_datum *datum;
582 datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name, type,
584 return datum && datum->n ? datum->keys : NULL;
588 read_integer_column(const struct ovsdb_row *row, const char *column_name,
589 long long int *integerp)
591 const union ovsdb_atom *atom;
593 atom = read_column(row, column_name, OVSDB_TYPE_INTEGER);
594 *integerp = atom ? atom->integer : 0;
599 read_string_column(const struct ovsdb_row *row, const char *column_name,
600 const char **stringp)
602 const union ovsdb_atom *atom;
604 atom = read_column(row, column_name, OVSDB_TYPE_STRING);
605 *stringp = atom ? atom->string : NULL;
610 write_bool_column(struct ovsdb_row *row, const char *column_name, bool value)
612 const struct ovsdb_column *column;
613 struct ovsdb_datum *datum;
615 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
616 datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
623 ovsdb_datum_destroy(datum, &column->type);
626 datum->keys = xmalloc(sizeof *datum->keys);
627 datum->values = NULL;
630 datum->keys[0].boolean = value;
634 write_string_string_column(struct ovsdb_row *row, const char *column_name,
635 char **keys, char **values, size_t n)
637 const struct ovsdb_column *column;
638 struct ovsdb_datum *datum;
641 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
642 datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
645 for (i = 0; i < n; i++) {
652 /* Free existing data. */
653 ovsdb_datum_destroy(datum, &column->type);
655 /* Allocate space for new values. */
657 datum->keys = xmalloc(n * sizeof *datum->keys);
658 datum->values = xmalloc(n * sizeof *datum->values);
660 for (i = 0; i < n; ++i) {
661 datum->keys[i].string = keys[i];
662 datum->values[i].string = values[i];
665 /* Sort and check constraints. */
666 ovsdb_datum_sort_assert(datum, column->type.key.type);
669 /* Adds a remote and options to 'remotes', based on the Manager table row in
672 add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
674 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
675 struct ovsdb_jsonrpc_options *options;
676 long long int max_backoff, probe_interval;
677 const char *target, *dscp_string;
679 if (!read_string_column(row, "target", &target) || !target) {
680 VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
681 row->table->schema->name);
685 options = add_remote(remotes, target);
686 if (read_integer_column(row, "max_backoff", &max_backoff)) {
687 options->max_backoff = max_backoff;
689 if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
690 options->probe_interval = probe_interval;
693 options->dscp = DSCP_DEFAULT;
694 dscp_string = read_map_string_column(row, "other_config", "dscp");
696 int dscp = atoi(dscp_string);
697 if (dscp >= 0 && dscp <= 63) {
698 options->dscp = dscp;
704 query_db_remotes(const char *name, const struct shash *all_dbs,
705 struct shash *remotes, struct ds *errors)
707 const struct ovsdb_column *column;
708 const struct ovsdb_table *table;
709 const struct ovsdb_row *row;
713 retval = parse_db_column(all_dbs, name, &db, &table, &column);
715 ds_put_format(errors, "%s\n", retval);
720 if (column->type.key.type == OVSDB_TYPE_STRING
721 && column->type.value.type == OVSDB_TYPE_VOID) {
722 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
723 const struct ovsdb_datum *datum;
726 datum = &row->fields[column->index];
727 for (i = 0; i < datum->n; i++) {
728 add_remote(remotes, datum->keys[i].string);
731 } else if (column->type.key.type == OVSDB_TYPE_UUID
732 && column->type.key.u.uuid.refTable
733 && column->type.value.type == OVSDB_TYPE_VOID) {
734 const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
735 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
736 const struct ovsdb_datum *datum;
739 datum = &row->fields[column->index];
740 for (i = 0; i < datum->n; i++) {
741 const struct ovsdb_row *ref_row;
743 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
745 add_manager_options(remotes, ref_row);
753 update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
754 const struct ovsdb_jsonrpc_server *jsonrpc)
756 struct ovsdb_jsonrpc_remote_status status;
757 struct ovsdb_row *rw_row;
759 char *keys[9], *values[9];
762 /* Get the "target" (protocol/host/port) spec. */
763 if (!read_string_column(row, "target", &target)) {
764 /* Bad remote spec or incorrect schema. */
767 rw_row = ovsdb_txn_row_modify(txn, row);
768 ovsdb_jsonrpc_server_get_remote_status(jsonrpc, target, &status);
770 /* Update status information columns. */
771 write_bool_column(rw_row, "is_connected", status.is_connected);
774 keys[n] = xstrdup("state");
775 values[n++] = xstrdup(status.state);
777 if (status.sec_since_connect != UINT_MAX) {
778 keys[n] = xstrdup("sec_since_connect");
779 values[n++] = xasprintf("%u", status.sec_since_connect);
781 if (status.sec_since_disconnect != UINT_MAX) {
782 keys[n] = xstrdup("sec_since_disconnect");
783 values[n++] = xasprintf("%u", status.sec_since_disconnect);
785 if (status.last_error) {
786 keys[n] = xstrdup("last_error");
788 xstrdup(ovs_retval_to_string(status.last_error));
790 if (status.locks_held && status.locks_held[0]) {
791 keys[n] = xstrdup("locks_held");
792 values[n++] = xstrdup(status.locks_held);
794 if (status.locks_waiting && status.locks_waiting[0]) {
795 keys[n] = xstrdup("locks_waiting");
796 values[n++] = xstrdup(status.locks_waiting);
798 if (status.locks_lost && status.locks_lost[0]) {
799 keys[n] = xstrdup("locks_lost");
800 values[n++] = xstrdup(status.locks_lost);
802 if (status.n_connections > 1) {
803 keys[n] = xstrdup("n_connections");
804 values[n++] = xasprintf("%d", status.n_connections);
806 if (status.bound_port != htons(0)) {
807 keys[n] = xstrdup("bound_port");
808 values[n++] = xasprintf("%"PRIu16, ntohs(status.bound_port));
810 write_string_string_column(rw_row, "status", keys, values, n);
812 ovsdb_jsonrpc_server_free_remote_status(&status);
816 update_remote_rows(const struct shash *all_dbs,
817 const char *remote_name,
818 const struct ovsdb_jsonrpc_server *jsonrpc)
820 const struct ovsdb_table *table, *ref_table;
821 const struct ovsdb_column *column;
822 const struct ovsdb_row *row;
826 if (strncmp("db:", remote_name, 3)) {
830 retval = parse_db_column(all_dbs, remote_name, &db, &table, &column);
836 if (column->type.key.type != OVSDB_TYPE_UUID
837 || !column->type.key.u.uuid.refTable
838 || column->type.value.type != OVSDB_TYPE_VOID) {
842 ref_table = column->type.key.u.uuid.refTable;
844 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
845 const struct ovsdb_datum *datum;
848 datum = &row->fields[column->index];
849 for (i = 0; i < datum->n; i++) {
850 const struct ovsdb_row *ref_row;
852 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
854 update_remote_row(ref_row, db->txn, jsonrpc);
861 update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
862 const struct sset *remotes,
863 struct shash *all_dbs)
865 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
868 struct shash_node *node;
870 SHASH_FOR_EACH(node, all_dbs) {
872 db->txn = ovsdb_txn_create(db->db);
875 /* Iterate over --remote arguments given on command line. */
876 SSET_FOR_EACH (remote, remotes) {
877 update_remote_rows(all_dbs, remote, jsonrpc);
880 SHASH_FOR_EACH(node, all_dbs) {
881 struct ovsdb_error *error;
883 error = ovsdb_txn_commit(db->txn, false);
885 VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
886 ovsdb_error_to_string(error));
887 ovsdb_error_destroy(error);
892 /* Reconfigures ovsdb-server's remotes based on information in the database. */
894 reconfigure_remotes(struct ovsdb_jsonrpc_server *jsonrpc,
895 const struct shash *all_dbs, struct sset *remotes)
897 struct ds errors = DS_EMPTY_INITIALIZER;
898 struct shash resolved_remotes;
901 /* Configure remotes. */
902 shash_init(&resolved_remotes);
903 SSET_FOR_EACH (name, remotes) {
904 if (!strncmp(name, "db:", 3)) {
905 query_db_remotes(name, all_dbs, &resolved_remotes, &errors);
907 add_remote(&resolved_remotes, name);
910 ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
911 shash_destroy_free_data(&resolved_remotes);
913 return errors.string;
917 reconfigure_ssl(const struct shash *all_dbs)
919 struct ds errors = DS_EMPTY_INITIALIZER;
920 const char *resolved_private_key;
921 const char *resolved_certificate;
922 const char *resolved_ca_cert;
924 resolved_private_key = query_db_string(all_dbs, private_key_file, &errors);
925 resolved_certificate = query_db_string(all_dbs, certificate_file, &errors);
926 resolved_ca_cert = query_db_string(all_dbs, ca_cert_file, &errors);
928 stream_ssl_set_key_and_cert(resolved_private_key, resolved_certificate);
929 stream_ssl_set_ca_cert_file(resolved_ca_cert, bootstrap_ca_cert);
931 return errors.string;
935 report_error_if_changed(char *error, char **last_errorp)
938 if (!*last_errorp || strcmp(error, *last_errorp)) {
939 VLOG_WARN("%s", error);
941 *last_errorp = error;
952 ovsdb_server_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
953 const char *argv[] OVS_UNUSED,
956 bool *exiting = exiting_;
958 unixctl_command_reply(conn, NULL);
962 ovsdb_server_compact(struct unixctl_conn *conn, int argc,
963 const char *argv[], void *dbs_)
965 struct shash *all_dbs = dbs_;
968 struct shash_node *node;
972 SHASH_FOR_EACH(node, all_dbs) {
976 name = db->db->schema->name;
978 if (argc < 2 || !strcmp(argv[1], name)) {
979 struct ovsdb_error *error;
981 VLOG_INFO("compacting %s database by user request", name);
983 error = ovsdb_file_compact(db->file);
985 char *s = ovsdb_error_to_string(error);
986 ds_put_format(&reply, "%s\n", s);
995 unixctl_command_reply_error(conn, "no database by that name");
996 } else if (reply.length) {
997 unixctl_command_reply_error(conn, ds_cstr(&reply));
999 unixctl_command_reply(conn, NULL);
1004 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
1005 * connections and reconnect. */
1007 ovsdb_server_reconnect(struct unixctl_conn *conn, int argc OVS_UNUSED,
1008 const char *argv[] OVS_UNUSED, void *jsonrpc_)
1010 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
1012 ovsdb_jsonrpc_server_reconnect(jsonrpc);
1013 unixctl_command_reply(conn, NULL);
1016 /* "ovsdb-server/add-remote REMOTE": adds REMOTE to the set of remotes that
1017 * ovsdb-server services. */
1019 ovsdb_server_add_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
1020 const char *argv[], void *config_)
1022 struct server_config *config = config_;
1023 const char *remote = argv[1];
1025 const struct ovsdb_column *column;
1026 const struct ovsdb_table *table;
1027 const struct db *db;
1030 retval = (strncmp("db:", remote, 3)
1032 : parse_db_column(config->all_dbs, remote,
1033 &db, &table, &column));
1035 if (sset_add(config->remotes, remote)) {
1036 save_config(config);
1038 unixctl_command_reply(conn, NULL);
1040 unixctl_command_reply_error(conn, retval);
1045 /* "ovsdb-server/remove-remote REMOTE": removes REMOTE frmo the set of remotes
1046 * that ovsdb-server services. */
1048 ovsdb_server_remove_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
1049 const char *argv[], void *config_)
1051 struct server_config *config = config_;
1052 struct sset_node *node;
1054 node = sset_find(config->remotes, argv[1]);
1056 sset_delete(config->remotes, node);
1057 save_config(config);
1058 unixctl_command_reply(conn, NULL);
1060 unixctl_command_reply_error(conn, "no such remote");
1064 /* "ovsdb-server/list-remotes": outputs a list of configured rmeotes. */
1066 ovsdb_server_list_remotes(struct unixctl_conn *conn, int argc OVS_UNUSED,
1067 const char *argv[] OVS_UNUSED, void *remotes_)
1069 struct sset *remotes = remotes_;
1070 const char **list, **p;
1075 list = sset_sort(remotes);
1076 for (p = list; *p; p++) {
1077 ds_put_format(&s, "%s\n", *p);
1081 unixctl_command_reply(conn, ds_cstr(&s));
1086 /* "ovsdb-server/add-db DB": adds the DB to ovsdb-server. */
1088 ovsdb_server_add_database(struct unixctl_conn *conn, int argc OVS_UNUSED,
1089 const char *argv[], void *config_)
1091 struct server_config *config = config_;
1092 const char *filename = argv[1];
1095 error = open_db(config, filename);
1097 save_config(config);
1098 unixctl_command_reply(conn, NULL);
1100 unixctl_command_reply_error(conn, error);
1106 ovsdb_server_remove_database(struct unixctl_conn *conn, int argc OVS_UNUSED,
1107 const char *argv[], void *config_)
1109 struct server_config *config = config_;
1110 struct shash_node *node;
1114 node = shash_find(config->all_dbs, argv[1]);
1116 unixctl_command_reply_error(conn, "Failed to find the database.");
1121 ok = ovsdb_jsonrpc_server_remove_db(config->jsonrpc, db->db);
1124 ovsdb_destroy(db->db);
1125 shash_delete(config->all_dbs, node);
1129 save_config(config);
1130 unixctl_command_reply(conn, NULL);
1134 ovsdb_server_list_databases(struct unixctl_conn *conn, int argc OVS_UNUSED,
1135 const char *argv[] OVS_UNUSED, void *all_dbs_)
1137 struct shash *all_dbs = all_dbs_;
1138 const struct shash_node **nodes;
1144 nodes = shash_sort(all_dbs);
1145 for (i = 0; i < shash_count(all_dbs); i++) {
1146 struct db *db = nodes[i]->data;
1147 ds_put_format(&s, "%s\n", db->db->schema->name);
1151 unixctl_command_reply(conn, ds_cstr(&s));
1156 parse_options(int *argcp, char **argvp[],
1157 struct sset *remotes, char **unixctl_pathp, char **run_command)
1160 OPT_REMOTE = UCHAR_MAX + 1,
1163 OPT_BOOTSTRAP_CA_CERT,
1168 static const struct option long_options[] = {
1169 {"remote", required_argument, NULL, OPT_REMOTE},
1170 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
1171 {"run", required_argument, NULL, OPT_RUN},
1172 {"help", no_argument, NULL, 'h'},
1173 {"version", no_argument, NULL, 'V'},
1174 DAEMON_LONG_OPTIONS,
1176 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
1177 {"private-key", required_argument, NULL, 'p'},
1178 {"certificate", required_argument, NULL, 'c'},
1179 {"ca-cert", required_argument, NULL, 'C'},
1180 {"enable-dummy", optional_argument, NULL, OPT_ENABLE_DUMMY},
1183 char *short_options = long_options_to_short_options(long_options);
1185 char **argv = *argvp;
1191 c = getopt_long(argc, argv, short_options, long_options, NULL);
1198 sset_add(remotes, optarg);
1202 *unixctl_pathp = optarg;
1206 *run_command = optarg;
1213 ovs_print_version(0, 0);
1216 VLOG_OPTION_HANDLERS
1217 DAEMON_OPTION_HANDLERS
1220 private_key_file = optarg;
1224 certificate_file = optarg;
1228 ca_cert_file = optarg;
1229 bootstrap_ca_cert = false;
1232 case OPT_BOOTSTRAP_CA_CERT:
1233 ca_cert_file = optarg;
1234 bootstrap_ca_cert = true;
1237 case OPT_ENABLE_DUMMY:
1238 dummy_enable(optarg && !strcmp(optarg, "override"));
1248 free(short_options);
1257 printf("%s: Open vSwitch database server\n"
1258 "usage: %s [OPTIONS] [DATABASE...]\n"
1259 "where each DATABASE is a database file in ovsdb format.\n"
1260 "The default DATABASE, if none is given, is\n%s/conf.db.\n",
1261 program_name, program_name, ovs_dbdir());
1262 printf("\nJSON-RPC options (may be specified any number of times):\n"
1263 " --remote=REMOTE connect or listen to REMOTE\n");
1264 stream_usage("JSON-RPC", true, true, true);
1267 printf("\nOther options:\n"
1268 " --run COMMAND run COMMAND as subprocess then exit\n"
1269 " --unixctl=SOCKET override default control socket name\n"
1270 " -h, --help display this help message\n"
1271 " -V, --version display version information\n");
1275 static struct json *
1276 sset_to_json(const struct sset *sset)
1281 array = json_array_create_empty();
1282 SSET_FOR_EACH (s, sset) {
1283 json_array_add(array, json_string_create(s));
1288 /* Truncates and replaces the contents of 'config_file' by a representation of
1289 * 'remotes' and 'db_filenames'. */
1291 save_config__(FILE *config_file, const struct sset *remotes,
1292 const struct sset *db_filenames)
1297 if (ftruncate(fileno(config_file), 0) == -1) {
1298 VLOG_FATAL("failed to truncate temporary file (%s)",
1299 ovs_strerror(errno));
1302 obj = json_object_create();
1303 json_object_put(obj, "remotes", sset_to_json(remotes));
1304 json_object_put(obj, "db_filenames", sset_to_json(db_filenames));
1305 s = json_to_string(obj, 0);
1308 if (fseek(config_file, 0, SEEK_SET) != 0
1309 || fputs(s, config_file) == EOF
1310 || fflush(config_file) == EOF) {
1311 VLOG_FATAL("failed to write temporary file (%s)", ovs_strerror(errno));
1316 /* Truncates and replaces the contents of 'config_file' by a representation of
1319 save_config(struct server_config *config)
1321 struct sset db_filenames;
1322 struct shash_node *node;
1324 sset_init(&db_filenames);
1325 SHASH_FOR_EACH (node, config->all_dbs) {
1326 struct db *db = node->data;
1327 sset_add(&db_filenames, db->filename);
1330 save_config__(config->config_tmpfile, config->remotes, &db_filenames);
1332 sset_destroy(&db_filenames);
1336 sset_from_json(struct sset *sset, const struct json *array)
1342 ovs_assert(array->type == JSON_ARRAY);
1343 for (i = 0; i < array->u.array.n; i++) {
1344 const struct json *elem = array->u.array.elems[i];
1345 sset_add(sset, json_string(elem));
1349 /* Clears and replaces 'remotes' and 'dbnames' by a configuration read from
1350 * 'config_file', which must have been previously written by save_config(). */
1352 load_config(FILE *config_file, struct sset *remotes, struct sset *db_filenames)
1356 if (fseek(config_file, 0, SEEK_SET) != 0) {
1357 VLOG_FATAL("seek failed in temporary file (%s)", ovs_strerror(errno));
1359 json = json_from_stream(config_file);
1360 if (json->type == JSON_STRING) {
1361 VLOG_FATAL("reading json failed (%s)", json_string(json));
1363 ovs_assert(json->type == JSON_OBJECT);
1365 sset_from_json(remotes, shash_find_data(json_object(json), "remotes"));
1366 sset_from_json(db_filenames,
1367 shash_find_data(json_object(json), "db_filenames"));