1 /* Copyright (c) 2009, 2010, 2011 Nicira Networks
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.
26 #include "command-line.h"
31 #include "jsonrpc-server.h"
32 #include "leak-checker.h"
34 #include "ovsdb-data.h"
35 #include "ovsdb-types.h"
36 #include "ovsdb-error.h"
37 #include "poll-loop.h"
40 #include "stream-ssl.h"
46 #include "transaction.h"
52 VLOG_DEFINE_THIS_MODULE(ovsdb_server);
55 /* SSL configuration. */
56 static char *private_key_file;
57 static char *certificate_file;
58 static char *ca_cert_file;
59 static bool bootstrap_ca_cert;
62 static unixctl_cb_func ovsdb_server_exit;
63 static unixctl_cb_func ovsdb_server_compact;
64 static unixctl_cb_func ovsdb_server_reconnect;
66 static void parse_options(int argc, char *argv[], char **file_namep,
67 struct sset *remotes, char **unixctl_pathp,
69 static void usage(void) NO_RETURN;
71 static void reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
72 const struct ovsdb *db, struct sset *remotes);
74 static void update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
75 const struct sset *remotes,
79 main(int argc, char *argv[])
81 char *unixctl_path = NULL;
82 char *run_command = NULL;
83 struct unixctl_server *unixctl;
84 struct ovsdb_jsonrpc_server *jsonrpc;
86 struct ovsdb_error *error;
87 struct ovsdb_file *file;
89 struct process *run_process;
93 long long int status_timer = LLONG_MIN;
95 proctitle_init(argc, argv);
96 set_program_name(argv[0]);
97 stress_init_command();
98 signal(SIGPIPE, SIG_IGN);
101 parse_options(argc, argv, &file_name, &remotes, &unixctl_path,
106 error = ovsdb_file_open(file_name, false, &db, &file);
108 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
111 jsonrpc = ovsdb_jsonrpc_server_create(db);
112 reconfigure_from_db(jsonrpc, db, &remotes);
114 retval = unixctl_server_create(unixctl_path, &unixctl);
122 run_argv[0] = "/bin/sh";
124 run_argv[2] = run_command;
127 retval = process_start(run_argv, NULL, 0, NULL, 0, &run_process);
129 ovs_fatal(retval, "%s: process failed to start", run_command);
135 daemonize_complete();
137 unixctl_command_register("exit", ovsdb_server_exit, &exiting);
138 unixctl_command_register("ovsdb-server/compact", ovsdb_server_compact,
140 unixctl_command_register("ovsdb-server/reconnect", ovsdb_server_reconnect,
145 reconfigure_from_db(jsonrpc, db, &remotes);
146 ovsdb_jsonrpc_server_run(jsonrpc);
147 unixctl_server_run(unixctl);
148 ovsdb_trigger_run(db, time_msec());
149 if (run_process && process_exited(run_process)) {
153 /* update Manager status(es) every 5 seconds */
154 if (time_msec() >= status_timer) {
155 status_timer = time_msec() + 5000;
156 update_remote_status(jsonrpc, &remotes, db);
159 ovsdb_jsonrpc_server_wait(jsonrpc);
160 unixctl_server_wait(unixctl);
161 ovsdb_trigger_wait(db, time_msec());
163 process_wait(run_process);
166 poll_immediate_wake();
168 poll_timer_wait_until(status_timer);
171 ovsdb_jsonrpc_server_destroy(jsonrpc);
173 sset_destroy(&remotes);
174 unixctl_server_destroy(unixctl);
176 if (run_process && process_exited(run_process)) {
177 int status = process_status(run_process);
179 ovs_fatal(0, "%s: child exited, %s",
180 run_command, process_status_msg(status));
188 parse_db_column(const struct ovsdb *db,
190 const struct ovsdb_table **tablep,
191 const struct ovsdb_column **columnp)
193 char *name, *table_name, *column_name;
194 const struct ovsdb_column *column;
195 const struct ovsdb_table *table;
196 char *save_ptr = NULL;
198 name = xstrdup(name_);
199 strtok_r(name, ":", &save_ptr); /* "db:" */
200 table_name = strtok_r(NULL, ",", &save_ptr);
201 column_name = strtok_r(NULL, ",", &save_ptr);
202 if (!table_name || !column_name) {
203 ovs_fatal(0, "\"%s\": invalid syntax", name_);
206 table = ovsdb_get_table(db, table_name);
208 ovs_fatal(0, "\"%s\": no table named %s", name_, table_name);
211 column = ovsdb_table_schema_get_column(table->schema, column_name);
213 ovs_fatal(0, "\"%s\": table \"%s\" has no column \"%s\"",
214 name_, table_name, column_name);
223 parse_db_string_column(const struct ovsdb *db,
225 const struct ovsdb_table **tablep,
226 const struct ovsdb_column **columnp)
228 const struct ovsdb_column *column;
229 const struct ovsdb_table *table;
231 parse_db_column(db, name, &table, &column);
233 if (column->type.key.type != OVSDB_TYPE_STRING
234 || column->type.value.type != OVSDB_TYPE_VOID) {
235 ovs_fatal(0, "\"%s\": table \"%s\" column \"%s\" is "
236 "not string or set of strings",
237 name, table->schema->name, column->name);
244 static OVS_UNUSED const char *
245 query_db_string(const struct ovsdb *db, const char *name)
247 if (!name || strncmp(name, "db:", 3)) {
250 const struct ovsdb_column *column;
251 const struct ovsdb_table *table;
252 const struct ovsdb_row *row;
254 parse_db_string_column(db, name, &table, &column);
256 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
257 const struct ovsdb_datum *datum;
260 datum = &row->fields[column->index];
261 for (i = 0; i < datum->n; i++) {
262 if (datum->keys[i].string[0]) {
263 return datum->keys[i].string;
271 static struct ovsdb_jsonrpc_options *
272 add_remote(struct shash *remotes, const char *target)
274 struct ovsdb_jsonrpc_options *options;
276 options = shash_find_data(remotes, target);
278 options = ovsdb_jsonrpc_default_options();
279 shash_add(remotes, target, options);
285 static struct ovsdb_datum *
286 get_datum(struct ovsdb_row *row, const char *column_name,
287 const enum ovsdb_atomic_type key_type,
288 const enum ovsdb_atomic_type value_type,
291 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
292 const struct ovsdb_table_schema *schema = row->table->schema;
293 const struct ovsdb_column *column;
295 column = ovsdb_table_schema_get_column(schema, column_name);
297 VLOG_DBG_RL(&rl, "Table `%s' has no `%s' column",
298 schema->name, column_name);
302 if (column->type.key.type != key_type
303 || column->type.value.type != value_type
304 || column->type.n_max != n_max) {
305 if (!VLOG_DROP_DBG(&rl)) {
306 char *type_name = ovsdb_type_to_english(&column->type);
307 VLOG_DBG("Table `%s' column `%s' has type %s, not expected "
308 "key type %s, value type %s, max elements %zd.",
309 schema->name, column_name, type_name,
310 ovsdb_atomic_type_to_string(key_type),
311 ovsdb_atomic_type_to_string(value_type),
318 return &row->fields[column->index];
321 static const union ovsdb_atom *
322 read_column(const struct ovsdb_row *row, const char *column_name,
323 enum ovsdb_atomic_type type)
325 const struct ovsdb_datum *datum;
327 datum = get_datum((struct ovsdb_row *) row, column_name, type, OVSDB_TYPE_VOID, 1);
328 return datum && datum->n ? datum->keys : NULL;
332 read_integer_column(const struct ovsdb_row *row, const char *column_name,
333 long long int *integerp)
335 const union ovsdb_atom *atom;
337 atom = read_column(row, column_name, OVSDB_TYPE_INTEGER);
338 *integerp = atom ? atom->integer : 0;
343 read_string_column(const struct ovsdb_row *row, const char *column_name,
344 const char **stringp)
346 const union ovsdb_atom *atom;
348 atom = read_column(row, column_name, OVSDB_TYPE_STRING);
349 *stringp = atom ? atom->string : 0;
354 write_bool_column(struct ovsdb_row *row, const char *column_name, bool value)
356 struct ovsdb_datum *datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
362 datum->keys[0].boolean = value;
366 write_string_string_column(struct ovsdb_row *row, const char *column_name,
367 char **keys, char **values, size_t n)
369 const struct ovsdb_column *column;
370 struct ovsdb_datum *datum;
373 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
374 datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
380 /* Free existing data. */
381 ovsdb_datum_destroy(datum, &column->type);
383 /* Allocate space for new values. */
385 datum->keys = xmalloc(n * sizeof *datum->keys);
386 datum->values = xmalloc(n * sizeof *datum->values);
388 for (i = 0; i < n; ++i) {
389 datum->keys[i].string = keys[i];
390 datum->values[i].string = values[i];
393 /* Sort and check constraints. */
394 ovsdb_datum_sort_assert(datum, column->type.key.type);
397 /* Adds a remote and options to 'remotes', based on the Manager table row in
400 add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
402 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
403 struct ovsdb_jsonrpc_options *options;
404 long long int max_backoff, probe_interval;
407 if (!read_string_column(row, "target", &target) || !target) {
408 VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
409 row->table->schema->name);
413 options = add_remote(remotes, target);
414 if (read_integer_column(row, "max_backoff", &max_backoff)) {
415 options->max_backoff = max_backoff;
417 if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
418 options->probe_interval = probe_interval;
423 query_db_remotes(const char *name, const struct ovsdb *db,
424 struct shash *remotes)
426 const struct ovsdb_column *column;
427 const struct ovsdb_table *table;
428 const struct ovsdb_row *row;
430 parse_db_column(db, name, &table, &column);
432 if (column->type.key.type == OVSDB_TYPE_STRING
433 && column->type.value.type == OVSDB_TYPE_VOID) {
434 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
435 const struct ovsdb_datum *datum;
438 datum = &row->fields[column->index];
439 for (i = 0; i < datum->n; i++) {
440 add_remote(remotes, datum->keys[i].string);
443 } else if (column->type.key.type == OVSDB_TYPE_UUID
444 && column->type.key.u.uuid.refTable
445 && column->type.value.type == OVSDB_TYPE_VOID) {
446 const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
447 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
448 const struct ovsdb_datum *datum;
451 datum = &row->fields[column->index];
452 for (i = 0; i < datum->n; i++) {
453 const struct ovsdb_row *ref_row;
455 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
457 add_manager_options(remotes, ref_row);
465 update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
466 const struct shash *statuses)
468 struct ovsdb_row *rw_row;
470 const struct ovsdb_jsonrpc_remote_status *status;
471 char *keys[4], *values[4];
474 /* Get the "target" (protocol/host/port) spec. */
475 if (!read_string_column(row, "target", &target)) {
476 /* Bad remote spec or incorrect schema. */
480 /* Prepare to modify this row. */
481 rw_row = ovsdb_txn_row_modify(txn, row);
483 /* Find status information for this target. */
484 status = shash_find_data(statuses, target);
486 /* Should never happen, but just in case... */
490 /* Update status information columns. */
492 write_bool_column(rw_row, "is_connected",
493 status->is_connected);
495 keys[n] = xstrdup("state");
496 values[n++] = xstrdup(status->state);
497 if (status->sec_since_connect != UINT_MAX) {
498 keys[n] = xstrdup("sec_since_connect");
499 values[n++] = xasprintf("%u", status->sec_since_connect);
501 if (status->sec_since_disconnect != UINT_MAX) {
502 keys[n] = xstrdup("sec_since_disconnect");
503 values[n++] = xasprintf("%u", status->sec_since_disconnect);
505 if (status->last_error) {
506 keys[n] = xstrdup("last_error");
508 xstrdup(ovs_retval_to_string(status->last_error));
510 write_string_string_column(rw_row, "status", keys, values, n);
514 update_remote_rows(const struct ovsdb *db, struct ovsdb_txn *txn,
515 const char *remote_name, const struct shash *statuses)
517 const struct ovsdb_table *table, *ref_table;
518 const struct ovsdb_column *column;
519 const struct ovsdb_row *row;
521 if (strncmp("db:", remote_name, 3)) {
525 parse_db_column(db, remote_name, &table, &column);
527 if (column->type.key.type != OVSDB_TYPE_UUID
528 || !column->type.key.u.uuid.refTable
529 || column->type.value.type != OVSDB_TYPE_VOID) {
533 ref_table = column->type.key.u.uuid.refTable;
535 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
536 const struct ovsdb_datum *datum;
539 datum = &row->fields[column->index];
540 for (i = 0; i < datum->n; i++) {
541 const struct ovsdb_row *ref_row;
543 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
545 update_remote_row(ref_row, txn, statuses);
552 update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
553 const struct sset *remotes, struct ovsdb *db)
555 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
556 struct shash statuses;
557 struct ovsdb_txn *txn;
558 const bool durable_txn = false;
559 struct ovsdb_error *error;
562 /* Get status of current connections. */
563 ovsdb_jsonrpc_server_get_remote_status(jsonrpc, &statuses);
565 txn = ovsdb_txn_create(db);
567 /* Iterate over --remote arguments given on command line. */
568 SSET_FOR_EACH (remote, remotes) {
569 update_remote_rows(db, txn, remote, &statuses);
572 error = ovsdb_txn_commit(txn, durable_txn);
574 VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
575 ovsdb_error_to_string(error));
578 shash_destroy_free_data(&statuses);
581 /* Reconfigures ovsdb-server based on information in the database. */
583 reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
584 const struct ovsdb *db, struct sset *remotes)
586 struct shash resolved_remotes;
589 /* Configure remotes. */
590 shash_init(&resolved_remotes);
591 SSET_FOR_EACH (name, remotes) {
592 if (!strncmp(name, "db:", 3)) {
593 query_db_remotes(name, db, &resolved_remotes);
595 add_remote(&resolved_remotes, name);
598 ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
599 shash_destroy_free_data(&resolved_remotes);
603 stream_ssl_set_key_and_cert(query_db_string(db, private_key_file),
604 query_db_string(db, certificate_file));
605 stream_ssl_set_ca_cert_file(query_db_string(db, ca_cert_file),
611 ovsdb_server_exit(struct unixctl_conn *conn, const char *args OVS_UNUSED,
614 bool *exiting = exiting_;
616 unixctl_command_reply(conn, 200, NULL);
620 ovsdb_server_compact(struct unixctl_conn *conn, const char *args OVS_UNUSED,
623 struct ovsdb_file *file = file_;
624 struct ovsdb_error *error;
626 VLOG_INFO("compacting database by user request");
627 error = ovsdb_file_compact(file);
629 unixctl_command_reply(conn, 200, NULL);
631 char *s = ovsdb_error_to_string(error);
632 ovsdb_error_destroy(error);
633 unixctl_command_reply(conn, 503, s);
638 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
639 * connections and reconnect. */
641 ovsdb_server_reconnect(struct unixctl_conn *conn, const char *args OVS_UNUSED,
644 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
646 ovsdb_jsonrpc_server_reconnect(jsonrpc);
647 unixctl_command_reply(conn, 200, NULL);
651 parse_options(int argc, char *argv[], char **file_namep,
652 struct sset *remotes, char **unixctl_pathp,
656 OPT_DUMMY = UCHAR_MAX + 1,
660 OPT_BOOTSTRAP_CA_CERT,
662 LEAK_CHECKER_OPTION_ENUMS,
665 static struct option long_options[] = {
666 {"remote", required_argument, 0, OPT_REMOTE},
667 {"unixctl", required_argument, 0, OPT_UNIXCTL},
668 {"run", required_argument, 0, OPT_RUN},
669 {"help", no_argument, 0, 'h'},
670 {"version", no_argument, 0, 'V'},
673 LEAK_CHECKER_LONG_OPTIONS,
675 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
676 {"private-key", required_argument, 0, 'p'},
677 {"certificate", required_argument, 0, 'c'},
678 {"ca-cert", required_argument, 0, 'C'},
682 char *short_options = long_options_to_short_options(long_options);
688 c = getopt_long(argc, argv, short_options, long_options, NULL);
695 sset_add(remotes, optarg);
699 *unixctl_pathp = optarg;
703 *run_command = optarg;
710 OVS_PRINT_VERSION(0, 0);
714 DAEMON_OPTION_HANDLERS
715 LEAK_CHECKER_OPTION_HANDLERS
719 private_key_file = optarg;
723 certificate_file = optarg;
727 ca_cert_file = optarg;
728 bootstrap_ca_cert = false;
731 case OPT_BOOTSTRAP_CA_CERT:
732 ca_cert_file = optarg;
733 bootstrap_ca_cert = true;
750 ovs_fatal(0, "database file is only non-option argument; "
751 "use --help for usage");
752 } else if (argc < 1) {
753 ovs_fatal(0, "missing database file argument; use --help for usage");
756 *file_namep = argv[0];
762 printf("%s: Open vSwitch database server\n"
763 "usage: %s [OPTIONS] DATABASE\n"
764 "where DATABASE is a database file in ovsdb format.\n",
765 program_name, program_name);
766 printf("\nJSON-RPC options (may be specified any number of times):\n"
767 " --remote=REMOTE connect or listen to REMOTE\n");
768 stream_usage("JSON-RPC", true, true, true);
771 printf("\nOther options:\n"
772 " --run COMMAND run COMMAND as subprocess then exit\n"
773 " --unixctl=SOCKET override default control socket name\n"
774 " -h, --help display this help message\n"
775 " -V, --version display version information\n");
776 leak_checker_usage();