Skip to contentSkip to conversation

eal: choose initial PRNG seed source at runtime

PATCH Superseded Dan Gora 3 participants 10 messages Apr 15, 2020 – Apr 17, 2020 Arm/Amlogic Meson SoC support

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Dan Gora

author
#0 lore raw

PATCH 1/1 eal: choose initial PRNG seed source at runtime

Instead of choosing to use getentropy() or the rdseed instruction for the random number generator entropy source using compilation flags, determine the best source at run time.

This is accomplished by defining a weak symbol for getentropy(), checking that the compiler can generate the rdseed instruction even if the compilation platform does not natively support it, and checking for the availability of the rdseed instruction on the execution platform using rte_cpu_get_flag_enabled().

If neither getentropy() or rdseed is available, rte_get_timer_cycles() will be continue to be used as the entropy source.

This also allows non-Mason builds to use getentropy().

---
 config/x86/meson.build             |  7 +++++++
 lib/librte_eal/common/rte_random.c | 29 ++++++++++++++++++++++++-----
 lib/librte_eal/meson.build         |  3 ---
 mk/rte.cpuflags.mk                 |  8 ++++++++
 4 files changed, 39 insertions(+), 8 deletions(-)
config/x86/meson.build +7 -0
index adc857ba2..214b16f2a 100644
@@ -20,6 +20,13 @@ if cc.get_define('__SSE4_2__', args: machine_args) == ''
 	machine_args += '-msse4'
 endif
 
+# set -mrdseed if necessary so _rdseed32_step compiles if the
+# compilation host does not support the RDSEED instruction.
+if cc.get_define('__RDSEED__', args: machine_args) == '' and cc.has_argument('-mrdseed')
+	machine_args += '-mrdseed'
+	message('RDSEED not enabled by default, explicitly setting -mrdseed')
+endif
+
 base_flags = ['SSE', 'SSE2', 'SSE3','SSSE3', 'SSE4_1', 'SSE4_2']
 foreach f:base_flags
 	dpdk_conf.set('RTE_MACHINE_CPUFLAG_' + f, 1)
lib/librte_eal/common/rte_random.c +24 -5
index 57ec8fb2b..40f8b5aab 100644
@@ -25,6 +25,8 @@ struct rte_rand_state {
 
 static struct rte_rand_state rand_states[RTE_MAX_LCORE];
 
+__rte_weak int getentropy(void *__buffer, size_t __length);
+
 static uint32_t
 __rte_rand_lcg32(uint32_t *seed)
 {
@@ -176,10 +178,24 @@ rte_rand_max(uint64_t upper_bound)
 	return res;
 }
 
+/* Use rte_get_timer_cycles() if the system does not have
+ * genentropy() or the rdseed instruction.
+ */
+__rte_weak int
+getentropy(void *__buffer, size_t __length __rte_unused)
+{
+	uint64_t *ge_seed = __buffer;
+#ifdef RTE_MACHINE_CPUFLAG_RDSEED
+	if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_RDSEED))
+		return -1;
+#endif
+	*ge_seed = rte_get_timer_cycles();
+	return 0;
+}
+
 static uint64_t
 __rte_random_initial_seed(void)
 {
-#ifdef RTE_LIBEAL_USE_GETENTROPY
 	int ge_rc;
 	uint64_t ge_seed;
 
@@ -187,15 +203,18 @@ __rte_random_initial_seed(void)
 
 	if (ge_rc == 0)
 		return ge_seed;
-#endif
+
 #ifdef RTE_MACHINE_CPUFLAG_RDSEED
 	unsigned int rdseed_low;
 	unsigned int rdseed_high;
 
 	/* first fallback: rdseed instruction, if available */
-	if (_rdseed32_step(&rdseed_low) == 1 &&
-	    _rdseed32_step(&rdseed_high) == 1)
-		return (uint64_t)rdseed_low | ((uint64_t)rdseed_high << 32);
+	if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_RDSEED)) {
+		if (_rdseed32_step(&rdseed_low) == 1 &&
+		    _rdseed32_step(&rdseed_high) == 1)
+			return (uint64_t)rdseed_low |
+				((uint64_t)rdseed_high << 32);
+	}
 #endif
 	/* second fallback: seed using rdtsc */
 	return rte_get_timer_cycles();
lib/librte_eal/meson.build +0 -3
index 4be5118ce..8382efbb8 100644
@@ -17,9 +17,6 @@ deps += 'kvargs'
 if dpdk_conf.has('RTE_USE_LIBBSD')
 	ext_deps += libbsd
 endif
-if cc.has_function('getentropy', prefix : '#include <unistd.h>')
-	cflags += '-DRTE_LIBEAL_USE_GETENTROPY'
-endif
 sources = common_sources + env_sources
 objs = common_objs + env_objs
 headers = common_headers + env_headers
mk/rte.cpuflags.mk +8 -0
index fa8753531..fb7bf8a53 100644
@@ -53,6 +53,14 @@ endif
 
 ifneq ($(filter $(AUTO_CPUFLAGS),__RDSEED__),)
 CPUFLAGS += RDSEED
+else
+# If the native environment doesn't define __RDSEED__, see
+# if the compiler supports -mrdseed.
+RDSEED_CPUFLAGS := $(shell $(CC) $(MACHINE_CFLAGS) $(WERROR_FLAGS) $(EXTRA_CFLAGS) -mrdseed -dM -E - < /dev/null)
+ifneq ($(filter $(RDSEED_CPUFLAGS),__RDSEED__),)
+CPUFLAGS += RDSEED
+MACHINE_CFLAGS += -mrdseed
+endif
 endif
 
 ifneq ($(filter $(AUTO_CPUFLAGS),__FSGSBASE__),)
