Merge "next" branch into "master".
[cascardo/ovs.git] / ovsdb / ovsdb-server.c
1 /* Copyright (c) 2009, 2010 Nicira Networks
2  *
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <config.h>
17
18 #include "ovsdb.h"
19
20 #include <errno.h>
21 #include <getopt.h>
22 #include <signal.h>
23 #include <unistd.h>
24
25 #include "column.h"
26 #include "command-line.h"
27 #include "daemon.h"
28 #include "file.h"
29 #include "json.h"
30 #include "jsonrpc.h"
31 #include "jsonrpc-server.h"
32 #include "leak-checker.h"
33 #include "list.h"
34 #include "ovsdb-data.h"
35 #include "ovsdb-types.h"
36 #include "ovsdb-error.h"
37 #include "poll-loop.h"
38 #include "process.h"
39 #include "row.h"
40 #include "stream-ssl.h"
41 #include "stream.h"
42 #include "svec.h"
43 #include "table.h"
44 #include "timeval.h"
45 #include "trigger.h"
46 #include "util.h"
47 #include "unixctl.h"
48
49 #include "vlog.h"
50 #define THIS_MODULE VLM_ovsdb_server
51
52 static unixctl_cb_func ovsdb_server_exit;
53
54 static void parse_options(int argc, char *argv[], char **file_namep,
55                           struct shash *remotes, char **unixctl_pathp,
56                           char **run_command);
57 static void usage(void) NO_RETURN;
58
59 static void set_remotes(struct ovsdb_jsonrpc_server *jsonrpc,
60                         const struct ovsdb *db, struct shash *remotes);
61
62 int
63 main(int argc, char *argv[])
64 {
65     char *unixctl_path = NULL;
66     char *run_command = NULL;
67     struct unixctl_server *unixctl;
68     struct ovsdb_jsonrpc_server *jsonrpc;
69     struct shash remotes;
70     struct ovsdb_error *error;
71     struct ovsdb *db;
72     struct process *run_process;
73     char *file_name;
74     bool exiting;
75     int retval;
76
77     proctitle_init(argc, argv);
78     set_program_name(argv[0]);
79     time_init();
80     vlog_init();
81     signal(SIGPIPE, SIG_IGN);
82     process_init();
83
84     parse_options(argc, argv, &file_name, &remotes, &unixctl_path,
85                   &run_command);
86
87     die_if_already_running();
88     daemonize_start();
89
90     error = ovsdb_file_open(file_name, false, &db);
91     if (error) {
92         ovs_fatal(0, "%s", ovsdb_error_to_string(error));
93     }
94
95     jsonrpc = ovsdb_jsonrpc_server_create(db);
96     set_remotes(jsonrpc, db, &remotes);
97
98     retval = unixctl_server_create(unixctl_path, &unixctl);
99     if (retval) {
100         exit(EXIT_FAILURE);
101     }
102
103     if (run_command) {
104         char *run_argv[4];
105
106         run_argv[0] = "/bin/sh";
107         run_argv[1] = "-c";
108         run_argv[2] = run_command;
109         run_argv[3] = NULL;
110
111         retval = process_start(run_argv, NULL, 0, NULL, 0, &run_process);
112         if (retval) {
113             ovs_fatal(retval, "%s: process failed to start", run_command);
114         }
115     } else {
116         run_process = NULL;
117     }
118
119     daemonize_complete();
120
121     unixctl_command_register("exit", ovsdb_server_exit, &exiting);
122
123     exiting = false;
124     while (!exiting) {
125         set_remotes(jsonrpc, db, &remotes);
126         ovsdb_jsonrpc_server_run(jsonrpc);
127         unixctl_server_run(unixctl);
128         ovsdb_trigger_run(db, time_msec());
129         if (run_process && process_exited(run_process)) {
130             exiting = true;
131         }
132
133         ovsdb_jsonrpc_server_wait(jsonrpc);
134         unixctl_server_wait(unixctl);
135         ovsdb_trigger_wait(db, time_msec());
136         if (run_process) {
137             process_wait(run_process);
138         }
139         poll_block();
140     }
141     ovsdb_jsonrpc_server_destroy(jsonrpc);
142     ovsdb_destroy(db);
143     shash_destroy(&remotes);
144     unixctl_server_destroy(unixctl);
145
146     if (run_process && process_exited(run_process)) {
147         int status = process_status(run_process);
148         if (status) {
149             ovs_fatal(0, "%s: child exited, %s",
150                       run_command, process_status_msg(status));
151         }
152     }
153
154     return 0;
155 }
156
157 static void
158 query_db_remotes(const char *name_, const struct ovsdb *db,
159                  struct shash *remotes)
160 {
161     char *name, *table_name, *column_name;
162     const struct ovsdb_column *column;
163     const struct ovsdb_table *table;
164     const struct ovsdb_row *row;
165     char *save_ptr = NULL;
166
167     name = xstrdup(name_);
168     strtok_r(name, ":", &save_ptr); /* "db:" */
169     table_name = strtok_r(NULL, ",", &save_ptr);
170     column_name = strtok_r(NULL, ",", &save_ptr);
171     if (!table_name || !column_name) {
172         ovs_fatal(0, "remote \"%s\": invalid syntax", name_);
173     }
174
175     table = ovsdb_get_table(db, table_name);
176     if (!table) {
177         ovs_fatal(0, "remote \"%s\": no table named %s", name_, table_name);
178     }
179
180     column = ovsdb_table_schema_get_column(table->schema, column_name);
181     if (!column) {
182         ovs_fatal(0, "remote \"%s\": table \"%s\" has no column \"%s\"",
183                   name_, table_name, column_name);
184     }
185
186     if (column->type.key.type != OVSDB_TYPE_STRING
187         || column->type.value.type != OVSDB_TYPE_VOID) {
188         ovs_fatal(0, "remote \"%s\": type of table \"%s\" column \"%s\" is "
189                   "not string or set of strings",
190                   name_, table_name, column_name);
191     }
192
193     HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node, &table->rows) {
194         const struct ovsdb_datum *datum;
195         size_t i;
196
197         datum = &row->fields[column->index];
198         for (i = 0; i < datum->n; i++) {
199             shash_add_once(remotes, datum->keys[i].string, NULL);
200         }
201     }
202
203     free(name);
204 }
205
206 static void
207 set_remotes(struct ovsdb_jsonrpc_server *jsonrpc,
208             const struct ovsdb *db, struct shash *remotes)
209 {
210     struct shash resolved_remotes;
211     struct shash_node *node;
212
213     shash_init(&resolved_remotes);
214     SHASH_FOR_EACH (node, remotes) {
215         const char *name = node->name;
216
217         if (!strncmp(name, "db:", 3)) {
218             query_db_remotes(name, db, &resolved_remotes);
219         } else {
220             shash_add_once(&resolved_remotes, name, NULL);
221         }
222     }
223     ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
224     shash_destroy(&resolved_remotes);
225 }
226
227
228 static void
229 ovsdb_server_exit(struct unixctl_conn *conn, const char *args OVS_UNUSED,
230                   void *exiting_)
231 {
232     bool *exiting = exiting_;
233     *exiting = true;
234     unixctl_command_reply(conn, 200, NULL);
235 }
236
237 static void
238 parse_options(int argc, char *argv[], char **file_namep,
239               struct shash *remotes, char **unixctl_pathp,
240               char **run_command)
241 {
242     enum {
243         OPT_DUMMY = UCHAR_MAX + 1,
244         OPT_REMOTE,
245         OPT_UNIXCTL,
246         OPT_RUN,
247         OPT_BOOTSTRAP_CA_CERT,
248         VLOG_OPTION_ENUMS,
249         LEAK_CHECKER_OPTION_ENUMS
250     };
251     static struct option long_options[] = {
252         {"remote",      required_argument, 0, OPT_REMOTE},
253         {"unixctl",     required_argument, 0, OPT_UNIXCTL},
254         {"run",         required_argument, 0, OPT_RUN},
255         {"help",        no_argument, 0, 'h'},
256         {"version",     no_argument, 0, 'V'},
257         DAEMON_LONG_OPTIONS,
258         VLOG_LONG_OPTIONS,
259         LEAK_CHECKER_LONG_OPTIONS,
260 #ifdef HAVE_OPENSSL
261         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
262         STREAM_SSL_LONG_OPTIONS
263 #endif
264         {0, 0, 0, 0},
265     };
266     char *short_options = long_options_to_short_options(long_options);
267
268     shash_init(remotes);
269     for (;;) {
270         int c;
271
272         c = getopt_long(argc, argv, short_options, long_options, NULL);
273         if (c == -1) {
274             break;
275         }
276
277         switch (c) {
278         case OPT_REMOTE:
279             shash_add_once(remotes, optarg, NULL);
280             break;
281
282         case OPT_UNIXCTL:
283             *unixctl_pathp = optarg;
284             break;
285
286         case OPT_RUN:
287             *run_command = optarg;
288             break;
289
290         case 'h':
291             usage();
292
293         case 'V':
294             OVS_PRINT_VERSION(0, 0);
295             exit(EXIT_SUCCESS);
296
297         VLOG_OPTION_HANDLERS
298         DAEMON_OPTION_HANDLERS
299         LEAK_CHECKER_OPTION_HANDLERS
300
301 #ifdef HAVE_OPENSSL
302         STREAM_SSL_OPTION_HANDLERS
303
304         case OPT_BOOTSTRAP_CA_CERT:
305             stream_ssl_set_ca_cert_file(optarg, true);
306             break;
307 #endif
308
309
310         case '?':
311             exit(EXIT_FAILURE);
312
313         default:
314             abort();
315         }
316     }
317     free(short_options);
318
319     argc -= optind;
320     argv += optind;
321
322     if (argc > 1) {
323         ovs_fatal(0, "database file is only non-option argument; "
324                 "use --help for usage");
325     } else if (argc < 1) {
326         ovs_fatal(0, "missing database file argument; use --help for usage");
327     }
328
329     *file_namep = argv[0];
330 }
331
332 static void
333 usage(void)
334 {
335     printf("%s: Open vSwitch database server\n"
336            "usage: %s [OPTIONS] DATABASE\n"
337            "where DATABASE is a database file in ovsdb format.\n",
338            program_name, program_name);
339     printf("\nJSON-RPC options (may be specified any number of times):\n"
340            "  --remote=REMOTE         connect or listen to REMOTE\n");
341     stream_usage("JSON-RPC", true, true, true);
342     daemon_usage();
343     vlog_usage();
344     printf("\nOther options:\n"
345            "  --run COMMAND           run COMMAND as subprocess then exit\n"
346            "  -h, --help              display this help message\n"
347            "  -V, --version           display version information\n");
348     leak_checker_usage();
349     exit(EXIT_SUCCESS);
350 }