diff --git a/pkg/elfparser/.elf.go.swp b/pkg/elfparser/.elf.go.swp deleted file mode 100644 index 37af116..0000000 Binary files a/pkg/elfparser/.elf.go.swp and /dev/null differ diff --git a/pkg/elfparser/elf.go b/pkg/elfparser/elf.go index e1eb3bc..21e8c3d 100644 --- a/pkg/elfparser/elf.go +++ b/pkg/elfparser/elf.go @@ -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) @@ -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 @@ -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() @@ -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 } @@ -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{} @@ -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 @@ -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") } diff --git a/pkg/elfparser/elf_test.go b/pkg/elfparser/elf_test.go index 056feb6..b28b0a8 100644 --- a/pkg/elfparser/elf_test.go +++ b/pkg/elfparser/elf_test.go @@ -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)) @@ -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 { @@ -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 { @@ -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() diff --git a/test/go.mod b/test/go.mod index 4615013..0f9c9fe 100644 --- a/test/go.mod +++ b/test/go.mod @@ -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 ) diff --git a/test/go.sum b/test/go.sum index be5847e..90aaa7e 100644 --- a/test/go.sum +++ b/test/go.sum @@ -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= diff --git a/test/main.go b/test/main.go index 4ef04d9..338254c 100644 --- a/test/main.go +++ b/test/main.go @@ -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) @@ -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 + +}