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);
54 /* SSL configuration. */
55 static char *private_key_file;
56 static char *certificate_file;
57 static char *ca_cert_file;
58 static bool bootstrap_ca_cert;
60 static unixctl_cb_func ovsdb_server_exit;
61 static unixctl_cb_func ovsdb_server_compact;
62 static unixctl_cb_func ovsdb_server_reconnect;
64 static void parse_options(int argc, char *argv[], char **file_namep,
65 struct sset *remotes, char **unixctl_pathp,
67 static void usage(void) NO_RETURN;
69 static void reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
70 const struct ovsdb *db, struct sset *remotes);
72 static void update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
73 const struct sset *remotes,
77 main(int argc, char *argv[])
79 char *unixctl_path = NULL;
80 char *run_command = NULL;
81 struct unixctl_server *unixctl;
82 struct ovsdb_jsonrpc_server *jsonrpc;
84 struct ovsdb_error *error;
85 struct ovsdb_file *file;
87 struct process *run_process;
91 long long int status_timer = LLONG_MIN;
93 proctitle_init(argc, argv);
94 set_program_name(argv[0]);
95 stress_init_command();
96 signal(SIGPIPE, SIG_IGN);
99 parse_options(argc, argv, &file_name, &remotes, &unixctl_path,
104 error = ovsdb_file_open(file_name, false, &db, &file);
106 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
109 jsonrpc = ovsdb_jsonrpc_server_create(db);
110 reconfigure_from_db(jsonrpc, db, &remotes);
112 retval = unixctl_server_create(unixctl_path, &unixctl);
120 run_argv[0] = "/bin/sh";
122 run_argv[2] = run_command;
125 retval = process_start(run_argv, NULL, 0, NULL, 0, &run_process);
127 ovs_fatal(retval, "%s: process failed to start", run_command);
133 daemonize_complete();
135 unixctl_command_register("exit", ovsdb_server_exit, &exiting);
136 unixctl_command_register("ovsdb-server/compact", ovsdb_server_compact,
138 unixctl_command_register("ovsdb-server/reconnect", ovsdb_server_reconnect,
143 reconfigure_from_db(jsonrpc, db, &remotes);
144 ovsdb_jsonrpc_server_run(jsonrpc);
145 unixctl_server_run(unixctl);
146 ovsdb_trigger_run(db, time_msec());
147 if (run_process && process_exited(run_process)) {
151 /* update Manager status(es) every 5 seconds */
152 if (time_msec() >= status_timer) {
153 status_timer = time_msec() + 5000;
154 update_remote_status(jsonrpc, &remotes, db);
157 ovsdb_jsonrpc_server_wait(jsonrpc);
158 unixctl_server_wait(unixctl);
159 ovsdb_trigger_wait(db, time_msec());
161 process_wait(run_process);
164 poll_immediate_wake();
166 poll_timer_wait_until(status_timer);
169 ovsdb_jsonrpc_server_destroy(jsonrpc);
171 sset_destroy(&remotes);
172 unixctl_server_destroy(unixctl);
174 if (run_process && process_exited(run_process)) {
175 int status = process_status(run_process);
177 ovs_fatal(0, "%s: child exited, %s",
178 run_command, process_status_msg(status));
186 parse_db_column(const struct ovsdb *db,
188 const struct ovsdb_table **tablep,
189 const struct ovsdb_column **columnp)
191 char *name, *table_name, *column_name;
192 const struct ovsdb_column *column;
193 const struct ovsdb_table *table;
194 char *save_ptr = NULL;
196 name = xstrdup(name_);
197 strtok_r(name, ":", &save_ptr); /* "db:" */
198 table_name = strtok_r(NULL, ",", &save_ptr);
199 column_name = strtok_r(NULL, ",", &save_ptr);
200 if (!table_name || !column_name) {
201 ovs_fatal(0, "\"%s\": invalid syntax", name_);
204 table = ovsdb_get_table(db, table_name);
206 ovs_fatal(0, "\"%s\": no table named %s", name_, table_name);
209 column = ovsdb_table_schema_get_column(table->schema, column_name);
211 ovs_fatal(0, "\"%s\": table \"%s\" has no column \"%s\"",
212 name_, table_name, column_name);
221 parse_db_string_column(const struct ovsdb *db,
223 const struct ovsdb_table **tablep,
224 const struct ovsdb_column **columnp)
226 const struct ovsdb_column *column;
227 const struct ovsdb_table *table;
229 parse_db_column(db, name, &table, &column);
231 if (column->type.key.type != OVSDB_TYPE_STRING
232 || column->type.value.type != OVSDB_TYPE_VOID) {
233 ovs_fatal(0, "\"%s\": table \"%s\" column \"%s\" is "
234 "not string or set of strings",
235 name, table->schema->name, column->name);
242 static OVS_UNUSED const char *
243 query_db_string(const struct ovsdb *db, const char *name)
245 if (!name || strncmp(name, "db:", 3)) {
248 const struct ovsdb_column *column;
249 const struct ovsdb_table *table;
250 const struct ovsdb_row *row;
252 parse_db_string_column(db, name, &table, &column);
254 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
255 const struct ovsdb_datum *datum;
258 datum = &row->fields[column->index];
259 for (i = 0; i < datum->n; i++) {
260 if (datum->keys[i].string[0]) {
261 return datum->keys[i].string;
269 static struct ovsdb_jsonrpc_options *
270 add_remote(struct shash *remotes, const char *target)
272 struct ovsdb_jsonrpc_options *options;
274 options = shash_find_data(remotes, target);
276 options = ovsdb_jsonrpc_default_options();
277 shash_add(remotes, target, options);
283 static struct ovsdb_datum *
284 get_datum(struct ovsdb_row *row, const char *column_name,
285 const enum ovsdb_atomic_type key_type,
286 const enum ovsdb_atomic_type value_type,
289 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
290 const struct ovsdb_table_schema *schema = row->table->schema;
291 const struct ovsdb_column *column;
293 column = ovsdb_table_schema_get_column(schema, column_name);
295 VLOG_DBG_RL(&rl, "Table `%s' has no `%s' column",
296 schema->name, column_name);
300 if (column->type.key.type != key_type
301 || column->type.value.type != value_type
302 || column->type.n_max != n_max) {
303 if (!VLOG_DROP_DBG(&rl)) {
304 char *type_name = ovsdb_type_to_english(&column->type);
305 VLOG_DBG("Table `%s' column `%s' has type %s, not expected "
306 "key type %s, value type %s, max elements %zd.",
307 schema->name, column_name, type_name,
308 ovsdb_atomic_type_to_string(key_type),
309 ovsdb_atomic_type_to_string(value_type),
316 return &row->fields[column->index];
319 static const union ovsdb_atom *
320 read_column(const struct ovsdb_row *row, const char *column_name,
321 enum ovsdb_atomic_type type)
323 const struct ovsdb_datum *datum;
325 datum = get_datum((struct ovsdb_row *) row, column_name, type, OVSDB_TYPE_VOID, 1);
326 return datum && datum->n ? datum->keys : NULL;
330 read_integer_column(const struct ovsdb_row *row, const char *column_name,
331 long long int *integerp)
333 const union ovsdb_atom *atom;
335 atom = read_column(row, column_name, OVSDB_TYPE_INTEGER);
336 *integerp = atom ? atom->integer : 0;
341 read_string_column(const struct ovsdb_row *row, const char *column_name,
342 const char **stringp)
344 const union ovsdb_atom *atom;
346 atom = read_column(row, column_name, OVSDB_TYPE_STRING);
347 *stringp = atom ? atom->string : NULL;
352 write_bool_column(struct ovsdb_row *row, const char *column_name, bool value)
354 struct ovsdb_datum *datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
360 datum->keys[0].boolean = value;
364 write_string_string_column(struct ovsdb_row *row, const char *column_name,
365 char **keys, char **values, size_t n)
367 const struct ovsdb_column *column;
368 struct ovsdb_datum *datum;
371 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
372 datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
378 /* Free existing data. */
379 ovsdb_datum_destroy(datum, &column->type);
381 /* Allocate space for new values. */
383 datum->keys = xmalloc(n * sizeof *datum->keys);
384 datum->values = xmalloc(n * sizeof *datum->values);
386 for (i = 0; i < n; ++i) {
387 datum->keys[i].string = keys[i];
388 datum->values[i].string = values[i];
391 /* Sort and check constraints. */
392 ovsdb_datum_sort_assert(datum, column->type.key.type);
395 /* Adds a remote and options to 'remotes', based on the Manager table row in
398 add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
400 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
401 struct ovsdb_jsonrpc_options *options;
402 long long int max_backoff, probe_interval;
405 if (!read_string_column(row, "target", &target) || !target) {
406 VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
407 row->table->schema->name);
411 options = add_remote(remotes, target);
412 if (read_integer_column(row, "max_backoff", &max_backoff)) {
413 options->max_backoff = max_backoff;
415 if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
416 options->probe_interval = probe_interval;
421 query_db_remotes(const char *name, const struct ovsdb *db,
422 struct shash *remotes)
424 const struct ovsdb_column *column;
425 const struct ovsdb_table *table;
426 const struct ovsdb_row *row;
428 parse_db_column(db, name, &table, &column);
430 if (column->type.key.type == OVSDB_TYPE_STRING
431 && column->type.value.type == OVSDB_TYPE_VOID) {
432 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
433 const struct ovsdb_datum *datum;
436 datum = &row->fields[column->index];
437 for (i = 0; i < datum->n; i++) {
438 add_remote(remotes, datum->keys[i].string);
441 } else if (column->type.key.type == OVSDB_TYPE_UUID
442 && column->type.key.u.uuid.refTable
443 && column->type.value.type == OVSDB_TYPE_VOID) {
444 const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
445 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
446 const struct ovsdb_datum *datum;
449 datum = &row->fields[column->index];
450 for (i = 0; i < datum->n; i++) {
451 const struct ovsdb_row *ref_row;
453 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
455 add_manager_options(remotes, ref_row);
463 update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
464 const struct shash *statuses)
466 struct ovsdb_row *rw_row;
468 const struct ovsdb_jsonrpc_remote_status *status;
469 char *keys[4], *values[4];
472 /* Get the "target" (protocol/host/port) spec. */
473 if (!read_string_column(row, "target", &target)) {
474 /* Bad remote spec or incorrect schema. */
478 /* Prepare to modify this row. */
479 rw_row = ovsdb_txn_row_modify(txn, row);
481 /* Find status information for this target. */
482 status = shash_find_data(statuses, target);
484 /* Should never happen, but just in case... */
488 /* Update status information columns. */
490 write_bool_column(rw_row, "is_connected",
491 status->is_connected);
493 keys[n] = xstrdup("state");
494 values[n++] = xstrdup(status->state);
495 if (status->sec_since_connect != UINT_MAX) {
496 keys[n] = xstrdup("sec_since_connect");
497 values[n++] = xasprintf("%u", status->sec_since_connect);
499 if (status->sec_since_disconnect != UINT_MAX) {
500 keys[n] = xstrdup("sec_since_disconnect");
501 values[n++] = xasprintf("%u", status->sec_since_disconnect);
503 if (status->last_error) {
504 keys[n] = xstrdup("last_error");
506 xstrdup(ovs_retval_to_string(status->last_error));
508 write_string_string_column(rw_row, "status", keys, values, n);
512 update_remote_rows(const struct ovsdb *db, struct ovsdb_txn *txn,
513 const char *remote_name, const struct shash *statuses)
515 const struct ovsdb_table *table, *ref_table;
516 const struct ovsdb_column *column;
517 const struct ovsdb_row *row;
519 if (strncmp("db:", remote_name, 3)) {
523 parse_db_column(db, remote_name, &table, &column);
525 if (column->type.key.type != OVSDB_TYPE_UUID
526 || !column->type.key.u.uuid.refTable
527 || column->type.value.type != OVSDB_TYPE_VOID) {
531 ref_table = column->type.key.u.uuid.refTable;
533 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
534 const struct ovsdb_datum *datum;
537 datum = &row->fields[column->index];
538 for (i = 0; i < datum->n; i++) {
539 const struct ovsdb_row *ref_row;
541 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
543 update_remote_row(ref_row, txn, statuses);
550 update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
551 const struct sset *remotes, struct ovsdb *db)
553 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
554 struct shash statuses;
555 struct ovsdb_txn *txn;
556 const bool durable_txn = false;
557 struct ovsdb_error *error;
560 /* Get status of current connections. */
561 ovsdb_jsonrpc_server_get_remote_status(jsonrpc, &statuses);
563 txn = ovsdb_txn_create(db);
565 /* Iterate over --remote arguments given on command line. */
566 SSET_FOR_EACH (remote, remotes) {
567 update_remote_rows(db, txn, remote, &statuses);
570 error = ovsdb_txn_commit(txn, durable_txn);
572 VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
573 ovsdb_error_to_string(error));
576 shash_destroy_free_data(&statuses);
579 /* Reconfigures ovsdb-server based on information in the database. */
581 reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
582 const struct ovsdb *db, struct sset *remotes)
584 struct shash resolved_remotes;
587 /* Configure remotes. */
588 shash_init(&resolved_remotes);
589 SSET_FOR_EACH (name, remotes) {
590 if (!strncmp(name, "db:", 3)) {
591 query_db_remotes(name, db, &resolved_remotes);
593 add_remote(&resolved_remotes, name);
596 ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
597 shash_destroy_free_data(&resolved_remotes);
600 stream_ssl_set_key_and_cert(query_db_string(db, private_key_file),
601 query_db_string(db, certificate_file));
602 stream_ssl_set_ca_cert_file(query_db_string(db, ca_cert_file),
607 ovsdb_server_exit(struct unixctl_conn *conn, const char *args OVS_UNUSED,
610 bool *exiting = exiting_;
612 unixctl_command_reply(conn, 200, NULL);
616 ovsdb_server_compact(struct unixctl_conn *conn, const char *args OVS_UNUSED,
619 struct ovsdb_file *file = file_;
620 struct ovsdb_error *error;
622 VLOG_INFO("compacting database by user request");
623 error = ovsdb_file_compact(file);
625 unixctl_command_reply(conn, 200, NULL);
627 char *s = ovsdb_error_to_string(error);
628 ovsdb_error_destroy(error);
629 unixctl_command_reply(conn, 503, s);
634 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
635 * connections and reconnect. */
637 ovsdb_server_reconnect(struct unixctl_conn *conn, const char *args OVS_UNUSED,
640 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
642 ovsdb_jsonrpc_server_reconnect(jsonrpc);
643 unixctl_command_reply(conn, 200, NULL);
647 parse_options(int argc, char *argv[], char **file_namep,
648 struct sset *remotes, char **unixctl_pathp,
652 OPT_DUMMY = UCHAR_MAX + 1,
656 OPT_BOOTSTRAP_CA_CERT,
658 LEAK_CHECKER_OPTION_ENUMS,
661 static struct option long_options[] = {
662 {"remote", required_argument, NULL, OPT_REMOTE},
663 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
664 {"run", required_argument, NULL, OPT_RUN},
665 {"help", no_argument, NULL, 'h'},
666 {"version", no_argument, NULL, 'V'},
669 LEAK_CHECKER_LONG_OPTIONS,
670 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
671 {"private-key", required_argument, NULL, 'p'},
672 {"certificate", required_argument, NULL, 'c'},
673 {"ca-cert", required_argument, NULL, 'C'},
676 char *short_options = long_options_to_short_options(long_options);
682 c = getopt_long(argc, argv, short_options, long_options, NULL);
689 sset_add(remotes, optarg);
693 *unixctl_pathp = optarg;
697 *run_command = optarg;
704 OVS_PRINT_VERSION(0, 0);
708 DAEMON_OPTION_HANDLERS
709 LEAK_CHECKER_OPTION_HANDLERS
712 private_key_file = optarg;
716 certificate_file = optarg;
720 ca_cert_file = optarg;
721 bootstrap_ca_cert = false;
724 case OPT_BOOTSTRAP_CA_CERT:
725 ca_cert_file = optarg;
726 bootstrap_ca_cert = true;
742 ovs_fatal(0, "database file is only non-option argument; "
743 "use --help for usage");
744 } else if (argc < 1) {
745 ovs_fatal(0, "missing database file argument; use --help for usage");
748 *file_namep = argv[0];
754 printf("%s: Open vSwitch database server\n"
755 "usage: %s [OPTIONS] DATABASE\n"
756 "where DATABASE is a database file in ovsdb format.\n",
757 program_name, program_name);
758 printf("\nJSON-RPC options (may be specified any number of times):\n"
759 " --remote=REMOTE connect or listen to REMOTE\n");
760 stream_usage("JSON-RPC", true, true, true);
763 printf("\nOther options:\n"
764 " --run COMMAND run COMMAND as subprocess then exit\n"
765 " --unixctl=SOCKET override default control socket name\n"
766 " -h, --help display this help message\n"
767 " -V, --version display version information\n");
768 leak_checker_usage();