From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=none 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=asEMvGqV; 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 ESMTP id 35CF95A061A for ; Tue, 19 Nov 2024 16:10:59 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1732029058; 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=B255FhO4I9j/wXsWnqB5tU3ECgHrEY6gK3y94J/lSnw=; b=asEMvGqVmSkNo4Bi531XHtWKZR/P6FdwPM32YZ4LF1fbWmkQdSUN6IBxOl3frdEdkuI9a4 B9vFj02oSBTIH20xXhySDZuURvbJgxFhXyqMxeMh0qFEueFkZJqVZYGXlUQKe52VT16qVq gDKSBettuQnNjpScoGtmS2cyMyvmcuY= Received: from mx-prod-mc-04.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-359-K2Z0SmN4NSywA6Vxc4QALA-1; Tue, 19 Nov 2024 10:10:56 -0500 X-MC-Unique: K2Z0SmN4NSywA6Vxc4QALA-1 X-Mimecast-MFC-AGG-ID: K2Z0SmN4NSywA6Vxc4QALA Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0DF121955DC3; Tue, 19 Nov 2024 15:10:56 +0000 (UTC) Received: from lenovo-t14s.redhat.com (unknown [10.39.192.150]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id DB66719560A3; Tue, 19 Nov 2024 15:10:54 +0000 (UTC) From: Laurent Vivier To: passt-dev@passt.top Subject: [PATCH v13 09/10] tcp_vu: Share more header construction between IPv4 and IPv6 paths Date: Tue, 19 Nov 2024 16:10:35 +0100 Message-ID: <20241119151037.2196652-10-lvivier@redhat.com> In-Reply-To: <20241119151037.2196652-1-lvivier@redhat.com> References: <20241119151037.2196652-1-lvivier@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: FlDrWQzjCxfX01Pr0Xd6St4cGhwIe7mbakfmc99XtiY_1732029056 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: TBUMGGV6ZFIDZY423ECZE4QY2IHY7C3N X-Message-ID-Hash: TBUMGGV6ZFIDZY423ECZE4QY2IHY7C3N 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: David Gibson , 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: From: David Gibson tcp_vu_send_flag() and tcp_vu_prepare() both needs to do some different things for IPv4 vs. IPv6. However the two paths have a number of lines of duplicated code. We can share those at the expense of an additional conditional (which we might be able to simplify again later). Signed-off-by: David Gibson Signed-off-by: Laurent Vivier --- tcp_vu.c | 91 +++++++++++++++++++++++--------------------------------- 1 file changed, 38 insertions(+), 53 deletions(-) diff --git a/tcp_vu.c b/tcp_vu.c index 7240f83dcccc..91b7b306c2ad 100644 --- a/tcp_vu.c +++ b/tcp_vu.c @@ -32,6 +32,7 @@ #include "tcp_internal.h" #include "checksum.h" #include "vu_common.h" +#include static struct iovec iov_vu[VIRTQUEUE_MAX_SIZE + 1]; static struct vu_virtq_element elem[VIRTQUEUE_MAX_SIZE]; @@ -96,8 +97,12 @@ int tcp_vu_send_flag(const struct ctx *c, struct tcp_tap_conn *conn, int flags) const struct flowside *tapside = TAPFLOW(conn); size_t l2len, l4len, optlen, hdrlen; struct vu_virtq_element flags_elem[2]; + struct tcp_payload_t *payload; + struct ipv6hdr *ip6h = NULL; struct iovec flags_iov[2]; + struct iphdr *iph = NULL; struct ethhdr *eh; + uint32_t seq; int elem_cnt; int nb_ack; int ret; @@ -119,56 +124,38 @@ int tcp_vu_send_flag(const struct ctx *c, struct tcp_tap_conn *conn, int flags) memcpy(eh->h_source, c->our_tap_mac, sizeof(eh->h_source)); if (CONN_V4(conn)) { - struct tcp_payload_t *payload; - struct iphdr *iph; - uint32_t seq; - eh->h_proto = htons(ETH_P_IP); iph = vu_ip(flags_elem[0].in_sg[0].iov_base); *iph = (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_TCP); payload = vu_payloadv4(flags_elem[0].in_sg[0].iov_base); - memset(&payload->th, 0, sizeof(payload->th)); - payload->th.doff = offsetof(struct tcp_payload_t, data) / 4; - payload->th.ack = 1; - - seq = conn->seq_to_tap; - ret = tcp_prepare_flags(c, conn, flags, &payload->th, - (struct tcp_syn_opts *)payload->data, - &optlen); - if (ret <= 0) { - vu_queue_rewind(vq, 1); - return ret; - } - - l4len = tcp_fill_headers4(conn, NULL, iph, payload, optlen, - NULL, seq, true); - l2len = sizeof(*iph); } else { - struct tcp_payload_t *payload; - struct ipv6hdr *ip6h; - uint32_t seq; - 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); - payload = vu_payloadv6(flags_elem[0].in_sg[0].iov_base); - memset(&payload->th, 0, sizeof(payload->th)); - payload->th.doff = offsetof(struct tcp_payload_t, data) / 4; - payload->th.ack = 1; - - seq = conn->seq_to_tap; - ret = tcp_prepare_flags(c, conn, flags, &payload->th, - (struct tcp_syn_opts *)payload->data, - &optlen); - if (ret <= 0) { - vu_queue_rewind(vq, 1); - return ret; - } + } + + memset(&payload->th, 0, sizeof(payload->th)); + payload->th.doff = offsetof(struct tcp_payload_t, data) / 4; + payload->th.ack = 1; + + seq = conn->seq_to_tap; + ret = tcp_prepare_flags(c, conn, flags, &payload->th, + (struct tcp_syn_opts *)payload->data, + &optlen); + if (ret <= 0) { + vu_queue_rewind(vq, 1); + return ret; + } + if (CONN_V4(conn)) { + l4len = tcp_fill_headers4(conn, NULL, iph, payload, optlen, + NULL, seq, true); + l2len = sizeof(*iph); + } else { l4len = tcp_fill_headers6(conn, NULL, ip6h, payload, optlen, seq, true); l2len = sizeof(*ip6h); @@ -301,7 +288,10 @@ static void tcp_vu_prepare(const struct ctx *c, size_t dlen, const uint16_t **check) { const struct flowside *toside = TAPFLOW(conn); + struct tcp_payload_t *payload; char *base = first->iov_base; + struct ipv6hdr *ip6h = NULL; + struct iphdr *iph = NULL; struct ethhdr *eh; /* we guess the first iovec provided by the guest can embed @@ -314,10 +304,8 @@ static void tcp_vu_prepare(const struct ctx *c, memcpy(eh->h_source, c->our_tap_mac, sizeof(eh->h_source)); /* initialize header */ - if (inany_v4(&toside->eaddr) && inany_v4(&toside->oaddr)) { - struct tcp_payload_t *payload; - struct iphdr *iph; + if (inany_v4(&toside->eaddr) && inany_v4(&toside->oaddr)) { ASSERT(first[0].iov_len >= tcp_vu_hdrlen(false)); eh->h_proto = htons(ETH_P_IP); @@ -325,17 +313,7 @@ static void tcp_vu_prepare(const struct ctx *c, iph = vu_ip(base); *iph = (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_TCP); payload = vu_payloadv4(base); - memset(&payload->th, 0, sizeof(payload->th)); - payload->th.doff = offsetof(struct tcp_payload_t, data) / 4; - payload->th.ack = 1; - - tcp_fill_headers4(conn, NULL, iph, payload, dlen, - *check, conn->seq_to_tap, true); - *check = &iph->check; } else { - struct tcp_payload_t *payload; - struct ipv6hdr *ip6h; - ASSERT(first[0].iov_len >= tcp_vu_hdrlen(true)); eh->h_proto = htons(ETH_P_IPV6); @@ -344,10 +322,17 @@ static void tcp_vu_prepare(const struct ctx *c, *ip6h = (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_TCP); payload = vu_payloadv6(base); - memset(&payload->th, 0, sizeof(payload->th)); - payload->th.doff = offsetof(struct tcp_payload_t, data) / 4; - payload->th.ack = 1; + } + + memset(&payload->th, 0, sizeof(payload->th)); + payload->th.doff = offsetof(struct tcp_payload_t, data) / 4; + payload->th.ack = 1; + if (inany_v4(&toside->eaddr) && inany_v4(&toside->oaddr)) { + tcp_fill_headers4(conn, NULL, iph, payload, dlen, + *check, conn->seq_to_tap, true); + *check = &iph->check; + } else { tcp_fill_headers6(conn, NULL, ip6h, payload, dlen, conn->seq_to_tap, true); } -- 2.47.0