selftests/bpf: Add start_server_addr helper
authorGeliang Tang <tanggeliang@kylinos.cn>
Thu, 18 Apr 2024 08:09:07 +0000 (16:09 +0800)
committerMartin KaFai Lau <martin.lau@kernel.org>
Sat, 20 Apr 2024 00:13:28 +0000 (17:13 -0700)
In order to pair up with connect_to_addr(), this patch adds a new helper
start_server_addr(), which is a wrapper of __start_server(). It accepts
an argument 'addr' of 'struct sockaddr_storage' type instead of a string
type argument like start_server(), and a network_helper_opts argument as
the last one.

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
Link: https://lore.kernel.org/r/2f01d48fa026467926738debe554ac452c19b86f.1713427236.git.tanggeliang@kylinos.cn
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
tools/testing/selftests/bpf/network_helpers.c
tools/testing/selftests/bpf/network_helpers.h

index dc1fd7af9c7a3af5ae11c81016cbbe6c7c6ebfb4..28fe8367451b00a6c514d9e08416a44c12c5107f 100644 (file)
@@ -52,6 +52,8 @@ struct ipv6_packet pkt_v6 = {
        .tcp.doff = 5,
 };
 
+static const struct network_helper_opts default_opts;
+
 int settimeo(int fd, int timeout_ms)
 {
        struct timeval timeout = { .tv_sec = 3 };
@@ -185,6 +187,16 @@ close_fds:
        return NULL;
 }
 
+int start_server_addr(int type, const struct sockaddr_storage *addr, socklen_t len,
+                     const struct network_helper_opts *opts)
+{
+       if (!opts)
+               opts = &default_opts;
+
+       return __start_server(type, 0, (struct sockaddr *)addr, len,
+                             opts->timeout_ms, 0);
+}
+
 void free_fds(int *fds, unsigned int nr_close_fds)
 {
        if (fds) {
@@ -278,8 +290,6 @@ error_close:
        return -1;
 }
 
-static const struct network_helper_opts default_opts;
-
 int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
 {
        struct sockaddr_storage addr;
index 70f4e4c927335ea9bd1589ebecffc0830ade8946..414ea50bb3fcd972fce0648823585c56c54b379c 100644 (file)
@@ -53,6 +53,8 @@ int start_mptcp_server(int family, const char *addr, __u16 port,
 int *start_reuseport_server(int family, int type, const char *addr_str,
                            __u16 port, int timeout_ms,
                            unsigned int nr_listens);
+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 connect_to_addr(const struct sockaddr_storage *addr, socklen_t len, int type);
 int connect_to_fd(int server_fd, int timeout_ms);