Skip to content

Commit 4fb21fc

Browse files
link: ipvlan: Change flag mode from u16 to bitflag.
default => bridge; 0x01 => private; 0x02 => vepa; Signed-off-by: xujunjie-cover <xujunjielxx@163.com>
1 parent c91b2e9 commit 4fb21fc

File tree

5 files changed

+83
-22
lines changed

5 files changed

+83
-22
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(IpVlanFlags),
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(IpVlanFlags::from_bits_retain(
62+
parse_u16(payload).context("failed to parse IFLA_IPVLAN_FLAGS")?,
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(IpVtapFlags),
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(IpVtapFlags::from_bits_retain(
118+
parse_u16(payload).context("failed to parse IFLA_IPVLAN_FLAGS")?,
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 IpVlanFlags: u16 {
174+
const Private = IPVLAN_F_PRIVATE;
175+
const Vepa = IPVLAN_F_VEPA;
176+
const _ = !0;
177+
}
178+
}
179+
180+
impl Default for IpVlanFlags {
181+
fn default() -> Self {
182+
Self::empty()
183+
}
184+
}
185+
186+
pub type IpVtapFlags = IpVlanFlags;

src/link/link_info/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ pub use self::info_data::InfoData;
4747
pub use self::info_port::{InfoPortData, InfoPortKind, InfoVrfPort};
4848
pub use self::infos::{InfoKind, LinkInfo};
4949
pub use self::ipoib::InfoIpoib;
50-
pub use self::ipvlan::{InfoIpVlan, InfoIpVtap, IpVlanMode, IpVtapMode};
50+
pub use self::ipvlan::{
51+
InfoIpVlan, InfoIpVtap, IpVlanFlags, IpVlanMode, IpVtapFlags, IpVtapMode,
52+
};
5153
pub use self::mac_vlan::{InfoMacVlan, InfoMacVtap, MacVlanMode, MacVtapMode};
5254
pub use self::macsec::{
5355
InfoMacSec, MacSecCipherId, MacSecOffload, MacSecValidate,

src/link/mod.rs

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

src/link/tests/ipvlan.rs

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

@@ -31,7 +31,46 @@ 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(IpVlanFlags::Vepa),
35+
])),
36+
])],
37+
};
38+
39+
assert_eq!(
40+
expected,
41+
LinkMessage::parse(&LinkMessageBuffer::new(&raw)).unwrap()
42+
);
43+
44+
let mut buf = vec![0; expected.buffer_len()];
45+
46+
expected.emit(&mut buf);
47+
48+
assert_eq!(buf, raw);
49+
}
50+
51+
#[test]
52+
fn test_ipvlan_bridge_link_info() {
53+
let raw: Vec<u8> = vec![
54+
0x00, 0x00, 0x01, 0x00, 0x12, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00,
55+
0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x12, 0x00, 0x0b, 0x00, 0x01, 0x00,
56+
0x69, 0x70, 0x76, 0x6c, 0x61, 0x6e, 0x00, 0x00, 0x14, 0x00, 0x02, 0x00,
57+
0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x02, 0x00,
58+
0x00, 0x00, 0x00, 0x00,
59+
];
60+
61+
let expected = LinkMessage {
62+
header: LinkHeader {
63+
interface_family: AddressFamily::Unspec,
64+
index: 18,
65+
link_layer_type: LinkLayerType::Ether,
66+
flags: LinkFlags::Broadcast | LinkFlags::Multicast,
67+
change_mask: LinkFlags::empty(),
68+
},
69+
attributes: vec![LinkAttribute::LinkInfo(vec![
70+
LinkInfo::Kind(InfoKind::IpVlan),
71+
LinkInfo::Data(InfoData::IpVlan(vec![
72+
InfoIpVlan::Mode(IpVlanMode::L2),
73+
InfoIpVlan::Flags(IpVlanFlags::default()),
3574
])),
3675
])],
3776
};

src/link/tests/ipvtap.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, InfoIpVtap, InfoKind, IpVtapMode, LinkAttribute, LinkHeader,
8-
LinkInfo, LinkLayerType, LinkMessage, LinkMessageBuffer,
7+
InfoData, InfoIpVtap, InfoKind, IpVtapFlags, IpVtapMode, LinkAttribute,
8+
LinkHeader, LinkInfo, LinkLayerType, LinkMessage, LinkMessageBuffer,
99
};
1010
use crate::AddressFamily;
1111

@@ -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(IpVtapFlags::Vepa),
3535
])),
3636
])],
3737
};

0 commit comments

Comments
 (0)