From c11b066728c64d49d02ca91d8c483c2d556304a0 Mon Sep 17 00:00:00 2001 From: Xuewei Niu Date: Mon, 11 Dec 2023 10:07:02 +0800 Subject: [PATCH] runtime-rs: Use vhost-net device by default This patch set vhost-net as default backend of networking. It allows users to set `disable_vhost_net` to `true` to reenable virtio-net backend. Plus, which backend to use is a matter of hypervisor, runtime-rs will no longer need to know that. Fixes: #8608 Signed-off-by: Xuewei Niu --- .../crates/hypervisor/src/ch/inner_device.rs | 3 +- .../hypervisor/src/device/driver/mod.rs | 2 +- .../src/device/driver/virtio_net.rs | 9 --- .../hypervisor/src/dragonball/inner_device.rs | 5 +- .../crates/hypervisor/src/dragonball/mod.rs | 72 +++++++++---------- .../src/network/endpoint/ipvlan_endpoint.rs | 3 +- .../src/network/endpoint/macvlan_endpoint.rs | 3 +- .../src/network/endpoint/tap_endpoint.rs | 3 +- .../src/network/endpoint/veth_endpoint.rs | 3 +- .../src/network/endpoint/vlan_endpoint.rs | 3 +- 10 files changed, 45 insertions(+), 61 deletions(-) diff --git a/src/runtime-rs/crates/hypervisor/src/ch/inner_device.rs b/src/runtime-rs/crates/hypervisor/src/ch/inner_device.rs index 3d53320db3..c4aa39e818 100644 --- a/src/runtime-rs/crates/hypervisor/src/ch/inner_device.rs +++ b/src/runtime-rs/crates/hypervisor/src/ch/inner_device.rs @@ -475,7 +475,7 @@ impl TryFrom for FsConfig { #[cfg(test)] mod tests { use super::*; - use crate::{Address, Backend}; + use crate::Address; #[test] fn test_networkconfig_to_netconfig() { @@ -489,7 +489,6 @@ mod tests { allow_duplicate_mac: false, use_generic_irq: None, use_shared_irq: None, - backend: Backend::default(), }; let net = NetConfig::try_from(cfg.clone()); diff --git a/src/runtime-rs/crates/hypervisor/src/device/driver/mod.rs b/src/runtime-rs/crates/hypervisor/src/device/driver/mod.rs index c8eb70e564..2aff85b8c4 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/driver/mod.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/driver/mod.rs @@ -22,7 +22,7 @@ pub use virtio_blk::{ pub use virtio_fs::{ ShareFsConfig, ShareFsDevice, ShareFsMountConfig, ShareFsMountOperation, ShareFsMountType, }; -pub use virtio_net::{Address, Backend, NetworkConfig, NetworkDevice}; +pub use virtio_net::{Address, NetworkConfig, NetworkDevice}; pub use virtio_vsock::{ HybridVsockConfig, HybridVsockDevice, VsockConfig, VsockDevice, DEFAULT_GUEST_VSOCK_CID, }; diff --git a/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_net.rs b/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_net.rs index eac28d81d9..bc17e3f21a 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_net.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_net.rs @@ -26,20 +26,11 @@ impl fmt::Debug for Address { } } -#[derive(Clone, Debug, Default)] -pub enum Backend { - #[default] - Virtio, - Vhost, -} - #[derive(Clone, Debug, Default)] pub struct NetworkConfig { /// for detach, now it's default value 0. pub index: u64, - /// Network device backend - pub backend: Backend, /// Host level path for the guest network interface. pub host_dev_name: String, /// Guest iface name for the guest network interface. diff --git a/src/runtime-rs/crates/hypervisor/src/dragonball/inner_device.rs b/src/runtime-rs/crates/hypervisor/src/dragonball/inner_device.rs index 9484d3dc42..5836f92653 100644 --- a/src/runtime-rs/crates/hypervisor/src/dragonball/inner_device.rs +++ b/src/runtime-rs/crates/hypervisor/src/dragonball/inner_device.rs @@ -12,7 +12,7 @@ use dragonball::api::v1::{ }; use dragonball::device_manager::blk_dev_mgr::BlockDeviceType; -use super::DragonballInner; +use super::{build_dragonball_network_config, DragonballInner}; use crate::{ device::DeviceType, HybridVsockConfig, NetworkConfig, ShareFsConfig, ShareFsMountConfig, ShareFsMountOperation, ShareFsMountType, VfioBusMode, VfioDevice, VmmState, JAILER_ROOT, @@ -210,8 +210,9 @@ impl DragonballInner { } fn add_net_device(&mut self, config: &NetworkConfig) -> Result<()> { + let net_cfg = build_dragonball_network_config(&self.config, config); self.vmm_instance - .insert_network_device(config.into()) + .insert_network_device(net_cfg) .context("insert network device") } diff --git a/src/runtime-rs/crates/hypervisor/src/dragonball/mod.rs b/src/runtime-rs/crates/hypervisor/src/dragonball/mod.rs index 399f8ca1b3..f0ed02f706 100644 --- a/src/runtime-rs/crates/hypervisor/src/dragonball/mod.rs +++ b/src/runtime-rs/crates/hypervisor/src/dragonball/mod.rs @@ -26,7 +26,7 @@ use kata_types::config::hypervisor::Hypervisor as HypervisorConfig; use tokio::sync::RwLock; use tracing::instrument; -use crate::{Backend, DeviceType, Hypervisor, NetworkConfig, VcpuThreadIds}; +use crate::{DeviceType, Hypervisor, NetworkConfig, VcpuThreadIds}; pub struct Dragonball { inner: Arc>, @@ -201,41 +201,39 @@ impl Persist for Dragonball { } } -impl From for DragonballNetworkConfig { - fn from(value: NetworkConfig) -> Self { - let r = &value; - r.into() - } -} - -impl From<&NetworkConfig> for DragonballNetworkConfig { - fn from(value: &NetworkConfig) -> Self { - let virtio_config = DragonballVirtioConfig { - iface_id: value.virt_iface_name.clone(), - host_dev_name: value.host_dev_name.clone(), - // TODO(justxuewei): rx_rate_limiter is not supported, see: - // https://github.com/kata-containers/kata-containers/issues/8327. - rx_rate_limiter: None, - // TODO(justxuewei): tx_rate_limiter is not supported, see: - // https://github.com/kata-containers/kata-containers/issues/8327. - tx_rate_limiter: None, - allow_duplicate_mac: value.allow_duplicate_mac, - }; - let backend = match value.backend { - Backend::Virtio => DragonballBackend::Virtio(virtio_config), - Backend::Vhost => DragonballBackend::Vhost(virtio_config), - }; - - Self { - num_queues: Some(value.queue_num), - queue_size: Some(value.queue_size as u16), - backend, - guest_mac: value.guest_mac.clone().map(|mac| { - // We are safety since mac address is checked by endpoints. - DragonballMacAddr::from_bytes(&mac.0).unwrap() - }), - use_shared_irq: value.use_shared_irq, - use_generic_irq: value.use_generic_irq, - } +/// Generate Dragonball network config according to hypervisor config and +/// runtime network config. +pub(crate) fn build_dragonball_network_config( + hconfig: &HypervisorConfig, + nconfig: &NetworkConfig, +) -> DragonballNetworkConfig { + let virtio_config = DragonballVirtioConfig { + iface_id: nconfig.virt_iface_name.clone(), + host_dev_name: nconfig.host_dev_name.clone(), + // TODO(justxuewei): rx_rate_limiter is not supported, see: + // https://github.com/kata-containers/kata-containers/issues/8327. + rx_rate_limiter: None, + // TODO(justxuewei): tx_rate_limiter is not supported, see: + // https://github.com/kata-containers/kata-containers/issues/8327. + tx_rate_limiter: None, + allow_duplicate_mac: nconfig.allow_duplicate_mac, + }; + + let backend = if hconfig.network_info.disable_vhost_net { + DragonballBackend::Virtio(virtio_config) + } else { + DragonballBackend::Vhost(virtio_config) + }; + + DragonballNetworkConfig { + num_queues: Some(nconfig.queue_num), + queue_size: Some(nconfig.queue_size as u16), + backend, + guest_mac: nconfig.guest_mac.clone().map(|mac| { + // We are safety since mac address is checked by endpoints. + DragonballMacAddr::from_bytes(&mac.0).unwrap() + }), + use_shared_irq: nconfig.use_shared_irq, + use_generic_irq: nconfig.use_generic_irq, } } diff --git a/src/runtime-rs/crates/resource/src/network/endpoint/ipvlan_endpoint.rs b/src/runtime-rs/crates/resource/src/network/endpoint/ipvlan_endpoint.rs index eb63d45192..bc075186d1 100644 --- a/src/runtime-rs/crates/resource/src/network/endpoint/ipvlan_endpoint.rs +++ b/src/runtime-rs/crates/resource/src/network/endpoint/ipvlan_endpoint.rs @@ -12,7 +12,7 @@ use async_trait::async_trait; use hypervisor::device::device_manager::{do_handle_device, DeviceManager}; use hypervisor::device::driver::NetworkConfig; use hypervisor::device::{DeviceConfig, DeviceType}; -use hypervisor::{Backend, Hypervisor, NetworkDevice}; +use hypervisor::{Hypervisor, NetworkDevice}; use tokio::sync::RwLock; use super::endpoint_persist::{EndpointState, IpVlanEndpointState}; @@ -57,7 +57,6 @@ impl IPVlanEndpoint { Ok(NetworkConfig { host_dev_name: iface.name.clone(), virt_iface_name: self.net_pair.virt_iface.name.clone(), - backend: Backend::Virtio, guest_mac: Some(guest_mac), ..Default::default() }) diff --git a/src/runtime-rs/crates/resource/src/network/endpoint/macvlan_endpoint.rs b/src/runtime-rs/crates/resource/src/network/endpoint/macvlan_endpoint.rs index d3c6862bf6..633db428f0 100644 --- a/src/runtime-rs/crates/resource/src/network/endpoint/macvlan_endpoint.rs +++ b/src/runtime-rs/crates/resource/src/network/endpoint/macvlan_endpoint.rs @@ -12,7 +12,7 @@ use async_trait::async_trait; use hypervisor::device::device_manager::{do_handle_device, DeviceManager}; use hypervisor::device::driver::NetworkConfig; use hypervisor::device::{DeviceConfig, DeviceType}; -use hypervisor::{Backend, Hypervisor, NetworkDevice}; +use hypervisor::{Hypervisor, NetworkDevice}; use tokio::sync::RwLock; use super::endpoint_persist::{EndpointState, MacvlanEndpointState}; @@ -56,7 +56,6 @@ impl MacVlanEndpoint { Ok(NetworkConfig { host_dev_name: iface.name.clone(), virt_iface_name: self.net_pair.virt_iface.name.clone(), - backend: Backend::Virtio, guest_mac: Some(guest_mac), ..Default::default() }) diff --git a/src/runtime-rs/crates/resource/src/network/endpoint/tap_endpoint.rs b/src/runtime-rs/crates/resource/src/network/endpoint/tap_endpoint.rs index eae12699fc..e22a91d922 100644 --- a/src/runtime-rs/crates/resource/src/network/endpoint/tap_endpoint.rs +++ b/src/runtime-rs/crates/resource/src/network/endpoint/tap_endpoint.rs @@ -10,7 +10,7 @@ use anyhow::{Context, Result}; use async_trait::async_trait; use hypervisor::device::device_manager::{do_handle_device, DeviceManager}; use hypervisor::device::{DeviceConfig, DeviceType}; -use hypervisor::{Backend, Hypervisor, NetworkConfig, NetworkDevice}; +use hypervisor::{Hypervisor, NetworkConfig, NetworkDevice}; use tokio::sync::RwLock; use super::endpoint_persist::TapEndpointState; @@ -76,7 +76,6 @@ impl TapEndpoint { Ok(NetworkConfig { host_dev_name: self.tap_iface.name.clone(), virt_iface_name: self.name.clone(), - backend: Backend::Virtio, guest_mac: Some(guest_mac), queue_num: self.queue_num, queue_size: self.queue_size, diff --git a/src/runtime-rs/crates/resource/src/network/endpoint/veth_endpoint.rs b/src/runtime-rs/crates/resource/src/network/endpoint/veth_endpoint.rs index 807250d23e..e7c758c206 100644 --- a/src/runtime-rs/crates/resource/src/network/endpoint/veth_endpoint.rs +++ b/src/runtime-rs/crates/resource/src/network/endpoint/veth_endpoint.rs @@ -12,7 +12,7 @@ use async_trait::async_trait; use hypervisor::device::device_manager::{do_handle_device, DeviceManager}; use hypervisor::device::driver::NetworkConfig; use hypervisor::device::{DeviceConfig, DeviceType}; -use hypervisor::{Backend, Hypervisor, NetworkDevice}; +use hypervisor::{Hypervisor, NetworkDevice}; use tokio::sync::RwLock; use super::endpoint_persist::{EndpointState, VethEndpointState}; @@ -56,7 +56,6 @@ impl VethEndpoint { Ok(NetworkConfig { host_dev_name: iface.name.clone(), virt_iface_name: self.net_pair.virt_iface.name.clone(), - backend: Backend::Virtio, guest_mac: Some(guest_mac), ..Default::default() }) diff --git a/src/runtime-rs/crates/resource/src/network/endpoint/vlan_endpoint.rs b/src/runtime-rs/crates/resource/src/network/endpoint/vlan_endpoint.rs index f8e09077b5..9617c029fe 100644 --- a/src/runtime-rs/crates/resource/src/network/endpoint/vlan_endpoint.rs +++ b/src/runtime-rs/crates/resource/src/network/endpoint/vlan_endpoint.rs @@ -12,7 +12,7 @@ use async_trait::async_trait; use hypervisor::device::device_manager::{do_handle_device, DeviceManager}; use hypervisor::device::driver::NetworkConfig; use hypervisor::device::{DeviceConfig, DeviceType}; -use hypervisor::{Backend, Hypervisor, NetworkDevice}; +use hypervisor::{Hypervisor, NetworkDevice}; use tokio::sync::RwLock; use super::endpoint_persist::{EndpointState, VlanEndpointState}; @@ -56,7 +56,6 @@ impl VlanEndpoint { Ok(NetworkConfig { host_dev_name: iface.name.clone(), virt_iface_name: self.net_pair.virt_iface.name.clone(), - backend: Backend::Virtio, guest_mac: Some(guest_mac), ..Default::default() })