ovn: Connect to remote lports through localnet port.
[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 "struct local_datpath" nodes whose hash values are the
282          * tunnel_key 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         if (chassis_id) {
289             chassis_run(&ctx, chassis_id);
290             encaps_run(&ctx, br_int, chassis_id);
291             binding_run(&ctx, br_int, chassis_id, &ct_zones, ct_zone_bitmap,
292                     &local_datapaths);
293         }
294
295         if (br_int) {
296             patch_run(&ctx, br_int, &local_datapaths);
297
298             enum mf_field_id mff_ovn_geneve = ofctrl_run(br_int);
299
300             pinctrl_run(&ctx, br_int);
301
302             struct hmap flow_table = HMAP_INITIALIZER(&flow_table);
303             lflow_run(&ctx, &flow_table, &ct_zones, &local_datapaths);
304             if (chassis_id) {
305                 physical_run(&ctx, mff_ovn_geneve,
306                              br_int, chassis_id, &ct_zones, &flow_table,
307                              &local_datapaths);
308             }
309             ofctrl_put(&flow_table);
310             hmap_destroy(&flow_table);
311         }
312
313         struct local_datapath *cur_node, *next_node;
314         HMAP_FOR_EACH_SAFE (cur_node, next_node, hmap_node, &local_datapaths) {
315             hmap_remove(&local_datapaths, &cur_node->hmap_node);
316             free(cur_node);
317         }
318         hmap_destroy(&local_datapaths);
319
320         unixctl_server_run(unixctl);
321
322         unixctl_server_wait(unixctl);
323         if (exiting) {
324             poll_immediate_wake();
325         }
326
327         ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
328         ovsdb_idl_loop_commit_and_wait(&ovs_idl_loop);
329
330         if (br_int) {
331             ofctrl_wait();
332             pinctrl_wait();
333         }
334         poll_block();
335         if (should_service_stop()) {
336             exiting = true;
337         }
338     }
339
340     /* It's time to exit.  Clean up the databases. */
341     bool done = false;
342     while (!done) {
343         struct controller_ctx ctx = {
344             .ovs_idl = ovs_idl_loop.idl,
345             .ovs_idl_txn = ovsdb_idl_loop_run(&ovs_idl_loop),
346             .ovnsb_idl = ovnsb_idl_loop.idl,
347             .ovnsb_idl_txn = ovsdb_idl_loop_run(&ovnsb_idl_loop),
348         };
349
350         const struct ovsrec_bridge *br_int = get_br_int(&ctx);
351         const char *chassis_id = get_chassis_id(ctx.ovs_idl);
352
353         /* Run all of the cleanup functions, even if one of them returns false.
354          * We're done if all of them return true. */
355         done = binding_cleanup(&ctx, chassis_id);
356         done = chassis_cleanup(&ctx, chassis_id) && done;
357         done = encaps_cleanup(&ctx, br_int) && done;
358         if (done) {
359             poll_immediate_wake();
360         }
361
362         ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
363         ovsdb_idl_loop_commit_and_wait(&ovs_idl_loop);
364         poll_block();
365     }
366
367     unixctl_server_destroy(unixctl);
368     lflow_destroy();
369     ofctrl_destroy();
370     pinctrl_destroy();
371
372     simap_destroy(&ct_zones);
373
374     ovsdb_idl_loop_destroy(&ovs_idl_loop);
375     ovsdb_idl_loop_destroy(&ovnsb_idl_loop);
376
377     free(ovnsb_remote);
378     free(ovs_remote);
379     service_stop();
380
381     exit(retval);
382 }
383
384 static void
385 parse_options(int argc, char *argv[])
386 {
387     enum {
388         OPT_PEER_CA_CERT = UCHAR_MAX + 1,
389         OPT_BOOTSTRAP_CA_CERT,
390         VLOG_OPTION_ENUMS,
391         DAEMON_OPTION_ENUMS
392     };
393
394     static struct option long_options[] = {
395         {"help", no_argument, NULL, 'h'},
396         {"version", no_argument, NULL, 'V'},
397         VLOG_LONG_OPTIONS,
398         DAEMON_LONG_OPTIONS,
399         STREAM_SSL_LONG_OPTIONS,
400         {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
401         {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
402         {NULL, 0, NULL, 0}
403     };
404     char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
405
406     for (;;) {
407         int c;
408
409         c = getopt_long(argc, argv, short_options, long_options, NULL);
410         if (c == -1) {
411             break;
412         }
413
414         switch (c) {
415         case 'h':
416             usage();
417
418         case 'V':
419             ovs_print_version(OFP13_VERSION, OFP13_VERSION);
420             exit(EXIT_SUCCESS);
421
422         VLOG_OPTION_HANDLERS
423         DAEMON_OPTION_HANDLERS
424         STREAM_SSL_OPTION_HANDLERS
425
426         case OPT_PEER_CA_CERT:
427             stream_ssl_set_peer_ca_cert_file(optarg);
428             break;
429
430         case OPT_BOOTSTRAP_CA_CERT:
431             stream_ssl_set_ca_cert_file(optarg, true);
432             break;
433
434         case '?':
435             exit(EXIT_FAILURE);
436
437         default:
438             abort();
439         }
440     }
441     free(short_options);
442
443     argc -= optind;
444     argv += optind;
445
446     if (argc == 0) {
447         ovs_remote = xasprintf("unix:%s/db.sock", ovs_rundir());
448     } else if (argc == 1) {
449         ovs_remote = xstrdup(argv[0]);
450     } else {
451         VLOG_FATAL("exactly zero or one non-option argument required; "
452                    "use --help for usage");
453     }
454 }
455
456 static void
457 usage(void)
458 {
459     printf("%s: OVN controller\n"
460            "usage %s [OPTIONS] [OVS-DATABASE]\n"
461            "where OVS-DATABASE is a socket on which the OVS OVSDB server is listening.\n",
462                program_name, program_name);
463     stream_usage("OVS-DATABASE", true, false, false);
464     daemon_usage();
465     vlog_usage();
466     printf("\nOther options:\n"
467            "  -h, --help              display this help message\n"
468            "  -V, --version           display version information\n");
469     exit(EXIT_SUCCESS);
470 }
471
472 static void
473 ovn_controller_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
474              const char *argv[] OVS_UNUSED, void *exiting_)
475 {
476     bool *exiting = exiting_;
477     *exiting = true;
478
479     unixctl_command_reply(conn, NULL);
480 }
481
482 static void
483 ct_zone_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
484              const char *argv[] OVS_UNUSED, void *ct_zones_)
485 {
486     struct simap *ct_zones = ct_zones_;
487     struct ds ds = DS_EMPTY_INITIALIZER;
488     struct simap_node *zone;
489
490     SIMAP_FOR_EACH(zone, ct_zones) {
491         ds_put_format(&ds, "%s %d\n", zone->name, zone->data);
492     }
493
494     unixctl_command_reply(conn, ds_cstr(&ds));
495     ds_destroy(&ds);
496 }