Signature · 1 line
2.24.1.425.g7034cd094b
author
#1 lore raw

PATCH v2 1/1 eal: choose initial PRNG seed source at runtime

Instead of choosing to use getentropy() or the rdseed instruction for the random number generator entropy source using compilation flags, determine the best source at run time.

This is accomplished by defining a weak symbol for getentropy(), checking that the compiler can generate the rdseed instruction even if the compilation platform does not natively support it, and checking for the availability of the rdseed instruction on the execution platform using rte_cpu_get_flag_enabled().

If neither getentropy() or rdseed is available, rte_get_timer_cycles() will be continue to be used as the entropy source.

This also allows non-meson builds to use getentropy().

---
v2:
* Rebase to latest master.
* Fix spelling of "meson".

 config/x86/meson.build             |  7 +++++++
 lib/librte_eal/common/rte_random.c | 29 ++++++++++++++++++++++++-----
 lib/librte_eal/meson.build         |  3 ---
 mk/rte.cpuflags.mk                 |  8 ++++++++
 4 files changed, 39 insertions(+), 8 deletions(-)
config/x86/meson.build +7 -0
index adc857ba2..214b16f2a 100644
@@ -20,6 +20,13 @@ if cc.get_define('__SSE4_2__', args: machine_args) == ''
 	machine_args += '-msse4'
 endif
 
+# set -mrdseed if necessary so _rdseed32_step compiles if the
+# compilation host does not support the RDSEED instruction.
+if cc.get_define('__RDSEED__', args: machine_args) == '' and cc.has_argument('-mrdseed')
+	machine_args += '-mrdseed'
+	message('RDSEED not enabled by default, explicitly setting -mrdseed')
+endif
+
 base_flags = ['SSE', 'SSE2', 'SSE3','SSSE3', 'SSE4_1', 'SSE4_2']
 foreach f:base_flags
 	dpdk_conf.set('RTE_MACHINE_CPUFLAG_' + f, 1)
lib/librte_eal/common/rte_random.c +24 -5
index 57ec8fb2b..40f8b5aab 100644
@@ -25,6 +25,8 @@ struct rte_rand_state {
 
 static struct rte_rand_state rand_states[RTE_MAX_LCORE];
 
+__rte_weak int getentropy(void *__buffer, size_t __length);
+
 static uint32_t
 __rte_rand_lcg32(uint32_t *seed)
 {
@@ -176,10 +178,24 @@ rte_rand_max(uint64_t upper_bound)
 	return res;
 }
 
+/* Use rte_get_timer_cycles() if the system does not have
+ * genentropy() or the rdseed instruction.
+ */
+__rte_weak int
+getentropy(void *__buffer, size_t __length __rte_unused)
+{
+	uint64_t *ge_seed = __buffer;
+#ifdef RTE_MACHINE_CPUFLAG_RDSEED
+	if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_RDSEED))
+		return -1;
+#endif
+	*ge_seed = rte_get_timer_cycles();
+	return 0;
+}
+
 static uint64_t
 __rte_random_initial_seed(void)
 {
-#ifdef RTE_LIBEAL_USE_GETENTROPY
 	int ge_rc;
 	uint64_t ge_seed;
 
@@ -187,15 +203,18 @@ __rte_random_initial_seed(void)
 
 	if (ge_rc == 0)
 		return ge_seed;
-#endif
+
 #ifdef RTE_MACHINE_CPUFLAG_RDSEED
 	unsigned int rdseed_low;
 	unsigned int rdseed_high;
 
 	/* first fallback: rdseed instruction, if available */
-	if (_rdseed32_step(&rdseed_low) == 1 &&
-	    _rdseed32_step(&rdseed_high) == 1)
-		return (uint64_t)rdseed_low | ((uint64_t)rdseed_high << 32);
+	if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_RDSEED)) {
+		if (_rdseed32_step(&rdseed_low) == 1 &&
+		    _rdseed32_step(&rdseed_high) == 1)
+			return (uint64_t)rdseed_low |
+				((uint64_t)rdseed_high << 32);
+	}
 #endif
 	/* second fallback: seed using rdtsc */
 	return rte_get_timer_cycles();
lib/librte_eal/meson.build +0 -3
index 0267c3b9d..748359b8c 100644
@@ -15,9 +15,6 @@ deps += 'kvargs'
 if dpdk_conf.has('RTE_USE_LIBBSD')
 	ext_deps += libbsd
 endif
-if cc.has_function('getentropy', prefix : '#include <unistd.h>')
-	cflags += '-DRTE_LIBEAL_USE_GETENTROPY'
-endif
 if cc.has_header('getopt.h')
 	cflags += ['-DHAVE_GETOPT_H', '-DHAVE_GETOPT', '-DHAVE_GETOPT_LONG']
 endif
mk/rte.cpuflags.mk +8 -0
index fa8753531..fb7bf8a53 100644
@@ -53,6 +53,14 @@ endif
 
 ifneq ($(filter $(AUTO_CPUFLAGS),__RDSEED__),)
 CPUFLAGS += RDSEED
+else
+# If the native environment doesn't define __RDSEED__, see
+# if the compiler supports -mrdseed.
+RDSEED_CPUFLAGS := $(shell $(CC) $(MACHINE_CFLAGS) $(WERROR_FLAGS) $(EXTRA_CFLAGS) -mrdseed -dM -E - < /dev/null)
+ifneq ($(filter $(RDSEED_CPUFLAGS),__RDSEED__),)
+CPUFLAGS += RDSEED
+MACHINE_CFLAGS += -mrdseed
+endif
 endif
 
 ifneq ($(filter $(AUTO_CPUFLAGS),__FSGSBASE__),)
Signature · 1 line
2.24.1.425.g7034cd094b
#2 lore raw
Quoted 3 lines · Dan Gora

Instead of choosing to use getentropy() or the rdseed instruction for the random number generator entropy source using compilation flags, determine the best source at run time.

I like this idea.

Quoted 55 lines

This is accomplished by defining a weak symbol for getentropy(), checking that the compiler can generate the rdseed instruction even if the compilation platform does not natively support it, and checking for the availability of the rdseed instruction on the execution platform using rte_cpu_get_flag_enabled().

If neither getentropy() or rdseed is available, rte_get_timer_cycles() will be continue to be used as the entropy source.

This also allows non-meson builds to use getentropy().

---
v2:
* Rebase to latest master.
* Fix spelling of "meson".

  config/x86/meson.build             |  7 +++++++
  lib/librte_eal/common/rte_random.c | 29 ++++++++++++++++++++++++-----
  lib/librte_eal/meson.build         |  3 ---
  mk/rte.cpuflags.mk                 |  8 ++++++++
  4 files changed, 39 insertions(+), 8 deletions(-)
config/x86/meson.build +7 -0
index adc857ba2..214b16f2a 100644
@@ -20,6 +20,13 @@ if cc.get_define('__SSE4_2__', args: machine_args) == ''
  	machine_args += '-msse4'
  endif
 
+# set -mrdseed if necessary so _rdseed32_step compiles if the
+# compilation host does not support the RDSEED instruction.
+if cc.get_define('__RDSEED__', args: machine_args) == '' and cc.has_argument('-mrdseed')
+	machine_args += '-mrdseed'
+	message('RDSEED not enabled by default, explicitly setting -mrdseed')
+endif
+
  base_flags = ['SSE', 'SSE2', 'SSE3','SSSE3', 'SSE4_1', 'SSE4_2']
  foreach f:base_flags
  	dpdk_conf.set('RTE_MACHINE_CPUFLAG_' + f, 1)
lib/librte_eal/common/rte_random.c +2 -0
index 57ec8fb2b..40f8b5aab 100644
@@ -25,6 +25,8 @@ struct rte_rand_state {
 
  static struct rte_rand_state rand_states[RTE_MAX_LCORE];
 
+__rte_weak int getentropy(void *__buffer, size_t __length);
+
  static uint32_t
  __rte_rand_lcg32(uint32_t *seed)
  {

lib/librte_eal/common/rte_random.c rte_rand_max(uint64_t upper_bound) · line 186

Quoted 9 lines
lib/librte_eal/common/rte_random.c +6 -0
@@ -176,10 +178,24 @@ rte_rand_max(uint64_t upper_bound)
  	return res;
  }
 
+/* Use rte_get_timer_cycles() if the system does not have
+ * genentropy() or the rdseed instruction.
+ */
+__rte_weak int
+getentropy(void *__buffer, size_t __length __rte_unused)
+{

Just make the weak getentropy() to return -1, always.

Quoted 21 lines
lib/librte_eal/common/rte_random.c +9 -2
+	uint64_t *ge_seed = __buffer;
+#ifdef RTE_MACHINE_CPUFLAG_RDSEED
+	if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_RDSEED))
+		return -1;
+#endif
+	*ge_seed = rte_get_timer_cycles();
+	return 0;
+}
+
  static uint64_t
  __rte_random_initial_seed(void)
  {
-#ifdef RTE_LIBEAL_USE_GETENTROPY
  	int ge_rc;
  	uint64_t ge_seed;
 
@@ -187,15 +203,18 @@ __rte_random_initial_seed(void)
 
  	if (ge_rc == 0)
  		return ge_seed;
-#endif

lib/librte_eal/common/rte_random.c __rte_random_initial_seed(void) · line 212

Quoted 10 lines
lib/librte_eal/common/rte_random.c +2 -3
+
  #ifdef RTE_MACHINE_CPUFLAG_RDSEED
  	unsigned int rdseed_low;
  	unsigned int rdseed_high;
 
  	/* first fallback: rdseed instruction, if available */
-	if (_rdseed32_step(&rdseed_low) == 1 &&
-	    _rdseed32_step(&rdseed_high) == 1)
-		return (uint64_t)rdseed_low | ((uint64_t)rdseed_high << 32);
+	if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_RDSEED)) {

Move the variable declarations into this scope.

Quoted 27 lines
lib/librte_eal/common/rte_random.c +5 -0
+		if (_rdseed32_step(&rdseed_low) == 1 &&
+		    _rdseed32_step(&rdseed_high) == 1)
+			return (uint64_t)rdseed_low |
+				((uint64_t)rdseed_high << 32);
+	}
  #endif
  	/* second fallback: seed using rdtsc */
  	return rte_get_timer_cycles();
lib/librte_eal/meson.build +0 -3
index 0267c3b9d..748359b8c 100644
@@ -15,9 +15,6 @@ deps += 'kvargs'
  if dpdk_conf.has('RTE_USE_LIBBSD')
  	ext_deps += libbsd
  endif
-if cc.has_function('getentropy', prefix : '#include <unistd.h>')
-	cflags += '-DRTE_LIBEAL_USE_GETENTROPY'
-endif
  if cc.has_header('getopt.h')
  	cflags += ['-DHAVE_GETOPT_H', '-DHAVE_GETOPT', '-DHAVE_GETOPT_LONG']
  endif
mk/rte.cpuflags.mk +0 -0
index fa8753531..fb7bf8a53 100644

mk/rte.cpuflags.mk endif · line 58

Quoted 6 lines
mk/rte.cpuflags.mk +3 -0
@@ -53,6 +53,14 @@ endif
 
  ifneq ($(filter $(AUTO_CPUFLAGS),__RDSEED__),)
  CPUFLAGS += RDSEED
+else
+# If the native environment doesn't define __RDSEED__, see
+# if the compiler supports -mrdseed.

For which environments is this true?

mk/rte.cpuflags.mk endif · line 60

Quoted 2 lines
mk/rte.cpuflags.mk +2 -0
+RDSEED_CPUFLAGS := $(shell $(CC) $(MACHINE_CFLAGS) $(WERROR_FLAGS) $(EXTRA_CFLAGS) -mrdseed -dM -E - < /dev/null)
+ifneq ($(filter $(RDSEED_CPUFLAGS),__RDSEED__),)

There are no better ways to achieve this? It seems like a bit of a hack.

Quoted 6 lines
mk/rte.cpuflags.mk +3 -0
+CPUFLAGS += RDSEED
+MACHINE_CFLAGS += -mrdseed
+endif
  endif
 
  ifneq ($(filter $(AUTO_CPUFLAGS),__FSGSBASE__),)
author
#3 lore raw
Quoted 20 lines · Mattias Rönnblom
Quoted 20 lines
lib/librte_eal/meson.build +0 -3
index 0267c3b9d..748359b8c 100644
@@ -15,9 +15,6 @@ deps += 'kvargs'
  if dpdk_conf.has('RTE_USE_LIBBSD')
      ext_deps += libbsd
  endif
-if cc.has_function('getentropy', prefix : '#include <unistd.h>')
-     cflags += '-DRTE_LIBEAL_USE_GETENTROPY'
-endif
  if cc.has_header('getopt.h')
      cflags += ['-DHAVE_GETOPT_H', '-DHAVE_GETOPT', '-DHAVE_GETOPT_LONG']
  endif
mk/rte.cpuflags.mk +0 -0
index fa8753531..fb7bf8a53 100644
@@ -53,6 +53,14 @@ endif
 
Quoted 8 lines

mk/rte.cpuflags.mk endif · line 58

Quoted 5 lines
mk/rte.cpuflags.mk +3 -0
  ifneq ($(filter $(AUTO_CPUFLAGS),__RDSEED__),)
  CPUFLAGS += RDSEED
+else
+# If the native environment doesn't define __RDSEED__, see
+# if the compiler supports -mrdseed.

For which environments is this true?

If you compile on a machine which does not have the RDSEED cpu flag, gcc will not define __RDSEED__ nor compile the _rdseed32_step() functions unless you explicitly set -mrdseed on the gcc/clang command line..

We have a HP DL380 machine which do not have rdseed:
model name : Intel(R) Xeon(R) CPU E5-2630 v3 @ 2.40GHz

And another which does:
model name : Intel(R) Xeon(R) CPU E5-2640 v4 @ 2.40GHz

_rdseed32_step() will only compile on the v3 if we explicitly set -mrdseed.

Quoted 5 lines

mk/rte.cpuflags.mk endif · line 60

Quoted 2 lines
mk/rte.cpuflags.mk +2 -0
+RDSEED_CPUFLAGS := $(shell $(CC) $(MACHINE_CFLAGS) $(WERROR_FLAGS) $(EXTRA_CFLAGS) -mrdseed -dM -E - < /dev/null)
+ifneq ($(filter $(RDSEED_CPUFLAGS),__RDSEED__),)

There are no better ways to achieve this? It seems like a bit of a hack.

eh.. I don't know of any really.. It's the same command that is used to set AUTO_CPUFLAGS a few lines before, just with -mrdseed set. It doesn't seem that bad to my mind, but if there is a better way, I'm open to suggestions.

That said, this patch does not work. We cannot set RTE_MACHINE_CPUFLAG_RDSEED because rte_eal_init() will fail on a machine which does not have rdseed because rte_cpu_is_supported() checks all of these cpu flags set at compile time. Since we're detecting this flag at run time, we have to remove it from this list of "required" CPU flags.

I'm working on a new patch.. I should have something tomorrow.

There is also still a problem with getting getentropy() to work when the binary is compiled on a system with a glibc < 2.25 but run on a system with glibc >= 2.25. It doesn't resolve the weak symbol to the glibc version.. I think that it's because getentropy() is versioned within glibc. I'm still working on how to fix this.. It might come down to using dlopen(), dlsym()... :(

I'm not really sure if it's worth it.

thanks
dan

#4 lore raw
Quoted 58 lines · Dan Gora
Quoted 20 lines · Mattias Rönnblom
Quoted 20 lines
lib/librte_eal/meson.build +0 -3
index 0267c3b9d..748359b8c 100644
@@ -15,9 +15,6 @@ deps += 'kvargs'
   if dpdk_conf.has('RTE_USE_LIBBSD')
       ext_deps += libbsd
   endif
