[dpdk-dev,v2,2/3] bus/pci: expose sysfs parsing API

Message ID 20180321132108.52464-3-xiao.w.wang@intel.com (mailing list archive)
State Superseded, archived
Delegated to: Ferruh Yigit
Headers

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation fail apply issues

Commit Message

Xiao Wang March 21, 2018, 1:21 p.m. UTC
  Some existing sysfs parsing functions are helpful for the later vDPA
driver, this patch make them global and expose them to shared lib.

Signed-off-by: Xiao Wang <xiao.w.wang@intel.com>
---
v2:
- Rename function pci_get_kernel_driver_by_path to rte_pci_device_kdriver_name
  to make the API generic cross Linux and BSD, make it as EXPERIMENTAL.
---
 drivers/bus/pci/Makefile                |  2 ++
 drivers/bus/pci/bsd/pci.c               | 14 ++++++++++++++
 drivers/bus/pci/linux/pci.c             | 22 +++++++++++++---------
 drivers/bus/pci/rte_bus_pci.h           | 32 ++++++++++++++++++++++++++++++++
 drivers/bus/pci/rte_bus_pci_version.map |  8 ++++++++
 5 files changed, 69 insertions(+), 9 deletions(-)
  

Comments

Thomas Monjalon March 21, 2018, 8:44 p.m. UTC | #1
21/03/2018 14:21, Xiao Wang:
> Some existing sysfs parsing functions are helpful for the later vDPA
> driver, this patch make them global and expose them to shared lib.
> 
> Signed-off-by: Xiao Wang <xiao.w.wang@intel.com>
> ---
>  	/* parse driver */
>  	snprintf(filename, sizeof(filename), "%s/driver", dirname);
> -	ret = pci_get_kernel_driver_by_path(filename, driver);
> +	ret = rte_pci_device_kdriver_name(addr, driver);

I guess the snprintf above becomes useless.

> + * @param dri_name
> + *   Output buffer pointer.

Parameter name and comment can be improved here:
"kdrv_name" would be more meaningful.
As a comment, "Output buffer for kernel driver name"

> +/**
> + * @warning
> + * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
> + *
> + * Parse the "resource" sysfs file.
> + *
> + * @param filename
> + *   The PCI resource file path.
> + * @dev
> + *   Pointer of rte_pci_device object, into which the parse result is recorded.
> + * @return
> + *   0 on success, -1 on error, 1 on no driver found.
> + */
> +int __rte_experimental
> +rte_pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev);

This is a Linux specific API.
Maybe remove "sysfs" and replace "filename" by "resource"?
  
Xiao Wang March 22, 2018, 2:46 a.m. UTC | #2
Hi Thomas,

> -----Original Message-----
> From: Thomas Monjalon [mailto:thomas@monjalon.net]
> Sent: Thursday, March 22, 2018 4:45 AM
> To: Wang, Xiao W <xiao.w.wang@intel.com>
> Cc: dev@dpdk.org; maxime.coquelin@redhat.com; yliu@fridaylinux.org; Wang,
> Zhihong <zhihong.wang@intel.com>; Bie, Tiwei <tiwei.bie@intel.com>; Chen,
> Junjie J <junjie.j.chen@intel.com>; Xu, Rosen <rosen.xu@intel.com>; Daly,
> Dan <dan.daly@intel.com>; Liang, Cunming <cunming.liang@intel.com>;
> Burakov, Anatoly <anatoly.burakov@intel.com>; gaetan.rivet@6wind.com
> Subject: Re: [dpdk-dev] [PATCH v2 2/3] bus/pci: expose sysfs parsing API
> 
> 21/03/2018 14:21, Xiao Wang:
> > Some existing sysfs parsing functions are helpful for the later vDPA
> > driver, this patch make them global and expose them to shared lib.
> >
> > Signed-off-by: Xiao Wang <xiao.w.wang@intel.com>
> > ---
> >  	/* parse driver */
> >  	snprintf(filename, sizeof(filename), "%s/driver", dirname);
> > -	ret = pci_get_kernel_driver_by_path(filename, driver);
> > +	ret = rte_pci_device_kdriver_name(addr, driver);
> 
> I guess the snprintf above becomes useless.

Will remove it.
> 
> > + * @param dri_name
> > + *   Output buffer pointer.
> 
> Parameter name and comment can be improved here:
> "kdrv_name" would be more meaningful.
> As a comment, "Output buffer for kernel driver name"

Thanks for the suggestion. Will improve it.

> 
> > +/**
> > + * @warning
> > + * @b EXPERIMENTAL: this API may change, or be removed, without prior
> notice
> > + *
> > + * Parse the "resource" sysfs file.
> > + *
> > + * @param filename
> > + *   The PCI resource file path.
> > + * @dev
> > + *   Pointer of rte_pci_device object, into which the parse result is recorded.
> > + * @return
> > + *   0 on success, -1 on error, 1 on no driver found.
> > + */
> > +int __rte_experimental
> > +rte_pci_parse_sysfs_resource(const char *filename, struct rte_pci_device
> *dev);
> 
> This is a Linux specific API.
> Maybe remove "sysfs" and replace "filename" by "resource"?

Yes, "sysfs" makes it Linux specific. Will change it.
Thanks for the above comments.

BRs,
Xiao
  

Patch

