From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from out4-smtp.messagingengine.com (out4-smtp.messagingengine.com [66.111.4.28]) by dpdk.org (Postfix) with ESMTP id 30AF01B161 for ; Thu, 30 Nov 2017 22:47:29 +0100 (CET) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 9FF7220C89; Thu, 30 Nov 2017 16:47:28 -0500 (EST) Received: from frontend1 ([10.202.2.160]) by compute1.internal (MEProxy); Thu, 30 Nov 2017 16:47:28 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=monjalon.net; h= date:from:message-id:subject:to:x-me-sender:x-me-sender :x-sasl-enc; s=mesmtp; bh=VnoblMBSEz20WJzkIrQ+3B98kdYBFa58j0Pktw TW0/Y=; b=sneOr4mGJxiFVAmq5JO1JzJEnL1kiq4MCzCiNkOoSBSfaAhECkEA1y XqlFK7/TH9nspAIz1azm4Q+chfHNZIEgwH+4ncl6KiFB3zwYsm+ChMf7kSZRIlKq HWLoRBjHQPv5YWt/mJf+qZxa/OkJGkPRXGbbiFtbkSVCaNkef6vYg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=date:from:message-id:subject:to :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=VnoblMBSEz20WJzkI rQ+3B98kdYBFa58j0PktwTW0/Y=; b=iziZ3bov2G1+fuIxcmIzaJEacCU6uAjsB IC7lAfq21zCYXE66wAT2QTIBhREjUSwm4u/RvHnN6/wMHQGjFgoLXWcHHxoLTO2v 4AUABAYBY2Zte0/Orq1wA8EILRmRh10adQCIDGBPKy2tQ8lm9lHdDLAmoGYhmCNk tnslCmisNF8ww3jwTTjI0w1fdjUWZvPIe1O02/kQvB8XQTAfQ3sIhUFc2QMkhPYC aBm1S5kMeKzcSWUl7OoPUyRJ+wC6TrvCuC+iLt2KxltOBngcFviNfsosuLs5AIXW GqUdmcbnZkXVmN5HPrdvPxWmBHc911uyQJz7AOoqULjrC5Suz3Ekg== X-ME-Sender: Received: from xps.monjalon.net (184.203.134.77.rev.sfr.net [77.134.203.184]) by mail.messagingengine.com (Postfix) with ESMTPA id 27F5D7F9A9 for ; Thu, 30 Nov 2017 16:47:28 -0500 (EST) From: Thomas Monjalon To: dev@dpdk.org Date: Thu, 30 Nov 2017 22:47:20 +0100 Message-Id: <20171130214720.30098-1-thomas@monjalon.net> X-Mailer: git-send-email 2.15.0 Subject: [dpdk-dev] [PATCH] eal/x86: get hypervisor name 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: , X-List-Received-Date: Thu, 30 Nov 2017 21:47:29 -0000 The CPUID instruction is catched by hypervisor which can return a flag indicating one is running, and its name. Suggested-by: Stephen Hemminger Signed-off-by: Thomas Monjalon --- warning: to be tested --- lib/librte_eal/common/arch/arm/rte_cpuflags.c | 6 +++++ lib/librte_eal/common/arch/ppc_64/rte_cpuflags.c | 6 +++++ lib/librte_eal/common/arch/x86/rte_cpuflags.c | 30 ++++++++++++++++++++++ .../common/include/arch/x86/rte_cpuflags.h | 1 + .../common/include/generic/rte_cpuflags.h | 14 ++++++++++ lib/librte_eal/rte_eal_version.map | 9 ++++++- 6 files changed, 65 insertions(+), 1 deletion(-) diff --git a/lib/librte_eal/common/arch/arm/rte_cpuflags.c b/lib/librte_eal/common/arch/arm/rte_cpuflags.c index 88f1cbe37..f1f0cb51d 100644 --- a/lib/librte_eal/common/arch/arm/rte_cpuflags.c +++ b/lib/librte_eal/common/arch/arm/rte_cpuflags.c @@ -178,3 +178,9 @@ rte_cpu_get_flag_name(enum rte_cpu_flag_t feature) return NULL; return rte_cpu_feature_table[feature].name; } + +enum rte_hypervisor +rte_hypervisor_get_name(void) +{ + return RTE_HYPERVISOR_UNKNOWN; +} diff --git a/lib/librte_eal/common/arch/ppc_64/rte_cpuflags.c b/lib/librte_eal/common/arch/ppc_64/rte_cpuflags.c index 970a61c5e..b905cbd2c 100644 --- a/lib/librte_eal/common/arch/ppc_64/rte_cpuflags.c +++ b/lib/librte_eal/common/arch/ppc_64/rte_cpuflags.c @@ -146,3 +146,9 @@ rte_cpu_get_flag_name(enum rte_cpu_flag_t feature) return NULL; return rte_cpu_feature_table[feature].name; } + +enum rte_hypervisor +rte_hypervisor_get_name(void) +{ + return RTE_HYPERVISOR_UNKNOWN; +} diff --git a/lib/librte_eal/common/arch/x86/rte_cpuflags.c b/lib/librte_eal/common/arch/x86/rte_cpuflags.c index 7d4a0fefb..76893b8f1 100644 --- a/lib/librte_eal/common/arch/x86/rte_cpuflags.c +++ b/lib/librte_eal/common/arch/x86/rte_cpuflags.c @@ -34,6 +34,7 @@ #include "rte_cpuflags.h" #include +#include #include #include #include @@ -92,6 +93,7 @@ const struct feature_entry rte_cpu_feature_table[] = { FEAT_DEF(AVX, 0x00000001, 0, RTE_REG_ECX, 28) FEAT_DEF(F16C, 0x00000001, 0, RTE_REG_ECX, 29) FEAT_DEF(RDRAND, 0x00000001, 0, RTE_REG_ECX, 30) + FEAT_DEF(HYPERVISOR, 0x00000001, 0, RTE_REG_ECX, 31) FEAT_DEF(FPU, 0x00000001, 0, RTE_REG_EDX, 0) FEAT_DEF(VME, 0x00000001, 0, RTE_REG_EDX, 1) @@ -194,3 +196,31 @@ rte_cpu_get_flag_name(enum rte_cpu_flag_t feature) return NULL; return rte_cpu_feature_table[feature].name; } + +/* See http://lwn.net/Articles/301888/ */ +#define HYPERVISOR_INFO_LEAF 0x40000000 + +enum rte_hypervisor +rte_hypervisor_get_name(void) +{ + cpuid_registers_t regs; + char name[13]; + + if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_HYPERVISOR)) + return RTE_HYPERVISOR_NONE; + + __cpuid(HYPERVISOR_INFO_LEAF, + regs[RTE_REG_EAX], regs[RTE_REG_EBX], + regs[RTE_REG_ECX], regs[RTE_REG_EDX]); + for (int reg = 1; reg < 4; reg++) + memcpy(name + (reg - 1) * 4, ®s[reg], 4); + name[12] = '\0'; + + if (strcmp("KVMKVMKVM", name) == 0) + return RTE_HYPERVISOR_KVM; + if (strcmp("Microsoft Hv", name) == 0) + return RTE_HYPERVISOR_HYPERV; + if (strcmp("VMwareVMware", name) == 0) + return RTE_HYPERVISOR_VMWARE; + return RTE_HYPERVISOR_UNKNOWN; +} diff --git a/lib/librte_eal/common/include/arch/x86/rte_cpuflags.h b/lib/librte_eal/common/include/arch/x86/rte_cpuflags.h index 26204fabb..686ac07f3 100644 --- a/lib/librte_eal/common/include/arch/x86/rte_cpuflags.h +++ b/lib/librte_eal/common/include/arch/x86/rte_cpuflags.h @@ -69,6 +69,7 @@ enum rte_cpu_flag_t { RTE_CPUFLAG_AVX, /**< AVX */ RTE_CPUFLAG_F16C, /**< F16C */ RTE_CPUFLAG_RDRAND, /**< RDRAND */ + RTE_CPUFLAG_HYPERVISOR, /**< Running on a hypervisor */ /* (EAX 01h) EDX features */ RTE_CPUFLAG_FPU, /**< FPU */ diff --git a/lib/librte_eal/common/include/generic/rte_cpuflags.h b/lib/librte_eal/common/include/generic/rte_cpuflags.h index c1c5551fc..3832fb851 100644 --- a/lib/librte_eal/common/include/generic/rte_cpuflags.h +++ b/lib/librte_eal/common/include/generic/rte_cpuflags.h @@ -93,4 +93,18 @@ rte_cpu_check_supported(void); int rte_cpu_is_supported(void); +enum rte_hypervisor { + RTE_HYPERVISOR_NONE, + RTE_HYPERVISOR_KVM, + RTE_HYPERVISOR_HYPERV, + RTE_HYPERVISOR_VMWARE, + RTE_HYPERVISOR_UNKNOWN +}; + +/** + * Get the type of hypervisor it is running on. + */ +enum rte_hypervisor +rte_hypervisor_get_name(void); + #endif /* _RTE_CPUFLAGS_H_ */ diff --git a/lib/librte_eal/rte_eal_version.map b/lib/librte_eal/rte_eal_version.map index f4f46c1be..3f8d81577 100644 --- a/lib/librte_eal/rte_eal_version.map +++ b/lib/librte_eal/rte_eal_version.map @@ -200,6 +200,13 @@ DPDK_17.11 { } DPDK_17.08; +DPDK_18.02 { + global: + + rte_hypervisor_get_name; + +} DPDK_17.11; + EXPERIMENTAL { global: @@ -235,4 +242,4 @@ EXPERIMENTAL { rte_service_set_stats_enable; rte_service_start_with_defaults; -} DPDK_17.11; +} DPDK_18.02; -- 2.15.0