1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
3 #include <test_progs.h>
4 #include "timer_mim.skel.h"
5 #include "timer_mim_reject.skel.h"
6 
7 static int timer_mim(struct timer_mim *timer_skel)
8 {
9 	__u32 duration = 0, retval;
10 	__u64 cnt1, cnt2;
11 	int err, prog_fd, key1 = 1;
12 
13 	err = timer_mim__attach(timer_skel);
14 	if (!ASSERT_OK(err, "timer_attach"))
15 		return err;
16 
17 	prog_fd = bpf_program__fd(timer_skel->progs.test1);
18 	err = bpf_prog_test_run(prog_fd, 1, NULL, 0,
19 				NULL, NULL, &retval, &duration);
20 	ASSERT_OK(err, "test_run");
21 	ASSERT_EQ(retval, 0, "test_run");
22 	timer_mim__detach(timer_skel);
23 
24 	/* check that timer_cb[12] are incrementing 'cnt' */
25 	cnt1 = READ_ONCE(timer_skel->bss->cnt);
26 	for (int i = 0; i < 100; i++) {
27 		cnt2 = READ_ONCE(timer_skel->bss->cnt);
28 		if (cnt2 != cnt1)
29 			break;
30 		usleep(200); /* 100 times more than interval */
31 	}
32 	ASSERT_GT(cnt2, cnt1, "cnt");
33 
34 	ASSERT_EQ(timer_skel->bss->err, 0, "err");
35 	/* check that code paths completed */
36 	ASSERT_EQ(timer_skel->bss->ok, 1 | 2, "ok");
37 
38 	close(bpf_map__fd(timer_skel->maps.inner_htab));
39 	err = bpf_map_delete_elem(bpf_map__fd(timer_skel->maps.outer_arr), &key1);
40 	ASSERT_EQ(err, 0, "delete inner map");
41 
42 	/* check that timer_cb[12] are no longer running */
43 	cnt1 = READ_ONCE(timer_skel->bss->cnt);
44 	for (int i = 0; i < 100; i++) {
45 		usleep(200); /* 100 times more than interval */
46 		cnt2 = READ_ONCE(timer_skel->bss->cnt);
47 		if (cnt2 == cnt1)
48 			break;
49 	}
50 	ASSERT_EQ(cnt2, cnt1, "cnt");
51 
52 	return 0;
53 }
54 
55 void serial_test_timer_mim(void)
56 {
57 	struct timer_mim_reject *timer_reject_skel = NULL;
58 	libbpf_print_fn_t old_print_fn = NULL;
59 	struct timer_mim *timer_skel = NULL;
60 	int err;
61 
62 	old_print_fn = libbpf_set_print(NULL);
63 	timer_reject_skel = timer_mim_reject__open_and_load();
64 	libbpf_set_print(old_print_fn);
65 	if (!ASSERT_ERR_PTR(timer_reject_skel, "timer_reject_skel_load"))
66 		goto cleanup;
67 
68 	timer_skel = timer_mim__open_and_load();
69 	if (!ASSERT_OK_PTR(timer_skel, "timer_skel_load"))
70 		goto cleanup;
71 
72 	err = timer_mim(timer_skel);
73 	ASSERT_OK(err, "timer_mim");
74 cleanup:
75 	timer_mim__destroy(timer_skel);
76 	timer_mim_reject__destroy(timer_reject_skel);
77 }
78