Skip to contentSkip to conversation

selftests/bpf: enable mptcp before testing

PATCH In review Hangbin Liu 3 participants 11 messages Feb 10, 2023 – Feb 17, 2023

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: enable mptcp before testing

Some distros may not enable mptcp by default. Enable it before start the mptcp server. To use the {read/write}_int_sysctl() functions, I moved them to test_progs.c

Fixes 8039d353217c ("selftests/bpf: Add MPTCP test base")
---
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 15 ++++++-
 .../bpf/prog_tests/select_reuseport.c         | 43 -------------------
 tools/testing/selftests/bpf/test_progs.c      | 36 ++++++++++++++++
 tools/testing/selftests/bpf/test_progs.h      |  9 ++++
 4 files changed, 59 insertions(+), 44 deletions(-)
tools/testing/selftests/bpf/prog_tests/mptcp.c +14 -1
index 59f08d6d1d53..c0a0ee7abd06 100644
@@ -11,6 +11,8 @@
 #define TCP_CA_NAME_MAX	16
 #endif
 
+#define MPTCP_ENABLED_SYSCTL "/proc/sys/net/mptcp/enabled"
+
 struct mptcp_storage {
 	__u32 invoked;
 	__u32 is_mptcp;
@@ -138,7 +140,7 @@ static int run_test(int cgroup_fd, int server_fd, bool is_mptcp)
 
 static void test_base(void)
 {
-	int server_fd, cgroup_fd;
+	int server_fd, cgroup_fd, saved_mptcp_enabled = -1;
 
 	cgroup_fd = test__join_cgroup("/mptcp");
 	if (!ASSERT_GE(cgroup_fd, 0, "test__join_cgroup"))
@@ -155,6 +157,14 @@ static void test_base(void)
 
 with_mptcp:
 	/* with MPTCP */
+	saved_mptcp_enabled = read_int_sysctl(MPTCP_ENABLED_SYSCTL);
+	if (saved_mptcp_enabled < 0)
+		goto close_cgroup_fd;
+
+	if (saved_mptcp_enabled == 0 &&
+	    write_int_sysctl(MPTCP_ENABLED_SYSCTL, 1))
+		goto close_cgroup_fd;
+
 	server_fd = start_mptcp_server(AF_INET, NULL, 0, 0);
 	if (!ASSERT_GE(server_fd, 0, "start_mptcp_server"))
 		goto close_cgroup_fd;
@@ -164,6 +174,9 @@ static void test_base(void)
 	close(server_fd);
 
 close_cgroup_fd:
+	if (saved_mptcp_enabled == 0)
+		write_int_sysctl(MPTCP_ENABLED_SYSCTL, saved_mptcp_enabled);
+
 	close(cgroup_fd);
 }
 
tools/testing/selftests/bpf/prog_tests/select_reuseport.c +0 -43

55 lines not on this page · raw message

tools/testing/selftests/bpf/test_progs.c +36 -0
index 4716e38e153a..d50599ccba9a 100644
@@ -967,6 +967,42 @@ int write_sysctl(const char *sysctl, const char *value)
 	return 0;
 }
 
+int read_int_sysctl(const char *sysctl)
+{
+	char buf[16];
+	int fd, ret;
+
+	fd = open(sysctl, 0);
+	RET_ERR(fd == -1, "open(sysctl)",
+		"sysctl:%s fd:%d errno:%d\n", sysctl, fd, errno);
+
+	ret = read(fd, buf, sizeof(buf));
+	RET_ERR(ret <= 0, "read(sysctl)",
+		"sysctl:%s ret:%d errno:%d\n", sysctl, ret, errno);
+
+	close(fd);
+	return atoi(buf);
+}
+
+int write_int_sysctl(const char *sysctl, int v)
+{
+	int fd, ret, size;
+	char buf[16];
+
+	fd = open(sysctl, O_RDWR);
+	RET_ERR(fd == -1, "open(sysctl)",
+		"sysctl:%s fd:%d errno:%d\n", sysctl, fd, errno);
+
+	size = snprintf(buf, sizeof(buf), "%d", v);
+	ret = write(fd, buf, size);
+	RET_ERR(ret != size, "write(sysctl)",
+		"sysctl:%s ret:%d size:%d errno:%d\n",
+		sysctl, ret, size, errno);
+
+	close(fd);
+	return 0;
+}
+
 #define MAX_BACKTRACE_SZ 128
 void crash_handler(int signum)
 {
tools/testing/selftests/bpf/test_progs.h +9 -0
index 3f058dfadbaf..1522ea930bf6 100644
@@ -376,6 +376,13 @@ int test__join_cgroup(const char *path);
 	___ok;								\
 })
 
