Skip to content

Commit

Permalink
selftests/bpf: Add getsockopt to inspect mptcp subflow
Browse files Browse the repository at this point in the history
This patch adds a "cgroup/getsockopt" way to inspect the subflows of a
mptcp socket.

mptcp_for_each_stubflow() and other helpers related to list_dentry are
added into progs/mptcp_bpf.h.

Add an extra "cgroup/getsockopt" prog to walk the msk->conn_list and use
bpf_core_cast to cast a pointer to tcp_sock for readonly. It will allow
to inspect all the fields in a tcp_sock.

Suggested-by: Martin KaFai Lau <[email protected]>
Signed-off-by: Geliang Tang <[email protected]>
Message-Id: <7639ece4513d3caa7902013943cdd222d237bee7.1724415435.git.tanggeliang@kylinos.cn>
  • Loading branch information
Geliang Tang authored and Patchew Applier committed Aug 23, 2024
1 parent 85cbd80 commit b5cf5be
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tools/testing/selftests/bpf/progs/mptcp_bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,44 @@

#include <vmlinux.h>
#include <bpf/bpf_core_read.h>
#include "bpf_experimental.h"

#define MPTCP_SUBFLOWS_MAX 8

static inline int list_is_head(const struct list_head *list,
const struct list_head *head)
{
return list == head;
}

#define list_entry(ptr, type, member) \
container_of(ptr, type, member)

#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)

#define list_next_entry(pos, member) \
list_entry((pos)->member.next, typeof(*(pos)), member)

#define list_entry_is_head(pos, head, member) \
list_is_head(&pos->member, (head))

#define list_for_each_entry(pos, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member); \
cond_break, !list_entry_is_head(pos, head, member); \
pos = list_next_entry(pos, member))

#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member), \
n = list_next_entry(pos, member); \
cond_break, !list_entry_is_head(pos, head, member); \
pos = n, n = list_next_entry(n, member))

#define mptcp_for_each_subflow(__msk, __subflow) \
list_for_each_entry(__subflow, &((__msk)->conn_list), node)
#define mptcp_for_each_subflow_safe(__msk, __subflow, __tmp) \
list_for_each_entry_safe(__subflow, __tmp, &((__msk)->conn_list), node)

extern void mptcp_subflow_set_scheduled(struct mptcp_subflow_context *subflow,
bool scheduled) __ksym;

Expand Down
64 changes: 64 additions & 0 deletions tools/testing/selftests/bpf/progs/mptcp_subflow.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

/* vmlinux.h, bpf_helpers.h and other 'define' */
#include "bpf_tracing_net.h"
#include "mptcp_bpf.h"

char _license[] SEC("license") = "GPL";

Expand Down Expand Up @@ -57,3 +58,66 @@ int mptcp_subflow(struct bpf_sock_ops *skops)

return 1;
}

static int _check_getsockopt_subflows_mark(struct mptcp_sock *msk, struct bpf_sockopt *ctx)
{
struct mptcp_subflow_context *subflow;
int *optval = ctx->optval;

mptcp_for_each_subflow(msk, subflow) {
struct sock *ssk;

ssk = mptcp_subflow_tcp_sock(bpf_core_cast(subflow,
struct mptcp_subflow_context));

if (ctx->optval + sizeof(int) <= ctx->optval_end) {
*optval = ssk->sk_mark;
ctx->retval = 0;
}
}

return 1;
}

static int _check_getsockopt_subflow_cc(struct mptcp_sock *msk, struct bpf_sockopt *ctx)
{
struct mptcp_subflow_context *subflow, *tmp;
char *optval = ctx->optval;

mptcp_for_each_subflow_safe(msk, subflow, tmp) {
struct inet_connection_sock *icsk;
struct sock *ssk;

ssk = mptcp_subflow_tcp_sock(bpf_core_cast(subflow,
struct mptcp_subflow_context));
icsk = bpf_core_cast(ssk, struct inet_connection_sock);

if (ctx->optval + TCP_CA_NAME_MAX <= ctx->optval_end) {
__builtin_memcpy(optval, icsk->icsk_ca_ops->name, TCP_CA_NAME_MAX);
ctx->retval = 0;
}
}

return 1;
}

SEC("cgroup/getsockopt")
int _getsockopt_subflow(struct bpf_sockopt *ctx)
{
struct bpf_sock *sk = ctx->sk;
struct mptcp_sock *msk;

if (!sk || sk->protocol != IPPROTO_MPTCP)
return 1;

msk = bpf_core_cast(sk, struct mptcp_sock);
if (msk->pm.subflows != 1)
return 1;

if (ctx->level == SOL_SOCKET && ctx->optname == SO_MARK)
return _check_getsockopt_subflows_mark(msk, ctx);
if (ctx->level == SOL_TCP && ctx->optname == TCP_CONGESTION)
return _check_getsockopt_subflow_cc(msk, ctx);

return 1;
}

0 comments on commit b5cf5be

Please sign in to comment.