diff mbox series

[bpf-next,v2] selftests/bpf: remove duplicated functions

Message ID 20230808162858.326871-1-thinker.li@gmail.com (mailing list archive)
State Accepted
Commit 61e4c165fb8d9a9435f9a710c2a85bae3d2869f9
Delegated to: BPF
Headers show
Series [bpf-next,v2] selftests/bpf: remove duplicated functions | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-30 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-31 success Logs for veristat
netdev/series_format success Single patches do not need cover letters
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 10 maintainers not CCed: daniel@iogearbox.net kpsingh@kernel.org john.fastabend@gmail.com sdf@google.com shuah@kernel.org yonghong.song@linux.dev mykolal@fb.com linux-kselftest@vger.kernel.org jolsa@kernel.org haoluo@google.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 WARNING: Patch version information should be after the --- line
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-9 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-29 success Logs for veristat
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 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 aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 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 gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ${{ matrix.test }} on ${{ matrix.arch }} with ${{ matrix.toolchain_full }}
bpf/vmtest-bpf-next-VM_Test-2 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 fail Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-7 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-8 success Logs for veristat

Commit Message

Kui-Feng Lee Aug. 8, 2023, 4:28 p.m. UTC
From: Kui-Feng Lee <thinker.li@gmail.com>

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

Changes from v1:

 - Remove get_sock_port_v6() as well.

v1: https://lore.kernel.org/all/20230807193840.567962-1-thinker.li@gmail.com/

Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
---
 .../selftests/bpf/prog_tests/cgroup_tcp_skb.c | 93 ++++---------------
 1 file changed, 17 insertions(+), 76 deletions(-)

Comments

patchwork-bot+netdevbpf@kernel.org Aug. 9, 2023, midnight UTC | #1
Hello:

This patch was applied to bpf/bpf-next.git (master)
by Martin KaFai Lau <martin.lau@kernel.org>:

On Tue,  8 Aug 2023 09:28:58 -0700 you wrote:
> From: Kui-Feng Lee <thinker.li@gmail.com>
> 
> The file cgroup_tcp_skb.c contains redundant implementations of the similar
> functions (create_server_sock_v6(), connect_client_server_v6() and
> get_sock_port_v6()) found in network_helpers.c. Let's eliminate these
> duplicated functions.
> 
> [...]

Here is the summary with links:
  - [bpf-next,v2] selftests/bpf: remove duplicated functions
    https://git.kernel.org/bpf/bpf-next/c/61e4c165fb8d

You are awesome, thank you!
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c b/tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c
index d686ef19f705..3353c42bc2be 100644
--- a/tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c
+++ b/tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c
@@ -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,80 +59,13 @@  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;
-	socklen_t len;
-	int err;
-
-	len = sizeof(addr);
-	err = getsockname(fd, (struct sockaddr *)&addr, &len);
-	if (err < 0) {
-		perror("getsockname");
-		return -1;
-	}
-
-	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)
 {
 	int err, cp;
 	char buf[5];
+	int port;
 
 	/* Create client & server socket */
 	err = join_root_cgroup();
@@ -143,14 +77,17 @@  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);
+	port = get_socket_local_port(*listen_fd);
+	if (!ASSERT_GE(port, 0, "get_socket_local_port"))
+		return -1;
+	skel->bss->g_sock_port = ntohs(port);
 
 	/* 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"))
@@ -175,12 +112,13 @@  static int talk_to_outside(int *client_fd, int *listen_fd, int *service_fd,
 {
 	int err, cp;
 	char buf[5];
+	int port;
 
 	/* Create client & server socket */
 	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);
@@ -192,11 +130,14 @@  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;
-	skel->bss->g_sock_port = get_sock_port_v6(*listen_fd);
+	port = get_socket_local_port(*listen_fd);
+	if (!ASSERT_GE(port, 0, "get_socket_local_port"))
+		return -1;
+	skel->bss->g_sock_port = ntohs(port);
 
 	/* 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"))