forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection_handler_impl.cc
366 lines (312 loc) · 15.1 KB
/
connection_handler_impl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "server/connection_handler_impl.h"
#include "envoy/event/dispatcher.h"
#include "envoy/event/timer.h"
#include "envoy/network/filter.h"
#include "envoy/stats/scope.h"
#include "envoy/stats/timespan.h"
#include "common/network/connection_impl.h"
#include "common/network/utility.h"
#include "extensions/transport_sockets/well_known_names.h"
namespace Envoy {
namespace Server {
ConnectionHandlerImpl::ConnectionHandlerImpl(spdlog::logger& logger, Event::Dispatcher& dispatcher)
: logger_(logger), dispatcher_(dispatcher), disable_listeners_(false) {}
void ConnectionHandlerImpl::addListener(Network::ListenerConfig& config) {
ActiveListenerBasePtr listener;
Network::Address::SocketType socket_type = config.socket().socketType();
if (socket_type == Network::Address::SocketType::Stream) {
ActiveTcpListenerPtr tcp(new ActiveTcpListener(*this, config));
listener = std::move(tcp);
} else {
ASSERT(socket_type == Network::Address::SocketType::Datagram,
"Only datagram/stream listener supported");
ActiveUdpListenerPtr udp(new ActiveUdpListener(*this, config));
listener = std::move(udp);
}
if (disable_listeners_) {
listener->listener_->disable();
}
listeners_.emplace_back(config.socket().localAddress(), std::move(listener));
}
void ConnectionHandlerImpl::removeListeners(uint64_t listener_tag) {
for (auto listener = listeners_.begin(); listener != listeners_.end();) {
if (listener->second->listener_tag_ == listener_tag) {
listener = listeners_.erase(listener);
} else {
++listener;
}
}
}
void ConnectionHandlerImpl::stopListeners(uint64_t listener_tag) {
for (auto& listener : listeners_) {
if (listener.second->listener_tag_ == listener_tag) {
listener.second->listener_.reset();
}
}
}
void ConnectionHandlerImpl::stopListeners() {
for (auto& listener : listeners_) {
listener.second->listener_.reset();
}
}
void ConnectionHandlerImpl::disableListeners() {
disable_listeners_ = true;
for (auto& listener : listeners_) {
listener.second->listener_->disable();
}
}
void ConnectionHandlerImpl::enableListeners() {
disable_listeners_ = false;
for (auto& listener : listeners_) {
listener.second->listener_->enable();
}
}
void ConnectionHandlerImpl::ActiveTcpListener::removeConnection(ActiveConnection& connection) {
ENVOY_CONN_LOG_TO_LOGGER(parent_.logger_, debug, "adding to cleanup list",
*connection.connection_);
ActiveConnectionPtr removed = connection.removeFromList(connections_);
parent_.dispatcher_.deferredDelete(std::move(removed));
ASSERT(parent_.num_connections_ > 0);
parent_.num_connections_--;
}
ConnectionHandlerImpl::ActiveListenerBase::ActiveListenerBase(ConnectionHandlerImpl& parent,
Network::ListenerPtr&& listener,
Network::ListenerConfig& config)
: parent_(parent), listener_(std::move(listener)),
stats_(generateStats(config.listenerScope())),
listener_filters_timeout_(config.listenerFiltersTimeout()),
listener_tag_(config.listenerTag()), config_(config) {}
ConnectionHandlerImpl::ActiveTcpListener::ActiveTcpListener(ConnectionHandlerImpl& parent,
Network::ListenerConfig& config)
: ActiveTcpListener(
parent,
parent.dispatcher_.createListener(config.socket(), *this, config.bindToPort(),
config.handOffRestoredDestinationConnections()),
config) {}
ConnectionHandlerImpl::ActiveTcpListener::ActiveTcpListener(ConnectionHandlerImpl& parent,
Network::ListenerPtr&& listener,
Network::ListenerConfig& config)
: ConnectionHandlerImpl::ActiveListenerBase(parent, std::move(listener), config) {}
ConnectionHandlerImpl::ActiveTcpListener::~ActiveTcpListener() {
// Purge sockets that have not progressed to connections. This should only happen when
// a listener filter stops iteration and never resumes.
while (!sockets_.empty()) {
ActiveSocketPtr removed = sockets_.front()->removeFromList(sockets_);
parent_.dispatcher_.deferredDelete(std::move(removed));
}
while (!connections_.empty()) {
connections_.front()->connection_->close(Network::ConnectionCloseType::NoFlush);
}
parent_.dispatcher_.clearDeferredDeleteList();
}
Network::Listener*
ConnectionHandlerImpl::findListenerByAddress(const Network::Address::Instance& address) {
ActiveListenerBase* listener = findActiveListenerByAddress(address);
return listener ? listener->listener_.get() : nullptr;
}
ConnectionHandlerImpl::ActiveListenerBase*
ConnectionHandlerImpl::findActiveListenerByAddress(const Network::Address::Instance& address) {
// This is a linear operation, may need to add a map<address, listener> to improve performance.
// However, linear performance might be adequate since the number of listeners is small.
// We do not return stopped listeners.
auto listener_it = std::find_if(
listeners_.begin(), listeners_.end(),
[&address](
const std::pair<Network::Address::InstanceConstSharedPtr, ActiveListenerBasePtr>& p) {
return p.second->listener_ != nullptr && p.first->type() == Network::Address::Type::Ip &&
*(p.first) == address;
});
// If there is exact address match, return the corresponding listener.
if (listener_it != listeners_.end()) {
return listener_it->second.get();
}
// Otherwise, we need to look for the wild card match, i.e., 0.0.0.0:[address_port].
// We do not return stopped listeners.
// TODO(wattli): consolidate with previous search for more efficiency.
listener_it = std::find_if(
listeners_.begin(), listeners_.end(),
[&address](
const std::pair<Network::Address::InstanceConstSharedPtr, ActiveListenerBasePtr>& p) {
return p.second->listener_ != nullptr && p.first->type() == Network::Address::Type::Ip &&
p.first->ip()->port() == address.ip()->port() && p.first->ip()->isAnyAddress();
});
return (listener_it != listeners_.end()) ? listener_it->second.get() : nullptr;
}
void ConnectionHandlerImpl::ActiveSocket::onTimeout() {
listener_.stats_.downstream_pre_cx_timeout_.inc();
ASSERT(inserted());
unlink();
}
void ConnectionHandlerImpl::ActiveSocket::startTimer() {
if (listener_.listener_filters_timeout_.count() > 0) {
timer_ = listener_.parent_.dispatcher_.createTimer([this]() -> void { onTimeout(); });
timer_->enableTimer(listener_.listener_filters_timeout_);
}
}
void ConnectionHandlerImpl::ActiveSocket::unlink() {
ActiveSocketPtr removed = removeFromList(listener_.sockets_);
if (removed->timer_ != nullptr) {
removed->timer_->disableTimer();
}
listener_.parent_.dispatcher_.deferredDelete(std::move(removed));
}
void ConnectionHandlerImpl::ActiveSocket::continueFilterChain(bool success) {
if (success) {
if (iter_ == accept_filters_.end()) {
iter_ = accept_filters_.begin();
} else {
iter_ = std::next(iter_);
}
for (; iter_ != accept_filters_.end(); iter_++) {
Network::FilterStatus status = (*iter_)->onAccept(*this);
if (status == Network::FilterStatus::StopIteration) {
// The filter is responsible for calling us again at a later time to continue the filter
// chain from the next filter.
return;
}
}
// Successfully ran all the accept filters.
// Check if the socket may need to be redirected to another listener.
ActiveListenerBase* new_listener = nullptr;
if (hand_off_restored_destination_connections_ && socket_->localAddressRestored()) {
// Find a listener associated with the original destination address.
new_listener = listener_.parent_.findActiveListenerByAddress(*socket_->localAddress());
}
if (new_listener != nullptr) {
// TODO(sumukhs): Try to avoid dynamic_cast by coming up with a better interface design
ActiveTcpListener* tcp_listener = dynamic_cast<ActiveTcpListener*>(new_listener);
ASSERT(tcp_listener != nullptr, "ActiveSocket listener is expected to be tcp");
// Hands off connections redirected by iptables to the listener associated with the
// original destination address. Pass 'hand_off_restored_destination_connections' as false to
// prevent further redirection.
tcp_listener->onAccept(std::move(socket_),
false /* hand_off_restored_destination_connections */);
} else {
// Set default transport protocol if none of the listener filters did it.
if (socket_->detectedTransportProtocol().empty()) {
socket_->setDetectedTransportProtocol(
Extensions::TransportSockets::TransportSocketNames::get().RawBuffer);
}
// Create a new connection on this listener.
listener_.newConnection(std::move(socket_));
}
}
// Filter execution concluded, unlink and delete this ActiveSocket if it was linked.
if (inserted()) {
unlink();
}
}
void ConnectionHandlerImpl::ActiveTcpListener::onAccept(
Network::ConnectionSocketPtr&& socket, bool hand_off_restored_destination_connections) {
auto active_socket = std::make_unique<ActiveSocket>(*this, std::move(socket),
hand_off_restored_destination_connections);
// Create and run the filters
config_.filterChainFactory().createListenerFilterChain(*active_socket);
active_socket->continueFilterChain(true);
// Move active_socket to the sockets_ list if filter iteration needs to continue later.
// Otherwise we let active_socket be destructed when it goes out of scope.
if (active_socket->iter_ != active_socket->accept_filters_.end()) {
active_socket->startTimer();
active_socket->moveIntoListBack(std::move(active_socket), sockets_);
}
}
void ConnectionHandlerImpl::ActiveTcpListener::newConnection(
Network::ConnectionSocketPtr&& socket) {
// Find matching filter chain.
const auto filter_chain = config_.filterChainManager().findFilterChain(*socket);
if (filter_chain == nullptr) {
ENVOY_LOG_TO_LOGGER(parent_.logger_, debug,
"closing connection: no matching filter chain found");
stats_.no_filter_chain_match_.inc();
socket->close();
return;
}
auto transport_socket = filter_chain->transportSocketFactory().createTransportSocket(nullptr);
Network::ConnectionPtr new_connection =
parent_.dispatcher_.createServerConnection(std::move(socket), std::move(transport_socket));
new_connection->setBufferLimits(config_.perConnectionBufferLimitBytes());
const bool empty_filter_chain = !config_.filterChainFactory().createNetworkFilterChain(
*new_connection, filter_chain->networkFilterFactories());
if (empty_filter_chain) {
ENVOY_CONN_LOG_TO_LOGGER(parent_.logger_, debug, "closing connection: no filters",
*new_connection);
new_connection->close(Network::ConnectionCloseType::NoFlush);
return;
}
onNewConnection(std::move(new_connection));
}
void ConnectionHandlerImpl::ActiveTcpListener::onNewConnection(
Network::ConnectionPtr&& new_connection) {
ENVOY_CONN_LOG_TO_LOGGER(parent_.logger_, debug, "new connection", *new_connection);
// If the connection is already closed, we can just let this connection immediately die.
if (new_connection->state() != Network::Connection::State::Closed) {
ActiveConnectionPtr active_connection(
new ActiveConnection(*this, std::move(new_connection), parent_.dispatcher_.timeSource()));
active_connection->moveIntoList(std::move(active_connection), connections_);
parent_.num_connections_++;
}
}
ConnectionHandlerImpl::ActiveConnection::ActiveConnection(ActiveTcpListener& listener,
Network::ConnectionPtr&& new_connection,
TimeSource& time_source)
: listener_(listener), connection_(std::move(new_connection)),
conn_length_(new Stats::Timespan(listener_.stats_.downstream_cx_length_ms_, time_source)) {
// We just universally set no delay on connections. Theoretically we might at some point want
// to make this configurable.
connection_->noDelay(true);
connection_->addConnectionCallbacks(*this);
listener_.stats_.downstream_cx_total_.inc();
listener_.stats_.downstream_cx_active_.inc();
}
ConnectionHandlerImpl::ActiveConnection::~ActiveConnection() {
listener_.stats_.downstream_cx_active_.dec();
listener_.stats_.downstream_cx_destroy_.inc();
conn_length_->complete();
}
ListenerStats ConnectionHandlerImpl::generateStats(Stats::Scope& scope) {
return {ALL_LISTENER_STATS(POOL_COUNTER(scope), POOL_GAUGE(scope), POOL_HISTOGRAM(scope))};
}
ConnectionHandlerImpl::ActiveUdpListener::ActiveUdpListener(ConnectionHandlerImpl& parent,
Network::ListenerConfig& config)
: ActiveUdpListener(parent, parent.dispatcher_.createUdpListener(config.socket(), *this),
config) {}
ConnectionHandlerImpl::ActiveUdpListener::ActiveUdpListener(ConnectionHandlerImpl& parent,
Network::ListenerPtr&& listener,
Network::ListenerConfig& config)
: ConnectionHandlerImpl::ActiveListenerBase(parent, std::move(listener), config),
udp_listener_(dynamic_cast<Network::UdpListener*>(listener_.get())), read_filter_(nullptr) {
// TODO(sumukhs): Try to avoid dynamic_cast by coming up with a better interface design
ASSERT(udp_listener_ != nullptr, "");
// Create the filter chain on creating a new udp listener
config_.filterChainFactory().createUdpListenerFilterChain(*this, *this);
// If filter is nullptr, fail the creation of the listener
if (read_filter_ == nullptr) {
throw Network::CreateListenerException(
fmt::format("Cannot create listener as no read filter registered for the udp listener: {} ",
config_.name()));
}
}
void ConnectionHandlerImpl::ActiveUdpListener::onData(Network::UdpRecvData& data) {
read_filter_->onData(data);
}
void ConnectionHandlerImpl::ActiveUdpListener::onWriteReady(const Network::Socket&) {
// TODO(sumukhs): This is not used now. When write filters are implemented, this is a
// trigger to invoke the on write ready API on the filters which is when they can write
// data
}
void ConnectionHandlerImpl::ActiveUdpListener::onReceiveError(
const Network::UdpListenerCallbacks::ErrorCode&, Api::IoError::IoErrorCode) {
// TODO(sumukhs): Determine what to do on receive error.
// Would the filters need to know on error? Can't foresee a scenario where they
// would take an action
}
void ConnectionHandlerImpl::ActiveUdpListener::addReadFilter(
Network::UdpListenerReadFilterPtr&& filter) {
ASSERT(read_filter_ == nullptr, "Cannot add a 2nd UDP read filter");
read_filter_ = std::move(filter);
}
Network::UdpListener& ConnectionHandlerImpl::ActiveUdpListener::udpListener() {
return *udp_listener_;
}
} // namespace Server
} // namespace Envoy