diff --git a/tools/testing/selftests/bpf/prog_tests/mptcp.c b/tools/testing/selftests/bpf/prog_tests/mptcp.c index 101835598a15..81e33506bae5 100644 --- a/tools/testing/selftests/bpf/prog_tests/mptcp.c +++ b/tools/testing/selftests/bpf/prog_tests/mptcp.c @@ -30,6 +30,7 @@ #define ADDR_4 "10.0.1.4" #define PORT_1 10001 #define PM_EVENTS "/tmp/bpf_userspace_pm_events" +#define PM_CTL "./mptcp_pm_nl_ctl" #define WITH_DATA true #define WITHOUT_DATA false @@ -388,8 +389,8 @@ static int endpoint_add(char *addr, char *flags, bool ip_mptcp) 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 ./mptcp_pm_nl_ctl add %s flags %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) @@ -405,7 +406,7 @@ 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)) { - SYS(fail, "ip netns exec %s ./mptcp_pm_nl_ctl limits 4 4", NS_TEST); + SYS(fail, "ip netns exec %s %s limits 4 4", NS_TEST, PM_CTL); ip_mptcp = false; } @@ -580,34 +581,31 @@ static void test_iters_subflow(void) mptcp_bpf_iters_subflow__destroy(skel); } -static struct nstoken *userspace_pm_init(enum mptcp_pm_type pm_type) +static int userspace_pm_init(enum mptcp_pm_type pm_type) { - struct nstoken *nstoken; - - nstoken = create_netns(NS_TEST); - if (!ASSERT_OK_PTR(nstoken, "create_netns")) - return NULL; - if (address_init()) goto fail; - SYS(fail, "ip netns exec %s sysctl -qw net.mptcp.pm_type=%u", NS_TEST, pm_type); - SYS(fail, "ip netns exec %s ./mptcp_pm_nl_ctl limits 4 4", NS_TEST); - SYS(fail, "ip netns exec %s ./mptcp_pm_nl_ctl events >> %s 2>&1 &", NS_TEST, PM_EVENTS); + SYS(fail, "ip netns exec %s sysctl -qw net.mptcp.pm_type=%u", + NS_TEST, pm_type); + SYS(fail, "ip netns exec %s %s limits 4 4", + NS_TEST, PM_CTL); + SYS(fail, "ip netns exec %s %s events >> %s 2>&1 &", + NS_TEST, PM_CTL, PM_EVENTS); - return nstoken; + return 0; fail: - cleanup_netns(nstoken); - return NULL; + return -1; } static void userspace_pm_cleanup(void) { - SYS(fail, "ip netns exec %s cat %s", NS_TEST, PM_EVENTS); + //SYS(fail, "ip netns exec %s cat %s", NS_TEST, PM_EVENTS); - SYS_NOFAIL("ip netns exec %s killall mptcp_pm_nl_ctl > /dev/null 2>&1", NS_TEST); + SYS_NOFAIL("ip netns exec %s killall %s > /dev/null 2>&1", + NS_TEST, PM_CTL); SYS_NOFAIL("ip netns exec %s rm -rf %s", NS_TEST, PM_EVENTS); -fail: +//fail: ; } @@ -633,15 +631,20 @@ static void userspace_pm_add_subflow(char *addr, __u8 id) { __u32 token, sport, dport; char *str; + int n; str = get_events_str("type:2"); if (!str) return; - sscanf(strstr(str, "token"), "token:%u,", &token); - sscanf(strstr(str, "sport"), "sport:%u,dport:%u,", &sport, &dport); - SYS_NOFAIL("ip netns exec %s ./mptcp_pm_nl_ctl csf lip %s lid %u rip %s rport %u token %u", - NS_TEST, addr, id, ADDR_1, dport, token); + n = sscanf(strstr(str, "token"), "token:%u,", &token); + if (n != 1) + return; + n = sscanf(strstr(str, "sport"), "sport:%u,dport:%u,", &sport, &dport); + if (n != 2) + return; + SYS_NOFAIL("ip netns exec %s %s csf lip %s lid %u rip %s rport %u token %u", + NS_TEST, PM_CTL, addr, id, ADDR_1, dport, token); } static void run_iters_entry(void) @@ -710,15 +713,20 @@ static void test_iters_entry(void) if (!ASSERT_OK_PTR(skel->links.iters_getsockopt, "attach getsockopt")) goto skel_destroy; - nstoken = userspace_pm_init(MPTCP_PM_TYPE_USERSPACE); - if (!ASSERT_OK_PTR(nstoken, "create_netns: iters_entry")) + nstoken = create_netns(NS_TEST); + if (!ASSERT_OK_PTR(nstoken, "create_netns")) goto skel_destroy; + err = userspace_pm_init(MPTCP_PM_TYPE_USERSPACE); + if (!ASSERT_OK(err, "userspace_pm_init: iters_entry")) + goto close_netns; + run_iters_entry(); ASSERT_EQ(skel->bss->entries, entries, "entries"); userspace_pm_cleanup(); +close_netns: cleanup_netns(nstoken); skel_destroy: mptcp_bpf_iters_entry__destroy(skel); @@ -817,96 +825,144 @@ static void userspace_pm_add_addr(char *addr, __u8 id) { __u32 token; char *str; + int n; str = get_events_str("type:2"); if (!str) return; - sscanf(strstr(str, "token"), "token:%u,", &token); - SYS_NOFAIL("ip netns exec %s ./mptcp_pm_nl_ctl ann %s id %u token %u", - NS_TEST, addr, id, token); + n = sscanf(strstr(str, "token"), "token:%u,", &token); + if (n != 1) + return; + SYS_NOFAIL("ip netns exec %s %s ann %s id %u token %u", + NS_TEST, PM_CTL, addr, id, token); } -static void userspace_pm_get_addr(__u8 id) +static int userspace_pm_get_addr(__u8 id, char *output) { + char buf[1024]; __u32 token; char *str; + FILE *fp; + int n; str = get_events_str("type:2"); if (!str) - return; + return -1; - sscanf(strstr(str, "token"), "token:%u,", &token); - printf("token=%u\n", token); - SYS(fail, "ip netns exec %s ./mptcp_pm_nl_ctl get %u token %u", - NS_TEST, id, token); -fail: - ; + n = sscanf(strstr(str, "token"), "token:%u,", &token); + if (n != 1) + return -1; + + sprintf(buf, "ip netns exec %s %s get %u token %u", + NS_TEST, PM_CTL, id, token); + fp = popen(buf, "r"); + if (!fp) + return -1; + + bzero(buf, sizeof(buf)); + fread(buf, 1, sizeof(buf), fp); + pclose(fp); + + if (!ASSERT_STRNEQ(buf, output, sizeof(buf), "get_addr")) + return -1; + + return 0; } -static void userspace_pm_dump_addr(void) +static int userspace_pm_dump_addr(char *output) { + char buf[1024]; __u32 token; char *str; + FILE *fp; + int n; str = get_events_str("type:2"); if (!str) - return; + return -1; - sscanf(strstr(str, "token"), "token:%u,", &token); - SYS(fail, "ip netns exec %s ./mptcp_pm_nl_ctl dump token %u", - NS_TEST, token); -fail: - ; + n = sscanf(strstr(str, "token"), "token:%u,", &token); + if (n != 1) + return -1; + + sprintf(buf, "ip netns exec %s %s dump token %u", + NS_TEST, PM_CTL, token); + fp = popen(buf, "r"); + if (!fp) + return -1; + + bzero(buf, sizeof(buf)); + fread(buf, 1, sizeof(buf), fp); + pclose(fp); + + if (!ASSERT_STRNEQ(buf, output, sizeof(buf), "dump_addr")) + return -1; + + return 0; } static void userspace_pm_set_flags(char *addr, char *flags) { __u32 token, sport, dport; char *str; + int n; str = get_events_str("type:10"); if (!str) return; - sscanf(strstr(str, "token"), "token:%u,", &token); - sscanf(strstr(str, "sport"), "sport:%u,dport:%u,", &sport, &dport); - SYS_NOFAIL("ip netns exec %s ./mptcp_pm_nl_ctl set %s port %u rip %s rport %u flags %s token %u", - NS_TEST, addr, sport, ADDR_1, dport, flags, token); + n = sscanf(strstr(str, "token"), "token:%u,", &token); + if (n != 1) + return; + n = sscanf(strstr(str, "sport"), "sport:%u,dport:%u,", &sport, &dport); + if (n != 2) + return; + SYS_NOFAIL("ip netns exec %s %s set %s port %u rip %s rport %u flags %s token %u", + NS_TEST, PM_CTL, addr, sport, ADDR_1, dport, flags, token); } static void userspace_pm_rm_subflow(char *addr, __u8 id) { __u32 token, sport, dport; char *str; + int n; str = get_events_str("type:10"); if (!str) return; - sscanf(strstr(str, "token"), "token:%u,", &token); - sscanf(strstr(str, "sport"), "sport:%u,dport:%u,", &sport, &dport); - SYS_NOFAIL("ip netns exec %s ./mptcp_pm_nl_ctl dsf lip %s lport %u rip %s rport %u token %u", - NS_TEST, addr, sport, ADDR_1, dport, token); + n = sscanf(strstr(str, "token"), "token:%u,", &token); + if (n != 1) + return; + n = sscanf(strstr(str, "sport"), "sport:%u,dport:%u,", &sport, &dport); + if (n != 2) + return; + SYS_NOFAIL("ip netns exec %s %s dsf lip %s lport %u rip %s rport %u token %u", + NS_TEST, PM_CTL, addr, sport, ADDR_1, dport, token); } static void userspace_pm_rm_addr(__u8 id) { __u32 token; char *str; + int n; str = get_events_str("type:2"); if (!str) return; - sscanf(strstr(str, "token"), "token:%u,", &token); - SYS_NOFAIL("ip netns exec %s ./mptcp_pm_nl_ctl rem id %u token %u", - NS_TEST, id, token); + n = sscanf(strstr(str, "token"), "token:%u,", &token); + if (n != 1) + return; + SYS_NOFAIL("ip netns exec %s %s rem id %u token %u", + NS_TEST, PM_CTL, id, token); } static void run_userspace_pm(void) { int server_fd, client_fd, accept_fd; + int err; server_fd = start_mptcp_server(AF_INET, ADDR_1, PORT_1, 0); if (!ASSERT_OK_FD(server_fd, "start_mptcp_server")) @@ -930,7 +986,9 @@ static void run_userspace_pm(void) send_byte(accept_fd); recv_byte(client_fd); - userspace_pm_get_addr(100); + err = userspace_pm_get_addr(100, "id 100 flags subflow 10.0.1.2\n"); + if (!ASSERT_OK(err, "userspace_pm_get_addr")) + goto close_accept; send_byte(accept_fd); recv_byte(client_fd); @@ -945,12 +1003,18 @@ static void run_userspace_pm(void) send_byte(accept_fd); recv_byte(client_fd); + err = userspace_pm_dump_addr(""); + if (!ASSERT_OK(err, "userspace_pm_dump_addr")) + goto close_accept; + userspace_pm_add_addr(ADDR_3, 200); send_byte(accept_fd); recv_byte(client_fd); - userspace_pm_dump_addr(); + err = userspace_pm_dump_addr("id 200 flags signal 10.0.1.3\n"); + if (!ASSERT_OK(err, "userspace_pm_dump_addr")) + goto close_accept; send_byte(accept_fd); recv_byte(client_fd); @@ -962,6 +1026,7 @@ static void run_userspace_pm(void) userspace_pm_rm_addr(0); +close_accept: close(accept_fd); close_client: close(client_fd); @@ -972,15 +1037,20 @@ static void run_userspace_pm(void) static void test_userspace_pm(void) { struct nstoken *nstoken; + int err; - nstoken = userspace_pm_init(MPTCP_PM_TYPE_USERSPACE); - if (!nstoken) + nstoken = create_netns(NS_TEST); + if (!ASSERT_OK_PTR(nstoken, "create_netns")) + return; + + err = userspace_pm_init(MPTCP_PM_TYPE_USERSPACE); + if (!ASSERT_OK(err, "userspace_pm_init: userspace pm")) goto fail; run_userspace_pm(); -fail: userspace_pm_cleanup(); +fail: cleanup_netns(nstoken); } @@ -989,21 +1059,27 @@ static void test_bpf_pm(struct bpf_object *obj, char *pm) struct nstoken *nstoken; struct bpf_link *link; struct bpf_map *map; + int err; map = bpf_object__find_map_by_name(obj, pm); link = bpf_map__attach_struct_ops(map); if (CHECK(!link, pm, "attach_struct_ops: %d\n", errno)) return; - nstoken = userspace_pm_init(MPTCP_PM_TYPE_BPF); - if (!nstoken) - goto fail; + nstoken = create_netns(NS_TEST); + if (!ASSERT_OK_PTR(nstoken, "create_netns")) + goto link_destroy; + + err = userspace_pm_init(MPTCP_PM_TYPE_BPF); + if (!ASSERT_OK(err, "userspace_pm_init: bpf pm")) + goto close_netns; run_userspace_pm(); -fail: userspace_pm_cleanup(); +close_netns: cleanup_netns(nstoken); +link_destroy: bpf_link__destroy(link); } @@ -1234,14 +1310,12 @@ static void test_stale(void) void test_mptcp(void) { -#if 1 if (test__start_subtest("base")) test_base(); if (test__start_subtest("mptcpify")) test_mptcpify(); if (test__start_subtest("subflow")) test_subflow(); -#endif if (test__start_subtest("iters_subflow")) test_iters_subflow(); if (test__start_subtest("iters_entry")) @@ -1252,7 +1326,6 @@ void test_mptcp(void) test_userspace_pm(); if (test__start_subtest("userspace_bpf")) test_userspace_bpf(); -#if 1 if (test__start_subtest("default")) test_default(); if (test__start_subtest("first")) @@ -1267,5 +1340,4 @@ void test_mptcp(void) test_burst(); if (test__start_subtest("stale")) test_stale(); -#endif }