Commit f7afef56 authored by Menglong Dong's avatar Menglong Dong Committed by Alexei Starovoitov
Browse files

selftests/bpf: add testcases for fsession



Add testcases for BPF_TRACE_FSESSION. The function arguments and return
value are tested both in the entry and exit. And the kfunc
bpf_session_is_ret() is also tested.

Signed-off-by: default avatarMenglong Dong <dongml2@chinatelecom.cn>
Link: https://lore.kernel.org/r/20260124062008.8657-11-dongml2@chinatelecom.cn


Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 85fc4be6
Loading
Loading
Loading
Loading
+90 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2025 ChinaTelecom */
#include <test_progs.h>
#include "fsession_test.skel.h"

static int check_result(struct fsession_test *skel)
{
	LIBBPF_OPTS(bpf_test_run_opts, topts);
	int err, prog_fd;

	/* Trigger test function calls */
	prog_fd = bpf_program__fd(skel->progs.test1);
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	if (!ASSERT_OK(err, "test_run_opts err"))
		return err;
	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
		return topts.retval;

	for (int i = 0; i < sizeof(*skel->bss) / sizeof(__u64); i++) {
		if (!ASSERT_EQ(((__u64 *)skel->bss)[i], 1, "test_result"))
			return -EINVAL;
	}

	return 0;
}

static void test_fsession_basic(void)
{
	struct fsession_test *skel = NULL;
	int err;

	skel = fsession_test__open_and_load();
	if (!ASSERT_OK_PTR(skel, "fsession_test__open_and_load"))
		goto cleanup;

	err = fsession_test__attach(skel);
	if (!ASSERT_OK(err, "fsession_attach"))
		goto cleanup;

	check_result(skel);
cleanup:
	fsession_test__destroy(skel);
}

static void test_fsession_reattach(void)
{
	struct fsession_test *skel = NULL;
	int err;

	skel = fsession_test__open_and_load();
	if (!ASSERT_OK_PTR(skel, "fsession_test__open_and_load"))
		goto cleanup;

	/* first attach */
	err = fsession_test__attach(skel);
	if (!ASSERT_OK(err, "fsession_first_attach"))
		goto cleanup;

	if (check_result(skel))
		goto cleanup;

	/* detach */
	fsession_test__detach(skel);

	/* reset counters */
	memset(skel->bss, 0, sizeof(*skel->bss));

	/* second attach */
	err = fsession_test__attach(skel);
	if (!ASSERT_OK(err, "fsession_second_attach"))
		goto cleanup;

	if (check_result(skel))
		goto cleanup;

cleanup:
	fsession_test__destroy(skel);
}

void test_fsession_test(void)
{
#if !defined(__x86_64__)
	test__skip();
	return;
#endif
	if (test__start_subtest("fsession_test"))
		test_fsession_basic();
	if (test__start_subtest("fsession_reattach"))
		test_fsession_reattach();
}
+97 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2025 ChinaTelecom */
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

char _license[] SEC("license") = "GPL";

__u64 test1_entry_result = 0;
__u64 test1_exit_result = 0;

SEC("fsession/bpf_fentry_test1")
int BPF_PROG(test1, int a, int ret)
{
	bool is_exit = bpf_session_is_return(ctx);

	if (!is_exit) {
		test1_entry_result = a == 1 && ret == 0;
		return 0;
	}

	test1_exit_result = a == 1 && ret == 2;
	return 0;
}

__u64 test2_entry_result = 0;
__u64 test2_exit_result = 0;

SEC("fsession/bpf_fentry_test3")
int BPF_PROG(test2, char a, int b, __u64 c, int ret)
{
	bool is_exit = bpf_session_is_return(ctx);

	if (!is_exit) {
		test2_entry_result = a == 4 && b == 5 && c == 6 && ret == 0;
		return 0;
	}

	test2_exit_result = a == 4 && b == 5 && c == 6 && ret == 15;
	return 0;
}

__u64 test3_entry_result = 0;
__u64 test3_exit_result = 0;

SEC("fsession/bpf_fentry_test4")
int BPF_PROG(test3, void *a, char b, int c, __u64 d, int ret)
{
	bool is_exit = bpf_session_is_return(ctx);

	if (!is_exit) {
		test3_entry_result = a == (void *)7 && b == 8 && c == 9 && d == 10 && ret == 0;
		return 0;
	}

	test3_exit_result = a == (void *)7 && b == 8 && c == 9 && d == 10 && ret == 34;
	return 0;
}

__u64 test4_entry_result = 0;
__u64 test4_exit_result = 0;

SEC("fsession/bpf_fentry_test5")
int BPF_PROG(test4, __u64 a, void *b, short c, int d, __u64 e, int ret)
{
	bool is_exit = bpf_session_is_return(ctx);

	if (!is_exit) {
		test4_entry_result = a == 11 && b == (void *)12 && c == 13 && d == 14 &&
			e == 15 && ret == 0;
		return 0;
	}

	test4_exit_result = a == 11 && b == (void *)12 && c == 13 && d == 14 &&
		e == 15 && ret == 65;
	return 0;
}

__u64 test5_entry_result = 0;
__u64 test5_exit_result = 0;

SEC("fsession/bpf_fentry_test7")
int BPF_PROG(test5, struct bpf_fentry_test_t *arg, int ret)
{
	bool is_exit = bpf_session_is_return(ctx);

	if (!is_exit) {
		if (!arg)
			test5_entry_result = ret == 0;
		return 0;
	}

	if (!arg)
		test5_exit_result = 1;
	return 0;
}