Message ID | 20190514092046.30808-4-mattias.ronnblom@ericsson.com (mailing list archive) |
---|---|
State | Superseded, archived |
Delegated to: | Thomas Monjalon |
Headers |
Return-Path: <dev-bounces@dpdk.org> X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 1BA245F1D; Tue, 14 May 2019 11:21:49 +0200 (CEST) Received: from sesbmg23.ericsson.net (sesbmg23.ericsson.net [193.180.251.37]) by dpdk.org (Postfix) with ESMTP id 4E0F75F1B for <dev@dpdk.org>; Tue, 14 May 2019 11:21:48 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1557825708; x=1560417708; h=From:Sender:Reply-To:Subject:Date:Message-ID:To:CC:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=h5GAOK7LZyMqXa8dFI/Hz/qrD/CKwXlWzZPkx0rt9vs=; b=Y16jGUA83eUHMOdBwIOBC/SHWnedtcDSmcw1uRIEhvN2cA7uN5BMoI89vGOubD9p 4dNe4LHH0sBa2zxpx07JnEt6sUdUEV0DhU0Ys2qGI8DBJRwv9L+Fs9OV38YG45NX xwHGW7f60c377Z5/SBG28CrGkkQLeophQSzJ3I/rWmw=; X-AuditID: c1b4fb25-3b1ff700000029f0-03-5cda88ab9f24 Received: from ESESBMB503.ericsson.se (Unknown_Domain [153.88.183.116]) by sesbmg23.ericsson.net (Symantec Mail Security) with SMTP id 60.30.10736.BA88ADC5; Tue, 14 May 2019 11:21:47 +0200 (CEST) Received: from ESESBMR503.ericsson.se (153.88.183.135) by ESESBMB503.ericsson.se (153.88.183.170) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Tue, 14 May 2019 11:21:47 +0200 Received: from ESESSMB503.ericsson.se (153.88.183.164) by ESESBMR503.ericsson.se (153.88.183.135) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Tue, 14 May 2019 11:21:47 +0200 Received: from selio1a020.lmera.ericsson.se (153.88.183.153) by smtp.internal.ericsson.com (153.88.183.191) with Microsoft SMTP Server id 15.1.1713.5 via Frontend Transport; Tue, 14 May 2019 11:21:47 +0200 Received: from breslau.lmera.ericsson.se (breslau.lmera.ericsson.se [150.132.109.241]) by selio1a020.lmera.ericsson.se (8.15.1+Sun/8.15.1) with ESMTP id x4E9LQN7027252; Tue, 14 May 2019 11:21:46 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= <mattias.ronnblom@ericsson.com> To: <dev@dpdk.org> CC: <nhorman@tuxdriver.com>, <stephen@networkplumber.org>, <david.marchand@redhat.com>, <bruce.richardson@intel.com>, =?utf-8?q?Mattias_R=C3=B6nnblom?= <mattias.ronnblom@ericsson.com> Date: Tue, 14 May 2019 11:20:43 +0200 Message-ID: <20190514092046.30808-4-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190514092046.30808-1-mattias.ronnblom@ericsson.com> References: <20190508181014.7dde7580@xps13> <20190514092046.30808-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrOLMWRmVeSWpSXmKPExsUyM2J7ie7qjlsxBkuabS1urLK32L6ii83i 3aftTBa3mk+yWSy+I+fA6vFrwVJWj8V7XjJ59Jycx+Txft9VNo8r31czBrBGcdmkpOZklqUW 6dslcGW8XHiApWAVR0V35zqmBsY/bF2MHBwSAiYST3YndDFycQgJHGWUOLl9N3sXIyeQ841R 4lWLP0QCyH7xaC8rhHORUaK96TKUc5lRYtWF08wgLWwCnhKT33WzgNgiAkISSz9eZgcpYhbY CzRq3TywhLCAvcS1uz/BGlgEVCVad3Qxgdi8Ak4S13oawWokBOQlVm84AFbDKeAsMWnKLaib UiT+9DxigagXlDg58wmYzSygKdG6/Tc7hC0v0bx1NjNEvZbE/SVfmCcwCs9C0jILScssJC0L GJlXMYoWpxYn5aYbGeulFmUmFxfn5+nlpZZsYgRGx8Etv1V3MF5+43iIUYCDUYmH1y/9VowQ a2JZcWXuIUYJDmYlEd4oxRsxQrwpiZVVqUX58UWlOanFhxilOViUxHmjV++JERJITyxJzU5N LUgtgskycXBKNTC29AYesw5Vr1k03af8mWLPcv+OuI8C8moXdYu1gww3e5ivS+H69kkncn9Z xa3Wb72V/9/tTXB0qrN3Pf1p6dwFrnwKjSyHwtLPHUhJKtPN3u4Wk2TOpdO1elVpz8qu51OK U+bFMq4IOnfr6s2i/tcH1jCIK3rn8q0NlEuyvXl5orXO/U1cbUosxRmJhlrMRcWJAExYeTqK AgAA Subject: [dpdk-dev] [PATCH 3/6] eal: improve entropy for initial PRNG 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> |
Series |
Pseudo-random number generation improvements
|
|
Checks
Context | Check | Description |
---|---|---|
ci/checkpatch | success | coding style OK |
ci/Intel-compilation | fail | Compilation issues |
Commit Message
Mattias Rönnblom
May 14, 2019, 9:20 a.m. UTC
Replace the use of rte_get_timer_cycles() with getentropy() for
seeding the pseudo-random number generator. getentropy() provides a
more truly random value.
Suggested-by: Stephen Hemminger <stephen@networkplumber.org>
Signed-off-by: Mattias Rönnblom <mattias.ronnblom@ericsson.com>
---
lib/librte_eal/common/rte_random.c | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)
Comments
On 2019-05-14 11:20, Mattias Rönnblom wrote: > Replace the use of rte_get_timer_cycles() with getentropy() for > seeding the pseudo-random number generator. getentropy() provides a > more truly random value. > getentropy() doens't exist in libc versions earler than 2.25, and it also (like Stephen mentioned) requires the getrandom() syscall not available until Linux 3.17. If this is deemed too much of a limitation, I'll change to /dev/urandom-based seeding.
On Tue, May 14, 2019 at 11:20:43AM +0200, Mattias Rönnblom wrote: > Replace the use of rte_get_timer_cycles() with getentropy() for > seeding the pseudo-random number generator. getentropy() provides a > more truly random value. > > Suggested-by: Stephen Hemminger <stephen@networkplumber.org> > Signed-off-by: Mattias Rönnblom <mattias.ronnblom@ericsson.com> > --- > lib/librte_eal/common/rte_random.c | 12 +++++++++++- > 1 file changed, 11 insertions(+), 1 deletion(-) > > diff --git a/lib/librte_eal/common/rte_random.c b/lib/librte_eal/common/rte_random.c > index 4d3cf5226..aafc2f7ad 100644 > --- a/lib/librte_eal/common/rte_random.c > +++ b/lib/librte_eal/common/rte_random.c > @@ -3,6 +3,7 @@ > */ > > #include <stdlib.h> > +#include <unistd.h> > > #include <rte_branch_prediction.h> > #include <rte_cycles.h> > @@ -135,5 +136,14 @@ rte_rand(void) > > RTE_INIT(rte_rand_init) > { > - rte_srand(rte_get_timer_cycles()); > + uint64_t seed; > + int rc; > + > + rc = getentropy(&seed, sizeof(seed)); This API was only introduced in FreeBSD in v12, so you may want to have a define for enabling it, and (for meson) but in a check for cc.has_function(get_entropy). > + > + /* fall back to rdtsc in case of failure */ > + if (rc < 0) > + seed = rte_get_timer_cycles(); Rather than fallback immediately to time, what about putting in a call to HW random number generator support if available, e.g. rdseed instruction. > + > + rte_srand(seed); > } > -- > 2.17.1 >
On Tue, May 14, 2019 at 11:36:49AM +0200, Mattias Rönnblom wrote: > On 2019-05-14 11:20, Mattias Rönnblom wrote: > > Replace the use of rte_get_timer_cycles() with getentropy() for > > seeding the pseudo-random number generator. getentropy() provides a > > more truly random value. > > > > getentropy() doens't exist in libc versions earler than 2.25, and it also > (like Stephen mentioned) requires the getrandom() syscall not available > until Linux 3.17. > > If this is deemed too much of a limitation, I'll change to > /dev/urandom-based seeding. Since we are shifting over to meson builds more and more, I'd suggest just using function detection there with a very basic fallback (e.g. your time-based one) for cases where the function is not supported. /Bruce
On 2019-05-14 11:39, Bruce Richardson wrote: > On Tue, May 14, 2019 at 11:36:49AM +0200, Mattias Rönnblom wrote: >> On 2019-05-14 11:20, Mattias Rönnblom wrote: >>> Replace the use of rte_get_timer_cycles() with getentropy() for >>> seeding the pseudo-random number generator. getentropy() provides a >>> more truly random value. >>> >> >> getentropy() doens't exist in libc versions earler than 2.25, and it also >> (like Stephen mentioned) requires the getrandom() syscall not available >> until Linux 3.17. >> >> If this is deemed too much of a limitation, I'll change to >> /dev/urandom-based seeding. > > Since we are shifting over to meson builds more and more, I'd suggest just > using function detection there with a very basic fallback (e.g. your > time-based one) for cases where the function is not supported. > OK, fixed. Thanks. Would it be OK to always use the rdtsc fallback w/ make-based builds?
diff --git a/lib/librte_eal/common/rte_random.c b/lib/librte_eal/common/rte_random.c index 4d3cf5226..aafc2f7ad 100644 --- a/lib/librte_eal/common/rte_random.c +++ b/lib/librte_eal/common/rte_random.c @@ -3,6 +3,7 @@ */ #include <stdlib.h> +#include <unistd.h> #include <rte_branch_prediction.h> #include <rte_cycles.h> @@ -135,5 +136,14 @@ rte_rand(void) RTE_INIT(rte_rand_init) { - rte_srand(rte_get_timer_cycles()); + uint64_t seed; + int rc; + + rc = getentropy(&seed, sizeof(seed)); + + /* fall back to rdtsc in case of failure */ + if (rc < 0) + seed = rte_get_timer_cycles(); + + rte_srand(seed); }