Skip to content

Commit

Permalink
iters_address fix
Browse files Browse the repository at this point in the history
Signed-off-by: Geliang Tang <[email protected]>
  • Loading branch information
Geliang Tang committed Oct 21, 2024
1 parent c6b1935 commit 98f7ce0
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 110 deletions.
183 changes: 74 additions & 109 deletions tools/testing/selftests/bpf/prog_tests/mptcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,31 +660,52 @@ static char *get_events_str(char *type)
int fd;

fd = open(PM_EVENTS, O_RDONLY);
if (!ASSERT_OK_FD(fd, "failed to open pm events"))
if (fd < 0)
return NULL;

len = read(fd, buf, sizeof(buf));
ASSERT_GT(len, 0, "failed to read pm events");

close(fd);
if (len <= 0)
return NULL;

return strstr(buf, type);
}

static int userspace_pm_add_subflow(char *addr, __u8 id)
static int wait_for_token(int fd)
{
__u32 token;
char *str;
int i, n;

/* Wait max 5 sec for pm events */
for (i = 0; i < 50; i++) {
SYS(fail, "ip netns exec %s echo sleep 0.2 s", NS_TEST);
usleep(200000); /* 0.2s */
send_byte(fd);

str = get_events_str("type:2");
if (!str)
continue;
n = sscanf(strstr(str, "token"), "token:%u,", &token);
if (n != 1)
continue;
return token;
}
fail:
return 0;
}

static int userspace_pm_add_subflow(__u32 token, char *addr, __u8 id)
{
bool ipv6 = strstr(addr, ":");
__u32 token, sport, dport;
__u32 sport, dport;
char *str;
int n;

str = get_events_str("type:2");
if (!str)
return -1;

n = sscanf(strstr(str, "token"), "token:%u,", &token);
if (n != 1)
return -1;
n = sscanf(strstr(str, "sport"), "sport:%u,dport:%u,", &sport, &dport);
if (n != 2)
return -1;
Expand All @@ -698,9 +719,9 @@ static int userspace_pm_add_subflow(char *addr, __u8 id)
static void run_iters_address(void)
{
int server_fd, client_fd, accept_fd;
char cc[TCP_CA_NAME_MAX];
int is_mptcp, err;
socklen_t len;
int err;
__u32 token;

server_fd = start_mptcp_server(AF_INET6, "::ffff:"ADDR_1, PORT_1, 0);
if (!ASSERT_OK_FD(server_fd, "start_mptcp_server"))
Expand All @@ -714,24 +735,26 @@ static void run_iters_address(void)
if (!ASSERT_OK_FD(accept_fd, "accept"))
goto close_client;

usleep(100000);
send_byte(client_fd);
token = wait_for_token(client_fd);
if (!token)
goto close_client;

recv_byte(accept_fd);
usleep(100000);

err = userspace_pm_add_subflow("::ffff:"ADDR_2, 10);
err = err ?: userspace_pm_add_subflow("::ffff:"ADDR_3, 20);
err = err ?: userspace_pm_add_subflow("::ffff:"ADDR_4, 30);
err = userspace_pm_add_subflow(token, "::ffff:"ADDR_2, 10);
err = err ?: userspace_pm_add_subflow(token, "::ffff:"ADDR_3, 20);
err = err ?: userspace_pm_add_subflow(token, "::ffff:"ADDR_4, 30);
if (!ASSERT_OK(err, "userspace_pm_add_subflow"))
goto close_accept;

send_byte(accept_fd);
recv_byte(client_fd);

len = sizeof(cc);
err = getsockopt(client_fd, SOL_TCP, TCP_CONGESTION, cc, &len);
if (ASSERT_OK(err, "getsockopt(client_fd, TCP_CONGESTION)"))
ASSERT_STREQ(cc, "cubic", "cc");
len = sizeof(is_mptcp);
/* mainly to trigger the BPF program */
err = getsockopt(client_fd, SOL_TCP, TCP_IS_MPTCP, &is_mptcp, &len);
if (ASSERT_OK(err, "getsockopt(client_fd, TCP_IS_MPTCP)"))
ASSERT_EQ(is_mptcp, 1, "is_mptcp");

close_accept:
close(accept_fd);
Expand Down Expand Up @@ -771,6 +794,7 @@ static void test_iters_address(void)

run_iters_address();

/* 10 + 20 + 30 = 60 */
ASSERT_EQ(skel->bss->ids, 60, "address ids");

userspace_pm_cleanup();
Expand All @@ -782,40 +806,16 @@ static void test_iters_address(void)
close(cgroup_fd);
}

static int userspace_pm_add_addr(char *addr, __u8 id)
static int userspace_pm_add_addr(__u32 token, char *addr, __u8 id)
{
__u32 token;
char *str;
int n;

str = get_events_str("type:2");
if (!str)
return -1;

n = sscanf(strstr(str, "token"), "token:%u,", &token);
if (n != 1)
return -1;
SYS_NOFAIL("ip netns exec %s %s ann %s id %u token %u",
NS_TEST, PM_CTL, addr, id, token);

return 0;
return SYS_NOFAIL("ip netns exec %s %s ann %s id %u token %u",
NS_TEST, PM_CTL, addr, id, token);
}

static int userspace_pm_get_addr(__u8 id, char *output)
static int userspace_pm_get_addr(__u32 token, __u8 id, char *output)
{
char buf[1024];
__u32 token;
char *str;
FILE *fp;
int n;

str = get_events_str("type:2");
if (!str)
return -1;

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);
Expand All @@ -833,21 +833,10 @@ static int userspace_pm_get_addr(__u8 id, char *output)
return 0;
}

