From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from dpdk.org (dpdk.org [92.243.14.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 13085A05A0;
	Tue, 21 Apr 2020 22:41:54 +0200 (CEST)
Received: from [92.243.14.124] (localhost [127.0.0.1])
	by dpdk.org (Postfix) with ESMTP id E7FD41D51D;
	Tue, 21 Apr 2020 22:41:53 +0200 (CEST)
Received: from mail-qk1-f196.google.com (mail-qk1-f196.google.com
 [209.85.222.196]) by dpdk.org (Postfix) with ESMTP id 618B21D533
 for <dev@dpdk.org>; Tue, 21 Apr 2020 22:41:52 +0200 (CEST)
Received: by mail-qk1-f196.google.com with SMTP id v7so201789qkc.0
 for <dev@dpdk.org>; Tue, 21 Apr 2020 13:41:52 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025;
 h=sender:from:to:cc:subject:date:message-id:in-reply-to:references
 :mime-version:content-transfer-encoding;
 bh=iBuOXQLMOzTjK6zGwpmTn2glHuXf76ojkSu68vEj5KE=;
 b=W/e24SAaRoqu6Br5zMZPx7k6yP3X++FP3a1CxUoZ2N4SWEUfRE+cxuNqgKyNCzWug6
 x3v2HTfpWvrLWAMGOcZXFg5s9y5NOP9a59b1ljWbJh9bZliEGkwHdOmCy2iln4n5y66X
 Y+pEMUfqUUGWvYr/91Xt66whsdW22TkWIPGQeJ9+pGdI3krtdn6DGtgD4ERUZrGUSfBJ
 WkUpGpVOcEA97Oqs4OClkOCiytrgLWyuVyO5zRoNTYUI9eXojA87NnGrxxJ8RStVh5St
 FuZm61UdtnmygElZBY0oOe1W2nMNewRi47lf9g+Z59Pj1HlQDkBq1JqN02ql0WXJ59H/
 vmCQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20161025;
 h=x-gm-message-state:sender:from:to:cc:subject:date:message-id
 :in-reply-to:references:mime-version:content-transfer-encoding;
 bh=iBuOXQLMOzTjK6zGwpmTn2glHuXf76ojkSu68vEj5KE=;
 b=ti0nmgwzWshPGcc3y9Hgj/FfS81ueS5PyHK8NMbAVJR1dihnXlPs6CB2MMgKJyatPI
 RzFfd3zgrmjYVuEiB0m3YUxCaDOrYJOVfhDPPto9C5lymGb8Fj6N8GgbzvcsyzoNC3If
 QegYSBpI1lvkFfqsYkbqJCXkbTVHD+jjrs6AOVJMqNH1IqXkFFjeH6UY0qv4CED1oOh0
 im96WbAIj7jKLGA1eShAw5fiYSj+H5HMEklKsnI8RSlkQJ0I1wELPFdoLiRmVBpxAJDJ
 5wZg4z/+bpqB17lPX5Ghu6IveQHHGgijyzxWj9R35t6/r4RZejfEqilcszh5MJsyKZrz
 EoFA==
X-Gm-Message-State: AGi0PuYoFqCjbMOgK//MRem4IuzXudCiT4non1skgmpZOsqMADc7asNB
 pEJOODeBHC3vNztYhHOY4A2uRNj5GRY=
X-Google-Smtp-Source: APiQypKnN/lz1FO/g9AbFFOvp92y2AmAyQPQPJ7CHcTeQISPz50oqC+Gblsh6fSZeLMFMzeOXGAN5A==
X-Received: by 2002:a05:620a:1396:: with SMTP id
 k22mr22603400qki.424.1587501711540; 
 Tue, 21 Apr 2020 13:41:51 -0700 (PDT)
Received: from localhost.localdomain ([168.121.99.161])
 by smtp.gmail.com with ESMTPSA id d23sm2431067qkj.26.2020.04.21.13.41.49
 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128);
 Tue, 21 Apr 2020 13:41:51 -0700 (PDT)
From: Dan Gora <dg@adax.com>
To: dev@dpdk.org,
 =?UTF-8?q?Mattias=20R=C3=B6nnblom?= <mattias.ronnblom@ericsson.com>
Cc: David Marchand <david.marchand@redhat.com>,
 Jerin Jacob <jerinjacobk@gmail.com>, Dan Gora <dg@adax.com>
Date: Tue, 21 Apr 2020 17:41:15 -0300
Message-Id: <20200421204115.31950-3-dg@adax.com>
X-Mailer: git-send-email 2.24.1.425.g7034cd094b
In-Reply-To: <20200421204115.31950-1-dg@adax.com>
References: <20200421195446.1730-1-dg@adax.com>
 <20200421204115.31950-1-dg@adax.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Subject: [dpdk-dev] [PATCH v2 2/2] eal: resolve getentropy at run time for
	random seed
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org
Sender: "dev" <dev-bounces@dpdk.org>

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.

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@adax.com>
---
 lib/librte_eal/common/rte_random.c | 35 +++++++++++++++++++++++++-----
 lib/librte_eal/meson.build         |  3 ---
 2 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/lib/librte_eal/common/rte_random.c b/lib/librte_eal/common/rte_random.c
index 2c84c8527..3a05b23db 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,40 @@ 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)
+		return -1;
+
+	sym = dlsym(handle, "getentropy");
+	if (!sym || !*sym) {
+		/* Cannot resolve getentropy */
+		dlclose(handle);
+		return -1;
+	}
+
+	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
-- 
2.24.1.425.g7034cd094b