From 9fad680db14bd2e7263ac8c3ceb9a130122ef21e Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Wed, 2 Oct 2019 14:26:02 -0700 Subject: [PATCH] vm-virtio: Add IOMMU support to virtio-net Adding virtio feature VIRTIO_F_IOMMU_PLATFORM when explicitly asked by the user. The need for this feature is to be able to attach the virtio device to a virtual IOMMU. Signed-off-by: Sebastien Boeuf --- vm-virtio/src/net.rs | 15 ++++++++++++--- vmm/src/device_manager.rs | 4 ++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/vm-virtio/src/net.rs b/vm-virtio/src/net.rs index 89b9568fe..beae9b88b 100644 --- a/vm-virtio/src/net.rs +++ b/vm-virtio/src/net.rs @@ -445,7 +445,7 @@ pub struct Net { impl Net { /// Create a new virtio network device with the given TAP interface. - pub fn new_with_tap(tap: Tap, guest_mac: Option<&MacAddr>) -> Result { + pub fn new_with_tap(tap: Tap, guest_mac: Option<&MacAddr>, iommu: bool) -> Result { // Set offload flags to match the virtio features below. tap.set_offload( net_gen::TUN_F_CSUM | net_gen::TUN_F_UFO | net_gen::TUN_F_TSO4 | net_gen::TUN_F_TSO6, @@ -464,6 +464,10 @@ impl Net { | 1 << VIRTIO_NET_F_HOST_UFO | 1 << VIRTIO_F_VERSION_1; + if iommu { + avail_features |= 1u64 << VIRTIO_F_IOMMU_PLATFORM; + } + let mut config_space; if let Some(mac) = guest_mac { config_space = Vec::with_capacity(MAC_ADDR_LEN); @@ -490,13 +494,18 @@ impl Net { /// Create a new virtio network device with the given IP address and /// netmask. - pub fn new(ip_addr: Ipv4Addr, netmask: Ipv4Addr, guest_mac: Option<&MacAddr>) -> Result { + pub fn new( + ip_addr: Ipv4Addr, + netmask: Ipv4Addr, + guest_mac: Option<&MacAddr>, + iommu: bool, + ) -> Result { let tap = Tap::new().map_err(Error::TapOpen)?; tap.set_ip_addr(ip_addr).map_err(Error::TapSetIp)?; tap.set_netmask(netmask).map_err(Error::TapSetNetmask)?; tap.enable().map_err(Error::TapEnable)?; - Self::new_with_tap(tap, guest_mac) + Self::new_with_tap(tap, guest_mac, iommu) } } diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index cf5504d4c..8548c462f 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -612,10 +612,10 @@ impl DeviceManager { for net_cfg in net_list_cfg.iter() { let virtio_net_device = if let Some(ref tap_if_name) = net_cfg.tap { let tap = Tap::open_named(tap_if_name).map_err(DeviceManagerError::OpenTap)?; - vm_virtio::Net::new_with_tap(tap, Some(&net_cfg.mac)) + vm_virtio::Net::new_with_tap(tap, Some(&net_cfg.mac), false) .map_err(DeviceManagerError::CreateVirtioNet)? } else { - vm_virtio::Net::new(net_cfg.ip, net_cfg.mask, Some(&net_cfg.mac)) + vm_virtio::Net::new(net_cfg.ip, net_cfg.mask, Some(&net_cfg.mac), false) .map_err(DeviceManagerError::CreateVirtioNet)? };