Skip to content

Commit

Permalink
Merge main to rel-1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jayanthvn committed Jun 20, 2024
2 parents 5ab064c + 312ca16 commit 4f89097
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 15 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/stretchr/testify v1.9.0
github.com/vishvananda/netlink v1.1.0
go.uber.org/zap v1.27.0
golang.org/x/sys v0.19.0
golang.org/x/sys v0.20.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
Binary file removed pkg/elfparser/.elf.go.swp
Binary file not shown.
49 changes: 44 additions & 5 deletions pkg/elfparser/elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var sdkCache = cache.Get()
type BpfSDKClient interface {
IncreaseRlimit() error
LoadBpfFile(path, customizedPinPath string) (map[string]BpfData, map[string]ebpf_maps.BpfMap, error)
LoadBpfFileWithCustomData(inputData BpfCustomData) (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)
Expand All @@ -60,6 +61,12 @@ type BpfData struct {
Maps map[string]ebpf_maps.BpfMap // List of associated maps
}

type BpfCustomData struct {
FilePath string // Filepath for the BPF program
CustomPinPath string // PinPath
CustomMapSize map[string]int // Map of bpfMaps with custom size
}

type bpfSDKClient struct {
mapApi ebpf_maps.BpfMapAPIs
progApi ebpf_progs.BpfProgAPIs
Expand Down Expand Up @@ -124,9 +131,10 @@ func newElfLoader(elfFile *elf.File, bpfmapapi ebpf_maps.BpfMapAPIs, bpfprogapi
}

func (b *bpfSDKClient) LoadBpfFile(path, customizedPinPath string) (map[string]BpfData, map[string]ebpf_maps.BpfMap, error) {

bpfFile, err := os.Open(path)
if err != nil {
log.Infof("LoadBpfFile failed to open")
log.Errorf("LoadBpfFile failed to open")
return nil, nil, err
}
defer bpfFile.Close()
Expand All @@ -138,7 +146,30 @@ func (b *bpfSDKClient) LoadBpfFile(path, customizedPinPath string) (map[string]B

elfLoader := newElfLoader(elfFile, b.mapApi, b.progApi, customizedPinPath)

bpfLoadedProg, bpfLoadedMaps, err := elfLoader.doLoadELF()
bpfLoadedProg, bpfLoadedMaps, err := elfLoader.doLoadELF(BpfCustomData{})
if err != nil {
return nil, nil, err
}
return bpfLoadedProg, bpfLoadedMaps, nil
}

func (b *bpfSDKClient) LoadBpfFileWithCustomData(inputData BpfCustomData) (map[string]BpfData, map[string]ebpf_maps.BpfMap, error) {

bpfFile, err := os.Open(inputData.FilePath)
if err != nil {
log.Errorf("LoadBpfFileWithCustomData failed to open")
return nil, nil, err
}
defer bpfFile.Close()

elfFile, err := elf.NewFile(bpfFile)
if err != nil {
return nil, nil, err
}

elfLoader := newElfLoader(elfFile, b.mapApi, b.progApi, inputData.CustomPinPath)

bpfLoadedProg, bpfLoadedMaps, err := elfLoader.doLoadELF(inputData)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -362,7 +393,7 @@ func (e *elfLoader) getLicense() string {
return e.license
}

func (e *elfLoader) parseMap() ([]ebpf_maps.CreateEBPFMapInput, error) {
func (e *elfLoader) parseMap(customData BpfCustomData) ([]ebpf_maps.CreateEBPFMapInput, error) {
mapDefinitionSize := bpfMapDefSize
parsedMapData := []ebpf_maps.CreateEBPFMapInput{}

Expand Down Expand Up @@ -408,6 +439,13 @@ func (e *elfLoader) parseMap() ([]ebpf_maps.CreateEBPFMapInput, error) {
}
}
log.Infof("Found map name %s", mapData.Name)

if len(customData.CustomMapSize) != 0 {
//Update the MaxEntries
if customSize, ok := customData.CustomMapSize[mapData.Name]; ok {
mapData.MaxEntries = uint32(customSize)
}
}
parsedMapData = append(parsedMapData, mapData)
}
return parsedMapData, nil
Expand Down Expand Up @@ -583,16 +621,17 @@ func (e *elfLoader) parseProg(loadedMaps map[string]ebpf_maps.BpfMap) (map[strin

}

func (e *elfLoader) doLoadELF() (map[string]BpfData, map[string]ebpf_maps.BpfMap, error) {
func (e *elfLoader) doLoadELF(inputData BpfCustomData) (map[string]BpfData, map[string]ebpf_maps.BpfMap, error) {
var err error

//Parse all sections
if err := e.parseSection(); err != nil {
fmt.Println(err)
return nil, nil, fmt.Errorf("failed to parse sections in elf file")
}

//Parse Map
parsedMapData, err := e.parseMap()
parsedMapData, err := e.parseMap(inputData)
if err != nil {
return nil, nil, fmt.Errorf("failed to parse maps")
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/elfparser/elf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestLoad(t *testing.T) {
elfFile, err := elf.NewFile(f)
assert.NoError(t, err)
elfLoader := newElfLoader(elfFile, m.ebpf_maps, m.ebpf_progs, "test")
loadedProgs, loadedMaps, err := elfLoader.doLoadELF()
loadedProgs, loadedMaps, err := elfLoader.doLoadELF(BpfCustomData{})
assert.NoError(t, err)
assert.Equal(t, tt.wantProg, len(loadedProgs))
assert.Equal(t, tt.wantMap, len(loadedMaps))
Expand Down Expand Up @@ -334,7 +334,7 @@ func TestParseMap(t *testing.T) {

err = elfLoader.parseSection()
assert.NoError(t, err)
mapData, err := elfLoader.parseMap()
mapData, err := elfLoader.parseMap(BpfCustomData{})
if tt.wantErr != nil {
assert.EqualError(t, err, tt.wantErr.Error())
} else {
Expand Down Expand Up @@ -388,7 +388,7 @@ func TestParseMap(t *testing.T) {
copiedMapSection.SectionHeader = dummySection.SectionHeader
elfLoader.mapSection = &copiedMapSection
}
mapData, err := elfLoader.parseMap()
mapData, err := elfLoader.parseMap(BpfCustomData{})
if tt.wantErr != nil {
assert.EqualError(t, err, tt.wantErr.Error())
} else {
Expand Down Expand Up @@ -456,7 +456,7 @@ func TestParseProg(t *testing.T) {
err = elfLoader.parseSection()
assert.NoError(t, err)

mapData, err := elfLoader.parseMap()
mapData, err := elfLoader.parseMap(BpfCustomData{})
assert.NoError(t, err)

m.ebpf_maps.EXPECT().CreateBPFMap(gomock.Any()).AnyTimes()
Expand Down
2 changes: 1 addition & 1 deletion test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/vishvananda/netns v0.0.4 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/sys v0.20.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
)

Expand Down
4 changes: 2 additions & 2 deletions test/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
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.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.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=
24 changes: 24 additions & 0 deletions test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func main() {
{Name: "Test loading TC filter", Func: TestLoadTCfilter},
{Name: "Test loading Maps without Program", Func: TestLoadMapWithNoProg},
{Name: "Test loading Map operations", Func: TestMapOperations},
{Name: "Test updating Map size", Func: TestLoadMapWithCustomSize},
}

testSummary := make(map[string]string)
Expand Down Expand Up @@ -316,3 +317,26 @@ func TestLoadTCfilter() error {
}
return nil
}

func TestLoadMapWithCustomSize() error {
gosdkClient := goelf.New()

var customData goelf.BpfCustomData
customData.FilePath = "c/test-map.bpf.elf"
customData.CustomPinPath = "test"
customData.CustomMapSize = make(map[string]int)
customData.CustomMapSize["ingress_map"] = 1024

_, loadedMap, err := gosdkClient.LoadBpfFileWithCustomData(customData)
if err != nil {
fmt.Println("Load BPF failed", "err:", err)
return err
}

for mapName, mapData := range loadedMap {
fmt.Println("Map Info: ", "Name: ", mapName)
fmt.Println("Map Info: ", "Size: ", mapData.MapMetaData.MaxEntries)
}
return nil

}

0 comments on commit 4f89097

Please sign in to comment.