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