]> www.infradead.org Git - users/hch/configfs.git/commitdiff
selftests/bpf: Add client_socket helper
authorGeliang Tang <tanggeliang@kylinos.cn>
Fri, 21 Jun 2024 02:16:00 +0000 (10:16 +0800)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 21 Jun 2024 03:42:44 +0000 (20:42 -0700)
This patch extracts a new helper client_socket() from connect_to_fd_opts()
to create the client socket, but don't connect to the server. Then
connect_to_fd_opts() can be implemented using client_socket() and
connect_fd_to_addr(). This helper can be used in connect_to_addr() too,
and make "noconnect" opts useless.

Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
Link: https://lore.kernel.org/r/4169c554e1cee79223feea49a1adc459d55e1ffe.1718932493.git.tanggeliang@kylinos.cn
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/network_helpers.c
tools/testing/selftests/bpf/network_helpers.h

index 5d1b4f165def1d35c9a9c399b04006126f0532eb..5f8214e2880d8438666dc89facb784a845fbf3e5 100644 (file)
@@ -249,6 +249,34 @@ error_close:
        return -1;
 }
 
+int client_socket(int family, int type,
+                 const struct network_helper_opts *opts)
+{
+       int fd;
+
+       if (!opts)
+               opts = &default_opts;
+
+       fd = socket(family, type, opts->proto);
+       if (fd < 0) {
+               log_err("Failed to create client socket");
+               return -1;
+       }
+
+       if (settimeo(fd, opts->timeout_ms))
+               goto error_close;
+
+       if (opts->post_socket_cb &&
+           opts->post_socket_cb(fd, opts->cb_opts))
+               goto error_close;
+
+       return fd;
+
+error_close:
+       save_errno_close(fd);
+       return -1;
+}
+
 static int connect_fd_to_addr(int fd,
                              const struct sockaddr_storage *addr,
                              socklen_t addrlen, const bool must_fail)
@@ -284,19 +312,12 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add
        if (!opts)
                opts = &default_opts;
 
-       fd = socket(addr->ss_family, type, opts->proto);
+       fd = client_socket(addr->ss_family, type, opts);
        if (fd < 0) {
                log_err("Failed to create client socket");
                return -1;
        }
 
-       if (settimeo(fd, opts->timeout_ms))
-               goto error_close;
-
-       if (opts->post_socket_cb &&
-           opts->post_socket_cb(fd, opts->cb_opts))
-               goto error_close;
-
        if (!opts->noconnect)
                if (connect_fd_to_addr(fd, addr, addrlen, opts->must_fail))
                        goto error_close;
index c92bed35dfe2cbc609a93749a461f935097e2e0c..e89eadfb02d6945dbbe297e3cf38b66c11833338 100644 (file)
@@ -57,6 +57,8 @@ int *start_reuseport_server(int family, int type, const char *addr_str,
 int start_server_addr(int type, const struct sockaddr_storage *addr, socklen_t len,
                      const struct network_helper_opts *opts);
 void free_fds(int *fds, unsigned int nr_close_fds);
+int client_socket(int family, int type,
+                 const struct network_helper_opts *opts);
 int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len,
                    const struct network_helper_opts *opts);
 int connect_to_fd(int server_fd, int timeout_ms);