diff --git a/src/runtime-rs/crates/hypervisor/src/device/device_manager.rs b/src/runtime-rs/crates/hypervisor/src/device/device_manager.rs index 967bc42d86..4d14dea88f 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/device_manager.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/device_manager.rs @@ -12,8 +12,8 @@ use tokio::sync::{Mutex, RwLock}; use crate::{ vhost_user_blk::VhostUserBlkDevice, BlockConfig, BlockDevice, HybridVsockDevice, Hypervisor, - NetworkBackend, NetworkDevice, VfioDevice, VhostUserConfig, KATA_BLK_DEV_TYPE, - KATA_MMIO_BLK_DEV_TYPE, KATA_NVDIMM_DEV_TYPE, VIRTIO_BLOCK_MMIO, VIRTIO_BLOCK_PCI, VIRTIO_PMEM, + NetworkDevice, VfioDevice, VhostUserConfig, KATA_BLK_DEV_TYPE, KATA_MMIO_BLK_DEV_TYPE, + KATA_NVDIMM_DEV_TYPE, VIRTIO_BLOCK_MMIO, VIRTIO_BLOCK_PCI, VIRTIO_PMEM, }; use super::{ @@ -221,18 +221,11 @@ impl DeviceManager { return Some(device_id.to_string()); } } - DeviceType::Network(device) => match device.config.backend { - NetworkBackend::Virtio(config) => { - if config.host_dev_name == host_path { - return Some(device_id.to_string()); - } + DeviceType::Network(device) => { + if device.config.host_dev_name == host_path { + return Some(device_id.to_string()); } - NetworkBackend::Vhost(config) => { - if config.host_dev_name == host_path { - return Some(device_id.to_string()); - } - } - }, + } _ => { // TODO: support find other device type continue; @@ -314,10 +307,7 @@ impl DeviceManager { } DeviceConfig::NetworkCfg(config) => { // try to find the device, found and just return id. - let host_path = match &config.backend { - NetworkBackend::Virtio(config) => &config.host_dev_name, - NetworkBackend::Vhost(config) => &config.host_dev_name, - }; + let host_path = config.host_dev_name.as_str(); if let Some(dev_id_matched) = self.find_device(host_path.to_owned()).await { info!( sl!(), 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 22b833205a..0818d7cec2 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/driver/mod.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/driver/mod.rs @@ -23,8 +23,7 @@ pub use virtio_fs::{ ShareFsDevice, ShareFsDeviceConfig, ShareFsMountConfig, ShareFsMountDevice, ShareFsMountType, ShareFsOperation, }; -pub use virtio_net::{Address, NetworkBackend, NetworkConfig, NetworkDevice, VirtioConfig}; -pub use virtio_net::{Address, NetworkConfig, NetworkDevice}; +pub use virtio_net::{Address, Backend, 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 e2b389e5e7..c05503ab53 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,27 +26,11 @@ impl fmt::Debug for Address { } } -#[derive(Clone, Debug)] -pub enum NetworkBackend { - Virtio(VirtioConfig), - Vhost(VirtioConfig), -} - -impl Default for NetworkBackend { - fn default() -> Self { - Self::Virtio(VirtioConfig::default()) - } -} - -/// Virtio network backend config #[derive(Clone, Debug, Default)] -pub struct VirtioConfig { - /// Host level path for the guest network interface. - pub host_dev_name: String, - /// Guest iface name for the guest network interface. - pub virt_iface_name: String, - /// Allow duplicate mac - pub allow_duplicate_mac: bool, +pub enum Backend { + #[default] + Virtio, + Vhost, } #[derive(Clone, Debug, Default)] @@ -55,7 +39,11 @@ pub struct NetworkConfig { pub index: u64, /// Network device backend - pub backend: NetworkBackend, + pub backend: Backend, + /// Host level path for the guest network interface. + pub host_dev_name: String, + /// Guest iface name for the guest network interface. + pub virt_iface_name: String, /// Guest MAC address. pub guest_mac: Option
, /// Virtio queue size @@ -66,6 +54,8 @@ pub struct NetworkConfig { pub use_shared_irq: Option, /// Use generic irq pub use_generic_irq: Option, + /// Allow duplicate mac + pub allow_duplicate_mac: bool, } #[derive(Clone, Debug, Default)] diff --git a/src/runtime-rs/crates/hypervisor/src/dragonball/mod.rs b/src/runtime-rs/crates/hypervisor/src/dragonball/mod.rs index d9efc40667..5376d8343f 100644 --- a/src/runtime-rs/crates/hypervisor/src/dragonball/mod.rs +++ b/src/runtime-rs/crates/hypervisor/src/dragonball/mod.rs @@ -18,7 +18,7 @@ use anyhow::{Context, Result}; use async_trait::async_trait; use dbs_utils::net::MacAddr as DragonballMacAddr; use dragonball::api::v1::{ - Backend as DragonballNetworkBackend, NetworkInterfaceConfig as DragonballNetworkConfig, + Backend as DragonballBackend, NetworkInterfaceConfig as DragonballNetworkConfig, VirtioConfig as DragonballVirtioConfig, }; use kata_types::capabilities::Capabilities; @@ -26,7 +26,7 @@ use kata_types::config::hypervisor::Hypervisor as HypervisorConfig; use tokio::sync::RwLock; use tracing::instrument; -use crate::{DeviceType, Hypervisor, NetworkBackend, NetworkConfig, VcpuThreadIds}; +use crate::{Backend, DeviceType, Hypervisor, NetworkConfig, VcpuThreadIds}; pub struct Dragonball { inner: Arc>, @@ -196,27 +196,6 @@ impl Persist for Dragonball { } } -impl From for DragonballNetworkBackend { - fn from(value: NetworkBackend) -> Self { - match value { - NetworkBackend::Virtio(config) => Self::Virtio(DragonballVirtioConfig { - iface_id: config.virt_iface_name.clone(), - host_dev_name: config.host_dev_name.clone(), - rx_rate_limiter: None, - tx_rate_limiter: None, - allow_duplicate_mac: config.allow_duplicate_mac, - }), - NetworkBackend::Vhost(config) => Self::Vhost(DragonballVirtioConfig { - iface_id: config.virt_iface_name.clone(), - host_dev_name: config.host_dev_name.clone(), - rx_rate_limiter: None, - tx_rate_limiter: None, - allow_duplicate_mac: config.allow_duplicate_mac, - }), - } - } -} - impl From for DragonballNetworkConfig { fn from(value: NetworkConfig) -> Self { let r = &value; @@ -226,10 +205,24 @@ impl From for DragonballNetworkConfig { 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. + rx_rate_limiter: None, + // TODO(justxuewei): tx_rate_limiter is not supported. + 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: value.backend.clone().into(), + 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() 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 13300c0f86..eb63d45192 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 @@ -4,28 +4,19 @@ // SPDX-License-Identifier: Apache-2.0 // -use std::{ - io::{self, Error}, - sync::Arc, -}; +use std::io::{self, Error}; +use std::sync::Arc; use anyhow::{Context, Result}; 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 tokio::sync::RwLock; -use hypervisor::{ - device::{ - device_manager::{do_handle_device, DeviceManager}, - driver::NetworkConfig, - DeviceConfig, DeviceType, - }, - Hypervisor, NetworkBackend, NetworkDevice, VirtioConfig, -}; - -use super::{ - endpoint_persist::{EndpointState, IpVlanEndpointState}, - Endpoint, -}; +use super::endpoint_persist::{EndpointState, IpVlanEndpointState}; +use super::Endpoint; use crate::network::{network_model::TC_FILTER_NET_MODEL_STR, utils, NetworkPair}; // IPVlanEndpoint is the endpoint bridged to VM @@ -64,11 +55,9 @@ impl IPVlanEndpoint { })?; Ok(NetworkConfig { - backend: NetworkBackend::Virtio(VirtioConfig { - host_dev_name: iface.name.clone(), - virt_iface_name: self.net_pair.virt_iface.name.clone(), - ..Default::default() - }), + 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 d5db2df2b5..d3c6862bf6 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 @@ -4,28 +4,19 @@ // SPDX-License-Identifier: Apache-2.0 // -use std::{ - io::{self, Error}, - sync::Arc, -}; +use std::io::{self, Error}; +use std::sync::Arc; use anyhow::{Context, Result}; 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 tokio::sync::RwLock; -use hypervisor::{ - device::{ - device_manager::{do_handle_device, DeviceManager}, - driver::NetworkConfig, - DeviceConfig, DeviceType, - }, - Hypervisor, NetworkBackend, NetworkDevice, VirtioConfig, -}; - -use super::{ - endpoint_persist::{EndpointState, MacvlanEndpointState}, - Endpoint, -}; +use super::endpoint_persist::{EndpointState, MacvlanEndpointState}; +use super::Endpoint; use crate::network::{utils, NetworkPair}; #[derive(Debug)] @@ -63,11 +54,9 @@ impl MacVlanEndpoint { })?; Ok(NetworkConfig { - backend: NetworkBackend::Virtio(VirtioConfig { - host_dev_name: iface.name.clone(), - virt_iface_name: self.net_pair.virt_iface.name.clone(), - ..Default::default() - }), + 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 0b5db23f96..eae12699fc 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::{Hypervisor, NetworkBackend, NetworkConfig, NetworkDevice, VirtioConfig}; +use hypervisor::{Backend, Hypervisor, NetworkConfig, NetworkDevice}; use tokio::sync::RwLock; use super::endpoint_persist::TapEndpointState; @@ -74,11 +74,9 @@ impl TapEndpoint { fn get_network_config(&self) -> Result { let guest_mac = utils::parse_mac(&self.guest_mac).context("Parse mac address")?; Ok(NetworkConfig { - backend: NetworkBackend::Virtio(VirtioConfig { - host_dev_name: self.tap_iface.name.clone(), - virt_iface_name: self.name.clone(), - ..Default::default() - }), + 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 131de67362..807250d23e 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 @@ -4,28 +4,19 @@ // SPDX-License-Identifier: Apache-2.0 // -use std::{ - io::{self, Error}, - sync::Arc, -}; +use std::io::{self, Error}; +use std::sync::Arc; use anyhow::{Context, Result}; 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 tokio::sync::RwLock; -use hypervisor::{ - device::{ - device_manager::{do_handle_device, DeviceManager}, - driver::NetworkConfig, - DeviceConfig, DeviceType, - }, - Hypervisor, NetworkBackend, NetworkDevice, VirtioConfig, -}; - -use super::{ - endpoint_persist::{EndpointState, VethEndpointState}, - Endpoint, -}; +use super::endpoint_persist::{EndpointState, VethEndpointState}; +use super::Endpoint; use crate::network::{utils, NetworkPair}; #[derive(Debug)] @@ -63,11 +54,9 @@ impl VethEndpoint { })?; Ok(NetworkConfig { - backend: NetworkBackend::Virtio(VirtioConfig { - host_dev_name: iface.name.clone(), - virt_iface_name: self.net_pair.virt_iface.name.clone(), - ..Default::default() - }), + 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 770390b568..f8e09077b5 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 @@ -4,29 +4,21 @@ // SPDX-License-Identifier: Apache-2.0 // -use std::{ - io::{self, Error}, - sync::Arc, -}; +use std::io::{self, Error}; +use std::sync::Arc; use anyhow::{Context, Result}; 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 tokio::sync::RwLock; -use hypervisor::{ - device::{ - device_manager::{do_handle_device, DeviceManager}, - driver::NetworkConfig, - DeviceConfig, DeviceType, - }, - Hypervisor, NetworkBackend, NetworkDevice, VirtioConfig, -}; - -use super::{ - endpoint_persist::{EndpointState, VlanEndpointState}, - Endpoint, -}; -use crate::network::{network_model::TC_FILTER_NET_MODEL_STR, utils, NetworkPair}; +use super::endpoint_persist::{EndpointState, VlanEndpointState}; +use super::Endpoint; +use crate::network::network_model::TC_FILTER_NET_MODEL_STR; +use crate::network::{utils, NetworkPair}; #[derive(Debug)] pub struct VlanEndpoint { @@ -62,11 +54,9 @@ impl VlanEndpoint { })?; Ok(NetworkConfig { - backend: NetworkBackend::Virtio(VirtioConfig { - host_dev_name: iface.name.clone(), - virt_iface_name: self.net_pair.virt_iface.name.clone(), - ..Default::default() - }), + 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() })