Let QuicClientDefaultNetworkHelper read UDP GRO trains. `QuicPacketReader` can read UDP GRO trains when configured, but clients built on `QuicClientDefaultNetworkHelper` have no way to opt in because the helper creates both the reader and the sockets. Add `QuicClientDefaultNetworkHelper::EnableUdpGro()`, which consumers will call before the client creates its sockets. The helper then reads with a GRO-capable `QuicPacketReader` and enables UDP GRO on each socket that it creates. If the kernel does not support UDP GRO, the helper logs a warning and uses the socket without it. Calling it after a socket exists is a `QUIC_BUG` and does nothing, because replacing the reader could free it in the middle of a read. PiperOrigin-RevId: 1020120712
diff --git a/quiche/quic/tools/quic_client_default_network_helper.cc b/quiche/quic/tools/quic_client_default_network_helper.cc index d08b5ab..0e9b130 100644 --- a/quiche/quic/tools/quic_client_default_network_helper.cc +++ b/quiche/quic/tools/quic_client_default_network_helper.cc
@@ -14,9 +14,11 @@ #include "quiche/quic/core/io/socket.h" #include "quiche/quic/core/quic_connection.h" #include "quiche/quic/core/quic_default_packet_writer.h" +#include "quiche/quic/core/quic_packet_reader.h" #include "quiche/quic/core/quic_packets.h" #include "quiche/quic/core/quic_types.h" #include "quiche/quic/core/quic_udp_socket.h" +#include "quiche/quic/platform/api/quic_bug_tracker.h" #include "quiche/quic/platform/api/quic_flag_utils.h" #include "quiche/quic/platform/api/quic_logging.h" #include "quiche/quic/platform/api/quic_socket_address.h" @@ -147,6 +149,20 @@ event_loop_->RunEventLoopOnce(QuicTime::Delta::FromMilliseconds(50)); } +void QuicClientDefaultNetworkHelper::EnableUdpGro() { + if (!fd_address_map_.empty()) { + // Replacing the reader could free it in the middle of a read. + QUIC_BUG(quic_enable_udp_gro_after_socket_creation) + << "UDP GRO must be enabled before any socket is created."; + return; + } + if (packet_reader_->gro_enabled()) { + return; + } + packet_reader_ = std::make_unique<QuicPacketReader>( + QuicPacketReader::Options{.enable_gro = true}); +} + void QuicClientDefaultNetworkHelper::OnSocketEvent( QuicEventLoop* /*event_loop*/, QuicUdpSocketFd fd, QuicSocketEventMask events) { @@ -238,6 +254,10 @@ *overflow_supported = api.EnableDroppedPacketCount(fd); api.EnableReceiveTimestamp(fd); + if (packet_reader_->gro_enabled() && !api.EnableUdpGro(fd)) { + QUIC_LOG(WARNING) << "Failed to enable UDP GRO on socket (" << fd + << "), continuing without it."; + } if (!BindInterfaceNameIfNeeded(fd)) { CleanUpUDPSocket(fd);
diff --git a/quiche/quic/tools/quic_client_default_network_helper.h b/quiche/quic/tools/quic_client_default_network_helper.h index e831d41..25b20c4 100644 --- a/quiche/quic/tools/quic_client_default_network_helper.h +++ b/quiche/quic/tools/quic_client_default_network_helper.h
@@ -118,6 +118,12 @@ void set_max_reads_per_event_loop(int num_reads) { max_reads_per_event_loop_ = num_reads; } + + // Enables UDP GRO on each socket that this helper creates, if the kernel + // supports it, and reads with a QuicPacketReader that can read GRO trains. + // Must be called before the helper creates any socket. + void EnableUdpGro(); + // If |fd| is an open UDP socket, unregister and close it. Otherwise, do // nothing. void CleanUpUDPSocket(SocketFd fd);
diff --git a/quiche/quic/tools/quic_default_client_test.cc b/quiche/quic/tools/quic_default_client_test.cc index 666ecbc..5b456d6 100644 --- a/quiche/quic/tools/quic_default_client_test.cc +++ b/quiche/quic/tools/quic_default_client_test.cc
@@ -9,9 +9,12 @@ #include "quiche/quic/tools/quic_default_client.h" #include <dirent.h> +#include <netinet/udp.h> +#include <sys/socket.h> #include <sys/types.h> #include <memory> +#include <optional> #include <string> #include <utility> @@ -20,9 +23,12 @@ #include "quiche/quic/core/io/quic_default_event_loop.h" #include "quiche/quic/core/io/quic_event_loop.h" #include "quiche/quic/core/quic_default_clock.h" +#include "quiche/quic/core/quic_udp_socket.h" +#include "quiche/quic/platform/api/quic_expect_bug.h" #include "quiche/quic/platform/api/quic_test.h" #include "quiche/quic/platform/api/quic_test_loopback.h" #include "quiche/quic/test_tools/crypto_test_utils.h" +#include "quiche/quic/tools/quic_client_default_network_helper.h" #include "quiche/common/quiche_text_utils.h" namespace quic { @@ -67,6 +73,17 @@ return socket_count; } +// Returns whether UDP GRO is enabled on |fd|, or nullopt if the kernel does not +// support UDP GRO. +std::optional<bool> IsUdpGroEnabled(int fd) { + int gro_enabled = 0; + socklen_t length = sizeof(gro_enabled); + if (getsockopt(fd, SOL_UDP, UDP_GRO, &gro_enabled, &length) != 0) { + return std::nullopt; + } + return gro_enabled != 0; +} + class QuicDefaultClientTest : public QuicTest { public: QuicDefaultClientTest() @@ -78,15 +95,20 @@ CreateAndInitializeQuicClient(); } - // Creates a new QuicClient and Initializes it on an unused port. - // Caller is responsible for deletion. - std::unique_ptr<QuicDefaultClient> CreateAndInitializeQuicClient() { + // Creates a new QuicClient for an unused port, without initializing it. + std::unique_ptr<QuicDefaultClient> CreateQuicClient() { QuicSocketAddress server_address(QuicSocketAddress(TestLoopback(), 0)); QuicServerId server_id("hostname", server_address.port()); ParsedQuicVersionVector versions = AllSupportedVersions(); - auto client = std::make_unique<QuicDefaultClient>( + return std::make_unique<QuicDefaultClient>( server_address, server_id, versions, event_loop_.get(), crypto_test_utils::ProofVerifierForTesting()); + } + + // Creates a new QuicClient and Initializes it on an unused port. + // Caller is responsible for deletion. + std::unique_ptr<QuicDefaultClient> CreateAndInitializeQuicClient() { + std::unique_ptr<QuicDefaultClient> client = CreateQuicClient(); EXPECT_TRUE(client->Initialize()); return client; } @@ -140,6 +162,49 @@ EXPECT_EQ(number_of_open_fds + 1, NumOpenSocketFDs()); } +TEST_F(QuicDefaultClientTest, UdpGroIsOffByDefault) { + std::unique_ptr<QuicDefaultClient> client(CreateAndInitializeQuicClient()); + + std::optional<bool> gro_enabled = IsUdpGroEnabled(client->GetLatestFD()); + if (!gro_enabled.has_value()) { + GTEST_SKIP() << "UDP GRO is not supported."; + } + EXPECT_FALSE(*gro_enabled); +} + +TEST_F(QuicDefaultClientTest, EnableUdpGro) { + std::unique_ptr<QuicDefaultClient> client(CreateQuicClient()); + client->default_network_helper()->EnableUdpGro(); + ASSERT_TRUE(client->Initialize()); + + std::optional<bool> gro_enabled = IsUdpGroEnabled(client->GetLatestFD()); + if (!gro_enabled.has_value()) { + GTEST_SKIP() << "UDP GRO is not supported."; + } + EXPECT_TRUE(*gro_enabled); +} + +TEST_F(QuicDefaultClientTest, EnableUdpGroTwice) { + std::unique_ptr<QuicDefaultClient> client(CreateQuicClient()); + client->default_network_helper()->EnableUdpGro(); + // A second call before any socket exists is a no-op. + client->default_network_helper()->EnableUdpGro(); + ASSERT_TRUE(client->Initialize()); + + std::optional<bool> gro_enabled = IsUdpGroEnabled(client->GetLatestFD()); + if (!gro_enabled.has_value()) { + GTEST_SKIP() << "UDP GRO is not supported."; + } + EXPECT_TRUE(*gro_enabled); +} + +TEST_F(QuicDefaultClientTest, EnableUdpGroAfterCreatingSocket) { + std::unique_ptr<QuicDefaultClient> client(CreateAndInitializeQuicClient()); + + EXPECT_QUIC_BUG(client->default_network_helper()->EnableUdpGro(), + "UDP GRO must be enabled before any socket is created."); +} + } // namespace } // namespace test } // namespace quic