From patchwork Mon Oct 26 18:16:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Pfaff X-Patchwork-Id: 1387972 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.136; helo=silver.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=ovn.org Received: from silver.osuosl.org (smtp3.osuosl.org [140.211.166.136]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CKjjb0v5pz9sSW for ; Tue, 27 Oct 2020 05:17:22 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id 5AC002E15E; Mon, 26 Oct 2020 18:17:21 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from silver.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id U6HosyGXIXxw; Mon, 26 Oct 2020 18:17:13 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by silver.osuosl.org (Postfix) with ESMTP id F0E882E15D; Mon, 26 Oct 2020 18:16:59 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id C9B29C1D7C; Mon, 26 Oct 2020 18:16:59 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from silver.osuosl.org (smtp3.osuosl.org [140.211.166.136]) by lists.linuxfoundation.org (Postfix) with ESMTP id 88D80C1DA4 for ; Mon, 26 Oct 2020 18:16:57 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id E45A12E150 for ; Mon, 26 Oct 2020 18:16:56 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from silver.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 3YiDapJ+k-wZ for ; Mon, 26 Oct 2020 18:16:43 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from relay11.mail.gandi.net (relay11.mail.gandi.net [217.70.178.231]) by silver.osuosl.org (Postfix) with ESMTPS id 5484C2E141 for ; Mon, 26 Oct 2020 18:16:42 +0000 (UTC) Received: from sigfpe.attlocal.net (75-54-222-30.lightspeed.rdcyca.sbcglobal.net [75.54.222.30]) (Authenticated sender: blp@ovn.org) by relay11.mail.gandi.net (Postfix) with ESMTPSA id 073A1100006; Mon, 26 Oct 2020 18:16:39 +0000 (UTC) From: Ben Pfaff To: dev@openvswitch.org Date: Mon, 26 Oct 2020 11:16:26 -0700 Message-Id: <20201026181626.1827014-7-blp@ovn.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201026181626.1827014-1-blp@ovn.org> References: <20201026181626.1827014-1-blp@ovn.org> MIME-Version: 1.0 Cc: Ben Pfaff Subject: [ovs-dev] [PATCH ovn 7/7] northd: Enhance implementation of port tunnel key requests. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" When a tunnel key was requested, the implementation was not smart enough to reassign a port that had been automatically assigned the same key. This fixes the problem and adds a test. Signed-off-by: Ben Pfaff Acked-by: Numan Siddique --- northd/ovn-northd.c | 142 ++++++++++++++++++++++++-------------------- tests/ovn-northd.at | 59 +++++++++++++++++- 2 files changed, 136 insertions(+), 65 deletions(-) diff --git a/northd/ovn-northd.c b/northd/ovn-northd.c index 92d578c405a2..66b0a81267cc 100644 --- a/northd/ovn-northd.c +++ b/northd/ovn-northd.c @@ -1427,6 +1427,8 @@ struct ovn_port { const struct sbrec_port_binding *sb; /* May be NULL. */ + uint32_t tunnel_key; + /* Logical switch port data. */ const struct nbrec_logical_switch_port *nbsp; /* May be NULL. */ @@ -1465,13 +1467,6 @@ struct ovn_port { struct ovs_list list; /* In list of similar records. */ }; -static void -ovn_port_set_sb(struct ovn_port *op, - const struct sbrec_port_binding *sb) -{ - op->sb = sb; -} - static void ovn_port_set_nb(struct ovn_port *op, const struct nbrec_logical_switch_port *nbsp, @@ -1495,7 +1490,7 @@ ovn_port_create(struct hmap *ports, const char *key, op->json_key = ds_steal_cstr(&json_key); op->key = xstrdup(key); - ovn_port_set_sb(op, sb); + op->sb = sb; ovn_port_set_nb(op, nbsp, nbrp); op->derived = false; hmap_insert(ports, &op->key_node, hash_string(op->key, 0)); @@ -1541,13 +1536,6 @@ ovn_port_find(const struct hmap *ports, const char *name) return NULL; } -static uint32_t -ovn_port_allocate_key(struct ovn_datapath *od) -{ - return ovn_allocate_tnlid(&od->port_tnlids, "port", - 1, (1u << 15) - 1, &od->port_key_hint); -} - /* Returns true if the logical switch port 'enabled' column is empty or * set to true. Otherwise, returns false. */ static bool @@ -2979,15 +2967,6 @@ copy_gw_chassis_from_nbrp_to_sbpb( free(sb_ha_chassis); } -static int64_t -op_get_requested_tnl_key(const struct ovn_port *op) -{ - ovs_assert(op->nbsp || op->nbrp); - const struct smap *op_options = op->nbsp ? &op->nbsp->options - : &op->nbrp->options; - return smap_get_int(op_options, "requested-tnl-key", 0); -} - static const char* op_get_name(const struct ovn_port *op) { @@ -3304,17 +3283,8 @@ ovn_port_update_sbrec(struct northd_context *ctx, sbrec_port_binding_set_external_ids(op->sb, &ids); smap_destroy(&ids); } - int64_t tnl_key = op_get_requested_tnl_key(op); - if (tnl_key && tnl_key != op->sb->tunnel_key) { - if (!ovn_add_tnlid(&op->od->port_tnlids, tnl_key)) { - static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1); - VLOG_WARN_RL(&rl, "Cannot update port binding for " - "%s due to duplicate key set " - "in options:requested-tnl-key: %"PRId64, - op_get_name(op), tnl_key); - } else { - sbrec_port_binding_set_tunnel_key(op->sb, tnl_key); - } + if (op->tunnel_key != op->sb->tunnel_key) { + sbrec_port_binding_set_tunnel_key(op->sb, op->tunnel_key); } } @@ -3707,6 +3677,52 @@ destroy_ovn_lbs(struct hmap *lbs) } } +static bool +ovn_port_add_tnlid(struct ovn_port *op, uint32_t tunnel_key) +{ + bool added = ovn_add_tnlid(&op->od->port_tnlids, tunnel_key); + if (added) { + op->tunnel_key = tunnel_key; + if (tunnel_key > op->od->port_key_hint) { + op->od->port_key_hint = tunnel_key; + } + } + return added; +} + +static void +ovn_port_assign_requested_tnl_id(struct ovn_port *op) +{ + const struct smap *options = (op->nbsp + ? &op->nbsp->options + : &op->nbrp->options); + uint32_t tunnel_key = smap_get_int(options, "requested-tnl-key", 0); + if (tunnel_key && !ovn_port_add_tnlid(op, tunnel_key)) { + static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1); + VLOG_WARN_RL(&rl, "Logical %s port %s requests same tunnel key " + "%"PRIu32" as another LSP or LRP", + op->nbsp ? "switch" : "router", + op_get_name(op), tunnel_key); + } +} + +static void +ovn_port_allocate_key(struct hmap *ports, struct ovn_port *op) +{ + if (!op->tunnel_key) { + op->tunnel_key = ovn_allocate_tnlid(&op->od->port_tnlids, "port", + 1, (1u << 15) - 1, + &op->od->port_key_hint); + if (!op->tunnel_key) { + if (op->sb) { + sbrec_port_binding_delete(op->sb); + } + ovs_list_remove(&op->list); + ovn_port_destroy(ports, op); + } + } +} + /* Updates the southbound Port_Binding table so that it contains the logical * switch ports specified by the northbound database. * @@ -3732,15 +3748,30 @@ build_ports(struct northd_context *ctx, /* Purge stale Mac_Bindings if ports are deleted. */ bool remove_mac_bindings = !ovs_list_is_empty(&sb_only); + /* Assign explicitly requested tunnel ids first. */ struct ovn_port *op, *next; - /* For logical ports that are in both databases, index the in-use - * tunnel_keys. */ LIST_FOR_EACH (op, list, &both) { - ovn_add_tnlid(&op->od->port_tnlids, op->sb->tunnel_key); - if (op->sb->tunnel_key > op->od->port_key_hint) { - op->od->port_key_hint = op->sb->tunnel_key; + ovn_port_assign_requested_tnl_id(op); + } + LIST_FOR_EACH (op, list, &nb_only) { + ovn_port_assign_requested_tnl_id(op); + } + + /* Keep nonconflicting tunnel IDs that are already assigned. */ + LIST_FOR_EACH (op, list, &both) { + if (!op->tunnel_key) { + ovn_port_add_tnlid(op, op->sb->tunnel_key); } } + + /* Assign new tunnel ids where needed. */ + LIST_FOR_EACH_SAFE (op, next, list, &both) { + ovn_port_allocate_key(ports, op); + } + LIST_FOR_EACH_SAFE (op, next, list, &nb_only) { + ovn_port_allocate_key(ports, op); + } + /* For logical ports that are in both databases, update the southbound * record based on northbound data. * For logical ports that are in NB database, do any tag allocation @@ -3762,38 +3793,21 @@ build_ports(struct northd_context *ctx, /* Add southbound record for each unmatched northbound record. */ LIST_FOR_EACH_SAFE (op, next, list, &nb_only) { - int64_t tunnel_key = op_get_requested_tnl_key(op); - if (tunnel_key && !ovn_add_tnlid(&op->od->port_tnlids, tunnel_key)) { - static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1); - VLOG_WARN_RL(&rl, "Cannot create port binding for " - "%s due to duplicate key set " - "in options:requested-tnl-key: %"PRId64, - op_get_name(op), tunnel_key); - continue; - } - - if (!tunnel_key) { - tunnel_key = ovn_port_allocate_key(op->od); - if (!tunnel_key) { - continue; - } - } - - ovn_port_set_sb(op, sbrec_port_binding_insert(ctx->ovnsb_txn)); + op->sb = sbrec_port_binding_insert(ctx->ovnsb_txn); ovn_port_update_sbrec(ctx, sbrec_chassis_by_name, op, &chassis_qdisc_queues, &active_ha_chassis_grps); sbrec_port_binding_set_logical_port(op->sb, op->key); - sbrec_port_binding_set_tunnel_key(op->sb, tunnel_key); } /* Delete southbound records without northbound matches. */ - LIST_FOR_EACH_SAFE(op, next, list, &sb_only) { - ovs_list_remove(&op->list); - sbrec_port_binding_delete(op->sb); - ovn_port_destroy(ports, op); + if (!ovs_list_is_empty(&sb_only)) { + LIST_FOR_EACH_SAFE(op, next, list, &sb_only) { + ovs_list_remove(&op->list); + sbrec_port_binding_delete(op->sb); + ovn_port_destroy(ports, op); + } } - if (remove_mac_bindings) { cleanup_mac_bindings(ctx, datapaths, ports); } diff --git a/tests/ovn-northd.at b/tests/ovn-northd.at index 9ad562ee15f7..946f20b6a176 100644 --- a/tests/ovn-northd.at +++ b/tests/ovn-northd.at @@ -2068,7 +2068,7 @@ action=(reg0 = 0; reject { /* eth.dst <-> eth.src; ip.dst <-> ip.src; is implici AT_CLEANUP -AT_SETUP([requested-tnl-key]) +AT_SETUP([datapath requested-tnl-key]) AT_KEYWORDS([requested tnl tunnel key keys]) ovn_start @@ -2113,3 +2113,60 @@ AT_CHECK( get_tunnel_keys AT_CHECK([test $ls2 = 3]) AT_CLEANUP +]) + +AT_SETUP([port requested-tnl-key]) +AT_KEYWORDS([requested tnl tunnel key keys]) +ovn_start + +get_tunnel_keys() { + set $(ovn-sbctl get port_binding lsp00 tunnel_key \ + -- get port_binding lsp01 tunnel_key \ + -- get port_binding lsp02 tunnel_key \ + -- get port_binding lsp10 tunnel_key \ + -- get port_binding lsp11 tunnel_key \ + -- get port_binding lsp12 tunnel_key) + lsp00=$1 lsp01=$2 lsp02=$3 lsp10=$4 lsp11=$5 lsp12=$6 + ls0=$1$2$3 ls1=$4$5$6 + echo "ls0=$1$2$3 ls1=$4$5$6" + AT_CHECK([test "$lsp00" != "$lsp01" && \ + test "$lsp01" != "$lsp02" && \ + test "$lsp00" != "$lsp02"]) + AT_CHECK([test "$lsp10" != "$lsp11" && \ + test "$lsp11" != "$lsp12" && \ + test "$lsp10" != "$lsp12"]) +} + +echo +echo "__file__:__line__: Add two logical switches with three ports each, check tunnel ids" +AT_CHECK( + [for i in 0 1; do + ovn-nbctl --wait=sb ls-add ls$i || exit $? + for j in 0 1 2; do + ovn-nbctl --wait=sb lsp-add ls$i lsp$i$j || exit $? + done + done]) +get_tunnel_keys +AT_CHECK([test $ls0 = 123 && test $ls1 = 123]) + +echo +echo "__file__:__line__: Assign lsp00 new tunnel key, others don't change." +AT_CHECK( + [ovn-nbctl --wait=sb set logical-switch-port lsp00 options:requested-tnl-key=4]) +get_tunnel_keys +AT_CHECK([test $ls0 = 423 && test $ls1 = 123]) + +echo +echo "__file__:__line__: Assign lsp00 a conflict with lsp01, which moves aside." +AT_CHECK( + [ovn-nbctl --wait=sb set logical-switch-port lsp00 options:requested-tnl-key=2]) +get_tunnel_keys +AT_CHECK([test $lsp00 = 2 && test $lsp02 = 3 && test $ls1 = 123]) + +echo +echo "__file__:__line__: Assign lsp00 and lsp01 conflicts and verify that they end up different and lsp02 doesn't change." +AT_CHECK( + [ovn-nbctl --wait=sb set logical-switch-port lsp01 options:requested-tnl-key=2]) +get_tunnel_keys +AT_CHECK([test $lsp02 = 3 && test $ls1 = 123]) +AT_CLEANUP