net_util: Implement IntoAsync + try_clone() for Tap

BUG=none
TEST=Use it in a later change

Change-Id: I6fa8084dd47e84e6a67fdf4bcc194552d8f9f45a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2891121
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
This commit is contained in:
Chirantan Ekbote 2021-05-12 19:59:45 +09:00 committed by Commit Bot
parent d2399b7d6f
commit 0c6d96af7b
2 changed files with 20 additions and 0 deletions

View file

@ -9,3 +9,4 @@ libc = "*"
data_model = { path = "../data_model" }
net_sys = { path = "../net_sys" }
base = { path = "../base" }
cros_async = { path = "../cros_async" }

View file

@ -20,6 +20,7 @@ use base::{
ioctl_with_mut_ref, ioctl_with_ref, ioctl_with_val, volatile_impl, AsRawDescriptor,
FromRawDescriptor, IoctlNr, RawDescriptor,
};
use cros_async::IntoAsync;
#[derive(Debug)]
pub enum Error {
@ -29,6 +30,8 @@ pub enum Error {
OpenTun(SysError),
/// Unable to create tap interface.
CreateTap(SysError),
/// Unable to clone tap interface.
CloneTap(SysError),
/// ioctl failed.
IoctlError(SysError),
}
@ -42,6 +45,7 @@ impl Display for Error {
CreateSocket(e) => write!(f, "failed to create a socket: {}", e),
OpenTun(e) => write!(f, "failed to open /dev/net/tun: {}", e),
CreateTap(e) => write!(f, "failed to create tap interface: {}", e),
CloneTap(e) => write!(f, "failed to clone tap interface: {}", e),
IoctlError(e) => write!(f, "ioctl failed: {}", e),
}
}
@ -53,6 +57,7 @@ impl Error {
Error::CreateSocket(e) => e,
Error::OpenTun(e) => e,
Error::CreateTap(e) => e,
Error::CloneTap(e) => e,
Error::IoctlError(e) => e,
}
}
@ -227,6 +232,18 @@ impl Tap {
if_flags: unsafe { ifreq.ifr_ifru.ifru_flags },
})
}
pub fn try_clone(&self) -> Result<Tap> {
self.tap_file
.try_clone()
.map(|tap_file| Tap {
tap_file,
if_name: self.if_name,
if_flags: self.if_flags,
})
.map_err(SysError::from)
.map_err(Error::CloneTap)
}
}
pub trait TapT: FileReadWriteVolatile + Read + Write + AsRawDescriptor + Send + Sized {
@ -527,6 +544,8 @@ impl AsRawDescriptor for Tap {
}
}
impl IntoAsync for Tap {}
volatile_impl!(Tap);
pub mod fakes {