From patchwork Fri Jul 1 18:13:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Valerio X-Patchwork-Id: 1651341 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=I8e5TQzz; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.138; helo=smtp1.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LZNd14Xknz9ryY for ; Sat, 2 Jul 2022 04:14:13 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id EBA2A84733; Fri, 1 Jul 2022 18:14:10 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org EBA2A84733 Authentication-Results: smtp1.osuosl.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=I8e5TQzz X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id KB7tlIqtjaH2; Fri, 1 Jul 2022 18:14:09 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp1.osuosl.org (Postfix) with ESMTPS id C64B38472E; Fri, 1 Jul 2022 18:14:08 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org C64B38472E Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 983A5C002D; Fri, 1 Jul 2022 18:14:08 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp3.osuosl.org (smtp3.osuosl.org [IPv6:2605:bc80:3010::136]) by lists.linuxfoundation.org (Postfix) with ESMTP id 312C3C002D for ; Fri, 1 Jul 2022 18:14:07 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id 9E09E60F68 for ; Fri, 1 Jul 2022 18:14:06 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org 9E09E60F68 Authentication-Results: smtp3.osuosl.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=I8e5TQzz 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 J99zMT0C1wS8 for ; Fri, 1 Jul 2022 18:14:05 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org 8BA05613E4 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by smtp3.osuosl.org (Postfix) with ESMTPS id 8BA05613E4 for ; Fri, 1 Jul 2022 18:14:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656699244; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iyjX1zla/MAKp4qeqrM9ZdmRrINc/MouYuroqpWHHmI=; b=I8e5TQzz0TFQ1bnsUZz0VggIPKzE3ClRU0lJq0rhtKmMjodGWDyCLU06J+orpm8+xXJNEd k1BoKcxrOyaViwSvv11vXQBA1eI8N8Le+DdJik/7xyJs/tZy7yuVKXyL4Lnkhs6LJEXpgZ F6GSDqezxaA69DV9nA3+UDT/H3UrWGg= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-357-_T39_bIHNBCOpGB6OukBLA-1; Fri, 01 Jul 2022 14:14:02 -0400 X-MC-Unique: _T39_bIHNBCOpGB6OukBLA-1 Received: by mail-wm1-f72.google.com with SMTP id v8-20020a05600c214800b003a1819451b1so1709512wml.7 for ; Fri, 01 Jul 2022 11:14:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=iyjX1zla/MAKp4qeqrM9ZdmRrINc/MouYuroqpWHHmI=; b=v2lF4XMm1nt3GNfzrGbu5Mp1EQ5rKpX1qRx6iER8OTsXUrqNW7DrvZsFqVML+9IWow wk+lx2Tpkj+Hq1qftO0mSUYZKJDj8tcf8zTf53JBGL8TaH3S1JkvdIWkkKKRf9zCZwWM 2t+PhFdJej0Tzw0w1+JrbO6YxnylIw2Z/KskIqu/U9pDw3C1YbO5k5pNLpIhPjL0lCFh lHOPxt/cwa4IpYuoR3wOV/Wrnri9NTzfYe0PlMfQ3ggnZqOYjkOS0zUYhIQEkENXpfWG cj9EwW6ftlO88u7fltfw1i73tzaD+aNGNB92qVvjiTxuyxfNtqFGJh4LyFtsE5GuYdvj w71A== X-Gm-Message-State: AJIora9q2SStinSzT802h1Qb09WRBKoAbbLhljwzBfAjwrNpHCcIGZle F6DLHY8dn20NMd0mcANPEHm1ziDVG5Sd9Q2D8hzf+27lSfVGaDz/4j4zQrU6106cptuaftrx99b PFVUA49qUzjWnUsSkrKxANyIjjWO5UXwWjaoUkKl1B8lyO0hkpqNT8Oee52kp/IYw X-Received: by 2002:a05:6000:991:b0:21b:b7b1:ac50 with SMTP id by17-20020a056000099100b0021bb7b1ac50mr14946425wrb.718.1656699241332; Fri, 01 Jul 2022 11:14:01 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vb7S6PpgQRHlYc7L0rvnSnp/7nFTZGzhfJ06vUY8vs8BmFlvxKm2iqsp06P+Ollh/tjk5ZfA== X-Received: by 2002:a05:6000:991:b0:21b:b7b1:ac50 with SMTP id by17-20020a056000099100b0021bb7b1ac50mr14946395wrb.718.1656699240994; Fri, 01 Jul 2022 11:14:00 -0700 (PDT) Received: from localhost (net-5-95-131-226.cust.vodafonedsl.it. [5.95.131.226]) by smtp.gmail.com with ESMTPSA id 18-20020a05600c269200b003a03a8475bfsm6852434wmt.16.2022.07.01.11.14.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Jul 2022 11:14:00 -0700 (PDT) From: Paolo Valerio To: dev@openvswitch.org Date: Fri, 01 Jul 2022 20:13:59 +0200 Message-ID: <165669923981.2104762.11257719106089126658.stgit@fed.void> In-Reply-To: <165669922758.2104762.4398770071010254560.stgit@fed.void> References: <165669922758.2104762.4398770071010254560.stgit@fed.void> User-Agent: StGit/1.1 MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=pvalerio@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Cc: fbl@sysclose.org, i.maximets@ovn.org Subject: [ovs-dev] [PATCH v6 1/5] conntrack: Use a cmap to store zone limits 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" From: Gaetan Rivet Change the data structure from hmap to cmap for zone limits. As they are shared amongst multiple conntrack users, multiple readers want to check the current zone limit state before progressing in their processing. Using a CMAP allows doing lookups without taking the global 'ct_lock', thus reducing contention. Signed-off-by: Gaetan Rivet Reviewed-by: Eli Britstein Signed-off-by: Paolo Valerio --- lib/conntrack-private.h | 2 + lib/conntrack.c | 70 ++++++++++++++++++++++++++++++++--------------- lib/conntrack.h | 2 + lib/dpif-netdev.c | 5 ++- 4 files changed, 53 insertions(+), 26 deletions(-) diff --git a/lib/conntrack-private.h b/lib/conntrack-private.h index dfdf4e676..d9461b811 100644 --- a/lib/conntrack-private.h +++ b/lib/conntrack-private.h @@ -192,7 +192,7 @@ struct conntrack { struct ovs_mutex ct_lock; /* Protects 2 following fields. */ struct cmap conns OVS_GUARDED; struct ovs_list exp_lists[N_CT_TM] OVS_GUARDED; - struct hmap zone_limits OVS_GUARDED; + struct cmap zone_limits OVS_GUARDED; struct hmap timeout_policies OVS_GUARDED; uint32_t hash_basis; /* Salt for hashing a connection key. */ pthread_t clean_thread; /* Periodically cleans up connection tracker. */ diff --git a/lib/conntrack.c b/lib/conntrack.c index faa2d6ab7..6df1142b9 100644 --- a/lib/conntrack.c +++ b/lib/conntrack.c @@ -81,7 +81,7 @@ enum ct_alg_ctl_type { }; struct zone_limit { - struct hmap_node node; + struct cmap_node node; struct conntrack_zone_limit czl; }; @@ -311,7 +311,7 @@ conntrack_init(void) for (unsigned i = 0; i < ARRAY_SIZE(ct->exp_lists); i++) { ovs_list_init(&ct->exp_lists[i]); } - hmap_init(&ct->zone_limits); + cmap_init(&ct->zone_limits); ct->zone_limit_seq = 0; timeout_policy_init(ct); ovs_mutex_unlock(&ct->ct_lock); @@ -346,12 +346,25 @@ zone_key_hash(int32_t zone, uint32_t basis) } static struct zone_limit * -zone_limit_lookup(struct conntrack *ct, int32_t zone) +zone_limit_lookup_protected(struct conntrack *ct, int32_t zone) OVS_REQUIRES(ct->ct_lock) { uint32_t hash = zone_key_hash(zone, ct->hash_basis); struct zone_limit *zl; - HMAP_FOR_EACH_IN_BUCKET (zl, node, hash, &ct->zone_limits) { + CMAP_FOR_EACH_WITH_HASH_PROTECTED (zl, node, hash, &ct->zone_limits) { + if (zl->czl.zone == zone) { + return zl; + } + } + return NULL; +} + +static struct zone_limit * +zone_limit_lookup(struct conntrack *ct, int32_t zone) +{ + uint32_t hash = zone_key_hash(zone, ct->hash_basis); + struct zone_limit *zl; + CMAP_FOR_EACH_WITH_HASH (zl, node, hash, &ct->zone_limits) { if (zl->czl.zone == zone) { return zl; } @@ -361,7 +374,6 @@ zone_limit_lookup(struct conntrack *ct, int32_t zone) static struct zone_limit * zone_limit_lookup_or_default(struct conntrack *ct, int32_t zone) - OVS_REQUIRES(ct->ct_lock) { struct zone_limit *zl = zone_limit_lookup(ct, zone); return zl ? zl : zone_limit_lookup(ct, DEFAULT_ZONE); @@ -370,13 +382,16 @@ zone_limit_lookup_or_default(struct conntrack *ct, int32_t zone) struct conntrack_zone_limit zone_limit_get(struct conntrack *ct, int32_t zone) { - ovs_mutex_lock(&ct->ct_lock); - struct conntrack_zone_limit czl = {DEFAULT_ZONE, 0, 0, 0}; + struct conntrack_zone_limit czl = { + .zone = DEFAULT_ZONE, + .limit = 0, + .count = ATOMIC_COUNT_INIT(0), + .zone_limit_seq = 0, + }; struct zone_limit *zl = zone_limit_lookup_or_default(ct, zone); if (zl) { czl = zl->czl; } - ovs_mutex_unlock(&ct->ct_lock); return czl; } @@ -384,13 +399,19 @@ static int zone_limit_create(struct conntrack *ct, int32_t zone, uint32_t limit) OVS_REQUIRES(ct->ct_lock) { + struct zone_limit *zl = zone_limit_lookup_protected(ct, zone); + + if (zl) { + return 0; + } + if (zone >= DEFAULT_ZONE && zone <= MAX_ZONE) { - struct zone_limit *zl = xzalloc(sizeof *zl); + zl = xzalloc(sizeof *zl); zl->czl.limit = limit; zl->czl.zone = zone; zl->czl.zone_limit_seq = ct->zone_limit_seq++; uint32_t hash = zone_key_hash(zone, ct->hash_basis); - hmap_insert(&ct->zone_limits, &zl->node, hash); + cmap_insert(&ct->zone_limits, &zl->node, hash); return 0; } else { return EINVAL; @@ -401,13 +422,14 @@ int zone_limit_update(struct conntrack *ct, int32_t zone, uint32_t limit) { int err = 0; - ovs_mutex_lock(&ct->ct_lock); struct zone_limit *zl = zone_limit_lookup(ct, zone); if (zl) { zl->czl.limit = limit; VLOG_INFO("Changed zone limit of %u for zone %d", limit, zone); } else { + ovs_mutex_lock(&ct->ct_lock); err = zone_limit_create(ct, zone, limit); + ovs_mutex_unlock(&ct->ct_lock); if (!err) { VLOG_INFO("Created zone limit of %u for zone %d", limit, zone); } else { @@ -415,7 +437,6 @@ zone_limit_update(struct conntrack *ct, int32_t zone, uint32_t limit) zone); } } - ovs_mutex_unlock(&ct->ct_lock); return err; } @@ -423,23 +444,25 @@ static void zone_limit_clean(struct conntrack *ct, struct zone_limit *zl) OVS_REQUIRES(ct->ct_lock) { - hmap_remove(&ct->zone_limits, &zl->node); - free(zl); + uint32_t hash = zone_key_hash(zl->czl.zone, ct->hash_basis); + cmap_remove(&ct->zone_limits, &zl->node, hash); + ovsrcu_postpone(free, zl); } int zone_limit_delete(struct conntrack *ct, uint16_t zone) { ovs_mutex_lock(&ct->ct_lock); - struct zone_limit *zl = zone_limit_lookup(ct, zone); + struct zone_limit *zl = zone_limit_lookup_protected(ct, zone); if (zl) { zone_limit_clean(ct, zl); + ovs_mutex_unlock(&ct->ct_lock); VLOG_INFO("Deleted zone limit for zone %d", zone); } else { + ovs_mutex_unlock(&ct->ct_lock); VLOG_INFO("Attempted delete of non-existent zone limit: zone %d", zone); } - ovs_mutex_unlock(&ct->ct_lock); return 0; } @@ -456,7 +479,7 @@ conn_clean_cmn(struct conntrack *ct, struct conn *conn) struct zone_limit *zl = zone_limit_lookup(ct, conn->admit_zone); if (zl && zl->czl.zone_limit_seq == conn->zone_limit_seq) { - zl->czl.count--; + atomic_count_dec(&zl->czl.count); } } @@ -510,10 +533,13 @@ conntrack_destroy(struct conntrack *ct) cmap_destroy(&ct->conns); struct zone_limit *zl; - HMAP_FOR_EACH_POP (zl, node, &ct->zone_limits) { - free(zl); + CMAP_FOR_EACH (zl, node, &ct->zone_limits) { + uint32_t hash = zone_key_hash(zl->czl.zone, ct->hash_basis); + + cmap_remove(&ct->zone_limits, &zl->node, hash); + ovsrcu_postpone(free, zl); } - hmap_destroy(&ct->zone_limits); + cmap_destroy(&ct->zone_limits); struct timeout_policy *tp; HMAP_FOR_EACH_POP (tp, node, &ct->timeout_policies) { @@ -991,7 +1017,7 @@ conn_not_found(struct conntrack *ct, struct dp_packet *pkt, if (commit) { struct zone_limit *zl = zone_limit_lookup_or_default(ct, ctx->key.zone); - if (zl && zl->czl.count >= zl->czl.limit) { + if (zl && atomic_count_get(&zl->czl.count) >= zl->czl.limit) { return nc; } @@ -1064,7 +1090,7 @@ conn_not_found(struct conntrack *ct, struct dp_packet *pkt, if (zl) { nc->admit_zone = zl->czl.zone; nc->zone_limit_seq = zl->czl.zone_limit_seq; - zl->czl.count++; + atomic_count_inc(&zl->czl.count); } else { nc->admit_zone = INVALID_ZONE; } diff --git a/lib/conntrack.h b/lib/conntrack.h index 9553b188a..58b181834 100644 --- a/lib/conntrack.h +++ b/lib/conntrack.h @@ -108,7 +108,7 @@ struct conntrack_dump { struct conntrack_zone_limit { int32_t zone; uint32_t limit; - uint32_t count; + atomic_count count; uint32_t zone_limit_seq; /* Used to disambiguate zone limit counts. */ }; diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c index d09138f2c..760cde1a4 100644 --- a/lib/dpif-netdev.c +++ b/lib/dpif-netdev.c @@ -9263,7 +9263,8 @@ dpif_netdev_ct_get_limits(struct dpif *dpif, czl = zone_limit_get(dp->conntrack, zone_limit->zone); if (czl.zone == zone_limit->zone || czl.zone == DEFAULT_ZONE) { ct_dpif_push_zone_limit(zone_limits_reply, zone_limit->zone, - czl.limit, czl.count); + czl.limit, + atomic_count_get(&czl.count)); } else { return EINVAL; } @@ -9273,7 +9274,7 @@ dpif_netdev_ct_get_limits(struct dpif *dpif, czl = zone_limit_get(dp->conntrack, z); if (czl.zone == z) { ct_dpif_push_zone_limit(zone_limits_reply, z, czl.limit, - czl.count); + atomic_count_get(&czl.count)); } } } From patchwork Fri Jul 1 18:14:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Valerio X-Patchwork-Id: 1651342 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Hmivv7aE; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.138; helo=smtp1.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LZNdB5qdPz9ryY for ; Sat, 2 Jul 2022 04:14:22 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id EEC0C8470B; Fri, 1 Jul 2022 18:14:20 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org EEC0C8470B Authentication-Results: smtp1.osuosl.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Hmivv7aE X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id TphFl4lpGDH1; Fri, 1 Jul 2022 18:14:19 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp1.osuosl.org (Postfix) with ESMTPS id A193B8474A; Fri, 1 Jul 2022 18:14:18 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org A193B8474A Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 6D3F3C0032; Fri, 1 Jul 2022 18:14:18 +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 27090C0036 for ; Fri, 1 Jul 2022 18:14:17 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id 8C35D424D1 for ; Fri, 1 Jul 2022 18:14:13 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp4.osuosl.org 8C35D424D1 Authentication-Results: smtp4.osuosl.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Hmivv7aE 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 hW6BQF5LOhu8 for ; Fri, 1 Jul 2022 18:14:11 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp4.osuosl.org 62E77424C7 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by smtp4.osuosl.org (Postfix) with ESMTPS id 62E77424C7 for ; Fri, 1 Jul 2022 18:14:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656699250; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DQ8AJ5QUxo9XnIkLf81k7Qz4hZ0b0lm4rQh44tOhYXk=; b=Hmivv7aEQJh+bs/P3XZZAn15pV6qhAnsfFKwV/vb7sM9AwPCi6UC70hKTlwzyE8vkjY1HB 83ZF8RgcAEAdSAMlfzMgmC13zsYOK8K6yPahpbDCPGvOcuw2363zK5OFv3isL32WwrOMr/ bIwcH6aa4EjsMFO7t7djTy9P33WgEXM= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-213-ziQ7MSYdNt63UV8-QhONMw-1; Fri, 01 Jul 2022 14:14:09 -0400 X-MC-Unique: ziQ7MSYdNt63UV8-QhONMw-1 Received: by mail-wm1-f69.google.com with SMTP id 130-20020a1c0288000000b003a18127d11aso312562wmc.6 for ; Fri, 01 Jul 2022 11:14:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=DQ8AJ5QUxo9XnIkLf81k7Qz4hZ0b0lm4rQh44tOhYXk=; b=KCueHNOXRmgIs3zHfjqP07RWhjjn4pkA9dPQZdiyZiei12e6KW8gFsdLE8IAXgAWbE YtauriOS5gHjqf71MIXoeRQZhPQkkOsfgyvXHgDsfvucEuW8uK7yPw9nCIzVW3ZTuP3G jDK0AtVzPDbqIKYTkQOrVaD61WGRwnh3fhlXscKdMphNN2wHCkJeC2X/VfmPcZpUFApE xuV/lqgKCj7rUn90thpGa1f8jo4qUkAD96cM+2vphsc+Sz147gWwOvARoLbhW2TeBWbI b1iEf1pCGiHNiDrfvHH96111xy0qmvXJnqbD5woKCKsiW/7rBQzAD3aI+nQJKLsv1kYD GOaw== X-Gm-Message-State: AJIora+MIpvL5ytgCPJm3NPnkfeN4DbwNLk9zLvvq7KCTYlXx2MXuDlt YjhAfyGZZGoHB12diUNxr70hh4FCx6ySCuGochvzvyiIBCHfhIrpGViY4f8X/WwVWZ2uP/OeQoi 04LfIkTD5nSLpZbu2Rd1JoHJITSnhy482MG2VmcEZwCSDZIz+8po8KAEbNHn+NhaP X-Received: by 2002:a5d:62c4:0:b0:21b:c031:a94b with SMTP id o4-20020a5d62c4000000b0021bc031a94bmr14949925wrv.624.1656699247617; Fri, 01 Jul 2022 11:14:07 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uwuqlfUzFUn1SucCtpKsqSQfs8lHlV7k8Py936P8KOhBZ8dQPpJ1zATJPBQZyk4/Mgxdycqw== X-Received: by 2002:a5d:62c4:0:b0:21b:c031:a94b with SMTP id o4-20020a5d62c4000000b0021bc031a94bmr14949901wrv.624.1656699247258; Fri, 01 Jul 2022 11:14:07 -0700 (PDT) Received: from localhost (net-5-95-131-226.cust.vodafonedsl.it. [5.95.131.226]) by smtp.gmail.com with ESMTPSA id n7-20020a5d67c7000000b0021b9e8d4c22sm23048340wrw.61.2022.07.01.11.14.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Jul 2022 11:14:06 -0700 (PDT) From: Paolo Valerio To: dev@openvswitch.org Date: Fri, 01 Jul 2022 20:14:06 +0200 Message-ID: <165669924610.2104762.3230988027335443233.stgit@fed.void> In-Reply-To: <165669922758.2104762.4398770071010254560.stgit@fed.void> References: <165669922758.2104762.4398770071010254560.stgit@fed.void> User-Agent: StGit/1.1 MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=pvalerio@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Cc: fbl@sysclose.org, i.maximets@ovn.org Subject: [ovs-dev] [PATCH v6 2/5] conntrack-tp: Use a cmap to store timeout policies 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" From: Gaetan Rivet Multiple lookups are done to stored timeout policies, each time blocking the global 'ct_lock'. This is usually not necessary and it should be acceptable to get policy updates slightly delayed (by one RCU sync at most). Using a CMAP reduces multiple lock taking and releasing in the connection insertion path. Signed-off-by: Gaetan Rivet Reviewed-by: Eli Britstein Acked-by: William Tu Signed-off-by: Paolo Valerio --- lib/conntrack-private.h | 2 +- lib/conntrack-tp.c | 54 ++++++++++++++++++++++++++--------------------- lib/conntrack.c | 9 +++++--- lib/conntrack.h | 2 +- 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/lib/conntrack-private.h b/lib/conntrack-private.h index d9461b811..34c688821 100644 --- a/lib/conntrack-private.h +++ b/lib/conntrack-private.h @@ -193,7 +193,7 @@ struct conntrack { struct cmap conns OVS_GUARDED; struct ovs_list exp_lists[N_CT_TM] OVS_GUARDED; struct cmap zone_limits OVS_GUARDED; - struct hmap timeout_policies OVS_GUARDED; + struct cmap timeout_policies OVS_GUARDED; uint32_t hash_basis; /* Salt for hashing a connection key. */ pthread_t clean_thread; /* Periodically cleans up connection tracker. */ struct latch clean_thread_exit; /* To destroy the 'clean_thread'. */ diff --git a/lib/conntrack-tp.c b/lib/conntrack-tp.c index a586d3a8d..c2245038b 100644 --- a/lib/conntrack-tp.c +++ b/lib/conntrack-tp.c @@ -47,14 +47,15 @@ static unsigned int ct_dpif_netdev_tp_def[] = { }; static struct timeout_policy * -timeout_policy_lookup(struct conntrack *ct, int32_t tp_id) +timeout_policy_lookup_protected(struct conntrack *ct, int32_t tp_id) OVS_REQUIRES(ct->ct_lock) { struct timeout_policy *tp; uint32_t hash; hash = hash_int(tp_id, ct->hash_basis); - HMAP_FOR_EACH_IN_BUCKET (tp, node, hash, &ct->timeout_policies) { + CMAP_FOR_EACH_WITH_HASH_PROTECTED (tp, node, hash, + &ct->timeout_policies) { if (tp->policy.id == tp_id) { return tp; } @@ -62,20 +63,25 @@ timeout_policy_lookup(struct conntrack *ct, int32_t tp_id) return NULL; } -struct timeout_policy * -timeout_policy_get(struct conntrack *ct, int32_t tp_id) +static struct timeout_policy * +timeout_policy_lookup(struct conntrack *ct, int32_t tp_id) { struct timeout_policy *tp; + uint32_t hash; - ovs_mutex_lock(&ct->ct_lock); - tp = timeout_policy_lookup(ct, tp_id); - if (!tp) { - ovs_mutex_unlock(&ct->ct_lock); - return NULL; + hash = hash_int(tp_id, ct->hash_basis); + CMAP_FOR_EACH_WITH_HASH (tp, node, hash, &ct->timeout_policies) { + if (tp->policy.id == tp_id) { + return tp; + } } + return NULL; +} - ovs_mutex_unlock(&ct->ct_lock); - return tp; +struct timeout_policy * +timeout_policy_get(struct conntrack *ct, int32_t tp_id) +{ + return timeout_policy_lookup(ct, tp_id); } static void @@ -125,27 +131,30 @@ timeout_policy_create(struct conntrack *ct, init_default_tp(tp, tp_id); update_existing_tp(tp, new_tp); hash = hash_int(tp_id, ct->hash_basis); - hmap_insert(&ct->timeout_policies, &tp->node, hash); + cmap_insert(&ct->timeout_policies, &tp->node, hash); } static void timeout_policy_clean(struct conntrack *ct, struct timeout_policy *tp) OVS_REQUIRES(ct->ct_lock) { - hmap_remove(&ct->timeout_policies, &tp->node); - free(tp); + uint32_t hash = hash_int(tp->policy.id, ct->hash_basis); + cmap_remove(&ct->timeout_policies, &tp->node, hash); + ovsrcu_postpone(free, tp); } static int -timeout_policy_delete__(struct conntrack *ct, uint32_t tp_id) +timeout_policy_delete__(struct conntrack *ct, uint32_t tp_id, + bool warn_on_error) OVS_REQUIRES(ct->ct_lock) { + struct timeout_policy *tp; int err = 0; - struct timeout_policy *tp = timeout_policy_lookup(ct, tp_id); + tp = timeout_policy_lookup_protected(ct, tp_id); if (tp) { timeout_policy_clean(ct, tp); - } else { + } else if (warn_on_error) { VLOG_WARN_RL(&rl, "Failed to delete a non-existent timeout " "policy: id=%d", tp_id); err = ENOENT; @@ -159,7 +168,7 @@ timeout_policy_delete(struct conntrack *ct, uint32_t tp_id) int err; ovs_mutex_lock(&ct->ct_lock); - err = timeout_policy_delete__(ct, tp_id); + err = timeout_policy_delete__(ct, tp_id, true); ovs_mutex_unlock(&ct->ct_lock); return err; } @@ -170,7 +179,7 @@ timeout_policy_init(struct conntrack *ct) { struct timeout_policy tp; - hmap_init(&ct->timeout_policies); + cmap_init(&ct->timeout_policies); /* Create default timeout policy. */ memset(&tp, 0, sizeof tp); @@ -182,14 +191,11 @@ int timeout_policy_update(struct conntrack *ct, struct timeout_policy *new_tp) { - int err = 0; uint32_t tp_id = new_tp->policy.id; + int err = 0; ovs_mutex_lock(&ct->ct_lock); - struct timeout_policy *tp = timeout_policy_lookup(ct, tp_id); - if (tp) { - err = timeout_policy_delete__(ct, tp_id); - } + timeout_policy_delete__(ct, tp_id, false); timeout_policy_create(ct, new_tp); ovs_mutex_unlock(&ct->ct_lock); return err; diff --git a/lib/conntrack.c b/lib/conntrack.c index 6df1142b9..38ddc9b91 100644 --- a/lib/conntrack.c +++ b/lib/conntrack.c @@ -542,10 +542,13 @@ conntrack_destroy(struct conntrack *ct) cmap_destroy(&ct->zone_limits); struct timeout_policy *tp; - HMAP_FOR_EACH_POP (tp, node, &ct->timeout_policies) { - free(tp); + CMAP_FOR_EACH (tp, node, &ct->timeout_policies) { + uint32_t hash = hash_int(tp->policy.id, ct->hash_basis); + + cmap_remove(&ct->timeout_policies, &tp->node, hash); + ovsrcu_postpone(free, tp); } - hmap_destroy(&ct->timeout_policies); + cmap_destroy(&ct->timeout_policies); ovs_mutex_unlock(&ct->ct_lock); ovs_mutex_destroy(&ct->ct_lock); diff --git a/lib/conntrack.h b/lib/conntrack.h index 58b181834..b064abc9f 100644 --- a/lib/conntrack.h +++ b/lib/conntrack.h @@ -113,7 +113,7 @@ struct conntrack_zone_limit { }; struct timeout_policy { - struct hmap_node node; + struct cmap_node node; struct ct_dpif_timeout_policy policy; }; From patchwork Fri Jul 1 18:14:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Valerio X-Patchwork-Id: 1651343 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=LP/uxJJs; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::137; helo=smtp4.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from smtp4.osuosl.org (smtp4.osuosl.org [IPv6:2605:bc80:3010::137]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LZNdR1WbGz9ryY for ; Sat, 2 Jul 2022 04:14:34 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id BA9B2424E7; Fri, 1 Jul 2022 18:14:32 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp4.osuosl.org BA9B2424E7 Authentication-Results: smtp4.osuosl.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=LP/uxJJs 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 KCAEl4dRtVYj; Fri, 1 Jul 2022 18:14:30 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp4.osuosl.org (Postfix) with ESMTPS id 0636E424E1; Fri, 1 Jul 2022 18:14:29 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp4.osuosl.org 0636E424E1 Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id B3217C0032; Fri, 1 Jul 2022 18:14:28 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp3.osuosl.org (smtp3.osuosl.org [IPv6:2605:bc80:3010::136]) by lists.linuxfoundation.org (Postfix) with ESMTP id 0ABC0C002D for ; Fri, 1 Jul 2022 18:14:28 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id AB2176143D for ; Fri, 1 Jul 2022 18:14:19 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org AB2176143D Authentication-Results: smtp3.osuosl.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=LP/uxJJs 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 V5gdkeAHLhsv for ; Fri, 1 Jul 2022 18:14:18 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org 1E1D8613F3 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by smtp3.osuosl.org (Postfix) with ESMTPS id 1E1D8613F3 for ; Fri, 1 Jul 2022 18:14:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656699257; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CyAW96n6wJ+evz87YteDE3G4CeguwPrsnQHsEJYl7CU=; b=LP/uxJJsoOJWdu9ScrwHSpHRUA9viqx+4mppIsWhZ9JuyzcIU64toFEtYt+z1zQ7LDDbD/ PGHnEsxCPBCLIQMly8dkBrnKhqPL5wX0KE0sYpeZUL+IVNjJR7PHImZzrKfmKu7ZIt4UFT C9Ehi1cZxiVKLhcNCOoqnA+tMPD5pR8= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-5-OO5P80glPOS19Qm_4OdoFw-1; Fri, 01 Jul 2022 14:14:16 -0400 X-MC-Unique: OO5P80glPOS19Qm_4OdoFw-1 Received: by mail-wm1-f70.google.com with SMTP id j35-20020a05600c1c2300b003a167dfa0ecso1711560wms.5 for ; Fri, 01 Jul 2022 11:14:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=NUcZS/pcdza/+JDWvQf0uZ8KEOywRgUH6614VUbw2Z8=; b=th6NtWLNaL2DC/468JymCfiWtFz5ZpZyzGDMwU8CzUR5Rl1Kk/O0pqoY25jcyNoSya HHp7MrO9+hVjEmR4udbYcjmeeUWNdXJA2I/tdmPOz7nCL3EWVjXUXoyjhB9nPVIF/jWC xDBHXrFLRQcHzD0ubmw4vzJ2fAF2U35IK83TUp0zO6GhOWbtEJL+Eqaad/hhbMsP3kdL 3u0/VZnt3Lef4dNBF0prTft3Arbnc3B/GhQwB8kYrE3N9RUM6YgR+uJZTPIAgfbCTA+/ mvVES3hocs9g59N3JLQtRVZjbTxoQ8mcng0DH78FmyWMVpKWZKc/Dd5QCaWkM7xZ2B7Y ec1A== X-Gm-Message-State: AJIora9nfwvEdqO28YKCY85/NiTazqQKKcLvw043ouu+MeVACUCS5hWT U/CptUV4lbgPGHUbG3rr2qB6+1oEI8S0ZJjVY+q9Xs8BsZ9y+Dz4LCSSDejyWiJPwdjbQcpcicb vCPELVMIWHCTzKBPsjbsYr4TJUVTak1TZ71oV7PiWAup1dj0CLlaVrZjyhsSqGC9r X-Received: by 2002:a05:600c:500a:b0:3a1:8c53:9bd5 with SMTP id n10-20020a05600c500a00b003a18c539bd5mr5695044wmr.82.1656699254197; Fri, 01 Jul 2022 11:14:14 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vBLSzQnrfqggrGFyGGbafrlJQMKOobAWmk5q0r1+h8K2P8673tcR0NUs/ysBTUoA7YdQhsgQ== X-Received: by 2002:a05:600c:500a:b0:3a1:8c53:9bd5 with SMTP id n10-20020a05600c500a00b003a18c539bd5mr5695002wmr.82.1656699253642; Fri, 01 Jul 2022 11:14:13 -0700 (PDT) Received: from localhost (net-5-95-131-226.cust.vodafonedsl.it. [5.95.131.226]) by smtp.gmail.com with ESMTPSA id g34-20020a05600c4ca200b0039c7dbafa7asm6402473wmp.19.2022.07.01.11.14.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Jul 2022 11:14:13 -0700 (PDT) From: Paolo Valerio To: dev@openvswitch.org Date: Fri, 01 Jul 2022 20:14:12 +0200 Message-ID: <165669925241.2104762.12417592328400309216.stgit@fed.void> In-Reply-To: <165669922758.2104762.4398770071010254560.stgit@fed.void> References: <165669922758.2104762.4398770071010254560.stgit@fed.void> User-Agent: StGit/1.1 MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=pvalerio@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Cc: fbl@sysclose.org, i.maximets@ovn.org Subject: [ovs-dev] [PATCH v6 3/5] conntrack: Replace timeout based expiration lists with rculists. 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" From: Gaetan Rivet This patch aims to replace the expiration lists as, due to the way they are used, besides being a source of contention, they have a known issue when used with non-default policies for different zones that could lead to retaining expired connections potentially for a long time. This patch replaces them with an array of rculist used to distribute all the newly created connections in order to, during the sweeping phase, scan them without locking, and evict the expired connections only locking during the actual removal. This allows to reduce the contention introduced by the pushback performed at every packet update, also solving the issue related to zones and timeout policies. Signed-off-by: Gaetan Rivet Co-authored-by: Paolo Valerio Signed-off-by: Paolo Valerio --- v6: - minor function renaming - removed conn->lock in conn_clean() as this was unneeded. - minor commit message rephrase --- lib/conntrack-private.h | 84 +++++++++++++++++--------- lib/conntrack-tp.c | 44 +------------- lib/conntrack.c | 152 +++++++++++++++++++++++------------------------ 3 files changed, 133 insertions(+), 147 deletions(-) diff --git a/lib/conntrack-private.h b/lib/conntrack-private.h index 34c688821..676f58d83 100644 --- a/lib/conntrack-private.h +++ b/lib/conntrack-private.h @@ -29,6 +29,7 @@ #include "openvswitch/list.h" #include "openvswitch/types.h" #include "packets.h" +#include "rculist.h" #include "unaligned.h" #include "dp-packet.h" @@ -86,6 +87,31 @@ struct alg_exp_node { bool nat_rpl_dst; }; +/* Timeouts: all the possible timeout states passed to update_expiration() + * are listed here. The name will be prefix by CT_TM_ and the value is in + * milliseconds */ +#define CT_TIMEOUTS \ + CT_TIMEOUT(TCP_FIRST_PACKET) \ + CT_TIMEOUT(TCP_OPENING) \ + CT_TIMEOUT(TCP_ESTABLISHED) \ + CT_TIMEOUT(TCP_CLOSING) \ + CT_TIMEOUT(TCP_FIN_WAIT) \ + CT_TIMEOUT(TCP_CLOSED) \ + CT_TIMEOUT(OTHER_FIRST) \ + CT_TIMEOUT(OTHER_MULTIPLE) \ + CT_TIMEOUT(OTHER_BIDIR) \ + CT_TIMEOUT(ICMP_FIRST) \ + CT_TIMEOUT(ICMP_REPLY) + +enum ct_timeout { +#define CT_TIMEOUT(NAME) CT_TM_##NAME, + CT_TIMEOUTS +#undef CT_TIMEOUT + N_CT_TM +}; + +#define EXP_LISTS 100 + enum OVS_PACKED_ENUM ct_conn_type { CT_CONN_TYPE_DEFAULT, CT_CONN_TYPE_UN_NAT, @@ -96,11 +122,16 @@ struct conn { struct conn_key key; struct conn_key rev_key; struct conn_key parent_key; /* Only used for orig_tuple support. */ - struct ovs_list exp_node; struct cmap_node cm_node; uint16_t nat_action; char *alg; struct conn *nat_conn; /* The NAT 'conn' context, if there is one. */ + atomic_flag reclaimed; /* False during the lifetime of the connection, + * True as soon as a thread has started freeing + * its memory. */ + + /* Inserted once by a PMD, then managed by the 'ct_clean' thread. */ + struct rculist node; /* Mutable data. */ struct ovs_mutex lock; /* Guards all mutable fields. */ @@ -116,7 +147,6 @@ struct conn { /* Mutable data. */ bool seq_skew_dir; /* TCP sequence skew direction due to NATTing of FTP * control messages; true if reply direction. */ - bool cleaned; /* True if cleaned from expiry lists. */ /* Immutable data. */ bool alg_related; /* True if alg data connection. */ @@ -132,22 +162,6 @@ enum ct_update_res { CT_UPDATE_VALID_NEW, }; -/* Timeouts: all the possible timeout states passed to update_expiration() - * are listed here. The name will be prefix by CT_TM_ and the value is in - * milliseconds */ -#define CT_TIMEOUTS \ - CT_TIMEOUT(TCP_FIRST_PACKET) \ - CT_TIMEOUT(TCP_OPENING) \ - CT_TIMEOUT(TCP_ESTABLISHED) \ - CT_TIMEOUT(TCP_CLOSING) \ - CT_TIMEOUT(TCP_FIN_WAIT) \ - CT_TIMEOUT(TCP_CLOSED) \ - CT_TIMEOUT(OTHER_FIRST) \ - CT_TIMEOUT(OTHER_MULTIPLE) \ - CT_TIMEOUT(OTHER_BIDIR) \ - CT_TIMEOUT(ICMP_FIRST) \ - CT_TIMEOUT(ICMP_REPLY) - #define NAT_ACTION_SNAT_ALL (NAT_ACTION_SRC | NAT_ACTION_SRC_PORT) #define NAT_ACTION_DNAT_ALL (NAT_ACTION_DST | NAT_ACTION_DST_PORT) @@ -181,22 +195,17 @@ enum ct_ephemeral_range { #define FOR_EACH_PORT_IN_RANGE(curr, min, max) \ FOR_EACH_PORT_IN_RANGE__(curr, min, max, OVS_JOIN(idx, __COUNTER__)) -enum ct_timeout { -#define CT_TIMEOUT(NAME) CT_TM_##NAME, - CT_TIMEOUTS -#undef CT_TIMEOUT - N_CT_TM -}; - struct conntrack { struct ovs_mutex ct_lock; /* Protects 2 following fields. */ struct cmap conns OVS_GUARDED; - struct ovs_list exp_lists[N_CT_TM] OVS_GUARDED; + struct rculist exp_lists[EXP_LISTS]; struct cmap zone_limits OVS_GUARDED; struct cmap timeout_policies OVS_GUARDED; uint32_t hash_basis; /* Salt for hashing a connection key. */ pthread_t clean_thread; /* Periodically cleans up connection tracker. */ struct latch clean_thread_exit; /* To destroy the 'clean_thread'. */ + atomic_uint ct_next_list; + unsigned int next_sweep; /* Counting connections. */ atomic_count n_conn; /* Number of connections currently tracked. */ @@ -216,8 +225,8 @@ struct conntrack { }; /* Lock acquisition order: - * 1. 'ct_lock' - * 2. 'conn->lock' + * 1. 'conn->lock' + * 2. 'ct_lock' * 3. 'resources_lock' */ @@ -237,4 +246,23 @@ struct ct_l4_proto { struct ct_dpif_protoinfo *); }; +static unsigned int +ct_next_list(struct conntrack *ct) +{ + unsigned int old; + + atomic_add_relaxed(&ct->ct_next_list, 1u, &old); + + return old % EXP_LISTS; +} + +static inline void +conn_expire_push_front(struct conntrack *ct, struct conn *conn) + OVS_REQUIRES(ct->ct_lock) +{ + unsigned int next = ct_next_list(ct); + + rculist_push_front(&ct->exp_lists[next], &conn->node); +} + #endif /* conntrack-private.h */ diff --git a/lib/conntrack-tp.c b/lib/conntrack-tp.c index c2245038b..7b8f9007b 100644 --- a/lib/conntrack-tp.c +++ b/lib/conntrack-tp.c @@ -236,27 +236,6 @@ tm_to_ct_dpif_tp(enum ct_timeout tm) return CT_DPIF_TP_ATTR_MAX; } -static void -conn_update_expiration__(struct conntrack *ct, struct conn *conn, - enum ct_timeout tm, long long now, - uint32_t tp_value) - OVS_REQUIRES(conn->lock) -{ - ovs_mutex_unlock(&conn->lock); - - ovs_mutex_lock(&ct->ct_lock); - ovs_mutex_lock(&conn->lock); - if (!conn->cleaned) { - conn->expiration = now + tp_value * 1000; - ovs_list_remove(&conn->exp_node); - ovs_list_push_back(&ct->exp_lists[tm], &conn->exp_node); - } - ovs_mutex_unlock(&conn->lock); - ovs_mutex_unlock(&ct->ct_lock); - - ovs_mutex_lock(&conn->lock); -} - /* The conn entry lock must be held on entry and exit. */ void conn_update_expiration(struct conntrack *ct, struct conn *conn, @@ -266,41 +245,22 @@ conn_update_expiration(struct conntrack *ct, struct conn *conn, struct timeout_policy *tp; uint32_t val; - ovs_mutex_unlock(&conn->lock); - - ovs_mutex_lock(&ct->ct_lock); - ovs_mutex_lock(&conn->lock); tp = timeout_policy_lookup(ct, conn->tp_id); if (tp) { val = tp->policy.attrs[tm_to_ct_dpif_tp(tm)]; } else { val = ct_dpif_netdev_tp_def[tm_to_ct_dpif_tp(tm)]; } - ovs_mutex_unlock(&conn->lock); - ovs_mutex_unlock(&ct->ct_lock); - - ovs_mutex_lock(&conn->lock); VLOG_DBG_RL(&rl, "Update timeout %s zone=%u with policy id=%d " "val=%u sec.", ct_timeout_str[tm], conn->key.zone, conn->tp_id, val); - conn_update_expiration__(ct, conn, tm, now, val); + conn->expiration = now + val * 1000; } -static void -conn_init_expiration__(struct conntrack *ct, struct conn *conn, - enum ct_timeout tm, long long now, - uint32_t tp_value) -{ - conn->expiration = now + tp_value * 1000; - ovs_list_push_back(&ct->exp_lists[tm], &conn->exp_node); -} - -/* ct_lock must be held. */ void conn_init_expiration(struct conntrack *ct, struct conn *conn, enum ct_timeout tm, long long now) - OVS_REQUIRES(ct->ct_lock) { struct timeout_policy *tp; uint32_t val; @@ -315,5 +275,5 @@ conn_init_expiration(struct conntrack *ct, struct conn *conn, VLOG_DBG_RL(&rl, "Init timeout %s zone=%u with policy id=%d val=%u sec.", ct_timeout_str[tm], conn->key.zone, conn->tp_id, val); - conn_init_expiration__(ct, conn, tm, now, val); + conn->expiration = now + val * 1000; } diff --git a/lib/conntrack.c b/lib/conntrack.c index 38ddc9b91..819c356c1 100644 --- a/lib/conntrack.c +++ b/lib/conntrack.c @@ -39,12 +39,12 @@ #include "ovs-thread.h" #include "openvswitch/poll-loop.h" #include "random.h" +#include "rculist.h" #include "timeval.h" VLOG_DEFINE_THIS_MODULE(conntrack); COVERAGE_DEFINE(conntrack_full); -COVERAGE_DEFINE(conntrack_long_cleanup); COVERAGE_DEFINE(conntrack_l3csum_err); COVERAGE_DEFINE(conntrack_l4csum_err); COVERAGE_DEFINE(conntrack_lookup_natted_miss); @@ -94,9 +94,8 @@ static bool valid_new(struct dp_packet *pkt, struct conn_key *); static struct conn *new_conn(struct conntrack *ct, struct dp_packet *pkt, struct conn_key *, long long now, uint32_t tp_id); -static void delete_conn_cmn(struct conn *); +static void delete_conn__(struct conn *); static void delete_conn(struct conn *); -static void delete_conn_one(struct conn *conn); static enum ct_update_res conn_update(struct conntrack *ct, struct conn *conn, struct dp_packet *pkt, struct conn_lookup_ctx *ctx, @@ -309,7 +308,7 @@ conntrack_init(void) ovs_mutex_lock(&ct->ct_lock); cmap_init(&ct->conns); for (unsigned i = 0; i < ARRAY_SIZE(ct->exp_lists); i++) { - ovs_list_init(&ct->exp_lists[i]); + rculist_init(&ct->exp_lists[i]); } cmap_init(&ct->zone_limits); ct->zone_limit_seq = 0; @@ -319,6 +318,7 @@ conntrack_init(void) atomic_count_init(&ct->n_conn, 0); atomic_init(&ct->n_conn_limit, DEFAULT_N_CONN_LIMIT); atomic_init(&ct->tcp_seq_chk, true); + atomic_init(&ct->ct_next_list, 0); latch_init(&ct->clean_thread_exit); ct->clean_thread = ovs_thread_create("ct_clean", clean_thread_main, ct); ct->ipf = ipf_init(); @@ -467,7 +467,7 @@ zone_limit_delete(struct conntrack *ct, uint16_t zone) } static void -conn_clean_cmn(struct conntrack *ct, struct conn *conn) +conn_clean__(struct conntrack *ct, struct conn *conn) OVS_REQUIRES(ct->ct_lock) { if (conn->alg) { @@ -487,32 +487,34 @@ conn_clean_cmn(struct conntrack *ct, struct conn *conn) * removes the associated nat 'conn' from the lookup datastructures. */ static void conn_clean(struct conntrack *ct, struct conn *conn) - OVS_REQUIRES(ct->ct_lock) + OVS_EXCLUDED(conn->lock, ct->ct_lock) { ovs_assert(conn->conn_type == CT_CONN_TYPE_DEFAULT); - conn_clean_cmn(ct, conn); + if (atomic_flag_test_and_set(&conn->reclaimed)) { + return; + } + + ovs_mutex_lock(&ct->ct_lock); + conn_clean__(ct, conn); if (conn->nat_conn) { uint32_t hash = conn_key_hash(&conn->nat_conn->key, ct->hash_basis); cmap_remove(&ct->conns, &conn->nat_conn->cm_node, hash); } - ovs_list_remove(&conn->exp_node); - conn->cleaned = true; + + rculist_remove(&conn->node); + ovs_mutex_unlock(&ct->ct_lock); + ovsrcu_postpone(delete_conn, conn); atomic_count_dec(&ct->n_conn); } static void -conn_clean_one(struct conntrack *ct, struct conn *conn) - OVS_REQUIRES(ct->ct_lock) +conn_force_expire(struct conn *conn) { - conn_clean_cmn(ct, conn); - if (conn->conn_type == CT_CONN_TYPE_DEFAULT) { - ovs_list_remove(&conn->exp_node); - conn->cleaned = true; - atomic_count_dec(&ct->n_conn); - } - ovsrcu_postpone(delete_conn_one, conn); + ovs_mutex_lock(&conn->lock); + conn->expiration = 0; + ovs_mutex_unlock(&conn->lock); } /* Destroys the connection tracker 'ct' and frees all the allocated memory. @@ -522,15 +524,16 @@ void conntrack_destroy(struct conntrack *ct) { struct conn *conn; + latch_set(&ct->clean_thread_exit); pthread_join(ct->clean_thread, NULL); latch_destroy(&ct->clean_thread_exit); - ovs_mutex_lock(&ct->ct_lock); - CMAP_FOR_EACH (conn, cm_node, &ct->conns) { - conn_clean_one(ct, conn); + for (unsigned i = 0; i < EXP_LISTS; i++) { + RCULIST_FOR_EACH (conn, node, &ct->exp_lists[i]) { + conn_clean(ct, conn); + } } - cmap_destroy(&ct->conns); struct zone_limit *zl; CMAP_FOR_EACH (zl, node, &ct->zone_limits) { @@ -539,7 +542,6 @@ conntrack_destroy(struct conntrack *ct) cmap_remove(&ct->zone_limits, &zl->node, hash); ovsrcu_postpone(free, zl); } - cmap_destroy(&ct->zone_limits); struct timeout_policy *tp; CMAP_FOR_EACH (tp, node, &ct->timeout_policies) { @@ -548,6 +550,11 @@ conntrack_destroy(struct conntrack *ct) cmap_remove(&ct->timeout_policies, &tp->node, hash); ovsrcu_postpone(free, tp); } + + ovs_mutex_lock(&ct->ct_lock); + + cmap_destroy(&ct->conns); + cmap_destroy(&ct->zone_limits); cmap_destroy(&ct->timeout_policies); ovs_mutex_unlock(&ct->ct_lock); @@ -1087,7 +1094,9 @@ conn_not_found(struct conntrack *ct, struct dp_packet *pkt, nc->nat_conn = nat_conn; ovs_mutex_init_adaptive(&nc->lock); nc->conn_type = CT_CONN_TYPE_DEFAULT; + atomic_flag_clear(&nc->reclaimed); cmap_insert(&ct->conns, &nc->cm_node, ctx->hash); + conn_expire_push_front(ct, nc); atomic_count_inc(&ct->n_conn); ctx->conn = nc; /* For completeness. */ if (zl) { @@ -1108,8 +1117,7 @@ conn_not_found(struct conntrack *ct, struct dp_packet *pkt, * can limit DoS impact. */ nat_res_exhaustion: free(nat_conn); - ovs_list_remove(&nc->exp_node); - delete_conn_cmn(nc); + delete_conn__(nc); static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5); VLOG_WARN_RL(&rl, "Unable to NAT due to tuple space exhaustion - " "if DoS attack, use firewalling and/or zone partitioning."); @@ -1148,11 +1156,9 @@ conn_update_state(struct conntrack *ct, struct dp_packet *pkt, pkt->md.ct_state = CS_INVALID; break; case CT_UPDATE_NEW: - ovs_mutex_lock(&ct->ct_lock); if (conn_lookup(ct, &conn->key, now, NULL, NULL)) { - conn_clean(ct, conn); + conn_force_expire(conn); } - ovs_mutex_unlock(&ct->ct_lock); create_new_conn = true; break; case CT_UPDATE_VALID_NEW: @@ -1363,11 +1369,9 @@ process_one(struct conntrack *ct, struct dp_packet *pkt, /* Delete found entry if in wrong direction. 'force' implies commit. */ if (OVS_UNLIKELY(force && ctx->reply && conn)) { - ovs_mutex_lock(&ct->ct_lock); if (conn_lookup(ct, &conn->key, now, NULL, NULL)) { - conn_clean(ct, conn); + conn_force_expire(conn); } - ovs_mutex_unlock(&ct->ct_lock); conn = NULL; } @@ -1553,39 +1557,21 @@ set_label(struct dp_packet *pkt, struct conn *conn, * LLONG_MAX if 'ctb' is empty. The return value might be smaller than 'now', * if 'limit' is reached */ static long long -ct_sweep(struct conntrack *ct, long long now, size_t limit) +ct_sweep(struct conntrack *ct, struct rculist *list, long long now) + OVS_NO_THREAD_SAFETY_ANALYSIS { struct conn *conn; - long long min_expiration = LLONG_MAX; size_t count = 0; - ovs_mutex_lock(&ct->ct_lock); - - for (unsigned i = 0; i < N_CT_TM; i++) { - LIST_FOR_EACH_SAFE (conn, exp_node, &ct->exp_lists[i]) { - ovs_mutex_lock(&conn->lock); - if (now < conn->expiration || count >= limit) { - min_expiration = MIN(min_expiration, conn->expiration); - ovs_mutex_unlock(&conn->lock); - if (count >= limit) { - /* Do not check other lists. */ - COVERAGE_INC(conntrack_long_cleanup); - goto out; - } - break; - } else { - ovs_mutex_unlock(&conn->lock); - conn_clean(ct, conn); - } - count++; + RCULIST_FOR_EACH (conn, node, list) { + if (conn_expired(conn, now)) { + conn_clean(ct, conn); } + + count++; } -out: - VLOG_DBG("conntrack cleanup %"PRIuSIZE" entries in %lld msec", count, - time_msec() - now); - ovs_mutex_unlock(&ct->ct_lock); - return min_expiration; + return count; } /* Cleans up old connection entries from 'ct'. Returns the time when the @@ -1595,11 +1581,26 @@ out: static long long conntrack_clean(struct conntrack *ct, long long now) { - unsigned int n_conn_limit; + long long next_wakeup = now + 30 * 1000; + unsigned int n_conn_limit, i, count = 0; + size_t clean_end; + atomic_read_relaxed(&ct->n_conn_limit, &n_conn_limit); - size_t clean_max = n_conn_limit > 10 ? n_conn_limit / 10 : 1; - long long min_exp = ct_sweep(ct, now, clean_max); - long long next_wakeup = MIN(min_exp, now + CT_DPIF_NETDEV_TP_MIN); + clean_end = n_conn_limit / 64; + + for (i = ct->next_sweep; i < EXP_LISTS; i++) { + count += ct_sweep(ct, &ct->exp_lists[i], now); + + if (count > clean_end) { + next_wakeup = 0; + break; + } + } + + ct->next_sweep = (i < EXP_LISTS) ? i : 0; + + VLOG_DBG("conntrack cleanup %"PRIu32" entries in %lld msec", count, + time_msec() - now); return next_wakeup; } @@ -1628,6 +1629,7 @@ conntrack_clean(struct conntrack *ct, long long now) static void * clean_thread_main(void *f_) + OVS_NO_THREAD_SAFETY_ANALYSIS { struct conntrack *ct = f_; @@ -2539,7 +2541,7 @@ new_conn(struct conntrack *ct, struct dp_packet *pkt, struct conn_key *key, } static void -delete_conn_cmn(struct conn *conn) +delete_conn__(struct conn *conn) { free(conn->alg); free(conn); @@ -2551,18 +2553,9 @@ delete_conn(struct conn *conn) ovs_assert(conn->conn_type == CT_CONN_TYPE_DEFAULT); ovs_mutex_destroy(&conn->lock); free(conn->nat_conn); - delete_conn_cmn(conn); + delete_conn__(conn); } -/* Only used by conn_clean_one(). */ -static void -delete_conn_one(struct conn *conn) -{ - if (conn->conn_type == CT_CONN_TYPE_DEFAULT) { - ovs_mutex_destroy(&conn->lock); - } - delete_conn_cmn(conn); -} /* Convert a conntrack address 'a' into an IP address 'b' based on 'dl_type'. * @@ -2714,6 +2707,11 @@ conntrack_dump_next(struct conntrack_dump *dump, struct ct_dpif_entry *entry) } struct conn *conn; INIT_CONTAINER(conn, cm_node, cm_node); + + if (conn_expired(conn, now)) { + continue; + } + if ((!dump->filter_zone || conn->key.zone == dump->zone) && (conn->conn_type != CT_CONN_TYPE_UN_NAT)) { conn_to_ct_dpif_entry(conn, entry, now); @@ -2735,13 +2733,15 @@ conntrack_flush(struct conntrack *ct, const uint16_t *zone) { struct conn *conn; - ovs_mutex_lock(&ct->ct_lock); CMAP_FOR_EACH (conn, cm_node, &ct->conns) { + if (conn->conn_type != CT_CONN_TYPE_DEFAULT) { + continue; + } + if (!zone || *zone == conn->key.zone) { - conn_clean_one(ct, conn); + conn_clean(ct, conn); } } - ovs_mutex_unlock(&ct->ct_lock); return 0; } @@ -2756,7 +2756,6 @@ conntrack_flush_tuple(struct conntrack *ct, const struct ct_dpif_tuple *tuple, memset(&key, 0, sizeof(key)); tuple_to_conn_key(tuple, zone, &key); - ovs_mutex_lock(&ct->ct_lock); conn_lookup(ct, &key, time_msec(), &conn, NULL); if (conn && conn->conn_type == CT_CONN_TYPE_DEFAULT) { @@ -2766,7 +2765,6 @@ conntrack_flush_tuple(struct conntrack *ct, const struct ct_dpif_tuple *tuple, error = ENOENT; } - ovs_mutex_unlock(&ct->ct_lock); return error; } From patchwork Fri Jul 1 18:14:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Valerio X-Patchwork-Id: 1651344 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=bgpnwqhb; dkim-atps=neutral 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 (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LZNdW3fq6z9ryY for ; Sat, 2 Jul 2022 04:14:39 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id 11E8761443; Fri, 1 Jul 2022 18:14:37 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org 11E8761443 Authentication-Results: smtp3.osuosl.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=bgpnwqhb 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 FyOd6i-uJO1Q; Fri, 1 Jul 2022 18:14:35 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp3.osuosl.org (Postfix) with ESMTPS id C452B6146C; Fri, 1 Jul 2022 18:14:32 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org C452B6146C Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 61C7BC0032; Fri, 1 Jul 2022 18:14:32 +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 05D90C0032 for ; Fri, 1 Jul 2022 18:14:31 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 55F9D415C5 for ; Fri, 1 Jul 2022 18:14:24 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 55F9D415C5 Authentication-Results: smtp2.osuosl.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=bgpnwqhb 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 Bmb4PanFHnXi for ; Fri, 1 Jul 2022 18:14:23 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 6ACEF40492 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by smtp2.osuosl.org (Postfix) with ESMTPS id 6ACEF40492 for ; Fri, 1 Jul 2022 18:14:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656699262; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SAUQKIsH0mf3Jh+/eIb1I5u8Gg8Z5qncreea6blL7hQ=; b=bgpnwqhbpKaHifGfrSmn1oOLoMdj+KEkhP/fuyfEF7tN2jk+sm2KWHCXvKAL2sYTc38tXN YsnFV4UFIwzX+Vgjl07xi2eXYaLuwbdrmCWKdhXdTxOc8tnnK85E1sBTnvJTWV7FZ7YF0K xwwnLhcI/vixJdVfGhyeWDtbLucsFWE= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-517-dP2AdXzPNRyFEJVYBztIFA-1; Fri, 01 Jul 2022 14:14:21 -0400 X-MC-Unique: dP2AdXzPNRyFEJVYBztIFA-1 Received: by mail-wm1-f72.google.com with SMTP id j35-20020a05600c1c2300b003a167dfa0ecso1711700wms.5 for ; Fri, 01 Jul 2022 11:14:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=SAUQKIsH0mf3Jh+/eIb1I5u8Gg8Z5qncreea6blL7hQ=; b=yd0V+Q53jADrG8SaoqyutFuwpV1AGHOKaorS2g7ehCNCPPzLqqvYUmdbuybZ6YnCCU RRVVYVJwFjs5Gp14Mi2YrvCJX9+tPxGFE2xjiuvqjlGODOIsD84TBnJvJZvHFdfWgxGS og6coQ5zxcFLAuyqyyQB3UtBPYMALFo4sGNyIPIXD7lgAcoXjKWaWQGObBo2Z2KYRXAk 9ViPNgaiiMU96WLYrfTwaoZ9W8Bkv+kBsF0GYlvt6SDMJxomJwk3d4bKtC9DOXPOUpin CtAKeCYH/1eG1Hdx/ygsVZ5zTgpMoL2YtAjrn4V2DkAa54pZl12lYU/vgWod2ALm7+by b8pQ== X-Gm-Message-State: AJIora8O/8UabOkVGXL2R/1K2ziPc41oB7mVU2QdV9xCZRJasLl+z+7R DyphjL2cplci/OfiiMi/S/5PNy5Fms/Zp6GsLq7S3X0KyWiIZOj8MjPNsbWqWX0bOTS1XTwAQtb A3t9NbX9gxHd5nFnEh32xrglhcL5frtHRFKaWAZlrQf0/+hrTMnOMEuKZaoGUPUlS X-Received: by 2002:a05:6000:2a8:b0:21b:bdd9:569a with SMTP id l8-20020a05600002a800b0021bbdd9569amr15380076wry.170.1656699260082; Fri, 01 Jul 2022 11:14:20 -0700 (PDT) X-Google-Smtp-Source: AGRyM1siG8HVM3HDj91QdGIp6SuQPhJJ9j/9fWg3IeRBx68HMt7xh35IRyS5phx9dJRH3QZy+UvZZg== X-Received: by 2002:a05:6000:2a8:b0:21b:bdd9:569a with SMTP id l8-20020a05600002a800b0021bbdd9569amr15380046wry.170.1656699259845; Fri, 01 Jul 2022 11:14:19 -0700 (PDT) Received: from localhost (net-5-95-131-226.cust.vodafonedsl.it. [5.95.131.226]) by smtp.gmail.com with ESMTPSA id r185-20020a1c2bc2000000b003a0484c069bsm12524197wmr.41.2022.07.01.11.14.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Jul 2022 11:14:19 -0700 (PDT) From: Paolo Valerio To: dev@openvswitch.org Date: Fri, 01 Jul 2022 20:14:18 +0200 Message-ID: <165669925875.2104762.11271928401616464351.stgit@fed.void> In-Reply-To: <165669922758.2104762.4398770071010254560.stgit@fed.void> References: <165669922758.2104762.4398770071010254560.stgit@fed.void> User-Agent: StGit/1.1 MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=pvalerio@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Cc: fbl@sysclose.org, i.maximets@ovn.org Subject: [ovs-dev] [PATCH v6 4/5] conntrack: Use an atomic conn expiration value 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" From: Gaetan Rivet A lock is taken during conn_lookup() to check whether a connection is expired before returning it. This lock can have some contention. Even though this lock ensures a consistent sequence of writes, it does not imply a specific order. A ct_clean thread taking the lock first could read a value that would be updated immediately after by a PMD waiting on the same lock, just as well as the inverse order. As such, the expiration time can be stale anytime it is read. In this context, using an atomic will ensure the same guarantees for either writes or reads, i.e. writes are consistent and reads are not undefined behaviour. Reading an atomic is however less costly than taking and releasing a lock. Signed-off-by: Gaetan Rivet Signed-off-by: Paolo Valerio --- v6: - A couple of hunks slipped away from the stg refresh before sending v5. --- lib/conntrack-private.h | 2 +- lib/conntrack-tp.c | 2 +- lib/conntrack.c | 27 +++++++++++++++------------ 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/lib/conntrack-private.h b/lib/conntrack-private.h index 676f58d83..d36845c2d 100644 --- a/lib/conntrack-private.h +++ b/lib/conntrack-private.h @@ -136,7 +136,7 @@ struct conn { /* Mutable data. */ struct ovs_mutex lock; /* Guards all mutable fields. */ ovs_u128 label; - long long expiration; + atomic_llong expiration; uint32_t mark; int seq_skew; diff --git a/lib/conntrack-tp.c b/lib/conntrack-tp.c index 7b8f9007b..89cb2704a 100644 --- a/lib/conntrack-tp.c +++ b/lib/conntrack-tp.c @@ -255,7 +255,7 @@ conn_update_expiration(struct conntrack *ct, struct conn *conn, "val=%u sec.", ct_timeout_str[tm], conn->key.zone, conn->tp_id, val); - conn->expiration = now + val * 1000; + atomic_store_relaxed(&conn->expiration, now + val * 1000); } void diff --git a/lib/conntrack.c b/lib/conntrack.c index 819c356c1..9d7891d6a 100644 --- a/lib/conntrack.c +++ b/lib/conntrack.c @@ -100,6 +100,7 @@ static enum ct_update_res conn_update(struct conntrack *ct, struct conn *conn, struct dp_packet *pkt, struct conn_lookup_ctx *ctx, long long now); +static long long int conn_expiration(const struct conn *); static bool conn_expired(struct conn *, long long now); static void set_mark(struct dp_packet *, struct conn *, uint32_t val, uint32_t mask); @@ -512,9 +513,7 @@ conn_clean(struct conntrack *ct, struct conn *conn) static void conn_force_expire(struct conn *conn) { - ovs_mutex_lock(&conn->lock); - conn->expiration = 0; - ovs_mutex_unlock(&conn->lock); + atomic_store_relaxed(&conn->expiration, 0); } /* Destroys the connection tracker 'ct' and frees all the allocated memory. @@ -969,13 +968,10 @@ un_nat_packet(struct dp_packet *pkt, const struct conn *conn, static void conn_seq_skew_set(struct conntrack *ct, const struct conn *conn_in, long long now, int seq_skew, bool seq_skew_dir) - OVS_NO_THREAD_SAFETY_ANALYSIS { struct conn *conn; - ovs_mutex_unlock(&conn_in->lock); - conn_lookup(ct, &conn_in->key, now, &conn, NULL); - ovs_mutex_lock(&conn_in->lock); + conn_lookup(ct, &conn_in->key, now, &conn, NULL); if (conn && seq_skew) { conn->seq_skew = seq_skew; conn->seq_skew_dir = seq_skew_dir; @@ -2515,14 +2511,21 @@ conn_update(struct conntrack *ct, struct conn *conn, struct dp_packet *pkt, return update_res; } +static long long int +conn_expiration(const struct conn *conn) +{ + long long int expiration; + + atomic_read_relaxed(&CONST_CAST(struct conn *, conn)->expiration, + &expiration); + return expiration; +} + static bool conn_expired(struct conn *conn, long long now) { if (conn->conn_type == CT_CONN_TYPE_DEFAULT) { - ovs_mutex_lock(&conn->lock); - bool expired = now >= conn->expiration ? true : false; - ovs_mutex_unlock(&conn->lock); - return expired; + return now >= conn_expiration(conn); } return false; } @@ -2655,7 +2658,7 @@ conn_to_ct_dpif_entry(const struct conn *conn, struct ct_dpif_entry *entry, entry->mark = conn->mark; memcpy(&entry->labels, &conn->label, sizeof entry->labels); - long long expiration = conn->expiration - now; + long long expiration = conn_expiration(conn) - now; struct ct_l4_proto *class = l4_protos[conn->key.nw_proto]; if (class->conn_get_protoinfo) { From patchwork Fri Jul 1 18:14:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Valerio X-Patchwork-Id: 1651345 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=iYIcTMaN; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.136; helo=smtp3.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from smtp3.osuosl.org (smtp3.osuosl.org [140.211.166.136]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LZNdj1t6Hz9ryY for ; Sat, 2 Jul 2022 04:14:49 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id 6E16D61455; Fri, 1 Jul 2022 18:14:47 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org 6E16D61455 Authentication-Results: smtp3.osuosl.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=iYIcTMaN 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 MB24s2z7Dzbm; Fri, 1 Jul 2022 18:14:45 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp3.osuosl.org (Postfix) with ESMTPS id 1310861040; Fri, 1 Jul 2022 18:14:44 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org 1310861040 Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id DD6F3C0032; Fri, 1 Jul 2022 18:14:43 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp3.osuosl.org (smtp3.osuosl.org [IPv6:2605:bc80:3010::136]) by lists.linuxfoundation.org (Postfix) with ESMTP id 068B4C002D for ; Fri, 1 Jul 2022 18:14:43 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id 7F78F61454 for ; Fri, 1 Jul 2022 18:14:30 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org 7F78F61454 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 ub0FXWRXyhjV for ; Fri, 1 Jul 2022 18:14:30 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org B9DAA61458 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by smtp3.osuosl.org (Postfix) with ESMTPS id B9DAA61458 for ; Fri, 1 Jul 2022 18:14:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656699268; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5dcZg0jHa7qdf+1Ur2A59X1ZIfmbdFK+0Bqn/oHU/pE=; b=iYIcTMaNekVri7wk4hoRgyiWkgxQd48kjXW3wIpKrNpgLhZ7cRBfxryYobpGyl8wQhqFs6 1Daq/V6M5LmxnN1u2k6J56hVtx+tiHscRPFzDaIEZUGIbLRdDFkJ5CtFmGudZo39PMfaqk zWeJ9QX5ohtEsfGv+ckJJHhrM55zMIo= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-356-soVRgEIfNfy61WjjPiJs9g-1; Fri, 01 Jul 2022 14:14:27 -0400 X-MC-Unique: soVRgEIfNfy61WjjPiJs9g-1 Received: by mail-wm1-f72.google.com with SMTP id m20-20020a05600c4f5400b003a03aad6bdfso1299957wmq.6 for ; Fri, 01 Jul 2022 11:14:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=5dcZg0jHa7qdf+1Ur2A59X1ZIfmbdFK+0Bqn/oHU/pE=; b=LCLfc+ju3xqhs9PKQ5OwE/PNaqP0C08dHwpCNe+wHzZ/Y4a41W6SHQDaoOsUr0RqL5 0zzDt0PCrkNZgsx/wjHNXaV3/mZs8BZZqvwL9pvQFUvFh8HhuUHuFdKmfsOIgIajOkjT FYXFgo6/hliFKzozfqkuIcAwRVyQkDSaVKObxwPD2j5nVDaqtfNCyBBkQ7Uc9EFnsM4M 8+TDsYAiev/EzT6K243Av2GvjhLDmVSXs5hWArVDMcGPbfICbS33u8JmGHNSiydwK7Yc M+WhitESVVWQg0PCVWKpPxhaHBDJge9691GjXjlR4IpUmp3OcFCMeqZ7vVpXYrmS1LR0 f4XQ== X-Gm-Message-State: AJIora++9Ef+7qRxeaKzfWN8VdDR7O93TLslRTGVQNdbYdzMkYGS+IuP JQvJw16QHSocd/Ibgrr8eJ2/TeCfbz4d16oo43ZlPmhOhFeOzVMHEuqu1Orw8JWDeP06lC72AiY aTKDn2p+5Cf/zFsLcwxrIw+HiYuJFKdlLGP1TUG4eNedvL4jFwgZ5jWjn+7dAF4y5 X-Received: by 2002:a05:600c:1c93:b0:3a0:579e:9f44 with SMTP id k19-20020a05600c1c9300b003a0579e9f44mr17844109wms.82.1656699266251; Fri, 01 Jul 2022 11:14:26 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sG7UHw1McvoC9mA/nwKdUY2Epp6OujK4JJwyw5bNCzGhoEPn3ysOkBaPAjjeOnf21W8vT8rA== X-Received: by 2002:a05:600c:1c93:b0:3a0:579e:9f44 with SMTP id k19-20020a05600c1c9300b003a0579e9f44mr17844088wms.82.1656699265984; Fri, 01 Jul 2022 11:14:25 -0700 (PDT) Received: from localhost (net-5-95-131-226.cust.vodafonedsl.it. [5.95.131.226]) by smtp.gmail.com with ESMTPSA id bd16-20020a05600c1f1000b003a18ecfcd8csm2390495wmb.19.2022.07.01.11.14.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Jul 2022 11:14:25 -0700 (PDT) From: Paolo Valerio To: dev@openvswitch.org Date: Fri, 01 Jul 2022 20:14:24 +0200 Message-ID: <165669926493.2104762.9912313397847070677.stgit@fed.void> In-Reply-To: <165669922758.2104762.4398770071010254560.stgit@fed.void> References: <165669922758.2104762.4398770071010254560.stgit@fed.void> User-Agent: StGit/1.1 MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=pvalerio@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Cc: fbl@sysclose.org, i.maximets@ovn.org Subject: [ovs-dev] [PATCH v6 5/5] conntrack: Check for expiration before comparing the keys during the lookup 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" From: Ilya Maximets This could save some costly key comparison miss, especially in the case there are many expired connections waiting for the sweeper to evict them. Signed-off-by: Ilya Maximets Signed-off-by: Paolo Valerio --- lib/conntrack.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/conntrack.c b/lib/conntrack.c index 9d7891d6a..4a0166ecb 100644 --- a/lib/conntrack.c +++ b/lib/conntrack.c @@ -583,14 +583,17 @@ conn_key_lookup(struct conntrack *ct, const struct conn_key *key, bool found = false; CMAP_FOR_EACH_WITH_HASH (conn, cm_node, hash, &ct->conns) { - if (!conn_key_cmp(&conn->key, key) && !conn_expired(conn, now)) { + if (conn_expired(conn, now)) { + continue; + } + if (!conn_key_cmp(&conn->key, key)) { found = true; if (reply) { *reply = false; } break; } - if (!conn_key_cmp(&conn->rev_key, key) && !conn_expired(conn, now)) { + if (!conn_key_cmp(&conn->rev_key, key)) { found = true; if (reply) { *reply = true;