eal: replace libc-based random generation with LFSR

This commit replaces rte_rand()'s use of lrand48() with a DPDK-native
combined Linear Feedback Shift Register (LFSR) (also known as
Tausworthe) pseudo-random number generator.

This generator is faster and produces better-quality random numbers
than the linear congruential generator (LCG) of lib's lrand48(). The
implementation, as opposed to lrand48(), is multi-thread safe in
regards to concurrent rte_rand() calls from different lcore threads.
A LCG is still used, but only to seed the five per-lcore LFSR
sequences.

In addition, this patch also addresses the issue of the legacy
implementation only producing 62 bits of pseudo randomness, while the
API requires all 64 bits to be random.

This pseudo-random number generator is not cryptographically secure -
just like lrand48().

Bugzilla ID: 114
Bugzilla ID: 276

Signed-off-by: Mattias Rönnblom <mattias.ronnblom@ericsson.com>
Acked-by: Bruce Richardson <bruce.richardson@intel.com>
This commit is contained in:
Mattias Rönnblom 2019-06-28 11:01:20 +02:00 committed by Thomas Monjalon
parent 609e79841f
commit 3f002f0696
13 changed files with 249 additions and 22 deletions

View file

@ -227,6 +227,12 @@ M: Joyce Kong <joyce.kong@arm.com>
F: lib/librte_eal/common/include/generic/rte_ticketlock.h
F: app/test/test_ticketlock.c
Pseudo-random Number Generation
M: Mattias Rönnblom <mattias.ronnblom@ericsson.com>
F: lib/librte_eal/common/include/rte_random.h
F: lib/librte_eal/common/rte_random.c
F: app/test/test_rand_perf.c
ARM v7
M: Jan Viktorin <viktorin@rehivetech.com>
M: Gavin Hu <gavin.hu@arm.com>

View file

@ -73,6 +73,7 @@ SRCS-y += test_reciprocal_division.c
SRCS-y += test_reciprocal_division_perf.c
SRCS-y += test_fbarray.c
SRCS-y += test_external_mem.c
SRCS-y += test_rand_perf.c
SRCS-y += test_ring.c
SRCS-y += test_ring_perf.c

View file

