Skip to content
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
2fb53f1
listener_impl: Refactor for listener filters.
jrajahalme Aug 26, 2017
11766c4
listener: Add filter framework.
jrajahalme Aug 25, 2017
ee60ccf
Merge branch 'master' into listener-filters
jrajahalme Jan 11, 2018
6874568
Style fixes.
jrajahalme Jan 11, 2018
bc51bf8
test: Add missing 'override' keyword.
jrajahalme Jan 11, 2018
0a8fd6f
test: Remove unused 'this' lambda capture.
jrajahalme Jan 11, 2018
da0c39b
test: Fix format.
jrajahalme Jan 11, 2018
044a089
filter/original_dst: Increase log level.
jrajahalme Jan 12, 2018
d97257a
listener filters: Implement proto initializers.
jrajahalme Jan 12, 2018
efdf2f1
Merge branch 'master' into listener-filters
jrajahalme Jan 13, 2018
31ea8a0
Merge branch 'master' into listener-filters
jrajahalme Jan 13, 2018
c6f4fb2
test: Add Listener filter mocks.
jrajahalme Jan 13, 2018
a7845db
listener: Use ListenerFilter type, drop v1 support.
jrajahalme Jan 13, 2018
f9c5650
include: Fix comments.
jrajahalme Jan 13, 2018
d42ce02
dispatcher: Rename createConnection() as createServerConnection().
jrajahalme Jan 13, 2018
aa81333
Listener: Rename AcceptSocket class as AcceptedSocket
jrajahalme Jan 13, 2018
add8512
Review fixes.
jrajahalme Jan 13, 2018
c32fcaa
listener: Use unique_ptr for listener filters.
jrajahalme Jan 13, 2018
818858a
network: Use "Network" in function names for network filters.
jrajahalme Jan 13, 2018
228ac19
listener_manager_impl_test: Add OriginalDstFilter test to increase co…
jrajahalme Jan 13, 2018
0703ecc
connection_handler_impl: Fix doc comments.
jrajahalme Jan 13, 2018
84854b2
test: Fix format.
jrajahalme Jan 15, 2018
ad6f413
Merge branch 'master' into listener-filters
jrajahalme Jan 16, 2018
d4b2140
listen_socket_impl: Check if local address really changed.
jrajahalme Jan 15, 2018
597d05a
connection_handler: Check for local address reset after all listener …
jrajahalme Jan 15, 2018
1ad7886
listen_socket: Remove explicit close().
jrajahalme Jan 15, 2018
8bd512f
proxy_protocol: Do not reset addresses when "UNKNOWN"
jrajahalme Jan 15, 2018
dca40d8
listen_socket: Remove clearReset() member from AcceptedSocket
jrajahalme Jan 15, 2018
ab098f3
Merge branch 'master' into listener-filters
jrajahalme Jan 17, 2018
d0ad3d5
filter: Move listener filters to 'listener' subdirectory.
jrajahalme Jan 17, 2018
7f77eb0
filter: Move listener filters to 'Filter::Listener' namespace
jrajahalme Jan 17, 2018
4d72b93
connection_impl: Take ConnectionSocket as an argument.
jrajahalme Jan 17, 2018
07629cb
filter: Avoid using bare pointer.
jrajahalme Jan 17, 2018
de46c3b
Fix comments, add TODO.
jrajahalme Jan 17, 2018
f047852
Merge branch 'master' into listener-filters
jrajahalme Jan 17, 2018
13cba72
listen_socket: Add comment on ClientSocket.
jrajahalme Jan 17, 2018
7d82bbe
config_schemas: Revert remaining v1 changes for listener filters.
jrajahalme Jan 17, 2018
61da63f
Review fixes.
jrajahalme Jan 18, 2018
3503579
listener_manager: Translate legacy flags to listener filters.
jrajahalme Jan 18, 2018
b7e16d3
proxy_protocol: Reduce logging level.
jrajahalme Jan 19, 2018
e75f50d
connection_handler: Do not assert for listener filter completion.
jrajahalme Jan 20, 2018
9a2c62a
proxy_protocol: Release file event when done.
jrajahalme Jan 20, 2018
94751c0
proxy_protocol_test: Make tests more robust.
jrajahalme Jan 20, 2018
1021213
Merge branch 'master' into listener-filters
jrajahalme Jan 23, 2018
a390886
listener: Deprecate 'use_proxy_protocol' boolean from the v2 LDS API
jrajahalme Jan 23, 2018
ae07e0f
Review fixes.
jrajahalme Jan 23, 2018
393e2bc
Merge branch 'master' into listener-filters
jrajahalme Jan 23, 2018
f5c19a9
Last nits.
jrajahalme Jan 23, 2018
6b3e81d
Remame handOffRestoredDestinations() as handOffRestoredDestinationCon…
jrajahalme Jan 23, 2018
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
4 changes: 2 additions & 2 deletions bazel/repository_locations.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ REPOSITORY_LOCATIONS = dict(
urls = ["https://github.com/google/protobuf/archive/v3.5.0.tar.gz"],
),
envoy_api = dict(
commit = "0811371f1738a2e5f0cb594b500e9db3b5bd8af8",
remote = "https://github.com/envoyproxy/data-plane-api",
commit = "362031f5b716b6019b1ec9c6df2bc41f0eec25a4",
remote = "https://github.com/jrajahalme/envoy-api",
),
grpc_httpjson_transcoding = dict(
commit = "e4f58aa07b9002befa493a0a82e10f2e98b51fc6",
Expand Down
36 changes: 14 additions & 22 deletions include/envoy/event/dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ class Dispatcher {
*/
virtual void clearDeferredDeleteList() PURE;

/**
* Create a server connection.
* @param socket supplies an accepted socket with an open file descriptor and connection metadata
* to use for the connection. Takes ownership of the socket.
* @param ssl_ctx supplies the SSL context to use, if not nullptr.
* @return Network::ConnectionPtr a server connection that is owned by the caller.
*/
virtual Network::ConnectionPtr createServerConnection(Network::AcceptedSocketPtr&& socket,
Ssl::Context* ssl_ctx) PURE;

/**
* Create a client connection.
* @param address supplies the address to connect to.
Expand Down Expand Up @@ -78,32 +88,14 @@ class Dispatcher {

/**
* Create a listener on a specific port.
* @param conn_handler supplies the handler for connections received by the listener
* @param socket supplies the socket to listen on.
* @param cb supplies the callbacks to invoke for listener events.
* @param scope supplies the Stats::Scope to use.
* @param listener_options listener configuration options.
* @return Network::ListenerPtr a new listener that is owned by the caller.
*/
virtual Network::ListenerPtr
createListener(Network::ConnectionHandler& conn_handler, Network::ListenSocket& socket,
Network::ListenerCallbacks& cb, Stats::Scope& scope,
const Network::ListenerOptions& listener_options) PURE;

/**
* Create a listener on a specific port.
* @param conn_handler supplies the handler for connections received by the listener
* @param ssl_ctx supplies the SSL context to use.
* @param socket supplies the socket to listen on.
* @param cb supplies the callbacks to invoke for listener events.
* @param scope supplies the Stats::Scope to use.
* @param listener_options listener configuration options.
* @param bind_to_port controls whether the listener binds to a transport port or not.
* @return Network::ListenerPtr a new listener that is owned by the caller.
*/
virtual Network::ListenerPtr
createSslListener(Network::ConnectionHandler& conn_handler, Ssl::ServerContext& ssl_ctx,
Network::ListenSocket& socket, Network::ListenerCallbacks& cb,
Stats::Scope& scope, const Network::ListenerOptions& listener_options) PURE;
virtual Network::ListenerPtr createListener(Network::ListenSocket& socket,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please fix doc comments above

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK, sorry for the sloppiness...

Network::ListenerCallbacks& cb,
bool bind_to_port) PURE;

/**
* Allocate a timer. @see Event::Timer for docs on how to use the timer.
Expand Down
1 change: 1 addition & 0 deletions include/envoy/network/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ envoy_cc_library(
envoy_cc_library(
name = "listener_interface",
hdrs = ["listener.h"],
deps = ["//include/envoy/network:listen_socket_interface"],
)

envoy_cc_library(
Expand Down
23 changes: 2 additions & 21 deletions include/envoy/network/connection_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,9 @@ class ConnectionHandler {

/**
* Adds listener to the handler.
* @param factory supplies the configuration factory for new connections.
* @param socket supplies the already bound socket to listen on.
* @param scope supplies the stats scope to use for listener specific stats.
* @param listener_tag supplies an opaque tag that can be used to stop or remove the listener.
* @param listener_options listener configuration options.
* @param config listener configuration options.
*/
virtual void addListener(Network::FilterChainFactory& factory, Network::ListenSocket& socket,
Stats::Scope& scope, uint64_t listener_tag,
const Network::ListenerOptions& listener_options) PURE;

/**
* Adds listener to the handler.
* @param factory supplies the configuration factory for new connections.
* @param socket supplies the already bound socket to listen on.
* @param scope supplies the stats scope to use for listener specific stats.
* @param listener_tag supplies an opaque tag that can be used to stop or remove the listener.
* @param listener_options listener configuration options.
*/
virtual void addSslListener(Network::FilterChainFactory& factory, Ssl::ServerContext& ssl_ctx,
Network::ListenSocket& socket, Stats::Scope& scope,
uint64_t listener_tag,
const Network::ListenerOptions& listener_options) PURE;
virtual void addListener(ListenerConfig& config) PURE;

/**
* Find a listener based on the provided listener address value.
Expand Down
73 changes: 71 additions & 2 deletions include/envoy/network/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Envoy {
namespace Network {

class Connection;
class AcceptedSocket;

/**
* Status codes returned by filters that can cause future filters to not get iterated to.
Expand Down Expand Up @@ -147,6 +148,67 @@ class FilterManager {
virtual bool initializeReadFilters() PURE;
};

/**
* Callbacks used by individual listener filter instances to communicate with the listener filter
* manager.
*/
class ListenerFilterCallbacks {
public:
virtual ~ListenerFilterCallbacks() {}

/**
* @return the AcceptedSocket that owns this manager and the managed filters.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: "that owns this manager and the managed filters" is a little confusing from the filter perspective. Can you reword?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reworded as "@return AcceptedSocket the socket the filter is operating on."

*/
virtual AcceptedSocket& socket() PURE;

/**
* @return the Dispatcher for this manager.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"for this manager" doesn't really apply in filter context. I would reword.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This better: "@return the Dispatcher for issuing events."?

*/
virtual Event::Dispatcher& dispatcher() PURE;

/**
* If a filter stopped filter iteration by returning FilterStatus::StopIteration,
* the filter should call continueFilterChain(true) when complete to continue the filter chain,
* or continueFilterChain(false) if the filter execution failed and the connection must be
* closed.
* @param success boolean telling whether the filter execution was successful or not.
*/
virtual void continueFilterChain(bool success) PURE;
};

/**
* Listener Filter
*/
class ListenerFilter {
public:
virtual ~ListenerFilter() {}

/**
* Called when a new connection is accepted, but before a Connection is created.
* Filter chain iteration can be stopped if needed.
* @param cb the callbacks the filter instance can use to communicate with the filter chain.
* @return status used by the filter manager to manage further filter iteration.
*/
virtual FilterStatus onAccept(ListenerFilterCallbacks& cb) PURE;
};

typedef std::unique_ptr<ListenerFilter> ListenerFilterPtr;

/**
* Interface for filter callbacks and adding listener filters to a manager.
*/
class ListenerFilterManager {
public:
virtual ~ListenerFilterManager() {}

/**
* Add a filter to the listener. Filters are invoked in FIFO order (the filter added
* first is called first).
* @param filter supplies the filter being added.
*/
virtual void addAcceptFilter(ListenerFilterPtr&& filter) PURE;
};

/**
* Creates a chain of network filters for a new connection.
*/
Expand All @@ -155,12 +217,19 @@ class FilterChainFactory {
virtual ~FilterChainFactory() {}

/**
* Called to create the filter chain.
* Called to create the network filter chain.
* @param connection supplies the connection to create the chain on.
* @return true if filter chain was created successfully. Otherwise
* false, e.g. filter chain is empty.
*/
virtual bool createFilterChain(Connection& connection) PURE;
virtual bool createNetworkFilterChain(Connection& connection) PURE;

/**
* Called to create the listener filter chain.
* @param listener supplies the listener to create the chain on.
* @return true if filter chain was created successfully. Otherwise false.
*/
virtual bool createListenerFilterChain(ListenerFilterManager& listener) PURE;
};

} // namespace Network
Expand Down
70 changes: 70 additions & 0 deletions include/envoy/network/listen_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,75 @@ class ListenSocket {
typedef std::unique_ptr<ListenSocket> ListenSocketPtr;
typedef std::shared_ptr<ListenSocket> ListenSocketSharedPtr;

/**
* A socket passed to a connection.
*/
class ConnectionSocket {
public:
virtual ~ConnectionSocket() {}

/**
* @return the local address of the socket.
*/
virtual const Address::InstanceConstSharedPtr& localAddress() const PURE;

/**
* @return the remote address of the socket.
*/
virtual const Address::InstanceConstSharedPtr& remoteAddress() const PURE;

/**
* @return true if the local address has been reset.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: I still really don't know what "reset" means in this context (as a casual reader). Can you add an example? WDYT about my previous suggestion of localAddressChanged()?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I must have missed that suggestion. The real semantics is that the original destination cluster needs to know if the local address was "restored to it's original value, as it was before redirection". The significance of this is that missing that distinction will cause the upstream connection be opened to the same local address the Listener was listening at leading to recursion of open connections until Envoy reaches the max number of open connections and dies. This also explains the difference between the resetLocalAddress() and setLocalAddress(). The latter is used for client connections, where the local address is fully known only after the socket is connected.

So, initially the local address is the one the listener received the new connection at, but it can be restored to the original destination address by the original dst listener filter.

Maybe it would be clearer to add a separate flag to the address change method to mark as the address as 'restored'? This way the semantics would be more explicit and likely easier to understand. I'll try this out.

*/
virtual bool localAddressReset() const PURE;

/**
* @return fd the socket's file descriptor.
*/
virtual int fd() const PURE;

/**
* Close the underlying socket.
*/
virtual void close() PURE;
};

typedef std::unique_ptr<ConnectionSocket> ConnectionSocketPtr;

/**
* An abstract accepted socket.
*
* TODO(jrajahalme): Hide internals (e.g., fd) from the filters by providing callbacks filters
* may need (set/getsockopt(), peek(), recv(), etc.)
*/
class AcceptedSocket : virtual public ConnectionSocket {
public:
virtual ~AcceptedSocket() {}

/**
* Reset the destination address of the socket to a different address than the one

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I still prefer "change" vs. "reset" but that's me. If you prefer "reset" I can live with it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm trying out "set"...

* the socket was accepted at.
*/
virtual void resetLocalAddress(const Address::InstanceConstSharedPtr& local_address) PURE;

/**
* Reset the source address of the socket to a different address than the one
* the socket was accepted at.
*/
virtual void resetRemoteAddress(const Address::InstanceConstSharedPtr& remote_address) PURE;
};

typedef std::unique_ptr<AcceptedSocket> AcceptedSocketPtr;

class ClientSocket : virtual public ConnectionSocket {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you please add some comments on what this interface is for?

public:
virtual ~ClientSocket() {}

/**
* Set the local address of the socket.
*/
virtual void setLocalAddress(const Address::InstanceConstSharedPtr& local_address) PURE;
};

} // namespace Network
} // namespace Envoy
40 changes: 11 additions & 29 deletions include/envoy/network/listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,12 @@

