1 /* Copyright (c) 2009, 2010 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"
50 #define THIS_MODULE VLM_ovsdb_server
52 static unixctl_cb_func ovsdb_server_exit;
54 static void parse_options(int argc, char *argv[], char **file_namep,
55 struct shash *remotes, char **unixctl_pathp);
56 static void usage(void) NO_RETURN;
58 static void set_remotes(struct ovsdb_jsonrpc_server *jsonrpc,
59 const struct ovsdb *db, struct shash *remotes);
62 main(int argc, char *argv[])
64 char *unixctl_path = NULL;
65 struct unixctl_server *unixctl;
66 struct ovsdb_jsonrpc_server *jsonrpc;
68 struct ovsdb_error *error;
74 proctitle_init(argc, argv);
75 set_program_name(argv[0]);
78 signal(SIGPIPE, SIG_IGN);
81 parse_options(argc, argv, &file_name, &remotes, &unixctl_path);
83 die_if_already_running();
86 error = ovsdb_file_open(file_name, false, &db);
88 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
91 jsonrpc = ovsdb_jsonrpc_server_create(db);
92 set_remotes(jsonrpc, db, &remotes);
94 retval = unixctl_server_create(unixctl_path, &unixctl);
101 unixctl_command_register("exit", ovsdb_server_exit, &exiting);
105 set_remotes(jsonrpc, db, &remotes);
106 ovsdb_jsonrpc_server_run(jsonrpc);
107 unixctl_server_run(unixctl);
108 ovsdb_trigger_run(db, time_msec());
110 ovsdb_jsonrpc_server_wait(jsonrpc);
111 unixctl_server_wait(unixctl);
112 ovsdb_trigger_wait(db, time_msec());
120 query_db_remotes(const char *name_, const struct ovsdb *db,
121 struct shash *remotes)
123 char *name, *db_prefix, *table_name, *column_name;
124 const struct ovsdb_column *column;
125 const struct ovsdb_table *table;
126 const struct ovsdb_row *row;
127 char *save_ptr = NULL;
129 name = xstrdup(name_);
130 db_prefix = strtok_r(name, ":", &save_ptr);
131 table_name = strtok_r(NULL, ",", &save_ptr);
132 column_name = strtok_r(NULL, ",", &save_ptr);
133 if (!table_name || !column_name) {
134 ovs_fatal(0, "remote \"%s\": invalid syntax", name_);
137 table = ovsdb_get_table(db, table_name);
139 ovs_fatal(0, "remote \"%s\": no table named %s", name_, table_name);
142 column = ovsdb_table_schema_get_column(table->schema, column_name);
144 ovs_fatal(0, "remote \"%s\": table \"%s\" has no column \"%s\"",
145 name_, table_name, column_name);
148 if (column->type.key_type != OVSDB_TYPE_STRING
149 || column->type.value_type != OVSDB_TYPE_VOID) {
150 ovs_fatal(0, "remote \"%s\": type of table \"%s\" column \"%s\" is "
151 "not string or set of strings",
152 name_, table_name, column_name);
155 HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node, &table->rows) {
156 const struct ovsdb_datum *datum;
159 datum = &row->fields[column->index];
160 for (i = 0; i < datum->n; i++) {
161 shash_add_once(remotes, datum->keys[i].string, NULL);
169 set_remotes(struct ovsdb_jsonrpc_server *jsonrpc,
170 const struct ovsdb *db, struct shash *remotes)
172 struct shash resolved_remotes;
173 struct shash_node *node;
175 shash_init(&resolved_remotes);
176 SHASH_FOR_EACH (node, remotes) {
177 const char *name = node->name;
179 if (!strncmp(name, "db:", 3)) {
180 query_db_remotes(name, db, &resolved_remotes);
182 shash_add_once(&resolved_remotes, name, NULL);
185 ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
186 shash_destroy(&resolved_remotes);
191 ovsdb_server_exit(struct unixctl_conn *conn, const char *args UNUSED,
194 bool *exiting = exiting_;
196 unixctl_command_reply(conn, 200, NULL);
200 parse_options(int argc, char *argv[], char **file_namep,
201 struct shash *remotes, char **unixctl_pathp)
204 OPT_DUMMY = UCHAR_MAX + 1,
207 OPT_BOOTSTRAP_CA_CERT,
209 LEAK_CHECKER_OPTION_ENUMS
211 static struct option long_options[] = {
212 {"remote", required_argument, 0, OPT_REMOTE},
213 {"unixctl", required_argument, 0, OPT_UNIXCTL},
214 {"help", no_argument, 0, 'h'},
215 {"version", no_argument, 0, 'V'},
218 LEAK_CHECKER_LONG_OPTIONS,
220 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
221 STREAM_SSL_LONG_OPTIONS
225 char *short_options = long_options_to_short_options(long_options);
231 c = getopt_long(argc, argv, short_options, long_options, NULL);
238 shash_add_once(remotes, optarg, NULL);
242 *unixctl_pathp = optarg;
249 OVS_PRINT_VERSION(0, 0);
253 DAEMON_OPTION_HANDLERS
254 LEAK_CHECKER_OPTION_HANDLERS
257 STREAM_SSL_OPTION_HANDLERS
259 case OPT_BOOTSTRAP_CA_CERT:
260 stream_ssl_set_ca_cert_file(optarg, true);
278 ovs_fatal(0, "database file is only non-option argument; "
279 "use --help for usage");
280 } else if (argc < 1) {
281 ovs_fatal(0, "missing database file argument; use --help for usage");
284 *file_namep = argv[0];
290 printf("%s: Open vSwitch database server\n"
291 "usage: %s [OPTIONS] DATABASE\n"
292 "where DATABASE is a database file in ovsdb format.\n",
293 program_name, program_name);
294 printf("\nJSON-RPC options (may be specified any number of times):\n"
295 " --remote=REMOTE connect or listen to REMOTE\n");
296 stream_usage("JSON-RPC", true, true, true);
299 printf("\nOther options:\n"
300 " -h, --help display this help message\n"
301 " -V, --version display version information\n");
302 leak_checker_usage();