Skip to content

Commit ee8397a

Browse files
authored
Merge pull request #1299 from sancppp/ebpf_ut
Add ebpf ut framework & Add xdp ut
2 parents 578f3ee + 054c9c0 commit ee8397a

File tree

19 files changed

+2159
-11
lines changed

19 files changed

+2159
-11
lines changed

.github/workflows/main-5.15.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,7 @@ jobs:
3636
- name: Go Test
3737
run: |
3838
sudo make test RUN_IN_CONTAINER=1
39+
40+
- name: eBPF Unit Test
41+
run: |
42+
sudo make ebpf_unit_test RUN_IN_CONTAINER=1 V=1

.github/workflows/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ jobs:
6161
run: |
6262
sudo env LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:$GITHUB_WORKSPACE/api/v2-c:$GITHUB_WORKSPACE/bpf/deserialization_to_bpf_map PKG_CONFIG_PATH=$GITHUB_WORKSPACE/mk go test -race -v -vet=off -coverprofile=coverage.out ./pkg/...
6363
64+
- name: eBPF Unit Test
65+
run: |
66+
sudo env LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:$GITHUB_WORKSPACE/api/v2-c:$GITHUB_WORKSPACE/bpf/deserialization_to_bpf_map make ebpf_unit_test V=1
67+
6468
- name: Upload coverage reports to Codecov
6569
uses: codecov/codecov-action@v4
6670
with:

Makefile

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,28 @@ e2e-ipv6:
210210
format:
211211
./hack/format.sh
212212

213+
214+
GO_TEST_FLAGS:=
215+
ifeq ($(V),1)
216+
GO_TEST_FLAGS += --verbose
217+
endif
218+
213219
.PHONY: test
214220
ifeq ($(RUN_IN_CONTAINER),1)
215221
test:
216-
./hack/run-ut.sh --docker
222+
./hack/run-ut.sh --docker $(GO_TEST_FLAGS)
217223
else
218224
test:
219-
./hack/run-ut.sh --local
225+
./hack/run-ut.sh --local $(GO_TEST_FLAGS)
226+
endif
227+
228+
.PHONY: ebpf_unit_test
229+
ifeq ($(RUN_IN_CONTAINER),1)
230+
ebpf_unit_test:
231+
./hack/run-ebpf-ut.sh --docker $(GO_TEST_FLAGS)
232+
else
233+
ebpf_unit_test:
234+
./hack/run-ebpf-ut.sh --local $(GO_TEST_FLAGS)
220235
endif
221236

222237
UPDATE_VERSION ?= ${VERSION}

