tap: ensure the tap file is O_NONBLOCK

CrosVM must ensure that the tap file it uses is O_NONBLOCK, otherwise
the networking is very slow. Currently CrosVM uses O_NONBLOCK when
opening the tap file directly in create_tap_with_ifreq, and it must
ensure the same behavior if the tap file is created from a raw
descriptor.

BUG=b:257304931

Change-Id: I32ce42085ba312993dce32c457a8e5376b94c46b
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4003798
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Peter Oskolkov <posk@google.com>
This commit is contained in:
Peter Oskolkov 2022-11-03 20:12:02 +00:00 committed by crosvm LUCI
parent a2853028f1
commit 84edec5323

View file

@ -13,6 +13,7 @@ use std::os::unix::io::AsRawFd;
use std::os::unix::io::FromRawFd;
use std::os::unix::io::RawFd;
use base::add_fd_flags;
use base::error;
use base::ioctl_with_mut_ref;
use base::ioctl_with_ref;
@ -55,6 +56,10 @@ impl Tap {
pub unsafe fn from_raw_descriptor(descriptor: RawDescriptor) -> Result<Tap> {
let tap_file = File::from_raw_descriptor(descriptor);
// Ensure that the file is opened non-blocking, otherwise
// ipvtaps with shell-provided FDs are very slow.
add_fd_flags(tap_file.as_raw_descriptor(), libc::O_NONBLOCK).map_err(Error::IoctlError)?;
// Get the interface name since we will need it for some ioctls.
let mut ifreq: net_sys::ifreq = Default::default();
let ret = ioctl_with_mut_ref(&tap_file, net_sys::TUNGETIFF(), &mut ifreq);