Skip to content

Commit

Permalink
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 b98a7a0 commit 00a16f3
Showing 1 changed file with 71 additions and 56 deletions.
127 changes: 71 additions & 56 deletions tools/testing/selftests/bpf/prog_tests/mptcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -653,28 +653,33 @@ static void userspace_pm_cleanup(void)
;
}

static char *get_events_str(char *type)
static int get_events_line(char *type, char *line)
{
char buf[1024];
char buf[4096], *str;
size_t len;
int fd;

fd = open(PM_EVENTS, O_RDONLY);
if (fd < 0)
return NULL;
return -1;

len = read(fd, buf, sizeof(buf));
close(fd);
if (len <= 0)
return NULL;
return -1;

str = strstr(buf, type);
if (!str)
return -1;

return strstr(buf, type);
strcpy(line, str);
return 0;
}

static int wait_for_token(int fd)
{
char line[1024], *str;
__u32 token;
char *str;
int i;

/* Wait max 2 sec for the connection to be established */
Expand All @@ -683,10 +688,12 @@ static int wait_for_token(int fd)
usleep(200000); /* 0.2s */
send_byte(fd);

str = get_events_str("type:2");
if (get_events_line("type:2", line))
continue;
str = strstr(line, "token");
if (!str)
continue;
if (sscanf(strstr(str, "token"), "token:%u,", &token) != 1)
if (sscanf(str, "token:%u,", &token) != 1)
continue;
return token;
}
Expand All @@ -697,17 +704,20 @@ static int wait_for_token(int fd)
static int userspace_pm_add_subflow(__u32 token, char *addr, __u8 id)
{
bool ipv6 = strstr(addr, ":");
char line[1024], *str;
__u32 sport, dport;
char *str;

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

if (sscanf(strstr(str, "sport"), "sport:%u,", &sport) != 1)
str = strstr(line, "sport");
if (!str || sscanf(str, "sport:%u,", &sport) != 1)
return -1;
if (sscanf(strstr(str, "dport"), "dport:%u,", &dport) != 1)

str = strstr(line, "dport");
if (!str || sscanf(str, "dport:%u,", &dport) != 1)
return -1;

str = ipv6 ? (strstr(addr, ".") ? "::ffff:"ADDR_1 : ADDR6_1) : ADDR_1;
SYS_NOFAIL("ip netns exec %s %s csf lip %s lid %u rip %s rport %u token %u",
NS_TEST, PM_CTL, addr, id, str, dport, token);
Expand Down Expand Up @@ -810,62 +820,59 @@ static int userspace_pm_add_addr(__u32 token, char *addr, __u8 id)
NS_TEST, PM_CTL, addr, id, token);
}

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

sprintf(buf, "ip netns exec %s %s get %u token %u",
sprintf(cmd, "ip netns exec %s %s get %u token %u",
NS_TEST, PM_CTL, id, token);
fp = popen(buf, "r");
fp = popen(cmd, "r");
if (!fp)
return -1;

bzero(buf, sizeof(buf));
fread(buf, 1, sizeof(buf), fp);
bzero(output, size);
fread(output, 1, size, fp);
pclose(fp);

if (!ASSERT_STRNEQ(buf, output, sizeof(buf), "get_addr"))
return -1;

return 0;
}

static int userspace_pm_dump_addr(__u32 token, char *output)
static int userspace_pm_dump_addr(__u32 token, char *output, size_t size)
{
char buf[1024];
char cmd[1024];
FILE *fp;

sprintf(buf, "ip netns exec %s %s dump token %u",
sprintf(cmd, "ip netns exec %s %s dump token %u",
NS_TEST, PM_CTL, token);
fp = popen(buf, "r");
fp = popen(cmd, "r");
if (!fp)
return -1;

bzero(buf, sizeof(buf));
fread(buf, 1, sizeof(buf), fp);
bzero(output, size);
fread(output, 1, size, fp);
pclose(fp);

if (!ASSERT_STRNEQ(buf, output, sizeof(buf), "dump_addr"))
return -1;

return 0;
}

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

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

if (sscanf(strstr(str, "sport"), "sport:%u,", &sport) != 1)
str = strstr(line, "sport");
if (!str || sscanf(str, "sport:%u,", &sport) != 1)
return -1;
if (sscanf(strstr(str, "dport"), "dport:%u,", &dport) != 1)

str = strstr(line, "dport");
if (!str || sscanf(str, "dport:%u,", &dport) != 1)
return -1;

str = ipv6 ? (strstr(addr, ".") ? "::ffff:"ADDR_1 : ADDR6_1) : ADDR_1;
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);
Expand All @@ -874,17 +881,20 @@ static int userspace_pm_set_flags(__u32 token, char *addr, char *flags)
static int userspace_pm_rm_subflow(__u32 token, char *addr, __u8 id)
{
bool ipv6 = strstr(addr, ":");
char line[1024], *str;
__u32 sport, dport;
char *str;

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

if (sscanf(strstr(str, "sport"), "sport:%u,", &sport) != 1)
str = strstr(line, "sport");
if (!str || sscanf(str, "sport:%u,", &sport) != 1)
return -1;
if (sscanf(strstr(str, "dport"), "dport:%u,", &dport) != 1)

str = strstr(line, "dport");
if (!str || sscanf(str, "dport:%u,", &dport) != 1)
return -1;

str = ipv6 ? (strstr(addr, ".") ? "::ffff:"ADDR_1 : ADDR6_1) : ADDR_1;
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);
Expand All @@ -901,7 +911,7 @@ static void run_userspace_pm(enum mptcp_pm_family family)
bool ipv6 = (family == IPV6 || family == IPV4MAPPED);
bool ipv4mapped = (family == IPV4MAPPED);
int server_fd, client_fd, accept_fd;
char output[1024];
char output[1024], expect[1024];
__u32 token;
char *addr;
int err;
Expand Down Expand Up @@ -932,9 +942,10 @@ static void run_userspace_pm(enum mptcp_pm_family family)
send_byte(accept_fd);
recv_byte(client_fd);

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

send_byte(client_fd);
Expand All @@ -947,9 +958,10 @@ static void run_userspace_pm(enum mptcp_pm_family family)
send_byte(accept_fd);
recv_byte(client_fd);

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

send_byte(client_fd);
Expand All @@ -962,9 +974,10 @@ static void run_userspace_pm(enum mptcp_pm_family family)
send_byte(accept_fd);
recv_byte(client_fd);

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

send_byte(client_fd);
Expand All @@ -977,8 +990,9 @@ static void run_userspace_pm(enum mptcp_pm_family family)
send_byte(accept_fd);
recv_byte(client_fd);

err = userspace_pm_dump_addr(token, "");
if (!ASSERT_OK(err, "userspace_pm_dump_addr"))
err = userspace_pm_dump_addr(token, output, sizeof(output));
if (!ASSERT_OK(err, "userspace_pm_dump_addr") ||
!ASSERT_STRNEQ(output, "", sizeof(output), "dump_addr"))
goto close_accept;

send_byte(client_fd);
Expand All @@ -992,9 +1006,10 @@ static void run_userspace_pm(enum mptcp_pm_family family)
send_byte(accept_fd);
recv_byte(client_fd);

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

send_byte(client_fd);
Expand Down

0 comments on commit 00a16f3

Please sign in to comment.