Skip to content

Commit

Permalink
Introduce SetKeySize, Autocreate and SetAutocreate
Browse files Browse the repository at this point in the history
Co-authored-by: sc07kvm <[email protected]>
  • Loading branch information
sc07kvm and sc07kvm authored Oct 23, 2023
1 parent 0f666f8 commit 7a58b8e
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 3 deletions.
28 changes: 25 additions & 3 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,16 @@ func (m *BPFMap) KeySize() int {
return int(C.bpf_map__key_size(m.bpfMap))
}

// TODO: implement `bpf_map__set_key_size` wrapper
// func (m *BPFMap) SetKeySize(size uint32) error {
// }
// SetKeySize sets the key size to a BPFMap instance that is not yet associated
// with a file descriptor.
func (m *BPFMap) SetKeySize(size uint32) error {
retC := C.bpf_map__set_key_size(m.bpfMap, C.uint(size))
if retC < 0 {
return fmt.Errorf("could not set map key size: %w", syscall.Errno(-retC))
}

return nil
}

func (m *BPFMap) ValueSize() int {
return int(C.bpf_map__value_size(m.bpfMap))
Expand All @@ -168,6 +175,21 @@ func (m *BPFMap) SetValueSize(size uint32) error {
return nil
}

func (m *BPFMap) Autocreate() bool {
return bool(C.bpf_map__autocreate(m.bpfMap))
}

// Autocreate sets whether libbpf has to auto-create BPF map during BPF object
// load phase.
func (m *BPFMap) SetAutocreate(autocreate bool) error {
retC := C.bpf_map__set_autocreate(m.bpfMap, C.bool(autocreate))
if retC < 0 {
return fmt.Errorf("could not set map autocreate: %w", syscall.Errno(-retC))
}

return nil
}

func (m *BPFMap) BTFKeyTypeID() uint32 {
return uint32(C.bpf_map__btf_key_type_id(m.bpfMap))
}
Expand Down
1 change: 1 addition & 0 deletions selftest/map-autocreate/Makefile
7 changes: 7 additions & 0 deletions selftest/map-autocreate/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/aquasecurity/libbpfgo/selftest/map-update

go 1.18

require github.com/aquasecurity/libbpfgo v0.4.7-libbpf-1.2.0-b2e29a1

replace github.com/aquasecurity/libbpfgo => ../../
4 changes: 4 additions & 0 deletions selftest/map-autocreate/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
11 changes: 11 additions & 0 deletions selftest/map-autocreate/main.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//+build ignore

#include <vmlinux.h>

#include <bpf/bpf_helpers.h>

struct {
__uint(type, ~0U);
} tester SEC(".maps");

char LICENSE[] SEC("license") = "Dual BSD/GPL";
58 changes: 58 additions & 0 deletions selftest/map-autocreate/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import "C"

import (
"log"

bpf "github.com/aquasecurity/libbpfgo"
)

func main() {
bpfModuleWithAutocreate, err := bpf.NewModuleFromFile("main.bpf.o")
if err != nil {
log.Fatal(err)
}
defer bpfModuleWithAutocreate.Close()

testerMapWithAutocreate, err := bpfModuleWithAutocreate.GetMap("tester")
if err != nil {
log.Fatal(err)
}

isAutocreate := testerMapWithAutocreate.Autocreate()
if !isAutocreate {
log.Fatal("Autocreate is false")
}

err = bpfModuleWithAutocreate.BPFLoadObject()
if err == nil {
log.Fatal("Was able to load with a bad type of map")
}

bpfModuleWithoutAutocreate, err := bpf.NewModuleFromFile("main.bpf.o")
if err != nil {
log.Fatal(err)
}
defer bpfModuleWithoutAutocreate.Close()

testerMapWithoutAutocreate, err := bpfModuleWithoutAutocreate.GetMap("tester")
if err != nil {
log.Fatal(err)
}

err = testerMapWithoutAutocreate.SetAutocreate(false)
if err != nil {
log.Fatal(err)
}

isAutocreate = testerMapWithoutAutocreate.Autocreate()
if isAutocreate {
log.Fatal("Autocreate is true")
}

err = bpfModuleWithoutAutocreate.BPFLoadObject()
if err != nil {
log.Fatal(err)
}
}
1 change: 1 addition & 0 deletions selftest/map-autocreate/run.sh
1 change: 1 addition & 0 deletions selftest/map-keysize/Makefile
7 changes: 7 additions & 0 deletions selftest/map-keysize/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/aquasecurity/libbpfgo/selftest/map-update

go 1.18

require github.com/aquasecurity/libbpfgo v0.4.7-libbpf-1.2.0-b2e29a1

replace github.com/aquasecurity/libbpfgo => ../../
4 changes: 4 additions & 0 deletions selftest/map-keysize/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
14 changes: 14 additions & 0 deletions selftest/map-keysize/main.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//+build ignore

#include <vmlinux.h>

#include <bpf/bpf_helpers.h>

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, u32);
__type(value, u32);
__uint(max_entries, 1);
} tester SEC(".maps");

char LICENSE[] SEC("license") = "Dual BSD/GPL";
39 changes: 39 additions & 0 deletions selftest/map-keysize/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import "C"

import (
"log"

bpf "github.com/aquasecurity/libbpfgo"
)

func main() {
bpfModule, err := bpf.NewModuleFromFile("main.bpf.o")
if err != nil {
log.Fatal(err)
}
defer bpfModule.Close()

testerMap, err := bpfModule.GetMap("tester")
if err != nil {
log.Fatal(err)
}

keySize := testerMap.KeySize()
if keySize != 4 {
log.Fatal("keySize do not match")
}

err = testerMap.SetKeySize(8)
if err != nil {
log.Fatal(err)
}

keySize = testerMap.KeySize()
if keySize != 8 {
log.Fatal("keySize do not match")
}

bpfModule.BPFLoadObject()
}
1 change: 1 addition & 0 deletions selftest/map-keysize/run.sh

0 comments on commit 7a58b8e

Please sign in to comment.