Skip to content

Commit

Permalink
mptcp: init and release mptcp_pm_ops
Browse files Browse the repository at this point in the history
Add a struct mptcp_pm_ops pointer "ops" in struct mptcp_pm_data, and two
functions mptcp_init_pm() and mptcp_release_pm(), to set and release this
pointer. mptcp_init_pm() is invoked in mptcp_pm_data_reset(), while
mptcp_release_pm() is invoked in __mptcp_destroy_sock().

In this way, different userspace path managers can be initialized through
the pm_type sysctl, and then called into their respective interfaces
through "ops" of "msk->pm".

Signed-off-by: Geliang Tang <[email protected]>
Message-Id: <0518e94994cfb655487ad70f3a01218b91fc4f24.1737012662.git.tanggeliang@kylinos.cn>
  • Loading branch information
Geliang Tang authored and Patchew Applier committed Jan 16, 2025
1 parent d31fd0f commit 5005992
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
4 changes: 4 additions & 0 deletions net/mptcp/pm.c
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,10 @@ void mptcp_pm_data_reset(struct mptcp_sock *msk)
WRITE_ONCE(pm->work_pending, 0);
WRITE_ONCE(pm->accept_addr, 0);
WRITE_ONCE(pm->accept_subflow, 0);

rcu_read_lock();
mptcp_init_pm(msk, mptcp_pm_find(pm_type));
rcu_read_unlock();
}

WRITE_ONCE(pm->addr_signal, 0);
Expand Down
58 changes: 51 additions & 7 deletions net/mptcp/pm_userspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ int mptcp_userspace_pm_get_local_id(struct mptcp_sock *msk,
if (new_entry.addr.port == msk_sport)
new_entry.addr.port = 0;

return userspace_pm_get_local_id(msk, &new_entry);
return msk->pm.ops->get_local_id ?
msk->pm.ops->get_local_id(msk, &new_entry) :
userspace_pm_get_local_id(msk, &new_entry);
}

