mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-28 00:07:16 +00:00
runtime-rs: support functionalities of ipvlan endpoint
Add support for ipvlan endpoint Fixes: #4655 Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com>
This commit is contained in:
parent
9f49f7adca
commit
d8920b00cd
@ -0,0 +1,90 @@
|
|||||||
|
// Copyright (c) 2019-2022 Alibaba Cloud
|
||||||
|
// Copyright (c) 2019-2022 Ant Group
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
//
|
||||||
|
|
||||||
|
use std::io::{self, Error};
|
||||||
|
|
||||||
|
use anyhow::{Context, Result};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
|
||||||
|
use super::Endpoint;
|
||||||
|
use crate::network::network_model::TC_FILTER_NET_MODEL_STR;
|
||||||
|
use crate::network::{utils, NetworkPair};
|
||||||
|
use hypervisor::{device::NetworkConfig, Device, Hypervisor};
|
||||||
|
|
||||||
|
// IPVlanEndpoint is the endpoint bridged to VM
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct IPVlanEndpoint {
|
||||||
|
pub(crate) net_pair: NetworkPair,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IPVlanEndpoint {
|
||||||
|
pub async fn new(
|
||||||
|
handle: &rtnetlink::Handle,
|
||||||
|
name: &str,
|
||||||
|
idx: u32,
|
||||||
|
queues: usize,
|
||||||
|
) -> Result<Self> {
|
||||||
|
// tc filter network model is the only one works for ipvlan
|
||||||
|
let net_pair = NetworkPair::new(handle, idx, name, TC_FILTER_NET_MODEL_STR, queues)
|
||||||
|
.await
|
||||||
|
.context("error creating new NetworkPair")?;
|
||||||
|
Ok(IPVlanEndpoint { net_pair })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_network_config(&self) -> Result<NetworkConfig> {
|
||||||
|
let iface = &self.net_pair.tap.tap_iface;
|
||||||
|
let guest_mac = utils::parse_mac(&iface.hard_addr).ok_or_else(|| {
|
||||||
|
Error::new(
|
||||||
|
io::ErrorKind::InvalidData,
|
||||||
|
format!("hard_addr {}", &iface.hard_addr),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
Ok(NetworkConfig {
|
||||||
|
id: self.net_pair.virt_iface.name.clone(),
|
||||||
|
host_dev_name: iface.name.clone(),
|
||||||
|
guest_mac: Some(guest_mac),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Endpoint for IPVlanEndpoint {
|
||||||
|
async fn name(&self) -> String {
|
||||||
|
self.net_pair.virt_iface.name.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn hardware_addr(&self) -> String {
|
||||||
|
self.net_pair.tap.tap_iface.hard_addr.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn attach(&self, h: &dyn Hypervisor) -> Result<()> {
|
||||||
|
self.net_pair
|
||||||
|
.add_network_model()
|
||||||
|
.await
|
||||||
|
.context("error adding network model")?;
|
||||||
|
let config = self.get_network_config().context("get network config")?;
|
||||||
|
h.add_device(Device::Network(config))
|
||||||
|
.await
|
||||||
|
.context("error adding device by hypervisor")?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn detach(&self, h: &dyn Hypervisor) -> Result<()> {
|
||||||
|
self.net_pair
|
||||||
|
.del_network_model()
|
||||||
|
.await
|
||||||
|
.context("error deleting network model")?;
|
||||||
|
let config = self
|
||||||
|
.get_network_config()
|
||||||
|
.context("error getting network config")?;
|
||||||
|
h.remove_device(Device::Network(config))
|
||||||
|
.await
|
||||||
|
.context("error removing device by hypervisor")?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
@ -8,6 +8,8 @@ mod physical_endpoint;
|
|||||||
pub use physical_endpoint::PhysicalEndpoint;
|
pub use physical_endpoint::PhysicalEndpoint;
|
||||||
mod veth_endpoint;
|
mod veth_endpoint;
|
||||||
pub use veth_endpoint::VethEndpoint;
|
pub use veth_endpoint::VethEndpoint;
|
||||||
|
mod ipvlan_endpoint;
|
||||||
|
pub use ipvlan_endpoint::IPVlanEndpoint;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
@ -15,8 +15,8 @@ use async_trait::async_trait;
|
|||||||
|
|
||||||
use super::NetworkPair;
|
use super::NetworkPair;
|
||||||
|
|
||||||
const TC_FILTER_NET_MODEL_STR: &str = "tcfilter";
|
pub(crate) const TC_FILTER_NET_MODEL_STR: &str = "tcfilter";
|
||||||
const ROUTE_NET_MODEL_STR: &str = "route";
|
pub(crate) const ROUTE_NET_MODEL_STR: &str = "route";
|
||||||
|
|
||||||
pub enum NetworkModelType {
|
pub enum NetworkModelType {
|
||||||
NoneModel,
|
NoneModel,
|
||||||
|
@ -54,6 +54,7 @@ impl NetworkPair {
|
|||||||
let tap_link = create_link(handle, &tap_iface_name, queues)
|
let tap_link = create_link(handle, &tap_iface_name, queues)
|
||||||
.await
|
.await
|
||||||
.context("create link")?;
|
.context("create link")?;
|
||||||
|
|
||||||
let virt_link = get_link_by_name(handle, virt_iface_name.clone().as_str())
|
let virt_link = get_link_by_name(handle, virt_iface_name.clone().as_str())
|
||||||
.await
|
.await
|
||||||
.context("get link by name")?;
|
.context("get link by name")?;
|
||||||
|
@ -17,7 +17,7 @@ use scopeguard::defer;
|
|||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
endpoint::{Endpoint, PhysicalEndpoint, VethEndpoint},
|
endpoint::{Endpoint, IPVlanEndpoint, PhysicalEndpoint, VethEndpoint},
|
||||||
network_entity::NetworkEntity,
|
network_entity::NetworkEntity,
|
||||||
network_info::network_info_from_link::NetworkInfoFromLink,
|
network_info::network_info_from_link::NetworkInfoFromLink,
|
||||||
utils::{link, netns},
|
utils::{link, netns},
|
||||||
@ -186,6 +186,12 @@ async fn create_endpoint(
|
|||||||
.context("veth endpoint")?;
|
.context("veth endpoint")?;
|
||||||
Arc::new(ret)
|
Arc::new(ret)
|
||||||
}
|
}
|
||||||
|
"ipvlan" => {
|
||||||
|
let ret = IPVlanEndpoint::new(handle, &attrs.name, idx, config.queues)
|
||||||
|
.await
|
||||||
|
.context("ipvlan endpoint")?;
|
||||||
|
Arc::new(ret)
|
||||||
|
}
|
||||||
_ => return Err(anyhow!("unsupported link type: {}", link_type)),
|
_ => return Err(anyhow!("unsupported link type: {}", link_type)),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user