bpf/kmesh/workload/include/authz.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -658,17 +658,27 @@ int policy_check(struct xdp_md *ctx)
658658
BPF_LOG(
659659
DEBUG,
660660
AUTH,
661-
"src ip: %u, dst ip %u, dst port: %u\n",
661+
"src ip: %s, src port:%u",
662662
ip2str(&tuple_key.ipv4.saddr, true),
663+
bpf_ntohs(tuple_key.ipv4.sport));
664+
BPF_LOG(
665+
DEBUG,
666+
AUTH,
667+
"dst ip: %s, dst port:%u\n",
663668
ip2str(&tuple_key.ipv4.daddr, true),
664669
bpf_ntohs(tuple_key.ipv4.dport));
665670
} else {
666671
BPF_LOG(
667672
DEBUG,
668673
AUTH,
669-
"src ip: %u, dst ip %u, dst port: %u\n",
670-
ip2str(&tuple_key.ipv6.saddr[0], false),
671-
ip2str(&tuple_key.ipv6.daddr[0], false),
674+
"src ip: %s, src port:%u",
675+
ip2str(tuple_key.ipv6.saddr, false),
676+
bpf_ntohs(tuple_key.ipv6.sport));
677+
BPF_LOG(
678+
DEBUG,
679+
AUTH,
680+
"dst ip: %s, dst port:%u\n",
681+
ip2str(tuple_key.ipv6.daddr, false),
672682
bpf_ntohs(tuple_key.ipv6.dport));
673683
}
674684
if (bpf_map_delete_elem(&kmesh_tc_args, &tuple_key) != 0) {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/cncf/xds/go v0.0.0-20241213214725-57cfbe6fad57
1313
github.com/containernetworking/cni v1.2.3
1414
github.com/containernetworking/plugins v1.6.1
15+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
1516
github.com/envoyproxy/go-control-plane v0.13.2-0.20241125134052-fc612d4a3afa
1617
github.com/fsnotify/fsnotify v1.8.0
1718
github.com/hashicorp/go-multierror v1.1.1
@@ -67,7 +68,6 @@ require (
6768
github.com/containerd/stargz-snapshotter/estargz v0.16.3 // indirect
6869
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
6970
github.com/cyphar/filepath-securejoin v0.3.5 // indirect
70-
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
7171
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
7272
github.com/docker/cli v27.4.0+incompatible // indirect
7373
github.com/docker/distribution v2.8.3+incompatible // indirect

hack/run-ebpf-ut.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/bash
2+
3+
ROOT_DIR=$(git rev-parse --show-toplevel)
4+
5+
. $ROOT_DIR/hack/utils.sh
6+
7+
DOCKER_EBPF_TEST_COMMAND="make -C /kmesh/test/bpf_ut run"
8+
LOCAL_EBPF_TEST_COMMAND="make -C $ROOT_DIR/test/bpf_ut run"
9+
10+
function docker_run_ebpf_ut() {
11+
local container_id=$1
12+
13+
docker exec $container_id bash -c "$DOCKER_EBPF_TEST_COMMAND"
14+
15+
exit_code=$?
16+
return $exit_code
17+
}
18+
19+
function run_ebpf_ut_local() {
20+
bash $ROOT_DIR/build.sh
21+
export PKG_CONFIG_PATH=$ROOT_DIR/mk
22+
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ROOT_DIR/api/v2-c:$ROOT_DIR/bpf/deserialization_to_bpf_map
23+
eval "$LOCAL_EBPF_TEST_COMMAND"
24+
}
25+
26+
function run_ebpf_ut_in_docker() {
27+
container_id=$(run_docker_container)
28+
build_kmesh $container_id
29+
docker_run_ebpf_ut $container_id
30+
ut_exit_code=$?
31+
clean_container $container_id
32+
exit $ut_exit_code
33+
}
34+
35+
function clean() {
36+
make clean $ROOT_DIR
37+
}
38+
39+
TEST_FLAGS=""
40+
41+
if [[ "$*" == *"-v"* ]] || [[ "$*" == *"--verbose"* ]]; then
42+
TEST_FLAGS="$TEST_FLAGS V=1"
43+
fi
44+
45+
DOCKER_EBPF_TEST_COMMAND="$DOCKER_EBPF_TEST_COMMAND $TEST_FLAGS"
46+
LOCAL_EBPF_TEST_COMMAND="$LOCAL_EBPF_TEST_COMMAND $TEST_FLAGS"
47+
48+
# Running ebpf ut with docker by default
49+
if [ -z "$1" -o "$1" == "-d" -o "$1" == "--docker" ]; then
50+
run_ebpf_ut_in_docker
51+
exit
52+
fi
53+
54+
if [ "$1" == "-l" -o "$1" == "--local" ]; then
55+
run_ebpf_ut_local
56+
exit
57+
fi
58+
59+
if [ "$1" == "-h" -o "$1" == "--help" ]; then
60+
echo run-ebpf-ut.sh -h/--help : Help.
61+
echo run-ebpf-ut.sh -d/--docker: run ebpf unit test in docker.
62+
echo run-ebpf-ut.sh -l/--local: run ebpf unit test locally.
63+
exit
64+
fi
65+
66+
if [ "$1" == "-c" -o "$1" == "--clean" ]; then
67+
clean
68+
exit
69+
fi

kmesh_compile_env_pre.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function dependency_pkg_install() {
1717
echo "Checking for required packages on a Debian-based system..."
1818

1919

20-
packages=(git make clang libbpf-dev llvm linux-tools-generic protobuf-compiler libprotobuf-dev libprotobuf-c-dev protobuf-c-compiler cmake pkg-config)
20+
packages=(git make clang libbpf-dev llvm linux-tools-generic protobuf-compiler libprotobuf-dev libprotobuf-c-dev protobuf-c-compiler cmake pkg-config gcc-multilib)
2121

2222

2323
update_needed=false
@@ -51,7 +51,7 @@ function dependency_pkg_install() {
5151
echo "Checking for required packages on a Red Hat-based system..."
5252

5353
# List of required packages
54-
packages=(git make clang llvm libboundscheck protobuf protobuf-c protobuf-c-devel bpftool libbpf libbpf-devel cmake pkg-config)
54+
packages=(git make clang llvm libboundscheck protobuf protobuf-c protobuf-c-devel bpftool libbpf libbpf-devel cmake pkg-config glibc-devel libstdc++-devel)
5555

5656
# Install each missing package
5757
for pkg in "${packages[@]}"; do

pkg/bpf/workload/loader.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ func (w *BpfWorkload) Start() error {
9494
return fmt.Errorf("failed to set api env")
9595
}
9696

97+
if err := w.DeserialInit(); err != nil {
98+
return fmt.Errorf("failed to init deserialization: %v", err)
99+
}
100+
return nil
101+
}
102+
103+
func (w *BpfWorkload) DeserialInit() error {
97104
ret := C.deserial_init()
98105
if ret != 0 {
99106
return fmt.Errorf("deserial_init failed:%v", ret)

pkg/constants/constants.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ const (
7676
KmKernelNativeBpfPath = "/bpf_kmesh"
7777
KmDualEngineBpfPath = "/bpf_kmesh_workload"
7878

79-
TailCallMap = "tail_call_map"
80-
Prog_link = "prog_link"
79+
TailCallMap = "tail_call_map"
80+
XDPTailCallMap = "km_xdp_tailcall"
81+
Prog_link = "prog_link"
8182

8283
ALL_CIDR = "0.0.0.0/0"
8384
)

test/bpf_ut/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.d
2+
*.o

test/bpf_ut/Makefile

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
MAKEFLAGS += -r
2+
3+
QUIET ?= @
4+
CLANG ?= clang
5+
GO ?= go
6+
7+
ROOT_DIR := $(shell git rev-parse --show-toplevel)
8+
TEST_DIR := $(ROOT_DIR)/test/bpf_ut
9+
10+
FLAGS := -isystem /usr/include -I/usr/local/include
11+
FLAGS += -I./include -I$(ROOT_DIR)/bpf/include
12+
FLAGS += -fPIC -D__NR_CPUS__=$(shell nproc --all) -D__TARGET_ARCH_x86_64 -D__x86_64__ -D_GNU_SOURCE -DKERNEL_VERSION_HIGHER_5_13_0=1
13+
FLAGS += -O2 -g
14+
15+
CLANG_FLAGS := ${FLAGS} --target=bpf -std=gnu99
16+
# eBPF verifier enforces unaligned access checks where necessary, so don't
17+
# let clang complain too early.
18+
CLANG_FLAGS += -Wall -Wextra
19+
CLANG_FLAGS += -Wno-address-of-packed-member
20+
CLANG_FLAGS += -Wno-unknown-warning-option
21+
CLANG_FLAGS += -Wno-gnu-variable-sized-type-not-at-end
22+
CLANG_FLAGS += -Wenum-conversion
23+
CLANG_FLAGS += -Wimplicit-fallthrough
24+
# Create dependency files for each .o file.
25+
CLANG_FLAGS += -MD
26+
27+
.PHONY: all clean run
28+
29+
TEST_OBJECTS = xdp_shutdown_in_userspace_test.o xdp_authz_offload_test.o
30+
31+
XDP_FLAGS = -I$(ROOT_DIR)/bpf/kmesh/ -I$(ROOT_DIR)/bpf/kmesh/workload/include -I$(ROOT_DIR)/api/v2-c
32+
xdp_%.o: xdp_%.c clean
33+
$(QUIET) $(CLANG) $(CLANG_FLAGS) $(XDP_FLAGS) -c $< -o $@
34+
35+
36+
all: $(TEST_OBJECTS)
37+
38+
clean:
39+
$(QUIET) rm -f $(wildcard *.o)
40+
$(QUIET) rm -f $(wildcard *.d)
41+
$(QUIET) echo "clean all object files"
42+
43+
BPF_TEST_FLAGS:=
44+
ifneq ($(shell id -u), 0)
45+
BPF_TEST_FLAGS += -exec "sudo -E"
46+
endif
47+
ifeq ($(V),1)
48+
BPF_TEST_FLAGS += -test.v
49+
endif
50+
ifeq ($(COVER),1)
51+
ifndef COVERFORMAT
52+
COVERFORMAT:=html
53+
endif
54+
BPF_TEST_FLAGS += -coverage-report $(ROOT_DIR)/bpf-coverage.$(COVERFORMAT) -coverage-format $(COVERFORMAT)
55+
ifdef NOCOVER
56+
BPF_TEST_FLAGS += -no-test-coverage "$(NOCOVER)"
57+
endif
58+
endif
59+
ifeq ($(INSTRLOG),1)
60+
BPF_TEST_FLAGS += -instrumentation-log $(ROOT_DIR)/test/bpf-instrumentation.log
61+
endif
62+
ifdef RUN
63+
BPF_TEST_FLAGS += -run $(RUN)
64+
endif
65+
ifdef BPF_TEST_DUMP_CTX
66+
BPF_TEST_FLAGS += -dump-ctx
67+
endif
68+
ifdef BPF_TEST_FILE
69+
BPF_TEST_FLAGS += -test $(BPF_TEST_FILE)
70+
endif
71+
72+
run: all
73+
$(QUIET)echo "rebuild and run bpf tests"
74+
$(GO) test ./bpftest -bpf-ut-path $(ROOT_DIR)/test/bpf_ut $(BPF_TEST_FLAGS)
75+
76+
-include $(TEST_OBJECTS:.o=.d)

0 commit comments

Comments
 (0)