diff --git a/drivers/bus/pci/Makefile b/drivers/bus/pci/Makefile
index f3df1c4ce..eb494e0f2 100644
--- a/drivers/bus/pci/Makefile
+++ b/drivers/bus/pci/Makefile
@@ -49,6 +49,8 @@  CFLAGS += -I$(RTE_SDK)/drivers/bus/pci/$(SYSTEM)
 CFLAGS += -I$(RTE_SDK)/lib/librte_eal/common
 CFLAGS += -I$(RTE_SDK)/lib/librte_eal/$(SYSTEM)app/eal
 
+CFLAGS += -DALLOW_EXPERIMENTAL_API
+
 LDLIBS += -lrte_eal -lrte_mbuf -lrte_mempool -lrte_ring
 LDLIBS += -lrte_ethdev -lrte_pci
 
diff --git a/drivers/bus/pci/bsd/pci.c b/drivers/bus/pci/bsd/pci.c
index 655b34b7e..08fbe085e 100644
--- a/drivers/bus/pci/bsd/pci.c
+++ b/drivers/bus/pci/bsd/pci.c
@@ -649,3 +649,17 @@  rte_pci_ioport_unmap(struct rte_pci_ioport *p)
 
 	return ret;
 }
+
+int __rte_experimental
+rte_pci_device_kdriver_name(__rte_unused const struct rte_pci_addr *addr,
+		__rte_unused char *dri_name)
+{
+	return -1;
+}
+
+int __rte_experimental
+rte_pci_parse_sysfs_resource(__rte_unused const char *filename,
+		__rte_unused struct rte_pci_device *dev)
+{
+	return -1;
+}
diff --git a/drivers/bus/pci/linux/pci.c b/drivers/bus/pci/linux/pci.c
index abde64119..4ba411c7c 100644
--- a/drivers/bus/pci/linux/pci.c
+++ b/drivers/bus/pci/linux/pci.c
@@ -32,17 +32,22 @@ 
 
 extern struct rte_pci_bus rte_pci_bus;
 
-static int
-pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
+int __rte_experimental
+rte_pci_device_kdriver_name(const struct rte_pci_addr *addr, char *dri_name)
 {
 	int count;
+	char link[PATH_MAX];
 	char path[PATH_MAX];
 	char *name;
 
-	if (!filename || !dri_name)
+	if (!addr || !dri_name)
 		return -1;
 
-	count = readlink(filename, path, PATH_MAX);
+	snprintf(link, sizeof(link), "%s/" PCI_PRI_FMT "/driver",
+		 rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
+		 addr->function);
+
+	count = readlink(link, path, PATH_MAX);
 	if (count >= PATH_MAX)
 		return -1;
 
@@ -168,9 +173,8 @@  pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr,
 	return 0;
 }
 
-/* parse the "resource" sysfs file */
-static int
-pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
+int __rte_experimental
+rte_pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
 {
 	FILE *f;
 	char buf[BUFSIZ];
@@ -302,7 +306,7 @@  pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
 
 	/* parse resources */
 	snprintf(filename, sizeof(filename), "%s/resource", dirname);
-	if (pci_parse_sysfs_resource(filename, dev) < 0) {
+	if (rte_pci_parse_sysfs_resource(filename, dev) < 0) {
 		RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
 		free(dev);
 		return -1;
@@ -310,7 +314,7 @@  pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
 
 	/* parse driver */
 	snprintf(filename, sizeof(filename), "%s/driver", dirname);
-	ret = pci_get_kernel_driver_by_path(filename, driver);
+	ret = rte_pci_device_kdriver_name(addr, driver);
 	if (ret < 0) {
 		RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
 		free(dev);
diff --git a/drivers/bus/pci/rte_bus_pci.h b/drivers/bus/pci/rte_bus_pci.h
index 357afb912..98b643356 100644
--- a/drivers/bus/pci/rte_bus_pci.h
+++ b/drivers/bus/pci/rte_bus_pci.h
@@ -304,6 +304,38 @@  void rte_pci_ioport_read(struct rte_pci_ioport *p,
 void rte_pci_ioport_write(struct rte_pci_ioport *p,
 		const void *data, size_t len, off_t offset);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
+ *
+ * Get the name of kernel driver bound to PCI device.
+ *
+ * @param addr
+ *   The PCI device's location.
+ * @param dri_name
+ *   Output buffer pointer.
+ * @return
+ *   0 on success, negative on error.
+ */
+int __rte_experimental
+rte_pci_device_kdriver_name(const struct rte_pci_addr *addr, char *dri_name);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
+ *
+ * Parse the "resource" sysfs file.
+ *
+ * @param filename
+ *   The PCI resource file path.
+ * @dev
+ *   Pointer of rte_pci_device object, into which the parse result is recorded.
+ * @return
+ *   0 on success, -1 on error, 1 on no driver found.
+ */
+int __rte_experimental
+rte_pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/drivers/bus/pci/rte_bus_pci_version.map b/drivers/bus/pci/rte_bus_pci_version.map
index 27e9c4f10..d5576302b 100644
--- a/drivers/bus/pci/rte_bus_pci_version.map
+++ b/drivers/bus/pci/rte_bus_pci_version.map
@@ -16,3 +16,11 @@  DPDK_17.11 {
 
 	local: *;
 };
+
+EXPERIMENTAL {
+	global:
+
+	rte_pci_device_kdriver_name;
+	rte_pci_parse_sysfs_resource;
+
+} DPDK_17.11;