public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top, Stefano Brivio <sbrivio@redhat.com>
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 4/5] migrate: Make more handling common rather than vhost-user specific
Date: Thu, 30 Jan 2025 19:33:29 +1100	[thread overview]
Message-ID: <20250130083330.917030-5-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20250130083330.917030-1-david@gibson.dropbear.id.au>

A lot of the migration logic in vhost_user.c and vu_common.c isn't really
specific to vhost-user, but matches the overall structure of migration.
This applies to vu_migrate() and to the parts of of
vu_set_device_state_fd_exec() which aren't related to parsing the specific
vhost-user control request.

Move this logic to migrate.c, with matching renames.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 epoll_type.h |  4 +--
 migrate.c    | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 migrate.h    |  6 ++--
 passt.c      |  6 ++--
 passt.h      |  6 ++++
 vhost_user.c | 59 ++++-----------------------------
 virtio.h     |  4 ---
 vu_common.c  | 27 ---------------
 vu_common.h  |  2 +-
 9 files changed, 112 insertions(+), 94 deletions(-)

diff --git a/epoll_type.h b/epoll_type.h
index 706238aa..b981d30e 100644
--- a/epoll_type.h
+++ b/epoll_type.h
@@ -40,8 +40,8 @@ enum epoll_type {
 	EPOLL_TYPE_VHOST_CMD,
 	/* vhost-user kick event socket */
 	EPOLL_TYPE_VHOST_KICK,
-	/* vhost-user migration socket */
-	EPOLL_TYPE_VHOST_MIGRATION,
+	/* migration device state channel */
+	EPOLL_TYPE_DEVICE_STATE,
 	/* TCP_REPAIR helper listening socket */
 	EPOLL_TYPE_REPAIR_LISTEN,
 	/* TCP_REPAIR helper socket */
diff --git a/migrate.c b/migrate.c
index 148cd660..04df6822 100644
--- a/migrate.c
+++ b/migrate.c
@@ -21,6 +21,7 @@
 #include "inany.h"
 #include "flow.h"
 #include "flow_table.h"
+#include "repair.h"
 
 #include "migrate.h"
 
@@ -154,7 +155,7 @@ static void migrate_source_post(struct ctx *c, struct migrate_meta *m)
  *
  * Return: 0 on success, positive error code on failure
  */
-int migrate_source(struct ctx *c, int fd)
+static int migrate_source(struct ctx *c, int fd)
 {
 	struct migrate_meta m;
 	int rc;
@@ -299,7 +300,7 @@ static void migrate_target_post(struct ctx *c, struct migrate_meta *m)
  *
  * Return: 0 on success, positive error code on failure
  */
-int migrate_target(struct ctx *c, int fd)
+static int migrate_target(struct ctx *c, int fd)
 {
 	struct migrate_meta m;
 	int rc;
@@ -325,3 +326,90 @@ int migrate_target(struct ctx *c, int fd)
 
 	return rc;
 }
+
+/**
+ * set_migration_watch() - Add the migration file descriptor to epoll
+ * @c:		Execution context
+ * @fd:		File descriptor to add
+ * @target:	Are we the target of the migration?
+ */
+static void set_migration_watch(const struct ctx *c, int fd, bool target)
+{
+	union epoll_ref ref = {
+		.type = EPOLL_TYPE_DEVICE_STATE,
+		.fd = fd,
+	 };
+	struct epoll_event ev = { 0 };
+
+	ev.data.u64 = ref.u64;
+	ev.events = target ? EPOLLIN : EPOLLOUT;
+
+	epoll_ctl(c->epollfd, EPOLL_CTL_ADD, ref.fd, &ev);
+}
+
+/**
+ * migrate_init() - Set up things necessary for migration
+ * @c:		Execution context
+ */
+void migrate_init(struct ctx *c)
+{
+	c->device_state_fd = -1;
+	c->device_state_result = -1;
+	repair_sock_init(c);
+}
+
+/**
+ * migrate_close() - Close migration channel
+ * @c:		Execution context
+ */
+void migrate_close(struct ctx *c)
+{
+	if (c->device_state_fd != -1) {
+		debug("Closing migration channel, fd: %d", c->device_state_fd);
+		epoll_del(c, c->device_state_fd);
+		close(c->device_state_fd);
+		c->device_state_fd = -1;
+		c->device_state_result = -1;
+	}
+}
+
+/**
+ * migrate_request() - Request a migration of device state
+ * @c:		Execution context
+ * @fd:		fd to transfer state
+ * @target:	Are we the target of the migration?
+ */
+void migrate_request(struct ctx *c, int fd, bool target)
+{
+	debug("Migration requested, fd: %d", c->device_state_fd);
+
+	if (c->device_state_fd != -1)
+		migrate_close(c);
+
+	c->device_state_fd = fd;
+	set_migration_watch(c, c->device_state_fd, target);
+
+}
+
+/**
+ * migrate_handler() - Send/receive passt internal state to/from QEMU
+ * @c:		Execution context
+ * @events:	epoll events
+ */
+void migrate_handler(struct ctx *c, uint32_t events)
+{
+	int rc = EIO;
+
+	debug("migrate_handler fd %d events %x", c->device_state_fd, events);
+
+	if (events & EPOLLOUT)
+		rc = migrate_source(c, c->device_state_fd);
+	else if (events & EPOLLIN)
+		rc = migrate_target(c, c->device_state_fd);
+
+	/* EPOLLHUP without EPOLLIN/EPOLLOUT, or EPOLLERR? Migration failed */
+
+	migrate_close(c);
+
+	c->device_state_result = rc;
+}
diff --git a/migrate.h b/migrate.h
index edf6303e..b97e55e7 100644
--- a/migrate.h
+++ b/migrate.h
@@ -76,7 +76,9 @@ struct migrate_target_handlers {
 	struct migrate_handler *post;
 };
 
-int migrate_source(struct ctx *c, int fd);
-int migrate_target(struct ctx *c, int fd);
+void migrate_init(struct ctx *c);
+void migrate_close(struct ctx *c);
+void migrate_request(struct ctx *c, int fd, bool target);
+void migrate_handler(struct ctx *c, uint32_t events);
 
 #endif /* MIGRATE_H */
diff --git a/passt.c b/passt.c
index 1fa2ddd2..3c3a3313 100644
--- a/passt.c
+++ b/passt.c
@@ -76,7 +76,7 @@ char *epoll_type_str[] = {
 	[EPOLL_TYPE_TAP_LISTEN]		= "listening qemu socket",
 	[EPOLL_TYPE_VHOST_CMD]		= "vhost-user command socket",
 	[EPOLL_TYPE_VHOST_KICK]		= "vhost-user kick socket",
-	[EPOLL_TYPE_VHOST_MIGRATION]	= "vhost-user migration socket",
+	[EPOLL_TYPE_DEVICE_STATE]	= "migration device state channel",
 	[EPOLL_TYPE_REPAIR_LISTEN]	= "TCP_REPAIR helper listening socket",
 	[EPOLL_TYPE_REPAIR]		= "TCP_REPAIR helper socket",
 };
@@ -360,8 +360,8 @@ loop:
 		case EPOLL_TYPE_VHOST_KICK:
 			vu_kick_cb(c.vdev, ref, &now);
 			break;
-		case EPOLL_TYPE_VHOST_MIGRATION:
-			vu_migrate(&c, eventmask);
+		case EPOLL_TYPE_DEVICE_STATE:
+			migrate_handler(&c, eventmask);
 			break;
 		case EPOLL_TYPE_REPAIR_LISTEN:
 			repair_listen_handler(&c, eventmask);
diff --git a/passt.h b/passt.h
index 85b0a10f..5992cbeb 100644
--- a/passt.h
+++ b/passt.h
@@ -239,6 +239,8 @@ struct ip6_ctx {
  * @low_wmem:		Low probed net.core.wmem_max
  * @low_rmem:		Low probed net.core.rmem_max
  * @vdev:		vhost-user device
+ * @device_state_fd:	Device state migration channel
+ * @device_state_result: Device state migration result
  */
 struct ctx {
 	enum passt_modes mode;
@@ -307,6 +309,10 @@ struct ctx {
 	int low_rmem;
 
 	struct vu_dev *vdev;
+
+	/* Migration */
+	int device_state_fd;
+	int device_state_result;
 };
 
 void proto_update_l2_buf(const unsigned char *eth_d,
diff --git a/vhost_user.c b/vhost_user.c
index 5df29c47..2dde405a 100644
--- a/vhost_user.c
+++ b/vhost_user.c
@@ -44,7 +44,6 @@
 #include "tap.h"
 #include "vhost_user.h"
 #include "pcap.h"
-#include "repair.h"
 
 /* vhost-user version we are compatible with */
 #define VHOST_USER_VERSION 1
@@ -998,36 +997,6 @@ static bool vu_send_rarp_exec(struct vu_dev *vdev,
 	return false;
 }
 
-/**
- * vu_set_migration_watch() - Add the migration file descriptor to epoll
- * @vdev:	vhost-user device
- * @fd:		File descriptor to add
- * @direction:	Direction of the migration (save or load backend state)
- */
-static void vu_set_migration_watch(const struct vu_dev *vdev, int fd,
-				   uint32_t direction)
-{
-	union epoll_ref ref = {
-		.type = EPOLL_TYPE_VHOST_MIGRATION,
-		.fd = fd,
-	 };
-	struct epoll_event ev = { 0 };
-
-	ev.data.u64 = ref.u64;
-	switch (direction) {
-	case VHOST_USER_TRANSFER_STATE_DIRECTION_SAVE:
-		ev.events = EPOLLOUT;
-		break;
-	case VHOST_USER_TRANSFER_STATE_DIRECTION_LOAD:
-		ev.events = EPOLLIN;
-		break;
-	default:
-		ASSERT(0);
-	}
-
-	epoll_ctl(vdev->context->epollfd, EPOLL_CTL_ADD, ref.fd, &ev);
-}
-
 /**
  * vu_set_device_state_fd_exec() - Set the device state migration channel
  * @vdev:	vhost-user device
@@ -1052,16 +1021,8 @@ static bool vu_set_device_state_fd_exec(struct vu_dev *vdev,
 	    direction != VHOST_USER_TRANSFER_STATE_DIRECTION_LOAD)
 		die("Invalide device_state_fd direction: %d", direction);
 
-	if (vdev->device_state_fd != -1) {
-		epoll_del(vdev->context, vdev->device_state_fd);
-		close(vdev->device_state_fd);
-	}
-
-	vdev->device_state_fd = msg->fds[0];
-	vdev->device_state_result = -1;
-	vu_set_migration_watch(vdev, vdev->device_state_fd, direction);
-
-	debug("Got device_state_fd: %d", vdev->device_state_fd);
+	migrate_request(vdev->context, msg->fds[0],
+			direction == VHOST_USER_TRANSFER_STATE_DIRECTION_LOAD);
 
 	/* We don't provide a new fd for the data transfer */
 	vmsg_set_reply_u64(msg, VHOST_USER_VRING_NOFD_MASK);
@@ -1079,9 +1040,7 @@ static bool vu_set_device_state_fd_exec(struct vu_dev *vdev,
 static bool vu_check_device_state_exec(struct vu_dev *vdev,
 				       struct vhost_user_msg *msg)
 {
-	(void)vdev;
-
-	vmsg_set_reply_u64(msg, vdev->device_state_result);
+	vmsg_set_reply_u64(msg, vdev->context->device_state_result);
 
 	return true;
 }
@@ -1108,9 +1067,7 @@ void vu_init(struct ctx *c)
 	c->vdev->log_table = NULL;
 	c->vdev->log_call_fd = -1;
 
-	c->vdev->device_state_fd = -1;
-	c->vdev->device_state_result = -1;
-	repair_sock_init(c);
+	migrate_init(c);
 }
 
 
@@ -1160,12 +1117,8 @@ void vu_cleanup(struct vu_dev *vdev)
 
 	vu_close_log(vdev);
 
-	if (vdev->device_state_fd != -1) {
-		epoll_del(vdev->context, vdev->device_state_fd);
-		close(vdev->device_state_fd);
-		vdev->device_state_fd = -1;
-		vdev->device_state_result = -1;
-	}
+	/* If we lose the VU dev, we also lose our migration channel */
+	migrate_close(vdev->context);
 }
 
 /**
diff --git a/virtio.h b/virtio.h
index 7bef2d27..0a59441b 100644
--- a/virtio.h
+++ b/virtio.h
@@ -106,8 +106,6 @@ struct vu_dev_region {
  * @log_call_fd:		Eventfd to report logging update
  * @log_size:			Size of the logging memory region
  * @log_table:			Base of the logging memory region
- * @device_state_fd:		Device state migration channel
- * @device_state_result:	Device state migration result
  */
 struct vu_dev {
 	struct ctx *context;
@@ -119,8 +117,6 @@ struct vu_dev {
 	int log_call_fd;
 	uint64_t log_size;
 	uint8_t *log_table;
-	int device_state_fd;
-	int device_state_result;
 };
 
 /**
diff --git a/vu_common.c b/vu_common.c
index d7e39e63..78d1c1ba 100644
--- a/vu_common.c
+++ b/vu_common.c
@@ -305,30 +305,3 @@ err:
 
 	return -1;
 }
-
-/**
- * vu_migrate() - Send/receive passt internal state to/from QEMU
- * @c:		Execution context
- * @events:	epoll events
- */
-void vu_migrate(struct ctx *c, uint32_t events)
-{
-	struct vu_dev *vdev = c->vdev;
-	int rc = EIO;
-
-	debug("vu_migrate fd %d events %x", vdev->device_state_fd, events);
-
-	if (events & EPOLLOUT)
-		rc = migrate_source(c, vdev->device_state_fd);
-	else if (events & EPOLLIN)
-		rc = migrate_target(c, vdev->device_state_fd);
-
-	/* EPOLLHUP without EPOLLIN/EPOLLOUT, or EPOLLERR? Migration failed */
-
-	vdev->device_state_result = rc;
-
-	epoll_del(c, vdev->device_state_fd);
-	debug("Closing migration channel");
-	close(vdev->device_state_fd);
-	vdev->device_state_fd = -1;
-}
diff --git a/vu_common.h b/vu_common.h
index 69c40063..f538f237 100644
--- a/vu_common.h
+++ b/vu_common.h
@@ -57,5 +57,5 @@ void vu_flush(const struct vu_dev *vdev, struct vu_virtq *vq,
 void vu_kick_cb(struct vu_dev *vdev, union epoll_ref ref,
 		const struct timespec *now);
 int vu_send_single(const struct ctx *c, const void *buf, size_t size);
-void vu_migrate(struct ctx *c, uint32_t events);
+
 #endif /* VU_COMMON_H */
-- 
@@ -57,5 +57,5 @@ void vu_flush(const struct vu_dev *vdev, struct vu_virtq *vq,
 void vu_kick_cb(struct vu_dev *vdev, union epoll_ref ref,
 		const struct timespec *now);
 int vu_send_single(const struct ctx *c, const void *buf, size_t size);
-void vu_migrate(struct ctx *c, uint32_t events);
+
 #endif /* VU_COMMON_H */
-- 
2.48.1


  parent reply	other threads:[~2025-01-30  8:33 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-30  8:33 [PATCH 0/5] RFC: Assorted migration improvements David Gibson
2025-01-30  8:33 ` [PATCH 1/5] migrate: Add passt-repair to .gitignore David Gibson
2025-01-30  8:33 ` [PATCH 2/5] migrate: vu_migrate_{source,target}() aren't actually vu speciic David Gibson
2025-01-30  8:33 ` [PATCH 3/5] migrate: Move repair_sock_init() to vu_init() David Gibson
2025-01-30  8:33 ` David Gibson [this message]
2025-01-30  8:33 ` [PATCH 5/5] migrate: Don't handle the migration channel through epoll David Gibson
2025-01-31  5:37   ` Stefano Brivio
2025-01-30  8:46 ` [PATCH 0/5] RFC: Assorted migration improvements Stefano Brivio
2025-01-30  8:55   ` David Gibson
2025-01-31  5:46     ` Stefano Brivio

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250130083330.917030-5-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

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 IMAP folder(s).