From da58b65997cb884ad745912c4dfc74f0b90b9e90 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Thu, 22 Apr 2021 10:24:15 +0100 Subject: [PATCH] virtio-devices: net: Support rebooting when tap fd specfied Duplicate the fd that is specified in the config so that be used again after a reboot. When rebooting we destroy all VM state and restore from the config. Signed-off-by: Rob Bradford --- virtio-devices/src/net.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/virtio-devices/src/net.rs b/virtio-devices/src/net.rs index bfce598c1..b95fd77a4 100644 --- a/virtio-devices/src/net.rs +++ b/virtio-devices/src/net.rs @@ -54,6 +54,9 @@ pub enum Error { // Using existing tap TapError(TapError), + + // Error calling dup() on tap fd + DuplicateTapFd(std::io::Error), } pub type Result = result::Result; @@ -397,7 +400,12 @@ impl Net { let num_queue_pairs = fds.len(); for fd in fds.iter() { - let tap = Tap::from_tap_fd(*fd, num_queue_pairs).map_err(Error::TapError)?; + // Duplicate so that it can survive reboots + let fd = unsafe { libc::dup(*fd) }; + if fd < 0 { + return Err(Error::DuplicateTapFd(std::io::Error::last_os_error())); + } + let tap = Tap::from_tap_fd(fd, num_queue_pairs).map_err(Error::TapError)?; taps.push(tap); }