diff mbox series

[21.02,2/3] vhost: refactor postcopy registration

Message ID 20201116113620.587073-3-maxime.coquelin@redhat.com (mailing list archive)
State Superseded, archived
Delegated to: Maxime Coquelin
Headers show
Series vhost: vhost_user_set_mem_table refactoring | expand

Checks

Context Check Description
ci/checkpatch warning coding style issues

Commit Message

Maxime Coquelin Nov. 16, 2020, 11:36 a.m. UTC
This patch moves the registration of postcopy to a
dedicated function, with the goal of simplifying
VHOST_USER_SET_MEM_TABLE request handling function.

Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
---
 lib/librte_vhost/vhost_user.c | 98 +++++++++++++++++++++--------------
 1 file changed, 58 insertions(+), 40 deletions(-)

Comments

Xia, Chenbo Dec. 9, 2020, 2:16 p.m. UTC | #1
> -----Original Message-----
> From: Maxime Coquelin <maxime.coquelin@redhat.com>
> Sent: Monday, November 16, 2020 7:36 PM
> To: dev@dpdk.org; Xia, Chenbo <chenbo.xia@intel.com>; Ding, Xuan
> <xuan.ding@intel.com>
> Cc: Maxime Coquelin <maxime.coquelin@redhat.com>
> Subject: [PATCH 21.02 2/3] vhost: refactor postcopy registration
> 
> This patch moves the registration of postcopy to a
> dedicated function, with the goal of simplifying
> VHOST_USER_SET_MEM_TABLE request handling function.
> 
> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
> ---
>  lib/librte_vhost/vhost_user.c | 98 +++++++++++++++++++++--------------
>  1 file changed, 58 insertions(+), 40 deletions(-)
> 
> diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
> index b8a9e41a2d..2ee22ef76d 100644
> --- a/lib/librte_vhost/vhost_user.c
> +++ b/lib/librte_vhost/vhost_user.c
> @@ -1041,6 +1041,62 @@ vhost_user_postcopy_region_register(struct
> virtio_net *dev,
>  }
>  #endif
> 
> +static int
> +vhost_user_postcopy_register(struct virtio_net *dev, int main_fd,
> +		struct VhostUserMsg *msg)
> +{
> +	struct VhostUserMemory *memory;
> +	struct rte_vhost_mem_region *reg;
> +	VhostUserMsg ack_msg;
> +	uint32_t i;
> +
> +	if (!dev->postcopy_listening)
> +		return 0;
> +
> +	/*
> +	 * We haven't a better way right now than sharing
> +	 * DPDK's virtual address with Qemu, so that Qemu can
> +	 * retrieve the region offset when handling userfaults.
> +	 */
> +	memory = &msg->payload.memory;
> +	for (i = 0; i < memory->nregions; i++) {
> +		reg = &dev->mem->regions[i];
> +		memory->regions[i].userspace_addr = reg->host_user_addr;
> +	}
> +
> +	/* Send the addresses back to qemu */
> +	msg->fd_num = 0;
> +	send_vhost_reply(main_fd, msg);
> +
> +	/* Wait for qemu to acknolwedge it's got the addresses
> +	 * we've got to wait before we're allowed to generate faults.
> +	 */
> +	if (read_vhost_message(main_fd, &ack_msg) <= 0) {
> +		VHOST_LOG_CONFIG(ERR,
> +				"Failed to read qemu ack on postcopy set-mem-
> table\n");
> +		return -1;
> +	}
> +
> +	if (validate_msg_fds(&ack_msg, 0) != 0)
> +		return -1;
> +
> +	if (ack_msg.request.master != VHOST_USER_SET_MEM_TABLE) {
> +		VHOST_LOG_CONFIG(ERR,
> +				"Bad qemu ack on postcopy set-mem-table (%d)\n",
> +				ack_msg.request.master);
> +		return -1;
> +	}
> +
> +	/* Now userfault register and we can use the memory */
> +	for (i = 0; i < memory->nregions; i++) {
> +		reg = &dev->mem->regions[i];
> +		if (vhost_user_postcopy_region_register(dev, reg) < 0)
> +			return -1;
> +	}
> +
> +	return 0;
> +}
> +
>  static int
>  vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg
> *msg,
>  			int main_fd)
> @@ -1215,48 +1271,10 @@ vhost_user_set_mem_table(struct virtio_net **pdev,
> struct VhostUserMsg *msg,
>  			mmap_size,
>  			alignment,
>  			mmap_offset);
> -
> -		if (dev->postcopy_listening) {
> -			/*
> -			 * We haven't a better way right now than sharing
> -			 * DPDK's virtual address with Qemu, so that Qemu can
> -			 * retrieve the region offset when handling userfaults.
> -			 */
> -			memory->regions[i].userspace_addr =
> -				reg->host_user_addr;
> -		}
>  	}
> -	if (dev->postcopy_listening) {
> -		/* Send the addresses back to qemu */
> -		msg->fd_num = 0;
> -		send_vhost_reply(main_fd, msg);
> -
> -		/* Wait for qemu to acknolwedge it's got the addresses
> -		 * we've got to wait before we're allowed to generate faults.
> -		 */
> -		VhostUserMsg ack_msg;
> -		if (read_vhost_message(main_fd, &ack_msg) <= 0) {
> -			VHOST_LOG_CONFIG(ERR,
> -				"Failed to read qemu ack on postcopy set-mem-
> table\n");
> -			goto free_mem_table;
> -		}
> -
> -		if (validate_msg_fds(&ack_msg, 0) != 0)
> -			goto free_mem_table;
> -
> -		if (ack_msg.request.master != VHOST_USER_SET_MEM_TABLE) {
> -			VHOST_LOG_CONFIG(ERR,
> -				"Bad qemu ack on postcopy set-mem-table (%d)\n",
> -				ack_msg.request.master);
> -			goto free_mem_table;
> -		}
> 
> -		/* Now userfault register and we can use the memory */
> -		for (i = 0; i < memory->nregions; i++)
> -			if (vhost_user_postcopy_region_register(dev,
> -						&dev->mem->regions[i]) < 0)
> -				goto free_mem_table;
> -	}
> +	if (vhost_user_postcopy_register(dev, main_fd, msg) < 0)
> +		goto free_mem_table;
> 
>  	for (i = 0; i < dev->nr_vring; i++) {
>  		struct vhost_virtqueue *vq = dev->virtqueue[i];
> --
> 2.26.2

Reviewed-by: Chenbo Xia <chenbo.xia@intel.com>
diff mbox series

Patch

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index b8a9e41a2d..2ee22ef76d 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -1041,6 +1041,62 @@  vhost_user_postcopy_region_register(struct virtio_net *dev,
 }
 #endif
 
+static int
+vhost_user_postcopy_register(struct virtio_net *dev, int main_fd,
+		struct VhostUserMsg *msg)
+{
+	struct VhostUserMemory *memory;
+	struct rte_vhost_mem_region *reg;
+	VhostUserMsg ack_msg;
+	uint32_t i;
+
+	if (!dev->postcopy_listening)
+		return 0;
+
+	/*
+	 * We haven't a better way right now than sharing
+	 * DPDK's virtual address with Qemu, so that Qemu can
+	 * retrieve the region offset when handling userfaults.
+	 */
+	memory = &msg->payload.memory;
+	for (i = 0; i < memory->nregions; i++) {
+		reg = &dev->mem->regions[i];
+		memory->regions[i].userspace_addr = reg->host_user_addr;
+	}
+
+	/* Send the addresses back to qemu */
+	msg->fd_num = 0;
+	send_vhost_reply(main_fd, msg);
+
+	/* Wait for qemu to acknolwedge it's got the addresses
+	 * we've got to wait before we're allowed to generate faults.
+	 */
+	if (read_vhost_message(main_fd, &ack_msg) <= 0) {
+		VHOST_LOG_CONFIG(ERR,
+				"Failed to read qemu ack on postcopy set-mem-table\n");
+		return -1;
+	}
+
+	if (validate_msg_fds(&ack_msg, 0) != 0)
+		return -1;
+
+	if (ack_msg.request.master != VHOST_USER_SET_MEM_TABLE) {
+		VHOST_LOG_CONFIG(ERR,
+				"Bad qemu ack on postcopy set-mem-table (%d)\n",
+				ack_msg.request.master);
+		return -1;
+	}
+
+	/* Now userfault register and we can use the memory */
+	for (i = 0; i < memory->nregions; i++) {
+		reg = &dev->mem->regions[i];
+		if (vhost_user_postcopy_region_register(dev, reg) < 0)
+			return -1;
+	}
+
+	return 0;
+}
+
 static int
 vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
 			int main_fd)
@@ -1215,48 +1271,10 @@  vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
 			mmap_size,
 			alignment,
 			mmap_offset);
