timeval: Make time_init() static and remove calls to it.
[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 #if HAVE_OPENSSL
53 /* SSL configuration. */
54 static char *private_key_file;
55 static char *certificate_file;
56 static char *ca_cert_file;
57 static bool bootstrap_ca_cert;
58 #endif
59
60 static unixctl_cb_func ovsdb_server_exit;
61 static unixctl_cb_func ovsdb_server_compact;
62 static unixctl_cb_func ovsdb_server_reconnect;
63
64 static void parse_options(int argc, char *argv[], char **file_namep,
65                           struct shash *remotes, char **unixctl_pathp,
66                           char **run_command);
67 static void usage(void) NO_RETURN;
68
69 static void reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
70                                 const struct ovsdb *db, struct shash *remotes);
71
72 int
73 main(int argc, char *argv[])
74 {
75     char *unixctl_path = NULL;
76     char *run_command = NULL;
77     struct unixctl_server *unixctl;
78     struct ovsdb_jsonrpc_server *jsonrpc;
79     struct shash remotes;
80     struct ovsdb_error *error;
81     struct ovsdb_file *file;
82     struct ovsdb *db;
83     struct process *run_process;
84     char *file_name;
85     bool exiting;
86     int retval;
87
88     proctitle_init(argc, argv);
89     set_program_name(argv[0]);
90     vlog_init();
91     signal(SIGPIPE, SIG_IGN);
92     process_init();
93
94     parse_options(argc, argv, &file_name, &remotes, &unixctl_path,
95                   &run_command);
96
97     die_if_already_running();
98     daemonize_start();
99
100     error = ovsdb_file_open(file_name, false, &db, &file);
101     if (error) {
102         ovs_fatal(0, "%s", ovsdb_error_to_string(error));
103     }
104
105     jsonrpc = ovsdb_jsonrpc_server_create(db);
106     reconfigure_from_db(jsonrpc, db, &remotes);
107
108     retval = unixctl_server_create(unixctl_path, &unixctl);
109     if (retval) {
110         exit(EXIT_FAILURE);
111     }
112
113     if (run_command) {
114         char *run_argv[4];
115
116         run_argv[0] = "/bin/sh";
117         run_argv[1] = "-c";
118         run_argv[2] = run_command;
119         run_argv[3] = NULL;
120
121         retval = process_start(run_argv, NULL, 0, NULL, 0, &run_process);
122         if (retval) {
123             ovs_fatal(retval, "%s: process failed to start", run_command);
124         }
125     } else {
126         run_process = NULL;
127     }
128
129     daemonize_complete();
130
131     unixctl_command_register("exit", ovsdb_server_exit, &exiting);
132     unixctl_command_register("ovsdb-server/compact", ovsdb_server_compact,
133                              file);
134     unixctl_command_register("ovsdb-server/reconnect", ovsdb_server_reconnect,
135                              jsonrpc);
136
137     exiting = false;
138     while (!exiting) {
139         reconfigure_from_db(jsonrpc, db, &remotes);
140         ovsdb_jsonrpc_server_run(jsonrpc);
141         unixctl_server_run(unixctl);
142         ovsdb_trigger_run(db, time_msec());
143         if (run_process && process_exited(run_process)) {
144             exiting = true;
145         }
146
147         ovsdb_jsonrpc_server_wait(jsonrpc);
148         unixctl_server_wait(unixctl);
149         ovsdb_trigger_wait(db, time_msec());
150         if (run_process) {
151             process_wait(run_process);
152         }
153         poll_block();
154     }
155     ovsdb_jsonrpc_server_destroy(jsonrpc);
156     ovsdb_destroy(db);
157     shash_destroy(&remotes);
158     unixctl_server_destroy(unixctl);
159
160     if (run_process && process_exited(run_process)) {
161         int status = process_status(run_process);
162         if (status) {
163             ovs_fatal(0, "%s: child exited, %s",
164                       run_command, process_status_msg(status));
165         }
166     }
167
168     return 0;
169 }
170
171 static void
172 parse_db_string_column(const struct ovsdb *db,
173                        const char *name_,
174                        const struct ovsdb_table **tablep,
175                        const struct ovsdb_column **columnp)
176 {
177     char *name, *table_name, *column_name;
178     const struct ovsdb_column *column;
179     const struct ovsdb_table *table;
180     char *save_ptr = NULL;
181
182     name = xstrdup(name_);
183     strtok_r(name, ":", &save_ptr); /* "db:" */
184     table_name = strtok_r(NULL, ",", &save_ptr);
185     column_name = strtok_r(NULL, ",", &save_ptr);
186     if (!table_name || !column_name) {
187         ovs_fatal(0, "\"%s\": invalid syntax", name_);
188     }
189
190     table = ovsdb_get_table(db, table_name);
191     if (!table) {
192         ovs_fatal(0, "\"%s\": no table named %s", name_, table_name);
193     }
194
195     column = ovsdb_table_schema_get_column(table->schema, column_name);
196     if (!column) {
197         ovs_fatal(0, "\"%s\": table \"%s\" has no column \"%s\"",
198                   name_, table_name, column_name);
199     }
200     free(name);
201
202     if (column->type.key.type != OVSDB_TYPE_STRING
203         || column->type.value.type != OVSDB_TYPE_VOID) {
204         ovs_fatal(0, "\"%s\": table \"%s\" column \"%s\" is "
205                   "not string or set of strings",
206                   name_, table->schema->name, column->name);
207     }
208
209     *columnp = column;
210     *tablep = table;
211 }
212
213 #if HAVE_OPENSSL
214 static const char *
215 query_db_string(const struct ovsdb *db, const char *name)
216 {
217     if (!name || strncmp(name, "db:", 3)) {
218         return name;
219     } else {
220         const struct ovsdb_column *column;
221         const struct ovsdb_table *table;
222         const struct ovsdb_row *row;
223
224         parse_db_string_column(db, name, &table, &column);
225
226         HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node, &table->rows) {
227             const struct ovsdb_datum *datum;
228             size_t i;
229
230             datum = &row->fields[column->index];
231             for (i = 0; i < datum->n; i++) {
232                 if (datum->keys[i].string[0]) {
233                     return datum->keys[i].string;
234                 }
235             }
236         }
237         return NULL;
238     }
239 }
240 #endif /* HAVE_OPENSSL */
241
242 static void
243 query_db_remotes(const char *name, const struct ovsdb *db,
244                  struct shash *remotes)
245 {
246     const struct ovsdb_column *column;
247     const struct ovsdb_table *table;
248     const struct ovsdb_row *row;
249
250     parse_db_string_column(db, name, &table, &column);
251
252     HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node, &table->rows) {
253         const struct ovsdb_datum *datum;
254         size_t i;
255
256         datum = &row->fields[column->index];
257         for (i = 0; i < datum->n; i++) {
258             shash_add_once(remotes, datum->keys[i].string, NULL);
259         }
260     }
261 }
262
263 /* Reconfigures ovsdb-server based on information in the database. */
264 static void
265 reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
266                     const struct ovsdb *db, struct shash *remotes)
267 {
268     struct shash resolved_remotes;
269     struct shash_node *node;
270
271     /* Configure remotes. */
272     shash_init(&resolved_remotes);
273     SHASH_FOR_EACH (node, remotes) {
274         const char *name = node->name;
275
276         if (!strncmp(name, "db:", 3)) {
277             query_db_remotes(name, db, &resolved_remotes);
278         } else {
279             shash_add_once(&resolved_remotes, name, NULL);
280         }
281     }
282     ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
283     shash_destroy(&resolved_remotes);
284
285 #if HAVE_OPENSSL
286     /* Configure SSL. */
287     stream_ssl_set_private_key_file(query_db_string(db, private_key_file));
288     stream_ssl_set_certificate_file(query_db_string(db, certificate_file));
289     stream_ssl_set_ca_cert_file(query_db_string(db, ca_cert_file),
290                                 bootstrap_ca_cert);
291 #endif
292 }
293
294 static void
295 ovsdb_server_exit(struct unixctl_conn *conn, const char *args OVS_UNUSED,
296                   void *exiting_)
297 {
298     bool *exiting = exiting_;
299     *exiting = true;
300     unixctl_command_reply(conn, 200, NULL);
301 }
302
303 static void
304 ovsdb_server_compact(struct unixctl_conn *conn, const char *args OVS_UNUSED,
305                      void *file_)
306 {
307     struct ovsdb_file *file = file_;
308     struct ovsdb_error *error;
309
310     VLOG_INFO("compacting database by user request");
311     error = ovsdb_file_compact(file);
312     if (!error) {
313         unixctl_command_reply(conn, 200, NULL);
314     } else {
315         char *s = ovsdb_error_to_string(error);
316         ovsdb_error_destroy(error);
317         unixctl_command_reply(conn, 503, s);
318         free(s);
319     }
320 }
321
322 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
323  * connections and reconnect. */
324 static void
325 ovsdb_server_reconnect(struct unixctl_conn *conn, const char *args OVS_UNUSED,
326                        void *jsonrpc_)
327 {
328     struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
329
330     ovsdb_jsonrpc_server_reconnect(jsonrpc);
331     unixctl_command_reply(conn, 200, NULL);
332 }
333
334 static void
335 parse_options(int argc, char *argv[], char **file_namep,
336               struct shash *remotes, char **unixctl_pathp,
337               char **run_command)
338 {
339     enum {
340         OPT_DUMMY = UCHAR_MAX + 1,
341         OPT_REMOTE,
342         OPT_UNIXCTL,
343         OPT_RUN,
344         OPT_BOOTSTRAP_CA_CERT,
345         VLOG_OPTION_ENUMS,
346         LEAK_CHECKER_OPTION_ENUMS
347     };
348     static struct option long_options[] = {
349         {"remote",      required_argument, 0, OPT_REMOTE},
350         {"unixctl",     required_argument, 0, OPT_UNIXCTL},
351         {"run",         required_argument, 0, OPT_RUN},
352         {"help",        no_argument, 0, 'h'},
353         {"version",     no_argument, 0, 'V'},
354         DAEMON_LONG_OPTIONS,
355         VLOG_LONG_OPTIONS,
356         LEAK_CHECKER_LONG_OPTIONS,
357 #ifdef HAVE_OPENSSL
358         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
359         {"private-key", required_argument, 0, 'p'},
360         {"certificate", required_argument, 0, 'c'},
361         {"ca-cert",     required_argument, 0, 'C'},
362 #endif
363         {0, 0, 0, 0},
364     };
365     char *short_options = long_options_to_short_options(long_options);
366
367     shash_init(remotes);
368     for (;;) {
369         int c;
370
371         c = getopt_long(argc, argv, short_options, long_options, NULL);
372         if (c == -1) {
373             break;
374         }
375
376         switch (c) {
377         case OPT_REMOTE:
378             shash_add_once(remotes, optarg, NULL);
379             break;
380
381         case OPT_UNIXCTL:
382             *unixctl_pathp = optarg;
383             break;
384
385         case OPT_RUN:
386             *run_command = optarg;
387             break;
388
389         case 'h':
390             usage();
391
392         case 'V':
393             OVS_PRINT_VERSION(0, 0);
394             exit(EXIT_SUCCESS);
395
396         VLOG_OPTION_HANDLERS
397         DAEMON_OPTION_HANDLERS
398         LEAK_CHECKER_OPTION_HANDLERS
399
400 #ifdef HAVE_OPENSSL
401         case 'p':
402             private_key_file = optarg;
403             break;
404
405         case 'c':
406             certificate_file = optarg;
407             break;
408
409         case 'C':
410             ca_cert_file = optarg;
411             bootstrap_ca_cert = false;
412             break;
413
414         case OPT_BOOTSTRAP_CA_CERT:
415             ca_cert_file = optarg;
416             bootstrap_ca_cert = true;
417             break;
418 #endif
419
420         case '?':
421             exit(EXIT_FAILURE);
422
423         default:
424             abort();
425         }
426     }
427     free(short_options);
428
429     argc -= optind;
430     argv += optind;
431
432     if (argc > 1) {
433         ovs_fatal(0, "database file is only non-option argument; "
434                 "use --help for usage");
435     } else if (argc < 1) {
436         ovs_fatal(0, "missing database file argument; use --help for usage");
437     }
438
439     *file_namep = argv[0];
440 }
441
442 static void
443 usage(void)
444 {
445     printf("%s: Open vSwitch database server\n"
446            "usage: %s [OPTIONS] DATABASE\n"
447            "where DATABASE is a database file in ovsdb format.\n",
448            program_name, program_name);
449     printf("\nJSON-RPC options (may be specified any number of times):\n"
450            "  --remote=REMOTE         connect or listen to REMOTE\n");
451     stream_usage("JSON-RPC", true, true, true);
452     daemon_usage();
453     vlog_usage();
454     printf("\nOther options:\n"
455            "  --run COMMAND           run COMMAND as subprocess then exit\n"
456            "  --unixctl=SOCKET        override default control socket name\n"
457            "  -h, --help              display this help message\n"
458            "  -V, --version           display version information\n");
459     leak_checker_usage();
460     exit(EXIT_SUCCESS);
461 }