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