02700df466651f38fefa9caa55f10d22f672bace
[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 "fault.h"
29 #include "file.h"
30 #include "json.h"
31 #include "jsonrpc.h"
32 #include "jsonrpc-server.h"
33 #include "leak-checker.h"
34 #include "list.h"
35 #include "ovsdb-data.h"
36 #include "ovsdb-types.h"
37 #include "ovsdb-error.h"
38 #include "poll-loop.h"
39 #include "process.h"
40 #include "row.h"
41 #include "stream-ssl.h"
42 #include "stream.h"
43 #include "svec.h"
44 #include "table.h"
45 #include "timeval.h"
46 #include "trigger.h"
47 #include "util.h"
48 #include "unixctl.h"
49
50 #include "vlog.h"
51 #define THIS_MODULE VLM_ovsdb_server
52
53 static unixctl_cb_func ovsdb_server_exit;
54
55 static void parse_options(int argc, char *argv[], char **file_namep,
56                           struct shash *remotes, char **unixctl_pathp);
57 static void usage(void) NO_RETURN;
58
59 static void set_remotes(struct ovsdb_jsonrpc_server *jsonrpc,
60                         const struct ovsdb *db, struct shash *remotes);
61
62 int
63 main(int argc, char *argv[])
64 {
65     char *unixctl_path = NULL;
66     struct unixctl_server *unixctl;
67     struct ovsdb_jsonrpc_server *jsonrpc;
68     struct shash remotes;
69     struct ovsdb_error *error;
70     struct ovsdb *db;
71     char *file_name;
72     bool exiting;
73     int retval;
74
75     set_program_name(argv[0]);
76     register_fault_handlers();
77     time_init();
78     vlog_init();
79     signal(SIGPIPE, SIG_IGN);
80     process_init();
81
82     parse_options(argc, argv, &file_name, &remotes, &unixctl_path);
83
84     die_if_already_running();
85     daemonize_start();
86
87     error = ovsdb_file_open(file_name, false, &db);
88     if (error) {
89         ovs_fatal(0, "%s", ovsdb_error_to_string(error));
90     }
91
92     jsonrpc = ovsdb_jsonrpc_server_create(db);
93     set_remotes(jsonrpc, db, &remotes);
94
95     retval = unixctl_server_create(unixctl_path, &unixctl);
96     if (retval) {
97         ovs_fatal(retval, "could not listen for control connections");
98     }
99
100     daemonize_complete();
101
102     unixctl_command_register("exit", ovsdb_server_exit, &exiting);
103
104     exiting = false;
105     while (!exiting) {
106         set_remotes(jsonrpc, db, &remotes);
107         ovsdb_jsonrpc_server_run(jsonrpc);
108         unixctl_server_run(unixctl);
109         ovsdb_trigger_run(db, time_msec());
110
111         ovsdb_jsonrpc_server_wait(jsonrpc);
112         unixctl_server_wait(unixctl);
113         ovsdb_trigger_wait(db, time_msec());
114         poll_block();
115     }
116
117     return 0;
118 }
119
120 static void
121 query_db_remotes(const char *name_, const struct ovsdb *db,
122                  struct shash *remotes)
123 {
124     char *name, *db_prefix, *table_name, *column_name;
125     const struct ovsdb_column *column;
126     const struct ovsdb_table *table;
127     const struct ovsdb_row *row;
128     char *save_ptr = NULL;
129
130     name = xstrdup(name_);
131     db_prefix = strtok_r(name, ":", &save_ptr);
132     table_name = strtok_r(NULL, ",", &save_ptr);
133     column_name = strtok_r(NULL, ",", &save_ptr);
134     if (!table_name || !column_name) {
135         ovs_fatal(0, "remote \"%s\": invalid syntax", name_);
136     }
137
138     table = ovsdb_get_table(db, table_name);
139     if (!table) {
140         ovs_fatal(0, "remote \"%s\": no table named %s", name_, table_name);
141     }
142
143     column = ovsdb_table_schema_get_column(table->schema, column_name);
144     if (!column) {
145         ovs_fatal(0, "remote \"%s\": table \"%s\" has no column \"%s\"",
146                   name_, table_name, column_name);
147     }
148
149     if (column->type.key_type != OVSDB_TYPE_STRING
150         || column->type.value_type != OVSDB_TYPE_VOID) {
151         ovs_fatal(0, "remote \"%s\": type of table \"%s\" column \"%s\" is "
152                   "not string or set of strings",
153                   name_, table_name, column_name);
154     }
155
156     HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node, &table->rows) {
157         const struct ovsdb_datum *datum;
158         size_t i;
159
160         datum = &row->fields[column->index];
161         for (i = 0; i < datum->n; i++) {
162             shash_add_once(remotes, datum->keys[i].string, NULL);
163         }
164     }
165
166     free(name);
167 }
168
169 static void
170 set_remotes(struct ovsdb_jsonrpc_server *jsonrpc,
171             const struct ovsdb *db, struct shash *remotes)
172 {
173     struct shash resolved_remotes;
174     struct shash_node *node;
175
176     shash_init(&resolved_remotes);
177     SHASH_FOR_EACH (node, remotes) {
178         const char *name = node->name;
179
180         if (!strncmp(name, "db:", 3)) {
181             query_db_remotes(name, db, &resolved_remotes);
182         } else {
183             shash_add_once(&resolved_remotes, name, NULL);
184         }
185     }
186     ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
187     shash_destroy(&resolved_remotes);
188 }
189
190
191 static void
192 ovsdb_server_exit(struct unixctl_conn *conn, const char *args UNUSED,
193                   void *exiting_)
194 {
195     bool *exiting = exiting_;
196     *exiting = true;
197     unixctl_command_reply(conn, 200, NULL);
198 }
199
200 static void
201 parse_options(int argc, char *argv[], char **file_namep,
202               struct shash *remotes, char **unixctl_pathp)
203 {
204     enum {
205         OPT_DUMMY = UCHAR_MAX + 1,
206         OPT_REMOTE,
207         OPT_UNIXCTL,
208         OPT_BOOTSTRAP_CA_CERT,
209         VLOG_OPTION_ENUMS,
210         LEAK_CHECKER_OPTION_ENUMS
211     };
212     static struct option long_options[] = {
213         {"remote",      required_argument, 0, OPT_REMOTE},
214         {"unixctl",     required_argument, 0, OPT_UNIXCTL},
215         {"help",        no_argument, 0, 'h'},
216         {"version",     no_argument, 0, 'V'},
217         DAEMON_LONG_OPTIONS,
218         VLOG_LONG_OPTIONS,
219         LEAK_CHECKER_LONG_OPTIONS,
220 #ifdef HAVE_OPENSSL
221         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
222         STREAM_SSL_LONG_OPTIONS
223 #endif
224         {0, 0, 0, 0},
225     };
226     char *short_options = long_options_to_short_options(long_options);
227
228     shash_init(remotes);
229     for (;;) {
230         int c;
231
232         c = getopt_long(argc, argv, short_options, long_options, NULL);
233         if (c == -1) {
234             break;
235         }
236
237         switch (c) {
238         case OPT_REMOTE:
239             shash_add_once(remotes, optarg, NULL);
240             break;
241
242         case OPT_UNIXCTL:
243             *unixctl_pathp = optarg;
244             break;
245
246         case 'h':
247             usage();
248
249         case 'V':
250             OVS_PRINT_VERSION(0, 0);
251             exit(EXIT_SUCCESS);
252
253         VLOG_OPTION_HANDLERS
254         DAEMON_OPTION_HANDLERS
255         LEAK_CHECKER_OPTION_HANDLERS
256
257 #ifdef HAVE_OPENSSL
258         STREAM_SSL_OPTION_HANDLERS
259
260         case OPT_BOOTSTRAP_CA_CERT:
261             stream_ssl_set_ca_cert_file(optarg, true);
262             break;
263 #endif
264
265
266         case '?':
267             exit(EXIT_FAILURE);
268
269         default:
270             abort();
271         }
272     }
273     free(short_options);
274
275     argc -= optind;
276     argv += optind;
277
278     if (argc > 1) {
279         ovs_fatal(0, "database file is only non-option argument; "
280                 "use --help for usage");
281     } else if (argc < 1) {
282         ovs_fatal(0, "missing database file argument; use --help for usage");
283     }
284
285     *file_namep = argv[0];
286 }
287
288 static void
289 usage(void)
290 {
291     printf("%s: Open vSwitch database server\n"
292            "usage: %s [OPTIONS] DATABASE\n"
293            "where DATABASE is a database file in ovsdb format.\n",
294            program_name, program_name);
295     printf("\nJSON-RPC options (may be specified any number of times):\n"
296            "  --remote=REMOTE         connect or listen to REMOTE\n");
297     stream_usage("JSON-RPC", true, true, true);
298     daemon_usage();
299     vlog_usage();
300     printf("\nOther options:\n"
301            "  -h, --help              display this help message\n"
302            "  -V, --version           display version information\n");
303     leak_checker_usage();
304     exit(EXIT_SUCCESS);
305 }