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"
45 #include "stream-ssl.h"
51 #include "transaction.h"
57 VLOG_DEFINE_THIS_MODULE(ovsdb_server);
60 /* Initialized in main(). */
62 struct ovsdb_file *file;
65 /* Only used by update_remote_status(). */
66 struct ovsdb_txn *txn;
69 /* SSL configuration. */
70 static char *private_key_file;
71 static char *certificate_file;
72 static char *ca_cert_file;
73 static bool bootstrap_ca_cert;
75 static unixctl_cb_func ovsdb_server_exit;
76 static unixctl_cb_func ovsdb_server_compact;
77 static unixctl_cb_func ovsdb_server_reconnect;
79 struct add_remote_aux {
85 static unixctl_cb_func ovsdb_server_add_remote;
87 struct remove_remote_aux {
91 static unixctl_cb_func ovsdb_server_remove_remote;
92 static unixctl_cb_func ovsdb_server_list_remotes;
94 static void parse_options(int *argc, char **argvp[],
95 struct sset *remotes, char **unixctl_pathp,
97 static void usage(void) NO_RETURN;
99 static void reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
100 const struct db dbs[], size_t n_dbs,
101 struct sset *remotes);
103 static void update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
104 const struct sset *remotes,
105 struct db dbs[], size_t n_dbs);
107 static void save_config(FILE *config_file, const struct sset *);
108 static void load_config(FILE *config_file, struct sset *);
111 main(int argc, char *argv[])
113 char *unixctl_path = NULL;
114 char *run_command = NULL;
115 struct unixctl_server *unixctl;
116 struct ovsdb_jsonrpc_server *jsonrpc;
118 struct process *run_process;
121 long long int status_timer = LLONG_MIN;
122 struct add_remote_aux add_remote_aux;
123 struct remove_remote_aux remove_remote_aux;
124 FILE *config_tmpfile;
130 proctitle_init(argc, argv);
131 set_program_name(argv[0]);
132 stress_init_command();
133 signal(SIGPIPE, SIG_IGN);
136 parse_options(&argc, &argv, &remotes, &unixctl_path, &run_command);
138 /* Create and initialize 'config_tmpfile' as a temporary file to hold
139 * ovsdb-server's most basic configuration, and then save our initial
140 * configuration to it. When --monitor is used, this preserves the effects
141 * of ovs-appctl commands such as ovsdb-server/add-remote (which saves the
142 * new configuration) across crashes. */
143 config_tmpfile = tmpfile();
144 if (!config_tmpfile) {
145 ovs_fatal(errno, "failed to create temporary file");
147 save_config(config_tmpfile, &remotes);
151 /* Load the saved config. */
152 load_config(config_tmpfile, &remotes);
154 n_dbs = MAX(1, argc);
155 dbs = xcalloc(n_dbs + 1, sizeof *dbs);
157 for (i = 0; i < argc; i++) {
158 dbs[i].filename = argv[i];
161 dbs[0].filename = xasprintf("%s/conf.db", ovs_dbdir());
164 for (i = 0; i < n_dbs; i++) {
165 struct ovsdb_error *error;
167 error = ovsdb_file_open(dbs[i].filename, false,
168 &dbs[i].db, &dbs[i].file);
170 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
174 jsonrpc = ovsdb_jsonrpc_server_create();
175 for (i = 0; i < n_dbs; i++) {
176 if (!ovsdb_jsonrpc_server_add_db(jsonrpc, dbs[i].db)) {
177 ovs_fatal(0, "%s: duplicate database name",
178 dbs[i].db->schema->name);
181 reconfigure_from_db(jsonrpc, dbs, n_dbs, &remotes);
183 retval = unixctl_server_create(unixctl_path, &unixctl);
191 run_argv[0] = "/bin/sh";
193 run_argv[2] = run_command;
196 retval = process_start(run_argv, &run_process);
198 ovs_fatal(retval, "%s: process failed to start", run_command);
204 daemonize_complete();
207 /* ovsdb-server is usually a long-running process, in which case it
208 * makes plenty of sense to log the version, but --run makes
209 * ovsdb-server more like a command-line tool, so skip it. */
210 VLOG_INFO("%s (Open vSwitch) %s", program_name, VERSION);
213 unixctl_command_register("exit", "", 0, 0, ovsdb_server_exit, &exiting);
214 unixctl_command_register("ovsdb-server/compact", "", 0, 1,
215 ovsdb_server_compact, dbs);
216 unixctl_command_register("ovsdb-server/reconnect", "", 0, 0,
217 ovsdb_server_reconnect, jsonrpc);
219 add_remote_aux.remotes = &remotes;
220 add_remote_aux.dbs = dbs;
221 add_remote_aux.n_dbs = n_dbs;
222 add_remote_aux.config_tmpfile = config_tmpfile;
223 unixctl_command_register("ovsdb-server/add-remote", "REMOTE", 1, 1,
224 ovsdb_server_add_remote, &add_remote_aux);
226 remove_remote_aux.remotes = &remotes;
227 remove_remote_aux.config_tmpfile = config_tmpfile;
228 unixctl_command_register("ovsdb-server/remove-remote", "REMOTE", 1, 1,
229 ovsdb_server_remove_remote, &remove_remote_aux);
231 unixctl_command_register("ovsdb-server/list-remotes", "", 0, 0,
232 ovsdb_server_list_remotes, &remotes);
239 if (memory_should_report()) {
243 ovsdb_jsonrpc_server_get_memory_usage(jsonrpc, &usage);
244 for (i = 0; i < n_dbs; i++) {
245 ovsdb_get_memory_usage(dbs[i].db, &usage);
247 memory_report(&usage);
248 simap_destroy(&usage);
251 /* Run unixctl_server_run() before reconfigure_from_db() because
252 * ovsdb-server/add-remote and ovsdb-server/remove-remote can change
253 * the set of remotes that reconfigure_from_db() uses. */
254 unixctl_server_run(unixctl);
256 reconfigure_from_db(jsonrpc, dbs, n_dbs, &remotes);
257 ovsdb_jsonrpc_server_run(jsonrpc);
259 for (i = 0; i < n_dbs; i++) {
260 ovsdb_trigger_run(dbs[i].db, time_msec());
264 if (process_exited(run_process)) {
269 /* update Manager status(es) every 5 seconds */
270 if (time_msec() >= status_timer) {
271 status_timer = time_msec() + 5000;
272 update_remote_status(jsonrpc, &remotes, dbs, n_dbs);
276 ovsdb_jsonrpc_server_wait(jsonrpc);
277 unixctl_server_wait(unixctl);
278 for (i = 0; i < n_dbs; i++) {
279 ovsdb_trigger_wait(dbs[i].db, time_msec());
282 process_wait(run_process);
285 poll_immediate_wake();
287 poll_timer_wait_until(status_timer);
290 ovsdb_jsonrpc_server_destroy(jsonrpc);
291 for (i = 0; i < n_dbs; i++) {
292 ovsdb_destroy(dbs[i].db);
294 sset_destroy(&remotes);
295 unixctl_server_destroy(unixctl);
297 if (run_process && process_exited(run_process)) {
298 int status = process_status(run_process);
300 ovs_fatal(0, "%s: child exited, %s",
301 run_command, process_status_msg(status));
308 static const struct db *
309 find_db(const struct db dbs[], size_t n_dbs, const char *db_name)
313 for (i = 0; i < n_dbs; i++) {
314 if (!strcmp(dbs[i].db->schema->name, db_name)) {
322 static char * WARN_UNUSED_RESULT
323 parse_db_column__(const struct db dbs[], size_t n_dbs,
324 const char *name_, char *name,
325 const struct db **dbp,
326 const struct ovsdb_table **tablep,
327 const struct ovsdb_column **columnp)
329 const char *table_name, *column_name;
330 const struct ovsdb_column *column;
331 const struct ovsdb_table *table;
332 const char *tokens[3];
333 char *save_ptr = NULL;
340 strtok_r(name, ":", &save_ptr); /* "db:" */
341 tokens[0] = strtok_r(NULL, ",", &save_ptr);
342 tokens[1] = strtok_r(NULL, ",", &save_ptr);
343 tokens[2] = strtok_r(NULL, ",", &save_ptr);
344 if (!tokens[0] || !tokens[1]) {
345 return xasprintf("\"%s\": invalid syntax", name_);
348 const char *db_name = tokens[0];
349 table_name = tokens[1];
350 column_name = tokens[2];
352 db = find_db(dbs, n_dbs, tokens[0]);
354 return xasprintf("\"%s\": no database named %s", name_, db_name);
358 return xasprintf("\"%s\": database name must be specified "
359 "(because multiple databases are configured)",
363 table_name = tokens[0];
364 column_name = tokens[1];
368 table = ovsdb_get_table(db->db, table_name);
370 return xasprintf("\"%s\": no table named %s", name_, table_name);
373 column = ovsdb_table_schema_get_column(table->schema, column_name);
375 return xasprintf("\"%s\": table \"%s\" has no column \"%s\"",
376 name_, table_name, column_name);
385 /* Returns NULL if successful, otherwise a malloc()'d string describing the
387 static char * WARN_UNUSED_RESULT
388 parse_db_column(const struct db dbs[], size_t n_dbs,
390 const struct db **dbp,
391 const struct ovsdb_table **tablep,
392 const struct ovsdb_column **columnp)
394 char *name = xstrdup(name_);
395 char *retval = parse_db_column__(dbs, n_dbs, name_, name,
396 dbp, tablep, columnp);
401 /* Returns NULL if successful, otherwise a malloc()'d string describing the
403 static char * WARN_UNUSED_RESULT
404 parse_db_string_column(const struct db dbs[], size_t n_dbs,
406 const struct db **dbp,
407 const struct ovsdb_table **tablep,
408 const struct ovsdb_column **columnp)
412 retval = parse_db_column(dbs, n_dbs, name, dbp, tablep, columnp);
417 if ((*columnp)->type.key.type != OVSDB_TYPE_STRING
418 || (*columnp)->type.value.type != OVSDB_TYPE_VOID) {
419 return xasprintf("\"%s\": table \"%s\" column \"%s\" is "
420 "not string or set of strings",
421 name, (*tablep)->schema->name, (*columnp)->name);
427 static OVS_UNUSED const char *
428 query_db_string(const struct db dbs[], size_t n_dbs, const char *name)
430 if (!name || strncmp(name, "db:", 3)) {
433 const struct ovsdb_column *column;
434 const struct ovsdb_table *table;
435 const struct ovsdb_row *row;
439 retval = parse_db_string_column(dbs, n_dbs, name,
440 &db, &table, &column);
442 ovs_fatal(0, "%s", retval);
445 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
446 const struct ovsdb_datum *datum;
449 datum = &row->fields[column->index];
450 for (i = 0; i < datum->n; i++) {
451 if (datum->keys[i].string[0]) {
452 return datum->keys[i].string;
460 static struct ovsdb_jsonrpc_options *
461 add_remote(struct shash *remotes, const char *target)
463 struct ovsdb_jsonrpc_options *options;
465 options = shash_find_data(remotes, target);
467 options = ovsdb_jsonrpc_default_options(target);
468 shash_add(remotes, target, options);
474 static struct ovsdb_datum *
475 get_datum(struct ovsdb_row *row, const char *column_name,
476 const enum ovsdb_atomic_type key_type,
477 const enum ovsdb_atomic_type value_type,
480 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
481 const struct ovsdb_table_schema *schema = row->table->schema;
482 const struct ovsdb_column *column;
484 column = ovsdb_table_schema_get_column(schema, column_name);
486 VLOG_DBG_RL(&rl, "Table `%s' has no `%s' column",
487 schema->name, column_name);
491 if (column->type.key.type != key_type
492 || column->type.value.type != value_type
493 || column->type.n_max != n_max) {
494 if (!VLOG_DROP_DBG(&rl)) {
495 char *type_name = ovsdb_type_to_english(&column->type);
496 VLOG_DBG("Table `%s' column `%s' has type %s, not expected "
497 "key type %s, value type %s, max elements %zd.",
498 schema->name, column_name, type_name,
499 ovsdb_atomic_type_to_string(key_type),
500 ovsdb_atomic_type_to_string(value_type),
507 return &row->fields[column->index];
510 /* Read string-string key-values from a map. Returns the value associated with
511 * 'key', if found, or NULL */
513 read_map_string_column(const struct ovsdb_row *row, const char *column_name,
516 const struct ovsdb_datum *datum;
517 union ovsdb_atom *atom_key = NULL, *atom_value = NULL;
520 datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name,
521 OVSDB_TYPE_STRING, OVSDB_TYPE_STRING, UINT_MAX);
527 for (i = 0; i < datum->n; i++) {
528 atom_key = &datum->keys[i];
529 if (!strcmp(atom_key->string, key)){
530 atom_value = &datum->values[i];
535 return atom_value ? atom_value->string : NULL;
538 static const union ovsdb_atom *
539 read_column(const struct ovsdb_row *row, const char *column_name,
540 enum ovsdb_atomic_type type)
542 const struct ovsdb_datum *datum;
544 datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name, type,
546 return datum && datum->n ? datum->keys : NULL;
550 read_integer_column(const struct ovsdb_row *row, const char *column_name,
551 long long int *integerp)
553 const union ovsdb_atom *atom;
555 atom = read_column(row, column_name, OVSDB_TYPE_INTEGER);
556 *integerp = atom ? atom->integer : 0;
561 read_string_column(const struct ovsdb_row *row, const char *column_name,
562 const char **stringp)
564 const union ovsdb_atom *atom;
566 atom = read_column(row, column_name, OVSDB_TYPE_STRING);
567 *stringp = atom ? atom->string : NULL;
572 write_bool_column(struct ovsdb_row *row, const char *column_name, bool value)
574 const struct ovsdb_column *column;
575 struct ovsdb_datum *datum;
577 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
578 datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
585 ovsdb_datum_destroy(datum, &column->type);
588 datum->keys = xmalloc(sizeof *datum->keys);
589 datum->values = NULL;
592 datum->keys[0].boolean = value;
596 write_string_string_column(struct ovsdb_row *row, const char *column_name,
597 char **keys, char **values, size_t n)
599 const struct ovsdb_column *column;
600 struct ovsdb_datum *datum;
603 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
604 datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
607 for (i = 0; i < n; i++) {
614 /* Free existing data. */
615 ovsdb_datum_destroy(datum, &column->type);
617 /* Allocate space for new values. */
619 datum->keys = xmalloc(n * sizeof *datum->keys);
620 datum->values = xmalloc(n * sizeof *datum->values);
622 for (i = 0; i < n; ++i) {
623 datum->keys[i].string = keys[i];
624 datum->values[i].string = values[i];
627 /* Sort and check constraints. */
628 ovsdb_datum_sort_assert(datum, column->type.key.type);
631 /* Adds a remote and options to 'remotes', based on the Manager table row in
634 add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
636 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
637 struct ovsdb_jsonrpc_options *options;
638 long long int max_backoff, probe_interval;
639 const char *target, *dscp_string;
641 if (!read_string_column(row, "target", &target) || !target) {
642 VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
643 row->table->schema->name);
647 options = add_remote(remotes, target);
648 if (read_integer_column(row, "max_backoff", &max_backoff)) {
649 options->max_backoff = max_backoff;
651 if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
652 options->probe_interval = probe_interval;
655 options->dscp = DSCP_DEFAULT;
656 dscp_string = read_map_string_column(row, "other_config", "dscp");
658 int dscp = atoi(dscp_string);
659 if (dscp >= 0 && dscp <= 63) {
660 options->dscp = dscp;
666 query_db_remotes(const char *name, const struct db dbs[], size_t n_dbs,
667 struct shash *remotes)
669 const struct ovsdb_column *column;
670 const struct ovsdb_table *table;
671 const struct ovsdb_row *row;
675 retval = parse_db_column(dbs, n_dbs, name, &db, &table, &column);
677 ovs_fatal(0, "%s", retval);
680 if (column->type.key.type == OVSDB_TYPE_STRING
681 && column->type.value.type == OVSDB_TYPE_VOID) {
682 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
683 const struct ovsdb_datum *datum;
686 datum = &row->fields[column->index];
687 for (i = 0; i < datum->n; i++) {
688 add_remote(remotes, datum->keys[i].string);
691 } else if (column->type.key.type == OVSDB_TYPE_UUID
692 && column->type.key.u.uuid.refTable
693 && column->type.value.type == OVSDB_TYPE_VOID) {
694 const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
695 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
696 const struct ovsdb_datum *datum;
699 datum = &row->fields[column->index];
700 for (i = 0; i < datum->n; i++) {
701 const struct ovsdb_row *ref_row;
703 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
705 add_manager_options(remotes, ref_row);
713 update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
714 const struct ovsdb_jsonrpc_server *jsonrpc)
716 struct ovsdb_jsonrpc_remote_status status;
717 struct ovsdb_row *rw_row;
719 char *keys[9], *values[9];
722 /* Get the "target" (protocol/host/port) spec. */
723 if (!read_string_column(row, "target", &target)) {
724 /* Bad remote spec or incorrect schema. */
727 rw_row = ovsdb_txn_row_modify(txn, row);
728 ovsdb_jsonrpc_server_get_remote_status(jsonrpc, target, &status);
730 /* Update status information columns. */
731 write_bool_column(rw_row, "is_connected", status.is_connected);
734 keys[n] = xstrdup("state");
735 values[n++] = xstrdup(status.state);
737 if (status.sec_since_connect != UINT_MAX) {
738 keys[n] = xstrdup("sec_since_connect");
739 values[n++] = xasprintf("%u", status.sec_since_connect);
741 if (status.sec_since_disconnect != UINT_MAX) {
742 keys[n] = xstrdup("sec_since_disconnect");
743 values[n++] = xasprintf("%u", status.sec_since_disconnect);
745 if (status.last_error) {
746 keys[n] = xstrdup("last_error");
748 xstrdup(ovs_retval_to_string(status.last_error));
750 if (status.locks_held && status.locks_held[0]) {
751 keys[n] = xstrdup("locks_held");
752 values[n++] = xstrdup(status.locks_held);
754 if (status.locks_waiting && status.locks_waiting[0]) {
755 keys[n] = xstrdup("locks_waiting");
756 values[n++] = xstrdup(status.locks_waiting);
758 if (status.locks_lost && status.locks_lost[0]) {
759 keys[n] = xstrdup("locks_lost");
760 values[n++] = xstrdup(status.locks_lost);
762 if (status.n_connections > 1) {
763 keys[n] = xstrdup("n_connections");
764 values[n++] = xasprintf("%d", status.n_connections);
766 if (status.bound_port != htons(0)) {
767 keys[n] = xstrdup("bound_port");
768 values[n++] = xasprintf("%"PRIu16, ntohs(status.bound_port));
770 write_string_string_column(rw_row, "status", keys, values, n);
772 ovsdb_jsonrpc_server_free_remote_status(&status);
776 update_remote_rows(const struct db dbs[], size_t n_dbs,
777 const char *remote_name,
778 const struct ovsdb_jsonrpc_server *jsonrpc)
780 const struct ovsdb_table *table, *ref_table;
781 const struct ovsdb_column *column;
782 const struct ovsdb_row *row;
786 if (strncmp("db:", remote_name, 3)) {
790 retval = parse_db_column(dbs, n_dbs, remote_name, &db, &table, &column);
792 ovs_fatal(0, "%s", retval);
795 if (column->type.key.type != OVSDB_TYPE_UUID
796 || !column->type.key.u.uuid.refTable
797 || column->type.value.type != OVSDB_TYPE_VOID) {
801 ref_table = column->type.key.u.uuid.refTable;
803 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
804 const struct ovsdb_datum *datum;
807 datum = &row->fields[column->index];
808 for (i = 0; i < datum->n; i++) {
809 const struct ovsdb_row *ref_row;
811 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
813 update_remote_row(ref_row, db->txn, jsonrpc);
820 update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
821 const struct sset *remotes,
822 struct db dbs[], size_t n_dbs)
824 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
828 for (i = 0; i < n_dbs; i++) {
829 dbs[i].txn = ovsdb_txn_create(dbs[i].db);
832 /* Iterate over --remote arguments given on command line. */
833 SSET_FOR_EACH (remote, remotes) {
834 update_remote_rows(dbs, n_dbs, remote, jsonrpc);
837 for (i = 0; i < n_dbs; i++) {
838 struct ovsdb_error *error = ovsdb_txn_commit(dbs[i].txn, false);
840 VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
841 ovsdb_error_to_string(error));
842 ovsdb_error_destroy(error);
847 /* Reconfigures ovsdb-server based on information in the database. */
849 reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
850 const struct db dbs[], size_t n_dbs, struct sset *remotes)
852 struct shash resolved_remotes;
855 /* Configure remotes. */
856 shash_init(&resolved_remotes);
857 SSET_FOR_EACH (name, remotes) {
858 if (!strncmp(name, "db:", 3)) {
859 query_db_remotes(name, dbs, n_dbs, &resolved_remotes);
861 add_remote(&resolved_remotes, name);
864 ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
865 shash_destroy_free_data(&resolved_remotes);
868 stream_ssl_set_key_and_cert(query_db_string(dbs, n_dbs, private_key_file),
869 query_db_string(dbs, n_dbs, certificate_file));
870 stream_ssl_set_ca_cert_file(query_db_string(dbs, n_dbs, ca_cert_file),
875 ovsdb_server_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
876 const char *argv[] OVS_UNUSED,
879 bool *exiting = exiting_;
881 unixctl_command_reply(conn, NULL);
885 ovsdb_server_compact(struct unixctl_conn *conn, int argc,
886 const char *argv[], void *dbs_)
888 struct db *dbs = dbs_;
894 for (db = dbs; db->filename != NULL; db++) {
895 const char *name = db->db->schema->name;
897 if (argc < 2 || !strcmp(argv[1], name)) {
898 struct ovsdb_error *error;
900 VLOG_INFO("compacting %s database by user request", name);
902 error = ovsdb_file_compact(db->file);
904 char *s = ovsdb_error_to_string(error);
905 ds_put_format(&reply, "%s\n", s);
914 unixctl_command_reply_error(conn, "no database by that name");
915 } else if (reply.length) {
916 unixctl_command_reply_error(conn, ds_cstr(&reply));
918 unixctl_command_reply(conn, NULL);
923 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
924 * connections and reconnect. */
926 ovsdb_server_reconnect(struct unixctl_conn *conn, int argc OVS_UNUSED,
927 const char *argv[] OVS_UNUSED, void *jsonrpc_)
929 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
931 ovsdb_jsonrpc_server_reconnect(jsonrpc);
932 unixctl_command_reply(conn, NULL);
935 /* "ovsdb-server/add-remote REMOTE": adds REMOTE to the set of remotes that
936 * ovsdb-server services. */
938 ovsdb_server_add_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
939 const char *argv[], void *aux_)
941 struct add_remote_aux *aux = aux_;
942 const char *remote = argv[1];
944 const struct ovsdb_column *column;
945 const struct ovsdb_table *table;
949 retval = (strncmp("db:", remote, 3)
951 : parse_db_column(aux->dbs, aux->n_dbs, remote,
952 &db, &table, &column));
954 if (sset_add(aux->remotes, remote)) {
955 save_config(aux->config_tmpfile, aux->remotes);
957 unixctl_command_reply(conn, NULL);
959 unixctl_command_reply_error(conn, retval);
964 /* "ovsdb-server/remove-remote REMOTE": removes REMOTE frmo the set of remotes
965 * that ovsdb-server services. */
967 ovsdb_server_remove_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
968 const char *argv[], void *aux_)
970 struct remove_remote_aux *aux = aux_;
971 struct sset_node *node;
973 node = sset_find(aux->remotes, argv[1]);
975 sset_delete(aux->remotes, node);
976 save_config(aux->config_tmpfile, aux->remotes);
977 unixctl_command_reply(conn, NULL);
979 unixctl_command_reply_error(conn, "no such remote");
983 /* "ovsdb-server/list-remotes": outputs a list of configured rmeotes. */
985 ovsdb_server_list_remotes(struct unixctl_conn *conn, int argc OVS_UNUSED,
986 const char *argv[] OVS_UNUSED, void *remotes_)
988 struct sset *remotes = remotes_;
989 const char **list, **p;
994 list = sset_sort(remotes);
995 for (p = list; *p; p++) {
996 ds_put_format(&s, "%s\n", *p);
1000 unixctl_command_reply(conn, ds_cstr(&s));
1005 parse_options(int *argcp, char **argvp[],
1006 struct sset *remotes, char **unixctl_pathp, char **run_command)
1009 OPT_REMOTE = UCHAR_MAX + 1,
1012 OPT_BOOTSTRAP_CA_CERT,
1017 static const struct option long_options[] = {
1018 {"remote", required_argument, NULL, OPT_REMOTE},
1019 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
1020 {"run", required_argument, NULL, OPT_RUN},
1021 {"help", no_argument, NULL, 'h'},
1022 {"version", no_argument, NULL, 'V'},
1023 DAEMON_LONG_OPTIONS,
1025 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
1026 {"private-key", required_argument, NULL, 'p'},
1027 {"certificate", required_argument, NULL, 'c'},
1028 {"ca-cert", required_argument, NULL, 'C'},
1029 {"enable-dummy", optional_argument, NULL, OPT_ENABLE_DUMMY},
1032 char *short_options = long_options_to_short_options(long_options);
1034 char **argv = *argvp;
1040 c = getopt_long(argc, argv, short_options, long_options, NULL);
1047 sset_add(remotes, optarg);
1051 *unixctl_pathp = optarg;
1055 *run_command = optarg;
1062 ovs_print_version(0, 0);
1065 VLOG_OPTION_HANDLERS
1066 DAEMON_OPTION_HANDLERS
1069 private_key_file = optarg;
1073 certificate_file = optarg;
1077 ca_cert_file = optarg;
1078 bootstrap_ca_cert = false;
1081 case OPT_BOOTSTRAP_CA_CERT:
1082 ca_cert_file = optarg;
1083 bootstrap_ca_cert = true;
1086 case OPT_ENABLE_DUMMY:
1087 dummy_enable(optarg && !strcmp(optarg, "override"));
1097 free(short_options);
1106 printf("%s: Open vSwitch database server\n"
1107 "usage: %s [OPTIONS] [DATABASE...]\n"
1108 "where each DATABASE is a database file in ovsdb format.\n"
1109 "The default DATABASE, if none is given, is\n%s/conf.db.\n",
1110 program_name, program_name, ovs_dbdir());
1111 printf("\nJSON-RPC options (may be specified any number of times):\n"
1112 " --remote=REMOTE connect or listen to REMOTE\n");
1113 stream_usage("JSON-RPC", true, true, true);
1116 printf("\nOther options:\n"
1117 " --run COMMAND run COMMAND as subprocess then exit\n"
1118 " --unixctl=SOCKET override default control socket name\n"
1119 " -h, --help display this help message\n"
1120 " -V, --version display version information\n");
1124 /* Truncates and replaces the contents of 'config_file' by a representation
1127 save_config(FILE *config_file, const struct sset *remotes)
1133 if (ftruncate(fileno(config_file), 0) == -1) {
1134 VLOG_FATAL("failed to truncate temporary file (%s)", strerror(errno));
1137 json = json_array_create_empty();
1138 SSET_FOR_EACH (remote, remotes) {
1139 json_array_add(json, json_string_create(remote));
1141 s = json_to_string(json, 0);
1144 if (fseek(config_file, 0, SEEK_SET) != 0
1145 || fputs(s, config_file) == EOF
1146 || fflush(config_file) == EOF) {
1147 VLOG_FATAL("failed to write temporary file (%s)", strerror(errno));
1152 /* Clears and replaces 'remotes' by a configuration read from 'config_file',
1153 * which must have been previously written by save_config(). */
1155 load_config(FILE *config_file, struct sset *remotes)
1160 sset_clear(remotes);
1162 if (fseek(config_file, 0, SEEK_SET) != 0) {
1163 VLOG_FATAL("seek failed in temporary file (%s)", strerror(errno));
1165 json = json_from_stream(config_file);
1166 if (json->type == JSON_STRING) {
1167 VLOG_FATAL("reading json failed (%s)", json_string(json));
1169 ovs_assert(json->type == JSON_ARRAY);
1170 for (i = 0; i < json->u.array.n; i++) {
1171 const struct json *remote = json->u.array.elems[i];
1172 sset_add(remotes, json_string(remote));