[dpdk-dev] [PATCH 2/2] eal: resolve getentropy at run time for random seed

Mattias Rönnblom mattias.ronnblom at ericsson.com
Wed Apr 22 10:28:43 CEST 2020


On 2020-04-21 21:54, Dan Gora wrote:
> The getentropy() function was introduced into glibc v2.25 and so is
> not available on all supported platforms.  Previously, if DPDK was
> compiled (using meson) on a system which has getentropy(), it would
> introduce a dependency on glibc v2.25 which would prevent that binary
> from running on a system with an older glibc.  Similarly if DPDK was
> compiled on a system which did not have getentropy(), getentropy()
> could not be used even if the execution system supported it.
>
> Introduce a new static function, __rte_getentropy() which will try to
> resolve the getentropy() function dynamically using dlopen()/dlsym(),
> returning failure if the getentropy() function cannot be resolved or
> if it fails.


Two other options: providing a DPDK-native syscall wrapper for 
getrandom(), or falling back to reading /dev/urandom. Have you 
considered any of those two options? If so, why do you prefer 
dlopen()/dlsym()?


Failure to run on old libc seems like a non-issue to me.


> This also allows getentropy() to be used as the random seed source
> when the traditional Makefile build for DPDK is used.
>
> Signed-off-by: Dan Gora <dg at adax.com>
> ---
>   lib/librte_eal/common/rte_random.c | 33 ++++++++++++++++++++++++------
>   lib/librte_eal/meson.build         |  3 ---
>   2 files changed, 27 insertions(+), 9 deletions(-)
>
> diff --git a/lib/librte_eal/common/rte_random.c b/lib/librte_eal/common/rte_random.c
> index df02f1307..f8203f4c7 100644
> --- a/lib/librte_eal/common/rte_random.c
> +++ b/lib/librte_eal/common/rte_random.c
> @@ -7,6 +7,7 @@
>   #endif
>   #include <stdlib.h>
>   #include <unistd.h>
> +#include <dlfcn.h>
>   
>   #include <rte_branch_prediction.h>
>   #include <rte_cycles.h>
> @@ -176,18 +177,38 @@ rte_rand_max(uint64_t upper_bound)
>   	return res;
>   }
>   
> +/* Try to use the getentropy() function from glibc >= 2.25 */
> +static int
> +__rte_getentropy(uint64_t *ge_seed)
> +{
> +	void *handle = NULL;
> +	void **sym;
> +	int (*getentropy_p)(void *__buffer, size_t __length);
> +	int gc_rc;
> +
> +	handle = dlopen("libc.so.6", RTLD_LAZY);
> +	if (!handle)
!= NULL
> +		return -1;
> +
> +	sym = dlsym(handle, "getentropy");
> +	if (!sym || !*sym)

!= NULL again


dlsym() returns a "void *". The man page says nothing about 
de-referencing it. Is that allowed?


> +		/* Cannot resolve getentropy */
> +		return -1;
Missing a dlclose()
> +
> +	getentropy_p = (int (*)(void *, size_t)) sym;
> +	gc_rc = (*getentropy_p)((void *)ge_seed, sizeof(*ge_seed));
> +	dlclose(handle);
> +	return gc_rc;
> +}
> +
>   static uint64_t
>   __rte_random_initial_seed(void)
>   {
> -#ifdef RTE_LIBEAL_USE_GETENTROPY
> -	int ge_rc;
>   	uint64_t ge_seed;
>   
> -	ge_rc = getentropy(&ge_seed, sizeof(ge_seed));
> -
> -	if (ge_rc == 0)
> +	if (__rte_getentropy(&ge_seed) == 0)
>   		return ge_seed;
> -#endif
> +
>   #if defined(RTE_ARCH_X86)
>   	/* first fallback: rdseed instruction, if available */
>   	if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_RDSEED)) {
> diff --git a/lib/librte_eal/meson.build b/lib/librte_eal/meson.build
> index 0267c3b9d..748359b8c 100644
> --- a/lib/librte_eal/meson.build
> +++ b/lib/librte_eal/meson.build
> @@ -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




More information about the dev mailing list