-
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.
Run method is used to load and run the BPF program without attaching it to any hook. This is useful for testing the BPF program. It makes use of the bpf(BPF_PROG_TEST_RUN) via bpf_prog_test_run_opts(). The prog-run selftest is added to test the Run method. More examples can be found in the linux kernel source code: tools/testing/selftests/bpf/prog_tests. Original code #428 was authored by sc07kvm, and this commiter have only made cosmetics adjustments. Co-authored-by: Geyslan Gregório <[email protected]>
- Loading branch information
Showing
10 changed files
with
333 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
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,22 @@ | ||
#!/bin/bash | ||
|
||
# SETTINGS | ||
|
||
TEST=$(dirname $0)/$1 # execute | ||
TIMEOUT=10 # seconds | ||
|
||
# COMMON | ||
|
||
COMMON="$(dirname $0)/../common/common.sh" | ||
[[ -f $COMMON ]] && { . $COMMON; } || { error "no common"; exit 1; } | ||
|
||
# MAIN | ||
|
||
kern_version gt 4.12 | ||
|
||
check_build | ||
check_ppid | ||
test_exec | ||
test_finish | ||
|
||
exit 0 |
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/testrun | ||
|
||
go 1.21 | ||
|
||
require github.com/aquasecurity/libbpfgo v0.0.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,8 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/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/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/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,27 @@ | ||
//+build ignore | ||
|
||
#include <vmlinux.h> | ||
|
||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
|
||
SEC("tc") | ||
int test_tc(struct __sk_buff *skb) | ||
{ | ||
void *data = (void *) (long) skb->data; | ||
void *data_end = (void *) (long) skb->data_end; | ||
if (data + 4 > data_end) { | ||
return -1; | ||
} | ||
|
||
if (*(__u32 *) data == 0xdeadbeef) { | ||
char new_data[] = {0x01, 0x02, 0x03, 0x04}; | ||
bpf_skb_store_bytes(skb, 0, new_data, 4, 0); | ||
bpf_skb_change_tail(skb, 14, 0); | ||
return 1; | ||
} | ||
|
||
return 2; | ||
} | ||
|
||
char LICENSE[] SEC("license") = "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,52 @@ | ||
package main | ||
|
||
import "C" | ||
|
||
import ( | ||
"encoding/binary" | ||
"log" | ||
|
||
bpf "github.com/aquasecurity/libbpfgo" | ||
) | ||
|
||
func main() { | ||
bpfModule, err := bpf.NewModuleFromFile("main.bpf.o") | ||
if err != nil { | ||
log.Fatalf("Failed to load BPF module: %v", err) | ||
} | ||
defer bpfModule.Close() | ||
|
||
err = bpfModule.BPFLoadObject() | ||
if err != nil { | ||
log.Fatalf("Failed to load object: %v", err) | ||
} | ||
|
||
tcProg, err := bpfModule.GetProgram("test_tc") | ||
if err != nil || tcProg == nil { | ||
log.Fatalf("Failed to get prog test_tc: %v", err) | ||
} | ||
|
||
dataIn := make([]byte, 16) | ||
binary.LittleEndian.PutUint32(dataIn, 0xdeadbeef) | ||
opts := bpf.RunOpts{ | ||
DataIn: dataIn, | ||
DataSizeIn: 16, | ||
DataOut: make([]byte, 32), | ||
DataSizeOut: 32, | ||
Repeat: 1, | ||
} | ||
|
||
err = tcProg.Run(&opts) | ||
if err != nil { | ||
log.Fatalf("Failed to run prog: %v", err) | ||
} | ||
if opts.RetVal != 1 { | ||
log.Fatalf("retVal %d should be 1", opts.RetVal) | ||
} | ||
if len(opts.DataOut) != 14 { | ||
log.Fatalf("dataOut len %v should be 14", opts.DataOut) | ||
} | ||
if binary.LittleEndian.Uint32(opts.DataOut) != 0x04030201 { | ||
log.Fatalf("dataOut 0x%x should be 0x04030201", binary.LittleEndian.Uint32(opts.DataOut)) | ||
} | ||
} |
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-4.12.sh |