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"
37 #include "lib/vswitch-idl.h"
40 #include "stream-ssl.h"
53 VLOG_DEFINE_THIS_MODULE(main);
55 static unixctl_cb_func ovn_controller_exit;
56 static unixctl_cb_func ct_zone_list;
58 #define DEFAULT_BRIDGE_NAME "br-int"
60 static void parse_options(int argc, char *argv[]);
61 OVS_NO_RETURN static void usage(void);
63 static char *ovs_remote;
65 const struct sbrec_chassis *
66 get_chassis(struct ovsdb_idl *ovnsb_idl, const char *chassis_id)
68 const struct sbrec_chassis *chassis_rec;
70 SBREC_CHASSIS_FOR_EACH(chassis_rec, ovnsb_idl) {
71 if (!strcmp(chassis_rec->name, chassis_id)) {
80 get_tunnel_type(const char *name)
82 if (!strcmp(name, "geneve")) {
84 } else if (!strcmp(name, "stt")) {
86 } else if (!strcmp(name, "vxlan")) {
93 const struct ovsrec_bridge *
94 get_bridge(struct ovsdb_idl *ovs_idl, const char *br_name)
96 const struct ovsrec_bridge *br;
97 OVSREC_BRIDGE_FOR_EACH (br, ovs_idl) {
98 if (!strcmp(br->name, br_name)) {
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)
110 if (!ctx->ovs_idl_txn) {
114 ovsdb_idl_txn_add_comment(ctx->ovs_idl_txn,
115 "ovn-controller: creating integration bridge '%s'", bridge_name);
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");
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);
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);
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);
146 static const struct ovsrec_bridge *
147 get_br_int(struct controller_ctx *ctx)
149 const struct ovsrec_open_vswitch *cfg;
150 cfg = ovsrec_open_vswitch_first(ctx->ovs_idl);
155 const char *br_int_name = smap_get(&cfg->external_ids, "ovn-bridge");
157 br_int_name = DEFAULT_BRIDGE_NAME;
160 const struct ovsrec_bridge *br;
161 br = get_bridge(ctx->ovs_idl, br_int_name);
163 return create_br_int(ctx, cfg, br_int_name);
169 get_chassis_id(const struct ovsdb_idl *ovs_idl)
171 const struct ovsrec_open_vswitch *cfg = ovsrec_open_vswitch_first(ovs_idl);
172 return cfg ? smap_get(&cfg->external_ids, "system-id") : NULL;
175 /* Retrieves the OVN Southbound remote location from the
176 * "external-ids:ovn-remote" key in 'ovs_idl' and returns a copy of it.
178 * XXX ovn-controller does not support this changing mid-run, but that should
179 * be addressed later. */
181 get_ovnsb_remote(struct ovsdb_idl *ovs_idl)
184 ovsdb_idl_run(ovs_idl);
186 const struct ovsrec_open_vswitch *cfg
187 = ovsrec_open_vswitch_first(ovs_idl);
189 const char *remote = smap_get(&cfg->external_ids, "ovn-remote");
191 return xstrdup(remote);
195 VLOG_INFO("OVN OVSDB remote not specified. Waiting...");
196 ovsdb_idl_wait(ovs_idl);
202 main(int argc, char *argv[])
204 struct unixctl_server *unixctl;
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();
214 daemonize_start(false);
216 retval = unixctl_server_create(NULL, &unixctl);
220 unixctl_command_register("exit", "", 0, 0, ovn_controller_exit, &exiting);
222 daemonize_complete();
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);
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);
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);
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),
281 /* Contains bare "struct hmap_node"s whose hash values are the tunnel_key
282 * of datapaths with at least one local port binding. */
283 struct hmap local_datapaths = HMAP_INITIALIZER(&local_datapaths);
285 const struct ovsrec_bridge *br_int = get_br_int(&ctx);
286 const char *chassis_id = get_chassis_id(ctx.ovs_idl);
288 /* Map bridges to local nets from ovn-bridge-mappings */
290 patch_run(&ctx, br_int);
294 chassis_run(&ctx, chassis_id);
295 encaps_run(&ctx, br_int, chassis_id);
296 binding_run(&ctx, br_int, chassis_id, &ct_zones, ct_zone_bitmap,
301 enum mf_field_id mff_ovn_geneve = ofctrl_run(br_int);
303 pinctrl_run(&ctx, br_int);
305 struct hmap flow_table = HMAP_INITIALIZER(&flow_table);
306 lflow_run(&ctx, &flow_table, &ct_zones);
308 physical_run(&ctx, mff_ovn_geneve,
309 br_int, chassis_id, &ct_zones, &flow_table,
312 ofctrl_put(&flow_table);
313 hmap_destroy(&flow_table);
316 /* local_datapaths contains bare hmap_node instances.
317 * We use this wrapper so that we can make use of
318 * HMAP_FOR_EACH_SAFE to tear down the hmap. */
320 struct hmap_node node;
321 } *cur_node, *next_node;
322 HMAP_FOR_EACH_SAFE (cur_node, next_node, node, &local_datapaths) {
323 hmap_remove(&local_datapaths, &cur_node->node);
326 hmap_destroy(&local_datapaths);
328 unixctl_server_run(unixctl);
330 unixctl_server_wait(unixctl);
332 poll_immediate_wake();
335 ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
336 ovsdb_idl_loop_commit_and_wait(&ovs_idl_loop);
343 if (should_service_stop()) {
348 /* It's time to exit. Clean up the databases. */
351 struct controller_ctx ctx = {
352 .ovs_idl = ovs_idl_loop.idl,
353 .ovs_idl_txn = ovsdb_idl_loop_run(&ovs_idl_loop),
354 .ovnsb_idl = ovnsb_idl_loop.idl,
355 .ovnsb_idl_txn = ovsdb_idl_loop_run(&ovnsb_idl_loop),
358 const struct ovsrec_bridge *br_int = get_br_int(&ctx);
359 const char *chassis_id = get_chassis_id(ctx.ovs_idl);
361 /* Run all of the cleanup functions, even if one of them returns false.
362 * We're done if all of them return true. */
363 done = binding_cleanup(&ctx, chassis_id);
364 done = chassis_cleanup(&ctx, chassis_id) && done;
365 done = encaps_cleanup(&ctx, br_int) && done;
367 poll_immediate_wake();
370 ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
371 ovsdb_idl_loop_commit_and_wait(&ovs_idl_loop);
375 unixctl_server_destroy(unixctl);
380 simap_destroy(&ct_zones);
382 ovsdb_idl_loop_destroy(&ovs_idl_loop);
383 ovsdb_idl_loop_destroy(&ovnsb_idl_loop);
393 parse_options(int argc, char *argv[])
396 OPT_PEER_CA_CERT = UCHAR_MAX + 1,
397 OPT_BOOTSTRAP_CA_CERT,
402 static struct option long_options[] = {
403 {"help", no_argument, NULL, 'h'},
404 {"version", no_argument, NULL, 'V'},
407 STREAM_SSL_LONG_OPTIONS,
408 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
409 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
412 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
417 c = getopt_long(argc, argv, short_options, long_options, NULL);
427 ovs_print_version(OFP13_VERSION, OFP13_VERSION);
431 DAEMON_OPTION_HANDLERS
432 STREAM_SSL_OPTION_HANDLERS
434 case OPT_PEER_CA_CERT:
435 stream_ssl_set_peer_ca_cert_file(optarg);
438 case OPT_BOOTSTRAP_CA_CERT:
439 stream_ssl_set_ca_cert_file(optarg, true);
455 ovs_remote = xasprintf("unix:%s/db.sock", ovs_rundir());
456 } else if (argc == 1) {
457 ovs_remote = xstrdup(argv[0]);
459 VLOG_FATAL("exactly zero or one non-option argument required; "
460 "use --help for usage");
467 printf("%s: OVN controller\n"
468 "usage %s [OPTIONS] [OVS-DATABASE]\n"
469 "where OVS-DATABASE is a socket on which the OVS OVSDB server is listening.\n",
470 program_name, program_name);
471 stream_usage("OVS-DATABASE", true, false, false);
474 printf("\nOther options:\n"
475 " -h, --help display this help message\n"
476 " -V, --version display version information\n");
481 ovn_controller_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
482 const char *argv[] OVS_UNUSED, void *exiting_)
484 bool *exiting = exiting_;
487 unixctl_command_reply(conn, NULL);
491 ct_zone_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
492 const char *argv[] OVS_UNUSED, void *ct_zones_)
494 struct simap *ct_zones = ct_zones_;
495 struct ds ds = DS_EMPTY_INITIALIZER;
496 struct simap_node *zone;
498 SIMAP_FOR_EACH(zone, ct_zones) {
499 ds_put_format(&ds, "%s %d\n", zone->name, zone->data);
502 unixctl_command_reply(conn, ds_cstr(&ds));