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: Martin KaFai Lau <martin.lau@kernel.org> Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
This commit is contained in:
@@ -277,33 +277,6 @@ error_close:
|
||||
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,
|
||||
const struct network_helper_opts *opts)
|
||||
{
|
||||
@@ -318,17 +291,17 @@ 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))
|
||||
goto error_close;
|
||||
if (connect(fd, (const struct sockaddr *)addr, addrlen)) {
|
||||
log_err("Failed to connect to server");
|
||||
save_errno_close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return fd;
|
||||
|
||||
error_close:
|
||||
save_errno_close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int connect_to_fd_opts(int server_fd, int type, 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)
|
||||
{
|
||||
struct sockaddr_storage addr;
|
||||
socklen_t addrlen;
|
||||
@@ -336,6 +309,27 @@ int connect_to_fd_opts(int server_fd, int type, const struct network_helper_opts
|
||||
if (!opts)
|
||||
opts = &default_opts;
|
||||
|
||||
if (make_sockaddr(family, addr_str, port, &addr, &addrlen))
|
||||
return -1;
|
||||
|
||||
return connect_to_addr(type, &addr, addrlen, opts);
|
||||
}
|
||||
|
||||
int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
|
||||
{
|
||||
struct sockaddr_storage addr;
|
||||
socklen_t addrlen, optlen;
|
||||
int type;
|
||||
|
||||
if (!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);
|
||||
if (getsockname(server_fd, (struct sockaddr *)&addr, &addrlen)) {
|
||||
log_err("Failed to get server addr");
|
||||
@@ -350,14 +344,8 @@ int connect_to_fd(int server_fd, int timeout_ms)
|
||||
struct network_helper_opts opts = {
|
||||
.timeout_ms = timeout_ms,
|
||||
};
|
||||
int type, protocol;
|
||||
socklen_t optlen;
|
||||
|
||||
optlen = sizeof(type);
|
||||
if (getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &type, &optlen)) {
|
||||
log_err("getsockopt(SOL_TYPE)");
|
||||
return -1;
|
||||
}
|
||||
int protocol;
|
||||
|
||||
optlen = sizeof(protocol);
|
||||
if (getsockopt(server_fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &optlen)) {
|
||||
@@ -366,7 +354,7 @@ int connect_to_fd(int server_fd, int timeout_ms)
|
||||
}
|
||||
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)
|
||||
@@ -382,8 +370,10 @@ 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(client_fd, (const struct sockaddr *)&addr, len)) {
|
||||
log_err("Failed to connect to server");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ typedef __u16 __sum16;
|
||||
|
||||
struct network_helper_opts {
|
||||
int timeout_ms;
|
||||
bool must_fail;
|
||||
int proto;
|
||||
/* +ve: Passed to listen() as-is.
|
||||
* 0: Default when the test does not set
|
||||
@@ -70,8 +69,10 @@ int client_socket(int family, int type,
|
||||
const struct network_helper_opts *opts);
|
||||
int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len,
|
||||
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_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 fastopen_connect(int server_fd, const char *data, unsigned int data_len,
|
||||
int timeout_ms);
|
||||
|
||||
@@ -49,7 +49,7 @@ static bool start_test(char *addr_str,
|
||||
goto err;
|
||||
|
||||
/* 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"))
|
||||
goto err;
|
||||
|
||||
|
||||
@@ -9,9 +9,6 @@
|
||||
|
||||
static int run_test(int cgroup_fd, int server_fd, bool classid)
|
||||
{
|
||||
struct network_helper_opts opts = {
|
||||
.must_fail = true,
|
||||
};
|
||||
struct connect4_dropper *skel;
|
||||
int fd, err = 0;
|
||||
|
||||
@@ -32,11 +29,16 @@ static int run_test(int cgroup_fd, int server_fd, bool classid)
|
||||
goto out;
|
||||
}
|
||||
|
||||
fd = connect_to_fd_opts(server_fd, SOCK_STREAM, &opts);
|
||||
if (fd < 0)
|
||||
errno = 0;
|
||||
fd = connect_to_fd_opts(server_fd, NULL);
|
||||
if (fd >= 0) {
|
||||
log_err("Unexpected success to connect to server");
|
||||
err = -1;
|
||||
else
|
||||
close(fd);
|
||||
} else if (errno != EPERM) {
|
||||
log_err("Unexpected errno from connect to server");
|
||||
err = -1;
|
||||
}
|
||||
out:
|
||||
connect4_dropper__destroy(skel);
|
||||
return err;
|
||||
@@ -52,7 +54,7 @@ void test_cgroup_v1v2(void)
|
||||
server_fd = start_server(AF_INET, SOCK_STREAM, NULL, port, 0);
|
||||
if (!ASSERT_GE(server_fd, 0, "server_fd"))
|
||||
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")) {
|
||||
close(server_fd);
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user