Skip to content

Commit 916bc62

Browse files
committed
support direct local address
cherry-pick: envoyproxy@090e73d use directLocalAddress instead of localAddress in LocalPortValueExtractorImpl of srds Link: https://code.alibaba-inc.com/Ingress/envoy/codereview/21602142 * support direct local address * add metrics stats
1 parent 583feb5 commit 916bc62

File tree

13 files changed

+161
-6
lines changed

13 files changed

+161
-6
lines changed

docs/root/configuration/observability/access_log/usage.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,14 +624,54 @@ The following command operators are supported:
624624
If the original connection was redirected by iptables TPROXY, and the listener's transparent
625625
option was set to true, this represents the original destination address and port.
626626

627+
.. note::
628+
629+
This may not be the physical remote address of the peer if the address has been inferred from
630+
:ref:`Proxy Protocol filter <config_listener_filters_proxy_protocol>`.
631+
632+
%DOWNSTREAM_DIRECT_LOCAL_ADDRESS%
633+
Direct local address of the downstream connection.
634+
635+
.. note::
636+
637+
This is always the physical local address even if the downstream remote address has been inferred from
638+
:ref:`Proxy Protocol filter <config_listener_filters_proxy_protocol>`.
639+
627640
%DOWNSTREAM_LOCAL_ADDRESS_WITHOUT_PORT%
628641
Local address of the downstream connection, without any port component.
629642
IP addresses are the only address type with a port component.
630643

644+
.. note::
645+
646+
This may not be the physical local address if the downstream local address has been inferred from
647+
:ref:`Proxy Protocol filter <config_listener_filters_proxy_protocol>`.
648+
649+
%DOWNSTREAM_DIRECT_LOCAL_ADDRESS_WITHOUT_PORT%
650+
Direct local address of the downstream connection, without any port component.
651+
652+
.. note::
653+
654+
This is always the physical local address even if the downstream local address has been inferred from
655+
:ref:`Proxy Protocol filter <config_listener_filters_proxy_protocol>`.
656+
631657
%DOWNSTREAM_LOCAL_PORT%
632658
Local port of the downstream connection.
633659
IP addresses are the only address type with a port component.
634660

661+
.. note::
662+
663+
This may not be the physical port if the downstream local address has been inferred from
664+
:ref:`Proxy Protocol filter <config_listener_filters_proxy_protocol>`.
665+
666+
%DOWNSTREAM_DIRECT_LOCAL_PORT%
667+
Direct local port of the downstream connection.
668+
IP addresses are the only address type with a port component.
669+
670+
.. note::
671+
672+
This is always the listener port even if the downstream local address has been inferred from
673+
:ref:`Proxy Protocol filter <config_listener_filters_proxy_protocol>`.
674+
635675
.. _config_access_log_format_connection_id:
636676

637677
%CONNECTION_ID%

envoy/network/socket.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ class ConnectionInfoProvider {
176176
*/
177177
virtual const Address::InstanceConstSharedPtr& localAddress() const PURE;
178178

179+
/**
180+
* @return the direct local address of the socket. This is the listener address and it can not be
181+
* modified by listener filters.
182+
*/
183+
virtual const Address::InstanceConstSharedPtr& directLocalAddress() const PURE;
184+
179185
/**
180186
* @return true if the local address has been restored to a value that is different from the
181187
* address the socket was initially accepted at.

source/common/http/conn_manager_config.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
namespace Envoy {
2323
namespace Http {
2424

25+
#if defined(ALIMESH)
26+
#define HIGRESS_EXT_HTTP_CONN_MAN_STATS(COUNTER, GAUGE, HISTOGRAM) \
27+
COUNTER(downstream_rq_retry_scope_found_total) \
28+
COUNTER(downstream_rq_retry_scope_not_found_total)
29+
#endif
30+
2531
/**
2632
* All stats for the connection manager. @see stats_macros.h
2733
*/
@@ -92,6 +98,10 @@ namespace Http {
9298
*/
9399
struct ConnectionManagerNamedStats {
94100
ALL_HTTP_CONN_MAN_STATS(GENERATE_COUNTER_STRUCT, GENERATE_GAUGE_STRUCT, GENERATE_HISTOGRAM_STRUCT)
101+
#if defined(ALIMESH)
102+
HIGRESS_EXT_HTTP_CONN_MAN_STATS(GENERATE_COUNTER_STRUCT, GENERATE_GAUGE_STRUCT,
103+
GENERATE_HISTOGRAM_STRUCT)
104+
#endif
95105
};
96106

97107
struct ConnectionManagerStats {

source/common/http/conn_manager_impl.cc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,16 @@ bool requestWasConnect(const RequestHeaderMapSharedPtr& headers, Protocol protoc
8080
ConnectionManagerStats ConnectionManagerImpl::generateStats(const std::string& prefix,
8181
Stats::Scope& scope) {
8282
return ConnectionManagerStats(
83+
#if defined(ALIMESH)
84+
{ALL_HTTP_CONN_MAN_STATS(POOL_COUNTER_PREFIX(scope, prefix), POOL_GAUGE_PREFIX(scope, prefix),
85+
POOL_HISTOGRAM_PREFIX(scope, prefix))
86+
HIGRESS_EXT_HTTP_CONN_MAN_STATS(POOL_COUNTER_PREFIX(scope, prefix),
87+
POOL_GAUGE_PREFIX(scope, prefix),
88+
POOL_HISTOGRAM_PREFIX(scope, prefix))},
89+
#else
8390
{ALL_HTTP_CONN_MAN_STATS(POOL_COUNTER_PREFIX(scope, prefix), POOL_GAUGE_PREFIX(scope, prefix),
8491
POOL_HISTOGRAM_PREFIX(scope, prefix))},
92+
#endif
8593
prefix, scope);
8694
}
8795

@@ -1673,9 +1681,15 @@ void ConnectionManagerImpl::ActiveStream::refreshCachedRoute(const Router::Route
16731681
}
16741682
route = snapped_route_config_->route(cb, *request_headers_, filter_manager_.streamInfo(),
16751683
stream_id_);
1684+
bool retry_found = route != nullptr;
16761685
ENVOY_STREAM_LOG(debug,
16771686
"after the route was not found, search again in other scopes and found:{}",
1678-
*this, route != nullptr);
1687+
*this, retry_found);
1688+
if (retry_found) {
1689+
connection_manager_.stats_.named_.downstream_rq_retry_scope_found_total_.inc();
1690+
} else {
1691+
connection_manager_.stats_.named_.downstream_rq_retry_scope_not_found_total_.inc();
1692+
}
16791693
}
16801694
}
16811695
#endif

source/common/http/filter_manager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,9 @@ class OverridableRemoteConnectionInfoSetterStreamInfo : public StreamInfo::Strea
569569
const Network::Address::InstanceConstSharedPtr& localAddress() const override {
570570
return StreamInfoImpl::downstreamAddressProvider().localAddress();
571571
}
572+
const Network::Address::InstanceConstSharedPtr& directLocalAddress() const override {
573+
return StreamInfoImpl::downstreamAddressProvider().directLocalAddress();
574+
}
572575
bool localAddressRestored() const override {
573576
return StreamInfoImpl::downstreamAddressProvider().localAddressRestored();
574577
}

source/common/network/socket_impl.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class ConnectionInfoSetterImpl : public ConnectionInfoSetter {
1313
public:
1414
ConnectionInfoSetterImpl(const Address::InstanceConstSharedPtr& local_address,
1515
const Address::InstanceConstSharedPtr& remote_address)
16-
: local_address_(local_address), remote_address_(remote_address),
17-
direct_remote_address_(remote_address) {}
16+
: local_address_(local_address), direct_local_address_(local_address),
17+
remote_address_(remote_address), direct_remote_address_(remote_address) {}
1818

1919
void setDirectRemoteAddressForTest(const Address::InstanceConstSharedPtr& direct_remote_address) {
2020
direct_remote_address_ = direct_remote_address;
@@ -31,6 +31,9 @@ class ConnectionInfoSetterImpl : public ConnectionInfoSetter {
3131

3232
// ConnectionInfoSetter
3333
const Address::InstanceConstSharedPtr& localAddress() const override { return local_address_; }
34+
const Address::InstanceConstSharedPtr& directLocalAddress() const override {
35+
return direct_local_address_;
36+
}
3437
void setLocalAddress(const Address::InstanceConstSharedPtr& local_address) override {
3538
local_address_ = local_address;
3639
}
@@ -76,6 +79,7 @@ class ConnectionInfoSetterImpl : public ConnectionInfoSetter {
7679

7780
private:
7881
Address::InstanceConstSharedPtr local_address_;
82+
Address::InstanceConstSharedPtr direct_local_address_;
7983
bool local_address_restored_{false};
8084
Address::InstanceConstSharedPtr remote_address_;
8185
Address::InstanceConstSharedPtr direct_remote_address_;

source/common/router/scoped_config_impl.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ LocalPortValueExtractorImpl::LocalPortValueExtractorImpl(
4343
std::unique_ptr<ScopeKeyFragmentBase> LocalPortValueExtractorImpl::computeFragment(
4444
const Http::HeaderMap&, const StreamInfo::StreamInfo* info, ReComputeCbPtr&) const {
4545
ASSERT(info != nullptr, "streamInfo is nullptr.");
46-
auto port = info->downstreamAddressProvider().localAddress()->ip()->port();
46+
auto port = info->downstreamAddressProvider().directLocalAddress()->ip()->port();
4747
return std::make_unique<StringKeyFragment>(std::to_string(long(port)));
4848
}
4949

source/extensions/filters/http/lua/wrappers.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,13 @@ int StreamInfoWrapper::luaDownstreamLocalAddress(lua_State* state) {
192192
return 1;
193193
}
194194

195+
int StreamInfoWrapper::luaDownstreamDirectLocalAddress(lua_State* state) {
196+
const std::string& local_address =
197+
stream_info_.downstreamAddressProvider().directLocalAddress()->asString();
198+
lua_pushlstring(state, local_address.data(), local_address.size());
199+
return 1;
200+
}
201+
195202
int StreamInfoWrapper::luaDownstreamDirectRemoteAddress(lua_State* state) {
196203
const std::string& direct_remote_address =
197204
stream_info_.downstreamAddressProvider().directRemoteAddress()->asString();

source/extensions/filters/http/lua/wrappers.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ class StreamInfoWrapper : public Filters::Common::Lua::BaseLuaObject<StreamInfoW
207207
static ExportedFunctions exportedFunctions() {
208208
return {{"protocol", static_luaProtocol},
209209
{"dynamicMetadata", static_luaDynamicMetadata},
210+
{"downstreamDirectLocalAddress", static_luaDownstreamDirectLocalAddress},
210211
{"downstreamLocalAddress", static_luaDownstreamLocalAddress},
211212
{"downstreamDirectRemoteAddress", static_luaDownstreamDirectRemoteAddress},
212213
{"downstreamSslConnection", static_luaDownstreamSslConnection},
@@ -239,7 +240,14 @@ class StreamInfoWrapper : public Filters::Common::Lua::BaseLuaObject<StreamInfoW
239240
DECLARE_LUA_FUNCTION(StreamInfoWrapper, luaDownstreamLocalAddress);
240241

241242
/**
242-
* Get current downstream local address
243+
* Get current direct downstream local address
244+
* @return string representation of downstream directly connected local address.
245+
* This is equivalent to the local address of the physical connection.
246+
*/
247+
DECLARE_LUA_FUNCTION(StreamInfoWrapper, luaDownstreamDirectLocalAddress);
248+
249+
/**
250+
* Get current direct downstream remote address
243251
* @return string representation of downstream directly connected address.
244252
* This is equivalent to the address of the physical connection.
245253
*/

test/common/formatter/substitution_formatter_test.cc

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,18 @@ TEST(SubstitutionFormatterTest, streamInfoFormatter) {
880880
ProtoEq(ValueUtil::stringValue("127.0.0.2:0")));
881881
}
882882

883+
{
884+
StreamInfoFormatter format("DOWNSTREAM_DIRECT_LOCAL_ADDRESS");
885+
auto address = Network::Address::InstanceConstSharedPtr{new Network::Address::Ipv4Instance(
886+
"127.1.2.3", 6745)},
887+
original_address = stream_info.downstream_connection_info_provider_->localAddress();
888+
stream_info.downstream_connection_info_provider_->setLocalAddress(address);
889+
EXPECT_EQ("127.0.0.2:0", format.formatWithContext({}, stream_info));
890+
EXPECT_THAT(format.formatValueWithContext({}, stream_info),
891+
ProtoEq(ValueUtil::stringValue("127.0.0.2:0")));
892+
stream_info.downstream_connection_info_provider_->setLocalAddress(original_address);
893+
}
894+
883895
{
884896
StreamInfoFormatter upstream_format("DOWNSTREAM_LOCAL_ADDRESS_WITHOUT_PORT");
885897
EXPECT_EQ("127.0.0.2",
@@ -891,8 +903,33 @@ TEST(SubstitutionFormatterTest, streamInfoFormatter) {
891903
}
892904

893905
{
894-
StreamInfoFormatter upstream_format("DOWNSTREAM_LOCAL_PORT");
906+
StreamInfoFormatter format("DOWNSTREAM_DIRECT_LOCAL_ADDRESS_WITHOUT_PORT");
907+
EXPECT_EQ("127.0.0.2", format.formatWithContext({}, stream_info));
908+
EXPECT_THAT(format.formatValueWithContext({}, stream_info),
909+
ProtoEq(ValueUtil::stringValue("127.0.0.2")));
910+
}
895911

912+
{
913+
StreamInfoFormatter format("DOWNSTREAM_DIRECT_LOCAL_ADDRESS_WITHOUT_PORT");
914+
auto address = Network::Address::InstanceConstSharedPtr{
915+
new Network::Address::Ipv4Instance("127.1.2.3", 8900)};
916+
stream_info.downstream_connection_info_provider_->setLocalAddress(address);
917+
EXPECT_EQ("127.0.0.2", format.formatWithContext({}, stream_info));
918+
EXPECT_THAT(format.formatValueWithContext({}, stream_info),
919+
ProtoEq(ValueUtil::stringValue("127.0.0.2")));
920+
}
921+
922+
{
923+
StreamInfoFormatter format("DOWNSTREAM_DIRECT_LOCAL_PORT");
924+
EXPECT_EQ("0", format.formatWithContext({}, stream_info));
925+
EXPECT_THAT(format.formatValueWithContext({}, stream_info), ProtoEq(ValueUtil::numberValue(0)));
926+
}
927+
928+
{
929+
StreamInfoFormatter downstream_local_port_format("DOWNSTREAM_LOCAL_PORT"),
930+
downstream_direct_downstream_local_port_format("DOWNSTREAM_DIRECT_LOCAL_PORT");
931+
932+
StreamInfoFormatter upstream_format("DOWNSTREAM_LOCAL_PORT");
896933
// Validate for IPv4 address
897934
auto address = Network::Address::InstanceConstSharedPtr{
898935
new Network::Address::Ipv4Instance("127.1.2.3", 8443)};
@@ -942,6 +979,11 @@ TEST(SubstitutionFormatterTest, streamInfoFormatter) {
942979
EXPECT_THAT(upstream_format.formatValue(request_headers, response_headers, response_trailers,
943980
stream_info, body, AccessLog::AccessLogType::NotSet),
944981
ProtoEq(ValueUtil::nullValue()));
982+
EXPECT_EQ("0",
983+
downstream_direct_downstream_local_port_format.formatWithContext({}, stream_info));
984+
EXPECT_THAT(
985+
downstream_direct_downstream_local_port_format.formatValueWithContext({}, stream_info),
986+
ProtoEq(ValueUtil::numberValue(0)));
945987
}
946988

947989
{

0 commit comments

Comments
 (0)