Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tetragon: Factor the maps max entries setup #2565

Merged
merged 4 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/tetragon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ func tetragonExecute() error {
initialSensor.Unload()
}()

cgrouprate.NewCgroupRate(ctx, pm, base.CgroupRateMapExec, &option.Config.CgroupRate)
cgrouprate.NewCgroupRate(ctx, pm, base.CgroupRateMap, &option.Config.CgroupRate)
cgrouprate.Config(base.CgroupRateOptionsMap)

// now that the base sensor was loaded, we can start the sensor manager
Expand Down
3 changes: 2 additions & 1 deletion pkg/cgrouprate/cgrouprate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ func TestProcessCgroup(t *testing.T) {
MaxEntries: 32768,
}

hash := program.MapBuilder("hash", nil)
load := program.Builder("", "", "", "", "")
hash := program.MapBuilder("hash", load)
err := hash.New(spec)
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/observer/observertesthelper/observer_test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func loadExporter(tb testing.TB, ctx context.Context, obs *observer.Observer, op
obs.RemoveListener(processManager)
})

cgrouprate.NewCgroupRate(ctx, processManager, base.CgroupRateMapExec, &option.Config.CgroupRate)
cgrouprate.NewCgroupRate(ctx, processManager, base.CgroupRateMap, &option.Config.CgroupRate)
return nil
}

