Skip to content

Commit

Permalink
vmm: add IPv6 support for pod networking
Browse files Browse the repository at this point in the history
Signed-off-by: MorningTZH <[email protected]>
  • Loading branch information
morningtzh committed Aug 9, 2024
1 parent a500ca6 commit f6ccaa2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
14 changes: 7 additions & 7 deletions vmm/sandbox/src/network/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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,
Expand Down Expand Up @@ -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(),
}
}
Expand Down
39 changes: 25 additions & 14 deletions vmm/sandbox/src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,29 @@ pub struct Network {
routes: Vec<Route>,
}

async fn get_route(
ip_version: IpVersion,
handle: &Handle,
intfs: &Vec<NetworkInterface>,
routes: &mut Vec<Route>,
) -> 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<Self> {
debug!("create network with config: {:?}", config);
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions vmm/sandbox/src/network/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub struct Route {
pub gateway: String,
#[serde(default)]
pub scope: u32,
#[serde(default)]
pub family: u16,
}

impl Route {
Expand All @@ -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;
Expand Down

0 comments on commit f6ccaa2

Please sign in to comment.