All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC] eal: allow setting random number generator seed
@ 2026-09-11 22:58 Stephen Hemminger
  2026-09-12  6:26 ` Morten Brørup
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Stephen Hemminger @ 2026-09-11 22:58 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Mattias Rönnblom

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.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/rte_random.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/lib/eal/common/rte_random.c b/lib/eal/common/rte_random.c
index 576a32a46c..cdad60049b 100644
--- a/lib/eal/common/rte_random.c
+++ b/lib/eal/common/rte_random.c
@@ -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);
 }
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* RE: [RFC] eal: allow setting random number generator seed
  2026-09-11 22:58 [RFC] eal: allow setting random number generator seed Stephen Hemminger
@ 2026-09-12  6:26 ` Morten Brørup
  2026-09-12 16:16 ` [PATCH v2] " Stephen Hemminger
  2026-09-14  9:05 ` [RFC] " Marat Khalili
  2 siblings, 0 replies; 8+ messages in thread
From: Morten Brørup @ 2026-09-12  6:26 UTC (permalink / raw)
  To: Stephen Hemminger, dev; +Cc: Mattias Rönnblom

> From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> Sent: Saturday, 12 September 2026 00.59
> 
> 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.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>  lib/eal/common/rte_random.c | 13 ++++++++++++-
>  1 file changed, 12 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/eal/common/rte_random.c b/lib/eal/common/rte_random.c
> index 576a32a46c..cdad60049b 100644
> --- a/lib/eal/common/rte_random.c
> +++ b/lib/eal/common/rte_random.c
> @@ -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);
>  }
> --
> 2.53.0

Might be useful.
Reviewed-by: Morten Brørup <mb@smartsharesystems.com>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2] eal: allow setting random number generator seed
  2026-09-11 22:58 [RFC] eal: allow setting random number generator seed Stephen Hemminger
  2026-09-12  6:26 ` Morten Brørup
@ 2026-09-12 16:16 ` Stephen Hemminger
  2026-09-14 11:29   ` Mattias Rönnblom
  2026-09-14  9:05 ` [RFC] " Marat Khalili
  2 siblings, 1 reply; 8+ messages in thread
From: Stephen Hemminger @ 2026-09-12 16:16 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Morten Brørup, Mattias Rönnblom

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.

This is not a security problem since rte_rand() is documented
as not being cryptographically secure.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
Reviewed-by: Morten Brørup <mb@smartsharesystems.com>
---
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(-)

diff --git a/lib/eal/common/rte_random.c b/lib/eal/common/rte_random.c
index 576a32a46c..3cc0e3ec5f 100644
--- a/lib/eal/common/rte_random.c
+++ b/lib/eal/common/rte_random.c
@@ -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);
 }
diff --git a/lib/eal/include/rte_random.h b/lib/eal/include/rte_random.h
index 15cbe6215a..bdd4001e78 100644
--- a/lib/eal/include/rte_random.h
+++ b/lib/eal/include/rte_random.h
@@ -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(),
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* RE: [RFC] eal: allow setting random number generator seed
  2026-09-11 22:58 [RFC] eal: allow setting random number generator seed Stephen Hemminger
  2026-09-12  6:26 ` Morten Brørup
  2026-09-12 16:16 ` [PATCH v2] " Stephen Hemminger
@ 2026-09-14  9:05 ` Marat Khalili
  2 siblings, 0 replies; 8+ messages in thread
From: Marat Khalili @ 2026-09-14  9:05 UTC (permalink / raw)
  To: Stephen Hemminger, dev@dpdk.org; +Cc: Mattias Rönnblom

> -----Original Message-----
> From: Stephen Hemminger <stephen@networkplumber.org>
> Sent: Friday 11 September 2026 23:59
> To: dev@dpdk.org
> Cc: Stephen Hemminger <stephen@networkplumber.org>; Mattias Rönnblom <mattias.ronnblom@ericsson.com>
> Subject: [RFC] 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.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>  lib/eal/common/rte_random.c | 13 ++++++++++++-
>  1 file changed, 12 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/eal/common/rte_random.c b/lib/eal/common/rte_random.c
> index 576a32a46c..cdad60049b 100644
> --- a/lib/eal/common/rte_random.c
> +++ b/lib/eal/common/rte_random.c
> @@ -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);
>  }
> --
> 2.53.0

Lgtm, although as a minor nit `env` is maybe not the best name here.

Regardless,

Acked-by: Marat Khalili <marat.khalili@huawei.com>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2] eal: allow setting random number generator seed
  2026-09-12 16:16 ` [PATCH v2] " Stephen Hemminger