Expand Down
12 changes: 3 additions & 9 deletions pkg/sensors/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ var (
StatsMap = program.MapBuilder("tg_stats_map", Execve)

/* Cgroup rate data, attached to execve sensor */
CgroupRateMapExec = program.MapBuilder("cgroup_rate_map", Execve)
CgroupRateMapExit = program.MapBuilder("cgroup_rate_map", Exit)
CgroupRateMapFork = program.MapBuilder("cgroup_rate_map", Fork)
CgroupRateMapCgroup = program.MapBuilder("cgroup_rate_map", CgroupRmdir)
CgroupRateMap = program.MapBuilder("cgroup_rate_map", Execve, Exit, Fork, CgroupRmdir)
CgroupRateOptionsMap = program.MapBuilder("cgroup_rate_options_map", Execve)

sensor = sensors.Sensor{
Expand Down Expand Up @@ -151,7 +148,7 @@ func GetDefaultMaps(cgroupRate bool) []*program.Map {
StatsMap,
}
if cgroupRate {
maps = append(maps, CgroupRateMapExec, CgroupRateOptionsMap)
maps = append(maps, CgroupRateMap, CgroupRateOptionsMap)
}
return maps

Expand Down Expand Up @@ -193,8 +190,5 @@ func ConfigCgroupRate(opts *option.CgroupRate) {
return
}

CgroupRateMapExec.SetMaxEntries(cgroupRateMaxEntries)
CgroupRateMapExit.SetMaxEntries(cgroupRateMaxEntries)
CgroupRateMapFork.SetMaxEntries(cgroupRateMaxEntries)
CgroupRateMapCgroup.SetMaxEntries(cgroupRateMaxEntries)
CgroupRateMap.SetMaxEntries(cgroupRateMaxEntries)
}
4 changes: 2 additions & 2 deletions pkg/sensors/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,11 @@ func (s *Sensor) loadMaps(bpfDir string) error {
return fmt.Errorf("map '%s' not found from '%s'", m.Name, m.Prog.Name)
}

if max, ok := m.Prog.MaxEntriesMap[mapSpec.Name]; ok {
if max, ok := m.GetMaxEntries(); ok {
mapSpec.MaxEntries = max
}

if innerMax, ok := m.Prog.MaxEntriesInnerMap[mapSpec.Name]; ok {
if innerMax, ok := m.GetMaxInnerEntries(); ok {
if innerMs := mapSpec.InnerMap; innerMs != nil {
mapSpec.InnerMap.MaxEntries = innerMax
}
Expand Down
29 changes: 17 additions & 12 deletions pkg/sensors/program/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,9 @@ func MultiKprobeAttach(load *Program, bpfDir string) AttachFunc {

func LoadTracepointProgram(bpfDir string, load *Program, verbose int) error {
var tc tailCall
for mName, mPath := range load.PinMap {
for mName, m := range load.PinMap {
if mName == "tp_calls" || mName == "execve_calls" {
tc = tailCall{mPath, "tracepoint"}
tc = tailCall{m.PinName, "tracepoint"}
break
}
}
Expand All @@ -502,9 +502,9 @@ func LoadRawTracepointProgram(bpfDir string, load *Program, verbose int) error {

func LoadKprobeProgram(bpfDir string, load *Program, verbose int) error {
var tc tailCall
for mName, mPath := range load.PinMap {
for mName, m := range load.PinMap {
if mName == "kprobe_calls" || mName == "retkprobe_calls" {
tc = tailCall{mPath, "kprobe"}
tc = tailCall{m.PinName, "kprobe"}
break
}
}
Expand Down Expand Up @@ -548,9 +548,9 @@ func LoadKprobeProgramAttachMany(bpfDir string, load *Program, syms []string, ve

func LoadUprobeProgram(bpfDir string, load *Program, verbose int) error {
var tc tailCall
for mName, mPath := range load.PinMap {
for mName, m := range load.PinMap {
if mName == "uprobe_calls" {
tc = tailCall{mPath, "uprobe"}
tc = tailCall{m.PinName, "uprobe"}
break
}
}
Expand All @@ -564,9 +564,9 @@ func LoadUprobeProgram(bpfDir string, load *Program, verbose int) error {

func LoadMultiKprobeProgram(bpfDir string, load *Program, verbose int) error {
var tc tailCall
for mName, mPath := range load.PinMap {
for mName, m := range load.PinMap {
if mName == "kprobe_calls" || mName == "retkprobe_calls" {
tc = tailCall{mPath, "kprobe"}
tc = tailCall{m.PinName, "kprobe"}
break
}
}
Expand Down Expand Up @@ -746,11 +746,16 @@ func doLoadProgram(
}

for _, ms := range spec.Maps {
if max, ok := load.MaxEntriesMap[ms.Name]; ok {
m, ok := load.PinMap[ms.Name]
if !ok {
continue
}

if max, ok := m.GetMaxEntries(); ok {
ms.MaxEntries = max
}

if innerMax, ok := load.MaxEntriesInnerMap[ms.Name]; ok {
if innerMax, ok := m.GetMaxInnerEntries(); ok {
if ms.InnerMap == nil {
return nil, fmt.Errorf("no inner map for %s", ms.Name)
}
Expand Down Expand Up @@ -783,8 +788,8 @@ func doLoadProgram(
var m *ebpf.Map
var err error
var mapPath string
if pinName, ok := load.PinMap[name]; ok {
mapPath = filepath.Join(bpfDir, pinName)
if pm, ok := load.PinMap[name]; ok {
mapPath = filepath.Join(bpfDir, pm.PinName)
} else {
mapPath = filepath.Join(bpfDir, name)
}
Expand Down
66 changes: 47 additions & 19 deletions pkg/sensors/program/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,49 @@ import (
"github.com/sirupsen/logrus"
)

// Map represents BPF maps.
type Map struct {
Name string
PinName string
Prog *Program
PinState State
MapHandle *ebpf.Map
}

func MapBuilder(name string, ld *Program) *Map {
return &Map{name, name, ld, Idle(), nil}
type MaxEntries struct {
Val uint32
Set bool
}

func MapBuilderPinManyProgs(name, pin string, lds ...*Program) *Map {
// Map represents BPF maps.
type Map struct {
Name string
PinName string
Prog *Program
PinState State
MapHandle *ebpf.Map
Entries MaxEntries
InnerEntries MaxEntries
}

// Map holds pointer to Program object as a source of its ebpf object
// file. We assume all the programs sharing the map have same map
// definition, so it's ok to use the first program if there's more.
//
// m.prog -> lds[0]
//
// Every program has PinMap map that links map name woth the map object,
// so the loader has all program's map object available.
//
// p.PinMap["map1"] = &map1
// p.PinMap["map2"] = &map2
// ...
// p.PinMap["mapX"] = &mapX
func mapBuilder(name, pin string, lds ...*Program) *Map {
m := &Map{name, pin, lds[0], Idle(), nil, MaxEntries{0, false}, MaxEntries{0, false}}
for _, ld := range lds {
ld.PinMap[name] = pin
ld.PinMap[name] = m
}
return &Map{name, pin, lds[0], Idle(), nil}
return m
}

func MapBuilderPin(name, pin string, ld *Program) *Map {
ld.PinMap[name] = pin
return &Map{name, pin, ld, Idle(), nil}
func MapBuilder(name string, lds ...*Program) *Map {
return mapBuilder(name, name, lds...)
}

func MapBuilderPin(name, pin string, lds ...*Program) *Map {
return mapBuilder(name, pin, lds...)
}

func (m *Map) Unload() error {
Expand Down Expand Up @@ -179,9 +199,17 @@ func LoadOrCreatePinnedMap(pinPath string, mapSpec *ebpf.MapSpec) (*ebpf.Map, er
}

func (m *Map) SetMaxEntries(max int) {
m.Prog.MaxEntriesMap[m.Name] = uint32(max)
m.Entries = MaxEntries{uint32(max), true}
}

func (m *Map) SetInnerMaxEntries(max int) {
m.Prog.MaxEntriesInnerMap[m.Name] = uint32(max)
m.InnerEntries = MaxEntries{uint32(max), true}
}

func (m *Map) GetMaxEntries() (uint32, bool) {
return m.Entries.Val, m.Entries.Set
}

func (m *Map) GetMaxInnerEntries() (uint32, bool) {
return m.InnerEntries.Val, m.InnerEntries.Set
}
33 changes: 14 additions & 19 deletions pkg/sensors/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,19 @@ func Builder(
ty string,
) *Program {
return &Program{
Name: objFile,
Attach: attach,
Label: label,
PinPath: pinFile,
RetProbe: false,
ErrorFatal: true,
Override: false,
Type: ty,
LoadState: Idle(),
LoaderData: struct{}{},
MapLoad: nil,
unloader: nil,
PinMap: make(map[string]string),
MaxEntriesMap: make(map[string]uint32),
MaxEntriesInnerMap: make(map[string]uint32),
Name: objFile,
Attach: attach,
Label: label,
PinPath: pinFile,
RetProbe: false,
ErrorFatal: true,
Override: false,
Type: ty,
LoadState: Idle(),
LoaderData: struct{}{},
MapLoad: nil,
unloader: nil,
PinMap: make(map[string]*Map),
}
}

Expand Down Expand Up @@ -105,13 +103,10 @@ type Program struct {
unloader unloader.Unloader
unloaderOverride unloader.Unloader

PinMap map[string]string
PinMap map[string]*Map

// available when program.KeepCollection is true
LC *LoadedCollection

MaxEntriesMap map[string]uint32
MaxEntriesInnerMap map[string]uint32
}

func (p *Program) SetRetProbe(ret bool) *Program {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sensors/tracing/enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func init() {
}

func enforcerMap(policyName string, load ...*program.Program) *program.Map {
return program.MapBuilderPinManyProgs(enforcerDataMapName,
return program.MapBuilderPin(enforcerDataMapName,
fmt.Sprintf("%s_%s", enforcerDataMapName, policyName), load...)
}

Expand Down
Loading