+#define RET_ERR(condition, tag, format...) ({				\
+	if (CHECK_FAIL(condition)) {					\
+		printf(tag " " format);					\
+		return -1;						\
+	}								\
+})
+
 static inline __u64 ptr_to_u64(const void *ptr)
 {
 	return (__u64) (unsigned long) ptr;
@@ -394,6 +401,8 @@ int kern_sync_rcu(void);
 int trigger_module_test_read(int read_sz);
 int trigger_module_test_write(int write_sz);
 int write_sysctl(const char *sysctl, const char *value);
+int read_int_sysctl(const char *sysctl);
+int write_int_sysctl(const char *sysctl, int v);
 
 #ifdef __x86_64__
 #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
Signature · 1 line
2.38.1
#1 lore raw

Hi Hangbin Liu,

Quoted 8 lines · Hangbin Liu

Some distros may not enable mptcp by default. Enable it before start the mptcp server. To use the {read/write}_int_sysctl() functions, I moved them to test_progs.c

Fixes 8039d353217c ("selftests/bpf: Add MPTCP test base")
---
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 15 ++++++-

Thank you for the patch!

The modifications linked to MPTCP look good to me.

But I don't think it is needed here: I maybe didn't look properly at 'bpf/test_progs.c' file but I think each program from 'prog_tests' directory is executed in a dedicated netns, no?

I don't have an environment ready to validate that but if yes, it means that on a "vanilla" kernel, net.mptcp.enabled sysctl knob should be set to 1. In this case, this modification would be specific to these distros patching MPTCP code to disable it by default. It might then be better to add this patch next to the one disabling MPTCP by default, no? (or revert it to have MPTCP available by default for the applications asking for it :) )

Cheers,
Matt

Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions
www.tessares.net
#2 lore raw
Quoted 20 lines · Matthieu Baerts

Hi Hangbin Liu,

Quoted 8 lines · Hangbin Liu

Some distros may not enable mptcp by default. Enable it before start the mptcp server. To use the {read/write}_int_sysctl() functions, I moved them to test_progs.c

Fixes 8039d353217c ("selftests/bpf: Add MPTCP test base")
---
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 15 ++++++-

Thank you for the patch!

The modifications linked to MPTCP look good to me.

But I don't think it is needed here: I maybe didn't look properly at 'bpf/test_progs.c' file but I think each program from 'prog_tests' directory is executed in a dedicated netns, no?

Quoted 7 lines

I don't have an environment ready to validate that but if yes, it means that on a "vanilla" kernel, net.mptcp.enabled sysctl knob should be set to 1. In this case, this modification would be specific to these distros patching MPTCP code to disable it by default. It might then be better to add this patch next to the one disabling MPTCP by default, no? (or revert it to have MPTCP available by default for the applications asking for it :) )

I think this issue looks like the rp_filter setting. The default rp_filter is 0. But many distros set it to 1 for safety reason. Thus there are some fixes for the rp_filter setting like this one. e.g.

[Liu@Laptop-X1 net]$ git log --oneline tools/testing/selftests/net | grep rp_filter f6071e5e3961 selftests/fib_tests: Rework fib_rp_filter_test() d8e336f77e3b selftests: mptcp: turn rp_filter off on each NIC e86580235708 selftests: set conf.all.rp_filter=0 in bareudp.sh 1ccd58331f6f selftests: disable rp_filter when testing bareudp 71a0e29e9940 selftests: forwarding: Add missing 'rp_filter' configuration bcf7ddb0186d selftests: disable rp_filter for icmp_redirect.sh adb701d6cfa4 selftests: add a test case for rp_filter
42801298386c selftests: forwarding: mirror_gre_nh: Unset rp_filter on host VRF 27a2628b3c24 selftests: forwarding: mirror_gre_vlan_bridge_1q: Unset rp_filter

Thanks
Hangbin

#3 lore raw

Hi Hangbin Liu,

Quoted 29 lines · Hangbin Liu
Quoted 20 lines · Matthieu Baerts

Hi Hangbin Liu,

Quoted 8 lines · Hangbin Liu

Some distros may not enable mptcp by default. Enable it before start the mptcp server. To use the {read/write}_int_sysctl() functions, I moved them to test_progs.c

Fixes 8039d353217c ("selftests/bpf: Add MPTCP test base")
---
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 15 ++++++-

Thank you for the patch!

The modifications linked to MPTCP look good to me.

But I don't think it is needed here: I maybe didn't look properly at 'bpf/test_progs.c' file but I think each program from 'prog_tests' directory is executed in a dedicated netns, no?

Quoted 7 lines

I don't have an environment ready to validate that but if yes, it means that on a "vanilla" kernel, net.mptcp.enabled sysctl knob should be set to 1. In this case, this modification would be specific to these distros patching MPTCP code to disable it by default. It might then be better to add this patch next to the one disabling MPTCP by default, no? (or revert it to have MPTCP available by default for the applications asking for it :) )

Quoted 3 lines

I think this issue looks like the rp_filter setting. The default rp_filter is 0. But many distros set it to 1 for safety reason. Thus there are some fixes for the rp_filter setting like this one. e.g.

I think we should not compare net.mptcp.enabled with rp_filter because the latter is a bit particular: the initial value of rp_filter in a newly created netns is inherited from the "host" (the initial netns). In this case, it is difficult to predict the value of rp_filter in a new netns and it makes sense to force it to the expected value.

Here for net.mptcp.enabled, the value should be 1 in a newly created netns. If not, it means the kernel has been modified. (It was making sense to disable it when MPTCP was backported to older kernels and marked as "Tech Preview". Now, I don't think we should recommend distros to do that and support such modifications in the upstream kernel :) )

But again, I'm not totally against that, I'm just saying that if these tests are executed in dedicated netns, this modification is not needed when using a vanilla kernel ;-)

Except if I misunderstood and these tests are not executed in dedicated netns?

(Note: to reduce the size of the patch, if these tests are correctly executed in dedicated netns, then you can simply force net.mptcp.enabled to be set to 1, no need to reset it to the previous value.)

Cheers,
Matt

Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions
www.tessares.net
#4 lore raw
Quoted 6 lines · Matthieu Baerts

But again, I'm not totally against that, I'm just saying that if these tests are executed in dedicated netns, this modification is not needed when using a vanilla kernel ;-)

