bpfunit

package
v0.0.0-...-6ed2b60 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

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

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

func DrainTelemetryByMACs(m *ebpf.Map, srcMAC, dstMAC uint64) error

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

func FindZone(m *ebpf.Map, srcMAC, dstMAC uint64, dir bpf.Direction) (bpf.ZoneCode, bool, error)

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

func MACBytes(v uint64) [6]byte

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) Close

func (d *Driver) Close() error

Close releases the BPF resources held by the driver. Safe to call twice.

func (*Driver) Map

func (d *Driver) Map(name string) *ebpf.Map

Map returns the named BPF map, or nil if not loaded.

func (*Driver) Program

func (d *Driver) Program(name string) *ebpf.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

func (d *Driver) Run(progName string, frame []byte) (verdict uint32, err error)

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.

Directories

Path Synopsis
Package fixtures contains BPF programs used only by the test infrastructure.
Package fixtures contains BPF programs used only by the test infrastructure.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL