public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/2] cleanups for sockmap_listen
@ 2023-09-28  9:20 Geliang Tang
  2023-09-28  9:20 ` [PATCH bpf-next 1/2] selftests/bpf: Enable CONFIG_VSOCKETS in config Geliang Tang
  2023-09-28  9:20 ` [PATCH bpf-next 2/2] selftests/bpf: Add pair_redir_to_connected helper Geliang Tang
  0 siblings, 2 replies; 4+ messages in thread
From: Geliang Tang @ 2023-09-28  9:20 UTC (permalink / raw)
  To: Andrii Nakryiko, Mykola Lysenko, Alexei Starovoitov,
	Daniel Borkmann, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa
  Cc: Geliang Tang, bpf, linux-kselftest

Two cleanups for sockmap_listen selftests: enable a kconfig and add a
new helper.

Geliang Tang (2):
  selftests/bpf: Enable CONFIG_VSOCKETS in config
  selftests/bpf: Add pair_redir_to_connected helper

 tools/testing/selftests/bpf/config            |   1 +
 .../selftests/bpf/prog_tests/sockmap_listen.c | 149 ++++--------------
 2 files changed, 31 insertions(+), 119 deletions(-)

-- 
2.35.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH bpf-next 1/2] selftests/bpf: Enable CONFIG_VSOCKETS in config
  2023-09-28  9:20 [PATCH bpf-next 0/2] cleanups for sockmap_listen Geliang Tang
@ 2023-09-28  9:20 ` Geliang Tang
  2023-09-28  9:20 ` [PATCH bpf-next 2/2] selftests/bpf: Add pair_redir_to_connected helper Geliang Tang
  1 sibling, 0 replies; 4+ messages in thread
From: Geliang Tang @ 2023-09-28  9:20 UTC (permalink / raw)
  To: Andrii Nakryiko, Mykola Lysenko, Alexei Starovoitov,
	Daniel Borkmann, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa
  Cc: Geliang Tang, bpf, linux-kselftest

CONFIG_VSOCKETS is required by BPF selftests, otherwise we get errors
like this:

    ./test_progs:socket_loopback_reuseport:386: socket:
		Address family not supported by protocol
    socket_loopback_reuseport:FAIL:386
    ./test_progs:vsock_unix_redir_connectible:1496:
		vsock_socketpair_connectible() failed
    vsock_unix_redir_connectible:FAIL:1496

So this patch enables it in tools/testing/selftests/bpf/config.

Signed-off-by: Geliang Tang <geliang.tang@suse.com>
---
 tools/testing/selftests/bpf/config | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/testing/selftests/bpf/config b/tools/testing/selftests/bpf/config
index e41eb33b2704..02dd4409200e 100644
--- a/tools/testing/selftests/bpf/config
+++ b/tools/testing/selftests/bpf/config
@@ -84,3 +84,4 @@ CONFIG_USERFAULTFD=y
 CONFIG_VXLAN=y
 CONFIG_XDP_SOCKETS=y
 CONFIG_XFRM_INTERFACE=y
+CONFIG_VSOCKETS=y
-- 
2.35.3


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH bpf-next 2/2] selftests/bpf: Add pair_redir_to_connected helper
  2023-09-28  9:20 [PATCH bpf-next 0/2] cleanups for sockmap_listen Geliang Tang
  2023-09-28  9:20 ` [PATCH bpf-next 1/2] selftests/bpf: Enable CONFIG_VSOCKETS in config Geliang Tang
@ 2023-09-28  9:20 ` Geliang Tang
  2023-10-02 13:35   ` Daniel Borkmann
  1 sibling, 1 reply; 4+ messages in thread
From: Geliang Tang @ 2023-09-28  9:20 UTC (permalink / raw)
  To: Andrii Nakryiko, Mykola Lysenko, Alexei Starovoitov,
	Daniel Borkmann, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa
  Cc: Geliang Tang, bpf, linux-kselftest

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 create a new helper pair_redir_to_connected(). Create the different
socketpair in these four functions, then pass the socketpair infos to
the new common helper to do the connections.

Signed-off-by: Geliang Tang <geliang.tang@suse.com>
---
 .../selftests/bpf/prog_tests/sockmap_listen.c | 149 ++++--------------
 1 file changed, 30 insertions(+), 119 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c
index 8df8cbb447f1..fbdf7eb23c1b 100644
--- a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c
+++ b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c
@@ -1336,27 +1336,17 @@ static void test_redir(struct test_sockmap_listen *skel, struct bpf_map *map,
 	}
 }
 
-static void unix_redir_to_connected(int sotype, int sock_mapfd,
-			       int verd_mapfd, enum redir_mode mode)
+static void pair_redir_to_connected(int c0, int p0, int c1, int p1,
+		int sock_mapfd, int verd_mapfd, enum redir_mode mode)
 {
 	const char *log_prefix = redir_mode_str(mode);
-	int c0, c1, p0, p1;
 	unsigned int pass;
 	int err, n;
-	int sfd[2];
 	u32 key;
 	char b;
 
 	zero_verdict_count(verd_mapfd);
 
-	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];
-
 	err = add_to_sockmap(sock_mapfd, p0, p1);
 	if (err)
 		goto close;
@@ -1385,7 +1375,25 @@ static void unix_redir_to_connected(int sotype, int sock_mapfd,
 close:
 	xclose(c1);
 	xclose(p1);
-close0:
+}
+
+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 close;
+	c1 = sfd[0], p1 = sfd[1];
+
+	pair_redir_to_connected(c0, p0, c1, p1, sock_mapfd, verd_mapfd, mode);
+
+close:
 	xclose(c0);
 	xclose(p0);
 }
@@ -1661,51 +1669,19 @@ 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,
 				   enum redir_mode mode)
 {
-	const char *log_prefix = redir_mode_str(mode);
 	int c0, c1, p0, p1;
-	unsigned int pass;
-	int err, n;
-	u32 key;
-	char b;
-
-	zero_verdict_count(verd_mapfd);
+	int err;
 
 	err = inet_socketpair(family, SOCK_DGRAM, &p0, &c0);
 	if (err)
 		return;
 	err = inet_socketpair(family, SOCK_DGRAM, &p1, &c1);
 	if (err)
-		goto close_cli0;
-
-	err = add_to_sockmap(sock_mapfd, p0, p1);
-	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);
+		goto close;
 
-	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);
+	pair_redir_to_connected(c0, p0, c1, p1, sock_mapfd, verd_mapfd, mode);
 
-close_cli1:
-	xclose(c1);
-	xclose(p1);
-close_cli0:
+close:
 	xclose(c0);
 	xclose(p0);
 }
@@ -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,
 					int verd_mapfd, enum redir_mode mode)
 {
-	const char *log_prefix = redir_mode_str(mode);
 	int c0, c1, p0, p1;
-	unsigned int pass;
-	int err, n;
 	int sfd[2];
-	u32 key;
-	char b;
-
-	zero_verdict_count(verd_mapfd);
+	int err;
 
 	if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, sfd))
 		return;
@@ -1765,34 +1735,8 @@ static void inet_unix_redir_to_connected(int family, int type, int sock_mapfd,
 	if (err)
 		goto close;
 
-	err = add_to_sockmap(sock_mapfd, p0, p1);
-	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;
+	pair_redir_to_connected(c0, p0, c1, p1, sock_mapfd, verd_mapfd, mode);
 
-	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(p1);
 close:
 	xclose(c0);
 	xclose(p0);
@@ -1827,56 +1771,23 @@ 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,
 					int verd_mapfd, enum redir_mode mode)
 {
-	const char *log_prefix = redir_mode_str(mode);
 	int c0, c1, p0, p1;
-	unsigned int pass;
-	int err, n;
 	int sfd[2];
-	u32 key;
-	char b;
-
-	zero_verdict_count(verd_mapfd);
+	int err;
 
 	err = inet_socketpair(family, SOCK_DGRAM, &p0, &c0);
 	if (err)
 		return;
 
 	if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, sfd))
-		goto close_cli0;
-	c1 = sfd[0], p1 = sfd[1];
-
-	err = add_to_sockmap(sock_mapfd, p0, p1);
-	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;
+	c1 = sfd[0], p1 = sfd[1];
 
-	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);
+	pair_redir_to_connected(c0, p0, c1, p1, sock_mapfd, verd_mapfd, mode);
 
 close:
-	xclose(c1);
-	xclose(p1);
-close_cli0:
 	xclose(c0);
 	xclose(p0);
-
 }
 
 static void unix_inet_skb_redir_to_connected(struct test_sockmap_listen *skel,
-- 
2.35.3


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf-next 2/2] selftests/bpf: Add pair_redir_to_connected helper
  2023-09-28  9:20 ` [PATCH bpf-next 2/2] selftests/bpf: Add pair_redir_to_connected helper Geliang Tang
@ 2023-10-02 13:35   ` Daniel Borkmann
  0 siblings, 0 replies; 4+ messages in thread
From: Daniel Borkmann @ 2023-10-02 13:35 UTC (permalink / raw)
  To: Geliang Tang, Andrii Nakryiko, Mykola Lysenko, Alexei Starovoitov,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa
  Cc: bpf, linux-kselftest

On 9/28/23 11:20 AM, Geliang Tang wrote:
> 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 create a new helper pair_redir_to_connected(). Create the different
> socketpair in these four functions, then pass the socketpair infos to
> the new common helper to do the connections.
> 
> Signed-off-by: Geliang Tang <geliang.tang@suse.com>
> ---
>   .../selftests/bpf/prog_tests/sockmap_listen.c | 149 ++++--------------
>   1 file changed, 30 insertions(+), 119 deletions(-)
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c
> index 8df8cbb447f1..fbdf7eb23c1b 100644
> --- a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c
> +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c
> @@ -1336,27 +1336,17 @@ static void test_redir(struct test_sockmap_listen *skel, struct bpf_map *map,
>   	}
>   }
>   
> -static void unix_redir_to_connected(int sotype, int sock_mapfd,
> -			       int verd_mapfd, enum redir_mode mode)
> +static void pair_redir_to_connected(int c0, int p0, int c1, int p1,
> +		int sock_mapfd, int verd_mapfd, enum redir_mode mode)

just nit: indent, and could we find sth more descriptive than {c,p}0 and {c,p}1?

otherwise, lgtm.

>   {
>   	const char *log_prefix = redir_mode_str(mode);
> -	int c0, c1, p0, p1;
>   	unsigned int pass;
>   	int err, n;
> -	int sfd[2];
>   	u32 key;
>   	char b;
>   
>   	zero_verdict_count(verd_mapfd);
>   

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-10-02 13:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-28  9:20 [PATCH bpf-next 0/2] cleanups for sockmap_listen Geliang Tang
2023-09-28  9:20 ` [PATCH bpf-next 1/2] selftests/bpf: Enable CONFIG_VSOCKETS in config Geliang Tang
2023-09-28  9:20 ` [PATCH bpf-next 2/2] selftests/bpf: Add pair_redir_to_connected helper Geliang Tang
2023-10-02 13:35   ` Daniel Borkmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox