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 "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"
39 #include "stream-ssl.h"
52 VLOG_DEFINE_THIS_MODULE(main);
54 static unixctl_cb_func ovn_controller_exit;
55 static unixctl_cb_func ct_zone_list;
57 #define DEFAULT_BRIDGE_NAME "br-int"
59 static void parse_options(int argc, char *argv[]);
60 OVS_NO_RETURN static void usage(void);
62 static char *ovs_remote;
64 const struct sbrec_chassis *
65 get_chassis(struct ovsdb_idl *ovnsb_idl, const char *chassis_id)
67 const struct sbrec_chassis *chassis_rec;
69 SBREC_CHASSIS_FOR_EACH(chassis_rec, ovnsb_idl) {
70 if (!strcmp(chassis_rec->name, chassis_id)) {
79 get_tunnel_type(const char *name)
81 if (!strcmp(name, "geneve")) {
83 } else if (!strcmp(name, "stt")) {
85 } else if (!strcmp(name, "vxlan")) {
92 const struct ovsrec_bridge *
93 get_bridge(struct ovsdb_idl *ovs_idl, const char *br_name)
95 const struct ovsrec_bridge *br;
96 OVSREC_BRIDGE_FOR_EACH (br, ovs_idl) {
97 if (!strcmp(br->name, br_name)) {
104 static const struct ovsrec_bridge *
105 create_br_int(struct controller_ctx *ctx,
106 const struct ovsrec_open_vswitch *cfg,
107 const char *bridge_name)
109 if (!ctx->ovs_idl_txn) {
113 ovsdb_idl_txn_add_comment(ctx->ovs_idl_txn,
114 "ovn-controller: creating integration bridge '%s'", bridge_name);
116 struct ovsrec_interface *iface;
117 iface = ovsrec_interface_insert(ctx->ovs_idl_txn);
118 ovsrec_interface_set_name(iface, bridge_name);
119 ovsrec_interface_set_type(iface, "internal");
121 struct ovsrec_port *port;
122 port = ovsrec_port_insert(ctx->ovs_idl_txn);
123 ovsrec_port_set_name(port, bridge_name);
124 ovsrec_port_set_interfaces(port, &iface, 1);
126 struct ovsrec_bridge *bridge;
127 bridge = ovsrec_bridge_insert(ctx->ovs_idl_txn);
128 ovsrec_bridge_set_name(bridge, bridge_name);
129 ovsrec_bridge_set_fail_mode(bridge, "secure");
130 const struct smap oc = SMAP_CONST1(&oc, "disable-in-band", "true");
131 ovsrec_bridge_set_other_config(bridge, &oc);
132 ovsrec_bridge_set_ports(bridge, &port, 1);
134 struct ovsrec_bridge **bridges;
135 size_t bytes = sizeof *bridges * cfg->n_bridges;
136 bridges = xmalloc(bytes + sizeof *bridges);
137 memcpy(bridges, cfg->bridges, bytes);
138 bridges[cfg->n_bridges] = bridge;
139 ovsrec_open_vswitch_verify_bridges(cfg);
140 ovsrec_open_vswitch_set_bridges(cfg, bridges, cfg->n_bridges + 1);
145 static const struct ovsrec_bridge *
146 get_br_int(struct controller_ctx *ctx)
148 const struct ovsrec_open_vswitch *cfg;
149 cfg = ovsrec_open_vswitch_first(ctx->ovs_idl);
154 const char *br_int_name = smap_get(&cfg->external_ids, "ovn-bridge");
156 br_int_name = DEFAULT_BRIDGE_NAME;
159 const struct ovsrec_bridge *br;
160 br = get_bridge(ctx->ovs_idl, br_int_name);
162 return create_br_int(ctx, cfg, br_int_name);
168 get_chassis_id(const struct ovsdb_idl *ovs_idl)
170 const struct ovsrec_open_vswitch *cfg = ovsrec_open_vswitch_first(ovs_idl);
171 return cfg ? smap_get(&cfg->external_ids, "system-id") : NULL;
174 /* Retrieves the OVN Southbound remote location from the
175 * "external-ids:ovn-remote" key in 'ovs_idl' and returns a copy of it.
177 * XXX ovn-controller does not support this changing mid-run, but that should
178 * be addressed later. */
180 get_ovnsb_remote(struct ovsdb_idl *ovs_idl)
183 ovsdb_idl_run(ovs_idl);
185 const struct ovsrec_open_vswitch *cfg
186 = ovsrec_open_vswitch_first(ovs_idl);
188 const char *remote = smap_get(&cfg->external_ids, "ovn-remote");
190 return xstrdup(remote);
194 VLOG_INFO("OVN OVSDB remote not specified. Waiting...");
195 ovsdb_idl_wait(ovs_idl);
201 main(int argc, char *argv[])
203 struct unixctl_server *unixctl;
207 ovs_cmdl_proctitle_init(argc, argv);
208 set_program_name(argv[0]);
209 service_start(&argc, &argv);
210 parse_options(argc, argv);
211 fatal_ignore_sigpipe();
213 daemonize_start(false);
215 retval = unixctl_server_create(NULL, &unixctl);
219 unixctl_command_register("exit", "", 0, 0, ovn_controller_exit, &exiting);
221 daemonize_complete();
230 /* Connect to OVS OVSDB instance. We do not monitor all tables by
231 * default, so modules must register their interest explicitly. */
232 struct ovsdb_idl_loop ovs_idl_loop = OVSDB_IDL_LOOP_INITIALIZER(
233 ovsdb_idl_create(ovs_remote, &ovsrec_idl_class, false, true));
234 ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_open_vswitch);
235 ovsdb_idl_add_column(ovs_idl_loop.idl,
236 &ovsrec_open_vswitch_col_external_ids);
237 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_open_vswitch_col_bridges);
238 ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_interface);
239 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_interface_col_name);
240 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_interface_col_type);
241 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_interface_col_options);
242 ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_port);
243 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_port_col_name);
244 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_port_col_interfaces);
245 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_port_col_external_ids);
246 ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_bridge);
247 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_ports);
248 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_name);
249 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_fail_mode);
250 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_other_config);
251 chassis_register_ovs_idl(ovs_idl_loop.idl);
252 encaps_register_ovs_idl(ovs_idl_loop.idl);
253 binding_register_ovs_idl(ovs_idl_loop.idl);
254 physical_register_ovs_idl(ovs_idl_loop.idl);
255 ovsdb_idl_get_initial_snapshot(ovs_idl_loop.idl);
257 /* Connect to OVN SB database. */
258 char *ovnsb_remote = get_ovnsb_remote(ovs_idl_loop.idl);
259 struct ovsdb_idl_loop ovnsb_idl_loop = OVSDB_IDL_LOOP_INITIALIZER(
260 ovsdb_idl_create(ovnsb_remote, &sbrec_idl_class, true, true));
261 ovsdb_idl_get_initial_snapshot(ovnsb_idl_loop.idl);
263 /* Initialize connection tracking zones. */
264 struct simap ct_zones = SIMAP_INITIALIZER(&ct_zones);
265 unsigned long ct_zone_bitmap[BITMAP_N_LONGS(MAX_CT_ZONES)];
266 bitmap_set1(ct_zone_bitmap, 0); /* Zone 0 is reserved. */
267 unixctl_command_register("ct-zone-list", "", 0, 0,
268 ct_zone_list, &ct_zones);
273 struct controller_ctx ctx = {
274 .ovs_idl = ovs_idl_loop.idl,
275 .ovs_idl_txn = ovsdb_idl_loop_run(&ovs_idl_loop),
276 .ovnsb_idl = ovnsb_idl_loop.idl,
277 .ovnsb_idl_txn = ovsdb_idl_loop_run(&ovnsb_idl_loop),
280 const struct ovsrec_bridge *br_int = get_br_int(&ctx);
281 const char *chassis_id = get_chassis_id(ctx.ovs_idl);
283 /* Map bridges to local nets from ovn-bridge-mappings */
285 patch_run(&ctx, br_int);
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);
295 enum mf_field_id mff_ovn_geneve = ofctrl_run(br_int);
297 pinctrl_run(&ctx, br_int);
299 struct hmap flow_table = HMAP_INITIALIZER(&flow_table);
300 lflow_run(&ctx, &flow_table, &ct_zones);
302 physical_run(&ctx, mff_ovn_geneve,
303 br_int, chassis_id, &ct_zones, &flow_table);
305 ofctrl_put(&flow_table);
306 hmap_destroy(&flow_table);
309 unixctl_server_run(unixctl);
311 unixctl_server_wait(unixctl);
313 poll_immediate_wake();
316 ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
317 ovsdb_idl_loop_commit_and_wait(&ovs_idl_loop);
324 if (should_service_stop()) {
329 /* It's time to exit. Clean up the databases. */
332 struct controller_ctx ctx = {
333 .ovs_idl = ovs_idl_loop.idl,
334 .ovs_idl_txn = ovsdb_idl_loop_run(&ovs_idl_loop),
335 .ovnsb_idl = ovnsb_idl_loop.idl,
336 .ovnsb_idl_txn = ovsdb_idl_loop_run(&ovnsb_idl_loop),
339 const struct ovsrec_bridge *br_int = get_br_int(&ctx);
340 const char *chassis_id = get_chassis_id(ctx.ovs_idl);
342 /* Run all of the cleanup functions, even if one of them returns false.
343 * We're done if all of them return true. */
344 done = binding_cleanup(&ctx, chassis_id);
345 done = chassis_cleanup(&ctx, chassis_id) && done;
346 done = encaps_cleanup(&ctx, br_int) && done;
348 poll_immediate_wake();
351 ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
352 ovsdb_idl_loop_commit_and_wait(&ovs_idl_loop);
356 unixctl_server_destroy(unixctl);
361 simap_destroy(&ct_zones);
363 ovsdb_idl_loop_destroy(&ovs_idl_loop);
364 ovsdb_idl_loop_destroy(&ovnsb_idl_loop);
374 parse_options(int argc, char *argv[])
377 OPT_PEER_CA_CERT = UCHAR_MAX + 1,
378 OPT_BOOTSTRAP_CA_CERT,
383 static struct option long_options[] = {
384 {"help", no_argument, NULL, 'h'},
385 {"version", no_argument, NULL, 'V'},
388 STREAM_SSL_LONG_OPTIONS,
389 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
390 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
393 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
398 c = getopt_long(argc, argv, short_options, long_options, NULL);
408 ovs_print_version(OFP13_VERSION, OFP13_VERSION);
412 DAEMON_OPTION_HANDLERS
413 STREAM_SSL_OPTION_HANDLERS
415 case OPT_PEER_CA_CERT:
416 stream_ssl_set_peer_ca_cert_file(optarg);
419 case OPT_BOOTSTRAP_CA_CERT:
420 stream_ssl_set_ca_cert_file(optarg, true);
436 ovs_remote = xasprintf("unix:%s/db.sock", ovs_rundir());
437 } else if (argc == 1) {
438 ovs_remote = xstrdup(argv[0]);
440 VLOG_FATAL("exactly zero or one non-option argument required; "
441 "use --help for usage");
448 printf("%s: OVN controller\n"
449 "usage %s [OPTIONS] [OVS-DATABASE]\n"
450 "where OVS-DATABASE is a socket on which the OVS OVSDB server is listening.\n",
451 program_name, program_name);
452 stream_usage("OVS-DATABASE", true, false, false);
455 printf("\nOther options:\n"
456 " -h, --help display this help message\n"
457 " -V, --version display version information\n");
462 ovn_controller_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
463 const char *argv[] OVS_UNUSED, void *exiting_)
465 bool *exiting = exiting_;
468 unixctl_command_reply(conn, NULL);
472 ct_zone_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
473 const char *argv[] OVS_UNUSED, void *ct_zones_)
475 struct simap *ct_zones = ct_zones_;
476 struct ds ds = DS_EMPTY_INITIALIZER;
477 struct simap_node *zone;
479 SIMAP_FOR_EACH(zone, ct_zones) {
480 ds_put_format(&ds, "%s %d\n", zone->name, zone->data);
483 unixctl_command_reply(conn, ds_cstr(&ds));