Skip to contentSkip to conversation

add mptcp_address bpf_iter

PATCH Superseded Geliang Tang 2 participants 8 messages Oct 22, 2024

Subsystems (5)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Geliang Tang

author reviewer
#0 lore raw

v2 cover add mptcp_address bpf_iter

From: Geliang Tang <[email protected]>

v2:
- squash patches 5,6 of v1 into one.
- patch 3, use lockdep_assert_held instead of spin_is_locked.
- update patch 5 to make it run normally in btf-debug mode.

Another bpf_iter for BPF path manager to traverse all address entries on userspace_pm_local_addr_list of an MPTCP socket is added in this set. Userspace pm tool is needed by the self test, so add pm_nl_ctl link in patch 1.

Geliang Tang (5):
selftests/bpf: Add mptcp pm_nl_ctl link selftests/bpf: Use pm_nl_ctl if ip mptcp not supported
bpf: Add mptcp_address bpf_iter
bpf: Export more helpers used by mptcp_address test selftests/bpf: Add mptcp_address bpf_iter subtest

 MAINTAINERS                                   |   1 +
 net/mptcp/bpf.c                               |  66 +++++
 tools/testing/selftests/bpf/Makefile          |   3 +-
 .../testing/selftests/bpf/bpf_experimental.h  |   8 +
 tools/testing/selftests/bpf/mptcp_pm_nl_ctl.c |   1 +
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 235 +++++++++++++++++-
 tools/testing/selftests/bpf/progs/mptcp_bpf.h |   5 +
 .../selftests/bpf/progs/mptcp_bpf_iters.c     |  39 +++
 8 files changed, 349 insertions(+), 9 deletions(-)
 create mode 120000 tools/testing/selftests/bpf/mptcp_pm_nl_ctl.c
Signature · 1 line
2.45.2
author reviewer
#1 lore raw

PATCH v2 1/5 selftests/bpf: Add mptcp pm_nl_ctl link

From: Geliang Tang <[email protected]>

This patch adds a symlink to MPTCP's pm_nl_ctl tool into bpf selftests, and updates Makefile to compile it.

This is useful to run MPTCP BPF selftests on systems with an old version of IPRoute2. This tool can be used as an alternative to 'ip mptcp'.

In addition, this tool is used as userspace path manager. "csf" and "dsf" commands of pm_nl_ctl for creating and destroying subflows, and "ann" and "rem" commands for signaling ADD_ADDR and RM_ADDR. These commands are not currently supported in 'ip mptcp' yet.

MAINTAINERS needs to be updated since a new file is added in a non covered place.

---
 MAINTAINERS                                   | 1 +
 tools/testing/selftests/bpf/Makefile          | 3 ++-
 tools/testing/selftests/bpf/mptcp_pm_nl_ctl.c | 1 +
 3 files changed, 4 insertions(+), 1 deletion(-)
 create mode 120000 tools/testing/selftests/bpf/mptcp_pm_nl_ctl.c
