1 /* Copyright (c) 2009, 2010, 2011, 2012 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"
32 #include "jsonrpc-server.h"
33 #include "leak-checker.h"
37 #include "ovsdb-data.h"
38 #include "ovsdb-types.h"
39 #include "ovsdb-error.h"
40 #include "poll-loop.h"
44 #include "stream-ssl.h"
50 #include "transaction.h"
56 VLOG_DEFINE_THIS_MODULE(ovsdb_server);
58 /* SSL configuration. */
59 static char *private_key_file;
60 static char *certificate_file;
61 static char *ca_cert_file;
62 static bool bootstrap_ca_cert;
64 static unixctl_cb_func ovsdb_server_exit;
65 static unixctl_cb_func ovsdb_server_compact;
66 static unixctl_cb_func ovsdb_server_reconnect;
68 static void parse_options(int argc, char *argv[], char **file_namep,
69 struct sset *remotes, char **unixctl_pathp,
71 static void usage(void) NO_RETURN;
73 static void reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
74 const struct ovsdb *db, struct sset *remotes);
76 static void update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
77 const struct sset *remotes,
81 main(int argc, char *argv[])
83 char *unixctl_path = NULL;
84 char *run_command = NULL;
85 struct unixctl_server *unixctl;
86 struct ovsdb_jsonrpc_server *jsonrpc;
88 struct ovsdb_error *error;
89 struct ovsdb_file *file;
91 struct process *run_process;
95 long long int status_timer = LLONG_MIN;
97 proctitle_init(argc, argv);
98 set_program_name(argv[0]);
99 stress_init_command();
100 signal(SIGPIPE, SIG_IGN);
103 parse_options(argc, argv, &file_name, &remotes, &unixctl_path,
108 error = ovsdb_file_open(file_name, false, &db, &file);
110 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
114 jsonrpc = ovsdb_jsonrpc_server_create(db);
115 reconfigure_from_db(jsonrpc, db, &remotes);
117 retval = unixctl_server_create(unixctl_path, &unixctl);
125 run_argv[0] = "/bin/sh";
127 run_argv[2] = run_command;
130 retval = process_start(run_argv, NULL, 0, NULL, 0, &run_process);
132 ovs_fatal(retval, "%s: process failed to start", run_command);
138 daemonize_complete();
141 /* ovsdb-server is usually a long-running process, in which case it
142 * makes plenty of sense to log the version, but --run makes
143 * ovsdb-server more like a command-line tool, so skip it. */
144 VLOG_INFO("%s (Open vSwitch) %s", program_name, VERSION);
147 unixctl_command_register("exit", "", 0, 0, ovsdb_server_exit, &exiting);
148 unixctl_command_register("ovsdb-server/compact", "", 0, 0,
149 ovsdb_server_compact, file);
150 unixctl_command_register("ovsdb-server/reconnect", "", 0, 0,
151 ovsdb_server_reconnect, jsonrpc);
156 if (memory_should_report()) {
160 ovsdb_jsonrpc_server_get_memory_usage(jsonrpc, &usage);
161 ovsdb_get_memory_usage(db, &usage);
162 memory_report(&usage);
163 simap_destroy(&usage);
166 reconfigure_from_db(jsonrpc, db, &remotes);
167 ovsdb_jsonrpc_server_run(jsonrpc);
168 unixctl_server_run(unixctl);
169 ovsdb_trigger_run(db, time_msec());
170 if (run_process && process_exited(run_process)) {
174 /* update Manager status(es) every 5 seconds */
175 if (time_msec() >= status_timer) {
176 status_timer = time_msec() + 5000;
177 update_remote_status(jsonrpc, &remotes, db);
181 ovsdb_jsonrpc_server_wait(jsonrpc);
182 unixctl_server_wait(unixctl);
183 ovsdb_trigger_wait(db, time_msec());
185 process_wait(run_process);
188 poll_immediate_wake();
190 poll_timer_wait_until(status_timer);
193 ovsdb_jsonrpc_server_destroy(jsonrpc);
195 sset_destroy(&remotes);
196 unixctl_server_destroy(unixctl);
198 if (run_process && process_exited(run_process)) {
199 int status = process_status(run_process);
201 ovs_fatal(0, "%s: child exited, %s",
202 run_command, process_status_msg(status));
210 parse_db_column(const struct ovsdb *db,
212 const struct ovsdb_table **tablep,
213 const struct ovsdb_column **columnp)
215 char *name, *table_name, *column_name;
216 const struct ovsdb_column *column;
217 const struct ovsdb_table *table;
218 char *save_ptr = NULL;
220 name = xstrdup(name_);
221 strtok_r(name, ":", &save_ptr); /* "db:" */
222 table_name = strtok_r(NULL, ",", &save_ptr);
223 column_name = strtok_r(NULL, ",", &save_ptr);
224 if (!table_name || !column_name) {
225 ovs_fatal(0, "\"%s\": invalid syntax", name_);
228 table = ovsdb_get_table(db, table_name);
230 ovs_fatal(0, "\"%s\": no table named %s", name_, table_name);
233 column = ovsdb_table_schema_get_column(table->schema, column_name);
235 ovs_fatal(0, "\"%s\": table \"%s\" has no column \"%s\"",
236 name_, table_name, column_name);
245 parse_db_string_column(const struct ovsdb *db,
247 const struct ovsdb_table **tablep,
248 const struct ovsdb_column **columnp)
250 const struct ovsdb_column *column;
251 const struct ovsdb_table *table;
253 parse_db_column(db, name, &table, &column);
255 if (column->type.key.type != OVSDB_TYPE_STRING
256 || column->type.value.type != OVSDB_TYPE_VOID) {
257 ovs_fatal(0, "\"%s\": table \"%s\" column \"%s\" is "
258 "not string or set of strings",
259 name, table->schema->name, column->name);
266 static OVS_UNUSED const char *
267 query_db_string(const struct ovsdb *db, const char *name)
269 if (!name || strncmp(name, "db:", 3)) {
272 const struct ovsdb_column *column;
273 const struct ovsdb_table *table;
274 const struct ovsdb_row *row;
276 parse_db_string_column(db, name, &table, &column);
278 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
279 const struct ovsdb_datum *datum;
282 datum = &row->fields[column->index];
283 for (i = 0; i < datum->n; i++) {
284 if (datum->keys[i].string[0]) {
285 return datum->keys[i].string;
293 static struct ovsdb_jsonrpc_options *
294 add_remote(struct shash *remotes, const char *target)
296 struct ovsdb_jsonrpc_options *options;
298 options = shash_find_data(remotes, target);
300 options = ovsdb_jsonrpc_default_options(target);
301 shash_add(remotes, target, options);
307 static struct ovsdb_datum *
308 get_datum(struct ovsdb_row *row, const char *column_name,
309 const enum ovsdb_atomic_type key_type,
310 const enum ovsdb_atomic_type value_type,
313 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
314 const struct ovsdb_table_schema *schema = row->table->schema;
315 const struct ovsdb_column *column;
317 column = ovsdb_table_schema_get_column(schema, column_name);
319 VLOG_DBG_RL(&rl, "Table `%s' has no `%s' column",
320 schema->name, column_name);
324 if (column->type.key.type != key_type
325 || column->type.value.type != value_type
326 || column->type.n_max != n_max) {
327 if (!VLOG_DROP_DBG(&rl)) {
328 char *type_name = ovsdb_type_to_english(&column->type);
329 VLOG_DBG("Table `%s' column `%s' has type %s, not expected "
330 "key type %s, value type %s, max elements %zd.",
331 schema->name, column_name, type_name,
332 ovsdb_atomic_type_to_string(key_type),
333 ovsdb_atomic_type_to_string(value_type),
340 return &row->fields[column->index];
343 /* Read string-string key-values from a map. Returns the value associated with
344 * 'key', if found, or NULL */
346 read_map_string_column(const struct ovsdb_row *row, const char *column_name,
349 const struct ovsdb_datum *datum;
350 union ovsdb_atom *atom_key = NULL, *atom_value = NULL;
353 datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name,
354 OVSDB_TYPE_STRING, OVSDB_TYPE_STRING, UINT_MAX);
360 for (i = 0; i < datum->n; i++) {
361 atom_key = &datum->keys[i];
362 if (!strcmp(atom_key->string, key)){
363 atom_value = &datum->values[i];
368 return atom_value ? atom_value->string : NULL;
371 static const union ovsdb_atom *
372 read_column(const struct ovsdb_row *row, const char *column_name,
373 enum ovsdb_atomic_type type)
375 const struct ovsdb_datum *datum;
377 datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name, type,
379 return datum && datum->n ? datum->keys : NULL;
383 read_integer_column(const struct ovsdb_row *row, const char *column_name,
384 long long int *integerp)
386 const union ovsdb_atom *atom;
388 atom = read_column(row, column_name, OVSDB_TYPE_INTEGER);
389 *integerp = atom ? atom->integer : 0;
394 read_string_column(const struct ovsdb_row *row, const char *column_name,
395 const char **stringp)
397 const union ovsdb_atom *atom;
399 atom = read_column(row, column_name, OVSDB_TYPE_STRING);
400 *stringp = atom ? atom->string : NULL;
405 write_bool_column(struct ovsdb_row *row, const char *column_name, bool value)
407 const struct ovsdb_column *column;
408 struct ovsdb_datum *datum;
410 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
411 datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
418 ovsdb_datum_destroy(datum, &column->type);
421 datum->keys = xmalloc(sizeof *datum->keys);
422 datum->values = NULL;
425 datum->keys[0].boolean = value;
429 write_string_string_column(struct ovsdb_row *row, const char *column_name,
430 char **keys, char **values, size_t n)
432 const struct ovsdb_column *column;
433 struct ovsdb_datum *datum;
436 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
437 datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
443 /* Free existing data. */
444 ovsdb_datum_destroy(datum, &column->type);
446 /* Allocate space for new values. */
448 datum->keys = xmalloc(n * sizeof *datum->keys);
449 datum->values = xmalloc(n * sizeof *datum->values);
451 for (i = 0; i < n; ++i) {
452 datum->keys[i].string = keys[i];
453 datum->values[i].string = values[i];
456 /* Sort and check constraints. */
457 ovsdb_datum_sort_assert(datum, column->type.key.type);
460 /* Adds a remote and options to 'remotes', based on the Manager table row in
463 add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
465 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
466 struct ovsdb_jsonrpc_options *options;
467 long long int max_backoff, probe_interval;
468 const char *target, *dscp_string;
470 if (!read_string_column(row, "target", &target) || !target) {
471 VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
472 row->table->schema->name);
476 options = add_remote(remotes, target);
477 if (read_integer_column(row, "max_backoff", &max_backoff)) {
478 options->max_backoff = max_backoff;
480 if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
481 options->probe_interval = probe_interval;
484 options->dscp = DSCP_DEFAULT;
485 dscp_string = read_map_string_column(row, "other_config", "dscp");
487 int dscp = atoi(dscp_string);
488 if (dscp >= 0 && dscp <= 63) {
489 options->dscp = dscp;
495 query_db_remotes(const char *name, const struct ovsdb *db,
496 struct shash *remotes)
498 const struct ovsdb_column *column;
499 const struct ovsdb_table *table;
500 const struct ovsdb_row *row;
502 parse_db_column(db, name, &table, &column);
504 if (column->type.key.type == OVSDB_TYPE_STRING
505 && column->type.value.type == OVSDB_TYPE_VOID) {
506 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
507 const struct ovsdb_datum *datum;
510 datum = &row->fields[column->index];
511 for (i = 0; i < datum->n; i++) {
512 add_remote(remotes, datum->keys[i].string);
515 } else if (column->type.key.type == OVSDB_TYPE_UUID
516 && column->type.key.u.uuid.refTable
517 && column->type.value.type == OVSDB_TYPE_VOID) {
518 const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
519 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
520 const struct ovsdb_datum *datum;
523 datum = &row->fields[column->index];
524 for (i = 0; i < datum->n; i++) {
525 const struct ovsdb_row *ref_row;
527 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
529 add_manager_options(remotes, ref_row);
537 update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
538 const struct ovsdb_jsonrpc_server *jsonrpc)
540 struct ovsdb_jsonrpc_remote_status status;
541 struct ovsdb_row *rw_row;
543 char *keys[8], *values[8];
546 /* Get the "target" (protocol/host/port) spec. */
547 if (!read_string_column(row, "target", &target)) {
548 /* Bad remote spec or incorrect schema. */
551 rw_row = ovsdb_txn_row_modify(txn, row);
552 ovsdb_jsonrpc_server_get_remote_status(jsonrpc, target, &status);
554 /* Update status information columns. */
555 write_bool_column(rw_row, "is_connected", status.is_connected);
558 keys[n] = xstrdup("state");
559 values[n++] = xstrdup(status.state);
561 if (status.sec_since_connect != UINT_MAX) {
562 keys[n] = xstrdup("sec_since_connect");
563 values[n++] = xasprintf("%u", status.sec_since_connect);
565 if (status.sec_since_disconnect != UINT_MAX) {
566 keys[n] = xstrdup("sec_since_disconnect");
567 values[n++] = xasprintf("%u", status.sec_since_disconnect);
569 if (status.last_error) {
570 keys[n] = xstrdup("last_error");
572 xstrdup(ovs_retval_to_string(status.last_error));
574 if (status.locks_held && status.locks_held[0]) {
575 keys[n] = xstrdup("locks_held");
576 values[n++] = xstrdup(status.locks_held);
578 if (status.locks_waiting && status.locks_waiting[0]) {
579 keys[n] = xstrdup("locks_waiting");
580 values[n++] = xstrdup(status.locks_waiting);
582 if (status.locks_lost && status.locks_lost[0]) {
583 keys[n] = xstrdup("locks_lost");
584 values[n++] = xstrdup(status.locks_lost);
586 if (status.n_connections > 1) {
587 keys[n] = xstrdup("n_connections");
588 values[n++] = xasprintf("%d", status.n_connections);
590 write_string_string_column(rw_row, "status", keys, values, n);
592 ovsdb_jsonrpc_server_free_remote_status(&status);
596 update_remote_rows(const struct ovsdb *db, struct ovsdb_txn *txn,
597 const char *remote_name,
598 const struct ovsdb_jsonrpc_server *jsonrpc)
600 const struct ovsdb_table *table, *ref_table;
601 const struct ovsdb_column *column;
602 const struct ovsdb_row *row;
604 if (strncmp("db:", remote_name, 3)) {
608 parse_db_column(db, remote_name, &table, &column);
610 if (column->type.key.type != OVSDB_TYPE_UUID
611 || !column->type.key.u.uuid.refTable
612 || column->type.value.type != OVSDB_TYPE_VOID) {
616 ref_table = column->type.key.u.uuid.refTable;
618 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
619 const struct ovsdb_datum *datum;
622 datum = &row->fields[column->index];
623 for (i = 0; i < datum->n; i++) {
624 const struct ovsdb_row *ref_row;
626 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
628 update_remote_row(ref_row, txn, jsonrpc);
635 update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
636 const struct sset *remotes, struct ovsdb *db)
638 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
639 struct ovsdb_txn *txn;
640 const bool durable_txn = false;
641 struct ovsdb_error *error;
644 txn = ovsdb_txn_create(db);
646 /* Iterate over --remote arguments given on command line. */
647 SSET_FOR_EACH (remote, remotes) {
648 update_remote_rows(db, txn, remote, jsonrpc);
651 error = ovsdb_txn_commit(txn, durable_txn);
653 VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
654 ovsdb_error_to_string(error));
658 /* Reconfigures ovsdb-server based on information in the database. */
660 reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
661 const struct ovsdb *db, struct sset *remotes)
663 struct shash resolved_remotes;
666 /* Configure remotes. */
667 shash_init(&resolved_remotes);
668 SSET_FOR_EACH (name, remotes) {
669 if (!strncmp(name, "db:", 3)) {
670 query_db_remotes(name, db, &resolved_remotes);
672 add_remote(&resolved_remotes, name);
675 ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
676 shash_destroy_free_data(&resolved_remotes);
679 stream_ssl_set_key_and_cert(query_db_string(db, private_key_file),
680 query_db_string(db, certificate_file));
681 stream_ssl_set_ca_cert_file(query_db_string(db, ca_cert_file),
686 ovsdb_server_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
687 const char *argv[] OVS_UNUSED,
690 bool *exiting = exiting_;
692 unixctl_command_reply(conn, NULL);
696 ovsdb_server_compact(struct unixctl_conn *conn, int argc OVS_UNUSED,
697 const char *argv[] OVS_UNUSED, void *file_)
699 struct ovsdb_file *file = file_;
700 struct ovsdb_error *error;
702 VLOG_INFO("compacting database by user request");
703 error = ovsdb_file_compact(file);
705 unixctl_command_reply(conn, NULL);
707 char *s = ovsdb_error_to_string(error);
708 ovsdb_error_destroy(error);
709 unixctl_command_reply_error(conn, s);
714 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
715 * connections and reconnect. */
717 ovsdb_server_reconnect(struct unixctl_conn *conn, int argc OVS_UNUSED,
718 const char *argv[] OVS_UNUSED, void *jsonrpc_)
720 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
722 ovsdb_jsonrpc_server_reconnect(jsonrpc);
723 unixctl_command_reply(conn, NULL);
727 parse_options(int argc, char *argv[], char **file_namep,
728 struct sset *remotes, char **unixctl_pathp,
732 OPT_DUMMY = UCHAR_MAX + 1,
736 OPT_BOOTSTRAP_CA_CERT,
738 LEAK_CHECKER_OPTION_ENUMS,
741 static struct option long_options[] = {
742 {"remote", required_argument, NULL, OPT_REMOTE},
743 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
744 {"run", required_argument, NULL, OPT_RUN},
745 {"help", no_argument, NULL, 'h'},
746 {"version", no_argument, NULL, 'V'},
749 LEAK_CHECKER_LONG_OPTIONS,
750 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
751 {"private-key", required_argument, NULL, 'p'},
752 {"certificate", required_argument, NULL, 'c'},
753 {"ca-cert", required_argument, NULL, 'C'},
756 char *short_options = long_options_to_short_options(long_options);
762 c = getopt_long(argc, argv, short_options, long_options, NULL);
769 sset_add(remotes, optarg);
773 *unixctl_pathp = optarg;
777 *run_command = optarg;
784 ovs_print_version(0, 0);
788 DAEMON_OPTION_HANDLERS
789 LEAK_CHECKER_OPTION_HANDLERS
792 private_key_file = optarg;
796 certificate_file = optarg;
800 ca_cert_file = optarg;
801 bootstrap_ca_cert = false;
804 case OPT_BOOTSTRAP_CA_CERT:
805 ca_cert_file = optarg;
806 bootstrap_ca_cert = true;
823 *file_namep = xasprintf("%s/conf.db", ovs_dbdir());
827 *file_namep = xstrdup(argv[0]);
831 ovs_fatal(0, "database file is only non-option argument; "
832 "use --help for usage");
839 printf("%s: Open vSwitch database server\n"
840 "usage: %s [OPTIONS] DATABASE\n"
841 "where DATABASE is a database file in ovsdb format.\n",
842 program_name, program_name);
843 printf("\nJSON-RPC options (may be specified any number of times):\n"
844 " --remote=REMOTE connect or listen to REMOTE\n");
845 stream_usage("JSON-RPC", true, true, true);
848 printf("\nOther options:\n"
849 " --run COMMAND run COMMAND as subprocess then exit\n"
850 " --unixctl=SOCKET override default control socket name\n"
851 " -h, --help display this help message\n"
852 " -V, --version display version information\n");
853 leak_checker_usage();