From patchwork Wed Jun 5 10:43:57 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: 54413 X-Patchwork-Delegate: thomas@monjalon.net 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 885361BAE5; Wed, 5 Jun 2019 12:44:41 +0200 (CEST) Received: from sessmg22.ericsson.net (sessmg22.ericsson.net [193.180.251.58]) by dpdk.org (Postfix) with ESMTP id 82A981BA8F for ; Wed, 5 Jun 2019 12:44:36 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1559731476; x=1562323476; 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=gMaWQ6JYI8hxP2SaHcXcVVHKvl9SYrXf9SEHy5UNcXMqxpT6f52F97J7hGGHU+ar 1vxv9heOdMqsGZSnf/nRY2QPfcurkkyWtDsWgI9WJ3+hYXlgje70QHl17hOWN5nd vmbt5dWJYM4Z217rBAt4jxJsKeh/TjRMqfdBee/+lZ8=; X-AuditID: c1b4fb3a-b6bb79e00000189f-51-5cf79d147f24 Received: from ESESBMB501.ericsson.se (Unknown_Domain [153.88.183.114]) by sessmg22.ericsson.net (Symantec Mail Security) with SMTP id E9.FD.06303.41D97FC5; Wed, 5 Jun 2019 12:44:36 +0200 (CEST) Received: from ESESBMR503.ericsson.se (153.88.183.135) by ESESBMB501.ericsson.se (153.88.183.168) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Wed, 5 Jun 2019 12:44:35 +0200 Received: from ESESBMB501.ericsson.se (153.88.183.168) 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; Wed, 5 Jun 2019 12:44:27 +0200 Received: from selio1a020.lmera.ericsson.se (153.88.183.153) by smtp.internal.ericsson.com (153.88.183.184) with Microsoft SMTP Server id 15.1.1713.5 via Frontend Transport; Wed, 5 Jun 2019 12:44:26 +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 x55AiQRD025408; Wed, 5 Jun 2019 12:44:27 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , , , , =?utf-8?q?Mattias_R=C3=B6nnblom?= Date: Wed, 5 Jun 2019 12:43:57 +0200 Message-ID: <20190605104400.24484-4-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190605104400.24484-1-mattias.ronnblom@ericsson.com> References: <20190516203529.GA642@bricha3-MOBL.ger.corp.intel.com> <20190605104400.24484-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrJLMWRmVeSWpSXmKPExsUyM2J7ka7I3O8xBj/3SFvcWGVvsX1FF5vF u0/bmSxuNZ9ks1h8R86B1ePXgqWsHov3vGTy6Dk5j8nj/b6rbB5Xvq9mDGCN4rJJSc3JLEst 0rdL4MqYNbWNueCZaMXyruksDYwrhboYOTkkBEwkFs78xtzFyMUhJHCUUWLzyl9MEM5XRom9 c5exwjmNRw9DZS4wSjyZ/YUFwrnEKPHu7WxGkGFsAp4Sk991s4DYIgJCEks/XmYHKWIW2Mso 8WrdPLCEsICTxImL05hBbBYBFYm2DZ1gcV6geNOexWwQV8lLrN5wAKyGU8BZ4vH0/UwgtpBA tcSEt81MEPWCEidnPgHrZRbQlGjd/psdwpaXaN46mxmiXkvi/pIvzBMYhWchaZmFpGUWkpYF jMyrGEWLU4uLc9ONjPRSizKTi4vz8/TyUks2MQIj5OCW31Y7GA8+dzzEKMDBqMTDe633e4wQ a2JZcWXuIUYJDmYlEd7E219ihHhTEiurUovy44tKc1KLDzFKc7AoifNGr94TIySQnliSmp2a WpBaBJNl4uCUamB0tdybOOs7W2pNfqH9o9+Cs5UnGfXe2bfS2f/BxJqiCIZNe7hEbmrpOtl7 5u3Yffqh/NICzrmlX46Xh/676sUeGhUk4GDIXu/xcZbEivg582LdaqXiTieqbuO9+2fed6v/ L/vZX1+W4d6zf96m+e9fBF32ZGl9E3tr07MfLBP2Ml9PCG5Tv/FaiaU4I9FQi7moOBEAerZT xYwCAAA= Subject: [dpdk-dev] [PATCH v3 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 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