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
// SPDX-License-Identifier: MIT

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

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

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
    for VecLinkVfPort
{
    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_PORTS value: {:?}",
                buf.value()
            ))?;
            if nla.kind() == IFLA_VF_PORT {
                nlas.push(LinkVfPort::parse(&NlaBuffer::new_checked(
                    nla.value(),
                )?)?);
            } else {
                log::warn!(
                    "BUG: Expecting IFLA_VF_PORT in IFLA_VF_PORTS, \
                    but got {}",
                    nla.kind()
                );
            }
        }
        Ok(Self(nlas))
    }
}

const IFLA_VF_PORT: u16 = 1;

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

impl Nla for LinkVfPort {
    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_PORT
    }
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for LinkVfPort {
    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_PORT value {:?}",
                buf.value()
            ))?;
            nlas.push(VfPort::parse(nla)?);
        }
        Ok(Self(nlas))
    }
}

/*
const IFLA_PORT_VF: u16 = 1;
const IFLA_PORT_PROFILE: u16 = 2;
// No kernel code is accepting or generating IFLA_PORT_VSI_TYPE.
// const IFLA_PORT_VSI_TYPE: u16 = 3;
const IFLA_PORT_INSTANCE_UUID: u16 = 4;
const IFLA_PORT_HOST_UUID: u16 = 5;
const IFLA_PORT_REQUEST: u16 = 6;
const IFLA_PORT_RESPONSE: u16 = 7;

const UUID_LEN: usize = 16;
*/

#[derive(Debug, Clone, Eq, PartialEq)]
#[non_exhaustive]
pub enum VfPort {
    //    Vf(u32),
    //    Profile(String),
    //    InstanceUuid([u8; UUID_LEN]),
    //    HostUuid([u8; UUID_LEN]),
    //    Request(u8),
    Other(DefaultNla),
}

impl Nla for VfPort {
    fn value_len(&self) -> usize {
        match self {
            Self::Other(v) => v.value_len(),
        }
    }

    fn emit_value(&self, buffer: &mut [u8]) {
        match self {
            Self::Other(attr) => attr.emit_value(buffer),
        }
    }

    fn kind(&self) -> u16 {
        match self {
            Self::Other(v) => v.kind(),
        }
    }
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for VfPort {
    fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
        let payload = buf.value();
        #[allow(clippy::match_single_binding)]
        Ok(match buf.kind() {
            kind => Self::Other(DefaultNla::parse(buf).context(format!(
                "failed to parse {kind} as DefaultNla: {payload:?}"
            ))?),
        })
    }
}