DPDK patches and discussions
 help / color / mirror / Atom feed
From: Bruce Richardson <bruce.richardson@intel.com>
To: dev@dpdk.org
Cc: Bruce Richardson <bruce.richardson@intel.com>
Subject: [dpdk-dev] [PATCH 3/4] eal: allow checking CPU flags by name
Date: Wed, 29 May 2019 16:41:31 +0100	[thread overview]
Message-ID: <20190529154132.49955-4-bruce.richardson@intel.com> (raw)
In-Reply-To: <20190529154132.49955-1-bruce.richardson@intel.com>

Rather than using enum values for CPU flags, which means the symbols don't
exist on other architectures, provide a flag lookup by name, allowing us to
unconditionally check for a CPU flag.

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
 lib/librte_eal/common/eal_common_cpuflags.c   | 41 +++++++++++++++++
 .../common/include/generic/rte_cpuflags.h     | 44 ++++++++++++++++++-
 lib/librte_eal/rte_eal_version.map            |  3 ++
 3 files changed, 87 insertions(+), 1 deletion(-)

diff --git a/lib/librte_eal/common/eal_common_cpuflags.c b/lib/librte_eal/common/eal_common_cpuflags.c
index 3a055f7c7..5084a3e7a 100644
--- a/lib/librte_eal/common/eal_common_cpuflags.c
+++ b/lib/librte_eal/common/eal_common_cpuflags.c
@@ -3,6 +3,7 @@
  */
 
 #include <stdio.h>
+#include <string.h>
 
 #include <rte_common.h>
 #include <rte_cpuflags.h>
@@ -48,3 +49,43 @@ rte_cpu_is_supported(void)
 
 	return 1;
 }
+
+static enum rte_cpu_flag_t
+rte_cpu_get_flag(const char *flagname)
+{
+	int i;
+
+	if (flagname == NULL)
+		return RTE_CPUFLAG_NUMFLAGS;
+
+	for (i = 0; i < RTE_CPUFLAG_NUMFLAGS; i++)
+		if (strcmp(flagname, rte_cpu_get_flag_name(i)) == 0)
+			break;
+	return i;
+}
+
+static int
+rte_cpu_is_architecture(enum rte_cpu_arch arch)
+{
+	switch (arch) {
+	case rte_cpu_arch_arm:
+		return strcmp(RTE_ARCH, "arm") == 0 ||
+				strcmp(RTE_ARCH, "arm64") == 0;
+	case rte_cpu_arch_ppc:
+		return strcmp(RTE_ARCH, "ppc_64") == 0;
+	case rte_cpu_arch_x86:
+		return strcmp(RTE_ARCH, "x86_64") == 0 ||
+				strcmp(RTE_ARCH, "i686") == 0;
+	default:
+		return -EINVAL;
+	}
+}
+
+int
+rte_cpu_get_flagname_enabled(enum rte_cpu_arch arch, const char *flagname)
+{
+	if (!rte_cpu_is_architecture(arch))
+		return 0;
+
+	return rte_cpu_get_flag_enabled(rte_cpu_get_flag(flagname)) == 1;
+}
diff --git a/lib/librte_eal/common/include/generic/rte_cpuflags.h b/lib/librte_eal/common/include/generic/rte_cpuflags.h
index 156ea0029..a53551eba 100644
--- a/lib/librte_eal/common/include/generic/rte_cpuflags.h
+++ b/lib/librte_eal/common/include/generic/rte_cpuflags.h
@@ -10,7 +10,8 @@
  * Architecture specific API to determine available CPU features at runtime.
  */
 
-#include "rte_common.h"
+#include <rte_common.h>
+#include <rte_compat.h>
 #include <errno.h>
 
 /**
@@ -46,6 +47,47 @@ __extension__
 int
 rte_cpu_get_flag_enabled(enum rte_cpu_flag_t feature);
 
+/**
+ * Enumeration of the various CPU architectures supported by DPDK.
+ *
+ * When checking for CPU flags by name, it's possible that multiple
+ * architectures have flags with the same name e.g. AES is defined in
+ * both arm and x86 feature lists. Therefore we need to pass in at runtime
+ * the architecture we are checking for as well as the CPU flag. This enum
+ * defines the various supported architectures to be used for that checking.
+ */
+enum rte_cpu_arch {
+	rte_cpu_arch_arm = 0,
+	rte_cpu_arch_ppc,
+	rte_cpu_arch_x86,
+
+	rte_cpu_num_arch /* must always be the last */
+};
+
+/**
+ * Function for checking if a named CPU flag is enabled
+ *
+ * Wrapper around the rte_cpu_get_flag() and rte_cpu_get_flag_enabled()
+ * calls, which is safe to use even if the flag doesn't exist on target
+ * architecture. The function also verifies the target architecture so that
+ * we can distinguish e.g. AES support for arm vs x86 platforms.
+ *
+ * Note: This function uses multiple string compares in its operation and
+ * so is not recommended for data-path use. It should be called once, and
+ * the return value cached for later use.
+ *
+ * @param arch
+ *   The architecture on which we need to check the flag, since multiple
+ *   architectures could have flags with the same name.
+ * @param flagname
+ *   The name of the flag to query
+ * @return
+ *   1 if flag is available
+ *   0 if flag is not unavailable or invalid
+ */
+__rte_experimental int
+rte_cpu_get_flagname_enabled(enum rte_cpu_arch arch, const char *flagname);
+
 /**
  * This function checks that the currently used CPU supports the CPU features
  * that were specified at compile time. It is called automatically within the
diff --git a/lib/librte_eal/rte_eal_version.map b/lib/librte_eal/rte_eal_version.map
index 245493461..5ca5584a5 100644
--- a/lib/librte_eal/rte_eal_version.map
+++ b/lib/librte_eal/rte_eal_version.map
@@ -378,4 +378,7 @@ EXPERIMENTAL {
 	rte_service_lcore_attr_get;
 	rte_service_lcore_attr_reset_all;
 	rte_service_may_be_active;
+
+	# added in 19.08
+	rte_cpu_get_flagname_enabled;
 };
-- 
2.21.0


  parent reply	other threads:[~2019-05-29 15:42 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-29 15:41 [dpdk-dev] [PATCH 0/4] Enhance CPU flag support Bruce Richardson
2019-05-29 15:41 ` [dpdk-dev] [PATCH 1/4] build: fix quoting on RTE_ARCH string value Bruce Richardson
2019-05-29 15:53   ` Luca Boccassi
2019-05-29 15:41 ` [dpdk-dev] [PATCH 2/4] config/arm: fix missing define for arm platforms Bruce Richardson
2019-05-29 15:41 ` Bruce Richardson [this message]
2019-06-27 13:22   ` [dpdk-dev] [PATCH 3/4] eal: allow checking CPU flags by name David Marchand
2019-06-28 12:40     ` Bruce Richardson
2019-06-28 13:34       ` David Marchand
2019-05-29 15:41 ` [dpdk-dev] [PATCH 4/4] net: replace ifdefs with runtime branches Bruce Richardson
2019-07-01 19:30   ` Thomas Monjalon
2019-07-01 20:41     ` Bruce Richardson
2019-07-04 20:20       ` Thomas Monjalon
2019-07-08 17:24         ` David Christensen
2019-06-27 12:39 ` [dpdk-dev] [PATCH 0/4] Enhance CPU flag support Ananyev, Konstantin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190529154132.49955-4-bruce.richardson@intel.com \
    --to=bruce.richardson@intel.com \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).