From 2e800450912badaf9ddb3b030b8a608f32aad20c Mon Sep 17 00:00:00 2001 From: xujunjie-cover Date: Fri, 29 Mar 2024 16:50:21 +0800 Subject: [PATCH] link: ipvlan: Change mode type from u16 to Enum. Signed-off-by: xujunjie-cover --- src/link/link_info/ipvlan.rs | 46 +++++++++++++++++++++++++++++++++--- src/link/link_info/mod.rs | 2 +- src/link/mod.rs | 2 +- src/link/tests/ipvlan.rs | 6 ++--- 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/link/link_info/ipvlan.rs b/src/link/link_info/ipvlan.rs index 384427e5..c103307f 100644 --- a/src/link/link_info/ipvlan.rs +++ b/src/link/link_info/ipvlan.rs @@ -15,7 +15,7 @@ const IFLA_IPVLAN_FLAGS: u16 = 2; #[derive(Debug, PartialEq, Eq, Clone)] #[non_exhaustive] pub enum InfoIpVlan { - Mode(u16), + Mode(IpVlanMode), Flags(u16), Other(DefaultNla), } @@ -32,7 +32,7 @@ impl Nla for InfoIpVlan { fn emit_value(&self, buffer: &mut [u8]) { use self::InfoIpVlan::*; match self { - Mode(value) => NativeEndian::write_u16(buffer, *value), + Mode(value) => NativeEndian::write_u16(buffer, (*value).into()), Flags(value) => NativeEndian::write_u16(buffer, *value), Other(nla) => nla.emit_value(buffer), } @@ -54,7 +54,9 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> for InfoIpVlan { let payload = buf.value(); Ok(match buf.kind() { IFLA_IPVLAN_MODE => Mode( - parse_u16(payload).context("invalid IFLA_IPVLAN_MODE value")?, + parse_u16(payload) + .context("invalid IFLA_IPVLAN_MODE value")? + .into(), ), IFLA_IPVLAN_FLAGS => Flags( parse_u16(payload) @@ -66,3 +68,41 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> for InfoIpVlan { }) } } + +const IPVLAN_MODE_L2: u16 = 0; +const IPVLAN_MODE_L3: u16 = 1; +const IPVLAN_MODE_L3S: u16 = 2; + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +#[non_exhaustive] +pub enum IpVlanMode { + L2, + L3, + L3S, + Other(u16), +} + +impl From for IpVlanMode { + fn from(d: u16) -> Self { + match d { + IPVLAN_MODE_L2 => Self::L2, + IPVLAN_MODE_L3 => Self::L3, + IPVLAN_MODE_L3S => Self::L3S, + _ => { + log::warn!("Unknown IP VLAN mode {}", d); + Self::Other(d) + } + } + } +} + +impl From for u16 { + fn from(v: IpVlanMode) -> u16 { + match v { + IpVlanMode::L2 => IPVLAN_MODE_L2, + IpVlanMode::L3 => IPVLAN_MODE_L3, + IpVlanMode::L3S => IPVLAN_MODE_L3S, + IpVlanMode::Other(d) => d, + } + } +} diff --git a/src/link/link_info/mod.rs b/src/link/link_info/mod.rs index e2bd9c9b..4bc1bbdf 100644 --- a/src/link/link_info/mod.rs +++ b/src/link/link_info/mod.rs @@ -45,7 +45,7 @@ pub use self::info_data::InfoData; pub use self::info_port::{InfoPortData, InfoPortKind}; pub use self::infos::{InfoKind, LinkInfo}; pub use self::ipoib::InfoIpoib; -pub use self::ipvlan::InfoIpVlan; +pub use self::ipvlan::{InfoIpVlan, IpVlanMode}; pub use self::mac_vlan::{InfoMacVlan, InfoMacVtap, MacVlanMode, MacVtapMode}; pub use self::macsec::{ InfoMacSec, MacSecCipherId, MacSecOffload, MacSecValidate, diff --git a/src/link/mod.rs b/src/link/mod.rs index 3ace2a1c..8aac457b 100644 --- a/src/link/mod.rs +++ b/src/link/mod.rs @@ -44,7 +44,7 @@ pub use self::link_info::{ InfoGreTap, InfoGreTap6, InfoGreTun, InfoGreTun6, InfoGtp, InfoHsr, InfoIpVlan, InfoIpoib, InfoKind, InfoMacSec, InfoMacVlan, InfoMacVtap, InfoPortData, InfoPortKind, InfoSitTun, InfoTun, InfoVeth, InfoVlan, - InfoVrf, InfoVti, InfoVxlan, InfoXfrm, LinkInfo, LinkXstats, + InfoVrf, InfoVti, InfoVxlan, InfoXfrm, IpVlanMode, LinkInfo, LinkXstats, MacSecCipherId, MacSecOffload, MacSecValidate, MacVlanMode, MacVtapMode, MiiStatus, VlanQosMapping, }; diff --git a/src/link/tests/ipvlan.rs b/src/link/tests/ipvlan.rs index db0f0696..b3eaa7fe 100644 --- a/src/link/tests/ipvlan.rs +++ b/src/link/tests/ipvlan.rs @@ -4,8 +4,8 @@ use netlink_packet_utils::{Emitable, Parseable}; use crate::link::link_flag::LinkFlags; use crate::link::{ - InfoData, InfoIpVlan, InfoKind, LinkAttribute, LinkHeader, LinkInfo, - LinkLayerType, LinkMessage, LinkMessageBuffer, + InfoData, InfoIpVlan, InfoKind, IpVlanMode, LinkAttribute, LinkHeader, + LinkInfo, LinkLayerType, LinkMessage, LinkMessageBuffer, }; use crate::AddressFamily; @@ -30,7 +30,7 @@ fn test_ipvlan_link_info() { attributes: vec![LinkAttribute::LinkInfo(vec![ LinkInfo::Kind(InfoKind::IpVlan), LinkInfo::Data(InfoData::IpVlan(vec![ - InfoIpVlan::Mode(0), + InfoIpVlan::Mode(IpVlanMode::L2), InfoIpVlan::Flags(2), ])), ])],