Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utils to create and prepare socket for tcp client #31009

Merged
merged 16 commits into from
Sep 22, 2022
Merged
73 changes: 72 additions & 1 deletion src/core/lib/event_engine/posix_engine/tcp_socket_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include <grpc/support/port_platform.h>

#include "src/core/lib/event_engine/posix_engine/tcp_socket_utils.h"

#include <arpa/inet.h>
#include <errno.h>
#include <inttypes.h>
Expand Down Expand Up @@ -49,13 +51,23 @@
#include <grpc/impl/codegen/grpc_types.h>
#include <grpc/support/log.h>

#include "src/core/lib/event_engine/posix_engine/tcp_socket_utils.h"
#include "src/core/lib/gprpp/host_port.h"

#ifdef GRPC_HAVE_UNIX_SOCKET
#include <sys/un.h>
#endif

// If expr evaluates to absl Not-OK status, then execute expr2 and return the
// evaluated status.
#define RETURN_IF_ERROR(expr, expr2) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The single-argument version is more general. I'd recommend keeping the absl::cleanup as you had it before, and standardizing on a general solution that works across gRPC.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved it to src/core/lib/gprpp/status_helper.h file now.

do { \
const absl::Status status = (expr); \
if (!status.ok()) { \
(expr2); \
return status; \
} \
} while (0)

namespace grpc_event_engine {
namespace posix_engine {

Expand Down Expand Up @@ -100,6 +112,23 @@ int CreateSocket(std::function<int(int, int, int)> socket_factory, int family,

const uint8_t kV4MappedPrefix[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff};

absl::Status PrepareTcpClientSocket(PosixSocketWrapper sock,
const EventEngine::ResolvedAddress& addr,
const PosixTcpOptions& options) {
RETURN_IF_ERROR(sock.SetSocketNonBlocking(1), close(sock.Fd()));
RETURN_IF_ERROR(sock.SetSocketCloexec(1), close(sock.Fd()));

if (reinterpret_cast<const sockaddr*>(addr.address())->sa_family != AF_UNIX) {
// If its not a unix socket address.
RETURN_IF_ERROR(sock.SetSocketLowLatency(1), close(sock.Fd()));
RETURN_IF_ERROR(sock.SetSocketReuseAddr(1), close(sock.Fd()));
sock.TrySetSocketTcpUserTimeout(options, true);
}
RETURN_IF_ERROR(sock.SetSocketNoSigpipeIfPossible(), close(sock.Fd()));
return sock.ApplySocketMutatorInOptions(GRPC_FD_CLIENT_CONNECTION_USAGE,
options);
}

#endif /* GRPC_POSIX_SOCKET_UTILS_COMMON */

} // namespace
Expand Down Expand Up @@ -771,6 +800,40 @@ absl::StatusOr<PosixSocketWrapper> PosixSocketWrapper::CreateDualStackSocket(
return PosixSocketWrapper(newfd);
}

absl::StatusOr<PosixSocketWrapper>
PosixSocketWrapper::CreateAndPrepareTcpClientSocket(
const PosixTcpOptions& options,
const EventEngine::ResolvedAddress& target_addr,
EventEngine::ResolvedAddress& output_mapped_target_addr) {
PosixSocketWrapper::DSMode dsmode;
absl::StatusOr<PosixSocketWrapper> posix_socket_wrapper;

// Use dualstack sockets where available. Set mapped to v6 or v4 mapped to v6.
if (!SockaddrToV4Mapped(&target_addr, &output_mapped_target_addr)) {
// addr is v4 mapped to v6 or just v6.
output_mapped_target_addr = target_addr;
}
posix_socket_wrapper = PosixSocketWrapper::CreateDualStackSocket(
nullptr, output_mapped_target_addr, SOCK_STREAM, 0, dsmode);
if (!posix_socket_wrapper.ok()) {
return posix_socket_wrapper;
}

if (dsmode == PosixSocketWrapper::DSMode::DSMODE_IPV4) {
// Original addr is either v4 or v4 mapped to v6. Set mapped_addr to v4.
if (!SockaddrIsV4Mapped(&target_addr, &output_mapped_target_addr)) {
output_mapped_target_addr = target_addr;
}
}

auto error = PrepareTcpClientSocket(*posix_socket_wrapper,
output_mapped_target_addr, options);
if (!error.ok()) {
return error;
}
return *posix_socket_wrapper;
}

#else /* GRPC_POSIX_SOCKET_UTILS_COMMON */

bool SockaddrIsV4Mapped(const EventEngine::ResolvedAddress* /*resolved_addr*/,
Expand Down Expand Up @@ -875,6 +938,14 @@ PosixSocketWrapper::CreateDualStackSocket(
int /*protocol*/, DSMode& /*dsmode*/) {
GPR_ASSERT(false && "unimplemented");
}

absl::StatusOr<PosixSocketWrapper>
PosixSocketWrapper::CreateAndPrepareTcpClientSocket(
const PosixTcpOptions& /*options*/,
const EventEngine::ResolvedAddress& /*target_addr*/,
EventEngine::ResolvedAddress& /*output_mapped_target_addr*/) {
GPR_ASSERT(false && "unimplemented");
}
}

#endif /* GRPC_POSIX_SOCKET_UTILS_COMMON */
Expand Down
16 changes: 16 additions & 0 deletions src/core/lib/event_engine/posix_engine/tcp_socket_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,22 @@ class PosixSocketWrapper {
const experimental::EventEngine::ResolvedAddress& addr, int type,
int protocol, DSMode& dsmode);

// Return a PosixSocketWrapper which manages a configured, unbound,
// unconnected TCP client fd.
// options: may contain custom tcp settings for the fd.
// target_addr: the destination address.
// output_mapped_target_addr: A out parameter. It is target_addr mapped to an
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can output_mapped_target_addr be replaced by a member of the PosixSocketWrapper? Should it always map to PeerAddress? It would be a cleaner API to have either a return value or output parameters, not both.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to keep the memory footprint of PosixSocketWrapper very small - currently it has only one member variable i.e the socket descriptor and all the operations it supports are stateless. This allows creating socket wrappers at will and using them to manipulate socket flags. The peer address is requested only once and I dont think we should be storing it in the socket wrapper. We can store it in the endpoint code that requests for it.

Shall I modify this to return an absl::StatusOr<std::tuple<PosixSocketWrapper, ResolvedAddress>> instead ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. In that case, I'd suggest a return struct, per https://abseil.io/tips/176

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

// address appropriate to the type of socket FD created. For example, if
// target_addr is IPv4 and dual stack sockets are available,
// output_mapped_target_addr will be an IPv4-mapped IPv6 address.
//
// Returns: Not-OK status on error. Out parameters are not set on error.
//
static absl::StatusOr<PosixSocketWrapper> CreateAndPrepareTcpClientSocket(
const PosixTcpOptions& options,
const EventEngine::ResolvedAddress& target_addr,
EventEngine::ResolvedAddress& output_mapped_target_addr);

private:
int fd_;
};
Expand Down