-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(selftest): map-of-maps-outer-high-inner-high
Introduce a new selftest that tests a map of maps with outer and inner maps pre-allocated in the BPF program.
- Loading branch information
Showing
6 changed files
with
120 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 @@ | ||
../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,26 @@ | ||
//+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 SEC(".maps"); | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_HASH_OF_MAPS); | ||
__uint(max_entries, 1); | ||
__type(key, __u32); | ||
__array(values, typeof(inner_array)); | ||
} outer_hash SEC(".maps") = { | ||
.values = | ||
{ | ||
[1917] = &inner_array, | ||
}, | ||
}; | ||
|
||
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,81 @@ | ||
package main | ||
|
||
import "C" | ||
|
||
import ( | ||
"encoding/binary" | ||
"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() | ||
|
||
// Since the outer and inner map definitions are pre-allocated in the | ||
// BPF object, we do not need to do anything before loading the object. | ||
err = bpfModule.BPFLoadObject() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
outerHash, err := bpfModule.GetMap("outer_hash") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
innerArray, err := bpfModule.GetMap("inner_array") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Retrieve the "inner_array" map ID from the "outer_hash" map, | ||
// using the hash key 1917. | ||
key1 := uint32(1917) | ||
key1Unsafe := unsafe.Pointer(&key1) | ||
innerMapIDBytes, err := outerHash.GetValue(key1Unsafe) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Inner map ID retrieved from the outer map element. | ||
innerMapID := endian().Uint32(innerMapIDBytes) | ||
|
||
// Retrieve the "inner_array" map Info. | ||
innerMapInfo, error := bpf.GetMapInfoByFD(innerArray.FileDescriptor()) | ||
if error != nil { | ||
log.Fatal(error) | ||
} | ||
|
||
// Check if the inner map ID retrieved from the outer map matches the | ||
// inner map ID retrieved directly from the inner map. | ||
if innerMapInfo.ID != innerMapID { | ||
log.Fatal("inner map ID does not match") | ||
} | ||
|
||
// Save an element in the "inner_array" map. | ||
key1 = uint32(0) // index 0 | ||
value1 := uint32(191711) | ||
value1Unsafe := unsafe.Pointer(&value1) | ||
err = innerArray.Update(key1Unsafe, value1Unsafe) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func endian() binary.ByteOrder { | ||
var i int32 = 0x01020304 | ||
u := unsafe.Pointer(&i) | ||
pb := (*byte)(u) | ||
b := *pb | ||
if b == 0x04 { | ||
return binary.LittleEndian | ||
} | ||
|
||
return binary.BigEndian | ||
} |
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 |