static u8 userspace_pm_get_flags(struct mptcp_sock *msk,
Expand All @@ -184,7 +186,9 @@ static u8 userspace_pm_get_flags(struct mptcp_sock *msk,
u8 mptcp_userspace_pm_get_flags(struct mptcp_sock *msk,
struct mptcp_addr_info *skc)
{
return userspace_pm_get_flags(msk, skc);
return msk->pm.ops->get_flags ?
msk->pm.ops->get_flags(msk, skc) :
userspace_pm_get_flags(msk, skc);
}

static struct mptcp_sock *mptcp_userspace_pm_get_sock(const struct genl_info *info)
Expand Down Expand Up @@ -269,7 +273,9 @@ int mptcp_pm_nl_announce_doit(struct sk_buff *skb, struct genl_info *info)
}

lock_sock(sk);
err = userspace_pm_address_announce(msk, &addr_val);
err = msk->pm.ops->address_announce ?
msk->pm.ops->address_announce(msk, &addr_val) :
userspace_pm_address_announce(msk, &addr_val);
release_sock(sk);
if (err)
NL_SET_ERR_MSG_ATTR(info->extack, addr,
Expand Down Expand Up @@ -369,7 +375,9 @@ int mptcp_pm_nl_remove_doit(struct sk_buff *skb, struct genl_info *info)
sk = (struct sock *)msk;

lock_sock(sk);
err = userspace_pm_address_remove(msk, id_val);
err = msk->pm.ops->address_remove ?
msk->pm.ops->address_remove(msk, id_val) :
userspace_pm_address_remove(msk, id_val);
release_sock(sk);
if (err)
NL_SET_ERR_MSG_ATTR_FMT(info->extack, id,
Expand Down Expand Up @@ -450,7 +458,9 @@ int mptcp_pm_nl_subflow_create_doit(struct sk_buff *skb, struct genl_info *info)
}

lock_sock(sk);
err = userspace_pm_subflow_create(msk, &entry, &addr_r);
err = msk->pm.ops->subflow_create ?
msk->pm.ops->subflow_create(msk, &entry, &addr_r) :
userspace_pm_subflow_create(msk, &entry, &addr_r);
release_sock(sk);

if (err)
Expand Down Expand Up @@ -586,7 +596,9 @@ int mptcp_pm_nl_subflow_destroy_doit(struct sk_buff *skb, struct genl_info *info
}

lock_sock(sk);
err = userspace_pm_subflow_destroy(msk, &addr_l, &addr_r);
err = msk->pm.ops->subflow_destroy ?
msk->pm.ops->subflow_destroy(msk, &addr_l, &addr_r) :
userspace_pm_subflow_destroy(msk, &addr_l, &addr_r);
release_sock(sk);
if (err)
GENL_SET_ERR_MSG(info, "subflow not found");
Expand Down Expand Up @@ -658,7 +670,9 @@ int mptcp_userspace_pm_set_flags(struct mptcp_pm_addr_entry *local,
}

lock_sock(sk);
ret = userspace_pm_set_flags(msk, local, &rem);
ret = msk->pm.ops->set_flags ?
msk->pm.ops->set_flags(msk, local, &rem) :
userspace_pm_set_flags(msk, local, &rem);
release_sock(sk);

/* mptcp_pm_nl_mp_prio_send_ack() only fails in one case */
Expand Down Expand Up @@ -805,6 +819,36 @@ void mptcp_unregister_path_manager(struct mptcp_pm_ops *pm)
spin_unlock(&mptcp_pm_list_lock);
}

int mptcp_init_pm(struct mptcp_sock *msk, struct mptcp_pm_ops *pm)
{
if (!pm)
pm = &mptcp_userspace_pm;

if (!bpf_try_module_get(pm, pm->owner))
return -EBUSY;

msk->pm.ops = pm;
if (msk->pm.ops->init)
msk->pm.ops->init(msk);

pr_debug("userspace_pm type %u initialized\n", msk->pm.ops->type);
return 0;
}

void mptcp_release_pm(struct mptcp_sock *msk)
{
struct mptcp_pm_ops *pm = msk->pm.ops;

if (!pm)
return;

msk->pm.ops = NULL;
if (pm->release)
pm->release(msk);

bpf_module_put(pm, pm->owner);
}

void __init mptcp_userspace_pm_init(void)
{
mptcp_register_path_manager(&mptcp_userspace_pm);
Expand Down
1 change: 1 addition & 0 deletions net/mptcp/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -2940,6 +2940,7 @@ static void __mptcp_destroy_sock(struct sock *sk)
sk_stop_timer(sk, &sk->sk_timer);
msk->pm.status = 0;
mptcp_release_sched(msk);
mptcp_release_pm(msk);

sk->sk_prot->destroy(sk);

Expand Down
3 changes: 3 additions & 0 deletions net/mptcp/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ struct mptcp_pm_data {
struct mptcp_addr_info remote;
struct list_head anno_list;
struct list_head userspace_pm_local_addr_list;
struct mptcp_pm_ops *ops;

spinlock_t lock; /*protects the whole PM data */

Expand Down Expand Up @@ -1052,6 +1053,8 @@ struct mptcp_pm_ops *mptcp_pm_find(enum mptcp_pm_type type);
int mptcp_validate_path_manager(struct mptcp_pm_ops *pm);
int mptcp_register_path_manager(struct mptcp_pm_ops *pm);
void mptcp_unregister_path_manager(struct mptcp_pm_ops *pm);
int mptcp_init_pm(struct mptcp_sock *msk, struct mptcp_pm_ops *pm);
void mptcp_release_pm(struct mptcp_sock *msk);

void mptcp_free_local_addr_list(struct mptcp_sock *msk);

Expand Down

0 comments on commit 5005992

Please sign in to comment.