Except if I misunderstood and these tests are not executed in dedicated netns?

I tried on my test machine, it looks the test is executed in init netns. I modified my patch by setting the net.mptcp.enabled to 1 without setting it back. You can find the value is changed after testing.

# cat /proc/sys/net/mptcp/enabled
0
# ./test_progs -t mptcp
#127/1   mptcp/base:OK
#127     mptcp:OK
Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED
# cat /proc/sys/net/mptcp/enabled
1

Thanks
Hangbin

#5 lore raw
Quoted 9 lines · Hangbin Liu
Quoted 6 lines · Matthieu Baerts

But again, I'm not totally against that, I'm just saying that if these tests are executed in dedicated netns, this modification is not needed when using a vanilla kernel ;-)

Except if I misunderstood and these tests are not executed in dedicated netns?

I tried on my test machine, it looks the test is executed in init netns.

The new test is needed to run under its own netns whenever possible. The existing mptcp test should be changed to run in its own netns also. Then changing any pernet sysctl (eg. mptcp.enabled) is a less concern to other tests. You can take a look at how other tests doing it (eg. decap_sanity.c).

#6 lore raw
Quoted 9 lines · Martin KaFai Lau
Quoted 8 lines · Hangbin Liu
Quoted 6 lines · Matthieu Baerts

But again, I'm not totally against that, I'm just saying that if these tests are executed in dedicated netns, this modification is not needed when using a vanilla kernel ;-)

Except if I misunderstood and these tests are not executed in dedicated netns?

Quoted 6 lines
Quoted 1 line

I tried on my test machine, it looks the test is executed in init netns.

The new test is needed to run under its own netns whenever possible. The existing mptcp test should be changed to run in its own netns also. Then changing any pernet sysctl (eg. mptcp.enabled) is a less concern to other tests. You can take a look at how other tests doing it (eg. decap_sanity.c).

Thanks for the reference. I will update the patch to make mptcp run in it's own netns.

