Skip to contentSkip to conversation

selftests/rdma: Add an eBPF-based testcase using netkit for RXE

PATCH Superseded Zhu Yanjun 2 participants 5 messages Oct 19

Subsystems (3)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Zhu Yanjun

author
#0 lore raw

v2 cover selftests/rdma: Add an eBPF-based testcase using netkit for RXE

Add a new self-test case to verify RXE (Soft-RoCE) traffic using netkit virtual interfaces and an eBPF tcx ingress program to dump and parse RDMA transport headers (BTH, etc.).

The test results:

# make -C tools/testing/selftests/ TARGETS=rdma run_tests
make: Entering directory '/root/linux/tools/testing/selftests'
clang -g -O2 -target bpf -D__TARGET_ARCH_x86 -I../../../../usr/include -I../../../../include/uapi -I../bpf -I/root/linux/tools/testing/selftests/rdma -c rxe_pkg_kernel_dump.c -o /root/linux/tools/testing/selftests/rdma/rxe_pkg_kernel_dump.o
bpftool gen skeleton /root/linux/tools/testing/selftests/rdma/rxe_pkg_kernel_dump.o > /root/linux/tools/testing/selftests/rdma/rxe_pkg_kernel.skel.h
gcc -D_GNU_SOURCE= -I/root/linux/tools/testing/selftests/../../../tools/testing/selftests  -g -Wall -I../../../../usr/include -I../../../../include/uapi -I../bpf -I/root/linux/tools/testing/selftests/rdma rxe_pkg_user_dump.c  -lbpf -lelf -lz -o /root/linux/tools/testing/selftests/rdma/rxe_netkit_dump
# timeout set to 45
# selftests: rdma: rxe_rping_between_netns.sh
# client DISCONNECT EVENT...
ok 1 selftests: rdma: rxe_rping_between_netns.sh
# timeout set to 45
# selftests: rdma: rxe_ipv6.sh
ok 2 selftests: rdma: rxe_ipv6.sh
# timeout set to 45
# selftests: rdma: rxe_socket_with_netns.sh
ok 3 selftests: rdma: rxe_socket_with_netns.sh
# timeout set to 45
# selftests: rdma: rxe_test_NETDEV_UNREGISTER.sh
ok 4 selftests: rdma: rxe_test_NETDEV_UNREGISTER.sh
# timeout set to 45
# selftests: rdma: rxe_sent_rcvd_bytes.sh
# server DISCONNECT EVENT...
# wait for RDMA_READ_ADV state 10
# client DISCONNECT EVENT...
ok 5 selftests: rdma: rxe_sent_rcvd_bytes.sh
# timeout set to 45
# selftests: rdma: rxe_netns_names.sh
# 1..0 # SKIP rdma netns exclusive mode is not supported
ok 6 selftests: rdma: rxe_netns_names.sh # SKIP
# timeout set to 45
# selftests: rdma: rxe_pkg_netkit.sh
# server DISCONNECT EVENT...
# wait for RDMA_READ_ADV state 10
# ./rxe_pkg_netkit.sh: line 28:  9866 Killed                  "$CUR_WD/rxe_netkit_dump"
ok 7 selftests: rdma: rxe_pkg_netkit.sh
make: Leaving directory '/root/linux/tools/testing/selftests'

---
V1 - > V2: Fix problems from sashiko
---

Zhu Yanjun (2):
selftests/rdma: Introduce eBPF kernel and user dump utilities for RXE selftests/rdma: Add netkit-based automated test script for RXE

 tools/testing/selftests/rdma/Makefile         |   23 +-
 tools/testing/selftests/rdma/config           |    3 +
 .../selftests/rdma/rxe_pkg_kernel_dump.c      |   62 +
 .../testing/selftests/rdma/rxe_pkg_netkit.sh  |   63 +
 .../selftests/rdma/rxe_pkg_user_dump.c        | 1275 +++++++++++++++++
 5 files changed, 1425 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/rdma/rxe_pkg_kernel_dump.c
 create mode 100755 tools/testing/selftests/rdma/rxe_pkg_netkit.sh
 create mode 100644 tools/testing/selftests/rdma/rxe_pkg_user_dump.c
Signature · 1 line
2.54.0
author
#1 lore raw

PATCH v2 1/2 selftests/rdma: Introduce eBPF kernel and user dump utilities for RXE

Add eBPF-based kernel and user-space packet dump utilities along with a netkit test script (rxe_pkg_netkit.sh) to tools/testing/selftests/rdma/. This enables capturing and parsing Soft-RoCE (RXE) traffic over netkit devices for testing purposes.

