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=F1MH7TBB; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by passt.top (Postfix) with ESMTPS id 95A105A0265 for ; Wed, 20 May 2026 17:18:33 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1779290312; 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; bh=lOGfz10pShAECFSyqjMj929bbYg3Wx4yutLDg9o+bOs=; b=F1MH7TBBiRGHdaqHzlK6D12bDIiQAHF0uHKu1Hv4YbG9mMRKQUFqn9eSMFaEwoEe3eW9TC CMnf28ypO+rSv+N8Umc2YFQMATofaczx4OcTbiqg6wDu7YQZkvNKVr2FdR6Xgw6FtaRU2f iboU+HPvu52gi5GqgB/HLXOXOh39iWM= Received: from mx-prod-mc-05.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-378-m6gSHsSSPfSqqTCewrfj1w-1; Wed, 20 May 2026 11:18:31 -0400 X-MC-Unique: m6gSHsSSPfSqqTCewrfj1w-1 X-Mimecast-MFC-AGG-ID: m6gSHsSSPfSqqTCewrfj1w_1779290310 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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0D6B91956080; Wed, 20 May 2026 15:18:30 +0000 (UTC) Received: from lenovo-t14s.redhat.corp (headnet01.pony-001.prod.iad2.dc.redhat.com [10.2.32.101]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C51DE30001A2; Wed, 20 May 2026 15:18:28 +0000 (UTC) From: Laurent Vivier To: passt-dev@passt.top Subject: [PATCH v7 2/4] tcp_vu: Build headers on the stack and write them into the iovec Date: Wed, 20 May 2026 17:18:25 +0200 Message-ID: <20260520151827.496828-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: 0tI7uq7TKqJicA162uPUSSthRi4hA40_oHQ9vCr7VmQ_1779290310 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: 7Q7OE7E4B7L7WVQKZ4XACC6EVA2DE4SU X-Message-ID-Hash: 7Q7OE7E4B7L7WVQKZ4XACC6EVA2DE4SU 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 , Jon Maloy , David Gibson 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_prepare() currently assumes the first iovec element provided by the guest is large enough to hold all L2-L4 headers, and builds them in place via pointer casts into iov[0].iov_base. This assumption is enforced by an assert(). Since the headers in the buffer are uninitialized anyway, we can just as well build the Ethernet, IP, and TCP headers on the stack instead, and write them into the iovec with IOV_PUSH_HEADER(). This mirrors the approach already used in udp_vu_prepare(), and prepares for support of elements with multiple iovecs. Signed-off-by: Laurent Vivier Reviewed-by: Jon Maloy Reviewed-by: David Gibson --- tcp_vu.c | 64 +++++++++++++++++++++++++------------------------------- 1 file changed, 28 insertions(+), 36 deletions(-) diff --git a/tcp_vu.c b/tcp_vu.c index 65e7dfe63437..c13c45f71f3b 100644 --- a/tcp_vu.c +++ b/tcp_vu.c @@ -298,49 +298,41 @@ static void tcp_vu_prepare(const struct ctx *c, struct tcp_tap_conn *conn, bool v6 = !(inany_v4(&toside->eaddr) && inany_v4(&toside->oaddr)); size_t hdrlen = tcp_vu_hdrlen(v6); struct iov_tail payload = IOV_TAIL(iov, iov_cnt, hdrlen); - char *base = iov[0].iov_base; - struct ipv6hdr *ip6h = NULL; - struct iphdr *ip4h = NULL; - struct tcphdr *th; - struct ethhdr *eh; - - /* we guess the first iovec provided by the guest can embed - * all the headers needed by L2 frame, including any padding - */ - assert(iov[0].iov_len >= hdrlen); - - eh = vu_eth(base); - - memcpy(eh->h_dest, c->guest_mac, sizeof(eh->h_dest)); + struct tcphdr th = { + .doff = sizeof(th) / 4, + .ack = 1, + .psh = push, + }; + struct iov_tail l2frame; + struct ipv6hdr ip6h; + struct iphdr ip4h; + struct ethhdr eh; + + memcpy(eh.h_dest, c->guest_mac, sizeof(eh.h_dest)); /* initialize header */ - if (!v6) { - eh->h_proto = htons(ETH_P_IP); - - ip4h = vu_ip(base); - *ip4h = (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_TCP); - th = vu_payloadv4(base); - } else { - eh->h_proto = htons(ETH_P_IPV6); - - ip6h = vu_ip(base); - *ip6h = (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_TCP); + if (!v6) + ip4h = (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_TCP); + else + ip6h = (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_TCP); - th = vu_payloadv6(base); - } + tcp_fill_headers(c, conn, &eh, v6 ? NULL : &ip4h, v6 ? &ip6h : NULL, &th, + &payload, dlen, *csum_flags, conn->seq_to_tap); - memset(th, 0, sizeof(*th)); - th->doff = sizeof(*th) / 4; - th->ack = 1; - th->psh = push; + /* Preserve TCP_CSUM, overwrite IP4_CSUM as we set the checksum */ + if (!v6) + *csum_flags = (*csum_flags & TCP_CSUM) | ip4h.check; - tcp_fill_headers(c, conn, eh, ip4h, ip6h, th, &payload, dlen, - *csum_flags, conn->seq_to_tap); + /* write headers */ + l2frame = IOV_TAIL(iov, iov_cnt, VNET_HLEN); - /* Preserve TCP_CSUM, overwrite IP4_CSUM as we set the checksum */ - if (ip4h) - *csum_flags = (*csum_flags & TCP_CSUM) | ip4h->check; + IOV_PUSH_HEADER(&l2frame, eh); + if (!v6) + IOV_PUSH_HEADER(&l2frame, ip4h); + else + IOV_PUSH_HEADER(&l2frame, ip6h); + IOV_PUSH_HEADER(&l2frame, th); } /** -- 2.54.0