Let the QBONE TUN device be opened with a virtio-net header. `TunTapDevice` takes a new `use_vnet_hdr` argument. When it is set, each queue is opened with `IFF_VNET_HDR`, so every packet read from or written to the device is preceded by a `virtio_net_hdr`. The argument defaults to false and nothing sets it yet. PiperOrigin-RevId: 1026713070
diff --git a/build/source_list.bzl b/build/source_list.bzl index d4b5221..d0310d4 100644 --- a/build/source_list.bzl +++ b/build/source_list.bzl
@@ -1802,6 +1802,7 @@ "quic/qbone/platform/netlink_interface.h", "quic/qbone/platform/rtnetlink_message.h", "quic/qbone/platform/tcp_packet.h", + "quic/qbone/platform/virtio_net_header.h", "quic/qbone/qbone_client.h", "quic/qbone/qbone_client_interface.h", "quic/qbone/qbone_client_session.h",
diff --git a/build/source_list.gni b/build/source_list.gni index 170e9f8..b7776a2 100644 --- a/build/source_list.gni +++ b/build/source_list.gni
@@ -1811,6 +1811,7 @@ "src/quiche/quic/qbone/platform/netlink_interface.h", "src/quiche/quic/qbone/platform/rtnetlink_message.h", "src/quiche/quic/qbone/platform/tcp_packet.h", + "src/quiche/quic/qbone/platform/virtio_net_header.h", "src/quiche/quic/qbone/qbone_client.h", "src/quiche/quic/qbone/qbone_client_interface.h", "src/quiche/quic/qbone/qbone_client_session.h",
diff --git a/build/source_list.json b/build/source_list.json index cece503..1cc061f 100644 --- a/build/source_list.json +++ b/build/source_list.json
@@ -1810,6 +1810,7 @@ "quiche/quic/qbone/platform/netlink_interface.h", "quiche/quic/qbone/platform/rtnetlink_message.h", "quiche/quic/qbone/platform/tcp_packet.h", + "quiche/quic/qbone/platform/virtio_net_header.h", "quiche/quic/qbone/qbone_client.h", "quiche/quic/qbone/qbone_client_interface.h", "quiche/quic/qbone/qbone_client_session.h",
diff --git a/quiche/quic/qbone/bonnet/tun_device.cc b/quiche/quic/qbone/bonnet/tun_device.cc index 182177e..b985181 100644 --- a/quiche/quic/qbone/bonnet/tun_device.cc +++ b/quiche/quic/qbone/bonnet/tun_device.cc
@@ -23,6 +23,7 @@ #include "quiche/quic/platform/api/quic_bug_tracker.h" #include "quiche/quic/platform/api/quic_logging.h" #include "quiche/quic/qbone/platform/kernel_interface.h" +#include "quiche/quic/qbone/platform/virtio_net_header.h" #include "quiche/common/quiche_endian.h" ABSL_FLAG(std::string, qbone_client_tun_device_path, "/dev/net/tun", @@ -43,13 +44,15 @@ TunTapDevice::TunTapDevice(const std::string& interface_name, int mtu, int tx_queue_len, bool persist, bool setup_tun, - bool is_tap, KernelInterface* kernel) + bool is_tap, KernelInterface* kernel, + bool use_vnet_hdr) : interface_name_(interface_name), mtu_(mtu), tx_queue_len_(tx_queue_len), persist_(persist), setup_tun_(setup_tun), is_tap_(is_tap), + use_vnet_hdr_(use_vnet_hdr), kernel_(*kernel) {} TunTapDevice::~TunTapDevice() { @@ -148,6 +151,9 @@ } else { if_request.ifr_flags |= IFF_TUN; } + if (use_vnet_hdr_) { + if_request.ifr_flags |= IFF_VNET_HDR; + } int new_fd = kInvalidFd; if (!OpenFileDescriptor(kernel_, @@ -168,13 +174,15 @@ return new_fd; } -bool TunTapDevice::CheckFeatures(KernelInterface& kernel, int tun_device_fd) { +bool TunTapDevice::CheckFeatures(KernelInterface& kernel, int tun_device_fd, + unsigned int extra_required_features) { unsigned int actual_features; if (kernel.ioctl(tun_device_fd, TUNGETFEATURES, &actual_features) != 0) { QUIC_PLOG(WARNING) << "Failed to TUNGETFEATURES"; return false; } - unsigned int required_features = IFF_TUN | IFF_NO_PI; + unsigned int required_features = + IFF_TUN | IFF_NO_PI | extra_required_features; if ((required_features & actual_features) != required_features) { QUIC_LOG(WARNING) << "Required feature does not exist. required_features: 0x" << std::hex @@ -194,7 +202,8 @@ *return_fd = kInvalidFd; return false; } - if (!CheckFeatures(kernel, *return_fd)) { + const bool use_vnet_hdr = (if_request.ifr_flags & IFF_VNET_HDR) != 0; + if (!CheckFeatures(kernel, *return_fd, use_vnet_hdr ? IFF_VNET_HDR : 0)) { return false; } @@ -204,6 +213,40 @@ return false; } + // The header is a property of the device, not of the descriptor: a + // descriptor attached to a device that already has other queues keeps the + // device's flags, whatever it asked for. Reading or writing with the wrong + // framing would corrupt every packet, so check that the device has the + // header exactly when it was asked for. + struct ifreq actual_request; + memset(&actual_request, 0, sizeof(actual_request)); + if (kernel.ioctl(*return_fd, TUNGETIFF, + reinterpret_cast<void*>(&actual_request)) != 0) { + QUIC_PLOG(WARNING) << "Failed to TUNGETIFF on fd(" << *return_fd << ")"; + return false; + } + const bool device_uses_vnet_hdr = + (actual_request.ifr_flags & IFF_VNET_HDR) != 0; + if (device_uses_vnet_hdr != use_vnet_hdr) { + QUIC_LOG(WARNING) << "The TUN device was already in use " + << (device_uses_vnet_hdr ? "with" : "without") + << " IFF_VNET_HDR, which a new queue can't change. " + "Device flags: 0x" + << std::hex << actual_request.ifr_flags; + return false; + } + + if (use_vnet_hdr) { + // Makes sure the right header size is still used, in case it was changed + // outside of Bonnet. + int vnet_hdr_size = sizeof(virtio_net_hdr); + if (kernel.ioctl(*return_fd, TUNSETVNETHDRSZ, &vnet_hdr_size) != 0) { + QUIC_PLOG(WARNING) << "Failed to TUNSETVNETHDRSZ on fd(" << *return_fd + << ")"; + return false; + } + } + if (kernel.ioctl(*return_fd, TUNSETPERSIST, persist ? reinterpret_cast<void*>(&if_request) : nullptr) != 0) {
diff --git a/quiche/quic/qbone/bonnet/tun_device.h b/quiche/quic/qbone/bonnet/tun_device.h index e74c6fb..ae1e997 100644 --- a/quiche/quic/qbone/bonnet/tun_device.h +++ b/quiche/quic/qbone/bonnet/tun_device.h
@@ -42,10 +42,14 @@ // The transmit queue length in packets is configured via tx_queue_len. // Non-positive values leave the kernel default queue length unchanged. // + // If use_vnet_hdr is set, the device is opened with IFF_VNET_HDR: every + // packet read from or written to any of its file descriptors is then + // preceded by a `virtio_net_hdr`. + // // The caller should own kernel and make sure it outlives this. TunTapDevice(const std::string& interface_name, int mtu, int tx_queue_len, bool persist, bool setup_tun, bool is_tap, - KernelInterface* kernel); + KernelInterface* kernel, bool use_vnet_hdr = false); ~TunTapDevice() override; @@ -75,7 +79,14 @@ std::optional<ethhdr> eth_hdr() const override { return eth_hdr_; } - static bool CheckFeatures(KernelInterface& kernel, int tun_device_fd); + // Returns whether the kernel's TUN driver supports IFF_TUN and IFF_NO_PI, + // plus the IFF_* flags in `extra_required_features`. + static bool CheckFeatures(KernelInterface& kernel, int tun_device_fd, + unsigned int extra_required_features = 0); + + // Opens `path` and attaches the descriptor to the device that `if_request` + // names, with the flags that it holds. It fails unless the device's + // IFF_VNET_HDR state matches if_request. static bool OpenFileDescriptor(KernelInterface& kernel, const std::string& path, ifreq if_request, int flags, bool persist, int* return_fd); @@ -97,6 +108,7 @@ const bool persist_; const bool setup_tun_; const bool is_tap_; + const bool use_vnet_hdr_; KernelInterface& kernel_; std::vector<int> file_descriptors_;
diff --git a/quiche/quic/qbone/bonnet/tun_device_test.cc b/quiche/quic/qbone/bonnet/tun_device_test.cc index 06f1472..3479ea2 100644 --- a/quiche/quic/qbone/bonnet/tun_device_test.cc +++ b/quiche/quic/qbone/bonnet/tun_device_test.cc
@@ -14,19 +14,21 @@ #include "quiche/quic/platform/api/quic_test.h" #include "quiche/quic/qbone/platform/mock_kernel.h" +#include "quiche/quic/qbone/platform/virtio_net_header.h" namespace quic::test { namespace { using ::testing::_; using ::testing::AnyNumber; +using ::testing::Exactly; using ::testing::Return; using ::testing::StrEq; using ::testing::Unused; const char kDeviceName[] = "tun0"; -const int kSupportedFeatures = - IFF_TUN | IFF_TAP | IFF_MULTI_QUEUE | IFF_ONE_QUEUE | IFF_NO_PI; +const int kSupportedFeatures = IFF_TUN | IFF_TAP | IFF_MULTI_QUEUE | + IFF_ONE_QUEUE | IFF_NO_PI | IFF_VNET_HDR; // Quite a bit of EXPECT_CALL().Times(AnyNumber()).WillRepeatedly() are used to // make sure we can correctly set common expectations and override the @@ -46,7 +48,7 @@ // Set the expectations for calling Init(). void SetInitExpectations(int mtu, int tx_queue_len, bool persist, - bool is_tap = false) { + bool is_tap = false, bool use_vnet_hdr = false) { EXPECT_CALL(mock_kernel_, open(StrEq("/dev/net/tun"), _)) .Times(AnyNumber()) .WillRepeatedly([this](Unused, Unused) { @@ -62,14 +64,36 @@ }); EXPECT_CALL(mock_kernel_, ioctl(_, TUNSETIFF, _)) .Times(AnyNumber()) - .WillRepeatedly([is_tap](Unused, Unused, void* argp) { + .WillRepeatedly([is_tap, use_vnet_hdr](Unused, Unused, void* argp) { auto* ifr = reinterpret_cast<struct ifreq*>(argp); int expected_flags = (is_tap ? IFF_TAP : IFF_TUN) | IFF_MULTI_QUEUE | IFF_NO_PI; + if (use_vnet_hdr) { + expected_flags |= IFF_VNET_HDR; + } EXPECT_EQ(expected_flags, ifr->ifr_flags); EXPECT_THAT(ifr->ifr_name, StrEq(kDeviceName)); return 0; }); + // Each queue checks the device's flags, which here match what it asked + // for. Without the virtio-net header, the header size is left alone. + EXPECT_CALL(mock_kernel_, ioctl(_, TUNGETIFF, _)) + .Times(AnyNumber()) + .WillRepeatedly([is_tap, use_vnet_hdr](Unused, Unused, void* argp) { + auto* ifr = reinterpret_cast<struct ifreq*>(argp); + ifr->ifr_flags = + (is_tap ? IFF_TAP : IFF_TUN) | IFF_MULTI_QUEUE | IFF_NO_PI; + if (use_vnet_hdr) { + ifr->ifr_flags |= IFF_VNET_HDR; + } + return 0; + }); + EXPECT_CALL(mock_kernel_, ioctl(_, TUNSETVNETHDRSZ, _)) + .Times(use_vnet_hdr ? AnyNumber() : Exactly(0)) + .WillRepeatedly([](Unused, Unused, void* argp) { + EXPECT_EQ(*reinterpret_cast<int*>(argp), sizeof(virtio_net_hdr)); + return 0; + }); EXPECT_CALL(mock_kernel_, ioctl(_, TUNSETPERSIST, _)) .Times(AnyNumber()) .WillRepeatedly([persist](Unused, Unused, void* argp) { @@ -363,5 +387,124 @@ ExpectDown(/* fail = */ false); } +TEST_F(TunDeviceTest, VnetHdrDeviceSetsFlagAndHeaderSize) { + SetInitExpectations(/* mtu = */ 1500, /* tx_queue_len = */ 1000, + /* persist = */ false, /* is_tap = */ false, + /* use_vnet_hdr = */ true); + // Each queue checks the device's flags and sets the header size. + EXPECT_CALL(mock_kernel_, ioctl(_, TUNGETIFF, _)) + .Times(2) + .WillRepeatedly([](Unused, Unused, void* argp) { + auto* ifr = reinterpret_cast<struct ifreq*>(argp); + ifr->ifr_flags = IFF_TUN | IFF_MULTI_QUEUE | IFF_NO_PI | IFF_VNET_HDR; + return 0; + }); + EXPECT_CALL(mock_kernel_, ioctl(_, TUNSETVNETHDRSZ, _)) + .Times(2) + .WillRepeatedly([](Unused, Unused, void* argp) { + EXPECT_EQ(*reinterpret_cast<int*>(argp), sizeof(virtio_net_hdr)); + return 0; + }); + TunTapDevice tun_device(kDeviceName, 1500, 1000, false, true, false, + &mock_kernel_, /* use_vnet_hdr = */ true); + EXPECT_TRUE(tun_device.Init()); + EXPECT_GT(tun_device.GetReadFileDescriptor(), -1); + + EXPECT_GT(tun_device.OpenQueue(), -1); + + ExpectDown(/* fail = */ false); +} + +TEST_F(TunDeviceTest, VnetHdrNotSupportedByKernel) { + SetInitExpectations(/* mtu = */ 1500, /* tx_queue_len = */ 1000, + /* persist = */ false, /* is_tap = */ false, + /* use_vnet_hdr = */ true); + EXPECT_CALL(mock_kernel_, ioctl(_, TUNGETFEATURES, _)) + .WillOnce([](Unused, Unused, void* argp) { + int* actual_features = reinterpret_cast<int*>(argp); + *actual_features = IFF_TUN | IFF_MULTI_QUEUE | IFF_NO_PI; + return 0; + }); + EXPECT_CALL(mock_kernel_, ioctl(_, TUNSETIFF, _)).Times(0); + TunTapDevice tun_device(kDeviceName, 1500, 1000, false, true, false, + &mock_kernel_, /* use_vnet_hdr = */ true); + EXPECT_FALSE(tun_device.Init()); + EXPECT_EQ(tun_device.GetReadFileDescriptor(), -1); + ExpectDown(/* fail = */ false); +} + +TEST_F(TunDeviceTest, VnetHdrNotTurnedOnByDeviceAlreadyInUse) { + SetInitExpectations(/* mtu = */ 1500, /* tx_queue_len = */ 1000, + /* persist = */ false, /* is_tap = */ false, + /* use_vnet_hdr = */ true); + // The device keeps the flags of the queues that it already had. + EXPECT_CALL(mock_kernel_, ioctl(_, TUNGETIFF, _)) + .WillOnce([](Unused, Unused, void* argp) { + auto* ifr = reinterpret_cast<struct ifreq*>(argp); + ifr->ifr_flags = IFF_TUN | IFF_MULTI_QUEUE | IFF_NO_PI; + return 0; + }); + EXPECT_CALL(mock_kernel_, ioctl(_, TUNSETVNETHDRSZ, _)).Times(0); + TunTapDevice tun_device(kDeviceName, 1500, 1000, false, true, false, + &mock_kernel_, /* use_vnet_hdr = */ true); + EXPECT_FALSE(tun_device.Init()); + EXPECT_EQ(tun_device.GetReadFileDescriptor(), -1); + ExpectDown(/* fail = */ false); +} + +TEST_F(TunDeviceTest, VnetHdrNotTurnedOffByDeviceAlreadyInUse) { + SetInitExpectations(/* mtu = */ 1500, /* tx_queue_len = */ 1000, + /* persist = */ false); + // The device keeps the flags of the queues that it already had, which + // asked for the header. Packets would come with a header that nothing + // expects. + EXPECT_CALL(mock_kernel_, ioctl(_, TUNGETIFF, _)) + .WillOnce([](Unused, Unused, void* argp) { + auto* ifr = reinterpret_cast<struct ifreq*>(argp); + ifr->ifr_flags = IFF_TUN | IFF_MULTI_QUEUE | IFF_NO_PI | IFF_VNET_HDR; + return 0; + }); + TunTapDevice tun_device(kDeviceName, 1500, 1000, false, true, false, + &mock_kernel_); + EXPECT_FALSE(tun_device.Init()); + EXPECT_EQ(tun_device.GetReadFileDescriptor(), -1); + ExpectDown(/* fail = */ false); +} + +TEST_F(TunDeviceTest, FailToGetDeviceFlags) { + SetInitExpectations(/* mtu = */ 1500, /* tx_queue_len = */ 1000, + /* persist = */ false); + EXPECT_CALL(mock_kernel_, ioctl(_, TUNGETIFF, _)).WillOnce(Return(-1)); + TunTapDevice tun_device(kDeviceName, 1500, 1000, false, true, false, + &mock_kernel_); + EXPECT_FALSE(tun_device.Init()); + EXPECT_EQ(tun_device.GetReadFileDescriptor(), -1); + ExpectDown(/* fail = */ false); +} + +TEST_F(TunDeviceTest, FailToGetDeviceFlagsForVnetHdr) { + SetInitExpectations(/* mtu = */ 1500, /* tx_queue_len = */ 1000, + /* persist = */ false, /* is_tap = */ false, + /* use_vnet_hdr = */ true); + EXPECT_CALL(mock_kernel_, ioctl(_, TUNGETIFF, _)).WillOnce(Return(-1)); + TunTapDevice tun_device(kDeviceName, 1500, 1000, false, true, false, + &mock_kernel_, /* use_vnet_hdr = */ true); + EXPECT_FALSE(tun_device.Init()); + EXPECT_EQ(tun_device.GetReadFileDescriptor(), -1); + ExpectDown(/* fail = */ false); +} + +TEST_F(TunDeviceTest, FailToSetVnetHdrSize) { + SetInitExpectations(/* mtu = */ 1500, /* tx_queue_len = */ 1000, + /* persist = */ false, /* is_tap = */ false, + /* use_vnet_hdr = */ true); + EXPECT_CALL(mock_kernel_, ioctl(_, TUNSETVNETHDRSZ, _)).WillOnce(Return(-1)); + TunTapDevice tun_device(kDeviceName, 1500, 1000, false, true, false, + &mock_kernel_, /* use_vnet_hdr = */ true); + EXPECT_FALSE(tun_device.Init()); + EXPECT_EQ(tun_device.GetReadFileDescriptor(), -1); + ExpectDown(/* fail = */ false); +} + } // namespace } // namespace quic::test
diff --git a/quiche/quic/qbone/platform/virtio_net_header.h b/quiche/quic/qbone/platform/virtio_net_header.h new file mode 100644 index 0000000..b431ecc --- /dev/null +++ b/quiche/quic/qbone/platform/virtio_net_header.h
@@ -0,0 +1,18 @@ +// Copyright 2026 The Chromium Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef QUICHE_QUIC_QBONE_PLATFORM_VIRTIO_NET_HEADER_H_ +#define QUICHE_QUIC_QBONE_PLATFORM_VIRTIO_NET_HEADER_H_ + +// Provides `struct virtio_net_hdr`, the header that precedes each packet read +// from or written to a TUN device opened with IFF_VNET_HDR. +// +// <linux/virtio_net.h> can't be included from C++ as it is: it also declares +// `struct virtio_net_ctrl_hdr`, which TUN devices don't use and which has a +// member named `class`. So `class` is renamed for the length of the include. +#define class virtio_net_ctrl_hdr_class +#include <linux/virtio_net.h> // IWYU pragma: export +#undef class + +#endif // QUICHE_QUIC_QBONE_PLATFORM_VIRTIO_NET_HEADER_H_