---
 tools/testing/selftests/rdma/Makefile         |   24 +-
 tools/testing/selftests/rdma/config           |    3 +
 .../selftests/rdma/rxe_pkg_kernel_dump.c      |   73 +
 .../selftests/rdma/rxe_pkg_user_dump.c        | 1334 +++++++++++++++++
 4 files changed, 1433 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/rdma/rxe_pkg_kernel_dump.c
 create mode 100644 tools/testing/selftests/rdma/rxe_pkg_user_dump.c
tools/testing/selftests/rdma/Makefile +23 -1
index a91c14c45006..ba225e580787 100644
@@ -4,6 +4,28 @@ TEST_PROGS := rxe_rping_between_netns.sh \
 		rxe_socket_with_netns.sh \
 		rxe_test_NETDEV_UNREGISTER.sh \
 		rxe_sent_rcvd_bytes.sh \
-		rxe_netns_names.sh
+		rxe_netns_names.sh \
+		rxe_pkg_netkit.sh
 
+TEST_GEN_PROGS_EXTENDED := rxe_netkit_dump
 include ../lib.mk
+
+CLANG	?= clang
+LLC	?= llc
+BPFTOOL	?= bpftool
+ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' | sed 's/aarch64/arm64/' | sed 's/ppc64le/powerpc/')
+
+INCLUDES	:= -I../../../../usr/include -I../../../../include/uapi -I../bpf -I$(OUTPUT)
+CFLAGS		+= -g -Wall $(INCLUDES)
+LDFLAGS		+= -lbpf -lelf -lz
+
+$(OUTPUT)/rxe_netkit_dump: rxe_pkg_user_dump.c $(OUTPUT)/rxe_pkg_kernel.skel.h
+	$(CC) $(CFLAGS) $< $(LDFLAGS) -o $@
+
+$(OUTPUT)/rxe_pkg_kernel_dump.o: rxe_pkg_kernel_dump.c
+	$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) $(INCLUDES) -c $< -o $@
+
+$(OUTPUT)/rxe_pkg_kernel.skel.h: $(OUTPUT)/rxe_pkg_kernel_dump.o
+	$(BPFTOOL) gen skeleton $< > $@
+
+EXTRA_CLEAN := $(OUTPUT)/rxe_pkg_kernel_dump.o $(OUTPUT)/rxe_pkg_kernel.skel.h
tools/testing/selftests/rdma/config +3 -0
index e1ff54ec0f57..a98ffa95b0b7 100644
@@ -3,3 +3,6 @@ CONFIG_VETH
 CONFIG_DUMMY
 CONFIG_NET_NS
 CONFIG_RDMA_RXE
+CONFIG_BPF=y
+CONFIG_BPF_SYSCALL=y
+CONFIG_NETKIT=y
tools/testing/selftests/rdma/rxe_pkg_kernel_dump.c new file +73 -0

73 lines not on this page · raw message

tools/testing/selftests/rdma/rxe_pkg_user_dump.c new file +1334 -0

1,334 lines not on this page · raw message

Signature · 1 line
2.54.0
author
#3 lore raw

PATCH v2 2/2 selftests/rdma: Add netkit-based automated test script for RXE

In reply to Zhu Yanjun

Add a shell script to automate functional and tracing tests for the Soft-RoCE (RXE) driver over netkit network interfaces. The script creates an isolated network namespace, sets up a netkit link pair (nk0/nk1), attaches RXE links, and runs an rping client/server verification test along with kernel tracing output collection.

---
 .../testing/selftests/rdma/rxe_pkg_netkit.sh  | 83 +++++++++++++++++++
 1 file changed, 83 insertions(+)
 create mode 100755 tools/testing/selftests/rdma/rxe_pkg_netkit.sh
tools/testing/selftests/rdma/rxe_pkg_netkit.sh new file +83 -0
new file mode 100755
index 000000000000..a876bd9d57ef
@@ -0,0 +1,83 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+# Copyright (c) 2026 Zhu Yanjun <[email protected]>
+if [ "$(id -u)" -ne 0 ]; then
+    echo "SKIP: This test must be run as root"
+    exit 4
+fi
+
+for cmd in ip rdma rping; do
+    if ! command -v "$cmd" >/dev/null 2>&1; then
+        echo "SKIP: Required tool '$cmd' not found"
+        exit 4
+    fi
+done
+
+if ! modprobe -n netkit >/dev/null 2>&1 || ! modprobe -n rdma_rxe >/dev/null 2>&1; then
+    echo "SKIP: Required kernel modules (netkit or rdma_rxe) are not available"
+    exit 4
+fi
+
+# exec > /dev/null
+
+set -e
+
+CUR_WD="$(dirname "$0")"
+[ "$CUR_WD" = "." ] && CUR_WD="$(pwd)"
+
+cleanup() {
+    set +e
+    [ -n "$DUMP_PID" ] && kill -9 "$DUMP_PID" 2>/dev/null
+    unset DUMP_PID
+
+    rdma link del rxe1 2>/dev/null
+    ip link del nk0 2>/dev/null
+    ip netns delete ns0 2>/dev/null
+
+    rm -f "$CUR_WD/rxe_netkit_dump" "$CUR_WD/rxe_pkg_kernel_dump.o" "$CUR_WD/rxe_pkg_kernel.skel.h"
+}
+
+trap cleanup EXIT INT TERM
+
+# Clear tracing buffer
+echo > /sys/kernel/debug/tracing/trace
+
+# Create network namespace and netkit peer links
+ip netns add ns0
+ip link add nk0 type netkit mode l2 peer name nk1
+ip link set nk0 up
+ip link set nk1 up
+
+# Configure namespace interface
+ip link set nk1 netns ns0
+ip netns exec ns0 ip addr add 10.0.0.1/24 dev nk1
+ip netns exec ns0 ip link set nk1 up
+
+# Configure host interface
+ip addr add 10.0.0.2/24 dev nk0
+
+# Start dump utility in background
+"$CUR_WD/rxe_netkit_dump" &
+DUMP_PID=$!
+
+# Add RXE links over netkit devices
+ip netns exec ns0 rdma link add rxe0 type rxe netdev nk1
+rdma link add rxe1 type rxe netdev nk0
+
+# Start rping server in namespace
+ip netns exec ns0 rping -s -a 10.0.0.1 -C 3 &
+
+# Add wait time
+sleep 1
+
+# Run rping client on host
+rping -c -a 10.0.0.1 -C 3
+
+# Tear down RDMA links and netkit devices
+ip netns exec ns0 rdma link del rxe0
+rdma link del rxe1
+ip link del nk0
+ip netns delete ns0
+
+# Display kernel trace output
+cat /sys/kernel/debug/tracing/trace
Signature · 1 line
2.54.0
#4 lore raw

Re: [PATCH v2 2/2] selftests/rdma: Add netkit-based automated test script for RXE

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [Medium] selftests/rdma: cleanup() in rxe_pkg_netkit.sh deletes compiled test binary - [Medium]…

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [Medium] selftests/rdma: cleanup() in rxe_pkg_netkit.sh deletes compiled test binary
- [Medium] selftests/rdma: rxe_pkg_netkit.sh leaks background rping server and rxe0 link
- [Medium] selftests/rdma: rxe_pkg_netkit.sh fails if debugfs trace path is unavailable
- [Medium] selftests/rdma: rxe_pkg_netkit.sh ignores rxe_netkit_dump failure and output
- [Medium] selftests/rdma: cleanup() trap on INT/TERM resumes execution with set +e
- [Medium] selftests/rdma: overread in print_bpf_output() when dumping RXE_PAYLOAD --

commit 6a324ce884c90ec9814616bc95190ffe879b8905
Author: Zhu Yanjun <[email protected]>

selftests/rdma: Add netkit-based automated test script for RXE

This commit adds a shell script to automate functional and tracing tests for the Soft-RoCE (RXE) driver over netkit network interfaces using rping and rxe_netkit_dump.

Quoted 6 lines
tools/testing/selftests/rdma/rxe_pkg_netkit.sh new file +0 -0
new file mode 100755
index 0000000000000..a876bd9d57efb
@@ -0,0 +1,83 @@

[ ... ]

Quoted 5 lines
tools/testing/selftests/rdma/rxe_pkg_netkit.sh new file +5 -0
+set -e
+
+CUR_WD="$(dirname "$0")"
+[ "$CUR_WD" = "." ] && CUR_WD="$(pwd)"
+

tools/testing/selftests/rdma/rxe_pkg_netkit.sh line 37

