1 /* Copyright (c) 2015 Nicira, Inc.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "ovn-controller.h"
26 #include "command-line.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"
38 #include "stream-ssl.h"
49 VLOG_DEFINE_THIS_MODULE(main);
51 static unixctl_cb_func ovn_controller_exit;
53 #define DEFAULT_BRIDGE_NAME "br-int"
55 static void parse_options(int argc, char *argv[]);
56 OVS_NO_RETURN static void usage(void);
58 static char *ovs_remote;
60 const struct sbrec_chassis *
61 get_chassis(struct ovsdb_idl *ovnsb_idl, const char *chassis_id)
63 const struct sbrec_chassis *chassis_rec;
65 SBREC_CHASSIS_FOR_EACH(chassis_rec, ovnsb_idl) {
66 if (!strcmp(chassis_rec->name, chassis_id)) {
74 static const struct ovsrec_bridge *
75 get_bridge(struct ovsdb_idl *ovs_idl, const char *br_name)
77 const struct ovsrec_bridge *br;
78 OVSREC_BRIDGE_FOR_EACH (br, ovs_idl) {
79 if (!strcmp(br->name, br_name)) {
86 static const struct ovsrec_bridge *
87 create_br_int(struct controller_ctx *ctx,
88 const struct ovsrec_open_vswitch *cfg,
89 const char *bridge_name)
91 if (!ctx->ovs_idl_txn) {
95 ovsdb_idl_txn_add_comment(ctx->ovs_idl_txn,
96 "ovn-controller: creating integration bridge '%s'", bridge_name);
98 struct ovsrec_interface *iface;
99 iface = ovsrec_interface_insert(ctx->ovs_idl_txn);
100 ovsrec_interface_set_name(iface, bridge_name);
101 ovsrec_interface_set_type(iface, "internal");
103 struct ovsrec_port *port;
104 port = ovsrec_port_insert(ctx->ovs_idl_txn);
105 ovsrec_port_set_name(port, bridge_name);
106 ovsrec_port_set_interfaces(port, &iface, 1);
108 struct ovsrec_bridge *bridge;
109 bridge = ovsrec_bridge_insert(ctx->ovs_idl_txn);
110 ovsrec_bridge_set_name(bridge, bridge_name);
111 ovsrec_bridge_set_fail_mode(bridge, "secure");
112 const struct smap oc = SMAP_CONST1(&oc, "disable-in-band", "true");
113 ovsrec_bridge_set_other_config(bridge, &oc);
114 ovsrec_bridge_set_ports(bridge, &port, 1);
116 struct ovsrec_bridge **bridges;
117 size_t bytes = sizeof *bridges * cfg->n_bridges;
118 bridges = xmalloc(bytes + sizeof *bridges);
119 memcpy(bridges, cfg->bridges, bytes);
120 bridges[cfg->n_bridges] = bridge;
121 ovsrec_open_vswitch_verify_bridges(cfg);
122 ovsrec_open_vswitch_set_bridges(cfg, bridges, cfg->n_bridges + 1);
127 static const struct ovsrec_bridge *
128 get_br_int(struct controller_ctx *ctx)
130 const struct ovsrec_open_vswitch *cfg;
131 cfg = ovsrec_open_vswitch_first(ctx->ovs_idl);
136 const char *br_int_name = smap_get(&cfg->external_ids, "ovn-bridge");
138 br_int_name = DEFAULT_BRIDGE_NAME;
141 const struct ovsrec_bridge *br;
142 br = get_bridge(ctx->ovs_idl, br_int_name);
144 return create_br_int(ctx, cfg, br_int_name);
150 get_chassis_id(const struct ovsdb_idl *ovs_idl)
152 const struct ovsrec_open_vswitch *cfg = ovsrec_open_vswitch_first(ovs_idl);
153 return cfg ? smap_get(&cfg->external_ids, "system-id") : NULL;
157 patch_port_name(const struct ovsrec_bridge *b1, const struct ovsrec_bridge *b2)
159 return xasprintf("patch-%s-to-%s", b1->name, b2->name);
163 * Return true if the port is a patch port from b1 to b2
166 match_patch_port(const struct ovsrec_port *port,
167 const struct ovsrec_bridge *b1,
168 const struct ovsrec_bridge *b2)
170 struct ovsrec_interface *iface;
172 char *peer_port_name;
175 peer_port_name = patch_port_name(b2, b1);
177 for (i = 0; i < port->n_interfaces; i++) {
178 iface = port->interfaces[i];
179 if (strcmp(iface->type, "patch")) {
183 peer = smap_get(&iface->options, "peer");
184 if (peer && !strcmp(peer, peer_port_name)) {
190 free(peer_port_name);
196 create_patch_port(struct controller_ctx *ctx,
198 const struct ovsrec_bridge *b1,
199 const struct ovsrec_bridge *b2)
201 if (!ctx->ovs_idl_txn) {
205 char *port_name = patch_port_name(b1, b2);
206 char *peer_port_name = patch_port_name(b2, b1);
208 ovsdb_idl_txn_add_comment(ctx->ovs_idl_txn,
209 "ovn-controller: creating patch port '%s' from '%s' to '%s'",
210 port_name, b1->name, b2->name);
212 struct ovsrec_interface *iface;
213 iface = ovsrec_interface_insert(ctx->ovs_idl_txn);
214 ovsrec_interface_set_name(iface, port_name);
215 ovsrec_interface_set_type(iface, "patch");
216 const struct smap options = SMAP_CONST1(&options, "peer", peer_port_name);
217 ovsrec_interface_set_options(iface, &options);
219 struct ovsrec_port *port;
220 port = ovsrec_port_insert(ctx->ovs_idl_txn);
221 ovsrec_port_set_name(port, port_name);
222 ovsrec_port_set_interfaces(port, &iface, 1);
223 const struct smap ids = SMAP_CONST1(&ids, "ovn-patch-port", network);
224 ovsrec_port_set_external_ids(port, &ids);
226 struct ovsrec_port **ports;
227 ports = xmalloc(sizeof *ports * (b1->n_ports + 1));
228 memcpy(ports, b1->ports, sizeof *ports * b1->n_ports);
229 ports[b1->n_ports] = port;
230 ovsrec_bridge_verify_ports(b1);
231 ovsrec_bridge_set_ports(b1, ports, b1->n_ports + 1);
235 free(peer_port_name);
239 create_patch_ports(struct controller_ctx *ctx,
241 struct shash *existing_ports,
242 const struct ovsrec_bridge *b1,
243 const struct ovsrec_bridge *b2)
247 for (i = 0; i < b1->n_ports; i++) {
248 if (match_patch_port(b1->ports[i], b1, b2)) {
249 /* Patch port already exists on b1 */
250 shash_find_and_delete(existing_ports, b1->ports[i]->name);
254 if (i == b1->n_ports) {
255 create_patch_port(ctx, network, b1, b2);
260 init_existing_ports(struct controller_ctx *ctx,
261 struct shash *existing_ports)
263 const struct ovsrec_port *port;
265 OVSREC_PORT_FOR_EACH (port, ctx->ovs_idl) {
266 if (smap_get(&port->external_ids, "ovn-patch-port")) {
267 shash_add(existing_ports, port->name, port);
273 remove_port(struct controller_ctx *ctx,
274 const struct ovsrec_port *port)
276 const struct ovsrec_bridge *bridge;
278 /* We know the port we want to delete, but we have to find the bridge its on
279 * to do so. Note this only runs on a config change that should be pretty
281 OVSREC_BRIDGE_FOR_EACH (bridge, ctx->ovs_idl) {
283 for (i = 0; i < bridge->n_ports; i++) {
284 if (bridge->ports[i] != port) {
287 struct ovsrec_port **new_ports;
288 new_ports = xmemdup(bridge->ports,
289 sizeof *new_ports * (bridge->n_ports - 1));
290 if (i != bridge->n_ports - 1) {
291 /* Removed port was not last */
292 new_ports[i] = bridge->ports[bridge->n_ports - 1];
294 ovsrec_bridge_verify_ports(bridge);
295 ovsrec_bridge_set_ports(bridge, new_ports, bridge->n_ports - 1);
297 ovsrec_port_delete(port);
304 parse_bridge_mappings(struct controller_ctx *ctx,
305 const struct ovsrec_bridge *br_int,
306 const char *mappings_cfg)
308 struct shash existing_ports = SHASH_INITIALIZER(&existing_ports);
309 init_existing_ports(ctx, &existing_ports);
311 char *cur, *next, *start;
312 next = start = xstrdup(mappings_cfg);
313 while ((cur = strsep(&next, ",")) && *cur) {
314 char *network, *bridge = cur;
315 const struct ovsrec_bridge *ovs_bridge;
317 network = strsep(&bridge, ":");
318 if (!bridge || !*network || !*bridge) {
319 VLOG_ERR("Invalid ovn-bridge-mappings configuration: '%s'",
324 ovs_bridge = get_bridge(ctx->ovs_idl, bridge);
326 VLOG_WARN("Bridge '%s' not found for network '%s'",
331 create_patch_ports(ctx, network, &existing_ports, br_int, ovs_bridge);
332 create_patch_ports(ctx, network, &existing_ports, ovs_bridge, br_int);
336 /* Any ports left in existing_ports are related to configuration that has
337 * been removed, so we should delete the ports now. */
338 struct shash_node *port_node, *port_next_node;
339 SHASH_FOR_EACH_SAFE (port_node, port_next_node, &existing_ports) {
340 struct ovsrec_port *port = port_node->data;
341 shash_delete(&existing_ports, port_node);
342 remove_port(ctx, port);
344 shash_destroy(&existing_ports);
348 init_bridge_mappings(struct controller_ctx *ctx,
349 const struct ovsrec_bridge *br_int)
351 const char *mappings_cfg = "";
352 const struct ovsrec_open_vswitch *cfg;
354 cfg = ovsrec_open_vswitch_first(ctx->ovs_idl);
356 mappings_cfg = smap_get(&cfg->external_ids, "ovn-bridge-mappings");
361 parse_bridge_mappings(ctx, br_int, mappings_cfg);
364 /* Retrieves the OVN Southbound remote location from the
365 * "external-ids:ovn-remote" key in 'ovs_idl' and returns a copy of it.
367 * XXX ovn-controller does not support this changing mid-run, but that should
368 * be addressed later. */
370 get_ovnsb_remote(struct ovsdb_idl *ovs_idl)
373 ovsdb_idl_run(ovs_idl);
375 const struct ovsrec_open_vswitch *cfg
376 = ovsrec_open_vswitch_first(ovs_idl);
378 const char *remote = smap_get(&cfg->external_ids, "ovn-remote");
380 return xstrdup(remote);
384 VLOG_INFO("OVN OVSDB remote not specified. Waiting...");
385 ovsdb_idl_wait(ovs_idl);
391 main(int argc, char *argv[])
393 struct unixctl_server *unixctl;
397 ovs_cmdl_proctitle_init(argc, argv);
398 set_program_name(argv[0]);
399 service_start(&argc, &argv);
400 parse_options(argc, argv);
401 fatal_ignore_sigpipe();
403 daemonize_start(false);
405 retval = unixctl_server_create(NULL, &unixctl);
409 unixctl_command_register("exit", "", 0, 0, ovn_controller_exit, &exiting);
411 daemonize_complete();
419 /* Connect to OVS OVSDB instance. We do not monitor all tables by
420 * default, so modules must register their interest explicitly. */
421 struct ovsdb_idl_loop ovs_idl_loop = OVSDB_IDL_LOOP_INITIALIZER(
422 ovsdb_idl_create(ovs_remote, &ovsrec_idl_class, false, true));
423 ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_open_vswitch);
424 ovsdb_idl_add_column(ovs_idl_loop.idl,
425 &ovsrec_open_vswitch_col_external_ids);
426 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_open_vswitch_col_bridges);
427 ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_interface);
428 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_interface_col_name);
429 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_interface_col_type);
430 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_interface_col_options);
431 ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_port);
432 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_port_col_name);
433 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_port_col_interfaces);
434 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_port_col_external_ids);
435 ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_bridge);
436 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_ports);
437 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_name);
438 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_fail_mode);
439 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_other_config);
440 chassis_register_ovs_idl(ovs_idl_loop.idl);
441 encaps_register_ovs_idl(ovs_idl_loop.idl);
442 binding_register_ovs_idl(ovs_idl_loop.idl);
443 physical_register_ovs_idl(ovs_idl_loop.idl);
444 ovsdb_idl_get_initial_snapshot(ovs_idl_loop.idl);
446 /* Connect to OVN SB database. */
447 char *ovnsb_remote = get_ovnsb_remote(ovs_idl_loop.idl);
448 struct ovsdb_idl_loop ovnsb_idl_loop = OVSDB_IDL_LOOP_INITIALIZER(
449 ovsdb_idl_create(ovnsb_remote, &sbrec_idl_class, true, true));
450 ovsdb_idl_get_initial_snapshot(ovnsb_idl_loop.idl);
455 struct controller_ctx ctx = {
456 .ovs_idl = ovs_idl_loop.idl,
457 .ovs_idl_txn = ovsdb_idl_loop_run(&ovs_idl_loop),
458 .ovnsb_idl = ovnsb_idl_loop.idl,
459 .ovnsb_idl_txn = ovsdb_idl_loop_run(&ovnsb_idl_loop),
462 const struct ovsrec_bridge *br_int = get_br_int(&ctx);
463 const char *chassis_id = get_chassis_id(ctx.ovs_idl);
465 /* Map bridges to local nets from ovn-bridge-mappings */
467 init_bridge_mappings(&ctx, br_int);
471 chassis_run(&ctx, chassis_id);
472 encaps_run(&ctx, br_int, chassis_id);
473 binding_run(&ctx, br_int, chassis_id);
477 enum mf_field_id mff_ovn_geneve = ofctrl_run(br_int);
479 struct hmap flow_table = HMAP_INITIALIZER(&flow_table);
480 lflow_run(&ctx, &flow_table);
482 physical_run(&ctx, mff_ovn_geneve,
483 br_int, chassis_id, &flow_table);
485 ofctrl_put(&flow_table);
486 hmap_destroy(&flow_table);
489 unixctl_server_run(unixctl);
491 unixctl_server_wait(unixctl);
493 poll_immediate_wake();
496 ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
497 ovsdb_idl_loop_commit_and_wait(&ovs_idl_loop);
503 if (should_service_stop()) {
508 /* It's time to exit. Clean up the databases. */
511 struct controller_ctx ctx = {
512 .ovs_idl = ovs_idl_loop.idl,
513 .ovs_idl_txn = ovsdb_idl_loop_run(&ovs_idl_loop),
514 .ovnsb_idl = ovnsb_idl_loop.idl,
515 .ovnsb_idl_txn = ovsdb_idl_loop_run(&ovnsb_idl_loop),
518 const struct ovsrec_bridge *br_int = get_br_int(&ctx);
519 const char *chassis_id = get_chassis_id(ctx.ovs_idl);
521 /* Run all of the cleanup functions, even if one of them returns false.
522 * We're done if all of them return true. */
523 done = binding_cleanup(&ctx, chassis_id);
524 done = chassis_cleanup(&ctx, chassis_id) && done;
525 done = encaps_cleanup(&ctx, br_int) && done;
527 poll_immediate_wake();
530 ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
531 ovsdb_idl_loop_commit_and_wait(&ovs_idl_loop);
535 unixctl_server_destroy(unixctl);
539 ovsdb_idl_loop_destroy(&ovs_idl_loop);
540 ovsdb_idl_loop_destroy(&ovnsb_idl_loop);
550 parse_options(int argc, char *argv[])
553 OPT_PEER_CA_CERT = UCHAR_MAX + 1,
554 OPT_BOOTSTRAP_CA_CERT,
559 static struct option long_options[] = {
560 {"help", no_argument, NULL, 'h'},
561 {"version", no_argument, NULL, 'V'},
564 STREAM_SSL_LONG_OPTIONS,
565 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
566 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
569 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
574 c = getopt_long(argc, argv, short_options, long_options, NULL);
584 ovs_print_version(OFP13_VERSION, OFP13_VERSION);
588 DAEMON_OPTION_HANDLERS
589 STREAM_SSL_OPTION_HANDLERS
591 case OPT_PEER_CA_CERT:
592 stream_ssl_set_peer_ca_cert_file(optarg);
595 case OPT_BOOTSTRAP_CA_CERT:
596 stream_ssl_set_ca_cert_file(optarg, true);
612 ovs_remote = xasprintf("unix:%s/db.sock", ovs_rundir());
613 } else if (argc == 1) {
614 ovs_remote = xstrdup(argv[0]);
616 VLOG_FATAL("exactly zero or one non-option argument required; "
617 "use --help for usage");
624 printf("%s: OVN controller\n"
625 "usage %s [OPTIONS] [OVS-DATABASE]\n"
626 "where OVS-DATABASE is a socket on which the OVS OVSDB server is listening.\n",
627 program_name, program_name);
628 stream_usage("OVS-DATABASE", true, false, false);
631 printf("\nOther options:\n"
632 " -h, --help display this help message\n"
633 " -V, --version display version information\n");
638 ovn_controller_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
639 const char *argv[] OVS_UNUSED, void *exiting_)
641 bool *exiting = exiting_;
644 unixctl_command_reply(conn, NULL);