@ -90,6 +90,7 @@ test_sources = files('commands.c',
'test_power_acpi_cpufreq.c',
'test_power_kvm_vm.c',
'test_prefetch.c',
'test_rand_perf.c',
'test_rcu_qsbr.c',
'test_rcu_qsbr_perf.c',
'test_reciprocal_division.c',
@ -256,6 +257,7 @@ perf_test_names = [
'pmd_perf_autotest',
'stack_perf_autotest',
'stack_lf_perf_autotest',
'rand_perf_autotest',
]
driver_test_names = [

75
app/test/test_rand_perf.c Normal file
View file

@ -0,0 +1,75 @@
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2019 Ericsson AB
*/
#include <inttypes.h>
#include <stdio.h>
#include <rte_common.h>
#include <rte_cycles.h>
#include <rte_random.h>
#include "test.h"
static volatile uint64_t vsum;
#define ITERATIONS (100000000)
enum rand_type {
rand_type_64
};
static const char *
rand_type_desc(enum rand_type rand_type)
{
switch (rand_type) {
case rand_type_64:
return "Full 64-bit [rte_rand()]";
default:
return NULL;
}
}
static __rte_always_inline void
test_rand_perf_type(enum rand_type rand_type)
{
uint64_t start;
uint32_t i;
uint64_t end;
uint64_t sum = 0;
uint64_t op_latency;
start = rte_rdtsc();
for (i = 0; i < ITERATIONS; i++) {
switch (rand_type) {
case rand_type_64:
sum += rte_rand();
break;
}
}
end = rte_rdtsc();
/* to avoid an optimizing compiler removing the whole loop */
vsum = sum;
op_latency = (end - start) / ITERATIONS;
printf("%s: %"PRId64" TSC cycles/op\n", rand_type_desc(rand_type),
op_latency);
}
static int
test_rand_perf(void)
{
rte_srand(42);
printf("Pseudo-random number generation latencies:\n");
test_rand_perf_type(rand_type_64);
return 0;
}
REGISTER_TEST_COMMAND(rand_perf_autotest, test_rand_perf);

View file

@ -54,6 +54,16 @@ New Features
Also, make sure to start the actual text at the margin.
=========================================================
* **Updated the EAL Pseudo-random Number Generator.**
The lrand48()-based rte_rand() function is replaced with a
DPDK-native combined Linear Feedback Shift Register (LFSR)
pseudo-random number generator (PRNG).
This new PRNG implementation is multi-thread safe, provides
higher-quality pseudo-random numbers (including full 64 bit
support) and improved performance.
* **Updated the bnxt PMD.**
Updated the bnxt PMD. The major enhancements include:

View file

@ -16,7 +16,6 @@ extern "C" {
#endif
#include <stdint.h>
#include <stdlib.h>
/**
* Seed the pseudo-random generator.
@ -25,34 +24,28 @@ extern "C" {
* value. It may need to be re-seeded by the user with a real random
* value.
*
* This function is not multi-thread safe in regards to other
* rte_srand() calls, nor is it in relation to concurrent rte_rand()
* calls.
*
* @param seedval
* The value of the seed.
*/
static inline void
rte_srand(uint64_t seedval)
{
srand48((long)seedval);
}
void
rte_srand(uint64_t seedval);
/**
* Get a pseudo-random value.
*
* This function generates pseudo-random numbers using the linear
* congruential algorithm and 48-bit integer arithmetic, called twice
* to generate a 64-bit value.
* The generator is not cryptographically secure.
*
* If called from lcore threads, this function is thread-safe.
*
* @return
* A pseudo-random value between 0 and (1<<64)-1.
*/
static inline uint64_t
rte_rand(void)
{
uint64_t val;
val = (uint64_t)lrand48();
val <<= 32;
val += (uint64_t)lrand48();
return val;
}
uint64_t
rte_rand(void);
#ifdef __cplusplus
}

View file

@ -35,6 +35,7 @@ common_sources = files(
'rte_keepalive.c',
'rte_malloc.c',
'rte_option.c',
'rte_random.c',
'rte_reciprocal.c',
'rte_service.c'
)

View file

@ -0,0 +1,139 @@
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2019 Ericsson AB
*/
#include <stdlib.h>
#include <rte_branch_prediction.h>
#include <rte_cycles.h>
#include <rte_eal.h>
#include <rte_lcore.h>
#include <rte_memory.h>
#include <rte_random.h>
struct rte_rand_state {
uint64_t z1;
uint64_t z2;
uint64_t z3;
uint64_t z4;
uint64_t z5;
} __rte_cache_aligned;
static struct rte_rand_state rand_states[RTE_MAX_LCORE];
static uint32_t
__rte_rand_lcg32(uint32_t *seed)
{
*seed = 1103515245U * *seed + 12345U;
return *seed;
}
static uint64_t
__rte_rand_lcg64(uint32_t *seed)
{
uint64_t low;
uint64_t high;
/* A 64-bit LCG would have been much cleaner, but good
* multiplier/increments for such seem hard to come by.
*/
low = __rte_rand_lcg32(seed);
high = __rte_rand_lcg32(seed);
return low | (high << 32);
}
static uint64_t
__rte_rand_lfsr258_gen_seed(uint32_t *seed, uint64_t min_value)
{
uint64_t res;
res = __rte_rand_lcg64(seed);
if (res < min_value)
res += min_value;
return res;
}
static void
__rte_srand_lfsr258(uint64_t seed, struct rte_rand_state *state)
{
uint32_t lcg_seed;
lcg_seed = (uint32_t)(seed ^ (seed >> 32));
state->z1 = __rte_rand_lfsr258_gen_seed(&lcg_seed, 2UL);
state->z2 = __rte_rand_lfsr258_gen_seed(&lcg_seed, 512UL);
state->z3 = __rte_rand_lfsr258_gen_seed(&lcg_seed, 4096UL);
state->z4 = __rte_rand_lfsr258_gen_seed(&lcg_seed, 131072UL);
state->z5 = __rte_rand_lfsr258_gen_seed(&lcg_seed, 8388608UL);
}
void
rte_srand(uint64_t seed)
{
unsigned int lcore_id;
/* add lcore_id to seed to avoid having the same sequence */
for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
__rte_srand_lfsr258(seed + lcore_id, &rand_states[lcore_id]);
}
static __rte_always_inline uint64_t
__rte_rand_lfsr258_comp(uint64_t z, uint64_t a, uint64_t b, uint64_t c,
uint64_t d)
{
return ((z & c) << d) ^ (((z << a) ^ z) >> b);
}
/* Based on LEcuyer, P.: Tables of maximally equidistributed combined
* LFSR generators.
*/
static __rte_always_inline uint64_t
__rte_rand_lfsr258(struct rte_rand_state *state)
{
state->z1 = __rte_rand_lfsr258_comp(state->z1, 1UL, 53UL,
18446744073709551614UL, 10UL);
state->z2 = __rte_rand_lfsr258_comp(state->z2, 24UL, 50UL,
18446744073709551104UL, 5UL);
state->z3 = __rte_rand_lfsr258_comp(state->z3, 3UL, 23UL,
18446744073709547520UL, 29UL);
state->z4 = __rte_rand_lfsr258_comp(state->z4, 5UL, 24UL,
18446744073709420544UL, 23UL);
state->z5 = __rte_rand_lfsr258_comp(state->z5, 3UL, 33UL,
18446744073701163008UL, 8UL);
return state->z1 ^ state->z2 ^ state->z3 ^ state->z4 ^ state->z5;
}
static __rte_always_inline
struct rte_rand_state *__rte_rand_get_state(void)
{
unsigned int lcore_id;
lcore_id = rte_lcore_id();
if (unlikely(lcore_id == LCORE_ID_ANY))
lcore_id = rte_get_master_lcore();
return &rand_states[lcore_id];
}
uint64_t
rte_rand(void)
{
struct rte_rand_state *state;
state = __rte_rand_get_state();
return __rte_rand_lfsr258(state);
}
RTE_INIT(rte_rand_init)
{
rte_srand(rte_get_timer_cycles());
}

View file

@ -69,6 +69,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += malloc_mp.c
SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_keepalive.c
SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_option.c
SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_service.c
SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_random.c
SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_reciprocal.c
# from arch dir

View file

@ -758,8 +758,6 @@ rte_eal_init(int argc, char **argv)
#endif
}
rte_srand(rte_rdtsc());
/* in secondary processes, memory init may allocate additional fbarrays
* not present in primary processes, so to avoid any potential issues,
* initialize memzones first.

View file

@ -77,6 +77,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += malloc_mp.c
SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_keepalive.c
SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_option.c
SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_service.c
SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_random.c
SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_reciprocal.c
# from arch dir

View file

@ -1122,8 +1122,6 @@ rte_eal_init(int argc, char **argv)
#endif
}
rte_srand(rte_rdtsc());
if (rte_eal_log_init(logid, internal_config.syslog_facility) < 0) {
rte_eal_init_alert("Cannot init logging.");
rte_errno = ENOMEM;

View file

@ -292,6 +292,8 @@ DPDK_19.08 {
rte_lcore_index;
rte_lcore_to_socket_id;
rte_rand;
rte_srand;
} DPDK_19.05;