From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id DD05B1FF0B6 for ; Thu, 12 Nov 2026 18:11:43 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 96F8D216C8; Thu, 12 Nov 2026 18:11:24 +0100 (CET) From: Stefan Hanreich To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox 1/4] network-types: add a type for network interface names Date: Thu, 12 Nov 2026 18:10:57 +0100 Message-ID: <20261112171102.674232-2-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: 1794503476979 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.282 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: NIZDJQI4AA7NBS3KESZBEU7SU3DL76DH X-Message-ID-Hash: NIZDJQI4AA7NBS3KESZBEU7SU3DL76DH 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: There is no common type for network interface names yet, and the pve-iface format of the Perl JSON schema even accepts names that the kernel rejects, e.g. names longer than 15 bytes, while it rejects names with a '-' (bug #6366 [1]). NetworkInterfaceName accepts every name that the APIs of PVE, PBS and PDM accept and the kernel can use, plus a few that pve-iface rejects: names with a '-', names starting with a digit or '_', single-character names and names with more than one or a non-numeric dotted suffix, e.g. eth0.100.200. It uses the format of the network API of PBS and PDM. The api_string_type macro requires anyhow, so add it to the api-types feature. [1] https://bugzilla.proxmox.com/show_bug.cgi?id=6366 Signed-off-by: Stefan Hanreich --- proxmox-network-types/Cargo.toml | 4 +- proxmox-network-types/src/lib.rs | 2 + .../src/network_interface.rs | 102 ++++++++++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 proxmox-network-types/src/network_interface.rs diff --git a/proxmox-network-types/Cargo.toml b/proxmox-network-types/Cargo.toml index 1e1eacbf..b5ab5c5e 100644 --- a/proxmox-network-types/Cargo.toml +++ b/proxmox-network-types/Cargo.toml @@ -10,6 +10,7 @@ exclude.workspace = true rust-version.workspace = true [dependencies] +anyhow = { workspace = true, optional = true } regex = { workspace = true, optional = true } serde = { workspace = true, features = [ "derive", "std" ] } serde_with = "3.8.1" @@ -17,7 +18,8 @@ serde_plain.workspace = true thiserror.workspace = true proxmox-schema = { workspace = true, features = [ "api-macro", "api-types" ], optional = true} +proxmox-serde = { workspace = true, optional = true } [features] default = [] -api-types = ["dep:proxmox-schema", "dep:regex"] +api-types = ["dep:anyhow", "dep:proxmox-schema", "dep:proxmox-serde", "dep:regex"] diff --git a/proxmox-network-types/src/lib.rs b/proxmox-network-types/src/lib.rs index 6a5ddbf9..ebbabe76 100644 --- a/proxmox-network-types/src/lib.rs +++ b/proxmox-network-types/src/lib.rs @@ -9,3 +9,5 @@ pub use ip_address::*; pub mod mac_address; pub use mac_address::*; + +pub mod network_interface; diff --git a/proxmox-network-types/src/network_interface.rs b/proxmox-network-types/src/network_interface.rs new file mode 100644 index 00000000..24972058 --- /dev/null +++ b/proxmox-network-types/src/network_interface.rs @@ -0,0 +1,102 @@ +//! Types for network interface names. +//! +//! The API, the kernel and existing configurations accept different names, so each kind of name +//! has its own type, with explicit conversions between them. The API types are in `api_types` and +//! require the `api-types` feature. +//! +//! # Which type to use +//! +//! * `api_types::NetworkInterfaceName`: names in the API and in configuration files, and names +//! that are created or passed to `ip`. Not for values that can also be altnames, and not for +//! reading names from the kernel, which can be outside of the API format. + +/// API types for network interface names. +/// +/// [`NetworkInterfaceName`](api_types::NetworkInterfaceName) is the name of a network interface +/// as accepted by the API. It is meant for every network interface that our stack creates, +/// configures or references: +/// +/// ``` +/// # use proxmox_network_types::network_interface::api_types::NetworkInterfaceName; +/// let name: NetworkInterfaceName = "uplink-0".parse().unwrap(); +/// assert_eq!(name.as_str(), "uplink-0"); +/// +/// assert!("veth999999999i31".parse::().is_err()); +/// ``` +#[cfg(feature = "api-types")] +pub mod api_types { + use proxmox_schema::{api, api_string_type, api_types::SAFE_ID_FORMAT}; + + /// The size of the kernel buffer for network interface names, including the terminating NUL. + const IFNAMSIZ: usize = 16; + + api_string_type! { + #[api(format: &SAFE_ID_FORMAT, min_length: 1, max_length: IFNAMSIZ - 1)] + /// The name of a network interface. + /// + /// Uses the format of other names in the API, limited to the 15 bytes the kernel allows + /// for interface names. Every such name is valid for the kernel. + #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)] + #[repr(transparent)] + pub struct NetworkInterfaceName(String); + } + + proxmox_serde::forward_deserialize_to_from_str!(NetworkInterfaceName); + proxmox_serde::forward_serialize_to_display!(NetworkInterfaceName); + + #[cfg(test)] + mod tests { + use super::*; + + #[test] + fn parse_network_interface_name() { + for name in [ + "eth0", + "vmbr0", + "bond0.100", + "eth0.100.200", + "uplink-0", + "0eth", + "_eth0", + "a", + "tap100i0", + // 15 bytes is the longest name the kernel accepts + "abcdefghijklmno", + ] { + let parsed: NetworkInterfaceName = name.parse().expect(name); + assert_eq!(parsed.as_str(), name); + assert_eq!(parsed.to_string(), name); + } + + for name in [ + "", + ".", + "..", + ".eth0", + "-eth0", + "eth 0", + "eth0/1", + "eth0:1", + "eth\u{e9}", + "abcdefghijklmnop", + "veth999999999i31", + ] { + assert!(name.parse::().is_err(), "{name:?}"); + } + } + + #[test] + fn deserialize_network_interface_name() { + let name: NetworkInterfaceName = serde_plain::from_str("eth0").expect("valid name"); + assert_eq!(name.as_str(), "eth0"); + assert_eq!(serde_plain::to_string(&name).expect("serializable"), "eth0"); + + for name in ["eth:0", "abcdefghijklmnop"] { + assert!( + serde_plain::from_str::(name).is_err(), + "{name:?}" + ); + } + } + } +} -- 2.47.3