Skip to content

Commit 286741c

Browse files
link: ipvlan: Change flag mode from u16 to bitflag.
default => bridge; 0x01 => private; 0x02 => vepa; Signed-off-by: xujunjie-cover <[email protected]>
1 parent 570c4f2 commit 286741c

File tree

5 files changed

+46
-24
lines changed

5 files changed

+46
-24
lines changed

src/link/link_info/ipvlan.rs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const IFLA_IPVLAN_FLAGS: u16 = 2;
1616
#[non_exhaustive]
1717
pub enum InfoIpVlan {
1818
Mode(IpVlanMode),
19-
Flags(u16),
19+
Flags(IpVlanFlag),
2020
Other(DefaultNla),
2121
}
2222

@@ -33,7 +33,7 @@ impl Nla for InfoIpVlan {
3333
use self::InfoIpVlan::*;
3434
match self {
3535
Mode(value) => NativeEndian::write_u16(buffer, (*value).into()),
36-
Flags(value) => NativeEndian::write_u16(buffer, *value),
36+
Flags(f) => NativeEndian::write_u16(buffer, f.bits()),
3737
Other(nla) => nla.emit_value(buffer),
3838
}
3939
}
@@ -58,10 +58,9 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoIpVlan {
5858
.context("invalid IFLA_IPVLAN_MODE value")?
5959
.into(),
6060
),
61-
IFLA_IPVLAN_FLAGS => Flags(
62-
parse_u16(payload)
63-
.context("invalid IFLA_IPVLAN_FLAGS value")?,
64-
),
61+
IFLA_IPVLAN_FLAGS => Self::Flags(IpVlanFlag::from_bits_retain(
62+
parse_u16(payload).context("failed to parse IPVLAN_FLAG")?,
63+
)),
6564
kind => Other(DefaultNla::parse(buf).context(format!(
6665
"unknown NLA type {kind} for IFLA_INFO_DATA(ipvlan)"
6766
))?),
@@ -73,7 +72,7 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoIpVlan {
7372
#[non_exhaustive]
7473
pub enum InfoIpVtap {
7574
Mode(IpVtapMode),
76-
Flags(u16),
75+
Flags(IpVtapFlag),
7776
Other(DefaultNla),
7877
}
7978

@@ -90,7 +89,7 @@ impl Nla for InfoIpVtap {
9089
use self::InfoIpVtap::*;
9190
match self {
9291
Mode(value) => NativeEndian::write_u16(buffer, (*value).into()),
93-
Flags(value) => NativeEndian::write_u16(buffer, *value),
92+
Flags(f) => NativeEndian::write_u16(buffer, f.bits()),
9493
Other(nla) => nla.emit_value(buffer),
9594
}
9695
}
@@ -115,10 +114,9 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoIpVtap {
115114
.context("invalid IFLA_IPVLAN_MODE value")?
116115
.into(),
117116
),
118-
IFLA_IPVLAN_FLAGS => Flags(
119-
parse_u16(payload)
120-
.context("invalid IFLA_IPVLAN_FLAGS value")?,
121-
),
117+
IFLA_IPVLAN_FLAGS => Self::Flags(IpVtapFlag::from_bits_retain(
118+
parse_u16(payload).context("failed to parse IPVTAP_FLAG")?,
119+
)),
122120
kind => Other(DefaultNla::parse(buf).context(format!(
123121
"unknown NLA type {kind} for IFLA_INFO_DATA(ipvlan)"
124122
))?),
@@ -165,3 +163,24 @@ impl From<IpVlanMode> for u16 {
165163
}
166164
}
167165
}
166+
167+
const IPVLAN_F_PRIVATE: u16 = 0x01;
168+
const IPVLAN_F_VEPA: u16 = 0x02;
169+
170+
bitflags! {
171+
#[non_exhaustive]
172+
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
173+
pub struct IpVlanFlag: u16 {
174+
const PRIVATE = IPVLAN_F_PRIVATE as u16;
175+
const VEPA = IPVLAN_F_VEPA as u16;
176+
const _ = !0;
177+
}
178+
}
179+
180+
impl Default for IpVlanFlag {
181+
fn default() -> Self {
182+
Self::empty()
183+
}
184+
}
185+
186+
pub type IpVtapFlag = IpVlanFlag;

