diff --git a/libbpfgo.go b/libbpfgo.go index dc9077cd..87e617aa 100644 --- a/libbpfgo.go +++ b/libbpfgo.go @@ -564,6 +564,15 @@ func (b *BPFMap) Type() MapType { return MapType(C.bpf_map__type(b.bpfMap)) } +func (b *BPFMap) MapReuseFd(fd int) error { + errC := C.bpf_map__reuse_fd(b.bpfMap, C.int(fd)) + if errC != 0 { + return fmt.Errorf("could not reuse bpf map fd %d: %w", fd, syscall.Errno(-errC)) + } + b.fd = C.int(fd) + return nil +} + // SetType is used to set the type of a bpf map that isn't associated // with a file descriptor already. If the map is already associated // with a file descriptor the libbpf API will return error code EBUSY diff --git a/selftest/common/common.sh b/selftest/common/common.sh index 0064854c..2fa0cf9c 100644 --- a/selftest/common/common.sh +++ b/selftest/common/common.sh @@ -33,7 +33,7 @@ okcontinue() { okay "$1"; } kern_version() { _oper=$1; _version=$2; _notfatal=$3; _given=$(($(echo $_version | sed 's:\.::g'))) - _current=$(($(uname -r | cut -d'.' -f1,2 | sed 's:\.::g'))) + _current=$(($(uname -r | cut -d'.' -f1,2,3 | sed 's:\.::g'))) [[ "$_version" == "" ]] && errexit "no kernel version given" diff --git a/selftest/create-map/main.go b/selftest/create-map/main.go index 8062bc33..6b22c7cd 100644 --- a/selftest/create-map/main.go +++ b/selftest/create-map/main.go @@ -36,10 +36,6 @@ func main() { } defer bpfModule.Close() - bpfModule.BPFLoadObject() - opts := bpf.BPFMapCreateOpts{} - opts.Size = uint64(unsafe.Sizeof(opts)) - m, err := bpf.CreateMap(bpf.MapTypeHash, "foobar", 4, 4, 420, nil) if err != nil { log.Fatal(err) diff --git a/selftest/map-update/main.bpf.c b/selftest/map-update/main.bpf.c index 3c8a9288..421337dc 100644 --- a/selftest/map-update/main.bpf.c +++ b/selftest/map-update/main.bpf.c @@ -16,6 +16,13 @@ struct { __uint(max_entries, 1 << 24); } tester SEC(".maps"); +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __type(key, u32); + __type(value, struct value); + __uint(max_entries, 1 << 24); +} tester_reused SEC(".maps"); + struct { __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); __uint(key_size, sizeof(u32)); diff --git a/selftest/map-update/main.go b/selftest/map-update/main.go index 23301b02..988050bb 100644 --- a/selftest/map-update/main.go +++ b/selftest/map-update/main.go @@ -87,6 +87,23 @@ func main() { os.Exit(-1) } + testerMap2, err := bpfModule.GetMap("tester_reused") + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(-1) + } + + err = testerMap2.MapReuseFd(testerMap.FileDescriptor()) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(-1) + } + + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(-1) + } + eventsChannel := make(chan []byte) lostChannel := make(chan uint64) pb, err := bpfModule.InitPerfBuf("events", eventsChannel, lostChannel, 1)