From patchwork Fri Jun 28 09:01:22 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Mattias_R=C3=B6nnblom?= X-Patchwork-Id: 55589 Return-Path: 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 2800F1B9C3; Fri, 28 Jun 2019 11:01:46 +0200 (CEST) Received: from sessmg22.ericsson.net (sessmg22.ericsson.net [193.180.251.58]) by dpdk.org (Postfix) with ESMTP id DEFAD1B99C for ; Fri, 28 Jun 2019 11:01:31 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1561712491; x=1564304491; 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=oriMSYzD69gLHrQpWqsvfZuMqM7qy5GGOQ+W7kCH8aU=; b=axKpiD3JZAYuhYH8SAkjGKdcEpHhhwrBuA+d9/QHSsynBlE1wNpf3gJWv+EQo3c9 7BwO9AVrcbgf8fCf4afVUw5Tu20aTsAAznXnw4EHFhmfPzvmOaHJVn7+rqLW2aYA hVqy3DGCGvpFTNtrg1KUkklR0SJV+u5ssEVek+lwxOs=; X-AuditID: c1b4fb3a-b6bb79e00000189f-00-5d15d76ba0bd Received: from ESESSMB505.ericsson.se (Unknown_Domain [153.88.183.123]) by sessmg22.ericsson.net (Symantec Mail Security) with SMTP id 26.64.06303.B67D51D5; Fri, 28 Jun 2019 11:01:31 +0200 (CEST) Received: from ESESSMR504.ericsson.se (153.88.183.126) by ESESSMB505.ericsson.se (153.88.183.193) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Fri, 28 Jun 2019 11:01:29 +0200 Received: from ESESBMB502.ericsson.se (153.88.183.169) by ESESSMR504.ericsson.se (153.88.183.126) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Fri, 28 Jun 2019 11:01:29 +0200 Received: from selio1a020.lmera.ericsson.se (153.88.183.153) by smtp.internal.ericsson.com (153.88.183.185) with Microsoft SMTP Server id 15.1.1713.5 via Frontend Transport; Fri, 28 Jun 2019 11:01:30 +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 x5S91SYX019342; Fri, 28 Jun 2019 11:01:29 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , , , , , =?utf-8?q?Mattias_R=C3=B6nnblom?= Date: Fri, 28 Jun 2019 11:01:22 +0200 Message-ID: <20190628090124.16849-4-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190628090124.16849-1-mattias.ronnblom@ericsson.com> References: <20190605104400.24484-1-mattias.ronnblom@ericsson.com> <20190628090124.16849-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmpmkeLIzCtJLcpLzFFi42KZGbG9Wjf7umisQet2CYsbq+wttq/oYrN4 92k7k8Wt5pNsFovvyFl8enCCxYHN49eCpawei/e8ZPI4dnMau0fPyXlMHu/3XWXzuPJ9NWMA WxSXTUpqTmZZapG+XQJXxqypbcwFz0QrlndNZ2lgXCnUxcjJISFgIjH90mXWLkYuDiGBo4wS HQvOMUM43xgl5iyZhOB8PP2QCcK5yChx/G4DI4RzmVHizOYNrCDD2AQ8JSa/62YBsUUEhCSW frzMDlLELHCNUeLrpRfMIAlhASeJk+8Xs4PYLAKqEu+3r2ICsXmB4mtvzmKFuEpeYvWGA2D1 nALOEl1re8HqhQSqJX6eeMgGUS8ocXLmE7BlzAKaEq3bf7ND2PISzVtnM0PUa0ncX/KFeQKj 8CwkLbOQtMxC0rKAkXkVo2hxanFxbrqRkV5qUWZycXF+nl5easkmRmDUHNzy22oH48HnjocY BTgYlXh4U3eLxgqxJpYVV+YeYpTgYFYS4ZU8JxIrxJuSWFmVWpQfX1Sak1p8iFGag0VJnHe9 978YIYH0xJLU7NTUgtQimCwTB6dUA6PKDtcHN3Yp8Xz6VelWZODvwXHcRzsg6q6C6sp7PJuM Wf0WR9SfqxBV379s0SONyXU1eoEK/NnzT7gU/c+ZwrVU2yXT1CfXnC/dOFdutfzsu2zXFG6t Obtj8mf1TfEs4V5z5hsf4Ag+GSl6ZOpJr56S9vpXXb/KHO5PUpwspi/TJn07/FtmpBJLcUai oRZzUXEiAKHYzjmWAgAA Subject: [dpdk-dev] [PATCH v4 3/5] 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" 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() requires glibc 2.25 and Linux kernel 3.17. In case getentropy() is not found at compile time, or the relevant syscall fails in runtime, the rdseed machine instruction will be used as a fallback. rdseed is only available on x86 (Broadwell or later). In case it is not present, rte_get_timer_cycles() will be used as a second fallback. On non-Meson builds, getentropy() will not be used. Suggested-by: Bruce Richardson Suggested-by: Stephen Hemminger Signed-off-by: Mattias Rönnblom Acked-by: Bruce Richardson --- lib/librte_eal/common/rte_random.c | 36 +++++++++++++++++++++++++++++- lib/librte_eal/meson.build | 3 +++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/librte_eal/common/rte_random.c b/lib/librte_eal/common/rte_random.c index 4d3cf5226..e53d96d18 100644 --- a/lib/librte_eal/common/rte_random.c +++ b/lib/librte_eal/common/rte_random.c @@ -2,7 +2,11 @@ * Copyright(c) 2019 Ericsson AB */ +#ifdef RTE_MACHINE_CPUFLAG_RDSEED +#include +#endif #include +#include #include #include @@ -133,7 +137,37 @@ rte_rand(void) return __rte_rand_lfsr258(state); } +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 +#ifdef RTE_MACHINE_CPUFLAG_RDSEED + unsigned int rdseed_rc; + unsigned long long rdseed_seed; + + /* first fallback: rdseed instruction, if available */ + rdseed_rc = _rdseed64_step(&rdseed_seed); + + if (rdseed_rc == 1) + return (uint64_t)rdseed_seed; +#endif + /* second fallback: seed using rdtsc */ + return rte_get_timer_cycles(); +} + RTE_INIT(rte_rand_init) { - rte_srand(rte_get_timer_cycles()); + uint64_t seed; + + seed = __rte_random_initial_seed(); + + rte_srand(seed); } diff --git a/lib/librte_eal/meson.build b/lib/librte_eal/meson.build index fa36b20e0..ccd5b85b8 100644 --- a/lib/librte_eal/meson.build +++ b/lib/librte_eal/meson.build @@ -18,6 +18,9 @@ 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 sources = common_sources + env_sources objs = common_objs + env_objs headers = common_headers + env_headers