Skip to content

Commit

Permalink
Make map size configurable (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
jayanthvn authored Jun 20, 2024
1 parent 87d761c commit 312ca16
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 17 deletions.
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
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.26.0 // indirect
golang.org/x/sys v0.15.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/sys v0.20.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
)

Expand Down
12 changes: 6 additions & 6 deletions test/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJH8j0=
github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE=
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU=
github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8=
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
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.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
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.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.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 312ca16

Please sign in to comment.