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

link: ipvlan: Change flag mode from u16 to Flag. #125

Merged
merged 2 commits into from
Sep 10, 2024
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
41 changes: 31 additions & 10 deletions src/link/link_info/ipvlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const IFLA_IPVLAN_FLAGS: u16 = 2;
#[non_exhaustive]
pub enum InfoIpVlan {
Mode(IpVlanMode),
Flags(u16),
Flags(IpVlanFlags),
Other(DefaultNla),
}

Expand All @@ -33,7 +33,7 @@ impl Nla for InfoIpVlan {
use self::InfoIpVlan::*;
match self {
Mode(value) => NativeEndian::write_u16(buffer, (*value).into()),
Flags(value) => NativeEndian::write_u16(buffer, *value),
Flags(f) => NativeEndian::write_u16(buffer, f.bits()),
Other(nla) => nla.emit_value(buffer),
}
}
Expand All @@ -58,10 +58,10 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoIpVlan {
.context("invalid IFLA_IPVLAN_MODE value")?
.into(),
),
IFLA_IPVLAN_FLAGS => Flags(
IFLA_IPVLAN_FLAGS => Self::Flags(IpVlanFlags::from_bits_retain(
parse_u16(payload)
.context("invalid IFLA_IPVLAN_FLAGS value")?,
),
.context("failed to parse IFLA_IPVLAN_FLAGS")?,
)),
kind => Other(DefaultNla::parse(buf).context(format!(
"unknown NLA type {kind} for IFLA_INFO_DATA(ipvlan)"
))?),
Expand All @@ -73,7 +73,7 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoIpVlan {
#[non_exhaustive]
pub enum InfoIpVtap {
Mode(IpVtapMode),
Flags(u16),
Flags(IpVtapFlags),
Other(DefaultNla),
}

Expand All @@ -90,7 +90,7 @@ impl Nla for InfoIpVtap {
use self::InfoIpVtap::*;
match self {
Mode(value) => NativeEndian::write_u16(buffer, (*value).into()),
Flags(value) => NativeEndian::write_u16(buffer, *value),
Flags(f) => NativeEndian::write_u16(buffer, f.bits()),
Other(nla) => nla.emit_value(buffer),
}
}
Expand All @@ -115,10 +115,10 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoIpVtap {
.context("invalid IFLA_IPVLAN_MODE value")?
.into(),
),
IFLA_IPVLAN_FLAGS => Flags(
IFLA_IPVLAN_FLAGS => Self::Flags(IpVtapFlags::from_bits_retain(
parse_u16(payload)
.context("invalid IFLA_IPVLAN_FLAGS value")?,
),
.context("failed to parse IFLA_IPVLAN_FLAGS")?,
)),
kind => Other(DefaultNla::parse(buf).context(format!(
"unknown NLA type {kind} for IFLA_INFO_DATA(ipvlan)"
))?),
Expand Down Expand Up @@ -165,3 +165,24 @@ impl From<IpVlanMode> for u16 {
}
}
}

const IPVLAN_F_PRIVATE: u16 = 0x01;
const IPVLAN_F_VEPA: u16 = 0x02;

bitflags! {
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct IpVlanFlags: u16 {
const Private = IPVLAN_F_PRIVATE;
const Vepa = IPVLAN_F_VEPA;
const _ = !0;
}
}

impl Default for IpVlanFlags {
fn default() -> Self {
Self::empty()
}
}

pub type IpVtapFlags = IpVlanFlags;
4 changes: 3 additions & 1 deletion src/link/link_info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ pub use self::info_data::InfoData;
pub use self::info_port::{InfoPortData, InfoPortKind, InfoVrfPort};
pub use self::infos::{InfoKind, LinkInfo};
pub use self::ipoib::InfoIpoib;
pub use self::ipvlan::{InfoIpVlan, InfoIpVtap, IpVlanMode, IpVtapMode};
pub use self::ipvlan::{
InfoIpVlan, InfoIpVtap, IpVlanFlags, IpVlanMode, IpVtapFlags, IpVtapMode,
};
pub use self::mac_vlan::{InfoMacVlan, InfoMacVtap, MacVlanMode, MacVtapMode};
pub use self::macsec::{
InfoMacSec, MacSecCipherId, MacSecOffload, MacSecValidate,
Expand Down
7 changes: 4 additions & 3 deletions src/link/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ pub use self::link_info::{
InfoGreTun, InfoGreTun6, InfoGtp, InfoHsr, InfoIpVlan, InfoIpVtap,
InfoIpoib, InfoKind, InfoMacSec, InfoMacVlan, InfoMacVtap, InfoPortData,
InfoPortKind, InfoSitTun, InfoTun, InfoVeth, InfoVlan, InfoVrf,
InfoVrfPort, InfoVti, InfoVxlan, InfoXfrm, IpVlanMode, IpVtapMode,
LinkInfo, LinkXstats, MacSecCipherId, MacSecOffload, MacSecValidate,
MacVlanMode, MacVtapMode, MiiStatus, VlanQosMapping,
InfoVrfPort, InfoVti, InfoVxlan, InfoXfrm, IpVlanFlags, IpVlanMode,
IpVtapFlags, IpVtapMode, LinkInfo, LinkXstats, MacSecCipherId,
MacSecOffload, MacSecValidate, MacVlanMode, MacVtapMode, MiiStatus,
VlanQosMapping,
};
pub use self::link_layer_type::LinkLayerType;
pub use self::link_state::State;
Expand Down
45 changes: 42 additions & 3 deletions src/link/tests/ipvlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use netlink_packet_utils::{Emitable, Parseable};

use crate::link::link_flag::LinkFlags;
use crate::link::{
InfoData, InfoIpVlan, InfoKind, IpVlanMode, LinkAttribute, LinkHeader,
LinkInfo, LinkLayerType, LinkMessage, LinkMessageBuffer,
InfoData, InfoIpVlan, InfoKind, IpVlanFlags, IpVlanMode, LinkAttribute,
LinkHeader, LinkInfo, LinkLayerType, LinkMessage, LinkMessageBuffer,
};
use crate::AddressFamily;

Expand All @@ -31,7 +31,46 @@ fn test_ipvlan_link_info() {
LinkInfo::Kind(InfoKind::IpVlan),
LinkInfo::Data(InfoData::IpVlan(vec![
InfoIpVlan::Mode(IpVlanMode::L2),
InfoIpVlan::Flags(2),
InfoIpVlan::Flags(IpVlanFlags::Vepa),
])),
])],
};

assert_eq!(
expected,
LinkMessage::parse(&LinkMessageBuffer::new(&raw)).unwrap()
);

let mut buf = vec![0; expected.buffer_len()];

expected.emit(&mut buf);

assert_eq!(buf, raw);
}

#[test]
fn test_ipvlan_bridge_link_info() {
let raw: Vec<u8> = vec![
0x00, 0x00, 0x01, 0x00, 0x12, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x12, 0x00, 0x0b, 0x00, 0x01, 0x00,
0x69, 0x70, 0x76, 0x6c, 0x61, 0x6e, 0x00, 0x00, 0x14, 0x00, 0x02, 0x00,
0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x02, 0x00,
0x00, 0x00, 0x00, 0x00,
xujunjie-cover marked this conversation as resolved.
Show resolved Hide resolved
];

let expected = LinkMessage {
header: LinkHeader {
interface_family: AddressFamily::Unspec,
index: 18,
link_layer_type: LinkLayerType::Ether,
flags: LinkFlags::Broadcast | LinkFlags::Multicast,
change_mask: LinkFlags::empty(),
},
attributes: vec![LinkAttribute::LinkInfo(vec![
LinkInfo::Kind(InfoKind::IpVlan),
LinkInfo::Data(InfoData::IpVlan(vec![
InfoIpVlan::Mode(IpVlanMode::L2),
InfoIpVlan::Flags(IpVlanFlags::default()),
])),
])],
};
Expand Down
6 changes: 3 additions & 3 deletions src/link/tests/ipvtap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use netlink_packet_utils::{Emitable, Parseable};

use crate::link::link_flag::LinkFlags;
use crate::link::{
InfoData, InfoIpVtap, InfoKind, IpVtapMode, LinkAttribute, LinkHeader,
LinkInfo, LinkLayerType, LinkMessage, LinkMessageBuffer,
InfoData, InfoIpVtap, InfoKind, IpVtapFlags, IpVtapMode, LinkAttribute,
LinkHeader, LinkInfo, LinkLayerType, LinkMessage, LinkMessageBuffer,
};
use crate::AddressFamily;

Expand All @@ -31,7 +31,7 @@ fn test_ipvtap_link_info() {
LinkInfo::Kind(InfoKind::IpVtap),
LinkInfo::Data(InfoData::IpVtap(vec![
InfoIpVtap::Mode(IpVtapMode::L2),
InfoIpVtap::Flags(2),
InfoIpVtap::Flags(IpVtapFlags::Vepa),
])),
])],
};
Expand Down
Loading