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

Prototype: filter chain manager replaceable listener #5

Open
wants to merge 1 commit into
base: splitlistenerimpl
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/envoy/network/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ class FilterChainManager {
virtual const FilterChain* findFilterChain(const ConnectionSocket& socket) const PURE;
};

using FilterChainManagerSharedPtr = std::shared_ptr<FilterChainManager>;

/**
* Callbacks used by individual UDP listener read filter instances to communicate with the filter
* manager.
Expand Down
6 changes: 6 additions & 0 deletions include/envoy/network/listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class ListenerConfig {
*/
virtual FilterChainManager& filterChainManager() PURE;

/**
* @return FilterChainManagerSharedPtr the factory for adding and searching through configured
* filter chains.
*/
virtual FilterChainManagerSharedPtr sharedFilterChainManager() PURE;

/**
* @return FilterChainFactory& the factory for setting up the filter chain on a new
* connection.
Expand Down
21 changes: 12 additions & 9 deletions source/server/connection_handler_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ ConnectionHandlerImpl::ActiveTcpListener::ActiveTcpListener(ConnectionHandlerImp
Network::ListenerPtr&& listener,
Network::ListenerConfig& config)
: ConnectionHandlerImpl::ActiveListenerImplBase(parent, std::move(listener), config),
parent_(parent) {
parent_(parent), active_filter_chain_manager_(config.sharedFilterChainManager()) {
config.connectionBalancer().registerHandler(*this);
}

Expand Down Expand Up @@ -267,7 +267,7 @@ void ConnectionHandlerImpl::ActiveTcpSocket::newConnection() {
Extensions::TransportSockets::TransportProtocolNames::get().RawBuffer);
}
// Create a new connection on this listener.
listener_.newConnection(std::move(socket_));
listener_.newConnection(*this);
}
}

Expand Down Expand Up @@ -303,21 +303,24 @@ void ConnectionHandlerImpl::ActiveTcpListener::onAcceptWorker(
}

void ConnectionHandlerImpl::ActiveTcpListener::newConnection(
Network::ConnectionSocketPtr&& socket) {
ConnectionHandlerImpl::ActiveTcpSocket& tcp_socket) {
// Find matching filter chain.
const auto filter_chain = config_.filterChainManager().findFilterChain(*socket);
// TODO(socket)
const auto filter_chain =
tcp_socket.snapped_filter_chain_manager_->findFilterChain(*tcp_socket.socket_);
if (filter_chain == nullptr) {
ENVOY_LOG(debug, "closing connection: no matching filter chain found");
stats_.no_filter_chain_match_.inc();
socket->close();
tcp_socket.socket_->close();
return;
}

auto transport_socket = filter_chain->transportSocketFactory().createTransportSocket(nullptr);
ActiveTcpConnectionPtr active_connection(new ActiveTcpConnection(
*this,
parent_.dispatcher_.createServerConnection(std::move(socket), std::move(transport_socket)),
parent_.dispatcher_.timeSource()));
ActiveTcpConnectionPtr active_connection(
new ActiveTcpConnection(*this,
parent_.dispatcher_.createServerConnection(
std::move(tcp_socket.socket_), std::move(transport_socket)),
parent_.dispatcher_.timeSource()));
active_connection->connection_->setBufferLimits(config_.perConnectionBufferLimitBytes());

const bool empty_filter_chain = !config_.filterChainFactory().createNetworkFilterChain(
Expand Down
14 changes: 11 additions & 3 deletions source/server/connection_handler_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,18 @@ class ConnectionHandlerImpl : public Network::ConnectionHandler,
/**
* Create a new connection from a socket accepted by the listener.
*/
void newConnection(Network::ConnectionSocketPtr&& socket);
void newConnection(ActiveTcpSocket& socket);

void
updateFilterChainManager(const Network::FilterChainManagerSharedPtr& new_filter_chain_manager);

ConnectionHandlerImpl& parent_;
// Sockets going through listener filter chain
std::list<ActiveTcpSocketPtr> sockets_;
// Connections completed listener filter chain and currently going through network filter chain
std::list<ActiveTcpConnectionPtr> connections_;

// The filter chain manager which should serve the new connections.
Network::FilterChainManagerSharedPtr active_filter_chain_manager_;
// The number of connections currently active on this listener. This is typically used for
// connection balancing across per-handler listeners.
std::atomic<uint64_t> num_listener_connections_{};
Expand Down Expand Up @@ -185,7 +191,8 @@ class ConnectionHandlerImpl : public Network::ConnectionHandler,
bool hand_off_restored_destination_connections)
: listener_(listener), socket_(std::move(socket)),
hand_off_restored_destination_connections_(hand_off_restored_destination_connections),
iter_(accept_filters_.end()) {
iter_(accept_filters_.end()),
snapped_filter_chain_manager_(listener.active_filter_chain_manager_) {
listener_.stats_.downstream_pre_cx_active_.inc();
}
~ActiveTcpSocket() override {
Expand Down Expand Up @@ -224,6 +231,7 @@ class ConnectionHandlerImpl : public Network::ConnectionHandler,
const bool hand_off_restored_destination_connections_;
std::list<Network::ListenerFilterPtr> accept_filters_;
std::list<Network::ListenerFilterPtr>::iterator iter_;
Network::FilterChainManagerSharedPtr snapped_filter_chain_manager_;
Event::TimerPtr timer_;
};

Expand Down
6 changes: 6 additions & 0 deletions source/server/http/admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ class AdminImpl : public Admin,

// Network::ListenerConfig
Network::FilterChainManager& filterChainManager() override { return parent_; }
// TODO: replace this if the interface is agreed
Network::FilterChainManagerSharedPtr sharedFilterChainManager() override {
return Network::FilterChainManagerSharedPtr(&parent_,
// A fake deleter
[](auto) {});
}
Network::FilterChainFactory& filterChainFactory() override { return parent_; }
Network::Socket& socket() override { return parent_.mutable_socket(); }
const Network::Socket& socket() const override { return parent_.mutable_socket(); }
Expand Down
4 changes: 2 additions & 2 deletions source/server/listener_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ ListenerImpl::ListenerImpl(const envoy::api::v2::Listener& config, const std::st
bool workers_started, uint64_t hash,
ProtobufMessage::ValidationVisitor& validation_visitor)
: parent_(parent), address_(Network::Address::resolveProtoAddress(config.address())),
filter_chain_manager_(address_),
filter_chain_manager_(std::make_shared<FilterChainManagerImpl>(address_)),
socket_type_(Network::Utility::protobufAddressSocketType(config.address())),
global_scope_(parent_.server_.stats().createScope("")),
listener_scope_(
Expand Down Expand Up @@ -123,7 +123,7 @@ ListenerImpl::ListenerImpl(const envoy::api::v2::Listener& config, const std::st
parent_.server_.threadLocal(), validation_visitor, parent_.server_.api());
factory_context.setInitManager(initManager());
ListenerFilterChainFactoryBuilder builder(*this, factory_context);
filter_chain_manager_.addFilterChain(config.filter_chains(), builder);
filter_chain_manager_->addFilterChain(config.filter_chains(), builder);

if (socket_type_ == Network::Address::SocketType::Datagram) {
return;
Expand Down
7 changes: 5 additions & 2 deletions source/server/listener_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ class ListenerImpl : public Network::ListenerConfig,
const std::string& versionInfo() { return version_info_; }

// Network::ListenerConfig
Network::FilterChainManager& filterChainManager() override { return filter_chain_manager_; }
Network::FilterChainManager& filterChainManager() override { return *filter_chain_manager_; }
Network::FilterChainManagerSharedPtr sharedFilterChainManager() override {
return filter_chain_manager_;
}
Network::FilterChainFactory& filterChainFactory() override { return *this; }
Network::Socket& socket() override { return *socket_; }
const Network::Socket& socket() const override { return *socket_; }
Expand Down Expand Up @@ -160,7 +163,7 @@ class ListenerImpl : public Network::ListenerConfig,

ListenerManagerImpl& parent_;
Network::Address::InstanceConstSharedPtr address_;
FilterChainManagerImpl filter_chain_manager_;
std::shared_ptr<FilterChainManagerImpl> filter_chain_manager_;

Network::Address::SocketType socket_type_;
Network::SocketSharedPtr socket_;
Expand Down