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 v2 5/5] tcp_vu: Support multibuffer frames in tcp_vu_send_flag()
Date: Fri, 27 Mar 2026 19:09:58 +0100	[thread overview]
Message-ID: <20260327180958.833430-6-lvivier@redhat.com> (raw)
In-Reply-To: <20260327180958.833430-1-lvivier@redhat.com>

Build the Ethernet, IP, and TCP headers on the stack instead of
directly in the buffer via pointer casts, then write them into the
iovec with IOV_PUSH_HEADER().  This mirrors the approach already used
in tcp_vu_prepare() and udp_vu_prepare().

Remove the vu_eth(), vu_ip(), vu_payloadv4() and vu_payloadv6() helpers
from vu_common.h, as they are no longer used anywhere.

Introduce tcp_vu_send_dup() to handle DUP_ACK duplication using
vu_collect() and iov_memcopy() instead of a plain memcpy(), so that
the duplicated frame is also properly scattered across multiple iovecs.

Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
 iov.c       |   1 -
 tcp_vu.c    | 134 +++++++++++++++++++++++++++++-----------------------
 vu_common.h |  20 --------
 3 files changed, 76 insertions(+), 79 deletions(-)

diff --git a/iov.c b/iov.c
index 5bd8ece5a106..e0d2665683fa 100644
--- a/iov.c
+++ b/iov.c
@@ -207,7 +207,6 @@ void iov_memset(const struct iovec *iov, size_t iov_cnt, size_t offset, int c,
  *
  * Return: total number of bytes copied
  */
-/* cppcheck-suppress unusedFunction */
 size_t iov_memcopy(struct iovec *dst_iov, size_t dst_iov_cnt, size_t dst_offs,
 		   const struct iovec *iov, size_t iov_cnt, size_t offs)
 {
diff --git a/tcp_vu.c b/tcp_vu.c
index 557db13c24b0..9f8f66a66e04 100644
--- a/tcp_vu.c
+++ b/tcp_vu.c
@@ -74,6 +74,43 @@ static size_t tcp_vu_hdrlen(bool v6)
 	return hdrlen;
 }
 
+/**
+ * tcp_vu_send_dup() - Duplicate a frame into a new virtqueue element
+ * @c:		Execution context
+ * @vq:		Receive virtqueue
+ * @dest_elem:	Destination virtqueue element to collect
+ * @dest_iov:	Destination iovec array for collected buffers
+ * @max_dest_iov: Maximum number of entries in @dest_iov
+ * @src_iov:	Source iovec array containing the frame to duplicate
+ * @src_cnt:	Number of entries in @src_iov
+ *
+ * Return: number of virtqueue elements collected (0 if none available)
+ */
+static int tcp_vu_send_dup(const struct ctx *c, struct vu_virtq *vq,
+			   struct vu_virtq_element *dest_elem,
+			   struct iovec *dest_iov, size_t max_dest_iov,
+			   const struct iovec *src_iov, size_t src_cnt)
+{
+	size_t dest_cnt, frame_size = iov_size(src_iov, src_cnt);
+	const struct vu_dev *vdev = c->vdev;
+	int elem_cnt;
+
+	elem_cnt = vu_collect(vdev, vq, dest_elem, 1, dest_iov, max_dest_iov,
+			      &dest_cnt, frame_size, NULL);
+	if (elem_cnt == 0)
+		return 0;
+
+	vu_pad(dest_iov, dest_cnt, 0, frame_size);
+	iov_memcopy(dest_iov, dest_cnt, 0, src_iov, src_cnt, 0);
+
+	vu_flush(vdev, vq, dest_elem, elem_cnt);
+
+	if (*c->pcap)
+		pcap_iov(dest_iov, dest_cnt, VNET_HLEN);
+
+	return elem_cnt;
+}
+
 /**
  * tcp_vu_send_flag() - Send segment with flags to vhost-user (no payload)
  * @c:		Execution context
@@ -87,14 +124,14 @@ int tcp_vu_send_flag(const struct ctx *c, struct tcp_tap_conn *conn, int flags)
 	struct vu_dev *vdev = c->vdev;
 	struct vu_virtq *vq = &vdev->vq[VHOST_USER_RX_QUEUE];
 	struct vu_virtq_element flags_elem[2];
-	struct ipv6hdr *ip6h = NULL;
-	struct iphdr *ip4h = NULL;
-	struct iovec flags_iov[2];
-	struct tcp_syn_opts *opts;
+	size_t optlen, hdrlen, iov_cnt;
+	struct iovec flags_iov[64];
+	struct tcp_syn_opts opts;
 	struct iov_tail payload;
-	size_t optlen, hdrlen;
-	struct tcphdr *th;
-	struct ethhdr *eh;
+	struct ipv6hdr ip6h;
+	struct iphdr ip4h;
+	struct tcphdr th;
+	struct ethhdr eh;
 	uint32_t seq;
 	int elem_cnt;
 	int ret;
@@ -102,77 +139,58 @@ int tcp_vu_send_flag(const struct ctx *c, struct tcp_tap_conn *conn, int flags)
 	hdrlen = tcp_vu_hdrlen(CONN_V6(conn));
 
 	elem_cnt = vu_collect(vdev, vq, &flags_elem[0], 1,
-			      &flags_iov[0], 1, NULL,
-			      hdrlen + sizeof(*opts), NULL);
-	if (elem_cnt != 1)
+			      flags_iov, ARRAY_SIZE(flags_iov), &iov_cnt,
+			      hdrlen + sizeof(opts), NULL);
+	if (elem_cnt == 0)
 		return -1;
 
-	assert(flags_elem[0].in_num == 1);
-	assert(flags_elem[0].in_sg[0].iov_len >=
-	       MAX(hdrlen + sizeof(*opts), ETH_ZLEN + VNET_HLEN));
-
-	eh = vu_eth(flags_elem[0].in_sg[0].iov_base);
-
-	memcpy(eh->h_dest, c->guest_mac, sizeof(eh->h_dest));
-	memcpy(eh->h_source, c->our_tap_mac, sizeof(eh->h_source));
-
-	if (CONN_V4(conn)) {
-		eh->h_proto = htons(ETH_P_IP);
-
-		ip4h = vu_ip(flags_elem[0].in_sg[0].iov_base);
-		*ip4h = (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_TCP);
-
-		th = vu_payloadv4(flags_elem[0].in_sg[0].iov_base);
-	} else {
-		eh->h_proto = htons(ETH_P_IPV6);
+	memcpy(eh.h_dest, c->guest_mac, sizeof(eh.h_dest));
 
-		ip6h = vu_ip(flags_elem[0].in_sg[0].iov_base);
-		*ip6h = (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_TCP);
-		th = vu_payloadv6(flags_elem[0].in_sg[0].iov_base);
-	}
+	if (CONN_V4(conn))
+		ip4h = (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_TCP);
+	else
+		ip6h = (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_TCP);
 
-	memset(th, 0, sizeof(*th));
-	th->doff = sizeof(*th) / 4;
-	th->ack = 1;
+	memset(&th, 0, sizeof(th));
 
 	seq = conn->seq_to_tap;
-	opts = (struct tcp_syn_opts *)(th + 1);
-	ret = tcp_prepare_flags(c, conn, flags, th, opts, &optlen);
+	ret = tcp_prepare_flags(c, conn, flags, &th, &opts, &optlen);
 	if (ret <= 0) {
-		vu_queue_rewind(vq, 1);
+		vu_queue_rewind(vq, elem_cnt);
 		return ret;
 	}
 
-	vu_pad(&flags_iov[0], 1, 0, hdrlen + optlen);
-	payload = IOV_TAIL(flags_elem[0].in_sg, 1, hdrlen);
+	vu_pad(flags_elem[0].in_sg, iov_cnt, 0, hdrlen + optlen);
 
 	if (flags & KEEPALIVE)
 		seq--;
 
-	tcp_fill_headers(c, conn, eh, ip4h, ip6h, th, &payload,
+	payload = IOV_TAIL(flags_elem[0].in_sg, iov_cnt, hdrlen);
+	iov_from_buf(payload.iov, payload.cnt, payload.off, &opts, optlen);
+	tcp_fill_headers(c, conn, &eh, CONN_V4(conn) ? &ip4h : NULL,
+			 CONN_V6(conn) ? &ip6h : NULL, &th, &payload,
 			 IP4_CSUM | (*c->pcap ? TCP_CSUM : 0), seq);
 
-	vu_flush(vdev, vq, flags_elem, 1);
+	vu_flush(vdev, vq, flags_elem, elem_cnt);
+
+	/* write headers */
+	payload = IOV_TAIL(flags_elem[0].in_sg, iov_cnt, VNET_HLEN);
+
+	IOV_PUSH_HEADER(&payload, eh);
+	if (CONN_V4(conn))
+		IOV_PUSH_HEADER(&payload, ip4h);
+	else
+		IOV_PUSH_HEADER(&payload, ip6h);
+	IOV_PUSH_HEADER(&payload, th);
 
 	if (*c->pcap)
-		pcap_iov(&flags_elem[0].in_sg[0], 1, VNET_HLEN);
+		pcap_iov(flags_elem[0].in_sg, iov_cnt, VNET_HLEN);
 
 	if (flags & DUP_ACK) {
-		elem_cnt = vu_collect(vdev, vq, &flags_elem[1], 1,
-				      &flags_iov[1], 1, NULL,
-				      hdrlen + optlen, NULL);
-		if (elem_cnt == 1 &&
-		    flags_elem[1].in_sg[0].iov_len >=
-		    flags_elem[0].in_sg[0].iov_len) {
-			vu_pad(&flags_iov[1], 1, 0, hdrlen + optlen);
-			memcpy(flags_elem[1].in_sg[0].iov_base,
-			       flags_elem[0].in_sg[0].iov_base,
-			       flags_elem[0].in_sg[0].iov_len);
-			vu_flush(vdev, vq, &flags_elem[1], 1);
-
-			if (*c->pcap)
-				pcap_iov(&flags_elem[1].in_sg[0], 1, VNET_HLEN);
-		}
+		tcp_vu_send_dup(c, vq, &flags_elem[elem_cnt],
+				&flags_iov[iov_cnt],
+				ARRAY_SIZE(flags_iov) - iov_cnt,
+				flags_elem[0].in_sg, iov_cnt);
 	}
 	vu_queue_notify(vdev, vq);
 
diff --git a/vu_common.h b/vu_common.h
index 13e0126fb16c..cb9407e5cf20 100644
--- a/vu_common.h
+++ b/vu_common.h
@@ -15,26 +15,6 @@
 #include "ip.h"
 #include "virtio.h"
 
-static inline void *vu_eth(void *base)
-{
-	return ((char *)base + VNET_HLEN);
-}
-
-static inline void *vu_ip(void *base)
-{
-	return (struct ethhdr *)vu_eth(base) + 1;
-}
-
-static inline void *vu_payloadv4(void *base)
-{
-	return (struct iphdr *)vu_ip(base) + 1;
-}
-
-static inline void *vu_payloadv6(void *base)
-{
-	return (struct ipv6hdr *)vu_ip(base) + 1;
-}
-
 int vu_collect(const struct vu_dev *vdev, struct vu_virtq *vq,
 	       struct vu_virtq_element *elem, int max_elem,
 	       struct iovec *in_sg, size_t max_in_sg, size_t *in_total,
-- 
2.53.0


      parent reply	other threads:[~2026-03-27 18:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-27 18:09 [PATCH v2 0/5] vhost-user,tcp: Handle multiple iovec entries per virtqueue element Laurent Vivier
2026-03-27 18:09 ` [PATCH v2 1/5] tcp: Encode checksum computation flags in a single parameter Laurent Vivier
2026-03-27 18:09 ` [PATCH v2 2/5] tcp_vu: Build headers on the stack and write them into the iovec Laurent Vivier
2026-03-27 18:09 ` [PATCH v2 3/5] tcp_vu: Support multibuffer frames in tcp_vu_sock_recv() Laurent Vivier
2026-03-27 18:09 ` [PATCH v2 4/5] iov: Add iov_memcopy() to copy data between iovec arrays Laurent Vivier
2026-03-27 18:09 ` 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=20260327180958.833430-6-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).