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=SKngkte3; 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 8B49C5A0622 for ; Wed, 13 Nov 2024 09:04:34 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1731485073; 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=UY3Arm1E0fk7Eo9Qia3awCEP0mx9vmyRT8bRB2TVRt0=; b=SKngkte3ijOuBgstOX5ulkHoXGakXx7yHDKQeelIQMHeoHWY3biNBi4P5HnK28x9zs/009 ICMTdoqFFIjp7KMJ++R9t+mmE+7YVXqOVIN4jFYSrABNNXC8VGJEWQukNZWouDT1HcGyQ/ kqb4jt27fycPKT3NZ8s/M3Eao2d3/kA= Received: from mx-prod-mc-01.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-169-Xb2UeaBANcWLJjCKELk5gw-1; Wed, 13 Nov 2024 03:04:31 -0500 X-MC-Unique: Xb2UeaBANcWLJjCKELk5gw-1 X-Mimecast-MFC-AGG-ID: Xb2UeaBANcWLJjCKELk5gw Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id F04B61954B22; Wed, 13 Nov 2024 08:04:30 +0000 (UTC) Received: from lenovo-t14s.redhat.com (unknown [10.39.192.187]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 14D3019560A3; Wed, 13 Nov 2024 08:04:28 +0000 (UTC) From: Laurent Vivier To: passt-dev@passt.top Subject: [PATCH v11 09/10] tcp_vu: Share more header construction between IPv4 and IPv6 paths Date: Wed, 13 Nov 2024 09:04:08 +0100 Message-ID: <20241113080411.476646-10-lvivier@redhat.com> In-Reply-To: <20241113080411.476646-1-lvivier@redhat.com> References: <20241113080411.476646-1-lvivier@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: LyBQZC-vm6OQoYjUBYfiCxaLyTG-NJS9DOmjhSW4y10_1731485071 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: P4UY5FZEKFJWTL65GKRPWOC3YRU63NN2 X-Message-ID-Hash: P4UY5FZEKFJWTL65GKRPWOC3YRU63NN2 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 04a10b5ef8c6..ae4dab74d254 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