From 87f6691459d4dd3c5de40babbf031561c4ff23a7 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Fri, 8 Dec 2023 16:55:24 +0100 Subject: [PATCH] bump netlink-packet-route to 0.18.1 This was painful, a lot of big breaking chnages. However now that I have done to work I see the benefits. The naming is much better and the API is much more type safe. Signed-off-by: Paul Holzinger --- Cargo.lock | 20 +--- Cargo.toml | 2 +- examples/host-device-plugin.rs | 29 ++---- src/network/bridge.rs | 35 ++++--- src/network/core_utils.rs | 12 ++- src/network/netlink.rs | 177 ++++++++++++++++++--------------- src/network/vlan.rs | 20 ++-- src/test/netlink.rs | 16 +-- 8 files changed, 151 insertions(+), 160 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 01ab3059f..eb1d096c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1353,7 +1353,7 @@ dependencies = [ "log", "mozim", "netlink-packet-core", - "netlink-packet-route 0.17.1", + "netlink-packet-route", "netlink-packet-utils", "netlink-sys", "nispor", @@ -1399,20 +1399,6 @@ dependencies = [ "netlink-packet-utils", ] -[[package]] -name = "netlink-packet-route" -version = "0.17.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "053998cea5a306971f88580d0829e90f270f940befd7cf928da179d4187a5a66" -dependencies = [ - "anyhow", - "bitflags 1.3.2", - "byteorder", - "libc", - "netlink-packet-core", - "netlink-packet-utils", -] - [[package]] name = "netlink-packet-route" version = "0.18.1" @@ -1478,7 +1464,7 @@ dependencies = [ "libc", "log", "mptcp-pm", - "netlink-packet-route 0.18.1", + "netlink-packet-route", "netlink-packet-utils", "netlink-sys", "rtnetlink", @@ -1840,7 +1826,7 @@ dependencies = [ "futures", "log", "netlink-packet-core", - "netlink-packet-route 0.18.1", + "netlink-packet-route", "netlink-packet-utils", "netlink-proto", "netlink-sys", diff --git a/Cargo.toml b/Cargo.toml index c815b2010..8d5e1eabb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,7 +47,7 @@ nix = { version = "0.27.1", features = ["sched", "signal", "user"] } rand = "0.8.5" sha2 = "0.10.8" netlink-packet-utils = "0.5.2" -netlink-packet-route = "0.17.1" +netlink-packet-route = "0.18.1" netlink-packet-core = "0.7.0" fs2 = "0.4.3" netlink-sys = "0.8.5" diff --git a/examples/host-device-plugin.rs b/examples/host-device-plugin.rs index a45bc88a1..8d1340b82 100644 --- a/examples/host-device-plugin.rs +++ b/examples/host-device-plugin.rs @@ -1,10 +1,6 @@ //! This is just an example plugin, do not use it in production! -use std::{ - collections::HashMap, - net::{Ipv4Addr, Ipv6Addr}, - os::fd::AsFd, -}; +use std::{collections::HashMap, os::fd::AsFd}; use netavark::{ network::{ @@ -14,7 +10,7 @@ use netavark::{ new_error, plugin::{Info, Plugin, PluginExec, API_VERSION}, }; -use netlink_packet_route::{address::Nla, nlas::link}; +use netlink_packet_route::{address::AddressAttribute, link::LinkAttribute}; fn main() { let info = Info::new("0.1.0-dev".to_owned(), API_VERSION.to_owned(), None); @@ -48,8 +44,8 @@ impl Plugin for Exec { let link = host.netlink.get_link(netlink::LinkID::Name(name.clone()))?; let mut mac_address = String::from(""); - for nla in link.nlas { - if let link::Nla::Address(ref addr) = nla { + for nla in link.attributes { + if let LinkAttribute::Address(ref addr) = nla { mac_address = CoreUtils::encode_address_to_hex(addr); } } @@ -58,20 +54,9 @@ impl Plugin for Exec { let mut subnets = Vec::new(); for address in addresses { if address.header.index == link.header.index { - for nla in address.nlas { - if let Nla::Address(a) = &nla { - let ip = match a.len() { - 4 => Ipv4Addr::new(a[0], a[1], a[2], a[3]).into(), - 16 => Ipv6Addr::from([ - a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], - a[11], a[12], a[13], a[14], a[15], - ]) - .into(), - len => { - return Err(new_error!("invalid netlink address, length: {}", len)) - } - }; - let net = ipnet::IpNet::new(ip, address.header.prefix_len)?; + for nla in address.attributes { + if let AddressAttribute::Address(ip) = &nla { + let net = ipnet::IpNet::new(*ip, address.header.prefix_len)?; subnets.push(types::NetAddress { gateway: None, ipnet: net, diff --git a/src/network/bridge.rs b/src/network/bridge.rs index 7c0eaaa1c..b7f714a2f 100644 --- a/src/network/bridge.rs +++ b/src/network/bridge.rs @@ -2,9 +2,8 @@ use std::{collections::HashMap, net::IpAddr, os::fd::BorrowedFd, sync::Once}; use ipnet::IpNet; use log::{debug, error}; -use netlink_packet_route::{ - nlas::link::{Info, InfoData, InfoKind, Nla, VethInfo}, - LinkMessage, +use netlink_packet_route::link::{ + InfoData, InfoKind, InfoVeth, LinkAttribute, LinkInfo, LinkMessage, }; use crate::{ @@ -576,8 +575,8 @@ fn create_interfaces( .wrap("get bridge interface")?; let mut mac = None; - for nla in link.nlas.into_iter() { - if let Nla::Address(addr) = nla { + for nla in link.attributes.into_iter() { + if let LinkAttribute::Address(addr) = nla { mac = Some(addr); } } @@ -637,7 +636,7 @@ fn create_veth_pair<'fd>( let mut host_veth = netlink::CreateLinkOptions::new(String::from(""), InfoKind::Veth); host_veth.mtu = data.mtu; host_veth.primary_index = primary_index; - host_veth.info_data = Some(InfoData::Veth(VethInfo::Peer(peer))); + host_veth.info_data = Some(InfoData::Veth(InfoVeth::Peer(peer))); host.create_link(host_veth).map_err(|err| match err { NetavarkError::Netlink(ref e) if -e.raw_code() == libc::EEXIST => NetavarkError::wrap( @@ -659,11 +658,11 @@ fn create_veth_pair<'fd>( let mut mac = String::from(""); let mut host_link = 0; - for nla in veth.nlas.into_iter() { - if let Nla::Address(ref addr) = nla { + for nla in veth.attributes.into_iter() { + if let LinkAttribute::Address(ref addr) = nla { mac = CoreUtils::encode_address_to_hex(addr); } - if let Nla::Link(link) = nla { + if let LinkAttribute::Link(link) = nla { host_link = link; } } @@ -697,8 +696,8 @@ fn create_veth_pair<'fd>( if data.ipam.ipv6_enabled { let host_veth = host.get_link(netlink::LinkID::ID(host_link))?; - for nla in host_veth.nlas.into_iter() { - if let Nla::IfName(name) = nla { + for nla in host_veth.attributes.into_iter() { + if let LinkAttribute::IfName(name) = nla { // Disable dad inside on the host too let disable_dad_in_container = format!("/proc/sys/net/ipv6/conf/{name}/accept_dad"); core_utils::CoreUtils::apply_sysctl_value(disable_dad_in_container, "0")?; @@ -747,10 +746,10 @@ fn create_veth_pair<'fd>( /// make sure the LinkMessage has the kind bridge fn check_link_is_bridge(msg: LinkMessage, br_name: &str) -> NetavarkResult { - for nla in msg.nlas.iter() { - if let Nla::Info(info) = nla { + for nla in msg.attributes.iter() { + if let LinkAttribute::LinkInfo(info) = nla { for inf in info.iter() { - if let Info::Kind(kind) = inf { + if let LinkInfo::Kind(kind) = inf { if *kind == InfoKind::Bridge { return Ok(msg); } else { @@ -769,10 +768,10 @@ fn check_link_is_bridge(msg: LinkMessage, br_name: &str) -> NetavarkResult NetavarkResult { - for nla in msg.nlas.iter() { - if let Nla::Info(info) = nla { + for nla in msg.attributes.iter() { + if let LinkAttribute::LinkInfo(info) = nla { for inf in info.iter() { - if let Info::Kind(kind) = inf { + if let LinkInfo::Kind(kind) = inf { if *kind == InfoKind::Vrf { return Ok(msg); } else { @@ -808,7 +807,7 @@ fn remove_link( .wrap("failed to get bridge interface")?; let links = host - .dump_links(&mut vec![Nla::Master(br.header.index)]) + .dump_links(&mut vec![LinkAttribute::Controller(br.header.index)]) .wrap("failed to get connected bridge interfaces")?; // no connected interfaces on that bridge we can remove it if links.is_empty() { diff --git a/src/network/core_utils.rs b/src/network/core_utils.rs index 0b0678051..8638f98f4 100644 --- a/src/network/core_utils.rs +++ b/src/network/core_utils.rs @@ -3,10 +3,6 @@ use crate::network::{constants, internal_types, types}; use crate::wrap; use ipnet::IpNet; use log::debug; -use netlink_packet_route::{ - MACVLAN_MODE_BRIDGE, MACVLAN_MODE_PASSTHRU, MACVLAN_MODE_PRIVATE, MACVLAN_MODE_SOURCE, - MACVLAN_MODE_VEPA, -}; use nix::sched; use sha2::{Digest, Sha512}; use std::collections::HashMap; @@ -27,6 +23,14 @@ pub const IPVLAN_MODE_L2: u16 = 0; pub const IPVLAN_MODE_L3: u16 = 1; pub const IPVLAN_MODE_L3S: u16 = 2; +// const were removed upstream: +// https://github.com/rust-netlink/netlink-packet-route/issues/88 +pub const MACVLAN_MODE_PRIVATE: u32 = 1; +pub const MACVLAN_MODE_VEPA: u32 = 2; +pub const MACVLAN_MODE_BRIDGE: u32 = 4; +pub const MACVLAN_MODE_PASSTHRU: u32 = 8; +pub const MACVLAN_MODE_SOURCE: u32 = 16; + pub struct CoreUtils { pub networkns: String, } diff --git a/src/network/netlink.rs b/src/network/netlink.rs index c1d9dfd88..c95a67b4f 100644 --- a/src/network/netlink.rs +++ b/src/network/netlink.rs @@ -14,9 +14,11 @@ use netlink_packet_core::{ NLM_F_REQUEST, }; use netlink_packet_route::{ - nlas::link::{Info, InfoData, InfoKind, Nla}, - AddressMessage, LinkMessage, RouteMessage, RtnlMessage, AF_INET, AF_INET6, IFF_UP, RTN_UNICAST, - RTPROT_STATIC, RTPROT_UNSPEC, RT_SCOPE_UNIVERSE, RT_TABLE_MAIN, + address::AddressMessage, + link::{InfoData, InfoKind, LinkAttribute, LinkInfo}, + link::{LinkFlag, LinkMessage}, + route::{RouteAddress, RouteMessage, RouteProtocol, RouteScope, RouteType}, + AddressFamily, RouteNetlinkMessage, }; use netlink_sys::{protocols::NETLINK_ROUTE, SocketAddr}; @@ -125,13 +127,13 @@ impl Socket { match id { LinkID::ID(id) => msg.header.index = id, - LinkID::Name(name) => msg.nlas.push(Nla::IfName(name)), + LinkID::Name(name) => msg.attributes.push(LinkAttribute::IfName(name)), } - let mut result = self.make_netlink_request(RtnlMessage::GetLink(msg), 0)?; + let mut result = self.make_netlink_request(RouteNetlinkMessage::GetLink(msg), 0)?; expect_netlink_result!(result, 1); match result.remove(0) { - RtnlMessage::NewLink(m) => Ok(m), + RouteNetlinkMessage::NewLink(m) => Ok(m), m => Err(NetavarkError::Message(format!( "unexpected netlink message type: {}", m.message_type() @@ -143,7 +145,7 @@ impl Socket { let mut msg = LinkMessage::default(); parse_create_link_options(&mut msg, options); let result = self.make_netlink_request( - RtnlMessage::NewLink(msg), + RouteNetlinkMessage::NewLink(msg), NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE, )?; expect_netlink_result!(result, 0); @@ -154,8 +156,8 @@ impl Socket { pub fn set_link_name(&mut self, id: u32, name: String) -> NetavarkResult<()> { let mut msg = LinkMessage::default(); msg.header.index = id; - msg.nlas.push(Nla::IfName(name)); - let result = self.make_netlink_request(RtnlMessage::SetLink(msg), NLM_F_ACK)?; + msg.attributes.push(LinkAttribute::IfName(name)); + let result = self.make_netlink_request(RouteNetlinkMessage::SetLink(msg), NLM_F_ACK)?; expect_netlink_result!(result, 0); Ok(()) @@ -166,10 +168,10 @@ impl Socket { match id { LinkID::ID(id) => msg.header.index = id, - LinkID::Name(name) => msg.nlas.push(Nla::IfName(name)), + LinkID::Name(name) => msg.attributes.push(LinkAttribute::IfName(name)), } - let result = self.make_netlink_request(RtnlMessage::DelLink(msg), NLM_F_ACK)?; + let result = self.make_netlink_request(RouteNetlinkMessage::DelLink(msg), NLM_F_ACK)?; expect_netlink_result!(result, 0); Ok(()) } @@ -177,9 +179,10 @@ impl Socket { pub fn set_link_ns(&mut self, link_id: u32, netns: Fd) -> NetavarkResult<()> { let mut msg = LinkMessage::default(); msg.header.index = link_id; - msg.nlas.push(Nla::NetNsFd(netns.as_fd().as_raw_fd())); + msg.attributes + .push(LinkAttribute::NetNsFd(netns.as_fd().as_raw_fd())); - let result = self.make_netlink_request(RtnlMessage::SetLink(msg), NLM_F_ACK)?; + let result = self.make_netlink_request(RouteNetlinkMessage::SetLink(msg), NLM_F_ACK)?; expect_netlink_result!(result, 0); Ok(()) } @@ -188,30 +191,31 @@ impl Socket { let mut msg = AddressMessage::default(); msg.header.index = link_id; - let addr_vec = match addr { + match addr { ipnet::IpNet::V4(v4) => { - msg.header.family = AF_INET as u8; - msg.nlas.push(netlink_packet_route::address::Nla::Broadcast( - v4.broadcast().octets().to_vec(), - )); - v4.addr().octets().to_vec() + msg.header.family = AddressFamily::Inet; + msg.attributes + .push(netlink_packet_route::address::AddressAttribute::Broadcast( + v4.broadcast(), + )); } - ipnet::IpNet::V6(v6) => { - msg.header.family = AF_INET6 as u8; - v6.addr().octets().to_vec() + ipnet::IpNet::V6(_) => { + msg.header.family = AddressFamily::Inet6; } }; msg.header.prefix_len = addr.prefix_len(); - msg.nlas - .push(netlink_packet_route::address::Nla::Local(addr_vec)); + msg.attributes + .push(netlink_packet_route::address::AddressAttribute::Local( + addr.addr(), + )); msg } pub fn add_addr(&mut self, link_id: u32, addr: &ipnet::IpNet) -> NetavarkResult<()> { let msg = Self::create_addr_msg(link_id, addr); let result = match self.make_netlink_request( - RtnlMessage::NewAddress(msg), + RouteNetlinkMessage::NewAddress(msg), NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE, ) { Ok(result) => result, @@ -236,7 +240,7 @@ impl Socket { pub fn del_addr(&mut self, link_id: u32, addr: &ipnet::IpNet) -> NetavarkResult<()> { let msg = Self::create_addr_msg(link_id, addr); - let result = self.make_netlink_request(RtnlMessage::DelAddress(msg), NLM_F_ACK)?; + let result = self.make_netlink_request(RouteNetlinkMessage::DelAddress(msg), NLM_F_ACK)?; expect_netlink_result!(result, 0); Ok(()) @@ -245,39 +249,45 @@ impl Socket { fn create_route_msg(route: &Route) -> RouteMessage { let mut msg = RouteMessage::default(); - msg.header.table = RT_TABLE_MAIN; - msg.header.protocol = RTPROT_STATIC; - msg.header.scope = RT_SCOPE_UNIVERSE; - msg.header.kind = RTN_UNICAST; + msg.header.table = libc::RT_TABLE_MAIN; + msg.header.protocol = RouteProtocol::Static; + msg.header.scope = RouteScope::Universe; + msg.header.kind = RouteType::Unicast; - let (dest_vec, dest_prefix, gateway_vec, final_metric) = match route { + let (dest, dest_prefix, gateway, final_metric) = match route { Route::Ipv4 { dest, gw, metric } => { - msg.header.address_family = AF_INET as u8; + msg.header.address_family = AddressFamily::Inet; ( - dest.addr().octets().to_vec(), + RouteAddress::Inet(dest.addr()), dest.prefix_len(), - gw.octets().to_vec(), + RouteAddress::Inet(*gw), metric.unwrap_or(constants::DEFAULT_METRIC), ) } Route::Ipv6 { dest, gw, metric } => { - msg.header.address_family = AF_INET6 as u8; + msg.header.address_family = AddressFamily::Inet6; ( - dest.addr().octets().to_vec(), + RouteAddress::Inet6(dest.addr()), dest.prefix_len(), - gw.octets().to_vec(), + RouteAddress::Inet6(*gw), metric.unwrap_or(constants::DEFAULT_METRIC), ) } }; msg.header.destination_prefix_length = dest_prefix; - msg.nlas - .push(netlink_packet_route::route::Nla::Destination(dest_vec)); - msg.nlas - .push(netlink_packet_route::route::Nla::Gateway(gateway_vec)); - msg.nlas - .push(netlink_packet_route::route::Nla::Priority(final_metric)); + msg.attributes + .push(netlink_packet_route::route::RouteAttribute::Destination( + dest, + )); + msg.attributes + .push(netlink_packet_route::route::RouteAttribute::Gateway( + gateway, + )); + msg.attributes + .push(netlink_packet_route::route::RouteAttribute::Priority( + final_metric, + )); msg } @@ -285,8 +295,8 @@ impl Socket { let msg = Self::create_route_msg(route); info!("Adding route {}", route); - let result = - self.make_netlink_request(RtnlMessage::NewRoute(msg), NLM_F_ACK | NLM_F_CREATE)?; + let result = self + .make_netlink_request(RouteNetlinkMessage::NewRoute(msg), NLM_F_ACK | NLM_F_CREATE)?; expect_netlink_result!(result, 0); Ok(()) @@ -296,7 +306,7 @@ impl Socket { let msg = Self::create_route_msg(route); info!("Deleting route {}", route); - let result = self.make_netlink_request(RtnlMessage::DelRoute(msg), NLM_F_ACK)?; + let result = self.make_netlink_request(RouteNetlinkMessage::DelRoute(msg), NLM_F_ACK)?; expect_netlink_result!(result, 0); Ok(()) @@ -305,19 +315,19 @@ impl Socket { pub fn dump_routes(&mut self) -> NetavarkResult> { let mut msg = RouteMessage::default(); - msg.header.table = RT_TABLE_MAIN; - msg.header.protocol = RTPROT_UNSPEC; - msg.header.scope = RT_SCOPE_UNIVERSE; - msg.header.kind = RTN_UNICAST; + msg.header.table = libc::RT_TABLE_MAIN; + msg.header.protocol = RouteProtocol::Unspec; + msg.header.scope = RouteScope::Universe; + msg.header.kind = RouteType::Unicast; let results = - self.make_netlink_request(RtnlMessage::GetRoute(msg), NLM_F_DUMP | NLM_F_ACK)?; + self.make_netlink_request(RouteNetlinkMessage::GetRoute(msg), NLM_F_DUMP | NLM_F_ACK)?; let mut routes = Vec::with_capacity(results.len()); for res in results { match res { - RtnlMessage::NewRoute(m) => routes.push(m), + RouteNetlinkMessage::NewRoute(m) => routes.push(m), m => { return Err(NetavarkError::Message(format!( "unexpected netlink message type: {}", @@ -329,18 +339,21 @@ impl Socket { Ok(routes) } - pub fn dump_links(&mut self, nlas: &mut Vec) -> NetavarkResult> { + pub fn dump_links( + &mut self, + nlas: &mut Vec, + ) -> NetavarkResult> { let mut msg = LinkMessage::default(); - msg.nlas.append(nlas); + msg.attributes.append(nlas); let results = - self.make_netlink_request(RtnlMessage::GetLink(msg), NLM_F_DUMP | NLM_F_ACK)?; + self.make_netlink_request(RouteNetlinkMessage::GetLink(msg), NLM_F_DUMP | NLM_F_ACK)?; let mut links = Vec::with_capacity(results.len()); for res in results { match res { - RtnlMessage::NewLink(m) => links.push(m), + RouteNetlinkMessage::NewLink(m) => links.push(m), m => { return Err(NetavarkError::Message(format!( "unexpected netlink message type: {}", @@ -355,14 +368,14 @@ impl Socket { pub fn dump_addresses(&mut self) -> NetavarkResult> { let msg = AddressMessage::default(); - let results = - self.make_netlink_request(RtnlMessage::GetAddress(msg), NLM_F_DUMP | NLM_F_ACK)?; + let results = self + .make_netlink_request(RouteNetlinkMessage::GetAddress(msg), NLM_F_DUMP | NLM_F_ACK)?; let mut addresses = Vec::with_capacity(results.len()); for res in results { match res { - RtnlMessage::NewAddress(m) => addresses.push(m), + RouteNetlinkMessage::NewAddress(m) => addresses.push(m), m => { return Err(NetavarkError::Message(format!( "unexpected netlink message type: {}", @@ -379,14 +392,14 @@ impl Socket { match id { LinkID::ID(id) => msg.header.index = id, - LinkID::Name(name) => msg.nlas.push(Nla::IfName(name)), + LinkID::Name(name) => msg.attributes.push(LinkAttribute::IfName(name)), } - msg.header.flags |= IFF_UP; - msg.header.change_mask |= IFF_UP; + msg.header.flags = vec![LinkFlag::Up]; + msg.header.change_mask = vec![LinkFlag::Up]; let result = self.make_netlink_request( - RtnlMessage::SetLink(msg), + RouteNetlinkMessage::SetLink(msg), NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE, )?; expect_netlink_result!(result, 0); @@ -399,12 +412,12 @@ impl Socket { match id { LinkID::ID(id) => msg.header.index = id, - LinkID::Name(name) => msg.nlas.push(Nla::IfName(name)), + LinkID::Name(name) => msg.attributes.push(LinkAttribute::IfName(name)), } - msg.nlas.push(Nla::Address(mac)); + msg.attributes.push(LinkAttribute::Address(mac)); - let result = self.make_netlink_request(RtnlMessage::SetLink(msg), NLM_F_ACK)?; + let result = self.make_netlink_request(RouteNetlinkMessage::SetLink(msg), NLM_F_ACK)?; expect_netlink_result!(result, 0); Ok(()) @@ -412,14 +425,14 @@ impl Socket { fn make_netlink_request( &mut self, - msg: RtnlMessage, + msg: RouteNetlinkMessage, flags: u16, - ) -> NetavarkResult> { + ) -> NetavarkResult> { self.send(msg, flags).wrap("send to netlink")?; self.recv(flags & NLM_F_DUMP == NLM_F_DUMP) } - fn send(&mut self, msg: RtnlMessage, flags: u16) -> NetavarkResult<()> { + fn send(&mut self, msg: RouteNetlinkMessage, flags: u16) -> NetavarkResult<()> { let mut packet = NetlinkMessage::new(NetlinkHeader::default(), NetlinkPayload::from(msg)); packet.header.flags = NLM_F_REQUEST | flags; packet.header.sequence_number = { @@ -435,7 +448,7 @@ impl Socket { Ok(()) } - fn recv(&mut self, multi: bool) -> NetavarkResult> { + fn recv(&mut self, multi: bool) -> NetavarkResult> { let mut offset = 0; let mut result = Vec::new(); @@ -448,8 +461,8 @@ impl Socket { loop { let bytes = &self.buffer[offset..]; - let rx_packet: NetlinkMessage = NetlinkMessage::deserialize(bytes) - .map_err(|e| { + let rx_packet: NetlinkMessage = + NetlinkMessage::deserialize(bytes).map_err(|e| { NetavarkError::Message(format!( "failed to deserialize netlink message: {e}", )) @@ -517,39 +530,41 @@ impl CreateLinkOptions<'_> { pub fn parse_create_link_options(msg: &mut LinkMessage, options: CreateLinkOptions) { // add link specific data - let mut link_info_nlas = vec![Info::Kind(options.kind)]; + let mut link_info_nlas = vec![LinkInfo::Kind(options.kind)]; if let Some(data) = options.info_data { - link_info_nlas.push(Info::Data(data)); + link_info_nlas.push(LinkInfo::Data(data)); } - msg.nlas.push(Nla::Info(link_info_nlas)); + msg.attributes.push(LinkAttribute::LinkInfo(link_info_nlas)); // add name if !options.name.is_empty() { - msg.nlas.push(Nla::IfName(options.name)); + msg.attributes.push(LinkAttribute::IfName(options.name)); } // add mtu if options.mtu != 0 { - msg.nlas.push(Nla::Mtu(options.mtu)); + msg.attributes.push(LinkAttribute::Mtu(options.mtu)); } // add mac address if !options.mac.is_empty() { - msg.nlas.push(Nla::Address(options.mac)); + msg.attributes.push(LinkAttribute::Address(options.mac)); } // add primary device if options.primary_index != 0 { - msg.nlas.push(Nla::Master(options.primary_index)); + msg.attributes + .push(LinkAttribute::Controller(options.primary_index)); } // add link device if options.link != 0 { - msg.nlas.push(Nla::Link(options.link)); + msg.attributes.push(LinkAttribute::Link(options.link)); } // add netnsfd if let Some(netns) = options.netns { - msg.nlas.push(Nla::NetNsFd(netns.as_raw_fd())); + msg.attributes + .push(LinkAttribute::NetNsFd(netns.as_raw_fd())); } } diff --git a/src/network/vlan.rs b/src/network/vlan.rs index 95035ebb9..83e84c214 100644 --- a/src/network/vlan.rs +++ b/src/network/vlan.rs @@ -2,7 +2,7 @@ use log::{debug, error}; use std::os::fd::BorrowedFd; use std::{collections::HashMap, net::IpAddr}; -use netlink_packet_route::nlas::link::{InfoData, InfoIpVlan, InfoKind, InfoMacVlan, Nla}; +use netlink_packet_route::link::{InfoData, InfoIpVlan, InfoKind, InfoMacVlan, LinkAttribute}; use rand::distributions::{Alphanumeric, DistString}; use crate::network::macvlan_dhcp::{get_dhcp_lease, release_dhcp_lease}; @@ -220,7 +220,7 @@ impl driver::NetworkDriver for Vlan<'_> { &self.info.per_network_opts.interface_name ))?; - let container_mac_address = get_mac_address(dev.nlas)?; + let container_mac_address = get_mac_address(dev.attributes)?; release_dhcp_lease( &self .info @@ -373,12 +373,12 @@ fn setup( netns.add_route(route)? } - get_mac_address(dev.nlas) + get_mac_address(dev.attributes) } -fn get_mac_address(v: Vec) -> NetavarkResult { +fn get_mac_address(v: Vec) -> NetavarkResult { for nla in v.into_iter() { - if let Nla::Address(ref addr) = nla { + if let LinkAttribute::Address(ref addr) = nla { return Ok(CoreUtils::encode_address_to_hex(addr)); } } @@ -393,11 +393,11 @@ fn get_default_route_interface(host: &mut netlink::Socket) -> NetavarkResult NetavarkResult 0 { let link = host.get_link(netlink::LinkID::ID(out_if))?; - let name = link.nlas.iter().find_map(|nla| { - if let Nla::IfName(name) = nla { + let name = link.attributes.iter().find_map(|nla| { + if let LinkAttribute::IfName(name) = nla { Some(name) } else { None diff --git a/src/test/netlink.rs b/src/test/netlink.rs index c0ec7645a..6b7eb651c 100644 --- a/src/test/netlink.rs +++ b/src/test/netlink.rs @@ -1,7 +1,9 @@ #[cfg(test)] mod tests { + use std::net::{IpAddr, Ipv4Addr}; + use netavark::network::netlink::*; - use netlink_packet_route::{address, nlas::link::InfoKind}; + use netlink_packet_route::{address, link::InfoKind}; macro_rules! test_setup { () => { @@ -174,14 +176,14 @@ mod tests { assert!(out.status.success(), "failed to set up lo via ip"); let addresses = sock.dump_addresses().expect("dump_addresses failed"); - for nla in addresses[0].nlas.iter() { - if let address::Nla::Address(a) = nla { - assert_eq!(a, &vec![127, 0, 0, 1]) + for nla in addresses[0].attributes.iter() { + if let address::AddressAttribute::Address(ip) = nla { + assert_eq!(ip, &IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))) } } - for nla in addresses[1].nlas.iter() { - if let address::Nla::Address(a) = nla { - assert_eq!(a, &vec![10, 0, 0, 2]) + for nla in addresses[1].attributes.iter() { + if let address::AddressAttribute::Address(ip) = nla { + assert_eq!(ip, &IpAddr::V4(Ipv4Addr::new(10, 0, 0, 2))) } } }