public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
* [PATCH] iov: Add iov_truncate() helper and use it in udp_vu_sock_recv()
@ 2026-03-04 19:21 Laurent Vivier
  0 siblings, 0 replies; only message in thread
From: Laurent Vivier @ 2026-03-04 19:21 UTC (permalink / raw)
  To: passt-dev; +Cc: Laurent Vivier

Add a generic iov_truncate() function that truncates an IO vector to a
given number of bytes, returning the number of iov entries that contain
data after truncation.

Use it in udp_vu_sock_recv() to replace the open-coded truncation logic
that adjusted iov entries after recvmsg().

Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
 iov.c    | 22 ++++++++++++++++++++++
 iov.h    |  1 +
 udp_vu.c | 12 +++---------
 3 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/iov.c b/iov.c
index ad726daa4cd8..31a3f5bc29e5 100644
--- a/iov.c
+++ b/iov.c
@@ -147,6 +147,28 @@ size_t iov_size(const struct iovec *iov, size_t iov_cnt)
 	return len;
 }
 
+/**
+ * iov_truncate() - Truncate an IO vector to a given number of bytes
+ * @iov:	IO vector (modified)
+ * @iov_cnt:	Number of entries in @iov
+ * @size:	Total number of bytes to keep
+ *
+ * Return: number of iov entries that contain data after truncation
+ */
+size_t iov_truncate(struct iovec *iov, size_t iov_cnt, size_t size)
+{
+	size_t i, offset;
+
+	i = iov_skip_bytes(iov, iov_cnt, size, &offset);
+
+	if (i < iov_cnt) {
+		iov[i].iov_len = offset;
+		i += !!offset;
+	}
+
+	return i;
+}
+
 /**
  * iov_tail_prune() - Remove any unneeded buffers from an IOV tail
  * @tail:	IO vector tail (modified)
diff --git a/iov.h b/iov.h
index d2184bfd12bd..f87436f5479b 100644
--- a/iov.h
+++ b/iov.h
@@ -28,6 +28,7 @@ size_t iov_from_buf(const struct iovec *iov, size_t iov_cnt,
 size_t iov_to_buf(const struct iovec *iov, size_t iov_cnt,
 		  size_t offset, void *buf, size_t bytes);
 size_t iov_size(const struct iovec *iov, size_t iov_cnt);
+size_t iov_truncate(struct iovec *iov, size_t iov_cnt, size_t size);
 
 /*
  * DOC: Theory of Operation, struct iov_tail
diff --git a/udp_vu.c b/udp_vu.c
index 51f3718f5925..3f558a33b4f2 100644
--- a/udp_vu.c
+++ b/udp_vu.c
@@ -71,9 +71,9 @@ static int udp_vu_sock_recv(const struct ctx *c, struct vu_virtq *vq, int s,
 			    bool v6, ssize_t *dlen)
 {
 	const struct vu_dev *vdev = c->vdev;
-	int iov_cnt, idx, iov_used;
-	size_t off, hdrlen, l2len;
 	struct msghdr msg  = { 0 };
+	int iov_cnt, iov_used;
+	size_t hdrlen, l2len;
 
 	ASSERT(!c->no_udp);
 
@@ -115,13 +115,7 @@ static int udp_vu_sock_recv(const struct ctx *c, struct vu_virtq *vq, int s,
 	iov_vu[0].iov_base = (char *)iov_vu[0].iov_base - hdrlen;
 	iov_vu[0].iov_len += hdrlen;
 
-	/* count the numbers of buffer filled by recvmsg() */
-	idx = iov_skip_bytes(iov_vu, iov_cnt, *dlen + hdrlen, &off);
-
-	/* adjust last iov length */
-	if (idx < iov_cnt)
-		iov_vu[idx].iov_len = off;
-	iov_used = idx + !!off;
+	iov_used = iov_truncate(iov_vu, iov_cnt, *dlen + hdrlen);
 
 	/* pad frame to 60 bytes: first buffer is at least ETH_ZLEN long */
 	l2len = *dlen + hdrlen - VNET_HLEN;
-- 
2.53.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-03-04 19:21 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-03-04 19:21 [PATCH] iov: Add iov_truncate() helper and use it in udp_vu_sock_recv() Laurent Vivier

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).