public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Stefan Hanreich <s.hanreich@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH proxmox 4/4] network-types: add a type for names of existing configurations
Date: Thu, 12 Nov 2026 18:11:00 +0100	[thread overview]
Message-ID: <20261112171102.674232-5-s.hanreich@proxmox.com> (raw)
In-Reply-To: <20261112171102.674232-1-s.hanreich@proxmox.com>

Existing configurations can contain names that were validated by the
pve-iface format of the Perl JSON schema, which accepts names that the
API types reject, e.g. alias labels like eth0:1, which name an
address and not an interface, or names longer than 127 characters.

LegacyNetworkInterfaceName accepts exactly the pve-iface format, so
such configurations can still be read, and converts into the API
types. It uses the regex of pve-iface.

Signed-off-by: Stefan Hanreich <s.hanreich@proxmox.com>
---
 .../src/network_interface.rs                  | 122 +++++++++++++++++-
 1 file changed, 121 insertions(+), 1 deletion(-)

diff --git a/proxmox-network-types/src/network_interface.rs b/proxmox-network-types/src/network_interface.rs
index afa8af2f..6977beed 100644
--- a/proxmox-network-types/src/network_interface.rs
+++ b/proxmox-network-types/src/network_interface.rs
@@ -19,6 +19,10 @@
 //!   `String`, since they do not have to be valid UTF-8, and convert them into the API types one
 //!   by one. Not for the API or configuration files, and not for names passed to `ip`, which
 //!   rejects some altnames that the kernel accepts.
+//!
+//! * `api_types::LegacyNetworkInterfaceName`: only for reading names from configurations that
+//!   were validated by the `pve-iface` format. Convert them into the API types before using them,
+//!   since the format also accepts alias labels like `eth0:1` and names the kernel rejects.
 
 use std::cmp::Ordering;
 use std::fmt;
@@ -379,11 +383,28 @@ kernel_name_impls!(KernelNetworkInterfaceAltname, "altname");
 /// let name: NetworkInterfaceName = "wg-mesh".parse().unwrap();
 /// assert_eq!(KernelNetworkInterfaceName::from(name).as_bytes(), b"wg-mesh");
 /// ```
+///
+/// Names from configurations validated by the `pve-iface` format are represented by
+/// [`LegacyNetworkInterfaceName`](api_types::LegacyNetworkInterfaceName), which converts into the
+/// API types, except for e.g. alias labels:
+///
+/// ```
+/// # use proxmox_network_types::network_interface::api_types::{
+/// #     LegacyNetworkInterfaceName, NetworkInterfaceAltname, NetworkInterfaceName,
+/// # };
+/// let legacy: LegacyNetworkInterfaceName = "eth0:1".parse().unwrap();
+/// assert!(NetworkInterfaceAltname::try_from(legacy).is_err());
+///
+/// let legacy: LegacyNetworkInterfaceName = "eth0".parse().unwrap();
+/// assert_eq!(NetworkInterfaceName::try_from(legacy).unwrap().as_str(), "eth0");
+/// ```
 #[cfg(feature = "api-types")]
 pub mod api_types {
     use anyhow::{Context, Error};
 
-    use proxmox_schema::{ApiType, api, api_string_type, api_types::SAFE_ID_FORMAT};
+    use proxmox_schema::{
+        ApiStringFormat, ApiType, api, api_string_type, api_types::SAFE_ID_FORMAT, const_regex,
+    };
 
     use super::{ALTIFNAMSIZ, IFNAMSIZ, KernelNetworkInterfaceAltname, KernelNetworkInterfaceName};
 
@@ -467,6 +488,46 @@ pub mod api_types {
         }
     }
 
