@@ -35,3 +35,4 @@ test_cpp
/tools
/runqslower
/bench
+*.ko
@@ -82,7 +82,7 @@ TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \
flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
test_lirc_mode2_user xdping test_cpp runqslower bench
-TEST_CUSTOM_PROGS = urandom_read
+TEST_CUSTOM_PROGS = urandom_read bpf_sidecar.ko
# Emit succinct information message describing current building step
# $1 - generic step name (e.g., CC, LINK, etc);
@@ -104,6 +104,7 @@ OVERRIDE_TARGETS := 1
override define CLEAN
$(call msg,CLEAN)
$(Q)$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
+ $(Q)$(MAKE) -C bpf_sidecar clean
endef
include ../lib.mk
@@ -136,6 +137,11 @@ $(OUTPUT)/urandom_read: urandom_read.c
$(call msg,BINARY,,$@)
$(Q)$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS) -Wl,--build-id=sha1
+$(OUTPUT)/bpf_sidecar.ko: $(wildcard bpf_sidecar/Makefile bpf_sidecar/*.[ch])
+ $(call msg,MOD,,$@)
+ $(Q)$(MAKE) $(submake_extras) -C bpf_sidecar
+ $(Q)cp bpf_sidecar/bpf_sidecar.ko $@
+
$(OUTPUT)/test_stub.o: test_stub.c $(BPFOBJ)
$(call msg,CC,,$@)
$(Q)$(CC) -c $(CFLAGS) -o $@ $<
@@ -388,7 +394,7 @@ TRUNNER_BPF_PROGS_DIR := progs
TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \
network_helpers.c testing_helpers.c \
btf_helpers.c flow_dissector_load.h
-TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \
+TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_sidecar.ko \
$(wildcard progs/btf_dump_test_case_*.c)
TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
@@ -459,4 +465,4 @@ $(OUTPUT)/bench: $(OUTPUT)/bench.o $(OUTPUT)/testing_helpers.o \
EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) \
prog_tests/tests.h map_tests/tests.h verifier/tests.h \
feature \
- $(addprefix $(OUTPUT)/,*.o *.skel.h no_alu32 bpf_gcc)
+ $(addprefix $(OUTPUT)/,*.o *.skel.h no_alu32 bpf_gcc bpf_sidecar.ko)
new file mode 100644
@@ -0,0 +1,6 @@
+*.mod
+*.mod.c
+*.o
+.ko
+/Module.symvers
+/modules.order
new file mode 100644
@@ -0,0 +1,20 @@
+BPF_SIDECAR_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
+KDIR ?= $(abspath $(BPF_SIDECAR_DIR)/../../../../..)
+
+ifeq ($(V),1)
+Q =
+else
+Q = @
+endif
+
+MODULES = bpf_sidecar.ko
+
+obj-m += bpf_sidecar.o
+CFLAGS_bpf_sidecar.o = -I$(src)
+
+all:
+ +$(Q)make -C $(KDIR) M=$(BPF_SIDECAR_DIR) modules
+
+clean:
+ +$(Q)make -C $(KDIR) M=$(BPF_SIDECAR_DIR) clean
+
new file mode 100644
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2020 Facebook */
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM bpf_sidecar
+
+#if !defined(_BPF_SIDECAR_EVENTS_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _BPF_SIDECAR_EVENTS_H
+
+#include <linux/tracepoint.h>
+#include "bpf_sidecar.h"
+
+TRACE_EVENT(bpf_sidecar_test_read,
+ TP_PROTO(struct task_struct *task, struct bpf_sidecar_test_read_ctx *ctx),
+ TP_ARGS(task, ctx),
+ TP_STRUCT__entry(
+ __field(pid_t, pid)
+ __array(char, comm, TASK_COMM_LEN)
+ __field(loff_t, off)
+ __field(size_t, len)
+ ),
+ TP_fast_assign(
+ __entry->pid = task->pid;
+ memcpy(__entry->comm, task->comm, TASK_COMM_LEN);
+ __entry->off = ctx->off;
+ __entry->len = ctx->len;
+ ),
+ TP_printk("pid=%d comm=%s off=%llu len=%zu",
+ __entry->pid, __entry->comm, __entry->off, __entry->len)
+);
+
+#endif /* _BPF_SIDECAR_EVENTS_H */
+
+#undef TRACE_INCLUDE_PATH
+#define TRACE_INCLUDE_PATH .
+#define TRACE_INCLUDE_FILE bpf_sidecar-events
+#include <trace/define_trace.h>
new file mode 100644
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
+#include <linux/error-injection.h>
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/sysfs.h>
+#include <linux/tracepoint.h>
+#include "bpf_sidecar.h"
+
+#define CREATE_TRACE_POINTS
+#include "bpf_sidecar-events.h"
+
+static noinline ssize_t
+bpf_sidecar_test_read(struct file *file, struct kobject *kobj,
+ struct bin_attribute *bin_attr,
+ char *buf, loff_t off, size_t len)
+{
+ struct bpf_sidecar_test_read_ctx ctx = {
+ .buf = buf,
+ .off = off,
+ .len = len,
+ };
+
+ trace_bpf_sidecar_test_read(current, &ctx);
+
+ return -EIO; /* always fail */
+}
+ALLOW_ERROR_INJECTION(bpf_sidecar_test_read, ERRNO);
+
+static struct bin_attribute bin_attr_bpf_sidecar_file __ro_after_init = {
+ .attr = { .name = "bpf_sidecar", .mode = 0444, },
+ .read = bpf_sidecar_test_read,
+};
+
+static int bpf_sidecar_init(void)
+{
+ return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_sidecar_file);
+}
+
+static void bpf_sidecar_exit(void)
+{
+ return sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_sidecar_file);
+}
+
+module_init(bpf_sidecar_init);
+module_exit(bpf_sidecar_exit);
+
+MODULE_AUTHOR("Andrii Nakryiko");
+MODULE_DESCRIPTION("BPF selftests sidecar module");
+MODULE_LICENSE("Dual BSD/GPL");
+
new file mode 100644
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2020 Facebook */
+#ifndef _BPF_SIDECAR_H
+#define _BPF_SIDECAR_H
+
+#include <linux/types.h>
+
+struct bpf_sidecar_test_read_ctx {
+ char *buf;
+ loff_t off;
+ size_t len;
+};
+
+#endif /* _BPF_SIDECAR_H */
@@ -360,6 +360,56 @@ int extract_build_id(char *build_id, size_t size)
return -1;
}
+static int finit_module(int fd, const char *param_values, int flags)
+{
+ return syscall(__NR_finit_module, fd, param_values, flags);
+}
+
+static int delete_module(const char *name, int flags)
+{
+ return syscall(__NR_delete_module, name, flags);
+}
+
+void unload_bpf_sidecar_module()
+{
+ if (delete_module("bpf_sidecar", 0)) {
+ if (errno == ENOENT) {
+ if (env.verbosity > VERBOSE_NONE)
+ fprintf(stdout, "bpf_sidecar.ko is already unloaded.\n");
+ return;
+ }
+ fprintf(env.stderr, "Failed to unload bpf_sidecar.ko from kernel: %d\n", -errno);
+ exit(1);
+ }
+ if (env.verbosity > VERBOSE_NONE)
+ fprintf(stdout, "Successfully unloaded bpf_sidecar.ko.\n");
+}
+
+void load_bpf_sidecar_module()
+{
+ int fd;
+
+ /* ensure previous instance of the module is unloaded */
+ unload_bpf_sidecar_module();
+
+ if (env.verbosity > VERBOSE_NONE)
+ fprintf(stdout, "Loading bpf_sidecar.ko...\n");
+
+ fd = open("bpf_sidecar.ko", O_RDONLY);
+ if (fd < 0) {
+ fprintf(env.stderr, "Can't find bpf_sidecar.ko kernel module: %d\n", -errno);
+ exit(1);
+ }
+ if (finit_module(fd, "", 0)) {
+ fprintf(env.stderr, "Failed to load bpf_sidecar.ko into the kernel: %d\n", -errno);
+ exit(1);
+ }
+ close(fd);
+
+ if (env.verbosity > VERBOSE_NONE)
+ fprintf(stdout, "Successfully loaded bpf_sidecar.ko.\n");
+}
+
/* extern declarations for test funcs */
#define DEFINE_TEST(name) extern void test_##name(void);
#include <prog_tests/tests.h>
@@ -678,6 +728,7 @@ int main(int argc, char **argv)
save_netns();
stdio_hijack();
+ load_bpf_sidecar_module();
for (i = 0; i < prog_test_cnt; i++) {
struct prog_test_def *test = &prog_test_defs[i];
@@ -722,6 +773,7 @@ int main(int argc, char **argv)
if (test->need_cgroup_cleanup)
cleanup_cgroup_environment();
}
+ unload_bpf_sidecar_module();
stdio_restore();
if (env.get_test_cnt) {
Add bpf_sidecar module, which is conceptually out-of-tree module and provides ways for selftests/bpf to test various kernel module-related functionality: raw tracepoint, fentry/fexit/fmod_ret, etc. This module will be auto-loaded by test_progs test runner and expected by some of selftests to be present and loaded. Signed-off-by: Andrii Nakryiko <andrii@kernel.org> --- tools/testing/selftests/bpf/.gitignore | 1 + tools/testing/selftests/bpf/Makefile | 12 +++-- .../selftests/bpf/bpf_sidecar/.gitignore | 6 +++ .../selftests/bpf/bpf_sidecar/Makefile | 20 +++++++ .../bpf/bpf_sidecar/bpf_sidecar-events.h | 36 +++++++++++++ .../selftests/bpf/bpf_sidecar/bpf_sidecar.c | 51 ++++++++++++++++++ .../selftests/bpf/bpf_sidecar/bpf_sidecar.h | 14 +++++ tools/testing/selftests/bpf/test_progs.c | 52 +++++++++++++++++++ 8 files changed, 189 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/bpf/bpf_sidecar/.gitignore create mode 100644 tools/testing/selftests/bpf/bpf_sidecar/Makefile create mode 100644 tools/testing/selftests/bpf/bpf_sidecar/bpf_sidecar-events.h create mode 100644 tools/testing/selftests/bpf/bpf_sidecar/bpf_sidecar.c create mode 100644 tools/testing/selftests/bpf/bpf_sidecar/bpf_sidecar.h