From patchwork Wed Sep 1 04:25:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: wenxu X-Patchwork-Id: 1522847 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=2605:bc80:3010::136; helo=smtp3.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from smtp3.osuosl.org (smtp3.osuosl.org [IPv6:2605:bc80:3010::136]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GzrZc1wfrz9sRN for ; Wed, 1 Sep 2021 14:25:26 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id D1ABC606E9; Wed, 1 Sep 2021 04:25:23 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp3.osuosl.org ([127.0.0.1]) by localhost (smtp3.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id GQmA2YeT-dgp; Wed, 1 Sep 2021 04:25:20 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp3.osuosl.org (Postfix) with ESMTPS id 308E060617; Wed, 1 Sep 2021 04:25:19 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 0D44FC0010; Wed, 1 Sep 2021 04:25:19 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp4.osuosl.org (smtp4.osuosl.org [140.211.166.137]) by lists.linuxfoundation.org (Postfix) with ESMTP id 5765EC000E for ; Wed, 1 Sep 2021 04:25:17 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id 336D6402D8 for ; Wed, 1 Sep 2021 04:25:17 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp4.osuosl.org ([127.0.0.1]) by localhost (smtp4.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 2pMJEcltV1hx for ; Wed, 1 Sep 2021 04:25:16 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0 Received: from mail-m2456.qiye.163.com (mail-m2456.qiye.163.com [220.194.24.56]) by smtp4.osuosl.org (Postfix) with ESMTPS id 3FA43402C5 for ; Wed, 1 Sep 2021 04:25:15 +0000 (UTC) Received: from localhost.localdomain (unknown [117.50.0.204]) by mail-m2456.qiye.163.com (Hmail) with ESMTPA id 5C956700192; Wed, 1 Sep 2021 12:25:11 +0800 (CST) From: wenxu@ucloud.cn To: i.maximets@ovn.org, dlu998@gmail.com, pvalerio@redhat.com, aconole@redhat.com Date: Wed, 1 Sep 2021 12:25:09 +0800 Message-Id: <1630470310-16283-1-git-send-email-wenxu@ucloud.cn> X-Mailer: git-send-email 1.8.3.1 X-HM-Spam-Status: e1kfGhgUHx5ZQUtXWQgPGg8OCBgUHx5ZQUlOS1dZCBgUCR5ZQVlLVUtZV1 kWDxoPAgseWUFZKDYvK1lXWShZQUlCN1dZLVlBSVdZDwkaFQgSH1lBWUJISkJWTRofQ05OH09JQh 1IVRkRExYaEhckFA4PWVdZFhoPEhUdFFlBWU9LSFVKSktISkxVS1kG X-HM-Sender-Digest: e1kMHhlZQR0aFwgeV1kSHx4VD1lBWUc6MEk6Cjo*HzNJLx4TED9ROEI0 DD0wFB1VSlVKTUhLT0xLSEpKTkhNVTMWGhIXVQweFQMOOw4YFxQOH1UYFUVZV1kSC1lBWUpKTFVO S1VLVUlLT1lXWQgBWUFJTk9JNwY+ X-HM-Tid: 0a7b9f9a8de58c15kuqt5c956700192 Cc: dev@openvswitch.org Subject: [ovs-dev] [PATCH 1/2] conntrack: restore the origin port for each round with new address 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: , MIME-Version: 1.0 Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" From: wenxu It is better to choose the origin select port as current port for each port search round with new address. Signed-off-by: wenxu --- lib/conntrack.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/conntrack.c b/lib/conntrack.c index 551c206..2d14205 100644 --- a/lib/conntrack.c +++ b/lib/conntrack.c @@ -2412,8 +2412,8 @@ nat_get_unique_tuple(struct conntrack *ct, const struct conn *conn, uint32_t hash = nat_range_hash(conn, ct->hash_basis); bool pat_proto = conn->key.nw_proto == IPPROTO_TCP || conn->key.nw_proto == IPPROTO_UDP; - uint16_t min_dport, max_dport, curr_dport; - uint16_t min_sport, max_sport, curr_sport; + uint16_t min_dport, max_dport, curr_dport, orig_dport; + uint16_t min_sport, max_sport, curr_sport, orig_sport; min_addr = conn->nat_info->min_addr; max_addr = conn->nat_info->max_addr; @@ -2425,9 +2425,9 @@ nat_get_unique_tuple(struct conntrack *ct, const struct conn *conn, * we can stop once we reach it. */ guard_addr = curr_addr; - set_sport_range(conn->nat_info, &conn->key, hash, &curr_sport, + set_sport_range(conn->nat_info, &conn->key, hash, &orig_sport, &min_sport, &max_sport); - set_dport_range(conn->nat_info, &conn->key, hash, &curr_dport, + set_dport_range(conn->nat_info, &conn->key, hash, &orig_dport, &min_dport, &max_dport); another_round: @@ -2443,6 +2443,9 @@ another_round: goto next_addr; } + curr_sport = orig_sport; + curr_dport = orig_dport; + FOR_EACH_PORT_IN_RANGE(curr_dport, min_dport, max_dport) { nat_conn->rev_key.src.port = htons(curr_dport); FOR_EACH_PORT_IN_RANGE(curr_sport, min_sport, max_sport) { From patchwork Wed Sep 1 04:25:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: wenxu X-Patchwork-Id: 1522848 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=2605:bc80:3010::133; helo=smtp2.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from smtp2.osuosl.org (smtp2.osuosl.org [IPv6:2605:bc80:3010::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GzrZf1rPqz9sRN for ; Wed, 1 Sep 2021 14:25:29 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 3778340180; Wed, 1 Sep 2021 04:25:26 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id l1X0g6MeBrHS; Wed, 1 Sep 2021 04:25:22 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp2.osuosl.org (Postfix) with ESMTPS id 554EA40117; Wed, 1 Sep 2021 04:25:21 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id A7A9DC0021; Wed, 1 Sep 2021 04:25:19 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp2.osuosl.org (smtp2.osuosl.org [IPv6:2605:bc80:3010::133]) by lists.linuxfoundation.org (Postfix) with ESMTP id 7C28BC000E for ; Wed, 1 Sep 2021 04:25:17 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 5D489400C3 for ; Wed, 1 Sep 2021 04:25:17 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 9ZHqjKTgEA95 for ; Wed, 1 Sep 2021 04:25:16 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0 Received: from mail-m2456.qiye.163.com (mail-m2456.qiye.163.com [220.194.24.56]) by smtp2.osuosl.org (Postfix) with ESMTPS id 4222640003 for ; Wed, 1 Sep 2021 04:25:15 +0000 (UTC) Received: from localhost.localdomain (unknown [117.50.0.204]) by mail-m2456.qiye.163.com (Hmail) with ESMTPA id 86E837001B5; Wed, 1 Sep 2021 12:25:11 +0800 (CST) From: wenxu@ucloud.cn To: i.maximets@ovn.org, dlu998@gmail.com, pvalerio@redhat.com, aconole@redhat.com Date: Wed, 1 Sep 2021 12:25:10 +0800 Message-Id: <1630470310-16283-2-git-send-email-wenxu@ucloud.cn> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1630470310-16283-1-git-send-email-wenxu@ucloud.cn> References: <1630470310-16283-1-git-send-email-wenxu@ucloud.cn> X-HM-Spam-Status: e1kfGhgUHx5ZQUtXWQgPGg8OCBgUHx5ZQUlOS1dZCBgUCR5ZQVlLVUtZV1 kWDxoPAgseWUFZKDYvK1lXWShZQUlCN1dZLVlBSVdZDwkaFQgSH1lBWRpIQ0NWGRgdGUNIH0pMTk 8fVRkRExYaEhckFA4PWVdZFhoPEhUdFFlBWU9LSFVKSktISkxVS1kG X-HM-Sender-Digest: e1kMHhlZQR0aFwgeV1kSHx4VD1lBWUc6OQg6IRw5FTMBHR4pEDkeOEwa UQMaCwFVSlVKTUhLT0xLSEpKTEpJVTMWGhIXVQweFQMOOw4YFxQOH1UYFUVZV1kSC1lBWUpKTFVO S1VLVUlLT1lXWQgBWUFPS0xONwY+ X-HM-Tid: 0a7b9f9a8e938c15kuqt86e837001b5 Cc: dev@openvswitch.org Subject: [ovs-dev] [PATCH 2/2] conntrack: limit port clash resolution attempts 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: , MIME-Version: 1.0 Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" From: wenxu In case almost or all available ports are taken, clash resolution can take a very long time, resulting in soft lockup. This can happen when many to-be-natted hosts connect to same destination:port (e.g. a proxy) and all connections pass the same SNAT. Pick a random offset in the acceptable range, then try ever smaller number of adjacent port numbers, until either the limit is reached or a useable port was found. This results in at most 248 attempts (128 + 64 + 32 + 16 + 8, i.e. 4 restarts with new search offset) instead of 64000+. Signed-off-by: wenxu --- lib/conntrack.c | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/conntrack.c b/lib/conntrack.c index 2d14205..a500d64 100644 --- a/lib/conntrack.c +++ b/lib/conntrack.c @@ -2414,6 +2414,10 @@ nat_get_unique_tuple(struct conntrack *ct, const struct conn *conn, conn->key.nw_proto == IPPROTO_UDP; uint16_t min_dport, max_dport, curr_dport, orig_dport; uint16_t min_sport, max_sport, curr_sport, orig_sport; + static const unsigned int max_attempts = 128; + uint16_t range_src, range_dst, range_max; + unsigned int attempts; + unsigned int i; min_addr = conn->nat_info->min_addr; max_addr = conn->nat_info->max_addr; @@ -2430,6 +2434,10 @@ nat_get_unique_tuple(struct conntrack *ct, const struct conn *conn, set_dport_range(conn->nat_info, &conn->key, hash, &orig_dport, &min_dport, &max_dport); + range_src = max_sport - min_sport + 1; + range_dst = max_dport - min_dport + 1; + range_max = range_src > range_dst ? range_src : range_dst; + another_round: store_addr_to_key(&curr_addr, &nat_conn->rev_key, conn->nat_info->nat_action); @@ -2446,17 +2454,36 @@ another_round: curr_sport = orig_sport; curr_dport = orig_dport; +another_port_round: + i = 0; + attempts = range_max; + if (attempts > max_attempts) { + attempts = max_attempts; + } + FOR_EACH_PORT_IN_RANGE(curr_dport, min_dport, max_dport) { nat_conn->rev_key.src.port = htons(curr_dport); FOR_EACH_PORT_IN_RANGE(curr_sport, min_sport, max_sport) { - nat_conn->rev_key.dst.port = htons(curr_sport); - if (!conn_lookup(ct, &nat_conn->rev_key, - time_msec(), NULL, NULL)) { - return true; + if (i++ < attempts) { + nat_conn->rev_key.dst.port = htons(curr_sport); + if (!conn_lookup(ct, &nat_conn->rev_key, + time_msec(), NULL, NULL)) { + return true; + } } } } + if (attempts >= range_max || attempts < 16) { + goto next_addr; + } + + attempts /= 2; + curr_dport = random_uint32() % range_dst; + curr_sport = random_uint32() % range_src; + + goto another_port_round; + /* Check if next IP is in range and respin. Otherwise, notify * exhaustion to the caller. */ next_addr: