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"
32 #include "jsonrpc-server.h"
33 #include "leak-checker.h"
35 #include "ovsdb-data.h"
36 #include "ovsdb-types.h"
37 #include "ovsdb-error.h"
38 #include "poll-loop.h"
41 #include "stream-ssl.h"
47 #include "transaction.h"
53 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;
61 static unixctl_cb_func ovsdb_server_exit;
62 static unixctl_cb_func ovsdb_server_compact;
63 static unixctl_cb_func ovsdb_server_reconnect;
65 static void parse_options(int argc, char *argv[], char **file_namep,
66 struct sset *remotes, char **unixctl_pathp,
68 static void usage(void) NO_RETURN;
70 static void reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
71 const struct ovsdb *db, struct sset *remotes);
73 static void update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
74 const struct sset *remotes,
78 main(int argc, char *argv[])
80 char *unixctl_path = NULL;
81 char *run_command = NULL;
82 struct unixctl_server *unixctl;
83 struct ovsdb_jsonrpc_server *jsonrpc;
85 struct ovsdb_error *error;
86 struct ovsdb_file *file;
88 struct process *run_process;
92 long long int status_timer = LLONG_MIN;
94 proctitle_init(argc, argv);
95 set_program_name(argv[0]);
96 stress_init_command();
97 signal(SIGPIPE, SIG_IGN);
100 parse_options(argc, argv, &file_name, &remotes, &unixctl_path,
105 error = ovsdb_file_open(file_name, false, &db, &file);
107 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 : NULL;
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 ovsdb_jsonrpc_server *jsonrpc)
468 struct ovsdb_jsonrpc_remote_status status;
469 struct ovsdb_row *rw_row;
471 char *keys[8], *values[8];
474 /* Get the "target" (protocol/host/port) spec. */
475 if (!read_string_column(row, "target", &target)) {
476 /* Bad remote spec or incorrect schema. */
479 rw_row = ovsdb_txn_row_modify(txn, row);
480 ovsdb_jsonrpc_server_get_remote_status(jsonrpc, target, &status);
482 /* Update status information columns. */
483 write_bool_column(rw_row, "is_connected", status.is_connected);
486 keys[n] = xstrdup("state");
487 values[n++] = xstrdup(status.state);
489 if (status.sec_since_connect != UINT_MAX) {
490 keys[n] = xstrdup("sec_since_connect");
491 values[n++] = xasprintf("%u", status.sec_since_connect);
493 if (status.sec_since_disconnect != UINT_MAX) {
494 keys[n] = xstrdup("sec_since_disconnect");
495 values[n++] = xasprintf("%u", status.sec_since_disconnect);
497 if (status.last_error) {
498 keys[n] = xstrdup("last_error");
500 xstrdup(ovs_retval_to_string(status.last_error));
502 if (status.n_connections > 1) {
503 keys[n] = xstrdup("n_connections");
504 values[n++] = xasprintf("%d", status.n_connections);
506 write_string_string_column(rw_row, "status", keys, values, n);
510 update_remote_rows(const struct ovsdb *db, struct ovsdb_txn *txn,
511 const char *remote_name,
512 const struct ovsdb_jsonrpc_server *jsonrpc)
514 const struct ovsdb_table *table, *ref_table;
515 const struct ovsdb_column *column;
516 const struct ovsdb_row *row;
518 if (strncmp("db:", remote_name, 3)) {
522 parse_db_column(db, remote_name, &table, &column);
524 if (column->type.key.type != OVSDB_TYPE_UUID
525 || !column->type.key.u.uuid.refTable
526 || column->type.value.type != OVSDB_TYPE_VOID) {
530 ref_table = column->type.key.u.uuid.refTable;
532 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
533 const struct ovsdb_datum *datum;
536 datum = &row->fields[column->index];
537 for (i = 0; i < datum->n; i++) {
538 const struct ovsdb_row *ref_row;
540 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
542 update_remote_row(ref_row, txn, jsonrpc);
549 update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
550 const struct sset *remotes, struct ovsdb *db)
552 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
553 struct ovsdb_txn *txn;
554 const bool durable_txn = false;
555 struct ovsdb_error *error;
558 txn = ovsdb_txn_create(db);
560 /* Iterate over --remote arguments given on command line. */
561 SSET_FOR_EACH (remote, remotes) {
562 update_remote_rows(db, txn, remote, jsonrpc);
565 error = ovsdb_txn_commit(txn, durable_txn);
567 VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
568 ovsdb_error_to_string(error));
572 /* Reconfigures ovsdb-server based on information in the database. */
574 reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
575 const struct ovsdb *db, struct sset *remotes)
577 struct shash resolved_remotes;
580 /* Configure remotes. */
581 shash_init(&resolved_remotes);
582 SSET_FOR_EACH (name, remotes) {
583 if (!strncmp(name, "db:", 3)) {
584 query_db_remotes(name, db, &resolved_remotes);
586 add_remote(&resolved_remotes, name);
589 ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
590 shash_destroy_free_data(&resolved_remotes);
593 stream_ssl_set_key_and_cert(query_db_string(db, private_key_file),
594 query_db_string(db, certificate_file));
595 stream_ssl_set_ca_cert_file(query_db_string(db, ca_cert_file),
600 ovsdb_server_exit(struct unixctl_conn *conn, const char *args OVS_UNUSED,
603 bool *exiting = exiting_;
605 unixctl_command_reply(conn, 200, NULL);
609 ovsdb_server_compact(struct unixctl_conn *conn, const char *args OVS_UNUSED,
612 struct ovsdb_file *file = file_;
613 struct ovsdb_error *error;
615 VLOG_INFO("compacting database by user request");
616 error = ovsdb_file_compact(file);
618 unixctl_command_reply(conn, 200, NULL);
620 char *s = ovsdb_error_to_string(error);
621 ovsdb_error_destroy(error);
622 unixctl_command_reply(conn, 503, s);
627 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
628 * connections and reconnect. */
630 ovsdb_server_reconnect(struct unixctl_conn *conn, const char *args OVS_UNUSED,
633 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
635 ovsdb_jsonrpc_server_reconnect(jsonrpc);
636 unixctl_command_reply(conn, 200, NULL);
640 parse_options(int argc, char *argv[], char **file_namep,
641 struct sset *remotes, char **unixctl_pathp,
645 OPT_DUMMY = UCHAR_MAX + 1,
649 OPT_BOOTSTRAP_CA_CERT,
651 LEAK_CHECKER_OPTION_ENUMS,
654 static struct option long_options[] = {
655 {"remote", required_argument, NULL, OPT_REMOTE},
656 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
657 {"run", required_argument, NULL, OPT_RUN},
658 {"help", no_argument, NULL, 'h'},
659 {"version", no_argument, NULL, 'V'},
662 LEAK_CHECKER_LONG_OPTIONS,
663 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
664 {"private-key", required_argument, NULL, 'p'},
665 {"certificate", required_argument, NULL, 'c'},
666 {"ca-cert", required_argument, NULL, 'C'},
669 char *short_options = long_options_to_short_options(long_options);
675 c = getopt_long(argc, argv, short_options, long_options, NULL);
682 sset_add(remotes, optarg);
686 *unixctl_pathp = optarg;
690 *run_command = optarg;
697 OVS_PRINT_VERSION(0, 0);
701 DAEMON_OPTION_HANDLERS
702 LEAK_CHECKER_OPTION_HANDLERS
705 private_key_file = optarg;
709 certificate_file = optarg;
713 ca_cert_file = optarg;
714 bootstrap_ca_cert = false;
717 case OPT_BOOTSTRAP_CA_CERT:
718 ca_cert_file = optarg;
719 bootstrap_ca_cert = true;
736 *file_namep = xasprintf("%s/openvswitch/conf.db", ovs_sysconfdir());
740 *file_namep = xstrdup(argv[0]);
744 ovs_fatal(0, "database file is only non-option argument; "
745 "use --help for usage");
752 printf("%s: Open vSwitch database server\n"
753 "usage: %s [OPTIONS] DATABASE\n"
754 "where DATABASE is a database file in ovsdb format.\n",
755 program_name, program_name);
756 printf("\nJSON-RPC options (may be specified any number of times):\n"
757 " --remote=REMOTE connect or listen to REMOTE\n");
758 stream_usage("JSON-RPC", true, true, true);
761 printf("\nOther options:\n"
762 " --run COMMAND run COMMAND as subprocess then exit\n"
763 " --unixctl=SOCKET override default control socket name\n"
764 " -h, --help display this help message\n"
765 " -V, --version display version information\n");
766 leak_checker_usage();