From: Geliang Tang tanggeliang@kylinos.cn
v4: - a new patch to use make_sockaddr in sockmap_ktls. - a new patch to close fd in error path in drop_on_reuseport. - drop make_server() in patch 7. - drop make_client() too in patch 9.
v3: - a new patch to add backlog for network_helper_opts. - use start_server_str in sockmap_ktls now, not start_server.
v2: - address Eduard's comments in v1. (thanks) - fix errors reported by CI.
This patch set uses network helpers in sockmap_ktls and sk_lookup, and drop three local helpers tcp_server(), inetaddr_len(), make_socket(), make_server() and make_client() in them.
Geliang Tang (9): selftests/bpf: Add backlog for network_helper_opts selftests/bpf: Use start_server_str in sockmap_ktls selftests/bpf: Use connect_to_fd in sockmap_ktls selftests/bpf: Use make_sockaddr in sockmap_ktls selftests/bpf: Close fd in error path in drop_on_reuseport selftests/bpf: Invoke attach_reuseport out of make_server selftests/bpf: Use start_server_str in sk_lookup selftests/bpf: Use connect_to_fd in sk_lookup selftests/bpf: Use connect_to_addr in sk_lookup
tools/testing/selftests/bpf/network_helpers.c | 2 +- tools/testing/selftests/bpf/network_helpers.h | 1 + .../selftests/bpf/prog_tests/sk_lookup.c | 263 +++++++++--------- .../selftests/bpf/prog_tests/sockmap_ktls.c | 51 +--- 4 files changed, 140 insertions(+), 177 deletions(-)
From: Geliang Tang tanggeliang@kylinos.cn
Some callers expect __start_server() helper to pass their own "backlog" value to listen() instead of the default of 1. So this patch adds struct member "backlog" for network_helper_opts to allow callers to set "backlog" value via start_server_str() helper.
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn --- tools/testing/selftests/bpf/network_helpers.c | 2 +- tools/testing/selftests/bpf/network_helpers.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 44c2c8fa542a..16cbb3fdcabf 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -106,7 +106,7 @@ static int __start_server(int type, const struct sockaddr *addr, socklen_t addrl }
if (type == SOCK_STREAM) { - if (listen(fd, 1) < 0) { + if (listen(fd, opts->backlog ? : 1) < 0) { log_err("Failed to listed on socket"); goto error_close; } diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 9ea36524b9db..8339c4e4b075 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -25,6 +25,7 @@ struct network_helper_opts { int timeout_ms; bool must_fail; int proto; + int backlog; int (*post_socket_cb)(int fd, void *opts); void *cb_opts; };
From: Geliang Tang tanggeliang@kylinos.cn
Include network_helpers.h in prog_tests/sockmap_ktls.c, use public network helper start_server_str() instead of local defined function tcp_server(). This can avoid duplicate code.
Technically, this is not a one-for-one replacement, as start_server_str() also does bind(). But the difference does not seem to matter.
Acked-by: Eduard Zingerman eddyz87@gmail.com Signed-off-by: Geliang Tang tanggeliang@kylinos.cn --- .../selftests/bpf/prog_tests/sockmap_ktls.c | 21 +++++-------------- 1 file changed, 5 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..4dc7933bb556 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 }; @@ -35,11 +21,14 @@ 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 network_helper_opts opts = { + .backlog = SOMAXCONN, + }; struct sockaddr_storage addr = {0}; socklen_t len = sizeof(addr); int err, cli, srv, zero = 0;
- srv = tcp_server(family); + srv = start_server_str(family, SOCK_STREAM, NULL, 0, &opts); if (srv == -1) return;
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.
Acked-by: Eduard Zingerman eddyz87@gmail.com 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 4dc7933bb556..a6b0ed633505 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c @@ -24,26 +24,16 @@ static void test_sockmap_ktls_disconnect_after_delete(int family, int map) struct network_helper_opts opts = { .backlog = SOMAXCONN, }; - struct sockaddr_storage addr = {0}; - socklen_t len = sizeof(addr); int err, cli, srv, zero = 0;
srv = start_server_str(family, SOCK_STREAM, NULL, 0, &opts); 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;
From: Geliang Tang tanggeliang@kylinos.cn
This patch uses public helper make_sockaddr() exported in network_helpers.h instead of open-coding in sockmap_ktls.c. This can avoid duplicate code.
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn --- .../selftests/bpf/prog_tests/sockmap_ktls.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c index a6b0ed633505..34fdb1cf10f1 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c @@ -59,23 +59,11 @@ static void test_sockmap_ktls_update_fails_when_sock_has_ulp(int family, int map { struct sockaddr_storage addr = {}; socklen_t len = sizeof(addr); - struct sockaddr_in6 *v6; - struct sockaddr_in *v4; int err, s, zero = 0;
- switch (family) { - case AF_INET: - v4 = (struct sockaddr_in *)&addr; - v4->sin_family = AF_INET; - break; - case AF_INET6: - v6 = (struct sockaddr_in6 *)&addr; - v6->sin6_family = AF_INET6; - break; - default: - PRINT_FAIL("unsupported socket family %d", family); + err = make_sockaddr(family, NULL, 0, &addr, &len); + if (!ASSERT_OK(err, "make_sockaddr")) return; - }
s = socket(family, SOCK_STREAM, 0); if (!ASSERT_GE(s, 0, "socket"))
On Mon, 2024-06-24 at 12:33 +0800, Geliang Tang wrote:
From: Geliang Tang tanggeliang@kylinos.cn
This patch uses public helper make_sockaddr() exported in network_helpers.h instead of open-coding in 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
Server 1 fd should be closed in the error path when update_lookup_map() fails. This patch fixes it by goto "close_srv1" instead of "detach" lable in that case.
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn --- tools/testing/selftests/bpf/prog_tests/sk_lookup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index 597d0467a926..de2466547efe 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -994,7 +994,7 @@ static void drop_on_reuseport(const struct test *t)
err = update_lookup_map(t->sock_map, SERVER_A, server1); if (err) - goto detach; + goto close_srv1;
/* second server on destination address we should never reach */ server2 = make_server(t->sotype, t->connect_to.ip, t->connect_to.port,
On Mon, 2024-06-24 at 12:33 +0800, Geliang Tang wrote:
From: Geliang Tang tanggeliang@kylinos.cn
Server 1 fd should be closed in the error path when update_lookup_map() fails. This patch fixes it by goto "close_srv1" instead of "detach" lable in that case.
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn
Acked-by: Eduard Zingerman eddyz87@gmail.com
From: Geliang Tang tanggeliang@kylinos.cn
In order to facilitate subsequent commits to drop make_server(), this patch invokes attach_reuseport() out of make_server(), right after invoking make_server() if the passed "reuseport_prog" argument is not NULL.
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn --- .../selftests/bpf/prog_tests/sk_lookup.c | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index de2466547efe..d87dfcf5db07 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -204,15 +204,6 @@ static int make_server(int sotype, const char *ip, int port, } }
- /* Late attach reuseport prog so we can have one init path */ - if (reuseport_prog) { - err = attach_reuseport(fd, reuseport_prog); - if (CHECK(err, "attach_reuseport", "failed\n")) { - log_err("failed to attach reuseport prog"); - goto fail; - } - } - return fd; fail: close(fd); @@ -610,7 +601,8 @@ static void run_lookup_prog(const struct test *t) server_fds[i] = make_server(t->sotype, t->listen_at.ip, t->listen_at.port, t->reuseport_prog); - if (server_fds[i] < 0) + if (server_fds[i] < 0 || + attach_reuseport(server_fds[i], t->reuseport_prog)) goto close;
err = update_lookup_map(t->sock_map, i, server_fds[i]); @@ -636,7 +628,8 @@ static void run_lookup_prog(const struct test *t) reuse_conn_fd = make_server(t->sotype, t->listen_at.ip, t->listen_at.port, t->reuseport_prog); - if (reuse_conn_fd < 0) + if (reuse_conn_fd < 0 || + attach_reuseport(reuse_conn_fd, t->reuseport_prog)) goto close;
/* Connect the extra socket to itself */ @@ -878,6 +871,9 @@ static void drop_on_lookup(const struct test *t) if (server_fd < 0) goto detach;
+ if (attach_reuseport(server_fd, t->reuseport_prog)) + goto close_srv; + client_fd = make_socket(t->sotype, t->connect_to.ip, t->connect_to.port, &dst); if (client_fd < 0) @@ -992,6 +988,9 @@ static void drop_on_reuseport(const struct test *t) if (server1 < 0) goto detach;
+ if (attach_reuseport(server1, t->reuseport_prog)) + goto close_srv1; + err = update_lookup_map(t->sock_map, SERVER_A, server1); if (err) goto close_srv1;
On Mon, 2024-06-24 at 12:33 +0800, Geliang Tang wrote:
From: Geliang Tang tanggeliang@kylinos.cn
In order to facilitate subsequent commits to drop make_server(), this patch invokes attach_reuseport() out of make_server(), right after invoking make_server() if the passed "reuseport_prog" argument is not NULL.
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn
.../selftests/bpf/prog_tests/sk_lookup.c | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index de2466547efe..d87dfcf5db07 100644
Tbh, I don't like this refactoring for sk_lookup, it does not seem to make the code clearer or shorter (e.g. 1414 LOC vs 1409). If anything, it looks like reuseport_prog, callbacks setup and start_server_str could be hidden insider make_server().
--- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -204,15 +204,6 @@ static int make_server(int sotype, const char *ip, int port, } }
- /* Late attach reuseport prog so we can have one init path */
- if (reuseport_prog) {
err = attach_reuseport(fd, reuseport_prog);
if (CHECK(err, "attach_reuseport", "failed\n")) {
log_err("failed to attach reuseport prog");
goto fail;
}
- }
- return fd;
fail: close(fd); @@ -610,7 +601,8 @@ static void run_lookup_prog(const struct test *t) server_fds[i] = make_server(t->sotype, t->listen_at.ip, t->listen_at.port, t->reuseport_prog);
if (server_fds[i] < 0)
if (server_fds[i] < 0 ||
attach_reuseport(server_fds[i], t->reuseport_prog)) goto close;
err = update_lookup_map(t->sock_map, i, server_fds[i]); @@ -636,7 +628,8 @@ static void run_lookup_prog(const struct test *t) reuse_conn_fd = make_server(t->sotype, t->listen_at.ip, t->listen_at.port, t->reuseport_prog);
if (reuse_conn_fd < 0)
if (reuse_conn_fd < 0 ||
attach_reuseport(reuse_conn_fd, t->reuseport_prog)) goto close;
/* Connect the extra socket to itself */ @@ -878,6 +871,9 @@ static void drop_on_lookup(const struct test *t) if (server_fd < 0) goto detach;
- if (attach_reuseport(server_fd, t->reuseport_prog))
goto close_srv;
- client_fd = make_socket(t->sotype, t->connect_to.ip, t->connect_to.port, &dst); if (client_fd < 0)
@@ -992,6 +988,9 @@ static void drop_on_reuseport(const struct test *t) if (server1 < 0) goto detach;
- if (attach_reuseport(server1, t->reuseport_prog))
goto close_srv1;
- err = update_lookup_map(t->sock_map, SERVER_A, server1); if (err) goto close_srv1;
From: Geliang Tang tanggeliang@kylinos.cn
This patch uses public helper start_server_addr() instead of local defined function 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().
Acked-by: Eduard Zingerman eddyz87@gmail.com Signed-off-by: Geliang Tang tanggeliang@kylinos.cn --- .../selftests/bpf/prog_tests/sk_lookup.c | 141 ++++++++++++------ 1 file changed, 96 insertions(+), 45 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index d87dfcf5db07..634c2ac0595e 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,24 +191,8 @@ 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; - } - - 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; - } - } - - return fd; fail: - close(fd); - return -1; + return err; }
static int make_client(int sotype, const char *ip, int port) @@ -588,6 +573,17 @@ static void query_lookup_prog(struct test_sk_lookup *skel)
static void run_lookup_prog(const struct test *t) { + int family = is_ipv6(t->listen_at.ip) ? AF_INET6 : AF_INET; + struct cb_opts cb_opts = { + .family = family, + .sotype = t->sotype, + .reuseport = t->reuseport_prog, + }; + struct network_helper_opts srv_opts = { + .backlog = SOMAXCONN, + .post_socket_cb = setsockopts, + .cb_opts = &cb_opts, + }; int server_fds[] = { [0 ... MAX_SERVERS - 1] = -1 }; int client_fd, reuse_conn_fd = -1; struct bpf_link *lookup_link; @@ -598,9 +594,8 @@ static void run_lookup_prog(const struct test *t) return;
for (i = 0; i < ARRAY_SIZE(server_fds); i++) { - server_fds[i] = make_server(t->sotype, t->listen_at.ip, - t->listen_at.port, - t->reuseport_prog); + server_fds[i] = start_server_str(family, t->sotype, t->listen_at.ip, + t->listen_at.port, &srv_opts); if (server_fds[i] < 0 || attach_reuseport(server_fds[i], t->reuseport_prog)) goto close; @@ -625,9 +620,8 @@ static void run_lookup_prog(const struct test *t) socklen_t len = sizeof(addr);
/* Add an extra socket to reuseport group */ - reuse_conn_fd = make_server(t->sotype, t->listen_at.ip, - t->listen_at.port, - t->reuseport_prog); + reuse_conn_fd = start_server_str(family, t->sotype, t->listen_at.ip, + t->listen_at.port, &srv_opts); if (reuse_conn_fd < 0 || attach_reuseport(reuse_conn_fd, t->reuseport_prog)) goto close; @@ -857,6 +851,16 @@ static void test_redirect_lookup(struct test_sk_lookup *skel)
static void drop_on_lookup(const struct test *t) { + struct cb_opts cb_opts = { + .family = is_ipv6(t->listen_at.ip) ? AF_INET6 : AF_INET, + .sotype = t->sotype, + .reuseport = t->reuseport_prog, + }; + struct network_helper_opts opts = { + .backlog = SOMAXCONN, + .post_socket_cb = setsockopts, + .cb_opts = &cb_opts, + }; struct sockaddr_storage dst = {}; int client_fd, server_fd, err; struct bpf_link *lookup_link; @@ -866,8 +870,8 @@ static void drop_on_lookup(const struct test *t) if (!lookup_link) return;
- server_fd = make_server(t->sotype, t->listen_at.ip, t->listen_at.port, - t->reuseport_prog); + server_fd = start_server_str(cb_opts.family, t->sotype, t->listen_at.ip, + t->listen_at.port, &opts); if (server_fd < 0) goto detach;
@@ -974,6 +978,25 @@ static void test_drop_on_lookup(struct test_sk_lookup *skel)
static void drop_on_reuseport(const struct test *t) { + struct cb_opts cb_opts1 = { + .family = is_ipv6(t->listen_at.ip) ? AF_INET6 : AF_INET, + .sotype = t->sotype, + .reuseport = t->reuseport_prog, + }; + struct network_helper_opts opts1 = { + .backlog = SOMAXCONN, + .post_socket_cb = setsockopts, + .cb_opts = &cb_opts1, + }; + struct cb_opts cb_opts2 = { + .family = is_ipv6(t->connect_to.ip) ? AF_INET6 : AF_INET, + .sotype = t->sotype, + }; + struct network_helper_opts opts2 = { + .backlog = SOMAXCONN, + .post_socket_cb = setsockopts, + .cb_opts = &cb_opts2, + }; struct sockaddr_storage dst = { 0 }; int client, server1, server2, err; struct bpf_link *lookup_link; @@ -983,8 +1006,8 @@ static void drop_on_reuseport(const struct test *t) if (!lookup_link) return;
- server1 = make_server(t->sotype, t->listen_at.ip, t->listen_at.port, - t->reuseport_prog); + server1 = start_server_str(cb_opts1.family, t->sotype, t->listen_at.ip, + t->listen_at.port, &opts1); if (server1 < 0) goto detach;
@@ -996,8 +1019,8 @@ static void drop_on_reuseport(const struct test *t) goto close_srv1;
/* second server on destination address we should never reach */ - server2 = make_server(t->sotype, t->connect_to.ip, t->connect_to.port, - NULL /* reuseport prog */); + server2 = start_server_str(cb_opts2.family, t->sotype, t->connect_to.ip, + t->connect_to.port, &opts2); if (server2 < 0) goto close_srv1;
@@ -1082,6 +1105,15 @@ static void run_sk_assign(struct test_sk_lookup *skel, struct bpf_program *lookup_prog, const char *remote_ip, const char *local_ip) { + struct cb_opts cb_opts = { + .family = is_ipv6(local_ip) ? AF_INET6 : AF_INET, + .sotype = SOCK_STREAM, + }; + struct network_helper_opts srv_opts = { + .backlog = SOMAXCONN, + .post_socket_cb = setsockopts, + .cb_opts = &cb_opts, + }; int server_fds[] = { [0 ... MAX_SERVERS - 1] = -1 }; struct bpf_sk_lookup ctx; __u64 server_cookie; @@ -1100,7 +1132,8 @@ static void run_sk_assign(struct test_sk_lookup *skel, ctx.protocol = IPPROTO_TCP;
for (i = 0; i < ARRAY_SIZE(server_fds); i++) { - server_fds[i] = make_server(SOCK_STREAM, local_ip, 0, NULL); + server_fds[i] = start_server_str(cb_opts.family, SOCK_STREAM, + local_ip, 0, &srv_opts); if (server_fds[i] < 0) goto close_servers;
@@ -1146,10 +1179,19 @@ static void run_sk_assign_v6(struct test_sk_lookup *skel, static void run_sk_assign_connected(struct test_sk_lookup *skel, int sotype) { + struct cb_opts cb_opts = { + .family = AF_INET, + .sotype = sotype, + }; + struct network_helper_opts opts = { + .backlog = SOMAXCONN, + .post_socket_cb = setsockopts, + .cb_opts = &cb_opts, + }; int err, client_fd, connected_fd, server_fd; struct bpf_link *lookup_link;
- server_fd = make_server(sotype, EXT_IP4, EXT_PORT, NULL); + server_fd = start_server_str(AF_INET, sotype, EXT_IP4, EXT_PORT, &opts); if (server_fd < 0) return;
@@ -1216,6 +1258,15 @@ struct test_multi_prog {
static void run_multi_prog_lookup(const struct test_multi_prog *t) { + struct cb_opts cb_opts = { + .family = is_ipv6(t->listen_at.ip) ? AF_INET6 : AF_INET, + .sotype = SOCK_STREAM, + }; + struct network_helper_opts srv_opts = { + .backlog = SOMAXCONN, + .post_socket_cb = setsockopts, + .cb_opts = &cb_opts, + }; struct sockaddr_storage dst = {}; int map_fd, server_fd, client_fd; struct bpf_link *link1, *link2; @@ -1240,8 +1291,8 @@ static void run_multi_prog_lookup(const struct test_multi_prog *t) if (!link2) goto out_unlink1;
- server_fd = make_server(SOCK_STREAM, t->listen_at.ip, - t->listen_at.port, NULL); + server_fd = start_server_str(cb_opts.family, SOCK_STREAM, t->listen_at.ip, + t->listen_at.port, &srv_opts); if (server_fd < 0) goto out_unlink2;
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 --- .../testing/selftests/bpf/prog_tests/sk_lookup.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index 634c2ac0595e..e1c5b7d1fb3a 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -861,7 +861,6 @@ static void drop_on_lookup(const struct test *t) .post_socket_cb = setsockopts, .cb_opts = &cb_opts, }; - struct sockaddr_storage dst = {}; int client_fd, server_fd, err; struct bpf_link *lookup_link; ssize_t n; @@ -878,12 +877,11 @@ static void drop_on_lookup(const struct test *t) if (attach_reuseport(server_fd, t->reuseport_prog)) goto close_srv;
- client_fd = make_socket(t->sotype, t->connect_to.ip, - t->connect_to.port, &dst); + client_fd = connect_to_fd(server_fd, IO_TIMEOUT_SEC); if (client_fd < 0) goto close_srv;
- err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); + err = 0; if (t->sotype == SOCK_DGRAM) { err = send_byte(client_fd); if (err) @@ -997,7 +995,6 @@ static void drop_on_reuseport(const struct test *t) .post_socket_cb = setsockopts, .cb_opts = &cb_opts2, }; - struct sockaddr_storage dst = { 0 }; int client, server1, server2, err; struct bpf_link *lookup_link; ssize_t n; @@ -1024,12 +1021,11 @@ 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); + client = connect_to_fd(server2, IO_TIMEOUT_SEC); if (client < 0) goto close_srv2;
- err = connect(client, (void *)&dst, inetaddr_len(&dst)); + err = 0; if (t->sotype == SOCK_DGRAM) { err = send_byte(client); if (err) @@ -1195,7 +1191,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, IO_TIMEOUT_SEC); if (connected_fd < 0) goto out_close_server;
@@ -1209,7 +1205,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, IO_TIMEOUT_SEC); if (client_fd < 0) goto out_unlink_prog; if (sotype == SOCK_DGRAM) {
From: Geliang Tang tanggeliang@kylinos.cn
Use public network helpers make_sockaddr() and connect_to_addr() instead of using make_socket() + connect() or make_client().
Now local defined functions inetaddr_len(), make_socket() and make_client() all can be dropped.
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn --- .../selftests/bpf/prog_tests/sk_lookup.c | 83 ++++--------------- 1 file changed, 16 insertions(+), 67 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index e1c5b7d1fb3a..5556796068f0 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -108,46 +108,6 @@ 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 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); - 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; @@ -195,27 +155,6 @@ static int setsockopts(int fd, void *opts) return err; }
-static int make_client(int sotype, const char *ip, int port) -{ - struct sockaddr_storage addr = {0}; - int err, fd; - - fd = make_socket(sotype, ip, port, &addr); - if (fd < 0) - return -1; - - err = connect(fd, (void *)&addr, inetaddr_len(&addr)); - if (CHECK(err, "make_client", "connect")) { - log_err("failed to connect client socket"); - goto fail; - } - - return fd; -fail: - close(fd); - return -1; -} - static __u64 socket_cookie(int fd) { __u64 cookie; @@ -584,8 +523,13 @@ static void run_lookup_prog(const struct test *t) .post_socket_cb = setsockopts, .cb_opts = &cb_opts, }; + struct network_helper_opts cli_opts = { + .timeout_ms = IO_TIMEOUT_SEC, + }; int server_fds[] = { [0 ... MAX_SERVERS - 1] = -1 }; int client_fd, reuse_conn_fd = -1; + struct sockaddr_storage addr = {}; + socklen_t len = sizeof(addr); struct bpf_link *lookup_link; int i, err;
@@ -616,9 +560,6 @@ static void run_lookup_prog(const struct test *t) * BPF socket lookup. */ if (t->reuseport_has_conns) { - struct sockaddr_storage addr = {}; - socklen_t len = sizeof(addr); - /* Add an extra socket to reuseport group */ reuse_conn_fd = start_server_str(family, t->sotype, t->listen_at.ip, t->listen_at.port, &srv_opts); @@ -635,7 +576,9 @@ static void run_lookup_prog(const struct test *t) goto close; }
- client_fd = make_client(t->sotype, t->connect_to.ip, t->connect_to.port); + if (make_sockaddr(family, t->connect_to.ip, t->connect_to.port, &addr, &len)) + goto close; + client_fd = connect_to_addr(t->sotype, &addr, len, &cli_opts); if (client_fd < 0) goto close;
@@ -1263,10 +1206,14 @@ static void run_multi_prog_lookup(const struct test_multi_prog *t) .post_socket_cb = setsockopts, .cb_opts = &cb_opts, }; + struct network_helper_opts cli_opts = { + .timeout_ms = IO_TIMEOUT_SEC, + }; 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);
@@ -1296,11 +1243,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); + if (make_sockaddr(AF_INET, EXT_IP4, EXT_PORT, &dst, &len)) + goto out_close_server; + client_fd = connect_to_addr(SOCK_STREAM, &dst, len, &cli_opts); if (client_fd < 0) goto out_close_server;
- err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); + err = 0; if (CHECK(err && !t->expect_errno, "connect", "unexpected error %d\n", errno)) goto out_close_client;
linux-kselftest-mirror@lists.linaro.org