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