Skip to content

Commit

Permalink
link: ipvlan: Change flag mode from u16 to bitflag.
Browse files Browse the repository at this point in the history
default => bridge;
0x01 => private;
0x02 => vepa;

Signed-off-by: xujunjie-cover <[email protected]>
  • Loading branch information
xujunjie-cover committed Sep 7, 2024
1 parent c91b2e9 commit 679efae
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 23 deletions.
43 changes: 31 additions & 12 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,9 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoIpVlan {
.context("invalid IFLA_IPVLAN_MODE value")?
.into(),
),
IFLA_IPVLAN_FLAGS => Flags(
parse_u16(payload)
.context("invalid IFLA_IPVLAN_FLAGS value")?,
),
IFLA_IPVLAN_FLAGS => Self::Flags(IpVlanFlags::from_bits_retain(
parse_u16(payload).context("failed to parse IPVLAN_FLAG")?,
)),
kind => Other(DefaultNla::parse(buf).context(format!(
"unknown NLA type {kind} for IFLA_INFO_DATA(ipvlan)"
))?),
Expand All @@ -73,7 +72,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 +89,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 +114,9 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoIpVtap {
.context("invalid IFLA_IPVLAN_MODE value")?
.into(),
),
IFLA_IPVLAN_FLAGS => Flags(
parse_u16(payload)
.context("invalid IFLA_IPVLAN_FLAGS value")?,
),
IFLA_IPVLAN_FLAGS => Self::Flags(IpVtapFlags::from_bits_retain(
parse_u16(payload).context("failed to parse IPVTAP_FLAG")?,
)),
kind => Other(DefaultNla::parse(buf).context(format!(
"unknown NLA type {kind} for IFLA_INFO_DATA(ipvlan)"
))?),
Expand Down Expand Up @@ -165,3 +163,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 @@ -45,9 +45,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
6 changes: 3 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,7 @@ 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),
])),
])],
};
Expand Down
8 changes: 4 additions & 4 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 @@ -16,7 +16,7 @@ fn test_ipvtap_link_info() {
0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x12, 0x00, 0x0b, 0x00, 0x01, 0x00,
0x69, 0x70, 0x76, 0x74, 0x61, 0x70, 0x00, 0x00, 0x14, 0x00, 0x02, 0x00,
0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x02, 0x00,
0x02, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00,
];

let expected = LinkMessage {
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::PRIVATE),
])),
])],
};
Expand Down

0 comments on commit 679efae

Please sign in to comment.