Commit fdd11c14 authored by Geliang Tang's avatar Geliang Tang Committed by Martin KaFai Lau

selftests/bpf: Add pairs_redir_to_connected helper

Extract duplicate code from these four functions

 unix_redir_to_connected()
 udp_redir_to_connected()
 inet_unix_redir_to_connected()
 unix_inet_redir_to_connected()

to generate a new helper pairs_redir_to_connected(). Create the
different socketpairs in these four functions, then pass the
socketpairs info to the new common helper to do the connections.
Signed-off-by: default avatarGeliang Tang <geliang.tang@suse.com>
Link: https://lore.kernel.org/r/54bb28dcf764e7d4227ab160883931d2173f4f3d.1696588133.git.geliang.tang@suse.comSigned-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
parent 0af3aace
...@@ -1336,53 +1336,59 @@ static void test_redir(struct test_sockmap_listen *skel, struct bpf_map *map, ...@@ -1336,53 +1336,59 @@ static void test_redir(struct test_sockmap_listen *skel, struct bpf_map *map,
} }
} }
static void unix_redir_to_connected(int sotype, int sock_mapfd, static void pairs_redir_to_connected(int cli0, int peer0, int cli1, int peer1,
int verd_mapfd, enum redir_mode mode) int sock_mapfd, int verd_mapfd, enum redir_mode mode)
{ {
const char *log_prefix = redir_mode_str(mode); const char *log_prefix = redir_mode_str(mode);
int c0, c1, p0, p1;
unsigned int pass; unsigned int pass;
int err, n; int err, n;
int sfd[2];
u32 key; u32 key;
char b; char b;
zero_verdict_count(verd_mapfd); zero_verdict_count(verd_mapfd);
if (socketpair(AF_UNIX, sotype | SOCK_NONBLOCK, 0, sfd)) err = add_to_sockmap(sock_mapfd, peer0, peer1);
return;
c0 = sfd[0], p0 = sfd[1];
if (socketpair(AF_UNIX, sotype | SOCK_NONBLOCK, 0, sfd))
goto close0;
c1 = sfd[0], p1 = sfd[1];
err = add_to_sockmap(sock_mapfd, p0, p1);
if (err) if (err)
goto close; return;
n = write(c1, "a", 1); n = write(cli1, "a", 1);
if (n < 0) if (n < 0)
FAIL_ERRNO("%s: write", log_prefix); FAIL_ERRNO("%s: write", log_prefix);
if (n == 0) if (n == 0)
FAIL("%s: incomplete write", log_prefix); FAIL("%s: incomplete write", log_prefix);
if (n < 1) if (n < 1)
goto close; return;
key = SK_PASS; key = SK_PASS;
err = xbpf_map_lookup_elem(verd_mapfd, &key, &pass); err = xbpf_map_lookup_elem(verd_mapfd, &key, &pass);
if (err) if (err)
goto close; return;
if (pass != 1) if (pass != 1)
FAIL("%s: want pass count 1, have %d", log_prefix, pass); FAIL("%s: want pass count 1, have %d", log_prefix, pass);
n = recv_timeout(mode == REDIR_INGRESS ? p0 : c0, &b, 1, 0, IO_TIMEOUT_SEC); n = recv_timeout(mode == REDIR_INGRESS ? peer0 : cli0, &b, 1, 0, IO_TIMEOUT_SEC);
if (n < 0) if (n < 0)
FAIL_ERRNO("%s: recv_timeout", log_prefix); FAIL_ERRNO("%s: recv_timeout", log_prefix);
if (n == 0) if (n == 0)
FAIL("%s: incomplete recv", log_prefix); FAIL("%s: incomplete recv", log_prefix);
}
static void unix_redir_to_connected(int sotype, int sock_mapfd,
int verd_mapfd, enum redir_mode mode)
{
int c0, c1, p0, p1;
int sfd[2];
if (socketpair(AF_UNIX, sotype | SOCK_NONBLOCK, 0, sfd))
return;
c0 = sfd[0], p0 = sfd[1];
if (socketpair(AF_UNIX, sotype | SOCK_NONBLOCK, 0, sfd))
goto close0;
c1 = sfd[0], p1 = sfd[1];
pairs_redir_to_connected(c0, p0, c1, p1, sock_mapfd, verd_mapfd, mode);
close:
xclose(c1); xclose(c1);
xclose(p1); xclose(p1);
close0: close0:
...@@ -1661,14 +1667,8 @@ static int inet_socketpair(int family, int type, int *s, int *c) ...@@ -1661,14 +1667,8 @@ static int inet_socketpair(int family, int type, int *s, int *c)
static void udp_redir_to_connected(int family, int sock_mapfd, int verd_mapfd, static void udp_redir_to_connected(int family, int sock_mapfd, int verd_mapfd,
enum redir_mode mode) enum redir_mode mode)
{ {
const char *log_prefix = redir_mode_str(mode);
int c0, c1, p0, p1; int c0, c1, p0, p1;
unsigned int pass; int err;
int err, n;
u32 key;
char b;
zero_verdict_count(verd_mapfd);
err = inet_socketpair(family, SOCK_DGRAM, &p0, &c0); err = inet_socketpair(family, SOCK_DGRAM, &p0, &c0);
if (err) if (err)
...@@ -1677,32 +1677,8 @@ static void udp_redir_to_connected(int family, int sock_mapfd, int verd_mapfd, ...@@ -1677,32 +1677,8 @@ static void udp_redir_to_connected(int family, int sock_mapfd, int verd_mapfd,
if (err) if (err)
goto close_cli0; goto close_cli0;
err = add_to_sockmap(sock_mapfd, p0, p1); pairs_redir_to_connected(c0, p0, c1, p1, sock_mapfd, verd_mapfd, mode);
if (err)
goto close_cli1;
n = write(c1, "a", 1);
if (n < 0)
FAIL_ERRNO("%s: write", log_prefix);
if (n == 0)
FAIL("%s: incomplete write", log_prefix);
if (n < 1)
goto close_cli1;
key = SK_PASS;
err = xbpf_map_lookup_elem(verd_mapfd, &key, &pass);
if (err)
goto close_cli1;
if (pass != 1)
FAIL("%s: want pass count 1, have %d", log_prefix, pass);
n = recv_timeout(mode == REDIR_INGRESS ? p0 : c0, &b, 1, 0, IO_TIMEOUT_SEC);
if (n < 0)
FAIL_ERRNO("%s: recv_timeout", log_prefix);
if (n == 0)
FAIL("%s: incomplete recv", log_prefix);
close_cli1:
xclose(c1); xclose(c1);
xclose(p1); xclose(p1);
close_cli0: close_cli0:
...@@ -1747,15 +1723,9 @@ static void test_udp_redir(struct test_sockmap_listen *skel, struct bpf_map *map ...@@ -1747,15 +1723,9 @@ static void test_udp_redir(struct test_sockmap_listen *skel, struct bpf_map *map
static void inet_unix_redir_to_connected(int family, int type, int sock_mapfd, static void inet_unix_redir_to_connected(int family, int type, int sock_mapfd,
int verd_mapfd, enum redir_mode mode) int verd_mapfd, enum redir_mode mode)
{ {
const char *log_prefix = redir_mode_str(mode);
int c0, c1, p0, p1; int c0, c1, p0, p1;
unsigned int pass;
int err, n;
int sfd[2]; int sfd[2];
u32 key; int err;
char b;
zero_verdict_count(verd_mapfd);
if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, sfd)) if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, sfd))
return; return;
...@@ -1765,32 +1735,8 @@ static void inet_unix_redir_to_connected(int family, int type, int sock_mapfd, ...@@ -1765,32 +1735,8 @@ static void inet_unix_redir_to_connected(int family, int type, int sock_mapfd,
if (err) if (err)
goto close; goto close;
err = add_to_sockmap(sock_mapfd, p0, p1); pairs_redir_to_connected(c0, p0, c1, p1, sock_mapfd, verd_mapfd, mode);
if (err)
goto close_cli1;
n = write(c1, "a", 1);
if (n < 0)
FAIL_ERRNO("%s: write", log_prefix);
if (n == 0)
FAIL("%s: incomplete write", log_prefix);
if (n < 1)
goto close_cli1;
key = SK_PASS;
err = xbpf_map_lookup_elem(verd_mapfd, &key, &pass);
if (err)
goto close_cli1;
if (pass != 1)
FAIL("%s: want pass count 1, have %d", log_prefix, pass);
n = recv_timeout(mode == REDIR_INGRESS ? p0 : c0, &b, 1, 0, IO_TIMEOUT_SEC);
if (n < 0)
FAIL_ERRNO("%s: recv_timeout", log_prefix);
if (n == 0)
FAIL("%s: incomplete recv", log_prefix);
close_cli1:
xclose(c1); xclose(c1);
xclose(p1); xclose(p1);
close: close:
...@@ -1827,15 +1773,9 @@ static void inet_unix_skb_redir_to_connected(struct test_sockmap_listen *skel, ...@@ -1827,15 +1773,9 @@ static void inet_unix_skb_redir_to_connected(struct test_sockmap_listen *skel,
static void unix_inet_redir_to_connected(int family, int type, int sock_mapfd, static void unix_inet_redir_to_connected(int family, int type, int sock_mapfd,
int verd_mapfd, enum redir_mode mode) int verd_mapfd, enum redir_mode mode)
{ {
const char *log_prefix = redir_mode_str(mode);
int c0, c1, p0, p1; int c0, c1, p0, p1;
unsigned int pass;
int err, n;
int sfd[2]; int sfd[2];
u32 key; int err;
char b;
zero_verdict_count(verd_mapfd);
err = inet_socketpair(family, SOCK_DGRAM, &p0, &c0); err = inet_socketpair(family, SOCK_DGRAM, &p0, &c0);
if (err) if (err)
...@@ -1845,32 +1785,8 @@ static void unix_inet_redir_to_connected(int family, int type, int sock_mapfd, ...@@ -1845,32 +1785,8 @@ static void unix_inet_redir_to_connected(int family, int type, int sock_mapfd,
goto close_cli0; goto close_cli0;
c1 = sfd[0], p1 = sfd[1]; c1 = sfd[0], p1 = sfd[1];
err = add_to_sockmap(sock_mapfd, p0, p1); pairs_redir_to_connected(c0, p0, c1, p1, sock_mapfd, verd_mapfd, mode);
if (err)
goto close;
n = write(c1, "a", 1);
if (n < 0)
FAIL_ERRNO("%s: write", log_prefix);
if (n == 0)
FAIL("%s: incomplete write", log_prefix);
if (n < 1)
goto close;
key = SK_PASS;
err = xbpf_map_lookup_elem(verd_mapfd, &key, &pass);
if (err)
goto close;
if (pass != 1)
FAIL("%s: want pass count 1, have %d", log_prefix, pass);
n = recv_timeout(mode == REDIR_INGRESS ? p0 : c0, &b, 1, 0, IO_TIMEOUT_SEC);
if (n < 0)
FAIL_ERRNO("%s: recv_timeout", log_prefix);
if (n == 0)
FAIL("%s: incomplete recv", log_prefix);
close:
xclose(c1); xclose(c1);
xclose(p1); xclose(p1);
close_cli0: close_cli0:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment