Skip to content

Commit

Permalink
bump netlink-packet-route to 0.18.1
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Luap99 committed Dec 8, 2023
1 parent ae60957 commit 87f6691
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 160 deletions.
20 changes: 3 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
29 changes: 7 additions & 22 deletions examples/host-device-plugin.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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,
Expand Down
35 changes: 17 additions & 18 deletions src/network/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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(
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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")?;
Expand Down Expand Up @@ -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<LinkMessage> {
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 {
Expand All @@ -769,10 +768,10 @@ fn check_link_is_bridge(msg: LinkMessage, br_name: &str) -> NetavarkResult<LinkM

/// make sure the LinkMessage is the kind VRF
fn check_link_is_vrf(msg: LinkMessage, vrf_name: &str) -> NetavarkResult<LinkMessage> {
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 {
Expand Down Expand Up @@ -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() {
Expand Down
12 changes: 8 additions & 4 deletions src/network/core_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
}
Expand Down
Loading

0 comments on commit 87f6691

Please sign in to comment.