Skip to content

Commit 9ecee15

Browse files
authored
Merge pull request kuasar-io#161 from Burning1020/add-route-flag
vmm: Sync route flags
2 parents f6594a1 + ab97ded commit 9ecee15

File tree

11 files changed

+235
-187
lines changed

11 files changed

+235
-187
lines changed

vmm/common/src/protos/sandbox.proto

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ message Route {
102102
string source = 4;
103103
uint32 scope = 5;
104104
IPFamily family = 6;
105+
uint32 flags = 7;
105106
}
106107

107108
message UpdateInterfacesRequest {

vmm/sandbox/Cargo.lock

+20-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vmm/sandbox/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ qapi = { version = "0.8.0", features = ["qmp", "async-tokio-all"] }
4141
qapi-spec = { version = "0.3.1" }
4242
sandbox-derive = { path = "derive" }
4343
api_client = { git = "https://github.com/cloud-hypervisor/cloud-hypervisor.git" }
44-
rtnetlink = "0.13.1"
45-
netlink-packet-route = "0.17.0"
44+
rtnetlink = "0.14.1"
45+
netlink-packet-route = "0.19.0"
4646
netlink-packet-core = "0.7.0"
4747
ttrpc = { version = "0.7", features = ["async"] }
4848
protobuf = "3.2"

vmm/sandbox/src/network/address.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ limitations under the License.
1515
*/
1616

1717
use std::{
18-
convert::TryInto,
1918
fmt::{Debug, Formatter},
2019
net::{IpAddr, Ipv4Addr, Ipv6Addr},
2120
str::FromStr,
2221
};
2322

2423
use anyhow::anyhow;
2524
use containerd_sandbox::error::Result;
25+
use netlink_packet_route::route::RouteAddress;
2626
use serde_derive::{Deserialize, Serialize};
2727

2828
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
@@ -141,16 +141,10 @@ impl std::fmt::Display for MacAddress {
141141
}
142142
}
143143

