Skip to content

Commit 7be1634

Browse files
mrcrglcathay4t
authored andcommitted
feat: bond primary preselect as enum values closes #81
1 parent 7ef6bf6 commit 7be1634

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

src/link/link_info/bond.rs

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ const BOND_XMIT_POLICY_ENCAP34: u8 = 4;
7979
const BOND_XMIT_POLICY_VLAN_SRCMAC: u8 = 5;
8080
const BOND_OPT_ARP_ALL_TARGETS_ANY: u32 = 0;
8181
const BOND_OPT_ARP_ALL_TARGETS_ALL: u32 = 1;
82+
const BOND_PRI_RESELECT_ALWAYS: u8 = 0;
83+
const BOND_PRI_RESELECT_BETTER: u8 = 1;
84+
const BOND_PRI_RESELECT_FAILURE: u8 = 2;
8285

8386
#[derive(Debug, Clone, Eq, PartialEq)]
8487
#[non_exhaustive]
@@ -280,6 +283,51 @@ impl std::fmt::Display for BondArpValidate {
280283
}
281284
}
282285

286+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
287+
pub enum BondPrimaryReselect {
288+
#[default]
289+
Always,
290+
Better,
291+
Failure,
292+
Other(u8),
293+
}
294+
295+
impl From<BondPrimaryReselect> for u8 {
296+
fn from(value: BondPrimaryReselect) -> Self {
297+
match value {
298+
BondPrimaryReselect::Always => BOND_PRI_RESELECT_ALWAYS,
299+
BondPrimaryReselect::Better => BOND_PRI_RESELECT_BETTER,
300+
BondPrimaryReselect::Failure => BOND_PRI_RESELECT_FAILURE,
301+
BondPrimaryReselect::Other(d) => d,
302+
}
303+
}
304+
}
305+
306+
impl From<u8> for BondPrimaryReselect {
307+
fn from(value: u8) -> Self {
308+
match value {
309+
BOND_PRI_RESELECT_ALWAYS => BondPrimaryReselect::Always,
310+
BOND_PRI_RESELECT_BETTER => BondPrimaryReselect::Better,
311+
BOND_PRI_RESELECT_FAILURE => BondPrimaryReselect::Failure,
312+
d => BondPrimaryReselect::Other(d),
313+
}
314+
}
315+
}
316+
317+
impl std::fmt::Display for BondPrimaryReselect {
318+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
319+
let kernel_name = match self {
320+
BondPrimaryReselect::Always => "always",
321+
BondPrimaryReselect::Better => "better",
322+
BondPrimaryReselect::Failure => "failure",
323+
BondPrimaryReselect::Other(d) => {
324+
return write!(f, "unknown-variant ({d})")
325+
}
326+
};
327+
f.write_str(kernel_name)
328+
}
329+
}
330+
283331
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
284332
pub enum BondXmitHashPolicy {
285333
#[default]
@@ -458,7 +506,7 @@ pub enum InfoBond {
458506
ArpValidate(BondArpValidate),
459507
ArpAllTargets(BondArpAllTargets),
460508
Primary(u32),
461-
PrimaryReselect(u8),
509+
PrimaryReselect(BondPrimaryReselect),
462510
FailOverMac(BondFailOverMac),
463511
XmitHashPolicy(BondXmitHashPolicy),
464512
ResendIgmp(u32),
@@ -526,8 +574,10 @@ impl Nla for InfoBond {
526574
match self {
527575
Self::Mode(value) => buffer[0] = (*value).into(),
528576
Self::XmitHashPolicy(value) => buffer[0] = (*value).into(),
577+
Self::PrimaryReselect(value) => buffer[0] = (*value).into(),
529578
Self::UseCarrier(value)
530-
| Self::PrimaryReselect(value)
579+
| Self::FailOverMac(value)
580+
| Self::XmitHashPolicy(value)
531581
| Self::NumPeerNotif(value)
532582
| Self::AllPortsActive(value)
533583
| Self::AdLacpActive(value)
@@ -667,7 +717,8 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoBond {
667717
),
668718
IFLA_BOND_PRIMARY_RESELECT => Self::PrimaryReselect(
669719
parse_u8(payload)
670-
.context("invalid IFLA_BOND_PRIMARY_RESELECT value")?,
720+
.context("invalid IFLA_BOND_PRIMARY_RESELECT value")?
721+
.into(),
671722
),
672723
IFLA_BOND_FAIL_OVER_MAC => Self::FailOverMac(
673724
parse_u8(payload)

src/link/link_info/mod.rs

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

3131
pub use self::bond::{
3232
BondAdInfo, BondArpAllTargets, BondArpValidate, BondFailOverMac, BondMode,
33-
BondXmitHashPolicy, InfoBond,
33+
BondXmitHashPolicy, InfoBond, BondPrimaryReselect
3434
};
3535
pub use self::bond_port::{BondPortState, InfoBondPort, MiiStatus};
3636
pub use self::bridge::{

src/link/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub use self::link_info::{
4949
InfoVeth, InfoVlan, InfoVrf, InfoVrfPort, InfoVti, InfoVxlan, InfoXfrm,
5050
IpVlanFlags, IpVlanMode, IpVtapFlags, IpVtapMode, LinkInfo, LinkXstats,
5151
MacSecCipherId, MacSecOffload, MacSecValidate, MacVlanMode, MacVtapMode,
52-
MiiStatus, VlanQosMapping,
52+
MiiStatus, VlanQosMapping,BondPrimaryReselect
5353
};
5454
pub use self::link_layer_type::LinkLayerType;
5555
pub use self::link_state::State;

src/link/tests/bond.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::link::{
77
BondArpAllTargets, BondArpValidate, BondFailOverMac, BondMode,
88
BondPortState, BondXmitHashPolicy, InfoBond, InfoBondPort, InfoData,
99
InfoKind, InfoPortData, InfoPortKind, LinkAttribute, LinkHeader, LinkInfo,
10-
LinkLayerType, LinkMessage, LinkMessageBuffer, Map, MiiStatus, State,
10+
LinkLayerType, LinkMessage, LinkMessageBuffer, Map, MiiStatus, State, BondPrimaryReselect
1111
};
1212
use crate::{AddressFamily, RouteNetlinkMessage};
1313

@@ -66,7 +66,7 @@ fn test_bond_link_info() {
6666
InfoBond::ArpInterval(0),
6767
InfoBond::ArpValidate(BondArpValidate::None),
6868
InfoBond::ArpAllTargets(BondArpAllTargets::Any),
69-
InfoBond::PrimaryReselect(0),
69+
InfoBond::PrimaryReselect(BondPrimaryReselect::Always),
7070
InfoBond::FailOverMac(BondFailOverMac::None),
7171
InfoBond::XmitHashPolicy(BondXmitHashPolicy::Layer2),
7272
InfoBond::ResendIgmp(1),

0 commit comments

Comments
 (0)