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"
51 VLOG_DEFINE_THIS_MODULE(main);
53 static unixctl_cb_func ovn_controller_exit;
54 static unixctl_cb_func ct_zone_list;
56 #define DEFAULT_BRIDGE_NAME "br-int"
58 static void parse_options(int argc, char *argv[]);
59 OVS_NO_RETURN static void usage(void);
61 static char *ovs_remote;
63 const struct sbrec_chassis *
64 get_chassis(struct ovsdb_idl *ovnsb_idl, const char *chassis_id)
66 const struct sbrec_chassis *chassis_rec;
68 SBREC_CHASSIS_FOR_EACH(chassis_rec, ovnsb_idl) {
69 if (!strcmp(chassis_rec->name, chassis_id)) {
78 get_tunnel_type(const char *name)
80 if (!strcmp(name, "geneve")) {
82 } else if (!strcmp(name, "stt")) {
84 } else if (!strcmp(name, "vxlan")) {
91 const struct ovsrec_bridge *
92 get_bridge(struct ovsdb_idl *ovs_idl, const char *br_name)
94 const struct ovsrec_bridge *br;
95 OVSREC_BRIDGE_FOR_EACH (br, ovs_idl) {
96 if (!strcmp(br->name, br_name)) {
103 static const struct ovsrec_bridge *
104 create_br_int(struct controller_ctx *ctx,
105 const struct ovsrec_open_vswitch *cfg,
106 const char *bridge_name)
108 if (!ctx->ovs_idl_txn) {
112 ovsdb_idl_txn_add_comment(ctx->ovs_idl_txn,
113 "ovn-controller: creating integration bridge '%s'", bridge_name);
115 struct ovsrec_interface *iface;
116 iface = ovsrec_interface_insert(ctx->ovs_idl_txn);
117 ovsrec_interface_set_name(iface, bridge_name);
118 ovsrec_interface_set_type(iface, "internal");
120 struct ovsrec_port *port;
121 port = ovsrec_port_insert(ctx->ovs_idl_txn);
122 ovsrec_port_set_name(port, bridge_name);
123 ovsrec_port_set_interfaces(port, &iface, 1);
125 struct ovsrec_bridge *bridge;
126 bridge = ovsrec_bridge_insert(ctx->ovs_idl_txn);
127 ovsrec_bridge_set_name(bridge, bridge_name);
128 ovsrec_bridge_set_fail_mode(bridge, "secure");
129 const struct smap oc = SMAP_CONST1(&oc, "disable-in-band", "true");
130 ovsrec_bridge_set_other_config(bridge, &oc);
131 ovsrec_bridge_set_ports(bridge, &port, 1);
133 struct ovsrec_bridge **bridges;
134 size_t bytes = sizeof *bridges * cfg->n_bridges;
135 bridges = xmalloc(bytes + sizeof *bridges);
136 memcpy(bridges, cfg->bridges, bytes);
137 bridges[cfg->n_bridges] = bridge;
138 ovsrec_open_vswitch_verify_bridges(cfg);
139 ovsrec_open_vswitch_set_bridges(cfg, bridges, cfg->n_bridges + 1);
144 static const struct ovsrec_bridge *
145 get_br_int(struct controller_ctx *ctx)
147 const struct ovsrec_open_vswitch *cfg;
148 cfg = ovsrec_open_vswitch_first(ctx->ovs_idl);
153 const char *br_int_name = smap_get(&cfg->external_ids, "ovn-bridge");
155 br_int_name = DEFAULT_BRIDGE_NAME;
158 const struct ovsrec_bridge *br;
159 br = get_bridge(ctx->ovs_idl, br_int_name);
161 return create_br_int(ctx, cfg, br_int_name);
167 get_chassis_id(const struct ovsdb_idl *ovs_idl)
169 const struct ovsrec_open_vswitch *cfg = ovsrec_open_vswitch_first(ovs_idl);
170 return cfg ? smap_get(&cfg->external_ids, "system-id") : NULL;
173 /* Retrieves the OVN Southbound remote location from the
174 * "external-ids:ovn-remote" key in 'ovs_idl' and returns a copy of it.
176 * XXX ovn-controller does not support this changing mid-run, but that should
177 * be addressed later. */
179 get_ovnsb_remote(struct ovsdb_idl *ovs_idl)
182 ovsdb_idl_run(ovs_idl);
184 const struct ovsrec_open_vswitch *cfg
185 = ovsrec_open_vswitch_first(ovs_idl);
187 const char *remote = smap_get(&cfg->external_ids, "ovn-remote");
189 return xstrdup(remote);
193 VLOG_INFO("OVN OVSDB remote not specified. Waiting...");
194 ovsdb_idl_wait(ovs_idl);
200 main(int argc, char *argv[])
202 struct unixctl_server *unixctl;
206 ovs_cmdl_proctitle_init(argc, argv);
207 set_program_name(argv[0]);
208 service_start(&argc, &argv);
209 parse_options(argc, argv);
210 fatal_ignore_sigpipe();
212 daemonize_start(false);
214 retval = unixctl_server_create(NULL, &unixctl);
218 unixctl_command_register("exit", "", 0, 0, ovn_controller_exit, &exiting);
220 daemonize_complete();
228 /* Connect to OVS OVSDB instance. We do not monitor all tables by
229 * default, so modules must register their interest explicitly. */
230 struct ovsdb_idl_loop ovs_idl_loop = OVSDB_IDL_LOOP_INITIALIZER(
231 ovsdb_idl_create(ovs_remote, &ovsrec_idl_class, false, true));
232 ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_open_vswitch);
233 ovsdb_idl_add_column(ovs_idl_loop.idl,
234 &ovsrec_open_vswitch_col_external_ids);
235 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_open_vswitch_col_bridges);
236 ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_interface);
237 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_interface_col_name);
238 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_interface_col_type);
239 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_interface_col_options);
240 ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_port);
241 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_port_col_name);
242 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_port_col_interfaces);
243 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_port_col_external_ids);
244 ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_bridge);
245 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_ports);
246 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_name);
247 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_fail_mode);
248 ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_other_config);
249 chassis_register_ovs_idl(ovs_idl_loop.idl);
250 encaps_register_ovs_idl(ovs_idl_loop.idl);
251 binding_register_ovs_idl(ovs_idl_loop.idl);
252 physical_register_ovs_idl(ovs_idl_loop.idl);
253 ovsdb_idl_get_initial_snapshot(ovs_idl_loop.idl);
255 /* Connect to OVN SB database. */
256 char *ovnsb_remote = get_ovnsb_remote(ovs_idl_loop.idl);
257 struct ovsdb_idl_loop ovnsb_idl_loop = OVSDB_IDL_LOOP_INITIALIZER(
258 ovsdb_idl_create(ovnsb_remote, &sbrec_idl_class, true, true));
259 ovsdb_idl_get_initial_snapshot(ovnsb_idl_loop.idl);
261 /* Initialize connection tracking zones. */
262 struct simap ct_zones = SIMAP_INITIALIZER(&ct_zones);
263 unsigned long ct_zone_bitmap[BITMAP_N_LONGS(MAX_CT_ZONES)];
264 bitmap_set1(ct_zone_bitmap, 0); /* Zone 0 is reserved. */
265 unixctl_command_register("ct-zone-list", "", 0, 0,
266 ct_zone_list, &ct_zones);
271 struct controller_ctx ctx = {
272 .ovs_idl = ovs_idl_loop.idl,
273 .ovs_idl_txn = ovsdb_idl_loop_run(&ovs_idl_loop),
274 .ovnsb_idl = ovnsb_idl_loop.idl,
275 .ovnsb_idl_txn = ovsdb_idl_loop_run(&ovnsb_idl_loop),
278 const struct ovsrec_bridge *br_int = get_br_int(&ctx);
279 const char *chassis_id = get_chassis_id(ctx.ovs_idl);
281 /* Map bridges to local nets from ovn-bridge-mappings */
283 patch_run(&ctx, br_int);
287 chassis_run(&ctx, chassis_id);
288 encaps_run(&ctx, br_int, chassis_id);
289 binding_run(&ctx, br_int, chassis_id, &ct_zones, ct_zone_bitmap);
293 enum mf_field_id mff_ovn_geneve = ofctrl_run(br_int);
295 struct hmap flow_table = HMAP_INITIALIZER(&flow_table);
296 lflow_run(&ctx, &flow_table, &ct_zones);
298 physical_run(&ctx, mff_ovn_geneve,
299 br_int, chassis_id, &ct_zones, &flow_table);
301 ofctrl_put(&flow_table);
302 hmap_destroy(&flow_table);
305 unixctl_server_run(unixctl);
307 unixctl_server_wait(unixctl);
309 poll_immediate_wake();
312 ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
313 ovsdb_idl_loop_commit_and_wait(&ovs_idl_loop);
319 if (should_service_stop()) {
324 /* It's time to exit. Clean up the databases. */
327 struct controller_ctx ctx = {
328 .ovs_idl = ovs_idl_loop.idl,
329 .ovs_idl_txn = ovsdb_idl_loop_run(&ovs_idl_loop),
330 .ovnsb_idl = ovnsb_idl_loop.idl,
331 .ovnsb_idl_txn = ovsdb_idl_loop_run(&ovnsb_idl_loop),
334 const struct ovsrec_bridge *br_int = get_br_int(&ctx);
335 const char *chassis_id = get_chassis_id(ctx.ovs_idl);
337 /* Run all of the cleanup functions, even if one of them returns false.
338 * We're done if all of them return true. */
339 done = binding_cleanup(&ctx, chassis_id);
340 done = chassis_cleanup(&ctx, chassis_id) && done;
341 done = encaps_cleanup(&ctx, br_int) && done;
343 poll_immediate_wake();
346 ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
347 ovsdb_idl_loop_commit_and_wait(&ovs_idl_loop);
351 unixctl_server_destroy(unixctl);
355 simap_destroy(&ct_zones);
357 ovsdb_idl_loop_destroy(&ovs_idl_loop);
358 ovsdb_idl_loop_destroy(&ovnsb_idl_loop);
368 parse_options(int argc, char *argv[])
371 OPT_PEER_CA_CERT = UCHAR_MAX + 1,
372 OPT_BOOTSTRAP_CA_CERT,
377 static struct option long_options[] = {
378 {"help", no_argument, NULL, 'h'},
379 {"version", no_argument, NULL, 'V'},
382 STREAM_SSL_LONG_OPTIONS,
383 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
384 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
387 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
392 c = getopt_long(argc, argv, short_options, long_options, NULL);
402 ovs_print_version(OFP13_VERSION, OFP13_VERSION);
406 DAEMON_OPTION_HANDLERS
407 STREAM_SSL_OPTION_HANDLERS
409 case OPT_PEER_CA_CERT:
410 stream_ssl_set_peer_ca_cert_file(optarg);
413 case OPT_BOOTSTRAP_CA_CERT:
414 stream_ssl_set_ca_cert_file(optarg, true);
430 ovs_remote = xasprintf("unix:%s/db.sock", ovs_rundir());
431 } else if (argc == 1) {
432 ovs_remote = xstrdup(argv[0]);
434 VLOG_FATAL("exactly zero or one non-option argument required; "
435 "use --help for usage");
442 printf("%s: OVN controller\n"
443 "usage %s [OPTIONS] [OVS-DATABASE]\n"
444 "where OVS-DATABASE is a socket on which the OVS OVSDB server is listening.\n",
445 program_name, program_name);
446 stream_usage("OVS-DATABASE", true, false, false);
449 printf("\nOther options:\n"
450 " -h, --help display this help message\n"
451 " -V, --version display version information\n");
456 ovn_controller_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
457 const char *argv[] OVS_UNUSED, void *exiting_)
459 bool *exiting = exiting_;
462 unixctl_command_reply(conn, NULL);
466 ct_zone_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
467 const char *argv[] OVS_UNUSED, void *ct_zones_)
469 struct simap *ct_zones = ct_zones_;
470 struct ds ds = DS_EMPTY_INITIALIZER;
471 struct simap_node *zone;
473 SIMAP_FOR_EACH(zone, ct_zones) {
474 ds_put_format(&ds, "%s %d\n", zone->name, zone->data);
477 unixctl_command_reply(conn, ds_cstr(&ds));