[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230210093205.1378597-1-liuhangbin@gmail.com>
Date: Fri, 10 Feb 2023 17:32:05 +0800
From: Hangbin Liu <liuhangbin@...il.com>
To: netdev@...r.kernel.org
Cc: Alexei Starovoitov <ast@...nel.org>,
Daniel Borkmann <daniel@...earbox.net>,
Andrii Nakryiko <andrii@...nel.org>, bpf@...r.kernel.org,
Jakub Kicinski <kuba@...nel.org>,
"David S . Miller" <davem@...emloft.net>,
Martin KaFai Lau <martin.lau@...ux.dev>,
Song Liu <song@...nel.org>, Yonghong Song <yhs@...com>,
John Fastabend <john.fastabend@...il.com>,
KP Singh <kpsingh@...nel.org>,
Stanislav Fomichev <sdf@...gle.com>,
Hao Luo <haoluo@...gle.com>, Jiri Olsa <jolsa@...nel.org>,
Mykola Lysenko <mykolal@...com>,
Felix Maurer <fmaurer@...hat.com>,
Nicolas Rybowski <nicolas.rybowski@...sares.net>,
Hangbin Liu <liuhangbin@...il.com>
Subject: [PATCH bpf] 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")
Signed-off-by: Hangbin Liu <liuhangbin@...il.com>
---
.../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(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/mptcp.c b/tools/testing/selftests/bpf/prog_tests/mptcp.c
index 59f08d6d1d53..c0a0ee7abd06 100644
--- a/tools/testing/selftests/bpf/prog_tests/mptcp.c
+++ b/tools/testing/selftests/bpf/prog_tests/mptcp.c
@@ -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);
}
diff --git a/tools/testing/selftests/bpf/prog_tests/select_reuseport.c b/tools/testing/selftests/bpf/prog_tests/select_reuseport.c
index 64c5f5eb2994..cdf9dd626877 100644
--- a/tools/testing/selftests/bpf/prog_tests/select_reuseport.c
+++ b/tools/testing/selftests/bpf/prog_tests/select_reuseport.c
@@ -56,13 +56,6 @@ static union sa46 {
} \
})
-#define RET_ERR(condition, tag, format...) ({ \
- if (CHECK_FAIL(condition)) { \
- printf(tag " " format); \
- return -1; \
- } \
-})
-
static int create_maps(enum bpf_map_type inner_type)
{
LIBBPF_OPTS(bpf_map_create_opts, opts);
@@ -157,42 +150,6 @@ static void sa46_init_inany(union sa46 *sa, sa_family_t family)
sa->v4.sin_addr.s_addr = INADDR_ANY;
}
-static 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);
-}
-
-static 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;
-}
-
static void restore_sysctls(void)
{
if (saved_tcp_fo != -1)
diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c
index 4716e38e153a..d50599ccba9a 100644
--- a/tools/testing/selftests/bpf/test_progs.c
+++ b/tools/testing/selftests/bpf/test_progs.c
@@ -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)
{
diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h
index 3f058dfadbaf..1522ea930bf6 100644
--- a/tools/testing/selftests/bpf/test_progs.h
+++ b/tools/testing/selftests/bpf/test_progs.h
@@ -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"
--
2.38.1
Powered by blists - more mailing lists