static int userspace_pm_dump_addr(char *output)
static int userspace_pm_dump_addr(__u32 token, char *output)
{
char buf[1024];
__u32 token;
char *str;
FILE *fp;
int n;

str = get_events_str("type:2");
if (!str)
return -1;

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);
Expand All @@ -865,71 +854,48 @@ static int userspace_pm_dump_addr(char *output)
return 0;
}

static int userspace_pm_set_flags(char *addr, char *flags)
static int userspace_pm_set_flags(__u32 token, char *addr, char *flags)
{
bool ipv6 = strstr(addr, ":");
__u32 token, sport, dport;
__u32 sport, dport;
char *str;
int n;

str = get_events_str("type:10");
if (!str)
return -1;

n = sscanf(strstr(str, "token"), "token:%u,", &token);
if (n != 1)
return -1;
n = sscanf(strstr(str, "sport"), "sport:%u,dport:%u,", &sport, &dport);
if (n != 2)
return -1;
str = ipv6 ? (strstr(addr, ".") ? "::ffff:"ADDR_1 : ADDR6_1) : ADDR_1;
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, str, dport, flags, token);

return 0;
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, str, dport, flags, token);
}

static int userspace_pm_rm_subflow(char *addr, __u8 id)
static int userspace_pm_rm_subflow(__u32 token, char *addr, __u8 id)
{
bool ipv6 = strstr(addr, ":");
__u32 token, sport, dport;
__u32 sport, dport;
char *str;
int n;

str = get_events_str("type:10");
if (!str)
return -1;

n = sscanf(strstr(str, "token"), "token:%u,", &token);
if (n != 1)
return -1;
n = sscanf(strstr(str, "sport"), "sport:%u,dport:%u,", &sport, &dport);
if (n != 2)
return -1;
str = ipv6 ? (strstr(addr, ".") ? "::ffff:"ADDR_1 : ADDR6_1) : ADDR_1;
SYS_NOFAIL("ip netns exec %s %s dsf lip %s lport %u rip %s rport %u token %u",
NS_TEST, PM_CTL, addr, sport, str, dport, token);

return 0;
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, str, dport, token);
}

static int userspace_pm_rm_addr(__u8 id)
static int userspace_pm_rm_addr(__u32 token, __u8 id)
{
__u32 token;
char *str;
int n;

str = get_events_str("type:2");
if (!str)
return -1;

n = sscanf(strstr(str, "token"), "token:%u,", &token);
if (n != 1)
return -1;
SYS_NOFAIL("ip netns exec %s %s rem id %u token %u",
NS_TEST, PM_CTL, id, token);

return 0;
return SYS_NOFAIL("ip netns exec %s %s rem id %u token %u",
NS_TEST, PM_CTL, id, token);
}

static void run_userspace_pm(enum mptcp_pm_family family)
Expand All @@ -938,6 +904,7 @@ static void run_userspace_pm(enum mptcp_pm_family family)
bool ipv4mapped = (family == IPV4MAPPED);
int server_fd, client_fd, accept_fd;
char output[1024];
__u32 token;
char *addr;
int err;

