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

Remove cache dependency for getAll CLI #64

Merged
merged 5 commits into from
Dec 5, 2023
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
Binary file added pkg/elfparser/.elf.go.swp
Binary file not shown.
174 changes: 160 additions & 14 deletions pkg/elfparser/elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type BpfSDKClient interface {
LoadBpfFile(path, customizedPinPath string) (map[string]BpfData, map[string]ebpf_maps.BpfMap, error)
RecoverGlobalMaps() (map[string]ebpf_maps.BpfMap, error)
RecoverAllBpfProgramsAndMaps() (map[string]BpfData, error)
GetAllBpfProgramsAndMaps() (map[string]BpfData, error)
}

type BpfData struct {
Expand Down Expand Up @@ -641,16 +642,16 @@ func (e *elfLoader) doLoadELF() (map[string]BpfData, map[string]ebpf_maps.BpfMap

func GetMapNameFromBPFPinPath(pinPath string) (string, string) {

splitedPinPath := strings.Split(pinPath, "/")
podIdentifier := strings.SplitN(splitedPinPath[len(splitedPinPath)-1], "_", 2)
splittedPinPath := strings.Split(pinPath, "/")
podIdentifier := strings.SplitN(splittedPinPath[len(splittedPinPath)-1], "_", 2)
log.Infof("Found Identified - %s : %s", podIdentifier[0], podIdentifier[1])

mapNamespace := podIdentifier[0]
mapName := podIdentifier[1]

log.Infof("Found -> ", mapNamespace, mapName)

directionIdentifier := strings.Split(splitedPinPath[len(splitedPinPath)-1], "_")
directionIdentifier := strings.Split(splittedPinPath[len(splittedPinPath)-1], "_")
direction := directionIdentifier[1]

if direction == "ingress" {
Expand Down Expand Up @@ -732,7 +733,7 @@ func (b *bpfSDKClient) RecoverGlobalMaps() (map[string]ebpf_maps.BpfMap, error)
}
return nil
}); err != nil {
log.Infof("Error walking bpfdirectory:", err)
log.Infof("Error walking bpf map directory:", err)
return nil, fmt.Errorf("error walking the bpfdirectory %v", err)
}
} else {
Expand Down Expand Up @@ -806,7 +807,7 @@ func (b *bpfSDKClient) RecoverAllBpfProgramsAndMaps() (map[string]BpfData, error
}
return nil
}); err != nil {
log.Infof("Error walking bpfdirectory:", err)
log.Infof("Error walking bpf map directory:", err)
return nil, fmt.Errorf("failed walking the bpfdirectory %v", err)
}
}
Expand All @@ -822,24 +823,22 @@ func (b *bpfSDKClient) RecoverAllBpfProgramsAndMaps() (map[string]BpfData, error
pgmData := ebpf_progs.BpfProgram{
PinPath: pinPath,
}
splitedPinPath := strings.Split(pinPath, "/")
podIdentifier := strings.SplitN(splitedPinPath[len(splitedPinPath)-1], "_", 2)
splittedPinPath := strings.Split(pinPath, "/")
podIdentifier := strings.SplitN(splittedPinPath[len(splittedPinPath)-1], "_", 2)
log.Infof("Found Identified - %s : %s", podIdentifier[0], podIdentifier[1])

mapNamespace := podIdentifier[0]
if mapNamespace == "global" {
progNamespace := podIdentifier[0]
if progNamespace == "global" {
log.Infof("Skipping global progs")
return nil
}

//mapData := [string]ebpf_maps.BPFMap{}
bpfProgInfo, progFD, err := (b.progApi).GetProgFromPinPath(pinPath)
if err != nil {
log.Infof("Failed to progInfo for pinPath %s", pinPath)
return err
}
pgmData.ProgFD = progFD
//Conv type to string here

recoveredMapData := make(map[string]ebpf_maps.BpfMap)
if bpfProgInfo.NrMapIDs > 0 {
Expand All @@ -857,9 +856,9 @@ func (b *bpfSDKClient) RecoverAllBpfProgramsAndMaps() (map[string]BpfData, error
//Fill BPF map
recoveredBpfMap.MapID = uint32(newMapID)

mapIds, ok := mapPodSelector[mapNamespace]
mapIds, ok := mapPodSelector[progNamespace]
if !ok {
log.Infof("Failed to ID for %s", mapNamespace)
log.Infof("Failed to get ID for %s", progNamespace)
return fmt.Errorf("failed to get err")
}
mapName := mapIds[int(recoveredBpfMap.MapID)]
Expand All @@ -874,7 +873,7 @@ func (b *bpfSDKClient) RecoverAllBpfProgramsAndMaps() (map[string]BpfData, error
//Fill New FD since old FDs will be deleted on recovery
localMapFD, ok := mapIDsToFDs[int(newMapID)]
if !ok {
log.Infof("Unable to Get FD from ID %d", int(newMapID))
log.Infof("Unable to get FD from ID %d", int(newMapID))
return fmt.Errorf("unable to get FD")
}
mapFD = localMapFD
Expand Down Expand Up @@ -903,6 +902,153 @@ func (b *bpfSDKClient) RecoverAllBpfProgramsAndMaps() (map[string]BpfData, error
loadedPrograms[pinPath] = recoveredBPFdata
}
return nil
}); err != nil {
log.Infof("Error walking bpf prog directory:", err)
return nil, fmt.Errorf("failed walking the bpfdirectory %v", err)
}
}
} else {
log.Infof("error checking BPF FS, please make sure it is mounted %v", err)
return nil, fmt.Errorf("error checking BPF FS, please make sure it is mounted")
}
//Return DS here
return loadedPrograms, nil
}

func (b *bpfSDKClient) GetAllBpfProgramsAndMaps() (map[string]BpfData, error) {
_, err := os.Stat(constdef.BPF_DIR_MNT)
if err != nil {
log.Infof("BPF FS directory is not present")
return nil, fmt.Errorf("eBPF FS directory is not present %v", err)
}

var statfs syscall.Statfs_t

//Pass DS here
loadedPrograms := make(map[string]BpfData)
mapIDsToNames := make(map[int]string)
mapPodSelector := make(map[string]map[int]string)

mapsDirExists := true
progsDirExists := true
_, err = os.Stat(constdef.MAP_BPF_FS)
if err != nil {
mapsDirExists = false
}

_, err = os.Stat(constdef.PROG_BPF_FS)
if err != nil {
progsDirExists = false
}

if err := syscall.Statfs(constdef.BPF_DIR_MNT, &statfs); err == nil && statfs.Type == unix.BPF_FS_MAGIC {
if mapsDirExists {
if err := filepath.Walk(constdef.MAP_BPF_FS, func(pinPath string, fsinfo os.FileInfo, err error) error {
if err != nil {
return err
}
if !fsinfo.IsDir() {
log.Infof("Dumping pinpaths - ", pinPath)

bpfMapInfo, err := b.mapApi.GetMapFromPinPath(pinPath)
if err != nil {
log.Infof("error getting mapInfo for pin path, this shouldn't happen")
return err
}
mapID := bpfMapInfo.Id
log.Infof("Got ID %d", mapID)
//Get map name
mapName, mapNamespace := GetMapNameFromBPFPinPath(pinPath)
mapIDsToNames[int(mapID)] = mapName

log.Infof("Adding ID %d to name %s and NS %s", mapID, mapName, mapNamespace)
mapPodSelector[mapNamespace] = mapIDsToNames
}
return nil
}); err != nil {
log.Infof("Error walking bpfdirectory:", err)
jayanthvn marked this conversation as resolved.
Show resolved Hide resolved
return nil, fmt.Errorf("failed walking the bpfdirectory %v", err)
}
}

if progsDirExists {
if err := filepath.Walk(constdef.PROG_BPF_FS, func(pinPath string, fsinfo os.FileInfo, err error) error {
if err != nil {
return err
}
if !fsinfo.IsDir() {
log.Infof("Dumping pinpaths - ", pinPath)

pgmData := ebpf_progs.BpfProgram{
PinPath: pinPath,
}
splittedPinPath := strings.Split(pinPath, "/")
podIdentifier := strings.SplitN(splittedPinPath[len(splittedPinPath)-1], "_", 2)
log.Infof("Found Identified - %s : %s", podIdentifier[0], podIdentifier[1])

mapNamespace := podIdentifier[0]
jayanthvn marked this conversation as resolved.
Show resolved Hide resolved
if mapNamespace == "global" {
log.Infof("Skipping global progs")
return nil
}

bpfProgInfo, progFD, err := (b.progApi).GetProgFromPinPath(pinPath)
if err != nil {
log.Infof("Failed to progInfo for pinPath %s", pinPath)
return err
}
pgmData.ProgID = int(bpfProgInfo.ID)
//Conv type to string here

recoveredMapData := make(map[string]ebpf_maps.BpfMap)
if bpfProgInfo.NrMapIDs > 0 {
log.Infof("Have associated maps to link")
associatedBpfMapList, associatedBPFMapIDs, err := ebpf_progs.BpfGetMapInfoFromProgInfo(progFD, bpfProgInfo.NrMapIDs)
if err != nil {
log.Infof("Failed to get associated maps")
return err
}
//Close progFD..we don't need it
unix.Close(progFD)
for mapInfoIdx := 0; mapInfoIdx < len(associatedBpfMapList); mapInfoIdx++ {
bpfMapInfo := associatedBpfMapList[mapInfoIdx]
newMapID := associatedBPFMapIDs[mapInfoIdx]
recoveredBpfMap := ebpf_maps.BpfMap{}

//Fill BPF map
recoveredBpfMap.MapID = uint32(newMapID)

mapIds, ok := mapPodSelector[mapNamespace]
if !ok {
log.Infof("Failed to ID for %s", mapNamespace)
jayanthvn marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("failed to get err")
}
mapName := mapIds[int(recoveredBpfMap.MapID)]

recoveredBpfMap.MapFD = 0

log.Infof("Mapinfo MapName - %v", bpfMapInfo.Name)
//Fill BPF map metadata
recoveredBpfMapMetaData := ebpf_maps.CreateEBPFMapInput{
Type: bpfMapInfo.Type,
KeySize: bpfMapInfo.KeySize,
ValueSize: bpfMapInfo.ValueSize,
MaxEntries: bpfMapInfo.MaxEntries,
Flags: bpfMapInfo.MapFlags,
Name: mapName,
}
recoveredBpfMap.MapMetaData = recoveredBpfMapMetaData
recoveredMapData[mapName] = recoveredBpfMap
}

}
recoveredBPFdata := BpfData{
Program: pgmData,
Maps: recoveredMapData,
}
loadedPrograms[pinPath] = recoveredBPFdata
}
return nil
}); err != nil {
log.Infof("Error walking bpfdirectory:", err)
return nil, fmt.Errorf("failed walking the bpfdirectory %v", err)
Expand Down
4 changes: 2 additions & 2 deletions test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ require (
github.com/vishvananda/netlink v1.1.0 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.25.0 // indirect
golang.org/x/sys v0.12.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/sys v0.14.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
)

Expand Down
9 changes: 4 additions & 5 deletions test/go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
Expand All @@ -18,12 +17,12 @@ github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZla
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c=
go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=