1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// SPDX-License-Identifier: MIT
use anyhow::Context;
use byteorder::{ByteOrder, NativeEndian};
use netlink_packet_utils::{
    nla::{DefaultNla, Nla, NlaBuffer},
    parsers::{parse_i32, parse_u16, parse_u32, parse_u8},
    traits::Parseable,
    DecodeError,
};

const IFLA_BOND_PORT_STATE_ACTIVE: u8 = 0;
const IFLA_BOND_PORT_STATE_BACKUP: u8 = 1;

const IFLA_BOND_PORT_MII_STATUS_UP: u8 = 0;
const IFLA_BOND_PORT_MII_STATUS_GOING_DOWN: u8 = 1;
const IFLA_BOND_PORT_MII_STATUS_DOWN: u8 = 2;
const IFLA_BOND_PORT_MII_STATUS_GOING_BACK: u8 = 3;

const IFLA_BOND_PORT_STATE: u16 = 1;
const IFLA_BOND_PORT_MII_STATUS: u16 = 2;
const IFLA_BOND_PORT_LINK_FAILURE_COUNT: u16 = 3;
const IFLA_BOND_PORT_PERM_HWADDR: u16 = 4;
const IFLA_BOND_PORT_QUEUE_ID: u16 = 5;
// const IFLA_BOND_PORT_AD_AGGREGATOR_ID: u16 = 6;
// const IFLA_BOND_PORT_AD_ACTOR_OPER_PORT_STATE: u16 = 7;
// const IFLA_BOND_PORT_AD_PARTNER_OPER_PORT_STATE: u16 = 8;
const IFLA_BOND_PORT_PRIO: u16 = 9;

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[non_exhaustive]
pub enum BondPortState {
    Active,
    Backup,
    Other(u8),
}

impl From<u8> for BondPortState {
    fn from(value: u8) -> Self {
        use self::BondPortState::*;
        match value {
            IFLA_BOND_PORT_STATE_ACTIVE => Active,
            IFLA_BOND_PORT_STATE_BACKUP => Backup,
            _ => Other(value),
        }
    }
}

impl From<BondPortState> for u8 {
    fn from(value: BondPortState) -> Self {
        use self::BondPortState::*;
        match value {
            Active => IFLA_BOND_PORT_STATE_ACTIVE,
            Backup => IFLA_BOND_PORT_STATE_BACKUP,
            Other(other) => other,
        }
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[non_exhaustive]
pub enum MiiStatus {
    Up,
    GoingDown,
    Down,
    GoingBack,
    Other(u8),
}

impl From<u8> for MiiStatus {
    fn from(value: u8) -> Self {
        use self::MiiStatus::*;
        match value {
            IFLA_BOND_PORT_MII_STATUS_UP => Up,
            IFLA_BOND_PORT_MII_STATUS_GOING_DOWN => GoingDown,
            IFLA_BOND_PORT_MII_STATUS_DOWN => Down,
            IFLA_BOND_PORT_MII_STATUS_GOING_BACK => GoingBack,
            _ => Other(value),
        }
    }
}

impl From<MiiStatus> for u8 {
    fn from(value: MiiStatus) -> Self {
        use self::MiiStatus::*;
        match value {
            Up => IFLA_BOND_PORT_MII_STATUS_UP,
            GoingDown => IFLA_BOND_PORT_MII_STATUS_GOING_DOWN,
            Down => IFLA_BOND_PORT_MII_STATUS_DOWN,
            GoingBack => IFLA_BOND_PORT_MII_STATUS_GOING_BACK,
            Other(other) => other,
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub enum InfoBondPort {
    LinkFailureCount(u32),
    MiiStatus(MiiStatus),
    PermHwaddr(Vec<u8>),
    Prio(i32),
    QueueId(u16),
    BondPortState(BondPortState),
    Other(DefaultNla),
}

impl Nla for InfoBondPort {
    #[rustfmt::skip]
    fn value_len(&self) -> usize {
        use self::InfoBondPort::*;
        match self {
            QueueId(_)
                => 2,
            LinkFailureCount(_) |
            Prio(_)
                => 4,
            PermHwaddr(ref bytes)
            => bytes.len(),
            MiiStatus(_) => 1,
            BondPortState(_) => 1,
            Other(nla)
                => nla.value_len(),
        }
    }

    #[rustfmt::skip]
    fn emit_value(&self, buffer: &mut [u8]) {
        use self::InfoBondPort::*;
        match self {
            QueueId(ref value)
             => NativeEndian::write_u16(buffer, *value),
            PermHwaddr(ref bytes)
             => buffer.copy_from_slice(bytes.as_slice()),
            Prio(ref value)
             => NativeEndian::write_i32(buffer, *value),
            LinkFailureCount(value)
             => NativeEndian::write_u32(buffer, *value),
            MiiStatus(state) => buffer[0] = (*state).into(),
            BondPortState(state) => buffer[0] = (*state).into(),
            Other(nla)
             => nla.emit_value(buffer),
        }
    }

    fn kind(&self) -> u16 {
        use self::InfoBondPort::*;

        match self {
            LinkFailureCount(_) => IFLA_BOND_PORT_LINK_FAILURE_COUNT,
            MiiStatus(_) => IFLA_BOND_PORT_MII_STATUS,
            PermHwaddr(_) => IFLA_BOND_PORT_PERM_HWADDR,
            Prio(_) => IFLA_BOND_PORT_PRIO,
            QueueId(_) => IFLA_BOND_PORT_QUEUE_ID,
            BondPortState(_) => IFLA_BOND_PORT_STATE,
            Other(nla) => nla.kind(),
        }
    }
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoBondPort {
    fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
        use self::InfoBondPort::*;
        let payload = buf.value();
        Ok(match buf.kind() {
            IFLA_BOND_PORT_LINK_FAILURE_COUNT => {
                LinkFailureCount(parse_u32(payload).context(
                    "invalid IFLA_BOND_PORT_LINK_FAILURE_COUNT value",
                )?)
            }
            IFLA_BOND_PORT_MII_STATUS => MiiStatus(
                parse_u8(payload)
                    .context("invalid IFLA_BOND_PORT_MII_STATUS value")?
                    .into(),
            ),
            IFLA_BOND_PORT_PERM_HWADDR => PermHwaddr(payload.to_vec()),
            IFLA_BOND_PORT_PRIO => Prio(
                parse_i32(payload)
                    .context("invalid IFLA_BOND_PORT_PRIO value")?,
            ),
            IFLA_BOND_PORT_QUEUE_ID => QueueId(
                parse_u16(payload)
                    .context("invalid IFLA_BOND_PORT_QUEUE_ID value")?,
            ),
            IFLA_BOND_PORT_STATE => BondPortState(
                parse_u8(payload)
                    .context("invalid IFLA_BOND_PORT_STATE value")?
                    .into(),
            ),
            kind => Other(
                DefaultNla::parse(buf)
                    .context(format!("unknown NLA type {kind}"))?,
            ),
        })
    }
}