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

Message ID 20210601030644.3318-5-chenbo.xia@intel.com (mailing list archive)
State Changes Requested, archived
Delegated to: David Marchand
Headers
Series Add mdev (Mediated device) support in DPDK |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Chenbo Xia June 1, 2021, 3:06 a.m. UTC
  From: Tiwei Bie <tiwei.bie@intel.com>

This patch adds a helper for reading string from sysfs.

Signed-off-by: Cunming Liang <cunming.liang@intel.com>
Signed-off-by: Tiwei Bie <tiwei.bie@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(+)
  

Comments

Stephen Hemminger June 1, 2021, 5:37 a.m. UTC | #1
On Tue,  1 Jun 2021 11:06:42 +0800
Chenbo Xia <chenbo.xia@intel.com> wrote:

> +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;
> +}

It would be helpful if function removed trailing newline.
	strchrnul(buf, '\n') = '\0';
  
Stephen Hemminger June 1, 2021, 5:39 a.m. UTC | #2
On Tue,  1 Jun 2021 11:06:42 +0800
Chenbo Xia <chenbo.xia@intel.com> wrote:

>  
> +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);

Helpful to decode errno.
		RTE_LOG(ERR, EAL, "%s(): cannot open sysfs file %s:%s\n",
			__func__, filename, strerror(errno));
  
Chenbo Xia June 8, 2021, 5:47 a.m. UTC | #3
Hi Stephen,

> -----Original Message-----
> From: Stephen Hemminger <stephen@networkplumber.org>
> Sent: Tuesday, June 1, 2021 1:38 PM
> To: Xia, Chenbo <chenbo.xia@intel.com>
> Cc: dev@dpdk.org; thomas@monjalon.net; Liang, Cunming
> <cunming.liang@intel.com>; Wu, Jingjing <jingjing.wu@intel.com>; Burakov,
> Anatoly <anatoly.burakov@intel.com>; Yigit, Ferruh <ferruh.yigit@intel.com>;
> mdr@ashroe.eu; nhorman@tuxdriver.com; Richardson, Bruce
> <bruce.richardson@intel.com>; david.marchand@redhat.com; Ananyev, Konstantin
> <konstantin.ananyev@intel.com>; Tiwei Bie <tiwei.bie@intel.com>
> Subject: Re: [RFC v3 4/6] eal: add a helper for reading string from sysfs
> 
> On Tue,  1 Jun 2021 11:06:42 +0800
> Chenbo Xia <chenbo.xia@intel.com> wrote:
> 
> > +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;
> > +}
> 
> It would be helpful if function removed trailing newline.
> 	strchrnul(buf, '\n') = '\0';

Make sense. Will fix it.

Thanks,
Chenbo
  
Chenbo Xia June 8, 2021, 5:48 a.m. UTC | #4
Hi Stephen,

> -----Original Message-----
> From: Stephen Hemminger <stephen@networkplumber.org>
> Sent: Tuesday, June 1, 2021 1:39 PM
> To: Xia, Chenbo <chenbo.xia@intel.com>
> Cc: dev@dpdk.org; thomas@monjalon.net; Liang, Cunming
> <cunming.liang@intel.com>; Wu, Jingjing <jingjing.wu@intel.com>; Burakov,
> Anatoly <anatoly.burakov@intel.com>; Yigit, Ferruh <ferruh.yigit@intel.com>;
> mdr@ashroe.eu; nhorman@tuxdriver.com; Richardson, Bruce
> <bruce.richardson@intel.com>; david.marchand@redhat.com; Ananyev, Konstantin
> <konstantin.ananyev@intel.com>; Tiwei Bie <tiwei.bie@intel.com>
> Subject: Re: [RFC v3 4/6] eal: add a helper for reading string from sysfs
> 
> On Tue,  1 Jun 2021 11:06:42 +0800
> Chenbo Xia <chenbo.xia@intel.com> wrote:
> 
> >
> > +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);
> 
> Helpful to decode errno.
> 		RTE_LOG(ERR, EAL, "%s(): cannot open sysfs file %s:%s\n",
> 			__func__, filename, strerror(errno));

Yes. Will fix.

Thanks,
Chenbo
  
Thomas Monjalon June 11, 2021, 7:19 a.m. UTC | #5
01/06/2021 05:06, Chenbo Xia:
> From: Tiwei Bie <tiwei.bie@intel.com>
> 
> This patch adds a helper for reading string from sysfs.
> 
> Signed-off-by: Cunming Liang <cunming.liang@intel.com>
> Signed-off-by: Tiwei Bie <tiwei.bie@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(+)

3 separate comments:

1/ How much code is portable between Linux and FreeBSD?
I guess the path will be different?

2/ Please think about Windows stub.

3/ Instead of EAL, we should start lib/sysfs/
I have other ideas of sysfs functions for PMD use.
  

Patch

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 {