From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 333E55A0280 for ; Mon, 4 Dec 2023 04:16:20 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1701659773; bh=jUKn2Wtr+tvVq3ZC3KEVtA2oJdoL5mQIZzUBgUN+1xY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HvgJP9FAcj3SMljfZtmwMFqndG9gdBOYEqbt6KdsK1Pxb3oAhfMATNaRfA5BN9CRc S1aUJ/JkeWvpWVq+l9kSLuFZ8ZW1v3TAsjYu0GYUfJoR+azSXkzAVz9FgaTRWbDiw5 3q2LVAJyQSEx+izArvxLtjR1ik7rPO/FZ5TwTFsq+LGY2dZQU1kWERsB7rWU3Akqyl FIU115Y3YXnzVOr8l9U7lcWOZNVd/7FSX/KE6UwxD+FfulVy3xPi1OFhlUjPkBrizj coP57erhMXk6c2OrHu240ipLXLfBNpBarYWekrSxRvj6TvjYced/Wa/xK88wbjS++5 PDPdSIZxiCmSQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Sk82P5nfBz4xCt; Mon, 4 Dec 2023 14:16:13 +1100 (AEDT) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 3/3] tcp: Don't account for hash table size in tcp_hash() Date: Mon, 4 Dec 2023 14:16:11 +1100 Message-ID: <20231204031611.3566791-4-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231204031611.3566791-1-david@gibson.dropbear.id.au> References: <20231204031611.3566791-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 5ZNSY3AQQG3E323QHBLMHNOJ6CG6BHU4 X-Message-ID-Hash: 5ZNSY3AQQG3E323QHBLMHNOJ6CG6BHU4 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: Currently tcp_hash() returns the hash bucket for a value, that is the hash modulo the size of the hash table. Usually it's a bit more flexible to have hash functions return a "raw" hash value and perform the modulus in the callers. That allows the same hash function to be used for multiple tables of different sizes, or to re-use the hash for other purposes. We don't do anything like that with tcp_hash() at present, but we have some plans to do so. Prepare for that by making tcp_hash() and tcp_conn_hash() return raw hash values. Signed-off-by: David Gibson --- tcp.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/tcp.c b/tcp.c index 7e438b7..6b12feb 100644 --- a/tcp.c +++ b/tcp.c @@ -1159,18 +1159,15 @@ static int tcp_hash_match(const struct tcp_tap_conn *conn, * @eport: Guest side endpoint port * @fport: Guest side forwarding port * - * Return: hash value, already modulo size of the hash table + * Return: hash value, needs to be adjusted for table size */ -static unsigned int tcp_hash(const struct ctx *c, const union inany_addr *faddr, - in_port_t eport, in_port_t fport) +static uint64_t tcp_hash(const struct ctx *c, const union inany_addr *faddr, + in_port_t eport, in_port_t fport) { struct siphash_state state = SIPHASH_INIT(c->hash_secret); - uint64_t hash; inany_siphash_feed(&state, faddr); - hash = siphash_final(&state, 20, (uint64_t)eport << 16 | fport); - - return (unsigned int)(hash % TCP_HASH_TABLE_SIZE); + return siphash_final(&state, 20, (uint64_t)eport << 16 | fport); } /** @@ -1178,10 +1175,10 @@ static unsigned int tcp_hash(const struct ctx *c, const union inany_addr *faddr, * @c: Execution context * @conn: Connection * - * Return: hash value, already modulo size of the hash table + * Return: hash value, needs to be adjusted for table size */ -static unsigned int tcp_conn_hash(const struct ctx *c, - const struct tcp_tap_conn *conn) +static uint64_t tcp_conn_hash(const struct ctx *c, + const struct tcp_tap_conn *conn) { return tcp_hash(c, &conn->faddr, conn->eport, conn->fport); } @@ -1201,7 +1198,7 @@ static inline unsigned tcp_hash_probe(const struct ctx *c, unsigned b; /* Linear probing */ - for (b = tcp_conn_hash(c, conn); + for (b = tcp_conn_hash(c, conn) % TCP_HASH_TABLE_SIZE; !flow_sidx_eq(tc_hash[b], FLOW_SIDX_NONE) && !flow_sidx_eq(tc_hash[b], sidx); b = (b + 1) % TCP_HASH_TABLE_SIZE) @@ -1243,7 +1240,7 @@ static void tcp_hash_remove(const struct ctx *c, for (s = (b + 1) % TCP_HASH_TABLE_SIZE; (flow = flow_at_sidx(tc_hash[s])); s = (s + 1) % TCP_HASH_TABLE_SIZE) { - unsigned h = tcp_conn_hash(c, &flow->tcp); + unsigned h = tcp_conn_hash(c, &flow->tcp) % TCP_HASH_TABLE_SIZE; if (in_mod_range(h, b, s, TCP_HASH_TABLE_SIZE)) { /* tc_hash[s] can live in tc_hash[b]'s slot */ @@ -1299,7 +1296,7 @@ static struct tcp_tap_conn *tcp_hash_lookup(const struct ctx *c, inany_from_af(&aany, af, faddr); - for (b = tcp_hash(c, &aany, eport, fport); + for (b = tcp_hash(c, &aany, eport, fport) % TCP_HASH_TABLE_SIZE; (flow = flow_at_sidx(tc_hash[b])) && !tcp_hash_match(&flow->tcp, &aany, eport, fport); b = (b + 1) % TCP_HASH_TABLE_SIZE) -- 2.43.0