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!(
23                    "BUG: Got unknown VLAN protocol {}, treating as {}",
24                    d,
25                    Self::Ieee8021Q
26                );
27                Self::Ieee8021Q
28            }
29        }
30    }
31}
32
33impl From<VlanProtocol> for u16 {
34    fn from(v: VlanProtocol) -> u16 {
35        match v {
36            VlanProtocol::Ieee8021Q => ETH_P_8021Q,
37            VlanProtocol::Ieee8021Ad => ETH_P_8021AD,
38        }
39    }
40}
41
42impl std::fmt::Display for VlanProtocol {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        write!(
45            f,
46            "{}",
47            match self {
48                VlanProtocol::Ieee8021Q => "802.1q",
49                VlanProtocol::Ieee8021Ad => "802.1ad",
50            }
51        )
52    }
53}