From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 29003A0588; Thu, 16 Apr 2020 02:00:37 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 6E4531DA42; Thu, 16 Apr 2020 02:00:36 +0200 (CEST) Received: from mail-qv1-f67.google.com (mail-qv1-f67.google.com [209.85.219.67]) by dpdk.org (Postfix) with ESMTP id 0280A1DA3F for ; Thu, 16 Apr 2020 02:00:34 +0200 (CEST) Received: by mail-qv1-f67.google.com with SMTP id da9so1186225qvb.3 for ; Wed, 15 Apr 2020 17:00:34 -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=rSnjRt5a7Bg62HQqJU/EkA5q+tW8N6yQYNVl01jr14Q=; b=NVGZnjbjqA0JYGyg6uDawIW3a5wbyVoYxQNXilf61iPNziix+abX8SaP8ffIVpHbhi AJiQDNhFjv3qdMiyLa4wj6kc0WGU1GyiZpejhaNiF7tCqF4UgE2we0DHhioc4eI8O9nj rIVMkq8yi9Sog5IBP1n3konHprYjG6aVcj9jea+9/sCACxc6P1rie4V1mIznrEm+iXAj zniyXWxsVI52+7FnFY2ZygrVC4SitgBugqAD2AJhwXR9GxGwLk1Snn12+Cn35t6zfwhu 8Hc035xUbjaim+GA6XZ7W2DPauGJ1QQCLSrAJrqRFcnHJqaVOX0bWifyiUCFS/fhzHV/ v9MA== 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=rSnjRt5a7Bg62HQqJU/EkA5q+tW8N6yQYNVl01jr14Q=; b=maAnVKC/ZNs6wW89Pf+hYqO0a8v8bTJDATdXLaijcg6nv8yg+C/T3j4Th/mUQhFEYo oaC3a8saSMSgwpqAOe1u14/OKl1O1vpH/DuSS3ai+nXWTNc2x+748KnRMrXItdz2O8KQ pRX1FxhiIKJsFV1zl7al9YIYObmGrUibbC2ktWltZSKFLZgOUSdzu9FbNCjyAbl+4Drv DV4vlP56K9xqzMmB58EIfhnRSCKaejy2O6+0x+tNOIJIYbOD9Pav7aTpivS28w/K5GVD jwFgzltBZU7Vd6SH5nrhovCQLOrnraC3t7vXsdnEWAfw/pmHhwVvl7njyQRKq/ZGaXJb lMAQ== X-Gm-Message-State: AGi0Pub9Louh/gBPMmsQ1sttniRN8C/+Aedbn5Oz5v4u/2ea5BO16Jc8 orcEd2Vy8mWhS1b8Cfx3OYk= X-Google-Smtp-Source: APiQypLRY13TsxiqvnPgO1/oC4akQ9K+RFEalp1zfnnYGl2AJRcwPPY2+rHMl3QwiWmkNBaJHWfuwA== X-Received: by 2002:a0c:9e45:: with SMTP id z5mr7728199qve.250.1586995234023; Wed, 15 Apr 2020 17:00:34 -0700 (PDT) Received: from localhost.localdomain ([168.121.99.161]) by smtp.gmail.com with ESMTPSA id b13sm13231686qtp.46.2020.04.15.17.00.32 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 15 Apr 2020 17:00:33 -0700 (PDT) From: Dan Gora To: Thomas Monjalon , =?UTF-8?q?Mattias=20R=C3=B6nnblom?= Cc: David Marchand , dev@dpdk.org, Dan Gora Date: Wed, 15 Apr 2020 20:59:13 -0300 Message-Id: <20200415235913.31949-1-dg@adax.com> X-Mailer: git-send-email 2.24.1.425.g7034cd094b In-Reply-To: <20200415231119.27845-1-dg@adax.com> References: <20200415231119.27845-1-dg@adax.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v2] eal: choose initial PRNG seed source at runtime 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 choosing to use getentropy() or the rdseed instruction for the random number generator entropy source using compilation flags, determine the best source at run time. This is accomplished by defining a weak symbol for getentropy(), checking that the compiler can generate the rdseed instruction even if the compilation platform does not natively support it, and checking for the availability of the rdseed instruction on the execution platform using rte_cpu_get_flag_enabled(). If neither getentropy() or rdseed is available, rte_get_timer_cycles() will be continue to be used as the entropy source. This also allows non-meson builds to use getentropy(). Signed-off-by: Dan Gora --- v2: * Rebase to latest master. * Fix spelling of "meson". config/x86/meson.build | 7 +++++++ lib/librte_eal/common/rte_random.c | 29 ++++++++++++++++++++++++----- lib/librte_eal/meson.build | 3 --- mk/rte.cpuflags.mk | 8 ++++++++ 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/config/x86/meson.build b/config/x86/meson.build index adc857ba2..214b16f2a 100644 --- a/config/x86/meson.build +++ b/config/x86/meson.build @@ -20,6 +20,13 @@ 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) diff --git a/lib/librte_eal/common/rte_random.c b/lib/librte_eal/common/rte_random.c index 57ec8fb2b..40f8b5aab 100644 --- a/lib/librte_eal/common/rte_random.c +++ b/lib/librte_eal/common/rte_random.c @@ -25,6 +25,8 @@ struct rte_rand_state { static struct rte_rand_state rand_states[RTE_MAX_LCORE]; +__rte_weak int getentropy(void *__buffer, size_t __length); + static uint32_t __rte_rand_lcg32(uint32_t *seed) { @@ -176,10 +178,24 @@ rte_rand_max(uint64_t upper_bound) return res; } +/* Use rte_get_timer_cycles() if the system does not have + * genentropy() or the rdseed instruction. + */ +__rte_weak int +getentropy(void *__buffer, size_t __length __rte_unused) +{ + uint64_t *ge_seed = __buffer; +#ifdef RTE_MACHINE_CPUFLAG_RDSEED + if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_RDSEED)) + return -1; +#endif + *ge_seed = rte_get_timer_cycles(); + return 0; +} + static uint64_t __rte_random_initial_seed(void) { -#ifdef RTE_LIBEAL_USE_GETENTROPY int ge_rc; uint64_t ge_seed; @@ -187,15 +203,18 @@ __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; /* 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)) { + 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_timer_cycles(); 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 diff --git a/mk/rte.cpuflags.mk b/mk/rte.cpuflags.mk index fa8753531..fb7bf8a53 100644 --- a/mk/rte.cpuflags.mk +++ b/mk/rte.cpuflags.mk @@ -53,6 +53,14 @@ endif ifneq ($(filter $(AUTO_CPUFLAGS),__RDSEED__),) CPUFLAGS += RDSEED +else +# 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__),) +CPUFLAGS += RDSEED +MACHINE_CFLAGS += -mrdseed +endif endif ifneq ($(filter $(AUTO_CPUFLAGS),__FSGSBASE__),) -- 2.24.1.425.g7034cd094b