-
Notifications
You must be signed in to change notification settings - Fork 384
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
bpf: unit tests and fixes for prepend_name
function
#1902
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: BPF Unit Tests | ||
on: | ||
pull_request: | ||
paths: | ||
- 'bpf/**' | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- 'bpf/**' | ||
|
||
jobs: | ||
build: | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
matrix: | ||
os: [ ubuntu-22.04, actuated-arm64-4cpu-8gb ] | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 | ||
|
||
- name: Install Go | ||
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0 | ||
with: | ||
# renovate: datasource=golang-version depName=go | ||
go-version: '1.21.5' | ||
|
||
- name: Install LLVM | ||
run: sudo apt-get -y install clang llvm | ||
|
||
- name: Run BPF unit test | ||
run: make bpf-test BPFGOTESTFLAGS="-v" |
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,36 @@ | ||
SHELL=/bin/bash # needed for the *.{o,ll,i,s} pattern in the clean target | ||
|
||
CLANG ?= clang | ||
LLC ?= llc | ||
|
||
# Build the BPF programs for the detected architecture, default to x86, and | ||
# allow easy overriding by using ?= for cross-compilation | ||
UNAME_M := $(shell uname -m) | ||
ifeq ($(UNAME_M),x86_64) | ||
BPF_TARGET_ARCH ?= x86 | ||
endif | ||
ifeq ($(UNAME_M),aarch64) | ||
BPF_TARGET_ARCH ?= arm64 | ||
endif | ||
BPF_TARGET_ARCH ?= x86 | ||
|
||
ROOT_DIR := $(dir $(lastword $(MAKEFILE_LIST))) | ||
|
||
IDIR = $(ROOT_DIR)include/ | ||
LIBBPF = $(ROOT_DIR)libbpf/ | ||
LDIR = $(ROOT_DIR)lib | ||
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS)) | ||
|
||
FLAGS := -I$(ROOT_DIR) \ | ||
-Wall -Werror \ | ||
-Wno-address-of-packed-member -Wno-compare-distinct-pointer-types -Wno-unknown-warning-option \ | ||
-O2 | ||
|
||
DEBUG ?= 0 | ||
ifeq ($(DEBUG),1) | ||
__DEBUG_FLAGS = -DTETRAGON_BPF_DEBUG | ||
endif | ||
|
||
CLANG_FLAGS += $(FLAGS) -I $(LIBBPF) -I $(IDIR) -I $(LDIR) -target bpf -emit-llvm -g -D__TARGET_ARCH_$(BPF_TARGET_ARCH) -fdebug-default-version=4 $(__DEBUG_FLAGS) | ||
LLC_FLAGS := -march=bpf -mcpu=v2 -mattr=dwarfris | ||
LLC_FLAGS_ALU32 := -march=bpf -mcpu=v3 -mattr=dwarfris |
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,30 @@ | ||
include ../Makefile.defs | ||
|
||
TESTS = prepend_name_test.o | ||
|
||
OBJSDIR := objs/ | ||
OBJS := $(addprefix $(OBJSDIR),$(TESTS)) | ||
SUDO ?= sudo | ||
|
||
.PHONY: all | ||
all: $(OBJS) | ||
|
||
.PHONY: test | ||
test: $(OBJS) | ||
go test -exec "$(SUDO)" ./ $(BPFGOTESTFLAGS) | ||
|
||
$(OBJS): | $(OBJSDIR) | ||
|
||
$(OBJSDIR): | ||
mkdir $(OBJSDIR) | ||
|
||
objs/%.ll: %.c | ||
$(CLANG) $(CLANG_FLAGS) -I ../ -c $< -o $@ | ||
|
||
objs/%.o: objs/%.ll | ||
$(LLC) $(LLC_FLAGS) -filetype=obj $< -o $@ | ||
|
||
.PHONY: clean | ||
clean: | ||
@$(ECHO_CLEAN) | ||
$(QUIET)rm -f $(OBJSDIR)*.{o,ll,i,s} |
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,56 @@ | ||
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) | ||
/* Copyright Authors of Cilium */ | ||
|
||
//go:build ignore | ||
|
||
#include "vmlinux.h" | ||
|
||
#include "bpf_tracing.h" // bpf_printk | ||
|
||
#include "bpf_task.h" | ||
#include "process/bpf_process_event.h" | ||
|
||
char _license[] __attribute__((section("license"), used)) = "Dual BSD/GPL"; | ||
|
||
#define TEST_MAX_BUF_LEN 256 | ||
#define NAME_MAX 255 | ||
|
||
struct test_prepend_name_state_map_value { | ||
char buf[TEST_MAX_BUF_LEN]; | ||
u64 buflen; | ||
char dname[NAME_MAX]; | ||
char pad; | ||
u32 dlen; | ||
u32 offset; | ||
}; | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_ARRAY); | ||
__uint(max_entries, 1); | ||
__type(key, int); | ||
__type(value, struct test_prepend_name_state_map_value); | ||
} test_prepend_name_state_map SEC(".maps"); | ||
|
||
__attribute__((section("raw_tracepoint/test"), used)) int | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This BPF unit testing is extremely valuable and honestly long overdue. Thanks for adding the first of hopefully many to come |
||
test_prepend_name() | ||
{ | ||
struct test_prepend_name_state_map_value *ts; | ||
int zero = 0; | ||
|
||
ts = map_lookup_elem(&test_prepend_name_state_map, &zero); | ||
if (!ts) | ||
return 1; | ||
|
||
if (ts->buflen < 0 || ts->buflen > TEST_MAX_BUF_LEN) | ||
return 2; | ||
|
||
char *bufptr = ts->buf + ts->buflen; | ||
|
||
ts->dlen &= 255; | ||
|
||
int ret = prepend_name((char *)&ts->buf, &bufptr, (int *)&ts->buflen, ts->dname, ts->dlen); | ||
|
||
ts->offset = bufptr - (char *)&ts->buf; | ||
|
||
return ret; | ||
} |
Oops, something went wrong.
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.
Question: Why do we need that?
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.
So this is a bit weird but you can't have a
.c
file in the same directory as a go module unless you are using CGO 🤔 most Go commands will returnC source files not allowed when not using cgo
.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 wanted to house the C file with the BPF test harness and the Go file in the same directory so it's needed!