diff --git a/tools/testing/selftests/bpf/prog_tests/mptcp.c b/tools/testing/selftests/bpf/prog_tests/mptcp.c index c0a7cb22d8e5..a33710b0cdf0 100644 --- a/tools/testing/selftests/bpf/prog_tests/mptcp.c +++ b/tools/testing/selftests/bpf/prog_tests/mptcp.c @@ -329,6 +329,69 @@ static void test_mptcpify(void) close(cgroup_fd); } +static int getsetsockopt(void) +{ + int server_fd, client_fd, err = 0; + + server_fd = start_mptcp_server(AF_INET, NULL, 0, 0); + if (!ASSERT_GE(server_fd, 0, "start_mptcp_server")) { + err = -EIO; + goto out; + } + + client_fd = connect_to_fd(server_fd, 0); + if (!ASSERT_GE(client_fd, 0, "connect to fd")) { + err = -EIO; + goto close_server; + } + + send_byte(client_fd); + + close(client_fd); +close_server: + close(server_fd); +out: + return err; +} + +static void run_test_sockopt(int cgroup_fd) +{ + struct mptcp_sock *skel; + + skel = mptcp_sock__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_load")) + goto cleanup; + + skel->bss->page_size = getpagesize(); + + skel->links._setsockopt = + bpf_program__attach_cgroup(skel->progs._setsockopt, cgroup_fd); + if (!ASSERT_OK_PTR(skel->links._setsockopt, "setsockopt_link")) + goto cleanup; + + skel->links._getsockopt = + bpf_program__attach_cgroup(skel->progs._getsockopt, cgroup_fd); + if (!ASSERT_OK_PTR(skel->links._getsockopt, "getsockopt_link")) + goto cleanup; + + ASSERT_OK(getsetsockopt(), "getsetsockopt"); + +cleanup: + mptcp_sock__destroy(skel); +} + +void test_sockopt_mptcp(void) +{ + int cgroup_fd; + + cgroup_fd = test__join_cgroup("/sockopt_mptcp"); + if (!ASSERT_GE(cgroup_fd, 0, "join_cgroup /sockopt_mptcp")) + return; + + run_test_sockopt(cgroup_fd); + close(cgroup_fd); +} + static const unsigned int total_bytes = 10 * 1024 * 1024; static int stop, duration; @@ -693,6 +756,8 @@ void test_mptcp(void) test_base(); if (test__start_subtest("mptcpify")) test_mptcpify(); + if (test__start_subtest("sockopt")) + test_sockopt_mptcp(); if (test__start_subtest("default")) test_default(); if (test__start_subtest("first")) diff --git a/tools/testing/selftests/bpf/progs/mptcp_sock.c b/tools/testing/selftests/bpf/progs/mptcp_sock.c index 91a0d7eff2ac..bb9b075f9edd 100644 --- a/tools/testing/selftests/bpf/progs/mptcp_sock.c +++ b/tools/testing/selftests/bpf/progs/mptcp_sock.c @@ -9,6 +9,8 @@ char _license[] SEC("license") = "GPL"; __u32 token = 0; +int page_size = 0; /* userspace should set it */ + struct mptcp_storage { __u32 invoked; __u32 is_mptcp; @@ -86,3 +88,15 @@ int BPF_PROG(trace_mptcp_pm_new_connection, struct mptcp_sock *msk, return 0; } + +SEC("cgroup/getsockopt") +int _getsockopt(struct bpf_sockopt *ctx) +{ + return 1; +} + +SEC("cgroup/setsockopt") +int _setsockopt(struct bpf_sockopt *ctx) +{ + return 1; +}