[PATCH v3 2/2] net/mlx5: improve debug dump file path handling

Yang Ming mosesyyoung at gmail.com
Fri Aug 29 16:49:53 CEST 2025


The current implementation always tries to open debug dump files
under /var/log, which may not be writable in containerized or
restricted environments (e.g. when the filesystem is mounted as
read-only).

This patch introduces an OS-specific helper function
mlx5_os_debug_dump_file_open() to unify the logic:

* On Linux:
  1. Try /var/log if it is writable (kept for backward
     compatibility).
  2. Fallback to the DPDK runtime directory.
  3. Finally, use the current working directory.

* On Windows:
  1. Use the DPDK runtime directory.
  2. Fallback to the current working directory.

This preserves the previous directory preference while ensuring
debug dumps can still be written in restricted environments such
as containers. The structure of the open logic is kept unchanged
so that test cases and expected logs remain valid.

Signed-off-by: Yang Ming <mosesyyoung at gmail.com>
---
 drivers/net/mlx5/linux/mlx5_os.c   | 44 ++++++++++++++++++++++++++++++
 drivers/net/mlx5/mlx5.h            |  1 +
 drivers/net/mlx5/mlx5_rxtx.c       | 16 ++---------
 drivers/net/mlx5/windows/mlx5_os.c | 30 ++++++++++++++++++++
 4 files changed, 78 insertions(+), 13 deletions(-)

diff --git a/drivers/net/mlx5/linux/mlx5_os.c b/drivers/net/mlx5/linux/mlx5_os.c
index 696a3e12c7..fdb2102cd5 100644
--- a/drivers/net/mlx5/linux/mlx5_os.c
+++ b/drivers/net/mlx5/linux/mlx5_os.c
@@ -3020,6 +3020,50 @@ mlx5_os_net_cleanup(void)
 	mlx5_pmd_socket_uninit();
 }
 
+/* Default system log directory on Linux. */
+#define MLX5_SYSTEM_LOG_DIR "/var/log"
+
+/*
+ * Open a debug dump file on Linux.
+ *
+ * The function attempts to create/open the file in the following order:
+ * 1. /var/log (if writable),
+ * 2. EAL runtime directory,
+ * 3. current working directory ("./").
+ */
+FILE *
+mlx5_os_debug_dump_file_open(const char *fname)
+{
+	FILE *fd = NULL;
+
+	if (access(MLX5_SYSTEM_LOG_DIR, W_OK) == 0) {
+		MKSTR(path, "%s/%s", MLX5_SYSTEM_LOG_DIR, fname);
+		fd = fopen(path, "a+");
+		if (fd) {
+			DRV_LOG(INFO, "New debug dump in file %s", path);
+			return fd;
+		}
+		DRV_LOG(WARNING, "cannot open %s for debug dump", path);
+	}
+
+	MKSTR(path2, "%s/%s", rte_eal_get_runtime_dir(), fname);
+	fd = fopen(path2, "a+");
+	if (fd) {
+		DRV_LOG(INFO, "New debug dump in file %s", path2);
+		return fd;
+	}
+	DRV_LOG(WARNING, "cannot open %s for debug dump", path2);
+
+	MKSTR(path3, "./%s", fname);
+	fd = fopen(path3, "a+");
+	if (fd)
+		DRV_LOG(INFO, "New debug dump in file %s", path3);
+	else
+		DRV_LOG(ERR, "cannot open %s for debug dump", path3);
+
+	return fd;
+}
+
 /**
  * Install shared asynchronous device events handler.
  * This function is implemented to support event sharing
diff --git a/drivers/net/mlx5/mlx5.h b/drivers/net/mlx5/mlx5.h
index c08894cd03..fc7ac08039 100644
--- a/drivers/net/mlx5/mlx5.h
+++ b/drivers/net/mlx5/mlx5.h
@@ -2597,6 +2597,7 @@ int mlx5_os_capabilities_prepare(struct mlx5_dev_ctx_shared *sh);
 void mlx5_os_free_shared_dr(struct mlx5_priv *priv);
 int mlx5_os_net_probe(struct mlx5_common_device *cdev,
 		      struct mlx5_kvargs_ctrl *mkvlist);
+FILE *mlx5_os_debug_dump_file_open(const char *name);
 void mlx5_os_dev_shared_handler_install(struct mlx5_dev_ctx_shared *sh);
 void mlx5_os_dev_shared_handler_uninstall(struct mlx5_dev_ctx_shared *sh);
 void mlx5_os_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
diff --git a/drivers/net/mlx5/mlx5_rxtx.c b/drivers/net/mlx5/mlx5_rxtx.c
index 82c5481ad7..b35365ffd3 100644
--- a/drivers/net/mlx5/mlx5_rxtx.c
+++ b/drivers/net/mlx5/mlx5_rxtx.c
@@ -391,7 +391,6 @@ mlx5_set_swp_types_table(void)
 	}
 }
 
-#define MLX5_SYSTEM_LOG_DIR "/var/log"
 /**
  * Dump debug information to log file.
  *
@@ -411,20 +410,11 @@ mlx5_dump_debug_information(const char *fname, const char *hex_title,
 {
 	FILE *fd;
 
-	MKSTR(path, "%s/%s", MLX5_SYSTEM_LOG_DIR, fname);
-	fd = fopen(path, "a+");
+	fd = mlx5_os_debug_dump_file_open(fname);
 	if (!fd) {
-		DRV_LOG(WARNING, "cannot open %s for debug dump", path);
-		MKSTR(path2, "./%s", fname);
-		fd = fopen(path2, "a+");
-		if (!fd) {
-			DRV_LOG(ERR, "cannot open %s for debug dump", path2);
-			return;
-		}
-		DRV_LOG(INFO, "New debug dump in file %s", path2);
-	} else {
-		DRV_LOG(INFO, "New debug dump in file %s", path);
+		return;
 	}
+
 	if (hex_title)
 		rte_hexdump(fd, hex_title, buf, hex_len);
 	else
diff --git a/drivers/net/mlx5/windows/mlx5_os.c b/drivers/net/mlx5/windows/mlx5_os.c
index d583730066..734b71e3f7 100644
--- a/drivers/net/mlx5/windows/mlx5_os.c
+++ b/drivers/net/mlx5/windows/mlx5_os.c
@@ -629,6 +629,36 @@ mlx5_dev_spawn(struct rte_device *dpdk_dev,
 	return NULL;
 }
 
+/*
+ * Open a debug dump file on Windows.
+ *
+ * The function attempts to create/open the file in the following order:
+ * 1. EAL runtime directory,
+ * 2. current working directory ("./").
+ */
+FILE *
+mlx5_os_debug_dump_file_open(const char *fname)
+{
+	FILE *fd = NULL;
+
+	MKSTR(path, "%s/%s", rte_eal_get_runtime_dir(), fname);
+	fd = fopen(path, "a+");
+	if (fd) {
+		DRV_LOG(INFO, "New debug dump in file %s", path);
+		return fd;
+	}
+	DRV_LOG(WARNING, "cannot open %s for debug dump", path);
+
+	MKSTR(path2, "./%s", fname);
+	fd = fopen(path2, "a+");
+	if (fd)
+		DRV_LOG(INFO, "New debug dump in file %s", path2);
+	else
+		DRV_LOG(ERR, "cannot open %s for debug dump", path2);
+
+	return fd;
+}
+
 /**
  * This function should share events between multiple ports of single IB
  * device.  Currently it has no support under Windows.
-- 
2.49.0.windows.1



More information about the dev mailing list