From ca48f1c995da792fa4c555ab49d90e0166ca5b82 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Wed, 16 Dec 2020 17:00:59 +0000 Subject: [PATCH] virtio-devices: net: Support creating a device from a TAP fd Add support for creating virtio-net device from existing TAP fd. Currently only a single fd and thus no-more than 2 queues (one pair) is suppored. Fixes: #2052 Signed-off-by: Rob Bradford --- virtio-devices/src/net.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/virtio-devices/src/net.rs b/virtio-devices/src/net.rs index 0d2c39c55..e98cc543c 100644 --- a/virtio-devices/src/net.rs +++ b/virtio-devices/src/net.rs @@ -18,13 +18,13 @@ use crate::seccomp_filters::{get_seccomp_filter, Thread}; use crate::VirtioInterrupt; use anyhow::anyhow; use net_util::{ - open_tap, MacAddr, NetCounters, NetQueuePair, OpenTapError, RxVirtio, Tap, TxVirtio, + open_tap, MacAddr, NetCounters, NetQueuePair, OpenTapError, RxVirtio, Tap, TapError, TxVirtio, }; use seccomp::{SeccompAction, SeccompFilter}; use std::collections::HashMap; use std::net::Ipv4Addr; use std::num::Wrapping; -use std::os::unix::io::AsRawFd; +use std::os::unix::io::{AsRawFd, RawFd}; use std::result; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Barrier}; @@ -50,6 +50,9 @@ pub const RX_TAP_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 3; pub enum Error { /// Failed to open taps. OpenTap(OpenTapError), + + // Using existing tap + TapError(TapError), } pub type Result = result::Result; @@ -293,6 +296,26 @@ impl Net { ) } + pub fn from_tap_fd( + id: String, + fd: RawFd, + guest_mac: Option, + iommu: bool, + queue_size: u16, + seccomp_action: SeccompAction, + ) -> Result { + let tap = Tap::from_tap_fd(fd).map_err(Error::TapError)?; + Self::new_with_tap( + id, + vec![tap], + guest_mac, + iommu, + 2, + queue_size, + seccomp_action, + ) + } + fn state(&self) -> NetState { NetState { avail_features: self.common.avail_features,