From: Andrii Nakryiko <andrii@kernel.org>
To: <bpf@vger.kernel.org>, <netdev@vger.kernel.org>, <ast@fb.com>,
<daniel@iogearbox.net>
Cc: <andrii@kernel.org>, <kernel-team@fb.com>
Subject: [PATCH bpf-next 3/3] selftests/bpf: allow compiling BPF objects without BTF
Date: Fri, 19 Mar 2021 13:59:09 -0700 [thread overview]
Message-ID: <20210319205909.1748642-4-andrii@kernel.org> (raw)
In-Reply-To: <20210319205909.1748642-1-andrii@kernel.org>
Add ability to skip BTF generation for some BPF object files. This is done
through using a convention of .nobtf.c file name suffix.
Also add third statically linked file to static_linked selftest. This file has
no BTF, causing resulting object file to have only some of DATASEC BTF types.
It also is using (from BPF code) global variables. This tests both libbpf's
static linking logic and bpftool's skeleton generation logic.
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
tools/testing/selftests/bpf/Makefile | 21 +++++++----
.../selftests/bpf/prog_tests/static_linked.c | 6 +++-
.../bpf/progs/test_static_linked3.nobtf.c | 36 +++++++++++++++++++
3 files changed, 56 insertions(+), 7 deletions(-)
create mode 100644 tools/testing/selftests/bpf/progs/test_static_linked3.nobtf.c
diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index 6448c626498f..0a481a75a416 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -270,7 +270,7 @@ IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \
MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
-BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \
+BPF_CFLAGS = -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \
-I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) \
-I$(abspath $(OUTPUT)/../usr/include)
@@ -282,30 +282,39 @@ $(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline
$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h
-# Build BPF object using Clang
+# Build BPF object using Clang.
+# Source files with .nobtf.c suffix are built without BTF
# $1 - input .c file
# $2 - output .o file
# $3 - CFLAGS
define CLANG_BPF_BUILD_RULE
$(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
- $(Q)$(CLANG) $3 -O2 -target bpf -c $1 -o $2 -mcpu=v3
+ $(Q)$(CLANG) $3 -O2 -target bpf -mcpu=v3 \
+ $(if $(filter %.nobtf.c,$1),,-g) \
+ -c $1 -o $2
endef
# Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32
define CLANG_NOALU32_BPF_BUILD_RULE
$(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
- $(Q)$(CLANG) $3 -O2 -target bpf -c $1 -o $2 -mcpu=v2
+ $(Q)$(CLANG) $3 -O2 -target bpf -mcpu=v2 \
+ $(if $(filter %.nobtf.c,$1),,-g) \
+ -c $1 -o $2
endef
# Build BPF object using GCC
define GCC_BPF_BUILD_RULE
$(call msg,GCC-BPF,$(TRUNNER_BINARY),$2)
- $(Q)$(BPF_GCC) $3 -O2 -c $1 -o $2
+ $(Q)$(BPF_GCC) $3 -O2 \
+ $(if $(filter %.nobtf.c,$1),,-g) \
+ -c $1 -o $2
endef
SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
LINKED_SKELS := test_static_linked.skel.h
-test_static_linked.skel.h-deps := test_static_linked1.o test_static_linked2.o
+test_static_linked.skel.h-deps := test_static_linked1.o \
+ test_static_linked2.o \
+ test_static_linked3.nobtf.o
# Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on
# $eval()) and pass control to DEFINE_TEST_RUNNER_RULES.
diff --git a/tools/testing/selftests/bpf/prog_tests/static_linked.c b/tools/testing/selftests/bpf/prog_tests/static_linked.c
index 46556976dccc..1e6701483d27 100644
--- a/tools/testing/selftests/bpf/prog_tests/static_linked.c
+++ b/tools/testing/selftests/bpf/prog_tests/static_linked.c
@@ -6,7 +6,7 @@
void test_static_linked(void)
{
- int err;
+ int err, key = 0, value = 0;
struct test_static_linked* skel;
skel = test_static_linked__open();
@@ -35,6 +35,10 @@ void test_static_linked(void)
ASSERT_EQ(skel->bss->var1, 1 * 2 + 2 + 3, "var1");
ASSERT_EQ(skel->bss->var2, 4 * 3 + 5 + 6, "var2");
+ err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.legacy_map), &key, &value);
+ ASSERT_OK(err, "legacy_map_lookup");
+ ASSERT_EQ(value, 1 * 3 + 3, "legacy_map_value");
+
cleanup:
test_static_linked__destroy(skel);
}
diff --git a/tools/testing/selftests/bpf/progs/test_static_linked3.nobtf.c b/tools/testing/selftests/bpf/progs/test_static_linked3.nobtf.c
new file mode 100644
index 000000000000..e5fbde21381c
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_static_linked3.nobtf.c
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2021 Facebook */
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+/* global variables don't need BTF to be used, but are extremely unconvenient
+ * to be consumed from user-space without BPF skeleton, that uses BTF
+ */
+
+static volatile int mul3 = 3;
+static volatile int add3 = 3;
+
+/* same "subprog" name in all files */
+static __noinline int subprog(int x)
+{
+ /* but different formula */
+ return x * mul3 + add3;
+}
+
+struct bpf_map_def SEC("maps") legacy_map = {
+ .type = BPF_MAP_TYPE_ARRAY,
+ .key_size = sizeof(int),
+ .value_size = sizeof(int),
+ .max_entries = 1,
+};
+
+SEC("raw_tp/sys_enter")
+int handler3(const void *ctx)
+{
+ int key = 0, value = subprog(1);
+
+ bpf_map_update_elem(&legacy_map, &key, &value, BPF_ANY);
+
+ return 0;
+}
--
2.30.2
next prev parent reply other threads:[~2021-03-19 20:59 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-19 20:59 [PATCH bpf-next 0/3] Handle no-BTF object files better Andrii Nakryiko
2021-03-19 20:59 ` [PATCH bpf-next 1/3] bpftool: improve skeleton generation for objects without BTF Andrii Nakryiko
2021-03-19 20:59 ` [PATCH bpf-next 2/3] libbpf: skip BTF fixup if object file has no BTF Andrii Nakryiko
2021-03-19 21:25 ` Jiri Olsa
2021-03-19 20:59 ` Andrii Nakryiko [this message]
2021-03-20 2:21 ` [PATCH bpf-next 3/3] selftests/bpf: allow compiling BPF objects without BTF Alexei Starovoitov
2021-03-20 17:00 ` Andrii Nakryiko
2021-03-22 1:07 ` Alexei Starovoitov
2021-03-22 16:56 ` Andrii Nakryiko
2021-03-22 17:54 ` Alexei Starovoitov
2021-03-26 16:44 ` Andrii Nakryiko
2021-03-29 1:16 ` Alexei Starovoitov
2021-03-29 6:09 ` Andrii Nakryiko
2021-03-29 18:55 ` Alexei Starovoitov
2021-03-30 18:00 ` Andrii Nakryiko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210319205909.1748642-4-andrii@kernel.org \
--to=andrii@kernel.org \
--cc=ast@fb.com \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@fb.com \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).