DPDK-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Bruce Richardson <bruce.richardson@intel.com>
To: dev@dpdk.org
Cc: Bruce Richardson <bruce.richardson@intel.com>
Subject: [PATCH 08/11] hash: add explicit handling for RSS with split L3/L4 data
Date: Tue,  1 Dec 2026 15:16:20 +0000	[thread overview]
Message-ID: <20261201151623.2240031-9-bruce.richardson@intel.com> (raw)
In-Reply-To: <20261201151623.2240031-1-bruce.richardson@intel.com>

If the L3 header has options or extension headers after the IP
addresses, then we cannot just compute the RSS hash of the L3+L4 data as
a contiguous block. Creating a generic function which merges the data
together using scalar functions leads to slowdown in the subsequent
vector functions, due to failed store forwarding (small stores followed
by larger loads). We can fix this by creating explicit vector split-key
handling functions, which will use vector stores rather than scalar when
merging the values to be hashed.

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
 app/test/test_thash.c        | 129 +++++++++++++++++++++++++++++++++++
 lib/hash/thash_gfni_avx512.c |  33 +++++++++
 lib/hash/thash_private.h     |  20 ++++++
 lib/hash/thash_rss.c         |  52 +++++++-------
 4 files changed, 210 insertions(+), 24 deletions(-)

diff --git a/app/test/test_thash.c b/app/test/test_thash.c
index 413292943d..148213be4b 100644
--- a/app/test/test_thash.c
+++ b/app/test/test_thash.c
@@ -2,6 +2,9 @@
  * Copyright(c) 2015-2019 Vladimir Medvedkin <medvedkinv@gmail.com>
  */
 
+#include <stdbool.h>
+#include <string.h>
+
 #include <rte_common.h>
 #include <rte_eal.h>
 #include <rte_errno.h>
@@ -9,7 +12,9 @@
 #include <rte_random.h>
 #include <rte_malloc.h>
 #include <rte_byteorder.h>
+#include <rte_vect.h>
 
+#include "generic/rte_vect.h"
 #include "test.h"
 
 #include <rte_thash.h>
@@ -402,6 +407,129 @@ test_rss_ctx_create_free(void)
 	return TEST_SUCCESS;
 }
 
+static int
+check_rss_packet_hashes(const struct rte_thash_rss_ctx *ctx)
+{
+	struct {
+		struct rte_ipv4_hdr ip;
+		uint8_t ports[4];
+	} v4_packet = {0};
+	struct {
+		struct rte_ipv6_hdr ip;
+		uint8_t ports[4];
+	} v6_packet = {0};
+	uint8_t split_ports[4];
+	uint32_t hash;
+
+	for (unsigned int i = 0; i < RTE_DIM(v4_tbl); i++) {
+		const rte_be16_t sport = rte_cpu_to_be_16(v4_tbl[i].src_port);
+		const rte_be16_t dport = rte_cpu_to_be_16(v4_tbl[i].dst_port);
+
+		v4_packet.ip.src_addr = rte_cpu_to_be_32(v4_tbl[i].src_ip);
+		v4_packet.ip.dst_addr = rte_cpu_to_be_32(v4_tbl[i].dst_ip);
+		memcpy(v4_packet.ports, &sport, sizeof(sport));
+		memcpy(v4_packet.ports + sizeof(sport), &dport, sizeof(dport));
+		memcpy(split_ports, v4_packet.ports, sizeof(split_ports));
+
+		TEST_ASSERT_SUCCESS(rte_thash_rss_ipv4_l3(ctx, &v4_packet.ip, &hash),
+			"IPv4 L3 hash failed at %u", i);
+		TEST_ASSERT_EQUAL(hash, v4_tbl[i].hash_l3, "IPv4 L3 hash mismatch at %u", i);
+		TEST_ASSERT_SUCCESS(rte_thash_rss_ipv4_l4(ctx, &v4_packet.ip,
+				v4_packet.ports, &hash), "IPv4 adjacent L4 hash failed at %u", i);
+		TEST_ASSERT_EQUAL(hash, v4_tbl[i].hash_l3l4,
+			"IPv4 adjacent L4 hash mismatch at %u", i);
+		TEST_ASSERT_SUCCESS(rte_thash_rss_ipv4_l4(ctx, &v4_packet.ip, split_ports, &hash),
+			"IPv4 split L4 hash failed at %u", i);
+		TEST_ASSERT_EQUAL(hash, v4_tbl[i].hash_l3l4,
+			"IPv4 split L4 hash mismatch at %u", i);
+	}
+
+	for (unsigned int i = 0; i < RTE_DIM(v6_tbl); i++) {
+		const rte_be16_t sport = rte_cpu_to_be_16(v6_tbl[i].src_port);
+		const rte_be16_t dport = rte_cpu_to_be_16(v6_tbl[i].dst_port);
+
+		v6_packet.ip.src_addr = v6_tbl[i].src_ip;
+		v6_packet.ip.dst_addr = v6_tbl[i].dst_ip;
+		memcpy(v6_packet.ports, &sport, sizeof(sport));
+		memcpy(v6_packet.ports + sizeof(sport), &dport, sizeof(dport));
+		memcpy(split_ports, v6_packet.ports, sizeof(split_ports));
+
+		TEST_ASSERT_SUCCESS(rte_thash_rss_ipv6_l3(ctx, &v6_packet.ip, &hash),
+			"IPv6 L3 hash failed at %u", i);
+		TEST_ASSERT_EQUAL(hash, v6_tbl[i].hash_l3, "IPv6 L3 hash mismatch at %u", i);
+		TEST_ASSERT_SUCCESS(rte_thash_rss_ipv6_l4(ctx, &v6_packet.ip,
+				v6_packet.ports, &hash), "IPv6 adjacent L4 hash failed at %u", i);
+		TEST_ASSERT_EQUAL(hash, v6_tbl[i].hash_l3l4,
+			"IPv6 adjacent L4 hash mismatch at %u", i);
+		TEST_ASSERT_SUCCESS(rte_thash_rss_ipv6_l4(ctx, &v6_packet.ip, split_ports, &hash),
+			"IPv6 split L4 hash failed at %u", i);
+		TEST_ASSERT_EQUAL(hash, v6_tbl[i].hash_l3l4,
+			"IPv6 split L4 hash mismatch at %u", i);
+	}
+
+	TEST_ASSERT_EQUAL(rte_thash_rss_ipv4_l3(NULL, &v4_packet.ip, &hash), -EINVAL,
+		"IPv4 L3 accepted NULL context");
+	TEST_ASSERT_EQUAL(rte_thash_rss_ipv4_l3(ctx, NULL, &hash), -EINVAL,
+		"IPv4 L3 accepted NULL header");
+	TEST_ASSERT_EQUAL(rte_thash_rss_ipv4_l3(ctx, &v4_packet.ip, NULL), -EINVAL,
+		"IPv4 L3 accepted NULL result");
+	TEST_ASSERT_EQUAL(rte_thash_rss_ipv6_l3(NULL, &v6_packet.ip, &hash), -EINVAL,
+		"IPv6 L3 accepted NULL context");
+	TEST_ASSERT_EQUAL(rte_thash_rss_ipv6_l3(ctx, NULL, &hash), -EINVAL,
+		"IPv6 L3 accepted NULL header");
+	TEST_ASSERT_EQUAL(rte_thash_rss_ipv6_l3(ctx, &v6_packet.ip, NULL), -EINVAL,
+		"IPv6 L3 accepted NULL result");
+	TEST_ASSERT_EQUAL(rte_thash_rss_ipv4_l4(NULL, &v4_packet.ip, split_ports, &hash),
+		-EINVAL, "IPv4 L4 accepted NULL context");
+	TEST_ASSERT_EQUAL(rte_thash_rss_ipv4_l4(ctx, NULL, split_ports, &hash),
+		-EINVAL, "IPv4 L4 accepted NULL header");
+	TEST_ASSERT_EQUAL(rte_thash_rss_ipv4_l4(ctx, &v4_packet.ip, NULL, &hash),
+		-EINVAL, "IPv4 L4 accepted NULL ports");
+	TEST_ASSERT_EQUAL(rte_thash_rss_ipv4_l4(ctx, &v4_packet.ip, split_ports, NULL),
+		-EINVAL, "IPv4 L4 accepted NULL result");
+	TEST_ASSERT_EQUAL(rte_thash_rss_ipv6_l4(NULL, &v6_packet.ip, split_ports, &hash),
+		-EINVAL, "IPv6 L4 accepted NULL context");
+	TEST_ASSERT_EQUAL(rte_thash_rss_ipv6_l4(ctx, NULL, split_ports, &hash),
+		-EINVAL, "IPv6 L4 accepted NULL header");
+	TEST_ASSERT_EQUAL(rte_thash_rss_ipv6_l4(ctx, &v6_packet.ip, NULL, &hash),
+		-EINVAL, "IPv6 L4 accepted NULL ports");
+	TEST_ASSERT_EQUAL(rte_thash_rss_ipv6_l4(ctx, &v6_packet.ip, split_ports, NULL),
+		-EINVAL, "IPv6 L4 accepted NULL result");
+
+	return TEST_SUCCESS;
+}
+
+static int
+test_rss_packet_hashes(void)
+{
+	const uint16_t original_bitwidth = rte_vect_get_max_simd_bitwidth();
+	const uint16_t bitwidths[] = { RTE_VECT_SIMD_DISABLED, RTE_VECT_SIMD_512 };
+	bool bitwidth_changed = false;
+	int ret = TEST_SUCCESS;
+
+	for (unsigned int i = 0; i < RTE_DIM(bitwidths); i++) {
+		const bool forced = rte_vect_set_max_simd_bitwidth(bitwidths[i]) != 0;
+		struct rte_thash_rss_ctx *ctx = rte_thash_rss_ctx_create(default_rss_key,
+				sizeof(default_rss_key), 0);
+
+		if (!forced)
+			bitwidth_changed = true;
+		if (ctx == NULL)
+			ret = -TEST_FAILED;
+		else {
+			ret = check_rss_packet_hashes(ctx);
+			rte_thash_rss_ctx_free(ctx);
+		}
+		if (ret != TEST_SUCCESS || forced)
+			break;
+		printf("Checked RSS packet hashes with SIMD bitwidth: %u\n", bitwidths[i]);
+	}
+
+	if (bitwidth_changed)
+		rte_vect_set_max_simd_bitwidth(original_bitwidth);
+	return ret;
+}
+
 static int
 test_create_invalid(void)
 {
@@ -1075,6 +1203,7 @@ static struct unit_test_suite thash_tests = {
 	TEST_CASE(test_toeplitz_hash_gfni_bulk),
 	TEST_CASE(test_big_tuple_gfni),
 	TEST_CASE(test_rss_ctx_create_free),
+	TEST_CASE(test_rss_packet_hashes),
 	TEST_CASE(test_create_invalid),
 	TEST_CASE(test_multiple_create),
 	TEST_CASE(test_free_null),
diff --git a/lib/hash/thash_gfni_avx512.c b/lib/hash/thash_gfni_avx512.c
index 3804341f7b..e738812807 100644
--- a/lib/hash/thash_gfni_avx512.c
+++ b/lib/hash/thash_gfni_avx512.c
@@ -2,6 +2,9 @@
  * Copyright(c) 2026 Intel Corporation
  */
 
+#include <stdalign.h>
+#include <string.h>
+
 #include <rte_thash_x86_gfni.h>
 
 #include "thash_private.h"
@@ -33,3 +36,33 @@ thash_gfni_avx512_36B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple)
 {
 	return rte_thash_gfni(ctx->data.matrices, tuple, 36);
 }
+
+uint32_t
+thash_gfni_avx512_12B_split(const struct rte_thash_rss_ctx *ctx,
+	const uint8_t *addresses, const uint8_t *ports)
+{
+	alignas(64) uint8_t tuple[64];
+	uint32_t ports_value;
+
+	memcpy(&ports_value, ports, sizeof(ports_value));
+	__m512i value = _mm512_maskz_loadu_epi8(UINT64_C(0xff), addresses);
+	value = _mm512_mask_set1_epi32(value, UINT16_C(1) << 2, ports_value);
+	_mm512_store_si512(tuple, value);
+
+	return rte_thash_gfni(ctx->data.matrices, tuple, 12);
+}
+
+uint32_t
+thash_gfni_avx512_36B_split(const struct rte_thash_rss_ctx *ctx,
+	const uint8_t *addresses, const uint8_t *ports)
+{
+	alignas(64) uint8_t tuple[64];
+	uint32_t ports_value;
+
+	memcpy(&ports_value, ports, sizeof(ports_value));
+	__m512i value = _mm512_maskz_loadu_epi8(UINT64_C(0xffffffff), addresses);
+	value = _mm512_mask_set1_epi32(value, UINT16_C(1) << 8, ports_value);
+	_mm512_store_si512(tuple, value);
+
+	return rte_thash_gfni(ctx->data.matrices, tuple, 36);
+}
diff --git a/lib/hash/thash_private.h b/lib/hash/thash_private.h
index 2860866c4a..a7a1b0148f 100644
--- a/lib/hash/thash_private.h
+++ b/lib/hash/thash_private.h
@@ -13,12 +13,16 @@
 struct rte_thash_rss_ctx;
 
 typedef uint32_t (*thash_fn_t)(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple);
+typedef uint32_t (*thash_split_fn_t)(const struct rte_thash_rss_ctx *ctx,
+	const uint8_t *addresses, const uint8_t *ports);
 
 struct thash_rss_ops {
 	thash_fn_t hash_8;
 	thash_fn_t hash_12;
 	thash_fn_t hash_32;
 	thash_fn_t hash_36;
+	thash_split_fn_t hash_12_split;
+	thash_split_fn_t hash_36_split;
 };
 
 struct rte_thash_rss_ctx {
@@ -47,6 +51,14 @@ thash_gfni_avx512_32B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple)
 uint32_t
 thash_gfni_avx512_36B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple);
 
