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

stream info: add interface name of the local side of the upstream connection #19336

Merged
merged 23 commits into from
Dec 27, 2021
Merged
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
12 changes: 12 additions & 0 deletions envoy/network/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ class ConnectionInfoProvider {
**/
virtual absl::optional<uint64_t> connectionID() const PURE;

/**
* @return the name of the network interface used by local end of the connection, or unset if not
*available.
**/
virtual absl::optional<absl::string_view> interfaceName() const PURE;

/**
* Dumps the state of the ConnectionInfoProvider to the given ostream.
*
Expand Down Expand Up @@ -143,6 +149,12 @@ class ConnectionInfoSetter : public ConnectionInfoProvider {
**/
virtual void setConnectionID(uint64_t id) PURE;

/**
* @param interface_name the name of the network interface used by the local end of the
*connection.
**/
virtual void setInterfaceName(absl::string_view interface_name) PURE;

/**
* @param connection_info sets the downstream ssl connection.
*/
Expand Down
11 changes: 11 additions & 0 deletions envoy/stream_info/stream_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,17 @@ class UpstreamInfo {
*/
virtual absl::optional<uint64_t> upstreamConnectionId() const PURE;

/**
* @param interface name of the upstream connection's local socket.
*/
virtual void setUpstreamInterfaceName(absl::string_view interface_name) PURE;

/**
* @return interface name of the upstream connection's local socket, or absl::nullopt if not
* available.
*/
virtual absl::optional<absl::string_view> upstreamInterfaceName() const PURE;

/**
* @param connection_info sets the upstream ssl connection.
*/
Expand Down
3 changes: 3 additions & 0 deletions source/common/http/filter_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,9 @@ class OverridableRemoteConnectionInfoSetterStreamInfo : public StreamInfo::Strea
absl::optional<uint64_t> connectionID() const override {
return StreamInfoImpl::downstreamAddressProvider().connectionID();
}
absl::optional<absl::string_view> interfaceName() const override {
return StreamInfoImpl::downstreamAddressProvider().interfaceName();
}
Ssl::ConnectionInfoConstSharedPtr sslConnection() const override {
return StreamInfoImpl::downstreamAddressProvider().sslConnection();
}
Expand Down
10 changes: 10 additions & 0 deletions source/common/network/connection_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,16 @@ void ClientConnectionImpl::connect() {

void ClientConnectionImpl::onConnected() {
stream_info_.upstreamInfo()->upstreamTiming().onUpstreamConnectComplete(dispatcher_.timeSource());
// There are no meaningful socket source address semantics for non-IP sockets, so skip.
Copy link
Contributor

Choose a reason for hiding this comment

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

Would interfaceName() just be empty in that case? I wonder if it would be simpler semantically to gate on just that

Copy link
Member

Choose a reason for hiding this comment

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

Or we can make the set* API take a optional arg absl::optional<absl::string_view>. Then we only need do simple check in the set* API implementation. 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

Would interfaceName() just be empty in that case? I wonder if it would be simpler semantically to gate on just that

No, unfortunately this goes all the way to what unix provides us in getsockname: a successful syscall but an non-zero bytes "empty" string. So, as it stands calling localAddress() on a UDS throws. I can fix that. But it seemed independent of this change.

if (socket_->connectionInfoProviderSharedPtr()->remoteAddress()->ip()) {
// interfaceName makes a syscall. Call once to minimize perf hit.
const auto maybe_interface_name = ioHandle().interfaceName();
if (maybe_interface_name.has_value()) {
ENVOY_CONN_LOG_EVENT(debug, "conn_interface", "connected on local interface '{}'", *this,
maybe_interface_name.value());
socket_->connectionInfoProvider().setInterfaceName(maybe_interface_name.value());
}
}
ConnectionImpl::onConnected();
}

Expand Down
5 changes: 5 additions & 0 deletions source/common/network/socket_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class ConnectionInfoSetterImpl : public ConnectionInfoSetter {
}
absl::optional<uint64_t> connectionID() const override { return connection_id_; }
void setConnectionID(uint64_t id) override { connection_id_ = id; }
absl::optional<absl::string_view> interfaceName() const override { return interface_name_; }
void setInterfaceName(absl::string_view interface_name) override {
interface_name_ = std::string(interface_name);
}
Ssl::ConnectionInfoConstSharedPtr sslConnection() const override { return ssl_info_; }
void setSslConnection(const Ssl::ConnectionInfoConstSharedPtr& ssl_connection_info) override {
ASSERT(!ssl_info_);
Expand All @@ -67,6 +71,7 @@ class ConnectionInfoSetterImpl : public ConnectionInfoSetter {
Address::InstanceConstSharedPtr direct_remote_address_;
std::string server_name_;
absl::optional<uint64_t> connection_id_;
absl::optional<std::string> interface_name_;
Ssl::ConnectionInfoConstSharedPtr ssl_info_;
std::string ja3_hash_;
};
Expand Down
5 changes: 5 additions & 0 deletions source/common/router/upstream_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,11 @@ void UpstreamRequest::onPoolReady(
upstream_info.setUpstreamConnectionId(info.downstreamAddressProvider().connectionID().value());
}

if (info.downstreamAddressProvider().interfaceName().has_value()) {
upstream_info.setUpstreamInterfaceName(
info.downstreamAddressProvider().interfaceName().value());
}

stream_info_.setUpstreamBytesMeter(upstream_->bytesMeter());
StreamInfo::StreamInfo::syncUpstreamAndDownstreamBytesMeter(parent_.callbacks()->streamInfo(),
stream_info_);
Expand Down
9 changes: 9 additions & 0 deletions source/common/stream_info/stream_info_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ struct UpstreamInfoImpl : public UpstreamInfo {

absl::optional<uint64_t> upstreamConnectionId() const override { return upstream_connection_id_; }

void setUpstreamInterfaceName(absl::string_view interface_name) override {
upstream_connection_interface_name_ = std::string(interface_name);
}

absl::optional<absl::string_view> upstreamInterfaceName() const override {
return upstream_connection_interface_name_;
}

void
setUpstreamSslConnection(const Ssl::ConnectionInfoConstSharedPtr& ssl_connection_info) override {
upstream_ssl_info_ = ssl_connection_info;
Expand Down Expand Up @@ -76,6 +84,7 @@ struct UpstreamInfoImpl : public UpstreamInfo {
UpstreamTiming upstream_timing_;
Ssl::ConnectionInfoConstSharedPtr upstream_ssl_info_;
absl::optional<uint64_t> upstream_connection_id_;
absl::optional<std::string> upstream_connection_interface_name_;
std::string upstream_transport_failure_reason_;
FilterStateSharedPtr upstream_filter_state_;
size_t num_streams_{};
Expand Down
1 change: 1 addition & 0 deletions test/common/router/router_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5078,6 +5078,7 @@ TEST_F(RouterTest, UpstreamSSLConnection) {
EXPECT_EQ(session_id,
callbacks_.streamInfo().upstreamInfo()->upstreamSslConnection()->sessionId());
EXPECT_FALSE(callbacks_.streamInfo().upstreamInfo()->upstreamConnectionId().has_value());
EXPECT_FALSE(callbacks_.streamInfo().upstreamInfo()->upstreamInterfaceName().has_value());
}

// Verify that upstream timing information is set into the StreamInfo after the upstream
Expand Down
5 changes: 5 additions & 0 deletions test/common/stream_info/stream_info_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ TEST_F(StreamInfoImplTest, MiscSettersAndGetters) {
ASSERT_TRUE(stream_info.upstreamInfo()->upstreamConnectionId().has_value());
EXPECT_EQ(12345, stream_info.upstreamInfo()->upstreamConnectionId().value());

EXPECT_FALSE(stream_info.upstreamInfo()->upstreamInterfaceName().has_value());
stream_info.upstreamInfo()->setUpstreamInterfaceName("lo");
ASSERT_TRUE(stream_info.upstreamInfo()->upstreamInterfaceName().has_value());
EXPECT_EQ("lo", stream_info.upstreamInfo()->upstreamInterfaceName().value());

std::shared_ptr<UpstreamInfo> new_info = std::make_shared<UpstreamInfoImpl>();
EXPECT_NE(stream_info.upstreamInfo(), new_info);
stream_info.setUpstreamInfo(new_info);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,15 @@ TEST_P(ProxyProtocolTest, ErrorRecv_2) {
[this](os_fd_t sockfd, sockaddr* addr, socklen_t* addrlen) -> Api::SysCallSocketResult {
return os_sys_calls_actual_.accept(sockfd, addr, addrlen);
}));
EXPECT_CALL(os_sys_calls, supportsGetifaddrs())
.Times(AnyNumber())
.WillRepeatedly(
Invoke([this]() -> bool { return os_sys_calls_actual_.supportsGetifaddrs(); }));
EXPECT_CALL(os_sys_calls, getifaddrs(_))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](Api::InterfaceAddressVector& vector) -> Api::SysCallIntResult {
return os_sys_calls_actual_.getifaddrs(vector);
}));
connect(false);
write(buffer, sizeof(buffer));

Expand Down Expand Up @@ -422,6 +431,15 @@ TEST_P(ProxyProtocolTest, ErrorRecv_1) {
[this](os_fd_t sockfd, sockaddr* addr, socklen_t* addrlen) -> Api::SysCallSocketResult {
return os_sys_calls_actual_.accept(sockfd, addr, addrlen);
}));
EXPECT_CALL(os_sys_calls, supportsGetifaddrs())
.Times(AnyNumber())
.WillRepeatedly(
Invoke([this]() -> bool { return os_sys_calls_actual_.supportsGetifaddrs(); }));
EXPECT_CALL(os_sys_calls, getifaddrs(_))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](Api::InterfaceAddressVector& vector) -> Api::SysCallIntResult {
return os_sys_calls_actual_.getifaddrs(vector);
}));
connect(false);
write(buffer, sizeof(buffer));

Expand Down Expand Up @@ -658,6 +676,15 @@ TEST_P(ProxyProtocolTest, V2ParseExtensionsRecvError) {
[this](os_fd_t sockfd, sockaddr* addr, socklen_t* addrlen) -> Api::SysCallSocketResult {
return os_sys_calls_actual_.accept(sockfd, addr, addrlen);
}));
EXPECT_CALL(os_sys_calls, supportsGetifaddrs())
.Times(AnyNumber())
.WillRepeatedly(
Invoke([this]() -> bool { return os_sys_calls_actual_.supportsGetifaddrs(); }));
EXPECT_CALL(os_sys_calls, getifaddrs(_))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](Api::InterfaceAddressVector& vector) -> Api::SysCallIntResult {
return os_sys_calls_actual_.getifaddrs(vector);
}));
connect(false);
write(buffer, sizeof(buffer));
dispatcher_->run(Event::Dispatcher::RunType::NonBlock);
Expand Down Expand Up @@ -835,6 +862,15 @@ TEST_P(ProxyProtocolTest, V2Fragmented4Error) {
[this](os_fd_t sockfd, sockaddr* addr, socklen_t* addrlen) -> Api::SysCallSocketResult {
return os_sys_calls_actual_.accept(sockfd, addr, addrlen);
}));
EXPECT_CALL(os_sys_calls, supportsGetifaddrs())
.Times(AnyNumber())
.WillRepeatedly(
Invoke([this]() -> bool { return os_sys_calls_actual_.supportsGetifaddrs(); }));
EXPECT_CALL(os_sys_calls, getifaddrs(_))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](Api::InterfaceAddressVector& vector) -> Api::SysCallIntResult {
return os_sys_calls_actual_.getifaddrs(vector);
}));
connect(false);
write(buffer, 17);

Expand Down Expand Up @@ -902,6 +938,15 @@ TEST_P(ProxyProtocolTest, V2Fragmented5Error) {
[this](os_fd_t sockfd, sockaddr* addr, socklen_t* addrlen) -> Api::SysCallSocketResult {
return os_sys_calls_actual_.accept(sockfd, addr, addrlen);
}));
EXPECT_CALL(os_sys_calls, supportsGetifaddrs())
.Times(AnyNumber())
.WillRepeatedly(
Invoke([this]() -> bool { return os_sys_calls_actual_.supportsGetifaddrs(); }));
EXPECT_CALL(os_sys_calls, getifaddrs(_))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](Api::InterfaceAddressVector& vector) -> Api::SysCallIntResult {
return os_sys_calls_actual_.getifaddrs(vector);
}));
connect(false);
write(buffer, 10);
dispatcher_->run(Event::Dispatcher::RunType::NonBlock);
Expand Down