Skip to content

Commit ddba122

Browse files
geliangtangMartin KaFai Lau
authored andcommitted
selftests/bpf: Add mptcpify test
Implement a new test program mptcpify: if the family is AF_INET or AF_INET6, the type is SOCK_STREAM, and the protocol ID is 0 or IPPROTO_TCP, set it to IPPROTO_MPTCP. It will be hooked in update_socket_protocol(). Extend the MPTCP test base, add a selftest test_mptcpify() for the mptcpify case. Open and load the mptcpify test prog to mptcpify the TCP sockets dynamically, then use start_server() and connect_to_fd() to create a TCP socket, but actually what's created is an MPTCP socket, which can be verified through 'getsockopt(SOL_PROTOCOL)' and 'getsockopt(MPTCP_INFO)'. Acked-by: Yonghong Song <yonghong.song@linux.dev> Reviewed-by: Matthieu Baerts <matthieu.baerts@tessares.net> Signed-off-by: Geliang Tang <geliang.tang@suse.com> Link: https://lore.kernel.org/r/364e72f307e7bb38382ec7442c182d76298a9c41.1692147782.git.geliang.tang@suse.com Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
1 parent 2077465 commit ddba122

2 files changed

Lines changed: 161 additions & 0 deletions

File tree

tools/testing/selftests/bpf/prog_tests/mptcp.c

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,59 @@
22
/* Copyright (c) 2020, Tessares SA. */
33
/* Copyright (c) 2022, SUSE. */
44

5+
#include <linux/const.h>
6+
#include <netinet/in.h>
57
#include <test_progs.h>
68
#include "cgroup_helpers.h"
79
#include "network_helpers.h"
810
#include "mptcp_sock.skel.h"
11+
#include "mptcpify.skel.h"
912

1013
#define NS_TEST "mptcp_ns"
1114

15+
#ifndef IPPROTO_MPTCP
16+
#define IPPROTO_MPTCP 262
17+
#endif
18+
19+
#ifndef SOL_MPTCP
20+
#define SOL_MPTCP 284
21+
#endif
22+
#ifndef MPTCP_INFO
23+
#define MPTCP_INFO 1
24+
#endif
25+
#ifndef MPTCP_INFO_FLAG_FALLBACK
26+
#define MPTCP_INFO_FLAG_FALLBACK _BITUL(0)
27+
#endif
28+
#ifndef MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED
29+
#define MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED _BITUL(1)
30+
#endif
31+
1232
#ifndef TCP_CA_NAME_MAX
1333
#define TCP_CA_NAME_MAX 16
1434
#endif
1535

36+
struct __mptcp_info {
37+
__u8 mptcpi_subflows;
38+
__u8 mptcpi_add_addr_signal;
39+
__u8 mptcpi_add_addr_accepted;
40+
__u8 mptcpi_subflows_max;
41+
__u8 mptcpi_add_addr_signal_max;
42+
__u8 mptcpi_add_addr_accepted_max;
43+
__u32 mptcpi_flags;
44+
__u32 mptcpi_token;
45+
__u64 mptcpi_write_seq;
46+
__u64 mptcpi_snd_una;
47+
__u64 mptcpi_rcv_nxt;
48+
__u8 mptcpi_local_addr_used;
49+
__u8 mptcpi_local_addr_max;
50+
__u8 mptcpi_csum_enabled;
51+
__u32 mptcpi_retransmits;
52+
__u64 mptcpi_bytes_retrans;
53+
__u64 mptcpi_bytes_sent;
54+
__u64 mptcpi_bytes_received;
55+
__u64 mptcpi_bytes_acked;
56+
};
57+
1658
struct mptcp_storage {
1759
__u32 invoked;
1860
__u32 is_mptcp;
@@ -183,8 +225,107 @@ static void test_base(void)
183225
close(cgroup_fd);
184226
}
185227

