From: Geliang Tang tanggeliang@kylinos.cn
v10: - a new patch 10 is added. - patches 1-6, 8-9 unchanged, only commit logs updated. - "err = -errno" is used in patches 7, 11, 12 to get the real error number before checking value of "err".
v9: - new patches 5-7, new struct member expect_errno for network_helper_opts. - patches 1-4, 8-9 unchanged. - update patches 10-11 to make sure all tests pass.
v8: - only patch 8 updated, to fix errors reported by CI.
v7: - address Martin's comments in v6. (thanks) - use MAX(opts->backlog, 0) instead of opts->backlog. - use connect_to_fd_opts instead connect_to_fd. - more ASSERT_* to check errors.
v6: - update patch 6 as Daniel suggested. (thanks)
v5: - keep make_server and make_client as Eduard suggested.
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() and make_socket() in them.
Geliang Tang (12): selftests/bpf: Add backlog for network_helper_opts selftests/bpf: Use start_server_str in sockmap_ktls selftests/bpf: Use connect_to_fd_opts in sockmap_ktls selftests/bpf: Use make_sockaddr in sockmap_ktls selftests/bpf: Add network_helper_opts for connect_fd_to_fd selftests/bpf: Add expect_errno for network_helper_opts selftests/bpf: Set expect_errno for cgroup_skb_sk_lookup selftests/bpf: Close fd in error path in drop_on_reuseport selftests/bpf: Use start_server_str in sk_lookup selftests/bpf: Use connect_fd_to_fd in sk_lookup selftests/bpf: Use connect_to_addr in sk_lookup selftests/bpf: Drop make_socket in sk_lookup
tools/testing/selftests/bpf/network_helpers.c | 23 ++- tools/testing/selftests/bpf/network_helpers.h | 8 +- .../testing/selftests/bpf/prog_tests/bpf_nf.c | 5 +- .../bpf/prog_tests/cgroup_skb_sk_lookup.c | 8 +- .../selftests/bpf/prog_tests/cgroup_tcp_skb.c | 4 +- .../selftests/bpf/prog_tests/cgroup_v1v2.c | 1 + .../selftests/bpf/prog_tests/sk_lookup.c | 162 +++++++----------- .../selftests/bpf/prog_tests/sockmap_ktls.c | 53 ++---- 8 files changed, 108 insertions(+), 156 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.
listen(fd, 0 /* backlog */) can be used to enforce syncookie. Meaning backlog 0 is a legit value.
Using 0 as a default and changing it to 1 here is fine. It makes the test program easier to write for the common case. Enforcing syncookie mode by using backlog 0 is a niche use case but it should at least have a way for the caller to do that.
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn --- tools/testing/selftests/bpf/network_helpers.c | 2 +- tools/testing/selftests/bpf/network_helpers.h | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 44c2c8fa542a..e0cba4178e41 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 ? MAX(opts->backlog, 0) : 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..4f26bfc2dbf5 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -25,6 +25,10 @@ struct network_helper_opts { int timeout_ms; bool must_fail; int proto; + /* The backlog argument for listen(), defines the maximum length to which + * the queue of pending connections for sockfd may grow. + */ + 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 | 23 +++++-------------- 1 file changed, 6 insertions(+), 17 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..32be112967a5 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,12 +21,15 @@ 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); - if (srv == -1) + srv = start_server_str(family, SOCK_STREAM, NULL, 0, &opts); + if (!ASSERT_GE(srv, 0, "start_server_str")) return;
err = getsockname(srv, (struct sockaddr *)&addr, &len);
On Mon, 2024-07-08 at 10:29 +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_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.
I'd like to add a "nobind" flag in network_helper_opts to handle this.
And another flag "nolisten" to allow to use start_server_str() in test_sockmap_ktls_update_fails_when_sock_has_ulp() too.
These two flags are also useful for later commits such as "use start_server_str() in test_insert_bound()" in prog_tests/sockmap_listen.c.
I changed the states of patches 2-4 (for sockmap_ktls) as "Changes Requested".
Other 9 patches for sk_lookup in this set are still valid, and no conflict.
Thanks, -Geliang
Acked-by: Eduard Zingerman eddyz87@gmail.com Signed-off-by: Geliang Tang tanggeliang@kylinos.cn
.../selftests/bpf/prog_tests/sockmap_ktls.c | 23 +++++-------------- 1 file changed, 6 insertions(+), 17 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..32be112967a5 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,12 +21,15 @@ 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);
- if (srv == -1)
- srv = start_server_str(family, SOCK_STREAM, NULL, 0, &opts);
- if (!ASSERT_GE(srv, 0, "start_server_str"))
return; err = getsockname(srv, (struct sockaddr *)&addr, &len);
From: Geliang Tang tanggeliang@kylinos.cn
Use public network helper connect_to_fd_opts() 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 32be112967a5..a794aa688ab5 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 (!ASSERT_GE(srv, 0, "start_server_str")) 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_opts(srv, SOCK_STREAM, NULL); + if (!ASSERT_GE(cli, 0, "connect_to_fd_opts")) 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.
Acked-by: Eduard Zingerman eddyz87@gmail.com 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 a794aa688ab5..3c223601e8b6 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"))
From: Geliang Tang tanggeliang@kylinos.cn
To make connect_fd_to_fd() more flexible, use a struct network_helper_opts parameter "opts" instead of timeout_ms. With this change, other opts like "must_fail" can be used in connect_fd_to_fd() too.
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn --- tools/testing/selftests/bpf/network_helpers.c | 10 +++++++--- tools/testing/selftests/bpf/network_helpers.h | 3 ++- tools/testing/selftests/bpf/prog_tests/bpf_nf.c | 5 ++++- .../selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c | 5 ++++- .../testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c | 4 ++-- 5 files changed, 19 insertions(+), 8 deletions(-)
diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index e0cba4178e41..475a5a04e61e 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -369,12 +369,16 @@ int connect_to_fd(int server_fd, int timeout_ms) return connect_to_fd_opts(server_fd, type, &opts); }
-int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms) +int connect_fd_to_fd(int client_fd, int server_fd, + const struct network_helper_opts *opts) { struct sockaddr_storage addr; socklen_t len = sizeof(addr);
- if (settimeo(client_fd, timeout_ms)) + if (!opts) + opts = &default_opts; + + if (settimeo(client_fd, opts->timeout_ms)) return -1;
if (getsockname(server_fd, (struct sockaddr *)&addr, &len)) { @@ -382,7 +386,7 @@ int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms) return -1; }
- if (connect_fd_to_addr(client_fd, &addr, len, false)) + if (connect_fd_to_addr(client_fd, &addr, len, opts->must_fail)) return -1;
return 0; diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 4f26bfc2dbf5..fcda6b2333ad 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -66,7 +66,8 @@ 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); int connect_to_fd_opts(int server_fd, int type, const struct network_helper_opts *opts); -int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms); +int connect_fd_to_fd(int client_fd, int server_fd, + const struct network_helper_opts *opts); int fastopen_connect(int server_fd, const char *data, unsigned int data_len, int timeout_ms); int make_sockaddr(int family, const char *addr_str, __u16 port, diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_nf.c b/tools/testing/selftests/bpf/prog_tests/bpf_nf.c index a4a1f93878d4..cd4dce5bd7f1 100644 --- a/tools/testing/selftests/bpf/prog_tests/bpf_nf.c +++ b/tools/testing/selftests/bpf/prog_tests/bpf_nf.c @@ -33,13 +33,16 @@ enum {
static int connect_to_server(int srv_fd) { + struct network_helper_opts opts = { + .timeout_ms = TIMEOUT_MS, + }; int fd = -1;
fd = socket(AF_INET, SOCK_STREAM, 0); if (!ASSERT_GE(fd, 0, "socket")) goto out;
- if (!ASSERT_EQ(connect_fd_to_fd(fd, srv_fd, TIMEOUT_MS), 0, "connect_fd_to_fd")) { + if (!ASSERT_EQ(connect_fd_to_fd(fd, srv_fd, &opts), 0, "connect_fd_to_fd")) { close(fd); fd = -1; } diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c index b9dc4ec655b5..bcf142779cc6 100644 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c @@ -8,6 +8,9 @@
static void run_lookup_test(__u16 *g_serv_port, int out_sk) { + struct network_helper_opts opts = { + .timeout_ms = 1000, + }; int serv_sk = -1, in_sk = -1, serv_in_sk = -1, err; struct sockaddr_in6 addr = {}; socklen_t addr_len = sizeof(addr); @@ -24,7 +27,7 @@ static void run_lookup_test(__u16 *g_serv_port, int out_sk) *g_serv_port = addr.sin6_port;
/* Client outside of test cgroup should fail to connect by timeout. */ - err = connect_fd_to_fd(out_sk, serv_sk, 1000); + err = connect_fd_to_fd(out_sk, serv_sk, &opts); if (CHECK(!err || errno != EINPROGRESS, "connect_fd_to_fd", "unexpected result err %d errno %d\n", err, errno)) goto cleanup; diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c b/tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c index a1542faf7873..f35a9897db24 100644 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c @@ -86,7 +86,7 @@ static int talk_to_cgroup(int *client_fd, int *listen_fd, int *service_fd, skel->bss->g_sock_port = ntohs(port);
/* Connect client to server */ - err = connect_fd_to_fd(*client_fd, *listen_fd, 0); + err = connect_fd_to_fd(*client_fd, *listen_fd, NULL); if (!ASSERT_OK(err, "connect_fd_to_fd")) return -1; *service_fd = accept(*listen_fd, NULL, NULL); @@ -136,7 +136,7 @@ static int talk_to_outside(int *client_fd, int *listen_fd, int *service_fd, skel->bss->g_sock_port = ntohs(port);
/* Connect client to server */ - err = connect_fd_to_fd(*client_fd, *listen_fd, 0); + err = connect_fd_to_fd(*client_fd, *listen_fd, NULL); if (!ASSERT_OK(err, "connect_fd_to_fd")) return -1; *service_fd = accept(*listen_fd, NULL, NULL);
From: Geliang Tang tanggeliang@kylinos.cn
The errno EPERM is skipped in connect_fd_to_addr() by cgroup_v1v2 tests. More generally, it makes sense to add a struct member "expect_errno" for network_helper_opts to identify the expect errno to be skipped.
Correspondingly, connect_fd_to_addr() helper needs to add a new parameter "expect_errno" too to accept "opts->expect_errno" passed from the caller connect_to_addr() or connect_fd_to_fd().
With this change, only need to set "expect_errno" as EPERM in run_test() in prog_tests/cgroup_v1v2.c.
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn --- tools/testing/selftests/bpf/network_helpers.c | 11 +++++++---- tools/testing/selftests/bpf/network_helpers.h | 1 + tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c | 1 + 3 files changed, 9 insertions(+), 4 deletions(-)
diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 475a5a04e61e..062170d6be1c 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -279,7 +279,8 @@ int client_socket(int family, int type,
static int connect_fd_to_addr(int fd, const struct sockaddr_storage *addr, - socklen_t addrlen, const bool must_fail) + socklen_t addrlen, const bool must_fail, + const int expect_errno) { int ret;
@@ -290,7 +291,7 @@ static int connect_fd_to_addr(int fd, log_err("Unexpected success to connect to server"); return -1; } - if (errno != EPERM) { + if (errno != expect_errno) { log_err("Unexpected error from connect to server"); return -1; } @@ -318,7 +319,8 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add return -1; }
- if (connect_fd_to_addr(fd, addr, addrlen, opts->must_fail)) + if (connect_fd_to_addr(fd, addr, addrlen, opts->must_fail, + opts->expect_errno)) goto error_close;
return fd; @@ -386,7 +388,8 @@ int connect_fd_to_fd(int client_fd, int server_fd, return -1; }
- if (connect_fd_to_addr(client_fd, &addr, len, opts->must_fail)) + if (connect_fd_to_addr(client_fd, &addr, len, opts->must_fail, + opts->expect_errno)) return -1;
return 0; diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index fcda6b2333ad..14d161d35248 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -24,6 +24,7 @@ typedef __u16 __sum16; struct network_helper_opts { int timeout_ms; bool must_fail; + int expect_errno; int proto; /* The backlog argument for listen(), defines the maximum length to which * the queue of pending connections for sockfd may grow. diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c b/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c index 9709c8db7275..ff477163f0ea 100644 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c @@ -11,6 +11,7 @@ static int run_test(int cgroup_fd, int server_fd, bool classid) { struct network_helper_opts opts = { .must_fail = true, + .expect_errno = EPERM, }; struct connect4_dropper *skel; int fd, err = 0;
On Mon, 2024-07-08 at 10:29 +0800, Geliang Tang wrote:
[...]
diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index fcda6b2333ad..14d161d35248 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -24,6 +24,7 @@ typedef __u16 __sum16; struct network_helper_opts { int timeout_ms; bool must_fail;
- int expect_errno;
I think this option obfuscates actual test cases. Each helper that accepts network_helper_opts as a parameter, does multiple system calls. It is not obvious which of these calls is expected to fail.
int proto; /* The backlog argument for listen(), defines the maximum length to which * the queue of pending connections for sockfd may grow.
[...]
On 7/8/24 12:15 PM, Eduard Zingerman wrote:
On Mon, 2024-07-08 at 10:29 +0800, Geliang Tang wrote:
[...]
diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index fcda6b2333ad..14d161d35248 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -24,6 +24,7 @@ typedef __u16 __sum16; struct network_helper_opts { int timeout_ms; bool must_fail;
- int expect_errno;
I think this option obfuscates actual test cases. Each helper that accepts network_helper_opts as a parameter, does multiple system calls. It is not obvious which of these calls is expected to fail.
+1
Please no. On top of that, handling specific errno is not something that belongs to the generic network_helpers.c. The individual test should check for the errno that it expects on the error path.
Is it for make_client()? The caller of make_client can check the errno instead, no?
[ The same goes for the existing "bool must_fail;" but it can be left for a separate cleanup patch ]
pw-bot: cr
int proto; /* The backlog argument for listen(), defines the maximum length to which * the queue of pending connections for sockfd may grow.
[...]
From: Geliang Tang tanggeliang@kylinos.cn
EINPROGRESS is skipped in run_lookup_test() in cgroup_skb_sk_lookup tests, but it is still showed in the log:
./test_progs -t cgroup_skb_sk_lookup -v
run_cgroup_bpf_test:PASS:skel_open_load 0 nsec run_cgroup_bpf_test:PASS:cgroup_join 0 nsec run_cgroup_bpf_test:PASS:cgroup_attach 0 nsec run_lookup_test:PASS:start_server 0 nsec run_lookup_test:PASS:getsockname 0 nsec (network_helpers.c:300: errno: Operation now in progress) Failed to \ connect to server run_lookup_test:PASS:connect_fd_to_fd 0 nsec run_lookup_test:PASS:connect_to_fd 0 nsec run_lookup_test:PASS:accept 0 nsec #51 cgroup_skb_sk_lookup:OK
To fix this, set EINPROGRESS as "expect_errno" of network_helper_opts and pass it to connect_fd_to_fd(). Skip this expect_errno when must_fail is false too in connect_fd_to_addr().
connect_fd_to_fd() returns "0" when connect() fails but "expect_errno" matched. So "err" is "0" after invoking connect_fd_to_fd() in run_lookup_test(). "err = -errno" is needed to get the real error number before checking value of "err".
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn --- tools/testing/selftests/bpf/network_helpers.c | 2 +- tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 062170d6be1c..1403487c5921 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -296,7 +296,7 @@ static int connect_fd_to_addr(int fd, return -1; } } else { - if (ret) { + if (ret && errno != expect_errno) { log_err("Failed to connect to server"); return -1; } diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c index bcf142779cc6..cce6ca45b2b0 100644 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c @@ -10,6 +10,7 @@ static void run_lookup_test(__u16 *g_serv_port, int out_sk) { struct network_helper_opts opts = { .timeout_ms = 1000, + .expect_errno = EINPROGRESS, }; int serv_sk = -1, in_sk = -1, serv_in_sk = -1, err; struct sockaddr_in6 addr = {}; @@ -28,6 +29,8 @@ static void run_lookup_test(__u16 *g_serv_port, int out_sk)
/* Client outside of test cgroup should fail to connect by timeout. */ err = connect_fd_to_fd(out_sk, serv_sk, &opts); + if (!err) + err = -errno; if (CHECK(!err || errno != EINPROGRESS, "connect_fd_to_fd", "unexpected result err %d errno %d\n", err, errno)) goto cleanup;
On 7/7/24 7:29 PM, Geliang Tang wrote:
From: Geliang Tangtanggeliang@kylinos.cn
EINPROGRESS is skipped in run_lookup_test() in cgroup_skb_sk_lookup tests, but it is still showed in the log:
./test_progs -t cgroup_skb_sk_lookup -v
run_cgroup_bpf_test:PASS:skel_open_load 0 nsec run_cgroup_bpf_test:PASS:cgroup_join 0 nsec run_cgroup_bpf_test:PASS:cgroup_attach 0 nsec run_lookup_test:PASS:start_server 0 nsec run_lookup_test:PASS:getsockname 0 nsec (network_helpers.c:300: errno: Operation now in progress) Failed to \ connect to server
This log is fine as-is and no need to "fix". The network_helpers failed in doing some socket operations and log this fact without treating it as a test_progs failure.
The individual test does expect this socket operation to fail, so test_progs does not report ":FAIL" for this test. CI is also smart enough and only shows the individual test's details log when that test ":FAIL" (i.e. the ASSERT_* failed).
run_lookup_test:PASS:connect_fd_to_fd 0 nsec run_lookup_test:PASS:connect_to_fd 0 nsec run_lookup_test:PASS:accept 0 nsec #51 cgroup_skb_sk_lookup:OK
To fix this, set EINPROGRESS as "expect_errno" of network_helper_opts and pass it to connect_fd_to_fd(). Skip this expect_errno when must_fail is false too in connect_fd_to_addr().
connect_fd_to_fd() returns "0" when connect() fails but "expect_errno" matched. So "err" is "0" after invoking connect_fd_to_fd() in run_lookup_test(). "err = -errno" is needed to get the real error number before checking value of "err".
From: Geliang Tang tanggeliang@kylinos.cn
In the error path when update_lookup_map() fails in drop_on_reuseport in prog_tests/sk_lookup.c, "server1", the fd of server 1, should be closed. This patch fixes this by using "goto close_srv1" lable instead of "detach" to close "server1" in this case.
Acked-by: Eduard Zingerman eddyz87@gmail.com 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,
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 | 58 +++++++++++-------- 1 file changed, 34 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 de2466547efe..d03ea3e64a2e 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,28 @@ 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 = { + .backlog = SOMAXCONN, + .post_socket_cb = setsockopts, + .cb_opts = &cb_opts, + }; + int err, fd; + + fd = start_server_str(cb_opts.family, sotype, ip, port, &opts); + if (!ASSERT_GE(fd, 0, "start_server_str")) + return -1;
/* Late attach reuseport prog so we can have one init path */ if (reuseport_prog) {
From: Geliang Tang tanggeliang@kylinos.cn
This patch uses public helper connect_fd_to_fd() exported in network_helpers.h instead of using getsockname() + connect() in run_lookup_prog() in prog_tests/sk_lookup.c. This can simplify the code.
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn --- tools/testing/selftests/bpf/prog_tests/sk_lookup.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index d03ea3e64a2e..184b838a9872 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -639,9 +639,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 = make_server(t->sotype, t->listen_at.ip, t->listen_at.port, @@ -649,12 +646,8 @@ static void run_lookup_prog(const struct test *t) if (reuse_conn_fd < 0) goto close;
- /* Connect the extra socket to itself */ - err = getsockname(reuse_conn_fd, (void *)&addr, &len); - if (CHECK(err, "getsockname", "errno %d\n", errno)) - goto close; - err = connect(reuse_conn_fd, (void *)&addr, len); - if (CHECK(err, "connect", "errno %d\n", errno)) + err = connect_fd_to_fd(reuse_conn_fd, reuse_conn_fd, NULL); + if (!ASSERT_OK(err, "connect_fd_to_fd")) goto close; }
From: Geliang Tang tanggeliang@kylinos.cn
Add a new parameter expect_errno for make_client(), and use public network helpers make_sockaddr() + connect_to_addr() in it instead of using local defined function make_socket() + connect(). This local function can be dropped latter.
A new parameter "expect_errno" is added for make_client() too to allow different "expect_errno" is passed to make_client() and set as "opts->expect_errno", then passed this opts to connect_to_addr().
connect_to_addr() returns normal "fd" instead of "-1" when connect() fails but "expect_errno" matched. So "err = -errno" are needed to get the real error numbers before checking values of "err" in both drop_on_lookup() and drop_on_reuseport().
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn --- .../selftests/bpf/prog_tests/sk_lookup.c | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index 184b838a9872..5e013e0f8136 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -229,25 +229,27 @@ static int make_server(int sotype, const char *ip, int port, return -1; }
-static int make_client(int sotype, const char *ip, int port) +static int make_client(int sotype, const char *ip, int port, + const int expect_errno) { - struct sockaddr_storage addr = {0}; + int family = is_ipv6(ip) ? AF_INET6 : AF_INET; + struct network_helper_opts opts = { + .timeout_ms = IO_TIMEOUT_SEC, + .expect_errno = expect_errno, + }; + struct sockaddr_storage addr; + socklen_t len; int err, fd;
- fd = make_socket(sotype, ip, port, &addr); - if (fd < 0) + err = make_sockaddr(family, ip, port, &addr, &len); + if (!ASSERT_OK(err, "make_sockaddr")) 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; - } + fd = connect_to_addr(sotype, &addr, len, &opts); + if (!ASSERT_GE(fd, 0, "connect_to_addr")) + return -1;
return fd; -fail: - close(fd); - return -1; }
static __u64 socket_cookie(int fd) @@ -651,7 +653,7 @@ 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); + client_fd = make_client(t->sotype, t->connect_to.ip, t->connect_to.port, 0); if (client_fd < 0) goto close;
@@ -867,7 +869,6 @@ 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; ssize_t n; @@ -881,12 +882,12 @@ 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); + client_fd = make_client(t->sotype, t->connect_to.ip, + t->connect_to.port, ECONNREFUSED); if (client_fd < 0) goto close_srv;
- err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); + err = -errno; if (t->sotype == SOCK_DGRAM) { err = send_byte(client_fd); if (err) @@ -981,7 +982,6 @@ 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; ssize_t n; @@ -1005,12 +1005,12 @@ 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 = make_client(t->sotype, t->connect_to.ip, + t->connect_to.port, ECONNREFUSED); if (client < 0) goto close_srv2;
- err = connect(client, (void *)&dst, inetaddr_len(&dst)); + err = -errno; if (t->sotype == SOCK_DGRAM) { err = send_byte(client); if (err) @@ -1157,7 +1157,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 = make_client(sotype, EXT_IP4, EXT_PORT, 0); if (connected_fd < 0) goto out_close_server;
@@ -1171,7 +1171,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 = make_client(sotype, EXT_IP4, EXT_PORT, 0); if (client_fd < 0) goto out_unlink_prog; if (sotype == SOCK_DGRAM) {
From: Geliang Tang tanggeliang@kylinos.cn
Use local helper make_client() in run_multi_prog_lookup() instead of using make_socket() + connect(). Then make_socket() and inetaddr_len() can be dropped now.
make_client() returns normal "fd" instead of "-1" when connect() fails but "expect_errno" matched in it. So "err = -errno" is needed to get the real error number before checking value of "err".
Signed-off-by: Geliang Tang tanggeliang@kylinos.cn --- .../selftests/bpf/prog_tests/sk_lookup.c | 45 +------------------ 1 file changed, 2 insertions(+), 43 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index 5e013e0f8136..95307ccbfc42 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; @@ -1220,7 +1180,6 @@ 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; @@ -1253,11 +1212,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_client(SOCK_STREAM, EXT_IP4, EXT_PORT, t->expect_errno); if (client_fd < 0) goto out_close_server;
- err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); + err = -errno; if (CHECK(err && !t->expect_errno, "connect", "unexpected error %d\n", errno)) goto out_close_client;
linux-kselftest-mirror@lists.linaro.org