Expand All @@ -954,95 +921,93 @@ static void run_userspace_pm(enum mptcp_pm_family family)
if (!ASSERT_OK_FD(accept_fd, "accept"))
goto close_client;

usleep(100000);
send_byte(client_fd);
token = wait_for_token(client_fd);
recv_byte(accept_fd);
usleep(100000);

addr = ipv6 ? (ipv4mapped ? "::ffff:"ADDR_2 : ADDR6_2) : ADDR_2;
err = userspace_pm_add_subflow(addr, 100);
err = userspace_pm_add_subflow(token, addr, 100);
if (!ASSERT_OK(err, "userspace_pm_add_subflow 100"))
goto close_accept;

send_byte(accept_fd);
recv_byte(client_fd);

sprintf(output, "id 100 flags subflow %s\n", addr);
err = userspace_pm_get_addr(100, output);
err = userspace_pm_get_addr(token, 100, output);
if (!ASSERT_OK(err, "userspace_pm_get_addr 100"))
goto close_accept;

send_byte(client_fd);
recv_byte(accept_fd);

err = userspace_pm_set_flags(addr, "backup");
err = userspace_pm_set_flags(token, addr, "backup");
if (!ASSERT_OK(err, "userspace_pm_set_flags backup"))
goto close_accept;

send_byte(accept_fd);
recv_byte(client_fd);

sprintf(output, "id 100 flags subflow,backup %s\n", addr);
err = userspace_pm_get_addr(100, output);
err = userspace_pm_get_addr(token, 100, output);
if (!ASSERT_OK(err, "userspace_pm_get_addr 100"))
goto close_accept;

send_byte(client_fd);
recv_byte(accept_fd);

err = userspace_pm_set_flags(addr, "nobackup");
err = userspace_pm_set_flags(token, addr, "nobackup");
if (!ASSERT_OK(err, "userspace_pm_set_flags nobackup"))
goto close_accept;

send_byte(accept_fd);
recv_byte(client_fd);

sprintf(output, "id 100 flags subflow %s\n", addr);
err = userspace_pm_get_addr(100, output);
err = userspace_pm_get_addr(token, 100, output);
if (!ASSERT_OK(err, "userspace_pm_get_addr 100"))
goto close_accept;

send_byte(client_fd);
recv_byte(accept_fd);

err = userspace_pm_rm_subflow(addr, 100);
err = userspace_pm_rm_subflow(token, addr, 100);
if (!ASSERT_OK(err, "userspace_pm_rm_subflow 100"))
goto close_accept;

send_byte(accept_fd);
recv_byte(client_fd);

err = userspace_pm_dump_addr("");
err = userspace_pm_dump_addr(token, "");
if (!ASSERT_OK(err, "userspace_pm_dump_addr"))
goto close_accept;

send_byte(client_fd);
recv_byte(accept_fd);

addr = ipv6 ? (ipv4mapped ? "::ffff:"ADDR_3 : ADDR6_3) : ADDR_3;
err = userspace_pm_add_addr(addr, 200);
err = userspace_pm_add_addr(token, addr, 200);
if (!ASSERT_OK(err, "userspace_pm_add_addr 200"))
goto close_accept;

send_byte(accept_fd);
recv_byte(client_fd);

sprintf(output, "id 200 flags signal %s\n", addr);
err = userspace_pm_dump_addr(output);
err = userspace_pm_dump_addr(token, output);
if (!ASSERT_OK(err, "userspace_pm_dump_addr"))
goto close_accept;

send_byte(client_fd);
recv_byte(accept_fd);

err = userspace_pm_rm_addr(200);
err = userspace_pm_rm_addr(token, 200);
if (!ASSERT_OK(err, "userspace_pm_rm_addr 200"))
goto close_accept;

send_byte(accept_fd);
recv_byte(client_fd);

err = userspace_pm_rm_addr(0);
err = userspace_pm_rm_addr(token, 0);
ASSERT_OK(err, "userspace_pm_rm_addr 0");

close_accept:
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/progs/mptcp_bpf_iters.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ int iters_address(struct bpf_sockopt *ctx)
int local_ids = 0;

if (!sk || sk->protocol != IPPROTO_MPTCP ||
ctx->level != SOL_TCP || ctx->optname != TCP_CONGESTION)
ctx->level != SOL_TCP || ctx->optname != TCP_IS_MPTCP)
return 1;

msk = bpf_mptcp_sk((struct sock *)sk);
Expand Down

0 comments on commit 98f7ce0

Please sign in to comment.