diff mbox series

[bpf-next,v8,8/9] selftests/bpf: Use connect_to_addr in sk_lookup

Message ID 52c22148c66184baf5ba09d60c06d49a8a33d743.1720147953.git.tanggeliang@kylinos.cn (mailing list archive)
State New
Headers show
Series use network helpers, part 8 | expand

Commit Message

Geliang Tang July 5, 2024, 2:58 a.m. UTC
From: Geliang Tang <tanggeliang@kylinos.cn>

Use public network helpers make_sockaddr() and connect_to_addr() instead
of using the local defined function make_socket() and connect().

This make_socket() can be dropped latter.

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 .../selftests/bpf/prog_tests/sk_lookup.c      | 22 ++++++++-----------
 1 file changed, 9 insertions(+), 13 deletions(-)

Comments

Martin KaFai Lau July 5, 2024, 9:56 p.m. UTC | #1
On 7/4/24 7:58 PM, Geliang Tang wrote:
> From: Geliang Tang <tanggeliang@kylinos.cn>
> 
> Use public network helpers make_sockaddr() and connect_to_addr() instead
> of using the local defined function make_socket() and connect().
> 
> This make_socket() can be dropped latter.
> 
> Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
> ---
>   .../selftests/bpf/prog_tests/sk_lookup.c      | 22 ++++++++-----------
>   1 file changed, 9 insertions(+), 13 deletions(-)
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c
> index ef4a3db34c5f..a436ed8b34e0 100644
> --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c
> +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c
> @@ -231,23 +231,19 @@ static int make_server(int sotype, const char *ip, int port,
>   
>   static int make_client(int sotype, const char *ip, int port)
>   {
> +	int family = is_ipv6(ip) ? AF_INET6 : AF_INET;
> +	struct network_helper_opts opts = {
> +		.timeout_ms = IO_TIMEOUT_SEC,
> +	};
>   	struct sockaddr_storage addr = {0};
> -	int err, fd;
> +	socklen_t len;
> +	int err;
>   
> -	fd = make_socket(sotype, ip, port, &addr);
> -	if (fd < 0)
> +	err = make_sockaddr(family, ip, port, &addr, &len);
> +	if (!ASSERT_OK(err, "make_sockaddr"))
>   		return -1;
>   
> -	err = connect(fd, (void *)&addr, inetaddr_len(&addr));
> -	if (CHECK(err, "make_client", "connect")) {
> -		log_err("failed to connect client socket");
> -		goto fail;
> -	}
> -
> -	return fd;
> -fail:
> -	close(fd);
> -	return -1;
> +	return connect_to_addr(sotype, &addr, len, &opts);

You mentioned in v7 that ASSERT on the return value of the connect_to_addr() 
breaks the test.

However, the original code does CHECK which calls test__fail(). There are 
multiple tests call make_client() and some of them don't expect make_client() to 
fail. It is obvious that the failure will be hidden without the ASSERT. 
Something else needs to be adjusted for this refactoring. Please take a closer 
look first instead of hiding the potential future errors to pass the CI.

pw-bot: cr

>   }
>   
>   static __u64 socket_cookie(int fd)
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c
index ef4a3db34c5f..a436ed8b34e0 100644
--- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c
+++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c
@@ -231,23 +231,19 @@  static int make_server(int sotype, const char *ip, int port,
 
 static int make_client(int sotype, const char *ip, int port)
 {
+	int family = is_ipv6(ip) ? AF_INET6 : AF_INET;
+	struct network_helper_opts opts = {
+		.timeout_ms = IO_TIMEOUT_SEC,
+	};
 	struct sockaddr_storage addr = {0};
-	int err, fd;
+	socklen_t len;
+	int err;
 
-	fd = make_socket(sotype, ip, port, &addr);
-	if (fd < 0)
+	err = make_sockaddr(family, ip, port, &addr, &len);
+	if (!ASSERT_OK(err, "make_sockaddr"))
 		return -1;
 
-	err = connect(fd, (void *)&addr, inetaddr_len(&addr));
-	if (CHECK(err, "make_client", "connect")) {
-		log_err("failed to connect client socket");
-		goto fail;
-	}
-
-	return fd;
-fail:
-	close(fd);
-	return -1;
+	return connect_to_addr(sotype, &addr, len, &opts);
 }
 
 static __u64 socket_cookie(int fd)