Skip to content

Commit

Permalink
feat(btf): introduce GetBTFFDByID()
Browse files Browse the repository at this point in the history
This allows to get a FD to a BTF by its ID. This is useful to create a
BPFMapLow passing options set with a BTF ID.
  • Loading branch information
geyslan committed Sep 4, 2023
1 parent 180e890 commit 4afed55
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 0 deletions.
26 changes: 26 additions & 0 deletions btf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package libbpfgo

/*
#cgo LDFLAGS: -lelf -lz
#include "libbpfgo.h"
*/
import "C"

import (
"fmt"
"syscall"
)

//
// BTF (low-level API)
//

// GetBTFFDByID returns a file descriptor for the BTF with the given ID.
func GetBTFFDByID(id uint32) (int, error) {
fdC := C.bpf_btf_get_fd_by_id(C.uint(id))
if fdC < 0 {
return int(fdC), fmt.Errorf("could not find BTF id %d: %w", id, syscall.Errno(-fdC))
}

return int(fdC), nil
}
1 change: 1 addition & 0 deletions selftest/getbtffdbyid/Makefile
7 changes: 7 additions & 0 deletions selftest/getbtffdbyid/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/getbtffdbyid/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/getbtffdbyid/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_ARRAY);
__uint(max_entries, 1);
__type(key, __u32);
__type(value, __u32);
} inner_array_proto SEC(".maps");

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

import "C"

import (
"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)
}

// Use the "inner_array_proto" map BTF ID to create a new map with the same
// BTF type.
innerArrayProto, err := bpfModule.GetMap("inner_array_proto")
if err != nil {
log.Fatal(err)
}

optsProto, err := bpf.GetMapInfoByFD(innerArrayProto.FileDescriptor())
if err != nil {
log.Fatal(err)
}

// The "inner_array_proto" map is a BTF map, so its ID can be used to create
// a new map with the same BTF type.
btfFD, err := bpf.GetBTFFDByID(optsProto.BTFID)
if err != nil {
log.Fatal(err)
}

createOpts := &bpf.BPFMapCreateOpts{
BTFFD: uint32(btfFD),
}
innerArray, err := bpf.CreateMap(bpf.MapTypeArray, "inner_array", 4, 4, 1, createOpts)
if err != nil {
log.Fatal(err)
}

// Save an element in the "inner_array" map.
key := uint32(0) // index 0
keyUnsafe := unsafe.Pointer(&key)
value := uint32(191711)
valueUnsafe := unsafe.Pointer(&value)
err = innerArray.Update(keyUnsafe, valueUnsafe)
if err != nil {
log.Fatal(err)
}
}
1 change: 1 addition & 0 deletions selftest/getbtffdbyid/run.sh

0 comments on commit 4afed55

Please sign in to comment.