From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 943605A004F for ; Thu, 04 Jul 2024 06:58:58 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1720069127; bh=eDpyxKO/SSoX7kL6KN8YABu+sP84Tv/3cxZvhYANEiw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=V/JmkWEVq+jWLX1taz+jqJL9WHblRGJ+9P0BTzWEj/IdeEceF/Gvo8nD4sIO+SqF+ ps6prsgxiNmFC6qmiVKmyzd5wi4KnwlC1fG5lZzGWks22Gl8Tini77F2c465O6efMo OqVoCgiWmIWEIVn4E2O2i1eSHaX5VPdv9PpchBJdqilkuQXIC8sMJgZjTgJhkc3461 9S53hZgRbsvdCcS/YVyEqqTnfq9f2/P1JYAB6PpqdXO7GB+7Bj+DPIYYF6LimiVV1T ySAw7E+IYAY7LMYfo3kTbd21tYamyxUqRC4wnXGRRMgzd/sEoB+C2Tl5JnVvAVlcn3 y6uPw6yFpcAoQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4WF4DR3cTrz4wx6; Thu, 4 Jul 2024 14:58:47 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 02/11] flow: Add flow_sidx_valid() helper Date: Thu, 4 Jul 2024 14:58:26 +1000 Message-ID: <20240704045835.1149746-3-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240704045835.1149746-1-david@gibson.dropbear.id.au> References: <20240704045835.1149746-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 5P4E6YL7HTQCYJAQEPTVHC3OWF33EMQC X-Message-ID-Hash: 5P4E6YL7HTQCYJAQEPTVHC3OWF33EMQC X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: To implement the TCP hash table, we need an invalid (NULL-like) value for flow_sidx_t. We use FLOW_SIDX_NONE for that, but for defensiveness, we treat (usually) anything with an out of bounds flow index the same way. That's not always done consistently though. In flow_at_sidx() we open code a check on the flow index. In tcp_hash_probe() we instead compare against FLOW_SIDX_NONE, and in some other places we use the fact that flow_at_sidx() will return NULL in this case, even if we don't otherwise need the flow it returns. Clean this up a bit, by adding an explicit flow_sidx_valid() test function. Signed-off-by: David Gibson --- flow.h | 11 +++++++++++ flow_table.h | 2 +- tcp.c | 7 +++---- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/flow.h b/flow.h index 29ef9f12..d1f49c65 100644 --- a/flow.h +++ b/flow.h @@ -176,6 +176,17 @@ static_assert(sizeof(flow_sidx_t) <= sizeof(uint32_t), #define FLOW_SIDX_NONE ((flow_sidx_t){ .flow = FLOW_MAX }) +/** + * flow_sidx_valid() - Test if a sidx is valid + * @sidx: sidx value + * + * Return: true if @sidx refers to a valid flow & side + */ +static inline bool flow_sidx_valid(flow_sidx_t sidx) +{ + return sidx.flow < FLOW_MAX; +} + /** * flow_sidx_eq() - Test if two sidx values are equal * @a, @b: sidx values diff --git a/flow_table.h b/flow_table.h index 1b163491..226ddbdd 100644 --- a/flow_table.h +++ b/flow_table.h @@ -73,7 +73,7 @@ static inline unsigned flow_idx(const struct flow_common *f) */ static inline union flow *flow_at_sidx(flow_sidx_t sidx) { - if (sidx.flow >= FLOW_MAX) + if (!flow_sidx_valid(sidx)) return NULL; return FLOW(sidx.flow); } diff --git a/tcp.c b/tcp.c index a490920a..75b959a2 100644 --- a/tcp.c +++ b/tcp.c @@ -880,8 +880,7 @@ static inline unsigned tcp_hash_probe(const struct ctx *c, flow_sidx_t sidx = FLOW_SIDX(conn, TAPSIDE(conn)); /* Linear probing */ - while (!flow_sidx_eq(tc_hash[b], FLOW_SIDX_NONE) && - !flow_sidx_eq(tc_hash[b], sidx)) + while (flow_sidx_valid(tc_hash[b]) && !flow_sidx_eq(tc_hash[b], sidx)) b = mod_sub(b, 1, TCP_HASH_TABLE_SIZE); return b; @@ -909,9 +908,9 @@ static void tcp_hash_remove(const struct ctx *c, const struct tcp_tap_conn *conn) { unsigned b = tcp_hash_probe(c, conn), s; - union flow *flow = flow_at_sidx(tc_hash[b]); + union flow *flow; - if (!flow) + if (!flow_sidx_valid(tc_hash[b])) return; /* Redundant remove */ flow_dbg(conn, "hash table remove: sock %i, bucket: %u", conn->sock, b); -- 2.45.2