d705a16be28283fa5b209852510414afe857d44d
[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 "lflow.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
60 static const struct ovsrec_bridge *
61 get_bridge(struct ovsdb_idl *ovs_idl, const char *br_name)
62 {
63     const struct ovsrec_bridge *br;
64     OVSREC_BRIDGE_FOR_EACH (br, ovs_idl) {
65         if (!strcmp(br->name, br_name)) {
66             return br;
67         }
68     }
69     return NULL;
70 }
71
72 static const struct ovsrec_bridge *
73 create_br_int(struct controller_ctx *ctx,
74               const struct ovsrec_open_vswitch *cfg,
75               const char *bridge_name)
76 {
77     if (!ctx->ovs_idl_txn) {
78         return NULL;
79     }
80
81     ovsdb_idl_txn_add_comment(ctx->ovs_idl_txn,
82             "ovn-controller: creating integration bridge '%s'", bridge_name);
83
84     struct ovsrec_interface *iface;
85     iface = ovsrec_interface_insert(ctx->ovs_idl_txn);
86     ovsrec_interface_set_name(iface, bridge_name);
87     ovsrec_interface_set_type(iface, "internal");
88
89     struct ovsrec_port *port;
90     port = ovsrec_port_insert(ctx->ovs_idl_txn);
91     ovsrec_port_set_name(port, bridge_name);
92     ovsrec_port_set_interfaces(port, &iface, 1);
93
94     struct ovsrec_bridge *bridge;
95     bridge = ovsrec_bridge_insert(ctx->ovs_idl_txn);
96     ovsrec_bridge_set_name(bridge, bridge_name);
97     ovsrec_bridge_set_fail_mode(bridge, "secure");
98     struct smap other_config = SMAP_INITIALIZER(&other_config);
99     smap_add(&other_config, "disable-in-band", "true");
100     ovsrec_bridge_set_other_config(bridge, &other_config);
101     smap_destroy(&other_config);
102     ovsrec_bridge_set_ports(bridge, &port, 1);
103
104     struct ovsrec_bridge **bridges;
105     size_t bytes = sizeof *bridges * cfg->n_bridges;
106     bridges = xmalloc(bytes + sizeof *bridges);
107     memcpy(bridges, cfg->bridges, bytes);
108     bridges[cfg->n_bridges] = bridge;
109     ovsrec_open_vswitch_verify_bridges(cfg);
110     ovsrec_open_vswitch_set_bridges(cfg, bridges, cfg->n_bridges + 1);
111
112     return bridge;
113 }
114
115 static const struct ovsrec_bridge *
116 get_br_int(struct controller_ctx *ctx)
117 {
118     const struct ovsrec_open_vswitch *cfg;
119     cfg = ovsrec_open_vswitch_first(ctx->ovs_idl);
120     if (!cfg) {
121         return NULL;
122     }
123
124     const char *br_int_name = smap_get(&cfg->external_ids, "ovn-bridge");
125     if (!br_int_name) {
126         br_int_name = DEFAULT_BRIDGE_NAME;
127     }
128
129     const struct ovsrec_bridge *br;
130     br = get_bridge(ctx->ovs_idl, br_int_name);
131     if (!br) {
132         return create_br_int(ctx, cfg, br_int_name);
133     }
134     return br;
135 }
136
137 static const char *
138 get_chassis_id(const struct ovsdb_idl *ovs_idl)
139 {
140     const struct ovsrec_open_vswitch *cfg = ovsrec_open_vswitch_first(ovs_idl);
141     return cfg ? smap_get(&cfg->external_ids, "system-id") : NULL;
142 }
143
144 static char *
145 patch_port_name(const struct ovsrec_bridge *b1, const struct ovsrec_bridge *b2)
146 {
147     return xasprintf("patch-%s-to-%s", b1->name, b2->name);
148 }
149
150 /*
151  * Return true if the port is a patch port from b1 to b2
152  */
153 static bool
154 match_patch_port(const struct ovsrec_port *port,
155                  const struct ovsrec_bridge *b1,
156                  const struct ovsrec_bridge *b2)
157 {
158     struct ovsrec_interface *iface;
159     size_t i;
160     char *peer_port_name;
161     bool res = false;
162
163     peer_port_name = patch_port_name(b2, b1);
164
165     for (i = 0; i < port->n_interfaces; i++) {
166         iface = port->interfaces[i];
167         if (strcmp(iface->type, "patch")) {
168             continue;
169         }
170         const char *peer;
171         peer = smap_get(&iface->options, "peer");
172         if (peer && !strcmp(peer, peer_port_name)) {
173             res = true;
174             break;
175         }
176     }
177
178     free(peer_port_name);
179
180     return res;
181 }
182
183 static void
184 create_patch_port(struct controller_ctx *ctx,
185                   const char *network,
186                   const struct ovsrec_bridge *b1,
187                   const struct ovsrec_bridge *b2)
188 {
189     if (!ctx->ovs_idl_txn) {
190         return;
191     }
192
193     char *port_name = patch_port_name(b1, b2);
194     char *peer_port_name = patch_port_name(b2, b1);
195
196     ovsdb_idl_txn_add_comment(ctx->ovs_idl_txn,
197             "ovn-controller: creating patch port '%s' from '%s' to '%s'",
198             port_name, b1->name, b2->name);
199
200     struct ovsrec_interface *iface;
201     iface = ovsrec_interface_insert(ctx->ovs_idl_txn);
202     ovsrec_interface_set_name(iface, port_name);
203     ovsrec_interface_set_type(iface, "patch");
204     struct smap options = SMAP_INITIALIZER(&options);
205     smap_add(&options, "peer", peer_port_name);
206     ovsrec_interface_set_options(iface, &options);
207     smap_destroy(&options);
208
209     struct ovsrec_port *port;
210     port = ovsrec_port_insert(ctx->ovs_idl_txn);
211     ovsrec_port_set_name(port, port_name);
212     ovsrec_port_set_interfaces(port, &iface, 1);
213     struct smap ext_ids = SMAP_INITIALIZER(&ext_ids);
214     smap_add(&ext_ids, "ovn-patch-port", network);
215     ovsrec_port_set_external_ids(port, &ext_ids);
216     smap_destroy(&ext_ids);
217
218     struct ovsrec_port **ports;
219     ports = xmalloc(sizeof *ports * (b1->n_ports + 1));
220     memcpy(ports, b1->ports, sizeof *ports * b1->n_ports);
221     ports[b1->n_ports] = port;
222     ovsrec_bridge_verify_ports(b1);
223     ovsrec_bridge_set_ports(b1, ports, b1->n_ports + 1);
224
225     free(ports);
226     free(port_name);
227     free(peer_port_name);
228 }
229
230 static void
231 create_patch_ports(struct controller_ctx *ctx,
232                    const char *network,
233                    struct shash *existing_ports,
234                    const struct ovsrec_bridge *b1,
235                    const struct ovsrec_bridge *b2)
236 {
237     size_t i;
238
239     for (i = 0; i < b1->n_ports; i++) {
240         if (match_patch_port(b1->ports[i], b1, b2)) {
241             /* Patch port already exists on b1 */
242             shash_find_and_delete(existing_ports, b1->ports[i]->name);
243             break;
244         }
245     }
246     if (i == b1->n_ports) {
247         create_patch_port(ctx, network, b1, b2);
248     }
249 }
250
251 static void
252 init_existing_ports(struct controller_ctx *ctx,
253                     struct shash *existing_ports)
254 {
255     const struct ovsrec_port *port;
256
257     OVSREC_PORT_FOR_EACH (port, ctx->ovs_idl) {
258         if (smap_get(&port->external_ids, "ovn-patch-port")) {
259             shash_add(existing_ports, port->name, port);
260         }
261     }
262 }
263
264 static void
265 remove_port(struct controller_ctx *ctx,
266             const struct ovsrec_port *port)
267 {
268     const struct ovsrec_bridge *bridge;
269
270     /* We know the port we want to delete, but we have to find the bridge its on
271      * to do so.  Note this only runs on a config change that should be pretty
272      * rare. */
273     OVSREC_BRIDGE_FOR_EACH (bridge, ctx->ovs_idl) {
274         size_t i;
275         for (i = 0; i < bridge->n_ports; i++) {
276             if (bridge->ports[i] != port) {
277                 continue;
278             }
279             struct ovsrec_port **new_ports;
280             new_ports = xmemdup(bridge->ports,
281                     sizeof *new_ports * (bridge->n_ports - 1));
282             if (i != bridge->n_ports - 1) {
283                 /* Removed port was not last */
284                 new_ports[i] = bridge->ports[bridge->n_ports - 1];
285             }
286             ovsrec_bridge_verify_ports(bridge);
287             ovsrec_bridge_set_ports(bridge, new_ports, bridge->n_ports - 1);
288             free(new_ports);
289             ovsrec_port_delete(port);
290             return;
291         }
292     }
293 }
294
295 static void
296 parse_bridge_mappings(struct controller_ctx *ctx,
297                       const struct ovsrec_bridge *br_int,
298                       const char *mappings_cfg)
299 {
300     struct shash existing_ports = SHASH_INITIALIZER(&existing_ports);
301     init_existing_ports(ctx, &existing_ports);
302
303     char *cur, *next, *start;
304     next = start = xstrdup(mappings_cfg);
305     while ((cur = strsep(&next, ",")) && *cur) {
306         char *network, *bridge = cur;
307         const struct ovsrec_bridge *ovs_bridge;
308
309         network = strsep(&bridge, ":");
310         if (!bridge || !*network || !*bridge) {
311             VLOG_ERR("Invalid ovn-bridge-mappings configuration: '%s'",
312                     mappings_cfg);
313             break;
314         }
315
316         ovs_bridge = get_bridge(ctx->ovs_idl, bridge);
317         if (!ovs_bridge) {
318             VLOG_WARN("Bridge '%s' not found for network '%s'",
319                     bridge, network);
320             continue;
321         }
322
323         create_patch_ports(ctx, network, &existing_ports, br_int, ovs_bridge);
324         create_patch_ports(ctx, network, &existing_ports, ovs_bridge, br_int);
325     }
326     free(start);
327
328     /* Any ports left in existing_ports are related to configuration that has
329      * been removed, so we should delete the ports now. */
330     struct shash_node *port_node, *port_next_node;
331     SHASH_FOR_EACH_SAFE (port_node, port_next_node, &existing_ports) {
332         struct ovsrec_port *port = port_node->data;
333         shash_delete(&existing_ports, port_node);
334         remove_port(ctx, port);
335     }
336     shash_destroy(&existing_ports);
337 }
338
339 static void
340 init_bridge_mappings(struct controller_ctx *ctx,
341                      const struct ovsrec_bridge *br_int)
342 {
343     const char *mappings_cfg = "";
344     const struct ovsrec_open_vswitch *cfg;
345
346     cfg = ovsrec_open_vswitch_first(ctx->ovs_idl);
347     if (cfg) {
348         mappings_cfg = smap_get(&cfg->external_ids, "ovn-bridge-mappings");
349         if (!mappings_cfg) {
350             mappings_cfg = "";
351         }
352     }
353     parse_bridge_mappings(ctx, br_int, mappings_cfg);
354 }
355
356 /* Retrieves the OVN Southbound remote location from the
357  * "external-ids:ovn-remote" key in 'ovs_idl' and returns a copy of it.
358  *
359  * XXX ovn-controller does not support this changing mid-run, but that should
360  * be addressed later. */
361 static char *
362 get_ovnsb_remote(struct ovsdb_idl *ovs_idl)
363 {
364     while (1) {
365         ovsdb_idl_run(ovs_idl);
366
367         const struct ovsrec_open_vswitch *cfg
368             = ovsrec_open_vswitch_first(ovs_idl);
369         if (cfg) {
370             const char *remote = smap_get(&cfg->external_ids, "ovn-remote");
371             if (remote) {
372                 return xstrdup(remote);
373             }
374         }
375
376         VLOG_INFO("OVN OVSDB remote not specified.  Waiting...");
377         ovsdb_idl_wait(ovs_idl);
378         poll_block();
379     }
380 }
381
382 int
383 main(int argc, char *argv[])
384 {
385     struct unixctl_server *unixctl;
386     bool exiting;
387     int retval;
388
389     ovs_cmdl_proctitle_init(argc, argv);
390     set_program_name(argv[0]);
391     service_start(&argc, &argv);
392     parse_options(argc, argv);
393     fatal_ignore_sigpipe();
394
395     daemonize_start();
396
397     retval = unixctl_server_create(NULL, &unixctl);
398     if (retval) {
399         exit(EXIT_FAILURE);
400     }
401     unixctl_command_register("exit", "", 0, 0, ovn_controller_exit, &exiting);
402
403     daemonize_complete();
404
405     ovsrec_init();
406     sbrec_init();
407
408     ofctrl_init();
409     lflow_init();
410
411     /* Connect to OVS OVSDB instance.  We do not monitor all tables by
412      * default, so modules must register their interest explicitly.  */
413     struct ovsdb_idl_loop ovs_idl_loop = OVSDB_IDL_LOOP_INITIALIZER(
414         ovsdb_idl_create(ovs_remote, &ovsrec_idl_class, false, true));
415     ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_open_vswitch);
416     ovsdb_idl_add_column(ovs_idl_loop.idl,
417                          &ovsrec_open_vswitch_col_external_ids);
418     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_open_vswitch_col_bridges);
419     ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_interface);
420     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_interface_col_name);
421     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_interface_col_type);
422     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_interface_col_options);
423     ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_port);
424     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_port_col_name);
425     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_port_col_interfaces);
426     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_port_col_external_ids);
427     ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_bridge);
428     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_ports);
429     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_name);
430     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_fail_mode);
431     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_other_config);
432     chassis_register_ovs_idl(ovs_idl_loop.idl);
433     encaps_register_ovs_idl(ovs_idl_loop.idl);
434     binding_register_ovs_idl(ovs_idl_loop.idl);
435     physical_register_ovs_idl(ovs_idl_loop.idl);
436     ovsdb_idl_get_initial_snapshot(ovs_idl_loop.idl);
437
438     /* Connect to OVN SB database. */
439     char *ovnsb_remote = get_ovnsb_remote(ovs_idl_loop.idl);
440     struct ovsdb_idl_loop ovnsb_idl_loop = OVSDB_IDL_LOOP_INITIALIZER(
441         ovsdb_idl_create(ovnsb_remote, &sbrec_idl_class, true, true));
442     ovsdb_idl_get_initial_snapshot(ovnsb_idl_loop.idl);
443
444     /* Main loop. */
445     exiting = false;
446     while (!exiting) {
447         struct controller_ctx ctx = {
448             .ovs_idl = ovs_idl_loop.idl,
449             .ovs_idl_txn = ovsdb_idl_loop_run(&ovs_idl_loop),
450             .ovnsb_idl = ovnsb_idl_loop.idl,
451             .ovnsb_idl_txn = ovsdb_idl_loop_run(&ovnsb_idl_loop),
452         };
453
454         const struct ovsrec_bridge *br_int = get_br_int(&ctx);
455         const char *chassis_id = get_chassis_id(ctx.ovs_idl);
456
457         /* Map bridges to local nets from ovn-bridge-mappings */
458         if (br_int) {
459             init_bridge_mappings(&ctx, br_int);
460         }
461
462         if (chassis_id) {
463             chassis_run(&ctx, chassis_id);
464             encaps_run(&ctx, br_int, chassis_id);
465             binding_run(&ctx, br_int, chassis_id);
466         }
467
468         if (br_int) {
469             enum mf_field_id mff_ovn_geneve = ofctrl_run(br_int);
470
471             struct hmap flow_table = HMAP_INITIALIZER(&flow_table);
472             lflow_run(&ctx, &flow_table);
473             if (chassis_id) {
474                 physical_run(&ctx, mff_ovn_geneve,
475                              br_int, chassis_id, &flow_table);
476             }
477             ofctrl_put(&flow_table);
478             hmap_destroy(&flow_table);
479         }
480
481         unixctl_server_run(unixctl);
482
483         unixctl_server_wait(unixctl);
484         if (exiting) {
485             poll_immediate_wake();
486         }
487
488         ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
489         ovsdb_idl_loop_commit_and_wait(&ovs_idl_loop);
490
491         if (br_int) {
492             ofctrl_wait();
493         }
494         poll_block();
495         if (should_service_stop()) {
496             exiting = true;
497         }
498     }
499
500     /* It's time to exit.  Clean up the databases. */
501     bool done = false;
502     while (!done) {
503         struct controller_ctx ctx = {
504             .ovs_idl = ovs_idl_loop.idl,
505             .ovs_idl_txn = ovsdb_idl_loop_run(&ovs_idl_loop),
506             .ovnsb_idl = ovnsb_idl_loop.idl,
507             .ovnsb_idl_txn = ovsdb_idl_loop_run(&ovnsb_idl_loop),
508         };
509
510         const struct ovsrec_bridge *br_int = get_br_int(&ctx);
511         const char *chassis_id = get_chassis_id(ctx.ovs_idl);
512
513         /* Run all of the cleanup functions, even if one of them returns false.
514          * We're done if all of them return true. */
515         done = binding_cleanup(&ctx, chassis_id);
516         done = chassis_cleanup(&ctx, chassis_id) && done;
517         done = encaps_cleanup(&ctx, br_int) && done;
518         if (done) {
519             poll_immediate_wake();
520         }
521
522         ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
523         ovsdb_idl_loop_commit_and_wait(&ovs_idl_loop);
524         poll_block();
525     }
526
527     unixctl_server_destroy(unixctl);
528     lflow_destroy();
529     ofctrl_destroy();
530
531     ovsdb_idl_loop_destroy(&ovs_idl_loop);
532     ovsdb_idl_loop_destroy(&ovnsb_idl_loop);
533
534     free(ovnsb_remote);
535     free(ovs_remote);
536     service_stop();
537
538     exit(retval);
539 }
540
541 static void
542 parse_options(int argc, char *argv[])
543 {
544     enum {
545         OPT_PEER_CA_CERT = UCHAR_MAX + 1,
546         OPT_BOOTSTRAP_CA_CERT,
547         VLOG_OPTION_ENUMS,
548         DAEMON_OPTION_ENUMS
549     };
550
551     static struct option long_options[] = {
552         {"help", no_argument, NULL, 'h'},
553         {"version", no_argument, NULL, 'V'},
554         VLOG_LONG_OPTIONS,
555         DAEMON_LONG_OPTIONS,
556         STREAM_SSL_LONG_OPTIONS,
557         {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
558         {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
559         {NULL, 0, NULL, 0}
560     };
561     char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
562
563     for (;;) {
564         int c;
565
566         c = getopt_long(argc, argv, short_options, long_options, NULL);
567         if (c == -1) {
568             break;
569         }
570
571         switch (c) {
572         case 'h':
573             usage();
574
575         case 'V':
576             ovs_print_version(OFP13_VERSION, OFP13_VERSION);
577             exit(EXIT_SUCCESS);
578
579         VLOG_OPTION_HANDLERS
580         DAEMON_OPTION_HANDLERS
581         STREAM_SSL_OPTION_HANDLERS
582
583         case OPT_PEER_CA_CERT:
584             stream_ssl_set_peer_ca_cert_file(optarg);
585             break;
586
587         case OPT_BOOTSTRAP_CA_CERT:
588             stream_ssl_set_ca_cert_file(optarg, true);
589             break;
590
591         case '?':
592             exit(EXIT_FAILURE);
593
594         default:
595             abort();
596         }
597     }
598     free(short_options);
599
600     argc -= optind;
601     argv += optind;
602
603     if (argc == 0) {
604         ovs_remote = xasprintf("unix:%s/db.sock", ovs_rundir());
605     } else if (argc == 1) {
606         ovs_remote = xstrdup(argv[0]);
607     } else {
608         VLOG_FATAL("exactly zero or one non-option argument required; "
609                    "use --help for usage");
610     }
611 }
612
613 static void
614 usage(void)
615 {
616     printf("%s: OVN controller\n"
617            "usage %s [OPTIONS] [OVS-DATABASE]\n"
618            "where OVS-DATABASE is a socket on which the OVS OVSDB server is listening.\n",
619                program_name, program_name);
620     stream_usage("OVS-DATABASE", true, false, false);
621     daemon_usage();
622     vlog_usage();
623     printf("\nOther options:\n"
624            "  -h, --help              display this help message\n"
625            "  -V, --version           display version information\n");
626     exit(EXIT_SUCCESS);
627 }
628
629 static void
630 ovn_controller_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
631              const char *argv[] OVS_UNUSED, void *exiting_)
632 {
633     bool *exiting = exiting_;
634     *exiting = true;
635
636     unixctl_command_reply(conn, NULL);
637 }