Skip to content

Commit

Permalink
link: ipvlan: Change flag mode from u16 to Enum.
Browse files Browse the repository at this point in the history
0 => bridge;
1 => private;
2 => vepa;

Signed-off-by: xujunjie-cover <[email protected]>
  • Loading branch information
xujunjie-cover committed Jul 1, 2024
1 parent 570c4f2 commit ff2164b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 18 deletions.
54 changes: 48 additions & 6 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(IpVlanFlag),
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(value) => NativeEndian::write_u16(buffer, (*value).into()),
Other(nla) => nla.emit_value(buffer),
}
}
Expand All @@ -60,7 +60,8 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoIpVlan {
),
IFLA_IPVLAN_FLAGS => Flags(
parse_u16(payload)
.context("invalid IFLA_IPVLAN_FLAGS value")?,
.context("invalid IFLA_IPVLAN_FLAGS value")?
.into(),
),
kind => Other(DefaultNla::parse(buf).context(format!(
"unknown NLA type {kind} for IFLA_INFO_DATA(ipvlan)"
Expand All @@ -73,7 +74,7 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoIpVlan {
#[non_exhaustive]
pub enum InfoIpVtap {
Mode(IpVtapMode),
Flags(u16),
Flags(IpVtapFlag),
Other(DefaultNla),
}

Expand All @@ -90,7 +91,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(value) => NativeEndian::write_u16(buffer, (*value).into()),
Other(nla) => nla.emit_value(buffer),
}
}
Expand All @@ -117,7 +118,8 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoIpVtap {
),
IFLA_IPVLAN_FLAGS => Flags(
parse_u16(payload)
.context("invalid IFLA_IPVLAN_FLAGS value")?,
.context("invalid IFLA_IPVLAN_FLAGS value")?
.into(),
),
kind => Other(DefaultNla::parse(buf).context(format!(
"unknown NLA type {kind} for IFLA_INFO_DATA(ipvlan)"
Expand Down Expand Up @@ -165,3 +167,43 @@ impl From<IpVlanMode> for u16 {
}
}
}

const IPVLAN_FLAG_BRIDGE: u16 = 0;
const IPVLAN_FLAG_PRIVATE: u16 = 1;
const IPVLAN_FLAG_VEPA: u16 = 2;

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[non_exhaustive]
pub enum IpVlanFlag {
Bridge,
Private,
Vepa,
Other(u16),
}

pub type IpVtapFlag = IpVlanFlag;

impl From<u16> for IpVlanFlag {
fn from(d: u16) -> Self {
match d {
IPVLAN_FLAG_BRIDGE => Self::Bridge,
IPVLAN_FLAG_PRIVATE => Self::Private,
IPVLAN_FLAG_VEPA => Self::Vepa,
_ => {
log::warn!("Unknown IP VLAN flag {}", d);
Self::Other(d)
}
}
}
}

impl From<IpVlanFlag> for u16 {
fn from(v: IpVlanFlag) -> u16 {
match v {
IpVlanFlag::Bridge => IPVLAN_FLAG_BRIDGE,
IpVlanFlag::Private => IPVLAN_FLAG_PRIVATE,
IpVlanFlag::Vepa => IPVLAN_FLAG_VEPA,
IpVlanFlag::Other(d) => d,
}
}
}
4 changes: 3 additions & 1 deletion src/link/link_info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,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, IpVlanFlag, IpVlanMode, IpVtapFlag, 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 @@ -44,9 +44,10 @@ pub use self::link_info::{
InfoGreTap, InfoGreTap6, 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,
InfoVlan, InfoVrf, InfoVrfPort, InfoVti, InfoVxlan, InfoXfrm, IpVlanFlag,
IpVlanMode, IpVtapFlag, 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
8 changes: 4 additions & 4 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, IpVlanFlag, IpVlanMode, LinkAttribute,
LinkHeader, LinkInfo, LinkLayerType, LinkMessage, LinkMessageBuffer,
};
use crate::AddressFamily;

Expand All @@ -16,7 +16,7 @@ fn test_ipvlan_link_info() {
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,
0x02, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
];

let expected = LinkMessage {
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(IpVlanFlag::Bridge),
])),
])],
};
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, IpVtapFlag, 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(IpVtapFlag::Private),
])),
])],
};
Expand Down

0 comments on commit ff2164b

Please sign in to comment.