Skip to content

Commit

Permalink
mptcp: reuse sending nlmsg code in dump_addr
Browse files Browse the repository at this point in the history
A new type mptcp_pm_addr_id_bitmap_t is defined to easily modify
dump_addr() interface of the path managers to accept an id_bitmap
type parameter. It also allows this parameter of dump_addr() can
be modified by BPF program when implementing this interface of a
BFP path manager.

With the help of get_addr(), we can modify dump_addr() interfaces
to reuse send_nlmsg code between the netlink PM and userspace PM.

The current dump_addr() flow looks like this:

	lock();
	for_each_entry(entry)
		send_nlmsg(entry);
	unlock();

After holding the lock, get every entry by walking the address list,
send each one looply, and finally release the lock.

This set changes the process by copying the address list to an id
bitmap while holding the lock, then release the lock immediately.
After that, without locking, walking the copied id bitmap to get
every copy of entry by using get_addr(), and send each one looply:

	lock();
	for_each_entry(entry)
		set_bit(bitmap);
	unlock();

	for_each_bit(bitmap) {
		copy = get_addr();
		send_nlmsg(copy);
	}

With this, we can reuse the send_nlmsg() code in dump_addr() interfaces
between the netlink PM and userspace PM. They only need to implement
their own dump_addr() interfaces to hold the different locks, copy the
different address lists to an id bitmap, then release the locks.

Signed-off-by: Geliang Tang <[email protected]>
  • Loading branch information
Geliang Tang authored and intel-lab-lkp committed Dec 7, 2024
1 parent 38d7bb6 commit 704cf34
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 71 deletions.
7 changes: 7 additions & 0 deletions include/net/mptcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ struct mptcp_sched_ops {
void (*release)(struct mptcp_sock *msk);
} ____cacheline_aligned_in_smp;

/* max value of mptcp_addr_info.id */
#define MPTCP_PM_MAX_ADDR_ID U8_MAX

typedef struct {
DECLARE_BITMAP(map, MPTCP_PM_MAX_ADDR_ID + 1);
} mptcp_pm_addr_id_bitmap_t;

#ifdef CONFIG_MPTCP
void mptcp_init(void);

Expand Down
42 changes: 38 additions & 4 deletions net/mptcp/pm.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,20 +485,54 @@ int mptcp_pm_nl_get_addr_doit(struct sk_buff *skb, struct genl_info *info)
return ret;
}

static int mptcp_pm_dump_addr(struct sk_buff *msg, struct netlink_callback *cb,
static int mptcp_pm_dump_addr(mptcp_pm_addr_id_bitmap_t *bitmap,
const struct genl_info *info)
{
if (info->attrs[MPTCP_PM_ATTR_TOKEN])
return mptcp_userspace_pm_dump_addr(msg, cb, info);
return mptcp_pm_nl_dump_addr(msg, cb, info);
return mptcp_userspace_pm_dump_addr(bitmap, info);
return mptcp_pm_nl_dump_addr(bitmap, info);
}

int mptcp_pm_nl_get_addr_dumpit(struct sk_buff *msg,
struct netlink_callback *cb)
{
const struct genl_info *info = genl_info_dump(cb);
mptcp_pm_addr_id_bitmap_t *bitmap;
struct mptcp_pm_addr_entry entry;
int id = cb->args[0];
void *hdr;
int i;

return mptcp_pm_dump_addr(msg, cb, info);
bitmap = (mptcp_pm_addr_id_bitmap_t *)cb->ctx;

mptcp_pm_dump_addr(bitmap, info);

for (i = id; i < MPTCP_PM_MAX_ADDR_ID + 1; i++) {
if (test_bit(i, bitmap->map)) {
if (mptcp_pm_get_addr(i, &entry, info))
break;

if (id && entry.addr.id <= id)
continue;

hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid,
cb->nlh->nlmsg_seq, &mptcp_genl_family,
NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR);
if (!hdr)
break;

if (mptcp_nl_fill_addr(msg, &entry) < 0) {
genlmsg_cancel(msg, hdr);
break;
}

id = entry.addr.id;
genlmsg_end(msg, hdr);
}
}

cb->args[0] = id;
return msg->len;
}

static int mptcp_pm_set_flags(struct sk_buff *skb, struct genl_info *info)
Expand Down
35 changes: 3 additions & 32 deletions net/mptcp/pm_netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -1783,48 +1783,19 @@ int mptcp_pm_nl_get_addr(u8 id, struct mptcp_pm_addr_entry *addr,
return ret;
}

int mptcp_pm_nl_dump_addr(struct sk_buff *msg,
struct netlink_callback *cb,
int mptcp_pm_nl_dump_addr(mptcp_pm_addr_id_bitmap_t *bitmap,
const struct genl_info *info)
{
struct net *net = genl_info_net(info);
struct mptcp_pm_addr_entry *entry;
struct pm_nl_pernet *pernet;
int id = cb->args[0];
void *hdr;
int i;

pernet = pm_nl_get_pernet(net);

rcu_read_lock();
for (i = id; i < MPTCP_PM_MAX_ADDR_ID + 1; i++) {
if (test_bit(i, pernet->id_bitmap)) {
entry = __lookup_addr_by_id(pernet, i);
if (!entry)
break;

if (entry->addr.id <= id)
continue;

hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid,
cb->nlh->nlmsg_seq, &mptcp_genl_family,
NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR);
if (!hdr)
break;

if (mptcp_nl_fill_addr(msg, entry) < 0) {
genlmsg_cancel(msg, hdr);
break;
}

id = entry->addr.id;
genlmsg_end(msg, hdr);
}
}
bitmap_copy(bitmap->map, pernet->id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
rcu_read_unlock();

cb->args[0] = id;
return msg->len;
return 0;
}

static int parse_limit(struct genl_info *info, int id, unsigned int *limit)
Expand Down
43 changes: 15 additions & 28 deletions net/mptcp/pm_userspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -614,20 +614,25 @@ int mptcp_userspace_pm_set_flags(struct sk_buff *skb, struct genl_info *info)
return ret;
}

int mptcp_userspace_pm_dump_addr(struct sk_buff *msg,
struct netlink_callback *cb,
const struct genl_info *info)
static int mptcp_userspace_pm_reset_bitmap(struct mptcp_sock *msk,
mptcp_pm_addr_id_bitmap_t *bitmap)
{
struct id_bitmap {
DECLARE_BITMAP(map, MPTCP_PM_MAX_ADDR_ID + 1);
} *bitmap;
struct mptcp_pm_addr_entry *entry;

bitmap_zero(bitmap->map, MPTCP_PM_MAX_ADDR_ID + 1);

mptcp_for_each_userspace_pm_addr(msk, entry)
__set_bit(entry->addr.id, bitmap->map);

return 0;
}

int mptcp_userspace_pm_dump_addr(mptcp_pm_addr_id_bitmap_t *bitmap,
const struct genl_info *info)
{
struct mptcp_sock *msk;
int ret = -EINVAL;
struct sock *sk;
void *hdr;

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

msk = mptcp_userspace_pm_get_sock(info);
if (!msk)
Expand All @@ -637,27 +642,9 @@ int mptcp_userspace_pm_dump_addr(struct sk_buff *msg,

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))
continue;

hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid,
cb->nlh->nlmsg_seq, &mptcp_genl_family,
NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR);
if (!hdr)
break;

if (mptcp_nl_fill_addr(msg, entry) < 0) {
genlmsg_cancel(msg, hdr);
break;
}

__set_bit(entry->addr.id, bitmap->map);
genlmsg_end(msg, hdr);
}
ret = mptcp_userspace_pm_reset_bitmap(msk, bitmap);
spin_unlock_bh(&msk->pm.lock);
release_sock(sk);
ret = msg->len;

sock_put(sk);
return ret;
Expand Down
9 changes: 2 additions & 7 deletions net/mptcp/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,6 @@ enum mptcp_addr_signal_status {
MPTCP_RM_ADDR_SIGNAL,
};

/* max value of mptcp_addr_info.id */
#define MPTCP_PM_MAX_ADDR_ID U8_MAX

struct mptcp_pm_data {
struct mptcp_addr_info local;
struct mptcp_addr_info remote;
Expand Down Expand Up @@ -1127,11 +1124,9 @@ int mptcp_userspace_pm_get_local_id(struct mptcp_sock *msk, struct mptcp_addr_in
bool mptcp_pm_is_backup(struct mptcp_sock *msk, struct sock_common *skc);
bool mptcp_pm_nl_is_backup(struct mptcp_sock *msk, struct mptcp_addr_info *skc);
bool mptcp_userspace_pm_is_backup(struct mptcp_sock *msk, struct mptcp_addr_info *skc);
int mptcp_pm_nl_dump_addr(struct sk_buff *msg,
struct netlink_callback *cb,
int mptcp_pm_nl_dump_addr(mptcp_pm_addr_id_bitmap_t *bitmap,
const struct genl_info *info);
int mptcp_userspace_pm_dump_addr(struct sk_buff *msg,
struct netlink_callback *cb,
int mptcp_userspace_pm_dump_addr(mptcp_pm_addr_id_bitmap_t *bitmap,
const struct genl_info *info);
int mptcp_pm_nl_get_addr(u8 id, struct mptcp_pm_addr_entry *addr,
const struct genl_info *info);
Expand Down

0 comments on commit 704cf34

Please sign in to comment.