forked from aquasecurity/libbpfgo
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(map): introduce ReuseFD() to reuse a map fd
The `ReuseFD` function links the current BPFMap instance to a provided map file descriptor. This enables the reuse of a map that was initiated by another process. Initial effort: aquasecurity#346 Co-authored-by: Francisco Javier Honduvilla Coto <[email protected]>
- Loading branch information
1 parent
2065362
commit 70d093b
Showing
9 changed files
with
200 additions
and
3 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
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
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,23 @@ | ||
//+build ignore | ||
|
||
#include <vmlinux.h> | ||
|
||
#include <bpf/bpf_helpers.h> | ||
|
||
#include "main.bpf.h" | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_HASH); | ||
__type(key, u32); | ||
__type(value, struct value); | ||
__uint(max_entries, 1 << 4); | ||
} tester SEC(".maps"); | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_HASH); | ||
__type(key, u32); | ||
__type(value, struct value); | ||
__uint(max_entries, 1 << 4); | ||
} tester_reused 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,6 @@ | ||
struct value { | ||
char a; | ||
char b; | ||
char c; | ||
char d; | ||
}; |
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,102 @@ | ||
package main | ||
|
||
// #include "main.bpf.h" | ||
import "C" | ||
|
||
import ( | ||
"bytes" | ||
"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() | ||
|
||
bpfModule.BPFLoadObject() | ||
|
||
testerMap, err := bpfModule.GetMap("tester") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
testerReusedMap, err := bpfModule.GetMap("tester_reused") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// | ||
// BPFMap ReuseFD | ||
// | ||
|
||
// The current instance of "tester_reused" will be closed, and testerReusedMap | ||
// will now point to the same map as testerMap "tester", however, via a | ||
// different FD. | ||
err = testerReusedMap.ReuseFD(testerMap.FileDescriptor()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
valueSize := C.sizeof_struct_value | ||
|
||
key1 := uint32(1) | ||
value1 := make([]byte, valueSize) | ||
value1[0] = '7' | ||
value1[1] = '1' | ||
value1[2] = '9' | ||
value1[3] = '1' | ||
key1Unsafe := unsafe.Pointer(&key1) | ||
value1Unsafe := unsafe.Pointer(&value1[0]) | ||
err = testerMap.Update(key1Unsafe, value1Unsafe) // update "tester" | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
key2 := int32(42069420) | ||
value2 := make([]byte, valueSize) | ||
value2[0] = '1' | ||
value2[1] = '1' | ||
value2[2] = '0' | ||
value2[3] = '7' | ||
key2Unsafe := unsafe.Pointer(&key2) | ||
value2Unsafe := unsafe.Pointer(&value2[0]) | ||
err = testerReusedMap.Update(key2Unsafe, value2Unsafe) // also update "tester" | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// | ||
// BPFMapLow ReuseFD | ||
// | ||
|
||
toReuseCreated, err := bpf.CreateMap(bpf.MapTypeArray, "toreuse", 4, 4, 420, nil) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// The current instance of "toreuse" will be closed, and toReuseCreated | ||
// will now point to the same map as testerMap "tester", however, via a | ||
// different FD. | ||
err = toReuseCreated.ReuseFD(testerMap.FileDescriptor()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
val2, err := toReuseCreated.GetValue(key2Unsafe) // lookup "tester" | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if !bytes.Equal(val2, value2) { | ||
log.Fatal("wrong value") | ||
} | ||
} |
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 |