#include "envoy/common/exception.h"
#include "envoy/network/connection.h"
#include "envoy/network/listen_socket.h"
#include "envoy/ssl/context.h"

namespace Envoy {
namespace Network {

/**
* Listener configurations options.
*/
struct ListenerOptions {
// Specifies if the listener should actually bind to the port. A listener that doesn't bind can
// only receive connections redirected from other listeners that set use_origin_dst_ to true.
bool bind_to_port_;
// Whether to use the PROXY Protocol V1
// (http://www.haproxy.org/download/1.5/doc/proxy-protocol.txt)
bool use_proxy_proto_;
// If a connection was redirected to this port using iptables, allow the listener to hand it off
// to the listener associated to the original port.
bool use_original_dst_;
// Soft limit on size of the listener's new connection read and write buffers.
uint32_t per_connection_buffer_limit_bytes_;

/**
* Factory for ListenerOptions with bind_to_port_ set.
* @return ListenerOptions object initialized with bind_to_port_ set.
*/
static ListenerOptions listenerOptionsWithBindToPort() {
return {.bind_to_port_ = true,
.use_proxy_proto_ = false,
.use_original_dst_ = false,
.per_connection_buffer_limit_bytes_ = 0};
}
};

/**
* A configuration for an individual listener.
*/
Expand Down Expand Up @@ -96,7 +69,7 @@ class ListenerConfig {
/**
* @return uint64_t the tag the listener should use for connection handler tracking.
*/
virtual uint64_t listenerTag() PURE;
virtual uint64_t listenerTag() const PURE;

/**
* @return const std::string& the listener's name.
Expand All @@ -111,6 +84,15 @@ class ListenerCallbacks {
public:
virtual ~ListenerCallbacks() {}

/**
* Called when a new connection is accepted.
* @param socket supplies the accepted socket that is moved into the callee.
* @param redirected is true when the socket was first accepted by another listener
* and is redirected to a new listener. The recipient should not redirect
* the socket any further.
*/
virtual void onAccept(AcceptedSocketPtr&& socket, bool redirected = false) PURE;

/**
* Called when a new connection is accepted.
* @param new_connection supplies the new connection that is moved into the callee.
Expand Down
2 changes: 1 addition & 1 deletion include/envoy/network/transport_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class TransportSocketCallbacks {
/**
* @return int the file descriptor associated with the connection.
*/
virtual int fd() PURE;
virtual int fd() const PURE;

/**
* @return Network::Connection& the connection interface.
Expand Down
Loading