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 /* SSL configuration. */
53 static char *private_key_file;
54 static char *certificate_file;
55 static char *ca_cert_file;
56 static bool bootstrap_ca_cert;
58 static unixctl_cb_func ovsdb_server_exit;
59 static unixctl_cb_func ovsdb_server_compact;
61 static void parse_options(int argc, char *argv[], char **file_namep,
62 struct shash *remotes, char **unixctl_pathp,
64 static void usage(void) NO_RETURN;
66 static void reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
67 const struct ovsdb *db, struct shash *remotes);
70 main(int argc, char *argv[])
72 char *unixctl_path = NULL;
73 char *run_command = NULL;
74 struct unixctl_server *unixctl;
75 struct ovsdb_jsonrpc_server *jsonrpc;
77 struct ovsdb_error *error;
78 struct ovsdb_file *file;
80 struct process *run_process;
85 proctitle_init(argc, argv);
86 set_program_name(argv[0]);
89 signal(SIGPIPE, SIG_IGN);
92 parse_options(argc, argv, &file_name, &remotes, &unixctl_path,
95 die_if_already_running();
98 error = ovsdb_file_open(file_name, false, &db, &file);
100 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
103 jsonrpc = ovsdb_jsonrpc_server_create(db);
104 reconfigure_from_db(jsonrpc, db, &remotes);
106 retval = unixctl_server_create(unixctl_path, &unixctl);
114 run_argv[0] = "/bin/sh";
116 run_argv[2] = run_command;
119 retval = process_start(run_argv, NULL, 0, NULL, 0, &run_process);
121 ovs_fatal(retval, "%s: process failed to start", run_command);
127 daemonize_complete();
129 unixctl_command_register("exit", ovsdb_server_exit, &exiting);
130 unixctl_command_register("ovsdb-server/compact", ovsdb_server_compact,
135 reconfigure_from_db(jsonrpc, db, &remotes);
136 ovsdb_jsonrpc_server_run(jsonrpc);
137 unixctl_server_run(unixctl);
138 ovsdb_trigger_run(db, time_msec());
139 if (run_process && process_exited(run_process)) {
143 ovsdb_jsonrpc_server_wait(jsonrpc);
144 unixctl_server_wait(unixctl);
145 ovsdb_trigger_wait(db, time_msec());
147 process_wait(run_process);
151 ovsdb_jsonrpc_server_destroy(jsonrpc);
153 shash_destroy(&remotes);
154 unixctl_server_destroy(unixctl);
156 if (run_process && process_exited(run_process)) {
157 int status = process_status(run_process);
159 ovs_fatal(0, "%s: child exited, %s",
160 run_command, process_status_msg(status));
168 parse_db_string_column(const struct ovsdb *db,
170 const struct ovsdb_table **tablep,
171 const struct ovsdb_column **columnp)
173 char *name, *table_name, *column_name;
174 const struct ovsdb_column *column;
175 const struct ovsdb_table *table;
176 char *save_ptr = NULL;
178 name = xstrdup(name_);
179 strtok_r(name, ":", &save_ptr); /* "db:" */
180 table_name = strtok_r(NULL, ",", &save_ptr);
181 column_name = strtok_r(NULL, ",", &save_ptr);
182 if (!table_name || !column_name) {
183 ovs_fatal(0, "\"%s\": invalid syntax", name_);
186 table = ovsdb_get_table(db, table_name);
188 ovs_fatal(0, "\"%s\": no table named %s", name_, table_name);
191 column = ovsdb_table_schema_get_column(table->schema, column_name);
193 ovs_fatal(0, "\"%s\": table \"%s\" has no column \"%s\"",
194 name_, table_name, column_name);
198 if (column->type.key.type != OVSDB_TYPE_STRING
199 || column->type.value.type != OVSDB_TYPE_VOID) {
200 ovs_fatal(0, "\"%s\": table \"%s\" column \"%s\" is "
201 "not string or set of strings",
202 name_, table->schema->name, column->name);
210 query_db_string(const struct ovsdb *db, const char *name)
212 if (!name || strncmp(name, "db:", 3)) {
215 const struct ovsdb_column *column;
216 const struct ovsdb_table *table;
217 const struct ovsdb_row *row;
219 parse_db_string_column(db, name, &table, &column);
221 HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node, &table->rows) {
222 const struct ovsdb_datum *datum;
225 datum = &row->fields[column->index];
226 for (i = 0; i < datum->n; i++) {
227 if (datum->keys[i].string[0]) {
228 return datum->keys[i].string;
237 query_db_remotes(const char *name, const struct ovsdb *db,
238 struct shash *remotes)
240 const struct ovsdb_column *column;
241 const struct ovsdb_table *table;
242 const struct ovsdb_row *row;
244 parse_db_string_column(db, name, &table, &column);
246 HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node, &table->rows) {
247 const struct ovsdb_datum *datum;
250 datum = &row->fields[column->index];
251 for (i = 0; i < datum->n; i++) {
252 shash_add_once(remotes, datum->keys[i].string, NULL);
257 /* Reconfigures ovsdb-server based on information in the database. */
259 reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
260 const struct ovsdb *db, struct shash *remotes)
262 struct shash resolved_remotes;
263 struct shash_node *node;
265 /* Configure remotes. */
266 shash_init(&resolved_remotes);
267 SHASH_FOR_EACH (node, remotes) {
268 const char *name = node->name;
270 if (!strncmp(name, "db:", 3)) {
271 query_db_remotes(name, db, &resolved_remotes);
273 shash_add_once(&resolved_remotes, name, NULL);
276 ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
277 shash_destroy(&resolved_remotes);
280 stream_ssl_set_private_key_file(query_db_string(db, private_key_file));
281 stream_ssl_set_certificate_file(query_db_string(db, certificate_file));
282 stream_ssl_set_ca_cert_file(query_db_string(db, ca_cert_file),
287 ovsdb_server_exit(struct unixctl_conn *conn, const char *args OVS_UNUSED,
290 bool *exiting = exiting_;
292 unixctl_command_reply(conn, 200, NULL);
296 ovsdb_server_compact(struct unixctl_conn *conn, const char *args OVS_UNUSED,
299 struct ovsdb_file *file = file_;
300 struct ovsdb_error *error;
302 VLOG_INFO("compacting database by user request");
303 error = ovsdb_file_compact(file);
305 unixctl_command_reply(conn, 200, NULL);
307 char *s = ovsdb_error_to_string(error);
308 ovsdb_error_destroy(error);
309 unixctl_command_reply(conn, 503, s);
315 parse_options(int argc, char *argv[], char **file_namep,
316 struct shash *remotes, char **unixctl_pathp,
320 OPT_DUMMY = UCHAR_MAX + 1,
324 OPT_BOOTSTRAP_CA_CERT,
326 LEAK_CHECKER_OPTION_ENUMS
328 static struct option long_options[] = {
329 {"remote", required_argument, 0, OPT_REMOTE},
330 {"unixctl", required_argument, 0, OPT_UNIXCTL},
331 {"run", required_argument, 0, OPT_RUN},
332 {"help", no_argument, 0, 'h'},
333 {"version", no_argument, 0, 'V'},
336 LEAK_CHECKER_LONG_OPTIONS,
338 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
339 {"private-key", required_argument, 0, 'p'},
340 {"certificate", required_argument, 0, 'c'},
341 {"ca-cert", required_argument, 0, 'C'},
345 char *short_options = long_options_to_short_options(long_options);
351 c = getopt_long(argc, argv, short_options, long_options, NULL);
358 shash_add_once(remotes, optarg, NULL);
362 *unixctl_pathp = optarg;
366 *run_command = optarg;
373 OVS_PRINT_VERSION(0, 0);
377 DAEMON_OPTION_HANDLERS
378 LEAK_CHECKER_OPTION_HANDLERS
382 private_key_file = optarg;
386 certificate_file = optarg;
390 ca_cert_file = optarg;
391 bootstrap_ca_cert = false;
394 case OPT_BOOTSTRAP_CA_CERT:
395 ca_cert_file = optarg;
396 bootstrap_ca_cert = true;
413 ovs_fatal(0, "database file is only non-option argument; "
414 "use --help for usage");
415 } else if (argc < 1) {
416 ovs_fatal(0, "missing database file argument; use --help for usage");
419 *file_namep = argv[0];
425 printf("%s: Open vSwitch database server\n"
426 "usage: %s [OPTIONS] DATABASE\n"
427 "where DATABASE is a database file in ovsdb format.\n",
428 program_name, program_name);
429 printf("\nJSON-RPC options (may be specified any number of times):\n"
430 " --remote=REMOTE connect or listen to REMOTE\n");
431 stream_usage("JSON-RPC", true, true, true);
434 printf("\nOther options:\n"
435 " --run COMMAND run COMMAND as subprocess then exit\n"
436 " -h, --help display this help message\n"
437 " -V, --version display version information\n");
438 leak_checker_usage();