test(ebpf): add eBPF program unit tests via BPF_PROG_TEST_RUN#2049
Open
test(ebpf): add eBPF program unit tests via BPF_PROG_TEST_RUN#2049
Conversation
7 tasks
Add a test framework that executes compiled eBPF programs in the kernel using BPF_PROG_TEST_RUN with crafted packets, validating actual bytecode behavior rather than mocking the Go wrapper layer. Shared infrastructure in pkg/plugin/ebpftest/: - helpers.go: privilege checks, map pinning removal, perf event reading, IP/port byte-order helpers, filter map population - packet.go: TCP/UDP/ICMP/ARP packet construction via gopacket Packetparser tests (25 test functions) covering: - Packet parsing: TCP flags, timestamps, UDP, observation points - Filter map: IP matching and rejection - Malformed packets: runt, truncated IP/TCP, non-IPv4 - Conntrack perf events: is_reply, traffic direction, metrics - Conntrack map state: entry creation fields, flag accumulation, reply updates RX fields, direction-unknown for mid-stream flows, since-last-report counter accumulation and reset, lifetime metadata counters, eviction time extension - Recompilation variants: HIGH aggregation suppression, ENABLE_CONNTRACK_METRICS Includes CI workflow (.github/workflows/test-ebpf.yaml) running on both amd64 and arm64 runners, and a `make test-ebpf` target. Signed-off-by: Quang Nguyen <nguyenquang@microsoft.com>
2a83e29 to
6fa7245
Compare
Retina Code Coverage ReportTotal coverage no change |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Add an eBPF program unit test framework that executes compiled eBPF bytecode in the kernel using
BPF_PROG_TEST_RUN(prog.Run()in cilium/ebpf). This validates actual kernel-side packet parsing logic rather than mocking the Go wrapper layer, catching bugs like wrong offsets, missing bounds checks, or incorrect flag handling that would otherwise only surface at runtime in a cluster.Shared test infrastructure (
pkg/plugin/ebpftest/):helpers.go— privilege checks, map pinning removal, generic perf event reader, IP/port byte-order helpers, LPM trie filter map population, program runnerpacket.go— TCP/UDP/ICMP/ARP packet construction via gopacket with configurable flags, timestamps, and payloadsPacketparser tests (
pkg/plugin/packetparser/packetparser_ebpf_test.go) — 25 test functions covering:is_replyacross SYN/SYN-ACK/ACK, traffic direction per observation point, conntrack metrics counters, HIGH aggregation suppression,previously_observed_*fieldsis_direction_unknownfor mid-stream connections, since-last-report counter accumulation and reset on FIN, lifetime metadata counters withENABLE_CONNTRACK_METRICS, eviction time extension from SYN timeout to establisheddynamic.hvialoader.CompileEbpfforDATA_AGGREGATION_LEVEL=HIGHandENABLE_CONNTRACK_METRICS=1CI (
.github/workflows/test-ebpf.yaml): matrix strategy on amd64 (ubuntu-latest) and arm64 (ubuntu-24.04-arm), compiles eBPF viago generate, runs tests withsudo.Makefile:
make test-ebpftarget.Related Issue
N/A — new test coverage for previously untested eBPF programs.
Checklist
git commit -S -s ...). See this documentation on signing commits.Screenshots (if applicable) or Testing Completed
All 25 eBPF tests pass locally on Linux amd64 with kernel 6.14.0:
Lint passes cleanly (
make lint— 0 new issues).Existing unit tests unaffected (different build tag:
ebpfvsunit).Additional Notes
//go:build ebpf && linux— they do not run duringmake testCAP_BPF+CAP_NET_ADMIN(or root) and Linux kernel >= 5.xebpftestpackage is designed for reuse by other plugin tests (dropreason, dns, etc.)loader.CompileEbpf(same as production) and require clangPlease refer to the CONTRIBUTING.md file for more information on how to contribute to this project.