[PATCH bpf-next v2 11/13] selftests/bpf: add network helpers to generate udp checksums

Alexis Lothoré (eBPF Foundation) posted 13 patches 1 week, 1 day ago
There is a newer version of this series
[PATCH bpf-next v2 11/13] selftests/bpf: add network helpers to generate udp checksums
Posted by Alexis Lothoré (eBPF Foundation) 1 week, 1 day ago
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) <alexis.lothore@bootlin.com>
---
Changes in v2:
- new patch
---
 tools/testing/selftests/bpf/network_helpers.h | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h
index 6d1ae56080c56a65c437899c32566f0e4c496c33..fa82269f7a169a518ba210fa8641eba02f262333 100644
--- a/tools/testing/selftests/bpf/network_helpers.h
+++ b/tools/testing/selftests/bpf/network_helpers.h
@@ -161,6 +161,33 @@ build_ipv6_pseudo_header_csum(const struct in6_addr *saddr,
 	return csum_fold((__u32)s);
 }
 
+static inline __sum16 build_udp_v4_csum(const struct iphdr *iph, __u8 l4_proto,
+					__u16 l4_len, const void *l4_start,
+					int num_words)
+{
+	unsigned long pseudo_sum;
+	int num_u16 = sizeof(iph->saddr); /* halfwords: twice byte len */
+
+	pseudo_sum = add_csum_hword((void *)&iph->saddr, num_u16);
+	pseudo_sum += htons(l4_proto);
+	pseudo_sum += l4_len;
+	pseudo_sum += add_csum_hword(l4_start, num_words);
+	return csum_fold(pseudo_sum);
+}
+
+static inline __sum16 build_udp_v6_csum(const struct ipv6hdr *ip6h,
+					const void *l4_start, int num_words)
+{
+	unsigned long pseudo_sum;
+	int num_u16 = sizeof(ip6h->saddr); /* halfwords: twice byte len */
+
+	pseudo_sum = add_csum_hword((void *)&ip6h->saddr, num_u16);
+	pseudo_sum += htons(ip6h->nexthdr);
+	pseudo_sum += ip6h->payload_len;
+	pseudo_sum += add_csum_hword(l4_start, num_words);
+	return csum_fold(pseudo_sum);
+}
+
 struct tmonitor_ctx;
 
 #ifdef TRAFFIC_MONITOR

-- 
2.47.0

Re: [PATCH bpf-next v2 11/13] selftests/bpf: add network helpers to generate udp checksums
Posted by Stanislav Fomichev 1 week ago
On 11/14, Alexis Lothoré (eBPF Foundation) wrote:
> 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) <alexis.lothore@bootlin.com>
> ---
> Changes in v2:
> - new patch
> ---
>  tools/testing/selftests/bpf/network_helpers.h | 27 +++++++++++++++++++++++++++
>  1 file changed, 27 insertions(+)
> 
> diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h
> index 6d1ae56080c56a65c437899c32566f0e4c496c33..fa82269f7a169a518ba210fa8641eba02f262333 100644
> --- a/tools/testing/selftests/bpf/network_helpers.h
> +++ b/tools/testing/selftests/bpf/network_helpers.h
> @@ -161,6 +161,33 @@ build_ipv6_pseudo_header_csum(const struct in6_addr *saddr,
>  	return csum_fold((__u32)s);
>  }
>  
> +static inline __sum16 build_udp_v4_csum(const struct iphdr *iph, __u8 l4_proto,
> +					__u16 l4_len, const void *l4_start,
> +					int num_words)
> +{
> +	unsigned long pseudo_sum;
> +	int num_u16 = sizeof(iph->saddr); /* halfwords: twice byte len */
> +
> +	pseudo_sum = add_csum_hword((void *)&iph->saddr, num_u16);
> +	pseudo_sum += htons(l4_proto);
> +	pseudo_sum += l4_len;
> +	pseudo_sum += add_csum_hword(l4_start, num_words);
> +	return csum_fold(pseudo_sum);

I was expecting to see a call to csum_tcpudp_magic here. And csum_ipv6_magic
down below. These build pseudo header csum, so no need to manually do it
again.

> +}
> +
> +static inline __sum16 build_udp_v6_csum(const struct ipv6hdr *ip6h,
> +					const void *l4_start, int num_words)
> +{
> +	unsigned long pseudo_sum;
> +	int num_u16 = sizeof(ip6h->saddr); /* halfwords: twice byte len */
> +
> +	pseudo_sum = add_csum_hword((void *)&ip6h->saddr, num_u16);
> +	pseudo_sum += htons(ip6h->nexthdr);
> +	pseudo_sum += ip6h->payload_len;
> +	pseudo_sum += add_csum_hword(l4_start, num_words);
> +	return csum_fold(pseudo_sum);
> +}
> +
>  struct tmonitor_ctx;
>  
>  #ifdef TRAFFIC_MONITOR
> 
> -- 
> 2.47.0
> 
Re: [PATCH bpf-next v2 11/13] selftests/bpf: add network helpers to generate udp checksums
Posted by Alexis Lothoré 3 days, 17 hours ago
On 11/15/24 16:54, Stanislav Fomichev wrote:
> On 11/14, Alexis Lothoré (eBPF Foundation) wrote:
>> +static inline __sum16 build_udp_v4_csum(const struct iphdr *iph, __u8 l4_proto,
>> +					__u16 l4_len, const void *l4_start,
>> +					int num_words)
>> +{
>> +	unsigned long pseudo_sum;
>> +	int num_u16 = sizeof(iph->saddr); /* halfwords: twice byte len */
>> +
>> +	pseudo_sum = add_csum_hword((void *)&iph->saddr, num_u16);
>> +	pseudo_sum += htons(l4_proto);
>> +	pseudo_sum += l4_len;
>> +	pseudo_sum += add_csum_hword(l4_start, num_words);
>> +	return csum_fold(pseudo_sum);
> 
> I was expecting to see a call to csum_tcpudp_magic here. And csum_ipv6_magic
> down below. These build pseudo header csum, so no need to manually do it
> again.

I initially tried to fit csum_tcpudp_magic here and did not manage to make a
valid UDP checksum, but after more attempts, it looks like I had a
misunderstanding this checksum computation. I am now able to used
csum_tcpudp_magic in build_udp_v4_csum, it will be fixed in the next revision :)

Thanks,

Alexis

-- 
Alexis Lothoré, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com