Skip to content

Commit 2e80045

Browse files
link: ipvlan: Change mode type from u16 to Enum.
Signed-off-by: xujunjie-cover <[email protected]>
1 parent a543bb7 commit 2e80045

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

src/link/link_info/ipvlan.rs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const IFLA_IPVLAN_FLAGS: u16 = 2;
1515
#[derive(Debug, PartialEq, Eq, Clone)]
1616
#[non_exhaustive]
1717
pub enum InfoIpVlan {
18-
Mode(u16),
18+
Mode(IpVlanMode),
1919
Flags(u16),
2020
Other(DefaultNla),
2121
}
@@ -32,7 +32,7 @@ impl Nla for InfoIpVlan {
3232
fn emit_value(&self, buffer: &mut [u8]) {
3333
use self::InfoIpVlan::*;
3434
match self {
35-
Mode(value) => NativeEndian::write_u16(buffer, *value),
35+
Mode(value) => NativeEndian::write_u16(buffer, (*value).into()),
3636
Flags(value) => NativeEndian::write_u16(buffer, *value),
3737
Other(nla) => nla.emit_value(buffer),
3838
}
@@ -54,7 +54,9 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoIpVlan {
5454
let payload = buf.value();
5555
Ok(match buf.kind() {
5656
IFLA_IPVLAN_MODE => Mode(
57-
parse_u16(payload).context("invalid IFLA_IPVLAN_MODE value")?,
57+
parse_u16(payload)
58+
.context("invalid IFLA_IPVLAN_MODE value")?
59+
.into(),
5860
),
5961
IFLA_IPVLAN_FLAGS => Flags(
6062
parse_u16(payload)
@@ -66,3 +68,41 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoIpVlan {
6668
})
6769
}
6870
}
71+
72+
const IPVLAN_MODE_L2: u16 = 0;
73+
const IPVLAN_MODE_L3: u16 = 1;
74+
const IPVLAN_MODE_L3S: u16 = 2;
75+
76+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
77+
#[non_exhaustive]
78+
pub enum IpVlanMode {
79+
L2,
80+
L3,
81+
L3S,
82+
Other(u16),
83+
}
84+
85+
impl From<u16> for IpVlanMode {
86+
fn from(d: u16) -> Self {
87+
match d {
88+
IPVLAN_MODE_L2 => Self::L2,
89+
IPVLAN_MODE_L3 => Self::L3,
90+
IPVLAN_MODE_L3S => Self::L3S,
91+
_ => {
92+
log::warn!("Unknown IP VLAN mode {}", d);
93+
Self::Other(d)
94+
}
95+
}
96+
}
97+
}
98+
99+
impl From<IpVlanMode> for u16 {
100+
fn from(v: IpVlanMode) -> u16 {
101+
match v {
102+
IpVlanMode::L2 => IPVLAN_MODE_L2,
103+
IpVlanMode::L3 => IPVLAN_MODE_L3,
104+
IpVlanMode::L3S => IPVLAN_MODE_L3S,
105+
IpVlanMode::Other(d) => d,
106+
}
107+
}
108+
}

src/link/link_info/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub use self::info_data::InfoData;
4545
pub use self::info_port::{InfoPortData, InfoPortKind};
4646
pub use self::infos::{InfoKind, LinkInfo};
4747
pub use self::ipoib::InfoIpoib;
48-
pub use self::ipvlan::InfoIpVlan;
48+
pub use self::ipvlan::{InfoIpVlan, IpVlanMode};
4949
pub use self::mac_vlan::{InfoMacVlan, InfoMacVtap, MacVlanMode, MacVtapMode};
5050
pub use self::macsec::{
5151
InfoMacSec, MacSecCipherId, MacSecOffload, MacSecValidate,

src/link/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub use self::link_info::{
4444
InfoGreTap, InfoGreTap6, InfoGreTun, InfoGreTun6, InfoGtp, InfoHsr,
4545
InfoIpVlan, InfoIpoib, InfoKind, InfoMacSec, InfoMacVlan, InfoMacVtap,
4646
InfoPortData, InfoPortKind, InfoSitTun, InfoTun, InfoVeth, InfoVlan,
47-
InfoVrf, InfoVti, InfoVxlan, InfoXfrm, LinkInfo, LinkXstats,
47+
InfoVrf, InfoVti, InfoVxlan, InfoXfrm, IpVlanMode, LinkInfo, LinkXstats,
4848
MacSecCipherId, MacSecOffload, MacSecValidate, MacVlanMode, MacVtapMode,
4949
MiiStatus, VlanQosMapping,
5050
};

src/link/tests/ipvlan.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use netlink_packet_utils::{Emitable, Parseable};
44

55
use crate::link::link_flag::LinkFlags;
66
use crate::link::{
7-
InfoData, InfoIpVlan, InfoKind, LinkAttribute, LinkHeader, LinkInfo,
8-
LinkLayerType, LinkMessage, LinkMessageBuffer,
7+
InfoData, InfoIpVlan, InfoKind, IpVlanMode, LinkAttribute, LinkHeader,
8+
LinkInfo, LinkLayerType, LinkMessage, LinkMessageBuffer,
99
};
1010
use crate::AddressFamily;
1111

@@ -30,7 +30,7 @@ fn test_ipvlan_link_info() {
3030
attributes: vec![LinkAttribute::LinkInfo(vec![
3131
LinkInfo::Kind(InfoKind::IpVlan),
3232
LinkInfo::Data(InfoData::IpVlan(vec![
33-
InfoIpVlan::Mode(0),
33+
InfoIpVlan::Mode(IpVlanMode::L2),
3434
InfoIpVlan::Flags(2),
3535
])),
3636
])],

0 commit comments

Comments
 (0)