[v2,07/41] common/mlx5: retrieve the device index and name

Message ID 20211007184350.73858-8-srikanth.k@oneconvergence.com (mailing list archive)
State New
Delegated to: Raslan Darawsheh
Headers
Series add MLX5 FreeBSD support |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Srikanth Kaka Oct. 7, 2021, 6:43 p.m. UTC
  The mlx5_core kernel driver creates a sysctl variable
dev.mlx5_core.0.ifname: mce0

Using this the device index and interface name are obtained
The interface name can be used by PMD to communicate with
FreeBSD network stack

Signed-off-by: Srikanth Kaka <srikanth.k@oneconvergence.com>
Signed-off-by: Vag Singh <vag.singh@oneconvergence.com>
Signed-off-by: Anand Thulasiram <avelu@juniper.net>
---
 drivers/common/mlx5/freebsd/mlx5_common_os.c | 44 ++++++++++++++++++++
 drivers/common/mlx5/freebsd/mlx5_common_os.h |  6 +++
 2 files changed, 50 insertions(+)
  

Patch

diff --git a/drivers/common/mlx5/freebsd/mlx5_common_os.c b/drivers/common/mlx5/freebsd/mlx5_common_os.c
index 9c3cd1e4e6..23370857eb 100644
--- a/drivers/common/mlx5/freebsd/mlx5_common_os.c
+++ b/drivers/common/mlx5/freebsd/mlx5_common_os.c
@@ -4,14 +4,58 @@ 
 
 #include <unistd.h>
 #include <stdio.h>
+#include <net/if.h>
 #include <sys/sysctl.h>
 
 #include <rte_errno.h>
 
 #include "mlx5_common.h"
 #include "mlx5_common_log.h"
+#include "mlx5_common_os.h"
 #include "mlx5_glue.h"
 
+/**
+ * Derive IB dev index from devpath
+ */
+int
+mlx5_get_ibvindex(const char *ibdev_path)
+{
+	unsigned int ibv_idx;
+
+	MLX5_ASSERT(ibdev_path);
+	if (sscanf(ibdev_path, "/sys/class/infiniband/mlx5_%u",
+		   &ibv_idx) != 1)
+		ibv_idx = -1;
+
+	return ibv_idx;
+}
+
+int
+mlx5_get_ifname_sysfs(const char *ibdev_path, char *ifname)
+{
+	char buffer[MLX5_SYSCTL_BY_NAME_SIZE];
+	char name[IF_NAMESIZE];
+	size_t len = IF_NAMESIZE;
+	unsigned int idx;
+
+	idx = mlx5_get_ibvindex(ibdev_path);
+	if (idx < 0) {
+		rte_errno = EINVAL;
+		return -rte_errno;
+	}
+
+	snprintf(buffer, sizeof(buffer), "dev.mlx5_core.%u.ifname", idx);
+	if (sysctlbyname(buffer, &name, &len, NULL, 0)) {
+		DRV_LOG(ERR, "port %u failed to get interface name: %s",
+			idx, strerror(errno));
+		rte_errno = errno;
+		return -rte_errno;
+	}
+	strncpy(ifname, name, IF_NAMESIZE);
+
+	return 0;
+}
+
 /**
  * Initialization routine for run-time dependency on rdma-core.
  */
diff --git a/drivers/common/mlx5/freebsd/mlx5_common_os.h b/drivers/common/mlx5/freebsd/mlx5_common_os.h
index dab67ecbf0..60e7c4f21f 100644
--- a/drivers/common/mlx5/freebsd/mlx5_common_os.h
+++ b/drivers/common/mlx5/freebsd/mlx5_common_os.h
@@ -18,6 +18,8 @@ 
 #include "mlx5_autoconf.h"
 #include "mlx5_glue.h"
 
+#define MLX5_SYSCTL_BY_NAME_SIZE 100
+
 /**
  * Get device name. Given an ibv_device pointer - return a
  * pointer to the corresponding device name.
@@ -287,4 +289,8 @@  mlx5_os_free(void *addr)
 {
 	free(addr);
 }
+
+int
+mlx5_get_ibvindex(const char *ibdev_path);
+
 #endif /* RTE_PMD_MLX5_COMMON_OS_H_ */