Skip to content

Commit

Permalink
tetragon: Move loop out of loadMaps function
Browse files Browse the repository at this point in the history
Moving loop out of loadMaps function, it will ease up following
patch, there's no functional change.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
  • Loading branch information
olsajiri committed Oct 3, 2024
1 parent 6154d19 commit 78d10e0
Showing 1 changed file with 60 additions and 60 deletions.
120 changes: 60 additions & 60 deletions pkg/sensors/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@ func (s *Sensor) Load(bpfDir string) (err error) {
return fmt.Errorf("tetragon, aborting could not find BPF programs: %w", err)
}

if err = s.loadMaps(bpfDir); err != nil {
return fmt.Errorf("tetragon, aborting could not load sensor BPF maps: %w", err)
for _, m := range s.Maps {
if err = s.loadMap(bpfDir, m); err != nil {
return fmt.Errorf("tetragon, aborting could not load sensor BPF maps: %w", err)
}
}

for _, p := range s.Progs {
Expand Down Expand Up @@ -273,76 +275,74 @@ func (s *Sensor) setMapPinPath(m *program.Map) {
}
}

// loadMaps loads all the BPF maps in the sensor.
func (s *Sensor) loadMaps(bpfDir string) error {
// loadMap loads BPF map in the sensor.
func (s *Sensor) loadMap(bpfDir string, m *program.Map) error {
l := logger.GetLogger()
for _, m := range s.Maps {
if m.PinState.IsLoaded() {
l.WithFields(logrus.Fields{
"sensor": s.Name,
"map": m.Name,
}).Info("map is already loaded, incrementing reference count")
m.PinState.RefInc()
continue
}

spec, err := ebpf.LoadCollectionSpec(m.Prog.Name)
if err != nil {
return fmt.Errorf("failed to open collection '%s': %w", m.Prog.Name, err)
}
mapSpec, ok := spec.Maps[m.Name]
if !ok {
return fmt.Errorf("map '%s' not found from '%s'", m.Name, m.Prog.Name)
}
if m.PinState.IsLoaded() {
l.WithFields(logrus.Fields{
"sensor": s.Name,
"map": m.Name,
}).Info("map is already loaded, incrementing reference count")
m.PinState.RefInc()
return nil
}

s.setMapPinPath(m)
pinPath := filepath.Join(bpfDir, m.PinPath)
spec, err := ebpf.LoadCollectionSpec(m.Prog.Name)
if err != nil {
return fmt.Errorf("failed to open collection '%s': %w", m.Prog.Name, err)
}
mapSpec, ok := spec.Maps[m.Name]
if !ok {
return fmt.Errorf("map '%s' not found from '%s'", m.Name, m.Prog.Name)
}

if m.IsOwner() {
// If map is the owner we set configured max entries
// directly to map spec.
if max, ok := m.GetMaxEntries(); ok {
mapSpec.MaxEntries = max
}
s.setMapPinPath(m)
pinPath := filepath.Join(bpfDir, m.PinPath)

if innerMax, ok := m.GetMaxInnerEntries(); ok {
if innerMs := mapSpec.InnerMap; innerMs != nil {
mapSpec.InnerMap.MaxEntries = innerMax
}
}
} else {
// If map is NOT the owner we follow the max entries
// of the pinned map and update the spec with that.
max, err := program.GetMaxEntriesPinnedMap(pinPath)
if err != nil {
return err
}
if m.IsOwner() {
// If map is the owner we set configured max entries
// directly to map spec.
if max, ok := m.GetMaxEntries(); ok {
mapSpec.MaxEntries = max
}

// 'm' is not the owner but for some reason requires max
// entries setup, make sure it matches the pinned map.
if max, ok := m.GetMaxEntries(); ok {
if mapSpec.MaxEntries != max {
return fmt.Errorf("failed to load map '%s' max entries mismatch: %d %d",
m.Name, mapSpec.MaxEntries, max)
}
if innerMax, ok := m.GetMaxInnerEntries(); ok {
if innerMs := mapSpec.InnerMap; innerMs != nil {
mapSpec.InnerMap.MaxEntries = innerMax
}

m.SetMaxEntries(int(max))
}

if err := m.LoadOrCreatePinnedMap(pinPath, mapSpec); err != nil {
return fmt.Errorf("failed to load map '%s' for sensor '%s': %w", m.Name, s.Name, err)
} else {
// If map is NOT the owner we follow the max entries
// of the pinned map and update the spec with that.
max, err := program.GetMaxEntriesPinnedMap(pinPath)
if err != nil {
return err
}
mapSpec.MaxEntries = max

// 'm' is not the owner but for some reason requires max
// entries setup, make sure it matches the pinned map.
if max, ok := m.GetMaxEntries(); ok {
if mapSpec.MaxEntries != max {
return fmt.Errorf("failed to load map '%s' max entries mismatch: %d %d",
m.Name, mapSpec.MaxEntries, max)
}
}

l.WithFields(logrus.Fields{
"sensor": s.Name,
"map": m.Name,
"path": pinPath,
"max": m.Entries,
}).Info("tetragon, map loaded.")
m.SetMaxEntries(int(max))
}

if err := m.LoadOrCreatePinnedMap(pinPath, mapSpec); err != nil {
return fmt.Errorf("failed to load map '%s' for sensor '%s': %w", m.Name, s.Name, err)
}

l.WithFields(logrus.Fields{
"sensor": s.Name,
"map": m.Name,
"path": pinPath,
"max": m.Entries,
}).Info("tetragon, map loaded.")

return nil
}

Expand Down

0 comments on commit 78d10e0

Please sign in to comment.