[PATCH bpf-next 04/11] selftests/bpf: add test_progs flavor using libbpf as a shared lib

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add test_progs-shared flavor to compile against libbpf as a shared
library. This is useful to make sure that libbpf's backwards/forward
compatibility guarantees are upheld. Currently this has to be checked
locally, but in the future we'll automate at least some scenarios as
part of libbpf CI runs.

Biggest change is how either libbpf.a or libbpf.so is passed to the
compiler, which is controled on per-flavor through a new TRUNNER_LIBBPF
parameter. All the places that depend on libbpf artifacts (headers,
library itself, etc) to be built are moved to order-only dependency on
$(BPFOBJ). rpath is used to specify relative location to where libbpf.so
should be so that when test_progs-shared is run under QEMU, libbpf.so is
still going to be discovered correctly.

Few selftests are using or testing internal libbpf APIs, so are not
compatible with shared library use of libbpf. Filter them out for shared
flavor.

Signed-off-by: Andrii Nakryiko <andrii@xxxxxxxxxx>
---
 tools/testing/selftests/bpf/.gitignore |  2 ++
 tools/testing/selftests/bpf/Makefile   | 36 +++++++++++++++++++-------
 2 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/tools/testing/selftests/bpf/.gitignore b/tools/testing/selftests/bpf/.gitignore
index b6bc56c8127a..5adfac44c478 100644
--- a/tools/testing/selftests/bpf/.gitignore
+++ b/tools/testing/selftests/bpf/.gitignore
@@ -32,8 +32,10 @@ test_cpp
 *.tests.h
 /test_progs
 /test_progs-no_alu32
+/test_progs-shared
 /test_progs-bpf_gcc
 /no_alu32
+/shared
 /bpf_gcc
 /tools
 /runqslower
diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index 33f153a9de4c..42bc7913cc1a 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -32,16 +32,19 @@ ifneq ($(LLVM),)
 CFLAGS += -Wno-unused-command-line-argument
 endif
 
+TEST_RUNNERS = test_maps test_progs test_progs-no_alu32 test_progs-shared
+
 # Order correspond to 'make run_tests' order
-TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
+TEST_GEN_PROGS = test_verifier test_tag test_lru_map test_lpm_map \
 	test_verifier_log test_dev_cgroup \
 	test_sock test_sockmap get_cgroup_id_user \
 	test_cgroup_storage \
 	test_tcpnotify_user test_sysctl \
-	test_progs-no_alu32
+	$(TEST_RUNNERS)
 
 # Also test bpf-gcc, if present
 ifneq ($(BPF_GCC),)
+TEST_RUNNERS += test_progs-bpf_gcc
 TEST_GEN_PROGS += test_progs-bpf_gcc
 endif
 
@@ -190,7 +193,9 @@ $(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL) $(RUNQSLOWER_OUTPUT)
 
 TEST_GEN_PROGS_EXTENDED += $(DEFAULT_BPFTOOL)
 
-$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(BPFOBJ)
+# Add libbpf.a dependency to all the test binaries but those in $(TEST_RUNNERS)
+$(filter-out $(addprefix %,$(TEST_RUNNERS)),$(TEST_GEN_PROGS)): $(BPFOBJ)
+$(TEST_GEN_PROGS_EXTENDED): $(BPFOBJ)
 
 $(OUTPUT)/test_dev_cgroup: cgroup_helpers.c testing_helpers.o
 $(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c testing_helpers.o
@@ -436,7 +441,7 @@ $(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o:			\
 		      $(TRUNNER_BPF_SKELS)				\
 		      $(TRUNNER_BPF_LSKELS)				\
 		      $(TRUNNER_BPF_SKELS_LINKED)			\
-		      $$(BPFOBJ) | $(TRUNNER_OUTPUT)
+		      | $$(BPFOBJ) $(TRUNNER_OUTPUT)
 	$$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@)
 	$(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
 
@@ -444,7 +449,7 @@ $(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o:				\
 		       %.c						\
 		       $(TRUNNER_EXTRA_HDRS)				\
 		       $(TRUNNER_TESTS_HDR)				\
-		       $$(BPFOBJ) | $(TRUNNER_OUTPUT)
+		       | $$(BPFOBJ) $(TRUNNER_OUTPUT)
 	$$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@)
 	$(Q)$$(CC) $$(CFLAGS) -DTESTS_HDR=\"$(TRUNNER_TESTS_HDR)\" -c $$< $$(LDLIBS) -o $$@
 
@@ -457,11 +462,11 @@ ifneq ($2:$(OUTPUT),:$(shell pwd))
 endif
 
 $(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS)			\
-			     $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ)		\
+			     $(TRUNNER_EXTRA_OBJS)			\
 			     $(RESOLVE_BTFIDS)				\
-			     | $(TRUNNER_BINARY)-extras
+			     | $$(BPF_OBJ) $(TRUNNER_BINARY)-extras
 	$$(call msg,BINARY,,$$@)
-	$(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
+	$(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $(TRUNNER_LIBBPF) $$(LDLIBS) $(TRUNNER_EXTRA_CFLAGS) -o $$@
 	$(Q)$(RESOLVE_BTFIDS) --btf $(TRUNNER_OUTPUT)/btf_data.o $$@
 
 endef
@@ -477,17 +482,29 @@ TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko	\
 		       $(wildcard progs/btf_dump_test_case_*.c)
 TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
 TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS) -DENABLE_ATOMICS_TESTS
+TRUNNER_LIBBPF := $(BPFOBJ)
 $(eval $(call DEFINE_TEST_RUNNER,test_progs))
 
 # Define test_progs-no_alu32 test runner.
 TRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE
 TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
+TRUNNER_LIBBPF := $(BPFOBJ)
 $(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32))
 
+# Define test_progs-shared test runner.
+TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
+TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS) -DENABLE_ATOMICS_TESTS
+TRUNNER_EXTRA_CFLAGS := -Wl,-rpath=$(subst $(CURDIR)/,,$(dir $(BPFOBJ)))
+TRUNNER_LIBBPF := $(patsubst %libbpf.a,%libbpf.so,$(BPFOBJ))
+TRUNNER_TESTS_BLACKLIST := cpu_mask.c hashmap.c perf_buffer.c raw_tp_test_run.c
+$(eval $(call DEFINE_TEST_RUNNER,test_progs,shared))
+TRUNNER_TESTS_BLACKLIST :=
+
 # Define test_progs BPF-GCC-flavored test runner.
 ifneq ($(BPF_GCC),)
 TRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE
 TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc)
+TRUNNER_LIBBPF := $(BPFOBJ)
 $(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc))
 endif
 
@@ -542,6 +559,7 @@ $(OUTPUT)/bench: $(OUTPUT)/bench.o $(OUTPUT)/testing_helpers.o \
 
 EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) $(HOST_SCRATCH_DIR)	\
 	*.tests.h verifier/tests.h					\
-	$(addprefix $(OUTPUT)/,*.o *.skel.h *.lskel.h no_alu32 bpf_gcc bpf_testmod.ko)
+	$(addprefix $(OUTPUT)/,*.o *.skel.h *.lskel.h)			\
+	$(addprefix $(OUTPUT)/,no_alu32 shared bpf_gcc bpf_testmod.ko)
 
 .PHONY: docs docs-clean
-- 
2.30.2




[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux