diff mbox series

[bpf-next,v2,05/14] selftests/bpf: Use connect_to_addr in sk_assign

Message ID 55a16644cf8b53e2bbf305fbd9ae21c6f8ad3cbf.1712796967.git.tanggeliang@kylinos.cn (mailing list archive)
State New
Headers show
Series use start_server and connect_to helpers | expand

Commit Message

Geliang Tang April 11, 2024, 1:03 a.m. UTC
From: Geliang Tang <tanggeliang@kylinos.cn>

This patch uses public helper connect_to_addr() exported in
network_helpers.h instead of the local defined function connect_to_server()
in prog_tests/sk_assign.c. This can avoid duplicate code.

The code that sets SO_SNDTIMEO timeout as timeo_sec (3s) can be dropped,
since connect_to_addr() sets default timeout as 3s.

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 .../selftests/bpf/prog_tests/sk_assign.c      | 26 +------------------
 1 file changed, 1 insertion(+), 25 deletions(-)

Comments

Eduard Zingerman April 12, 2024, 4:38 p.m. UTC | #1
On Thu, 2024-04-11 at 09:03 +0800, Geliang Tang wrote:
> From: Geliang Tang <tanggeliang@kylinos.cn>
> 
> This patch uses public helper connect_to_addr() exported in
> network_helpers.h instead of the local defined function connect_to_server()
> in prog_tests/sk_assign.c. This can avoid duplicate code.
> 
> The code that sets SO_SNDTIMEO timeout as timeo_sec (3s) can be dropped,
> since connect_to_addr() sets default timeout as 3s.

Hi Geliang,

Thank you for this cleanup patch-set!
I think there is a mistake regarding connect_to_addr(),
as it does not set any timeouts (while start_server_addr() does):

-------------------------------------------------------- >8 ---

int connect_to_addr(...)
{
	...
	fd = socket(addr->ss_family, type, 0);
	if (fd < 0) { ... }
	if (connect_fd_to_addr(fd, addr, addrlen, false))
		goto error_close;
	return fd;

error_close:
	...
}

static int connect_fd_to_addr(int fd,
			      const struct sockaddr_storage *addr,
			      socklen_t addrlen, const bool must_fail)
{
	...
	ret = connect(fd, (const struct sockaddr *)addr, addrlen);
	if (must_fail) { ... error handling ... }
    else { ... error handling ... }
	return 0;
}

--- 8< --------------------------------------------------------

Maybe add a flavor like connect_to_addr_opt() with an additional
flag or options parameter?

Thanks,
Eduard

[...]
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign.c b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
index 130aafe8cff6..b574e162ce6e 100644
--- a/tools/testing/selftests/bpf/prog_tests/sk_assign.c
+++ b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
@@ -23,8 +23,6 @@ 
 #define NS_SELF "/proc/self/ns/net"
 #define SERVER_MAP_PATH "/sys/fs/bpf/tc/globals/server_map"
 
-static const struct timeval timeo_sec = { .tv_sec = 3 };
-static const size_t timeo_optlen = sizeof(timeo_sec);
 static int stop, duration;
 
 static bool
@@ -74,28 +72,6 @@  configure_stack(void)
 	return true;
 }
 
-static int
-connect_to_server(const struct sockaddr *addr, socklen_t len, int type)
-{
-	int fd = -1;
-
-	fd = socket(addr->sa_family, type, 0);
-	if (CHECK_FAIL(fd == -1))
-		goto out;
-	if (CHECK_FAIL(setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeo_sec,
-				  timeo_optlen)))
-		goto close_out;
-	if (CHECK_FAIL(connect(fd, addr, len)))
-		goto close_out;
-
-	goto out;
-close_out:
-	close(fd);
-	fd = -1;
-out:
-	return fd;
-}
-
 static in_port_t
 get_port(int fd)
 {
@@ -138,7 +114,7 @@  run_test(int server_fd, const struct sockaddr *addr, socklen_t len, int type)
 	in_port_t port;
 	int ret = 1;
 
-	client = connect_to_server(addr, len, type);
+	client = connect_to_addr((struct sockaddr_storage *)addr, len, type);
 	if (client == -1) {
 		perror("Cannot connect to server");
 		goto out;