Hangbin

#7 lore raw

Hi Hangbin, Martin,

Quoted 12 lines · Hangbin Liu
Quoted 11 lines · Martin KaFai Lau
Quoted 9 lines · Hangbin Liu
Quoted 6 lines · Matthieu Baerts

But again, I'm not totally against that, I'm just saying that if these tests are executed in dedicated netns, this modification is not needed when using a vanilla kernel ;-)

Except if I misunderstood and these tests are not executed in dedicated netns?

I tried on my test machine, it looks the test is executed in init netns.

Quoted 7 lines
Quoted 4 lines

The new test is needed to run under its own netns whenever possible. The existing mptcp test should be changed to run in its own netns also. Then changing any pernet sysctl (eg. mptcp.enabled) is a less concern to other tests. You can take a look at how other tests doing it (eg. decap_sanity.c).

Thanks for the reference. I will update the patch to make mptcp run in it's own netns.

Thank you both for your replies!

Yes, that would be good to have this test running in a dedicated NS.

Then mptcp.enabled can be forced using write_sysctl() or SYS("sysctl (...)".

Cheers,
Matt

Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions
www.tessares.net
#8 lore raw
Quoted 8 lines · Matthieu Baerts

Hi Hangbin, Martin,

Thank you both for your replies!

Yes, that would be good to have this test running in a dedicated NS.

Then mptcp.enabled can be forced using write_sysctl() or SYS("sysctl (...)".

Hi Matt, Martin,

I tried to set make mptcp test in netns, like decap_sanity.c does. But I got error when delete the netns. e.g.

# ./test_progs -t mptcp
Cannot remove namespace file "/var/run/netns/mptcp_ns": Device or resource busy
#127/1   mptcp/base:OK
#127     mptcp:OK
Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED

Do you have any idea why I can't remove the netns? Here is the draft patch:

tools/testing/selftests/bpf/prog_tests/mptcp.c +26 -0
index 59f08d6d1d53..5ad10a860994 100644
@@ -7,6 +7,16 @@
 #include "network_helpers.h"
 #include "mptcp_sock.skel.h"
 
+#define SYS(fmt, ...)						\
+	({							\
+		char cmd[1024];					\
+		snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__);	\
+		if (!ASSERT_OK(system(cmd), cmd))		\
+			goto fail;				\
+	})
+
+#define NS_TEST "mptcp_ns"
+
 #ifndef TCP_CA_NAME_MAX
 #define TCP_CA_NAME_MAX	16
 #endif
@@ -169,6 +179,22 @@ static void test_base(void)
 
 void test_mptcp(void)
 {
+	struct nstoken *nstoken = NULL;
+
+	SYS("ip netns add %s", NS_TEST);
+	SYS("ip -net %s link set dev lo up", NS_TEST);
+
+	nstoken = open_netns(NS_TEST);
+	if (!ASSERT_OK_PTR(nstoken, "open_netns"))
+		goto fail;
+
 	if (test__start_subtest("base"))
 		test_base();
+
+fail:
+	if (nstoken)
+		close_netns(nstoken);
+
+	//system("ip netns del " NS_TEST " >& /dev/null");
+	system("ip netns del " NS_TEST);
 }
#9 lore raw
Quoted 10 lines · Hangbin Liu
Quoted 8 lines · Matthieu Baerts

Hi Hangbin, Martin,

Thank you both for your replies!

Yes, that would be good to have this test running in a dedicated NS.

Then mptcp.enabled can be forced using write_sysctl() or SYS("sysctl (...)".

Quoted 7 lines

Hi Matt, Martin,

I tried to set make mptcp test in netns, like decap_sanity.c does. But I got error when delete the netns. e.g.

# ./test_progs -t mptcp
Cannot remove namespace file "/var/run/netns/mptcp_ns": Device or resource busy

Could you try to create and delete netns after test__join_cgroup()?

#10 lore raw
Quoted 10 lines · Martin KaFai Lau
Quoted 7 lines

Hi Matt, Martin,

I tried to set make mptcp test in netns, like decap_sanity.c does. But I got error when delete the netns. e.g.

# ./test_progs -t mptcp
Cannot remove namespace file "/var/run/netns/mptcp_ns": Device or resource busy

Could you try to create and delete netns after test__join_cgroup()?

Thanks a lot. It works now.

Hangbin