Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove LinkHeader::LinkFlags and add two constants to RouteHeader #76

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/link/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use netlink_packet_utils::{
};

use crate::{
link::{LinkFlag, LinkFlags, LinkLayerType},
link::{link_flag::VecLinkFlag, LinkFlag, LinkLayerType},
AddressFamily,
};

Expand Down Expand Up @@ -67,13 +67,12 @@ pub struct LinkHeader {
/// State of the link, described by a combinations of `IFF_*`
/// constants, for instance `vec![LinkFlag::Up, LinkFlag::LowerUp]`.
/// To convert `Vec<LinkFlag>` into `u32`, you may:
/// `u32::from(&LinkFlags(Vec<LinkFlag>)`
/// `u32::from(&VecLinkFlag(Vec<LinkFlag>)`
/// To convert `u32` to `Vec<LinkFlag>`, you may:
/// `LinkFlags::from(u32).0`
/// `VecLinkFlag::from(u32).0`
pub flags: Vec<LinkFlag>,
/// Change mask for the `flags` field. Reserved, it should be set
/// to u32::MAX or 0(equal to u32::MAX for backwards compatibility).
pub change_mask: u32,
/// Change mask for the `flags` field.
pub change_mask: Vec<LinkFlag>,
}

impl Emitable for LinkHeader {
Expand All @@ -85,9 +84,11 @@ impl Emitable for LinkHeader {
let mut packet = LinkMessageBuffer::new(buffer);
packet.set_interface_family(u8::from(self.interface_family));
packet.set_link_index(self.index);
packet.set_change_mask(self.change_mask);
packet.set_change_mask(u32::from(&VecLinkFlag(
self.change_mask.to_vec(),
)));
packet.set_link_layer_type(u16::from(self.link_layer_type));
packet.set_flags(u32::from(&LinkFlags(self.flags.to_vec())));
packet.set_flags(u32::from(&VecLinkFlag(self.flags.to_vec())));
}
}

Expand All @@ -97,8 +98,8 @@ impl<T: AsRef<[u8]>> Parseable<LinkMessageBuffer<T>> for LinkHeader {
interface_family: buf.interface_family().into(),
link_layer_type: buf.link_layer_type().into(),
index: buf.link_index(),
change_mask: buf.change_mask(),
flags: LinkFlags::from(buf.flags()).0,
change_mask: VecLinkFlag::from(buf.change_mask()).0,
flags: VecLinkFlag::from(buf.flags()).0,
})
}
}
8 changes: 4 additions & 4 deletions src/link/link_flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const IFF_DORMANT: u32 = 1 << 17;
const IFF_ECHO: u32 = 1 << 18;

#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct LinkFlags(pub Vec<LinkFlag>);
pub(crate) struct VecLinkFlag(pub Vec<LinkFlag>);

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[non_exhaustive]
Expand Down Expand Up @@ -155,7 +155,7 @@ const ALL_LINK_FLAGS: [LinkFlag; 19] = [
LinkFlag::Up,
];

impl From<u32> for LinkFlags {
impl From<u32> for VecLinkFlag {
fn from(d: u32) -> Self {
let mut got: u32 = 0;
let mut ret = Vec::new();
Expand All @@ -172,8 +172,8 @@ impl From<u32> for LinkFlags {
}
}

impl From<&LinkFlags> for u32 {
fn from(v: &LinkFlags) -> u32 {
impl From<&VecLinkFlag> for u32 {
fn from(v: &VecLinkFlag) -> u32 {
let mut d: u32 = 0;
for flag in &v.0 {
d += u32::from(*flag);
Expand Down
4 changes: 2 additions & 2 deletions src/link/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod buffer_tool;
mod down_reason;
mod event;
mod header;
mod link_flag;
pub(crate) mod link_flag;
mod link_info;
mod link_layer_type;
mod link_state;
Expand Down Expand Up @@ -34,7 +34,7 @@ pub use self::attribute::LinkAttribute;
pub use self::down_reason::LinkProtocolDownReason;
pub use self::event::LinkEvent;
pub use self::header::{LinkHeader, LinkMessageBuffer};
pub use self::link_flag::{LinkFlag, LinkFlags};
pub use self::link_flag::LinkFlag;
pub use self::link_info::{
BondAdInfo, BondPortState, BridgeQuerierState, HsrProtocol, InfoBond,
InfoBondPort, InfoBridge, InfoData, InfoGreTap, InfoGreTap6, InfoGreTun,
Expand Down
4 changes: 2 additions & 2 deletions src/link/tests/bond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn test_bond_link_info() {
LinkFlag::Running,
LinkFlag::Up,
],
change_mask: 0,
change_mask: vec![],
},
attributes: vec![LinkAttribute::LinkInfo(vec![
LinkInfo::Kind(InfoKind::Bond),
Expand Down Expand Up @@ -123,7 +123,7 @@ fn test_bond_port_link_info() {
LinkFlag::Running,
LinkFlag::Up,
],
change_mask: 0,
change_mask: vec![],
},
attributes: vec![LinkAttribute::LinkInfo(vec![
LinkInfo::Kind(InfoKind::Veth),
Expand Down
2 changes: 1 addition & 1 deletion src/link/tests/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fn test_parse_link_bridge_no_extention_mask() {
LinkFlag::Running,
LinkFlag::Up,
],
change_mask: 0,
change_mask: vec![],
},
attributes: vec![
LinkAttribute::IfName("br0".into()),
Expand Down
2 changes: 1 addition & 1 deletion src/link/tests/hsr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn test_parsing_link_hsr() {
LinkFlag::Running,
LinkFlag::Up,
],
change_mask: 0,
change_mask: vec![],
},
attributes: vec![LinkAttribute::LinkInfo(vec![
LinkInfo::Kind(InfoKind::Hsr),
Expand Down
2 changes: 1 addition & 1 deletion src/link/tests/ipvlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn test_ipvlan_link_info() {
index: 18,
link_layer_type: LinkLayerType::Ether,
flags: vec![LinkFlag::Broadcast, LinkFlag::Multicast],
change_mask: 0,
change_mask: vec![],
},
attributes: vec![LinkAttribute::LinkInfo(vec![
LinkInfo::Kind(InfoKind::IpVlan),
Expand Down
2 changes: 1 addition & 1 deletion src/link/tests/macsec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn test_macsec_link_info() {
LinkFlag::Running,
LinkFlag::Up,
],
change_mask: 0,
change_mask: vec![],
},
attributes: vec![LinkAttribute::LinkInfo(vec![
LinkInfo::Kind(InfoKind::MacSec),
Expand Down
2 changes: 1 addition & 1 deletion src/link/tests/macvlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn test_macvlan_link_info() {
LinkFlag::Running,
LinkFlag::Up,
],
change_mask: 0,
change_mask: vec![],
},
attributes: vec![LinkAttribute::LinkInfo(vec![
LinkInfo::Kind(InfoKind::MacVlan),
Expand Down
26 changes: 12 additions & 14 deletions src/link/tests/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use netlink_packet_utils::traits::{Emitable, ParseableParametrized};

use crate::link::{
LinkAttribute, LinkFlag, LinkFlags, LinkHeader, LinkLayerType, LinkMessage,
LinkAttribute, LinkFlag, LinkHeader, LinkLayerType, LinkMessage,
LinkMessageBuffer, State,
};
use crate::AddressFamily;
Expand Down Expand Up @@ -44,13 +44,11 @@ fn link_message_packet_header_read() {
assert_eq!(packet.link_layer_type(), LinkLayerType::Loopback.into());
assert_eq!(packet.link_index(), 1);
assert_eq!(
LinkFlags::from(packet.flags()).0,
vec![
LinkFlag::Loopback,
LinkFlag::LowerUp,
LinkFlag::Running,
LinkFlag::Up,
]
packet.flags(),
u32::from(LinkFlag::Loopback)
| u32::from(LinkFlag::LowerUp)
| u32::from(LinkFlag::Running)
| u32::from(LinkFlag::Up)
);
assert_eq!(packet.change_mask(), 0);
}
Expand All @@ -64,12 +62,12 @@ fn link_message_packet_header_build() {
packet.set_reserved_1(0);
packet.set_link_layer_type(LinkLayerType::Loopback.into());
packet.set_link_index(1);
packet.set_flags(u32::from(&LinkFlags(vec![
LinkFlag::Loopback,
LinkFlag::LowerUp,
LinkFlag::Running,
LinkFlag::Up,
])));
packet.set_flags(
u32::from(LinkFlag::Loopback)
| u32::from(LinkFlag::LowerUp)
| u32::from(LinkFlag::Running)
| u32::from(LinkFlag::Up),
);
packet.set_change_mask(0);
}
assert_eq!(&buf[..], &LINK_MSG[0..16]);
Expand Down
2 changes: 1 addition & 1 deletion src/link/tests/prop_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn test_wlan0_with_prop_altname() {
LinkFlag::Running,
LinkFlag::Up,
],
change_mask: 0,
change_mask: vec![],
},
attributes: vec![LinkAttribute::PropList(vec![
Prop::AltIfName("wlp0s20f3".to_string()),
Expand Down
2 changes: 1 addition & 1 deletion src/link/tests/veth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn test_veth_get_link_info() {
LinkFlag::Running,
LinkFlag::Up,
],
change_mask: 0,
change_mask: vec![],
},
attributes: vec![LinkAttribute::LinkInfo(vec![LinkInfo::Kind(
InfoKind::Veth,
Expand Down
2 changes: 1 addition & 1 deletion src/link/tests/vlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn test_parsing_link_vlan() {
LinkFlag::Running,
LinkFlag::Up,
],
change_mask: 0,
change_mask: vec![],
},
attributes: vec![LinkAttribute::LinkInfo(vec![
LinkInfo::Kind(InfoKind::Vlan),
Expand Down
2 changes: 1 addition & 1 deletion src/link/tests/vrf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn test_parsing_link_vrf() {
LinkFlag::Running,
LinkFlag::Up,
],
change_mask: 0,
change_mask: vec![],
},
attributes: vec![LinkAttribute::LinkInfo(vec![
LinkInfo::Kind(InfoKind::Vrf),
Expand Down
2 changes: 1 addition & 1 deletion src/link/tests/vxlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn test_parsing_link_vxlan() {
LinkFlag::Running,
LinkFlag::Up,
],
change_mask: 0,
change_mask: vec![],
},
attributes: vec![
LinkAttribute::IfName("vxlan0".into()),
Expand Down
2 changes: 1 addition & 1 deletion src/link/tests/xfrm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn test_parsing_link_xfrm() {
LinkFlag::Running,
LinkFlag::Up,
],
change_mask: 0,
change_mask: vec![],
},
attributes: vec![LinkAttribute::LinkInfo(vec![
LinkInfo::Kind(InfoKind::Xfrm),
Expand Down
5 changes: 5 additions & 0 deletions src/route/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ pub struct RouteHeader {
pub flags: Vec<RouteFlag>,
}

impl RouteHeader {
pub const RT_TABLE_MAIN: u8 = 254;
pub const RT_TABLE_UNSPEC: u8 = 0;
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<RouteMessageBuffer<&'a T>>
for RouteHeader
{
Expand Down