patch: Bail out earlier if OVS IDL transactions cannot be executed.
[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 "dynamic-string.h"
31 #include "openvswitch/vconn.h"
32 #include "openvswitch/vlog.h"
33 #include "ovn/lib/ovn-sb-idl.h"
34 #include "poll-loop.h"
35 #include "fatal-signal.h"
36 #include "lib/vswitch-idl.h"
37 #include "smap.h"
38 #include "stream.h"
39 #include "stream-ssl.h"
40 #include "unixctl.h"
41 #include "util.h"
42
43 #include "ofctrl.h"
44 #include "binding.h"
45 #include "chassis.h"
46 #include "encaps.h"
47 #include "patch.h"
48 #include "physical.h"
49 #include "lflow.h"
50
51 VLOG_DEFINE_THIS_MODULE(main);
52
53 static unixctl_cb_func ovn_controller_exit;
54 static unixctl_cb_func ct_zone_list;
55
56 #define DEFAULT_BRIDGE_NAME "br-int"
57
58 static void parse_options(int argc, char *argv[]);
59 OVS_NO_RETURN static void usage(void);
60
61 static char *ovs_remote;
62
63 const struct sbrec_chassis *
64 get_chassis(struct ovsdb_idl *ovnsb_idl, const char *chassis_id)
65 {
66     const struct sbrec_chassis *chassis_rec;
67
68     SBREC_CHASSIS_FOR_EACH(chassis_rec, ovnsb_idl) {
69         if (!strcmp(chassis_rec->name, chassis_id)) {
70             break;
71         }
72     }
73
74     return chassis_rec;
75 }
76
77 uint32_t
78 get_tunnel_type(const char *name)
79 {
80     if (!strcmp(name, "geneve")) {
81         return GENEVE;
82     } else if (!strcmp(name, "stt")) {
83         return STT;
84     } else if (!strcmp(name, "vxlan")) {
85         return VXLAN;
86     }
87
88     return 0;
89 }
90
91 const struct ovsrec_bridge *
92 get_bridge(struct ovsdb_idl *ovs_idl, const char *br_name)
93 {
94     const struct ovsrec_bridge *br;
95     OVSREC_BRIDGE_FOR_EACH (br, ovs_idl) {
96         if (!strcmp(br->name, br_name)) {
97             return br;
98         }
99     }
100     return NULL;
101 }
102
103 static const struct ovsrec_bridge *
104 create_br_int(struct controller_ctx *ctx,
105               const struct ovsrec_open_vswitch *cfg,
106               const char *bridge_name)
107 {
108     if (!ctx->ovs_idl_txn) {
109         return NULL;
110     }
111
112     ovsdb_idl_txn_add_comment(ctx->ovs_idl_txn,
113             "ovn-controller: creating integration bridge '%s'", bridge_name);
114
115     struct ovsrec_interface *iface;
116     iface = ovsrec_interface_insert(ctx->ovs_idl_txn);
117     ovsrec_interface_set_name(iface, bridge_name);
118     ovsrec_interface_set_type(iface, "internal");
119
120     struct ovsrec_port *port;
121     port = ovsrec_port_insert(ctx->ovs_idl_txn);
122     ovsrec_port_set_name(port, bridge_name);
123     ovsrec_port_set_interfaces(port, &iface, 1);
124
125     struct ovsrec_bridge *bridge;
126     bridge = ovsrec_bridge_insert(ctx->ovs_idl_txn);
127     ovsrec_bridge_set_name(bridge, bridge_name);
128     ovsrec_bridge_set_fail_mode(bridge, "secure");
129     const struct smap oc = SMAP_CONST1(&oc, "disable-in-band", "true");
130     ovsrec_bridge_set_other_config(bridge, &oc);
131     ovsrec_bridge_set_ports(bridge, &port, 1);
132
133     struct ovsrec_bridge **bridges;
134     size_t bytes = sizeof *bridges * cfg->n_bridges;
135     bridges = xmalloc(bytes + sizeof *bridges);
136     memcpy(bridges, cfg->bridges, bytes);
137     bridges[cfg->n_bridges] = bridge;
138     ovsrec_open_vswitch_verify_bridges(cfg);
139     ovsrec_open_vswitch_set_bridges(cfg, bridges, cfg->n_bridges + 1);
140
141     return bridge;
142 }
143
144 static const struct ovsrec_bridge *
145 get_br_int(struct controller_ctx *ctx)
146 {
147     const struct ovsrec_open_vswitch *cfg;
148     cfg = ovsrec_open_vswitch_first(ctx->ovs_idl);
149     if (!cfg) {
150         return NULL;
151     }
152
153     const char *br_int_name = smap_get(&cfg->external_ids, "ovn-bridge");
154     if (!br_int_name) {
155         br_int_name = DEFAULT_BRIDGE_NAME;
156     }
157
158     const struct ovsrec_bridge *br;
159     br = get_bridge(ctx->ovs_idl, br_int_name);
160     if (!br) {
161         return create_br_int(ctx, cfg, br_int_name);
162     }
163     return br;
164 }
165
166 static const char *
167 get_chassis_id(const struct ovsdb_idl *ovs_idl)
168 {
169     const struct ovsrec_open_vswitch *cfg = ovsrec_open_vswitch_first(ovs_idl);
170     return cfg ? smap_get(&cfg->external_ids, "system-id") : NULL;
171 }
172
173 /* Retrieves the OVN Southbound remote location from the
174  * "external-ids:ovn-remote" key in 'ovs_idl' and returns a copy of it.
175  *
176  * XXX ovn-controller does not support this changing mid-run, but that should
177  * be addressed later. */
178 static char *
179 get_ovnsb_remote(struct ovsdb_idl *ovs_idl)
180 {
181     while (1) {
182         ovsdb_idl_run(ovs_idl);
183
184         const struct ovsrec_open_vswitch *cfg
185             = ovsrec_open_vswitch_first(ovs_idl);
186         if (cfg) {
187             const char *remote = smap_get(&cfg->external_ids, "ovn-remote");
188             if (remote) {
189                 return xstrdup(remote);
190             }
191         }
192
193         VLOG_INFO("OVN OVSDB remote not specified.  Waiting...");
194         ovsdb_idl_wait(ovs_idl);
195         poll_block();
196     }
197 }
198
199 int
200 main(int argc, char *argv[])
201 {
202     struct unixctl_server *unixctl;
203     bool exiting;
204     int retval;
205
206     ovs_cmdl_proctitle_init(argc, argv);
207     set_program_name(argv[0]);
208     service_start(&argc, &argv);
209     parse_options(argc, argv);
210     fatal_ignore_sigpipe();
211
212     daemonize_start(false);
213
214     retval = unixctl_server_create(NULL, &unixctl);
215     if (retval) {
216         exit(EXIT_FAILURE);
217     }
218     unixctl_command_register("exit", "", 0, 0, ovn_controller_exit, &exiting);
219
220     daemonize_complete();
221
222     ovsrec_init();
223     sbrec_init();
224
225     ofctrl_init();
226     lflow_init();
227
228     /* Connect to OVS OVSDB instance.  We do not monitor all tables by
229      * default, so modules must register their interest explicitly.  */
230     struct ovsdb_idl_loop ovs_idl_loop = OVSDB_IDL_LOOP_INITIALIZER(
231         ovsdb_idl_create(ovs_remote, &ovsrec_idl_class, false, true));
232     ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_open_vswitch);
233     ovsdb_idl_add_column(ovs_idl_loop.idl,
234                          &ovsrec_open_vswitch_col_external_ids);
235     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_open_vswitch_col_bridges);
236     ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_interface);
237     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_interface_col_name);
238     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_interface_col_type);
239     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_interface_col_options);
240     ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_port);
241     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_port_col_name);
242     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_port_col_interfaces);
243     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_port_col_external_ids);
244     ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_bridge);
245     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_ports);
246     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_name);
247     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_fail_mode);
248     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_other_config);
249     chassis_register_ovs_idl(ovs_idl_loop.idl);
250     encaps_register_ovs_idl(ovs_idl_loop.idl);
251     binding_register_ovs_idl(ovs_idl_loop.idl);
252     physical_register_ovs_idl(ovs_idl_loop.idl);
253     ovsdb_idl_get_initial_snapshot(ovs_idl_loop.idl);
254
255     /* Connect to OVN SB database. */
256     char *ovnsb_remote = get_ovnsb_remote(ovs_idl_loop.idl);
257     struct ovsdb_idl_loop ovnsb_idl_loop = OVSDB_IDL_LOOP_INITIALIZER(
258         ovsdb_idl_create(ovnsb_remote, &sbrec_idl_class, true, true));
259     ovsdb_idl_get_initial_snapshot(ovnsb_idl_loop.idl);
260
261     /* Initialize connection tracking zones. */
262     struct simap ct_zones = SIMAP_INITIALIZER(&ct_zones);
263     unsigned long ct_zone_bitmap[BITMAP_N_LONGS(MAX_CT_ZONES)];
264     bitmap_set1(ct_zone_bitmap, 0); /* Zone 0 is reserved. */
265     unixctl_command_register("ct-zone-list", "", 0, 0,
266                              ct_zone_list, &ct_zones);
267
268     /* Main loop. */
269     exiting = false;
270     while (!exiting) {
271         struct controller_ctx ctx = {
272             .ovs_idl = ovs_idl_loop.idl,
273             .ovs_idl_txn = ovsdb_idl_loop_run(&ovs_idl_loop),
274             .ovnsb_idl = ovnsb_idl_loop.idl,
275             .ovnsb_idl_txn = ovsdb_idl_loop_run(&ovnsb_idl_loop),
276         };
277
278         const struct ovsrec_bridge *br_int = get_br_int(&ctx);
279         const char *chassis_id = get_chassis_id(ctx.ovs_idl);
280
281         /* Map bridges to local nets from ovn-bridge-mappings */
282         if (br_int) {
283             patch_run(&ctx, br_int);
284         }
285
286         if (chassis_id) {
287             chassis_run(&ctx, chassis_id);
288             encaps_run(&ctx, br_int, chassis_id);
289             binding_run(&ctx, br_int, chassis_id, &ct_zones, ct_zone_bitmap);
290         }
291
292         if (br_int) {
293             enum mf_field_id mff_ovn_geneve = ofctrl_run(br_int);
294
295             struct hmap flow_table = HMAP_INITIALIZER(&flow_table);
296             lflow_run(&ctx, &flow_table, &ct_zones);
297             if (chassis_id) {
298                 physical_run(&ctx, mff_ovn_geneve,
299                              br_int, chassis_id, &ct_zones, &flow_table);
300             }
301             ofctrl_put(&flow_table);
302             hmap_destroy(&flow_table);
303         }
304
305         unixctl_server_run(unixctl);
306
307         unixctl_server_wait(unixctl);
308         if (exiting) {
309             poll_immediate_wake();
310         }
311
312         ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
313         ovsdb_idl_loop_commit_and_wait(&ovs_idl_loop);
314
315         if (br_int) {
316             ofctrl_wait();
317         }
318         poll_block();
319         if (should_service_stop()) {
320             exiting = true;
321         }
322     }
323
324     /* It's time to exit.  Clean up the databases. */
325     bool done = false;
326     while (!done) {
327         struct controller_ctx ctx = {
328             .ovs_idl = ovs_idl_loop.idl,
329             .ovs_idl_txn = ovsdb_idl_loop_run(&ovs_idl_loop),
330             .ovnsb_idl = ovnsb_idl_loop.idl,
331             .ovnsb_idl_txn = ovsdb_idl_loop_run(&ovnsb_idl_loop),
332         };
333
334         const struct ovsrec_bridge *br_int = get_br_int(&ctx);
335         const char *chassis_id = get_chassis_id(ctx.ovs_idl);
336
337         /* Run all of the cleanup functions, even if one of them returns false.
338          * We're done if all of them return true. */
339         done = binding_cleanup(&ctx, chassis_id);
340         done = chassis_cleanup(&ctx, chassis_id) && done;
341         done = encaps_cleanup(&ctx, br_int) && done;
342         if (done) {
343             poll_immediate_wake();
344         }
345
346         ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
347         ovsdb_idl_loop_commit_and_wait(&ovs_idl_loop);
348         poll_block();
349     }
350
351     unixctl_server_destroy(unixctl);
352     lflow_destroy();
353     ofctrl_destroy();
354
355     simap_destroy(&ct_zones);
356
357     ovsdb_idl_loop_destroy(&ovs_idl_loop);
358     ovsdb_idl_loop_destroy(&ovnsb_idl_loop);
359
360     free(ovnsb_remote);
361     free(ovs_remote);
362     service_stop();
363
364     exit(retval);
365 }
366
367 static void
368 parse_options(int argc, char *argv[])
369 {
370     enum {
371         OPT_PEER_CA_CERT = UCHAR_MAX + 1,
372         OPT_BOOTSTRAP_CA_CERT,
373         VLOG_OPTION_ENUMS,
374         DAEMON_OPTION_ENUMS
375     };
376
377     static struct option long_options[] = {
378         {"help", no_argument, NULL, 'h'},
379         {"version", no_argument, NULL, 'V'},
380         VLOG_LONG_OPTIONS,
381         DAEMON_LONG_OPTIONS,
382         STREAM_SSL_LONG_OPTIONS,
383         {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
384         {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
385         {NULL, 0, NULL, 0}
386     };
387     char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
388
389     for (;;) {
390         int c;
391
392         c = getopt_long(argc, argv, short_options, long_options, NULL);
393         if (c == -1) {
394             break;
395         }
396
397         switch (c) {
398         case 'h':
399             usage();
400
401         case 'V':
402             ovs_print_version(OFP13_VERSION, OFP13_VERSION);
403             exit(EXIT_SUCCESS);
404
405         VLOG_OPTION_HANDLERS
406         DAEMON_OPTION_HANDLERS
407         STREAM_SSL_OPTION_HANDLERS
408
409         case OPT_PEER_CA_CERT:
410             stream_ssl_set_peer_ca_cert_file(optarg);
411             break;
412
413         case OPT_BOOTSTRAP_CA_CERT:
414             stream_ssl_set_ca_cert_file(optarg, true);
415             break;
416
417         case '?':
418             exit(EXIT_FAILURE);
419
420         default:
421             abort();
422         }
423     }
424     free(short_options);
425
426     argc -= optind;
427     argv += optind;
428
429     if (argc == 0) {
430         ovs_remote = xasprintf("unix:%s/db.sock", ovs_rundir());
431     } else if (argc == 1) {
432         ovs_remote = xstrdup(argv[0]);
433     } else {
434         VLOG_FATAL("exactly zero or one non-option argument required; "
435                    "use --help for usage");
436     }
437 }
438
439 static void
440 usage(void)
441 {
442     printf("%s: OVN controller\n"
443            "usage %s [OPTIONS] [OVS-DATABASE]\n"
444            "where OVS-DATABASE is a socket on which the OVS OVSDB server is listening.\n",
445                program_name, program_name);
446     stream_usage("OVS-DATABASE", true, false, false);
447     daemon_usage();
448     vlog_usage();
449     printf("\nOther options:\n"
450            "  -h, --help              display this help message\n"
451            "  -V, --version           display version information\n");
452     exit(EXIT_SUCCESS);
453 }
454
455 static void
456 ovn_controller_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
457              const char *argv[] OVS_UNUSED, void *exiting_)
458 {
459     bool *exiting = exiting_;
460     *exiting = true;
461
462     unixctl_command_reply(conn, NULL);
463 }
464
465 static void
466 ct_zone_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
467              const char *argv[] OVS_UNUSED, void *ct_zones_)
468 {
469     struct simap *ct_zones = ct_zones_;
470     struct ds ds = DS_EMPTY_INITIALIZER;
471     struct simap_node *zone;
472
473     SIMAP_FOR_EACH(zone, ct_zones) {
474         ds_put_format(&ds, "%s %d\n", zone->name, zone->data);
475     }
476
477     unixctl_command_reply(conn, ds_cstr(&ds));
478     ds_destroy(&ds);
479 }