mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-08-25 11:13:15 +00:00
netlink: implement TryFrom instead of From to avoid unwrap()
Implment `TryFrom<IPAddress> for RtIPAddr` instead of From<IPAddress>, so error code could be returned instead of unwrap(). Do the same for `TryFrom<Route> for RtRoute`. Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
This commit is contained in:
parent
1b8c2cba60
commit
de23ec1943
@ -6,9 +6,13 @@
|
|||||||
|
|
||||||
//! Dedicated Netlink interfaces for Kata agent protocol handler.
|
//! Dedicated Netlink interfaces for Kata agent protocol handler.
|
||||||
|
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
use protobuf::RepeatedField;
|
use protobuf::RepeatedField;
|
||||||
use protocols::types::{ARPNeighbor, IPAddress, IPFamily, Interface, Route};
|
use protocols::types::{ARPNeighbor, IPAddress, IPFamily, Interface, Route};
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
#[cfg(feature = "with-log")]
|
#[cfg(feature = "with-log")]
|
||||||
// Convenience macro to obtain the scope logger
|
// Convenience macro to obtain the scope logger
|
||||||
macro_rules! sl {
|
macro_rules! sl {
|
||||||
@ -17,8 +21,6 @@ macro_rules! sl {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
impl super::RtnlHandle {
|
impl super::RtnlHandle {
|
||||||
pub fn update_interface(&mut self, iface: &Interface) -> Result<Interface> {
|
pub fn update_interface(&mut self, iface: &Interface) -> Result<Interface> {
|
||||||
// the reliable way to find link is using hardware address
|
// the reliable way to find link is using hardware address
|
||||||
@ -41,7 +43,7 @@ impl super::RtnlHandle {
|
|||||||
|
|
||||||
// add new ip addresses in request
|
// add new ip addresses in request
|
||||||
for grpc_addr in &iface.IPAddresses {
|
for grpc_addr in &iface.IPAddresses {
|
||||||
let rtip = RtIPAddr::from(grpc_addr.clone());
|
let rtip = RtIPAddr::try_from(grpc_addr.clone())?;
|
||||||
self.add_one_address(&ifinfo, &rtip)?;
|
self.add_one_address(&ifinfo, &rtip)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,7 +227,7 @@ impl super::RtnlHandle {
|
|||||||
|
|
||||||
for grpcroute in rt {
|
for grpcroute in rt {
|
||||||
if grpcroute.gateway.as_str() == "" {
|
if grpcroute.gateway.as_str() == "" {
|
||||||
let r = RtRoute::from(grpcroute.clone());
|
let r = RtRoute::try_from(grpcroute.clone())?;
|
||||||
if r.index == -1 {
|
if r.index == -1 {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -235,7 +237,7 @@ impl super::RtnlHandle {
|
|||||||
|
|
||||||
for grpcroute in rt {
|
for grpcroute in rt {
|
||||||
if grpcroute.gateway.as_str() != "" {
|
if grpcroute.gateway.as_str() != "" {
|
||||||
let r = RtRoute::from(grpcroute.clone());
|
let r = RtRoute::try_from(grpcroute.clone())?;
|
||||||
if r.index == -1 {
|
if r.index == -1 {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -414,31 +416,35 @@ impl super::RtnlHandle {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<IPAddress> for RtIPAddr {
|
impl TryFrom<IPAddress> for RtIPAddr {
|
||||||
fn from(ipi: IPAddress) -> Self {
|
type Error = nix::Error;
|
||||||
|
|
||||||
|
fn try_from(ipi: IPAddress) -> std::result::Result<Self, Self::Error> {
|
||||||
let ip_family = if ipi.family == IPFamily::v4 {
|
let ip_family = if ipi.family == IPFamily::v4 {
|
||||||
libc::AF_INET
|
libc::AF_INET
|
||||||
} else {
|
} else {
|
||||||
libc::AF_INET6
|
libc::AF_INET6
|
||||||
} as __u8;
|
} as __u8;
|
||||||
|
|
||||||
let ip_mask = parser::parse_u8(ipi.mask.as_str(), 10).unwrap();
|
let ip_mask = parser::parse_u8(ipi.mask.as_str(), 10)?;
|
||||||
let addr = parser::parse_ip_addr(ipi.address.as_ref()).unwrap();
|
let addr = parser::parse_ip_addr(ipi.address.as_ref())?;
|
||||||
|
|
||||||
Self {
|
Ok(Self {
|
||||||
ip_family,
|
ip_family,
|
||||||
ip_mask,
|
ip_mask,
|
||||||
addr,
|
addr,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Route> for RtRoute {
|
impl TryFrom<Route> for RtRoute {
|
||||||
fn from(r: Route) -> Self {
|
type Error = nix::Error;
|
||||||
|
|
||||||
|
fn try_from(r: Route) -> std::result::Result<Self, Self::Error> {
|
||||||
// only handle ipv4
|
// only handle ipv4
|
||||||
|
|
||||||
let index = {
|
let index = {
|
||||||
let mut rh = RtnlHandle::new(NETLINK_ROUTE, 0).unwrap();
|
let mut rh = RtnlHandle::new(NETLINK_ROUTE, 0)?;
|
||||||
match rh.find_link_by_name(r.device.as_str()) {
|
match rh.find_link_by_name(r.device.as_str()) {
|
||||||
Ok(ifi) => ifi.ifi_index,
|
Ok(ifi) => ifi.ifi_index,
|
||||||
Err(_) => -1,
|
Err(_) => -1,
|
||||||
@ -448,21 +454,21 @@ impl From<Route> for RtRoute {
|
|||||||
let (dest, dst_len) = if r.dest.is_empty() {
|
let (dest, dst_len) = if r.dest.is_empty() {
|
||||||
(Some(vec![0 as u8; 4]), 0)
|
(Some(vec![0 as u8; 4]), 0)
|
||||||
} else {
|
} else {
|
||||||
let (dst, mask) = parser::parse_cidr(r.dest.as_str()).unwrap();
|
let (dst, mask) = parser::parse_cidr(r.dest.as_str())?;
|
||||||
(Some(dst), mask)
|
(Some(dst), mask)
|
||||||
};
|
};
|
||||||
|
|
||||||
let (source, src_len) = if r.source.is_empty() {
|
let (source, src_len) = if r.source.is_empty() {
|
||||||
(None, 0)
|
(None, 0)
|
||||||
} else {
|
} else {
|
||||||
let (src, mask) = parser::parse_cidr(r.source.as_str()).unwrap();
|
let (src, mask) = parser::parse_cidr(r.source.as_str())?;
|
||||||
(Some(src), mask)
|
(Some(src), mask)
|
||||||
};
|
};
|
||||||
|
|
||||||
let gateway = if r.gateway.is_empty() {
|
let gateway = if r.gateway.is_empty() {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(parser::parse_ip_addr(r.gateway.as_str()).unwrap())
|
Some(parser::parse_ip_addr(r.gateway.as_str())?)
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -472,7 +478,8 @@ impl From<Route> for RtRoute {
|
|||||||
(tdest, tdst_len)
|
(tdest, tdst_len)
|
||||||
};
|
};
|
||||||
*/
|
*/
|
||||||
Self {
|
|
||||||
|
Ok(Self {
|
||||||
dest,
|
dest,
|
||||||
source,
|
source,
|
||||||
src_len,
|
src_len,
|
||||||
@ -481,7 +488,7 @@ impl From<Route> for RtRoute {
|
|||||||
gateway,
|
gateway,
|
||||||
scope: r.scope as u8,
|
scope: r.scope as u8,
|
||||||
protocol: RTPROTO_UNSPEC,
|
protocol: RTPROTO_UNSPEC,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user