runtime-rs: Remove virtio config from Backend

Virtio-net and vhost-net share a common virtio config, and vhost-user-net
uses another config, named `VhostUserConfig`. Thus, the virtio config could
be added into `NetworkConfig` instead of `Backend`.

Signed-off-by: Xuewei Niu <niuxuewei.nxw@antgroup.com>
This commit is contained in:
Xuewei Niu
2023-09-06 10:48:25 +08:00
parent ad66378bf5
commit 8ea87405ed
9 changed files with 86 additions and 159 deletions

View File

@@ -12,8 +12,8 @@ use tokio::sync::{Mutex, RwLock};
use crate::{ use crate::{
vhost_user_blk::VhostUserBlkDevice, BlockConfig, BlockDevice, HybridVsockDevice, Hypervisor, vhost_user_blk::VhostUserBlkDevice, BlockConfig, BlockDevice, HybridVsockDevice, Hypervisor,
NetworkBackend, NetworkDevice, VfioDevice, VhostUserConfig, KATA_BLK_DEV_TYPE, NetworkDevice, VfioDevice, VhostUserConfig, KATA_BLK_DEV_TYPE, KATA_MMIO_BLK_DEV_TYPE,
KATA_MMIO_BLK_DEV_TYPE, KATA_NVDIMM_DEV_TYPE, VIRTIO_BLOCK_MMIO, VIRTIO_BLOCK_PCI, VIRTIO_PMEM, KATA_NVDIMM_DEV_TYPE, VIRTIO_BLOCK_MMIO, VIRTIO_BLOCK_PCI, VIRTIO_PMEM,
}; };
use super::{ use super::{
@@ -221,18 +221,11 @@ impl DeviceManager {
return Some(device_id.to_string()); return Some(device_id.to_string());
} }
} }
DeviceType::Network(device) => match device.config.backend { DeviceType::Network(device) => {
NetworkBackend::Virtio(config) => { if device.config.host_dev_name == host_path {
if config.host_dev_name == host_path { return Some(device_id.to_string());
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 // TODO: support find other device type
continue; continue;
@@ -314,10 +307,7 @@ impl DeviceManager {
} }
DeviceConfig::NetworkCfg(config) => { DeviceConfig::NetworkCfg(config) => {
// try to find the device, found and just return id. // try to find the device, found and just return id.
let host_path = match &config.backend { let host_path = config.host_dev_name.as_str();
NetworkBackend::Virtio(config) => &config.host_dev_name,
NetworkBackend::Vhost(config) => &config.host_dev_name,
};
if let Some(dev_id_matched) = self.find_device(host_path.to_owned()).await { if let Some(dev_id_matched) = self.find_device(host_path.to_owned()).await {
info!( info!(
sl!(), sl!(),

View File

@@ -23,8 +23,7 @@ pub use virtio_fs::{
ShareFsDevice, ShareFsDeviceConfig, ShareFsMountConfig, ShareFsMountDevice, ShareFsMountType, ShareFsDevice, ShareFsDeviceConfig, ShareFsMountConfig, ShareFsMountDevice, ShareFsMountType,
ShareFsOperation, ShareFsOperation,
}; };
pub use virtio_net::{Address, NetworkBackend, NetworkConfig, NetworkDevice, VirtioConfig}; pub use virtio_net::{Address, Backend, NetworkConfig, NetworkDevice};
pub use virtio_net::{Address, NetworkConfig, NetworkDevice};
pub use virtio_vsock::{ pub use virtio_vsock::{
HybridVsockConfig, HybridVsockDevice, VsockConfig, VsockDevice, DEFAULT_GUEST_VSOCK_CID, HybridVsockConfig, HybridVsockDevice, VsockConfig, VsockDevice, DEFAULT_GUEST_VSOCK_CID,
}; };

View File

@@ -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)] #[derive(Clone, Debug, Default)]
pub struct VirtioConfig { pub enum Backend {
/// Host level path for the guest network interface. #[default]
pub host_dev_name: String, Virtio,
/// Guest iface name for the guest network interface. Vhost,
pub virt_iface_name: String,
/// Allow duplicate mac
pub allow_duplicate_mac: bool,
} }
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
@@ -55,7 +39,11 @@ pub struct NetworkConfig {
pub index: u64, pub index: u64,
/// Network device backend /// 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. /// Guest MAC address.
pub guest_mac: Option<Address>, pub guest_mac: Option<Address>,
/// Virtio queue size /// Virtio queue size
@@ -66,6 +54,8 @@ pub struct NetworkConfig {
pub use_shared_irq: Option<bool>, pub use_shared_irq: Option<bool>,
/// Use generic irq /// Use generic irq
pub use_generic_irq: Option<bool>, pub use_generic_irq: Option<bool>,
/// Allow duplicate mac
pub allow_duplicate_mac: bool,
} }
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]

View File

@@ -18,7 +18,7 @@ use anyhow::{Context, Result};
use async_trait::async_trait; use async_trait::async_trait;
use dbs_utils::net::MacAddr as DragonballMacAddr; use dbs_utils::net::MacAddr as DragonballMacAddr;
use dragonball::api::v1::{ use dragonball::api::v1::{
Backend as DragonballNetworkBackend, NetworkInterfaceConfig as DragonballNetworkConfig, Backend as DragonballBackend, NetworkInterfaceConfig as DragonballNetworkConfig,
VirtioConfig as DragonballVirtioConfig, VirtioConfig as DragonballVirtioConfig,
}; };
use kata_types::capabilities::Capabilities; use kata_types::capabilities::Capabilities;
@@ -26,7 +26,7 @@ use kata_types::config::hypervisor::Hypervisor as HypervisorConfig;
use tokio::sync::RwLock; use tokio::sync::RwLock;
use tracing::instrument; use tracing::instrument;
use crate::{DeviceType, Hypervisor, NetworkBackend, NetworkConfig, VcpuThreadIds}; use crate::{Backend, DeviceType, Hypervisor, NetworkConfig, VcpuThreadIds};
pub struct Dragonball { pub struct Dragonball {
inner: Arc<RwLock<DragonballInner>>, inner: Arc<RwLock<DragonballInner>>,
@@ -196,27 +196,6 @@ impl Persist for Dragonball {
} }
} }
impl From<NetworkBackend> 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<NetworkConfig> for DragonballNetworkConfig { impl From<NetworkConfig> for DragonballNetworkConfig {
fn from(value: NetworkConfig) -> Self { fn from(value: NetworkConfig) -> Self {
let r = &value; let r = &value;
@@ -226,10 +205,24 @@ impl From<NetworkConfig> for DragonballNetworkConfig {
impl From<&NetworkConfig> for DragonballNetworkConfig { impl From<&NetworkConfig> for DragonballNetworkConfig {
fn from(value: &NetworkConfig) -> Self { 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 { Self {
num_queues: Some(value.queue_num), num_queues: Some(value.queue_num),
queue_size: Some(value.queue_size as u16), queue_size: Some(value.queue_size as u16),
backend: value.backend.clone().into(), backend,
guest_mac: value.guest_mac.clone().map(|mac| { guest_mac: value.guest_mac.clone().map(|mac| {
// We are safety since mac address is checked by endpoints. // We are safety since mac address is checked by endpoints.
DragonballMacAddr::from_bytes(&mac.0).unwrap() DragonballMacAddr::from_bytes(&mac.0).unwrap()

View File

@@ -4,28 +4,19 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// //
use std::{ use std::io::{self, Error};
io::{self, Error}, use std::sync::Arc;
sync::Arc,
};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use async_trait::async_trait; 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 tokio::sync::RwLock;
use hypervisor::{ use super::endpoint_persist::{EndpointState, IpVlanEndpointState};
device::{ use super::Endpoint;
device_manager::{do_handle_device, DeviceManager},
driver::NetworkConfig,
DeviceConfig, DeviceType,
},
Hypervisor, NetworkBackend, NetworkDevice, VirtioConfig,
};
use super::{
endpoint_persist::{EndpointState, IpVlanEndpointState},
Endpoint,
};
use crate::network::{network_model::TC_FILTER_NET_MODEL_STR, utils, NetworkPair}; use crate::network::{network_model::TC_FILTER_NET_MODEL_STR, utils, NetworkPair};
// IPVlanEndpoint is the endpoint bridged to VM // IPVlanEndpoint is the endpoint bridged to VM
@@ -64,11 +55,9 @@ impl IPVlanEndpoint {
})?; })?;
Ok(NetworkConfig { Ok(NetworkConfig {
backend: NetworkBackend::Virtio(VirtioConfig { host_dev_name: iface.name.clone(),
host_dev_name: iface.name.clone(), virt_iface_name: self.net_pair.virt_iface.name.clone(),
virt_iface_name: self.net_pair.virt_iface.name.clone(), backend: Backend::Virtio,
..Default::default()
}),
guest_mac: Some(guest_mac), guest_mac: Some(guest_mac),
..Default::default() ..Default::default()
}) })

View File

@@ -4,28 +4,19 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// //
use std::{ use std::io::{self, Error};
io::{self, Error}, use std::sync::Arc;
sync::Arc,
};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use async_trait::async_trait; 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 tokio::sync::RwLock;
use hypervisor::{ use super::endpoint_persist::{EndpointState, MacvlanEndpointState};
device::{ use super::Endpoint;
device_manager::{do_handle_device, DeviceManager},
driver::NetworkConfig,
DeviceConfig, DeviceType,
},
Hypervisor, NetworkBackend, NetworkDevice, VirtioConfig,
};
use super::{
endpoint_persist::{EndpointState, MacvlanEndpointState},
Endpoint,
};
use crate::network::{utils, NetworkPair}; use crate::network::{utils, NetworkPair};
#[derive(Debug)] #[derive(Debug)]
@@ -63,11 +54,9 @@ impl MacVlanEndpoint {
})?; })?;
Ok(NetworkConfig { Ok(NetworkConfig {
backend: NetworkBackend::Virtio(VirtioConfig { host_dev_name: iface.name.clone(),
host_dev_name: iface.name.clone(), virt_iface_name: self.net_pair.virt_iface.name.clone(),
virt_iface_name: self.net_pair.virt_iface.name.clone(), backend: Backend::Virtio,
..Default::default()
}),
guest_mac: Some(guest_mac), guest_mac: Some(guest_mac),
..Default::default() ..Default::default()
}) })

View File

@@ -10,7 +10,7 @@ use anyhow::{Context, Result};
use async_trait::async_trait; use async_trait::async_trait;
use hypervisor::device::device_manager::{do_handle_device, DeviceManager}; use hypervisor::device::device_manager::{do_handle_device, DeviceManager};
use hypervisor::device::{DeviceConfig, DeviceType}; use hypervisor::device::{DeviceConfig, DeviceType};
use hypervisor::{Hypervisor, NetworkBackend, NetworkConfig, NetworkDevice, VirtioConfig}; use hypervisor::{Backend, Hypervisor, NetworkConfig, NetworkDevice};
use tokio::sync::RwLock; use tokio::sync::RwLock;
use super::endpoint_persist::TapEndpointState; use super::endpoint_persist::TapEndpointState;
@@ -74,11 +74,9 @@ impl TapEndpoint {
fn get_network_config(&self) -> Result<NetworkConfig> { fn get_network_config(&self) -> Result<NetworkConfig> {
let guest_mac = utils::parse_mac(&self.guest_mac).context("Parse mac address")?; let guest_mac = utils::parse_mac(&self.guest_mac).context("Parse mac address")?;
Ok(NetworkConfig { Ok(NetworkConfig {
backend: NetworkBackend::Virtio(VirtioConfig { host_dev_name: self.tap_iface.name.clone(),
host_dev_name: self.tap_iface.name.clone(), virt_iface_name: self.name.clone(),
virt_iface_name: self.name.clone(), backend: Backend::Virtio,
..Default::default()
}),
guest_mac: Some(guest_mac), guest_mac: Some(guest_mac),
queue_num: self.queue_num, queue_num: self.queue_num,
queue_size: self.queue_size, queue_size: self.queue_size,

View File

@@ -4,28 +4,19 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// //
use std::{ use std::io::{self, Error};
io::{self, Error}, use std::sync::Arc;
sync::Arc,
};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use async_trait::async_trait; 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 tokio::sync::RwLock;
use hypervisor::{ use super::endpoint_persist::{EndpointState, VethEndpointState};
device::{ use super::Endpoint;
device_manager::{do_handle_device, DeviceManager},
driver::NetworkConfig,
DeviceConfig, DeviceType,
},
Hypervisor, NetworkBackend, NetworkDevice, VirtioConfig,
};
use super::{
endpoint_persist::{EndpointState, VethEndpointState},
Endpoint,
};
use crate::network::{utils, NetworkPair}; use crate::network::{utils, NetworkPair};
#[derive(Debug)] #[derive(Debug)]
@@ -63,11 +54,9 @@ impl VethEndpoint {
})?; })?;
Ok(NetworkConfig { Ok(NetworkConfig {
backend: NetworkBackend::Virtio(VirtioConfig { host_dev_name: iface.name.clone(),
host_dev_name: iface.name.clone(), virt_iface_name: self.net_pair.virt_iface.name.clone(),
virt_iface_name: self.net_pair.virt_iface.name.clone(), backend: Backend::Virtio,
..Default::default()
}),
guest_mac: Some(guest_mac), guest_mac: Some(guest_mac),
..Default::default() ..Default::default()
}) })

View File

@@ -4,29 +4,21 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// //
use std::{ use std::io::{self, Error};
io::{self, Error}, use std::sync::Arc;
sync::Arc,
};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use async_trait::async_trait; 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 tokio::sync::RwLock;
use hypervisor::{ use super::endpoint_persist::{EndpointState, VlanEndpointState};
device::{ use super::Endpoint;
device_manager::{do_handle_device, DeviceManager}, use crate::network::network_model::TC_FILTER_NET_MODEL_STR;
driver::NetworkConfig, use crate::network::{utils, NetworkPair};
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};
#[derive(Debug)] #[derive(Debug)]
pub struct VlanEndpoint { pub struct VlanEndpoint {
@@ -62,11 +54,9 @@ impl VlanEndpoint {
})?; })?;
Ok(NetworkConfig { Ok(NetworkConfig {
backend: NetworkBackend::Virtio(VirtioConfig { host_dev_name: iface.name.clone(),
host_dev_name: iface.name.clone(), virt_iface_name: self.net_pair.virt_iface.name.clone(),
virt_iface_name: self.net_pair.virt_iface.name.clone(), backend: Backend::Virtio,
..Default::default()
}),
guest_mac: Some(guest_mac), guest_mac: Some(guest_mac),
..Default::default() ..Default::default()
}) })