+uint32_t
+thash_gfni_avx512_12B_split(const struct rte_thash_rss_ctx *ctx, const uint8_t *addresses,
+		const uint8_t *ports);
+
+uint32_t
+thash_gfni_avx512_36B_split(const struct rte_thash_rss_ctx *ctx, const uint8_t *addresses,
+		const uint8_t *ports);
+
 uint32_t
 thash_scalar_8B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple);
 
@@ -59,6 +71,14 @@ thash_scalar_32B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple);
 uint32_t
 thash_scalar_36B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple);
 
+uint32_t
+thash_scalar_12B_split(const struct rte_thash_rss_ctx *ctx, const uint8_t *addresses,
+		const uint8_t *ports);
+
+uint32_t
+thash_scalar_36B_split(const struct rte_thash_rss_ctx *ctx, const uint8_t *addresses,
+		const uint8_t *ports);
+
 bool
 thash_gfni_avx512_usable(void);
 
diff --git a/lib/hash/thash_rss.c b/lib/hash/thash_rss.c
index ddc07720a3..95fecd6d92 100644
--- a/lib/hash/thash_rss.c
+++ b/lib/hash/thash_rss.c
@@ -53,11 +53,35 @@ thash_scalar_36B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple)
 	return thash_scalar((const uint8_t *)ctx->data.key, tuple, 36);
 }
 
