netlink_packet_route/link/
event.rs

1// SPDX-License-Identifier: MIT
2
3use anyhow::Context;
4use byteorder::{ByteOrder, NativeEndian};
5use netlink_packet_utils::{
6    parsers::parse_u32, DecodeError, Emitable, Parseable,
7};
8
9const IFLA_EVENT_NONE: u32 = 0;
10const IFLA_EVENT_REBOOT: u32 = 1;
11const IFLA_EVENT_FEATURES: u32 = 2;
12const IFLA_EVENT_BONDING_FAILOVER: u32 = 3;
13const IFLA_EVENT_NOTIFY_PEERS: u32 = 4;
14const IFLA_EVENT_IGMP_RESEND: u32 = 5;
15const IFLA_EVENT_BONDING_OPTIONS: u32 = 6;
16
17#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
18#[non_exhaustive]
19pub enum LinkEvent {
20    #[default]
21    None,
22    Reboot,
23    Features,
24    BondingFailover,
25    NotifyPeers,
26    IgmpResend,
27    BondingOptions,
28    Other(u32),
29}
30
31impl From<u32> for LinkEvent {
32    fn from(d: u32) -> Self {
33        match d {
34            IFLA_EVENT_NONE => Self::None,
35            IFLA_EVENT_REBOOT => Self::Reboot,
36            IFLA_EVENT_FEATURES => Self::Features,
37            IFLA_EVENT_BONDING_FAILOVER => Self::BondingFailover,
38            IFLA_EVENT_NOTIFY_PEERS => Self::NotifyPeers,
39            IFLA_EVENT_IGMP_RESEND => Self::IgmpResend,
40            IFLA_EVENT_BONDING_OPTIONS => Self::BondingOptions,
41            _ => Self::Other(d),
42        }
43    }
44}
45
46impl From<LinkEvent> for u32 {
47    fn from(v: LinkEvent) -> u32 {
48        match v {
49            LinkEvent::None => IFLA_EVENT_NONE,
50            LinkEvent::Reboot => IFLA_EVENT_REBOOT,
51            LinkEvent::Features => IFLA_EVENT_FEATURES,
52            LinkEvent::BondingFailover => IFLA_EVENT_BONDING_FAILOVER,
53            LinkEvent::NotifyPeers => IFLA_EVENT_NOTIFY_PEERS,
54            LinkEvent::IgmpResend => IFLA_EVENT_IGMP_RESEND,
55            LinkEvent::BondingOptions => IFLA_EVENT_BONDING_OPTIONS,
56            LinkEvent::Other(d) => d,
57        }
58    }
59}
60
61impl<T: AsRef<[u8]> + ?Sized> Parseable<T> for LinkEvent {
62    fn parse(buf: &T) -> Result<Self, DecodeError> {
63        Ok(LinkEvent::from(
64            parse_u32(buf.as_ref()).context("invalid IFLA_EVENT value")?,
65        ))
66    }
67}
68
69impl Emitable for LinkEvent {
70    fn buffer_len(&self) -> usize {
71        4
72    }
73
74    fn emit(&self, buffer: &mut [u8]) {
75        NativeEndian::write_u32(buffer, u32::from(*self));
76    }
77}