From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BCE22606A320 for ; Tue, 1 Dec 2026 15:17:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=nHCDed+M Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 7933E40678; Tue, 1 Dec 2026 16:16:53 +0100 (CET) Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) by mails.dpdk.org (Postfix) with ESMTPS id 3F3F4402D9 for ; Tue, 1 Dec 2026 16:16:48 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796138209; x=1827674209; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=NE4p0ESuKBFLWQEYaww0Ot/BY2XtpjKSw4jyqmZXAYc=; b=nHCDed+MUkTqAy8GLN7TG70VWsaZZlxKRXCddMT0416p/glaUMq3iJGg cODGIbT4sCAky7I89bdJZ9NZTY5CH9vVcM/NsoMY1CNbmQ717iCAJbl8o mnb9sIK/Z78n1k2ywg2BtofJxwyISJJbJEZ1bxz3F6jDsV8G0qtcXdVqQ 81lLMKoqBduc39jfDxkGHKug7aiDEo+qG0/67A7hJ9D9IARhCsWMsEGd/ ptuEqXnUnxKyxKUFpew+t3crc5n6GBJ3mmLkQE+D5dthZhQkgwBKF2OAM NviVi9B6RP19m8qhRjzy5tq1/d59tjraBlQBOqU2P8yrzw4Yxj0n7/CKO A==; X-CSE-ConnectionGUID: hbv6g4P0ShGyQEtwVNsIqQ== X-CSE-MsgGUID: B/XBgnjaRamP50SOSQWvsA== X-IronPort-AV: E=McAfee;i="6800,10657,11983"; a="5023492" X-IronPort-AV: E=Sophos;i="6.27,245,1787036400"; d="scan'208";a="5023492" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 07:16:48 -0800 X-CSE-ConnectionGUID: nuhQnDdrQbWMw17b/quMHQ== X-CSE-MsgGUID: foV6/NMPQuCFx98Y4h00kw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,245,1787036400"; d="scan'208";a="13749405" Received: from silpixa00401385.ir.intel.com ([10.20.224.226]) by fmviesa001.fm.intel.com with ESMTP; 01 Dec 2026 07:16:46 -0800 From: Bruce Richardson To: dev@dpdk.org Cc: Bruce Richardson Subject: [PATCH 06/11] hash: add thash context struct for runtime path selection Date: Tue, 1 Dec 2026 15:16:18 +0000 Message-ID: <20261201151623.2240031-7-bruce.richardson@intel.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261201151623.2240031-1-bruce.richardson@intel.com> References: <20261201151623.2240031-1-bruce.richardson@intel.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Archive: List-Post: List-Help: List-Subscribe: , X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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 --- 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 #include +#include #include #include #include @@ -12,6 +13,7 @@ #include "test.h" #include +#include #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 + +#include + +#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 #include +#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 +#include #include +#include #include +#include #include +#include #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