diff --git a/source/common/network/io_uring_socket_handle_impl.h b/source/common/network/io_uring_socket_handle_impl.h index 737584bcff2e7..87721a67a9a76 100644 --- a/source/common/network/io_uring_socket_handle_impl.h +++ b/source/common/network/io_uring_socket_handle_impl.h @@ -4,13 +4,10 @@ #include "envoy/network/io_handle.h" #include "source/common/common/logger.h" +#include "source/common/io/io_uring.h" namespace Envoy { -namespace Io { -class IoUringFactory; -} // namespace Io - namespace Network { class IoUringSocketHandleImpl; diff --git a/source/common/network/socket_interface_impl.cc b/source/common/network/socket_interface_impl.cc index fa41ac7f7ea9f..936025dee2949 100644 --- a/source/common/network/socket_interface_impl.cc +++ b/source/common/network/socket_interface_impl.cc @@ -14,15 +14,6 @@ namespace Envoy { namespace Network { -namespace { - -// TODO (soulxu): making those configurable if needed. -constexpr uint32_t DefaultIoUringSize = 300; -constexpr uint32_t DefaultReadBufferSize = 8192; -constexpr bool UseSubmissionQueuePolling = false; - -} // namespace - void DefaultSocketInterfaceExtension::onServerInitialized() { if (io_uring_factory_ != nullptr) { io_uring_factory_->onServerInitialized(); @@ -60,7 +51,7 @@ IoHandlePtr SocketInterfaceImpl::socket(Socket::Type socket_type, Address::Type #else int flags = SOCK_NONBLOCK; - if (io_uring_factory_ != nullptr) { + if (io_uring_factory_.lock() != nullptr) { flags = 0; } @@ -99,7 +90,7 @@ IoHandlePtr SocketInterfaceImpl::socket(Socket::Type socket_type, Address::Type RELEASE_ASSERT(SOCKET_VALID(result.return_value_), fmt::format("socket(2) failed, got error: {}", errorDetails(result.errno_))); IoHandlePtr io_handle = - makeSocket(result.return_value_, socket_v6only, domain, io_uring_factory_.get()); + makeSocket(result.return_value_, socket_v6only, domain, io_uring_factory_.lock().get()); #if defined(__APPLE__) || defined(WIN32) // Cannot set SOCK_NONBLOCK as a ::socket flag. @@ -146,10 +137,15 @@ Server::BootstrapExtensionPtr SocketInterfaceImpl::createBootstrapExtension( const Protobuf::Message&, Server::Configuration::ServerFactoryContext& context) { // TODO (soulxu): Add runtime flag here. if (Io::isIoUringSupported()) { - io_uring_factory_ = std::make_unique( - DefaultIoUringSize, UseSubmissionQueuePolling, context.threadLocal()); + std::shared_ptr io_uring_factory = + std::make_unique(DefaultIoUringSize, UseSubmissionQueuePolling, + context.threadLocal()); + io_uring_factory_ = io_uring_factory; + + return std::make_unique(*this, io_uring_factory); + } else { + return std::make_unique(*this, nullptr); } - return std::make_unique(*this, io_uring_factory_); } ProtobufTypes::MessagePtr SocketInterfaceImpl::createEmptyConfigProto() { diff --git a/source/common/network/socket_interface_impl.h b/source/common/network/socket_interface_impl.h index 2ab101119e0f6..f0db5ff8336d0 100644 --- a/source/common/network/socket_interface_impl.h +++ b/source/common/network/socket_interface_impl.h @@ -11,14 +11,14 @@ namespace Network { class DefaultSocketInterfaceExtension : public Network::SocketInterfaceExtension { public: DefaultSocketInterfaceExtension(Network::SocketInterface& sock_interface, - std::unique_ptr& io_uring_factory) + std::shared_ptr io_uring_factory) : Network::SocketInterfaceExtension(sock_interface), io_uring_factory_(io_uring_factory) {} // Server::BootstrapExtension void onServerInitialized() override; protected: - std::unique_ptr& io_uring_factory_; + std::shared_ptr io_uring_factory_; }; class SocketInterfaceImpl : public SocketInterfaceBase { @@ -44,12 +44,17 @@ class SocketInterfaceImpl : public SocketInterfaceBase { absl::optional domain, Io::IoUringFactory* io_uring_factory = nullptr); + // TODO (soulxu): making those configurable if needed. + static constexpr uint32_t DefaultIoUringSize = 300; + static constexpr uint32_t DefaultReadBufferSize = 8192; + static constexpr bool UseSubmissionQueuePolling = false; + protected: virtual IoHandlePtr makeSocket(int socket_fd, bool socket_v6only, absl::optional domain, Io::IoUringFactory* io_uring_factory = nullptr) const; private: - std::unique_ptr io_uring_factory_; + std::weak_ptr io_uring_factory_; }; DECLARE_FACTORY(SocketInterfaceImpl);