-if cc.has_function('getentropy', prefix : '#include <unistd.h>')
-     cflags += '-DRTE_LIBEAL_USE_GETENTROPY'
-endif
   if cc.has_header('getopt.h')
       cflags += ['-DHAVE_GETOPT_H', '-DHAVE_GETOPT', '-DHAVE_GETOPT_LONG']
   endif
mk/rte.cpuflags.mk +0 -0
index fa8753531..fb7bf8a53 100644
@@ -53,6 +53,14 @@ endif
 
Quoted 7 lines

mk/rte.cpuflags.mk endif · line 58

Quoted 5 lines
mk/rte.cpuflags.mk +3 -0
   ifneq ($(filter $(AUTO_CPUFLAGS),__RDSEED__),)
   CPUFLAGS += RDSEED
+else
+# If the native environment doesn't define __RDSEED__, see
+# if the compiler supports -mrdseed.

For which environments is this true?

If you compile on a machine which does not have the RDSEED cpu flag, gcc will not define __RDSEED__ nor compile the _rdseed32_step() functions unless you explicitly set -mrdseed on the gcc/clang command line..

We have a HP DL380 machine which do not have rdseed:
model name : Intel(R) Xeon(R) CPU E5-2630 v3 @ 2.40GHz

And another which does:
model name : Intel(R) Xeon(R) CPU E5-2640 v4 @ 2.40GHz

_rdseed32_step() will only compile on the v3 if we explicitly set -mrdseed.

Quoted 4 lines

mk/rte.cpuflags.mk endif · line 60

Quoted 2 lines
mk/rte.cpuflags.mk +2 -0
+RDSEED_CPUFLAGS := $(shell $(CC) $(MACHINE_CFLAGS) $(WERROR_FLAGS) $(EXTRA_CFLAGS) -mrdseed -dM -E - < /dev/null)
+ifneq ($(filter $(RDSEED_CPUFLAGS),__RDSEED__),)

There are no better ways to achieve this? It seems like a bit of a hack.

eh.. I don't know of any really.. It's the same command that is used to set AUTO_CPUFLAGS a few lines before, just with -mrdseed set. It doesn't seem that bad to my mind, but if there is a better way, I'm open to suggestions.

That said, this patch does not work. We cannot set RTE_MACHINE_CPUFLAG_RDSEED because rte_eal_init() will fail on a machine which does not have rdseed because rte_cpu_is_supported() checks all of these cpu flags set at compile time. Since we're detecting this flag at run time, we have to remove it from this list of "required" CPU flags.

Quoted 8 lines

I'm working on a new patch.. I should have something tomorrow.

There is also still a problem with getting getentropy() to work when the binary is compiled on a system with a glibc < 2.25 but run on a system with glibc >= 2.25. It doesn't resolve the weak symbol to the glibc version.. I think that it's because getentropy() is versioned within glibc. I'm still working on how to fix this.. It might come down to using dlopen(), dlsym()... :(

One way would be to include a getrandom syscall wrapper, instead of going via libc, in case libc is too old.

Quoted 1 line

I'm not really sure if it's worth it.

I personally don't think having high-quality initial, automatic, seeding is important-enough to warrant much complexity.

Quoted 2 lines

thanks
dan

#5 lore raw
In reply to Dan Gora
Quoted 62 lines · Dan Gora

Instead of choosing to use getentropy() or the rdseed instruction for the random number generator entropy source using compilation flags, determine the best source at run time.

This is accomplished by defining a weak symbol for getentropy(), checking that the compiler can generate the rdseed instruction even if the compilation platform does not natively support it, and checking for the availability of the rdseed instruction on the execution platform using rte_cpu_get_flag_enabled().

If neither getentropy() or rdseed is available, rte_get_timer_cycles() will be continue to be used as the entropy source.

This also allows non-Mason builds to use getentropy().

---
 config/x86/meson.build             |  7 +++++++
 lib/librte_eal/common/rte_random.c | 29 ++++++++++++++++++++++++-----
 lib/librte_eal/meson.build         |  3 ---
 mk/rte.cpuflags.mk                 |  8 ++++++++
 4 files changed, 39 insertions(+), 8 deletions(-)
config/x86/meson.build +7 -0
index adc857ba2..214b16f2a 100644
@@ -20,6 +20,13 @@ if cc.get_define('__SSE4_2__', args: machine_args) == ''
        machine_args += '-msse4'
 endif
 
+# set -mrdseed if necessary so _rdseed32_step compiles if the
+# compilation host does not support the RDSEED instruction.
+if cc.get_define('__RDSEED__', args: machine_args) == '' and cc.has_argument('-mrdseed')
+       machine_args += '-mrdseed'
+       message('RDSEED not enabled by default, explicitly setting -mrdseed')
+endif
+
 base_flags = ['SSE', 'SSE2', 'SSE3','SSSE3', 'SSE4_1', 'SSE4_2']
 foreach f:base_flags
        dpdk_conf.set('RTE_MACHINE_CPUFLAG_' + f, 1)
