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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// SPDX-License-Identifier: MIT

use anyhow::Context;
use netlink_packet_utils::{
    nla::{DefaultNla, Nla, NlaBuffer, NlasIterator},
    DecodeError, Emitable, Parseable,
};

use crate::link::{
    VfInfoBroadcast, VfInfoBroadcastBuffer, VfInfoGuid, VfInfoGuidBuffer,
    VfInfoLinkState, VfInfoLinkStateBuffer, VfInfoMac, VfInfoMacBuffer,
    VfInfoRate, VfInfoRateBuffer, VfInfoRssQueryEn, VfInfoRssQueryEnBuffer,
    VfInfoSpoofCheck, VfInfoSpoofCheckBuffer, VfInfoTrust, VfInfoTrustBuffer,
    VfInfoTxRate, VfInfoTxRateBuffer, VfInfoVlan, VfInfoVlanBuffer, VfStats,
    VfVlan,
};

const IFLA_VF_INFO: u16 = 1;

#[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) struct VecLinkVfInfo(pub(crate) Vec<LinkVfInfo>);

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
    for VecLinkVfInfo
{
    fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
        let mut nlas = vec![];
        for nla in NlasIterator::new(buf.into_inner()) {
            let nla = &nla.context(format!(
                "invalid IFLA_VFINFO_LIST value: {:?}",
                buf.value()
            ))?;
            if nla.kind() == IFLA_VF_INFO {
                nlas.push(LinkVfInfo::parse(&NlaBuffer::new_checked(
                    nla.value(),
                )?)?);
            } else {
                log::warn!(
                    "BUG: Expecting IFLA_VF_INFO in IFLA_VFINFO_LIST, \
                    but got {}",
                    nla.kind()
                );
            }
        }
        Ok(Self(nlas))
    }
}

#[derive(Debug, Clone, Default, Eq, PartialEq)]
pub struct LinkVfInfo(pub Vec<VfInfo>);

impl Nla for LinkVfInfo {
    fn value_len(&self) -> usize {
        self.0.as_slice().buffer_len()
    }

    fn emit_value(&self, buffer: &mut [u8]) {
        self.0.as_slice().emit(buffer)
    }

    fn kind(&self) -> u16 {
        IFLA_VF_INFO
    }
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for LinkVfInfo {
    fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
        let mut nlas = vec![];
        for nla in NlasIterator::new(buf.into_inner()) {
            let nla = &nla.context(format!(
                "invalid IFLA_VF_INFO value {:?}",
                buf.value()
            ))?;
            nlas.push(VfInfo::parse(nla)?);
        }
        Ok(Self(nlas))
    }
}

const IFLA_VF_MAC: u16 = 1;
const IFLA_VF_VLAN: u16 = 2;
const IFLA_VF_TX_RATE: u16 = 3;
const IFLA_VF_SPOOFCHK: u16 = 4;
const IFLA_VF_LINK_STATE: u16 = 5;
const IFLA_VF_RATE: u16 = 6;
const IFLA_VF_RSS_QUERY_EN: u16 = 7;
const IFLA_VF_STATS: u16 = 8;
const IFLA_VF_TRUST: u16 = 9;
const IFLA_VF_IB_NODE_GUID: u16 = 10;
const IFLA_VF_IB_PORT_GUID: u16 = 11;
const IFLA_VF_VLAN_LIST: u16 = 12;
const IFLA_VF_BROADCAST: u16 = 13;

#[derive(Debug, Clone, Eq, PartialEq)]
#[non_exhaustive]
pub enum VfInfo {
    Mac(VfInfoMac),
    Broadcast(VfInfoBroadcast),
    Vlan(VfInfoVlan),
    Rate(VfInfoRate),
    TxRate(VfInfoTxRate),
    SpoofCheck(VfInfoSpoofCheck),
    LinkState(VfInfoLinkState),
    RssQueryEn(VfInfoRssQueryEn),
    Trust(VfInfoTrust),
    IbNodeGuid(VfInfoGuid),
    IbPortGuid(VfInfoGuid),
    VlanList(Vec<VfVlan>),
    Stats(Vec<VfStats>),
    Other(DefaultNla),
}

impl Nla for VfInfo {
    fn value_len(&self) -> usize {
        match self {
            Self::Mac(v) => v.buffer_len(),
            Self::Vlan(v) => v.buffer_len(),
            Self::Broadcast(v) => v.buffer_len(),
            Self::Rate(v) => v.buffer_len(),
            Self::TxRate(v) => v.buffer_len(),
            Self::SpoofCheck(v) => v.buffer_len(),
            Self::LinkState(v) => v.buffer_len(),
            Self::RssQueryEn(v) => v.buffer_len(),
            Self::Trust(v) => v.buffer_len(),
            Self::IbNodeGuid(v) => v.buffer_len(),
            Self::IbPortGuid(v) => v.buffer_len(),
            Self::VlanList(v) => v.as_slice().buffer_len(),
            Self::Stats(v) => v.as_slice().buffer_len(),
            Self::Other(v) => v.value_len(),
        }
    }

