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 B69035A0269 for ; Fri, 4 Nov 2022 09:43:42 +0100 (CET) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4N3Z0W2Ttsz4xwt; Fri, 4 Nov 2022 19:43:39 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1667551419; bh=ao1suVGJomSqkxkkIaoY++BzsG5hJpHWlIPcUKxv0w0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mWRd2Q1vnTj1SAhbh9qGyy25yVPX/rh1LUZlUwnbOZvbop3SMOgj0cohqajlXmLb3 FPiEn6NImTkmxE9p2ypL0y3zhaeJYM2h+BSAEScAkWwG4AoTgEMNZTv+7+DrwmsoIL KFZFrolCk2A+b1peIcWFx9drnAU016qs1av9n1dI= From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 04/10] tcp: Hash IPv4 and IPv4-mapped-IPv6 addresses the same Date: Fri, 4 Nov 2022 19:43:27 +1100 Message-Id: <20221104084333.3761760-5-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221104084333.3761760-1-david@gibson.dropbear.id.au> References: <20221104084333.3761760-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: E33DOUH2NEJNJMEIHOZE2PGVQRKHHW65 X-Message-ID-Hash: E33DOUH2NEJNJMEIHOZE2PGVQRKHHW65 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.3 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: In the tcp_conn structure, we represent IPv4 connections with IPv4-mapped IPv6 addresses in the single address field. However, we have different paths which will calculate different hashes for IPv4 and equivalent IPv4-mapped IPv6 addresses. This will cause problems for some future changes. Make the hash function work the same for these two cases, by always mapping IPv4 addresses into IPv4-mapped addresses before hashing. This involves some ugly temporaries, but later changes should improve this again. Signed-off-by: David Gibson --- siphash.c | 1 + tcp.c | 33 +++++++++++++++------------------ 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/siphash.c b/siphash.c index 37a6d73..516a508 100644 --- a/siphash.c +++ b/siphash.c @@ -104,6 +104,7 @@ * * Return: the 64-bit hash output */ +/* cppcheck-suppress unusedFunction */ uint64_t siphash_8b(const uint8_t *in, const uint64_t *k) { PREAMBLE(8); diff --git a/tcp.c b/tcp.c index 508d6e9..4645004 100644 --- a/tcp.c +++ b/tcp.c @@ -1315,30 +1315,27 @@ __attribute__((__noinline__)) /* See comment in Makefile */ static unsigned int tcp_hash(const struct ctx *c, int af, const void *addr, in_port_t tap_port, in_port_t sock_port) { + struct { + struct in6_addr addr; + in_port_t tap_port; + in_port_t sock_port; + } __attribute__((__packed__)) in = { + .tap_port = tap_port, + .sock_port = sock_port, + }; uint64_t b = 0; if (af == AF_INET) { - struct { - struct in_addr addr; - in_port_t tap_port; - in_port_t sock_port; - } __attribute__((__packed__)) in = { - *(struct in_addr *)addr, tap_port, sock_port, - }; - - b = siphash_8b((uint8_t *)&in, c->tcp.hash_secret); - } else if (af == AF_INET6) { - struct { - struct in6_addr addr; - in_port_t tap_port; - in_port_t sock_port; - } __attribute__((__packed__)) in = { - *(struct in6_addr *)addr, tap_port, sock_port, - }; + struct in6_addr v4mapped; - b = siphash_20b((uint8_t *)&in, c->tcp.hash_secret); + encode_ip4mapped_ip6(&v4mapped, addr); + in.addr = v4mapped; + } else { + in.addr = *(struct in6_addr *)addr; } + b = siphash_20b((uint8_t *)&in, c->tcp.hash_secret); + return (unsigned int)(b % TCP_HASH_TABLE_SIZE); } -- 2.38.1