MAINTAINERS +1 -0
index b21dbd7a7a26..f53d8820e233 100644
@@ -16298,6 +16298,7 @@ F:	include/trace/events/mptcp.h
 F:	include/uapi/linux/mptcp*.h
 F:	net/mptcp/
 F:	tools/testing/selftests/bpf/*/*mptcp*.[ch]
+F:	tools/testing/selftests/bpf/*mptcp*.[ch]
 F:	tools/testing/selftests/net/mptcp/
 
 NETWORKING [TCP]
tools/testing/selftests/bpf/Makefile +2 -1
index f04af11df8eb..662825f55f99 100644
@@ -157,7 +157,7 @@ TEST_GEN_PROGS_EXTENDED = \
 	flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
 	test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \
 	xskxceiver xdp_redirect_multi xdp_synproxy veristat xdp_hw_metadata \
-	xdp_features bpf_test_no_cfi.ko
+	xdp_features bpf_test_no_cfi.ko mptcp_pm_nl_ctl
 
 TEST_GEN_FILES += liburandom_read.so urandom_read sign-file uprobe_multi
 
@@ -726,6 +726,7 @@ TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko	\
 		       $(OUTPUT)/xdp_synproxy				\
 		       $(OUTPUT)/sign-file				\
 		       $(OUTPUT)/uprobe_multi				\
+		       $(OUTPUT)/mptcp_pm_nl_ctl			\
 		       ima_setup.sh 					\
 		       verify_sig_setup.sh				\
 		       $(wildcard progs/btf_dump_test_case_*.c)		\
tools/testing/selftests/bpf/mptcp_pm_nl_ctl.c new file +1 -0
new file mode 120000
index 000000000000..5a08c255b278
@@ -0,0 +1 @@
+../net/mptcp/pm_nl_ctl.c
\ No newline at end of file
Signature · 1 line
2.45.2
author reviewer
#2 lore raw

PATCH v2 2/5 selftests/bpf: Use pm_nl_ctl if ip mptcp not supported

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

If running MPTCP BPF selftests on systems with an old version of IPRoute2, 'ip mptcp' command is not supported. In this case, instead of skipping the test, falling back to using 'pm_nl_ctl' tool is a better option. This patch adds an 'ip_mptcp' argument for endpoint_add() to control whether to use 'ip mptcp' or 'pm_nl_ctl' to add an endpoint.

---
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 21 ++++++++++++-------
 1 file changed, 13 insertions(+), 8 deletions(-)
tools/testing/selftests/bpf/prog_tests/mptcp.c +13 -8
index be746e0fc393..97652f7683e8 100644
@@ -28,6 +28,7 @@
 #define ADDR6_3	"dead:beef:3::1"
 #define ADDR6_4	"dead:beef:4::1"
 #define PORT_1	10001
+#define PM_CTL		"./mptcp_pm_nl_ctl"
 #define WITH_DATA	true
 #define WITHOUT_DATA	false
 
@@ -384,13 +385,18 @@ static int address_init(void)
 	return -1;
 }
 
-static int endpoint_add(char *addr, char *flags)
+static int endpoint_add(char *addr, char *flags, bool ip_mptcp)
 {
-	return SYS_NOFAIL("ip -net %s mptcp endpoint add %s %s", NS_TEST, addr, flags);
+	if (ip_mptcp)
+		return SYS_NOFAIL("ip -net %s mptcp endpoint add %s %s",
+				  NS_TEST, addr, flags);
+	return SYS_NOFAIL("ip netns exec %s %s add %s flags %s",
+			  NS_TEST, PM_CTL, addr, flags);
 }
 
 static int endpoint_init(char *flags, u8 endpoints)
 {
+	bool ip_mptcp = true;
 	int ret = -1;
 
 	if (!endpoints || endpoints > 4)
@@ -401,17 +407,16 @@ static int endpoint_init(char *flags, u8 endpoints)
 
 	if (SYS_NOFAIL("ip -net %s mptcp limits set add_addr_accepted 4 subflows 4",
 		       NS_TEST)) {
-		printf("'ip mptcp' not supported, skip this test.\n");
-		test__skip();
-		goto fail;
+		SYS(fail, "ip netns exec %s %s limits 4 4", NS_TEST, PM_CTL);
+		ip_mptcp = false;
 	}
 
 	if (endpoints > 1)
-		ret = endpoint_add(ADDR_2, flags);
+		ret = endpoint_add(ADDR_2, flags, ip_mptcp);
 	if (endpoints > 2)
-		ret = ret ?: endpoint_add(ADDR_3, flags);
+		ret = ret ?: endpoint_add(ADDR_3, flags, ip_mptcp);
 	if (endpoints > 3)
-		ret = ret ?: endpoint_add(ADDR_4, flags);
+		ret = ret ?: endpoint_add(ADDR_4, flags, ip_mptcp);
 
 fail:
 	return ret;
Signature · 1 line
2.45.2
author reviewer
#3 lore raw

PATCH v2 3/5 bpf: Add mptcp_address bpf_iter

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Just like the mptcp_subflow bpf_iter used to implement the MPTCP BPF packet scheduler, another bpf_iter is also needed, named mptcp_address, to traverse all address entries on userspace_pm_local_addr_list of an MPTCP socket for implementing the MPTCP BPF path manager.

In kernel space, we walk this list like this:

list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list)

	 kfunc(entry);

With the mptcp_address bpf_iter, bpf_for_each() can be used to do the same thing in BPF program:

bpf_for_each(mptcp_address, entry, msk)

	 kfunc(entry);

This bpf_iter should be invoked under holding the msk pm lock, so use lockdep_assert_held() to assert the lock is holding.

---
 net/mptcp/bpf.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
net/mptcp/bpf.c +45 -0
index e9db856972cb..8889e5351897 100644
@@ -214,6 +214,15 @@ struct bpf_iter_mptcp_subflow_kern {
 	struct list_head *pos;
 } __aligned(8);
 
+struct bpf_iter_mptcp_address {
+	__u64 __opaque[2];
+} __aligned(8);
+
+struct bpf_iter_mptcp_address_kern {
+	struct mptcp_sock *msk;
+	struct list_head *pos;
+} __aligned(8);
+
 __bpf_kfunc_start_defs();
 
 __bpf_kfunc static struct mptcp_sock *bpf_mptcp_sk(struct sock *sk)
@@ -266,6 +275,39 @@ bpf_iter_mptcp_subflow_destroy(struct bpf_iter_mptcp_subflow *it)
 {
 }
 
+__bpf_kfunc static int
+bpf_iter_mptcp_address_new(struct bpf_iter_mptcp_address *it,
+			   struct mptcp_sock *msk)
+{
+	struct bpf_iter_mptcp_address_kern *kit = (void *)it;
+
+	kit->msk = msk;
+	if (!msk)
+		return -EINVAL;
+
+	lockdep_assert_held(&msk->pm.lock);
+
+	kit->pos = &msk->pm.userspace_pm_local_addr_list;
+	return 0;
+}
+
+__bpf_kfunc static struct mptcp_pm_addr_entry *
+bpf_iter_mptcp_address_next(struct bpf_iter_mptcp_address *it)
+{
+	struct bpf_iter_mptcp_address_kern *kit = (void *)it;
+
+	if (!kit->msk || list_is_last(kit->pos, &kit->msk->pm.userspace_pm_local_addr_list))
+		return NULL;
+
+	kit->pos = kit->pos->next;
+	return list_entry(kit->pos, struct mptcp_pm_addr_entry, list);
+}
+
+__bpf_kfunc static void
+bpf_iter_mptcp_address_destroy(struct bpf_iter_mptcp_address *it)
+{
+}
+
 __bpf_kfunc static struct
 mptcp_sock *bpf_mptcp_sock_acquire(struct mptcp_sock *msk)
 {
@@ -305,6 +347,9 @@ BTF_ID_FLAGS(func, bpf_mptcp_subflow_tcp_sock)
 BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_new, KF_ITER_NEW | KF_TRUSTED_ARGS)
 BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_iter_mptcp_address_new, KF_ITER_NEW | KF_TRUSTED_ARGS)
+BTF_ID_FLAGS(func, bpf_iter_mptcp_address_next, KF_ITER_NEXT | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_iter_mptcp_address_destroy, KF_ITER_DESTROY)
 BTF_ID_FLAGS(func, bpf_mptcp_sock_acquire, KF_ACQUIRE | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_mptcp_sock_release, KF_RELEASE)
 BTF_KFUNCS_END(bpf_mptcp_common_kfunc_ids)
Signature · 1 line
2.45.2
author reviewer
#4 lore raw

PATCH v2 4/5 bpf: Export more helpers used by mptcp_address test

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

The mptcp_address bpf_iter should be invoked under holding the msk pm lock, so spin_lock_bh() and spin_unlock_bh() are needed in BPF program to hold or release the msk pm lock.

And bpf_ipv6_addr_v4mapped() helper is also needed by the mptcp_address bpf_iter selftest program.

This patch adds the corresponding BPF wrappers for these helpers and adds them to the mptcp common kfunc_set.

---
 net/mptcp/bpf.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
net/mptcp/bpf.c +21 -0
index 8889e5351897..057017d3f5c0 100644
@@ -325,6 +325,24 @@ __bpf_kfunc static void bpf_mptcp_sock_release(struct mptcp_sock *msk)
 	WARN_ON_ONCE(!sk || !refcount_dec_not_one(&sk->sk_refcnt));
 }
 
+__bpf_kfunc static void bpf_spin_lock_bh(spinlock_t *lock)
+{
+	spin_lock_bh(lock);
+}
+
+__bpf_kfunc static void bpf_spin_unlock_bh(spinlock_t *lock)
+{
+	spin_unlock_bh(lock);
+}
+
+__bpf_kfunc static bool bpf_ipv6_addr_v4mapped(const struct mptcp_addr_info *a)
+{
+#if IS_ENABLED(CONFIG_MPTCP_IPV6)
+	return ipv6_addr_v4mapped(&a->addr6);
+#endif
+	return false;
+}
+
 __bpf_kfunc struct mptcp_subflow_context *
 bpf_mptcp_subflow_ctx_by_pos(const struct mptcp_sched_data *data, unsigned int pos)
 {
@@ -352,6 +370,9 @@ BTF_ID_FLAGS(func, bpf_iter_mptcp_address_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_mptcp_address_destroy, KF_ITER_DESTROY)
 BTF_ID_FLAGS(func, bpf_mptcp_sock_acquire, KF_ACQUIRE | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_mptcp_sock_release, KF_RELEASE)
+BTF_ID_FLAGS(func, bpf_spin_lock_bh)
+BTF_ID_FLAGS(func, bpf_spin_unlock_bh)
+BTF_ID_FLAGS(func, bpf_ipv6_addr_v4mapped)
 BTF_KFUNCS_END(bpf_mptcp_common_kfunc_ids)
 
 static const struct btf_kfunc_id_set bpf_mptcp_common_kfunc_set = {
Signature · 1 line
2.45.2
author reviewer
#5 lore raw

PATCH v2 5/5 selftests/bpf: Add mptcp_address bpf_iter subtest

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

This patch adds a test program for the newly added mptcp_address bpf_iter in SEC "cgroup/getsockopt". This test iterates over all address entries on the local address list of userspace PM and check whether each one is an IPv4mapped address.

Export mptcp_address helpers bpf_iter_mptcp_address_new/_next/_destroy into bpf_experimental.h. Use bpf_mptcp_sock_acquire() to acquire the msk, then lock the msk pm lock and use bpf_for_each(mptcp_address) to walk the local address list of this msk. Invoke bpf_ipv6_addr_v4mapped() in the loop to check whether the address is an IPv4mapped one. Then Add the address ID of each entry to local veriable local_ids.

Out of the loop, unlock the msk pm lock and use bpf_mptcp_sock_release() to release the msk. Finally, assign local_ids to global variable ids so that the application can obtain this value.

Add a subtest named test_iters_address to load and verify the newly added mptcp_address type bpf_iter example in test_mptcp.

Since mptcp_address bpf_iter iterates over all address entries on the local address list of userspace PM. A set of userspace PM helpers are added.

Set pm_type and start "pm_nl_ctl events" command to save pm events into the log file /tmp/bpf_userspace_pm_events in userspace_pm_init(). Kill "pm_nl_ctl" command and remove the log file in userspace_pm_cleanup(). Parse the log file in userspace_pm_add_subflow() to get the token, sport and dport values, then use "pm_nl_ctl csf" command to create a subflow.

Use the helper userspace_pm_add_subflow() to add 3 new subflow endpoints. Send a byte of message to start the mptcp connection, and receive the message.

getsockopt() is invoked to trigger the "cgroup/getsockopt" test program. Check if skel->bss->ids equals 60 to verify whether the program loops three times as expected.

---
 .../testing/selftests/bpf/bpf_experimental.h  |   8 +
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 214 ++++++++++++++++++
 tools/testing/selftests/bpf/progs/mptcp_bpf.h |   5 +
 .../selftests/bpf/progs/mptcp_bpf_iters.c     |  39 ++++
 4 files changed, 266 insertions(+)
tools/testing/selftests/bpf/bpf_experimental.h +8 -0
index 08eaa431aafd..305c19fb377b 100644
@@ -583,6 +583,14 @@ bpf_iter_mptcp_subflow_next(struct bpf_iter_mptcp_subflow *it) __weak __ksym;
 extern void
 bpf_iter_mptcp_subflow_destroy(struct bpf_iter_mptcp_subflow *it) __weak __ksym;
 
+struct bpf_iter_mptcp_address;
+extern int bpf_iter_mptcp_address_new(struct bpf_iter_mptcp_address *it,
+				      struct mptcp_sock *msk) __weak __ksym;
+extern struct mptcp_pm_addr_entry *
+bpf_iter_mptcp_address_next(struct bpf_iter_mptcp_address *it) __weak __ksym;
+extern void
+bpf_iter_mptcp_address_destroy(struct bpf_iter_mptcp_address *it) __weak __ksym;
+
 extern int bpf_wq_init(struct bpf_wq *wq, void *p__map, unsigned int flags) __weak __ksym;
 extern int bpf_wq_start(struct bpf_wq *wq, unsigned int flags) __weak __ksym;
 extern int bpf_wq_set_callback_impl(struct bpf_wq *wq,
tools/testing/selftests/bpf/prog_tests/mptcp.c +214 -0

244 lines not on this page · raw message

tools/testing/selftests/bpf/progs/mptcp_bpf.h +5 -0
index 3b20cfd44505..376979a9c4f0 100644
@@ -52,6 +52,11 @@ bpf_mptcp_subflow_ctx(const struct sock *sk) __ksym;
 extern struct sock *
 bpf_mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow) __ksym;
 
+extern void bpf_spin_lock_bh(spinlock_t *lock) __ksym;
+extern void bpf_spin_unlock_bh(spinlock_t *lock) __ksym;
+
+extern bool bpf_ipv6_addr_v4mapped(const struct mptcp_addr_info *a) __ksym;
+
 extern void mptcp_subflow_set_scheduled(struct mptcp_subflow_context *subflow,
 					bool scheduled) __ksym;
 
tools/testing/selftests/bpf/progs/mptcp_bpf_iters.c +39 -0
index 1bede22a7e3d..513be1bbe749 100644
@@ -62,3 +62,42 @@ int iters_subflow(struct bpf_sockopt *ctx)
 	bpf_mptcp_sock_release(msk);
 	return 1;
 }
+
+SEC("cgroup/getsockopt")
+int iters_address(struct bpf_sockopt *ctx)
+{
+	struct mptcp_pm_addr_entry *entry;
+	struct bpf_sock *sk = ctx->sk;
+	struct mptcp_sock *msk;
+	int local_ids = 0;
+
+	if (!sk || sk->protocol != IPPROTO_MPTCP ||
+	    ctx->level != SOL_TCP || ctx->optname != TCP_IS_MPTCP)
+		return 1;
+
+	msk = bpf_mptcp_sk((struct sock *)sk);
+	if (msk->pm.server_side)
+		return 1;
+
+	msk = bpf_mptcp_sock_acquire(msk);
+	if (!msk)
+		return 1;
+	bpf_spin_lock_bh(&msk->pm.lock);
+	bpf_for_each(mptcp_address, entry, msk) {
+		/* Here MPTCP-specific path manager kfunc can be called:
+		 * this test is not doing anything really useful, only to
+		 * verify the iteration works.
+		 */
+
+		if (!bpf_ipv6_addr_v4mapped(&entry->addr))
+			break;
+
+		local_ids += entry->addr.id;
+	}
+	bpf_spin_unlock_bh(&msk->pm.lock);
+	bpf_mptcp_sock_release(msk);
+
+	ids = local_ids;
+
+	return 1;
+}
Signature · 1 line
2.45.2
#6 lore raw
In reply to Geliang Tang

Hi Geliang,

Thank you for your modifications, that's great!

But sadly, our CI spotted some issues with it when trying to build it.

You can find more details there:

https://github.com/multipath-tcp/mptcp_net-next/actions/runs/11455981022

Status: failure
Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/d3d814a163c5
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=901713

Feel free to reply to this email if you cannot access logs, if you need some support to fix the error, if this doesn't seem to be caused by your modifications or if the error is a false positive one.

Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)

#7 lore raw
In reply to Geliang Tang

Hi Geliang,

Thank you for your modifications, that's great!

Our CI did some validations and here is its report:

- KVM Validation: normal: Success! ✅
- KVM Validation: debug: Success! ✅
- KVM Validation: btf-normal (only bpftest_all): Success! ✅
- KVM Validation: btf-debug (only bpftest_all): Success! ✅
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/11455981023

Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/d3d814a163c5
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=901713

If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:

    $ cd [kernel source code]
    $ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
        --pull always mptcp/mptcp-upstream-virtme-docker:latest \
        auto-normal

For more details:

https://github.com/multipath-tcp/mptcp-upstream-virtme-docker

Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)

Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)