eal: allow setting random number generator seed
RFC 1/1 eal: allow setting random number generator seed
Many performance tests use rte_rand() and the random number can perturb the results. Add an ability to overide the automatic random seed on DPDK startup.
This is not a security problem since rte_rand() is documented as not being cryptographically secure.
--- lib/eal/common/rte_random.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
lib/eal/common/rte_random.c +12 -1
@@ -247,7 +247,18 @@ eal_rand_init(void)RTE_LCORE_VAR_ALLOC (rand_state); - seed =__rte_random_initial_seed (); +const char *env =getenv ("DPDK_RANDOM_SEED" ); +if (env !=NULL && *env !='\0' ) { +char *end; + + errno =0 ; + seed =strtoull (env, &end,0 ); +if (errno !=0 || *end !='\0' ) +rte_exit (EXIT_FAILURE , +"invalid DPDK_RANDOM_SEED: %s\n" , env); + }else { + seed =__rte_random_initial_seed (); + }rte_srand (seed); }
Signature · 1 line
2.53.0
Quoted 40 lines
From: Stephen Hemminger [mailto:[email protected]]
Sent: Saturday, 12 September 2026 00.59Many performance tests use
rte_rand()and the random number can perturb the results. Add an ability to overide the automatic random seed on DPDK startup.This is not a security problem since
rte_rand()is documented as not being cryptographically secure.Signed-off-by Stephen Hemminger <[email protected]>--- lib/eal/common/rte_random.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)lib/eal/common/rte_random.c +12
-2@@ -247,7 +247,18 @@ eal_rand_init(void)RTE_LCORE_VAR_ALLOC (rand_state); - seed =__rte_random_initial_seed (); +const char *env =getenv ("DPDK_RANDOM_SEED" ); +if (env !=NULL && *env !='\0' ) { +char *end; + + errno =0 ; + seed =strtoull (env, &end,0 ); +if (errno !=0 || *end !='\0' ) +rte_exit (EXIT_FAILURE , +"invalid DPDK_RANDOM_SEED: %s\n" , env); + }else { + seed =__rte_random_initial_seed (); + }rte_srand (seed); } --
Quoted 1 line
2.53.0
Might be useful.
PATCH v2 1/1 eal: allow setting random number generator seed
In reply to Stephen HemmingerMany performance tests use rte_rand() and the random number can perturb the results. Add an ability to override the automatic random seed on DPDK startup.
This is not a security problem since rte_rand() is documented as not being cryptographically secure.
---
v2 - fix header inclusion
- add docbook comment
lib/eal/common/rte_random.c | 14 +++++++++++++- lib/eal/include/rte_random.h | 8 +++++--- 2 files changed, 18 insertions(+), 4 deletions(-)
lib/eal/common/rte_random.c +13 -1
@@ -8,6 +8,7 @@#endif #endif #include <unistd.h> +#include <stdlib.h> #include <rte_bitops.h> #include <rte_branch_prediction.h>
@@ -247,7 +248,18 @@ eal_rand_init(void)RTE_LCORE_VAR_ALLOC (rand_state); - seed =__rte_random_initial_seed (); +const char *env =getenv ("DPDK_RANDOM_SEED" ); +if (env !=NULL && *env !='\0' ) { +char *end; + + errno =0 ; + seed =strtoull (env, &end,0 ); +if (errno !=0 || *end !='\0' ) +rte_exit (EXIT_FAILURE , +"invalid DPDK_RANDOM_SEED: %s\n" , env); + }else { + seed =__rte_random_initial_seed (); + }rte_srand (seed); }
lib/eal/include/rte_random.h +5 -3
@@ -20,9 +20,11 @@ extern "C" {/** * Seed the pseudo-random generator. * -* The generator is automatically seeded by the EAL init with a timer -* value. It may need to be re-seeded by the user with a real random -* value. +* The generator is automatically seeded by the EAL init with +* a system provided random entropy source. But for testing +* it can be useful to force a repeatable starting point by +* setting the initial seed. This can be done by setting +* the `DPDK_RANDOM_SEED` environment variable. * * This function is not multi-thread safe in regards to other * rte_srand() calls, nor is it in relation to concurrent rte_rand(),
Signature · 1 line
2.53.0
Den 2026-09-12 kl. 18:16, skrev Stephen Hemminger:
Quoted 3 lines
Many performance tests use
rte_rand()and the random number can perturb the results. Add an ability to override the automatic random seed on DPDK startup.
There is a way to seed the PRNG already, rte_srand(). Why the tests can't use this?
Provided the test results depend on something that use the PRNG before the test driver has had the opportunity to call rte_srand(), controlling the *initial* seed may be required.
I don't think this feature should be controlled by an environment variable. There is no precedent for that. If this functionality is deemed useful, it should be an EAL command line option, it seems to me.
Quoted 30 lines
This is not a security problem since
rte_rand()is documented as not being cryptographically secure.Signed-off-by Stephen Hemminger <[email protected]>Reviewed-by Morten Brørup <[email protected]>---
v2 - fix header inclusion
- add docbook commentlib/eal/common/rte_random.c | 14 +++++++++++++- lib/eal/include/rte_random.h | 8 +++++--- 2 files changed, 18 insertions(+), 4 deletions(-)lib/eal/common/rte_random.c +1
-0@@ -8,6 +8,7 @@#endif #endif #include <unistd.h> +#include <stdlib.h> #include <rte_bitops.h> #include <rte_branch_prediction.h> @@ -247,7 +248,18 @@ eal_rand_init(void)RTE_LCORE_VAR_ALLOC (rand_state);
lib/eal/common/rte_random.c eal_rand_init(void) · line 259
Quoted 10 lines
lib/eal/common/rte_random.c +9
-1- seed =__rte_random_initial_seed (); +const char *env =getenv ("DPDK_RANDOM_SEED" ); +if (env !=NULL && *env !='\0' ) { +char *end; + + errno =0 ; + seed =strtoull (env, &end,0 ); +if (errno !=0 || *end !='\0' ) +rte_exit (EXIT_FAILURE , +"invalid DPDK_RANDOM_SEED: %s\n" , env);
Is rte_exit() the way to deal with errors here? Not to be used in DPDK libraries, if I recall correctly.
I would think logging an error would suffice. Or rte_eal_init_alert().
Quoted 25 lines
lib/eal/common/rte_random.c +3
-0+ }else { + seed =__rte_random_initial_seed (); + }rte_srand (seed); }lib/eal/include/rte_random.h +5
-3@@ -20,9 +20,11 @@ extern "C" {/** * Seed the pseudo-random generator. * -* The generator is automatically seeded by the EAL init with a timer -* value. It may need to be re-seeded by the user with a real random -* value. +* The generator is automatically seeded by the EAL init with +* a system provided random entropy source. But for testing +* it can be useful to force a repeatable starting point by +* setting the initial seed. This can be done by setting +* the `DPDK_RANDOM_SEED` environment variable. * * This function is not multi-thread safe in regards to other * rte_srand() calls, nor is it in relation to concurrent rte_rand(),
Quoted 10 lines
Den 2026-09-12 kl. 18:16, skrev Stephen Hemminger:
Quoted 3 lines
Many performance tests use
rte_rand()and the random number can perturb the results. Add an ability to override the automatic random seed on DPDK startup.There is a way to seed the PRNG already,
rte_srand(). Why the tests can't use this?
Quoted 7 lines
Provided the test results depend on something that use the PRNG before the test driver has had the opportunity to call
rte_srand(), controlling the *initial* seed may be required.I don't think this feature should be controlled by an environment variable. There is no precedent for that. If this functionality is deemed useful, it should be an EAL command line option, it seems to me.
+1
Quoted 31 lines
Quoted 31 lines
This is not a security problem since
rte_rand()is documented as not being cryptographically secure.Signed-off-by Stephen Hemminger <[email protected]>Reviewed-by Morten Brørup <[email protected]>---
v2 - fix header inclusion
- add docbook commentlib/eal/common/rte_random.c | 14 +++++++++++++- lib/eal/include/rte_random.h | 8 +++++--- 2 files changed, 18 insertions(+), 4 deletions(-)lib/eal/common/rte_random.c +1
-0@@ -8,6 +8,7 @@#endif #endif #include <unistd.h> +#include <stdlib.h> #include <rte_bitops.h> #include <rte_branch_prediction.h> @@ -247,7 +248,18 @@ eal_rand_init(void)RTE_LCORE_VAR_ALLOC (rand_state);
Quoted 13 lines
lib/eal/common/rte_random.c eal_rand_init(void) · line 259
Quoted 10 lines
lib/eal/common/rte_random.c +9
-1- seed =__rte_random_initial_seed (); +const char *env =getenv ("DPDK_RANDOM_SEED" ); +if (env !=NULL && *env !='\0' ) { +char *end; + + errno =0 ; + seed =strtoull (env, &end,0 ); +if (errno !=0 || *end !='\0' ) +rte_exit (EXIT_FAILURE , +"invalid DPDK_RANDOM_SEED: %s\n" , env);Is
rte_exit()the way to deal with errors here? Not to be used in DPDK libraries, if I recall correctly.
Again, +1
Whole patch looks to me like a strange hack that completely ignores DPDK coding practices. My vote is NACK.
Quoted 28 lines
I would think logging an error would suffice. Or
rte_eal_init_alert().Quoted 25 lines
lib/eal/common/rte_random.c +3
-0+ }else { + seed =__rte_random_initial_seed (); + }rte_srand (seed); }lib/eal/include/rte_random.h +5
-3@@ -20,9 +20,11 @@ extern "C" {/** * Seed the pseudo-random generator. * -* The generator is automatically seeded by the EAL init with a timer -* value. It may need to be re-seeded by the user with a real random -* value. +* The generator is automatically seeded by the EAL init with +* a system provided random entropy source. But for testing +* it can be useful to force a repeatable starting point by +* setting the initial seed. This can be done by setting +* the `DPDK_RANDOM_SEED` environment variable. * * This function is not multi-thread safe in regards to other * rte_srand() calls, nor is it in relation to concurrent rte_rand(),
Quoted 10 lines · On Mon, 14 Sep 2026 12:04:16 +0000 Konstantin Ananyev…
Quoted 8 lines
Provided the test results depend on something that use the PRNG before the test driver has had the opportunity to call
rte_srand(), controlling the *initial* seed may be required.I don't think this feature should be controlled by an environment variable. There is no precedent for that. If this functionality is deemed useful, it should be an EAL command line option, it seems to me.
+1
I thought about that but getting command line options into EAL when using meson test runner is awkward (not impossible though).
Quoted 8 lines · On Mon, 14 Sep 2026 13:29:22 +0200 Mattias Rönnblom <[email protected]>…
Den 2026-09-12 kl. 18:16, skrev Stephen Hemminger:
Quoted 3 lines
Many performance tests use
rte_rand()and the random number can perturb the results. Add an ability to override the automatic random seed on DPDK startup.There is a way to seed the PRNG already,
rte_srand(). Why the tests can't use this?
Quoted 7 lines
Provided the test results depend on something that use the PRNG before the test driver has had the opportunity to call
rte_srand(), controlling the *initial* seed may be required.I don't think this feature should be controlled by an environment variable. There is no precedent for that. If this functionality is deemed useful, it should be an EAL command line option, it seems to me.
There are several tests already using rte_srand() so that is one option. The choice of environment variable was to match the existing test harness infrastructure that uses MALLOC_PERTURB to pre-seed malloc.
Quoted 44 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Friday 11 September 2026 23:59
To: [email protected]
Cc: Stephen Hemminger <[email protected]>; Mattias Rönnblom <[email protected]>
Subject: [RFC] eal: allow setting random number generator seedMany performance tests use
rte_rand()and the random number can perturb the results. Add an ability to overide the automatic random seed on DPDK startup.This is not a security problem since
rte_rand()is documented as not being cryptographically secure.Signed-off-by Stephen Hemminger <[email protected]>--- lib/eal/common/rte_random.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)lib/eal/common/rte_random.c +12
-2@@ -247,7 +247,18 @@ eal_rand_init(void)RTE_LCORE_VAR_ALLOC (rand_state); - seed =__rte_random_initial_seed (); +const char *env =getenv ("DPDK_RANDOM_SEED" ); +if (env !=NULL && *env !='\0' ) { +char *end; + + errno =0 ; + seed =strtoull (env, &end,0 ); +if (errno !=0 || *end !='\0' ) +rte_exit (EXIT_FAILURE , +"invalid DPDK_RANDOM_SEED: %s\n" , env); + }else { + seed =__rte_random_initial_seed (); + }rte_srand (seed); } --
Quoted 1 line
2.53.0
Lgtm, although as a minor nit env is maybe not the best name here.
Regardless,