From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: passt.top; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=KucGyHk3; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTPS id 76C595A0626 for ; Mon, 23 Mar 2026 17:53:15 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1774284794; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yaYKlLlhky3yNoJgSSQ+O5To2v7dVWNHVl0mpEizv4g=; b=KucGyHk3ZIm3lRRG12ERd2wB/nVhkVwe3/FppPapeK+qHZqixDtYVlx5hkFnqLcJzzjxX8 jpplMCbgSaihoqLMxuDPgI7f3vdT4sucaIkcSgeDp+SCHFIZMt4AO+3cTeQQ7Brgum9QmE qnhTAoTKkgxsL4s2CJ4y2E/tI8lFj+M= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-263-VBLr2YlwNEaMBqYK51zB9w-1; Mon, 23 Mar 2026 12:53:12 -0400 X-MC-Unique: VBLr2YlwNEaMBqYK51zB9w-1 X-Mimecast-MFC-AGG-ID: VBLr2YlwNEaMBqYK51zB9w_1774284792 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 22D0E195607B for ; Mon, 23 Mar 2026 16:53:12 +0000 (UTC) Received: from lenovo-t14s.redhat.com (unknown [10.44.32.96]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2E34930001A2; Mon, 23 Mar 2026 16:53:10 +0000 (UTC) From: Laurent Vivier To: passt-dev@passt.top Subject: [PATCH 7/7] tcp_vu: Use iov_tail helpers to build headers in tcp_vu_send_flag() Date: Mon, 23 Mar 2026 17:52:59 +0100 Message-ID: <20260323165259.1253482-8-lvivier@redhat.com> In-Reply-To: <20260323165259.1253482-1-lvivier@redhat.com> References: <20260323165259.1253482-1-lvivier@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 3jSEoBoHSecFZhs5QkTbn350kx9QUF1FnBvlwCWtLtE_1774284792 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: CSCZFIQJNFPYZUSAXZRTWAAPP6V6IEVS X-Message-ID-Hash: CSCZFIQJNFPYZUSAXZRTWAAPP6V6IEVS X-MailFrom: lvivier@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Laurent Vivier X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: tcp_vu_send_flag() previously used vu_eth(), vu_ip(), vu_payloadv4() and vu_payloadv6() to access headers via direct pointer arithmetic on a single contiguous buffer. Replace these with iov_tail-based with_header() and IOV_DROP_HEADER() accessors, which work correctly with multi-buffer virtio descriptors. Enlarge the flags_iov and flags_elem arrays to accommodate split descriptors, and pass iov_cnt through vu_collect() so we track how many iovecs the frame actually spans. Extract a tcp_vu_send_dup() helper for the DUP_ACK path that uses iov_memcopy() to duplicate a frame across potentially multi-buffer descriptors, replacing the previous single-buffer memcpy. With all callers converted, remove the vu_eth(), vu_ip(), vu_payloadv4() and vu_payloadv6() inline helpers from vu_common.h, and drop the cppcheck-suppress annotation on iov_memcopy() which is now used. Signed-off-by: Laurent Vivier --- iov.c | 1 - tcp_vu.c | 113 ++++++++++++++++++++++++++++------------------------ vu_common.h | 20 ---------- 3 files changed, 62 insertions(+), 72 deletions(-) diff --git a/iov.c b/iov.c index 55263ec10b44..9638dc6de03d 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 d6bd6a34d6da..776b47aea18c 100644 --- a/tcp_vu.c +++ b/tcp_vu.c @@ -74,6 +74,41 @@ 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); + + 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,13 +122,9 @@ 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]; + size_t optlen, hdrlen, iov_cnt; + struct iovec flags_iov[64]; struct iov_tail payload; - size_t optlen, hdrlen; - struct tcphdr *th; - struct ethhdr *eh; uint32_t seq; int elem_cnt; int nb_ack; @@ -102,77 +133,57 @@ 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, + flags_iov, ARRAY_SIZE(flags_iov), &iov_cnt, hdrlen + sizeof(struct tcp_syn_opts), NULL); - if (elem_cnt != 1) + 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(struct tcp_syn_opts), ETH_ZLEN + VNET_HLEN)); - - vu_set_vnethdr(flags_elem[0].in_sg[0].iov_base, 1); + vu_set_vnethdr(flags_elem[0].in_sg[0].iov_base, elem_cnt); - eh = vu_eth(flags_elem[0].in_sg[0].iov_base); + payload = IOV_TAIL(flags_elem[0].in_sg, iov_cnt, VNET_HLEN); - memcpy(eh->h_dest, c->guest_mac, sizeof(eh->h_dest)); - memcpy(eh->h_source, c->our_tap_mac, sizeof(eh->h_source)); + with_header(struct ethhdr, eh, &payload) + memcpy(eh->h_dest, c->guest_mac, sizeof(eh->h_dest)); + IOV_DROP_HEADER(&payload, struct ethhdr); 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); + with_header(struct iphdr, ip4h, &payload) + *ip4h = (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_TCP); + IOV_DROP_HEADER(&payload, struct iphdr); } else { - eh->h_proto = htons(ETH_P_IPV6); - - 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); + with_header(struct ipv6hdr, ip6h, &payload) + *ip6h = (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_TCP); + IOV_DROP_HEADER(&payload, struct ipv6hdr); } - memset(th, 0, sizeof(*th)); - th->doff = sizeof(*th) / 4; - th->ack = 1; + with_header(struct tcphdr, th, &payload) + memset(th, 0, sizeof(*th)); seq = conn->seq_to_tap; - payload = IOV_TAIL(flags_elem[0].in_sg, 1, hdrlen - sizeof(*th)); ret = tcp_prepare_flags(c, conn, flags, &payload, &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); + vu_pad(flags_elem[0].in_sg, iov_cnt, 0, hdrlen + optlen); if (flags & KEEPALIVE) seq--; - payload = IOV_TAIL(flags_elem[0].in_sg, 1, VNET_HLEN); + payload = IOV_TAIL(flags_elem[0].in_sg, iov_cnt, VNET_HLEN); tcp_fill_headers(c, conn, CONN_V4(conn), &payload, -1, seq, !*c->pcap); if (*c->pcap) - pcap_iov(&flags_elem[0].in_sg[0], 1, VNET_HLEN); - nb_ack = 1; + pcap_iov(flags_elem[0].in_sg, iov_cnt, VNET_HLEN); + nb_ack = elem_cnt; 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); - nb_ack++; - - if (*c->pcap) - pcap_iov(&flags_elem[1].in_sg[0], 1, VNET_HLEN); - } + elem_cnt = 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); + nb_ack += elem_cnt; } vu_flush(vdev, vq, flags_elem, nb_ack); diff --git a/vu_common.h b/vu_common.h index f09dd25d3d1f..8e42ce88b2ab 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