Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion rattan-core/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -442,11 +442,23 @@ pub fn get_std_env(config: &StdNetEnvConfig) -> Result<StdNetEnv, Error> {
info!("Set default route");

debug!("Set default route for left namespace");

debug!("Set left interface[1] 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[1]'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");
Expand All @@ -458,15 +470,24 @@ pub fn get_std_env(config: &StdNetEnvConfig) -> Result<StdNetEnv, Error> {
None,
right_veth_pairs[1].right.index,
right_netns.clone(),
RouteScope::Link,
)?;
}
}
_ => {
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,
)?;
}
}
Expand Down
68 changes: 41 additions & 27 deletions rattan-core/src/metal/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F>(netns: Arc<NetNs>, f: F) -> Result<(), Error>
Expand Down Expand Up @@ -42,6 +45,7 @@ pub fn add_route_with_netns<
gateway: U,
outif_id: V,
netns: Arc<NetNs>,
scope: RouteScope,
) -> Result<(), Error> {
let dest = dest.into();
let gateway = gateway.into();
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down