1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2019 Facebook */
3#include <test_progs.h>
4#include "fentry_test.lskel.h"
5#include "fentry_many_args.skel.h"
6
7static int fentry_test_common(struct fentry_test_lskel *fentry_skel)
8{
9	int err, prog_fd, i;
10	int link_fd;
11	__u64 *result;
12	LIBBPF_OPTS(bpf_test_run_opts, topts);
13
14	err = fentry_test_lskel__attach(fentry_skel);
15	if (!ASSERT_OK(err, "fentry_attach"))
16		return err;
17
18	/* Check that already linked program can't be attached again. */
19	link_fd = fentry_test_lskel__test1__attach(fentry_skel);
20	if (!ASSERT_LT(link_fd, 0, "fentry_attach_link"))
21		return -1;
22
23	prog_fd = fentry_skel->progs.test1.prog_fd;
24	err = bpf_prog_test_run_opts(prog_fd, &topts);
25	ASSERT_OK(err, "test_run");
26	ASSERT_EQ(topts.retval, 0, "test_run");
27
28	result = (__u64 *)fentry_skel->bss;
29	for (i = 0; i < sizeof(*fentry_skel->bss) / sizeof(__u64); i++) {
30		if (!ASSERT_EQ(result[i], 1, "fentry_result"))
31			return -1;
32	}
33
34	fentry_test_lskel__detach(fentry_skel);
35
36	/* zero results for re-attach test */
37	memset(fentry_skel->bss, 0, sizeof(*fentry_skel->bss));
38	return 0;
39}
40
41static void fentry_test(void)
42{
43	struct fentry_test_lskel *fentry_skel = NULL;
44	int err;
45
46	fentry_skel = fentry_test_lskel__open_and_load();
47	if (!ASSERT_OK_PTR(fentry_skel, "fentry_skel_load"))
48		goto cleanup;
49
50	err = fentry_test_common(fentry_skel);
51	if (!ASSERT_OK(err, "fentry_first_attach"))
52		goto cleanup;
53
54	err = fentry_test_common(fentry_skel);
55	ASSERT_OK(err, "fentry_second_attach");
56
57cleanup:
58	fentry_test_lskel__destroy(fentry_skel);
59}
60
61static void fentry_many_args(void)
62{
63	struct fentry_many_args *fentry_skel = NULL;
64	int err;
65
66	fentry_skel = fentry_many_args__open_and_load();
67	if (!ASSERT_OK_PTR(fentry_skel, "fentry_many_args_skel_load"))
68		goto cleanup;
69
70	err = fentry_many_args__attach(fentry_skel);
71	if (!ASSERT_OK(err, "fentry_many_args_attach"))
72		goto cleanup;
73
74	ASSERT_OK(trigger_module_test_read(1), "trigger_read");
75
76	ASSERT_EQ(fentry_skel->bss->test1_result, 1,
77		  "fentry_many_args_result1");
78	ASSERT_EQ(fentry_skel->bss->test2_result, 1,
79		  "fentry_many_args_result2");
80	ASSERT_EQ(fentry_skel->bss->test3_result, 1,
81		  "fentry_many_args_result3");
82
83cleanup:
84	fentry_many_args__destroy(fentry_skel);
85}
86
87void test_fentry_test(void)
88{
89	if (test__start_subtest("fentry"))
90		fentry_test();
91	if (test__start_subtest("fentry_many_args"))
92		fentry_many_args();
93}
94