hash: fix thash lfsr initialization
Checks
Commit Message
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(-)
@@ -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++)