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 struct ovsdb_datum *datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
413 datum->keys[0].boolean = value;
417 write_string_string_column(struct ovsdb_row *row, const char *column_name,
418 char **keys, char **values, size_t n)
420 const struct ovsdb_column *column;
421 struct ovsdb_datum *datum;
424 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
425 datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
431 /* Free existing data. */
432 ovsdb_datum_destroy(datum, &column->type);
434 /* Allocate space for new values. */
436 datum->keys = xmalloc(n * sizeof *datum->keys);
437 datum->values = xmalloc(n * sizeof *datum->values);
439 for (i = 0; i < n; ++i) {
440 datum->keys[i].string = keys[i];
441 datum->values[i].string = values[i];
444 /* Sort and check constraints. */
445 ovsdb_datum_sort_assert(datum, column->type.key.type);
448 /* Adds a remote and options to 'remotes', based on the Manager table row in
451 add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
453 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
454 struct ovsdb_jsonrpc_options *options;
455 long long int max_backoff, probe_interval;
456 const char *target, *dscp_string;
458 if (!read_string_column(row, "target", &target) || !target) {
459 VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
460 row->table->schema->name);
464 options = add_remote(remotes, target);
465 if (read_integer_column(row, "max_backoff", &max_backoff)) {
466 options->max_backoff = max_backoff;
468 if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
469 options->probe_interval = probe_interval;
472 options->dscp = DSCP_DEFAULT;
473 dscp_string = read_map_string_column(row, "other_config", "dscp");
475 int dscp = atoi(dscp_string);
476 if (dscp >= 0 && dscp <= 63) {
477 options->dscp = dscp;
483 query_db_remotes(const char *name, const struct ovsdb *db,
484 struct shash *remotes)
486 const struct ovsdb_column *column;
487 const struct ovsdb_table *table;
488 const struct ovsdb_row *row;
490 parse_db_column(db, name, &table, &column);
492 if (column->type.key.type == OVSDB_TYPE_STRING
493 && column->type.value.type == OVSDB_TYPE_VOID) {
494 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
495 const struct ovsdb_datum *datum;
498 datum = &row->fields[column->index];
499 for (i = 0; i < datum->n; i++) {
500 add_remote(remotes, datum->keys[i].string);
503 } else if (column->type.key.type == OVSDB_TYPE_UUID
504 && column->type.key.u.uuid.refTable
505 && column->type.value.type == OVSDB_TYPE_VOID) {
506 const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
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 const struct ovsdb_row *ref_row;
515 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
517 add_manager_options(remotes, ref_row);
525 update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
526 const struct ovsdb_jsonrpc_server *jsonrpc)
528 struct ovsdb_jsonrpc_remote_status status;
529 struct ovsdb_row *rw_row;
531 char *keys[8], *values[8];
534 /* Get the "target" (protocol/host/port) spec. */
535 if (!read_string_column(row, "target", &target)) {
536 /* Bad remote spec or incorrect schema. */
539 rw_row = ovsdb_txn_row_modify(txn, row);
540 ovsdb_jsonrpc_server_get_remote_status(jsonrpc, target, &status);
542 /* Update status information columns. */
543 write_bool_column(rw_row, "is_connected", status.is_connected);
546 keys[n] = xstrdup("state");
547 values[n++] = xstrdup(status.state);
549 if (status.sec_since_connect != UINT_MAX) {
550 keys[n] = xstrdup("sec_since_connect");
551 values[n++] = xasprintf("%u", status.sec_since_connect);
553 if (status.sec_since_disconnect != UINT_MAX) {
554 keys[n] = xstrdup("sec_since_disconnect");
555 values[n++] = xasprintf("%u", status.sec_since_disconnect);
557 if (status.last_error) {
558 keys[n] = xstrdup("last_error");
560 xstrdup(ovs_retval_to_string(status.last_error));
562 if (status.locks_held && status.locks_held[0]) {
563 keys[n] = xstrdup("locks_held");
564 values[n++] = xstrdup(status.locks_held);
566 if (status.locks_waiting && status.locks_waiting[0]) {
567 keys[n] = xstrdup("locks_waiting");
568 values[n++] = xstrdup(status.locks_waiting);
570 if (status.locks_lost && status.locks_lost[0]) {
571 keys[n] = xstrdup("locks_lost");
572 values[n++] = xstrdup(status.locks_lost);
574 if (status.n_connections > 1) {
575 keys[n] = xstrdup("n_connections");
576 values[n++] = xasprintf("%d", status.n_connections);
578 write_string_string_column(rw_row, "status", keys, values, n);
580 ovsdb_jsonrpc_server_free_remote_status(&status);
584 update_remote_rows(const struct ovsdb *db, struct ovsdb_txn *txn,
585 const char *remote_name,
586 const struct ovsdb_jsonrpc_server *jsonrpc)
588 const struct ovsdb_table *table, *ref_table;
589 const struct ovsdb_column *column;
590 const struct ovsdb_row *row;
592 if (strncmp("db:", remote_name, 3)) {
596 parse_db_column(db, remote_name, &table, &column);
598 if (column->type.key.type != OVSDB_TYPE_UUID
599 || !column->type.key.u.uuid.refTable
600 || column->type.value.type != OVSDB_TYPE_VOID) {
604 ref_table = column->type.key.u.uuid.refTable;
606 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
607 const struct ovsdb_datum *datum;
610 datum = &row->fields[column->index];
611 for (i = 0; i < datum->n; i++) {
612 const struct ovsdb_row *ref_row;
614 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
616 update_remote_row(ref_row, txn, jsonrpc);
623 update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
624 const struct sset *remotes, struct ovsdb *db)
626 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
627 struct ovsdb_txn *txn;
628 const bool durable_txn = false;
629 struct ovsdb_error *error;
632 txn = ovsdb_txn_create(db);
634 /* Iterate over --remote arguments given on command line. */
635 SSET_FOR_EACH (remote, remotes) {
636 update_remote_rows(db, txn, remote, jsonrpc);
639 error = ovsdb_txn_commit(txn, durable_txn);
641 VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
642 ovsdb_error_to_string(error));
646 /* Reconfigures ovsdb-server based on information in the database. */
648 reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
649 const struct ovsdb *db, struct sset *remotes)
651 struct shash resolved_remotes;
654 /* Configure remotes. */
655 shash_init(&resolved_remotes);
656 SSET_FOR_EACH (name, remotes) {
657 if (!strncmp(name, "db:", 3)) {
658 query_db_remotes(name, db, &resolved_remotes);
660 add_remote(&resolved_remotes, name);
663 ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
664 shash_destroy_free_data(&resolved_remotes);
667 stream_ssl_set_key_and_cert(query_db_string(db, private_key_file),
668 query_db_string(db, certificate_file));
669 stream_ssl_set_ca_cert_file(query_db_string(db, ca_cert_file),
674 ovsdb_server_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
675 const char *argv[] OVS_UNUSED,
678 bool *exiting = exiting_;
680 unixctl_command_reply(conn, NULL);
684 ovsdb_server_compact(struct unixctl_conn *conn, int argc OVS_UNUSED,
685 const char *argv[] OVS_UNUSED, void *file_)
687 struct ovsdb_file *file = file_;
688 struct ovsdb_error *error;
690 VLOG_INFO("compacting database by user request");
691 error = ovsdb_file_compact(file);
693 unixctl_command_reply(conn, NULL);
695 char *s = ovsdb_error_to_string(error);
696 ovsdb_error_destroy(error);
697 unixctl_command_reply_error(conn, s);
702 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
703 * connections and reconnect. */
705 ovsdb_server_reconnect(struct unixctl_conn *conn, int argc OVS_UNUSED,
706 const char *argv[] OVS_UNUSED, void *jsonrpc_)
708 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
710 ovsdb_jsonrpc_server_reconnect(jsonrpc);
711 unixctl_command_reply(conn, NULL);
715 parse_options(int argc, char *argv[], char **file_namep,
716 struct sset *remotes, char **unixctl_pathp,
720 OPT_DUMMY = UCHAR_MAX + 1,
724 OPT_BOOTSTRAP_CA_CERT,
726 LEAK_CHECKER_OPTION_ENUMS,
729 static struct option long_options[] = {
730 {"remote", required_argument, NULL, OPT_REMOTE},
731 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
732 {"run", required_argument, NULL, OPT_RUN},
733 {"help", no_argument, NULL, 'h'},
734 {"version", no_argument, NULL, 'V'},
737 LEAK_CHECKER_LONG_OPTIONS,
738 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
739 {"private-key", required_argument, NULL, 'p'},
740 {"certificate", required_argument, NULL, 'c'},
741 {"ca-cert", required_argument, NULL, 'C'},
744 char *short_options = long_options_to_short_options(long_options);
750 c = getopt_long(argc, argv, short_options, long_options, NULL);
757 sset_add(remotes, optarg);
761 *unixctl_pathp = optarg;
765 *run_command = optarg;
772 ovs_print_version(0, 0);
776 DAEMON_OPTION_HANDLERS
777 LEAK_CHECKER_OPTION_HANDLERS
780 private_key_file = optarg;
784 certificate_file = optarg;
788 ca_cert_file = optarg;
789 bootstrap_ca_cert = false;
792 case OPT_BOOTSTRAP_CA_CERT:
793 ca_cert_file = optarg;
794 bootstrap_ca_cert = true;
811 *file_namep = xasprintf("%s/conf.db", ovs_dbdir());
815 *file_namep = xstrdup(argv[0]);
819 ovs_fatal(0, "database file is only non-option argument; "
820 "use --help for usage");
827 printf("%s: Open vSwitch database server\n"
828 "usage: %s [OPTIONS] DATABASE\n"
829 "where DATABASE is a database file in ovsdb format.\n",
830 program_name, program_name);
831 printf("\nJSON-RPC options (may be specified any number of times):\n"
832 " --remote=REMOTE connect or listen to REMOTE\n");
833 stream_usage("JSON-RPC", true, true, true);
836 printf("\nOther options:\n"
837 " --run COMMAND run COMMAND as subprocess then exit\n"
838 " --unixctl=SOCKET override default control socket name\n"
839 " -h, --help display this help message\n"
840 " -V, --version display version information\n");
841 leak_checker_usage();