From patchwork Fri Sep 27 22:34:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han Zhou X-Patchwork-Id: 1168753 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="udV4KTiD"; dkim-atps=neutral Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46g69t2M9zz9sN1 for ; Sat, 28 Sep 2019 08:37:22 +1000 (AEST) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 5623B1223; Fri, 27 Sep 2019 22:34:38 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id AD64311F5 for ; Fri, 27 Sep 2019 22:34:35 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-ed1-f68.google.com (mail-ed1-f68.google.com [209.85.208.68]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id AE67491C for ; Fri, 27 Sep 2019 22:34:34 +0000 (UTC) Received: by mail-ed1-f68.google.com with SMTP id l21so3647741edr.5 for ; Fri, 27 Sep 2019 15:34:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=1/meX5hZMilYUIwIdcjfGvbb47+SHnkTlZnizVx+Lxw=; b=udV4KTiDHxNa4TP3C379F4Fwnlx57L+Zx5zlIjf0G2yAKwKr/dG7Dr3saVoF+vw3gF QXxAlZfuEzn6fZH/EudBv7y8npCDdcK4rH00SbWH/PNVV7lMMYCuZ80ADK3fiEOychiV k9ZzK4pixQbRFLlZgISUKKwGleozjYHO4FeGUlpXiSpFarztk24jLY5G6KLwBW4wKdFR JgzzP9q22GFbgfd3eTTzF220KRMPi0BYnUjS5RYAHIGgh+AANjBTcQamuas7Iu0/2b9q 1q4tkUGLWIJ9bCUzibLTN3Vg//ekbJTk7i7F6CePk4BtRdPHij6PfblLdaYpXtj4H2WZ bmgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=1/meX5hZMilYUIwIdcjfGvbb47+SHnkTlZnizVx+Lxw=; b=Lk11PYPgiJOBBC3AvUC8dqJYFS9NvuFbcbUtsa6qq1HLeuqtTQFfgOJJ4vxPwvL1hF 0dB2P/P7c/jQ45gTZ5ERjpAs3RqIIThrWDa5yi1fKxPJ2Km8euEbj96cPxkXvTX1SGx6 T6NSA+2ghRkTD6yIlmukH87k6IpHeLd7swMLgl6pVNMa3toGt0uifeV/xjr9yD8JZkJA I5AywkNiH3E5uyXiRjVhaRW2dNVlOLn0TpDyj0V/ge0n7k2uoA4cx+RV711oM2FZf0iy ozrNVVi1i/yadiHX7fG+3hP6w11Xcav/JZNBrJj94G9Qxk6/NNwSar93ACIeQpg8FjZe zSUA== X-Gm-Message-State: APjAAAV/Wjd4HFc2uRLB1HHNlTJ2jTfsTCOqZIVYISN8K6osMfyy7AE/ xlp3JUqeJlg7Aj3T0XTccKsNJkUi X-Google-Smtp-Source: APXvYqx8YQ640pd3R2jPCZg27+xuZ2mnN9NMsKcsNCiG1E+6yWxS525BzOG/kAw2UmWQmonHjddjMA== X-Received: by 2002:a17:906:5214:: with SMTP id g20mr9624324ejm.220.1569623673002; Fri, 27 Sep 2019 15:34:33 -0700 (PDT) Received: from localhost.localdomain.localdomain ([216.113.160.77]) by smtp.gmail.com with ESMTPSA id ng5sm715794ejb.9.2019.09.27.15.34.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 27 Sep 2019 15:34:32 -0700 (PDT) From: Han Zhou X-Google-Original-From: Han Zhou To: dev@openvswitch.org Date: Fri, 27 Sep 2019 15:34:21 -0700 Message-Id: <1569623665-77390-7-git-send-email-hzhou8@ebay.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1569623665-77390-1-git-send-email-hzhou8@ebay.com> References: <1569623665-77390-1-git-send-email-hzhou8@ebay.com> X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Subject: [ovs-dev] [RFC PATCH ovn 06/10] ovn-northd.c: Refactor allocate_tnlid. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org From: Han Zhou Move allocate_tnlid() and related interfaces to ovn_util module, so that they be reused by ovn-ic (in next patches). At the same time, define macros for the range of datapath tunnel keys, and reserve a range with ((1u << 16) - 1) keys for global transit switch datapaths, among the ((1u << 24) - 1) datapath tunnel key space. Signed-off-by: Han Zhou --- lib/ovn-util.c | 59 +++++++++++++++++++++++++++++++++++++ lib/ovn-util.h | 11 +++++++ northd/ovn-northd.c | 85 +++++++++-------------------------------------------- 3 files changed, 84 insertions(+), 71 deletions(-) diff --git a/lib/ovn-util.c b/lib/ovn-util.c index 8d7db44..950e86f 100644 --- a/lib/ovn-util.c +++ b/lib/ovn-util.c @@ -449,3 +449,62 @@ ovn_logical_flow_hash(const struct uuid *logical_datapath, hash = hash_string(match, hash); return hash_string(actions, hash); } + +struct tnlid_node { + struct hmap_node hmap_node; + uint32_t tnlid; +}; + +void +ovn_destroy_tnlids(struct hmap *tnlids) +{ + struct tnlid_node *node; + HMAP_FOR_EACH_POP (node, hmap_node, tnlids) { + free(node); + } + hmap_destroy(tnlids); +} + +void +ovn_add_tnlid(struct hmap *set, uint32_t tnlid) +{ + struct tnlid_node *node = xmalloc(sizeof *node); + hmap_insert(set, &node->hmap_node, hash_int(tnlid, 0)); + node->tnlid = tnlid; +} + +static bool +tnlid_in_use(const struct hmap *set, uint32_t tnlid) +{ + const struct tnlid_node *node; + HMAP_FOR_EACH_IN_BUCKET (node, hmap_node, hash_int(tnlid, 0), set) { + if (node->tnlid == tnlid) { + return true; + } + } + return false; +} + +static uint32_t +next_tnlid(uint32_t tnlid, uint32_t min, uint32_t max) +{ + return tnlid + 1 <= max ? tnlid + 1 : min; +} + +uint32_t +ovn_allocate_tnlid(struct hmap *set, const char *name, uint32_t min, uint32_t max, + uint32_t *hint) +{ + for (uint32_t tnlid = next_tnlid(*hint, min, max); tnlid != *hint; + tnlid = next_tnlid(tnlid, min, max)) { + if (!tnlid_in_use(set, tnlid)) { + ovn_add_tnlid(set, tnlid); + *hint = tnlid; + return tnlid; + } + } + + static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1); + VLOG_WARN_RL(&rl, "all %s tunnel ids exhausted", name); + return 0; +} diff --git a/lib/ovn-util.h b/lib/ovn-util.h index 694c953..b9bda8d 100644 --- a/lib/ovn-util.h +++ b/lib/ovn-util.h @@ -89,4 +89,15 @@ uint32_t ovn_logical_flow_hash(const struct uuid *logical_datapath, uint16_t priority, const char *match, const char *actions); +#define OVN_MAX_DP_KEY ((1u << 24) - 1) +#define OVN_MAX_DP_GLOBAL_NUM ((1u << 16) - 1) +#define OVN_MIN_DP_KEY_LOCAL 1 +#define OVN_MAX_DP_KEY_LOCAL (OVN_MAX_DP_KEY - OVN_MAX_DP_GLOBAL_NUM) +#define OVN_MIN_DP_KEY_GLOBAL (OVN_MAX_DP_KEY_LOCAL + 1) +#define OVN_MAX_DP_KEY_GLOBAL OVN_MAX_DP_KEY +struct hmap; +void ovn_destroy_tnlids(struct hmap *tnlids); +void ovn_add_tnlid(struct hmap *set, uint32_t tnlid); +uint32_t ovn_allocate_tnlid(struct hmap *set, const char *name, uint32_t min, + uint32_t max, uint32_t *hint); #endif diff --git a/northd/ovn-northd.c b/northd/ovn-northd.c index 1bb60ce..f99adfb 100644 --- a/northd/ovn-northd.c +++ b/northd/ovn-northd.c @@ -279,65 +279,6 @@ Options:\n\ stream_usage("database", true, true, false); } -struct tnlid_node { - struct hmap_node hmap_node; - uint32_t tnlid; -}; - -static void -destroy_tnlids(struct hmap *tnlids) -{ - struct tnlid_node *node; - HMAP_FOR_EACH_POP (node, hmap_node, tnlids) { - free(node); - } - hmap_destroy(tnlids); -} - -static void -add_tnlid(struct hmap *set, uint32_t tnlid) -{ - struct tnlid_node *node = xmalloc(sizeof *node); - hmap_insert(set, &node->hmap_node, hash_int(tnlid, 0)); - node->tnlid = tnlid; -} - -static bool -tnlid_in_use(const struct hmap *set, uint32_t tnlid) -{ - const struct tnlid_node *node; - HMAP_FOR_EACH_IN_BUCKET (node, hmap_node, hash_int(tnlid, 0), set) { - if (node->tnlid == tnlid) { - return true; - } - } - return false; -} - -static uint32_t -next_tnlid(uint32_t tnlid, uint32_t min, uint32_t max) -{ - return tnlid + 1 <= max ? tnlid + 1 : min; -} - -static uint32_t -allocate_tnlid(struct hmap *set, const char *name, uint32_t min, uint32_t max, - uint32_t *hint) -{ - for (uint32_t tnlid = next_tnlid(*hint, min, max); tnlid != *hint; - tnlid = next_tnlid(tnlid, min, max)) { - if (!tnlid_in_use(set, tnlid)) { - add_tnlid(set, tnlid); - *hint = tnlid; - return tnlid; - } - } - - static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1); - VLOG_WARN_RL(&rl, "all %s tunnel ids exhausted", name); - return 0; -} - struct ovn_chassis_qdisc_queues { struct hmap_node key_node; uint32_t queue_id; @@ -488,9 +429,9 @@ struct mcast_info { static uint32_t ovn_mcast_group_allocate_key(struct mcast_info *mcast_info) { - return allocate_tnlid(&mcast_info->group_tnlids, "multicast group", - OVN_MIN_IP_MULTICAST, OVN_MAX_IP_MULTICAST, - &mcast_info->group_tnlid_hint); + return ovn_allocate_tnlid(&mcast_info->group_tnlids, "multicast group", + OVN_MIN_IP_MULTICAST, OVN_MAX_IP_MULTICAST, + &mcast_info->group_tnlid_hint); } /* The 'key' comes from nbs->header_.uuid or nbr->header_.uuid or @@ -593,7 +534,7 @@ ovn_datapath_destroy(struct hmap *datapaths, struct ovn_datapath *od) * private list and once we've exited that function it is not safe to * use it. */ hmap_remove(datapaths, &od->key_node); - destroy_tnlids(&od->port_tnlids); + ovn_destroy_tnlids(&od->port_tnlids); bitmap_free(od->ipam_info.allocated_ipv4s); free(od->router_ports); ovn_ls_port_group_destroy(&od->nb_pgs); @@ -829,7 +770,7 @@ destroy_mcast_info_for_datapath(struct ovn_datapath *od) destroy_mcast_info_for_switch_datapath(od); } - destroy_tnlids(&od->mcast_info.group_tnlids); + ovn_destroy_tnlids(&od->mcast_info.group_tnlids); } static void @@ -980,7 +921,8 @@ static uint32_t ovn_datapath_allocate_key(struct hmap *dp_tnlids) { static uint32_t hint; - return allocate_tnlid(dp_tnlids, "datapath", 1, (1u << 24) - 1, &hint); + return ovn_allocate_tnlid(dp_tnlids, "datapath", OVN_MIN_DP_KEY_LOCAL, + OVN_MAX_DP_KEY_LOCAL, &hint); } /* Updates the southbound Datapath_Binding table so that it contains the @@ -1001,7 +943,7 @@ build_datapaths(struct northd_context *ctx, struct hmap *datapaths, struct hmap dp_tnlids = HMAP_INITIALIZER(&dp_tnlids); struct ovn_datapath *od; LIST_FOR_EACH (od, list, &both) { - add_tnlid(&dp_tnlids, od->sb->tunnel_key); + ovn_add_tnlid(&dp_tnlids, od->sb->tunnel_key); } /* Add southbound record for each unmatched northbound record. */ @@ -1015,7 +957,7 @@ build_datapaths(struct northd_context *ctx, struct hmap *datapaths, ovn_datapath_update_external_ids(od); sbrec_datapath_binding_set_tunnel_key(od->sb, tunnel_key); } - destroy_tnlids(&dp_tnlids); + ovn_destroy_tnlids(&dp_tnlids); } /* Delete southbound records without northbound matches. */ @@ -1127,8 +1069,8 @@ ovn_port_find(const struct hmap *ports, const char *name) static uint32_t ovn_port_allocate_key(struct ovn_datapath *od) { - return allocate_tnlid(&od->port_tnlids, "port", - 1, (1u << 15) - 1, &od->port_key_hint); + return ovn_allocate_tnlid(&od->port_tnlids, "port", + 1, (1u << 15) - 1, &od->port_key_hint); } static char * @@ -2881,7 +2823,7 @@ build_ports(struct northd_context *ctx, ovn_port_update_sbrec(ctx, sbrec_chassis_by_name, op, &chassis_qdisc_queues, &active_ha_chassis_grps); - add_tnlid(&op->od->port_tnlids, op->sb->tunnel_key); + 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; } @@ -3109,7 +3051,8 @@ ovn_igmp_group_add(struct northd_context *ctx, struct hmap *igmp_groups, igmp_group->address = *address; if (mcgroup) { igmp_group->mcgroup.key = mcgroup->tunnel_key; - add_tnlid(&datapath->mcast_info.group_tnlids, mcgroup->tunnel_key); + ovn_add_tnlid(&datapath->mcast_info.group_tnlids, + mcgroup->tunnel_key); } else { igmp_group->mcgroup.key = 0; }