From patchwork Thu May 16 17:55:07 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: 53486 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 23EF45F3C; Thu, 16 May 2019 19:55:29 +0200 (CEST) Received: from sessmg23.ericsson.net (sessmg23.ericsson.net [193.180.251.45]) by dpdk.org (Postfix) with ESMTP id 9534E4CAB for ; Thu, 16 May 2019 19:55:20 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1558029320; x=1560621320; 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=etnIbbaJOa5w1s/djhxcAn0JCwzBlDzy37MAb8E/V24=; b=JQOn6Wi1pFraGOG/V8MkfD8OT+Bxp9NSJLtqBGV0g2ljEvbEE9FSKQ0B261z77eQ Ifhj8EXNwQrIaBzy4H0ZKtYQqlk7Yx9TbQD+lk9GX9x1mkRCCRar1oZhB/lJnyuD QTeg8H23uGWqWjvZnMtZZH6cqbiIh0OXJqULtJ3EGHg=; X-AuditID: c1b4fb2d-17dff70000001a6d-6c-5cdda408a6ae Received: from ESESSMB501.ericsson.se (Unknown_Domain [153.88.183.119]) by sessmg23.ericsson.net (Symantec Mail Security) with SMTP id 7A.27.06765.804ADDC5; Thu, 16 May 2019 19:55:20 +0200 (CEST) Received: from ESESBMB503.ericsson.se (153.88.183.170) by ESESSMB501.ericsson.se (153.88.183.162) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Thu, 16 May 2019 19:55:20 +0200 Received: from selio1a020.lmera.ericsson.se (153.88.183.153) by smtp.internal.ericsson.com (153.88.183.186) with Microsoft SMTP Server id 15.1.1713.5 via Frontend Transport; Thu, 16 May 2019 19:55:19 +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 x4GHtHcv023701; Thu, 16 May 2019 19:55:20 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , , =?utf-8?q?Mattias_R=C3=B6nnblom?= Date: Thu, 16 May 2019 19:55:07 +0200 Message-ID: <20190516175510.27154-4-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190516175510.27154-1-mattias.ronnblom@ericsson.com> References: <20190514092046.30808-1-mattias.ronnblom@ericsson.com> <20190516175510.27154-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrOLMWRmVeSWpSXmKPExsUyM2J7uS7HkrsxBjOeylncWGVv8e7TdiaL xXfkHJg9fi1YyuqxeM9LJo+ek/OYApijuGxSUnMyy1KL9O0SuDJetb9lKlgrWtG00LaB8atg FyMnh4SAiUT/tWssXYxcHEICRxklJuw9BuV8Y5SYtGEGE4RzkVFi/8bv7BDOZUaJa19uM4L0 swl4Skx+180CYosICEks/XgZrIhZoJVRYsfDRlaQhLCAk8SiqVvAilgEVCXmHnzLBGLzAsU3 7bzCBnGIvMTqDQeYQWxOAWeJq+cPgPUKCVRL/OxaA1UvKHFy5hOwOcwCmhKt23+zQ9jyEs1b ZzND1GtJ3F/yhXkCo9AsJC2zkLTMQtKygJF5FaNocWpxcW66kbFealFmcnFxfp5eXmrJJkZg cB/c8lt3B+Pq146HGAU4GJV4eL/PuRsjxJpYVlyZe4hRgoNZSYR3w/vbMUK8KYmVValF+fFF pTmpxYcYpTlYlMR5o1fviRESSE8sSc1OTS1ILYLJMnFwSjUwFvq1RxwwuuJhcKadIWB1XXyv eJvTeo2Nzq81pW5/Zgh5l1V8Z+sj5gC+m0GtaZJCt/MMNp+flmRz4EDn1KgNbz7Xm35tF313 iyngMOf6Jyezbohv7jsxoeybdpx/5vQ/xuFlMcv6hIv/9z2vm3Z/6/uPBUZtwcYMn3aHzbro zjXDsH3u5/ZSJZbijERDLeai4kQAKAm7+WoCAAA= Subject: [dpdk-dev] [PATCH v2 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 --- 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