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