+    const_regex! {
+        /// The `pve-iface` format of the Perl JSON schema.
+        LEGACY_NETWORK_INTERFACE_NAME_REGEX = r"^(?i)[a-z][a-z0-9_]{1,20}([:\.]\d+)?$";
+    }
+
+    const LEGACY_NETWORK_INTERFACE_NAME_FORMAT: ApiStringFormat =
+        ApiStringFormat::Pattern(&LEGACY_NETWORK_INTERFACE_NAME_REGEX);
+
+    api_string_type! {
+        #[api(format: &LEGACY_NETWORK_INTERFACE_NAME_FORMAT)]
+        /// The name of a network interface in an existing configuration, as accepted by the
+        /// pve-iface format of the Perl JSON schema.
+        ///
+        /// This includes names that are not valid for the kernel, e.g. names longer than 15 bytes
+        /// and alias labels such as `eth0:1`. Convert it into a network interface name or altname
+        /// before using it.
+        #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
+        #[repr(transparent)]
+        pub struct LegacyNetworkInterfaceName(String);
+    }
+
+    proxmox_serde::forward_deserialize_to_from_str!(LegacyNetworkInterfaceName);
+    proxmox_serde::forward_serialize_to_display!(LegacyNetworkInterfaceName);
+
+    impl TryFrom<LegacyNetworkInterfaceName> for NetworkInterfaceName {
+        type Error = Error;
+
+        fn try_from(name: LegacyNetworkInterfaceName) -> Result<Self, Self::Error> {
+            Self::from_string(name.into_string())
+        }
+    }
+
+    impl TryFrom<LegacyNetworkInterfaceName> for NetworkInterfaceAltname {
+        type Error = Error;
+
+        fn try_from(name: LegacyNetworkInterfaceName) -> Result<Self, Self::Error> {
+            Self::from_string(name.into_string())
+        }
+    }
+
     #[cfg(test)]
     mod tests {
         use super::*;
@@ -610,6 +671,65 @@ pub mod api_types {
                 );
             }
         }
+
+        #[test]
+        fn parse_legacy_network_interface_name() {
+            for name in [
+                "eth0",
+                "ETH0",
+                "eth0:1",
+                "bond0.100",
+                "ab",
+                "abcdefghijklmnopqrstu",
+                "abcdefghijklmnopqrstu.4094",
+            ] {
+                let parsed: LegacyNetworkInterfaceName = name.parse().expect(name);
+                assert_eq!(parsed.as_str(), name);
+            }
+
+            for name in [
+                "",
+                "a",
+                "0eth",
+                "_eth0",
+                "eth-0",
+                "eth0.1.2",
+                "eth0.",
+                "eth0:a",
+                "abcdefghijklmnopqrstuv",
+            ] {
+                assert!(
+                    name.parse::<LegacyNetworkInterfaceName>().is_err(),
+                    "{name:?}"
+                );
+            }
+        }
+
+        #[test]
+        fn convert_legacy_network_interface_name() {
+            for (name, is_name, is_altname) in [
+                ("eth0", true, true),
+                ("ETH0", true, true),
+                ("bond0.100", true, true),
+                ("abcdefghijklmno", true, true),
+                ("abcdefghijklmnop", false, true),
+                ("abcdefghijklmnopqrstu.4094", false, true),
+                ("eth0:1", false, false),
+            ] {
+                let legacy: LegacyNetworkInterfaceName = name.parse().expect(name);
+
+                assert_eq!(
+                    NetworkInterfaceName::try_from(legacy.clone()).is_ok(),
+                    is_name,
+                    "{name:?}"
+                );
+                assert_eq!(
+                    NetworkInterfaceAltname::try_from(legacy).is_ok(),
+                    is_altname,
+                    "{name:?}"
+                );
+            }
+        }
     }
 }
 
-- 
2.47.3





  parent reply	other threads:[~2026-11-12 17:11 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12 17:10 [PATCH proxmox 0/4] network-types: add types for network interface names Stefan Hanreich
2026-11-12 17:10 ` [PATCH proxmox 1/4] network-types: add a type " Stefan Hanreich
2026-11-12 17:10 ` [PATCH proxmox 2/4] network-types: add a type for network interface altnames Stefan Hanreich
2026-11-12 17:10 ` [PATCH proxmox 3/4] network-types: add types for names reported by the kernel Stefan Hanreich
2026-11-12 17:11 ` Stefan Hanreich [this message]
2026-11-12 17:36   ` [PATCH proxmox 4/4] network-types: add a type for names of existing configurations Stefan Hanreich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261112171102.674232-5-s.hanreich@proxmox.com \
    --to=s.hanreich@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal