]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
selftests/bpf: test struct_ops with epoll
authorKui-Feng Lee <thinker.li@gmail.com>
Thu, 30 May 2024 06:59:43 +0000 (23:59 -0700)
committerMartin KaFai Lau <martin.lau@kernel.org>
Thu, 30 May 2024 22:34:14 +0000 (15:34 -0700)
Verify whether a user space program is informed through epoll with EPOLLHUP
when a struct_ops object is detached.

The BPF code in selftests/bpf/progs/struct_ops_module.c has become
complex. Therefore, struct_ops_detach.c has been added to segregate the BPF
code for detachment tests from the BPF code for other tests based on the
recommendation of Andrii Nakryiko.

Suggested-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
Link: https://lore.kernel.org/r/20240530065946.979330-6-thinker.li@gmail.com
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c
tools/testing/selftests/bpf/progs/struct_ops_detach.c [new file with mode: 0644]

index 29e183a80f49004d35e69bb39df67b5225a6be6e..bbcf12696a6bc8002b024f45bd181f536d53a3c4 100644 (file)
@@ -3,9 +3,12 @@
 #include <test_progs.h>
 #include <time.h>
 
+#include <sys/epoll.h>
+
 #include "struct_ops_module.skel.h"
 #include "struct_ops_nulled_out_cb.skel.h"
 #include "struct_ops_forgotten_cb.skel.h"
+#include "struct_ops_detach.skel.h"
 
 static void check_map_info(struct bpf_map_info *info)
 {
@@ -242,6 +245,58 @@ cleanup:
        struct_ops_forgotten_cb__destroy(skel);
 }
 
+/* Detach a link from a user space program */
+static void test_detach_link(void)
+{
+       struct epoll_event ev, events[2];
+       struct struct_ops_detach *skel;
+       struct bpf_link *link = NULL;
+       int fd, epollfd = -1, nfds;
+       int err;
+
+       skel = struct_ops_detach__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "struct_ops_detach__open_and_load"))
+               return;
+
+       link = bpf_map__attach_struct_ops(skel->maps.testmod_do_detach);
+       if (!ASSERT_OK_PTR(link, "attach_struct_ops"))
+               goto cleanup;
+
+       fd = bpf_link__fd(link);
+       if (!ASSERT_GE(fd, 0, "link_fd"))
+               goto cleanup;
+
+       epollfd = epoll_create1(0);
+       if (!ASSERT_GE(epollfd, 0, "epoll_create1"))
+               goto cleanup;
+
+       ev.events = EPOLLHUP;
+       ev.data.fd = fd;
+       err = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev);
+       if (!ASSERT_OK(err, "epoll_ctl"))
+               goto cleanup;
+
+       err = bpf_link__detach(link);
+       if (!ASSERT_OK(err, "detach_link"))
+               goto cleanup;
+
+       /* Wait for EPOLLHUP */
+       nfds = epoll_wait(epollfd, events, 2, 500);
+       if (!ASSERT_EQ(nfds, 1, "epoll_wait"))
+               goto cleanup;
+
+       if (!ASSERT_EQ(events[0].data.fd, fd, "epoll_wait_fd"))
+               goto cleanup;
+       if (!ASSERT_TRUE(events[0].events & EPOLLHUP, "events[0].events"))
+               goto cleanup;
+
+cleanup:
+       if (epollfd >= 0)
+               close(epollfd);
+       bpf_link__destroy(link);
+       struct_ops_detach__destroy(skel);
+}
+
 void serial_test_struct_ops_module(void)
 {
        if (test__start_subtest("struct_ops_load"))
@@ -254,5 +309,7 @@ void serial_test_struct_ops_module(void)
                test_struct_ops_nulled_out_cb();
        if (test__start_subtest("struct_ops_forgotten_cb"))
                test_struct_ops_forgotten_cb();
+       if (test__start_subtest("test_detach_link"))
+               test_detach_link();
 }
 
diff --git a/tools/testing/selftests/bpf/progs/struct_ops_detach.c b/tools/testing/selftests/bpf/progs/struct_ops_detach.c
new file mode 100644 (file)
index 0000000..56b787a
--- /dev/null
@@ -0,0 +1,10 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "../bpf_testmod/bpf_testmod.h"
+
+char _license[] SEC("license") = "GPL";
+
+SEC(".struct_ops.link")
+struct bpf_testmod_ops testmod_do_detach;