From patchwork Wed Nov 20 07:43:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= X-Patchwork-Id: 13880815 Received: from relay1-d.mail.gandi.net (relay1-d.mail.gandi.net [217.70.183.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BF5891A0731; Wed, 20 Nov 2024 07:43:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.193 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732088626; cv=none; b=atn7b7EwlM7InlsbrUZtbTwA8uF2tyhMr2ZxzXlHV7N7RCs77dwi5c+NpoAnqQUi6qFEiBo5H/4+yz97z+Kbl33t6PGxHEEegELgBDE7YPYzB6zxJAwz+v5coGFm2V/YtSjJXU9pGGPYN5kE5ACxbohnGSZ2bBWHfjz+Z+HSD9A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732088626; c=relaxed/simple; bh=FVYhgVeURP8mF5EiKITLrPtuzr/WORCsEsePlgvAQWI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=YbRjpeX3x+Moq/YQK/GJ+8l57qH0vBDaKuLgRRTn6Ux3jfTUyWixYPL19gpFtH1REMChFIKSbp7OYaQ9J0hu9iempo949SAtYoMb39ZfeuCw3rZ2AMLZQbbGvsI+CZtrvUul9UEV/JgmGawZuOYhyycEmXlEIDftasKCek687Ow= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=RiK4oBMf; arc=none smtp.client-ip=217.70.183.193 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="RiK4oBMf" Received: by mail.gandi.net (Postfix) with ESMTPSA id C7534240005; Wed, 20 Nov 2024 07:43:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1732088622; 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=J4tsdzmcSvpGE0kiX9FqApjzjA1QKlCC/JxJ6336bXg=; b=RiK4oBMf3RK/SaX3msxqKgkxzugwjKE+ov1Ixi5EmRoA+3EFumjobCUE4AE2awnaMu7Ap6 uE+kvKXUlFIbApJcIRI9Dm6e6ljW5tLKihqQ713O20RdDOBvLt3m4rEEfc3qKCI9+Q823r P2p1lAyV1I78SIefEduL8vfByDfgu4NUDe3sndJ64rwqOhzZ7TOCy+5AUUT3zZZNvjW/tV Nn1XCJ8NOab+J7mmoJY1qawr8O2rsSL9rYm3Dst3jBLNW3HnDgWH7Dm4gtyv9O/O6/VpZt 9PAuv0492WAatAvzlnrkvYswNTVtQ7Off/70LDF1MnroxvNdk6OibqBi2TO4zA== From: =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= Date: Wed, 20 Nov 2024 08:43:22 +0100 Subject: [PATCH bpf-next v3 12/14] selftests/bpf: add network helpers to generate udp checksums Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241120-flow_dissector-v3-12-45b46494f937@bootlin.com> References: <20241120-flow_dissector-v3-0-45b46494f937@bootlin.com> In-Reply-To: <20241120-flow_dissector-v3-0-45b46494f937@bootlin.com> To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer Cc: ebpf@linuxfoundation.org, Thomas Petazzoni , Bastien Curutchet , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation?= =?utf-8?q?=29?= X-Mailer: b4 0.14.2 X-GND-Sasl: alexis.lothore@bootlin.com network_helpers.c provides some helpers to generate ip checksums or ip pseudo-header checksums, but not for upper layers (eg: udp checksums) Add helpers for udp checksum to allow manually building udp packets. Signed-off-by: Alexis Lothoré (eBPF Foundation) Acked-by: Stanislav Fomichev --- Changes in v3: - reuse pseudo-header checksum helper in the new helpers - reduce parameters list (eg: it does not make sense to passe the l4 protocol in a udp-specific helper) Changes in v2: - new patch --- tools/testing/selftests/bpf/network_helpers.h | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 6ca636643ea2b77fb5cc459b03bf7f8c4f749377..479f678af191e9fa8a7129e050a25bf9ed0cf37e 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -13,6 +13,7 @@ typedef __u16 __sum16; #include #include #include +#include #include #include @@ -192,6 +193,47 @@ static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr, return csum_fold((__u32)s); } +/** + * build_udp_v4_csum - compute UDP checksum for UDP over IPv4 + * + * Compute the checksum to embed in UDP header, composed of the sum of IP + * pseudo-header checksum, UDP header checksum and UDP data checksum + * @iph IP header + * @udph UDP header, which must be immediately followed by UDP data + * + * Returns the total checksum + */ + +static inline __sum16 build_udp_v4_csum(const struct iphdr *iph, + const struct udphdr *udph) +{ + unsigned long sum; + + sum = csum_partial(udph, ntohs(udph->len), 0); + return csum_tcpudp_magic(iph->saddr, iph->daddr, ntohs(udph->len), + IPPROTO_UDP, sum); +} + +/** + * build_udp_v6_csum - compute UDP checksum for UDP over IPv6 + * + * Compute the checksum to embed in UDP header, composed of the sum of IPv6 + * pseudo-header checksum, UDP header checksum and UDP data checksum + * @ip6h IPv6 header + * @udph UDP header, which must be immediately followed by UDP data + * + * Returns the total checksum + */ +static inline __sum16 build_udp_v6_csum(const struct ipv6hdr *ip6h, + const struct udphdr *udph) +{ + unsigned long sum; + + sum = csum_partial(udph, ntohs(udph->len), 0); + return csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, ntohs(udph->len), + IPPROTO_UDP, sum); +} + struct tmonitor_ctx; #ifdef TRAFFIC_MONITOR