@ 2026-09-14 11:29   ` Mattias Rönnblom
  2026-09-14 12:04     ` Konstantin Ananyev
  2026-09-14 16:46     ` Stephen Hemminger
  0 siblings, 2 replies; 8+ messages in thread
From: Mattias Rönnblom @ 2026-09-14 11:29 UTC (permalink / raw)
  To: Stephen Hemminger, dev; +Cc: Morten Brørup, Mattias Rönnblom

Den 2026-09-12 kl. 18:16, skrev Stephen Hemminger:
> 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.

> 
> This is not a security problem since rte_rand() is documented
> as not being cryptographically secure.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> Reviewed-by: Morten Brørup <mb@smartsharesystems.com>
> ---
> 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(-)
> 
> diff --git a/lib/eal/common/rte_random.c b/lib/eal/common/rte_random.c
> index 576a32a46c..3cc0e3ec5f 100644
> --- a/lib/eal/common/rte_random.c
> +++ b/lib/eal/common/rte_random.c
> @@ -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);

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().

> +	} else {
> +		seed = __rte_random_initial_seed();
> +	}
>   
>   	rte_srand(seed);
>   }
> diff --git a/lib/eal/include/rte_random.h b/lib/eal/include/rte_random.h
> index 15cbe6215a..bdd4001e78 100644
> --- a/lib/eal/include/rte_random.h
> +++ b/lib/eal/include/rte_random.h
> @@ -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(),


^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: [PATCH v2] eal: allow setting random number generator seed
  2026-09-14 11:29   ` Mattias Rönnblom
@ 2026-09-14 12:04     ` Konstantin Ananyev
  2026-09-14 16:47       ` Stephen Hemminger
  2026-09-14 16:46     ` Stephen Hemminger
  1 sibling, 1 reply; 8+ messages in thread
From: Konstantin Ananyev @ 2026-09-14 12:04 UTC (permalink / raw)
  To: Mattias Rönnblom, Stephen Hemminger, dev@dpdk.org
  Cc: Morten Brørup, Mattias Rönnblom



> Den 2026-09-12 kl. 18:16, skrev Stephen Hemminger:
> > 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.

+1

> 
> >
> > This is not a security problem since rte_rand() is documented
> > as not being cryptographically secure.
> >
> > Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> > Reviewed-by: Morten Brørup <mb@smartsharesystems.com>
> > ---
> > 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(-)
> >
> > diff --git a/lib/eal/common/rte_random.c b/lib/eal/common/rte_random.c
> > index 576a32a46c..3cc0e3ec5f 100644
> > --- a/lib/eal/common/rte_random.c
> > +++ b/lib/eal/common/rte_random.c
> > @@ -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);
> 
> 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.

> 
> I would think logging an error would suffice. Or rte_eal_init_alert().
> 
> > +	} else {
> > +		seed = __rte_random_initial_seed();
> > +	}
> >
> >   	rte_srand(seed);
> >   }
> > diff --git a/lib/eal/include/rte_random.h b/lib/eal/include/rte_random.h
> > index 15cbe6215a..bdd4001e78 100644
> > --- a/lib/eal/include/rte_random.h
> > +++ b/lib/eal/include/rte_random.h
> > @@ -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(),


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2] eal: allow setting random number generator seed
  2026-09-14 11:29   ` Mattias Rönnblom
  2026-09-14 12:04     ` Konstantin Ananyev
@ 2026-09-14 16:46     ` Stephen Hemminger
  1 sibling, 0 replies; 8+ messages in thread
From: Stephen Hemminger @ 2026-09-14 16:46 UTC (permalink / raw)
  To: Mattias Rönnblom; +Cc: dev, Morten Brørup, Mattias Rönnblom

On Mon, 14 Sep 2026 13:29:22 +0200
Mattias Rönnblom <hofors@lysator.liu.se> wrote:

> Den 2026-09-12 kl. 18:16, skrev Stephen Hemminger:
> > 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.

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.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2] eal: allow setting random number generator seed
  2026-09-14 12:04     ` Konstantin Ananyev
@ 2026-09-14 16:47       ` Stephen Hemminger
  0 siblings, 0 replies; 8+ messages in thread
From: Stephen Hemminger @ 2026-09-14 16:47 UTC (permalink / raw)
  To: Konstantin Ananyev
  Cc: Mattias Rönnblom, dev@dpdk.org, Morten Brørup,
	Mattias Rönnblom

On Mon, 14 Sep 2026 12:04:16 +0000
Konstantin Ananyev <konstantin.ananyev@huawei.com> wrote:

> > 
> > 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).

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-09-14 16:47 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-11 22:58 [RFC] eal: allow setting random number generator seed Stephen Hemminger
2026-09-12  6:26 ` Morten Brørup
2026-09-12 16:16 ` [PATCH v2] " Stephen Hemminger
2026-09-14 11:29   ` Mattias Rönnblom
2026-09-14 12:04     ` Konstantin Ananyev
2026-09-14 16:47       ` Stephen Hemminger
2026-09-14 16:46     ` Stephen Hemminger
2026-09-14  9:05 ` [RFC] " Marat Khalili

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.