netlink_packet_route/link/sriov/
vf_port.rs1use netlink_packet_core::{
4 DecodeError, DefaultNla, Emitable, ErrorContext, Nla, NlaBuffer,
5 NlasIterator, Parseable,
6};
7
8#[derive(Debug, Clone, Eq, PartialEq)]
9pub(crate) struct VecLinkVfPort(pub(crate) Vec<LinkVfPort>);
10
11impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
12 for VecLinkVfPort
13{
14 fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
15 let mut nlas = vec![];
16 for nla in NlasIterator::new(buf.into_inner()) {
17 let nla = &nla.context(format!(
18 "invalid IFLA_VF_PORTS value: {:?}",
19 buf.value()
20 ))?;
21 if nla.kind() == IFLA_VF_PORT {
22 nlas.push(LinkVfPort::parse(&NlaBuffer::new_checked(
23 nla.value(),
24 )?)?);
25 } else {
26 log::warn!(
27 "BUG: Expecting IFLA_VF_PORT in IFLA_VF_PORTS, but got {}",
28 nla.kind()
29 );
30 }
31 }
32 Ok(Self(nlas))
33 }
34}
35
36const IFLA_VF_PORT: u16 = 1;
37
38#[derive(Debug, Clone, Default, Eq, PartialEq)]
39pub struct LinkVfPort(pub Vec<VfPort>);
40
41impl Nla for LinkVfPort {
42 fn value_len(&self) -> usize {
43 self.0.as_slice().buffer_len()
44 }
45
46 fn emit_value(&self, buffer: &mut [u8]) {
47 self.0.as_slice().emit(buffer)
48 }
49
50 fn kind(&self) -> u16 {
51 IFLA_VF_PORT
52 }
53}
54
55impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for LinkVfPort {
56 fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
57 let mut nlas = vec![];
58 for nla in NlasIterator::new(buf.into_inner()) {
59 let nla = &nla.context(format!(
60 "invalid IFLA_VF_PORT value {:?}",
61 buf.value()
62 ))?;
63 nlas.push(VfPort::parse(nla)?);
64 }
65 Ok(Self(nlas))
66 }
67}
68
69#[derive(Debug, Clone, Eq, PartialEq)]
83#[non_exhaustive]
84pub enum VfPort {
85 Other(DefaultNla),
91}
92
93impl Nla for VfPort {
94 fn value_len(&self) -> usize {
95 match self {
96 Self::Other(v) => v.value_len(),
97 }
98 }
99
100 fn emit_value(&self, buffer: &mut [u8]) {
101 match self {
102 Self::Other(attr) => attr.emit_value(buffer),
103 }
104 }
105
106 fn kind(&self) -> u16 {
107 match self {
108 Self::Other(v) => v.kind(),
109 }
110 }
111}
112
113impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for VfPort {
114 fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
115 let payload = buf.value();
116 #[allow(clippy::match_single_binding)]
117 Ok(match buf.kind() {
118 kind => Self::Other(DefaultNla::parse(buf).context(format!(
119 "failed to parse {kind} as DefaultNla: {payload:?}"
120 ))?),
121 })
122 }
123}