From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 539C01FF0B6 for ; Thu, 12 Nov 2026 18:11:28 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id CB0682163D; Thu, 12 Nov 2026 18:11:23 +0100 (CET) From: Stefan Hanreich 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 Message-ID: <20261112171102.674232-5-s.hanreich@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261112171102.674232-1-s.hanreich@proxmox.com> References: <20261112171102.674232-1-s.hanreich@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794503477166 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.279 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: 27TADTYYBDTW4UN6D7BCBI3BAHJLGTFZ X-Message-ID-Hash: 27TADTYYBDTW4UN6D7BCBI3BAHJLGTFZ X-MailFrom: s.hanreich@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- .../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 for NetworkInterfaceName { + type Error = Error; + + fn try_from(name: LegacyNetworkInterfaceName) -> Result { + Self::from_string(name.into_string()) + } + } + + impl TryFrom for NetworkInterfaceAltname { + type Error = Error; + + fn try_from(name: LegacyNetworkInterfaceName) -> Result { + 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::().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