public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Laurent Vivier <lvivier@redhat.com>
To: passt-dev@passt.top
Cc: Laurent Vivier <lvivier@redhat.com>
Subject: [PATCH v4 28/28] packet: add memory regions information into pool
Date: Wed, 16 Apr 2025 17:26:05 +0200	[thread overview]
Message-ID: <20250416152605.3947623-29-lvivier@redhat.com> (raw)
In-Reply-To: <20250416152605.3947623-1-lvivier@redhat.com>

Do not hack anymore the buf pointer to detect and manage memory regions

Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
 packet.c     |  4 ++--
 packet.h     | 16 ++++++++++++----
 tap.c        | 23 +++++++++++++++--------
 tap.h        |  1 -
 vhost_user.c | 28 +++++++++++-----------------
 virtio.c     |  4 ++--
 virtio.h     |  8 ++++++--
 vu_common.c  | 20 +++++++++++---------
 8 files changed, 59 insertions(+), 45 deletions(-)

diff --git a/packet.c b/packet.c
index 53ede9fcb509..0a2ad28d3d25 100644
--- a/packet.c
+++ b/packet.c
@@ -41,10 +41,10 @@ static int packet_check_range(const struct pool *p, const char *ptr, size_t len,
 		return -1;
 	}
 
-	if (p->buf_size == 0) {
+	if (p->memory) {
 		int ret;
 
-		ret = vu_packet_check_range((void *)p->buf, ptr, len);
+		ret = vu_packet_check_range(p->memory, ptr, len);
 
 		if (ret == -1)
 			debug("cannot find region, %s:%i", func, line);
diff --git a/packet.h b/packet.h
index bbea04eac455..d553521a30c5 100644
--- a/packet.h
+++ b/packet.h
@@ -9,6 +9,8 @@
 #include <stdbool.h>
 #include "iov.h"
 
+struct vdev_memory;
+
 /* Maximum size of a single packet stored in pool, including headers */
 #define PACKET_MAX_LEN	((size_t)UINT16_MAX)
 
@@ -20,6 +22,7 @@
  * 		0 for passt vhost-user mode
  * @size:	Number of usable descriptors for the pool
  * @count:	Number of used descriptors for the pool
+ * @memory:	Memory regions defined for vhost-user, NULL otherwise
  * @pkt:	Descriptors: see macros below
  */
 struct pool {
@@ -27,10 +30,12 @@ struct pool {
 	size_t buf_size;
 	size_t size;
 	size_t count;
+	struct vdev_memory *memory;
 	struct iovec pkt[];
 };
 
-int vu_packet_check_range(void *buf, const char *ptr, size_t len);
+int vu_packet_check_range(struct vdev_memory *memory,
+			  const char *ptr, size_t len);
 void packet_add_do(struct pool *p, struct iov_tail *data,
 		   const char *func, int line);
 bool packet_data_do(const struct pool *p, const size_t idx,
@@ -50,18 +55,21 @@ struct _name ## _t {							\
 	size_t buf_size;						\
 	size_t size;							\
 	size_t count;							\
+	struct vdev_memory *memory;					\
 	struct iovec pkt[_size];					\
 }
 
-#define PACKET_POOL_INIT_NOCAST(_size, _buf, _buf_size)			\
+#define PACKET_POOL_INIT_NOCAST(_size, _buf, _buf_size, _memory)	\
 {									\
 	.buf_size = _buf_size,						\
 	.buf = _buf,							\
 	.size = _size,							\
+	.memory = _memory						\
 }
 
-#define PACKET_INIT(name, size, buf, buf_size)				\
-	(struct name ## _t) PACKET_POOL_INIT_NOCAST(size, buf, buf_size)
+#define PACKET_INIT(name, size, buf, buf_size, memory)			\
+	(struct name ## _t)						\
+		PACKET_POOL_INIT_NOCAST(size, buf, buf_size, memory)
 
 #define PACKET_POOL_NOINIT(name, size)					\
 	PACKET_POOL_DECL(name, size) name ## _storage;			\
diff --git a/tap.c b/tap.c
index 615609d4ded2..419b09b4cee7 100644
--- a/tap.c
+++ b/tap.c
@@ -1453,17 +1453,23 @@ static void tap_sock_tun_init(struct ctx *c)
  * tap_sock_update_pool() - Set the buffer base and size for the pool of packets
  * @base:	Buffer base
  * @size	Buffer size
+ * @memory:	vhost-user memory regions
  */
-void tap_sock_update_pool(void *base, size_t size)
+static void tap_sock_update_pool(void *base, size_t size,
+				 struct vdev_memory *memory)
 {
 	int i;
 
-	pool_tap4_storage = PACKET_INIT(pool_tap4, TAP_MSGS_IP4, base, size);
-	pool_tap6_storage = PACKET_INIT(pool_tap6, TAP_MSGS_IP6, base, size);
+	pool_tap4_storage = PACKET_INIT(pool_tap4, TAP_MSGS_IP4, base, size,
+					memory);
+	pool_tap6_storage = PACKET_INIT(pool_tap6, TAP_MSGS_IP6, base, size,
+					memory);
 
 	for (i = 0; i < TAP_SEQS; i++) {
-		tap4_l4[i].p = PACKET_INIT(pool_l4, UIO_MAXIOV, base, size);
-		tap6_l4[i].p = PACKET_INIT(pool_l4, UIO_MAXIOV, base, size);
+		tap4_l4[i].p = PACKET_INIT(pool_l4, UIO_MAXIOV, base, size,
+					   memory);
+		tap6_l4[i].p = PACKET_INIT(pool_l4, UIO_MAXIOV, base, size,
+					   memory);
 	}
 }
 
@@ -1474,12 +1480,13 @@ void tap_sock_update_pool(void *base, size_t size)
  */
 void tap_backend_init(struct ctx *c)
 {
+	struct vdev_memory *memory = NULL;
+
 	if (c->mode == MODE_VU) {
-		tap_sock_update_pool(NULL, 0);
 		vu_init(c);
-	} else {
-		tap_sock_update_pool(pkt_buf, sizeof(pkt_buf));
+		memory = &c->vdev->memory;
 	}
+	tap_sock_update_pool(pkt_buf, sizeof(pkt_buf), memory);
 
 	if (c->fd_tap != -1) { /* Passed as --fd */
 		ASSERT(c->one_off);
diff --git a/tap.h b/tap.h
index 095190eea44e..38f560b678f0 100644
--- a/tap.h
+++ b/tap.h
@@ -116,7 +116,6 @@ void tap_handler_passt(struct ctx *c, uint32_t events,
 int tap_sock_unix_open(char *sock_path);
 void tap_sock_reset(struct ctx *c);
 void tap_sock_set_mr(struct vhost_user_memory *mr, int nb);
-void tap_sock_update_pool(void *base, size_t size);
 void tap_backend_init(struct ctx *c);
 void tap_flush_pools(void);
 void tap_handler(struct ctx *c, const struct timespec *now);
diff --git a/vhost_user.c b/vhost_user.c
index 105f77a5df2b..e53ea21909e3 100644
--- a/vhost_user.c
+++ b/vhost_user.c
@@ -137,8 +137,8 @@ static void *qva_to_va(struct vu_dev *dev, uint64_t qemu_addr)
 	unsigned int i;
 
 	/* Find matching memory region.  */
-	for (i = 0; i < dev->nregions; i++) {
-		const struct vu_dev_region *r = &dev->regions[i];
+	for (i = 0; i < dev->memory.nregions; i++) {
+		const struct vu_dev_region *r = &dev->memory.regions[i];
 
 		if ((qemu_addr >= r->qva) && (qemu_addr < (r->qva + r->size))) {
 			/* NOLINTNEXTLINE(performance-no-int-to-ptr) */
@@ -428,8 +428,8 @@ static bool vu_set_mem_table_exec(struct vu_dev *vdev,
 	struct vhost_user_memory m = msg->payload.memory, *memory = &m;
 	unsigned int i;
 
-	for (i = 0; i < vdev->nregions; i++) {
-		const struct vu_dev_region *r = &vdev->regions[i];
+	for (i = 0; i < vdev->memory.nregions; i++) {
+		const struct vu_dev_region *r = &vdev->memory.regions[i];
 
 		if (r->mmap_addr) {
 			/* NOLINTNEXTLINE(performance-no-int-to-ptr) */
@@ -437,12 +437,12 @@ static bool vu_set_mem_table_exec(struct vu_dev *vdev,
 			       r->size + r->mmap_offset);
 		}
 	}
-	vdev->nregions = memory->nregions;
+	vdev->memory.nregions = memory->nregions;
 
 	debug("vhost-user nregions: %u", memory->nregions);
-	for (i = 0; i < vdev->nregions; i++) {
+	for (i = 0; i < vdev->memory.nregions; i++) {
 		struct vhost_user_memory_region *msg_region = &memory->regions[i];
-		struct vu_dev_region *dev_region = &vdev->regions[i];
+		struct vu_dev_region *dev_region = &vdev->memory.regions[i];
 		void *mmap_addr;
 
 		debug("vhost-user region %d", i);
@@ -484,13 +484,7 @@ static bool vu_set_mem_table_exec(struct vu_dev *vdev,
 		}
 	}
 
-	/* As vu_packet_check_range() has no access to the number of
-	 * memory regions, mark the end of the array with mmap_addr = 0
-	 */
-	ASSERT(vdev->nregions < VHOST_USER_MAX_RAM_SLOTS - 1);
-	vdev->regions[vdev->nregions].mmap_addr = 0;
-
-	tap_sock_update_pool(vdev->regions, 0);
+	ASSERT(vdev->memory.nregions < VHOST_USER_MAX_RAM_SLOTS);
 
 	return false;
 }
@@ -1107,8 +1101,8 @@ void vu_cleanup(struct vu_dev *vdev)
 		vq->vring.avail = 0;
 	}
 
-	for (i = 0; i < vdev->nregions; i++) {
-		const struct vu_dev_region *r = &vdev->regions[i];
+	for (i = 0; i < vdev->memory.nregions; i++) {
+		const struct vu_dev_region *r = &vdev->memory.regions[i];
 
 		if (r->mmap_addr) {
 			/* NOLINTNEXTLINE(performance-no-int-to-ptr) */
@@ -1116,7 +1110,7 @@ void vu_cleanup(struct vu_dev *vdev)
 			       r->size + r->mmap_offset);
 		}
 	}
-	vdev->nregions = 0;
+	vdev->memory.nregions = 0;
 
 	vu_close_log(vdev);
 
diff --git a/virtio.c b/virtio.c
index bc2b89a7f4a7..2b0a8d0dc4c5 100644
--- a/virtio.c
+++ b/virtio.c
@@ -102,8 +102,8 @@ static void *vu_gpa_to_va(const struct vu_dev *dev, uint64_t *plen,
 		return NULL;
 
 	/* Find matching memory region. */
-	for (i = 0; i < dev->nregions; i++) {
-		const struct vu_dev_region *r = &dev->regions[i];
+	for (i = 0; i < dev->memory.nregions; i++) {
+		const struct vu_dev_region *r = &dev->memory.regions[i];
 
 		if ((guest_addr >= r->gpa) &&
 		    (guest_addr < (r->gpa + r->size))) {
diff --git a/virtio.h b/virtio.h
index 7a370bdeae65..5acd4f19f0ed 100644
--- a/virtio.h
+++ b/virtio.h
@@ -96,6 +96,11 @@ struct vu_dev_region {
  */
 #define VHOST_USER_MAX_RAM_SLOTS 32
 
+struct vdev_memory {
+	uint32_t nregions;
+	struct vu_dev_region regions[VHOST_USER_MAX_RAM_SLOTS];
+};
+
 /**
  * struct vu_dev - vhost-user device information
  * @context:			Execution context
@@ -109,8 +114,7 @@ struct vu_dev_region {
  */
 struct vu_dev {
 	struct ctx *context;
-	uint32_t nregions;
-	struct vu_dev_region regions[VHOST_USER_MAX_RAM_SLOTS];
+	struct vdev_memory memory;
 	struct vu_virtq vq[VHOST_USER_MAX_QUEUES];
 	uint64_t features;
 	uint64_t protocol_features;
diff --git a/vu_common.c b/vu_common.c
index b77b21420c57..19fe22935a1e 100644
--- a/vu_common.c
+++ b/vu_common.c
@@ -25,26 +25,28 @@
 /**
  * vu_packet_check_range() - Check if a given memory zone is contained in
  * 			     a mapped guest memory region
- * @buf:	Array of the available memory regions
+ * @memory:	Allowed memory regions
  * @ptr:	Start of desired data range
  * @size:	Length of desired data range
  *
  * Return: 0 if the zone is in a mapped memory region, -1 otherwise
  */
-int vu_packet_check_range(void *buf, const char *ptr, size_t len)
+int vu_packet_check_range(struct vdev_memory *memory,
+			  const char *ptr, size_t len)
 {
-	struct vu_dev_region *dev_region;
+	struct vu_dev_region *dev_region = memory->regions;
+	unsigned int i;
 
-	for (dev_region = buf; dev_region->mmap_addr; dev_region++) {
-		uintptr_t base_addr = dev_region->mmap_addr +
-			dev_region->mmap_offset;
+	for (i = 0; i < memory->nregions; i++) {
+		uintptr_t base_addr = dev_region[i].mmap_addr +
+			dev_region[i].mmap_offset;
 		/* NOLINTNEXTLINE(performance-no-int-to-ptr) */
 		const char *base = (const char *)base_addr;
 
-		ASSERT(base_addr >= dev_region->mmap_addr);
+		ASSERT(base_addr >= dev_region[i].mmap_addr);
 
-		if (len <= dev_region->size && base <= ptr &&
-		    (size_t)(ptr - base) <= dev_region->size - len)
+		if (len <= dev_region[i].size && base <= ptr &&
+		    (size_t)(ptr - base) <= dev_region[i].size - len)
 			return 0;
 	}
 
-- 
@@ -25,26 +25,28 @@
 /**
  * vu_packet_check_range() - Check if a given memory zone is contained in
  * 			     a mapped guest memory region
- * @buf:	Array of the available memory regions
+ * @memory:	Allowed memory regions
  * @ptr:	Start of desired data range
  * @size:	Length of desired data range
  *
  * Return: 0 if the zone is in a mapped memory region, -1 otherwise
  */
-int vu_packet_check_range(void *buf, const char *ptr, size_t len)
+int vu_packet_check_range(struct vdev_memory *memory,
+			  const char *ptr, size_t len)
 {
-	struct vu_dev_region *dev_region;
+	struct vu_dev_region *dev_region = memory->regions;
+	unsigned int i;
 
-	for (dev_region = buf; dev_region->mmap_addr; dev_region++) {
-		uintptr_t base_addr = dev_region->mmap_addr +
-			dev_region->mmap_offset;
+	for (i = 0; i < memory->nregions; i++) {
+		uintptr_t base_addr = dev_region[i].mmap_addr +
+			dev_region[i].mmap_offset;
 		/* NOLINTNEXTLINE(performance-no-int-to-ptr) */
 		const char *base = (const char *)base_addr;
 
-		ASSERT(base_addr >= dev_region->mmap_addr);
+		ASSERT(base_addr >= dev_region[i].mmap_addr);
 
-		if (len <= dev_region->size && base <= ptr &&
-		    (size_t)(ptr - base) <= dev_region->size - len)
+		if (len <= dev_region[i].size && base <= ptr &&
+		    (size_t)(ptr - base) <= dev_region[i].size - len)
 			return 0;
 	}
 
-- 
2.49.0


      parent reply	other threads:[~2025-04-16 15:27 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-16 15:25 [PATCH v4 00/28] Introduce discontiguous frames management Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 01/28] arp: Don't mix incoming and outgoing buffers Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 02/28] iov: Introduce iov_slice(), iov_tail_slice() and iov_tail_drop() Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 03/28] iov: Update IOV_REMOVE_HEADER() and IOV_PEEK_HEADER() Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 04/28] tap: Use iov_tail with tap_add_packet() Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 05/28] packet: Use iov_tail with packet_add() Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 06/28] packet: Add packet_data() Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 07/28] arp: Convert to iov_tail Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 08/28] ndp: " Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 09/28] icmp: " Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 10/28] udp: " Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 11/28] tcp: Convert tcp_tap_handler() to use iov_tail Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 12/28] tcp: Convert tcp_data_from_tap() " Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 13/28] dhcpv6: move offset initialization out of dhcpv6_opt() Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 14/28] dhcpv6: Extract sending of NotOnLink status Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 15/28] dhcpv6: Convert to iov_tail Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 16/28] dhcpv6: Use iov_tail in dhcpv6_opt() Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 17/28] dhcp: Convert to iov_tail Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 18/28] ip: Use iov_tail in ipv6_l4hdr() Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 19/28] tap: Convert tap4_handler() to iov_tail Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 20/28] tap: Convert tap6_handler() " Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 21/28] arp: use iov_tail rather than pool Laurent Vivier
2025-04-16 15:25 ` [PATCH v4 22/28] dhcp: " Laurent Vivier
2025-04-16 15:26 ` [PATCH v4 23/28] dhcpv6: " Laurent Vivier
2025-04-16 15:26 ` [PATCH v4 24/28] icmp: " Laurent Vivier
2025-04-16 15:26 ` [PATCH v4 25/28] ndp: " Laurent Vivier
2025-04-16 15:26 ` [PATCH v4 26/28] packet: remove PACKET_POOL() and PACKET_POOL_P() Laurent Vivier
2025-04-16 15:26 ` [PATCH v4 27/28] packet: remove unused parameter from PACKET_POOL_DECL() Laurent Vivier
2025-04-16 15:26 ` Laurent Vivier [this message]

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=20250416152605.3947623-29-lvivier@redhat.com \
    --to=lvivier@redhat.com \
    --cc=passt-dev@passt.top \
    /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).