-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
7 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../common/Makefile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => ../../ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../common/run.sh |