hash: fix thash lfsr initialization

Message ID 20240906170237.1324060-1-vladimir.medvedkin@intel.com (mailing list archive)
State New
Delegated to: David Marchand
Headers
Series hash: fix thash lfsr initialization |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/github-robot: build success github build: passed
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/intel-Functional success Functional PASS
ci/iol-sample-apps-testing success Testing PASS
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-compile-amd64-testing success Testing PASS
ci/iol-compile-arm64-testing success Testing PASS
ci/iol-broadcom-Functional success Functional Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-marvell-Functional success Functional Testing PASS

Commit Message

Vladimir Medvedkin Sept. 6, 2024, 5:01 p.m. UTC
Reverse polynomial for an LFSR was initialized improperly which
could generate improper bit sequence in some situations.
This patch implements proper polynomial reversing function.

Fixes: 28ebff11c2dc ("hash: add predictable RSS")
Cc: stable@dpdk.org

Signed-off-by: Vladimir Medvedkin <vladimir.medvedkin@intel.com>
---
 lib/hash/rte_thash.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
  

Patch

diff --git a/lib/hash/rte_thash.c b/lib/hash/rte_thash.c
index 10721effe6..99a685f0c8 100644
--- a/lib/hash/rte_thash.c
+++ b/lib/hash/rte_thash.c
@@ -166,6 +166,30 @@  thash_get_rand_poly(uint32_t poly_degree)
 		RTE_DIM(irreducible_poly_table[poly_degree])];
 }
 
+static inline uint32_t
+get_rev_poly(uint32_t poly, int degree)
+{
+	int i;
+	/*
+	 * The implicit highest coefficient of the polynomial
+	 * becomes the lowest after reversal.
+	 */
+	uint32_t rev_poly = 1;
+	uint32_t mask = (1 << degree) - 1;
+
+	/*
+	 * Here we assume "poly" argument is an irreducible polynomial,
+	 * thus the lowest coefficient of the "poly" must always be equal to "1".
+	 * After the reversal, this the lowest coefficient becomes the highest and
+	 * it is omitted since the highest coefficient is implicitly determined by
+	 * degree of the polynomial.
+	 */
+	for (i = 1; i < degree; i++)
+		rev_poly |= ((poly >> i) & 0x1) << (degree - i);
+
+	return rev_poly & mask;
+}
+
 static struct thash_lfsr *
 alloc_lfsr(struct rte_thash_ctx *ctx)
 {
@@ -185,7 +209,7 @@  alloc_lfsr(struct rte_thash_ctx *ctx)
 		lfsr->state = rte_rand() & ((1 << lfsr->deg) - 1);
 	} while (lfsr->state == 0);
 	/* init reverse order polynomial */
-	lfsr->rev_poly = (lfsr->poly >> 1) | (1 << (lfsr->deg - 1));
+	lfsr->rev_poly = get_rev_poly(lfsr->poly, lfsr->deg);
 	/* init proper rev_state*/
 	lfsr->rev_state = lfsr->state;
 	for (i = 0; i <= lfsr->deg; i++)