Quoted 10 lines
tools/testing/selftests/rdma/rxe_pkg_netkit.sh new file +10 -0
+cleanup() {
+    set +e
+    [ -n "$DUMP_PID" ] && kill -9 "$DUMP_PID" 2>/dev/null
+    unset DUMP_PID
+
+    rdma link del rxe1 2>/dev/null
+    ip link del nk0 2>/dev/null
+    ip netns delete ns0 2>/dev/null
+
+    rm -f "$CUR_WD/rxe_netkit_dump" "$CUR_WD/rxe_pkg_kernel_dump.o" "$CUR_WD/rxe_pkg_kernel.skel.h"

[Severity: Medium]
Should cleanup() really remove rxe_netkit_dump, rxe_pkg_kernel_dump.o, and rxe_pkg_kernel.skel.h here?

tools/testing/selftests/rdma/Makefile builds these files during compilation and cleans them via TEST_GEN_PROGS_EXTENDED and EXTRA_CLEAN. Because cleanup() is registered via trap on EXIT, running rxe_pkg_netkit.sh once deletes the compiled rxe_netkit_dump binary from the test or installed kselftest directory, causing all subsequent runs of the script to fail when invoking "$CUR_WD/rxe_netkit_dump".

tools/testing/selftests/rdma/rxe_pkg_netkit.sh line 40

Quoted 3 lines
tools/testing/selftests/rdma/rxe_pkg_netkit.sh new file +3 -0
+}
+
+trap cleanup EXIT INT TERM

[Severity: Medium]
When SIGINT or SIGTERM is delivered to rxe_pkg_netkit.sh (for example, on a kselftest timeout), cleanup() runs set +e, tears down the test interfaces, and returns without exiting the shell.

Won't the script then resume execution on the torn-down interfaces with error checking disabled, run cleanup() a second time on EXIT, and potentially exit with status 0 after being interrupted by a signal?

tools/testing/selftests/rdma/rxe_pkg_netkit.sh line 43

Quoted 3 lines
tools/testing/selftests/rdma/rxe_pkg_netkit.sh new file +3 -0
+
+# Clear tracing buffer
+echo > /sys/kernel/debug/tracing/trace

[Severity: Medium]
What happens on systems where tracefs is mounted standalone at /sys/kernel/tracing without debugfs at /sys/kernel/debug/tracing, or when kernel tracing is not enabled in the config?

With set -e active, writing to /sys/kernel/debug/tracing/trace will abort the script with a failure exit code rather than falling back to /sys/kernel/tracing/trace or skipping with exit 4.

Quoted 8 lines
tools/testing/selftests/rdma/rxe_pkg_netkit.sh new file +8 -0
+
+# Create network namespace and netkit peer links
+ip netns add ns0
+ip link add nk0 type netkit mode l2 peer name nk1
+ip link set nk0 up
+ip link set nk1 up
+
+# Configure namespace interface

tools/testing/selftests/rdma/rxe_pkg_netkit.sh line 61

Quoted 10 lines
tools/testing/selftests/rdma/rxe_pkg_netkit.sh new file +10 -0
+ip link set nk1 netns ns0
+ip netns exec ns0 ip addr add 10.0.0.1/24 dev nk1
+ip netns exec ns0 ip link set nk1 up
+
+# Configure host interface
+ip addr add 10.0.0.2/24 dev nk0
+
+# Start dump utility in background
+"$CUR_WD/rxe_netkit_dump" &
+DUMP_PID=$!

[Severity: Medium]
When rxe_pkg_netkit.sh starts rxe_netkit_dump and generates RoCEv2 rping traffic, can print_bpf_output() in rxe_pkg_user_dump.c read past the end of the captured packet into uninitialized ring buffer memory, or out-of-bounds past the ring buffer record?

