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"
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 "poll-loop.h"
45 #include "replication.h"
48 #include "openvswitch/shash.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_monitor_cond;
80 static unixctl_cb_func ovsdb_server_set_remote_ovsdb_server;
81 static unixctl_cb_func ovsdb_server_get_remote_ovsdb_server;
82 static unixctl_cb_func ovsdb_server_connect_remote_ovsdb_server;
83 static unixctl_cb_func ovsdb_server_disconnect_remote_ovsdb_server;
84 static unixctl_cb_func ovsdb_server_set_sync_excluded_tables;
85 static unixctl_cb_func ovsdb_server_get_sync_excluded_tables;
87 struct server_config {
89 struct shash *all_dbs;
91 struct ovsdb_jsonrpc_server *jsonrpc;
93 static unixctl_cb_func ovsdb_server_add_remote;
94 static unixctl_cb_func ovsdb_server_remove_remote;
95 static unixctl_cb_func ovsdb_server_list_remotes;
97 static unixctl_cb_func ovsdb_server_add_database;
98 static unixctl_cb_func ovsdb_server_remove_database;
99 static unixctl_cb_func ovsdb_server_list_databases;
101 static char *open_db(struct server_config *config, const char *filename);
102 static void close_db(struct db *db);
104 static void parse_options(int *argc, char **argvp[],
105 struct sset *remotes, char **unixctl_pathp,
107 OVS_NO_RETURN static void usage(void);
109 static char *reconfigure_remotes(struct ovsdb_jsonrpc_server *,
110 const struct shash *all_dbs,
111 struct sset *remotes);
112 static char *reconfigure_ssl(const struct shash *all_dbs);
113 static void report_error_if_changed(char *error, char **last_errorp);
115 static void update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
116 const struct sset *remotes,
117 struct shash *all_dbs);
119 static void save_config__(FILE *config_file, const struct sset *remotes,
120 const struct sset *db_filenames);
121 static void save_config(struct server_config *);
122 static void load_config(FILE *config_file, struct sset *remotes,
123 struct sset *db_filenames);
126 main_loop(struct ovsdb_jsonrpc_server *jsonrpc, struct shash *all_dbs,
127 struct unixctl_server *unixctl, struct sset *remotes,
128 struct process *run_process, bool *exiting)
130 char *remotes_error, *ssl_error;
131 struct shash_node *node;
132 long long int status_timer = LLONG_MIN;
136 remotes_error = NULL;
139 if (memory_should_report()) {
143 ovsdb_jsonrpc_server_get_memory_usage(jsonrpc, &usage);
144 ovsdb_monitor_get_memory_usage(&usage);
145 SHASH_FOR_EACH(node, all_dbs) {
146 struct db *db = node->data;
147 ovsdb_get_memory_usage(db->db, &usage);
149 memory_report(&usage);
150 simap_destroy(&usage);
153 /* Run unixctl_server_run() before reconfigure_remotes() because
154 * ovsdb-server/add-remote and ovsdb-server/remove-remote can change
155 * the set of remotes that reconfigure_remotes() uses. */
156 unixctl_server_run(unixctl);
158 report_error_if_changed(
159 reconfigure_remotes(jsonrpc, all_dbs, remotes),
161 report_error_if_changed(reconfigure_ssl(all_dbs), &ssl_error);
162 ovsdb_jsonrpc_server_run(jsonrpc);
164 if (connect_to_remote_server) {
165 replication_run(all_dbs);
168 SHASH_FOR_EACH(node, all_dbs) {
169 struct db *db = node->data;
170 ovsdb_trigger_run(db->db, time_msec());
174 if (process_exited(run_process)) {
179 /* update Manager status(es) every 2.5 seconds */
180 if (time_msec() >= status_timer) {
181 status_timer = time_msec() + 2500;
182 update_remote_status(jsonrpc, remotes, all_dbs);
186 ovsdb_jsonrpc_server_wait(jsonrpc);
187 unixctl_server_wait(unixctl);
188 SHASH_FOR_EACH(node, all_dbs) {
189 struct db *db = node->data;
190 ovsdb_trigger_wait(db->db, time_msec());
193 process_wait(run_process);
196 poll_immediate_wake();
198 poll_timer_wait_until(status_timer);
200 if (should_service_stop()) {
209 main(int argc, char *argv[])
211 char *unixctl_path = NULL;
212 char *run_command = NULL;
213 struct unixctl_server *unixctl;
214 struct ovsdb_jsonrpc_server *jsonrpc;
215 struct sset remotes, db_filenames;
216 const char *db_filename;
217 struct process *run_process;
220 FILE *config_tmpfile;
221 struct server_config server_config;
222 struct shash all_dbs;
223 struct shash_node *node, *next;
227 ovs_cmdl_proctitle_init(argc, argv);
228 set_program_name(argv[0]);
229 service_start(&argc, &argv);
230 fatal_ignore_sigpipe();
233 parse_options(&argc, &argv, &remotes, &unixctl_path, &run_command);
234 daemon_become_new_user(false);
236 /* Create and initialize 'config_tmpfile' as a temporary file to hold
237 * ovsdb-server's most basic configuration, and then save our initial
238 * configuration to it. When --monitor is used, this preserves the effects
239 * of ovs-appctl commands such as ovsdb-server/add-remote (which saves the
240 * new configuration) across crashes. */
241 config_tmpfile = tmpfile();
242 if (!config_tmpfile) {
243 ovs_fatal(errno, "failed to create temporary file");
246 sset_init(&db_filenames);
248 for (i = 0; i < argc; i++) {
249 sset_add(&db_filenames, argv[i]);
252 char *default_db = xasprintf("%s/conf.db", ovs_dbdir());
253 sset_add(&db_filenames, default_db);
257 server_config.remotes = &remotes;
258 server_config.config_tmpfile = config_tmpfile;
260 save_config__(config_tmpfile, &remotes, &db_filenames);
262 daemonize_start(false);
264 /* Load the saved config. */
265 load_config(config_tmpfile, &remotes, &db_filenames);
266 jsonrpc = ovsdb_jsonrpc_server_create();
268 shash_init(&all_dbs);
269 server_config.all_dbs = &all_dbs;
270 server_config.jsonrpc = jsonrpc;
272 perf_counters_init();
274 SSET_FOR_EACH (db_filename, &db_filenames) {
275 error = open_db(&server_config, db_filename);
277 ovs_fatal(0, "%s", error);
281 error = reconfigure_remotes(jsonrpc, &all_dbs, &remotes);
283 error = reconfigure_ssl(&all_dbs);
286 ovs_fatal(0, "%s", error);
289 retval = unixctl_server_create(unixctl_path, &unixctl);
297 run_argv[0] = "/bin/sh";
299 run_argv[2] = run_command;
302 retval = process_start(run_argv, &run_process);
304 ovs_fatal(retval, "%s: process failed to start", run_command);
310 daemonize_complete();
313 /* ovsdb-server is usually a long-running process, in which case it
314 * makes plenty of sense to log the version, but --run makes
315 * ovsdb-server more like a command-line tool, so skip it. */
316 VLOG_INFO("%s (Open vSwitch) %s", program_name, VERSION);
319 unixctl_command_register("exit", "", 0, 0, ovsdb_server_exit, &exiting);
320 unixctl_command_register("ovsdb-server/compact", "", 0, 1,
321 ovsdb_server_compact, &all_dbs);
322 unixctl_command_register("ovsdb-server/reconnect", "", 0, 0,
323 ovsdb_server_reconnect, jsonrpc);
325 unixctl_command_register("ovsdb-server/add-remote", "REMOTE", 1, 1,
326 ovsdb_server_add_remote, &server_config);
327 unixctl_command_register("ovsdb-server/remove-remote", "REMOTE", 1, 1,
328 ovsdb_server_remove_remote, &server_config);
329 unixctl_command_register("ovsdb-server/list-remotes", "", 0, 0,
330 ovsdb_server_list_remotes, &remotes);
332 unixctl_command_register("ovsdb-server/add-db", "DB", 1, 1,
333 ovsdb_server_add_database, &server_config);
334 unixctl_command_register("ovsdb-server/remove-db", "DB", 1, 1,
335 ovsdb_server_remove_database, &server_config);
336 unixctl_command_register("ovsdb-server/list-dbs", "", 0, 0,
337 ovsdb_server_list_databases, &all_dbs);
338 unixctl_command_register("ovsdb-server/perf-counters-show", "", 0, 0,
339 ovsdb_server_perf_counters_show, NULL);
340 unixctl_command_register("ovsdb-server/perf-counters-clear", "", 0, 0,
341 ovsdb_server_perf_counters_clear, NULL);
343 unixctl_command_register("ovsdb-server/set-remote-ovsdb-server", "", 0, 1,
344 ovsdb_server_set_remote_ovsdb_server, NULL);
345 unixctl_command_register("ovsdb-server/get-remote-ovsdb-server", "", 0, 0,
346 ovsdb_server_get_remote_ovsdb_server, NULL);
347 unixctl_command_register("ovsdb-server/connect-remote-ovsdb-server", "", 0, 0,
348 ovsdb_server_connect_remote_ovsdb_server, NULL);
349 unixctl_command_register("ovsdb-server/disconnect-remote-ovsdb-server", "", 0, 0,
350 ovsdb_server_disconnect_remote_ovsdb_server, NULL);
351 unixctl_command_register("ovsdb-server/set-sync-excluded-tables", "", 0, 1,
352 ovsdb_server_set_sync_excluded_tables, NULL);
353 unixctl_command_register("ovsdb-server/get-sync-excluded-tables", "", 0, 0,
354 ovsdb_server_get_sync_excluded_tables, NULL);
356 /* Simulate the behavior of OVS release prior to version 2.5 that
357 * does not support the monitor_cond method. */
358 unixctl_command_register("ovsdb-server/disable-monitor-cond", "", 0, 0,
359 ovsdb_server_disable_monitor_cond, jsonrpc);
361 main_loop(jsonrpc, &all_dbs, unixctl, &remotes, run_process, &exiting);
363 ovsdb_jsonrpc_server_destroy(jsonrpc);
364 SHASH_FOR_EACH_SAFE(node, next, &all_dbs) {
365 struct db *db = node->data;
367 shash_delete(&all_dbs, node);
369 shash_destroy(&all_dbs);
370 sset_destroy(&remotes);
371 sset_destroy(&db_filenames);
372 unixctl_server_destroy(unixctl);
373 disconnect_remote_server();
375 if (run_process && process_exited(run_process)) {
376 int status = process_status(run_process);
378 ovs_fatal(0, "%s: child exited, %s",
379 run_command, process_status_msg(status));
382 perf_counters_destroy();
387 /* Returns true if 'filename' is known to be already open as a database,
390 * "False negatives" are possible. */
392 is_already_open(struct server_config *config OVS_UNUSED,
393 const char *filename OVS_UNUSED)
398 if (!stat(filename, &s)) {
399 struct shash_node *node;
401 SHASH_FOR_EACH (node, config->all_dbs) {
402 struct db *db = node->data;
405 if (!stat(db->filename, &s2)
406 && s.st_dev == s2.st_dev
407 && s.st_ino == s2.st_ino) {
418 close_db(struct db *db)
420 ovsdb_destroy(db->db);
426 open_db(struct server_config *config, const char *filename)
428 struct ovsdb_error *db_error;
432 /* If we know that the file is already open, return a good error message.
433 * Otherwise, if the file is open, we'll fail later on with a harder to
434 * interpret file locking error. */
435 if (is_already_open(config, filename)) {
436 return xasprintf("%s: already open", filename);
439 db = xzalloc(sizeof *db);
440 db->filename = xstrdup(filename);
442 db_error = ovsdb_file_open(db->filename, false, &db->db, &db->file);
444 error = ovsdb_error_to_string(db_error);
445 } else if (!ovsdb_jsonrpc_server_add_db(config->jsonrpc, db->db)) {
446 error = xasprintf("%s: duplicate database name", db->db->schema->name);
448 shash_add_assert(config->all_dbs, db->db->schema->name, db);
452 ovsdb_error_destroy(db_error);
457 static char * OVS_WARN_UNUSED_RESULT
458 parse_db_column__(const struct shash *all_dbs,
459 const char *name_, char *name,
460 const struct db **dbp,
461 const struct ovsdb_table **tablep,
462 const struct ovsdb_column **columnp)
464 const char *db_name, *table_name, *column_name;
465 const struct ovsdb_column *column;
466 const struct ovsdb_table *table;
467 const char *tokens[3];
468 char *save_ptr = NULL;
475 strtok_r(name, ":", &save_ptr); /* "db:" */
476 tokens[0] = strtok_r(NULL, ",", &save_ptr);
477 tokens[1] = strtok_r(NULL, ",", &save_ptr);
478 tokens[2] = strtok_r(NULL, ",", &save_ptr);
479 if (!tokens[0] || !tokens[1] || !tokens[2]) {
480 return xasprintf("\"%s\": invalid syntax", name_);
484 table_name = tokens[1];
485 column_name = tokens[2];
487 db = find_db(all_dbs, tokens[0]);
489 return xasprintf("\"%s\": no database named %s", name_, db_name);
492 table = ovsdb_get_table(db->db, table_name);
494 return xasprintf("\"%s\": no table named %s", name_, table_name);
497 column = ovsdb_table_schema_get_column(table->schema, column_name);
499 return xasprintf("\"%s\": table \"%s\" has no column \"%s\"",
500 name_, table_name, column_name);
509 /* Returns NULL if successful, otherwise a malloc()'d string describing the
511 static char * OVS_WARN_UNUSED_RESULT
512 parse_db_column(const struct shash *all_dbs,
514 const struct db **dbp,
515 const struct ovsdb_table **tablep,
516 const struct ovsdb_column **columnp)
518 char *name = xstrdup(name_);
519 char *retval = parse_db_column__(all_dbs, name_, name,
520 dbp, tablep, columnp);
525 /* Returns NULL if successful, otherwise a malloc()'d string describing the
527 static char * OVS_WARN_UNUSED_RESULT
528 parse_db_string_column(const struct shash *all_dbs,
530 const struct db **dbp,
531 const struct ovsdb_table **tablep,
532 const struct ovsdb_column **columnp)
536 retval = parse_db_column(all_dbs, name, dbp, tablep, columnp);
541 if ((*columnp)->type.key.type != OVSDB_TYPE_STRING
542 || (*columnp)->type.value.type != OVSDB_TYPE_VOID) {
543 return xasprintf("\"%s\": table \"%s\" column \"%s\" is "
544 "not string or set of strings",
545 name, (*tablep)->schema->name, (*columnp)->name);
552 query_db_string(const struct shash *all_dbs, const char *name,
555 if (!name || strncmp(name, "db:", 3)) {
558 const struct ovsdb_column *column;
559 const struct ovsdb_table *table;
560 const struct ovsdb_row *row;
564 retval = parse_db_string_column(all_dbs, name,
565 &db, &table, &column);
567 ds_put_format(errors, "%s\n", retval);
572 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
573 const struct ovsdb_datum *datum;
576 datum = &row->fields[column->index];
577 for (i = 0; i < datum->n; i++) {
578 if (datum->keys[i].string[0]) {
579 return datum->keys[i].string;
587 static struct ovsdb_jsonrpc_options *
588 add_remote(struct shash *remotes, const char *target)
590 struct ovsdb_jsonrpc_options *options;
592 options = shash_find_data(remotes, target);
594 options = ovsdb_jsonrpc_default_options(target);
595 shash_add(remotes, target, options);
601 static struct ovsdb_datum *
602 get_datum(struct ovsdb_row *row, const char *column_name,
603 const enum ovsdb_atomic_type key_type,
604 const enum ovsdb_atomic_type value_type,
607 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
608 const struct ovsdb_table_schema *schema = row->table->schema;
609 const struct ovsdb_column *column;
611 column = ovsdb_table_schema_get_column(schema, column_name);
613 VLOG_DBG_RL(&rl, "Table `%s' has no `%s' column",
614 schema->name, column_name);
618 if (column->type.key.type != key_type
619 || column->type.value.type != value_type
620 || column->type.n_max != n_max) {
621 if (!VLOG_DROP_DBG(&rl)) {
622 char *type_name = ovsdb_type_to_english(&column->type);
623 VLOG_DBG("Table `%s' column `%s' has type %s, not expected "
624 "key type %s, value type %s, max elements %"PRIuSIZE".",
625 schema->name, column_name, type_name,
626 ovsdb_atomic_type_to_string(key_type),
627 ovsdb_atomic_type_to_string(value_type),
634 return &row->fields[column->index];
637 /* Read string-string key-values from a map. Returns the value associated with
638 * 'key', if found, or NULL */
640 read_map_string_column(const struct ovsdb_row *row, const char *column_name,
643 const struct ovsdb_datum *datum;
644 union ovsdb_atom *atom_key = NULL, *atom_value = NULL;
647 datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name,
648 OVSDB_TYPE_STRING, OVSDB_TYPE_STRING, UINT_MAX);
654 for (i = 0; i < datum->n; i++) {
655 atom_key = &datum->keys[i];
656 if (!strcmp(atom_key->string, key)){
657 atom_value = &datum->values[i];
662 return atom_value ? atom_value->string : NULL;
665 static const union ovsdb_atom *
666 read_column(const struct ovsdb_row *row, const char *column_name,
667 enum ovsdb_atomic_type type)
669 const struct ovsdb_datum *datum;
671 datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name, type,
673 return datum && datum->n ? datum->keys : NULL;
677 read_integer_column(const struct ovsdb_row *row, const char *column_name,
678 long long int *integerp)
680 const union ovsdb_atom *atom;
682 atom = read_column(row, column_name, OVSDB_TYPE_INTEGER);
683 *integerp = atom ? atom->integer : 0;
688 read_string_column(const struct ovsdb_row *row, const char *column_name,
689 const char **stringp)
691 const union ovsdb_atom *atom;
693 atom = read_column(row, column_name, OVSDB_TYPE_STRING);
694 *stringp = atom ? atom->string : NULL;
699 write_bool_column(struct ovsdb_row *row, const char *column_name, bool value)
701 const struct ovsdb_column *column;
702 struct ovsdb_datum *datum;
704 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
705 datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
712 ovsdb_datum_destroy(datum, &column->type);
715 datum->keys = xmalloc(sizeof *datum->keys);
716 datum->values = NULL;
719 datum->keys[0].boolean = value;
723 write_string_string_column(struct ovsdb_row *row, const char *column_name,
724 char **keys, char **values, size_t n)
726 const struct ovsdb_column *column;
727 struct ovsdb_datum *datum;
730 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
731 datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
734 for (i = 0; i < n; i++) {
741 /* Free existing data. */
742 ovsdb_datum_destroy(datum, &column->type);
744 /* Allocate space for new values. */
746 datum->keys = xmalloc(n * sizeof *datum->keys);
747 datum->values = xmalloc(n * sizeof *datum->values);
749 for (i = 0; i < n; ++i) {
750 datum->keys[i].string = keys[i];
751 datum->values[i].string = values[i];
754 /* Sort and check constraints. */
755 ovsdb_datum_sort_assert(datum, column->type.key.type);
758 /* Adds a remote and options to 'remotes', based on the Manager table row in
761 add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
763 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
764 struct ovsdb_jsonrpc_options *options;
765 long long int max_backoff, probe_interval;
766 const char *target, *dscp_string;
768 if (!read_string_column(row, "target", &target) || !target) {
769 VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
770 row->table->schema->name);
774 options = add_remote(remotes, target);
775 if (read_integer_column(row, "max_backoff", &max_backoff)) {
776 options->max_backoff = max_backoff;
778 if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
779 options->probe_interval = probe_interval;
782 options->dscp = DSCP_DEFAULT;
783 dscp_string = read_map_string_column(row, "other_config", "dscp");
785 int dscp = atoi(dscp_string);
786 if (dscp >= 0 && dscp <= 63) {
787 options->dscp = dscp;
793 query_db_remotes(const char *name, const struct shash *all_dbs,
794 struct shash *remotes, struct ds *errors)
796 const struct ovsdb_column *column;
797 const struct ovsdb_table *table;
798 const struct ovsdb_row *row;
802 retval = parse_db_column(all_dbs, name, &db, &table, &column);
804 ds_put_format(errors, "%s\n", retval);
809 if (column->type.key.type == OVSDB_TYPE_STRING
810 && column->type.value.type == OVSDB_TYPE_VOID) {
811 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
812 const struct ovsdb_datum *datum;
815 datum = &row->fields[column->index];
816 for (i = 0; i < datum->n; i++) {
817 add_remote(remotes, datum->keys[i].string);
820 } else if (column->type.key.type == OVSDB_TYPE_UUID
821 && column->type.key.u.uuid.refTable
822 && column->type.value.type == OVSDB_TYPE_VOID) {
823 const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
824 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
825 const struct ovsdb_datum *datum;
828 datum = &row->fields[column->index];
829 for (i = 0; i < datum->n; i++) {
830 const struct ovsdb_row *ref_row;
832 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
834 add_manager_options(remotes, ref_row);
842 update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
843 const struct ovsdb_jsonrpc_server *jsonrpc)
845 struct ovsdb_jsonrpc_remote_status status;
846 struct ovsdb_row *rw_row;
848 char *keys[9], *values[9];
851 /* Get the "target" (protocol/host/port) spec. */
852 if (!read_string_column(row, "target", &target)) {
853 /* Bad remote spec or incorrect schema. */
856 rw_row = ovsdb_txn_row_modify(txn, row);
857 ovsdb_jsonrpc_server_get_remote_status(jsonrpc, target, &status);
859 /* Update status information columns. */
860 write_bool_column(rw_row, "is_connected", status.is_connected);
863 keys[n] = xstrdup("state");
864 values[n++] = xstrdup(status.state);
866 if (status.sec_since_connect != UINT_MAX) {
867 keys[n] = xstrdup("sec_since_connect");
868 values[n++] = xasprintf("%u", status.sec_since_connect);
870 if (status.sec_since_disconnect != UINT_MAX) {
871 keys[n] = xstrdup("sec_since_disconnect");
872 values[n++] = xasprintf("%u", status.sec_since_disconnect);
874 if (status.last_error) {
875 keys[n] = xstrdup("last_error");
877 xstrdup(ovs_retval_to_string(status.last_error));
879 if (status.locks_held && status.locks_held[0]) {
880 keys[n] = xstrdup("locks_held");
881 values[n++] = xstrdup(status.locks_held);
883 if (status.locks_waiting && status.locks_waiting[0]) {
884 keys[n] = xstrdup("locks_waiting");
885 values[n++] = xstrdup(status.locks_waiting);
887 if (status.locks_lost && status.locks_lost[0]) {
888 keys[n] = xstrdup("locks_lost");
889 values[n++] = xstrdup(status.locks_lost);
891 if (status.n_connections > 1) {
892 keys[n] = xstrdup("n_connections");
893 values[n++] = xasprintf("%d", status.n_connections);
895 if (status.bound_port != htons(0)) {
896 keys[n] = xstrdup("bound_port");
897 values[n++] = xasprintf("%"PRIu16, ntohs(status.bound_port));
899 write_string_string_column(rw_row, "status", keys, values, n);
901 ovsdb_jsonrpc_server_free_remote_status(&status);
905 update_remote_rows(const struct shash *all_dbs,
906 const char *remote_name,
907 const struct ovsdb_jsonrpc_server *jsonrpc)
909 const struct ovsdb_table *table, *ref_table;
910 const struct ovsdb_column *column;
911 const struct ovsdb_row *row;
915 if (strncmp("db:", remote_name, 3)) {
919 retval = parse_db_column(all_dbs, remote_name, &db, &table, &column);
925 if (column->type.key.type != OVSDB_TYPE_UUID
926 || !column->type.key.u.uuid.refTable
927 || column->type.value.type != OVSDB_TYPE_VOID) {
931 ref_table = column->type.key.u.uuid.refTable;
933 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
934 const struct ovsdb_datum *datum;
937 datum = &row->fields[column->index];
938 for (i = 0; i < datum->n; i++) {
939 const struct ovsdb_row *ref_row;
941 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
943 update_remote_row(ref_row, db->txn, jsonrpc);
950 update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
951 const struct sset *remotes,
952 struct shash *all_dbs)
954 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
957 struct shash_node *node;
959 SHASH_FOR_EACH(node, all_dbs) {
961 db->txn = ovsdb_txn_create(db->db);
964 /* Iterate over --remote arguments given on command line. */
965 SSET_FOR_EACH (remote, remotes) {
966 update_remote_rows(all_dbs, remote, jsonrpc);
969 SHASH_FOR_EACH(node, all_dbs) {
970 struct ovsdb_error *error;
972 error = ovsdb_txn_commit(db->txn, false);
974 VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
975 ovsdb_error_to_string(error));
976 ovsdb_error_destroy(error);
981 /* Reconfigures ovsdb-server's remotes based on information in the database. */
983 reconfigure_remotes(struct ovsdb_jsonrpc_server *jsonrpc,
984 const struct shash *all_dbs, struct sset *remotes)
986 struct ds errors = DS_EMPTY_INITIALIZER;
987 struct shash resolved_remotes;
990 /* Configure remotes. */
991 shash_init(&resolved_remotes);
992 SSET_FOR_EACH (name, remotes) {
993 if (!strncmp(name, "db:", 3)) {
994 query_db_remotes(name, all_dbs, &resolved_remotes, &errors);
996 add_remote(&resolved_remotes, name);
999 ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
1000 shash_destroy_free_data(&resolved_remotes);
1002 return errors.string;
1006 reconfigure_ssl(const struct shash *all_dbs)
1008 struct ds errors = DS_EMPTY_INITIALIZER;
1009 const char *resolved_private_key;
1010 const char *resolved_certificate;
1011 const char *resolved_ca_cert;
1013 resolved_private_key = query_db_string(all_dbs, private_key_file, &errors);
1014 resolved_certificate = query_db_string(all_dbs, certificate_file, &errors);
1015 resolved_ca_cert = query_db_string(all_dbs, ca_cert_file, &errors);
1017 stream_ssl_set_key_and_cert(resolved_private_key, resolved_certificate);
1018 stream_ssl_set_ca_cert_file(resolved_ca_cert, bootstrap_ca_cert);
1020 return errors.string;
1024 report_error_if_changed(char *error, char **last_errorp)
1027 if (!*last_errorp || strcmp(error, *last_errorp)) {
1028 VLOG_WARN("%s", error);
1030 *last_errorp = error;
1036 *last_errorp = NULL;
1041 ovsdb_server_set_remote_ovsdb_server(struct unixctl_conn *conn,
1042 int argc OVS_UNUSED, const char *argv[],
1043 void *arg_ OVS_UNUSED)
1045 set_remote_ovsdb_server(argv[1]);
1046 connect_to_remote_server = false;
1047 unixctl_command_reply(conn, NULL);
1051 ovsdb_server_get_remote_ovsdb_server(struct unixctl_conn *conn,
1052 int argc OVS_UNUSED,
1053 const char *argv[] OVS_UNUSED,
1054 void *arg_ OVS_UNUSED)
1059 ds_put_format(&s, "%s\n", get_remote_ovsdb_server());
1061 unixctl_command_reply(conn, ds_cstr(&s));
1066 ovsdb_server_connect_remote_ovsdb_server(struct unixctl_conn *conn,
1067 int argc OVS_UNUSED,
1068 const char *argv[] OVS_UNUSED,
1069 void *arg_ OVS_UNUSED)
1071 if (!connect_to_remote_server) {
1073 connect_to_remote_server = true;
1075 unixctl_command_reply(conn, NULL);
1079 ovsdb_server_disconnect_remote_ovsdb_server(struct unixctl_conn *conn,
1080 int argc OVS_UNUSED,
1081 const char *argv[] OVS_UNUSED,
1082 void *arg_ OVS_UNUSED)
1084 disconnect_remote_server();
1085 connect_to_remote_server = false;
1086 unixctl_command_reply(conn, NULL);
1090 ovsdb_server_set_sync_excluded_tables(struct unixctl_conn *conn,
1091 int argc OVS_UNUSED,
1093 void *arg_ OVS_UNUSED)
1095 set_tables_blacklist(argv[1]);
1096 unixctl_command_reply(conn, NULL);
1100 ovsdb_server_get_sync_excluded_tables(struct unixctl_conn *conn,
1101 int argc OVS_UNUSED,
1102 const char *argv[] OVS_UNUSED,
1103 void *arg_ OVS_UNUSED)
1106 const char *table_name;
1107 struct sset table_blacklist = get_tables_blacklist();
1111 SSET_FOR_EACH(table_name, &table_blacklist) {
1112 ds_put_format(&s, "%s\n", table_name);
1115 unixctl_command_reply(conn, ds_cstr(&s));
1119 ovsdb_server_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
1120 const char *argv[] OVS_UNUSED,
1123 bool *exiting = exiting_;
1125 unixctl_command_reply(conn, NULL);
1129 ovsdb_server_perf_counters_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
1130 const char *argv[] OVS_UNUSED,
1131 void *arg_ OVS_UNUSED)
1133 char *s = perf_counters_to_string();
1135 unixctl_command_reply(conn, s);
1140 ovsdb_server_perf_counters_clear(struct unixctl_conn *conn, int argc OVS_UNUSED,
1141 const char *argv[] OVS_UNUSED,
1142 void *arg_ OVS_UNUSED)
1144 perf_counters_clear();
1145 unixctl_command_reply(conn, NULL);
1148 /* "ovsdb-server/disable-monitor-cond": makes ovsdb-server drop all of its
1149 * JSON-RPC connections and reconnect. New sessions will not recognize
1150 * the 'monitor_cond' method. */
1152 ovsdb_server_disable_monitor_cond(struct unixctl_conn *conn,
1153 int argc OVS_UNUSED,
1154 const char *argv[] OVS_UNUSED,
1157 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
1159 ovsdb_jsonrpc_disable_monitor_cond();
1160 ovsdb_jsonrpc_server_reconnect(jsonrpc);
1161 unixctl_command_reply(conn, NULL);
1165 ovsdb_server_compact(struct unixctl_conn *conn, int argc,
1166 const char *argv[], void *dbs_)
1168 struct shash *all_dbs = dbs_;
1171 struct shash_node *node;
1175 SHASH_FOR_EACH(node, all_dbs) {
1179 name = db->db->schema->name;
1181 if (argc < 2 || !strcmp(argv[1], name)) {
1182 struct ovsdb_error *error;
1184 VLOG_INFO("compacting %s database by user request", name);
1186 error = ovsdb_file_compact(db->file);
1188 char *s = ovsdb_error_to_string(error);
1189 ds_put_format(&reply, "%s\n", s);
1191 ovsdb_error_destroy(error);
1199 unixctl_command_reply_error(conn, "no database by that name");
1200 } else if (reply.length) {
1201 unixctl_command_reply_error(conn, ds_cstr(&reply));
1203 unixctl_command_reply(conn, NULL);
1208 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
1209 * connections and reconnect. */
1211 ovsdb_server_reconnect(struct unixctl_conn *conn, int argc OVS_UNUSED,
1212 const char *argv[] OVS_UNUSED, void *jsonrpc_)
1214 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
1216 ovsdb_jsonrpc_server_reconnect(jsonrpc);
1217 unixctl_command_reply(conn, NULL);
1220 /* "ovsdb-server/add-remote REMOTE": adds REMOTE to the set of remotes that
1221 * ovsdb-server services. */
1223 ovsdb_server_add_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
1224 const char *argv[], void *config_)
1226 struct server_config *config = config_;
1227 const char *remote = argv[1];
1229 const struct ovsdb_column *column;
1230 const struct ovsdb_table *table;
1231 const struct db *db;
1234 retval = (strncmp("db:", remote, 3)
1236 : parse_db_column(config->all_dbs, remote,
1237 &db, &table, &column));
1239 if (sset_add(config->remotes, remote)) {
1240 save_config(config);
1242 unixctl_command_reply(conn, NULL);
1244 unixctl_command_reply_error(conn, retval);
1249 /* "ovsdb-server/remove-remote REMOTE": removes REMOTE frmo the set of remotes
1250 * that ovsdb-server services. */
1252 ovsdb_server_remove_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
1253 const char *argv[], void *config_)
1255 struct server_config *config = config_;
1256 struct sset_node *node;
1258 node = sset_find(config->remotes, argv[1]);
1260 sset_delete(config->remotes, node);
1261 save_config(config);
1262 unixctl_command_reply(conn, NULL);
1264 unixctl_command_reply_error(conn, "no such remote");
1268 /* "ovsdb-server/list-remotes": outputs a list of configured rmeotes. */
1270 ovsdb_server_list_remotes(struct unixctl_conn *conn, int argc OVS_UNUSED,
1271 const char *argv[] OVS_UNUSED, void *remotes_)
1273 struct sset *remotes = remotes_;
1274 const char **list, **p;
1279 list = sset_sort(remotes);
1280 for (p = list; *p; p++) {
1281 ds_put_format(&s, "%s\n", *p);
1285 unixctl_command_reply(conn, ds_cstr(&s));
1290 /* "ovsdb-server/add-db DB": adds the DB to ovsdb-server. */
1292 ovsdb_server_add_database(struct unixctl_conn *conn, int argc OVS_UNUSED,
1293 const char *argv[], void *config_)
1295 struct server_config *config = config_;
1296 const char *filename = argv[1];
1299 error = open_db(config, filename);
1301 save_config(config);
1302 unixctl_command_reply(conn, NULL);
1304 unixctl_command_reply_error(conn, error);
1310 ovsdb_server_remove_database(struct unixctl_conn *conn, int argc OVS_UNUSED,
1311 const char *argv[], void *config_)
1313 struct server_config *config = config_;
1314 struct shash_node *node;
1318 node = shash_find(config->all_dbs, argv[1]);
1320 unixctl_command_reply_error(conn, "Failed to find the database.");
1325 ok = ovsdb_jsonrpc_server_remove_db(config->jsonrpc, db->db);
1329 shash_delete(config->all_dbs, node);
1331 save_config(config);
1332 unixctl_command_reply(conn, NULL);
1336 ovsdb_server_list_databases(struct unixctl_conn *conn, int argc OVS_UNUSED,
1337 const char *argv[] OVS_UNUSED, void *all_dbs_)
1339 struct shash *all_dbs = all_dbs_;
1340 const struct shash_node **nodes;
1346 nodes = shash_sort(all_dbs);
1347 for (i = 0; i < shash_count(all_dbs); i++) {
1348 struct db *db = nodes[i]->data;
1349 ds_put_format(&s, "%s\n", db->db->schema->name);
1353 unixctl_command_reply(conn, ds_cstr(&s));
1358 parse_options(int *argcp, char **argvp[],
1359 struct sset *remotes, char **unixctl_pathp, char **run_command)
1362 OPT_REMOTE = UCHAR_MAX + 1,
1365 OPT_BOOTSTRAP_CA_CERT,
1372 static const struct option long_options[] = {
1373 {"remote", required_argument, NULL, OPT_REMOTE},
1374 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
1376 {"run", required_argument, NULL, OPT_RUN},
1378 {"help", no_argument, NULL, 'h'},
1379 {"version", no_argument, NULL, 'V'},
1380 DAEMON_LONG_OPTIONS,
1382 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
1383 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
1384 {"private-key", required_argument, NULL, 'p'},
1385 {"certificate", required_argument, NULL, 'c'},
1386 {"ca-cert", required_argument, NULL, 'C'},
1387 {"sync-from", required_argument, NULL, OPT_SYNC_FROM},
1388 {"sync-exclude-tables", required_argument, NULL, OPT_SYNC_EXCLUDE},
1391 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
1393 char **argv = *argvp;
1399 c = getopt_long(argc, argv, short_options, long_options, NULL);
1406 sset_add(remotes, optarg);
1410 *unixctl_pathp = optarg;
1414 *run_command = optarg;
1421 ovs_print_version(0, 0);
1424 VLOG_OPTION_HANDLERS
1425 DAEMON_OPTION_HANDLERS
1428 private_key_file = optarg;
1432 certificate_file = optarg;
1436 ca_cert_file = optarg;
1437 bootstrap_ca_cert = false;
1440 case OPT_BOOTSTRAP_CA_CERT:
1441 ca_cert_file = optarg;
1442 bootstrap_ca_cert = true;
1445 case OPT_PEER_CA_CERT:
1446 stream_ssl_set_peer_ca_cert_file(optarg);
1450 set_remote_ovsdb_server(optarg);
1451 connect_to_remote_server = true;
1454 case OPT_SYNC_EXCLUDE:
1455 set_tables_blacklist(optarg);
1465 free(short_options);
1474 printf("%s: Open vSwitch database server\n"
1475 "usage: %s [OPTIONS] [DATABASE...]\n"
1476 "where each DATABASE is a database file in ovsdb format.\n"
1477 "The default DATABASE, if none is given, is\n%s/conf.db.\n",
1478 program_name, program_name, ovs_dbdir());
1479 printf("\nJSON-RPC options (may be specified any number of times):\n"
1480 " --remote=REMOTE connect or listen to REMOTE\n");
1481 stream_usage("JSON-RPC", true, true, true);
1484 replication_usage();
1485 printf("\nOther options:\n"
1486 " --run COMMAND run COMMAND as subprocess then exit\n"
1487 " --unixctl=SOCKET override default control socket name\n"
1488 " -h, --help display this help message\n"
1489 " -V, --version display version information\n");
1493 static struct json *
1494 sset_to_json(const struct sset *sset)
1499 array = json_array_create_empty();
1500 SSET_FOR_EACH (s, sset) {
1501 json_array_add(array, json_string_create(s));
1506 /* Truncates and replaces the contents of 'config_file' by a representation of
1507 * 'remotes' and 'db_filenames'. */
1509 save_config__(FILE *config_file, const struct sset *remotes,
1510 const struct sset *db_filenames)
1515 if (ftruncate(fileno(config_file), 0) == -1) {
1516 VLOG_FATAL("failed to truncate temporary file (%s)",
1517 ovs_strerror(errno));
1520 obj = json_object_create();
1521 json_object_put(obj, "remotes", sset_to_json(remotes));
1522 json_object_put(obj, "db_filenames", sset_to_json(db_filenames));
1523 s = json_to_string(obj, 0);
1526 if (fseek(config_file, 0, SEEK_SET) != 0
1527 || fputs(s, config_file) == EOF
1528 || fflush(config_file) == EOF) {
1529 VLOG_FATAL("failed to write temporary file (%s)", ovs_strerror(errno));
1534 /* Truncates and replaces the contents of 'config_file' by a representation of
1537 save_config(struct server_config *config)
1539 struct sset db_filenames;
1540 struct shash_node *node;
1542 sset_init(&db_filenames);
1543 SHASH_FOR_EACH (node, config->all_dbs) {
1544 struct db *db = node->data;
1545 sset_add(&db_filenames, db->filename);
1548 save_config__(config->config_tmpfile, config->remotes, &db_filenames);
1550 sset_destroy(&db_filenames);
1554 sset_from_json(struct sset *sset, const struct json *array)
1560 ovs_assert(array->type == JSON_ARRAY);
1561 for (i = 0; i < array->u.array.n; i++) {
1562 const struct json *elem = array->u.array.elems[i];
1563 sset_add(sset, json_string(elem));
1567 /* Clears and replaces 'remotes' and 'dbnames' by a configuration read from
1568 * 'config_file', which must have been previously written by save_config(). */
1570 load_config(FILE *config_file, struct sset *remotes, struct sset *db_filenames)
1574 if (fseek(config_file, 0, SEEK_SET) != 0) {
1575 VLOG_FATAL("seek failed in temporary file (%s)", ovs_strerror(errno));
1577 json = json_from_stream(config_file);
1578 if (json->type == JSON_STRING) {
1579 VLOG_FATAL("reading json failed (%s)", json_string(json));
1581 ovs_assert(json->type == JSON_OBJECT);
1583 sset_from_json(remotes, shash_find_data(json_object(json), "remotes"));
1584 sset_from_json(db_filenames,
1585 shash_find_data(json_object(json), "db_filenames"));