diff mbox series

[bpf-next,15/15] selftests/bpf: Use start_server_str in sockmap_helpers

Message ID 2de2cd7a23bcd02d4711ec66d3b3a77c5632d1e7.1721771340.git.tanggeliang@kylinos.cn
State New
Headers show
Series [bpf-next,01/15] selftests/bpf: Use start_server_str in skc_to_unix_sock | expand

Commit Message

Geliang Tang July 23, 2024, 10:22 p.m. UTC
From: Geliang Tang <tanggeliang@kylinos.cn>

Now both SOCK_SEQPACKET and SOCK_NONBLOCK can be passed as "type" parameter
into start_server_str(), use this helper in socket_loopback_reuseport() in
sockmap_helpers.h to simplify the code.

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 .../bpf/prog_tests/sockmap_helpers.h          | 47 ++++++++-----------
 1 file changed, 19 insertions(+), 28 deletions(-)
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h b/tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h
index 0fe095497f34..1e681e71e9ad 100644
--- a/tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h
+++ b/tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h
@@ -310,8 +310,14 @@  static inline int create_socket_pairs(int s, int family, int sotype,
 	return err;
 }
 
-static inline int enable_reuseport(int s, int progfd)
+struct cb_opts {
+	int progfd;
+};
+
+static int enable_reuseport(int s, void *opts)
 {
+	struct cb_opts *co = (struct cb_opts *)opts;
+	int progfd = co->progfd;
 	int err, one = 1;
 
 	err = xsetsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
@@ -327,34 +333,19 @@  static inline int enable_reuseport(int s, int progfd)
 
 static inline int socket_loopback_reuseport(int family, int sotype, int progfd)
 {
-	struct sockaddr_storage addr;
-	socklen_t len = 0;
-	int err, s;
-
-	init_addr_loopback(family, &addr, &len);
-
-	s = xsocket(family, sotype, 0);
-	if (s == -1)
-		return -1;
-
-	if (progfd >= 0)
-		enable_reuseport(s, progfd);
-
-	err = xbind(s, sockaddr(&addr), len);
-	if (err)
-		goto close;
-
-	if (sotype & SOCK_DGRAM)
-		return s;
-
-	err = xlisten(s, SOMAXCONN);
-	if (err)
-		goto close;
+	struct cb_opts cb_opts = {
+		.progfd = progfd,
+	};
+	struct network_helper_opts opts = {
+		.backlog = SOMAXCONN,
+	};
+
+	if (progfd >= 0) {
+		opts.post_socket_cb = enable_reuseport;
+		opts.cb_opts = &cb_opts;
+	}
 
-	return s;
-close:
-	xclose(s);
-	return -1;
+	return start_server_str(family, sotype, loopback_addr_str(family), 0, &opts);
 }
 
 static inline int socket_loopback(int family, int sotype)