    fn emit_value(&self, buffer: &mut [u8]) {
        match self {
            Self::Mac(v) => v.emit(buffer),
            Self::Vlan(v) => v.emit(buffer),
            Self::Broadcast(v) => v.emit(buffer),
            Self::Rate(v) => v.emit(buffer),
            Self::TxRate(v) => v.emit(buffer),
            Self::SpoofCheck(v) => v.emit(buffer),
            Self::LinkState(v) => v.emit(buffer),
            Self::RssQueryEn(v) => v.emit(buffer),
            Self::Trust(v) => v.emit(buffer),
            Self::IbNodeGuid(v) => v.emit(buffer),
            Self::IbPortGuid(v) => v.emit(buffer),
            Self::VlanList(v) => v.as_slice().emit(buffer),
            Self::Stats(v) => v.as_slice().emit(buffer),
            Self::Other(attr) => attr.emit_value(buffer),
        }
    }

    fn kind(&self) -> u16 {
        match self {
            Self::Mac(_) => IFLA_VF_MAC,
            Self::Vlan(_) => IFLA_VF_VLAN,
            Self::Broadcast(_) => IFLA_VF_BROADCAST,
            Self::Rate(_) => IFLA_VF_RATE,
            Self::TxRate(_) => IFLA_VF_TX_RATE,
            Self::SpoofCheck(_) => IFLA_VF_SPOOFCHK,
            Self::LinkState(_) => IFLA_VF_LINK_STATE,
            Self::RssQueryEn(_) => IFLA_VF_RSS_QUERY_EN,
            Self::Trust(_) => IFLA_VF_TRUST,
            Self::IbNodeGuid(_) => IFLA_VF_IB_NODE_GUID,
            Self::IbPortGuid(_) => IFLA_VF_IB_PORT_GUID,
            Self::VlanList(_) => IFLA_VF_VLAN_LIST,
            Self::Stats(_) => IFLA_VF_STATS,
            Self::Other(v) => v.kind(),
        }
    }
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for VfInfo {
    fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
        let payload = buf.value();
        Ok(match buf.kind() {
            IFLA_VF_MAC => Self::Mac(
                VfInfoMac::parse(&VfInfoMacBuffer::new(payload))
                    .context(format!("invalid IFLA_VF_MAC {payload:?}"))?,
            ),
            IFLA_VF_VLAN => Self::Vlan(
                VfInfoVlan::parse(&VfInfoVlanBuffer::new(payload))
                    .context(format!("invalid IFLA_VF_VLAN {payload:?}"))?,
            ),
            IFLA_VF_BROADCAST => Self::Broadcast(
                VfInfoBroadcast::parse(&VfInfoBroadcastBuffer::new(payload))
                    .context(format!(
                        "invalid IFLA_VF_BROADCAST {payload:?}"
                    ))?,
            ),
            IFLA_VF_RATE => Self::Rate(
                VfInfoRate::parse(&VfInfoRateBuffer::new(payload))
                    .context(format!("invalid IFLA_VF_RATE {payload:?}"))?,
            ),
            IFLA_VF_TX_RATE => Self::TxRate(
                VfInfoTxRate::parse(&VfInfoTxRateBuffer::new(payload))
                    .context(format!("invalid IFLA_VF_TX_RATE {payload:?}"))?,
            ),
            IFLA_VF_SPOOFCHK => Self::SpoofCheck(
                VfInfoSpoofCheck::parse(&VfInfoSpoofCheckBuffer::new(payload))
                    .context(format!("invalid IFLA_VF_SPOOFCHK {payload:?}"))?,
            ),
            IFLA_VF_LINK_STATE => Self::LinkState(
                VfInfoLinkState::parse(&VfInfoLinkStateBuffer::new(payload))
                    .context(format!(
                        "invalid IFLA_VF_LINK_STATE {payload:?}"
                    ))?,
            ),
            IFLA_VF_RSS_QUERY_EN => Self::RssQueryEn(
                VfInfoRssQueryEn::parse(&VfInfoRssQueryEnBuffer::new(payload))
                    .context(format!(
                        "invalid IFLA_VF_RSS_QUERY_EN {payload:?}"
                    ))?,
            ),
            IFLA_VF_TRUST => Self::Trust(
                VfInfoTrust::parse(&VfInfoTrustBuffer::new(payload))
                    .context(format!("invalid IFLA_VF_TRUST {payload:?}"))?,
            ),
            IFLA_VF_IB_NODE_GUID => Self::IbNodeGuid(
                VfInfoGuid::parse(&VfInfoGuidBuffer::new(payload)).context(
                    format!("invalid IFLA_VF_IB_NODE_GUID {payload:?}"),
                )?,
            ),
            IFLA_VF_IB_PORT_GUID => Self::IbPortGuid(
                VfInfoGuid::parse(&VfInfoGuidBuffer::new(payload)).context(
                    format!("invalid IFLA_VF_IB_PORT_GUID {payload:?}"),
                )?,
            ),
            IFLA_VF_VLAN_LIST => {
                let mut nlas: Vec<VfVlan> = Vec::new();
                for nla in NlasIterator::new(payload) {
                    let nla = &nla.context(format!(
                        "invalid IFLA_VF_VLAN_LIST value: {:?}",
                        buf.value()
                    ))?;

                    nlas.push(VfVlan::parse(nla)?);
                }
                Self::VlanList(nlas)
            }
            IFLA_VF_STATS => {
                let mut nlas: Vec<VfStats> = Vec::new();
                for nla in NlasIterator::new(payload) {
                    let nla = &nla.context(format!(
                        "invalid IFLA_VF_STATS value: {:?}",
                        buf.value()
                    ))?;

                    nlas.push(VfStats::parse(nla)?);
                }
                Self::Stats(nlas)
            }
            kind => Self::Other(DefaultNla::parse(buf).context(format!(
                "failed to parse {kind} as DefaultNla: {payload:?}"
            ))?),
        })
    }
}