Merge branch 'bpf: mptcp: Support for mptcp_sock'
Mat Martineau says: ==================== This patch set adds BPF access to mptcp_sock structures, along with associated self tests. You may recognize some of the code from earlier (https://lore.kernel.org/bpf/20200918121046.190240-6-nicolas.rybowski@tessares.net/) but it has been reworked quite a bit. v1 -> v2: Emit BTF type, add func_id checks in verifier.c and bpf_trace.c, remove build check for CONFIG_BPF_JIT, add selftest check for CONFIG_MPTCP, and add a patch to include CONFIG_IKCONFIG/CONFIG_IKCONFIG_PROC for the BPF self tests. v2 -> v3: Access sysctl through the filesystem to work around CI use of the more limited busybox sysctl command. v3 -> v4: Dropped special case kernel code for tcp_sock is_mptcp, use existing bpf_tcp_helpers.h, and add check for 'ip mptcp monitor' support. v4 -> v5: Use BPF test skeleton, more consistent use of ASSERT macros, drop some unnecessary parameters / checks, and use tracing to acquire MPTCP token. Geliang Tang (6): bpf: add bpf_skc_to_mptcp_sock_proto selftests/bpf: Enable CONFIG_IKCONFIG_PROC in config selftests/bpf: test bpf_skc_to_mptcp_sock selftests/bpf: verify token of struct mptcp_sock selftests/bpf: verify ca_name of struct mptcp_sock selftests/bpf: verify first of struct mptcp_sock ==================== Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
This commit is contained in:
commit
96af42c50a
|
@ -13780,6 +13780,7 @@ F: include/net/mptcp.h
|
|||
F: include/trace/events/mptcp.h
|
||||
F: include/uapi/linux/mptcp.h
|
||||
F: net/mptcp/
|
||||
F: tools/testing/selftests/bpf/*/*mptcp*.c
|
||||
F: tools/testing/selftests/net/mptcp/
|
||||
|
||||
NETWORKING [TCP]
|
||||
|
|
|
@ -2231,6 +2231,7 @@ extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
|
|||
extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
|
||||
extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
|
||||
extern const struct bpf_func_proto bpf_skc_to_unix_sock_proto;
|
||||
extern const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto;
|
||||
extern const struct bpf_func_proto bpf_copy_from_user_proto;
|
||||
extern const struct bpf_func_proto bpf_snprintf_btf_proto;
|
||||
extern const struct bpf_func_proto bpf_snprintf_proto;
|
||||
|
|
|
@ -178,7 +178,8 @@ extern struct btf_id_set name;
|
|||
BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP6, tcp6_sock) \
|
||||
BTF_SOCK_TYPE(BTF_SOCK_TYPE_UDP, udp_sock) \
|
||||
BTF_SOCK_TYPE(BTF_SOCK_TYPE_UDP6, udp6_sock) \
|
||||
BTF_SOCK_TYPE(BTF_SOCK_TYPE_UNIX, unix_sock)
|
||||
BTF_SOCK_TYPE(BTF_SOCK_TYPE_UNIX, unix_sock) \
|
||||
BTF_SOCK_TYPE(BTF_SOCK_TYPE_MPTCP, mptcp_sock)
|
||||
|
||||
enum {
|
||||
#define BTF_SOCK_TYPE(name, str) name,
|
||||
|
|
|
@ -284,4 +284,10 @@ static inline int mptcpv6_init(void) { return 0; }
|
|||
static inline void mptcpv6_handle_mapped(struct sock *sk, bool mapped) { }
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_MPTCP) && defined(CONFIG_BPF_SYSCALL)
|
||||
struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk);
|
||||
#else
|
||||
static inline struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk) { return NULL; }
|
||||
#endif
|
||||
|
||||
#endif /* __NET_MPTCP_H */
|
||||
|
|
|
@ -5172,6 +5172,12 @@ union bpf_attr {
|
|||
* Return
|
||||
* Map value associated to *key* on *cpu*, or **NULL** if no entry
|
||||
* was found or *cpu* is invalid.
|
||||
*
|
||||
* struct mptcp_sock *bpf_skc_to_mptcp_sock(void *sk)
|
||||
* Description
|
||||
* Dynamically cast a *sk* pointer to a *mptcp_sock* pointer.
|
||||
* Return
|
||||
* *sk* if casting is valid, or **NULL** otherwise.
|
||||
*/
|
||||
#define __BPF_FUNC_MAPPER(FN) \
|
||||
FN(unspec), \
|
||||
|
@ -5370,6 +5376,7 @@ union bpf_attr {
|
|||
FN(ima_file_hash), \
|
||||
FN(kptr_xchg), \
|
||||
FN(map_lookup_percpu_elem), \
|
||||
FN(skc_to_mptcp_sock), \
|
||||
/* */
|
||||
|
||||
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
|
||||
|
|
|
@ -509,6 +509,7 @@ static bool is_ptr_cast_function(enum bpf_func_id func_id)
|
|||
func_id == BPF_FUNC_skc_to_tcp_sock ||
|
||||
func_id == BPF_FUNC_skc_to_tcp6_sock ||
|
||||
func_id == BPF_FUNC_skc_to_udp6_sock ||
|
||||
func_id == BPF_FUNC_skc_to_mptcp_sock ||
|
||||
func_id == BPF_FUNC_skc_to_tcp_timewait_sock ||
|
||||
func_id == BPF_FUNC_skc_to_tcp_request_sock;
|
||||
}
|
||||
|
|
|
@ -1705,6 +1705,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
|||
return &bpf_skc_to_udp6_sock_proto;
|
||||
case BPF_FUNC_skc_to_unix_sock:
|
||||
return &bpf_skc_to_unix_sock_proto;
|
||||
case BPF_FUNC_skc_to_mptcp_sock:
|
||||
return &bpf_skc_to_mptcp_sock_proto;
|
||||
case BPF_FUNC_sk_storage_get:
|
||||
return &bpf_sk_storage_get_tracing_proto;
|
||||
case BPF_FUNC_sk_storage_delete:
|
||||
|
|
|
@ -78,6 +78,7 @@
|
|||
#include <linux/btf_ids.h>
|
||||
#include <net/tls.h>
|
||||
#include <net/xdp.h>
|
||||
#include <net/mptcp.h>
|
||||
|
||||
static const struct bpf_func_proto *
|
||||
bpf_sk_base_func_proto(enum bpf_func_id func_id);
|
||||
|
@ -11281,6 +11282,20 @@ const struct bpf_func_proto bpf_skc_to_unix_sock_proto = {
|
|||
.ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UNIX],
|
||||
};
|
||||
|
||||
BPF_CALL_1(bpf_skc_to_mptcp_sock, struct sock *, sk)
|
||||
{
|
||||
BTF_TYPE_EMIT(struct mptcp_sock);
|
||||
return (unsigned long)bpf_mptcp_sock_from_subflow(sk);
|
||||
}
|
||||
|
||||
const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto = {
|
||||
.func = bpf_skc_to_mptcp_sock,
|
||||
.gpl_only = false,
|
||||
.ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
|
||||
.arg1_type = ARG_PTR_TO_SOCK_COMMON,
|
||||
.ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_MPTCP],
|
||||
};
|
||||
|
||||
BPF_CALL_1(bpf_sock_from_file, struct file *, file)
|
||||
{
|
||||
return (unsigned long)sock_from_file(file);
|
||||
|
@ -11323,6 +11338,9 @@ bpf_sk_base_func_proto(enum bpf_func_id func_id)
|
|||
case BPF_FUNC_skc_to_unix_sock:
|
||||
func = &bpf_skc_to_unix_sock_proto;
|
||||
break;
|
||||
case BPF_FUNC_skc_to_mptcp_sock:
|
||||
func = &bpf_skc_to_mptcp_sock_proto;
|
||||
break;
|
||||
case BPF_FUNC_ktime_get_coarse_ns:
|
||||
return &bpf_ktime_get_coarse_ns_proto;
|
||||
default:
|
||||
|
|
|
@ -10,3 +10,5 @@ obj-$(CONFIG_INET_MPTCP_DIAG) += mptcp_diag.o
|
|||
mptcp_crypto_test-objs := crypto_test.o
|
||||
mptcp_token_test-objs := token_test.o
|
||||
obj-$(CONFIG_MPTCP_KUNIT_TEST) += mptcp_crypto_test.o mptcp_token_test.o
|
||||
|
||||
obj-$(CONFIG_BPF_SYSCALL) += bpf.o
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
/* Multipath TCP
|
||||
*
|
||||
* Copyright (c) 2020, Tessares SA.
|
||||
* Copyright (c) 2022, SUSE.
|
||||
*
|
||||
* Author: Nicolas Rybowski <nicolas.rybowski@tessares.net>
|
||||
*/
|
||||
|
||||
#define pr_fmt(fmt) "MPTCP: " fmt
|
||||
|
||||
#include <linux/bpf.h>
|
||||
#include "protocol.h"
|
||||
|
||||
struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk)
|
||||
{
|
||||
if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP && sk_is_mptcp(sk))
|
||||
return mptcp_sk(mptcp_subflow_ctx(sk)->conn);
|
||||
|
||||
return NULL;
|
||||
}
|
|
@ -633,6 +633,7 @@ class PrinterHelpers(Printer):
|
|||
'struct socket',
|
||||
'struct file',
|
||||
'struct bpf_timer',
|
||||
'struct mptcp_sock',
|
||||
]
|
||||
known_types = {
|
||||
'...',
|
||||
|
@ -682,6 +683,7 @@ class PrinterHelpers(Printer):
|
|||
'struct socket',
|
||||
'struct file',
|
||||
'struct bpf_timer',
|
||||
'struct mptcp_sock',
|
||||
}
|
||||
mapped_types = {
|
||||
'u8': '__u8',
|
||||
|
|
|
@ -5172,6 +5172,12 @@ union bpf_attr {
|
|||
* Return
|
||||
* Map value associated to *key* on *cpu*, or **NULL** if no entry
|
||||
* was found or *cpu* is invalid.
|
||||
*
|
||||
* struct mptcp_sock *bpf_skc_to_mptcp_sock(void *sk)
|
||||
* Description
|
||||
* Dynamically cast a *sk* pointer to a *mptcp_sock* pointer.
|
||||
* Return
|
||||
* *sk* if casting is valid, or **NULL** otherwise.
|
||||
*/
|
||||
#define __BPF_FUNC_MAPPER(FN) \
|
||||
FN(unspec), \
|
||||
|
@ -5370,6 +5376,7 @@ union bpf_attr {
|
|||
FN(ima_file_hash), \
|
||||
FN(kptr_xchg), \
|
||||
FN(map_lookup_percpu_elem), \
|
||||
FN(skc_to_mptcp_sock), \
|
||||
/* */
|
||||
|
||||
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
|
||||
|
|
|
@ -16,6 +16,10 @@ BPF_PROG(name, args)
|
|||
#define SOL_TCP 6
|
||||
#endif
|
||||
|
||||
#ifndef TCP_CA_NAME_MAX
|
||||
#define TCP_CA_NAME_MAX 16
|
||||
#endif
|
||||
|
||||
#define tcp_jiffies32 ((__u32)bpf_jiffies64())
|
||||
|
||||
struct sock_common {
|
||||
|
@ -81,6 +85,7 @@ struct tcp_sock {
|
|||
__u32 lsndtime;
|
||||
__u32 prior_cwnd;
|
||||
__u64 tcp_mstamp; /* most recent packet received/sent */
|
||||
bool is_mptcp;
|
||||
} __attribute__((preserve_access_index));
|
||||
|
||||
static __always_inline struct inet_connection_sock *inet_csk(const struct sock *sk)
|
||||
|
@ -225,4 +230,12 @@ static __always_inline bool tcp_cc_eq(const char *a, const char *b)
|
|||
extern __u32 tcp_slow_start(struct tcp_sock *tp, __u32 acked) __ksym;
|
||||
extern void tcp_cong_avoid_ai(struct tcp_sock *tp, __u32 w, __u32 acked) __ksym;
|
||||
|
||||
struct mptcp_sock {
|
||||
struct inet_connection_sock sk;
|
||||
|
||||
__u32 token;
|
||||
struct sock *first;
|
||||
char ca_name[TCP_CA_NAME_MAX];
|
||||
} __attribute__((preserve_access_index));
|
||||
|
||||
#endif
|
||||
|
|
|
@ -54,3 +54,6 @@ CONFIG_NF_DEFRAG_IPV6=y
|
|||
CONFIG_NF_CONNTRACK=y
|
||||
CONFIG_USERFAULTFD=y
|
||||
CONFIG_FPROBE=y
|
||||
CONFIG_IKCONFIG=y
|
||||
CONFIG_IKCONFIG_PROC=y
|
||||
CONFIG_MPTCP=y
|
||||
|
|
|
@ -21,6 +21,10 @@
|
|||
#include "network_helpers.h"
|
||||
#include "test_progs.h"
|
||||
|
||||
#ifndef IPPROTO_MPTCP
|
||||
#define IPPROTO_MPTCP 262
|
||||
#endif
|
||||
|
||||
#define clean_errno() (errno == 0 ? "None" : strerror(errno))
|
||||
#define log_err(MSG, ...) ({ \
|
||||
int __save = errno; \
|
||||
|
@ -73,13 +77,13 @@ int settimeo(int fd, int timeout_ms)
|
|||
|
||||
#define save_errno_close(fd) ({ int __save = errno; close(fd); errno = __save; })
|
||||
|
||||
static int __start_server(int type, const struct sockaddr *addr,
|
||||
static int __start_server(int type, int protocol, const struct sockaddr *addr,
|
||||
socklen_t addrlen, int timeout_ms, bool reuseport)
|
||||
{
|
||||
int on = 1;
|
||||
int fd;
|
||||
|
||||
fd = socket(addr->sa_family, type, 0);
|
||||
fd = socket(addr->sa_family, type, protocol);
|
||||
if (fd < 0) {
|
||||
log_err("Failed to create server socket");
|
||||
return -1;
|
||||
|
@ -113,8 +117,8 @@ error_close:
|
|||
return -1;
|
||||
}
|
||||
|
||||
int start_server(int family, int type, const char *addr_str, __u16 port,
|
||||
int timeout_ms)
|
||||
static int start_server_proto(int family, int type, int protocol,
|
||||
const char *addr_str, __u16 port, int timeout_ms)
|
||||
{
|
||||
struct sockaddr_storage addr;
|
||||
socklen_t addrlen;
|
||||
|
@ -122,10 +126,23 @@ int start_server(int family, int type, const char *addr_str, __u16 port,
|
|||
if (make_sockaddr(family, addr_str, port, &addr, &addrlen))
|
||||
return -1;
|
||||
|
||||
return __start_server(type, (struct sockaddr *)&addr,
|
||||
return __start_server(type, protocol, (struct sockaddr *)&addr,
|
||||
addrlen, timeout_ms, false);
|
||||
}
|
||||
|
||||
int start_server(int family, int type, const char *addr_str, __u16 port,
|
||||
int timeout_ms)
|
||||
{
|
||||
return start_server_proto(family, type, 0, addr_str, port, timeout_ms);
|
||||
}
|
||||
|
||||
int start_mptcp_server(int family, const char *addr_str, __u16 port,
|
||||
int timeout_ms)
|
||||
{
|
||||
return start_server_proto(family, SOCK_STREAM, IPPROTO_MPTCP, addr_str,
|
||||
port, timeout_ms);
|
||||
}
|
||||
|
||||
int *start_reuseport_server(int family, int type, const char *addr_str,
|
||||
__u16 port, int timeout_ms, unsigned int nr_listens)
|
||||
{
|
||||
|
@ -144,7 +161,7 @@ int *start_reuseport_server(int family, int type, const char *addr_str,
|
|||
if (!fds)
|
||||
return NULL;
|
||||
|
||||
fds[0] = __start_server(type, (struct sockaddr *)&addr, addrlen,
|
||||
fds[0] = __start_server(type, 0, (struct sockaddr *)&addr, addrlen,
|
||||
timeout_ms, true);
|
||||
if (fds[0] == -1)
|
||||
goto close_fds;
|
||||
|
@ -154,7 +171,7 @@ int *start_reuseport_server(int family, int type, const char *addr_str,
|
|||
goto close_fds;
|
||||
|
||||
for (; nr_fds < nr_listens; nr_fds++) {
|
||||
fds[nr_fds] = __start_server(type, (struct sockaddr *)&addr,
|
||||
fds[nr_fds] = __start_server(type, 0, (struct sockaddr *)&addr,
|
||||
addrlen, timeout_ms, true);
|
||||
if (fds[nr_fds] == -1)
|
||||
goto close_fds;
|
||||
|
@ -247,7 +264,7 @@ int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
|
|||
struct sockaddr_storage addr;
|
||||
struct sockaddr_in *addr_in;
|
||||
socklen_t addrlen, optlen;
|
||||
int fd, type;
|
||||
int fd, type, protocol;
|
||||
|
||||
if (!opts)
|
||||
opts = &default_opts;
|
||||
|
@ -258,6 +275,11 @@ int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
|
|||
return -1;
|
||||
}
|
||||
|
||||
if (getsockopt(server_fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &optlen)) {
|
||||
log_err("getsockopt(SOL_PROTOCOL)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
addrlen = sizeof(addr);
|
||||
if (getsockname(server_fd, (struct sockaddr *)&addr, &addrlen)) {
|
||||
log_err("Failed to get server addr");
|
||||
|
@ -265,7 +287,7 @@ int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
|
|||
}
|
||||
|
||||
addr_in = (struct sockaddr_in *)&addr;
|
||||
fd = socket(addr_in->sin_family, type, 0);
|
||||
fd = socket(addr_in->sin_family, type, protocol);
|
||||
if (fd < 0) {
|
||||
log_err("Failed to create client socket");
|
||||
return -1;
|
||||
|
|
|
@ -42,6 +42,8 @@ extern struct ipv6_packet pkt_v6;
|
|||
int settimeo(int fd, int timeout_ms);
|
||||
int start_server(int family, int type, const char *addr, __u16 port,
|
||||
int timeout_ms);
|
||||
int start_mptcp_server(int family, const char *addr, __u16 port,
|
||||
int timeout_ms);
|
||||
int *start_reuseport_server(int family, int type, const char *addr_str,
|
||||
__u16 port, int timeout_ms,
|
||||
unsigned int nr_listens);
|
||||
|
|
|
@ -0,0 +1,174 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
/* Copyright (c) 2020, Tessares SA. */
|
||||
/* Copyright (c) 2022, SUSE. */
|
||||
|
||||
#include <test_progs.h>
|
||||
#include "cgroup_helpers.h"
|
||||
#include "network_helpers.h"
|
||||
#include "mptcp_sock.skel.h"
|
||||
|
||||
#ifndef TCP_CA_NAME_MAX
|
||||
#define TCP_CA_NAME_MAX 16
|
||||
#endif
|
||||
|
||||
struct mptcp_storage {
|
||||
__u32 invoked;
|
||||
__u32 is_mptcp;
|
||||
struct sock *sk;
|
||||
__u32 token;
|
||||
struct sock *first;
|
||||
char ca_name[TCP_CA_NAME_MAX];
|
||||
};
|
||||
|
||||
static int verify_tsk(int map_fd, int client_fd)
|
||||
{
|
||||
int err, cfd = client_fd;
|
||||
struct mptcp_storage val;
|
||||
|
||||
err = bpf_map_lookup_elem(map_fd, &cfd, &val);
|
||||
if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
|
||||
return err;
|
||||
|
||||
if (!ASSERT_EQ(val.invoked, 1, "unexpected invoked count"))
|
||||
err++;
|
||||
|
||||
if (!ASSERT_EQ(val.is_mptcp, 0, "unexpected is_mptcp"))
|
||||
err++;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static void get_msk_ca_name(char ca_name[])
|
||||
{
|
||||
size_t len;
|
||||
int fd;
|
||||
|
||||
fd = open("/proc/sys/net/ipv4/tcp_congestion_control", O_RDONLY);
|
||||
if (!ASSERT_GE(fd, 0, "failed to open tcp_congestion_control"))
|
||||
return;
|
||||
|
||||
len = read(fd, ca_name, TCP_CA_NAME_MAX);
|
||||
if (!ASSERT_GT(len, 0, "failed to read ca_name"))
|
||||
goto err;
|
||||
|
||||
if (len > 0 && ca_name[len - 1] == '\n')
|
||||
ca_name[len - 1] = '\0';
|
||||
|
||||
err:
|
||||
close(fd);
|
||||
}
|
||||
|
||||
static int verify_msk(int map_fd, int client_fd, __u32 token)
|
||||
{
|
||||
char ca_name[TCP_CA_NAME_MAX];
|
||||
int err, cfd = client_fd;
|
||||
struct mptcp_storage val;
|
||||
|
||||
if (!ASSERT_GT(token, 0, "invalid token"))
|
||||
return -1;
|
||||
|
||||
get_msk_ca_name(ca_name);
|
||||
|
||||
err = bpf_map_lookup_elem(map_fd, &cfd, &val);
|
||||
if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
|
||||
return err;
|
||||
|
||||
if (!ASSERT_EQ(val.invoked, 1, "unexpected invoked count"))
|
||||
err++;
|
||||
|
||||
if (!ASSERT_EQ(val.is_mptcp, 1, "unexpected is_mptcp"))
|
||||
err++;
|
||||
|
||||
if (!ASSERT_EQ(val.token, token, "unexpected token"))
|
||||
err++;
|
||||
|
||||
if (!ASSERT_EQ(val.first, val.sk, "unexpected first"))
|
||||
err++;
|
||||
|
||||
if (!ASSERT_STRNEQ(val.ca_name, ca_name, TCP_CA_NAME_MAX, "unexpected ca_name"))
|
||||
err++;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static int run_test(int cgroup_fd, int server_fd, bool is_mptcp)
|
||||
{
|
||||
int client_fd, prog_fd, map_fd, err;
|
||||
struct mptcp_sock *sock_skel;
|
||||
|
||||
sock_skel = mptcp_sock__open_and_load();
|
||||
if (!ASSERT_OK_PTR(sock_skel, "skel_open_load"))
|
||||
return -EIO;
|
||||
|
||||
err = mptcp_sock__attach(sock_skel);
|
||||
if (!ASSERT_OK(err, "skel_attach"))
|
||||
goto out;
|
||||
|
||||
prog_fd = bpf_program__fd(sock_skel->progs._sockops);
|
||||
if (!ASSERT_GE(prog_fd, 0, "bpf_program__fd")) {
|
||||
err = -EIO;
|
||||
goto out;
|
||||
}
|
||||
|
||||
map_fd = bpf_map__fd(sock_skel->maps.socket_storage_map);
|
||||
if (!ASSERT_GE(map_fd, 0, "bpf_map__fd")) {
|
||||
err = -EIO;
|
||||
goto out;
|
||||
}
|
||||
|
||||
err = bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_SOCK_OPS, 0);
|
||||
if (!ASSERT_OK(err, "bpf_prog_attach"))
|
||||
goto out;
|
||||
|
||||
client_fd = connect_to_fd(server_fd, 0);
|
||||
if (!ASSERT_GE(client_fd, 0, "connect to fd")) {
|
||||
err = -EIO;
|
||||
goto out;
|
||||
}
|
||||
|
||||
err += is_mptcp ? verify_msk(map_fd, client_fd, sock_skel->bss->token) :
|
||||
verify_tsk(map_fd, client_fd);
|
||||
|
||||
close(client_fd);
|
||||
|
||||
out:
|
||||
mptcp_sock__destroy(sock_skel);
|
||||
return err;
|
||||
}
|
||||
|
||||
static void test_base(void)
|
||||
{
|
||||
int server_fd, cgroup_fd;
|
||||
|
||||
cgroup_fd = test__join_cgroup("/mptcp");
|
||||
if (!ASSERT_GE(cgroup_fd, 0, "test__join_cgroup"))
|
||||
return;
|
||||
|
||||
/* without MPTCP */
|
||||
server_fd = start_server(AF_INET, SOCK_STREAM, NULL, 0, 0);
|
||||
if (!ASSERT_GE(server_fd, 0, "start_server"))
|
||||
goto with_mptcp;
|
||||
|
||||
ASSERT_OK(run_test(cgroup_fd, server_fd, false), "run_test tcp");
|
||||
|
||||
close(server_fd);
|
||||
|
||||
with_mptcp:
|
||||
/* with MPTCP */
|
||||
server_fd = start_mptcp_server(AF_INET, NULL, 0, 0);
|
||||
if (!ASSERT_GE(server_fd, 0, "start_mptcp_server"))
|
||||
goto close_cgroup_fd;
|
||||
|
||||
ASSERT_OK(run_test(cgroup_fd, server_fd, true), "run_test mptcp");
|
||||
|
||||
close(server_fd);
|
||||
|
||||
close_cgroup_fd:
|
||||
close(cgroup_fd);
|
||||
}
|
||||
|
||||
void test_mptcp(void)
|
||||
{
|
||||
if (test__start_subtest("base"))
|
||||
test_base();
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
/* Copyright (c) 2020, Tessares SA. */
|
||||
/* Copyright (c) 2022, SUSE. */
|
||||
|
||||
#include <linux/bpf.h>
|
||||
#include <bpf/bpf_helpers.h>
|
||||
#include "bpf_tcp_helpers.h"
|
||||
|
||||
char _license[] SEC("license") = "GPL";
|
||||
__u32 token = 0;
|
||||
|
||||
struct mptcp_storage {
|
||||
__u32 invoked;
|
||||
__u32 is_mptcp;
|
||||
struct sock *sk;
|
||||
__u32 token;
|
||||
struct sock *first;
|
||||
char ca_name[TCP_CA_NAME_MAX];
|
||||
};
|
||||
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_SK_STORAGE);
|
||||
__uint(map_flags, BPF_F_NO_PREALLOC);
|
||||
__type(key, int);
|
||||
__type(value, struct mptcp_storage);
|
||||
} socket_storage_map SEC(".maps");
|
||||
|
||||
SEC("sockops")
|
||||
int _sockops(struct bpf_sock_ops *ctx)
|
||||
{
|
||||
struct mptcp_storage *storage;
|
||||
struct mptcp_sock *msk;
|
||||
int op = (int)ctx->op;
|
||||
struct tcp_sock *tsk;
|
||||
struct bpf_sock *sk;
|
||||
bool is_mptcp;
|
||||
|
||||
if (op != BPF_SOCK_OPS_TCP_CONNECT_CB)
|
||||
return 1;
|
||||
|
||||
sk = ctx->sk;
|
||||
if (!sk)
|
||||
return 1;
|
||||
|
||||
tsk = bpf_skc_to_tcp_sock(sk);
|
||||
if (!tsk)
|
||||
return 1;
|
||||
|
||||
is_mptcp = bpf_core_field_exists(tsk->is_mptcp) ? tsk->is_mptcp : 0;
|
||||
if (!is_mptcp) {
|
||||
storage = bpf_sk_storage_get(&socket_storage_map, sk, 0,
|
||||
BPF_SK_STORAGE_GET_F_CREATE);
|
||||
if (!storage)
|
||||
return 1;
|
||||
|
||||
storage->token = 0;
|
||||
__builtin_memset(storage->ca_name, 0, TCP_CA_NAME_MAX);
|
||||
storage->first = NULL;
|
||||
} else {
|
||||
msk = bpf_skc_to_mptcp_sock(sk);
|
||||
if (!msk)
|
||||
return 1;
|
||||
|
||||
storage = bpf_sk_storage_get(&socket_storage_map, msk, 0,
|
||||
BPF_SK_STORAGE_GET_F_CREATE);
|
||||
if (!storage)
|
||||
return 1;
|
||||
|
||||
storage->token = msk->token;
|
||||
__builtin_memcpy(storage->ca_name, msk->ca_name, TCP_CA_NAME_MAX);
|
||||
storage->first = msk->first;
|
||||
}
|
||||
storage->invoked++;
|
||||
storage->is_mptcp = is_mptcp;
|
||||
storage->sk = (struct sock *)sk;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
SEC("fentry/mptcp_pm_new_connection")
|
||||
int BPF_PROG(trace_mptcp_pm_new_connection, struct mptcp_sock *msk,
|
||||
const struct sock *ssk, int server_side)
|
||||
{
|
||||
if (!server_side)
|
||||
token = msk->token;
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue