From 7a58b8ec6cc3982096818815afffd348cd236bd7 Mon Sep 17 00:00:00 2001 From: sc07kvm Date: Mon, 23 Oct 2023 15:20:45 +0300 Subject: [PATCH] Introduce SetKeySize, Autocreate and SetAutocreate Co-authored-by: sc07kvm --- map.go | 28 +++++++++++++-- selftest/map-autocreate/Makefile | 1 + selftest/map-autocreate/go.mod | 7 ++++ selftest/map-autocreate/go.sum | 4 +++ selftest/map-autocreate/main.bpf.c | 11 ++++++ selftest/map-autocreate/main.go | 58 ++++++++++++++++++++++++++++++ selftest/map-autocreate/run.sh | 1 + selftest/map-keysize/Makefile | 1 + selftest/map-keysize/go.mod | 7 ++++ selftest/map-keysize/go.sum | 4 +++ selftest/map-keysize/main.bpf.c | 14 ++++++++ selftest/map-keysize/main.go | 39 ++++++++++++++++++++ selftest/map-keysize/run.sh | 1 + 13 files changed, 173 insertions(+), 3 deletions(-) create mode 120000 selftest/map-autocreate/Makefile create mode 100644 selftest/map-autocreate/go.mod create mode 100644 selftest/map-autocreate/go.sum create mode 100644 selftest/map-autocreate/main.bpf.c create mode 100644 selftest/map-autocreate/main.go create mode 120000 selftest/map-autocreate/run.sh create mode 120000 selftest/map-keysize/Makefile create mode 100644 selftest/map-keysize/go.mod create mode 100644 selftest/map-keysize/go.sum create mode 100644 selftest/map-keysize/main.bpf.c create mode 100644 selftest/map-keysize/main.go create mode 120000 selftest/map-keysize/run.sh diff --git a/map.go b/map.go index 8a1b66fc..f9e4c0e5 100644 --- a/map.go +++ b/map.go @@ -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)) @@ -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)) } diff --git a/selftest/map-autocreate/Makefile b/selftest/map-autocreate/Makefile new file mode 120000 index 00000000..d981720c --- /dev/null +++ b/selftest/map-autocreate/Makefile @@ -0,0 +1 @@ +../common/Makefile \ No newline at end of file diff --git a/selftest/map-autocreate/go.mod b/selftest/map-autocreate/go.mod new file mode 100644 index 00000000..1fd0933d --- /dev/null +++ b/selftest/map-autocreate/go.mod @@ -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 => ../../ diff --git a/selftest/map-autocreate/go.sum b/selftest/map-autocreate/go.sum new file mode 100644 index 00000000..c60af667 --- /dev/null +++ b/selftest/map-autocreate/go.sum @@ -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= diff --git a/selftest/map-autocreate/main.bpf.c b/selftest/map-autocreate/main.bpf.c new file mode 100644 index 00000000..0d9c8d5e --- /dev/null +++ b/selftest/map-autocreate/main.bpf.c @@ -0,0 +1,11 @@ +//+build ignore + +#include + +#include + +struct { + __uint(type, ~0U); +} tester SEC(".maps"); + +char LICENSE[] SEC("license") = "Dual BSD/GPL"; diff --git a/selftest/map-autocreate/main.go b/selftest/map-autocreate/main.go new file mode 100644 index 00000000..f863a0af --- /dev/null +++ b/selftest/map-autocreate/main.go @@ -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) + } +} diff --git a/selftest/map-autocreate/run.sh b/selftest/map-autocreate/run.sh new file mode 120000 index 00000000..aee911b2 --- /dev/null +++ b/selftest/map-autocreate/run.sh @@ -0,0 +1 @@ +../common/run.sh \ No newline at end of file diff --git a/selftest/map-keysize/Makefile b/selftest/map-keysize/Makefile new file mode 120000 index 00000000..d981720c --- /dev/null +++ b/selftest/map-keysize/Makefile @@ -0,0 +1 @@ +../common/Makefile \ No newline at end of file diff --git a/selftest/map-keysize/go.mod b/selftest/map-keysize/go.mod new file mode 100644 index 00000000..1fd0933d --- /dev/null +++ b/selftest/map-keysize/go.mod @@ -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 => ../../ diff --git a/selftest/map-keysize/go.sum b/selftest/map-keysize/go.sum new file mode 100644 index 00000000..c60af667 --- /dev/null +++ b/selftest/map-keysize/go.sum @@ -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= diff --git a/selftest/map-keysize/main.bpf.c b/selftest/map-keysize/main.bpf.c new file mode 100644 index 00000000..ba944d74 --- /dev/null +++ b/selftest/map-keysize/main.bpf.c @@ -0,0 +1,14 @@ +//+build ignore + +#include + +#include + +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"; diff --git a/selftest/map-keysize/main.go b/selftest/map-keysize/main.go new file mode 100644 index 00000000..edbf2451 --- /dev/null +++ b/selftest/map-keysize/main.go @@ -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() +} diff --git a/selftest/map-keysize/run.sh b/selftest/map-keysize/run.sh new file mode 120000 index 00000000..aee911b2 --- /dev/null +++ b/selftest/map-keysize/run.sh @@ -0,0 +1 @@ +../common/run.sh \ No newline at end of file