runtime-rs: Move Dragonball stuff out of device drivers

Moving Dragonball structs convertions out of device drivers to keep driver
neutral. The convertions include `NetworkBackend` to
`DragonballNetworkBackend` and `NetworkConfig` to
`DragonballNetworkConfig`.

Signed-off-by: Xuewei Niu <niuxuewei.nxw@antgroup.com>
This commit is contained in:
Xuewei Niu 2023-09-06 09:56:13 +08:00
parent 3e0614cdf0
commit ad66378bf5
2 changed files with 52 additions and 54 deletions

View File

@ -8,16 +8,9 @@ use std::fmt;
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,
VirtioConfig as DragonballVirtioConfig,
};
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]);
@ -45,27 +38,6 @@ impl Default for NetworkBackend {
}
}
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,
}),
}
}
}
/// Virtio network backend config
#[derive(Clone, Debug, Default)]
pub struct VirtioConfig {
@ -96,29 +68,6 @@ pub struct NetworkConfig {
pub use_generic_irq: Option<bool>,
}
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 {
Self {
num_queues: Some(value.queue_num),
queue_size: Some(value.queue_size as u16),
backend: value.backend.clone().into(),
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,
}
}
}
#[derive(Clone, Debug, Default)]
pub struct NetworkDevice {
/// Unique identifier of the 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 DragonballNetworkBackend, 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::{DeviceType, Hypervisor, NetworkBackend, NetworkConfig, VcpuThreadIds};
pub struct Dragonball {
inner: Arc<RwLock<DragonballInner>>,
@ -190,3 +195,47 @@ 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 {
fn from(value: NetworkConfig) -> Self {
let r = &value;
r.into()
}
}
impl From<&NetworkConfig> for DragonballNetworkConfig {
fn from(value: &NetworkConfig) -> Self {
Self {
num_queues: Some(value.queue_num),
queue_size: Some(value.queue_size as u16),
backend: value.backend.clone().into(),
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,
}
}
}