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 06/11] hash: add thash context struct for runtime path selection
Date: Tue,  1 Dec 2026 15:16:18 +0000	[thread overview]
Message-ID: <20261201151623.2240031-7-bruce.richardson@intel.com> (raw)
In-Reply-To: <20261201151623.2240031-1-bruce.richardson@intel.com>

Add a context structure to hold function pointers and appropriate key
structure data for use of either scalar or AVX-512 GFNI RSS
calculations. The context is kept opaque from applications with
lifecycle management through APIs and path selection done at context
creation time.

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
 app/test/test_thash.c        | 37 ++++++++++++++++++
 lib/hash/meson.build         |  2 +
 lib/hash/rte_thash.c         | 23 +++++++----
 lib/hash/rte_thash_rss.h     | 69 +++++++++++++++++++++++++++++++++
 lib/hash/thash_gfni_avx512.c | 16 ++++----
 lib/hash/thash_private.h     | 41 ++++++++++++++++----
 lib/hash/thash_rss.c         | 74 ++++++++++++++++++++++++++++++++----
 7 files changed, 231 insertions(+), 31 deletions(-)
 create mode 100644 lib/hash/rte_thash_rss.h

diff --git a/app/test/test_thash.c b/app/test/test_thash.c
index eeb2e87f2c..413292943d 100644
--- a/app/test/test_thash.c
+++ b/app/test/test_thash.c
@@ -4,6 +4,7 @@
 
 #include <rte_common.h>
 #include <rte_eal.h>
+#include <rte_errno.h>
 #include <rte_ip.h>
 #include <rte_random.h>
 #include <rte_malloc.h>
@@ -12,6 +13,7 @@
 #include "test.h"
 
 #include <rte_thash.h>
+#include <rte_thash_rss.h>
 
 #define HASH_MSK(reta_sz)	((1 << reta_sz) - 1)
 #define TUPLE_SZ	(RTE_THASH_V4_L4_LEN * 4)
@@ -366,6 +368,40 @@ test_big_tuple_gfni(void)
 	return TEST_SUCCESS;
 }
 