In tools/testing/selftests/rdma/rxe_pkg_kernel_dump.c:
handle_netkit_ingress() reserves a fixed MAX_PKT_LEN (512) bytes in the BPF ring buffer, but only copies len (skb->len) bytes:

    void *reserve = bpf_ringbuf_reserve(&rb, MAX_PKT_LEN, 0);
    ...
    if (bpf_skb_load_bytes(skb, 0, reserve, len) < 0) {

Then in tools/testing/selftests/rdma/rxe_pkg_user_dump.c, handle_event() passes data_sz - sizeof(struct ethhdr) (which is always 512 - 14 = 498) to print_bpf_output(), and the RXE_PAYLOAD handler computes:

    case RXE_PAYLOAD:
        printf("RXE_PAYLOAD\n");
        int len = ntohs(udp_hdr->len) - rxe_opcode[bth->opcode].length;

        for (int j = 0; j < len; j++) {
            if (isprint(cur_p[j]))
                printf("%c", cur_p[j]);
            else
                printf("%02x", cur_p[j]);
        }

Because cur_p starts at (char *)bth + offset (after the UDP and RDMA headers), and len does not subtract sizeof(struct udphdr) (8 bytes) or check against the captured packet size, doesn't this loop read 8 bytes past skb->len into uninitialized ring buffer memory on every RoCEv2 packet, and read past the 512-byte ring buffer record when skb->len > 504?

tools/testing/selftests/rdma/rxe_pkg_netkit.sh line 68

Quoted 7 lines
tools/testing/selftests/rdma/rxe_pkg_netkit.sh new file +7 -0
+
+# Add RXE links over netkit devices
+ip netns exec ns0 rdma link add rxe0 type rxe netdev nk1
+rdma link add rxe1 type rxe netdev nk0
+
+# Start rping server in namespace
+ip netns exec ns0 rping -s -a 10.0.0.1 -C 3 &

[Severity: Medium]
Does cleanup() leak the background rping server process and the rxe0 RDMA link if the rping client fails or the script is interrupted?

Unlike DUMP_PID, the PID of this background rping -s process is not saved, and cleanup() neither kills it nor runs:

ip netns exec ns0 rdma link del rxe0 2>/dev/null

If rping -c fails under set -e, ip netns delete ns0 in cleanup() only unlinks the netns mount point while the orphaned rping -s process stays alive holding ns0, nk1, rxe0, and the inherited stdout/stderr pipe file descriptors open (which can also cause the kselftest runner to hang waiting for pipe EOF).

Quoted 5 lines
tools/testing/selftests/rdma/rxe_pkg_netkit.sh new file +5 -0
+
+# Add wait time
+sleep 1
+
+# Run rping client on host

tools/testing/selftests/rdma/rxe_pkg_netkit.sh line 83

Quoted 10 lines
tools/testing/selftests/rdma/rxe_pkg_netkit.sh new file +10 -0
+rping -c -a 10.0.0.1 -C 3
+
+# Tear down RDMA links and netkit devices
+ip netns exec ns0 rdma link del rxe0
+rdma link del rxe1
+ip link del nk0
+ip netns delete ns0
+
+# Display kernel trace output
+cat /sys/kernel/debug/tracing/trace

[Severity: Medium]
Will this test pass even if rxe_netkit_dump fails to start, fails to attach its BPF program to nk0, or captures no packets?

Launching "$CUR_WD/rxe_netkit_dump" & in the background succeeds even if the binary is missing or exits immediately (and main() in rxe_pkg_user_dump.c returns 0 on its goto cleanup error paths). In addition, cleanup() terminates DUMP_PID with kill -9, which prevents rxe_netkit_dump from flushing block-buffered stdout when running under non-interactive kselftest pipes or cleaning up its BPF resources, and the final cat /sys/kernel/debug/tracing/trace does not check that any expected trace events or packets were recorded.

Would it be better to verify that DUMP_PID is alive and ready before running rping, signal it with SIGINT or SIGTERM instead of SIGKILL, and check that the expected trace output was actually captured?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#2 lore raw

Re: [PATCH v2 1/2] selftests/rdma: Introduce eBPF kernel and user dump utilities for RXE

In reply to Zhu Yanjun
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Medium] selftests/rdma: uninit ringbuf read and OOB payload dump in print_bpf_output() - [Medium]…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Medium] selftests/rdma: uninit ringbuf read and OOB payload dump in print_bpf_output()
- [Medium] selftests/rdma: missing packed attribute on struct rxe_atmeth
- [Medium] selftests/rdma: main() in rxe_pkg_user_dump.c masks errors by returning 0
- [Low] selftests/rdma: broken include paths and system bpf deps in Makefile --

commit cad47e7d3ec5a1fbd0772ac7d820b7588ad1671d
Author: Zhu Yanjun <[email protected]>

selftests/rdma: Introduce eBPF kernel and user dump utilities for RXE

This commit adds eBPF-based kernel and user-space packet dump utilities (rxe_pkg_kernel_dump.c and rxe_pkg_user_dump.c) to tools/testing/selftests/rdma/ to capture and parse Soft-RoCE (RXE) traffic over netkit devices.

Quoted 22 lines
tools/testing/selftests/rdma/Makefile +11 -1
index a91c14c45006e..ba225e580787d 100644
@@ -4,6 +4,28 @@ TEST_PROGS := rxe_rping_between_netns.sh \
 		rxe_socket_with_netns.sh \
 		rxe_test_NETDEV_UNREGISTER.sh \
 		rxe_sent_rcvd_bytes.sh \
-		rxe_netns_names.sh
+		rxe_netns_names.sh \
+		rxe_pkg_netkit.sh
 
+TEST_GEN_PROGS_EXTENDED := rxe_netkit_dump
 include ../lib.mk
+
+CLANG	?= clang
+LLC	?= llc
+BPFTOOL	?= bpftool
+ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' | sed 's/aarch64/arm64/' | sed 's/ppc64le/powerpc/')
+
+INCLUDES	:= -I../../../../usr/include -I../../../../include/uapi -I../bpf -I$(OUTPUT)
+CFLAGS		+= -g -Wall $(INCLUDES)