144-
pub fn convert_to_ip_address(addr: Vec<u8>) -> Result<IpAddr> {
145-
if addr.len() == 4 {
146-
let arr: &[u8; 4] = addr.as_slice().try_into().unwrap();
147-
let address = IpAddr::from(*arr);
148-
return Ok(address);
144+
pub fn convert_to_ip_address(address: RouteAddress) -> Result<IpAddr> {
145+
match address {
146+
RouteAddress::Inet(addr) => Ok(IpAddr::V4(addr)),
147+
RouteAddress::Inet6(addr) => Ok(IpAddr::V6(addr)),
148+
_ => Err(anyhow!("unsupported ip address {:?}", address).into()),
149149
}
150-
if addr.len() == 16 {
151-
let arr: &[u8; 16] = addr.as_slice().try_into().unwrap();
152-
let address = IpAddr::from(*arr);
153-
return Ok(address);
154-
}
155-
Err(anyhow!("ip address vec has length {}", addr.len()).into())
156150
}

vmm/sandbox/src/network/convert.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
use netlink_packet_route::{AF_INET, AF_INET6};
17+
use netlink_packet_route::AddressFamily;
1818
use protobuf::{EnumOrUnknown, SpecialFields};
1919
use vmm_common::api::sandbox::{IPAddress, IPFamily, Interface, Route};
2020

@@ -57,12 +57,13 @@ impl From<&crate::network::Route> for Route {
5757
gateway: r.gateway.to_string(),
5858
device: r.device.to_string(),
5959
source: r.source.to_string(),
60-
scope: r.scope,
61-
family: EnumOrUnknown::from(match r.family {
62-
AF_INET => IPFamily::v4,
63-
AF_INET6 => IPFamily::v6,
60+
scope: r.scope as u32,
61+
family: EnumOrUnknown::from(match AddressFamily::from(r.family) {
62+
AddressFamily::Inet => IPFamily::v4,
63+
AddressFamily::Inet6 => IPFamily::v6,
6464
_ => IPFamily::default(),
6565
}),
66+
flags: r.flags,
6667
special_fields: Default::default(),
6768
}
6869
}

vmm/sandbox/src/network/link.rs

+32-24
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ use anyhow::anyhow;
2828
use containerd_sandbox::error::Result;
2929
use futures_util::TryStreamExt;
3030
use libc::{IFF_MULTI_QUEUE, IFF_NO_PI, IFF_TAP, IFF_VNET_HDR};
31-
use netlink_packet_route::{
32-
link::nlas::{Info, InfoData, InfoIpVlan, InfoKind, InfoMacVlan, InfoMacVtap, InfoVlan},
33-
nlas::link::InfoVxlan,
34-
LinkMessage,
31+
use netlink_packet_route::link::{
32+
InfoData, InfoIpVlan, InfoKind, InfoMacVlan, InfoMacVtap, InfoVlan, InfoVxlan, LinkFlag,
33+
LinkInfo, LinkMessage,
3534
};
3635
use nix::{
3736
ioctl_read_bad, ioctl_write_ptr_bad, libc,
@@ -44,7 +43,7 @@ use serde_derive::{Deserialize, Serialize};
4443
use crate::{
4544
device::{DeviceInfo, PhysicalDeviceInfo, TapDeviceInfo, VhostUserDeviceInfo},
4645
network::{
47-
address::{convert_to_ip_address, CniIPAddress, IpNet, MacAddress},
46+
address::{CniIPAddress, IpNet, MacAddress},
4847
create_netlink_handle, execute_in_netns, run_in_new_netns,
4948
},
5049
sandbox::KuasarSandbox,
@@ -98,7 +97,6 @@ pub enum LinkType {
9897
Ipvlan(u16),
9998
Macvlan(u32),
10099
Macvtap(u32),
101-
Iptun,
102100
Tun,
103101
VhostUser(String),
104102
Physical(String, String),
@@ -124,7 +122,6 @@ impl Display for LinkType {
124122
LinkType::Ipvlan(_) => "ipvlan".to_string(),
125123
LinkType::Macvlan(_) => "macvlan".to_string(),
126124
LinkType::Macvtap(_) => "macvtap".to_string(),
127-
LinkType::Iptun => "iptun".to_string(),
128125
LinkType::Tun => "tun".to_string(),
129126
LinkType::VhostUser(_) => "vhostuser".to_string(),
130127
LinkType::Physical(_, _) => "physical".to_string(),
@@ -167,7 +164,6 @@ impl From<InfoData> for LinkType {
167164
return Self::Macvtap(*i);
168165
}
169166
}
170-
InfoData::IpTun(_) => return Self::Iptun,
171167
_ => return Self::Unkonwn,
172168
}
173169
Self::Unkonwn
@@ -210,6 +206,19 @@ pub struct NetworkInterface {
210206
pub queue: u32,
211207
}
212208

209+
// netlink-packet-route-0.19.0/src/link/link_flag.rs:26
210+
pub(crate) struct VecLinkFlag(pub Vec<LinkFlag>);
211+
212+
impl From<&VecLinkFlag> for u32 {
213+
fn from(v: &VecLinkFlag) -> u32 {
214+
let mut d: u32 = 0;
215+
for flag in &v.0 {
216+
d += u32::from(*flag);
217+
}
218+
d
219+
}
220+
}
221+
213222
impl NetworkInterface {
214223
pub async fn parse_from_message(
215224
msg: LinkMessage,
@@ -218,31 +227,31 @@ impl NetworkInterface {
218227
handle: &Handle,
219228
) -> Result<Self> {
220229
let mut intf = NetworkInterface {
221-
flags: msg.header.flags,
230+
flags: u32::from(&VecLinkFlag(msg.header.flags)),
222231
index: msg.header.index,
223232
..NetworkInterface::default()
224233
};
225-
for nla in msg.nlas.into_iter() {
226-
use netlink_packet_route::nlas::link::Nla;
227-
match nla {
228-
Nla::Info(infos) => {
234+
use netlink_packet_route::link::LinkAttribute;
235+
for attribute in msg.attributes.into_iter() {
236+
match attribute {
237+
LinkAttribute::LinkInfo(infos) => {
229238
for info in infos {
230-
if let Info::Data(d) = info {
239+
if let LinkInfo::Data(d) = info {
231240
intf.r#type = d.into();
232-
} else if let Info::Kind(InfoKind::Veth) = info {
233-
// for veth, there is no Info::Data, but SlaveKind and SlaveData
241+
} else if let LinkInfo::Kind(InfoKind::Veth) = info {
242+
// for veth, there is no Info::Data, but SlaveKind and SlaveData,
234243
// so we have to get the type from Info::Kind
235244
intf.queue = queue;
236245
intf.r#type = LinkType::Veth;
237246
}
238247
}
239248
}
240-
Nla::IfName(s) => {
249+
LinkAttribute::IfName(s) => {
241250
intf.name = s;
242251
}
243-
Nla::IfAlias(a) => intf.alias = a,
244-
Nla::Mtu(m) => intf.mtu = m,
245-
Nla::Address(u) => intf.mac_address = MacAddress(u),
252+
LinkAttribute::IfAlias(a) => intf.alias = a,
253+
LinkAttribute::Mtu(m) => intf.mtu = m,
254+
LinkAttribute::Address(u) => intf.mac_address = MacAddress(u),
246255
_ => {}
247256
}
248257
}
@@ -252,10 +261,9 @@ impl NetworkInterface {
252261
.set_link_index_filter(msg.header.index)
253262
.execute();
254263
while let Some(msg) = addresses.try_next().await.map_err(|e| anyhow!(e))? {
255-
use netlink_packet_route::nlas::address::Nla;
256-
for nla in msg.nlas.into_iter() {
257-
if let Nla::Address(addr) = nla {
258-
let address = convert_to_ip_address(addr)?;
264+
use netlink_packet_route::address::AddressAttribute;
265+
for nla in msg.attributes.into_iter() {
266+
if let AddressAttribute::Address(address) = nla {
259267
let mask_len = msg.header.prefix_len;
260268
if address.is_loopback() {
261269
intf.r#type = LinkType::Loopback;

vmm/sandbox/src/network/netlink.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License.
1616

1717
use futures_util::StreamExt;
1818
use netlink_packet_core::{NetlinkMessage, NLM_F_ACK, NLM_F_CREATE, NLM_F_EXCL, NLM_F_REQUEST};
19-
use netlink_packet_route::{RtnlMessage, TcMessage};
19+
use netlink_packet_route::{tc::TcMessage, RouteNetlinkMessage};
2020
use rtnetlink::{try_nl, Error, Handle};
2121

2222
const HANDLE_INGRESS: u32 = 0xfffffff1;
@@ -42,7 +42,7 @@ impl QDiscAddRequest {
4242
message,
4343
} = self;
4444

45-
let mut req = NetlinkMessage::from(RtnlMessage::NewQueueDiscipline(message));
45+
let mut req = NetlinkMessage::from(RouteNetlinkMessage::NewQueueDiscipline(message));
4646
req.header.flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK;
4747

4848
let mut response = handle.request(req)?;
@@ -58,7 +58,7 @@ impl QDiscAddRequest {
5858
}
5959

6060
pub fn ingress(mut self) -> Self {
61-
self.message.header.parent = HANDLE_INGRESS;
61+
self.message.header.parent = HANDLE_INGRESS.into();
6262
self
6363
}
6464
}
@@ -73,7 +73,7 @@ impl TrafficFilterSetRequest {
7373
pub(crate) fn new(handle: Handle, ifindex: i32) -> Self {
7474
let mut message = TcMessage::default();
7575
message.header.index = ifindex;
76-
message.header.parent = HANDLE_TC_FILTER;
76+
message.header.parent = HANDLE_TC_FILTER.into();
7777

7878
Self { handle, message }
7979
}
@@ -84,7 +84,7 @@ impl TrafficFilterSetRequest {
8484
message,
8585
} = self;
8686

87-
let mut req = NetlinkMessage::from(RtnlMessage::NewTrafficFilter(message));
87+
let mut req = NetlinkMessage::from(RouteNetlinkMessage::NewTrafficFilter(message));
8888
req.header.flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK;
8989

9090
let mut response = handle.request(req)?;

0 commit comments

Comments
 (0)