+static int
+test_rss_ctx_create_free(void)
+{
+	struct rte_thash_rss_ctx *ctx1;
+	struct rte_thash_rss_ctx *ctx2;
+
+	ctx1 = rte_thash_rss_ctx_create(NULL, sizeof(default_rss_key), 0);
+	TEST_ASSERT_NULL(ctx1, "Created context with NULL key");
+	TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong rte_errno for NULL key");
+
+	ctx1 = rte_thash_rss_ctx_create(default_rss_key, sizeof(default_rss_key) - 1, 0);
+	TEST_ASSERT_NULL(ctx1, "Created context with short key");
+	TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong rte_errno for short key");
+
+	ctx1 = rte_thash_rss_ctx_create(default_rss_key, sizeof(default_rss_key), 1);
+	TEST_ASSERT_NULL(ctx1, "Created context with non-zero flags");
+	TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong rte_errno for non-zero flags");
+
+	ctx1 = rte_thash_rss_ctx_create(default_rss_key, sizeof(default_rss_key), 0);
+	TEST_ASSERT_NOT_NULL(ctx1, "Failed to create context");
+
+	ctx2 = rte_thash_rss_ctx_create(big_rss_key, sizeof(big_rss_key), 0);
+	if (ctx2 == NULL)
+		rte_thash_rss_ctx_free(ctx1);
+	TEST_ASSERT_NOT_NULL(ctx2, "Failed to create context with long key");
+	TEST_ASSERT(ctx1 != ctx2, "Created contexts have the same address");
+
+	rte_thash_rss_ctx_free(ctx1);
+	rte_thash_rss_ctx_free(ctx2);
+	rte_thash_rss_ctx_free(NULL);
+
+	return TEST_SUCCESS;
+}
+
 static int
 test_create_invalid(void)
 {
@@ -1038,6 +1074,7 @@ static struct unit_test_suite thash_tests = {
 	TEST_CASE(test_toeplitz_hash_rand_data),
 	TEST_CASE(test_toeplitz_hash_gfni_bulk),
 	TEST_CASE(test_big_tuple_gfni),
+	TEST_CASE(test_rss_ctx_create_free),
 	TEST_CASE(test_create_invalid),
 	TEST_CASE(test_multiple_create),
 	TEST_CASE(test_free_null),
diff --git a/lib/hash/meson.build b/lib/hash/meson.build
index abee43fa2a..72fb7efd8d 100644
--- a/lib/hash/meson.build
+++ b/lib/hash/meson.build
@@ -8,6 +8,7 @@ headers = files(
         'rte_jhash.h',
         'rte_thash.h',
         'rte_thash_gfni.h',
+        'rte_thash_rss.h',
 )
 indirect_headers += files(
         'rte_crc_arm64.h',
@@ -36,5 +37,6 @@ if dpdk_conf.has('RTE_ARCH_X86') and cc_has_avx512
         if cc.has_multi_arguments(thash_gfni_cflags)
                 sources_avx512 += files('thash_gfni_avx512.c')
                 cflags_avx512 += thash_gfni_cflags
+                cflags += '-DCC_THASH_GFNI_AVX512_SUPPORT'
         endif
 endif
diff --git a/lib/hash/rte_thash.c b/lib/hash/rte_thash.c
index dfbcb931d9..f544cc0d69 100644
--- a/lib/hash/rte_thash.c
+++ b/lib/hash/rte_thash.c
@@ -74,19 +74,28 @@ struct rte_thash_ctx {
 	uint8_t		hash_key[];
 };
 
+bool
+thash_gfni_avx512_usable(void)
+{
+#ifdef RTE_ARCH_X86
+	return rte_cpu_get_flag_enabled(RTE_CPUFLAG_GFNI) > 0 &&
+			rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512VBMI) > 0 &&
+			rte_vect_get_max_simd_bitwidth() >= RTE_VECT_SIMD_512;
+#else
+	return false;
+#endif
+}
+
 RTE_EXPORT_SYMBOL(rte_thash_gfni_supported)
 int
 rte_thash_gfni_supported(void)
 {
 #ifdef RTE_THASH_GFNI_DEFINED
-	if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_GFNI) &&
-			(rte_vect_get_max_simd_bitwidth() >=
-			RTE_VECT_SIMD_512))
-		return 1;
-#endif
-
+	return thash_gfni_avx512_usable();
+#else
 	return 0;
-};
+#endif
+}
 
 RTE_EXPORT_SYMBOL(rte_thash_complete_matrix)
 void
diff --git a/lib/hash/rte_thash_rss.h b/lib/hash/rte_thash_rss.h
new file mode 100644
index 0000000000..f76dd667fb
--- /dev/null
+++ b/lib/hash/rte_thash_rss.h
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2026 Intel Corporation
+ */
+
+#ifndef RTE_THASH_RSS_H
+#define RTE_THASH_RSS_H
+
+/**
+ * @file
+ *
+ * RSS hash calculation using the fastest implementation available.
+ */
+
+#include <stdint.h>
+
+#include <rte_compat.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** Opaque context for RSS hash calculation. */
+struct rte_thash_rss_ctx;
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Create an RSS hash context.
+ *
+ * The context selects the fastest available implementation,
+ * subject to the maximum SIMD bitwidth configured in EAL.
+ * A context is local to the process that creates it and must not be shared
+ * with other processes.
+ *
+ * @param key
+ *  Pointer to the RSS hash key, as programmed into the NIC.
+ * @param key_len
+ *  Length of the key in bytes. Must be at least 40 bytes.
+ *  Bytes beyond the first 40 are ignored.
+ * @param flags
+ *  Reserved for future use; must be zero.
+ * @return
+ *  Pointer to the new context on success.
+ *  NULL on error, with rte_errno set to EINVAL for invalid parameters
+ *  or ENOMEM if memory allocation failed.
+ */
+__rte_experimental
+struct rte_thash_rss_ctx *
+rte_thash_rss_ctx_create(const uint8_t *key, uint32_t key_len, uint64_t flags);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Free an RSS hash context.
+ *
+ * @param ctx
+ *  Context to free. If NULL, no operation is performed.
+ */
+__rte_experimental
+void
+rte_thash_rss_ctx_free(struct rte_thash_rss_ctx *ctx);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* RTE_THASH_RSS_H */
diff --git a/lib/hash/thash_gfni_avx512.c b/lib/hash/thash_gfni_avx512.c
index d62b5acc1f..3804341f7b 100644
--- a/lib/hash/thash_gfni_avx512.c
+++ b/lib/hash/thash_gfni_avx512.c
@@ -11,25 +11,25 @@
 #endif
 
 uint32_t
-thash_gfni_avx512_8B(const uint64_t *mtrx, const uint8_t *tuple)
+thash_gfni_avx512_8B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple)
 {
-	return rte_thash_gfni(mtrx, tuple, 8);
+	return rte_thash_gfni(ctx->data.matrices, tuple, 8);
 }
 
 uint32_t
-thash_gfni_avx512_12B(const uint64_t *mtrx, const uint8_t *tuple)
+thash_gfni_avx512_12B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple)
 {
-	return rte_thash_gfni(mtrx, tuple, 12);
+	return rte_thash_gfni(ctx->data.matrices, tuple, 12);
 }
 
 uint32_t
-thash_gfni_avx512_32B(const uint64_t *mtrx, const uint8_t *tuple)
+thash_gfni_avx512_32B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple)
 {
-	return rte_thash_gfni(mtrx, tuple, 32);
+	return rte_thash_gfni(ctx->data.matrices, tuple, 32);
 }
 
 uint32_t
-thash_gfni_avx512_36B(const uint64_t *mtrx, const uint8_t *tuple)
+thash_gfni_avx512_36B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple)
 {
-	return rte_thash_gfni(mtrx, tuple, 36);
+	return rte_thash_gfni(ctx->data.matrices, tuple, 36);
 }
diff --git a/lib/hash/thash_private.h b/lib/hash/thash_private.h
index f9187536aa..2860866c4a 100644
--- a/lib/hash/thash_private.h
+++ b/lib/hash/thash_private.h
@@ -5,8 +5,30 @@
 #ifndef THASH_PRIVATE_H
 #define THASH_PRIVATE_H
 
+#include <stdbool.h>
 #include <stdint.h>
 
+#define THASH_RSS_KEY_LEN 40
+
+struct rte_thash_rss_ctx;
+
+typedef uint32_t (*thash_fn_t)(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple);
+
+struct thash_rss_ops {
+	thash_fn_t hash_8;
+	thash_fn_t hash_12;
+	thash_fn_t hash_32;
+	thash_fn_t hash_36;
+};
+
+struct rte_thash_rss_ctx {
+	union {
+		uint32_t key[THASH_RSS_KEY_LEN / sizeof(uint32_t)];
+		uint64_t matrices[THASH_RSS_KEY_LEN];
+	} data;
+	const struct thash_rss_ops *ops;
+};
+
 /**
  * Get a random polynomial of the specified degree.
  */
@@ -14,27 +36,30 @@ uint32_t
 thash_get_rand_poly(uint32_t poly_degree);
 
 uint32_t
-thash_gfni_avx512_8B(const uint64_t *mtrx, const uint8_t *tuple);
+thash_gfni_avx512_8B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple);
 
 uint32_t
-thash_gfni_avx512_12B(const uint64_t *mtrx, const uint8_t *tuple);
+thash_gfni_avx512_12B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple);
 
 uint32_t
-thash_gfni_avx512_32B(const uint64_t *mtrx, const uint8_t *tuple);
+thash_gfni_avx512_32B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple);
 
 uint32_t
-thash_gfni_avx512_36B(const uint64_t *mtrx, const uint8_t *tuple);
+thash_gfni_avx512_36B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple);
 
 uint32_t
-thash_scalar_8B(const uint8_t *key, const uint8_t *tuple);
+thash_scalar_8B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple);
 
 uint32_t
-thash_scalar_12B(const uint8_t *key, const uint8_t *tuple);
+thash_scalar_12B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple);
 
 uint32_t
-thash_scalar_32B(const uint8_t *key, const uint8_t *tuple);
+thash_scalar_32B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple);
 
 uint32_t
