diff mbox series

[bpf-next,v2,09/14] selftests/bpf: Add function pointer for __start_server

Message ID eedd84f856fe2c9101ab4b7086276e4830370839.1712796967.git.tanggeliang@kylinos.cn (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series use start_server and connect_to helpers | 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, async
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
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: 8 this patch: 8
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 16 of 16 maintainers
netdev/build_clang success Errors and warnings before: 8 this patch: 8
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: 8 this patch: 8
netdev/checkpatch warning WARNING: line length of 95 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
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 Lint
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-18 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc

Commit Message

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

__start_server() sets SO_REUSPORT through setsockopt() when the parameter
'reuseport' is set. This patch makes it more flexible by accepting a
function pointer named setsockopt.

Then the original start_reuseport_server() can be implemented by passing
in a newly defined setsockopt_reuse() function pointer to __start_server().

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

Comments

Eduard Zingerman April 12, 2024, 5:23 p.m. UTC | #1
On Thu, 2024-04-11 at 09:03 +0800, Geliang Tang wrote:


[...]

>  static int __start_server(int type, int protocol, const struct sockaddr *addr,
> -			  socklen_t addrlen, int timeout_ms, bool reuseport)
> +			  socklen_t addrlen, int timeout_ms,
> +			  int *(*setsockopt)(int fd, int val), int val)

There is no need for setsockopt to return pointer to int,
the code could be simplified e.g. as in the patch below (on top of this series):

---

diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c
index f6810bb54edc..30ac03322c61 100644
--- a/tools/testing/selftests/bpf/network_helpers.c
+++ b/tools/testing/selftests/bpf/network_helpers.c
@@ -80,7 +80,7 @@ int settimeo(int fd, int timeout_ms)
 
 static int __start_server(int type, int protocol, const struct sockaddr *addr,
 			  socklen_t addrlen, int timeout_ms,
-			  int *(*setsockopt)(int fd, int val), int val)
+			  int (*setsockopt)(int fd, int val), int val)
 {
 	int fd;
 
@@ -167,7 +167,7 @@ int *start_reuseport_server(int family, int type, const char *addr_str,
 		return NULL;
 
 	fds[0] = __start_server(type, 0, (struct sockaddr *)&addr, addrlen,
-				timeout_ms, (void *)setsockopt_reuse, 1);
+				timeout_ms, setsockopt_reuse, 1);
 	if (fds[0] == -1)
 		goto close_fds;
 	nr_fds = 1;
@@ -177,7 +177,7 @@ int *start_reuseport_server(int family, int type, const char *addr_str,
 
 	for (; nr_fds < nr_listens; nr_fds++) {
 		fds[nr_fds] = __start_server(type, 0, (struct sockaddr *)&addr,
-					     addrlen, timeout_ms, (void *)setsockopt_reuse, 1);
+					     addrlen, timeout_ms, setsockopt_reuse, 1);
 		if (fds[nr_fds] == -1)
 			goto close_fds;
 	}
@@ -195,7 +195,7 @@ int start_server_addr(const struct sockaddr *addr, socklen_t addrlen, int type)
 }
 
 int start_server_setsockopt(const struct sockaddr *addr, socklen_t addrlen, int type,
-			    int *(*setsockopt)(int fd, int val), int val)
+			    int (*setsockopt)(int fd, int val), int val)
 {
 	return __start_server(type, 0, addr, addrlen, 0, setsockopt, val);
 }
diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h
index 543295230062..0f0f1803e0c8 100644
--- a/tools/testing/selftests/bpf/network_helpers.h
+++ b/tools/testing/selftests/bpf/network_helpers.h
@@ -56,7 +56,7 @@ int *start_reuseport_server(int family, int type, const char *addr_str,
 			    unsigned int nr_listens);
 int start_server_addr(const struct sockaddr *addr, socklen_t addrlen, int type);
 int start_server_setsockopt(const struct sockaddr *addr, socklen_t addrlen, int type,
-			    int *(*setsockopt)(int fd, int val), int val);
+			    int (*setsockopt)(int fd, int val), int val);
 void free_fds(int *fds, unsigned int nr_close_fds);
 int connect_to_addr(const struct sockaddr_storage *addr, socklen_t len, int type);
 int connect_to_fd(int server_fd, int timeout_ms);
diff --git a/tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c b/tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c
index 4fee4b343255..0c39170d543a 100644
--- a/tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c
+++ b/tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c
@@ -97,7 +97,7 @@ static int start_server_lo(void)
 	if (make_sockaddr(AF_INET, "127.0.0.1", 0, &addr, &addrlen))
 		return -1;
 	return start_server_setsockopt((struct sockaddr *)&addr, addrlen,
-				       SOCK_STREAM, (void *)setsockopt_loop, 0);
+				       SOCK_STREAM, setsockopt_loop, 0);
 }
 
 static void run_test(int cgroup_fd)
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c
index 91b014784dd9..2a389e8c2503 100644
--- a/tools/testing/selftests/bpf/network_helpers.c
+++ b/tools/testing/selftests/bpf/network_helpers.c
@@ -79,9 +79,9 @@  int settimeo(int fd, int timeout_ms)
 #define save_errno_close(fd) ({ int __save = errno; close(fd); errno = __save; })
 
 static int __start_server(int type, int protocol, const struct sockaddr *addr,
-			  socklen_t addrlen, int timeout_ms, bool reuseport)
+			  socklen_t addrlen, int timeout_ms,
+			  int *(*setsockopt)(int fd, int val), int val)
 {
-	int on = 1;
 	int fd;
 
 	fd = socket(addr->sa_family, type, protocol);
@@ -93,9 +93,8 @@  static int __start_server(int type, int protocol, const struct sockaddr *addr,
 	if (settimeo(fd, timeout_ms))
 		goto error_close;
 
-	if (reuseport &&
-	    setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on))) {
-		log_err("Failed to set SO_REUSEPORT");
+	if (setsockopt && setsockopt(fd, val)) {
+		log_err("Failed to set sockopt");
 		goto error_close;
 	}
 
@@ -128,7 +127,7 @@  static int start_server_proto(int family, int type, int protocol,
 		return -1;
 
 	return __start_server(type, protocol, (struct sockaddr *)&addr,
-			      addrlen, timeout_ms, false);
+			      addrlen, timeout_ms, NULL, 0);
 }
 
 int start_server(int family, int type, const char *addr_str, __u16 port,
@@ -144,6 +143,11 @@  int start_mptcp_server(int family, const char *addr_str, __u16 port,
 				  port, timeout_ms);
 }
 
+static int setsockopt_reuse(int fd, int on)
+{
+	return setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on));
+}
+
 int *start_reuseport_server(int family, int type, const char *addr_str,
 			    __u16 port, int timeout_ms, unsigned int nr_listens)
 {
@@ -163,7 +167,7 @@  int *start_reuseport_server(int family, int type, const char *addr_str,
 		return NULL;
 
 	fds[0] = __start_server(type, 0, (struct sockaddr *)&addr, addrlen,
-				timeout_ms, true);
+				timeout_ms, (void *)setsockopt_reuse, 1);
 	if (fds[0] == -1)
 		goto close_fds;
 	nr_fds = 1;
@@ -173,7 +177,7 @@  int *start_reuseport_server(int family, int type, const char *addr_str,
 
 	for (; nr_fds < nr_listens; nr_fds++) {
 		fds[nr_fds] = __start_server(type, 0, (struct sockaddr *)&addr,
-					     addrlen, timeout_ms, true);
+					     addrlen, timeout_ms, (void *)setsockopt_reuse, 1);
 		if (fds[nr_fds] == -1)
 			goto close_fds;
 	}
@@ -187,7 +191,7 @@  int *start_reuseport_server(int family, int type, const char *addr_str,
 
 int start_server_addr(const struct sockaddr *addr, socklen_t addrlen, int type)
 {
-	return __start_server(type, 0, addr, addrlen, 0, 0);
+	return __start_server(type, 0, addr, addrlen, 0, NULL, 0);
 }
 
 void free_fds(int *fds, unsigned int nr_close_fds)