DPDK patches and discussions
 help / color / mirror / Atom feed
From: Chaoyong He <chaoyong.he@corigine.com>
To: dev@dpdk.org
Cc: oss-drivers@corigine.com, Xinying Yu <xinying.yu@corigine.com>,
	Chaoyong He <chaoyong.he@corigine.com>,
	Long Wu <long.wu@corigine.com>,
	Peng Zhang <peng.zhang@corigine.com>
Subject: [PATCH v2 09/11] vdpa/nfp: setup vring relay thread
Date: Fri, 14 Jun 2024 15:01:18 +0800	[thread overview]
Message-ID: <20240614070120.2663160-10-chaoyong.he@corigine.com> (raw)
In-Reply-To: <20240614070120.2663160-1-chaoyong.he@corigine.com>

From: Xinying Yu <xinying.yu@corigine.com>

Setup the vring relay thread to monitor the interruption from
device. And do the dirty page logging or notify device according
to event data.

Signed-off-by: Xinying Yu <xinying.yu@corigine.com>
Reviewed-by: Chaoyong He <chaoyong.he@corigine.com>
Reviewed-by: Long Wu <long.wu@corigine.com>
Reviewed-by: Peng Zhang <peng.zhang@corigine.com>
---
 drivers/vdpa/nfp/nfp_vdpa.c      | 148 +++++++++++++++++++++++++++++++
 drivers/vdpa/nfp/nfp_vdpa_core.c |   9 ++
 drivers/vdpa/nfp/nfp_vdpa_core.h |   2 +
 3 files changed, 159 insertions(+)

diff --git a/drivers/vdpa/nfp/nfp_vdpa.c b/drivers/vdpa/nfp/nfp_vdpa.c
index 65f7144671..e57765eb1a 100644
--- a/drivers/vdpa/nfp/nfp_vdpa.c
+++ b/drivers/vdpa/nfp/nfp_vdpa.c
@@ -26,6 +26,8 @@
 #define NFP_VDPA_USED_RING_LEN(size) \
 		((size) * sizeof(struct vring_used_elem) + sizeof(struct vring_used))
 
+#define EPOLL_DATA_INTR        1
+
 struct nfp_vdpa_dev {
 	struct rte_pci_device *pci_dev;
 	struct rte_vdpa_device *vdev;
@@ -776,6 +778,139 @@ update_datapath(struct nfp_vdpa_dev *device)
 	return ret;
 }
 
+static int
+nfp_vdpa_vring_epoll_ctl(uint32_t queue_num,
+		struct nfp_vdpa_dev *device)
+{
+	int ret;
+	uint32_t qid;
+	struct epoll_event ev;
+	struct rte_vhost_vring vring;
+
+	for (qid = 0; qid < queue_num; qid++) {
+		ev.events = EPOLLIN | EPOLLPRI;
+		rte_vhost_get_vhost_vring(device->vid, qid, &vring);
+		ev.data.u64 = qid << 1 | (uint64_t)vring.kickfd << 32;
+		ret = epoll_ctl(device->epoll_fd, EPOLL_CTL_ADD, vring.kickfd, &ev);
+		if (ret < 0) {
+			DRV_VDPA_LOG(ERR, "Epoll add error for queue %u", qid);
+			return ret;
+		}
+	}
+
+	/* vDPA driver interrupt */
+	for (qid = 0; qid < queue_num; qid += 2) {
+		ev.events = EPOLLIN | EPOLLPRI;
+		/* Leave a flag to mark it's for interrupt */
+		ev.data.u64 = EPOLL_DATA_INTR | qid << 1 |
+				(uint64_t)device->intr_fd[qid] << 32;
+		ret = epoll_ctl(device->epoll_fd, EPOLL_CTL_ADD,
+				device->intr_fd[qid], &ev);
+		if (ret < 0) {
+			DRV_VDPA_LOG(ERR, "Epoll add error for queue %u", qid);
+			return ret;
+		}
+
+		nfp_vdpa_update_used_ring(device, qid);
+	}
+
+	return 0;
+}
+
+static int
+nfp_vdpa_vring_epoll_wait(uint32_t queue_num,
+		struct nfp_vdpa_dev *device)
+{
+	int i;
+	int fds;
+	int kickfd;
+	uint32_t qid;
+	struct epoll_event events[NFP_VDPA_MAX_QUEUES * 2];
+
+	for (;;) {
+		fds = epoll_wait(device->epoll_fd, events, queue_num * 2, -1);
+		if (fds < 0) {
+			if (errno == EINTR)
+				continue;
+
+			DRV_VDPA_LOG(ERR, "Epoll wait fail");
+			return -EACCES;
+		}
+
+		for (i = 0; i < fds; i++) {
+			qid = events[i].data.u32 >> 1;
+			kickfd = (uint32_t)(events[i].data.u64 >> 32);
+
+			nfp_vdpa_read_kickfd(kickfd);
+			if ((events[i].data.u32 & EPOLL_DATA_INTR) != 0) {
+				nfp_vdpa_update_used_ring(device, qid);
+				nfp_vdpa_irq_unmask(&device->hw);
+			} else {
+				nfp_vdpa_notify_queue(&device->hw, qid);
+			}
+		}
+	}
+
+	return 0;
+}
+
+static uint32_t
+nfp_vdpa_vring_relay(void *arg)
+{
+	int ret;
+	int epoll_fd;
+	uint16_t queue_id;
+	uint32_t queue_num;
+	struct nfp_vdpa_dev *device = arg;
+
+	epoll_fd = epoll_create(NFP_VDPA_MAX_QUEUES * 2);
+	if (epoll_fd < 0) {
+		DRV_VDPA_LOG(ERR, "failed to create epoll instance.");
+		return 1;
+	}
+
+	device->epoll_fd = epoll_fd;
+
+	queue_num = rte_vhost_get_vring_num(device->vid);
+
+	ret = nfp_vdpa_vring_epoll_ctl(queue_num, device);
+	if (ret != 0)
+		goto notify_exit;
+
+	/* Start relay with a first kick */
+	for (queue_id = 0; queue_id < queue_num; queue_id++)
+		nfp_vdpa_notify_queue(&device->hw, queue_id);
+
+	ret = nfp_vdpa_vring_epoll_wait(queue_num, device);
+	if (ret != 0)
+		goto notify_exit;
+
+	return 0;
+
+notify_exit:
+	close(device->epoll_fd);
+	device->epoll_fd = -1;
+
+	return 1;
+}
+
+static int
+nfp_vdpa_setup_vring_relay(struct nfp_vdpa_dev *device)
+{
+	int ret;
+	char name[RTE_THREAD_INTERNAL_NAME_SIZE];
+
+	snprintf(name, sizeof(name), "nfp_vring%d", device->vid);
+	ret = rte_thread_create_internal_control(&device->tid, name,
+			nfp_vdpa_vring_relay, (void *)device);
+	if (ret != 0) {
+		DRV_VDPA_LOG(ERR, "Failed to create vring relay pthread.");
+		return -EPERM;
+	}
+
+	return 0;
+}
+
 static int
 nfp_vdpa_sw_fallback(struct nfp_vdpa_dev *device)
 {
@@ -802,10 +937,17 @@ nfp_vdpa_sw_fallback(struct nfp_vdpa_dev *device)
 	if (ret != 0)
 		goto unset_intr;
 
+	/* Setup vring relay thread */
+	ret = nfp_vdpa_setup_vring_relay(device);
+	if (ret != 0)
+		goto stop_vf;
+
 	device->hw.sw_fallback_running = true;
 
 	return 0;
 
+stop_vf:
+	nfp_vdpa_stop(device, true);
 unset_intr:
 	nfp_vdpa_disable_vfio_intr(device);
 error:
@@ -859,6 +1001,12 @@ nfp_vdpa_dev_close(int vid)
 		/* Reset VF */
 		nfp_vdpa_stop(device, true);
 
+		/* Remove interrupt setting */
+		nfp_vdpa_disable_vfio_intr(device);
+
+		/* Unset DMA map for guest memory */
+		nfp_vdpa_dma_map(device, false);
+
 		device->hw.sw_fallback_running = false;
 
 		rte_atomic_store_explicit(&device->dev_attached, 0,
diff --git a/drivers/vdpa/nfp/nfp_vdpa_core.c b/drivers/vdpa/nfp/nfp_vdpa_core.c
index d7c48e2490..3b3481a99c 100644
--- a/drivers/vdpa/nfp/nfp_vdpa_core.c
+++ b/drivers/vdpa/nfp/nfp_vdpa_core.c
@@ -270,3 +270,12 @@ nfp_vdpa_notify_queue(struct nfp_vdpa_hw *vdpa_hw,
 	nfp_qcp_notify_ptr_add(vdpa_hw->notify_addr[qid],
 			NFP_QCP_NOTIFY_WRITE_PTR, qid);
 }
+
+void nfp_vdpa_irq_unmask(struct nfp_vdpa_hw *vdpa_hw)
+{
+	struct nfp_hw *hw = &vdpa_hw->super;
+
+	/* Make sure all updates are written before un-masking */
+	rte_wmb();
+	nn_cfg_writeb(hw, NFP_NET_CFG_ICR(1), NFP_NET_CFG_ICR_UNMASKED);
+}
diff --git a/drivers/vdpa/nfp/nfp_vdpa_core.h b/drivers/vdpa/nfp/nfp_vdpa_core.h
index a339ace601..bc4db556a2 100644
--- a/drivers/vdpa/nfp/nfp_vdpa_core.h
+++ b/drivers/vdpa/nfp/nfp_vdpa_core.h
@@ -60,4 +60,6 @@ void nfp_vdpa_notify_queue(struct nfp_vdpa_hw *vdpa_hw, uint16_t qid);
 
 uint64_t nfp_vdpa_get_queue_notify_offset(struct nfp_vdpa_hw *vdpa_hw, int qid);
 
+void nfp_vdpa_irq_unmask(struct nfp_vdpa_hw *vdpa_hw);
+
 #endif /* __NFP_VDPA_CORE_H__ */
-- 
2.39.1


  parent reply	other threads:[~2024-06-14  7:03 UTC|newest]

Thread overview: 76+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-26  7:48 [PATCH 00/10] support software live migration Chaoyong He
2024-04-26  7:48 ` [PATCH 01/10] mailmap: add new contributor Chaoyong He
2024-04-26  7:48 ` [PATCH 02/10] vdpa/nfp: fix logic in hardware init Chaoyong He
2024-06-13 11:41   ` Maxime Coquelin
2024-04-26  7:48 ` [PATCH 03/10] vdpa/nfp: fix the logic of reconfiguration Chaoyong He
2024-06-13 11:43   ` Maxime Coquelin
2024-06-14  2:30     ` Chaoyong He
2024-04-26  7:48 ` [PATCH 04/10] vdpa/nfp: refactor the logic of datapath update Chaoyong He
2024-06-13 11:45   ` Maxime Coquelin
2024-04-26  7:48 ` [PATCH 05/10] vdpa/nfp: add the live migration logic Chaoyong He
2024-06-13 11:49   ` Maxime Coquelin
2024-06-14  2:32     ` Chaoyong He
2024-04-26  7:48 ` [PATCH 06/10] vdpa/nfp: add the interrupt logic of vring relay Chaoyong He
2024-06-13 11:52   ` Maxime Coquelin
2024-04-26  7:48 ` [PATCH 07/10] vdpa/nfp: setup the VF configure Chaoyong He
2024-06-13 11:58   ` Maxime Coquelin
2024-06-14  2:33     ` Chaoyong He
2024-04-26  7:48 ` [PATCH 08/10] vdpa/nfp: recover the ring index on new host Chaoyong He
2024-06-13 12:00   ` Maxime Coquelin
2024-06-14  6:40     ` Chaoyong He
2024-04-26  7:48 ` [PATCH 09/10] vdpa/nfp: setup vring relay thread Chaoyong He
2024-06-13 12:03   ` Maxime Coquelin
2024-04-26  7:48 ` [PATCH 10/10] doc: update nfp document Chaoyong He
2024-04-26 21:31   ` Patrick Robb
2024-06-13 12:03   ` Maxime Coquelin
2024-05-30  8:51 ` [PATCH 00/10] support software live migration Chaoyong He
2024-06-14  7:01 ` [PATCH v2 00/11] " Chaoyong He
2024-06-14  7:01   ` [PATCH v2 01/11] mailmap: add new contributor Chaoyong He
2024-06-14  7:01   ` [PATCH v2 02/11] vdpa/nfp: fix logic in hardware init Chaoyong He
2024-06-14  7:01   ` [PATCH v2 03/11] vdpa/nfp: fix the logic of reconfiguration Chaoyong He
2024-06-14  7:01   ` [PATCH v2 04/11] vdpa/nfp: refactor the logic of datapath update Chaoyong He
2024-06-14  7:01   ` [PATCH v2 05/11] vdpa/nfp: add the live migration logic Chaoyong He
2024-06-14  7:01   ` [PATCH v2 06/11] vdpa/nfp: add the interrupt logic of vring relay Chaoyong He
2024-06-14  7:01   ` [PATCH v2 07/11] vdpa/nfp: setup the VF configure Chaoyong He
2024-06-14  7:01   ` [PATCH v2 08/11] vdpa/nfp: recover the ring index on new host Chaoyong He
2024-06-14  7:01   ` Chaoyong He [this message]
2024-06-14  7:01   ` [PATCH v2 10/11] vdpa/nfp: enable feature bits of live migration Chaoyong He
2024-06-14  7:01   ` [PATCH v2 11/11] doc: update nfp document Chaoyong He
2024-06-17  6:26   ` [PATCH v3 00/11] support software live migration Chaoyong He
2024-06-17  6:26     ` [PATCH v3 01/11] mailmap: add new contributor Chaoyong He
2024-06-18  7:43       ` Maxime Coquelin
2024-06-17  6:26     ` [PATCH v3 02/11] vdpa/nfp: fix logic in hardware init Chaoyong He
2024-06-18  7:50       ` Maxime Coquelin
2024-06-17  6:27     ` [PATCH v3 03/11] vdpa/nfp: fix the logic of reconfiguration Chaoyong He
2024-06-18  7:51       ` Maxime Coquelin
2024-06-17  6:27     ` [PATCH v3 04/11] vdpa/nfp: refactor the logic of datapath update Chaoyong He
2024-06-18  8:12       ` Maxime Coquelin
2024-06-17  6:27     ` [PATCH v3 05/11] vdpa/nfp: add the live migration logic Chaoyong He
2024-06-18  8:12       ` Maxime Coquelin
2024-06-17  6:27     ` [PATCH v3 06/11] vdpa/nfp: add the interrupt logic of vring relay Chaoyong He
2024-06-18  8:14       ` Maxime Coquelin
2024-06-17  6:27     ` [PATCH v3 07/11] vdpa/nfp: setup the VF configure Chaoyong He
2024-06-18  8:19       ` Maxime Coquelin
2024-06-17  6:27     ` [PATCH v3 08/11] vdpa/nfp: recover the ring index on new host Chaoyong He
2024-06-18  8:20       ` Maxime Coquelin
2024-06-17  6:27     ` [PATCH v3 09/11] vdpa/nfp: setup vring relay thread Chaoyong He
2024-06-18  8:25       ` Maxime Coquelin
2024-06-17  6:27     ` [PATCH v3 10/11] vdpa/nfp: enable feature bits of live migration Chaoyong He
2024-06-18  8:25       ` Maxime Coquelin
2024-06-17  6:27     ` [PATCH v3 11/11] doc: update nfp document Chaoyong He
2024-06-18  8:25       ` Maxime Coquelin
2024-06-18  7:45     ` [PATCH v3 00/11] support software live migration Maxime Coquelin
2024-06-18  7:48       ` Chaoyong He
2024-08-05  2:12     ` [PATCH v4 " Chaoyong He
2024-08-05  2:12       ` [PATCH v4 01/11] mailmap: add new contributor Chaoyong He
2024-08-05  2:12       ` [PATCH v4 02/11] vdpa/nfp: fix logic in hardware init Chaoyong He
2024-08-05  2:12       ` [PATCH v4 03/11] vdpa/nfp: fix the logic of reconfiguration Chaoyong He
2024-08-05  2:12       ` [PATCH v4 04/11] vdpa/nfp: refactor the logic of datapath update Chaoyong He
2024-08-05  2:12       ` [PATCH v4 05/11] vdpa/nfp: add the live migration logic Chaoyong He
2024-08-05  2:12       ` [PATCH v4 06/11] vdpa/nfp: add the interrupt logic of vring relay Chaoyong He
2024-08-05  2:12       ` [PATCH v4 07/11] vdpa/nfp: setup the VF configure Chaoyong He
2024-08-05  2:12       ` [PATCH v4 08/11] vdpa/nfp: recover the ring index on new host Chaoyong He
2024-08-05  2:12       ` [PATCH v4 09/11] vdpa/nfp: setup vring relay thread Chaoyong He
2024-08-05  2:12       ` [PATCH v4 10/11] vdpa/nfp: enable feature bits of live migration Chaoyong He
2024-08-05  2:12       ` [PATCH v4 11/11] doc: update nfp document Chaoyong He
2024-09-19 12:05       ` [PATCH v4 00/11] support software live migration Maxime Coquelin

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=20240614070120.2663160-10-chaoyong.he@corigine.com \
    --to=chaoyong.he@corigine.com \
    --cc=dev@dpdk.org \
    --cc=long.wu@corigine.com \
    --cc=oss-drivers@corigine.com \
    --cc=peng.zhang@corigine.com \
    --cc=xinying.yu@corigine.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.
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).