1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020, Tessares SA. */
3 /* Copyright (c) 2022, SUSE. */
5 #include <test_progs.h>
6 #include "cgroup_helpers.h"
7 #include "network_helpers.h"
8 #include "mptcp_sock.skel.h"
10 struct mptcp_storage {
16 static int verify_tsk(int map_fd, int client_fd)
18 int err, cfd = client_fd;
19 struct mptcp_storage val;
21 err = bpf_map_lookup_elem(map_fd, &cfd, &val);
22 if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
25 if (!ASSERT_EQ(val.invoked, 1, "unexpected invoked count"))
28 if (!ASSERT_EQ(val.is_mptcp, 0, "unexpected is_mptcp"))
34 static int verify_msk(int map_fd, int client_fd, __u32 token)
36 int err, cfd = client_fd;
37 struct mptcp_storage val;
39 if (!ASSERT_GT(token, 0, "invalid token"))
42 err = bpf_map_lookup_elem(map_fd, &cfd, &val);
43 if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
46 if (!ASSERT_EQ(val.invoked, 1, "unexpected invoked count"))
49 if (!ASSERT_EQ(val.is_mptcp, 1, "unexpected is_mptcp"))
52 if (!ASSERT_EQ(val.token, token, "unexpected token"))
58 static int run_test(int cgroup_fd, int server_fd, bool is_mptcp)
60 int client_fd, prog_fd, map_fd, err;
61 struct mptcp_sock *sock_skel;
63 sock_skel = mptcp_sock__open_and_load();
64 if (!ASSERT_OK_PTR(sock_skel, "skel_open_load"))
67 err = mptcp_sock__attach(sock_skel);
68 if (!ASSERT_OK(err, "skel_attach"))
71 prog_fd = bpf_program__fd(sock_skel->progs._sockops);
72 if (!ASSERT_GE(prog_fd, 0, "bpf_program__fd")) {
77 map_fd = bpf_map__fd(sock_skel->maps.socket_storage_map);
78 if (!ASSERT_GE(map_fd, 0, "bpf_map__fd")) {
83 err = bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_SOCK_OPS, 0);
84 if (!ASSERT_OK(err, "bpf_prog_attach"))
87 client_fd = connect_to_fd(server_fd, 0);
88 if (!ASSERT_GE(client_fd, 0, "connect to fd")) {
93 err += is_mptcp ? verify_msk(map_fd, client_fd, sock_skel->bss->token) :
94 verify_tsk(map_fd, client_fd);
99 mptcp_sock__destroy(sock_skel);
103 static void test_base(void)
105 int server_fd, cgroup_fd;
107 cgroup_fd = test__join_cgroup("/mptcp");
108 if (!ASSERT_GE(cgroup_fd, 0, "test__join_cgroup"))
112 server_fd = start_server(AF_INET, SOCK_STREAM, NULL, 0, 0);
113 if (!ASSERT_GE(server_fd, 0, "start_server"))
116 ASSERT_OK(run_test(cgroup_fd, server_fd, false), "run_test tcp");
122 server_fd = start_mptcp_server(AF_INET, NULL, 0, 0);
123 if (!ASSERT_GE(server_fd, 0, "start_mptcp_server"))
124 goto close_cgroup_fd;
126 ASSERT_OK(run_test(cgroup_fd, server_fd, true), "run_test mptcp");
134 void test_mptcp(void)
136 if (test__start_subtest("base"))