From b83bd3849fdd32db6dc2f3b094d845b1a9962540 Mon Sep 17 00:00:00 2001 From: MorningTZH Date: Fri, 9 Aug 2024 11:55:50 +0800 Subject: [PATCH] vmm: add IPv6 support for pod networking Signed-off-by: MorningTZH --- vmm/sandbox/src/network/convert.rs | 14 +++++------ vmm/sandbox/src/network/mod.rs | 39 +++++++++++++++++++----------- vmm/sandbox/src/network/route.rs | 3 +++ 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/vmm/sandbox/src/network/convert.rs b/vmm/sandbox/src/network/convert.rs index 4035aac7..56bd120d 100644 --- a/vmm/sandbox/src/network/convert.rs +++ b/vmm/sandbox/src/network/convert.rs @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ +use netlink_packet_route::{AF_INET, AF_INET6}; use protobuf::{EnumOrUnknown, SpecialFields}; use vmm_common::api::sandbox::{IPAddress, IPFamily, Interface, Route}; @@ -24,12 +25,7 @@ impl From<&NetworkInterface> for Interface { Self { device: interface.name.to_string(), name: interface.name.to_string(), - IPAddresses: interface - .ip_addresses - .iter() - .filter(|x| x.ip.is_ipv4()) - .map(|i| i.into()) - .collect(), + IPAddresses: interface.ip_addresses.iter().map(|i| i.into()).collect(), mtu: interface.mtu as u64, hwAddr: interface.mac_address.to_string(), raw_flags: interface.flags, @@ -62,7 +58,11 @@ impl From<&crate::network::Route> for Route { device: r.device.to_string(), source: r.source.to_string(), scope: r.scope, - family: Default::default(), + family: EnumOrUnknown::from(match r.family { + AF_INET => IPFamily::v4, + AF_INET6 => IPFamily::v6, + _ => IPFamily::default(), + }), special_fields: Default::default(), } } diff --git a/vmm/sandbox/src/network/mod.rs b/vmm/sandbox/src/network/mod.rs index 721f88ee..9e2d212b 100644 --- a/vmm/sandbox/src/network/mod.rs +++ b/vmm/sandbox/src/network/mod.rs @@ -49,6 +49,29 @@ pub struct Network { routes: Vec, } +async fn get_route( + ip_version: IpVersion, + handle: &Handle, + intfs: &[NetworkInterface], + routes: &mut Vec, +) -> Result<()> { + let mut route_msgs = handle.route().get(ip_version).execute(); + while let Some(route_msg) = route_msgs.try_next().await.map_err(|e| anyhow!("{}", e))? { + let route_res = Route::parse_from_message(route_msg, intfs); + match route_res { + Ok(r) => { + routes.push(r); + } + Err(e) => { + // ignore those routes that can not be parsed + debug!("can not parse the route message to route {}", e); + } + } + } + + Ok(()) +} + impl Network { pub async fn new(config: NetworkConfig) -> Result { debug!("create network with config: {:?}", config); @@ -91,21 +114,9 @@ impl Network { let intfs = Self::filter_intfs(intfs); // get all routes from netns - // TODO ipv6 routes not supported yet - let mut route_msgs = handle.route().get(IpVersion::V4).execute(); let mut routes = vec![]; - while let Some(route_msg) = route_msgs.try_next().await.map_err(|e| anyhow!("{}", e))? { - let route_res = Route::parse_from_message(route_msg, &intfs); - match route_res { - Ok(r) => { - routes.push(r); - } - Err(e) => { - // ignore those routes that can not be parsed - debug!("can not parse the route message to route {}", e); - } - } - } + get_route(IpVersion::V4, &handle, &intfs, &mut routes).await?; + get_route(IpVersion::V6, &handle, &intfs, &mut routes).await?; Ok(Network { config, diff --git a/vmm/sandbox/src/network/route.rs b/vmm/sandbox/src/network/route.rs index 01747267..791ae272 100644 --- a/vmm/sandbox/src/network/route.rs +++ b/vmm/sandbox/src/network/route.rs @@ -32,6 +32,8 @@ pub struct Route { pub gateway: String, #[serde(default)] pub scope: u32, + #[serde(default)] + pub family: u16, } impl Route { @@ -41,6 +43,7 @@ impl Route { } let mut route = Route { scope: msg.header.scope as u32, + family: msg.header.address_family as u16, ..Route::default() }; use netlink_packet_route::nlas::route::Nla;