From fc6ad6aa7c40d6a7605c907c8a86dd41952bacdc Mon Sep 17 00:00:00 2001 From: Geliang Tang Date: Thu, 9 Jan 2025 15:03:25 +0800 Subject: [PATCH] fix Signed-off-by: Geliang Tang --- net/mptcp/bpf.c | 11 ++++++ net/mptcp/pm_userspace.c | 37 ++++++++++++------- net/mptcp/protocol.h | 3 ++ .../testing/selftests/bpf/prog_tests/mptcp.c | 9 ++--- tools/testing/selftests/bpf/progs/mptcp_bpf.h | 5 +++ .../bpf/progs/mptcp_bpf_userspace_pm.c | 30 +++++++++++++++ 6 files changed, 75 insertions(+), 20 deletions(-) diff --git a/net/mptcp/bpf.c b/net/mptcp/bpf.c index ab9beee4bd7b..7887d1955e9f 100644 --- a/net/mptcp/bpf.c +++ b/net/mptcp/bpf.c @@ -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 = { @@ -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) { @@ -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 = { @@ -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); diff --git a/net/mptcp/pm_userspace.c b/net/mptcp/pm_userspace.c index 27edcc0b5562..3aaf0e544a63 100644 --- a/net/mptcp/pm_userspace.c +++ b/net/mptcp/pm_userspace.c @@ -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)) @@ -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; diff --git a/net/mptcp/protocol.h b/net/mptcp/protocol.h index 097141f42bb2..a4e242ad9e65 100644 --- a/net/mptcp/protocol.h +++ b/net/mptcp/protocol.h @@ -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, diff --git a/tools/testing/selftests/bpf/prog_tests/mptcp.c b/tools/testing/selftests/bpf/prog_tests/mptcp.c index f4dda27d5fd6..933681ac1877 100644 --- a/tools/testing/selftests/bpf/prog_tests/mptcp.c +++ b/tools/testing/selftests/bpf/prog_tests/mptcp.c @@ -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(); @@ -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")) @@ -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); } diff --git a/tools/testing/selftests/bpf/progs/mptcp_bpf.h b/tools/testing/selftests/bpf/progs/mptcp_bpf.h index a094de115319..e7add8195407 100644 --- a/tools/testing/selftests/bpf/progs/mptcp_bpf.h +++ b/tools/testing/selftests/bpf/progs/mptcp_bpf.h @@ -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; diff --git a/tools/testing/selftests/bpf/progs/mptcp_bpf_userspace_pm.c b/tools/testing/selftests/bpf/progs/mptcp_bpf_userspace_pm.c index fbc80c60c3a3..f92d02480a5c 100644 --- a/tools/testing/selftests/bpf/progs/mptcp_bpf_userspace_pm.c +++ b/tools/testing/selftests/bpf/progs/mptcp_bpf_userspace_pm.c @@ -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; +}