Add tests to verify the new logs in link_create and xdp link_create, including a log reported via extack when fail to create an xdp link. Signed-off-by: Leon Hwang --- .../selftests/bpf/prog_tests/xdp_link.c | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_link.c b/tools/testing/selftests/bpf/prog_tests/xdp_link.c index e7e9f3c22edf..18c1f1039d4e 100644 --- a/tools/testing/selftests/bpf/prog_tests/xdp_link.c +++ b/tools/testing/selftests/bpf/prog_tests/xdp_link.c @@ -150,3 +150,104 @@ void serial_test_xdp_link(void) test_xdp_link__destroy(skel1); test_xdp_link__destroy(skel2); } + +static void test_link_create(int prog_fd, int ifindex, enum bpf_attach_type attach_type, + struct bpf_link_create_opts *opts, int exp_err, const char *msg) +{ + char log_buf[128] = {}; + int fd; + LIBBPF_OPTS(bpf_log_opts, log_opts, + .buf = log_buf, + .size = sizeof(log_buf), + .level = 1, + ); + + fd = bpf_link_create_log(prog_fd, ifindex, attach_type, opts, &log_opts); + if (!ASSERT_LT(fd, 0, "bpf_link_create_log")) { + close(fd); + return; + } + + ASSERT_EQ(fd, exp_err, "fd"); + ASSERT_STREQ(log_buf, msg, "log_buf"); + ASSERT_EQ(log_opts.true_size, strlen(msg) + 1, "log_size"); +} + +static void test_link_create_invalid_prog_fd(void) +{ + const char *msg = "Invalid prog_fd.\n"; + + test_link_create(-1, IFINDEX_LO, BPF_XDP, NULL, -EBADF, msg); +} + +static void test_link_create_invalid_prog_type(void) +{ + const struct bpf_insn insns[] = { + BPF_MOV64_IMM(BPF_REG_0, 0), + BPF_EXIT_INSN(), + }; + const char *msg = "Invalid prog type.\n"; + int prog_fd; + + prog_fd = bpf_prog_load(BPF_PROG_TYPE_LIRC_MODE2, NULL, "GPL", insns, + ARRAY_SIZE(insns), NULL); + if (!ASSERT_GE(prog_fd, 0, "bpf_prog_load")) + return; + + test_link_create(prog_fd, 0, BPF_LIRC_MODE2, NULL, -EINVAL, msg); + + close(prog_fd); +} + +static void test_link_create_attach(int ifindex, enum bpf_attach_type attach_type, + struct bpf_link_create_opts *opts, const char *msg) +{ + struct test_xdp_link *skel; + + skel = test_xdp_link__open_and_load(); + if (!ASSERT_OK_PTR(skel, "test_xdp_link__open_and_load")) + return; + + test_link_create(bpf_program__fd(skel->progs.xdp_handler), ifindex, attach_type, opts, + -EINVAL, msg); + + test_xdp_link__destroy(skel); +} + +static void test_link_create_invalid_attach_type(void) +{ + const char *msg = "Invalid attach_type.\n"; + + test_link_create_attach(IFINDEX_LO, BPF_CGROUP_INET_INGRESS, NULL, msg); +} + +static void test_link_create_xdp_invalid_target_ifindex(void) +{ + const char *msg = "Invalid target_ifindex.\n"; + + test_link_create_attach(0, BPF_XDP, NULL, msg); +} + +static void test_link_create_xdp_extack(void) +{ + const char *msg = "Invalid XDP flags for BPF link attachment\n"; + LIBBPF_OPTS(bpf_link_create_opts, opts, + .flags = XDP_FLAGS_REPLACE, + ); + + test_link_create_attach(IFINDEX_LO, BPF_XDP, &opts, msg); +} + +void test_link_create_log(void) +{ + if (test__start_subtest("invalid_prog_fd")) + test_link_create_invalid_prog_fd(); + if (test__start_subtest("invalid_prog_type")) + test_link_create_invalid_prog_type(); + if (test__start_subtest("invalid_attach_type")) + test_link_create_invalid_attach_type(); + if (test__start_subtest("xdp/invalid_target_ifindex")) + test_link_create_xdp_invalid_target_ifindex(); + if (test__start_subtest("xdp/extack")) + test_link_create_xdp_extack(); +} -- 2.55.0