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 
timer_mim(struct timer_mim * timer_skel)7 static int timer_mim(struct timer_mim *timer_skel)
8 {
9 	__u64 cnt1, cnt2;
10 	int err, prog_fd, key1 = 1;
11 	LIBBPF_OPTS(bpf_test_run_opts, topts);
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_opts(prog_fd, &topts);
19 	ASSERT_OK(err, "test_run");
20 	ASSERT_EQ(topts.retval, 0, "test_run");
21 	timer_mim__detach(timer_skel);
22 
23 	/* check that timer_cb[12] are incrementing 'cnt' */
24 	cnt1 = READ_ONCE(timer_skel->bss->cnt);
25 	for (int i = 0; i < 100; i++) {
26 		cnt2 = READ_ONCE(timer_skel->bss->cnt);
27 		if (cnt2 != cnt1)
28 			break;
29 		usleep(200); /* 100 times more than interval */
30 	}
31 	ASSERT_GT(cnt2, cnt1, "cnt");
32 
33 	ASSERT_EQ(timer_skel->bss->err, 0, "err");
34 	/* check that code paths completed */
35 	ASSERT_EQ(timer_skel->bss->ok, 1 | 2, "ok");
36 
37 	close(bpf_map__fd(timer_skel->maps.inner_htab));
38 	err = bpf_map__delete_elem(timer_skel->maps.outer_arr, &key1, sizeof(key1), 0);
39 	ASSERT_EQ(err, 0, "delete inner map");
40 
41 	/* check that timer_cb[12] are no longer running */
42 	cnt1 = READ_ONCE(timer_skel->bss->cnt);
43 	for (int i = 0; i < 100; i++) {
44 		usleep(200); /* 100 times more than interval */
45 		cnt2 = READ_ONCE(timer_skel->bss->cnt);
46 		if (cnt2 == cnt1)
47 			break;
48 	}
49 	ASSERT_EQ(cnt2, cnt1, "cnt");
50 
51 	return 0;
52 }
53 
serial_test_timer_mim(void)54 void serial_test_timer_mim(void)
55 {
56 	struct timer_mim_reject *timer_reject_skel = NULL;
57 	libbpf_print_fn_t old_print_fn = NULL;
58 	struct timer_mim *timer_skel = NULL;
59 	int err;
60 
61 	old_print_fn = libbpf_set_print(NULL);
62 	timer_reject_skel = timer_mim_reject__open_and_load();
63 	libbpf_set_print(old_print_fn);
64 	if (!ASSERT_ERR_PTR(timer_reject_skel, "timer_reject_skel_load"))
65 		goto cleanup;
66 
67 	timer_skel = timer_mim__open_and_load();
68 	if (!ASSERT_OK_PTR(timer_skel, "timer_skel_load"))
69 		goto cleanup;
70 
71 	err = timer_mim(timer_skel);
72 	ASSERT_OK(err, "timer_mim");
73 cleanup:
74 	timer_mim__destroy(timer_skel);
75 	timer_mim_reject__destroy(timer_reject_skel);
76 }
77