Skip to content

Commit

Permalink
network: move socket type from address to socket (envoyproxy#11486)
Browse files Browse the repository at this point in the history
Signed-off-by: Florin Coras <[email protected]>
  • Loading branch information
florincoras authored Jun 8, 2020
1 parent 97ecef8 commit 6679d57
Show file tree
Hide file tree
Showing 46 changed files with 160 additions and 174 deletions.
1 change: 0 additions & 1 deletion include/envoy/network/address.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ class Pipe {
};

enum class Type { Ip, Pipe };
enum class SocketType { Stream, Datagram };

/**
* Interface for all network addresses.
Expand Down
2 changes: 1 addition & 1 deletion include/envoy/network/listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ListenSocketFactory {
/**
* @return the type of the socket getListenSocket() returns.
*/
virtual Address::SocketType socketType() const PURE;
virtual Socket::Type socketType() const PURE;

/**
* @return the listening address of the socket getListenSocket() returns. Before getListenSocket()
Expand Down
11 changes: 8 additions & 3 deletions include/envoy/network/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class Socket {
public:
virtual ~Socket() = default;

/**
* Type of sockets supported. See man 2 socket for more details
*/
enum class Type { Stream, Datagram };

/**
* @return the local address of the socket.
*/
Expand Down Expand Up @@ -76,7 +81,7 @@ class Socket {
/**
* @return the type (stream or datagram) of the socket.
*/
virtual Address::SocketType socketType() const PURE;
virtual Socket::Type socketType() const PURE;

/**
* @return the type (IP or pipe) of addresses used by the socket (subset of socket domain)
Expand Down Expand Up @@ -234,7 +239,7 @@ class SocketInterface {
* @param version IP version if address type is IP
* @return @ref Network::IoHandlePtr that wraps the underlying socket file descriptor
*/
virtual IoHandlePtr socket(Address::SocketType type, Address::Type addr_type,
virtual IoHandlePtr socket(Socket::Type type, Address::Type addr_type,
Address::IpVersion version) PURE;

/**
Expand All @@ -244,7 +249,7 @@ class SocketInterface {
* @param addr address that is gleaned for address type and version if needed
* @return @ref Network::IoHandlePtr that wraps the underlying socket file descriptor
*/
virtual IoHandlePtr socket(Address::SocketType socket_type,
virtual IoHandlePtr socket(Socket::Type socket_type,
const Address::InstanceConstSharedPtr addr) PURE;

/**
Expand Down
2 changes: 1 addition & 1 deletion include/envoy/server/listener_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ListenerComponentFactory {
*/
virtual Network::SocketSharedPtr
createListenSocket(Network::Address::InstanceConstSharedPtr address,
Network::Address::SocketType socket_type,
Network::Socket::Type socket_type,
const Network::Socket::OptionsSharedPtr& options,
const ListenSocketCreationParams& params) PURE;

Expand Down
8 changes: 3 additions & 5 deletions source/common/network/listen_socket_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ void ListenSocketImpl::setupSocket(const Network::Socket::OptionsSharedPtr& opti
}

template <>
void NetworkListenSocket<
NetworkSocketTrait<Address::SocketType::Stream>>::setPrebindSocketOptions() {
void NetworkListenSocket<NetworkSocketTrait<Socket::Type::Stream>>::setPrebindSocketOptions() {
// On Windows, SO_REUSEADDR does not restrict subsequent bind calls when there is a listener as on
// Linux and later BSD socket stacks
#ifndef WIN32
Expand All @@ -58,11 +57,10 @@ void NetworkListenSocket<
}

template <>
void NetworkListenSocket<
NetworkSocketTrait<Address::SocketType::Datagram>>::setPrebindSocketOptions() {}
void NetworkListenSocket<NetworkSocketTrait<Socket::Type::Datagram>>::setPrebindSocketOptions() {}

UdsListenSocket::UdsListenSocket(const Address::InstanceConstSharedPtr& address)
: ListenSocketImpl(SocketInterfaceSingleton::get().socket(Address::SocketType::Stream, address),
: ListenSocketImpl(SocketInterfaceSingleton::get().socket(Socket::Type::Stream, address),
address) {
RELEASE_ASSERT(io_handle_->fd() != -1, "");
bind(local_address_);
Expand Down
29 changes: 14 additions & 15 deletions source/common/network/listen_socket_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ class ListenSocketImpl : public SocketImpl {
/**
* Wraps a unix socket.
*/
template <Address::SocketType T> struct NetworkSocketTrait {};
template <Socket::Type T> struct NetworkSocketTrait {};

template <> struct NetworkSocketTrait<Address::SocketType::Stream> {
static constexpr Address::SocketType type = Address::SocketType::Stream;
template <> struct NetworkSocketTrait<Socket::Type::Stream> {
static constexpr Socket::Type type = Socket::Type::Stream;
};

template <> struct NetworkSocketTrait<Address::SocketType::Datagram> {
static constexpr Address::SocketType type = Address::SocketType::Datagram;
template <> struct NetworkSocketTrait<Socket::Type::Datagram> {
static constexpr Socket::Type type = Socket::Type::Datagram;
};

template <typename T> class NetworkListenSocket : public ListenSocketImpl {
Expand All @@ -58,23 +58,23 @@ template <typename T> class NetworkListenSocket : public ListenSocketImpl {
setListenSocketOptions(options);
}

Address::SocketType socketType() const override { return T::type; }
Socket::Type socketType() const override { return T::type; }

protected:
void setPrebindSocketOptions();
};

using TcpListenSocket = NetworkListenSocket<NetworkSocketTrait<Address::SocketType::Stream>>;
using TcpListenSocket = NetworkListenSocket<NetworkSocketTrait<Socket::Type::Stream>>;
using TcpListenSocketPtr = std::unique_ptr<TcpListenSocket>;

using UdpListenSocket = NetworkListenSocket<NetworkSocketTrait<Address::SocketType::Datagram>>;
using UdpListenSocket = NetworkListenSocket<NetworkSocketTrait<Socket::Type::Datagram>>;
using UdpListenSocketPtr = std::unique_ptr<UdpListenSocket>;

class UdsListenSocket : public ListenSocketImpl {
public:
UdsListenSocket(const Address::InstanceConstSharedPtr& address);
UdsListenSocket(IoHandlePtr&& io_handle, const Address::InstanceConstSharedPtr& address);
Address::SocketType socketType() const override { return Address::SocketType::Stream; }
Socket::Type socketType() const override { return Socket::Type::Stream; }
};

class ConnectionSocketImpl : public SocketImpl, public ConnectionSocket {
Expand All @@ -85,16 +85,15 @@ class ConnectionSocketImpl : public SocketImpl, public ConnectionSocket {
: SocketImpl(std::move(io_handle), local_address), remote_address_(remote_address),
direct_remote_address_(remote_address) {}

ConnectionSocketImpl(Address::SocketType type,
const Address::InstanceConstSharedPtr& local_address,
ConnectionSocketImpl(Socket::Type type, const Address::InstanceConstSharedPtr& local_address,
const Address::InstanceConstSharedPtr& remote_address)
: SocketImpl(type, local_address), remote_address_(remote_address),
direct_remote_address_(remote_address) {
setLocalAddress(local_address);
}

// Network::Socket
Address::SocketType socketType() const override { return Address::SocketType::Stream; }
Socket::Type socketType() const override { return Socket::Type::Stream; }

// Network::ConnectionSocket
const Address::InstanceConstSharedPtr& remoteAddress() const override { return remote_address_; }
Expand Down Expand Up @@ -152,9 +151,9 @@ class ClientSocketImpl : public ConnectionSocketImpl {
public:
ClientSocketImpl(const Address::InstanceConstSharedPtr& remote_address,
const OptionsSharedPtr& options)
: ConnectionSocketImpl(Network::SocketInterfaceSingleton::get().socket(
Address::SocketType::Stream, remote_address),
nullptr, remote_address) {
: ConnectionSocketImpl(
Network::SocketInterfaceSingleton::get().socket(Socket::Type::Stream, remote_address),
nullptr, remote_address) {
if (options) {
addOptions(options);
}
Expand Down
5 changes: 2 additions & 3 deletions source/common/network/socket_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
namespace Envoy {
namespace Network {

SocketImpl::SocketImpl(Address::SocketType type, Address::Type addr_type,
Address::IpVersion version)
SocketImpl::SocketImpl(Socket::Type type, Address::Type addr_type, Address::IpVersion version)
: io_handle_(SocketInterfaceSingleton::get().socket(type, addr_type, version)) {}

SocketImpl::SocketImpl(Address::SocketType sock_type, const Address::InstanceConstSharedPtr addr)
SocketImpl::SocketImpl(Socket::Type sock_type, const Address::InstanceConstSharedPtr addr)
: io_handle_(SocketInterfaceSingleton::get().socket(sock_type, addr)), sock_type_(sock_type),
addr_type_(addr->type()) {}

Expand Down
8 changes: 4 additions & 4 deletions source/common/network/socket_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace Network {

class SocketImpl : public virtual Socket {
public:
SocketImpl(Address::SocketType type, Address::Type addr_type, Address::IpVersion version);
SocketImpl(Address::SocketType socket_type, const Address::InstanceConstSharedPtr addr);
SocketImpl(Socket::Type type, Address::Type addr_type, Address::IpVersion version);
SocketImpl(Socket::Type socket_type, const Address::InstanceConstSharedPtr addr);

// Network::Socket
const Address::InstanceConstSharedPtr& localAddress() const override { return local_address_; }
Expand Down Expand Up @@ -50,7 +50,7 @@ class SocketImpl : public virtual Socket {
Api::SysCallIntResult setBlockingForTest(bool blocking) override;

const OptionsSharedPtr& options() const override { return options_; }
Address::SocketType socketType() const override { return sock_type_; }
Socket::Type socketType() const override { return sock_type_; }
Address::Type addressType() const override { return addr_type_; }

protected:
Expand All @@ -59,7 +59,7 @@ class SocketImpl : public virtual Socket {
const IoHandlePtr io_handle_;
Address::InstanceConstSharedPtr local_address_;
OptionsSharedPtr options_;
Address::SocketType sock_type_;
Socket::Type sock_type_;
Address::Type addr_type_;
};

Expand Down
6 changes: 3 additions & 3 deletions source/common/network/socket_interface_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
namespace Envoy {
namespace Network {

IoHandlePtr SocketInterfaceImpl::socket(Address::SocketType socket_type, Address::Type addr_type,
IoHandlePtr SocketInterfaceImpl::socket(Socket::Type socket_type, Address::Type addr_type,
Address::IpVersion version) {
#if defined(__APPLE__) || defined(WIN32)
int flags = 0;
#else
int flags = SOCK_NONBLOCK;
#endif

if (socket_type == Address::SocketType::Stream) {
if (socket_type == Socket::Type::Stream) {
flags |= SOCK_STREAM;
} else {
flags |= SOCK_DGRAM;
Expand Down Expand Up @@ -51,7 +51,7 @@ IoHandlePtr SocketInterfaceImpl::socket(Address::SocketType socket_type, Address
return io_handle;
}

IoHandlePtr SocketInterfaceImpl::socket(Address::SocketType socket_type,
IoHandlePtr SocketInterfaceImpl::socket(Socket::Type socket_type,
const Address::InstanceConstSharedPtr addr) {
Address::IpVersion ip_version = addr->ip() ? addr->ip()->version() : Address::IpVersion::v4;
IoHandlePtr io_handle = SocketInterfaceImpl::socket(socket_type, addr->type(), ip_version);
Expand Down
5 changes: 2 additions & 3 deletions source/common/network/socket_interface_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ namespace Network {

class SocketInterfaceImpl : public SocketInterface {
public:
IoHandlePtr socket(Address::SocketType socket_type, Address::Type addr_type,
IoHandlePtr socket(Socket::Type socket_type, Address::Type addr_type,
Address::IpVersion version) override;
IoHandlePtr socket(Address::SocketType socket_type,
const Address::InstanceConstSharedPtr addr) override;
IoHandlePtr socket(Socket::Type socket_type, const Address::InstanceConstSharedPtr addr) override;
bool ipFamilySupported(int domain) override;
Address::InstanceConstSharedPtr addressFromFd(os_fd_t fd) override;
Address::InstanceConstSharedPtr peerAddressFromFd(os_fd_t fd) override;
Expand Down
8 changes: 4 additions & 4 deletions source/common/network/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -472,22 +472,22 @@ void Utility::addressToProtobufAddress(const Address::Instance& address,
}
}

Address::SocketType
Socket::Type
Utility::protobufAddressSocketType(const envoy::config::core::v3::Address& proto_address) {
switch (proto_address.address_case()) {
case envoy::config::core::v3::Address::AddressCase::kSocketAddress: {
const auto protocol = proto_address.socket_address().protocol();
switch (protocol) {
case envoy::config::core::v3::SocketAddress::TCP:
return Address::SocketType::Stream;
return Socket::Type::Stream;
case envoy::config::core::v3::SocketAddress::UDP:
return Address::SocketType::Datagram;
return Socket::Type::Datagram;
default:
NOT_REACHED_GCOVR_EXCL_LINE;
}
}
case envoy::config::core::v3::Address::AddressCase::kPipe:
return Address::SocketType::Stream;
return Socket::Type::Stream;
default:
NOT_REACHED_GCOVR_EXCL_LINE;
}
Expand Down
2 changes: 1 addition & 1 deletion source/common/network/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ class Utility {
* @param proto_address the address protobuf
* @return socket type
*/
static Address::SocketType
static Socket::Type
protobufAddressSocketType(const envoy::config::core::v3::Address& proto_address);

/**
Expand Down
2 changes: 1 addition & 1 deletion source/extensions/filters/udp/udp_proxy/udp_proxy_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class UdpProxyFilter : public Network::UdpListenerReadFilter,

virtual Network::IoHandlePtr createIoHandle(const Upstream::HostConstSharedPtr& host) {
// Virtual so this can be overridden in unit tests.
return Network::SocketInterfaceSingleton::get().socket(Network::Address::SocketType::Datagram,
return Network::SocketInterfaceSingleton::get().socket(Network::Socket::Type::Datagram,
host->address());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ createConnectionSocket(Network::Address::InstanceConstSharedPtr& peer_addr,
Network::Address::InstanceConstSharedPtr& local_addr,
const Network::ConnectionSocket::OptionsSharedPtr& options) {
auto connection_socket = std::make_unique<Network::ConnectionSocketImpl>(
Network::Address::SocketType::Datagram, local_addr, peer_addr);
Network::Socket::Type::Datagram, local_addr, peer_addr);
connection_socket->addOptions(Network::SocketOptionFactory::buildIpPacketInfoOptions());
connection_socket->addOptions(Network::SocketOptionFactory::buildRxQueueOverFlowOptions());
if (options != nullptr) {
Expand Down
2 changes: 1 addition & 1 deletion source/extensions/stat_sinks/common/statsd/statsd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Statsd {

UdpStatsdSink::WriterImpl::WriterImpl(UdpStatsdSink& parent)
: parent_(parent), io_handle_(Network::SocketInterfaceSingleton::get().socket(
Network::Address::SocketType::Datagram, parent_.server_address_)) {}
Network::Socket::Type::Datagram, parent_.server_address_)) {}

void UdpStatsdSink::WriterImpl::write(const std::string& message) {
// TODO(mattklein123): We can avoid this const_cast pattern by having a constant variant of
Expand Down
4 changes: 2 additions & 2 deletions source/extensions/tracers/xray/daemon_broker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ std::string createHeader(const std::string& format, uint32_t version) {

DaemonBrokerImpl::DaemonBrokerImpl(const std::string& daemon_endpoint)
: address_(Network::Utility::parseInternetAddressAndPort(daemon_endpoint, false /*v6only*/)),
io_handle_(Network::SocketInterfaceSingleton::get().socket(
Network::Address::SocketType::Datagram, address_)) {}
io_handle_(Network::SocketInterfaceSingleton::get().socket(Network::Socket::Type::Datagram,
address_)) {}

void DaemonBrokerImpl::send(const std::string& data) const {
auto& logger = Logger::Registry::getLog(Logger::Id::tracing);
Expand Down
2 changes: 1 addition & 1 deletion source/server/admin/admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ class AdminImpl : public Admin,
AdminListenSocketFactory(Network::SocketSharedPtr socket) : socket_(socket) {}

// Network::ListenSocketFactory
Network::Address::SocketType socketType() const override { return socket_->socketType(); }
Network::Socket::Type socketType() const override { return socket_->socketType(); }

const Network::Address::InstanceConstSharedPtr& localAddress() const override {
return socket_->localAddress();
Expand Down
2 changes: 1 addition & 1 deletion source/server/config_validation/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class ValidationInstance final : Logger::Loggable<Logger::Id::main>,
return ProdListenerComponentFactory::createUdpListenerFilterFactoryList_(filters, context);
}
Network::SocketSharedPtr createListenSocket(Network::Address::InstanceConstSharedPtr,
Network::Address::SocketType,
Network::Socket::Type,
const Network::Socket::OptionsSharedPtr&,
const ListenSocketCreationParams&) override {
// Returned sockets are not currently used so we can return nothing here safely vs. a
Expand Down
2 changes: 1 addition & 1 deletion source/server/connection_handler_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void ConnectionHandlerImpl::decNumConnections() {
void ConnectionHandlerImpl::addListener(absl::optional<uint64_t> overridden_listener,
Network::ListenerConfig& config) {
ActiveListenerDetails details;
if (config.listenSocketFactory().socketType() == Network::Address::SocketType::Stream) {
if (config.listenSocketFactory().socketType() == Network::Socket::Type::Stream) {
if (overridden_listener.has_value()) {
for (auto& listener : listeners_) {
if (listener.second.listener_->listenerTag() == overridden_listener) {
Expand Down
Loading

0 comments on commit 6679d57

Please sign in to comment.