From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 5BC42A00C2; Thu, 23 Apr 2020 01:44:33 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id E321A1D173; Thu, 23 Apr 2020 01:44:26 +0200 (CEST) Received: from mail-qk1-f193.google.com (mail-qk1-f193.google.com [209.85.222.193]) by dpdk.org (Postfix) with ESMTP id 6AD5E1D164 for ; Thu, 23 Apr 2020 01:44:25 +0200 (CEST) Received: by mail-qk1-f193.google.com with SMTP id o19so4515713qkk.5 for ; Wed, 22 Apr 2020 16:44:25 -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=xdQN6XP1bQ7p/HTW903byCKzoWIoVhknTniEKCN0GCA=; b=Nv4Mc9hHlFhmDIseXc+jjTZWbsh3W0NfmBdgR5inzJ1WZxfbtkluBzpw4esW77xHLp 948aVPfRqNjTE7QdhrPYS70ZcSBqshUZ+HZE4ehk2BlfyDEKw7zOYVuD1gEZYCefvdXl J2vBu8hNSds2VsPisRtGLrIO3Q7hqrAkPsqCTfFzs8XkK2y8R89d/fqKfZcAo6QgUE8m taWbKhbuVM2Vln39WAjcc9CN85Y8JLGoAPw58j1HKoLk2CSJHov89nNbzIHucgMhtzFN Js8FXqYu9TDkgnSOHRnAYtCVtujYvGvXNsIglV6Hz0DVcLqqpAVWfb7nA4Z2UTkhPIgu LzWQ== 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=xdQN6XP1bQ7p/HTW903byCKzoWIoVhknTniEKCN0GCA=; b=IIiCsXk9kuME3J7abNEuN97fxc76umptESRF88utT9a0M6s//PcKGbxSp7ZrEH5ci0 8YTQG7IjxCjkbFQYjK3nvoRseAsTqN7TXEP91vB9Gr/GGz8AFY83pgkI95F+3jKPhIAA yk8pil7LMYroWDBWv9Fd3N8Wlb0by+qJz0UNiul5HoVgTLqnchkzMNn/pob/RECF+pXH /PRODs02PpUGsH44ft3RKYcOOIAEKfN/lGR11iPAYqzlLOrRR84sCnMpKCmw6r7dAvEg 7SDICoj3LDwgDSHrekGpfJIlfZdkSxs6em9A7Mec0ZrqR/7Bn24z/nqY8DXlCFoUrbYX aNFw== X-Gm-Message-State: AGi0PubexI9gU2nS7FPXyrepI2MqOC6XaWu5GsHn5h0GrwKtQyxWq8xj vGNGN/4JUWROzl24hVtuJfxrINGC2FE= X-Google-Smtp-Source: APiQypJ5Udu2fM7WXIE9sjTvAR7IKJm2ICC/hY4HVKIppsHjR8Ijm431+if58IM3AxPxBZv2o50CiQ== X-Received: by 2002:a37:a348:: with SMTP id m69mr878999qke.31.1587599064556; Wed, 22 Apr 2020 16:44:24 -0700 (PDT) Received: from localhost.localdomain ([168.121.99.161]) by smtp.gmail.com with ESMTPSA id t15sm614263qtc.64.2020.04.22.16.44.22 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 Apr 2020 16:44:24 -0700 (PDT) From: Dan Gora To: dev@dpdk.org, =?UTF-8?q?Mattias=20R=C3=B6nnblom?= Cc: David Marchand , Jerin Jacob , Dan Gora Date: Wed, 22 Apr 2020 20:42:54 -0300 Message-Id: <20200422234255.7066-3-dg@adax.com> X-Mailer: git-send-email 2.24.1.425.g7034cd094b In-Reply-To: <20200422234255.7066-1-dg@adax.com> References: <20200421195446.1730-1-dg@adax.com> <20200422234255.7066-1-dg@adax.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v4 2/2] eal: emulate glibc getentropy for initial random seed X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" 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() to emulate the glibc getentropy() function by reading from /dev/urandom to remove this dependency on the glibc version. Since __rte_genentropy() should never fail, the rdseed method is tried first. Signed-off-by: Dan Gora --- lib/librte_eal/common/rte_random.c | 62 ++++++++++++++++++++++++++---- lib/librte_eal/meson.build | 3 -- 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/lib/librte_eal/common/rte_random.c b/lib/librte_eal/common/rte_random.c index 2c84c8527..f043adf03 100644 --- a/lib/librte_eal/common/rte_random.c +++ b/lib/librte_eal/common/rte_random.c @@ -7,6 +7,7 @@ #endif #include #include +#include #include #include @@ -176,20 +177,61 @@ rte_rand_max(uint64_t upper_bound) return res; } +/* Emulate glibc getentropy() using /dev/urandom */ +static int +__rte_getentropy(void *buffer, size_t length) +{ + uint8_t *start = buffer; + uint8_t *end; + ssize_t bytes; + int fd; + int rc = -1; + + if (length > 256) { + errno = EIO; + return -1; + } + + fd = open("/dev/urandom", O_RDONLY); + if (fd < 0) { + errno = ENODEV; + return -1; + } + + end = start + length; + while (start < end) { + bytes = read(fd, start, end - start); + if (bytes < 0) { + if (errno == EINTR) + /* Supposedly cannot be interrupted by + * a signal, but just in case... + */ + continue; + else + goto out; + } + if (bytes == 0) { + /* no more bytes available, should not happen under + * normal circumstances. + */ + errno = EIO; + goto out; + } + start += bytes; + } + rc = 0; + errno = 0; +out: + close(fd); + return 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) - return ge_seed; -#endif #if defined(RTE_ARCH_X86) - /* first fallback: rdseed instruction, if available */ if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_RDSEED)) { unsigned int rdseed_low; unsigned int rdseed_high; @@ -200,6 +242,10 @@ __rte_random_initial_seed(void) ((uint64_t)rdseed_high << 32); } #endif + /* first fallback: read from /dev/urandom.. */ + if (__rte_getentropy(&ge_seed, sizeof(ge_seed)) == 0) + return ge_seed; + /* second fallback: seed using rdtsc */ return rte_get_tsc_cycles(); } 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 ') - 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