-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New CreateMap API implementing bpf_map_create, Type/name accessors (#138
) * Add bpf_map_create() API and selftest Signed-off-by: grantseltzer <grantseltzer@gmail.com> * Add API functions for set/get of map type, and map names * Add comment further explaining use of CreateMap Signed-off-by: grantseltzer <grantseltzer@gmail.com>
- Loading branch information
1 parent
5aa6aaa
commit 5ced2a6
Showing
8 changed files
with
254 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
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-pin-info | ||
|
||
go 1.16 | ||
|
||
require github.com/aquasecurity/libbpfgo v0.2.1-libbpf-0.4.0 | ||
|
||
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,11 @@ | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= | ||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
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,11 @@ | ||
//+build ignore | ||
#include "vmlinux.h" | ||
#include <bpf/bpf_helpers.h> | ||
|
||
SEC("kprobe/sys_execve") | ||
int kprobe__sys_execve(struct pt_regs *ctx) | ||
{ | ||
return 0; | ||
} | ||
|
||
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,59 @@ | ||
package main | ||
|
||
import "C" | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"unsafe" | ||
|
||
"github.com/aquasecurity/libbpfgo" | ||
bpf "github.com/aquasecurity/libbpfgo" | ||
) | ||
|
||
/* | ||
CreateMap uses `bpf_map_create()`, a 'low-level' API in libbpf | ||
As such, it does not have access to the higher level APIs in | ||
libbpf, which are denoted by starting with `bpf_map__*`. | ||
As such, you can use bpf_map_create() to populate arrays of maps | ||
in userspace, then iterate over those arrays on the bpf side, and | ||
use them as a normal map there as those operations only require | ||
a file descriptor. | ||
You can update values in the map from userspace, but currently | ||
can't retrieve values outright. | ||
For example: | ||
https://elixir.bootlin.com/linux/latest/source/samples/bpf/fds_example.c | ||
https://elixir.bootlin.com/linux/latest/source/tools/testing/selftests/bpf/test_maps.c | ||
https://lore.kernel.org/bpf/CAO658oWagXsQDeFtRA2vZBzov7cwwVNTs5nHE9fMGrMOs6bbpQ@mail.gmail.com/ | ||
*/ | ||
|
||
func main() { | ||
bpfModule, err := bpf.NewModuleFromFile("main.bpf.o") | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(-1) | ||
} | ||
defer bpfModule.Close() | ||
|
||
bpfModule.BPFLoadObject() | ||
opts := bpf.BPFMapCreateOpts{} | ||
opts.Size = uint64(unsafe.Sizeof(opts)) | ||
|
||
m, err := libbpfgo.CreateMap(libbpfgo.MapTypeHash, "foobar", 4, 4, 420, nil) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
key1 := uint32(1) | ||
value1 := uint32(55) | ||
key1Unsafe := unsafe.Pointer(&key1) | ||
value1Unsafe := unsafe.Pointer(&value1) | ||
err = m.Update(key1Unsafe, value1Unsafe) | ||
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 |
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