Merge pull request #7675 from justxuewei/vhost-net

This commit is contained in:
Xuewei Niu
2023-11-12 20:38:18 +08:00
committed by GitHub
30 changed files with 2241 additions and 143 deletions

View File

@@ -36,7 +36,7 @@ kata-types = { path = "../../../libs/kata-types" }
logging = { path = "../../../libs/logging" }
shim-interface = { path = "../../../libs/shim-interface" }
dragonball = { path = "../../../dragonball", features = ["atomic-guest-memory", "virtio-vsock", "hotplug", "virtio-blk", "virtio-net", "virtio-fs", "dbs-upcall"] }
dragonball = { path = "../../../dragonball", features = ["atomic-guest-memory", "virtio-vsock", "hotplug", "virtio-blk", "virtio-net", "virtio-fs", "vhost-net", "dbs-upcall"] }
ch-config = { path = "ch-config", optional = true }
tests_utils = { path = "../../tests/utils" }

View File

@@ -413,7 +413,7 @@ impl TryFrom<ShareFsSettings> for FsConfig {
#[cfg(test)]
mod tests {
use super::*;
use crate::Address;
use crate::{Address, Backend};
#[test]
fn test_networkconfig_to_netconfig() {
@@ -424,6 +424,10 @@ mod tests {
queue_num: 2,
guest_mac: None,
index: 1,
allow_duplicate_mac: false,
use_generic_irq: None,
use_shared_irq: None,
backend: Backend::default(),
};
let net = NetConfig::try_from(cfg.clone());

View File

@@ -307,11 +307,12 @@ impl DeviceManager {
}
DeviceConfig::NetworkCfg(config) => {
// try to find the device, found and just return id.
if let Some(dev_id_matched) = self.find_device(config.host_dev_name.clone()).await {
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!(),
"network device with path:{:?} found. return network device id: {:?}",
config.host_dev_name.clone(),
host_path,
dev_id_matched
);

View File

@@ -23,7 +23,7 @@ pub use virtio_fs::{
ShareFsDevice, ShareFsDeviceConfig, ShareFsMountConfig, ShareFsMountDevice, ShareFsMountType,
ShareFsOperation,
};
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,
};

View File

@@ -9,10 +9,8 @@ use std::fmt;
use anyhow::{Context, Result};
use async_trait::async_trait;
use crate::{
device::{Device, DeviceType},
Hypervisor as hypervisor,
};
use crate::device::{Device, DeviceType};
use crate::Hypervisor as hypervisor;
#[derive(Clone)]
pub struct Address(pub [u8; 6]);
@@ -28,25 +26,36 @@ 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.
pub virt_iface_name: String,
/// Guest MAC address.
pub guest_mac: Option<Address>,
/// Virtio queue size
pub queue_size: usize,
/// Virtio queue num
pub queue_num: usize,
/// Use shared irq
pub use_shared_irq: Option<bool>,
/// Use generic irq
pub use_generic_irq: Option<bool>,
/// Allow duplicate mac
pub allow_duplicate_mac: bool,
}
#[derive(Clone, Debug, Default)]

View File

@@ -7,14 +7,10 @@
use std::path::PathBuf;
use anyhow::{anyhow, Context, Result};
use dbs_utils::net::MacAddr;
use dragonball::{
api::v1::{
BlockDeviceConfigInfo, FsDeviceConfigInfo, FsMountConfigInfo, VirtioNetDeviceConfigInfo,
VsockDeviceConfigInfo,
},
device_manager::blk_dev_mgr::BlockDeviceType,
use dragonball::api::v1::{
BlockDeviceConfigInfo, FsDeviceConfigInfo, FsMountConfigInfo, VsockDeviceConfigInfo,
};
use dragonball::device_manager::blk_dev_mgr::BlockDeviceType;
use super::DragonballInner;
use crate::{
@@ -85,7 +81,7 @@ impl DragonballInner {
// Dragonball doesn't support remove network device, just print message.
info!(
sl!(),
"dragonball remove network device: {:?}.", network.config.virt_iface_name
"dragonball remove network device: {:?}.", network.config
);
Ok(())
@@ -204,25 +200,8 @@ impl DragonballInner {
}
fn add_net_device(&mut self, config: &NetworkConfig) -> Result<()> {
let iface_cfg = VirtioNetDeviceConfigInfo {
iface_id: config.virt_iface_name.clone(),
host_dev_name: config.host_dev_name.clone(),
guest_mac: match &config.guest_mac {
Some(mac) => MacAddr::from_bytes(&mac.0).ok(),
None => None,
},
num_queues: config.queue_num,
queue_size: config.queue_size as u16,
..Default::default()
};
info!(
sl!(),
"add {} endpoint to {}", iface_cfg.host_dev_name, iface_cfg.iface_id
);
self.vmm_instance
.insert_network_device(iface_cfg)
.insert_network_device(config.into())
.context("insert network device")
}

View File

@@ -16,12 +16,17 @@ use std::sync::Arc;
use anyhow::{Context, Result};
use async_trait::async_trait;
use dbs_utils::net::MacAddr as DragonballMacAddr;
use dragonball::api::v1::{
Backend as DragonballBackend, NetworkInterfaceConfig as DragonballNetworkConfig,
VirtioConfig as DragonballVirtioConfig,
};
use kata_types::capabilities::Capabilities;
use kata_types::config::hypervisor::Hypervisor as HypervisorConfig;
use tokio::sync::RwLock;
use tracing::instrument;
use crate::{DeviceType, Hypervisor, VcpuThreadIds};
use crate::{Backend, DeviceType, Hypervisor, NetworkConfig, VcpuThreadIds};
pub struct Dragonball {
inner: Arc<RwLock<DragonballInner>>,
@@ -190,3 +195,42 @@ impl Persist for Dragonball {
})
}
}
impl From<NetworkConfig> 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,
}
}
}

View File

@@ -16,7 +16,7 @@ use crossbeam_channel::{unbounded, Receiver, Sender};
use dragonball::{
api::v1::{
BlockDeviceConfigInfo, BootSourceConfig, FsDeviceConfigInfo, FsMountConfigInfo,
InstanceInfo, InstanceState, VcpuResizeInfo, VirtioNetDeviceConfigInfo, VmmAction,
InstanceInfo, InstanceState, NetworkInterfaceConfig, VcpuResizeInfo, VmmAction,
VmmActionError, VmmData, VmmRequest, VmmResponse, VmmService, VsockDeviceConfigInfo,
},
vm::VmConfigInfo,
@@ -216,7 +216,7 @@ impl VmmInstance {
Ok(())
}
pub fn insert_network_device(&self, net_cfg: VirtioNetDeviceConfigInfo) -> Result<()> {
pub fn insert_network_device(&self, net_cfg: NetworkInterfaceConfig) -> Result<()> {
self.handle_request_with_retry(Request::Sync(VmmAction::InsertNetworkDevice(
net_cfg.clone(),
)))