Skip to main content

etherip_xdp_common/
lib.rs

1#![no_std]
2
3pub mod vlan {
4    /// VLAN ID 0 is for the remote IPv6 address of the native VLAN
5    pub const VLAN_ID_NATIVE: u16 = 0;
6
7    /// VLAN IDs 1-4094 are for remote IPv6 addresses of the bridged VLANs
8    pub const VLAN_ID_MIN: u16 = 1;
9
10    /// VLAN IDs 1-4094 are for remote IPv6 addresses of the bridged VLANs
11    pub const VLAN_ID_MAX: u16 = 4094;
12
13    /// 'VLAN ID' 4095 is reserved for the local IPv6 address
14    pub const VLAN_ID_LOCAL: u16 = 4095;
15}
16
17pub mod iface {
18    pub const IF_INDEX_INNER: u32 = 1;
19    pub const IF_INDEX_OUTER: u32 = 2;
20}
21
22pub mod mac {
23    /// MAC address of the local interface
24    pub const MAC_ADDR_LOCAL: u32 = 1;
25
26    /// MAC address of the next hop
27    pub const MAC_ADDR_GATEWAY: u32 = 2;
28
29    pub fn to_u64(mac: &[u8; 6]) -> u64 {
30        let mut mac_u64 = 0u64;
31        for i in 0..6 {
32            mac_u64 |= (mac[i] as u64) << (i * 8);
33        }
34        mac_u64
35    }
36
37    pub fn from_u64(mac: u64) -> [u8; 6] {
38        let mut mac_arr = [0u8; 6];
39        for i in 0..6 {
40            mac_arr[i] = ((mac >> (i * 8)) & 0xff) as u8;
41        }
42        mac_arr
43    }
44}
45
46pub mod ipv6 {
47    pub fn to_u128(addr: [u8; 16]) -> u128 {
48        u128::from_be_bytes(addr)
49    }
50
51    pub fn from_u128(addr: u128) -> [u8; 16] {
52        addr.to_be_bytes()
53    }
54}