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 <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2020-12-16 17:00:59 +00:00 committed by Samuel Ortiz
parent 444905071b
commit ca48f1c995

View File

@ -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<T> = result::Result<T, Error>;
@ -293,6 +296,26 @@ impl Net {
)
}
pub fn from_tap_fd(
id: String,
fd: RawFd,
guest_mac: Option<MacAddr>,
iommu: bool,
queue_size: u16,
seccomp_action: SeccompAction,
) -> Result<Self> {
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,