netlink_packet_route/link/
vlan_protocol.rs

1// SPDX-License-Identifier: MIT
2
3const ETH_P_8021Q: u16 = 0x8100;
4const ETH_P_8021AD: u16 = 0x88A8;
5
6#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
7#[non_exhaustive]
8#[repr(u16)]
9// VLAN protocol seldom add new, so no Other for this enum.
10pub enum VlanProtocol {
11    #[default]
12    Ieee8021Q = ETH_P_8021Q,
13    Ieee8021Ad = ETH_P_8021AD,
14}
15
16impl From<u16> for VlanProtocol {
17    fn from(d: u16) -> Self {
18        match d {
19            ETH_P_8021Q => Self::Ieee8021Q,
20            ETH_P_8021AD => Self::Ieee8021Ad,
21            _ => {
22                log::warn!("BUG: Got unknown VLAN protocol {}, treating as {}", d, Self::Ieee8021Q);
23                Self::Ieee8021Q
24            }
25        }
26    }
27}
28
29impl From<VlanProtocol> for u16 {
30    fn from(v: VlanProtocol) -> u16 {
31        match v {
32            VlanProtocol::Ieee8021Q => ETH_P_8021Q,
33            VlanProtocol::Ieee8021Ad => ETH_P_8021AD,
34        }
35    }
36}
37
38impl std::fmt::Display for VlanProtocol {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        write!(
41            f,
42            "{}",
43            match self {
44                VlanProtocol::Ieee8021Q => "802.1q",
45                VlanProtocol::Ieee8021Ad => "802.1ad",
46            }
47        )
48    }
49}