ovn-controller: Make integration bridge config part of general context.
[cascardo/ovs.git] / ovn / controller / ovn-controller.c
1 /* Copyright (c) 2015 Nicira, Inc.
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 <errno.h>
19 #include <getopt.h>
20 #include <signal.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "command-line.h"
25 #include "compiler.h"
26 #include "daemon.h"
27 #include "dirs.h"
28 #include "openvswitch/vconn.h"
29 #include "openvswitch/vlog.h"
30 #include "ovn/lib/ovn-sb-idl.h"
31 #include "poll-loop.h"
32 #include "fatal-signal.h"
33 #include "lib/vswitch-idl.h"
34 #include "smap.h"
35 #include "stream.h"
36 #include "stream-ssl.h"
37 #include "unixctl.h"
38 #include "util.h"
39
40 #include "ovn-controller.h"
41 #include "bindings.h"
42 #include "chassis.h"
43
44 VLOG_DEFINE_THIS_MODULE(main);
45
46 static unixctl_cb_func ovn_controller_exit;
47
48 #define DEFAULT_BRIDGE_NAME "br-int"
49
50 static void parse_options(int argc, char *argv[]);
51 OVS_NO_RETURN static void usage(void);
52
53 static char *ovs_remote;
54 static char *ovnsb_remote;
55
56
57 static void
58 get_initial_snapshot(struct ovsdb_idl *idl)
59 {
60     while (1) {
61         ovsdb_idl_run(idl);
62         if (ovsdb_idl_has_ever_connected(idl)) {
63             return;
64         }
65         ovsdb_idl_wait(idl);
66         poll_block();
67     }
68 }
69
70 static const struct ovsrec_bridge *
71 get_bridge(struct controller_ctx *ctx, const char *name)
72 {
73     const struct ovsrec_bridge *br;
74
75     OVSREC_BRIDGE_FOR_EACH(br, ctx->ovs_idl) {
76         if (!strcmp(br->name, name)) {
77             return br;
78         }
79     }
80
81     return NULL;
82 }
83
84 /* Retrieve the OVN integration bridge from the "external-ids:ovn-bridge"
85  * key, the remote location from the "external-ids:ovn-remote" key, and
86  * the chassis name from the "external-ids:system-id" key in the
87  * Open_vSwitch table of the OVS database instance. */
88 static void
89 get_core_config(struct controller_ctx *ctx)
90 {
91     const struct ovsrec_open_vswitch *cfg;
92
93     cfg = ovsrec_open_vswitch_first(ctx->ovs_idl);
94     if (!cfg) {
95         VLOG_ERR("No Open_vSwitch row defined.");
96         ovsdb_idl_destroy(ctx->ovs_idl);
97         exit(EXIT_FAILURE);
98     }
99
100     while (1) {
101         const struct ovsrec_bridge *br_int;
102         const char *remote, *system_id;
103
104         ovsdb_idl_run(ctx->ovs_idl);
105
106         ctx->br_int_name = smap_get(&cfg->external_ids, "ovn-bridge");
107         if (!ctx->br_int_name) {
108             ctx->br_int_name = DEFAULT_BRIDGE_NAME;
109         }
110
111         br_int = get_bridge(ctx, ctx->br_int_name);
112         if (!br_int) {
113             VLOG_INFO("Integration bridge '%s' does not exist.  Waiting...",
114                       ctx->br_int_name);
115             goto try_again;
116         }
117
118         /* xxx This does not support changing OVN Southbound OVSDB mid-run. */
119         remote = smap_get(&cfg->external_ids, "ovn-remote");
120         if (!remote) {
121             VLOG_INFO("OVN OVSDB remote not specified.  Waiting...");
122             goto try_again;
123         }
124
125         system_id = smap_get(&cfg->external_ids, "system-id");
126         if (!system_id) {
127             VLOG_INFO("system-id not specified.  Waiting...");
128             goto try_again;
129         }
130
131         ovnsb_remote = xstrdup(remote);
132         ctx->chassis_name = xstrdup(system_id);
133         return;
134
135 try_again:
136         ovsdb_idl_wait(ctx->ovs_idl);
137         poll_block();
138     }
139
140 }
141
142 int
143 main(int argc, char *argv[])
144 {
145     struct unixctl_server *unixctl;
146     struct controller_ctx ctx = { .chassis_name = NULL };
147     bool exiting;
148     int retval;
149
150     ovs_cmdl_proctitle_init(argc, argv);
151     set_program_name(argv[0]);
152     parse_options(argc, argv);
153     fatal_ignore_sigpipe();
154
155     daemonize_start();
156
157     retval = unixctl_server_create(NULL, &unixctl);
158     if (retval) {
159         exit(EXIT_FAILURE);
160     }
161     unixctl_command_register("exit", "", 0, 0, ovn_controller_exit, &exiting);
162
163     daemonize_complete();
164
165     ovsrec_init();
166     sbrec_init();
167
168     /* Connect to OVS OVSDB instance.  We do not monitor all tables by
169      * default, so modules must register their interest explicitly.  */
170     ctx.ovs_idl = ovsdb_idl_create(ovs_remote, &ovsrec_idl_class, false, true);
171
172     /* Register interest in "external_ids" column in "Open_vSwitch" table,
173      * since we'll need to get the OVN OVSDB remote. */
174     ovsdb_idl_add_table(ctx.ovs_idl, &ovsrec_table_open_vswitch);
175     ovsdb_idl_add_column(ctx.ovs_idl, &ovsrec_open_vswitch_col_external_ids);
176
177     chassis_init(&ctx);
178     bindings_init(&ctx);
179
180     get_initial_snapshot(ctx.ovs_idl);
181
182     get_core_config(&ctx);
183
184     ctx.ovnsb_idl = ovsdb_idl_create(ovnsb_remote, &sbrec_idl_class,
185                                      true, true);
186
187     get_initial_snapshot(ctx.ovnsb_idl);
188
189     exiting = false;
190     while (!exiting) {
191         ovsdb_idl_run(ctx.ovs_idl);
192         ovsdb_idl_run(ctx.ovnsb_idl);
193
194         /* xxx If run into any surprising changes, we exit.  We should
195          * xxx handle this more gracefully. */
196         ctx.br_int = get_bridge(&ctx, ctx.br_int_name);
197         if (!ctx.br_int) {
198             VLOG_ERR("Integration bridge '%s' disappeared",
199                      ctx.br_int_name);
200             retval = EXIT_FAILURE;
201             break;
202         }
203
204         if (!ovsdb_idl_is_alive(ctx.ovnsb_idl)) {
205             int retval = ovsdb_idl_get_last_error(ctx.ovnsb_idl);
206             VLOG_ERR("%s: database connection failed (%s)",
207                      ovnsb_remote, ovs_retval_to_string(retval));
208             retval = EXIT_FAILURE;
209             break;
210         }
211
212         if (!ovsdb_idl_is_alive(ctx.ovs_idl)) {
213             int retval = ovsdb_idl_get_last_error(ctx.ovs_idl);
214             VLOG_ERR("%s: database connection failed (%s)",
215                      ovs_remote, ovs_retval_to_string(retval));
216             retval = EXIT_FAILURE;
217             break;
218         }
219
220         chassis_run(&ctx);
221         bindings_run(&ctx);
222         unixctl_server_run(unixctl);
223
224         unixctl_server_wait(unixctl);
225         if (exiting) {
226             poll_immediate_wake();
227         }
228
229         ovsdb_idl_wait(ctx.ovs_idl);
230         ovsdb_idl_wait(ctx.ovnsb_idl);
231         poll_block();
232     }
233
234     unixctl_server_destroy(unixctl);
235     bindings_destroy(&ctx);
236     chassis_destroy(&ctx);
237
238     ovsdb_idl_destroy(ctx.ovs_idl);
239     ovsdb_idl_destroy(ctx.ovnsb_idl);
240
241     exit(retval);
242 }
243
244 static void
245 parse_options(int argc, char *argv[])
246 {
247     enum {
248         OPT_PEER_CA_CERT = UCHAR_MAX + 1,
249         VLOG_OPTION_ENUMS,
250         DAEMON_OPTION_ENUMS
251     };
252
253     static struct option long_options[] = {
254         {"help", no_argument, NULL, 'h'},
255         {"version", no_argument, NULL, 'V'},
256         VLOG_LONG_OPTIONS,
257         DAEMON_LONG_OPTIONS,
258         STREAM_SSL_LONG_OPTIONS,
259         {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
260         {NULL, 0, NULL, 0}
261     };
262     char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
263
264     for (;;) {
265         int c;
266
267         c = getopt_long(argc, argv, short_options, long_options, NULL);
268         if (c == -1) {
269             break;
270         }
271
272         switch (c) {
273         case 'h':
274             usage();
275
276         case 'V':
277             ovs_print_version(OFP13_VERSION, OFP13_VERSION);
278             exit(EXIT_SUCCESS);
279
280         VLOG_OPTION_HANDLERS
281         DAEMON_OPTION_HANDLERS
282         STREAM_SSL_OPTION_HANDLERS
283
284         case OPT_PEER_CA_CERT:
285             stream_ssl_set_peer_ca_cert_file(optarg);
286             break;
287
288         case '?':
289             exit(EXIT_FAILURE);
290
291         default:
292             abort();
293         }
294     }
295     free(short_options);
296
297     argc -= optind;
298     argv += optind;
299
300     if (argc == 0) {
301         ovs_remote = xasprintf("unix:%s/db.sock", ovs_rundir());
302     } else if (argc == 1) {
303         ovs_remote = argv[0];
304     } else {
305         VLOG_FATAL("exactly zero or one non-option argument required; "
306                    "use --help for usage");
307     }
308 }
309
310 static void
311 usage(void)
312 {
313     printf("%s: OVN controller\n"
314            "usage %s [OPTIONS] [OVS-DATABASE]\n"
315            "where OVS-DATABASE is a socket on which the OVS OVSDB server is listening.\n",
316                program_name, program_name);
317     stream_usage("OVS-DATABASE", true, false, false);
318     daemon_usage();
319     vlog_usage();
320     printf("\nOther options:\n"
321            "  -h, --help              display this help message\n"
322            "  -V, --version           display version information\n");
323     exit(EXIT_SUCCESS);
324 }
325
326 static void
327 ovn_controller_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
328              const char *argv[] OVS_UNUSED, void *exiting_)
329 {
330     bool *exiting = exiting_;
331     *exiting = true;
332
333     unixctl_command_reply(conn, NULL);
334 }