Skip to content

Commit

Permalink
update netlink-packet-route to 0.20.0
Browse files Browse the repository at this point in the history
Includes fixes for the breaking changes:
- new macvlan/ipvlan types
- net LinkFlags bitmap instad of the old vector

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed May 30, 2024
1 parent 46e4559 commit 8c28d20
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 33 deletions.
21 changes: 18 additions & 3 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.29.0", features = ["sched", "signal", "user"] }
rand = "0.8.5"
sha2 = "0.10.8"
netlink-packet-utils = "0.5.2"
netlink-packet-route = "0.19.0"
netlink-packet-route = "0.20.0"
netlink-packet-core = "0.7.0"
nftables = "0.4"
fs2 = "0.4.3"
Expand Down
33 changes: 11 additions & 22 deletions src/network/core_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::network::{constants, internal_types, types};
use crate::wrap;
use ipnet::IpNet;
use log::debug;
use netlink_packet_route::link::{IpVlanMode, MacVlanMode};
use nix::sched;
use sha2::{Digest, Sha512};
use std::collections::HashMap;
Expand All @@ -19,18 +20,6 @@ use sysctl::{Sysctl, SysctlError};

use super::netlink;

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 Expand Up @@ -229,27 +218,27 @@ impl CoreUtils {
Ok(result)
}

pub fn get_macvlan_mode_from_string(mode: Option<&str>) -> NetavarkResult<u32> {
pub fn get_macvlan_mode_from_string(mode: Option<&str>) -> NetavarkResult<MacVlanMode> {
match mode {
// default to bridge when unset
None | Some("") | Some("bridge") => Ok(MACVLAN_MODE_BRIDGE),
Some("private") => Ok(MACVLAN_MODE_PRIVATE),
Some("vepa") => Ok(MACVLAN_MODE_VEPA),
Some("passthru") => Ok(MACVLAN_MODE_PASSTHRU),
Some("source") => Ok(MACVLAN_MODE_SOURCE),
None | Some("") | Some("bridge") => Ok(MacVlanMode::Bridge),
Some("private") => Ok(MacVlanMode::Private),
Some("vepa") => Ok(MacVlanMode::Vepa),
Some("passthru") => Ok(MacVlanMode::Passthrough),
Some("source") => Ok(MacVlanMode::Source),
// default to bridge
Some(name) => Err(NetavarkError::msg(format!(
"invalid macvlan mode \"{name}\""
))),
}
}

pub fn get_ipvlan_mode_from_string(mode: Option<&str>) -> NetavarkResult<u16> {
pub fn get_ipvlan_mode_from_string(mode: Option<&str>) -> NetavarkResult<IpVlanMode> {
match mode {
// default to l2 when unset
None | Some("") | Some("l2") => Ok(IPVLAN_MODE_L2),
Some("l3") => Ok(IPVLAN_MODE_L3),
Some("l3s") => Ok(IPVLAN_MODE_L3S),
None | Some("") | Some("l2") => Ok(IpVlanMode::L2),
Some("l3") => Ok(IpVlanMode::L3),
Some("l3s") => Ok(IpVlanMode::L3S),
Some(name) => Err(NetavarkError::msg(format!(
"invalid ipvlan mode \"{name}\""
))),
Expand Down
7 changes: 3 additions & 4 deletions src/network/netlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use netlink_packet_core::{
};
use netlink_packet_route::{
address::AddressMessage,
link::{InfoData, InfoKind, LinkAttribute, LinkInfo},
link::{LinkFlag, LinkMessage},
link::{InfoData, InfoKind, LinkAttribute, LinkFlags, LinkInfo, LinkMessage},
route::{RouteAddress, RouteMessage, RouteProtocol, RouteScope, RouteType},
AddressFamily, RouteNetlinkMessage,
};
Expand Down Expand Up @@ -395,8 +394,8 @@ impl Socket {
LinkID::Name(name) => msg.attributes.push(LinkAttribute::IfName(name)),
}

msg.header.flags = vec![LinkFlag::Up];
msg.header.change_mask = vec![LinkFlag::Up];
msg.header.flags = LinkFlags::Up;
msg.header.change_mask = LinkFlags::Up;

let result = self.make_netlink_request(
RouteNetlinkMessage::SetLink(msg),
Expand Down
8 changes: 5 additions & 3 deletions src/network/vlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use log::{debug, error};
use std::os::fd::BorrowedFd;
use std::{collections::HashMap, net::IpAddr};

use netlink_packet_route::link::{InfoData, InfoIpVlan, InfoKind, InfoMacVlan, LinkAttribute};
use netlink_packet_route::link::{
InfoData, InfoIpVlan, InfoKind, InfoMacVlan, IpVlanMode, LinkAttribute, MacVlanMode,
};
use rand::distributions::{Alphanumeric, DistString};

use crate::network::macvlan_dhcp::{get_dhcp_lease, release_dhcp_lease};
Expand Down Expand Up @@ -30,14 +32,14 @@ enum KindData {
/// static mac address
mac_address: Option<Vec<u8>>,
/// macvlan mode
mode: u32,
mode: MacVlanMode,

// IFLA_MACVLAN_BC_CUTOFF option if set
bclim: Option<i32>,
},
IpVlan {
/// ipvlan mode
mode: u16,
mode: IpVlanMode,
},
}

Expand Down

0 comments on commit 8c28d20

Please sign in to comment.