lib/librte_eal/common/rte_random.c +5 -0
index 57ec8fb2b..40f8b5aab 100644
@@ -25,6 +25,8 @@ struct rte_rand_state {
 
 static struct rte_rand_state rand_states[RTE_MAX_LCORE];
 
+__rte_weak int getentropy(void *__buffer, size_t __length);
+
 static uint32_t
 __rte_rand_lcg32(uint32_t *seed)
 {
@@ -176,10 +178,24 @@ rte_rand_max(uint64_t upper_bound)
        return res;
 }
 
+/* Use rte_get_timer_cycles() if the system does not have
+ * genentropy() or the rdseed instruction.
+ */

lib/librte_eal/common/rte_random.c rte_rand_max(uint64_t upper_bound) · line 193

Quoted 10 lines
lib/librte_eal/common/rte_random.c +10 -0
+__rte_weak int
+getentropy(void *__buffer, size_t __length __rte_unused)
+{
+       uint64_t *ge_seed = __buffer;
+#ifdef RTE_MACHINE_CPUFLAG_RDSEED
+       if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_RDSEED))
+               return -1;
+#endif
+       *ge_seed = rte_get_timer_cycles();
+       return 0;

IMO, we need to create a new arch EAL abstraction to get uint64_t random number. Reason being:
1) ARMv8.5 supports similar instruction
https://developer.arm.com/docs/ddi0595/c/aarch64-system-registers/rndr
2) Avoid #ifdef clutter in common code.

Abstraction can return a random uint64_t number. Based on the arch capabilities, it can hook
to specialized instruction or rte_get_timer_cycles()

#6 lore raw
Quoted 68 lines · Jerin Jacob
Quoted 67 lines · Dan Gora

Instead of choosing to use getentropy() or the rdseed instruction for the random number generator entropy source using compilation flags, determine the best source at run time.

This is accomplished by defining a weak symbol for getentropy(), checking that the compiler can generate the rdseed instruction even if the compilation platform does not natively support it, and checking for the availability of the rdseed instruction on the execution platform using rte_cpu_get_flag_enabled().

If neither getentropy() or rdseed is available, rte_get_timer_cycles() will be continue to be used as the entropy source.

This also allows non-Mason builds to use getentropy().

---
  config/x86/meson.build             |  7 +++++++
  lib/librte_eal/common/rte_random.c | 29 ++++++++++++++++++++++++-----
  lib/librte_eal/meson.build         |  3 ---
  mk/rte.cpuflags.mk                 |  8 ++++++++
  4 files changed, 39 insertions(+), 8 deletions(-)
config/x86/meson.build +7 -0
index adc857ba2..214b16f2a 100644
@@ -20,6 +20,13 @@ if cc.get_define('__SSE4_2__', args: machine_args) == ''
         machine_args += '-msse4'
  endif
 
+# set -mrdseed if necessary so _rdseed32_step compiles if the
+# compilation host does not support the RDSEED instruction.
+if cc.get_define('__RDSEED__', args: machine_args) == '' and cc.has_argument('-mrdseed')
+       machine_args += '-mrdseed'
+       message('RDSEED not enabled by default, explicitly setting -mrdseed')
+endif
+
  base_flags = ['SSE', 'SSE2', 'SSE3','SSSE3', 'SSE4_1', 'SSE4_2']
  foreach f:base_flags
         dpdk_conf.set('RTE_MACHINE_CPUFLAG_' + f, 1)
lib/librte_eal/common/rte_random.c +11 -0
index 57ec8fb2b..40f8b5aab 100644
@@ -25,6 +25,8 @@ struct rte_rand_state {
 
  static struct rte_rand_state rand_states[RTE_MAX_LCORE];
 
+__rte_weak int getentropy(void *__buffer, size_t __length);
+
  static uint32_t
  __rte_rand_lcg32(uint32_t *seed)
  {
@@ -176,10 +178,24 @@ rte_rand_max(uint64_t upper_bound)
         return res;
  }
 
+/* Use rte_get_timer_cycles() if the system does not have
+ * genentropy() or the rdseed instruction.
+ */
+__rte_weak int
+getentropy(void *__buffer, size_t __length __rte_unused)
+{
+       uint64_t *ge_seed = __buffer;
+#ifdef RTE_MACHINE_CPUFLAG_RDSEED
+       if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_RDSEED))
Quoted 8 lines

lib/librte_eal/common/rte_random.c rte_rand_max(uint64_t upper_bound) · line 193

Quoted 4 lines
lib/librte_eal/common/rte_random.c +4 -0
+               return -1;
+#endif
+       *ge_seed = rte_get_timer_cycles();
+       return 0;

IMO, we need to create a new arch EAL abstraction to get uint64_t random number. Reason being:
1) ARMv8.5 supports similar instruction
https://developer.arm.com/docs/ddi0595/c/aarch64-system-registers/rndr

How many ARMv8.5 system will there be running a libc older than 2.25 or a Linux kernel older than 3.17?

Quoted 5 lines

2) Avoid #ifdef clutter in common code.

Abstraction can return a random uint64_t number. Based on the arch capabilities, it can hook
to specialized instruction or rte_get_timer_cycles()

#7 lore raw
Quoted 80 lines · Mattias Rönnblom
Quoted 68 lines · Jerin Jacob
Quoted 67 lines · Dan Gora

Instead of choosing to use getentropy() or the rdseed instruction for the random number generator entropy source using compilation flags, determine the best source at run time.

This is accomplished by defining a weak symbol for getentropy(), checking that the compiler can generate the rdseed instruction even if the compilation platform does not natively support it, and checking for the availability of the rdseed instruction on the execution platform using rte_cpu_get_flag_enabled().

If neither getentropy() or rdseed is available, rte_get_timer_cycles() will be continue to be used as the entropy source.

This also allows non-Mason builds to use getentropy().

---
  config/x86/meson.build             |  7 +++++++
  lib/librte_eal/common/rte_random.c | 29 ++++++++++++++++++++++++-----
  lib/librte_eal/meson.build         |  3 ---
  mk/rte.cpuflags.mk                 |  8 ++++++++
  4 files changed, 39 insertions(+), 8 deletions(-)
config/x86/meson.build +7 -0
index adc857ba2..214b16f2a 100644
@@ -20,6 +20,13 @@ if cc.get_define('__SSE4_2__', args: machine_args) == ''
         machine_args += '-msse4'
  endif
 
+# set -mrdseed if necessary so _rdseed32_step compiles if the
+# compilation host does not support the RDSEED instruction.
+if cc.get_define('__RDSEED__', args: machine_args) == '' and cc.has_argument('-mrdseed')
+       machine_args += '-mrdseed'
+       message('RDSEED not enabled by default, explicitly setting -mrdseed')
+endif
+
  base_flags = ['SSE', 'SSE2', 'SSE3','SSSE3', 'SSE4_1', 'SSE4_2']
  foreach f:base_flags
         dpdk_conf.set('RTE_MACHINE_CPUFLAG_' + f, 1)
lib/librte_eal/common/rte_random.c +11 -0
index 57ec8fb2b..40f8b5aab 100644
@@ -25,6 +25,8 @@ struct rte_rand_state {
 
  static struct rte_rand_state rand_states[RTE_MAX_LCORE];
 
+__rte_weak int getentropy(void *__buffer, size_t __length);
+
  static uint32_t
  __rte_rand_lcg32(uint32_t *seed)
  {
@@ -176,10 +178,24 @@ rte_rand_max(uint64_t upper_bound)
         return res;
  }
 
+/* Use rte_get_timer_cycles() if the system does not have
+ * genentropy() or the rdseed instruction.
+ */
+__rte_weak int
+getentropy(void *__buffer, size_t __length __rte_unused)
+{
+       uint64_t *ge_seed = __buffer;
+#ifdef RTE_MACHINE_CPUFLAG_RDSEED
+       if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_RDSEED))
Quoted 8 lines

lib/librte_eal/common/rte_random.c rte_rand_max(uint64_t upper_bound) · line 193

Quoted 4 lines
lib/librte_eal/common/rte_random.c +4 -0
+               return -1;
+#endif
+       *ge_seed = rte_get_timer_cycles();
+       return 0;

IMO, we need to create a new arch EAL abstraction to get uint64_t random number. Reason being:
1) ARMv8.5 supports similar instruction
https://developer.arm.com/docs/ddi0595/c/aarch64-system-registers/rndr

Quoted 2 lines

How many ARMv8.5 system will there be running a libc older than 2.25 or a Linux kernel older than 3.17?

Probably none.

Quoted 7 lines
Quoted 7 lines

2) Avoid #ifdef clutter in common code.

Abstraction can return a random uint64_t number. Based on the arch capabilities, it can hook
to specialized instruction or rte_get_timer_cycles()

#8 lore raw
Quoted 80 lines · Jerin Jacob
Quoted 78 lines · Mattias Rönnblom
Quoted 76 lines · Jerin Jacob
Quoted 61 lines · Dan Gora

Instead of choosing to use getentropy() or the rdseed instruction for the random number generator entropy source using compilation flags, determine the best source at run time.

This is accomplished by defining a weak symbol for getentropy(), checking that the compiler can generate the rdseed instruction even if the compilation platform does not natively support it, and checking for the availability of the rdseed instruction on the execution platform using rte_cpu_get_flag_enabled().

If neither getentropy() or rdseed is available, rte_get_timer_cycles() will be continue to be used as the entropy source.

This also allows non-Mason builds to use getentropy().

---
   config/x86/meson.build             |  7 +++++++
   lib/librte_eal/common/rte_random.c | 29 ++++++++++++++++++++++++-----
   lib/librte_eal/meson.build         |  3 ---
   mk/rte.cpuflags.mk                 |  8 ++++++++
   4 files changed, 39 insertions(+), 8 deletions(-)
config/x86/meson.build +7 -0
index adc857ba2..214b16f2a 100644
@@ -20,6 +20,13 @@ if cc.get_define('__SSE4_2__', args: machine_args) == ''
          machine_args += '-msse4'
   endif
 
+# set -mrdseed if necessary so _rdseed32_step compiles if the
+# compilation host does not support the RDSEED instruction.
+if cc.get_define('__RDSEED__', args: machine_args) == '' and cc.has_argument('-mrdseed')
+       machine_args += '-mrdseed'
+       message('RDSEED not enabled by default, explicitly setting -mrdseed')
+endif
+
   base_flags = ['SSE', 'SSE2', 'SSE3','SSSE3', 'SSE4_1', 'SSE4_2']
   foreach f:base_flags
          dpdk_conf.set('RTE_MACHINE_CPUFLAG_' + f, 1)
