selftests/bpf: Merge test_stub.c into testing_helpers.c
authorAndrii Nakryiko <andrii@kernel.org>
Wed, 3 Nov 2021 22:08:43 +0000 (15:08 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Sun, 7 Nov 2021 16:34:23 +0000 (08:34 -0800)
Move testing prog and object load wrappers (bpf_prog_test_load and
bpf_test_load_program) into testing_helpers.{c,h} and get rid of
otherwise useless test_stub.c. Make testing_helpers.c available to
non-test_progs binaries as well.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Dave Marchevsky <davemarchevsky@fb.com>
Link: https://lore.kernel.org/bpf/20211103220845.2676888-11-andrii@kernel.org
tools/testing/selftests/bpf/Makefile
tools/testing/selftests/bpf/test_stub.c [deleted file]
tools/testing/selftests/bpf/testing_helpers.c
tools/testing/selftests/bpf/testing_helpers.h

index c4497a4..5588c62 100644 (file)
@@ -178,10 +178,6 @@ $(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_tes
        $(Q)$(MAKE) $(submake_extras) -C bpf_testmod
        $(Q)cp bpf_testmod/bpf_testmod.ko $@
 
-$(OUTPUT)/test_stub.o: test_stub.c $(BPFOBJ)
-       $(call msg,CC,,$@)
-       $(Q)$(CC) -c $(CFLAGS) -o $@ $<
-
 DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool
 
 $(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL) $(RUNQSLOWER_OUTPUT)
@@ -194,18 +190,23 @@ $(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL) $(RUNQSLOWER_OUTPUT)
 
 TEST_GEN_PROGS_EXTENDED += $(DEFAULT_BPFTOOL)
 
-$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/test_stub.o $(BPFOBJ)
-
-$(OUTPUT)/test_dev_cgroup: cgroup_helpers.c
-$(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c
-$(OUTPUT)/test_sock: cgroup_helpers.c
-$(OUTPUT)/test_sock_addr: cgroup_helpers.c
-$(OUTPUT)/test_sockmap: cgroup_helpers.c
-$(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c
-$(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c
-$(OUTPUT)/test_cgroup_storage: cgroup_helpers.c
-$(OUTPUT)/test_sock_fields: cgroup_helpers.c
-$(OUTPUT)/test_sysctl: cgroup_helpers.c
+$(TEST_GEN_PROGS) $(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
+$(OUTPUT)/test_sock: cgroup_helpers.c testing_helpers.o
+$(OUTPUT)/test_sock_addr: cgroup_helpers.c testing_helpers.o
+$(OUTPUT)/test_sockmap: cgroup_helpers.c testing_helpers.o
+$(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c testing_helpers.o
+$(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c testing_helpers.o
+$(OUTPUT)/test_cgroup_storage: cgroup_helpers.c testing_helpers.o
+$(OUTPUT)/test_sock_fields: cgroup_helpers.c testing_helpers.o
+$(OUTPUT)/test_sysctl: cgroup_helpers.c testing_helpers.o
+$(OUTPUT)/test_tag: testing_helpers.o
+$(OUTPUT)/test_lirc_mode2_user: testing_helpers.o
+$(OUTPUT)/xdping: testing_helpers.o
+$(OUTPUT)/flow_dissector_load: testing_helpers.o
+$(OUTPUT)/test_maps: testing_helpers.o
 
 BPFTOOL ?= $(DEFAULT_BPFTOOL)
 $(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)    \
diff --git a/tools/testing/selftests/bpf/test_stub.c b/tools/testing/selftests/bpf/test_stub.c
deleted file mode 100644 (file)
index 47e1327..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
-/* Copyright (C) 2019 Netronome Systems, Inc. */
-
-#include <bpf/bpf.h>
-#include <bpf/libbpf.h>
-#include <string.h>
-
-int extra_prog_load_log_flags = 0;
-
-int bpf_prog_test_load(const char *file, enum bpf_prog_type type,
-                      struct bpf_object **pobj, int *prog_fd)
-{
-       struct bpf_prog_load_attr attr;
-
-       memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
-       attr.file = file;
-       attr.prog_type = type;
-       attr.expected_attach_type = 0;
-       attr.prog_flags = BPF_F_TEST_RND_HI32;
-       attr.log_level = extra_prog_load_log_flags;
-
-       return bpf_prog_load_xattr(&attr, pobj, prog_fd);
-}
-
-int bpf_test_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
-                         size_t insns_cnt, const char *license,
-                         __u32 kern_version, char *log_buf,
-                    size_t log_buf_sz)
-{
-       struct bpf_load_program_attr load_attr;
-
-       memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
-       load_attr.prog_type = type;
-       load_attr.expected_attach_type = 0;
-       load_attr.name = NULL;
-       load_attr.insns = insns;
-       load_attr.insns_cnt = insns_cnt;
-       load_attr.license = license;
-       load_attr.kern_version = kern_version;
-       load_attr.prog_flags = BPF_F_TEST_RND_HI32;
-       load_attr.log_level = extra_prog_load_log_flags;
-
-       return bpf_load_program_xattr(&load_attr, log_buf, log_buf_sz);
-}
index 800d503..ef61d43 100644 (file)
@@ -1,7 +1,11 @@
 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+/* Copyright (C) 2019 Netronome Systems, Inc. */
 /* Copyright (C) 2020 Facebook, Inc. */
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
+#include <bpf/bpf.h>
+#include <bpf/libbpf.h>
 #include "testing_helpers.h"
 
 int parse_num_list(const char *s, bool **num_set, int *num_set_len)
@@ -78,3 +82,54 @@ __u32 link_info_prog_id(const struct bpf_link *link, struct bpf_link_info *info)
        }
        return info->prog_id;
 }
+
+int extra_prog_load_log_flags = 0;
+
+int bpf_prog_test_load(const char *file, enum bpf_prog_type type,
+                      struct bpf_object **pobj, int *prog_fd)
+{
+       struct bpf_object *obj;
+       struct bpf_program *prog;
+       int err;
+
+       obj = bpf_object__open(file);
+       if (!obj)
+               return -errno;
+
+       prog = bpf_object__next_program(obj, NULL);
+       if (!prog) {
+               err = -ENOENT;
+               goto err_out;
+       }
+
+       if (type != BPF_PROG_TYPE_UNSPEC)
+               bpf_program__set_type(prog, type);
+
+       err = bpf_object__load(obj);
+       if (err)
+               goto err_out;
+
+       *pobj = obj;
+       *prog_fd = bpf_program__fd(prog);
+
+       return 0;
+err_out:
+       bpf_object__close(obj);
+       return err;
+}
+
+int bpf_test_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
+                         size_t insns_cnt, const char *license,
+                         __u32 kern_version, char *log_buf,
+                         size_t log_buf_sz)
+{
+       LIBBPF_OPTS(bpf_prog_load_opts, opts,
+               .kern_version = kern_version,
+               .prog_flags = BPF_F_TEST_RND_HI32,
+               .log_level = extra_prog_load_log_flags,
+               .log_buf = log_buf,
+               .log_size = log_buf_sz,
+       );
+
+       return bpf_prog_load(type, NULL, license, insns, insns_cnt, &opts);
+}
index d4f8e74..f46ebc4 100644 (file)
@@ -6,3 +6,9 @@
 
 int parse_num_list(const char *s, bool **set, int *set_len);
 __u32 link_info_prog_id(const struct bpf_link *link, struct bpf_link_info *info);
+int bpf_prog_test_load(const char *file, enum bpf_prog_type type,
+                      struct bpf_object **pobj, int *prog_fd);
+int bpf_test_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
+                         size_t insns_cnt, const char *license,
+                         __u32 kern_version, char *log_buf,
+                         size_t log_buf_sz);