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
Geliang Tang committed Oct 25, 2024
1 parent 53e1c27 commit b654193
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 38 deletions.
7 changes: 7 additions & 0 deletions net/mptcp/bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <linux/btf_ids.h>
#include <net/bpf_sk_storage.h>
#include "protocol.h"
#include "mib.h"

#ifdef CONFIG_BPF_JIT
static struct bpf_struct_ops bpf_mptcp_pm_ops,
Expand Down Expand Up @@ -686,6 +687,11 @@ __bpf_kfunc static void bpf_ipv6_addr_set_v4mapped(const __be32 addr,
#endif
}

__bpf_kfunc static void mptcp_inc_stats_rmsubflow(struct sock *sk)
{
MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMSUBFLOW);
}

__bpf_kfunc static bool bpf_mptcp_subflow_queues_empty(struct sock *sk)
{
return tcp_rtx_queue_empty(sk);
Expand Down Expand Up @@ -737,6 +743,7 @@ BTF_ID_FLAGS(func, mptcp_pm_remove_addr_entry, KF_SLEEPABLE)
BTF_ID_FLAGS(func, __mptcp_subflow_connect, KF_SLEEPABLE)
BTF_ID_FLAGS(func, mptcp_subflow_shutdown, KF_SLEEPABLE)
BTF_ID_FLAGS(func, mptcp_close_ssk, KF_SLEEPABLE)
BTF_ID_FLAGS(func, mptcp_inc_stats_rmsubflow)
BTF_ID_FLAGS(func, mptcp_pm_nl_mp_prio_send_ack, KF_SLEEPABLE)
BTF_ID_FLAGS(func, mptcp_subflow_active)
BTF_ID_FLAGS(func, mptcp_set_timeout)
Expand Down
2 changes: 2 additions & 0 deletions tools/testing/selftests/bpf/progs/mptcp_bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extern bool CONFIG_MPTCP_IPV6 __kconfig __weak;
#define RCV_SHUTDOWN 1
#define SEND_SHUTDOWN 2

#define ESRCH 3 /* No such process */
#define ENOMEM 12 /* Out of Memory */
#define EINVAL 22 /* Invalid argument */

Expand Down Expand Up @@ -132,6 +133,7 @@ extern void bpf_ipv6_addr_set_v4mapped(const __be32 addr,
extern void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) __ksym;
extern void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
struct mptcp_subflow_context *subflow) __ksym;
extern void mptcp_inc_stats_rmsubflow(struct sock *sk) __ksym;

extern int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk,
struct mptcp_addr_info *addr,
Expand Down
4 changes: 2 additions & 2 deletions tools/testing/selftests/bpf/progs/mptcp_bpf_bytes.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ int BPF_PROG(trace_mptcp_sched_get_send, struct mptcp_sock *msk)
tp = bpf_core_cast(ssk, struct tcp_sock);

if (subflow->subflow_id == 1) {
bpf_printk("bytes 1: sent %lu received %lu subflows %u", tp->bytes_sent, tp->bytes_received, msk->pm.subflows);
//bpf_printk("bytes 1: sent %lu received %lu subflows %u", tp->bytes_sent, tp->bytes_received, msk->pm.subflows);
bytes_sent_1 += tp->bytes_sent;
} else if (subflow->subflow_id == 2) {
bpf_printk("bytes 2: sent %lu received %lu subflows %u", tp->bytes_sent, tp->bytes_received, msk->pm.subflows);
//bpf_printk("bytes 2: sent %lu received %lu subflows %u", tp->bytes_sent, tp->bytes_received, msk->pm.subflows);
bytes_sent_2 += tp->bytes_sent;
}
}
Expand Down
1 change: 1 addition & 0 deletions tools/testing/selftests/bpf/progs/mptcp_bpf_rr.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ int BPF_PROG(bpf_rr_get_subflow, struct mptcp_sock *msk,
out:
next = bpf_core_cast(next, struct mptcp_subflow_context);
mptcp_subflow_set_scheduled(next, true);
//bpf_printk("rr subflow=%u/%u", next->subflow_id, msk->pm.subflows + 1);
ptr->last_snd = mptcp_subflow_tcp_sock(next);
return 0;
}
Expand Down
115 changes: 79 additions & 36 deletions tools/testing/selftests/bpf/progs/mptcp_bpf_userspace_pm.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,40 @@ SEC("struct_ops")
int BPF_PROG(mptcp_pm_address_announce, struct mptcp_sock *msk,
struct mptcp_pm_addr_entry *local)
{
int err;
int err = -EINVAL;

if (local->addr.id == 0 || !(local->flags & MPTCP_PM_ADDR_FLAG_SIGNAL))
return -EINVAL;
if (local->addr.id == 0 || !(local->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
bpf_printk("invalid addr id or flags");
goto announce_err;
}

err = mptcp_userspace_pm_append_new_local_addr(msk, local, false);
if (err < 0)
return err;
if (err < 0) {
bpf_printk("did not match address and id");
goto announce_err;
}

bpf_spin_lock_bh(&msk->pm.lock);

if (mptcp_pm_alloc_anno_list(msk, &local->addr)) {
msk->pm.add_addr_signaled++;
mptcp_pm_announce_addr(msk, &local->addr, false);
mptcp_pm_nl_addr_send_ack(msk);
}

bpf_spin_unlock_bh(&msk->pm.lock);

return 0;
err = 0;
announce_err:
return err;
}

static int mptcp_pm_remove_id_zero_address(struct mptcp_sock *msk)
{
struct mptcp_rm_list list = { .nr = 0 };
struct mptcp_subflow_context *subflow;
bool has_id_0 = false;
int err = -EINVAL;

mptcp_for_each_subflow(msk, subflow) {
subflow = bpf_core_cast(subflow, struct mptcp_subflow_context);
Expand All @@ -108,16 +117,21 @@ static int mptcp_pm_remove_id_zero_address(struct mptcp_sock *msk)
break;
}
}
if (!has_id_0)
return -EINVAL;
if (!has_id_0) {
bpf_printk("address with id 0 not found");
goto remove_err;
}

list.ids[list.nr++] = 0;

bpf_spin_lock_bh(&msk->pm.lock);
mptcp_pm_remove_addr(msk, &list);
bpf_spin_unlock_bh(&msk->pm.lock);

return 0;
err = 0;

remove_err:
return err;
}

static struct mptcp_pm_addr_entry *
Expand All @@ -137,15 +151,20 @@ int BPF_PROG(mptcp_pm_address_remove, struct mptcp_sock *msk, u8 id)
{
struct sock *sk = (struct sock *)msk;
struct mptcp_pm_addr_entry *entry;
int err = -EINVAL;

if (id == 0)
return mptcp_pm_remove_id_zero_address(msk);
if (id == 0) {
err = mptcp_pm_remove_id_zero_address(msk);
goto out;
}

bpf_spin_lock_bh(&msk->pm.lock);
entry = mptcp_userspace_pm_lookup_addr_by_id(msk, id);
bpf_spin_unlock_bh(&msk->pm.lock);
if (!entry)
return -EINVAL;
if (!entry) {
bpf_printk("address with specified id not found");
goto out;
}

mptcp_pm_remove_addr_entry(msk, entry);

Expand All @@ -154,7 +173,9 @@ int BPF_PROG(mptcp_pm_address_remove, struct mptcp_sock *msk, u8 id)
bpf_pm_free_entry(sk, entry);
bpf_spin_unlock_bh(&msk->pm.lock);

return 0;
err = 0;
out:
return err;
}

