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 28EB45A027A for ; Thu, 7 Dec 2023 06:54:06 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1701928435; bh=MDEtaqz9VohY7Tw1b1uSJJlNDLm0jf1FtAsjGoVoEAQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XCBr/2cI0gnoXktQ3V9guy7OV0Rj5ebQ1XmY47KCO3m+vB6Rg5PnA3zJliluwp4xb JsBJGuS4S6WsCmYow1hcIvPWpUu9f4bB0FTorx9B327qPRP122u6z9VzL2cnXbzLOc 18ZPZlso5AsrOTp2bTaDwZakW1Ht1Zj9BZD+mnd5t0S9VvRbz4OEIViCcqnqnGDtQm KDoJMVPbLxVQgCa15NIXlhPRW+HiTq+/h2RIs0T9DKOxqIgCrjZ1Vgm4IzY5n6mH6e fYluepKZMYJImGi+12dqAdd+AEe7uNt6C0/T8xvR4eKH0886YviseKBs9wYaXHcyTe 7bWpUQ7XoKwYA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Sm3Nz6CzYz4x2N; Thu, 7 Dec 2023 16:53:55 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v2 4/4] tcp: Don't account for hash table size in tcp_hash() Date: Thu, 7 Dec 2023 16:53:53 +1100 Message-ID: <20231207055353.1245933-5-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231207055353.1245933-1-david@gibson.dropbear.id.au> References: <20231207055353.1245933-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: OFWGHZABRVOTSCXVZK3FNPKYE74LLBOC X-Message-ID-Hash: OFWGHZABRVOTSCXVZK3FNPKYE74LLBOC 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 8dc9f31..63b39e0 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); } @@ -1198,7 +1195,7 @@ static inline unsigned tcp_hash_probe(const struct ctx *c, const struct tcp_tap_conn *conn) { flow_sidx_t sidx = FLOW_SIDX(conn, TAPSIDE); - unsigned b = tcp_conn_hash(c, conn); + unsigned b = tcp_conn_hash(c, conn) % TCP_HASH_TABLE_SIZE; /* Linear probing */ while (!flow_sidx_eq(tc_hash[b], FLOW_SIDX_NONE) && @@ -1241,7 +1238,7 @@ static void tcp_hash_remove(const struct ctx *c, for (s = mod_sub(b, 1, TCP_HASH_TABLE_SIZE); (flow = flow_at_sidx(tc_hash[s])); s = mod_sub(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 (!mod_between(h, s, b, TCP_HASH_TABLE_SIZE)) { /* tc_hash[s] can live in tc_hash[b]'s slot */ @@ -1297,7 +1294,7 @@ static struct tcp_tap_conn *tcp_hash_lookup(const struct ctx *c, inany_from_af(&aany, af, faddr); - b = tcp_hash(c, &aany, eport, fport); + b = tcp_hash(c, &aany, eport, fport) % TCP_HASH_TABLE_SIZE; while ((flow = flow_at_sidx(tc_hash[b])) && !tcp_hash_match(&flow->tcp, &aany, eport, fport)) b = mod_sub(b, 1, TCP_HASH_TABLE_SIZE); -- 2.43.0