Documentation
¶
Overview ¶
Package bpfunit drives BPF programs through BPF_PROG_TEST_RUN for unit testing and microbenchmarking.
It wraps the cilium/ebpf primitives with a small surface so test authors don't deal with CollectionSpec lifecycles or syscall semantics directly. Production code does not import this package.
Index ¶
- func ARPFrame(srcMAC, dstMAC net.HardwareAddr) []byte
- func DrainTelemetryByMACs(m *ebpf.Map, srcMAC, dstMAC uint64) error
- func EthIPv4TCP(srcMAC, dstMAC net.HardwareAddr, srcIP, dstIP net.IP, srcPort, dstPort uint16, ...) []byte
- func EthIPv4UDP(srcMAC, dstMAC net.HardwareAddr, srcIP, dstIP net.IP, srcPort, dstPort uint16, ...) []byte
- func EthIPv6TCP(srcMAC, dstMAC net.HardwareAddr, srcIP, dstIP net.IP, srcPort, dstPort uint16, ...) []byte
- func FindZone(m *ebpf.Map, srcMAC, dstMAC uint64, dir bpf.Direction) (bpf.ZoneCode, bool, error)
- func MAC(v uint64) net.HardwareAddr
- func MACBytes(v uint64) [6]byte
- type Driver
- func (d *Driver) Close() error
- func (d *Driver) Map(name string) *ebpf.Map
- func (d *Driver) Program(name string) *ebpf.Program
- func (d *Driver) Run(progName string, frame []byte) (verdict uint32, err error)
- func (d *Driver) RunRepeat(progName string, frame []byte, count uint32) (total, perRun time.Duration, err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ARPFrame ¶
func ARPFrame(srcMAC, dstMAC net.HardwareAddr) []byte
ARPFrame returns a minimal ARP request frame for non-IP pass-through tests.
func DrainTelemetryByMACs ¶
DrainTelemetryByMACs deletes every telemetry_map entry whose flow_key matches the (srcMAC, dstMAC) pair in either direction. Tests call this between subcases so a prior /24 hit doesn't carry over to a later case sharing the same MAC pair.
func EthIPv4TCP ¶
func EthIPv4TCP(srcMAC, dstMAC net.HardwareAddr, srcIP, dstIP net.IP, srcPort, dstPort uint16, payload []byte) []byte
EthIPv4TCP returns a serialized Ethernet + IPv4 + TCP SYN frame. payload may be nil. Returned slice is owned by the caller.
func EthIPv4UDP ¶
func EthIPv4UDP(srcMAC, dstMAC net.HardwareAddr, srcIP, dstIP net.IP, srcPort, dstPort uint16, payload []byte) []byte
EthIPv4UDP returns a serialized Ethernet + IPv4 + UDP frame.
func EthIPv6TCP ¶
func EthIPv6TCP(srcMAC, dstMAC net.HardwareAddr, srcIP, dstIP net.IP, srcPort, dstPort uint16, payload []byte) []byte
EthIPv6TCP returns a serialized Ethernet + IPv6 + TCP SYN frame.
func FindZone ¶
FindZone scans telemetry_map for the first entry whose flow_key matches (srcMAC, dstMAC, dir) and returns the recorded DstZone. The map is PERCPU_HASH; this helper inspects only keys.
func MAC ¶
func MAC(v uint64) net.HardwareAddr
MAC builds a hardware address from the low 48 bits of v in big-endian order. Convenience for tests already working in u64-as-MAC form.
func MACBytes ¶
MACBytes converts the low 48 bits of v into a 6-byte array in big-endian order — the layout used by bpf.FlowKey.SrcMac / bpf.FlowKey.DstMac. It is the exact inverse of bpf.MACKey (the forward MAC→u64 contract); keep the two in sync. Sibling of MAC for callers that need the array form rather than the net.HardwareAddr slice.
Types ¶
type Driver ¶
type Driver struct {
// contains filtered or unexported fields
}
Driver owns a loaded BPF Collection and exposes Run / RunRepeat for tests.
The zero value is not usable; call New.
func New ¶
func New(spec *ebpf.CollectionSpec) (*Driver, error)
New loads spec into the kernel and returns a Driver. The caller owns the returned Driver and must Close it.
func (*Driver) Program ¶
Program returns the named BPF program, or nil if not loaded. Tests that attach the program to a real interface via TC need the *ebpf.Program; tests that exercise it via BPF_PROG_TEST_RUN should use Run / RunRepeat.
func (*Driver) Run ¶
Run executes progName against frame and returns the BPF verdict.
frame must be a complete L2 Ethernet frame — use the builders in packet.go. The verdict is the program's return value (TC_ACT_OK=0, TC_ACT_SHOT=2, …).
func (*Driver) RunRepeat ¶
func (d *Driver) RunRepeat(progName string, frame []byte, count uint32) (total, perRun time.Duration, err error)
RunRepeat executes progName count times in a single syscall and reports per-run kernel-measured runtime plus the extrapolated total.
The kernel times only program execution (excludes the syscall boundary), making this the right primitive for ns/packet benchmarks. cilium/ebpf's Benchmark already returns the time *per iteration* (the kernel averages over count internally), so perRun is that value directly — do not divide by count again — and total is perRun reconstructed across the run.