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;
59 static char *ovnsb_remote;
63 get_initial_snapshot(struct ovsdb_idl *idl)
67 if (ovsdb_idl_has_ever_connected(idl)) {
75 static const struct ovsrec_bridge *
76 get_bridge(struct controller_ctx *ctx, const char *name)
78 const struct ovsrec_bridge *br;
80 OVSREC_BRIDGE_FOR_EACH(br, ctx->ovs_idl) {
81 if (!strcmp(br->name, name)) {
86 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
87 VLOG_WARN_RL(&rl, "%s: integration bridge does not exist", name);
91 /* Retrieve the OVN integration bridge from the "external-ids:ovn-bridge"
92 * key, the remote location from the "external-ids:ovn-remote" key, and
93 * the chassis name from the "external-ids:system-id" key in the
94 * Open_vSwitch table of the OVS database instance.
96 * xxx ovn-controller does not support changing any of these mid-run,
97 * xxx but that should be addressed later. */
99 get_core_config(struct controller_ctx *ctx, char **br_int_namep,
103 ovsdb_idl_run(ctx->ovs_idl);
105 const struct ovsrec_open_vswitch *cfg;
106 cfg = ovsrec_open_vswitch_first(ctx->ovs_idl);
108 VLOG_ERR("No Open_vSwitch row defined.");
109 ovsdb_idl_destroy(ctx->ovs_idl);
113 const char *remote, *system_id, *br_int_name;
115 br_int_name = smap_get(&cfg->external_ids, "ovn-bridge");
117 br_int_name = DEFAULT_BRIDGE_NAME;
120 remote = smap_get(&cfg->external_ids, "ovn-remote");
122 VLOG_INFO("OVN OVSDB remote not specified. Waiting...");
126 system_id = smap_get(&cfg->external_ids, "system-id");
128 VLOG_INFO("system-id not specified. Waiting...");
132 ovnsb_remote = xstrdup(remote);
133 *chassis_idp = xstrdup(system_id);
134 *br_int_namep = xstrdup(br_int_name);
138 ovsdb_idl_wait(ctx->ovs_idl);
145 struct ovsdb_idl *idl;
146 unsigned int skip_seqno;
148 struct ovsdb_idl_txn *committing_txn;
149 unsigned int precommit_seqno;
151 struct ovsdb_idl_txn *open_txn;
154 #define IDL_LOOP_INITIALIZER(IDL) { .idl = (IDL) }
157 idl_loop_destroy(struct idl_loop *loop)
160 ovsdb_idl_destroy(loop->idl);
164 static struct ovsdb_idl_txn *
165 idl_loop_run(struct idl_loop *loop)
167 ovsdb_idl_run(loop->idl);
168 loop->open_txn = (loop->committing_txn
169 || ovsdb_idl_get_seqno(loop->idl) == loop->skip_seqno
171 : ovsdb_idl_txn_create(loop->idl));
172 return loop->open_txn;
176 idl_loop_commit_and_wait(struct idl_loop *loop)
178 if (loop->open_txn) {
179 loop->committing_txn = loop->open_txn;
180 loop->open_txn = NULL;
182 loop->precommit_seqno = ovsdb_idl_get_seqno(loop->idl);
185 struct ovsdb_idl_txn *txn = loop->committing_txn;
187 enum ovsdb_idl_txn_status status = ovsdb_idl_txn_commit(txn);
188 if (status != TXN_INCOMPLETE) {
191 /* We want to re-evaluate the database when it's changed from
192 * the contents that it had when we started the commit. (That
193 * might have already happened.) */
194 loop->skip_seqno = loop->precommit_seqno;
195 if (ovsdb_idl_get_seqno(loop->idl) != loop->skip_seqno) {
196 poll_immediate_wake();
201 /* If the database has already changed since we started the
202 * commit, re-evaluate it immediately to avoid missing a change
204 if (ovsdb_idl_get_seqno(loop->idl) != loop->precommit_seqno) {
205 poll_immediate_wake();
215 case TXN_UNCOMMITTED:
220 ovsdb_idl_txn_destroy(txn);
221 loop->committing_txn = NULL;
225 ovsdb_idl_wait(loop->idl);
229 main(int argc, char *argv[])
231 struct unixctl_server *unixctl;
232 struct controller_ctx ctx = { .ovs_idl = NULL };
236 ovs_cmdl_proctitle_init(argc, argv);
237 set_program_name(argv[0]);
238 parse_options(argc, argv);
239 fatal_ignore_sigpipe();
243 retval = unixctl_server_create(NULL, &unixctl);
247 unixctl_command_register("exit", "", 0, 0, ovn_controller_exit, &exiting);
249 daemonize_complete();
256 /* Connect to OVS OVSDB instance. We do not monitor all tables by
257 * default, so modules must register their interest explicitly. */
258 ctx.ovs_idl = ovsdb_idl_create(ovs_remote, &ovsrec_idl_class, false, true);
260 /* Register interest in "external_ids" column in "Open_vSwitch" table,
261 * since we'll need to get the OVN OVSDB remote. */
262 ovsdb_idl_add_table(ctx.ovs_idl, &ovsrec_table_open_vswitch);
263 ovsdb_idl_add_column(ctx.ovs_idl, &ovsrec_open_vswitch_col_external_ids);
271 get_initial_snapshot(ctx.ovs_idl);
273 char *br_int_name, *chassis_id;
274 get_core_config(&ctx, &br_int_name, &chassis_id);
276 ctx.ovnsb_idl = ovsdb_idl_create(ovnsb_remote, &sbrec_idl_class,
278 get_initial_snapshot(ctx.ovnsb_idl);
280 struct idl_loop ovnsb_idl_loop = IDL_LOOP_INITIALIZER(ctx.ovnsb_idl);
281 struct idl_loop ovs_idl_loop = IDL_LOOP_INITIALIZER(ctx.ovs_idl);
286 ctx.ovnsb_idl_txn = idl_loop_run(&ovnsb_idl_loop);
287 ctx.ovs_idl_txn = idl_loop_run(&ovs_idl_loop);
289 const struct ovsrec_bridge *br_int = get_bridge(&ctx, br_int_name);
291 chassis_run(&ctx, chassis_id);
292 encaps_run(&ctx, br_int, chassis_id);
293 binding_run(&ctx, br_int, chassis_id);
296 struct hmap flow_table = HMAP_INITIALIZER(&flow_table);
297 pipeline_run(&ctx, &flow_table);
298 physical_run(&ctx, br_int, chassis_id, &flow_table);
299 ofctrl_run(br_int, &flow_table);
300 hmap_destroy(&flow_table);
303 unixctl_server_run(unixctl);
305 unixctl_server_wait(unixctl);
307 poll_immediate_wake();
310 idl_loop_commit_and_wait(&ovnsb_idl_loop);
311 idl_loop_commit_and_wait(&ovs_idl_loop);
319 /* It's time to exit. Clean up the databases. */
322 ctx.ovnsb_idl_txn = idl_loop_run(&ovnsb_idl_loop);
323 ctx.ovs_idl_txn = idl_loop_run(&ovs_idl_loop);
325 const struct ovsrec_bridge *br_int = get_bridge(&ctx, br_int_name);
327 /* Run all of the cleanup functions, even if one of them returns false.
328 * We're done if all of them return true. */
329 done = binding_cleanup(&ctx, chassis_id);
330 done = chassis_cleanup(&ctx, chassis_id) && done;
331 done = encaps_cleanup(&ctx, br_int) && done;
333 poll_immediate_wake();
336 idl_loop_commit_and_wait(&ovnsb_idl_loop);
337 idl_loop_commit_and_wait(&ovs_idl_loop);
341 unixctl_server_destroy(unixctl);
342 pipeline_destroy(&ctx);
345 idl_loop_destroy(&ovs_idl_loop);
346 idl_loop_destroy(&ovnsb_idl_loop);
357 parse_options(int argc, char *argv[])
360 OPT_PEER_CA_CERT = UCHAR_MAX + 1,
365 static struct option long_options[] = {
366 {"help", no_argument, NULL, 'h'},
367 {"version", no_argument, NULL, 'V'},
370 STREAM_SSL_LONG_OPTIONS,
371 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
374 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
379 c = getopt_long(argc, argv, short_options, long_options, NULL);
389 ovs_print_version(OFP13_VERSION, OFP13_VERSION);
393 DAEMON_OPTION_HANDLERS
394 STREAM_SSL_OPTION_HANDLERS
396 case OPT_PEER_CA_CERT:
397 stream_ssl_set_peer_ca_cert_file(optarg);
413 ovs_remote = xasprintf("unix:%s/db.sock", ovs_rundir());
414 } else if (argc == 1) {
415 ovs_remote = xstrdup(argv[0]);
417 VLOG_FATAL("exactly zero or one non-option argument required; "
418 "use --help for usage");
425 printf("%s: OVN controller\n"
426 "usage %s [OPTIONS] [OVS-DATABASE]\n"
427 "where OVS-DATABASE is a socket on which the OVS OVSDB server is listening.\n",
428 program_name, program_name);
429 stream_usage("OVS-DATABASE", true, false, false);
432 printf("\nOther options:\n"
433 " -h, --help display this help message\n"
434 " -V, --version display version information\n");
439 ovn_controller_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
440 const char *argv[] OVS_UNUSED, void *exiting_)
442 bool *exiting = exiting_;
445 unixctl_command_reply(conn, NULL);