selftests/bpf: Add testcase for async callback return value failure
[platform/kernel/linux-starfive.git] / tools / testing / selftests / bpf / prog_tests / timer.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
3 #include <test_progs.h>
4 #include "timer.skel.h"
5 #include "timer_failure.skel.h"
6
7 static int timer(struct timer *timer_skel)
8 {
9         int err, prog_fd;
10         LIBBPF_OPTS(bpf_test_run_opts, topts);
11
12         err = timer__attach(timer_skel);
13         if (!ASSERT_OK(err, "timer_attach"))
14                 return err;
15
16         ASSERT_EQ(timer_skel->data->callback_check, 52, "callback_check1");
17         ASSERT_EQ(timer_skel->data->callback2_check, 52, "callback2_check1");
18
19         prog_fd = bpf_program__fd(timer_skel->progs.test1);
20         err = bpf_prog_test_run_opts(prog_fd, &topts);
21         ASSERT_OK(err, "test_run");
22         ASSERT_EQ(topts.retval, 0, "test_run");
23         timer__detach(timer_skel);
24
25         usleep(50); /* 10 usecs should be enough, but give it extra */
26         /* check that timer_cb1() was executed 10+10 times */
27         ASSERT_EQ(timer_skel->data->callback_check, 42, "callback_check2");
28         ASSERT_EQ(timer_skel->data->callback2_check, 42, "callback2_check2");
29
30         /* check that timer_cb2() was executed twice */
31         ASSERT_EQ(timer_skel->bss->bss_data, 10, "bss_data");
32
33         /* check that timer_cb3() was executed twice */
34         ASSERT_EQ(timer_skel->bss->abs_data, 12, "abs_data");
35
36         /* check that there were no errors in timer execution */
37         ASSERT_EQ(timer_skel->bss->err, 0, "err");
38
39         /* check that code paths completed */
40         ASSERT_EQ(timer_skel->bss->ok, 1 | 2 | 4, "ok");
41
42         return 0;
43 }
44
45 /* TODO: use pid filtering */
46 void serial_test_timer(void)
47 {
48         struct timer *timer_skel = NULL;
49         int err;
50
51         timer_skel = timer__open_and_load();
52         if (!ASSERT_OK_PTR(timer_skel, "timer_skel_load"))
53                 return;
54
55         err = timer(timer_skel);
56         ASSERT_OK(err, "timer");
57         timer__destroy(timer_skel);
58
59         RUN_TESTS(timer_failure);
60 }