From 649bcf308f8019b03434ee03776bf33d7dd37c21 Mon Sep 17 00:00:00 2001 From: grantseltzer Date: Mon, 27 Jun 2022 14:53:47 -0400 Subject: [PATCH] Per CPU map needed API and selftest (#156) * Add API 'GetValueReadInto' which lets you specify the byte slice for the map lookup to read into Signed-off-by: grantseltzer --- libbpfgo.go | 23 ++++++++++++ selftest/percpu/Makefile | 1 + selftest/percpu/README.md | 1 + selftest/percpu/go.mod | 7 ++++ selftest/percpu/go.sum | 11 ++++++ selftest/percpu/main.bpf.c | 33 +++++++++++++++++ selftest/percpu/main.go | 74 ++++++++++++++++++++++++++++++++++++++ selftest/percpu/run.sh | 1 + 8 files changed, 151 insertions(+) create mode 120000 selftest/percpu/Makefile create mode 100644 selftest/percpu/README.md create mode 100644 selftest/percpu/go.mod create mode 100644 selftest/percpu/go.sum create mode 100644 selftest/percpu/main.bpf.c create mode 100644 selftest/percpu/main.go create mode 120000 selftest/percpu/run.sh diff --git a/libbpfgo.go b/libbpfgo.go index a24cc32b..a5c77c57 100644 --- a/libbpfgo.go +++ b/libbpfgo.go @@ -670,6 +670,14 @@ func (b *BPFMap) ValueSize() int { return int(C.bpf_map__value_size(b.bpfMap)) } +func (b *BPFMap) SetValueSize(size uint32) error { + errC := C.bpf_map__set_value_size(b.bpfMap, C.uint(size)) + if errC != 0 { + return fmt.Errorf("could not set map value size: %w", syscall.Errno(-errC)) + } + return nil +} + // GetValue takes a pointer to the key which is stored in the map. // It returns the associated value as a slice of bytes. // All basic types, and structs are supported as keys. @@ -700,6 +708,21 @@ func (b *BPFMap) GetValueFlags(key unsafe.Pointer, flags MapFlag) ([]byte, error return value, nil } +// GetValueReadInto is like GetValue, except it allows the caller to pass in +// a pointer to the slice of bytes that the value would be read into from the +// map. +// This is useful for reading from maps with variable sizes, especially +// per-cpu arrays and hash maps where the size of each value depends on the +// number of CPUs +func (b *BPFMap) GetValueReadInto(key unsafe.Pointer, value *[]byte) error { + valuePtr := unsafe.Pointer(&(*value)[0]) + errC := C.bpf_map__lookup_elem(b.bpfMap, key, C.ulong(b.KeySize()), valuePtr, C.ulong(len(*value)), 0) + if errC != 0 { + return fmt.Errorf("failed to lookup value %v in map %s: %w", key, b.name, syscall.Errno(-errC)) + } + return nil +} + // BPFMapBatchOpts mirrors the C structure bpf_map_batch_opts. type BPFMapBatchOpts struct { Sz uint64 diff --git a/selftest/percpu/Makefile b/selftest/percpu/Makefile new file mode 120000 index 00000000..d981720c --- /dev/null +++ b/selftest/percpu/Makefile @@ -0,0 +1 @@ +../common/Makefile \ No newline at end of file diff --git a/selftest/percpu/README.md b/selftest/percpu/README.md new file mode 100644 index 00000000..4778e46a --- /dev/null +++ b/selftest/percpu/README.md @@ -0,0 +1 @@ +# PER CPU \ No newline at end of file diff --git a/selftest/percpu/go.mod b/selftest/percpu/go.mod new file mode 100644 index 00000000..d282d08b --- /dev/null +++ b/selftest/percpu/go.mod @@ -0,0 +1,7 @@ +module github.com/aquasecurity/libbpfgo/selftest/perfbuffers + +go 1.16 + +require github.com/aquasecurity/libbpfgo v0.2.1-libbpf-0.4.0 + +replace github.com/aquasecurity/libbpfgo => ../../ diff --git a/selftest/percpu/go.sum b/selftest/percpu/go.sum new file mode 100644 index 00000000..3b1886cc --- /dev/null +++ b/selftest/percpu/go.sum @@ -0,0 +1,11 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/selftest/percpu/main.bpf.c b/selftest/percpu/main.bpf.c new file mode 100644 index 00000000..8a35d902 --- /dev/null +++ b/selftest/percpu/main.bpf.c @@ -0,0 +1,33 @@ +//+build ignore +#include "vmlinux.h" +#include + +#ifdef asm_inline +#undef asm_inline +#define asm_inline asm +#endif + + +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u64); +} percpu_hash SEC(".maps"); + +SEC("fentry/__x64_sys_mmap") +int mmap_fentry(struct pt_regs *ctx) +{ + __u32 key = 0; + __u8 *value = bpf_map_lookup_elem(&percpu_hash, &key); + if (value) { + *value += 1; + bpf_printk("%d",*value); + return 0; + } + + bpf_printk("nothing"); + + return 0; +} +char LICENSE[] SEC("license") = "GPL"; diff --git a/selftest/percpu/main.go b/selftest/percpu/main.go new file mode 100644 index 00000000..469f40d5 --- /dev/null +++ b/selftest/percpu/main.go @@ -0,0 +1,74 @@ +package main + +import "C" + +import ( + "encoding/binary" + "fmt" + "os" + "runtime" + "syscall" + "time" + "unsafe" + + bpf "github.com/aquasecurity/libbpfgo" +) + +func main() { + + bpfModule, err := bpf.NewModuleFromFile("main.bpf.o") + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(-1) + } + defer bpfModule.Close() + + err = bpfModule.BPFLoadObject() + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(-1) + } + + prog, err := bpfModule.GetProgram("mmap_fentry") + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(-1) + } + + link, err := prog.AttachGeneric() + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(-1) + } + if link.GetFd() == 0 { + os.Exit(-1) + } + + go func() { + for { + syscall.Mmap(999, 999, 999, 1, 1) + time.Sleep(time.Millisecond * 30) + } + }() + + lostEventCounterMap, err := bpfModule.GetMap("percpu_hash") + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(-1) + } + + time.Sleep(time.Second * 2) + key := 0 + values := make([]byte, 8*runtime.NumCPU()) + err = lostEventCounterMap.GetValueReadInto(unsafe.Pointer(&key), &values) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(-1) + } + + last := 0 + for i := 0; i < runtime.NumCPU(); i++ { + fmt.Printf("CPU %d: %d\n", i, binary.LittleEndian.Uint32(values[last:last+8])) + last += 8 + } +} diff --git a/selftest/percpu/run.sh b/selftest/percpu/run.sh new file mode 120000 index 00000000..c1317de3 --- /dev/null +++ b/selftest/percpu/run.sh @@ -0,0 +1 @@ +../common/run-5.8.sh \ No newline at end of file