Skip to contentSkip to conversation

selftests/bpf: remove duplicated functions

PATCH Superseded [email protected] 3 participants 3 messages Aug 7, 2023 – Aug 8, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: remove duplicated functions

From: Kui-Feng Lee <[email protected]>

The file cgroup_tcp_skb.c contains redundant implementations of the similar functions (create_server_sock_v6() and connect_client_server_v6()) found in network_helpers.c. Let's eliminate these duplicated functions.

---
 .../selftests/bpf/prog_tests/cgroup_tcp_skb.c | 65 ++-----------------
 1 file changed, 7 insertions(+), 58 deletions(-)
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c +7 -58
index d686ef19f705..3085d539608d 100644
@@ -9,6 +9,7 @@
 #include "testing_helpers.h"
 #include "cgroup_tcp_skb.skel.h"
 #include "cgroup_tcp_skb.h"
+#include "network_helpers.h"
 
 #define CGROUP_TCP_SKB_PATH "/test_cgroup_tcp_skb"
 
@@ -58,36 +59,6 @@ static int create_client_sock_v6(void)
 	return fd;
 }
 
-static int create_server_sock_v6(void)
-{
-	struct sockaddr_in6 addr = {
-		.sin6_family = AF_INET6,
-		.sin6_port = htons(0),
-		.sin6_addr = IN6ADDR_LOOPBACK_INIT,
-	};
-	int fd, err;
-
-	fd = socket(AF_INET6, SOCK_STREAM, 0);
-	if (fd < 0) {
-		perror("socket");
-		return -1;
-	}
-
-	err = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
-	if (err < 0) {
-		perror("bind");
-		return -1;
-	}
-
-	err = listen(fd, 1);
-	if (err < 0) {
-		perror("listen");
-		return -1;
-	}
-
-	return fd;
-}
-
 static int get_sock_port_v6(int fd)
 {
 	struct sockaddr_in6 addr;
@@ -104,28 +75,6 @@ static int get_sock_port_v6(int fd)
 	return ntohs(addr.sin6_port);
 }
 
-static int connect_client_server_v6(int client_fd, int listen_fd)
-{
-	struct sockaddr_in6 addr = {
-		.sin6_family = AF_INET6,
-		.sin6_addr = IN6ADDR_LOOPBACK_INIT,
-	};
-	int err, port;
-
-	port = get_sock_port_v6(listen_fd);
-	if (port < 0)
-		return -1;
-	addr.sin6_port = htons(port);
-
-	err = connect(client_fd, (struct sockaddr *)&addr, sizeof(addr));
-	if (err < 0) {
-		perror("connect");
-		return -1;
-	}
-
-	return 0;
-}
-
 /* Connect to the server in a cgroup from the outside of the cgroup. */
 static int talk_to_cgroup(int *client_fd, int *listen_fd, int *service_fd,
 			  struct cgroup_tcp_skb *skel)
@@ -143,14 +92,14 @@ static int talk_to_cgroup(int *client_fd, int *listen_fd, int *service_fd,
 	err = join_cgroup(CGROUP_TCP_SKB_PATH);
 	if (!ASSERT_OK(err, "join_cgroup"))
 		return -1;
-	*listen_fd = create_server_sock_v6();
+	*listen_fd = start_server(AF_INET6, SOCK_STREAM, NULL, 0, 2000);
 	if (!ASSERT_GE(*listen_fd, 0, "listen_fd"))
 		return -1;
 	skel->bss->g_sock_port = get_sock_port_v6(*listen_fd);
 
 	/* Connect client to server */
-	err = connect_client_server_v6(*client_fd, *listen_fd);
-	if (!ASSERT_OK(err, "connect_client_server_v6"))
+	err = connect_fd_to_fd(*client_fd, *listen_fd, 500);
+	if (!ASSERT_OK(err, "connect_fd_to_fd"))
 		return -1;
 	*service_fd = accept(*listen_fd, NULL, NULL);
 	if (!ASSERT_GE(*service_fd, 0, "service_fd"))
@@ -180,7 +129,7 @@ static int talk_to_outside(int *client_fd, int *listen_fd, int *service_fd,
 	err = join_root_cgroup();
 	if (!ASSERT_OK(err, "join_root_cgroup"))
 		return -1;
-	*listen_fd = create_server_sock_v6();
+	*listen_fd = start_server(AF_INET6, SOCK_STREAM, NULL, 0, 2000);
 	if (!ASSERT_GE(*listen_fd, 0, "listen_fd"))
 		return -1;
 	err = join_cgroup(CGROUP_TCP_SKB_PATH);
@@ -195,8 +144,8 @@ static int talk_to_outside(int *client_fd, int *listen_fd, int *service_fd,
 	skel->bss->g_sock_port = get_sock_port_v6(*listen_fd);
 
 	/* Connect client to server */
-	err = connect_client_server_v6(*client_fd, *listen_fd);
-	if (!ASSERT_OK(err, "connect_client_server_v6"))
+	err = connect_fd_to_fd(*client_fd, *listen_fd, 500);
+	if (!ASSERT_OK(err, "connect_fd_to_fd"))
 		return -1;
 	*service_fd = accept(*listen_fd, NULL, NULL);
 	if (!ASSERT_GE(*service_fd, 0, "service_fd"))
Signature · 1 line
2.34.1
#1 lore raw
Quoted 5 lines · On 8/7/23 12:38 PM, [email protected] wrote:

From: Kui-Feng Lee <[email protected]>

The file cgroup_tcp_skb.c contains redundant implementations of the similar functions (create_server_sock_v6() and connect_client_server_v6()) found in network_helpers.c. Let's eliminate these duplicated functions.

How about the port function mentioned in https://lore.kernel.org/bpf/[email protected]/ :

Quoted 2 lines
Quoted 2 lines

There is get_socket_local_port() that supports both v4 and v6 in network_helpers.c which is equivalent to the get_sock_port_v6() here.

#2 lore raw
Quoted 12 lines · Martin KaFai Lau
Quoted 5 lines · On 8/7/23 12:38 PM, [email protected] wrote:

From: Kui-Feng Lee <[email protected]>

The file cgroup_tcp_skb.c contains redundant implementations of the similar functions (create_server_sock_v6() and connect_client_server_v6()) found in network_helpers.c. Let's eliminate these duplicated functions.

How about the port function mentioned in https://lore.kernel.org/bpf/[email protected]/ :

Quoted 2 lines
Quoted 2 lines

There is get_socket_local_port() that supports both v4 and v6 in network_helpers.c which is equivalent to the get_sock_port_v6() here.

Ya, sure!