From: Geliang Tang <[email protected]>

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 <[email protected]>
---
 .../bpf/prog_tests/sockmap_helpers.h          | 47 ++++++++-----------
 1 file changed, 19 insertions(+), 28 deletions(-)

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)
-- 
2.43.0


Reply via email to