From patchwork Wed Apr 22 18:15:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Gora X-Patchwork-Id: 69118 X-Patchwork-Delegate: david.marchand@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 68DBDA00C2; Wed, 22 Apr 2020 20:16:38 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 3D46B1D572; Wed, 22 Apr 2020 20:16:38 +0200 (CEST) Received: from mail-qk1-f194.google.com (mail-qk1-f194.google.com [209.85.222.194]) by dpdk.org (Postfix) with ESMTP id 861761D559 for ; Wed, 22 Apr 2020 20:16:36 +0200 (CEST) Received: by mail-qk1-f194.google.com with SMTP id b62so3442606qkf.6 for ; Wed, 22 Apr 2020 11:16:36 -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=Lo8+d918gZimIDGjN/gkv+6HIWmTeGcYnnMcht/KXPo=; b=ozi34Tw/9EPk5TWfwL97SFeO4HDq4vWAM1CVGsaL912UC2BJauNe6U8268uDgVpn8e SRBPNn1PKaTsgSb6i6P0gaPBjwnKGaOC7dxtUC7WjEXhgiaOIhX5FlmpDBxM+FUz3SCS M0YmnGYzTkx64Hp1BZ+UgMIRifyxLmOF5Cwycd0kAWMp2CDIcAyT0vKEeVDpJulig5CS c7GwEyPywHMNUvKjP7sjbDe0q84vEItHzYPIgELLqXjeSKYB2LXcg4xqYsL4LXRpB0s0 qTmldCrD1FlU88HSkVmdV1G6QwApT2tAY0JlZvpZrV2cBzVyGSaS+NznTsf8BopNLcwV +oUw== 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=Lo8+d918gZimIDGjN/gkv+6HIWmTeGcYnnMcht/KXPo=; b=A4ZWuPzZGeBMIYvLQArqNswBvX4103En+L+0rNRzxR7im0MzJgocdiLgBMEhFtWLoO MP6O3XMmbYYppGB1lj86BAlTHOqNzpfVAW0iEX0Y4g3EXjCVuhno4urVkEJTaBBmxSe4 DiA1IMEOkT7l6seJhWJ8dHs+yOXsClOSbaPvEfoj919N7+ZtAq9bmfyBDc3DNYIrM+PT e93bUlRECMIfT+dKTiRA0XAeRMQfb/Dt5oN7OaUkFf4SLL1W80Uu2JQmB9pUazGxg4iW 2DDR+QK/lUrSHj2F7GJORT9IKLW3C2cbbc79DqJecGft1SaZCehxurIqMWWXKKozh9i6 Resg== X-Gm-Message-State: AGi0Pua9JzqpSN8IXcz1E1VNDBU/4HJ+kpnYM3lHTMKNK+VglFJXUVaR 3axJbTDBBw1cEfyW5YVP8PdtCZnUTK0= X-Google-Smtp-Source: APiQypIAbApKBAz9+5FF8MPXaVoXGNTL6ERlkL8ITrFH9FRDbjW9KCNyYVJIwco/CWBn5SRPprmwjQ== X-Received: by 2002:a37:a049:: with SMTP id j70mr27140130qke.193.1587579395583; Wed, 22 Apr 2020 11:16:35 -0700 (PDT) Received: from localhost.localdomain ([168.121.99.161]) by smtp.gmail.com with ESMTPSA id d26sm4360227qkk.69.2020.04.22.11.16.33 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 Apr 2020 11:16:35 -0700 (PDT) From: Dan Gora To: dev@dpdk.org, Thomas Monjalon , =?utf-8?q?Mattias_R?= =?utf-8?q?=C3=B6nnblom?= Cc: David Marchand , Jerin Jacob , Dan Gora Date: Wed, 22 Apr 2020 15:15:23 -0300 Message-Id: <20200422181524.18679-2-dg@adax.com> X-Mailer: git-send-email 2.24.1.425.g7034cd094b In-Reply-To: <20200422181524.18679-1-dg@adax.com> References: <20200421195446.1730-1-dg@adax.com> <20200422181524.18679-1-dg@adax.com> MIME-Version: 1.0 Subject: [dpdk-dev] [PATCH v3 1/2] eal: check for rdseed at run time for 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" Instead of enabling the rdseed instruction for the random number generator entropy source at compilation time, determine if the instruction can be used at run time. The DPDK build is updated to check that the compiler can generate the rdseed instruction even if the compilation platform does not natively support it. If so, the -mrdseed flag is explicitly added. This allows binaries compiled on systems which do not support the rdseed instruction to use the instruction if the execution platform supports it. At run-time on x86, __rte_random_initial_seed() will check for the availability of the rdseed instruction on the execution platform using rte_cpu_get_flag_enabled(). This allows binaries which were compiled on systems which support the rdseed instruction to run on x86 CPUs which do not support the rdseed instruction. RTE_CPUFLAG_RDSEED is removed from the list of RTE_COMPILE_TIME_CPUFLAGS which are checked in rte_eal_init() at run time because it is no longer required to match the compilation system. Signed-off-by: Dan Gora --- config/x86/meson.build | 11 ++++++++--- lib/librte_eal/common/rte_random.c | 19 +++++++++++-------- mk/rte.cpuflags.mk | 9 +++++++-- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/config/x86/meson.build b/config/x86/meson.build index adc857ba2..9491fad3a 100644 --- a/config/x86/meson.build +++ b/config/x86/meson.build @@ -20,15 +20,20 @@ if cc.get_define('__SSE4_2__', args: machine_args) == '' machine_args += '-msse4' endif +# set -mrdseed if necessary so _rdseed32_step compiles if the +# compilation host does not support the RDSEED instruction. +if cc.get_define('__RDSEED__', args: machine_args) == '' and cc.has_argument('-mrdseed') + machine_args += '-mrdseed' + message('RDSEED not enabled by default, explicitly setting -mrdseed') +endif + base_flags = ['SSE', 'SSE2', 'SSE3','SSSE3', 'SSE4_1', 'SSE4_2'] foreach f:base_flags dpdk_conf.set('RTE_MACHINE_CPUFLAG_' + f, 1) compile_time_cpuflags += ['RTE_CPUFLAG_' + f] endforeach -optional_flags = ['AES', 'PCLMUL', - 'AVX', 'AVX2', 'AVX512F', - 'RDRND', 'RDSEED'] +optional_flags = ['AES', 'PCLMUL', 'AVX', 'AVX2', 'AVX512F', 'RDRND'] foreach f:optional_flags if cc.get_define('__@0@__'.format(f), args: machine_args) == '1' if f == 'PCLMUL' # special case flags with different defines diff --git a/lib/librte_eal/common/rte_random.c b/lib/librte_eal/common/rte_random.c index b7a089ac4..2c84c8527 100644 --- a/lib/librte_eal/common/rte_random.c +++ b/lib/librte_eal/common/rte_random.c @@ -2,7 +2,7 @@ * Copyright(c) 2019 Ericsson AB */ -#ifdef RTE_MACHINE_CPUFLAG_RDSEED +#if defined(RTE_ARCH_X86) #include #endif #include @@ -188,14 +188,17 @@ __rte_random_initial_seed(void) if (ge_rc == 0) return ge_seed; #endif -#ifdef RTE_MACHINE_CPUFLAG_RDSEED - unsigned int rdseed_low; - unsigned int rdseed_high; - +#if defined(RTE_ARCH_X86) /* first fallback: rdseed instruction, if available */ - if (_rdseed32_step(&rdseed_low) == 1 && - _rdseed32_step(&rdseed_high) == 1) - return (uint64_t)rdseed_low | ((uint64_t)rdseed_high << 32); + if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_RDSEED)) { + unsigned int rdseed_low; + unsigned int rdseed_high; + + if (_rdseed32_step(&rdseed_low) == 1 && + _rdseed32_step(&rdseed_high) == 1) + return (uint64_t)rdseed_low | + ((uint64_t)rdseed_high << 32); + } #endif /* second fallback: seed using rdtsc */ return rte_get_tsc_cycles(); diff --git a/mk/rte.cpuflags.mk b/mk/rte.cpuflags.mk index fa8753531..a114e9c02 100644 --- a/mk/rte.cpuflags.mk +++ b/mk/rte.cpuflags.mk @@ -51,8 +51,13 @@ ifneq ($(filter $(AUTO_CPUFLAGS),__RDRND__),) CPUFLAGS += RDRAND endif -ifneq ($(filter $(AUTO_CPUFLAGS),__RDSEED__),) -CPUFLAGS += RDSEED +ifeq ($(filter $(AUTO_CPUFLAGS),__RDSEED__),) +# If the native environment doesn't define __RDSEED__, see +# if the compiler supports -mrdseed. +RDSEED_CPUFLAGS := $(shell $(CC) $(MACHINE_CFLAGS) $(WERROR_FLAGS) $(EXTRA_CFLAGS) -mrdseed -dM -E - < /dev/null) +ifneq ($(filter $(RDSEED_CPUFLAGS),__RDSEED__),) +MACHINE_CFLAGS += -mrdseed +endif endif ifneq ($(filter $(AUTO_CPUFLAGS),__FSGSBASE__),) From patchwork Wed Apr 22 18:15:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Gora X-Patchwork-Id: 69119 X-Patchwork-Delegate: david.marchand@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 9B288A00C2; Wed, 22 Apr 2020 20:16:44 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id BFCE31D58E; Wed, 22 Apr 2020 20:16:42 +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 33C0C1D58C for ; Wed, 22 Apr 2020 20:16:42 +0200 (CEST) Received: by mail-qk1-f193.google.com with SMTP id b62so3442939qkf.6 for ; Wed, 22 Apr 2020 11:16:42 -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=I86adsQf6YOuHk4/hcl0IQ0VyaAPCTKy1hpvvuMvToA=; b=kEQbdwRbxa5/7bJKbRIdBspuT3Lc6bFii+ekue2VJ+MNl8l6iWl0w20jp9KumfoLV9 gqSZ091najgAJqOs1b8HkyJPQn6g0m+4fwFrWqb9oKkrJl5hSvXgAAsMsF4a4TIt5MD2 TimqbgVtgnWo1LBX20/pMxVv/a4cM5+pe93ZWVjzWbXldgmWmaucX0RK3TBOaOuA33tm K5hsMEeP+fuxMD2VHejQztyMTH7RIfxa8TQU7+DK/fUefPFHXkqtVuxFnJSvYDCryQYK Y/rJDjyxKfCbOSSb4lDqT1JM7h8O4WxXiTz1nHGb7Fycibu7bzx2apWXs73G/FDmejiR mH+w== 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=I86adsQf6YOuHk4/hcl0IQ0VyaAPCTKy1hpvvuMvToA=; b=hECihx4iSwCT3ErcHlsMpNDCBSf7IMXJd7ig1KrBWyKP6mMzYINshZ92xnMGajWVb/ zwreTLnSBoVhuo8jGtAk/y3yF4ygopUHHOVKEyhNfNrMvN0SgSK9Dm2xcs6kRdxTCKqZ 0RtwX/3jN9nI2b+hUyNmddJWv6bMVoIp8NgX3GjErFLEfNSWXFZShj2gTA872qCQSV8z LOs+LySWYk8DNs+A1v5L7hoI/KiwSwrIJLHj1PS0pDfTr+PlNURJS+m+aZHjV6RkurVj US/+iFyxO1tfyUWfqe7iu3tFP+GU8GAcVBO72QEqX1PXreC0RvIXk6sL0I880Zmnn4GV Og2w== X-Gm-Message-State: AGi0PubPTUMlxCR1s0/dfnAaP0Y3bVj6L0PBqzwnl9+x6EiSVK1FXOHV Fc8qT6kZl/cUFQSpTdeEHT21RXVahuA= X-Google-Smtp-Source: APiQypIY/lsYH9jkplAgo63dsv2FusR2ApzoSWwTm3r+WrnEFIwF2IWQPt9IeqRI9ywlYYxRIrzKQg== X-Received: by 2002:a37:6415:: with SMTP id y21mr18669645qkb.258.1587579401043; Wed, 22 Apr 2020 11:16:41 -0700 (PDT) Received: from localhost.localdomain ([168.121.99.161]) by smtp.gmail.com with ESMTPSA id d26sm4360227qkk.69.2020.04.22.11.16.39 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 Apr 2020 11:16:40 -0700 (PDT) From: Dan Gora To: dev@dpdk.org, =?utf-8?q?Mattias_R=C3=B6nnblom?= Cc: David Marchand , Jerin Jacob , Dan Gora Date: Wed, 22 Apr 2020 15:15:24 -0300 Message-Id: <20200422181524.18679-3-dg@adax.com> X-Mailer: git-send-email 2.24.1.425.g7034cd094b In-Reply-To: <20200422181524.18679-1-dg@adax.com> References: <20200421195446.1730-1-dg@adax.com> <20200422181524.18679-1-dg@adax.com> MIME-Version: 1.0 Subject: [dpdk-dev] [PATCH v3 2/2] eal: resolve getentropy at run time for 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() which will try to resolve the getentropy() function dynamically using dlopen()/dlsym(), returning failure if the getentropy() function cannot be resolved or if it fails. This also allows getentropy() to be used as the random seed source when the traditional Makefile build for DPDK is used. Signed-off-by: Dan Gora --- lib/librte_eal/common/rte_random.c | 36 +++++++++++++++++++++++++----- lib/librte_eal/meson.build | 3 --- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/lib/librte_eal/common/rte_random.c b/lib/librte_eal/common/rte_random.c index 2c84c8527..f8036aa20 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,18 +177,41 @@ rte_rand_max(uint64_t upper_bound) return res; } +/* Try to use the getentropy() function from glibc >= 2.25 */ +static int +__rte_getentropy(uint64_t *ge_seed) +{ + void *handle = NULL; + void **sym; + int (*getentropy_p)(void *__buffer, size_t __length); + int gc_rc; + + handle = dlopen("libc.so.6", RTLD_LAZY); + if (handle == NULL) + return -1; + + dlerror(); + sym = dlsym(handle, "getentropy"); + if (dlerror() != NULL) { + /* Cannot resolve getentropy */ + dlclose(handle); + return -1; + } + + getentropy_p = (int (*)(void *, size_t)) sym; + gc_rc = (*getentropy_p)((void *)ge_seed, sizeof(*ge_seed)); + dlclose(handle); + return gc_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) + if (__rte_getentropy(&ge_seed) == 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)) { 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