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

mobile: Add an option to set the UDP socket send and receive buffer sizes #35190

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions mobile/library/cc/engine_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ EngineBuilder& EngineBuilder::setSocketReceiveBufferSize(int32_t size) {
return *this;
}

EngineBuilder& EngineBuilder::setUdpSocketSendBufferSize(int32_t size) {
alyssawilk marked this conversation as resolved.
Show resolved Hide resolved
udp_socket_send_buffer_size_ = size;
return *this;
}

EngineBuilder& EngineBuilder::enableDrainPostDnsRefresh(bool drain_post_dns_refresh_on) {
enable_drain_post_dns_refresh_ = drain_post_dns_refresh_on;
return *this;
Expand Down Expand Up @@ -734,6 +739,15 @@ std::unique_ptr<envoy::config::bootstrap::v3::Bootstrap> EngineBuilder::generate
rcv_buf_sock_opt->set_int_value(socket_receive_buffer_size_);
Copy link
Contributor

Choose a reason for hiding this comment

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

change this to datagram only?

Copy link
Member Author

Choose a reason for hiding this comment

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

Initially I wanted to do it in a separate PR, but the change is small enough that I can do it in this PR. Done.

rcv_buf_sock_opt->set_description(
absl::StrCat("SO_RCVBUF = ", socket_receive_buffer_size_, " bytes"));

envoy::config::core::v3::SocketOption* udp_snd_buf_sock_opt =
base_cluster->mutable_upstream_bind_config()->add_socket_options();
udp_snd_buf_sock_opt->set_name(SO_SNDBUF);
udp_snd_buf_sock_opt->set_level(SOL_SOCKET);
udp_snd_buf_sock_opt->set_int_value(udp_socket_send_buffer_size_);
udp_snd_buf_sock_opt->mutable_type()->mutable_datagram();
Copy link
Contributor

Choose a reason for hiding this comment

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

Mind adding a comment
// Make sure this option is only applied to UDP sockets and not TCP

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

udp_snd_buf_sock_opt->set_description(
absl::StrCat("UDP SO_SNDBUF = ", udp_socket_send_buffer_size_, " bytes"));
// Set the network service type on iOS, if supplied.
#if defined(__APPLE__)
if (ios_network_service_type_ > 0) {
Expand Down
5 changes: 5 additions & 0 deletions mobile/library/cc/engine_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class EngineBuilder {
// Sets whether to use GRO for upstream UDP sockets (QUIC/HTTP3).
EngineBuilder& setUseGroIfAvailable(bool use_gro_if_available);
EngineBuilder& setSocketReceiveBufferSize(int32_t size);
EngineBuilder& setUdpSocketSendBufferSize(int32_t size);
EngineBuilder& enforceTrustChainVerification(bool trust_chain_verification_on);
EngineBuilder& setUpstreamTlsSni(std::string sni);
EngineBuilder& enablePlatformCertificatesValidation(bool platform_certificates_validation_on);
Expand Down Expand Up @@ -183,6 +184,10 @@ class EngineBuilder {
// This is the same value Cronet uses for QUIC:
// https://source.chromium.org/chromium/chromium/src/+/main:net/quic/quic_context.h;drc=ccfe61524368c94b138ddf96ae8121d7eb7096cf;l=87
int32_t socket_receive_buffer_size_ = 1024 * 1024; // 1MB
// This is the same value Cronet uses for QUIC:
// https://source.chromium.org/chromium/chromium/src/+/main:net/quic/quic_session_pool.cc;l=790-793;drc=7f04a8e033c23dede6beae129cd212e6d4473d72
// https://source.chromium.org/chromium/chromium/src/+/main:net/third_party/quiche/src/quiche/quic/core/quic_constants.h;l=43-47;drc=34ad7f3844f882baf3d31a6bc6e300acaa0e3fc8
int32_t udp_socket_send_buffer_size_ = 1452 * 20;
};

using EngineBuilderSharedPtr = std::shared_ptr<EngineBuilder>;
Expand Down
31 changes: 31 additions & 0 deletions mobile/test/cc/unit/envoy_config_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,37 @@ TEST(TestConfig, SocketReceiveBufferSize) {
EXPECT_EQ(rcv_buf_option->level(), SOL_SOCKET);
EXPECT_EQ(rcv_buf_option->int_value(), 1024 * 1024 /* 1 MB */);
}

TEST(TestConfig, UdpSocketSendBufferSize) {
EngineBuilder engine_builder;
engine_builder.enableHttp3(true);

std::unique_ptr<Bootstrap> bootstrap = engine_builder.generateBootstrap();
Cluster const* base_cluster = nullptr;
for (const Cluster& cluster : bootstrap->static_resources().clusters()) {
if (cluster.name() == "base") {
base_cluster = &cluster;
break;
}
}

// The base H3 cluster should always be found.
ASSERT_THAT(base_cluster, NotNull());

SocketOption const* snd_buf_option = nullptr;
for (const SocketOption& sock_opt : base_cluster->upstream_bind_config().socket_options()) {
if (sock_opt.name() == SO_SNDBUF) {
snd_buf_option = &sock_opt;
break;
}
}

// When using an H3 cluster, the UDP send buffer size option should always be set.
ASSERT_THAT(snd_buf_option, NotNull());
EXPECT_EQ(snd_buf_option->level(), SOL_SOCKET);
EXPECT_TRUE(snd_buf_option->type().has_datagram());
EXPECT_EQ(snd_buf_option->int_value(), 1452 * 20);
}
#endif

TEST(TestConfig, EnablePlatformCertificatesValidation) {
Expand Down