[v4,38/42] net/virtio: use rte strerror

Message ID 20241023082852.2780488-39-huangdengdui@huawei.com (mailing list archive)
State Changes Requested, archived
Delegated to: Thomas Monjalon
Headers
Series replace strerror |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

huangdengdui Oct. 23, 2024, 8:28 a.m. UTC
The function strerror() is insecure in a multi-thread environment.
This patch uses rte_strerror() to replace it.

Cc: stable@dpdk.org

Signed-off-by: Dengdui Huang <huangdengdui@huawei.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
---
 drivers/net/virtio/virtio_user/vhost_kernel.c |  8 +++---
 .../net/virtio/virtio_user/vhost_kernel_tap.c | 25 ++++++++++---------
 drivers/net/virtio/virtio_user/vhost_user.c   | 20 +++++++--------
 drivers/net/virtio/virtio_user/vhost_vdpa.c   | 12 ++++-----
 .../net/virtio/virtio_user/virtio_user_dev.c  |  8 +++---
 drivers/net/virtio/virtio_user_ethdev.c       |  6 ++---
 6 files changed, 40 insertions(+), 39 deletions(-)
  

Patch

diff --git a/drivers/net/virtio/virtio_user/vhost_kernel.c b/drivers/net/virtio/virtio_user/vhost_kernel.c
index e42bb35935..b1dec119fc 100644
--- a/drivers/net/virtio/virtio_user/vhost_kernel.c
+++ b/drivers/net/virtio/virtio_user/vhost_kernel.c
@@ -92,7 +92,7 @@  vhost_kernel_ioctl(int fd, uint64_t request, void *arg)
 	ret = ioctl(fd, request, arg);
 	if (ret) {
 		PMD_DRV_LOG(ERR, "Vhost-kernel ioctl %"PRIu64" failed (%s)",
-				request, strerror(errno));
+				request, rte_strerror(errno));
 		return -1;
 	}
 
@@ -428,7 +428,7 @@  vhost_kernel_setup(struct virtio_user_dev *dev)
 	for (i = 0; i < dev->max_queue_pairs; ++i) {
 		vhostfd = open(dev->path, O_RDWR);
 		if (vhostfd < 0) {
-			PMD_DRV_LOG(ERR, "fail to open %s, %s", dev->path, strerror(errno));
+			PMD_DRV_LOG(ERR, "fail to open %s, %s", dev->path, rte_strerror(errno));
 			goto err_tapfds;
 		}
 		data->vhostfds[i] = vhostfd;
@@ -511,14 +511,14 @@  vhost_kernel_set_backend(int vhostfd, int tapfd)
 	f.index = 0;
 	if (ioctl(vhostfd, VHOST_NET_SET_BACKEND, &f) < 0) {
 		PMD_DRV_LOG(ERR, "VHOST_NET_SET_BACKEND fails, %s",
-				strerror(errno));
+				rte_strerror(errno));
 		return -1;
 	}
 
 	f.index = 1;
 	if (ioctl(vhostfd, VHOST_NET_SET_BACKEND, &f) < 0) {
 		PMD_DRV_LOG(ERR, "VHOST_NET_SET_BACKEND fails, %s",
-				strerror(errno));
+				rte_strerror(errno));
 		return -1;
 	}
 
diff --git a/drivers/net/virtio/virtio_user/vhost_kernel_tap.c b/drivers/net/virtio/virtio_user/vhost_kernel_tap.c
index 611e2e25ec..4542ccbf04 100644
--- a/drivers/net/virtio/virtio_user/vhost_kernel_tap.c
+++ b/drivers/net/virtio/virtio_user/vhost_kernel_tap.c
@@ -13,6 +13,7 @@ 
 #include <limits.h>
 
 #include <rte_ether.h>
+#include <rte_errno.h>
 
 #include "vhost_kernel_tap.h"
 #include "../virtio_logs.h"
@@ -27,12 +28,12 @@  tap_support_features(unsigned int *tap_features)
 	tapfd = open(PATH_NET_TUN, O_RDWR);
 	if (tapfd < 0) {
 		PMD_DRV_LOG(ERR, "fail to open %s: %s",
-			    PATH_NET_TUN, strerror(errno));
+			    PATH_NET_TUN, rte_strerror(errno));
 		return -1;
 	}
 
 	if (ioctl(tapfd, TUNGETFEATURES, tap_features) == -1) {
-		PMD_DRV_LOG(ERR, "TUNGETFEATURES failed: %s", strerror(errno));
+		PMD_DRV_LOG(ERR, "TUNGETFEATURES failed: %s", rte_strerror(errno));
 		close(tapfd);
 		return -1;
 	}
@@ -49,11 +50,11 @@  tap_open(const char *ifname, unsigned int r_flags, bool multi_queue)
 
 	tapfd = open(PATH_NET_TUN, O_RDWR);
 	if (tapfd < 0) {
-		PMD_DRV_LOG(ERR, "fail to open %s: %s", PATH_NET_TUN, strerror(errno));
+		PMD_DRV_LOG(ERR, "fail to open %s: %s", PATH_NET_TUN, rte_strerror(errno));
 		return -1;
 	}
 	if (fcntl(tapfd, F_SETFL, O_NONBLOCK) < 0) {
-		PMD_DRV_LOG(ERR, "fcntl tapfd failed: %s", strerror(errno));
+		PMD_DRV_LOG(ERR, "fcntl tapfd failed: %s", rte_strerror(errno));
 		close(tapfd);
 		return -1;
 	}
@@ -68,12 +69,12 @@  tap_open(const char *ifname, unsigned int r_flags, bool multi_queue)
 		if (multi_queue) {
 			PMD_DRV_LOG(DEBUG,
 				"TUNSETIFF failed (will retry without IFF_MULTI_QUEUE): %s",
-				strerror(errno));
+				rte_strerror(errno));
 			multi_queue = false;
 			goto retry_mono_q;
 		}
 
-		PMD_DRV_LOG(ERR, "TUNSETIFF failed: %s", strerror(errno));
+		PMD_DRV_LOG(ERR, "TUNSETIFF failed: %s", rte_strerror(errno));
 		close(tapfd);
 		tapfd = -1;
 	}
@@ -88,7 +89,7 @@  tap_get_name(int tapfd, char **name)
 
 	memset(&ifr, 0, sizeof(ifr));
 	if (ioctl(tapfd, TUNGETIFF, (void *)&ifr) == -1) {
-		PMD_DRV_LOG(ERR, "TUNGETIFF failed: %s", strerror(errno));
+		PMD_DRV_LOG(ERR, "TUNGETIFF failed: %s", rte_strerror(errno));
 		return -1;
 	}
 	ret = asprintf(name, "%s", ifr.ifr_name);
@@ -104,7 +105,7 @@  tap_get_flags(int tapfd, unsigned int *tap_flags)
 
 	memset(&ifr, 0, sizeof(ifr));
 	if (ioctl(tapfd, TUNGETIFF, (void *)&ifr) == -1) {
-		PMD_DRV_LOG(ERR, "TUNGETIFF failed: %s", strerror(errno));
+		PMD_DRV_LOG(ERR, "TUNGETIFF failed: %s", rte_strerror(errno));
 		return -1;
 	}
 	*tap_flags = ifr.ifr_flags;
@@ -120,7 +121,7 @@  tap_set_mac(int tapfd, uint8_t *mac)
 	ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
 	memcpy(ifr.ifr_hwaddr.sa_data, mac, RTE_ETHER_ADDR_LEN);
 	if (ioctl(tapfd, SIOCSIFHWADDR, (void *)&ifr) == -1) {
-		PMD_DRV_LOG(ERR, "SIOCSIFHWADDR failed: %s", strerror(errno));
+		PMD_DRV_LOG(ERR, "SIOCSIFHWADDR failed: %s", rte_strerror(errno));
 		return -1;
 	}
 	return 0;
@@ -155,7 +156,7 @@  vhost_kernel_tap_set_offload(int fd, uint64_t features)
 		offload &= ~TUN_F_UFO;
 		if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
 			PMD_DRV_LOG(ERR, "TUNSETOFFLOAD ioctl() failed: %s",
-				strerror(errno));
+				rte_strerror(errno));
 			return -1;
 		}
 	}
