Skip to content

Commit 3270863

Browse files
mrcrglcathay4t
authored andcommitted
feat: failovermac as enum values closes #82
1 parent a54fe4c commit 3270863

File tree

4 files changed

+69
-19
lines changed

4 files changed

+69
-19
lines changed

src/link/link_info/bond.rs

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ pub enum InfoBond {
459459
ArpAllTargets(BondArpAllTargets),
460460
Primary(u32),
461461
PrimaryReselect(u8),
462-
FailOverMac(u8),
462+
FailOverMac(BondFailOverMac),
463463
XmitHashPolicy(BondXmitHashPolicy),
464464
ResendIgmp(u32),
465465
NumPeerNotif(u8),
@@ -528,14 +528,14 @@ impl Nla for InfoBond {
528528
Self::XmitHashPolicy(value) => buffer[0] = (*value).into(),
529529
Self::UseCarrier(value)
530530
| Self::PrimaryReselect(value)
531-
| Self::FailOverMac(value)
532531
| Self::NumPeerNotif(value)
533532
| Self::AllPortsActive(value)
534533
| Self::AdLacpActive(value)
535534
| Self::AdLacpRate(value)
536535
| Self::AdSelect(value)
537536
| Self::TlbDynamicLb(value)
538537
| Self::MissedMax(value) => buffer[0] = *value,
538+
Self::FailOverMac(value) => buffer[0] = (*value).into(),
539539
Self::AdActorSysPrio(value) | Self::AdUserPortKey(value) => {
540540
NativeEndian::write_u16(buffer, *value)
541541
}
@@ -671,7 +671,8 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoBond {
671671
),
672672
IFLA_BOND_FAIL_OVER_MAC => Self::FailOverMac(
673673
parse_u8(payload)
674-
.context("invalid IFLA_BOND_FAIL_OVER_MAC value")?,
674+
.context("invalid IFLA_BOND_FAIL_OVER_MAC value")?
675+
.into(),
675676
),
676677
IFLA_BOND_XMIT_HASH_POLICY => Self::XmitHashPolicy(
677678
parse_u8(payload)
@@ -766,3 +767,52 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoBond {
766767
})
767768
}
768769
}
770+
771+
const BOND_FOM_NONE: u8 = 0;
772+
const BOND_FOM_ACTIVE: u8 = 1;
773+
const BOND_FOM_FOLLOW: u8 = 2;
774+
775+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
776+
pub enum BondFailOverMac {
777+
#[default]
778+
None,
779+
Active,
780+
Follow,
781+
Other(u8),
782+
}
783+
784+
impl From<BondFailOverMac> for u8 {
785+
fn from(value: BondFailOverMac) -> Self {
786+
match value {
787+
BondFailOverMac::None => BOND_FOM_NONE,
788+
BondFailOverMac::Active => BOND_FOM_ACTIVE,
789+
BondFailOverMac::Follow => BOND_FOM_FOLLOW,
790+
BondFailOverMac::Other(d) => d,
791+
}
792+
}
793+
}
794+
795+
impl From<u8> for BondFailOverMac {
796+
fn from(value: u8) -> Self {
797+
match value {
798+
BOND_FOM_NONE => BondFailOverMac::None,
799+
BOND_FOM_ACTIVE => BondFailOverMac::Active,
800+
BOND_FOM_FOLLOW => BondFailOverMac::Follow,
801+
d => BondFailOverMac::Other(d),
802+
}
803+
}
804+
}
805+
806+
impl std::fmt::Display for BondFailOverMac {
807+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
808+
let kernel_name = match self {
809+
BondFailOverMac::None => "none",
810+
BondFailOverMac::Active => "active",
811+
BondFailOverMac::Follow => "follow",
812+
BondFailOverMac::Other(d) => {
813+
return write!(f, "unknown-variant ({d})")
814+
}
815+
};
816+
f.write_str(kernel_name)
817+
}
818+
}

src/link/link_info/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ mod xfrm;
2929
mod xstats;
3030

3131
pub use self::bond::{
32-
BondAdInfo, BondArpAllTargets, BondArpValidate, BondMode,
32+
BondAdInfo, BondArpAllTargets, BondArpValidate, BondFailOverMac, BondMode,
3333
BondXmitHashPolicy, InfoBond,
3434
};
3535
pub use self::bond_port::{BondPortState, InfoBondPort, MiiStatus};

src/link/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ 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, BondArpAllTargets, BondArpValidate, BondMode, BondPortState,
43-
BondXmitHashPolicy, BridgeId, BridgeIdBuffer, BridgePortMulticastRouter,
44-
BridgePortState, BridgeQuerierState, GeneveDf, HsrProtocol, InfoBond,
45-
InfoBondPort, InfoBridge, InfoBridgePort, InfoData, InfoGeneve, InfoGreTap,
46-
InfoGreTap6, InfoGreTun, InfoGreTun6, InfoGtp, InfoHsr, InfoIpVlan,
47-
InfoIpVtap, InfoIpoib, InfoKind, InfoMacSec, InfoMacVlan, InfoMacVtap,
48-
InfoPortData, InfoPortKind, InfoSitTun, InfoTun, InfoVeth, InfoVlan,
49-
InfoVrf, InfoVrfPort, InfoVti, InfoVxlan, InfoXfrm, IpVlanFlags,
50-
IpVlanMode, IpVtapFlags, IpVtapMode, LinkInfo, LinkXstats, MacSecCipherId,
51-
MacSecOffload, MacSecValidate, MacVlanMode, MacVtapMode, MiiStatus,
52-
VlanQosMapping,
42+
BondAdInfo, BondArpAllTargets, BondArpValidate, BondFailOverMac, BondMode,
43+
BondPortState, BondXmitHashPolicy, BridgeId, BridgeIdBuffer,
44+
BridgePortMulticastRouter, BridgePortState, BridgeQuerierState, GeneveDf,
45+
HsrProtocol, InfoBond, InfoBondPort, InfoBridge, InfoBridgePort, InfoData,
46+
InfoGeneve, InfoGreTap, InfoGreTap6, InfoGreTun, InfoGreTun6, InfoGtp,
47+
InfoHsr, InfoIpVlan, InfoIpVtap, InfoIpoib, InfoKind, InfoMacSec,
48+
InfoMacVlan, InfoMacVtap, InfoPortData, InfoPortKind, InfoSitTun, InfoTun,
49+
InfoVeth, InfoVlan, InfoVrf, InfoVrfPort, InfoVti, InfoVxlan, InfoXfrm,
50+
IpVlanFlags, IpVlanMode, IpVtapFlags, IpVtapMode, LinkInfo, LinkXstats,
51+
MacSecCipherId, MacSecOffload, MacSecValidate, MacVlanMode, MacVtapMode,
52+
MiiStatus, VlanQosMapping,
5353
};
5454
pub use self::link_layer_type::LinkLayerType;
5555
pub use self::link_state::State;

src/link/tests/bond.rs

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

55
use crate::link::link_flag::LinkFlags;
66
use crate::link::{
7-
BondArpAllTargets, BondArpValidate, BondMode, BondPortState,
8-
BondXmitHashPolicy, InfoBond, InfoBondPort, InfoData, InfoKind,
9-
InfoPortData, InfoPortKind, LinkAttribute, LinkHeader, LinkInfo,
7+
BondArpAllTargets, BondArpValidate, BondFailOverMac, BondMode,
8+
BondPortState, BondXmitHashPolicy, InfoBond, InfoBondPort, InfoData,
9+
InfoKind, InfoPortData, InfoPortKind, LinkAttribute, LinkHeader, LinkInfo,
1010
LinkLayerType, LinkMessage, LinkMessageBuffer, Map, MiiStatus, State,
1111
};
1212
use crate::{AddressFamily, RouteNetlinkMessage};
@@ -67,7 +67,7 @@ fn test_bond_link_info() {
6767
InfoBond::ArpValidate(BondArpValidate::None),
6868
InfoBond::ArpAllTargets(BondArpAllTargets::Any),
6969
InfoBond::PrimaryReselect(0),
70-
InfoBond::FailOverMac(0),
70+
InfoBond::FailOverMac(BondFailOverMac::None),
7171
InfoBond::XmitHashPolicy(BondXmitHashPolicy::Layer2),
7272
InfoBond::ResendIgmp(1),
7373
InfoBond::NumPeerNotif(1),

0 commit comments

Comments
 (0)