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 VLOG_INFO("%s (Open vSwitch) %s", program_name, VERSION);
110 error = ovsdb_file_open(file_name, false, &db, &file);
112 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
116 jsonrpc = ovsdb_jsonrpc_server_create(db);
117 reconfigure_from_db(jsonrpc, db, &remotes);
119 retval = unixctl_server_create(unixctl_path, &unixctl);
127 run_argv[0] = "/bin/sh";
129 run_argv[2] = run_command;
132 retval = process_start(run_argv, NULL, 0, NULL, 0, &run_process);
134 ovs_fatal(retval, "%s: process failed to start", run_command);
140 daemonize_complete();
142 unixctl_command_register("exit", "", 0, 0, ovsdb_server_exit, &exiting);
143 unixctl_command_register("ovsdb-server/compact", "", 0, 0,
144 ovsdb_server_compact, file);
145 unixctl_command_register("ovsdb-server/reconnect", "", 0, 0,
146 ovsdb_server_reconnect, jsonrpc);
151 if (memory_should_report()) {
155 ovsdb_jsonrpc_server_get_memory_usage(jsonrpc, &usage);
156 ovsdb_get_memory_usage(db, &usage);
157 memory_report(&usage);
158 simap_destroy(&usage);
161 reconfigure_from_db(jsonrpc, db, &remotes);
162 ovsdb_jsonrpc_server_run(jsonrpc);
163 unixctl_server_run(unixctl);
164 ovsdb_trigger_run(db, time_msec());
165 if (run_process && process_exited(run_process)) {
169 /* update Manager status(es) every 5 seconds */
170 if (time_msec() >= status_timer) {
171 status_timer = time_msec() + 5000;
172 update_remote_status(jsonrpc, &remotes, db);
176 ovsdb_jsonrpc_server_wait(jsonrpc);
177 unixctl_server_wait(unixctl);
178 ovsdb_trigger_wait(db, time_msec());
180 process_wait(run_process);
183 poll_immediate_wake();
185 poll_timer_wait_until(status_timer);
188 ovsdb_jsonrpc_server_destroy(jsonrpc);
190 sset_destroy(&remotes);
191 unixctl_server_destroy(unixctl);
193 if (run_process && process_exited(run_process)) {
194 int status = process_status(run_process);
196 ovs_fatal(0, "%s: child exited, %s",
197 run_command, process_status_msg(status));
205 parse_db_column(const struct ovsdb *db,
207 const struct ovsdb_table **tablep,
208 const struct ovsdb_column **columnp)
210 char *name, *table_name, *column_name;
211 const struct ovsdb_column *column;
212 const struct ovsdb_table *table;
213 char *save_ptr = NULL;
215 name = xstrdup(name_);
216 strtok_r(name, ":", &save_ptr); /* "db:" */
217 table_name = strtok_r(NULL, ",", &save_ptr);
218 column_name = strtok_r(NULL, ",", &save_ptr);
219 if (!table_name || !column_name) {
220 ovs_fatal(0, "\"%s\": invalid syntax", name_);
223 table = ovsdb_get_table(db, table_name);
225 ovs_fatal(0, "\"%s\": no table named %s", name_, table_name);
228 column = ovsdb_table_schema_get_column(table->schema, column_name);
230 ovs_fatal(0, "\"%s\": table \"%s\" has no column \"%s\"",
231 name_, table_name, column_name);
240 parse_db_string_column(const struct ovsdb *db,
242 const struct ovsdb_table **tablep,
243 const struct ovsdb_column **columnp)
245 const struct ovsdb_column *column;
246 const struct ovsdb_table *table;
248 parse_db_column(db, name, &table, &column);
250 if (column->type.key.type != OVSDB_TYPE_STRING
251 || column->type.value.type != OVSDB_TYPE_VOID) {
252 ovs_fatal(0, "\"%s\": table \"%s\" column \"%s\" is "
253 "not string or set of strings",
254 name, table->schema->name, column->name);
261 static OVS_UNUSED const char *
262 query_db_string(const struct ovsdb *db, const char *name)
264 if (!name || strncmp(name, "db:", 3)) {
267 const struct ovsdb_column *column;
268 const struct ovsdb_table *table;
269 const struct ovsdb_row *row;
271 parse_db_string_column(db, name, &table, &column);
273 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
274 const struct ovsdb_datum *datum;
277 datum = &row->fields[column->index];
278 for (i = 0; i < datum->n; i++) {
279 if (datum->keys[i].string[0]) {
280 return datum->keys[i].string;
288 static struct ovsdb_jsonrpc_options *
289 add_remote(struct shash *remotes, const char *target)
291 struct ovsdb_jsonrpc_options *options;
293 options = shash_find_data(remotes, target);
295 options = ovsdb_jsonrpc_default_options(target);
296 shash_add(remotes, target, options);
302 static struct ovsdb_datum *
303 get_datum(struct ovsdb_row *row, const char *column_name,
304 const enum ovsdb_atomic_type key_type,
305 const enum ovsdb_atomic_type value_type,
308 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
309 const struct ovsdb_table_schema *schema = row->table->schema;
310 const struct ovsdb_column *column;
312 column = ovsdb_table_schema_get_column(schema, column_name);
314 VLOG_DBG_RL(&rl, "Table `%s' has no `%s' column",
315 schema->name, column_name);
319 if (column->type.key.type != key_type
320 || column->type.value.type != value_type
321 || column->type.n_max != n_max) {
322 if (!VLOG_DROP_DBG(&rl)) {
323 char *type_name = ovsdb_type_to_english(&column->type);
324 VLOG_DBG("Table `%s' column `%s' has type %s, not expected "
325 "key type %s, value type %s, max elements %zd.",
326 schema->name, column_name, type_name,
327 ovsdb_atomic_type_to_string(key_type),
328 ovsdb_atomic_type_to_string(value_type),
335 return &row->fields[column->index];
338 /* Read string-string key-values from a map. Returns the value associated with
339 * 'key', if found, or NULL */
341 read_map_string_column(const struct ovsdb_row *row, const char *column_name,
344 const struct ovsdb_datum *datum;
345 union ovsdb_atom *atom_key = NULL, *atom_value = NULL;
348 datum = get_datum((struct ovsdb_row *) row, column_name, OVSDB_TYPE_STRING,
349 OVSDB_TYPE_STRING, UINT_MAX);
355 for (i = 0; i < datum->n; i++) {
356 atom_key = &datum->keys[i];
357 if (!strcmp(atom_key->string, key)){
358 atom_value = &datum->values[i];
363 return atom_value ? atom_value->string : NULL;
366 static const union ovsdb_atom *
367 read_column(const struct ovsdb_row *row, const char *column_name,
368 enum ovsdb_atomic_type type)
370 const struct ovsdb_datum *datum;
372 datum = get_datum((struct ovsdb_row *) row, column_name, type, OVSDB_TYPE_VOID,
374 return datum && datum->n ? datum->keys : NULL;
378 read_integer_column(const struct ovsdb_row *row, const char *column_name,
379 long long int *integerp)
381 const union ovsdb_atom *atom;
383 atom = read_column(row, column_name, OVSDB_TYPE_INTEGER);
384 *integerp = atom ? atom->integer : 0;
389 read_string_column(const struct ovsdb_row *row, const char *column_name,
390 const char **stringp)
392 const union ovsdb_atom *atom;
394 atom = read_column(row, column_name, OVSDB_TYPE_STRING);
395 *stringp = atom ? atom->string : NULL;
400 write_bool_column(struct ovsdb_row *row, const char *column_name, bool value)
402 struct ovsdb_datum *datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
408 datum->keys[0].boolean = value;
412 write_string_string_column(struct ovsdb_row *row, const char *column_name,
413 char **keys, char **values, size_t n)
415 const struct ovsdb_column *column;
416 struct ovsdb_datum *datum;
419 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
420 datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
426 /* Free existing data. */
427 ovsdb_datum_destroy(datum, &column->type);
429 /* Allocate space for new values. */
431 datum->keys = xmalloc(n * sizeof *datum->keys);
432 datum->values = xmalloc(n * sizeof *datum->values);
434 for (i = 0; i < n; ++i) {
435 datum->keys[i].string = keys[i];
436 datum->values[i].string = values[i];
439 /* Sort and check constraints. */
440 ovsdb_datum_sort_assert(datum, column->type.key.type);
443 /* Adds a remote and options to 'remotes', based on the Manager table row in
446 add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
448 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
449 struct ovsdb_jsonrpc_options *options;
450 long long int max_backoff, probe_interval;
451 const char *target, *dscp_string;
453 if (!read_string_column(row, "target", &target) || !target) {
454 VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
455 row->table->schema->name);
459 options = add_remote(remotes, target);
460 if (read_integer_column(row, "max_backoff", &max_backoff)) {
461 options->max_backoff = max_backoff;
463 if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
464 options->probe_interval = probe_interval;
467 options->dscp = DSCP_DEFAULT;
468 dscp_string = read_map_string_column(row, "other_config", "dscp");
470 int dscp = atoi(dscp_string);
471 if (dscp >= 0 && dscp <= 63) {
472 options->dscp = dscp;
478 query_db_remotes(const char *name, const struct ovsdb *db,
479 struct shash *remotes)
481 const struct ovsdb_column *column;
482 const struct ovsdb_table *table;
483 const struct ovsdb_row *row;
485 parse_db_column(db, name, &table, &column);
487 if (column->type.key.type == OVSDB_TYPE_STRING
488 && column->type.value.type == OVSDB_TYPE_VOID) {
489 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
490 const struct ovsdb_datum *datum;
493 datum = &row->fields[column->index];
494 for (i = 0; i < datum->n; i++) {
495 add_remote(remotes, datum->keys[i].string);
498 } else if (column->type.key.type == OVSDB_TYPE_UUID
499 && column->type.key.u.uuid.refTable
500 && column->type.value.type == OVSDB_TYPE_VOID) {
501 const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
502 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
503 const struct ovsdb_datum *datum;
506 datum = &row->fields[column->index];
507 for (i = 0; i < datum->n; i++) {
508 const struct ovsdb_row *ref_row;
510 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
512 add_manager_options(remotes, ref_row);
520 update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
521 const struct ovsdb_jsonrpc_server *jsonrpc)
523 struct ovsdb_jsonrpc_remote_status status;
524 struct ovsdb_row *rw_row;
526 char *keys[8], *values[8];
529 /* Get the "target" (protocol/host/port) spec. */
530 if (!read_string_column(row, "target", &target)) {
531 /* Bad remote spec or incorrect schema. */
534 rw_row = ovsdb_txn_row_modify(txn, row);
535 ovsdb_jsonrpc_server_get_remote_status(jsonrpc, target, &status);
537 /* Update status information columns. */
538 write_bool_column(rw_row, "is_connected", status.is_connected);
541 keys[n] = xstrdup("state");
542 values[n++] = xstrdup(status.state);
544 if (status.sec_since_connect != UINT_MAX) {
545 keys[n] = xstrdup("sec_since_connect");
546 values[n++] = xasprintf("%u", status.sec_since_connect);
548 if (status.sec_since_disconnect != UINT_MAX) {
549 keys[n] = xstrdup("sec_since_disconnect");
550 values[n++] = xasprintf("%u", status.sec_since_disconnect);
552 if (status.last_error) {
553 keys[n] = xstrdup("last_error");
555 xstrdup(ovs_retval_to_string(status.last_error));
557 if (status.locks_held && status.locks_held[0]) {
558 keys[n] = xstrdup("locks_held");
559 values[n++] = xstrdup(status.locks_held);
561 if (status.locks_waiting && status.locks_waiting[0]) {
562 keys[n] = xstrdup("locks_waiting");
563 values[n++] = xstrdup(status.locks_waiting);
565 if (status.locks_lost && status.locks_lost[0]) {
566 keys[n] = xstrdup("locks_lost");
567 values[n++] = xstrdup(status.locks_lost);
569 if (status.n_connections > 1) {
570 keys[n] = xstrdup("n_connections");
571 values[n++] = xasprintf("%d", status.n_connections);
573 write_string_string_column(rw_row, "status", keys, values, n);
575 ovsdb_jsonrpc_server_free_remote_status(&status);
579 update_remote_rows(const struct ovsdb *db, struct ovsdb_txn *txn,
580 const char *remote_name,
581 const struct ovsdb_jsonrpc_server *jsonrpc)
583 const struct ovsdb_table *table, *ref_table;
584 const struct ovsdb_column *column;
585 const struct ovsdb_row *row;
587 if (strncmp("db:", remote_name, 3)) {
591 parse_db_column(db, remote_name, &table, &column);
593 if (column->type.key.type != OVSDB_TYPE_UUID
594 || !column->type.key.u.uuid.refTable
595 || column->type.value.type != OVSDB_TYPE_VOID) {
599 ref_table = column->type.key.u.uuid.refTable;
601 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
602 const struct ovsdb_datum *datum;
605 datum = &row->fields[column->index];
606 for (i = 0; i < datum->n; i++) {
607 const struct ovsdb_row *ref_row;
609 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
611 update_remote_row(ref_row, txn, jsonrpc);
618 update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
619 const struct sset *remotes, struct ovsdb *db)
621 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
622 struct ovsdb_txn *txn;
623 const bool durable_txn = false;
624 struct ovsdb_error *error;
627 txn = ovsdb_txn_create(db);
629 /* Iterate over --remote arguments given on command line. */
630 SSET_FOR_EACH (remote, remotes) {
631 update_remote_rows(db, txn, remote, jsonrpc);
634 error = ovsdb_txn_commit(txn, durable_txn);
636 VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
637 ovsdb_error_to_string(error));
641 /* Reconfigures ovsdb-server based on information in the database. */
643 reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
644 const struct ovsdb *db, struct sset *remotes)
646 struct shash resolved_remotes;
649 /* Configure remotes. */
650 shash_init(&resolved_remotes);
651 SSET_FOR_EACH (name, remotes) {
652 if (!strncmp(name, "db:", 3)) {
653 query_db_remotes(name, db, &resolved_remotes);
655 add_remote(&resolved_remotes, name);
658 ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
659 shash_destroy_free_data(&resolved_remotes);
662 stream_ssl_set_key_and_cert(query_db_string(db, private_key_file),
663 query_db_string(db, certificate_file));
664 stream_ssl_set_ca_cert_file(query_db_string(db, ca_cert_file),
669 ovsdb_server_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
670 const char *argv[] OVS_UNUSED,
673 bool *exiting = exiting_;
675 unixctl_command_reply(conn, NULL);
679 ovsdb_server_compact(struct unixctl_conn *conn, int argc OVS_UNUSED,
680 const char *argv[] OVS_UNUSED, void *file_)
682 struct ovsdb_file *file = file_;
683 struct ovsdb_error *error;
685 VLOG_INFO("compacting database by user request");
686 error = ovsdb_file_compact(file);
688 unixctl_command_reply(conn, NULL);
690 char *s = ovsdb_error_to_string(error);
691 ovsdb_error_destroy(error);
692 unixctl_command_reply_error(conn, s);
697 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
698 * connections and reconnect. */
700 ovsdb_server_reconnect(struct unixctl_conn *conn, int argc OVS_UNUSED,
701 const char *argv[] OVS_UNUSED, void *jsonrpc_)
703 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
705 ovsdb_jsonrpc_server_reconnect(jsonrpc);
706 unixctl_command_reply(conn, NULL);
710 parse_options(int argc, char *argv[], char **file_namep,
711 struct sset *remotes, char **unixctl_pathp,
715 OPT_DUMMY = UCHAR_MAX + 1,
719 OPT_BOOTSTRAP_CA_CERT,
721 LEAK_CHECKER_OPTION_ENUMS,
724 static struct option long_options[] = {
725 {"remote", required_argument, NULL, OPT_REMOTE},
726 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
727 {"run", required_argument, NULL, OPT_RUN},
728 {"help", no_argument, NULL, 'h'},
729 {"version", no_argument, NULL, 'V'},
732 LEAK_CHECKER_LONG_OPTIONS,
733 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
734 {"private-key", required_argument, NULL, 'p'},
735 {"certificate", required_argument, NULL, 'c'},
736 {"ca-cert", required_argument, NULL, 'C'},
739 char *short_options = long_options_to_short_options(long_options);
745 c = getopt_long(argc, argv, short_options, long_options, NULL);
752 sset_add(remotes, optarg);
756 *unixctl_pathp = optarg;
760 *run_command = optarg;
767 ovs_print_version(0, 0);
771 DAEMON_OPTION_HANDLERS
772 LEAK_CHECKER_OPTION_HANDLERS
775 private_key_file = optarg;
779 certificate_file = optarg;
783 ca_cert_file = optarg;
784 bootstrap_ca_cert = false;
787 case OPT_BOOTSTRAP_CA_CERT:
788 ca_cert_file = optarg;
789 bootstrap_ca_cert = true;
806 *file_namep = xasprintf("%s/openvswitch/conf.db", ovs_sysconfdir());
810 *file_namep = xstrdup(argv[0]);
814 ovs_fatal(0, "database file is only non-option argument; "
815 "use --help for usage");
822 printf("%s: Open vSwitch database server\n"
823 "usage: %s [OPTIONS] DATABASE\n"
824 "where DATABASE is a database file in ovsdb format.\n",
825 program_name, program_name);
826 printf("\nJSON-RPC options (may be specified any number of times):\n"
827 " --remote=REMOTE connect or listen to REMOTE\n");
828 stream_usage("JSON-RPC", true, true, true);
831 printf("\nOther options:\n"
832 " --run COMMAND run COMMAND as subprocess then exit\n"
833 " --unixctl=SOCKET override default control socket name\n"
834 " -h, --help display this help message\n"
835 " -V, --version display version information\n");
836 leak_checker_usage();