Skip to content

Commit

Permalink
pkg/bugtool: fix walking bpf fs using new c/ebpf pin pkg
Browse files Browse the repository at this point in the history
PR cilium/ebpf#1626 added the new pin package that simplifies a lot how
one can parse a bpf fs. I just love it.

Signed-off-by: Mahe Tardy <[email protected]>
  • Loading branch information
mtardy committed Dec 20, 2024
1 parent 9c44714 commit 4c31e33
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 53 deletions.
73 changes: 20 additions & 53 deletions pkg/bugtool/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import (
"iter"
"maps"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"syscall"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/pin"
"github.com/cilium/tetragon/pkg/bpf"
)

Expand Down Expand Up @@ -90,27 +89,20 @@ func FindAllMaps() ([]bpf.ExtendedMapInfo, error) {
// specified as argument.
func FindPinnedMaps(path string) ([]bpf.ExtendedMapInfo, error) {
var infos []bpf.ExtendedMapInfo
err := filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {

err := pin.WalkDir(path, func(path string, d fs.DirEntry, obj pin.Pinner, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil // skip directories
}
m, err := ebpf.LoadPinnedMap(path, nil)
if err != nil {
return fmt.Errorf("failed to load pinned map %q: %w", path, err)
}
defer m.Close()

// check if it's really a map because ebpf.LoadPinnedMap does not return
// an error but garbage info on doing this on a prog
if ok, err := isMap(m.FD()); err != nil || !ok {
if err != nil {
return err
}
m, ok := obj.(*ebpf.Map)
if !ok {
return nil // skip non map
}
defer m.Close()

xInfo, err := bpf.ExtendedInfoFromMap(m)
if err != nil {
Expand Down Expand Up @@ -153,58 +145,33 @@ func mapIDsFromProgs(prog *ebpf.Program) (iter.Seq[int], error) {
func mapIDsFromPinnedProgs(path string) (iter.Seq[int], error) {
mapSet := map[int]bool{}
progArrays := []*ebpf.Map{}
err := filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
err := pin.WalkDir(path, func(path string, d fs.DirEntry, obj pin.Pinner, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil // skip directories
}
if strings.HasSuffix(path, "/link") || strings.HasSuffix(path, "/link_override") {
return nil // skip BPF links, they make the syscall fail since cilium/ebpf@78074c59
}
prog, err := ebpf.LoadPinnedProgram(path, nil)
if err != nil {
return fmt.Errorf("failed to load pinned object %q: %w", path, err)
}
defer prog.Close()

if ok, err := isProg(prog.FD()); err != nil || !ok {
switch typedObj := obj.(type) {
case *ebpf.Program:
newIDs, err := mapIDsFromProgs(typedObj)
if err != nil {
return err
return fmt.Errorf("failed to retrieve map IDs from prog: %w", err)
}

// we want to keep a ref to prog array containing tail calls to
// search reference to map inside
ok, err := isMap(prog.FD())
if err != nil {
return err
typedObj.Close()
for id := range newIDs {
mapSet[id] = true
}
if ok {
m, err := ebpf.LoadPinnedMap(path, &ebpf.LoadPinOptions{
ReadOnly: true,
})
if err != nil {
return fmt.Errorf("failed to load pinned map %q: %w", path, err)
}
if m.Type() == ebpf.ProgramArray {
progArrays = append(progArrays, m)
// don't forget to close those files when used later on
} else {
m.Close()
}
case *ebpf.Map:
if typedObj.Type() == ebpf.ProgramArray {
progArrays = append(progArrays, typedObj)
// don't forget to close those files when used later on
} else {
typedObj.Close()
}

return nil // skip the non-prog
}

newIDs, err := mapIDsFromProgs(prog)
if err != nil {
return fmt.Errorf("failed to retrieve map IDs from prog: %w", err)
}
for id := range newIDs {
mapSet[id] = true
}
return nil
})
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions vendor/github.com/cilium/ebpf/pin/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions vendor/github.com/cilium/ebpf/pin/load.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions vendor/github.com/cilium/ebpf/pin/walk.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ github.com/cilium/ebpf/internal/tracefs
github.com/cilium/ebpf/internal/unix
github.com/cilium/ebpf/link
github.com/cilium/ebpf/perf
github.com/cilium/ebpf/pin
github.com/cilium/ebpf/rlimit
# github.com/cilium/little-vm-helper v0.0.19
## explicit; go 1.22.0
Expand Down

0 comments on commit 4c31e33

Please sign in to comment.