xref: /linux/tools/testing/selftests/bpf/prog_tests/timer.c (revision 0ad53fe3ae82443c74ff8cfd7bd13377cc1134a3)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
3 #include <test_progs.h>
4 #include "timer.skel.h"
5 
6 static int timer(struct timer *timer_skel)
7 {
8 	int err, prog_fd;
9 	__u32 duration = 0, retval;
10 
11 	err = timer__attach(timer_skel);
12 	if (!ASSERT_OK(err, "timer_attach"))
13 		return err;
14 
15 	ASSERT_EQ(timer_skel->data->callback_check, 52, "callback_check1");
16 	ASSERT_EQ(timer_skel->data->callback2_check, 52, "callback2_check1");
17 
18 	prog_fd = bpf_program__fd(timer_skel->progs.test1);
19 	err = bpf_prog_test_run(prog_fd, 1, NULL, 0,
20 				NULL, NULL, &retval, &duration);
21 	ASSERT_OK(err, "test_run");
22 	ASSERT_EQ(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 there were no errors in timer execution */
34 	ASSERT_EQ(timer_skel->bss->err, 0, "err");
35 
36 	/* check that code paths completed */
37 	ASSERT_EQ(timer_skel->bss->ok, 1 | 2 | 4, "ok");
38 
39 	return 0;
40 }
41 
42 void test_timer(void)
43 {
44 	struct timer *timer_skel = NULL;
45 	int err;
46 
47 	timer_skel = timer__open_and_load();
48 	if (!ASSERT_OK_PTR(timer_skel, "timer_skel_load"))
49 		goto cleanup;
50 
51 	err = timer(timer_skel);
52 	ASSERT_OK(err, "timer");
53 cleanup:
54 	timer__destroy(timer_skel);
55 }
56