@@ -175,12 +176,12 @@  vhost_kernel_tap_setup(int tapfd, int hdr_size, uint64_t features)
 	 * max_mem_regions, supported in newer version linux kernel
 	 */
 	if (ioctl(tapfd, TUNSETVNETHDRSZ, &hdr_size) < 0) {
-		PMD_DRV_LOG(ERR, "TUNSETVNETHDRSZ failed: %s", strerror(errno));
+		PMD_DRV_LOG(ERR, "TUNSETVNETHDRSZ failed: %s", rte_strerror(errno));
 		return -1;
 	}
 
 	if (ioctl(tapfd, TUNSETSNDBUF, &sndbuf) < 0) {
-		PMD_DRV_LOG(ERR, "TUNSETSNDBUF failed: %s", strerror(errno));
+		PMD_DRV_LOG(ERR, "TUNSETSNDBUF failed: %s", rte_strerror(errno));
 		return -1;
 	}
 
diff --git a/drivers/net/virtio/virtio_user/vhost_user.c b/drivers/net/virtio/virtio_user/vhost_user.c
index c10252506b..d3742c6c62 100644
--- a/drivers/net/virtio/virtio_user/vhost_user.c
+++ b/drivers/net/virtio/virtio_user/vhost_user.c
@@ -136,7 +136,7 @@  vhost_user_write(int fd, struct vhost_user_msg *msg, int *fds, int fd_num)
 	} while (r < 0 && errno == EINTR);
 
 	if (r < 0)
-		PMD_DRV_LOG(ERR, "Failed to send msg: %s", strerror(errno));
+		PMD_DRV_LOG(ERR, "Failed to send msg: %s", rte_strerror(errno));
 
 	return r;
 }
@@ -149,7 +149,7 @@  vhost_user_read(int fd, struct vhost_user_msg *msg)
 
 	ret = recv(fd, (void *)msg, sz_hdr, 0);
 	if (ret < 0) {
-		PMD_DRV_LOG(ERR, "Failed to recv msg header: %s", strerror(errno));
+		PMD_DRV_LOG(ERR, "Failed to recv msg header: %s", rte_strerror(errno));
 		return -1;
 	} else if (ret < sz_hdr) {
 		PMD_DRV_LOG(ERR, "Failed to recv msg hdr: %d instead of %d.",
@@ -175,7 +175,7 @@  vhost_user_read(int fd, struct vhost_user_msg *msg)
 	if (sz_payload) {
 		ret = recv(fd, (void *)((char *)msg + sz_hdr), sz_payload, 0);
 		if (ret < 0) {
-			PMD_DRV_LOG(ERR, "Failed to recv msg payload: %s", strerror(errno));
+			PMD_DRV_LOG(ERR, "Failed to recv msg payload: %s", rte_strerror(errno));
 			return -1;
 		} else if (ret < sz_payload) {
 			PMD_DRV_LOG(ERR, "Failed to recv msg payload: %d instead of %u.",
@@ -750,7 +750,7 @@  vhost_user_start_server(struct virtio_user_dev *dev, struct sockaddr_un *un)
 	ret = bind(fd, (struct sockaddr *)un, sizeof(*un));
 	if (ret < 0) {
 		PMD_DRV_LOG(ERR, "failed to bind to %s: %s; remove it and try again",
-			    dev->path, strerror(errno));
+			    dev->path, rte_strerror(errno));
 		return -1;
 	}
 	ret = listen(fd, MAX_VIRTIO_USER_BACKLOG);
@@ -761,13 +761,13 @@  vhost_user_start_server(struct virtio_user_dev *dev, struct sockaddr_un *un)
 	data->vhostfd = accept(fd, NULL, NULL);
 	if (data->vhostfd < 0) {
 		PMD_DRV_LOG(ERR, "Failed to accept initial client connection (%s)",
-				strerror(errno));
+				rte_strerror(errno));
 		return -1;
 	}
 
 	flag = fcntl(fd, F_GETFL);
 	if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
-		PMD_DRV_LOG(ERR, "fcntl failed, %s", strerror(errno));
+		PMD_DRV_LOG(ERR, "fcntl failed, %s", rte_strerror(errno));
 		return -1;
 	}
 
@@ -835,15 +835,15 @@  vhost_user_setup(struct virtio_user_dev *dev)
 
 	fd = socket(AF_UNIX, SOCK_STREAM, 0);
 	if (fd < 0) {
-		PMD_DRV_LOG(ERR, "socket() error, %s", strerror(errno));
+		PMD_DRV_LOG(ERR, "socket() error, %s", rte_strerror(errno));
 		goto err_data;
 	}
 
 	flag = fcntl(fd, F_GETFD);
 	if (flag == -1)
-		PMD_DRV_LOG(WARNING, "fcntl get fd failed, %s", strerror(errno));
+		PMD_DRV_LOG(WARNING, "fcntl get fd failed, %s", rte_strerror(errno));
 	else if (fcntl(fd, F_SETFD, flag | FD_CLOEXEC) < 0)
-		PMD_DRV_LOG(WARNING, "fcntl set fd failed, %s", strerror(errno));
+		PMD_DRV_LOG(WARNING, "fcntl set fd failed, %s", rte_strerror(errno));
 
 	memset(&un, 0, sizeof(un));
 	un.sun_family = AF_UNIX;
@@ -857,7 +857,7 @@  vhost_user_setup(struct virtio_user_dev *dev)
 		}
 	} else {
 		if (connect(fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
-			PMD_DRV_LOG(ERR, "connect error, %s", strerror(errno));
+			PMD_DRV_LOG(ERR, "connect error, %s", rte_strerror(errno));
 			goto err_socket;
 		}
 		data->vhostfd = fd;
diff --git a/drivers/net/virtio/virtio_user/vhost_vdpa.c b/drivers/net/virtio/virtio_user/vhost_vdpa.c
index bc3e2a9af5..f8c883ee64 100644
--- a/drivers/net/virtio/virtio_user/vhost_vdpa.c
+++ b/drivers/net/virtio/virtio_user/vhost_vdpa.c
@@ -93,7 +93,7 @@  vhost_vdpa_ioctl(int fd, uint64_t request, void *arg)
 	ret = ioctl(fd, request, arg);
 	if (ret) {
 		PMD_DRV_LOG(ERR, "Vhost-vDPA ioctl %"PRIu64" failed (%s)",
-				request, strerror(errno));
+				request, rte_strerror(errno));
 		return -1;
 	}
 
@@ -187,7 +187,7 @@  vhost_vdpa_iotlb_batch_begin(struct virtio_user_dev *dev)
 
 	if (write(data->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
 		PMD_DRV_LOG(ERR, "Failed to send IOTLB batch begin (%s)",
-				strerror(errno));
+				rte_strerror(errno));
 		return -1;
 	}
 
@@ -213,7 +213,7 @@  vhost_vdpa_iotlb_batch_end(struct virtio_user_dev *dev)
 
 	if (write(data->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
 		PMD_DRV_LOG(ERR, "Failed to send IOTLB batch end (%s)",
-				strerror(errno));
+				rte_strerror(errno));
 		return -1;
 	}
 
@@ -244,7 +244,7 @@  vhost_vdpa_dma_map(struct virtio_user_dev *dev, void *addr,
 
 	if (write(data->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
 		PMD_DRV_LOG(ERR, "Failed to send IOTLB update (%s)",
-				strerror(errno));
+				rte_strerror(errno));
 		return -1;
 	}
 
@@ -273,7 +273,7 @@  vhost_vdpa_dma_unmap(struct virtio_user_dev *dev, __rte_unused void *addr,
 
 	if (write(data->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
 		PMD_DRV_LOG(ERR, "Failed to send IOTLB invalidate (%s)",
-				strerror(errno));
+				rte_strerror(errno));
 		return -1;
 	}
 
@@ -531,7 +531,7 @@  vhost_vdpa_setup(struct virtio_user_dev *dev)
 	data->vhostfd = open(dev->path, O_RDWR);
 	if (data->vhostfd < 0) {
 		PMD_DRV_LOG(ERR, "Failed to open %s: %s",
-				dev->path, strerror(errno));
+				dev->path, rte_strerror(errno));
 		free(data);
 		return -1;
 	}
diff --git a/drivers/net/virtio/virtio_user/virtio_user_dev.c b/drivers/net/virtio/virtio_user/virtio_user_dev.c
index 2997d2bd26..33389c702e 100644
--- a/drivers/net/virtio/virtio_user/virtio_user_dev.c
+++ b/drivers/net/virtio/virtio_user/virtio_user_dev.c
@@ -60,13 +60,13 @@  virtio_user_init_notify_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
 	dev->callfds[queue_sel] = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
 	if (dev->callfds[queue_sel] < 0) {
 		PMD_DRV_LOG(ERR, "(%s) Failed to setup callfd for queue %u: %s",
-				dev->path, queue_sel, strerror(errno));
+				dev->path, queue_sel, rte_strerror(errno));
 		return -1;
 	}
 	dev->kickfds[queue_sel] = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
 	if (dev->kickfds[queue_sel] < 0) {
 		PMD_DRV_LOG(ERR, "(%s) Failed to setup kickfd for queue %u: %s",
-				dev->path, queue_sel, strerror(errno));
+				dev->path, queue_sel, rte_strerror(errno));
 		return -1;
 	}
 
@@ -1113,7 +1113,7 @@  virtio_user_control_queue_notify(struct virtqueue *vq, void *cookie)
 	if (!dev->notify_area) {
 		if (write(dev->kickfds[vq->vq_queue_index], &notify_data, sizeof(notify_data)) < 0)
 			PMD_DRV_LOG(ERR, "failed to kick backend: %s",
-				    strerror(errno));
+				    rte_strerror(errno));
 		return;
 	} else if (!virtio_with_feature(&dev->hw, VIRTIO_F_NOTIFICATION_DATA)) {
 		rte_write16(vq->vq_queue_index, vq->notify_addr);
@@ -1358,7 +1358,7 @@  virtio_user_dev_server_reconnect(struct virtio_user_dev *dev)
 
 	if (dev->ops->get_features(dev, &dev->device_features) < 0) {
 		PMD_INIT_LOG(ERR, "get_features failed: %s",
-			     strerror(errno));
+			     rte_strerror(errno));
 		return -1;
 	}
 
diff --git a/drivers/net/virtio/virtio_user_ethdev.c b/drivers/net/virtio/virtio_user_ethdev.c
index 747dddeb2e..5a23d63012 100644
--- a/drivers/net/virtio/virtio_user_ethdev.c
+++ b/drivers/net/virtio/virtio_user_ethdev.c
@@ -285,7 +285,7 @@  virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
 		if (write(dev->kickfds[vq->vq_queue_index], &notify_data,
 			  sizeof(notify_data)) < 0)
 			PMD_DRV_LOG(ERR, "failed to kick backend: %s",
-				    strerror(errno));
+				    rte_strerror(errno));
 		return;
 	} else if (!virtio_with_feature(hw, VIRTIO_F_NOTIFICATION_DATA)) {
 		rte_write16(vq->vq_queue_index, vq->notify_addr);
@@ -417,7 +417,7 @@  vdpa_dynamic_major_num(void)
 	fp = fopen("/proc/devices", "r");
 	if (fp == NULL) {
 		PMD_INIT_LOG(ERR, "Cannot open /proc/devices: %s",
-			     strerror(errno));
+			     rte_strerror(errno));
 		return UNNAMED_MAJOR;
 	}
 
@@ -444,7 +444,7 @@  virtio_user_backend_type(const char *path)
 			return VIRTIO_USER_BACKEND_VHOST_USER;
 
 		PMD_INIT_LOG(ERR, "Stat fails: %s (%s)", path,
-			     strerror(errno));
+			     rte_strerror(errno));
 		return VIRTIO_USER_BACKEND_UNKNOWN;
 	}