src/link/link_info/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ pub use self::info_data::InfoData;
4545
pub use self::info_port::{InfoPortData, InfoPortKind, InfoVrfPort};
4646
pub use self::infos::{InfoKind, LinkInfo};
4747
pub use self::ipoib::InfoIpoib;
48-
pub use self::ipvlan::{InfoIpVlan, InfoIpVtap, IpVlanMode, IpVtapMode};
48+
pub use self::ipvlan::{
49+
InfoIpVlan, InfoIpVtap, IpVlanFlag, IpVlanMode, IpVtapFlag, IpVtapMode,
50+
};
4951
pub use self::mac_vlan::{InfoMacVlan, InfoMacVtap, MacVlanMode, MacVtapMode};
5052
pub use self::macsec::{
5153
InfoMacSec, MacSecCipherId, MacSecOffload, MacSecValidate,

src/link/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ pub use self::link_info::{
4444
InfoGreTap, InfoGreTap6, InfoGreTun, InfoGreTun6, InfoGtp, InfoHsr,
4545
InfoIpVlan, InfoIpVtap, InfoIpoib, InfoKind, InfoMacSec, InfoMacVlan,
4646
InfoMacVtap, InfoPortData, InfoPortKind, InfoSitTun, InfoTun, InfoVeth,
47-
InfoVlan, InfoVrf, InfoVrfPort, InfoVti, InfoVxlan, InfoXfrm, IpVlanMode,
48-
IpVtapMode, LinkInfo, LinkXstats, MacSecCipherId, MacSecOffload,
49-
MacSecValidate, MacVlanMode, MacVtapMode, MiiStatus, VlanQosMapping,
47+
InfoVlan, InfoVrf, InfoVrfPort, InfoVti, InfoVxlan, InfoXfrm, IpVlanFlag,
48+
IpVlanMode, IpVtapFlag, IpVtapMode, LinkInfo, LinkXstats, MacSecCipherId,
49+
MacSecOffload, MacSecValidate, MacVlanMode, MacVtapMode, MiiStatus,
50+
VlanQosMapping,
5051
};
5152
pub use self::link_layer_type::LinkLayerType;
5253
pub use self::link_state::State;

src/link/tests/ipvlan.rs

Lines changed: 4 additions & 4 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, IpVlanMode, LinkAttribute, LinkHeader,
8-
LinkInfo, LinkLayerType, LinkMessage, LinkMessageBuffer,
7+
InfoData, InfoIpVlan, InfoKind, IpVlanFlag, IpVlanMode, LinkAttribute,
8+
LinkHeader, LinkInfo, LinkLayerType, LinkMessage, LinkMessageBuffer,
99
};
1010
use crate::AddressFamily;
1111

@@ -16,7 +16,7 @@ fn test_ipvlan_link_info() {
1616
0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x12, 0x00, 0x0b, 0x00, 0x01, 0x00,
1717
0x69, 0x70, 0x76, 0x6c, 0x61, 0x6e, 0x00, 0x00, 0x14, 0x00, 0x02, 0x00,
1818
0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x02, 0x00,
19-
0x02, 0x00, 0x00, 0x00,
19+
0x00, 0x00, 0x00, 0x00,
2020
];
2121

2222
let expected = LinkMessage {
@@ -31,7 +31,7 @@ fn test_ipvlan_link_info() {
3131
LinkInfo::Kind(InfoKind::IpVlan),
3232
LinkInfo::Data(InfoData::IpVlan(vec![
3333
InfoIpVlan::Mode(IpVlanMode::L2),
34-
InfoIpVlan::Flags(2),
34+
InfoIpVlan::Flags(IpVlanFlag::default()),
3535
])),
3636
])],
3737
};

src/link/tests/ipvtap.rs

Lines changed: 4 additions & 4 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, InfoIpVtap, InfoKind, IpVtapMode, LinkAttribute, LinkHeader,
8-
LinkInfo, LinkLayerType, LinkMessage, LinkMessageBuffer,
7+
InfoData, InfoIpVtap, InfoKind, IpVtapFlag, IpVtapMode, LinkAttribute,
8+
LinkHeader, LinkInfo, LinkLayerType, LinkMessage, LinkMessageBuffer,
99
};
1010
use crate::AddressFamily;
1111

@@ -16,7 +16,7 @@ fn test_ipvtap_link_info() {
1616
0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x12, 0x00, 0x0b, 0x00, 0x01, 0x00,
1717
0x69, 0x70, 0x76, 0x74, 0x61, 0x70, 0x00, 0x00, 0x14, 0x00, 0x02, 0x00,
1818
0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x02, 0x00,
19-
0x02, 0x00, 0x00, 0x00,
19+
0x01, 0x00, 0x00, 0x00,
2020
];
2121

2222
let expected = LinkMessage {
@@ -31,7 +31,7 @@ fn test_ipvtap_link_info() {
3131
LinkInfo::Kind(InfoKind::IpVtap),
3232
LinkInfo::Data(InfoData::IpVtap(vec![
3333
InfoIpVtap::Mode(IpVtapMode::L2),
34-
InfoIpVtap::Flags(2),
34+
InfoIpVtap::Flags(IpVtapFlag::PRIVATE),
3535
])),
3636
])],
3737
};

0 commit comments

Comments
 (0)