Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Geliang Tang <[email protected]>
  • Loading branch information
geliangtang authored and Geliang Tang committed Jan 14, 2025
1 parent 24f58b5 commit fc6ad6a
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 20 deletions.
11 changes: 11 additions & 0 deletions net/mptcp/bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ struct mptcp_sock *bpf_mptcp_sock_from_sock(struct sock *sk)

BTF_SET8_START(bpf_mptcp_fmodret_ids)
BTF_ID_FLAGS(func, update_socket_protocol)
BTF_ID_FLAGS(func, mptcp_userspace_pm_dump_addr_msk)
BTF_SET8_END(bpf_mptcp_fmodret_ids)

static const struct btf_kfunc_id_set bpf_mptcp_fmodret_set = {
Expand Down Expand Up @@ -708,6 +709,12 @@ __bpf_kfunc static void BPF_MPTCP_INC_STATS(struct net *net,
MPTCP_INC_STATS(net, field);
}

__bpf_kfunc static struct mptcp_pm_addr_id_bitmap *
bpf_get_bitmap(struct netlink_callback *cb)
{
return (struct mptcp_pm_addr_id_bitmap *)cb->ctx;
}

__bpf_kfunc static struct sock *
bpf_mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow)
{
Expand Down Expand Up @@ -766,6 +773,8 @@ BTF_ID_FLAGS(func, tcp_stream_memory_free)
BTF_ID_FLAGS(func, bpf_mptcp_subflow_queues_empty)
BTF_ID_FLAGS(func, bpf_mptcp_subflow_tcp_sock, KF_RET_NULL)
BTF_ID_FLAGS(func, mptcp_pm_subflow_chk_stale, KF_SLEEPABLE)
BTF_ID_FLAGS(func, mptcp_userspace_genlmsg_entry)
BTF_ID_FLAGS(func, bpf_get_bitmap)
BTF_KFUNCS_END(bpf_mptcp_common_kfunc_ids)

static const struct btf_kfunc_id_set bpf_mptcp_common_kfunc_set = {
Expand All @@ -782,6 +791,8 @@ static int __init bpf_mptcp_kfunc_init(void)
&bpf_mptcp_common_kfunc_set);
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
&bpf_mptcp_common_kfunc_set);
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
&bpf_mptcp_common_kfunc_set);
#ifdef CONFIG_BPF_JIT
ret = ret ?: register_bpf_struct_ops(&bpf_mptcp_pm_ops, mptcp_pm_ops);
ret = ret ?: register_bpf_struct_ops(&bpf_mptcp_sched_ops, mptcp_sched_ops);
Expand Down
37 changes: 23 additions & 14 deletions net/mptcp/pm_userspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -675,25 +675,15 @@ int mptcp_userspace_pm_set_flags(struct mptcp_pm_addr_entry *local,
return ret;
}

