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
Changes from 1 commit
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
148 changes: 148 additions & 0 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 @@ -915,3 +916,150 @@ func (b *bpfSDKClient) RecoverAllBpfProgramsAndMaps() (map[string]BpfData, error
//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,
}
splitedPinPath := strings.Split(pinPath, "/")
podIdentifier := strings.SplitN(splitedPinPath[len(splitedPinPath)-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)
}
}
} 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
}
Loading