DPDK patches and discussions
 help / color / mirror / Atom feed
* [PATCH 1/2] vhost: remove unneeded max enums
@ 2022-04-25 12:54 David Marchand
  2022-04-25 12:54 ` [PATCH 2/2] vhost: validate fds attached to messages David Marchand
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: David Marchand @ 2022-04-25 12:54 UTC (permalink / raw)
  To: dev; +Cc: maxime.coquelin, chenbo.xia

Move message handler description and callbacks into a single array and
remove unneeded VHOST_USER_MAX and VHOST_SLAVE_MAX enums.

Signed-off-by: David Marchand <david.marchand@redhat.com>
---
 drivers/net/virtio/virtio_user/vhost_user.c |   1 -
 examples/vhost_blk/blk_spec.h               |   1 -
 lib/vhost/vhost_user.c                      | 175 +++++++++-----------
 lib/vhost/vhost_user.h                      |   2 -
 4 files changed, 76 insertions(+), 103 deletions(-)

diff --git a/drivers/net/virtio/virtio_user/vhost_user.c b/drivers/net/virtio/virtio_user/vhost_user.c
index 00d0dcaa74..7d1749114d 100644
--- a/drivers/net/virtio/virtio_user/vhost_user.c
+++ b/drivers/net/virtio/virtio_user/vhost_user.c
@@ -78,7 +78,6 @@ enum vhost_user_request {
 	VHOST_USER_SET_VRING_ENABLE = 18,
 	VHOST_USER_SET_STATUS = 39,
 	VHOST_USER_GET_STATUS = 40,
-	VHOST_USER_MAX
 };
 
 struct vhost_user_msg {
diff --git a/examples/vhost_blk/blk_spec.h b/examples/vhost_blk/blk_spec.h
index 594bd6a29b..3c54f70eaf 100644
--- a/examples/vhost_blk/blk_spec.h
+++ b/examples/vhost_blk/blk_spec.h
@@ -56,7 +56,6 @@ enum vhost_user_request {
 	VHOST_USER_SET_PROTOCOL_FEATURES = 16,
 	VHOST_USER_GET_QUEUE_NUM = 17,
 	VHOST_USER_SET_VRING_ENABLE = 18,
-	VHOST_USER_MAX
 };
 
 /** Get/set config msg payload */
diff --git a/lib/vhost/vhost_user.c b/lib/vhost/vhost_user.c
index 1d390677fa..17cfeafa16 100644
--- a/lib/vhost/vhost_user.c
+++ b/lib/vhost/vhost_user.c
@@ -56,40 +56,12 @@
 #define INFLIGHT_ALIGNMENT	64
 #define INFLIGHT_VERSION	0x1
 
-static const char *vhost_message_str[VHOST_USER_MAX] = {
-	[VHOST_USER_NONE] = "VHOST_USER_NONE",
-	[VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
-	[VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
-	[VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
-	[VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
-	[VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
-	[VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
-	[VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
-	[VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
-	[VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
-	[VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
-	[VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
-	[VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
-	[VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
-	[VHOST_USER_SET_VRING_ERR]  = "VHOST_USER_SET_VRING_ERR",
-	[VHOST_USER_GET_PROTOCOL_FEATURES]  = "VHOST_USER_GET_PROTOCOL_FEATURES",
-	[VHOST_USER_SET_PROTOCOL_FEATURES]  = "VHOST_USER_SET_PROTOCOL_FEATURES",
-	[VHOST_USER_GET_QUEUE_NUM]  = "VHOST_USER_GET_QUEUE_NUM",
-	[VHOST_USER_SET_VRING_ENABLE]  = "VHOST_USER_SET_VRING_ENABLE",
-	[VHOST_USER_SEND_RARP]  = "VHOST_USER_SEND_RARP",
-	[VHOST_USER_NET_SET_MTU]  = "VHOST_USER_NET_SET_MTU",
-	[VHOST_USER_SET_SLAVE_REQ_FD]  = "VHOST_USER_SET_SLAVE_REQ_FD",
-	[VHOST_USER_IOTLB_MSG]  = "VHOST_USER_IOTLB_MSG",
-	[VHOST_USER_CRYPTO_CREATE_SESS] = "VHOST_USER_CRYPTO_CREATE_SESS",
-	[VHOST_USER_CRYPTO_CLOSE_SESS] = "VHOST_USER_CRYPTO_CLOSE_SESS",
-	[VHOST_USER_POSTCOPY_ADVISE]  = "VHOST_USER_POSTCOPY_ADVISE",
-	[VHOST_USER_POSTCOPY_LISTEN]  = "VHOST_USER_POSTCOPY_LISTEN",
-	[VHOST_USER_POSTCOPY_END]  = "VHOST_USER_POSTCOPY_END",
-	[VHOST_USER_GET_INFLIGHT_FD] = "VHOST_USER_GET_INFLIGHT_FD",
-	[VHOST_USER_SET_INFLIGHT_FD] = "VHOST_USER_SET_INFLIGHT_FD",
-	[VHOST_USER_SET_STATUS] = "VHOST_USER_SET_STATUS",
-	[VHOST_USER_GET_STATUS] = "VHOST_USER_GET_STATUS",
-};
+typedef struct vhost_message_handler {
+	const char *description;
+	int (*callback)(struct virtio_net **pdev, struct vhu_msg_context *ctx,
+		int main_fd);
+} vhost_message_handler_t;
+static vhost_message_handler_t vhost_message_handlers[];
 
 static int send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx);
 static int read_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx);
@@ -122,7 +94,7 @@ validate_msg_fds(struct virtio_net *dev, struct vhu_msg_context *ctx, int expect
 
 	VHOST_LOG_CONFIG(ERR, "(%s) expect %d FDs for request %s, received %d\n",
 		dev->ifname, expected_fds,
-		vhost_message_str[ctx->msg.request.master],
+		vhost_message_handlers[ctx->msg.request.master].description,
 		ctx->fd_num);
 
 	close_msg_fds(ctx);
@@ -2754,42 +2726,44 @@ vhost_user_set_status(struct virtio_net **pdev,
 	return RTE_VHOST_MSG_RESULT_OK;
 }
 
-typedef int (*vhost_message_handler_t)(struct virtio_net **pdev,
-					struct vhu_msg_context *ctx,
-					int main_fd);
-
-static vhost_message_handler_t vhost_message_handlers[VHOST_USER_MAX] = {
-	[VHOST_USER_NONE] = NULL,
-	[VHOST_USER_GET_FEATURES] = vhost_user_get_features,
-	[VHOST_USER_SET_FEATURES] = vhost_user_set_features,
-	[VHOST_USER_SET_OWNER] = vhost_user_set_owner,
-	[VHOST_USER_RESET_OWNER] = vhost_user_reset_owner,
-	[VHOST_USER_SET_MEM_TABLE] = vhost_user_set_mem_table,
-	[VHOST_USER_SET_LOG_BASE] = vhost_user_set_log_base,
-	[VHOST_USER_SET_LOG_FD] = vhost_user_set_log_fd,
-	[VHOST_USER_SET_VRING_NUM] = vhost_user_set_vring_num,
-	[VHOST_USER_SET_VRING_ADDR] = vhost_user_set_vring_addr,
-	[VHOST_USER_SET_VRING_BASE] = vhost_user_set_vring_base,
-	[VHOST_USER_GET_VRING_BASE] = vhost_user_get_vring_base,
-	[VHOST_USER_SET_VRING_KICK] = vhost_user_set_vring_kick,
-	[VHOST_USER_SET_VRING_CALL] = vhost_user_set_vring_call,
-	[VHOST_USER_SET_VRING_ERR] = vhost_user_set_vring_err,
-	[VHOST_USER_GET_PROTOCOL_FEATURES] = vhost_user_get_protocol_features,
-	[VHOST_USER_SET_PROTOCOL_FEATURES] = vhost_user_set_protocol_features,
-	[VHOST_USER_GET_QUEUE_NUM] = vhost_user_get_queue_num,
-	[VHOST_USER_SET_VRING_ENABLE] = vhost_user_set_vring_enable,
-	[VHOST_USER_SEND_RARP] = vhost_user_send_rarp,
-	[VHOST_USER_NET_SET_MTU] = vhost_user_net_set_mtu,
-	[VHOST_USER_SET_SLAVE_REQ_FD] = vhost_user_set_req_fd,
-	[VHOST_USER_IOTLB_MSG] = vhost_user_iotlb_msg,
-	[VHOST_USER_POSTCOPY_ADVISE] = vhost_user_set_postcopy_advise,
-	[VHOST_USER_POSTCOPY_LISTEN] = vhost_user_set_postcopy_listen,
-	[VHOST_USER_POSTCOPY_END] = vhost_user_postcopy_end,
-	[VHOST_USER_GET_INFLIGHT_FD] = vhost_user_get_inflight_fd,
-	[VHOST_USER_SET_INFLIGHT_FD] = vhost_user_set_inflight_fd,
-	[VHOST_USER_SET_STATUS] = vhost_user_set_status,
-	[VHOST_USER_GET_STATUS] = vhost_user_get_status,
+#define VHOST_MESSAGE_HANDLERS \
+VHOST_MESSAGE_HANDLER(VHOST_USER_NONE, NULL) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_GET_FEATURES, vhost_user_get_features) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_FEATURES, vhost_user_set_features) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_OWNER, vhost_user_set_owner) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_RESET_OWNER, vhost_user_reset_owner) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_MEM_TABLE, vhost_user_set_mem_table) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_BASE, vhost_user_set_log_base) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_FD, vhost_user_set_log_fd) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_NUM, vhost_user_set_vring_num) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ADDR, vhost_user_set_vring_addr) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_BASE, vhost_user_set_vring_base) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_GET_VRING_BASE, vhost_user_get_vring_base) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_KICK, vhost_user_set_vring_kick) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_CALL, vhost_user_set_vring_call) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ERR, vhost_user_set_vring_err) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_GET_PROTOCOL_FEATURES, vhost_user_get_protocol_features) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_PROTOCOL_FEATURES, vhost_user_set_protocol_features) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_GET_QUEUE_NUM, vhost_user_get_queue_num) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ENABLE, vhost_user_set_vring_enable) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SEND_RARP, vhost_user_send_rarp) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_NET_SET_MTU, vhost_user_net_set_mtu) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_SLAVE_REQ_FD, vhost_user_set_req_fd) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_IOTLB_MSG, vhost_user_iotlb_msg) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_ADVISE, vhost_user_set_postcopy_advise) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_LISTEN, vhost_user_set_postcopy_listen) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_END, vhost_user_postcopy_end) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_GET_INFLIGHT_FD, vhost_user_get_inflight_fd) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_INFLIGHT_FD, vhost_user_set_inflight_fd) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_STATUS, vhost_user_set_status) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_GET_STATUS, vhost_user_get_status)
+
+#define VHOST_MESSAGE_HANDLER(id, handler) \
+	[id] = { #id, handler },
+static vhost_message_handler_t vhost_message_handlers[] = {
+	VHOST_MESSAGE_HANDLERS
 };
+#undef VHOST_MESSAGE_HANDLER
 
 /* return bytes# of read on success or negative val on failure. */
 static int
@@ -2946,11 +2920,12 @@ vhost_user_msg_handler(int vid, int fd)
 {
 	struct virtio_net *dev;
 	struct vhu_msg_context ctx;
+	vhost_message_handler_t *msg_handler;
 	struct rte_vdpa_device *vdpa_dev;
 	int ret;
 	int unlock_required = 0;
 	bool handled;
-	int request;
+	uint32_t request;
 	uint32_t i;
 
 	dev = get_device(vid);
@@ -2978,14 +2953,18 @@ vhost_user_msg_handler(int vid, int fd)
 
 	ret = 0;
 	request = ctx.msg.request.master;
-	if (request > VHOST_USER_NONE && request < VHOST_USER_MAX &&
-			vhost_message_str[request]) {
+	if (request > VHOST_USER_NONE && request < RTE_DIM(vhost_message_handlers))
+		msg_handler = &vhost_message_handlers[request];
+	else
+		msg_handler = NULL;
+
+	if (msg_handler != NULL && msg_handler->description != NULL) {
 		if (request != VHOST_USER_IOTLB_MSG)
 			VHOST_LOG_CONFIG(INFO, "(%s) read message %s\n",
-				dev->ifname, vhost_message_str[request]);
+				dev->ifname, msg_handler->description);
 		else
 			VHOST_LOG_CONFIG(DEBUG, "(%s) read message %s\n",
-				dev->ifname, vhost_message_str[request]);
+				dev->ifname, msg_handler->description);
 	} else {
 		VHOST_LOG_CONFIG(DEBUG, "(%s) external request %d\n", dev->ifname, request);
 	}
@@ -3048,31 +3027,29 @@ vhost_user_msg_handler(int vid, int fd)
 		}
 	}
 
-	if (request > VHOST_USER_NONE && request < VHOST_USER_MAX) {
-		if (!vhost_message_handlers[request])
-			goto skip_to_post_handle;
-		ret = vhost_message_handlers[request](&dev, &ctx, fd);
+	if (msg_handler == NULL || msg_handler->callback == NULL)
+		goto skip_to_post_handle;
 
-		switch (ret) {
-		case RTE_VHOST_MSG_RESULT_ERR:
-			VHOST_LOG_CONFIG(ERR, "(%s) processing %s failed.\n",
-					dev->ifname, vhost_message_str[request]);
-			handled = true;
-			break;
-		case RTE_VHOST_MSG_RESULT_OK:
-			VHOST_LOG_CONFIG(DEBUG, "(%s) processing %s succeeded.\n",
-					dev->ifname, vhost_message_str[request]);
-			handled = true;
-			break;
-		case RTE_VHOST_MSG_RESULT_REPLY:
-			VHOST_LOG_CONFIG(DEBUG, "(%s) processing %s succeeded and needs reply.\n",
-					dev->ifname, vhost_message_str[request]);
-			send_vhost_reply(dev, fd, &ctx);
-			handled = true;
-			break;
-		default:
-			break;
-		}
+	ret = msg_handler->callback(&dev, &ctx, fd);
+	switch (ret) {
+	case RTE_VHOST_MSG_RESULT_ERR:
+		VHOST_LOG_CONFIG(ERR, "(%s) processing %s failed.\n",
+			dev->ifname, msg_handler->description);
+		handled = true;
+		break;
+	case RTE_VHOST_MSG_RESULT_OK:
+		VHOST_LOG_CONFIG(DEBUG, "(%s) processing %s succeeded.\n",
+			dev->ifname, msg_handler->description);
+		handled = true;
+		break;
+	case RTE_VHOST_MSG_RESULT_REPLY:
+		VHOST_LOG_CONFIG(DEBUG, "(%s) processing %s succeeded and needs reply.\n",
+			dev->ifname, msg_handler->description);
+		send_vhost_reply(dev, fd, &ctx);
+		handled = true;
+		break;
+	default:
+		break;
 	}
 
 skip_to_post_handle:
diff --git a/lib/vhost/vhost_user.h b/lib/vhost/vhost_user.h
index c946cc2ef4..ba1c5c7969 100644
--- a/lib/vhost/vhost_user.h
+++ b/lib/vhost/vhost_user.h
@@ -59,7 +59,6 @@ typedef enum VhostUserRequest {
 	VHOST_USER_SET_INFLIGHT_FD = 32,
 	VHOST_USER_SET_STATUS = 39,
 	VHOST_USER_GET_STATUS = 40,
-	VHOST_USER_MAX = 41
 } VhostUserRequest;
 
 typedef enum VhostUserSlaveRequest {
@@ -67,7 +66,6 @@ typedef enum VhostUserSlaveRequest {
 	VHOST_USER_SLAVE_IOTLB_MSG = 1,
 	VHOST_USER_SLAVE_CONFIG_CHANGE_MSG = 2,
 	VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG = 3,
-	VHOST_USER_SLAVE_MAX
 } VhostUserSlaveRequest;
 
 typedef struct VhostUserMemoryRegion {
-- 
2.23.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 2/2] vhost: validate fds attached to messages
  2022-04-25 12:54 [PATCH 1/2] vhost: remove unneeded max enums David Marchand
@ 2022-04-25 12:54 ` David Marchand
  2022-05-05 14:31   ` Maxime Coquelin
  2022-05-05 19:58   ` Maxime Coquelin
  2022-05-05 14:22 ` [PATCH 1/2] vhost: remove unneeded max enums Maxime Coquelin
  2022-05-05 19:57 ` Maxime Coquelin
  2 siblings, 2 replies; 7+ messages in thread
From: David Marchand @ 2022-04-25 12:54 UTC (permalink / raw)
  To: dev; +Cc: maxime.coquelin, chenbo.xia

Some message handlers do not expect any file descriptor attached as
ancillary data.
Provide a common way to enforce this by adding a accepts_fd boolean in
the message handler structure. When a message handler sets accepts_fd to
true, it is responsible for calling validate_msg_fds with a right
expected file descriptor count.
This will avoid leaking some file descriptor by mistake when adding
support for new vhost user message types.

Signed-off-by: David Marchand <david.marchand@redhat.com>
---
 lib/vhost/vhost_user.c | 145 ++++++++++++-----------------------------
 1 file changed, 43 insertions(+), 102 deletions(-)

diff --git a/lib/vhost/vhost_user.c b/lib/vhost/vhost_user.c
index 17cfeafa16..850848c269 100644
--- a/lib/vhost/vhost_user.c
+++ b/lib/vhost/vhost_user.c
@@ -60,6 +60,7 @@ typedef struct vhost_message_handler {
 	const char *description;
 	int (*callback)(struct virtio_net **pdev, struct vhu_msg_context *ctx,
 		int main_fd);
+	bool accepts_fd;
 } vhost_message_handler_t;
 static vhost_message_handler_t vhost_message_handlers[];
 
@@ -262,28 +263,20 @@ vhost_user_notify_queue_state(struct virtio_net *dev, uint16_t index,
  * the device hasn't been initialised.
  */
 static int
-vhost_user_set_owner(struct virtio_net **pdev,
-			struct vhu_msg_context *ctx,
+vhost_user_set_owner(struct virtio_net **pdev __rte_unused,
+			struct vhu_msg_context *ctx __rte_unused,
 			int main_fd __rte_unused)
 {
-	struct virtio_net *dev = *pdev;
-
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	return RTE_VHOST_MSG_RESULT_OK;
 }
 
 static int
 vhost_user_reset_owner(struct virtio_net **pdev,
-			struct vhu_msg_context *ctx,
+			struct vhu_msg_context *ctx __rte_unused,
 			int main_fd __rte_unused)
 {
 	struct virtio_net *dev = *pdev;
 
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	vhost_destroy_device_notify(dev);
 
 	cleanup_device(dev, 0);
@@ -302,9 +295,6 @@ vhost_user_get_features(struct virtio_net **pdev,
 	struct virtio_net *dev = *pdev;
 	uint64_t features = 0;
 
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	rte_vhost_driver_get_features(dev->ifname, &features);
 
 	ctx->msg.payload.u64 = features;
@@ -325,9 +315,6 @@ vhost_user_get_queue_num(struct virtio_net **pdev,
 	struct virtio_net *dev = *pdev;
 	uint32_t queue_num = 0;
 
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
 
 	ctx->msg.payload.u64 = (uint64_t)queue_num;
@@ -350,9 +337,6 @@ vhost_user_set_features(struct virtio_net **pdev,
 	uint64_t vhost_features = 0;
 	struct rte_vdpa_device *vdpa_dev;
 
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	rte_vhost_driver_get_features(dev->ifname, &vhost_features);
 	if (features & ~vhost_features) {
 		VHOST_LOG_CONFIG(ERR, "(%s) received invalid negotiated features.\n",
@@ -438,9 +422,6 @@ vhost_user_set_vring_num(struct virtio_net **pdev,
 	struct virtio_net *dev = *pdev;
 	struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
 
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	if (ctx->msg.payload.state.num > 32768) {
 		VHOST_LOG_CONFIG(ERR, "(%s) invalid virtqueue size %u\n",
 				dev->ifname, ctx->msg.payload.state.num);
@@ -882,9 +863,6 @@ vhost_user_set_vring_addr(struct virtio_net **pdev,
 	struct vhost_vring_addr *addr = &ctx->msg.payload.addr;
 	bool access_ok;
 
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	if (dev->mem == NULL)
 		return RTE_VHOST_MSG_RESULT_ERR;
 
@@ -926,9 +904,6 @@ vhost_user_set_vring_base(struct virtio_net **pdev,
 	struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
 	uint64_t val = ctx->msg.payload.state.num;
 
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	if (vq_is_packed(dev)) {
 		/*
 		 * Bit[0:14]: avail index
@@ -1574,9 +1549,6 @@ vhost_user_get_inflight_fd(struct virtio_net **pdev,
 	int numa_node = SOCKET_ID_ANY;
 	void *addr;
 
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	if (ctx->msg.size != sizeof(ctx->msg.payload.inflight)) {
 		VHOST_LOG_CONFIG(ERR, "(%s) invalid get_inflight_fd message size is %d\n",
 			dev->ifname, ctx->msg.size);
@@ -2097,9 +2069,6 @@ vhost_user_get_vring_base(struct virtio_net **pdev,
 	struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
 	uint64_t val;
 
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	/* We have to stop the queue (virtio) if it is running. */
 	vhost_destroy_device_notify(dev);
 
@@ -2176,9 +2145,6 @@ vhost_user_set_vring_enable(struct virtio_net **pdev,
 	bool enable = !!ctx->msg.payload.state.num;
 	int index = (int)ctx->msg.payload.state.index;
 
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	VHOST_LOG_CONFIG(INFO, "(%s) set queue enable: %d to qp idx: %d\n",
 			dev->ifname, enable, index);
 
@@ -2204,9 +2170,6 @@ vhost_user_get_protocol_features(struct virtio_net **pdev,
 	struct virtio_net *dev = *pdev;
 	uint64_t features, protocol_features;
 
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	rte_vhost_driver_get_features(dev->ifname, &features);
 	rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features);
 
@@ -2226,9 +2189,6 @@ vhost_user_set_protocol_features(struct virtio_net **pdev,
 	uint64_t protocol_features = ctx->msg.payload.u64;
 	uint64_t slave_protocol_features = 0;
 
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	rte_vhost_driver_get_protocol_features(dev->ifname,
 			&slave_protocol_features);
 	if (protocol_features & ~slave_protocol_features) {
@@ -2368,9 +2328,6 @@ vhost_user_send_rarp(struct virtio_net **pdev,
 	uint8_t *mac = (uint8_t *)&ctx->msg.payload.u64;
 	struct rte_vdpa_device *vdpa_dev;
 
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	VHOST_LOG_CONFIG(DEBUG, "(%s) MAC: " RTE_ETHER_ADDR_PRT_FMT "\n",
 		dev->ifname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
 	memcpy(dev->mac.addr_bytes, mac, 6);
@@ -2397,9 +2354,6 @@ vhost_user_net_set_mtu(struct virtio_net **pdev,
 {
 	struct virtio_net *dev = *pdev;
 
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	if (ctx->msg.payload.u64 < VIRTIO_MIN_MTU ||
 			ctx->msg.payload.u64 > VIRTIO_MAX_MTU) {
 		VHOST_LOG_CONFIG(ERR, "(%s) invalid MTU size (%"PRIu64")\n",
@@ -2523,9 +2477,6 @@ vhost_user_iotlb_msg(struct virtio_net **pdev,
 	uint16_t i;
 	uint64_t vva, len;
 
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	switch (imsg->type) {
 	case VHOST_IOTLB_UPDATE:
 		len = imsg->size;
@@ -2584,9 +2535,6 @@ vhost_user_set_postcopy_advise(struct virtio_net **pdev,
 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
 	struct uffdio_api api_struct;
 
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
 
 	if (dev->postcopy_ufd == -1) {
@@ -2622,9 +2570,6 @@ vhost_user_set_postcopy_listen(struct virtio_net **pdev,
 {
 	struct virtio_net *dev = *pdev;
 
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	if (dev->mem && dev->mem->nregions) {
 		VHOST_LOG_CONFIG(ERR, "(%s) regions already registered at postcopy-listen\n",
 				dev->ifname);
@@ -2642,9 +2587,6 @@ vhost_user_postcopy_end(struct virtio_net **pdev,
 {
 	struct virtio_net *dev = *pdev;
 
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	dev->postcopy_listening = 0;
 	if (dev->postcopy_ufd >= 0) {
 		close(dev->postcopy_ufd);
@@ -2665,9 +2607,6 @@ vhost_user_get_status(struct virtio_net **pdev,
 {
 	struct virtio_net *dev = *pdev;
 
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	ctx->msg.payload.u64 = dev->status;
 	ctx->msg.size = sizeof(ctx->msg.payload.u64);
 	ctx->fd_num = 0;
@@ -2682,9 +2621,6 @@ vhost_user_set_status(struct virtio_net **pdev,
 {
 	struct virtio_net *dev = *pdev;
 
-	if (validate_msg_fds(dev, ctx, 0) != 0)
-		return RTE_VHOST_MSG_RESULT_ERR;
-
 	/* As per Virtio specification, the device status is 8bits long */
 	if (ctx->msg.payload.u64 > UINT8_MAX) {
 		VHOST_LOG_CONFIG(ERR, "(%s) invalid VHOST_USER_SET_STATUS payload 0x%" PRIx64 "\n",
@@ -2727,39 +2663,39 @@ vhost_user_set_status(struct virtio_net **pdev,
 }
 
 #define VHOST_MESSAGE_HANDLERS \
-VHOST_MESSAGE_HANDLER(VHOST_USER_NONE, NULL) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_GET_FEATURES, vhost_user_get_features) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_SET_FEATURES, vhost_user_set_features) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_SET_OWNER, vhost_user_set_owner) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_RESET_OWNER, vhost_user_reset_owner) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_SET_MEM_TABLE, vhost_user_set_mem_table) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_BASE, vhost_user_set_log_base) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_FD, vhost_user_set_log_fd) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_NUM, vhost_user_set_vring_num) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ADDR, vhost_user_set_vring_addr) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_BASE, vhost_user_set_vring_base) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_GET_VRING_BASE, vhost_user_get_vring_base) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_KICK, vhost_user_set_vring_kick) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_CALL, vhost_user_set_vring_call) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ERR, vhost_user_set_vring_err) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_GET_PROTOCOL_FEATURES, vhost_user_get_protocol_features) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_SET_PROTOCOL_FEATURES, vhost_user_set_protocol_features) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_GET_QUEUE_NUM, vhost_user_get_queue_num) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ENABLE, vhost_user_set_vring_enable) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_SEND_RARP, vhost_user_send_rarp) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_NET_SET_MTU, vhost_user_net_set_mtu) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_SET_SLAVE_REQ_FD, vhost_user_set_req_fd) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_IOTLB_MSG, vhost_user_iotlb_msg) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_ADVISE, vhost_user_set_postcopy_advise) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_LISTEN, vhost_user_set_postcopy_listen) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_END, vhost_user_postcopy_end) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_GET_INFLIGHT_FD, vhost_user_get_inflight_fd) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_SET_INFLIGHT_FD, vhost_user_set_inflight_fd) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_SET_STATUS, vhost_user_set_status) \
-VHOST_MESSAGE_HANDLER(VHOST_USER_GET_STATUS, vhost_user_get_status)
-
-#define VHOST_MESSAGE_HANDLER(id, handler) \
-	[id] = { #id, handler },
+VHOST_MESSAGE_HANDLER(VHOST_USER_NONE, NULL, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_GET_FEATURES, vhost_user_get_features, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_FEATURES, vhost_user_set_features, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_OWNER, vhost_user_set_owner, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_RESET_OWNER, vhost_user_reset_owner, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_MEM_TABLE, vhost_user_set_mem_table, true) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_BASE, vhost_user_set_log_base, true) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_FD, vhost_user_set_log_fd, true) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_NUM, vhost_user_set_vring_num, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ADDR, vhost_user_set_vring_addr, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_BASE, vhost_user_set_vring_base, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_GET_VRING_BASE, vhost_user_get_vring_base, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_KICK, vhost_user_set_vring_kick, true) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_CALL, vhost_user_set_vring_call, true) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ERR, vhost_user_set_vring_err, true) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_GET_PROTOCOL_FEATURES, vhost_user_get_protocol_features, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_PROTOCOL_FEATURES, vhost_user_set_protocol_features, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_GET_QUEUE_NUM, vhost_user_get_queue_num, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ENABLE, vhost_user_set_vring_enable, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SEND_RARP, vhost_user_send_rarp, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_NET_SET_MTU, vhost_user_net_set_mtu, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_SLAVE_REQ_FD, vhost_user_set_req_fd, true) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_IOTLB_MSG, vhost_user_iotlb_msg, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_ADVISE, vhost_user_set_postcopy_advise, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_LISTEN, vhost_user_set_postcopy_listen, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_END, vhost_user_postcopy_end, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_GET_INFLIGHT_FD, vhost_user_get_inflight_fd, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_INFLIGHT_FD, vhost_user_set_inflight_fd, true) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_SET_STATUS, vhost_user_set_status, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_GET_STATUS, vhost_user_get_status, false)
+
+#define VHOST_MESSAGE_HANDLER(id, handler, accepts_fd) \
+	[id] = { #id, handler, accepts_fd },
 static vhost_message_handler_t vhost_message_handlers[] = {
 	VHOST_MESSAGE_HANDLERS
 };
@@ -3030,7 +2966,12 @@ vhost_user_msg_handler(int vid, int fd)
 	if (msg_handler == NULL || msg_handler->callback == NULL)
 		goto skip_to_post_handle;
 
-	ret = msg_handler->callback(&dev, &ctx, fd);
+	if (!msg_handler->accepts_fd && validate_msg_fds(dev, &ctx, 0) != 0) {
+		ret = RTE_VHOST_MSG_RESULT_ERR;
+	} else {
+		ret = msg_handler->callback(&dev, &ctx, fd);
+	}
+
 	switch (ret) {
 	case RTE_VHOST_MSG_RESULT_ERR:
 		VHOST_LOG_CONFIG(ERR, "(%s) processing %s failed.\n",
-- 
2.23.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] vhost: remove unneeded max enums
  2022-04-25 12:54 [PATCH 1/2] vhost: remove unneeded max enums David Marchand
  2022-04-25 12:54 ` [PATCH 2/2] vhost: validate fds attached to messages David Marchand
@ 2022-05-05 14:22 ` Maxime Coquelin
  2022-05-05 19:57 ` Maxime Coquelin
  2 siblings, 0 replies; 7+ messages in thread
From: Maxime Coquelin @ 2022-05-05 14:22 UTC (permalink / raw)
  To: David Marchand, dev; +Cc: chenbo.xia

Hi David,

On 4/25/22 14:54, David Marchand wrote:
> Move message handler description and callbacks into a single array and
> remove unneeded VHOST_USER_MAX and VHOST_SLAVE_MAX enums.
> 
> Signed-off-by: David Marchand <david.marchand@redhat.com>
> ---
>   drivers/net/virtio/virtio_user/vhost_user.c |   1 -
>   examples/vhost_blk/blk_spec.h               |   1 -
>   lib/vhost/vhost_user.c                      | 175 +++++++++-----------
>   lib/vhost/vhost_user.h                      |   2 -
>   4 files changed, 76 insertions(+), 103 deletions(-)
> 

The patch does a bit more than what the commit title mentions.
What about: "vhost: refactor messages handlers declaration"?

Other than that, the patch content looks good to me:

Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>

Thanks,
Maxime


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] vhost: validate fds attached to messages
  2022-04-25 12:54 ` [PATCH 2/2] vhost: validate fds attached to messages David Marchand
@ 2022-05-05 14:31   ` Maxime Coquelin
  2022-05-05 19:58   ` Maxime Coquelin
  1 sibling, 0 replies; 7+ messages in thread
From: Maxime Coquelin @ 2022-05-05 14:31 UTC (permalink / raw)
  To: David Marchand, dev; +Cc: chenbo.xia



On 4/25/22 14:54, David Marchand wrote:
> Some message handlers do not expect any file descriptor attached as
> ancillary data.
> Provide a common way to enforce this by adding a accepts_fd boolean in
> the message handler structure. When a message handler sets accepts_fd to
> true, it is responsible for calling validate_msg_fds with a right
> expected file descriptor count.
> This will avoid leaking some file descriptor by mistake when adding
> support for new vhost user message types.
> 
> Signed-off-by: David Marchand <david.marchand@redhat.com>
> ---
>   lib/vhost/vhost_user.c | 145 ++++++++++++-----------------------------
>   1 file changed, 43 insertions(+), 102 deletions(-)
> 

Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>

Thanks,
Maxime


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] vhost: remove unneeded max enums
  2022-04-25 12:54 [PATCH 1/2] vhost: remove unneeded max enums David Marchand
  2022-04-25 12:54 ` [PATCH 2/2] vhost: validate fds attached to messages David Marchand
  2022-05-05 14:22 ` [PATCH 1/2] vhost: remove unneeded max enums Maxime Coquelin
@ 2022-05-05 19:57 ` Maxime Coquelin
  2022-05-06  7:12   ` David Marchand
  2 siblings, 1 reply; 7+ messages in thread
From: Maxime Coquelin @ 2022-05-05 19:57 UTC (permalink / raw)
  To: David Marchand, dev; +Cc: chenbo.xia



On 4/25/22 14:54, David Marchand wrote:
> Move message handler description and callbacks into a single array and
> remove unneeded VHOST_USER_MAX and VHOST_SLAVE_MAX enums.
> 
> Signed-off-by: David Marchand <david.marchand@redhat.com>
> ---
>   drivers/net/virtio/virtio_user/vhost_user.c |   1 -
>   examples/vhost_blk/blk_spec.h               |   1 -
>   lib/vhost/vhost_user.c                      | 175 +++++++++-----------
>   lib/vhost/vhost_user.h                      |   2 -
>   4 files changed, 76 insertions(+), 103 deletions(-)
> 


Applied to dpdk-next-virtio/main without changing the commit title.
Feel free to change it while pulling if you feel this is relevant.

Thanks,
Maxime


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] vhost: validate fds attached to messages
  2022-04-25 12:54 ` [PATCH 2/2] vhost: validate fds attached to messages David Marchand
  2022-05-05 14:31   ` Maxime Coquelin
@ 2022-05-05 19:58   ` Maxime Coquelin
  1 sibling, 0 replies; 7+ messages in thread
From: Maxime Coquelin @ 2022-05-05 19:58 UTC (permalink / raw)
  To: David Marchand, dev; +Cc: chenbo.xia



On 4/25/22 14:54, David Marchand wrote:
> Some message handlers do not expect any file descriptor attached as
> ancillary data.
> Provide a common way to enforce this by adding a accepts_fd boolean in
> the message handler structure. When a message handler sets accepts_fd to
> true, it is responsible for calling validate_msg_fds with a right
> expected file descriptor count.
> This will avoid leaking some file descriptor by mistake when adding
> support for new vhost user message types.
> 
> Signed-off-by: David Marchand <david.marchand@redhat.com>
> ---
>   lib/vhost/vhost_user.c | 145 ++++++++++++-----------------------------
>   1 file changed, 43 insertions(+), 102 deletions(-)
> 

Applied to dpdk-next-virtio/main.

Thanks,
Maxime


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] vhost: remove unneeded max enums
  2022-05-05 19:57 ` Maxime Coquelin
@ 2022-05-06  7:12   ` David Marchand
  0 siblings, 0 replies; 7+ messages in thread
From: David Marchand @ 2022-05-06  7:12 UTC (permalink / raw)
  To: Maxime Coquelin; +Cc: dev, Xia, Chenbo, Thomas Monjalon

On Thu, May 5, 2022 at 9:58 PM Maxime Coquelin
<maxime.coquelin@redhat.com> wrote:
> On 4/25/22 14:54, David Marchand wrote:
> > Move message handler description and callbacks into a single array and
> > remove unneeded VHOST_USER_MAX and VHOST_SLAVE_MAX enums.
> >
> > Signed-off-by: David Marchand <david.marchand@redhat.com>
> > ---
> >   drivers/net/virtio/virtio_user/vhost_user.c |   1 -
> >   examples/vhost_blk/blk_spec.h               |   1 -
> >   lib/vhost/vhost_user.c                      | 175 +++++++++-----------
> >   lib/vhost/vhost_user.h                      |   2 -
> >   4 files changed, 76 insertions(+), 103 deletions(-)
> >
>
>
> Applied to dpdk-next-virtio/main without changing the commit title.
> Feel free to change it while pulling if you feel this is relevant.

This bad title probably comes from a time when the patch was indeed
only touching max enums.
Your suggestion is ok.

This will be fixed when pulling, thanks.

-- 
David Marchand


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-05-06  7:12 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-25 12:54 [PATCH 1/2] vhost: remove unneeded max enums David Marchand
2022-04-25 12:54 ` [PATCH 2/2] vhost: validate fds attached to messages David Marchand
2022-05-05 14:31   ` Maxime Coquelin
2022-05-05 19:58   ` Maxime Coquelin
2022-05-05 14:22 ` [PATCH 1/2] vhost: remove unneeded max enums Maxime Coquelin
2022-05-05 19:57 ` Maxime Coquelin
2022-05-06  7:12   ` David Marchand

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).