[dpdk-dev] [RFC v3 4/6] eal: add a helper for reading string from sysfs

Chenbo Xia chenbo.xia at intel.com
Tue Jun 1 05:06:42 CEST 2021


From: Tiwei Bie <tiwei.bie at intel.com>

This patch adds a helper for reading string from sysfs.

Signed-off-by: Cunming Liang <cunming.liang at intel.com>
Signed-off-by: Tiwei Bie <tiwei.bie at intel.com>
---
 lib/eal/common/eal_filesystem.h | 10 ++++++++++
 lib/eal/freebsd/eal.c           | 22 ++++++++++++++++++++++
 lib/eal/linux/eal.c             | 22 ++++++++++++++++++++++
 lib/eal/version.map             |  3 +++
 4 files changed, 57 insertions(+)

diff --git a/lib/eal/common/eal_filesystem.h b/lib/eal/common/eal_filesystem.h
index 5d21f07c20..be4c51ebb2 100644
--- a/lib/eal/common/eal_filesystem.h
+++ b/lib/eal/common/eal_filesystem.h
@@ -104,4 +104,14 @@ eal_get_hugefile_path(char *buffer, size_t buflen, const char *hugedir, int f_id
  * Used to read information from files on /sys */
 int eal_parse_sysfs_value(const char *filename, unsigned long *val);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Function to read a line from a file on the filesystem.
+ * Used to read information from files on /sys
+ */
+__rte_experimental
+int rte_eal_parse_sysfs_str(const char *filename, char *buf, unsigned long sz);
+
 #endif /* EAL_FILESYSTEM_H */
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index f4d1676754..002f07f4da 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -169,6 +169,28 @@ eal_parse_sysfs_value(const char *filename, unsigned long *val)
 	return 0;
 }
 
+int
+rte_eal_parse_sysfs_str(const char *filename, char *buf, unsigned long sz)
+{
+	FILE *f;
+
+	f = fopen(filename, "r");
+	if (f == NULL) {
+		RTE_LOG(ERR, EAL, "%s(): cannot open sysfs file %s\n",
+			__func__, filename);
+		return -1;
+	}
+
+	if (fgets(buf, sz, f) == NULL) {
+		RTE_LOG(ERR, EAL, "%s(): cannot read sysfs file %s\n",
+			__func__, filename);
+		fclose(f);
+		return -1;
+	}
+
+	fclose(f);
+	return 0;
+}
 
 /* create memory configuration in shared/mmap memory. Take out
  * a write lock on the memsegs, so we can auto-detect primary/secondary.
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index ba19fc6347..d5917a48ca 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -260,6 +260,28 @@ eal_parse_sysfs_value(const char *filename, unsigned long *val)
 	return 0;
 }
 
+int
+rte_eal_parse_sysfs_str(const char *filename, char *buf, unsigned long sz)
+{
+	FILE *f;
+
+	f = fopen(filename, "r");
+	if (f == NULL) {
+		RTE_LOG(ERR, EAL, "%s(): cannot open sysfs file %s\n",
+			__func__, filename);
+		return -1;
+	}
+
+	if (fgets(buf, sz, f) == NULL) {
+		RTE_LOG(ERR, EAL, "%s(): cannot read sysfs file %s\n",
+			__func__, filename);
+		fclose(f);
+		return -1;
+	}
+
+	fclose(f);
+	return 0;
+}
 
 /* create memory configuration in shared/mmap memory. Take out
  * a write lock on the memsegs, so we can auto-detect primary/secondary.
diff --git a/lib/eal/version.map b/lib/eal/version.map
index fe5c3dac98..3d7fce26a4 100644
--- a/lib/eal/version.map
+++ b/lib/eal/version.map
@@ -423,6 +423,9 @@ EXPERIMENTAL {
 	rte_version_release; # WINDOWS_NO_EXPORT
 	rte_version_suffix; # WINDOWS_NO_EXPORT
 	rte_version_year; # WINDOWS_NO_EXPORT
+
+	# added in 21.08
+	rte_eal_parse_sysfs_str; # WINDOWS_NO_EXPORT
 };
 
 INTERNAL {
-- 
2.17.1



More information about the dev mailing list