netlink_packet_route/link/sriov/
vf_port.rsuse anyhow::Context;
use netlink_packet_utils::nla::{DefaultNla, Nla, NlaBuffer, NlasIterator};
use netlink_packet_utils::{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 {
type Error = DecodeError;
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 {
type Error = DecodeError;
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))
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
#[non_exhaustive]
pub enum VfPort {
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 {
type Error = DecodeError;
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:?}"))?,
),
})
}
}