|
|
|
@ -41,9 +41,10 @@ |
|
|
|
|
|
|
|
|
|
#include <string> |
|
|
|
|
|
|
|
|
|
#include "absl/log/log.h" |
|
|
|
|
|
|
|
|
|
#include <grpc/grpc.h> |
|
|
|
|
#include <grpc/support/alloc.h> |
|
|
|
|
#include <grpc/support/log.h> |
|
|
|
|
#include <grpc/support/sync.h> |
|
|
|
|
#include <grpc/support/time.h> |
|
|
|
|
|
|
|
|
@ -59,7 +60,7 @@ |
|
|
|
|
#include "src/core/lib/resource_quota/api.h" |
|
|
|
|
#include "test/core/test_util/port.h" |
|
|
|
|
|
|
|
|
|
#define LOG_TEST(x) gpr_log(GPR_INFO, "%s", #x) |
|
|
|
|
#define LOG_TEST(x) LOG(INFO) << #x |
|
|
|
|
|
|
|
|
|
static gpr_mu* g_mu; |
|
|
|
|
static grpc_pollset* g_pollset; |
|
|
|
@ -272,7 +273,7 @@ static grpc_error_handle tcp_connect(const test_addr* remote, |
|
|
|
|
const struct sockaddr* remote_addr = |
|
|
|
|
reinterpret_cast<const struct sockaddr*>(remote->addr.addr); |
|
|
|
|
|
|
|
|
|
gpr_log(GPR_INFO, "Connecting to %s", remote->str); |
|
|
|
|
LOG(INFO) << "Connecting to " << remote->str; |
|
|
|
|
gpr_mu_lock(g_mu); |
|
|
|
|
nconnects_before = g_nconnects; |
|
|
|
|
on_connect_result_init(&g_result); |
|
|
|
@ -281,14 +282,14 @@ static grpc_error_handle tcp_connect(const test_addr* remote, |
|
|
|
|
gpr_mu_unlock(g_mu); |
|
|
|
|
return GRPC_OS_ERROR(errno, "Failed to create socket"); |
|
|
|
|
} |
|
|
|
|
gpr_log(GPR_DEBUG, "start connect to %s", remote->str); |
|
|
|
|
VLOG(2) << "start connect to " << remote->str; |
|
|
|
|
if (connect(clifd, remote_addr, static_cast<socklen_t>(remote->addr.len)) != |
|
|
|
|
0) { |
|
|
|
|
gpr_mu_unlock(g_mu); |
|
|
|
|
close(clifd); |
|
|
|
|
return GRPC_OS_ERROR(errno, "connect"); |
|
|
|
|
} |
|
|
|
|
gpr_log(GPR_DEBUG, "wait"); |
|
|
|
|
VLOG(2) << "wait"; |
|
|
|
|
while (g_nconnects == nconnects_before && |
|
|
|
|
deadline > grpc_core::Timestamp::Now()) { |
|
|
|
|
grpc_pollset_worker* worker = nullptr; |
|
|
|
@ -303,7 +304,7 @@ static grpc_error_handle tcp_connect(const test_addr* remote, |
|
|
|
|
|
|
|
|
|
gpr_mu_lock(g_mu); |
|
|
|
|
} |
|
|
|
|
gpr_log(GPR_DEBUG, "wait done"); |
|
|
|
|
VLOG(2) << "wait done"; |
|
|
|
|
if (g_nconnects != nconnects_before + 1) { |
|
|
|
|
gpr_mu_unlock(g_mu); |
|
|
|
|
close(clifd); |
|
|
|
@ -313,8 +314,8 @@ static grpc_error_handle tcp_connect(const test_addr* remote, |
|
|
|
|
*result = g_result; |
|
|
|
|
|
|
|
|
|
gpr_mu_unlock(g_mu); |
|
|
|
|
gpr_log(GPR_INFO, "Result (%d, %d) fd %d", result->port_index, |
|
|
|
|
result->fd_index, result->server_fd); |
|
|
|
|
LOG(INFO) << "Result (" << result->port_index << ", " << result->fd_index |
|
|
|
|
<< ") fd " << result->server_fd; |
|
|
|
|
grpc_tcp_server_unref(result->server); |
|
|
|
|
return absl::OkStatus(); |
|
|
|
|
} |
|
|
|
@ -356,12 +357,10 @@ static void test_connect(size_t num_connects, |
|
|
|
|
server_weak_ref_init(&weak_ref); |
|
|
|
|
server_weak_ref_set(&weak_ref, s); |
|
|
|
|
LOG_TEST("test_connect"); |
|
|
|
|
gpr_log(GPR_INFO, |
|
|
|
|
"clients=%lu, num chan args=%lu, remote IP=%s, test_dst_addrs=%d", |
|
|
|
|
static_cast<unsigned long>(num_connects), |
|
|
|
|
static_cast<unsigned long>( |
|
|
|
|
channel_args != nullptr ? channel_args->num_args : 0), |
|
|
|
|
dst_addrs != nullptr ? "<specific>" : "::", test_dst_addrs); |
|
|
|
|
LOG(INFO) << "clients=" << num_connects << ", num chan args=" |
|
|
|
|
<< (channel_args != nullptr ? channel_args->num_args : 0) |
|
|
|
|
<< ", remote IP=" << (dst_addrs != nullptr ? "<specific>" : "::") |
|
|
|
|
<< ", test_dst_addrs=" << test_dst_addrs; |
|
|
|
|
memset(&resolved_addr, 0, sizeof(resolved_addr)); |
|
|
|
|
memset(&resolved_addr1, 0, sizeof(resolved_addr1)); |
|
|
|
|
resolved_addr.len = static_cast<socklen_t>(sizeof(struct sockaddr_storage)); |
|
|
|
@ -370,13 +369,13 @@ static void test_connect(size_t num_connects, |
|
|
|
|
ASSERT_TRUE(GRPC_LOG_IF_ERROR( |
|
|
|
|
"grpc_tcp_server_add_port", |
|
|
|
|
grpc_tcp_server_add_port(s, &resolved_addr, &svr_port))); |
|
|
|
|
gpr_log(GPR_INFO, "Allocated port %d", svr_port); |
|
|
|
|
LOG(INFO) << "Allocated port " << svr_port; |
|
|
|
|
ASSERT_GT(svr_port, 0); |
|
|
|
|
// Cannot use wildcard (port==0), because add_port() will try to reuse the
|
|
|
|
|
// same port as a previous add_port().
|
|
|
|
|
svr1_port = grpc_pick_unused_port_or_die(); |
|
|
|
|
ASSERT_GT(svr1_port, 0); |
|
|
|
|
gpr_log(GPR_INFO, "Picked unused port %d", svr1_port); |
|
|
|
|
LOG(INFO) << "Picked unused port " << svr1_port; |
|
|
|
|
grpc_sockaddr_set_port(&resolved_addr1, svr1_port); |
|
|
|
|
ASSERT_EQ(grpc_tcp_server_add_port(s, &resolved_addr1, &port), |
|
|
|
|
absl::OkStatus()); |
|
|
|
@ -410,8 +409,7 @@ static void test_connect(size_t num_connects, |
|
|
|
|
on_connect_result result; |
|
|
|
|
grpc_error_handle err; |
|
|
|
|
if (dst.addr.len == 0) { |
|
|
|
|
gpr_log(GPR_DEBUG, "Skipping test of non-functional local IP %s", |
|
|
|
|
dst.str); |
|
|
|
|
VLOG(2) << "Skipping test of non-functional local IP " << dst.str; |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
ASSERT_TRUE(grpc_sockaddr_set_port(&dst.addr, ports[port_num])); |
|
|
|
@ -422,8 +420,8 @@ static void test_connect(size_t num_connects, |
|
|
|
|
result.server_fd >= 0 && result.server == s) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
gpr_log(GPR_ERROR, "Failed to connect to %s: %s", dst.str, |
|
|
|
|
grpc_core::StatusToString(err).c_str()); |
|
|
|
|
LOG(ERROR) << "Failed to connect to " << dst.str << ": " |
|
|
|
|
<< grpc_core::StatusToString(err); |
|
|
|
|
ASSERT_TRUE(test_dst_addrs); |
|
|
|
|
dst_addrs->addrs[dst_idx].addr.len = 0; |
|
|
|
|
} |
|
|
|
@ -444,8 +442,9 @@ static void test_connect(size_t num_connects, |
|
|
|
|
0); |
|
|
|
|
ASSERT_LE(dst.addr.len, sizeof(dst.addr.addr)); |
|
|
|
|
test_addr_init_str(&dst); |
|
|
|
|
gpr_log(GPR_INFO, "(%d, %d) fd %d family %s listening on %s", port_num, |
|
|
|
|
fd_num, fd, sock_family_name(addr->ss_family), dst.str); |
|
|
|
|
LOG(INFO) << "(" << port_num << ", " << fd_num << ") fd " << fd |
|
|
|
|
<< " family " << sock_family_name(addr->ss_family) |
|
|
|
|
<< " listening on " << dst.str; |
|
|
|
|
for (connect_num = 0; connect_num < num_connects; ++connect_num) { |
|
|
|
|
on_connect_result result; |
|
|
|
|
on_connect_result_init(&result); |
|
|
|
@ -482,7 +481,7 @@ static int pre_allocate_inet_sock(grpc_tcp_server* s, int family, int port, |
|
|
|
|
|
|
|
|
|
int pre_fd = socket(address.sin6_family, SOCK_STREAM, 0); |
|
|
|
|
if (pre_fd < 0) { |
|
|
|
|
gpr_log(GPR_ERROR, "Unable to create inet socket: %m"); |
|
|
|
|
LOG(ERROR) << "Unable to create inet socket: %m"; |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -492,13 +491,13 @@ static int pre_allocate_inet_sock(grpc_tcp_server* s, int family, int port, |
|
|
|
|
int b = bind(pre_fd, reinterpret_cast<struct sockaddr*>(&address), |
|
|
|
|
sizeof(address)); |
|
|
|
|
if (b < 0) { |
|
|
|
|
gpr_log(GPR_ERROR, "Unable to bind inet socket: %m"); |
|
|
|
|
LOG(ERROR) << "Unable to bind inet socket: %m"; |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int l = listen(pre_fd, SOMAXCONN); |
|
|
|
|
if (l < 0) { |
|
|
|
|
gpr_log(GPR_ERROR, "Unable to listen on inet socket: %m"); |
|
|
|
|
LOG(ERROR) << "Unable to listen on inet socket: %m"; |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -587,20 +586,20 @@ static int pre_allocate_unix_sock(grpc_tcp_server* s, const char* path, |
|
|
|
|
|
|
|
|
|
int pre_fd = socket(address.sun_family, SOCK_STREAM, 0); |
|
|
|
|
if (pre_fd < 0) { |
|
|
|
|
gpr_log(GPR_ERROR, "Unable to create unix socket: %m"); |
|
|
|
|
LOG(ERROR) << "Unable to create unix socket: %m"; |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int b = bind(pre_fd, reinterpret_cast<struct sockaddr*>(&address), |
|
|
|
|
sizeof(address)); |
|
|
|
|
if (b < 0) { |
|
|
|
|
gpr_log(GPR_ERROR, "Unable to bind unix socket: %m"); |
|
|
|
|
LOG(ERROR) << "Unable to bind unix socket: %m"; |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int l = listen(pre_fd, SOMAXCONN); |
|
|
|
|
if (l < 0) { |
|
|
|
|
gpr_log(GPR_ERROR, "Unable to listen on unix socket: %m"); |
|
|
|
|
LOG(ERROR) << "Unable to listen on unix socket: %m"; |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -679,8 +678,8 @@ static void test_pre_allocated_unix_fd() { |
|
|
|
|
// If the path no longer exists, errno is 2. This can happen when
|
|
|
|
|
// runninig the test multiple times in parallel. Do not fail the test
|
|
|
|
|
if (absl::IsUnknown(res_conn) && res_conn.raw_code() == 2) { |
|
|
|
|
gpr_log(GPR_ERROR, |
|
|
|
|
"Unable to test pre_allocated unix socket: path does not exist"); |
|
|
|
|
LOG(ERROR) |
|
|
|
|
<< "Unable to test pre_allocated unix socket: path does not exist"; |
|
|
|
|
grpc_tcp_server_unref(s); |
|
|
|
|
close(pre_fd); |
|
|
|
|
return; |
|
|
|
|