-
-		if (dev->postcopy_listening) {
-			/*
-			 * We haven't a better way right now than sharing
-			 * DPDK's virtual address with Qemu, so that Qemu can
-			 * retrieve the region offset when handling userfaults.
-			 */
-			memory->regions[i].userspace_addr =
-				reg->host_user_addr;
-		}
 	}
-	if (dev->postcopy_listening) {
-		/* Send the addresses back to qemu */
-		msg->fd_num = 0;
-		send_vhost_reply(main_fd, msg);
-
-		/* Wait for qemu to acknolwedge it's got the addresses
-		 * we've got to wait before we're allowed to generate faults.
-		 */
-		VhostUserMsg ack_msg;
-		if (read_vhost_message(main_fd, &ack_msg) <= 0) {
-			VHOST_LOG_CONFIG(ERR,
-				"Failed to read qemu ack on postcopy set-mem-table\n");
-			goto free_mem_table;
-		}
-
-		if (validate_msg_fds(&ack_msg, 0) != 0)
-			goto free_mem_table;
-
-		if (ack_msg.request.master != VHOST_USER_SET_MEM_TABLE) {
-			VHOST_LOG_CONFIG(ERR,
-				"Bad qemu ack on postcopy set-mem-table (%d)\n",
-				ack_msg.request.master);
-			goto free_mem_table;
-		}
 
-		/* Now userfault register and we can use the memory */
-		for (i = 0; i < memory->nregions; i++)
-			if (vhost_user_postcopy_region_register(dev,
-						&dev->mem->regions[i]) < 0)
-				goto free_mem_table;
-	}
+	if (vhost_user_postcopy_register(dev, main_fd, msg) < 0)
+		goto free_mem_table;
 
 	for (i = 0; i < dev->nr_vring; i++) {
 		struct vhost_virtqueue *vq = dev->virtqueue[i];