-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add method Run to BPFProg #440
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be nice to add an error here when the program type isn't compatible (rather than relying on an internal failure).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be dependent on kernel version support, e.g.:
The output for
ENOTSUPP
isfailed to run program: errno 524
. https://elixir.bootlin.com/linux/v6.9.3/source/include/linux/errno.h#L27.Error()
doesn't find a correct entry, since "not supported" for syscall isENOTSUP
95. https://github.com/golang/sys/blob/master/unix/zerrors_linux_amd64.go#L614There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WDYT? If you agree, we must check all others returns for this
ENOTSUPP
.failed to run program: operation not supported
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyway, merging. If decided for it, we can tackle it in a bunch.