From: Geliang Tang tanggeliang@kylinos.cn
This patch set uses network helpers in sockmap_ktls and sk_lookup, and drop three local helpers tcp_server(), inetaddr_len() and make_socket() in them.
Geliang Tang (6): selftests/bpf: Use start_server in sockmap_ktls selftests/bpf: Use connect_to_fd in sockmap_ktls selftests/bpf: Use start_server_str in sk_lookup selftests/bpf: Drop inetaddr_len in sk_lookup selftests/bpf: Use connect_to_fd in sk_lookup selftests/bpf: Drop make_socket in sk_lookup
.../selftests/bpf/prog_tests/sk_lookup.c | 144 ++++++------------ .../selftests/bpf/prog_tests/sockmap_ktls.c | 32 +--- 2 files changed, 53 insertions(+), 123 deletions(-)
From: Geliang Tang tanggeliang@kylinos.cn
Include network_helpers.h in prog_tests/sockmap_ktls.c, use public network helper start_server() instead of the local defined function tcp_server(). This can avoid duplicate code.
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn --- .../selftests/bpf/prog_tests/sockmap_ktls.c | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c index 2d0796314862..9cc881b38669 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c @@ -6,25 +6,11 @@
#include <netinet/tcp.h> #include "test_progs.h" +#include "network_helpers.h"
#define MAX_TEST_NAME 80 #define TCP_ULP 31
-static int tcp_server(int family) -{ - int err, s; - - s = socket(family, SOCK_STREAM, 0); - if (!ASSERT_GE(s, 0, "socket")) - return -1; - - err = listen(s, SOMAXCONN); - if (!ASSERT_OK(err, "listen")) - return -1; - - return s; -} - static int disconnect(int fd) { struct sockaddr unspec = { AF_UNSPEC }; @@ -39,7 +25,7 @@ static void test_sockmap_ktls_disconnect_after_delete(int family, int map) socklen_t len = sizeof(addr); int err, cli, srv, zero = 0;
- srv = tcp_server(family); + srv = start_server(family, SOCK_STREAM, NULL, 0, 0); if (srv == -1) return;
On Fri, 2024-06-21 at 18:35 +0800, Geliang Tang wrote:
From: Geliang Tang tanggeliang@kylinos.cn
Include network_helpers.h in prog_tests/sockmap_ktls.c, use public network helper start_server() instead of the local defined function tcp_server(). This can avoid duplicate code.
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn
Technically, this is not a one-for-one replacement, as start_server() also does bind() and it's listen backlog is 1, not SOMAXCONN. Looking at the original commit, the difference does not seem to matter.
Acked-by: Eduard Zingerman eddyz87@gmail.com
[...]
From: Geliang Tang tanggeliang@kylinos.cn
Use public network helper connect_to_fd() instead of open-coding it in prog_tests/sockmap_ktls.c. This can avoid duplicate code.
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn --- .../selftests/bpf/prog_tests/sockmap_ktls.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c index 9cc881b38669..68867028c432 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c @@ -21,26 +21,16 @@ static int disconnect(int fd) /* Disconnect (unhash) a kTLS socket after removing it from sockmap. */ static void test_sockmap_ktls_disconnect_after_delete(int family, int map) { - struct sockaddr_storage addr = {0}; - socklen_t len = sizeof(addr); int err, cli, srv, zero = 0;
srv = start_server(family, SOCK_STREAM, NULL, 0, 0); if (srv == -1) return;
- err = getsockname(srv, (struct sockaddr *)&addr, &len); - if (!ASSERT_OK(err, "getsockopt")) - goto close_srv; - - cli = socket(family, SOCK_STREAM, 0); - if (!ASSERT_GE(cli, 0, "socket")) + cli = connect_to_fd(srv, 0); + if (!ASSERT_GE(cli, 0, "connect_to_fd")) goto close_srv;
- err = connect(cli, (struct sockaddr *)&addr, len); - if (!ASSERT_OK(err, "connect")) - goto close_cli; - err = bpf_map_update_elem(map, &zero, &cli, 0); if (!ASSERT_OK(err, "bpf_map_update_elem")) goto close_cli;
On Fri, 2024-06-21 at 18:35 +0800, Geliang Tang wrote:
From: Geliang Tang tanggeliang@kylinos.cn
Use public network helper connect_to_fd() instead of open-coding it in prog_tests/sockmap_ktls.c. This can avoid duplicate code.
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn
Acked-by: Eduard Zingerman eddyz87@gmail.com
[...]
From: Geliang Tang tanggeliang@kylinos.cn
This patch uses public helper start_server_addr() in make_server() in prog_tests/sk_lookup.c to avoid duplicate code.
Add a helper setsockopts() to set SOL_CUSTOM sockopt looply, set it to setsockopt pointer of struct network_helper_opts, and pass it to start_server_addr().
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn --- .../selftests/bpf/prog_tests/sk_lookup.c | 57 +++++++++++-------- 1 file changed, 33 insertions(+), 24 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index 597d0467a926..ba73b56f4484 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -77,6 +77,12 @@ struct test { bool reuseport_has_conns; /* Add a connected socket to reuseport group */ };
+struct cb_opts { + int family; + int sotype; + bool reuseport; +}; + static __u32 duration; /* for CHECK macro */
static bool is_ipv6(const char *ip) @@ -142,19 +148,14 @@ static int make_socket(int sotype, const char *ip, int port, return fd; }
-static int make_server(int sotype, const char *ip, int port, - struct bpf_program *reuseport_prog) +static int setsockopts(int fd, void *opts) { - struct sockaddr_storage addr = {0}; + struct cb_opts *co = (struct cb_opts *)opts; const int one = 1; - int err, fd = -1; - - fd = make_socket(sotype, ip, port, &addr); - if (fd < 0) - return -1; + int err = 0;
/* Enabled for UDPv6 sockets for IPv4-mapped IPv6 to work. */ - if (sotype == SOCK_DGRAM) { + if (co->sotype == SOCK_DGRAM) { err = setsockopt(fd, SOL_IP, IP_RECVORIGDSTADDR, &one, sizeof(one)); if (CHECK(err, "setsockopt(IP_RECVORIGDSTADDR)", "failed\n")) { @@ -163,7 +164,7 @@ static int make_server(int sotype, const char *ip, int port, } }
- if (sotype == SOCK_DGRAM && addr.ss_family == AF_INET6) { + if (co->sotype == SOCK_DGRAM && co->family == AF_INET6) { err = setsockopt(fd, SOL_IPV6, IPV6_RECVORIGDSTADDR, &one, sizeof(one)); if (CHECK(err, "setsockopt(IPV6_RECVORIGDSTADDR)", "failed\n")) { @@ -172,7 +173,7 @@ static int make_server(int sotype, const char *ip, int port, } }
- if (sotype == SOCK_STREAM) { + if (co->sotype == SOCK_STREAM) { err = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); if (CHECK(err, "setsockopt(SO_REUSEADDR)", "failed\n")) { @@ -181,7 +182,7 @@ static int make_server(int sotype, const char *ip, int port, } }
- if (reuseport_prog) { + if (co->reuseport) { err = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); if (CHECK(err, "setsockopt(SO_REUSEPORT)", "failed\n")) { @@ -190,19 +191,27 @@ static int make_server(int sotype, const char *ip, int port, } }
- err = bind(fd, (void *)&addr, inetaddr_len(&addr)); - if (CHECK(err, "bind", "failed\n")) { - log_err("failed to bind listen socket"); - goto fail; - } +fail: + return err; +}
- if (sotype == SOCK_STREAM) { - err = listen(fd, SOMAXCONN); - if (CHECK(err, "make_server", "listen")) { - log_err("failed to listen on port %d", port); - goto fail; - } - } +static int make_server(int sotype, const char *ip, int port, + struct bpf_program *reuseport_prog) +{ + struct cb_opts cb_opts = { + .family = is_ipv6(ip) ? AF_INET6 : AF_INET, + .sotype = sotype, + .reuseport = reuseport_prog, + }; + struct network_helper_opts opts = { + .post_socket_cb = setsockopts, + .cb_opts = &cb_opts, + }; + int err, fd = -1; + + fd = start_server_str(cb_opts.family, sotype, ip, port, &opts); + if (CHECK(fd < 0, "start_server_addr", "failed\n")) + return -1;
/* Late attach reuseport prog so we can have one init path */ if (reuseport_prog) {
On Fri, 2024-06-21 at 18:35 +0800, Geliang Tang wrote:
From: Geliang Tang tanggeliang@kylinos.cn
This patch uses public helper start_server_addr() in make_server() in prog_tests/sk_lookup.c to avoid duplicate code.
Add a helper setsockopts() to set SOL_CUSTOM sockopt looply, set it to setsockopt pointer of struct network_helper_opts, and pass it to start_server_addr().
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn
Acked-by: Eduard Zingerman eddyz87@gmail.com
[...]
From: Geliang Tang tanggeliang@kylinos.cn
This patch adds a new parameter "len" for make_socket() to get the length of the address by make_sockaddr(), then local helper inetaddr_len() can be dropped.
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn --- .../selftests/bpf/prog_tests/sk_lookup.c | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index ba73b56f4484..2fc2b928e168 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -108,20 +108,14 @@ static int attach_reuseport(int sock_fd, struct bpf_program *reuseport_prog) return 0; }
-static socklen_t inetaddr_len(const struct sockaddr_storage *addr) -{ - return (addr->ss_family == AF_INET ? sizeof(struct sockaddr_in) : - addr->ss_family == AF_INET6 ? sizeof(struct sockaddr_in6) : 0); -} - static int make_socket(int sotype, const char *ip, int port, - struct sockaddr_storage *addr) + struct sockaddr_storage *addr, socklen_t *len) { struct timeval timeo = { .tv_sec = IO_TIMEOUT_SEC }; int err, family, fd;
family = is_ipv6(ip) ? AF_INET6 : AF_INET; - err = make_sockaddr(family, ip, port, addr, NULL); + err = make_sockaddr(family, ip, port, addr, len); if (CHECK(err, "make_address", "failed\n")) return -1;
@@ -231,13 +225,14 @@ static int make_server(int sotype, const char *ip, int port, static int make_client(int sotype, const char *ip, int port) { struct sockaddr_storage addr = {0}; + socklen_t len; int err, fd;
- fd = make_socket(sotype, ip, port, &addr); + fd = make_socket(sotype, ip, port, &addr, &len); if (fd < 0) return -1;
- err = connect(fd, (void *)&addr, inetaddr_len(&addr)); + err = connect(fd, (void *)&addr, len); if (CHECK(err, "make_client", "connect")) { log_err("failed to connect client socket"); goto fail; @@ -876,6 +871,7 @@ static void drop_on_lookup(const struct test *t) struct sockaddr_storage dst = {}; int client_fd, server_fd, err; struct bpf_link *lookup_link; + socklen_t len; ssize_t n;
lookup_link = attach_lookup_prog(t->lookup_prog); @@ -888,11 +884,11 @@ static void drop_on_lookup(const struct test *t) goto detach;
client_fd = make_socket(t->sotype, t->connect_to.ip, - t->connect_to.port, &dst); + t->connect_to.port, &dst, &len); if (client_fd < 0) goto close_srv;
- err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); + err = connect(client_fd, (void *)&dst, len); if (t->sotype == SOCK_DGRAM) { err = send_byte(client_fd); if (err) @@ -990,6 +986,7 @@ static void drop_on_reuseport(const struct test *t) struct sockaddr_storage dst = { 0 }; int client, server1, server2, err; struct bpf_link *lookup_link; + socklen_t len; ssize_t n;
lookup_link = attach_lookup_prog(t->lookup_prog); @@ -1012,11 +1009,11 @@ static void drop_on_reuseport(const struct test *t) goto close_srv1;
client = make_socket(t->sotype, t->connect_to.ip, - t->connect_to.port, &dst); + t->connect_to.port, &dst, &len); if (client < 0) goto close_srv2;
- err = connect(client, (void *)&dst, inetaddr_len(&dst)); + err = connect(client, (void *)&dst, len); if (t->sotype == SOCK_DGRAM) { err = send_byte(client); if (err) @@ -1230,6 +1227,7 @@ static void run_multi_prog_lookup(const struct test_multi_prog *t) int map_fd, server_fd, client_fd; struct bpf_link *link1, *link2; int prog_idx, done, err; + socklen_t len;
map_fd = bpf_map__fd(t->run_map);
@@ -1259,11 +1257,11 @@ static void run_multi_prog_lookup(const struct test_multi_prog *t) if (err) goto out_close_server;
- client_fd = make_socket(SOCK_STREAM, EXT_IP4, EXT_PORT, &dst); + client_fd = make_socket(SOCK_STREAM, EXT_IP4, EXT_PORT, &dst, &len); if (client_fd < 0) goto out_close_server;
- err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); + err = connect(client_fd, (void *)&dst, len); if (CHECK(err && !t->expect_errno, "connect", "unexpected error %d\n", errno)) goto out_close_client;
From: Geliang Tang tanggeliang@kylinos.cn
This patch uses public helper connect_to_fd() exported in network_helpers.h instead of using make_socket() and connect() in prog_tests/sk_lookup.c. This can simplify the code.
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn --- .../selftests/bpf/prog_tests/sk_lookup.c | 33 +++++-------------- 1 file changed, 9 insertions(+), 24 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index 2fc2b928e168..0f2e8a4c1524 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -868,10 +868,8 @@ static void test_redirect_lookup(struct test_sk_lookup *skel)
static void drop_on_lookup(const struct test *t) { - struct sockaddr_storage dst = {}; int client_fd, server_fd, err; struct bpf_link *lookup_link; - socklen_t len; ssize_t n;
lookup_link = attach_lookup_prog(t->lookup_prog); @@ -883,12 +881,10 @@ static void drop_on_lookup(const struct test *t) if (server_fd < 0) goto detach;
- client_fd = make_socket(t->sotype, t->connect_to.ip, - t->connect_to.port, &dst, &len); + client_fd = connect_to_fd(server_fd, 0); if (client_fd < 0) goto close_srv;
- err = connect(client_fd, (void *)&dst, len); if (t->sotype == SOCK_DGRAM) { err = send_byte(client_fd); if (err) @@ -983,10 +979,8 @@ static void test_drop_on_lookup(struct test_sk_lookup *skel)
static void drop_on_reuseport(const struct test *t) { - struct sockaddr_storage dst = { 0 }; int client, server1, server2, err; struct bpf_link *lookup_link; - socklen_t len; ssize_t n;
lookup_link = attach_lookup_prog(t->lookup_prog); @@ -1008,12 +1002,10 @@ static void drop_on_reuseport(const struct test *t) if (server2 < 0) goto close_srv1;
- client = make_socket(t->sotype, t->connect_to.ip, - t->connect_to.port, &dst, &len); + client = connect_to_fd(server2, 0); if (client < 0) goto close_srv2;
- err = connect(client, (void *)&dst, len); if (t->sotype == SOCK_DGRAM) { err = send_byte(client); if (err) @@ -1160,7 +1152,7 @@ static void run_sk_assign_connected(struct test_sk_lookup *skel, if (server_fd < 0) return;
- connected_fd = make_client(sotype, EXT_IP4, EXT_PORT); + connected_fd = connect_to_fd(server_fd, 0); if (connected_fd < 0) goto out_close_server;
@@ -1174,7 +1166,7 @@ static void run_sk_assign_connected(struct test_sk_lookup *skel, goto out_close_connected;
/* Try to redirect TCP SYN / UDP packet to a connected socket */ - client_fd = make_client(sotype, EXT_IP4, EXT_PORT); + client_fd = connect_to_fd(server_fd, 0); if (client_fd < 0) goto out_unlink_prog; if (sotype == SOCK_DGRAM) { @@ -1223,11 +1215,9 @@ struct test_multi_prog {
static void run_multi_prog_lookup(const struct test_multi_prog *t) { - struct sockaddr_storage dst = {}; int map_fd, server_fd, client_fd; struct bpf_link *link1, *link2; int prog_idx, done, err; - socklen_t len;
map_fd = bpf_map__fd(t->run_map);
@@ -1257,17 +1247,13 @@ static void run_multi_prog_lookup(const struct test_multi_prog *t) if (err) goto out_close_server;
- client_fd = make_socket(SOCK_STREAM, EXT_IP4, EXT_PORT, &dst, &len); - if (client_fd < 0) - goto out_close_server; - - err = connect(client_fd, (void *)&dst, len); - if (CHECK(err && !t->expect_errno, "connect", + client_fd = connect_to_fd(server_fd, 0); + if (CHECK(client_fd < 0 && !t->expect_errno, "connect", "unexpected error %d\n", errno)) - goto out_close_client; - if (CHECK(err && t->expect_errno && errno != t->expect_errno, + goto out_close_server; + if (CHECK(client_fd < 0 && t->expect_errno && errno != t->expect_errno, "connect", "unexpected error %d\n", errno)) - goto out_close_client; + goto out_close_server;
done = 0; prog_idx = PROG1; @@ -1281,7 +1267,6 @@ static void run_multi_prog_lookup(const struct test_multi_prog *t) CHECK(err, "bpf_map_lookup_elem", "failed\n"); CHECK(!done, "bpf_map_lookup_elem", "PROG2 !done\n");
-out_close_client: close(client_fd); out_close_server: close(server_fd);
On Fri, 2024-06-21 at 18:35 +0800, Geliang Tang wrote:
[...]
@@ -883,12 +881,10 @@ static void drop_on_lookup(const struct test *t) if (server_fd < 0) goto detach;
- client_fd = make_socket(t->sotype, t->connect_to.ip,
t->connect_to.port, &dst, &len);
- client_fd = connect_to_fd(server_fd, 0);
Nit: make_socket() used timeout { .tv_sec = IO_TIMEOUT_SEC }, while here commect_to_fd is called with timeout disabled. Same applies to other replacements below.
if (client_fd < 0) goto close_srv;
- err = connect(client_fd, (void *)&dst, len); if (t->sotype == SOCK_DGRAM) { err = send_byte(client_fd); if (err)
[...]
@@ -1257,17 +1247,13 @@ static void run_multi_prog_lookup(const struct test_multi_prog *t) if (err) goto out_close_server;
- client_fd = make_socket(SOCK_STREAM, EXT_IP4, EXT_PORT, &dst, &len);
- if (client_fd < 0)
goto out_close_server;
- err = connect(client_fd, (void *)&dst, len);
- if (CHECK(err && !t->expect_errno, "connect",
- client_fd = connect_to_fd(server_fd, 0);
I'm not sure this is correct. The original test uses EXT_IP4, EXT_PORT here, while t->listen_at could be both { EXT_IP4, EXT_PORT } and { INT_IP4, INT_PORT }. See test_multi_prog_lookup().
- if (CHECK(client_fd < 0 && !t->expect_errno, "connect", "unexpected error %d\n", errno))
goto out_close_client;
- if (CHECK(err && t->expect_errno && errno != t->expect_errno,
goto out_close_server;
- if (CHECK(client_fd < 0 && t->expect_errno && errno != t->expect_errno, "connect", "unexpected error %d\n", errno))
goto out_close_client;
goto out_close_server;
done = 0; prog_idx = PROG1;
[...]
From: Geliang Tang tanggeliang@kylinos.cn
Use public network helpers make_sockaddr() and connect_to_addr() instead of using the local defined function make_socket() and connect().
Then make_socket() can be dropped.
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn --- .../selftests/bpf/prog_tests/sk_lookup.c | 50 +++---------------- 1 file changed, 6 insertions(+), 44 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index 0f2e8a4c1524..e4c983a00511 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -47,8 +47,6 @@ #define INT_IP6 "fd00::2" #define INT_PORT 8008
-#define IO_TIMEOUT_SEC 3 - enum server { SERVER_A = 0, SERVER_B = 1, @@ -108,40 +106,6 @@ static int attach_reuseport(int sock_fd, struct bpf_program *reuseport_prog) return 0; }
-static int make_socket(int sotype, const char *ip, int port, - struct sockaddr_storage *addr, socklen_t *len) -{ - struct timeval timeo = { .tv_sec = IO_TIMEOUT_SEC }; - int err, family, fd; - - family = is_ipv6(ip) ? AF_INET6 : AF_INET; - err = make_sockaddr(family, ip, port, addr, len); - if (CHECK(err, "make_address", "failed\n")) - return -1; - - fd = socket(addr->ss_family, sotype, 0); - if (CHECK(fd < 0, "socket", "failed\n")) { - log_err("failed to make socket"); - return -1; - } - - err = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)); - if (CHECK(err, "setsockopt(SO_SNDTIMEO)", "failed\n")) { - log_err("failed to set SNDTIMEO"); - close(fd); - return -1; - } - - err = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo)); - if (CHECK(err, "setsockopt(SO_RCVTIMEO)", "failed\n")) { - log_err("failed to set RCVTIMEO"); - close(fd); - return -1; - } - - return fd; -} - static int setsockopts(int fd, void *opts) { struct cb_opts *co = (struct cb_opts *)opts; @@ -224,24 +188,22 @@ static int make_server(int sotype, const char *ip, int port,
static int make_client(int sotype, const char *ip, int port) { + int family = is_ipv6(ip) ? AF_INET6 : AF_INET; struct sockaddr_storage addr = {0}; socklen_t len; int err, fd;
- fd = make_socket(sotype, ip, port, &addr, &len); - if (fd < 0) + err = make_sockaddr(family, ip, port, &addr, &len); + if (err) return -1;
- err = connect(fd, (void *)&addr, len); - if (CHECK(err, "make_client", "connect")) { + fd = connect_to_addr(sotype, &addr, len, NULL); + if (CHECK(fd < 0, "connect_to_addr", "connect")) { log_err("failed to connect client socket"); - goto fail; + return -1; }
return fd; -fail: - close(fd); - return -1; }
static __u64 socket_cookie(int fd)
On Fri, 2024-06-21 at 18:35 +0800, Geliang Tang wrote:
[...]
@@ -224,24 +188,22 @@ static int make_server(int sotype, const char *ip, int port, static int make_client(int sotype, const char *ip, int port) {
- int family = is_ipv6(ip) ? AF_INET6 : AF_INET; struct sockaddr_storage addr = {0}; socklen_t len; int err, fd;
- fd = make_socket(sotype, ip, port, &addr, &len);
- if (fd < 0)
- err = make_sockaddr(family, ip, port, &addr, &len);
- if (err) return -1;
- err = connect(fd, (void *)&addr, len);
- if (CHECK(err, "make_client", "connect")) {
- fd = connect_to_addr(sotype, &addr, len, NULL);
Nit: same nit about timeout, need to pass options here.
- if (CHECK(fd < 0, "connect_to_addr", "connect")) { log_err("failed to connect client socket");
goto fail;
}return -1;
return fd; -fail:
- close(fd);
- return -1;
} static __u64 socket_cookie(int fd)
linux-kselftest-mirror@lists.linaro.org