Skip to content

Commit

Permalink
integration tests: add test that loads all maps and programs
Browse files Browse the repository at this point in the history
initializeMapsAndPrograms() provides core functionality in sizing and
loading eBPF maps and programs. So far per-kernel tests only load specific
named eBPF programs and therefore could not catch Linux kernel version
specific issues with eBPF programs that were not loaded.

Signed-off-by: Florian Lehner <[email protected]>
  • Loading branch information
florianl committed Oct 25, 2024
1 parent b98de5d commit 7313c33
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
9 changes: 9 additions & 0 deletions tracer/ebpf_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

cebpf "github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -244,3 +245,11 @@ Loop:
})
}
}

func TestAllTracers(t *testing.T) {
coll, err := support.LoadCollectionSpec(false)
require.NoError(t, err)

_, _, err = initializeMapsAndPrograms(coll, tracertypes.AllTracers(), nil, false, 1, false, 0)
require.NoError(t, err)
}
30 changes: 15 additions & 15 deletions tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,20 @@ func NewTracer(ctx context.Context, cfg *Config) (*Tracer, error) {
return nil, fmt.Errorf("failed to read kernel symbols: %v", err)
}

// Loading specifications about eBPF programs and maps from the embedded elf file
// does not load them into the kernel.
// A collection specification holds the information about eBPF programs and maps.
// References to eBPF maps in the eBPF programs are just placeholders that need to be
// replaced by the actual loaded maps later on with RewriteMaps before loading the
// programs into the kernel.
coll, err := support.LoadCollectionSpec(cfg.DebugTracer)
if err != nil {
return nil, fmt.Errorf("failed to load specification for tracers: %v", err)
}

// Based on includeTracers we decide later which are loaded into the kernel.
ebpfMaps, ebpfProgs, err := initializeMapsAndPrograms(cfg.IncludeTracers, kernelSymbols,
cfg.FilterErrorFrames, cfg.MapScaleFactor, cfg.KernelVersionCheck, cfg.DebugTracer,
cfg.BPFVerifierLogLevel)
ebpfMaps, ebpfProgs, err := initializeMapsAndPrograms(coll, cfg.IncludeTracers, kernelSymbols,
cfg.FilterErrorFrames, cfg.MapScaleFactor, cfg.KernelVersionCheck, cfg.BPFVerifierLogLevel)
if err != nil {
return nil, fmt.Errorf("failed to load eBPF code: %v", err)
}
Expand Down Expand Up @@ -369,20 +379,10 @@ func buildStackDeltaTemplates(coll *cebpf.CollectionSpec) error {

// initializeMapsAndPrograms loads the definitions for the eBPF maps and programs provided
// by the embedded elf file and loads these into the kernel.
func initializeMapsAndPrograms(includeTracers types.IncludedTracers,
func initializeMapsAndPrograms(coll *cebpf.CollectionSpec, includeTracers types.IncludedTracers,
kernelSymbols *libpf.SymbolMap, filterErrorFrames bool, mapScaleFactor int,
kernelVersionCheck bool, debugTracer bool, bpfVerifierLogLevel uint32) (
kernelVersionCheck bool, bpfVerifierLogLevel uint32) (
ebpfMaps map[string]*cebpf.Map, ebpfProgs map[string]*cebpf.Program, err error) {
// Loading specifications about eBPF programs and maps from the embedded elf file
// does not load them into the kernel.
// A collection specification holds the information about eBPF programs and maps.
// References to eBPF maps in the eBPF programs are just placeholders that need to be
// replaced by the actual loaded maps later on with RewriteMaps before loading the
// programs into the kernel.
coll, err := support.LoadCollectionSpec(debugTracer)
if err != nil {
return nil, nil, fmt.Errorf("failed to load specification for tracers: %v", err)
}

err = buildStackDeltaTemplates(coll)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions tracer/types/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,11 @@ func Parse(tracers string) (IncludedTracers, error) {

return result, nil
}

// AllTracers is a shortcut that returns an element with all
// tracers enabled.
func AllTracers() IncludedTracers {
var result IncludedTracers
result.enableAll()
return result
}

0 comments on commit 7313c33

Please sign in to comment.