Skip to content

Commit

Permalink
added GetBPFmapInfo method to support PIN_NONE where pinpath does not…
Browse files Browse the repository at this point in the history
… exist
  • Loading branch information
nithmu committed Nov 4, 2024
1 parent 30604a6 commit 76b211d
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 5 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/tools v0.1.1 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand All @@ -38,9 +39,11 @@ 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=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1 h1:wGiQel/hW0NnEkJUk8lbzkX2gFJU6PFxf1v5OlCfuOs=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
18 changes: 13 additions & 5 deletions pkg/elfparser/elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,20 @@ func (e *elfLoader) loadMap(parsedMapData []ebpf_maps.CreateEBPFMapInput) (map[s
}

//Fill ID
mapInfo, err := (e.bpfMapApi).GetMapFromPinPath(pinPath)
if err != nil {
return nil, fmt.Errorf("map '%s' doesn't exist", mapNameStr)
if loadedMaps.PinOptions.Type == constdef.PIN_NONE.Index() {
mapInfo, err := (e.bpfMapApi).GetBPFmapInfo(bpfMap.MapFD)
if err != nil {
return nil, fmt.Errorf("failed to get map info '%s'", mapNameStr)
}
bpfMap.MapID = uint32(mapInfo.Id)
} else {
mapInfo, err := (e.bpfMapApi).GetMapFromPinPath(pinPath)
if err != nil {
return nil, fmt.Errorf("map '%s' doesn't exist", mapNameStr)
}
mapID := uint32(mapInfo.Id)
bpfMap.MapID = mapID
}
mapID := uint32(mapInfo.Id)
bpfMap.MapID = mapID

programmedMaps[loadedMaps.Name] = bpfMap

Expand Down
86 changes: 86 additions & 0 deletions pkg/elfparser/elf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package elfparser
import (
"debug/elf"
"errors"
"fmt"
ebpf_maps "github.com/aws/aws-ebpf-sdk-go/pkg/maps"
"os"
"sort"
"strings"
Expand Down Expand Up @@ -668,3 +670,87 @@ func TestProgType(t *testing.T) {
})
}
}

func TestLoadMap(t *testing.T) {
tests := []struct {
name string
pinType uint32
mapFD uint32
mapInfo ebpf_maps.BpfMapInfo
wantMapID uint32
wantErr bool
getInfoErr error
pinPath string
}{
{
name: "Successful retrieval of map info",
pinType: constdef.PIN_NONE.Index(),
mapFD: 10,
mapInfo: ebpf_maps.BpfMapInfo{Id: 12345},
wantMapID: 12345,
wantErr: false,
},
{
name: "Map retrieval error",
pinType: constdef.PIN_NONE.Index(),
mapFD: 20,
getInfoErr: fmt.Errorf("failed to get map info"),
wantErr: true,
},
{
name: "Pinned map retrieval from path",
pinType: constdef.PIN_GLOBAL_NS.Index(),
mapFD: 30,
mapInfo: ebpf_maps.BpfMapInfo{Id: 54321},
wantMapID: 54321,
wantErr: false,
pinPath: "/sys/fs/bpf/globals/aws/maps/test_map",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockBpfMapAPI := mock_ebpf_maps.NewMockBpfMapAPIs(ctrl)
mockBpfProgAPI := mock_ebpf_progs.NewMockBpfProgAPIs(ctrl)

// Mock CreateBPFMap to return a BpfMap with MapFD set to tt.mapFD
mockBpfMapAPI.EXPECT().CreateBPFMap(gomock.Any()).Return(ebpf_maps.BpfMap{MapFD: tt.mapFD}, nil).AnyTimes()

// Mock GetBPFmapInfo or GetMapFromPinPath based on the pin type and error expectation
if tt.getInfoErr != nil {
mockBpfMapAPI.EXPECT().GetBPFmapInfo(tt.mapFD).Return(ebpf_maps.BpfMapInfo{}, tt.getInfoErr)
} else if tt.pinType == constdef.PIN_NONE.Index() {
mockBpfMapAPI.EXPECT().GetBPFmapInfo(tt.mapFD).Return(tt.mapInfo, nil)
} else {
mockBpfMapAPI.EXPECT().GetMapFromPinPath(tt.pinPath).Return(tt.mapInfo, nil)
}

// Set up the loader and the map input
elfLoader := &elfLoader{
bpfMapApi: mockBpfMapAPI,
bpfProgApi: mockBpfProgAPI,
}
parsedMapData := []ebpf_maps.CreateEBPFMapInput{
{
Name: "test_map",
PinOptions: &ebpf_maps.BpfMapPinOptions{Type: tt.pinType, PinPath: tt.pinPath},
},
}

loadedMaps, err := elfLoader.loadMap(parsedMapData)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
if loadedMap, exists := loadedMaps["test_map"]; exists {
assert.Equal(t, tt.wantMapID, loadedMap.MapID)
} else {
t.Errorf("Expected map 'test_map' to be loaded")
}
}
})
}
}
6 changes: 6 additions & 0 deletions pkg/maps/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ type BpfMapAPIs interface {
BulkRefreshMapEntries(newMapContents map[string][]byte) error
// Retrieve map info from pin path
GetMapFromPinPath(pinPath string) (BpfMapInfo, error)
// Retrieve map info without pin path
GetBPFmapInfo(mapFD uint32) (BpfMapInfo, error)
}

func (m *BpfMap) CreateBPFMap(MapMetaData CreateEBPFMapInput) (BpfMap, error) {
Expand Down Expand Up @@ -542,6 +544,10 @@ func GetIDFromFD(mapFD int) (int, error) {
return int(mapInfo.Id), nil
}

func (m *BpfMap) GetBPFmapInfo(mapFD uint32) (BpfMapInfo, error) {
return GetBPFmapInfo(int(mapFD))
}

func GetBPFmapInfo(mapFD int) (BpfMapInfo, error) {
var bpfMapInfo BpfMapInfo
objInfo := BpfObjGetInfo{
Expand Down
15 changes: 15 additions & 0 deletions pkg/maps/mocks/ebpf_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 76b211d

Please sign in to comment.