tools/testing/selftests/rdma/Makefile TEST_PROGS := rxe_rping_between_netns.sh \ · line 29

Quoted 10 lines
tools/testing/selftests/rdma/Makefile +10 -0
+LDFLAGS		+= -lbpf -lelf -lz
+
+$(OUTPUT)/rxe_netkit_dump: rxe_pkg_user_dump.c $(OUTPUT)/rxe_pkg_kernel.skel.h
+	$(CC) $(CFLAGS) $< $(LDFLAGS) -o $@
+
+$(OUTPUT)/rxe_pkg_kernel_dump.o: rxe_pkg_kernel_dump.c
+	$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) $(INCLUDES) -c $< -o $@
+
+$(OUTPUT)/rxe_pkg_kernel.skel.h: $(OUTPUT)/rxe_pkg_kernel_dump.o
+	$(BPFTOOL) gen skeleton $< > $@

[Severity: Low]
Will these build rules work for out-of-tree builds or on systems without a recent system-wide libbpf and bpftool installed?

There appear to be a few issues with the build configuration here:

1. INCLUDES hardcodes -I../../../../usr/include and -I../../../../include/uapi instead of using $(KHDR_INCLUDES) from ../lib.mk. In out-of-tree builds (make O=...), exported headers are placed under $(O)/usr/include rather than ../../../../usr/include. Falling back to raw unexported kernel headers in ../../../../include/uapi lacks generated asm headers, triggers the "Attempt to use kernel headers from user space" warning in include/uapi/linux/types.h, and uses raw _UAPI_* header guards that conflict with glibc headers such as net/ethernet.h. In addition, invoking $(CLANG) -target bpf without host multiarch system include paths (such as get_sys_includes in tools/testing/selftests/net/bpf.mk) can fail to locate architecture headers like sys/cdefs.h, gnu/stubs.h, and asm/types.h.

2. The Makefile links against a system-installed -lbpf and invokes a system bpftool instead of building the in-tree tools/lib/bpf and tools/bpf/bpftool (as done in tools/testing/selftests/net/bpf.mk and tools/testing/selftests/hid/Makefile). Note that -I../bpf does not provide bpf/bpf_helpers.h or bpf/libbpf.h, and rxe_pkg_user_dump.c requires recent libbpf support for bpf_program__attach_netkit() and SEC("netkit/primary").

3. The rule for $(OUTPUT)/rxe_pkg_kernel.skel.h redirects stdout directly to $@ without .DELETE_ON_ERROR or temporary file cleanup on failure, so a missing or failing bpftool leaves behind an empty header file that subsequent make invocations treat as up to date.

Quoted 7 lines
tools/testing/selftests/rdma/Makefile +2 -0
+
+EXTRA_CLEAN := $(OUTPUT)/rxe_pkg_kernel_dump.o $(OUTPUT)/rxe_pkg_kernel.skel.h
tools/testing/selftests/rdma/rxe_pkg_kernel_dump.c new file +0 -0
new file mode 100644
index 0000000000000..7227fc4a6f57e

[ ... ]

Quoted 3 lines
tools/testing/selftests/rdma/rxe_pkg_kernel_dump.c new file +3 -0
+SEC("netkit/primary")
+int handle_netkit_ingress(struct __sk_buff *skb)
+{

[ ... ]

Quoted 15 lines
tools/testing/selftests/rdma/rxe_pkg_kernel_dump.c new file +15 -0
+			__u32 len = skb->len;
+
+			/* It is a rping test, the size of packets should be
+			 * less than MAX_PKT_LEN
+			 */
+			if (len == 0 || len > MAX_PKT_LEN) {
+				bpf_printk("len is not in 0..MAX_PKT_LEN\n");
+				return BPF_OK;
+			}
+
+			/* The len is fixed to MAX_PKT_LEN */
+			void *reserve = bpf_ringbuf_reserve(&rb, MAX_PKT_LEN, 0);
+
+			if (!reserve) {
+				bpf_printk("bpf_ringbuf_reserve fails\n");

tools/testing/selftests/rdma/rxe_pkg_kernel_dump.c line 63

Quoted 10 lines
tools/testing/selftests/rdma/rxe_pkg_kernel_dump.c new file +10 -0
+				return BPF_OK;
+			}
+
+			if (bpf_skb_load_bytes(skb, 0, reserve, len) < 0) {
+				bpf_ringbuf_discard(reserve, 0);
+				bpf_printk("bpf_skb_load_bytes fails\n");
+				return BPF_OK;
+			}
+
+			bpf_ringbuf_submit(reserve, 0);

[Severity: Medium]
In handle_netkit_ingress(), bpf_ringbuf_reserve() always reserves MAX_PKT_LEN
(512) bytes, while bpf_skb_load_bytes() only writes len (skb->len) bytes into reserve without zeroing the trailing MAX_PKT_LEN - len bytes or passing the actual captured packet length to user space.

When handle_event() in rxe_pkg_user_dump.c receives this record, data_sz is always 512 rather than skb->len, so print_bpf_output() cannot tell how many bytes were actually initialized by bpf_skb_load_bytes() (see the comment on print_bpf_output() below).

Quoted 15 lines
tools/testing/selftests/rdma/rxe_pkg_kernel_dump.c new file +10 -0
+		}
+	}
+
+	/* BPF_OK:	Pass
+	 * BPF_DROP:	Drop skb
+	 */
+	return BPF_OK;
+}
+
+char _license[] SEC("license") = "GPL";
tools/testing/selftests/rdma/rxe_pkg_user_dump.c new file +0 -0
new file mode 100644
index 0000000000000..b98d77b1dcf2c

