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 write_string_string_column(rw_row, "status", keys, values, n);
506 update_remote_rows(const struct ovsdb *db, struct ovsdb_txn *txn,
507 const char *remote_name,
508 const struct ovsdb_jsonrpc_server *jsonrpc)
510 const struct ovsdb_table *table, *ref_table;
511 const struct ovsdb_column *column;
512 const struct ovsdb_row *row;
514 if (strncmp("db:", remote_name, 3)) {
518 parse_db_column(db, remote_name, &table, &column);
520 if (column->type.key.type != OVSDB_TYPE_UUID
521 || !column->type.key.u.uuid.refTable
522 || column->type.value.type != OVSDB_TYPE_VOID) {
526 ref_table = column->type.key.u.uuid.refTable;
528 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
529 const struct ovsdb_datum *datum;
532 datum = &row->fields[column->index];
533 for (i = 0; i < datum->n; i++) {
534 const struct ovsdb_row *ref_row;
536 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
538 update_remote_row(ref_row, txn, jsonrpc);
545 update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
546 const struct sset *remotes, struct ovsdb *db)
548 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
549 struct ovsdb_txn *txn;
550 const bool durable_txn = false;
551 struct ovsdb_error *error;
554 txn = ovsdb_txn_create(db);
556 /* Iterate over --remote arguments given on command line. */
557 SSET_FOR_EACH (remote, remotes) {
558 update_remote_rows(db, txn, remote, jsonrpc);
561 error = ovsdb_txn_commit(txn, durable_txn);
563 VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
564 ovsdb_error_to_string(error));
568 /* Reconfigures ovsdb-server based on information in the database. */
570 reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
571 const struct ovsdb *db, struct sset *remotes)
573 struct shash resolved_remotes;
576 /* Configure remotes. */
577 shash_init(&resolved_remotes);
578 SSET_FOR_EACH (name, remotes) {
579 if (!strncmp(name, "db:", 3)) {
580 query_db_remotes(name, db, &resolved_remotes);
582 add_remote(&resolved_remotes, name);
585 ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
586 shash_destroy_free_data(&resolved_remotes);
589 stream_ssl_set_key_and_cert(query_db_string(db, private_key_file),
590 query_db_string(db, certificate_file));
591 stream_ssl_set_ca_cert_file(query_db_string(db, ca_cert_file),
596 ovsdb_server_exit(struct unixctl_conn *conn, const char *args OVS_UNUSED,
599 bool *exiting = exiting_;
601 unixctl_command_reply(conn, 200, NULL);
605 ovsdb_server_compact(struct unixctl_conn *conn, const char *args OVS_UNUSED,
608 struct ovsdb_file *file = file_;
609 struct ovsdb_error *error;
611 VLOG_INFO("compacting database by user request");
612 error = ovsdb_file_compact(file);
614 unixctl_command_reply(conn, 200, NULL);
616 char *s = ovsdb_error_to_string(error);
617 ovsdb_error_destroy(error);
618 unixctl_command_reply(conn, 503, s);
623 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
624 * connections and reconnect. */
626 ovsdb_server_reconnect(struct unixctl_conn *conn, const char *args OVS_UNUSED,
629 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
631 ovsdb_jsonrpc_server_reconnect(jsonrpc);
632 unixctl_command_reply(conn, 200, NULL);
636 parse_options(int argc, char *argv[], char **file_namep,
637 struct sset *remotes, char **unixctl_pathp,
641 OPT_DUMMY = UCHAR_MAX + 1,
645 OPT_BOOTSTRAP_CA_CERT,
647 LEAK_CHECKER_OPTION_ENUMS,
650 static struct option long_options[] = {
651 {"remote", required_argument, NULL, OPT_REMOTE},
652 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
653 {"run", required_argument, NULL, OPT_RUN},
654 {"help", no_argument, NULL, 'h'},
655 {"version", no_argument, NULL, 'V'},
658 LEAK_CHECKER_LONG_OPTIONS,
659 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
660 {"private-key", required_argument, NULL, 'p'},
661 {"certificate", required_argument, NULL, 'c'},
662 {"ca-cert", required_argument, NULL, 'C'},
665 char *short_options = long_options_to_short_options(long_options);
671 c = getopt_long(argc, argv, short_options, long_options, NULL);
678 sset_add(remotes, optarg);
682 *unixctl_pathp = optarg;
686 *run_command = optarg;
693 OVS_PRINT_VERSION(0, 0);
697 DAEMON_OPTION_HANDLERS
698 LEAK_CHECKER_OPTION_HANDLERS
701 private_key_file = optarg;
705 certificate_file = optarg;
709 ca_cert_file = optarg;
710 bootstrap_ca_cert = false;
713 case OPT_BOOTSTRAP_CA_CERT:
714 ca_cert_file = optarg;
715 bootstrap_ca_cert = true;
732 *file_namep = xasprintf("%s/openvswitch/conf.db", ovs_sysconfdir());
736 *file_namep = xstrdup(argv[0]);
740 ovs_fatal(0, "database file is only non-option argument; "
741 "use --help for usage");
748 printf("%s: Open vSwitch database server\n"
749 "usage: %s [OPTIONS] DATABASE\n"
750 "where DATABASE is a database file in ovsdb format.\n",
751 program_name, program_name);
752 printf("\nJSON-RPC options (may be specified any number of times):\n"
753 " --remote=REMOTE connect or listen to REMOTE\n");
754 stream_usage("JSON-RPC", true, true, true);
757 printf("\nOther options:\n"
758 " --run COMMAND run COMMAND as subprocess then exit\n"
759 " --unixctl=SOCKET override default control socket name\n"
760 " -h, --help display this help message\n"
761 " -V, --version display version information\n");
762 leak_checker_usage();