Merge pull request #7219 from Apokleos/network-refactor

runtime-rs: enhancement of Device Manager for network endpoints.
This commit is contained in:
Zhongtao Hu
2023-07-17 14:13:51 +08:00
committed by GitHub
14 changed files with 325 additions and 101 deletions

View File

@@ -11,8 +11,9 @@ use kata_sys_util::rand::RandomBytes;
use tokio::sync::{Mutex, RwLock}; use tokio::sync::{Mutex, RwLock};
use crate::{ use crate::{
device::VhostUserBlkDevice, BlockConfig, BlockDevice, Hypervisor, VfioDevice, VhostUserConfig, device::VhostUserBlkDevice, BlockConfig, BlockDevice, Hypervisor, NetworkDevice, VfioDevice,
KATA_BLK_DEV_TYPE, KATA_MMIO_BLK_DEV_TYPE, VIRTIO_BLOCK_MMIO, VIRTIO_BLOCK_PCI, VhostUserConfig, KATA_BLK_DEV_TYPE, KATA_MMIO_BLK_DEV_TYPE, VIRTIO_BLOCK_MMIO,
VIRTIO_BLOCK_PCI,
}; };
use super::{ use super::{
@@ -193,6 +194,11 @@ impl DeviceManager {
return Some(device_id.to_string()); return Some(device_id.to_string());
} }
} }
DeviceType::Network(device) => {
if device.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;
@@ -267,6 +273,21 @@ impl DeviceManager {
.await .await
.context("failed to create vhost blk device")? .context("failed to create vhost blk device")?
} }
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 {
info!(
sl!(),
"network device with path:{:?} found. return network device id: {:?}",
config.host_dev_name.clone(),
dev_id_matched
);
return Ok(dev_id_matched);
}
Arc::new(Mutex::new(NetworkDevice::new(device_id.clone(), config)))
}
_ => { _ => {
return Err(anyhow!("invliad device type")); return Err(anyhow!("invliad device type"));
} }

View File

@@ -6,6 +6,14 @@
use std::fmt; use std::fmt;
use anyhow::{Context, Result};
use async_trait::async_trait;
use crate::{
device::{Device, DeviceType},
Hypervisor as hypervisor,
};
#[derive(Clone)] #[derive(Clone)]
pub struct Address(pub [u8; 6]); pub struct Address(pub [u8; 6]);
@@ -20,20 +28,71 @@ impl fmt::Debug for Address {
} }
} }
#[derive(Debug, Clone)] #[derive(Clone, Debug, Default)]
pub struct NetworkConfig { pub struct NetworkConfig {
/// for detach, now it's default value 0.
pub index: u64,
/// Host level path for the guest network interface. /// Host level path for the guest network interface.
pub host_dev_name: String, 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>,
} }
#[derive(Debug, Clone)] #[derive(Clone, Debug, Default)]
pub struct NetworkDevice { pub struct NetworkDevice {
/// Unique identifier of the device /// Unique identifier of the device
pub id: String, pub device_id: String,
/// Network Device config info /// Network Device config info
pub config: NetworkConfig, pub config: NetworkConfig,
} }
impl NetworkDevice {
// new creates a NetworkDevice
pub fn new(device_id: String, config: &NetworkConfig) -> Self {
Self {
device_id,
config: config.clone(),
}
}
}
#[async_trait]
impl Device for NetworkDevice {
async fn attach(&mut self, h: &dyn hypervisor) -> Result<()> {
h.add_device(DeviceType::Network(self.clone()))
.await
.context("add network device.")?;
return Ok(());
}
async fn detach(&mut self, h: &dyn hypervisor) -> Result<Option<u64>> {
h.remove_device(DeviceType::Network(self.clone()))
.await
.context("remove network device.")?;
Ok(Some(self.config.index))
}
async fn get_device_info(&self) -> DeviceType {
DeviceType::Network(self.clone())
}
async fn increase_attach_count(&mut self) -> Result<bool> {
// network devices will not be attached multiple times, Just return Ok(false)
Ok(false)
}
async fn decrease_attach_count(&mut self) -> Result<bool> {
// network devices will not be detached multiple times, Just return Ok(false)
Ok(false)
}
}

View File

