From 91ae206597a8944fe0d3a1d9ef1133f90f5e5c1c Mon Sep 17 00:00:00 2001 From: Russell Bryant Date: Thu, 16 Apr 2015 17:13:54 -0400 Subject: ovn: Rename ovn-nbd to ovn-northd. It came up in a recent discussion that the use of 'nbd' could cause confusion since nbd is used for "Network Block Device", which may be used in many of the same environments that OVN would be used in. To avoid the possible issue, just rename ovn-nbd to ovn-northd. While we're at it, create a subdirectory under ovn/ to hold all files related to ovn-northd. For now it's just a single C file, but we may add more source files in the future. Signed-off-by: Russell Bryant Signed-off-by: Ben Pfaff --- ovn/.gitignore | 1 - ovn/TODO | 6 +- ovn/automake.mk | 6 +- ovn/northd/.gitignore | 1 + ovn/northd/automake.mk | 4 + ovn/northd/ovn-northd.c | 518 +++++++++++++++++++++++++++++++++++++++++++++ ovn/ovn-architecture.7.xml | 62 +++--- ovn/ovn-nb.xml | 10 +- ovn/ovn-nbd.c | 518 --------------------------------------------- ovn/ovn-sb.xml | 8 +- tutorial/ovs-sandbox | 12 +- 11 files changed, 574 insertions(+), 572 deletions(-) create mode 100644 ovn/northd/.gitignore create mode 100644 ovn/northd/automake.mk create mode 100644 ovn/northd/ovn-northd.c delete mode 100644 ovn/ovn-nbd.c diff --git a/ovn/.gitignore b/ovn/.gitignore index cbd65a1fb..8f6a63145 100644 --- a/ovn/.gitignore +++ b/ovn/.gitignore @@ -13,4 +13,3 @@ /ovn-sb-idl.ovsidl /ovn-nbctl /ovn-nbctl.8 -/ovn-nbd diff --git a/ovn/TODO b/ovn/TODO index 3b5d97e50..2e563ea8f 100644 --- a/ovn/TODO +++ b/ovn/TODO @@ -2,7 +2,7 @@ ovn-controller is the primary user of flow match expressions, but the same syntax and I imagine the same code ought to be useful in - ovn-nbd for ACL match expressions. + ovn-northd for ACL match expressions. * ovn-controller @@ -118,7 +118,7 @@ Can probably get this from Open_vSwitch database. -* ovn-nbd +* ovn-northd ** Monitor OVN_Northbound database, trigger Pipeline recomputation on change. @@ -234,7 +234,7 @@ No details yet. -** Init scripts for ovn-controller (on HVs), ovn-nbd, OVN DB server. +** Init scripts for ovn-controller (on HVs), ovn-northd, OVN DB server. ** Distribution packaging. diff --git a/ovn/automake.mk b/ovn/automake.mk index 1609d7009..672ef3fc4 100644 --- a/ovn/automake.mk +++ b/ovn/automake.mk @@ -123,10 +123,6 @@ bin_PROGRAMS += ovn/ovn-nbctl ovn_ovn_nbctl_SOURCES = ovn/ovn-nbctl.c ovn_ovn_nbctl_LDADD = ovn/libovn.la ovsdb/libovsdb.la lib/libopenvswitch.la -# ovn-nbd -bin_PROGRAMS += ovn/ovn-nbd -ovn_ovn_nbd_SOURCES = ovn/ovn-nbd.c -ovn_ovn_nbd_LDADD = ovn/libovn.la ovsdb/libovsdb.la lib/libopenvswitch.la - include ovn/controller/automake.mk include ovn/lib/automake.mk +include ovn/northd/automake.mk diff --git a/ovn/northd/.gitignore b/ovn/northd/.gitignore new file mode 100644 index 000000000..c0a87abd9 --- /dev/null +++ b/ovn/northd/.gitignore @@ -0,0 +1 @@ +/ovn-northd diff --git a/ovn/northd/automake.mk b/ovn/northd/automake.mk new file mode 100644 index 000000000..c9c64c0f7 --- /dev/null +++ b/ovn/northd/automake.mk @@ -0,0 +1,4 @@ +# ovn-northd +bin_PROGRAMS += ovn/northd/ovn-northd +ovn_northd_ovn_northd_SOURCES = ovn/northd/ovn-northd.c +ovn_northd_ovn_northd_LDADD = ovn/libovn.la ovsdb/libovsdb.la lib/libopenvswitch.la diff --git a/ovn/northd/ovn-northd.c b/ovn/northd/ovn-northd.c new file mode 100644 index 000000000..241d6ec2a --- /dev/null +++ b/ovn/northd/ovn-northd.c @@ -0,0 +1,518 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include +#include + +#include "command-line.h" +#include "daemon.h" +#include "dirs.h" +#include "fatal-signal.h" +#include "hash.h" +#include "hmap.h" +#include "ovn/ovn-nb-idl.h" +#include "ovn/ovn-sb-idl.h" +#include "poll-loop.h" +#include "stream.h" +#include "stream-ssl.h" +#include "util.h" +#include "uuid.h" +#include "openvswitch/vlog.h" + +VLOG_DEFINE_THIS_MODULE(ovn_nbd); + +struct nbd_context { + struct ovsdb_idl *ovnnb_idl; + struct ovsdb_idl *ovnsb_idl; + struct ovsdb_idl_txn *ovnnb_txn; + struct ovsdb_idl_txn *ovnsb_txn; +}; + +static const char *ovnnb_db; +static const char *ovnsb_db; + +static const char *default_db(void); + +static void +usage(void) +{ + printf("\ +%s: OVN northbound management daemon\n\ +usage: %s [OPTIONS]\n\ +\n\ +Options:\n\ + --ovnnb-db=DATABASE connect to ovn-nb database at DATABASE\n\ + (default: %s)\n\ + --ovnsb-db=DATABASE connect to ovn-sb database at DATABASE\n\ + (default: %s)\n\ + -h, --help display this help message\n\ + -o, --options list available options\n\ + -V, --version display version information\n\ +", program_name, program_name, default_db(), default_db()); + daemon_usage(); + vlog_usage(); + stream_usage("database", true, true, false); +} + +static int +compare_strings(const void *a_, const void *b_) +{ + char *const *a = a_; + char *const *b = b_; + return strcmp(*a, *b); +} + +/* + * Determine whether 2 arrays of MAC addresses are the same. It's possible that + * the lists could be *very* long and this check is being done a lot (every + * time the OVN_Northbound database changes). + */ +static bool +macs_equal(char **binding_macs_, size_t b_n_macs, + char **lport_macs_, size_t l_n_macs) +{ + char **binding_macs, **lport_macs; + size_t bytes, i; + + if (b_n_macs != l_n_macs) { + return false; + } + + bytes = b_n_macs * sizeof binding_macs_[0]; + binding_macs = xmalloc(bytes); + lport_macs = xmalloc(bytes); + + memcpy(binding_macs, binding_macs_, bytes); + memcpy(lport_macs, lport_macs_, bytes); + + qsort(binding_macs, b_n_macs, sizeof binding_macs[0], compare_strings); + qsort(lport_macs, l_n_macs, sizeof lport_macs[0], compare_strings); + + for (i = 0; i < b_n_macs; i++) { + if (strcmp(binding_macs[i], lport_macs[i])) { + break; + } + } + + free(binding_macs); + free(lport_macs); + + return (i == b_n_macs) ? true : false; +} + +/* + * When a change has occurred in the OVN_Northbound database, we go through and + * make sure that the contents of the Bindings table in the OVN_Southbound + * database are up to date with the logical ports defined in the + * OVN_Northbound database. + */ +static void +set_bindings(struct nbd_context *ctx) +{ + struct hmap bindings_hmap; + const struct sbrec_bindings *binding; + const struct nbrec_logical_port *lport; + + struct binding_hash_node { + struct hmap_node node; + const struct sbrec_bindings *binding; + } *hash_node, *hash_node_next; + + /* + * We will need to look up a binding for every logical port. We don't want + * to have to do an O(n) search for every binding, so start out by hashing + * them on the logical port. + * + * As we go through every logical port, we will update the binding if it + * exists or create one otherwise. When the update is done, we'll remove it + * from the hashmap. At the end, any bindings left in the hashmap are for + * logical ports that have been deleted. + */ + hmap_init(&bindings_hmap); + + SBREC_BINDINGS_FOR_EACH(binding, ctx->ovnsb_idl) { + hash_node = xzalloc(sizeof *hash_node); + hash_node->binding = binding; + hmap_insert(&bindings_hmap, &hash_node->node, + hash_string(binding->logical_port, 0)); + } + + NBREC_LOGICAL_PORT_FOR_EACH(lport, ctx->ovnnb_idl) { + binding = NULL; + HMAP_FOR_EACH_WITH_HASH(hash_node, node, + hash_string(lport->name, 0), &bindings_hmap) { + if (!strcmp(lport->name, hash_node->binding->logical_port)) { + binding = hash_node->binding; + break; + } + } + + if (binding) { + /* We found an existing binding for this logical port. Update its + * contents. Right now the only thing we expect that could change + * is the list of MAC addresses. */ + + hmap_remove(&bindings_hmap, &hash_node->node); + free(hash_node); + hash_node = NULL; + + if (!macs_equal(binding->mac, binding->n_mac, + lport->macs, lport->n_macs)) { + sbrec_bindings_set_mac(binding, + (const char **) lport->macs, lport->n_macs); + } + } else { + /* There is no binding for this logical port, so create one. */ + + binding = sbrec_bindings_insert(ctx->ovnsb_txn); + sbrec_bindings_set_logical_port(binding, lport->name); + sbrec_bindings_set_mac(binding, + (const char **) lport->macs, lport->n_macs); + } + } + + HMAP_FOR_EACH_SAFE(hash_node, hash_node_next, node, &bindings_hmap) { + hmap_remove(&bindings_hmap, &hash_node->node); + sbrec_bindings_delete(hash_node->binding); + free(hash_node); + } + hmap_destroy(&bindings_hmap); +} + +static void +ovnnb_db_changed(struct nbd_context *ctx) +{ + VLOG_DBG("ovn-northd: ovn-nb db contents have changed.\n"); + + set_bindings(ctx); +} + +/* + * The only change we get notified about is if the 'chassis' column of the + * 'Bindings' table changes. When this column is not empty, it means we need to + * set the corresponding logical port as 'up' in the northbound DB. + */ +static void +ovnsb_db_changed(struct nbd_context *ctx) +{ + struct hmap lports_hmap; + const struct sbrec_bindings *binding; + const struct nbrec_logical_port *lport; + + struct lport_hash_node { + struct hmap_node node; + const struct nbrec_logical_port *lport; + } *hash_node, *hash_node_next; + + VLOG_DBG("Recalculating port up states for ovn-nb db."); + + hmap_init(&lports_hmap); + + NBREC_LOGICAL_PORT_FOR_EACH(lport, ctx->ovnnb_idl) { + hash_node = xzalloc(sizeof *hash_node); + hash_node->lport = lport; + hmap_insert(&lports_hmap, &hash_node->node, + hash_string(lport->name, 0)); + } + + SBREC_BINDINGS_FOR_EACH(binding, ctx->ovnsb_idl) { + lport = NULL; + HMAP_FOR_EACH_WITH_HASH(hash_node, node, + hash_string(binding->logical_port, 0), &lports_hmap) { + if (!strcmp(binding->logical_port, hash_node->lport->name)) { + lport = hash_node->lport; + break; + } + } + + if (!lport) { + /* The logical port doesn't exist for this binding. This can happen + * under normal circumstances when ovn-nbd hasn't gotten around to + * pruning the Binding yet. */ + continue; + } + + if (*binding->chassis && (!lport->up || !*lport->up)) { + bool up = true; + nbrec_logical_port_set_up(lport, &up, 1); + } else if (!*binding->chassis && (!lport->up || *lport->up)) { + bool up = false; + nbrec_logical_port_set_up(lport, &up, 1); + } + } + + HMAP_FOR_EACH_SAFE(hash_node, hash_node_next, node, &lports_hmap) { + hmap_remove(&lports_hmap, &hash_node->node); + free(hash_node); + } + hmap_destroy(&lports_hmap); +} + +static const char * +default_db(void) +{ + static char *def; + if (!def) { + def = xasprintf("unix:%s/db.sock", ovs_rundir()); + } + return def; +} + +static void +parse_options(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +{ + enum { + DAEMON_OPTION_ENUMS, + VLOG_OPTION_ENUMS, + }; + static const struct option long_options[] = { + {"ovnsb-db", required_argument, NULL, 'd'}, + {"ovnnb-db", required_argument, NULL, 'D'}, + {"help", no_argument, NULL, 'h'}, + {"options", no_argument, NULL, 'o'}, + {"version", no_argument, NULL, 'V'}, + DAEMON_LONG_OPTIONS, + VLOG_LONG_OPTIONS, + STREAM_SSL_LONG_OPTIONS, + {NULL, 0, NULL, 0}, + }; + char *short_options = ovs_cmdl_long_options_to_short_options(long_options); + + for (;;) { + int c; + + c = getopt_long(argc, argv, short_options, long_options, NULL); + if (c == -1) { + break; + } + + switch (c) { + DAEMON_OPTION_HANDLERS; + VLOG_OPTION_HANDLERS; + STREAM_SSL_OPTION_HANDLERS; + + case 'd': + ovnsb_db = optarg; + break; + + case 'D': + ovnnb_db = optarg; + break; + + case 'h': + usage(); + exit(EXIT_SUCCESS); + + case 'o': + ovs_cmdl_print_options(long_options); + exit(EXIT_SUCCESS); + + case 'V': + ovs_print_version(0, 0); + exit(EXIT_SUCCESS); + + default: + break; + } + } + + if (!ovnsb_db) { + ovnsb_db = default_db(); + } + + if (!ovnnb_db) { + ovnnb_db = default_db(); + } + + free(short_options); +} + +int +main(int argc, char *argv[]) +{ + extern struct vlog_module VLM_reconnect; + struct ovsdb_idl *ovnnb_idl, *ovnsb_idl; + unsigned int ovnnb_seqno, ovn_seqno; + int res = EXIT_SUCCESS; + struct nbd_context ctx = { + .ovnsb_txn = NULL, + }; + bool ovnnb_changes_pending = false; + bool ovn_changes_pending = false; + + fatal_ignore_sigpipe(); + set_program_name(argv[0]); + vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN); + vlog_set_levels(&VLM_reconnect, VLF_ANY_DESTINATION, VLL_WARN); + parse_options(argc, argv); + + daemonize(); + + nbrec_init(); + sbrec_init(); + + /* We want to detect all changes to the ovn-nb db. */ + ctx.ovnnb_idl = ovnnb_idl = ovsdb_idl_create(ovnnb_db, + &nbrec_idl_class, true, true); + + /* There is only a small subset of changes to the ovn-sb db that ovn-northd + * has to care about, so we'll enable monitoring those directly. */ + ctx.ovnsb_idl = ovnsb_idl = ovsdb_idl_create(ovnsb_db, + &sbrec_idl_class, false, true); + ovsdb_idl_add_table(ovnsb_idl, &sbrec_table_bindings); + ovsdb_idl_add_column(ovnsb_idl, &sbrec_bindings_col_logical_port); + ovsdb_idl_add_column(ovnsb_idl, &sbrec_bindings_col_chassis); + ovsdb_idl_add_column(ovnsb_idl, &sbrec_bindings_col_mac); + + /* + * The loop here just runs the IDL in a loop waiting for the seqno to + * change, which indicates that the contents of the db have changed. + * + * If the contents of the ovn-nb db change, the mappings to the ovn-sb + * db must be recalculated. + * + * If the contents of the ovn-sb db change, it means the 'up' state of + * a port may have changed, as that's the only type of change ovn-northd is + * watching for. + */ + + ovnnb_seqno = ovsdb_idl_get_seqno(ovnnb_idl); + ovn_seqno = ovsdb_idl_get_seqno(ovnsb_idl); + for (;;) { + ovsdb_idl_run(ovnnb_idl); + ovsdb_idl_run(ovnsb_idl); + + if (!ovsdb_idl_is_alive(ovnnb_idl)) { + int retval = ovsdb_idl_get_last_error(ovnnb_idl); + VLOG_ERR("%s: database connection failed (%s)", + ovnnb_db, ovs_retval_to_string(retval)); + res = EXIT_FAILURE; + break; + } + + if (!ovsdb_idl_is_alive(ovnsb_idl)) { + int retval = ovsdb_idl_get_last_error(ovnsb_idl); + VLOG_ERR("%s: database connection failed (%s)", + ovnsb_db, ovs_retval_to_string(retval)); + res = EXIT_FAILURE; + break; + } + + if (ovnnb_seqno != ovsdb_idl_get_seqno(ovnnb_idl)) { + ovnnb_seqno = ovsdb_idl_get_seqno(ovnnb_idl); + ovnnb_changes_pending = true; + } + + if (ovn_seqno != ovsdb_idl_get_seqno(ovnsb_idl)) { + ovn_seqno = ovsdb_idl_get_seqno(ovnsb_idl); + ovn_changes_pending = true; + } + + /* + * If there are any pending changes, we delay recalculating the + * necessary updates until after an existing transaction finishes. + * This avoids the possibility of rapid updates causing ovn-northd to + * never be able to successfully make the corresponding updates to the + * other db. Instead, pending changes are batched up until the next + * time we get a chance to calculate the new state and apply it. + */ + + if (ovnnb_changes_pending && !ctx.ovnsb_txn) { + /* + * The OVN-nb db contents have changed, so create a transaction for + * updating the OVN-sb DB. + */ + ctx.ovnsb_txn = ovsdb_idl_txn_create(ctx.ovnsb_idl); + ovsdb_idl_txn_add_comment(ctx.ovnsb_txn, + "ovn-northd: northbound db changed"); + ovnnb_db_changed(&ctx); + ovnnb_changes_pending = false; + } + + if (ovn_changes_pending && !ctx.ovnnb_txn) { + /* + * The OVN-sb db contents have changed, so create a transaction for + * updating the northbound DB. + */ + ctx.ovnnb_txn = ovsdb_idl_txn_create(ctx.ovnnb_idl); + ovsdb_idl_txn_add_comment(ctx.ovnnb_txn, + "ovn-northd: southbound db changed"); + ovnsb_db_changed(&ctx); + ovn_changes_pending = false; + } + + if (ctx.ovnnb_txn) { + enum ovsdb_idl_txn_status txn_status; + txn_status = ovsdb_idl_txn_commit(ctx.ovnnb_txn); + switch (txn_status) { + case TXN_UNCOMMITTED: + case TXN_INCOMPLETE: + /* Come back around and try to commit this transaction again */ + break; + case TXN_ABORTED: + case TXN_TRY_AGAIN: + case TXN_NOT_LOCKED: + case TXN_ERROR: + /* Something went wrong, so try creating a new transaction. */ + ovn_changes_pending = true; + case TXN_UNCHANGED: + case TXN_SUCCESS: + ovsdb_idl_txn_destroy(ctx.ovnnb_txn); + ctx.ovnnb_txn = NULL; + } + } + + if (ctx.ovnsb_txn) { + enum ovsdb_idl_txn_status txn_status; + txn_status = ovsdb_idl_txn_commit(ctx.ovnsb_txn); + switch (txn_status) { + case TXN_UNCOMMITTED: + case TXN_INCOMPLETE: + /* Come back around and try to commit this transaction again */ + break; + case TXN_ABORTED: + case TXN_TRY_AGAIN: + case TXN_NOT_LOCKED: + case TXN_ERROR: + /* Something went wrong, so try creating a new transaction. */ + ovnnb_changes_pending = true; + case TXN_UNCHANGED: + case TXN_SUCCESS: + ovsdb_idl_txn_destroy(ctx.ovnsb_txn); + ctx.ovnsb_txn = NULL; + } + } + + if (ovnnb_seqno == ovsdb_idl_get_seqno(ovnnb_idl) && + ovn_seqno == ovsdb_idl_get_seqno(ovnsb_idl)) { + ovsdb_idl_wait(ovnnb_idl); + ovsdb_idl_wait(ovnsb_idl); + if (ctx.ovnnb_txn) { + ovsdb_idl_txn_wait(ctx.ovnnb_txn); + } + if (ctx.ovnsb_txn) { + ovsdb_idl_txn_wait(ctx.ovnsb_txn); + } + poll_block(); + } + } + + ovsdb_idl_destroy(ovnsb_idl); + ovsdb_idl_destroy(ovnnb_idl); + + exit(res); +} diff --git a/ovn/ovn-architecture.7.xml b/ovn/ovn-architecture.7.xml index 69a88743a..9ed162b3b 100644 --- a/ovn/ovn-architecture.7.xml +++ b/ovn/ovn-architecture.7.xml @@ -103,13 +103,13 @@

The OVN Northbound Database has only two clients: the OVN/CMS Plugin - above it and ovn-nbd below it. + above it and ovn-northd below it.

  • - ovn-nbd(8) connects to the OVN Northbound Database above it - and the OVN Southbound Database below it. It translates the + ovn-northd(8) connects to the OVN Northbound Database + above it and the OVN Southbound Database below it. It translates the logical network configuration in terms of conventional network concepts, taken from the OVN Northbound Database, into logical datapath flows in the OVN Southbound Database below it. @@ -118,7 +118,7 @@
  • The OVN Southbound Database is the center of the system. - Its clients are ovn-nbd(8) above it and + Its clients are ovn-northd(8) above it and ovn-controller(8) on every transport node below it.

    @@ -129,7 +129,7 @@ logical network in terms of ``logical datapath flows,'' and Binding tables that link logical network components' locations to the physical network. The hypervisors populate the PN and - Binding tables, whereas ovn-nbd(8) populates the LN + Binding tables, whereas ovn-northd(8) populates the LN tables.

    @@ -177,7 +177,7 @@ | OVN Northbound DB | | | | | | | - | ovn-nbd | + | ovn-northd | | | | +-----------|-----------+ | @@ -292,7 +292,7 @@
  • - ovn-nbd receives the OVN Northbound database update. + ovn-northd receives the OVN Northbound database update. In turn, it makes the corresponding updates to the OVN Southbound database, by adding rows to the OVN Southbound database Pipeline table to reflect the new port, e.g. add a @@ -306,8 +306,8 @@
  • On every hypervisor, ovn-controller receives the - Pipeline table updates that ovn-nbd made in the - previous step. As long as the VM that owns the VIF is powered off, + Pipeline table updates that ovn-northd made + in the previous step. As long as the VM that owns the VIF is powered off, ovn-controller cannot do much; it cannot, for example, arrange to send packets to or receive packets from the VIF, because the VIF does not actually exist anywhere. @@ -337,12 +337,13 @@
  • Some CMS systems, including OpenStack, fully start a VM only when its - networking is ready. To support this, ovn-nbd notices the - chassis column updated for the row in Bindings - table and pushes this upward by updating the column in the OVN Northbound - database's table to indicate - that the VIF is now up. The CMS, if it uses this feature, can then + networking is ready. To support this, ovn-northd notices + the chassis column updated for the row in + Bindings table and pushes this upward by updating the + column in the OVN + Northbound database's table to + indicate that the VIF is now up. The CMS, if it uses this feature, can + then react by allowing the VM's execution to proceed.
  • @@ -389,7 +390,7 @@
  • - ovn-nbd receives the OVN Northbound update and in turn + ovn-northd receives the OVN Northbound update and in turn updates the OVN Southbound database accordingly, by removing or updating the rows from the OVN Southbound database Pipeline table and Bindings table that @@ -398,9 +399,9 @@
  • On every hypervisor, ovn-controller receives the - Pipeline table updates that ovn-nbd made in the - previous step. ovn-controller updates OpenFlow tables to - reflect the update, although there may not be much to do, since the VIF + Pipeline table updates that ovn-northd made + in the previous step. ovn-controller updates OpenFlow tables + to reflect the update, although there may not be much to do, since the VIF had already become unreachable when it was removed from the Bindings table in a previous step.
  • @@ -486,8 +487,8 @@
  • - ovn-nbd receives the OVN Northbound database update. In - turn, it makes the corresponding updates to the OVN Southbound + ovn-northd receives the OVN Northbound database update. + In turn, it makes the corresponding updates to the OVN Southbound database, by adding rows to the OVN Southbound database's Pipeline table to reflect the new port and also by creating a new row in the Bindings table and @@ -498,10 +499,11 @@
  • On every hypervisor, ovn-controller subscribes to the changes in the Bindings table. When a new row is created - by ovn-nbd that includes a value in parent_port - column of Bindings table, the ovn-controller - in the hypervisor whose OVN integration bridge has that same value in - vif-id in external-ids:iface-id + by ovn-northd that includes a value in + parent_port column of Bindings table, the + ovn-controller in the hypervisor whose OVN integration bridge + has that same value in vif-id in + external-ids:iface-id updates the local hypervisor's OpenFlow tables so that packets to and from the VIF with the particular VLAN tag are properly handled. Afterward it updates the chassis column of @@ -510,7 +512,7 @@
  • One can only start the application inside the container after the - underlying network is ready. To support this, ovn-nbd + underlying network is ready. To support this, ovn-northd notices the updated chassis column in Bindings table and updates the column in the OVN Northbound database's @@ -526,7 +528,7 @@
  • - ovn-nbd receives the OVN Northbound update and in turn + ovn-northd receives the OVN Northbound update and in turn updates the OVN Southbound database accordingly, by removing or updating the rows from the OVN Southbound database Pipeline table that were related to the now-destroyed @@ -536,9 +538,9 @@
  • On every hypervisor, ovn-controller receives the - Pipeline table updates that ovn-nbd made in the - previous step. ovn-controller updates OpenFlow tables to - reflect the update. + Pipeline table updates that ovn-northd made + in the previous step. ovn-controller updates OpenFlow tables + to reflect the update.
  • diff --git a/ovn/ovn-nb.xml b/ovn/ovn-nb.xml index 14ee117e7..2d0632e2d 100644 --- a/ovn/ovn-nb.xml +++ b/ovn/ovn-nb.xml @@ -3,8 +3,8 @@

    This database is the interface between OVN and the cloud management system (CMS), such as OpenStack, running above it. The CMS produces almost all of - the contents of the database. The ovn-nbd program monitors - the database contents, transforms it, and stores it into the ovn-northd program + monitors the database contents, transforms it, and stores it into the database.

    @@ -116,10 +116,10 @@ - This column is populated by ovn-nbd, rather than by the CMS - plugin as is most of this database. When a logical port is bound + This column is populated by ovn-northd, rather than by + the CMS plugin as is most of this database. When a logical port is bound to a physical location in the OVN Southbound database table, ovn-nbd + db="OVN_Southbound" table="Bindings"/> table, ovn-northd sets this column to true; otherwise, or if the port becomes unbound later, it sets it to false. This allows the CMS to wait for a VM's (or container's) networking to diff --git a/ovn/ovn-nbd.c b/ovn/ovn-nbd.c deleted file mode 100644 index 27e90c60d..000000000 --- a/ovn/ovn-nbd.c +++ /dev/null @@ -1,518 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include -#include -#include - -#include "command-line.h" -#include "daemon.h" -#include "dirs.h" -#include "fatal-signal.h" -#include "hash.h" -#include "hmap.h" -#include "ovn/ovn-nb-idl.h" -#include "ovn/ovn-sb-idl.h" -#include "poll-loop.h" -#include "stream.h" -#include "stream-ssl.h" -#include "util.h" -#include "uuid.h" -#include "openvswitch/vlog.h" - -VLOG_DEFINE_THIS_MODULE(ovn_nbd); - -struct nbd_context { - struct ovsdb_idl *ovnnb_idl; - struct ovsdb_idl *ovnsb_idl; - struct ovsdb_idl_txn *ovnnb_txn; - struct ovsdb_idl_txn *ovnsb_txn; -}; - -static const char *ovnnb_db; -static const char *ovnsb_db; - -static const char *default_db(void); - -static void -usage(void) -{ - printf("\ -%s: OVN northbound management daemon\n\ -usage: %s [OPTIONS]\n\ -\n\ -Options:\n\ - --ovnnb-db=DATABASE connect to ovn-nb database at DATABASE\n\ - (default: %s)\n\ - --ovnsb-db=DATABASE connect to ovn-sb database at DATABASE\n\ - (default: %s)\n\ - -h, --help display this help message\n\ - -o, --options list available options\n\ - -V, --version display version information\n\ -", program_name, program_name, default_db(), default_db()); - daemon_usage(); - vlog_usage(); - stream_usage("database", true, true, false); -} - -static int -compare_strings(const void *a_, const void *b_) -{ - char *const *a = a_; - char *const *b = b_; - return strcmp(*a, *b); -} - -/* - * Determine whether 2 arrays of MAC addresses are the same. It's possible that - * the lists could be *very* long and this check is being done a lot (every - * time the OVN_Northbound database changes). - */ -static bool -macs_equal(char **binding_macs_, size_t b_n_macs, - char **lport_macs_, size_t l_n_macs) -{ - char **binding_macs, **lport_macs; - size_t bytes, i; - - if (b_n_macs != l_n_macs) { - return false; - } - - bytes = b_n_macs * sizeof binding_macs_[0]; - binding_macs = xmalloc(bytes); - lport_macs = xmalloc(bytes); - - memcpy(binding_macs, binding_macs_, bytes); - memcpy(lport_macs, lport_macs_, bytes); - - qsort(binding_macs, b_n_macs, sizeof binding_macs[0], compare_strings); - qsort(lport_macs, l_n_macs, sizeof lport_macs[0], compare_strings); - - for (i = 0; i < b_n_macs; i++) { - if (strcmp(binding_macs[i], lport_macs[i])) { - break; - } - } - - free(binding_macs); - free(lport_macs); - - return (i == b_n_macs) ? true : false; -} - -/* - * When a change has occurred in the OVN_Northbound database, we go through and - * make sure that the contents of the Bindings table in the OVN_Southbound - * database are up to date with the logical ports defined in the - * OVN_Northbound database. - */ -static void -set_bindings(struct nbd_context *ctx) -{ - struct hmap bindings_hmap; - const struct sbrec_bindings *binding; - const struct nbrec_logical_port *lport; - - struct binding_hash_node { - struct hmap_node node; - const struct sbrec_bindings *binding; - } *hash_node, *hash_node_next; - - /* - * We will need to look up a binding for every logical port. We don't want - * to have to do an O(n) search for every binding, so start out by hashing - * them on the logical port. - * - * As we go through every logical port, we will update the binding if it - * exists or create one otherwise. When the update is done, we'll remove it - * from the hashmap. At the end, any bindings left in the hashmap are for - * logical ports that have been deleted. - */ - hmap_init(&bindings_hmap); - - SBREC_BINDINGS_FOR_EACH(binding, ctx->ovnsb_idl) { - hash_node = xzalloc(sizeof *hash_node); - hash_node->binding = binding; - hmap_insert(&bindings_hmap, &hash_node->node, - hash_string(binding->logical_port, 0)); - } - - NBREC_LOGICAL_PORT_FOR_EACH(lport, ctx->ovnnb_idl) { - binding = NULL; - HMAP_FOR_EACH_WITH_HASH(hash_node, node, - hash_string(lport->name, 0), &bindings_hmap) { - if (!strcmp(lport->name, hash_node->binding->logical_port)) { - binding = hash_node->binding; - break; - } - } - - if (binding) { - /* We found an existing binding for this logical port. Update its - * contents. Right now the only thing we expect that could change - * is the list of MAC addresses. */ - - hmap_remove(&bindings_hmap, &hash_node->node); - free(hash_node); - hash_node = NULL; - - if (!macs_equal(binding->mac, binding->n_mac, - lport->macs, lport->n_macs)) { - sbrec_bindings_set_mac(binding, - (const char **) lport->macs, lport->n_macs); - } - } else { - /* There is no binding for this logical port, so create one. */ - - binding = sbrec_bindings_insert(ctx->ovnsb_txn); - sbrec_bindings_set_logical_port(binding, lport->name); - sbrec_bindings_set_mac(binding, - (const char **) lport->macs, lport->n_macs); - } - } - - HMAP_FOR_EACH_SAFE(hash_node, hash_node_next, node, &bindings_hmap) { - hmap_remove(&bindings_hmap, &hash_node->node); - sbrec_bindings_delete(hash_node->binding); - free(hash_node); - } - hmap_destroy(&bindings_hmap); -} - -static void -ovnnb_db_changed(struct nbd_context *ctx) -{ - VLOG_DBG("ovn-nbd: ovn-nb db contents have changed.\n"); - - set_bindings(ctx); -} - -/* - * The only change we get notified about is if the 'chassis' column of the - * 'Bindings' table changes. When this column is not empty, it means we need to - * set the corresponding logical port as 'up' in the northbound DB. - */ -static void -ovnsb_db_changed(struct nbd_context *ctx) -{ - struct hmap lports_hmap; - const struct sbrec_bindings *binding; - const struct nbrec_logical_port *lport; - - struct lport_hash_node { - struct hmap_node node; - const struct nbrec_logical_port *lport; - } *hash_node, *hash_node_next; - - VLOG_DBG("Recalculating port up states for ovn-nb db."); - - hmap_init(&lports_hmap); - - NBREC_LOGICAL_PORT_FOR_EACH(lport, ctx->ovnnb_idl) { - hash_node = xzalloc(sizeof *hash_node); - hash_node->lport = lport; - hmap_insert(&lports_hmap, &hash_node->node, - hash_string(lport->name, 0)); - } - - SBREC_BINDINGS_FOR_EACH(binding, ctx->ovnsb_idl) { - lport = NULL; - HMAP_FOR_EACH_WITH_HASH(hash_node, node, - hash_string(binding->logical_port, 0), &lports_hmap) { - if (!strcmp(binding->logical_port, hash_node->lport->name)) { - lport = hash_node->lport; - break; - } - } - - if (!lport) { - /* The logical port doesn't exist for this binding. This can happen - * under normal circumstances when ovn-nbd hasn't gotten around to - * pruning the Binding yet. */ - continue; - } - - if (*binding->chassis && (!lport->up || !*lport->up)) { - bool up = true; - nbrec_logical_port_set_up(lport, &up, 1); - } else if (!*binding->chassis && (!lport->up || *lport->up)) { - bool up = false; - nbrec_logical_port_set_up(lport, &up, 1); - } - } - - HMAP_FOR_EACH_SAFE(hash_node, hash_node_next, node, &lports_hmap) { - hmap_remove(&lports_hmap, &hash_node->node); - free(hash_node); - } - hmap_destroy(&lports_hmap); -} - -static const char * -default_db(void) -{ - static char *def; - if (!def) { - def = xasprintf("unix:%s/db.sock", ovs_rundir()); - } - return def; -} - -static void -parse_options(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) -{ - enum { - DAEMON_OPTION_ENUMS, - VLOG_OPTION_ENUMS, - }; - static const struct option long_options[] = { - {"ovnsb-db", required_argument, NULL, 'd'}, - {"ovnnb-db", required_argument, NULL, 'D'}, - {"help", no_argument, NULL, 'h'}, - {"options", no_argument, NULL, 'o'}, - {"version", no_argument, NULL, 'V'}, - DAEMON_LONG_OPTIONS, - VLOG_LONG_OPTIONS, - STREAM_SSL_LONG_OPTIONS, - {NULL, 0, NULL, 0}, - }; - char *short_options = ovs_cmdl_long_options_to_short_options(long_options); - - for (;;) { - int c; - - c = getopt_long(argc, argv, short_options, long_options, NULL); - if (c == -1) { - break; - } - - switch (c) { - DAEMON_OPTION_HANDLERS; - VLOG_OPTION_HANDLERS; - STREAM_SSL_OPTION_HANDLERS; - - case 'd': - ovnsb_db = optarg; - break; - - case 'D': - ovnnb_db = optarg; - break; - - case 'h': - usage(); - exit(EXIT_SUCCESS); - - case 'o': - ovs_cmdl_print_options(long_options); - exit(EXIT_SUCCESS); - - case 'V': - ovs_print_version(0, 0); - exit(EXIT_SUCCESS); - - default: - break; - } - } - - if (!ovnsb_db) { - ovnsb_db = default_db(); - } - - if (!ovnnb_db) { - ovnnb_db = default_db(); - } - - free(short_options); -} - -int -main(int argc, char *argv[]) -{ - extern struct vlog_module VLM_reconnect; - struct ovsdb_idl *ovnnb_idl, *ovnsb_idl; - unsigned int ovnnb_seqno, ovn_seqno; - int res = EXIT_SUCCESS; - struct nbd_context ctx = { - .ovnsb_txn = NULL, - }; - bool ovnnb_changes_pending = false; - bool ovn_changes_pending = false; - - fatal_ignore_sigpipe(); - set_program_name(argv[0]); - vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN); - vlog_set_levels(&VLM_reconnect, VLF_ANY_DESTINATION, VLL_WARN); - parse_options(argc, argv); - - daemonize(); - - nbrec_init(); - sbrec_init(); - - /* We want to detect all changes to the ovn-nb db. */ - ctx.ovnnb_idl = ovnnb_idl = ovsdb_idl_create(ovnnb_db, - &nbrec_idl_class, true, true); - - /* There is only a small subset of changes to the ovn-sb db that ovn-nbd - * has to care about, so we'll enable monitoring those directly. */ - ctx.ovnsb_idl = ovnsb_idl = ovsdb_idl_create(ovnsb_db, - &sbrec_idl_class, false, true); - ovsdb_idl_add_table(ovnsb_idl, &sbrec_table_bindings); - ovsdb_idl_add_column(ovnsb_idl, &sbrec_bindings_col_logical_port); - ovsdb_idl_add_column(ovnsb_idl, &sbrec_bindings_col_chassis); - ovsdb_idl_add_column(ovnsb_idl, &sbrec_bindings_col_mac); - - /* - * The loop here just runs the IDL in a loop waiting for the seqno to - * change, which indicates that the contents of the db have changed. - * - * If the contents of the ovn-nb db change, the mappings to the ovn-sb - * db must be recalculated. - * - * If the contents of the ovn-sb db change, it means the 'up' state of - * a port may have changed, as that's the only type of change ovn-nbd is - * watching for. - */ - - ovnnb_seqno = ovsdb_idl_get_seqno(ovnnb_idl); - ovn_seqno = ovsdb_idl_get_seqno(ovnsb_idl); - for (;;) { - ovsdb_idl_run(ovnnb_idl); - ovsdb_idl_run(ovnsb_idl); - - if (!ovsdb_idl_is_alive(ovnnb_idl)) { - int retval = ovsdb_idl_get_last_error(ovnnb_idl); - VLOG_ERR("%s: database connection failed (%s)", - ovnnb_db, ovs_retval_to_string(retval)); - res = EXIT_FAILURE; - break; - } - - if (!ovsdb_idl_is_alive(ovnsb_idl)) { - int retval = ovsdb_idl_get_last_error(ovnsb_idl); - VLOG_ERR("%s: database connection failed (%s)", - ovnsb_db, ovs_retval_to_string(retval)); - res = EXIT_FAILURE; - break; - } - - if (ovnnb_seqno != ovsdb_idl_get_seqno(ovnnb_idl)) { - ovnnb_seqno = ovsdb_idl_get_seqno(ovnnb_idl); - ovnnb_changes_pending = true; - } - - if (ovn_seqno != ovsdb_idl_get_seqno(ovnsb_idl)) { - ovn_seqno = ovsdb_idl_get_seqno(ovnsb_idl); - ovn_changes_pending = true; - } - - /* - * If there are any pending changes, we delay recalculating the - * necessary updates until after an existing transaction finishes. - * This avoids the possibility of rapid updates causing ovn-nbd to never - * be able to successfully make the corresponding updates to the other - * db. Instead, pending changes are batched up until the next time we - * get a chance to calculate the new state and apply it. - */ - - if (ovnnb_changes_pending && !ctx.ovnsb_txn) { - /* - * The OVN-nb db contents have changed, so create a transaction for - * updating the OVN-sb DB. - */ - ctx.ovnsb_txn = ovsdb_idl_txn_create(ctx.ovnsb_idl); - ovsdb_idl_txn_add_comment(ctx.ovnsb_txn, - "ovn-nbd: northbound db changed"); - ovnnb_db_changed(&ctx); - ovnnb_changes_pending = false; - } - - if (ovn_changes_pending && !ctx.ovnnb_txn) { - /* - * The OVN-sb db contents have changed, so create a transaction for - * updating the northbound DB. - */ - ctx.ovnnb_txn = ovsdb_idl_txn_create(ctx.ovnnb_idl); - ovsdb_idl_txn_add_comment(ctx.ovnnb_txn, - "ovn-nbd: southbound db changed"); - ovnsb_db_changed(&ctx); - ovn_changes_pending = false; - } - - if (ctx.ovnnb_txn) { - enum ovsdb_idl_txn_status txn_status; - txn_status = ovsdb_idl_txn_commit(ctx.ovnnb_txn); - switch (txn_status) { - case TXN_UNCOMMITTED: - case TXN_INCOMPLETE: - /* Come back around and try to commit this transaction again */ - break; - case TXN_ABORTED: - case TXN_TRY_AGAIN: - case TXN_NOT_LOCKED: - case TXN_ERROR: - /* Something went wrong, so try creating a new transaction. */ - ovn_changes_pending = true; - case TXN_UNCHANGED: - case TXN_SUCCESS: - ovsdb_idl_txn_destroy(ctx.ovnnb_txn); - ctx.ovnnb_txn = NULL; - } - } - - if (ctx.ovnsb_txn) { - enum ovsdb_idl_txn_status txn_status; - txn_status = ovsdb_idl_txn_commit(ctx.ovnsb_txn); - switch (txn_status) { - case TXN_UNCOMMITTED: - case TXN_INCOMPLETE: - /* Come back around and try to commit this transaction again */ - break; - case TXN_ABORTED: - case TXN_TRY_AGAIN: - case TXN_NOT_LOCKED: - case TXN_ERROR: - /* Something went wrong, so try creating a new transaction. */ - ovnnb_changes_pending = true; - case TXN_UNCHANGED: - case TXN_SUCCESS: - ovsdb_idl_txn_destroy(ctx.ovnsb_txn); - ctx.ovnsb_txn = NULL; - } - } - - if (ovnnb_seqno == ovsdb_idl_get_seqno(ovnnb_idl) && - ovn_seqno == ovsdb_idl_get_seqno(ovnsb_idl)) { - ovsdb_idl_wait(ovnnb_idl); - ovsdb_idl_wait(ovnsb_idl); - if (ctx.ovnnb_txn) { - ovsdb_idl_txn_wait(ctx.ovnnb_txn); - } - if (ctx.ovnsb_txn) { - ovsdb_idl_txn_wait(ctx.ovnsb_txn); - } - poll_block(); - } - } - - ovsdb_idl_destroy(ovnsb_idl); - ovsdb_idl_destroy(ovnnb_idl); - - exit(res); -} diff --git a/ovn/ovn-sb.xml b/ovn/ovn-sb.xml index 2c6017b83..e4159db66 100644 --- a/ovn/ovn-sb.xml +++ b/ovn/ovn-sb.xml @@ -11,7 +11,7 @@ architecture. It is the one component that speaks both southbound directly to all the hypervisors and gateways, via ovn-controller, and northbound to the Cloud Management - System, via ovn-nbd: + System, via ovn-northd:

    Database Structure

    @@ -64,7 +64,7 @@ That CMS determines the entire OVN logical configuration and therefore the LN's content at any given time is a deterministic function of the CMS's configuration, although that happens indirectly via the OVN Northbound DB - and ovn-nbd. + and ovn-northd.

    @@ -629,8 +629,8 @@

    For every Logical_Port record in OVN_Northbound - database, ovn-nbd creates a record in this table. - ovn-nbd populates and maintains every column except + database, ovn-northd creates a record in this table. + ovn-northd populates and maintains every column except the chassis column, which it leaves empty in new records.

    diff --git a/tutorial/ovs-sandbox b/tutorial/ovs-sandbox index 133585e49..8aa2ddd49 100755 --- a/tutorial/ovs-sandbox +++ b/tutorial/ovs-sandbox @@ -44,7 +44,7 @@ rungdb() { gdb_vswitchd=false gdb_ovsdb=false -gdb_ovn_nbd=false +gdb_ovn_northd=false gdb_ovn_controller=false builddir= srcdir= @@ -94,7 +94,7 @@ These options force ovs-sandbox to use an installed Open vSwitch: -i, --installed use installed Open vSwitch -g, --gdb-vswitchd run ovs-vswitchd under gdb -d, --gdb-ovsdb run ovsdb-server under gdb - --gdb-ovn-nbd run ovn-nbd under gdb + --gdb-ovn-northd run ovn-northd under gdb --gdb-ovn-controller run ovn-controller under gdb -S, --schema=FILE use FILE as vswitch.ovsschema -o, --ovn enable OVN @@ -138,8 +138,8 @@ EOF -d|--gdb-ovsdb) gdb_ovsdb=true ;; - --gdb-ovn-nbd) - gdb_ovn_nbd=true + --gdb-ovn-northd) + gdb_ovn_northd=true ;; --gdb-ovn-controller) gdb_ovn_controller=true @@ -217,7 +217,7 @@ if $built; then fi PATH=$builddir/ovsdb:$builddir/vswitchd:$builddir/utilities:$PATH if $ovn; then - PATH=$builddir/ovn:$builddir/ovn/controller:$PATH + PATH=$builddir/ovn:$builddir/ovn/controller:$builddir/ovn/northd:$PATH fi export PATH else @@ -293,7 +293,7 @@ if $ovn; then ovs-vsctl set open . external-ids:ovn-encap-ip=127.0.0.1 ovs-vsctl add-br br-int - rungdb $gdb_ovn_nbd ovn-nbd --detach --no-chdir --pidfile -vconsole:off --log-file + rungdb $gdb_ovn_northd ovn-northd --detach --no-chdir --pidfile -vconsole:off --log-file rungdb $gdb_ovn_controller ovn-controller --detach --no-chdir --pidfile -vconsole:off --log-file fi -- cgit v1.2.1