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();
140 unixctl_command_register("exit", "", 0, 0, ovsdb_server_exit, &exiting);
141 unixctl_command_register("ovsdb-server/compact", "", 0, 0,
142 ovsdb_server_compact, file);
143 unixctl_command_register("ovsdb-server/reconnect", "", 0, 0,
144 ovsdb_server_reconnect, jsonrpc);
149 if (memory_should_report()) {
153 ovsdb_jsonrpc_server_get_memory_usage(jsonrpc, &usage);
154 ovsdb_get_memory_usage(db, &usage);
155 memory_report(&usage);
156 simap_destroy(&usage);
159 reconfigure_from_db(jsonrpc, db, &remotes);
160 ovsdb_jsonrpc_server_run(jsonrpc);
161 unixctl_server_run(unixctl);
162 ovsdb_trigger_run(db, time_msec());
163 if (run_process && process_exited(run_process)) {
167 /* update Manager status(es) every 5 seconds */
168 if (time_msec() >= status_timer) {
169 status_timer = time_msec() + 5000;
170 update_remote_status(jsonrpc, &remotes, db);
174 ovsdb_jsonrpc_server_wait(jsonrpc);
175 unixctl_server_wait(unixctl);
176 ovsdb_trigger_wait(db, time_msec());
178 process_wait(run_process);
181 poll_immediate_wake();
183 poll_timer_wait_until(status_timer);
186 ovsdb_jsonrpc_server_destroy(jsonrpc);
188 sset_destroy(&remotes);
189 unixctl_server_destroy(unixctl);
191 if (run_process && process_exited(run_process)) {
192 int status = process_status(run_process);
194 ovs_fatal(0, "%s: child exited, %s",
195 run_command, process_status_msg(status));
203 parse_db_column(const struct ovsdb *db,
205 const struct ovsdb_table **tablep,
206 const struct ovsdb_column **columnp)
208 char *name, *table_name, *column_name;
209 const struct ovsdb_column *column;
210 const struct ovsdb_table *table;
211 char *save_ptr = NULL;
213 name = xstrdup(name_);
214 strtok_r(name, ":", &save_ptr); /* "db:" */
215 table_name = strtok_r(NULL, ",", &save_ptr);
216 column_name = strtok_r(NULL, ",", &save_ptr);
217 if (!table_name || !column_name) {
218 ovs_fatal(0, "\"%s\": invalid syntax", name_);
221 table = ovsdb_get_table(db, table_name);
223 ovs_fatal(0, "\"%s\": no table named %s", name_, table_name);
226 column = ovsdb_table_schema_get_column(table->schema, column_name);
228 ovs_fatal(0, "\"%s\": table \"%s\" has no column \"%s\"",
229 name_, table_name, column_name);
238 parse_db_string_column(const struct ovsdb *db,
240 const struct ovsdb_table **tablep,
241 const struct ovsdb_column **columnp)
243 const struct ovsdb_column *column;
244 const struct ovsdb_table *table;
246 parse_db_column(db, name, &table, &column);
248 if (column->type.key.type != OVSDB_TYPE_STRING
249 || column->type.value.type != OVSDB_TYPE_VOID) {
250 ovs_fatal(0, "\"%s\": table \"%s\" column \"%s\" is "
251 "not string or set of strings",
252 name, table->schema->name, column->name);
259 static OVS_UNUSED const char *
260 query_db_string(const struct ovsdb *db, const char *name)
262 if (!name || strncmp(name, "db:", 3)) {
265 const struct ovsdb_column *column;
266 const struct ovsdb_table *table;
267 const struct ovsdb_row *row;
269 parse_db_string_column(db, name, &table, &column);
271 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
272 const struct ovsdb_datum *datum;
275 datum = &row->fields[column->index];
276 for (i = 0; i < datum->n; i++) {
277 if (datum->keys[i].string[0]) {
278 return datum->keys[i].string;
286 static struct ovsdb_jsonrpc_options *
287 add_remote(struct shash *remotes, const char *target)
289 struct ovsdb_jsonrpc_options *options;
291 options = shash_find_data(remotes, target);
293 options = ovsdb_jsonrpc_default_options(target);
294 shash_add(remotes, target, options);
300 static struct ovsdb_datum *
301 get_datum(struct ovsdb_row *row, const char *column_name,
302 const enum ovsdb_atomic_type key_type,
303 const enum ovsdb_atomic_type value_type,
306 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
307 const struct ovsdb_table_schema *schema = row->table->schema;
308 const struct ovsdb_column *column;
310 column = ovsdb_table_schema_get_column(schema, column_name);
312 VLOG_DBG_RL(&rl, "Table `%s' has no `%s' column",
313 schema->name, column_name);
317 if (column->type.key.type != key_type
318 || column->type.value.type != value_type
319 || column->type.n_max != n_max) {
320 if (!VLOG_DROP_DBG(&rl)) {
321 char *type_name = ovsdb_type_to_english(&column->type);
322 VLOG_DBG("Table `%s' column `%s' has type %s, not expected "
323 "key type %s, value type %s, max elements %zd.",
324 schema->name, column_name, type_name,
325 ovsdb_atomic_type_to_string(key_type),
326 ovsdb_atomic_type_to_string(value_type),
333 return &row->fields[column->index];
336 /* Read string-string key-values from a map. Returns the value associated with
337 * 'key', if found, or NULL */
339 read_map_string_column(const struct ovsdb_row *row, const char *column_name,
342 const struct ovsdb_datum *datum;
343 union ovsdb_atom *atom_key = NULL, *atom_value = NULL;
346 datum = get_datum((struct ovsdb_row *) row, column_name, OVSDB_TYPE_STRING,
347 OVSDB_TYPE_STRING, UINT_MAX);
353 for (i = 0; i < datum->n; i++) {
354 atom_key = &datum->keys[i];
355 if (!strcmp(atom_key->string, key)){
356 atom_value = &datum->values[i];
361 return atom_value ? atom_value->string : NULL;
364 static const union ovsdb_atom *
365 read_column(const struct ovsdb_row *row, const char *column_name,
366 enum ovsdb_atomic_type type)
368 const struct ovsdb_datum *datum;
370 datum = get_datum((struct ovsdb_row *) row, column_name, type, OVSDB_TYPE_VOID,
372 return datum && datum->n ? datum->keys : NULL;
376 read_integer_column(const struct ovsdb_row *row, const char *column_name,
377 long long int *integerp)
379 const union ovsdb_atom *atom;
381 atom = read_column(row, column_name, OVSDB_TYPE_INTEGER);
382 *integerp = atom ? atom->integer : 0;
387 read_string_column(const struct ovsdb_row *row, const char *column_name,
388 const char **stringp)
390 const union ovsdb_atom *atom;
392 atom = read_column(row, column_name, OVSDB_TYPE_STRING);
393 *stringp = atom ? atom->string : NULL;
398 write_bool_column(struct ovsdb_row *row, const char *column_name, bool value)
400 struct ovsdb_datum *datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
406 datum->keys[0].boolean = value;
410 write_string_string_column(struct ovsdb_row *row, const char *column_name,
411 char **keys, char **values, size_t n)
413 const struct ovsdb_column *column;
414 struct ovsdb_datum *datum;
417 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
418 datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
424 /* Free existing data. */
425 ovsdb_datum_destroy(datum, &column->type);
427 /* Allocate space for new values. */
429 datum->keys = xmalloc(n * sizeof *datum->keys);
430 datum->values = xmalloc(n * sizeof *datum->values);
432 for (i = 0; i < n; ++i) {
433 datum->keys[i].string = keys[i];
434 datum->values[i].string = values[i];
437 /* Sort and check constraints. */
438 ovsdb_datum_sort_assert(datum, column->type.key.type);
441 /* Adds a remote and options to 'remotes', based on the Manager table row in
444 add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
446 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
447 struct ovsdb_jsonrpc_options *options;
448 long long int max_backoff, probe_interval;
449 const char *target, *dscp_string;
451 if (!read_string_column(row, "target", &target) || !target) {
452 VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
453 row->table->schema->name);
457 options = add_remote(remotes, target);
458 if (read_integer_column(row, "max_backoff", &max_backoff)) {
459 options->max_backoff = max_backoff;
461 if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
462 options->probe_interval = probe_interval;
465 options->dscp = DSCP_DEFAULT;
466 dscp_string = read_map_string_column(row, "other_config", "dscp");
468 int dscp = atoi(dscp_string);
469 if (dscp >= 0 && dscp <= 63) {
470 options->dscp = dscp;
476 query_db_remotes(const char *name, const struct ovsdb *db,
477 struct shash *remotes)
479 const struct ovsdb_column *column;
480 const struct ovsdb_table *table;
481 const struct ovsdb_row *row;
483 parse_db_column(db, name, &table, &column);
485 if (column->type.key.type == OVSDB_TYPE_STRING
486 && column->type.value.type == OVSDB_TYPE_VOID) {
487 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
488 const struct ovsdb_datum *datum;
491 datum = &row->fields[column->index];
492 for (i = 0; i < datum->n; i++) {
493 add_remote(remotes, datum->keys[i].string);
496 } else if (column->type.key.type == OVSDB_TYPE_UUID
497 && column->type.key.u.uuid.refTable
498 && column->type.value.type == OVSDB_TYPE_VOID) {
499 const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
500 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
501 const struct ovsdb_datum *datum;
504 datum = &row->fields[column->index];
505 for (i = 0; i < datum->n; i++) {
506 const struct ovsdb_row *ref_row;
508 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
510 add_manager_options(remotes, ref_row);
518 update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
519 const struct ovsdb_jsonrpc_server *jsonrpc)
521 struct ovsdb_jsonrpc_remote_status status;
522 struct ovsdb_row *rw_row;
524 char *keys[8], *values[8];
527 /* Get the "target" (protocol/host/port) spec. */
528 if (!read_string_column(row, "target", &target)) {
529 /* Bad remote spec or incorrect schema. */
532 rw_row = ovsdb_txn_row_modify(txn, row);
533 ovsdb_jsonrpc_server_get_remote_status(jsonrpc, target, &status);
535 /* Update status information columns. */
536 write_bool_column(rw_row, "is_connected", status.is_connected);
539 keys[n] = xstrdup("state");
540 values[n++] = xstrdup(status.state);
542 if (status.sec_since_connect != UINT_MAX) {
543 keys[n] = xstrdup("sec_since_connect");
544 values[n++] = xasprintf("%u", status.sec_since_connect);
546 if (status.sec_since_disconnect != UINT_MAX) {
547 keys[n] = xstrdup("sec_since_disconnect");
548 values[n++] = xasprintf("%u", status.sec_since_disconnect);
550 if (status.last_error) {
551 keys[n] = xstrdup("last_error");
553 xstrdup(ovs_retval_to_string(status.last_error));
555 if (status.locks_held && status.locks_held[0]) {
556 keys[n] = xstrdup("locks_held");
557 values[n++] = xstrdup(status.locks_held);
559 if (status.locks_waiting && status.locks_waiting[0]) {
560 keys[n] = xstrdup("locks_waiting");
561 values[n++] = xstrdup(status.locks_waiting);
563 if (status.locks_lost && status.locks_lost[0]) {
564 keys[n] = xstrdup("locks_lost");
565 values[n++] = xstrdup(status.locks_lost);
567 if (status.n_connections > 1) {
568 keys[n] = xstrdup("n_connections");
569 values[n++] = xasprintf("%d", status.n_connections);
571 write_string_string_column(rw_row, "status", keys, values, n);
573 ovsdb_jsonrpc_server_free_remote_status(&status);
577 update_remote_rows(const struct ovsdb *db, struct ovsdb_txn *txn,
578 const char *remote_name,
579 const struct ovsdb_jsonrpc_server *jsonrpc)
581 const struct ovsdb_table *table, *ref_table;
582 const struct ovsdb_column *column;
583 const struct ovsdb_row *row;
585 if (strncmp("db:", remote_name, 3)) {
589 parse_db_column(db, remote_name, &table, &column);
591 if (column->type.key.type != OVSDB_TYPE_UUID
592 || !column->type.key.u.uuid.refTable
593 || column->type.value.type != OVSDB_TYPE_VOID) {
597 ref_table = column->type.key.u.uuid.refTable;
599 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
600 const struct ovsdb_datum *datum;
603 datum = &row->fields[column->index];
604 for (i = 0; i < datum->n; i++) {
605 const struct ovsdb_row *ref_row;
607 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
609 update_remote_row(ref_row, txn, jsonrpc);
616 update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
617 const struct sset *remotes, struct ovsdb *db)
619 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
620 struct ovsdb_txn *txn;
621 const bool durable_txn = false;
622 struct ovsdb_error *error;
625 txn = ovsdb_txn_create(db);
627 /* Iterate over --remote arguments given on command line. */
628 SSET_FOR_EACH (remote, remotes) {
629 update_remote_rows(db, txn, remote, jsonrpc);
632 error = ovsdb_txn_commit(txn, durable_txn);
634 VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
635 ovsdb_error_to_string(error));
639 /* Reconfigures ovsdb-server based on information in the database. */
641 reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
642 const struct ovsdb *db, struct sset *remotes)
644 struct shash resolved_remotes;
647 /* Configure remotes. */
648 shash_init(&resolved_remotes);
649 SSET_FOR_EACH (name, remotes) {
650 if (!strncmp(name, "db:", 3)) {
651 query_db_remotes(name, db, &resolved_remotes);
653 add_remote(&resolved_remotes, name);
656 ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
657 shash_destroy_free_data(&resolved_remotes);
660 stream_ssl_set_key_and_cert(query_db_string(db, private_key_file),
661 query_db_string(db, certificate_file));
662 stream_ssl_set_ca_cert_file(query_db_string(db, ca_cert_file),
667 ovsdb_server_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
668 const char *argv[] OVS_UNUSED,
671 bool *exiting = exiting_;
673 unixctl_command_reply(conn, NULL);
677 ovsdb_server_compact(struct unixctl_conn *conn, int argc OVS_UNUSED,
678 const char *argv[] OVS_UNUSED, void *file_)
680 struct ovsdb_file *file = file_;
681 struct ovsdb_error *error;
683 VLOG_INFO("compacting database by user request");
684 error = ovsdb_file_compact(file);
686 unixctl_command_reply(conn, NULL);
688 char *s = ovsdb_error_to_string(error);
689 ovsdb_error_destroy(error);
690 unixctl_command_reply_error(conn, s);
695 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
696 * connections and reconnect. */
698 ovsdb_server_reconnect(struct unixctl_conn *conn, int argc OVS_UNUSED,
699 const char *argv[] OVS_UNUSED, void *jsonrpc_)
701 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
703 ovsdb_jsonrpc_server_reconnect(jsonrpc);
704 unixctl_command_reply(conn, NULL);
708 parse_options(int argc, char *argv[], char **file_namep,
709 struct sset *remotes, char **unixctl_pathp,
713 OPT_DUMMY = UCHAR_MAX + 1,
717 OPT_BOOTSTRAP_CA_CERT,
719 LEAK_CHECKER_OPTION_ENUMS,
722 static struct option long_options[] = {
723 {"remote", required_argument, NULL, OPT_REMOTE},
724 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
725 {"run", required_argument, NULL, OPT_RUN},
726 {"help", no_argument, NULL, 'h'},
727 {"version", no_argument, NULL, 'V'},
730 LEAK_CHECKER_LONG_OPTIONS,
731 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
732 {"private-key", required_argument, NULL, 'p'},
733 {"certificate", required_argument, NULL, 'c'},
734 {"ca-cert", required_argument, NULL, 'C'},
737 char *short_options = long_options_to_short_options(long_options);
743 c = getopt_long(argc, argv, short_options, long_options, NULL);
750 sset_add(remotes, optarg);
754 *unixctl_pathp = optarg;
758 *run_command = optarg;
765 ovs_print_version(0, 0);
769 DAEMON_OPTION_HANDLERS
770 LEAK_CHECKER_OPTION_HANDLERS
773 private_key_file = optarg;
777 certificate_file = optarg;
781 ca_cert_file = optarg;
782 bootstrap_ca_cert = false;
785 case OPT_BOOTSTRAP_CA_CERT:
786 ca_cert_file = optarg;
787 bootstrap_ca_cert = true;
804 *file_namep = xasprintf("%s/openvswitch/conf.db", ovs_sysconfdir());
808 *file_namep = xstrdup(argv[0]);
812 ovs_fatal(0, "database file is only non-option argument; "
813 "use --help for usage");
820 printf("%s: Open vSwitch database server\n"
821 "usage: %s [OPTIONS] DATABASE\n"
822 "where DATABASE is a database file in ovsdb format.\n",
823 program_name, program_name);
824 printf("\nJSON-RPC options (may be specified any number of times):\n"
825 " --remote=REMOTE connect or listen to REMOTE\n");
826 stream_usage("JSON-RPC", true, true, true);
829 printf("\nOther options:\n"
830 " --run COMMAND run COMMAND as subprocess then exit\n"
831 " --unixctl=SOCKET override default control socket name\n"
832 " -h, --help display this help message\n"
833 " -V, --version display version information\n");
834 leak_checker_usage();