@@ -48,7 +48,7 @@ impl DragonballInner {
info!(sl!(), "dragonball add device {:?}", &device); info!(sl!(), "dragonball add device {:?}", &device);
match device { match device {
DeviceType::Network(network) => self DeviceType::Network(network) => self
.add_net_device(&network.config, network.id) .add_net_device(&network.config)
.context("add net device"), .context("add net device"),
DeviceType::Vfio(hostdev) => self.add_vfio_device(&hostdev).context("add vfio device"), DeviceType::Vfio(hostdev) => self.add_vfio_device(&hostdev).context("add vfio device"),
DeviceType::Block(block) => self DeviceType::Block(block) => self
@@ -84,6 +84,15 @@ impl DragonballInner {
info!(sl!(), "remove device {} ", device); info!(sl!(), "remove device {} ", device);
match device { match device {
DeviceType::Network(network) => {
// Dragonball doesn't support remove network device, just print message.
info!(
sl!(),
"dragonball remove network device: {:?}.", network.config.virt_iface_name
);
Ok(())
}
DeviceType::Block(block) => { DeviceType::Block(block) => {
let drive_id = drive_index_to_id(block.config.index); let drive_id = drive_index_to_id(block.config.index);
self.remove_block_drive(drive_id.as_str()) self.remove_block_drive(drive_id.as_str())
@@ -197,9 +206,9 @@ impl DragonballInner {
Ok(()) Ok(())
} }
fn add_net_device(&mut self, config: &NetworkConfig, device_id: String) -> Result<()> { fn add_net_device(&mut self, config: &NetworkConfig) -> Result<()> {
let iface_cfg = VirtioNetDeviceConfigInfo { let iface_cfg = VirtioNetDeviceConfigInfo {
iface_id: device_id, iface_id: config.virt_iface_name.clone(),
host_dev_name: config.host_dev_name.clone(), host_dev_name: config.host_dev_name.clone(),
guest_mac: match &config.guest_mac { guest_mac: match &config.guest_mac {
Some(mac) => MacAddr::from_bytes(&mac.0).ok(), Some(mac) => MacAddr::from_bytes(&mac.0).ok(),

View File

@@ -141,15 +141,13 @@ impl ResourceManagerInner {
// but it is not in netns. So, the previous thread would still remain in the pod netns. // but it is not in netns. So, the previous thread would still remain in the pod netns.
// The solution is to block the future on the current thread, it is enabled by spawn an os thread, create a // The solution is to block the future on the current thread, it is enabled by spawn an os thread, create a
// tokio runtime, and block the task on it. // tokio runtime, and block the task on it.
let hypervisor = self.hypervisor.clone();
let device_manager = self.device_manager.clone(); let device_manager = self.device_manager.clone();
let network = thread::spawn(move || -> Result<Arc<dyn Network>> { let network = thread::spawn(move || -> Result<Arc<dyn Network>> {
let rt = runtime::Builder::new_current_thread().enable_io().build()?; let rt = runtime::Builder::new_current_thread().enable_io().build()?;
let d = rt let d = rt
.block_on(network::new(&network_config, device_manager)) .block_on(network::new(&network_config, device_manager))
.context("new network")?; .context("new network")?;
rt.block_on(d.setup(hypervisor.as_ref())) rt.block_on(d.setup()).context("setup network")?;
.context("setup network")?;
Ok(d) Ok(d)
}) })
.join() .join()

View File

@@ -6,11 +6,12 @@
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use anyhow::Context; use std::{fs, path::Path, sync::Arc};
use anyhow::{anyhow, Context, Result};
use netlink_packet_route::MACVLAN_MODE_PRIVATE; use netlink_packet_route::MACVLAN_MODE_PRIVATE;
use scopeguard::defer; use scopeguard::defer;
use tokio::sync::RwLock;
use std::sync::Arc;
use crate::network::{ use crate::network::{
endpoint::{IPVlanEndpoint, MacVlanEndpoint, VlanEndpoint}, endpoint::{IPVlanEndpoint, MacVlanEndpoint, VlanEndpoint},
@@ -22,6 +23,41 @@ mod tests {
network_pair::{NetworkInterface, NetworkPair, TapInterface}, network_pair::{NetworkInterface, NetworkPair, TapInterface},
utils::link::net_test_utils::delete_link, utils::link::net_test_utils::delete_link,
}; };
use hypervisor::{device::device_manager::DeviceManager, qemu::Qemu};
use kata_types::config::{QemuConfig, TomlConfig};
async fn get_device_manager() -> Result<Arc<RwLock<DeviceManager>>> {
let path = env!("CARGO_MANIFEST_DIR");
let path = Path::new(path)
.join("../../../libs/kata-types/tests/texture/configuration-anno-0.toml");
let content = fs::read_to_string(path).context("read configuration failed")?;
// just for test, use x/kata-types/tests/texture/configuration-anno-0.toml as
// the test configuration.toml which is for qemu.
let hypervisor_name: &str = "qemu";
let qemu = QemuConfig::new();
qemu.register();
let toml_config = TomlConfig::load(&content).context("load toml config failed")?;
let hypervisor_config = toml_config
.hypervisor
.get(hypervisor_name)
.ok_or_else(|| anyhow!("failed to get hypervisor for {}", &hypervisor_name))?;
let mut hypervisor = Qemu::new();
hypervisor
.set_hypervisor_config(hypervisor_config.clone())
.await;
let dm = Arc::new(RwLock::new(
DeviceManager::new(Arc::new(hypervisor))
.await
.context("device manager")?,
));
Ok(dm)
}
// this unit test tests the integrity of MacVlanEndpoint::new() // this unit test tests the integrity of MacVlanEndpoint::new()
#[actix_rt::test] #[actix_rt::test]
@@ -33,6 +69,10 @@ mod tests {
let dummy_name = format!("dummy{}", idx); let dummy_name = format!("dummy{}", idx);
let vlanid = 123; let vlanid = 123;
let dm = get_device_manager().await;
assert!(dm.is_ok());
let d = dm.unwrap();
if let Ok((conn, handle, _)) = if let Ok((conn, handle, _)) =
rtnetlink::new_connection().context("failed to create netlink connection") rtnetlink::new_connection().context("failed to create netlink connection")
{ {
@@ -63,11 +103,12 @@ mod tests {
.await .await
.context("failed to create manual veth pair") .context("failed to create manual veth pair")
{ {
if let Ok(mut result) = VlanEndpoint::new(&handle, "", idx, 5) if let Ok(mut result) = VlanEndpoint::new(&d, &handle, "", idx, 5)
.await .await
.context("failed to create new ipvlan endpoint") .context("failed to create new ipvlan endpoint")
{ {
let manual = VlanEndpoint { let manual = VlanEndpoint {
d,
net_pair: NetworkPair { net_pair: NetworkPair {
tap: TapInterface { tap: TapInterface {
id: String::from("uniqueTestID_kata"), id: String::from("uniqueTestID_kata"),
@@ -144,6 +185,9 @@ mod tests {
let tap_iface_name = format!("tap{}_kata", idx); // create by NetworkPair::new() let tap_iface_name = format!("tap{}_kata", idx); // create by NetworkPair::new()
let model_str = TC_FILTER_NET_MODEL_STR; let model_str = TC_FILTER_NET_MODEL_STR;
let dummy_name = format!("dummy{}", idx); let dummy_name = format!("dummy{}", idx);
let dm = get_device_manager().await;
assert!(dm.is_ok());
let d = dm.unwrap();
if let Ok((conn, handle, _)) = if let Ok((conn, handle, _)) =
rtnetlink::new_connection().context("failed to create netlink connection") rtnetlink::new_connection().context("failed to create netlink connection")
@@ -180,6 +224,7 @@ mod tests {
{ {
// model here does not matter, could be any of supported models // model here does not matter, could be any of supported models
if let Ok(mut result) = MacVlanEndpoint::new( if let Ok(mut result) = MacVlanEndpoint::new(
&d,
&handle, &handle,
manual_macvlan_iface_name.clone().as_str(), manual_macvlan_iface_name.clone().as_str(),
idx, idx,
@@ -190,6 +235,7 @@ mod tests {
.context("failed to create new macvlan endpoint") .context("failed to create new macvlan endpoint")
{ {
let manual = MacVlanEndpoint { let manual = MacVlanEndpoint {
d,
net_pair: NetworkPair { net_pair: NetworkPair {
tap: TapInterface { tap: TapInterface {
id: String::from("uniqueTestID_kata"), id: String::from("uniqueTestID_kata"),
@@ -267,6 +313,9 @@ mod tests {
let mac_addr = String::from("02:00:CA:FE:00:04"); let mac_addr = String::from("02:00:CA:FE:00:04");
let manual_virt_iface_name = format!("eth{}", idx); let manual_virt_iface_name = format!("eth{}", idx);
let tap_iface_name = format!("tap{}_kata", idx); // create by kata let tap_iface_name = format!("tap{}_kata", idx); // create by kata
let dm = get_device_manager().await;
assert!(dm.is_ok());
let d = dm.unwrap();
if let Ok((conn, handle, _)) = if let Ok((conn, handle, _)) =
rtnetlink::new_connection().context("failed to create netlink connection") rtnetlink::new_connection().context("failed to create netlink connection")
@@ -286,11 +335,12 @@ mod tests {
.await .await
.context("failed to create manual veth pair") .context("failed to create manual veth pair")
{ {
if let Ok(mut result) = IPVlanEndpoint::new(&handle, "", idx, 5) if let Ok(mut result) = IPVlanEndpoint::new(&d, &handle, "", idx, 5)
.await .await
.context("failed to create new ipvlan endpoint") .context("failed to create new ipvlan endpoint")
{ {
let manual = IPVlanEndpoint { let manual = IPVlanEndpoint {
d,
net_pair: NetworkPair { net_pair: NetworkPair {
tap: TapInterface { tap: TapInterface {
id: String::from("uniqueTestID_kata"), id: String::from("uniqueTestID_kata"),

View File

@@ -4,37 +4,54 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// //
use std::io::{self, Error}; use std::{
io::{self, Error},
sync::Arc,
};
use super::endpoint_persist::{EndpointState, IpVlanEndpointState};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use async_trait::async_trait; use async_trait::async_trait;
use hypervisor::device::DeviceType; use tokio::sync::RwLock;
use hypervisor::NetworkDevice;
use super::Endpoint; use hypervisor::{
use crate::network::network_model::TC_FILTER_NET_MODEL_STR; device::{
use crate::network::{utils, NetworkPair}; device_manager::{do_handle_device, DeviceManager},
use hypervisor::{device::driver::NetworkConfig, Hypervisor}; driver::NetworkConfig,
DeviceConfig, DeviceType,
},
Hypervisor, NetworkDevice,
};
use super::{
endpoint_persist::{EndpointState, IpVlanEndpointState},
Endpoint,
};
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
#[derive(Debug)] #[derive(Debug)]
pub struct IPVlanEndpoint { pub struct IPVlanEndpoint {
pub(crate) net_pair: NetworkPair, pub(crate) net_pair: NetworkPair,
pub(crate) d: Arc<RwLock<DeviceManager>>,
} }
impl IPVlanEndpoint { impl IPVlanEndpoint {
pub async fn new( pub async fn new(
d: &Arc<RwLock<DeviceManager>>,
handle: &rtnetlink::Handle, handle: &rtnetlink::Handle,
name: &str, name: &str,
idx: u32, idx: u32,
queues: usize, queues: usize,
) -> Result<Self> { ) -> Result<Self> {
// tc filter network model is the only one works for ipvlan // tc filter network model is the only for ipvlan
let net_pair = NetworkPair::new(handle, idx, name, TC_FILTER_NET_MODEL_STR, queues) let net_pair = NetworkPair::new(handle, idx, name, TC_FILTER_NET_MODEL_STR, queues)
.await .await
.context("error creating new NetworkPair")?; .context("error creating new NetworkPair")?;
Ok(IPVlanEndpoint { net_pair })
Ok(IPVlanEndpoint {
net_pair,
d: d.clone(),
})
} }
fn get_network_config(&self) -> Result<NetworkConfig> { fn get_network_config(&self) -> Result<NetworkConfig> {
@@ -45,9 +62,12 @@ impl IPVlanEndpoint {
format!("hard_addr {}", &iface.hard_addr), format!("hard_addr {}", &iface.hard_addr),
) )
})?; })?;
Ok(NetworkConfig { Ok(NetworkConfig {
host_dev_name: iface.name.clone(), host_dev_name: iface.name.clone(),
virt_iface_name: self.net_pair.virt_iface.name.clone(),
guest_mac: Some(guest_mac), guest_mac: Some(guest_mac),
..Default::default()
}) })
} }
} }
@@ -62,18 +82,16 @@ impl Endpoint for IPVlanEndpoint {
self.net_pair.tap.tap_iface.hard_addr.clone() self.net_pair.tap.tap_iface.hard_addr.clone()
} }
async fn attach(&self, h: &dyn Hypervisor) -> Result<()> { async fn attach(&self) -> Result<()> {
self.net_pair self.net_pair
.add_network_model() .add_network_model()
.await .await
.context("error adding network model")?; .context("error adding network model")?;
let config = self.get_network_config().context("get network config")?; let config = self.get_network_config().context("get network config")?;
h.add_device(DeviceType::Network(NetworkDevice { do_handle_device(&self.d, &DeviceConfig::NetworkCfg(config))
id: self.net_pair.virt_iface.name.clone(), .await
config, .context("do handle network IPVlan endpoint device failed.")?;
}))
.await
.context("error adding device by hypervisor")?;
Ok(()) Ok(())
} }
@@ -86,12 +104,13 @@ impl Endpoint for IPVlanEndpoint {
let config = self let config = self
.get_network_config() .get_network_config()
.context("error getting network config")?; .context("error getting network config")?;
h.remove_device(DeviceType::Network(NetworkDevice { h.remove_device(DeviceType::Network(NetworkDevice {
id: self.net_pair.virt_iface.name.clone(),
config, config,
..Default::default()
})) }))
.await .await
.context("error removing device by hypervisor")?; .context("remove IPVlan endpoint device by hypervisor failed.")?;
Ok(()) Ok(())
} }

View File

@@ -4,24 +4,39 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// //
use std::io::{self, Error}; use std::{
io::{self, Error},
sync::Arc,
};
use super::endpoint_persist::{EndpointState, MacvlanEndpointState};
use super::Endpoint;
use crate::network::{utils, NetworkPair};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use async_trait::async_trait; use async_trait::async_trait;
use hypervisor::device::DeviceType; use tokio::sync::RwLock;
use hypervisor::NetworkDevice;
use hypervisor::{device::driver::NetworkConfig, Hypervisor}; use hypervisor::{
device::{
device_manager::{do_handle_device, DeviceManager},
driver::NetworkConfig,
DeviceConfig, DeviceType,
},
Hypervisor, NetworkDevice,
};
use super::{
endpoint_persist::{EndpointState, MacvlanEndpointState},
Endpoint,
};
use crate::network::{utils, NetworkPair};
#[derive(Debug)] #[derive(Debug)]
pub struct MacVlanEndpoint { pub struct MacVlanEndpoint {
pub(crate) net_pair: NetworkPair, pub(crate) net_pair: NetworkPair,
pub(crate) d: Arc<RwLock<DeviceManager>>,
} }
impl MacVlanEndpoint { impl MacVlanEndpoint {
pub async fn new( pub async fn new(
d: &Arc<RwLock<DeviceManager>>,
handle: &rtnetlink::Handle, handle: &rtnetlink::Handle,
name: &str, name: &str,
idx: u32, idx: u32,
@@ -31,7 +46,11 @@ impl MacVlanEndpoint {
let net_pair = NetworkPair::new(handle, idx, name, model, queues) let net_pair = NetworkPair::new(handle, idx, name, model, queues)
.await .await
.context("error creating new networkInterfacePair")?; .context("error creating new networkInterfacePair")?;
Ok(MacVlanEndpoint { net_pair })
Ok(MacVlanEndpoint {
net_pair,
d: d.clone(),
})
} }
fn get_network_config(&self) -> Result<NetworkConfig> { fn get_network_config(&self) -> Result<NetworkConfig> {
@@ -42,9 +61,12 @@ impl MacVlanEndpoint {
format!("hard_addr {}", &iface.hard_addr), format!("hard_addr {}", &iface.hard_addr),
) )
})?; })?;
Ok(NetworkConfig { Ok(NetworkConfig {
host_dev_name: iface.name.clone(), host_dev_name: iface.name.clone(),
virt_iface_name: self.net_pair.virt_iface.name.clone(),
guest_mac: Some(guest_mac), guest_mac: Some(guest_mac),
..Default::default()
}) })
} }
} }
@@ -59,18 +81,16 @@ impl Endpoint for MacVlanEndpoint {
self.net_pair.tap.tap_iface.hard_addr.clone() self.net_pair.tap.tap_iface.hard_addr.clone()
} }
async fn attach(&self, h: &dyn Hypervisor) -> Result<()> { async fn attach(&self) -> Result<()> {
self.net_pair self.net_pair
.add_network_model() .add_network_model()
.await .await
.context("add network model")?; .context("add network model")?;
let config = self.get_network_config().context("get network config")?; let config = self.get_network_config().context("get network config")?;
h.add_device(DeviceType::Network(NetworkDevice { do_handle_device(&self.d, &DeviceConfig::NetworkCfg(config))
id: self.net_pair.virt_iface.name.clone(), .await
config, .context("do handle network MacVlan endpoint device failed.")?;
}))
.await
.context("error adding device by hypervisor")?;
Ok(()) Ok(())
} }
@@ -80,13 +100,14 @@ impl Endpoint for MacVlanEndpoint {
.del_network_model() .del_network_model()
.await .await
.context("del network model")?; .context("del network model")?;
let config = self.get_network_config().context("get network config")?; let config = self.get_network_config().context("get network config")?;
h.remove_device(DeviceType::Network(NetworkDevice { h.remove_device(DeviceType::Network(NetworkDevice {
id: self.net_pair.virt_iface.name.clone(),
config, config,
..Default::default()
})) }))
.await .await
.context("error removing device by hypervisor")?; .context("remove MacVlan endpoint device by hypervisor failed.")?;
Ok(()) Ok(())
} }

View File

@@ -27,7 +27,7 @@ use super::EndpointState;
pub trait Endpoint: std::fmt::Debug + Send + Sync { pub trait Endpoint: std::fmt::Debug + Send + Sync {
async fn name(&self) -> String; async fn name(&self) -> String;
async fn hardware_addr(&self) -> String; async fn hardware_addr(&self) -> String;
async fn attach(&self, hypervisor: &dyn Hypervisor) -> Result<()>; async fn attach(&self) -> Result<()>;
async fn detach(&self, hypervisor: &dyn Hypervisor) -> Result<()>; async fn detach(&self, hypervisor: &dyn Hypervisor) -> Result<()>;
async fn save(&self) -> Option<EndpointState>; async fn save(&self) -> Option<EndpointState>;
} }

View File

@@ -99,7 +99,7 @@ impl Endpoint for PhysicalEndpoint {
self.hard_addr.clone() self.hard_addr.clone()
} }
async fn attach(&self, _hypervisor: &dyn Hypervisor) -> Result<()> { async fn attach(&self) -> Result<()> {
// bind physical interface from host driver and bind to vfio // bind physical interface from host driver and bind to vfio
driver::bind_device_to_vfio( driver::bind_device_to_vfio(
&self.bdf, &self.bdf,

View File

@@ -4,24 +4,39 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// //
use std::io::{self, Error}; use std::{
io::{self, Error},
sync::Arc,
};
use super::endpoint_persist::{EndpointState, VethEndpointState};
use super::Endpoint;
use crate::network::{utils, NetworkPair};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use async_trait::async_trait; use async_trait::async_trait;
use hypervisor::device::DeviceType; use tokio::sync::RwLock;
use hypervisor::NetworkDevice;
use hypervisor::{device::driver::NetworkConfig, Hypervisor}; use hypervisor::{
device::{
device_manager::{do_handle_device, DeviceManager},
driver::NetworkConfig,
DeviceConfig, DeviceType,
},
Hypervisor, NetworkDevice,
};
use super::{
endpoint_persist::{EndpointState, VethEndpointState},
Endpoint,
};
use crate::network::{utils, NetworkPair};
#[derive(Debug)] #[derive(Debug)]
pub struct VethEndpoint { pub struct VethEndpoint {
net_pair: NetworkPair, pub(crate) net_pair: NetworkPair,
pub(crate) d: Arc<RwLock<DeviceManager>>,
} }
impl VethEndpoint { impl VethEndpoint {
pub async fn new( pub async fn new(
d: &Arc<RwLock<DeviceManager>>,
handle: &rtnetlink::Handle, handle: &rtnetlink::Handle,
name: &str, name: &str,
idx: u32, idx: u32,
@@ -30,8 +45,12 @@ impl VethEndpoint {
) -> Result<Self> { ) -> Result<Self> {
let net_pair = NetworkPair::new(handle, idx, name, model, queues) let net_pair = NetworkPair::new(handle, idx, name, model, queues)
.await .await
.context("new networkInterfacePair")?; .context("new network interface pair failed.")?;
Ok(VethEndpoint { net_pair })
Ok(VethEndpoint {
net_pair,
d: d.clone(),
})
} }
fn get_network_config(&self) -> Result<NetworkConfig> { fn get_network_config(&self) -> Result<NetworkConfig> {
@@ -42,9 +61,12 @@ impl VethEndpoint {
format!("hard_addr {}", &iface.hard_addr), format!("hard_addr {}", &iface.hard_addr),
) )
})?; })?;
Ok(NetworkConfig { Ok(NetworkConfig {
host_dev_name: iface.name.clone(), host_dev_name: iface.name.clone(),
virt_iface_name: self.net_pair.virt_iface.name.clone(),
guest_mac: Some(guest_mac), guest_mac: Some(guest_mac),
..Default::default()
}) })
} }
} }
@@ -59,18 +81,17 @@ impl Endpoint for VethEndpoint {
self.net_pair.tap.tap_iface.hard_addr.clone() self.net_pair.tap.tap_iface.hard_addr.clone()
} }
async fn attach(&self, h: &dyn Hypervisor) -> Result<()> { async fn attach(&self) -> Result<()> {
self.net_pair self.net_pair
.add_network_model() .add_network_model()
.await .await
.context("add network model")?; .context("add network model")?;
let config = self.get_network_config().context("get network config")?; let config = self.get_network_config().context("get network config")?;
h.add_device(DeviceType::Network(NetworkDevice { do_handle_device(&self.d, &DeviceConfig::NetworkCfg(config))
id: self.net_pair.virt_iface.name.clone(), .await
config, .context("do handle network Veth endpoint device failed.")?;
}))
.await
.context("error adding device by hypervisor")?;
Ok(()) Ok(())
} }
@@ -78,16 +99,19 @@ impl Endpoint for VethEndpoint {
self.net_pair self.net_pair
.del_network_model() .del_network_model()
.await .await
.context("del network model")?; .context("del network model failed.")?;
let config = self.get_network_config().context("get network config")?; let config = self.get_network_config().context("get network config")?;
h.remove_device(DeviceType::Network(NetworkDevice { h.remove_device(DeviceType::Network(NetworkDevice {
id: self.net_pair.virt_iface.name.clone(),
config, config,
..Default::default()
})) }))
.await .await
.context("error removing device by hypervisor")?; .context("remove Veth endpoint device by hypervisor failed.")?;
Ok(()) Ok(())
} }
async fn save(&self) -> Option<EndpointState> { async fn save(&self) -> Option<EndpointState> {
Some(EndpointState { Some(EndpointState {
veth_endpoint: Some(VethEndpointState { veth_endpoint: Some(VethEndpointState {

View File

@@ -4,25 +4,39 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// //
use std::io::{self, Error}; use std::{
io::{self, Error},
sync::Arc,
};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use async_trait::async_trait; use async_trait::async_trait;
use hypervisor::device::DeviceType; use tokio::sync::RwLock;
use hypervisor::NetworkDevice;
use hypervisor::{
device::{
device_manager::{do_handle_device, DeviceManager},
driver::NetworkConfig,
DeviceConfig, DeviceType,
},
Hypervisor, NetworkDevice,
};
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};
use hypervisor::{device::driver::NetworkConfig, Hypervisor};
#[derive(Debug)] #[derive(Debug)]
pub struct VlanEndpoint { pub struct VlanEndpoint {
pub(crate) net_pair: NetworkPair, pub(crate) net_pair: NetworkPair,
pub(crate) d: Arc<RwLock<DeviceManager>>,
} }
impl VlanEndpoint { impl VlanEndpoint {
pub async fn new( pub async fn new(
d: &Arc<RwLock<DeviceManager>>,
handle: &rtnetlink::Handle, handle: &rtnetlink::Handle,
name: &str, name: &str,
idx: u32, idx: u32,
@@ -30,8 +44,12 @@ impl VlanEndpoint {
) -> Result<Self> { ) -> Result<Self> {
let net_pair = NetworkPair::new(handle, idx, name, TC_FILTER_NET_MODEL_STR, queues) let net_pair = NetworkPair::new(handle, idx, name, TC_FILTER_NET_MODEL_STR, queues)
.await .await
.context("error creating networkInterfacePair")?; .context("new network interface pair failed.")?;
Ok(VlanEndpoint { net_pair })
Ok(VlanEndpoint {
net_pair,
d: d.clone(),
})
} }
fn get_network_config(&self) -> Result<NetworkConfig> { fn get_network_config(&self) -> Result<NetworkConfig> {
@@ -42,9 +60,12 @@ impl VlanEndpoint {
format!("hard_addr {}", &iface.hard_addr), format!("hard_addr {}", &iface.hard_addr),
) )
})?; })?;
Ok(NetworkConfig { Ok(NetworkConfig {
host_dev_name: iface.name.clone(), host_dev_name: iface.name.clone(),
virt_iface_name: self.net_pair.virt_iface.name.clone(),
guest_mac: Some(guest_mac), guest_mac: Some(guest_mac),
..Default::default()
}) })
} }
} }
@@ -59,18 +80,16 @@ impl Endpoint for VlanEndpoint {
self.net_pair.tap.tap_iface.hard_addr.clone() self.net_pair.tap.tap_iface.hard_addr.clone()
} }
async fn attach(&self, h: &dyn Hypervisor) -> Result<()> { async fn attach(&self) -> Result<()> {
self.net_pair self.net_pair
.add_network_model() .add_network_model()
.await .await
.context("error adding network model")?; .context("add network model failed.")?;
let config = self.get_network_config().context("get network config")?; let config = self.get_network_config().context("get network config")?;
h.add_device(DeviceType::Network(NetworkDevice { do_handle_device(&self.d, &DeviceConfig::NetworkCfg(config))
id: self.net_pair.virt_iface.name.clone(), .await
config, .context("do handle network Vlan endpoint device failed.")?;
}))
.await
.context("error adding device by hypervisor")?;
Ok(()) Ok(())
} }
@@ -79,16 +98,17 @@ impl Endpoint for VlanEndpoint {
self.net_pair self.net_pair
.del_network_model() .del_network_model()
.await .await
.context("error deleting network model")?; .context("delete network model failed.")?;
let config = self let config = self
.get_network_config() .get_network_config()
.context("error getting network config")?; .context("get network config failed.")?;
h.remove_device(DeviceType::Network(NetworkDevice { h.remove_device(DeviceType::Network(NetworkDevice {
id: self.net_pair.virt_iface.name.clone(),
config, config,
..Default::default()
})) }))
.await .await
.context("error removing device by hypervisor")?; .context("remove Vlan endpoint device by hypervisor failed.")?;
Ok(()) Ok(())
} }

View File

@@ -35,7 +35,7 @@ pub enum NetworkConfig {
#[async_trait] #[async_trait]
pub trait Network: Send + Sync { pub trait Network: Send + Sync {
async fn setup(&self, h: &dyn Hypervisor) -> Result<()>; async fn setup(&self) -> Result<()>;
async fn interfaces(&self) -> Result<Vec<agent::Interface>>; async fn interfaces(&self) -> Result<Vec<agent::Interface>>;
async fn routes(&self) -> Result<Vec<agent::Route>>; async fn routes(&self) -> Result<Vec<agent::Route>>;
async fn neighs(&self) -> Result<Vec<agent::ARPNeighbor>>; async fn neighs(&self) -> Result<Vec<agent::ARPNeighbor>>;

View File

@@ -39,6 +39,7 @@ pub struct NetworkPair {
pub model: Arc<dyn network_model::NetworkModel>, pub model: Arc<dyn network_model::NetworkModel>,
pub network_qos: bool, pub network_qos: bool,
} }
impl NetworkPair { impl NetworkPair {
pub(crate) async fn new( pub(crate) async fn new(
handle: &rtnetlink::Handle, handle: &rtnetlink::Handle,

View File

@@ -88,11 +88,11 @@ impl NetworkWithNetns {
#[async_trait] #[async_trait]
impl Network for NetworkWithNetns { impl Network for NetworkWithNetns {
async fn setup(&self, h: &dyn Hypervisor) -> Result<()> { async fn setup(&self) -> Result<()> {
let inner = self.inner.read().await; let inner = self.inner.read().await;
let _netns_guard = netns::NetnsGuard::new(&inner.netns_path).context("net netns guard")?; let _netns_guard = netns::NetnsGuard::new(&inner.netns_path).context("net netns guard")?;
for e in &inner.entity_list { for e in &inner.entity_list {
e.endpoint.attach(h).await.context("attach")?; e.endpoint.attach().await.context("attach")?;
} }
Ok(()) Ok(())
} }
@@ -241,6 +241,7 @@ async fn create_endpoint(
match link_type { match link_type {
"veth" => { "veth" => {
let ret = VethEndpoint::new( let ret = VethEndpoint::new(
&d,
handle, handle,
&attrs.name, &attrs.name,
idx, idx,
@@ -252,19 +253,20 @@ async fn create_endpoint(
Arc::new(ret) Arc::new(ret)
} }
"vlan" => { "vlan" => {
let ret = VlanEndpoint::new(handle, &attrs.name, idx, config.queues) let ret = VlanEndpoint::new(&d, handle, &attrs.name, idx, config.queues)
.await .await
.context("vlan endpoint")?; .context("vlan endpoint")?;
Arc::new(ret) Arc::new(ret)
} }
"ipvlan" => { "ipvlan" => {
let ret = IPVlanEndpoint::new(handle, &attrs.name, idx, config.queues) let ret = IPVlanEndpoint::new(&d, handle, &attrs.name, idx, config.queues)
.await .await
.context("ipvlan endpoint")?; .context("ipvlan endpoint")?;
Arc::new(ret) Arc::new(ret)
} }
"macvlan" => { "macvlan" => {
let ret = MacVlanEndpoint::new( let ret = MacVlanEndpoint::new(
&d,
handle, handle,
&attrs.name, &attrs.name,
idx, idx,