int mptcp_userspace_pm_dump_addr(struct sk_buff *msg,
struct netlink_callback *cb)
int mptcp_userspace_pm_dump_addr_msk(struct mptcp_sock *msk,
struct sk_buff *msg,
struct netlink_callback *cb)
{
const struct genl_info *info = genl_info_dump(cb);
struct mptcp_pm_addr_id_bitmap *bitmap;
struct mptcp_pm_addr_entry *entry;
struct mptcp_sock *msk;
int ret = -EINVAL;
struct sock *sk;

bitmap = (struct mptcp_pm_addr_id_bitmap *)cb->ctx;

msk = mptcp_userspace_pm_get_sock(info);
if (!msk)
return ret;

sk = (struct sock *)msk;

lock_sock(sk);
spin_lock_bh(&msk->pm.lock);
mptcp_for_each_userspace_pm_addr(msk, entry) {
if (test_bit(entry->addr.id, bitmap->map))
Expand All @@ -705,8 +695,27 @@ int mptcp_userspace_pm_dump_addr(struct sk_buff *msg,
__set_bit(entry->addr.id, bitmap->map);
}
spin_unlock_bh(&msk->pm.lock);

return msg->len;
}

int mptcp_userspace_pm_dump_addr(struct sk_buff *msg,
struct netlink_callback *cb)
{
const struct genl_info *info = genl_info_dump(cb);
struct mptcp_sock *msk;
int ret = -EINVAL;
struct sock *sk;

msk = mptcp_userspace_pm_get_sock(info);
if (!msk)
return ret;

sk = (struct sock *)msk;

lock_sock(sk);
ret = mptcp_userspace_pm_dump_addr_msk(msk, msg, cb);
release_sock(sk);
ret = msg->len;

sock_put(sk);
return ret;
Expand Down
3 changes: 3 additions & 0 deletions net/mptcp/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,9 @@ int mptcp_pm_nl_dump_addr(struct sk_buff *msg,
struct netlink_callback *cb);
int mptcp_userspace_pm_dump_addr(struct sk_buff *msg,
struct netlink_callback *cb);
int mptcp_userspace_pm_dump_addr_msk(struct mptcp_sock *msk,
struct sk_buff *msg,
struct netlink_callback *cb);
int mptcp_userspace_genlmsg_entry(struct sk_buff *msg, struct netlink_callback *cb,
struct mptcp_pm_addr_entry *entry);
int mptcp_pm_nl_get_addr(u8 id, struct mptcp_pm_addr_entry *addr,
Expand Down
9 changes: 3 additions & 6 deletions tools/testing/selftests/bpf/prog_tests/mptcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,6 @@ static void test_bpf_path_manager(void)
{
struct mptcp_bpf_userspace_pm *skel;
struct netns_obj *netns;
struct bpf_link *link;
int err;

skel = mptcp_bpf_userspace_pm__open();
Expand All @@ -1098,13 +1097,13 @@ static void test_bpf_path_manager(void)
if (!ASSERT_OK(mptcp_bpf_userspace_pm__load(skel), "load: userspace_pm"))
goto skel_destroy;

link = bpf_map__attach_struct_ops(skel->maps.userspace_pm);
if (!ASSERT_OK_PTR(link, "attach_struct_ops"))
err = mptcp_bpf_userspace_pm__attach(skel);
if (!ASSERT_OK(err, "attach skel"))
goto skel_destroy;

netns = netns_new(NS_TEST, true);
if (!ASSERT_OK_PTR(netns, "netns_new"))
goto link_destroy;
goto skel_destroy;

err = userspace_pm_init(MPTCP_PM_TYPE_BPF_USERSPACE);
if (!ASSERT_OK(err, "userspace_pm_init: bpf pm"))
Expand All @@ -1115,8 +1114,6 @@ static void test_bpf_path_manager(void)
userspace_pm_cleanup();
close_netns:
netns_free(netns);
link_destroy:
bpf_link__destroy(link);
skel_destroy:
mptcp_bpf_userspace_pm__destroy(skel);
}
Expand Down
5 changes: 5 additions & 0 deletions tools/testing/selftests/bpf/progs/mptcp_bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ extern int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk,
struct mptcp_addr_info *rem,
u8 bkup) __ksym;

extern int mptcp_userspace_genlmsg_entry(struct sk_buff *msg, struct netlink_callback *cb,
struct mptcp_pm_addr_entry *entry) __ksym;
extern struct mptcp_pm_addr_id_bitmap *
bpf_get_bitmap(struct netlink_callback *cb) __ksym;

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

Expand Down
30 changes: 30 additions & 0 deletions tools/testing/selftests/bpf/progs/mptcp_bpf_userspace_pm.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,33 @@ struct mptcp_pm_ops userspace_pm = {
.release = (void *)mptcp_pm_release,
.type = MPTCP_PM_TYPE_BPF_USERSPACE,
};

SEC("fmod_ret/mptcp_userspace_pm_dump_addr_msk")
int BPF_PROG(mptcp_pm_dump_addr, struct mptcp_sock *msk,
struct sk_buff *msg, struct netlink_callback *cb)
{
struct mptcp_pm_addr_id_bitmap *bitmap;
struct mptcp_pm_addr_entry *entry;

bitmap = bpf_get_bitmap(cb);

msk = bpf_mptcp_sock_acquire(msk);
if (!msk)
return 0;

bpf_spin_lock_bh(&msk->pm.lock);
bpf_for_each(mptcp_userspace_pm_addr, entry, msk) {
if (bpf_test_bit(entry->addr.id, bitmap))
continue;

bpf_printk("8 mptcp_pm_dump_addr id=%d", entry->addr.id);
if (mptcp_userspace_genlmsg_entry(msg, cb, entry))
break;

bpf_set_bit(entry->addr.id, bitmap);
}
bpf_spin_unlock_bh(&msk->pm.lock);
bpf_mptcp_sock_release(msk);

return msg->len;
}

0 comments on commit fc6ad6a

Please sign in to comment.