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"
33 #include "jsonrpc-server.h"
34 #include "leak-checker.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);
59 /* SSL configuration. */
60 static char *private_key_file;
61 static char *certificate_file;
62 static char *ca_cert_file;
63 static bool bootstrap_ca_cert;
65 static unixctl_cb_func ovsdb_server_exit;
66 static unixctl_cb_func ovsdb_server_compact;
67 static unixctl_cb_func ovsdb_server_reconnect;
69 static void parse_options(int argc, char *argv[], char **file_namep,
70 struct sset *remotes, char **unixctl_pathp,
72 static void usage(void) NO_RETURN;
74 static void reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
75 const struct ovsdb *db, struct sset *remotes);
77 static void update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
78 const struct sset *remotes,
82 main(int argc, char *argv[])
84 char *unixctl_path = NULL;
85 char *run_command = NULL;
86 struct unixctl_server *unixctl;
87 struct ovsdb_jsonrpc_server *jsonrpc;
89 struct ovsdb_error *error;
90 struct ovsdb_file *file;
92 struct process *run_process;
96 long long int status_timer = LLONG_MIN;
98 proctitle_init(argc, argv);
99 set_program_name(argv[0]);
100 stress_init_command();
101 signal(SIGPIPE, SIG_IGN);
104 parse_options(argc, argv, &file_name, &remotes, &unixctl_path,
109 error = ovsdb_file_open(file_name, false, &db, &file);
111 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
115 jsonrpc = ovsdb_jsonrpc_server_create(db);
116 reconfigure_from_db(jsonrpc, db, &remotes);
118 retval = unixctl_server_create(unixctl_path, &unixctl);
126 run_argv[0] = "/bin/sh";
128 run_argv[2] = run_command;
131 retval = process_start(run_argv, NULL, 0, NULL, 0, &run_process);
133 ovs_fatal(retval, "%s: process failed to start", run_command);
139 daemonize_complete();
142 /* ovsdb-server is usually a long-running process, in which case it
143 * makes plenty of sense to log the version, but --run makes
144 * ovsdb-server more like a command-line tool, so skip it. */
145 VLOG_INFO("%s (Open vSwitch) %s", program_name, VERSION);
148 unixctl_command_register("exit", "", 0, 0, ovsdb_server_exit, &exiting);
149 unixctl_command_register("ovsdb-server/compact", "", 0, 0,
150 ovsdb_server_compact, file);
151 unixctl_command_register("ovsdb-server/reconnect", "", 0, 0,
152 ovsdb_server_reconnect, jsonrpc);
157 if (memory_should_report()) {
161 ovsdb_jsonrpc_server_get_memory_usage(jsonrpc, &usage);
162 ovsdb_get_memory_usage(db, &usage);
163 memory_report(&usage);
164 simap_destroy(&usage);
167 reconfigure_from_db(jsonrpc, db, &remotes);
168 ovsdb_jsonrpc_server_run(jsonrpc);
169 unixctl_server_run(unixctl);
170 ovsdb_trigger_run(db, time_msec());
171 if (run_process && process_exited(run_process)) {
175 /* update Manager status(es) every 5 seconds */
176 if (time_msec() >= status_timer) {
177 status_timer = time_msec() + 5000;
178 update_remote_status(jsonrpc, &remotes, db);
182 ovsdb_jsonrpc_server_wait(jsonrpc);
183 unixctl_server_wait(unixctl);
184 ovsdb_trigger_wait(db, time_msec());
186 process_wait(run_process);
189 poll_immediate_wake();
191 poll_timer_wait_until(status_timer);
194 ovsdb_jsonrpc_server_destroy(jsonrpc);
196 sset_destroy(&remotes);
197 unixctl_server_destroy(unixctl);
199 if (run_process && process_exited(run_process)) {
200 int status = process_status(run_process);
202 ovs_fatal(0, "%s: child exited, %s",
203 run_command, process_status_msg(status));
211 parse_db_column(const struct ovsdb *db,
213 const struct ovsdb_table **tablep,
214 const struct ovsdb_column **columnp)
216 char *name, *table_name, *column_name;
217 const struct ovsdb_column *column;
218 const struct ovsdb_table *table;
219 char *save_ptr = NULL;
221 name = xstrdup(name_);
222 strtok_r(name, ":", &save_ptr); /* "db:" */
223 table_name = strtok_r(NULL, ",", &save_ptr);
224 column_name = strtok_r(NULL, ",", &save_ptr);
225 if (!table_name || !column_name) {
226 ovs_fatal(0, "\"%s\": invalid syntax", name_);
229 table = ovsdb_get_table(db, table_name);
231 ovs_fatal(0, "\"%s\": no table named %s", name_, table_name);
234 column = ovsdb_table_schema_get_column(table->schema, column_name);
236 ovs_fatal(0, "\"%s\": table \"%s\" has no column \"%s\"",
237 name_, table_name, column_name);
246 parse_db_string_column(const struct ovsdb *db,
248 const struct ovsdb_table **tablep,
249 const struct ovsdb_column **columnp)
251 const struct ovsdb_column *column;
252 const struct ovsdb_table *table;
254 parse_db_column(db, name, &table, &column);
256 if (column->type.key.type != OVSDB_TYPE_STRING
257 || column->type.value.type != OVSDB_TYPE_VOID) {
258 ovs_fatal(0, "\"%s\": table \"%s\" column \"%s\" is "
259 "not string or set of strings",
260 name, table->schema->name, column->name);
267 static OVS_UNUSED const char *
268 query_db_string(const struct ovsdb *db, const char *name)
270 if (!name || strncmp(name, "db:", 3)) {
273 const struct ovsdb_column *column;
274 const struct ovsdb_table *table;
275 const struct ovsdb_row *row;
277 parse_db_string_column(db, name, &table, &column);
279 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
280 const struct ovsdb_datum *datum;
283 datum = &row->fields[column->index];
284 for (i = 0; i < datum->n; i++) {
285 if (datum->keys[i].string[0]) {
286 return datum->keys[i].string;
294 static struct ovsdb_jsonrpc_options *
295 add_remote(struct shash *remotes, const char *target)
297 struct ovsdb_jsonrpc_options *options;
299 options = shash_find_data(remotes, target);
301 options = ovsdb_jsonrpc_default_options(target);
302 shash_add(remotes, target, options);
308 static struct ovsdb_datum *
309 get_datum(struct ovsdb_row *row, const char *column_name,
310 const enum ovsdb_atomic_type key_type,
311 const enum ovsdb_atomic_type value_type,
314 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
315 const struct ovsdb_table_schema *schema = row->table->schema;
316 const struct ovsdb_column *column;
318 column = ovsdb_table_schema_get_column(schema, column_name);
320 VLOG_DBG_RL(&rl, "Table `%s' has no `%s' column",
321 schema->name, column_name);
325 if (column->type.key.type != key_type
326 || column->type.value.type != value_type
327 || column->type.n_max != n_max) {
328 if (!VLOG_DROP_DBG(&rl)) {
329 char *type_name = ovsdb_type_to_english(&column->type);
330 VLOG_DBG("Table `%s' column `%s' has type %s, not expected "
331 "key type %s, value type %s, max elements %zd.",
332 schema->name, column_name, type_name,
333 ovsdb_atomic_type_to_string(key_type),
334 ovsdb_atomic_type_to_string(value_type),
341 return &row->fields[column->index];
344 /* Read string-string key-values from a map. Returns the value associated with
345 * 'key', if found, or NULL */
347 read_map_string_column(const struct ovsdb_row *row, const char *column_name,
350 const struct ovsdb_datum *datum;
351 union ovsdb_atom *atom_key = NULL, *atom_value = NULL;
354 datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name,
355 OVSDB_TYPE_STRING, OVSDB_TYPE_STRING, UINT_MAX);
361 for (i = 0; i < datum->n; i++) {
362 atom_key = &datum->keys[i];
363 if (!strcmp(atom_key->string, key)){
364 atom_value = &datum->values[i];
369 return atom_value ? atom_value->string : NULL;
372 static const union ovsdb_atom *
373 read_column(const struct ovsdb_row *row, const char *column_name,
374 enum ovsdb_atomic_type type)
376 const struct ovsdb_datum *datum;
378 datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name, type,
380 return datum && datum->n ? datum->keys : NULL;
384 read_integer_column(const struct ovsdb_row *row, const char *column_name,
385 long long int *integerp)
387 const union ovsdb_atom *atom;
389 atom = read_column(row, column_name, OVSDB_TYPE_INTEGER);
390 *integerp = atom ? atom->integer : 0;
395 read_string_column(const struct ovsdb_row *row, const char *column_name,
396 const char **stringp)
398 const union ovsdb_atom *atom;
400 atom = read_column(row, column_name, OVSDB_TYPE_STRING);
401 *stringp = atom ? atom->string : NULL;
406 write_bool_column(struct ovsdb_row *row, const char *column_name, bool value)
408 const struct ovsdb_column *column;
409 struct ovsdb_datum *datum;
411 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
412 datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
419 ovsdb_datum_destroy(datum, &column->type);
422 datum->keys = xmalloc(sizeof *datum->keys);
423 datum->values = NULL;
426 datum->keys[0].boolean = value;
430 write_string_string_column(struct ovsdb_row *row, const char *column_name,
431 char **keys, char **values, size_t n)
433 const struct ovsdb_column *column;
434 struct ovsdb_datum *datum;
437 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
438 datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
444 /* Free existing data. */
445 ovsdb_datum_destroy(datum, &column->type);
447 /* Allocate space for new values. */
449 datum->keys = xmalloc(n * sizeof *datum->keys);
450 datum->values = xmalloc(n * sizeof *datum->values);
452 for (i = 0; i < n; ++i) {
453 datum->keys[i].string = keys[i];
454 datum->values[i].string = values[i];
457 /* Sort and check constraints. */
458 ovsdb_datum_sort_assert(datum, column->type.key.type);
461 /* Adds a remote and options to 'remotes', based on the Manager table row in
464 add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
466 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
467 struct ovsdb_jsonrpc_options *options;
468 long long int max_backoff, probe_interval;
469 const char *target, *dscp_string;
471 if (!read_string_column(row, "target", &target) || !target) {
472 VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
473 row->table->schema->name);
477 options = add_remote(remotes, target);
478 if (read_integer_column(row, "max_backoff", &max_backoff)) {
479 options->max_backoff = max_backoff;
481 if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
482 options->probe_interval = probe_interval;
485 options->dscp = DSCP_DEFAULT;
486 dscp_string = read_map_string_column(row, "other_config", "dscp");
488 int dscp = atoi(dscp_string);
489 if (dscp >= 0 && dscp <= 63) {
490 options->dscp = dscp;
496 query_db_remotes(const char *name, const struct ovsdb *db,
497 struct shash *remotes)
499 const struct ovsdb_column *column;
500 const struct ovsdb_table *table;
501 const struct ovsdb_row *row;
503 parse_db_column(db, name, &table, &column);
505 if (column->type.key.type == OVSDB_TYPE_STRING
506 && column->type.value.type == OVSDB_TYPE_VOID) {
507 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
508 const struct ovsdb_datum *datum;
511 datum = &row->fields[column->index];
512 for (i = 0; i < datum->n; i++) {
513 add_remote(remotes, datum->keys[i].string);
516 } else if (column->type.key.type == OVSDB_TYPE_UUID
517 && column->type.key.u.uuid.refTable
518 && column->type.value.type == OVSDB_TYPE_VOID) {
519 const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
520 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
521 const struct ovsdb_datum *datum;
524 datum = &row->fields[column->index];
525 for (i = 0; i < datum->n; i++) {
526 const struct ovsdb_row *ref_row;
528 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
530 add_manager_options(remotes, ref_row);
538 update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
539 const struct ovsdb_jsonrpc_server *jsonrpc)
541 struct ovsdb_jsonrpc_remote_status status;
542 struct ovsdb_row *rw_row;
544 char *keys[8], *values[8];
547 /* Get the "target" (protocol/host/port) spec. */
548 if (!read_string_column(row, "target", &target)) {
549 /* Bad remote spec or incorrect schema. */
552 rw_row = ovsdb_txn_row_modify(txn, row);
553 ovsdb_jsonrpc_server_get_remote_status(jsonrpc, target, &status);
555 /* Update status information columns. */
556 write_bool_column(rw_row, "is_connected", status.is_connected);
559 keys[n] = xstrdup("state");
560 values[n++] = xstrdup(status.state);
562 if (status.sec_since_connect != UINT_MAX) {
563 keys[n] = xstrdup("sec_since_connect");
564 values[n++] = xasprintf("%u", status.sec_since_connect);
566 if (status.sec_since_disconnect != UINT_MAX) {
567 keys[n] = xstrdup("sec_since_disconnect");
568 values[n++] = xasprintf("%u", status.sec_since_disconnect);
570 if (status.last_error) {
571 keys[n] = xstrdup("last_error");
573 xstrdup(ovs_retval_to_string(status.last_error));
575 if (status.locks_held && status.locks_held[0]) {
576 keys[n] = xstrdup("locks_held");
577 values[n++] = xstrdup(status.locks_held);
579 if (status.locks_waiting && status.locks_waiting[0]) {
580 keys[n] = xstrdup("locks_waiting");
581 values[n++] = xstrdup(status.locks_waiting);
583 if (status.locks_lost && status.locks_lost[0]) {
584 keys[n] = xstrdup("locks_lost");
585 values[n++] = xstrdup(status.locks_lost);
587 if (status.n_connections > 1) {
588 keys[n] = xstrdup("n_connections");
589 values[n++] = xasprintf("%d", status.n_connections);
591 write_string_string_column(rw_row, "status", keys, values, n);
593 ovsdb_jsonrpc_server_free_remote_status(&status);
597 update_remote_rows(const struct ovsdb *db, struct ovsdb_txn *txn,
598 const char *remote_name,
599 const struct ovsdb_jsonrpc_server *jsonrpc)
601 const struct ovsdb_table *table, *ref_table;
602 const struct ovsdb_column *column;
603 const struct ovsdb_row *row;
605 if (strncmp("db:", remote_name, 3)) {
609 parse_db_column(db, remote_name, &table, &column);
611 if (column->type.key.type != OVSDB_TYPE_UUID
612 || !column->type.key.u.uuid.refTable
613 || column->type.value.type != OVSDB_TYPE_VOID) {
617 ref_table = column->type.key.u.uuid.refTable;
619 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
620 const struct ovsdb_datum *datum;
623 datum = &row->fields[column->index];
624 for (i = 0; i < datum->n; i++) {
625 const struct ovsdb_row *ref_row;
627 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
629 update_remote_row(ref_row, txn, jsonrpc);
636 update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
637 const struct sset *remotes, struct ovsdb *db)
639 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
640 struct ovsdb_txn *txn;
641 const bool durable_txn = false;
642 struct ovsdb_error *error;
645 txn = ovsdb_txn_create(db);
647 /* Iterate over --remote arguments given on command line. */
648 SSET_FOR_EACH (remote, remotes) {
649 update_remote_rows(db, txn, remote, jsonrpc);
652 error = ovsdb_txn_commit(txn, durable_txn);
654 VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
655 ovsdb_error_to_string(error));
659 /* Reconfigures ovsdb-server based on information in the database. */
661 reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
662 const struct ovsdb *db, struct sset *remotes)
664 struct shash resolved_remotes;
667 /* Configure remotes. */
668 shash_init(&resolved_remotes);
669 SSET_FOR_EACH (name, remotes) {
670 if (!strncmp(name, "db:", 3)) {
671 query_db_remotes(name, db, &resolved_remotes);
673 add_remote(&resolved_remotes, name);
676 ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
677 shash_destroy_free_data(&resolved_remotes);
680 stream_ssl_set_key_and_cert(query_db_string(db, private_key_file),
681 query_db_string(db, certificate_file));
682 stream_ssl_set_ca_cert_file(query_db_string(db, ca_cert_file),
687 ovsdb_server_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
688 const char *argv[] OVS_UNUSED,
691 bool *exiting = exiting_;
693 unixctl_command_reply(conn, NULL);
697 ovsdb_server_compact(struct unixctl_conn *conn, int argc OVS_UNUSED,
698 const char *argv[] OVS_UNUSED, void *file_)
700 struct ovsdb_file *file = file_;
701 struct ovsdb_error *error;
703 VLOG_INFO("compacting database by user request");
704 error = ovsdb_file_compact(file);
706 unixctl_command_reply(conn, NULL);
708 char *s = ovsdb_error_to_string(error);
709 ovsdb_error_destroy(error);
710 unixctl_command_reply_error(conn, s);
715 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
716 * connections and reconnect. */
718 ovsdb_server_reconnect(struct unixctl_conn *conn, int argc OVS_UNUSED,
719 const char *argv[] OVS_UNUSED, void *jsonrpc_)
721 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
723 ovsdb_jsonrpc_server_reconnect(jsonrpc);
724 unixctl_command_reply(conn, NULL);
728 parse_options(int argc, char *argv[], char **file_namep,
729 struct sset *remotes, char **unixctl_pathp,
733 OPT_REMOTE = UCHAR_MAX + 1,
736 OPT_BOOTSTRAP_CA_CERT,
739 LEAK_CHECKER_OPTION_ENUMS,
742 static struct option long_options[] = {
743 {"remote", required_argument, NULL, OPT_REMOTE},
744 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
745 {"run", required_argument, NULL, OPT_RUN},
746 {"help", no_argument, NULL, 'h'},
747 {"version", no_argument, NULL, 'V'},
750 LEAK_CHECKER_LONG_OPTIONS,
751 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
752 {"private-key", required_argument, NULL, 'p'},
753 {"certificate", required_argument, NULL, 'c'},
754 {"ca-cert", required_argument, NULL, 'C'},
755 {"enable-dummy", optional_argument, NULL, OPT_ENABLE_DUMMY},
758 char *short_options = long_options_to_short_options(long_options);
764 c = getopt_long(argc, argv, short_options, long_options, NULL);
771 sset_add(remotes, optarg);
775 *unixctl_pathp = optarg;
779 *run_command = optarg;
786 ovs_print_version(0, 0);
790 DAEMON_OPTION_HANDLERS
791 LEAK_CHECKER_OPTION_HANDLERS
794 private_key_file = optarg;
798 certificate_file = optarg;
802 ca_cert_file = optarg;
803 bootstrap_ca_cert = false;
806 case OPT_BOOTSTRAP_CA_CERT:
807 ca_cert_file = optarg;
808 bootstrap_ca_cert = true;
811 case OPT_ENABLE_DUMMY:
812 dummy_enable(optarg && !strcmp(optarg, "override"));
829 *file_namep = xasprintf("%s/conf.db", ovs_dbdir());
833 *file_namep = xstrdup(argv[0]);
837 ovs_fatal(0, "database file is only non-option argument; "
838 "use --help for usage");
845 printf("%s: Open vSwitch database server\n"
846 "usage: %s [OPTIONS] DATABASE\n"
847 "where DATABASE is a database file in ovsdb format.\n",
848 program_name, program_name);
849 printf("\nJSON-RPC options (may be specified any number of times):\n"
850 " --remote=REMOTE connect or listen to REMOTE\n");
851 stream_usage("JSON-RPC", true, true, true);
854 printf("\nOther options:\n"
855 " --run COMMAND run COMMAND as subprocess then exit\n"
856 " --unixctl=SOCKET override default control socket name\n"
857 " -h, --help display this help message\n"
858 " -V, --version display version information\n");
859 leak_checker_usage();