* [PATCH net-next v3] selftests: net: csum: filter packets by source address and port
@ 2026-09-08 15:54 Willem de Bruijn
2026-09-09 16:57 ` Simon Horman
2026-09-09 20:20 ` patchwork-bot+netdevbpf
0 siblings, 2 replies; 3+ messages in thread
From: Willem de Bruijn @ 2026-09-08 15:54 UTC (permalink / raw)
To: netdev
Cc: davem, kuba, edumazet, pabeni, horms, andrew+netdev,
Willem de Bruijn
From: Willem de Bruijn <willemb@google.com>
Add source address and port verification in recv_verify_packet_* and
recv_udp: non-matching packets are skipped.
The receiver reads from PF_PACKET and UDP sockets to verify incoming
packets to test hardware checksum offload. It did not validate the
packet source address or source port.
During tests expecting an invalid checksum (-E) or zero checksum (-Z),
background packets can fail the test.
For -Z, build_packet_udp chooses a specific source port that causes
the checksum to sum to zero. When running in receive-only mode (-R),
call build_packet in do_rx to compute the expected source port so
that source port filtering can be applied to -Z as well.
Also
- add an inter-packet delay to avoid drops from bursts.
- remove a comment that is no longer correct.
Fixes: 91a7de85600d ("selftests/net: add csum offload test")
Signed-off-by: Willem de Bruijn <willemb@google.com>
---
Changes
v2 -> v3
- also fix same issue for zero checksum:
calculate expected src_port by calling build_packet on rx side
v1 -> v2
- also check udp socket, with recvfrom
v2: https://lore.kernel.org/netdev/20260904030624.3833721-1-willemdebruijn.kernel@gmail.com/
v1: https://lore.kernel.org/netdev/20260831210128.1359978-1-willemdebruijn.kernel@gmail.com/
---
tools/testing/selftests/net/lib/csum.c | 55 +++++++++++++++++++++++---
1 file changed, 50 insertions(+), 5 deletions(-)
diff --git a/tools/testing/selftests/net/lib/csum.c b/tools/testing/selftests/net/lib/csum.c
index e28884ce3ab3..ad960e07e31b 100644
--- a/tools/testing/selftests/net/lib/csum.c
+++ b/tools/testing/selftests/net/lib/csum.c
@@ -2,8 +2,7 @@
/* Test hardware checksum offload: Rx + Tx, IPv4 + IPv6, TCP + UDP.
*
- * The test runs on two machines to exercise the NIC. For this reason it
- * is not integrated in kselftests.
+ * The test runs on two machines to exercise the NIC.
*
* CMD=$((./csum -[46] -[tu] -S $SADDR -D $DADDR -[RT] -r 1 $EXTRA_ARGS))
*
@@ -236,7 +235,8 @@ static void *build_packet_udp(void *_uh)
uh->source = 0;
uh->source = checksum(uh, IPPROTO_UDP, sizeof(*uh) + cfg_payload_len);
- fprintf(stderr, "tx: changing sport: %hu -> %hu\n",
+ fprintf(stderr, "%s: changing sport: %hu -> %hu\n",
+ cfg_do_tx ? "tx" : "rx",
cfg_port_src, ntohs(uh->source));
cfg_port_src = ntohs(uh->source);
}
@@ -249,7 +249,8 @@ static void *build_packet_udp(void *_uh)
if (cfg_bad_csum)
uh->check = ~uh->check;
- fprintf(stderr, "tx: sending checksum: 0x%x\n", uh->check);
+ if (cfg_do_tx)
+ fprintf(stderr, "tx: sending checksum: 0x%x\n", uh->check);
return uh + 1;
}
@@ -571,15 +572,36 @@ static int recv_prepare_packet(void)
static int recv_udp(int fd)
{
static char buf[MAX_PAYLOAD_LEN];
+ struct sockaddr_storage addr;
+ socklen_t addrlen;
int ret, count = 0;
while (1) {
- ret = recv(fd, buf, sizeof(buf), MSG_DONTWAIT);
+ addrlen = sizeof(addr);
+ ret = recvfrom(fd, buf, sizeof(buf), MSG_DONTWAIT,
+ (struct sockaddr *)&addr, &addrlen);
if (ret == -1 && errno == EAGAIN)
break;
if (ret == -1)
error(1, errno, "recv r");
+ if (cfg_family == PF_INET) {
+ struct sockaddr_in *sin = (void *)&addr;
+
+ if (sin->sin_addr.s_addr != cfg_saddr4.sin_addr.s_addr)
+ continue;
+ if (sin->sin_port != htons(cfg_port_src))
+ continue;
+ } else {
+ struct sockaddr_in6 *sin6 = (void *)&addr;
+
+ if (memcmp(&sin6->sin6_addr, &cfg_saddr6.sin6_addr,
+ sizeof(sin6->sin6_addr)))
+ continue;
+ if (sin6->sin6_port != htons(cfg_port_src))
+ continue;
+ }
+
fprintf(stderr, "rx: udp: len=%u\n", ret);
count++;
}
@@ -620,6 +642,9 @@ static int recv_verify_packet_tcp(void *th, int len)
if (len < sizeof(*tcph) || tcph->dest != htons(cfg_port_dst))
return -1;
+ if (tcph->source != htons(cfg_port_src))
+ return -1;
+
return recv_verify_csum(th, len, ntohs(tcph->source), tcph->check);
}
@@ -647,6 +672,9 @@ static int recv_verify_packet_udp(void *th, int len)
return recv_verify_packet_udp_encap(udph + 1,
len - sizeof(*udph));
+ if (udph->source != htons(cfg_port_src))
+ return -1;
+
return recv_verify_csum(th, len, ntohs(udph->source), udph->check);
}
@@ -659,6 +687,9 @@ static int recv_verify_packet_ipv4(void *nh, int len)
if (len < sizeof(*iph) || iph->protocol != proto)
return -1;
+ if (iph->saddr != cfg_saddr4.sin_addr.s_addr)
+ return -1;
+
ip_len = ntohs(iph->tot_len);
if (ip_len > len || ip_len < sizeof(*iph))
return -1;
@@ -680,6 +711,9 @@ static int recv_verify_packet_ipv6(void *nh, int len)
if (len < sizeof(*ip6h) || ip6h->nexthdr != proto)
return -1;
+ if (memcmp(&ip6h->saddr, &cfg_saddr6.sin6_addr, sizeof(ip6h->saddr)))
+ return -1;
+
payload_len = ntohs(ip6h->payload_len);
if (payload_len > len - sizeof(*ip6h))
return -1;
@@ -940,6 +974,9 @@ static void do_tx(void)
cfg_payload_len = rand() % MAX_PAYLOAD_LEN;
buf = build_packet(_buf, sizeof(_buf), &len);
}
+
+ /* avoid bursting */
+ usleep(20);
}
if (close(fd))
@@ -952,6 +989,14 @@ static void do_rx(int fdp, int fdr)
long tleft, tstop;
struct pollfd pfd;
+ if (!cfg_do_tx && cfg_zero_sum) {
+ static char _buf[MAX_HEADER_LEN + MAX_PAYLOAD_LEN];
+ int len;
+
+ /* calculate source port that causes csum to add up to zero */
+ build_packet(_buf, sizeof(_buf), &len);
+ }
+
tstop = gettimeofday_ms() + cfg_timeout_ms;
tleft = cfg_timeout_ms;
--
2.55.0.979.g7e5102b832-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH net-next v3] selftests: net: csum: filter packets by source address and port
2026-09-08 15:54 [PATCH net-next v3] selftests: net: csum: filter packets by source address and port Willem de Bruijn
@ 2026-09-09 16:57 ` Simon Horman
2026-09-09 20:20 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 3+ messages in thread
From: Simon Horman @ 2026-09-09 16:57 UTC (permalink / raw)
To: Willem de Bruijn
Cc: netdev, davem, kuba, edumazet, pabeni, andrew+netdev,
Willem de Bruijn
On Tue, Sep 08, 2026 at 11:54:29AM -0400, Willem de Bruijn wrote:
> From: Willem de Bruijn <willemb@google.com>
>
> Add source address and port verification in recv_verify_packet_* and
> recv_udp: non-matching packets are skipped.
>
> The receiver reads from PF_PACKET and UDP sockets to verify incoming
> packets to test hardware checksum offload. It did not validate the
> packet source address or source port.
>
> During tests expecting an invalid checksum (-E) or zero checksum (-Z),
> background packets can fail the test.
>
> For -Z, build_packet_udp chooses a specific source port that causes
> the checksum to sum to zero. When running in receive-only mode (-R),
> call build_packet in do_rx to compute the expected source port so
> that source port filtering can be applied to -Z as well.
>
> Also
> - add an inter-packet delay to avoid drops from bursts.
> - remove a comment that is no longer correct.
>
> Fixes: 91a7de85600d ("selftests/net: add csum offload test")
> Signed-off-by: Willem de Bruijn <willemb@google.com>
>
> ---
>
> Changes
> v2 -> v3
> - also fix same issue for zero checksum:
> calculate expected src_port by calling build_packet on rx side
> v1 -> v2
> - also check udp socket, with recvfrom
>
> v2: https://lore.kernel.org/netdev/20260904030624.3833721-1-willemdebruijn.kernel@gmail.com/
> v1: https://lore.kernel.org/netdev/20260831210128.1359978-1-willemdebruijn.kernel@gmail.com/
Reviewed-by: Simon Horman <horms@kernel.org>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH net-next v3] selftests: net: csum: filter packets by source address and port
2026-09-08 15:54 [PATCH net-next v3] selftests: net: csum: filter packets by source address and port Willem de Bruijn
2026-09-09 16:57 ` Simon Horman
@ 2026-09-09 20:20 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-09-09 20:20 UTC (permalink / raw)
To: Willem de Bruijn
Cc: netdev, davem, kuba, edumazet, pabeni, horms, andrew+netdev,
willemb
Hello:
This patch was applied to netdev/net-next.git (main)
by Jakub Kicinski <kuba@kernel.org>:
On Tue, 8 Sep 2026 11:54:29 -0400 you wrote:
> From: Willem de Bruijn <willemb@google.com>
>
> Add source address and port verification in recv_verify_packet_* and
> recv_udp: non-matching packets are skipped.
>
> The receiver reads from PF_PACKET and UDP sockets to verify incoming
> packets to test hardware checksum offload. It did not validate the
> packet source address or source port.
>
> [...]
Here is the summary with links:
- [net-next,v3] selftests: net: csum: filter packets by source address and port
https://git.kernel.org/netdev/net-next/c/8342c68435dd
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-09-09 20:21 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-08 15:54 [PATCH net-next v3] selftests: net: csum: filter packets by source address and port Willem de Bruijn
2026-09-09 16:57 ` Simon Horman
2026-09-09 20:20 ` patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox