diff mbox series

[bpf-next,v2,2/2] selftests/bpf: Add pairs_redir_to_connected helper

Message ID 10920edc470974553e66e2391400dfa81ec03d47.1696490003.git.geliang.tang@suse.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series cleanups for sockmap_listen | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 9 this patch: 9
netdev/cc_maintainers warning 4 maintainers not CCed: bobby.eshleman@bytedance.com xukuohai@huawei.com shuah@kernel.org jakub@cloudflare.com
netdev/build_clang success Errors and warnings before: 9 this patch: 9
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 9 this patch: 9
netdev/checkpatch warning CHECK: Alignment should match open parenthesis WARNING: Possible comma where semicolon could be used WARNING: line length of 89 exceeds 80 columns WARNING: line length of 90 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-0 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-1 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-5 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-6 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-28 success Logs for veristat

Commit Message

Geliang Tang Oct. 5, 2023, 7:21 a.m. UTC
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 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: Geliang Tang <geliang.tang@suse.com>
---
 .../selftests/bpf/prog_tests/sockmap_listen.c | 159 ++++--------------
 1 file changed, 35 insertions(+), 124 deletions(-)

Comments

Martin KaFai Lau Oct. 6, 2023, 5:18 a.m. UTC | #1
On 10/5/23 12:21 AM, Geliang Tang wrote:
> --- a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c
> +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c
> @@ -1336,32 +1336,22 @@ 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 pairs_redir_to_connected(int cli0, int peer0, int cli1, int peer1,
> +				     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);
> +	err = add_to_sockmap(sock_mapfd, peer0, peer1);
>   	if (err)
>   		goto close;
>   
> -	n = write(c1, "a", 1);
> +	n = write(cli1, "a", 1);
>   	if (n < 0)
>   		FAIL_ERRNO("%s: write", log_prefix);
>   	if (n == 0)
> @@ -1376,16 +1366,34 @@ static void unix_redir_to_connected(int sotype, int sock_mapfd,
>   	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);
> +	n = recv_timeout(mode == REDIR_INGRESS ? peer0 : cli0, &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(p1);
> -close0:
> +	xclose(cli1);
> +	xclose(peer1);
> +}
> +
> +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];
> +
> +	pairs_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);
> +	pairs_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);

Patch 1 is applied. Thanks.

In patch 2, the xclose() here is confusing after this change. It is also 
inconsistent from how other tests in sockmap_listen.c is doing it. c0/p0 and 
c1/p1 are opened here but only c0/p0 is closed here and c1/p1 is closed in the 
pairs_redir_to_connected() above instead.
Jakub Sitnicki Oct. 13, 2023, 10:40 a.m. UTC | #2
On Thu, Oct 05, 2023 at 10:18 PM -07, Martin KaFai Lau wrote:
> On 10/5/23 12:21 AM, Geliang Tang wrote:
>> --- a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c
>> +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c
>> @@ -1336,32 +1336,22 @@ 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 pairs_redir_to_connected(int cli0, int peer0, int cli1, int peer1,
>> +				     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);
>> +	err = add_to_sockmap(sock_mapfd, peer0, peer1);
>>   	if (err)
>>   		goto close;
>>   -	n = write(c1, "a", 1);
>> +	n = write(cli1, "a", 1);
>>   	if (n < 0)
>>   		FAIL_ERRNO("%s: write", log_prefix);
>>   	if (n == 0)
>> @@ -1376,16 +1366,34 @@ static void unix_redir_to_connected(int sotype, int sock_mapfd,
>>   	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);
>> +	n = recv_timeout(mode == REDIR_INGRESS ? peer0 : cli0, &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(p1);
>> -close0:
>> +	xclose(cli1);
>> +	xclose(peer1);
>> +}
>> +
>> +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];
>> +
>> +	pairs_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);
>> +	pairs_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);
>
> Patch 1 is applied. Thanks.
>
> In patch 2, the xclose() here is confusing after this change. It is also
> inconsistent from how other tests in sockmap_listen.c is doing it. c0/p0 and
> c1/p1 are opened here but only c0/p0 is closed here and c1/p1 is closed in the
> pairs_redir_to_connected() above instead.

I agree with Martin.

The refactoring idea itself is nice. But, I also find it surprising that
newly extracted pairs_redir_to_connected helper takes ownership of cli1
& peer1 FDs.

For me, ideally, we would start using the __cleanup__ attribute, like we
do now in the kernel code [1], to auto-close FDs on return. That would
make error recovery and resource release less verbose.

[1] https://elixir.bootlin.com/linux/v6.5/source/include/linux/cleanup.h
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c
index 8df8cbb447f1..74cbce11e22a 100644
--- a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c
+++ b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c
@@ -1336,32 +1336,22 @@  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 pairs_redir_to_connected(int cli0, int peer0, int cli1, int peer1,
+				     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);
+	err = add_to_sockmap(sock_mapfd, peer0, peer1);
 	if (err)
 		goto close;
 
-	n = write(c1, "a", 1);
+	n = write(cli1, "a", 1);
 	if (n < 0)
 		FAIL_ERRNO("%s: write", log_prefix);
 	if (n == 0)
@@ -1376,16 +1366,34 @@  static void unix_redir_to_connected(int sotype, int sock_mapfd,
 	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);
+	n = recv_timeout(mode == REDIR_INGRESS ? peer0 : cli0, &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(p1);
-close0:
+	xclose(cli1);
+	xclose(peer1);
+}
+
+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];
+
+	pairs_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);
+	pairs_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;
+	pairs_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);
+	pairs_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,