netlink_packet_route/link/
down_reason.rs

1// SPDX-License-Identifier: MIT
2
3use netlink_packet_core::{
4    emit_u32, parse_u32, DecodeError, DefaultNla, ErrorContext, Nla, NlaBuffer,
5    Parseable,
6};
7
8const IFLA_PROTO_DOWN_REASON_MASK: u16 = 1;
9const IFLA_PROTO_DOWN_REASON_VALUE: u16 = 2;
10
11#[derive(Debug, PartialEq, Eq, Clone)]
12#[non_exhaustive]
13pub enum LinkProtocolDownReason {
14    Value(u32),
15    Mask(u32),
16    Other(DefaultNla),
17}
18
19impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
20    for LinkProtocolDownReason
21{
22    fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
23        let payload = buf.value();
24        Ok(match buf.kind() {
25            IFLA_PROTO_DOWN_REASON_MASK => {
26                Self::Mask(parse_u32(payload).context(format!(
27                    "invalid IFLA_PROTO_DOWN_REASON_MASK {payload:?}"
28                ))?)
29            }
30            IFLA_PROTO_DOWN_REASON_VALUE => {
31                Self::Value(parse_u32(payload).context(format!(
32                    "invalid IFLA_PROTO_DOWN_REASON_MASK {payload:?}"
33                ))?)
34            }
35            kind => Self::Other(DefaultNla::parse(buf).context(format!(
36                "unknown NLA type {kind} for IFLA_PROTO_DOWN_REASON: \
37                 {payload:?}"
38            ))?),
39        })
40    }
41}
42
43impl Nla for LinkProtocolDownReason {
44    fn kind(&self) -> u16 {
45        match self {
46            Self::Value(_) => IFLA_PROTO_DOWN_REASON_VALUE,
47            Self::Mask(_) => IFLA_PROTO_DOWN_REASON_MASK,
48            Self::Other(v) => v.kind(),
49        }
50    }
51
52    fn value_len(&self) -> usize {
53        match self {
54            Self::Value(_) | Self::Mask(_) => 4,
55            Self::Other(v) => v.value_len(),
56        }
57    }
58
59    fn emit_value(&self, buffer: &mut [u8]) {
60        match self {
61            Self::Value(v) | Self::Mask(v) => emit_u32(buffer, *v).unwrap(),
62            Self::Other(v) => v.emit_value(buffer),
63        }
64    }
65}