From 7f5b84eb06fd1b1504088b2f748426486c0d8f21 Mon Sep 17 00:00:00 2001 From: un-lock-able Date: Fri, 13 Sep 2024 19:01:10 +0800 Subject: [PATCH 1/2] fix(env): create route table with scope --- rattan-core/src/env.rs | 16 +++++++- rattan-core/src/metal/route.rs | 68 ++++++++++++++++++++-------------- 2 files changed, 56 insertions(+), 28 deletions(-) diff --git a/rattan-core/src/env.rs b/rattan-core/src/env.rs index 48fe59c3..30bf0891 100644 --- a/rattan-core/src/env.rs +++ b/rattan-core/src/env.rs @@ -7,7 +7,7 @@ use crate::{ }, }; use futures::TryStreamExt; -use netlink_packet_route::{address::AddressAttribute, link::LinkAttribute}; +use netlink_packet_route::{address::AddressAttribute, link::LinkAttribute, route::RouteScope}; use once_cell::sync::OnceCell; use rand::distributions::Alphanumeric; use rand::{thread_rng, Rng}; @@ -442,11 +442,23 @@ pub fn get_std_env(config: &StdNetEnvConfig) -> Result { info!("Set default route"); debug!("Set default route for left namespace"); + + debug!("Set left interface as default interface"); add_route_with_netns( + right_veth_pairs[1].right.ip_addr, None, + left_veth_pairs[1].left.index, + left_netns.clone(), + RouteScope::Link, + )?; + + debug!("Set left interface's ip as default route"); + add_route_with_netns( None, + Some(right_veth_pairs[1].right.ip_addr.0), left_veth_pairs[1].left.index, left_netns.clone(), + RouteScope::Universe, )?; debug!("Set default route for right namespace"); @@ -458,6 +470,7 @@ pub fn get_std_env(config: &StdNetEnvConfig) -> Result { None, right_veth_pairs[1].right.index, right_netns.clone(), + RouteScope::Link, )?; } } @@ -467,6 +480,7 @@ pub fn get_std_env(config: &StdNetEnvConfig) -> Result { None, right_veth_pairs[1].right.index, right_netns.clone(), + RouteScope::Universe, )?; } } diff --git a/rattan-core/src/metal/route.rs b/rattan-core/src/metal/route.rs index e6c5e844..12d6a7a8 100644 --- a/rattan-core/src/metal/route.rs +++ b/rattan-core/src/metal/route.rs @@ -9,7 +9,10 @@ use super::{ }; use futures::TryStreamExt; use ipnet::{Ipv4Net, Ipv6Net}; -use netlink_packet_route::link::{LinkAttribute, LinkLayerType}; +use netlink_packet_route::{ + link::{LinkAttribute, LinkLayerType}, + route::RouteScope, +}; use tracing::{debug, error, span, warn, Level}; fn execute_rtnetlink_with_new_thread(netns: Arc, f: F) -> Result<(), Error> @@ -42,6 +45,7 @@ pub fn add_route_with_netns< gateway: U, outif_id: V, netns: Arc, + scope: RouteScope, ) -> Result<(), Error> { let dest = dest.into(); let gateway = gateway.into(); @@ -55,18 +59,23 @@ pub fn add_route_with_netns< execute_rtnetlink_with_new_thread(netns, move |rt, rtnl_handle| { match dest { Some((IpAddr::V4(dest_v4), prefix_length)) => { - let mut handle = rtnl_handle.route().add().v4().destination_prefix( - Ipv4Net::new(dest_v4, prefix_length) - .map_err(|_| { - Error::ConfigError(format!( - "IPv4 prefix length {} is invalid", - prefix_length - )) - })? - .trunc() - .addr(), - prefix_length, - ); + let mut handle = rtnl_handle + .route() + .add() + .v4() + .scope(scope) + .destination_prefix( + Ipv4Net::new(dest_v4, prefix_length) + .map_err(|_| { + Error::ConfigError(format!( + "IPv4 prefix length {} is invalid", + prefix_length + )) + })? + .trunc() + .addr(), + prefix_length, + ); if let Some(gateway) = gateway { if let IpAddr::V4(gateway_v4) = gateway { handle = handle.gateway(gateway_v4); @@ -83,18 +92,23 @@ pub fn add_route_with_netns< rt.block_on(handle.execute()) } Some((IpAddr::V6(dest_v6), prefix_length)) => { - let mut handle = rtnl_handle.route().add().v6().destination_prefix( - Ipv6Net::new(dest_v6, prefix_length) - .map_err(|_| { - Error::ConfigError(format!( - "IPv6 prefix length {} is invalid", - prefix_length - )) - })? - .trunc() - .addr(), - prefix_length, - ); + let mut handle = rtnl_handle + .route() + .add() + .v6() + .scope(scope) + .destination_prefix( + Ipv6Net::new(dest_v6, prefix_length) + .map_err(|_| { + Error::ConfigError(format!( + "IPv6 prefix length {} is invalid", + prefix_length + )) + })? + .trunc() + .addr(), + prefix_length, + ); if let Some(gateway) = gateway { if let IpAddr::V6(gateway_v6) = gateway { handle = handle.gateway(gateway_v6); @@ -111,7 +125,7 @@ pub fn add_route_with_netns< rt.block_on(handle.execute()) } None => { - let mut handle = rtnl_handle.route().add(); + let mut handle = rtnl_handle.route().add().scope(scope); if let Some(if_id) = outif_id { handle = handle.output_interface(if_id); } @@ -125,7 +139,7 @@ pub fn add_route_with_netns< _ => { let res = rt.block_on(handle.v4().execute()); if res.is_ok() { - let mut handle = rtnl_handle.route().add(); + let mut handle = rtnl_handle.route().add().scope(scope); if let Some(if_id) = outif_id { handle = handle.output_interface(if_id); } From 3c9ec945a1c2e9209e85f5e54e0d6f00bf946571 Mon Sep 17 00:00:00 2001 From: un-lock-able Date: Mon, 16 Sep 2024 20:29:02 +0800 Subject: [PATCH 2/2] fix(env): correct route table in right --- rattan-core/src/env.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/rattan-core/src/env.rs b/rattan-core/src/env.rs index 30bf0891..15eff118 100644 --- a/rattan-core/src/env.rs +++ b/rattan-core/src/env.rs @@ -443,7 +443,7 @@ pub fn get_std_env(config: &StdNetEnvConfig) -> Result { debug!("Set default route for left namespace"); - debug!("Set left interface as default interface"); + debug!("Set left interface[1] as default interface"); add_route_with_netns( right_veth_pairs[1].right.ip_addr, None, @@ -452,7 +452,7 @@ pub fn get_std_env(config: &StdNetEnvConfig) -> Result { RouteScope::Link, )?; - debug!("Set left interface's ip as default route"); + debug!("Set left interface[1]'s ip as default route"); add_route_with_netns( None, Some(right_veth_pairs[1].right.ip_addr.0), @@ -476,8 +476,15 @@ pub fn get_std_env(config: &StdNetEnvConfig) -> Result { } _ => { add_route_with_netns( + left_veth_pairs[1].left.ip_addr, None, + right_veth_pairs[1].right.index, + right_netns.clone(), + RouteScope::Link, + )?; + add_route_with_netns( None, + Some(left_veth_pairs[1].left.ip_addr.0), right_veth_pairs[1].right.index, right_netns.clone(), RouteScope::Universe,