netlink_packet_route/link/
event.rs

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