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