Skip to content

Commit 507ea73

Browse files
mrcrglcathay4t
authored andcommitted
feat: InfoBond::ArpAllTargets(_) as enum values closes #79
1 parent a285aba commit 507ea73

File tree

4 files changed

+59
-10
lines changed

4 files changed

+59
-10
lines changed

src/link/link_info/bond.rs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ const BOND_ARP_VALIDATE_ALL: u32 =
7171
const BOND_ARP_FILTER: u32 = BOND_ARP_VALIDATE_ALL + 1;
7272
const BOND_ARP_FILTER_ACTIVE: u32 = BOND_ARP_FILTER | BOND_ARP_VALIDATE_ACTIVE;
7373
const BOND_ARP_FILTER_BACKUP: u32 = BOND_ARP_FILTER | BOND_ARP_VALIDATE_BACKUP;
74+
const BOND_OPT_ARP_ALL_TARGETS_ANY: u32 = 0;
75+
const BOND_OPT_ARP_ALL_TARGETS_ALL: u32 = 1;
7476

7577
#[derive(Debug, Clone, Eq, PartialEq)]
7678
#[non_exhaustive]
@@ -272,6 +274,47 @@ impl std::fmt::Display for BondArpValidate {
272274
}
273275
}
274276

277+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
278+
pub enum BondArpAllTargets {
279+
#[default]
280+
Any,
281+
All,
282+
Other(u32),
283+
}
284+
285+
impl From<BondArpAllTargets> for u32 {
286+
fn from(value: BondArpAllTargets) -> Self {
287+
match value {
288+
BondArpAllTargets::All => BOND_OPT_ARP_ALL_TARGETS_ALL,
289+
BondArpAllTargets::Any => BOND_OPT_ARP_ALL_TARGETS_ANY,
290+
BondArpAllTargets::Other(d) => d,
291+
}
292+
}
293+
}
294+
295+
impl From<u32> for BondArpAllTargets {
296+
fn from(value: u32) -> Self {
297+
match value {
298+
BOND_OPT_ARP_ALL_TARGETS_ANY => BondArpAllTargets::Any,
299+
BOND_OPT_ARP_ALL_TARGETS_ALL => BondArpAllTargets::All,
300+
d => BondArpAllTargets::Other(d),
301+
}
302+
}
303+
}
304+
305+
impl std::fmt::Display for BondArpAllTargets {
306+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
307+
let kernel_name = match self {
308+
BondArpAllTargets::Any => "any",
309+
BondArpAllTargets::All => "all",
310+
BondArpAllTargets::Other(d) => {
311+
return write!(f, "unknown-variant ({d})")
312+
}
313+
};
314+
f.write_str(kernel_name)
315+
}
316+
}
317+
275318
// Some attributes (ARP_IP_TARGET, NS_IP6_TARGET) contain a nested
276319
// list of IP addresses, where each element uses the index as NLA kind
277320
// and the address as value. InfoBond exposes vectors of IP addresses,
@@ -350,7 +393,7 @@ pub enum InfoBond {
350393
ArpInterval(u32),
351394
ArpIpTarget(Vec<Ipv4Addr>),
352395
ArpValidate(BondArpValidate),
353-
ArpAllTargets(u32),
396+
ArpAllTargets(BondArpAllTargets),
354397
Primary(u32),
355398
PrimaryReselect(u8),
356399
FailOverMac(u8),
@@ -436,12 +479,14 @@ impl Nla for InfoBond {
436479
Self::ArpValidate(value) => {
437480
NativeEndian::write_u32(buffer, (*value).into())
438481
}
482+
Self::ArpAllTargets(value) => {
483+
NativeEndian::write_u32(buffer, (*value).into())
484+
}
439485
Self::ActivePort(value)
440486
| Self::MiiMon(value)
441487
| Self::UpDelay(value)
442488
| Self::DownDelay(value)
443489
| Self::ArpInterval(value)
444-
| Self::ArpAllTargets(value)
445490
| Self::Primary(value)
446491
| Self::ResendIgmp(value)
447492
| Self::MinLinks(value)
@@ -550,7 +595,8 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoBond {
550595
),
551596
IFLA_BOND_ARP_ALL_TARGETS => Self::ArpAllTargets(
552597
parse_u32(payload)
553-
.context("invalid IFLA_BOND_ARP_ALL_TARGETS value")?,
598+
.context("invalid IFLA_BOND_ARP_ALL_TARGETS value")?
599+
.into(),
554600
),
555601
IFLA_BOND_PRIMARY => Self::Primary(
556602
parse_u32(payload)

src/link/link_info/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ mod vxlan;
2828
mod xfrm;
2929
mod xstats;
3030

31-
pub use self::bond::{BondAdInfo, BondArpValidate, BondMode, InfoBond};
31+
pub use self::bond::{
32+
BondAdInfo, BondArpAllTargets, BondArpValidate, BondMode, InfoBond,
33+
};
3234
pub use self::bond_port::{BondPortState, InfoBondPort, MiiStatus};
3335
pub use self::bridge::{
3436
BridgeId, BridgeIdBuffer, BridgeQuerierState, InfoBridge,

src/link/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ pub use self::ext_mask::LinkExtentMask;
3939
pub use self::header::{LinkHeader, LinkMessageBuffer};
4040
pub use self::link_flag::LinkFlags;
4141
pub use self::link_info::{
42-
BondAdInfo, BondArpValidate, BondMode, BondPortState, BridgeId,
43-
BridgeIdBuffer, BridgePortMulticastRouter, BridgePortState,
42+
BondAdInfo, BondArpAllTargets, BondArpValidate, BondMode, BondPortState,
43+
BridgeId, BridgeIdBuffer, BridgePortMulticastRouter, BridgePortState,
4444
BridgeQuerierState, GeneveDf, HsrProtocol, InfoBond, InfoBondPort,
4545
InfoBridge, InfoBridgePort, InfoData, InfoGeneve, InfoGreTap, InfoGreTap6,
4646
InfoGreTun, InfoGreTun6, InfoGtp, InfoHsr, InfoIpVlan, InfoIpVtap,

src/link/tests/bond.rs

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

55
use crate::link::link_flag::LinkFlags;
66
use crate::link::{
7-
BondArpValidate, BondMode, BondPortState, InfoBond, InfoBondPort, InfoData,
8-
InfoKind, InfoPortData, InfoPortKind, LinkAttribute, LinkHeader, LinkInfo,
9-
LinkLayerType, LinkMessage, LinkMessageBuffer, Map, MiiStatus, State,
7+
BondArpAllTargets, BondArpValidate, BondMode, BondPortState, InfoBond,
8+
InfoBondPort, InfoData, InfoKind, InfoPortData, InfoPortKind,
9+
LinkAttribute, LinkHeader, LinkInfo, LinkLayerType, LinkMessage,
10+
LinkMessageBuffer, Map, MiiStatus, State,
1011
};
1112
use crate::{AddressFamily, RouteNetlinkMessage};
1213

@@ -64,7 +65,7 @@ fn test_bond_link_info() {
6465
InfoBond::UseCarrier(1),
6566
InfoBond::ArpInterval(0),
6667
InfoBond::ArpValidate(BondArpValidate::None),
67-
InfoBond::ArpAllTargets(0),
68+
InfoBond::ArpAllTargets(BondArpAllTargets::Any),
6869
InfoBond::PrimaryReselect(0),
6970
InfoBond::FailOverMac(0),
7071
InfoBond::XmitHashPolicy(0),

0 commit comments

Comments
 (0)