lib/librte_eal/common/rte_random.c +5 -0
index 57ec8fb2b..40f8b5aab 100644
@@ -25,6 +25,8 @@ struct rte_rand_state {
 
   static struct rte_rand_state rand_states[RTE_MAX_LCORE];
 
+__rte_weak int getentropy(void *__buffer, size_t __length);
+
   static uint32_t
   __rte_rand_lcg32(uint32_t *seed)
   {
@@ -176,10 +178,24 @@ rte_rand_max(uint64_t upper_bound)
          return res;
   }
 
+/* Use rte_get_timer_cycles() if the system does not have
+ * genentropy() or the rdseed instruction.
+ */

lib/librte_eal/common/rte_random.c rte_rand_max(uint64_t upper_bound) · line 193

Quoted 10 lines
lib/librte_eal/common/rte_random.c +10 -0
+__rte_weak int
+getentropy(void *__buffer, size_t __length __rte_unused)
+{
+       uint64_t *ge_seed = __buffer;
+#ifdef RTE_MACHINE_CPUFLAG_RDSEED
+       if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_RDSEED))
+               return -1;
+#endif
+       *ge_seed = rte_get_timer_cycles();
+       return 0;

IMO, we need to create a new arch EAL abstraction to get uint64_t random number. Reason being:
1) ARMv8.5 supports similar instruction
https://developer.arm.com/docs/ddi0595/c/aarch64-system-registers/rndr

Quoted 3 lines
Quoted 2 lines

How many ARMv8.5 system will there be running a libc older than 2.25 or a Linux kernel older than 3.17?

Probably none.

In that case getentropy() will always available, and having ARMv8.5 rndr support would be pointless, as far as rte_random.c is concerned.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

2) Avoid #ifdef clutter in common code.

Abstraction can return a random uint64_t number. Based on the arch capabilities, it can hook
to specialized instruction or rte_get_timer_cycles()

#9 lore raw
Quoted 82 lines · Mattias Rönnblom
Quoted 80 lines · Jerin Jacob
Quoted 78 lines · Mattias Rönnblom
Quoted 76 lines · Jerin Jacob
Quoted 61 lines · Dan Gora

Instead of choosing to use getentropy() or the rdseed instruction for the random number generator entropy source using compilation flags, determine the best source at run time.

This is accomplished by defining a weak symbol for getentropy(), checking that the compiler can generate the rdseed instruction even if the compilation platform does not natively support it, and checking for the availability of the rdseed instruction on the execution platform using rte_cpu_get_flag_enabled().

If neither getentropy() or rdseed is available, rte_get_timer_cycles() will be continue to be used as the entropy source.

This also allows non-Mason builds to use getentropy().

---
   config/x86/meson.build             |  7 +++++++
   lib/librte_eal/common/rte_random.c | 29 ++++++++++++++++++++++++-----
   lib/librte_eal/meson.build         |  3 ---
   mk/rte.cpuflags.mk                 |  8 ++++++++
   4 files changed, 39 insertions(+), 8 deletions(-)
config/x86/meson.build +7 -0
index adc857ba2..214b16f2a 100644
@@ -20,6 +20,13 @@ if cc.get_define('__SSE4_2__', args: machine_args) == ''
          machine_args += '-msse4'
   endif
 
+# set -mrdseed if necessary so _rdseed32_step compiles if the
+# compilation host does not support the RDSEED instruction.
+if cc.get_define('__RDSEED__', args: machine_args) == '' and cc.has_argument('-mrdseed')
+       machine_args += '-mrdseed'
+       message('RDSEED not enabled by default, explicitly setting -mrdseed')
+endif
+
   base_flags = ['SSE', 'SSE2', 'SSE3','SSSE3', 'SSE4_1', 'SSE4_2']
   foreach f:base_flags
          dpdk_conf.set('RTE_MACHINE_CPUFLAG_' + f, 1)
lib/librte_eal/common/rte_random.c +5 -0
index 57ec8fb2b..40f8b5aab 100644
@@ -25,6 +25,8 @@ struct rte_rand_state {
 
   static struct rte_rand_state rand_states[RTE_MAX_LCORE];
 
+__rte_weak int getentropy(void *__buffer, size_t __length);
+
   static uint32_t
   __rte_rand_lcg32(uint32_t *seed)
   {
@@ -176,10 +178,24 @@ rte_rand_max(uint64_t upper_bound)
          return res;
   }
 
+/* Use rte_get_timer_cycles() if the system does not have
+ * genentropy() or the rdseed instruction.
+ */

lib/librte_eal/common/rte_random.c rte_rand_max(uint64_t upper_bound) · line 193

Quoted 10 lines
lib/librte_eal/common/rte_random.c +10 -0
+__rte_weak int
+getentropy(void *__buffer, size_t __length __rte_unused)
+{
+       uint64_t *ge_seed = __buffer;
+#ifdef RTE_MACHINE_CPUFLAG_RDSEED
+       if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_RDSEED))
+               return -1;
+#endif
+       *ge_seed = rte_get_timer_cycles();
+       return 0;

IMO, we need to create a new arch EAL abstraction to get uint64_t random number. Reason being:
1) ARMv8.5 supports similar instruction
https://developer.arm.com/docs/ddi0595/c/aarch64-system-registers/rndr

Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

How many ARMv8.5 system will there be running a libc older than 2.25 or a Linux kernel older than 3.17?

Probably none.

In that case getentropy() will always available, and having ARMv8.5 rndr support would be pointless, as far as rte_random.c is concerned.

Yes. My initial comment was based on this patch context. I just checked the lib/librte_eal/common/rte_random.c,
if this get activated only libc older than 2.25 or kernel < 3.17, it is not a concern for me.

Quoted 9 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

2) Avoid #ifdef clutter in common code.

Abstraction can return a random uint64_t number. Based on the arch capabilities, it can hook
to specialized instruction or rte_get_timer_cycles()