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