c84d7c593f9f7c303b81574a2c1925b0678d0d8c
[platform/kernel/linux-rpi.git] / tools / testing / selftests / bpf / prog_tests / mptcp.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020, Tessares SA. */
3 /* Copyright (c) 2022, SUSE. */
4
5 #include <test_progs.h>
6 #include "cgroup_helpers.h"
7 #include "network_helpers.h"
8 #include "mptcp_sock.skel.h"
9
10 struct mptcp_storage {
11         __u32 invoked;
12         __u32 is_mptcp;
13         __u32 token;
14 };
15
16 static int verify_tsk(int map_fd, int client_fd)
17 {
18         int err, cfd = client_fd;
19         struct mptcp_storage val;
20
21         err = bpf_map_lookup_elem(map_fd, &cfd, &val);
22         if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
23                 return err;
24
25         if (!ASSERT_EQ(val.invoked, 1, "unexpected invoked count"))
26                 err++;
27
28         if (!ASSERT_EQ(val.is_mptcp, 0, "unexpected is_mptcp"))
29                 err++;
30
31         return err;
32 }
33
34 static int verify_msk(int map_fd, int client_fd, __u32 token)
35 {
36         int err, cfd = client_fd;
37         struct mptcp_storage val;
38
39         if (!ASSERT_GT(token, 0, "invalid token"))
40                 return -1;
41
42         err = bpf_map_lookup_elem(map_fd, &cfd, &val);
43         if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
44                 return err;
45
46         if (!ASSERT_EQ(val.invoked, 1, "unexpected invoked count"))
47                 err++;
48
49         if (!ASSERT_EQ(val.is_mptcp, 1, "unexpected is_mptcp"))
50                 err++;
51
52         if (!ASSERT_EQ(val.token, token, "unexpected token"))
53                 err++;
54
55         return err;
56 }
57
58 static int run_test(int cgroup_fd, int server_fd, bool is_mptcp)
59 {
60         int client_fd, prog_fd, map_fd, err;
61         struct mptcp_sock *sock_skel;
62
63         sock_skel = mptcp_sock__open_and_load();
64         if (!ASSERT_OK_PTR(sock_skel, "skel_open_load"))
65                 return -EIO;
66
67         err = mptcp_sock__attach(sock_skel);
68         if (!ASSERT_OK(err, "skel_attach"))
69                 goto out;
70
71         prog_fd = bpf_program__fd(sock_skel->progs._sockops);
72         if (!ASSERT_GE(prog_fd, 0, "bpf_program__fd")) {
73                 err = -EIO;
74                 goto out;
75         }
76
77         map_fd = bpf_map__fd(sock_skel->maps.socket_storage_map);
78         if (!ASSERT_GE(map_fd, 0, "bpf_map__fd")) {
79                 err = -EIO;
80                 goto out;
81         }
82
83         err = bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_SOCK_OPS, 0);
84         if (!ASSERT_OK(err, "bpf_prog_attach"))
85                 goto out;
86
87         client_fd = connect_to_fd(server_fd, 0);
88         if (!ASSERT_GE(client_fd, 0, "connect to fd")) {
89                 err = -EIO;
90                 goto out;
91         }
92
93         err += is_mptcp ? verify_msk(map_fd, client_fd, sock_skel->bss->token) :
94                           verify_tsk(map_fd, client_fd);
95
96         close(client_fd);
97
98 out:
99         mptcp_sock__destroy(sock_skel);
100         return err;
101 }
102
103 static void test_base(void)
104 {
105         int server_fd, cgroup_fd;
106
107         cgroup_fd = test__join_cgroup("/mptcp");
108         if (!ASSERT_GE(cgroup_fd, 0, "test__join_cgroup"))
109                 return;
110
111         /* without MPTCP */
112         server_fd = start_server(AF_INET, SOCK_STREAM, NULL, 0, 0);
113         if (!ASSERT_GE(server_fd, 0, "start_server"))
114                 goto with_mptcp;
115
116         ASSERT_OK(run_test(cgroup_fd, server_fd, false), "run_test tcp");
117
118         close(server_fd);
119
120 with_mptcp:
121         /* with MPTCP */
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;
125
126         ASSERT_OK(run_test(cgroup_fd, server_fd, true), "run_test mptcp");
127
128         close(server_fd);
129
130 close_cgroup_fd:
131         close(cgroup_fd);
132 }
133
134 void test_mptcp(void)
135 {
136         if (test__start_subtest("base"))
137                 test_base();
138 }