]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
selftests/bpf: Add connect_to_addr_str helper
authorGeliang Tang <tanggeliang@kylinos.cn>
Thu, 18 Jul 2024 06:22:31 +0000 (14:22 +0800)
committerAndrii Nakryiko <andrii@kernel.org>
Mon, 29 Jul 2024 19:52:51 +0000 (12:52 -0700)
Similar to connect_to_addr() helper for connecting to a server with the
given sockaddr_storage type address, this patch adds a new helper named
connect_to_addr_str() for connecting to a server with the given string
type address "addr_str", together with its "family" and "port" as other
parameters of connect_to_addr_str().

In connect_to_addr_str(), the parameters "family", "addr_str" and "port"
are used to create a sockaddr_storage type address "addr" by invoking
make_sockaddr(). Then pass this "addr" together with "addrlen", "type"
and "opts" to connect_to_addr().

Suggested-by: Martin KaFai Lau <martin.lau@kernel.org>
Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
Link: https://lore.kernel.org/r/647e82170831558dbde132a7a3d86df660dba2c4.1721282219.git.tanggeliang@kylinos.cn
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
tools/testing/selftests/bpf/network_helpers.c
tools/testing/selftests/bpf/network_helpers.h

index bc4947afa0b4a6d191fd560c83365de3fe8466e3..9c98a60cf1e2692562f6c0caabc7e922e9313a05 100644 (file)
@@ -300,6 +300,21 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add
        return fd;
 }
 
+int connect_to_addr_str(int family, int type, const char *addr_str, __u16 port,
+                       const struct network_helper_opts *opts)
+{
+       struct sockaddr_storage addr;
+       socklen_t addrlen;
+
+       if (!opts)
+               opts = &default_opts;
+
+       if (make_sockaddr(family, addr_str, port, &addr, &addrlen))
+               return -1;
+
+       return connect_to_addr(type, &addr, addrlen, opts);
+}
+
 int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
 {
        struct sockaddr_storage addr;
index f39eeb5a4594889976a7a90d5916a601397050c5..cce56955371f5e69047314af52d8540840b45eb1 100644 (file)
@@ -69,6 +69,8 @@ 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_addr_str(int family, int type, const char *addr_str, __u16 port,
+                       const struct network_helper_opts *opts);
 int connect_to_fd(int server_fd, int timeout_ms);
 int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts);
 int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms);