network_types/
vxlan.rs

1use core::mem;
2
3use crate::bitfield::BitfieldUnit;
4
5/// VXLAN header, which is present at the beginning of every UDP payload containing VXLAN packets.
6#[repr(C, packed)]
7#[derive(Debug, Copy, Clone)]
8#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
9pub struct VxlanHdr {
10    /// VXLAN flags. See [`VxlanHdr::vni_valid`] and [`VxlanHdr::set_vni_valid`].
11    pub flags: BitfieldUnit<[u8; 1usize]>,
12    pub _reserved: [u8; 3],
13    /// VXLAN Virtual Network Identifier.
14    ///
15    /// This is a 24-bit number combined with reserved bytes, see [`VxlanHdr::vni`] and
16    /// [`VxlanHdr::set_vni`].
17    pub vni: u32,
18}
19
20impl VxlanHdr {
21    pub const LEN: usize = mem::size_of::<Self>();
22
23    #[inline]
24    pub fn vni_valid(&self) -> bool {
25        self.flags.get_bit(4)
26    }
27
28    #[inline]
29    pub fn set_vni_valid(&mut self, val: bool) {
30        self.flags.set_bit(4, val)
31    }
32
33    #[inline]
34    pub fn vni(&self) -> u32 {
35        u32::from_be(self.vni) >> 8
36    }
37
38    #[inline]
39    pub fn set_vni(&mut self, vni: u32) -> u32 {
40        (vni << 8).to_be()
41    }
42}