[ ... ]

tools/testing/selftests/rdma/rxe_pkg_user_dump.c line 215

Quoted 9 lines
tools/testing/selftests/rdma/rxe_pkg_user_dump.c new file +9 -0
+/******************************************************************************
+ * Atomic Extended Transport Header
+ ******************************************************************************/
+struct rxe_atmeth {
+	__be64			va;
+	__be32			rkey;
+	__be64			swap_add;
+	__be64			comp;
+};

[Severity: Medium]
Should struct rxe_atmeth be marked with __attribute__((packed))?

In drivers/infiniband/sw/rxe/rxe_hdr.h, struct rxe_atmeth is defined with __packed so that va (offset 0), rkey (offset 8), swap_add (offset 12), and comp (offset 20) match the 28-byte RoCEv2 wire layout. Even print_atmeth() below prints "} __packed;\n".

Without the packed attribute here, the compiler inserts 4 bytes of alignment padding between rkey and swap_add, increasing RXE_ATMETH_BYTES from 28 to 32 bytes, shifting swap_add and comp by 4 bytes in print_atmeth(), and miscalculating the header lengths and RXE_PAYLOAD offsets for IB_OPCODE_RC_COMPARE_SWAP, IB_OPCODE_RC_FETCH_ADD, IB_OPCODE_RD_COMPARE_SWAP, and IB_OPCODE_RD_FETCH_ADD.

[ ... ]

Quoted 2 lines
tools/testing/selftests/rdma/rxe_pkg_user_dump.c new file +2 -0
+static void print_bpf_output(void *data, __u32 size)
+{

[ ... ]

Quoted 7 lines
tools/testing/selftests/rdma/rxe_pkg_user_dump.c new file +7 -0
+	for (int i = RXE_BTH + 1; i < NUM_HDR_TYPES; i++) {
+		int offset = rxe_opcode[bth->opcode].offset[i];
+
+		if (offset && (size >= sizeof(struct iphdr) + sizeof(struct udphdr) + offset)) {
+			char *cur_p = (char *)bth + offset;
+
+			switch (i) {

[ ... ]

Quoted 17 lines
tools/testing/selftests/rdma/rxe_pkg_user_dump.c new file +17 -0
+			case	RXE_PAYLOAD:
+				printf("RXE_PAYLOAD\n");
+				int len = ntohs(udp_hdr->len) - rxe_opcode[bth->opcode].length;
+
+				for (int j = 0; j < len; j++) {
+					if (isprint(cur_p[j]))
+						printf("%c", cur_p[j]);
+					else
+						printf("%02x", cur_p[j]);
+				}
+				printf("\n");
+				break;
+			default:
+				break;
+			}
+		}
+	}

tools/testing/selftests/rdma/rxe_pkg_user_dump.c line 1278

Quoted 10 lines
tools/testing/selftests/rdma/rxe_pkg_user_dump.c new file +10 -0
+
+	printf("\n");
+}
+
+static int handle_event(void *ctx, void *data, unsigned long data_sz)
+{
+	print_bpf_output(data+sizeof(struct ethhdr),
+			 data_sz-sizeof(struct ethhdr));
+	return 0;
+}

[Severity: Medium]
Can print_bpf_output() read uninitialized ring buffer memory or read out of bounds past the end of data[] when dumping headers and RXE_PAYLOAD?

Looking at handle_event() -> print_bpf_output():

1. Because handle_netkit_ingress() reserves a fixed 512-byte ring buffer entry without passing skb->len, size is always 498 (512 - sizeof(struct ethhdr)) rather than the captured packet length. Furthermore, the loop condition (size >= sizeof(struct iphdr) + sizeof(struct udphdr) + offset) only checks that the start offset is within size, not that the full extended header (offset + sizeof(header)) fits within the captured packet.

2. Under case RXE_PAYLOAD, len is computed as:

       int len = ntohs(udp_hdr->len) - rxe_opcode[bth->opcode].length;

udp_hdr->len includes sizeof(struct udphdr) (8 bytes), the RoCE transport headers, the payload, BTH padding (0..3 bytes), and the 4-byte RoCEv2 ICRC. Because sizeof(struct udphdr), padding, and the 4-byte ICRC are not subtracted, len is at least 12 bytes larger than the actual payload (and 8 bytes larger than the entire UDP datagram). In addition, rxe_opcode[bth->opcode].offset[RXE_PAYLOAD] is populated even for header-only opcodes such as IB_OPCODE_RC_ACKNOWLEDGE and IB_OPCODE_RC_RDMA_READ_REQUEST, so RXE_PAYLOAD is entered unconditionally.

3. Because len is never clamped to the remaining bytes in data[], cur_p[j] reads at least 8 bytes of uninitialized ring buffer memory past the end of every packet shorter than 512 bytes, and reads out of bounds past the end of the 512-byte ring buffer record when a packet is 505..512 bytes long (or if udp_hdr->len is larger than the buffer).

4. cur_p is a signed char *, so passing cur_p[j] directly to isprint() and printf("%02x", cur_p[j]) without casting to unsigned char invokes undefined behavior in isprint() for bytes >= 0x80 and sign-extends them to 8 hex digits.

Quoted 46 lines
tools/testing/selftests/rdma/rxe_pkg_user_dump.c new file +46 -0
+
+int main(void)
+{
+	struct rxe_pkg_kernel_dump *skel;
+	int err = 1;
+	struct ring_buffer *ringbuf = NULL;
+	struct rlimit rlim = { .rlim_cur = RLIM_INFINITY, .rlim_max = RLIM_INFINITY };
+
+	if (setrlimit(RLIMIT_MEMLOCK, &rlim)) {
+		perror("setrlimit failed");
+		return 1;
+	}
+
+	skel = rxe_pkg_kernel_dump__open_and_load();
+	if (!skel) {
+		fprintf(stderr, "Failed to open and load BPF skeleton\n");
+		return 1;
+	}
+
+	int ifindex = if_nametoindex("nk0");
+
+	if (ifindex == 0) {
+		perror("if_nametoindex nk0 failed");
+		goto cleanup;
+	}
+
+	skel->links.handle_netkit_ingress = bpf_program__attach_netkit(
+		skel->progs.handle_netkit_ingress, ifindex, NULL);
+
+	if (!skel->links.handle_netkit_ingress) {
+		fprintf(stderr, "Failed to attach to netkit nk0\n");
+		goto cleanup;
+	}
+
+	ringbuf = ring_buffer__new(bpf_map__fd(skel->maps.rb),
+				   handle_event, NULL, NULL);
+	if (!ringbuf) {
+		fprintf(stderr, "Failed to create ring buffer\n");
+		goto cleanup;
+	}
+
+	err = 0;
+
+	while (1) {
+		err = ring_buffer__poll(ringbuf, 100 /* timeout */);
+		if (err < 0 && err != -EINTR) {

tools/testing/selftests/rdma/rxe_pkg_user_dump.c line 1334

Quoted 10 lines
tools/testing/selftests/rdma/rxe_pkg_user_dump.c new file +10 -0
+			fprintf(stderr, "poll error\n");
+			break;
+		}
+	}
+
+cleanup:
+	ring_buffer__free(ringbuf);
+	rxe_pkg_kernel_dump__destroy(skel);
+	return 0;
+}

[Severity: Medium]
Should main() return a non-zero exit status on error, and handle termination signals cleanly?

If if_nametoindex("nk0"), bpf_program__attach_netkit(), or ring_buffer__new() fails, execution jumps to cleanup with err == 1; similarly, if ring_buffer__poll() fails with an error other than -EINTR, the loop breaks to cleanup with err < 0. However, the cleanup path ignores err and unconditionally returns 0, masking failures from the calling test script.

Also, because the while (1) loop has no signal handler for SIGINT/SIGTERM and print_bpf_output() never flushes stdout, running rxe_netkit_dump in the background with stdout redirected to a file or pipe and killing it from a test script will lose block-buffered stdout output and skip the cleanup label.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1