xref: /openbmc/linux/tools/testing/selftests/bpf/prog_tests/timer.c (revision e8c127b0576660da9195504fe8393fe9da3de9ce)
13540f7c6SAlexei Starovoitov // SPDX-License-Identifier: GPL-2.0
23540f7c6SAlexei Starovoitov /* Copyright (c) 2021 Facebook */
33540f7c6SAlexei Starovoitov #include <test_progs.h>
43540f7c6SAlexei Starovoitov #include "timer.skel.h"
5*57ddeb86SDavid Vernet #include "timer_failure.skel.h"
63540f7c6SAlexei Starovoitov 
timer(struct timer * timer_skel)73540f7c6SAlexei Starovoitov static int timer(struct timer *timer_skel)
83540f7c6SAlexei Starovoitov {
93540f7c6SAlexei Starovoitov 	int err, prog_fd;
1004fcb5f9SDelyan Kratunov 	LIBBPF_OPTS(bpf_test_run_opts, topts);
113540f7c6SAlexei Starovoitov 
123540f7c6SAlexei Starovoitov 	err = timer__attach(timer_skel);
133540f7c6SAlexei Starovoitov 	if (!ASSERT_OK(err, "timer_attach"))
143540f7c6SAlexei Starovoitov 		return err;
153540f7c6SAlexei Starovoitov 
163540f7c6SAlexei Starovoitov 	ASSERT_EQ(timer_skel->data->callback_check, 52, "callback_check1");
173540f7c6SAlexei Starovoitov 	ASSERT_EQ(timer_skel->data->callback2_check, 52, "callback2_check1");
183540f7c6SAlexei Starovoitov 
193540f7c6SAlexei Starovoitov 	prog_fd = bpf_program__fd(timer_skel->progs.test1);
2004fcb5f9SDelyan Kratunov 	err = bpf_prog_test_run_opts(prog_fd, &topts);
213540f7c6SAlexei Starovoitov 	ASSERT_OK(err, "test_run");
2204fcb5f9SDelyan Kratunov 	ASSERT_EQ(topts.retval, 0, "test_run");
233540f7c6SAlexei Starovoitov 	timer__detach(timer_skel);
243540f7c6SAlexei Starovoitov 
253540f7c6SAlexei Starovoitov 	usleep(50); /* 10 usecs should be enough, but give it extra */
263540f7c6SAlexei Starovoitov 	/* check that timer_cb1() was executed 10+10 times */
273540f7c6SAlexei Starovoitov 	ASSERT_EQ(timer_skel->data->callback_check, 42, "callback_check2");
283540f7c6SAlexei Starovoitov 	ASSERT_EQ(timer_skel->data->callback2_check, 42, "callback2_check2");
293540f7c6SAlexei Starovoitov 
303540f7c6SAlexei Starovoitov 	/* check that timer_cb2() was executed twice */
313540f7c6SAlexei Starovoitov 	ASSERT_EQ(timer_skel->bss->bss_data, 10, "bss_data");
323540f7c6SAlexei Starovoitov 
33944459e8STero Kristo 	/* check that timer_cb3() was executed twice */
34944459e8STero Kristo 	ASSERT_EQ(timer_skel->bss->abs_data, 12, "abs_data");
35944459e8STero Kristo 
363540f7c6SAlexei Starovoitov 	/* check that there were no errors in timer execution */
373540f7c6SAlexei Starovoitov 	ASSERT_EQ(timer_skel->bss->err, 0, "err");
383540f7c6SAlexei Starovoitov 
393540f7c6SAlexei Starovoitov 	/* check that code paths completed */
403540f7c6SAlexei Starovoitov 	ASSERT_EQ(timer_skel->bss->ok, 1 | 2 | 4, "ok");
413540f7c6SAlexei Starovoitov 
423540f7c6SAlexei Starovoitov 	return 0;
433540f7c6SAlexei Starovoitov }
443540f7c6SAlexei Starovoitov 
45d3f7b166SYucong Sun /* TODO: use pid filtering */
serial_test_timer(void)46d3f7b166SYucong Sun void serial_test_timer(void)
473540f7c6SAlexei Starovoitov {
483540f7c6SAlexei Starovoitov 	struct timer *timer_skel = NULL;
493540f7c6SAlexei Starovoitov 	int err;
503540f7c6SAlexei Starovoitov 
513540f7c6SAlexei Starovoitov 	timer_skel = timer__open_and_load();
523540f7c6SAlexei Starovoitov 	if (!ASSERT_OK_PTR(timer_skel, "timer_skel_load"))
53*57ddeb86SDavid Vernet 		return;
543540f7c6SAlexei Starovoitov 
553540f7c6SAlexei Starovoitov 	err = timer(timer_skel);
563540f7c6SAlexei Starovoitov 	ASSERT_OK(err, "timer");
573540f7c6SAlexei Starovoitov 	timer__destroy(timer_skel);
58*57ddeb86SDavid Vernet 
59*57ddeb86SDavid Vernet 	RUN_TESTS(timer_failure);
603540f7c6SAlexei Starovoitov }
61