ovn-controller: Tolerate missing integration bridge.
[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 "ovn-controller.h"
19
20 #include <errno.h>
21 #include <getopt.h>
22 #include <signal.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "command-line.h"
27 #include "compiler.h"
28 #include "daemon.h"
29 #include "dirs.h"
30 #include "openvswitch/vconn.h"
31 #include "openvswitch/vlog.h"
32 #include "ovn/lib/ovn-sb-idl.h"
33 #include "poll-loop.h"
34 #include "fatal-signal.h"
35 #include "lib/vswitch-idl.h"
36 #include "smap.h"
37 #include "stream.h"
38 #include "stream-ssl.h"
39 #include "unixctl.h"
40 #include "util.h"
41
42 #include "ofctrl.h"
43 #include "binding.h"
44 #include "chassis.h"
45 #include "encaps.h"
46 #include "physical.h"
47 #include "pipeline.h"
48
49 VLOG_DEFINE_THIS_MODULE(main);
50
51 static unixctl_cb_func ovn_controller_exit;
52
53 #define DEFAULT_BRIDGE_NAME "br-int"
54
55 static void parse_options(int argc, char *argv[]);
56 OVS_NO_RETURN static void usage(void);
57
58 static char *ovs_remote;
59 static char *ovnsb_remote;
60
61
62 static void
63 get_initial_snapshot(struct ovsdb_idl *idl)
64 {
65     while (1) {
66         ovsdb_idl_run(idl);
67         if (ovsdb_idl_has_ever_connected(idl)) {
68             return;
69         }
70         ovsdb_idl_wait(idl);
71         poll_block();
72     }
73 }
74
75 static const struct ovsrec_bridge *
76 get_bridge(struct controller_ctx *ctx, const char *name)
77 {
78     const struct ovsrec_bridge *br;
79
80     OVSREC_BRIDGE_FOR_EACH(br, ctx->ovs_idl) {
81         if (!strcmp(br->name, name)) {
82             return br;
83         }
84     }
85
86     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
87     VLOG_WARN_RL(&rl, "%s: integration bridge does not exist", name);
88     return NULL;
89 }
90
91 /* Retrieve the OVN integration bridge from the "external-ids:ovn-bridge"
92  * key, the remote location from the "external-ids:ovn-remote" key, and
93  * the chassis name from the "external-ids:system-id" key in the
94  * Open_vSwitch table of the OVS database instance.
95  *
96  * xxx ovn-controller does not support changing any of these mid-run,
97  * xxx but that should be addressed later. */
98 static void
99 get_core_config(struct controller_ctx *ctx, char **br_int_namep,
100                 char **chassis_idp)
101 {
102     while (1) {
103         ovsdb_idl_run(ctx->ovs_idl);
104
105         const struct ovsrec_open_vswitch *cfg;
106         cfg = ovsrec_open_vswitch_first(ctx->ovs_idl);
107         if (!cfg) {
108             VLOG_ERR("No Open_vSwitch row defined.");
109             ovsdb_idl_destroy(ctx->ovs_idl);
110             exit(EXIT_FAILURE);
111         }
112
113         const char *remote, *system_id, *br_int_name;
114
115         br_int_name = smap_get(&cfg->external_ids, "ovn-bridge");
116         if (!br_int_name) {
117             br_int_name = DEFAULT_BRIDGE_NAME;
118         }
119
120         remote = smap_get(&cfg->external_ids, "ovn-remote");
121         if (!remote) {
122             VLOG_INFO("OVN OVSDB remote not specified.  Waiting...");
123             goto try_again;
124         }
125
126         system_id = smap_get(&cfg->external_ids, "system-id");
127         if (!system_id) {
128             VLOG_INFO("system-id not specified.  Waiting...");
129             goto try_again;
130         }
131
132         ovnsb_remote = xstrdup(remote);
133         *chassis_idp = xstrdup(system_id);
134         *br_int_namep = xstrdup(br_int_name);
135         return;
136
137 try_again:
138         ovsdb_idl_wait(ctx->ovs_idl);
139         poll_block();
140     }
141
142 }
143
144 struct idl_loop {
145     struct ovsdb_idl *idl;
146     unsigned int skip_seqno;
147
148     struct ovsdb_idl_txn *committing_txn;
149     unsigned int precommit_seqno;
150
151     struct ovsdb_idl_txn *open_txn;
152 };
153
154 #define IDL_LOOP_INITIALIZER(IDL) { .idl = (IDL) }
155
156 static void
157 idl_loop_destroy(struct idl_loop *loop)
158 {
159     if (loop) {
160         ovsdb_idl_destroy(loop->idl);
161     }
162 }
163
164 static struct ovsdb_idl_txn *
165 idl_loop_run(struct idl_loop *loop)
166 {
167     ovsdb_idl_run(loop->idl);
168     loop->open_txn = (loop->committing_txn
169                       || ovsdb_idl_get_seqno(loop->idl) == loop->skip_seqno
170                       ? NULL
171                       : ovsdb_idl_txn_create(loop->idl));
172     return loop->open_txn;
173 }
174
175 static void
176 idl_loop_commit_and_wait(struct idl_loop *loop)
177 {
178     if (loop->open_txn) {
179         loop->committing_txn = loop->open_txn;
180         loop->open_txn = NULL;
181
182         loop->precommit_seqno = ovsdb_idl_get_seqno(loop->idl);
183     }
184
185     struct ovsdb_idl_txn *txn = loop->committing_txn;
186     if (txn) {
187         enum ovsdb_idl_txn_status status = ovsdb_idl_txn_commit(txn);
188         if (status != TXN_INCOMPLETE) {
189             switch (status) {
190             case TXN_TRY_AGAIN:
191                 /* We want to re-evaluate the database when it's changed from
192                  * the contents that it had when we started the commit.  (That
193                  * might have already happened.) */
194                 loop->skip_seqno = loop->precommit_seqno;
195                 if (ovsdb_idl_get_seqno(loop->idl) != loop->skip_seqno) {
196                     poll_immediate_wake();
197                 }
198                 break;
199
200             case TXN_SUCCESS:
201                 /* If the database has already changed since we started the
202                  * commit, re-evaluate it immediately to avoid missing a change
203                  * for a while. */
204                 if (ovsdb_idl_get_seqno(loop->idl) != loop->precommit_seqno) {
205                     poll_immediate_wake();
206                 }
207                 break;
208
209             case TXN_UNCHANGED:
210             case TXN_ABORTED:
211             case TXN_NOT_LOCKED:
212             case TXN_ERROR:
213                 break;
214
215             case TXN_UNCOMMITTED:
216             case TXN_INCOMPLETE:
217                 OVS_NOT_REACHED();
218
219             }
220             ovsdb_idl_txn_destroy(txn);
221             loop->committing_txn = NULL;
222         }
223     }
224
225     ovsdb_idl_wait(loop->idl);
226 }
227
228 int
229 main(int argc, char *argv[])
230 {
231     struct unixctl_server *unixctl;
232     struct controller_ctx ctx = { .ovs_idl = NULL };
233     bool exiting;
234     int retval;
235
236     ovs_cmdl_proctitle_init(argc, argv);
237     set_program_name(argv[0]);
238     parse_options(argc, argv);
239     fatal_ignore_sigpipe();
240
241     daemonize_start();
242
243     retval = unixctl_server_create(NULL, &unixctl);
244     if (retval) {
245         exit(EXIT_FAILURE);
246     }
247     unixctl_command_register("exit", "", 0, 0, ovn_controller_exit, &exiting);
248
249     daemonize_complete();
250
251     ovsrec_init();
252     sbrec_init();
253
254     ofctrl_init();
255
256     /* Connect to OVS OVSDB instance.  We do not monitor all tables by
257      * default, so modules must register their interest explicitly.  */
258     ctx.ovs_idl = ovsdb_idl_create(ovs_remote, &ovsrec_idl_class, false, true);
259
260     /* Register interest in "external_ids" column in "Open_vSwitch" table,
261      * since we'll need to get the OVN OVSDB remote. */
262     ovsdb_idl_add_table(ctx.ovs_idl, &ovsrec_table_open_vswitch);
263     ovsdb_idl_add_column(ctx.ovs_idl, &ovsrec_open_vswitch_col_external_ids);
264
265     chassis_init(&ctx);
266     encaps_init(&ctx);
267     binding_init(&ctx);
268     physical_init(&ctx);
269     pipeline_init();
270
271     get_initial_snapshot(ctx.ovs_idl);
272
273     char *br_int_name, *chassis_id;
274     get_core_config(&ctx, &br_int_name, &chassis_id);
275
276     ctx.ovnsb_idl = ovsdb_idl_create(ovnsb_remote, &sbrec_idl_class,
277                                      true, true);
278     get_initial_snapshot(ctx.ovnsb_idl);
279
280     struct idl_loop ovnsb_idl_loop = IDL_LOOP_INITIALIZER(ctx.ovnsb_idl);
281     struct idl_loop ovs_idl_loop = IDL_LOOP_INITIALIZER(ctx.ovs_idl);
282
283     /* Main loop. */
284     exiting = false;
285     while (!exiting) {
286         ctx.ovnsb_idl_txn = idl_loop_run(&ovnsb_idl_loop);
287         ctx.ovs_idl_txn = idl_loop_run(&ovs_idl_loop);
288
289         const struct ovsrec_bridge *br_int = get_bridge(&ctx, br_int_name);
290
291         chassis_run(&ctx, chassis_id);
292         encaps_run(&ctx, br_int, chassis_id);
293         binding_run(&ctx, br_int, chassis_id);
294
295         if (br_int) {
296             struct hmap flow_table = HMAP_INITIALIZER(&flow_table);
297             pipeline_run(&ctx, &flow_table);
298             physical_run(&ctx, br_int, chassis_id, &flow_table);
299             ofctrl_run(br_int, &flow_table);
300             hmap_destroy(&flow_table);
301         }
302
303         unixctl_server_run(unixctl);
304
305         unixctl_server_wait(unixctl);
306         if (exiting) {
307             poll_immediate_wake();
308         }
309
310         idl_loop_commit_and_wait(&ovnsb_idl_loop);
311         idl_loop_commit_and_wait(&ovs_idl_loop);
312
313         if (br_int) {
314             ofctrl_wait();
315         }
316         poll_block();
317     }
318
319     /* It's time to exit.  Clean up the databases. */
320     bool done = false;
321     while (!done) {
322         ctx.ovnsb_idl_txn = idl_loop_run(&ovnsb_idl_loop);
323         ctx.ovs_idl_txn = idl_loop_run(&ovs_idl_loop);
324
325         const struct ovsrec_bridge *br_int = get_bridge(&ctx, br_int_name);
326
327         /* Run all of the cleanup functions, even if one of them returns false.
328          * We're done if all of them return true. */
329         done = binding_cleanup(&ctx, chassis_id);
330         done = chassis_cleanup(&ctx, chassis_id) && done;
331         done = encaps_cleanup(&ctx, br_int) && done;
332         if (done) {
333             poll_immediate_wake();
334         }
335
336         idl_loop_commit_and_wait(&ovnsb_idl_loop);
337         idl_loop_commit_and_wait(&ovs_idl_loop);
338         poll_block();
339     }
340
341     unixctl_server_destroy(unixctl);
342     pipeline_destroy(&ctx);
343     ofctrl_destroy();
344
345     idl_loop_destroy(&ovs_idl_loop);
346     idl_loop_destroy(&ovnsb_idl_loop);
347
348     free(br_int_name);
349     free(chassis_id);
350     free(ovnsb_remote);
351     free(ovs_remote);
352
353     exit(retval);
354 }
355
356 static void
357 parse_options(int argc, char *argv[])
358 {
359     enum {
360         OPT_PEER_CA_CERT = UCHAR_MAX + 1,
361         VLOG_OPTION_ENUMS,
362         DAEMON_OPTION_ENUMS
363     };
364
365     static struct option long_options[] = {
366         {"help", no_argument, NULL, 'h'},
367         {"version", no_argument, NULL, 'V'},
368         VLOG_LONG_OPTIONS,
369         DAEMON_LONG_OPTIONS,
370         STREAM_SSL_LONG_OPTIONS,
371         {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
372         {NULL, 0, NULL, 0}
373     };
374     char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
375
376     for (;;) {
377         int c;
378
379         c = getopt_long(argc, argv, short_options, long_options, NULL);
380         if (c == -1) {
381             break;
382         }
383
384         switch (c) {
385         case 'h':
386             usage();
387
388         case 'V':
389             ovs_print_version(OFP13_VERSION, OFP13_VERSION);
390             exit(EXIT_SUCCESS);
391
392         VLOG_OPTION_HANDLERS
393         DAEMON_OPTION_HANDLERS
394         STREAM_SSL_OPTION_HANDLERS
395
396         case OPT_PEER_CA_CERT:
397             stream_ssl_set_peer_ca_cert_file(optarg);
398             break;
399
400         case '?':
401             exit(EXIT_FAILURE);
402
403         default:
404             abort();
405         }
406     }
407     free(short_options);
408
409     argc -= optind;
410     argv += optind;
411
412     if (argc == 0) {
413         ovs_remote = xasprintf("unix:%s/db.sock", ovs_rundir());
414     } else if (argc == 1) {
415         ovs_remote = xstrdup(argv[0]);
416     } else {
417         VLOG_FATAL("exactly zero or one non-option argument required; "
418                    "use --help for usage");
419     }
420 }
421
422 static void
423 usage(void)
424 {
425     printf("%s: OVN controller\n"
426            "usage %s [OPTIONS] [OVS-DATABASE]\n"
427            "where OVS-DATABASE is a socket on which the OVS OVSDB server is listening.\n",
428                program_name, program_name);
429     stream_usage("OVS-DATABASE", true, false, false);
430     daemon_usage();
431     vlog_usage();
432     printf("\nOther options:\n"
433            "  -h, --help              display this help message\n"
434            "  -V, --version           display version information\n");
435     exit(EXIT_SUCCESS);
436 }
437
438 static void
439 ovn_controller_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
440              const char *argv[] OVS_UNUSED, void *exiting_)
441 {
442     bool *exiting = exiting_;
443     *exiting = true;
444
445     unixctl_command_reply(conn, NULL);
446 }