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@xxxxxxxxx> --- .../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