228+
static void send_byte(int fd)
229+
{
230+
char b = 0x55;
231+
232+
ASSERT_EQ(write(fd, &b, sizeof(b)), 1, "send single byte");
233+
}
234+
235+
static int verify_mptcpify(int server_fd, int client_fd)
236+
{
237+
struct __mptcp_info info;
238+
socklen_t optlen;
239+
int protocol;
240+
int err = 0;
241+
242+
optlen = sizeof(protocol);
243+
if (!ASSERT_OK(getsockopt(server_fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &optlen),
244+
"getsockopt(SOL_PROTOCOL)"))
245+
return -1;
246+
247+
if (!ASSERT_EQ(protocol, IPPROTO_MPTCP, "protocol isn't MPTCP"))
248+
err++;
249+
250+
optlen = sizeof(info);
251+
if (!ASSERT_OK(getsockopt(client_fd, SOL_MPTCP, MPTCP_INFO, &info, &optlen),
252+
"getsockopt(MPTCP_INFO)"))
253+
return -1;
254+
255+
if (!ASSERT_GE(info.mptcpi_flags, 0, "unexpected mptcpi_flags"))
256+
err++;
257+
if (!ASSERT_FALSE(info.mptcpi_flags & MPTCP_INFO_FLAG_FALLBACK,
258+
"MPTCP fallback"))
259+
err++;
260+
if (!ASSERT_TRUE(info.mptcpi_flags & MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED,
261+
"no remote key received"))
262+
err++;
263+
264+
return err;
265+
}
266+
267+
static int run_mptcpify(int cgroup_fd)
268+
{
269+
int server_fd, client_fd, err = 0;
270+
struct mptcpify *mptcpify_skel;
271+
272+
mptcpify_skel = mptcpify__open_and_load();
273+
if (!ASSERT_OK_PTR(mptcpify_skel, "skel_open_load"))
274+
return libbpf_get_error(mptcpify_skel);
275+
276+
err = mptcpify__attach(mptcpify_skel);
277+
if (!ASSERT_OK(err, "skel_attach"))
278+
goto out;
279+
280+
/* without MPTCP */
281+
server_fd = start_server(AF_INET, SOCK_STREAM, NULL, 0, 0);
282+
if (!ASSERT_GE(server_fd, 0, "start_server")) {
283+
err = -EIO;
284+
goto out;
285+
}
286+
287+
client_fd = connect_to_fd(server_fd, 0);
288+
if (!ASSERT_GE(client_fd, 0, "connect to fd")) {
289+
err = -EIO;
290+
goto close_server;
291+
}
292+
293+
send_byte(client_fd);
294+
295+
err = verify_mptcpify(server_fd, client_fd);
296+
297+
close(client_fd);
298+
close_server:
299+
close(server_fd);
300+
out:
301+
mptcpify__destroy(mptcpify_skel);
302+
return err;
303+
}
304+
305+
static void test_mptcpify(void)
306+
{
307+
struct nstoken *nstoken = NULL;
308+
int cgroup_fd;
309+
310+
cgroup_fd = test__join_cgroup("/mptcpify");
311+
if (!ASSERT_GE(cgroup_fd, 0, "test__join_cgroup"))
312+
return;
313+
314+
nstoken = create_netns();
315+
if (!ASSERT_OK_PTR(nstoken, "create_netns"))
316+
goto fail;
317+
318+
ASSERT_OK(run_mptcpify(cgroup_fd), "run_mptcpify");
319+
320+
fail:
321+
cleanup_netns(nstoken);
322+
close(cgroup_fd);
323+
}
324+
186325
void test_mptcp(void)
187326
{
188327
if (test__start_subtest("base"))
189328
test_base();
329+
if (test__start_subtest("mptcpify"))
330+
test_mptcpify();
190331
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2023, SUSE. */
3+
4+
#include "vmlinux.h"
5+
#include <bpf/bpf_tracing.h>
6+
#include "bpf_tracing_net.h"
7+
8+
char _license[] SEC("license") = "GPL";
9+
10+
SEC("fmod_ret/update_socket_protocol")
11+
int BPF_PROG(mptcpify, int family, int type, int protocol)
12+
{
13+
if ((family == AF_INET || family == AF_INET6) &&
14+
type == SOCK_STREAM &&
15+
(!protocol || protocol == IPPROTO_TCP)) {
16+
return IPPROTO_MPTCP;
17+
}
18+
19+
return protocol;
20+
}

0 commit comments

Comments
 (0)