+uint32_t
+thash_scalar_12B_split(const struct rte_thash_rss_ctx *ctx,
+	const uint8_t *addresses, const uint8_t *ports)
+{
+	uint8_t tuple[12];
+
+	memcpy(tuple, addresses, 8);
+	memcpy(&tuple[8], ports, 4);
+	return thash_scalar((const uint8_t *)ctx->data.key, tuple, 12);
+}
+
+uint32_t
+thash_scalar_36B_split(const struct rte_thash_rss_ctx *ctx,
+	const uint8_t *addresses, const uint8_t *ports)
+{
+	uint8_t tuple[36];
+
+	memcpy(tuple, addresses, 32);
+	memcpy(&tuple[32], ports, 4);
+	return thash_scalar((const uint8_t *)ctx->data.key, tuple, 36);
+}
+
 static const struct thash_rss_ops scalar_ops = {
 	.hash_8 = thash_scalar_8B,
 	.hash_12 = thash_scalar_12B,
 	.hash_32 = thash_scalar_32B,
 	.hash_36 = thash_scalar_36B,
+	.hash_12_split = thash_scalar_12B_split,
+	.hash_36_split = thash_scalar_36B_split,
 };
 
 #ifdef CC_THASH_GFNI_AVX512_SUPPORT
@@ -66,6 +90,8 @@ static const struct thash_rss_ops gfni_avx512_ops = {
 	.hash_12 = thash_gfni_avx512_12B,
 	.hash_32 = thash_gfni_avx512_32B,
 	.hash_36 = thash_gfni_avx512_36B,
+	.hash_12_split = thash_gfni_avx512_12B_split,
+	.hash_36_split = thash_gfni_avx512_36B_split,
 };
 #endif
 
