Skip to content

Commit

Permalink
Merge pull request #656 from eyakubovich/add-map-setters
Browse files Browse the repository at this point in the history
Add Resize() and GetMaxEntries() to BPFMap
  • Loading branch information
grantseltzer committed Apr 2, 2021
2 parents 7862e0e + 7ace63b commit 095336c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
27 changes: 27 additions & 0 deletions libbpfgo/libbpfgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,33 @@ func (b *BPFMap) SetPinPath(pinPath string) error {
return nil
}

// Resize changes the map's capacity to maxEntries.
// It should be called after the module was initialized but
// prior to it being loaded with BPFLoadObject.
// Note: for ring buffer and perf buffer, maxEntries is the
// capacity in bytes.
func (b *BPFMap) Resize(maxEntries uint32) error {
cs := C.CString(b.name)
bpfMap := C.bpf_object__find_map_by_name(b.module.obj, cs)
errC := C.bpf_map__resize(bpfMap, C.uint(maxEntries))
C.free(unsafe.Pointer(cs))
if errC != 0 {
return fmt.Errorf("failed to resize map %s to %v", b.name, maxEntries)
}
return nil
}

// GetMaxEntries returns the map's capacity.
// Note: for ring buffer and perf buffer, maxEntries is the
// capacity in bytes.
func (b *BPFMap) GetMaxEntries() uint32 {
cs := C.CString(b.name)
bpfMap := C.bpf_object__find_map_by_name(b.module.obj, cs)
maxEntries := C.bpf_map__max_entries(bpfMap)
C.free(unsafe.Pointer(cs))
return uint32(maxEntries)
}

func GetUnsafePointer(data interface{}) (unsafe.Pointer, error) {
var dataPtr unsafe.Pointer
if k, isType := data.(int8); isType {
Expand Down
21 changes: 21 additions & 0 deletions libbpfgo/selftest/perfbuffers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ import (
bpf "github.com/aquasecurity/tracee/libbpfgo"
)

func resizeMap(module *bpf.Module, name string, size uint32) error {
m, err := module.GetMap("events")
if err != nil {
return err
}

if err = m.Resize(size); err != nil {
return err
}

if actual := m.GetMaxEntries(); actual != size {
return fmt.Errorf("map resize failed, expected %v, actual %v", size, actual)
}

return nil
}

func main() {

bpfModule, err := bpf.NewModuleFromFile("self.bpf.o")
Expand All @@ -19,6 +36,10 @@ func main() {
}
defer bpfModule.Close()

if err = resizeMap(bpfModule, "events", 8192); err != nil {
os.Exit(-1)
}

bpfModule.BPFLoadObject()
prog, err := bpfModule.GetProgram("kprobe__sys_mmap")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion libbpfgo/selftest/ringbuffers/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ BPF_SRC := $(shell find . -type f -name '*.bpf.c')
PWD := $(shell pwd)

LIBBPF_HEADERS := /usr/include/bpf
LIBBPF_OBJ := /usr/lib64/libbpf.a
LIBBPF_OBJ := -lbpf

.PHONY: all
all: vmlinux.h $(TARGET) $(TARGET_BPF)
Expand Down
21 changes: 21 additions & 0 deletions libbpfgo/selftest/ringbuffers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ import (
bpf "github.com/aquasecurity/tracee/libbpfgo"
)

func resizeMap(module *bpf.Module, name string, size uint32) error {
m, err := module.GetMap("events")
if err != nil {
return err
}

if err = m.Resize(size); err != nil {
return err
}

if actual := m.GetMaxEntries(); actual != size {
return fmt.Errorf("map resize failed, expected %v, actual %v", size, actual)
}

return nil
}

func main() {

bpfModule, err := bpf.NewModuleFromFile("self.bpf.o")
Expand All @@ -19,6 +36,10 @@ func main() {
}
defer bpfModule.Close()

if err = resizeMap(bpfModule, "events", 8192); err != nil {
os.Exit(-1)
}

bpfModule.BPFLoadObject()
prog, err := bpfModule.GetProgram("kprobe__sys_mmap")
if err != nil {
Expand Down

0 comments on commit 095336c

Please sign in to comment.