static struct mptcp_pm_addr_entry *
Expand Down Expand Up @@ -192,16 +213,22 @@ int BPF_PROG(mptcp_pm_subflow_create, struct mptcp_sock *msk,
struct sock *sk = (struct sock *)msk;
int err = -EINVAL;

if (local->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)
return err;
if (local->flags & MPTCP_PM_ADDR_FLAG_SIGNAL) {
bpf_printk("invalid addr flags");
goto create_err;
}
local->flags |= MPTCP_PM_ADDR_FLAG_SUBFLOW;

if (!bpf_mptcp_pm_addr_families_match(sk, &local->addr, remote))
return err;
if (!bpf_mptcp_pm_addr_families_match(sk, &local->addr, remote)) {
bpf_printk("families mismatch");
goto create_err;
}

err = mptcp_userspace_pm_append_new_local_addr(msk, local, false);
if (err < 0)
return err;
if (err < 0) {
bpf_printk("did not match address and id");
goto create_err;
}

err = __mptcp_subflow_connect(sk, local, remote);
bpf_spin_lock_bh(&msk->pm.lock);
Expand All @@ -213,6 +240,7 @@ int BPF_PROG(mptcp_pm_subflow_create, struct mptcp_sock *msk,

bpf_printk("mptcp_pm_subflow_create done");

create_err:
return err;
}

Expand Down Expand Up @@ -267,8 +295,8 @@ int BPF_PROG(mptcp_pm_subflow_destroy, struct mptcp_sock *msk,
struct mptcp_pm_addr_entry *local, struct mptcp_addr_info *remote)
{
struct sock *sk = (struct sock *)msk;
int err = -EINVAL;
struct sock *ssk;
int err = 0;

if (local->addr.family == AF_INET && bpf_ipv6_addr_v4mapped(remote)) {
bpf_ipv6_addr_set_v4mapped(local->addr.addr.s_addr, remote);
Expand All @@ -279,25 +307,34 @@ int BPF_PROG(mptcp_pm_subflow_destroy, struct mptcp_sock *msk,
remote->family = AF_INET6;
}

if (local->addr.family != remote->family)
return err;
if (local->addr.family != remote->family) {
bpf_printk("address families do not match");
err = -EINVAL;
goto destroy_err;
}

if (!local->addr.port || !remote->port)
return err;
if (!local->addr.port || !remote->port) {
bpf_printk("missing local or remote port");
err = -EINVAL;
goto destroy_err;
}

ssk = mptcp_pm_find_ssk(msk, &local->addr, remote);
if (ssk) {
struct mptcp_subflow_context *subflow = bpf_mptcp_subflow_ctx(ssk);

bpf_spin_lock_bh(&msk->pm.lock);
err = mptcp_userspace_pm_delete_local_addr(msk, local);
bpf_spin_unlock_bh(&msk->pm.lock);
mptcp_subflow_shutdown(sk, ssk, RCV_SHUTDOWN | SEND_SHUTDOWN);
mptcp_close_ssk(sk, ssk, subflow);
if (!ssk) {
err = -ESRCH;
goto destroy_err;
}

bpf_spin_lock_bh(&msk->pm.lock);
err = mptcp_userspace_pm_delete_local_addr(msk, local);
bpf_spin_unlock_bh(&msk->pm.lock);
mptcp_subflow_shutdown(sk, ssk, RCV_SHUTDOWN | SEND_SHUTDOWN);
mptcp_close_ssk(sk, ssk, bpf_mptcp_subflow_ctx(ssk));
mptcp_inc_stats_rmsubflow(sk);

bpf_printk("mptcp_pm_subflow_destroy done");

destroy_err:
return err;
}

Expand Down Expand Up @@ -382,13 +419,16 @@ int BPF_PROG(mptcp_pm_set_flags, struct mptcp_sock *msk,
struct mptcp_pm_addr_entry *local, struct mptcp_addr_info *remote)
{
struct mptcp_pm_addr_entry *entry;
int ret = -EINVAL;
u8 bkup = 0;

bpf_printk("mptcp_pm_set_flags");

if (local->addr.family == AF_UNSPEC ||
remote->family == AF_UNSPEC)
return -EINVAL;
remote->family == AF_UNSPEC) {
bpf_printk("invalid address families");
goto set_flags_err;
}

if (local->flags & MPTCP_PM_ADDR_FLAG_BACKUP)
bkup = 1;
Expand All @@ -403,7 +443,10 @@ int BPF_PROG(mptcp_pm_set_flags, struct mptcp_sock *msk,
}
bpf_spin_unlock_bh(&msk->pm.lock);

return mptcp_pm_nl_mp_prio_send_ack(msk, &local->addr, remote, bkup);
ret = mptcp_pm_nl_mp_prio_send_ack(msk, &local->addr, remote, bkup);

set_flags_err:
return ret;
}

SEC(".struct_ops.link")
Expand Down

0 comments on commit b654193

Please sign in to comment.