1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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"
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 "poll-loop.h"
45 #include "replication.h"
49 #include "stream-ssl.h"
54 #include "transaction.h"
58 #include "perf-counter.h"
59 #include "openvswitch/vlog.h"
61 VLOG_DEFINE_THIS_MODULE(ovsdb_server);
65 /* SSL configuration. */
66 static char *private_key_file;
67 static char *certificate_file;
68 static char *ca_cert_file;
69 static bool bootstrap_ca_cert;
71 /* Replication configuration. */
72 static bool connect_to_remote_server;
74 static unixctl_cb_func ovsdb_server_exit;
75 static unixctl_cb_func ovsdb_server_compact;
76 static unixctl_cb_func ovsdb_server_reconnect;
77 static unixctl_cb_func ovsdb_server_perf_counters_clear;
78 static unixctl_cb_func ovsdb_server_perf_counters_show;
79 static unixctl_cb_func ovsdb_server_disable_monitor2;
81 struct server_config {
83 struct shash *all_dbs;
85 struct ovsdb_jsonrpc_server *jsonrpc;
87 static unixctl_cb_func ovsdb_server_add_remote;
88 static unixctl_cb_func ovsdb_server_remove_remote;
89 static unixctl_cb_func ovsdb_server_list_remotes;
91 static unixctl_cb_func ovsdb_server_add_database;
92 static unixctl_cb_func ovsdb_server_remove_database;
93 static unixctl_cb_func ovsdb_server_list_databases;
95 static char *open_db(struct server_config *config, const char *filename);
96 static void close_db(struct db *db);
98 static void parse_options(int *argc, char **argvp[],
99 struct sset *remotes, char **unixctl_pathp,
101 OVS_NO_RETURN static void usage(void);
103 static char *reconfigure_remotes(struct ovsdb_jsonrpc_server *,
104 const struct shash *all_dbs,
105 struct sset *remotes);
106 static char *reconfigure_ssl(const struct shash *all_dbs);
107 static void report_error_if_changed(char *error, char **last_errorp);
109 static void update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
110 const struct sset *remotes,
111 struct shash *all_dbs);
113 static void save_config__(FILE *config_file, const struct sset *remotes,
114 const struct sset *db_filenames);
115 static void save_config(struct server_config *);
116 static void load_config(FILE *config_file, struct sset *remotes,
117 struct sset *db_filenames);
120 main_loop(struct ovsdb_jsonrpc_server *jsonrpc, struct shash *all_dbs,
121 struct unixctl_server *unixctl, struct sset *remotes,
122 struct process *run_process, bool *exiting)
124 char *remotes_error, *ssl_error;
125 struct shash_node *node;
126 long long int status_timer = LLONG_MIN;
130 remotes_error = NULL;
133 if (memory_should_report()) {
137 ovsdb_jsonrpc_server_get_memory_usage(jsonrpc, &usage);
138 ovsdb_monitor_get_memory_usage(&usage);
139 SHASH_FOR_EACH(node, all_dbs) {
140 struct db *db = node->data;
141 ovsdb_get_memory_usage(db->db, &usage);
143 memory_report(&usage);
144 simap_destroy(&usage);
147 /* Run unixctl_server_run() before reconfigure_remotes() because
148 * ovsdb-server/add-remote and ovsdb-server/remove-remote can change
149 * the set of remotes that reconfigure_remotes() uses. */
150 unixctl_server_run(unixctl);
152 report_error_if_changed(
153 reconfigure_remotes(jsonrpc, all_dbs, remotes),
155 report_error_if_changed(reconfigure_ssl(all_dbs), &ssl_error);
156 ovsdb_jsonrpc_server_run(jsonrpc);
158 if (connect_to_remote_server) {
159 replication_run(all_dbs);
162 SHASH_FOR_EACH(node, all_dbs) {
163 struct db *db = node->data;
164 ovsdb_trigger_run(db->db, time_msec());
168 if (process_exited(run_process)) {
173 /* update Manager status(es) every 2.5 seconds */
174 if (time_msec() >= status_timer) {
175 status_timer = time_msec() + 2500;
176 update_remote_status(jsonrpc, remotes, all_dbs);
180 ovsdb_jsonrpc_server_wait(jsonrpc);
181 unixctl_server_wait(unixctl);
182 SHASH_FOR_EACH(node, all_dbs) {
183 struct db *db = node->data;
184 ovsdb_trigger_wait(db->db, time_msec());
187 process_wait(run_process);
190 poll_immediate_wake();
192 poll_timer_wait_until(status_timer);
194 if (should_service_stop()) {
203 main(int argc, char *argv[])
205 char *unixctl_path = NULL;
206 char *run_command = NULL;
207 struct unixctl_server *unixctl;
208 struct ovsdb_jsonrpc_server *jsonrpc;
209 struct sset remotes, db_filenames;
210 const char *db_filename;
211 struct process *run_process;
214 FILE *config_tmpfile;
215 struct server_config server_config;
216 struct shash all_dbs;
217 struct shash_node *node, *next;
221 ovs_cmdl_proctitle_init(argc, argv);
222 set_program_name(argv[0]);
223 service_start(&argc, &argv);
224 fatal_ignore_sigpipe();
227 parse_options(&argc, &argv, &remotes, &unixctl_path, &run_command);
228 daemon_become_new_user(false);
230 /* Create and initialize 'config_tmpfile' as a temporary file to hold
231 * ovsdb-server's most basic configuration, and then save our initial
232 * configuration to it. When --monitor is used, this preserves the effects
233 * of ovs-appctl commands such as ovsdb-server/add-remote (which saves the
234 * new configuration) across crashes. */
235 config_tmpfile = tmpfile();
236 if (!config_tmpfile) {
237 ovs_fatal(errno, "failed to create temporary file");
240 sset_init(&db_filenames);
242 for (i = 0; i < argc; i++) {
243 sset_add(&db_filenames, argv[i]);
246 char *default_db = xasprintf("%s/conf.db", ovs_dbdir());
247 sset_add(&db_filenames, default_db);
251 server_config.remotes = &remotes;
252 server_config.config_tmpfile = config_tmpfile;
254 save_config__(config_tmpfile, &remotes, &db_filenames);
256 daemonize_start(false);
258 /* Load the saved config. */
259 load_config(config_tmpfile, &remotes, &db_filenames);
260 jsonrpc = ovsdb_jsonrpc_server_create();
262 shash_init(&all_dbs);
263 server_config.all_dbs = &all_dbs;
264 server_config.jsonrpc = jsonrpc;
266 perf_counters_init();
268 SSET_FOR_EACH (db_filename, &db_filenames) {
269 error = open_db(&server_config, db_filename);
271 ovs_fatal(0, "%s", error);
275 error = reconfigure_remotes(jsonrpc, &all_dbs, &remotes);
277 error = reconfigure_ssl(&all_dbs);
280 ovs_fatal(0, "%s", error);
283 retval = unixctl_server_create(unixctl_path, &unixctl);
291 run_argv[0] = "/bin/sh";
293 run_argv[2] = run_command;
296 retval = process_start(run_argv, &run_process);
298 ovs_fatal(retval, "%s: process failed to start", run_command);
304 daemonize_complete();
307 /* ovsdb-server is usually a long-running process, in which case it
308 * makes plenty of sense to log the version, but --run makes
309 * ovsdb-server more like a command-line tool, so skip it. */
310 VLOG_INFO("%s (Open vSwitch) %s", program_name, VERSION);
313 unixctl_command_register("exit", "", 0, 0, ovsdb_server_exit, &exiting);
314 unixctl_command_register("ovsdb-server/compact", "", 0, 1,
315 ovsdb_server_compact, &all_dbs);
316 unixctl_command_register("ovsdb-server/reconnect", "", 0, 0,
317 ovsdb_server_reconnect, jsonrpc);
319 unixctl_command_register("ovsdb-server/add-remote", "REMOTE", 1, 1,
320 ovsdb_server_add_remote, &server_config);
321 unixctl_command_register("ovsdb-server/remove-remote", "REMOTE", 1, 1,
322 ovsdb_server_remove_remote, &server_config);
323 unixctl_command_register("ovsdb-server/list-remotes", "", 0, 0,
324 ovsdb_server_list_remotes, &remotes);
326 unixctl_command_register("ovsdb-server/add-db", "DB", 1, 1,
327 ovsdb_server_add_database, &server_config);
328 unixctl_command_register("ovsdb-server/remove-db", "DB", 1, 1,
329 ovsdb_server_remove_database, &server_config);
330 unixctl_command_register("ovsdb-server/list-dbs", "", 0, 0,
331 ovsdb_server_list_databases, &all_dbs);
332 unixctl_command_register("ovsdb-server/perf-counters-show", "", 0, 0,
333 ovsdb_server_perf_counters_show, NULL);
334 unixctl_command_register("ovsdb-server/perf-counters-clear", "", 0, 0,
335 ovsdb_server_perf_counters_clear, NULL);
337 /* Simulate the behavior of OVS release prior to version 2.5 that
338 * does not support the monitor2 method. */
339 unixctl_command_register("ovsdb-server/disable-monitor2", "", 0, 0,
340 ovsdb_server_disable_monitor2, jsonrpc);
342 main_loop(jsonrpc, &all_dbs, unixctl, &remotes, run_process, &exiting);
344 ovsdb_jsonrpc_server_destroy(jsonrpc);
345 SHASH_FOR_EACH_SAFE(node, next, &all_dbs) {
346 struct db *db = node->data;
348 shash_delete(&all_dbs, node);
350 shash_destroy(&all_dbs);
351 sset_destroy(&remotes);
352 sset_destroy(&db_filenames);
353 unixctl_server_destroy(unixctl);
354 disconnect_remote_server();
356 if (run_process && process_exited(run_process)) {
357 int status = process_status(run_process);
359 ovs_fatal(0, "%s: child exited, %s",
360 run_command, process_status_msg(status));
363 perf_counters_destroy();
368 /* Returns true if 'filename' is known to be already open as a database,
371 * "False negatives" are possible. */
373 is_already_open(struct server_config *config OVS_UNUSED,
374 const char *filename OVS_UNUSED)
379 if (!stat(filename, &s)) {
380 struct shash_node *node;
382 SHASH_FOR_EACH (node, config->all_dbs) {
383 struct db *db = node->data;
386 if (!stat(db->filename, &s2)
387 && s.st_dev == s2.st_dev
388 && s.st_ino == s2.st_ino) {
399 close_db(struct db *db)
401 ovsdb_destroy(db->db);
407 open_db(struct server_config *config, const char *filename)
409 struct ovsdb_error *db_error;
413 /* If we know that the file is already open, return a good error message.
414 * Otherwise, if the file is open, we'll fail later on with a harder to
415 * interpret file locking error. */
416 if (is_already_open(config, filename)) {
417 return xasprintf("%s: already open", filename);
420 db = xzalloc(sizeof *db);
421 db->filename = xstrdup(filename);
423 db_error = ovsdb_file_open(db->filename, false, &db->db, &db->file);
425 error = ovsdb_error_to_string(db_error);
426 } else if (!ovsdb_jsonrpc_server_add_db(config->jsonrpc, db->db)) {
427 error = xasprintf("%s: duplicate database name", db->db->schema->name);
429 shash_add_assert(config->all_dbs, db->db->schema->name, db);
433 ovsdb_error_destroy(db_error);
438 static char * OVS_WARN_UNUSED_RESULT
439 parse_db_column__(const struct shash *all_dbs,
440 const char *name_, char *name,
441 const struct db **dbp,
442 const struct ovsdb_table **tablep,
443 const struct ovsdb_column **columnp)
445 const char *db_name, *table_name, *column_name;
446 const struct ovsdb_column *column;
447 const struct ovsdb_table *table;
448 const char *tokens[3];
449 char *save_ptr = NULL;
456 strtok_r(name, ":", &save_ptr); /* "db:" */
457 tokens[0] = strtok_r(NULL, ",", &save_ptr);
458 tokens[1] = strtok_r(NULL, ",", &save_ptr);
459 tokens[2] = strtok_r(NULL, ",", &save_ptr);
460 if (!tokens[0] || !tokens[1] || !tokens[2]) {
461 return xasprintf("\"%s\": invalid syntax", name_);
465 table_name = tokens[1];
466 column_name = tokens[2];
468 db = find_db(all_dbs, tokens[0]);
470 return xasprintf("\"%s\": no database named %s", name_, db_name);
473 table = ovsdb_get_table(db->db, table_name);
475 return xasprintf("\"%s\": no table named %s", name_, table_name);
478 column = ovsdb_table_schema_get_column(table->schema, column_name);
480 return xasprintf("\"%s\": table \"%s\" has no column \"%s\"",
481 name_, table_name, column_name);
490 /* Returns NULL if successful, otherwise a malloc()'d string describing the
492 static char * OVS_WARN_UNUSED_RESULT
493 parse_db_column(const struct shash *all_dbs,
495 const struct db **dbp,
496 const struct ovsdb_table **tablep,
497 const struct ovsdb_column **columnp)
499 char *name = xstrdup(name_);
500 char *retval = parse_db_column__(all_dbs, name_, name,
501 dbp, tablep, columnp);
506 /* Returns NULL if successful, otherwise a malloc()'d string describing the
508 static char * OVS_WARN_UNUSED_RESULT
509 parse_db_string_column(const struct shash *all_dbs,
511 const struct db **dbp,
512 const struct ovsdb_table **tablep,
513 const struct ovsdb_column **columnp)
517 retval = parse_db_column(all_dbs, name, dbp, tablep, columnp);
522 if ((*columnp)->type.key.type != OVSDB_TYPE_STRING
523 || (*columnp)->type.value.type != OVSDB_TYPE_VOID) {
524 return xasprintf("\"%s\": table \"%s\" column \"%s\" is "
525 "not string or set of strings",
526 name, (*tablep)->schema->name, (*columnp)->name);
533 query_db_string(const struct shash *all_dbs, const char *name,
536 if (!name || strncmp(name, "db:", 3)) {
539 const struct ovsdb_column *column;
540 const struct ovsdb_table *table;
541 const struct ovsdb_row *row;
545 retval = parse_db_string_column(all_dbs, name,
546 &db, &table, &column);
548 ds_put_format(errors, "%s\n", retval);
553 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
554 const struct ovsdb_datum *datum;
557 datum = &row->fields[column->index];
558 for (i = 0; i < datum->n; i++) {
559 if (datum->keys[i].string[0]) {
560 return datum->keys[i].string;
568 static struct ovsdb_jsonrpc_options *
569 add_remote(struct shash *remotes, const char *target)
571 struct ovsdb_jsonrpc_options *options;
573 options = shash_find_data(remotes, target);
575 options = ovsdb_jsonrpc_default_options(target);
576 shash_add(remotes, target, options);
582 static struct ovsdb_datum *
583 get_datum(struct ovsdb_row *row, const char *column_name,
584 const enum ovsdb_atomic_type key_type,
585 const enum ovsdb_atomic_type value_type,
588 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
589 const struct ovsdb_table_schema *schema = row->table->schema;
590 const struct ovsdb_column *column;
592 column = ovsdb_table_schema_get_column(schema, column_name);
594 VLOG_DBG_RL(&rl, "Table `%s' has no `%s' column",
595 schema->name, column_name);
599 if (column->type.key.type != key_type
600 || column->type.value.type != value_type
601 || column->type.n_max != n_max) {
602 if (!VLOG_DROP_DBG(&rl)) {
603 char *type_name = ovsdb_type_to_english(&column->type);
604 VLOG_DBG("Table `%s' column `%s' has type %s, not expected "
605 "key type %s, value type %s, max elements %"PRIuSIZE".",
606 schema->name, column_name, type_name,
607 ovsdb_atomic_type_to_string(key_type),
608 ovsdb_atomic_type_to_string(value_type),
615 return &row->fields[column->index];
618 /* Read string-string key-values from a map. Returns the value associated with
619 * 'key', if found, or NULL */
621 read_map_string_column(const struct ovsdb_row *row, const char *column_name,
624 const struct ovsdb_datum *datum;
625 union ovsdb_atom *atom_key = NULL, *atom_value = NULL;
628 datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name,
629 OVSDB_TYPE_STRING, OVSDB_TYPE_STRING, UINT_MAX);
635 for (i = 0; i < datum->n; i++) {
636 atom_key = &datum->keys[i];
637 if (!strcmp(atom_key->string, key)){
638 atom_value = &datum->values[i];
643 return atom_value ? atom_value->string : NULL;
646 static const union ovsdb_atom *
647 read_column(const struct ovsdb_row *row, const char *column_name,
648 enum ovsdb_atomic_type type)
650 const struct ovsdb_datum *datum;
652 datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name, type,
654 return datum && datum->n ? datum->keys : NULL;
658 read_integer_column(const struct ovsdb_row *row, const char *column_name,
659 long long int *integerp)
661 const union ovsdb_atom *atom;
663 atom = read_column(row, column_name, OVSDB_TYPE_INTEGER);
664 *integerp = atom ? atom->integer : 0;
669 read_string_column(const struct ovsdb_row *row, const char *column_name,
670 const char **stringp)
672 const union ovsdb_atom *atom;
674 atom = read_column(row, column_name, OVSDB_TYPE_STRING);
675 *stringp = atom ? atom->string : NULL;
680 write_bool_column(struct ovsdb_row *row, const char *column_name, bool value)
682 const struct ovsdb_column *column;
683 struct ovsdb_datum *datum;
685 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
686 datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
693 ovsdb_datum_destroy(datum, &column->type);
696 datum->keys = xmalloc(sizeof *datum->keys);
697 datum->values = NULL;
700 datum->keys[0].boolean = value;
704 write_string_string_column(struct ovsdb_row *row, const char *column_name,
705 char **keys, char **values, size_t n)
707 const struct ovsdb_column *column;
708 struct ovsdb_datum *datum;
711 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
712 datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
715 for (i = 0; i < n; i++) {
722 /* Free existing data. */
723 ovsdb_datum_destroy(datum, &column->type);
725 /* Allocate space for new values. */
727 datum->keys = xmalloc(n * sizeof *datum->keys);
728 datum->values = xmalloc(n * sizeof *datum->values);
730 for (i = 0; i < n; ++i) {
731 datum->keys[i].string = keys[i];
732 datum->values[i].string = values[i];
735 /* Sort and check constraints. */
736 ovsdb_datum_sort_assert(datum, column->type.key.type);
739 /* Adds a remote and options to 'remotes', based on the Manager table row in
742 add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
744 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
745 struct ovsdb_jsonrpc_options *options;
746 long long int max_backoff, probe_interval;
747 const char *target, *dscp_string;
749 if (!read_string_column(row, "target", &target) || !target) {
750 VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
751 row->table->schema->name);
755 options = add_remote(remotes, target);
756 if (read_integer_column(row, "max_backoff", &max_backoff)) {
757 options->max_backoff = max_backoff;
759 if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
760 options->probe_interval = probe_interval;
763 options->dscp = DSCP_DEFAULT;
764 dscp_string = read_map_string_column(row, "other_config", "dscp");
766 int dscp = atoi(dscp_string);
767 if (dscp >= 0 && dscp <= 63) {
768 options->dscp = dscp;
774 query_db_remotes(const char *name, const struct shash *all_dbs,
775 struct shash *remotes, struct ds *errors)
777 const struct ovsdb_column *column;
778 const struct ovsdb_table *table;
779 const struct ovsdb_row *row;
783 retval = parse_db_column(all_dbs, name, &db, &table, &column);
785 ds_put_format(errors, "%s\n", retval);
790 if (column->type.key.type == OVSDB_TYPE_STRING
791 && column->type.value.type == OVSDB_TYPE_VOID) {
792 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
793 const struct ovsdb_datum *datum;
796 datum = &row->fields[column->index];
797 for (i = 0; i < datum->n; i++) {
798 add_remote(remotes, datum->keys[i].string);
801 } else if (column->type.key.type == OVSDB_TYPE_UUID
802 && column->type.key.u.uuid.refTable
803 && column->type.value.type == OVSDB_TYPE_VOID) {
804 const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
805 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
806 const struct ovsdb_datum *datum;
809 datum = &row->fields[column->index];
810 for (i = 0; i < datum->n; i++) {
811 const struct ovsdb_row *ref_row;
813 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
815 add_manager_options(remotes, ref_row);
823 update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
824 const struct ovsdb_jsonrpc_server *jsonrpc)
826 struct ovsdb_jsonrpc_remote_status status;
827 struct ovsdb_row *rw_row;
829 char *keys[9], *values[9];
832 /* Get the "target" (protocol/host/port) spec. */
833 if (!read_string_column(row, "target", &target)) {
834 /* Bad remote spec or incorrect schema. */
837 rw_row = ovsdb_txn_row_modify(txn, row);
838 ovsdb_jsonrpc_server_get_remote_status(jsonrpc, target, &status);
840 /* Update status information columns. */
841 write_bool_column(rw_row, "is_connected", status.is_connected);
844 keys[n] = xstrdup("state");
845 values[n++] = xstrdup(status.state);
847 if (status.sec_since_connect != UINT_MAX) {
848 keys[n] = xstrdup("sec_since_connect");
849 values[n++] = xasprintf("%u", status.sec_since_connect);
851 if (status.sec_since_disconnect != UINT_MAX) {
852 keys[n] = xstrdup("sec_since_disconnect");
853 values[n++] = xasprintf("%u", status.sec_since_disconnect);
855 if (status.last_error) {
856 keys[n] = xstrdup("last_error");
858 xstrdup(ovs_retval_to_string(status.last_error));
860 if (status.locks_held && status.locks_held[0]) {
861 keys[n] = xstrdup("locks_held");
862 values[n++] = xstrdup(status.locks_held);
864 if (status.locks_waiting && status.locks_waiting[0]) {
865 keys[n] = xstrdup("locks_waiting");
866 values[n++] = xstrdup(status.locks_waiting);
868 if (status.locks_lost && status.locks_lost[0]) {
869 keys[n] = xstrdup("locks_lost");
870 values[n++] = xstrdup(status.locks_lost);
872 if (status.n_connections > 1) {
873 keys[n] = xstrdup("n_connections");
874 values[n++] = xasprintf("%d", status.n_connections);
876 if (status.bound_port != htons(0)) {
877 keys[n] = xstrdup("bound_port");
878 values[n++] = xasprintf("%"PRIu16, ntohs(status.bound_port));
880 write_string_string_column(rw_row, "status", keys, values, n);
882 ovsdb_jsonrpc_server_free_remote_status(&status);
886 update_remote_rows(const struct shash *all_dbs,
887 const char *remote_name,
888 const struct ovsdb_jsonrpc_server *jsonrpc)
890 const struct ovsdb_table *table, *ref_table;
891 const struct ovsdb_column *column;
892 const struct ovsdb_row *row;
896 if (strncmp("db:", remote_name, 3)) {
900 retval = parse_db_column(all_dbs, remote_name, &db, &table, &column);
906 if (column->type.key.type != OVSDB_TYPE_UUID
907 || !column->type.key.u.uuid.refTable
908 || column->type.value.type != OVSDB_TYPE_VOID) {
912 ref_table = column->type.key.u.uuid.refTable;
914 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
915 const struct ovsdb_datum *datum;
918 datum = &row->fields[column->index];
919 for (i = 0; i < datum->n; i++) {
920 const struct ovsdb_row *ref_row;
922 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
924 update_remote_row(ref_row, db->txn, jsonrpc);
931 update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
932 const struct sset *remotes,
933 struct shash *all_dbs)
935 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
938 struct shash_node *node;
940 SHASH_FOR_EACH(node, all_dbs) {
942 db->txn = ovsdb_txn_create(db->db);
945 /* Iterate over --remote arguments given on command line. */
946 SSET_FOR_EACH (remote, remotes) {
947 update_remote_rows(all_dbs, remote, jsonrpc);
950 SHASH_FOR_EACH(node, all_dbs) {
951 struct ovsdb_error *error;
953 error = ovsdb_txn_commit(db->txn, false);
955 VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
956 ovsdb_error_to_string(error));
957 ovsdb_error_destroy(error);
962 /* Reconfigures ovsdb-server's remotes based on information in the database. */
964 reconfigure_remotes(struct ovsdb_jsonrpc_server *jsonrpc,
965 const struct shash *all_dbs, struct sset *remotes)
967 struct ds errors = DS_EMPTY_INITIALIZER;
968 struct shash resolved_remotes;
971 /* Configure remotes. */
972 shash_init(&resolved_remotes);
973 SSET_FOR_EACH (name, remotes) {
974 if (!strncmp(name, "db:", 3)) {
975 query_db_remotes(name, all_dbs, &resolved_remotes, &errors);
977 add_remote(&resolved_remotes, name);
980 ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
981 shash_destroy_free_data(&resolved_remotes);
983 return errors.string;
987 reconfigure_ssl(const struct shash *all_dbs)
989 struct ds errors = DS_EMPTY_INITIALIZER;
990 const char *resolved_private_key;
991 const char *resolved_certificate;
992 const char *resolved_ca_cert;
994 resolved_private_key = query_db_string(all_dbs, private_key_file, &errors);
995 resolved_certificate = query_db_string(all_dbs, certificate_file, &errors);
996 resolved_ca_cert = query_db_string(all_dbs, ca_cert_file, &errors);
998 stream_ssl_set_key_and_cert(resolved_private_key, resolved_certificate);
999 stream_ssl_set_ca_cert_file(resolved_ca_cert, bootstrap_ca_cert);
1001 return errors.string;
1005 report_error_if_changed(char *error, char **last_errorp)
1008 if (!*last_errorp || strcmp(error, *last_errorp)) {
1009 VLOG_WARN("%s", error);
1011 *last_errorp = error;
1017 *last_errorp = NULL;
1022 ovsdb_server_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
1023 const char *argv[] OVS_UNUSED,
1026 bool *exiting = exiting_;
1028 unixctl_command_reply(conn, NULL);
1032 ovsdb_server_perf_counters_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
1033 const char *argv[] OVS_UNUSED,
1034 void *arg_ OVS_UNUSED)
1036 char *s = perf_counters_to_string();
1038 unixctl_command_reply(conn, s);
1043 ovsdb_server_perf_counters_clear(struct unixctl_conn *conn, int argc OVS_UNUSED,
1044 const char *argv[] OVS_UNUSED,
1045 void *arg_ OVS_UNUSED)
1047 perf_counters_clear();
1048 unixctl_command_reply(conn, NULL);
1051 /* "ovsdb-server/disable-monitor2": makes ovsdb-server drop all of its
1052 * JSON-RPC connections and reconnect. New sessions will not recognize
1053 * the 'monitor2' method. */
1055 ovsdb_server_disable_monitor2(struct unixctl_conn *conn, int argc OVS_UNUSED,
1056 const char *argv[] OVS_UNUSED, void *jsonrpc_)
1058 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
1060 ovsdb_jsonrpc_disable_monitor2();
1061 ovsdb_jsonrpc_server_reconnect(jsonrpc);
1062 unixctl_command_reply(conn, NULL);
1066 ovsdb_server_compact(struct unixctl_conn *conn, int argc,
1067 const char *argv[], void *dbs_)
1069 struct shash *all_dbs = dbs_;
1072 struct shash_node *node;
1076 SHASH_FOR_EACH(node, all_dbs) {
1080 name = db->db->schema->name;
1082 if (argc < 2 || !strcmp(argv[1], name)) {
1083 struct ovsdb_error *error;
1085 VLOG_INFO("compacting %s database by user request", name);
1087 error = ovsdb_file_compact(db->file);
1089 char *s = ovsdb_error_to_string(error);
1090 ds_put_format(&reply, "%s\n", s);
1092 ovsdb_error_destroy(error);
1100 unixctl_command_reply_error(conn, "no database by that name");
1101 } else if (reply.length) {
1102 unixctl_command_reply_error(conn, ds_cstr(&reply));
1104 unixctl_command_reply(conn, NULL);
1109 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
1110 * connections and reconnect. */
1112 ovsdb_server_reconnect(struct unixctl_conn *conn, int argc OVS_UNUSED,
1113 const char *argv[] OVS_UNUSED, void *jsonrpc_)
1115 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
1117 ovsdb_jsonrpc_server_reconnect(jsonrpc);
1118 unixctl_command_reply(conn, NULL);
1121 /* "ovsdb-server/add-remote REMOTE": adds REMOTE to the set of remotes that
1122 * ovsdb-server services. */
1124 ovsdb_server_add_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
1125 const char *argv[], void *config_)
1127 struct server_config *config = config_;
1128 const char *remote = argv[1];
1130 const struct ovsdb_column *column;
1131 const struct ovsdb_table *table;
1132 const struct db *db;
1135 retval = (strncmp("db:", remote, 3)
1137 : parse_db_column(config->all_dbs, remote,
1138 &db, &table, &column));
1140 if (sset_add(config->remotes, remote)) {
1141 save_config(config);
1143 unixctl_command_reply(conn, NULL);
1145 unixctl_command_reply_error(conn, retval);
1150 /* "ovsdb-server/remove-remote REMOTE": removes REMOTE frmo the set of remotes
1151 * that ovsdb-server services. */
1153 ovsdb_server_remove_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
1154 const char *argv[], void *config_)
1156 struct server_config *config = config_;
1157 struct sset_node *node;
1159 node = sset_find(config->remotes, argv[1]);
1161 sset_delete(config->remotes, node);
1162 save_config(config);
1163 unixctl_command_reply(conn, NULL);
1165 unixctl_command_reply_error(conn, "no such remote");
1169 /* "ovsdb-server/list-remotes": outputs a list of configured rmeotes. */
1171 ovsdb_server_list_remotes(struct unixctl_conn *conn, int argc OVS_UNUSED,
1172 const char *argv[] OVS_UNUSED, void *remotes_)
1174 struct sset *remotes = remotes_;
1175 const char **list, **p;
1180 list = sset_sort(remotes);
1181 for (p = list; *p; p++) {
1182 ds_put_format(&s, "%s\n", *p);
1186 unixctl_command_reply(conn, ds_cstr(&s));
1191 /* "ovsdb-server/add-db DB": adds the DB to ovsdb-server. */
1193 ovsdb_server_add_database(struct unixctl_conn *conn, int argc OVS_UNUSED,
1194 const char *argv[], void *config_)
1196 struct server_config *config = config_;
1197 const char *filename = argv[1];
1200 error = open_db(config, filename);
1202 save_config(config);
1203 unixctl_command_reply(conn, NULL);
1205 unixctl_command_reply_error(conn, error);
1211 ovsdb_server_remove_database(struct unixctl_conn *conn, int argc OVS_UNUSED,
1212 const char *argv[], void *config_)
1214 struct server_config *config = config_;
1215 struct shash_node *node;
1219 node = shash_find(config->all_dbs, argv[1]);
1221 unixctl_command_reply_error(conn, "Failed to find the database.");
1226 ok = ovsdb_jsonrpc_server_remove_db(config->jsonrpc, db->db);
1230 shash_delete(config->all_dbs, node);
1232 save_config(config);
1233 unixctl_command_reply(conn, NULL);
1237 ovsdb_server_list_databases(struct unixctl_conn *conn, int argc OVS_UNUSED,
1238 const char *argv[] OVS_UNUSED, void *all_dbs_)
1240 struct shash *all_dbs = all_dbs_;
1241 const struct shash_node **nodes;
1247 nodes = shash_sort(all_dbs);
1248 for (i = 0; i < shash_count(all_dbs); i++) {
1249 struct db *db = nodes[i]->data;
1250 ds_put_format(&s, "%s\n", db->db->schema->name);
1254 unixctl_command_reply(conn, ds_cstr(&s));
1259 parse_options(int *argcp, char **argvp[],
1260 struct sset *remotes, char **unixctl_pathp, char **run_command)
1263 OPT_REMOTE = UCHAR_MAX + 1,
1266 OPT_BOOTSTRAP_CA_CERT,
1273 static const struct option long_options[] = {
1274 {"remote", required_argument, NULL, OPT_REMOTE},
1275 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
1277 {"run", required_argument, NULL, OPT_RUN},
1279 {"help", no_argument, NULL, 'h'},
1280 {"version", no_argument, NULL, 'V'},
1281 DAEMON_LONG_OPTIONS,
1283 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
1284 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
1285 {"private-key", required_argument, NULL, 'p'},
1286 {"certificate", required_argument, NULL, 'c'},
1287 {"ca-cert", required_argument, NULL, 'C'},
1288 {"sync-from", required_argument, NULL, OPT_SYNC_FROM},
1289 {"sync-exclude-tables", required_argument, NULL, OPT_SYNC_EXCLUDE},
1292 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
1294 char **argv = *argvp;
1300 c = getopt_long(argc, argv, short_options, long_options, NULL);
1307 sset_add(remotes, optarg);
1311 *unixctl_pathp = optarg;
1315 *run_command = optarg;
1322 ovs_print_version(0, 0);
1325 VLOG_OPTION_HANDLERS
1326 DAEMON_OPTION_HANDLERS
1329 private_key_file = optarg;
1333 certificate_file = optarg;
1337 ca_cert_file = optarg;
1338 bootstrap_ca_cert = false;
1341 case OPT_BOOTSTRAP_CA_CERT:
1342 ca_cert_file = optarg;
1343 bootstrap_ca_cert = true;
1346 case OPT_PEER_CA_CERT:
1347 stream_ssl_set_peer_ca_cert_file(optarg);
1351 set_remote_ovsdb_server(optarg);
1352 connect_to_remote_server = true;
1355 case OPT_SYNC_EXCLUDE:
1356 set_tables_blacklist(optarg);
1366 free(short_options);
1375 printf("%s: Open vSwitch database server\n"
1376 "usage: %s [OPTIONS] [DATABASE...]\n"
1377 "where each DATABASE is a database file in ovsdb format.\n"
1378 "The default DATABASE, if none is given, is\n%s/conf.db.\n",
1379 program_name, program_name, ovs_dbdir());
1380 printf("\nJSON-RPC options (may be specified any number of times):\n"
1381 " --remote=REMOTE connect or listen to REMOTE\n");
1382 stream_usage("JSON-RPC", true, true, true);
1385 replication_usage();
1386 printf("\nOther options:\n"
1387 " --run COMMAND run COMMAND as subprocess then exit\n"
1388 " --unixctl=SOCKET override default control socket name\n"
1389 " -h, --help display this help message\n"
1390 " -V, --version display version information\n");
1394 static struct json *
1395 sset_to_json(const struct sset *sset)
1400 array = json_array_create_empty();
1401 SSET_FOR_EACH (s, sset) {
1402 json_array_add(array, json_string_create(s));
1407 /* Truncates and replaces the contents of 'config_file' by a representation of
1408 * 'remotes' and 'db_filenames'. */
1410 save_config__(FILE *config_file, const struct sset *remotes,
1411 const struct sset *db_filenames)
1416 if (ftruncate(fileno(config_file), 0) == -1) {
1417 VLOG_FATAL("failed to truncate temporary file (%s)",
1418 ovs_strerror(errno));
1421 obj = json_object_create();
1422 json_object_put(obj, "remotes", sset_to_json(remotes));
1423 json_object_put(obj, "db_filenames", sset_to_json(db_filenames));
1424 s = json_to_string(obj, 0);
1427 if (fseek(config_file, 0, SEEK_SET) != 0
1428 || fputs(s, config_file) == EOF
1429 || fflush(config_file) == EOF) {
1430 VLOG_FATAL("failed to write temporary file (%s)", ovs_strerror(errno));
1435 /* Truncates and replaces the contents of 'config_file' by a representation of
1438 save_config(struct server_config *config)
1440 struct sset db_filenames;
1441 struct shash_node *node;
1443 sset_init(&db_filenames);
1444 SHASH_FOR_EACH (node, config->all_dbs) {
1445 struct db *db = node->data;
1446 sset_add(&db_filenames, db->filename);
1449 save_config__(config->config_tmpfile, config->remotes, &db_filenames);
1451 sset_destroy(&db_filenames);
1455 sset_from_json(struct sset *sset, const struct json *array)
1461 ovs_assert(array->type == JSON_ARRAY);
1462 for (i = 0; i < array->u.array.n; i++) {
1463 const struct json *elem = array->u.array.elems[i];
1464 sset_add(sset, json_string(elem));
1468 /* Clears and replaces 'remotes' and 'dbnames' by a configuration read from
1469 * 'config_file', which must have been previously written by save_config(). */
1471 load_config(FILE *config_file, struct sset *remotes, struct sset *db_filenames)
1475 if (fseek(config_file, 0, SEEK_SET) != 0) {
1476 VLOG_FATAL("seek failed in temporary file (%s)", ovs_strerror(errno));
1478 json = json_from_stream(config_file);
1479 if (json->type == JSON_STRING) {
1480 VLOG_FATAL("reading json failed (%s)", json_string(json));
1482 ovs_assert(json->type == JSON_OBJECT);
1484 sset_from_json(remotes, shash_find_data(json_object(json), "remotes"));
1485 sset_from_json(db_filenames,
1486 shash_find_data(json_object(json), "db_filenames"));