[PATCH bpf-next v10 11/12] selftests/bpf: add testcases for fsession cookie

Menglong Dong posted 12 patches 3 weeks, 3 days ago
There is a newer version of this series
[PATCH bpf-next v10 11/12] selftests/bpf: add testcases for fsession cookie
Posted by Menglong Dong 3 weeks, 3 days ago
Test session cookie for fsession. Multiple fsession BPF progs is attached
to bpf_fentry_test1() and session cookie is read and write in the
testcase.

Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
---
v3:
- restructure the testcase by combine the testcases for session cookie and
  get_func_ip into one patch
---
 .../selftests/bpf/progs/fsession_test.c       | 53 +++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/tools/testing/selftests/bpf/progs/fsession_test.c b/tools/testing/selftests/bpf/progs/fsession_test.c
index f504984d42f2..4e55ca67db46 100644
--- a/tools/testing/selftests/bpf/progs/fsession_test.c
+++ b/tools/testing/selftests/bpf/progs/fsession_test.c
@@ -108,3 +108,56 @@ int BPF_PROG(test6, int a)
 		test6_entry_result = (const void *) addr == &bpf_fentry_test1;
 	return 0;
 }
+
+__u64 test7_entry_ok = 0;
+__u64 test7_exit_ok = 0;
+SEC("fsession/bpf_fentry_test1")
+int BPF_PROG(test7, int a)
+{
+	__u64 *cookie = bpf_session_cookie(ctx);
+
+	if (!bpf_session_is_return(ctx)) {
+		*cookie = 0xAAAABBBBCCCCDDDDull;
+		test7_entry_ok = *cookie == 0xAAAABBBBCCCCDDDDull;
+		return 0;
+	}
+
+	test7_exit_ok = *cookie == 0xAAAABBBBCCCCDDDDull;
+	return 0;
+}
+
+__u64 test8_entry_ok = 0;
+__u64 test8_exit_ok = 0;
+
+SEC("fsession/bpf_fentry_test1")
+int BPF_PROG(test8, int a)
+{
+	__u64 *cookie = bpf_session_cookie(ctx);
+
+	if (!bpf_session_is_return(ctx)) {
+		*cookie = 0x1111222233334444ull;
+		test8_entry_ok = *cookie == 0x1111222233334444ull;
+		return 0;
+	}
+
+	test8_exit_ok = *cookie == 0x1111222233334444ull;
+	return 0;
+}
+
+__u64 test9_entry_result = 0;
+__u64 test9_exit_result = 0;
+
+SEC("fsession/bpf_fentry_test1")
+int BPF_PROG(test9, int a, int ret)
+{
+	__u64 *cookie = bpf_session_cookie(ctx);
+
+	if (!bpf_session_is_return(ctx)) {
+		test9_entry_result = a == 1 && ret == 0;
+		*cookie = 0x123456ULL;
+		return 0;
+	}
+
+	test9_exit_result = a == 1 && ret == 2 && *cookie == 0x123456ULL;
+	return 0;
+}
-- 
2.52.0
Re: [PATCH bpf-next v10 11/12] selftests/bpf: add testcases for fsession cookie
Posted by Andrii Nakryiko 2 weeks, 4 days ago
On Thu, Jan 15, 2026 at 3:24 AM Menglong Dong <menglong8.dong@gmail.com> wrote:
>
> Test session cookie for fsession. Multiple fsession BPF progs is attached
> to bpf_fentry_test1() and session cookie is read and write in the
> testcase.
>
> Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
> ---
> v3:
> - restructure the testcase by combine the testcases for session cookie and
>   get_func_ip into one patch
> ---
>  .../selftests/bpf/progs/fsession_test.c       | 53 +++++++++++++++++++
>  1 file changed, 53 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/progs/fsession_test.c b/tools/testing/selftests/bpf/progs/fsession_test.c
> index f504984d42f2..4e55ca67db46 100644
> --- a/tools/testing/selftests/bpf/progs/fsession_test.c
> +++ b/tools/testing/selftests/bpf/progs/fsession_test.c
> @@ -108,3 +108,56 @@ int BPF_PROG(test6, int a)
>                 test6_entry_result = (const void *) addr == &bpf_fentry_test1;
>         return 0;
>  }
> +
> +__u64 test7_entry_ok = 0;
> +__u64 test7_exit_ok = 0;
> +SEC("fsession/bpf_fentry_test1")
> +int BPF_PROG(test7, int a)
> +{
> +       __u64 *cookie = bpf_session_cookie(ctx);
> +
> +       if (!bpf_session_is_return(ctx)) {
> +               *cookie = 0xAAAABBBBCCCCDDDDull;
> +               test7_entry_ok = *cookie == 0xAAAABBBBCCCCDDDDull;

you are trying to double-check that writing into *cookie preserves the
written value, is that right? Do we need volatile read here to ensure
that compiler doesn't optimize this?

> +               return 0;
> +       }
> +
> +       test7_exit_ok = *cookie == 0xAAAABBBBCCCCDDDDull;
> +       return 0;
> +}
> +
> +__u64 test8_entry_ok = 0;
> +__u64 test8_exit_ok = 0;
> +
> +SEC("fsession/bpf_fentry_test1")
> +int BPF_PROG(test8, int a)
> +{
> +       __u64 *cookie = bpf_session_cookie(ctx);
> +
> +       if (!bpf_session_is_return(ctx)) {
> +               *cookie = 0x1111222233334444ull;
> +               test8_entry_ok = *cookie == 0x1111222233334444ull;
> +               return 0;
> +       }
> +
> +       test8_exit_ok = *cookie == 0x1111222233334444ull;
> +       return 0;
> +}
> +
> +__u64 test9_entry_result = 0;
> +__u64 test9_exit_result = 0;
> +
> +SEC("fsession/bpf_fentry_test1")
> +int BPF_PROG(test9, int a, int ret)
> +{
> +       __u64 *cookie = bpf_session_cookie(ctx);
> +
> +       if (!bpf_session_is_return(ctx)) {
> +               test9_entry_result = a == 1 && ret == 0;
> +               *cookie = 0x123456ULL;
> +               return 0;
> +       }
> +
> +       test9_exit_result = a == 1 && ret == 2 && *cookie == 0x123456ULL;
> +       return 0;
> +}
> --
> 2.52.0
>