-
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.
libbpf: Added AttachXDP wrapper (#170)
* libbpf: Added AttachXDP wrapper * Added new link type XDP * selftest: xdp: Added selftest
- Loading branch information
Showing
7 changed files
with
263 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,91 @@ | ||
BASEDIR = $(abspath ../../) | ||
|
||
OUTPUT = ../../output | ||
|
||
LIBBPF_SRC = $(abspath ../../libbpf/src) | ||
LIBBPF_OBJ = $(abspath $(OUTPUT)/libbpf.a) | ||
|
||
CC = gcc | ||
CLANG = clang | ||
GO = go | ||
|
||
ARCH := $(shell uname -m | sed 's/x86_64/amd64/g; s/aarch64/arm64/g') | ||
|
||
CFLAGS = -g -O2 -Wall -fpie | ||
LDFLAGS = | ||
|
||
CGO_CFLAGS_STATIC = "-I$(abspath $(OUTPUT))" | ||
CGO_LDFLAGS_STATIC = "-lelf -lz $(LIBBPF_OBJ)" | ||
CGO_EXTLDFLAGS_STATIC = '-w -extldflags "-static"' | ||
|
||
CGO_CFGLAGS_DYN = "-I. -I/usr/include/" | ||
CGO_LDFLAGS_DYN = "-lelf -lz -lbpf" | ||
|
||
.PHONY: $(TEST) | ||
.PHONY: $(TEST).go | ||
.PHONY: $(TEST).bpf.c | ||
|
||
TEST = main | ||
|
||
all: $(TEST)-static | ||
|
||
.PHONY: libbpfgo | ||
.PHONY: libbpfgo-static | ||
.PHONY: libbpfgo-dynamic | ||
|
||
## libbpfgo | ||
|
||
libbpfgo-static: | ||
$(MAKE) -C $(BASEDIR) libbpfgo-static | ||
|
||
libbpfgo-dynamic: | ||
$(MAKE) -C $(BASEDIR) libbpfgo-dynamic | ||
|
||
vmlinuxh: | ||
$(MAKE) -C $(BASEDIR) vmlinuxh | ||
|
||
outputdir: | ||
$(MAKE) -C $(BASEDIR) outputdir | ||
|
||
## test bpf dependency | ||
|
||
$(TEST).bpf.o: $(TEST).bpf.c | ||
$(MAKE) -C $(BASEDIR) vmlinuxh | ||
$(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -c $< -o $@ | ||
|
||
## test | ||
|
||
.PHONY: $(TEST)-static | ||
.PHONY: $(TEST)-dynamic | ||
|
||
$(TEST)-static: libbpfgo-static | $(TEST).bpf.o | ||
CC=$(CLANG) \ | ||
CGO_CFLAGS=$(CGO_CFLAGS_STATIC) \ | ||
CGO_LDFLAGS=$(CGO_LDFLAGS_STATIC) \ | ||
GOOS=linux GOARCH=$(ARCH) \ | ||
$(GO) build \ | ||
-tags netgo -ldflags $(CGO_EXTLDFLAGS_STATIC) \ | ||
-o $(TEST)-static ./$(TEST).go | ||
|
||
$(TEST)-dynamic: libbpfgo-dynamic | $(TEST).bpf.o | ||
CC=$(CLANG) \ | ||
CGO_CFLAGS=$(CGO_CFLAGS_DYN) \ | ||
CGO_LDFLAGS=$(CGO_LDFLAGS_DYN) \ | ||
$(GO) build -o ./$(TEST)-dynamic ./$(TEST).go | ||
|
||
## run | ||
|
||
.PHONY: run | ||
.PHONY: run-static | ||
.PHONY: run-dynamic | ||
|
||
run: run-static | ||
|
||
run-static: $(TEST)-static | ||
sudo ./run.sh $(TEST)-static | ||
|
||
run-dynamic: $(TEST)-dynamic | ||
sudo ./run.sh $(TEST)-dynamic | ||
|
||
clean: | ||
rm -f *.o *-static *-dynamic |
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/perfbuffers | ||
|
||
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,33 @@ | ||
//+build ignore | ||
#include "vmlinux.h" | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
|
||
#ifdef asm_inline | ||
#undef asm_inline | ||
#define asm_inline asm | ||
#endif | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_RINGBUF); | ||
__uint(max_entries, 1 << 24); | ||
} events SEC(".maps"); | ||
long ringbuffer_flags = 0; | ||
|
||
SEC("xdp") | ||
int target(struct xdp_md *ctx) { | ||
int *process; | ||
|
||
// Reserve space on the ringbuffer for the sample | ||
process = bpf_ringbuf_reserve(&events, sizeof(int), ringbuffer_flags); | ||
if (!process) { | ||
return 0; | ||
} | ||
|
||
*process = 2021; | ||
|
||
bpf_ringbuf_submit(process, ringbuffer_flags); | ||
return XDP_PASS; | ||
} | ||
|
||
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,78 @@ | ||
package main | ||
|
||
import "C" | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
|
||
bpf "github.com/aquasecurity/libbpfgo" | ||
) | ||
|
||
const ( | ||
deviceName = "lo" | ||
) | ||
|
||
func main() { | ||
|
||
bpfModule, err := bpf.NewModuleFromFile("main.bpf.o") | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(-1) | ||
} | ||
defer bpfModule.Close() | ||
|
||
err = bpfModule.BPFLoadObject() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(-1) | ||
} | ||
|
||
xdpProg, err := bpfModule.GetProgram("target") | ||
if xdpProg == nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(-1) | ||
} | ||
|
||
_, err = xdpProg.AttachXDP(deviceName) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(-1) | ||
} | ||
|
||
eventsChannel := make(chan []byte) | ||
rb, err := bpfModule.InitRingBuf("events", eventsChannel) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(-1) | ||
} | ||
|
||
rb.Start() | ||
numberOfEventsReceived := 0 | ||
go func() { | ||
_, err := exec.Command("ping", "localhost", "-c 10").Output() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(-1) | ||
} | ||
}() | ||
|
||
recvLoop: | ||
|
||
for { | ||
b := <-eventsChannel | ||
if binary.LittleEndian.Uint32(b) != 2021 { | ||
fmt.Fprintf(os.Stderr, "invalid data retrieved\n") | ||
os.Exit(-1) | ||
} | ||
numberOfEventsReceived++ | ||
if numberOfEventsReceived > 5 { | ||
break recvLoop | ||
} | ||
} | ||
|
||
rb.Stop() | ||
rb.Close() | ||
} |
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=5 # seconds | ||
|
||
# COMMON | ||
|
||
COMMON="$(dirname $0)/../common/common.sh" | ||
[[ -f $COMMON ]] && { . $COMMON; } || { error "no common"; exit 1; } | ||
|
||
# MAIN | ||
|
||
kern_version ge 5.8 | ||
|
||
check_build | ||
check_ppid | ||
test_exec | ||
test_finish | ||
|
||
exit 0 |