-thash_scalar_36B(const uint8_t *key, const uint8_t *tuple);
+thash_scalar_36B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple);
+
+bool
+thash_gfni_avx512_usable(void);
 
 #endif /* THASH_PRIVATE_H */
diff --git a/lib/hash/thash_rss.c b/lib/hash/thash_rss.c
index 65c109e1b0..c53e9dbefa 100644
--- a/lib/hash/thash_rss.c
+++ b/lib/hash/thash_rss.c
@@ -2,10 +2,15 @@
  * Copyright(c) 2026 Intel Corporation
  */
 
+#include <errno.h>
+#include <stdlib.h>
 #include <string.h>
 
+#include <eal_export.h>
 #include <rte_byteorder.h>
+#include <rte_errno.h>
 #include <rte_thash.h>
+#include <rte_thash_rss.h>
 
 #include "thash_private.h"
 
@@ -25,25 +30,78 @@ thash_scalar(const uint8_t *key, const uint8_t *tuple, uint32_t len)
 }
 
 uint32_t
-thash_scalar_8B(const uint8_t *key, const uint8_t *tuple)
+thash_scalar_8B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple)
 {
-	return thash_scalar(key, tuple, 8);
+	return thash_scalar((const uint8_t *)ctx->data.key, tuple, 8);
 }
 
 uint32_t
-thash_scalar_12B(const uint8_t *key, const uint8_t *tuple)
+thash_scalar_12B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple)
 {
-	return thash_scalar(key, tuple, 12);
+	return thash_scalar((const uint8_t *)ctx->data.key, tuple, 12);
 }
 
 uint32_t
-thash_scalar_32B(const uint8_t *key, const uint8_t *tuple)
+thash_scalar_32B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple)
 {
-	return thash_scalar(key, tuple, 32);
+	return thash_scalar((const uint8_t *)ctx->data.key, tuple, 32);
 }
 
 uint32_t
-thash_scalar_36B(const uint8_t *key, const uint8_t *tuple)
+thash_scalar_36B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple)
 {
-	return thash_scalar(key, tuple, 36);
+	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,
+};
+
+#ifdef CC_THASH_GFNI_AVX512_SUPPORT
+static const struct thash_rss_ops gfni_avx512_ops = {
+	.hash_8 = thash_gfni_avx512_8B,
+	.hash_12 = thash_gfni_avx512_12B,
+	.hash_32 = thash_gfni_avx512_32B,
+	.hash_36 = thash_gfni_avx512_36B,
+};
+#endif
+
+RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_thash_rss_ctx_create, 27.03)
+struct rte_thash_rss_ctx *
+rte_thash_rss_ctx_create(const uint8_t *key, uint32_t key_len, uint64_t flags)
+{
+	if (key == NULL || key_len < THASH_RSS_KEY_LEN || flags != 0) {
+		rte_errno = EINVAL;
+		return NULL;
+	}
+
+	struct rte_thash_rss_ctx *ctx = malloc(sizeof(*ctx));
+	if (ctx == NULL) {
+		rte_errno = ENOMEM;
+		return NULL;
+	}
+
+#ifdef CC_THASH_GFNI_AVX512_SUPPORT
+	if (thash_gfni_avx512_usable()) {
+		ctx->ops = &gfni_avx512_ops;
+		rte_thash_complete_matrix(ctx->data.matrices, key, THASH_RSS_KEY_LEN);
+		return ctx;
+	}
+#endif
+
+	ctx->ops = &scalar_ops;
+	memcpy(ctx->data.key, key, THASH_RSS_KEY_LEN);
+	rte_convert_rss_key(ctx->data.key, ctx->data.key, THASH_RSS_KEY_LEN);
+
+	return ctx;
+}
+
+RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_thash_rss_ctx_free, 27.03)
+void
+rte_thash_rss_ctx_free(struct rte_thash_rss_ctx *ctx)
+{
+	free(ctx);
 }
-- 
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 ` Bruce Richardson [this message]
2026-12-01 15:16 ` [PATCH 07/11] hash: implement IPv4 and IPv6 RSS hashing in SW Bruce Richardson
2026-12-01 15:16 ` [PATCH 08/11] hash: add explicit handling for RSS with split L3/L4 data Bruce Richardson
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-7-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