@@ -130,17 +156,6 @@ rte_thash_rss_ipv6_l3(const struct rte_thash_rss_ctx *ctx,
 	return 0;
 }
 
-static __rte_noinline uint32_t
-thash_ipv4_l4_gather(const struct rte_thash_rss_ctx *ctx,
-	const struct rte_ipv4_hdr *ip, const void *l4_hdr)
-{
-	uint8_t tuple[12];
-
-	memcpy(tuple, &ip->src_addr, 2 * sizeof(ip->src_addr));
-	memcpy(&tuple[8], l4_hdr, 4);
-	return ctx->ops->hash_12(ctx, tuple);
-}
-
 RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_thash_rss_ipv4_l4, 27.03)
 int
 rte_thash_rss_ipv4_l4(const struct rte_thash_rss_ctx *ctx,
@@ -152,22 +167,11 @@ rte_thash_rss_ipv4_l4(const struct rte_thash_rss_ctx *ctx,
 	if (l4_hdr == ip + 1)
 		*hash = ctx->ops->hash_12(ctx, (const uint8_t *)&ip->src_addr);
 	else
-		*hash = thash_ipv4_l4_gather(ctx, ip, l4_hdr);
+		*hash = ctx->ops->hash_12_split(ctx, (const uint8_t *)&ip->src_addr, l4_hdr);
 
 	return 0;
 }
 
-static __rte_noinline uint32_t
-thash_ipv6_l4_gather(const struct rte_thash_rss_ctx *ctx,
-	const struct rte_ipv6_hdr *ip, const void *l4_hdr)
-{
-	uint8_t tuple[36];
-
-	memcpy(tuple, &ip->src_addr, 2 * sizeof(ip->src_addr));
-	memcpy(&tuple[32], l4_hdr, 4);
-	return ctx->ops->hash_36(ctx, tuple);
-}
-
 RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_thash_rss_ipv6_l4, 27.03)
 int
 rte_thash_rss_ipv6_l4(const struct rte_thash_rss_ctx *ctx,
@@ -179,7 +183,7 @@ rte_thash_rss_ipv6_l4(const struct rte_thash_rss_ctx *ctx,
 	if (l4_hdr == ip + 1)
 		*hash = ctx->ops->hash_36(ctx, (const uint8_t *)&ip->src_addr);
 	else
-		*hash = thash_ipv6_l4_gather(ctx, ip, l4_hdr);
+		*hash = ctx->ops->hash_36_split(ctx, (const uint8_t *)&ip->src_addr, l4_hdr);
 
 	return 0;
 }
-- 
2.55.0


  parent reply	other threads:[~2026-12-01 15:17 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 15:16 [PATCH 00/11] Add optimized path selection for thash Bruce Richardson
2026-12-01 15:16 ` [PATCH 01/11] hash: improve build-time instruction set checks " Bruce Richardson
2026-12-01 15:16 ` [PATCH 02/11] test/thash_perf: test gfni thash when platform supports Bruce Richardson
2026-12-01 15:16 ` [PATCH 03/11] hash: create library internal header file for thash Bruce Richardson
2026-12-01 15:16 ` [PATCH 04/11] hash: add functions for GFNI RSS hash calculations Bruce Richardson
2026-12-01 15:16 ` [PATCH 05/11] hash: add fixed-size scalar " Bruce Richardson
2026-12-01 15:16 ` [PATCH 06/11] hash: add thash context struct for runtime path selection Bruce Richardson
2026-12-01 15:16 ` [PATCH 07/11] hash: implement IPv4 and IPv6 RSS hashing in SW Bruce Richardson
2026-12-01 15:16 ` Bruce Richardson [this message]
2026-12-01 15:16 ` [PATCH 09/11] test/thash: add perf tests for new RSS functions Bruce Richardson
2026-12-01 15:16 ` [PATCH 10/11] hash: remove extra store-load when performing RSS Bruce Richardson
2026-12-01 15:16 ` [PATCH 11/11] hash: add AVX2 RSS hash functions Bruce Richardson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201151623.2240031-9-bruce.richardson@intel.com \
    --to=bruce.richardson@intel.com \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox