Commit 22912472 authored by Martin KaFai Lau's avatar Martin KaFai Lau Committed by Andrii Nakryiko

Merge branch 'use network helpers, part 9'

Geliang Tang says:

====================
v3:
 - patch 2:
   - clear errno before connect_to_fd_opts.
   - print err logs in run_test.
   - set err to -1 when fd >= 0.
 - patch 3:
   - drop "int err".

v2:
 - update patch 2 as Martin suggested.

This is the 9th part of series "use network helpers" all BPF selftests
wide.

Patches 1-2 update network helpers interfaces suggested by Martin.
Patch 3 adds a new helper connect_to_addr_str() as Martin suggested
instead of adding connect_fd_to_addr_str().
Patch 4 uses this newly added helper in make_client().
Patch 5 uses make_client() in sk_lookup and drop make_socket().
====================
Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
parents 1722389b c70b2d90
...@@ -277,33 +277,6 @@ int client_socket(int family, int type, ...@@ -277,33 +277,6 @@ int client_socket(int family, int type,
return -1; return -1;
} }
static int connect_fd_to_addr(int fd,
const struct sockaddr_storage *addr,
socklen_t addrlen, const bool must_fail)
{
int ret;
errno = 0;
ret = connect(fd, (const struct sockaddr *)addr, addrlen);
if (must_fail) {
if (!ret) {
log_err("Unexpected success to connect to server");
return -1;
}
if (errno != EPERM) {
log_err("Unexpected error from connect to server");
return -1;
}
} else {
if (ret) {
log_err("Failed to connect to server");
return -1;
}
}
return 0;
}
int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t addrlen, int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t addrlen,
const struct network_helper_opts *opts) const struct network_helper_opts *opts)
{ {
...@@ -318,24 +291,45 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add ...@@ -318,24 +291,45 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add
return -1; return -1;
} }
if (connect_fd_to_addr(fd, addr, addrlen, opts->must_fail)) if (connect(fd, (const struct sockaddr *)addr, addrlen)) {
goto error_close; log_err("Failed to connect to server");
save_errno_close(fd);
return -1;
}
return fd; return fd;
}
error_close: int connect_to_addr_str(int family, int type, const char *addr_str, __u16 port,
save_errno_close(fd); const struct network_helper_opts *opts)
{
struct sockaddr_storage addr;
socklen_t addrlen;
if (!opts)
opts = &default_opts;
if (make_sockaddr(family, addr_str, port, &addr, &addrlen))
return -1; return -1;
return connect_to_addr(type, &addr, addrlen, opts);
} }
int connect_to_fd_opts(int server_fd, int type, const struct network_helper_opts *opts) int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
{ {
struct sockaddr_storage addr; struct sockaddr_storage addr;
socklen_t addrlen; socklen_t addrlen, optlen;
int type;
if (!opts) if (!opts)
opts = &default_opts; opts = &default_opts;
optlen = sizeof(type);
if (getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &type, &optlen)) {
log_err("getsockopt(SOL_TYPE)");
return -1;
}
addrlen = sizeof(addr); addrlen = sizeof(addr);
if (getsockname(server_fd, (struct sockaddr *)&addr, &addrlen)) { if (getsockname(server_fd, (struct sockaddr *)&addr, &addrlen)) {
log_err("Failed to get server addr"); log_err("Failed to get server addr");
...@@ -350,14 +344,8 @@ int connect_to_fd(int server_fd, int timeout_ms) ...@@ -350,14 +344,8 @@ int connect_to_fd(int server_fd, int timeout_ms)
struct network_helper_opts opts = { struct network_helper_opts opts = {
.timeout_ms = timeout_ms, .timeout_ms = timeout_ms,
}; };
int type, protocol;
socklen_t optlen; socklen_t optlen;
int protocol;
optlen = sizeof(type);
if (getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &type, &optlen)) {
log_err("getsockopt(SOL_TYPE)");
return -1;
}
optlen = sizeof(protocol); optlen = sizeof(protocol);
if (getsockopt(server_fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &optlen)) { if (getsockopt(server_fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &optlen)) {
...@@ -366,7 +354,7 @@ int connect_to_fd(int server_fd, int timeout_ms) ...@@ -366,7 +354,7 @@ int connect_to_fd(int server_fd, int timeout_ms)
} }
opts.proto = protocol; opts.proto = protocol;
return connect_to_fd_opts(server_fd, type, &opts); return connect_to_fd_opts(server_fd, &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, int timeout_ms)
...@@ -382,8 +370,10 @@ int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms) ...@@ -382,8 +370,10 @@ int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms)
return -1; return -1;
} }
if (connect_fd_to_addr(client_fd, &addr, len, false)) if (connect(client_fd, (const struct sockaddr *)&addr, len)) {
log_err("Failed to connect to server");
return -1; return -1;
}
return 0; return 0;
} }
......
...@@ -23,7 +23,6 @@ typedef __u16 __sum16; ...@@ -23,7 +23,6 @@ typedef __u16 __sum16;
struct network_helper_opts { struct network_helper_opts {
int timeout_ms; int timeout_ms;
bool must_fail;
int proto; int proto;
/* +ve: Passed to listen() as-is. /* +ve: Passed to listen() as-is.
* 0: Default when the test does not set * 0: Default when the test does not set
...@@ -70,8 +69,10 @@ int client_socket(int family, int type, ...@@ -70,8 +69,10 @@ int client_socket(int family, int type,
const struct network_helper_opts *opts); const struct network_helper_opts *opts);
int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len, int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len,
const struct network_helper_opts *opts); const struct network_helper_opts *opts);
int connect_to_addr_str(int family, int type, const char *addr_str, __u16 port,
const struct network_helper_opts *opts);
int connect_to_fd(int server_fd, int timeout_ms); int connect_to_fd(int server_fd, int timeout_ms);
int connect_to_fd_opts(int server_fd, int type, const struct network_helper_opts *opts); int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts);
int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms); int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms);
int fastopen_connect(int server_fd, const char *data, unsigned int data_len, int fastopen_connect(int server_fd, const char *data, unsigned int data_len,
int timeout_ms); int timeout_ms);
......
...@@ -49,7 +49,7 @@ static bool start_test(char *addr_str, ...@@ -49,7 +49,7 @@ static bool start_test(char *addr_str,
goto err; goto err;
/* connect to server */ /* connect to server */
*cli_fd = connect_to_fd_opts(*srv_fd, SOCK_STREAM, cli_opts); *cli_fd = connect_to_fd_opts(*srv_fd, cli_opts);
if (!ASSERT_NEQ(*cli_fd, -1, "connect_to_fd_opts")) if (!ASSERT_NEQ(*cli_fd, -1, "connect_to_fd_opts"))
goto err; goto err;
......
...@@ -9,9 +9,6 @@ ...@@ -9,9 +9,6 @@
static int run_test(int cgroup_fd, int server_fd, bool classid) static int run_test(int cgroup_fd, int server_fd, bool classid)
{ {
struct network_helper_opts opts = {
.must_fail = true,
};
struct connect4_dropper *skel; struct connect4_dropper *skel;
int fd, err = 0; int fd, err = 0;
...@@ -32,11 +29,16 @@ static int run_test(int cgroup_fd, int server_fd, bool classid) ...@@ -32,11 +29,16 @@ static int run_test(int cgroup_fd, int server_fd, bool classid)
goto out; goto out;
} }
fd = connect_to_fd_opts(server_fd, SOCK_STREAM, &opts); errno = 0;
if (fd < 0) fd = connect_to_fd_opts(server_fd, NULL);
if (fd >= 0) {
log_err("Unexpected success to connect to server");
err = -1; err = -1;
else
close(fd); close(fd);
} else if (errno != EPERM) {
log_err("Unexpected errno from connect to server");
err = -1;
}
out: out:
connect4_dropper__destroy(skel); connect4_dropper__destroy(skel);
return err; return err;
...@@ -52,7 +54,7 @@ void test_cgroup_v1v2(void) ...@@ -52,7 +54,7 @@ void test_cgroup_v1v2(void)
server_fd = start_server(AF_INET, SOCK_STREAM, NULL, port, 0); server_fd = start_server(AF_INET, SOCK_STREAM, NULL, port, 0);
if (!ASSERT_GE(server_fd, 0, "server_fd")) if (!ASSERT_GE(server_fd, 0, "server_fd"))
return; return;
client_fd = connect_to_fd_opts(server_fd, SOCK_STREAM, &opts); client_fd = connect_to_fd_opts(server_fd, &opts);
if (!ASSERT_GE(client_fd, 0, "client_fd")) { if (!ASSERT_GE(client_fd, 0, "client_fd")) {
close(server_fd); close(server_fd);
return; return;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment