Skip to content

Commit

Permalink
link: ipvlan: Change mode type from u16 to Enum.
Browse files Browse the repository at this point in the history
Signed-off-by: xujunjie-cover <[email protected]>
  • Loading branch information
xujunjie-cover authored and cathay4t committed Mar 29, 2024
1 parent a543bb7 commit 396d4b0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
46 changes: 43 additions & 3 deletions src/link/link_info/ipvlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand All @@ -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),
}
Expand All @@ -54,7 +54,9 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> 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)
Expand All @@ -66,3 +68,41 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> 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<u16> 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<IpVlanMode> 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,
}
}
}
2 changes: 1 addition & 1 deletion src/link/link_info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/link/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
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, LinkAttribute, LinkHeader, LinkInfo,
LinkLayerType, LinkMessage, LinkMessageBuffer,
InfoData, InfoIpVlan, InfoKind, IpVlanMode, LinkAttribute, LinkHeader,
LinkInfo, LinkLayerType, LinkMessage, LinkMessageBuffer,
};
use crate::AddressFamily;

Expand All @@ -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),
])),
])],
Expand Down

0 comments on commit 396d4b0

Please sign in to comment.