From 4bd1d5f5f552ed419433559f6d5000abd5242050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geyslan=20Greg=C3=B3rio?= Date: Mon, 14 Aug 2023 09:17:20 -0300 Subject: [PATCH] chore(selftest): map-of-maps-outer-low-inner-low Introduce a new selftest that tests a map of maps with outer and inner maps not pre-allocated in the BPF program. --- .../map-of-maps-outer-low-inner-low/Makefile | 1 + .../map-of-maps-outer-low-inner-low/go.mod | 7 ++ .../map-of-maps-outer-low-inner-low/go.sum | 4 ++ .../main.bpf.c | 7 ++ .../map-of-maps-outer-low-inner-low/main.go | 71 +++++++++++++++++++ .../map-of-maps-outer-low-inner-low/run.sh | 1 + 6 files changed, 91 insertions(+) create mode 120000 selftest/map-of-maps-outer-low-inner-low/Makefile create mode 100644 selftest/map-of-maps-outer-low-inner-low/go.mod create mode 100644 selftest/map-of-maps-outer-low-inner-low/go.sum create mode 100644 selftest/map-of-maps-outer-low-inner-low/main.bpf.c create mode 100644 selftest/map-of-maps-outer-low-inner-low/main.go create mode 120000 selftest/map-of-maps-outer-low-inner-low/run.sh diff --git a/selftest/map-of-maps-outer-low-inner-low/Makefile b/selftest/map-of-maps-outer-low-inner-low/Makefile new file mode 120000 index 00000000..d981720c --- /dev/null +++ b/selftest/map-of-maps-outer-low-inner-low/Makefile @@ -0,0 +1 @@ +../common/Makefile \ No newline at end of file diff --git a/selftest/map-of-maps-outer-low-inner-low/go.mod b/selftest/map-of-maps-outer-low-inner-low/go.mod new file mode 100644 index 00000000..1fd0933d --- /dev/null +++ b/selftest/map-of-maps-outer-low-inner-low/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-of-maps-outer-low-inner-low/go.sum b/selftest/map-of-maps-outer-low-inner-low/go.sum new file mode 100644 index 00000000..c60af667 --- /dev/null +++ b/selftest/map-of-maps-outer-low-inner-low/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-of-maps-outer-low-inner-low/main.bpf.c b/selftest/map-of-maps-outer-low-inner-low/main.bpf.c new file mode 100644 index 00000000..b734622c --- /dev/null +++ b/selftest/map-of-maps-outer-low-inner-low/main.bpf.c @@ -0,0 +1,7 @@ +//+build ignore + +#include + +#include + +char LICENSE[] SEC("license") = "Dual BSD/GPL"; diff --git a/selftest/map-of-maps-outer-low-inner-low/main.go b/selftest/map-of-maps-outer-low-inner-low/main.go new file mode 100644 index 00000000..45de7188 --- /dev/null +++ b/selftest/map-of-maps-outer-low-inner-low/main.go @@ -0,0 +1,71 @@ +package main + +import "C" + +import ( + "encoding/binary" + "log" + "unsafe" + + bpf "github.com/aquasecurity/libbpfgo" +) + +func main() { + bpfModule, err := bpf.NewModuleFromFile("main.bpf.o") + if err != nil { + log.Fatal(err) + } + defer bpfModule.Close() + + err = bpfModule.BPFLoadObject() + if err != nil { + log.Fatal(err) + } + + innerArray, err := bpf.CreateMap(bpf.MapTypeArray, "inner_array", 4, 4, 1, nil) + if err != nil { + log.Fatal(err) + } + + // Create the "outer_hash" map, using the "inner_array" map as a prototype. + opts := bpf.BPFMapCreateOpts{ + InnerMapFD: uint32(innerArray.FileDescriptor()), + } + outerHash, err := bpf.CreateMap(bpf.MapTypeHash, "outer_hash", 4, 4, 1, &opts) + if err != nil { + log.Fatal(err) + } + + // Save the inner map in the "outer_hash" map, using the hash key 1917. + // The value used to save the element is the the inner map file descriptor, + // however the actual saved value is the inner map ID. + key1 := uint32(1917) + key1Unsafe := unsafe.Pointer(&key1) + value1 := uint32(innerArray.FileDescriptor()) // "inner_array" FD. + value1Unsafe := unsafe.Pointer(&value1) + err = outerHash.Update(key1Unsafe, value1Unsafe) + if err != nil { + log.Fatal(err) + } + + // Save an element in the "inner_array" map. + key1 = uint32(0) // index 0 + value1 = uint32(191711) + value1Unsafe = unsafe.Pointer(&value1) + err = innerArray.Update(key1Unsafe, value1Unsafe) + if err != nil { + log.Fatal(err) + } +} + +func endian() binary.ByteOrder { + var i int32 = 0x01020304 + u := unsafe.Pointer(&i) + pb := (*byte)(u) + b := *pb + if b == 0x04 { + return binary.LittleEndian + } + + return binary.BigEndian +} diff --git a/selftest/map-of-maps-outer-low-inner-low/run.sh b/selftest/map-of-maps-outer-low-inner-low/run.sh new file mode 120000 index 00000000..aee911b2 --- /dev/null +++ b/selftest/map-of-maps-outer-low-inner-low/run.sh @@ -0,0 +1 @@ +../common/run.sh \ No newline at end of file