[pve-devel] [PATCH proxmox-ve-rs 03/17] network-types: add openfabric NET type

Gabriel Goller g.goller at proxmox.com
Fri Mar 28 18:12:52 CET 2025


The Network Entity Title is currently only used in openfabric, but it's
a generic construct, which in the future, will also be used in IS-IS
(when it's ported in rust).
Also include helpers that convert from IPv4 and IPv6 to NET by using the
Binary Coded Decimals (BCD). The IPv4 conversion is quite
straightforward, you just pad up the numbers and then split the address
in 3 quartets which is your SystemId. On IPv6 you take the last 3
segments, which can be directly converted into the NET address.

The NET is defined in a ISO standard and also briefly explained here:
https://datatracker.ietf.org/doc/html/rfc1142#section-7.1.1

Signed-off-by: Gabriel Goller <g.goller at proxmox.com>
---
 proxmox-network-types/src/lib.rs |   1 +
 proxmox-network-types/src/net.rs | 382 +++++++++++++++++++++++++++++++
 2 files changed, 383 insertions(+)
 create mode 100644 proxmox-network-types/src/net.rs

diff --git a/proxmox-network-types/src/lib.rs b/proxmox-network-types/src/lib.rs
index afceab018312..797084fec423 100644
--- a/proxmox-network-types/src/lib.rs
+++ b/proxmox-network-types/src/lib.rs
@@ -1,2 +1,3 @@
 pub mod hostname;
+pub mod net;
 pub mod openfabric;
diff --git a/proxmox-network-types/src/net.rs b/proxmox-network-types/src/net.rs
new file mode 100644
index 000000000000..97e019383bcc
--- /dev/null
+++ b/proxmox-network-types/src/net.rs
@@ -0,0 +1,382 @@
+use std::{
+    fmt::Display,
+    net::{IpAddr, Ipv4Addr, Ipv6Addr},
+    str::FromStr,
+};
+
+use serde::Serialize;
+use serde_with::{DeserializeFromStr, SerializeDisplay};
+use thiserror::Error;
+
+#[derive(Error, Debug)]
+pub enum NetError {
+    #[error("Some octets are missing")]
+    WrongLength,
+    #[error("The NET selector must be two characters wide and be 00")]
+    InvalidNetSelector,
+    #[error("Invalid AFI (wrong size or position)")]
+    InvalidAFI,
+    #[error("Invalid Area (wrong size or position)")]
+    InvalidArea,
+    #[error("Invalid SystemId (wrong size or position)")]
+    InvalidSystemId,
+}
+
+/// Address Family authority Identifier - 49 The AFI value 49 is what IS-IS (and openfabric) uses
+/// for private addressing.
+#[derive(
+    Debug, DeserializeFromStr, SerializeDisplay, Clone, Hash, PartialEq, Eq, PartialOrd, Ord,
+)]
+struct NetAFI(String);
+
+impl Default for NetAFI {
+    fn default() -> Self {
+        Self("49".to_owned())
+    }
+}
+
+impl Display for NetAFI {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "{}", self.0)
+    }
+}
+
+impl FromStr for NetAFI {
+    type Err = NetError;
+
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        if s.len() != 2 {
+            Err(NetError::InvalidAFI)
+        } else {
+            Ok(Self(s.to_owned()))
+        }
+    }
+}
+
+/// Area identifier: 0001 IS-IS area number (numerical area 1)
+/// The second part (system) of the `net` identifier. Every node has to have a different system
+/// number.
+#[derive(Debug, DeserializeFromStr, Serialize, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
+struct NetArea(String);
+
+impl Default for NetArea {
+    fn default() -> Self {
+        Self("0001".to_owned())
+    }
+}
+
+impl Display for NetArea {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "{}", self.0)
+    }
+}
+
+impl FromStr for NetArea {
+    type Err = NetError;
+
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        if s.len() != 4 {
+            Err(NetError::InvalidArea)
+        } else {
+            Ok(Self(s.to_owned()))
+        }
+    }
+}
+
+/// System identifier: 1921.6800.1002 - for system identifiers we recommend to use IP address or
+/// MAC address of the router itself. The way to construct this is to keep all of the zeroes of the
+/// router IP address, and then change the periods from being every three numbers to every four
+/// numbers. The address that is listed here is 192.168.1.2, which if expanded will turn into
+/// 192.168.001.002. Then all one has to do is move the dots to have four numbers instead of three.
+/// This gives us 1921.6800.1002.
+#[derive(Debug, DeserializeFromStr, Serialize, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
+struct NetSystemId(String);
+
+impl Display for NetSystemId {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "{}", self.0)
+    }
+}
+
+impl FromStr for NetSystemId {
+    type Err = NetError;
+
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        if s.split(".").count() != 3
+            || s.split(".").any(|segment| {
+                segment.len() != 4 || !segment.chars().all(|c| c.is_ascii_hexdigit())
+            })
+        {
+            Err(NetError::InvalidSystemId)
+        } else {
+            Ok(Self(s.to_owned()))
+        }
+    }
+}
+
+/// Convert IP-Address to a NET address with the default afi, area and selector values. Note that a
+/// valid Ipv4Addr is always a valid SystemId as well.
+impl From<Ipv4Addr> for NetSystemId {
+    fn from(value: Ipv4Addr) -> Self {
+        let octets = value.octets();
+
+        let system_id_str = format!(
+            "{:03}{:01}.{:02}{:02}.{:01}{:03}",
+            octets[0],
+            octets[1] / 100,
+            octets[1] % 100,
+            octets[2] / 10,
+            octets[2] % 10,
+            octets[3]
+        );
+
+        Self(system_id_str)
+    }
+}
+
+/// Convert IPv6-Address to a NET address with the default afi, area and selector values. Note that a
+/// valid Ipv6Addr is always a valid SystemId as well.
+impl From<Ipv6Addr> for NetSystemId {
+    fn from(value: Ipv6Addr) -> Self {
+        let segments = value.segments();
+        //
+        // Use the last 3 segments (out of 8) of the IPv6 address
+        let system_id_str = format!(
+            "{:04x}.{:04x}.{:04x}",
+            segments[5], segments[6], segments[7]
+        );
+
+        Self(system_id_str)
+    }
+}
+
+/// NET selector: 00 Must always be 00. This setting indicates “this system” or “local system.”
+#[derive(Debug, DeserializeFromStr, Serialize, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
+struct NetSelector(String);
+
+impl Default for NetSelector {
+    fn default() -> Self {
+        Self("00".to_owned())
+    }
+}
+
+impl Display for NetSelector {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "{}", self.0)
+    }
+}
+
+impl FromStr for NetSelector {
+    type Err = NetError;
+
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        if s.len() != 2 {
+            Err(NetError::InvalidNetSelector)
+        } else {
+            Ok(Self(s.to_owned()))
+        }
+    }
+}
+
+/// The OpenFabric Net.
+///
+/// Every OpenFabric node and fabric is identified through the NET. It has a network and a host
+/// part.
+/// The first part is the network part (also called area). The entire OpenFabric fabric has to have
+/// the same network part (area). The first number is the [`NetAFI`] and the second is the [`NetArea`].
+/// e.g.: "49.0001"
+/// The second part is the host part, which has to differ on every node in the fabric. It contains
+/// the [`NetSystemId`] and the [`NetSelector`].
+/// e.g.: "1921.6800.1002.00"
+#[derive(
+    Debug, DeserializeFromStr, SerializeDisplay, Clone, Hash, PartialEq, Eq, PartialOrd, Ord,
+)]
+pub struct Net {
+    afi: NetAFI,
+    area: NetArea,
+    system: NetSystemId,
+    selector: NetSelector,
+}
+
+impl FromStr for Net {
+    type Err = NetError;
+
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        if s.split(".").count() != 6 {
+            return Err(NetError::WrongLength);
+        }
+        let mut iter = s.split(".");
+        let afi = iter.next().ok_or(NetError::WrongLength)?;
+        let area = iter.next().ok_or(NetError::WrongLength)?;
+        let system = format!(
+            "{}.{}.{}",
+            iter.next().ok_or(NetError::WrongLength)?,
+            iter.next().ok_or(NetError::WrongLength)?,
+            iter.next().ok_or(NetError::WrongLength)?
+        );
+        let selector = iter.next().ok_or(NetError::WrongLength)?;
+        Ok(Self {
+            afi: afi.parse()?,
+            area: area.parse()?,
+            system: system.parse()?,
+            selector: selector.parse()?,
+        })
+    }
+}
+
+/// Default NET address for a given Ipv4Addr. This adds the default afi, area and selector to the
+/// address.
+impl From<Ipv4Addr> for Net {
+    fn from(value: Ipv4Addr) -> Self {
+        Self {
+            afi: NetAFI::default(),
+            area: NetArea::default(),
+            system: value.into(),
+            selector: NetSelector::default(),
+        }
+    }
+}
+
+/// Default NET address for a given Ipv6Addr. This adds the default afi, area and selector to the
+/// address.
+impl From<Ipv6Addr> for Net {
+    fn from(value: Ipv6Addr) -> Self {
+        Self {
+            afi: NetAFI::default(),
+            area: NetArea::default(),
+            system: value.into(),
+            selector: NetSelector::default(),
+        }
+    }
+}
+
+/// Default NET address for a given IpAddr (can be either Ipv4 or Ipv6). This adds the default afi,
+/// area and selector to the address.
+impl From<IpAddr> for Net {
+    fn from(value: IpAddr) -> Self {
+        Self {
+            afi: NetAFI::default(),
+            area: NetArea::default(),
+            system: match value {
+                IpAddr::V4(ipv4_addr) => ipv4_addr.into(),
+                IpAddr::V6(ipv6_addr) => ipv6_addr.into(),
+            },
+            selector: NetSelector::default(),
+        }
+    }
+}
+
+impl Display for Net {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(
+            f,
+            "{}.{}.{}.{}",
+            self.afi, self.area, self.system, self.selector
+        )
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_net_from_str() {
+        let input = "49.0001.1921.6800.1002.00";
+        let net = input.parse::<Net>().expect("this net should parse");
+        assert_eq!(net.afi, NetAFI("49".to_owned()));
+        assert_eq!(net.area, NetArea("0001".to_owned()));
+        assert_eq!(net.system, NetSystemId("1921.6800.1002".to_owned()));
+        assert_eq!(net.selector, NetSelector("00".to_owned()));
+
+        let input = "45.0200.0100.1001.ba1f.01";
+        let net = input.parse::<Net>().expect("this net should parse");
+        assert_eq!(net.afi, NetAFI("45".to_owned()));
+        assert_eq!(net.area, NetArea("0200".to_owned()));
+        assert_eq!(net.system, NetSystemId("0100.1001.ba1f".to_owned()));
+        assert_eq!(net.selector, NetSelector("01".to_owned()));
+    }
+
+    #[test]
+    fn test_net_from_str_failed() {
+        let input = "49.0001.1921.6800.1002.000";
+        assert!(matches!(
+            input.parse::<Net>(),
+            Err(NetError::InvalidNetSelector)
+        ));
+
+        let input = "49.0001.1921.6800.1002.00.00";
+        assert!(matches!(input.parse::<Net>(), Err(NetError::WrongLength)));
+
+        let input = "49.0001.1921.6800.10002.00";
+        assert!(matches!(
+            input.parse::<Net>(),
+            Err(NetError::InvalidSystemId)
+        ));
+
+        let input = "49.0001.1921.6800.1z02.00";
+        assert!(matches!(
+            input.parse::<Net>(),
+            Err(NetError::InvalidSystemId)
+        ));
+
+        let input = "409.0001.1921.6800.1002.00";
+        assert!(matches!(input.parse::<Net>(), Err(NetError::InvalidAFI)));
+
+        let input = "49.00001.1921.6800.1002.00";
+        assert!(matches!(input.parse::<Net>(), Err(NetError::InvalidArea)));
+    }
+
+    #[test]
+    fn test_net_display() {
+        let net = Net {
+            afi: NetAFI("49".to_owned()),
+            area: NetArea("0001".to_owned()),
+            system: NetSystemId("1921.6800.1002".to_owned()),
+            selector: NetSelector("00".to_owned()),
+        };
+        assert_eq!(format!("{net}"), "49.0001.1921.6800.1002.00");
+    }
+
+    #[test]
+    fn test_net_from_ipv4() {
+        let ip: Ipv4Addr = "192.168.1.100".parse().unwrap();
+        let net: Net = ip.into();
+        assert_eq!(format!("{net}"), "49.0001.1921.6800.1100.00");
+
+        let ip1: Ipv4Addr = "10.10.2.245".parse().unwrap();
+        let net1: Net = ip1.into();
+        assert_eq!(format!("{net1}"), "49.0001.0100.1000.2245.00");
+
+        let ip2: Ipv4Addr = "1.1.1.1".parse().unwrap();
+        let net2: Net = ip2.into();
+        assert_eq!(format!("{net2}"), "49.0001.0010.0100.1001.00");
+    }
+
+    #[test]
+    fn test_net_from_ipv6() {
+        // 2001:db8::1 -> [2001, 0db8, 0, 0, 0, 0, 0, 1]
+        // last 3 segments: [0, 0, 1]
+        let ip: Ipv6Addr = "2001:db8::1".parse().unwrap();
+        let net: Net = ip.into();
+        assert_eq!(format!("{net}"), "49.0001.0000.0000.0001.00");
+
+        // fe80::1234:5678:abcd -> [fe80, 0, 0, 0, 0, 1234, 5678, abcd]
+        // last 3 segments: [1234, 5678, abcd]
+        let ip1: Ipv6Addr = "fe80::1234:5678:abcd".parse().unwrap();
+        let net1: Net = ip1.into();
+        assert_eq!(format!("{net1}"), "49.0001.1234.5678.abcd.00");
+
+        // 2001:0db8:85a3::8a2e:370:7334 -> [2001, 0db8, 85a3, 0, 0, 8a2e, 0370, 7334]
+        // last 3 segments: [8a2e, 0370, 7334]
+        let ip2: Ipv6Addr = "2001:0db8:85a3::8a2e:370:7334".parse().unwrap();
+        let net2: Net = ip2.into();
+        assert_eq!(format!("{net2}"), "49.0001.8a2e.0370.7334.00");
+
+        // ::1 -> [0, 0, 0, 0, 0, 0, 0, 1]
+        // last 3 segments: [0, 0, 1]
+        let ip3: Ipv6Addr = "::1".parse().unwrap();
+        let net3: Net = ip3.into();
+        assert_eq!(format!("{net3}"), "49.0001.0000.0000.0001.00");
+    }
+}
-- 
2.39.5





More information about the pve-devel mailing list