aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/prog_tests/timer_mim.c
blob: 9ff7843909e7d3bf031a8d4c086321a2f6dc585c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2021 Facebook */
#include <test_progs.h>
#include "timer_mim.skel.h"
#include "timer_mim_reject.skel.h"

static int timer_mim(struct timer_mim *timer_skel)
{
	__u64 cnt1, cnt2;
	int err, prog_fd, key1 = 1;
	LIBBPF_OPTS(bpf_test_run_opts, topts);

	err = timer_mim__attach(timer_skel);
	if (!ASSERT_OK(err, "timer_attach"))
		return err;

	prog_fd = bpf_program__fd(timer_skel->progs.test1);
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	ASSERT_OK(err, "test_run");
	ASSERT_EQ(topts.retval, 0, "test_run");
	timer_mim__detach(timer_skel);

	/* check that timer_cb[12] are incrementing 'cnt' */
	cnt1 = READ_ONCE(timer_skel->bss->cnt);
	for (int i = 0; i < 100; i++) {
		cnt2 = READ_ONCE(timer_skel->bss->cnt);
		if (cnt2 != cnt1)
			break;
		usleep(200); /* 100 times more than interval */
	}
	ASSERT_GT(cnt2, cnt1, "cnt");

	ASSERT_EQ(timer_skel->bss->err, 0, "err");
	/* check that code paths completed */
	ASSERT_EQ(timer_skel->bss->ok, 1 | 2, "ok");

	close(bpf_map__fd(timer_skel->maps.inner_htab));
	err = bpf_map__delete_elem(timer_skel->maps.outer_arr, &key1, sizeof(key1), 0);
	ASSERT_EQ(err, 0, "delete inner map");

	/* check that timer_cb[12] are no longer running */
	cnt1 = READ_ONCE(timer_skel->bss->cnt);
	for (int i = 0; i < 100; i++) {
		usleep(200); /* 100 times more than interval */
		cnt2 = READ_ONCE(timer_skel->bss->cnt);
		if (cnt2 == cnt1)
			break;
	}
	ASSERT_EQ(cnt2, cnt1, "cnt");

	return 0;
}

void serial_test_timer_mim(void)
{
	struct timer_mim_reject *timer_reject_skel = NULL;
	libbpf_print_fn_t old_print_fn = NULL;
	struct timer_mim *timer_skel = NULL;
	int err;

	old_print_fn = libbpf_set_print(NULL);
	timer_reject_skel = timer_mim_reject__open_and_load();
	libbpf_set_print(old_print_fn);
	if (!ASSERT_ERR_PTR(timer_reject_skel, "timer_reject_skel_load"))
		goto cleanup;

	timer_skel = timer_mim__open_and_load();
	if (!ASSERT_OK_PTR(timer_skel, "timer_skel_load"))
		goto cleanup;

	err = timer_mim(timer_skel);
	ASSERT_OK(err, "timer_mim");
cleanup:
	timer_mim__destroy(timer_skel);
	timer_mim_reject__destroy(timer_reject_skel);
}